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
221 /* Prepare e500 core */
224 /* Switch to thread0.td_kstack now */
229 /* Machine independet part, does not return */
236 /************************************************************************/
238 /************************************************************************/
250 * Initial configuration
254 lis %r3, HID0_E500_DEFAULT_SET@h
255 ori %r3, %r3, HID0_E500_DEFAULT_SET@l
258 lis %r3, HID1_E500_DEFAULT_SET@h
259 ori %r3, %r3, HID1_E500_DEFAULT_SET@l
263 /* Enable branch prediction */
268 /* Invalidate all entries in TLB0 */
273 * Find TLB1 entry which is translating us now
277 bl tlb1_find_current /* the entry number found is in r29 */
279 bl tlb1_inval_all_but_current
281 * Create temporary translation in AS=1 and switch to it
283 bl tlb1_temp_mapping_as1
286 ori %r3, %r3, (PSL_IS | PSL_DS)
292 rfi /* Switch context */
295 * Invalidate initial entry
301 * Setup final mapping in TLB1[1] and switch to it
303 /* Final kernel mapping, map in 16 MB of RAM */
304 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
305 li %r4, 0 /* Entry 0 */
306 rlwimi %r3, %r4, 16, 4, 15
310 li %r3, (TLB_SIZE_16M << MAS1_TSIZE_SHIFT)@l
311 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
312 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
316 ori %r3, %r3, KERNBASE@l /* EPN = KERNBASE */
317 ori %r3, %r3, MAS2_M@l /* WIMGE = 0b00100 */
321 /* Retrieve kernel load [physical] address from kernload_ap */
324 rlwinm %r3, %r3, 0, 0, 19
325 lis %r4, kernload_ap@h
326 ori %r4, %r4, kernload_ap@l
327 lis %r5, __boot_page@h
328 ori %r5, %r5, __boot_page@l
329 sub %r4, %r4, %r5 /* offset of kernload_ap within __boot_page */
332 /* Set RPN and protection */
333 ori %r3, %r3, (MAS3_SX | MAS3_SW | MAS3_SR)@l
340 /* Switch to the final mapping */
343 rlwinm %r3, %r3, 0, 0xfff /* Offset from boot page start */
344 add %r3, %r3, %r5 /* Make this virtual address */
346 li %r4, 0 /* Note AS=0 */
352 * At this point we're running at virtual addresses KERNBASE and beyond so
353 * it's allowed to directly access all locations the kernel was linked
358 * Invalidate temp mapping
364 * Setup a temporary stack
367 addi %r1, %r1, tmpstack@l
368 addi %r1, %r1, (TMPSTACKSZ - 8)
371 * Initialise exception vector offsets
376 * Assign our pcpu instance
379 ori %r3, %r3, ap_pcpu@l
385 bl cpudep_ap_bootstrap
386 /* Switch to the idle thread's kstack */
389 bl machdep_ap_bootstrap
396 * Invalidate all entries in the given TLB.
401 rlwinm %r3, %r3, 3, 0x18 /* TLBSEL */
402 ori %r3, %r3, 0x4 /* INVALL */
412 * expects address to look up in r3, returns entry number in r29
414 * FIXME: the hidden assumption is we are now running in AS=0, but we should
415 * retrieve actual AS from MSR[IS|DS] and put it in MAS6[SAS]
419 slwi %r17, %r17, MAS6_SPID0_SHIFT
424 rlwinm %r29, %r17, 16, 20, 31 /* MAS0[ESEL] -> r29 */
426 /* Make sure we have IPROT set on the entry */
428 oris %r17, %r17, MAS1_IPROT@h
437 * Invalidates a single entry in TLB1.
443 lis %r4, MAS0_TLBSEL1@h /* Select TLB1 */
444 rlwimi %r4, %r3, 16, 12, 15 /* Select our entry */
448 li %r5, 0 /* MAS1[V] = 0 */
457 * r29 current entry number
458 * r28 returned temp entry
461 tlb1_temp_mapping_as1:
462 /* Read our current translation */
463 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
464 rlwimi %r3, %r29, 16, 12, 15 /* Select our current entry */
470 * Prepare and write temp entry
472 * FIXME this is not robust against overflow i.e. when the current
473 * entry is the last in TLB1
475 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
476 addi %r28, %r29, 1 /* Use next entry. */
477 rlwimi %r3, %r28, 16, 12, 15 /* Select temp entry */
482 rlwimi %r5, %r4, 12, 19, 19
483 li %r4, 0 /* Global mapping, TID=0 */
484 rlwimi %r5, %r4, 16, 8, 15
485 oris %r5, %r5, (MAS1_VALID | MAS1_IPROT)@h
494 * Loops over TLB1, invalidates all entries skipping the one which currently
500 tlb1_inval_all_but_current:
502 mfspr %r3, SPR_TLB1CFG /* Get number of entries */
503 andi. %r3, %r3, TLBCFG_NENTRY_MASK@l
504 li %r4, 0 /* Start from Entry 0 */
505 1: lis %r5, MAS0_TLBSEL1@h
506 rlwimi %r5, %r4, 16, 12, 15
511 cmpw %r4, %r29 /* our current entry? */
513 rlwinm %r5, %r5, 0, 2, 31 /* clear VALID and IPROT bits */
520 cmpw %r4, %r3 /* Check if this is the last entry */
527 * Boot page needs to be exactly 4K, with the last word of this page
528 * acting as the reset vector, so we need to stuff the remainder.
529 * Upon release from holdoff CPU fetches the last word of the boot
532 .space 4092 - (__boot_page_padding - __boot_page)
536 /************************************************************************/
537 /* locore subroutines */
538 /************************************************************************/
541 /* Set base address of interrupt handler routines */
542 lis %r3, interrupt_vector_base@h
545 /* Assign interrupt handler routines offsets */
546 li %r3, int_critical_input@l
548 li %r3, int_machine_check@l
550 li %r3, int_data_storage@l
552 li %r3, int_instr_storage@l
554 li %r3, int_external_input@l
556 li %r3, int_alignment@l
558 li %r3, int_program@l
560 li %r3, int_syscall@l
562 li %r3, int_decrementer@l
563 mtspr SPR_IVOR10, %r3
564 li %r3, int_fixed_interval_timer@l
565 mtspr SPR_IVOR11, %r3
566 li %r3, int_watchdog@l
567 mtspr SPR_IVOR12, %r3
568 li %r3, int_data_tlb_error@l
569 mtspr SPR_IVOR13, %r3
570 li %r3, int_inst_tlb_error@l
571 mtspr SPR_IVOR14, %r3
573 mtspr SPR_IVOR15, %r3
577 * void tid_flush(tlbtid_t tid);
579 * Invalidate all TLB0 entries which match the given TID. Note this is
580 * dedicated for cases when invalidation(s) should NOT be propagated to other
583 * Global vars tlb0_ways, tlb0_entries_per_way are assumed to have been set up
584 * correctly (by tlb0_get_tlbconf()).
588 cmpwi %r3, TID_KERNEL
589 beq tid_flush_end /* don't evict kernel translations */
591 /* Number of TLB0 ways */
593 ori %r4, %r4, tlb0_ways@l
596 /* Number of entries / way */
597 lis %r5, tlb0_entries_per_way@h
598 ori %r5, %r5, tlb0_entries_per_way@l
601 /* Disable interrupts */
605 li %r6, 0 /* ways counter */
607 li %r7, 0 /* entries [per way] counter */
609 /* Select TLB0 and ESEL (way) */
610 lis %r8, MAS0_TLBSEL0@h
611 rlwimi %r8, %r6, 16, 14, 15
615 /* Select EPN (entry within the way) */
616 rlwinm %r8, %r7, 12, 13, 19
621 /* Check if valid entry */
623 andis. %r9, %r8, MAS1_VALID@h
624 beq next_entry /* invalid entry */
626 /* Check if this is our TID */
627 rlwinm %r9, %r8, 16, 24, 31
630 bne next_entry /* not our TID */
632 /* Clear VALID bit */
633 rlwinm %r8, %r8, 0, 1, 31
650 /* Restore MSR (possibly re-enable interrupts) */
658 * Cache disable/enable/inval sequences according
659 * to section 2.16 of E500CORE RM.
662 /* Invalidate d-cache */
663 mfspr %r3, SPR_L1CSR0
664 ori %r3, %r3, (L1CSR0_DCFI | L1CSR0_DCLFR)@l
667 mtspr SPR_L1CSR0, %r3
669 1: mfspr %r3, SPR_L1CSR0
670 andi. %r3, %r3, L1CSR0_DCFI
674 ENTRY(dcache_disable)
675 /* Disable d-cache */
676 mfspr %r3, SPR_L1CSR0
682 mtspr SPR_L1CSR0, %r3
688 mfspr %r3, SPR_L1CSR0
689 oris %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@h
690 ori %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@l
693 mtspr SPR_L1CSR0, %r3
698 /* Invalidate i-cache */
699 mfspr %r3, SPR_L1CSR1
700 ori %r3, %r3, (L1CSR1_ICFI | L1CSR1_ICLFR)@l
702 mtspr SPR_L1CSR1, %r3
704 1: mfspr %r3, SPR_L1CSR1
705 andi. %r3, %r3, L1CSR1_ICFI
709 ENTRY(icache_disable)
710 /* Disable i-cache */
711 mfspr %r3, SPR_L1CSR1
716 mtspr SPR_L1CSR1, %r3
722 mfspr %r3, SPR_L1CSR1
723 oris %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@h
724 ori %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@l
726 mtspr SPR_L1CSR1, %r3
733 * Similar to setjmp to setup for handling faults on accesses to user memory.
734 * Any routine using this may only call bcopy, either the form below,
735 * or the (currently used) C code optimized, so it doesn't use any non-volatile
742 lwz %r4, PC_CURTHREAD(%r4)
744 stw %r3, PCB_ONFAULT(%r4)
751 stmw %r10, 12(%r3) /* store CR, CTR, XER, [r13 .. r31] */
752 li %r3, 0 /* return FALSE */
755 /************************************************************************/
757 /************************************************************************/
764 * Compiled KERNBASE locations
767 .set kernbase, KERNBASE
772 #define INTRCNT_COUNT 256 /* max(HROWPIC_IRQMAX,OPENPIC_IRQMAX) */
775 .space INTRCNT_COUNT * (MAXCOMLEN + 1) * 2
777 .long INTRCNT_COUNT * (MAXCOMLEN + 1) * 2
781 .space INTRCNT_COUNT * 4 * 2
783 .long INTRCNT_COUNT * 4 * 2
785 #include <powerpc/booke/trap_subr.S>