1 /* $NetBSD: cpufunc_asm_xscale.S,v 1.16 2002/08/17 16:36:32 thorpej Exp $ */
4 * Copyright (c) 2001, 2002 Wasabi Systems, Inc.
7 * Written by Allen Briggs and Jason R. Thorpe for Wasabi Systems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed for the NetBSD Project by
20 * Wasabi Systems, Inc.
21 * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22 * or promote products derived from this software without specific prior
25 * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
40 * Copyright (c) 2001 Matt Thomas.
41 * Copyright (c) 1997,1998 Mark Brinicombe.
42 * Copyright (c) 1997 Causality Limited
43 * All rights reserved.
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by Causality Limited.
56 * 4. The name of Causality Limited may not be used to endorse or promote
57 * products derived from this software without specific prior written
60 * THIS SOFTWARE IS PROVIDED BY CAUSALITY LIMITED ``AS IS'' AND ANY EXPRESS
61 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
62 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
63 * DISCLAIMED. IN NO EVENT SHALL CAUSALITY LIMITED BE LIABLE FOR ANY DIRECT,
64 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
65 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
66 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72 * XScale assembly functions for CPU / MMU / TLB specific operations
75 #include <machine/asm.h>
76 __FBSDID("$FreeBSD$");
79 * Size of the XScale core D-cache.
81 #define DCACHE_SIZE 0x00008000
83 .Lblock_userspace_access:
84 .word _C_LABEL(block_userspace_access)
87 * CPWAIT -- Canonical method to wait for CP15 update.
88 * From: Intel 80200 manual, section 2.3.3.
90 * NOTE: Clobbers the specified temp reg.
92 #define CPWAIT_BRANCH \
96 mrc p15, 0, tmp, c2, c0, 0 /* arbitrary read of CP15 */ ;\
97 mov tmp, tmp /* wait for it to complete */ ;\
98 CPWAIT_BRANCH /* branch to next insn */
100 #define CPWAIT_AND_RETURN_SHIFTER lsr #32
102 #define CPWAIT_AND_RETURN(tmp) \
103 mrc p15, 0, tmp, c2, c0, 0 /* arbitrary read of CP15 */ ;\
104 /* Wait for it to complete and branch to the return address */ \
105 sub pc, lr, tmp, CPWAIT_AND_RETURN_SHIFTER
108 CPWAIT_AND_RETURN(r0)
111 * We need a separate cpu_control() entry point, since we have to
112 * invalidate the Branch Target Buffer in the event the BPRD bit
113 * changes in the control register.
115 ENTRY(xscale_control)
116 mrc p15, 0, r3, c1, c0, 0 /* Read the control register */
117 bic r2, r3, r0 /* Clear bits */
118 eor r2, r2, r1 /* XOR bits */
120 teq r2, r3 /* Only write if there was a change */
121 mcrne p15, 0, r0, c7, c5, 6 /* Invalidate the BTB */
122 mcrne p15, 0, r2, c1, c0, 0 /* Write new control register */
123 mov r0, r3 /* Return old value */
125 CPWAIT_AND_RETURN(r1)
128 * Functions to set the MMU Translation Table Base register
130 * We need to clean and flush the cache as it uses virtual
131 * addresses that are about to change.
134 #ifdef CACHE_CLEAN_BLOCK_INTR
136 orr r1, r3, #(I32_bit | F32_bit)
139 ldr r3, .Lblock_userspace_access
144 stmfd sp!, {r0-r3, lr}
145 bl _C_LABEL(xscale_cache_cleanID)
146 mcr p15, 0, r0, c7, c5, 0 /* invalidate I$ and BTB */
147 mcr p15, 0, r0, c7, c10, 4 /* drain write and fill buffer */
151 ldmfd sp!, {r0-r3, lr}
154 mcr p15, 0, r0, c2, c0, 0
156 /* If we have updated the TTB we must flush the TLB */
157 mcr p15, 0, r0, c8, c7, 0 /* invalidate I+D TLB */
159 /* The cleanID above means we only need to flush the I cache here */
160 mcr p15, 0, r0, c7, c5, 0 /* invalidate I$ and BTB */
164 #ifdef CACHE_CLEAN_BLOCK_INTR
175 ENTRY(xscale_tlb_flushID_SE)
176 mcr p15, 0, r0, c8, c6, 1 /* flush D tlb single entry */
177 mcr p15, 0, r0, c8, c5, 1 /* flush I tlb single entry */
178 CPWAIT_AND_RETURN(r0)
183 ENTRY(xscale_cache_flushID)
184 mcr p15, 0, r0, c7, c7, 0 /* flush I+D cache */
185 CPWAIT_AND_RETURN(r0)
187 ENTRY(xscale_cache_flushI)
188 mcr p15, 0, r0, c7, c5, 0 /* flush I cache */
189 CPWAIT_AND_RETURN(r0)
191 ENTRY(xscale_cache_flushD)
192 mcr p15, 0, r0, c7, c6, 0 /* flush D cache */
193 CPWAIT_AND_RETURN(r0)
195 ENTRY(xscale_cache_flushI_SE)
196 mcr p15, 0, r0, c7, c5, 1 /* flush I cache single entry */
197 CPWAIT_AND_RETURN(r0)
199 ENTRY(xscale_cache_flushD_SE)
201 * Errata (rev < 2): Must clean-dcache-line to an address
202 * before invalidate-dcache-line to an address, or dirty
203 * bits will not be cleared in the dcache array.
205 mcr p15, 0, r0, c7, c10, 1
206 mcr p15, 0, r0, c7, c6, 1 /* flush D cache single entry */
207 CPWAIT_AND_RETURN(r0)
209 ENTRY(xscale_cache_cleanD_E)
210 mcr p15, 0, r0, c7, c10, 1 /* clean D cache entry */
211 CPWAIT_AND_RETURN(r0)
214 * Information for the XScale cache clean/purge functions:
216 * * Virtual address of the memory region to use
217 * * Size of memory region
219 * Note the virtual address for the Data cache clean operation
220 * does not need to be backed by physical memory, since no loads
221 * will actually be performed by the allocate-line operation.
223 * Note that the Mini-Data cache MUST be cleaned by executing
224 * loads from memory mapped into a region reserved exclusively
225 * for cleaning of the Mini-Data cache.
229 .global _C_LABEL(xscale_cache_clean_addr)
230 _C_LABEL(xscale_cache_clean_addr):
233 .global _C_LABEL(xscale_cache_clean_size)
234 _C_LABEL(xscale_cache_clean_size):
237 .global _C_LABEL(xscale_minidata_clean_addr)
238 _C_LABEL(xscale_minidata_clean_addr):
241 .global _C_LABEL(xscale_minidata_clean_size)
242 _C_LABEL(xscale_minidata_clean_size):
247 .Lxscale_cache_clean_addr:
248 .word _C_LABEL(xscale_cache_clean_addr)
249 .Lxscale_cache_clean_size:
250 .word _C_LABEL(xscale_cache_clean_size)
252 .Lxscale_minidata_clean_addr:
253 .word _C_LABEL(xscale_minidata_clean_addr)
254 .Lxscale_minidata_clean_size:
255 .word _C_LABEL(xscale_minidata_clean_size)
257 #ifdef CACHE_CLEAN_BLOCK_INTR
258 #define XSCALE_CACHE_CLEAN_BLOCK \
260 orr r0, r3, #(I32_bit | F32_bit) ; \
263 #define XSCALE_CACHE_CLEAN_UNBLOCK \
266 #define XSCALE_CACHE_CLEAN_BLOCK \
267 ldr r3, .Lblock_userspace_access ; \
272 #define XSCALE_CACHE_CLEAN_UNBLOCK \
274 #endif /* CACHE_CLEAN_BLOCK_INTR */
276 #define XSCALE_CACHE_CLEAN_PROLOGUE \
277 XSCALE_CACHE_CLEAN_BLOCK ; \
278 ldr r2, .Lxscale_cache_clean_addr ; \
279 ldmia r2, {r0, r1} ; \
283 * The XScale core has a strange cache eviction bug, which \
284 * requires us to use 2x the cache size for the cache clean \
285 * and for that area to be aligned to 2 * cache size. \
287 * The work-around is to use 2 areas for cache clean, and to \
288 * alternate between them whenever this is done. No one knows \
289 * why the work-around works (mmm!). \
291 eor r0, r0, #(DCACHE_SIZE) ; \
295 #define XSCALE_CACHE_CLEAN_EPILOGUE \
296 XSCALE_CACHE_CLEAN_UNBLOCK
298 ENTRY_NP(xscale_cache_syncI)
299 ENTRY_NP(xscale_cache_purgeID)
300 mcr p15, 0, r0, c7, c5, 0 /* flush I cache (D cleaned below) */
301 ENTRY_NP(xscale_cache_cleanID)
302 ENTRY_NP(xscale_cache_purgeD)
303 ENTRY(xscale_cache_cleanD)
304 XSCALE_CACHE_CLEAN_PROLOGUE
307 mcr p15, 0, r0, c7, c2, 5 /* allocate cache line */
313 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
317 XSCALE_CACHE_CLEAN_EPILOGUE
321 * Clean the mini-data cache.
323 * It's expected that we only use the mini-data cache for
324 * kernel addresses, so there is no need to purge it on
325 * context switch, and no need to prevent userspace access
328 ENTRY(xscale_cache_clean_minidata)
329 ldr r2, .Lxscale_minidata_clean_addr
335 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
337 CPWAIT_AND_RETURN(r1)
339 ENTRY(xscale_cache_purgeID_E)
340 mcr p15, 0, r0, c7, c10, 1 /* clean D cache entry */
342 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
343 mcr p15, 0, r0, c7, c5, 1 /* flush I cache single entry */
344 mcr p15, 0, r0, c7, c6, 1 /* flush D cache single entry */
345 CPWAIT_AND_RETURN(r1)
347 ENTRY(xscale_cache_purgeD_E)
348 mcr p15, 0, r0, c7, c10, 1 /* clean D cache entry */
350 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
351 mcr p15, 0, r0, c7, c6, 1 /* flush D cache single entry */
352 CPWAIT_AND_RETURN(r1)
357 /* xscale_cache_syncI is identical to xscale_cache_purgeID */
359 ENTRY(xscale_cache_cleanID_rng)
360 ENTRY(xscale_cache_cleanD_rng)
362 bcs _C_LABEL(xscale_cache_cleanID)
368 1: mcr p15, 0, r0, c7, c10, 1 /* clean D cache entry */
375 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
377 CPWAIT_AND_RETURN(r0)
379 ENTRY(xscale_cache_purgeID_rng)
381 bcs _C_LABEL(xscale_cache_purgeID)
387 1: mcr p15, 0, r0, c7, c10, 1 /* clean D cache entry */
388 mcr p15, 0, r0, c7, c6, 1 /* flush D cache single entry */
389 mcr p15, 0, r0, c7, c5, 1 /* flush I cache single entry */
396 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
398 CPWAIT_AND_RETURN(r0)
400 ENTRY(xscale_cache_purgeD_rng)
402 bcs _C_LABEL(xscale_cache_purgeD)
408 1: mcr p15, 0, r0, c7, c10, 1 /* clean D cache entry */
409 mcr p15, 0, r0, c7, c6, 1 /* flush D cache single entry */
416 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
418 CPWAIT_AND_RETURN(r0)
420 ENTRY(xscale_cache_syncI_rng)
422 bcs _C_LABEL(xscale_cache_syncI)
428 1: mcr p15, 0, r0, c7, c10, 1 /* clean D cache entry */
429 mcr p15, 0, r0, c7, c5, 1 /* flush I cache single entry */
436 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
438 CPWAIT_AND_RETURN(r0)
440 ENTRY(xscale_cache_flushD_rng)
445 1: mcr p15, 0, r0, c7, c6, 1 /* flush D cache single entry */
450 mcr p15, 0, r0, c7, c10, 4 /* drain write buffer */
452 CPWAIT_AND_RETURN(r0)
457 * These is the CPU-specific parts of the context switcher cpu_switch()
458 * These functions actually perform the TTB reload.
460 * NOTE: Special calling convention
461 * r1, r4-r13 must be preserved
463 ENTRY(xscale_context_switch)
465 * CF_CACHE_PURGE_ID will *ALWAYS* be called prior to this.
466 * Thus the data cache will contain only kernel data and the
467 * instruction cache will contain only kernel code, and all
468 * kernel mappings are shared by all processes.
472 mcr p15, 0, r0, c2, c0, 0
474 /* If we have updated the TTB we must flush the TLB */
475 mcr p15, 0, r0, c8, c7, 0 /* flush the I+D tlb */
477 CPWAIT_AND_RETURN(r0)
482 * This is called when there is nothing on any of the run queues.
483 * We go into IDLE mode so that any IRQ or FIQ will awaken us.
485 * If this is called with anything other than ARM_SLEEP_MODE_IDLE,
488 ENTRY(xscale_cpu_sleep)
492 mcr p14, 0, r0, c7, c0, 0