/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* This file contains kexec low-level functions.3*4* Copyright (C) 2002-2003 Eric Biederman <[email protected]>5* GameCube/ppc32 port Copyright (C) 2004 Albert Herranz6* PPC44x port. Copyright (C) 2011, IBM Corporation7* Author: Suzuki Poulose <[email protected]>8*/910#include <linux/objtool.h>11#include <asm/reg.h>12#include <asm/page.h>13#include <asm/mmu.h>14#include <asm/ppc_asm.h>15#include <asm/kexec.h>1617.text1819/*20* Must be relocatable PIC code callable as a C function.21*/22.globl relocate_new_kernel23relocate_new_kernel:24/* r3 = page_list */25/* r4 = reboot_code_buffer */26/* r5 = start_address */2728#ifdef CONFIG_PPC_85xx2930mr r29, r331mr r30, r432mr r31, r53334#define ENTRY_MAPPING_KEXEC_SETUP35#include <kernel/85xx_entry_mapping.S>36#undef ENTRY_MAPPING_KEXEC_SETUP3738mr r3, r2939mr r4, r3040mr r5, r314142li r0, 043#elif defined(CONFIG_44x)4445/* Save our parameters */46mr r29, r347mr r30, r448mr r31, r54950#ifdef CONFIG_PPC_47x51/* Check for 47x cores */52mfspr r3,SPRN_PVR53srwi r3,r3,1654cmplwi cr0,r3,PVR_476FPE@h55beq setup_map_47x56cmplwi cr0,r3,PVR_476@h57beq setup_map_47x58cmplwi cr0,r3,PVR_476_ISS@h59beq setup_map_47x60#endif /* CONFIG_PPC_47x */6162/*63* Code for setting up 1:1 mapping for PPC440x for KEXEC64*65* We cannot switch off the MMU on PPC44x.66* So we:67* 1) Invalidate all the mappings except the one we are running from.68* 2) Create a tmp mapping for our code in the other address space(TS) and69* jump to it. Invalidate the entry we started in.70* 3) Create a 1:1 mapping for 0-2GiB in chunks of 256M in original TS.71* 4) Jump to the 1:1 mapping in original TS.72* 5) Invalidate the tmp mapping.73*74* - Based on the kexec support code for FSL BookE75*76*/7778/*79* Load the PID with kernel PID (0).80* Also load our MSR_IS and TID to MMUCR for TLB search.81*/82li r3, 083mtspr SPRN_PID, r384mfmsr r485andi. r4,r4,MSR_IS@l86beq wmmucr87oris r3,r3,PPC44x_MMUCR_STS@h88wmmucr:89mtspr SPRN_MMUCR,r390sync9192/*93* Invalidate all the TLB entries except the current entry94* where we are running from95*/96bcl 20,31,$+4 /* Find our address */970: mflr r5 /* Make it accessible */98tlbsx r23,0,r5 /* Find entry we are in */99li r4,0 /* Start at TLB entry 0 */100li r3,0 /* Set PAGEID inval value */1011: cmpw r23,r4 /* Is this our entry? */102beq skip /* If so, skip the inval */103tlbwe r3,r4,PPC44x_TLB_PAGEID /* If not, inval the entry */104skip:105addi r4,r4,1 /* Increment */106cmpwi r4,64 /* Are we done? */107bne 1b /* If not, repeat */108isync109110/* Create a temp mapping and jump to it */111andi. r6, r23, 1 /* Find the index to use */112addi r24, r6, 1 /* r24 will contain 1 or 2 */113114mfmsr r9 /* get the MSR */115rlwinm r5, r9, 27, 31, 31 /* Extract the MSR[IS] */116xori r7, r5, 1 /* Use the other address space */117118/* Read the current mapping entries */119tlbre r3, r23, PPC44x_TLB_PAGEID120tlbre r4, r23, PPC44x_TLB_XLAT121tlbre r5, r23, PPC44x_TLB_ATTRIB122123/* Save our current XLAT entry */124mr r25, r4125126/* Extract the TLB PageSize */127li r10, 1 /* r10 will hold PageSize */128rlwinm r11, r3, 0, 24, 27 /* bits 24-27 */129130/* XXX: As of now we use 256M, 4K pages */131cmpwi r11, PPC44x_TLB_256M132bne tlb_4k133rotlwi r10, r10, 28 /* r10 = 256M */134b write_out135tlb_4k:136cmpwi r11, PPC44x_TLB_4K137bne default138rotlwi r10, r10, 12 /* r10 = 4K */139b write_out140default:141rotlwi r10, r10, 10 /* r10 = 1K */142143write_out:144/*145* Write out the tmp 1:1 mapping for this code in other address space146* Fixup EPN = RPN , TS=other address space147*/148insrwi r3, r7, 1, 23 /* Bit 23 is TS for PAGEID field */149150/* Write out the tmp mapping entries */151tlbwe r3, r24, PPC44x_TLB_PAGEID152tlbwe r4, r24, PPC44x_TLB_XLAT153tlbwe r5, r24, PPC44x_TLB_ATTRIB154155subi r11, r10, 1 /* PageOffset Mask = PageSize - 1 */156not r10, r11 /* Mask for PageNum */157158/* Switch to other address space in MSR */159insrwi r9, r7, 1, 26 /* Set MSR[IS] = r7 */160161bcl 20,31,$+41621: mflr r8163addi r8, r8, (2f-1b) /* Find the target offset */164165/* Jump to the tmp mapping */166mtspr SPRN_SRR0, r8167mtspr SPRN_SRR1, r9168rfi1691702:171/* Invalidate the entry we were executing from */172li r3, 0173tlbwe r3, r23, PPC44x_TLB_PAGEID174175/* attribute fields. rwx for SUPERVISOR mode */176li r5, 0177ori r5, r5, (PPC44x_TLB_SW | PPC44x_TLB_SR | PPC44x_TLB_SX | PPC44x_TLB_G)178179/* Create 1:1 mapping in 256M pages */180xori r7, r7, 1 /* Revert back to Original TS */181182li r8, 0 /* PageNumber */183li r6, 3 /* TLB Index, start at 3 */184185next_tlb:186rotlwi r3, r8, 28 /* Create EPN (bits 0-3) */187mr r4, r3 /* RPN = EPN */188ori r3, r3, (PPC44x_TLB_VALID | PPC44x_TLB_256M) /* SIZE = 256M, Valid */189insrwi r3, r7, 1, 23 /* Set TS from r7 */190191tlbwe r3, r6, PPC44x_TLB_PAGEID /* PageID field : EPN, V, SIZE */192tlbwe r4, r6, PPC44x_TLB_XLAT /* Address translation : RPN */193tlbwe r5, r6, PPC44x_TLB_ATTRIB /* Attributes */194195addi r8, r8, 1 /* Increment PN */196addi r6, r6, 1 /* Increment TLB Index */197cmpwi r8, 8 /* Are we done ? */198bne next_tlb199isync200201/* Jump to the new mapping 1:1 */202li r9,0203insrwi r9, r7, 1, 26 /* Set MSR[IS] = r7 */204205bcl 20,31,$+42061: mflr r8207and r8, r8, r11 /* Get our offset within page */208addi r8, r8, (2f-1b)209210and r5, r25, r10 /* Get our target PageNum */211or r8, r8, r5 /* Target jump address */212213mtspr SPRN_SRR0, r8214mtspr SPRN_SRR1, r9215rfi2162:217/* Invalidate the tmp entry we used */218li r3, 0219tlbwe r3, r24, PPC44x_TLB_PAGEID220sync221b ppc44x_map_done222223#ifdef CONFIG_PPC_47x224225/* 1:1 mapping for 47x */226227setup_map_47x:228229/*230* Load the kernel pid (0) to PID and also to MMUCR[TID].231* Also set the MSR IS->MMUCR STS232*/233li r3, 0234mtspr SPRN_PID, r3 /* Set PID */235mfmsr r4 /* Get MSR */236andi. r4, r4, MSR_IS@l /* TS=1? */237beq 1f /* If not, leave STS=0 */238oris r3, r3, PPC47x_MMUCR_STS@h /* Set STS=1 */2391: mtspr SPRN_MMUCR, r3 /* Put MMUCR */240sync241242/* Find the entry we are running from */243bcl 20,31,$+42442: mflr r23245tlbsx r23, 0, r23246tlbre r24, r23, 0 /* TLB Word 0 */247tlbre r25, r23, 1 /* TLB Word 1 */248tlbre r26, r23, 2 /* TLB Word 2 */249250251/*252* Invalidates all the tlb entries by writing to 256 RPNs(r4)253* of 4k page size in all 4 ways (0-3 in r3).254* This would invalidate the entire UTLB including the one we are255* running from. However the shadow TLB entries would help us256* to continue the execution, until we flush them (rfi/isync).257*/258addis r3, 0, 0x8000 /* specify the way */259addi r4, 0, 0 /* TLB Word0 = (EPN=0, VALID = 0) */260addi r5, 0, 0261b clear_utlb_entry262263/* Align the loop to speed things up. from head_44x.S */264.align 6265266clear_utlb_entry:267268tlbwe r4, r3, 0269tlbwe r5, r3, 1270tlbwe r5, r3, 2271addis r3, r3, 0x2000 /* Increment the way */272cmpwi r3, 0273bne clear_utlb_entry274addis r3, 0, 0x8000275addis r4, r4, 0x100 /* Increment the EPN */276cmpwi r4, 0277bne clear_utlb_entry278279/* Create the entries in the other address space */280mfmsr r5281rlwinm r7, r5, 27, 31, 31 /* Get the TS (Bit 26) from MSR */282xori r7, r7, 1 /* r7 = !TS */283284insrwi r24, r7, 1, 21 /* Change the TS in the saved TLB word 0 */285286/*287* write out the TLB entries for the tmp mapping288* Use way '0' so that we could easily invalidate it later.289*/290lis r3, 0x8000 /* Way '0' */291292tlbwe r24, r3, 0293tlbwe r25, r3, 1294tlbwe r26, r3, 2295296/* Update the msr to the new TS */297insrwi r5, r7, 1, 26298299bcl 20,31,$+43001: mflr r6301addi r6, r6, (2f-1b)302303mtspr SPRN_SRR0, r6304mtspr SPRN_SRR1, r5305rfi306307/*308* Now we are in the tmp address space.309* Create a 1:1 mapping for 0-2GiB in the original TS.310*/3112:312li r3, 0313li r4, 0 /* TLB Word 0 */314li r5, 0 /* TLB Word 1 */315li r6, 0316ori r6, r6, PPC47x_TLB2_S_RWX /* TLB word 2 */317318li r8, 0 /* PageIndex */319320xori r7, r7, 1 /* revert back to original TS */321322write_utlb:323rotlwi r5, r8, 28 /* RPN = PageIndex * 256M */324/* ERPN = 0 as we don't use memory above 2G */325326mr r4, r5 /* EPN = RPN */327ori r4, r4, (PPC47x_TLB0_VALID | PPC47x_TLB0_256M)328insrwi r4, r7, 1, 21 /* Insert the TS to Word 0 */329330tlbwe r4, r3, 0 /* Write out the entries */331tlbwe r5, r3, 1332tlbwe r6, r3, 2333addi r8, r8, 1334cmpwi r8, 8 /* Have we completed ? */335bne write_utlb336337/* make sure we complete the TLB write up */338isync339340/*341* Prepare to jump to the 1:1 mapping.342* 1) Extract page size of the tmp mapping343* DSIZ = TLB_Word0[22:27]344* 2) Calculate the physical address of the address345* to jump to.346*/347rlwinm r10, r24, 0, 22, 27348349cmpwi r10, PPC47x_TLB0_4K350li r10, 0x1000 /* r10 = 4k */351beq 0f352353/* Defaults to 256M */354lis r10, 0x10003553560: bcl 20,31,$+43571: mflr r4358addi r4, r4, (2f-1b) /* virtual address of 2f */359360subi r11, r10, 1 /* offsetmask = Pagesize - 1 */361not r10, r11 /* Pagemask = ~(offsetmask) */362363and r5, r25, r10 /* Physical page */364and r6, r4, r11 /* offset within the current page */365366or r5, r5, r6 /* Physical address for 2f */367368/* Switch the TS in MSR to the original one */369mfmsr r8370insrwi r8, r7, 1, 26371372mtspr SPRN_SRR1, r8373mtspr SPRN_SRR0, r5374rfi3753762:377/* Invalidate the tmp mapping */378lis r3, 0x8000 /* Way '0' */379380clrrwi r24, r24, 12 /* Clear the valid bit */381tlbwe r24, r3, 0382tlbwe r25, r3, 1383tlbwe r26, r3, 2384385/* Make sure we complete the TLB write and flush the shadow TLB */386isync387388#endif389390ppc44x_map_done:391392393/* Restore the parameters */394mr r3, r29395mr r4, r30396mr r5, r31397398li r0, 0399#else400li r0, 0401402/*403* Set Machine Status Register to a known status,404* switch the MMU off and jump to 1: in a single step.405*/406407mr r8, r0408ori r8, r8, MSR_RI|MSR_ME409mtspr SPRN_SRR1, r8410addi r8, r4, 1f - relocate_new_kernel411mtspr SPRN_SRR0, r8412sync413rfi4144151:416#endif417/* from this point address translation is turned off */418/* and interrupts are disabled */419420/* set a new stack at the bottom of our page... */421/* (not really needed now) */422addi r1, r4, KEXEC_CONTROL_PAGE_SIZE - 8 /* for LR Save+Back Chain */423stw r0, 0(r1)424425/* Do the copies */426li r6, 0 /* checksum */427mr r0, r3428b 1f4294300: /* top, read another word for the indirection page */431lwzu r0, 4(r3)4324331:434/* is it a destination page? (r8) */435rlwinm. r7, r0, 0, 31, 31 /* IND_DESTINATION (1<<0) */436beq 2f437438rlwinm r8, r0, 0, 0, 19 /* clear kexec flags, page align */439b 0b4404412: /* is it an indirection page? (r3) */442rlwinm. r7, r0, 0, 30, 30 /* IND_INDIRECTION (1<<1) */443beq 2f444445rlwinm r3, r0, 0, 0, 19 /* clear kexec flags, page align */446subi r3, r3, 4447b 0b4484492: /* are we done? */450rlwinm. r7, r0, 0, 29, 29 /* IND_DONE (1<<2) */451beq 2f452b 3f4534542: /* is it a source page? (r9) */455rlwinm. r7, r0, 0, 28, 28 /* IND_SOURCE (1<<3) */456beq 0b457458rlwinm r9, r0, 0, 0, 19 /* clear kexec flags, page align */459460li r7, PAGE_SIZE / 4461mtctr r7462subi r9, r9, 4463subi r8, r8, 44649:465lwzu r0, 4(r9) /* do the copy */466xor r6, r6, r0467stwu r0, 4(r8)468dcbst 0, r8469sync470icbi 0, r8471bdnz 9b472473addi r9, r9, 4474addi r8, r8, 4475b 0b4764773:478479/* To be certain of avoiding problems with self-modifying code480* execute a serializing instruction here.481*/482isync483sync484485mfspr r3, SPRN_PIR /* current core we are running on */486mr r4, r5 /* load physical address of chunk called */487488/* jump to the entry point, usually the setup routine */489mtlr r5490blrl4914921: b 1b493494relocate_new_kernel_end:495496.globl relocate_new_kernel_size497relocate_new_kernel_size:498.long relocate_new_kernel_end - relocate_new_kernel499500501