summaryrefslogtreecommitdiff
path: root/components/drivers
diff options
context:
space:
mode:
authorGUI <[email protected]>2025-12-13 16:32:25 +0800
committerGitHub <[email protected]>2025-12-13 16:32:25 +0800
commitd8709ba9fb65bb444f0494c243b9a4dcc08f337c (patch)
treeda50ba4e60918532a08d46497a4138dbe79c2348 /components/drivers
parentaf0e513bef194b591b6e40e0b39fad1a3009d858 (diff)
[dm][hwcache] support hwcache (#11049)
Some ARCH not has std cache ops, such as RISC-V Signed-off-by: GuEe-GUI <[email protected]>
Diffstat (limited to 'components/drivers')
-rwxr-xr-xcomponents/drivers/Kconfig1
-rw-r--r--components/drivers/hwcache/Kconfig9
-rw-r--r--components/drivers/hwcache/SConscript15
-rw-r--r--components/drivers/hwcache/hwcache.c139
-rw-r--r--components/drivers/include/drivers/hwcache.h48
-rw-r--r--components/drivers/include/rtdevice.h4
6 files changed, 216 insertions, 0 deletions
diff --git a/components/drivers/Kconfig b/components/drivers/Kconfig
index 84ffcad62f..87e37864bd 100755
--- a/components/drivers/Kconfig
+++ b/components/drivers/Kconfig
@@ -30,6 +30,7 @@ rsource "ata/Kconfig"
rsource "nvme/Kconfig"
rsource "block/Kconfig"
rsource "scsi/Kconfig"
+rsource "hwcache/Kconfig"
rsource "regulator/Kconfig"
rsource "reset/Kconfig"
rsource "pmdomain/Kconfig"
diff --git a/components/drivers/hwcache/Kconfig b/components/drivers/hwcache/Kconfig
new file mode 100644
index 0000000000..6936dd1ff3
--- /dev/null
+++ b/components/drivers/hwcache/Kconfig
@@ -0,0 +1,9 @@
+menuconfig RT_USING_HWCACHE
+ bool "Using Hardware Cache device drivers"
+ depends on RT_USING_DM
+ depends on RT_USING_CACHE
+ default n
+
+if RT_USING_HWCACHE
+ osource "$(SOC_DM_HWCACHE_DIR)/Kconfig"
+endif
diff --git a/components/drivers/hwcache/SConscript b/components/drivers/hwcache/SConscript
new file mode 100644
index 0000000000..6d16ba02e6
--- /dev/null
+++ b/components/drivers/hwcache/SConscript
@@ -0,0 +1,15 @@
+from building import *
+
+group = []
+
+if not GetDepend(['RT_USING_HWCACHE']):
+ Return('group')
+
+cwd = GetCurrentDir()
+CPPPATH = [cwd + '/../include']
+
+src = ['hwcache.c']
+
+group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
+
+Return('group')
diff --git a/components/drivers/hwcache/hwcache.c b/components/drivers/hwcache/hwcache.c
new file mode 100644
index 0000000000..65029bf66e
--- /dev/null
+++ b/components/drivers/hwcache/hwcache.c
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2006-2023, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2023-02-25 GuEe-GUI the first version
+ */
+
+#include <drivers/hwcache.h>
+
+#define DBG_TAG "rtdm.hwcache"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
+struct rt_hwcache_ops *rt_dm_cpu_dcache_ops = RT_NULL;
+struct rt_hwcache_ops *rt_dm_cpu_icache_ops = RT_NULL;
+
+static void hwcache_enable(struct rt_hwcache_ops *ops)
+{
+ if (ops && ops->enable)
+ {
+ ops->enable();
+ }
+}
+
+static void hwcache_disable(struct rt_hwcache_ops *ops)
+{
+ if (ops && ops->enable)
+ {
+ ops->enable();
+ }
+}
+
+static rt_base_t hwcache_status(struct rt_hwcache_ops *ops)
+{
+ if (ops && ops->status)
+ {
+ return ops->status();
+ }
+
+ return 0;
+}
+
+static void hwcache_ops(struct rt_hwcache_ops *ops, int op, void *addr, int size)
+{
+ if (ops)
+ {
+ if (op == RT_HW_CACHE_FLUSH)
+ {
+ if (ops->flush)
+ {
+ ops->flush(addr, size);
+ }
+ }
+ else if (op == RT_HW_CACHE_INVALIDATE)
+ {
+ if (ops->invalidate)
+ {
+ ops->invalidate(addr, size);
+ }
+ }
+ }
+}
+
+void rt_hwcache_icache_enable(void)
+{
+ hwcache_enable(rt_dm_cpu_icache_ops);
+}
+
+void rt_hwcache_icache_disable(void)
+{
+ hwcache_disable(rt_dm_cpu_icache_ops);
+}
+
+rt_base_t rt_hwcache_icache_status(void)
+{
+ return hwcache_status(rt_dm_cpu_icache_ops);
+}
+
+void rt_hwcache_icache_ops(int ops, void *addr, int size)
+{
+ hwcache_ops(rt_dm_cpu_icache_ops, ops, addr, size);
+}
+
+void rt_hwcache_dcache_enable(void)
+{
+ hwcache_enable(rt_dm_cpu_dcache_ops);
+}
+
+void rt_hwcache_dcache_disable(void)
+{
+ hwcache_disable(rt_dm_cpu_dcache_ops);
+}
+
+rt_base_t rt_hwcache_dcache_status(void)
+{
+ return hwcache_status(rt_dm_cpu_dcache_ops);
+}
+
+void rt_hwcache_dcache_ops(int ops, void *addr, int size)
+{
+ hwcache_ops(rt_dm_cpu_dcache_ops, ops, addr, size);
+}
+
+#ifdef RT_USING_OFW
+RT_OFW_STUB_RANGE_EXPORT(hwcache, _hwcache_ofw_start, _hwcache_ofw_end);
+
+static rt_err_t ofw_hwcache_init(void)
+{
+ struct rt_ofw_node *cache_np;
+
+ rt_ofw_foreach_allnodes(cache_np)
+ {
+ rt_ofw_stub_probe_range(cache_np, &_hwcache_ofw_start, &_hwcache_ofw_end);
+ }
+
+ return RT_EOK;
+}
+#else
+static rt_err_t ofw_hwcache_init(void)
+{
+ return RT_EOK;
+}
+#endif /* !RT_USING_OFW */
+
+rt_err_t rt_hwcache_init(void)
+{
+ rt_err_t err;
+
+ LOG_D("init start");
+
+ err = ofw_hwcache_init();
+
+ LOG_D("init end");
+
+ return err;
+}
diff --git a/components/drivers/include/drivers/hwcache.h b/components/drivers/include/drivers/hwcache.h
new file mode 100644
index 0000000000..048dd303bd
--- /dev/null
+++ b/components/drivers/include/drivers/hwcache.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2006-2023, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date Author Notes
+ * 2023-02-25 GuEe-GUI the first version
+ */
+
+#ifndef __HWCACHE_H__
+#define __HWCACHE_H__
+
+#include <rthw.h>
+#include <rtthread.h>
+#include <drivers/ofw.h>
+
+struct rt_hwcache_ops
+{
+ const char *name;
+
+ void (*enable)(void);
+ void (*disable)(void);
+
+ rt_base_t (*status)(void);
+
+ void (*flush)(void *vaddr, rt_size_t size);
+ void (*invalidate)(void *vaddr, rt_size_t size);
+};
+
+extern struct rt_hwcache_ops *rt_dm_cpu_dcache_ops;
+extern struct rt_hwcache_ops *rt_dm_cpu_icache_ops;
+
+#define RT_HWCACHE_OFW_DECLARE(name, ids, handler) RT_OFW_STUB_EXPORT(name, ids, hwcache, handler)
+
+void rt_hwcache_icache_enable(void);
+void rt_hwcache_icache_disable(void);
+rt_base_t rt_hwcache_icache_status(void);
+void rt_hwcache_icache_ops(int ops, void *addr, int size);
+
+void rt_hwcache_dcache_enable(void);
+void rt_hwcache_dcache_disable(void);
+rt_base_t rt_hwcache_dcache_status(void);
+void rt_hwcache_dcache_ops(int ops, void *addr, int size);
+
+rt_err_t rt_hwcache_init(void);
+
+#endif /* __HWCACHE_H__ */
diff --git a/components/drivers/include/rtdevice.h b/components/drivers/include/rtdevice.h
index d55a41f30b..89af4b933c 100644
--- a/components/drivers/include/rtdevice.h
+++ b/components/drivers/include/rtdevice.h
@@ -130,6 +130,10 @@ extern "C" {
#include "drivers/thermal.h"
#endif /* RT_USING_THERMAL */
+#ifdef RT_USING_HWCACHE
+#include "drivers/hwcache.h"
+#endif /* RT_USING_HWCACHE */
+
#ifdef RT_USING_NVMEM
#include "drivers/nvmem.h"
#endif /* RT_USING_NVMEM */