Skip to contentSkip to conversation

lib: sbi: Align SBI vendor extension id with mvendorid CSR

PATCH New Anup Patel 2 participants 4 messages Feb 13, 2023 – Feb 27, 2023

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

PATCH 1/2 lib: sbi: Align SBI vendor extension id with mvendorid CSR

As-per the SBI specification, the lower 24bits of the SBI vendor extension id is same as lower 24bits of the mvendorid CSR.

We update the SBI vendor extension id checking based on above.

---
 include/sbi/sbi_platform.h                   | 17 ++++++++---------
 lib/sbi/sbi_ecall_vendor.c                   | 19 +++++++++++++++++--
 platform/generic/include/platform_override.h |  1 -
 platform/generic/platform.c                  | 19 ++++++-------------
 4 files changed, 31 insertions(+), 25 deletions(-)
include/sbi/sbi_platform.h +8 -9
index 43d1c03..3a629a6 100644
@@ -126,8 +126,8 @@ struct sbi_platform_operations {
 	/** Exit platform timer for current HART */
 	void (*timer_exit)(void);
 
-	/** platform specific SBI extension implementation probe function */
-	int (*vendor_ext_check)(long extid);
+	/** Check if SBI vendor extension is implemented or not */
+	bool (*vendor_ext_check)(void);
 	/** platform specific SBI extension implementation provider */
 	int (*vendor_ext_provider)(long extid, long funcid,
 				   const struct sbi_trap_regs *regs,
@@ -636,20 +636,19 @@ static inline void sbi_platform_timer_exit(const struct sbi_platform *plat)
 }
 
 /**
- * Check if a vendor extension is implemented or not.
+ * Check if SBI vendor extension is implemented or not.
  *
  * @param plat pointer to struct sbi_platform
- * @param extid	vendor SBI extension id
  *
- * @return 0 if extid is not implemented and 1 if implemented
+ * @return false if not implemented and true if implemented
  */
-static inline int sbi_platform_vendor_ext_check(const struct sbi_platform *plat,
-						long extid)
+static inline bool sbi_platform_vendor_ext_check(
+					const struct sbi_platform *plat)
 {
 	if (plat && sbi_platform_ops(plat)->vendor_ext_check)
-		return sbi_platform_ops(plat)->vendor_ext_check(extid);
+		return sbi_platform_ops(plat)->vendor_ext_check();
 
-	return 0;
+	return false;
 }
 
 /**
lib/sbi/sbi_ecall_vendor.c +17 -2
index 9252829..9ea5156 100644
@@ -13,12 +13,23 @@
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_platform.h>
 #include <sbi/sbi_trap.h>
+#include <sbi/riscv_asm.h>
+
+static inline unsigned long sbi_ecall_vendor_id(void)
+{
+	return SBI_EXT_VENDOR_START +
+		(csr_read(CSR_MVENDORID) &
+		 (SBI_EXT_VENDOR_END - SBI_EXT_VENDOR_START));
+}
 
 static int sbi_ecall_vendor_probe(unsigned long extid,
 				  unsigned long *out_val)
 {
-	*out_val = sbi_platform_vendor_ext_check(sbi_platform_thishart_ptr(),
-						 extid);
+	if (!sbi_platform_vendor_ext_check(sbi_platform_thishart_ptr()) ||
+	    extid != sbi_ecall_vendor_id())
+		*out_val = 0;
+	else
+		*out_val = 1;
 	return 0;
 }
 
@@ -27,6 +38,10 @@ static int sbi_ecall_vendor_handler(unsigned long extid, unsigned long funcid,
 				    unsigned long *out_val,
 				    struct sbi_trap_info *out_trap)
 {
+	if (!sbi_platform_vendor_ext_check(sbi_platform_thishart_ptr()) ||
+	    extid != sbi_ecall_vendor_id())
+		return SBI_ERR_NOT_SUPPORTED;
+
 	return sbi_platform_vendor_ext_provider(sbi_platform_thishart_ptr(),
 						extid, funcid, regs,
 						out_val, out_trap);
platform/generic/include/platform_override.h +0 -1
index a59b06a..350c381 100644
@@ -27,7 +27,6 @@ struct platform_override {
 	int (*extensions_init)(const struct fdt_match *match,
 			       struct sbi_hart_features *hfeatures);
 	void (*fw_init)(void *fdt, const struct fdt_match *match);
-	int (*vendor_ext_check)(long extid, const struct fdt_match *match);
 	int (*vendor_ext_provider)(long extid, long funcid,
 				   const struct sbi_trap_regs *regs,
 				   unsigned long *out_value,
platform/generic/platform.c +6 -13
index a34d3b0..88c3ec7 100644
@@ -180,13 +180,10 @@ static int generic_final_init(bool cold_boot)
 	return 0;
 }
 
-static int generic_vendor_ext_check(long extid)
+static bool generic_vendor_ext_check(void)
 {
-	if (generic_plat && generic_plat->vendor_ext_check)
-		return generic_plat->vendor_ext_check(extid,
-						      generic_plat_match);
-
-	return 0;
+	return (generic_plat && generic_plat->vendor_ext_provider) ?
+		true : false;
 }
 
 static int generic_vendor_ext_provider(long extid, long funcid,
@@ -194,13 +191,9 @@ static int generic_vendor_ext_provider(long extid, long funcid,
 				       unsigned long *out_value,
 				       struct sbi_trap_info *out_trap)
 {
-	if (generic_plat && generic_plat->vendor_ext_provider) {
-		return generic_plat->vendor_ext_provider(extid, funcid, regs,
-							 out_value, out_trap,
-							 generic_plat_match);
-	}
-
-	return SBI_ENOTSUPP;
+	return generic_plat->vendor_ext_provider(extid, funcid, regs,
+						 out_value, out_trap,
+						 generic_plat_match);
 }
 
 static void generic_early_exit(void)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 2/2 include: sbi: Remove extid parameter from vendor_ext_provider() callback

The extid parameter of vendor_ext_provider() is redundant so let us remove it.

---
 include/sbi/sbi_platform.h                   | 9 ++++-----
 lib/sbi/sbi_ecall_vendor.c                   | 2 +-
 platform/generic/include/platform_override.h | 2 +-
 platform/generic/platform.c                  | 4 ++--
 4 files changed, 8 insertions(+), 9 deletions(-)
include/sbi/sbi_platform.h +4 -5
index 3a629a6..546c0a6 100644
@@ -129,7 +129,7 @@ struct sbi_platform_operations {
 	/** Check if SBI vendor extension is implemented or not */
 	bool (*vendor_ext_check)(void);
 	/** platform specific SBI extension implementation provider */
-	int (*vendor_ext_provider)(long extid, long funcid,
+	int (*vendor_ext_provider)(long funcid,
 				   const struct sbi_trap_regs *regs,
 				   unsigned long *out_value,
 				   struct sbi_trap_info *out_trap);
@@ -655,7 +655,6 @@ static inline bool sbi_platform_vendor_ext_check(
  * Invoke platform specific vendor SBI extension implementation.
  *
  * @param plat pointer to struct sbi_platform
- * @param extid	vendor SBI extension id
  * @param funcid SBI function id within the extension id
  * @param regs pointer to trap registers passed by the caller
  * @param out_value output value that can be filled by the callee
@@ -665,14 +664,14 @@ static inline bool sbi_platform_vendor_ext_check(
  */
 static inline int sbi_platform_vendor_ext_provider(
 					const struct sbi_platform *plat,
-					long extid, long funcid,
+					long funcid,
 					const struct sbi_trap_regs *regs,
 					unsigned long *out_value,
 					struct sbi_trap_info *out_trap)
 {
 	if (plat && sbi_platform_ops(plat)->vendor_ext_provider) {
-		return sbi_platform_ops(plat)->vendor_ext_provider(extid,
-								funcid, regs,
+		return sbi_platform_ops(plat)->vendor_ext_provider(funcid,
+								regs,
 								out_value,
 								out_trap);
 	}
lib/sbi/sbi_ecall_vendor.c +1 -1
index 9ea5156..8b8dab0 100644
@@ -43,7 +43,7 @@ static int sbi_ecall_vendor_handler(unsigned long extid, unsigned long funcid,
 		return SBI_ERR_NOT_SUPPORTED;
 
 	return sbi_platform_vendor_ext_provider(sbi_platform_thishart_ptr(),
-						extid, funcid, regs,
+						funcid, regs,
 						out_val, out_trap);
 }
 
platform/generic/include/platform_override.h +1 -1
index 350c381..0d9e5ee 100644
@@ -27,7 +27,7 @@ struct platform_override {
 	int (*extensions_init)(const struct fdt_match *match,
 			       struct sbi_hart_features *hfeatures);
 	void (*fw_init)(void *fdt, const struct fdt_match *match);
-	int (*vendor_ext_provider)(long extid, long funcid,
+	int (*vendor_ext_provider)(long funcid,
 				   const struct sbi_trap_regs *regs,
 				   unsigned long *out_value,
 				   struct sbi_trap_info *out_trap,
platform/generic/platform.c +2 -2
index 88c3ec7..0b90fd7 100644
@@ -186,12 +186,12 @@ static bool generic_vendor_ext_check(void)
 		true : false;
 }
 
-static int generic_vendor_ext_provider(long extid, long funcid,
+static int generic_vendor_ext_provider(long funcid,
 				       const struct sbi_trap_regs *regs,
 				       unsigned long *out_value,
 				       struct sbi_trap_info *out_trap)
 {
-	return generic_plat->vendor_ext_provider(extid, funcid, regs,
+	return generic_plat->vendor_ext_provider(funcid, regs,
 						 out_value, out_trap,
 						 generic_plat_match);
 }
Signature · 1 line
2.34.1
Applied
#3 lore raw

PATCH 1/2 lib: sbi: Align SBI vendor extension id with mvendorid CSR

In reply to Anup Patel
Quoted 7 lines · Anup Patel

As-per the SBI specification, the lower 24bits of the SBI vendor extension id is same as lower 24bits of the mvendorid CSR.

We update the SBI vendor extension id checking based on above.

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 143 lines
---
 include/sbi/sbi_platform.h                   | 17 ++++++++---------
 lib/sbi/sbi_ecall_vendor.c                   | 19 +++++++++++++++++--
 platform/generic/include/platform_override.h |  1 -
 platform/generic/platform.c                  | 19 ++++++-------------
 4 files changed, 31 insertions(+), 25 deletions(-)
include/sbi/sbi_platform.h +8 -9
index 43d1c03..3a629a6 100644
@@ -126,8 +126,8 @@ struct sbi_platform_operations {
        /** Exit platform timer for current HART */
        void (*timer_exit)(void);
 
-       /** platform specific SBI extension implementation probe function */
-       int (*vendor_ext_check)(long extid);
+       /** Check if SBI vendor extension is implemented or not */
+       bool (*vendor_ext_check)(void);
        /** platform specific SBI extension implementation provider */
        int (*vendor_ext_provider)(long extid, long funcid,
                                   const struct sbi_trap_regs *regs,
@@ -636,20 +636,19 @@ static inline void sbi_platform_timer_exit(const struct sbi_platform *plat)
 }
 
 /**
- * Check if a vendor extension is implemented or not.
+ * Check if SBI vendor extension is implemented or not.
  *
  * @param plat pointer to struct sbi_platform
- * @param extid        vendor SBI extension id
  *
- * @return 0 if extid is not implemented and 1 if implemented
+ * @return false if not implemented and true if implemented
  */
-static inline int sbi_platform_vendor_ext_check(const struct sbi_platform *plat,
-                                               long extid)
+static inline bool sbi_platform_vendor_ext_check(
+                                       const struct sbi_platform *plat)
 {
        if (plat && sbi_platform_ops(plat)->vendor_ext_check)
-               return sbi_platform_ops(plat)->vendor_ext_check(extid);
+               return sbi_platform_ops(plat)->vendor_ext_check();
 
-       return 0;
+       return false;
 }
 
 /**
lib/sbi/sbi_ecall_vendor.c +17 -2
index 9252829..9ea5156 100644
@@ -13,12 +13,23 @@
 #include <sbi/sbi_error.h>
 #include <sbi/sbi_platform.h>
 #include <sbi/sbi_trap.h>
+#include <sbi/riscv_asm.h>
+
+static inline unsigned long sbi_ecall_vendor_id(void)
+{
+       return SBI_EXT_VENDOR_START +
+               (csr_read(CSR_MVENDORID) &
+                (SBI_EXT_VENDOR_END - SBI_EXT_VENDOR_START));
+}
 
 static int sbi_ecall_vendor_probe(unsigned long extid,
                                  unsigned long *out_val)
 {
-       *out_val = sbi_platform_vendor_ext_check(sbi_platform_thishart_ptr(),
-                                                extid);
+       if (!sbi_platform_vendor_ext_check(sbi_platform_thishart_ptr()) ||
+           extid != sbi_ecall_vendor_id())
+               *out_val = 0;
+       else
+               *out_val = 1;
        return 0;
 }
 
@@ -27,6 +38,10 @@ static int sbi_ecall_vendor_handler(unsigned long extid, unsigned long funcid,
                                    unsigned long *out_val,
                                    struct sbi_trap_info *out_trap)
 {
+       if (!sbi_platform_vendor_ext_check(sbi_platform_thishart_ptr()) ||
+           extid != sbi_ecall_vendor_id())
+               return SBI_ERR_NOT_SUPPORTED;
+
        return sbi_platform_vendor_ext_provider(sbi_platform_thishart_ptr(),
                                                extid, funcid, regs,
                                                out_val, out_trap);
platform/generic/include/platform_override.h +0 -1
index a59b06a..350c381 100644
@@ -27,7 +27,6 @@ struct platform_override {
        int (*extensions_init)(const struct fdt_match *match,
                               struct sbi_hart_features *hfeatures);
        void (*fw_init)(void *fdt, const struct fdt_match *match);
-       int (*vendor_ext_check)(long extid, const struct fdt_match *match);
        int (*vendor_ext_provider)(long extid, long funcid,
                                   const struct sbi_trap_regs *regs,
                                   unsigned long *out_value,
platform/generic/platform.c +6 -14
index a34d3b0..88c3ec7 100644
@@ -180,13 +180,10 @@ static int generic_final_init(bool cold_boot)
        return 0;
 }
 
-static int generic_vendor_ext_check(long extid)
+static bool generic_vendor_ext_check(void)
 {
-       if (generic_plat && generic_plat->vendor_ext_check)
-               return generic_plat->vendor_ext_check(extid,
-                                                     generic_plat_match);
-
-       return 0;
+       return (generic_plat && generic_plat->vendor_ext_provider) ?
+               true : false;
 }
 
 static int generic_vendor_ext_provider(long extid, long funcid,
@@ -194,13 +191,9 @@ static int generic_vendor_ext_provider(long extid, long funcid,
                                       unsigned long *out_value,
                                       struct sbi_trap_info *out_trap)
 {
-       if (generic_plat && generic_plat->vendor_ext_provider) {
-               return generic_plat->vendor_ext_provider(extid, funcid, regs,
-                                                        out_value, out_trap,
-                                                        generic_plat_match);
-       }
-
-       return SBI_ENOTSUPP;
+       return generic_plat->vendor_ext_provider(extid, funcid, regs,
+                                                out_value, out_trap,
+                                                generic_plat_match);
 }
 
 static void generic_early_exit(void)
--

2.34.1

Applied
#2 lore raw

PATCH 2/2 include: sbi: Remove extid parameter from vendor_ext_provider() callback

In reply to Anup Patel
Quoted 5 lines · Anup Patel

The extid parameter of vendor_ext_provider() is redundant so let us remove it.

Applied this patch to the riscv/opensbi repo.

Regards,
Anup

Quoted 94 lines
---
 include/sbi/sbi_platform.h                   | 9 ++++-----
 lib/sbi/sbi_ecall_vendor.c                   | 2 +-
 platform/generic/include/platform_override.h | 2 +-
 platform/generic/platform.c                  | 4 ++--
 4 files changed, 8 insertions(+), 9 deletions(-)
include/sbi/sbi_platform.h +4 -5
index 3a629a6..546c0a6 100644
@@ -129,7 +129,7 @@ struct sbi_platform_operations {
        /** Check if SBI vendor extension is implemented or not */
        bool (*vendor_ext_check)(void);
        /** platform specific SBI extension implementation provider */
-       int (*vendor_ext_provider)(long extid, long funcid,
+       int (*vendor_ext_provider)(long funcid,
                                   const struct sbi_trap_regs *regs,
                                   unsigned long *out_value,
                                   struct sbi_trap_info *out_trap);
@@ -655,7 +655,6 @@ static inline bool sbi_platform_vendor_ext_check(
  * Invoke platform specific vendor SBI extension implementation.
  *
  * @param plat pointer to struct sbi_platform
- * @param extid        vendor SBI extension id
  * @param funcid SBI function id within the extension id
  * @param regs pointer to trap registers passed by the caller
  * @param out_value output value that can be filled by the callee
@@ -665,14 +664,14 @@ static inline bool sbi_platform_vendor_ext_check(
  */
 static inline int sbi_platform_vendor_ext_provider(
                                        const struct sbi_platform *plat,
-                                       long extid, long funcid,
+                                       long funcid,
                                        const struct sbi_trap_regs *regs,
                                        unsigned long *out_value,
                                        struct sbi_trap_info *out_trap)
 {
        if (plat && sbi_platform_ops(plat)->vendor_ext_provider) {
-               return sbi_platform_ops(plat)->vendor_ext_provider(extid,
-                                                               funcid, regs,
+               return sbi_platform_ops(plat)->vendor_ext_provider(funcid,
+                                                               regs,
                                                                out_value,
                                                                out_trap);
        }
lib/sbi/sbi_ecall_vendor.c +1 -1
index 9ea5156..8b8dab0 100644
@@ -43,7 +43,7 @@ static int sbi_ecall_vendor_handler(unsigned long extid, unsigned long funcid,
                return SBI_ERR_NOT_SUPPORTED;
 
        return sbi_platform_vendor_ext_provider(sbi_platform_thishart_ptr(),
-                                               extid, funcid, regs,
+                                               funcid, regs,
                                                out_val, out_trap);
 }
 
platform/generic/include/platform_override.h +1 -1
index 350c381..0d9e5ee 100644
@@ -27,7 +27,7 @@ struct platform_override {
        int (*extensions_init)(const struct fdt_match *match,
                               struct sbi_hart_features *hfeatures);
        void (*fw_init)(void *fdt, const struct fdt_match *match);
-       int (*vendor_ext_provider)(long extid, long funcid,
+       int (*vendor_ext_provider)(long funcid,
                                   const struct sbi_trap_regs *regs,
                                   unsigned long *out_value,
                                   struct sbi_trap_info *out_trap,
platform/generic/platform.c +2 -3
index 88c3ec7..0b90fd7 100644
@@ -186,12 +186,12 @@ static bool generic_vendor_ext_check(void)
                true : false;
 }
 
-static int generic_vendor_ext_provider(long extid, long funcid,
+static int generic_vendor_ext_provider(long funcid,
                                       const struct sbi_trap_regs *regs,
                                       unsigned long *out_value,
                                       struct sbi_trap_info *out_trap)
 {
-       return generic_plat->vendor_ext_provider(extid, funcid, regs,
+       return generic_plat->vendor_ext_provider(funcid, regs,
                                                 out_value, out_trap,
                                                 generic_plat_match);
 }
--

2.34.1