From patchwork Thu Mar 28 06:39:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cheng Yang X-Patchwork-Id: 1917169 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=i9vYCXU9; 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=bcq+RayZ; 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 4V4v6s6nxSz1yWr for ; Thu, 28 Mar 2024 17:40:21 +1100 (AEDT) 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=DPlUP55FGU1aaJnjFsPWmtRPlvhPaqJSHXD9LFswICQ=; b=i9vYCXU9xL9QSz NX6LZ+A6hsrytSqqYBjKcyPPi+/6k8+gFEScFhIQyZiUCyN9qyV9JYI3Xn8s//Kt2gZ86WdqS0Th4 s/M3KIY7moIQ5PHjvl4WsZneN4kBeUY+bLqR2pnpLpaR7PUgK+IKnT2Gcpc3N5QjQ13RB8muUUeZW BltJRMp4P7ojlIKtPwUgSXQZnNxESKObLRAjEKyh+rnooRDOMCjxCZoW8h+v1cqzZQxN/oROG2Zfc dLNqkwW1VItAin8JtAYBAtDKRlgudG74P9cThuCAOUMKo0Nb1PKYR+iohyp3cRt61MwXNCDM9R2BS 5Czw5Wtnh4swF+Lfrr6A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rpjQq-0000000CkK7-0mHj; Thu, 28 Mar 2024 06:40:12 +0000 Received: from out203-205-221-242.mail.qq.com ([203.205.221.242]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rpjQn-0000000CkIY-2Sp2 for opensbi@lists.infradead.org; Thu, 28 Mar 2024 06:40:11 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1711607995; bh=6csXIWi+SG7fVS30Pq4feD0qyQBf0r5+plEbhvC8SM0=; h=From:To:Cc:Subject:Date; b=bcq+RayZr5xnUMsLidddBLQgOYEpB1lTciyyMgGmJpEoGGDYBtpUkJsBjsYO7/q8X 5U/UqP7w0DMOGAieBF8J+vkp2uRPQU4OuNj/WlWCB5Bo8pyuWbLMCdEPhIz/YxzsJ1 FkmZkLXJQ3YQX9gTS4B9geSOSxJZOogZuACCyXbs= Received: from yc-home.taila82bd.ts.net ([2409:8a62:e78:44c0:af8e:fa83:7421:88f9]) by newxmesmtplogicsvrsza10-0.qq.com (NewEsmtp) with SMTP id 9F63B651; Thu, 28 Mar 2024 14:39:54 +0800 X-QQ-mid: xmsmtpt1711607994tljqnbk9r Message-ID: X-QQ-XMAILINFO: MRw/zKT/0BpPVQTXcxb/JLArx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ecHsW4FXQ 1ahrMhjCI6aLNwx5D+9R+Hw221sw== X-QQ-XMRINFO: OD9hHCdaPRBwq3WW+NvGbIU= From: Cheng Yang To: opensbi@lists.infradead.org Cc: wxjstz@126.com, Cheng Yang Subject: [PATCH v2 1/1] lib:sbi:platform:generic: Improved mlevel imsic check and init. Date: Thu, 28 Mar 2024 14:39:51 +0800 X-OQ-MSGID: <20240328063951.982418-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-20240327_234010_027848_6E9F9579 X-CRM114-Status: GOOD ( 19.03 ) 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 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [203.205.221.242 listed in list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -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.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [yangcheng.work(at)foxmail.com] 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 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 | 22 +++++++++++---- 5 files changed, 62 insertions(+), 19 deletions(-) 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 796cccc..8320f94 100644 --- a/lib/sbi/sbi_init.c +++ b/lib/sbi/sbi_init.c @@ -591,7 +591,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..015e01d 100644 --- a/platform/generic/platform.c +++ b/platform/generic/platform.c @@ -70,10 +70,10 @@ 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() @@ -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 != platform.hart_index2id[i]) + continue; + + if (bitmap_test(generic_hart_has_mlevel_imsic, i)) { + imsic_local_irqchip_init(); + break; + } + } + return 0; }