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 */
259 /* Switch to the above TLB1[1] mapping */
266 rlwinm %r4, %r4, 0, 6, 31 /* Current offset from kernel load address */
267 rlwinm %r3, %r3, 0, 0, 19
269 add %r4, %r4, %r3 /* Convert to kernel virtual address */
270 addi %r4, %r4, (5f - 4b)
271 li %r3, PSL_DE /* Note AS=0 */
273 oris %r3, %r3, PSL_CM@h
280 * Invalidate temp mapping
289 /* Set up the TOC pointer */
294 .llong __tocbase + 0x8000 - .
300 /* Get load offset */
301 ld %r31,-0x8000(%r2) /* First TOC entry is TOC base */
302 subf %r31,%r31,%r2 /* Subtract from real TOC base to get base */
304 /* Set up the stack pointer */
305 ld %r1,TOC_REF(tmpstack)(%r2)
306 addi %r1,%r1,TMPSTACKSZ-96
316 * Setup a temporary stack
323 addi %r1, %r1, (TMPSTACKSZ - 16)
330 .long _GLOBAL_OFFSET_TABLE_-.
332 lwz %r3,0(%r5) /* _DYNAMIC in %r3 */
334 lwz %r4,4(%r5) /* GOT pointer */
336 lwz %r4,4(%r4) /* got[0] is _DYNAMIC link addr */
337 subf %r4,%r4,%r3 /* subtract to calculate relocbase */
339 bl CNAME(elf_reloc_self)
343 * Initialise exception vector offsets
349 * Set up arguments and jump to system initialization code
358 /* Switch to thread0.td_kstack now */
363 /* Machine independet part, does not return */
371 /************************************************************************/
373 /************************************************************************/
389 * Initial configuration
392 mflr %r31 /* r31 hold the address of bp_trace */
396 rlwinm %r3, %r3, 16, 16, 31
398 /* HID0 for E500 is default */
399 lis %r4, HID0_E500_DEFAULT_SET@h
400 ori %r4, %r4, HID0_E500_DEFAULT_SET@l
402 cmpli 0, 0, %r3, FSL_E500mc
404 lis %r4, HID0_E500MC_DEFAULT_SET@h
405 ori %r4, %r4, HID0_E500MC_DEFAULT_SET@l
408 cmpli 0, 0, %r3, FSL_E5500
410 lis %r4, HID0_E5500_DEFAULT_SET@h
411 ori %r4, %r4, HID0_E5500_DEFAULT_SET@l
416 /* Enable branch prediction */
421 /* Invalidate all entries in TLB0 */
426 * Find TLB1 entry which is translating us now
430 bl tlb1_find_current /* the entry number found is in r29 */
432 bl tlb1_inval_all_but_current
435 * Create temporary translation in AS=1 and switch to it
438 bl tlb1_temp_mapping_as1
441 ori %r3, %r3, (PSL_IS | PSL_DS)
443 oris %r3, %r3, PSL_CM@h
447 addi %r4, %r4, (4f - 3b)
450 rfi /* Switch context */
453 * Invalidate initial entry
460 * Setup final mapping in TLB1[1] and switch to it
462 /* Final kernel mapping, map in 64 MB of RAM */
463 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
464 li %r4, 0 /* Entry 0 */
465 rlwimi %r3, %r4, 16, 4, 15
469 li %r3, (TLB_SIZE_64M << MAS1_TSIZE_SHIFT)@l
470 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
471 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
474 LOAD_ADDR(%r3, VM_MIN_KERNEL_ADDRESS)
475 ori %r3, %r3, (_TLB_ENTRY_SHARED | MAS2_M)@l /* WIMGE = 0b00100 */
479 /* Retrieve kernel load [physical] address from bp_kernload */
497 rlwinm %r3, %r3, 0, 0, 19
499 sub %r4, %r4, %r5 /* offset of bp_kernload within __boot_page */
502 /* Set RPN and protection */
503 ori %r3, %r3, (MAS3_SX | MAS3_SW | MAS3_SR)@l
512 /* Switch to the final mapping */
515 rlwinm %r3, %r3, 0, 0xfff /* Offset from boot page start */
516 add %r3, %r3, %r5 /* Make this virtual address */
517 addi %r3, %r3, (7f - 6b)
519 lis %r4, PSL_CM@h /* Note AS=0 */
521 li %r4, 0 /* Note AS=0 */
529 * At this point we're running at virtual addresses VM_MIN_KERNEL_ADDRESS and
530 * beyond so it's allowed to directly access all locations the kernel was linked
535 * Invalidate temp mapping
541 /* Set up the TOC pointer */
546 .llong __tocbase + 0x8000 - .
552 /* Set up the stack pointer */
553 ld %r1,TOC_REF(tmpstack)(%r2)
554 addi %r1,%r1,TMPSTACKSZ-96
557 * Setup a temporary stack
565 addi %r1, %r1, (TMPSTACKSZ - 16)
569 * Initialise exception vector offsets
575 * Assign our pcpu instance
585 bl CNAME(pmap_bootstrap_ap)
588 bl CNAME(cpudep_ap_bootstrap)
590 /* Switch to the idle thread's kstack */
593 bl CNAME(machdep_ap_bootstrap)
600 #if defined (BOOKE_E500)
602 * Invalidate all entries in the given TLB.
607 rlwinm %r3, %r3, 3, (1 << 3) /* TLBSEL */
608 ori %r3, %r3, (1 << 2) /* INVALL */
618 * expects address to look up in r3, returns entry number in r29
620 * FIXME: the hidden assumption is we are now running in AS=0, but we should
621 * retrieve actual AS from MSR[IS|DS] and put it in MAS6[SAS]
625 slwi %r17, %r17, MAS6_SPID0_SHIFT
630 rlwinm %r29, %r17, 16, 26, 31 /* MAS0[ESEL] -> r29 */
632 /* Make sure we have IPROT set on the entry */
634 oris %r17, %r17, MAS1_IPROT@h
643 * Invalidates a single entry in TLB1.
649 lis %r4, MAS0_TLBSEL1@h /* Select TLB1 */
650 rlwimi %r4, %r3, 16, 10, 15 /* Select our entry */
654 li %r5, 0 /* MAS1[V] = 0 */
663 * r29 current entry number
664 * r28 returned temp entry
667 tlb1_temp_mapping_as1:
668 /* Read our current translation */
669 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
670 rlwimi %r3, %r29, 16, 10, 15 /* Select our current entry */
676 * Prepare and write temp entry
678 * FIXME this is not robust against overflow i.e. when the current
679 * entry is the last in TLB1
681 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
682 addi %r28, %r29, 1 /* Use next entry. */
683 rlwimi %r3, %r28, 16, 10, 15 /* Select temp entry */
688 rlwimi %r5, %r4, 12, 19, 19
689 li %r4, 0 /* Global mapping, TID=0 */
690 rlwimi %r5, %r4, 16, 8, 15
691 oris %r5, %r5, (MAS1_VALID | MAS1_IPROT)@h
704 * Loops over TLB1, invalidates all entries skipping the one which currently
710 tlb1_inval_all_but_current:
711 mfspr %r3, SPR_TLB1CFG /* Get number of entries */
712 andi. %r3, %r3, TLBCFG_NENTRY_MASK@l
713 li %r4, 0 /* Start from Entry 0 */
714 1: lis %r5, MAS0_TLBSEL1@h
715 rlwimi %r5, %r4, 16, 10, 15
720 cmpw %r4, %r29 /* our current entry? */
722 rlwinm %r5, %r5, 0, 2, 31 /* clear VALID and IPROT bits */
729 cmpw %r4, %r3 /* Check if this is the last entry */
734 * MAS7 and MAS8 conditional zeroing.
739 rlwinm %r20, %r20, 16, 16, 31
740 cmpli 0, 0, %r20, FSL_E500v1
752 rlwinm %r20, %r20, 16, 16, 31
753 cmpli 0, 0, %r20, FSL_E500mc
755 cmpli 0, 0, %r20, FSL_E5500
769 * The __boot_tlb1 table is used to hold BSP TLB1 entries
770 * marked with _TLB_ENTRY_SHARED flag during AP bootstrap.
771 * The BSP fills in the table in tlb_ap_prep() function. Next,
772 * AP loads its contents to TLB1 hardware in pmap_bootstrap_ap().
775 .space TLB1_MAX_ENTRIES * TLB_ENTRY_SIZE
779 * Boot page needs to be exactly 4K, with the last word of this page
780 * acting as the reset vector, so we need to stuff the remainder.
781 * Upon release from holdoff CPU fetches the last word of the boot
784 .space 4092 - (__boot_page_padding - __boot_page)
788 /************************************************************************/
789 /* locore subroutines */
790 /************************************************************************/
793 * Cache disable/enable/inval sequences according
794 * to section 2.16 of E500CORE RM.
797 /* Invalidate d-cache */
798 mfspr %r3, SPR_L1CSR0
799 ori %r3, %r3, (L1CSR0_DCFI | L1CSR0_DCLFR)@l
802 mtspr SPR_L1CSR0, %r3
804 1: mfspr %r3, SPR_L1CSR0
805 andi. %r3, %r3, L1CSR0_DCFI
809 ENTRY(dcache_disable)
810 /* Disable d-cache */
811 mfspr %r3, SPR_L1CSR0
817 mtspr SPR_L1CSR0, %r3
823 mfspr %r3, SPR_L1CSR0
824 oris %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@h
825 ori %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@l
828 mtspr SPR_L1CSR0, %r3
833 /* Invalidate i-cache */
834 mfspr %r3, SPR_L1CSR1
835 ori %r3, %r3, (L1CSR1_ICFI | L1CSR1_ICLFR)@l
837 mtspr SPR_L1CSR1, %r3
839 1: mfspr %r3, SPR_L1CSR1
840 andi. %r3, %r3, L1CSR1_ICFI
844 ENTRY(icache_disable)
845 /* Disable i-cache */
846 mfspr %r3, SPR_L1CSR1
851 mtspr SPR_L1CSR1, %r3
857 mfspr %r3, SPR_L1CSR1
858 oris %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@h
859 ori %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@l
861 mtspr SPR_L1CSR1, %r3
866 * L2 cache disable/enable/inval sequences for E500mc.
870 mfspr %r3, SPR_L2CSR0
871 oris %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@h
872 ori %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@l
874 mtspr SPR_L2CSR0, %r3
876 1: mfspr %r3, SPR_L2CSR0
877 andis. %r3, %r3, L2CSR0_L2FI@h
881 ENTRY(l2cache_enable)
882 mfspr %r3, SPR_L2CSR0
883 oris %r3, %r3, (L2CSR0_L2E | L2CSR0_L2PE)@h
885 mtspr SPR_L2CSR0, %r3
890 * Branch predictor setup.
894 ori %r3, %r3, BUCSR_BBFI
898 ori %r3, %r3, BUCSR_BPEN
905 * XXX: This should be moved to a shared AIM/booke asm file, if one ever is
912 /************************************************************************/
914 /************************************************************************/
917 GLOBAL(__startkernel)
925 .space 10240 /* XXX: this really should not be necessary */
928 TOC_ENTRY(bp_kernload)
932 * Compiled KERNBASE locations
935 .set kernbase, KERNBASE
937 #include <powerpc/booke/trap_subr.S>