head.S 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. *
  5. * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
  6. * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
  7. * Adapted for Power Macintosh by Paul Mackerras.
  8. * Low-level exception handlers and MMU support
  9. * rewritten by Paul Mackerras.
  10. * Copyright (C) 1996 Paul Mackerras.
  11. * MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
  12. * Amiga/APUS changes by Jesper Skov (jskov@cygnus.co.uk).
  13. *
  14. * This file contains the low-level support and setup for the
  15. * PowerPC platform, including trap and interrupt dispatch.
  16. * (The PPC 8xx embedded CPUs use head_8xx.S instead.)
  17. *
  18. * This program is free software; you can redistribute it and/or
  19. * modify it under the terms of the GNU General Public License
  20. * as published by the Free Software Foundation; either version
  21. * 2 of the License, or (at your option) any later version.
  22. *
  23. */
  24. #include <asm/processor.h>
  25. #include <asm/page.h>
  26. #include <asm/mmu.h>
  27. #include <asm/pgtable.h>
  28. #include <asm/cputable.h>
  29. #include <asm/cache.h>
  30. #include <asm/thread_info.h>
  31. #include <asm/ppc_asm.h>
  32. #include <asm/asm-offsets.h>
  33. #ifdef CONFIG_APUS
  34. #include <asm/amigappc.h>
  35. #endif
  36. /* 601 only have IBAT; cr0.eq is set on 601 when using this macro */
  37. #define LOAD_BAT(n, reg, RA, RB) \
  38. /* see the comment for clear_bats() -- Cort */ \
  39. li RA,0; \
  40. mtspr SPRN_IBAT##n##U,RA; \
  41. mtspr SPRN_DBAT##n##U,RA; \
  42. lwz RA,(n*16)+0(reg); \
  43. lwz RB,(n*16)+4(reg); \
  44. mtspr SPRN_IBAT##n##U,RA; \
  45. mtspr SPRN_IBAT##n##L,RB; \
  46. beq 1f; \
  47. lwz RA,(n*16)+8(reg); \
  48. lwz RB,(n*16)+12(reg); \
  49. mtspr SPRN_DBAT##n##U,RA; \
  50. mtspr SPRN_DBAT##n##L,RB; \
  51. 1:
  52. .text
  53. .stabs "arch/ppc/kernel/",N_SO,0,0,0f
  54. .stabs "head.S",N_SO,0,0,0f
  55. 0:
  56. .globl _stext
  57. _stext:
  58. /*
  59. * _start is defined this way because the XCOFF loader in the OpenFirmware
  60. * on the powermac expects the entry point to be a procedure descriptor.
  61. */
  62. .text
  63. .globl _start
  64. _start:
  65. /*
  66. * These are here for legacy reasons, the kernel used to
  67. * need to look like a coff function entry for the pmac
  68. * but we're always started by some kind of bootloader now.
  69. * -- Cort
  70. */
  71. nop /* used by __secondary_hold on prep (mtx) and chrp smp */
  72. nop /* used by __secondary_hold on prep (mtx) and chrp smp */
  73. nop
  74. /* PMAC
  75. * Enter here with the kernel text, data and bss loaded starting at
  76. * 0, running with virtual == physical mapping.
  77. * r5 points to the prom entry point (the client interface handler
  78. * address). Address translation is turned on, with the prom
  79. * managing the hash table. Interrupts are disabled. The stack
  80. * pointer (r1) points to just below the end of the half-meg region
  81. * from 0x380000 - 0x400000, which is mapped in already.
  82. *
  83. * If we are booted from MacOS via BootX, we enter with the kernel
  84. * image loaded somewhere, and the following values in registers:
  85. * r3: 'BooX' (0x426f6f58)
  86. * r4: virtual address of boot_infos_t
  87. * r5: 0
  88. *
  89. * APUS
  90. * r3: 'APUS'
  91. * r4: physical address of memory base
  92. * Linux/m68k style BootInfo structure at &_end.
  93. *
  94. * PREP
  95. * This is jumped to on prep systems right after the kernel is relocated
  96. * to its proper place in memory by the boot loader. The expected layout
  97. * of the regs is:
  98. * r3: ptr to residual data
  99. * r4: initrd_start or if no initrd then 0
  100. * r5: initrd_end - unused if r4 is 0
  101. * r6: Start of command line string
  102. * r7: End of command line string
  103. *
  104. * This just gets a minimal mmu environment setup so we can call
  105. * start_here() to do the real work.
  106. * -- Cort
  107. */
  108. .globl __start
  109. __start:
  110. mr r31,r3 /* save parameters */
  111. mr r30,r4
  112. mr r29,r5
  113. mr r28,r6
  114. mr r27,r7
  115. li r24,0 /* cpu # */
  116. /*
  117. * early_init() does the early machine identification and does
  118. * the necessary low-level setup and clears the BSS
  119. * -- Cort <cort@fsmlabs.com>
  120. */
  121. bl early_init
  122. #ifdef CONFIG_APUS
  123. /* On APUS the __va/__pa constants need to be set to the correct
  124. * values before continuing.
  125. */
  126. mr r4,r30
  127. bl fix_mem_constants
  128. #endif /* CONFIG_APUS */
  129. /* Switch MMU off, clear BATs and flush TLB. At this point, r3 contains
  130. * the physical address we are running at, returned by early_init()
  131. */
  132. bl mmu_off
  133. __after_mmu_off:
  134. bl clear_bats
  135. bl flush_tlbs
  136. bl initial_bats
  137. #if !defined(CONFIG_APUS) && defined(CONFIG_BOOTX_TEXT)
  138. bl setup_disp_bat
  139. #endif
  140. /*
  141. * Call setup_cpu for CPU 0 and initialize 6xx Idle
  142. */
  143. bl reloc_offset
  144. li r24,0 /* cpu# */
  145. bl call_setup_cpu /* Call setup_cpu for this CPU */
  146. #ifdef CONFIG_6xx
  147. bl reloc_offset
  148. bl init_idle_6xx
  149. #endif /* CONFIG_6xx */
  150. #ifndef CONFIG_APUS
  151. /*
  152. * We need to run with _start at physical address 0.
  153. * If the MMU is already turned on, we copy stuff to KERNELBASE,
  154. * otherwise we copy it to 0.
  155. */
  156. bl reloc_offset
  157. mr r26,r3
  158. addis r4,r3,KERNELBASE@h /* current address of _start */
  159. cmpwi 0,r4,0 /* are we already running at 0? */
  160. bne relocate_kernel
  161. #endif /* CONFIG_APUS */
  162. /*
  163. * we now have the 1st 16M of ram mapped with the bats.
  164. * prep needs the mmu to be turned on here, but pmac already has it on.
  165. * this shouldn't bother the pmac since it just gets turned on again
  166. * as we jump to our code at KERNELBASE. -- Cort
  167. * Actually no, pmac doesn't have it on any more. BootX enters with MMU
  168. * off, and in other cases, we now turn it off before changing BATs above.
  169. */
  170. turn_on_mmu:
  171. mfmsr r0
  172. ori r0,r0,MSR_DR|MSR_IR
  173. mtspr SPRN_SRR1,r0
  174. lis r0,start_here@h
  175. ori r0,r0,start_here@l
  176. mtspr SPRN_SRR0,r0
  177. SYNC
  178. RFI /* enables MMU */
  179. /*
  180. * We need __secondary_hold as a place to hold the other cpus on
  181. * an SMP machine, even when we are running a UP kernel.
  182. */
  183. . = 0xc0 /* for prep bootloader */
  184. li r3,1 /* MTX only has 1 cpu */
  185. .globl __secondary_hold
  186. __secondary_hold:
  187. /* tell the master we're here */
  188. stw r3,4(0)
  189. #ifdef CONFIG_SMP
  190. 100: lwz r4,0(0)
  191. /* wait until we're told to start */
  192. cmpw 0,r4,r3
  193. bne 100b
  194. /* our cpu # was at addr 0 - go */
  195. mr r24,r3 /* cpu # */
  196. b __secondary_start
  197. #else
  198. b .
  199. #endif /* CONFIG_SMP */
  200. /*
  201. * Exception entry code. This code runs with address translation
  202. * turned off, i.e. using physical addresses.
  203. * We assume sprg3 has the physical address of the current
  204. * task's thread_struct.
  205. */
  206. #define EXCEPTION_PROLOG \
  207. mtspr SPRN_SPRG0,r10; \
  208. mtspr SPRN_SPRG1,r11; \
  209. mfcr r10; \
  210. EXCEPTION_PROLOG_1; \
  211. EXCEPTION_PROLOG_2
  212. #define EXCEPTION_PROLOG_1 \
  213. mfspr r11,SPRN_SRR1; /* check whether user or kernel */ \
  214. andi. r11,r11,MSR_PR; \
  215. tophys(r11,r1); /* use tophys(r1) if kernel */ \
  216. beq 1f; \
  217. mfspr r11,SPRN_SPRG3; \
  218. lwz r11,THREAD_INFO-THREAD(r11); \
  219. addi r11,r11,THREAD_SIZE; \
  220. tophys(r11,r11); \
  221. 1: subi r11,r11,INT_FRAME_SIZE /* alloc exc. frame */
  222. #define EXCEPTION_PROLOG_2 \
  223. CLR_TOP32(r11); \
  224. stw r10,_CCR(r11); /* save registers */ \
  225. stw r12,GPR12(r11); \
  226. stw r9,GPR9(r11); \
  227. mfspr r10,SPRN_SPRG0; \
  228. stw r10,GPR10(r11); \
  229. mfspr r12,SPRN_SPRG1; \
  230. stw r12,GPR11(r11); \
  231. mflr r10; \
  232. stw r10,_LINK(r11); \
  233. mfspr r12,SPRN_SRR0; \
  234. mfspr r9,SPRN_SRR1; \
  235. stw r1,GPR1(r11); \
  236. stw r1,0(r11); \
  237. tovirt(r1,r11); /* set new kernel sp */ \
  238. li r10,MSR_KERNEL & ~(MSR_IR|MSR_DR); /* can take exceptions */ \
  239. MTMSRD(r10); /* (except for mach check in rtas) */ \
  240. stw r0,GPR0(r11); \
  241. SAVE_4GPRS(3, r11); \
  242. SAVE_2GPRS(7, r11)
  243. /*
  244. * Note: code which follows this uses cr0.eq (set if from kernel),
  245. * r11, r12 (SRR0), and r9 (SRR1).
  246. *
  247. * Note2: once we have set r1 we are in a position to take exceptions
  248. * again, and we could thus set MSR:RI at that point.
  249. */
  250. /*
  251. * Exception vectors.
  252. */
  253. #define EXCEPTION(n, label, hdlr, xfer) \
  254. . = n; \
  255. label: \
  256. EXCEPTION_PROLOG; \
  257. addi r3,r1,STACK_FRAME_OVERHEAD; \
  258. xfer(n, hdlr)
  259. #define EXC_XFER_TEMPLATE(n, hdlr, trap, copyee, tfer, ret) \
  260. li r10,trap; \
  261. stw r10,TRAP(r11); \
  262. li r10,MSR_KERNEL; \
  263. copyee(r10, r9); \
  264. bl tfer; \
  265. i##n: \
  266. .long hdlr; \
  267. .long ret
  268. #define COPY_EE(d, s) rlwimi d,s,0,16,16
  269. #define NOCOPY(d, s)
  270. #define EXC_XFER_STD(n, hdlr) \
  271. EXC_XFER_TEMPLATE(n, hdlr, n, NOCOPY, transfer_to_handler_full, \
  272. ret_from_except_full)
  273. #define EXC_XFER_LITE(n, hdlr) \
  274. EXC_XFER_TEMPLATE(n, hdlr, n+1, NOCOPY, transfer_to_handler, \
  275. ret_from_except)
  276. #define EXC_XFER_EE(n, hdlr) \
  277. EXC_XFER_TEMPLATE(n, hdlr, n, COPY_EE, transfer_to_handler_full, \
  278. ret_from_except_full)
  279. #define EXC_XFER_EE_LITE(n, hdlr) \
  280. EXC_XFER_TEMPLATE(n, hdlr, n+1, COPY_EE, transfer_to_handler, \
  281. ret_from_except)
  282. /* System reset */
  283. /* core99 pmac starts the seconary here by changing the vector, and
  284. putting it back to what it was (unknown_exception) when done. */
  285. #if defined(CONFIG_GEMINI) && defined(CONFIG_SMP)
  286. . = 0x100
  287. b __secondary_start_gemini
  288. #else
  289. EXCEPTION(0x100, Reset, unknown_exception, EXC_XFER_STD)
  290. #endif
  291. /* Machine check */
  292. . = 0x200
  293. mtspr SPRN_SPRG0,r10
  294. mtspr SPRN_SPRG1,r11
  295. mfcr r10
  296. EXCEPTION_PROLOG_1
  297. 7: EXCEPTION_PROLOG_2
  298. addi r3,r1,STACK_FRAME_OVERHEAD
  299. EXC_XFER_STD(0x200, machine_check_exception)
  300. /* Data access exception. */
  301. . = 0x300
  302. DataAccess:
  303. EXCEPTION_PROLOG
  304. mfspr r10,SPRN_DSISR
  305. andis. r0,r10,0xa470 /* weird error? */
  306. bne 1f /* if not, try to put a PTE */
  307. mfspr r4,SPRN_DAR /* into the hash table */
  308. rlwinm r3,r10,32-15,21,21 /* DSISR_STORE -> _PAGE_RW */
  309. bl hash_page
  310. 1: stw r10,_DSISR(r11)
  311. mr r5,r10
  312. mfspr r4,SPRN_DAR
  313. EXC_XFER_EE_LITE(0x300, handle_page_fault)
  314. /* Instruction access exception. */
  315. . = 0x400
  316. InstructionAccess:
  317. EXCEPTION_PROLOG
  318. andis. r0,r9,0x4000 /* no pte found? */
  319. beq 1f /* if so, try to put a PTE */
  320. li r3,0 /* into the hash table */
  321. mr r4,r12 /* SRR0 is fault address */
  322. bl hash_page
  323. 1: mr r4,r12
  324. mr r5,r9
  325. EXC_XFER_EE_LITE(0x400, handle_page_fault)
  326. /* External interrupt */
  327. EXCEPTION(0x500, HardwareInterrupt, do_IRQ, EXC_XFER_LITE)
  328. /* Alignment exception */
  329. . = 0x600
  330. Alignment:
  331. EXCEPTION_PROLOG
  332. mfspr r4,SPRN_DAR
  333. stw r4,_DAR(r11)
  334. mfspr r5,SPRN_DSISR
  335. stw r5,_DSISR(r11)
  336. addi r3,r1,STACK_FRAME_OVERHEAD
  337. EXC_XFER_EE(0x600, alignment_exception)
  338. /* Program check exception */
  339. EXCEPTION(0x700, ProgramCheck, program_check_exception, EXC_XFER_STD)
  340. /* Floating-point unavailable */
  341. . = 0x800
  342. FPUnavailable:
  343. EXCEPTION_PROLOG
  344. bne load_up_fpu /* if from user, just load it up */
  345. addi r3,r1,STACK_FRAME_OVERHEAD
  346. EXC_XFER_EE_LITE(0x800, kernel_fp_unavailable_exception)
  347. /* Decrementer */
  348. EXCEPTION(0x900, Decrementer, timer_interrupt, EXC_XFER_LITE)
  349. EXCEPTION(0xa00, Trap_0a, unknown_exception, EXC_XFER_EE)
  350. EXCEPTION(0xb00, Trap_0b, unknown_exception, EXC_XFER_EE)
  351. /* System call */
  352. . = 0xc00
  353. SystemCall:
  354. EXCEPTION_PROLOG
  355. EXC_XFER_EE_LITE(0xc00, DoSyscall)
  356. /* Single step - not used on 601 */
  357. EXCEPTION(0xd00, SingleStep, single_step_exception, EXC_XFER_STD)
  358. EXCEPTION(0xe00, Trap_0e, unknown_exception, EXC_XFER_EE)
  359. /*
  360. * The Altivec unavailable trap is at 0x0f20. Foo.
  361. * We effectively remap it to 0x3000.
  362. * We include an altivec unavailable exception vector even if
  363. * not configured for Altivec, so that you can't panic a
  364. * non-altivec kernel running on a machine with altivec just
  365. * by executing an altivec instruction.
  366. */
  367. . = 0xf00
  368. b Trap_0f
  369. . = 0xf20
  370. b AltiVecUnavailable
  371. Trap_0f:
  372. EXCEPTION_PROLOG
  373. addi r3,r1,STACK_FRAME_OVERHEAD
  374. EXC_XFER_EE(0xf00, unknown_exception)
  375. /*
  376. * Handle TLB miss for instruction on 603/603e.
  377. * Note: we get an alternate set of r0 - r3 to use automatically.
  378. */
  379. . = 0x1000
  380. InstructionTLBMiss:
  381. /*
  382. * r0: stored ctr
  383. * r1: linux style pte ( later becomes ppc hardware pte )
  384. * r2: ptr to linux-style pte
  385. * r3: scratch
  386. */
  387. mfctr r0
  388. /* Get PTE (linux-style) and check access */
  389. mfspr r3,SPRN_IMISS
  390. lis r1,KERNELBASE@h /* check if kernel address */
  391. cmplw 0,r3,r1
  392. mfspr r2,SPRN_SPRG3
  393. li r1,_PAGE_USER|_PAGE_PRESENT /* low addresses tested as user */
  394. lwz r2,PGDIR(r2)
  395. blt+ 112f
  396. lis r2,swapper_pg_dir@ha /* if kernel address, use */
  397. addi r2,r2,swapper_pg_dir@l /* kernel page table */
  398. mfspr r1,SPRN_SRR1 /* and MSR_PR bit from SRR1 */
  399. rlwinm r1,r1,32-12,29,29 /* shift MSR_PR to _PAGE_USER posn */
  400. 112: tophys(r2,r2)
  401. rlwimi r2,r3,12,20,29 /* insert top 10 bits of address */
  402. lwz r2,0(r2) /* get pmd entry */
  403. rlwinm. r2,r2,0,0,19 /* extract address of pte page */
  404. beq- InstructionAddressInvalid /* return if no mapping */
  405. rlwimi r2,r3,22,20,29 /* insert next 10 bits of address */
  406. lwz r3,0(r2) /* get linux-style pte */
  407. andc. r1,r1,r3 /* check access & ~permission */
  408. bne- InstructionAddressInvalid /* return if access not permitted */
  409. ori r3,r3,_PAGE_ACCESSED /* set _PAGE_ACCESSED in pte */
  410. /*
  411. * NOTE! We are assuming this is not an SMP system, otherwise
  412. * we would need to update the pte atomically with lwarx/stwcx.
  413. */
  414. stw r3,0(r2) /* update PTE (accessed bit) */
  415. /* Convert linux-style PTE to low word of PPC-style PTE */
  416. rlwinm r1,r3,32-10,31,31 /* _PAGE_RW -> PP lsb */
  417. rlwinm r2,r3,32-7,31,31 /* _PAGE_DIRTY -> PP lsb */
  418. and r1,r1,r2 /* writable if _RW and _DIRTY */
  419. rlwimi r3,r3,32-1,30,30 /* _PAGE_USER -> PP msb */
  420. rlwimi r3,r3,32-1,31,31 /* _PAGE_USER -> PP lsb */
  421. ori r1,r1,0xe14 /* clear out reserved bits and M */
  422. andc r1,r3,r1 /* PP = user? (rw&dirty? 2: 3): 0 */
  423. mtspr SPRN_RPA,r1
  424. mfspr r3,SPRN_IMISS
  425. tlbli r3
  426. mfspr r3,SPRN_SRR1 /* Need to restore CR0 */
  427. mtcrf 0x80,r3
  428. rfi
  429. InstructionAddressInvalid:
  430. mfspr r3,SPRN_SRR1
  431. rlwinm r1,r3,9,6,6 /* Get load/store bit */
  432. addis r1,r1,0x2000
  433. mtspr SPRN_DSISR,r1 /* (shouldn't be needed) */
  434. mtctr r0 /* Restore CTR */
  435. andi. r2,r3,0xFFFF /* Clear upper bits of SRR1 */
  436. or r2,r2,r1
  437. mtspr SPRN_SRR1,r2
  438. mfspr r1,SPRN_IMISS /* Get failing address */
  439. rlwinm. r2,r2,0,31,31 /* Check for little endian access */
  440. rlwimi r2,r2,1,30,30 /* change 1 -> 3 */
  441. xor r1,r1,r2
  442. mtspr SPRN_DAR,r1 /* Set fault address */
  443. mfmsr r0 /* Restore "normal" registers */
  444. xoris r0,r0,MSR_TGPR>>16
  445. mtcrf 0x80,r3 /* Restore CR0 */
  446. mtmsr r0
  447. b InstructionAccess
  448. /*
  449. * Handle TLB miss for DATA Load operation on 603/603e
  450. */
  451. . = 0x1100
  452. DataLoadTLBMiss:
  453. /*
  454. * r0: stored ctr
  455. * r1: linux style pte ( later becomes ppc hardware pte )
  456. * r2: ptr to linux-style pte
  457. * r3: scratch
  458. */
  459. mfctr r0
  460. /* Get PTE (linux-style) and check access */
  461. mfspr r3,SPRN_DMISS
  462. lis r1,KERNELBASE@h /* check if kernel address */
  463. cmplw 0,r3,r1
  464. mfspr r2,SPRN_SPRG3
  465. li r1,_PAGE_USER|_PAGE_PRESENT /* low addresses tested as user */
  466. lwz r2,PGDIR(r2)
  467. blt+ 112f
  468. lis r2,swapper_pg_dir@ha /* if kernel address, use */
  469. addi r2,r2,swapper_pg_dir@l /* kernel page table */
  470. mfspr r1,SPRN_SRR1 /* and MSR_PR bit from SRR1 */
  471. rlwinm r1,r1,32-12,29,29 /* shift MSR_PR to _PAGE_USER posn */
  472. 112: tophys(r2,r2)
  473. rlwimi r2,r3,12,20,29 /* insert top 10 bits of address */
  474. lwz r2,0(r2) /* get pmd entry */
  475. rlwinm. r2,r2,0,0,19 /* extract address of pte page */
  476. beq- DataAddressInvalid /* return if no mapping */
  477. rlwimi r2,r3,22,20,29 /* insert next 10 bits of address */
  478. lwz r3,0(r2) /* get linux-style pte */
  479. andc. r1,r1,r3 /* check access & ~permission */
  480. bne- DataAddressInvalid /* return if access not permitted */
  481. ori r3,r3,_PAGE_ACCESSED /* set _PAGE_ACCESSED in pte */
  482. /*
  483. * NOTE! We are assuming this is not an SMP system, otherwise
  484. * we would need to update the pte atomically with lwarx/stwcx.
  485. */
  486. stw r3,0(r2) /* update PTE (accessed bit) */
  487. /* Convert linux-style PTE to low word of PPC-style PTE */
  488. rlwinm r1,r3,32-10,31,31 /* _PAGE_RW -> PP lsb */
  489. rlwinm r2,r3,32-7,31,31 /* _PAGE_DIRTY -> PP lsb */
  490. and r1,r1,r2 /* writable if _RW and _DIRTY */
  491. rlwimi r3,r3,32-1,30,30 /* _PAGE_USER -> PP msb */
  492. rlwimi r3,r3,32-1,31,31 /* _PAGE_USER -> PP lsb */
  493. ori r1,r1,0xe14 /* clear out reserved bits and M */
  494. andc r1,r3,r1 /* PP = user? (rw&dirty? 2: 3): 0 */
  495. mtspr SPRN_RPA,r1
  496. mfspr r3,SPRN_DMISS
  497. tlbld r3
  498. mfspr r3,SPRN_SRR1 /* Need to restore CR0 */
  499. mtcrf 0x80,r3
  500. rfi
  501. DataAddressInvalid:
  502. mfspr r3,SPRN_SRR1
  503. rlwinm r1,r3,9,6,6 /* Get load/store bit */
  504. addis r1,r1,0x2000
  505. mtspr SPRN_DSISR,r1
  506. mtctr r0 /* Restore CTR */
  507. andi. r2,r3,0xFFFF /* Clear upper bits of SRR1 */
  508. mtspr SPRN_SRR1,r2
  509. mfspr r1,SPRN_DMISS /* Get failing address */
  510. rlwinm. r2,r2,0,31,31 /* Check for little endian access */
  511. beq 20f /* Jump if big endian */
  512. xori r1,r1,3
  513. 20: mtspr SPRN_DAR,r1 /* Set fault address */
  514. mfmsr r0 /* Restore "normal" registers */
  515. xoris r0,r0,MSR_TGPR>>16
  516. mtcrf 0x80,r3 /* Restore CR0 */
  517. mtmsr r0
  518. b DataAccess
  519. /*
  520. * Handle TLB miss for DATA Store on 603/603e
  521. */
  522. . = 0x1200
  523. DataStoreTLBMiss:
  524. /*
  525. * r0: stored ctr
  526. * r1: linux style pte ( later becomes ppc hardware pte )
  527. * r2: ptr to linux-style pte
  528. * r3: scratch
  529. */
  530. mfctr r0
  531. /* Get PTE (linux-style) and check access */
  532. mfspr r3,SPRN_DMISS
  533. lis r1,KERNELBASE@h /* check if kernel address */
  534. cmplw 0,r3,r1
  535. mfspr r2,SPRN_SPRG3
  536. li r1,_PAGE_RW|_PAGE_USER|_PAGE_PRESENT /* access flags */
  537. lwz r2,PGDIR(r2)
  538. blt+ 112f
  539. lis r2,swapper_pg_dir@ha /* if kernel address, use */
  540. addi r2,r2,swapper_pg_dir@l /* kernel page table */
  541. mfspr r1,SPRN_SRR1 /* and MSR_PR bit from SRR1 */
  542. rlwinm r1,r1,32-12,29,29 /* shift MSR_PR to _PAGE_USER posn */
  543. 112: tophys(r2,r2)
  544. rlwimi r2,r3,12,20,29 /* insert top 10 bits of address */
  545. lwz r2,0(r2) /* get pmd entry */
  546. rlwinm. r2,r2,0,0,19 /* extract address of pte page */
  547. beq- DataAddressInvalid /* return if no mapping */
  548. rlwimi r2,r3,22,20,29 /* insert next 10 bits of address */
  549. lwz r3,0(r2) /* get linux-style pte */
  550. andc. r1,r1,r3 /* check access & ~permission */
  551. bne- DataAddressInvalid /* return if access not permitted */
  552. ori r3,r3,_PAGE_ACCESSED|_PAGE_DIRTY
  553. /*
  554. * NOTE! We are assuming this is not an SMP system, otherwise
  555. * we would need to update the pte atomically with lwarx/stwcx.
  556. */
  557. stw r3,0(r2) /* update PTE (accessed/dirty bits) */
  558. /* Convert linux-style PTE to low word of PPC-style PTE */
  559. rlwimi r3,r3,32-1,30,30 /* _PAGE_USER -> PP msb */
  560. li r1,0xe15 /* clear out reserved bits and M */
  561. andc r1,r3,r1 /* PP = user? 2: 0 */
  562. mtspr SPRN_RPA,r1
  563. mfspr r3,SPRN_DMISS
  564. tlbld r3
  565. mfspr r3,SPRN_SRR1 /* Need to restore CR0 */
  566. mtcrf 0x80,r3
  567. rfi
  568. #ifndef CONFIG_ALTIVEC
  569. #define altivec_assist_exception unknown_exception
  570. #endif
  571. EXCEPTION(0x1300, Trap_13, instruction_breakpoint_exception, EXC_XFER_EE)
  572. EXCEPTION(0x1400, SMI, SMIException, EXC_XFER_EE)
  573. EXCEPTION(0x1500, Trap_15, unknown_exception, EXC_XFER_EE)
  574. EXCEPTION(0x1600, Trap_16, altivec_assist_exception, EXC_XFER_EE)
  575. EXCEPTION(0x1700, Trap_17, TAUException, EXC_XFER_STD)
  576. EXCEPTION(0x1800, Trap_18, unknown_exception, EXC_XFER_EE)
  577. EXCEPTION(0x1900, Trap_19, unknown_exception, EXC_XFER_EE)
  578. EXCEPTION(0x1a00, Trap_1a, unknown_exception, EXC_XFER_EE)
  579. EXCEPTION(0x1b00, Trap_1b, unknown_exception, EXC_XFER_EE)
  580. EXCEPTION(0x1c00, Trap_1c, unknown_exception, EXC_XFER_EE)
  581. EXCEPTION(0x1d00, Trap_1d, unknown_exception, EXC_XFER_EE)
  582. EXCEPTION(0x1e00, Trap_1e, unknown_exception, EXC_XFER_EE)
  583. EXCEPTION(0x1f00, Trap_1f, unknown_exception, EXC_XFER_EE)
  584. EXCEPTION(0x2000, RunMode, RunModeException, EXC_XFER_EE)
  585. EXCEPTION(0x2100, Trap_21, unknown_exception, EXC_XFER_EE)
  586. EXCEPTION(0x2200, Trap_22, unknown_exception, EXC_XFER_EE)
  587. EXCEPTION(0x2300, Trap_23, unknown_exception, EXC_XFER_EE)
  588. EXCEPTION(0x2400, Trap_24, unknown_exception, EXC_XFER_EE)
  589. EXCEPTION(0x2500, Trap_25, unknown_exception, EXC_XFER_EE)
  590. EXCEPTION(0x2600, Trap_26, unknown_exception, EXC_XFER_EE)
  591. EXCEPTION(0x2700, Trap_27, unknown_exception, EXC_XFER_EE)
  592. EXCEPTION(0x2800, Trap_28, unknown_exception, EXC_XFER_EE)
  593. EXCEPTION(0x2900, Trap_29, unknown_exception, EXC_XFER_EE)
  594. EXCEPTION(0x2a00, Trap_2a, unknown_exception, EXC_XFER_EE)
  595. EXCEPTION(0x2b00, Trap_2b, unknown_exception, EXC_XFER_EE)
  596. EXCEPTION(0x2c00, Trap_2c, unknown_exception, EXC_XFER_EE)
  597. EXCEPTION(0x2d00, Trap_2d, unknown_exception, EXC_XFER_EE)
  598. EXCEPTION(0x2e00, Trap_2e, unknown_exception, EXC_XFER_EE)
  599. EXCEPTION(0x2f00, MOLTrampoline, unknown_exception, EXC_XFER_EE_LITE)
  600. .globl mol_trampoline
  601. .set mol_trampoline, i0x2f00
  602. . = 0x3000
  603. AltiVecUnavailable:
  604. EXCEPTION_PROLOG
  605. #ifdef CONFIG_ALTIVEC
  606. bne load_up_altivec /* if from user, just load it up */
  607. #endif /* CONFIG_ALTIVEC */
  608. addi r3,r1,STACK_FRAME_OVERHEAD
  609. EXC_XFER_EE_LITE(0xf20, altivec_unavailable_exception)
  610. #ifdef CONFIG_ALTIVEC
  611. /* Note that the AltiVec support is closely modeled after the FP
  612. * support. Changes to one are likely to be applicable to the
  613. * other! */
  614. load_up_altivec:
  615. /*
  616. * Disable AltiVec for the task which had AltiVec previously,
  617. * and save its AltiVec registers in its thread_struct.
  618. * Enables AltiVec for use in the kernel on return.
  619. * On SMP we know the AltiVec units are free, since we give it up every
  620. * switch. -- Kumar
  621. */
  622. mfmsr r5
  623. oris r5,r5,MSR_VEC@h
  624. MTMSRD(r5) /* enable use of AltiVec now */
  625. isync
  626. /*
  627. * For SMP, we don't do lazy AltiVec switching because it just gets too
  628. * horrendously complex, especially when a task switches from one CPU
  629. * to another. Instead we call giveup_altivec in switch_to.
  630. */
  631. #ifndef CONFIG_SMP
  632. tophys(r6,0)
  633. addis r3,r6,last_task_used_altivec@ha
  634. lwz r4,last_task_used_altivec@l(r3)
  635. cmpwi 0,r4,0
  636. beq 1f
  637. add r4,r4,r6
  638. addi r4,r4,THREAD /* want THREAD of last_task_used_altivec */
  639. SAVE_32VRS(0,r10,r4)
  640. mfvscr vr0
  641. li r10,THREAD_VSCR
  642. stvx vr0,r10,r4
  643. lwz r5,PT_REGS(r4)
  644. add r5,r5,r6
  645. lwz r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  646. lis r10,MSR_VEC@h
  647. andc r4,r4,r10 /* disable altivec for previous task */
  648. stw r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  649. 1:
  650. #endif /* CONFIG_SMP */
  651. /* enable use of AltiVec after return */
  652. oris r9,r9,MSR_VEC@h
  653. mfspr r5,SPRN_SPRG3 /* current task's THREAD (phys) */
  654. li r4,1
  655. li r10,THREAD_VSCR
  656. stw r4,THREAD_USED_VR(r5)
  657. lvx vr0,r10,r5
  658. mtvscr vr0
  659. REST_32VRS(0,r10,r5)
  660. #ifndef CONFIG_SMP
  661. subi r4,r5,THREAD
  662. sub r4,r4,r6
  663. stw r4,last_task_used_altivec@l(r3)
  664. #endif /* CONFIG_SMP */
  665. /* restore registers and return */
  666. /* we haven't used ctr or xer or lr */
  667. b fast_exception_return
  668. /*
  669. * AltiVec unavailable trap from kernel - print a message, but let
  670. * the task use AltiVec in the kernel until it returns to user mode.
  671. */
  672. KernelAltiVec:
  673. lwz r3,_MSR(r1)
  674. oris r3,r3,MSR_VEC@h
  675. stw r3,_MSR(r1) /* enable use of AltiVec after return */
  676. lis r3,87f@h
  677. ori r3,r3,87f@l
  678. mr r4,r2 /* current */
  679. lwz r5,_NIP(r1)
  680. bl printk
  681. b ret_from_except
  682. 87: .string "AltiVec used in kernel (task=%p, pc=%x) \n"
  683. .align 4,0
  684. /*
  685. * giveup_altivec(tsk)
  686. * Disable AltiVec for the task given as the argument,
  687. * and save the AltiVec registers in its thread_struct.
  688. * Enables AltiVec for use in the kernel on return.
  689. */
  690. .globl giveup_altivec
  691. giveup_altivec:
  692. mfmsr r5
  693. oris r5,r5,MSR_VEC@h
  694. SYNC
  695. MTMSRD(r5) /* enable use of AltiVec now */
  696. isync
  697. cmpwi 0,r3,0
  698. beqlr- /* if no previous owner, done */
  699. addi r3,r3,THREAD /* want THREAD of task */
  700. lwz r5,PT_REGS(r3)
  701. cmpwi 0,r5,0
  702. SAVE_32VRS(0, r4, r3)
  703. mfvscr vr0
  704. li r4,THREAD_VSCR
  705. stvx vr0,r4,r3
  706. beq 1f
  707. lwz r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  708. lis r3,MSR_VEC@h
  709. andc r4,r4,r3 /* disable AltiVec for previous task */
  710. stw r4,_MSR-STACK_FRAME_OVERHEAD(r5)
  711. 1:
  712. #ifndef CONFIG_SMP
  713. li r5,0
  714. lis r4,last_task_used_altivec@ha
  715. stw r5,last_task_used_altivec@l(r4)
  716. #endif /* CONFIG_SMP */
  717. blr
  718. #endif /* CONFIG_ALTIVEC */
  719. /*
  720. * This code is jumped to from the startup code to copy
  721. * the kernel image to physical address 0.
  722. */
  723. relocate_kernel:
  724. addis r9,r26,klimit@ha /* fetch klimit */
  725. lwz r25,klimit@l(r9)
  726. addis r25,r25,-KERNELBASE@h
  727. li r3,0 /* Destination base address */
  728. li r6,0 /* Destination offset */
  729. li r5,0x4000 /* # bytes of memory to copy */
  730. bl copy_and_flush /* copy the first 0x4000 bytes */
  731. addi r0,r3,4f@l /* jump to the address of 4f */
  732. mtctr r0 /* in copy and do the rest. */
  733. bctr /* jump to the copy */
  734. 4: mr r5,r25
  735. bl copy_and_flush /* copy the rest */
  736. b turn_on_mmu
  737. /*
  738. * Copy routine used to copy the kernel to start at physical address 0
  739. * and flush and invalidate the caches as needed.
  740. * r3 = dest addr, r4 = source addr, r5 = copy limit, r6 = start offset
  741. * on exit, r3, r4, r5 are unchanged, r6 is updated to be >= r5.
  742. */
  743. copy_and_flush:
  744. addi r5,r5,-4
  745. addi r6,r6,-4
  746. 4: li r0,L1_CACHE_BYTES/4
  747. mtctr r0
  748. 3: addi r6,r6,4 /* copy a cache line */
  749. lwzx r0,r6,r4
  750. stwx r0,r6,r3
  751. bdnz 3b
  752. dcbst r6,r3 /* write it to memory */
  753. sync
  754. icbi r6,r3 /* flush the icache line */
  755. cmplw 0,r6,r5
  756. blt 4b
  757. sync /* additional sync needed on g4 */
  758. isync
  759. addi r5,r5,4
  760. addi r6,r6,4
  761. blr
  762. #ifdef CONFIG_APUS
  763. /*
  764. * On APUS the physical base address of the kernel is not known at compile
  765. * time, which means the __pa/__va constants used are incorrect. In the
  766. * __init section is recorded the virtual addresses of instructions using
  767. * these constants, so all that has to be done is fix these before
  768. * continuing the kernel boot.
  769. *
  770. * r4 = The physical address of the kernel base.
  771. */
  772. fix_mem_constants:
  773. mr r10,r4
  774. addis r10,r10,-KERNELBASE@h /* virt_to_phys constant */
  775. neg r11,r10 /* phys_to_virt constant */
  776. lis r12,__vtop_table_begin@h
  777. ori r12,r12,__vtop_table_begin@l
  778. add r12,r12,r10 /* table begin phys address */
  779. lis r13,__vtop_table_end@h
  780. ori r13,r13,__vtop_table_end@l
  781. add r13,r13,r10 /* table end phys address */
  782. subi r12,r12,4
  783. subi r13,r13,4
  784. 1: lwzu r14,4(r12) /* virt address of instruction */
  785. add r14,r14,r10 /* phys address of instruction */
  786. lwz r15,0(r14) /* instruction, now insert top */
  787. rlwimi r15,r10,16,16,31 /* half of vp const in low half */
  788. stw r15,0(r14) /* of instruction and restore. */
  789. dcbst r0,r14 /* write it to memory */
  790. sync
  791. icbi r0,r14 /* flush the icache line */
  792. cmpw r12,r13
  793. bne 1b
  794. sync /* additional sync needed on g4 */
  795. isync
  796. /*
  797. * Map the memory where the exception handlers will
  798. * be copied to when hash constants have been patched.
  799. */
  800. #ifdef CONFIG_APUS_FAST_EXCEPT
  801. lis r8,0xfff0
  802. #else
  803. lis r8,0
  804. #endif
  805. ori r8,r8,0x2 /* 128KB, supervisor */
  806. mtspr SPRN_DBAT3U,r8
  807. mtspr SPRN_DBAT3L,r8
  808. lis r12,__ptov_table_begin@h
  809. ori r12,r12,__ptov_table_begin@l
  810. add r12,r12,r10 /* table begin phys address */
  811. lis r13,__ptov_table_end@h
  812. ori r13,r13,__ptov_table_end@l
  813. add r13,r13,r10 /* table end phys address */
  814. subi r12,r12,4
  815. subi r13,r13,4
  816. 1: lwzu r14,4(r12) /* virt address of instruction */
  817. add r14,r14,r10 /* phys address of instruction */
  818. lwz r15,0(r14) /* instruction, now insert top */
  819. rlwimi r15,r11,16,16,31 /* half of pv const in low half*/
  820. stw r15,0(r14) /* of instruction and restore. */
  821. dcbst r0,r14 /* write it to memory */
  822. sync
  823. icbi r0,r14 /* flush the icache line */
  824. cmpw r12,r13
  825. bne 1b
  826. sync /* additional sync needed on g4 */
  827. isync /* No speculative loading until now */
  828. blr
  829. /***********************************************************************
  830. * Please note that on APUS the exception handlers are located at the
  831. * physical address 0xfff0000. For this reason, the exception handlers
  832. * cannot use relative branches to access the code below.
  833. ***********************************************************************/
  834. #endif /* CONFIG_APUS */
  835. #ifdef CONFIG_SMP
  836. #ifdef CONFIG_GEMINI
  837. .globl __secondary_start_gemini
  838. __secondary_start_gemini:
  839. mfspr r4,SPRN_HID0
  840. ori r4,r4,HID0_ICFI
  841. li r3,0
  842. ori r3,r3,HID0_ICE
  843. andc r4,r4,r3
  844. mtspr SPRN_HID0,r4
  845. sync
  846. b __secondary_start
  847. #endif /* CONFIG_GEMINI */
  848. .globl __secondary_start_pmac_0
  849. __secondary_start_pmac_0:
  850. /* NB the entries for cpus 0, 1, 2 must each occupy 8 bytes. */
  851. li r24,0
  852. b 1f
  853. li r24,1
  854. b 1f
  855. li r24,2
  856. b 1f
  857. li r24,3
  858. 1:
  859. /* on powersurge, we come in here with IR=0 and DR=1, and DBAT 0
  860. set to map the 0xf0000000 - 0xffffffff region */
  861. mfmsr r0
  862. rlwinm r0,r0,0,28,26 /* clear DR (0x10) */
  863. SYNC
  864. mtmsr r0
  865. isync
  866. .globl __secondary_start
  867. __secondary_start:
  868. /* Copy some CPU settings from CPU 0 */
  869. bl __restore_cpu_setup
  870. lis r3,-KERNELBASE@h
  871. mr r4,r24
  872. bl call_setup_cpu /* Call setup_cpu for this CPU */
  873. #ifdef CONFIG_6xx
  874. lis r3,-KERNELBASE@h
  875. bl init_idle_6xx
  876. #endif /* CONFIG_6xx */
  877. /* get current_thread_info and current */
  878. lis r1,secondary_ti@ha
  879. tophys(r1,r1)
  880. lwz r1,secondary_ti@l(r1)
  881. tophys(r2,r1)
  882. lwz r2,TI_TASK(r2)
  883. /* stack */
  884. addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
  885. li r0,0
  886. tophys(r3,r1)
  887. stw r0,0(r3)
  888. /* load up the MMU */
  889. bl load_up_mmu
  890. /* ptr to phys current thread */
  891. tophys(r4,r2)
  892. addi r4,r4,THREAD /* phys address of our thread_struct */
  893. CLR_TOP32(r4)
  894. mtspr SPRN_SPRG3,r4
  895. li r3,0
  896. mtspr SPRN_SPRG2,r3 /* 0 => not in RTAS */
  897. /* enable MMU and jump to start_secondary */
  898. li r4,MSR_KERNEL
  899. FIX_SRR1(r4,r5)
  900. lis r3,start_secondary@h
  901. ori r3,r3,start_secondary@l
  902. mtspr SPRN_SRR0,r3
  903. mtspr SPRN_SRR1,r4
  904. SYNC
  905. RFI
  906. #endif /* CONFIG_SMP */
  907. /*
  908. * Those generic dummy functions are kept for CPUs not
  909. * included in CONFIG_6xx
  910. */
  911. #if !defined(CONFIG_6xx)
  912. _GLOBAL(__save_cpu_setup)
  913. blr
  914. _GLOBAL(__restore_cpu_setup)
  915. blr
  916. #endif /* !defined(CONFIG_6xx) */
  917. /*
  918. * Load stuff into the MMU. Intended to be called with
  919. * IR=0 and DR=0.
  920. */
  921. load_up_mmu:
  922. sync /* Force all PTE updates to finish */
  923. isync
  924. tlbia /* Clear all TLB entries */
  925. sync /* wait for tlbia/tlbie to finish */
  926. TLBSYNC /* ... on all CPUs */
  927. /* Load the SDR1 register (hash table base & size) */
  928. lis r6,_SDR1@ha
  929. tophys(r6,r6)
  930. lwz r6,_SDR1@l(r6)
  931. mtspr SPRN_SDR1,r6
  932. li r0,16 /* load up segment register values */
  933. mtctr r0 /* for context 0 */
  934. lis r3,0x2000 /* Ku = 1, VSID = 0 */
  935. li r4,0
  936. 3: mtsrin r3,r4
  937. addi r3,r3,0x111 /* increment VSID */
  938. addis r4,r4,0x1000 /* address of next segment */
  939. bdnz 3b
  940. /* Load the BAT registers with the values set up by MMU_init.
  941. MMU_init takes care of whether we're on a 601 or not. */
  942. mfpvr r3
  943. srwi r3,r3,16
  944. cmpwi r3,1
  945. lis r3,BATS@ha
  946. addi r3,r3,BATS@l
  947. tophys(r3,r3)
  948. LOAD_BAT(0,r3,r4,r5)
  949. LOAD_BAT(1,r3,r4,r5)
  950. LOAD_BAT(2,r3,r4,r5)
  951. LOAD_BAT(3,r3,r4,r5)
  952. blr
  953. /*
  954. * This is where the main kernel code starts.
  955. */
  956. start_here:
  957. /* ptr to current */
  958. lis r2,init_task@h
  959. ori r2,r2,init_task@l
  960. /* Set up for using our exception vectors */
  961. /* ptr to phys current thread */
  962. tophys(r4,r2)
  963. addi r4,r4,THREAD /* init task's THREAD */
  964. CLR_TOP32(r4)
  965. mtspr SPRN_SPRG3,r4
  966. li r3,0
  967. mtspr SPRN_SPRG2,r3 /* 0 => not in RTAS */
  968. /* stack */
  969. lis r1,init_thread_union@ha
  970. addi r1,r1,init_thread_union@l
  971. li r0,0
  972. stwu r0,THREAD_SIZE-STACK_FRAME_OVERHEAD(r1)
  973. /*
  974. * Do early bootinfo parsing, platform-specific initialization,
  975. * and set up the MMU.
  976. */
  977. mr r3,r31
  978. mr r4,r30
  979. mr r5,r29
  980. mr r6,r28
  981. mr r7,r27
  982. bl machine_init
  983. bl MMU_init
  984. #ifdef CONFIG_APUS
  985. /* Copy exception code to exception vector base on APUS. */
  986. lis r4,KERNELBASE@h
  987. #ifdef CONFIG_APUS_FAST_EXCEPT
  988. lis r3,0xfff0 /* Copy to 0xfff00000 */
  989. #else
  990. lis r3,0 /* Copy to 0x00000000 */
  991. #endif
  992. li r5,0x4000 /* # bytes of memory to copy */
  993. li r6,0
  994. bl copy_and_flush /* copy the first 0x4000 bytes */
  995. #endif /* CONFIG_APUS */
  996. /*
  997. * Go back to running unmapped so we can load up new values
  998. * for SDR1 (hash table pointer) and the segment registers
  999. * and change to using our exception vectors.
  1000. */
  1001. lis r4,2f@h
  1002. ori r4,r4,2f@l
  1003. tophys(r4,r4)
  1004. li r3,MSR_KERNEL & ~(MSR_IR|MSR_DR)
  1005. FIX_SRR1(r3,r5)
  1006. mtspr SPRN_SRR0,r4
  1007. mtspr SPRN_SRR1,r3
  1008. SYNC
  1009. RFI
  1010. /* Load up the kernel context */
  1011. 2: bl load_up_mmu
  1012. #ifdef CONFIG_BDI_SWITCH
  1013. /* Add helper information for the Abatron bdiGDB debugger.
  1014. * We do this here because we know the mmu is disabled, and
  1015. * will be enabled for real in just a few instructions.
  1016. */
  1017. lis r5, abatron_pteptrs@h
  1018. ori r5, r5, abatron_pteptrs@l
  1019. stw r5, 0xf0(r0) /* This much match your Abatron config */
  1020. lis r6, swapper_pg_dir@h
  1021. ori r6, r6, swapper_pg_dir@l
  1022. tophys(r5, r5)
  1023. stw r6, 0(r5)
  1024. #endif /* CONFIG_BDI_SWITCH */
  1025. /* Now turn on the MMU for real! */
  1026. li r4,MSR_KERNEL
  1027. FIX_SRR1(r4,r5)
  1028. lis r3,start_kernel@h
  1029. ori r3,r3,start_kernel@l
  1030. mtspr SPRN_SRR0,r3
  1031. mtspr SPRN_SRR1,r4
  1032. SYNC
  1033. RFI
  1034. /*
  1035. * Set up the segment registers for a new context.
  1036. */
  1037. _GLOBAL(set_context)
  1038. mulli r3,r3,897 /* multiply context by skew factor */
  1039. rlwinm r3,r3,4,8,27 /* VSID = (context & 0xfffff) << 4 */
  1040. addis r3,r3,0x6000 /* Set Ks, Ku bits */
  1041. li r0,NUM_USER_SEGMENTS
  1042. mtctr r0
  1043. #ifdef CONFIG_BDI_SWITCH
  1044. /* Context switch the PTE pointer for the Abatron BDI2000.
  1045. * The PGDIR is passed as second argument.
  1046. */
  1047. lis r5, KERNELBASE@h
  1048. lwz r5, 0xf0(r5)
  1049. stw r4, 0x4(r5)
  1050. #endif
  1051. li r4,0
  1052. isync
  1053. 3:
  1054. mtsrin r3,r4
  1055. addi r3,r3,0x111 /* next VSID */
  1056. rlwinm r3,r3,0,8,3 /* clear out any overflow from VSID field */
  1057. addis r4,r4,0x1000 /* address of next segment */
  1058. bdnz 3b
  1059. sync
  1060. isync
  1061. blr
  1062. /*
  1063. * An undocumented "feature" of 604e requires that the v bit
  1064. * be cleared before changing BAT values.
  1065. *
  1066. * Also, newer IBM firmware does not clear bat3 and 4 so
  1067. * this makes sure it's done.
  1068. * -- Cort
  1069. */
  1070. clear_bats:
  1071. li r10,0
  1072. mfspr r9,SPRN_PVR
  1073. rlwinm r9,r9,16,16,31 /* r9 = 1 for 601, 4 for 604 */
  1074. cmpwi r9, 1
  1075. beq 1f
  1076. mtspr SPRN_DBAT0U,r10
  1077. mtspr SPRN_DBAT0L,r10
  1078. mtspr SPRN_DBAT1U,r10
  1079. mtspr SPRN_DBAT1L,r10
  1080. mtspr SPRN_DBAT2U,r10
  1081. mtspr SPRN_DBAT2L,r10
  1082. mtspr SPRN_DBAT3U,r10
  1083. mtspr SPRN_DBAT3L,r10
  1084. 1:
  1085. mtspr SPRN_IBAT0U,r10
  1086. mtspr SPRN_IBAT0L,r10
  1087. mtspr SPRN_IBAT1U,r10
  1088. mtspr SPRN_IBAT1L,r10
  1089. mtspr SPRN_IBAT2U,r10
  1090. mtspr SPRN_IBAT2L,r10
  1091. mtspr SPRN_IBAT3U,r10
  1092. mtspr SPRN_IBAT3L,r10
  1093. BEGIN_FTR_SECTION
  1094. /* Here's a tweak: at this point, CPU setup have
  1095. * not been called yet, so HIGH_BAT_EN may not be
  1096. * set in HID0 for the 745x processors. However, it
  1097. * seems that doesn't affect our ability to actually
  1098. * write to these SPRs.
  1099. */
  1100. mtspr SPRN_DBAT4U,r10
  1101. mtspr SPRN_DBAT4L,r10
  1102. mtspr SPRN_DBAT5U,r10
  1103. mtspr SPRN_DBAT5L,r10
  1104. mtspr SPRN_DBAT6U,r10
  1105. mtspr SPRN_DBAT6L,r10
  1106. mtspr SPRN_DBAT7U,r10
  1107. mtspr SPRN_DBAT7L,r10
  1108. mtspr SPRN_IBAT4U,r10
  1109. mtspr SPRN_IBAT4L,r10
  1110. mtspr SPRN_IBAT5U,r10
  1111. mtspr SPRN_IBAT5L,r10
  1112. mtspr SPRN_IBAT6U,r10
  1113. mtspr SPRN_IBAT6L,r10
  1114. mtspr SPRN_IBAT7U,r10
  1115. mtspr SPRN_IBAT7L,r10
  1116. END_FTR_SECTION_IFSET(CPU_FTR_HAS_HIGH_BATS)
  1117. blr
  1118. flush_tlbs:
  1119. lis r10, 0x40
  1120. 1: addic. r10, r10, -0x1000
  1121. tlbie r10
  1122. blt 1b
  1123. sync
  1124. blr
  1125. mmu_off:
  1126. addi r4, r3, __after_mmu_off - _start
  1127. mfmsr r3
  1128. andi. r0,r3,MSR_DR|MSR_IR /* MMU enabled? */
  1129. beqlr
  1130. andc r3,r3,r0
  1131. mtspr SPRN_SRR0,r4
  1132. mtspr SPRN_SRR1,r3
  1133. sync
  1134. RFI
  1135. /*
  1136. * Use the first pair of BAT registers to map the 1st 16MB
  1137. * of RAM to KERNELBASE. From this point on we can't safely
  1138. * call OF any more.
  1139. */
  1140. initial_bats:
  1141. lis r11,KERNELBASE@h
  1142. mfspr r9,SPRN_PVR
  1143. rlwinm r9,r9,16,16,31 /* r9 = 1 for 601, 4 for 604 */
  1144. cmpwi 0,r9,1
  1145. bne 4f
  1146. ori r11,r11,4 /* set up BAT registers for 601 */
  1147. li r8,0x7f /* valid, block length = 8MB */
  1148. oris r9,r11,0x800000@h /* set up BAT reg for 2nd 8M */
  1149. oris r10,r8,0x800000@h /* set up BAT reg for 2nd 8M */
  1150. mtspr SPRN_IBAT0U,r11 /* N.B. 601 has valid bit in */
  1151. mtspr SPRN_IBAT0L,r8 /* lower BAT register */
  1152. mtspr SPRN_IBAT1U,r9
  1153. mtspr SPRN_IBAT1L,r10
  1154. isync
  1155. blr
  1156. 4: tophys(r8,r11)
  1157. #ifdef CONFIG_SMP
  1158. ori r8,r8,0x12 /* R/W access, M=1 */
  1159. #else
  1160. ori r8,r8,2 /* R/W access */
  1161. #endif /* CONFIG_SMP */
  1162. #ifdef CONFIG_APUS
  1163. ori r11,r11,BL_8M<<2|0x2 /* set up 8MB BAT registers for 604 */
  1164. #else
  1165. ori r11,r11,BL_256M<<2|0x2 /* set up BAT registers for 604 */
  1166. #endif /* CONFIG_APUS */
  1167. mtspr SPRN_DBAT0L,r8 /* N.B. 6xx (not 601) have valid */
  1168. mtspr SPRN_DBAT0U,r11 /* bit in upper BAT register */
  1169. mtspr SPRN_IBAT0L,r8
  1170. mtspr SPRN_IBAT0U,r11
  1171. isync
  1172. blr
  1173. #if !defined(CONFIG_APUS) && defined(CONFIG_BOOTX_TEXT)
  1174. setup_disp_bat:
  1175. /*
  1176. * setup the display bat prepared for us in prom.c
  1177. */
  1178. mflr r8
  1179. bl reloc_offset
  1180. mtlr r8
  1181. addis r8,r3,disp_BAT@ha
  1182. addi r8,r8,disp_BAT@l
  1183. lwz r11,0(r8)
  1184. lwz r8,4(r8)
  1185. mfspr r9,SPRN_PVR
  1186. rlwinm r9,r9,16,16,31 /* r9 = 1 for 601, 4 for 604 */
  1187. cmpwi 0,r9,1
  1188. beq 1f
  1189. mtspr SPRN_DBAT3L,r8
  1190. mtspr SPRN_DBAT3U,r11
  1191. blr
  1192. 1: mtspr SPRN_IBAT3L,r8
  1193. mtspr SPRN_IBAT3U,r11
  1194. blr
  1195. #endif /* !defined(CONFIG_APUS) && defined(CONFIG_BOOTX_TEXT) */
  1196. #ifdef CONFIG_8260
  1197. /* Jump into the system reset for the rom.
  1198. * We first disable the MMU, and then jump to the ROM reset address.
  1199. *
  1200. * r3 is the board info structure, r4 is the location for starting.
  1201. * I use this for building a small kernel that can load other kernels,
  1202. * rather than trying to write or rely on a rom monitor that can tftp load.
  1203. */
  1204. .globl m8260_gorom
  1205. m8260_gorom:
  1206. mfmsr r0
  1207. rlwinm r0,r0,0,17,15 /* clear MSR_EE in r0 */
  1208. sync
  1209. mtmsr r0
  1210. sync
  1211. mfspr r11, SPRN_HID0
  1212. lis r10, 0
  1213. ori r10,r10,HID0_ICE|HID0_DCE
  1214. andc r11, r11, r10
  1215. mtspr SPRN_HID0, r11
  1216. isync
  1217. li r5, MSR_ME|MSR_RI
  1218. lis r6,2f@h
  1219. addis r6,r6,-KERNELBASE@h
  1220. ori r6,r6,2f@l
  1221. mtspr SPRN_SRR0,r6
  1222. mtspr SPRN_SRR1,r5
  1223. isync
  1224. sync
  1225. rfi
  1226. 2:
  1227. mtlr r4
  1228. blr
  1229. #endif
  1230. /*
  1231. * We put a few things here that have to be page-aligned.
  1232. * This stuff goes at the beginning of the data segment,
  1233. * which is page-aligned.
  1234. */
  1235. .data
  1236. .globl sdata
  1237. sdata:
  1238. .globl empty_zero_page
  1239. empty_zero_page:
  1240. .space 4096
  1241. .globl swapper_pg_dir
  1242. swapper_pg_dir:
  1243. .space 4096
  1244. /*
  1245. * This space gets a copy of optional info passed to us by the bootstrap
  1246. * Used to pass parameters into the kernel like root=/dev/sda1, etc.
  1247. */
  1248. .globl cmd_line
  1249. cmd_line:
  1250. .space 512
  1251. .globl intercept_table
  1252. intercept_table:
  1253. .long 0, 0, i0x200, i0x300, i0x400, 0, i0x600, i0x700
  1254. .long i0x800, 0, 0, 0, 0, i0xd00, 0, 0
  1255. .long 0, 0, 0, i0x1300, 0, 0, 0, 0
  1256. .long 0, 0, 0, 0, 0, 0, 0, 0
  1257. .long 0, 0, 0, 0, 0, 0, 0, 0
  1258. .long 0, 0, 0, 0, 0, 0, 0, 0
  1259. /* Room for two PTE pointers, usually the kernel and current user pointers
  1260. * to their respective root page table.
  1261. */
  1262. abatron_pteptrs:
  1263. .space 8