1 /* SPDX-License-Identifier: GPL-2.0-or-later */
5 * Linux architectural port borrowing liberally from similar works of
6 * others. All original copyrights apply as per the original source
9 * Modifications for the OpenRISC architecture:
10 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
11 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
14 #include <linux/linkage.h>
15 #include <linux/threads.h>
16 #include <linux/errno.h>
17 #include <linux/init.h>
18 #include <linux/serial_reg.h>
19 #include <linux/pgtable.h>
20 #include <asm/processor.h>
23 #include <asm/thread_info.h>
24 #include <asm/cache.h>
25 #include <asm/spr_defs.h>
26 #include <asm/asm-offsets.h>
27 #include <linux/of_fdt.h>
29 #define tophys(rd,rs) \
30 l.movhi rd,hi(-KERNELBASE) ;\
33 #define CLEAR_GPR(gpr) \
36 #define LOAD_SYMBOL_2_GPR(gpr,symbol) \
37 l.movhi gpr,hi(symbol) ;\
38 l.ori gpr,gpr,lo(symbol)
41 #define UART_BASE_ADD 0x90000000
43 #define EXCEPTION_SR (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_SM)
44 #define SYSCALL_SR (SPR_SR_DME | SPR_SR_IME | SPR_SR_DCE | SPR_SR_ICE | SPR_SR_IEE | SPR_SR_TEE | SPR_SR_SM)
46 /* ============================================[ tmp store locations ]=== */
48 #define SPR_SHADOW_GPR(x) ((x) + SPR_GPR_BASE + 32)
51 * emergency_print temporary stores
53 #ifdef CONFIG_OPENRISC_HAVE_SHADOW_GPRS
54 #define EMERGENCY_PRINT_STORE_GPR4 l.mtspr r0,r4,SPR_SHADOW_GPR(14)
55 #define EMERGENCY_PRINT_LOAD_GPR4 l.mfspr r4,r0,SPR_SHADOW_GPR(14)
57 #define EMERGENCY_PRINT_STORE_GPR5 l.mtspr r0,r5,SPR_SHADOW_GPR(15)
58 #define EMERGENCY_PRINT_LOAD_GPR5 l.mfspr r5,r0,SPR_SHADOW_GPR(15)
60 #define EMERGENCY_PRINT_STORE_GPR6 l.mtspr r0,r6,SPR_SHADOW_GPR(16)
61 #define EMERGENCY_PRINT_LOAD_GPR6 l.mfspr r6,r0,SPR_SHADOW_GPR(16)
63 #define EMERGENCY_PRINT_STORE_GPR7 l.mtspr r0,r7,SPR_SHADOW_GPR(7)
64 #define EMERGENCY_PRINT_LOAD_GPR7 l.mfspr r7,r0,SPR_SHADOW_GPR(7)
66 #define EMERGENCY_PRINT_STORE_GPR8 l.mtspr r0,r8,SPR_SHADOW_GPR(8)
67 #define EMERGENCY_PRINT_LOAD_GPR8 l.mfspr r8,r0,SPR_SHADOW_GPR(8)
69 #define EMERGENCY_PRINT_STORE_GPR9 l.mtspr r0,r9,SPR_SHADOW_GPR(9)
70 #define EMERGENCY_PRINT_LOAD_GPR9 l.mfspr r9,r0,SPR_SHADOW_GPR(9)
72 #else /* !CONFIG_OPENRISC_HAVE_SHADOW_GPRS */
73 #define EMERGENCY_PRINT_STORE_GPR4 l.sw 0x20(r0),r4
74 #define EMERGENCY_PRINT_LOAD_GPR4 l.lwz r4,0x20(r0)
76 #define EMERGENCY_PRINT_STORE_GPR5 l.sw 0x24(r0),r5
77 #define EMERGENCY_PRINT_LOAD_GPR5 l.lwz r5,0x24(r0)
79 #define EMERGENCY_PRINT_STORE_GPR6 l.sw 0x28(r0),r6
80 #define EMERGENCY_PRINT_LOAD_GPR6 l.lwz r6,0x28(r0)
82 #define EMERGENCY_PRINT_STORE_GPR7 l.sw 0x2c(r0),r7
83 #define EMERGENCY_PRINT_LOAD_GPR7 l.lwz r7,0x2c(r0)
85 #define EMERGENCY_PRINT_STORE_GPR8 l.sw 0x30(r0),r8
86 #define EMERGENCY_PRINT_LOAD_GPR8 l.lwz r8,0x30(r0)
88 #define EMERGENCY_PRINT_STORE_GPR9 l.sw 0x34(r0),r9
89 #define EMERGENCY_PRINT_LOAD_GPR9 l.lwz r9,0x34(r0)
94 * TLB miss handlers temorary stores
96 #ifdef CONFIG_OPENRISC_HAVE_SHADOW_GPRS
97 #define EXCEPTION_STORE_GPR2 l.mtspr r0,r2,SPR_SHADOW_GPR(2)
98 #define EXCEPTION_LOAD_GPR2 l.mfspr r2,r0,SPR_SHADOW_GPR(2)
100 #define EXCEPTION_STORE_GPR3 l.mtspr r0,r3,SPR_SHADOW_GPR(3)
101 #define EXCEPTION_LOAD_GPR3 l.mfspr r3,r0,SPR_SHADOW_GPR(3)
103 #define EXCEPTION_STORE_GPR4 l.mtspr r0,r4,SPR_SHADOW_GPR(4)
104 #define EXCEPTION_LOAD_GPR4 l.mfspr r4,r0,SPR_SHADOW_GPR(4)
106 #define EXCEPTION_STORE_GPR5 l.mtspr r0,r5,SPR_SHADOW_GPR(5)
107 #define EXCEPTION_LOAD_GPR5 l.mfspr r5,r0,SPR_SHADOW_GPR(5)
109 #define EXCEPTION_STORE_GPR6 l.mtspr r0,r6,SPR_SHADOW_GPR(6)
110 #define EXCEPTION_LOAD_GPR6 l.mfspr r6,r0,SPR_SHADOW_GPR(6)
112 #else /* !CONFIG_OPENRISC_HAVE_SHADOW_GPRS */
113 #define EXCEPTION_STORE_GPR2 l.sw 0x64(r0),r2
114 #define EXCEPTION_LOAD_GPR2 l.lwz r2,0x64(r0)
116 #define EXCEPTION_STORE_GPR3 l.sw 0x68(r0),r3
117 #define EXCEPTION_LOAD_GPR3 l.lwz r3,0x68(r0)
119 #define EXCEPTION_STORE_GPR4 l.sw 0x6c(r0),r4
120 #define EXCEPTION_LOAD_GPR4 l.lwz r4,0x6c(r0)
122 #define EXCEPTION_STORE_GPR5 l.sw 0x70(r0),r5
123 #define EXCEPTION_LOAD_GPR5 l.lwz r5,0x70(r0)
125 #define EXCEPTION_STORE_GPR6 l.sw 0x74(r0),r6
126 #define EXCEPTION_LOAD_GPR6 l.lwz r6,0x74(r0)
131 * EXCEPTION_HANDLE temporary stores
134 #ifdef CONFIG_OPENRISC_HAVE_SHADOW_GPRS
135 #define EXCEPTION_T_STORE_GPR30 l.mtspr r0,r30,SPR_SHADOW_GPR(30)
136 #define EXCEPTION_T_LOAD_GPR30(reg) l.mfspr reg,r0,SPR_SHADOW_GPR(30)
138 #define EXCEPTION_T_STORE_GPR10 l.mtspr r0,r10,SPR_SHADOW_GPR(10)
139 #define EXCEPTION_T_LOAD_GPR10(reg) l.mfspr reg,r0,SPR_SHADOW_GPR(10)
141 #define EXCEPTION_T_STORE_SP l.mtspr r0,r1,SPR_SHADOW_GPR(1)
142 #define EXCEPTION_T_LOAD_SP(reg) l.mfspr reg,r0,SPR_SHADOW_GPR(1)
144 #else /* !CONFIG_OPENRISC_HAVE_SHADOW_GPRS */
145 #define EXCEPTION_T_STORE_GPR30 l.sw 0x78(r0),r30
146 #define EXCEPTION_T_LOAD_GPR30(reg) l.lwz reg,0x78(r0)
148 #define EXCEPTION_T_STORE_GPR10 l.sw 0x7c(r0),r10
149 #define EXCEPTION_T_LOAD_GPR10(reg) l.lwz reg,0x7c(r0)
151 #define EXCEPTION_T_STORE_SP l.sw 0x80(r0),r1
152 #define EXCEPTION_T_LOAD_SP(reg) l.lwz reg,0x80(r0)
155 /* =========================================================[ macros ]=== */
158 #define GET_CURRENT_PGD(reg,t1) \
159 LOAD_SYMBOL_2_GPR(reg,current_pgd) ;\
160 l.mfspr t1,r0,SPR_COREID ;\
166 #define GET_CURRENT_PGD(reg,t1) \
167 LOAD_SYMBOL_2_GPR(reg,current_pgd) ;\
172 /* Load r10 from current_thread_info_set - clobbers r1 and r30 */
174 #define GET_CURRENT_THREAD_INFO \
175 LOAD_SYMBOL_2_GPR(r1,current_thread_info_set) ;\
177 l.mfspr r10,r0,SPR_COREID ;\
180 /* r10: current_thread_info */ ;\
183 #define GET_CURRENT_THREAD_INFO \
184 LOAD_SYMBOL_2_GPR(r1,current_thread_info_set) ;\
186 /* r10: current_thread_info */ ;\
191 * DSCR: this is a common hook for handling exceptions. it will save
192 * the needed registers, set up stack and pointer to current
193 * then jump to the handler while enabling MMU
195 * PRMS: handler - a function to jump to. it has to save the
196 * remaining registers to kernel stack, call
197 * appropriate arch-independant exception handler
198 * and finaly jump to ret_from_except
200 * PREQ: unchanged state from the time exception happened
202 * POST: SAVED the following registers original value
203 * to the new created exception frame pointed to by r1
205 * r1 - ksp pointing to the new (exception) frame
206 * r4 - EEAR exception EA
207 * r10 - current pointing to current_thread_info struct
208 * r12 - syscall 0, since we didn't come from syscall
209 * r30 - handler address of the handler we'll jump to
211 * handler has to save remaining registers to the exception
212 * ksp frame *before* tainting them!
214 * NOTE: this function is not reentrant per se. reentrancy is guaranteed
215 * by processor disabling all exceptions/interrupts when exception
218 * OPTM: no need to make it so wasteful to extract ksp when in user mode
221 #define EXCEPTION_HANDLE(handler) \
222 EXCEPTION_T_STORE_GPR30 ;\
223 l.mfspr r30,r0,SPR_ESR_BASE ;\
224 l.andi r30,r30,SPR_SR_SM ;\
226 EXCEPTION_T_STORE_GPR10 ;\
227 l.bnf 2f /* kernel_mode */ ;\
228 EXCEPTION_T_STORE_SP /* delay slot */ ;\
229 1: /* user_mode: */ ;\
230 GET_CURRENT_THREAD_INFO ;\
232 l.lwz r1,(TI_KSP)(r30) ;\
233 /* fall through */ ;\
234 2: /* kernel_mode: */ ;\
235 /* create new stack frame, save only needed gprs */ ;\
236 /* r1: KSP, r10: current, r4: EEAR, r31: __pa(KSP) */ ;\
237 /* r12: temp, syscall indicator */ ;\
238 l.addi r1,r1,-(INT_FRAME_SIZE) ;\
239 /* r1 is KSP, r30 is __pa(KSP) */ ;\
241 l.sw PT_GPR12(r30),r12 ;\
242 /* r4 use for tmp before EA */ ;\
243 l.mfspr r12,r0,SPR_EPCR_BASE ;\
244 l.sw PT_PC(r30),r12 ;\
245 l.mfspr r12,r0,SPR_ESR_BASE ;\
246 l.sw PT_SR(r30),r12 ;\
248 EXCEPTION_T_LOAD_GPR30(r12) ;\
249 l.sw PT_GPR30(r30),r12 ;\
250 /* save r10 as was prior to exception */ ;\
251 EXCEPTION_T_LOAD_GPR10(r12) ;\
252 l.sw PT_GPR10(r30),r12 ;\
253 /* save PT_SP as was prior to exception */ ;\
254 EXCEPTION_T_LOAD_SP(r12) ;\
255 l.sw PT_SP(r30),r12 ;\
256 /* save exception r4, set r4 = EA */ ;\
257 l.sw PT_GPR4(r30),r4 ;\
258 l.mfspr r4,r0,SPR_EEAR_BASE ;\
259 /* r12 == 1 if we come from syscall */ ;\
261 /* ----- turn on MMU ----- */ ;\
262 /* Carry DSX into exception SR */ ;\
263 l.mfspr r30,r0,SPR_SR ;\
264 l.andi r30,r30,SPR_SR_DSX ;\
265 l.ori r30,r30,(EXCEPTION_SR) ;\
266 l.mtspr r0,r30,SPR_ESR_BASE ;\
267 /* r30: EA address of handler */ ;\
268 LOAD_SYMBOL_2_GPR(r30,handler) ;\
269 l.mtspr r0,r30,SPR_EPCR_BASE ;\
276 * #ifdef CONFIG_JUMP_UPON_UNHANDLED_EXCEPTION
277 * #define UNHANDLED_EXCEPTION(handler) \
279 * l.mtspr r0,r3,SPR_SR ;\
280 * l.movhi r3,hi(0xf0000100) ;\
281 * l.ori r3,r3,lo(0xf0000100) ;\
288 /* DSCR: this is the same as EXCEPTION_HANDLE(), we are just
289 * a bit more carefull (if we have a PT_SP or current pointer
290 * corruption) and set them up from 'current_set'
293 #define UNHANDLED_EXCEPTION(handler) \
294 EXCEPTION_T_STORE_GPR30 ;\
295 EXCEPTION_T_STORE_GPR10 ;\
296 EXCEPTION_T_STORE_SP ;\
297 /* temporary store r3, r9 into r1, r10 */ ;\
300 LOAD_SYMBOL_2_GPR(r9,_string_unhandled_exception) ;\
302 l.jal _emergency_print ;\
304 l.mfspr r3,r0,SPR_NPC ;\
305 l.jal _emergency_print_nr ;\
306 l.andi r3,r3,0x1f00 ;\
307 LOAD_SYMBOL_2_GPR(r9,_string_epc_prefix) ;\
309 l.jal _emergency_print ;\
311 l.jal _emergency_print_nr ;\
312 l.mfspr r3,r0,SPR_EPCR_BASE ;\
313 LOAD_SYMBOL_2_GPR(r9,_string_nl) ;\
315 l.jal _emergency_print ;\
317 /* end of printing */ ;\
320 /* extract current, ksp from current_set */ ;\
321 LOAD_SYMBOL_2_GPR(r1,_unhandled_stack_top) ;\
322 LOAD_SYMBOL_2_GPR(r10,init_thread_union) ;\
323 /* create new stack frame, save only needed gprs */ ;\
324 /* r1: KSP, r10: current, r31: __pa(KSP) */ ;\
325 /* r12: temp, syscall indicator, r13 temp */ ;\
326 l.addi r1,r1,-(INT_FRAME_SIZE) ;\
327 /* r1 is KSP, r30 is __pa(KSP) */ ;\
329 l.sw PT_GPR12(r30),r12 ;\
330 l.mfspr r12,r0,SPR_EPCR_BASE ;\
331 l.sw PT_PC(r30),r12 ;\
332 l.mfspr r12,r0,SPR_ESR_BASE ;\
333 l.sw PT_SR(r30),r12 ;\
335 EXCEPTION_T_LOAD_GPR30(r12) ;\
336 l.sw PT_GPR30(r30),r12 ;\
337 /* save r10 as was prior to exception */ ;\
338 EXCEPTION_T_LOAD_GPR10(r12) ;\
339 l.sw PT_GPR10(r30),r12 ;\
340 /* save PT_SP as was prior to exception */ ;\
341 EXCEPTION_T_LOAD_SP(r12) ;\
342 l.sw PT_SP(r30),r12 ;\
343 l.sw PT_GPR13(r30),r13 ;\
345 /* save exception r4, set r4 = EA */ ;\
346 l.sw PT_GPR4(r30),r4 ;\
347 l.mfspr r4,r0,SPR_EEAR_BASE ;\
348 /* r12 == 1 if we come from syscall */ ;\
350 /* ----- play a MMU trick ----- */ ;\
351 l.ori r30,r0,(EXCEPTION_SR) ;\
352 l.mtspr r0,r30,SPR_ESR_BASE ;\
353 /* r31: EA address of handler */ ;\
354 LOAD_SYMBOL_2_GPR(r30,handler) ;\
355 l.mtspr r0,r30,SPR_EPCR_BASE ;\
358 /* =====================================================[ exceptions] === */
362 /* ---[ 0x100: RESET exception ]----------------------------------------- */
364 /* Jump to .init code at _start which lives in the .head section
365 * and will be discarded after boot.
367 LOAD_SYMBOL_2_GPR(r15, _start)
368 tophys (r13,r15) /* MMU disabled */
372 /* ---[ 0x200: BUS exception ]------------------------------------------- */
375 EXCEPTION_HANDLE(_bus_fault_handler)
377 /* ---[ 0x300: Data Page Fault exception ]------------------------------- */
379 _dispatch_do_dpage_fault:
380 // totaly disable timer interrupt
381 // l.mtspr r0,r0,SPR_TTMR
382 // DEBUG_TLB_PROBE(0x300)
383 // EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x300)
384 EXCEPTION_HANDLE(_data_page_fault_handler)
386 /* ---[ 0x400: Insn Page Fault exception ]------------------------------- */
388 _dispatch_do_ipage_fault:
389 // totaly disable timer interrupt
390 // l.mtspr r0,r0,SPR_TTMR
391 // DEBUG_TLB_PROBE(0x400)
392 // EXCEPTION_DEBUG_VALUE_ER_ENABLED(0x400)
393 EXCEPTION_HANDLE(_insn_page_fault_handler)
395 /* ---[ 0x500: Timer exception ]----------------------------------------- */
397 EXCEPTION_HANDLE(_timer_handler)
399 /* ---[ 0x600: Alignment exception ]------------------------------------- */
401 EXCEPTION_HANDLE(_alignment_handler)
403 /* ---[ 0x700: Illegal insn exception ]---------------------------------- */
405 EXCEPTION_HANDLE(_illegal_instruction_handler)
407 /* ---[ 0x800: External interrupt exception ]---------------------------- */
409 EXCEPTION_HANDLE(_external_irq_handler)
411 /* ---[ 0x900: DTLB miss exception ]------------------------------------- */
413 l.j boot_dtlb_miss_handler
416 /* ---[ 0xa00: ITLB miss exception ]------------------------------------- */
418 l.j boot_itlb_miss_handler
421 /* ---[ 0xb00: Range exception ]----------------------------------------- */
423 UNHANDLED_EXCEPTION(_vector_0xb00)
425 /* ---[ 0xc00: Syscall exception ]--------------------------------------- */
427 EXCEPTION_HANDLE(_sys_call_handler)
429 /* ---[ 0xd00: Floating point exception ]-------------------------------- */
431 EXCEPTION_HANDLE(_fpe_trap_handler)
433 /* ---[ 0xe00: Trap exception ]------------------------------------------ */
435 // UNHANDLED_EXCEPTION(_vector_0xe00)
436 EXCEPTION_HANDLE(_trap_handler)
438 /* ---[ 0xf00: Reserved exception ]-------------------------------------- */
440 UNHANDLED_EXCEPTION(_vector_0xf00)
442 /* ---[ 0x1000: Reserved exception ]------------------------------------- */
444 UNHANDLED_EXCEPTION(_vector_0x1000)
446 /* ---[ 0x1100: Reserved exception ]------------------------------------- */
448 UNHANDLED_EXCEPTION(_vector_0x1100)
450 /* ---[ 0x1200: Reserved exception ]------------------------------------- */
452 UNHANDLED_EXCEPTION(_vector_0x1200)
454 /* ---[ 0x1300: Reserved exception ]------------------------------------- */
456 UNHANDLED_EXCEPTION(_vector_0x1300)
458 /* ---[ 0x1400: Reserved exception ]------------------------------------- */
460 UNHANDLED_EXCEPTION(_vector_0x1400)
462 /* ---[ 0x1500: Reserved exception ]------------------------------------- */
464 UNHANDLED_EXCEPTION(_vector_0x1500)
466 /* ---[ 0x1600: Reserved exception ]------------------------------------- */
468 UNHANDLED_EXCEPTION(_vector_0x1600)
470 /* ---[ 0x1700: Reserved exception ]------------------------------------- */
472 UNHANDLED_EXCEPTION(_vector_0x1700)
474 /* ---[ 0x1800: Reserved exception ]------------------------------------- */
476 UNHANDLED_EXCEPTION(_vector_0x1800)
478 /* ---[ 0x1900: Reserved exception ]------------------------------------- */
480 UNHANDLED_EXCEPTION(_vector_0x1900)
482 /* ---[ 0x1a00: Reserved exception ]------------------------------------- */
484 UNHANDLED_EXCEPTION(_vector_0x1a00)
486 /* ---[ 0x1b00: Reserved exception ]------------------------------------- */
488 UNHANDLED_EXCEPTION(_vector_0x1b00)
490 /* ---[ 0x1c00: Reserved exception ]------------------------------------- */
492 UNHANDLED_EXCEPTION(_vector_0x1c00)
494 /* ---[ 0x1d00: Reserved exception ]------------------------------------- */
496 UNHANDLED_EXCEPTION(_vector_0x1d00)
498 /* ---[ 0x1e00: Reserved exception ]------------------------------------- */
500 UNHANDLED_EXCEPTION(_vector_0x1e00)
502 /* ---[ 0x1f00: Reserved exception ]------------------------------------- */
504 UNHANDLED_EXCEPTION(_vector_0x1f00)
507 /* ===================================================[ kernel start ]=== */
511 /* This early stuff belongs in the .init.text section, but some of the functions below definitely
517 /* Init r0 to zero as per spec */
520 /* save kernel parameters */
521 l.or r25,r0,r3 /* pointer to fdt */
524 * ensure a deterministic start
531 * Start the TTCR as early as possible, so that the RNG can make use of
532 * measurements of boot time from the earliest opportunity. Especially
533 * important is that the TTCR does not return zero by the time we reach
536 l.movhi r3,hi(SPR_TTMR_CR)
537 l.mtspr r0,r3,SPR_TTMR
571 l.mfspr r26,r0,SPR_COREID
577 * set up initial ksp and current
579 /* setup kernel stack */
580 LOAD_SYMBOL_2_GPR(r1,init_thread_union + THREAD_SIZE)
581 LOAD_SYMBOL_2_GPR(r10,init_thread_union) // setup current
589 * .data contains initialized data,
590 * .bss contains uninitialized data - clear it up
593 LOAD_SYMBOL_2_GPR(r24, __bss_start)
594 LOAD_SYMBOL_2_GPR(r26, _end)
617 /* The MMU needs to be enabled before or1k_early_setup is called */
622 * SR[5] = 0, SR[6] = 0, 6th and 7th bit of SR set to 0
624 l.mfspr r30,r0,SPR_SR
625 l.movhi r28,hi(SPR_SR_DME | SPR_SR_IME)
626 l.ori r28,r28,lo(SPR_SR_DME | SPR_SR_IME)
628 l.mtspr r0,r30,SPR_SR
646 // reset the simulation counters
649 /* check fdt header magic word */
650 l.lwz r3,0(r25) /* load magic from fdt into r3 */
651 l.movhi r4,hi(OF_DT_HEADER)
652 l.ori r4,r4,lo(OF_DT_HEADER)
656 /* magic number mismatch, set fdt pointer to null */
659 /* pass fdt pointer to or1k_early_setup in r3 */
661 LOAD_SYMBOL_2_GPR(r24, or1k_early_setup)
667 * clear all GPRS to increase determinism
701 * jump to kernel entry (start_kernel)
703 LOAD_SYMBOL_2_GPR(r30, start_kernel)
709 * I N V A L I D A T E T L B e n t r i e s
711 LOAD_SYMBOL_2_GPR(r5,SPR_DTLBMR_BASE(0))
712 LOAD_SYMBOL_2_GPR(r6,SPR_ITLBMR_BASE(0))
713 l.addi r7,r0,128 /* Maximum number of sets */
729 /* Doze the cpu until we are asked to run */
730 /* If we dont have power management skip doze */
731 l.mfspr r25,r0,SPR_UPR
732 l.andi r25,r25,SPR_UPR_PMP
734 l.bf secondary_check_release
737 /* Setup special secondary exception handler */
738 LOAD_SYMBOL_2_GPR(r3, _secondary_evbar)
740 l.mtspr r0,r25,SPR_EVBAR
742 /* Enable Interrupts */
743 l.mfspr r25,r0,SPR_SR
744 l.ori r25,r25,SPR_SR_IEE
745 l.mtspr r0,r25,SPR_SR
747 /* Unmask interrupts interrupts */
748 l.mfspr r25,r0,SPR_PICMR
750 l.mtspr r0,r25,SPR_PICMR
753 l.mfspr r25,r0,SPR_PMR
754 LOAD_SYMBOL_2_GPR(r3, SPR_PMR_DME)
756 l.mtspr r0,r25,SPR_PMR
758 /* Wakeup - Restore exception handler */
759 l.mtspr r0,r0,SPR_EVBAR
761 secondary_check_release:
763 * Check if we actually got the release signal, if not go-back to
766 l.mfspr r25,r0,SPR_COREID
767 LOAD_SYMBOL_2_GPR(r3, secondary_release)
773 /* fall through to secondary_init */
777 * set up initial ksp and current
779 LOAD_SYMBOL_2_GPR(r10, secondary_thread_info)
782 l.addi r1,r10,THREAD_SIZE
798 l.mfspr r30,r0,SPR_SR
799 l.movhi r28,hi(SPR_SR_DME | SPR_SR_IME)
800 l.ori r28,r28,lo(SPR_SR_DME | SPR_SR_IME)
803 * This is a bit tricky, we need to switch over from physical addresses
804 * to virtual addresses on the fly.
805 * To do that, we first set up ESR with the IME and DME bits set.
806 * Then EPCR is set to secondary_start and then a l.rfe is issued to
809 l.mtspr r0,r30,SPR_ESR_BASE
810 LOAD_SYMBOL_2_GPR(r30, secondary_start)
811 l.mtspr r0,r30,SPR_EPCR_BASE
815 LOAD_SYMBOL_2_GPR(r30, secondary_start_kernel)
821 /* ==========================================================[ cache ]=== */
823 /* alignment here so we don't change memory offsets with
824 * memory controller defined
829 /* Check if IC present and skip enabling otherwise */
830 l.mfspr r24,r0,SPR_UPR
831 l.andi r26,r24,SPR_UPR_ICP
839 l.xori r5,r5,SPR_SR_ICE
843 /* Establish cache block size
846 r14 contain block size
848 l.mfspr r24,r0,SPR_ICCFGR
849 l.andi r26,r24,SPR_ICCFGR_CBS
854 /* Establish number of cache sets
855 r16 contains number of cache sets
856 r28 contains log(# of cache sets)
858 l.andi r26,r24,SPR_ICCFGR_NCS
868 // l.addi r5,r0,IC_SIZE
870 l.mtspr r0,r6,SPR_ICBIR
874 // l.addi r6,r6,IC_LINE
878 l.ori r6,r6,SPR_SR_ICE
895 /* Check if DC present and skip enabling otherwise */
896 l.mfspr r24,r0,SPR_UPR
897 l.andi r26,r24,SPR_UPR_DCP
905 l.xori r5,r5,SPR_SR_DCE
909 /* Establish cache block size
912 r14 contain block size
914 l.mfspr r24,r0,SPR_DCCFGR
915 l.andi r26,r24,SPR_DCCFGR_CBS
920 /* Establish number of cache sets
921 r16 contains number of cache sets
922 r28 contains log(# of cache sets)
924 l.andi r26,r24,SPR_DCCFGR_NCS
933 l.mtspr r0,r6,SPR_DCBIR
940 l.ori r6,r6,SPR_SR_DCE
946 /* ===============================================[ page table masks ]=== */
948 #define DTLB_UP_CONVERT_MASK 0x3fa
949 #define ITLB_UP_CONVERT_MASK 0x3a
951 /* for SMP we'd have (this is a bit subtle, CC must be always set
952 * for SMP, but since we have _PAGE_PRESENT bit always defined
953 * we can just modify the mask)
955 #define DTLB_SMP_CONVERT_MASK 0x3fb
956 #define ITLB_SMP_CONVERT_MASK 0x3b
958 /* ---[ boot dtlb miss handler ]----------------------------------------- */
960 boot_dtlb_miss_handler:
962 /* mask for DTLB_MR register: - (0) sets V (valid) bit,
963 * - (31-12) sets bits belonging to VPN (31-12)
965 #define DTLB_MR_MASK 0xfffff001
967 /* mask for DTLB_TR register: - (2) sets CI (cache inhibit) bit,
968 * - (4) sets A (access) bit,
969 * - (5) sets D (dirty) bit,
970 * - (8) sets SRE (superuser read) bit
971 * - (9) sets SWE (superuser write) bit
972 * - (31-12) sets bits belonging to VPN (31-12)
974 #define DTLB_TR_MASK 0xfffff332
976 /* These are for masking out the VPN/PPN value from the MR/TR registers...
977 * it's not the same as the PFN */
978 #define VPN_MASK 0xfffff000
979 #define PPN_MASK 0xfffff000
985 l.mfspr r6,r0,SPR_ESR_BASE //
986 l.andi r6,r6,SPR_SR_SM // are we in kernel mode ?
987 l.sfeqi r6,0 // r6 == 0x1 --> SM
988 l.bf exit_with_no_dtranslation //
992 /* this could be optimized by moving storing of
993 * non r6 registers here, and jumping r6 restore
994 * if not in supervisor mode
1000 EXCEPTION_STORE_GPR5
1002 l.mfspr r4,r0,SPR_EEAR_BASE // get the offending EA
1004 immediate_translation:
1007 l.srli r3,r4,0xd // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
1009 l.mfspr r6, r0, SPR_DMMUCFGR
1010 l.andi r6, r6, SPR_DMMUCFGR_NTS
1011 l.srli r6, r6, SPR_DMMUCFGR_NTS_OFF
1013 l.sll r5, r5, r6 // r5 = number DMMU sets
1014 l.addi r6, r5, -1 // r6 = nsets mask
1015 l.and r2, r3, r6 // r2 <- r3 % NSETS_MASK
1017 l.or r6,r6,r4 // r6 <- r4
1018 l.ori r6,r6,~(VPN_MASK) // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
1019 l.movhi r5,hi(DTLB_MR_MASK) // r5 <- ffff:0000.x000
1020 l.ori r5,r5,lo(DTLB_MR_MASK) // r5 <- ffff:1111.x001 - apply DTLB_MR_MASK
1021 l.and r5,r5,r6 // r5 <- VPN :VPN .x001 - we have DTLBMR entry
1022 l.mtspr r2,r5,SPR_DTLBMR_BASE(0) // set DTLBMR
1024 /* set up DTLB with no translation for EA <= 0xbfffffff */
1025 LOAD_SYMBOL_2_GPR(r6,0xbfffffff)
1026 l.sfgeu r6,r4 // flag if r6 >= r4 (if 0xbfffffff >= EA)
1028 l.and r3,r4,r4 // delay slot :: 24 <- r4 (if flag==1)
1030 tophys(r3,r4) // r3 <- PA
1032 l.ori r3,r3,~(PPN_MASK) // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
1033 l.movhi r5,hi(DTLB_TR_MASK) // r5 <- ffff:0000.x000
1034 l.ori r5,r5,lo(DTLB_TR_MASK) // r5 <- ffff:1111.x330 - apply DTLB_MR_MASK
1035 l.and r5,r5,r3 // r5 <- PPN :PPN .x330 - we have DTLBTR entry
1036 l.mtspr r2,r5,SPR_DTLBTR_BASE(0) // set DTLBTR
1044 l.rfe // SR <- ESR, PC <- EPC
1046 exit_with_no_dtranslation:
1047 /* EA out of memory or not in supervisor mode */
1050 l.j _dispatch_bus_fault
1052 /* ---[ boot itlb miss handler ]----------------------------------------- */
1054 boot_itlb_miss_handler:
1056 /* mask for ITLB_MR register: - sets V (valid) bit,
1057 * - sets bits belonging to VPN (15-12)
1059 #define ITLB_MR_MASK 0xfffff001
1061 /* mask for ITLB_TR register: - sets A (access) bit,
1062 * - sets SXE (superuser execute) bit
1063 * - sets bits belonging to VPN (15-12)
1065 #define ITLB_TR_MASK 0xfffff050
1068 #define VPN_MASK 0xffffe000
1069 #define PPN_MASK 0xffffe000
1074 EXCEPTION_STORE_GPR2
1075 EXCEPTION_STORE_GPR3
1076 EXCEPTION_STORE_GPR4
1077 EXCEPTION_STORE_GPR5
1078 EXCEPTION_STORE_GPR6
1081 l.mfspr r6,r0,SPR_ESR_BASE //
1082 l.andi r6,r6,SPR_SR_SM // are we in kernel mode ?
1083 l.sfeqi r6,0 // r6 == 0x1 --> SM
1084 l.bf exit_with_no_itranslation
1089 l.mfspr r4,r0,SPR_EEAR_BASE // get the offending EA
1094 l.srli r3,r4,0xd // r3 <- r4 / 8192 (sets are relative to page size (8Kb) NOT VPN size (4Kb)
1096 l.mfspr r6, r0, SPR_IMMUCFGR
1097 l.andi r6, r6, SPR_IMMUCFGR_NTS
1098 l.srli r6, r6, SPR_IMMUCFGR_NTS_OFF
1100 l.sll r5, r5, r6 // r5 = number IMMU sets from IMMUCFGR
1101 l.addi r6, r5, -1 // r6 = nsets mask
1102 l.and r2, r3, r6 // r2 <- r3 % NSETS_MASK
1104 l.or r6,r6,r4 // r6 <- r4
1105 l.ori r6,r6,~(VPN_MASK) // r6 <- VPN :VPN .xfff - clear up lo(r6) to 0x**** *fff
1106 l.movhi r5,hi(ITLB_MR_MASK) // r5 <- ffff:0000.x000
1107 l.ori r5,r5,lo(ITLB_MR_MASK) // r5 <- ffff:1111.x001 - apply ITLB_MR_MASK
1108 l.and r5,r5,r6 // r5 <- VPN :VPN .x001 - we have ITLBMR entry
1109 l.mtspr r2,r5,SPR_ITLBMR_BASE(0) // set ITLBMR
1112 * set up ITLB with no translation for EA <= 0x0fffffff
1114 * we need this for head.S mapping (EA = PA). if we move all functions
1115 * which run with mmu enabled into entry.S, we might be able to eliminate this.
1118 LOAD_SYMBOL_2_GPR(r6,0x0fffffff)
1119 l.sfgeu r6,r4 // flag if r6 >= r4 (if 0xb0ffffff >= EA)
1121 l.and r3,r4,r4 // delay slot :: 24 <- r4 (if flag==1)
1123 tophys(r3,r4) // r3 <- PA
1125 l.ori r3,r3,~(PPN_MASK) // r3 <- PPN :PPN .xfff - clear up lo(r6) to 0x**** *fff
1126 l.movhi r5,hi(ITLB_TR_MASK) // r5 <- ffff:0000.x000
1127 l.ori r5,r5,lo(ITLB_TR_MASK) // r5 <- ffff:1111.x050 - apply ITLB_MR_MASK
1128 l.and r5,r5,r3 // r5 <- PPN :PPN .x050 - we have ITLBTR entry
1129 l.mtspr r2,r5,SPR_ITLBTR_BASE(0) // set ITLBTR
1137 l.rfe // SR <- ESR, PC <- EPC
1139 exit_with_no_itranslation:
1142 l.j _dispatch_bus_fault
1145 /* ====================================================================== */
1147 * Stuff below here shouldn't go into .head section... maybe this stuff
1148 * can be moved to entry.S ???
1151 /* ==============================================[ DTLB miss handler ]=== */
1155 * Exception handlers are entered with MMU off so the following handler
1156 * needs to use physical addressing
1161 ENTRY(dtlb_miss_handler)
1162 EXCEPTION_STORE_GPR2
1163 EXCEPTION_STORE_GPR3
1164 EXCEPTION_STORE_GPR4
1166 * get EA of the miss
1168 l.mfspr r2,r0,SPR_EEAR_BASE
1170 * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
1172 GET_CURRENT_PGD(r3,r4) // r3 is current_pgd, r4 is temp
1173 l.srli r4,r2,0x18 // >> PAGE_SHIFT + (PAGE_SHIFT - 2)
1174 l.slli r4,r4,0x2 // to get address << 2
1175 l.add r3,r4,r3 // r4 is pgd_index(daddr)
1177 * if (pmd_none(*pmd))
1181 l.lwz r3,0x0(r4) // get *pmd value
1184 l.addi r3,r0,0xffffe000 // PAGE_MASK
1188 * pte = *pte_offset(pmd, daddr);
1190 l.lwz r4,0x0(r4) // get **pmd value
1191 l.and r4,r4,r3 // & PAGE_MASK
1192 l.srli r2,r2,0xd // >> PAGE_SHIFT, r2 == EEAR
1193 l.andi r3,r2,0x7ff // (1UL << PAGE_SHIFT - 2) - 1
1194 l.slli r3,r3,0x2 // to get address << 2
1196 l.lwz r3,0x0(r3) // this is pte at last
1198 * if (!pte_present(pte))
1201 l.sfne r4,r0 // is pte present
1202 l.bnf d_pte_not_present
1203 l.addi r4,r0,0xffffe3fa // PAGE_MASK | DTLB_UP_CONVERT_MASK
1205 * fill DTLB TR register
1207 l.and r4,r3,r4 // apply the mask
1208 // Determine number of DMMU sets
1209 l.mfspr r2, r0, SPR_DMMUCFGR
1210 l.andi r2, r2, SPR_DMMUCFGR_NTS
1211 l.srli r2, r2, SPR_DMMUCFGR_NTS_OFF
1213 l.sll r3, r3, r2 // r3 = number DMMU sets DMMUCFGR
1214 l.addi r2, r3, -1 // r2 = nsets mask
1215 l.mfspr r3, r0, SPR_EEAR_BASE
1216 l.srli r3, r3, 0xd // >> PAGE_SHIFT
1217 l.and r2, r3, r2 // calc offset: & (NUM_TLB_ENTRIES-1)
1219 l.mtspr r2,r4,SPR_DTLBTR_BASE(0)
1221 * fill DTLB MR register
1223 l.slli r3, r3, 0xd /* << PAGE_SHIFT => EA & PAGE_MASK */
1224 l.ori r4,r3,0x1 // set hardware valid bit: DTBL_MR entry
1225 l.mtspr r2,r4,SPR_DTLBMR_BASE(0)
1236 EXCEPTION_HANDLE(_dtlb_miss_page_fault_handler)
1238 /* ==============================================[ ITLB miss handler ]=== */
1239 ENTRY(itlb_miss_handler)
1240 EXCEPTION_STORE_GPR2
1241 EXCEPTION_STORE_GPR3
1242 EXCEPTION_STORE_GPR4
1244 * get EA of the miss
1246 l.mfspr r2,r0,SPR_EEAR_BASE
1249 * pmd = (pmd_t *)(current_pgd + pgd_index(daddr));
1252 GET_CURRENT_PGD(r3,r4) // r3 is current_pgd, r5 is temp
1253 l.srli r4,r2,0x18 // >> PAGE_SHIFT + (PAGE_SHIFT - 2)
1254 l.slli r4,r4,0x2 // to get address << 2
1255 l.add r3,r4,r3 // r4 is pgd_index(daddr)
1257 * if (pmd_none(*pmd))
1261 l.lwz r3,0x0(r4) // get *pmd value
1264 l.addi r3,r0,0xffffe000 // PAGE_MASK
1268 * pte = *pte_offset(pmd, iaddr);
1271 l.lwz r4,0x0(r4) // get **pmd value
1272 l.and r4,r4,r3 // & PAGE_MASK
1273 l.srli r2,r2,0xd // >> PAGE_SHIFT, r2 == EEAR
1274 l.andi r3,r2,0x7ff // (1UL << PAGE_SHIFT - 2) - 1
1275 l.slli r3,r3,0x2 // to get address << 2
1277 l.lwz r3,0x0(r3) // this is pte at last
1279 * if (!pte_present(pte))
1283 l.sfne r4,r0 // is pte present
1284 l.bnf i_pte_not_present
1285 l.addi r4,r0,0xffffe03a // PAGE_MASK | ITLB_UP_CONVERT_MASK
1287 * fill ITLB TR register
1289 l.and r4,r3,r4 // apply the mask
1290 l.andi r3,r3,0x7c0 // _PAGE_EXEC | _PAGE_SRE | _PAGE_SWE | _PAGE_URE | _PAGE_UWE
1292 l.bf itlb_tr_fill //_workaround
1293 // Determine number of IMMU sets
1294 l.mfspr r2, r0, SPR_IMMUCFGR
1295 l.andi r2, r2, SPR_IMMUCFGR_NTS
1296 l.srli r2, r2, SPR_IMMUCFGR_NTS_OFF
1298 l.sll r3, r3, r2 // r3 = number IMMU sets IMMUCFGR
1299 l.addi r2, r3, -1 // r2 = nsets mask
1300 l.mfspr r3, r0, SPR_EEAR_BASE
1301 l.srli r3, r3, 0xd // >> PAGE_SHIFT
1302 l.and r2, r3, r2 // calc offset: & (NUM_TLB_ENTRIES-1)
1306 * we should not just blindly set executable flags,
1307 * but it does help with ping. the clean way would be to find out
1308 * (and fix it) why stack doesn't have execution permissions
1311 itlb_tr_fill_workaround:
1312 l.ori r4,r4,0xc0 // | (SPR_ITLBTR_UXE | ITLBTR_SXE)
1314 l.mtspr r2,r4,SPR_ITLBTR_BASE(0)
1316 * fill DTLB MR register
1318 l.slli r3, r3, 0xd /* << PAGE_SHIFT => EA & PAGE_MASK */
1319 l.ori r4,r3,0x1 // set hardware valid bit: ITBL_MR entry
1320 l.mtspr r2,r4,SPR_ITLBMR_BASE(0)
1332 EXCEPTION_HANDLE(_itlb_miss_page_fault_handler)
1334 /* ==============================================[ boot tlb handlers ]=== */
1337 /* =================================================[ debugging aids ]=== */
1340 * DESC: Prints ASCII character stored in r7
1342 * PRMS: r7 - a 32-bit value with an ASCII character in the first byte
1345 * PREQ: The UART at UART_BASE_ADD has to be initialized
1347 * POST: internally used but restores:
1348 * r4 - to store UART_BASE_ADD
1349 * r5 - for loading OFF_TXFULL / THRE,TEMT
1350 * r6 - for storing bitmask (SERIAL_8250)
1352 ENTRY(_emergency_putc)
1353 EMERGENCY_PRINT_STORE_GPR4
1354 EMERGENCY_PRINT_STORE_GPR5
1355 EMERGENCY_PRINT_STORE_GPR6
1357 l.movhi r4,hi(UART_BASE_ADD)
1358 l.ori r4,r4,lo(UART_BASE_ADD)
1360 #if defined(CONFIG_SERIAL_LITEUART)
1361 /* Check OFF_TXFULL status */
1368 /* Write character */
1371 #elif defined(CONFIG_SERIAL_8250)
1372 /* Check UART LSR THRE (hold) bit */
1380 /* Write character */
1383 /* Check UART LSR THRE|TEMT (hold, empty) bits */
1391 EMERGENCY_PRINT_LOAD_GPR6
1392 EMERGENCY_PRINT_LOAD_GPR5
1393 EMERGENCY_PRINT_LOAD_GPR4
1398 * DSCR: prints a string referenced by r3.
1400 * PRMS: r3 - address of the first character of null
1401 * terminated string to be printed
1403 * PREQ: UART at UART_BASE_ADD has to be initialized
1405 * POST: caller should be aware that r3, r9 are changed
1407 ENTRY(_emergency_print)
1408 EMERGENCY_PRINT_STORE_GPR7
1409 EMERGENCY_PRINT_STORE_GPR9
1411 /* Load character to r7, check for null terminator */
1417 l.jal _emergency_putc
1420 /* next character */
1425 EMERGENCY_PRINT_LOAD_GPR9
1426 EMERGENCY_PRINT_LOAD_GPR7
1431 * DSCR: prints a number in r3 in hex.
1433 * PRMS: r3 - a 32-bit unsigned integer
1435 * PREQ: UART at UART_BASE_ADD has to be initialized
1437 * POST: caller should be aware that r3, r9 are changed
1439 ENTRY(_emergency_print_nr)
1440 EMERGENCY_PRINT_STORE_GPR7
1441 EMERGENCY_PRINT_STORE_GPR8
1442 EMERGENCY_PRINT_STORE_GPR9
1444 l.addi r8,r0,32 // shift register
1446 1: /* remove leading zeros */
1451 /* don't skip the last zero if number == 0x0 */
1467 /* Numbers greater than 9 translate to a-f */
1473 /* Convert to ascii and output character */
1474 8: l.jal _emergency_putc
1477 /* next character */
1482 EMERGENCY_PRINT_LOAD_GPR9
1483 EMERGENCY_PRINT_LOAD_GPR8
1484 EMERGENCY_PRINT_LOAD_GPR7
1489 * This should be used for debugging only.
1490 * It messes up the Linux early serial output
1491 * somehow, so use it sparingly and essentially
1492 * only if you need to debug something that goes wrong
1493 * before Linux gets the early serial going.
1495 * Furthermore, you'll have to make sure you set the
1496 * UART_DEVISOR correctly according to the system
1504 #define SYS_CLK 20000000
1505 //#define SYS_CLK 1843200
1506 #define OR32_CONSOLE_BAUD 115200
1507 #define UART_DIVISOR SYS_CLK/(16*OR32_CONSOLE_BAUD)
1509 ENTRY(_early_uart_init)
1510 l.movhi r3,hi(UART_BASE_ADD)
1511 l.ori r3,r3,lo(UART_BASE_ADD)
1513 #if defined(CONFIG_SERIAL_8250)
1526 l.addi r4,r0,((UART_DIVISOR>>8) & 0x000000ff)
1527 l.sb UART_DLM(r3),r4
1528 l.addi r4,r0,((UART_DIVISOR) & 0x000000ff)
1529 l.sb UART_DLL(r3),r4
1537 .global _secondary_evbar
1541 /* Just disable interrupts and Return */
1542 l.ori r3,r0,SPR_SR_SM
1543 l.mtspr r0,r3,SPR_ESR_BASE
1548 _string_unhandled_exception:
1549 .string "\r\nRunarunaround: Unhandled exception 0x\0"
1552 .string ": EPC=0x\0"
1558 /* ========================================[ page aligned structures ]=== */
1561 * .data section should be page aligned
1562 * (look into arch/openrisc/kernel/vmlinux.lds.S)
1566 .global empty_zero_page
1570 .global swapper_pg_dir
1574 .global _unhandled_stack
1577 _unhandled_stack_top:
1579 /* ============================================================[ EOF ]=== */