/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* PowerPC version3* Copyright (C) 1995-1996 Gary Thomas ([email protected])4* Rewritten by Cort Dougan ([email protected]) for PReP5* Copyright (C) 1996 Cort Dougan <[email protected]>6* Adapted for Power Macintosh by Paul Mackerras.7* Low-level exception handlers and MMU support8* rewritten by Paul Mackerras.9* Copyright (C) 1996 Paul Mackerras.10*11* This file contains low-level assembler routines for managing12* the PowerPC MMU hash table. (PPC 8xx processors don't use a13* hash table, so this file is not used on them.)14*/1516#include <linux/export.h>17#include <linux/pgtable.h>18#include <linux/init.h>19#include <asm/reg.h>20#include <asm/page.h>21#include <asm/cputable.h>22#include <asm/ppc_asm.h>23#include <asm/thread_info.h>24#include <asm/asm-offsets.h>25#include <asm/feature-fixups.h>26#include <asm/code-patching-asm.h>2728#ifdef CONFIG_PTE_64BIT29#define PTE_T_SIZE 830#define PTE_FLAGS_OFFSET 4 /* offset of PTE flags, in bytes */31#else32#define PTE_T_SIZE 433#define PTE_FLAGS_OFFSET 034#endif3536/*37* Load a PTE into the hash table, if possible.38* The address is in r4, and r3 contains required access flags:39* - For ISI: _PAGE_PRESENT | _PAGE_EXEC40* - For DSI: _PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE if a write.41* r9 contains the SRR1 value, from which we use the MSR_PR bit.42* SPRG_THREAD contains the physical address of the current task's thread.43*44* Returns to the caller if the access is illegal or there is no45* mapping for the address. Otherwise it places an appropriate PTE46* in the hash table and returns from the exception.47* Uses r0, r3 - r6, r8, r10, ctr, lr.48*/49.text50_GLOBAL(hash_page)51#ifdef CONFIG_SMP52lis r8, (mmu_hash_lock - PAGE_OFFSET)@h53ori r8, r8, (mmu_hash_lock - PAGE_OFFSET)@l54lis r0,0x0fff55b 10f5611: lwz r6,0(r8)57cmpwi 0,r6,058bne 11b5910: lwarx r6,0,r860cmpwi 0,r6,061bne- 11b62stwcx. r0,0,r863bne- 10b64isync65#endif66/* Get PTE (linux-style) and check access */67lis r0, TASK_SIZE@h /* check if kernel address */68cmplw 0,r4,r069mfspr r8,SPRN_SPRG_THREAD /* current task's THREAD (phys) */70lwz r5,PGDIR(r8) /* virt page-table root */71blt+ 112f /* assume user more likely */72lis r5,swapper_pg_dir@ha /* if kernel address, use */73andi. r0,r9,MSR_PR /* Check usermode */74addi r5,r5,swapper_pg_dir@l /* kernel page table */75#ifdef CONFIG_SMP76bne- .Lhash_page_out /* return if usermode */77#else78bnelr-79#endif80112: tophys(r5, r5)81#ifndef CONFIG_PTE_64BIT82rlwimi r5,r4,12,20,29 /* insert top 10 bits of address */83lwz r8,0(r5) /* get pmd entry */84rlwinm. r8,r8,0,0,19 /* extract address of pte page */85#else86rlwinm r8,r4,13,19,29 /* Compute pgdir/pmd offset */87lwzx r8,r8,r5 /* Get L1 entry */88rlwinm. r8,r8,0,0,20 /* extract pt base address */89#endif90#ifdef CONFIG_SMP91beq- .Lhash_page_out /* return if no mapping */92#else93/* XXX it seems like the 601 will give a machine fault on the94rfi if its alignment is wrong (bottom 4 bits of address are958 or 0xc) and we have had a not-taken conditional branch96to the address following the rfi. */97beqlr-98#endif99#ifndef CONFIG_PTE_64BIT100rlwimi r8,r4,22,20,29 /* insert next 10 bits of address */101#else102rlwimi r8,r4,23,20,28 /* compute pte address */103/*104* If PTE_64BIT is set, the low word is the flags word; use that105* word for locking since it contains all the interesting bits.106*/107addi r8,r8,PTE_FLAGS_OFFSET108#endif109110/*111* Update the linux PTE atomically. We do the lwarx up-front112* because almost always, there won't be a permission violation113* and there won't already be an HPTE, and thus we will have114* to update the PTE to set _PAGE_HASHPTE. -- paulus.115*/116.Lretry:117lwarx r6,0,r8 /* get linux-style pte, flag word */118#ifdef CONFIG_PPC_KUAP119mfsrin r5,r4120rlwinm r0,r9,28,_PAGE_WRITE /* MSR[PR] => _PAGE_WRITE */121rlwinm r5,r5,12,_PAGE_WRITE /* Ks => _PAGE_WRITE */122andc r5,r5,r0 /* Ks & ~MSR[PR] */123andc r5,r6,r5 /* Clear _PAGE_WRITE when Ks = 1 && MSR[PR] = 0 */124andc. r5,r3,r5 /* check access & ~permission */125#else126andc. r5,r3,r6 /* check access & ~permission */127#endif128rlwinm r0,r3,32-3,24,24 /* _PAGE_WRITE access -> _PAGE_DIRTY */129ori r0,r0,_PAGE_ACCESSED|_PAGE_HASHPTE130#ifdef CONFIG_SMP131bne- .Lhash_page_out /* return if access not permitted */132#else133bnelr-134#endif135or r5,r0,r6 /* set accessed/dirty bits */136#ifdef CONFIG_PTE_64BIT137#ifdef CONFIG_SMP138subf r10,r6,r8 /* create false data dependency */139subi r10,r10,PTE_FLAGS_OFFSET140lwzx r10,r6,r10 /* Get upper PTE word */141#else142lwz r10,-PTE_FLAGS_OFFSET(r8)143#endif /* CONFIG_SMP */144#endif /* CONFIG_PTE_64BIT */145stwcx. r5,0,r8 /* attempt to update PTE */146bne- .Lretry /* retry if someone got there first */147148mfsrin r3,r4 /* get segment reg for segment */149bl create_hpte /* add the hash table entry */150151#ifdef CONFIG_SMP152eieio153lis r8, (mmu_hash_lock - PAGE_OFFSET)@ha154li r0,0155stw r0, (mmu_hash_lock - PAGE_OFFSET)@l(r8)156#endif157b fast_hash_page_return158159#ifdef CONFIG_SMP160.Lhash_page_out:161eieio162lis r8, (mmu_hash_lock - PAGE_OFFSET)@ha163li r0,0164stw r0, (mmu_hash_lock - PAGE_OFFSET)@l(r8)165blr166#endif /* CONFIG_SMP */167_ASM_NOKPROBE_SYMBOL(hash_page)168169/*170* Add an entry for a particular page to the hash table.171*172* add_hash_page(unsigned context, unsigned long va, unsigned long pmdval)173*174* We assume any necessary modifications to the pte (e.g. setting175* the accessed bit) have already been done and that there is actually176* a hash table in use (i.e. we're not on a 603).177*/178_GLOBAL(add_hash_page)179mflr r0180stw r0,4(r1)181182#ifdef CONFIG_SMP183lwz r8,TASK_CPU(r2) /* to go in mmu_hash_lock */184oris r8,r8,12185#endif /* CONFIG_SMP */186187/*188* We disable interrupts here, even on UP, because we don't189* want to race with hash_page, and because we want the190* _PAGE_HASHPTE bit to be a reliable indication of whether191* the HPTE exists (or at least whether one did once).192* We also turn off the MMU for data accesses so that we193* we can't take a hash table miss (assuming the code is194* covered by a BAT). -- paulus195*/196mfmsr r9197rlwinm r0,r9,0,17,15 /* clear bit 16 (MSR_EE) */198rlwinm r0,r0,0,28,26 /* clear MSR_DR */199mtmsr r0200isync201202#ifdef CONFIG_SMP203lis r6, (mmu_hash_lock - PAGE_OFFSET)@ha204addi r6, r6, (mmu_hash_lock - PAGE_OFFSET)@l20510: lwarx r0,0,r6 /* take the mmu_hash_lock */206cmpwi 0,r0,0207bne- 11f208stwcx. r8,0,r6209beq+ 12f21011: lwz r0,0(r6)211cmpwi 0,r0,0212beq 10b213b 11b21412: isync215#endif216217/*218* Fetch the linux pte and test and set _PAGE_HASHPTE atomically.219* If _PAGE_HASHPTE was already set, we don't replace the existing220* HPTE, so we just unlock and return.221*/222mr r8,r5223#ifndef CONFIG_PTE_64BIT224rlwimi r8,r4,22,20,29225#else226rlwimi r8,r4,23,20,28227addi r8,r8,PTE_FLAGS_OFFSET228#endif2291: lwarx r6,0,r8230andi. r0,r6,_PAGE_HASHPTE231bne 9f /* if HASHPTE already set, done */232#ifdef CONFIG_PTE_64BIT233#ifdef CONFIG_SMP234subf r10,r6,r8 /* create false data dependency */235subi r10,r10,PTE_FLAGS_OFFSET236lwzx r10,r6,r10 /* Get upper PTE word */237#else238lwz r10,-PTE_FLAGS_OFFSET(r8)239#endif /* CONFIG_SMP */240#endif /* CONFIG_PTE_64BIT */241ori r5,r6,_PAGE_HASHPTE242stwcx. r5,0,r8243bne- 1b244245/* Convert context and va to VSID */246mulli r3,r3,897*16 /* multiply context by context skew */247rlwinm r0,r4,4,28,31 /* get ESID (top 4 bits of va) */248mulli r0,r0,0x111 /* multiply by ESID skew */249add r3,r3,r0 /* note create_hpte trims to 24 bits */250251bl create_hpte2522539:254#ifdef CONFIG_SMP255lis r6, (mmu_hash_lock - PAGE_OFFSET)@ha256addi r6, r6, (mmu_hash_lock - PAGE_OFFSET)@l257eieio258li r0,0259stw r0,0(r6) /* clear mmu_hash_lock */260#endif261262/* reenable interrupts and DR */263mtmsr r9264isync265266lwz r0,4(r1)267mtlr r0268blr269_ASM_NOKPROBE_SYMBOL(add_hash_page)270271/*272* This routine adds a hardware PTE to the hash table.273* It is designed to be called with the MMU either on or off.274* r3 contains the VSID, r4 contains the virtual address,275* r5 contains the linux PTE, r6 contains the old value of the276* linux PTE (before setting _PAGE_HASHPTE). r10 contains the277* upper half of the PTE if CONFIG_PTE_64BIT.278* On SMP, the caller should have the mmu_hash_lock held.279* We assume that the caller has (or will) set the _PAGE_HASHPTE280* bit in the linux PTE in memory. The value passed in r6 should281* be the old linux PTE value; if it doesn't have _PAGE_HASHPTE set282* this routine will skip the search for an existing HPTE.283* This procedure modifies r0, r3 - r6, r8, cr0.284* -- paulus.285*286* For speed, 4 of the instructions get patched once the size and287* physical address of the hash table are known. These definitions288* of Hash_base and Hash_bits below are for the early hash table.289*/290Hash_base = early_hash291Hash_bits = 12 /* e.g. 256kB hash table */292Hash_msk = (((1 << Hash_bits) - 1) * 64)293294/* defines for the PTE format for 32-bit PPCs */295#define HPTE_SIZE 8296#define PTEG_SIZE 64297#define LG_PTEG_SIZE 6298#define LDPTEu lwzu299#define LDPTE lwz300#define STPTE stw301#define CMPPTE cmpw302#define PTE_H 0x40303#define PTE_V 0x80000000304#define TST_V(r) rlwinm. r,r,0,0,0305#define SET_V(r) oris r,r,PTE_V@h306#define CLR_V(r,t) rlwinm r,r,0,1,31307308#define HASH_LEFT 31-(LG_PTEG_SIZE+Hash_bits-1)309#define HASH_RIGHT 31-LG_PTEG_SIZE310311__REF312_GLOBAL(create_hpte)313/* Convert linux-style PTE (r5) to low word of PPC-style PTE (r8) */314lis r0, TASK_SIZE@h315rlwinm r5,r5,0,~3 /* Clear PP bits */316cmplw r4,r0317rlwinm r8,r5,32-9,30,30 /* _PAGE_WRITE -> PP msb */318rlwinm r0,r5,32-6,30,30 /* _PAGE_DIRTY -> PP msb */319and r8,r8,r0 /* writable if _RW & _DIRTY */320bge- 1f /* Kernelspace ? Skip */321ori r5,r5,3 /* Userspace ? PP = 3 */3221: ori r8,r8,0xe04 /* clear out reserved bits */323andc r8,r5,r8 /* PP = user? (rw&dirty? 1: 3): 0 */324BEGIN_FTR_SECTION325rlwinm r8,r8,0,~_PAGE_COHERENT /* clear M (coherence not required) */326END_FTR_SECTION_IFCLR(CPU_FTR_NEED_COHERENT)327#ifdef CONFIG_PTE_64BIT328/* Put the XPN bits into the PTE */329rlwimi r8,r10,8,20,22330rlwimi r8,r10,2,29,29331#endif332333/* Construct the high word of the PPC-style PTE (r5) */334rlwinm r5,r3,7,1,24 /* put VSID in 0x7fffff80 bits */335rlwimi r5,r4,10,26,31 /* put in API (abbrev page index) */336SET_V(r5) /* set V (valid) bit */337338patch_site 0f, patch__hash_page_A0339patch_site 1f, patch__hash_page_A1340patch_site 2f, patch__hash_page_A2341/* Get the address of the primary PTE group in the hash table (r3) */3420: lis r0, (Hash_base - PAGE_OFFSET)@h /* base address of hash table */3431: rlwimi r0,r3,LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* VSID -> hash */3442: rlwinm r3,r4,20+LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* PI -> hash */345xor r3,r3,r0 /* make primary hash */346li r0,8 /* PTEs/group */347348/*349* Test the _PAGE_HASHPTE bit in the old linux PTE, and skip the search350* if it is clear, meaning that the HPTE isn't there already...351*/352andi. r6,r6,_PAGE_HASHPTE353beq+ 10f /* no PTE: go look for an empty slot */354tlbie r4355356/* Search the primary PTEG for a PTE whose 1st (d)word matches r5 */357mtctr r0358addi r4,r3,-HPTE_SIZE3591: LDPTEu r6,HPTE_SIZE(r4) /* get next PTE */360CMPPTE 0,r6,r5361bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */362beq+ .Lfound_slot363364patch_site 0f, patch__hash_page_B365/* Search the secondary PTEG for a matching PTE */366ori r5,r5,PTE_H /* set H (secondary hash) bit */3670: xoris r4,r3,Hash_msk>>16 /* compute secondary hash */368xori r4,r4,(-PTEG_SIZE & 0xffff)369addi r4,r4,-HPTE_SIZE370mtctr r03712: LDPTEu r6,HPTE_SIZE(r4)372CMPPTE 0,r6,r5373bdnzf 2,2b374beq+ .Lfound_slot375xori r5,r5,PTE_H /* clear H bit again */376377/* Search the primary PTEG for an empty slot */37810: mtctr r0379addi r4,r3,-HPTE_SIZE /* search primary PTEG */3801: LDPTEu r6,HPTE_SIZE(r4) /* get next PTE */381TST_V(r6) /* test valid bit */382bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */383beq+ .Lfound_empty384385patch_site 0f, patch__hash_page_C386/* Search the secondary PTEG for an empty slot */387ori r5,r5,PTE_H /* set H (secondary hash) bit */3880: xoris r4,r3,Hash_msk>>16 /* compute secondary hash */389xori r4,r4,(-PTEG_SIZE & 0xffff)390addi r4,r4,-HPTE_SIZE391mtctr r03922: LDPTEu r6,HPTE_SIZE(r4)393TST_V(r6)394bdnzf 2,2b395beq+ .Lfound_empty396xori r5,r5,PTE_H /* clear H bit again */397398/*399* Choose an arbitrary slot in the primary PTEG to overwrite.400* Since both the primary and secondary PTEGs are full, and we401* have no information that the PTEs in the primary PTEG are402* more important or useful than those in the secondary PTEG,403* and we know there is a definite (although small) speed404* advantage to putting the PTE in the primary PTEG, we always405* put the PTE in the primary PTEG.406*/407408lis r4, (next_slot - PAGE_OFFSET)@ha /* get next evict slot */409lwz r6, (next_slot - PAGE_OFFSET)@l(r4)410addi r6,r6,HPTE_SIZE /* search for candidate */411andi. r6,r6,7*HPTE_SIZE412stw r6,next_slot@l(r4)413add r4,r3,r6414415#ifndef CONFIG_SMP416/* Store PTE in PTEG */417.Lfound_empty:418STPTE r5,0(r4)419.Lfound_slot:420STPTE r8,HPTE_SIZE/2(r4)421422#else /* CONFIG_SMP */423/*424* Between the tlbie above and updating the hash table entry below,425* another CPU could read the hash table entry and put it in its TLB.426* There are 3 cases:427* 1. using an empty slot428* 2. updating an earlier entry to change permissions (i.e. enable write)429* 3. taking over the PTE for an unrelated address430*431* In each case it doesn't really matter if the other CPUs have the old432* PTE in their TLB. So we don't need to bother with another tlbie here,433* which is convenient as we've overwritten the register that had the434* address. :-) The tlbie above is mainly to make sure that this CPU comes435* and gets the new PTE from the hash table.436*437* We do however have to make sure that the PTE is never in an invalid438* state with the V bit set.439*/440.Lfound_empty:441.Lfound_slot:442CLR_V(r5,r0) /* clear V (valid) bit in PTE */443STPTE r5,0(r4)444sync445TLBSYNC446STPTE r8,HPTE_SIZE/2(r4) /* put in correct RPN, WIMG, PP bits */447sync448SET_V(r5)449STPTE r5,0(r4) /* finally set V bit in PTE */450#endif /* CONFIG_SMP */451452sync /* make sure pte updates get to memory */453blr454.previous455_ASM_NOKPROBE_SYMBOL(create_hpte)456457.section .bss458.align 2459next_slot:460.space 4461.previous462463/*464* Flush the entry for a particular page from the hash table.465*466* flush_hash_pages(unsigned context, unsigned long va, unsigned long pmdval,467* int count)468*469* We assume that there is a hash table in use (Hash != 0).470*/471__REF472_GLOBAL(flush_hash_pages)473/*474* We disable interrupts here, even on UP, because we want475* the _PAGE_HASHPTE bit to be a reliable indication of476* whether the HPTE exists (or at least whether one did once).477* We also turn off the MMU for data accesses so that we478* we can't take a hash table miss (assuming the code is479* covered by a BAT). -- paulus480*/481mfmsr r10482rlwinm r0,r10,0,17,15 /* clear bit 16 (MSR_EE) */483rlwinm r0,r0,0,28,26 /* clear MSR_DR */484mtmsr r0485isync486487/* First find a PTE in the range that has _PAGE_HASHPTE set */488#ifndef CONFIG_PTE_64BIT489rlwimi r5,r4,22,20,29490#else491rlwimi r5,r4,23,20,28492addi r5,r5,PTE_FLAGS_OFFSET493#endif4941: lwz r0,0(r5)495cmpwi cr1,r6,1496andi. r0,r0,_PAGE_HASHPTE497bne 2f498ble cr1,19f499addi r4,r4,0x1000500addi r5,r5,PTE_T_SIZE501addi r6,r6,-1502b 1b503504/* Convert context and va to VSID */5052: mulli r3,r3,897*16 /* multiply context by context skew */506rlwinm r0,r4,4,28,31 /* get ESID (top 4 bits of va) */507mulli r0,r0,0x111 /* multiply by ESID skew */508add r3,r3,r0 /* note code below trims to 24 bits */509510/* Construct the high word of the PPC-style PTE (r11) */511rlwinm r11,r3,7,1,24 /* put VSID in 0x7fffff80 bits */512rlwimi r11,r4,10,26,31 /* put in API (abbrev page index) */513SET_V(r11) /* set V (valid) bit */514515#ifdef CONFIG_SMP516lis r9, (mmu_hash_lock - PAGE_OFFSET)@ha517addi r9, r9, (mmu_hash_lock - PAGE_OFFSET)@l518tophys (r8, r2)519lwz r8, TASK_CPU(r8)520oris r8,r8,952110: lwarx r0,0,r9522cmpwi 0,r0,0523bne- 11f524stwcx. r8,0,r9525beq+ 12f52611: lwz r0,0(r9)527cmpwi 0,r0,0528beq 10b529b 11b53012: isync531#endif532533/*534* Check the _PAGE_HASHPTE bit in the linux PTE. If it is535* already clear, we're done (for this pte). If not,536* clear it (atomically) and proceed. -- paulus.537*/53833: lwarx r8,0,r5 /* fetch the pte flags word */539andi. r0,r8,_PAGE_HASHPTE540beq 8f /* done if HASHPTE is already clear */541rlwinm r8,r8,0,31,29 /* clear HASHPTE bit */542stwcx. r8,0,r5 /* update the pte */543bne- 33b544545patch_site 0f, patch__flush_hash_A0546patch_site 1f, patch__flush_hash_A1547patch_site 2f, patch__flush_hash_A2548/* Get the address of the primary PTE group in the hash table (r3) */5490: lis r8, (Hash_base - PAGE_OFFSET)@h /* base address of hash table */5501: rlwimi r8,r3,LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* VSID -> hash */5512: rlwinm r0,r4,20+LG_PTEG_SIZE,HASH_LEFT,HASH_RIGHT /* PI -> hash */552xor r8,r0,r8 /* make primary hash */553554/* Search the primary PTEG for a PTE whose 1st (d)word matches r5 */555li r0,8 /* PTEs/group */556mtctr r0557addi r12,r8,-HPTE_SIZE5581: LDPTEu r0,HPTE_SIZE(r12) /* get next PTE */559CMPPTE 0,r0,r11560bdnzf 2,1b /* loop while ctr != 0 && !cr0.eq */561beq+ 3f562563patch_site 0f, patch__flush_hash_B564/* Search the secondary PTEG for a matching PTE */565ori r11,r11,PTE_H /* set H (secondary hash) bit */566li r0,8 /* PTEs/group */5670: xoris r12,r8,Hash_msk>>16 /* compute secondary hash */568xori r12,r12,(-PTEG_SIZE & 0xffff)569addi r12,r12,-HPTE_SIZE570mtctr r05712: LDPTEu r0,HPTE_SIZE(r12)572CMPPTE 0,r0,r11573bdnzf 2,2b574xori r11,r11,PTE_H /* clear H again */575bne- 4f /* should rarely fail to find it */5765773: li r0,0578STPTE r0,0(r12) /* invalidate entry */5794: sync580tlbie r4 /* in hw tlb too */581sync5825838: ble cr1,9f /* if all ptes checked */58481: addi r6,r6,-1585addi r5,r5,PTE_T_SIZE586addi r4,r4,0x1000587lwz r0,0(r5) /* check next pte */588cmpwi cr1,r6,1589andi. r0,r0,_PAGE_HASHPTE590bne 33b591bgt cr1,81b5925939:594#ifdef CONFIG_SMP595TLBSYNC596li r0,0597stw r0,0(r9) /* clear mmu_hash_lock */598#endif59960019: mtmsr r10601isync602blr603.previous604EXPORT_SYMBOL(flush_hash_pages)605_ASM_NOKPROBE_SYMBOL(flush_hash_pages)606607608