1152723Scognet/*	$NetBSD: cpufunc_asm_arm9.S,v 1.3 2004/01/26 15:54:16 rearnsha Exp $	*/
2129198Scognet
3152723Scognet/*
4152723Scognet * Copyright (c) 2001, 2004 ARM Limited
5129198Scognet * All rights reserved.
6129198Scognet *
7129198Scognet * Redistribution and use in source and binary forms, with or without
8129198Scognet * modification, are permitted provided that the following conditions
9129198Scognet * are met:
10129198Scognet * 1. Redistributions of source code must retain the above copyright
11129198Scognet *    notice, this list of conditions and the following disclaimer.
12129198Scognet * 2. Redistributions in binary form must reproduce the above copyright
13129198Scognet *    notice, this list of conditions and the following disclaimer in the
14129198Scognet *    documentation and/or other materials provided with the distribution.
15129198Scognet * 3. The name of the company may not be used to endorse or promote
16129198Scognet *    products derived from this software without specific prior written
17129198Scognet *    permission.
18129198Scognet *
19129198Scognet * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
20129198Scognet * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21129198Scognet * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22129198Scognet * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23129198Scognet * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24129198Scognet * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25129198Scognet * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26129198Scognet * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27129198Scognet * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28129198Scognet * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29129198Scognet * SUCH DAMAGE.
30129198Scognet *
31129198Scognet * ARM9 assembly functions for CPU / MMU / TLB specific operations
32129198Scognet */
33236991Simp
34129198Scognet#include <machine/asm.h>
35129198Scognet__FBSDID("$FreeBSD$");
36129198Scognet
37129198Scognet/*
38129198Scognet * Functions to set the MMU Translation Table Base register
39129198Scognet *
40129198Scognet * We need to clean and flush the cache as it uses virtual
41129198Scognet * addresses that are about to change.
42129198Scognet */
43129198ScognetENTRY(arm9_setttb)
44152723Scognet	stmfd	sp!, {r0, lr}
45152723Scognet	bl	_C_LABEL(arm9_idcache_wbinv_all)
46152723Scognet	ldmfd	sp!, {r0, lr}
47129198Scognet
48129198Scognet	mcr	p15, 0, r0, c2, c0, 0	/* load new TTB */
49129198Scognet
50129198Scognet	mcr	p15, 0, r0, c8, c7, 0	/* invalidate I+D TLBs */
51152723Scognet	mov	pc, lr
52248361SandrewEND(arm9_setttb)
53129198Scognet
54129198Scognet/*
55129198Scognet * TLB functions
56129198Scognet */
57129198ScognetENTRY(arm9_tlb_flushID_SE)
58129198Scognet	mcr	p15, 0, r0, c8, c6, 1	/* flush D tlb single entry */
59129198Scognet	mcr	p15, 0, r0, c8, c5, 1	/* flush I tlb single entry */
60152723Scognet	mov	pc, lr
61248361SandrewEND(arm9_tlb_flushID_SE)
62129198Scognet
63129198Scognet/*
64152723Scognet * Cache operations.  For the entire cache we use the set/index
65152723Scognet * operations.
66129198Scognet */
67152723Scognet	s_max	.req r0
68152723Scognet	i_max	.req r1
69152723Scognet	s_inc	.req r2
70152723Scognet	i_inc	.req r3
71129198Scognet
72152723ScognetENTRY_NP(arm9_icache_sync_range)
73152723Scognet	ldr	ip, .Larm9_line_size
74152723Scognet	cmp	r1, #0x4000
75152723Scognet	bcs	.Larm9_icache_sync_all
76152723Scognet	ldr	ip, [ip]
77152723Scognet	sub	r3, ip, #1
78152723Scognet	and	r2, r0, r3
79152723Scognet	add	r1, r1, r2
80152723Scognet	bic	r0, r0, r3
81152723Scognet.Larm9_sync_next:
82152723Scognet	mcr	p15, 0, r0, c7, c5, 1	/* Invalidate I cache SE with VA */
83152723Scognet	mcr	p15, 0, r0, c7, c10, 1	/* Clean D cache SE with VA */
84152723Scognet	add	r0, r0, ip
85152723Scognet	subs	r1, r1, ip
86246001Sian	bhi	.Larm9_sync_next
87152723Scognet	mov	pc, lr
88129198Scognet
89152723Scognet.Larm9_icache_sync_all:
90152723Scognet	/*
91152723Scognet	 * We assume that the code here can never be out of sync with the
92152723Scognet	 * dcache, so that we can safely flush the Icache and fall through
93152723Scognet	 * into the Dcache cleaning code.
94152723Scognet	 */
95152723Scognet	mcr	p15, 0, r0, c7, c5, 0	/* Flush I cache */
96152723Scognet	/* Fall through to clean Dcache. */
97129198Scognet
98152723Scognet.Larm9_dcache_wb:
99152723Scognet	ldr	ip, .Larm9_cache_data
100152723Scognet	ldmia	ip, {s_max, i_max, s_inc, i_inc}
101152723Scognet.Lnext_set:
102152723Scognet	orr	ip, s_max, i_max
103152723Scognet.Lnext_index:
104152723Scognet	mcr	p15, 0, ip, c7, c10, 2	/* Clean D cache SE with Set/Index */
105246001Sian	subs	ip, ip, i_inc
106246001Sian	bhs	.Lnext_index		/* Next index */
107152723Scognet	subs	s_max, s_max, s_inc
108246001Sian	bhs	.Lnext_set		/* Next set */
109152723Scognet	mov	pc, lr
110295207SmmelEND(arm9_icache_sync_range)
111129198Scognet
112152723Scognet.Larm9_line_size:
113152723Scognet	.word	_C_LABEL(arm_pdcache_line_size)
114129198Scognet
115152723ScognetENTRY(arm9_dcache_wb_range)
116152723Scognet	ldr	ip, .Larm9_line_size
117152723Scognet	cmp	r1, #0x4000
118152723Scognet	bcs	.Larm9_dcache_wb
119152723Scognet	ldr	ip, [ip]
120152723Scognet	sub	r3, ip, #1
121152723Scognet	and	r2, r0, r3
122152723Scognet	add	r1, r1, r2
123152723Scognet	bic	r0, r0, r3
124152723Scognet.Larm9_wb_next:
125152723Scognet	mcr	p15, 0, r0, c7, c10, 1	/* Clean D cache SE with VA */
126152723Scognet	add	r0, r0, ip
127152723Scognet	subs	r1, r1, ip
128246001Sian	bhi	.Larm9_wb_next
129152723Scognet	mov	pc, lr
130248361SandrewEND(arm9_dcache_wb_range)
131283366Sandrew
132152723ScognetENTRY(arm9_dcache_wbinv_range)
133152723Scognet	ldr	ip, .Larm9_line_size
134152723Scognet	cmp	r1, #0x4000
135152723Scognet	bcs	.Larm9_dcache_wbinv_all
136152723Scognet	ldr	ip, [ip]
137152723Scognet	sub	r3, ip, #1
138152723Scognet	and	r2, r0, r3
139152723Scognet	add	r1, r1, r2
140152723Scognet	bic	r0, r0, r3
141152723Scognet.Larm9_wbinv_next:
142152723Scognet	mcr	p15, 0, r0, c7, c14, 1	/* Purge D cache SE with VA */
143152723Scognet	add	r0, r0, ip
144152723Scognet	subs	r1, r1, ip
145246001Sian	bhi	.Larm9_wbinv_next
146152723Scognet	mov	pc, lr
147248361SandrewEND(arm9_dcache_wbinv_range)
148283366Sandrew
149129198Scognet/*
150152723Scognet * Note, we must not invalidate everything.  If the range is too big we
151152723Scognet * must use wb-inv of the entire cache.
152129198Scognet */
153152723ScognetENTRY(arm9_dcache_inv_range)
154152723Scognet	ldr	ip, .Larm9_line_size
155152723Scognet	cmp	r1, #0x4000
156152723Scognet	bcs	.Larm9_dcache_wbinv_all
157152723Scognet	ldr	ip, [ip]
158152723Scognet	sub	r3, ip, #1
159152723Scognet	and	r2, r0, r3
160152723Scognet	add	r1, r1, r2
161152723Scognet	bic	r0, r0, r3
162152723Scognet.Larm9_inv_next:
163152723Scognet	mcr	p15, 0, r0, c7, c6, 1	/* Invalidate D cache SE with VA */
164152723Scognet	add	r0, r0, ip
165152723Scognet	subs	r1, r1, ip
166246001Sian	bhi	.Larm9_inv_next
167152723Scognet	mov	pc, lr
168248361SandrewEND(arm9_dcache_inv_range)
169129198Scognet
170152723ScognetENTRY(arm9_idcache_wbinv_range)
171152723Scognet	ldr	ip, .Larm9_line_size
172152723Scognet	cmp	r1, #0x4000
173152723Scognet	bcs	.Larm9_idcache_wbinv_all
174152723Scognet	ldr	ip, [ip]
175152723Scognet	sub	r3, ip, #1
176152723Scognet	and	r2, r0, r3
177152723Scognet	add	r1, r1, r2
178152723Scognet	bic	r0, r0, r3
179152723Scognet.Larm9_id_wbinv_next:
180152723Scognet	mcr	p15, 0, r0, c7, c5, 1	/* Invalidate I cache SE with VA */
181152723Scognet	mcr	p15, 0, r0, c7, c14, 1	/* Purge D cache SE with VA */
182152723Scognet	add	r0, r0, ip
183152723Scognet	subs	r1, r1, ip
184246001Sian	bhi	.Larm9_id_wbinv_next
185152723Scognet	mov	pc, lr
186248361SandrewEND(arm9_idcache_wbinv_range)
187129198Scognet
188152723ScognetENTRY_NP(arm9_idcache_wbinv_all)
189152723Scognet.Larm9_idcache_wbinv_all:
190152723Scognet	/*
191152723Scognet	 * We assume that the code here can never be out of sync with the
192152723Scognet	 * dcache, so that we can safely flush the Icache and fall through
193152723Scognet	 * into the Dcache purging code.
194152723Scognet	 */
195167761Skevlo	mcr	p15, 0, r0, c7, c5, 0	/* Flush I cache */
196152723Scognet	/* Fall through */
197129198Scognet
198269390SianEENTRY(arm9_dcache_wbinv_all)
199152723Scognet.Larm9_dcache_wbinv_all:
200152723Scognet	ldr	ip, .Larm9_cache_data
201152723Scognet	ldmia	ip, {s_max, i_max, s_inc, i_inc}
202152723Scognet.Lnext_set_inv:
203152723Scognet	orr	ip, s_max, i_max
204152723Scognet.Lnext_index_inv:
205152723Scognet	mcr	p15, 0, ip, c7, c14, 2	/* Purge D cache SE with Set/Index */
206246001Sian	subs	ip, ip, i_inc
207246001Sian	bhs	.Lnext_index_inv		/* Next index */
208152723Scognet	subs	s_max, s_max, s_inc
209246001Sian	bhs	.Lnext_set_inv		/* Next set */
210152723Scognet	mov	pc, lr
211269390SianEEND(arm9_dcache_wbinv_all)
212248361SandrewEND(arm9_idcache_wbinv_all)
213129198Scognet
214152723Scognet.Larm9_cache_data:
215152723Scognet	.word	_C_LABEL(arm9_dcache_sets_max)
216152723Scognet
217129198Scognet/*
218129198Scognet * Context switch.
219129198Scognet *
220129198Scognet * These is the CPU-specific parts of the context switcher cpu_switch()
221129198Scognet * These functions actually perform the TTB reload.
222129198Scognet *
223129198Scognet * NOTE: Special calling convention
224129198Scognet *	r1, r4-r13 must be preserved
225129198Scognet */
226129198ScognetENTRY(arm9_context_switch)
227129198Scognet	/*
228129198Scognet	 * We can assume that the caches will only contain kernel addresses
229129198Scognet	 * at this point.  So no need to flush them again.
230129198Scognet	 */
231129198Scognet	mcr	p15, 0, r0, c7, c10, 4	/* drain the write buffer */
232129198Scognet	mcr	p15, 0, r0, c2, c0, 0	/* set the new TTB */
233129198Scognet	mcr	p15, 0, r0, c8, c7, 0	/* and flush the I+D tlbs */
234129198Scognet
235129198Scognet	/* Paranoia -- make sure the pipeline is empty. */
236129198Scognet	nop
237129198Scognet	nop
238129198Scognet	nop
239152723Scognet	mov	pc, lr
240248361SandrewEND(arm9_context_switch)
241152723Scognet
242152723Scognet	.bss
243152723Scognet
244152723Scognet/* XXX The following macros should probably be moved to asm.h */
245152723Scognet#define _DATA_OBJECT(x) .globl x; .type x,_ASM_TYPE_OBJECT; x:
246152723Scognet#define C_OBJECT(x)	_DATA_OBJECT(_C_LABEL(x))
247152723Scognet
248152723Scognet/*
249152723Scognet * Parameters for the cache cleaning code.  Note that the order of these
250236991Simp * four variables is assumed in the code above.  Hence the reason for
251152723Scognet * declaring them in the assembler file.
252152723Scognet */
253276596Sian	.align 2
254152723ScognetC_OBJECT(arm9_dcache_sets_max)
255152723Scognet	.space	4
256152723ScognetC_OBJECT(arm9_dcache_index_max)
257152723Scognet	.space	4
258152723ScognetC_OBJECT(arm9_dcache_sets_inc)
259152723Scognet	.space	4
260152723ScognetC_OBJECT(arm9_dcache_index_inc)
261152723Scognet	.space	4
262