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
131 * Create temporary mapping in AS=1 and switch to it
134 bl tlb1_temp_mapping_as1
137 ori %r3, %r3, (PSL_IS | PSL_DS)
143 rfi /* Switch context */
146 * Invalidate initial entry
152 * Setup final mapping in TLB1[1] and switch to it
154 /* Final kernel mapping, map in 16 MB of RAM */
155 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
156 li %r4, 0 /* Entry 0 */
157 rlwimi %r3, %r4, 16, 12, 15
161 li %r3, (TLB_SIZE_64M << MAS1_TSIZE_SHIFT)@l
162 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
163 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
167 ori %r3, %r3, KERNBASE@l /* EPN = KERNBASE */
169 ori %r3, %r3, MAS2_M@l /* WIMGE = 0b00100 */
174 /* Discover phys load address */
176 3: mflr %r4 /* Use current address */
177 rlwinm %r4, %r4, 0, 0, 7 /* 16MB alignment mask */
178 ori %r4, %r4, (MAS3_SX | MAS3_SW | MAS3_SR)@l
179 mtspr SPR_MAS3, %r4 /* Set RPN and protection */
185 /* Switch to the above TLB1[1] mapping */
188 rlwinm %r4, %r4, 0, 8, 31 /* Current offset from kernel load address */
189 rlwinm %r3, %r3, 0, 0, 19
190 add %r4, %r4, %r3 /* Convert to kernel virtual address */
192 li %r3, PSL_DE /* Note AS=0 */
198 * Invalidate temp mapping
206 * Setup a temporary stack
209 addi %r1, %r1, tmpstack@l
210 addi %r1, %r1, (TMPSTACKSZ - 8)
213 * Initialise exception vector offsets
218 * Set up arguments and jump to system initialization code
226 /* Switch to thread0.td_kstack now */
231 /* Machine independet part, does not return */
238 /************************************************************************/
240 /************************************************************************/
259 * Initial configuration
261 1: mflr %r31 /* r31 hold the address of bp_ntlb1s */
264 lis %r3, HID0_E500_DEFAULT_SET@h
265 ori %r3, %r3, HID0_E500_DEFAULT_SET@l
268 lis %r3, HID1_E500_DEFAULT_SET@h
269 ori %r3, %r3, HID1_E500_DEFAULT_SET@l
273 /* Enable branch prediction */
278 /* Invalidate all entries in TLB0 */
283 * Find TLB1 entry which is translating us now
287 bl tlb1_find_current /* the entry number found is in r29 */
289 bl tlb1_inval_all_but_current
292 * Create temporary translation in AS=1 and switch to it
295 bl tlb1_temp_mapping_as1
298 ori %r3, %r3, (PSL_IS | PSL_DS)
304 rfi /* Switch context */
307 * Invalidate initial entry
313 * Setup final mapping in TLB1[1] and switch to it
319 4: lis %r3, MAS0_TLBSEL1@h
320 rlwimi %r3, %r4, 16, 12, 15
340 /* Switch to the final mapping */
341 lis %r5, __boot_page@ha
342 ori %r5, %r5, __boot_page@l
345 rlwinm %r3, %r3, 0, 0xfff /* Offset from boot page start */
346 add %r3, %r3, %r5 /* Make this virtual address */
348 li %r4, 0 /* Note AS=0 */
354 * At this point we're running at virtual addresses KERNBASE and beyond so
355 * it's allowed to directly access all locations the kernel was linked
360 * Invalidate temp mapping
366 * Setup a temporary stack
369 addi %r1, %r1, tmpstack@l
370 addi %r1, %r1, (TMPSTACKSZ - 8)
373 * Initialise exception vector offsets
378 * Assign our pcpu instance
381 ori %r3, %r3, ap_pcpu@l
387 bl cpudep_ap_bootstrap
388 /* Switch to the idle thread's kstack */
391 bl machdep_ap_bootstrap
398 * Invalidate all entries in the given TLB.
403 rlwinm %r3, %r3, 3, 0x18 /* TLBSEL */
404 ori %r3, %r3, 0x4 /* INVALL */
414 * expects address to look up in r3, returns entry number in r29
416 * FIXME: the hidden assumption is we are now running in AS=0, but we should
417 * retrieve actual AS from MSR[IS|DS] and put it in MAS6[SAS]
421 slwi %r17, %r17, MAS6_SPID0_SHIFT
426 rlwinm %r29, %r17, 16, 20, 31 /* MAS0[ESEL] -> r29 */
428 /* Make sure we have IPROT set on the entry */
430 oris %r17, %r17, MAS1_IPROT@h
439 * Invalidates a single entry in TLB1.
445 lis %r4, MAS0_TLBSEL1@h /* Select TLB1 */
446 rlwimi %r4, %r3, 16, 12, 15 /* Select our entry */
450 li %r5, 0 /* MAS1[V] = 0 */
459 * r3 entry of temp translation
460 * r29 entry of current translation
461 * r28 returns temp entry passed in r3
464 tlb1_temp_mapping_as1:
467 /* Read our current translation */
468 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
469 rlwimi %r3, %r29, 16, 12, 15 /* Select our current entry */
474 /* Prepare and write temp entry */
475 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
476 rlwimi %r3, %r28, 16, 12, 15 /* Select temp entry */
481 rlwimi %r5, %r4, 12, 19, 19
482 li %r4, 0 /* Global mapping, TID=0 */
483 rlwimi %r5, %r4, 16, 8, 15
484 oris %r5, %r5, (MAS1_VALID | MAS1_IPROT)@h
493 * Loops over TLB1, invalidates all entries skipping the one which currently
499 tlb1_inval_all_but_current:
501 mfspr %r3, SPR_TLB1CFG /* Get number of entries */
502 andi. %r3, %r3, TLBCFG_NENTRY_MASK@l
503 li %r4, 0 /* Start from Entry 0 */
504 1: lis %r5, MAS0_TLBSEL1@h
505 rlwimi %r5, %r4, 16, 12, 15
510 cmpw %r4, %r29 /* our current entry? */
512 rlwinm %r5, %r5, 0, 2, 31 /* clear VALID and IPROT bits */
519 cmpw %r4, %r3 /* Check if this is the last entry */
526 * Boot page needs to be exactly 4K, with the last word of this page
527 * acting as the reset vector, so we need to stuff the remainder.
528 * Upon release from holdoff CPU fetches the last word of the boot
531 .space 4092 - (__boot_page_padding - __boot_page)
535 /************************************************************************/
536 /* locore subroutines */
537 /************************************************************************/
540 /* Set base address of interrupt handler routines */
541 lis %r3, interrupt_vector_base@h
544 /* Assign interrupt handler routines offsets */
545 li %r3, int_critical_input@l
547 li %r3, int_machine_check@l
549 li %r3, int_data_storage@l
551 li %r3, int_instr_storage@l
553 li %r3, int_external_input@l
555 li %r3, int_alignment@l
557 li %r3, int_program@l
559 li %r3, int_syscall@l
561 li %r3, int_decrementer@l
562 mtspr SPR_IVOR10, %r3
563 li %r3, int_fixed_interval_timer@l
564 mtspr SPR_IVOR11, %r3
565 li %r3, int_watchdog@l
566 mtspr SPR_IVOR12, %r3
567 li %r3, int_data_tlb_error@l
568 mtspr SPR_IVOR13, %r3
569 li %r3, int_inst_tlb_error@l
570 mtspr SPR_IVOR14, %r3
572 mtspr SPR_IVOR15, %r3
576 * void tid_flush(tlbtid_t tid);
578 * Invalidate all TLB0 entries which match the given TID. Note this is
579 * dedicated for cases when invalidation(s) should NOT be propagated to other
582 * Global vars tlb0_ways, tlb0_entries_per_way are assumed to have been set up
583 * correctly (by tlb0_get_tlbconf()).
587 cmpwi %r3, TID_KERNEL
588 beq tid_flush_end /* don't evict kernel translations */
590 /* Number of TLB0 ways */
592 ori %r4, %r4, tlb0_ways@l
595 /* Number of entries / way */
596 lis %r5, tlb0_entries_per_way@h
597 ori %r5, %r5, tlb0_entries_per_way@l
600 /* Disable interrupts */
604 li %r6, 0 /* ways counter */
606 li %r7, 0 /* entries [per way] counter */
608 /* Select TLB0 and ESEL (way) */
609 lis %r8, MAS0_TLBSEL0@h
610 rlwimi %r8, %r6, 16, 14, 15
614 /* Select EPN (entry within the way) */
615 rlwinm %r8, %r7, 12, 13, 19
620 /* Check if valid entry */
622 andis. %r9, %r8, MAS1_VALID@h
623 beq next_entry /* invalid entry */
625 /* Check if this is our TID */
626 rlwinm %r9, %r8, 16, 24, 31
629 bne next_entry /* not our TID */
631 /* Clear VALID bit */
632 rlwinm %r8, %r8, 0, 1, 31
649 /* Restore MSR (possibly re-enable interrupts) */
657 * Cache disable/enable/inval sequences according
658 * to section 2.16 of E500CORE RM.
661 /* Invalidate d-cache */
662 mfspr %r3, SPR_L1CSR0
663 ori %r3, %r3, (L1CSR0_DCFI | L1CSR0_DCLFR)@l
666 mtspr SPR_L1CSR0, %r3
668 1: mfspr %r3, SPR_L1CSR0
669 andi. %r3, %r3, L1CSR0_DCFI
673 ENTRY(dcache_disable)
674 /* Disable d-cache */
675 mfspr %r3, SPR_L1CSR0
681 mtspr SPR_L1CSR0, %r3
687 mfspr %r3, SPR_L1CSR0
688 oris %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@h
689 ori %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@l
692 mtspr SPR_L1CSR0, %r3
697 /* Invalidate i-cache */
698 mfspr %r3, SPR_L1CSR1
699 ori %r3, %r3, (L1CSR1_ICFI | L1CSR1_ICLFR)@l
701 mtspr SPR_L1CSR1, %r3
703 1: mfspr %r3, SPR_L1CSR1
704 andi. %r3, %r3, L1CSR1_ICFI
708 ENTRY(icache_disable)
709 /* Disable i-cache */
710 mfspr %r3, SPR_L1CSR1
715 mtspr SPR_L1CSR1, %r3
721 mfspr %r3, SPR_L1CSR1
722 oris %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@h
723 ori %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@l
725 mtspr SPR_L1CSR1, %r3
732 * Similar to setjmp to setup for handling faults on accesses to user memory.
733 * Any routine using this may only call bcopy, either the form below,
734 * or the (currently used) C code optimized, so it doesn't use any non-volatile
742 stw %r3, PCB_ONFAULT(%r4)
749 stmw %r10, 12(%r3) /* store CR, CTR, XER, [r13 .. r31] */
750 li %r3, 0 /* return FALSE */
753 /************************************************************************/
755 /************************************************************************/
762 * Compiled KERNBASE locations
765 .set kernbase, KERNBASE
770 #define INTRCNT_COUNT 256 /* max(HROWPIC_IRQMAX,OPENPIC_IRQMAX) */
773 .space INTRCNT_COUNT * (MAXCOMLEN + 1) * 2
775 .long INTRCNT_COUNT * (MAXCOMLEN + 1) * 2
779 .space INTRCNT_COUNT * 4 * 2
781 .long INTRCNT_COUNT * 4 * 2
783 #include <powerpc/booke/trap_subr.S>