diff mbox series

[14/15] crypto: ux500/hash: Implement runtime PM

Message ID 20220721134050.1047866-15-linus.walleij@linaro.org (mailing list archive)
State Superseded
Delegated to: Herbert Xu
Headers show
Series Ux500 hash cleanup | expand

Commit Message

Linus Walleij July 21, 2022, 1:40 p.m. UTC
This implements runtime PM to gate the clock and regulator
when the hash block is unused.

Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/crypto/ux500/hash/hash_alg.h  |  1 +
 drivers/crypto/ux500/hash/hash_core.c | 60 +++++++++++++++++++++++----
 2 files changed, 52 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/drivers/crypto/ux500/hash/hash_alg.h b/drivers/crypto/ux500/hash/hash_alg.h
index 96c614444fa2..57e4c7df4ac4 100644
--- a/drivers/crypto/ux500/hash/hash_alg.h
+++ b/drivers/crypto/ux500/hash/hash_alg.h
@@ -16,6 +16,7 @@ 
 #define HASH_DMA_ALIGN_SIZE		4
 #define HASH_DMA_PERFORMANCE_MIN_SIZE	1024
 #define HASH_BYTES_PER_WORD		4
+#define UX500_HASH_AUTOSUSPEND_DELAY_MS	50
 
 /* Number of context swap registers */
 #define HASH_CSR_COUNT			52
diff --git a/drivers/crypto/ux500/hash/hash_core.c b/drivers/crypto/ux500/hash/hash_core.c
index a64edfb1cd96..55d27af8c9de 100644
--- a/drivers/crypto/ux500/hash/hash_core.c
+++ b/drivers/crypto/ux500/hash/hash_core.c
@@ -24,6 +24,7 @@ 
 #include <linux/module.h>
 #include <linux/mod_devicetable.h>
 #include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
 #include <linux/regmap.h>
 #include <linux/crypto.h>
 
@@ -438,6 +439,10 @@  static int ux500_hash_init(struct ahash_request *req)
 	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
 	struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
 	struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
+	struct hash_device_data *device_data = ctx->device;
+
+	/* Power up on init() power down on final() */
+	pm_runtime_get_sync(device_data->dev);
 
 	if (!ctx->key)
 		ctx->keylen = 0;
@@ -463,6 +468,7 @@  static int ux500_hash_init(struct ahash_request *req)
 			}
 		}
 	}
+
 	return 0;
 }
 
@@ -773,6 +779,8 @@  static int hash_dma_final(struct ahash_request *req)
 	memcpy(req->result, digest, ctx->digestsize);
 
 out:
+	pm_runtime_mark_last_busy(device_data->dev);
+	pm_runtime_put_autosuspend(device_data->dev);
 	/**
 	 * Allocated in setkey, and only used in HMAC.
 	 */
@@ -1023,8 +1031,11 @@  static int ahash_update(struct ahash_request *req)
  */
 static int ahash_final(struct ahash_request *req)
 {
-	int ret = 0;
+	struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
+	struct hash_ctx *ctx = crypto_ahash_ctx(tfm);
+	struct hash_device_data *device_data = ctx->device;
 	struct hash_req_ctx *req_ctx = ahash_request_ctx(req);
+	int ret = 0;
 
 	pr_debug("%s: data size: %d\n", __func__, req->nbytes);
 
@@ -1033,6 +1044,9 @@  static int ahash_final(struct ahash_request *req)
 	else
 		ret = hash_hw_final(req);
 
+	pm_runtime_mark_last_busy(device_data->dev);
+	pm_runtime_put_autosuspend(device_data->dev);
+
 	if (ret) {
 		pr_err("%s: hash_hw/dma_final() failed\n", __func__);
 	}
@@ -1133,6 +1147,9 @@  static int ahash_import(struct ahash_request *req, const void *in)
 	req_ctx->hw_initialized = hstate->hw_initialized;
 	memcpy(req_ctx->buffer, hstate->buffer, HASH_BLOCK_SIZE);
 
+	/* Power up as we may have lost power being exported */
+	pm_runtime_get_sync(device_data->dev);
+
 	/*
 	 * Restore hardware state
 	 * INIT bit. Set this bit to 0b1 to reset the HASH processor core and
@@ -1200,6 +1217,10 @@  static int ahash_export(struct ahash_request *req, void *out)
 	hstate->hw_initialized = req_ctx->hw_initialized;
 	memcpy(hstate->buffer, req_ctx->buffer, HASH_BLOCK_SIZE);
 
+	/* We can power down while exported */
+	pm_runtime_mark_last_busy(device_data->dev);
+	pm_runtime_put_autosuspend(device_data->dev);
+
 	return 0;
 }
 
@@ -1554,7 +1575,6 @@  static int ux500_hash_probe(struct platform_device *pdev)
 	spin_lock_init(&device_data->ctx_lock);
 	spin_lock_init(&device_data->power_state_lock);
 
