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
61 #define GET_TOCBASE(r)
72 #define THREAD_REG %r2
82 * This symbol is here for the benefit of kvm_mkdb, and is supposed to
83 * mark the start of kernel text.
89 * Startup entry. Note, this must be the first thing in the text segment!
96 * Assumptions on the boot loader:
97 * - System memory starts from physical address 0
98 * - It's mapped by a single TLB1 entry
99 * - TLB1 mapping is 1:1 pa to va
100 * - Kernel is loaded at 64MB boundary
101 * - All PID registers are set to the same value
102 * - CPU is running in AS=0
104 * Registers contents provided by the loader(8):
106 * r3 : metadata pointer
108 * We rearrange the TLB1 layout as follows:
109 * - Find TLB1 entry we started in
110 * - Make sure it's protected, invalidate other entries
111 * - Create temp entry in the second AS (make sure it's not TLB[1])
112 * - Switch to temp mapping
113 * - Map 64MB of RAM in TLB1[1]
114 * - Use AS=1, set EPN to KERNBASE and RPN to kernel load address
115 * - Switch to to TLB1[1] mapping
116 * - Invalidate temp mapping
118 * locore registers use:
120 * r2 : trace pointer (AP only, for early diagnostics)
121 * r3-r27 : scratch registers
122 * r28 : temp TLB1 entry
123 * r29 : initial TLB1 entry we started in
124 * r30-r31 : arguments (metadata pointer)
128 * Keep arguments in r30 & r31 for later use.
136 li %r3, PSL_DE /* Keep debug exceptions for CodeWarrior. */
138 oris %r3, %r3, PSL_CM@h
144 * Initial HIDs configuration
148 rlwinm %r3, %r3, 16, 16, 31
150 lis %r4, HID0_E500_DEFAULT_SET@h
151 ori %r4, %r4, HID0_E500_DEFAULT_SET@l
153 /* Check for e500mc and e5500 */
154 cmpli 0, 0, %r3, FSL_E500mc
157 lis %r4, HID0_E500MC_DEFAULT_SET@h
158 ori %r4, %r4, HID0_E500MC_DEFAULT_SET@l
161 cmpli 0, 0, %r3, FSL_E5500
164 lis %r4, HID0_E5500_DEFAULT_SET@h
165 ori %r4, %r4, HID0_E5500_DEFAULT_SET@l
172 * E500mc and E5500 do not have HID1 register, so skip HID1 setup on
175 cmpli 0, 0, %r3, FSL_E500mc
177 cmpli 0, 0, %r3, FSL_E5500
179 cmpli 0, 0, %r3, FSL_E6500
182 lis %r3, HID1_E500_DEFAULT_SET@h
183 ori %r3, %r3, HID1_E500_DEFAULT_SET@l
187 /* Invalidate all entries in TLB0 */
195 * Locate the TLB1 entry that maps this code
199 bl tlb1_find_current /* the entry found is returned in r29 */
201 bl tlb1_inval_all_but_current
204 * Create temporary mapping in AS=1 and switch to it
206 bl tlb1_temp_mapping_as1
209 ori %r3, %r3, (PSL_IS | PSL_DS)
212 addi %r4, %r4, (3f - 2b)
215 rfi /* Switch context */
218 * Invalidate initial entry
225 * Setup final mapping in TLB1[1] and switch to it
227 /* Final kernel mapping, map in 64 MB of RAM */
228 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
229 li %r4, 0 /* Entry 0 */
230 rlwimi %r3, %r4, 16, 10, 15
234 li %r3, (TLB_SIZE_64M << MAS1_TSIZE_SHIFT)@l
235 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
236 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
239 LOAD_ADDR(%r3, KERNBASE)
240 ori %r3, %r3, (_TLB_ENTRY_SHARED | MAS2_M)@l /* WIMGE = 0b00100 */
244 /* Discover phys load address */
246 3: mflr %r4 /* Use current address */
247 rlwinm %r4, %r4, 0, 0, 5 /* 64MB alignment mask */
248 ori %r4, %r4, (MAS3_SX | MAS3_SW | MAS3_SR)@l
249 mtspr SPR_MAS3, %r4 /* Set RPN and protection */
257 /* Switch to the above TLB1[1] mapping */
264 rlwinm %r4, %r4, 0, 6, 31 /* Current offset from kernel load address */
265 rlwinm %r3, %r3, 0, 0, 19
267 add %r4, %r4, %r3 /* Convert to kernel virtual address */
268 addi %r4, %r4, (5f - 4b)
269 li %r3, PSL_DE /* Note AS=0 */
271 oris %r3, %r3, PSL_CM@h
278 * Invalidate temp mapping
287 /* Set up the TOC pointer */
292 .llong __tocbase + 0x8000 - .
298 /* Get load offset */
299 ld %r31,-0x8000(%r2) /* First TOC entry is TOC base */
300 subf %r31,%r31,%r2 /* Subtract from real TOC base to get base */
302 /* Set up the stack pointer */
303 ld %r1,TOC_REF(tmpstack)(%r2)
304 addi %r1,%r1,TMPSTACKSZ-96
314 * Setup a temporary stack
321 addi %r1, %r1, (TMPSTACKSZ - 16)
328 .long _GLOBAL_OFFSET_TABLE_-.
330 lwz %r3,0(%r5) /* _DYNAMIC in %r3 */
332 lwz %r4,4(%r5) /* GOT pointer */
334 lwz %r4,4(%r4) /* got[0] is _DYNAMIC link addr */
335 subf %r4,%r4,%r3 /* subtract to calculate relocbase */
337 bl CNAME(elf_reloc_self)
341 * Initialise exception vector offsets
347 * Set up arguments and jump to system initialization code
356 /* Switch to thread0.td_kstack now */
361 /* Machine independet part, does not return */
369 /************************************************************************/
371 /************************************************************************/
387 * Initial configuration
390 mflr %r31 /* r31 hold the address of bp_trace */
394 rlwinm %r3, %r3, 16, 16, 31
396 /* HID0 for E500 is default */
397 lis %r4, HID0_E500_DEFAULT_SET@h
398 ori %r4, %r4, HID0_E500_DEFAULT_SET@l
400 cmpli 0, 0, %r3, FSL_E500mc
402 lis %r4, HID0_E500MC_DEFAULT_SET@h
403 ori %r4, %r4, HID0_E500MC_DEFAULT_SET@l
406 cmpli 0, 0, %r3, FSL_E5500
408 lis %r4, HID0_E5500_DEFAULT_SET@h
409 ori %r4, %r4, HID0_E5500_DEFAULT_SET@l
414 /* Enable branch prediction */
419 /* Invalidate all entries in TLB0 */
424 * Find TLB1 entry which is translating us now
428 bl tlb1_find_current /* the entry number found is in r29 */
430 bl tlb1_inval_all_but_current
433 * Create temporary translation in AS=1 and switch to it
436 bl tlb1_temp_mapping_as1
439 ori %r3, %r3, (PSL_IS | PSL_DS)
441 oris %r3, %r3, PSL_CM@h
445 addi %r4, %r4, (4f - 3b)
448 rfi /* Switch context */
451 * Invalidate initial entry
458 * Setup final mapping in TLB1[1] and switch to it
460 /* Final kernel mapping, map in 64 MB of RAM */
461 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
462 li %r4, 0 /* Entry 0 */
463 rlwimi %r3, %r4, 16, 4, 15
467 li %r3, (TLB_SIZE_64M << MAS1_TSIZE_SHIFT)@l
468 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
469 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
472 LOAD_ADDR(%r3, KERNBASE)
473 ori %r3, %r3, (_TLB_ENTRY_SHARED | MAS2_M)@l /* WIMGE = 0b00100 */
477 /* Retrieve kernel load [physical] address from bp_kernload */
495 rlwinm %r3, %r3, 0, 0, 19
497 sub %r4, %r4, %r5 /* offset of bp_kernload within __boot_page */
500 /* Set RPN and protection */
501 ori %r3, %r3, (MAS3_SX | MAS3_SW | MAS3_SR)@l
510 /* Switch to the final mapping */
513 rlwinm %r3, %r3, 0, 0xfff /* Offset from boot page start */
514 add %r3, %r3, %r5 /* Make this virtual address */
515 addi %r3, %r3, (7f - 6b)
517 lis %r4, PSL_CM@h /* Note AS=0 */
519 li %r4, 0 /* Note AS=0 */
527 * At this point we're running at virtual addresses KERNBASE and beyond so
528 * it's allowed to directly access all locations the kernel was linked
533 * Invalidate temp mapping
539 /* Set up the TOC pointer */
544 .llong __tocbase + 0x8000 - .
550 /* Get load offset */
551 ld %r31,-0x8000(%r2) /* First TOC entry is TOC base */
552 subf %r31,%r31,%r2 /* Subtract from real TOC base to get base */
554 /* Set up the stack pointer */
555 ld %r1,TOC_REF(tmpstack)(%r2)
556 addi %r1,%r1,TMPSTACKSZ-96
560 * Setup a temporary stack
568 addi %r1, %r1, (TMPSTACKSZ - 16)
572 * Initialise exception vector offsets
578 * Assign our pcpu instance
588 bl CNAME(pmap_bootstrap_ap)
591 bl CNAME(cpudep_ap_bootstrap)
593 /* Switch to the idle thread's kstack */
596 bl CNAME(machdep_ap_bootstrap)
603 #if defined (BOOKE_E500)
605 * Invalidate all entries in the given TLB.
610 rlwinm %r3, %r3, 3, (1 << 3) /* TLBSEL */
611 ori %r3, %r3, (1 << 2) /* INVALL */
621 * expects address to look up in r3, returns entry number in r29
623 * FIXME: the hidden assumption is we are now running in AS=0, but we should
624 * retrieve actual AS from MSR[IS|DS] and put it in MAS6[SAS]
628 slwi %r17, %r17, MAS6_SPID0_SHIFT
633 rlwinm %r29, %r17, 16, 26, 31 /* MAS0[ESEL] -> r29 */
635 /* Make sure we have IPROT set on the entry */
637 oris %r17, %r17, MAS1_IPROT@h
646 * Invalidates a single entry in TLB1.
652 lis %r4, MAS0_TLBSEL1@h /* Select TLB1 */
653 rlwimi %r4, %r3, 16, 10, 15 /* Select our entry */
657 li %r5, 0 /* MAS1[V] = 0 */
666 * r29 current entry number
667 * r28 returned temp entry
670 tlb1_temp_mapping_as1:
671 /* Read our current translation */
672 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
673 rlwimi %r3, %r29, 16, 10, 15 /* Select our current entry */
679 * Prepare and write temp entry
681 * FIXME this is not robust against overflow i.e. when the current
682 * entry is the last in TLB1
684 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
685 addi %r28, %r29, 1 /* Use next entry. */
686 rlwimi %r3, %r28, 16, 10, 15 /* Select temp entry */
691 rlwimi %r5, %r4, 12, 19, 19
692 li %r4, 0 /* Global mapping, TID=0 */
693 rlwimi %r5, %r4, 16, 8, 15
694 oris %r5, %r5, (MAS1_VALID | MAS1_IPROT)@h
707 * Loops over TLB1, invalidates all entries skipping the one which currently
713 tlb1_inval_all_but_current:
714 mfspr %r3, SPR_TLB1CFG /* Get number of entries */
715 andi. %r3, %r3, TLBCFG_NENTRY_MASK@l
716 li %r4, 0 /* Start from Entry 0 */
717 1: lis %r5, MAS0_TLBSEL1@h
718 rlwimi %r5, %r4, 16, 10, 15
723 cmpw %r4, %r29 /* our current entry? */
725 rlwinm %r5, %r5, 0, 2, 31 /* clear VALID and IPROT bits */
732 cmpw %r4, %r3 /* Check if this is the last entry */
737 * MAS7 and MAS8 conditional zeroing.
742 rlwinm %r20, %r20, 16, 16, 31
743 cmpli 0, 0, %r20, FSL_E500v1
755 rlwinm %r20, %r20, 16, 16, 31
756 cmpli 0, 0, %r20, FSL_E500mc
758 cmpli 0, 0, %r20, FSL_E5500
772 * The __boot_tlb1 table is used to hold BSP TLB1 entries
773 * marked with _TLB_ENTRY_SHARED flag during AP bootstrap.
774 * The BSP fills in the table in tlb_ap_prep() function. Next,
775 * AP loads its contents to TLB1 hardware in pmap_bootstrap_ap().
778 .space TLB1_MAX_ENTRIES * TLB_ENTRY_SIZE
782 * Boot page needs to be exactly 4K, with the last word of this page
783 * acting as the reset vector, so we need to stuff the remainder.
784 * Upon release from holdoff CPU fetches the last word of the boot
787 .space 4092 - (__boot_page_padding - __boot_page)
791 /************************************************************************/
792 /* locore subroutines */
793 /************************************************************************/
796 * Cache disable/enable/inval sequences according
797 * to section 2.16 of E500CORE RM.
800 /* Invalidate d-cache */
801 mfspr %r3, SPR_L1CSR0
802 ori %r3, %r3, (L1CSR0_DCFI | L1CSR0_DCLFR)@l
805 mtspr SPR_L1CSR0, %r3
807 1: mfspr %r3, SPR_L1CSR0
808 andi. %r3, %r3, L1CSR0_DCFI
812 ENTRY(dcache_disable)
813 /* Disable d-cache */
814 mfspr %r3, SPR_L1CSR0
820 mtspr SPR_L1CSR0, %r3
826 mfspr %r3, SPR_L1CSR0
827 oris %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@h
828 ori %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@l
831 mtspr SPR_L1CSR0, %r3
836 /* Invalidate i-cache */
837 mfspr %r3, SPR_L1CSR1
838 ori %r3, %r3, (L1CSR1_ICFI | L1CSR1_ICLFR)@l
840 mtspr SPR_L1CSR1, %r3
842 1: mfspr %r3, SPR_L1CSR1
843 andi. %r3, %r3, L1CSR1_ICFI
847 ENTRY(icache_disable)
848 /* Disable i-cache */
849 mfspr %r3, SPR_L1CSR1
854 mtspr SPR_L1CSR1, %r3
860 mfspr %r3, SPR_L1CSR1
861 oris %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@h
862 ori %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@l
864 mtspr SPR_L1CSR1, %r3
869 * L2 cache disable/enable/inval sequences for E500mc.
873 mfspr %r3, SPR_L2CSR0
874 oris %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@h
875 ori %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@l
877 mtspr SPR_L2CSR0, %r3
879 1: mfspr %r3, SPR_L2CSR0
880 andis. %r3, %r3, L2CSR0_L2FI@h
884 ENTRY(l2cache_enable)
885 mfspr %r3, SPR_L2CSR0
886 oris %r3, %r3, (L2CSR0_L2E | L2CSR0_L2PE)@h
888 mtspr SPR_L2CSR0, %r3
893 * Branch predictor setup.
897 ori %r3, %r3, BUCSR_BBFI
901 ori %r3, %r3, BUCSR_BPEN
907 ENTRY(dataloss_erratum_access)
908 /* Lock two cache lines into I-Cache */
910 mfspr %r11, SPR_L1CSR1
911 rlwinm %r11, %r11, 0, ~L1CSR1_ICUL
914 mtspr SPR_L1CSR1, %r11
923 mfspr %r11, SPR_L1CSR1
924 3: andi. %r11, %r11, L1CSR1_ICUL
930 mfspr %r11, SPR_L1CSR1
931 3: andi. %r11, %r11, L1CSR1_ICUL
936 /* Inside a locked cacheline, wait a while, write, then wait a while */
940 4: addis %r11, %r5, 0x100000@h /* wait around one million timebase ticks */
948 4: addis %r11, %r5, 0x100000@h /* wait around one million timebase ticks */
956 * Fill out the rest of this cache line and the next with nops,
957 * to ensure that nothing outside the locked area will be
958 * fetched due to a branch.
970 * XXX: This should be moved to a shared AIM/booke asm file, if one ever is
977 /************************************************************************/
979 /************************************************************************/
982 GLOBAL(__startkernel)
990 .space 10240 /* XXX: this really should not be necessary */
993 TOC_ENTRY(bp_kernload)
997 * Compiled KERNBASE locations
1000 .set kernbase, KERNBASE
1002 #include <powerpc/booke/trap_subr.S>