Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2152353rwd; Thu, 15 Jun 2023 23:12:31 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6WrfdWNNkmMWemTRE4J6MkYvRsbeIx/uHMz4Wr/l0vGip7d5JOnlBhscF8u9FUkzA0CO9W X-Received: by 2002:a17:90b:1894:b0:259:1812:c9f9 with SMTP id mn20-20020a17090b189400b002591812c9f9mr904246pjb.1.1686895950819; Thu, 15 Jun 2023 23:12:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686895950; cv=none; d=google.com; s=arc-20160816; b=va2zGL43i/TtZytycBn0Wz49FpLEWZo6G/mce9YRu54ICmaO+a0JtQaTTbMIQPzgpa 1rRyX2N6q/izbYNFsK22PkLsa9vGZiEQoBOEY4rwX9P9EHi4lICqdOPM64EOKmBRzMuo AaRZvBgm/XkUY3pgzYImczRVdJz3MtyIqfNk8Dst33Hdhr0b52doF5x2WIya0bdURvnT CoDg0YZKj6U+4UJZjrIcyKY+nBBD01ig4v1PWuvFWpvCYsMub2zekGJsSBdP4/+Fwo+x fpXV6bm4ROu0MpObRNqaxf59FTHljz7xa1zKG9eKWoRdxxqtXEj3VPV8aehRP4x3F/+E vK6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from; bh=+O4ZZzXJ8JKEmQO9b9mdmv6dAEv1neFaBHBuAycrkL8=; b=WRIWUvv7yYbCWQWvM03hETN2AdWhjFW94yhS2tNXp+Vt5P1/4HOdCe4I3szuH9Tbk+ oHWy+7JOtDI3NYxJmOn27UPeVFqZZFenpGrd3wYDM4TbtGvW0hpA4RTvlAmCu7D2WOFX e4czRTqdPwehwoB71qXhwSUnHQ8BgYrANOSZGoTvg0uwbf2QzSUnpuVQvvbNukqvbIhn X6jhzaU7WD+abp1U3pn4F0HIcnxGz5DRTBQpyc9XJK1OxRcg1AIUkMj/zXO1kMCqHxf+ ++T0qSABwI6DSp6JNus6+kpwxfm0JGFB4M8owwqkJ5oNUNU+/Xrr2+gj3LX0pBghoU/q dQVg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id y12-20020a17090aa40c00b0024e0f3308a0si469695pjp.32.2023.06.15.23.12.18; Thu, 15 Jun 2023 23:12:30 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233557AbjFPFxP (ORCPT + 99 others); Fri, 16 Jun 2023 01:53:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41436 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241268AbjFPFxI (ORCPT ); Fri, 16 Jun 2023 01:53:08 -0400 Received: from inva020.nxp.com (inva020.nxp.com [92.121.34.13]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 807162D5A for ; Thu, 15 Jun 2023 22:53:03 -0700 (PDT) Received: from inva020.nxp.com (localhost [127.0.0.1]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 6EE931A033D; Fri, 16 Jun 2023 07:43:05 +0200 (CEST) Received: from aprdc01srsp001v.ap-rdc01.nxp.com (aprdc01srsp001v.ap-rdc01.nxp.com [165.114.16.16]) by inva020.eu-rdc02.nxp.com (Postfix) with ESMTP id 0D9F61A05B8; Fri, 16 Jun 2023 07:43:05 +0200 (CEST) Received: from localhost.localdomain (mega.ap.freescale.net [10.192.208.232]) by aprdc01srsp001v.ap-rdc01.nxp.com (Postfix) with ESMTP id 094A61820F58; Fri, 16 Jun 2023 13:43:03 +0800 (+08) From: Alison Wang To: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Cc: leoyang.li@nxp.com, xuelin.shi@nxp.com, xiaofeng.ren@nxp.com, feng.guo@nxp.com, Alison Wang Subject: [PATCH 4/8] ethosu: Add suspend/resume power management Date: Fri, 16 Jun 2023 13:59:09 +0800 Message-Id: <20230616055913.2360-5-alison.wang@nxp.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230616055913.2360-1-alison.wang@nxp.com> References: <20230616055913.2360-1-alison.wang@nxp.com> X-Virus-Scanned: ClamAV using ClamSMTP X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds suspend and resume power management support. The corresponding message will be sent to Cortex-M core via RPMsg protocol. Signed-off-by: Alison Wang Signed-off-by: Feng Guo Reviewed-by: Peng Fan --- drivers/firmware/ethosu/ethosu_buffer.c | 3 ++ .../firmware/ethosu/ethosu_core_interface.h | 14 ++++++++ drivers/firmware/ethosu/ethosu_device.c | 36 +++++++++++++++++++ drivers/firmware/ethosu/ethosu_device.h | 10 ++++++ drivers/firmware/ethosu/ethosu_driver.c | 29 +++++++++++++++ drivers/firmware/ethosu/ethosu_rpmsg.c | 30 ++++++++++++++++ drivers/firmware/ethosu/ethosu_rpmsg.h | 11 ++++++ 7 files changed, 133 insertions(+) diff --git a/drivers/firmware/ethosu/ethosu_buffer.c b/drivers/firmware/ethosu/ethosu_buffer.c index 43a433355f30..c8aa8031a8de 100644 --- a/drivers/firmware/ethosu/ethosu_buffer.c +++ b/drivers/firmware/ethosu/ethosu_buffer.c @@ -222,6 +222,9 @@ int ethosu_buffer_create(struct ethosu_device *edev, struct ethosu_buffer *buf; int ret = -ENOMEM; + if (!capacity) + return -EINVAL; + buf = devm_kzalloc(edev->dev, sizeof(*buf), GFP_KERNEL); if (!buf) return -ENOMEM; diff --git a/drivers/firmware/ethosu/ethosu_core_interface.h b/drivers/firmware/ethosu/ethosu_core_interface.h index b60508e4792f..9267e96aec9b 100644 --- a/drivers/firmware/ethosu/ethosu_core_interface.h +++ b/drivers/firmware/ethosu/ethosu_core_interface.h @@ -60,6 +60,8 @@ enum ethosu_core_msg_type { ETHOSU_CORE_MSG_VERSION_RSP, ETHOSU_CORE_MSG_CAPABILITIES_REQ, ETHOSU_CORE_MSG_CAPABILITIES_RSP, + ETHOSU_CORE_MSG_POWER_REQ, + ETHOSU_CORE_MSG_POWER_RSP, ETHOSU_CORE_MSG_MAX }; @@ -161,6 +163,18 @@ struct ethosu_core_msg_capabilities_rsp { uint32_t custom_dma; }; +enum ethosu_core_power_req_type { + ETHOSU_CORE_POWER_REQ_SUSPEND = 0, + ETHOSU_CORE_POWER_REQ_RESUME +}; + +/** + * struct ethosu_core_power_req - Message power request + */ +struct ethosu_core_power_req { + enum ethosu_core_power_req_type type; +}; + /** * enum ethosu_core_msg_err_type - Error types */ diff --git a/drivers/firmware/ethosu/ethosu_device.c b/drivers/firmware/ethosu/ethosu_device.c index b2d4737080ac..1627f25c8a95 100644 --- a/drivers/firmware/ethosu/ethosu_device.c +++ b/drivers/firmware/ethosu/ethosu_device.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include @@ -49,6 +50,8 @@ #define CAPABILITIES_RESP_TIMEOUT_MS 2000 +#define ETHOSU_FIRMWARE_NAME "ethosu_firmware" + /**************************************************************************** * Types ****************************************************************************/ @@ -161,6 +164,9 @@ static int ethosu_handle_msg(struct ethosu_device *edev, void *data) case ETHOSU_CORE_MSG_PONG: dev_info(edev->dev, "Msg: Pong\n"); break; + case ETHOSU_CORE_MSG_POWER_RSP: + dev_info(edev->dev, "Msg: Power response\n"); + break; case ETHOSU_CORE_MSG_INFERENCE_RSP: if (header->length != sizeof(struct ethosu_core_inference_rsp)) { dev_warn(edev->dev, @@ -243,11 +249,41 @@ static int ethosu_open(struct inode *inode, { struct ethosu_device *edev = container_of(inode->i_cdev, struct ethosu_device, cdev); + phandle rproc_phandle; + struct rproc *rproc; + int ret = 0; file->private_data = edev; dev_info(edev->dev, "Opening device node.\n"); + if (of_property_read_u32(edev->dev->of_node, "fsl,cm33-proc", + &rproc_phandle)) { + dev_err(edev->dev, "could not get rproc phandle\n"); + return -ENODEV; + } + + rproc = rproc_get_by_phandle(rproc_phandle); + if (!rproc) { + dev_err(edev->dev, "could not get rproc handle\n"); + return -EINVAL; + } + + ret = rproc_set_firmware(rproc, ETHOSU_FIRMWARE_NAME); + + if (!ret && atomic_read(&rproc->power) == 0) { + init_completion(&edev->erp.rpmsg_ready); + ret = rproc_boot(rproc); + if (ret) + dev_err(edev->dev, "could not boot a remote processor\n"); + else + wait_for_completion_interruptible(&edev->erp.rpmsg_ready); + } else { + dev_err(edev->dev, "can't change firmware or remote processor is running\n"); + } + + edev->open = true; + return nonseekable_open(inode, file); } diff --git a/drivers/firmware/ethosu/ethosu_device.h b/drivers/firmware/ethosu/ethosu_device.h index 3943c8df8f28..da470241b6a1 100644 --- a/drivers/firmware/ethosu/ethosu_device.h +++ b/drivers/firmware/ethosu/ethosu_device.h @@ -51,6 +51,7 @@ struct ethosu_device { struct ethosu_rpmsg erp; struct list_head capabilities_list; struct list_head inference_list; + bool open; }; /** @@ -83,4 +84,13 @@ int ethosu_dev_init(struct ethosu_device *edev, */ void ethosu_dev_deinit(struct ethosu_device *edev); +/** + * ethosu_suspend() - Suspend the device + */ +int ethosu_suspend(struct device *dev); +/** + * ethosu_resume() - Resume the device + */ +int ethosu_resume(struct device *dev); + #endif /* ETHOSU_DEVICE_H */ diff --git a/drivers/firmware/ethosu/ethosu_driver.c b/drivers/firmware/ethosu/ethosu_driver.c index 27931e9aa97c..d3306ef625ae 100644 --- a/drivers/firmware/ethosu/ethosu_driver.c +++ b/drivers/firmware/ethosu/ethosu_driver.c @@ -27,6 +27,7 @@ #include #include +#include "ethosu_core_interface.h" #include "ethosu_device.h" /**************************************************************************** @@ -101,6 +102,33 @@ static int ethosu_pdev_remove(struct platform_device *pdev) return 0; } +int ethosu_suspend(struct device *dev) +{ + struct ethosu_device *edev = dev->driver_data; + int ret = 0; + + if (edev->open) + ret = ethosu_rpmsg_power_request(&edev->erp, ETHOSU_CORE_POWER_REQ_SUSPEND); + + return ret; +} + +int ethosu_resume(struct device *dev) +{ + struct ethosu_device *edev = dev->driver_data; + int ret = 0; + + if (edev->open) + ret = ethosu_rpmsg_power_request(&edev->erp, ETHOSU_CORE_POWER_REQ_RESUME); + + return ret; +} + +struct dev_pm_ops ethosu_pm_ops = { + .suspend = ethosu_suspend, + .resume = ethosu_resume, +}; + static const struct of_device_id ethosu_pdev_match[] = { { .compatible = "arm,ethosu" }, { /* Sentinel */ }, @@ -115,6 +143,7 @@ static struct platform_driver ethosu_pdev_driver = { .name = ETHOSU_DRIVER_NAME, .owner = THIS_MODULE, .of_match_table = of_match_ptr(ethosu_pdev_match), + .pm = ðosu_pm_ops, }, }; diff --git a/drivers/firmware/ethosu/ethosu_rpmsg.c b/drivers/firmware/ethosu/ethosu_rpmsg.c index e4cf398468e4..a0320b8407d1 100644 --- a/drivers/firmware/ethosu/ethosu_rpmsg.c +++ b/drivers/firmware/ethosu/ethosu_rpmsg.c @@ -98,6 +98,35 @@ int ethosu_rpmsg_capabilities_request(struct ethosu_rpmsg *erp, void *user_arg) return 0; } +int ethosu_rpmsg_power_request(struct ethosu_rpmsg *erp, + enum ethosu_core_power_req_type power_type) +{ + struct ethosu_core_msg msg = { + .magic = ETHOSU_CORE_MSG_MAGIC, + .type = ETHOSU_CORE_MSG_POWER_REQ, + .length = sizeof(struct ethosu_core_power_req) + }; + struct ethosu_core_power_req req; + struct rpmsg_device *rpdev = erp->rpdev; + uint8_t data[sizeof(struct ethosu_core_msg) + + sizeof(struct ethosu_core_power_req)]; + int ret; + + req.type = power_type; + memcpy(data, &msg, sizeof(struct ethosu_core_msg)); + memcpy(data + sizeof(struct ethosu_core_msg), &req, + sizeof(struct ethosu_core_power_req)); + + ret = rpmsg_send(rpdev->ept, (void *)&data, + sizeof(struct ethosu_core_msg) + + sizeof(struct ethosu_core_power_req)); + if (ret) { + dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret); + return ret; + } + return 0; +} + int ethosu_rpmsg_inference(struct ethosu_rpmsg *erp, void *user_arg, uint32_t ifm_count, @@ -193,6 +222,7 @@ static int ethosu_rpmsg_probe(struct rpmsg_device *rpdev) if (ret) { dev_err(&rpdev->dev, "rpmsg_send failed: %d\n", ret); } + complete(&grp->rpmsg_ready); return 0; } diff --git a/drivers/firmware/ethosu/ethosu_rpmsg.h b/drivers/firmware/ethosu/ethosu_rpmsg.h index a4a639997a26..cee3d96c7895 100644 --- a/drivers/firmware/ethosu/ethosu_rpmsg.h +++ b/drivers/firmware/ethosu/ethosu_rpmsg.h @@ -7,6 +7,8 @@ #define ETHOSU_RPMSG_H #include +#include +#include "ethosu_core_interface.h" struct device; struct ethosu_buffer; @@ -19,6 +21,7 @@ struct ethosu_rpmsg { struct rpmsg_device *rpdev; ethosu_rpmsg_cb callback; void *user_arg; + struct completion rpmsg_ready; }; /** * ethosu_rpmsg_ping() - Send ping message @@ -49,6 +52,14 @@ int ethosu_rpmsg_version_request(struct ethosu_rpmsg *erp); int ethosu_rpmsg_capabilities_request(struct ethosu_rpmsg *erp, void *user_arg); +/** + * ethosu_rpmsg_power_request - Send power request + * + * Return: 0 on success, else error code + */ +int ethosu_rpmsg_power_request(struct ethosu_rpmsg *erp, + enum ethosu_core_power_req_type power_type); + /** * ethosu_rpmsg_inference() - Send inference * -- 2.17.1