head_85xx.S 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Kernel execution entry point code.
  4. *
  5. * Copyright (c) 1995-1996 Gary Thomas <gdt@linuxppc.org>
  6. * Initial PowerPC version.
  7. * Copyright (c) 1996 Cort Dougan <cort@cs.nmt.edu>
  8. * Rewritten for PReP
  9. * Copyright (c) 1996 Paul Mackerras <paulus@cs.anu.edu.au>
  10. * Low-level exception handers, MMU support, and rewrite.
  11. * Copyright (c) 1997 Dan Malek <dmalek@jlc.net>
  12. * PowerPC 8xx modifications.
  13. * Copyright (c) 1998-1999 TiVo, Inc.
  14. * PowerPC 403GCX modifications.
  15. * Copyright (c) 1999 Grant Erickson <grant@lcse.umn.edu>
  16. * PowerPC 403GCX/405GP modifications.
  17. * Copyright 2000 MontaVista Software Inc.
  18. * PPC405 modifications
  19. * PowerPC 403GCX/405GP modifications.
  20. * Author: MontaVista Software, Inc.
  21. * frank_rowand@mvista.com or source@mvista.com
  22. * debbie_chu@mvista.com
  23. * Copyright 2002-2004 MontaVista Software, Inc.
  24. * PowerPC 44x support, Matt Porter <mporter@kernel.crashing.org>
  25. * Copyright 2004 Freescale Semiconductor, Inc
  26. * PowerPC e500 modifications, Kumar Gala <galak@kernel.crashing.org>
  27. */
  28. #include <linux/init.h>
  29. #include <linux/threads.h>
  30. #include <linux/pgtable.h>
  31. #include <linux/linkage.h>
  32. #include <asm/processor.h>
  33. #include <asm/page.h>
  34. #include <asm/mmu.h>
  35. #include <asm/cputable.h>
  36. #include <asm/thread_info.h>
  37. #include <asm/ppc_asm.h>
  38. #include <asm/asm-offsets.h>
  39. #include <asm/cache.h>
  40. #include <asm/ptrace.h>
  41. #include <asm/feature-fixups.h>
  42. #include "head_booke.h"
  43. /* As with the other PowerPC ports, it is expected that when code
  44. * execution begins here, the following registers contain valid, yet
  45. * optional, information:
  46. *
  47. * r3 - Board info structure pointer (DRAM, frequency, MAC address, etc.)
  48. * r4 - Starting address of the init RAM disk
  49. * r5 - Ending address of the init RAM disk
  50. * r6 - Start of kernel command line string (e.g. "mem=128")
  51. * r7 - End of kernel command line string
  52. *
  53. */
  54. __HEAD
  55. _GLOBAL(_stext);
  56. _GLOBAL(_start);
  57. /*
  58. * Reserve a word at a fixed location to store the address
  59. * of abatron_pteptrs
  60. */
  61. nop
  62. /* Translate device tree address to physical, save in r30/r31 */
  63. bl get_phys_addr
  64. mr r30,r3
  65. mr r31,r4
  66. li r25,0 /* phys kernel start (low) */
  67. li r24,0 /* CPU number */
  68. li r23,0 /* phys kernel start (high) */
  69. #ifdef CONFIG_RELOCATABLE
  70. LOAD_REG_ADDR_PIC(r3, _stext) /* Get our current runtime base */
  71. /* Translate _stext address to physical, save in r23/r25 */
  72. bl get_phys_addr
  73. mr r23,r3
  74. mr r25,r4
  75. bcl 20,31,$+4
  76. 0: mflr r8
  77. addis r3,r8,(is_second_reloc - 0b)@ha
  78. lwz r19,(is_second_reloc - 0b)@l(r3)
  79. /* Check if this is the second relocation. */
  80. cmpwi r19,1
  81. bne 1f
  82. /*
  83. * For the second relocation, we already get the real memstart_addr
  84. * from device tree. So we will map PAGE_OFFSET to memstart_addr,
  85. * then the virtual address of start kernel should be:
  86. * PAGE_OFFSET + (kernstart_addr - memstart_addr)
  87. * Since the offset between kernstart_addr and memstart_addr should
  88. * never be beyond 1G, so we can just use the lower 32bit of them
  89. * for the calculation.
  90. */
  91. lis r3,PAGE_OFFSET@h
  92. addis r4,r8,(kernstart_addr - 0b)@ha
  93. addi r4,r4,(kernstart_addr - 0b)@l
  94. lwz r5,4(r4)
  95. addis r6,r8,(memstart_addr - 0b)@ha
  96. addi r6,r6,(memstart_addr - 0b)@l
  97. lwz r7,4(r6)
  98. subf r5,r7,r5
  99. add r3,r3,r5
  100. b 2f
  101. 1:
  102. /*
  103. * We have the runtime (virtual) address of our base.
  104. * We calculate our shift of offset from a 64M page.
  105. * We could map the 64M page we belong to at PAGE_OFFSET and
  106. * get going from there.
  107. */
  108. lis r4,KERNELBASE@h
  109. ori r4,r4,KERNELBASE@l
  110. rlwinm r6,r25,0,0x3ffffff /* r6 = PHYS_START % 64M */
  111. rlwinm r5,r4,0,0x3ffffff /* r5 = KERNELBASE % 64M */
  112. subf r3,r5,r6 /* r3 = r6 - r5 */
  113. add r3,r4,r3 /* Required Virtual Address */
  114. 2: bl relocate
  115. /*
  116. * For the second relocation, we already set the right tlb entries
  117. * for the kernel space, so skip the code in 85xx_entry_mapping.S
  118. */
  119. cmpwi r19,1
  120. beq set_ivor
  121. #endif
  122. /* We try to not make any assumptions about how the boot loader
  123. * setup or used the TLBs. We invalidate all mappings from the
  124. * boot loader and load a single entry in TLB1[0] to map the
  125. * first 64M of kernel memory. Any boot info passed from the
  126. * bootloader needs to live in this first 64M.
  127. *
  128. * Requirement on bootloader:
  129. * - The page we're executing in needs to reside in TLB1 and
  130. * have IPROT=1. If not an invalidate broadcast could
  131. * evict the entry we're currently executing in.
  132. *
  133. * r3 = Index of TLB1 were executing in
  134. * r4 = Current MSR[IS]
  135. * r5 = Index of TLB1 temp mapping
  136. *
  137. * Later in mapin_ram we will correctly map lowmem, and resize TLB1[0]
  138. * if needed
  139. */
  140. _GLOBAL(__early_start)
  141. LOAD_REG_ADDR_PIC(r20, kernstart_virt_addr)
  142. lwz r20,0(r20)
  143. #define ENTRY_MAPPING_BOOT_SETUP
  144. #include "85xx_entry_mapping.S"
  145. #undef ENTRY_MAPPING_BOOT_SETUP
  146. set_ivor:
  147. /* Establish the interrupt vector offsets */
  148. SET_IVOR(0, CriticalInput);
  149. SET_IVOR(1, MachineCheck);
  150. SET_IVOR(2, DataStorage);
  151. SET_IVOR(3, InstructionStorage);
  152. SET_IVOR(4, ExternalInput);
  153. SET_IVOR(5, Alignment);
  154. SET_IVOR(6, Program);
  155. SET_IVOR(7, FloatingPointUnavailable);
  156. SET_IVOR(8, SystemCall);
  157. SET_IVOR(9, AuxillaryProcessorUnavailable);
  158. SET_IVOR(10, Decrementer);
  159. SET_IVOR(11, FixedIntervalTimer);
  160. SET_IVOR(12, WatchdogTimer);
  161. SET_IVOR(13, DataTLBError);
  162. SET_IVOR(14, InstructionTLBError);
  163. SET_IVOR(15, DebugCrit);
  164. /* Establish the interrupt vector base */
  165. lis r4,interrupt_base@h /* IVPR only uses the high 16-bits */
  166. mtspr SPRN_IVPR,r4
  167. /* Setup the defaults for TLB entries */
  168. li r2,(MAS4_TSIZED(BOOK3E_PAGESZ_4K))@l
  169. mtspr SPRN_MAS4, r2
  170. #if !defined(CONFIG_BDI_SWITCH)
  171. /*
  172. * The Abatron BDI JTAG debugger does not tolerate others
  173. * mucking with the debug registers.
  174. */
  175. lis r2,DBCR0_IDM@h
  176. mtspr SPRN_DBCR0,r2
  177. isync
  178. /* clear any residual debug events */
  179. li r2,-1
  180. mtspr SPRN_DBSR,r2
  181. #endif
  182. #ifdef CONFIG_SMP
  183. /* Check to see if we're the second processor, and jump
  184. * to the secondary_start code if so
  185. */
  186. LOAD_REG_ADDR_PIC(r24, boot_cpuid)
  187. lwz r24, 0(r24)
  188. cmpwi r24, -1
  189. mfspr r24,SPRN_PIR
  190. bne __secondary_start
  191. #endif
  192. /*
  193. * This is where the main kernel code starts.
  194. */
  195. /* ptr to current */
  196. lis r2,init_task@h
  197. ori r2,r2,init_task@l
  198. /* ptr to current thread */
  199. addi r4,r2,THREAD /* init task's THREAD */
  200. mtspr SPRN_SPRG_THREAD,r4
  201. /* stack */
  202. lis r1,init_thread_union@h
  203. ori r1,r1,init_thread_union@l
  204. li r0,0
  205. stwu r0,THREAD_SIZE-STACK_FRAME_MIN_SIZE(r1)
  206. #ifdef CONFIG_SMP
  207. stw r24, TASK_CPU(r2)
  208. #endif
  209. bl early_init
  210. #ifdef CONFIG_KASAN
  211. bl kasan_early_init
  212. #endif
  213. #ifdef CONFIG_RELOCATABLE
  214. mr r3,r30
  215. mr r4,r31
  216. #ifdef CONFIG_PHYS_64BIT
  217. mr r5,r23
  218. mr r6,r25
  219. #else
  220. mr r5,r25
  221. #endif
  222. bl relocate_init
  223. #endif
  224. #ifdef CONFIG_DYNAMIC_MEMSTART
  225. lis r3,kernstart_addr@ha
  226. la r3,kernstart_addr@l(r3)
  227. #ifdef CONFIG_PHYS_64BIT
  228. stw r23,0(r3)
  229. stw r25,4(r3)
  230. #else
  231. stw r25,0(r3)
  232. #endif
  233. #endif
  234. /*
  235. * Decide what sort of machine this is and initialize the MMU.
  236. */
  237. mr r3,r30
  238. mr r4,r31
  239. bl machine_init
  240. bl MMU_init
  241. /* Setup PTE pointers for the Abatron bdiGDB */
  242. lis r6, swapper_pg_dir@h
  243. ori r6, r6, swapper_pg_dir@l
  244. lis r5, abatron_pteptrs@h
  245. ori r5, r5, abatron_pteptrs@l
  246. lis r3, kernstart_virt_addr@ha
  247. lwz r4, kernstart_virt_addr@l(r3)
  248. stw r5, 0(r4) /* Save abatron_pteptrs at a fixed location */
  249. stw r6, 0(r5)
  250. /* Let's move on */
  251. lis r4,start_kernel@h
  252. ori r4,r4,start_kernel@l
  253. lis r3,MSR_KERNEL@h
  254. ori r3,r3,MSR_KERNEL@l
  255. mtspr SPRN_SRR0,r4
  256. mtspr SPRN_SRR1,r3
  257. rfi /* change context and jump to start_kernel */
  258. /* Macros to hide the PTE size differences
  259. *
  260. * FIND_PTE -- walks the page tables given EA & pgdir pointer
  261. * r10 -- free
  262. * r11 -- PGDIR pointer
  263. * r12 -- free
  264. * r13 -- EA of fault
  265. * label 2: is the bailout case
  266. *
  267. * if we find the pte (fall through):
  268. * r11 is low pte word
  269. * r12 is pointer to the pte
  270. * r10 is the pshift from the PGD, if we're a hugepage
  271. */
  272. #ifdef CONFIG_PTE_64BIT
  273. #ifdef CONFIG_HUGETLB_PAGE
  274. #define FIND_PTE \
  275. rlwinm r12, r13, 14, 18, 28; /* Compute pgdir/pmd offset */ \
  276. add r12, r11, r12; \
  277. lwz r11, 4(r12); /* Get pgd/pmd entry */ \
  278. rlwinm. r10, r11, 32 - _PAGE_PSIZE_SHIFT, 0x1e; /* get tsize*/ \
  279. bne 1000f; /* Huge page (leaf entry) */ \
  280. rlwinm. r12, r11, 0, 0, 20; /* Extract pt base address */ \
  281. beq 2f; /* Bail if no table */ \
  282. rlwimi r12, r13, 23, 20, 28; /* Compute pte address */ \
  283. li r10, 0; /* clear r10 */ \
  284. lwz r11, 4(r12); /* Get pte entry */ \
  285. 1000:
  286. #else
  287. #define FIND_PTE \
  288. rlwinm r12, r13, 14, 18, 28; /* Compute pgdir/pmd offset */ \
  289. add r12, r11, r12; \
  290. lwz r11, 4(r12); /* Get pgd/pmd entry */ \
  291. rlwinm. r12, r11, 0, 0, 20; /* Extract pt base address */ \
  292. beq 2f; /* Bail if no table */ \
  293. rlwimi r12, r13, 23, 20, 28; /* Compute pte address */ \
  294. lwz r11, 4(r12); /* Get pte entry */
  295. #endif /* HUGEPAGE */
  296. #else /* !PTE_64BIT */
  297. #define FIND_PTE \
  298. rlwimi r11, r13, 12, 20, 29; /* Create L1 (pgdir/pmd) address */ \
  299. lwz r11, 0(r11); /* Get L1 entry */ \
  300. rlwinm. r12, r11, 0, 0, 19; /* Extract L2 (pte) base address */ \
  301. beq 2f; /* Bail if no table */ \
  302. rlwimi r12, r13, 22, 20, 29; /* Compute PTE address */ \
  303. lwz r11, 0(r12); /* Get Linux PTE */
  304. #endif
  305. /*
  306. * Interrupt vector entry code
  307. *
  308. * The Book E MMUs are always on so we don't need to handle
  309. * interrupts in real mode as with previous PPC processors. In
  310. * this case we handle interrupts in the kernel virtual address
  311. * space.
  312. *
  313. * Interrupt vectors are dynamically placed relative to the
  314. * interrupt prefix as determined by the address of interrupt_base.
  315. * The interrupt vectors offsets are programmed using the labels
  316. * for each interrupt vector entry.
  317. *
  318. * Interrupt vectors must be aligned on a 16 byte boundary.
  319. * We align on a 32 byte cache line boundary for good measure.
  320. */
  321. interrupt_base:
  322. /* Critical Input Interrupt */
  323. CRITICAL_EXCEPTION(0x0100, CRITICAL, CriticalInput, unknown_exception)
  324. /* Machine Check Interrupt */
  325. MCHECK_EXCEPTION(0x0200, MachineCheck, machine_check_exception)
  326. /* Data Storage Interrupt */
  327. START_EXCEPTION(DataStorage)
  328. NORMAL_EXCEPTION_PROLOG(0x300, DATA_STORAGE)
  329. mfspr r5,SPRN_ESR /* Grab the ESR, save it */
  330. stw r5,_ESR(r11)
  331. mfspr r4,SPRN_DEAR /* Grab the DEAR, save it */
  332. stw r4, _DEAR(r11)
  333. andis. r10,r5,(ESR_ILK|ESR_DLK)@h
  334. bne 1f
  335. prepare_transfer_to_handler
  336. bl do_page_fault
  337. b interrupt_return
  338. 1:
  339. prepare_transfer_to_handler
  340. bl CacheLockingException
  341. b interrupt_return
  342. /* Instruction Storage Interrupt */
  343. INSTRUCTION_STORAGE_EXCEPTION
  344. /* External Input Interrupt */
  345. EXCEPTION(0x0500, EXTERNAL, ExternalInput, do_IRQ)
  346. /* Alignment Interrupt */
  347. ALIGNMENT_EXCEPTION
  348. /* Program Interrupt */
  349. PROGRAM_EXCEPTION
  350. /* Floating Point Unavailable Interrupt */
  351. #ifdef CONFIG_PPC_FPU
  352. FP_UNAVAILABLE_EXCEPTION
  353. #else
  354. EXCEPTION(0x0800, FP_UNAVAIL, FloatingPointUnavailable, emulation_assist_interrupt)
  355. #endif
  356. /* System Call Interrupt */
  357. START_EXCEPTION(SystemCall)
  358. SYSCALL_ENTRY 0xc00 BOOKE_INTERRUPT_SYSCALL SPRN_SRR1
  359. /* Auxiliary Processor Unavailable Interrupt */
  360. EXCEPTION(0x2900, AP_UNAVAIL, AuxillaryProcessorUnavailable, unknown_exception)
  361. /* Decrementer Interrupt */
  362. DECREMENTER_EXCEPTION
  363. /* Fixed Internal Timer Interrupt */
  364. /* TODO: Add FIT support */
  365. EXCEPTION(0x3100, FIT, FixedIntervalTimer, unknown_exception)
  366. /* Watchdog Timer Interrupt */
  367. #ifdef CONFIG_BOOKE_WDT
  368. CRITICAL_EXCEPTION(0x3200, WATCHDOG, WatchdogTimer, WatchdogException)
  369. #else
  370. CRITICAL_EXCEPTION(0x3200, WATCHDOG, WatchdogTimer, unknown_exception)
  371. #endif
  372. /* Data TLB Error Interrupt */
  373. START_EXCEPTION(DataTLBError)
  374. mtspr SPRN_SPRG_WSCRATCH0, r10 /* Save some working registers */
  375. mfspr r10, SPRN_SPRG_THREAD
  376. stw r11, THREAD_NORMSAVE(0)(r10)
  377. #ifdef CONFIG_KVM_BOOKE_HV
  378. BEGIN_FTR_SECTION
  379. mfspr r11, SPRN_SRR1
  380. END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
  381. #endif
  382. stw r12, THREAD_NORMSAVE(1)(r10)
  383. stw r13, THREAD_NORMSAVE(2)(r10)
  384. mfcr r13
  385. stw r13, THREAD_NORMSAVE(3)(r10)
  386. DO_KVM BOOKE_INTERRUPT_DTLB_MISS SPRN_SRR1
  387. START_BTB_FLUSH_SECTION
  388. mfspr r11, SPRN_SRR1
  389. andi. r10,r11,MSR_PR
  390. beq 1f
  391. BTB_FLUSH(r10)
  392. 1:
  393. END_BTB_FLUSH_SECTION
  394. mfspr r13, SPRN_DEAR /* Get faulting address */
  395. /* If we are faulting a kernel address, we have to use the
  396. * kernel page tables.
  397. */
  398. lis r11, PAGE_OFFSET@h
  399. cmplw 5, r13, r11
  400. blt 5, 3f
  401. lis r11, swapper_pg_dir@h
  402. ori r11, r11, swapper_pg_dir@l
  403. mfspr r12,SPRN_MAS1 /* Set TID to 0 */
  404. rlwinm r12,r12,0,16,1
  405. mtspr SPRN_MAS1,r12
  406. b 4f
  407. /* Get the PGD for the current thread */
  408. 3:
  409. mfspr r11,SPRN_SPRG_THREAD
  410. lwz r11,PGDIR(r11)
  411. #ifdef CONFIG_PPC_KUAP
  412. mfspr r12, SPRN_MAS1
  413. rlwinm. r12,r12,0,0x3fff0000
  414. beq 2f /* KUAP fault */
  415. #endif
  416. 4:
  417. FIND_PTE
  418. #ifdef CONFIG_PTE_64BIT
  419. li r13,_PAGE_PRESENT|_PAGE_BAP_SR
  420. oris r13,r13,_PAGE_ACCESSED@h
  421. #else
  422. li r13,_PAGE_PRESENT|_PAGE_READ|_PAGE_ACCESSED
  423. #endif
  424. andc. r13,r13,r11 /* Check permission */
  425. #ifdef CONFIG_PTE_64BIT
  426. #ifdef CONFIG_SMP
  427. subf r13,r11,r12 /* create false data dep */
  428. lwzx r13,r11,r13 /* Get upper pte bits */
  429. #else
  430. lwz r13,0(r12) /* Get upper pte bits */
  431. #endif
  432. #endif
  433. bne 2f /* Bail if permission/valid mismatch */
  434. /* Jump to common tlb load */
  435. b finish_tlb_load
  436. 2:
  437. /* The bailout. Restore registers to pre-exception conditions
  438. * and call the heavyweights to help us out.
  439. */
  440. mfspr r10, SPRN_SPRG_THREAD
  441. lwz r11, THREAD_NORMSAVE(3)(r10)
  442. mtcr r11
  443. lwz r13, THREAD_NORMSAVE(2)(r10)
  444. lwz r12, THREAD_NORMSAVE(1)(r10)
  445. lwz r11, THREAD_NORMSAVE(0)(r10)
  446. mfspr r10, SPRN_SPRG_RSCRATCH0
  447. b DataStorage
  448. /* Instruction TLB Error Interrupt */
  449. /*
  450. * Nearly the same as above, except we get our
  451. * information from different registers and bailout
  452. * to a different point.
  453. */
  454. START_EXCEPTION(InstructionTLBError)
  455. mtspr SPRN_SPRG_WSCRATCH0, r10 /* Save some working registers */
  456. mfspr r10, SPRN_SPRG_THREAD
  457. stw r11, THREAD_NORMSAVE(0)(r10)
  458. #ifdef CONFIG_KVM_BOOKE_HV
  459. BEGIN_FTR_SECTION
  460. mfspr r11, SPRN_SRR1
  461. END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
  462. #endif
  463. stw r12, THREAD_NORMSAVE(1)(r10)
  464. stw r13, THREAD_NORMSAVE(2)(r10)
  465. mfcr r13
  466. stw r13, THREAD_NORMSAVE(3)(r10)
  467. DO_KVM BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR1
  468. START_BTB_FLUSH_SECTION
  469. mfspr r11, SPRN_SRR1
  470. andi. r10,r11,MSR_PR
  471. beq 1f
  472. BTB_FLUSH(r10)
  473. 1:
  474. END_BTB_FLUSH_SECTION
  475. mfspr r13, SPRN_SRR0 /* Get faulting address */
  476. /* If we are faulting a kernel address, we have to use the
  477. * kernel page tables.
  478. */
  479. lis r11, PAGE_OFFSET@h
  480. cmplw 5, r13, r11
  481. blt 5, 3f
  482. lis r11, swapper_pg_dir@h
  483. ori r11, r11, swapper_pg_dir@l
  484. mfspr r12,SPRN_MAS1 /* Set TID to 0 */
  485. rlwinm r12,r12,0,16,1
  486. mtspr SPRN_MAS1,r12
  487. FIND_PTE
  488. /* Make up the required permissions for kernel code */
  489. #ifdef CONFIG_PTE_64BIT
  490. li r13,_PAGE_PRESENT | _PAGE_BAP_SX
  491. oris r13,r13,_PAGE_ACCESSED@h
  492. #else
  493. li r13,_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_EXEC
  494. #endif
  495. b 4f
  496. /* Get the PGD for the current thread */
  497. 3:
  498. mfspr r11,SPRN_SPRG_THREAD
  499. lwz r11,PGDIR(r11)
  500. #ifdef CONFIG_PPC_KUAP
  501. mfspr r12, SPRN_MAS1
  502. rlwinm. r12,r12,0,0x3fff0000
  503. beq 2f /* KUAP fault */
  504. #endif
  505. FIND_PTE
  506. /* Make up the required permissions for user code */
  507. #ifdef CONFIG_PTE_64BIT
  508. li r13,_PAGE_PRESENT | _PAGE_BAP_UX
  509. oris r13,r13,_PAGE_ACCESSED@h
  510. #else
  511. li r13,_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_EXEC
  512. #endif
  513. 4:
  514. andc. r13,r13,r11 /* Check permission */
  515. #ifdef CONFIG_PTE_64BIT
  516. #ifdef CONFIG_SMP
  517. subf r13,r11,r12 /* create false data dep */
  518. lwzx r13,r11,r13 /* Get upper pte bits */
  519. #else
  520. lwz r13,0(r12) /* Get upper pte bits */
  521. #endif
  522. #endif
  523. bne 2f /* Bail if permission mismatch */
  524. /* Jump to common TLB load point */
  525. b finish_tlb_load
  526. 2:
  527. /* The bailout. Restore registers to pre-exception conditions
  528. * and call the heavyweights to help us out.
  529. */
  530. mfspr r10, SPRN_SPRG_THREAD
  531. lwz r11, THREAD_NORMSAVE(3)(r10)
  532. mtcr r11
  533. lwz r13, THREAD_NORMSAVE(2)(r10)
  534. lwz r12, THREAD_NORMSAVE(1)(r10)
  535. lwz r11, THREAD_NORMSAVE(0)(r10)
  536. mfspr r10, SPRN_SPRG_RSCRATCH0
  537. b InstructionStorage
  538. /* Define SPE handlers for e500v2 */
  539. #ifdef CONFIG_SPE
  540. /* SPE Unavailable */
  541. START_EXCEPTION(SPEUnavailable)
  542. NORMAL_EXCEPTION_PROLOG(0x2010, SPE_UNAVAIL)
  543. beq 1f
  544. bl load_up_spe
  545. b fast_exception_return
  546. 1: prepare_transfer_to_handler
  547. bl KernelSPE
  548. b interrupt_return
  549. #elif defined(CONFIG_SPE_POSSIBLE)
  550. EXCEPTION(0x2020, SPE_UNAVAIL, SPEUnavailable, unknown_exception)
  551. #endif /* CONFIG_SPE_POSSIBLE */
  552. /* SPE Floating Point Data */
  553. #ifdef CONFIG_SPE
  554. START_EXCEPTION(SPEFloatingPointData)
  555. NORMAL_EXCEPTION_PROLOG(0x2030, SPE_FP_DATA)
  556. prepare_transfer_to_handler
  557. bl SPEFloatingPointException
  558. REST_NVGPRS(r1)
  559. b interrupt_return
  560. /* SPE Floating Point Round */
  561. START_EXCEPTION(SPEFloatingPointRound)
  562. NORMAL_EXCEPTION_PROLOG(0x2050, SPE_FP_ROUND)
  563. prepare_transfer_to_handler
  564. bl SPEFloatingPointRoundException
  565. REST_NVGPRS(r1)
  566. b interrupt_return
  567. #elif defined(CONFIG_SPE_POSSIBLE)
  568. EXCEPTION(0x2040, SPE_FP_DATA, SPEFloatingPointData, unknown_exception)
  569. EXCEPTION(0x2050, SPE_FP_ROUND, SPEFloatingPointRound, unknown_exception)
  570. #endif /* CONFIG_SPE_POSSIBLE */
  571. /* Performance Monitor */
  572. EXCEPTION(0x2060, PERFORMANCE_MONITOR, PerformanceMonitor, \
  573. performance_monitor_exception)
  574. EXCEPTION(0x2070, DOORBELL, Doorbell, doorbell_exception)
  575. CRITICAL_EXCEPTION(0x2080, DOORBELL_CRITICAL, \
  576. CriticalDoorbell, unknown_exception)
  577. /* Debug Interrupt */
  578. DEBUG_DEBUG_EXCEPTION
  579. DEBUG_CRIT_EXCEPTION
  580. GUEST_DOORBELL_EXCEPTION
  581. CRITICAL_EXCEPTION(0, GUEST_DBELL_CRIT, CriticalGuestDoorbell, \
  582. unknown_exception)
  583. /* Hypercall */
  584. EXCEPTION(0, HV_SYSCALL, Hypercall, unknown_exception)
  585. /* Embedded Hypervisor Privilege */
  586. EXCEPTION(0, HV_PRIV, Ehvpriv, unknown_exception)
  587. interrupt_end:
  588. /*
  589. * Local functions
  590. */
  591. /*
  592. * Both the instruction and data TLB miss get to this
  593. * point to load the TLB.
  594. * r10 - tsize encoding (if HUGETLB_PAGE) or available to use
  595. * r11 - TLB (info from Linux PTE)
  596. * r12 - available to use
  597. * r13 - upper bits of PTE (if PTE_64BIT) or available to use
  598. * CR5 - results of addr >= PAGE_OFFSET
  599. * MAS0, MAS1 - loaded with proper value when we get here
  600. * MAS2, MAS3 - will need additional info from Linux PTE
  601. * Upon exit, we reload everything and RFI.
  602. */
  603. finish_tlb_load:
  604. #ifdef CONFIG_HUGETLB_PAGE
  605. cmpwi 6, r10, 0 /* check for huge page */
  606. beq 6, finish_tlb_load_cont /* !huge */
  607. /* Alas, we need more scratch registers for hugepages */
  608. mfspr r12, SPRN_SPRG_THREAD
  609. stw r14, THREAD_NORMSAVE(4)(r12)
  610. stw r15, THREAD_NORMSAVE(5)(r12)
  611. stw r16, THREAD_NORMSAVE(6)(r12)
  612. stw r17, THREAD_NORMSAVE(7)(r12)
  613. /* Get the next_tlbcam_idx percpu var */
  614. #ifdef CONFIG_SMP
  615. lwz r15, TASK_CPU-THREAD(r12)
  616. lis r14, __per_cpu_offset@h
  617. ori r14, r14, __per_cpu_offset@l
  618. rlwinm r15, r15, 2, 0, 29
  619. lwzx r16, r14, r15
  620. #else
  621. li r16, 0
  622. #endif
  623. lis r17, next_tlbcam_idx@h
  624. ori r17, r17, next_tlbcam_idx@l
  625. add r17, r17, r16 /* r17 = *next_tlbcam_idx */
  626. lwz r15, 0(r17) /* r15 = next_tlbcam_idx */
  627. lis r14, MAS0_TLBSEL(1)@h /* select TLB1 (TLBCAM) */
  628. rlwimi r14, r15, 16, 4, 15 /* next_tlbcam_idx entry */
  629. mtspr SPRN_MAS0, r14
  630. /* Extract TLB1CFG(NENTRY) */
  631. mfspr r16, SPRN_TLB1CFG
  632. andi. r16, r16, 0xfff
  633. /* Update next_tlbcam_idx, wrapping when necessary */
  634. addi r15, r15, 1
  635. cmpw r15, r16
  636. blt 100f
  637. lis r14, tlbcam_index@h
  638. ori r14, r14, tlbcam_index@l
  639. lwz r15, 0(r14)
  640. 100: stw r15, 0(r17)
  641. mfspr r16, SPRN_MAS1
  642. rlwimi r16, r10, MAS1_TSIZE_SHIFT, MAS1_TSIZE_MASK
  643. mtspr SPRN_MAS1, r16
  644. /* copy the pshift for use later */
  645. addi r14, r10, _PAGE_PSIZE_SHIFT_OFFSET
  646. /* fall through */
  647. #endif /* CONFIG_HUGETLB_PAGE */
  648. /*
  649. * We set execute, because we don't have the granularity to
  650. * properly set this at the page level (Linux problem).
  651. * Many of these bits are software only. Bits we don't set
  652. * here we (properly should) assume have the appropriate value.
  653. */
  654. finish_tlb_load_cont:
  655. #ifdef CONFIG_PTE_64BIT
  656. rlwinm r12, r11, 32-2, 26, 31 /* Move in perm bits */
  657. andi. r10, r11, _PAGE_DIRTY
  658. bne 1f
  659. li r10, MAS3_SW | MAS3_UW
  660. andc r12, r12, r10
  661. 1: rlwimi r12, r13, 20, 0, 11 /* grab RPN[32:43] */
  662. rlwimi r12, r11, 20, 12, 19 /* grab RPN[44:51] */
  663. 2: mtspr SPRN_MAS3, r12
  664. BEGIN_MMU_FTR_SECTION
  665. srwi r10, r13, 12 /* grab RPN[12:31] */
  666. mtspr SPRN_MAS7, r10
  667. END_MMU_FTR_SECTION_IFSET(MMU_FTR_BIG_PHYS)
  668. #else
  669. li r10, (_PAGE_EXEC | _PAGE_READ)
  670. mr r13, r11
  671. rlwimi r10, r11, 31, 29, 29 /* extract _PAGE_DIRTY into SW */
  672. and r12, r11, r10
  673. mcrf cr0, cr5 /* Test for user page */
  674. slwi r10, r12, 1
  675. or r10, r10, r12
  676. rlwinm r10, r10, 0, ~_PAGE_EXEC /* Clear SX on user pages */
  677. isellt r12, r10, r12
  678. rlwimi r13, r12, 0, 20, 31 /* Get RPN from PTE, merge w/ perms */
  679. mtspr SPRN_MAS3, r13
  680. #endif
  681. mfspr r12, SPRN_MAS2
  682. #ifdef CONFIG_PTE_64BIT
  683. rlwimi r12, r11, 32-19, 27, 31 /* extract WIMGE from pte */
  684. #else
  685. rlwimi r12, r11, 26, 27, 31 /* extract WIMGE from pte */
  686. #endif
  687. #ifdef CONFIG_HUGETLB_PAGE
  688. beq 6, 3f /* don't mask if page isn't huge */
  689. li r13, 1
  690. slw r13, r13, r14
  691. subi r13, r13, 1
  692. rlwinm r13, r13, 0, 0, 19 /* bottom bits used for WIMGE/etc */
  693. andc r12, r12, r13 /* mask off ea bits within the page */
  694. #endif
  695. 3: mtspr SPRN_MAS2, r12
  696. tlb_write_entry:
  697. tlbwe
  698. /* Done...restore registers and get out of here. */
  699. mfspr r10, SPRN_SPRG_THREAD
  700. #ifdef CONFIG_HUGETLB_PAGE
  701. beq 6, 8f /* skip restore for 4k page faults */
  702. lwz r14, THREAD_NORMSAVE(4)(r10)
  703. lwz r15, THREAD_NORMSAVE(5)(r10)
  704. lwz r16, THREAD_NORMSAVE(6)(r10)
  705. lwz r17, THREAD_NORMSAVE(7)(r10)
  706. #endif
  707. 8: lwz r11, THREAD_NORMSAVE(3)(r10)
  708. mtcr r11
  709. lwz r13, THREAD_NORMSAVE(2)(r10)
  710. lwz r12, THREAD_NORMSAVE(1)(r10)
  711. lwz r11, THREAD_NORMSAVE(0)(r10)
  712. mfspr r10, SPRN_SPRG_RSCRATCH0
  713. rfi /* Force context change */
  714. #ifdef CONFIG_SPE
  715. /* Note that the SPE support is closely modeled after the AltiVec
  716. * support. Changes to one are likely to be applicable to the
  717. * other! */
  718. _GLOBAL(load_up_spe)
  719. /*
  720. * Disable SPE for the task which had SPE previously,
  721. * and save its SPE registers in its thread_struct.
  722. * Enables SPE for use in the kernel on return.
  723. * On SMP we know the SPE units are free, since we give it up every
  724. * switch. -- Kumar
  725. */
  726. mfmsr r5
  727. oris r5,r5,MSR_SPE@h
  728. mtmsr r5 /* enable use of SPE now */
  729. isync
  730. /* enable use of SPE after return */
  731. oris r9,r9,MSR_SPE@h
  732. mfspr r5,SPRN_SPRG_THREAD /* current task's THREAD (phys) */
  733. li r4,1
  734. li r10,THREAD_ACC
  735. stw r4,THREAD_USED_SPE(r5)
  736. evlddx evr4,r10,r5
  737. evmra evr4,evr4
  738. REST_32EVRS(0,r10,r5,THREAD_EVR0)
  739. blr
  740. /*
  741. * SPE unavailable trap from kernel - print a message, but let
  742. * the task use SPE in the kernel until it returns to user mode.
  743. */
  744. SYM_FUNC_START_LOCAL(KernelSPE)
  745. lwz r3,_MSR(r1)
  746. oris r3,r3,MSR_SPE@h
  747. stw r3,_MSR(r1) /* enable use of SPE after return */
  748. #ifdef CONFIG_PRINTK
  749. lis r3,87f@h
  750. ori r3,r3,87f@l
  751. mr r4,r2 /* current */
  752. lwz r5,_NIP(r1)
  753. bl _printk
  754. #endif
  755. b interrupt_return
  756. #ifdef CONFIG_PRINTK
  757. 87: .string "SPE used in kernel (task=%p, pc=%x) \n"
  758. #endif
  759. .align 4,0
  760. SYM_FUNC_END(KernelSPE)
  761. #endif /* CONFIG_SPE */
  762. /*
  763. * Translate the effec addr in r3 to phys addr. The phys addr will be put
  764. * into r3(higher 32bit) and r4(lower 32bit)
  765. */
  766. SYM_FUNC_START_LOCAL(get_phys_addr)
  767. mfmsr r8
  768. mfspr r9,SPRN_PID
  769. rlwinm r9,r9,16,0x3fff0000 /* turn PID into MAS6[SPID] */
  770. rlwimi r9,r8,28,0x00000001 /* turn MSR[DS] into MAS6[SAS] */
  771. mtspr SPRN_MAS6,r9
  772. tlbsx 0,r3 /* must succeed */
  773. mfspr r8,SPRN_MAS1
  774. mfspr r12,SPRN_MAS3
  775. rlwinm r9,r8,25,0x1f /* r9 = log2(page size) */
  776. li r10,1024
  777. slw r10,r10,r9 /* r10 = page size */
  778. addi r10,r10,-1
  779. and r11,r3,r10 /* r11 = page offset */
  780. andc r4,r12,r10 /* r4 = page base */
  781. or r4,r4,r11 /* r4 = devtree phys addr */
  782. #ifdef CONFIG_PHYS_64BIT
  783. mfspr r3,SPRN_MAS7
  784. #endif
  785. blr
  786. SYM_FUNC_END(get_phys_addr)
  787. /*
  788. * Global functions
  789. */
  790. #ifdef CONFIG_PPC_E500
  791. #ifndef CONFIG_PPC_E500MC
  792. /* Adjust or setup IVORs for e500v1/v2 */
  793. _GLOBAL(__setup_e500_ivors)
  794. li r3,DebugCrit@l
  795. mtspr SPRN_IVOR15,r3
  796. li r3,SPEUnavailable@l
  797. mtspr SPRN_IVOR32,r3
  798. li r3,SPEFloatingPointData@l
  799. mtspr SPRN_IVOR33,r3
  800. li r3,SPEFloatingPointRound@l
  801. mtspr SPRN_IVOR34,r3
  802. li r3,PerformanceMonitor@l
  803. mtspr SPRN_IVOR35,r3
  804. sync
  805. blr
  806. #else
  807. /* Adjust or setup IVORs for e500mc */
  808. _GLOBAL(__setup_e500mc_ivors)
  809. li r3,DebugDebug@l
  810. mtspr SPRN_IVOR15,r3
  811. li r3,PerformanceMonitor@l
  812. mtspr SPRN_IVOR35,r3
  813. li r3,Doorbell@l
  814. mtspr SPRN_IVOR36,r3
  815. li r3,CriticalDoorbell@l
  816. mtspr SPRN_IVOR37,r3
  817. sync
  818. blr
  819. /* setup ehv ivors for */
  820. _GLOBAL(__setup_ehv_ivors)
  821. li r3,GuestDoorbell@l
  822. mtspr SPRN_IVOR38,r3
  823. li r3,CriticalGuestDoorbell@l
  824. mtspr SPRN_IVOR39,r3
  825. li r3,Hypercall@l
  826. mtspr SPRN_IVOR40,r3
  827. li r3,Ehvpriv@l
  828. mtspr SPRN_IVOR41,r3
  829. sync
  830. blr
  831. #endif /* CONFIG_PPC_E500MC */
  832. #endif /* CONFIG_PPC_E500 */
  833. #ifdef CONFIG_SPE
  834. /*
  835. * extern void __giveup_spe(struct task_struct *prev)
  836. *
  837. */
  838. _GLOBAL(__giveup_spe)
  839. addi r3,r3,THREAD /* want THREAD of task */
  840. lwz r5,PT_REGS(r3)
  841. cmpi 0,r5,0
  842. SAVE_32EVRS(0, r4, r3, THREAD_EVR0)
  843. evxor evr6, evr6, evr6 /* clear out evr6 */
  844. evmwumiaa evr6, evr6, evr6 /* evr6 <- ACC = 0 * 0 + ACC */
  845. li r4,THREAD_ACC
  846. evstddx evr6, r4, r3 /* save off accumulator */
  847. beq 1f
  848. lwz r4,_MSR-STACK_INT_FRAME_REGS(r5)
  849. lis r3,MSR_SPE@h
  850. andc r4,r4,r3 /* disable SPE for previous task */
  851. stw r4,_MSR-STACK_INT_FRAME_REGS(r5)
  852. 1:
  853. blr
  854. #endif /* CONFIG_SPE */
  855. /*
  856. * extern void abort(void)
  857. *
  858. * At present, this routine just applies a system reset.
  859. */
  860. _GLOBAL(abort)
  861. li r13,0
  862. mtspr SPRN_DBCR0,r13 /* disable all debug events */
  863. isync
  864. mfmsr r13
  865. ori r13,r13,MSR_DE@l /* Enable Debug Events */
  866. mtmsr r13
  867. isync
  868. mfspr r13,SPRN_DBCR0
  869. lis r13,(DBCR0_IDM|DBCR0_RST_CHIP)@h
  870. mtspr SPRN_DBCR0,r13
  871. isync
  872. #ifdef CONFIG_SMP
  873. /* When we get here, r24 needs to hold the CPU # */
  874. .globl __secondary_start
  875. __secondary_start:
  876. LOAD_REG_ADDR_PIC(r3, tlbcam_index)
  877. lwz r3,0(r3)
  878. mtctr r3
  879. li r26,0 /* r26 safe? */
  880. bl switch_to_as1
  881. mr r27,r3 /* tlb entry */
  882. /* Load each CAM entry */
  883. 1: mr r3,r26
  884. bl loadcam_entry
  885. addi r26,r26,1
  886. bdnz 1b
  887. mr r3,r27 /* tlb entry */
  888. LOAD_REG_ADDR_PIC(r4, memstart_addr)
  889. lwz r4,0(r4)
  890. mr r5,r25 /* phys kernel start */
  891. rlwinm r5,r5,0,~0x3ffffff /* aligned 64M */
  892. subf r4,r5,r4 /* memstart_addr - phys kernel start */
  893. lis r7,KERNELBASE@h
  894. ori r7,r7,KERNELBASE@l
  895. cmpw r20,r7 /* if kernstart_virt_addr != KERNELBASE, randomized */
  896. beq 2f
  897. li r4,0
  898. 2: li r5,0 /* no device tree */
  899. li r6,0 /* not boot cpu */
  900. bl restore_to_as0
  901. lis r3,__secondary_hold_acknowledge@h
  902. ori r3,r3,__secondary_hold_acknowledge@l
  903. stw r24,0(r3)
  904. li r3,0
  905. mr r4,r24 /* Why? */
  906. bl call_setup_cpu
  907. /* get current's stack and current */
  908. lis r2,secondary_current@ha
  909. lwz r2,secondary_current@l(r2)
  910. lwz r1,TASK_STACK(r2)
  911. /* stack */
  912. addi r1,r1,THREAD_SIZE-STACK_FRAME_MIN_SIZE
  913. li r0,0
  914. stw r0,0(r1)
  915. /* ptr to current thread */
  916. addi r4,r2,THREAD /* address of our thread_struct */
  917. mtspr SPRN_SPRG_THREAD,r4
  918. /* Setup the defaults for TLB entries */
  919. li r4,(MAS4_TSIZED(BOOK3E_PAGESZ_4K))@l
  920. mtspr SPRN_MAS4,r4
  921. /* Jump to start_secondary */
  922. lis r4,MSR_KERNEL@h
  923. ori r4,r4,MSR_KERNEL@l
  924. lis r3,start_secondary@h
  925. ori r3,r3,start_secondary@l
  926. mtspr SPRN_SRR0,r3
  927. mtspr SPRN_SRR1,r4
  928. sync
  929. rfi
  930. sync
  931. .globl __secondary_hold_acknowledge
  932. __secondary_hold_acknowledge:
  933. .long -1
  934. #endif
  935. /*
  936. * Create a 64M tlb by address and entry
  937. * r3 - entry
  938. * r4 - virtual address
  939. * r5/r6 - physical address
  940. */
  941. _GLOBAL(create_kaslr_tlb_entry)
  942. lis r7,0x1000 /* Set MAS0(TLBSEL) = 1 */
  943. rlwimi r7,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r6) */
  944. mtspr SPRN_MAS0,r7 /* Write MAS0 */
  945. lis r3,(MAS1_VALID|MAS1_IPROT)@h
  946. ori r3,r3,(MAS1_TSIZE(BOOK3E_PAGESZ_64M))@l
  947. mtspr SPRN_MAS1,r3 /* Write MAS1 */
  948. lis r3,MAS2_EPN_MASK(BOOK3E_PAGESZ_64M)@h
  949. ori r3,r3,MAS2_EPN_MASK(BOOK3E_PAGESZ_64M)@l
  950. and r3,r3,r4
  951. ori r3,r3,MAS2_M_IF_NEEDED@l
  952. mtspr SPRN_MAS2,r3 /* Write MAS2(EPN) */
  953. #ifdef CONFIG_PHYS_64BIT
  954. ori r8,r6,(MAS3_SW|MAS3_SR|MAS3_SX)
  955. mtspr SPRN_MAS3,r8 /* Write MAS3(RPN) */
  956. mtspr SPRN_MAS7,r5
  957. #else
  958. ori r8,r5,(MAS3_SW|MAS3_SR|MAS3_SX)
  959. mtspr SPRN_MAS3,r8 /* Write MAS3(RPN) */
  960. #endif
  961. tlbwe /* Write TLB */
  962. isync
  963. sync
  964. blr
  965. /*
  966. * Return to the start of the relocated kernel and run again
  967. * r3 - virtual address of fdt
  968. * r4 - entry of the kernel
  969. */
  970. _GLOBAL(reloc_kernel_entry)
  971. mfmsr r7
  972. rlwinm r7, r7, 0, ~(MSR_IS | MSR_DS)
  973. mtspr SPRN_SRR0,r4
  974. mtspr SPRN_SRR1,r7
  975. rfi
  976. /*
  977. * Create a tlb entry with the same effective and physical address as
  978. * the tlb entry used by the current running code. But set the TS to 1.
  979. * Then switch to the address space 1. It will return with the r3 set to
  980. * the ESEL of the new created tlb.
  981. */
  982. _GLOBAL(switch_to_as1)
  983. mflr r5
  984. /* Find a entry not used */
  985. mfspr r3,SPRN_TLB1CFG
  986. andi. r3,r3,0xfff
  987. mfspr r4,SPRN_PID
  988. rlwinm r4,r4,16,0x3fff0000 /* turn PID into MAS6[SPID] */
  989. mtspr SPRN_MAS6,r4
  990. 1: lis r4,0x1000 /* Set MAS0(TLBSEL) = 1 */
  991. addi r3,r3,-1
  992. rlwimi r4,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r3) */
  993. mtspr SPRN_MAS0,r4
  994. tlbre
  995. mfspr r4,SPRN_MAS1
  996. andis. r4,r4,MAS1_VALID@h
  997. bne 1b
  998. /* Get the tlb entry used by the current running code */
  999. bcl 20,31,$+4
  1000. 0: mflr r4
  1001. tlbsx 0,r4
  1002. mfspr r4,SPRN_MAS1
  1003. ori r4,r4,MAS1_TS /* Set the TS = 1 */
  1004. mtspr SPRN_MAS1,r4
  1005. mfspr r4,SPRN_MAS0
  1006. rlwinm r4,r4,0,~MAS0_ESEL_MASK
  1007. rlwimi r4,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r3) */
  1008. mtspr SPRN_MAS0,r4
  1009. tlbwe
  1010. isync
  1011. sync
  1012. mfmsr r4
  1013. ori r4,r4,MSR_IS | MSR_DS
  1014. mtspr SPRN_SRR0,r5
  1015. mtspr SPRN_SRR1,r4
  1016. sync
  1017. rfi
  1018. /*
  1019. * Restore to the address space 0 and also invalidate the tlb entry created
  1020. * by switch_to_as1.
  1021. * r3 - the tlb entry which should be invalidated
  1022. * r4 - __pa(PAGE_OFFSET in AS1) - __pa(PAGE_OFFSET in AS0)
  1023. * r5 - device tree virtual address. If r4 is 0, r5 is ignored.
  1024. * r6 - boot cpu
  1025. */
  1026. _GLOBAL(restore_to_as0)
  1027. mflr r0
  1028. bcl 20,31,$+4
  1029. 0: mflr r9
  1030. addi r9,r9,1f - 0b
  1031. /*
  1032. * We may map the PAGE_OFFSET in AS0 to a different physical address,
  1033. * so we need calculate the right jump and device tree address based
  1034. * on the offset passed by r4.
  1035. */
  1036. add r9,r9,r4
  1037. add r5,r5,r4
  1038. add r0,r0,r4
  1039. 2: mfmsr r7
  1040. li r8,(MSR_IS | MSR_DS)
  1041. andc r7,r7,r8
  1042. mtspr SPRN_SRR0,r9
  1043. mtspr SPRN_SRR1,r7
  1044. sync
  1045. rfi
  1046. /* Invalidate the temporary tlb entry for AS1 */
  1047. 1: lis r9,0x1000 /* Set MAS0(TLBSEL) = 1 */
  1048. rlwimi r9,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r3) */
  1049. mtspr SPRN_MAS0,r9
  1050. tlbre
  1051. mfspr r9,SPRN_MAS1
  1052. rlwinm r9,r9,0,2,31 /* Clear MAS1 Valid and IPPROT */
  1053. mtspr SPRN_MAS1,r9
  1054. tlbwe
  1055. isync
  1056. cmpwi r4,0
  1057. cmpwi cr1,r6,0
  1058. cror eq,4*cr1+eq,eq
  1059. bne 3f /* offset != 0 && is_boot_cpu */
  1060. mtlr r0
  1061. blr
  1062. /*
  1063. * The PAGE_OFFSET will map to a different physical address,
  1064. * jump to _start to do another relocation again.
  1065. */
  1066. 3: mr r3,r5
  1067. bl _start