blob: 7f35d0c291297a311da7c78fa410ccff3c080d7e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/boot/compressed/head.S
3 *
4 * Copyright (C) 1996-2002 Russell King
Hyok S. Choi10c2df62006-03-27 10:21:34 +01005 * Copyright (C) 2004 Hyok S. Choi (MPU support)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/linkage.h>
12
13/*
14 * Debugging stuff
15 *
16 * Note that these macros must not contain any code which is not
17 * 100% relocatable. Any attempt to do so will result in a crash.
18 * Please select one of the following when turning on debugging.
19 */
20#ifdef DEBUG
Russell King5cd0c342005-05-03 12:18:46 +010021
Russell King5cd0c342005-05-03 12:18:46 +010022#if defined(CONFIG_DEBUG_ICEDCC)
Tony Lindgren7d95ded2006-09-20 13:03:34 +010023
Stephen Boyddfad5492011-03-23 22:46:15 +010024#if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K) || defined(CONFIG_CPU_V7)
Tony Lindgren4e6d4882010-02-01 23:26:53 +010025 .macro loadsp, rb, tmp
Tony Lindgren7d95ded2006-09-20 13:03:34 +010026 .endm
27 .macro writeb, ch, rb
28 mcr p14, 0, \ch, c0, c5, 0
29 .endm
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030#elif defined(CONFIG_CPU_V7)
31 .macro loadsp, rb, tmp
32 .endm
33 .macro writeb, ch, rb
34wait: mrc p14, 0, pc, c0, c1, 0
35 bcs wait
36 mcr p14, 0, \ch, c0, c5, 0
37 .endm
Jean-Christop PLAGNIOL-VILLARDc633c3c2009-02-25 04:20:40 +010038#elif defined(CONFIG_CPU_XSCALE)
Tony Lindgren4e6d4882010-02-01 23:26:53 +010039 .macro loadsp, rb, tmp
Jean-Christop PLAGNIOL-VILLARDc633c3c2009-02-25 04:20:40 +010040 .endm
41 .macro writeb, ch, rb
42 mcr p14, 0, \ch, c8, c0, 0
43 .endm
Tony Lindgren7d95ded2006-09-20 13:03:34 +010044#else
Tony Lindgren4e6d4882010-02-01 23:26:53 +010045 .macro loadsp, rb, tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 .endm
Russell King224b5be2005-11-16 14:59:51 +000047 .macro writeb, ch, rb
Uwe Kleine-König41a9e682007-12-13 09:31:34 +010048 mcr p14, 0, \ch, c1, c0, 0
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 .endm
Tony Lindgren7d95ded2006-09-20 13:03:34 +010050#endif
51
Russell King5cd0c342005-05-03 12:18:46 +010052#else
Russell King224b5be2005-11-16 14:59:51 +000053
Russell Kinga09e64f2008-08-05 16:14:15 +010054#include <mach/debug-macro.S>
Russell King224b5be2005-11-16 14:59:51 +000055
Russell King5cd0c342005-05-03 12:18:46 +010056 .macro writeb, ch, rb
57 senduart \ch, \rb
58 .endm
59
Russell King224b5be2005-11-16 14:59:51 +000060#if defined(CONFIG_ARCH_SA1100)
Tony Lindgren4e6d4882010-02-01 23:26:53 +010061 .macro loadsp, rb, tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 mov \rb, #0x80000000 @ physical base address
Russell King224b5be2005-11-16 14:59:51 +000063#ifdef CONFIG_DEBUG_LL_SER3
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 add \rb, \rb, #0x00050000 @ Ser3
Russell King224b5be2005-11-16 14:59:51 +000065#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 add \rb, \rb, #0x00010000 @ Ser1
Russell King224b5be2005-11-16 14:59:51 +000067#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 .endm
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#elif defined(CONFIG_ARCH_S3C2410)
Tony Lindgren4e6d4882010-02-01 23:26:53 +010070 .macro loadsp, rb, tmp
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 mov \rb, #0x50000000
Ben Dooksc7657842007-07-22 16:11:20 +010072 add \rb, \rb, #0x4000 * CONFIG_S3C_LOWLEVEL_UART_PORT
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 .endm
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#else
Tony Lindgren4e6d4882010-02-01 23:26:53 +010075 .macro loadsp, rb, tmp
76 addruart \rb, \tmp
Russell King224b5be2005-11-16 14:59:51 +000077 .endm
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#endif
79#endif
Russell King5cd0c342005-05-03 12:18:46 +010080#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 .macro kputc,val
83 mov r0, \val
84 bl putc
85 .endm
86
87 .macro kphex,val,len
88 mov r0, \val
89 mov r1, #\len
90 bl phex
91 .endm
92
93 .macro debug_reloc_start
94#ifdef DEBUG
95 kputc #'\n'
96 kphex r6, 8 /* processor id */
97 kputc #':'
98 kphex r7, 8 /* architecture id */
Hyok S. Choif12d0d72006-09-26 17:36:37 +090099#ifdef CONFIG_CPU_CP15
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 kputc #':'
101 mrc p15, 0, r0, c1, c0
102 kphex r0, 8 /* control reg */
Hyok S. Choif12d0d72006-09-26 17:36:37 +0900103#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 kputc #'\n'
105 kphex r5, 8 /* decompressed kernel start */
106 kputc #'-'
Russell Kingf4619022006-01-12 17:17:57 +0000107 kphex r9, 8 /* decompressed kernel end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 kputc #'>'
109 kphex r4, 8 /* kernel execution address */
110 kputc #'\n'
111#endif
112 .endm
113
114 .macro debug_reloc_end
115#ifdef DEBUG
116 kphex r5, 8 /* end of kernel */
117 kputc #'\n'
118 mov r0, r4
119 bl memdump /* dump 256 bytes at start of kernel */
120#endif
121 .endm
122
123 .section ".start", #alloc, #execinstr
124/*
125 * sort out different calling conventions
126 */
127 .align
Dave Martin26e5ca92010-11-29 19:43:27 +0100128 .arm @ Always enter in ARM state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129start:
130 .type start,#function
Nicolas Pitreb11fe382011-02-12 22:25:27 +0100131 .rept 7
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 mov r0, r0
133 .endr
Nicolas Pitreb11fe382011-02-12 22:25:27 +0100134 ARM( mov r0, r0 )
135 ARM( b 1f )
136 THUMB( adr r12, BSYM(1f) )
137 THUMB( bx r12 )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 .word 0x016f2818 @ Magic numbers to help the loader
140 .word start @ absolute load/run zImage address
141 .word _edata @ zImage end address
Dave Martin26e5ca92010-11-29 19:43:27 +0100142 THUMB( .thumb )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431: mov r7, r1 @ save architecture ID
Russell Kingf4619022006-01-12 17:17:57 +0000144 mov r8, r2 @ save atags pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146#ifndef __ARM_ARCH_2__
147 /*
148 * Booting from Angel - need to enter SVC mode and disable
149 * FIQs/IRQs (numeric definitions from angel arm.h source).
150 * We only do this if we were in user mode on entry.
151 */
152 mrs r2, cpsr @ get current mode
153 tst r2, #3 @ not user?
154 bne not_angel
155 mov r0, #0x17 @ angel_SWIreason_EnterSVC
Catalin Marinas0e056f22009-07-24 12:32:58 +0100156 ARM( swi 0x123456 ) @ angel_SWI_ARM
157 THUMB( svc 0xab ) @ angel_SWI_THUMB
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158not_angel:
159 mrs r2, cpsr @ turn off interrupts to
160 orr r2, r2, #0xc0 @ prevent angel from running
161 msr cpsr_c, r2
162#else
163 teqp pc, #0x0c000003 @ turn off interrupts
164#endif
165
166 /*
167 * Note that some cache flushing and other stuff may
168 * be needed here - is there an Angel SWI call for this?
169 */
170
171 /*
172 * some architecture specific code can be inserted
Russell Kingf4619022006-01-12 17:17:57 +0000173 * by the linker here, but it should preserve r7, r8, and r9.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
175
176 .text
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100177
Eric Miaoe69edc792010-07-05 15:56:50 +0200178#ifdef CONFIG_AUTO_ZRELADDR
179 @ determine final kernel image address
Dave Martinbfa64c42010-11-29 19:43:26 +0100180 mov r4, pc
181 and r4, r4, #0xf8000000
Eric Miaoe69edc792010-07-05 15:56:50 +0200182 add r4, r4, #TEXT_OFFSET
183#else
Russell King9e84ed62010-09-09 22:39:41 +0100184 ldr r4, =zreladdr
Eric Miaoe69edc792010-07-05 15:56:50 +0200185#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100187 bl cache_on
188
189restart: adr r0, LC0
Nicolas Pitre34cc1a82011-04-19 15:42:43 -0400190 ldmia r0, {r1, r2, r3, r6, r10, r11, r12}
Nicolas Pitreadcc2592011-04-27 16:15:11 -0400191 ldr sp, [r0, #28]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /*
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100194 * We might be running at a different address. We need
195 * to fix up various pointers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 */
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100197 sub r0, r0, r1 @ calculate the delta offset
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100198 add r6, r6, r0 @ _edata
Nicolas Pitre34cc1a82011-04-19 15:42:43 -0400199 add r10, r10, r0 @ inflated kernel size location
200
201 /*
202 * The kernel build system appends the size of the
203 * decompressed kernel at the end of the compressed data
204 * in little-endian form.
205 */
206 ldrb r9, [r10, #0]
207 ldrb lr, [r10, #1]
208 orr r9, r9, lr, lsl #8
209 ldrb lr, [r10, #2]
210 ldrb r10, [r10, #3]
211 orr r9, r9, lr, lsl #16
212 orr r9, r9, r10, lsl #24
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100213
214#ifndef CONFIG_ZBOOT_ROM
215 /* malloc space is above the relocated stack (64k max) */
216 add sp, sp, r0
217 add r10, sp, #0x10000
218#else
219 /*
220 * With ZBOOT_ROM the bss/stack is non relocatable,
221 * but someone could still run this code from RAM,
222 * in which case our reference is _edata.
223 */
224 mov r10, r6
225#endif
226
John Bonesioede5d5d2011-05-27 18:45:50 -0400227 mov r5, #0 @ init dtb size to 0
228#ifdef CONFIG_ARM_APPENDED_DTB
229/*
230 * r0 = delta
231 * r2 = BSS start
232 * r3 = BSS end
233 * r4 = final kernel address
234 * r5 = appended dtb size (still unknown)
235 * r6 = _edata
236 * r7 = architecture ID
237 * r8 = atags/device tree pointer
238 * r9 = size of decompressed image
239 * r10 = end of this image, including bss/stack/malloc space if non XIP
240 * r11 = GOT start
241 * r12 = GOT end
242 * sp = stack pointer
243 *
244 * if there are device trees (dtb) appended to zImage, advance r10 so that the
245 * dtb data will get relocated along with the kernel if necessary.
246 */
247
248 ldr lr, [r6, #0]
249#ifndef __ARMEB__
250 ldr r1, =0xedfe0dd0 @ sig is 0xd00dfeed big endian
251#else
252 ldr r1, =0xd00dfeed
253#endif
254 cmp lr, r1
255 bne dtb_check_done @ not found
256
Nicolas Pitrebbc98d12011-09-13 22:37:07 -0400257#ifdef CONFIG_ARM_ATAG_DTB_COMPAT
258 /*
259 * OK... Let's do some funky business here.
260 * If we do have a DTB appended to zImage, and we do have
261 * an ATAG list around, we want the later to be translated
262 * and folded into the former here. To be on the safe side,
263 * let's temporarily move the stack away into the malloc
264 * area. No GOT fixup has occurred yet, but none of the
265 * code we're about to call uses any global variable.
266 */
267 add sp, sp, #0x10000
268 stmfd sp!, {r0-r3, ip, lr}
269 mov r0, r8
270 mov r1, r6
271 sub r2, sp, r6
272 bl atags_to_fdt
273
274 /*
275 * If returned value is 1, there is no ATAG at the location
276 * pointed by r8. Try the typical 0x100 offset from start
277 * of RAM and hope for the best.
278 */
279 cmp r0, #1
280 sub r0, r4, #(TEXT_OFFSET - 0x100)
281 mov r1, r6
282 sub r2, sp, r6
283 blne atags_to_fdt
284
285 ldmfd sp!, {r0-r3, ip, lr}
286 sub sp, sp, #0x10000
287#endif
288
John Bonesioede5d5d2011-05-27 18:45:50 -0400289 mov r8, r6 @ use the appended device tree
290
Nicolas Pitre5a9dac22011-06-12 01:07:33 -0400291 /*
292 * Make sure that the DTB doesn't end up in the final
293 * kernel's .bss area. To do so, we adjust the decompressed
294 * kernel size to compensate if that .bss size is larger
295 * than the relocated code.
296 */
297 ldr r5, =_kernel_bss_size
298 adr r1, wont_overwrite
299 sub r1, r6, r1
300 subs r1, r5, r1
301 addhi r9, r9, r1
302
John Bonesioede5d5d2011-05-27 18:45:50 -0400303 /* Get the dtb's size */
304 ldr r5, [r6, #4]
305#ifndef __ARMEB__
306 /* convert r5 (dtb size) to little endian */
307 eor r1, r5, r5, ror #16
308 bic r1, r1, #0x00ff0000
309 mov r5, r5, ror #8
310 eor r5, r5, r1, lsr #8
311#endif
312
313 /* preserve 64-bit alignment */
314 add r5, r5, #7
315 bic r5, r5, #7
316
317 /* relocate some pointers past the appended dtb */
318 add r6, r6, r5
319 add r10, r10, r5
320 add sp, sp, r5
321dtb_check_done:
322#endif
323
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100324/*
325 * Check to see if we will overwrite ourselves.
326 * r4 = final kernel address
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100327 * r9 = size of decompressed image
328 * r10 = end of this image, including bss/stack/malloc space if non XIP
329 * We basically want:
Nicolas Pitreea9df3b2011-04-21 22:52:06 -0400330 * r4 - 16k page directory >= r10 -> OK
Nicolas Pitre5a9dac22011-06-12 01:07:33 -0400331 * r4 + image length <= address of wont_overwrite -> OK
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100332 */
Nicolas Pitreea9df3b2011-04-21 22:52:06 -0400333 add r10, r10, #16384
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100334 cmp r4, r10
335 bhs wont_overwrite
336 add r10, r4, r9
Nicolas Pitre5a9dac22011-06-12 01:07:33 -0400337 adr r9, wont_overwrite
338 cmp r10, r9
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100339 bls wont_overwrite
340
341/*
342 * Relocate ourselves past the end of the decompressed kernel.
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100343 * r6 = _edata
344 * r10 = end of the decompressed kernel
345 * Because we always copy ahead, we need to do it from the end and go
346 * backward in case the source and destination overlap.
347 */
Nicolas Pitreadcc2592011-04-27 16:15:11 -0400348 /*
349 * Bump to the next 256-byte boundary with the size of
350 * the relocation code added. This avoids overwriting
351 * ourself when the offset is small.
352 */
353 add r10, r10, #((reloc_code_end - restart + 256) & ~255)
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100354 bic r10, r10, #255
355
Nicolas Pitreadcc2592011-04-27 16:15:11 -0400356 /* Get start of code we want to copy and align it down. */
357 adr r5, restart
358 bic r5, r5, #31
359
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100360 sub r9, r6, r5 @ size to copy
361 add r9, r9, #31 @ rounded up to a multiple
362 bic r9, r9, #31 @ ... of 32 bytes
363 add r6, r9, r5
364 add r9, r9, r10
365
3661: ldmdb r6!, {r0 - r3, r10 - r12, lr}
367 cmp r6, r5
368 stmdb r9!, {r0 - r3, r10 - r12, lr}
369 bhi 1b
370
371 /* Preserve offset to relocated code. */
372 sub r6, r9, r6
373
Tony Lindgren7c2527f2011-04-26 05:37:46 -0700374#ifndef CONFIG_ZBOOT_ROM
375 /* cache_clean_flush may use the stack, so relocate it */
376 add sp, sp, r6
377#endif
378
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100379 bl cache_clean_flush
380
381 adr r0, BSYM(restart)
382 add r0, r0, r6
383 mov pc, r0
384
385wont_overwrite:
386/*
387 * If delta is zero, we are running at the address we were linked at.
388 * r0 = delta
389 * r2 = BSS start
390 * r3 = BSS end
391 * r4 = kernel execution address
John Bonesioede5d5d2011-05-27 18:45:50 -0400392 * r5 = appended dtb size (0 if not present)
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100393 * r7 = architecture ID
394 * r8 = atags pointer
395 * r11 = GOT start
396 * r12 = GOT end
397 * sp = stack pointer
398 */
John Bonesioede5d5d2011-05-27 18:45:50 -0400399 orrs r1, r0, r5
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100400 beq not_relocated
John Bonesioede5d5d2011-05-27 18:45:50 -0400401
Russell King98e12b52010-02-25 23:56:38 +0000402 add r11, r11, r0
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100403 add r12, r12, r0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405#ifndef CONFIG_ZBOOT_ROM
406 /*
407 * If we're running fully PIC === CONFIG_ZBOOT_ROM = n,
408 * we need to fix up pointers into the BSS region.
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100409 * Note that the stack pointer has already been fixed up.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
411 add r2, r2, r0
412 add r3, r3, r0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /*
415 * Relocate all entries in the GOT table.
John Bonesioede5d5d2011-05-27 18:45:50 -0400416 * Bump bss entries to _edata + dtb size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 */
Russell King98e12b52010-02-25 23:56:38 +00004181: ldr r1, [r11, #0] @ relocate entries in the GOT
John Bonesioede5d5d2011-05-27 18:45:50 -0400419 add r1, r1, r0 @ This fixes up C references
420 cmp r1, r2 @ if entry >= bss_start &&
421 cmphs r3, r1 @ bss_end > entry
422 addhi r1, r1, r5 @ entry += dtb size
423 str r1, [r11], #4 @ next entry
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100424 cmp r11, r12
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 blo 1b
John Bonesioede5d5d2011-05-27 18:45:50 -0400426
427 /* bump our bss pointers too */
428 add r2, r2, r5
429 add r3, r3, r5
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431#else
432
433 /*
434 * Relocate entries in the GOT table. We only relocate
435 * the entries that are outside the (relocated) BSS region.
436 */
Russell King98e12b52010-02-25 23:56:38 +00004371: ldr r1, [r11, #0] @ relocate entries in the GOT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 cmp r1, r2 @ entry < bss_start ||
439 cmphs r3, r1 @ _end < entry
440 addlo r1, r1, r0 @ table. This fixes up the
Russell King98e12b52010-02-25 23:56:38 +0000441 str r1, [r11], #4 @ C references.
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100442 cmp r11, r12
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 blo 1b
444#endif
445
446not_relocated: mov r0, #0
4471: str r0, [r2], #4 @ clear bss
448 str r0, [r2], #4
449 str r0, [r2], #4
450 str r0, [r2], #4
451 cmp r2, r3
452 blo 1b
453
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100454/*
455 * The C runtime environment should now be setup sufficiently.
456 * Set up some pointers, and start decompressing.
457 * r4 = kernel execution address
458 * r7 = architecture ID
459 * r8 = atags pointer
460 */
461 mov r0, r4
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 mov r1, sp @ malloc space above stack
463 add r2, sp, #0x10000 @ 64k max
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 mov r3, r7
465 bl decompress_kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 bl cache_clean_flush
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100467 bl cache_off
468 mov r0, #0 @ must be zero
469 mov r1, r7 @ restore architecture number
470 mov r2, r8 @ restore atags pointer
471 mov pc, r4 @ call kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Catalin Marinas88987ef2009-07-24 12:32:52 +0100473 .align 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 .type LC0, #object
475LC0: .word LC0 @ r1
476 .word __bss_start @ r2
477 .word _end @ r3
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100478 .word _edata @ r6
Nicolas Pitre34cc1a82011-04-19 15:42:43 -0400479 .word input_data_end - 4 @ r10 (inflated size location)
Russell King98e12b52010-02-25 23:56:38 +0000480 .word _got_start @ r11
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 .word _got_end @ ip
Nicolas Pitre8d7e4cc2011-04-27 14:54:39 -0400482 .word .L_user_stack_end @ sp
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 .size LC0, . - LC0
484
485#ifdef CONFIG_ARCH_RPC
486 .globl params
Eric Miaodb7b2b42010-06-03 15:36:49 +0800487params: ldr r0, =0x10000100 @ params_phys for RPC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 mov pc, lr
489 .ltorg
490 .align
491#endif
492
493/*
494 * Turn on the cache. We need to setup some page tables so that we
495 * can have both the I and D caches on.
496 *
497 * We place the page tables 16k down from the kernel execution address,
498 * and we hope that nothing else is using it. If we're using it, we
499 * will go pop!
500 *
501 * On entry,
502 * r4 = kernel execution address
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * r7 = architecture number
Russell Kingf4619022006-01-12 17:17:57 +0000504 * r8 = atags pointer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 * On exit,
Uwe Kleine-König21b28412010-01-26 22:08:09 +0100506 * r0, r1, r2, r3, r9, r10, r12 corrupted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * This routine must preserve:
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100508 * r4, r7, r8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 */
510 .align 5
511cache_on: mov r3, #8 @ cache_on function
512 b call_cache_fn
513
Hyok S. Choi10c2df62006-03-27 10:21:34 +0100514/*
515 * Initialize the highest priority protection region, PR7
516 * to cover all 32bit address and cacheable and bufferable.
517 */
518__armv4_mpu_cache_on:
519 mov r0, #0x3f @ 4G, the whole
520 mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
521 mcr p15, 0, r0, c6, c7, 1
522
523 mov r0, #0x80 @ PR7
524 mcr p15, 0, r0, c2, c0, 0 @ D-cache on
525 mcr p15, 0, r0, c2, c0, 1 @ I-cache on
526 mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
527
528 mov r0, #0xc000
529 mcr p15, 0, r0, c5, c0, 1 @ I-access permission
530 mcr p15, 0, r0, c5, c0, 0 @ D-access permission
531
532 mov r0, #0
533 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
534 mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
535 mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
536 mrc p15, 0, r0, c1, c0, 0 @ read control reg
537 @ ...I .... ..D. WC.M
538 orr r0, r0, #0x002d @ .... .... ..1. 11.1
539 orr r0, r0, #0x1000 @ ...1 .... .... ....
540
541 mcr p15, 0, r0, c1, c0, 0 @ write control reg
542
543 mov r0, #0
544 mcr p15, 0, r0, c7, c5, 0 @ flush(inval) I-Cache
545 mcr p15, 0, r0, c7, c6, 0 @ flush(inval) D-Cache
546 mov pc, lr
547
548__armv3_mpu_cache_on:
549 mov r0, #0x3f @ 4G, the whole
550 mcr p15, 0, r0, c6, c7, 0 @ PR7 Area Setting
551
552 mov r0, #0x80 @ PR7
553 mcr p15, 0, r0, c2, c0, 0 @ cache on
554 mcr p15, 0, r0, c3, c0, 0 @ write-buffer on
555
556 mov r0, #0xc000
557 mcr p15, 0, r0, c5, c0, 0 @ access permission
558
559 mov r0, #0
560 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
Uwe Kleine-König4a8d57a2010-01-26 22:14:23 +0100561 /*
562 * ?? ARMv3 MMU does not allow reading the control register,
563 * does this really work on ARMv3 MPU?
564 */
Hyok S. Choi10c2df62006-03-27 10:21:34 +0100565 mrc p15, 0, r0, c1, c0, 0 @ read control reg
566 @ .... .... .... WC.M
567 orr r0, r0, #0x000d @ .... .... .... 11.1
Uwe Kleine-König4a8d57a2010-01-26 22:14:23 +0100568 /* ?? this overwrites the value constructed above? */
Hyok S. Choi10c2df62006-03-27 10:21:34 +0100569 mov r0, #0
570 mcr p15, 0, r0, c1, c0, 0 @ write control reg
571
Uwe Kleine-König4a8d57a2010-01-26 22:14:23 +0100572 /* ?? invalidate for the second time? */
Hyok S. Choi10c2df62006-03-27 10:21:34 +0100573 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
574 mov pc, lr
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576__setup_mmu: sub r3, r4, #16384 @ Page directory size
577 bic r3, r3, #0xff @ Align the pointer
578 bic r3, r3, #0x3f00
579/*
580 * Initialise the page tables, turning on the cacheable and bufferable
581 * bits for the RAM area only.
582 */
583 mov r0, r3
Russell Kingf4619022006-01-12 17:17:57 +0000584 mov r9, r0, lsr #18
585 mov r9, r9, lsl #18 @ start of RAM
586 add r10, r9, #0x10000000 @ a reasonable RAM size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 mov r1, #0x12
588 orr r1, r1, #3 << 10
589 add r2, r3, #16384
Nicolas Pitre265d5e42006-01-18 22:38:51 +00005901: cmp r1, r9 @ if virt > start of RAM
Mark A. Greeraf3e4fd2011-04-01 15:41:26 +0100591#ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
592 orrhs r1, r1, #0x08 @ set cacheable
593#else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 orrhs r1, r1, #0x0c @ set cacheable, bufferable
Mark A. Greeraf3e4fd2011-04-01 15:41:26 +0100595#endif
Russell Kingf4619022006-01-12 17:17:57 +0000596 cmp r1, r10 @ if virt > end of RAM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 bichs r1, r1, #0x0c @ clear cacheable, bufferable
598 str r1, [r0], #4 @ 1:1 mapping
599 add r1, r1, #1048576
600 teq r0, r2
601 bne 1b
602/*
603 * If ever we are running from Flash, then we surely want the cache
604 * to be enabled also for our execution instance... We map 2MB of it
605 * so there is no map overlap problem for up to 1 MB compressed kernel.
606 * If the execution is in RAM then we would only be duplicating the above.
607 */
608 mov r1, #0x1e
609 orr r1, r1, #3 << 10
Dave Martinbfa64c42010-11-29 19:43:26 +0100610 mov r2, pc
611 mov r2, r2, lsr #20
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 orr r1, r1, r2, lsl #20
613 add r0, r3, r2, lsl #2
614 str r1, [r0], #4
615 add r1, r1, #1048576
616 str r1, [r0]
617 mov pc, lr
Catalin Marinas93ed3972008-08-28 11:22:32 +0100618ENDPROC(__setup_mmu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Mark A. Greeraf3e4fd2011-04-01 15:41:26 +0100620__arm926ejs_mmu_cache_on:
621#ifdef CONFIG_CPU_DCACHE_WRITETHROUGH
622 mov r0, #4 @ put dcache in WT mode
623 mcr p15, 7, r0, c15, c0, 0
624#endif
625
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000626__armv4_mmu_cache_on:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 mov r12, lr
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100628#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 bl __setup_mmu
630 mov r0, #0
631 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
632 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
633 mrc p15, 0, r0, c1, c0, 0 @ read control reg
634 orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
635 orr r0, r0, #0x0030
Catalin Marinas26584852009-05-30 14:00:18 +0100636#ifdef CONFIG_CPU_ENDIAN_BE8
637 orr r0, r0, #1 << 25 @ big-endian page tables
638#endif
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000639 bl __common_mmu_cache_on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 mov r0, #0
641 mcr p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100642#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 mov pc, r12
644
Catalin Marinas7d09e852007-06-01 17:14:53 +0100645__armv7_mmu_cache_on:
646 mov r12, lr
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100647#ifdef CONFIG_MMU
Catalin Marinas7d09e852007-06-01 17:14:53 +0100648 mrc p15, 0, r11, c0, c1, 4 @ read ID_MMFR0
649 tst r11, #0xf @ VMSA
650 blne __setup_mmu
651 mov r0, #0
652 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
653 tst r11, #0xf @ VMSA
654 mcrne p15, 0, r0, c8, c7, 0 @ flush I,D TLBs
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100655#endif
Catalin Marinas7d09e852007-06-01 17:14:53 +0100656 mrc p15, 0, r0, c1, c0, 0 @ read control reg
657 orr r0, r0, #0x5000 @ I-cache enable, RR cache replacement
658 orr r0, r0, #0x003c @ write buffer
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100659#ifdef CONFIG_MMU
Catalin Marinas26584852009-05-30 14:00:18 +0100660#ifdef CONFIG_CPU_ENDIAN_BE8
661 orr r0, r0, #1 << 25 @ big-endian page tables
662#endif
Catalin Marinas7d09e852007-06-01 17:14:53 +0100663 orrne r0, r0, #1 @ MMU enabled
664 movne r1, #-1
665 mcrne p15, 0, r3, c2, c0, 0 @ load page table pointer
666 mcrne p15, 0, r1, c3, c0, 0 @ load domain access control
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100667#endif
Catalin Marinas7d09e852007-06-01 17:14:53 +0100668 mcr p15, 0, r0, c1, c0, 0 @ load control register
669 mrc p15, 0, r0, c1, c0, 0 @ and read it back
670 mov r0, #0
671 mcr p15, 0, r0, c7, c5, 4 @ ISB
672 mov pc, r12
673
Paulius Zaleckas28853ac2009-03-25 13:10:01 +0200674__fa526_cache_on:
675 mov r12, lr
676 bl __setup_mmu
677 mov r0, #0
678 mcr p15, 0, r0, c7, c7, 0 @ Invalidate whole cache
679 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
680 mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
681 mrc p15, 0, r0, c1, c0, 0 @ read control reg
682 orr r0, r0, #0x1000 @ I-cache enable
683 bl __common_mmu_cache_on
684 mov r0, #0
685 mcr p15, 0, r0, c8, c7, 0 @ flush UTLB
686 mov pc, r12
687
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000688__arm6_mmu_cache_on:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 mov r12, lr
690 bl __setup_mmu
691 mov r0, #0
692 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
693 mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
694 mov r0, #0x30
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000695 bl __common_mmu_cache_on
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 mov r0, #0
697 mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
698 mov pc, r12
699
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000700__common_mmu_cache_on:
Catalin Marinas0e056f22009-07-24 12:32:58 +0100701#ifndef CONFIG_THUMB2_KERNEL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702#ifndef DEBUG
703 orr r0, r0, #0x000d @ Write buffer, mmu
704#endif
705 mov r1, #-1
706 mcr p15, 0, r3, c2, c0, 0 @ load page table pointer
707 mcr p15, 0, r1, c3, c0, 0 @ load domain access control
Nicolas Pitre2dc76672006-07-01 21:29:32 +0100708 b 1f
709 .align 5 @ cache line aligned
7101: mcr p15, 0, r0, c1, c0, 0 @ load control register
711 mrc p15, 0, r0, c1, c0, 0 @ and read it back to
712 sub pc, lr, r0, lsr #32 @ properly flush pipeline
Catalin Marinas0e056f22009-07-24 12:32:58 +0100713#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Dave Martin946a1052011-06-14 14:20:44 +0100715#define PROC_ENTRY_SIZE (4*5)
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 * Here follow the relocatable cache support functions for the
719 * various processors. This is a generic hook for locating an
720 * entry and jumping to an instruction at the specified offset
721 * from the start of the block. Please note this is all position
722 * independent code.
723 *
724 * r1 = corrupted
725 * r2 = corrupted
726 * r3 = block offset
Russell King98e12b52010-02-25 23:56:38 +0000727 * r9 = corrupted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * r12 = corrupted
729 */
730
731call_cache_fn: adr r12, proc_types
Hyok S. Choif12d0d72006-09-26 17:36:37 +0900732#ifdef CONFIG_CPU_CP15
Russell King98e12b52010-02-25 23:56:38 +0000733 mrc p15, 0, r9, c0, c0 @ get processor ID
Hyok S. Choif12d0d72006-09-26 17:36:37 +0900734#else
Russell King98e12b52010-02-25 23:56:38 +0000735 ldr r9, =CONFIG_PROCESSOR_ID
Hyok S. Choif12d0d72006-09-26 17:36:37 +0900736#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07007371: ldr r1, [r12, #0] @ get value
738 ldr r2, [r12, #4] @ get mask
Russell King98e12b52010-02-25 23:56:38 +0000739 eor r1, r1, r9 @ (real ^ match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 tst r1, r2 @ & mask
Catalin Marinas0e056f22009-07-24 12:32:58 +0100741 ARM( addeq pc, r12, r3 ) @ call cache function
742 THUMB( addeq r12, r3 )
743 THUMB( moveq pc, r12 ) @ call cache function
Dave Martin946a1052011-06-14 14:20:44 +0100744 add r12, r12, #PROC_ENTRY_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 b 1b
746
747/*
748 * Table for cache operations. This is basically:
749 * - CPU ID match
750 * - CPU ID mask
751 * - 'cache on' method instruction
752 * - 'cache off' method instruction
753 * - 'cache flush' method instruction
754 *
755 * We match an entry using: ((real_id ^ match) & mask) == 0
756 *
757 * Writethrough caches generally only need 'on' and 'off'
758 * methods. Writeback caches _must_ have the flush method
759 * defined.
760 */
Catalin Marinas88987ef2009-07-24 12:32:52 +0100761 .align 2
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 .type proc_types,#object
763proc_types:
764 .word 0x41560600 @ ARM6/610
765 .word 0xffffffe0
Catalin Marinas0e056f22009-07-24 12:32:58 +0100766 W(b) __arm6_mmu_cache_off @ works, but slow
767 W(b) __arm6_mmu_cache_off
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100769 THUMB( nop )
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000770@ b __arm6_mmu_cache_on @ untested
771@ b __arm6_mmu_cache_off
772@ b __armv3_mmu_cache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Brian Swetland180b7a02009-01-26 17:15:49 -0800774#if !defined(CONFIG_CPU_V7)
775 /* This collides with some V7 IDs, preventing correct detection */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 .word 0x00000000 @ old ARM ID
777 .word 0x0000f000
778 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100779 THUMB( nop )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100781 THUMB( nop )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100783 THUMB( nop )
Brian Swetland180b7a02009-01-26 17:15:49 -0800784#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 .word 0x41007000 @ ARM7/710
787 .word 0xfff8fe00
Catalin Marinas0e056f22009-07-24 12:32:58 +0100788 W(b) __arm7_mmu_cache_off
789 W(b) __arm7_mmu_cache_off
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100791 THUMB( nop )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 .word 0x41807200 @ ARM720T (writethrough)
794 .word 0xffffff00
Catalin Marinas0e056f22009-07-24 12:32:58 +0100795 W(b) __armv4_mmu_cache_on
796 W(b) __armv4_mmu_cache_off
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100798 THUMB( nop )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Hyok S. Choi10c2df62006-03-27 10:21:34 +0100800 .word 0x41007400 @ ARM74x
801 .word 0xff00ff00
Catalin Marinas0e056f22009-07-24 12:32:58 +0100802 W(b) __armv3_mpu_cache_on
803 W(b) __armv3_mpu_cache_off
804 W(b) __armv3_mpu_cache_flush
Hyok S. Choi10c2df62006-03-27 10:21:34 +0100805
806 .word 0x41009400 @ ARM94x
807 .word 0xff00ff00
Catalin Marinas0e056f22009-07-24 12:32:58 +0100808 W(b) __armv4_mpu_cache_on
809 W(b) __armv4_mpu_cache_off
810 W(b) __armv4_mpu_cache_flush
Hyok S. Choi10c2df62006-03-27 10:21:34 +0100811
Mark A. Greeraf3e4fd2011-04-01 15:41:26 +0100812 .word 0x41069260 @ ARM926EJ-S (v5TEJ)
813 .word 0xff0ffff0
Nicolas Pitre720c60e2011-06-09 05:05:27 +0100814 W(b) __arm926ejs_mmu_cache_on
815 W(b) __armv4_mmu_cache_off
816 W(b) __armv5tej_mmu_cache_flush
Mark A. Greeraf3e4fd2011-04-01 15:41:26 +0100817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .word 0x00007000 @ ARM7 IDs
819 .word 0x0000f000
820 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100821 THUMB( nop )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100823 THUMB( nop )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100825 THUMB( nop )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 @ Everything from here on will be the new ID system.
828
829 .word 0x4401a100 @ sa110 / sa1100
830 .word 0xffffffe0
Catalin Marinas0e056f22009-07-24 12:32:58 +0100831 W(b) __armv4_mmu_cache_on
832 W(b) __armv4_mmu_cache_off
833 W(b) __armv4_mmu_cache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 .word 0x6901b110 @ sa1110
836 .word 0xfffffff0
Catalin Marinas0e056f22009-07-24 12:32:58 +0100837 W(b) __armv4_mmu_cache_on
838 W(b) __armv4_mmu_cache_off
839 W(b) __armv4_mmu_cache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
Haojian Zhuang4157d312010-03-12 05:47:55 -0500841 .word 0x56056900
842 .word 0xffffff00 @ PXA9xx
Catalin Marinas0e056f22009-07-24 12:32:58 +0100843 W(b) __armv4_mmu_cache_on
844 W(b) __armv4_mmu_cache_off
845 W(b) __armv4_mmu_cache_flush
Eric Miao59c7bcd2008-11-29 21:42:39 +0800846
Eric Miao49cbe782009-01-20 14:15:18 +0800847 .word 0x56158000 @ PXA168
848 .word 0xfffff000
Catalin Marinas0e056f22009-07-24 12:32:58 +0100849 W(b) __armv4_mmu_cache_on
850 W(b) __armv4_mmu_cache_off
851 W(b) __armv5tej_mmu_cache_flush
Eric Miao49cbe782009-01-20 14:15:18 +0800852
Nicolas Pitre2e2023f2008-06-03 23:06:21 +0200853 .word 0x56050000 @ Feroceon
854 .word 0xff0f0000
Catalin Marinas0e056f22009-07-24 12:32:58 +0100855 W(b) __armv4_mmu_cache_on
856 W(b) __armv4_mmu_cache_off
857 W(b) __armv5tej_mmu_cache_flush
Nicolas Pitre3ebb5a22007-10-31 15:31:48 -0400858
Joonyoung Shim55879312009-06-16 20:05:57 +0900859#ifdef CONFIG_CPU_FEROCEON_OLD_ID
860 /* this conflicts with the standard ARMv5TE entry */
861 .long 0x41009260 @ Old Feroceon
862 .long 0xff00fff0
863 b __armv4_mmu_cache_on
864 b __armv4_mmu_cache_off
865 b __armv5tej_mmu_cache_flush
866#endif
867
Paulius Zaleckas28853ac2009-03-25 13:10:01 +0200868 .word 0x66015261 @ FA526
869 .word 0xff01fff1
Catalin Marinas0e056f22009-07-24 12:32:58 +0100870 W(b) __fa526_cache_on
871 W(b) __armv4_mmu_cache_off
872 W(b) __fa526_cache_flush
Paulius Zaleckas28853ac2009-03-25 13:10:01 +0200873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 @ These match on the architecture ID
875
876 .word 0x00020000 @ ARMv4T
877 .word 0x000f0000
Catalin Marinas0e056f22009-07-24 12:32:58 +0100878 W(b) __armv4_mmu_cache_on
879 W(b) __armv4_mmu_cache_off
880 W(b) __armv4_mmu_cache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 .word 0x00050000 @ ARMv5TE
883 .word 0x000f0000
Catalin Marinas0e056f22009-07-24 12:32:58 +0100884 W(b) __armv4_mmu_cache_on
885 W(b) __armv4_mmu_cache_off
886 W(b) __armv4_mmu_cache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 .word 0x00060000 @ ARMv5TEJ
889 .word 0x000f0000
Catalin Marinas0e056f22009-07-24 12:32:58 +0100890 W(b) __armv4_mmu_cache_on
891 W(b) __armv4_mmu_cache_off
Sascha Hauer75216852010-03-15 15:14:50 +0100892 W(b) __armv5tej_mmu_cache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Catalin Marinas45a7b9c2006-06-18 16:21:50 +0100894 .word 0x0007b000 @ ARMv6
Catalin Marinas7d09e852007-06-01 17:14:53 +0100895 .word 0x000ff000
Catalin Marinas0e056f22009-07-24 12:32:58 +0100896 W(b) __armv4_mmu_cache_on
897 W(b) __armv4_mmu_cache_off
898 W(b) __armv6_mmu_cache_flush
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Catalin Marinas7d09e852007-06-01 17:14:53 +0100900 .word 0x000f0000 @ new CPU Id
901 .word 0x000f0000
Catalin Marinas0e056f22009-07-24 12:32:58 +0100902 W(b) __armv7_mmu_cache_on
903 W(b) __armv7_mmu_cache_off
904 W(b) __armv7_mmu_cache_flush
Catalin Marinas7d09e852007-06-01 17:14:53 +0100905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 .word 0 @ unrecognised type
907 .word 0
908 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100909 THUMB( nop )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100911 THUMB( nop )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 mov pc, lr
Catalin Marinas0e056f22009-07-24 12:32:58 +0100913 THUMB( nop )
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 .size proc_types, . - proc_types
916
Dave Martin946a1052011-06-14 14:20:44 +0100917 /*
918 * If you get a "non-constant expression in ".if" statement"
919 * error from the assembler on this line, check that you have
920 * not accidentally written a "b" instruction where you should
921 * have written W(b).
922 */
923 .if (. - proc_types) % PROC_ENTRY_SIZE != 0
924 .error "The size of one or more proc_types entries is wrong."
925 .endif
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927/*
928 * Turn off the Cache and MMU. ARMv3 does not support
929 * reading the control register, but ARMv4 does.
930 *
Uwe Kleine-König21b28412010-01-26 22:08:09 +0100931 * On exit,
932 * r0, r1, r2, r3, r9, r12 corrupted
933 * This routine must preserve:
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +0100934 * r4, r7, r8
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 */
936 .align 5
937cache_off: mov r3, #12 @ cache_off function
938 b call_cache_fn
939
Hyok S. Choi10c2df62006-03-27 10:21:34 +0100940__armv4_mpu_cache_off:
941 mrc p15, 0, r0, c1, c0
942 bic r0, r0, #0x000d
943 mcr p15, 0, r0, c1, c0 @ turn MPU and cache off
944 mov r0, #0
945 mcr p15, 0, r0, c7, c10, 4 @ drain write buffer
946 mcr p15, 0, r0, c7, c6, 0 @ flush D-Cache
947 mcr p15, 0, r0, c7, c5, 0 @ flush I-Cache
948 mov pc, lr
949
950__armv3_mpu_cache_off:
951 mrc p15, 0, r0, c1, c0
952 bic r0, r0, #0x000d
953 mcr p15, 0, r0, c1, c0, 0 @ turn MPU and cache off
954 mov r0, #0
955 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
956 mov pc, lr
957
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000958__armv4_mmu_cache_off:
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100959#ifdef CONFIG_MMU
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 mrc p15, 0, r0, c1, c0
961 bic r0, r0, #0x000d
962 mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
963 mov r0, #0
964 mcr p15, 0, r0, c7, c7 @ invalidate whole cache v4
965 mcr p15, 0, r0, c8, c7 @ invalidate whole TLB v4
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100966#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 mov pc, lr
968
Catalin Marinas7d09e852007-06-01 17:14:53 +0100969__armv7_mmu_cache_off:
970 mrc p15, 0, r0, c1, c0
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100971#ifdef CONFIG_MMU
Catalin Marinas7d09e852007-06-01 17:14:53 +0100972 bic r0, r0, #0x000d
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100973#else
974 bic r0, r0, #0x000c
975#endif
Catalin Marinas7d09e852007-06-01 17:14:53 +0100976 mcr p15, 0, r0, c1, c0 @ turn MMU and cache off
977 mov r12, lr
978 bl __armv7_mmu_cache_flush
979 mov r0, #0
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100980#ifdef CONFIG_MMU
Catalin Marinas7d09e852007-06-01 17:14:53 +0100981 mcr p15, 0, r0, c8, c7, 0 @ invalidate whole TLB
Catalin Marinas8bdca0a2009-07-24 12:35:06 +0100982#endif
Catalin Marinasc30c2f92008-11-06 13:23:07 +0000983 mcr p15, 0, r0, c7, c5, 6 @ invalidate BTC
984 mcr p15, 0, r0, c7, c10, 4 @ DSB
985 mcr p15, 0, r0, c7, c5, 4 @ ISB
Catalin Marinas7d09e852007-06-01 17:14:53 +0100986 mov pc, r12
987
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000988__arm6_mmu_cache_off:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 mov r0, #0x00000030 @ ARM6 control reg.
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000990 b __armv3_mmu_cache_off
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000992__arm7_mmu_cache_off:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 mov r0, #0x00000070 @ ARM7 control reg.
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000994 b __armv3_mmu_cache_off
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Hyok S. Choic76b6b42006-03-24 09:53:18 +0000996__armv3_mmu_cache_off:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 mcr p15, 0, r0, c1, c0, 0 @ turn MMU and cache off
998 mov r0, #0
999 mcr p15, 0, r0, c7, c0, 0 @ invalidate whole cache v3
1000 mcr p15, 0, r0, c5, c0, 0 @ invalidate whole TLB v3
1001 mov pc, lr
1002
1003/*
1004 * Clean and flush the cache to maintain consistency.
1005 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 * On exit,
Uwe Kleine-König21b28412010-01-26 22:08:09 +01001007 * r1, r2, r3, r9, r10, r11, r12 corrupted
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 * This routine must preserve:
Nicolas Pitre6d7d0ae2011-02-21 07:06:45 +01001009 * r4, r6, r7, r8
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
1011 .align 5
1012cache_clean_flush:
1013 mov r3, #16
1014 b call_cache_fn
1015
Hyok S. Choi10c2df62006-03-27 10:21:34 +01001016__armv4_mpu_cache_flush:
1017 mov r2, #1
1018 mov r3, #0
1019 mcr p15, 0, ip, c7, c6, 0 @ invalidate D cache
1020 mov r1, #7 << 5 @ 8 segments
10211: orr r3, r1, #63 << 26 @ 64 entries
10222: mcr p15, 0, r3, c7, c14, 2 @ clean & invalidate D index
1023 subs r3, r3, #1 << 26
1024 bcs 2b @ entries 63 to 0
1025 subs r1, r1, #1 << 5
1026 bcs 1b @ segments 7 to 0
1027
1028 teq r2, #0
1029 mcrne p15, 0, ip, c7, c5, 0 @ invalidate I cache
1030 mcr p15, 0, ip, c7, c10, 4 @ drain WB
1031 mov pc, lr
1032
Paulius Zaleckas28853ac2009-03-25 13:10:01 +02001033__fa526_cache_flush:
1034 mov r1, #0
1035 mcr p15, 0, r1, c7, c14, 0 @ clean and invalidate D cache
1036 mcr p15, 0, r1, c7, c5, 0 @ flush I cache
1037 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1038 mov pc, lr
Hyok S. Choi10c2df62006-03-27 10:21:34 +01001039
Hyok S. Choic76b6b42006-03-24 09:53:18 +00001040__armv6_mmu_cache_flush:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 mov r1, #0
1042 mcr p15, 0, r1, c7, c14, 0 @ clean+invalidate D
1043 mcr p15, 0, r1, c7, c5, 0 @ invalidate I+BTB
1044 mcr p15, 0, r1, c7, c15, 0 @ clean+invalidate unified
1045 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1046 mov pc, lr
1047
Catalin Marinas7d09e852007-06-01 17:14:53 +01001048__armv7_mmu_cache_flush:
1049 mrc p15, 0, r10, c0, c1, 5 @ read ID_MMFR1
1050 tst r10, #0xf << 16 @ hierarchical cache (ARMv7)
Catalin Marinas7d09e852007-06-01 17:14:53 +01001051 mov r10, #0
Catalin Marinasc30c2f92008-11-06 13:23:07 +00001052 beq hierarchical
Catalin Marinas7d09e852007-06-01 17:14:53 +01001053 mcr p15, 0, r10, c7, c14, 0 @ clean+invalidate D
1054 b iflush
1055hierarchical:
Catalin Marinasc30c2f92008-11-06 13:23:07 +00001056 mcr p15, 0, r10, c7, c10, 5 @ DMB
Catalin Marinas0e056f22009-07-24 12:32:58 +01001057 stmfd sp!, {r0-r7, r9-r11}
Catalin Marinas7d09e852007-06-01 17:14:53 +01001058 mrc p15, 1, r0, c0, c0, 1 @ read clidr
1059 ands r3, r0, #0x7000000 @ extract loc from clidr
1060 mov r3, r3, lsr #23 @ left align loc bit field
1061 beq finished @ if loc is 0, then no need to clean
1062 mov r10, #0 @ start clean at cache level 0
1063loop1:
1064 add r2, r10, r10, lsr #1 @ work out 3x current cache level
1065 mov r1, r0, lsr r2 @ extract cache type bits from clidr
1066 and r1, r1, #7 @ mask of the bits for current cache only
1067 cmp r1, #2 @ see what cache we have at this level
1068 blt skip @ skip if no cache, or just i-cache
1069 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
1070 mcr p15, 0, r10, c7, c5, 4 @ isb to sych the new cssr&csidr
1071 mrc p15, 1, r1, c0, c0, 0 @ read the new csidr
1072 and r2, r1, #7 @ extract the length of the cache lines
1073 add r2, r2, #4 @ add 4 (line length offset)
1074 ldr r4, =0x3ff
1075 ands r4, r4, r1, lsr #3 @ find maximum number on the way size
Catalin Marinas000b5022008-10-03 11:09:10 +01001076 clz r5, r4 @ find bit position of way size increment
Catalin Marinas7d09e852007-06-01 17:14:53 +01001077 ldr r7, =0x7fff
1078 ands r7, r7, r1, lsr #13 @ extract max number of the index size
1079loop2:
1080 mov r9, r4 @ create working copy of max way size
1081loop3:
Catalin Marinas0e056f22009-07-24 12:32:58 +01001082 ARM( orr r11, r10, r9, lsl r5 ) @ factor way and cache number into r11
1083 ARM( orr r11, r11, r7, lsl r2 ) @ factor index number into r11
1084 THUMB( lsl r6, r9, r5 )
1085 THUMB( orr r11, r10, r6 ) @ factor way and cache number into r11
1086 THUMB( lsl r6, r7, r2 )
1087 THUMB( orr r11, r11, r6 ) @ factor index number into r11
Catalin Marinas7d09e852007-06-01 17:14:53 +01001088 mcr p15, 0, r11, c7, c14, 2 @ clean & invalidate by set/way
1089 subs r9, r9, #1 @ decrement the way
1090 bge loop3
1091 subs r7, r7, #1 @ decrement the index
1092 bge loop2
1093skip:
1094 add r10, r10, #2 @ increment cache number
1095 cmp r3, r10
1096 bgt loop1
1097finished:
Catalin Marinas0e056f22009-07-24 12:32:58 +01001098 ldmfd sp!, {r0-r7, r9-r11}
Catalin Marinas7d09e852007-06-01 17:14:53 +01001099 mov r10, #0 @ swith back to cache level 0
1100 mcr p15, 2, r10, c0, c0, 0 @ select current cache level in cssr
Catalin Marinas7d09e852007-06-01 17:14:53 +01001101iflush:
Catalin Marinasc30c2f92008-11-06 13:23:07 +00001102 mcr p15, 0, r10, c7, c10, 4 @ DSB
Catalin Marinas7d09e852007-06-01 17:14:53 +01001103 mcr p15, 0, r10, c7, c5, 0 @ invalidate I+BTB
Catalin Marinasc30c2f92008-11-06 13:23:07 +00001104 mcr p15, 0, r10, c7, c10, 4 @ DSB
1105 mcr p15, 0, r10, c7, c5, 4 @ ISB
Catalin Marinas7d09e852007-06-01 17:14:53 +01001106 mov pc, lr
1107
Nicolas Pitre15754bf2007-10-31 15:15:29 -04001108__armv5tej_mmu_cache_flush:
11091: mrc p15, 0, r15, c7, c14, 3 @ test,clean,invalidate D cache
1110 bne 1b
1111 mcr p15, 0, r0, c7, c5, 0 @ flush I cache
1112 mcr p15, 0, r0, c7, c10, 4 @ drain WB
1113 mov pc, lr
1114
Hyok S. Choic76b6b42006-03-24 09:53:18 +00001115__armv4_mmu_cache_flush:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 mov r2, #64*1024 @ default: 32K dcache size (*2)
1117 mov r11, #32 @ default: 32 byte line size
1118 mrc p15, 0, r3, c0, c0, 1 @ read cache type
Russell King98e12b52010-02-25 23:56:38 +00001119 teq r3, r9 @ cache ID register present?
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 beq no_cache_id
1121 mov r1, r3, lsr #18
1122 and r1, r1, #7
1123 mov r2, #1024
1124 mov r2, r2, lsl r1 @ base dcache size *2
1125 tst r3, #1 << 14 @ test M bit
1126 addne r2, r2, r2, lsr #1 @ +1/2 size if M == 1
1127 mov r3, r3, lsr #12
1128 and r3, r3, #3
1129 mov r11, #8
1130 mov r11, r11, lsl r3 @ cache line size in bytes
1131no_cache_id:
Catalin Marinas0e056f22009-07-24 12:32:58 +01001132 mov r1, pc
1133 bic r1, r1, #63 @ align to longest cache line
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 add r2, r1, r2
Catalin Marinas0e056f22009-07-24 12:32:58 +010011351:
1136 ARM( ldr r3, [r1], r11 ) @ s/w flush D cache
1137 THUMB( ldr r3, [r1] ) @ s/w flush D cache
1138 THUMB( add r1, r1, r11 )
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 teq r1, r2
1140 bne 1b
1141
1142 mcr p15, 0, r1, c7, c5, 0 @ flush I cache
1143 mcr p15, 0, r1, c7, c6, 0 @ flush D cache
1144 mcr p15, 0, r1, c7, c10, 4 @ drain WB
1145 mov pc, lr
1146
Hyok S. Choic76b6b42006-03-24 09:53:18 +00001147__armv3_mmu_cache_flush:
Hyok S. Choi10c2df62006-03-27 10:21:34 +01001148__armv3_mpu_cache_flush:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 mov r1, #0
Uwe Kleine-König63fa7182010-01-26 22:18:09 +01001150 mcr p15, 0, r1, c7, c0, 0 @ invalidate whole cache v3
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 mov pc, lr
1152
1153/*
1154 * Various debugging routines for printing hex characters and
1155 * memory, which again must be relocatable.
1156 */
1157#ifdef DEBUG
Catalin Marinas88987ef2009-07-24 12:32:52 +01001158 .align 2
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 .type phexbuf,#object
1160phexbuf: .space 12
1161 .size phexbuf, . - phexbuf
1162
Uwe Kleine-Königbe6f9f02010-01-26 22:22:20 +01001163@ phex corrupts {r0, r1, r2, r3}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164phex: adr r3, phexbuf
1165 mov r2, #0
1166 strb r2, [r3, r1]
11671: subs r1, r1, #1
1168 movmi r0, r3
1169 bmi puts
1170 and r2, r0, #15
1171 mov r0, r0, lsr #4
1172 cmp r2, #10
1173 addge r2, r2, #7
1174 add r2, r2, #'0'
1175 strb r2, [r3, r1]
1176 b 1b
1177
Uwe Kleine-Königbe6f9f02010-01-26 22:22:20 +01001178@ puts corrupts {r0, r1, r2, r3}
Tony Lindgren4e6d4882010-02-01 23:26:53 +01001179puts: loadsp r3, r1
Linus Torvalds1da177e2005-04-16 15:20:36 -070011801: ldrb r2, [r0], #1
1181 teq r2, #0
1182 moveq pc, lr
Russell King5cd0c342005-05-03 12:18:46 +010011832: writeb r2, r3
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 mov r1, #0x00020000
11853: subs r1, r1, #1
1186 bne 3b
1187 teq r2, #'\n'
1188 moveq r2, #'\r'
1189 beq 2b
1190 teq r0, #0
1191 bne 1b
1192 mov pc, lr
Uwe Kleine-Königbe6f9f02010-01-26 22:22:20 +01001193@ putc corrupts {r0, r1, r2, r3}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194putc:
1195 mov r2, r0
1196 mov r0, #0
Tony Lindgren4e6d4882010-02-01 23:26:53 +01001197 loadsp r3, r1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 b 2b
1199
Uwe Kleine-Königbe6f9f02010-01-26 22:22:20 +01001200@ memdump corrupts {r0, r1, r2, r3, r10, r11, r12, lr}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201memdump: mov r12, r0
1202 mov r10, lr
1203 mov r11, #0
12042: mov r0, r11, lsl #2
1205 add r0, r0, r12
1206 mov r1, #8
1207 bl phex
1208 mov r0, #':'
1209 bl putc
12101: mov r0, #' '
1211 bl putc
1212 ldr r0, [r12, r11, lsl #2]
1213 mov r1, #8
1214 bl phex
1215 and r0, r11, #7
1216 teq r0, #3
1217 moveq r0, #' '
1218 bleq putc
1219 and r0, r11, #7
1220 add r11, r11, #1
1221 teq r0, #7
1222 bne 1b
1223 mov r0, #'\n'
1224 bl putc
1225 cmp r11, #64
1226 blt 2b
1227 mov pc, r10
1228#endif
1229
Catalin Marinas92c83ff2007-06-22 14:27:50 +01001230 .ltorg
Nicolas Pitreadcc2592011-04-27 16:15:11 -04001231reloc_code_end:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 .align
Russell Kingb0c4d4e2010-11-22 12:00:59 +00001234 .section ".stack", "aw", %nobits
Nicolas Pitre8d7e4cc2011-04-27 14:54:39 -04001235.L_user_stack: .space 4096
1236.L_user_stack_end: