1 | /* |
---|
2 | * Cache Manager |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-1999. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * |
---|
12 | * The functions in this file implement the API to the RTEMS Cache Manager and |
---|
13 | * are divided into data cache and instruction cache functions. Data cache |
---|
14 | * functions only have bodies if a data cache is supported. Instruction |
---|
15 | * cache functions only have bodies if an instruction cache is supported. |
---|
16 | * Support for a particular cache exists only if CPU_x_CACHE_ALIGNMENT is |
---|
17 | * defined, where x E {DATA, INSTRUCTION}. These definitions are found in |
---|
18 | * the Cache Manager Wrapper header files, often |
---|
19 | * |
---|
20 | * rtems/c/src/lib/libcpu/CPU/cache_.h |
---|
21 | * |
---|
22 | * The functions below are implemented with CPU dependent inline routines |
---|
23 | * found in the cache.c files for each CPU. In the event that a CPU does |
---|
24 | * not support a specific function for a cache it has, the CPU dependent |
---|
25 | * routine does nothing (but does exist). |
---|
26 | * |
---|
27 | * At this point, the Cache Manager makes no considerations, and provides no |
---|
28 | * support for BSP specific issues such as a secondary cache. In such a system, |
---|
29 | * the CPU dependent routines would have to be modified, or a BSP layer added |
---|
30 | * to this Manager. |
---|
31 | */ |
---|
32 | |
---|
33 | #include <rtems.h> |
---|
34 | #include "cache_.h" |
---|
35 | |
---|
36 | /* |
---|
37 | * THESE FUNCTIONS ONLY HAVE BODIES IF WE HAVE A DATA CACHE |
---|
38 | */ |
---|
39 | |
---|
40 | /* |
---|
41 | * This function is called to flush the data cache by performing cache |
---|
42 | * copybacks. It must determine how many cache lines need to be copied |
---|
43 | * back and then perform the copybacks. |
---|
44 | */ |
---|
45 | void |
---|
46 | rtems_cache_flush_multiple_data_lines( const void * d_addr, size_t n_bytes ) |
---|
47 | { |
---|
48 | #if defined(CPU_DATA_CACHE_ALIGNMENT) |
---|
49 | const void * final_address; |
---|
50 | |
---|
51 | /* |
---|
52 | * Set d_addr to the beginning of the cache line; final_address indicates |
---|
53 | * the last address_t which needs to be pushed. Increment d_addr and push |
---|
54 | * the resulting line until final_address is passed. |
---|
55 | */ |
---|
56 | |
---|
57 | if( n_bytes == 0 ) |
---|
58 | /* Do nothing if number of bytes to flush is zero */ |
---|
59 | return; |
---|
60 | |
---|
61 | final_address = (void *)((size_t)d_addr + n_bytes - 1); |
---|
62 | d_addr = (void *)((size_t)d_addr & ~(CPU_DATA_CACHE_ALIGNMENT - 1)); |
---|
63 | while( d_addr <= final_address ) { |
---|
64 | _CPU_cache_flush_1_data_line( d_addr ); |
---|
65 | d_addr = (void *)((size_t)d_addr + CPU_DATA_CACHE_ALIGNMENT); |
---|
66 | } |
---|
67 | #endif |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | /* |
---|
72 | * This function is responsible for performing a data cache invalidate. |
---|
73 | * It must determine how many cache lines need to be invalidated and then |
---|
74 | * perform the invalidations. |
---|
75 | */ |
---|
76 | |
---|
77 | void |
---|
78 | rtems_cache_invalidate_multiple_data_lines( const void * d_addr, size_t n_bytes ) |
---|
79 | { |
---|
80 | #if defined(CPU_DATA_CACHE_ALIGNMENT) |
---|
81 | const void * final_address; |
---|
82 | |
---|
83 | /* |
---|
84 | * Set d_addr to the beginning of the cache line; final_address indicates |
---|
85 | * the last address_t which needs to be invalidated. Increment d_addr and |
---|
86 | * invalidate the resulting line until final_address is passed. |
---|
87 | */ |
---|
88 | |
---|
89 | if( n_bytes == 0 ) |
---|
90 | /* Do nothing if number of bytes to invalidate is zero */ |
---|
91 | return; |
---|
92 | |
---|
93 | final_address = (void *)((size_t)d_addr + n_bytes - 1); |
---|
94 | d_addr = (void *)((size_t)d_addr & ~(CPU_DATA_CACHE_ALIGNMENT - 1)); |
---|
95 | while( final_address >= d_addr ) { |
---|
96 | _CPU_cache_invalidate_1_data_line( d_addr ); |
---|
97 | d_addr = (void *)((size_t)d_addr + CPU_DATA_CACHE_ALIGNMENT); |
---|
98 | } |
---|
99 | #endif |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | /* |
---|
104 | * This function is responsible for performing a data cache flush. |
---|
105 | * It flushes the entire cache. |
---|
106 | */ |
---|
107 | void |
---|
108 | rtems_cache_flush_entire_data( void ) |
---|
109 | { |
---|
110 | #if defined(CPU_DATA_CACHE_ALIGNMENT) |
---|
111 | /* |
---|
112 | * Call the CPU-specific routine |
---|
113 | */ |
---|
114 | _CPU_cache_flush_entire_data(); |
---|
115 | #endif |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | /* |
---|
120 | * This function is responsible for performing a data cache |
---|
121 | * invalidate. It invalidates the entire cache. |
---|
122 | */ |
---|
123 | void |
---|
124 | rtems_cache_invalidate_entire_data( void ) |
---|
125 | { |
---|
126 | #if defined(CPU_DATA_CACHE_ALIGNMENT) |
---|
127 | /* |
---|
128 | * Call the CPU-specific routine |
---|
129 | */ |
---|
130 | |
---|
131 | _CPU_cache_invalidate_entire_data(); |
---|
132 | #endif |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /* |
---|
137 | * This function returns the data cache granularity. |
---|
138 | */ |
---|
139 | int |
---|
140 | rtems_cache_get_data_line_size( void ) |
---|
141 | { |
---|
142 | #if defined(CPU_DATA_CACHE_ALIGNMENT) |
---|
143 | return CPU_DATA_CACHE_ALIGNMENT; |
---|
144 | #else |
---|
145 | return 0; |
---|
146 | #endif |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | /* |
---|
151 | * This function freezes the data cache; cache lines |
---|
152 | * are not replaced. |
---|
153 | */ |
---|
154 | void |
---|
155 | rtems_cache_freeze_data( void ) |
---|
156 | { |
---|
157 | #if defined(CPU_DATA_CACHE_ALIGNMENT) |
---|
158 | _CPU_cache_freeze_data(); |
---|
159 | #endif |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | /* |
---|
164 | * This function unfreezes the instruction cache. |
---|
165 | */ |
---|
166 | void rtems_cache_unfreeze_data( void ) |
---|
167 | { |
---|
168 | #if defined(CPU_DATA_CACHE_ALIGNMENT) |
---|
169 | _CPU_cache_unfreeze_data(); |
---|
170 | #endif |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | /* Turn on the data cache. */ |
---|
175 | void |
---|
176 | rtems_cache_enable_data( void ) |
---|
177 | { |
---|
178 | #if defined(CPU_DATA_CACHE_ALIGNMENT) |
---|
179 | _CPU_cache_enable_data(); |
---|
180 | #endif |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | /* Turn off the data cache. */ |
---|
185 | void |
---|
186 | rtems_cache_disable_data( void ) |
---|
187 | { |
---|
188 | #if defined(CPU_DATA_CACHE_ALIGNMENT) |
---|
189 | _CPU_cache_disable_data(); |
---|
190 | #endif |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | /* |
---|
196 | * THESE FUNCTIONS ONLY HAVE BODIES IF WE HAVE AN INSTRUCTION CACHE |
---|
197 | */ |
---|
198 | |
---|
199 | /* |
---|
200 | * This function is responsible for performing an instruction cache |
---|
201 | * invalidate. It must determine how many cache lines need to be invalidated |
---|
202 | * and then perform the invalidations. |
---|
203 | */ |
---|
204 | void |
---|
205 | rtems_cache_invalidate_multiple_instruction_lines( const void * i_addr, size_t n_bytes ) |
---|
206 | { |
---|
207 | #if CPU_INSTRUCTION_CACHE_ALIGNMENT |
---|
208 | const void * final_address; |
---|
209 | |
---|
210 | /* |
---|
211 | * Set i_addr to the beginning of the cache line; final_address indicates |
---|
212 | * the last address_t which needs to be invalidated. Increment i_addr and |
---|
213 | * invalidate the resulting line until final_address is passed. |
---|
214 | */ |
---|
215 | |
---|
216 | if( n_bytes == 0 ) |
---|
217 | /* Do nothing if number of bytes to invalidate is zero */ |
---|
218 | return; |
---|
219 | |
---|
220 | final_address = (void *)((size_t)i_addr + n_bytes - 1); |
---|
221 | i_addr = (void *)((size_t)i_addr & ~(CPU_INSTRUCTION_CACHE_ALIGNMENT - 1)); |
---|
222 | while( final_address > i_addr ) { |
---|
223 | _CPU_cache_invalidate_1_instruction_line( i_addr ); |
---|
224 | i_addr = (void *)((size_t)i_addr + CPU_INSTRUCTION_CACHE_ALIGNMENT); |
---|
225 | } |
---|
226 | #endif |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | /* |
---|
231 | * This function is responsible for performing an instruction cache |
---|
232 | * invalidate. It invalidates the entire cache. |
---|
233 | */ |
---|
234 | void |
---|
235 | rtems_cache_invalidate_entire_instruction( void ) |
---|
236 | { |
---|
237 | #if defined(CPU_INSTRUCTION_CACHE_ALIGNMENT) |
---|
238 | /* |
---|
239 | * Call the CPU-specific routine |
---|
240 | */ |
---|
241 | |
---|
242 | _CPU_cache_invalidate_entire_instruction(); |
---|
243 | #endif |
---|
244 | } |
---|
245 | |
---|
246 | |
---|
247 | /* |
---|
248 | * This function returns the instruction cache granularity. |
---|
249 | */ |
---|
250 | int |
---|
251 | rtems_cache_get_instruction_line_size( void ) |
---|
252 | { |
---|
253 | #if defined(CPU_INSTRUCTION_CACHE_ALIGNMENT) |
---|
254 | return CPU_INSTRUCTION_CACHE_ALIGNMENT; |
---|
255 | #else |
---|
256 | return 0; |
---|
257 | #endif |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | /* |
---|
262 | * This function freezes the instruction cache; cache lines |
---|
263 | * are not replaced. |
---|
264 | */ |
---|
265 | void |
---|
266 | rtems_cache_freeze_instruction( void ) |
---|
267 | { |
---|
268 | #if defined(CPU_INSTRUCTION_CACHE_ALIGNMENT) |
---|
269 | _CPU_cache_freeze_instruction(); |
---|
270 | #endif |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | /* |
---|
275 | * This function unfreezes the instruction cache. |
---|
276 | */ |
---|
277 | void rtems_cache_unfreeze_instruction( void ) |
---|
278 | { |
---|
279 | #if defined(CPU_INSTRUCTION_CACHE_ALIGNMENT) |
---|
280 | _CPU_cache_unfreeze_instruction(); |
---|
281 | #endif |
---|
282 | } |
---|
283 | |
---|
284 | |
---|
285 | /* Turn on the instruction cache. */ |
---|
286 | void |
---|
287 | rtems_cache_enable_instruction( void ) |
---|
288 | { |
---|
289 | #if defined(CPU_INSTRUCTION_CACHE_ALIGNMENT) |
---|
290 | _CPU_cache_enable_instruction(); |
---|
291 | #endif |
---|
292 | } |
---|
293 | |
---|
294 | |
---|
295 | /* Turn off the instruction cache. */ |
---|
296 | void |
---|
297 | rtems_cache_disable_instruction( void ) |
---|
298 | { |
---|
299 | #if defined(CPU_INSTRUCTION_CACHE_ALIGNMENT) |
---|
300 | _CPU_cache_disable_instruction(); |
---|
301 | #endif |
---|
302 | } |
---|