From patchwork Wed Apr 10 06:03:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng Yang X-Patchwork-Id: 1921724 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=m3lAtU0X; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=foxmail.com header.i=@foxmail.com header.a=rsa-sha256 header.s=s201512 header.b=TCob5xYP; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VDsqp05FJz1yYS for ; Wed, 10 Apr 2024 16:09:58 +1000 (AEST) 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:Date:Subject:Cc:To:From: Message-ID: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=Fdc3Gcgk8sy2LhslIL2Fur7/JVeLolDWzudSAqQE/ko=; b=m3lAtU0XS5KTZ1 2Ov9rfPjaylhAvvhMMfSEhcL2jpi8k+Dgq+TRNICGfiKFHBZmmLo/BrvE51yMh6AnCaevPgdNfSQs Vr/FaqynseO7dlzak2x3z6nu9qX0vE1CkKeTi08Wvf3+uD2VIyyZDOaQvFizQtyVH6nXkW01VslyP zbc3qMJI89L8zVnCssYyemSANRltlJTrtIjIu0R5RSyhS7n5H+ydReZkaQ76BGFI1oYpSUJJ+xjvo Db0Jui7J+LrWpHfBQavbJc0O/vfOoUxqvZegYzpA6LsZV9VUoM4cNkvF4mAZLaRXb0mbZNfXA4d+7 6+sAOINY5Lu1F/d0ZQ8A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1ruR9Z-00000005JiP-3Tj2; Wed, 10 Apr 2024 06:09:49 +0000 Received: from out203-205-251-73.mail.qq.com ([203.205.251.73]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1ruR9W-00000005Jbl-3Oki for opensbi@lists.infradead.org; Wed, 10 Apr 2024 06:09:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1712729061; bh=RD08GzSP1Ev4AOjvEYaNV7MSzvoYCwVgUhomUCV47QE=; h=From:To:Cc:Subject:Date; b=TCob5xYPKpk23JBX08v9gUiPP3ft5GXM/jgYlPOrpoXXuESeiqL5bW5G2UYFCtm/w Q/Z64jh8PPW9zPEdDJqZiYLwNsWWEyAoGmQ2yvsh4d6MkZoZqW1N6c/uwt9Q24eqwN 0ogA2j75KnqBeCIj5yBs+OP0AtVxIxtJEKXPAOn8= Received: from yc-home.taila82bd.ts.net ([2409:8a62:e7e:b8c0:9d0b:3d31:48f7:4610]) by newxmesmtplogicsvrszc5-0.qq.com (NewEsmtp) with SMTP id 1140848C; Wed, 10 Apr 2024 14:04:20 +0800 X-QQ-mid: xmsmtpt1712729060tb4c034xy Message-ID: X-QQ-XMAILINFO: NcEwhrcyhX+EhRSxSs1xwiobQf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IPUPNwYeFl4zTgrDZ/YzbG5pG+ n2wEnn X-QQ-XMRINFO: Nq+8W0+stu50PRdwbJxPCL0= From: Cheng Yang To: opensbi@lists.infradead.org Cc: wxjstz@126.com, apatel@ventanamicro.com, Cheng Yang Subject: [PATCH v3] lib:sbi:platform:generic: Improved mlevel imsic check and init. Date: Wed, 10 Apr 2024 14:03:23 +0800 X-OQ-MSGID: <20240410060321.2098033-1-yangcheng.work@foxmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240409_230947_421412_A7B0747B X-CRM114-Status: GOOD ( 19.89 ) X-Spam-Score: 3.4 (+++) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: The current mlevel imsic check is only for the platform, which may cause hart without imsic in the platform to trigger an illegal instruction exception when initializing imsic. For example, the platfo [...] Content analysis details: (3.4 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [203.205.251.73 listed in list.dnswl.org] -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [yangcheng.work(at)foxmail.com] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [203.205.251.73 listed in wl.mailspike.net] 0.4 RDNS_DYNAMIC Delivered to internal network by host with dynamic-looking rDNS 3.2 HELO_DYNAMIC_IPADDR Relay HELO'd using suspicious hostname (IP addr 1) X-BeenThere: opensbi@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "opensbi" Errors-To: opensbi-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org The current mlevel imsic check is only for the platform, which may cause hart without imsic in the platform to trigger an illegal instruction exception when initializing imsic. For example, the platform contains a management hart that only supports wired interrupts. This patch will check whether each hart has imsic according to fdt and record it in a bitmap, and only allow harts with imsic to initialize imsic to avoid triggering illegal instruction exceptions. Signed-off-by: Cheng Yang --- Changes V2 -> V3: - Trailing whitespace. - Replace platform.hart_index2id to generic_hart_index2id. Changes V1 -> V2: - Add the processing of plat->hart_index2id be NULL in fdt_check_imsic_mlevel. include/sbi/sbi_platform.h | 8 ++++-- include/sbi_utils/fdt/fdt_helper.h | 4 ++- lib/sbi/sbi_init.c | 2 +- lib/utils/fdt/fdt_helper.c | 45 ++++++++++++++++++++++++------ platform/generic/platform.c | 26 ++++++++++++----- 5 files changed, 64 insertions(+), 21 deletions(-) -- 2.34.1 diff --git a/include/sbi/sbi_platform.h b/include/sbi/sbi_platform.h index 581935a..f62d23d 100644 --- a/include/sbi/sbi_platform.h +++ b/include/sbi/sbi_platform.h @@ -75,7 +75,7 @@ struct sbi_platform_operations { bool (*cold_boot_allowed)(u32 hartid); /* Platform nascent initialization */ - int (*nascent_init)(void); + int (*nascent_init)(u32 hartid); /** Platform early initialization */ int (*early_init)(bool cold_boot); @@ -395,10 +395,12 @@ static inline bool sbi_platform_cold_boot_allowed( * * @return 0 on success and negative error code on failure */ -static inline int sbi_platform_nascent_init(const struct sbi_platform *plat) +static inline int sbi_platform_nascent_init( + const struct sbi_platform *plat, + u32 hartid) { if (plat && sbi_platform_ops(plat)->nascent_init) - return sbi_platform_ops(plat)->nascent_init(); + return sbi_platform_ops(plat)->nascent_init(hartid); return 0; } diff --git a/include/sbi_utils/fdt/fdt_helper.h b/include/sbi_utils/fdt/fdt_helper.h index ab4a80f..1b7fdeb 100644 --- a/include/sbi_utils/fdt/fdt_helper.h +++ b/include/sbi_utils/fdt/fdt_helper.h @@ -12,6 +12,7 @@ #include #include +#include struct fdt_match { const char *compatible; @@ -89,7 +90,8 @@ int fdt_parse_aplic_node(void *fdt, int nodeoff, struct aplic_data *aplic); struct imsic_data; -bool fdt_check_imsic_mlevel(void *fdt); +int fdt_check_imsic_mlevel(void *fdt, struct sbi_platform *plat, + unsigned long *imsic_bitmap); int fdt_parse_imsic_node(void *fdt, int nodeoff, struct imsic_data *imsic); diff --git a/lib/sbi/sbi_init.c b/lib/sbi/sbi_init.c index 389172a..96f71a4 100644 --- a/lib/sbi/sbi_init.c +++ b/lib/sbi/sbi_init.c @@ -545,7 +545,7 @@ void __noreturn sbi_init(struct sbi_scratch *scratch) * that platform can initialize platform specific per-HART CSRs * or per-HART devices. */ - if (sbi_platform_nascent_init(plat)) + if (sbi_platform_nascent_init(plat, hartid)) sbi_hart_hang(); if (coldboot) diff --git a/lib/utils/fdt/fdt_helper.c b/lib/utils/fdt/fdt_helper.c index a0e93b9..ae5d9cc 100644 --- a/lib/utils/fdt/fdt_helper.c +++ b/lib/utils/fdt/fdt_helper.c @@ -764,27 +764,54 @@ skip_delegate_parse: return 0; } -bool fdt_check_imsic_mlevel(void *fdt) +int fdt_check_imsic_mlevel(void *fdt, struct sbi_platform *plat, unsigned long *imsic_bitmap) { const fdt32_t *val; - int i, len, noff = 0; + int i, h, len, err, cpu_offset, cpu_intc_offset, noff = 0; + u32 phandle, hwirq, hartid; + + bitmap_zero(imsic_bitmap, SBI_HARTMASK_MAX_BITS); if (!fdt) - return false; + return SBI_ENODEV; while ((noff = fdt_node_offset_by_compatible(fdt, noff, "riscv,imsics")) >= 0) { val = fdt_getprop(fdt, noff, "interrupts-extended", &len); - if (val && len > sizeof(fdt32_t)) { - len = len / sizeof(fdt32_t); - for (i = 0; i < len; i += 2) { - if (fdt32_to_cpu(val[i + 1]) == IRQ_M_EXT) - return true; + if (!val || len < sizeof(fdt32_t)) + continue; + + len = len / sizeof(fdt32_t); + for (i = 0; i < len; i += 2) { + phandle = fdt32_to_cpu(val[i]); + hwirq = fdt32_to_cpu(val[i + 1]); + + if (hwirq != IRQ_M_EXT) + continue; + + cpu_intc_offset = fdt_node_offset_by_phandle(fdt, phandle); + if (cpu_intc_offset < 0) + continue; + + cpu_offset = fdt_parent_offset(fdt, cpu_intc_offset); + if (cpu_offset < 0) + continue; + + err = fdt_parse_hart_id(fdt, cpu_offset, &hartid); + if (err) + return SBI_EINVAL; + + for (int i = 0; i < plat->hart_count; i++) { + h = plat->hart_index2id ? plat->hart_index2id[i] : i; + if (hartid == h) { + bitmap_set(imsic_bitmap, i, 1); + break; + } } } } - return false; + return SBI_OK; } int fdt_parse_imsic_node(void *fdt, int nodeoff, struct imsic_data *imsic) diff --git a/platform/generic/platform.c b/platform/generic/platform.c index 1f46b76..00f8f86 100644 --- a/platform/generic/platform.c +++ b/platform/generic/platform.c @@ -70,15 +70,15 @@ static u32 fw_platform_calculate_heap_size(u32 hart_count) } extern struct sbi_platform platform; -static bool platform_has_mlevel_imsic = false; static u32 generic_hart_index2id[SBI_HARTMASK_MAX_BITS] = { 0 }; static DECLARE_BITMAP(generic_coldboot_harts, SBI_HARTMASK_MAX_BITS); +static DECLARE_BITMAP(generic_hart_has_mlevel_imsic, SBI_HARTMASK_MAX_BITS); /* - * The fw_platform_coldboot_harts_init() function is called by fw_platform_init() + * The fw_platform_coldboot_harts_init() function is called by fw_platform_init() * function to initialize the cold boot harts allowed by the generic platform - * according to the DT property "cold-boot-harts" in "/chosen/opensbi-config" + * according to the DT property "cold-boot-harts" in "/chosen/opensbi-config" * DT node. If there is no "cold-boot-harts" in DT, all harts will be allowed. */ static void fw_platform_coldboot_harts_init(void *fdt) @@ -186,7 +186,11 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, platform.hart_count = hart_count; platform.heap_size = fw_platform_calculate_heap_size(hart_count); - platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt); + + rc = fdt_check_imsic_mlevel(fdt, &platform, + generic_hart_has_mlevel_imsic); + if (rc) + goto fail; fw_platform_coldboot_harts_init(fdt); @@ -212,10 +216,18 @@ static bool generic_cold_boot_allowed(u32 hartid) return false; } -static int generic_nascent_init(void) +static int generic_nascent_init(u32 hartid) { - if (platform_has_mlevel_imsic) - imsic_local_irqchip_init(); + for (int i = 0; i < platform.hart_count; i++) { + if (hartid != generic_hart_index2id[i]) + continue; + + if (bitmap_test(generic_hart_has_mlevel_imsic, i)) { + imsic_local_irqchip_init(); + break; + } + } + return 0; }