From patchwork Mon Jul 25 14:04:51 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 593566 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5BDCC43334 for ; Mon, 25 Jul 2022 14:07:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235275AbiGYOHa (ORCPT ); Mon, 25 Jul 2022 10:07:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234945AbiGYOH1 (ORCPT ); Mon, 25 Jul 2022 10:07:27 -0400 Received: from mail-lf1-x12e.google.com (mail-lf1-x12e.google.com [IPv6:2a00:1450:4864:20::12e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 044CF14087 for ; Mon, 25 Jul 2022 07:07:25 -0700 (PDT) Received: by mail-lf1-x12e.google.com with SMTP id b16so5762637lfb.7 for ; Mon, 25 Jul 2022 07:07:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZBy4mH3tH/mKdYrWAHsE8vj+OWKXhvzJV2v2xxRnpkk=; b=BoZYnRmPSAfvA0ruBqvQ3onCm2I/HVp0JbBxyUZ0IlOj6pCAxsM1kcMvqd0Mc7JFsS Lqk4A6xPAxhCY9KBy2CjH3478VC2EovsVOPBvkNaPMSOOOY3M52DbfTNNg3Lz15YoYjE tAtmvbKHFpa6heq/jMSD2QLGxunld9FrWsIkEt5xVsdHiJNsrfLRF/zlE1j/Oejhy8Ly sZDq0As1Qi7UQ44B6jEMOp2jQ7KColdNUysi5rJJv5gPEB6YljU2LrIzZeJL13290TvD n9QUGo/UbSzOtbNHC9j53s79b7QRjLrpjOXKYb/ctvkhRI/5XeNhsSDM8zi60G4K1tl3 6rxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZBy4mH3tH/mKdYrWAHsE8vj+OWKXhvzJV2v2xxRnpkk=; b=BndH6sGz6ECJRNnvU6+9qdZ45lKkEgq/eAK2j0yOz6GiExcKBw4rDfFVWKk4+SkDU6 4PfG7vS+YiqdI1UK6M7E4ZgFPGtrWd4Dn6rxnbNOKYGVOCQxBT/jLGwAcI95HqBcDhOq pOLzz1mIsmcNYG2sg6U7yc6eC+Ure2+Dy6dW9dQaWKdGi7jk1whYLruv12ZCXmVYJtWk 5hQ6PbNDP57ZuYD4dMGJZEAbqJvPnO9oSQlUUoml6Z+EOn2kzO11zbzfCwtDhe84/Sbb 2BjqjjhL6WAfpKZO0pXlFFRq6zEVRaXUzJtlgKgfDAiAqjMHfWZ7vCJx3tJ1csSb5hhx DppQ== X-Gm-Message-State: AJIora86zLHsKeOKEgWP5liujAJXLG/l2HuWFeGCuV6nLeCsPUs1bcCZ cIRv+aPWXRJSCwB6lXt82vJCudqoXHz23w== X-Google-Smtp-Source: AGRyM1sYgcqD+1vlk+ePPVy+Hs9MZJ8Zce8GL7/S4pHY6bp+oaUSCtUHmQ3Lqw+ruf1ecI0QaGXK8Q== X-Received: by 2002:a05:6512:258b:b0:48a:83da:4f0a with SMTP id bf11-20020a056512258b00b0048a83da4f0amr3326060lfb.256.1658758042940; Mon, 25 Jul 2022 07:07:22 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id o7-20020a05651205c700b0047f7419de4asm901127lfo.180.2022.07.25.07.07.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 07:07:22 -0700 (PDT) From: Linus Walleij To: linux-crypto@vger.kernel.org, Herbert Xu , "David S . Miller" Cc: phone-devel@vger.kernel.org, Stefan Hansson , Linus Walleij Subject: [PATCH 02/15 v2] crypto: ux500/hash: Get rid of custom device list Date: Mon, 25 Jul 2022 16:04:51 +0200 Message-Id: <20220725140504.2398965-3-linus.walleij@linaro.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220725140504.2398965-1-linus.walleij@linaro.org> References: <20220725140504.2398965-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The Ux500 hash driver builds a list with one (1) hash engine as it is all it has, then goes to great lengths to lock the one device using a semaphore. Instead do what other drivers do: trust the core to do the right thing, add the device state to the algorithm template, fill it in when registering the algorithms and assign the device state to the context when intializing each context. This saves us from a lot of complex code. Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - No changes --- drivers/crypto/ux500/hash/hash_core.c | 194 +++----------------------- 1 file changed, 17 insertions(+), 177 deletions(-) diff --git a/drivers/crypto/ux500/hash/hash_core.c b/drivers/crypto/ux500/hash/hash_core.c index dfdf3e35d94f..fd7a862244ac 100644 --- a/drivers/crypto/ux500/hash/hash_core.c +++ b/drivers/crypto/ux500/hash/hash_core.c @@ -19,7 +19,6 @@ #include #include #include -#include #include #include #include @@ -59,19 +58,6 @@ static const u8 zero_message_hmac_sha256[SHA256_DIGEST_SIZE] = { 0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 0xc5, 0xad }; -/** - * struct hash_driver_data - data specific to the driver. - * - * @device_list: A list of registered devices to choose from. - * @device_allocation: A semaphore initialized with number of devices. - */ -struct hash_driver_data { - struct klist device_list; - struct semaphore device_allocation; -}; - -static struct hash_driver_data driver_data; - /* Declaration of functions */ /** * hash_messagepad - Pads a message and write the nblw bits. @@ -86,24 +72,6 @@ static struct hash_driver_data driver_data; static void hash_messagepad(struct hash_device_data *device_data, const u32 *message, u8 index_bytes); -/** - * release_hash_device - Releases a previously allocated hash device. - * @device_data: Structure for the hash device. - * - */ -static void release_hash_device(struct hash_device_data *device_data) -{ - spin_lock(&device_data->ctx_lock); - device_data->current_ctx->device = NULL; - device_data->current_ctx = NULL; - spin_unlock(&device_data->ctx_lock); - - /* - * The down_interruptible part for this semaphore is called in - * cryp_get_device_data. - */ - up(&driver_data.device_allocation); -} static void hash_dma_setup_channel(struct hash_device_data *device_data, struct device *dev) @@ -354,65 +322,6 @@ static int hash_enable_power(struct hash_device_data *device_data, return ret; } -/** - * hash_get_device_data - Checks for an available hash device and return it. - * @ctx: Structure for the hash context. - * @device_data: Structure for the hash device. - * - * This function check for an available hash device and return it to - * the caller. - * Note! Caller need to release the device, calling up(). - */ -static int hash_get_device_data(struct hash_ctx *ctx, - struct hash_device_data **device_data) -{ - int ret; - struct klist_iter device_iterator; - struct klist_node *device_node; - struct hash_device_data *local_device_data = NULL; - - /* Wait until a device is available */ - ret = down_interruptible(&driver_data.device_allocation); - if (ret) - return ret; /* Interrupted */ - - /* Select a device */ - klist_iter_init(&driver_data.device_list, &device_iterator); - device_node = klist_next(&device_iterator); - while (device_node) { - local_device_data = container_of(device_node, - struct hash_device_data, list_node); - spin_lock(&local_device_data->ctx_lock); - /* current_ctx allocates a device, NULL = unallocated */ - if (local_device_data->current_ctx) { - device_node = klist_next(&device_iterator); - } else { - local_device_data->current_ctx = ctx; - ctx->device = local_device_data; - spin_unlock(&local_device_data->ctx_lock); - break; - } - spin_unlock(&local_device_data->ctx_lock); - } - klist_iter_exit(&device_iterator); - - if (!device_node) { - /** - * No free device found. - * Since we allocated a device with down_interruptible, this - * should not be able to happen. - * Number of available devices, which are contained in - * device_allocation, is therefore decremented by not doing - * an up(device_allocation). - */ - return -EBUSY; - } - - *device_data = local_device_data; - - return 0; -} - /** * hash_hw_write_key - Writes the key to the hardware registries. * @@ -859,14 +768,10 @@ static int hash_dma_final(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; + struct hash_device_data *device_data = ctx->device; u8 digest[SHA256_DIGEST_SIZE]; int bytes_written = 0; - ret = hash_get_device_data(ctx, &device_data); - if (ret) - return ret; - dev_dbg(device_data->dev, "%s: (ctx=0x%lx)!\n", __func__, (unsigned long)ctx); @@ -944,8 +849,6 @@ static int hash_dma_final(struct ahash_request *req) memcpy(req->result, digest, ctx->digestsize); out: - release_hash_device(device_data); - /** * Allocated in setkey, and only used in HMAC. */ @@ -964,13 +867,9 @@ static int hash_hw_final(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; + struct hash_device_data *device_data = ctx->device; u8 digest[SHA256_DIGEST_SIZE]; - ret = hash_get_device_data(ctx, &device_data); - if (ret) - return ret; - dev_dbg(device_data->dev, "%s: (ctx=0x%lx)!\n", __func__, (unsigned long)ctx); @@ -1047,7 +946,6 @@ static int hash_hw_final(struct ahash_request *req) memcpy(req->result, digest, ctx->digestsize); out: - release_hash_device(device_data); /** * Allocated in setkey, and only used in HMAC. @@ -1068,36 +966,29 @@ int hash_hw_update(struct ahash_request *req) int ret = 0; u8 index = 0; u8 *buffer; - struct hash_device_data *device_data; u8 *data_buffer; 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; struct crypto_hash_walk walk; int msg_length; index = req_ctx->state.index; buffer = (u8 *)req_ctx->state.buffer; - ret = hash_get_device_data(ctx, &device_data); - if (ret) - return ret; - msg_length = crypto_hash_walk_first(req, &walk); /* Empty message ("") is correct indata */ - if (msg_length == 0) { - ret = 0; - goto release_dev; - } + if (msg_length == 0) + return 0; /* Check if ctx->state.length + msg_length overflows */ if (msg_length > (req_ctx->state.length.low_word + msg_length) && HASH_HIGH_WORD_MAX_VAL == req_ctx->state.length.high_word) { pr_err("%s: HASH_MSG_LENGTH_OVERFLOW!\n", __func__); - ret = crypto_hash_walk_done(&walk, -EPERM); - goto release_dev; + return crypto_hash_walk_done(&walk, -EPERM); } /* Main loop */ @@ -1110,7 +1001,7 @@ int hash_hw_update(struct ahash_request *req) dev_err(device_data->dev, "%s: hash_internal_hw_update() failed!\n", __func__); crypto_hash_walk_done(&walk, ret); - goto release_dev; + return ret; } msg_length = crypto_hash_walk_done(&walk, 0); @@ -1120,10 +1011,7 @@ int hash_hw_update(struct ahash_request *req) dev_dbg(device_data->dev, "%s: indata length=%d, bin=%d\n", __func__, req_ctx->state.index, req_ctx->state.bit_index); -release_dev: - release_hash_device(device_data); - - return ret; + return 0; } /** @@ -1495,6 +1383,7 @@ static int hmac_sha256_setkey(struct crypto_ahash *tfm, struct hash_algo_template { struct hash_config conf; struct ahash_alg hash; + struct hash_device_data *device; }; static int hash_cra_init(struct crypto_tfm *tfm) @@ -1507,6 +1396,8 @@ static int hash_cra_init(struct crypto_tfm *tfm) struct hash_algo_template, hash); + ctx->device = hash_alg->device; + crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm), sizeof(struct hash_req_ctx)); @@ -1623,6 +1514,7 @@ static int ahash_algs_register_all(struct hash_device_data *device_data) int count; for (i = 0; i < ARRAY_SIZE(hash_algs); i++) { + hash_algs[i].device = device_data; ret = crypto_register_ahash(&hash_algs[i].hash); if (ret) { count = i; @@ -1723,11 +1615,6 @@ static int ux500_hash_probe(struct platform_device *pdev) platform_set_drvdata(pdev, device_data); - /* Put the new device into the device list... */ - klist_add_tail(&device_data->list_node, &driver_data.device_list); - /* ... and signal that a new device is available. */ - up(&driver_data.device_allocation); - ret = ahash_algs_register_all(device_data); if (ret) { dev_err(dev, "%s: ahash_algs_register_all() failed!\n", @@ -1766,10 +1653,6 @@ static int ux500_hash_remove(struct platform_device *pdev) return -ENOMEM; } - /* Try to decrease the number of available devices. */ - if (down_trylock(&driver_data.device_allocation)) - return -EBUSY; - /* Check that the device is free */ spin_lock(&device_data->ctx_lock); /* current_ctx allocates a device, NULL = unallocated */ @@ -1777,19 +1660,12 @@ static int ux500_hash_remove(struct platform_device *pdev) /* The device is busy */ spin_unlock(&device_data->ctx_lock); /* Return the device to the pool. */ - up(&driver_data.device_allocation); return -EBUSY; } spin_unlock(&device_data->ctx_lock); - /* Remove the device from the list */ - if (klist_node_attached(&device_data->list_node)) - klist_remove(&device_data->list_node); - - /* If this was the last device, remove the services */ - if (list_empty(&driver_data.device_list.k_list)) - ahash_algs_unregister_all(device_data); + ahash_algs_unregister_all(device_data); if (hash_disable_power(device_data, false)) dev_err(dev, "%s: hash_disable_power() failed\n", @@ -1820,9 +1696,6 @@ static void ux500_hash_shutdown(struct platform_device *pdev) spin_lock(&device_data->ctx_lock); /* current_ctx allocates a device, NULL = unallocated */ if (!device_data->current_ctx) { - if (down_trylock(&driver_data.device_allocation)) - dev_dbg(&pdev->dev, "%s: Cryp still in use! Shutting down anyway...\n", - __func__); /** * (Allocate the device) * Need to set this to non-null (dummy) value, @@ -1832,13 +1705,7 @@ static void ux500_hash_shutdown(struct platform_device *pdev) } spin_unlock(&device_data->ctx_lock); - /* Remove the device from the list */ - if (klist_node_attached(&device_data->list_node)) - klist_remove(&device_data->list_node); - - /* If this was the last device, remove the services */ - if (list_empty(&driver_data.device_list.k_list)) - ahash_algs_unregister_all(device_data); + ahash_algs_unregister_all(device_data); if (hash_disable_power(device_data, false)) dev_err(&pdev->dev, "%s: hash_disable_power() failed\n", @@ -1868,9 +1735,6 @@ static int ux500_hash_suspend(struct device *dev) spin_unlock(&device_data->ctx_lock); if (device_data->current_ctx == ++temp_ctx) { - if (down_interruptible(&driver_data.device_allocation)) - dev_dbg(dev, "%s: down_interruptible() failed\n", - __func__); ret = hash_disable_power(device_data, false); } else { @@ -1904,9 +1768,7 @@ static int ux500_hash_resume(struct device *dev) device_data->current_ctx = NULL; spin_unlock(&device_data->ctx_lock); - if (!device_data->current_ctx) - up(&driver_data.device_allocation); - else + if (device_data->current_ctx) ret = hash_enable_power(device_data, true); if (ret) @@ -1924,7 +1786,7 @@ static const struct of_device_id ux500_hash_match[] = { }; MODULE_DEVICE_TABLE(of, ux500_hash_match); -static struct platform_driver hash_driver = { +static struct platform_driver ux500_hash_driver = { .probe = ux500_hash_probe, .remove = ux500_hash_remove, .shutdown = ux500_hash_shutdown, @@ -1934,29 +1796,7 @@ static struct platform_driver hash_driver = { .pm = &ux500_hash_pm, } }; - -/** - * ux500_hash_mod_init - The kernel module init function. - */ -static int __init ux500_hash_mod_init(void) -{ - klist_init(&driver_data.device_list, NULL, NULL); - /* Initialize the semaphore to 0 devices (locked state) */ - sema_init(&driver_data.device_allocation, 0); - - return platform_driver_register(&hash_driver); -} - -/** - * ux500_hash_mod_fini - The kernel module exit function. - */ -static void __exit ux500_hash_mod_fini(void) -{ - platform_driver_unregister(&hash_driver); -} - -module_init(ux500_hash_mod_init); -module_exit(ux500_hash_mod_fini); +module_platform_driver(ux500_hash_driver); MODULE_DESCRIPTION("Driver for ST-Ericsson UX500 HASH engine."); MODULE_LICENSE("GPL"); From patchwork Mon Jul 25 14:04:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 593565 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 36081CCA48C for ; Mon, 25 Jul 2022 14:07:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235261AbiGYOHc (ORCPT ); Mon, 25 Jul 2022 10:07:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235272AbiGYOHa (ORCPT ); Mon, 25 Jul 2022 10:07:30 -0400 Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 73C8B165B6 for ; Mon, 25 Jul 2022 07:07:28 -0700 (PDT) Received: by mail-lj1-x22f.google.com with SMTP id z13so12857887ljj.6 for ; Mon, 25 Jul 2022 07:07:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rN8jYOVehuu3dO+n+b32giGGSLgkWgby9u0JmweOpzI=; b=aU7gzGVDCfF1zbEJ+RRAnz6f6z/HIkXICO+9eTKQKaKaOtrhDz2OWq0aH1mrF3D9hm ztUaNrj/UfrGYhs1fd4BokFaJJEFzmSEGL9/0cim+LpA4yq3sg/mvDIB5g6Kw98VXiOw /TgLym8YI7m3W9z5VpG6jD2JahtCfm255zRpgAo91CeNHrreocmcNLdPHp2xzoRvRMyX xUY370lSolVSj1PJ1CPh2vF1n7SKEdixPPhChr74MY7MVq2N0ytVdrLYyw3vQ+TfrYKo FGIfR7YbBlyWzAbp9zQp3e0BEtZx+nAzfdyWbAwd5e/YFtTgIf5lj+/e4pfapY8rluAe 1D6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rN8jYOVehuu3dO+n+b32giGGSLgkWgby9u0JmweOpzI=; b=nRmba+A1lUYqfWjYGCiYw2LijYon6oJ3P+7f1a3P6R2ewerWVADssUArQgGFubAS73 RC1vbgz9qbBueS0TLJeH+M78yc8RPuNTRJQF+k8pHadX6xtfzoBn/wrZrXGOEbKCKq+P 5VkRTWjqDq/INNskV9TWYcRr0H0nSDQC2L6M9xFm5hYf0U/3tKHG8ZMuITBDXCsMCWZx M1eq5WLF7yju6+4TctbRC/XNSUHKvi92pg7Z6H6XKrwHvoxU9AYtDs6aIQwaHOJen0Z8 BfS3ZKPY0nAiNdixpgp+ZhwuO60IiREMG9LlMG4AQtH2cmv/xk/bqswRUjt29rPN5A8t WK5Q== X-Gm-Message-State: AJIora+EVkIguwn8dFwZxjqu6ISRlxZQz5k+CbR/dxHKjeCZKR7pcyXD LJU2yw9eeo+lbPdvTtQy5jr9HiZluxXStw== X-Google-Smtp-Source: AGRyM1ty7gR21SbKWRLehmT4mU90C7+mFpjstmZxS4W5nnmffRbQxvMRTHnOK5DYNIc2/A/5HFfmpw== X-Received: by 2002:a05:651c:32d:b0:25e:946:2740 with SMTP id b13-20020a05651c032d00b0025e09462740mr1429814ljp.378.1658758046151; Mon, 25 Jul 2022 07:07:26 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id o7-20020a05651205c700b0047f7419de4asm901127lfo.180.2022.07.25.07.07.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 07:07:25 -0700 (PDT) From: Linus Walleij To: linux-crypto@vger.kernel.org, Herbert Xu , "David S . Miller" Cc: phone-devel@vger.kernel.org, Stefan Hansson , Linus Walleij Subject: [PATCH 04/15 v2] crypto: ux500/hash: Drop custom state save/restore Date: Mon, 25 Jul 2022 16:04:53 +0200 Message-Id: <20220725140504.2398965-5-linus.walleij@linaro.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220725140504.2398965-1-linus.walleij@linaro.org> References: <20220725140504.2398965-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Drop the code that is saving and restoring the device state as part of the PM operations: this is the job of .import and .export, do not try to work around the framework. Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - No changes --- drivers/crypto/ux500/hash/hash_core.c | 52 +++++---------------------- 1 file changed, 8 insertions(+), 44 deletions(-) diff --git a/drivers/crypto/ux500/hash/hash_core.c b/drivers/crypto/ux500/hash/hash_core.c index 884046e87262..e6e3a91ae795 100644 --- a/drivers/crypto/ux500/hash/hash_core.c +++ b/drivers/crypto/ux500/hash/hash_core.c @@ -243,13 +243,11 @@ static int get_empty_message_digest( /** * hash_disable_power - Request to disable power and clock. * @device_data: Structure for the hash device. - * @save_device_state: If true, saves the current hw state. * * This function request for disabling power (regulator) and clock, * and could also save current hw state. */ -static int hash_disable_power(struct hash_device_data *device_data, - bool save_device_state) +static int hash_disable_power(struct hash_device_data *device_data) { int ret = 0; struct device *dev = device_data->dev; @@ -258,12 +256,6 @@ static int hash_disable_power(struct hash_device_data *device_data, if (!device_data->power_state) goto out; - if (save_device_state) { - hash_save_state(device_data, - &device_data->state); - device_data->restore_dev_state = true; - } - clk_disable(device_data->clk); ret = regulator_disable(device_data->regulator); if (ret) @@ -280,13 +272,11 @@ static int hash_disable_power(struct hash_device_data *device_data, /** * hash_enable_power - Request to enable power and clock. * @device_data: Structure for the hash device. - * @restore_device_state: If true, restores a previous saved hw state. * * This function request for enabling power (regulator) and clock, * and could also restore a previously saved hw state. */ -static int hash_enable_power(struct hash_device_data *device_data, - bool restore_device_state) +static int hash_enable_power(struct hash_device_data *device_data) { int ret = 0; struct device *dev = device_data->dev; @@ -309,12 +299,6 @@ static int hash_enable_power(struct hash_device_data *device_data, device_data->power_state = true; } - if (device_data->restore_dev_state) { - if (restore_device_state) { - device_data->restore_dev_state = false; - hash_resume_state(device_data, &device_data->state); - } - } out: spin_unlock(&device_data->power_state_lock); @@ -1597,7 +1581,7 @@ static int ux500_hash_probe(struct platform_device *pdev) } /* Enable device power (and clock) */ - ret = hash_enable_power(device_data, false); + ret = hash_enable_power(device_data); if (ret) { dev_err(dev, "%s: hash_enable_power() failed!\n", __func__); goto out_clk_unprepare; @@ -1625,7 +1609,7 @@ static int ux500_hash_probe(struct platform_device *pdev) return 0; out_power: - hash_disable_power(device_data, false); + hash_disable_power(device_data); out_clk_unprepare: clk_unprepare(device_data->clk); @@ -1666,7 +1650,7 @@ static int ux500_hash_remove(struct platform_device *pdev) ahash_algs_unregister_all(device_data); - if (hash_disable_power(device_data, false)) + if (hash_disable_power(device_data)) dev_err(dev, "%s: hash_disable_power() failed\n", __func__); @@ -1706,7 +1690,7 @@ static void ux500_hash_shutdown(struct platform_device *pdev) ahash_algs_unregister_all(device_data); - if (hash_disable_power(device_data, false)) + if (hash_disable_power(device_data)) dev_err(&pdev->dev, "%s: hash_disable_power() failed\n", __func__); } @@ -1720,7 +1704,6 @@ static int ux500_hash_suspend(struct device *dev) { int ret; struct hash_device_data *device_data; - struct hash_ctx *temp_ctx = NULL; device_data = dev_get_drvdata(dev); if (!device_data) { @@ -1728,18 +1711,7 @@ static int ux500_hash_suspend(struct device *dev) return -ENOMEM; } - spin_lock(&device_data->ctx_lock); - if (!device_data->current_ctx) - device_data->current_ctx++; - spin_unlock(&device_data->ctx_lock); - - if (device_data->current_ctx == ++temp_ctx) { - ret = hash_disable_power(device_data, false); - - } else { - ret = hash_disable_power(device_data, true); - } - + ret = hash_disable_power(device_data); if (ret) dev_err(dev, "%s: hash_disable_power()\n", __func__); @@ -1754,7 +1726,6 @@ static int ux500_hash_resume(struct device *dev) { int ret = 0; struct hash_device_data *device_data; - struct hash_ctx *temp_ctx = NULL; device_data = dev_get_drvdata(dev); if (!device_data) { @@ -1762,14 +1733,7 @@ static int ux500_hash_resume(struct device *dev) return -ENOMEM; } - spin_lock(&device_data->ctx_lock); - if (device_data->current_ctx == ++temp_ctx) - device_data->current_ctx = NULL; - spin_unlock(&device_data->ctx_lock); - - if (device_data->current_ctx) - ret = hash_enable_power(device_data, true); - + ret = hash_enable_power(device_data); if (ret) dev_err(dev, "%s: hash_enable_power() failed!\n", __func__); From patchwork Mon Jul 25 14:04:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 593564 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C6884CCA473 for ; Mon, 25 Jul 2022 14:07:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235273AbiGYOHd (ORCPT ); Mon, 25 Jul 2022 10:07:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45734 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235222AbiGYOHb (ORCPT ); Mon, 25 Jul 2022 10:07:31 -0400 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83157167C9 for ; Mon, 25 Jul 2022 07:07:30 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id z13so12858048ljj.6 for ; Mon, 25 Jul 2022 07:07:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=XhDve6JLJ3Kju6H4o7eHpk5rJCSDlgoSXIzWMLgqIdY=; b=BrQmM4z6wseWqWYrUpGMUcwzzFz459jFZoxL8QfLFrHozJDEqLZxKcA2crq9qmCSHi 1PrXTJu9w3s7nvBMGm4t7tJOaE0t3l0djy9NZAMnslMJXdCwtjgeeg7i8FB3emO6965t bi36OxcGi4JodTJMNsyen0iGY289Wh51BNkZ9qDOHEyBA/LuUvla/LTeQ1cyQp5Ci8h5 I/BluA8Z+vWhILMH6p2s+DW+M29YFuE/bMoJv0lEdCHxEAe7JzpydrXMsMsrS03DMm7l ixiMIfs7pjPm/YWiruw8JIF9eyFW9ePtoFJd6ro8fH0if0dciGmWcz3/1pGwFAb+kOjC es5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=XhDve6JLJ3Kju6H4o7eHpk5rJCSDlgoSXIzWMLgqIdY=; b=EBI/WwuSqKthOGiaAvvK1utkipQd+ZWgQbXGydmbsBqqy70N8YYxciJFOOpv9+oijc 3/R6kMR1EOhkNfnNc4NaqBkrNEImpPsBABGB+8gG8LKxwkT8pT4H6WNgkkc2+4Ki3p5L y5JyXoS9HYBoL6Q+sFJkH0LGDm5bFsDdoeXyC3DS8xVMRQa5sv5MkiWcS1POETackEv9 0fy+hcmzSe3qgcYDH3M1i9I0C5Gfm2o/+UtJHsXmSASDzbsNfRJvYDTff8CkIpSv8zQM PxQQadypJRKFC72lx117AkqsHN1H80tKoaTj3Tqq1jjH7lMoxV81TbXwHhW54LIxFe+v D3mA== X-Gm-Message-State: AJIora/zuGw3yTT0126C6FRlyafQDM3ljnlKFLfR8oIPdjZ5BcL9sdWA sL3waVZkb3PFhEEtz1lwFakZ8popd1dF+Q== X-Google-Smtp-Source: AGRyM1vHMG4ei9iEs7o1abwOd/CPQsfJvXKmO7hj4znQHZHTukk3pyN/f7P0bAWe7I49adkyagEO0w== X-Received: by 2002:a05:651c:160a:b0:25a:62a4:9085 with SMTP id f10-20020a05651c160a00b0025a62a49085mr4575036ljq.214.1658758048757; Mon, 25 Jul 2022 07:07:28 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id o7-20020a05651205c700b0047f7419de4asm901127lfo.180.2022.07.25.07.07.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 07:07:28 -0700 (PDT) From: Linus Walleij To: linux-crypto@vger.kernel.org, Herbert Xu , "David S . Miller" Cc: phone-devel@vger.kernel.org, Stefan Hansson , Linus Walleij Subject: [PATCH 06/15 v2] crypto: ux500/hash: Break while/do instead of if/else Date: Mon, 25 Jul 2022 16:04:55 +0200 Message-Id: <20220725140504.2398965-7-linus.walleij@linaro.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220725140504.2398965-1-linus.walleij@linaro.org> References: <20220725140504.2398965-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org Instead of a deeply nested if/else inside the while/do loop, just break the loop as we know the termination requirement was just established (msg_length == 0). Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - No changes --- drivers/crypto/ux500/hash/hash_core.c | 115 +++++++++++++------------- 1 file changed, 58 insertions(+), 57 deletions(-) diff --git a/drivers/crypto/ux500/hash/hash_core.c b/drivers/crypto/ux500/hash/hash_core.c index e9962c8a29bd..c9ceaa0b1778 100644 --- a/drivers/crypto/ux500/hash/hash_core.c +++ b/drivers/crypto/ux500/hash/hash_core.c @@ -671,69 +671,70 @@ static int hash_process_data(struct hash_device_data *device_data, } *index += msg_length; msg_length = 0; - } else { - if (req_ctx->updated) { - ret = hash_resume_state(device_data, - &device_data->state); - memmove(req_ctx->state.buffer, - device_data->state.buffer, - HASH_BLOCK_SIZE); - if (ret) { - dev_err(device_data->dev, - "%s: hash_resume_state() failed!\n", - __func__); - goto out; - } - } else { - ret = init_hash_hw(device_data, ctx); - if (ret) { - dev_err(device_data->dev, - "%s: init_hash_hw() failed!\n", - __func__); - goto out; - } - req_ctx->updated = 1; - } - /* - * If 'data_buffer' is four byte aligned and - * local buffer does not have any data, we can - * write data directly from 'data_buffer' to - * HW peripheral, otherwise we first copy data - * to a local buffer - */ - if (IS_ALIGNED((unsigned long)data_buffer, 4) && - (0 == *index)) - hash_processblock(device_data, - (const u32 *)data_buffer, - HASH_BLOCK_SIZE); - else { - for (count = 0; - count < (u32)(HASH_BLOCK_SIZE - *index); - count++) { - buffer[*index + count] = - *(data_buffer + count); - } - hash_processblock(device_data, - (const u32 *)buffer, - HASH_BLOCK_SIZE); - } - hash_incrementlength(req_ctx, HASH_BLOCK_SIZE); - data_buffer += (HASH_BLOCK_SIZE - *index); - - msg_length -= (HASH_BLOCK_SIZE - *index); - *index = 0; - - ret = hash_save_state(device_data, - &device_data->state); + break; + } - memmove(device_data->state.buffer, - req_ctx->state.buffer, + if (req_ctx->updated) { + ret = hash_resume_state(device_data, + &device_data->state); + memmove(req_ctx->state.buffer, + device_data->state.buffer, HASH_BLOCK_SIZE); if (ret) { - dev_err(device_data->dev, "%s: hash_save_state() failed!\n", + dev_err(device_data->dev, + "%s: hash_resume_state() failed!\n", __func__); goto out; } + } else { + ret = init_hash_hw(device_data, ctx); + if (ret) { + dev_err(device_data->dev, + "%s: init_hash_hw() failed!\n", + __func__); + goto out; + } + req_ctx->updated = 1; + } + /* + * If 'data_buffer' is four byte aligned and + * local buffer does not have any data, we can + * write data directly from 'data_buffer' to + * HW peripheral, otherwise we first copy data + * to a local buffer + */ + if (IS_ALIGNED((unsigned long)data_buffer, 4) && + (*index == 0)) + hash_processblock(device_data, + (const u32 *)data_buffer, + HASH_BLOCK_SIZE); + else { + for (count = 0; + count < (u32)(HASH_BLOCK_SIZE - *index); + count++) { + buffer[*index + count] = + *(data_buffer + count); + } + hash_processblock(device_data, + (const u32 *)buffer, + HASH_BLOCK_SIZE); + } + hash_incrementlength(req_ctx, HASH_BLOCK_SIZE); + data_buffer += (HASH_BLOCK_SIZE - *index); + + msg_length -= (HASH_BLOCK_SIZE - *index); + *index = 0; + + ret = hash_save_state(device_data, + &device_data->state); + + memmove(device_data->state.buffer, + req_ctx->state.buffer, + HASH_BLOCK_SIZE); + if (ret) { + dev_err(device_data->dev, "%s: hash_save_state() failed!\n", + __func__); + goto out; } } while (msg_length != 0); out: From patchwork Mon Jul 25 14:04:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 593563 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8343CC433EF for ; Mon, 25 Jul 2022 14:07:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235329AbiGYOHr (ORCPT ); Mon, 25 Jul 2022 10:07:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235333AbiGYOHe (ORCPT ); Mon, 25 Jul 2022 10:07:34 -0400 Received: from mail-lj1-x22c.google.com (mail-lj1-x22c.google.com [IPv6:2a00:1450:4864:20::22c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 46A9414D35 for ; Mon, 25 Jul 2022 07:07:33 -0700 (PDT) Received: by mail-lj1-x22c.google.com with SMTP id a13so13080820ljr.11 for ; Mon, 25 Jul 2022 07:07:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=C6S3uKvTAP+Cp0+QHVaCOI5oR5HoGiw3zJjt6sISzSU=; b=yTVzpWenvwRkWoozJxMrX1G2Siyp5l8lHkNgDuuxKQTs0WDKiCSxJqCXzVRDMz+mwA 7Cbg2sfEMF5q9Nmz6q6yafvsoW5xXKgXWKnJ6fG90RHn7Kew1Y6ho5z7crhoHPadpjsL m2gjHyk8WZrOpgykW7W0s4kFhPhViFe+ADajhpDOomE6v0EnCoyYDR0TcrOgQVdsThvZ pnBCsl1r6MPKdTsGzYZcsLkB6o43iudjstrajruGG1dOBxnAUzfQFFcuqORdEEXw6Xuc NhlKHTahomNq2+eBF6kmiHgvijZy+OfFv5v6hXKY347t9n3wbmBtsbjgidTVyUY2lGe5 Wqrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=C6S3uKvTAP+Cp0+QHVaCOI5oR5HoGiw3zJjt6sISzSU=; b=bQWtzYtotqu1z0jKi1lqa43DgwBqE6DDcXlOuHGvwHjYU9dpRRDw4mE5OnhyH6g0gz r/EREQUCSNoOSaKphQ5iF12KC18pr+Z9GoYeOO2OOyVyUAIaFX66VMHDa97RNCW1j6t7 jKl8aQQF/Fw2OX2IIQ1vLvMG/UJg/oOyXApQ3Cu/z8JDLifW7y5X8k6ZYsmgg5XprZbR aaLXyY9ZVCvkEfflODyccoifAffYR+Eq9CTJ9A21O5E/GDMPseVOG6u3cbzoLgHxpF5O /rLw0ztrFJM7++sHlb/+hVHkkf3E2hfrTVW8GWzmzPJ9PMnTNeEOmXbp0nLwxiJ8UTIV Ij1g== X-Gm-Message-State: AJIora/5RkiWTg4/gQxr6xvk9gfSP4UIXJtXk8yHQl9xx3n9oecw8njQ nPZGhTq5u7q8qphuIA75x7Eqfk9S8KeNWA== X-Google-Smtp-Source: AGRyM1v6m9uG8KrasSYT71mXP4rd8Zh4mfRM7XRxUrJXE/q+b9w2l5/yQxgs+EkYgpzxrZbJJuVFcg== X-Received: by 2002:a2e:5705:0:b0:25d:f295:c9d8 with SMTP id l5-20020a2e5705000000b0025df295c9d8mr4528319ljb.292.1658758051314; Mon, 25 Jul 2022 07:07:31 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id o7-20020a05651205c700b0047f7419de4asm901127lfo.180.2022.07.25.07.07.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 07:07:31 -0700 (PDT) From: Linus Walleij To: linux-crypto@vger.kernel.org, Herbert Xu , "David S . Miller" Cc: phone-devel@vger.kernel.org, Stefan Hansson , Linus Walleij Subject: [PATCH 08/15 v2] crypto: ux500/hash: Stop saving/restoring compulsively Date: Mon, 25 Jul 2022 16:04:57 +0200 Message-Id: <20220725140504.2398965-9-linus.walleij@linaro.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220725140504.2398965-1-linus.walleij@linaro.org> References: <20220725140504.2398965-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The driver is saving/restoring state very intensively, because of assumptions that suspend/resume can be called at any time. (Android behaviours.) We removed the state save/restore from the PM hooks and will use runtime PM for this instead so get rid of this. Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - No changes --- drivers/crypto/ux500/hash/hash_alg.h | 1 - drivers/crypto/ux500/hash/hash_core.c | 44 +++------------------------ 2 files changed, 4 insertions(+), 41 deletions(-) diff --git a/drivers/crypto/ux500/hash/hash_alg.h b/drivers/crypto/ux500/hash/hash_alg.h index d124fd17519f..d9d59dba6e6e 100644 --- a/drivers/crypto/ux500/hash/hash_alg.h +++ b/drivers/crypto/ux500/hash/hash_alg.h @@ -369,7 +369,6 @@ struct hash_device_data { spinlock_t power_state_lock; struct regulator *regulator; struct clk *clk; - struct hash_state state; /* Used for saving and resuming state */ struct hash_dma dma; }; diff --git a/drivers/crypto/ux500/hash/hash_core.c b/drivers/crypto/ux500/hash/hash_core.c index b3649e00184f..c2e8bd977f57 100644 --- a/drivers/crypto/ux500/hash/hash_core.c +++ b/drivers/crypto/ux500/hash/hash_core.c @@ -674,19 +674,7 @@ static int hash_process_data(struct hash_device_data *device_data, break; } - if (req_ctx->hw_initialized) { - ret = hash_resume_state(device_data, - &device_data->state); - memmove(req_ctx->state.buffer, - device_data->state.buffer, - HASH_BLOCK_SIZE); - if (ret) { - dev_err(device_data->dev, - "%s: hash_resume_state() failed!\n", - __func__); - goto out; - } - } else { + if (!req_ctx->hw_initialized) { ret = init_hash_hw(device_data, ctx); if (ret) { dev_err(device_data->dev, @@ -725,17 +713,6 @@ static int hash_process_data(struct hash_device_data *device_data, msg_length -= (HASH_BLOCK_SIZE - *index); *index = 0; - ret = hash_save_state(device_data, - &device_data->state); - - memmove(device_data->state.buffer, - req_ctx->state.buffer, - HASH_BLOCK_SIZE); - if (ret) { - dev_err(device_data->dev, "%s: hash_save_state() failed!\n", - __func__); - goto out; - } } while (msg_length != 0); out: @@ -759,15 +736,7 @@ static int hash_dma_final(struct ahash_request *req) dev_dbg(device_data->dev, "%s: (ctx=0x%lx)!\n", __func__, (unsigned long)ctx); - if (req_ctx->hw_initialized) { - ret = hash_resume_state(device_data, &device_data->state); - - if (ret) { - dev_err(device_data->dev, "%s: hash_resume_state() failed!\n", - __func__); - goto out; - } - } else { + if (!req_ctx->hw_initialized) { ret = hash_setconfiguration(device_data, ctx); if (ret) { dev_err(device_data->dev, @@ -858,13 +827,8 @@ static int hash_hw_final(struct ahash_request *req) (unsigned long)ctx); if (req_ctx->hw_initialized) { - ret = hash_resume_state(device_data, &device_data->state); - - if (ret) { - dev_err(device_data->dev, - "%s: hash_resume_state() failed!\n", __func__); - goto out; - } + /* That's fine, result is in HW */ + dev_dbg(device_data->dev, "%s hw initialized\n", __func__); } else if (req->nbytes == 0 && ctx->keylen == 0) { u8 zero_hash[SHA256_DIGEST_SIZE]; u32 zero_hash_size = 0; From patchwork Mon Jul 25 14:04:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 593562 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AEFE8C433EF for ; Mon, 25 Jul 2022 14:08:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235440AbiGYOIB (ORCPT ); Mon, 25 Jul 2022 10:08:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235269AbiGYOHk (ORCPT ); Mon, 25 Jul 2022 10:07:40 -0400 Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E4E7F167C8 for ; Mon, 25 Jul 2022 07:07:34 -0700 (PDT) Received: by mail-lj1-x234.google.com with SMTP id p22so3443347lji.10 for ; Mon, 25 Jul 2022 07:07:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nYX7Y16K2holIUqsDpcgLU5VEJyjd2qVQe8bxbMCzME=; b=u6uEbEo4/mM3QNuCI9CL2W/16XGjQTXe9hBJraEsGqKQv0REc37KnjrgovR4OyxgRe 3wbRns4GJ8Rog5J+0suKcS6chgA4YUWFTSRaE/aaLbyiYUp+X3cynMtlyTiKOKfJQjDX eXBqcyruwMY/NX2LAK8H0jSAGCQ9YvihPSORH9j1qzj300wDUXs1rwuP8cXmSG6pmmSw v6Hn6DJWE/7U8/WNJ7ifYReVuNAYfz0XGuRu96tfwA4jZVXc4q7mKCz9uSl8hA2Q2bWW fBYzbU0ObvEUbZqPobZECYsPQdsp97MXGeNFacR/61SaHpslRdZ4+yhwiLEQLajx3AwG We7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nYX7Y16K2holIUqsDpcgLU5VEJyjd2qVQe8bxbMCzME=; b=og9kNl56rhvgNdkHXoOubwsNcLiowh+hLGuairXypdrm5X08i3pe7RdnffhZLtocPK 0Dd9AQJqEYZFW4e2Y4Z/VL7aOiTAZoL0j96xXlAW7fVunwf1V522FaZYD4xjFlPqT7ru gJXeil+OrWOgmL0LcLrpQPZ6FknjKT/XV8xuwH0+TUOuHMoDpfwZponxJfRx+VDaZvkd B16XmJiWFcJHJZl1In2Nt1aM30P8NZs0JT7uO1UdBJEsvzJ71VJQBXPvIHNaRaSYJMXb NjmsE/I8cx94NG1XGEx4sSZ4HV2XS06oD8aOENl9QmBBcOj4rPihqcbsbQxUYT7/UWh0 G2sw== X-Gm-Message-State: AJIora/7J22/2wP712N9CFfr+4Fb/AMIcpHVe2dgJeSiLlrud5YE9jaN aCql597j6v0fsTl6REKuGU2PWEVq/UDbfQ== X-Google-Smtp-Source: AGRyM1tZwOeQ2RKUG3Gnj0Mb03TYdDQSBMXGACg4+e/eM4YYUrZdP1tTzX6pgckOjTcCg9QADjXc1A== X-Received: by 2002:a2e:b88b:0:b0:25d:a15a:bba9 with SMTP id r11-20020a2eb88b000000b0025da15abba9mr4551031ljp.357.1658758054048; Mon, 25 Jul 2022 07:07:34 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id o7-20020a05651205c700b0047f7419de4asm901127lfo.180.2022.07.25.07.07.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 07:07:33 -0700 (PDT) From: Linus Walleij To: linux-crypto@vger.kernel.org, Herbert Xu , "David S . Miller" Cc: phone-devel@vger.kernel.org, Stefan Hansson , Linus Walleij Subject: [PATCH 10/15 v2] crypto: ux500/hash: Implement .export and .import Date: Mon, 25 Jul 2022 16:04:59 +0200 Message-Id: <20220725140504.2398965-11-linus.walleij@linaro.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220725140504.2398965-1-linus.walleij@linaro.org> References: <20220725140504.2398965-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org The .export and .import callbacks are just implemented as stubs which makes the tests fail: alg: ahash: hmac-sha256-ux500 export() failed with err -38 on test vector 0, cfg="import/export" ------------[ cut here ]------------ WARNING: CPU: 1 PID: 92 at crypto/testmgr.c:5777 alg_test.part.0+0x160/0x3ec alg: self-tests for hmac-sha256-ux500 (hmac(sha256)) failed (rc=-38) The driver already has code for saving and restoring the hardware state, which is now unused. Pass the tests by simply implementing the callbacks properly, extending the state with the length, index and buffer from the ongoing request context. Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - No changes --- drivers/crypto/ux500/hash/hash_alg.h | 5 + drivers/crypto/ux500/hash/hash_core.c | 227 +++++++++++++------------- 2 files changed, 114 insertions(+), 118 deletions(-) diff --git a/drivers/crypto/ux500/hash/hash_alg.h b/drivers/crypto/ux500/hash/hash_alg.h index 5aa86c4855f5..05f0b0221a13 100644 --- a/drivers/crypto/ux500/hash/hash_alg.h +++ b/drivers/crypto/ux500/hash/hash_alg.h @@ -226,6 +226,11 @@ struct hash_state { u32 csr[52]; u32 csfull; u32 csdatain; + u32 buffer[HASH_BLOCK_SIZE / sizeof(u32)]; + struct uint64 length; + u8 index; + bool dma_mode; + bool hw_initialized; }; /** diff --git a/drivers/crypto/ux500/hash/hash_core.c b/drivers/crypto/ux500/hash/hash_core.c index 46dad128b6fe..1edb11812c7d 100644 --- a/drivers/crypto/ux500/hash/hash_core.c +++ b/drivers/crypto/ux500/hash/hash_core.c @@ -964,108 +964,6 @@ int hash_hw_update(struct ahash_request *req) return 0; } -/** - * hash_resume_state - Function that resumes the state of an calculation. - * @device_data: Pointer to the device structure. - * @device_state: The state to be restored in the hash hardware - */ -int hash_resume_state(struct hash_device_data *device_data, - const struct hash_state *device_state) -{ - u32 temp_cr; - s32 count; - int hash_mode = HASH_OPER_MODE_HASH; - - if (NULL == device_state) { - dev_err(device_data->dev, "%s: HASH_INVALID_PARAMETER!\n", - __func__); - return -EPERM; - } - - /* - * INIT bit. Set this bit to 0b1 to reset the HASH processor core and - * prepare the initialize the HASH accelerator to compute the message - * digest of a new message. - */ - HASH_INITIALIZE; - - temp_cr = device_state->temp_cr; - writel_relaxed(temp_cr & HASH_CR_RESUME_MASK, &device_data->base->cr); - - if (readl(&device_data->base->cr) & HASH_CR_MODE_MASK) - hash_mode = HASH_OPER_MODE_HMAC; - else - hash_mode = HASH_OPER_MODE_HASH; - - for (count = 0; count < HASH_CSR_COUNT; count++) { - if ((count >= 36) && (hash_mode == HASH_OPER_MODE_HASH)) - break; - - writel_relaxed(device_state->csr[count], - &device_data->base->csrx[count]); - } - - writel_relaxed(device_state->csfull, &device_data->base->csfull); - writel_relaxed(device_state->csdatain, &device_data->base->csdatain); - - writel_relaxed(device_state->str_reg, &device_data->base->str); - writel_relaxed(temp_cr, &device_data->base->cr); - - return 0; -} - -/** - * hash_save_state - Function that saves the state of hardware. - * @device_data: Pointer to the device structure. - * @device_state: The strucure where the hardware state should be saved. - */ -int hash_save_state(struct hash_device_data *device_data, - struct hash_state *device_state) -{ - u32 temp_cr; - u32 count; - int hash_mode = HASH_OPER_MODE_HASH; - - if (NULL == device_state) { - dev_err(device_data->dev, "%s: HASH_INVALID_PARAMETER!\n", - __func__); - return -ENOTSUPP; - } - - /* Write dummy value to force digest intermediate calculation. This - * actually makes sure that there isn't any ongoing calculation in the - * hardware. - */ - while (readl(&device_data->base->str) & HASH_STR_DCAL_MASK) - cpu_relax(); - - temp_cr = readl_relaxed(&device_data->base->cr); - - device_state->str_reg = readl_relaxed(&device_data->base->str); - - device_state->din_reg = readl_relaxed(&device_data->base->din); - - if (readl(&device_data->base->cr) & HASH_CR_MODE_MASK) - hash_mode = HASH_OPER_MODE_HMAC; - else - hash_mode = HASH_OPER_MODE_HASH; - - for (count = 0; count < HASH_CSR_COUNT; count++) { - if ((count >= 36) && (hash_mode == HASH_OPER_MODE_HASH)) - break; - - device_state->csr[count] = - readl_relaxed(&device_data->base->csrx[count]); - } - - device_state->csfull = readl_relaxed(&device_data->base->csfull); - device_state->csdatain = readl_relaxed(&device_data->base->csdatain); - - device_state->temp_cr = temp_cr; - - return 0; -} - /** * hash_check_hw - This routine checks for peripheral Ids and PCell Ids. * @device_data: @@ -1244,14 +1142,107 @@ static int ahash_sha256_digest(struct ahash_request *req) return ret1 ? ret1 : ret2; } -static int ahash_noimport(struct ahash_request *req, const void *in) +static int ahash_import(struct ahash_request *req, const void *in) { - return -ENOSYS; + 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); + const struct hash_state *hstate = in; + int hash_mode = HASH_OPER_MODE_HASH; + u32 cr; + s32 count; + + /* Restore software state */ + req_ctx->length = hstate->length; + req_ctx->index = hstate->index; + req_ctx->dma_mode = hstate->dma_mode; + req_ctx->hw_initialized = hstate->hw_initialized; + memcpy(req_ctx->buffer, hstate->buffer, HASH_BLOCK_SIZE); + + /* + * Restore hardware state + * INIT bit. Set this bit to 0b1 to reset the HASH processor core and + * prepare the initialize the HASH accelerator to compute the message + * digest of a new message. + */ + HASH_INITIALIZE; + + cr = hstate->temp_cr; + writel_relaxed(cr & HASH_CR_RESUME_MASK, &device_data->base->cr); + + if (readl(&device_data->base->cr) & HASH_CR_MODE_MASK) + hash_mode = HASH_OPER_MODE_HMAC; + else + hash_mode = HASH_OPER_MODE_HASH; + + for (count = 0; count < HASH_CSR_COUNT; count++) { + if ((count >= 36) && (hash_mode == HASH_OPER_MODE_HASH)) + break; + writel_relaxed(hstate->csr[count], + &device_data->base->csrx[count]); + } + + writel_relaxed(hstate->csfull, &device_data->base->csfull); + writel_relaxed(hstate->csdatain, &device_data->base->csdatain); + writel_relaxed(hstate->str_reg, &device_data->base->str); + writel_relaxed(cr, &device_data->base->cr); + + return 0; } -static int ahash_noexport(struct ahash_request *req, void *out) +static int ahash_export(struct ahash_request *req, void *out) { - return -ENOSYS; + 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); + struct hash_state *hstate = out; + int hash_mode = HASH_OPER_MODE_HASH; + u32 cr; + u32 count; + + /* + * Save hardware state: + * Write dummy value to force digest intermediate calculation. This + * actually makes sure that there isn't any ongoing calculation in the + * hardware. + */ + while (readl(&device_data->base->str) & HASH_STR_DCAL_MASK) + cpu_relax(); + + cr = readl_relaxed(&device_data->base->cr); + + hstate->str_reg = readl_relaxed(&device_data->base->str); + + hstate->din_reg = readl_relaxed(&device_data->base->din); + + if (readl(&device_data->base->cr) & HASH_CR_MODE_MASK) + hash_mode = HASH_OPER_MODE_HMAC; + else + hash_mode = HASH_OPER_MODE_HASH; + + for (count = 0; count < HASH_CSR_COUNT; count++) { + if ((count >= 36) && (hash_mode == HASH_OPER_MODE_HASH)) + break; + + hstate->csr[count] = + readl_relaxed(&device_data->base->csrx[count]); + } + + hstate->csfull = readl_relaxed(&device_data->base->csfull); + hstate->csdatain = readl_relaxed(&device_data->base->csdatain); + + hstate->temp_cr = cr; + + /* Save software state */ + hstate->length = req_ctx->length; + hstate->index = req_ctx->index; + hstate->dma_mode = req_ctx->dma_mode; + hstate->hw_initialized = req_ctx->hw_initialized; + memcpy(hstate->buffer, req_ctx->buffer, HASH_BLOCK_SIZE); + + return 0; } static int hmac_sha1_init(struct ahash_request *req) @@ -1361,10 +1352,10 @@ static struct hash_algo_template hash_algs[] = { .update = ahash_update, .final = ahash_final, .digest = ahash_sha1_digest, - .export = ahash_noexport, - .import = ahash_noimport, + .export = ahash_export, + .import = ahash_import, .halg.digestsize = SHA1_DIGEST_SIZE, - .halg.statesize = sizeof(struct hash_ctx), + .halg.statesize = sizeof(struct hash_state), .halg.base = { .cra_name = "sha1", .cra_driver_name = "sha1-ux500", @@ -1384,10 +1375,10 @@ static struct hash_algo_template hash_algs[] = { .update = ahash_update, .final = ahash_final, .digest = ahash_sha256_digest, - .export = ahash_noexport, - .import = ahash_noimport, + .export = ahash_export, + .import = ahash_import, .halg.digestsize = SHA256_DIGEST_SIZE, - .halg.statesize = sizeof(struct hash_ctx), + .halg.statesize = sizeof(struct hash_state), .halg.base = { .cra_name = "sha256", .cra_driver_name = "sha256-ux500", @@ -1408,10 +1399,10 @@ static struct hash_algo_template hash_algs[] = { .final = ahash_final, .digest = hmac_sha1_digest, .setkey = hmac_sha1_setkey, - .export = ahash_noexport, - .import = ahash_noimport, + .export = ahash_export, + .import = ahash_import, .halg.digestsize = SHA1_DIGEST_SIZE, - .halg.statesize = sizeof(struct hash_ctx), + .halg.statesize = sizeof(struct hash_state), .halg.base = { .cra_name = "hmac(sha1)", .cra_driver_name = "hmac-sha1-ux500", @@ -1432,10 +1423,10 @@ static struct hash_algo_template hash_algs[] = { .final = ahash_final, .digest = hmac_sha256_digest, .setkey = hmac_sha256_setkey, - .export = ahash_noexport, - .import = ahash_noimport, + .export = ahash_export, + .import = ahash_import, .halg.digestsize = SHA256_DIGEST_SIZE, - .halg.statesize = sizeof(struct hash_ctx), + .halg.statesize = sizeof(struct hash_state), .halg.base = { .cra_name = "hmac(sha256)", .cra_driver_name = "hmac-sha256-ux500", From patchwork Mon Jul 25 14:05:01 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 593561 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7152DC43334 for ; Mon, 25 Jul 2022 14:08:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235321AbiGYOIF (ORCPT ); Mon, 25 Jul 2022 10:08:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45746 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232682AbiGYOH6 (ORCPT ); Mon, 25 Jul 2022 10:07:58 -0400 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [IPv6:2a00:1450:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B8B1D17A94 for ; Mon, 25 Jul 2022 07:07:38 -0700 (PDT) Received: by mail-lj1-x233.google.com with SMTP id u20so5466992ljk.0 for ; Mon, 25 Jul 2022 07:07:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=yi2lvCepVGwm5m2nCvkz5wHi60hhFaZd2WavgA33JOY=; b=nV1yHgodszVgqTT0PRJYaE3rLNkXFMwuZc/Ahb8HqL0mY/KHLAcHSZ8JJlqt6YwnW0 QeLD5UJFti4ggBKJxaEV66nykmHrwfxFbhiHdSasBhRxHtbPHD5sVpp0x0S4LH1j6pkd tzDovRmoAIJ1l+2QLHTEKCzMk8lBbsWcHJ/Dr6q18jLUtSbV4giipH0LCWaIkECDnlfz fTpT8i+NWskM4ZebkuRxxjcMCmJA8a3t9FOWaDJpUZvH4sF1KQzQhKUzIYqPWA82+NPb /725E390S3N9gWCDIJ+VFvIkV0YFmWSvxNAzBwAGdoNquUK3bGk5Igi6j/gggiADhRRz 1QnQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=yi2lvCepVGwm5m2nCvkz5wHi60hhFaZd2WavgA33JOY=; b=YKttJfvfLu1KA9/Br860zEVRUgV13TycXa6Dw97VpD0e60w7CHPCXpG3doclYU/dfw DE5CA1HexqT0Cp1h3qB8xY/67wPOPEr1aBVwJ504A7CSRYGuypZNMLLOaA2AJ82g/u7D OWhF7mtiuBjZlQvG+xgF1XVtq6dAgSzONDcxEC2iWY6pw7eFsKVNHV/kfYvJMKrRkFf/ DxRDaKH6ZuGf7l3q9SysxVT+BI3R6G9KFIX+c82o2PD/ybOdia+c8BuVN+ulMXJyrsAv bJsic6ddxEv4c26v9vbctEZ9IcyNtSgqhPm/TY6LeDD6auvFQ/G5bq8/nBcpbC87fVzp u8VQ== X-Gm-Message-State: AJIora83bfW6Kc55BWVTyL1C//RP4L0dh04bgSrHfzoVzEuWbQq4fng1 fIad1EpBby93PmW1gpTq/Eq6CQWIEzWgKQ== X-Google-Smtp-Source: AGRyM1vqpWKxgUjA7sTYscL5OkgIL6QlfPeVDFZYgyIRf7MHZLt+8BRJyTOCKlQqWHRwv/7irOFx4Q== X-Received: by 2002:a2e:3109:0:b0:25a:8a0c:40e2 with SMTP id x9-20020a2e3109000000b0025a8a0c40e2mr4155142ljx.26.1658758056835; Mon, 25 Jul 2022 07:07:36 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id o7-20020a05651205c700b0047f7419de4asm901127lfo.180.2022.07.25.07.07.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 07:07:36 -0700 (PDT) From: Linus Walleij To: linux-crypto@vger.kernel.org, Herbert Xu , "David S . Miller" Cc: phone-devel@vger.kernel.org, Stefan Hansson , Linus Walleij Subject: [PATCH 12/15 v2] crypto: ux500/hash: Drop regulator handling Date: Mon, 25 Jul 2022 16:05:01 +0200 Message-Id: <20220725140504.2398965-13-linus.walleij@linaro.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220725140504.2398965-1-linus.walleij@linaro.org> References: <20220725140504.2398965-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org This "APE" voltage is not handled by a regulator but by the power domain, drop it. Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - New patch after noticing the power domain is handling this voltage. --- drivers/crypto/ux500/hash/hash_core.c | 38 +++------------------------ 1 file changed, 4 insertions(+), 34 deletions(-) diff --git a/drivers/crypto/ux500/hash/hash_core.c b/drivers/crypto/ux500/hash/hash_core.c index 390e50b2b1d2..3bd58b60aade 100644 --- a/drivers/crypto/ux500/hash/hash_core.c +++ b/drivers/crypto/ux500/hash/hash_core.c @@ -24,8 +24,6 @@ #include #include #include - -#include #include #include @@ -244,22 +242,17 @@ static int get_empty_message_digest( * hash_disable_power - Request to disable power and clock. * @device_data: Structure for the hash device. * - * This function request for disabling power (regulator) and clock, - * and could also save current hw state. + * This function request for disabling the clock. */ static int hash_disable_power(struct hash_device_data *device_data) { int ret = 0; - struct device *dev = device_data->dev; spin_lock(&device_data->power_state_lock); if (!device_data->power_state) goto out; clk_disable(device_data->clk); - ret = regulator_disable(device_data->regulator); - if (ret) - dev_err(dev, "%s: regulator_disable() failed!\n", __func__); device_data->power_state = false; @@ -273,8 +266,7 @@ static int hash_disable_power(struct hash_device_data *device_data) * hash_enable_power - Request to enable power and clock. * @device_data: Structure for the hash device. * - * This function request for enabling power (regulator) and clock, - * and could also restore a previously saved hw state. + * This function request for enabling the clock. */ static int hash_enable_power(struct hash_device_data *device_data) { @@ -283,17 +275,9 @@ static int hash_enable_power(struct hash_device_data *device_data) spin_lock(&device_data->power_state_lock); if (!device_data->power_state) { - ret = regulator_enable(device_data->regulator); - if (ret) { - dev_err(dev, "%s: regulator_enable() failed!\n", - __func__); - goto out; - } ret = clk_enable(device_data->clk); if (ret) { dev_err(dev, "%s: clk_enable() failed!\n", __func__); - ret = regulator_disable( - device_data->regulator); goto out; } device_data->power_state = true; @@ -1487,27 +1471,17 @@ 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__); - ret = PTR_ERR(device_data->regulator); - device_data->regulator = NULL; - 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__); ret = PTR_ERR(device_data->clk); - goto out_regulator; + goto out; } ret = clk_prepare(device_data->clk); if (ret) { dev_err(dev, "%s: clk_prepare() failed!\n", __func__); - goto out_regulator; + goto out; } /* Enable device power (and clock) */ @@ -1544,9 +1518,6 @@ static int ux500_hash_probe(struct platform_device *pdev) out_clk_unprepare: clk_unprepare(device_data->clk); -out_regulator: - regulator_put(device_data->regulator); - out: return ret; } @@ -1585,7 +1556,6 @@ static int ux500_hash_remove(struct platform_device *pdev) __func__); clk_unprepare(device_data->clk); - regulator_put(device_data->regulator); return 0; } From patchwork Mon Jul 25 14:05:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 593560 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 079B8CCA473 for ; Mon, 25 Jul 2022 14:08:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235376AbiGYOIH (ORCPT ); Mon, 25 Jul 2022 10:08:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46338 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235420AbiGYOIA (ORCPT ); Mon, 25 Jul 2022 10:08:00 -0400 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4DA2217A89 for ; Mon, 25 Jul 2022 07:07:43 -0700 (PDT) Received: by mail-lf1-x12d.google.com with SMTP id y11so18126059lfs.6 for ; Mon, 25 Jul 2022 07:07:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=rzgP+fRPARWak06I6AOjFFbBJ87jnAoawXDDrQ7nXN8=; b=tktI2KG9Y/wcP8pehTIB2urKwCsFFMDPGe5uM3LPTsJDkFE9zix7ZNu6kHxFLL96d6 gJOlJU2+YIJv0+AdhRnxq8KrJlKBXaXTuJgWrlLw5oZOHugXo2QG8fRgKjofwVYWBVEo hIotTZkNhXn/RPSAxY7hZkHqWKnGKqmJk06QzHqKqRD0dNtQL1jOxyGScX0ifBKQj18T dE0BuOO77crNjFaFCa1LsWEx6UfpKxuxtiQaSkRxwrJODhODSZGNHeG0c2kSGI5FJSce tq/jEWZxc7TQYmrZ9sks65u8m+/bZjxLRaKvaA5mTR1DOvTJctuYeOiJiFKAgYy+vAqg wW7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=rzgP+fRPARWak06I6AOjFFbBJ87jnAoawXDDrQ7nXN8=; b=qkOHzwApW47WB7xkXbld2pFY9Kv/1HE50pAKTqVq43xWP7FF7aFMdVfKEgKMZIB7Jc sgafeC4gfCxkQJC3ANuAXCk07FsY+DqR8ds5CXL2uw3qRuhD7ha9draV1t+9dwdaCDNk p1c+MGFXzRP2PH9bcuZWzP4yddVnEskf1LRqIco7Sh5EZkjiK0cKp7ziWrha5lmUhyc/ FPDsmDX/SeiFEB0Zk+tj1rmJFqy92Ek1uV/Dk6pnrZPwaoIMJZrnv4gxGg21dAPq4dbd /O0/uHzcBq4pa7yuWRC/VxC2amCD/3f1tKbXlkKrHkfV3bgezV2cUxaVSU/CruRQZqWy dZvg== X-Gm-Message-State: AJIora8V/KTp3MEiJTBjL2+re4csoJl/dBedcxEvGvqHdyTFcCiKdpcf 8I+h5NjMC4jYO+UxPjsbq4tLGwT17J0sTw== X-Google-Smtp-Source: AGRyM1tQQbFddQZ2mAD4qxR5UHI1LCl1fJEM3UPn4AUOXEnt8taIT5fyv7dxh6WFglSOcPfSwI4SjA== X-Received: by 2002:a05:6512:1681:b0:48a:2e71:e202 with SMTP id bu1-20020a056512168100b0048a2e71e202mr4797141lfb.378.1658758061274; Mon, 25 Jul 2022 07:07:41 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id o7-20020a05651205c700b0047f7419de4asm901127lfo.180.2022.07.25.07.07.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Jul 2022 07:07:40 -0700 (PDT) From: Linus Walleij To: linux-crypto@vger.kernel.org, Herbert Xu , "David S . Miller" Cc: phone-devel@vger.kernel.org, Stefan Hansson , Linus Walleij Subject: [PATCH 15/15 v2] crypto: ux500/hash: Implement runtime PM Date: Mon, 25 Jul 2022 16:05:04 +0200 Message-Id: <20220725140504.2398965-16-linus.walleij@linaro.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220725140504.2398965-1-linus.walleij@linaro.org> References: <20220725140504.2398965-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org This implements runtime PM to gate the clock and regulator when the hash block is unused. Drop the own-invented "power state" and its associated lock: we don't need that when we have runtime PM. Delete the specific power functions and just enable/disable the clock in the runtime PM functions. Use the full *_prepare_enable() and *disable_unprepare() calls for really making sure the clock is off. Signed-off-by: Linus Walleij --- ChangeLog v1->v2: - New patch to clean up also the custom PM. --- drivers/crypto/ux500/hash/hash_alg.h | 3 +- drivers/crypto/ux500/hash/hash_core.c | 164 +++++++++++--------------- 2 files changed, 71 insertions(+), 96 deletions(-) diff --git a/drivers/crypto/ux500/hash/hash_alg.h b/drivers/crypto/ux500/hash/hash_alg.h index 96c614444fa2..344f2294a938 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 @@ -221,8 +222,6 @@ struct hash_device_data { struct device *dev; spinlock_t ctx_lock; struct hash_ctx *current_ctx; - bool power_state; - spinlock_t power_state_lock; struct regulator *regulator; struct clk *clk; struct hash_dma dma; diff --git a/drivers/crypto/ux500/hash/hash_core.c b/drivers/crypto/ux500/hash/hash_core.c index 71ed2a573714..8a2da25f69ca 100644 --- a/drivers/crypto/ux500/hash/hash_core.c +++ b/drivers/crypto/ux500/hash/hash_core.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -240,57 +241,6 @@ static int get_empty_message_digest( return ret; } -/** - * hash_disable_power - Request to disable power and clock. - * @device_data: Structure for the hash device. - * - * This function request for disabling the clock. - */ -static int hash_disable_power(struct hash_device_data *device_data) -{ - int ret = 0; - - spin_lock(&device_data->power_state_lock); - if (!device_data->power_state) - goto out; - - clk_disable(device_data->clk); - - device_data->power_state = false; - -out: - spin_unlock(&device_data->power_state_lock); - - return ret; -} - -/** - * hash_enable_power - Request to enable power and clock. - * @device_data: Structure for the hash device. - * - * This function request for enabling the clock. - */ -static int hash_enable_power(struct hash_device_data *device_data) -{ - int ret = 0; - struct device *dev = device_data->dev; - - spin_lock(&device_data->power_state_lock); - if (!device_data->power_state) { - ret = clk_enable(device_data->clk); - if (ret) { - dev_err(dev, "%s: clk_enable() failed!\n", __func__); - goto out; - } - device_data->power_state = true; - } - -out: - spin_unlock(&device_data->power_state_lock); - - return ret; -} - static void hash_wait_for_dcal(struct hash_device_data *device_data) { unsigned int val; @@ -449,6 +399,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; @@ -474,6 +428,7 @@ static int ux500_hash_init(struct ahash_request *req) } } } + return 0; } @@ -782,6 +737,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. */ @@ -1040,8 +997,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); @@ -1050,6 +1010,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__); } @@ -1150,6 +1113,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 @@ -1217,6 +1183,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; } @@ -1593,7 +1563,6 @@ static int ux500_hash_probe(struct platform_device *pdev) */ regcache_cache_bypass(device_data->map, true); spin_lock_init(&device_data->ctx_lock); - spin_lock_init(&device_data->power_state_lock); device_data->clk = devm_clk_get(dev, NULL); if (IS_ERR(device_data->clk)) { @@ -1602,25 +1571,24 @@ static int ux500_hash_probe(struct platform_device *pdev) goto out; } - ret = clk_prepare(device_data->clk); + ret = clk_prepare_enable(device_data->clk); if (ret) { - dev_err(dev, "%s: clk_prepare() failed!\n", __func__); + dev_err(dev, "%s: clk_prepare_enable() failed!\n", __func__); goto out; } - /* Enable device power (and clock) */ - ret = hash_enable_power(device_data); - if (ret) { - dev_err(dev, "%s: hash_enable_power() failed!\n", __func__); - goto out_clk_unprepare; - } - ret = hash_check_hw(device_data); if (ret) { dev_err(dev, "%s: hash_check_hw() failed!\n", __func__); - goto out_power; + goto out_clk; } + 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); @@ -1630,18 +1598,20 @@ static int ux500_hash_probe(struct platform_device *pdev) if (ret) { dev_err(dev, "%s: ahash_algs_register_all() failed!\n", __func__); - goto out_power; + goto out_pm_disable; } + pm_runtime_put_sync(dev); dev_info(dev, "successfully registered\n"); - return 0; - -out_power: - hash_disable_power(device_data); -out_clk_unprepare: - clk_unprepare(device_data->clk); + return 0; +out_pm_disable: + pm_runtime_get_sync(device_data->dev); + pm_runtime_put_noidle(device_data->dev); + pm_runtime_disable(device_data->dev); +out_clk: + clk_disable_unprepare(device_data->clk); out: return ret; } @@ -1675,11 +1645,10 @@ static int ux500_hash_remove(struct platform_device *pdev) ahash_algs_unregister_all(device_data); - if (hash_disable_power(device_data)) - dev_err(dev, "%s: hash_disable_power() failed\n", - __func__); - - clk_unprepare(device_data->clk); + pm_runtime_get_sync(device_data->dev); + pm_runtime_put_noidle(device_data->dev); + pm_runtime_disable(device_data->dev); + clk_disable_unprepare(device_data->clk); return 0; } @@ -1714,17 +1683,17 @@ static void ux500_hash_shutdown(struct platform_device *pdev) ahash_algs_unregister_all(device_data); - if (hash_disable_power(device_data)) - dev_err(&pdev->dev, "%s: hash_disable_power() failed\n", - __func__); + pm_runtime_get_sync(device_data->dev); + pm_runtime_put_noidle(device_data->dev); + pm_runtime_disable(device_data->dev); + clk_disable_unprepare(device_data->clk); } -#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 __maybe_unused ux500_hash_runtime_suspend(struct device *dev) { int ret; struct hash_device_data *device_data; @@ -1735,21 +1704,20 @@ static int ux500_hash_suspend(struct device *dev) return -ENOMEM; } - ret = hash_disable_power(device_data); - if (ret) - dev_err(dev, "%s: hash_disable_power()\n", __func__); + clk_disable_unprepare(device_data->clk); + 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 __maybe_unused ux500_hash_runtime_resume(struct device *dev) { - int ret = 0; struct hash_device_data *device_data; + int ret; device_data = dev_get_drvdata(dev); if (!device_data) { @@ -1757,15 +1725,23 @@ static int ux500_hash_resume(struct device *dev) return -ENOMEM; } - ret = hash_enable_power(device_data); - if (ret) - dev_err(dev, "%s: hash_enable_power() failed!\n", __func__); + ret = clk_prepare_enable(device_data->clk); + if (ret) { + dev_err(dev, "%s: clk_enable() failed!\n", __func__); + return ret; + } - return ret; + dev_info(dev, "runtime resumed\n"); + + return 0; } -#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" }, @@ -1780,7 +1756,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);