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 <asm/processor.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>
31 #define tophys(rd,rs) \
32 l.movhi rd,hi(-KERNELBASE) ;\
35 #define CLEAR_GPR(gpr) \
38 #define LOAD_SYMBOL_2_GPR(gpr,symbol) \
39 l.movhi gpr,hi(symbol) ;\
40 l.ori gpr,gpr,lo(symbol)
43 #define UART_BASE_ADD 0x90000000
45 #define EXCEPTION_SR (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_SM)
46 #define SYSCALL_SR (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_IEE | SPR_SR_TEE | SPR_SR_SM)
48 /* ============================================[ tmp store locations ]=== */
51 * emergency_print temporary stores
53 #define EMERGENCY_PRINT_STORE_GPR4 l.sw 0x20(r0),r4
54 #define EMERGENCY_PRINT_LOAD_GPR4 l.lwz r4,0x20(r0)
56 #define EMERGENCY_PRINT_STORE_GPR5 l.sw 0x24(r0),r5
57 #define EMERGENCY_PRINT_LOAD_GPR5 l.lwz r5,0x24(r0)
59 #define EMERGENCY_PRINT_STORE_GPR6 l.sw 0x28(r0),r6
60 #define EMERGENCY_PRINT_LOAD_GPR6 l.lwz r6,0x28(r0)
62 #define EMERGENCY_PRINT_STORE_GPR7 l.sw 0x2c(r0),r7
63 #define EMERGENCY_PRINT_LOAD_GPR7 l.lwz r7,0x2c(r0)
65 #define EMERGENCY_PRINT_STORE_GPR8 l.sw 0x30(r0),r8
66 #define EMERGENCY_PRINT_LOAD_GPR8 l.lwz r8,0x30(r0)
68 #define EMERGENCY_PRINT_STORE_GPR9 l.sw 0x34(r0),r9
69 #define EMERGENCY_PRINT_LOAD_GPR9 l.lwz r9,0x34(r0)
73 * TLB miss handlers temorary stores
75 #define EXCEPTION_STORE_GPR9 l.sw 0x10(r0),r9
76 #define EXCEPTION_LOAD_GPR9 l.lwz r9,0x10(r0)
78 #define EXCEPTION_STORE_GPR2 l.sw 0x64(r0),r2
79 #define EXCEPTION_LOAD_GPR2 l.lwz r2,0x64(r0)
81 #define EXCEPTION_STORE_GPR3 l.sw 0x68(r0),r3
82 #define EXCEPTION_LOAD_GPR3 l.lwz r3,0x68(r0)
84 #define EXCEPTION_STORE_GPR4 l.sw 0x6c(r0),r4
85 #define EXCEPTION_LOAD_GPR4 l.lwz r4,0x6c(r0)
87 #define EXCEPTION_STORE_GPR5 l.sw 0x70(r0),r5
88 #define EXCEPTION_LOAD_GPR5 l.lwz r5,0x70(r0)
90 #define EXCEPTION_STORE_GPR6 l.sw 0x74(r0),r6
91 #define EXCEPTION_LOAD_GPR6 l.lwz r6,0x74(r0)
95 * EXCEPTION_HANDLE temporary stores
98 #define EXCEPTION_T_STORE_GPR30 l.sw 0x78(r0),r30
99 #define EXCEPTION_T_LOAD_GPR30(reg) l.lwz reg,0x78(r0)
101 #define EXCEPTION_T_STORE_GPR10 l.sw 0x7c(r0),r10
102 #define EXCEPTION_T_LOAD_GPR10(reg) l.lwz reg,0x7c(r0)
104 #define EXCEPTION_T_STORE_SP l.sw 0x80(r0),r1
105 #define EXCEPTION_T_LOAD_SP(reg) l.lwz reg,0x80(r0)
108 * For UNHANLDED_EXCEPTION
111 #define EXCEPTION_T_STORE_GPR31 l.sw 0x84(r0),r31
112 #define EXCEPTION_T_LOAD_GPR31(reg) l.lwz reg,0x84(r0)
114 /* =========================================================[ macros ]=== */
117 #define GET_CURRENT_PGD(reg,t1) \
118 LOAD_SYMBOL_2_GPR(reg,current_pgd) ;\
124 * DSCR: this is a common hook for handling exceptions. it will save
125 * the needed registers, set up stack and pointer to current
126 * then jump to the handler while enabling MMU
128 * PRMS: handler - a function to jump to. it has to save the
129 * remaining registers to kernel stack, call
130 * appropriate arch-independant exception handler
131 * and finaly jump to ret_from_except
133 * PREQ: unchanged state from the time exception happened
135 * POST: SAVED the following registers original value
136 * to the new created exception frame pointed to by r1
138 * r1 - ksp pointing to the new (exception) frame
139 * r4 - EEAR exception EA
140 * r10 - current pointing to current_thread_info struct
141 * r12 - syscall 0, since we didn't come from syscall
142 * r13 - temp it actually contains new SR, not needed anymore
143 * r31 - handler address of the handler we'll jump to
145 * handler has to save remaining registers to the exception
146 * ksp frame *before* tainting them!
148 * NOTE: this function is not reentrant per se. reentrancy is guaranteed
149 * by processor disabling all exceptions/interrupts when exception
152 * OPTM: no need to make it so wasteful to extract ksp when in user mode
155 #define EXCEPTION_HANDLE(handler) \
156 EXCEPTION_T_STORE_GPR30 ;\
157 l.mfspr r30,r0,SPR_ESR_BASE ;\
158 l.andi r30,r30,SPR_SR_SM ;\
160 EXCEPTION_T_STORE_GPR10 ;\
161 l.bnf 2f /* kernel_mode */ ;\
162 EXCEPTION_T_STORE_SP /* delay slot */ ;\
163 1: /* user_mode: */ ;\
164 LOAD_SYMBOL_2_GPR(r1,current_thread_info_set) ;\
166 /* r10: current_thread_info */ ;\
169 l.lwz r1,(TI_KSP)(r30) ;\
170 /* fall through */ ;\
171 2: /* kernel_mode: */ ;\
172 /* create new stack frame, save only needed gprs */ ;\
173 /* r1: KSP, r10: current, r4: EEAR, r31: __pa(KSP) */ ;\
174 /* r12: temp, syscall indicator */ ;\
175 l.addi r1,r1,-(INT_FRAME_SIZE) ;\
176 /* r1 is KSP, r30 is __pa(KSP) */ ;\
178 l.sw PT_GPR12(r30),r12 ;\
179 l.mfspr r12,r0,SPR_EPCR_BASE ;\
180 l.sw PT_PC(r30),r12 ;\
181 l.mfspr r12,r0,SPR_ESR_BASE ;\
182 l.sw PT_SR(r30),r12 ;\
184 EXCEPTION_T_LOAD_GPR30(r12) ;\
185 l.sw PT_GPR30(r30),r12 ;\
186 /* save r10 as was prior to exception */ ;\
187 EXCEPTION_T_LOAD_GPR10(r12) ;\
188 l.sw PT_GPR10(r30),r12 ;\
189 /* save PT_SP as was prior to exception */ ;\
190 EXCEPTION_T_LOAD_SP(r12) ;\
191 l.sw PT_SP(r30),r12 ;\
192 /* save exception r4, set r4 = EA */ ;\
193 l.sw PT_GPR4(r30),r4 ;\
194 l.mfspr r4,r0,SPR_EEAR_BASE ;\
195 /* r12 == 1 if we come from syscall */ ;\
197 /* ----- turn on MMU ----- */ ;\
198 l.ori r30,r0,(EXCEPTION_SR) ;\
199 l.mtspr r0,r30,SPR_ESR_BASE ;\
200 /* r30: EA address of handler */ ;\
201 LOAD_SYMBOL_2_GPR(r30,handler) ;\
202 l.mtspr r0,r30,SPR_EPCR_BASE ;\
209 * #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
210 * #define UNHANDLED_EXCEPTION(handler) \
212 * l.mtspr r0,r3,SPR_SR ;\
213 * l.movhi r3,hi(0xf0000100) ;\
214 * l.ori r3,r3,lo(0xf0000100) ;\
221 /* DSCR: this is the same as EXCEPTION_HANDLE(), we are just
222 * a bit more carefull (if we have a PT_SP or current pointer
223 * corruption) and set them up from 'current_set'
226 #define UNHANDLED_EXCEPTION(handler) \
227 EXCEPTION_T_STORE_GPR31 ;\
228 EXCEPTION_T_STORE_GPR10 ;\
229 EXCEPTION_T_STORE_SP ;\
230 /* temporary store r3, r9 into r1, r10 */ ;\
233 /* the string referenced by r3 must be low enough */ ;\
234 l.jal _emergency_print ;\
235 l.ori r3,r0,lo(_string_unhandled_exception) ;\
236 l.mfspr r3,r0,SPR_NPC ;\
237 l.jal _emergency_print_nr ;\
238 l.andi r3,r3,0x1f00 ;\
239 /* the string referenced by r3 must be low enough */ ;\
240 l.jal _emergency_print ;\
241 l.ori r3,r0,lo(_string_epc_prefix) ;\
242 l.jal _emergency_print_nr ;\
243 l.mfspr r3,r0,SPR_EPCR_BASE ;\
244 l.jal _emergency_print ;\
245 l.ori r3,r0,lo(_string_nl) ;\
246 /* end of printing */ ;\
249 /* extract current, ksp from current_set */ ;\
250 LOAD_SYMBOL_2_GPR(r1,_unhandled_stack_top) ;\
251 LOAD_SYMBOL_2_GPR(r10,init_thread_union) ;\
252 /* create new stack frame, save only needed gprs */ ;\
253 /* r1: KSP, r10: current, r31: __pa(KSP) */ ;\
254 /* r12: temp, syscall indicator, r13 temp */ ;\
255 l.addi r1,r1,-(INT_FRAME_SIZE) ;\
256 /* r1 is KSP, r31 is __pa(KSP) */ ;\
258 l.sw PT_GPR12(r31),r12 ;\
259 l.mfspr r12,r0,SPR_EPCR_BASE ;\
260 l.sw PT_PC(r31),r12 ;\
261 l.mfspr r12,r0,SPR_ESR_BASE ;\
262 l.sw PT_SR(r31),r12 ;\
264 EXCEPTION_T_LOAD_GPR31(r12) ;\
265 l.sw PT_GPR31(r31),r12 ;\
266 /* save r10 as was prior to exception */ ;\
267 EXCEPTION_T_LOAD_GPR10(r12) ;\
268 l.sw PT_GPR10(r31),r12 ;\
269 /* save PT_SP as was prior to exception */ ;\
270 EXCEPTION_T_LOAD_SP(r12) ;\
271 l.sw PT_SP(r31),r12 ;\
272 l.sw PT_GPR13(r31),r13 ;\
274 /* save exception r4, set r4 = EA */ ;\
275 l.sw PT_GPR4(r31),r4 ;\
276 l.mfspr r4,r0,SPR_EEAR_BASE ;\
277 /* r12 == 1 if we come from syscall */ ;\
279 /* ----- play a MMU trick ----- */ ;\
280 l.ori r31,r0,(EXCEPTION_SR) ;\
281 l.mtspr r0,r31,SPR_ESR_BASE ;\
282 /* r31: EA address of handler */ ;\
283 LOAD_SYMBOL_2_GPR(r31,handler) ;\
284 l.mtspr r0,r31,SPR_EPCR_BASE ;\
287 /* =====================================================[ exceptions] === */
289 /* ---[ 0x100: RESET exception ]----------------------------------------- */
291 /* Jump to .init code at _start which lives in the .head section
292 * and will be discarded after boot.
294 LOAD_SYMBOL_2_GPR(r4, _start)
295 tophys (r3,r4) /* MMU disabled */
299 /* ---[ 0x200: BUS exception ]------------------------------------------- */
302 EXCEPTION_HANDLE(_bus_fault_handler)
304 /* ---[ 0x300: Data Page Fault exception ]------------------------------- */
306 _dispatch_do_dpage_fault:
307 // totaly disable timer interrupt
308 // l.mtspr r0,r0,SPR_TTMR
309 // DEBUG_TLB_PROBE(0x300)
310 // EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x300)
311 EXCEPTION_HANDLE(_data_page_fault_handler)
313 /* ---[ 0x400: Insn Page Fault exception ]------------------------------- */
315 _dispatch_do_ipage_fault:
316 // totaly disable timer interrupt
317 // l.mtspr r0,r0,SPR_TTMR
318 // DEBUG_TLB_PROBE(0x400)
319 // EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x400)
320 EXCEPTION_HANDLE(_insn_page_fault_handler)
322 /* ---[ 0x500: Timer exception ]----------------------------------------- */
324 EXCEPTION_HANDLE(_timer_handler)
326 /* ---[ 0x600: Aligment exception ]-------------------------------------- */
328 EXCEPTION_HANDLE(_alignment_handler)
330 /* ---[ 0x700: Illegal insn exception ]---------------------------------- */
332 EXCEPTION_HANDLE(_illegal_instruction_handler)
334 /* ---[ 0x800: External interrupt exception ]---------------------------- */
336 EXCEPTION_HANDLE(_external_irq_handler)
338 /* ---[ 0x900: DTLB miss exception ]------------------------------------- */
340 l.j boot_dtlb_miss_handler
343 /* ---[ 0xa00: ITLB miss exception ]------------------------------------- */
345 l.j boot_itlb_miss_handler
348 /* ---[ 0xb00: Range exception ]----------------------------------------- */
350 UNHANDLED_EXCEPTION(_vector_0xb00)
352 /* ---[ 0xc00: Syscall exception ]--------------------------------------- */
354 EXCEPTION_HANDLE(_sys_call_handler)
356 /* ---[ 0xd00: Trap exception ]------------------------------------------ */
358 UNHANDLED_EXCEPTION(_vector_0xd00)
360 /* ---[ 0xe00: Trap exception ]------------------------------------------ */
362 // UNHANDLED_EXCEPTION(_vector_0xe00)
363 EXCEPTION_HANDLE(_trap_handler)
365 /* ---[ 0xf00: Reserved exception ]-------------------------------------- */
367 UNHANDLED_EXCEPTION(_vector_0xf00)
369 /* ---[ 0x1000: Reserved exception ]------------------------------------- */
371 UNHANDLED_EXCEPTION(_vector_0x1000)
373 /* ---[ 0x1100: Reserved exception ]------------------------------------- */
375 UNHANDLED_EXCEPTION(_vector_0x1100)
377 /* ---[ 0x1200: Reserved exception ]------------------------------------- */
379 UNHANDLED_EXCEPTION(_vector_0x1200)
381 /* ---[ 0x1300: Reserved exception ]------------------------------------- */
383 UNHANDLED_EXCEPTION(_vector_0x1300)
385 /* ---[ 0x1400: Reserved exception ]------------------------------------- */
387 UNHANDLED_EXCEPTION(_vector_0x1400)
389 /* ---[ 0x1500: Reserved exception ]------------------------------------- */
391 UNHANDLED_EXCEPTION(_vector_0x1500)
393 /* ---[ 0x1600: Reserved exception ]------------------------------------- */
395 UNHANDLED_EXCEPTION(_vector_0x1600)
397 /* ---[ 0x1700: Reserved exception ]------------------------------------- */
399 UNHANDLED_EXCEPTION(_vector_0x1700)
401 /* ---[ 0x1800: Reserved exception ]------------------------------------- */
403 UNHANDLED_EXCEPTION(_vector_0x1800)
405 /* ---[ 0x1900: Reserved exception ]------------------------------------- */
407 UNHANDLED_EXCEPTION(_vector_0x1900)
409 /* ---[ 0x1a00: Reserved exception ]------------------------------------- */
411 UNHANDLED_EXCEPTION(_vector_0x1a00)
413 /* ---[ 0x1b00: Reserved exception ]------------------------------------- */
415 UNHANDLED_EXCEPTION(_vector_0x1b00)
417 /* ---[ 0x1c00: Reserved exception ]------------------------------------- */
419 UNHANDLED_EXCEPTION(_vector_0x1c00)
421 /* ---[ 0x1d00: Reserved exception ]------------------------------------- */
423 UNHANDLED_EXCEPTION(_vector_0x1d00)
425 /* ---[ 0x1e00: Reserved exception ]------------------------------------- */
427 UNHANDLED_EXCEPTION(_vector_0x1e00)
429 /* ---[ 0x1f00: Reserved exception ]------------------------------------- */
431 UNHANDLED_EXCEPTION(_vector_0x1f00)
434 /* ===================================================[ kernel start ]=== */
438 /* This early stuff belongs in HEAD, but some of the functions below definitely
444 /* save kernel parameters */
445 l.or r25,r0,r3 /* pointer to fdt */
448 * ensure a deterministic start
486 * set up initial ksp and current
488 LOAD_SYMBOL_2_GPR(r1,init_thread_union+0x2000) // setup kernel stack
489 LOAD_SYMBOL_2_GPR(r10,init_thread_union) // setup current
497 * .data contains initialized data,
498 * .bss contains uninitialized data - clear it up
501 LOAD_SYMBOL_2_GPR(r24, __bss_start)
502 LOAD_SYMBOL_2_GPR(r26, _end)
523 * I N V A L I D A T E T L B e n t r i e s
525 LOAD_SYMBOL_2_GPR(r5,SPR_DTLBMR_BASE(0))
526 LOAD_SYMBOL_2_GPR(r6,SPR_ITLBMR_BASE(0))
527 l.addi r7,r0,128 /* Maximum number of sets */
539 /* The MMU needs to be enabled before or32_early_setup is called */
544 * SR[5] = 0, SR[6] = 0, 6th and 7th bit of SR set to 0
546 l.mfspr r30,r0,SPR_SR
547 l.movhi r28,hi(SPR_SR_DME | SPR_SR_IME)
548 l.ori r28,r28,lo(SPR_SR_DME | SPR_SR_IME)
550 l.mtspr r0,r30,SPR_SR
568 // reset the simulation counters
571 /* check fdt header magic word */
572 l.lwz r3,0(r25) /* load magic from fdt into r3 */
573 l.movhi r4,hi(OF_DT_HEADER)
574 l.ori r4,r4,lo(OF_DT_HEADER)
578 /* magic number mismatch, set fdt pointer to null */
581 /* pass fdt pointer to or32_early_setup in r3 */
583 LOAD_SYMBOL_2_GPR(r24, or32_early_setup)
589 * clear all GPRS to increase determinism
623 * jump to kernel entry (start_kernel)
625 LOAD_SYMBOL_2_GPR(r30, start_kernel)
629 /* ========================================[ cache ]=== */
631 /* aligment here so we don't change memory offsets with
632 * memory controler defined
637 /* Check if IC present and skip enabling otherwise */
638 l.mfspr r24,r0,SPR_UPR
639 l.andi r26,r24,SPR_UPR_ICP
647 l.xori r5,r5,SPR_SR_ICE
651 /* Establish cache block size
654 r14 contain block size
656 l.mfspr r24,r0,SPR_ICCFGR
657 l.andi r26,r24,SPR_ICCFGR_CBS
662 /* Establish number of cache sets
663 r16 contains number of cache sets
664 r28 contains log(# of cache sets)
666 l.andi r26,r24,SPR_ICCFGR_NCS
676 // l.addi r5,r0,IC_SIZE
678 l.mtspr r0,r6,SPR_ICBIR
682 // l.addi r6,r6,IC_LINE
686 l.ori r6,r6,SPR_SR_ICE
703 /* Check if DC present and skip enabling otherwise */
704 l.mfspr r24,r0,SPR_UPR
705 l.andi r26,r24,SPR_UPR_DCP
713 l.xori r5,r5,SPR_SR_DCE
717 /* Establish cache block size
720 r14 contain block size
722 l.mfspr r24,r0,SPR_DCCFGR
723 l.andi r26,r24,SPR_DCCFGR_CBS
728 /* Establish number of cache sets
729 r16 contains number of cache sets
730 r28 contains log(# of cache sets)
732 l.andi r26,r24,SPR_DCCFGR_NCS
741 l.mtspr r0,r6,SPR_DCBIR
748 l.ori r6,r6,SPR_SR_DCE
754 /* ===============================================[ page table masks ]=== */
756 /* bit 4 is used in hardware as write back cache bit. we never use this bit
757 * explicitly, so we can reuse it as _PAGE_FILE bit and mask it out when
758 * writing into hardware pte's
761 #define DTLB_UP_CONVERT_MASK 0x3fa
762 #define ITLB_UP_CONVERT_MASK 0x3a
764 /* for SMP we'd have (this is a bit subtle, CC must be always set
765 * for SMP, but since we have _PAGE_PRESENT bit always defined
766 * we can just modify the mask)
768 #define DTLB_SMP_CONVERT_MASK 0x3fb
769 #define ITLB_SMP_CONVERT_MASK 0x3b
771 /* ---[ boot dtlb miss handler ]----------------------------------------- */
773 boot_dtlb_miss_handler:
775 /* mask for DTLB_MR register: - (0) sets V (valid) bit,
776 * - (31-12) sets bits belonging to VPN (31-12)
778 #define DTLB_MR_MASK 0xfffff001
780 /* mask for DTLB_TR register: - (2) sets CI (cache inhibit) bit,
781 * - (4) sets A (access) bit,
782 * - (5) sets D (dirty) bit,
783 * - (8) sets SRE (superuser read) bit
784 * - (9) sets SWE (superuser write) bit
785 * - (31-12) sets bits belonging to VPN (31-12)
787 #define DTLB_TR_MASK 0xfffff332
789 /* These are for masking out the VPN/PPN value from the MR/TR registers...
790 * it's not the same as the PFN */
791 #define VPN_MASK 0xfffff000
792 #define PPN_MASK 0xfffff000
798 l.mfspr r6,r0,SPR_ESR_BASE //
799 l.andi r6,r6,SPR_SR_SM // are we in kernel mode ?
800 l.sfeqi r6,0 // r6 == 0x1 --> SM
801 l.bf exit_with_no_dtranslation //
805 /* this could be optimized by moving storing of
806 * non r6 registers here, and jumping r6 restore
807 * if not in supervisor mode
815 l.mfspr r4,r0,SPR_EEAR_BASE // get the offending EA
817 immediate_translation:
820 l.srli r3,r4,0xd // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
822 l.mfspr r6, r0, SPR_DMMUCFGR
823 l.andi r6, r6, SPR_DMMUCFGR_NTS
824 l.srli r6, r6, SPR_DMMUCFGR_NTS_OFF
826 l.sll r5, r5, r6 // r5 = number DMMU sets
827 l.addi r6, r5, -1 // r6 = nsets mask
828 l.and r2, r3, r6 // r2 <- r3 % NSETS_MASK
830 l.or r6,r6,r4 // r6 <- r4
831 l.ori r6,r6,~(VPN_MASK) // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
832 l.movhi r5,hi(DTLB_MR_MASK) // r5 <- ffff:0000.x000
833 l.ori r5,r5,lo(DTLB_MR_MASK) // r5 <- ffff:1111.x001 - apply DTLB_MR_MASK
834 l.and r5,r5,r6 // r5 <- VPN :VPN .x001 - we have DTLBMR entry
835 l.mtspr r2,r5,SPR_DTLBMR_BASE(0) // set DTLBMR
837 /* set up DTLB with no translation for EA <= 0xbfffffff */
838 LOAD_SYMBOL_2_GPR(r6,0xbfffffff)
839 l.sfgeu r6,r4 // flag if r6 >= r4 (if 0xbfffffff >= EA)
841 l.and r3,r4,r4 // delay slot :: 24 <- r4 (if flag==1)
843 tophys(r3,r4) // r3 <- PA
845 l.ori r3,r3,~(PPN_MASK) // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
846 l.movhi r5,hi(DTLB_TR_MASK) // r5 <- ffff:0000.x000
847 l.ori r5,r5,lo(DTLB_TR_MASK) // r5 <- ffff:1111.x330 - apply DTLB_MR_MASK
848 l.and r5,r5,r3 // r5 <- PPN :PPN .x330 - we have DTLBTR entry
849 l.mtspr r2,r5,SPR_DTLBTR_BASE(0) // set DTLBTR
857 l.rfe // SR <- ESR, PC <- EPC
859 exit_with_no_dtranslation:
860 /* EA out of memory or not in supervisor mode */
863 l.j _dispatch_bus_fault
865 /* ---[ boot itlb miss handler ]----------------------------------------- */
867 boot_itlb_miss_handler:
869 /* mask for ITLB_MR register: - sets V (valid) bit,
870 * - sets bits belonging to VPN (15-12)
872 #define ITLB_MR_MASK 0xfffff001
874 /* mask for ITLB_TR register: - sets A (access) bit,
875 * - sets SXE (superuser execute) bit
876 * - sets bits belonging to VPN (15-12)
878 #define ITLB_TR_MASK 0xfffff050
881 #define VPN_MASK 0xffffe000
882 #define PPN_MASK 0xffffe000
894 l.mfspr r6,r0,SPR_ESR_BASE //
895 l.andi r6,r6,SPR_SR_SM // are we in kernel mode ?
896 l.sfeqi r6,0 // r6 == 0x1 --> SM
897 l.bf exit_with_no_itranslation
902 l.mfspr r4,r0,SPR_EEAR_BASE // get the offending EA
907 l.srli r3,r4,0xd // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
909 l.mfspr r6, r0, SPR_IMMUCFGR
910 l.andi r6, r6, SPR_IMMUCFGR_NTS
911 l.srli r6, r6, SPR_IMMUCFGR_NTS_OFF
913 l.sll r5, r5, r6 // r5 = number IMMU sets from IMMUCFGR
914 l.addi r6, r5, -1 // r6 = nsets mask
915 l.and r2, r3, r6 // r2 <- r3 % NSETS_MASK
917 l.or r6,r6,r4 // r6 <- r4
918 l.ori r6,r6,~(VPN_MASK) // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
919 l.movhi r5,hi(ITLB_MR_MASK) // r5 <- ffff:0000.x000
920 l.ori r5,r5,lo(ITLB_MR_MASK) // r5 <- ffff:1111.x001 - apply ITLB_MR_MASK
921 l.and r5,r5,r6 // r5 <- VPN :VPN .x001 - we have ITLBMR entry
922 l.mtspr r2,r5,SPR_ITLBMR_BASE(0) // set ITLBMR
925 * set up ITLB with no translation for EA <= 0x0fffffff
927 * we need this for head.S mapping (EA = PA). if we move all functions
928 * which run with mmu enabled into entry.S, we might be able to eliminate this.
931 LOAD_SYMBOL_2_GPR(r6,0x0fffffff)
932 l.sfgeu r6,r4 // flag if r6 >= r4 (if 0xb0ffffff >= EA)
934 l.and r3,r4,r4 // delay slot :: 24 <- r4 (if flag==1)
936 tophys(r3,r4) // r3 <- PA
938 l.ori r3,r3,~(PPN_MASK) // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
939 l.movhi r5,hi(ITLB_TR_MASK) // r5 <- ffff:0000.x000
940 l.ori r5,r5,lo(ITLB_TR_MASK) // r5 <- ffff:1111.x050 - apply ITLB_MR_MASK
941 l.and r5,r5,r3 // r5 <- PPN :PPN .x050 - we have ITLBTR entry
942 l.mtspr r2,r5,SPR_ITLBTR_BASE(0) // set ITLBTR
950 l.rfe // SR <- ESR, PC <- EPC
952 exit_with_no_itranslation:
955 l.j _dispatch_bus_fault
958 /* ====================================================================== */
960 * Stuff below here shouldn't go into .head section... maybe this stuff
961 * can be moved to entry.S ???
964 /* ==============================================[ DTLB miss handler ]=== */
968 * Exception handlers are entered with MMU off so the following handler
969 * needs to use physical addressing
974 ENTRY(dtlb_miss_handler)
983 l.mfspr r2,r0,SPR_EEAR_BASE
985 * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
987 GET_CURRENT_PGD(r3,r5) // r3 is current_pgd, r5 is temp
988 l.srli r4,r2,0x18 // >> PAGE_SHIFT + (PAGE_SHIFT - 2)
989 l.slli r4,r4,0x2 // to get address << 2
990 l.add r5,r4,r3 // r4 is pgd_index(daddr)
992 * if (pmd_none(*pmd))
996 l.lwz r3,0x0(r4) // get *pmd value
999 l.andi r3,r3,~PAGE_MASK //0x1fff // ~PAGE_MASK
1001 * if (pmd_bad(*pmd))
1005 // l.sfeq r3,r0 // check *pmd value
1007 l.addi r3,r0,0xffffe000 // PAGE_MASK
1009 // l.sw 0x0(r4),r0 // clear pmd
1012 * pte = *pte_offset(pmd, daddr);
1014 l.lwz r4,0x0(r4) // get **pmd value
1015 l.and r4,r4,r3 // & PAGE_MASK
1016 l.srli r5,r2,0xd // >> PAGE_SHIFT, r2 == EEAR
1017 l.andi r3,r5,0x7ff // (1UL << PAGE_SHIFT - 2) - 1
1018 l.slli r3,r3,0x2 // to get address << 2
1020 l.lwz r2,0x0(r3) // this is pte at last
1022 * if (!pte_present(pte))
1025 l.sfne r4,r0 // is pte present
1026 l.bnf d_pte_not_present
1027 l.addi r3,r0,0xffffe3fa // PAGE_MASK | DTLB_UP_CONVERT_MASK
1029 * fill DTLB TR register
1031 l.and r4,r2,r3 // apply the mask
1032 // Determine number of DMMU sets
1033 l.mfspr r6, r0, SPR_DMMUCFGR
1034 l.andi r6, r6, SPR_DMMUCFGR_NTS
1035 l.srli r6, r6, SPR_DMMUCFGR_NTS_OFF
1037 l.sll r3, r3, r6 // r3 = number DMMU sets DMMUCFGR
1038 l.addi r6, r3, -1 // r6 = nsets mask
1039 l.and r5, r5, r6 // calc offset: & (NUM_TLB_ENTRIES-1)
1041 l.mtspr r5,r4,SPR_DTLBTR_BASE(0)
1043 * fill DTLB MR register
1045 l.mfspr r2,r0,SPR_EEAR_BASE
1046 l.addi r3,r0,0xffffe000 // PAGE_MASK
1047 l.and r4,r2,r3 // apply PAGE_MASK to EA (__PHX__ do we really need this?)
1048 l.ori r4,r4,0x1 // set hardware valid bit: DTBL_MR entry
1049 l.mtspr r5,r4,SPR_DTLBMR_BASE(0)
1072 l.j _dispatch_do_dpage_fault
1075 /* ==============================================[ ITLB miss handler ]=== */
1076 ENTRY(itlb_miss_handler)
1077 EXCEPTION_STORE_GPR2
1078 EXCEPTION_STORE_GPR3
1079 EXCEPTION_STORE_GPR4
1080 EXCEPTION_STORE_GPR5
1081 EXCEPTION_STORE_GPR6
1083 * get EA of the miss
1085 l.mfspr r2,r0,SPR_EEAR_BASE
1088 * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
1091 GET_CURRENT_PGD(r3,r5) // r3 is current_pgd, r5 is temp
1092 l.srli r4,r2,0x18 // >> PAGE_SHIFT + (PAGE_SHIFT - 2)
1093 l.slli r4,r4,0x2 // to get address << 2
1094 l.add r5,r4,r3 // r4 is pgd_index(daddr)
1096 * if (pmd_none(*pmd))
1100 l.lwz r3,0x0(r4) // get *pmd value
1103 l.andi r3,r3,0x1fff // ~PAGE_MASK
1105 * if (pmd_bad(*pmd))
1110 // l.sfeq r3,r0 // check *pmd value
1112 l.addi r3,r0,0xffffe000 // PAGE_MASK
1114 // l.sw 0x0(r4),r0 // clear pmd
1118 * pte = *pte_offset(pmd, iaddr);
1121 l.lwz r4,0x0(r4) // get **pmd value
1122 l.and r4,r4,r3 // & PAGE_MASK
1123 l.srli r5,r2,0xd // >> PAGE_SHIFT, r2 == EEAR
1124 l.andi r3,r5,0x7ff // (1UL << PAGE_SHIFT - 2) - 1
1125 l.slli r3,r3,0x2 // to get address << 2
1127 l.lwz r2,0x0(r3) // this is pte at last
1129 * if (!pte_present(pte))
1133 l.sfne r4,r0 // is pte present
1134 l.bnf i_pte_not_present
1135 l.addi r3,r0,0xffffe03a // PAGE_MASK | ITLB_UP_CONVERT_MASK
1137 * fill ITLB TR register
1139 l.and r4,r2,r3 // apply the mask
1140 l.andi r3,r2,0x7c0 // _PAGE_EXEC | _PAGE_SRE | _PAGE_SWE | _PAGE_URE | _PAGE_UWE
1141 // l.andi r3,r2,0x400 // _PAGE_EXEC
1143 l.bf itlb_tr_fill //_workaround
1144 // Determine number of IMMU sets
1145 l.mfspr r6, r0, SPR_IMMUCFGR
1146 l.andi r6, r6, SPR_IMMUCFGR_NTS
1147 l.srli r6, r6, SPR_IMMUCFGR_NTS_OFF
1149 l.sll r3, r3, r6 // r3 = number IMMU sets IMMUCFGR
1150 l.addi r6, r3, -1 // r6 = nsets mask
1151 l.and r5, r5, r6 // calc offset: & (NUM_TLB_ENTRIES-1)
1155 * we should not just blindly set executable flags,
1156 * but it does help with ping. the clean way would be to find out
1157 * (and fix it) why stack doesn't have execution permissions
1160 itlb_tr_fill_workaround:
1161 l.ori r4,r4,0xc0 // | (SPR_ITLBTR_UXE | ITLBTR_SXE)
1163 l.mtspr r5,r4,SPR_ITLBTR_BASE(0)
1165 * fill DTLB MR register
1167 l.mfspr r2,r0,SPR_EEAR_BASE
1168 l.addi r3,r0,0xffffe000 // PAGE_MASK
1169 l.and r4,r2,r3 // apply PAGE_MASK to EA (__PHX__ do we really need this?)
1170 l.ori r4,r4,0x1 // set hardware valid bit: DTBL_MR entry
1171 l.mtspr r5,r4,SPR_ITLBMR_BASE(0)
1195 l.j _dispatch_do_ipage_fault
1198 /* ==============================================[ boot tlb handlers ]=== */
1201 /* =================================================[ debugging aids ]=== */
1206 _immu_trampoline_top:
1208 #define TRAMP_SLOT_0 (0x0)
1209 #define TRAMP_SLOT_1 (0x4)
1210 #define TRAMP_SLOT_2 (0x8)
1211 #define TRAMP_SLOT_3 (0xc)
1212 #define TRAMP_SLOT_4 (0x10)
1213 #define TRAMP_SLOT_5 (0x14)
1214 #define TRAMP_FRAME_SIZE (0x18)
1216 ENTRY(_immu_trampoline_workaround)
1218 // r6 is physical EEA
1221 LOAD_SYMBOL_2_GPR(r5,_immu_trampoline)
1222 tophys (r3,r5) // r3 is trampoline (physical)
1224 LOAD_SYMBOL_2_GPR(r4,0x15000000)
1225 l.sw TRAMP_SLOT_0(r3),r4
1226 l.sw TRAMP_SLOT_1(r3),r4
1227 l.sw TRAMP_SLOT_4(r3),r4
1228 l.sw TRAMP_SLOT_5(r3),r4
1231 l.lwz r4,0x0(r6) // load op @ EEA + 0x0 (fc address)
1232 l.sw TRAMP_SLOT_3(r3),r4 // store it to _immu_trampoline_data
1233 l.lwz r4,-0x4(r6) // load op @ EEA - 0x4 (f8 address)
1234 l.sw TRAMP_SLOT_2(r3),r4 // store it to _immu_trampoline_data
1236 l.srli r5,r4,26 // check opcode for write access
1239 l.sfeqi r5,0x11 // l.jr
1241 l.sfeqi r5,1 // l.jal
1243 l.sfeqi r5,0x12 // l.jalr
1245 l.sfeqi r5,3 // l.bnf
1247 l.sfeqi r5,4 // l.bf
1251 l.j 99b // should never happen
1255 // r3 is trampoline address (physical)
1256 // r4 is instruction
1257 // r6 is physical(EEA)
1263 /* 19 20 aa aa l.movhi r9,0xaaaa
1264 * a9 29 bb bb l.ori r9,0xbbbb
1266 * where 0xaaaabbbb is EEA + 0x4 shifted right 2
1269 l.addi r6,r2,0x4 // this is 0xaaaabbbb
1271 // l.movhi r9,0xaaaa
1272 l.ori r5,r0,0x1920 // 0x1920 == l.movhi r9
1273 l.sh (TRAMP_SLOT_0+0x0)(r3),r5
1275 l.sh (TRAMP_SLOT_0+0x2)(r3),r5
1278 l.ori r5,r0,0xa929 // 0xa929 == l.ori r9
1279 l.sh (TRAMP_SLOT_1+0x0)(r3),r5
1281 l.sh (TRAMP_SLOT_1+0x2)(r3),r5
1283 /* falthrough, need to set up new jump offset */
1287 l.slli r6,r4,6 // original offset shifted left 6 - 2
1288 // l.srli r6,r6,6 // original offset shifted right 2
1290 l.slli r4,r2,4 // old jump position: EEA shifted left 4
1291 // l.srli r4,r4,6 // old jump position: shifted right 2
1293 l.addi r5,r3,0xc // new jump position (physical)
1294 l.slli r5,r5,4 // new jump position: shifted left 4
1296 // calculate new jump offset
1297 // new_off = old_off + (old_jump - new_jump)
1299 l.sub r5,r4,r5 // old_jump - new_jump
1300 l.add r5,r6,r5 // orig_off + (old_jump - new_jump)
1301 l.srli r5,r5,6 // new offset shifted right 2
1303 // r5 is new jump offset
1304 // l.j has opcode 0x0...
1305 l.sw TRAMP_SLOT_2(r3),r5 // write it back
1310 /* ----------------------------- */
1314 /* 19 20 aa aa l.movhi r9,0xaaaa
1315 * a9 29 bb bb l.ori r9,0xbbbb
1317 * where 0xaaaabbbb is EEA + 0x4 shifted right 2
1320 l.addi r6,r2,0x4 // this is 0xaaaabbbb
1322 // l.movhi r9,0xaaaa
1323 l.ori r5,r0,0x1920 // 0x1920 == l.movhi r9
1324 l.sh (TRAMP_SLOT_0+0x0)(r3),r5
1326 l.sh (TRAMP_SLOT_0+0x2)(r3),r5
1329 l.ori r5,r0,0xa929 // 0xa929 == l.ori r9
1330 l.sh (TRAMP_SLOT_1+0x0)(r3),r5
1332 l.sh (TRAMP_SLOT_1+0x2)(r3),r5
1334 l.lhz r5,(TRAMP_SLOT_2+0x0)(r3) // load hi part of jump instruction
1335 l.andi r5,r5,0x3ff // clear out opcode part
1336 l.ori r5,r5,0x4400 // opcode changed from l.jalr -> l.jr
1337 l.sh (TRAMP_SLOT_2+0x0)(r3),r5 // write it back
1345 /* ----------------------------- */
1349 l.slli r6,r4,6 // original offset shifted left 6 - 2
1350 // l.srli r6,r6,6 // original offset shifted right 2
1352 l.slli r4,r2,4 // old jump position: EEA shifted left 4
1353 // l.srli r4,r4,6 // old jump position: shifted right 2
1355 l.addi r5,r3,0xc // new jump position (physical)
1356 l.slli r5,r5,4 // new jump position: shifted left 4
1358 // calculate new jump offset
1359 // new_off = old_off + (old_jump - new_jump)
1361 l.add r6,r6,r4 // (orig_off + old_jump)
1362 l.sub r6,r6,r5 // (orig_off + old_jump) - new_jump
1363 l.srli r6,r6,6 // new offset shifted right 2
1365 // r6 is new jump offset
1366 l.lwz r4,(TRAMP_SLOT_2+0x0)(r3) // load jump instruction
1368 l.andi r4,r4,0xfc00 // get opcode part
1370 l.or r6,r4,r6 // l.b(n)f new offset
1371 l.sw TRAMP_SLOT_2(r3),r6 // write it back
1373 /* we need to add l.j to EEA + 0x8 */
1374 tophys (r4,r2) // may not be needed (due to shifts down_
1375 l.addi r4,r4,(0x8 - 0x8) // jump target = r2 + 0x8 (compensate for 0x8)
1376 // jump position = r5 + 0x8 (0x8 compensated)
1377 l.sub r4,r4,r5 // jump offset = target - new_position + 0x8
1379 l.slli r4,r4,4 // the amount of info in imediate of jump
1380 l.srli r4,r4,6 // jump instruction with offset
1381 l.sw TRAMP_SLOT_4(r3),r4 // write it to 4th slot
1386 // set up new EPC to point to our trampoline code
1387 LOAD_SYMBOL_2_GPR(r5,_immu_trampoline)
1388 l.mtspr r0,r5,SPR_EPCR_BASE
1390 // immu_trampoline is (4x) CACHE_LINE aligned
1391 // and only 6 instructions long,
1392 // so we need to invalidate only 2 lines
1394 /* Establish cache block size
1397 r14 contain block size
1399 l.mfspr r21,r0,SPR_ICCFGR
1400 l.andi r21,r21,SPR_ICCFGR_CBS
1405 l.mtspr r0,r5,SPR_ICBIR
1407 l.mtspr r0,r5,SPR_ICBIR
1414 * DSCR: prints a string referenced by r3.
1416 * PRMS: r3 - address of the first character of null
1417 * terminated string to be printed
1419 * PREQ: UART at UART_BASE_ADD has to be initialized
1421 * POST: caller should be aware that r3, r9 are changed
1423 ENTRY(_emergency_print)
1424 EMERGENCY_PRINT_STORE_GPR4
1425 EMERGENCY_PRINT_STORE_GPR5
1426 EMERGENCY_PRINT_STORE_GPR6
1427 EMERGENCY_PRINT_STORE_GPR7
1435 l.movhi r4,hi(UART_BASE_ADD)
1453 /* next character */
1458 EMERGENCY_PRINT_LOAD_GPR7
1459 EMERGENCY_PRINT_LOAD_GPR6
1460 EMERGENCY_PRINT_LOAD_GPR5
1461 EMERGENCY_PRINT_LOAD_GPR4
1465 ENTRY(_emergency_print_nr)
1466 EMERGENCY_PRINT_STORE_GPR4
1467 EMERGENCY_PRINT_STORE_GPR5
1468 EMERGENCY_PRINT_STORE_GPR6
1469 EMERGENCY_PRINT_STORE_GPR7
1470 EMERGENCY_PRINT_STORE_GPR8
1472 l.addi r8,r0,32 // shift register
1474 1: /* remove leading zeros */
1479 /* don't skip the last zero if number == 0x0 */
1503 l.movhi r4,hi(UART_BASE_ADD)
1521 /* next character */
1526 EMERGENCY_PRINT_LOAD_GPR8
1527 EMERGENCY_PRINT_LOAD_GPR7
1528 EMERGENCY_PRINT_LOAD_GPR6
1529 EMERGENCY_PRINT_LOAD_GPR5
1530 EMERGENCY_PRINT_LOAD_GPR4
1536 * This should be used for debugging only.
1537 * It messes up the Linux early serial output
1538 * somehow, so use it sparingly and essentially
1539 * only if you need to debug something that goes wrong
1540 * before Linux gets the early serial going.
1542 * Furthermore, you'll have to make sure you set the
1543 * UART_DEVISOR correctly according to the system
1551 #define SYS_CLK 20000000
1552 //#define SYS_CLK 1843200
1553 #define OR32_CONSOLE_BAUD 115200
1554 #define UART_DIVISOR SYS_CLK/(16*OR32_CONSOLE_BAUD)
1556 ENTRY(_early_uart_init)
1557 l.movhi r3,hi(UART_BASE_ADD)
1571 l.addi r4,r0,((UART_DIVISOR>>8) & 0x000000ff)
1572 l.sb UART_DLM(r3),r4
1573 l.addi r4,r0,((UART_DIVISOR) & 0x000000ff)
1574 l.sb UART_DLL(r3),r4
1580 _string_copying_linux:
1581 .string "\n\n\n\n\n\rCopying Linux... \0"
1584 .string "Ok, booting the kernel.\n\r\0"
1586 _string_unhandled_exception:
1587 .string "\n\rRunarunaround: Unhandled exception 0x\0"
1590 .string ": EPC=0x\0"
1595 .global _string_esr_irq_bug
1596 _string_esr_irq_bug:
1597 .string "\n\rESR external interrupt bug, for details look into entry.S\n\r\0"
1601 /* ========================================[ page aligned structures ]=== */
1604 * .data section should be page aligned
1605 * (look into arch/or32/kernel/vmlinux.lds)
1609 .global empty_zero_page
1613 .global swapper_pg_dir
1617 .global _unhandled_stack
1620 _unhandled_stack_top:
1622 /* ============================================================[ EOF ]=== */