summaryrefslogtreecommitdiff
path: root/bsp/rockchip/common/drivers/drv_cache.c
blob: ff0618ab00d9b39041dc6fd14a1c77729d7e31c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/**
  * Copyright (c) 2018 Fuzhou Rockchip Electronics Co., Ltd
  *
  * SPDX-License-Identifier: Apache-2.0
  ******************************************************************************
  * @file    drv_cache.c
  * @version V0.1
  * @brief   cpu cache interface
  *
  * Change Logs:
  * Date           Author          Notes
  * 2019-04-01     Cliff.Chen      first implementation
  *
  ******************************************************************************
  */

/** @addtogroup RKBSP_Driver_Reference
 *  @{
 */

/** @addtogroup Cache
 *  @{
 */

/** @defgroup Cache_How_To_Use How To Use
 *  @{

The Cache driver use to keeping data coherent between cpu and device, it can be used in the following three scenarios:

- **The cpu want to read the latest data that has been modified by device**:
    - The device modify the data;
    - The cpu invalidate the data by rt_hw_cpu_dcache_ops(RT_HW_CACHE_INVALIDATE,
           addr, size);
    - The cpu read the latest data;

- **The device want to read the latest data that was modified by cpu**:
    - The cpu modify the data;
    - The device flush the data by rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, addr, size);
    - The device read the latest data;

- **The cpu want to execute two code section on the same memory**:
    - Loading the code A in the memory from start address of ADDR;
    - Executing the code A;
    - Loading the code B in the memory from start address of ADDR;
    - Invalidating by rt_hw_cpu_icache_ops(RT_HW_CACHE_INVALIDATE, ADDR, size);
    - Executing the code B

 @} */

#include <rthw.h>
#include "drv_cache.h"
#include "hal_base.h"

#if defined(ARCH_ARM_CORTEX_M)

#ifdef RT_USING_CMBACKTRACE
#include "cm_backtrace.h"
#endif

/********************* Private MACRO Definition ******************************/
/** @defgroup CACHE_Private_Macro Private Macro
 *  @{
 */

/** @} */  // CACHE_Private_Macro

/********************* Private Structure Definition **************************/
/** @defgroup CACHE_Private_Structure Private Structure
 *  @{
 */

/** @} */  // CACHE_Private_Structure

/********************* Private Variable Definition ***************************/
/** @defgroup CACHE_Private_Variable Private Variable
 *  @{
 */

/** @} */  // CACHE_Private_Variable

/********************* Private Function Definition ***************************/
/** @defgroup CACHE_Private_Function Private Function
 *  @{
 */

/** @} */  // CACHE_Private_Function

/********************* Public Function Definition ****************************/

/** @defgroup CACHE_Public_Functions Public Functions
 *  @{
 */

/**
 * @brief  Enable the icache of cpu.
 * @attention The cache will be enabled when board initialization, do not dynamically switch cache
 *      unless specifically required.
 */
void rt_hw_cpu_icache_enable(void)
{
    HAL_ICACHE_Enable();
}

/**
 * @brief  Disable the icache of cpu.
 * @attention The cache will be enabled when board initialization, do not dynamically switch cache
 *      unless specifically required.
 */
void rt_hw_cpu_icache_disable(void)
{
    HAL_ICACHE_Disable();
}

/**
 * @brief  Get icache status.
 * @return 0
 * @attention Not yet implemnted.
 */
rt_base_t rt_hw_cpu_icache_status(void)
{
    return 0;
}

/**
 * @brief  Icache maintain operation.
 * @param ops: RT_HW_CACHE_INVALIDATE for cache invalidate.
 * @param addr: The start address of memory you want maintain.
 * @param size: The length of memory you want maintain.
 */
void rt_hw_cpu_icache_ops(int ops, void *addr, int size)
{
    if (ops & RT_HW_CACHE_INVALIDATE)
    {
        HAL_ICACHE_InvalidateByRange((uint32_t)addr, size);
    }
}

/**
 * @brief  Enable the dcache of cpu.
 * @attention The cache will be enabled when board initialization, do not dynamically switch cache
 *      unless specifically required.
 */
void rt_hw_cpu_dcache_enable(void)
{
    HAL_DCACHE_Enable();
}

/**
 * @brief  Disable the dcache of cpu.
 * @attention The cache will be enabled when board initialization, do not dynamically switch cache
 *      unless specifically required.
 */
