From patchwork Fri Mar 17 17:28:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Etienne Carriere X-Patchwork-Id: 13179334 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id A5D9DC74A5B for ; Fri, 17 Mar 2023 17:30:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=xOaOE8Z+nbNv4NqeCgoHR3kCY1e3tKOA/pTbvpW3LPQ=; b=KaMKYtkUfpf6+k tp5TeaXk7z0eKjJPI2EIb+raQEuNO3KhUY6T0C3coI9tMWTw/GHUxaKcuc4YAKPHfKIiafDHZBtPQ qNYVC4wgL5wfWdgCGSST4HqU1ZlsbZQQoJDxlyDb87hDrSf3QqepCbRpwLGhq59K33A7rI3zBAxoK znFJqHLtVTEak8p1UuAg3P3wzX7aQQSibw0ByY9ggXmJtM8dSV/ssu9KMomaQQ/25G8hr8+wp2yu+ L1x/jL63N083LQz2lNFaEi9tHl+f96sPMNJCyrDH82eH+tvwaWP5eElLPk7BdwBqb8/toWZby+CFF zBhC0MU1fUfgeMIkXtug==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pdDtB-002tDf-1W; Fri, 17 Mar 2023 17:29:13 +0000 Received: from mail-ed1-x535.google.com ([2a00:1450:4864:20::535]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pdDt3-002tBu-0O for linux-arm-kernel@lists.infradead.org; Fri, 17 Mar 2023 17:29:06 +0000 Received: by mail-ed1-x535.google.com with SMTP id fd5so23202629edb.7 for ; Fri, 17 Mar 2023 10:29:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1679074143; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9RN/6059EmJ8uecEffZz5gMIvnjjePGQHRIIWUkBdNM=; b=Ut/RR2xA56vEYKrKuS7gEo2q7AVA0iFdxzfP2WAnh6Ag7Ja3e9TZZyuNBuXkY2/RaA bWYJAMWbSkEVCwpz0K6Or0TamzEPRMgnKA8NmsmgxUq3uBQk0P/h6FAl4SvTdAgqicb4 Tcx4OU8+A60lqCbJjZdCn6e8O3cIscx+VnzYYZyAn+/gD5tEPrs7xR0t5YUSrIFg/TIM WaKe/2CjwBdoxq+gCFwfFmmfWssB3hONH25pIKGz0dqbHD8iV5nsIIsPcMFoICPW4PEK cGBRSZFJY5HmHAt+hR5S2tzISVr/7xV3CEVeQZG1Dntr6fgV/HQ+GeCPIbCobPbfL4K9 PNEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679074143; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9RN/6059EmJ8uecEffZz5gMIvnjjePGQHRIIWUkBdNM=; b=t+whUXgt+rBW4Pcs2Wldsn6D19NGTg1W5YL58I966MhcM+8rlO0cQq10HwuOr8TuMh lNAWJPrAoc9Dg90f8cFVWf2AP1OoXy0sCOkQj8gdlA/UF+mYDzx5DqyGn2w8vZP/UH6W qBF2apwGplRHf+C4tNDP/LBAL9OjmPXoWdeDJpMyNuk16tjJaawTTeZ9fh78LKIqHMWX 6EovN71TQqxVFfLnj3CTqB3an8DGBJ34jJLzNhe2c0r0hP/RdyzBsMXQD6M7unf+SIji y+dkx/LvGxtRveCIMVbmCHXjJtM8mLpQPzPQ9sojqmodZsc3wqf7iR7A7XUJX2b4aIhY +xXA== X-Gm-Message-State: AO0yUKV6vw6ZYmtZO6Utt6PQtJGY7f2g42nR4fNeXtGmTEVZSmFfoSwL VhbjrFGIc3eZrpk+D2T59s7f0A== X-Google-Smtp-Source: AK7set/x889DPM/ZzoOHbdJRs3xuOvqtj4A/489Wofixk5wHGFBTaj30HKkNa1g01923INNvo9kijg== X-Received: by 2002:a17:906:849a:b0:931:cac0:60ec with SMTP id m26-20020a170906849a00b00931cac060ecmr114338ejx.48.1679074143629; Fri, 17 Mar 2023 10:29:03 -0700 (PDT) Received: from lmecxl1178.lme.st.com ([2a04:cec0:10af:6120:4312:7f15:6b35:a247]) by smtp.gmail.com with ESMTPSA id la18-20020a170907781200b00914001c91fcsm1200089ejc.86.2023.03.17.10.29.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Mar 2023 10:29:03 -0700 (PDT) From: Etienne Carriere To: linux-kernel@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, Sumit Garg , Krzysztof Kozlowski , Rob Herring , Jens Wiklander , Etienne Carriere , Marc Zyngier , Alexandre Torgue Subject: [PATCH v5 2/2] optee: add per cpu asynchronous notification Date: Fri, 17 Mar 2023 18:28:59 +0100 Message-Id: <20230317172859.989650-2-etienne.carriere@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230317172859.989650-1-etienne.carriere@linaro.org> References: <20230317172859.989650-1-etienne.carriere@linaro.org> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230317_102905_160652_92C9D50D X-CRM114-Status: GOOD ( 24.41 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Implements use of per-cpu irq for optee asynchronous notification. Existing optee async notif implementation allows OP-TEE world to raise an interrupt on which Linux optee driver will query some pending events. This change allows the signaling interrupt to be a per-cpu interrupt as with Arm GIC PPIs. Using a PPI instead of an SPI is useful when no GIC lines are provisioned in the chip design and there are spare PPI lines. Cc: Jens Wiklander Cc: Sumit Garg Cc: Marc Zyngier Co-developed-by: Alexandre Torgue Signed-off-by: Alexandre Torgue Signed-off-by: Etienne Carriere --- Changes since v4: - Used cpuhp_setup_state()/cpuhp_remove_state() to enable/disable the per-cpu interrupt and let CPU hotplug also manage it. - Added a call to destroy_workqueue() that was missing in uninit_pcpu_irq(). - Rephrased commit message. Changes since v3: - Fixed typo in commit message. - Added few words in commit message about why we do this change. - Appended a 2nd commit to the series for request DT bindings update. Changes since v2: - Irq and per-cpu irq no more share the primary same handler function but have a common irq_handler() helper function. - Removed useless spinlocks. - Wrapped lines > 80 char. Changes since v1: - Fixed missing __percpu attribute reported by kernel test robot. - Rephrased commit message and added Cc tags. --- drivers/tee/optee/optee_private.h | 24 ++++++ drivers/tee/optee/smc_abi.c | 125 +++++++++++++++++++++++++++++- 2 files changed, 145 insertions(+), 4 deletions(-) diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h index 04ae58892608..6129d43dd354 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -94,11 +94,35 @@ struct optee_supp { struct completion reqs_c; }; +/* + * struct optee_pcpu - per cpu notif private struct passed to work functions + * @optee optee device reference + */ +struct optee_pcpu { + struct optee *optee; +}; + +/* + * struct optee_smc - optee smc communication struct + * @invoke_fn handler function to invoke secure monitor + * @memremaped_shm virtual address of memory in shared memory pool + * @sec_caps: secure world capabilities defined by + * OPTEE_SMC_SEC_CAP_* in optee_smc.h + * @notif_irq interrupt used as async notification by OP-TEE or 0 + * @optee_pcpu per_cpu optee instance for per cpu work or NULL + * @notif_pcpu_wq workqueue for per cpu aynchronous notification or NULL + * @notif_pcpu_work work for per cpu asynchronous notification + * @notif_cpuhp_state CPU hotplug state assigned for pcpu interrupt management + */ struct optee_smc { optee_invoke_fn *invoke_fn; void *memremaped_shm; u32 sec_caps; unsigned int notif_irq; + struct optee_pcpu __percpu *optee_pcpu; + struct workqueue_struct *notif_pcpu_wq; + struct work_struct notif_pcpu_work; + unsigned int notif_cpuhp_state; }; /** diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index a1c1fa1a9c28..a2473fbe2461 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -52,6 +52,23 @@ */ #define OPTEE_MIN_STATIC_POOL_ALIGN 9 /* 512 bytes aligned */ +/* SMC ABI considers at most a single TEE firmware */ +static unsigned int pcpu_irq_num; + +static int optee_cpuhp_enable_pcpu_irq(unsigned int cpu) +{ + enable_percpu_irq(pcpu_irq_num, IRQ_TYPE_NONE); + + return 0; +} + +static int optee_cpuhp_disable_pcpu_irq(unsigned int cpu) +{ + disable_percpu_irq(pcpu_irq_num); + + return 0; +} + /* * 1. Convert between struct tee_param and struct optee_msg_param * @@ -991,9 +1008,8 @@ static u32 get_async_notif_value(optee_invoke_fn *invoke_fn, bool *value_valid, return res.a1; } -static irqreturn_t notif_irq_handler(int irq, void *dev_id) +static irqreturn_t irq_handler(struct optee *optee) { - struct optee *optee = dev_id; bool do_bottom_half = false; bool value_valid; bool value_pending; @@ -1016,6 +1032,13 @@ static irqreturn_t notif_irq_handler(int irq, void *dev_id) return IRQ_HANDLED; } +static irqreturn_t notif_irq_handler(int irq, void *dev_id) +{ + struct optee *optee = dev_id; + + return irq_handler(optee); +} + static irqreturn_t notif_irq_thread_fn(int irq, void *dev_id) { struct optee *optee = dev_id; @@ -1025,7 +1048,7 @@ static irqreturn_t notif_irq_thread_fn(int irq, void *dev_id) return IRQ_HANDLED; } -static int optee_smc_notif_init_irq(struct optee *optee, u_int irq) +static int init_irq(struct optee *optee, u_int irq) { int rc; @@ -1040,12 +1063,106 @@ static int optee_smc_notif_init_irq(struct optee *optee, u_int irq) return 0; } +static irqreturn_t notif_pcpu_irq_handler(int irq, void *dev_id) +{ + struct optee_pcpu __percpu *pcpu = (struct optee_pcpu *)dev_id; + struct optee *optee = pcpu->optee; + + if (irq_handler(optee) == IRQ_WAKE_THREAD) + queue_work(optee->smc.notif_pcpu_wq, + &optee->smc.notif_pcpu_work); + + return IRQ_HANDLED; +} + +static void notif_pcpu_irq_work_fn(struct work_struct *work) +{ + struct optee_smc *optee_smc = container_of(work, struct optee_smc, + notif_pcpu_work); + struct optee *optee = container_of(optee_smc, struct optee, smc); + + optee_smc_do_bottom_half(optee->ctx); +} + +static int init_pcpu_irq(struct optee *optee, u_int irq) +{ + struct optee_pcpu __percpu *optee_pcpu; + int cpu, rc; + + optee_pcpu = alloc_percpu(struct optee_pcpu); + if (!optee_pcpu) + return -ENOMEM; + + for_each_present_cpu(cpu) { + struct optee_pcpu __percpu *p = per_cpu_ptr(optee_pcpu, cpu); + + p->optee = optee; + } + + rc = request_percpu_irq(irq, notif_pcpu_irq_handler, + "optee_pcpu_notification", optee_pcpu); + if (rc) + goto err_free_pcpu; + + INIT_WORK(&optee->smc.notif_pcpu_work, notif_pcpu_irq_work_fn); + optee->smc.notif_pcpu_wq = create_workqueue("optee_pcpu_notification"); + if (!optee->smc.notif_pcpu_wq) { + rc = -EINVAL; + goto err_free_pcpu_irq; + } + + optee->smc.optee_pcpu = optee_pcpu; + optee->smc.notif_irq = irq; + + pcpu_irq_num = irq; + rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "optee/pcpu-notif:starting", + optee_cpuhp_enable_pcpu_irq, + optee_cpuhp_disable_pcpu_irq); + if (!rc) + rc = -EINVAL; + if (rc < 0) + goto err_free_pcpu_irq; + + optee->smc.notif_cpuhp_state = rc; + + return 0; + +err_free_pcpu_irq: + free_percpu_irq(irq, optee_pcpu); +err_free_pcpu: + free_percpu(optee_pcpu); + + return rc; +} + +static int optee_smc_notif_init_irq(struct optee *optee, u_int irq) +{ + if (irq_is_percpu_devid(irq)) + return init_pcpu_irq(optee, irq); + else + return init_irq(optee, irq); +} + +static void uninit_pcpu_irq(struct optee *optee) +{ + cpuhp_remove_state(optee->smc.notif_cpuhp_state); + + destroy_workqueue(optee->smc.notif_pcpu_wq); + + free_percpu_irq(optee->smc.notif_irq, optee->smc.optee_pcpu); + free_percpu(optee->smc.optee_pcpu); +} + static void optee_smc_notif_uninit_irq(struct optee *optee) { if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_ASYNC_NOTIF) { optee_smc_stop_async_notif(optee->ctx); if (optee->smc.notif_irq) { - free_irq(optee->smc.notif_irq, optee); + if (irq_is_percpu_devid(optee->smc.notif_irq)) + uninit_pcpu_irq(optee); + else + free_irq(optee->smc.notif_irq, optee); + irq_dispose_mapping(optee->smc.notif_irq); } }