|
0
|
1 #ifndef CYGONCE_HAL_CACHE_H |
|
|
2 #define CYGONCE_HAL_CACHE_H |
|
|
3 |
|
|
4 //============================================================================= |
|
|
5 // |
|
2
|
6 // hal_cache.h |
|
0
|
7 // |
|
2
|
8 // HAL cache control API |
|
0
|
9 // |
|
|
10 //============================================================================= |
|
|
11 //####COPYRIGHTBEGIN#### |
|
|
12 // |
|
|
13 // ------------------------------------------- |
|
|
14 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
15 // Version 1.0 (the "License"); you may not use this file except in |
|
|
16 // compliance with the License. You may obtain a copy of the License at |
|
|
17 // http://sourceware.cygnus.com/ecos |
|
|
18 // |
|
|
19 // Software distributed under the License is distributed on an "AS IS" |
|
|
20 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
21 // License for the specific language governing rights and limitations under |
|
|
22 // the License. |
|
|
23 // |
|
|
24 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
25 // September 30, 1998. |
|
|
26 // |
|
|
27 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
28 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
29 // ------------------------------------------- |
|
|
30 // |
|
|
31 //####COPYRIGHTEND#### |
|
|
32 //============================================================================= |
|
|
33 //#####DESCRIPTIONBEGIN#### |
|
|
34 // |
|
2
|
35 // Author(s): nickg |
|
|
36 // Contributors: nickg |
|
|
37 // Date: 1998-02-17 |
|
|
38 // Purpose: Cache control API |
|
|
39 // Description: The macros defined here provide the HAL APIs for handling |
|
0
|
40 // cache control operations. |
|
|
41 // Usage: |
|
2
|
42 // #include <cyg/hal/hal_cache.h> |
|
|
43 // ... |
|
|
44 // |
|
0
|
45 // |
|
|
46 //####DESCRIPTIONEND#### |
|
|
47 // |
|
|
48 //============================================================================= |
|
|
49 |
|
2
|
50 #include <pkgconf/hal.h> |
|
0
|
51 #include <cyg/infra/cyg_type.h> |
|
|
52 |
|
|
53 //============================================================================= |
|
|
54 // MIPS simulator |
|
|
55 |
|
|
56 #ifdef CYG_HAL_MIPS_SIM |
|
|
57 |
|
|
58 //----------------------------------------------------------------------------- |
|
|
59 // Cache dimensions |
|
|
60 |
|
|
61 // Data cache |
|
|
62 #define HAL_DCACHE_SIZE 4096 // Size of data cache in bytes |
|
|
63 #define HAL_DCACHE_LINE_SIZE 16 // Size of a data cache line |
|
|
64 #define HAL_DCACHE_WAYS 2 // Associativity of the cache |
|
|
65 |
|
|
66 // Instruction cache |
|
|
67 #define HAL_ICACHE_SIZE 4096 // Size of cache in bytes |
|
|
68 #define HAL_ICACHE_LINE_SIZE 16 // Size of a cache line |
|
|
69 #define HAL_ICACHE_WAYS 2 // Associativity of the cache |
|
|
70 |
|
|
71 #define HAL_DCACHE_SETS (HAL_DCACHE_SIZE/(HAL_DCACHE_LINE_SIZE*HAL_DCACHE_WAYS)) |
|
|
72 #define HAL_ICACHE_SETS (HAL_ICACHE_SIZE/(HAL_ICACHE_LINE_SIZE*HAL_ICACHE_WAYS)) |
|
|
73 |
|
|
74 //----------------------------------------------------------------------------- |
|
|
75 // Global control of data cache |
|
|
76 |
|
|
77 // Enable the data cache |
|
|
78 #define HAL_DCACHE_ENABLE() |
|
|
79 |
|
|
80 // Disable the data cache |
|
|
81 #define HAL_DCACHE_DISABLE() |
|
|
82 |
|
|
83 // Invalidate the entire cache |
|
|
84 #define HAL_DCACHE_INVALIDATE_ALL() |
|
|
85 |
|
|
86 // Synchronize the contents of the cache with memory. |
|
|
87 #define HAL_DCACHE_SYNC() |
|
|
88 |
|
|
89 // Set the data cache refill burst size |
|
|
90 //#define HAL_DCACHE_BURST_SIZE(_size_) |
|
|
91 |
|
|
92 // Set the data cache write mode |
|
|
93 //#define HAL_DCACHE_WRITE_MODE( _mode_ ) |
|
|
94 |
|
|
95 //#define HAL_DCACHE_WRITETHRU_MODE 0 |
|
|
96 //#define HAL_DCACHE_WRITEBACK_MODE 1 |
|
|
97 |
|
|
98 // Load the contents of the given address range into the data cache |
|
|
99 // and then lock the cache so that it stays there. |
|
|
100 //#define HAL_DCACHE_LOCK(_base_, _size_) |
|
|
101 |
|
|
102 // Undo a previous lock operation |
|
|
103 //#define HAL_DCACHE_UNLOCK(_base_, _size_) |
|
|
104 |
|
2
|
105 // Unlock entire cache |
|
|
106 //#define HAL_DCACHE_UNLOCK_ALL() |
|
|
107 |
|
0
|
108 //----------------------------------------------------------------------------- |
|
|
109 // Data cache line control |
|
|
110 |
|
|
111 // Allocate cache lines for the given address range without reading its |
|
|
112 // contents from memory. |
|
|
113 //#define HAL_DCACHE_ALLOCATE( _base_ , _size_ ) |
|
|
114 |
|
|
115 // Write dirty cache lines to memory and invalidate the cache entries |
|
|
116 // for the given address range. |
|
|
117 //#define HAL_DCACHE_FLUSH( _base_ , _size_ ) |
|
|
118 |
|
|
119 // Invalidate cache lines in the given range without writing to memory. |
|
|
120 //#define HAL_DCACHE_INVALIDATE( _base_ , _size_ ) |
|
|
121 |
|
|
122 // Write dirty cache lines to memory for the given address range. |
|
|
123 //#define HAL_DCACHE_STORE( _base_ , _size_ ) |
|
|
124 |
|
|
125 // Preread the given range into the cache with the intention of reading |
|
|
126 // from it later. |
|
|
127 //#define HAL_DCACHE_READ_HINT( _base_ , _size_ ) |
|
|
128 |
|
|
129 // Preread the given range into the cache with the intention of writing |
|
|
130 // to it later. |
|
|
131 //#define HAL_DCACHE_WRITE_HINT( _base_ , _size_ ) |
|
|
132 |
|
|
133 // Allocate and zero the cache lines associated with the given range. |
|
|
134 //#define HAL_DCACHE_ZERO( _base_ , _size_ ) |
|
|
135 |
|
|
136 //----------------------------------------------------------------------------- |
|
|
137 // Global control of Instruction cache |
|
|
138 |
|
|
139 // Enable the instruction cache |
|
|
140 #define HAL_ICACHE_ENABLE() |
|
|
141 |
|
|
142 // Disable the instruction cache |
|
|
143 #define HAL_ICACHE_DISABLE() |
|
|
144 |
|
|
145 // Invalidate the entire cache |
|
|
146 #define HAL_ICACHE_INVALIDATE_ALL() |
|
|
147 |
|
|
148 // Synchronize the contents of the cache with memory. |
|
|
149 #define HAL_ICACHE_SYNC() |
|
|
150 |
|
|
151 // Set the instruction cache refill burst size |
|
|
152 //#define HAL_ICACHE_BURST_SIZE(_size_) |
|
|
153 |
|
|
154 // Load the contents of the given address range into the instruction cache |
|
|
155 // and then lock the cache so that it stays there. |
|
|
156 //#define HAL_ICACHE_LOCK(_base_, _size_) |
|
|
157 |
|
|
158 // Undo a previous lock operation |
|
|
159 //#define HAL_ICACHE_UNLOCK(_base_, _size_) |
|
|
160 |
|
2
|
161 // Unlock entire cache |
|
|
162 //#define HAL_ICACHE_UNLOCK_ALL() |
|
|
163 |
|
0
|
164 //----------------------------------------------------------------------------- |
|
|
165 // Instruction cache line control |
|
|
166 |
|
|
167 // Invalidate cache lines in the given range without writing to memory. |
|
|
168 //#define HAL_ICACHE_INVALIDATE( _base_ , _size_ ) |
|
|
169 |
|
|
170 #endif |
|
|
171 |
|
|
172 |
|
|
173 //============================================================================= |
|
|
174 // Toshiba TX3904 |
|
|
175 |
|
|
176 #ifdef CYG_HAL_MIPS_TX3904 |
|
|
177 |
|
|
178 //----------------------------------------------------------------------------- |
|
|
179 // Cache dimensions |
|
|
180 |
|
|
181 // Data cache |
|
|
182 #define HAL_DCACHE_SIZE 1024 // Size of data cache in bytes |
|
|
183 #define HAL_DCACHE_LINE_SIZE 4 // Size of a data cache line |
|
|
184 #define HAL_DCACHE_WAYS 2 // Associativity of the cache |
|
|
185 |
|
|
186 // Instruction cache |
|
|
187 #define HAL_ICACHE_SIZE 4096 // Size of cache in bytes |
|
|
188 #define HAL_ICACHE_LINE_SIZE 16 // Size of a cache line |
|
|
189 #define HAL_ICACHE_WAYS 1 // Associativity of the cache |
|
|
190 |
|
|
191 #define HAL_DCACHE_SETS (HAL_DCACHE_SIZE/(HAL_DCACHE_LINE_SIZE*HAL_DCACHE_WAYS)) |
|
|
192 #define HAL_ICACHE_SETS (HAL_ICACHE_SIZE/(HAL_ICACHE_LINE_SIZE*HAL_ICACHE_WAYS)) |
|
|
193 |
|
|
194 //----------------------------------------------------------------------------- |
|
|
195 // Global control of data cache |
|
|
196 |
|
|
197 // Enable the data cache |
|
|
198 #define HAL_DCACHE_ENABLE() \ |
|
|
199 { \ |
|
|
200 asm volatile ("mfc0 $2,$3;" \ |
|
|
201 "ori $2,$2,0x0010;" \ |
|
|
202 "mtc0 $2,$3;" \ |
|
|
203 : \ |
|
|
204 : \ |
|
|
205 : "$2" \ |
|
|
206 ); \ |
|
|
207 \ |
|
|
208 } |
|
|
209 |
|
|
210 // Disable the data cache |
|
|
211 #define HAL_DCACHE_DISABLE() \ |
|
|
212 { \ |
|
2
|
213 asm volatile ("mfc0 $2,$3;" \ |
|
0
|
214 "la $3,0xFFFFFFEF;" \ |
|
|
215 "and $2,$2,$3;" \ |
|
2
|
216 "mtc0 $2,$3;" \ |
|
0
|
217 : \ |
|
|
218 : \ |
|
|
219 : "$2", "$3" \ |
|
|
220 ); \ |
|
|
221 \ |
|
|
222 } |
|
|
223 |
|
|
224 // Invalidate the entire cache |
|
2
|
225 // The TX39 only has hit-invalidate on the DCACHE, not |
|
|
226 // index-invalidate, so we cannot just empty the cache out without |
|
|
227 // knowing what is in it. This is annoying. So, the best we can do is |
|
|
228 // fill the cache with data that is unlikely to be there |
|
|
229 // otherwise. Hence we read bytes from the ROM space since this is |
|
|
230 // most likely to be code, and will not get out of sync even if it is not. |
|
0
|
231 #define HAL_DCACHE_INVALIDATE_ALL() \ |
|
|
232 { \ |
|
2
|
233 CYG_BYTE volatile *addr = (CYG_BYTE *)(0x9fc00000); \ |
|
|
234 CYG_BYTE volatile tmp = 0; \ |
|
0
|
235 int i; \ |
|
2
|
236 for( i = 0; i < (HAL_DCACHE_SIZE*2); i += HAL_DCACHE_LINE_SIZE ) \ |
|
0
|
237 { \ |
|
2
|
238 tmp = addr[i]; \ |
|
0
|
239 } \ |
|
|
240 } |
|
|
241 |
|
|
242 // Synchronize the contents of the cache with memory. |
|
|
243 #define HAL_DCACHE_SYNC() HAL_DCACHE_INVALIDATE_ALL() |
|
|
244 |
|
|
245 // Set the data cache refill burst size |
|
|
246 //#define HAL_DCACHE_BURST_SIZE(_size_) |
|
|
247 |
|
|
248 // Set the data cache write mode |
|
|
249 //#define HAL_DCACHE_WRITE_MODE( _mode_ ) |
|
|
250 |
|
|
251 //#define HAL_DCACHE_WRITETHRU_MODE 0 |
|
|
252 //#define HAL_DCACHE_WRITEBACK_MODE 1 |
|
|
253 |
|
|
254 // Load the contents of the given address range into the data cache |
|
|
255 // and then lock the cache so that it stays there. |
|
|
256 #define HAL_DCACHE_LOCK(_base_, _size_) \ |
|
|
257 { \ |
|
|
258 asm volatile ("mfc0 $2,$7;" \ |
|
|
259 "ori $2,$2,0x0100;" \ |
|
|
260 "mtc0 $2,$7;" \ |
|
|
261 : \ |
|
|
262 : \ |
|
|
263 : "$2" \ |
|
|
264 ); \ |
|
|
265 } |
|
|
266 |
|
|
267 // Undo a previous lock operation |
|
|
268 #define HAL_DCACHE_UNLOCK(_base_, _size_) \ |
|
|
269 { \ |
|
|
270 asm volatile ("mfc0 $2,$7;" \ |
|
|
271 "la $3,0xFFFFFEFF;" \ |
|
|
272 "and $2,$2,$3;" \ |
|
|
273 "mtc0 $2,$7;" \ |
|
|
274 : \ |
|
|
275 : \ |
|
|
276 : "$2", "$3" \ |
|
|
277 ); \ |
|
|
278 } |
|
|
279 |
|
2
|
280 // Unlock entire cache |
|
|
281 #define HAL_DCACHE_UNLOCK_ALL() |
|
|
282 |
|
0
|
283 //----------------------------------------------------------------------------- |
|
|
284 // Data cache line control |
|
|
285 |
|
|
286 // Allocate cache lines for the given address range without reading its |
|
|
287 // contents from memory. |
|
|
288 //#define HAL_DCACHE_ALLOCATE( _base_ , _size_ ) |
|
|
289 |
|
|
290 // Write dirty cache lines to memory and invalidate the cache entries |
|
|
291 // for the given address range. |
|
|
292 //#define HAL_DCACHE_FLUSH( _base_ , _size_ ) |
|
|
293 |
|
|
294 // Invalidate cache lines in the given range without writing to memory. |
|
|
295 #define HAL_DCACHE_INVALIDATE( _base_ , _asize_ ) \ |
|
|
296 { \ |
|
|
297 register CYG_ADDRESS _addr_ = (CYG_ADDRESS)(_base_); \ |
|
|
298 register CYG_WORD _size_ = (_asize_); \ |
|
|
299 HAL_DCACHE_DISABLE(); \ |
|
|
300 for( ; _addr_ <= _addr_+_size_; _addr_ += HAL_DCACHE_LINE_SIZE ) \ |
|
|
301 { \ |
|
2
|
302 asm volatile ("cache 17,0(%0)" : : "r"(_addr_) ); \ |
|
0
|
303 } \ |
|
|
304 HAL_DCACHE_ENABLE(); \ |
|
|
305 } |
|
|
306 |
|
|
307 // Write dirty cache lines to memory for the given address range. |
|
|
308 //#define HAL_DCACHE_STORE( _base_ , _size_ ) |
|
|
309 |
|
|
310 // Preread the given range into the cache with the intention of reading |
|
|
311 // from it later. |
|
|
312 //#define HAL_DCACHE_READ_HINT( _base_ , _size_ ) |
|
|
313 |
|
|
314 // Preread the given range into the cache with the intention of writing |
|
|
315 // to it later. |
|
|
316 //#define HAL_DCACHE_WRITE_HINT( _base_ , _size_ ) |
|
|
317 |
|
|
318 // Allocate and zero the cache lines associated with the given range. |
|
|
319 //#define HAL_DCACHE_ZERO( _base_ , _size_ ) |
|
|
320 |
|
|
321 //----------------------------------------------------------------------------- |
|
|
322 // Global control of Instruction cache |
|
|
323 |
|
|
324 // Enable the instruction cache |
|
|
325 #define HAL_ICACHE_ENABLE() \ |
|
|
326 { \ |
|
|
327 asm volatile ("mfc0 $2,$3;" \ |
|
|
328 "ori $2,$2,0x0020;" \ |
|
|
329 "mtc0 $2,$3;" \ |
|
|
330 : \ |
|
|
331 : \ |
|
|
332 : "$2" \ |
|
|
333 ); \ |
|
|
334 \ |
|
|
335 } |
|
|
336 |
|
|
337 // Disable the instruction cache |
|
|
338 #define HAL_ICACHE_DISABLE() \ |
|
|
339 { \ |
|
|
340 asm volatile ("mfc0 $2,$3;" \ |
|
|
341 "la $3,0xFFFFFFDF;" \ |
|
|
342 "and $2,$2,$3;" \ |
|
|
343 "mtc0 $2,$3;" \ |
|
|
344 : \ |
|
|
345 : \ |
|
|
346 : "$2", "$3" \ |
|
|
347 ); \ |
|
|
348 \ |
|
|
349 } |
|
|
350 |
|
|
351 // Invalidate the entire cache |
|
|
352 #define HAL_ICACHE_INVALIDATE_ALL() \ |
|
|
353 { \ |
|
2
|
354 register CYG_ADDRESS _addr_; \ |
|
0
|
355 HAL_ICACHE_DISABLE(); \ |
|
2
|
356 for( _addr_ = 0; _addr_ < HAL_ICACHE_SIZE; _addr_ += HAL_ICACHE_LINE_SIZE ) \ |
|
0
|
357 { \ |
|
2
|
358 asm volatile ("cache 0,0(%0)" : : "r"(_addr_) ); \ |
|
0
|
359 } \ |
|
|
360 HAL_ICACHE_ENABLE(); \ |
|
|
361 } |
|
|
362 |
|
|
363 // Synchronize the contents of the cache with memory. |
|
|
364 #define HAL_ICACHE_SYNC() HAL_ICACHE_INVALIDATE_ALL() |
|
|
365 |
|
|
366 // Set the instruction cache refill burst size |
|
|
367 //#define HAL_ICACHE_BURST_SIZE(_size_) |
|
|
368 |
|
|
369 // Load the contents of the given address range into the instruction cache |
|
|
370 // and then lock the cache so that it stays there. |
|
|
371 #define HAL_ICACHE_LOCK(_base_, _size_) \ |
|
|
372 { \ |
|
|
373 asm volatile ("mfc0 $2,$7;" \ |
|
|
374 "ori $2,$2,0x0200;" \ |
|
|
375 "mtc0 $2,$7;" \ |
|
|
376 : \ |
|
|
377 : \ |
|
|
378 : "$2" \ |
|
|
379 ); \ |
|
|
380 } |
|
|
381 |
|
|
382 // Undo a previous lock operation |
|
|
383 #define HAL_ICACHE_UNLOCK(_base_, _size_) \ |
|
|
384 { \ |
|
|
385 asm volatile ("mfc0 $2,$7;" \ |
|
|
386 "la $3,0xFFFFFDFF;" \ |
|
|
387 "and $2,$2,$3;" \ |
|
|
388 "mtc0 $2,$7;" \ |
|
|
389 : \ |
|
|
390 : \ |
|
|
391 : "$2", "$3" \ |
|
|
392 ); \ |
|
|
393 } |
|
|
394 |
|
2
|
395 // Unlock entire cache |
|
|
396 #define HAL_ICACHE_UNLOCK_ALL() |
|
|
397 |
|
0
|
398 //----------------------------------------------------------------------------- |
|
|
399 // Instruction cache line control |
|
|
400 |
|
|
401 // Invalidate cache lines in the given range without writing to memory. |
|
|
402 #define HAL_ICACHE_INVALIDATE( _base_ , _asize_ ) \ |
|
|
403 { \ |
|
|
404 register CYG_ADDRESS _addr_ = (CYG_ADDRESS)(_base_); \ |
|
|
405 register CYG_WORD _size_ = (_asize_); \ |
|
|
406 HAL_ICACHE_DISABLE(); \ |
|
|
407 for( ; _addr_ <= _addr_+_size_; _addr_ += HAL_ICACHE_LINE_SIZE ) \ |
|
|
408 { \ |
|
2
|
409 asm volatile ("cache 0,0(%0)" : : "r"(_addr_) ); \ |
|
0
|
410 } \ |
|
|
411 HAL_ICACHE_ENABLE(); \ |
|
|
412 } |
|
|
413 |
|
|
414 #endif |
|
|
415 |
|
|
416 //----------------------------------------------------------------------------- |
|
2
|
417 // Check that a supported configuration has actually defined some macros. |
|
|
418 |
|
|
419 #ifndef HAL_DCACHE_ENABLE |
|
|
420 |
|
|
421 #error Unsupported MIPS configuration |
|
|
422 |
|
|
423 #endif |
|
|
424 |
|
|
425 //----------------------------------------------------------------------------- |
|
0
|
426 #endif // ifndef CYGONCE_HAL_CACHE_H |
|
|
427 // End of hal_cache.h |