head.S 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. /*
  2. * OpenRISC head.S
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/linkage.h>
  18. #include <linux/threads.h>
  19. #include <linux/errno.h>
  20. #include <linux/init.h>
  21. #include <linux/serial_reg.h>
  22. #include <asm/processor.h>
  23. #include <asm/page.h>
  24. #include <asm/mmu.h>
  25. #include <asm/pgtable.h>
  26. #include <asm/cache.h>
  27. #include <asm/spr_defs.h>
  28. #include <asm/asm-offsets.h>
  29. #include <linux/of_fdt.h>
  30. #define tophys(rd,rs) \
  31. l.movhi rd,hi(-KERNELBASE) ;\
  32. l.add rd,rd,rs
  33. #define CLEAR_GPR(gpr) \
  34. l.or gpr,r0,r0
  35. #define LOAD_SYMBOL_2_GPR(gpr,symbol) \
  36. l.movhi gpr,hi(symbol) ;\
  37. l.ori gpr,gpr,lo(symbol)
  38. #define UART_BASE_ADD 0x90000000
  39. #define EXCEPTION_SR (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_SM)
  40. #define SYSCALL_SR (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_IEE | SPR_SR_TEE | SPR_SR_SM)
  41. /* ============================================[ tmp store locations ]=== */
  42. /*
  43. * emergency_print temporary stores
  44. */
  45. #define EMERGENCY_PRINT_STORE_GPR4 l.sw 0x20(r0),r4
  46. #define EMERGENCY_PRINT_LOAD_GPR4 l.lwz r4,0x20(r0)
  47. #define EMERGENCY_PRINT_STORE_GPR5 l.sw 0x24(r0),r5
  48. #define EMERGENCY_PRINT_LOAD_GPR5 l.lwz r5,0x24(r0)
  49. #define EMERGENCY_PRINT_STORE_GPR6 l.sw 0x28(r0),r6
  50. #define EMERGENCY_PRINT_LOAD_GPR6 l.lwz r6,0x28(r0)
  51. #define EMERGENCY_PRINT_STORE_GPR7 l.sw 0x2c(r0),r7
  52. #define EMERGENCY_PRINT_LOAD_GPR7 l.lwz r7,0x2c(r0)
  53. #define EMERGENCY_PRINT_STORE_GPR8 l.sw 0x30(r0),r8
  54. #define EMERGENCY_PRINT_LOAD_GPR8 l.lwz r8,0x30(r0)
  55. #define EMERGENCY_PRINT_STORE_GPR9 l.sw 0x34(r0),r9
  56. #define EMERGENCY_PRINT_LOAD_GPR9 l.lwz r9,0x34(r0)
  57. /*
  58. * TLB miss handlers temorary stores
  59. */
  60. #define EXCEPTION_STORE_GPR9 l.sw 0x10(r0),r9
  61. #define EXCEPTION_LOAD_GPR9 l.lwz r9,0x10(r0)
  62. #define EXCEPTION_STORE_GPR2 l.sw 0x64(r0),r2
  63. #define EXCEPTION_LOAD_GPR2 l.lwz r2,0x64(r0)
  64. #define EXCEPTION_STORE_GPR3 l.sw 0x68(r0),r3
  65. #define EXCEPTION_LOAD_GPR3 l.lwz r3,0x68(r0)
  66. #define EXCEPTION_STORE_GPR4 l.sw 0x6c(r0),r4
  67. #define EXCEPTION_LOAD_GPR4 l.lwz r4,0x6c(r0)
  68. #define EXCEPTION_STORE_GPR5 l.sw 0x70(r0),r5
  69. #define EXCEPTION_LOAD_GPR5 l.lwz r5,0x70(r0)
  70. #define EXCEPTION_STORE_GPR6 l.sw 0x74(r0),r6
  71. #define EXCEPTION_LOAD_GPR6 l.lwz r6,0x74(r0)
  72. /*
  73. * EXCEPTION_HANDLE temporary stores
  74. */
  75. #define EXCEPTION_T_STORE_GPR30 l.sw 0x78(r0),r30
  76. #define EXCEPTION_T_LOAD_GPR30(reg) l.lwz reg,0x78(r0)
  77. #define EXCEPTION_T_STORE_GPR10 l.sw 0x7c(r0),r10
  78. #define EXCEPTION_T_LOAD_GPR10(reg) l.lwz reg,0x7c(r0)
  79. #define EXCEPTION_T_STORE_SP l.sw 0x80(r0),r1
  80. #define EXCEPTION_T_LOAD_SP(reg) l.lwz reg,0x80(r0)
  81. /*
  82. * For UNHANLDED_EXCEPTION
  83. */
  84. #define EXCEPTION_T_STORE_GPR31 l.sw 0x84(r0),r31
  85. #define EXCEPTION_T_LOAD_GPR31(reg) l.lwz reg,0x84(r0)
  86. /* =========================================================[ macros ]=== */
  87. #define GET_CURRENT_PGD(reg,t1) \
  88. LOAD_SYMBOL_2_GPR(reg,current_pgd) ;\
  89. tophys (t1,reg) ;\
  90. l.lwz reg,0(t1)
  91. /*
  92. * DSCR: this is a common hook for handling exceptions. it will save
  93. * the needed registers, set up stack and pointer to current
  94. * then jump to the handler while enabling MMU
  95. *
  96. * PRMS: handler - a function to jump to. it has to save the
  97. * remaining registers to kernel stack, call
  98. * appropriate arch-independant exception handler
  99. * and finaly jump to ret_from_except
  100. *
  101. * PREQ: unchanged state from the time exception happened
  102. *
  103. * POST: SAVED the following registers original value
  104. * to the new created exception frame pointed to by r1
  105. *
  106. * r1 - ksp pointing to the new (exception) frame
  107. * r4 - EEAR exception EA
  108. * r10 - current pointing to current_thread_info struct
  109. * r12 - syscall 0, since we didn't come from syscall
  110. * r13 - temp it actually contains new SR, not needed anymore
  111. * r31 - handler address of the handler we'll jump to
  112. *
  113. * handler has to save remaining registers to the exception
  114. * ksp frame *before* tainting them!
  115. *
  116. * NOTE: this function is not reentrant per se. reentrancy is guaranteed
  117. * by processor disabling all exceptions/interrupts when exception
  118. * accours.
  119. *
  120. * OPTM: no need to make it so wasteful to extract ksp when in user mode
  121. */
  122. #define EXCEPTION_HANDLE(handler) \
  123. EXCEPTION_T_STORE_GPR30 ;\
  124. l.mfspr r30,r0,SPR_ESR_BASE ;\
  125. l.andi r30,r30,SPR_SR_SM ;\
  126. l.sfeqi r30,0 ;\
  127. EXCEPTION_T_STORE_GPR10 ;\
  128. l.bnf 2f /* kernel_mode */ ;\
  129. EXCEPTION_T_STORE_SP /* delay slot */ ;\
  130. 1: /* user_mode: */ ;\
  131. LOAD_SYMBOL_2_GPR(r1,current_thread_info_set) ;\
  132. tophys (r30,r1) ;\
  133. /* r10: current_thread_info */ ;\
  134. l.lwz r10,0(r30) ;\
  135. tophys (r30,r10) ;\
  136. l.lwz r1,(TI_KSP)(r30) ;\
  137. /* fall through */ ;\
  138. 2: /* kernel_mode: */ ;\
  139. /* create new stack frame, save only needed gprs */ ;\
  140. /* r1: KSP, r10: current, r4: EEAR, r31: __pa(KSP) */ ;\
  141. /* r12: temp, syscall indicator */ ;\
  142. l.addi r1,r1,-(INT_FRAME_SIZE) ;\
  143. /* r1 is KSP, r30 is __pa(KSP) */ ;\
  144. tophys (r30,r1) ;\
  145. l.sw PT_GPR12(r30),r12 ;\
  146. l.mfspr r12,r0,SPR_EPCR_BASE ;\
  147. l.sw PT_PC(r30),r12 ;\
  148. l.mfspr r12,r0,SPR_ESR_BASE ;\
  149. l.sw PT_SR(r30),r12 ;\
  150. /* save r30 */ ;\
  151. EXCEPTION_T_LOAD_GPR30(r12) ;\
  152. l.sw PT_GPR30(r30),r12 ;\
  153. /* save r10 as was prior to exception */ ;\
  154. EXCEPTION_T_LOAD_GPR10(r12) ;\
  155. l.sw PT_GPR10(r30),r12 ;\
  156. /* save PT_SP as was prior to exception */ ;\
  157. EXCEPTION_T_LOAD_SP(r12) ;\
  158. l.sw PT_SP(r30),r12 ;\
  159. /* save exception r4, set r4 = EA */ ;\
  160. l.sw PT_GPR4(r30),r4 ;\
  161. l.mfspr r4,r0,SPR_EEAR_BASE ;\
  162. /* r12 == 1 if we come from syscall */ ;\
  163. CLEAR_GPR(r12) ;\
  164. /* ----- turn on MMU ----- */ ;\
  165. l.ori r30,r0,(EXCEPTION_SR) ;\
  166. l.mtspr r0,r30,SPR_ESR_BASE ;\
  167. /* r30: EA address of handler */ ;\
  168. LOAD_SYMBOL_2_GPR(r30,handler) ;\
  169. l.mtspr r0,r30,SPR_EPCR_BASE ;\
  170. l.rfe
  171. /*
  172. * this doesn't work
  173. *
  174. *
  175. * #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
  176. * #define UNHANDLED_EXCEPTION(handler) \
  177. * l.ori r3,r0,0x1 ;\
  178. * l.mtspr r0,r3,SPR_SR ;\
  179. * l.movhi r3,hi(0xf0000100) ;\
  180. * l.ori r3,r3,lo(0xf0000100) ;\
  181. * l.jr r3 ;\
  182. * l.nop 1
  183. *
  184. * #endif
  185. */
  186. /* DSCR: this is the same as EXCEPTION_HANDLE(), we are just
  187. * a bit more carefull (if we have a PT_SP or current pointer
  188. * corruption) and set them up from 'current_set'
  189. *
  190. */
  191. #define UNHANDLED_EXCEPTION(handler) \
  192. EXCEPTION_T_STORE_GPR31 ;\
  193. EXCEPTION_T_STORE_GPR10 ;\
  194. EXCEPTION_T_STORE_SP ;\
  195. /* temporary store r3, r9 into r1, r10 */ ;\
  196. l.addi r1,r3,0x0 ;\
  197. l.addi r10,r9,0x0 ;\
  198. /* the string referenced by r3 must be low enough */ ;\
  199. l.jal _emergency_print ;\
  200. l.ori r3,r0,lo(_string_unhandled_exception) ;\
  201. l.mfspr r3,r0,SPR_NPC ;\
  202. l.jal _emergency_print_nr ;\
  203. l.andi r3,r3,0x1f00 ;\
  204. /* the string referenced by r3 must be low enough */ ;\
  205. l.jal _emergency_print ;\
  206. l.ori r3,r0,lo(_string_epc_prefix) ;\
  207. l.jal _emergency_print_nr ;\
  208. l.mfspr r3,r0,SPR_EPCR_BASE ;\
  209. l.jal _emergency_print ;\
  210. l.ori r3,r0,lo(_string_nl) ;\
  211. /* end of printing */ ;\
  212. l.addi r3,r1,0x0 ;\
  213. l.addi r9,r10,0x0 ;\
  214. /* extract current, ksp from current_set */ ;\
  215. LOAD_SYMBOL_2_GPR(r1,_unhandled_stack_top) ;\
  216. LOAD_SYMBOL_2_GPR(r10,init_thread_union) ;\
  217. /* create new stack frame, save only needed gprs */ ;\
  218. /* r1: KSP, r10: current, r31: __pa(KSP) */ ;\
  219. /* r12: temp, syscall indicator, r13 temp */ ;\
  220. l.addi r1,r1,-(INT_FRAME_SIZE) ;\
  221. /* r1 is KSP, r31 is __pa(KSP) */ ;\
  222. tophys (r31,r1) ;\
  223. l.sw PT_GPR12(r31),r12 ;\
  224. l.mfspr r12,r0,SPR_EPCR_BASE ;\
  225. l.sw PT_PC(r31),r12 ;\
  226. l.mfspr r12,r0,SPR_ESR_BASE ;\
  227. l.sw PT_SR(r31),r12 ;\
  228. /* save r31 */ ;\
  229. EXCEPTION_T_LOAD_GPR31(r12) ;\
  230. l.sw PT_GPR31(r31),r12 ;\
  231. /* save r10 as was prior to exception */ ;\
  232. EXCEPTION_T_LOAD_GPR10(r12) ;\
  233. l.sw PT_GPR10(r31),r12 ;\
  234. /* save PT_SP as was prior to exception */ ;\
  235. EXCEPTION_T_LOAD_SP(r12) ;\
  236. l.sw PT_SP(r31),r12 ;\
  237. l.sw PT_GPR13(r31),r13 ;\
  238. /* --> */ ;\
  239. /* save exception r4, set r4 = EA */ ;\
  240. l.sw PT_GPR4(r31),r4 ;\
  241. l.mfspr r4,r0,SPR_EEAR_BASE ;\
  242. /* r12 == 1 if we come from syscall */ ;\
  243. CLEAR_GPR(r12) ;\
  244. /* ----- play a MMU trick ----- */ ;\
  245. l.ori r31,r0,(EXCEPTION_SR) ;\
  246. l.mtspr r0,r31,SPR_ESR_BASE ;\
  247. /* r31: EA address of handler */ ;\
  248. LOAD_SYMBOL_2_GPR(r31,handler) ;\
  249. l.mtspr r0,r31,SPR_EPCR_BASE ;\
  250. l.rfe
  251. /* =====================================================[ exceptions] === */
  252. /* ---[ 0x100: RESET exception ]----------------------------------------- */
  253. .org 0x100
  254. /* Jump to .init code at _start which lives in the .head section
  255. * and will be discarded after boot.
  256. */
  257. LOAD_SYMBOL_2_GPR(r4, _start)
  258. tophys (r3,r4) /* MMU disabled */
  259. l.jr r3
  260. l.nop
  261. /* ---[ 0x200: BUS exception ]------------------------------------------- */
  262. .org 0x200
  263. _dispatch_bus_fault:
  264. EXCEPTION_HANDLE(_bus_fault_handler)
  265. /* ---[ 0x300: Data Page Fault exception ]------------------------------- */
  266. .org 0x300
  267. _dispatch_do_dpage_fault:
  268. // totaly disable timer interrupt
  269. // l.mtspr r0,r0,SPR_TTMR
  270. // DEBUG_TLB_PROBE(0x300)
  271. // EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x300)
  272. EXCEPTION_HANDLE(_data_page_fault_handler)
  273. /* ---[ 0x400: Insn Page Fault exception ]------------------------------- */
  274. .org 0x400
  275. _dispatch_do_ipage_fault:
  276. // totaly disable timer interrupt
  277. // l.mtspr r0,r0,SPR_TTMR
  278. // DEBUG_TLB_PROBE(0x400)
  279. // EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x400)
  280. EXCEPTION_HANDLE(_insn_page_fault_handler)
  281. /* ---[ 0x500: Timer exception ]----------------------------------------- */
  282. .org 0x500
  283. EXCEPTION_HANDLE(_timer_handler)
  284. /* ---[ 0x600: Aligment exception ]-------------------------------------- */
  285. .org 0x600
  286. EXCEPTION_HANDLE(_alignment_handler)
  287. /* ---[ 0x700: Illegal insn exception ]---------------------------------- */
  288. .org 0x700
  289. EXCEPTION_HANDLE(_illegal_instruction_handler)
  290. /* ---[ 0x800: External interrupt exception ]---------------------------- */
  291. .org 0x800
  292. EXCEPTION_HANDLE(_external_irq_handler)
  293. /* ---[ 0x900: DTLB miss exception ]------------------------------------- */
  294. .org 0x900
  295. l.j boot_dtlb_miss_handler
  296. l.nop
  297. /* ---[ 0xa00: ITLB miss exception ]------------------------------------- */
  298. .org 0xa00
  299. l.j boot_itlb_miss_handler
  300. l.nop
  301. /* ---[ 0xb00: Range exception ]----------------------------------------- */
  302. .org 0xb00
  303. UNHANDLED_EXCEPTION(_vector_0xb00)
  304. /* ---[ 0xc00: Syscall exception ]--------------------------------------- */
  305. .org 0xc00
  306. EXCEPTION_HANDLE(_sys_call_handler)
  307. /* ---[ 0xd00: Trap exception ]------------------------------------------ */
  308. .org 0xd00
  309. UNHANDLED_EXCEPTION(_vector_0xd00)
  310. /* ---[ 0xe00: Trap exception ]------------------------------------------ */
  311. .org 0xe00
  312. // UNHANDLED_EXCEPTION(_vector_0xe00)
  313. EXCEPTION_HANDLE(_trap_handler)
  314. /* ---[ 0xf00: Reserved exception ]-------------------------------------- */
  315. .org 0xf00
  316. UNHANDLED_EXCEPTION(_vector_0xf00)
  317. /* ---[ 0x1000: Reserved exception ]------------------------------------- */
  318. .org 0x1000
  319. UNHANDLED_EXCEPTION(_vector_0x1000)
  320. /* ---[ 0x1100: Reserved exception ]------------------------------------- */
  321. .org 0x1100
  322. UNHANDLED_EXCEPTION(_vector_0x1100)
  323. /* ---[ 0x1200: Reserved exception ]------------------------------------- */
  324. .org 0x1200
  325. UNHANDLED_EXCEPTION(_vector_0x1200)
  326. /* ---[ 0x1300: Reserved exception ]------------------------------------- */
  327. .org 0x1300
  328. UNHANDLED_EXCEPTION(_vector_0x1300)
  329. /* ---[ 0x1400: Reserved exception ]------------------------------------- */
  330. .org 0x1400
  331. UNHANDLED_EXCEPTION(_vector_0x1400)
  332. /* ---[ 0x1500: Reserved exception ]------------------------------------- */
  333. .org 0x1500
  334. UNHANDLED_EXCEPTION(_vector_0x1500)
  335. /* ---[ 0x1600: Reserved exception ]------------------------------------- */
  336. .org 0x1600
  337. UNHANDLED_EXCEPTION(_vector_0x1600)
  338. /* ---[ 0x1700: Reserved exception ]------------------------------------- */
  339. .org 0x1700
  340. UNHANDLED_EXCEPTION(_vector_0x1700)
  341. /* ---[ 0x1800: Reserved exception ]------------------------------------- */
  342. .org 0x1800
  343. UNHANDLED_EXCEPTION(_vector_0x1800)
  344. /* ---[ 0x1900: Reserved exception ]------------------------------------- */
  345. .org 0x1900
  346. UNHANDLED_EXCEPTION(_vector_0x1900)
  347. /* ---[ 0x1a00: Reserved exception ]------------------------------------- */
  348. .org 0x1a00
  349. UNHANDLED_EXCEPTION(_vector_0x1a00)
  350. /* ---[ 0x1b00: Reserved exception ]------------------------------------- */
  351. .org 0x1b00
  352. UNHANDLED_EXCEPTION(_vector_0x1b00)
  353. /* ---[ 0x1c00: Reserved exception ]------------------------------------- */
  354. .org 0x1c00
  355. UNHANDLED_EXCEPTION(_vector_0x1c00)
  356. /* ---[ 0x1d00: Reserved exception ]------------------------------------- */
  357. .org 0x1d00
  358. UNHANDLED_EXCEPTION(_vector_0x1d00)
  359. /* ---[ 0x1e00: Reserved exception ]------------------------------------- */
  360. .org 0x1e00
  361. UNHANDLED_EXCEPTION(_vector_0x1e00)
  362. /* ---[ 0x1f00: Reserved exception ]------------------------------------- */
  363. .org 0x1f00
  364. UNHANDLED_EXCEPTION(_vector_0x1f00)
  365. .org 0x2000
  366. /* ===================================================[ kernel start ]=== */
  367. /* .text*/
  368. /* This early stuff belongs in HEAD, but some of the functions below definitely
  369. * don't... */
  370. __HEAD
  371. .global _start
  372. _start:
  373. /* save kernel parameters */
  374. l.or r25,r0,r3 /* pointer to fdt */
  375. /*
  376. * ensure a deterministic start
  377. */
  378. l.ori r3,r0,0x1
  379. l.mtspr r0,r3,SPR_SR
  380. CLEAR_GPR(r1)
  381. CLEAR_GPR(r2)
  382. CLEAR_GPR(r3)
  383. CLEAR_GPR(r4)
  384. CLEAR_GPR(r5)
  385. CLEAR_GPR(r6)
  386. CLEAR_GPR(r7)
  387. CLEAR_GPR(r8)
  388. CLEAR_GPR(r9)
  389. CLEAR_GPR(r10)
  390. CLEAR_GPR(r11)
  391. CLEAR_GPR(r12)
  392. CLEAR_GPR(r13)
  393. CLEAR_GPR(r14)
  394. CLEAR_GPR(r15)
  395. CLEAR_GPR(r16)
  396. CLEAR_GPR(r17)
  397. CLEAR_GPR(r18)
  398. CLEAR_GPR(r19)
  399. CLEAR_GPR(r20)
  400. CLEAR_GPR(r21)
  401. CLEAR_GPR(r22)
  402. CLEAR_GPR(r23)
  403. CLEAR_GPR(r24)
  404. CLEAR_GPR(r26)
  405. CLEAR_GPR(r27)
  406. CLEAR_GPR(r28)
  407. CLEAR_GPR(r29)
  408. CLEAR_GPR(r30)
  409. CLEAR_GPR(r31)
  410. /*
  411. * set up initial ksp and current
  412. */
  413. LOAD_SYMBOL_2_GPR(r1,init_thread_union+0x2000) // setup kernel stack
  414. LOAD_SYMBOL_2_GPR(r10,init_thread_union) // setup current
  415. tophys (r31,r10)
  416. l.sw TI_KSP(r31), r1
  417. l.ori r4,r0,0x0
  418. /*
  419. * .data contains initialized data,
  420. * .bss contains uninitialized data - clear it up
  421. */
  422. clear_bss:
  423. LOAD_SYMBOL_2_GPR(r24, __bss_start)
  424. LOAD_SYMBOL_2_GPR(r26, _end)
  425. tophys(r28,r24)
  426. tophys(r30,r26)
  427. CLEAR_GPR(r24)
  428. CLEAR_GPR(r26)
  429. 1:
  430. l.sw (0)(r28),r0
  431. l.sfltu r28,r30
  432. l.bf 1b
  433. l.addi r28,r28,4
  434. enable_ic:
  435. l.jal _ic_enable
  436. l.nop
  437. enable_dc:
  438. l.jal _dc_enable
  439. l.nop
  440. flush_tlb:
  441. /*
  442. * I N V A L I D A T E T L B e n t r i e s
  443. */
  444. LOAD_SYMBOL_2_GPR(r5,SPR_DTLBMR_BASE(0))
  445. LOAD_SYMBOL_2_GPR(r6,SPR_ITLBMR_BASE(0))
  446. l.addi r7,r0,128 /* Maximum number of sets */
  447. 1:
  448. l.mtspr r5,r0,0x0
  449. l.mtspr r6,r0,0x0
  450. l.addi r5,r5,1
  451. l.addi r6,r6,1
  452. l.sfeq r7,r0
  453. l.bnf 1b
  454. l.addi r7,r7,-1
  455. /* The MMU needs to be enabled before or32_early_setup is called */
  456. enable_mmu:
  457. /*
  458. * enable dmmu & immu
  459. * SR[5] = 0, SR[6] = 0, 6th and 7th bit of SR set to 0
  460. */
  461. l.mfspr r30,r0,SPR_SR
  462. l.movhi r28,hi(SPR_SR_DME | SPR_SR_IME)
  463. l.ori r28,r28,lo(SPR_SR_DME | SPR_SR_IME)
  464. l.or r30,r30,r28
  465. l.mtspr r0,r30,SPR_SR
  466. l.nop
  467. l.nop
  468. l.nop
  469. l.nop
  470. l.nop
  471. l.nop
  472. l.nop
  473. l.nop
  474. l.nop
  475. l.nop
  476. l.nop
  477. l.nop
  478. l.nop
  479. l.nop
  480. l.nop
  481. l.nop
  482. // reset the simulation counters
  483. l.nop 5
  484. /* check fdt header magic word */
  485. l.lwz r3,0(r25) /* load magic from fdt into r3 */
  486. l.movhi r4,hi(OF_DT_HEADER)
  487. l.ori r4,r4,lo(OF_DT_HEADER)
  488. l.sfeq r3,r4
  489. l.bf _fdt_found
  490. l.nop
  491. /* magic number mismatch, set fdt pointer to null */
  492. l.or r25,r0,r0
  493. _fdt_found:
  494. /* pass fdt pointer to or32_early_setup in r3 */
  495. l.or r3,r0,r25
  496. LOAD_SYMBOL_2_GPR(r24, or32_early_setup)
  497. l.jalr r24
  498. l.nop
  499. clear_regs:
  500. /*
  501. * clear all GPRS to increase determinism
  502. */
  503. CLEAR_GPR(r2)
  504. CLEAR_GPR(r3)
  505. CLEAR_GPR(r4)
  506. CLEAR_GPR(r5)
  507. CLEAR_GPR(r6)
  508. CLEAR_GPR(r7)
  509. CLEAR_GPR(r8)
  510. CLEAR_GPR(r9)
  511. CLEAR_GPR(r11)
  512. CLEAR_GPR(r12)
  513. CLEAR_GPR(r13)
  514. CLEAR_GPR(r14)
  515. CLEAR_GPR(r15)
  516. CLEAR_GPR(r16)
  517. CLEAR_GPR(r17)
  518. CLEAR_GPR(r18)
  519. CLEAR_GPR(r19)
  520. CLEAR_GPR(r20)
  521. CLEAR_GPR(r21)
  522. CLEAR_GPR(r22)
  523. CLEAR_GPR(r23)
  524. CLEAR_GPR(r24)
  525. CLEAR_GPR(r25)
  526. CLEAR_GPR(r26)
  527. CLEAR_GPR(r27)
  528. CLEAR_GPR(r28)
  529. CLEAR_GPR(r29)
  530. CLEAR_GPR(r30)
  531. CLEAR_GPR(r31)
  532. jump_start_kernel:
  533. /*
  534. * jump to kernel entry (start_kernel)
  535. */
  536. LOAD_SYMBOL_2_GPR(r30, start_kernel)
  537. l.jr r30
  538. l.nop
  539. /* ========================================[ cache ]=== */
  540. /* aligment here so we don't change memory offsets with
  541. * memory controler defined
  542. */
  543. .align 0x2000
  544. _ic_enable:
  545. /* Check if IC present and skip enabling otherwise */
  546. l.mfspr r24,r0,SPR_UPR
  547. l.andi r26,r24,SPR_UPR_ICP
  548. l.sfeq r26,r0
  549. l.bf 9f
  550. l.nop
  551. /* Disable IC */
  552. l.mfspr r6,r0,SPR_SR
  553. l.addi r5,r0,-1
  554. l.xori r5,r5,SPR_SR_ICE
  555. l.and r5,r6,r5
  556. l.mtspr r0,r5,SPR_SR
  557. /* Establish cache block size
  558. If BS=0, 16;
  559. If BS=1, 32;
  560. r14 contain block size
  561. */
  562. l.mfspr r24,r0,SPR_ICCFGR
  563. l.andi r26,r24,SPR_ICCFGR_CBS
  564. l.srli r28,r26,7
  565. l.ori r30,r0,16
  566. l.sll r14,r30,r28
  567. /* Establish number of cache sets
  568. r16 contains number of cache sets
  569. r28 contains log(# of cache sets)
  570. */
  571. l.andi r26,r24,SPR_ICCFGR_NCS
  572. l.srli r28,r26,3
  573. l.ori r30,r0,1
  574. l.sll r16,r30,r28
  575. /* Invalidate IC */
  576. l.addi r6,r0,0
  577. l.sll r5,r14,r28
  578. // l.mul r5,r14,r16
  579. // l.trap 1
  580. // l.addi r5,r0,IC_SIZE
  581. 1:
  582. l.mtspr r0,r6,SPR_ICBIR
  583. l.sfne r6,r5
  584. l.bf 1b
  585. l.add r6,r6,r14
  586. // l.addi r6,r6,IC_LINE
  587. /* Enable IC */
  588. l.mfspr r6,r0,SPR_SR
  589. l.ori r6,r6,SPR_SR_ICE
  590. l.mtspr r0,r6,SPR_SR
  591. l.nop
  592. l.nop
  593. l.nop
  594. l.nop
  595. l.nop
  596. l.nop
  597. l.nop
  598. l.nop
  599. l.nop
  600. l.nop
  601. 9:
  602. l.jr r9
  603. l.nop
  604. _dc_enable:
  605. /* Check if DC present and skip enabling otherwise */
  606. l.mfspr r24,r0,SPR_UPR
  607. l.andi r26,r24,SPR_UPR_DCP
  608. l.sfeq r26,r0
  609. l.bf 9f
  610. l.nop
  611. /* Disable DC */
  612. l.mfspr r6,r0,SPR_SR
  613. l.addi r5,r0,-1
  614. l.xori r5,r5,SPR_SR_DCE
  615. l.and r5,r6,r5
  616. l.mtspr r0,r5,SPR_SR
  617. /* Establish cache block size
  618. If BS=0, 16;
  619. If BS=1, 32;
  620. r14 contain block size
  621. */
  622. l.mfspr r24,r0,SPR_DCCFGR
  623. l.andi r26,r24,SPR_DCCFGR_CBS
  624. l.srli r28,r26,7
  625. l.ori r30,r0,16
  626. l.sll r14,r30,r28
  627. /* Establish number of cache sets
  628. r16 contains number of cache sets
  629. r28 contains log(# of cache sets)
  630. */
  631. l.andi r26,r24,SPR_DCCFGR_NCS
  632. l.srli r28,r26,3
  633. l.ori r30,r0,1
  634. l.sll r16,r30,r28
  635. /* Invalidate DC */
  636. l.addi r6,r0,0
  637. l.sll r5,r14,r28
  638. 1:
  639. l.mtspr r0,r6,SPR_DCBIR
  640. l.sfne r6,r5
  641. l.bf 1b
  642. l.add r6,r6,r14
  643. /* Enable DC */
  644. l.mfspr r6,r0,SPR_SR
  645. l.ori r6,r6,SPR_SR_DCE
  646. l.mtspr r0,r6,SPR_SR
  647. 9:
  648. l.jr r9
  649. l.nop
  650. /* ===============================================[ page table masks ]=== */
  651. /* bit 4 is used in hardware as write back cache bit. we never use this bit
  652. * explicitly, so we can reuse it as _PAGE_FILE bit and mask it out when
  653. * writing into hardware pte's
  654. */
  655. #define DTLB_UP_CONVERT_MASK 0x3fa
  656. #define ITLB_UP_CONVERT_MASK 0x3a
  657. /* for SMP we'd have (this is a bit subtle, CC must be always set
  658. * for SMP, but since we have _PAGE_PRESENT bit always defined
  659. * we can just modify the mask)
  660. */
  661. #define DTLB_SMP_CONVERT_MASK 0x3fb
  662. #define ITLB_SMP_CONVERT_MASK 0x3b
  663. /* ---[ boot dtlb miss handler ]----------------------------------------- */
  664. boot_dtlb_miss_handler:
  665. /* mask for DTLB_MR register: - (0) sets V (valid) bit,
  666. * - (31-12) sets bits belonging to VPN (31-12)
  667. */
  668. #define DTLB_MR_MASK 0xfffff001
  669. /* mask for DTLB_TR register: - (2) sets CI (cache inhibit) bit,
  670. * - (4) sets A (access) bit,
  671. * - (5) sets D (dirty) bit,
  672. * - (8) sets SRE (superuser read) bit
  673. * - (9) sets SWE (superuser write) bit
  674. * - (31-12) sets bits belonging to VPN (31-12)
  675. */
  676. #define DTLB_TR_MASK 0xfffff332
  677. /* These are for masking out the VPN/PPN value from the MR/TR registers...
  678. * it's not the same as the PFN */
  679. #define VPN_MASK 0xfffff000
  680. #define PPN_MASK 0xfffff000
  681. EXCEPTION_STORE_GPR6
  682. #if 0
  683. l.mfspr r6,r0,SPR_ESR_BASE //
  684. l.andi r6,r6,SPR_SR_SM // are we in kernel mode ?
  685. l.sfeqi r6,0 // r6 == 0x1 --> SM
  686. l.bf exit_with_no_dtranslation //
  687. l.nop
  688. #endif
  689. /* this could be optimized by moving storing of
  690. * non r6 registers here, and jumping r6 restore
  691. * if not in supervisor mode
  692. */
  693. EXCEPTION_STORE_GPR2
  694. EXCEPTION_STORE_GPR3
  695. EXCEPTION_STORE_GPR4
  696. EXCEPTION_STORE_GPR5
  697. l.mfspr r4,r0,SPR_EEAR_BASE // get the offending EA
  698. immediate_translation:
  699. CLEAR_GPR(r6)
  700. l.srli r3,r4,0xd // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
  701. l.mfspr r6, r0, SPR_DMMUCFGR
  702. l.andi r6, r6, SPR_DMMUCFGR_NTS
  703. l.srli r6, r6, SPR_DMMUCFGR_NTS_OFF
  704. l.ori r5, r0, 0x1
  705. l.sll r5, r5, r6 // r5 = number DMMU sets
  706. l.addi r6, r5, -1 // r6 = nsets mask
  707. l.and r2, r3, r6 // r2 <- r3 % NSETS_MASK
  708. l.or r6,r6,r4 // r6 <- r4
  709. l.ori r6,r6,~(VPN_MASK) // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
  710. l.movhi r5,hi(DTLB_MR_MASK) // r5 <- ffff:0000.x000
  711. l.ori r5,r5,lo(DTLB_MR_MASK) // r5 <- ffff:1111.x001 - apply DTLB_MR_MASK
  712. l.and r5,r5,r6 // r5 <- VPN :VPN .x001 - we have DTLBMR entry
  713. l.mtspr r2,r5,SPR_DTLBMR_BASE(0) // set DTLBMR
  714. /* set up DTLB with no translation for EA <= 0xbfffffff */
  715. LOAD_SYMBOL_2_GPR(r6,0xbfffffff)
  716. l.sfgeu r6,r4 // flag if r6 >= r4 (if 0xbfffffff >= EA)
  717. l.bf 1f // goto out
  718. l.and r3,r4,r4 // delay slot :: 24 <- r4 (if flag==1)
  719. tophys(r3,r4) // r3 <- PA
  720. 1:
  721. l.ori r3,r3,~(PPN_MASK) // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
  722. l.movhi r5,hi(DTLB_TR_MASK) // r5 <- ffff:0000.x000
  723. l.ori r5,r5,lo(DTLB_TR_MASK) // r5 <- ffff:1111.x330 - apply DTLB_MR_MASK
  724. l.and r5,r5,r3 // r5 <- PPN :PPN .x330 - we have DTLBTR entry
  725. l.mtspr r2,r5,SPR_DTLBTR_BASE(0) // set DTLBTR
  726. EXCEPTION_LOAD_GPR6
  727. EXCEPTION_LOAD_GPR5
  728. EXCEPTION_LOAD_GPR4
  729. EXCEPTION_LOAD_GPR3
  730. EXCEPTION_LOAD_GPR2
  731. l.rfe // SR <- ESR, PC <- EPC
  732. exit_with_no_dtranslation:
  733. /* EA out of memory or not in supervisor mode */
  734. EXCEPTION_LOAD_GPR6
  735. EXCEPTION_LOAD_GPR4
  736. l.j _dispatch_bus_fault
  737. /* ---[ boot itlb miss handler ]----------------------------------------- */
  738. boot_itlb_miss_handler:
  739. /* mask for ITLB_MR register: - sets V (valid) bit,
  740. * - sets bits belonging to VPN (15-12)
  741. */
  742. #define ITLB_MR_MASK 0xfffff001
  743. /* mask for ITLB_TR register: - sets A (access) bit,
  744. * - sets SXE (superuser execute) bit
  745. * - sets bits belonging to VPN (15-12)
  746. */
  747. #define ITLB_TR_MASK 0xfffff050
  748. /*
  749. #define VPN_MASK 0xffffe000
  750. #define PPN_MASK 0xffffe000
  751. */
  752. EXCEPTION_STORE_GPR2
  753. EXCEPTION_STORE_GPR3
  754. EXCEPTION_STORE_GPR4
  755. EXCEPTION_STORE_GPR5
  756. EXCEPTION_STORE_GPR6
  757. #if 0
  758. l.mfspr r6,r0,SPR_ESR_BASE //
  759. l.andi r6,r6,SPR_SR_SM // are we in kernel mode ?
  760. l.sfeqi r6,0 // r6 == 0x1 --> SM
  761. l.bf exit_with_no_itranslation
  762. l.nop
  763. #endif
  764. l.mfspr r4,r0,SPR_EEAR_BASE // get the offending EA
  765. earlyearly:
  766. CLEAR_GPR(r6)
  767. l.srli r3,r4,0xd // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
  768. l.mfspr r6, r0, SPR_IMMUCFGR
  769. l.andi r6, r6, SPR_IMMUCFGR_NTS
  770. l.srli r6, r6, SPR_IMMUCFGR_NTS_OFF
  771. l.ori r5, r0, 0x1
  772. l.sll r5, r5, r6 // r5 = number IMMU sets from IMMUCFGR
  773. l.addi r6, r5, -1 // r6 = nsets mask
  774. l.and r2, r3, r6 // r2 <- r3 % NSETS_MASK
  775. l.or r6,r6,r4 // r6 <- r4
  776. l.ori r6,r6,~(VPN_MASK) // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
  777. l.movhi r5,hi(ITLB_MR_MASK) // r5 <- ffff:0000.x000
  778. l.ori r5,r5,lo(ITLB_MR_MASK) // r5 <- ffff:1111.x001 - apply ITLB_MR_MASK
  779. l.and r5,r5,r6 // r5 <- VPN :VPN .x001 - we have ITLBMR entry
  780. l.mtspr r2,r5,SPR_ITLBMR_BASE(0) // set ITLBMR
  781. /*
  782. * set up ITLB with no translation for EA <= 0x0fffffff
  783. *
  784. * we need this for head.S mapping (EA = PA). if we move all functions
  785. * which run with mmu enabled into entry.S, we might be able to eliminate this.
  786. *
  787. */
  788. LOAD_SYMBOL_2_GPR(r6,0x0fffffff)
  789. l.sfgeu r6,r4 // flag if r6 >= r4 (if 0xb0ffffff >= EA)
  790. l.bf 1f // goto out
  791. l.and r3,r4,r4 // delay slot :: 24 <- r4 (if flag==1)
  792. tophys(r3,r4) // r3 <- PA
  793. 1:
  794. l.ori r3,r3,~(PPN_MASK) // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
  795. l.movhi r5,hi(ITLB_TR_MASK) // r5 <- ffff:0000.x000
  796. l.ori r5,r5,lo(ITLB_TR_MASK) // r5 <- ffff:1111.x050 - apply ITLB_MR_MASK
  797. l.and r5,r5,r3 // r5 <- PPN :PPN .x050 - we have ITLBTR entry
  798. l.mtspr r2,r5,SPR_ITLBTR_BASE(0) // set ITLBTR
  799. EXCEPTION_LOAD_GPR6
  800. EXCEPTION_LOAD_GPR5
  801. EXCEPTION_LOAD_GPR4
  802. EXCEPTION_LOAD_GPR3
  803. EXCEPTION_LOAD_GPR2
  804. l.rfe // SR <- ESR, PC <- EPC
  805. exit_with_no_itranslation:
  806. EXCEPTION_LOAD_GPR4
  807. EXCEPTION_LOAD_GPR6
  808. l.j _dispatch_bus_fault
  809. l.nop
  810. /* ====================================================================== */
  811. /*
  812. * Stuff below here shouldn't go into .head section... maybe this stuff
  813. * can be moved to entry.S ???
  814. */
  815. /* ==============================================[ DTLB miss handler ]=== */
  816. /*
  817. * Comments:
  818. * Exception handlers are entered with MMU off so the following handler
  819. * needs to use physical addressing
  820. *
  821. */
  822. .text
  823. ENTRY(dtlb_miss_handler)
  824. EXCEPTION_STORE_GPR2
  825. EXCEPTION_STORE_GPR3
  826. EXCEPTION_STORE_GPR4
  827. EXCEPTION_STORE_GPR5
  828. EXCEPTION_STORE_GPR6
  829. /*
  830. * get EA of the miss
  831. */
  832. l.mfspr r2,r0,SPR_EEAR_BASE
  833. /*
  834. * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
  835. */
  836. GET_CURRENT_PGD(r3,r5) // r3 is current_pgd, r5 is temp
  837. l.srli r4,r2,0x18 // >> PAGE_SHIFT + (PAGE_SHIFT - 2)
  838. l.slli r4,r4,0x2 // to get address << 2
  839. l.add r5,r4,r3 // r4 is pgd_index(daddr)
  840. /*
  841. * if (pmd_none(*pmd))
  842. * goto pmd_none:
  843. */
  844. tophys (r4,r5)
  845. l.lwz r3,0x0(r4) // get *pmd value
  846. l.sfne r3,r0
  847. l.bnf d_pmd_none
  848. l.andi r3,r3,~PAGE_MASK //0x1fff // ~PAGE_MASK
  849. /*
  850. * if (pmd_bad(*pmd))
  851. * pmd_clear(pmd)
  852. * goto pmd_bad:
  853. */
  854. // l.sfeq r3,r0 // check *pmd value
  855. // l.bf d_pmd_good
  856. l.addi r3,r0,0xffffe000 // PAGE_MASK
  857. // l.j d_pmd_bad
  858. // l.sw 0x0(r4),r0 // clear pmd
  859. d_pmd_good:
  860. /*
  861. * pte = *pte_offset(pmd, daddr);
  862. */
  863. l.lwz r4,0x0(r4) // get **pmd value
  864. l.and r4,r4,r3 // & PAGE_MASK
  865. l.srli r5,r2,0xd // >> PAGE_SHIFT, r2 == EEAR
  866. l.andi r3,r5,0x7ff // (1UL << PAGE_SHIFT - 2) - 1
  867. l.slli r3,r3,0x2 // to get address << 2
  868. l.add r3,r3,r4
  869. l.lwz r2,0x0(r3) // this is pte at last
  870. /*
  871. * if (!pte_present(pte))
  872. */
  873. l.andi r4,r2,0x1
  874. l.sfne r4,r0 // is pte present
  875. l.bnf d_pte_not_present
  876. l.addi r3,r0,0xffffe3fa // PAGE_MASK | DTLB_UP_CONVERT_MASK
  877. /*
  878. * fill DTLB TR register
  879. */
  880. l.and r4,r2,r3 // apply the mask
  881. // Determine number of DMMU sets
  882. l.mfspr r6, r0, SPR_DMMUCFGR
  883. l.andi r6, r6, SPR_DMMUCFGR_NTS
  884. l.srli r6, r6, SPR_DMMUCFGR_NTS_OFF
  885. l.ori r3, r0, 0x1
  886. l.sll r3, r3, r6 // r3 = number DMMU sets DMMUCFGR
  887. l.addi r6, r3, -1 // r6 = nsets mask
  888. l.and r5, r5, r6 // calc offset: & (NUM_TLB_ENTRIES-1)
  889. //NUM_TLB_ENTRIES
  890. l.mtspr r5,r4,SPR_DTLBTR_BASE(0)
  891. /*
  892. * fill DTLB MR register
  893. */
  894. l.mfspr r2,r0,SPR_EEAR_BASE
  895. l.addi r3,r0,0xffffe000 // PAGE_MASK
  896. l.and r4,r2,r3 // apply PAGE_MASK to EA (__PHX__ do we really need this?)
  897. l.ori r4,r4,0x1 // set hardware valid bit: DTBL_MR entry
  898. l.mtspr r5,r4,SPR_DTLBMR_BASE(0)
  899. EXCEPTION_LOAD_GPR2
  900. EXCEPTION_LOAD_GPR3
  901. EXCEPTION_LOAD_GPR4
  902. EXCEPTION_LOAD_GPR5
  903. EXCEPTION_LOAD_GPR6
  904. l.rfe
  905. d_pmd_bad:
  906. l.nop 1
  907. EXCEPTION_LOAD_GPR2
  908. EXCEPTION_LOAD_GPR3
  909. EXCEPTION_LOAD_GPR4
  910. EXCEPTION_LOAD_GPR5
  911. EXCEPTION_LOAD_GPR6
  912. l.rfe
  913. d_pmd_none:
  914. d_pte_not_present:
  915. EXCEPTION_LOAD_GPR2
  916. EXCEPTION_LOAD_GPR3
  917. EXCEPTION_LOAD_GPR4
  918. EXCEPTION_LOAD_GPR5
  919. EXCEPTION_LOAD_GPR6
  920. l.j _dispatch_do_dpage_fault
  921. l.nop
  922. /* ==============================================[ ITLB miss handler ]=== */
  923. ENTRY(itlb_miss_handler)
  924. EXCEPTION_STORE_GPR2
  925. EXCEPTION_STORE_GPR3
  926. EXCEPTION_STORE_GPR4
  927. EXCEPTION_STORE_GPR5
  928. EXCEPTION_STORE_GPR6
  929. /*
  930. * get EA of the miss
  931. */
  932. l.mfspr r2,r0,SPR_EEAR_BASE
  933. /*
  934. * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
  935. *
  936. */
  937. GET_CURRENT_PGD(r3,r5) // r3 is current_pgd, r5 is temp
  938. l.srli r4,r2,0x18 // >> PAGE_SHIFT + (PAGE_SHIFT - 2)
  939. l.slli r4,r4,0x2 // to get address << 2
  940. l.add r5,r4,r3 // r4 is pgd_index(daddr)
  941. /*
  942. * if (pmd_none(*pmd))
  943. * goto pmd_none:
  944. */
  945. tophys (r4,r5)
  946. l.lwz r3,0x0(r4) // get *pmd value
  947. l.sfne r3,r0
  948. l.bnf i_pmd_none
  949. l.andi r3,r3,0x1fff // ~PAGE_MASK
  950. /*
  951. * if (pmd_bad(*pmd))
  952. * pmd_clear(pmd)
  953. * goto pmd_bad:
  954. */
  955. // l.sfeq r3,r0 // check *pmd value
  956. // l.bf i_pmd_good
  957. l.addi r3,r0,0xffffe000 // PAGE_MASK
  958. // l.j i_pmd_bad
  959. // l.sw 0x0(r4),r0 // clear pmd
  960. i_pmd_good:
  961. /*
  962. * pte = *pte_offset(pmd, iaddr);
  963. *
  964. */
  965. l.lwz r4,0x0(r4) // get **pmd value
  966. l.and r4,r4,r3 // & PAGE_MASK
  967. l.srli r5,r2,0xd // >> PAGE_SHIFT, r2 == EEAR
  968. l.andi r3,r5,0x7ff // (1UL << PAGE_SHIFT - 2) - 1
  969. l.slli r3,r3,0x2 // to get address << 2
  970. l.add r3,r3,r4
  971. l.lwz r2,0x0(r3) // this is pte at last
  972. /*
  973. * if (!pte_present(pte))
  974. *
  975. */
  976. l.andi r4,r2,0x1
  977. l.sfne r4,r0 // is pte present
  978. l.bnf i_pte_not_present
  979. l.addi r3,r0,0xffffe03a // PAGE_MASK | ITLB_UP_CONVERT_MASK
  980. /*
  981. * fill ITLB TR register
  982. */
  983. l.and r4,r2,r3 // apply the mask
  984. l.andi r3,r2,0x7c0 // _PAGE_EXEC | _PAGE_SRE | _PAGE_SWE | _PAGE_URE | _PAGE_UWE
  985. // l.andi r3,r2,0x400 // _PAGE_EXEC
  986. l.sfeq r3,r0
  987. l.bf itlb_tr_fill //_workaround
  988. // Determine number of IMMU sets
  989. l.mfspr r6, r0, SPR_IMMUCFGR
  990. l.andi r6, r6, SPR_IMMUCFGR_NTS
  991. l.srli r6, r6, SPR_IMMUCFGR_NTS_OFF
  992. l.ori r3, r0, 0x1
  993. l.sll r3, r3, r6 // r3 = number IMMU sets IMMUCFGR
  994. l.addi r6, r3, -1 // r6 = nsets mask
  995. l.and r5, r5, r6 // calc offset: & (NUM_TLB_ENTRIES-1)
  996. /*
  997. * __PHX__ :: fixme
  998. * we should not just blindly set executable flags,
  999. * but it does help with ping. the clean way would be to find out
  1000. * (and fix it) why stack doesn't have execution permissions
  1001. */
  1002. itlb_tr_fill_workaround:
  1003. l.ori r4,r4,0xc0 // | (SPR_ITLBTR_UXE | ITLBTR_SXE)
  1004. itlb_tr_fill:
  1005. l.mtspr r5,r4,SPR_ITLBTR_BASE(0)
  1006. /*
  1007. * fill DTLB MR register
  1008. */
  1009. l.mfspr r2,r0,SPR_EEAR_BASE
  1010. l.addi r3,r0,0xffffe000 // PAGE_MASK
  1011. l.and r4,r2,r3 // apply PAGE_MASK to EA (__PHX__ do we really need this?)
  1012. l.ori r4,r4,0x1 // set hardware valid bit: DTBL_MR entry
  1013. l.mtspr r5,r4,SPR_ITLBMR_BASE(0)
  1014. EXCEPTION_LOAD_GPR2
  1015. EXCEPTION_LOAD_GPR3
  1016. EXCEPTION_LOAD_GPR4
  1017. EXCEPTION_LOAD_GPR5
  1018. EXCEPTION_LOAD_GPR6
  1019. l.rfe
  1020. i_pmd_bad:
  1021. l.nop 1
  1022. EXCEPTION_LOAD_GPR2
  1023. EXCEPTION_LOAD_GPR3
  1024. EXCEPTION_LOAD_GPR4
  1025. EXCEPTION_LOAD_GPR5
  1026. EXCEPTION_LOAD_GPR6
  1027. l.rfe
  1028. i_pmd_none:
  1029. i_pte_not_present:
  1030. EXCEPTION_LOAD_GPR2
  1031. EXCEPTION_LOAD_GPR3
  1032. EXCEPTION_LOAD_GPR4
  1033. EXCEPTION_LOAD_GPR5
  1034. EXCEPTION_LOAD_GPR6
  1035. l.j _dispatch_do_ipage_fault
  1036. l.nop
  1037. /* ==============================================[ boot tlb handlers ]=== */
  1038. /* =================================================[ debugging aids ]=== */
  1039. .align 64
  1040. _immu_trampoline:
  1041. .space 64
  1042. _immu_trampoline_top:
  1043. #define TRAMP_SLOT_0 (0x0)
  1044. #define TRAMP_SLOT_1 (0x4)
  1045. #define TRAMP_SLOT_2 (0x8)
  1046. #define TRAMP_SLOT_3 (0xc)
  1047. #define TRAMP_SLOT_4 (0x10)
  1048. #define TRAMP_SLOT_5 (0x14)
  1049. #define TRAMP_FRAME_SIZE (0x18)
  1050. ENTRY(_immu_trampoline_workaround)
  1051. // r2 EEA
  1052. // r6 is physical EEA
  1053. tophys(r6,r2)
  1054. LOAD_SYMBOL_2_GPR(r5,_immu_trampoline)
  1055. tophys (r3,r5) // r3 is trampoline (physical)
  1056. LOAD_SYMBOL_2_GPR(r4,0x15000000)
  1057. l.sw TRAMP_SLOT_0(r3),r4
  1058. l.sw TRAMP_SLOT_1(r3),r4
  1059. l.sw TRAMP_SLOT_4(r3),r4
  1060. l.sw TRAMP_SLOT_5(r3),r4
  1061. // EPC = EEA - 0x4
  1062. l.lwz r4,0x0(r6) // load op @ EEA + 0x0 (fc address)
  1063. l.sw TRAMP_SLOT_3(r3),r4 // store it to _immu_trampoline_data
  1064. l.lwz r4,-0x4(r6) // load op @ EEA - 0x4 (f8 address)
  1065. l.sw TRAMP_SLOT_2(r3),r4 // store it to _immu_trampoline_data
  1066. l.srli r5,r4,26 // check opcode for write access
  1067. l.sfeqi r5,0 // l.j
  1068. l.bf 0f
  1069. l.sfeqi r5,0x11 // l.jr
  1070. l.bf 1f
  1071. l.sfeqi r5,1 // l.jal
  1072. l.bf 2f
  1073. l.sfeqi r5,0x12 // l.jalr
  1074. l.bf 3f
  1075. l.sfeqi r5,3 // l.bnf
  1076. l.bf 4f
  1077. l.sfeqi r5,4 // l.bf
  1078. l.bf 5f
  1079. 99:
  1080. l.nop
  1081. l.j 99b // should never happen
  1082. l.nop 1
  1083. // r2 is EEA
  1084. // r3 is trampoline address (physical)
  1085. // r4 is instruction
  1086. // r6 is physical(EEA)
  1087. //
  1088. // r5
  1089. 2: // l.jal
  1090. /* 19 20 aa aa l.movhi r9,0xaaaa
  1091. * a9 29 bb bb l.ori r9,0xbbbb
  1092. *
  1093. * where 0xaaaabbbb is EEA + 0x4 shifted right 2
  1094. */
  1095. l.addi r6,r2,0x4 // this is 0xaaaabbbb
  1096. // l.movhi r9,0xaaaa
  1097. l.ori r5,r0,0x1920 // 0x1920 == l.movhi r9
  1098. l.sh (TRAMP_SLOT_0+0x0)(r3),r5
  1099. l.srli r5,r6,16
  1100. l.sh (TRAMP_SLOT_0+0x2)(r3),r5
  1101. // l.ori r9,0xbbbb
  1102. l.ori r5,r0,0xa929 // 0xa929 == l.ori r9
  1103. l.sh (TRAMP_SLOT_1+0x0)(r3),r5
  1104. l.andi r5,r6,0xffff
  1105. l.sh (TRAMP_SLOT_1+0x2)(r3),r5
  1106. /* falthrough, need to set up new jump offset */
  1107. 0: // l.j
  1108. l.slli r6,r4,6 // original offset shifted left 6 - 2
  1109. // l.srli r6,r6,6 // original offset shifted right 2
  1110. l.slli r4,r2,4 // old jump position: EEA shifted left 4
  1111. // l.srli r4,r4,6 // old jump position: shifted right 2
  1112. l.addi r5,r3,0xc // new jump position (physical)
  1113. l.slli r5,r5,4 // new jump position: shifted left 4
  1114. // calculate new jump offset
  1115. // new_off = old_off + (old_jump - new_jump)
  1116. l.sub r5,r4,r5 // old_jump - new_jump
  1117. l.add r5,r6,r5 // orig_off + (old_jump - new_jump)
  1118. l.srli r5,r5,6 // new offset shifted right 2
  1119. // r5 is new jump offset
  1120. // l.j has opcode 0x0...
  1121. l.sw TRAMP_SLOT_2(r3),r5 // write it back
  1122. l.j trampoline_out
  1123. l.nop
  1124. /* ----------------------------- */
  1125. 3: // l.jalr
  1126. /* 19 20 aa aa l.movhi r9,0xaaaa
  1127. * a9 29 bb bb l.ori r9,0xbbbb
  1128. *
  1129. * where 0xaaaabbbb is EEA + 0x4 shifted right 2
  1130. */
  1131. l.addi r6,r2,0x4 // this is 0xaaaabbbb
  1132. // l.movhi r9,0xaaaa
  1133. l.ori r5,r0,0x1920 // 0x1920 == l.movhi r9
  1134. l.sh (TRAMP_SLOT_0+0x0)(r3),r5
  1135. l.srli r5,r6,16
  1136. l.sh (TRAMP_SLOT_0+0x2)(r3),r5
  1137. // l.ori r9,0xbbbb
  1138. l.ori r5,r0,0xa929 // 0xa929 == l.ori r9
  1139. l.sh (TRAMP_SLOT_1+0x0)(r3),r5
  1140. l.andi r5,r6,0xffff
  1141. l.sh (TRAMP_SLOT_1+0x2)(r3),r5
  1142. l.lhz r5,(TRAMP_SLOT_2+0x0)(r3) // load hi part of jump instruction
  1143. l.andi r5,r5,0x3ff // clear out opcode part
  1144. l.ori r5,r5,0x4400 // opcode changed from l.jalr -> l.jr
  1145. l.sh (TRAMP_SLOT_2+0x0)(r3),r5 // write it back
  1146. /* falthrough */
  1147. 1: // l.jr
  1148. l.j trampoline_out
  1149. l.nop
  1150. /* ----------------------------- */
  1151. 4: // l.bnf
  1152. 5: // l.bf
  1153. l.slli r6,r4,6 // original offset shifted left 6 - 2
  1154. // l.srli r6,r6,6 // original offset shifted right 2
  1155. l.slli r4,r2,4 // old jump position: EEA shifted left 4
  1156. // l.srli r4,r4,6 // old jump position: shifted right 2
  1157. l.addi r5,r3,0xc // new jump position (physical)
  1158. l.slli r5,r5,4 // new jump position: shifted left 4
  1159. // calculate new jump offset
  1160. // new_off = old_off + (old_jump - new_jump)
  1161. l.add r6,r6,r4 // (orig_off + old_jump)
  1162. l.sub r6,r6,r5 // (orig_off + old_jump) - new_jump
  1163. l.srli r6,r6,6 // new offset shifted right 2
  1164. // r6 is new jump offset
  1165. l.lwz r4,(TRAMP_SLOT_2+0x0)(r3) // load jump instruction
  1166. l.srli r4,r4,16
  1167. l.andi r4,r4,0xfc00 // get opcode part
  1168. l.slli r4,r4,16
  1169. l.or r6,r4,r6 // l.b(n)f new offset
  1170. l.sw TRAMP_SLOT_2(r3),r6 // write it back
  1171. /* we need to add l.j to EEA + 0x8 */
  1172. tophys (r4,r2) // may not be needed (due to shifts down_
  1173. l.addi r4,r4,(0x8 - 0x8) // jump target = r2 + 0x8 (compensate for 0x8)
  1174. // jump position = r5 + 0x8 (0x8 compensated)
  1175. l.sub r4,r4,r5 // jump offset = target - new_position + 0x8
  1176. l.slli r4,r4,4 // the amount of info in imediate of jump
  1177. l.srli r4,r4,6 // jump instruction with offset
  1178. l.sw TRAMP_SLOT_4(r3),r4 // write it to 4th slot
  1179. /* fallthrough */
  1180. trampoline_out:
  1181. // set up new EPC to point to our trampoline code
  1182. LOAD_SYMBOL_2_GPR(r5,_immu_trampoline)
  1183. l.mtspr r0,r5,SPR_EPCR_BASE
  1184. // immu_trampoline is (4x) CACHE_LINE aligned
  1185. // and only 6 instructions long,
  1186. // so we need to invalidate only 2 lines
  1187. /* Establish cache block size
  1188. If BS=0, 16;
  1189. If BS=1, 32;
  1190. r14 contain block size
  1191. */
  1192. l.mfspr r21,r0,SPR_ICCFGR
  1193. l.andi r21,r21,SPR_ICCFGR_CBS
  1194. l.srli r21,r21,7
  1195. l.ori r23,r0,16
  1196. l.sll r14,r23,r21
  1197. l.mtspr r0,r5,SPR_ICBIR
  1198. l.add r5,r5,r14
  1199. l.mtspr r0,r5,SPR_ICBIR
  1200. l.jr r9
  1201. l.nop
  1202. /*
  1203. * DSCR: prints a string referenced by r3.
  1204. *
  1205. * PRMS: r3 - address of the first character of null
  1206. * terminated string to be printed
  1207. *
  1208. * PREQ: UART at UART_BASE_ADD has to be initialized
  1209. *
  1210. * POST: caller should be aware that r3, r9 are changed
  1211. */
  1212. ENTRY(_emergency_print)
  1213. EMERGENCY_PRINT_STORE_GPR4
  1214. EMERGENCY_PRINT_STORE_GPR5
  1215. EMERGENCY_PRINT_STORE_GPR6
  1216. EMERGENCY_PRINT_STORE_GPR7
  1217. 2:
  1218. l.lbz r7,0(r3)
  1219. l.sfeq r7,r0
  1220. l.bf 9f
  1221. l.nop
  1222. // putc:
  1223. l.movhi r4,hi(UART_BASE_ADD)
  1224. l.addi r6,r0,0x20
  1225. 1: l.lbz r5,5(r4)
  1226. l.andi r5,r5,0x20
  1227. l.sfeq r5,r6
  1228. l.bnf 1b
  1229. l.nop
  1230. l.sb 0(r4),r7
  1231. l.addi r6,r0,0x60
  1232. 1: l.lbz r5,5(r4)
  1233. l.andi r5,r5,0x60
  1234. l.sfeq r5,r6
  1235. l.bnf 1b
  1236. l.nop
  1237. /* next character */
  1238. l.j 2b
  1239. l.addi r3,r3,0x1
  1240. 9:
  1241. EMERGENCY_PRINT_LOAD_GPR7
  1242. EMERGENCY_PRINT_LOAD_GPR6
  1243. EMERGENCY_PRINT_LOAD_GPR5
  1244. EMERGENCY_PRINT_LOAD_GPR4
  1245. l.jr r9
  1246. l.nop
  1247. ENTRY(_emergency_print_nr)
  1248. EMERGENCY_PRINT_STORE_GPR4
  1249. EMERGENCY_PRINT_STORE_GPR5
  1250. EMERGENCY_PRINT_STORE_GPR6
  1251. EMERGENCY_PRINT_STORE_GPR7
  1252. EMERGENCY_PRINT_STORE_GPR8
  1253. l.addi r8,r0,32 // shift register
  1254. 1: /* remove leading zeros */
  1255. l.addi r8,r8,-0x4
  1256. l.srl r7,r3,r8
  1257. l.andi r7,r7,0xf
  1258. /* don't skip the last zero if number == 0x0 */
  1259. l.sfeqi r8,0x4
  1260. l.bf 2f
  1261. l.nop
  1262. l.sfeq r7,r0
  1263. l.bf 1b
  1264. l.nop
  1265. 2:
  1266. l.srl r7,r3,r8
  1267. l.andi r7,r7,0xf
  1268. l.sflts r8,r0
  1269. l.bf 9f
  1270. l.sfgtui r7,0x9
  1271. l.bnf 8f
  1272. l.nop
  1273. l.addi r7,r7,0x27
  1274. 8:
  1275. l.addi r7,r7,0x30
  1276. // putc:
  1277. l.movhi r4,hi(UART_BASE_ADD)
  1278. l.addi r6,r0,0x20
  1279. 1: l.lbz r5,5(r4)
  1280. l.andi r5,r5,0x20
  1281. l.sfeq r5,r6
  1282. l.bnf 1b
  1283. l.nop
  1284. l.sb 0(r4),r7
  1285. l.addi r6,r0,0x60
  1286. 1: l.lbz r5,5(r4)
  1287. l.andi r5,r5,0x60
  1288. l.sfeq r5,r6
  1289. l.bnf 1b
  1290. l.nop
  1291. /* next character */
  1292. l.j 2b
  1293. l.addi r8,r8,-0x4
  1294. 9:
  1295. EMERGENCY_PRINT_LOAD_GPR8
  1296. EMERGENCY_PRINT_LOAD_GPR7
  1297. EMERGENCY_PRINT_LOAD_GPR6
  1298. EMERGENCY_PRINT_LOAD_GPR5
  1299. EMERGENCY_PRINT_LOAD_GPR4
  1300. l.jr r9
  1301. l.nop
  1302. /*
  1303. * This should be used for debugging only.
  1304. * It messes up the Linux early serial output
  1305. * somehow, so use it sparingly and essentially
  1306. * only if you need to debug something that goes wrong
  1307. * before Linux gets the early serial going.
  1308. *
  1309. * Furthermore, you'll have to make sure you set the
  1310. * UART_DEVISOR correctly according to the system
  1311. * clock rate.
  1312. *
  1313. *
  1314. */
  1315. #define SYS_CLK 20000000
  1316. //#define SYS_CLK 1843200
  1317. #define OR32_CONSOLE_BAUD 115200
  1318. #define UART_DIVISOR SYS_CLK/(16*OR32_CONSOLE_BAUD)
  1319. ENTRY(_early_uart_init)
  1320. l.movhi r3,hi(UART_BASE_ADD)
  1321. l.addi r4,r0,0x7
  1322. l.sb 0x2(r3),r4
  1323. l.addi r4,r0,0x0
  1324. l.sb 0x1(r3),r4
  1325. l.addi r4,r0,0x3
  1326. l.sb 0x3(r3),r4
  1327. l.lbz r5,3(r3)
  1328. l.ori r4,r5,0x80
  1329. l.sb 0x3(r3),r4
  1330. l.addi r4,r0,((UART_DIVISOR>>8) & 0x000000ff)
  1331. l.sb UART_DLM(r3),r4
  1332. l.addi r4,r0,((UART_DIVISOR) & 0x000000ff)
  1333. l.sb UART_DLL(r3),r4
  1334. l.sb 0x3(r3),r5
  1335. l.jr r9
  1336. l.nop
  1337. _string_copying_linux:
  1338. .string "\n\n\n\n\n\rCopying Linux... \0"
  1339. _string_ok_booting:
  1340. .string "Ok, booting the kernel.\n\r\0"
  1341. _string_unhandled_exception:
  1342. .string "\n\rRunarunaround: Unhandled exception 0x\0"
  1343. _string_epc_prefix:
  1344. .string ": EPC=0x\0"
  1345. _string_nl:
  1346. .string "\n\r\0"
  1347. .global _string_esr_irq_bug
  1348. _string_esr_irq_bug:
  1349. .string "\n\rESR external interrupt bug, for details look into entry.S\n\r\0"
  1350. /* ========================================[ page aligned structures ]=== */
  1351. /*
  1352. * .data section should be page aligned
  1353. * (look into arch/or32/kernel/vmlinux.lds)
  1354. */
  1355. .section .data,"aw"
  1356. .align 8192
  1357. .global empty_zero_page
  1358. empty_zero_page:
  1359. .space 8192
  1360. .global swapper_pg_dir
  1361. swapper_pg_dir:
  1362. .space 8192
  1363. .global _unhandled_stack
  1364. _unhandled_stack:
  1365. .space 8192
  1366. _unhandled_stack_top:
  1367. /* ============================================================[ EOF ]=== */