[PATCH 2/5] lib: utils: Add vendor feature-configuration driver support
Leo Yu-Chi Liang
leo.liang at sifive.com
Tue Sep 29 22:42:07 PDT 2026
Add the vendor half of the feature-configuration mechanism: the driver
interface, the carray that registers drivers, and feature_config_probe(),
which matches the current hart's mvendorid against a registered driver
and calls its init() callback.
A driver declares the mvendorid it supports and a init() callback
that caches the CSR values that hart needs. Returning nonzero is a
hard failure; an unrecognised hart is left untouched, which is not
an error.
Keeping the driver interface here rather than in lib/sbi lets
libsbi.a stay self-contained: it depends only on the
feature_config_init platform operation, not on vendor-specific code.
Signed-off-by: Leo Yu-Chi Liang <leo.liang at sifive.com>
Assisted-by: LLM
---
include/sbi_utils/feature/feature_config.h | 71 +++++++++++++++++++
lib/utils/Kconfig | 2 +
lib/utils/feature/Kconfig | 5 ++
lib/utils/feature/feature_config.c | 36 ++++++++++
.../feature/feature_config_drivers.carray | 3 +
lib/utils/feature/objects.mk | 8 +++
6 files changed, 125 insertions(+)
create mode 100644 include/sbi_utils/feature/feature_config.h
create mode 100644 lib/utils/feature/Kconfig
create mode 100644 lib/utils/feature/feature_config.c
create mode 100644 lib/utils/feature/feature_config_drivers.carray
create mode 100644 lib/utils/feature/objects.mk
diff --git a/include/sbi_utils/feature/feature_config.h b/include/sbi_utils/feature/feature_config.h
new file mode 100644
index 00000000..b57873dd
--- /dev/null
+++ b/include/sbi_utils/feature/feature_config.h
@@ -0,0 +1,71 @@
+/* SPDX-License-Identifier: BSD-2-Clause */
+/*
+ * feature_config.h - Vendor feature-configuration drivers.
+ *
+ * Copyright (c) 2026 SiFive Inc.
+ */
+
+#ifndef __FEATURE_CONFIG_H__
+#define __FEATURE_CONFIG_H__
+
+#include <sbi/sbi_types.h>
+
+/**
+ * A single feature-configuration CSR programming request
+ */
+struct feature_config_csr {
+ unsigned long csr_num;
+ unsigned long value;
+};
+
+/**
+ * Vendor feature-configuration driver
+ *
+ * Registered through the feature_config_drivers carray, so adding a
+ * vendor never requires touching common code.
+ */
+struct feature_config_driver {
+ const char *name;
+ /** mvendorid of the harts this driver applies to */
+ unsigned long mvendorid;
+ /**
+ * Match the current hart's identity and cache its CSR write
+ * via sbi_hart_feature_config_set().
+ * No match is not an error: the hart is left untouched.
+ *
+ * @return 0 on success or a negative error code on failure
+ */
+ int (*init)(void);
+};
+
+extern const struct feature_config_driver *const feature_config_drivers[];
+
+/**
+ * Match and cache the current hart's feature-configuration values
+ *
+ * Matches the hart's mvendorid against the registered drivers and
+ * calls the matching driver's init().
+ *
+ * Meant to be wired up as a platform's feature_config_init operation,
+ * called on every hart so a heterogeneous or AMP system picks the
+ * right driver per hart.
+ *
+ * No matching vendor is not an error: the hart is left at its reset
+ * configuration.
+ *
+ * @return 0 on success or a negative error code on failure
+ */
+#ifdef CONFIG_FEATURE_CONFIG
+
+int feature_config_probe(void);
+
+#else
+
+static inline int feature_config_probe(void)
+{
+ return 0;
+}
+
+#endif
+
+#endif /* __FEATURE_CONFIG_H__ */
diff --git a/lib/utils/Kconfig b/lib/utils/Kconfig
index 5a5b3b1e..0c1d157e 100644
--- a/lib/utils/Kconfig
+++ b/lib/utils/Kconfig
@@ -8,6 +8,8 @@ source "$(OPENSBI_SRC_DIR)/lib/utils/cppc/Kconfig"
source "$(OPENSBI_SRC_DIR)/lib/utils/fdt/Kconfig"
+source "$(OPENSBI_SRC_DIR)/lib/utils/feature/Kconfig"
+
source "$(OPENSBI_SRC_DIR)/lib/utils/gpio/Kconfig"
source "$(OPENSBI_SRC_DIR)/lib/utils/hsm/Kconfig"
diff --git a/lib/utils/feature/Kconfig b/lib/utils/feature/Kconfig
new file mode 100644
index 00000000..f6760564
--- /dev/null
+++ b/lib/utils/feature/Kconfig
@@ -0,0 +1,5 @@
+# SPDX-License-Identifier: BSD-2-Clause
+
+menu "Feature Configuration Support"
+
+endmenu
diff --git a/lib/utils/feature/feature_config.c b/lib/utils/feature/feature_config.c
new file mode 100644
index 00000000..11be2bbf
--- /dev/null
+++ b/lib/utils/feature/feature_config.c
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: BSD-2-Clause
+/*
+ * feature_config.c - Vendor feature-configuration drivers.
+ *
+ * Copyright (c) 2026 SiFive Inc.
+ *
+ * Matches the current hart's mvendorid against the registered vendor
+ * drivers and hands off to the matching driver's init().
+ */
+
+#include <sbi/riscv_asm.h>
+#include <sbi/riscv_encoding.h>
+#include <sbi/sbi_console.h>
+#include <sbi/sbi_scratch.h>
+#include <sbi_utils/feature/feature_config.h>
+
+int feature_config_probe(void)
+{
+ unsigned long mvendorid = csr_read(CSR_MVENDORID);
+
+ for (int i = 0; feature_config_drivers[i]; i++) {
+ const struct feature_config_driver *drv =
+ feature_config_drivers[i];
+
+ if (drv->mvendorid != mvendorid)
+ continue;
+
+ return drv->init();
+ }
+
+ /* Unknown vendor: leave the hart at its reset configuration */
+ sbi_dprintf("hart%lu: no driver registered for mvendorid 0x%lx\n",
+ current_hartindex(), mvendorid);
+
+ return 0;
+}
diff --git a/lib/utils/feature/feature_config_drivers.carray b/lib/utils/feature/feature_config_drivers.carray
new file mode 100644
index 00000000..5f3c219e
--- /dev/null
+++ b/lib/utils/feature/feature_config_drivers.carray
@@ -0,0 +1,3 @@
+HEADER: sbi_utils/feature/feature_config.h
+TYPE: const struct feature_config_driver
+NAME: feature_config_drivers
diff --git a/lib/utils/feature/objects.mk b/lib/utils/feature/objects.mk
new file mode 100644
index 00000000..4ed7a97b
--- /dev/null
+++ b/lib/utils/feature/objects.mk
@@ -0,0 +1,8 @@
+#
+# SPDX-License-Identifier: BSD-2-Clause
+#
+# Copyright (c) 2026 SiFive Inc.
+#
+
+libsbiutils-objs-$(CONFIG_FEATURE_CONFIG) += feature/feature_config.o
+libsbiutils-objs-$(CONFIG_FEATURE_CONFIG) += feature/feature_config_drivers.carray.o
--
2.43.7
More information about the opensbi
mailing list