head.S 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * linux/arch/arm/kernel/head.S
  3. *
  4. * Copyright (C) 1994-2002 Russell King
  5. * Copyright (c) 2003 ARM Limited
  6. * All Rights Reserved
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Kernel startup code for all 32-bit CPUs
  13. */
  14. #include <linux/config.h>
  15. #include <linux/linkage.h>
  16. #include <linux/init.h>
  17. #include <asm/assembler.h>
  18. #include <asm/domain.h>
  19. #include <asm/mach-types.h>
  20. #include <asm/procinfo.h>
  21. #include <asm/ptrace.h>
  22. #include <asm/constants.h>
  23. #include <asm/thread_info.h>
  24. #include <asm/system.h>
  25. #define PROCINFO_MMUFLAGS 8
  26. #define PROCINFO_INITFUNC 12
  27. #define MACHINFO_TYPE 0
  28. #define MACHINFO_PHYSRAM 4
  29. #define MACHINFO_PHYSIO 8
  30. #define MACHINFO_PGOFFIO 12
  31. #define MACHINFO_NAME 16
  32. #ifndef CONFIG_XIP_KERNEL
  33. /*
  34. * We place the page tables 16K below TEXTADDR. Therefore, we must make sure
  35. * that TEXTADDR is correctly set. Currently, we expect the least significant
  36. * 16 bits to be 0x8000, but we could probably relax this restriction to
  37. * TEXTADDR >= PAGE_OFFSET + 0x4000
  38. *
  39. * Note that swapper_pg_dir is the virtual address of the page tables, and
  40. * pgtbl gives us a position-independent reference to these tables. We can
  41. * do this because stext == TEXTADDR
  42. */
  43. #if (TEXTADDR & 0xffff) != 0x8000
  44. #error TEXTADDR must start at 0xXXXX8000
  45. #endif
  46. .globl swapper_pg_dir
  47. .equ swapper_pg_dir, TEXTADDR - 0x4000
  48. .macro pgtbl, rd, phys
  49. adr \rd, stext
  50. sub \rd, \rd, #0x4000
  51. .endm
  52. #else
  53. /*
  54. * XIP Kernel:
  55. *
  56. * We place the page tables 16K below DATAADDR. Therefore, we must make sure
  57. * that DATAADDR is correctly set. Currently, we expect the least significant
  58. * 16 bits to be 0x8000, but we could probably relax this restriction to
  59. * DATAADDR >= PAGE_OFFSET + 0x4000
  60. *
  61. * Note that pgtbl is meant to return the physical address of swapper_pg_dir.
  62. * We can't make it relative to the kernel position in this case since
  63. * the kernel can physically be anywhere.
  64. */
  65. #if (DATAADDR & 0xffff) != 0x8000
  66. #error DATAADDR must start at 0xXXXX8000
  67. #endif
  68. .globl swapper_pg_dir
  69. .equ swapper_pg_dir, DATAADDR - 0x4000
  70. .macro pgtbl, rd, phys
  71. ldr \rd, =((DATAADDR - 0x4000) - VIRT_OFFSET)
  72. add \rd, \rd, \phys
  73. .endm
  74. #endif
  75. /*
  76. * Kernel startup entry point.
  77. * ---------------------------
  78. *
  79. * This is normally called from the decompressor code. The requirements
  80. * are: MMU = off, D-cache = off, I-cache = dont care, r0 = 0,
  81. * r1 = machine nr.
  82. *
  83. * This code is mostly position independent, so if you link the kernel at
  84. * 0xc0008000, you call this at __pa(0xc0008000).
  85. *
  86. * See linux/arch/arm/tools/mach-types for the complete list of machine
  87. * numbers for r1.
  88. *
  89. * We're trying to keep crap to a minimum; DO NOT add any machine specific
  90. * crap here - that's what the boot loader (or in extreme, well justified
  91. * circumstances, zImage) is for.
  92. */
  93. __INIT
  94. .type stext, %function
  95. ENTRY(stext)
  96. msr cpsr_c, #PSR_F_BIT | PSR_I_BIT | MODE_SVC @ ensure svc mode
  97. @ and irqs disabled
  98. bl __lookup_processor_type @ r5=procinfo r9=cpuid
  99. movs r10, r5 @ invalid processor (r5=0)?
  100. beq __error_p @ yes, error 'p'
  101. bl __lookup_machine_type @ r5=machinfo
  102. movs r8, r5 @ invalid machine (r5=0)?
  103. beq __error_a @ yes, error 'a'
  104. bl __create_page_tables
  105. /*
  106. * The following calls CPU specific code in a position independent
  107. * manner. See arch/arm/mm/proc-*.S for details. r10 = base of
  108. * xxx_proc_info structure selected by __lookup_machine_type
  109. * above. On return, the CPU will be ready for the MMU to be
  110. * turned on, and r0 will hold the CPU control register value.
  111. */
  112. ldr r13, __switch_data @ address to jump to after
  113. @ mmu has been enabled
  114. adr lr, __enable_mmu @ return (PIC) address
  115. add pc, r10, #PROCINFO_INITFUNC
  116. .type __switch_data, %object
  117. __switch_data:
  118. .long __mmap_switched
  119. .long __data_loc @ r4
  120. .long __data_start @ r5
  121. .long __bss_start @ r6
  122. .long _end @ r7
  123. .long processor_id @ r4
  124. .long __machine_arch_type @ r5
  125. .long cr_alignment @ r6
  126. .long init_thread_union + THREAD_START_SP @ sp
  127. /*
  128. * The following fragment of code is executed with the MMU on, and uses
  129. * absolute addresses; this is not position independent.
  130. *
  131. * r0 = cp#15 control register
  132. * r1 = machine ID
  133. * r9 = processor ID
  134. */
  135. .type __mmap_switched, %function
  136. __mmap_switched:
  137. adr r3, __switch_data + 4
  138. ldmia r3!, {r4, r5, r6, r7}
  139. cmp r4, r5 @ Copy data segment if needed
  140. 1: cmpne r5, r6
  141. ldrne fp, [r4], #4
  142. strne fp, [r5], #4
  143. bne 1b
  144. mov fp, #0 @ Clear BSS (and zero fp)
  145. 1: cmp r6, r7
  146. strcc fp, [r6],#4
  147. bcc 1b
  148. ldmia r3, {r4, r5, r6, sp}
  149. str r9, [r4] @ Save processor ID
  150. str r1, [r5] @ Save machine type
  151. bic r4, r0, #CR_A @ Clear 'A' bit
  152. stmia r6, {r0, r4} @ Save control register values
  153. b start_kernel
  154. #if defined(CONFIG_SMP)
  155. .type secondary_startup, #function
  156. ENTRY(secondary_startup)
  157. /*
  158. * Common entry point for secondary CPUs.
  159. *
  160. * Ensure that we're in SVC mode, and IRQs are disabled. Lookup
  161. * the processor type - there is no need to check the machine type
  162. * as it has already been validated by the primary processor.
  163. */
  164. msr cpsr_c, #PSR_F_BIT | PSR_I_BIT | MODE_SVC
  165. bl __lookup_processor_type
  166. movs r10, r5 @ invalid processor?
  167. moveq r0, #'p' @ yes, error 'p'
  168. beq __error
  169. /*
  170. * Use the page tables supplied from __cpu_up.
  171. */
  172. adr r4, __secondary_data
  173. ldmia r4, {r5, r6, r13} @ address to jump to after
  174. sub r4, r4, r5 @ mmu has been enabled
  175. ldr r4, [r6, r4] @ get secondary_data.pgdir
  176. adr lr, __enable_mmu @ return address
  177. add pc, r10, #12 @ initialise processor
  178. @ (return control reg)
  179. /*
  180. * r6 = &secondary_data
  181. */
  182. ENTRY(__secondary_switched)
  183. ldr sp, [r6, #4] @ get secondary_data.stack
  184. mov fp, #0
  185. b secondary_start_kernel
  186. .type __secondary_data, %object
  187. __secondary_data:
  188. .long .
  189. .long secondary_data
  190. .long __secondary_switched
  191. #endif /* defined(CONFIG_SMP) */
  192. /*
  193. * Setup common bits before finally enabling the MMU. Essentially
  194. * this is just loading the page table pointer and domain access
  195. * registers.
  196. */
  197. .type __enable_mmu, %function
  198. __enable_mmu:
  199. #ifdef CONFIG_ALIGNMENT_TRAP
  200. orr r0, r0, #CR_A
  201. #else
  202. bic r0, r0, #CR_A
  203. #endif
  204. #ifdef CONFIG_CPU_DCACHE_DISABLE
  205. bic r0, r0, #CR_C
  206. #endif
  207. #ifdef CONFIG_CPU_BPREDICT_DISABLE
  208. bic r0, r0, #CR_Z
  209. #endif
  210. #ifdef CONFIG_CPU_ICACHE_DISABLE
  211. bic r0, r0, #CR_I
  212. #endif
  213. mov r5, #(domain_val(DOMAIN_USER, DOMAIN_MANAGER) | \
  214. domain_val(DOMAIN_KERNEL, DOMAIN_MANAGER) | \
  215. domain_val(DOMAIN_TABLE, DOMAIN_MANAGER) | \
  216. domain_val(DOMAIN_IO, DOMAIN_CLIENT))
  217. mcr p15, 0, r5, c3, c0, 0 @ load domain access register
  218. mcr p15, 0, r4, c2, c0, 0 @ load page table pointer
  219. b __turn_mmu_on
  220. /*
  221. * Enable the MMU. This completely changes the structure of the visible
  222. * memory space. You will not be able to trace execution through this.
  223. * If you have an enquiry about this, *please* check the linux-arm-kernel
  224. * mailing list archives BEFORE sending another post to the list.
  225. *
  226. * r0 = cp#15 control register
  227. * r13 = *virtual* address to jump to upon completion
  228. *
  229. * other registers depend on the function called upon completion
  230. */
  231. .align 5
  232. .type __turn_mmu_on, %function
  233. __turn_mmu_on:
  234. mov r0, r0
  235. mcr p15, 0, r0, c1, c0, 0 @ write control reg
  236. mrc p15, 0, r3, c0, c0, 0 @ read id reg
  237. mov r3, r3
  238. mov r3, r3
  239. mov pc, r13
  240. /*
  241. * Setup the initial page tables. We only setup the barest
  242. * amount which are required to get the kernel running, which
  243. * generally means mapping in the kernel code.
  244. *
  245. * r8 = machinfo
  246. * r9 = cpuid
  247. * r10 = procinfo
  248. *
  249. * Returns:
  250. * r0, r3, r5, r6, r7 corrupted
  251. * r4 = physical page table address
  252. */
  253. .type __create_page_tables, %function
  254. __create_page_tables:
  255. ldr r5, [r8, #MACHINFO_PHYSRAM] @ physram
  256. pgtbl r4, r5 @ page table address
  257. /*
  258. * Clear the 16K level 1 swapper page table
  259. */
  260. mov r0, r4
  261. mov r3, #0
  262. add r6, r0, #0x4000
  263. 1: str r3, [r0], #4
  264. str r3, [r0], #4
  265. str r3, [r0], #4
  266. str r3, [r0], #4
  267. teq r0, r6
  268. bne 1b
  269. ldr r7, [r10, #PROCINFO_MMUFLAGS] @ mmuflags
  270. /*
  271. * Create identity mapping for first MB of kernel to
  272. * cater for the MMU enable. This identity mapping
  273. * will be removed by paging_init(). We use our current program
  274. * counter to determine corresponding section base address.
  275. */
  276. mov r6, pc, lsr #20 @ start of kernel section
  277. orr r3, r7, r6, lsl #20 @ flags + kernel base
  278. str r3, [r4, r6, lsl #2] @ identity mapping
  279. /*
  280. * Now setup the pagetables for our kernel direct
  281. * mapped region. We round TEXTADDR down to the
  282. * nearest megabyte boundary. It is assumed that
  283. * the kernel fits within 4 contigous 1MB sections.
  284. */
  285. add r0, r4, #(TEXTADDR & 0xff000000) >> 18 @ start of kernel
  286. str r3, [r0, #(TEXTADDR & 0x00f00000) >> 18]!
  287. add r3, r3, #1 << 20
  288. str r3, [r0, #4]! @ KERNEL + 1MB
  289. add r3, r3, #1 << 20
  290. str r3, [r0, #4]! @ KERNEL + 2MB
  291. add r3, r3, #1 << 20
  292. str r3, [r0, #4] @ KERNEL + 3MB
  293. /*
  294. * Then map first 1MB of ram in case it contains our boot params.
  295. */
  296. add r0, r4, #VIRT_OFFSET >> 18
  297. orr r6, r5, r7
  298. str r6, [r0]
  299. #ifdef CONFIG_XIP_KERNEL
  300. /*
  301. * Map some ram to cover our .data and .bss areas.
  302. * Mapping 3MB should be plenty.
  303. */
  304. sub r3, r4, r5
  305. mov r3, r3, lsr #20
  306. add r0, r0, r3, lsl #2
  307. add r6, r6, r3, lsl #20
  308. str r6, [r0], #4
  309. add r6, r6, #(1 << 20)
  310. str r6, [r0], #4
  311. add r6, r6, #(1 << 20)
  312. str r6, [r0]
  313. #endif
  314. #ifdef CONFIG_DEBUG_LL
  315. bic r7, r7, #0x0c @ turn off cacheable
  316. @ and bufferable bits
  317. /*
  318. * Map in IO space for serial debugging.
  319. * This allows debug messages to be output
  320. * via a serial console before paging_init.
  321. */
  322. ldr r3, [r8, #MACHINFO_PGOFFIO]
  323. add r0, r4, r3
  324. rsb r3, r3, #0x4000 @ PTRS_PER_PGD*sizeof(long)
  325. cmp r3, #0x0800 @ limit to 512MB
  326. movhi r3, #0x0800
  327. add r6, r0, r3
  328. ldr r3, [r8, #MACHINFO_PHYSIO]
  329. orr r3, r3, r7
  330. 1: str r3, [r0], #4
  331. add r3, r3, #1 << 20
  332. teq r0, r6
  333. bne 1b
  334. #if defined(CONFIG_ARCH_NETWINDER) || defined(CONFIG_ARCH_CATS)
  335. /*
  336. * If we're using the NetWinder, we need to map in
  337. * the 16550-type serial port for the debug messages
  338. */
  339. teq r1, #MACH_TYPE_NETWINDER
  340. teqne r1, #MACH_TYPE_CATS
  341. bne 1f
  342. add r0, r4, #0xff000000 >> 18
  343. orr r3, r7, #0x7c000000
  344. str r3, [r0]
  345. 1:
  346. #endif
  347. #ifdef CONFIG_ARCH_RPC
  348. /*
  349. * Map in screen at 0x02000000 & SCREEN2_BASE
  350. * Similar reasons here - for debug. This is
  351. * only for Acorn RiscPC architectures.
  352. */
  353. add r0, r4, #0x02000000 >> 18
  354. orr r3, r7, #0x02000000
  355. str r3, [r0]
  356. add r0, r4, #0xd8000000 >> 18
  357. str r3, [r0]
  358. #endif
  359. #endif
  360. mov pc, lr
  361. .ltorg
  362. /*
  363. * Exception handling. Something went wrong and we can't proceed. We
  364. * ought to tell the user, but since we don't have any guarantee that
  365. * we're even running on the right architecture, we do virtually nothing.
  366. *
  367. * If CONFIG_DEBUG_LL is set we try to print out something about the error
  368. * and hope for the best (useful if bootloader fails to pass a proper
  369. * machine ID for example).
  370. */
  371. .type __error_p, %function
  372. __error_p:
  373. #ifdef CONFIG_DEBUG_LL
  374. adr r0, str_p1
  375. bl printascii
  376. b __error
  377. str_p1: .asciz "\nError: unrecognized/unsupported processor variant.\n"
  378. .align
  379. #endif
  380. .type __error_a, %function
  381. __error_a:
  382. #ifdef CONFIG_DEBUG_LL
  383. mov r4, r1 @ preserve machine ID
  384. adr r0, str_a1
  385. bl printascii
  386. mov r0, r4
  387. bl printhex8
  388. adr r0, str_a2
  389. bl printascii
  390. adr r3, 3f
  391. ldmia r3, {r4, r5, r6} @ get machine desc list
  392. sub r4, r3, r4 @ get offset between virt&phys
  393. add r5, r5, r4 @ convert virt addresses to
  394. add r6, r6, r4 @ physical address space
  395. 1: ldr r0, [r5, #MACHINFO_TYPE] @ get machine type
  396. bl printhex8
  397. mov r0, #'\t'
  398. bl printch
  399. ldr r0, [r5, #MACHINFO_NAME] @ get machine name
  400. add r0, r0, r4
  401. bl printascii
  402. mov r0, #'\n'
  403. bl printch
  404. add r5, r5, #SIZEOF_MACHINE_DESC @ next machine_desc
  405. cmp r5, r6
  406. blo 1b
  407. adr r0, str_a3
  408. bl printascii
  409. b __error
  410. str_a1: .asciz "\nError: unrecognized/unsupported machine ID (r1 = 0x"
  411. str_a2: .asciz ").\n\nAvailable machine support:\n\nID (hex)\tNAME\n"
  412. str_a3: .asciz "\nPlease check your kernel config and/or bootloader.\n"
  413. .align
  414. #endif
  415. .type __error, %function
  416. __error:
  417. #ifdef CONFIG_ARCH_RPC
  418. /*
  419. * Turn the screen red on a error - RiscPC only.
  420. */
  421. mov r0, #0x02000000
  422. mov r3, #0x11
  423. orr r3, r3, r3, lsl #8
  424. orr r3, r3, r3, lsl #16
  425. str r3, [r0], #4
  426. str r3, [r0], #4
  427. str r3, [r0], #4
  428. str r3, [r0], #4
  429. #endif
  430. 1: mov r0, r0
  431. b 1b
  432. /*
  433. * Read processor ID register (CP#15, CR0), and look up in the linker-built
  434. * supported processor list. Note that we can't use the absolute addresses
  435. * for the __proc_info lists since we aren't running with the MMU on
  436. * (and therefore, we are not in the correct address space). We have to
  437. * calculate the offset.
  438. *
  439. * Returns:
  440. * r3, r4, r6 corrupted
  441. * r5 = proc_info pointer in physical address space
  442. * r9 = cpuid
  443. */
  444. .type __lookup_processor_type, %function
  445. __lookup_processor_type:
  446. adr r3, 3f
  447. ldmda r3, {r5, r6, r9}
  448. sub r3, r3, r9 @ get offset between virt&phys
  449. add r5, r5, r3 @ convert virt addresses to
  450. add r6, r6, r3 @ physical address space
  451. mrc p15, 0, r9, c0, c0 @ get processor id
  452. 1: ldmia r5, {r3, r4} @ value, mask
  453. and r4, r4, r9 @ mask wanted bits
  454. teq r3, r4
  455. beq 2f
  456. add r5, r5, #PROC_INFO_SZ @ sizeof(proc_info_list)
  457. cmp r5, r6
  458. blo 1b
  459. mov r5, #0 @ unknown processor
  460. 2: mov pc, lr
  461. /*
  462. * This provides a C-API version of the above function.
  463. */
  464. ENTRY(lookup_processor_type)
  465. stmfd sp!, {r4 - r6, r9, lr}
  466. bl __lookup_processor_type
  467. mov r0, r5
  468. ldmfd sp!, {r4 - r6, r9, pc}
  469. /*
  470. * Look in include/asm-arm/procinfo.h and arch/arm/kernel/arch.[ch] for
  471. * more information about the __proc_info and __arch_info structures.
  472. */
  473. .long __proc_info_begin
  474. .long __proc_info_end
  475. 3: .long .
  476. .long __arch_info_begin
  477. .long __arch_info_end
  478. /*
  479. * Lookup machine architecture in the linker-build list of architectures.
  480. * Note that we can't use the absolute addresses for the __arch_info
  481. * lists since we aren't running with the MMU on (and therefore, we are
  482. * not in the correct address space). We have to calculate the offset.
  483. *
  484. * r1 = machine architecture number
  485. * Returns:
  486. * r3, r4, r6 corrupted
  487. * r5 = mach_info pointer in physical address space
  488. */
  489. .type __lookup_machine_type, %function
  490. __lookup_machine_type:
  491. adr r3, 3b
  492. ldmia r3, {r4, r5, r6}
  493. sub r3, r3, r4 @ get offset between virt&phys
  494. add r5, r5, r3 @ convert virt addresses to
  495. add r6, r6, r3 @ physical address space
  496. 1: ldr r3, [r5, #MACHINFO_TYPE] @ get machine type
  497. teq r3, r1 @ matches loader number?
  498. beq 2f @ found
  499. add r5, r5, #SIZEOF_MACHINE_DESC @ next machine_desc
  500. cmp r5, r6
  501. blo 1b
  502. mov r5, #0 @ unknown machine
  503. 2: mov pc, lr
  504. /*
  505. * This provides a C-API version of the above function.
  506. */
  507. ENTRY(lookup_machine_type)
  508. stmfd sp!, {r4 - r6, lr}
  509. mov r1, r0
  510. bl __lookup_machine_type
  511. mov r0, r5
  512. ldmfd sp!, {r4 - r6, pc}