head.S 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Copyright (C) 2007-2009 Michal Simek <[email protected]>
  3. * Copyright (C) 2007-2009 PetaLogix
  4. * Copyright (C) 2006 Atmark Techno, Inc.
  5. *
  6. * MMU code derived from arch/ppc/kernel/head_4xx.S:
  7. * Copyright (c) 1995-1996 Gary Thomas <[email protected]>
  8. * Initial PowerPC version.
  9. * Copyright (c) 1996 Cort Dougan <[email protected]>
  10. * Rewritten for PReP
  11. * Copyright (c) 1996 Paul Mackerras <[email protected]>
  12. * Low-level exception handers, MMU support, and rewrite.
  13. * Copyright (c) 1997 Dan Malek <[email protected]>
  14. * PowerPC 8xx modifications.
  15. * Copyright (c) 1998-1999 TiVo, Inc.
  16. * PowerPC 403GCX modifications.
  17. * Copyright (c) 1999 Grant Erickson <[email protected]>
  18. * PowerPC 403GCX/405GP modifications.
  19. * Copyright 2000 MontaVista Software Inc.
  20. * PPC405 modifications
  21. * PowerPC 403GCX/405GP modifications.
  22. * Author: MontaVista Software, Inc.
  23. * [email protected] or [email protected]
  24. * [email protected]
  25. *
  26. * This file is subject to the terms and conditions of the GNU General Public
  27. * License. See the file "COPYING" in the main directory of this archive
  28. * for more details.
  29. */
  30. #include <linux/init.h>
  31. #include <linux/linkage.h>
  32. #include <asm/thread_info.h>
  33. #include <asm/page.h>
  34. #include <linux/of_fdt.h> /* for OF_DT_HEADER */
  35. #include <asm/setup.h> /* COMMAND_LINE_SIZE */
  36. #include <asm/mmu.h>
  37. #include <asm/processor.h>
  38. .section .data
  39. .global empty_zero_page
  40. .align 12
  41. empty_zero_page:
  42. .space PAGE_SIZE
  43. .global swapper_pg_dir
  44. swapper_pg_dir:
  45. .space PAGE_SIZE
  46. .section .rodata
  47. .align 4
  48. endian_check:
  49. .word 1
  50. __HEAD
  51. ENTRY(_start)
  52. #if CONFIG_KERNEL_BASE_ADDR == 0
  53. brai TOPHYS(real_start)
  54. .org 0x100
  55. real_start:
  56. #endif
  57. mts rmsr, r0
  58. /* Disable stack protection from bootloader */
  59. mts rslr, r0
  60. addi r8, r0, 0xFFFFFFFF
  61. mts rshr, r8
  62. /*
  63. * According to Xilinx, msrclr instruction behaves like 'mfs rX,rpc'
  64. * if the msrclr instruction is not enabled. We use this to detect
  65. * if the opcode is available, by issuing msrclr and then testing the result.
  66. * r8 == 0 - msr instructions are implemented
  67. * r8 != 0 - msr instructions are not implemented
  68. */
  69. mfs r1, rmsr
  70. msrclr r8, 0 /* clear nothing - just read msr for test */
  71. cmpu r8, r8, r1 /* r1 must contain msr reg content */
  72. /* r7 may point to an FDT, or there may be one linked in.
  73. if it's in r7, we've got to save it away ASAP.
  74. We ensure r7 points to a valid FDT, just in case the bootloader
  75. is broken or non-existent */
  76. beqi r7, no_fdt_arg /* NULL pointer? don't copy */
  77. /* Does r7 point to a valid FDT? Load HEADER magic number */
  78. /* Run time Big/Little endian platform */
  79. /* Save 1 as word and load byte - 0 - BIG, 1 - LITTLE */
  80. lbui r11, r0, TOPHYS(endian_check)
  81. beqid r11, big_endian /* DO NOT break delay stop dependency */
  82. lw r11, r0, r7 /* Big endian load in delay slot */
  83. lwr r11, r0, r7 /* Little endian load */
  84. big_endian:
  85. rsubi r11, r11, OF_DT_HEADER /* Check FDT header */
  86. beqi r11, _prepare_copy_fdt
  87. or r7, r0, r0 /* clear R7 when not valid DTB */
  88. bnei r11, no_fdt_arg /* No - get out of here */
  89. _prepare_copy_fdt:
  90. or r11, r0, r0 /* incremment */
  91. ori r4, r0, TOPHYS(_fdt_start)
  92. ori r3, r0, (0x10000 - 4)
  93. _copy_fdt:
  94. lw r12, r7, r11 /* r12 = r7 + r11 */
  95. sw r12, r4, r11 /* addr[r4 + r11] = r12 */
  96. addik r11, r11, 4 /* increment counting */
  97. bgtid r3, _copy_fdt /* loop for all entries */
  98. addik r3, r3, -4 /* descrement loop */
  99. no_fdt_arg:
  100. #ifndef CONFIG_CMDLINE_BOOL
  101. /*
  102. * handling command line
  103. * copy command line directly to cmd_line placed in data section.
  104. */
  105. beqid r5, skip /* Skip if NULL pointer */
  106. or r11, r0, r0 /* incremment */
  107. ori r4, r0, cmd_line /* load address of command line */
  108. tophys(r4,r4) /* convert to phys address */
  109. ori r3, r0, COMMAND_LINE_SIZE - 1 /* number of loops */
  110. _copy_command_line:
  111. /* r2=r5+r11 - r5 contain pointer to command line */
  112. lbu r2, r5, r11
  113. beqid r2, skip /* Skip if no data */
  114. sb r2, r4, r11 /* addr[r4+r11]= r2 */
  115. addik r11, r11, 1 /* increment counting */
  116. bgtid r3, _copy_command_line /* loop for all entries */
  117. addik r3, r3, -1 /* decrement loop */
  118. addik r5, r4, 0 /* add new space for command line */
  119. tovirt(r5,r5)
  120. skip:
  121. #endif /* CONFIG_CMDLINE_BOOL */
  122. #ifdef NOT_COMPILE
  123. /* save bram context */
  124. or r11, r0, r0 /* incremment */
  125. ori r4, r0, TOPHYS(_bram_load_start) /* save bram context */
  126. ori r3, r0, (LMB_SIZE - 4)
  127. _copy_bram:
  128. lw r7, r0, r11 /* r7 = r0 + r11 */
  129. sw r7, r4, r11 /* addr[r4 + r11] = r7 */
  130. addik r11, r11, 4 /* increment counting */
  131. bgtid r3, _copy_bram /* loop for all entries */
  132. addik r3, r3, -4 /* descrement loop */
  133. #endif
  134. /* We have to turn on the MMU right away. */
  135. /*
  136. * Set up the initial MMU state so we can do the first level of
  137. * kernel initialization. This maps the first 16 MBytes of memory 1:1
  138. * virtual to physical.
  139. */
  140. nop
  141. addik r3, r0, MICROBLAZE_TLB_SIZE -1 /* Invalidate all TLB entries */
  142. _invalidate:
  143. mts rtlbx, r3
  144. mts rtlbhi, r0 /* flush: ensure V is clear */
  145. mts rtlblo, r0
  146. bgtid r3, _invalidate /* loop for all entries */
  147. addik r3, r3, -1
  148. /* sync */
  149. /* Setup the kernel PID */
  150. mts rpid,r0 /* Load the kernel PID */
  151. nop
  152. bri 4
  153. /*
  154. * We should still be executing code at physical address area
  155. * RAM_BASEADDR at this point. However, kernel code is at
  156. * a virtual address. So, set up a TLB mapping to cover this once
  157. * translation is enabled.
  158. */
  159. addik r3,r0, CONFIG_KERNEL_START /* Load the kernel virtual address */
  160. tophys(r4,r3) /* Load the kernel physical address */
  161. /* start to do TLB calculation */
  162. addik r12, r0, _end
  163. rsub r12, r3, r12
  164. addik r12, r12, CONFIG_LOWMEM_SIZE >> PTE_SHIFT /* that's the pad */
  165. or r9, r0, r0 /* TLB0 = 0 */
  166. or r10, r0, r0 /* TLB1 = 0 */
  167. addik r11, r12, -0x1000000
  168. bgei r11, GT16 /* size is greater than 16MB */
  169. addik r11, r12, -0x0800000
  170. bgei r11, GT8 /* size is greater than 8MB */
  171. addik r11, r12, -0x0400000
  172. bgei r11, GT4 /* size is greater than 4MB */
  173. /* size is less than 4MB */
  174. addik r11, r12, -0x0200000
  175. bgei r11, GT2 /* size is greater than 2MB */
  176. addik r9, r0, 0x0100000 /* TLB0 must be 1MB */
  177. addik r11, r12, -0x0100000
  178. bgei r11, GT1 /* size is greater than 1MB */
  179. /* TLB1 is 0 which is setup above */
  180. bri tlb_end
  181. GT4: /* r11 contains the rest - will be either 1 or 4 */
  182. ori r9, r0, 0x400000 /* TLB0 is 4MB */
  183. bri TLB1
  184. GT16: /* TLB0 is 16MB */
  185. addik r9, r0, 0x1000000 /* means TLB0 is 16MB */
  186. TLB1:
  187. /* must be used r2 because of subtract if failed */
  188. addik r2, r11, -0x0400000
  189. bgei r2, GT20 /* size is greater than 16MB */
  190. /* size is >16MB and <20MB */
  191. addik r11, r11, -0x0100000
  192. bgei r11, GT17 /* size is greater than 17MB */
  193. /* kernel is >16MB and < 17MB */
  194. GT1:
  195. addik r10, r0, 0x0100000 /* means TLB1 is 1MB */
  196. bri tlb_end
  197. GT2: /* TLB0 is 0 and TLB1 will be 4MB */
  198. GT17: /* TLB1 is 4MB - kernel size <20MB */
  199. addik r10, r0, 0x0400000 /* means TLB1 is 4MB */
  200. bri tlb_end
  201. GT8: /* TLB0 is still zero that's why I can use only TLB1 */
  202. GT20: /* TLB1 is 16MB - kernel size >20MB */
  203. addik r10, r0, 0x1000000 /* means TLB1 is 16MB */
  204. tlb_end:
  205. /*
  206. * Configure and load two entries into TLB slots 0 and 1.
  207. * In case we are pinning TLBs, these are reserved in by the
  208. * other TLB functions. If not reserving, then it doesn't
  209. * matter where they are loaded.
  210. */
  211. andi r4,r4,0xfffffc00 /* Mask off the real page number */
  212. ori r4,r4,(TLB_WR | TLB_EX) /* Set the write and execute bits */
  213. /*
  214. * TLB0 is always used - check if is not zero (r9 stores TLB0 value)
  215. * if is use TLB1 value and clear it (r10 stores TLB1 value)
  216. */
  217. bnei r9, tlb0_not_zero
  218. add r9, r10, r0
  219. add r10, r0, r0
  220. tlb0_not_zero:
  221. /* look at the code below */
  222. ori r30, r0, 0x200
  223. andi r29, r9, 0x100000
  224. bneid r29, 1f
  225. addik r30, r30, 0x80
  226. andi r29, r9, 0x400000
  227. bneid r29, 1f
  228. addik r30, r30, 0x80
  229. andi r29, r9, 0x1000000
  230. bneid r29, 1f
  231. addik r30, r30, 0x80
  232. 1:
  233. andi r3,r3,0xfffffc00 /* Mask off the effective page number */
  234. ori r3,r3,(TLB_VALID)
  235. or r3, r3, r30
  236. /* Load tlb_skip size value which is index to first unused TLB entry */
  237. lwi r11, r0, TOPHYS(tlb_skip)
  238. mts rtlbx,r11 /* TLB slow 0 */
  239. mts rtlblo,r4 /* Load the data portion of the entry */
  240. mts rtlbhi,r3 /* Load the tag portion of the entry */
  241. /* Increase tlb_skip size */
  242. addik r11, r11, 1
  243. swi r11, r0, TOPHYS(tlb_skip)
  244. /* TLB1 can be zeroes that's why we not setup it */
  245. beqi r10, jump_over2
  246. /* look at the code below */
  247. ori r30, r0, 0x200
  248. andi r29, r10, 0x100000
  249. bneid r29, 1f
  250. addik r30, r30, 0x80
  251. andi r29, r10, 0x400000
  252. bneid r29, 1f
  253. addik r30, r30, 0x80
  254. andi r29, r10, 0x1000000
  255. bneid r29, 1f
  256. addik r30, r30, 0x80
  257. 1:
  258. addk r4, r4, r9 /* previous addr + TLB0 size */
  259. addk r3, r3, r9
  260. andi r3,r3,0xfffffc00 /* Mask off the effective page number */
  261. ori r3,r3,(TLB_VALID)
  262. or r3, r3, r30
  263. lwi r11, r0, TOPHYS(tlb_skip)
  264. mts rtlbx, r11 /* r11 is used from TLB0 */
  265. mts rtlblo,r4 /* Load the data portion of the entry */
  266. mts rtlbhi,r3 /* Load the tag portion of the entry */
  267. /* Increase tlb_skip size */
  268. addik r11, r11, 1
  269. swi r11, r0, TOPHYS(tlb_skip)
  270. jump_over2:
  271. /*
  272. * Load a TLB entry for LMB, since we need access to
  273. * the exception vectors, using a 4k real==virtual mapping.
  274. */
  275. /* Use temporary TLB_ID for LMB - clear this temporary mapping later */
  276. ori r11, r0, MICROBLAZE_LMB_TLB_ID
  277. mts rtlbx,r11
  278. ori r4,r0,(TLB_WR | TLB_EX)
  279. ori r3,r0,(TLB_VALID | TLB_PAGESZ(PAGESZ_4K))
  280. mts rtlblo,r4 /* Load the data portion of the entry */
  281. mts rtlbhi,r3 /* Load the tag portion of the entry */
  282. /*
  283. * We now have the lower 16 Meg of RAM mapped into TLB entries, and the
  284. * caches ready to work.
  285. */
  286. turn_on_mmu:
  287. ori r15,r0,start_here
  288. ori r4,r0,MSR_KERNEL_VMS
  289. mts rmsr,r4
  290. nop
  291. rted r15,0 /* enables MMU */
  292. nop
  293. start_here:
  294. /* Initialize small data anchors */
  295. addik r13, r0, _KERNEL_SDA_BASE_
  296. addik r2, r0, _KERNEL_SDA2_BASE_
  297. /* Initialize stack pointer */
  298. addik r1, r0, init_thread_union + THREAD_SIZE - 4
  299. /* Initialize r31 with current task address */
  300. addik r31, r0, init_task
  301. addik r11, r0, machine_early_init
  302. brald r15, r11
  303. nop
  304. /*
  305. * Initialize the MMU.
  306. */
  307. bralid r15, mmu_init
  308. nop
  309. /* Go back to running unmapped so we can load up new values
  310. * and change to using our exception vectors.
  311. * On the MicroBlaze, all we invalidate the used TLB entries to clear
  312. * the old 16M byte TLB mappings.
  313. */
  314. ori r15,r0,TOPHYS(kernel_load_context)
  315. ori r4,r0,MSR_KERNEL
  316. mts rmsr,r4
  317. nop
  318. bri 4
  319. rted r15,0
  320. nop
  321. /* Load up the kernel context */
  322. kernel_load_context:
  323. ori r5, r0, MICROBLAZE_LMB_TLB_ID
  324. mts rtlbx,r5
  325. nop
  326. mts rtlbhi,r0
  327. nop
  328. addi r15, r0, machine_halt
  329. ori r17, r0, start_kernel
  330. ori r4, r0, MSR_KERNEL_VMS
  331. mts rmsr, r4
  332. nop
  333. rted r17, 0 /* enable MMU and jump to start_kernel */
  334. nop