2 * Copyright (C) 2007-2009 Semihalf, Rafal Jaworowski <raj@semihalf.com>
3 * Copyright (C) 2006 Semihalf, Marian Balakowicz <m8@semihalf.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
18 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
20 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <machine/asm.h>
32 #include <machine/hid.h>
33 #include <machine/param.h>
34 #include <machine/spr.h>
35 #include <machine/pte.h>
36 #include <machine/trap.h>
37 #include <machine/vmparam.h>
38 #include <machine/tlb.h>
40 #define TMPSTACKSZ 16384
47 * This symbol is here for the benefit of kvm_mkdb, and is supposed to
48 * mark the start of kernel text.
54 * Startup entry. Note, this must be the first thing in the text segment!
61 * Assumptions on the boot loader:
62 * - system memory starts from physical address 0
63 * - it's mapped by a single TBL1 entry
64 * - TLB1 mapping is 1:1 pa to va
65 * - kernel is loaded at 16MB boundary
66 * - all PID registers are set to the same value
67 * - CPU is running in AS=0
69 * Registers contents provided by the loader(8):
71 * r3 : metadata pointer
73 * We rearrange the TLB1 layout as follows:
74 * - find TLB1 entry we started in
75 * - make sure it's protected, ivalidate other entries
76 * - create temp entry in the second AS (make sure it's not TLB[1])
77 * - switch to temp mapping
78 * - map 16MB of RAM in TLB1[1]
79 * - use AS=1, set EPN to KERNBASE and RPN to kernel load address
80 * - switch to to TLB1[1] mapping
81 * - invalidate temp mapping
83 * locore registers use:
85 * r2 : trace pointer (AP only, for early diagnostics)
86 * r3-r27 : scratch registers
87 * r28 : temp TLB1 entry
88 * r29 : initial TLB1 entry we started in
89 * r30-r31 : arguments (metadata pointer)
93 * Keep arguments in r30 & r31 for later use.
101 li %r3, PSL_DE /* Keep debug exceptions for CodeWarrior. */
105 lis %r3, HID0_E500_DEFAULT_SET@h
106 ori %r3, %r3, HID0_E500_DEFAULT_SET@l
109 lis %r3, HID1_E500_DEFAULT_SET@h
110 ori %r3, %r3, HID1_E500_DEFAULT_SET@l
114 /* Invalidate all entries in TLB0 */
122 * Locate the TLB1 entry that maps this code
126 bl tlb1_find_current /* the entry found is returned in r29 */
128 bl tlb1_inval_all_but_current
130 * Create temporary mapping in AS=1 and switch to it
132 bl tlb1_temp_mapping_as1
135 ori %r3, %r3, (PSL_IS | PSL_DS)
141 rfi /* Switch context */
144 * Invalidate initial entry
150 * Setup final mapping in TLB1[1] and switch to it
152 /* Final kernel mapping, map in 16 MB of RAM */
153 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
154 li %r4, 0 /* Entry 0 */
155 rlwimi %r3, %r4, 16, 12, 15
159 li %r3, (TLB_SIZE_16M << MAS1_TSIZE_SHIFT)@l
160 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
161 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
165 ori %r3, %r3, KERNBASE@l /* EPN = KERNBASE */
167 ori %r3, %r3, MAS2_M@l /* WIMGE = 0b00100 */
172 /* Discover phys load address */
174 3: mflr %r4 /* Use current address */
175 rlwinm %r4, %r4, 0, 0, 7 /* 16MB alignment mask */
176 ori %r4, %r4, (MAS3_SX | MAS3_SW | MAS3_SR)@l
177 mtspr SPR_MAS3, %r4 /* Set RPN and protection */
183 /* Switch to the above TLB1[1] mapping */
186 rlwinm %r4, %r4, 0, 8, 31 /* Current offset from kernel load address */
187 rlwinm %r3, %r3, 0, 0, 19
188 add %r4, %r4, %r3 /* Convert to kernel virtual address */
190 li %r3, PSL_DE /* Note AS=0 */
196 * Invalidate temp mapping
204 * Setup a temporary stack
207 addi %r1, %r1, tmpstack@l
208 addi %r1, %r1, (TMPSTACKSZ - 8)
211 * Initialise exception vector offsets
216 * Set up arguments and jump to system initialization code
224 /* Switch to thread0.td_kstack now */
229 /* Machine independet part, does not return */
236 /************************************************************************/
238 /************************************************************************/
254 * Initial configuration
257 mflr %r31 /* r31 hold the address of bp_trace */
260 lis %r3, HID0_E500_DEFAULT_SET@h
261 ori %r3, %r3, HID0_E500_DEFAULT_SET@l
264 lis %r3, HID1_E500_DEFAULT_SET@h
265 ori %r3, %r3, HID1_E500_DEFAULT_SET@l
269 /* Enable branch prediction */
274 /* Invalidate all entries in TLB0 */
279 * Find TLB1 entry which is translating us now
283 bl tlb1_find_current /* the entry number found is in r29 */
285 bl tlb1_inval_all_but_current
287 * Create temporary translation in AS=1 and switch to it
289 bl tlb1_temp_mapping_as1
292 ori %r3, %r3, (PSL_IS | PSL_DS)
298 rfi /* Switch context */
301 * Invalidate initial entry
307 * Setup final mapping in TLB1[1] and switch to it
309 /* Final kernel mapping, map in 16 MB of RAM */
310 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
311 li %r4, 0 /* Entry 0 */
312 rlwimi %r3, %r4, 16, 4, 15
316 li %r3, (TLB_SIZE_16M << MAS1_TSIZE_SHIFT)@l
317 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
318 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
322 ori %r3, %r3, KERNBASE@l /* EPN = KERNBASE */
323 ori %r3, %r3, MAS2_M@l /* WIMGE = 0b00100 */
327 /* Retrieve kernel load [physical] address from bp_kernload */
330 rlwinm %r3, %r3, 0, 0, 19
331 lis %r4, bp_kernload@h
332 ori %r4, %r4, bp_kernload@l
333 lis %r5, __boot_page@h
334 ori %r5, %r5, __boot_page@l
335 sub %r4, %r4, %r5 /* offset of bp_kernload within __boot_page */
338 /* Set RPN and protection */
339 ori %r3, %r3, (MAS3_SX | MAS3_SW | MAS3_SR)@l
346 /* Switch to the final mapping */
349 rlwinm %r3, %r3, 0, 0xfff /* Offset from boot page start */
350 add %r3, %r3, %r5 /* Make this virtual address */
352 li %r4, 0 /* Note AS=0 */
358 * At this point we're running at virtual addresses KERNBASE and beyond so
359 * it's allowed to directly access all locations the kernel was linked
364 * Invalidate temp mapping
370 * Setup a temporary stack
373 addi %r1, %r1, tmpstack@l
374 addi %r1, %r1, (TMPSTACKSZ - 8)
377 * Initialise exception vector offsets
382 * Assign our pcpu instance
385 ori %r3, %r3, ap_pcpu@l
391 bl cpudep_ap_bootstrap
392 /* Switch to the idle thread's kstack */
395 bl machdep_ap_bootstrap
402 * Invalidate all entries in the given TLB.
407 rlwinm %r3, %r3, 3, 0x18 /* TLBSEL */
408 ori %r3, %r3, 0x4 /* INVALL */
418 * expects address to look up in r3, returns entry number in r29
420 * FIXME: the hidden assumption is we are now running in AS=0, but we should
421 * retrieve actual AS from MSR[IS|DS] and put it in MAS6[SAS]
425 slwi %r17, %r17, MAS6_SPID0_SHIFT
430 rlwinm %r29, %r17, 16, 20, 31 /* MAS0[ESEL] -> r29 */
432 /* Make sure we have IPROT set on the entry */
434 oris %r17, %r17, MAS1_IPROT@h
443 * Invalidates a single entry in TLB1.
449 lis %r4, MAS0_TLBSEL1@h /* Select TLB1 */
450 rlwimi %r4, %r3, 16, 12, 15 /* Select our entry */
454 li %r5, 0 /* MAS1[V] = 0 */
463 * r29 current entry number
464 * r28 returned temp entry
467 tlb1_temp_mapping_as1:
468 /* Read our current translation */
469 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
470 rlwimi %r3, %r29, 16, 12, 15 /* Select our current entry */
476 * Prepare and write temp entry
478 * FIXME this is not robust against overflow i.e. when the current
479 * entry is the last in TLB1
481 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
482 addi %r28, %r29, 1 /* Use next entry. */
483 rlwimi %r3, %r28, 16, 12, 15 /* Select temp entry */
488 rlwimi %r5, %r4, 12, 19, 19
489 li %r4, 0 /* Global mapping, TID=0 */
490 rlwimi %r5, %r4, 16, 8, 15
491 oris %r5, %r5, (MAS1_VALID | MAS1_IPROT)@h
500 * Loops over TLB1, invalidates all entries skipping the one which currently
506 tlb1_inval_all_but_current:
508 mfspr %r3, SPR_TLB1CFG /* Get number of entries */
509 andi. %r3, %r3, TLBCFG_NENTRY_MASK@l
510 li %r4, 0 /* Start from Entry 0 */
511 1: lis %r5, MAS0_TLBSEL1@h
512 rlwimi %r5, %r4, 16, 12, 15
517 cmpw %r4, %r29 /* our current entry? */
519 rlwinm %r5, %r5, 0, 2, 31 /* clear VALID and IPROT bits */
526 cmpw %r4, %r3 /* Check if this is the last entry */
533 * Boot page needs to be exactly 4K, with the last word of this page
534 * acting as the reset vector, so we need to stuff the remainder.
535 * Upon release from holdoff CPU fetches the last word of the boot
538 .space 4092 - (__boot_page_padding - __boot_page)
542 /************************************************************************/
543 /* locore subroutines */
544 /************************************************************************/
547 /* Set base address of interrupt handler routines */
548 lis %r3, interrupt_vector_base@h
551 /* Assign interrupt handler routines offsets */
552 li %r3, int_critical_input@l
554 li %r3, int_machine_check@l
556 li %r3, int_data_storage@l
558 li %r3, int_instr_storage@l
560 li %r3, int_external_input@l
562 li %r3, int_alignment@l
564 li %r3, int_program@l
566 li %r3, int_syscall@l
568 li %r3, int_decrementer@l
569 mtspr SPR_IVOR10, %r3
570 li %r3, int_fixed_interval_timer@l
571 mtspr SPR_IVOR11, %r3
572 li %r3, int_watchdog@l
573 mtspr SPR_IVOR12, %r3
574 li %r3, int_data_tlb_error@l
575 mtspr SPR_IVOR13, %r3
576 li %r3, int_inst_tlb_error@l
577 mtspr SPR_IVOR14, %r3
579 mtspr SPR_IVOR15, %r3
583 * void tid_flush(tlbtid_t tid);
585 * Invalidate all TLB0 entries which match the given TID. Note this is
586 * dedicated for cases when invalidation(s) should NOT be propagated to other
589 * Global vars tlb0_ways, tlb0_entries_per_way are assumed to have been set up
590 * correctly (by tlb0_get_tlbconf()).
594 cmpwi %r3, TID_KERNEL
595 beq tid_flush_end /* don't evict kernel translations */
597 /* Number of TLB0 ways */
599 ori %r4, %r4, tlb0_ways@l
602 /* Number of entries / way */
603 lis %r5, tlb0_entries_per_way@h
604 ori %r5, %r5, tlb0_entries_per_way@l
607 /* Disable interrupts */
611 li %r6, 0 /* ways counter */
613 li %r7, 0 /* entries [per way] counter */
615 /* Select TLB0 and ESEL (way) */
616 lis %r8, MAS0_TLBSEL0@h
617 rlwimi %r8, %r6, 16, 14, 15
621 /* Select EPN (entry within the way) */
622 rlwinm %r8, %r7, 12, 13, 19
627 /* Check if valid entry */
629 andis. %r9, %r8, MAS1_VALID@h
630 beq next_entry /* invalid entry */
632 /* Check if this is our TID */
633 rlwinm %r9, %r8, 16, 24, 31
636 bne next_entry /* not our TID */
638 /* Clear VALID bit */
639 rlwinm %r8, %r8, 0, 1, 31
656 /* Restore MSR (possibly re-enable interrupts) */
664 * Cache disable/enable/inval sequences according
665 * to section 2.16 of E500CORE RM.
668 /* Invalidate d-cache */
669 mfspr %r3, SPR_L1CSR0
670 ori %r3, %r3, (L1CSR0_DCFI | L1CSR0_DCLFR)@l
673 mtspr SPR_L1CSR0, %r3
675 1: mfspr %r3, SPR_L1CSR0
676 andi. %r3, %r3, L1CSR0_DCFI
680 ENTRY(dcache_disable)
681 /* Disable d-cache */
682 mfspr %r3, SPR_L1CSR0
688 mtspr SPR_L1CSR0, %r3
694 mfspr %r3, SPR_L1CSR0
695 oris %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@h
696 ori %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@l
699 mtspr SPR_L1CSR0, %r3
704 /* Invalidate i-cache */
705 mfspr %r3, SPR_L1CSR1
706 ori %r3, %r3, (L1CSR1_ICFI | L1CSR1_ICLFR)@l
708 mtspr SPR_L1CSR1, %r3
710 1: mfspr %r3, SPR_L1CSR1
711 andi. %r3, %r3, L1CSR1_ICFI
715 ENTRY(icache_disable)
716 /* Disable i-cache */
717 mfspr %r3, SPR_L1CSR1
722 mtspr SPR_L1CSR1, %r3
728 mfspr %r3, SPR_L1CSR1
729 oris %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@h
730 ori %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@l
732 mtspr SPR_L1CSR1, %r3
739 * Similar to setjmp to setup for handling faults on accesses to user memory.
740 * Any routine using this may only call bcopy, either the form below,
741 * or the (currently used) C code optimized, so it doesn't use any non-volatile
748 lwz %r4, PC_CURTHREAD(%r4)
750 stw %r3, PCB_ONFAULT(%r4)
757 stmw %r10, 12(%r3) /* store CR, CTR, XER, [r13 .. r31] */
758 li %r3, 0 /* return FALSE */
761 /************************************************************************/
763 /************************************************************************/
770 * Compiled KERNBASE locations
773 .set kernbase, KERNBASE
778 #define INTRCNT_COUNT 256 /* max(HROWPIC_IRQMAX,OPENPIC_IRQMAX) */
781 .space INTRCNT_COUNT * (MAXCOMLEN + 1) * 2
783 .long INTRCNT_COUNT * (MAXCOMLEN + 1) * 2
787 .space INTRCNT_COUNT * 4 * 2
789 .long INTRCNT_COUNT * 4 * 2
791 #include <powerpc/booke/trap_subr.S>