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
49 * This symbol is here for the benefit of kvm_mkdb, and is supposed to
50 * mark the start of kernel text.
56 * Startup entry. Note, this must be the first thing in the text segment!
63 * Assumptions on the boot loader:
64 * - System memory starts from physical address 0
65 * - It's mapped by a single TLB1 entry
66 * - TLB1 mapping is 1:1 pa to va
67 * - Kernel is loaded at 64MB boundary
68 * - All PID registers are set to the same value
69 * - CPU is running in AS=0
71 * Registers contents provided by the loader(8):
73 * r3 : metadata pointer
75 * We rearrange the TLB1 layout as follows:
76 * - Find TLB1 entry we started in
77 * - Make sure it's protected, invalidate other entries
78 * - Create temp entry in the second AS (make sure it's not TLB[1])
79 * - Switch to temp mapping
80 * - Map 64MB of RAM in TLB1[1]
81 * - Use AS=1, set EPN to KERNBASE and RPN to kernel load address
82 * - Switch to TLB1[1] mapping
83 * - Invalidate temp mapping
85 * locore registers use:
87 * r2 : trace pointer (AP only, for early diagnostics)
88 * r3-r27 : scratch registers
89 * r28 : temp TLB1 entry
90 * r29 : initial TLB1 entry we started in
91 * r30-r31 : arguments (metadata pointer)
95 * Keep arguments in r30 & r31 for later use.
103 li %r3, PSL_DE /* Keep debug exceptions for CodeWarrior. */
108 * Initial HIDs configuration
112 rlwinm %r3, %r3, 16, 16, 31
114 lis %r4, HID0_E500_DEFAULT_SET@h
115 ori %r4, %r4, HID0_E500_DEFAULT_SET@l
117 /* Check for e500mc and e5500 */
118 cmpli 0, 0, %r3, FSL_E500mc
121 lis %r4, HID0_E500MC_DEFAULT_SET@h
122 ori %r4, %r4, HID0_E500MC_DEFAULT_SET@l
125 cmpli 0, 0, %r3, FSL_E5500
128 lis %r4, HID0_E5500_DEFAULT_SET@h
129 ori %r4, %r4, HID0_E5500_DEFAULT_SET@l
136 * E500mc and E5500 do not have HID1 register, so skip HID1 setup on
139 cmpli 0, 0, %r3, FSL_E500mc
141 cmpli 0, 0, %r3, FSL_E5500
144 lis %r3, HID1_E500_DEFAULT_SET@h
145 ori %r3, %r3, HID1_E500_DEFAULT_SET@l
149 /* Invalidate all entries in TLB0 */
157 * Locate the TLB1 entry that maps this code
161 bl tlb1_find_current /* the entry found is returned in r29 */
163 bl tlb1_inval_all_but_current
166 * Create temporary mapping in AS=1 and switch to it
168 bl tlb1_temp_mapping_as1
171 ori %r3, %r3, (PSL_IS | PSL_DS)
177 rfi /* Switch context */
180 * Invalidate initial entry
186 * Setup final mapping in TLB1[1] and switch to it
188 /* Final kernel mapping, map in 64 MB of RAM */
189 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
190 li %r4, 0 /* Entry 0 */
191 rlwimi %r3, %r4, 16, 10, 15
195 li %r3, (TLB_SIZE_64M << MAS1_TSIZE_SHIFT)@l
196 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
197 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
201 ori %r3, %r3, KERNBASE@l /* EPN = KERNBASE */
203 ori %r3, %r3, (_TLB_ENTRY_SHARED | MAS2_M)@l /* WIMGE = 0b00100 */
208 /* Discover phys load address */
210 3: mflr %r4 /* Use current address */
211 rlwinm %r4, %r4, 0, 0, 5 /* 64MB alignment mask */
212 ori %r4, %r4, (MAS3_SX | MAS3_SW | MAS3_SR)@l
213 mtspr SPR_MAS3, %r4 /* Set RPN and protection */
221 /* Switch to the above TLB1[1] mapping */
224 rlwinm %r4, %r4, 0, 8, 31 /* Current offset from kernel load address */
225 rlwinm %r3, %r3, 0, 0, 19
226 add %r4, %r4, %r3 /* Convert to kernel virtual address */
228 li %r3, PSL_DE /* Note AS=0 */
234 * Invalidate temp mapping
242 * Setup a temporary stack
249 addi %r1, %r1, (TMPSTACKSZ - 16)
256 .long _GLOBAL_OFFSET_TABLE_-.
258 lwz %r3,0(%r5) /* _DYNAMIC in %r3 */
260 lwz %r4,4(%r5) /* GOT pointer */
262 lwz %r4,4(%r4) /* got[0] is _DYNAMIC link addr */
263 subf %r4,%r4,%r3 /* subtract to calculate relocbase */
267 * Initialise exception vector offsets
272 * Set up arguments and jump to system initialization code
280 /* Switch to thread0.td_kstack now */
285 /* Machine independet part, does not return */
292 /************************************************************************/
294 /************************************************************************/
310 * Initial configuration
313 mflr %r31 /* r31 hold the address of bp_trace */
317 rlwinm %r3, %r3, 16, 16, 31
319 /* HID0 for E500 is default */
320 lis %r4, HID0_E500_DEFAULT_SET@h
321 ori %r4, %r4, HID0_E500_DEFAULT_SET@l
323 cmpli 0, 0, %r3, FSL_E500mc
325 lis %r4, HID0_E500MC_DEFAULT_SET@h
326 ori %r4, %r4, HID0_E500MC_DEFAULT_SET@l
329 cmpli 0, 0, %r3, FSL_E5500
331 lis %r4, HID0_E5500_DEFAULT_SET@h
332 ori %r4, %r4, HID0_E5500_DEFAULT_SET@l
337 /* Enable branch prediction */
342 /* Invalidate all entries in TLB0 */
347 * Find TLB1 entry which is translating us now
351 bl tlb1_find_current /* the entry number found is in r29 */
353 bl tlb1_inval_all_but_current
356 * Create temporary translation in AS=1 and switch to it
359 bl tlb1_temp_mapping_as1
362 ori %r3, %r3, (PSL_IS | PSL_DS)
368 rfi /* Switch context */
371 * Invalidate initial entry
377 * Setup final mapping in TLB1[1] and switch to it
379 /* Final kernel mapping, map in 64 MB of RAM */
380 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
381 li %r4, 0 /* Entry 0 */
382 rlwimi %r3, %r4, 16, 4, 15
386 li %r3, (TLB_SIZE_64M << MAS1_TSIZE_SHIFT)@l
387 oris %r3, %r3, (MAS1_VALID | MAS1_IPROT)@h
388 mtspr SPR_MAS1, %r3 /* note TS was not filled, so it's TS=0 */
392 ori %r3, %r3, KERNBASE@l /* EPN = KERNBASE */
393 ori %r3, %r3, (_TLB_ENTRY_SHARED | MAS2_M)@l /* WIMGE = 0b00100 */
397 /* Retrieve kernel load [physical] address from bp_kernload */
404 rlwinm %r3, %r3, 0, 0, 19
405 sub %r4, %r4, %r5 /* offset of bp_kernload within __boot_page */
408 /* Set RPN and protection */
409 ori %r3, %r3, (MAS3_SX | MAS3_SW | MAS3_SR)@l
418 /* Switch to the final mapping */
421 rlwinm %r3, %r3, 0, 0xfff /* Offset from boot page start */
422 add %r3, %r3, %r5 /* Make this virtual address */
424 li %r4, 0 /* Note AS=0 */
430 * At this point we're running at virtual addresses KERNBASE and beyond so
431 * it's allowed to directly access all locations the kernel was linked
436 * Invalidate temp mapping
442 * Setup a temporary stack
450 addi %r1, %r1, (TMPSTACKSZ - 16)
453 * Initialise exception vector offsets
458 * Assign our pcpu instance
470 bl cpudep_ap_bootstrap
471 /* Switch to the idle thread's kstack */
474 bl machdep_ap_bootstrap
480 #if defined (BOOKE_E500)
482 * Invalidate all entries in the given TLB.
487 rlwinm %r3, %r3, 3, (1 << 3) /* TLBSEL */
488 ori %r3, %r3, (1 << 2) /* INVALL */
498 * expects address to look up in r3, returns entry number in r29
500 * FIXME: the hidden assumption is we are now running in AS=0, but we should
501 * retrieve actual AS from MSR[IS|DS] and put it in MAS6[SAS]
505 slwi %r17, %r17, MAS6_SPID0_SHIFT
510 rlwinm %r29, %r17, 16, 26, 31 /* MAS0[ESEL] -> r29 */
512 /* Make sure we have IPROT set on the entry */
514 oris %r17, %r17, MAS1_IPROT@h
523 * Invalidates a single entry in TLB1.
529 lis %r4, MAS0_TLBSEL1@h /* Select TLB1 */
530 rlwimi %r4, %r3, 16, 10, 15 /* Select our entry */
534 li %r5, 0 /* MAS1[V] = 0 */
543 * r29 current entry number
544 * r28 returned temp entry
547 tlb1_temp_mapping_as1:
548 /* Read our current translation */
549 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
550 rlwimi %r3, %r29, 16, 10, 15 /* Select our current entry */
556 * Prepare and write temp entry
558 * FIXME this is not robust against overflow i.e. when the current
559 * entry is the last in TLB1
561 lis %r3, MAS0_TLBSEL1@h /* Select TLB1 */
562 addi %r28, %r29, 1 /* Use next entry. */
563 rlwimi %r3, %r28, 16, 10, 15 /* Select temp entry */
568 rlwimi %r5, %r4, 12, 19, 19
569 li %r4, 0 /* Global mapping, TID=0 */
570 rlwimi %r5, %r4, 16, 8, 15
571 oris %r5, %r5, (MAS1_VALID | MAS1_IPROT)@h
584 * Loops over TLB1, invalidates all entries skipping the one which currently
590 tlb1_inval_all_but_current:
592 mfspr %r3, SPR_TLB1CFG /* Get number of entries */
593 andi. %r3, %r3, TLBCFG_NENTRY_MASK@l
594 li %r4, 0 /* Start from Entry 0 */
595 1: lis %r5, MAS0_TLBSEL1@h
596 rlwimi %r5, %r4, 16, 10, 15
601 cmpw %r4, %r29 /* our current entry? */
603 rlwinm %r5, %r5, 0, 2, 31 /* clear VALID and IPROT bits */
610 cmpw %r4, %r3 /* Check if this is the last entry */
615 * MAS7 and MAS8 conditional zeroing.
620 rlwinm %r20, %r20, 16, 16, 31
621 cmpli 0, 0, %r20, FSL_E500v1
633 rlwinm %r20, %r20, 16, 16, 31
634 cmpli 0, 0, %r20, FSL_E500mc
636 cmpli 0, 0, %r20, FSL_E5500
650 * The __boot_tlb1 table is used to hold BSP TLB1 entries
651 * marked with _TLB_ENTRY_SHARED flag during AP bootstrap.
652 * The BSP fills in the table in tlb_ap_prep() function. Next,
653 * AP loads its contents to TLB1 hardware in pmap_bootstrap_ap().
656 .space TLB1_MAX_ENTRIES * TLB_ENTRY_SIZE
660 * Boot page needs to be exactly 4K, with the last word of this page
661 * acting as the reset vector, so we need to stuff the remainder.
662 * Upon release from holdoff CPU fetches the last word of the boot
665 .space 4092 - (__boot_page_padding - __boot_page)
669 /************************************************************************/
670 /* locore subroutines */
671 /************************************************************************/
674 * Cache disable/enable/inval sequences according
675 * to section 2.16 of E500CORE RM.
678 /* Invalidate d-cache */
679 mfspr %r3, SPR_L1CSR0
680 ori %r3, %r3, (L1CSR0_DCFI | L1CSR0_DCLFR)@l
683 mtspr SPR_L1CSR0, %r3
685 1: mfspr %r3, SPR_L1CSR0
686 andi. %r3, %r3, L1CSR0_DCFI
690 ENTRY(dcache_disable)
691 /* Disable d-cache */
692 mfspr %r3, SPR_L1CSR0
698 mtspr SPR_L1CSR0, %r3
704 mfspr %r3, SPR_L1CSR0
705 oris %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@h
706 ori %r3, %r3, (L1CSR0_DCPE | L1CSR0_DCE)@l
709 mtspr SPR_L1CSR0, %r3
714 /* Invalidate i-cache */
715 mfspr %r3, SPR_L1CSR1
716 ori %r3, %r3, (L1CSR1_ICFI | L1CSR1_ICLFR)@l
718 mtspr SPR_L1CSR1, %r3
720 1: mfspr %r3, SPR_L1CSR1
721 andi. %r3, %r3, L1CSR1_ICFI
725 ENTRY(icache_disable)
726 /* Disable i-cache */
727 mfspr %r3, SPR_L1CSR1
732 mtspr SPR_L1CSR1, %r3
738 mfspr %r3, SPR_L1CSR1
739 oris %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@h
740 ori %r3, %r3, (L1CSR1_ICPE | L1CSR1_ICE)@l
742 mtspr SPR_L1CSR1, %r3
747 * L2 cache disable/enable/inval sequences for E500mc.
751 mfspr %r3, SPR_L2CSR0
752 oris %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@h
753 ori %r3, %r3, (L2CSR0_L2FI | L2CSR0_L2LFC)@l
755 mtspr SPR_L2CSR0, %r3
757 1: mfspr %r3, SPR_L2CSR0
758 andis. %r3, %r3, L2CSR0_L2FI@h
762 ENTRY(l2cache_enable)
763 mfspr %r3, SPR_L2CSR0
764 oris %r3, %r3, (L2CSR0_L2E | L2CSR0_L2PE)@h
766 mtspr SPR_L2CSR0, %r3
771 * Branch predictor setup.
775 ori %r3, %r3, BUCSR_BBFI
779 ori %r3, %r3, BUCSR_BPEN
785 ENTRY(dataloss_erratum_access)
786 /* Lock two cache lines into I-Cache */
788 mfspr %r11, SPR_L1CSR1
789 rlwinm %r11, %r11, 0, ~L1CSR1_ICUL
792 mtspr SPR_L1CSR1, %r11
801 mfspr %r11, SPR_L1CSR1
802 3: andi. %r11, %r11, L1CSR1_ICUL
808 mfspr %r11, SPR_L1CSR1
809 3: andi. %r11, %r11, L1CSR1_ICUL
814 /* Inside a locked cacheline, wait a while, write, then wait a while */
818 4: addis %r11, %r5, 0x100000@h /* wait around one million timebase ticks */
826 4: addis %r11, %r5, 0x100000@h /* wait around one million timebase ticks */
834 * Fill out the rest of this cache line and the next with nops,
835 * to ensure that nothing outside the locked area will be
836 * fetched due to a branch.
847 /************************************************************************/
849 /************************************************************************/
852 GLOBAL(__startkernel)
860 .space 10240 /* XXX: this really should not be necessary */
863 * Compiled KERNBASE locations
866 .set kernbase, KERNBASE
868 #include <powerpc/booke/trap_subr.S>