From patchwork Wed Jan 18 17:49:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Etienne Carriere X-Patchwork-Id: 13106758 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 95A10C32793 for ; Wed, 18 Jan 2023 17:51:14 +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: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:In-Reply-To:References: List-Owner; bh=6FXGpk6lFoko/WzXcYmHNZthiS0J/PT973/leiGsjrg=; b=rzfF6bHPOshSJI MfsLCmbqCG4P8WaupiSZpiILcyhN/JlhMuYxyxLVvaIi3F6atIzE907m1Nl1xUQ8f4Rp7M8zXh312 pEBPPHroPHHUoipf8Yx1KmCARju4vfmICjawR+POUH+Z69f3xjbeucAbiHpl7KQYhCNGrw7j/IdQq syxE/cBhMpp2Oy9yWllo2W298vwMFvnPoT7y72ZPxI4HJAkCbLtI2mmkYOIPc2v3GLOsDmbZuqBi6 jTFV+VdSfb1EoLu/CQTkNe9gbKJl/0CLcCiKJOqtQPayvQfx5MxwmcRcLH4q/+K59fjdbK3qdm4v8 Jw7fi3ZRtbKA4NgB09ZQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pICZi-0027EO-GQ; Wed, 18 Jan 2023 17:50:14 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pICZf-0027Dd-3z for linux-arm-kernel@lists.infradead.org; Wed, 18 Jan 2023 17:50:12 +0000 Received: by mail-wm1-x329.google.com with SMTP id j17so6770764wms.0 for ; Wed, 18 Jan 2023 09:50:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=yjYSw2n+Ps1iOJAbRHWuOEZaS8GB8SIlpqdkEMs8TMY=; b=CJapJn14m/+bbp95Kyo9SGUMzxZCp/hwd1y8DIUfbcRdvhSGO0TmVif35R3TpzCtfh 2vk8h0Zl1oCeggG5n/iHEJaVwbtkxOPHOx5C8ctpSpBxXlvcGBfH/M/zfyqlO8sId7sA 2xlOPnDbl0172Rr4Q+xO1U/ZET4yumMTQyGt1oRNLQ0RZ/YmmS83W2YAq0YyAFp9H+Om 0SBlOwcM4gQqQIuvA0dVQSh8xfJJz+0gjE+R7qBpYcS6XXB80QMQEfw9jwMeDbOiJf2K U6Y69vfIv1P2LMoga9MZV2cvQ/VN3ChqBnqHWWhxqxhFF6zbNFk5ZQcn9aQPx7ux+nB8 4JPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=yjYSw2n+Ps1iOJAbRHWuOEZaS8GB8SIlpqdkEMs8TMY=; b=P2rm6Z1HvD9OuZzlVhKnXBb+RMTtwu/LVSTSJZ8TjqUYQP2fhYvX1bvM4yd4IeUaQy tPtrzRLJTvPJ+ZdBkm0AnxFdu/jAW/w5BkIiD2hLEeFV4v4fuDbaItyl9tf2ymV0Othg +C4oC3rfbAk0dr5RuDg1eiEsqJawX/dXsYVp+XZ8/7JlKa1gdFJlkudjyBUBuQPWa1gg BllEK3v/Lnx/CJ4E1egSGN0zwakY67mS7WFnWZZhgu2ytevYRu/IyrhlHvllBVGQCNvK va5UyvddxUkTwlf461cVWID8++bMGCMiKHEuIXasS95oO+WTpYDJ6D4O35Kkq6jKahKR GWrA== X-Gm-Message-State: AFqh2korDEXDV/SujgHhhGr5Q/2XzdBqUKi/jESNFcVJpzK4ij6IJE7w DEG3tFutxqGiSuXXbfvEe15yrg== X-Google-Smtp-Source: AMrXdXui/pL9AQaRfGB+nJ8v6XbmkLiMwkqomyCZ5il8ZXln2vQBhoUydeHqnkxo0/+Y04o+k5oWdw== X-Received: by 2002:a05:600c:4256:b0:3da:2a78:d7a3 with SMTP id r22-20020a05600c425600b003da2a78d7a3mr7279762wmm.33.1674064206347; Wed, 18 Jan 2023 09:50:06 -0800 (PST) Received: from lmecxl1178.lme.st.com (lfbn-nan-1-1169-52.w86-212.abo.wanadoo.fr. [86.212.112.52]) by smtp.gmail.com with ESMTPSA id t2-20020a1c7702000000b003d995a704fdsm2503290wmi.33.2023.01.18.09.50.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Jan 2023 09:50:05 -0800 (PST) From: Etienne Carriere To: linux-kernel@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org, Etienne Carriere , Jens Wiklander , Sumit Garg , Marc Zyngier , Alexandre Torgue Subject: [PATCH v2] optee: add per cpu asynchronous notification Date: Wed, 18 Jan 2023 18:49:09 +0100 Message-Id: <20230118174909.2049638-1-etienne.carriere@linaro.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230118_095011_191831_0BC6AD2B X-CRM114-Status: GOOD ( 21.30 ) 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-TE world to raise an interrupt for the Linux optee driver to query pending events bound to waiting tasks in Linux world or threaded bottom half tasks to be invoked in TEE world. This change allows the signaling interrupt to be a per cpu interrupt as with Arm GIC PPIs. 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 v1: - Fixed missing __percpu attribute reported by kernel test robot. - Rephrased commit message and added Cc tags. --- drivers/tee/optee/optee_private.h | 22 ++++++ drivers/tee/optee/smc_abi.c | 107 ++++++++++++++++++++++++++++-- 2 files changed, 124 insertions(+), 5 deletions(-) diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h index 04ae58892608..e5bd3548691f 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -94,11 +94,33 @@ 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 + */ 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; }; /** diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index a1c1fa1a9c28..ffa3f3aa7244 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -993,12 +993,20 @@ static u32 get_async_notif_value(optee_invoke_fn *invoke_fn, bool *value_valid, static irqreturn_t notif_irq_handler(int irq, void *dev_id) { - struct optee *optee = dev_id; + struct optee *optee; bool do_bottom_half = false; bool value_valid; bool value_pending; u32 value; + if (irq_is_percpu_devid(irq)) { + struct optee_pcpu __percpu *pcpu = (struct optee_pcpu *)dev_id; + + optee = pcpu->optee; + } else { + optee = dev_id; + } + do { value = get_async_notif_value(optee->smc.invoke_fn, &value_valid, &value_pending); @@ -1011,8 +1019,13 @@ static irqreturn_t notif_irq_handler(int irq, void *dev_id) optee_notif_send(optee, value); } while (value_pending); - if (do_bottom_half) - return IRQ_WAKE_THREAD; + if (do_bottom_half) { + if (irq_is_percpu_devid(irq)) + queue_work(optee->smc.notif_pcpu_wq, &optee->smc.notif_pcpu_work); + else + return IRQ_WAKE_THREAD; + } + return IRQ_HANDLED; } @@ -1025,7 +1038,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 +1053,96 @@ static int optee_smc_notif_init_irq(struct optee *optee, u_int irq) return 0; } +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; + spinlock_t lock; + int cpu; + int 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_irq_handler, + "optee_pcpu_notification", optee_pcpu); + if (rc) + goto err_free_pcpu; + + spin_lock_init(&lock); + + spin_lock(&lock); + enable_percpu_irq(irq, 0); + spin_unlock(&lock); + + 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; + + return 0; + +err_free_pcpu_irq: + spin_lock(&lock); + disable_percpu_irq(irq); + spin_unlock(&lock); + 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) +{ + spinlock_t lock; + + spin_lock_init(&lock); + spin_lock(&lock); + disable_percpu_irq(optee->smc.notif_irq); + spin_unlock(&lock); + + 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); } }