/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* Kernel execution entry point code.3*4* Copyright (c) 1995-1996 Gary Thomas <[email protected]>5* Initial PowerPC version.6* Copyright (c) 1996 Cort Dougan <[email protected]>7* Rewritten for PReP8* Copyright (c) 1996 Paul Mackerras <[email protected]>9* Low-level exception handers, MMU support, and rewrite.10* Copyright (c) 1997 Dan Malek <[email protected]>11* PowerPC 8xx modifications.12* Copyright (c) 1998-1999 TiVo, Inc.13* PowerPC 403GCX modifications.14* Copyright (c) 1999 Grant Erickson <[email protected]>15* PowerPC 403GCX/405GP modifications.16* Copyright 2000 MontaVista Software Inc.17* PPC405 modifications18* PowerPC 403GCX/405GP modifications.19* Author: MontaVista Software, Inc.20* [email protected] or [email protected]21* [email protected]22* Copyright 2002-2004 MontaVista Software, Inc.23* PowerPC 44x support, Matt Porter <[email protected]>24* Copyright 2004 Freescale Semiconductor, Inc25* PowerPC e500 modifications, Kumar Gala <[email protected]>26*/2728#include <linux/init.h>29#include <linux/threads.h>30#include <linux/pgtable.h>31#include <linux/linkage.h>3233#include <asm/processor.h>34#include <asm/page.h>35#include <asm/mmu.h>36#include <asm/cputable.h>37#include <asm/thread_info.h>38#include <asm/ppc_asm.h>39#include <asm/asm-offsets.h>40#include <asm/cache.h>41#include <asm/ptrace.h>42#include <asm/feature-fixups.h>43#include "head_booke.h"4445/* As with the other PowerPC ports, it is expected that when code46* execution begins here, the following registers contain valid, yet47* optional, information:48*49* r3 - Board info structure pointer (DRAM, frequency, MAC address, etc.)50* r4 - Starting address of the init RAM disk51* r5 - Ending address of the init RAM disk52* r6 - Start of kernel command line string (e.g. "mem=128")53* r7 - End of kernel command line string54*55*/56__HEAD57_GLOBAL(_stext);58_GLOBAL(_start);59/*60* Reserve a word at a fixed location to store the address61* of abatron_pteptrs62*/63nop6465/* Translate device tree address to physical, save in r30/r31 */66bl get_phys_addr67mr r30,r368mr r31,r46970li r25,0 /* phys kernel start (low) */71li r24,0 /* CPU number */72li r23,0 /* phys kernel start (high) */7374#ifdef CONFIG_RELOCATABLE75LOAD_REG_ADDR_PIC(r3, _stext) /* Get our current runtime base */7677/* Translate _stext address to physical, save in r23/r25 */78bl get_phys_addr79mr r23,r380mr r25,r48182bcl 20,31,$+4830: mflr r884addis r3,r8,(is_second_reloc - 0b)@ha85lwz r19,(is_second_reloc - 0b)@l(r3)8687/* Check if this is the second relocation. */88cmpwi r19,189bne 1f9091/*92* For the second relocation, we already get the real memstart_addr93* from device tree. So we will map PAGE_OFFSET to memstart_addr,94* then the virtual address of start kernel should be:95* PAGE_OFFSET + (kernstart_addr - memstart_addr)96* Since the offset between kernstart_addr and memstart_addr should97* never be beyond 1G, so we can just use the lower 32bit of them98* for the calculation.99*/100lis r3,PAGE_OFFSET@h101102addis r4,r8,(kernstart_addr - 0b)@ha103addi r4,r4,(kernstart_addr - 0b)@l104lwz r5,4(r4)105106addis r6,r8,(memstart_addr - 0b)@ha107addi r6,r6,(memstart_addr - 0b)@l108lwz r7,4(r6)109110subf r5,r7,r5111add r3,r3,r5112b 2f1131141:115/*116* We have the runtime (virtual) address of our base.117* We calculate our shift of offset from a 64M page.118* We could map the 64M page we belong to at PAGE_OFFSET and119* get going from there.120*/121lis r4,KERNELBASE@h122ori r4,r4,KERNELBASE@l123rlwinm r6,r25,0,0x3ffffff /* r6 = PHYS_START % 64M */124rlwinm r5,r4,0,0x3ffffff /* r5 = KERNELBASE % 64M */125subf r3,r5,r6 /* r3 = r6 - r5 */126add r3,r4,r3 /* Required Virtual Address */1271282: bl relocate129130/*131* For the second relocation, we already set the right tlb entries132* for the kernel space, so skip the code in 85xx_entry_mapping.S133*/134cmpwi r19,1135beq set_ivor136#endif137138/* We try to not make any assumptions about how the boot loader139* setup or used the TLBs. We invalidate all mappings from the140* boot loader and load a single entry in TLB1[0] to map the141* first 64M of kernel memory. Any boot info passed from the142* bootloader needs to live in this first 64M.143*144* Requirement on bootloader:145* - The page we're executing in needs to reside in TLB1 and146* have IPROT=1. If not an invalidate broadcast could147* evict the entry we're currently executing in.148*149* r3 = Index of TLB1 were executing in150* r4 = Current MSR[IS]151* r5 = Index of TLB1 temp mapping152*153* Later in mapin_ram we will correctly map lowmem, and resize TLB1[0]154* if needed155*/156157_GLOBAL(__early_start)158LOAD_REG_ADDR_PIC(r20, kernstart_virt_addr)159lwz r20,0(r20)160161#define ENTRY_MAPPING_BOOT_SETUP162#include "85xx_entry_mapping.S"163#undef ENTRY_MAPPING_BOOT_SETUP164165set_ivor:166/* Establish the interrupt vector offsets */167SET_IVOR(0, CriticalInput);168SET_IVOR(1, MachineCheck);169SET_IVOR(2, DataStorage);170SET_IVOR(3, InstructionStorage);171SET_IVOR(4, ExternalInput);172SET_IVOR(5, Alignment);173SET_IVOR(6, Program);174SET_IVOR(7, FloatingPointUnavailable);175SET_IVOR(8, SystemCall);176SET_IVOR(9, AuxillaryProcessorUnavailable);177SET_IVOR(10, Decrementer);178SET_IVOR(11, FixedIntervalTimer);179SET_IVOR(12, WatchdogTimer);180SET_IVOR(13, DataTLBError);181SET_IVOR(14, InstructionTLBError);182SET_IVOR(15, DebugCrit);183184/* Establish the interrupt vector base */185lis r4,interrupt_base@h /* IVPR only uses the high 16-bits */186mtspr SPRN_IVPR,r4187188/* Setup the defaults for TLB entries */189li r2,(MAS4_TSIZED(BOOK3E_PAGESZ_4K))@l190mtspr SPRN_MAS4, r2191192#if !defined(CONFIG_BDI_SWITCH)193/*194* The Abatron BDI JTAG debugger does not tolerate others195* mucking with the debug registers.196*/197lis r2,DBCR0_IDM@h198mtspr SPRN_DBCR0,r2199isync200/* clear any residual debug events */201li r2,-1202mtspr SPRN_DBSR,r2203#endif204205#ifdef CONFIG_SMP206/* Check to see if we're the second processor, and jump207* to the secondary_start code if so208*/209LOAD_REG_ADDR_PIC(r24, boot_cpuid)210lwz r24, 0(r24)211cmpwi r24, -1212mfspr r24,SPRN_PIR213bne __secondary_start214#endif215216/*217* This is where the main kernel code starts.218*/219220/* ptr to current */221lis r2,init_task@h222ori r2,r2,init_task@l223224/* ptr to current thread */225addi r4,r2,THREAD /* init task's THREAD */226mtspr SPRN_SPRG_THREAD,r4227228/* stack */229lis r1,init_thread_union@h230ori r1,r1,init_thread_union@l231li r0,0232stwu r0,THREAD_SIZE-STACK_FRAME_MIN_SIZE(r1)233234#ifdef CONFIG_SMP235stw r24, TASK_CPU(r2)236#endif237238bl early_init239240#ifdef CONFIG_KASAN241bl kasan_early_init242#endif243#ifdef CONFIG_RELOCATABLE244mr r3,r30245mr r4,r31246#ifdef CONFIG_PHYS_64BIT247mr r5,r23248mr r6,r25249#else250mr r5,r25251#endif252bl relocate_init253#endif254255#ifdef CONFIG_DYNAMIC_MEMSTART256lis r3,kernstart_addr@ha257la r3,kernstart_addr@l(r3)258#ifdef CONFIG_PHYS_64BIT259stw r23,0(r3)260stw r25,4(r3)261#else262stw r25,0(r3)263#endif264#endif265266/*267* Decide what sort of machine this is and initialize the MMU.268*/269mr r3,r30270mr r4,r31271bl machine_init272bl MMU_init273274/* Setup PTE pointers for the Abatron bdiGDB */275lis r6, swapper_pg_dir@h276ori r6, r6, swapper_pg_dir@l277lis r5, abatron_pteptrs@h278ori r5, r5, abatron_pteptrs@l279lis r3, kernstart_virt_addr@ha280lwz r4, kernstart_virt_addr@l(r3)281stw r5, 0(r4) /* Save abatron_pteptrs at a fixed location */282stw r6, 0(r5)283284/* Let's move on */285lis r4,start_kernel@h286ori r4,r4,start_kernel@l287lis r3,MSR_KERNEL@h288ori r3,r3,MSR_KERNEL@l289mtspr SPRN_SRR0,r4290mtspr SPRN_SRR1,r3291rfi /* change context and jump to start_kernel */292293/* Macros to hide the PTE size differences294*295* FIND_PTE -- walks the page tables given EA & pgdir pointer296* r10 -- free297* r11 -- PGDIR pointer298* r12 -- free299* r13 -- EA of fault300* label 2: is the bailout case301*302* if we find the pte (fall through):303* r11 is low pte word304* r12 is pointer to the pte305* r10 is the pshift from the PGD, if we're a hugepage306*/307#ifdef CONFIG_HUGETLB_PAGE308#define FIND_PTE \309rlwinm r12, r13, 14, 18, 28; /* Compute pgdir/pmd offset */ \310add r12, r11, r12; \311lwz r11, 4(r12); /* Get pgd/pmd entry */ \312rlwinm. r10, r11, 32 - _PAGE_PSIZE_SHIFT, 0x1e; /* get tsize*/ \313bne 1000f; /* Huge page (leaf entry) */ \314rlwinm. r12, r11, 0, 0, 20; /* Extract pt base address */ \315beq 2f; /* Bail if no table */ \316rlwimi r12, r13, 23, 20, 28; /* Compute pte address */ \317li r10, 0; /* clear r10 */ \318lwz r11, 4(r12); /* Get pte entry */ \3191000:320#else321#define FIND_PTE \322rlwinm r12, r13, 14, 18, 28; /* Compute pgdir/pmd offset */ \323add r12, r11, r12; \324lwz r11, 4(r12); /* Get pgd/pmd entry */ \325rlwinm. r12, r11, 0, 0, 20; /* Extract pt base address */ \326beq 2f; /* Bail if no table */ \327rlwimi r12, r13, 23, 20, 28; /* Compute pte address */ \328lwz r11, 4(r12); /* Get pte entry */329#endif /* HUGEPAGE */330331/*332* Interrupt vector entry code333*334* The Book E MMUs are always on so we don't need to handle335* interrupts in real mode as with previous PPC processors. In336* this case we handle interrupts in the kernel virtual address337* space.338*339* Interrupt vectors are dynamically placed relative to the340* interrupt prefix as determined by the address of interrupt_base.341* The interrupt vectors offsets are programmed using the labels342* for each interrupt vector entry.343*344* Interrupt vectors must be aligned on a 16 byte boundary.345* We align on a 32 byte cache line boundary for good measure.346*/347348interrupt_base:349/* Critical Input Interrupt */350CRITICAL_EXCEPTION(0x0100, CRITICAL, CriticalInput, unknown_exception)351352/* Machine Check Interrupt */353MCHECK_EXCEPTION(0x0200, MachineCheck, machine_check_exception)354355/* Data Storage Interrupt */356START_EXCEPTION(DataStorage)357NORMAL_EXCEPTION_PROLOG(0x300, DATA_STORAGE)358mfspr r5,SPRN_ESR /* Grab the ESR, save it */359stw r5,_ESR(r11)360mfspr r4,SPRN_DEAR /* Grab the DEAR, save it */361stw r4, _DEAR(r11)362andis. r10,r5,(ESR_ILK|ESR_DLK)@h363bne 1f364prepare_transfer_to_handler365bl do_page_fault366b interrupt_return3671:368prepare_transfer_to_handler369bl CacheLockingException370b interrupt_return371372/* Instruction Storage Interrupt */373INSTRUCTION_STORAGE_EXCEPTION374375/* External Input Interrupt */376EXCEPTION(0x0500, EXTERNAL, ExternalInput, do_IRQ)377378/* Alignment Interrupt */379ALIGNMENT_EXCEPTION380381/* Program Interrupt */382PROGRAM_EXCEPTION383384/* Floating Point Unavailable Interrupt */385#ifdef CONFIG_PPC_FPU386FP_UNAVAILABLE_EXCEPTION387#else388EXCEPTION(0x0800, FP_UNAVAIL, FloatingPointUnavailable, emulation_assist_interrupt)389#endif390391/* System Call Interrupt */392START_EXCEPTION(SystemCall)393SYSCALL_ENTRY 0xc00 BOOKE_INTERRUPT_SYSCALL SPRN_SRR1394395/* Auxiliary Processor Unavailable Interrupt */396EXCEPTION(0x2900, AP_UNAVAIL, AuxillaryProcessorUnavailable, unknown_exception)397398/* Decrementer Interrupt */399DECREMENTER_EXCEPTION400401/* Fixed Internal Timer Interrupt */402/* TODO: Add FIT support */403EXCEPTION(0x3100, FIT, FixedIntervalTimer, unknown_exception)404405/* Watchdog Timer Interrupt */406#ifdef CONFIG_BOOKE_WDT407CRITICAL_EXCEPTION(0x3200, WATCHDOG, WatchdogTimer, WatchdogException)408#else409CRITICAL_EXCEPTION(0x3200, WATCHDOG, WatchdogTimer, unknown_exception)410#endif411412/* Data TLB Error Interrupt */413START_EXCEPTION(DataTLBError)414mtspr SPRN_SPRG_WSCRATCH0, r10 /* Save some working registers */415mfspr r10, SPRN_SPRG_THREAD416stw r11, THREAD_NORMSAVE(0)(r10)417#ifdef CONFIG_KVM_BOOKE_HV418BEGIN_FTR_SECTION419mfspr r11, SPRN_SRR1420END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)421#endif422stw r12, THREAD_NORMSAVE(1)(r10)423stw r13, THREAD_NORMSAVE(2)(r10)424mfcr r13425stw r13, THREAD_NORMSAVE(3)(r10)426DO_KVM BOOKE_INTERRUPT_DTLB_MISS SPRN_SRR1427START_BTB_FLUSH_SECTION428mfspr r11, SPRN_SRR1429andi. r10,r11,MSR_PR430beq 1f431BTB_FLUSH(r10)4321:433END_BTB_FLUSH_SECTION434mfspr r13, SPRN_DEAR /* Get faulting address */435436/* If we are faulting a kernel address, we have to use the437* kernel page tables.438*/439lis r11, PAGE_OFFSET@h440cmplw 5, r13, r11441blt 5, 3f442lis r11, swapper_pg_dir@h443ori r11, r11, swapper_pg_dir@l444445mfspr r12,SPRN_MAS1 /* Set TID to 0 */446rlwinm r12,r12,0,16,1447mtspr SPRN_MAS1,r12448449b 4f450451/* Get the PGD for the current thread */4523:453mfspr r11,SPRN_SPRG_THREAD454lwz r11,PGDIR(r11)455456#ifdef CONFIG_PPC_KUAP457mfspr r12, SPRN_MAS1458rlwinm. r12,r12,0,0x3fff0000459beq 2f /* KUAP fault */460#endif4614624:463FIND_PTE464465li r13,_PAGE_PRESENT|_PAGE_BAP_SR466oris r13,r13,_PAGE_ACCESSED@h467andc. r13,r13,r11 /* Check permission */468469#ifdef CONFIG_SMP470subf r13,r11,r12 /* create false data dep */471lwzx r13,r11,r13 /* Get upper pte bits */472#else473lwz r13,0(r12) /* Get upper pte bits */474#endif475476bne 2f /* Bail if permission/valid mismatch */477478/* Jump to common tlb load */479b finish_tlb_load4802:481/* The bailout. Restore registers to pre-exception conditions482* and call the heavyweights to help us out.483*/484mfspr r10, SPRN_SPRG_THREAD485lwz r11, THREAD_NORMSAVE(3)(r10)486mtcr r11487lwz r13, THREAD_NORMSAVE(2)(r10)488lwz r12, THREAD_NORMSAVE(1)(r10)489lwz r11, THREAD_NORMSAVE(0)(r10)490mfspr r10, SPRN_SPRG_RSCRATCH0491b DataStorage492493/* Instruction TLB Error Interrupt */494/*495* Nearly the same as above, except we get our496* information from different registers and bailout497* to a different point.498*/499START_EXCEPTION(InstructionTLBError)500mtspr SPRN_SPRG_WSCRATCH0, r10 /* Save some working registers */501mfspr r10, SPRN_SPRG_THREAD502stw r11, THREAD_NORMSAVE(0)(r10)503#ifdef CONFIG_KVM_BOOKE_HV504BEGIN_FTR_SECTION505mfspr r11, SPRN_SRR1506END_FTR_SECTION_IFSET(CPU_FTR_EMB_HV)507#endif508stw r12, THREAD_NORMSAVE(1)(r10)509stw r13, THREAD_NORMSAVE(2)(r10)510mfcr r13511stw r13, THREAD_NORMSAVE(3)(r10)512DO_KVM BOOKE_INTERRUPT_ITLB_MISS SPRN_SRR1513START_BTB_FLUSH_SECTION514mfspr r11, SPRN_SRR1515andi. r10,r11,MSR_PR516beq 1f517BTB_FLUSH(r10)5181:519END_BTB_FLUSH_SECTION520521mfspr r13, SPRN_SRR0 /* Get faulting address */522523/* If we are faulting a kernel address, we have to use the524* kernel page tables.525*/526lis r11, PAGE_OFFSET@h527cmplw 5, r13, r11528blt 5, 3f529lis r11, swapper_pg_dir@h530ori r11, r11, swapper_pg_dir@l531532mfspr r12,SPRN_MAS1 /* Set TID to 0 */533rlwinm r12,r12,0,16,1534mtspr SPRN_MAS1,r12535536FIND_PTE537/* Make up the required permissions for kernel code */538li r13,_PAGE_PRESENT | _PAGE_BAP_SX539oris r13,r13,_PAGE_ACCESSED@h540b 4f541542/* Get the PGD for the current thread */5433:544mfspr r11,SPRN_SPRG_THREAD545lwz r11,PGDIR(r11)546547#ifdef CONFIG_PPC_KUAP548mfspr r12, SPRN_MAS1549rlwinm. r12,r12,0,0x3fff0000550beq 2f /* KUAP fault */551#endif552553FIND_PTE554/* Make up the required permissions for user code */555li r13,_PAGE_PRESENT | _PAGE_BAP_UX556oris r13,r13,_PAGE_ACCESSED@h5575584:559andc. r13,r13,r11 /* Check permission */560561#ifdef CONFIG_SMP562subf r13,r11,r12 /* create false data dep */563lwzx r13,r11,r13 /* Get upper pte bits */564#else565lwz r13,0(r12) /* Get upper pte bits */566#endif567568bne 2f /* Bail if permission mismatch */569570/* Jump to common TLB load point */571b finish_tlb_load5725732:574/* The bailout. Restore registers to pre-exception conditions575* and call the heavyweights to help us out.576*/577mfspr r10, SPRN_SPRG_THREAD578lwz r11, THREAD_NORMSAVE(3)(r10)579mtcr r11580lwz r13, THREAD_NORMSAVE(2)(r10)581lwz r12, THREAD_NORMSAVE(1)(r10)582lwz r11, THREAD_NORMSAVE(0)(r10)583mfspr r10, SPRN_SPRG_RSCRATCH0584b InstructionStorage585586/* Define SPE handlers for e500v2 */587#ifdef CONFIG_SPE588/* SPE Unavailable */589START_EXCEPTION(SPEUnavailable)590NORMAL_EXCEPTION_PROLOG(0x2010, SPE_UNAVAIL)591beq 1f592bl load_up_spe593b fast_exception_return5941: prepare_transfer_to_handler595bl KernelSPE596b interrupt_return597#elif defined(CONFIG_SPE_POSSIBLE)598EXCEPTION(0x2020, SPE_UNAVAIL, SPEUnavailable, unknown_exception)599#endif /* CONFIG_SPE_POSSIBLE */600601/* SPE Floating Point Data */602#ifdef CONFIG_SPE603START_EXCEPTION(SPEFloatingPointData)604NORMAL_EXCEPTION_PROLOG(0x2030, SPE_FP_DATA)605prepare_transfer_to_handler606bl SPEFloatingPointException607REST_NVGPRS(r1)608b interrupt_return609610/* SPE Floating Point Round */611START_EXCEPTION(SPEFloatingPointRound)612NORMAL_EXCEPTION_PROLOG(0x2050, SPE_FP_ROUND)613prepare_transfer_to_handler614bl SPEFloatingPointRoundException615REST_NVGPRS(r1)616b interrupt_return617#elif defined(CONFIG_SPE_POSSIBLE)618EXCEPTION(0x2040, SPE_FP_DATA, SPEFloatingPointData, unknown_exception)619EXCEPTION(0x2050, SPE_FP_ROUND, SPEFloatingPointRound, unknown_exception)620#endif /* CONFIG_SPE_POSSIBLE */621622623/* Performance Monitor */624EXCEPTION(0x2060, PERFORMANCE_MONITOR, PerformanceMonitor, \625performance_monitor_exception)626627EXCEPTION(0x2070, DOORBELL, Doorbell, doorbell_exception)628629CRITICAL_EXCEPTION(0x2080, DOORBELL_CRITICAL, \630CriticalDoorbell, unknown_exception)631632/* Debug Interrupt */633DEBUG_DEBUG_EXCEPTION634DEBUG_CRIT_EXCEPTION635636GUEST_DOORBELL_EXCEPTION637638CRITICAL_EXCEPTION(0, GUEST_DBELL_CRIT, CriticalGuestDoorbell, \639unknown_exception)640641/* Hypercall */642EXCEPTION(0, HV_SYSCALL, Hypercall, unknown_exception)643644/* Embedded Hypervisor Privilege */645EXCEPTION(0, HV_PRIV, Ehvpriv, unknown_exception)646647interrupt_end:648649/*650* Local functions651*/652653/*654* Both the instruction and data TLB miss get to this655* point to load the TLB.656* r10 - tsize encoding (if HUGETLB_PAGE) or available to use657* r11 - TLB (info from Linux PTE)658* r12 - available to use659* r13 - upper bits of PTE660* CR5 - results of addr >= PAGE_OFFSET661* MAS0, MAS1 - loaded with proper value when we get here662* MAS2, MAS3 - will need additional info from Linux PTE663* Upon exit, we reload everything and RFI.664*/665finish_tlb_load:666#ifdef CONFIG_HUGETLB_PAGE667cmpwi 6, r10, 0 /* check for huge page */668beq 6, finish_tlb_load_cont /* !huge */669670/* Alas, we need more scratch registers for hugepages */671mfspr r12, SPRN_SPRG_THREAD672stw r14, THREAD_NORMSAVE(4)(r12)673stw r15, THREAD_NORMSAVE(5)(r12)674stw r16, THREAD_NORMSAVE(6)(r12)675stw r17, THREAD_NORMSAVE(7)(r12)676677/* Get the next_tlbcam_idx percpu var */678#ifdef CONFIG_SMP679lwz r15, TASK_CPU-THREAD(r12)680lis r14, __per_cpu_offset@h681ori r14, r14, __per_cpu_offset@l682rlwinm r15, r15, 2, 0, 29683lwzx r16, r14, r15684#else685li r16, 0686#endif687lis r17, next_tlbcam_idx@h688ori r17, r17, next_tlbcam_idx@l689add r17, r17, r16 /* r17 = *next_tlbcam_idx */690lwz r15, 0(r17) /* r15 = next_tlbcam_idx */691692lis r14, MAS0_TLBSEL(1)@h /* select TLB1 (TLBCAM) */693rlwimi r14, r15, 16, 4, 15 /* next_tlbcam_idx entry */694mtspr SPRN_MAS0, r14695696/* Extract TLB1CFG(NENTRY) */697mfspr r16, SPRN_TLB1CFG698andi. r16, r16, 0xfff699700/* Update next_tlbcam_idx, wrapping when necessary */701addi r15, r15, 1702cmpw r15, r16703blt 100f704lis r14, tlbcam_index@h705ori r14, r14, tlbcam_index@l706lwz r15, 0(r14)707100: stw r15, 0(r17)708709mfspr r16, SPRN_MAS1710rlwimi r16, r10, MAS1_TSIZE_SHIFT, MAS1_TSIZE_MASK711mtspr SPRN_MAS1, r16712713/* copy the pshift for use later */714addi r14, r10, _PAGE_PSIZE_SHIFT_OFFSET715716/* fall through */717718#endif /* CONFIG_HUGETLB_PAGE */719720/*721* We set execute, because we don't have the granularity to722* properly set this at the page level (Linux problem).723* Many of these bits are software only. Bits we don't set724* here we (properly should) assume have the appropriate value.725*/726finish_tlb_load_cont:727rlwinm r12, r11, 32-2, 26, 31 /* Move in perm bits */728andi. r10, r11, _PAGE_DIRTY729bne 1f730li r10, MAS3_SW | MAS3_UW731andc r12, r12, r107321: rlwimi r12, r13, 20, 0, 11 /* grab RPN[32:43] */733rlwimi r12, r11, 20, 12, 19 /* grab RPN[44:51] */7342: mtspr SPRN_MAS3, r12735BEGIN_MMU_FTR_SECTION736srwi r10, r13, 12 /* grab RPN[12:31] */737mtspr SPRN_MAS7, r10738END_MMU_FTR_SECTION_IFSET(MMU_FTR_BIG_PHYS)739740mfspr r12, SPRN_MAS2741rlwimi r12, r11, 32-19, 27, 31 /* extract WIMGE from pte */742#ifdef CONFIG_HUGETLB_PAGE743beq 6, 3f /* don't mask if page isn't huge */744li r13, 1745slw r13, r13, r14746subi r13, r13, 1747rlwinm r13, r13, 0, 0, 19 /* bottom bits used for WIMGE/etc */748andc r12, r12, r13 /* mask off ea bits within the page */749#endif7503: mtspr SPRN_MAS2, r12751752tlb_write_entry:753tlbwe754755/* Done...restore registers and get out of here. */756mfspr r10, SPRN_SPRG_THREAD757#ifdef CONFIG_HUGETLB_PAGE758beq 6, 8f /* skip restore for 4k page faults */759lwz r14, THREAD_NORMSAVE(4)(r10)760lwz r15, THREAD_NORMSAVE(5)(r10)761lwz r16, THREAD_NORMSAVE(6)(r10)762lwz r17, THREAD_NORMSAVE(7)(r10)763#endif7648: lwz r11, THREAD_NORMSAVE(3)(r10)765mtcr r11766lwz r13, THREAD_NORMSAVE(2)(r10)767lwz r12, THREAD_NORMSAVE(1)(r10)768lwz r11, THREAD_NORMSAVE(0)(r10)769mfspr r10, SPRN_SPRG_RSCRATCH0770rfi /* Force context change */771772#ifdef CONFIG_SPE773/* Note that the SPE support is closely modeled after the AltiVec774* support. Changes to one are likely to be applicable to the775* other! */776_GLOBAL(load_up_spe)777/*778* Disable SPE for the task which had SPE previously,779* and save its SPE registers in its thread_struct.780* Enables SPE for use in the kernel on return.781* On SMP we know the SPE units are free, since we give it up every782* switch. -- Kumar783*/784mfmsr r5785oris r5,r5,MSR_SPE@h786mtmsr r5 /* enable use of SPE now */787isync788/* enable use of SPE after return */789oris r9,r9,MSR_SPE@h790mfspr r5,SPRN_SPRG_THREAD /* current task's THREAD (phys) */791li r4,1792li r10,THREAD_ACC793stw r4,THREAD_USED_SPE(r5)794evlddx evr4,r10,r5795evmra evr4,evr4796REST_32EVRS(0,r10,r5,THREAD_EVR0)797blr798799/*800* SPE unavailable trap from kernel - print a message, but let801* the task use SPE in the kernel until it returns to user mode.802*/803SYM_FUNC_START_LOCAL(KernelSPE)804lwz r3,_MSR(r1)805oris r3,r3,MSR_SPE@h806stw r3,_MSR(r1) /* enable use of SPE after return */807#ifdef CONFIG_PRINTK808lis r3,87f@h809ori r3,r3,87f@l810mr r4,r2 /* current */811lwz r5,_NIP(r1)812bl _printk813#endif814b interrupt_return815#ifdef CONFIG_PRINTK81687: .string "SPE used in kernel (task=%p, pc=%x) \n"817#endif818.align 4,0819820SYM_FUNC_END(KernelSPE)821#endif /* CONFIG_SPE */822823/*824* Translate the effec addr in r3 to phys addr. The phys addr will be put825* into r3(higher 32bit) and r4(lower 32bit)826*/827SYM_FUNC_START_LOCAL(get_phys_addr)828mfmsr r8829mfspr r9,SPRN_PID830rlwinm r9,r9,16,0x3fff0000 /* turn PID into MAS6[SPID] */831rlwimi r9,r8,28,0x00000001 /* turn MSR[DS] into MAS6[SAS] */832mtspr SPRN_MAS6,r9833834tlbsx 0,r3 /* must succeed */835836mfspr r8,SPRN_MAS1837mfspr r12,SPRN_MAS3838rlwinm r9,r8,25,0x1f /* r9 = log2(page size) */839li r10,1024840slw r10,r10,r9 /* r10 = page size */841addi r10,r10,-1842and r11,r3,r10 /* r11 = page offset */843andc r4,r12,r10 /* r4 = page base */844or r4,r4,r11 /* r4 = devtree phys addr */845#ifdef CONFIG_PHYS_64BIT846mfspr r3,SPRN_MAS7847#endif848blr849SYM_FUNC_END(get_phys_addr)850851/*852* Global functions853*/854855#ifdef CONFIG_PPC_E500856#ifndef CONFIG_PPC_E500MC857/* Adjust or setup IVORs for e500v1/v2 */858_GLOBAL(__setup_e500_ivors)859li r3,DebugCrit@l860mtspr SPRN_IVOR15,r3861li r3,SPEUnavailable@l862mtspr SPRN_IVOR32,r3863li r3,SPEFloatingPointData@l864mtspr SPRN_IVOR33,r3865li r3,SPEFloatingPointRound@l866mtspr SPRN_IVOR34,r3867li r3,PerformanceMonitor@l868mtspr SPRN_IVOR35,r3869sync870blr871#else872/* Adjust or setup IVORs for e500mc */873_GLOBAL(__setup_e500mc_ivors)874li r3,DebugDebug@l875mtspr SPRN_IVOR15,r3876li r3,PerformanceMonitor@l877mtspr SPRN_IVOR35,r3878li r3,Doorbell@l879mtspr SPRN_IVOR36,r3880li r3,CriticalDoorbell@l881mtspr SPRN_IVOR37,r3882sync883blr884885/* setup ehv ivors for */886_GLOBAL(__setup_ehv_ivors)887li r3,GuestDoorbell@l888mtspr SPRN_IVOR38,r3889li r3,CriticalGuestDoorbell@l890mtspr SPRN_IVOR39,r3891li r3,Hypercall@l892mtspr SPRN_IVOR40,r3893li r3,Ehvpriv@l894mtspr SPRN_IVOR41,r3895sync896blr897#endif /* CONFIG_PPC_E500MC */898#endif /* CONFIG_PPC_E500 */899900#ifdef CONFIG_SPE901/*902* extern void __giveup_spe(struct task_struct *prev)903*904*/905_GLOBAL(__giveup_spe)906addi r3,r3,THREAD /* want THREAD of task */907lwz r5,PT_REGS(r3)908cmpi 0,r5,0909SAVE_32EVRS(0, r4, r3, THREAD_EVR0)910evxor evr6, evr6, evr6 /* clear out evr6 */911evmwumiaa evr6, evr6, evr6 /* evr6 <- ACC = 0 * 0 + ACC */912li r4,THREAD_ACC913evstddx evr6, r4, r3 /* save off accumulator */914beq 1f915lwz r4,_MSR-STACK_INT_FRAME_REGS(r5)916lis r3,MSR_SPE@h917andc r4,r4,r3 /* disable SPE for previous task */918stw r4,_MSR-STACK_INT_FRAME_REGS(r5)9191:920blr921#endif /* CONFIG_SPE */922923/*924* extern void abort(void)925*926* At present, this routine just applies a system reset.927*/928_GLOBAL(abort)929li r13,0930mtspr SPRN_DBCR0,r13 /* disable all debug events */931isync932mfmsr r13933ori r13,r13,MSR_DE@l /* Enable Debug Events */934mtmsr r13935isync936mfspr r13,SPRN_DBCR0937lis r13,(DBCR0_IDM|DBCR0_RST_CHIP)@h938mtspr SPRN_DBCR0,r13939isync940941#ifdef CONFIG_SMP942/* When we get here, r24 needs to hold the CPU # */943.globl __secondary_start944__secondary_start:945LOAD_REG_ADDR_PIC(r3, tlbcam_index)946lwz r3,0(r3)947mtctr r3948li r26,0 /* r26 safe? */949950bl switch_to_as1951mr r27,r3 /* tlb entry */952/* Load each CAM entry */9531: mr r3,r26954bl loadcam_entry955addi r26,r26,1956bdnz 1b957mr r3,r27 /* tlb entry */958LOAD_REG_ADDR_PIC(r4, memstart_addr)959lwz r4,0(r4)960mr r5,r25 /* phys kernel start */961rlwinm r5,r5,0,~0x3ffffff /* aligned 64M */962subf r4,r5,r4 /* memstart_addr - phys kernel start */963lis r7,KERNELBASE@h964ori r7,r7,KERNELBASE@l965cmpw r20,r7 /* if kernstart_virt_addr != KERNELBASE, randomized */966beq 2f967li r4,09682: li r5,0 /* no device tree */969li r6,0 /* not boot cpu */970bl restore_to_as0971972973lis r3,__secondary_hold_acknowledge@h974ori r3,r3,__secondary_hold_acknowledge@l975stw r24,0(r3)976977li r3,0978mr r4,r24 /* Why? */979bl call_setup_cpu980981/* get current's stack and current */982lis r2,secondary_current@ha983lwz r2,secondary_current@l(r2)984lwz r1,TASK_STACK(r2)985986/* stack */987addi r1,r1,THREAD_SIZE-STACK_FRAME_MIN_SIZE988li r0,0989stw r0,0(r1)990991/* ptr to current thread */992addi r4,r2,THREAD /* address of our thread_struct */993mtspr SPRN_SPRG_THREAD,r4994995/* Setup the defaults for TLB entries */996li r4,(MAS4_TSIZED(BOOK3E_PAGESZ_4K))@l997mtspr SPRN_MAS4,r4998999/* Jump to start_secondary */1000lis r4,MSR_KERNEL@h1001ori r4,r4,MSR_KERNEL@l1002lis r3,start_secondary@h1003ori r3,r3,start_secondary@l1004mtspr SPRN_SRR0,r31005mtspr SPRN_SRR1,r41006sync1007rfi1008sync10091010.globl __secondary_hold_acknowledge1011__secondary_hold_acknowledge:1012.long -11013#endif10141015/*1016* Create a 64M tlb by address and entry1017* r3 - entry1018* r4 - virtual address1019* r5/r6 - physical address1020*/1021_GLOBAL(create_kaslr_tlb_entry)1022lis r7,0x1000 /* Set MAS0(TLBSEL) = 1 */1023rlwimi r7,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r6) */1024mtspr SPRN_MAS0,r7 /* Write MAS0 */10251026lis r3,(MAS1_VALID|MAS1_IPROT)@h1027ori r3,r3,(MAS1_TSIZE(BOOK3E_PAGESZ_64M))@l1028mtspr SPRN_MAS1,r3 /* Write MAS1 */10291030lis r3,MAS2_EPN_MASK(BOOK3E_PAGESZ_64M)@h1031ori r3,r3,MAS2_EPN_MASK(BOOK3E_PAGESZ_64M)@l1032and r3,r3,r41033ori r3,r3,MAS2_M_IF_NEEDED@l1034mtspr SPRN_MAS2,r3 /* Write MAS2(EPN) */10351036#ifdef CONFIG_PHYS_64BIT1037ori r8,r6,(MAS3_SW|MAS3_SR|MAS3_SX)1038mtspr SPRN_MAS3,r8 /* Write MAS3(RPN) */1039mtspr SPRN_MAS7,r51040#else1041ori r8,r5,(MAS3_SW|MAS3_SR|MAS3_SX)1042mtspr SPRN_MAS3,r8 /* Write MAS3(RPN) */1043#endif10441045tlbwe /* Write TLB */1046isync1047sync1048blr10491050/*1051* Return to the start of the relocated kernel and run again1052* r3 - virtual address of fdt1053* r4 - entry of the kernel1054*/1055_GLOBAL(reloc_kernel_entry)1056mfmsr r71057rlwinm r7, r7, 0, ~(MSR_IS | MSR_DS)10581059mtspr SPRN_SRR0,r41060mtspr SPRN_SRR1,r71061rfi10621063/*1064* Create a tlb entry with the same effective and physical address as1065* the tlb entry used by the current running code. But set the TS to 1.1066* Then switch to the address space 1. It will return with the r3 set to1067* the ESEL of the new created tlb.1068*/1069_GLOBAL(switch_to_as1)1070mflr r510711072/* Find a entry not used */1073mfspr r3,SPRN_TLB1CFG1074andi. r3,r3,0xfff1075mfspr r4,SPRN_PID1076rlwinm r4,r4,16,0x3fff0000 /* turn PID into MAS6[SPID] */1077mtspr SPRN_MAS6,r410781: lis r4,0x1000 /* Set MAS0(TLBSEL) = 1 */1079addi r3,r3,-11080rlwimi r4,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r3) */1081mtspr SPRN_MAS0,r41082tlbre1083mfspr r4,SPRN_MAS11084andis. r4,r4,MAS1_VALID@h1085bne 1b10861087/* Get the tlb entry used by the current running code */1088bcl 20,31,$+410890: mflr r41090tlbsx 0,r410911092mfspr r4,SPRN_MAS11093ori r4,r4,MAS1_TS /* Set the TS = 1 */1094mtspr SPRN_MAS1,r410951096mfspr r4,SPRN_MAS01097rlwinm r4,r4,0,~MAS0_ESEL_MASK1098rlwimi r4,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r3) */1099mtspr SPRN_MAS0,r41100tlbwe1101isync1102sync11031104mfmsr r41105ori r4,r4,MSR_IS | MSR_DS1106mtspr SPRN_SRR0,r51107mtspr SPRN_SRR1,r41108sync1109rfi11101111/*1112* Restore to the address space 0 and also invalidate the tlb entry created1113* by switch_to_as1.1114* r3 - the tlb entry which should be invalidated1115* r4 - __pa(PAGE_OFFSET in AS1) - __pa(PAGE_OFFSET in AS0)1116* r5 - device tree virtual address. If r4 is 0, r5 is ignored.1117* r6 - boot cpu1118*/1119_GLOBAL(restore_to_as0)1120mflr r011211122bcl 20,31,$+411230: mflr r91124addi r9,r9,1f - 0b11251126/*1127* We may map the PAGE_OFFSET in AS0 to a different physical address,1128* so we need calculate the right jump and device tree address based1129* on the offset passed by r4.1130*/1131add r9,r9,r41132add r5,r5,r41133add r0,r0,r4113411352: mfmsr r71136li r8,(MSR_IS | MSR_DS)1137andc r7,r7,r811381139mtspr SPRN_SRR0,r91140mtspr SPRN_SRR1,r71141sync1142rfi11431144/* Invalidate the temporary tlb entry for AS1 */11451: lis r9,0x1000 /* Set MAS0(TLBSEL) = 1 */1146rlwimi r9,r3,16,4,15 /* Setup MAS0 = TLBSEL | ESEL(r3) */1147mtspr SPRN_MAS0,r91148tlbre1149mfspr r9,SPRN_MAS11150rlwinm r9,r9,0,2,31 /* Clear MAS1 Valid and IPPROT */1151mtspr SPRN_MAS1,r91152tlbwe1153isync11541155cmpwi r4,01156cmpwi cr1,r6,01157cror eq,4*cr1+eq,eq1158bne 3f /* offset != 0 && is_boot_cpu */1159mtlr r01160blr11611162/*1163* The PAGE_OFFSET will map to a different physical address,1164* jump to _start to do another relocation again.1165*/11663: mr r3,r51167bl _start116811691170