Path: blob/master/arch/powerpc/mm/hugetlbpage-hash64.c
10820 views
/*1* PPC64 Huge TLB Page Support for hash based MMUs (POWER4 and later)2*3* Copyright (C) 2003 David Gibson, IBM Corporation.4*5* Based on the IA-32 version:6* Copyright (C) 2002, Rohit Seth <[email protected]>7*/89#include <linux/mm.h>10#include <linux/hugetlb.h>11#include <asm/pgtable.h>12#include <asm/pgalloc.h>13#include <asm/cacheflush.h>14#include <asm/machdep.h>1516int __hash_page_huge(unsigned long ea, unsigned long access, unsigned long vsid,17pte_t *ptep, unsigned long trap, int local, int ssize,18unsigned int shift, unsigned int mmu_psize)19{20unsigned long old_pte, new_pte;21unsigned long va, rflags, pa, sz;22long slot;2324BUG_ON(shift != mmu_psize_defs[mmu_psize].shift);2526/* Search the Linux page table for a match with va */27va = hpt_va(ea, vsid, ssize);2829/* At this point, we have a pte (old_pte) which can be used to build30* or update an HPTE. There are 2 cases:31*32* 1. There is a valid (present) pte with no associated HPTE (this is33* the most common case)34* 2. There is a valid (present) pte with an associated HPTE. The35* current values of the pp bits in the HPTE prevent access36* because we are doing software DIRTY bit management and the37* page is currently not DIRTY.38*/394041do {42old_pte = pte_val(*ptep);43/* If PTE busy, retry the access */44if (unlikely(old_pte & _PAGE_BUSY))45return 0;46/* If PTE permissions don't match, take page fault */47if (unlikely(access & ~old_pte))48return 1;49/* Try to lock the PTE, add ACCESSED and DIRTY if it was50* a write access */51new_pte = old_pte | _PAGE_BUSY | _PAGE_ACCESSED;52if (access & _PAGE_RW)53new_pte |= _PAGE_DIRTY;54} while(old_pte != __cmpxchg_u64((unsigned long *)ptep,55old_pte, new_pte));5657rflags = 0x2 | (!(new_pte & _PAGE_RW));58/* _PAGE_EXEC -> HW_NO_EXEC since it's inverted */59rflags |= ((new_pte & _PAGE_EXEC) ? 0 : HPTE_R_N);60sz = ((1UL) << shift);61if (!cpu_has_feature(CPU_FTR_COHERENT_ICACHE))62/* No CPU has hugepages but lacks no execute, so we63* don't need to worry about that case */64rflags = hash_page_do_lazy_icache(rflags, __pte(old_pte), trap);6566/* Check if pte already has an hpte (case 2) */67if (unlikely(old_pte & _PAGE_HASHPTE)) {68/* There MIGHT be an HPTE for this pte */69unsigned long hash, slot;7071hash = hpt_hash(va, shift, ssize);72if (old_pte & _PAGE_F_SECOND)73hash = ~hash;74slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;75slot += (old_pte & _PAGE_F_GIX) >> 12;7677if (ppc_md.hpte_updatepp(slot, rflags, va, mmu_psize,78ssize, local) == -1)79old_pte &= ~_PAGE_HPTEFLAGS;80}8182if (likely(!(old_pte & _PAGE_HASHPTE))) {83unsigned long hash = hpt_hash(va, shift, ssize);84unsigned long hpte_group;8586pa = pte_pfn(__pte(old_pte)) << PAGE_SHIFT;8788repeat:89hpte_group = ((hash & htab_hash_mask) *90HPTES_PER_GROUP) & ~0x7UL;9192/* clear HPTE slot informations in new PTE */93#ifdef CONFIG_PPC_64K_PAGES94new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HPTE_SUB0;95#else96new_pte = (new_pte & ~_PAGE_HPTEFLAGS) | _PAGE_HASHPTE;97#endif98/* Add in WIMG bits */99rflags |= (new_pte & (_PAGE_WRITETHRU | _PAGE_NO_CACHE |100_PAGE_COHERENT | _PAGE_GUARDED));101102/* Insert into the hash table, primary slot */103slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags, 0,104mmu_psize, ssize);105106/* Primary is full, try the secondary */107if (unlikely(slot == -1)) {108hpte_group = ((~hash & htab_hash_mask) *109HPTES_PER_GROUP) & ~0x7UL;110slot = ppc_md.hpte_insert(hpte_group, va, pa, rflags,111HPTE_V_SECONDARY,112mmu_psize, ssize);113if (slot == -1) {114if (mftb() & 0x1)115hpte_group = ((hash & htab_hash_mask) *116HPTES_PER_GROUP)&~0x7UL;117118ppc_md.hpte_remove(hpte_group);119goto repeat;120}121}122123/*124* Hypervisor failure. Restore old pte and return -1125* similar to __hash_page_*126*/127if (unlikely(slot == -2)) {128*ptep = __pte(old_pte);129hash_failure_debug(ea, access, vsid, trap, ssize,130mmu_psize, old_pte);131return -1;132}133134new_pte |= (slot << 12) & (_PAGE_F_SECOND | _PAGE_F_GIX);135}136137/*138* No need to use ldarx/stdcx here139*/140*ptep = __pte(new_pte & ~_PAGE_BUSY);141return 0;142}143144145