lib: sbi: Only register available extensions
authorAndrew Jones <ajones@ventanamicro.com>
Mon, 15 May 2023 11:12:37 +0000 (13:12 +0200)
committerAnup Patel <anup@brainfault.org>
Sun, 21 May 2023 11:23:02 +0000 (16:53 +0530)
When an extension implements a probe function it means there's a
chance that the extension is not available. Use this function in the
register_extensions callback to determine if the extension should be
registered at all. Where the probe implementation is simple, just
open code the check.

Signed-off-by: Andrew Jones <ajones@ventanamicro.com>
Reviewed-by: Anup Patel <anup@brainfault.org>
lib/sbi/sbi_ecall_cppc.c
lib/sbi/sbi_ecall_dbcn.c
lib/sbi/sbi_ecall_srst.c
lib/sbi/sbi_ecall_susp.c
lib/sbi/sbi_ecall_vendor.c
lib/sbi/sbi_init.c

index 42ec744..a6398ac 100644 (file)
@@ -59,6 +59,9 @@ struct sbi_ecall_extension ecall_cppc;
 
 static int sbi_ecall_cppc_register_extensions(void)
 {
+       if (!sbi_cppc_get_device())
+               return 0;
+
        return sbi_ecall_register_extension(&ecall_cppc);
 }
 
index 58b19e4..cbb2e80 100644 (file)
@@ -68,6 +68,9 @@ struct sbi_ecall_extension ecall_dbcn;
 
 static int sbi_ecall_dbcn_register_extensions(void)
 {
+       if (!sbi_console_get_device())
+               return 0;
+
        return sbi_ecall_register_extension(&ecall_dbcn);
 }
 
index ad31537..ea0dc73 100644 (file)
@@ -71,6 +71,12 @@ struct sbi_ecall_extension ecall_srst;
 
 static int sbi_ecall_srst_register_extensions(void)
 {
+       unsigned long out_val;
+
+       sbi_ecall_srst_probe(SBI_EXT_SRST, &out_val);
+       if (!out_val)
+               return 0;
+
        return sbi_ecall_register_extension(&ecall_srst);
 }
 
index bfbdbe6..c412404 100644 (file)
@@ -44,6 +44,12 @@ struct sbi_ecall_extension ecall_susp;
 
 static int sbi_ecall_susp_register_extensions(void)
 {
+       unsigned long out_val;
+
+       sbi_ecall_susp_probe(SBI_EXT_SUSP, &out_val);
+       if (!out_val)
+               return 0;
+
        return sbi_ecall_register_extension(&ecall_susp);
 }
 
index 39c58c8..700f475 100644 (file)
@@ -22,24 +22,11 @@ static inline unsigned long sbi_ecall_vendor_id(void)
                 (SBI_EXT_VENDOR_END - SBI_EXT_VENDOR_START));
 }
 
-static int sbi_ecall_vendor_probe(unsigned long extid,
-                                 unsigned long *out_val)
-{
-       if (!sbi_platform_vendor_ext_check(sbi_platform_thishart_ptr()))
-               *out_val = 0;
-       else
-               *out_val = 1;
-       return 0;
-}
-
 static int sbi_ecall_vendor_handler(unsigned long extid, unsigned long funcid,
                                    const struct sbi_trap_regs *regs,
                                    unsigned long *out_val,
                                    struct sbi_trap_info *out_trap)
 {
-       if (!sbi_platform_vendor_ext_check(sbi_platform_thishart_ptr()))
-               return SBI_ERR_NOT_SUPPORTED;
-
        return sbi_platform_vendor_ext_provider(sbi_platform_thishart_ptr(),
                                                funcid, regs,
                                                out_val, out_trap);
@@ -51,6 +38,9 @@ static int sbi_ecall_vendor_register_extensions(void)
 {
        unsigned long extid = sbi_ecall_vendor_id();
 
+       if (!sbi_platform_vendor_ext_check(sbi_platform_thishart_ptr()))
+               return 0;
+
        ecall_vendor.extid_start = extid;
        ecall_vendor.extid_end = extid;
 
@@ -61,6 +51,5 @@ struct sbi_ecall_extension ecall_vendor = {
        .extid_start            = SBI_EXT_VENDOR_START,
        .extid_end              = SBI_EXT_VENDOR_END,
        .register_extensions    = sbi_ecall_vendor_register_extensions,
-       .probe                  = sbi_ecall_vendor_probe,
        .handle                 = sbi_ecall_vendor_handler,
 };
index 539f824..7c78d9b 100644 (file)
@@ -323,12 +323,6 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
                sbi_hart_hang();
        }
 
-       rc = sbi_ecall_init();
-       if (rc) {
-               sbi_printf("%s: ecall init failed (error %d)\n", __func__, rc);
-               sbi_hart_hang();
-       }
-
        /*
         * Note: Finalize domains after HSM initialization so that we
         * can startup non-root domains.
@@ -350,8 +344,9 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
        }
 
        /*
-        * Note: Platform final initialization should be last so that
-        * it sees correct domain assignment and PMP configuration.
+        * Note: Platform final initialization should be after finalizing
+        * domains so that it sees correct domain assignment and PMP
+        * configuration for FDT fixups.
         */
        rc = sbi_platform_final_init(plat, true);
        if (rc) {
@@ -360,6 +355,17 @@ static void __noreturn init_coldboot(struct sbi_scratch *scratch, u32 hartid)
                sbi_hart_hang();
        }
 
+       /*
+        * Note: Ecall initialization should be after platform final
+        * initialization so that all available platform devices are
+        * already registered.
+        */
+       rc = sbi_ecall_init();
+       if (rc) {
+               sbi_printf("%s: ecall init failed (error %d)\n", __func__, rc);
+               sbi_hart_hang();
+       }
+
        sbi_boot_print_general(scratch);
 
        sbi_boot_print_domains(scratch);