-	/* Enable power for HASH1 hardware block */
 	device_data->regulator = regulator_get(dev, "v-ape");
 	if (IS_ERR(device_data->regulator)) {
 		dev_err(dev, "%s: regulator_get() failed!\n", __func__);
@@ -1563,7 +1583,6 @@  static int ux500_hash_probe(struct platform_device *pdev)
 		goto out;
 	}
 
-	/* Enable the clock for HASH1 hardware block */
 	device_data->clk = devm_clk_get(dev, NULL);
 	if (IS_ERR(device_data->clk)) {
 		dev_err(dev, "%s: clk_get() failed!\n", __func__);
@@ -1590,6 +1609,12 @@  static int ux500_hash_probe(struct platform_device *pdev)
 		goto out_power;
 	}
 
+	pm_runtime_set_autosuspend_delay(dev, UX500_HASH_AUTOSUSPEND_DELAY_MS);
+	pm_runtime_use_autosuspend(dev);
+	pm_runtime_get_noresume(dev);
+	pm_runtime_set_active(dev);
+	pm_runtime_enable(dev);
+
 	if (hash_mode == HASH_MODE_DMA)
 		hash_dma_setup_channel(device_data, dev);
 
@@ -1602,6 +1627,7 @@  static int ux500_hash_probe(struct platform_device *pdev)
 		goto out_power;
 	}
 
+	pm_runtime_put_sync(dev);
 	dev_info(dev, "successfully registered\n");
 	return 0;
 
@@ -1647,6 +1673,10 @@  static int ux500_hash_remove(struct platform_device *pdev)
 
 	ahash_algs_unregister_all(device_data);
 
+	pm_runtime_get_sync(device_data->dev);
+	pm_runtime_put_noidle(device_data->dev);
+	pm_runtime_disable(device_data->dev);
+
 	if (hash_disable_power(device_data))
 		dev_err(dev, "%s: hash_disable_power() failed\n",
 			__func__);
@@ -1687,6 +1717,10 @@  static void ux500_hash_shutdown(struct platform_device *pdev)
 
 	ahash_algs_unregister_all(device_data);
 
+	pm_runtime_get_sync(device_data->dev);
+	pm_runtime_put_noidle(device_data->dev);
+	pm_runtime_disable(device_data->dev);
+
 	if (hash_disable_power(device_data))
 		dev_err(&pdev->dev, "%s: hash_disable_power() failed\n",
 			__func__);
@@ -1694,10 +1728,10 @@  static void ux500_hash_shutdown(struct platform_device *pdev)
 
 #ifdef CONFIG_PM_SLEEP
 /**
- * ux500_hash_suspend - Function that suspends the hash device.
+ * ux500_hash_runtime_suspend - Function that suspends the hash device.
  * @dev:	Device to suspend.
  */
-static int ux500_hash_suspend(struct device *dev)
+static int ux500_hash_runtime_suspend(struct device *dev)
 {
 	int ret;
 	struct hash_device_data *device_data;
@@ -1712,14 +1746,15 @@  static int ux500_hash_suspend(struct device *dev)
 	if (ret)
 		dev_err(dev, "%s: hash_disable_power()\n", __func__);
 
+	dev_info(dev, "runtime suspended\n");
 	return ret;
 }
 
 /**
- * ux500_hash_resume - Function that resume the hash device.
+ * ux500_hash_runtime_resume - Function that resume the hash device.
  * @dev:	Device to resume.
  */
-static int ux500_hash_resume(struct device *dev)
+static int ux500_hash_runtime_resume(struct device *dev)
 {
 	int ret = 0;
 	struct hash_device_data *device_data;
@@ -1734,11 +1769,18 @@  static int ux500_hash_resume(struct device *dev)
 	if (ret)
 		dev_err(dev, "%s: hash_enable_power() failed!\n", __func__);
 
+	dev_info(dev, "runtime resumed\n");
+
 	return ret;
 }
 #endif
 
-static SIMPLE_DEV_PM_OPS(ux500_hash_pm, ux500_hash_suspend, ux500_hash_resume);
+static const struct dev_pm_ops ux500_hash_pm_ops = {
+	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
+				pm_runtime_force_resume)
+	SET_RUNTIME_PM_OPS(ux500_hash_runtime_suspend,
+			   ux500_hash_runtime_resume, NULL)
+};
 
 static const struct of_device_id ux500_hash_match[] = {
 	{ .compatible = "stericsson,ux500-hash" },
@@ -1753,7 +1795,7 @@  static struct platform_driver ux500_hash_driver = {
 	.driver = {
 		.name  = "hash1",
 		.of_match_table = ux500_hash_match,
-		.pm    = &ux500_hash_pm,
+		.pm    = &ux500_hash_pm_ops,
 	}
 };
 module_platform_driver(ux500_hash_driver);