1 #ifndef CYGONCE_IMP_CACHE_H
2 #define CYGONCE_IMP_CACHE_H
4 //=============================================================================
8 // HAL cache control API
10 //=============================================================================
11 //####ECOSGPLCOPYRIGHTBEGIN####
12 // -------------------------------------------
13 // This file is part of eCos, the Embedded Configurable Operating System.
14 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
16 // eCos is free software; you can redistribute it and/or modify it under
17 // the terms of the GNU General Public License as published by the Free
18 // Software Foundation; either version 2 or (at your option) any later version.
20 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
21 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 // You should have received a copy of the GNU General Public License along
26 // with eCos; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 // As a special exception, if other files instantiate templates or use macros
30 // or inline functions from this file, or you compile this file and link it
31 // with other works to produce a work based on this file, this file does not
32 // by itself cause the resulting work to be covered by the GNU General Public
33 // License. However the source code for this file must still be made available
34 // in accordance with section (3) of the GNU General Public License.
36 // This exception does not invalidate any other reasons why a work based on
37 // this file might be covered by the GNU General Public License.
39 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
40 // at http://sources.redhat.com/ecos/ecos-license/
41 // -------------------------------------------
42 //####ECOSGPLCOPYRIGHTEND####
43 //=============================================================================
44 //#####DESCRIPTIONBEGIN####
47 // Contributors: nickg, dmoseley
49 // Purpose: Cache control API
50 // Description: The macros defined here provide the HAL APIs for handling
51 // cache control operations.
53 // #include <cyg/hal/imp_cache.h>
57 //####DESCRIPTIONEND####
59 //=============================================================================
61 #include <pkgconf/hal.h>
62 #include <cyg/infra/cyg_type.h>
64 #include <cyg/hal/mips-regs.h>
65 #include <cyg/hal/hal_arch.h>
66 #include <cyg/hal/plf_cache.h>
67 #include <cyg/hal/var_arch.h>
69 #ifdef CYGHWR_HAL_MIPS_MIPS64_CORE_5K
71 //-----------------------------------------------------------------------------
75 #define HAL_DCACHE_SIZE 8192 // Size of data cache in bytes
76 #define HAL_DCACHE_LINE_SIZE 32 // Size of a data cache line
77 #define HAL_DCACHE_WAYS 2 // Associativity of the cache
80 #define HAL_ICACHE_SIZE 8192 // Size of cache in bytes
81 #define HAL_ICACHE_LINE_SIZE 32 // Size of a cache line
82 #define HAL_ICACHE_WAYS 2 // Associativity of the cache
84 #define HAL_DCACHE_SETS (HAL_DCACHE_SIZE/(HAL_DCACHE_LINE_SIZE*HAL_DCACHE_WAYS))
85 #define HAL_ICACHE_SETS (HAL_ICACHE_SIZE/(HAL_ICACHE_LINE_SIZE*HAL_ICACHE_WAYS))
87 #define HAL_DCACHE_WRITETHRU_MODE 1
88 #define HAL_DCACHE_WRITEBACK_MODE 0
92 #error Unknown MIPS32 Variant
96 //-----------------------------------------------------------------------------
97 // General cache defines.
98 #define HAL_CLEAR_TAGLO() asm volatile (" mtc0 $0, $28;" \
102 #define HAL_CLEAR_TAGHI() asm volatile (" mtc0 $0, $29;" \
107 /* Cache instruction opcodes */
108 #define HAL_CACHE_OP(which, op) (which | (op << 2))
110 #define HAL_WHICH_ICACHE 0x0
111 #define HAL_WHICH_DCACHE 0x1
113 #define HAL_INDEX_INVALIDATE 0x0
114 #define HAL_INDEX_LOAD_TAG 0x1
115 #define HAL_INDEX_STORE_TAG 0x2
116 #define HAL_HIT_INVALIDATE 0x4
117 #define HAL_ICACHE_FILL 0x5
118 #define HAL_DCACHE_HIT_INVALIDATE 0x5
119 #define HAL_DCACHE_HIT_WRITEBACK 0x6
120 #define HAL_FETCH_AND_LOCK 0x7
122 //-----------------------------------------------------------------------------
123 // Global control of data cache
125 // Invalidate the entire cache
126 #define HAL_DCACHE_INVALIDATE_ALL_DEFINED
127 #define HAL_DCACHE_INVALIDATE_ALL() \
129 register volatile CYG_BYTE *addr; \
132 for (addr = (CYG_BYTE *)CYGARC_KSEG_CACHED_BASE; \
133 addr < (CYG_BYTE *)(CYGARC_KSEG_CACHED_BASE + HAL_DCACHE_SIZE); \
134 addr += HAL_DCACHE_LINE_SIZE ) \
136 asm volatile (" cache %0, 0(%1)" \
138 : "I" (HAL_CACHE_OP(HAL_WHICH_DCACHE, HAL_INDEX_STORE_TAG)), \
143 // Synchronize the contents of the cache with memory.
144 extern void hal_dcache_sync(void);
145 #define HAL_DCACHE_SYNC_DEFINED
146 #define HAL_DCACHE_SYNC() hal_dcache_sync()
148 // Set the data cache refill burst size
149 //#define HAL_DCACHE_BURST_SIZE(_asize_)
151 // Set the data cache write mode
152 //#define HAL_DCACHE_WRITE_MODE( _mode_ )
154 // Load the contents of the given address range into the data cache
155 // and then lock the cache so that it stays there.
156 #define HAL_DCACHE_LOCK_DEFINED
157 #define HAL_DCACHE_LOCK(_base_, _asize_) \
159 register CYG_ADDRESS _baddr_ = (CYG_ADDRESS)(_base_); \
160 register CYG_ADDRESS _addr_ = (CYG_ADDRESS)(_base_); \
161 register CYG_WORD _size_ = (_asize_); \
162 for( ; _addr_ <= _baddr_+_size_; _addr_ += HAL_DCACHE_LINE_SIZE ) \
163 asm volatile (" cache %0, 0(%1)" \
165 : "I" (HAL_CACHE_OP(HAL_WHICH_DCACHE, HAL_FETCH_AND_LOCK)), \
169 // Undo a previous lock operation
170 #define HAL_DCACHE_UNLOCK_DEFINED
171 #define HAL_DCACHE_UNLOCK(_base_, _asize_) \
173 register CYG_ADDRESS _baddr_ = (CYG_ADDRESS)(_base_); \
174 register CYG_ADDRESS _addr_ = (CYG_ADDRESS)(_base_); \
175 register CYG_WORD _size_ = (_asize_); \
176 for( ; _addr_ <= _baddr_+_size_; _addr_ += HAL_DCACHE_LINE_SIZE ) \
177 asm volatile (" cache %0, 0(%1)" \
179 : "I" (HAL_CACHE_OP(HAL_WHICH_DCACHE, HAL_HIT_INVALIDATE)), \
183 // Unlock entire cache
184 #define HAL_DCACHE_UNLOCK_ALL_DEFINED
185 #define HAL_DCACHE_UNLOCK_ALL() HAL_DCACHE_UNLOCK(0,HAL_DCACHE_SIZE)
188 //-----------------------------------------------------------------------------
189 // Data cache line control
191 // Allocate cache lines for the given address range without reading its
192 // contents from memory.
193 //#define HAL_DCACHE_ALLOCATE( _base_ , _asize_ )
195 // Write dirty cache lines to memory and invalidate the cache entries
196 // for the given address range.
197 #define HAL_DCACHE_FLUSH_DEFINED
198 #if HAL_DCACHE_WRITETHRU_MODE == 1
199 // No need to flush a writethrough cache
200 #define HAL_DCACHE_FLUSH( _base_ , _asize_ )
202 #error HAL_DCACHE_FLUSH undefined for MIPS32 writeback cache
205 // Write dirty cache lines to memory for the given address range.
206 #define HAL_DCACHE_STORE_DEFINED
207 #if HAL_DCACHE_WRITETHRU_MODE == 1
208 // No need to store a writethrough cache
209 #define HAL_DCACHE_STORE( _base_ , _asize_ )
211 #error HAL_DCACHE_STORE undefined for MIPS32 writeback cache
214 // Invalidate cache lines in the given range without writing to memory.
215 #define HAL_DCACHE_INVALIDATE_DEFINED
216 #define HAL_DCACHE_INVALIDATE( _base_ , _asize_ ) \
218 register CYG_ADDRESS _baddr_ = (CYG_ADDRESS)(_base_); \
219 register CYG_ADDRESS _addr_ = (CYG_ADDRESS)(_base_); \
220 register CYG_WORD _size_ = (_asize_); \
221 for( ; _addr_ <= _baddr_+_size_; _addr_ += HAL_DCACHE_LINE_SIZE ) \
222 asm volatile (" cache %0, 0(%1)" \
224 : "I" (HAL_CACHE_OP(HAL_WHICH_DCACHE, HAL_HIT_INVALIDATE)), \
233 //-----------------------------------------------------------------------------
234 // Global control of Instruction cache
236 // Invalidate the entire cache
237 #define HAL_ICACHE_INVALIDATE_ALL_DEFINED
238 #define HAL_ICACHE_INVALIDATE_ALL() \
240 register volatile CYG_BYTE *addr; \
243 for (addr = (CYG_BYTE *)CYGARC_KSEG_CACHED_BASE; \
244 addr < (CYG_BYTE *)(CYGARC_KSEG_CACHED_BASE + HAL_ICACHE_SIZE); \
245 addr += HAL_ICACHE_LINE_SIZE ) \
247 asm volatile (" cache %0, 0(%1)" \
249 : "I" (HAL_CACHE_OP(HAL_WHICH_ICACHE, HAL_INDEX_STORE_TAG)), \
254 // Synchronize the contents of the cache with memory.
255 extern void hal_icache_sync(void);
256 #define HAL_ICACHE_SYNC_DEFINED
257 #define HAL_ICACHE_SYNC() hal_icache_sync()
259 // Set the instruction cache refill burst size
260 //#define HAL_ICACHE_BURST_SIZE(_asize_)
262 // Load the contents of the given address range into the data cache
263 // and then lock the cache so that it stays there.
264 #define HAL_ICACHE_LOCK_DEFINED
265 #define HAL_ICACHE_LOCK(_base_, _asize_) \
267 register CYG_ADDRESS _baddr_ = (CYG_ADDRESS)(_base_); \
268 register CYG_ADDRESS _addr_ = (CYG_ADDRESS)(_base_); \
269 register CYG_WORD _size_ = (_asize_); \
270 for( ; _addr_ <= _baddr_+_size_; _addr_ += HAL_ICACHE_LINE_SIZE ) \
271 asm volatile (" cache %0, 0(%1)" \
273 : "I" (HAL_CACHE_OP(HAL_WHICH_ICACHE, HAL_FETCH_AND_LOCK)), \
277 // Undo a previous lock operation
278 #define HAL_ICACHE_UNLOCK_DEFINED
279 #define HAL_ICACHE_UNLOCK(_base_, _asize_) \
281 register CYG_ADDRESS _baddr_ = (CYG_ADDRESS)(_base_); \
282 register CYG_ADDRESS _addr_ = (CYG_ADDRESS)(_base_); \
283 register CYG_WORD _size_ = (_asize_); \
284 for( ; _addr_ <= _baddr_+_size_; _addr_ += HAL_ICACHE_LINE_SIZE ) \
285 asm volatile (" cache %0, 0(%1)" \
287 : "I" (HAL_CACHE_OP(HAL_WHICH_ICACHE, HAL_HIT_INVALIDATE)), \
291 // Unlock entire cache
292 #define HAL_ICACHE_UNLOCK_ALL_DEFINED
293 #define HAL_ICACHE_UNLOCK_ALL() HAL_ICACHE_UNLOCK(0,HAL_ICACHE_SIZE)
295 //-----------------------------------------------------------------------------
296 // Instruction cache line control
298 // Invalidate cache lines in the given range without writing to memory.
299 #define HAL_ICACHE_INVALIDATE_DEFINED
300 #define HAL_ICACHE_INVALIDATE( _base_ , _asize_ ) \
302 register CYG_ADDRESS _baddr_ = (CYG_ADDRESS)(_base_); \
303 register CYG_ADDRESS _addr_ = (CYG_ADDRESS)(_base_); \
304 register CYG_WORD _size_ = (_asize_); \
305 for( ; _addr_ <= _baddr_+_size_; _addr_ += HAL_ICACHE_LINE_SIZE ) \
306 asm volatile (" cache %0, 0(%1)" \
308 : "I" (HAL_CACHE_OP(HAL_WHICH_ICACHE, HAL_HIT_INVALIDATE)), \
312 //-----------------------------------------------------------------------------
313 #endif // ifndef CYGONCE_IMP_CACHE_H
314 // End of imp_cache.h