Skip to content

Commit 554c42b

Browse files
montjoieherbertx
authored andcommitted
crypto: sun4i-ss - enable pm_runtime
This patch enables power management on the Security System. sun4i-ss now depends on PM because it simplify code and prevent some ifdef. But this is not a problem since arch maintainer want ARCH_SUNXI to depend on PM in the future. Signed-off-by: Corentin Labbe <clabbe.montjoie@gmail.com> Acked-by: Maxime Ripard <mripard@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 70dd444 commit 554c42b

6 files changed

Lines changed: 97 additions & 19 deletions

File tree

‎drivers/crypto/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ config CRYPTO_DEV_IMGTEC_HASH
660660
config CRYPTO_DEV_SUN4I_SS
661661
tristate "Support for Allwinner Security System cryptographic accelerator"
662662
depends on ARCH_SUNXI && !64BIT
663+
depends on PM
663664
select CRYPTO_MD5
664665
select CRYPTO_SHA1
665666
select CRYPTO_AES

‎drivers/crypto/sunxi-ss/sun4i-ss-cipher.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,7 @@ int sun4i_ss_cipher_init(struct crypto_tfm *tfm)
480480
struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
481481
struct sun4i_ss_alg_template *algt;
482482
const char *name = crypto_tfm_alg_name(tfm);
483+
int err;
483484

484485
memset(op, 0, sizeof(struct sun4i_tfm_ctx));
485486

@@ -497,13 +498,22 @@ int sun4i_ss_cipher_init(struct crypto_tfm *tfm)
497498
return PTR_ERR(op->fallback_tfm);
498499
}
499500

501+
err = pm_runtime_get_sync(op->ss->dev);
502+
if (err < 0)
503+
goto error_pm;
504+
500505
return 0;
506+
error_pm:
507+
crypto_free_sync_skcipher(op->fallback_tfm);
508+
return err;
501509
}
502510

503511
void sun4i_ss_cipher_exit(struct crypto_tfm *tfm)
504512
{
505513
struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
514+
506515
crypto_free_sync_skcipher(op->fallback_tfm);
516+
pm_runtime_put(op->ss->dev);
507517
}
508518

509519
/* check and set the AES key, prepare the mode to be used */

‎drivers/crypto/sunxi-ss/sun4i-ss-core.c‎

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ static struct sun4i_ss_alg_template ss_algs[] = {
4444
.cra_blocksize = MD5_HMAC_BLOCK_SIZE,
4545
.cra_ctxsize = sizeof(struct sun4i_req_ctx),
4646
.cra_module = THIS_MODULE,
47-
.cra_init = sun4i_hash_crainit
47+
.cra_init = sun4i_hash_crainit,
48+
.cra_exit = sun4i_hash_craexit,
4849
}
4950
}
5051
}
@@ -70,7 +71,8 @@ static struct sun4i_ss_alg_template ss_algs[] = {
7071
.cra_blocksize = SHA1_BLOCK_SIZE,
7172
.cra_ctxsize = sizeof(struct sun4i_req_ctx),
7273
.cra_module = THIS_MODULE,
73-
.cra_init = sun4i_hash_crainit
74+
.cra_init = sun4i_hash_crainit,
75+
.cra_exit = sun4i_hash_craexit,
7476
}
7577
}
7678
}
@@ -223,17 +225,26 @@ static struct sun4i_ss_alg_template ss_algs[] = {
223225
#endif
224226
};
225227

226-
static void sun4i_ss_disable(struct sun4i_ss_ctx *ss)
228+
/*
229+
* Power management strategy: The device is suspended unless a TFM exists for
230+
* one of the algorithms proposed by this driver.
231+
*/
232+
static int sun4i_ss_pm_suspend(struct device *dev)
227233
{
234+
struct sun4i_ss_ctx *ss = dev_get_drvdata(dev);
235+
228236
if (ss->reset)
229237
reset_control_assert(ss->reset);
230238

231239
clk_disable_unprepare(ss->ssclk);
232240
clk_disable_unprepare(ss->busclk);
241+
return 0;
233242
}
234243

