4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
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>
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.
18 #include <linux/linkage.h>
19 #include <linux/threads.h>
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/serial_reg.h>
23 #include <asm/processor.h>
26 #include <asm/pgtable.h>
27 #include <asm/thread_info.h>
28 #include <asm/cache.h>
29 #include <asm/spr_defs.h>
30 #include <asm/asm-offsets.h>
31 #include <linux/of_fdt.h>
33 #define tophys(rd,rs) \
34 l.movhi rd,hi(-KERNELBASE) ;\
37 #define CLEAR_GPR(gpr) \
40 #define LOAD_SYMBOL_2_GPR(gpr,symbol) \
41 l.movhi gpr,hi(symbol) ;\
42 l.ori gpr,gpr,lo(symbol)
45 #define UART_BASE_ADD 0x90000000
47 #define EXCEPTION_SR (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_SM)
48 #define SYSCALL_SR (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_IEE | SPR_SR_TEE | SPR_SR_SM)
50 /* ============================================[ tmp store locations ]=== */
53 * emergency_print temporary stores
55 #define EMERGENCY_PRINT_STORE_GPR4 l.sw 0x20(r0),r4
56 #define EMERGENCY_PRINT_LOAD_GPR4 l.lwz r4,0x20(r0)
58 #define EMERGENCY_PRINT_STORE_GPR5 l.sw 0x24(r0),r5
59 #define EMERGENCY_PRINT_LOAD_GPR5 l.lwz r5,0x24(r0)
61 #define EMERGENCY_PRINT_STORE_GPR6 l.sw 0x28(r0),r6
62 #define EMERGENCY_PRINT_LOAD_GPR6 l.lwz r6,0x28(r0)
64 #define EMERGENCY_PRINT_STORE_GPR7 l.sw 0x2c(r0),r7
65 #define EMERGENCY_PRINT_LOAD_GPR7 l.lwz r7,0x2c(r0)
67 #define EMERGENCY_PRINT_STORE_GPR8 l.sw 0x30(r0),r8
68 #define EMERGENCY_PRINT_LOAD_GPR8 l.lwz r8,0x30(r0)
70 #define EMERGENCY_PRINT_STORE_GPR9 l.sw 0x34(r0),r9
71 #define EMERGENCY_PRINT_LOAD_GPR9 l.lwz r9,0x34(r0)
75 * TLB miss handlers temorary stores
77 #define EXCEPTION_STORE_GPR9 l.sw 0x10(r0),r9
78 #define EXCEPTION_LOAD_GPR9 l.lwz r9,0x10(r0)
80 #define EXCEPTION_STORE_GPR2 l.sw 0x64(r0),r2
81 #define EXCEPTION_LOAD_GPR2 l.lwz r2,0x64(r0)
83 #define EXCEPTION_STORE_GPR3 l.sw 0x68(r0),r3
84 #define EXCEPTION_LOAD_GPR3 l.lwz r3,0x68(r0)
86 #define EXCEPTION_STORE_GPR4 l.sw 0x6c(r0),r4
87 #define EXCEPTION_LOAD_GPR4 l.lwz r4,0x6c(r0)
89 #define EXCEPTION_STORE_GPR5 l.sw 0x70(r0),r5
90 #define EXCEPTION_LOAD_GPR5 l.lwz r5,0x70(r0)
92 #define EXCEPTION_STORE_GPR6 l.sw 0x74(r0),r6
93 #define EXCEPTION_LOAD_GPR6 l.lwz r6,0x74(r0)
97 * EXCEPTION_HANDLE temporary stores
100 #define EXCEPTION_T_STORE_GPR30 l.sw 0x78(r0),r30
101 #define EXCEPTION_T_LOAD_GPR30(reg) l.lwz reg,0x78(r0)
103 #define EXCEPTION_T_STORE_GPR10 l.sw 0x7c(r0),r10
104 #define EXCEPTION_T_LOAD_GPR10(reg) l.lwz reg,0x7c(r0)
106 #define EXCEPTION_T_STORE_SP l.sw 0x80(r0),r1
107 #define EXCEPTION_T_LOAD_SP(reg) l.lwz reg,0x80(r0)
110 * For UNHANLDED_EXCEPTION
113 #define EXCEPTION_T_STORE_GPR31 l.sw 0x84(r0),r31
114 #define EXCEPTION_T_LOAD_GPR31(reg) l.lwz reg,0x84(r0)
116 /* =========================================================[ macros ]=== */
119 #define GET_CURRENT_PGD(reg,t1) \
120 LOAD_SYMBOL_2_GPR(reg,current_pgd) ;\
126 * DSCR: this is a common hook for handling exceptions. it will save
127 * the needed registers, set up stack and pointer to current
128 * then jump to the handler while enabling MMU
130 * PRMS: handler - a function to jump to. it has to save the
131 * remaining registers to kernel stack, call
132 * appropriate arch-independant exception handler
133 * and finaly jump to ret_from_except
135 * PREQ: unchanged state from the time exception happened
137 * POST: SAVED the following registers original value
138 * to the new created exception frame pointed to by r1
140 * r1 - ksp pointing to the new (exception) frame
141 * r4 - EEAR exception EA
142 * r10 - current pointing to current_thread_info struct
143 * r12 - syscall 0, since we didn't come from syscall
144 * r30 - handler address of the handler we'll jump to
146 * handler has to save remaining registers to the exception
147 * ksp frame *before* tainting them!
149 * NOTE: this function is not reentrant per se. reentrancy is guaranteed
150 * by processor disabling all exceptions/interrupts when exception
153 * OPTM: no need to make it so wasteful to extract ksp when in user mode
156 #define EXCEPTION_HANDLE(handler) \
157 EXCEPTION_T_STORE_GPR30 ;\
158 l.mfspr r30,r0,SPR_ESR_BASE ;\
159 l.andi r30,r30,SPR_SR_SM ;\
161 EXCEPTION_T_STORE_GPR10 ;\
162 l.bnf 2f /* kernel_mode */ ;\
163 EXCEPTION_T_STORE_SP /* delay slot */ ;\
164 1: /* user_mode: */ ;\
165 LOAD_SYMBOL_2_GPR(r1,current_thread_info_set) ;\
167 /* r10: current_thread_info */ ;\
170 l.lwz r1,(TI_KSP)(r30) ;\
171 /* fall through */ ;\
172 2: /* kernel_mode: */ ;\
173 /* create new stack frame, save only needed gprs */ ;\
174 /* r1: KSP, r10: current, r4: EEAR, r31: __pa(KSP) */ ;\
175 /* r12: temp, syscall indicator */ ;\
176 l.addi r1,r1,-(INT_FRAME_SIZE) ;\
177 /* r1 is KSP, r30 is __pa(KSP) */ ;\
179 l.sw PT_GPR12(r30),r12 ;\
180 /* r4 use for tmp before EA */ ;\
181 l.mfspr r12,r0,SPR_EPCR_BASE ;\
182 l.sw PT_PC(r30),r12 ;\
183 l.mfspr r12,r0,SPR_ESR_BASE ;\
184 l.sw PT_SR(r30),r12 ;\
186 EXCEPTION_T_LOAD_GPR30(r12) ;\
187 l.sw PT_GPR30(r30),r12 ;\
188 /* save r10 as was prior to exception */ ;\
189 EXCEPTION_T_LOAD_GPR10(r12) ;\
190 l.sw PT_GPR10(r30),r12 ;\
191 /* save PT_SP as was prior to exception */ ;\
192 EXCEPTION_T_LOAD_SP(r12) ;\
193 l.sw PT_SP(r30),r12 ;\
194 /* save exception r4, set r4 = EA */ ;\
195 l.sw PT_GPR4(r30),r4 ;\
196 l.mfspr r4,r0,SPR_EEAR_BASE ;\
197 /* r12 == 1 if we come from syscall */ ;\
199 /* ----- turn on MMU ----- */ ;\
200 /* Carry DSX into exception SR */ ;\
201 l.mfspr r30,r0,SPR_SR ;\
202 l.andi r30,r30,SPR_SR_DSX ;\
203 l.ori r30,r30,(EXCEPTION_SR) ;\
204 l.mtspr r0,r30,SPR_ESR_BASE ;\
205 /* r30: EA address of handler */ ;\
206 LOAD_SYMBOL_2_GPR(r30,handler) ;\
207 l.mtspr r0,r30,SPR_EPCR_BASE ;\
214 * #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
215 * #define UNHANDLED_EXCEPTION(handler) \
217 * l.mtspr r0,r3,SPR_SR ;\
218 * l.movhi r3,hi(0xf0000100) ;\
219 * l.ori r3,r3,lo(0xf0000100) ;\
226 /* DSCR: this is the same as EXCEPTION_HANDLE(), we are just
227 * a bit more carefull (if we have a PT_SP or current pointer
228 * corruption) and set them up from 'current_set'
231 #define UNHANDLED_EXCEPTION(handler) \
232 EXCEPTION_T_STORE_GPR31 ;\
233 EXCEPTION_T_STORE_GPR10 ;\
234 EXCEPTION_T_STORE_SP ;\
235 /* temporary store r3, r9 into r1, r10 */ ;\
238 /* the string referenced by r3 must be low enough */ ;\
239 l.jal _emergency_print ;\
240 l.ori r3,r0,lo(_string_unhandled_exception) ;\
241 l.mfspr r3,r0,SPR_NPC ;\
242 l.jal _emergency_print_nr ;\
243 l.andi r3,r3,0x1f00 ;\
244 /* the string referenced by r3 must be low enough */ ;\
245 l.jal _emergency_print ;\
246 l.ori r3,r0,lo(_string_epc_prefix) ;\
247 l.jal _emergency_print_nr ;\
248 l.mfspr r3,r0,SPR_EPCR_BASE ;\
249 l.jal _emergency_print ;\
250 l.ori r3,r0,lo(_string_nl) ;\
251 /* end of printing */ ;\
254 /* extract current, ksp from current_set */ ;\
255 LOAD_SYMBOL_2_GPR(r1,_unhandled_stack_top) ;\
256 LOAD_SYMBOL_2_GPR(r10,init_thread_union) ;\
257 /* create new stack frame, save only needed gprs */ ;\
258 /* r1: KSP, r10: current, r31: __pa(KSP) */ ;\
259 /* r12: temp, syscall indicator, r13 temp */ ;\
260 l.addi r1,r1,-(INT_FRAME_SIZE) ;\
261 /* r1 is KSP, r31 is __pa(KSP) */ ;\
263 l.sw PT_GPR12(r31),r12 ;\
264 l.mfspr r12,r0,SPR_EPCR_BASE ;\
265 l.sw PT_PC(r31),r12 ;\
266 l.mfspr r12,r0,SPR_ESR_BASE ;\
267 l.sw PT_SR(r31),r12 ;\
269 EXCEPTION_T_LOAD_GPR31(r12) ;\
270 l.sw PT_GPR31(r31),r12 ;\
271 /* save r10 as was prior to exception */ ;\
272 EXCEPTION_T_LOAD_GPR10(r12) ;\
273 l.sw PT_GPR10(r31),r12 ;\
274 /* save PT_SP as was prior to exception */ ;\
275 EXCEPTION_T_LOAD_SP(r12) ;\
276 l.sw PT_SP(r31),r12 ;\
277 l.sw PT_GPR13(r31),r13 ;\
279 /* save exception r4, set r4 = EA */ ;\
280 l.sw PT_GPR4(r31),r4 ;\
281 l.mfspr r4,r0,SPR_EEAR_BASE ;\
282 /* r12 == 1 if we come from syscall */ ;\
284 /* ----- play a MMU trick ----- */ ;\
285 l.ori r31,r0,(EXCEPTION_SR) ;\
286 l.mtspr r0,r31,SPR_ESR_BASE ;\
287 /* r31: EA address of handler */ ;\
288 LOAD_SYMBOL_2_GPR(r31,handler) ;\
289 l.mtspr r0,r31,SPR_EPCR_BASE ;\
292 /* =====================================================[ exceptions] === */
294 /* ---[ 0x100: RESET exception ]----------------------------------------- */
296 /* Jump to .init code at _start which lives in the .head section
297 * and will be discarded after boot.
299 LOAD_SYMBOL_2_GPR(r15, _start)
300 tophys (r13,r15) /* MMU disabled */
304 /* ---[ 0x200: BUS exception ]------------------------------------------- */
307 EXCEPTION_HANDLE(_bus_fault_handler)
309 /* ---[ 0x300: Data Page Fault exception ]------------------------------- */
311 _dispatch_do_dpage_fault:
312 // totaly disable timer interrupt
313 // l.mtspr r0,r0,SPR_TTMR
314 // DEBUG_TLB_PROBE(0x300)
315 // EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x300)
316 EXCEPTION_HANDLE(_data_page_fault_handler)
318 /* ---[ 0x400: Insn Page Fault exception ]------------------------------- */
320 _dispatch_do_ipage_fault:
321 // totaly disable timer interrupt
322 // l.mtspr r0,r0,SPR_TTMR
323 // DEBUG_TLB_PROBE(0x400)
324 // EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x400)
325 EXCEPTION_HANDLE(_insn_page_fault_handler)
327 /* ---[ 0x500: Timer exception ]----------------------------------------- */
329 EXCEPTION_HANDLE(_timer_handler)
331 /* ---[ 0x600: Alignment exception ]-------------------------------------- */
333 EXCEPTION_HANDLE(_alignment_handler)
335 /* ---[ 0x700: Illegal insn exception ]---------------------------------- */
337 EXCEPTION_HANDLE(_illegal_instruction_handler)
339 /* ---[ 0x800: External interrupt exception ]---------------------------- */
341 EXCEPTION_HANDLE(_external_irq_handler)
343 /* ---[ 0x900: DTLB miss exception ]------------------------------------- */
345 l.j boot_dtlb_miss_handler
348 /* ---[ 0xa00: ITLB miss exception ]------------------------------------- */
350 l.j boot_itlb_miss_handler
353 /* ---[ 0xb00: Range exception ]----------------------------------------- */
355 UNHANDLED_EXCEPTION(_vector_0xb00)
357 /* ---[ 0xc00: Syscall exception ]--------------------------------------- */
359 EXCEPTION_HANDLE(_sys_call_handler)
361 /* ---[ 0xd00: Trap exception ]------------------------------------------ */
363 UNHANDLED_EXCEPTION(_vector_0xd00)
365 /* ---[ 0xe00: Trap exception ]------------------------------------------ */
367 // UNHANDLED_EXCEPTION(_vector_0xe00)
368 EXCEPTION_HANDLE(_trap_handler)
370 /* ---[ 0xf00: Reserved exception ]-------------------------------------- */
372 UNHANDLED_EXCEPTION(_vector_0xf00)
374 /* ---[ 0x1000: Reserved exception ]------------------------------------- */
376 UNHANDLED_EXCEPTION(_vector_0x1000)
378 /* ---[ 0x1100: Reserved exception ]------------------------------------- */
380 UNHANDLED_EXCEPTION(_vector_0x1100)
382 /* ---[ 0x1200: Reserved exception ]------------------------------------- */
384 UNHANDLED_EXCEPTION(_vector_0x1200)
386 /* ---[ 0x1300: Reserved exception ]------------------------------------- */
388 UNHANDLED_EXCEPTION(_vector_0x1300)
390 /* ---[ 0x1400: Reserved exception ]------------------------------------- */
392 UNHANDLED_EXCEPTION(_vector_0x1400)
394 /* ---[ 0x1500: Reserved exception ]------------------------------------- */
396 UNHANDLED_EXCEPTION(_vector_0x1500)
398 /* ---[ 0x1600: Reserved exception ]------------------------------------- */
400 UNHANDLED_EXCEPTION(_vector_0x1600)
402 /* ---[ 0x1700: Reserved exception ]------------------------------------- */
404 UNHANDLED_EXCEPTION(_vector_0x1700)
406 /* ---[ 0x1800: Reserved exception ]------------------------------------- */
408 UNHANDLED_EXCEPTION(_vector_0x1800)
410 /* ---[ 0x1900: Reserved exception ]------------------------------------- */
412 UNHANDLED_EXCEPTION(_vector_0x1900)
414 /* ---[ 0x1a00: Reserved exception ]------------------------------------- */
416 UNHANDLED_EXCEPTION(_vector_0x1a00)
418 /* ---[ 0x1b00: Reserved exception ]------------------------------------- */
420 UNHANDLED_EXCEPTION(_vector_0x1b00)
422 /* ---[ 0x1c00: Reserved exception ]------------------------------------- */
424 UNHANDLED_EXCEPTION(_vector_0x1c00)
426 /* ---[ 0x1d00: Reserved exception ]------------------------------------- */
428 UNHANDLED_EXCEPTION(_vector_0x1d00)
430 /* ---[ 0x1e00: Reserved exception ]------------------------------------- */
432 UNHANDLED_EXCEPTION(_vector_0x1e00)
434 /* ---[ 0x1f00: Reserved exception ]------------------------------------- */
436 UNHANDLED_EXCEPTION(_vector_0x1f00)
439 /* ===================================================[ kernel start ]=== */
443 /* This early stuff belongs in HEAD, but some of the functions below definitely
449 /* Init r0 to zero as per spec */
452 /* save kernel parameters */
453 l.or r25,r0,r3 /* pointer to fdt */
456 * ensure a deterministic start
494 * set up initial ksp and current
496 /* setup kernel stack */
497 LOAD_SYMBOL_2_GPR(r1,init_thread_union + THREAD_SIZE)
498 LOAD_SYMBOL_2_GPR(r10,init_thread_union) // setup current
506 * .data contains initialized data,
507 * .bss contains uninitialized data - clear it up
510 LOAD_SYMBOL_2_GPR(r24, __bss_start)
511 LOAD_SYMBOL_2_GPR(r26, _end)
534 /* The MMU needs to be enabled before or32_early_setup is called */
539 * SR[5] = 0, SR[6] = 0, 6th and 7th bit of SR set to 0
541 l.mfspr r30,r0,SPR_SR
542 l.movhi r28,hi(SPR_SR_DME | SPR_SR_IME)
543 l.ori r28,r28,lo(SPR_SR_DME | SPR_SR_IME)
545 l.mtspr r0,r30,SPR_SR
563 // reset the simulation counters
566 /* check fdt header magic word */
567 l.lwz r3,0(r25) /* load magic from fdt into r3 */
568 l.movhi r4,hi(OF_DT_HEADER)
569 l.ori r4,r4,lo(OF_DT_HEADER)
573 /* magic number mismatch, set fdt pointer to null */
576 /* pass fdt pointer to or32_early_setup in r3 */
578 LOAD_SYMBOL_2_GPR(r24, or32_early_setup)
584 * clear all GPRS to increase determinism
618 * jump to kernel entry (start_kernel)
620 LOAD_SYMBOL_2_GPR(r30, start_kernel)
626 * I N V A L I D A T E T L B e n t r i e s
628 LOAD_SYMBOL_2_GPR(r5,SPR_DTLBMR_BASE(0))
629 LOAD_SYMBOL_2_GPR(r6,SPR_ITLBMR_BASE(0))
630 l.addi r7,r0,128 /* Maximum number of sets */
644 /* ========================================[ cache ]=== */
646 /* alignment here so we don't change memory offsets with
647 * memory controller defined
652 /* Check if IC present and skip enabling otherwise */
653 l.mfspr r24,r0,SPR_UPR
654 l.andi r26,r24,SPR_UPR_ICP
662 l.xori r5,r5,SPR_SR_ICE
666 /* Establish cache block size
669 r14 contain block size
671 l.mfspr r24,r0,SPR_ICCFGR
672 l.andi r26,r24,SPR_ICCFGR_CBS
677 /* Establish number of cache sets
678 r16 contains number of cache sets
679 r28 contains log(# of cache sets)
681 l.andi r26,r24,SPR_ICCFGR_NCS
691 // l.addi r5,r0,IC_SIZE
693 l.mtspr r0,r6,SPR_ICBIR
697 // l.addi r6,r6,IC_LINE
701 l.ori r6,r6,SPR_SR_ICE
718 /* Check if DC present and skip enabling otherwise */
719 l.mfspr r24,r0,SPR_UPR
720 l.andi r26,r24,SPR_UPR_DCP
728 l.xori r5,r5,SPR_SR_DCE
732 /* Establish cache block size
735 r14 contain block size
737 l.mfspr r24,r0,SPR_DCCFGR
738 l.andi r26,r24,SPR_DCCFGR_CBS
743 /* Establish number of cache sets
744 r16 contains number of cache sets
745 r28 contains log(# of cache sets)
747 l.andi r26,r24,SPR_DCCFGR_NCS
756 l.mtspr r0,r6,SPR_DCBIR
763 l.ori r6,r6,SPR_SR_DCE
769 /* ===============================================[ page table masks ]=== */
771 #define DTLB_UP_CONVERT_MASK 0x3fa
772 #define ITLB_UP_CONVERT_MASK 0x3a
774 /* for SMP we'd have (this is a bit subtle, CC must be always set
775 * for SMP, but since we have _PAGE_PRESENT bit always defined
776 * we can just modify the mask)
778 #define DTLB_SMP_CONVERT_MASK 0x3fb
779 #define ITLB_SMP_CONVERT_MASK 0x3b
781 /* ---[ boot dtlb miss handler ]----------------------------------------- */
783 boot_dtlb_miss_handler:
785 /* mask for DTLB_MR register: - (0) sets V (valid) bit,
786 * - (31-12) sets bits belonging to VPN (31-12)
788 #define DTLB_MR_MASK 0xfffff001
790 /* mask for DTLB_TR register: - (2) sets CI (cache inhibit) bit,
791 * - (4) sets A (access) bit,
792 * - (5) sets D (dirty) bit,
793 * - (8) sets SRE (superuser read) bit
794 * - (9) sets SWE (superuser write) bit
795 * - (31-12) sets bits belonging to VPN (31-12)
797 #define DTLB_TR_MASK 0xfffff332
799 /* These are for masking out the VPN/PPN value from the MR/TR registers...
800 * it's not the same as the PFN */
801 #define VPN_MASK 0xfffff000
802 #define PPN_MASK 0xfffff000
808 l.mfspr r6,r0,SPR_ESR_BASE //
809 l.andi r6,r6,SPR_SR_SM // are we in kernel mode ?
810 l.sfeqi r6,0 // r6 == 0x1 --> SM
811 l.bf exit_with_no_dtranslation //
815 /* this could be optimized by moving storing of
816 * non r6 registers here, and jumping r6 restore
817 * if not in supervisor mode
825 l.mfspr r4,r0,SPR_EEAR_BASE // get the offending EA
827 immediate_translation:
830 l.srli r3,r4,0xd // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
832 l.mfspr r6, r0, SPR_DMMUCFGR
833 l.andi r6, r6, SPR_DMMUCFGR_NTS
834 l.srli r6, r6, SPR_DMMUCFGR_NTS_OFF
836 l.sll r5, r5, r6 // r5 = number DMMU sets
837 l.addi r6, r5, -1 // r6 = nsets mask
838 l.and r2, r3, r6 // r2 <- r3 % NSETS_MASK
840 l.or r6,r6,r4 // r6 <- r4
841 l.ori r6,r6,~(VPN_MASK) // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
842 l.movhi r5,hi(DTLB_MR_MASK) // r5 <- ffff:0000.x000
843 l.ori r5,r5,lo(DTLB_MR_MASK) // r5 <- ffff:1111.x001 - apply DTLB_MR_MASK
844 l.and r5,r5,r6 // r5 <- VPN :VPN .x001 - we have DTLBMR entry
845 l.mtspr r2,r5,SPR_DTLBMR_BASE(0) // set DTLBMR
847 /* set up DTLB with no translation for EA <= 0xbfffffff */
848 LOAD_SYMBOL_2_GPR(r6,0xbfffffff)
849 l.sfgeu r6,r4 // flag if r6 >= r4 (if 0xbfffffff >= EA)
851 l.and r3,r4,r4 // delay slot :: 24 <- r4 (if flag==1)
853 tophys(r3,r4) // r3 <- PA
855 l.ori r3,r3,~(PPN_MASK) // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
856 l.movhi r5,hi(DTLB_TR_MASK) // r5 <- ffff:0000.x000
857 l.ori r5,r5,lo(DTLB_TR_MASK) // r5 <- ffff:1111.x330 - apply DTLB_MR_MASK
858 l.and r5,r5,r3 // r5 <- PPN :PPN .x330 - we have DTLBTR entry
859 l.mtspr r2,r5,SPR_DTLBTR_BASE(0) // set DTLBTR
867 l.rfe // SR <- ESR, PC <- EPC
869 exit_with_no_dtranslation:
870 /* EA out of memory or not in supervisor mode */
873 l.j _dispatch_bus_fault
875 /* ---[ boot itlb miss handler ]----------------------------------------- */
877 boot_itlb_miss_handler:
879 /* mask for ITLB_MR register: - sets V (valid) bit,
880 * - sets bits belonging to VPN (15-12)
882 #define ITLB_MR_MASK 0xfffff001
884 /* mask for ITLB_TR register: - sets A (access) bit,
885 * - sets SXE (superuser execute) bit
886 * - sets bits belonging to VPN (15-12)
888 #define ITLB_TR_MASK 0xfffff050
891 #define VPN_MASK 0xffffe000
892 #define PPN_MASK 0xffffe000
904 l.mfspr r6,r0,SPR_ESR_BASE //
905 l.andi r6,r6,SPR_SR_SM // are we in kernel mode ?
906 l.sfeqi r6,0 // r6 == 0x1 --> SM
907 l.bf exit_with_no_itranslation
912 l.mfspr r4,r0,SPR_EEAR_BASE // get the offending EA
917 l.srli r3,r4,0xd // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
919 l.mfspr r6, r0, SPR_IMMUCFGR
920 l.andi r6, r6, SPR_IMMUCFGR_NTS
921 l.srli r6, r6, SPR_IMMUCFGR_NTS_OFF
923 l.sll r5, r5, r6 // r5 = number IMMU sets from IMMUCFGR
924 l.addi r6, r5, -1 // r6 = nsets mask
925 l.and r2, r3, r6 // r2 <- r3 % NSETS_MASK
927 l.or r6,r6,r4 // r6 <- r4
928 l.ori r6,r6,~(VPN_MASK) // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
929 l.movhi r5,hi(ITLB_MR_MASK) // r5 <- ffff:0000.x000
930 l.ori r5,r5,lo(ITLB_MR_MASK) // r5 <- ffff:1111.x001 - apply ITLB_MR_MASK
931 l.and r5,r5,r6 // r5 <- VPN :VPN .x001 - we have ITLBMR entry
932 l.mtspr r2,r5,SPR_ITLBMR_BASE(0) // set ITLBMR
935 * set up ITLB with no translation for EA <= 0x0fffffff
937 * we need this for head.S mapping (EA = PA). if we move all functions
938 * which run with mmu enabled into entry.S, we might be able to eliminate this.
941 LOAD_SYMBOL_2_GPR(r6,0x0fffffff)
942 l.sfgeu r6,r4 // flag if r6 >= r4 (if 0xb0ffffff >= EA)
944 l.and r3,r4,r4 // delay slot :: 24 <- r4 (if flag==1)
946 tophys(r3,r4) // r3 <- PA
948 l.ori r3,r3,~(PPN_MASK) // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
949 l.movhi r5,hi(ITLB_TR_MASK) // r5 <- ffff:0000.x000
950 l.ori r5,r5,lo(ITLB_TR_MASK) // r5 <- ffff:1111.x050 - apply ITLB_MR_MASK
951 l.and r5,r5,r3 // r5 <- PPN :PPN .x050 - we have ITLBTR entry
952 l.mtspr r2,r5,SPR_ITLBTR_BASE(0) // set ITLBTR
960 l.rfe // SR <- ESR, PC <- EPC
962 exit_with_no_itranslation:
965 l.j _dispatch_bus_fault
968 /* ====================================================================== */
970 * Stuff below here shouldn't go into .head section... maybe this stuff
971 * can be moved to entry.S ???
974 /* ==============================================[ DTLB miss handler ]=== */
978 * Exception handlers are entered with MMU off so the following handler
979 * needs to use physical addressing
984 ENTRY(dtlb_miss_handler)
991 l.mfspr r2,r0,SPR_EEAR_BASE
993 * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
995 GET_CURRENT_PGD(r3,r4) // r3 is current_pgd, r4 is temp
996 l.srli r4,r2,0x18 // >> PAGE_SHIFT + (PAGE_SHIFT - 2)
997 l.slli r4,r4,0x2 // to get address << 2
998 l.add r3,r4,r3 // r4 is pgd_index(daddr)
1000 * if (pmd_none(*pmd))
1004 l.lwz r3,0x0(r4) // get *pmd value
1007 l.addi r3,r0,0xffffe000 // PAGE_MASK
1011 * pte = *pte_offset(pmd, daddr);
1013 l.lwz r4,0x0(r4) // get **pmd value
1014 l.and r4,r4,r3 // & PAGE_MASK
1015 l.srli r2,r2,0xd // >> PAGE_SHIFT, r2 == EEAR
1016 l.andi r3,r2,0x7ff // (1UL << PAGE_SHIFT - 2) - 1
1017 l.slli r3,r3,0x2 // to get address << 2
1019 l.lwz r3,0x0(r3) // this is pte at last
1021 * if (!pte_present(pte))
1024 l.sfne r4,r0 // is pte present
1025 l.bnf d_pte_not_present
1026 l.addi r4,r0,0xffffe3fa // PAGE_MASK | DTLB_UP_CONVERT_MASK
1028 * fill DTLB TR register
1030 l.and r4,r3,r4 // apply the mask
1031 // Determine number of DMMU sets
1032 l.mfspr r2, r0, SPR_DMMUCFGR
1033 l.andi r2, r2, SPR_DMMUCFGR_NTS
1034 l.srli r2, r2, SPR_DMMUCFGR_NTS_OFF
1036 l.sll r3, r3, r2 // r3 = number DMMU sets DMMUCFGR
1037 l.addi r2, r3, -1 // r2 = nsets mask
1038 l.mfspr r3, r0, SPR_EEAR_BASE
1039 l.srli r3, r3, 0xd // >> PAGE_SHIFT
1040 l.and r2, r3, r2 // calc offset: & (NUM_TLB_ENTRIES-1)
1042 l.mtspr r2,r4,SPR_DTLBTR_BASE(0)
1044 * fill DTLB MR register
1046 l.slli r3, r3, 0xd /* << PAGE_SHIFT => EA & PAGE_MASK */
1047 l.ori r4,r3,0x1 // set hardware valid bit: DTBL_MR entry
1048 l.mtspr r2,r4,SPR_DTLBMR_BASE(0)
1059 EXCEPTION_HANDLE(_dtlb_miss_page_fault_handler)
1061 /* ==============================================[ ITLB miss handler ]=== */
1062 ENTRY(itlb_miss_handler)
1063 EXCEPTION_STORE_GPR2
1064 EXCEPTION_STORE_GPR3
1065 EXCEPTION_STORE_GPR4
1067 * get EA of the miss
1069 l.mfspr r2,r0,SPR_EEAR_BASE
1072 * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
1075 GET_CURRENT_PGD(r3,r4) // r3 is current_pgd, r5 is temp
1076 l.srli r4,r2,0x18 // >> PAGE_SHIFT + (PAGE_SHIFT - 2)
1077 l.slli r4,r4,0x2 // to get address << 2
1078 l.add r3,r4,r3 // r4 is pgd_index(daddr)
1080 * if (pmd_none(*pmd))
1084 l.lwz r3,0x0(r4) // get *pmd value
1087 l.addi r3,r0,0xffffe000 // PAGE_MASK
1091 * pte = *pte_offset(pmd, iaddr);
1094 l.lwz r4,0x0(r4) // get **pmd value
1095 l.and r4,r4,r3 // & PAGE_MASK
1096 l.srli r2,r2,0xd // >> PAGE_SHIFT, r2 == EEAR
1097 l.andi r3,r2,0x7ff // (1UL << PAGE_SHIFT - 2) - 1
1098 l.slli r3,r3,0x2 // to get address << 2
1100 l.lwz r3,0x0(r3) // this is pte at last
1102 * if (!pte_present(pte))
1106 l.sfne r4,r0 // is pte present
1107 l.bnf i_pte_not_present
1108 l.addi r4,r0,0xffffe03a // PAGE_MASK | ITLB_UP_CONVERT_MASK
1110 * fill ITLB TR register
1112 l.and r4,r3,r4 // apply the mask
1113 l.andi r3,r3,0x7c0 // _PAGE_EXEC | _PAGE_SRE | _PAGE_SWE | _PAGE_URE | _PAGE_UWE
1115 l.bf itlb_tr_fill //_workaround
1116 // Determine number of IMMU sets
1117 l.mfspr r2, r0, SPR_IMMUCFGR
1118 l.andi r2, r2, SPR_IMMUCFGR_NTS
1119 l.srli r2, r2, SPR_IMMUCFGR_NTS_OFF
1121 l.sll r3, r3, r2 // r3 = number IMMU sets IMMUCFGR
1122 l.addi r2, r3, -1 // r2 = nsets mask
1123 l.mfspr r3, r0, SPR_EEAR_BASE
1124 l.srli r3, r3, 0xd // >> PAGE_SHIFT
1125 l.and r2, r3, r2 // calc offset: & (NUM_TLB_ENTRIES-1)
1129 * we should not just blindly set executable flags,
1130 * but it does help with ping. the clean way would be to find out
1131 * (and fix it) why stack doesn't have execution permissions
1134 itlb_tr_fill_workaround:
1135 l.ori r4,r4,0xc0 // | (SPR_ITLBTR_UXE | ITLBTR_SXE)
1137 l.mtspr r2,r4,SPR_ITLBTR_BASE(0)
1139 * fill DTLB MR register
1141 l.slli r3, r3, 0xd /* << PAGE_SHIFT => EA & PAGE_MASK */
1142 l.ori r4,r3,0x1 // set hardware valid bit: ITBL_MR entry
1143 l.mtspr r2,r4,SPR_ITLBMR_BASE(0)
1155 EXCEPTION_HANDLE(_itlb_miss_page_fault_handler)
1157 /* ==============================================[ boot tlb handlers ]=== */
1160 /* =================================================[ debugging aids ]=== */
1165 _immu_trampoline_top:
1167 #define TRAMP_SLOT_0 (0x0)
1168 #define TRAMP_SLOT_1 (0x4)
1169 #define TRAMP_SLOT_2 (0x8)
1170 #define TRAMP_SLOT_3 (0xc)
1171 #define TRAMP_SLOT_4 (0x10)
1172 #define TRAMP_SLOT_5 (0x14)
1173 #define TRAMP_FRAME_SIZE (0x18)
1175 ENTRY(_immu_trampoline_workaround)
1177 // r6 is physical EEA
1180 LOAD_SYMBOL_2_GPR(r5,_immu_trampoline)
1181 tophys (r3,r5) // r3 is trampoline (physical)
1183 LOAD_SYMBOL_2_GPR(r4,0x15000000)
1184 l.sw TRAMP_SLOT_0(r3),r4
1185 l.sw TRAMP_SLOT_1(r3),r4
1186 l.sw TRAMP_SLOT_4(r3),r4
1187 l.sw TRAMP_SLOT_5(r3),r4
1190 l.lwz r4,0x0(r6) // load op @ EEA + 0x0 (fc address)
1191 l.sw TRAMP_SLOT_3(r3),r4 // store it to _immu_trampoline_data
1192 l.lwz r4,-0x4(r6) // load op @ EEA - 0x4 (f8 address)
1193 l.sw TRAMP_SLOT_2(r3),r4 // store it to _immu_trampoline_data
1195 l.srli r5,r4,26 // check opcode for write access
1198 l.sfeqi r5,0x11 // l.jr
1200 l.sfeqi r5,1 // l.jal
1202 l.sfeqi r5,0x12 // l.jalr
1204 l.sfeqi r5,3 // l.bnf
1206 l.sfeqi r5,4 // l.bf
1210 l.j 99b // should never happen
1214 // r3 is trampoline address (physical)
1215 // r4 is instruction
1216 // r6 is physical(EEA)
1222 /* 19 20 aa aa l.movhi r9,0xaaaa
1223 * a9 29 bb bb l.ori r9,0xbbbb
1225 * where 0xaaaabbbb is EEA + 0x4 shifted right 2
1228 l.addi r6,r2,0x4 // this is 0xaaaabbbb
1230 // l.movhi r9,0xaaaa
1231 l.ori r5,r0,0x1920 // 0x1920 == l.movhi r9
1232 l.sh (TRAMP_SLOT_0+0x0)(r3),r5
1234 l.sh (TRAMP_SLOT_0+0x2)(r3),r5
1237 l.ori r5,r0,0xa929 // 0xa929 == l.ori r9
1238 l.sh (TRAMP_SLOT_1+0x0)(r3),r5
1240 l.sh (TRAMP_SLOT_1+0x2)(r3),r5
1242 /* falthrough, need to set up new jump offset */
1246 l.slli r6,r4,6 // original offset shifted left 6 - 2
1247 // l.srli r6,r6,6 // original offset shifted right 2
1249 l.slli r4,r2,4 // old jump position: EEA shifted left 4
1250 // l.srli r4,r4,6 // old jump position: shifted right 2
1252 l.addi r5,r3,0xc // new jump position (physical)
1253 l.slli r5,r5,4 // new jump position: shifted left 4
1255 // calculate new jump offset
1256 // new_off = old_off + (old_jump - new_jump)
1258 l.sub r5,r4,r5 // old_jump - new_jump
1259 l.add r5,r6,r5 // orig_off + (old_jump - new_jump)
1260 l.srli r5,r5,6 // new offset shifted right 2
1262 // r5 is new jump offset
1263 // l.j has opcode 0x0...
1264 l.sw TRAMP_SLOT_2(r3),r5 // write it back
1269 /* ----------------------------- */
1273 /* 19 20 aa aa l.movhi r9,0xaaaa
1274 * a9 29 bb bb l.ori r9,0xbbbb
1276 * where 0xaaaabbbb is EEA + 0x4 shifted right 2
1279 l.addi r6,r2,0x4 // this is 0xaaaabbbb
1281 // l.movhi r9,0xaaaa
1282 l.ori r5,r0,0x1920 // 0x1920 == l.movhi r9
1283 l.sh (TRAMP_SLOT_0+0x0)(r3),r5
1285 l.sh (TRAMP_SLOT_0+0x2)(r3),r5
1288 l.ori r5,r0,0xa929 // 0xa929 == l.ori r9
1289 l.sh (TRAMP_SLOT_1+0x0)(r3),r5
1291 l.sh (TRAMP_SLOT_1+0x2)(r3),r5
1293 l.lhz r5,(TRAMP_SLOT_2+0x0)(r3) // load hi part of jump instruction
1294 l.andi r5,r5,0x3ff // clear out opcode part
1295 l.ori r5,r5,0x4400 // opcode changed from l.jalr -> l.jr
1296 l.sh (TRAMP_SLOT_2+0x0)(r3),r5 // write it back
1304 /* ----------------------------- */
1308 l.slli r6,r4,6 // original offset shifted left 6 - 2
1309 // l.srli r6,r6,6 // original offset shifted right 2
1311 l.slli r4,r2,4 // old jump position: EEA shifted left 4
1312 // l.srli r4,r4,6 // old jump position: shifted right 2
1314 l.addi r5,r3,0xc // new jump position (physical)
1315 l.slli r5,r5,4 // new jump position: shifted left 4
1317 // calculate new jump offset
1318 // new_off = old_off + (old_jump - new_jump)
1320 l.add r6,r6,r4 // (orig_off + old_jump)
1321 l.sub r6,r6,r5 // (orig_off + old_jump) - new_jump
1322 l.srli r6,r6,6 // new offset shifted right 2
1324 // r6 is new jump offset
1325 l.lwz r4,(TRAMP_SLOT_2+0x0)(r3) // load jump instruction
1327 l.andi r4,r4,0xfc00 // get opcode part
1329 l.or r6,r4,r6 // l.b(n)f new offset
1330 l.sw TRAMP_SLOT_2(r3),r6 // write it back
1332 /* we need to add l.j to EEA + 0x8 */
1333 tophys (r4,r2) // may not be needed (due to shifts down_
1334 l.addi r4,r4,(0x8 - 0x8) // jump target = r2 + 0x8 (compensate for 0x8)
1335 // jump position = r5 + 0x8 (0x8 compensated)
1336 l.sub r4,r4,r5 // jump offset = target - new_position + 0x8
1338 l.slli r4,r4,4 // the amount of info in imediate of jump
1339 l.srli r4,r4,6 // jump instruction with offset
1340 l.sw TRAMP_SLOT_4(r3),r4 // write it to 4th slot
1345 // set up new EPC to point to our trampoline code
1346 LOAD_SYMBOL_2_GPR(r5,_immu_trampoline)
1347 l.mtspr r0,r5,SPR_EPCR_BASE
1349 // immu_trampoline is (4x) CACHE_LINE aligned
1350 // and only 6 instructions long,
1351 // so we need to invalidate only 2 lines
1353 /* Establish cache block size
1356 r14 contain block size
1358 l.mfspr r21,r0,SPR_ICCFGR
1359 l.andi r21,r21,SPR_ICCFGR_CBS
1364 l.mtspr r0,r5,SPR_ICBIR
1366 l.mtspr r0,r5,SPR_ICBIR
1373 * DSCR: prints a string referenced by r3.
1375 * PRMS: r3 - address of the first character of null
1376 * terminated string to be printed
1378 * PREQ: UART at UART_BASE_ADD has to be initialized
1380 * POST: caller should be aware that r3, r9 are changed
1382 ENTRY(_emergency_print)
1383 EMERGENCY_PRINT_STORE_GPR4
1384 EMERGENCY_PRINT_STORE_GPR5
1385 EMERGENCY_PRINT_STORE_GPR6
1386 EMERGENCY_PRINT_STORE_GPR7
1394 l.movhi r4,hi(UART_BASE_ADD)
1412 /* next character */
1417 EMERGENCY_PRINT_LOAD_GPR7
1418 EMERGENCY_PRINT_LOAD_GPR6
1419 EMERGENCY_PRINT_LOAD_GPR5
1420 EMERGENCY_PRINT_LOAD_GPR4
1424 ENTRY(_emergency_print_nr)
1425 EMERGENCY_PRINT_STORE_GPR4
1426 EMERGENCY_PRINT_STORE_GPR5
1427 EMERGENCY_PRINT_STORE_GPR6
1428 EMERGENCY_PRINT_STORE_GPR7
1429 EMERGENCY_PRINT_STORE_GPR8
1431 l.addi r8,r0,32 // shift register
1433 1: /* remove leading zeros */
1438 /* don't skip the last zero if number == 0x0 */
1462 l.movhi r4,hi(UART_BASE_ADD)
1480 /* next character */
1485 EMERGENCY_PRINT_LOAD_GPR8
1486 EMERGENCY_PRINT_LOAD_GPR7
1487 EMERGENCY_PRINT_LOAD_GPR6
1488 EMERGENCY_PRINT_LOAD_GPR5
1489 EMERGENCY_PRINT_LOAD_GPR4
1495 * This should be used for debugging only.
1496 * It messes up the Linux early serial output
1497 * somehow, so use it sparingly and essentially
1498 * only if you need to debug something that goes wrong
1499 * before Linux gets the early serial going.
1501 * Furthermore, you'll have to make sure you set the
1502 * UART_DEVISOR correctly according to the system
1510 #define SYS_CLK 20000000
1511 //#define SYS_CLK 1843200
1512 #define OR32_CONSOLE_BAUD 115200
1513 #define UART_DIVISOR SYS_CLK/(16*OR32_CONSOLE_BAUD)
1515 ENTRY(_early_uart_init)
1516 l.movhi r3,hi(UART_BASE_ADD)
1530 l.addi r4,r0,((UART_DIVISOR>>8) & 0x000000ff)
1531 l.sb UART_DLM(r3),r4
1532 l.addi r4,r0,((UART_DIVISOR) & 0x000000ff)
1533 l.sb UART_DLL(r3),r4
1540 _string_unhandled_exception:
1541 .string "\n\rRunarunaround: Unhandled exception 0x\0"
1544 .string ": EPC=0x\0"
1550 /* ========================================[ page aligned structures ]=== */
1553 * .data section should be page aligned
1554 * (look into arch/openrisc/kernel/vmlinux.lds.S)
1558 .global empty_zero_page
1562 .global swapper_pg_dir
1566 .global _unhandled_stack
1569 _unhandled_stack_top:
1571 /* ============================================================[ EOF ]=== */