From patchwork Tue Sep 19 17:40:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sudeep Holla X-Patchwork-Id: 13391639 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 BE624CE79AC for ; Tue, 19 Sep 2023 17:42:11 +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:To:In-Reply-To:References:Message-Id: MIME-Version:Subject:Date:From:Reply-To:Cc:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Q8VxmK5+wDRZiitty1O5VJWRB/Dy4lZuybg60rz8ZyA=; b=Zaiu0oSa17hQfj vldt6hj5M1l7cLaA2Rl8q9pd5INcJO9cSsiqQ/Oxyr7qiwa9SCNkq1aGCyN1OlMM2lnKV5i3Z8oOB y+5TLb45Y37Q93KrOz1F6OJzvv6WhpRbrjN7wkWwgY9kbSztLuiPBdSiRuG4u9NhZr6PbE+kS08O/ vIhQ+wUPEcN8osjVJkA9D2Y6KinVcsGD4++6/G7JKd20otxLEbO/WigW40/9xTBbOjvZ8UC3JIK0H rEgrFCdGN3tFrxB4wPNeV4Qely/yNUVUKUWBRlZ5bFwhNIndGqlh/tmZe3DFsyBavG6JP4q7LBkSV zhioKCMh1GmaV82rGeLQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qiejE-000xMJ-13; Tue, 19 Sep 2023 17:41:40 +0000 Received: from foss.arm.com ([217.140.110.172]) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qiej5-000xFM-1b for linux-arm-kernel@lists.infradead.org; Tue, 19 Sep 2023 17:41:33 +0000 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 CBAB51FB; Tue, 19 Sep 2023 10:42:07 -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 806EC3F5A1; Tue, 19 Sep 2023 10:41:29 -0700 (PDT) From: Sudeep Holla Date: Tue, 19 Sep 2023 18:40:58 +0100 Subject: [PATCH RFT v2 10/18] firmware: arm_ffa: Add interfaces to request notification callbacks MIME-Version: 1.0 Message-Id: <20230919-ffa_v1-1_notif-v2-10-6f3a3ca3923c@arm.com> References: <20230919-ffa_v1-1_notif-v2-0-6f3a3ca3923c@arm.com> In-Reply-To: <20230919-ffa_v1-1_notif-v2-0-6f3a3ca3923c@arm.com> To: linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Sudeep Holla , Jens Wiklander , Marc Bonnici , Coboy Chen , Lorenzo Pieralisi , Olivier Deprez X-Mailer: b4 0.12.3 X-Developer-Signature: v=1; a=openpgp-sha256; l=6981; i=sudeep.holla@arm.com; h=from:subject:message-id; bh=72LF5V69jHHLAlFxDkCbmJCtna0X9v5HnGdWNK4WsiM=; b=owEBbQKS/ZANAwAIAQBBurwxfuKYAcsmYgBlCd07EzUm1uA88RfgnSHsBdsY6x2sSUTRThh/5 VuDJ6JcqPmJAjMEAAEIAB0WIQS6ceUSBvMeskPdk+EAQbq8MX7imAUCZQndOwAKCRAAQbq8MX7i mEWCEACAtRd1kXFWBOIGiTvWL76o/fzdF5pzpnwav6AiA5jhXFn4tOM/wZIue6Yo8/rF9peX164 4shKweJHFoGB9YB+qkBoXPIziTAIexf3tLMYRJ/BUw4njPyboWN2S/7wSC55brXjpS3aNYcmXFQ L2Ejwa7m3NTVDlmHchXqXtce+2vkXEDTfsqluhGylRWS9pEwiVOOPHXxtbINxVHI+KkMdRuK9b3 54LQM64Uxm+zsWkTwM4uJHPbrRAi/N/RWW3VXXt7fkm0BGV/OnLjDLgwGpW2eSbWf84KnrZ0iMa +YdPAcjyZhCsmHJoRY3nuqXYr5h02oRooXeWB0ZlKoO4az1kXPHkzZGXDI7txryQot9Rc/2q/SQ 7D6Ttoefu2wW0kfamXKEyVfucl7RonkUMahzksF0CV3FdZAWbKRjq7rAW2EflxIkxtOQIfg9NEb whfn/RGWyJfWKEGqmuVAYwwpguu6ClNZUYBBin/HtjEp2xaZqsw2vxOQ1W61kWlsl6HOAJqWi6U i6o8OA5uhsNExh6xgP9U83Dh+rEiRGl6e6TDXM4BMLebL0vihc6UiwQEe9ZBOVQEiJFp74nh0qA aR+yiyp8ArckcPjg+k8C+xyd0rfEeUs3jNQfojN2qz+02NEP/iUWKbCVSv/qnDW/o3f/XEQMmJh Z45Awc+O5z4eapA== X-Developer-Key: i=sudeep.holla@arm.com; a=openpgp; fpr=7360A21742ADF5A11767C1C139CFD4755FE2D5B4 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230919_104131_634041_7CDD93A9 X-CRM114-Status: GOOD ( 18.59 ) 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 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 | 146 ++++++++++++++++++++++++++++++++++++++ include/linux/arm_ffa.h | 5 ++ 2 files changed, 151 insertions(+) diff --git a/drivers/firmware/arm_ffa/driver.c b/drivers/firmware/arm_ffa/driver.c index 017724b40e0b..1d61442ca4fa 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 +#define FFA_MAX_NOTIFICATIONS 64 + static ffa_fn *invoke_ffa_fn; static const int ffa_linux_errmap[] = { @@ -103,6 +107,8 @@ struct ffa_drv_info { bool info_get_64b; 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 */ }; static struct ffa_drv_info *drv_info; @@ -615,6 +621,9 @@ static int ffa_notification_bitmap_destroy(void) #define MAX_IDS_64 20 #define MAX_IDS_32 10 +#define PER_VCPU_NOTIFICATION_FLAG BIT(0) +#define ALL_NOTIFICATION_BITMAPS_FLAGS (0xF) + static int ffa_notification_bind_common(u16 dst_id, u64 bitmap, u32 flags, bool is_bind) { @@ -859,6 +868,21 @@ static int ffa_memory_lend(struct ffa_mem_ops_args *args) return ffa_memory_ops(FFA_MEM_LEND, args); } +#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 callback, void *cb_data, bool is_registration) { @@ -892,6 +916,123 @@ static int ffa_sched_recv_cb_unregister(struct ffa_device *dev) return ffa_sched_recv_cb_update(dev->vm_id, NULL, NULL, false); } +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 == node->type) + return node; + + return NULL; +} + +static int +update_notifier_cb(int notify_id, enum notify_type type, ffa_notifier_cb cb, + void *cb_data, bool is_registration) +{ + struct notifier_cb_info *cb_info = NULL; + bool cb_found; + + cb_info = notifier_hash_node_get(notify_id, type); + cb_found = !!cb_info; + + if (!(is_registration ^ cb_found)) + return -EINVAL; + + if (is_registration) { + cb_info = kzalloc(sizeof(*cb_info), GFP_KERNEL); + if (!cb_info) + return -ENOMEM; + + cb_info->type = type; + cb_info->cb = cb; + cb_info->cb_data = 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 = ffa_notify_type_get(dev->vm_id); + + if (notify_id >= FFA_MAX_NOTIFICATIONS) + return -EINVAL; + + mutex_lock(&drv_info->notify_lock); + + rc = 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 = 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 = 0; + enum notify_type type = ffa_notify_type_get(dev->vm_id); + + if (notify_id >= FFA_MAX_NOTIFICATIONS) + return -EINVAL; + + mutex_lock(&drv_info->notify_lock); + + if (is_per_vcpu) + flags = PER_VCPU_NOTIFICATION_FLAG; + + rc = ffa_notification_bind(dev->vm_id, BIT(notify_id), flags); + if (rc) { + mutex_unlock(&drv_info->notify_lock); + return rc; + } + + rc = 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 = { .api_version_get = ffa_api_version_get, .partition_info_get = ffa_partition_info_get, @@ -915,6 +1056,8 @@ static const struct ffa_cpu_ops ffa_drv_cpu_ops = { static const struct ffa_notifier_ops ffa_drv_notifier_ops = { .sched_recv_cb_register = ffa_sched_recv_cb_register, .sched_recv_cb_unregister = ffa_sched_recv_cb_unregister, + .notify_request = ffa_notify_request, + .notify_relinquish = ffa_notify_relinquish, }; static const struct ffa_ops ffa_drv_ops = { @@ -1190,6 +1333,9 @@ static int ffa_notifications_setup(void) if (ret) goto cleanup; + 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 { }; typedef void (*ffa_sched_recv_cb)(u16 vcpu, bool is_per_vcpu, void *cb_data); +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); }; struct ffa_ops {