235-
static int sun4i_ss_enable(struct sun4i_ss_ctx *ss)
244+
static int sun4i_ss_pm_resume(struct device *dev)
236245
{
246+
struct sun4i_ss_ctx *ss = dev_get_drvdata(dev);
247+
237248
int err;
238249

239250
err = clk_prepare_enable(ss->busclk);
@@ -258,10 +269,38 @@ static int sun4i_ss_enable(struct sun4i_ss_ctx *ss)
258269

259270
return err;
260271
err_enable:
261-
sun4i_ss_disable(ss);
272+
sun4i_ss_pm_suspend(dev);
273+
return err;
274+
}
275+
276+
const struct dev_pm_ops sun4i_ss_pm_ops = {
277+
SET_RUNTIME_PM_OPS(sun4i_ss_pm_suspend, sun4i_ss_pm_resume, NULL)
278+
};
279+
280+
/*
281+
* When power management is enabled, this function enables the PM and set the
282+
* device as suspended
283+
* When power management is disabled, this function just enables the device
284+
*/
285+
static int sun4i_ss_pm_init(struct sun4i_ss_ctx *ss)
286+
{
287+
int err;
288+
289+
pm_runtime_use_autosuspend(ss->dev);
290+
pm_runtime_set_autosuspend_delay(ss->dev, 2000);
291+
292+
err = pm_runtime_set_suspended(ss->dev);
293+
if (err)
294+
return err;
295+
pm_runtime_enable(ss->dev);
262296
return err;
263297
}
264298

299+
static void sun4i_ss_pm_exit(struct sun4i_ss_ctx *ss)
300+
{
301+
pm_runtime_disable(ss->dev);
302+
}
303+
265304
static int sun4i_ss_probe(struct platform_device *pdev)
266305
{
267306
u32 v;
@@ -308,18 +347,14 @@ static int sun4i_ss_probe(struct platform_device *pdev)
308347
ss->reset = NULL;
309348
}
310349

311-
err = sun4i_ss_enable(ss);
312-
if (err)
313-
goto error_enable;
314-
315350
/*
316351
* Check that clock have the correct rates given in the datasheet
317352
* Try to set the clock to the maximum allowed
318353
*/
319354
err = clk_set_rate(ss->ssclk, cr_mod);
320355
if (err) {
321356
dev_err(&pdev->dev, "Cannot set clock rate to ssclk\n");
322-
goto error_enable;
357+
return err;
323358
}
324359

325360
/*
@@ -347,22 +382,34 @@ static int sun4i_ss_probe(struct platform_device *pdev)
347382
dev_warn(&pdev->dev, "Clock ss is at %lu (%lu MHz) (must be <= %lu)\n",
348383
cr, cr / 1000000, cr_mod);
349384

385+
ss->dev = &pdev->dev;
386+
platform_set_drvdata(pdev, ss);
387+
388+
spin_lock_init(&ss->slock);
389+
390+
err = sun4i_ss_pm_init(ss);
391+
if (err)
392+
return err;
393+
350394
/*
351395
* Datasheet named it "Die Bonding ID"
352396
* I expect to be a sort of Security System Revision number.
353397
* Since the A80 seems to have an other version of SS
354398
* this info could be useful
355399
*/
400+
401+
err = pm_runtime_get_sync(ss->dev);
402+
if (err < 0)
403+
goto error_pm;
404+
356405
writel(SS_ENABLED, ss->base + SS_CTL);
357406
v = readl(ss->base + SS_CTL);
358407
v >>= 16;
359408
v &= 0x07;
360409
dev_info(&pdev->dev, "Die ID %d\n", v);
361410
writel(0, ss->base + SS_CTL);
362411

363-
ss->dev = &pdev->dev;
364-
365-
spin_lock_init(&ss->slock);
412+
pm_runtime_put_sync(ss->dev);
366413

367414
for (i = 0; i < ARRAY_SIZE(ss_algs); i++) {
368415
ss_algs[i].ss = ss;
@@ -392,7 +439,6 @@ static int sun4i_ss_probe(struct platform_device *pdev)
392439
break;
393440
}
394441
}
395-
platform_set_drvdata(pdev, ss);
396442
return 0;
397443
error_alg:
398444
i--;
@@ -409,8 +455,8 @@ static int sun4i_ss_probe(struct platform_device *pdev)
409455
break;
410456
}
411457
}
412-
error_enable:
413-
sun4i_ss_disable(ss);
458+
error_pm:
459+
sun4i_ss_pm_exit(ss);
414460
return err;
415461
}
416462

