xen/riscv: support riscv,isa-extensions devicetree property
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 9 lines · Cristian Andrei
Xen only parses the riscv,isa devicetree property, which Linux has deprecated in favour of riscv,isa-base plus riscv,isa-extensions [1]. Xen panics if no CPU node has riscv,isa, so a devicetree using only the new properties cannot boot Xen.
Use riscv,isa-extensions when present, falling back to the legacy riscv,isa otherwise. With the new binding, riscv,isa-extensions and riscv,isa-base must both be present, and riscv,isa-base must match the base ISA Xen is built for ("rv64i" or "rv32i").
I understand why you panic in this case, as both properties should be present according to the bindings. However, can't we make the behavior a bit more lenient and avoid panicking in this case? Perhaps we could fall back to riscv,isa if it is present.
Quoted 52 lines
Add a shared helper,
riscv_isa_ext_probe(), so thatcpufeature.candsmpboot.cagree on which property a CPU uses.Currently, a CPU with neither property is skipped with a message. Make this an error instead, as a skipped CPU is left out of the ISA intersection and Xen could then use extensions that CPU lacks.
CPU nodes for which
dt_get_cpuid_from_node()fails, i.e. without a valid reg property, are still skipped: without a hart ID, Xen can never start that hart and it never runs Xen code, so leaving it out of the ISA intersection is harmless. The exception is the boot hart, which is already running Xen; that case isn't detected yet. Add a comment explaining this and a debug message when such a node is skipped.Add a per-CPU fallback so that any mix of new and legacy CPUs works. This deviates from Linux, which skips CPUs without riscv,isa-extensions and could assume extensions those CPUs lack.
Track the first CPU with a flag instead of testing whether the intersection is empty: with riscv,isa-extensions a CPU's set can be empty, which would make a later CPU look like the first one. This can't happen with the legacy riscv,isa, where 'i' is always set.
dt_processor_hartid(), used during interrupt-controller setup, also only reads riscv,isa. Make it use the new property too, with the same legacy fallback.[1] Linux commit
aeb71e42caae("dt-bindings: riscv: deprecate riscv,isa")Suggested-by Oleksii Kurochko <[email protected]>Signed-off-by Cristian Andrei <[email protected]>---
Changes in v2:
- Reword the commit message: describe the current behaviour first and cite the Linux deprecation of riscv,isa (Oleksii, Baptiste).
- Suggested-by: Oleksii, who suggested the task (Denis).
- Renameriscv_isa_ext_check()toriscv_isa_ext_probe()(Baptiste).
- Document the return values ofriscv_isa_ext_probe()(Oleksii).
-riscv_isa_ext_probe(): use early returns, look up the node name only for error messages, addXENLOG_ERRto its messages (Oleksii).
- Keep the original "riscv,isa must be lowercase" panic message (Oleksii).
- Use switch ( ret ) inriscv_fill_hwcap_from_dt()and read riscv,isa in the-ENOENTcase (Oleksii).
- Don't split the "Unable to find" format string (Baptiste).
- Drop the parentheses around isa[i] != '\0' (Baptiste).
- Reword the final panic message (Oleksii).
- Fix "no any guarantee" in the SSTC comment (Baptiste).
-dt_processor_hartid(): drop the goto label and theXENLOG_DEBUGfallback message (Oleksii).
Quoted 8 lines
-
dt_processor_hartid(): addXENLOG_ERRto its messages and don't split the format strings (Baptiste).
- Sort the <asm/...> includes insmpboot.c(Baptiste).
- booting.txt: say that riscv,isa-extensions takes precedence over riscv,isa when both are present (Oleksii).
- Explain in a comment why CPU nodes without a hart ID are still skipped, and print a debug message when one is (Oleksii, Denis).
- Drop Denis's Reviewed-by, as the patch changed significantly.
I think that geneally it isn't really needed to write whom comment you resolved. IMO, it is more then enough just to mention what was changed.
Quoted 74 lines
Tested on QEMU (virt, aia=aplic-imsic, -cpu rv64,svpbmt=on), debug build:
- QEMU's own DT, 1 and 2 CPUs: boots
- only riscv,isa-extensions, 1 and 2 CPUs: boots
- only the legacy riscv,isa, 2 CPUs: boots
- mixed (CPU 0 new, CPU 1 legacy), 2 CPUs: boots
- CPU 1 with riscv,isa-base "rv32i": panics with the reason, as expected
- CPU 1 with only one of riscv,isa-extensions and riscv,isa-base (each way): panics with the reason, as expected
- CPU node without reg: cannot be tested in QEMU, as OpenSBI stops before Xen startsCHANGELOG.md | 3 + docs/misc/riscv/booting.txt | 4 +- xen/arch/riscv/cpufeature.c | 155 +++++++++++++++--------- xen/arch/riscv/include/asm/cpufeature.h | 2 + xen/arch/riscv/smpboot.c | 23 +++- 5 files changed, 129 insertions(+), 58 deletions(-)CHANGELOG.md +3
-0@@ -17,6 +17,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) afterwards. ### Added + - On RISC-V: + - Support for riscv,isa-extensions devicetree cpu property, alongside + the legacy riscv,isa. ### Removed - On Arm:docs/misc/riscv/booting.txt +3
-1@@ -2,7 +2,9 @@ System requirements =================== The following extensions are required by Xen. Xen panics at boot if any of -them is missing from the riscv,isa property of the CPU nodes: +them is missing from the riscv,isa or riscv,isa-extensions property of the +CPU nodes, with the riscv,isa-extensions and riscv,isa-base pair taking +precedence over the legacy riscv,isa when both are present: - I, M, A, Zicsr, Zifencei: Base ISA Xen is compiled for. - C:xen/arch/riscv/cpufeature.c +5
-4@@ -42,10 +42,11 @@ struct riscv_isa_ext_data {* * These flags express Xen's policy, not the ISA's rules: extensions which * are architecturally tied to one XLEN (Zilsd on RV32, say) need no special -* treatment here, as they can only ever appear in the "riscv,isa" of a host -* of that XLEN, and guest_isa is masked against the host ISA bitmap anyway. -* They are only of use for extensions Xen chooses not to expose to guests of -* a given width despite the hardware implementing them. +* treatment here, as they can only ever appear in the "riscv,isa" or +* "riscv,isa-extensions" of a host of that XLEN, and guest_isa is masked +* against the host ISA bitmap anyway. They are only of use for extensions +* Xen chooses not to expose to guests of a given width despite the hardware +* implementing them. */ #define RISCV_ISA_EXT_GUEST_NONE 0#define RISCV_ISA_EXT_GUEST_RV32 (1U << 0)@@ -455,10 +456,47 @@ static int __init riscv_isa_parse_string(const char *isa,return -EINVAL ; }
xen/arch/riscv/cpufeature.c static int __init riscv_isa_parse_string(const char *isa, · line 467
Quoted 10 lines
xen/arch/riscv/cpufeature.c +9
-1-static void __init riscv_fill_hwcap_from_isa_string (void ) +/* +* Checks the state of the riscv,isa-extensions / riscv,isa-base properties +* in the DT. +* 0: use riscv,isa-extensions; +* -ENOENT: new binding absent, use legacy riscv,isa; +* -EINVAL: if only one of the pair is present or if riscv,isa-base doesn't +* match the build +*/ +int riscv_isa_ext_probe (const struct dt_device_node *cpu)
I think this function should be __init as it I expect it will be used only on boot time.
Quoted 110 lines
xen/arch/riscv/cpufeature.c +55
-30+{ +const char *base; +const char *expected_base =IS_ENABLED (CONFIG_RISCV_64 ) ?"rv64i" :"rv32i" ; +bool isa_ext_exists =dt_property_read_bool (cpu,"riscv,isa-extensions" ); +bool isa_base_exists = !dt_property_read_string (cpu,"riscv,isa-base" , + &base); + +if ( !isa_ext_exists && !isa_base_exists ) +return -ENOENT ; + +if ( !isa_ext_exists || !isa_base_exists ) + { +printk (XENLOG_ERR "%s: need both riscv,isa-extensions and riscv,isa-base\n" , +dt_node_full_name (cpu)); +return -EINVAL ; + } + +if (strcmp (base, expected_base) ) + { +printk (XENLOG_ERR "%s: riscv,isa-base \"%s\" doesn't match %s\n" , +dt_node_full_name (cpu), base, expected_base); +return -EINVAL ; + } + +return 0 ; +} + +static void __init riscv_fill_hwcap_from_dt (void ) {const struct dt_device_node *cpus =dt_find_node_by_path ("/cpus" );const struct dt_device_node *cpu; +bool first_cpu = true;if ( !cpus ) {@@ -471,6 +509,7 @@ static void __init riscv_fill_hwcap_from_isa_string(void)DECLARE_BITMAP (this_isa,RISCV_ISA_EXT_MAX );const char *isa;unsigned long cpuid; +int ret;bitmap_zero (this_isa,RISCV_ISA_EXT_MAX );@@ -480,58 +519,67 @@ static void __init riscv_fill_hwcap_from_isa_string(void)if ( !dt_device_is_available (cpu) )continue ; +/* +* Without a valid reg property there is no hart ID, so Xen can never +* start this hart and it never runs Xen code. Leaving it out of the +* ISA intersection is therefore harmless. The exception is the boot +* hart, which is already running Xen. That case isn't detected yet. +*/ if (dt_get_cpuid_from_node (cpu, &cpuid) <0 ) -continue ; - -if (dt_property_read_string (cpu,"riscv,isa" , &isa) ) { -printk ("Unable to find \"riscv,isa\" devicetree entry " -"for DT's cpu%ld node\n" , cpuid); +printk (XENLOG_DEBUG "%s: no hart ID, skipping this CPU node\n" , +dt_node_full_name (cpu));continue ; } -for (unsigned int i =0 ; (isa[i] !='\0' ); i++ ) -if ( !isdigit (isa[i]) && (isa[i] !='_' ) && !islower (isa[i]) ) -panic ("According to DT binding riscv,isa must be lowercase\n" ); - -if (riscv_isa_parse_string (isa, this_isa) ) -panic ("Check riscv,isa in dts file\n" ); - -if (bitmap_empty (riscv_isa,RISCV_ISA_EXT_MAX ) ) -bitmap_copy (riscv_isa, this_isa,RISCV_ISA_EXT_MAX ); -else -bitmap_and (riscv_isa, riscv_isa, this_isa,RISCV_ISA_EXT_MAX ); - } -} + ret =riscv_isa_ext_probe (cpu); -static bool __init has_isa_extensions_property (void ) -{ -const struct dt_device_node *cpus =dt_find_node_by_path ("/cpus" ); -const struct dt_device_node *cpu; +/* Try riscv,isa-extensions, fall back to the deprecated riscv,isa */ +switch ( ret ) + { +case 0 : +for (unsigned int i =0 ; i <ARRAY_SIZE (riscv_isa_ext); i++ ) +if (dt_property_match_string (cpu,"riscv,isa-extensions" , + riscv_isa_ext[i].name) >=0 ) +__set_bit (riscv_isa_ext[i].id, this_isa); +break ; -if ( !cpus ) - { -printk ("Missing /cpus node in the device tree?\n" ); -return false; - } +case -ENOENT : +if (dt_property_read_string (cpu,"riscv,isa" , &isa) ) +panic ("Unable to find \"riscv,isa\" or \"riscv,isa-extensions\" devicetree entry for DT's cpu%lu node\n" , + cpuid); -dt_for_each_child_node (cpus, cpu) - { -const char *isa; +for (unsigned int i =0 ; isa[i] !='\0' ; i++ ) +if ( !isdigit (isa[i]) && (isa[i] !='_' ) && !islower (isa[i]) ) +panic ("According to DT binding riscv,isa must be lowercase\n" );
xen/arch/riscv/cpufeature.c static void __init riscv_fill_hwcap_from_isa_string(void) · line 560
Quoted 10 lines
xen/arch/riscv/cpufeature.c +4
-4-if ( !dt_device_type_is_equal (cpu,"cpu" ) ) -continue ; +if (riscv_isa_parse_string (isa, this_isa) ) +panic ("cpu%lu: riscv,isa failed to parse \"%s\"\n" , + cpuid, isa); -if ( !dt_device_is_available (cpu) ) -continue ; +break ;
Please add a blank line before "case -EINVAL:", as you do between the other cases.
xen/arch/riscv/cpufeature.c static void __init riscv_fill_hwcap_from_isa_string(void) · line 563
Quoted 3 lines
xen/arch/riscv/cpufeature.c +3
-0+case -EINVAL : +panic ("cpu%lu: invalid riscv,isa-base/riscv,isa-extensions\n" , + cpuid);
riscv_isa_ext_probe() and the new "no hart ID" message already use dt_node_full_name(cpu), so I'd use the node name in these messages too for consistency.
For -EINVAL, riscv_isa_ext_probe() has already printed the exact reason, so the panic() message can be shorter, e.g. just the node name.
xen/arch/riscv/cpufeature.c static void __init riscv_fill_hwcap_from_isa_string(void) · line 564
Quoted 1 line
xen/arch/riscv/cpufeature.c +1
-0+break ;
I am okay to have break here but it looks like it is uncreachable because of panic().
[...]
Quoted 30 lines
xen/arch/riscv/smpboot.c +15
-0@@ -6,6 +6,7 @@#include <xen/sections.h> #include <xen/types.h> +#include <asm/cpufeature.h> #include <asm/current.h> cpumask_t __read_mostly cpu_online_map;@@ -77,9 +78,29 @@ int dt_processor_hartid(const struct dt_device_node *node,return -ENODEV ; } + ret =riscv_isa_ext_probe (node); +if ( ret != -ENOENT ) + { +if ( ret ) +return -ENODEV ; + +if (dt_property_match_string (node,"riscv,isa-extensions" ,"i" ) <0 || +dt_property_match_string (node,"riscv,isa-extensions" ,"m" ) <0 || +dt_property_match_string (node,"riscv,isa-extensions" ,"a" ) <0 ) + { +printk (XENLOG_ERR "CPU with hartid=%#lx does not support ima\n" , + *hartid); +return -ENODEV ; + }
xen/arch/riscv/smpboot.c int dt_processor_hartid(const struct dt_device_node *node, · line 103
Quoted 10 lines
xen/arch/riscv/smpboot.c +7
-1+ +return 0 ; + } + +/* Fall back to legacy riscv,isa property */ if ( (ret =dt_property_read_string (node,"riscv,isa" , &isa)) ) { -printk ("CPU with hartid=%#lx has no \"riscv,isa\" property\n" , *hartid); +printk (XENLOG_ERR "CPU with hartid=%#lx has no \"riscv,isa-base\" or \"riscv,isa\" property\n" , + *hartid);
We only get here with -ENOENT, i.e. when both riscv,isa-extensions and riscv,isa-base are missing. So I'd say something like:
"... has neither riscv,isa-extensions/riscv,isa-base nor riscv,isa"
xen/arch/riscv/smpboot.c int dt_processor_hartid(const struct dt_device_node *node, · line 106
Quoted 3 lines
xen/arch/riscv/smpboot.c +0
-0return ret; }
~ Oleksii