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 ADDR(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 %r4, (bp_kernload - __boot_page)(%r3)
552 LOAD %r5, (bp_virtaddr - __boot_page)(%r3)
554 /* Set RPN and protection */
555 ori %r4, %r4, (MAS3_SX | MAS3_SW | MAS3_SR)@l
565 /* Switch to the final mapping */
568 rlwinm %r3, %r3, 0, 0xfff /* Offset from boot page start */
569 add %r3, %r3, %r5 /* Make this a virtual address */
570 addi %r3, %r3, (7f - 6b) /* And figure out return address. */
572 lis %r4, PSL_CM@h /* Note AS=0 */
574 li %r4, 0 /* Note AS=0 */
582 * At this point we're running at virtual addresses VM_MIN_KERNEL_ADDRESS and
583 * beyond so it's allowed to directly access all locations the kernel was linked
588 * Invalidate temp mapping
594 /* Set up the TOC pointer */
599 .llong __tocbase + 0x8000 - .
605 /* Set up the stack pointer */
606 addis %r1,%r2,TOC_REF(tmpstack)@ha
607 ld %r1,TOC_REF(tmpstack)@l(%r1)
608 addi %r1,%r1,TMPSTACKSZ-96
611 * Setup a temporary stack
619 addi %r1, %r1, (TMPSTACKSZ - 16)
623 * Initialise exception vector offsets
629 * Assign our pcpu instance
639 bl CNAME(pmap_bootstrap_ap)
642 bl CNAME(cpudep_ap_bootstrap)
644 /* Switch to the idle thread's kstack */
647 bl CNAME(machdep_ap_bootstrap)
654 #if defined (BOOKE_E500)
656 * Invalidate all entries in the given TLB.
661 rlwinm %r3, %r3, 3, (1 << 3) /* TLBSEL */
662 ori %r3, %r3, (1 << 2) /* INVALL */
672 * expects address to look up in r3, returns entry number in r29
674 * FIXME: the hidden assumption is we are now running in AS=0, but we should
675 * retrieve actual AS from MSR[IS|DS] and put it in MAS6[SAS]
679 slwi %r17, %r17, MAS6_SPID0_SHIFT
684 rlwinm %r29, %r17, 16, 26, 31 /* MAS0[ESEL] -> r29 */
686 /* Make sure we have IPROT set on the entry */
688 oris %r17, %r17, MAS1_IPROT@h
697 * Invalidates a single entry in TLB1.
703 lis %r4, MAS0_TLBSEL1@h /* Select TLB1 */
704 rlwimi %r4, %r3, 16, 10, 15 /* Select our entry */
708 li %r5, 0 /* MAS1[V] = 0 */
717 * r29 current entry number
718 * r28 returned temp entry
721 tlb1_temp_mapping_as1:
722 /* Read our current translation */
723 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
724 rlwimi %r3, %r29, 16, 10, 15 /* Select our current entry */
730 * Prepare and write temp entry
732 * FIXME this is not robust against overflow i.e. when the current
733 * entry is the last in TLB1
735 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
736 addi %r28, %r29, 1 /* Use next entry. */
737 rlwimi %r3, %r28, 16, 10, 15 /* Select temp entry */
742 rlwimi %r5, %r4, 12, 19, 19
743 li %r4, 0 /* Global mapping, TID=0 */
744 rlwimi %r5, %r4, 16, 8, 15
745 oris %r5, %r5, (MAS1_VALID | MAS1_IPROT)@h
759 * Loops over TLB1, invalidates all entries skipping the one which currently
765 tlb1_inval_all_but_current:
766 mfspr %r3, SPR_TLB1CFG /* Get number of entries */
767 andi. %r3, %r3, TLBCFG_NENTRY_MASK@l
768 li %r4, 0 /* Start from Entry 0 */
769 1: lis %r5, MAS0_TLBSEL1@h
770 rlwimi %r5, %r4, 16, 10, 15
775 cmpw %r4, %r29 /* our current entry? */
777 rlwinm %r5, %r5, 0, 2, 31 /* clear VALID and IPROT bits */
784 cmpw %r4, %r3 /* Check if this is the last entry */
792 * The __boot_tlb1 table is used to hold BSP TLB1 entries
793 * marked with _TLB_ENTRY_SHARED flag during AP bootstrap.
794 * The BSP fills in the table in tlb_ap_prep() function. Next,
795 * AP loads its contents to TLB1 hardware in pmap_bootstrap_ap().
798 .space TLB1_MAX_ENTRIES * TLB_ENTRY_SIZE
802 * Boot page needs to be exactly 4K, with the last word of this page
803 * acting as the reset vector, so we need to stuff the remainder.
804 * Upon release from holdoff CPU fetches the last word of the boot
807 .space 4092 - (__boot_page_padding - __boot_page)
810 * This is the end of the boot page.
811 * During AP startup, the previous instruction is at 0xfffffffc
812 * virtual (i.e. the reset vector.)
816 /************************************************************************/
817 /* locore subroutines */
818 /************************************************************************/
821 * Cache disable/enable/inval sequences according
822 * to section 2.16 of E500CORE RM.
825 /* Invalidate d-cache */
826 mfspr %r3, SPR_L1CSR0
827 ori %r3, %r3, (L1CSR0_DCFI | L1CSR0_DCLFR)@l
830 mtspr SPR_L1CSR0, %r3
832 1: mfspr %r3, SPR_L1CSR0
833 andi. %r3, %r3, L1CSR0_DCFI
837 ENTRY(dcache_disable)
838 /* Disable d-cache */
839 mfspr %r3, SPR_L1CSR0
845 mtspr SPR_L1CSR0, %r3
851 mfspr %r3, SPR_L1CSR0
852 oris %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@h
853 ori %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@l
856 mtspr SPR_L1CSR0, %r3
861 /* Invalidate i-cache */
862 mfspr %r3, SPR_L1CSR1
863 ori %r3, %r3, (L1CSR1_ICFI | L1CSR1_ICLFR)@l
865 mtspr SPR_L1CSR1, %r3
867 1: mfspr %r3, SPR_L1CSR1
868 andi. %r3, %r3, L1CSR1_ICFI
872 ENTRY(icache_disable)
873 /* Disable i-cache */
874 mfspr %r3, SPR_L1CSR1
879 mtspr SPR_L1CSR1, %r3
885 mfspr %r3, SPR_L1CSR1
886 oris %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@h
887 ori %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@l
889 mtspr SPR_L1CSR1, %r3
894 * L2 cache disable/enable/inval sequences for E500mc.
898 mfspr %r3, SPR_L2CSR0
899 oris %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@h
900 ori %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@l
902 mtspr SPR_L2CSR0, %r3
904 1: mfspr %r3, SPR_L2CSR0
905 andis. %r3, %r3, L2CSR0_L2FI@h
909 ENTRY(l2cache_enable)
910 mfspr %r3, SPR_L2CSR0
911 oris %r3, %r3, (L2CSR0_L2E | L2CSR0_L2PE)@h
913 mtspr SPR_L2CSR0, %r3
918 * Branch predictor setup.
922 ori %r3, %r3, BUCSR_BBFI
926 ori %r3, %r3, BUCSR_BPEN
933 * XXX: This should be moved to a shared AIM/booke asm file, if one ever is
937 /* Note: The spr number is patched at runtime */
941 /************************************************************************/
943 /************************************************************************/
946 GLOBAL(__startkernel)
954 .space 10240 /* XXX: this really should not be necessary */
958 TOC_ENTRY(bp_kernload)
963 * Compiled KERNBASE locations
966 .set kernbase, KERNBASE
968 #include <powerpc/booke/trap_subr.S>