Skip to content

Commit d8709ba

Browse files
authored
[dm][hwcache] support hwcache (#11049)
Some ARCH not has std cache ops, such as RISC-V Signed-off-by: GuEe-GUI <[email protected]>
1 parent af0e513 commit d8709ba

File tree

6 files changed

+216
-0
lines changed

6 files changed

+216
-0
lines changed

components/drivers/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rsource "ata/Kconfig"
3030
rsource "nvme/Kconfig"
3131
rsource "block/Kconfig"
3232
rsource "scsi/Kconfig"
33+
rsource "hwcache/Kconfig"
3334
rsource "regulator/Kconfig"
3435
rsource "reset/Kconfig"
3536
rsource "pmdomain/Kconfig"

components/drivers/hwcache/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
menuconfig RT_USING_HWCACHE
2+
bool "Using Hardware Cache device drivers"
3+
depends on RT_USING_DM
4+
depends on RT_USING_CACHE
5+
default n
6+
7+
if RT_USING_HWCACHE
8+
osource "$(SOC_DM_HWCACHE_DIR)/Kconfig"
9+
endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from building import *
2+
3+
group = []
4+
5+
if not GetDepend(['RT_USING_HWCACHE']):
6+
Return('group')
7+
8+
cwd = GetCurrentDir()
9+
CPPPATH = [cwd + '/../include']
10+
11+
src = ['hwcache.c']
12+
13+
group = DefineGroup('DeviceDrivers', src, depend = [''], CPPPATH = CPPPATH)
14+
15+
Return('group')
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright (c) 2006-2023, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2023-02-25 GuEe-GUI the first version
9+
*/
10+
11+
#include <drivers/hwcache.h>
12+
13+
#define DBG_TAG "rtdm.hwcache"
14+
#define DBG_LVL DBG_INFO
15+
#include <rtdbg.h>
16+
17+
struct rt_hwcache_ops *rt_dm_cpu_dcache_ops = RT_NULL;
18+
struct rt_hwcache_ops *rt_dm_cpu_icache_ops = RT_NULL;
19+
20+
static void hwcache_enable(struct rt_hwcache_ops *ops)
21+
{
22+
if (ops && ops->enable)
23+
{
24+
ops->enable();
25+
}
26+
}
27+
28+
static void hwcache_disable(struct rt_hwcache_ops *ops)
29+
{
30+
if (ops && ops->enable)
31+
{
32+
ops->enable();
33+
}
34+
}
35+
36+
static rt_base_t hwcache_status(struct rt_hwcache_ops *ops)
37+
{
38+
if (ops && ops->status)
39+
{
40+
return ops->status();
41+
}
42+
43+
return 0;
44+
}
45+
46+
static void hwcache_ops(struct rt_hwcache_ops *ops, int op, void *addr, int size)
47+
{
48+
if (ops)
49+
{
50+
if (op == RT_HW_CACHE_FLUSH)
51+
{
52+
if (ops->flush)
53+
{
54+
ops->flush(addr, size);
55+
}
56+
}
57+
else if (op == RT_HW_CACHE_INVALIDATE)
58+
{
59+
if (ops->invalidate)
60+
{
61+
ops->invalidate(addr, size);
62+
}
63+
}
64+
}
65+
}
66+
67+
void rt_hwcache_icache_enable(void)
68+
{
69+
hwcache_enable(rt_dm_cpu_icache_ops);
70+
}
71+
72+
void rt_hwcache_icache_disable(void)
73+
{
74+
hwcache_disable(rt_dm_cpu_icache_ops);
75+
}
76+
77+
rt_base_t rt_hwcache_icache_status(void)
78+
{
79+
return hwcache_status(rt_dm_cpu_icache_ops);
80+
}
81+
82+
void rt_hwcache_icache_ops(int ops, void *addr, int size)
83+
{
84+
hwcache_ops(rt_dm_cpu_icache_ops, ops, addr, size);
85+
}
86+
87+
void rt_hwcache_dcache_enable(void)
88+
{
89+
hwcache_enable(rt_dm_cpu_dcache_ops);
90+
}
91+
92+
void rt_hwcache_dcache_disable(void)
93+
{
94+
hwcache_disable(rt_dm_cpu_dcache_ops);
95+
}
96+
97+
rt_base_t rt_hwcache_dcache_status(void)
98+
{
99+
return hwcache_status(rt_dm_cpu_dcache_ops);
100+
}
101+
102+
void rt_hwcache_dcache_ops(int ops, void *addr, int size)
103+
{
104+
hwcache_ops(rt_dm_cpu_dcache_ops, ops, addr, size);
105+
}
106+
107+
#ifdef RT_USING_OFW
108+
RT_OFW_STUB_RANGE_EXPORT(hwcache, _hwcache_ofw_start, _hwcache_ofw_end);
109+
110+
static rt_err_t ofw_hwcache_init(void)
111+
{
112+
struct rt_ofw_node *cache_np;
113+
114+
rt_ofw_foreach_allnodes(cache_np)
115+
{
116+
rt_ofw_stub_probe_range(cache_np, &_hwcache_ofw_start, &_hwcache_ofw_end);
117+
}
118+
119+
return RT_EOK;
120+
}
121+
#else
122+
static rt_err_t ofw_hwcache_init(void)
123+
{
124+
return RT_EOK;
125+
}
126+
#endif /* !RT_USING_OFW */
127+
128+
rt_err_t rt_hwcache_init(void)
129+
{
130+
rt_err_t err;
131+
132+
LOG_D("init start");
133+
134+
err = ofw_hwcache_init();
135+
136+
LOG_D("init end");
137+
138+
return err;
139+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2006-2023, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2023-02-25 GuEe-GUI the first version
9+
*/
10+
11+
#ifndef __HWCACHE_H__
12+
#define __HWCACHE_H__
13+
14+
#include <rthw.h>
15+
#include <rtthread.h>
16+
#include <drivers/ofw.h>
17+
18+
struct rt_hwcache_ops
19+
{
20+
const char *name;
21+
22+
void (*enable)(void);
23+
void (*disable)(void);
24+
25+
rt_base_t (*status)(void);
26+
27+
void (*flush)(void *vaddr, rt_size_t size);
28+
void (*invalidate)(void *vaddr, rt_size_t size);
29+
};
30+
31+
extern struct rt_hwcache_ops *rt_dm_cpu_dcache_ops;
32+
extern struct rt_hwcache_ops *rt_dm_cpu_icache_ops;
33+
34+
#define RT_HWCACHE_OFW_DECLARE(name, ids, handler) RT_OFW_STUB_EXPORT(name, ids, hwcache, handler)
35+
36+
void rt_hwcache_icache_enable(void);
37+
void rt_hwcache_icache_disable(void);
38+
rt_base_t rt_hwcache_icache_status(void);
39+
void rt_hwcache_icache_ops(int ops, void *addr, int size);
40+
41+
void rt_hwcache_dcache_enable(void);
42+
void rt_hwcache_dcache_disable(void);
43+
rt_base_t rt_hwcache_dcache_status(void);
44+
void rt_hwcache_dcache_ops(int ops, void *addr, int size);
45+
46+
rt_err_t rt_hwcache_init(void);
47+
48+
#endif /* __HWCACHE_H__ */

components/drivers/include/rtdevice.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ extern "C" {
130130
#include "drivers/thermal.h"
131131
#endif /* RT_USING_THERMAL */
132132

133+
#ifdef RT_USING_HWCACHE
134+
#include "drivers/hwcache.h"
135+
#endif /* RT_USING_HWCACHE */
136+
133137
#ifdef RT_USING_NVMEM
134138
#include "drivers/nvmem.h"
135139
#endif /* RT_USING_NVMEM */

0 commit comments

Comments
 (0)