@@ -433,8 +479,7 @@ static int sun4i_ss_remove(struct platform_device *pdev)
433479
}
434480
}
435481

436-
writel(0, ss->base + SS_CTL);
437-
sun4i_ss_disable(ss);
482+
sun4i_ss_pm_exit(ss);
438483
return 0;
439484
}
440485

@@ -449,6 +494,7 @@ static struct platform_driver sun4i_ss_driver = {
449494
.remove = sun4i_ss_remove,
450495
.driver = {
451496
.name = "sun4i-ss",
497+
.pm = &sun4i_ss_pm_ops,
452498
.of_match_table = a20ss_crypto_of_match_table,
453499
},
454500
};

‎drivers/crypto/sunxi-ss/sun4i-ss-hash.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,29 @@ int sun4i_hash_crainit(struct crypto_tfm *tfm)
1919
struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
2020
struct ahash_alg *alg = __crypto_ahash_alg(tfm->__crt_alg);
2121
struct sun4i_ss_alg_template *algt;
22+
int err;
2223

2324
memset(op, 0, sizeof(struct sun4i_tfm_ctx));
2425

2526
algt = container_of(alg, struct sun4i_ss_alg_template, alg.hash);
2627
op->ss = algt->ss;
2728

29+
err = pm_runtime_get_sync(op->ss->dev);
30+
if (err < 0)
31+
return err;
32+
2833
crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm),
2934
sizeof(struct sun4i_req_ctx));
3035
return 0;
3136
}
3237

38+
void sun4i_hash_craexit(struct crypto_tfm *tfm)
39+
{
40+
struct sun4i_tfm_ctx *op = crypto_tfm_ctx(tfm);
41+
42+
pm_runtime_put(op->ss->dev);
43+
}
44+
3345
/* sun4i_hash_init: initialize request context */
3446
int sun4i_hash_init(struct ahash_request *areq)
3547
{

‎drivers/crypto/sunxi-ss/sun4i-ss-prng.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
1717
{
1818
struct sun4i_ss_alg_template *algt;
1919
struct rng_alg *alg = crypto_rng_alg(tfm);
20-
int i;
20+
int i, err;
2121
u32 v;
2222
u32 *data = (u32 *)dst;
2323
const u32 mode = SS_OP_PRNG | SS_PRNG_CONTINUE | SS_ENABLED;
@@ -28,6 +28,10 @@ int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
2828
algt = container_of(alg, struct sun4i_ss_alg_template, alg.rng);
2929
ss = algt->ss;
3030

31+
err = pm_runtime_get_sync(ss->dev);
32+
if (err < 0)
33+
return err;
34+
3135
spin_lock_bh(&ss->slock);
3236

3337
writel(mode, ss->base + SS_CTL);
@@ -52,5 +56,8 @@ int sun4i_ss_prng_generate(struct crypto_rng *tfm, const u8 *src,
5256

5357
writel(0, ss->base + SS_CTL);
5458
spin_unlock_bh(&ss->slock);
59+
60+
pm_runtime_put(ss->dev);
61+
5562
return 0;
5663
}

‎drivers/crypto/sunxi-ss/sun4i-ss.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/scatterlist.h>
2323
#include <linux/interrupt.h>
2424
#include <linux/delay.h>
25+
#include <linux/pm_runtime.h>
2526
#include <crypto/md5.h>
2627
#include <crypto/skcipher.h>
2728
#include <crypto/sha.h>
@@ -177,6 +178,7 @@ struct sun4i_req_ctx {
177178
};
178179

179180
int sun4i_hash_crainit(struct crypto_tfm *tfm);
181+
void sun4i_hash_craexit(struct crypto_tfm *tfm);
180182
int sun4i_hash_init(struct ahash_request *areq);
181183
int sun4i_hash_update(struct ahash_request *areq);
182184
int sun4i_hash_final(struct ahash_request *areq);

0 commit comments

Comments
 (0)