From nobody Mon Sep 15 10:16:19 2025 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 6D90EC54EBC for ; Thu, 12 Jan 2023 15:07:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240356AbjALPHy (ORCPT ); Thu, 12 Jan 2023 10:07:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53950 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233462AbjALPGs (ORCPT ); Thu, 12 Jan 2023 10:06:48 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D3433CDF for ; Thu, 12 Jan 2023 06:55:39 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id e3so9127425wru.13 for ; Thu, 12 Jan 2023 06:55:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=3ekLw6Rzs5+1GcmkyJf60On54JMIc+jRWw605x9/goM=; b=pSTrmnVlFuHJsDRTMEACdTd1b6yGTpyIg4uXt5qCicRiJhN8UDIOef/opp6Xarn3FM sELe7g82S6vNkEBrr27XnaDIwrQjvtEU99I2KkE209nRwkzTdaqkZz9IAjl+jmu+/t5P LP7ILm5nzs+wOlPLv3UkxzyYGVGYYsDX2qNyKDWib/8GbmHXZRgsykX6db4US4ZgoPEK 0DGp31zkIW9uXRND4OO9tc9ckrZBR0V46tLYlzyCFkyJm9O8juFNIUqAHpjGA4JbbQRg RAWEfojO1iAv9mIhZaEI/D0DwUdvf3eA01PTC/piA00VZqDZwdH/Utb0CS3HFVJHszEX Blqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=3ekLw6Rzs5+1GcmkyJf60On54JMIc+jRWw605x9/goM=; b=fm5k3Jy28Iju+IAA9ityYZC0D2qaCmHqtJ/+JyMBDA/AbXbEOIs2Ebn1GgWpCRKty6 rJsNSAWVm93U05LRDWZWOfyxO8/5q2OqGzIqBiElNExPPZPowRdGS6OGVNILxPjUnBSA gD0XYKjSjzEH90aALQvHBTcKvNO2kzU4FJOAIzKjsOLEZ78YspyHO6KANSTZ+Y3mRJNN UifSGGu2VanqGGXwGT6gjpvMwj/DvzqIA4W+IXTXnzuzqhFQzZQCctUY9kF49pm/4Shx 2ifj+Tlw9yjSzIcAryQxz7HKdGpCVwnaltZDcT9/f3yHrApq0qFRXcF7QOVbeLJQfnrt SfRg== X-Gm-Message-State: AFqh2koana7ev/gCXX6HjH6pUxOuy8KyKqEArz61ToecYW3O0RCY/6k8 8drviUcHlKHWpvInDakdVN/yD9QgljxtXcuI X-Google-Smtp-Source: AMrXdXtLlgEIX+cP0YUvte35B20fwodAcqppiqwHJWb55/CZ5iM+y+yF0b8f8xwyvlB9fRNQcq5Mwg== X-Received: by 2002:a5d:59c7:0:b0:293:1868:3a15 with SMTP id v7-20020a5d59c7000000b0029318683a15mr36795107wry.34.1673535338046; Thu, 12 Jan 2023 06:55:38 -0800 (PST) Received: from lmecxl1178.lme.st.com ([80.215.38.209]) by smtp.gmail.com with ESMTPSA id v14-20020adff68e000000b002365730eae8sm16599779wrp.55.2023.01.12.06.55.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 06:55:37 -0800 (PST) From: Etienne Carriere To: linux-kernel@vger.kernel.org Cc: Jens Wiklander , Sumit Garg , Marc Zyngier , op-tee@lists.trustedfirmware.org, devicetree@vger.kernel.org, Krzysztof Kozlowski , Rob Herring , Etienne Carriere , Alexandre Torgue Subject: [PATCH 1/3] optee: add per cpu asynchronous notification Date: Thu, 12 Jan 2023 15:54:22 +0100 Message-Id: <20230112145424.3791276-2-etienne.carriere@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230112145424.3791276-1-etienne.carriere@linaro.org> References: <20230112145424.3791276-1-etienne.carriere@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Implements use of per CPU irq for asynchronous notification next to existing standard irq support. This change allows for example to use GIC_PPI on platforms where no GIC_SPI is provisioned for OP-TEE asynchronous notification. Co-developed-by: Alexandre Torgue Signed-off-by: Alexandre Torgue Signed-off-by: Etienne Carriere --- 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_pr= ivate.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; }; =20 +/* + * struct optee_pcpu - per cpu notif private struct passed to work functio= ns + * @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; }; =20 /** diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index a1c1fa1a9c28..8c2d58d605ac 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 *inv= oke_fn, bool *value_valid, =20 static irqreturn_t notif_irq_handler(int irq, void *dev_id) { - struct optee *optee =3D dev_id; + struct optee *optee; bool do_bottom_half =3D false; bool value_valid; bool value_pending; u32 value; =20 + if (irq_is_percpu_devid(irq)) { + struct optee_pcpu *pcpu =3D (struct optee_pcpu *)dev_id; + + optee =3D pcpu->optee; + } else { + optee =3D dev_id; + } + do { value =3D 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); =20 - 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; } =20 @@ -1025,7 +1038,7 @@ static irqreturn_t notif_irq_thread_fn(int irq, void = *dev_id) return IRQ_HANDLED; } =20 -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; =20 @@ -1040,12 +1053,96 @@ static int optee_smc_notif_init_irq(struct optee *o= ptee, u_int irq) return 0; } =20 +static void notif_pcpu_irq_work_fn(struct work_struct *work) +{ + struct optee_smc *optee_smc =3D container_of(work, struct optee_smc, noti= f_pcpu_work); + struct optee *optee =3D 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 *optee_pcpu; + spinlock_t lock; + int cpu; + int rc; + + optee_pcpu =3D alloc_percpu(struct optee_pcpu); + if (!optee_pcpu) + return -ENOMEM; + + for_each_present_cpu(cpu) { + struct optee_pcpu *p =3D per_cpu_ptr(optee_pcpu, cpu); + + p->optee =3D optee; + } + + rc =3D 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 =3D create_workqueue("optee_pcpu_notification"); + if (!optee->smc.notif_pcpu_wq) { + rc =3D -EINVAL; + goto err_free_pcpu_irq; + } + + optee->smc.optee_pcpu =3D optee_pcpu; + optee->smc.notif_irq =3D 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); } } --=20 2.25.1 From nobody Mon Sep 15 10:16:19 2025 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 143D1C54EBD for ; Thu, 12 Jan 2023 15:08:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240235AbjALPH5 (ORCPT ); Thu, 12 Jan 2023 10:07:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55122 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240322AbjALPGs (ORCPT ); Thu, 12 Jan 2023 10:06:48 -0500 Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E962437245 for ; Thu, 12 Jan 2023 06:55:39 -0800 (PST) Received: by mail-wm1-x332.google.com with SMTP id ay12-20020a05600c1e0c00b003d9ea12bafcso11359333wmb.3 for ; Thu, 12 Jan 2023 06:55:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=0PZmQdqSd+0aQvVRPY0Zhs8Yzr1xgEW04gBOSVGXblk=; b=r25oCopoNkIeX3PSmYBCj527lFmJgtv1ESE374T1KJYynR/yYWMxc/xqYMk0/TgOPQ MwSbnT/UxJ7p+vPGbfcKxdvOU1GHb50vaI6pcj32sIy14+Yfn9lnU0K7IGpha922c70Y w8TetzkIVuk9PqlrlhI+urUPzQX1ojnommYsA1Rl9pkiA+xK6RE3EnKDWFCK8cMmzhTL /sCVVpdYAPc0E+QC+viIzVM5yWA2faBrxN7s4dZbuuw+ORmNZEUriPbJZCD1iq2WDm1x oFaK5Tjl8ik3ROeCNzS9p8+st3DgNAhpmw+STM+RkiOVR9oWuB7lZydqyN/HPdwdiBf5 qUAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=0PZmQdqSd+0aQvVRPY0Zhs8Yzr1xgEW04gBOSVGXblk=; b=fkXUYYVXuD9I5UL6x/itaHd4fmIho3gtSqScencsGFO/LrDxLDTzGYrIIpfvORWc6y 8LJ4quZX1Dw7WNTTl2ZbJecyeW9d9Am+r9PYm1peRquBkY+XNuUqsqQRXPR8iw6xwMLq /1Dr0atcT+EiEX5MRbCRRC2u8jRsRpggg5cABOI2YaukXLfhq+ZslSlyiMVYqE/qF/UH 1a7ouqNyGpmHWz0MMy6/csotjcwCqshJrovNKf90azxUIFONPKD5aUCqOZCckY3F+Xy7 lzD24fQOOtxnd9bJST21iIHKDqciu2lQcHzKsN3Jkv5LmwoOfkQzHdMJpJjWMK6G8GeL sp2g== X-Gm-Message-State: AFqh2kr5EAEFQgq0+DtkoQzRidPLS9/QVInOhGmaJjYd14mn7Axtpz8l gmKzAHsJa+ITDBe0tbBXZAyYSoJlVnexQn3p X-Google-Smtp-Source: AMrXdXtRXyUaci7tCX5s6PeBV8jCoPfmJRexOD0Kvd+sp0HhrttjG8RXTeTDw7j/dRGoxSLQvvXNvA== X-Received: by 2002:a05:600c:22d4:b0:3cf:82b9:2fe6 with SMTP id 20-20020a05600c22d400b003cf82b92fe6mr65827720wmg.8.1673535339224; Thu, 12 Jan 2023 06:55:39 -0800 (PST) Received: from lmecxl1178.lme.st.com ([80.215.38.209]) by smtp.gmail.com with ESMTPSA id v14-20020adff68e000000b002365730eae8sm16599779wrp.55.2023.01.12.06.55.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 06:55:38 -0800 (PST) From: Etienne Carriere To: linux-kernel@vger.kernel.org Cc: Jens Wiklander , Sumit Garg , Marc Zyngier , op-tee@lists.trustedfirmware.org, devicetree@vger.kernel.org, Krzysztof Kozlowski , Rob Herring , Etienne Carriere Subject: [PATCH 2/3] dt-bindings: arm: optee: add interrupt controller properties Date: Thu, 12 Jan 2023 15:54:23 +0100 Message-Id: <20230112145424.3791276-3-etienne.carriere@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230112145424.3791276-1-etienne.carriere@linaro.org> References: <20230112145424.3791276-1-etienne.carriere@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adds optional interrupt controller properties used when OP-TEE generates interrupt events optee driver shall notified to its registered interrupt consumer. The example shows how OP-TEE can trigger a wakeup interrupt event consumed by a gpio-keys compatible device. Signed-off-by: Etienne Carriere --- .../arm/firmware/linaro,optee-tz.yaml | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz= .yaml b/Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz.yaml index d4dc0749f9fd..42874ca21b7e 100644 --- a/Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz.yaml +++ b/Documentation/devicetree/bindings/arm/firmware/linaro,optee-tz.yaml @@ -40,6 +40,11 @@ properties: HVC #0, register assignments register assignments are specified in drivers/tee/optee/optee_smc.h =20 + interrupt-controller: true + + "#interrupt-cells": + const: 1 + required: - compatible - method @@ -48,12 +53,24 @@ additionalProperties: false =20 examples: - | + #include #include firmware { - optee { + optee: optee { compatible =3D "linaro,optee-tz"; method =3D "smc"; interrupts =3D ; + interrupt-controller; + #interrupt-cells =3D <1>; + }; + }; + + wake_up { + compatible =3D "gpio-keys"; + + button { + linux,code =3D ; + interrupts-extended =3D <&optee 0>; }; }; =20 --=20 2.25.1 From nobody Mon Sep 15 10:16:19 2025 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 2BC03C54EBD for ; Thu, 12 Jan 2023 15:08:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240343AbjALPID (ORCPT ); Thu, 12 Jan 2023 10:08:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54324 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240110AbjALPGt (ORCPT ); Thu, 12 Jan 2023 10:06:49 -0500 Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62A4653721 for ; Thu, 12 Jan 2023 06:55:41 -0800 (PST) Received: by mail-wr1-x433.google.com with SMTP id e3so9127574wru.13 for ; Thu, 12 Jan 2023 06:55:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; 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=KO1qPhV1IetJ3e/aoUepVBj7y30h3+DK1HnH3CspGDY=; b=suru85umNT2w65xqaTjkxlovTI6k6Vpg3VVNHrzRUmHK9A7T3IF/2wVwQbRrysGwf4 vDTQVElJqcZPegfvg3tAVFaAkQWAoh4yzVnmZAe0UIwy40Af23JmRg0JvyJtUj6JrBE9 Aj5ObFOLbTB5+EieahKN8y4dT+K8Cwl7uWKz8LazDGoIm/VBTBtJJ9pfnKoUWefZKZ+2 Raj6AzIqDt7a4LEzz14Rl5MyakM6F8wzsz2UBLS9xfEJ8by1dx7VPbjqNzRpiokyWxst x9wIIqqP9Iv9sZa9D5HD2PFzrySLrhZzDili9E7+ZQIUSQMsfDUEu50mKCb2Tqahgyhh sJoQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=KO1qPhV1IetJ3e/aoUepVBj7y30h3+DK1HnH3CspGDY=; b=c4aGKXN72bK/TgRepfXubh5wA9KcHt/YQFO3jflwkJy/jAJwRZIyv+mdqNwPJ7se+b vQZaXuECV4zukvY0n3rf/j2zymputbmPzuGUayPVmZEhJTr4DB49CxRXku4vmWE+mWDw cseTm5xdNUGfpyPEHLcrF+Vj0xKUFR8DrNKXX5Z03iYfxMRdNeK4YAfFifUgR8WHZyLT i1MMX4t2/AwgVcpSIjQi05Ysq/WbLQZ+kCaG8Zx7Z0ZBPkpJGxmiyxqC8oZwPIP4KBza oaBB5IUorsbDUiqEfXPxQnRUgDcgQycrV9MwqU1cv0w4tCVpSJNlGYhEy33hUtGxLDno /c/Q== X-Gm-Message-State: AFqh2kpSXma3b0G1ghox9tUgR3PZWNELBUbpgxIC/XmNnDkEJYOeNeNs fZJtXz8GmaHWDlh78c4o+iZ3HcnoTImQ6MUC X-Google-Smtp-Source: AMrXdXtHVQLXQMBwYj5oNZifThd+AtMQysIXelqiSFcIRkCuHxnVG60YJiQBhtnUCNVcs6CB9lC0Qw== X-Received: by 2002:adf:ea12:0:b0:2bd:c097:848b with SMTP id q18-20020adfea12000000b002bdc097848bmr5686018wrm.16.1673535340536; Thu, 12 Jan 2023 06:55:40 -0800 (PST) Received: from lmecxl1178.lme.st.com ([80.215.38.209]) by smtp.gmail.com with ESMTPSA id v14-20020adff68e000000b002365730eae8sm16599779wrp.55.2023.01.12.06.55.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 06:55:40 -0800 (PST) From: Etienne Carriere To: linux-kernel@vger.kernel.org Cc: Jens Wiklander , Sumit Garg , Marc Zyngier , op-tee@lists.trustedfirmware.org, devicetree@vger.kernel.org, Krzysztof Kozlowski , Rob Herring , Etienne Carriere , Pascal Paillet , Fabrice Gasnier Subject: [PATCH 3/3] optee core: add irq chip using optee async notification Date: Thu, 12 Jan 2023 15:54:24 +0100 Message-Id: <20230112145424.3791276-4-etienne.carriere@linaro.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230112145424.3791276-1-etienne.carriere@linaro.org> References: <20230112145424.3791276-1-etienne.carriere@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Adds an irq chip in optee driver to generate interrupts from OP-TEE notified interrupt events based on optee async notification. Upon such notification, optee driver invokes OP-TEE to query a pending interrupt event. If an interrupt notification is pending the invocation return OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_IT and optee driver can get the pending interrupt number with SMC function ID OPTEE_SMC_FUNCID_GET_IT_VALUE. SMC function ID OPTEE_SMC_FUNCID_SET_IT_MASK allows Linux to mask/unmask an interrupt notification services. The optee irq_chip if flagged IRQCHIP_SKIP_SET_WAKE to skip set_wake as optee interrupt notifications doesn't support the set_wake option. In case a device is using the optee irq and is marked as wakeup source, this result in an "Unbalanced IRQ xx wake disable" backtrace, since: - in irq_set_irq_wake(ON), wake_depth gets incremented, then reset due to set_irq_wake_real() returns an error (irq_set_wake() isn't implemented) - in irq_set_irq_wake(OFF), wake_depth is always 0, hence the warning Co-developed-by: Pascal Paillet Signed-off-by: Pascal Paillet Co-developed-by: Fabrice Gasnier Signed-off-by: Fabrice Gasnier Signed-off-by: Etienne Carriere --- drivers/tee/optee/optee_private.h | 2 + drivers/tee/optee/optee_smc.h | 78 +++++++++++++++- drivers/tee/optee/smc_abi.c | 142 ++++++++++++++++++++++++++++-- 3 files changed, 216 insertions(+), 6 deletions(-) diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_pr= ivate.h index e5bd3548691f..2a146d884d27 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -112,6 +112,7 @@ struct optee_pcpu { * @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 + * @domain interrupt domain registered by OP-TEE driver */ struct optee_smc { optee_invoke_fn *invoke_fn; @@ -121,6 +122,7 @@ struct optee_smc { struct optee_pcpu __percpu *optee_pcpu; struct workqueue_struct *notif_pcpu_wq; struct work_struct notif_pcpu_work; + struct irq_domain *domain; }; =20 /** diff --git a/drivers/tee/optee/optee_smc.h b/drivers/tee/optee/optee_smc.h index 73b5e7760d10..0cf83d5a2931 100644 --- a/drivers/tee/optee/optee_smc.h +++ b/drivers/tee/optee/optee_smc.h @@ -226,7 +226,8 @@ struct optee_smc_get_shm_config_result { * a3 Bit[7:0]: Number of parameters needed for RPC to be supplied * as the second MSG arg struct for * OPTEE_SMC_CALL_WITH_ARG - * Bit[31:8]: Reserved (MBZ) + * Bit[23:8]: The maximum interrupt event notification number + * Bit[31:24]: Reserved (MBZ) * a4-7 Preserved * * Error return register usage: @@ -254,6 +255,11 @@ struct optee_smc_get_shm_config_result { #define OPTEE_SMC_SEC_CAP_ASYNC_NOTIF BIT(5) /* Secure world supports pre-allocating RPC arg struct */ #define OPTEE_SMC_SEC_CAP_RPC_ARG BIT(6) +/* Secure world supports interrupt events notification to normal world */ +#define OPTEE_SMC_SEC_CAP_IT_NOTIF BIT(7) + +#define OPTEE_SMC_SEC_CAP_MAX_NOTIF_IT_MASK GENMASK(23, 8) +#define OPTEE_SMC_SEC_CAP_MAX_NOTIF_IT_SHIFT 8 =20 #define OPTEE_SMC_FUNCID_EXCHANGE_CAPABILITIES 9 #define OPTEE_SMC_EXCHANGE_CAPABILITIES \ @@ -416,6 +422,12 @@ struct optee_smc_disable_shm_cache_result { */ #define OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF 0 =20 +/* + * Notification that OP-TEE triggers an interrupt event to Linux kernel + * for an interrupt consumer. + */ +#define OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_IT 1 + #define OPTEE_SMC_FUNCID_GET_ASYNC_NOTIF_VALUE 17 #define OPTEE_SMC_GET_ASYNC_NOTIF_VALUE \ OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_ASYNC_NOTIF_VALUE) @@ -426,6 +438,70 @@ struct optee_smc_disable_shm_cache_result { /* See OPTEE_SMC_CALL_WITH_REGD_ARG above */ #define OPTEE_SMC_FUNCID_CALL_WITH_REGD_ARG 19 =20 +/* + * Retrieve the interrupt number of the pending interrupt event notified to + * non-secure world since the last call of this function. + * + * OP-TEE keeps a record of all posted interrupt notification events. When= the + * async notif interrupt is received by non-secure world, this function sh= ould + * be called until all pended interrupt events have been retrieved. When an + * interrupt event is retrieved it is cleared from the record in secure wo= rld. + * + * It is expected that this function is called from an interrupt handler + * in normal world. + * + * Call requests usage: + * a0 SMC Function ID, OPTEE_SMC_GET_IT_NOTIF_VALUE + * a1-6 Not used + * a7 Hypervisor Client ID register + * + * Normal return register usage: + * a0 OPTEE_SMC_RETURN_OK + * a1 IT_NOTIF interrupt identifier value + * a2 Bit[0]: OPTEE_SMC_IT_NOTIF_VALID if the value in a1 is + * valid, else 0 if no interrupt event were pending + * a2 Bit[1]: OPTEE_SMC_IT_NOTIF_PENDING if another interrupt event + * value is pending, else 0. + * Bit[31:2]: MBZ + * a3-7 Preserved + * + * Not supported return register usage: + * a0 OPTEE_SMC_RETURN_ENOTAVAIL + * a1-7 Preserved + */ +#define OPTEE_SMC_IT_NOTIF_VALID BIT(0) +#define OPTEE_SMC_IT_NOTIF_PENDING BIT(1) + +#define OPTEE_SMC_FUNCID_GET_IT_NOTIF_VALUE 20 +#define OPTEE_SMC_GET_IT_NOTIF_VALUE \ + OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_GET_IT_NOTIF_VALUE) + +/* + * Mask or unmask an interrupt notification event. + * + * It is expected that this function is called from an interrupt handler + * in normal world. + * + * Call requests usage: + * a0 SMC Function ID, OPTEE_SMC_SET_IT_NOTIF_MASK + * a1 Interrupt identifier value + * a2 Bit[0]: 1 if interrupt event is to be masked, 0 if it is to be unmas= ked + * a2 Bit[31:1] MBZ + * a3-6 Not used + * a7 Hypervisor Client ID register + * + * Normal return register usage: + * a0 OPTEE_SMC_RETURN_OK + * a1-7 Preserved + * + * Not supported return register usage: + * a0 OPTEE_SMC_RETURN_ENOTAVAIL + * a1-7 Preserved + */ +#define OPTEE_SMC_FUNCID_SET_IT_NOTIF_MASK 21 +#define OPTEE_SMC_SET_IT_NOTIF_MASK \ + OPTEE_SMC_FAST_CALL_VAL(OPTEE_SMC_FUNCID_SET_IT_NOTIF_MASK) + /* * Resume from RPC (for example after processing a foreign interrupt) * diff --git a/drivers/tee/optee/smc_abi.c b/drivers/tee/optee/smc_abi.c index 8c2d58d605ac..0360afde119f 100644 --- a/drivers/tee/optee/smc_abi.c +++ b/drivers/tee/optee/smc_abi.c @@ -977,6 +977,112 @@ static int optee_smc_stop_async_notif(struct tee_cont= ext *ctx) * 5. Asynchronous notification */ =20 +static u32 get_it_value(optee_invoke_fn *invoke_fn, bool *value_valid, + bool *value_pending) +{ + struct arm_smccc_res res; + + invoke_fn(OPTEE_SMC_GET_IT_NOTIF_VALUE, 0, 0, 0, 0, 0, 0, 0, &res); + + if (res.a0) + return 0; + + *value_valid =3D res.a2 & OPTEE_SMC_IT_NOTIF_VALID; + *value_pending =3D res.a2 & OPTEE_SMC_IT_NOTIF_PENDING; + return res.a1; +} + +static u32 set_it_mask(optee_invoke_fn *invoke_fn, u32 it_value, bool mask) +{ + struct arm_smccc_res res; + + invoke_fn(OPTEE_SMC_SET_IT_NOTIF_MASK, it_value, mask, 0, 0, 0, 0, 0, &re= s); + + if (res.a0) + return 0; + + return res.a1; +} + +static int handle_optee_it(struct optee *optee) +{ + bool value_valid; + bool value_pending; + u32 it; + + do { + struct irq_desc *desc; + + it =3D get_it_value(optee->smc.invoke_fn, &value_valid, &value_pending); + if (!value_valid) + break; + + desc =3D irq_to_desc(irq_find_mapping(optee->smc.domain, it)); + if (!desc) { + pr_err("no desc for optee IT:%d\n", it); + return -EIO; + } + + handle_simple_irq(desc); + + } while (value_pending); + + return 0; +} + +static void optee_it_irq_mask(struct irq_data *d) +{ + struct optee *optee =3D d->domain->host_data; + + set_it_mask(optee->smc.invoke_fn, d->hwirq, true); +} + +static void optee_it_irq_unmask(struct irq_data *d) +{ + struct optee *optee =3D d->domain->host_data; + + set_it_mask(optee->smc.invoke_fn, d->hwirq, false); +} + +static struct irq_chip optee_it_irq_chip =3D { + .name =3D "optee-it", + .irq_disable =3D optee_it_irq_mask, + .irq_enable =3D optee_it_irq_unmask, + .flags =3D IRQCHIP_SKIP_SET_WAKE, +}; + +static int optee_it_alloc(struct irq_domain *d, unsigned int virq, + unsigned int nr_irqs, void *data) +{ + struct irq_fwspec *fwspec =3D data; + irq_hw_number_t hwirq; + + hwirq =3D fwspec->param[0]; + + irq_domain_set_hwirq_and_chip(d, virq, hwirq, &optee_it_irq_chip, d->host= _data); + + return 0; +} + +static const struct irq_domain_ops optee_it_irq_domain_ops =3D { + .alloc =3D optee_it_alloc, + .free =3D irq_domain_free_irqs_common, +}; + +static int optee_irq_domain_init(struct platform_device *pdev, struct opte= e *optee, u_int max_it) +{ + struct device *dev =3D &pdev->dev; + struct device_node *np =3D dev->of_node; + + optee->smc.domain =3D irq_domain_add_linear(np, max_it, &optee_it_irq_dom= ain_ops, optee); + if (!optee->smc.domain) { + dev_err(dev, "Unable to add irq domain\n"); + return -ENOMEM; + } + + return 0; +} + static u32 get_async_notif_value(optee_invoke_fn *invoke_fn, bool *value_v= alid, bool *value_pending) { @@ -1008,13 +1114,15 @@ static irqreturn_t notif_irq_handler(int irq, void = *dev_id) } =20 do { - value =3D get_async_notif_value(optee->smc.invoke_fn, - &value_valid, &value_pending); + value =3D get_async_notif_value(optee->smc.invoke_fn, &value_valid, &val= ue_pending); if (!value_valid) break; =20 if (value =3D=3D OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_BOTTOM_HALF) do_bottom_half =3D true; + else if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_IT_NOTIF && + value =3D=3D OPTEE_SMC_ASYNC_NOTIF_VALUE_DO_IT) + handle_optee_it(optee); else optee_notif_send(optee, value); } while (value_pending); @@ -1042,8 +1150,7 @@ static int init_irq(struct optee *optee, u_int irq) { int rc; =20 - rc =3D request_threaded_irq(irq, notif_irq_handler, - notif_irq_thread_fn, + rc =3D request_threaded_irq(irq, notif_irq_handler, notif_irq_thread_fn, 0, "optee_notification", optee); if (rc) return rc; @@ -1145,6 +1252,9 @@ static void optee_smc_notif_uninit_irq(struct optee *= optee) =20 irq_dispose_mapping(optee->smc.notif_irq); } + + if (optee->smc.sec_caps & OPTEE_SMC_SEC_CAP_IT_NOTIF) + irq_domain_remove(optee->smc.domain); } } =20 @@ -1284,6 +1394,7 @@ static bool optee_msg_api_revision_is_compatible(opte= e_invoke_fn *invoke_fn) =20 static bool optee_msg_exchange_capabilities(optee_invoke_fn *invoke_fn, u32 *sec_caps, u32 *max_notif_value, + u32 *max_notif_it, unsigned int *rpc_param_count) { union { @@ -1316,6 +1427,12 @@ static bool optee_msg_exchange_capabilities(optee_in= voke_fn *invoke_fn, else *rpc_param_count =3D 0; =20 + if (*sec_caps & OPTEE_SMC_SEC_CAP_IT_NOTIF) + *max_notif_it =3D (res.result.data & OPTEE_SMC_SEC_CAP_MAX_NOTIF_IT_MASK= ) >> + OPTEE_SMC_SEC_CAP_MAX_NOTIF_IT_SHIFT; + else + *max_notif_it =3D 0; + return true; } =20 @@ -1461,6 +1578,7 @@ static int optee_probe(struct platform_device *pdev) struct tee_device *teedev; struct tee_context *ctx; u32 max_notif_value; + u32 max_notif_it; u32 arg_cache_flags; u32 sec_caps; int rc; @@ -1482,7 +1600,7 @@ static int optee_probe(struct platform_device *pdev) } =20 if (!optee_msg_exchange_capabilities(invoke_fn, &sec_caps, - &max_notif_value, + &max_notif_value, &max_notif_it, &rpc_param_count)) { pr_warn("capabilities mismatch\n"); return -EINVAL; @@ -1603,6 +1721,20 @@ static int optee_probe(struct platform_device *pdev) irq_dispose_mapping(irq); goto err_notif_uninit; } + + if (sec_caps & OPTEE_SMC_SEC_CAP_IT_NOTIF) { + rc =3D optee_irq_domain_init(pdev, optee, max_notif_it); + if (rc) { + if (irq_is_percpu_devid(optee->smc.notif_irq)) + uninit_pcpu_irq(optee); + else + free_irq(optee->smc.notif_irq, optee); + + irq_dispose_mapping(irq); + goto err_notif_uninit; + } + } + enable_async_notif(optee->smc.invoke_fn); pr_info("Asynchronous notifications enabled\n"); } --=20 2.25.1