riscv: sbi: Add IPI extension tests.
Subsystems (3)
PATCH 1/1 riscv: sbi: Add IPI extension tests.
Add tests for the RISC-V OpenSBI inter-processor interrupt extension.
--- lib/riscv/asm/sbi.h | 5 ++ riscv/sbi.c | 138 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+)
lib/riscv/asm/sbi.h +5 -0
@@ -20,6 +20,7 @@ enum sbi_ext_id {SBI_EXT_HSM =0x48534d ,SBI_EXT_SRST =0x53525354 ,SBI_EXT_DBCN =0x4442434E , + SBI_EXT_IPI = 0x735049, }; enum sbi_ext_base_fid {
@@ -49,6 +50,10 @@ enum sbi_ext_dbcn_fid { SBI_EXT_DBCN_CONSOLE_WRITE_BYTE, }; +enum sbi_ext_ipi_fid { +SBI_EXT_IPI_SEND =0 , +}; +struct sbiret {long error;long value;
riscv/sbi.c +138 -0
173 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 13 lines · Cade Richard
Add tests for the RISC-V OpenSBI inter-processor interrupt extension.
Signed-off-by Cade Richard <[email protected]>--- lib/riscv/asm/sbi.h | 5 ++ riscv/sbi.c | 138 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 143 insertions(+)lib/riscv/asm/sbi.h +0
-0
lib/riscv/asm/sbi.h enum sbi_ext_id { · line 23
Quoted 4 lines
lib/riscv/asm/sbi.h +1
-0@@ -20,6 +20,7 @@ enum sbi_ext_id {SBI_EXT_HSM =0x48534d ,SBI_EXT_SRST =0x53525354 ,SBI_EXT_DBCN =0x4442434E , + SBI_EXT_IPI = 0x735049,
We're trying to list these in spec chapter order, so IPI should come before HSM.
Quoted 44 lines
lib/riscv/asm/sbi.h +4
-0};enum sbi_ext_base_fid {@@ -49,6 +50,10 @@ enum sbi_ext_dbcn_fid { SBI_EXT_DBCN_CONSOLE_WRITE_BYTE, }; +enum sbi_ext_ipi_fid { +SBI_EXT_IPI_SEND =0 , +}; +struct sbiret {long error;long value;riscv/sbi.c +6
-0@@ -6,11 +6,14 @@*/ #include <libcflat.h> #include <alloc_page.h> +#include <cpumask.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <vmalloc.h> #include <memregions.h> +#include <on-cpus.h> +#include <rand.h> #include <asm/barrier.h> #include <asm/csr.h> #include <asm/delay.h> @@ -23,6 +26,9 @@#include <asm/timer.h> #define HIGH_ADDR_BOUNDARY ((phys_addr_t)1 << 32) +static prng_state ps; +static bool ipi_received[__riscv_xlen]; +static bool ipi_timeout[__riscv_xlen];static void help (void ) {
riscv/sbi.c static struct sbiret __dbcn_sbi_ecall(int fid, unsigned long arg0, unsigned long · line 54
Quoted 4 lines
riscv/sbi.c +1
-0@@ -45,6 +51,11 @@ static struct sbiret __dbcn_sbi_ecall(int fid, unsigned long arg0, unsigned longreturn sbi_ecall (SBI_EXT_DBCN , fid, arg0, arg1, arg2,0 ,0 ,0 ); } +static struct sbiret __ipi_sbi_ecall (unsigned long arg0,unsigned long arg1)
This function is specific to IPI so we should use specific parameter
names: hart_mask, hart_mask_base
riscv/sbi.c static void check_dbcn(void) · line 434
Quoted 12 lines
riscv/sbi.c +5
-0+{ +return sbi_ecall (SBI_EXT_IPI ,SBI_EXT_IPI_SEND , arg0, arg1,0 ,0 ,0 ,0 ); +} +static void split_phys_addr (phys_addr_t paddr,unsigned long *hi,unsigned long *lo) { *lo = (unsigned long )paddr;@@ -420,6 +431,132 @@ static void check_dbcn(void)report_prefix_pop (); } +static int rand_online_cpu (prng_state * ps) {
'{' on its own line. Same comment for other functions below.
riscv/sbi.c static void check_dbcn(void) · line 436
Quoted 2 lines
riscv/sbi.c +2
-0+int me =smp_processor_id (); +int num_iters =prng32 (ps);
Do we really want to iterate up to 4294967295 times? I guess you want apply a mod with some max number of iterations to this.
riscv/sbi.c static void check_dbcn(void) · line 437
Quoted 1 line
riscv/sbi.c +1
-0+int rand_cpu =cpumask_next (me, &cpu_online_mask);
If me was the last cpu ID, then rand_cpu will be 'nr_cpus'
riscv/sbi.c static void check_dbcn(void) · line 441
Quoted 4 lines
riscv/sbi.c +4
-0+ +for (int i =0 ; i < num_iters; i++) { + rand_cpu =cpumask_next (me, &cpu_online_mask); + }
This must be incomplete. It does the exact same thing over and over for no reason.
riscv/sbi.c static void check_dbcn(void) · line 449
Quoted 8 lines
riscv/sbi.c +8
-0+ +return rand_cpu; +} + +static void ipi_timeout_handler (struct pt_regs *regs) { +int me =smp_processor_id (); + ipi_timeout[me] = true; +report_fail ("ipi timed out on hart %d" , me);
Let's not call stuff like report* from interrupt handlers. Just setting the boolean is enough. We can check that boolean and call report somewhere else which is outside interrupt context.
riscv/sbi.c static void check_dbcn(void) · line 455
Quoted 6 lines
riscv/sbi.c +6
-0+} + +static void ipi_irq_handler (struct pt_regs *regs) { +int me =smp_processor_id (); + ipi_received[me] = true; +report_pass ("ipi received on hart %d" , me);
Same comment as above about no report calls in interrupt context.
riscv/sbi.c static void check_dbcn(void) · line 456
Quoted 1 line
riscv/sbi.c +1
-0+
extra blank line here
riscv/sbi.c static void check_dbcn(void) · line 461
Quoted 5 lines
riscv/sbi.c +5
-0+} + +static void ipi_hart_init (void *irq_func) { +int me =smp_processor_id (); +printf ("Installing IPI IRQ handler on hart %d" , me);
Drop this print message since it's not useful.
riscv/sbi.c static void check_dbcn(void) · line 462
Quoted 1 line
riscv/sbi.c +1
-0+install_irq_handler (IRQ_S_IPI , (void *)ipi_irq_handler);
IRQ_S_IPI is not defined anywhere so this can't compile.
riscv/sbi.c static void check_dbcn(void) · line 467
Quoted 5 lines
riscv/sbi.c +5
-0+install_irq_handler (IRQ_S_TIMER , (void *)ipi_timeout_handler); +timer_irq_enable (); +while (!ipi_received[me] && !ipi_timeout[me]) { +cpu_relax (); + }
I don't think this loop will ever end because I don't see where local_irq_enable() is called.
No {} around single statements.
riscv/sbi.c static void check_dbcn(void) · line 471
Quoted 4 lines
riscv/sbi.c +4
-0+} + +static int offline_cpu (void ) { +for (int i =0 ; i < __riscv_xlen; i++) {
Looking ahead it looks like you want to find a cpu number which isn't valid. You should probably be looking@the cpu_present_mask instead in that case. Also there's no need to try cpus < nr_cpus since those should all be present.
riscv/sbi.c static void check_dbcn(void) · line 481
Quoted 10 lines
riscv/sbi.c +10
-0+if (!cpumask_test_cpu (i, &cpu_online_mask)) { +return i; + } + } +return -1 ; +} + +static void print_bits (size_t const size,void const *const ptr) +{ +unsigned char *b = (unsigned char *) ptr;
^ no space here
riscv/sbi.c static void check_dbcn(void) · line 487
Quoted 6 lines
riscv/sbi.c +6
-0+unsigned char byte; +int i, j; + +for (i = size-1 ; i >=0 ; i--) { +for (j =7 ; j >=0 ; j--) { + byte = (b[i] >> j) &1 ;
s/byte/bit/
riscv/sbi.c static void check_dbcn(void) · line 499
Quoted 12 lines
riscv/sbi.c +12
-0+printf ("%u" , byte); + } + } +puts ("" ); +} + +static void set_flags_false (bool arr[]) +{ +for (int i =0 ; i < __riscv_xlen; i++) { + arr[i] =0 ; + } +}
We don't need this function. We have memset().
Quoted 15 lines
riscv/sbi.c +15
-0+ +static void check_ipi (void ) +{ +int cpu =smp_processor_id (); +unsigned long me = (unsigned long )cpu; +struct sbiret ret; + ps =prng_init (0xDEADBEEF ); + +report_prefix_push ("ipi" ); + +if (!sbi_probe (SBI_EXT_IPI )) { +report_skip ("ipi extension not available" ); +report_prefix_pop (); +return ; + }
riscv/sbi.c static void check_dbcn(void) · line 524
Quoted 10 lines
riscv/sbi.c +10
-0+ +if (cpu_has_extension (smp_processor_id (),ISA_SSTC )) { +csr_write (CSR_STIMECMP ,ULONG_MAX ); +if (__riscv_xlen ==32 ) +csr_write (CSR_STIMECMPH ,ULONG_MAX ); + } + +report_prefix_push ("send to one random hart" ); +set_flags_false (ipi_received); +set_flags_false (ipi_timeout);
memset(ipi_received, 0, sizeof(ipi_received));memset(ipi_timeout, 0, sizeof(ipi_timeout));
riscv/sbi.c static void check_dbcn(void) · line 527
Quoted 3 lines
riscv/sbi.c +3
-0+int rand_hartid =rand_online_cpu (&ps); +on_cpu (rand_hartid, (void *)ipi_hart_init,NULL ); +unsigned long ipi_rand_mask =1 << rand_hartid;
If rand_hartid > xlen then this shift will overflow. We use use a cpumask.
We should be setting the timeout here before doing the test below.
riscv/sbi.c static void check_dbcn(void) · line 529
Quoted 2 lines
riscv/sbi.c +2
-0+ + ret =__ipi_sbi_ecall (ipi_rand_mask, me);
rand_hartid is relative to zero, but 'me' doesn't have to be zero, so we can't use 'me' for the base, we need to use zero or, if trying to specify harts with IDs larger than xlen, we need some other multiple of xlen here.
riscv/sbi.c static void check_dbcn(void) · line 537
Quoted 8 lines
riscv/sbi.c +8
-0+report (ret.error ==SBI_SUCCESS ,"send to one randomly chosen hart" ); +report_prefix_pop (); + +report_prefix_push ("broadcast" ); +set_flags_false (ipi_received); +set_flags_false (ipi_timeout); +on_cpus ((void *)ipi_hart_init,NULL ); +unsigned long ipi_broadcast_mask = (unsigned long )(cpumask_bits (&cpu_online_mask)[me]);
I'm not sure what this is trying to do, but since we should be using actual cpumasks instead of unsigned longs called masks, then, if we want a mask of all online harts, just do
cpumask_copy(&ipi_broadcast_mask, &cpu_online_mask);
riscv/sbi.c static void check_dbcn(void) · line 539
Quoted 2 lines
riscv/sbi.c +2
-0+puts ("online cpu mask: " ); +print_bits (CPUMASK_NR_LONGS *sizeof (long ), &ipi_broadcast_mask);
Let's drop print_bits. I'll provide a cpumask_to_list() function which returns a cpumask as cpu-list, e.g. 0,2,3-5,7-9 type of format.
riscv/sbi.c static void check_dbcn(void) · line 542
Quoted 3 lines
riscv/sbi.c +3
-0+puts ("\n" ); + + ret =__ipi_sbi_ecall (ipi_broadcast_mask, me);
This is fine to test broadcasting to a subset of all harts, but we should also have a test using (0, -1) for the parameters. See section 3.1 of the SBI spec.
riscv/sbi.c static void check_dbcn(void) · line 552
Quoted 10 lines
riscv/sbi.c +10
-0+report (ret.error ==SBI_SUCCESS ,"send to all available harts" ); +report_prefix_pop (); + +report_prefix_push ("invalid parameters" ); +unsigned long invalid_hart_mask_base =offline_cpu (); + ret =__ipi_sbi_ecall (ipi_rand_mask, invalid_hart_mask_base); +report (ret.error ==SBI_ERR_INVALID_PARAM ,"hart_mask_base" ); + +unsigned long invalid_cpu_mask =1 << invalid_hart_mask_base; + ret =__ipi_sbi_ecall (invalid_cpu_mask, me);
This may work to construct an invalid mask, if we don't have an xlen multiple of harts available. Another test might be to use a mask of zero with a mask-base that is *not* -1.
Quoted 21 lines
riscv/sbi.c +8
-0+report (ret.error ==SBI_ERR_INVALID_PARAM ,"hart_mask" ); +report_prefix_pop (); + +report_prefix_pop (); +report_prefix_pop (); +} +int main (int argc,char **argv) {if (argc >1 && !strcmp (argv[1 ],"-h" )) {@@ -431,6 +568,7 @@ int main(int argc, char **argv)check_base ();check_time ();check_dbcn (); +check_ipi ();return report_summary (); }Signature · 1 line
2.43.0
Thanks,
drew