head_85xx.S 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Kernel execution entry point code.
  4. *
  5. * Copyright (c) 1995-1996 Gary Thomas <[email protected]>
  6. * Initial PowerPC version.
  7. * Copyright (c) 1996 Cort Dougan <[email protected]>
  8. * Rewritten for PReP
  9. * Copyright (c) 1996 Paul Mackerras <[email protected]>
  10. * Low-level exception handers, MMU support, and rewrite.
  11. * Copyright (c) 1997 Dan Malek <[email protected]>
  12. * PowerPC 8xx modifications.
  13. * Copyright (c) 1998-1999 TiVo, Inc.
  14. * PowerPC 403GCX modifications.
  15. * Copyright (c) 1999 Grant Erickson <[email protected]>
  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. * [email protected] or [email protected]
  22. * [email protected]
  23. * Copyright 2002-2004 MontaVista Software, Inc.
  24. * PowerPC 44x support, Matt Porter <[email protected]>
  25. * Copyright 2004 Freescale Semiconductor, Inc
  26. * PowerPC e500 modifications, Kumar Gala <[email protected]>
  27. */
  28. #include <linux/init.h>
  29. #include <linux/threads.h>
  30. #include <linux/pgtable.h>
  31. #include <asm/processor.h>
  32. #include <asm/page.h>
  33. #include <asm/mmu.h>
  34. #include <asm/cputable.h>
  35. #include <asm/thread_info.h>
  36. #include <asm/ppc_asm.h>
  37. #include <asm/asm-offsets.h>
  38. #include <asm/cache.h>
  39. #include <asm/ptrace.h>
  40. #include <asm/export.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_OVERHEAD(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 -- EA of fault
  262. * r11 -- PGDIR pointer
  263. * r12 -- free
  264. * label 2: is the bailout case
  265. *
  266. * if we find the pte (fall through):
  267. * r11 is low pte word
  268. * r12 is pointer to the pte
  269. * r10 is the pshift from the PGD, if we're a hugepage
  270. */
  271. #ifdef CONFIG_PTE_64BIT
  272. #ifdef CONFIG_HUGETLB_PAGE
  273. #define FIND_PTE \
  274. rlwinm r12, r10, 13, 19, 29; /* Compute pgdir/pmd offset */ \
  275. lwzx r11, r12, r11; /* Get pgd/pmd entry */ \
  276. rlwinm. r12, r11, 0, 0, 20; /* Extract pt base address */ \
  277. blt 1000f; /* Normal non-huge page */ \
  278. beq 2f; /* Bail if no table */ \
  279. oris r11, r11, PD_HUGE@h; /* Put back address bit */ \
  280. andi. r10, r11, HUGEPD_SHIFT_MASK@l; /* extract size field */ \
  281. xor r12, r10, r11; /* drop size bits from pointer */ \
  282. b 1001f; \
  283. 1000: rlwimi r12, r10, 23, 20, 28; /* Compute pte address */ \
  284. li r10, 0; /* clear r10 */ \
  285. 1001: lwz r11, 4(r12); /* Get pte entry */
  286. #else
  287. #define FIND_PTE \
  288. rlwinm r12, r10, 13, 19, 29; /* Compute pgdir/pmd offset */ \
  289. lwzx r11, r12, r11; /* Get pgd/pmd entry */ \
  290. rlwinm. r12, r11, 0, 0, 20; /* Extract pt base address */ \
  291. beq 2f; /* Bail if no table */ \
  292. rlwimi r12, r10, 23, 20, 28; /* Compute pte address */ \
  293. lwz r11, 4(r12); /* Get pte entry */
  294. #endif /* HUGEPAGE */
  295. #else /* !PTE_64BIT */
  296. #define FIND_PTE \
  297. rlwimi r11, r10, 12, 20, 29; /* Create L1 (pgdir/pmd) address */ \
  298. lwz r11, 0(r11); /* Get L1 entry */ \
  299. rlwinm. r12, r11, 0, 0, 19; /* Extract L2 (pte) base address */ \
  300. beq 2f; /* Bail if no table */ \
  301. rlwimi r12, r10, 22, 20, 29; /* Compute PTE address */ \
  302. lwz r11, 0(r12); /* Get Linux PTE */
  303. #endif
  304. /*
  305. * Interrupt vector entry code
  306. *
  307. * The Book E MMUs are always on so we don't need to handle
  308. * interrupts in real mode as with previous PPC processors. In
  309. * this case we handle interrupts in the kernel virtual address
  310. * space.
  311. *
  312. * Interrupt vectors are dynamically placed relative to the
  313. * interrupt prefix as determined by the address of interrupt_base.
  314. * The interrupt vectors offsets are programmed using the labels
  315. * for each interrupt vector entry.
  316. *
  317. * Interrupt vectors must be aligned on a 16 byte boundary.
  318. * We align on a 32 byte cache line boundary for good measure.
  319. */
  320. interrupt_base:
  321. /* Critical Input Interrupt */
  322. CRITICAL_EXCEPTION(0x0100, CRITICAL, CriticalInput, unknown_exception)
  323. /* Machine Check Interrupt */
  324. MCHECK_EXCEPTION(0x0200, MachineCheck, machine_check_exception)
  325. /* Data Storage Interrupt */
  326. START_EXCEPTION(DataStorage)
  327. NORMAL_EXCEPTION_PROLOG(0x300, DATA_STORAGE)
  328. mfspr r5,SPRN_ESR /* Grab the ESR, save it */
  329. stw r5,_ESR(r11)
  330. mfspr r4,SPRN_DEAR /* Grab the DEAR, save it */
  331. stw r4, _DEAR(r11)
  332. andis. r10,r5,(ESR_ILK|ESR_DLK)@h
  333. bne 1f
  334. prepare_transfer_to_handler
  335. bl do_page_fault
  336. b interrupt_return
  337. 1:
  338. prepare_transfer_to_handler
  339. bl CacheLockingException
  340. b interrupt_return
  341. /* Instruction Storage Interrupt */
  342. INSTRUCTION_STORAGE_EXCEPTION
  343. /* External Input Interrupt */
  344. EXCEPTION(0x0500, EXTERNAL, ExternalInput, do_IRQ)
  345. /* Alignment Interrupt */
  346. ALIGNMENT_EXCEPTION
  347. /* Program Interrupt */
  348. PROGRAM_EXCEPTION
  349. /* Floating Point Unavailable Interrupt */
  350. #ifdef CONFIG_PPC_FPU
  351. FP_UNAVAILABLE_EXCEPTION
  352. #else
  353. EXCEPTION(0x0800, FP_UNAVAIL, FloatingPointUnavailable, emulation_assist_interrupt)
  354. #endif
  355. /* System Call Interrupt */
  356. START_EXCEPTION(SystemCall)
  357. SYSCALL_ENTRY 0xc00 BOOKE_INTERRUPT_SYSCALL SPRN_SRR1
  358. /* Auxiliary Processor Unavailable Interrupt */
  359. EXCEPTION(0x2900, AP_UNAVAIL, AuxillaryProcessorUnavailable, unknown_exception)
  360. /* Decrementer Interrupt */
  361. DECREMENTER_EXCEPTION
  362. /* Fixed Internal Timer Interrupt */
  363. /* TODO: Add FIT support */
  364. EXCEPTION(0x3100, FIT, FixedIntervalTimer, unknown_exception)
  365. /* Watchdog Timer Interrupt */
  366. #ifdef CONFIG_BOOKE_WDT
  367. CRITICAL_EXCEPTION(0x3200, WATCHDOG, WatchdogTimer, WatchdogException)
  368. #else
  369. CRITICAL_EXCEPTION(0x3200, WATCHDOG, WatchdogTimer, unknown_exception)
  370. #endif
  371. /* Data TLB Error Interrupt */
  372. START_EXCEPTION(DataTLBError)
  373. mtspr SPRN_SPRG_WSCRATCH0, r10 /* Save some working registers */
  374. mfspr r10, SPRN_SPRG_THREAD
  375. stw r11, THREAD_NORMSAVE(0)(r10)
  376. #ifdef CONFIG_KVM_BOOKE_HV
  377. BEGIN_FTR_SECTION
  378. mfspr r11, SPRN_SRR1
  379. END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
  380. #endif
  381. stw r12, THREAD_NORMSAVE(1)(r10)
  382. stw r13, THREAD_NORMSAVE(2)(r10)
  383. mfcr r13
  384. stw r13, THREAD_NORMSAVE(3)(r10)
  385. DO_KVM BOOKE_INTERRUPT_DTLB_MISS SPRN_SRR1
  386. START_BTB_FLUSH_SECTION
  387. mfspr r11, SPRN_SRR1
  388. andi. r10,r11,MSR_PR
  389. beq 1f
  390. BTB_FLUSH(r10)
  391. 1:
  392. END_BTB_FLUSH_SECTION
  393. mfspr r10, SPRN_DEAR /* Get faulting address */
  394. /* If we are faulting a kernel address, we have to use the
  395. * kernel page tables.
  396. */
  397. lis r11, PAGE_OFFSET@h
  398. cmplw 5, r10, r11
  399. blt 5, 3f
  400. lis r11, swapper_pg_dir@h
  401. ori r11, r11, swapper_pg_dir@l
  402. mfspr r12,SPRN_MAS1 /* Set TID to 0 */
  403. rlwinm r12,r12,0,16,1
  404. mtspr SPRN_MAS1,r12
  405. b 4f
  406. /* Get the PGD for the current thread */
  407. 3:
  408. mfspr r11,SPRN_SPRG_THREAD
  409. lwz r11,PGDIR(r11)
  410. #ifdef CONFIG_PPC_KUAP
  411. mfspr r12, SPRN_MAS1
  412. rlwinm. r12,r12,0,0x3fff0000
  413. beq 2f /* KUAP fault */
  414. #endif
  415. 4:
  416. /* Mask of required permission bits. Note that while we
  417. * do copy ESR:ST to _PAGE_RW position as trying to write
  418. * to an RO page is pretty common, we don't do it with
  419. * _PAGE_DIRTY. We could do it, but it's a fairly rare
  420. * event so I'd rather take the overhead when it happens
  421. * rather than adding an instruction here. We should measure
  422. * whether the whole thing is worth it in the first place
  423. * as we could avoid loading SPRN_ESR completely in the first
  424. * place...
  425. *
  426. * TODO: Is it worth doing that mfspr & rlwimi in the first
  427. * place or can we save a couple of instructions here ?
  428. */
  429. mfspr r12,SPRN_ESR
  430. #ifdef CONFIG_PTE_64BIT
  431. li r13,_PAGE_PRESENT
  432. oris r13,r13,_PAGE_ACCESSED@h
  433. #else
  434. li r13,_PAGE_PRESENT|_PAGE_ACCESSED
  435. #endif
  436. rlwimi r13,r12,11,29,29
  437. FIND_PTE
  438. andc. r13,r13,r11 /* Check permission */
  439. #ifdef CONFIG_PTE_64BIT
  440. #ifdef CONFIG_SMP
  441. subf r13,r11,r12 /* create false data dep */
  442. lwzx r13,r11,r13 /* Get upper pte bits */
  443. #else
  444. lwz r13,0(r12) /* Get upper pte bits */
  445. #endif
  446. #endif
  447. bne 2f /* Bail if permission/valid mismatch */
  448. /* Jump to common tlb load */
  449. b finish_tlb_load
  450. 2:
  451. /* The bailout. Restore registers to pre-exception conditions
  452. * and call the heavyweights to help us out.
  453. */
  454. mfspr r10, SPRN_SPRG_THREAD
  455. lwz r11, THREAD_NORMSAVE(3)(r10)
  456. mtcr r11
  457. lwz r13, THREAD_NORMSAVE(2)(r10)
  458. lwz r12, THREAD_NORMSAVE(1)(r10)
  459. lwz r11, THREAD_NORMSAVE(0)(r10)
  460. mfspr r10, SPRN_SPRG_RSCRATCH0
  461. b DataStorage
  462. /* Instruction TLB Error Interrupt */
  463. /*
  464. * Nearly the same as above, except we get our
  465. * information from different registers and bailout
  466. * to a different point.
  467. */
  468. START_EXCEPTION(InstructionTLBError)
  469. mtspr SPRN_SPRG_WSCRATCH0, r10 /* Save some working registers */
  470. mfspr r10, SPRN_SPRG_THREAD
  471. stw r11, THREAD_NORMSAVE(0)(r10)
  472. #ifdef CONFIG_KVM_BOOKE_HV
  473. BEGIN_FTR_SECTION
  474. mfspr r11, SPRN_SRR1
  475. END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)
  476. #endif
  477. stw r12, THREAD_NORMSAVE(1)(r10)
  478. stw r13, THREAD_NORMSAVE(2)(r10)
  479. mfcr r13
  480. stw r13, THREAD_NORMSAVE(3)(r10)
  481. DO_KVM BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR1
  482. START_BTB_FLUSH_SECTION
  483. mfspr r11, SPRN_SRR1
  484. andi. r10,r11,MSR_PR
  485. beq 1f
  486. BTB_FLUSH(r10)
  487. 1:
  488. END_BTB_FLUSH_SECTION
  489. mfspr r10, SPRN_SRR0 /* Get faulting address */
  490. /* If we are faulting a kernel address, we have to use the
  491. * kernel page tables.
  492. */
  493. lis r11, PAGE_OFFSET@h
  494. cmplw 5, r10, r11
  495. blt 5, 3f
  496. lis r11, swapper_pg_dir@h
  497. ori r11, r11, swapper_pg_dir@l
  498. mfspr r12,SPRN_MAS1 /* Set TID to 0 */
  499. rlwinm r12,r12,0,16,1
  500. mtspr SPRN_MAS1,r12
  501. /* Make up the required permissions for kernel code */
  502. #ifdef CONFIG_PTE_64BIT
  503. li r13,_PAGE_PRESENT | _PAGE_BAP_SX
  504. oris r13,r13,_PAGE_ACCESSED@h
  505. #else
  506. li r13,_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_EXEC
  507. #endif
  508. b 4f
  509. /* Get the PGD for the current thread */
  510. 3:
  511. mfspr r11,SPRN_SPRG_THREAD
  512. lwz r11,PGDIR(r11)
  513. #ifdef CONFIG_PPC_KUAP
  514. mfspr r12, SPRN_MAS1
  515. rlwinm. r12,r12,0,0x3fff0000
  516. beq 2f /* KUAP fault */
  517. #endif
  518. /* Make up the required permissions for user code */
  519. #ifdef CONFIG_PTE_64BIT
  520. li r13,_PAGE_PRESENT | _PAGE_BAP_UX
  521. oris r13,r13,_PAGE_ACCESSED@h
  522. #else
  523. li r13,_PAGE_PRESENT | _PAGE_ACCESSED | _PAGE_EXEC
  524. #endif
  525. 4:
  526. FIND_PTE
  527. andc. r13,r13,r11 /* Check permission */
  528. #ifdef CONFIG_PTE_64BIT
  529. #ifdef CONFIG_SMP
  530. subf r13,r11,r12 /* create false data dep */
  531. lwzx r13,r11,r13 /* Get upper pte bits */
  532. #else
  533. lwz r13,0(r12) /* Get upper pte bits */
  534. #endif
  535. #endif
  536. bne 2f /* Bail if permission mismatch */
  537. /* Jump to common TLB load point */
  538. b finish_tlb_load
  539. 2:
  540. /* The bailout. Restore registers to pre-exception conditions
  541. * and call the heavyweights to help us out.
  542. */
  543. mfspr r10, SPRN_SPRG_THREAD
  544. lwz r11, THREAD_NORMSAVE(3)(r10)
  545. mtcr r11
  546. lwz r13, THREAD_NORMSAVE(2)(r10)
  547. lwz r12, THREAD_NORMSAVE(1)(r10)
  548. lwz r11, THREAD_NORMSAVE(0)(r10)
  549. mfspr r10, SPRN_SPRG_RSCRATCH0
  550. b InstructionStorage
  551. /* Define SPE handlers for e500v2 */
  552. #ifdef CONFIG_SPE
  553. /* SPE Unavailable */
  554. START_EXCEPTION(SPEUnavailable)
  555. NORMAL_EXCEPTION_PROLOG(0x2010, SPE_UNAVAIL)
  556. beq 1f
  557. bl load_up_spe
  558. b fast_exception_return
  559. 1: prepare_transfer_to_handler
  560. bl KernelSPE
  561. b interrupt_return
  562. #elif defined(CONFIG_SPE_POSSIBLE)
  563. EXCEPTION(0x2020, SPE_UNAVAIL, SPEUnavailable, unknown_exception)
  564. #endif /* CONFIG_SPE_POSSIBLE */
  565. /* SPE Floating Point Data */
  566. #ifdef CONFIG_SPE
  567. START_EXCEPTION(SPEFloatingPointData)
  568. NORMAL_EXCEPTION_PROLOG(0x2030, SPE_FP_DATA)
  569. prepare_transfer_to_handler
  570. bl SPEFloatingPointException
  571. REST_NVGPRS(r1)
  572. b interrupt_return
  573. /* SPE Floating Point Round */
  574. START_EXCEPTION(SPEFloatingPointRound)
  575. NORMAL_EXCEPTION_PROLOG(0x2050, SPE_FP_ROUND)
  576. prepare_transfer_to_handler
  577. bl SPEFloatingPointRoundException
  578. REST_NVGPRS(r1)
  579. b interrupt_return
  580. #elif defined(CONFIG_SPE_POSSIBLE)
  581. EXCEPTION(0x2040, SPE_FP_DATA, SPEFloatingPointData, unknown_exception)
  582. EXCEPTION(0x2050, SPE_FP_ROUND, SPEFloatingPointRound, unknown_exception)
  583. #endif /* CONFIG_SPE_POSSIBLE */
  584. /* Performance Monitor */
  585. EXCEPTION(0x2060, PERFORMANCE_MONITOR, PerformanceMonitor, \
  586. performance_monitor_exception)
  587. EXCEPTION(0x2070, DOORBELL, Doorbell, doorbell_exception)
  588. CRITICAL_EXCEPTION(0x2080, DOORBELL_CRITICAL, \
  589. CriticalDoorbell, unknown_exception)
  590. /* Debug Interrupt */
  591. DEBUG_DEBUG_EXCEPTION
  592. DEBUG_CRIT_EXCEPTION
  593. GUEST_DOORBELL_EXCEPTION
  594. CRITICAL_EXCEPTION(0, GUEST_DBELL_CRIT, CriticalGuestDoorbell, \
  595. unknown_exception)
  596. /* Hypercall */
  597. EXCEPTION(0, HV_SYSCALL, Hypercall, unknown_exception)
  598. /* Embedded Hypervisor Privilege */
  599. EXCEPTION(0, HV_PRIV, Ehvpriv, unknown_exception)
  600. interrupt_end:
  601. /*
  602. * Local functions
  603. */
  604. /*
  605. * Both the instruction and data TLB miss get to this
  606. * point to load the TLB.
  607. * r10 - tsize encoding (if HUGETLB_PAGE) or available to use
  608. * r11 - TLB (info from Linux PTE)
  609. * r12 - available to use
  610. * r13 - upper bits of PTE (if PTE_64BIT) or available to use
  611. * CR5 - results of addr >= PAGE_OFFSET
  612. * MAS0, MAS1 - loaded with proper value when we get here
  613. * MAS2, MAS3 - will need additional info from Linux PTE
  614. * Upon exit, we reload everything and RFI.
  615. */
  616. finish_tlb_load:
  617. #ifdef CONFIG_HUGETLB_PAGE
  618. cmpwi 6, r10, 0 /* check for huge page */
  619. beq 6, finish_tlb_load_cont /* !huge */
  620. /* Alas, we need more scratch registers for hugepages */
  621. mfspr r12, SPRN_SPRG_THREAD
  622. stw r14, THREAD_NORMSAVE(4)(r12)
  623. stw r15, THREAD_NORMSAVE(5)(r12)
  624. stw r16, THREAD_NORMSAVE(6)(r12)
  625. stw r17, THREAD_NORMSAVE(7)(r12)
  626. /* Get the next_tlbcam_idx percpu var */
  627. #ifdef CONFIG_SMP
  628. lwz r15, TASK_CPU-THREAD(r12)
  629. lis r14, __per_cpu_offset@h
  630. ori r14, r14, __per_cpu_offset@l
  631. rlwinm r15, r15, 2, 0, 29
  632. lwzx r16, r14, r15
  633. #else
  634. li r16, 0
  635. #endif
  636. lis r17, next_tlbcam_idx@h
  637. ori r17, r17, next_tlbcam_idx@l
  638. add r17, r17, r16 /* r17 = *next_tlbcam_idx */
  639. lwz r15, 0(r17) /* r15 = next_tlbcam_idx */
  640. lis r14, MAS0_TLBSEL(1)@h /* select TLB1 (TLBCAM) */
  641. rlwimi r14, r15, 16, 4, 15 /* next_tlbcam_idx entry */
  642. mtspr SPRN_MAS0, r14
  643. /* Extract TLB1CFG(NENTRY) */
  644. mfspr r16, SPRN_TLB1CFG
  645. andi. r16, r16, 0xfff
  646. /* Update next_tlbcam_idx, wrapping when necessary */
  647. addi r15, r15, 1
  648. cmpw r15, r16
  649. blt 100f
  650. lis r14, tlbcam_index@h
  651. ori r14, r14, tlbcam_index@l
  652. lwz r15, 0(r14)
  653. 100: stw r15, 0(r17)
  654. /*
  655. * Calc MAS1_TSIZE from r10 (which has pshift encoded)
  656. * tlb_enc = (pshift - 10).
  657. */
  658. subi r15, r10, 10
  659. mfspr r16, SPRN_MAS1
  660. rlwimi r16, r15, 7, 20, 24
  661. mtspr SPRN_MAS1, r16
  662. /* copy the pshift for use later */
  663. mr r14, r10
  664. /* fall through */
  665. #endif /* CONFIG_HUGETLB_PAGE */
  666. /*
  667. * We set execute, because we don't have the granularity to
  668. * properly set this at the page level (Linux problem).
  669. * Many of these bits are software only. Bits we don't set
  670. * here we (properly should) assume have the appropriate value.
  671. */
  672. finish_tlb_load_cont:
  673. #ifdef CONFIG_PTE_64BIT
  674. rlwinm r12, r11, 32-2, 26, 31 /* Move in perm bits */
  675. andi. r10, r11, _PAGE_DIRTY
  676. bne 1f
  677. li r10, MAS3_SW | MAS3_UW
  678. andc r12, r12, r10
  679. 1: rlwimi r12, r13, 20, 0, 11 /* grab RPN[32:43] */
  680. rlwimi r12, r11, 20, 12, 19 /* grab RPN[44:51] */
  681. 2: mtspr SPRN_MAS3, r12
  682. BEGIN_MMU_FTR_SECTION
  683. srwi r10, r13, 12 /* grab RPN[12:31] */
  684. mtspr SPRN_MAS7, r10
  685. END_MMU_FTR_SECTION_IFSET(MMU_FTR_BIG_PHYS)
  686. #else
  687. li r10, (_PAGE_EXEC | _PAGE_PRESENT)
  688. mr r13, r11
  689. rlwimi r10, r11, 31, 29, 29 /* extract _PAGE_DIRTY into SW */
  690. and r12, r11, r10
  691. andi. r10, r11, _PAGE_USER /* Test for _PAGE_USER */
  692. slwi r10, r12, 1
  693. or r10, r10, r12
  694. rlwinm r10, r10, 0, ~_PAGE_EXEC /* Clear SX on user pages */
  695. iseleq r12, r12, r10
  696. rlwimi r13, r12, 0, 20, 31 /* Get RPN from PTE, merge w/ perms */
  697. mtspr SPRN_MAS3, r13
  698. #endif
  699. mfspr r12, SPRN_MAS2
  700. #ifdef CONFIG_PTE_64BIT
  701. rlwimi r12, r11, 32-19, 27, 31 /* extract WIMGE from pte */
  702. #else
  703. rlwimi r12, r11, 26, 27, 31 /* extract WIMGE from pte */
  704. #endif
  705. #ifdef CONFIG_HUGETLB_PAGE
  706. beq 6, 3f /* don't mask if page isn't huge */
  707. li r13, 1
  708. slw r13, r13, r14
  709. subi r13, r13, 1
  710. rlwinm r13, r13, 0, 0, 19 /* bottom bits used for WIMGE/etc */
  711. andc r12, r12, r13 /* mask off ea bits within the page */
  712. #endif
  713. 3: mtspr SPRN_MAS2, r12
  714. tlb_write_entry:
  715. tlbwe
  716. /* Done...restore registers and get out of here. */
  717. mfspr r10, SPRN_SPRG_THREAD
  718. #ifdef CONFIG_HUGETLB_PAGE
  719. beq 6, 8f /* skip restore for 4k page faults */
  720. lwz r14, THREAD_NORMSAVE(4)(r10)
  721. lwz r15, THREAD_NORMSAVE(5)(r10)
  722. lwz r16, THREAD_NORMSAVE(6)(r10)
  723. lwz r17, THREAD_NORMSAVE(7)(r10)
  724. #endif
  725. 8: lwz r11, THREAD_NORMSAVE(3)(r10)
  726. mtcr r11
  727. lwz r13, THREAD_NORMSAVE(2)(r10)
  728. lwz r12, THREAD_NORMSAVE(1)(r10)
  729. lwz r11, THREAD_NORMSAVE(0)(r10)
  730. mfspr r10, SPRN_SPRG_RSCRATCH0
  731. rfi /* Force context change */
  732. #ifdef CONFIG_SPE
  733. /* Note that the SPE support is closely modeled after the AltiVec
  734. * support. Changes to one are likely to be applicable to the
  735. * other! */
  736. _GLOBAL(load_up_spe)
  737. /*
  738. * Disable SPE for the task which had SPE previously,
  739. * and save its SPE registers in its thread_struct.
  740. * Enables SPE for use in the kernel on return.
  741. * On SMP we know the SPE units are free, since we give it up every
  742. * switch. -- Kumar
  743. */
  744. mfmsr r5
  745. oris r5,r5,MSR_SPE@h
  746. mtmsr r5 /* enable use of SPE now */
  747. isync
  748. /* enable use of SPE after return */
  749. oris r9,r9,MSR_SPE@h
  750. mfspr r5,SPRN_SPRG_THREAD /* current task's THREAD (phys) */
  751. li r4,1
  752. li r10,THREAD_ACC
  753. stw r4,THREAD_USED_SPE(r5)
  754. evlddx evr4,r10,r5
  755. evmra evr4,evr4
  756. REST_32EVRS(0,r10,r5,THREAD_EVR0)
  757. blr
  758. /*
  759. * SPE unavailable trap from kernel - print a message, but let
  760. * the task use SPE in the kernel until it returns to user mode.
  761. */
  762. KernelSPE:
  763. lwz r3,_MSR(r1)
  764. oris r3,r3,MSR_SPE@h
  765. stw r3,_MSR(r1) /* enable use of SPE after return */
  766. #ifdef CONFIG_PRINTK
  767. lis r3,87f@h
  768. ori r3,r3,87f@l
  769. mr r4,r2 /* current */
  770. lwz r5,_NIP(r1)
  771. bl _printk
  772. #endif
  773. b interrupt_return
  774. #ifdef CONFIG_PRINTK
  775. 87: .string "SPE used in kernel (task=%p, pc=%x) \n"
  776. #endif
  777. .align 4,0
  778. #endif /* CONFIG_SPE */
  779. /*
  780. * Translate the effec addr in r3 to phys addr. The phys addr will be put
  781. * into r3(higher 32bit) and r4(lower 32bit)
  782. */
  783. get_phys_addr:
  784. mfmsr r8
  785. mfspr r9,SPRN_PID
  786. rlwinm r9,r9,16,0x3fff0000 /* turn PID into MAS6[SPID] */
  787. rlwimi r9,r8,28,0x00000001 /* turn MSR[DS] into MAS6[SAS] */
  788. mtspr SPRN_MAS6,r9
  789. tlbsx 0,r3 /* must succeed */
  790. mfspr r8,SPRN_MAS1
  791. mfspr r12,SPRN_MAS3
  792. rlwinm r9,r8,25,0x1f /* r9 = log2(page size) */
  793. li r10,1024
  794. slw r10,r10,r9 /* r10 = page size */
  795. addi r10,r10,-1
  796. and r11,r3,r10 /* r11 = page offset */
  797. andc r4,r12,r10 /* r4 = page base */
  798. or r4,r4,r11 /* r4 = devtree phys addr */
  799. #ifdef CONFIG_PHYS_64BIT
  800. mfspr r3,SPRN_MAS7
  801. #endif
  802. blr
  803. /*
  804. * Global functions
  805. */
  806. #ifdef CONFIG_PPC_E500
  807. #ifndef CONFIG_PPC_E500MC
  808. /* Adjust or setup IVORs for e500v1/v2 */
  809. _GLOBAL(__setup_e500_ivors)
  810. li r3,DebugCrit@l
  811. mtspr SPRN_IVOR15,r3
  812. li r3,SPEUnavailable@l
  813. mtspr SPRN_IVOR32,r3
  814. li r3,SPEFloatingPointData@l
  815. mtspr SPRN_IVOR33,r3
  816. li r3,SPEFloatingPointRound@l
  817. mtspr SPRN_IVOR34,r3
  818. li r3,PerformanceMonitor@l
  819. mtspr SPRN_IVOR35,r3
  820. sync
  821. blr
  822. #else
  823. /* Adjust or setup IVORs for e500mc */
  824. _GLOBAL(__setup_e500mc_ivors)
  825. li r3,DebugDebug@l
  826. mtspr SPRN_IVOR15,r3
  827. li r3,PerformanceMonitor@l
  828. mtspr SPRN_IVOR35,r3
  829. li r3,Doorbell@l
  830. mtspr SPRN_IVOR36,r3
  831. li r3,CriticalDoorbell@l
  832. mtspr SPRN_IVOR37,r3
  833. sync
  834. blr
  835. /* setup ehv ivors for */
  836. _GLOBAL(__setup_ehv_ivors)
  837. li r3,GuestDoorbell@l
  838. mtspr SPRN_IVOR38,r3
  839. li r3,CriticalGuestDoorbell@l
  840. mtspr SPRN_IVOR39,r3
  841. li r3,Hypercall@l
  842. mtspr SPRN_IVOR40,r3
  843. li r3,Ehvpriv@l
  844. mtspr SPRN_IVOR41,r3
  845. sync
  846. blr
  847. #endif /* CONFIG_PPC_E500MC */
  848. #endif /* CONFIG_PPC_E500 */
  849. #ifdef CONFIG_SPE
  850. /*
  851. * extern void __giveup_spe(struct task_struct *prev)
  852. *
  853. */
  854. _GLOBAL(__giveup_spe)
  855. addi r3,r3,THREAD /* want THREAD of task */
  856. lwz r5,PT_REGS(r3)
  857. cmpi 0,r5,0
  858. SAVE_32EVRS(0, r4, r3, THREAD_EVR0)
  859. evxor evr6, evr6, evr6 /* clear out evr6 */
  860. evmwumiaa evr6, evr6, evr6 /* evr6 <- ACC = 0 * 0 + ACC */
  861. li r4,THREAD_ACC
  862. evstddx evr6, r4, r3 /* save off accumulator */
  863. beq 1f
  864. lwz r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  865. lis r3,MSR_SPE@h
  866. andc r4,r4,r3 /* disable SPE for previous task */
  867. stw r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  868. 1:
  869. blr
  870. #endif /* CONFIG_SPE */
  871. /*
  872. * extern void abort(void)
  873. *
  874. * At present, this routine just applies a system reset.
  875. */
  876. _GLOBAL(abort)
  877. li r13,0
  878. mtspr SPRN_DBCR0,r13 /* disable all debug events */
  879. isync
  880. mfmsr r13
  881. ori r13,r13,MSR_DE@l /* Enable Debug Events */
  882. mtmsr r13
  883. isync
  884. mfspr r13,SPRN_DBCR0
  885. lis r13,(DBCR0_IDM|DBCR0_RST_CHIP)@h
  886. mtspr SPRN_DBCR0,r13
  887. isync
  888. #ifdef CONFIG_SMP
  889. /* When we get here, r24 needs to hold the CPU # */
  890. .globl __secondary_start
  891. __secondary_start:
  892. LOAD_REG_ADDR_PIC(r3, tlbcam_index)
  893. lwz r3,0(r3)
  894. mtctr r3
  895. li r26,0 /* r26 safe? */
  896. bl switch_to_as1
  897. mr r27,r3 /* tlb entry */
  898. /* Load each CAM entry */
  899. 1: mr r3,r26
  900. bl loadcam_entry
  901. addi r26,r26,1
  902. bdnz 1b
  903. mr r3,r27 /* tlb entry */
  904. LOAD_REG_ADDR_PIC(r4, memstart_addr)
  905. lwz r4,0(r4)
  906. mr r5,r25 /* phys kernel start */
  907. rlwinm r5,r5,0,~0x3ffffff /* aligned 64M */
  908. subf r4,r5,r4 /* memstart_addr - phys kernel start */
  909. lis r7,KERNELBASE@h
  910. ori r7,r7,KERNELBASE@l
  911. cmpw r20,r7 /* if kernstart_virt_addr != KERNELBASE, randomized */
  912. beq 2f
  913. li r4,0
  914. 2: li r5,0 /* no device tree */
  915. li r6,0 /* not boot cpu */
  916. bl restore_to_as0
  917. lis r3,__secondary_hold_acknowledge@h
  918. ori r3,r3,__secondary_hold_acknowledge@l
  919. stw r24,0(r3)
  920. li r3,0
  921. mr r4,r24 /* Why? */
  922. bl call_setup_cpu
  923. /* get current's stack and current */
  924. lis r2,secondary_current@ha
  925. lwz r2,secondary_current@l(r2)
  926. lwz r1,TASK_STACK(r2)
  927. /* stack */
  928. addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
  929. li r0,0
  930. stw r0,0(r1)
  931. /* ptr to current thread */
  932. addi r4,r2,THREAD /* address of our thread_struct */
  933. mtspr SPRN_SPRG_THREAD,r4
  934. /* Setup the defaults for TLB entries */
  935. li r4,(MAS4_TSIZED(BOOK3E_PAGESZ_4K))@l
  936. mtspr SPRN_MAS4,r4
  937. /* Jump to start_secondary */
  938. lis r4,MSR_KERNEL@h
  939. ori r4,r4,MSR_KERNEL@l
  940. lis r3,start_secondary@h
  941. ori r3,r3,start_secondary@l
  942. mtspr SPRN_SRR0,r3
  943. mtspr SPRN_SRR1,r4
  944. sync
  945. rfi
  946. sync
  947. .globl __secondary_hold_acknowledge
  948. __secondary_hold_acknowledge:
  949. .long -1
  950. #endif
  951. /*
  952. * Create a 64M tlb by address and entry
  953. * r3 - entry
  954. * r4 - virtual address
  955. * r5/r6 - physical address
  956. */
  957. _GLOBAL(create_kaslr_tlb_entry)
  958. lis r7,0x1000 /* Set MAS0(TLBSEL) = 1 */
  959. rlwimi r7,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r6) */
  960. mtspr SPRN_MAS0,r7 /* Write MAS0 */
  961. lis r3,(MAS1_VALID|MAS1_IPROT)@h
  962. ori r3,r3,(MAS1_TSIZE(BOOK3E_PAGESZ_64M))@l
  963. mtspr SPRN_MAS1,r3 /* Write MAS1 */
  964. lis r3,MAS2_EPN_MASK(BOOK3E_PAGESZ_64M)@h
  965. ori r3,r3,MAS2_EPN_MASK(BOOK3E_PAGESZ_64M)@l
  966. and r3,r3,r4
  967. ori r3,r3,MAS2_M_IF_NEEDED@l
  968. mtspr SPRN_MAS2,r3 /* Write MAS2(EPN) */
  969. #ifdef CONFIG_PHYS_64BIT
  970. ori r8,r6,(MAS3_SW|MAS3_SR|MAS3_SX)
  971. mtspr SPRN_MAS3,r8 /* Write MAS3(RPN) */
  972. mtspr SPRN_MAS7,r5
  973. #else
  974. ori r8,r5,(MAS3_SW|MAS3_SR|MAS3_SX)
  975. mtspr SPRN_MAS3,r8 /* Write MAS3(RPN) */
  976. #endif
  977. tlbwe /* Write TLB */
  978. isync
  979. sync
  980. blr
  981. /*
  982. * Return to the start of the relocated kernel and run again
  983. * r3 - virtual address of fdt
  984. * r4 - entry of the kernel
  985. */
  986. _GLOBAL(reloc_kernel_entry)
  987. mfmsr r7
  988. rlwinm r7, r7, 0, ~(MSR_IS | MSR_DS)
  989. mtspr SPRN_SRR0,r4
  990. mtspr SPRN_SRR1,r7
  991. rfi
  992. /*
  993. * Create a tlb entry with the same effective and physical address as
  994. * the tlb entry used by the current running code. But set the TS to 1.
  995. * Then switch to the address space 1. It will return with the r3 set to
  996. * the ESEL of the new created tlb.
  997. */
  998. _GLOBAL(switch_to_as1)
  999. mflr r5
  1000. /* Find a entry not used */
  1001. mfspr r3,SPRN_TLB1CFG
  1002. andi. r3,r3,0xfff
  1003. mfspr r4,SPRN_PID
  1004. rlwinm r4,r4,16,0x3fff0000 /* turn PID into MAS6[SPID] */
  1005. mtspr SPRN_MAS6,r4
  1006. 1: lis r4,0x1000 /* Set MAS0(TLBSEL) = 1 */
  1007. addi r3,r3,-1
  1008. rlwimi r4,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r3) */
  1009. mtspr SPRN_MAS0,r4
  1010. tlbre
  1011. mfspr r4,SPRN_MAS1
  1012. andis. r4,r4,MAS1_VALID@h
  1013. bne 1b
  1014. /* Get the tlb entry used by the current running code */
  1015. bcl 20,31,$+4
  1016. 0: mflr r4
  1017. tlbsx 0,r4
  1018. mfspr r4,SPRN_MAS1
  1019. ori r4,r4,MAS1_TS /* Set the TS = 1 */
  1020. mtspr SPRN_MAS1,r4
  1021. mfspr r4,SPRN_MAS0
  1022. rlwinm r4,r4,0,~MAS0_ESEL_MASK
  1023. rlwimi r4,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r3) */
  1024. mtspr SPRN_MAS0,r4
  1025. tlbwe
  1026. isync
  1027. sync
  1028. mfmsr r4
  1029. ori r4,r4,MSR_IS | MSR_DS
  1030. mtspr SPRN_SRR0,r5
  1031. mtspr SPRN_SRR1,r4
  1032. sync
  1033. rfi
  1034. /*
  1035. * Restore to the address space 0 and also invalidate the tlb entry created
  1036. * by switch_to_as1.
  1037. * r3 - the tlb entry which should be invalidated
  1038. * r4 - __pa(PAGE_OFFSET in AS1) - __pa(PAGE_OFFSET in AS0)
  1039. * r5 - device tree virtual address. If r4 is 0, r5 is ignored.
  1040. * r6 - boot cpu
  1041. */
  1042. _GLOBAL(restore_to_as0)
  1043. mflr r0
  1044. bcl 20,31,$+4
  1045. 0: mflr r9
  1046. addi r9,r9,1f - 0b
  1047. /*
  1048. * We may map the PAGE_OFFSET in AS0 to a different physical address,
  1049. * so we need calculate the right jump and device tree address based
  1050. * on the offset passed by r4.
  1051. */
  1052. add r9,r9,r4
  1053. add r5,r5,r4
  1054. add r0,r0,r4
  1055. 2: mfmsr r7
  1056. li r8,(MSR_IS | MSR_DS)
  1057. andc r7,r7,r8
  1058. mtspr SPRN_SRR0,r9
  1059. mtspr SPRN_SRR1,r7
  1060. sync
  1061. rfi
  1062. /* Invalidate the temporary tlb entry for AS1 */
  1063. 1: lis r9,0x1000 /* Set MAS0(TLBSEL) = 1 */
  1064. rlwimi r9,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r3) */
  1065. mtspr SPRN_MAS0,r9
  1066. tlbre
  1067. mfspr r9,SPRN_MAS1
  1068. rlwinm r9,r9,0,2,31 /* Clear MAS1 Valid and IPPROT */
  1069. mtspr SPRN_MAS1,r9
  1070. tlbwe
  1071. isync
  1072. cmpwi r4,0
  1073. cmpwi cr1,r6,0
  1074. cror eq,4*cr1+eq,eq
  1075. bne 3f /* offset != 0 && is_boot_cpu */
  1076. mtlr r0
  1077. blr
  1078. /*
  1079. * The PAGE_OFFSET will map to a different physical address,
  1080. * jump to _start to do another relocation again.
  1081. */
  1082. 3: mr r3,r5
  1083. bl _start