lib:sbi:platform:generic: Improved mlevel imsic check and init.
PATCH v2 1/1 lib:sbi:platform:generic: Improved mlevel imsic check and init.
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.
---
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(-)
include/sbi/sbi_platform.h +5 -3
@@ -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 ; }
include/sbi_utils/fdt/fdt_helper.h +3 -1
@@ -12,6 +12,7 @@#include <sbi/sbi_types.h> #include <sbi/sbi_domain.h> +#include <sbi/sbi_platform.h> 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);
lib/sbi/sbi_init.c +1 -1
@@ -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)
lib/utils/fdt/fdt_helper.c +36 -9
63 lines not on this page · raw message
platform/generic/platform.c +17 -5
@@ -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 ; }
Signature · 1 line
2.34.1
PATCH v2 1/1 lib:sbi:platform:generic: Improved mlevel imsic check and init.
? 2024-03-28???? 14:39 +0800?Cheng Yang???
Quoted 174 lines
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 <[email protected]>---
Changes V1 -> V2:
? - Add the processing ofplat->hart_index2idbe NULL infdt_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(-)
include/sbi/sbi_platform.h +0
-0@@ -75,7 +75,7 @@ struct sbi_platform_operations { ? bool (*cold_boot_allowed)(u32 hartid);?
? /* Platform nascent initialization */include/sbi/sbi_platform.h +1
-1-int (*nascent_init)(void ); +int (*nascent_init)(u32 hartid);?
? /** Platform early initialization */
? int (*early_init)(boolcold_boot);include/sbi/sbi_platform.h +0
-0@@ -395,10 +395,12 @@ static inline bool sbi_platform_cold_boot_allowed(? *
? * @return 0 on success and negative error code on failure ? */include/sbi/sbi_platform.h +3
-1-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)include/sbi/sbi_platform.h +1
-1-return sbi_platform_ops (plat)->nascent_init (); +return sbi_platform_ops (plat)->nascent_init (hartid);? return 0;
?}
?include/sbi_utils/fdt/fdt_helper.h +0
-0@@ -12,6 +12,7 @@?
?#include <sbi/sbi_types.h>
?#include <sbi/sbi_domain.h>
+#include <sbi/sbi_platform.h>
?
?structfdt_match{
? const char *compatible;include/sbi_utils/fdt/fdt_helper.h +0
-0@@ -89,7 +90,8 @@ int fdt_parse_aplic_node(void *fdt, int nodeoff, struct aplic_data *aplic);?
?structimsic_data;
?include/sbi_utils/fdt/fdt_helper.h +2
-1-bool fdt_check_imsic_mlevel (void *fdt); +int fdt_check_imsic_mlevel (void *fdt,struct sbi_platform *plat, + ?? unsigned long *imsic_bitmap);?
?intfdt_parse_imsic_node(void *fdt, int nodeoff, struct imsic_data *imsic); ?lib/sbi/sbi_init.c +0
-0@@ -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.
? */lib/sbi/sbi_init.c +1
-1-if (sbi_platform_nascent_init (plat)) +if (sbi_platform_nascent_init (plat, hartid))?
sbi_hart_hang();
?
? if (coldboot)lib/utils/fdt/fdt_helper.c +0
-0@@ -764,27 +764,54 @@ skip_delegate_parse: ? return 0; ?}?
lib/utils/fdt/fdt_helper.c +1
-1-bool fdt_check_imsic_mlevel (void *fdt) +int fdt_check_imsic_mlevel (void *fdt,struct sbi_platform *plat,unsigned long *imsic_bitmap)?{
? constfdt32_t*val;lib/utils/fdt/fdt_helper.c +4
-1-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)lib/utils/fdt/fdt_helper.c +1
-1-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);lib/utils/fdt/fdt_helper.c +29
-5-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 ; + }? }
? }
? }
?lib/utils/fdt/fdt_helper.c +1
-1-return false; +return SBI_OK ;?}
?
?intfdt_parse_imsic_node(void *fdt, int nodeoff, struct imsic_data *imsic)platform/generic/platform.c +0
-0@@ -70,10 +70,10 @@ static u32 fw_platform_calculate_heap_size(u32 hart_count) ?}?
?extern structsbi_platformplatform;
-static boolplatform_has_mlevel_imsic= false;
?static u32 generic_hart_index2id[SBI_HARTMASK_MAX_BITS] = { 0 };
?
?staticDECLARE_BITMAP(generic_coldboot_harts, SBI_HARTMASK_MAX_BITS); +staticDECLARE_BITMAP(generic_hart_has_mlevel_imsic, SBI_HARTMASK_MAX_BITS); ?
?/*
? * Thefw_platform_coldboot_harts_init()function is called byfw_platform_init()platform/generic/platform.c +0
-0@@ -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/generic/platform.c unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, · line 190
Quoted 3 lines
platform/generic/platform.c +2
-1- platform_has_mlevel_imsic =fdt_check_imsic_mlevel (fdt); + + rc = fdt_check_imsic_mlevel(fdt, &platform,
trailing whitespace.
Regards,
Xiang W
Quoted 32 lines
platform/generic/platform.c +3
-0+ generic_hart_has_mlevel_imsic); +if (rc) + goto fail;?
?fw_platform_coldboot_harts_init(fdt);
?platform/generic/platform.c +0
-0@@ -212,10 +216,18 @@ static bool generic_cold_boot_allowed(u32 hartid) ? return false; ?}?
platform/generic/platform.c +1
-1-static int generic_nascent_init (void ) +static int generic_nascent_init (u32 hartid)?{
platform/generic/platform.c +10
-2-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;
?}
?Signature · 1 line
2.34.1
PATCH v2 1/1 lib:sbi:platform:generic: Improved mlevel imsic check and init.
In reply to Cheng Yang? 2024-03-28???? 14:39 +0800?Cheng Yang???
Quoted 190 lines
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 <[email protected]>---
Changes V1 -> V2:
? - Add the processing ofplat->hart_index2idbe NULL infdt_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(-)
include/sbi/sbi_platform.h +0
-0@@ -75,7 +75,7 @@ struct sbi_platform_operations { ? bool (*cold_boot_allowed)(u32 hartid);?
? /* Platform nascent initialization */include/sbi/sbi_platform.h +1
-1-int (*nascent_init)(void ); +int (*nascent_init)(u32 hartid);?
? /** Platform early initialization */
? int (*early_init)(boolcold_boot);include/sbi/sbi_platform.h +0
-0@@ -395,10 +395,12 @@ static inline bool sbi_platform_cold_boot_allowed(? *
? * @return 0 on success and negative error code on failure ? */include/sbi/sbi_platform.h +3
-1-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)include/sbi/sbi_platform.h +1
-1-return sbi_platform_ops (plat)->nascent_init (); +return sbi_platform_ops (plat)->nascent_init (hartid);? return 0;
?}
?include/sbi_utils/fdt/fdt_helper.h +0
-0@@ -12,6 +12,7 @@?
?#include <sbi/sbi_types.h>
?#include <sbi/sbi_domain.h>
+#include <sbi/sbi_platform.h>
?
?structfdt_match{
? const char *compatible;include/sbi_utils/fdt/fdt_helper.h +0
-0@@ -89,7 +90,8 @@ int fdt_parse_aplic_node(void *fdt, int nodeoff, struct aplic_data *aplic);?
?structimsic_data;
?include/sbi_utils/fdt/fdt_helper.h +2
-1-bool fdt_check_imsic_mlevel (void *fdt); +int fdt_check_imsic_mlevel (void *fdt,struct sbi_platform *plat, + ?? unsigned long *imsic_bitmap);?
?intfdt_parse_imsic_node(void *fdt, int nodeoff, struct imsic_data *imsic); ?lib/sbi/sbi_init.c +0
-0@@ -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.
? */lib/sbi/sbi_init.c +1
-1-if (sbi_platform_nascent_init (plat)) +if (sbi_platform_nascent_init (plat, hartid))?
sbi_hart_hang();
?
? if (coldboot)lib/utils/fdt/fdt_helper.c +0
-0@@ -764,27 +764,54 @@ skip_delegate_parse: ? return 0; ?}?
lib/utils/fdt/fdt_helper.c +1
-1-bool fdt_check_imsic_mlevel (void *fdt) +int fdt_check_imsic_mlevel (void *fdt,struct sbi_platform *plat,unsigned long *imsic_bitmap)?{
? constfdt32_t*val;lib/utils/fdt/fdt_helper.c +4
-1-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)lib/utils/fdt/fdt_helper.c +1
-1-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);lib/utils/fdt/fdt_helper.c +29
-5-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 ; + }? }
? }
? }
?lib/utils/fdt/fdt_helper.c +1
-1-return false; +return SBI_OK ;?}
?
?intfdt_parse_imsic_node(void *fdt, int nodeoff, struct imsic_data *imsic)platform/generic/platform.c +0
-0@@ -70,10 +70,10 @@ static u32 fw_platform_calculate_heap_size(u32 hart_count) ?}?
?extern structsbi_platformplatform;
-static boolplatform_has_mlevel_imsic= false;
?static u32 generic_hart_index2id[SBI_HARTMASK_MAX_BITS] = { 0 };
?
?staticDECLARE_BITMAP(generic_coldboot_harts, SBI_HARTMASK_MAX_BITS); +staticDECLARE_BITMAP(generic_hart_has_mlevel_imsic, SBI_HARTMASK_MAX_BITS); ?
?/*
? * Thefw_platform_coldboot_harts_init()function is called byfw_platform_init()platform/generic/platform.c +0
-0@@ -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/generic/platform.c +5
-1- 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);
?platform/generic/platform.c +0
-0@@ -212,10 +216,18 @@ static bool generic_cold_boot_allowed(u32 hartid) ? return false; ?}?
platform/generic/platform.c +1
-1-static int generic_nascent_init (void ) +static int generic_nascent_init (u32 hartid)?{
platform/generic/platform.c static bool generic_cold_boot_allowed(u32 hartid) · line 222
Quoted 4 lines
platform/generic/platform.c +2
-2-if (platform_has_mlevel_imsic) -imsic_local_irqchip_init (); +for (int i =0 ; i < platform.hart_count; i++) { +if (hartid != platform.hart_index2id[i])
s/platform.hart_index2id/generic_hart_index2id/
Regards,
Xiang W
Quoted 11 lines
platform/generic/platform.c +8
-0+continue ; + +if (bitmap_test (generic_hart_has_mlevel_imsic, i)) { +imsic_local_irqchip_init (); +break ; + } + } +? return 0;
?}
?