Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1CAE0606A330 for ; Fri, 4 Dec 2026 11:16:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=0yLhX4Ys; dkim=fail reason="signature verification failed" (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=DLHKC5EP 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-Unsubscribe-Post:List-Owner; bh=gMFHUAkoqj+54aWKIZnNvWuzNE9pDIy6b2P7/IhN/tA=; b=0yLhX4YsS6Lvl1S3xpgR4o2a2g Fu2Y9r+v7jnRB7dNdt0FGZgLY2000s3uu1uT2Lwen+LHrG0Ei6FErvQS1XX70YbA5pzJ0NhQuLJx9 Vk6f9xj3dIhgglBq4iI5cqNqls6q4IfcaQH3CgTkvaLt9zG4RcUISazeUi2R6nG/lZo+zdQKy+u+G se9EFrniX9shlsujiWnn65YOlIM+zE05UHhbJuOIAjG6+b0I6jLk37j6yph3Y4CVMvIsOFOUQUz+G 7v1puyhlxaOZdkaqJqid/aCNj6fZNmUHQLfN6pI5dyi3mXLzMUjAB50lAM5u8IAQeN3BO7Iv/Pk/e VGG4ruwA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZRGb-000000007Gr-2YEq; Fri, 04 Dec 2026 11:15:53 +0000 Received: from mx0b-0031df01.pphosted.com ([205.220.180.131]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xZRGR-000000007Bv-0Q2h for linux-riscv@lists.infradead.org; Fri, 04 Dec 2026 11:15:45 +0000 Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 6B45IbTa2079575; Fri, 4 Dec 2026 11:15:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:date:from:in-reply-to:message-id :mime-version:references:subject:to; s=qcppdkim1; bh=4167rLuFJrM od7b0fM2MfespVZSc2iNqo9c2zr4Gx0E=; b=DLHKC5EPDBoOMAI+ZDoixs0+6KB 90jQjDE/pnFSSDrGrm+f1xg+rSud8JHYxeKeEeE5rx1G+zN1f64z6T6Wz2wk+3ji pMjKL2zr1S5PV0ZS/6VgRU9vLanCpgOwF2+d9rXpIq+v6WXpKRNkgxIKFofWcRyZ /hi6EQSuM+s+fEc0K79ubfuBqeDmyoG1docUAILlwom+/I/JcpB7cOxS4/RGJDNw yTRYSZy+aWmUzj6G6i7hxWQ0jlWHoj3mV6jsCxire+7tM1m6OBCzzvF2OtjnpFI9 k8dLoIqxaEuNvBLgYaAPN7EoWP1JucpL3tHNQi4ZIbHRPJbUtsJ0Tkw8x3Q== Received: from apblrppmta02.qualcomm.com (blr-bdr-fw-01_GlobalNAT_AllZones-Outside.qualcomm.com [103.229.18.19]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4jbq7qh344-1 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 04 Dec 2026 11:15:19 +0000 (GMT) Received: from pps.filterd (apblrppmta02.qualcomm.com [127.0.0.1]) by APBLRPPMTA02.qualcomm.com (8.18.1.11/8.18.1.11) with ESMTP id 6B4BFGaL3383392; Fri, 4 Dec 2026 11:15:16 GMT Received: from hu-devc-blr-u24-a.qualcomm.com (hu-anuppate-blr.qualcomm.com [10.131.36.165]) by APBLRPPMTA02.qualcomm.com (PPS) with ESMTPS id 6B4BFGan3383375 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Fri, 04 Dec 2026 11:15:16 +0000 (GMT) Received: by hu-devc-blr-u24-a.qualcomm.com (Postfix, from userid 486687) id 4419521513; Fri, 4 Dec 2026 16:45:16 +0530 (+0530) From: Anup Patel To: Palmer Dabbelt , Paul Walmsley , Rahul Pathak , Stephen Boyd , Brian Masney , Jerome Brunet , Thomas Gleixner , Radu Rendec , Jassi Brar Cc: Sunil V L , Himanshu Chauhan , Atish Patra , Anup Patel , Amirreza Zarrabi , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-clk@vger.kernel.org, Anup Patel , Rahul Pathak Subject: [PATCH v3 1/5] RISC-V: Move common MPXY helper routines to arch/riscv Date: Fri, 4 Dec 2026 16:45:00 +0530 Message-ID: <20261204111504.3362889-2-anup.patel@oss.qualcomm.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20261204111504.3362889-1-anup.patel@oss.qualcomm.com> References: <20261204111504.3362889-1-anup.patel@oss.qualcomm.com> X-Archived-By: MIME-Version: 1.0 X-Proofpoint-Reinject: loops=2 maxloops=12 X-QCInternal: smtphost X-QCInternal: smtphost X-Authority-Analysis: v=2.4 cv=Cpsq4X4D c=1 sm=1 tr=0 ts=6b12a0c8 cx=c_pps a=Ou0eQOY4+eZoSc0qltEV5Q==:117 a=Ou0eQOY4+eZoSc0qltEV5Q==:17 a=pUM-KoujPXoA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=gowsoOTTUOVcmtlkKump:22 a=EUspDBNiAAAA:8 a=ndT2cITx6XjDZpHkP44A:9 X-Proofpoint-Spam-Info: AW1haW4tMjYxMjA0MDA0NCBTYWx0ZWRfX8Cj26u43hbJz 3SBYexfapRyw2lVcM2/fgtT/rQDUdeF+ZlLdwLtbSaiRNYqfonIyXdWiFj83tTowI3rPi56ZCYw hU/PAVZ+iZ6xXQoxN7yo3TJ8krntHAU= X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYxMjA0MDA0NCBTYWx0ZWRfX/XE1Pibnc6jM LVrVLt9BuYMa7e2xbXBpvOGY01irr9bxNoZtOmJ7QJ1Lc5npVJCNSpPqc58ZxYU5X0c0v5knUlg KvRbsQtBr72cwe+Y5rFNuWsv6go3m6uxgzmIa1guRRtXE3ealtiwRKP1qCclne6ppReEv1O7Zlt 7A8+v46UE2PWYi4ch8AnOqFo2ouQtZOakWXaHHjrcxo4Wzt3awBJQX+xtroe2jmv7jsYkzH/pop 9ylfpB8iBHLUKzYRCyqq5zrwSlPOL48L5570WeqL5sZoTN2GR8J5S4abwYI08pKpo3rpyFzcLG5 iZi45K+KIgt/mDgt/rczF2PVc7cIhS8LwQsWx0G+5kkzBmi8oK8jnmKWzUp475e+CSV0EXiEv46 w9pueG1/FFaMmnloTqjrfxBnr7F8PconcrLvGD4TTR9lIbJMuaEWRZPSI00NxHJIx7SzUxEzIp1 4b9byYFTdbZ3zIQ59Jw== X-Proofpoint-GUID: Lqc1LbLHA-U7XC7KT6k_RHEoDr4DNBkd X-Proofpoint-ORIG-GUID: Lqc1LbLHA-U7XC7KT6k_RHEoDr4DNBkd X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.138,FMLib:17.12.100.49 definitions=2026-12-04_01,2026-12-03_04,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 bulkscore=0 suspectscore=0 priorityscore=1501 spamscore=0 phishscore=0 malwarescore=0 adultscore=0 clxscore=1015 impostorscore=0 lowpriorityscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2611040001 definitions=main-2612040044 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261204_031543_365710_4A831715 X-CRM114-Status: GOOD ( 23.56 ) X-Spam-Score: -2.7 (--) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" X-Spam-Status: No, score=-17.3 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 There are upcoming kernel changes (such as RISC-V EINJ FFH support) where the MPXY channel_id is known but the Linux mailbox channel instance is not available. For such cases, the kernel will directly do SBI MPXY calls bypassing the Linux mailbox framework and share the MPXY shared memory with the Linux SBI MPXY mailbox driver. To support the above, move the common MPXY helper routines and MPXY shared memory management from Linux SBI MPXY mailbox driver to arch/riscv. Co-developed-by: Himanshu Chauhan Signed-off-by: Himanshu Chauhan Signed-off-by: Anup Patel Reviewed-by: Rahul Pathak --- arch/riscv/include/asm/sbi.h | 86 +++++- arch/riscv/kernel/Makefile | 2 +- arch/riscv/kernel/sbi_mpxy.c | 344 +++++++++++++++++++++++ drivers/mailbox/riscv-sbi-mpxy-mbox.c | 386 +++----------------------- 4 files changed, 464 insertions(+), 354 deletions(-) create mode 100644 arch/riscv/kernel/sbi_mpxy.c diff --git a/arch/riscv/include/asm/sbi.h b/arch/riscv/include/asm/sbi.h index fd6266a7b339..1ba1f66af02c 100644 --- a/arch/riscv/include/asm/sbi.h +++ b/arch/riscv/include/asm/sbi.h @@ -693,6 +693,37 @@ static inline int sbi_fwft_set_online_cpus(u32 feature, unsigned long value, return sbi_fwft_set_cpumask(cpu_online_mask, feature, value, flags); } +/* SBI MPXY notification data in shared memory */ +struct sbi_mpxy_notification_data { + /* Remaining number of notification events */ + __le32 remaining; + /* Number of notification events returned */ + __le32 returned; + /* Number of notification events lost */ + __le32 lost; + /* Reserved for future use */ + __le32 reserved; + /* Returned channel id array */ + u8 events_data[]; +}; + +unsigned long sbi_mpxy_shmem_size(void); +int sbi_mpxy_get_channel_count(u32 *channel_count); +int sbi_mpxy_get_channel_ids(u32 channel_count, u32 *channel_ids); +int sbi_mpxy_read_attrs(u32 channel_id, u32 base_attrid, u32 attr_count, + u32 *attrs_buf); +int sbi_mpxy_write_attrs(u32 channel_id, u32 base_attrid, u32 attr_count, + u32 *attrs_buf); +int sbi_mpxy_send_message_with_resp(u32 channel_id, u32 msg_id, + void *tx, unsigned long tx_len, + void *rx, unsigned long max_rx_len, + unsigned long *rx_len); +int sbi_mpxy_send_message_without_resp(u32 channel_id, u32 msg_id, + void *tx, unsigned long tx_len); +int sbi_mpxy_get_notifications(u32 channel_id, + struct sbi_mpxy_notification_data *notif_data, + unsigned long *events_data_len); + /* Check if current SBI specification version is 0.1 or not */ static inline int sbi_spec_is_0_1(void) { @@ -754,7 +785,60 @@ int sbi_debug_console_read(char *bytes, unsigned int num_bytes); #else /* CONFIG_RISCV_SBI */ static inline int sbi_remote_fence_i(const struct cpumask *cpu_mask) { return -1; } -static inline void sbi_init(void) {} +static inline void sbi_init(void) +{ +} + +struct sbi_mpxy_notification_data; + +static inline unsigned long sbi_mpxy_shmem_size(void) +{ + return 0; +} + +static inline int sbi_mpxy_get_channel_count(u32 *channel_count) +{ + return -ENODEV; +} + +static inline int sbi_mpxy_get_channel_ids(u32 channel_count, u32 *channel_ids) +{ + return -ENODEV; +} + +static inline int sbi_mpxy_read_attrs(u32 channel_id, u32 base_attrid, u32 attr_count, + u32 *attrs_buf) +{ + return -ENODEV; +} + +static inline int sbi_mpxy_write_attrs(u32 channel_id, u32 base_attrid, u32 attr_count, + u32 *attrs_buf) +{ + return -ENODEV; +} + +static inline int sbi_mpxy_send_message_with_resp(u32 channel_id, u32 msg_id, + void *tx, unsigned long tx_len, + void *rx, unsigned long max_rx_len, + unsigned long *rx_len) +{ + return -ENODEV; +} + +static inline int sbi_mpxy_send_message_without_resp(u32 channel_id, u32 msg_id, + void *tx, unsigned long tx_len) +{ + return -ENODEV; +} + +static inline int sbi_mpxy_get_notifications(u32 channel_id, + struct sbi_mpxy_notification_data *notif_data, + unsigned long *events_data_len) +{ + return -ENODEV; +} + #endif /* CONFIG_RISCV_SBI */ unsigned long riscv_get_mvendorid(void); diff --git a/arch/riscv/kernel/Makefile b/arch/riscv/kernel/Makefile index 7f52ae724225..cb2578182ee5 100644 --- a/arch/riscv/kernel/Makefile +++ b/arch/riscv/kernel/Makefile @@ -101,7 +101,7 @@ obj-$(CONFIG_DYNAMIC_FTRACE) += mcount-dyn.o obj-$(CONFIG_PERF_EVENTS) += perf_callchain.o obj-$(CONFIG_HAVE_PERF_REGS) += perf_regs.o -obj-$(CONFIG_RISCV_SBI) += sbi.o sbi_ecall.o +obj-$(CONFIG_RISCV_SBI) += sbi.o sbi_ecall.o sbi_mpxy.o ifeq ($(CONFIG_RISCV_SBI), y) obj-$(CONFIG_SMP) += sbi-ipi.o obj-$(CONFIG_SMP) += cpu_ops_sbi.o diff --git a/arch/riscv/kernel/sbi_mpxy.c b/arch/riscv/kernel/sbi_mpxy.c new file mode 100644 index 000000000000..c1c5058d3252 --- /dev/null +++ b/arch/riscv/kernel/sbi_mpxy.c @@ -0,0 +1,344 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Common SBI MPXY access library. + * + * Copyright (c) 2026 Qualcomm Technologies, Inc. + */ + +#define pr_fmt(fmt) "riscv: " fmt +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* SBI MPXY channel IDs data in shared memory */ +struct sbi_mpxy_channel_ids_data { + /* Remaining number of channel ids */ + __le32 remaining; + /* Returned channel ids in current function call */ + __le32 returned; + /* Returned channel id array */ + __le32 channel_array[]; +}; + +/* MPXY Per-CPU or local context */ +struct mpxy_local { + /* Shared memory base address */ + void *shmem; + /* Shared memory physical address */ + phys_addr_t shmem_phys_addr; + /* Flag representing whether shared memory is active or not */ + bool shmem_active; +}; + +static DEFINE_PER_CPU(struct mpxy_local, mpxy_local); +static unsigned long mpxy_shmem_size; +static bool mpxy_shmem_init_done; + +unsigned long sbi_mpxy_shmem_size(void) +{ + if (!mpxy_shmem_init_done) + return 0; + return mpxy_shmem_size; +} +EXPORT_SYMBOL_GPL(sbi_mpxy_shmem_size); + +int sbi_mpxy_get_channel_count(u32 *channel_count) +{ + struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); + struct sbi_mpxy_channel_ids_data *sdata = mpxy->shmem; + u32 remaining, returned; + struct sbiret sret; + + if (!mpxy->shmem_active) + return -ENODEV; + if (!channel_count) + return -EINVAL; + + get_cpu(); + + /* Get the remaining and returned fields to calculate total */ + sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_CHANNEL_IDS, + 0, 0, 0, 0, 0, 0); + if (sret.error) + goto err_put_cpu; + + remaining = le32_to_cpu(sdata->remaining); + returned = le32_to_cpu(sdata->returned); + *channel_count = remaining + returned; + +err_put_cpu: + put_cpu(); + return sbi_err_map_linux_errno(sret.error); +} +EXPORT_SYMBOL_GPL(sbi_mpxy_get_channel_count); + +int sbi_mpxy_get_channel_ids(u32 channel_count, u32 *channel_ids) +{ + struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); + struct sbi_mpxy_channel_ids_data *sdata = mpxy->shmem; + u32 remaining, returned, count, start_index = 0; + struct sbiret sret; + + if (!mpxy->shmem_active) + return -ENODEV; + if (!channel_count || !channel_ids) + return -EINVAL; + + get_cpu(); + + do { + sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_CHANNEL_IDS, + start_index, 0, 0, 0, 0, 0); + if (sret.error) + goto err_put_cpu; + + remaining = le32_to_cpu(sdata->remaining); + returned = le32_to_cpu(sdata->returned); + + count = returned < (channel_count - start_index) ? + returned : (channel_count - start_index); + memcpy_from_le32(&channel_ids[start_index], sdata->channel_array, count); + start_index += count; + } while (remaining && start_index < channel_count); + +err_put_cpu: + put_cpu(); + return sbi_err_map_linux_errno(sret.error); +} +EXPORT_SYMBOL_GPL(sbi_mpxy_get_channel_ids); + +int sbi_mpxy_read_attrs(u32 channel_id, u32 base_attrid, u32 attr_count, + u32 *attrs_buf) +{ + struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); + struct sbiret sret; + + if (!mpxy->shmem_active) + return -ENODEV; + if (!attr_count || !attrs_buf) + return -EINVAL; + + get_cpu(); + + sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_READ_ATTRS, + channel_id, base_attrid, attr_count, 0, 0, 0); + if (sret.error) + goto err_put_cpu; + + memcpy_from_le32(attrs_buf, (__le32 *)mpxy->shmem, attr_count); + +err_put_cpu: + put_cpu(); + return sbi_err_map_linux_errno(sret.error); +} +EXPORT_SYMBOL_GPL(sbi_mpxy_read_attrs); + +int sbi_mpxy_write_attrs(u32 channel_id, u32 base_attrid, u32 attr_count, + u32 *attrs_buf) +{ + struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); + struct sbiret sret; + + if (!mpxy->shmem_active) + return -ENODEV; + if (!attr_count || !attrs_buf) + return -EINVAL; + + get_cpu(); + + memcpy_to_le32((__le32 *)mpxy->shmem, attrs_buf, attr_count); + sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_WRITE_ATTRS, + channel_id, base_attrid, attr_count, 0, 0, 0); + + put_cpu(); + return sbi_err_map_linux_errno(sret.error); +} +EXPORT_SYMBOL_GPL(sbi_mpxy_write_attrs); + +int sbi_mpxy_send_message_with_resp(u32 channel_id, u32 msg_id, + void *tx, unsigned long tx_len, + void *rx, unsigned long max_rx_len, + unsigned long *rx_len) +{ + struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); + unsigned long rx_bytes; + struct sbiret sret; + + if (!mpxy->shmem_active) + return -ENODEV; + if (!tx && tx_len) + return -EINVAL; + if (mpxy_shmem_size < tx_len) + return -ENOSPC; + + get_cpu(); + + /* Message protocols allowed to have no data in messages */ + if (tx_len) + memcpy(mpxy->shmem, tx, tx_len); + + sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_SEND_MSG_WITH_RESP, + channel_id, msg_id, tx_len, 0, 0, 0); + if (rx && !sret.error) { + rx_bytes = sret.value; + if (rx_bytes > max_rx_len) { + put_cpu(); + return -ENOSPC; + } + + memcpy(rx, mpxy->shmem, rx_bytes); + if (rx_len) + *rx_len = rx_bytes; + } + + put_cpu(); + return sbi_err_map_linux_errno(sret.error); +} +EXPORT_SYMBOL_GPL(sbi_mpxy_send_message_with_resp); + +int sbi_mpxy_send_message_without_resp(u32 channel_id, u32 msg_id, + void *tx, unsigned long tx_len) +{ + struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); + struct sbiret sret; + + if (!mpxy->shmem_active) + return -ENODEV; + if (!tx && tx_len) + return -EINVAL; + if (mpxy_shmem_size < tx_len) + return -ENOSPC; + + get_cpu(); + + /* Message protocols allowed to have no data in messages */ + if (tx_len) + memcpy(mpxy->shmem, tx, tx_len); + + sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_SEND_MSG_WITHOUT_RESP, + channel_id, msg_id, tx_len, 0, 0, 0); + + put_cpu(); + return sbi_err_map_linux_errno(sret.error); +} +EXPORT_SYMBOL_GPL(sbi_mpxy_send_message_without_resp); + +int sbi_mpxy_get_notifications(u32 channel_id, + struct sbi_mpxy_notification_data *notif_data, + unsigned long *events_data_len) +{ + struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); + struct sbiret sret; + + if (!mpxy->shmem_active) + return -ENODEV; + if (!notif_data || !events_data_len) + return -EINVAL; + + get_cpu(); + + sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_NOTIFICATION_EVENTS, + channel_id, 0, 0, 0, 0, 0); + if (sret.error) + goto err_put_cpu; + if (sret.value < 0 || mpxy_shmem_size < sizeof(*notif_data) || + sret.value > mpxy_shmem_size - sizeof(*notif_data)) { + put_cpu(); + return -EOVERFLOW; + } + + memcpy(notif_data, mpxy->shmem, sret.value + sizeof(*notif_data)); + *events_data_len = sret.value; + +err_put_cpu: + put_cpu(); + return sbi_err_map_linux_errno(sret.error); +} +EXPORT_SYMBOL_GPL(sbi_mpxy_get_notifications); + +static int mpxy_get_shmem_size(unsigned long *shmem_size) +{ + struct sbiret sret; + + sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_SHMEM_SIZE, + 0, 0, 0, 0, 0, 0); + if (sret.error) + return sbi_err_map_linux_errno(sret.error); + if (shmem_size) + *shmem_size = sret.value; + return 0; +} + +static int mpxy_setup_shmem(unsigned int cpu) +{ + struct page *shmem_page; + struct mpxy_local *mpxy; + struct sbiret sret; + + mpxy = per_cpu_ptr(&mpxy_local, cpu); + if (mpxy->shmem_active) + return 0; + + shmem_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(mpxy_shmem_size)); + if (!shmem_page) + return -ENOMEM; + + /* + * Linux setup of shmem is done in mpxy OVERWRITE mode. + * flags[1:0] = 00b + */ + sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_SET_SHMEM, + page_to_phys(shmem_page), 0, 0, 0, 0, 0); + if (sret.error) { + free_pages((unsigned long)page_to_virt(shmem_page), + get_order(mpxy_shmem_size)); + return sbi_err_map_linux_errno(sret.error); + } + + mpxy->shmem = page_to_virt(shmem_page); + mpxy->shmem_phys_addr = page_to_phys(shmem_page); + mpxy->shmem_active = true; + + return 0; +} + +static int __init sbi_mpxy_init(void) +{ + int rc; + + /* Skip quietly when MPXY extension is not supported. */ + if (sbi_spec_version < sbi_mk_version(3, 0) || + !sbi_probe_extension(SBI_EXT_MPXY)) + return 0; + pr_info("SBI MPXY extension detected\n"); + + /* Find-out shared memory size */ + rc = mpxy_get_shmem_size(&mpxy_shmem_size); + if (rc) { + pr_err("failed to get MPXY shared memory size\n"); + return rc; + } + + /* + * Setup MPXY shared memory on each CPU + * + * Note: Don't cleanup MPXY shared memory upon CPU power-down + * because the RPMI System MSI irqchip driver needs it to be + * available when migrating IRQs in CPU power-down path. + */ + rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "riscv/sbi-mpxy-shmem", + mpxy_setup_shmem, NULL); + if (rc < 0) + return rc; + + /* Mark as MPXY shared memory initialization done */ + mpxy_shmem_init_done = true; + return 0; +} +arch_initcall(sbi_mpxy_init); diff --git a/drivers/mailbox/riscv-sbi-mpxy-mbox.c b/drivers/mailbox/riscv-sbi-mpxy-mbox.c index 2134ceea1044..432972195e46 100644 --- a/drivers/mailbox/riscv-sbi-mpxy-mbox.c +++ b/drivers/mailbox/riscv-sbi-mpxy-mbox.c @@ -17,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -85,295 +84,6 @@ struct sbi_mpxy_rpmi_channel_attrs { u32 impl_version; }; -/* SBI MPXY channel IDs data in shared memory */ -struct sbi_mpxy_channel_ids_data { - /* Remaining number of channel ids */ - __le32 remaining; - /* Returned channel ids in current function call */ - __le32 returned; - /* Returned channel id array */ - __le32 channel_array[]; -}; - -/* SBI MPXY notification data in shared memory */ -struct sbi_mpxy_notification_data { - /* Remaining number of notification events */ - __le32 remaining; - /* Number of notification events returned */ - __le32 returned; - /* Number of notification events lost */ - __le32 lost; - /* Reserved for future use */ - __le32 reserved; - /* Returned channel id array */ - u8 events_data[]; -}; - -/* ====== MPXY data structures & helper routines ====== */ - -/* MPXY Per-CPU or local context */ -struct mpxy_local { - /* Shared memory base address */ - void *shmem; - /* Shared memory physical address */ - phys_addr_t shmem_phys_addr; - /* Flag representing whether shared memory is active or not */ - bool shmem_active; -}; - -static DEFINE_PER_CPU(struct mpxy_local, mpxy_local); -static unsigned long mpxy_shmem_size; -static bool mpxy_shmem_init_done; - -static int mpxy_get_channel_count(u32 *channel_count) -{ - struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); - struct sbi_mpxy_channel_ids_data *sdata = mpxy->shmem; - u32 remaining, returned; - struct sbiret sret; - - if (!mpxy->shmem_active) - return -ENODEV; - if (!channel_count) - return -EINVAL; - - get_cpu(); - - /* Get the remaining and returned fields to calculate total */ - sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_CHANNEL_IDS, - 0, 0, 0, 0, 0, 0); - if (sret.error) - goto err_put_cpu; - - remaining = le32_to_cpu(sdata->remaining); - returned = le32_to_cpu(sdata->returned); - *channel_count = remaining + returned; - -err_put_cpu: - put_cpu(); - return sbi_err_map_linux_errno(sret.error); -} - -static int mpxy_get_channel_ids(u32 channel_count, u32 *channel_ids) -{ - struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); - struct sbi_mpxy_channel_ids_data *sdata = mpxy->shmem; - u32 remaining, returned, count, start_index = 0; - struct sbiret sret; - - if (!mpxy->shmem_active) - return -ENODEV; - if (!channel_count || !channel_ids) - return -EINVAL; - - get_cpu(); - - do { - sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_CHANNEL_IDS, - start_index, 0, 0, 0, 0, 0); - if (sret.error) - goto err_put_cpu; - - remaining = le32_to_cpu(sdata->remaining); - returned = le32_to_cpu(sdata->returned); - - count = returned < (channel_count - start_index) ? - returned : (channel_count - start_index); - memcpy_from_le32(&channel_ids[start_index], sdata->channel_array, count); - start_index += count; - } while (remaining && start_index < channel_count); - -err_put_cpu: - put_cpu(); - return sbi_err_map_linux_errno(sret.error); -} - -static int mpxy_read_attrs(u32 channel_id, u32 base_attrid, u32 attr_count, - u32 *attrs_buf) -{ - struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); - struct sbiret sret; - - if (!mpxy->shmem_active) - return -ENODEV; - if (!attr_count || !attrs_buf) - return -EINVAL; - - get_cpu(); - - sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_READ_ATTRS, - channel_id, base_attrid, attr_count, 0, 0, 0); - if (sret.error) - goto err_put_cpu; - - memcpy_from_le32(attrs_buf, (__le32 *)mpxy->shmem, attr_count); - -err_put_cpu: - put_cpu(); - return sbi_err_map_linux_errno(sret.error); -} - -static int mpxy_write_attrs(u32 channel_id, u32 base_attrid, u32 attr_count, - u32 *attrs_buf) -{ - struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); - struct sbiret sret; - - if (!mpxy->shmem_active) - return -ENODEV; - if (!attr_count || !attrs_buf) - return -EINVAL; - - get_cpu(); - - memcpy_to_le32((__le32 *)mpxy->shmem, attrs_buf, attr_count); - sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_WRITE_ATTRS, - channel_id, base_attrid, attr_count, 0, 0, 0); - - put_cpu(); - return sbi_err_map_linux_errno(sret.error); -} - -static int mpxy_send_message_with_resp(u32 channel_id, u32 msg_id, - void *tx, unsigned long tx_len, - void *rx, unsigned long max_rx_len, - unsigned long *rx_len) -{ - struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); - unsigned long rx_bytes; - struct sbiret sret; - - if (!mpxy->shmem_active) - return -ENODEV; - if (!tx && tx_len) - return -EINVAL; - - get_cpu(); - - /* Message protocols allowed to have no data in messages */ - if (tx_len) - memcpy(mpxy->shmem, tx, tx_len); - - sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_SEND_MSG_WITH_RESP, - channel_id, msg_id, tx_len, 0, 0, 0); - if (rx && !sret.error) { - rx_bytes = sret.value; - if (rx_bytes > max_rx_len) { - put_cpu(); - return -ENOSPC; - } - - memcpy(rx, mpxy->shmem, rx_bytes); - if (rx_len) - *rx_len = rx_bytes; - } - - put_cpu(); - return sbi_err_map_linux_errno(sret.error); -} - -static int mpxy_send_message_without_resp(u32 channel_id, u32 msg_id, - void *tx, unsigned long tx_len) -{ - struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); - struct sbiret sret; - - if (!mpxy->shmem_active) - return -ENODEV; - if (!tx && tx_len) - return -EINVAL; - - get_cpu(); - - /* Message protocols allowed to have no data in messages */ - if (tx_len) - memcpy(mpxy->shmem, tx, tx_len); - - sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_SEND_MSG_WITHOUT_RESP, - channel_id, msg_id, tx_len, 0, 0, 0); - - put_cpu(); - return sbi_err_map_linux_errno(sret.error); -} - -static int mpxy_get_notifications(u32 channel_id, - struct sbi_mpxy_notification_data *notif_data, - unsigned long *events_data_len) -{ - struct mpxy_local *mpxy = this_cpu_ptr(&mpxy_local); - struct sbiret sret; - - if (!mpxy->shmem_active) - return -ENODEV; - if (!notif_data || !events_data_len) - return -EINVAL; - - get_cpu(); - - sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_NOTIFICATION_EVENTS, - channel_id, 0, 0, 0, 0, 0); - if (sret.error) - goto err_put_cpu; - if (sret.value < 0 || mpxy_shmem_size < sizeof(*notif_data) || - sret.value > mpxy_shmem_size - sizeof(*notif_data)) { - put_cpu(); - return -EOVERFLOW; - } - - memcpy(notif_data, mpxy->shmem, sret.value + sizeof(*notif_data)); - *events_data_len = sret.value; - -err_put_cpu: - put_cpu(); - return sbi_err_map_linux_errno(sret.error); -} - -static int mpxy_get_shmem_size(unsigned long *shmem_size) -{ - struct sbiret sret; - - sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_GET_SHMEM_SIZE, - 0, 0, 0, 0, 0, 0); - if (sret.error) - return sbi_err_map_linux_errno(sret.error); - if (shmem_size) - *shmem_size = sret.value; - return 0; -} - -static int mpxy_setup_shmem(unsigned int cpu) -{ - struct page *shmem_page; - struct mpxy_local *mpxy; - struct sbiret sret; - - mpxy = per_cpu_ptr(&mpxy_local, cpu); - if (mpxy->shmem_active) - return 0; - - shmem_page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(mpxy_shmem_size)); - if (!shmem_page) - return -ENOMEM; - - /* - * Linux setup of shmem is done in mpxy OVERWRITE mode. - * flags[1:0] = 00b - */ - sret = sbi_ecall(SBI_EXT_MPXY, SBI_EXT_MPXY_SET_SHMEM, - page_to_phys(shmem_page), 0, 0, 0, 0, 0); - if (sret.error) { - free_pages((unsigned long)page_to_virt(shmem_page), - get_order(mpxy_shmem_size)); - return sbi_err_map_linux_errno(sret.error); - } - - mpxy->shmem = page_to_virt(shmem_page); - mpxy->shmem_phys_addr = page_to_phys(shmem_page); - mpxy->shmem_active = true; - - return 0; -} - /* ====== MPXY mailbox data structures ====== */ /* MPXY mailbox channel */ @@ -447,13 +157,13 @@ static void mpxy_mbox_send_rpmi_data(struct mpxy_mbox_channel *mchan, msg->error = -EIO; break; } - msg->error = mpxy_send_message_with_resp(mchan->channel_id, - msg->data.service_id, - msg->data.request, - msg->data.request_len, - msg->data.response, - msg->data.max_response_len, - &msg->data.out_response_len); + msg->error = sbi_mpxy_send_message_with_resp(mchan->channel_id, + msg->data.service_id, + msg->data.request, + msg->data.request_len, + msg->data.response, + msg->data.max_response_len, + &msg->data.out_response_len); break; case RPMI_MBOX_MSG_TYPE_SEND_WITHOUT_RESPONSE: if ((!msg->data.request && msg->data.request_len) || @@ -465,10 +175,10 @@ static void mpxy_mbox_send_rpmi_data(struct mpxy_mbox_channel *mchan, msg->error = -EIO; break; } - msg->error = mpxy_send_message_without_resp(mchan->channel_id, - msg->data.service_id, - msg->data.request, - msg->data.request_len); + msg->error = sbi_mpxy_send_message_without_resp(mchan->channel_id, + msg->data.service_id, + msg->data.request, + msg->data.request_len); break; default: msg->error = -EOPNOTSUPP; @@ -504,10 +214,10 @@ static void mpxy_mbox_peek_rpmi_data(struct mbox_chan *chan, static int mpxy_mbox_read_rpmi_attrs(struct mpxy_mbox_channel *mchan) { - return mpxy_read_attrs(mchan->channel_id, - SBI_MPXY_ATTR_MSGPROTO_ATTR_START, - sizeof(mchan->rpmi_attrs) / sizeof(u32), - (u32 *)&mchan->rpmi_attrs); + return sbi_mpxy_read_attrs(mchan->channel_id, + SBI_MPXY_ATTR_MSGPROTO_ATTR_START, + sizeof(mchan->rpmi_attrs) / sizeof(u32), + (u32 *)&mchan->rpmi_attrs); } /* ====== MPXY mailbox callbacks ====== */ @@ -536,7 +246,7 @@ static bool mpxy_mbox_peek_data(struct mbox_chan *chan) return false; do { - rc = mpxy_get_notifications(mchan->channel_id, notif, &data_len); + rc = sbi_mpxy_get_notifications(mchan->channel_id, notif, &data_len); if (rc || !data_len) break; @@ -580,8 +290,8 @@ static int mpxy_mbox_setup_msi(struct mbox_chan *chan, /* Enable channel MSI control */ mchan->attrs.msi_control = 1; - rc = mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSI_CONTROL, - 1, &mchan->attrs.msi_control); + rc = sbi_mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSI_CONTROL, + 1, &mchan->attrs.msi_control); if (rc) { dev_err(dev, "enable MSI control failed for MPXY channel 0x%x\n", mchan->channel_id); @@ -609,8 +319,8 @@ static void mpxy_mbox_cleanup_msi(struct mbox_chan *chan, /* Disable channel MSI control */ mchan->attrs.msi_control = 0; - rc = mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSI_CONTROL, - 1, &mchan->attrs.msi_control); + rc = sbi_mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSI_CONTROL, + 1, &mchan->attrs.msi_control); if (rc) { dev_err(dev, "disable MSI control failed for MPXY channel 0x%x\n", mchan->channel_id); @@ -635,8 +345,8 @@ static int mpxy_mbox_setup_events(struct mpxy_mbox_channel *mchan) /* Enable channel events state */ mchan->attrs.events_state_ctrl = 1; - rc = mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_EVENTS_STATE_CONTROL, - 1, &mchan->attrs.events_state_ctrl); + rc = sbi_mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_EVENTS_STATE_CONTROL, + 1, &mchan->attrs.events_state_ctrl); if (rc) { dev_err(dev, "enable events state failed for MPXY channel 0x%x\n", mchan->channel_id); @@ -662,8 +372,8 @@ static void mpxy_mbox_cleanup_events(struct mpxy_mbox_channel *mchan) /* Disable channel events state */ mchan->attrs.events_state_ctrl = 0; - rc = mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_EVENTS_STATE_CONTROL, - 1, &mchan->attrs.events_state_ctrl); + rc = sbi_mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_EVENTS_STATE_CONTROL, + 1, &mchan->attrs.events_state_ctrl); if (rc) dev_err(dev, "disable events state failed for MPXY channel 0x%x\n", mchan->channel_id); @@ -741,8 +451,8 @@ static void mpxy_mbox_msi_write(struct msi_desc *desc, struct msi_msg *msg) minfo->msi_addr_hi = msg->address_hi; minfo->msi_data = msg->data; - rc = mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSI_ADDR_LO, - sizeof(*minfo) / sizeof(u32), (u32 *)minfo); + rc = sbi_mpxy_write_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSI_ADDR_LO, + sizeof(*minfo) / sizeof(u32), (u32 *)minfo); if (rc) { dev_warn(dev, "failed to write MSI info for MPXY channel 0x%x\n", mchan->channel_id); @@ -776,7 +486,7 @@ static int mpxy_mbox_populate_channels(struct mpxy_mbox *mbox) int rc; /* Find-out of number of channels */ - rc = mpxy_get_channel_count(&mbox->channel_count); + rc = sbi_mpxy_get_channel_count(&mbox->channel_count); if (rc) return dev_err_probe(mbox->dev, rc, "failed to get number of MPXY channels\n"); if (!mbox->channel_count) @@ -786,7 +496,7 @@ static int mpxy_mbox_populate_channels(struct mpxy_mbox *mbox) channel_ids = kzalloc_objs(*channel_ids, mbox->channel_count); if (!channel_ids) return -ENOMEM; - rc = mpxy_get_channel_ids(mbox->channel_count, channel_ids); + rc = sbi_mpxy_get_channel_ids(mbox->channel_count, channel_ids); if (rc) return dev_err_probe(mbox->dev, rc, "failed to get MPXY channel IDs\n"); @@ -800,9 +510,9 @@ static int mpxy_mbox_populate_channels(struct mpxy_mbox *mbox) mchan->mbox = mbox; mchan->channel_id = channel_ids[i]; - rc = mpxy_read_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSG_PROT_ID, - sizeof(mchan->attrs) / sizeof(u32), - (u32 *)&mchan->attrs); + rc = sbi_mpxy_read_attrs(mchan->channel_id, SBI_MPXY_ATTR_MSG_PROT_ID, + sizeof(mchan->attrs) / sizeof(u32), + (u32 *)&mchan->attrs); if (rc) { return dev_err_probe(mbox->dev, rc, "MPXY channel 0x%x read attrs failed\n", @@ -818,11 +528,11 @@ static int mpxy_mbox_populate_channels(struct mpxy_mbox *mbox) } } - mchan->notif = devm_kzalloc(mbox->dev, mpxy_shmem_size, GFP_KERNEL); + mchan->notif = devm_kzalloc(mbox->dev, sbi_mpxy_shmem_size(), GFP_KERNEL); if (!mchan->notif) return -ENOMEM; - mchan->max_xfer_len = min(mpxy_shmem_size, mchan->attrs.msg_max_len); + mchan->max_xfer_len = min(sbi_mpxy_shmem_size(), mchan->attrs.msg_max_len); if ((mchan->attrs.capability & SBI_MPXY_CHAN_CAP_GET_NOTIFICATIONS) && (mchan->attrs.capability & SBI_MPXY_CHAN_CAP_EVENTS_STATE)) @@ -847,40 +557,12 @@ static int mpxy_mbox_probe(struct platform_device *pdev) int msi_idx, rc; u32 i; - /* - * Initialize MPXY shared memory only once. This also ensures - * that SBI MPXY mailbox is probed only once. - */ - if (mpxy_shmem_init_done) { - dev_err(dev, "SBI MPXY mailbox already initialized\n"); - return -EALREADY; - } - /* Probe for SBI MPXY extension */ - if (sbi_spec_version < sbi_mk_version(1, 0) || - sbi_probe_extension(SBI_EXT_MPXY) <= 0) { + if (!sbi_mpxy_shmem_size()) { dev_info(dev, "SBI MPXY extension not available\n"); return -ENODEV; } - /* Find-out shared memory size */ - rc = mpxy_get_shmem_size(&mpxy_shmem_size); - if (rc) - return dev_err_probe(dev, rc, "failed to get MPXY shared memory size\n"); - - /* - * Setup MPXY shared memory on each CPU - * - * Note: Don't cleanup MPXY shared memory upon CPU power-down - * because the RPMI System MSI irqchip driver needs it to be - * available when migrating IRQs in CPU power-down path. - */ - cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "riscv/sbi-mpxy-shmem", - mpxy_setup_shmem, NULL); - - /* Mark as MPXY shared memory initialization done */ - mpxy_shmem_init_done = true; - /* Allocate mailbox instance */ mbox = devm_kzalloc_obj(dev, *mbox); if (!mbox) -- 2.43.0 _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv