From nobody Fri Dec 19 09:45:03 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 157EAE9270D for ; Thu, 5 Oct 2023 16:22:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243037AbjJEQWU (ORCPT ); Thu, 5 Oct 2023 12:22:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36426 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241449AbjJEQQJ (ORCPT ); Thu, 5 Oct 2023 12:16:09 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E14C61FEF9 for ; Thu, 5 Oct 2023 07:45:41 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 8B57F1596; Thu, 5 Oct 2023 07:45:59 -0700 (PDT) Received: from e103737-lin.cambridge.arm.com (e103737-lin.cambridge.arm.com [10.1.197.49]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id D53253F641; Thu, 5 Oct 2023 07:45:19 -0700 (PDT) From: Sudeep Holla Date: Thu, 05 Oct 2023 15:45:03 +0100 Subject: [PATCH v4 10/17] firmware: arm_ffa: Add interfaces to request notification callbacks MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20231005-ffa_v1-1_notif-v4-10-cddd3237809c@arm.com> References: <20231005-ffa_v1-1_notif-v4-0-cddd3237809c@arm.com> In-Reply-To: <20231005-ffa_v1-1_notif-v4-0-cddd3237809c@arm.com> To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Sudeep Holla , Marc Bonnici , Jens Wiklander , Coboy Chen , Lorenzo Pieralisi , Olivier Deprez X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=6943; i=sudeep.holla@arm.com; h=from:subject:message-id; bh=jS9ZlPVRWP8/Wp7yswbAzCodeaTe0yPLbi3t5Cfh9Ag=; b=owEBbQKS/ZANAwAIAQBBurwxfuKYAcsmYgBlHsvxxToFsUAseBZwM6KJxyCdYs3fXrYhfoBev HCeQCsEg1mJAjMEAAEIAB0WIQS6ceUSBvMeskPdk+EAQbq8MX7imAUCZR7L8QAKCRAAQbq8MX7i mNK6EACWTqIstipLTxGSmcoa6S6ckNRC7YGl/dzKaA1U1CN4NJlnY7c3ndXCz5pXlEa4dYM/qS0 jRxaMokUoAM+oTzQ7oHpHcsbwV+U94b7PZUf8LE43zwDehpCDOVZiUw+JG3P8TqWZHHvyJ1uKWi twxvwIdASgc+QLTKMBsFqccxnZaHifF4CefM40n+vllCJCxHNPJNdniQkgNdHQafj5059JAavWj //Rv28k+dZJFGRpOpjhleRQdTJzCKQljD15ayt5RgeLeiusqE3wFRak8Nlht2+DrgDzaLNQHX8o Rnf61LLiBqlGbwpa73pFUY1jbHWkhsX2KXCGUs+PvVw+Kkwun+mkzF0wmC/u4Pq1J9hxKnoR+/q rcu0UJ/bz961gkivXmEmB4rxsxgZRZHcdJvXxftpVOKft4eMWhZjmOcV7vx+//W7Q0yOHl3sMoI pex2Bh/FzNT9tHCikz7PHQEcLbNGpDmTy0O/w4HfcslvVVmORIrWb8oysRawD/Q0IOHeZJS2JQr qAYdTBJjDKZJhQ0uwJYOyF/URnhCcfE97aSQ4UhrSQ80ls54ZTnMYJR0nE3gDe5z9jB/xIJ56Oe eoNYe6U5TjkGeqs3WWmwsh3coiR2lAL3EQCD+TVYeafWSwnQk8y/2tTnnoE0nBXcd//xg/b0yCW cIMJ8icGIGdFEsg== X-Developer-Key: i=sudeep.holla@arm.com; a=openpgp; fpr=7360A21742ADF5A11767C1C139CFD4755FE2D5B4 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add interface to the FFA driver to allow for client drivers to request and relinquish a notification as well as provide a callback for the notification. Signed-off-by: Sudeep Holla --- drivers/firmware/arm_ffa/driver.c | 145 ++++++++++++++++++++++++++++++++++= ++++ include/linux/arm_ffa.h | 5 ++ 2 files changed, 150 insertions(+) diff --git a/drivers/firmware/arm_ffa/driver.c b/drivers/firmware/arm_ffa/d= river.c index 04cdb49cc78b..fc8b29648fb2 100644 --- a/drivers/firmware/arm_ffa/driver.c +++ b/drivers/firmware/arm_ffa/driver.c @@ -27,11 +27,13 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include @@ -57,6 +59,8 @@ */ #define RXTX_BUFFER_SIZE SZ_4K =20 +#define FFA_MAX_NOTIFICATIONS 64 + static ffa_fn *invoke_ffa_fn; =20 static const int ffa_linux_errmap[] =3D { @@ -102,6 +106,8 @@ struct ffa_drv_info { struct work_struct irq_work; struct xarray partition_info; unsigned int partition_count; + DECLARE_HASHTABLE(notifier_hash, ilog2(FFA_MAX_NOTIFICATIONS)); + struct mutex notify_lock; /* lock to protect notifier hashtable */ }; =20 static struct ffa_drv_info *drv_info; @@ -626,6 +632,8 @@ static int ffa_notification_bitmap_destroy(void) #define MAX_IDS_64 20 #define MAX_IDS_32 10 =20 +#define PER_VCPU_NOTIFICATION_FLAG BIT(0) + static int ffa_notification_bind_common(u16 dst_id, u64 bitmap, u32 flags, bool is_bind) { @@ -865,6 +873,21 @@ static int ffa_memory_lend(struct ffa_mem_ops_args *ar= gs) return ffa_memory_ops(FFA_MEM_LEND, args); } =20 +#define FFA_SECURE_PARTITION_ID_FLAG BIT(15) + +enum notify_type { + NON_SECURE_VM, + SECURE_PARTITION, + FRAMEWORK, +}; + +struct notifier_cb_info { + struct hlist_node hnode; + ffa_notifier_cb cb; + void *cb_data; + enum notify_type type; +}; + static int ffa_sched_recv_cb_update(u16 part_id, ffa_sched_recv_cb callbac= k, void *cb_data, bool is_registration) { @@ -898,6 +921,123 @@ static int ffa_sched_recv_cb_unregister(struct ffa_de= vice *dev) return ffa_sched_recv_cb_update(dev->vm_id, NULL, NULL, false); } =20 +static int ffa_notification_bind(u16 dst_id, u64 bitmap, u32 flags) +{ + return ffa_notification_bind_common(dst_id, bitmap, flags, true); +} + +static int ffa_notification_unbind(u16 dst_id, u64 bitmap) +{ + return ffa_notification_bind_common(dst_id, bitmap, 0, false); +} + +/* Should be called while the notify_lock is taken */ +static struct notifier_cb_info * +notifier_hash_node_get(u16 notify_id, enum notify_type type) +{ + struct notifier_cb_info *node; + + hash_for_each_possible(drv_info->notifier_hash, node, hnode, notify_id) + if (type =3D=3D node->type) + return node; + + return NULL; +} + +static int +update_notifier_cb(int notify_id, enum notify_type type, ffa_notifier_cb c= b, + void *cb_data, bool is_registration) +{ + struct notifier_cb_info *cb_info =3D NULL; + bool cb_found; + + cb_info =3D notifier_hash_node_get(notify_id, type); + cb_found =3D !!cb_info; + + if (!(is_registration ^ cb_found)) + return -EINVAL; + + if (is_registration) { + cb_info =3D kzalloc(sizeof(*cb_info), GFP_KERNEL); + if (!cb_info) + return -ENOMEM; + + cb_info->type =3D type; + cb_info->cb =3D cb; + cb_info->cb_data =3D cb_data; + + hash_add(drv_info->notifier_hash, &cb_info->hnode, notify_id); + } else { + hash_del(&cb_info->hnode); + } + + return 0; +} + +static enum notify_type ffa_notify_type_get(u16 vm_id) +{ + if (vm_id & FFA_SECURE_PARTITION_ID_FLAG) + return SECURE_PARTITION; + else + return NON_SECURE_VM; +} + +static int ffa_notify_relinquish(struct ffa_device *dev, int notify_id) +{ + int rc; + enum notify_type type =3D ffa_notify_type_get(dev->vm_id); + + if (notify_id >=3D FFA_MAX_NOTIFICATIONS) + return -EINVAL; + + mutex_lock(&drv_info->notify_lock); + + rc =3D update_notifier_cb(notify_id, type, NULL, NULL, false); + if (rc) { + pr_err("Could not unregister notifcation callback\n"); + mutex_unlock(&drv_info->notify_lock); + return rc; + } + + rc =3D ffa_notification_unbind(dev->vm_id, BIT(notify_id)); + + mutex_unlock(&drv_info->notify_lock); + + return rc; +} + +static int ffa_notify_request(struct ffa_device *dev, bool is_per_vcpu, + ffa_notifier_cb cb, void *cb_data, int notify_id) +{ + int rc; + u32 flags =3D 0; + enum notify_type type =3D ffa_notify_type_get(dev->vm_id); + + if (notify_id >=3D FFA_MAX_NOTIFICATIONS) + return -EINVAL; + + mutex_lock(&drv_info->notify_lock); + + if (is_per_vcpu) + flags =3D PER_VCPU_NOTIFICATION_FLAG; + + rc =3D ffa_notification_bind(dev->vm_id, BIT(notify_id), flags); + if (rc) { + mutex_unlock(&drv_info->notify_lock); + return rc; + } + + rc =3D update_notifier_cb(notify_id, type, cb, cb_data, true); + if (rc) { + pr_err("Failed to register callback for %d - %d\n", + notify_id, rc); + ffa_notification_unbind(dev->vm_id, BIT(notify_id)); + } + mutex_unlock(&drv_info->notify_lock); + + return rc; +} + static const struct ffa_info_ops ffa_drv_info_ops =3D { .api_version_get =3D ffa_api_version_get, .partition_info_get =3D ffa_partition_info_get, @@ -921,6 +1061,8 @@ static const struct ffa_cpu_ops ffa_drv_cpu_ops =3D { static const struct ffa_notifier_ops ffa_drv_notifier_ops =3D { .sched_recv_cb_register =3D ffa_sched_recv_cb_register, .sched_recv_cb_unregister =3D ffa_sched_recv_cb_unregister, + .notify_request =3D ffa_notify_request, + .notify_relinquish =3D ffa_notify_relinquish, }; =20 static const struct ffa_ops ffa_drv_ops =3D { @@ -1194,6 +1336,9 @@ static int ffa_notifications_setup(void) if (ret) goto cleanup; =20 + hash_init(drv_info->notifier_hash); + mutex_init(&drv_info->notify_lock); + return 0; cleanup: ffa_notifications_cleanup(); diff --git a/include/linux/arm_ffa.h b/include/linux/arm_ffa.h index f9cf6114ef82..fb6f388a3737 100644 --- a/include/linux/arm_ffa.h +++ b/include/linux/arm_ffa.h @@ -392,10 +392,15 @@ struct ffa_cpu_ops { }; =20 typedef void (*ffa_sched_recv_cb)(u16 vcpu, bool is_per_vcpu, void *cb_dat= a); +typedef void (*ffa_notifier_cb)(int notify_id, void *cb_data); + struct ffa_notifier_ops { int (*sched_recv_cb_register)(struct ffa_device *dev, ffa_sched_recv_cb cb, void *cb_data); int (*sched_recv_cb_unregister)(struct ffa_device *dev); + int (*notify_request)(struct ffa_device *dev, bool per_vcpu, + ffa_notifier_cb cb, void *cb_data, int notify_id); + int (*notify_relinquish)(struct ffa_device *dev, int notify_id); }; =20 struct ffa_ops { --=20 2.42.0