void rt_hw_cpu_dcache_disable(void)
{
    HAL_DCACHE_Disable();
}

/**
 * @brief  Get dcache status.
 * @return 0
 * @attention Not yet implemnted.
 */
rt_base_t rt_hw_cpu_dcache_status(void)
{
    return 0;
}

/**
 * @brief  Dcache maintain operation.
 * @param ops: RT_HW_CACHE_INVALIDATE for cache invalidate,
 *      RT_HW_CACHE_FLUSH for cache clean.
 * @param addr: The start address of memory you want maintain.
 * @param size: The length of memory you want maintain.
 */
void rt_hw_cpu_dcache_ops(int ops, void *addr, int size)
{
    if ((ops & RT_HW_CACHE_FLUSH) && (ops & RT_HW_CACHE_INVALIDATE))
    {
        HAL_DCACHE_CleanInvalidateByRange((uint32_t)addr, size);
    }
    else if (ops & RT_HW_CACHE_FLUSH)
    {
        HAL_DCACHE_CleanByRange((uint32_t)addr, size);
    }
    else if (ops & RT_HW_CACHE_INVALIDATE)
    {
        HAL_DCACHE_InvalidateByRange((uint32_t)addr, size);
    }
    else
    {
        RT_ASSERT(0);
    }
}

/**
 * @brief  Dump ahb error occur in icache & dcache, it called by cache interrupt.
 * @param fault_handler_lr: The value of LR register.
 * @param fault_handler_sp: The value of SP register.
 */
void cache_dump_ahb_error(uint32_t fault_handler_lr, uint32_t fault_handler_sp)
{
    uint32_t addr;

    if (HAL_ICACHE_GetInt())
    {
        addr = HAL_ICACHE_GetErrAddr();
        rt_kprintf("a ahb bus error occur in icache, addr=%p\n", (void *)addr);
        HAL_ICACHE_ClearInt();
    }

    if (HAL_DCACHE_GetInt())
    {
        addr = HAL_DCACHE_GetErrAddr();
        rt_kprintf("a ahb bus error occur in dcache, addr=%p\n", (void *)addr);
        HAL_DCACHE_ClearInt();
    }

#ifdef RT_USING_CMBACKTRACE
    cm_backtrace_fault(fault_handler_lr, fault_handler_sp);
#endif
}

extern void CACHE_IRQHandler(void);

/**
 * @brief  Enable cache interrupt and register the handler, it called by board initialization.
 * @return RT_EOK
 */
int rt_hw_cpu_cache_init(void)
{
#if defined(ICACHE) || defined(DCACHE)
    HAL_ICACHE_EnableInt();
    HAL_DCACHE_EnableInt();
#if defined(RKMCU_PISCES) || defined(RKMCU_RK2108)
    rt_hw_interrupt_install(CACHE_IRQn, (rt_isr_handler_t)CACHE_IRQHandler, RT_NULL, RT_NULL);
    rt_hw_interrupt_umask(CACHE_IRQn);
#elif defined(RKMCU_RK2206)
    rt_hw_interrupt_install(CACHE0_I_IRQn, (rt_isr_handler_t)CACHE_IRQHandler, RT_NULL, RT_NULL);
    rt_hw_interrupt_install(CACHE0_D_IRQn, (rt_isr_handler_t)CACHE_IRQHandler, RT_NULL, RT_NULL);
    rt_hw_interrupt_umask(CACHE0_I_IRQn);
    rt_hw_interrupt_umask(CACHE0_D_IRQn);
#endif
#endif
    return RT_EOK;
}

/** @} */  // CACHE_Public_Functions

#else

rt_weak void rt_hw_cpu_icache_enable(void)
{
}

rt_weak void rt_hw_cpu_icache_disable(void)
{
}

rt_weak rt_base_t rt_hw_cpu_icache_status(void)
{
    return 0;
}

rt_weak void rt_hw_cpu_icache_ops(int ops, void *addr, int size)
{
}

rt_weak void rt_hw_cpu_dcache_enable(void)
{
}

rt_weak void rt_hw_cpu_dcache_disable(void)
{
}

rt_weak rt_base_t rt_hw_cpu_dcache_status(void)
{
    return 0;
}

rt_weak void rt_hw_cpu_dcache_ops(int ops, void *addr, int size)
{
}

#endif

/** @} */  // Cache

/** @} */  // RKBSP_Driver_Reference