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>
46 #define TMPSTACKSZ 16384
49 #define GET_TOCBASE(r) \
51 #define TOC_RESTORE nop
61 #define THREAD_REG %r13
66 #define GET_TOCBASE(r)
77 #define THREAD_REG %r2
84 /* Placate lld by creating a kboot stub. */
85 .section ".text.kboot", "x", @progbits
94 * This symbol is here for the benefit of kvm_mkdb, and is supposed to
95 * mark the start of kernel text.
101 * Startup entry. Note, this must be the first thing in the text segment!
108 * Assumptions on the boot loader:
109 * - System memory starts from physical address 0
110 * - It's mapped by a single TLB1 entry
111 * - TLB1 mapping is 1:1 pa to va
112 * - Kernel is loaded at 64MB boundary
113 * - All PID registers are set to the same value
114 * - CPU is running in AS=0
116 * Registers contents provided by the loader(8):
118 * r3 : metadata pointer
120 * We rearrange the TLB1 layout as follows:
121 * - Find TLB1 entry we started in
122 * - Make sure it's protected, invalidate other entries
123 * - Create temp entry in the second AS (make sure it's not TLB[1])
124 * - Switch to temp mapping
125 * - Map 64MB of RAM in TLB1[1]
126 * - Use AS=0, set EPN to VM_MIN_KERNEL_ADDRESS and RPN to kernel load address
127 * - Switch to TLB1[1] mapping
128 * - Invalidate temp mapping
130 * locore registers use:
132 * r2 : trace pointer (AP only, for early diagnostics)
133 * r3-r27 : scratch registers
134 * r28 : temp TLB1 entry
135 * r29 : initial TLB1 entry we started in
136 * r30-r31 : arguments (metadata pointer)
140 * Keep arguments in r30 & r31 for later use.
148 li %r3, PSL_DE /* Keep debug exceptions for CodeWarrior. */
150 oris %r3, %r3, PSL_CM@h
156 * Initial HIDs configuration
160 rlwinm %r3, %r3, 16, 16, 31
162 lis %r4, HID0_E500_DEFAULT_SET@h
163 ori %r4, %r4, HID0_E500_DEFAULT_SET@l
165 /* Check for e500mc and e5500 */
166 cmpli 0, 0, %r3, FSL_E500mc
169 lis %r4, HID0_E500MC_DEFAULT_SET@h
170 ori %r4, %r4, HID0_E500MC_DEFAULT_SET@l
173 cmpli 0, 0, %r3, FSL_E5500
176 lis %r4, HID0_E5500_DEFAULT_SET@h
177 ori %r4, %r4, HID0_E5500_DEFAULT_SET@l
184 * E500mc and E5500 do not have HID1 register, so skip HID1 setup on
187 cmpli 0, 0, %r3, FSL_E500mc
189 cmpli 0, 0, %r3, FSL_E5500
191 cmpli 0, 0, %r3, FSL_E6500
194 lis %r3, HID1_E500_DEFAULT_SET@h
195 ori %r3, %r3, HID1_E500_DEFAULT_SET@l
199 /* Invalidate all entries in TLB0 */
207 * Locate the TLB1 entry that maps this code
211 bl tlb1_find_current /* the entry found is returned in r29 */
213 bl tlb1_inval_all_but_current
216 * Create temporary mapping in AS=1 and switch to it
218 bl tlb1_temp_mapping_as1
221 ori %r3, %r3, (PSL_IS | PSL_DS)
224 addi %r4, %r4, (3f - 2b)
227 rfi /* Switch context */
230 * Invalidate initial entry
237 * Setup final mapping in TLB1[1] and switch to it
239 /* Final kernel mapping, map in 64 MB of RAM */
240 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
241 li %r4, 0 /* Entry 0 */
242 rlwimi %r3, %r4, 16, 10, 15
246 li %r3, (TLB_SIZE_64M << MAS1_TSIZE_SHIFT)@l
247 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
248 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
251 LOAD_ADDR(%r3, VM_MIN_KERNEL_ADDRESS)
252 ori %r3, %r3, (_TLB_ENTRY_SHARED | MAS2_M)@l /* WIMGE = 0b00100 */
256 /* Discover phys load address */
258 3: mflr %r4 /* Use current address */
259 rlwinm %r4, %r4, 0, 0, 5 /* 64MB alignment mask */
260 ori %r4, %r4, (MAS3_SX | MAS3_SW | MAS3_SR)@l
261 mtspr SPR_MAS3, %r4 /* Set RPN and protection */
270 /* Switch to the above TLB1[1] mapping */
277 rlwinm %r4, %r4, 0, 6, 31 /* Current offset from kernel load address */
278 rlwinm %r3, %r3, 0, 0, 19
280 add %r4, %r4, %r3 /* Convert to kernel virtual address */
281 addi %r4, %r4, (5f - 4b)
282 li %r3, PSL_DE /* Note AS=0 */
284 oris %r3, %r3, PSL_CM@h
291 * Invalidate temp mapping
300 /* Set up the TOC pointer */
305 .llong __tocbase + 0x8000 - .
312 /* Get load offset */
313 ld %r31,-0x8000(%r2) /* First TOC entry is TOC base */
314 subf %r31,%r31,%r2 /* Subtract from real TOC base to get base */
316 /* Set up the stack pointer */
318 .llong tmpstack + TMPSTACKSZ - 96 - .
333 * Setup a temporary stack
340 addi %r1, %r1, (TMPSTACKSZ - 16)
347 .long _GLOBAL_OFFSET_TABLE_-.
349 lwz %r3,0(%r5) /* _DYNAMIC in %r3 */
351 lwz %r4,4(%r5) /* GOT pointer */
353 lwz %r4,4(%r4) /* got[0] is _DYNAMIC link addr */
354 subf %r4,%r4,%r3 /* subtract to calculate relocbase */
356 bl CNAME(elf_reloc_self)
360 * Initialise exception vector offsets
366 * Set up arguments and jump to system initialization code
375 /* Switch to thread0.td_kstack now */
380 /* Machine independet part, does not return */
388 /************************************************************************/
390 /************************************************************************/
396 * The boot page is a special page of memory used during AP bringup.
397 * Before the AP comes out of reset, the physical 4K page holding this
398 * code is arranged to be mapped at 0xfffff000 by use of
399 * platform-dependent registers.
401 * Alternatively, this page may be executed using an ePAPR-standardized
402 * method -- writing to the address specified in "cpu-release-addr".
404 * In either case, execution begins at the last instruction of the
405 * page, which is a branch back to the start of the page.
407 * The code in the page must do initial MMU setup and normalize the
408 * TLBs for regular operation in the correct address space before
409 * reading outside the page.
411 * This implementation accomplishes this by:
412 * 1) Wiping TLB0 and all TLB1 entries but the one currently in use.
413 * 2) Establishing a temporary 4K TLB1 mapping in AS=1, and switching
414 * to it with rfi. This entry must NOT be in TLB1 slot 0.
415 * (This is needed to give the code freedom to clean up AS=0.)
416 * 3) Removing the initial TLB1 entry, leaving us with a single valid
417 * TLB1 entry, NOT in slot 0.
418 * 4) Installing an AS0 entry in TLB1 slot 0 mapping the 64MB kernel
419 * segment at its final virtual address. A second rfi is done to
420 * switch to the final address space. At this point we can finally
421 * access the rest of the kernel segment safely.
422 * 5) The temporary TLB1 AS=1 entry is removed, finally leaving us in
423 * a consistent (but minimal) state.
424 * 6) Set up TOC, stack, and pcpu registers.
425 * 7) Now that we can finally call C code, call pmap_boostrap_ap(),
426 * which finishes copying in the shared TLB1 entries.
428 * At this point, the MMU is fully set up, and we can proceed with
429 * running the actual AP bootstrap code.
431 * Pieces of this code are also used for UP kernel, but in this case
432 * the sections specific to boot page functionality are dropped by
436 nop /* PPC64 alignment word. 64-bit target. */
438 bl 1f /* 32-bit target. */
442 ADDR(0) /* Trace pointer (%r31). */
446 .llong 0 /* Kern phys. load address. */
450 ADDR(0) /* Virt. address of __boot_page. */
453 * Initial configuration
456 mflr %r31 /* r31 hold the address of bp_trace */
460 rlwinm %r3, %r3, 16, 16, 31
462 /* HID0 for E500 is default */
463 lis %r4, HID0_E500_DEFAULT_SET@h
464 ori %r4, %r4, HID0_E500_DEFAULT_SET@l
466 cmpli 0, 0, %r3, FSL_E500mc
468 lis %r4, HID0_E500MC_DEFAULT_SET@h
469 ori %r4, %r4, HID0_E500MC_DEFAULT_SET@l
472 cmpli 0, 0, %r3, FSL_E5500
474 lis %r4, HID0_E5500_DEFAULT_SET@h
475 ori %r4, %r4, HID0_E5500_DEFAULT_SET@l
480 /* Enable branch prediction */
485 /* Invalidate all entries in TLB0 */
490 * Find TLB1 entry which is translating us now
494 bl tlb1_find_current /* the entry number found is in r29 */
496 bl tlb1_inval_all_but_current
499 * Create temporary translation in AS=1 and switch to it
502 bl tlb1_temp_mapping_as1
505 ori %r3, %r3, (PSL_IS | PSL_DS)
507 oris %r3, %r3, PSL_CM@h /* Ensure we're in 64-bit after RFI */
511 addi %r4, %r4, (4f - 3b)
514 rfi /* Switch context */
517 * Invalidate initial entry
524 * Setup final mapping in TLB1[0] and switch to it
526 /* Final kernel mapping, map in 64 MB of RAM */
527 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
528 li %r4, 0 /* Entry 0 */
529 rlwimi %r3, %r4, 16, 4, 15
533 li %r3, (TLB_SIZE_64M << MAS1_TSIZE_SHIFT)@l
534 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
535 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
538 LOAD_ADDR(%r3, VM_MIN_KERNEL_ADDRESS)
539 ori %r3, %r3, (_TLB_ENTRY_SHARED | MAS2_M)@l /* WIMGE = 0b00100 */
543 /* Retrieve kernel load [physical] address from bp_kernload */
547 clrrdi %r3, %r3, PAGE_SHIFT /* trunc_page(%r3) */
549 clrrwi %r3, %r3, PAGE_SHIFT /* trunc_page(%r3) */
551 /* Load lower half of the kernel loadaddr. */
552 lwz %r4, (bp_kernload - __boot_page + 4)(%r3)
553 LOAD %r5, (bp_virtaddr - __boot_page)(%r3)
555 /* Set RPN and protection */
556 ori %r4, %r4, (MAS3_SX | MAS3_SW | MAS3_SR)@l
559 lwz %r4, (bp_kernload - __boot_page)(%r3)
566 /* Switch to the final mapping */
569 rlwinm %r3, %r3, 0, 0xfff /* Offset from boot page start */
570 add %r3, %r3, %r5 /* Make this a virtual address */
571 addi %r3, %r3, (7f - 6b) /* And figure out return address. */
573 lis %r4, PSL_CM@h /* Note AS=0 */
575 li %r4, 0 /* Note AS=0 */
583 * At this point we're running at virtual addresses VM_MIN_KERNEL_ADDRESS and
584 * beyond so it's allowed to directly access all locations the kernel was linked
589 * Invalidate temp mapping
595 /* Set up the TOC pointer */
600 .llong __tocbase + 0x8000 - .
606 /* Set up the stack pointer */
607 addis %r1,%r2,TOC_REF(tmpstack)@ha
608 ld %r1,TOC_REF(tmpstack)@l(%r1)
609 addi %r1,%r1,TMPSTACKSZ-96
612 * Setup a temporary stack
620 addi %r1, %r1, (TMPSTACKSZ - 16)
624 * Initialise exception vector offsets
630 * Assign our pcpu instance
640 bl CNAME(pmap_bootstrap_ap)
643 bl CNAME(cpudep_ap_bootstrap)
645 /* Switch to the idle thread's kstack */
648 bl CNAME(machdep_ap_bootstrap)
655 #if defined (BOOKE_E500)
657 * Invalidate all entries in the given TLB.
662 rlwinm %r3, %r3, 3, (1 << 3) /* TLBSEL */
663 ori %r3, %r3, (1 << 2) /* INVALL */
673 * expects address to look up in r3, returns entry number in r29
675 * FIXME: the hidden assumption is we are now running in AS=0, but we should
676 * retrieve actual AS from MSR[IS|DS] and put it in MAS6[SAS]
680 slwi %r17, %r17, MAS6_SPID0_SHIFT
685 rlwinm %r29, %r17, 16, 26, 31 /* MAS0[ESEL] -> r29 */
687 /* Make sure we have IPROT set on the entry */
689 oris %r17, %r17, MAS1_IPROT@h
698 * Invalidates a single entry in TLB1.
704 lis %r4, MAS0_TLBSEL1@h /* Select TLB1 */
705 rlwimi %r4, %r3, 16, 10, 15 /* Select our entry */
709 li %r5, 0 /* MAS1[V] = 0 */
718 * r29 current entry number
719 * r28 returned temp entry
722 tlb1_temp_mapping_as1:
723 /* Read our current translation */
724 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
725 rlwimi %r3, %r29, 16, 10, 15 /* Select our current entry */
731 * Prepare and write temp entry
733 * FIXME this is not robust against overflow i.e. when the current
734 * entry is the last in TLB1
736 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
737 addi %r28, %r29, 1 /* Use next entry. */
738 rlwimi %r3, %r28, 16, 10, 15 /* Select temp entry */
743 rlwimi %r5, %r4, 12, 19, 19
744 li %r4, 0 /* Global mapping, TID=0 */
745 rlwimi %r5, %r4, 16, 8, 15
746 oris %r5, %r5, (MAS1_VALID | MAS1_IPROT)@h
760 * Loops over TLB1, invalidates all entries skipping the one which currently
766 tlb1_inval_all_but_current:
767 mfspr %r3, SPR_TLB1CFG /* Get number of entries */
768 andi. %r3, %r3, TLBCFG_NENTRY_MASK@l
769 li %r4, 0 /* Start from Entry 0 */
770 1: lis %r5, MAS0_TLBSEL1@h
771 rlwimi %r5, %r4, 16, 10, 15
776 cmpw %r4, %r29 /* our current entry? */
778 rlwinm %r5, %r5, 0, 2, 31 /* clear VALID and IPROT bits */
785 cmpw %r4, %r3 /* Check if this is the last entry */
793 * The __boot_tlb1 table is used to hold BSP TLB1 entries
794 * marked with _TLB_ENTRY_SHARED flag during AP bootstrap.
795 * The BSP fills in the table in tlb_ap_prep() function. Next,
796 * AP loads its contents to TLB1 hardware in pmap_bootstrap_ap().
799 .space TLB1_MAX_ENTRIES * TLB_ENTRY_SIZE
803 * Boot page needs to be exactly 4K, with the last word of this page
804 * acting as the reset vector, so we need to stuff the remainder.
805 * Upon release from holdoff CPU fetches the last word of the boot
808 .space 4092 - (__boot_page_padding - __boot_page)
811 * This is the end of the boot page.
812 * During AP startup, the previous instruction is at 0xfffffffc
813 * virtual (i.e. the reset vector.)
817 /************************************************************************/
818 /* locore subroutines */
819 /************************************************************************/
822 * Cache disable/enable/inval sequences according
823 * to section 2.16 of E500CORE RM.
826 /* Invalidate d-cache */
827 mfspr %r3, SPR_L1CSR0
828 ori %r3, %r3, (L1CSR0_DCFI | L1CSR0_DCLFR)@l
831 mtspr SPR_L1CSR0, %r3
833 1: mfspr %r3, SPR_L1CSR0
834 andi. %r3, %r3, L1CSR0_DCFI
838 ENTRY(dcache_disable)
839 /* Disable d-cache */
840 mfspr %r3, SPR_L1CSR0
846 mtspr SPR_L1CSR0, %r3
852 mfspr %r3, SPR_L1CSR0
853 oris %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@h
854 ori %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@l
857 mtspr SPR_L1CSR0, %r3
862 /* Invalidate i-cache */
863 mfspr %r3, SPR_L1CSR1
864 ori %r3, %r3, (L1CSR1_ICFI | L1CSR1_ICLFR)@l
866 mtspr SPR_L1CSR1, %r3
868 1: mfspr %r3, SPR_L1CSR1
869 andi. %r3, %r3, L1CSR1_ICFI
873 ENTRY(icache_disable)
874 /* Disable i-cache */
875 mfspr %r3, SPR_L1CSR1
880 mtspr SPR_L1CSR1, %r3
886 mfspr %r3, SPR_L1CSR1
887 oris %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@h
888 ori %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@l
890 mtspr SPR_L1CSR1, %r3
895 * L2 cache disable/enable/inval sequences for E500mc.
899 mfspr %r3, SPR_L2CSR0
900 oris %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@h
901 ori %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@l
903 mtspr SPR_L2CSR0, %r3
905 1: mfspr %r3, SPR_L2CSR0
906 andis. %r3, %r3, L2CSR0_L2FI@h
910 ENTRY(l2cache_enable)
911 mfspr %r3, SPR_L2CSR0
912 oris %r3, %r3, (L2CSR0_L2E | L2CSR0_L2PE)@h
914 mtspr SPR_L2CSR0, %r3
919 * Branch predictor setup.
923 ori %r3, %r3, BUCSR_BBFI
927 ori %r3, %r3, BUCSR_BPEN
934 * XXX: This should be moved to a shared AIM/booke asm file, if one ever is
938 /* Note: The spr number is patched at runtime */
942 /************************************************************************/
944 /************************************************************************/
947 GLOBAL(__startkernel)
955 .space 10240 /* XXX: this really should not be necessary */
959 TOC_ENTRY(bp_kernload)
964 * Compiled KERNBASE locations
967 .set kernbase, KERNBASE
969 #include <powerpc/booke/trap_subr.S>