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 "opt_hwpmc_hooks.h"
33 #include <machine/asm.h>
34 #include <machine/hid.h>
35 #include <machine/param.h>
36 #include <machine/spr.h>
37 #include <machine/pte.h>
38 #include <machine/trap.h>
39 #include <machine/vmparam.h>
40 #include <machine/tlb.h>
42 #define TMPSTACKSZ 16384
45 #define GET_TOCBASE(r) \
47 #define TOC_RESTORE nop
57 #define THREAD_REG %r13
62 #define GET_TOCBASE(r)
73 #define THREAD_REG %r2
84 * This symbol is here for the benefit of kvm_mkdb, and is supposed to
85 * mark the start of kernel text.
91 * Startup entry. Note, this must be the first thing in the text segment!
98 * Assumptions on the boot loader:
99 * - System memory starts from physical address 0
100 * - It's mapped by a single TLB1 entry
101 * - TLB1 mapping is 1:1 pa to va
102 * - Kernel is loaded at 64MB boundary
103 * - All PID registers are set to the same value
104 * - CPU is running in AS=0
106 * Registers contents provided by the loader(8):
108 * r3 : metadata pointer
110 * We rearrange the TLB1 layout as follows:
111 * - Find TLB1 entry we started in
112 * - Make sure it's protected, invalidate other entries
113 * - Create temp entry in the second AS (make sure it's not TLB[1])
114 * - Switch to temp mapping
115 * - Map 64MB of RAM in TLB1[1]
116 * - Use AS=0, set EPN to VM_MIN_KERNEL_ADDRESS and RPN to kernel load address
117 * - Switch to TLB1[1] mapping
118 * - Invalidate temp mapping
120 * locore registers use:
122 * r2 : trace pointer (AP only, for early diagnostics)
123 * r3-r27 : scratch registers
124 * r28 : temp TLB1 entry
125 * r29 : initial TLB1 entry we started in
126 * r30-r31 : arguments (metadata pointer)
130 * Keep arguments in r30 & r31 for later use.
138 li %r3, PSL_DE /* Keep debug exceptions for CodeWarrior. */
140 oris %r3, %r3, PSL_CM@h
146 * Initial HIDs configuration
150 rlwinm %r3, %r3, 16, 16, 31
152 lis %r4, HID0_E500_DEFAULT_SET@h
153 ori %r4, %r4, HID0_E500_DEFAULT_SET@l
155 /* Check for e500mc and e5500 */
156 cmpli 0, 0, %r3, FSL_E500mc
159 lis %r4, HID0_E500MC_DEFAULT_SET@h
160 ori %r4, %r4, HID0_E500MC_DEFAULT_SET@l
163 cmpli 0, 0, %r3, FSL_E5500
166 lis %r4, HID0_E5500_DEFAULT_SET@h
167 ori %r4, %r4, HID0_E5500_DEFAULT_SET@l
174 * E500mc and E5500 do not have HID1 register, so skip HID1 setup on
177 cmpli 0, 0, %r3, FSL_E500mc
179 cmpli 0, 0, %r3, FSL_E5500
181 cmpli 0, 0, %r3, FSL_E6500
184 lis %r3, HID1_E500_DEFAULT_SET@h
185 ori %r3, %r3, HID1_E500_DEFAULT_SET@l
189 /* Invalidate all entries in TLB0 */
197 * Locate the TLB1 entry that maps this code
201 bl tlb1_find_current /* the entry found is returned in r29 */
203 bl tlb1_inval_all_but_current
206 * Create temporary mapping in AS=1 and switch to it
208 bl tlb1_temp_mapping_as1
211 ori %r3, %r3, (PSL_IS | PSL_DS)
214 addi %r4, %r4, (3f - 2b)
217 rfi /* Switch context */
220 * Invalidate initial entry
227 * Setup final mapping in TLB1[1] and switch to it
229 /* Final kernel mapping, map in 64 MB of RAM */
230 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
231 li %r4, 0 /* Entry 0 */
232 rlwimi %r3, %r4, 16, 10, 15
236 li %r3, (TLB_SIZE_64M << MAS1_TSIZE_SHIFT)@l
237 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
238 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
241 LOAD_ADDR(%r3, VM_MIN_KERNEL_ADDRESS)
242 ori %r3, %r3, (_TLB_ENTRY_SHARED | MAS2_M)@l /* WIMGE = 0b00100 */
246 /* Discover phys load address */
248 3: mflr %r4 /* Use current address */
249 rlwinm %r4, %r4, 0, 0, 5 /* 64MB alignment mask */
250 ori %r4, %r4, (MAS3_SX | MAS3_SW | MAS3_SR)@l
251 mtspr SPR_MAS3, %r4 /* Set RPN and protection */
261 /* Switch to the above TLB1[1] mapping */
268 rlwinm %r4, %r4, 0, 6, 31 /* Current offset from kernel load address */
269 rlwinm %r3, %r3, 0, 0, 19
271 add %r4, %r4, %r3 /* Convert to kernel virtual address */
272 addi %r4, %r4, (5f - 4b)
273 li %r3, PSL_DE /* Note AS=0 */
275 oris %r3, %r3, PSL_CM@h
282 * Invalidate temp mapping
291 /* Set up the TOC pointer */
296 .llong __tocbase + 0x8000 - .
302 /* Get load offset */
303 ld %r31,-0x8000(%r2) /* First TOC entry is TOC base */
304 subf %r31,%r31,%r2 /* Subtract from real TOC base to get base */
306 /* Set up the stack pointer */
307 addis %r1,%r2,TOC_REF(tmpstack)@ha
308 ld %r1,TOC_REF(tmpstack)@l(%r1)
309 addi %r1,%r1,TMPSTACKSZ-96
319 * Setup a temporary stack
326 addi %r1, %r1, (TMPSTACKSZ - 16)
333 .long _GLOBAL_OFFSET_TABLE_-.
335 lwz %r3,0(%r5) /* _DYNAMIC in %r3 */
337 lwz %r4,4(%r5) /* GOT pointer */
339 lwz %r4,4(%r4) /* got[0] is _DYNAMIC link addr */
340 subf %r4,%r4,%r3 /* subtract to calculate relocbase */
342 bl CNAME(elf_reloc_self)
346 * Initialise exception vector offsets
352 * Set up arguments and jump to system initialization code
361 /* Switch to thread0.td_kstack now */
366 /* Machine independet part, does not return */
374 /************************************************************************/
376 /************************************************************************/
392 * Initial configuration
395 mflr %r31 /* r31 hold the address of bp_trace */
399 rlwinm %r3, %r3, 16, 16, 31
401 /* HID0 for E500 is default */
402 lis %r4, HID0_E500_DEFAULT_SET@h
403 ori %r4, %r4, HID0_E500_DEFAULT_SET@l
405 cmpli 0, 0, %r3, FSL_E500mc
407 lis %r4, HID0_E500MC_DEFAULT_SET@h
408 ori %r4, %r4, HID0_E500MC_DEFAULT_SET@l
411 cmpli 0, 0, %r3, FSL_E5500
413 lis %r4, HID0_E5500_DEFAULT_SET@h
414 ori %r4, %r4, HID0_E5500_DEFAULT_SET@l
419 /* Enable branch prediction */
424 /* Invalidate all entries in TLB0 */
429 * Find TLB1 entry which is translating us now
433 bl tlb1_find_current /* the entry number found is in r29 */
435 bl tlb1_inval_all_but_current
438 * Create temporary translation in AS=1 and switch to it
441 bl tlb1_temp_mapping_as1
444 ori %r3, %r3, (PSL_IS | PSL_DS)
446 oris %r3, %r3, PSL_CM@h
450 addi %r4, %r4, (4f - 3b)
453 rfi /* Switch context */
456 * Invalidate initial entry
463 * Setup final mapping in TLB1[1] and switch to it
465 /* Final kernel mapping, map in 64 MB of RAM */
466 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
467 li %r4, 0 /* Entry 0 */
468 rlwimi %r3, %r4, 16, 4, 15
472 li %r3, (TLB_SIZE_64M << MAS1_TSIZE_SHIFT)@l
473 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
474 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
477 LOAD_ADDR(%r3, VM_MIN_KERNEL_ADDRESS)
478 ori %r3, %r3, (_TLB_ENTRY_SHARED | MAS2_M)@l /* WIMGE = 0b00100 */
482 /* Retrieve kernel load [physical] address from bp_kernload */
500 rlwinm %r3, %r3, 0, 0, 19
502 sub %r4, %r4, %r5 /* offset of bp_kernload within __boot_page */
505 /* Set RPN and protection */
506 ori %r3, %r3, (MAS3_SX | MAS3_SW | MAS3_SR)@l
517 /* Switch to the final mapping */
520 rlwinm %r3, %r3, 0, 0xfff /* Offset from boot page start */
521 add %r3, %r3, %r5 /* Make this virtual address */
522 addi %r3, %r3, (7f - 6b)
524 lis %r4, PSL_CM@h /* Note AS=0 */
526 li %r4, 0 /* Note AS=0 */
534 * At this point we're running at virtual addresses VM_MIN_KERNEL_ADDRESS and
535 * beyond so it's allowed to directly access all locations the kernel was linked
540 * Invalidate temp mapping
546 /* Set up the TOC pointer */
551 .llong __tocbase + 0x8000 - .
557 /* Set up the stack pointer */
558 addis %r1,%r2,TOC_REF(tmpstack)@ha
559 ld %r1,TOC_REF(tmpstack)@l(%r1)
560 addi %r1,%r1,TMPSTACKSZ-96
563 * Setup a temporary stack
571 addi %r1, %r1, (TMPSTACKSZ - 16)
575 * Initialise exception vector offsets
581 * Assign our pcpu instance
591 bl CNAME(pmap_bootstrap_ap)
594 bl CNAME(cpudep_ap_bootstrap)
596 /* Switch to the idle thread's kstack */
599 bl CNAME(machdep_ap_bootstrap)
606 #if defined (BOOKE_E500)
608 * Invalidate all entries in the given TLB.
613 rlwinm %r3, %r3, 3, (1 << 3) /* TLBSEL */
614 ori %r3, %r3, (1 << 2) /* INVALL */
624 * expects address to look up in r3, returns entry number in r29
626 * FIXME: the hidden assumption is we are now running in AS=0, but we should
627 * retrieve actual AS from MSR[IS|DS] and put it in MAS6[SAS]
631 slwi %r17, %r17, MAS6_SPID0_SHIFT
636 rlwinm %r29, %r17, 16, 26, 31 /* MAS0[ESEL] -> r29 */
638 /* Make sure we have IPROT set on the entry */
640 oris %r17, %r17, MAS1_IPROT@h
649 * Invalidates a single entry in TLB1.
655 lis %r4, MAS0_TLBSEL1@h /* Select TLB1 */
656 rlwimi %r4, %r3, 16, 10, 15 /* Select our entry */
660 li %r5, 0 /* MAS1[V] = 0 */
669 * r29 current entry number
670 * r28 returned temp entry
673 tlb1_temp_mapping_as1:
674 /* Read our current translation */
675 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
676 rlwimi %r3, %r29, 16, 10, 15 /* Select our current entry */
682 * Prepare and write temp entry
684 * FIXME this is not robust against overflow i.e. when the current
685 * entry is the last in TLB1
687 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
688 addi %r28, %r29, 1 /* Use next entry. */
689 rlwimi %r3, %r28, 16, 10, 15 /* Select temp entry */
694 rlwimi %r5, %r4, 12, 19, 19
695 li %r4, 0 /* Global mapping, TID=0 */
696 rlwimi %r5, %r4, 16, 8, 15
697 oris %r5, %r5, (MAS1_VALID | MAS1_IPROT)@h
712 * Loops over TLB1, invalidates all entries skipping the one which currently
718 tlb1_inval_all_but_current:
719 mfspr %r3, SPR_TLB1CFG /* Get number of entries */
720 andi. %r3, %r3, TLBCFG_NENTRY_MASK@l
721 li %r4, 0 /* Start from Entry 0 */
722 1: lis %r5, MAS0_TLBSEL1@h
723 rlwimi %r5, %r4, 16, 10, 15
728 cmpw %r4, %r29 /* our current entry? */
730 rlwinm %r5, %r5, 0, 2, 31 /* clear VALID and IPROT bits */
737 cmpw %r4, %r3 /* Check if this is the last entry */
742 * MAS8 conditional zeroing.
747 rlwinm %r20, %r20, 16, 16, 31
748 cmpli 0, 0, %r20, FSL_E500mc
750 cmpli 0, 0, %r20, FSL_E5500
763 * The __boot_tlb1 table is used to hold BSP TLB1 entries
764 * marked with _TLB_ENTRY_SHARED flag during AP bootstrap.
765 * The BSP fills in the table in tlb_ap_prep() function. Next,
766 * AP loads its contents to TLB1 hardware in pmap_bootstrap_ap().
769 .space TLB1_MAX_ENTRIES * TLB_ENTRY_SIZE
773 * Boot page needs to be exactly 4K, with the last word of this page
774 * acting as the reset vector, so we need to stuff the remainder.
775 * Upon release from holdoff CPU fetches the last word of the boot
778 .space 4092 - (__boot_page_padding - __boot_page)
782 /************************************************************************/
783 /* locore subroutines */
784 /************************************************************************/
787 * Cache disable/enable/inval sequences according
788 * to section 2.16 of E500CORE RM.
791 /* Invalidate d-cache */
792 mfspr %r3, SPR_L1CSR0
793 ori %r3, %r3, (L1CSR0_DCFI | L1CSR0_DCLFR)@l
796 mtspr SPR_L1CSR0, %r3
798 1: mfspr %r3, SPR_L1CSR0
799 andi. %r3, %r3, L1CSR0_DCFI
803 ENTRY(dcache_disable)
804 /* Disable d-cache */
805 mfspr %r3, SPR_L1CSR0
811 mtspr SPR_L1CSR0, %r3
817 mfspr %r3, SPR_L1CSR0
818 oris %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@h
819 ori %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@l
822 mtspr SPR_L1CSR0, %r3
827 /* Invalidate i-cache */
828 mfspr %r3, SPR_L1CSR1
829 ori %r3, %r3, (L1CSR1_ICFI | L1CSR1_ICLFR)@l
831 mtspr SPR_L1CSR1, %r3
833 1: mfspr %r3, SPR_L1CSR1
834 andi. %r3, %r3, L1CSR1_ICFI
838 ENTRY(icache_disable)
839 /* Disable i-cache */
840 mfspr %r3, SPR_L1CSR1
845 mtspr SPR_L1CSR1, %r3
851 mfspr %r3, SPR_L1CSR1
852 oris %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@h
853 ori %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@l
855 mtspr SPR_L1CSR1, %r3
860 * L2 cache disable/enable/inval sequences for E500mc.
864 mfspr %r3, SPR_L2CSR0
865 oris %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@h
866 ori %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@l
868 mtspr SPR_L2CSR0, %r3
870 1: mfspr %r3, SPR_L2CSR0
871 andis. %r3, %r3, L2CSR0_L2FI@h
875 ENTRY(l2cache_enable)
876 mfspr %r3, SPR_L2CSR0
877 oris %r3, %r3, (L2CSR0_L2E | L2CSR0_L2PE)@h
879 mtspr SPR_L2CSR0, %r3
884 * Branch predictor setup.
888 ori %r3, %r3, BUCSR_BBFI
892 ori %r3, %r3, BUCSR_BPEN
899 * XXX: This should be moved to a shared AIM/booke asm file, if one ever is
906 /************************************************************************/
908 /************************************************************************/
911 GLOBAL(__startkernel)
919 .space 10240 /* XXX: this really should not be necessary */
922 TOC_ENTRY(bp_kernload)
926 * Compiled KERNBASE locations
929 .set kernbase, KERNBASE
931 #include <powerpc/booke/trap_subr.S>