Path: blob/master/arch/powerpc/mm/hash_native_64.c
10817 views
/*1* native hashtable management.2*3* SMP scalability work:4* Copyright (C) 2001 Anton Blanchard <[email protected]>, IBM5*6* This program is free software; you can redistribute it and/or7* modify it under the terms of the GNU General Public License8* as published by the Free Software Foundation; either version9* 2 of the License, or (at your option) any later version.10*/1112#undef DEBUG_LOW1314#include <linux/spinlock.h>15#include <linux/bitops.h>16#include <linux/threads.h>17#include <linux/smp.h>1819#include <asm/abs_addr.h>20#include <asm/machdep.h>21#include <asm/mmu.h>22#include <asm/mmu_context.h>23#include <asm/pgtable.h>24#include <asm/tlbflush.h>25#include <asm/tlb.h>26#include <asm/cputable.h>27#include <asm/udbg.h>28#include <asm/kexec.h>29#include <asm/ppc-opcode.h>3031#ifdef DEBUG_LOW32#define DBG_LOW(fmt...) udbg_printf(fmt)33#else34#define DBG_LOW(fmt...)35#endif3637#define HPTE_LOCK_BIT 33839static DEFINE_RAW_SPINLOCK(native_tlbie_lock);4041static inline void __tlbie(unsigned long va, int psize, int ssize)42{43unsigned int penc;4445/* clear top 16 bits, non SLS segment */46va &= ~(0xffffULL << 48);4748switch (psize) {49case MMU_PAGE_4K:50va &= ~0xffful;51va |= ssize << 8;52asm volatile(ASM_FTR_IFCLR("tlbie %0,0", PPC_TLBIE(%1,%0), %2)53: : "r" (va), "r"(0), "i" (CPU_FTR_HVMODE_206)54: "memory");55break;56default:57penc = mmu_psize_defs[psize].penc;58va &= ~((1ul << mmu_psize_defs[psize].shift) - 1);59va |= penc << 12;60va |= ssize << 8;61va |= 1; /* L */62asm volatile(ASM_FTR_IFCLR("tlbie %0,1", PPC_TLBIE(%1,%0), %2)63: : "r" (va), "r"(0), "i" (CPU_FTR_HVMODE_206)64: "memory");65break;66}67}6869static inline void __tlbiel(unsigned long va, int psize, int ssize)70{71unsigned int penc;7273/* clear top 16 bits, non SLS segment */74va &= ~(0xffffULL << 48);7576switch (psize) {77case MMU_PAGE_4K:78va &= ~0xffful;79va |= ssize << 8;80asm volatile(".long 0x7c000224 | (%0 << 11) | (0 << 21)"81: : "r"(va) : "memory");82break;83default:84penc = mmu_psize_defs[psize].penc;85va &= ~((1ul << mmu_psize_defs[psize].shift) - 1);86va |= penc << 12;87va |= ssize << 8;88va |= 1; /* L */89asm volatile(".long 0x7c000224 | (%0 << 11) | (1 << 21)"90: : "r"(va) : "memory");91break;92}9394}9596static inline void tlbie(unsigned long va, int psize, int ssize, int local)97{98unsigned int use_local = local && mmu_has_feature(MMU_FTR_TLBIEL);99int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);100101if (use_local)102use_local = mmu_psize_defs[psize].tlbiel;103if (lock_tlbie && !use_local)104raw_spin_lock(&native_tlbie_lock);105asm volatile("ptesync": : :"memory");106if (use_local) {107__tlbiel(va, psize, ssize);108asm volatile("ptesync": : :"memory");109} else {110__tlbie(va, psize, ssize);111asm volatile("eieio; tlbsync; ptesync": : :"memory");112}113if (lock_tlbie && !use_local)114raw_spin_unlock(&native_tlbie_lock);115}116117static inline void native_lock_hpte(struct hash_pte *hptep)118{119unsigned long *word = &hptep->v;120121while (1) {122if (!test_and_set_bit_lock(HPTE_LOCK_BIT, word))123break;124while(test_bit(HPTE_LOCK_BIT, word))125cpu_relax();126}127}128129static inline void native_unlock_hpte(struct hash_pte *hptep)130{131unsigned long *word = &hptep->v;132133clear_bit_unlock(HPTE_LOCK_BIT, word);134}135136static long native_hpte_insert(unsigned long hpte_group, unsigned long va,137unsigned long pa, unsigned long rflags,138unsigned long vflags, int psize, int ssize)139{140struct hash_pte *hptep = htab_address + hpte_group;141unsigned long hpte_v, hpte_r;142int i;143144if (!(vflags & HPTE_V_BOLTED)) {145DBG_LOW(" insert(group=%lx, va=%016lx, pa=%016lx,"146" rflags=%lx, vflags=%lx, psize=%d)\n",147hpte_group, va, pa, rflags, vflags, psize);148}149150for (i = 0; i < HPTES_PER_GROUP; i++) {151if (! (hptep->v & HPTE_V_VALID)) {152/* retry with lock held */153native_lock_hpte(hptep);154if (! (hptep->v & HPTE_V_VALID))155break;156native_unlock_hpte(hptep);157}158159hptep++;160}161162if (i == HPTES_PER_GROUP)163return -1;164165hpte_v = hpte_encode_v(va, psize, ssize) | vflags | HPTE_V_VALID;166hpte_r = hpte_encode_r(pa, psize) | rflags;167168if (!(vflags & HPTE_V_BOLTED)) {169DBG_LOW(" i=%x hpte_v=%016lx, hpte_r=%016lx\n",170i, hpte_v, hpte_r);171}172173hptep->r = hpte_r;174/* Guarantee the second dword is visible before the valid bit */175eieio();176/*177* Now set the first dword including the valid bit178* NOTE: this also unlocks the hpte179*/180hptep->v = hpte_v;181182__asm__ __volatile__ ("ptesync" : : : "memory");183184return i | (!!(vflags & HPTE_V_SECONDARY) << 3);185}186187static long native_hpte_remove(unsigned long hpte_group)188{189struct hash_pte *hptep;190int i;191int slot_offset;192unsigned long hpte_v;193194DBG_LOW(" remove(group=%lx)\n", hpte_group);195196/* pick a random entry to start at */197slot_offset = mftb() & 0x7;198199for (i = 0; i < HPTES_PER_GROUP; i++) {200hptep = htab_address + hpte_group + slot_offset;201hpte_v = hptep->v;202203if ((hpte_v & HPTE_V_VALID) && !(hpte_v & HPTE_V_BOLTED)) {204/* retry with lock held */205native_lock_hpte(hptep);206hpte_v = hptep->v;207if ((hpte_v & HPTE_V_VALID)208&& !(hpte_v & HPTE_V_BOLTED))209break;210native_unlock_hpte(hptep);211}212213slot_offset++;214slot_offset &= 0x7;215}216217if (i == HPTES_PER_GROUP)218return -1;219220/* Invalidate the hpte. NOTE: this also unlocks it */221hptep->v = 0;222223return i;224}225226static long native_hpte_updatepp(unsigned long slot, unsigned long newpp,227unsigned long va, int psize, int ssize,228int local)229{230struct hash_pte *hptep = htab_address + slot;231unsigned long hpte_v, want_v;232int ret = 0;233234want_v = hpte_encode_v(va, psize, ssize);235236DBG_LOW(" update(va=%016lx, avpnv=%016lx, hash=%016lx, newpp=%x)",237va, want_v & HPTE_V_AVPN, slot, newpp);238239native_lock_hpte(hptep);240241hpte_v = hptep->v;242243/* Even if we miss, we need to invalidate the TLB */244if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID)) {245DBG_LOW(" -> miss\n");246ret = -1;247} else {248DBG_LOW(" -> hit\n");249/* Update the HPTE */250hptep->r = (hptep->r & ~(HPTE_R_PP | HPTE_R_N)) |251(newpp & (HPTE_R_PP | HPTE_R_N | HPTE_R_C));252}253native_unlock_hpte(hptep);254255/* Ensure it is out of the tlb too. */256tlbie(va, psize, ssize, local);257258return ret;259}260261static long native_hpte_find(unsigned long va, int psize, int ssize)262{263struct hash_pte *hptep;264unsigned long hash;265unsigned long i;266long slot;267unsigned long want_v, hpte_v;268269hash = hpt_hash(va, mmu_psize_defs[psize].shift, ssize);270want_v = hpte_encode_v(va, psize, ssize);271272/* Bolted mappings are only ever in the primary group */273slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;274for (i = 0; i < HPTES_PER_GROUP; i++) {275hptep = htab_address + slot;276hpte_v = hptep->v;277278if (HPTE_V_COMPARE(hpte_v, want_v) && (hpte_v & HPTE_V_VALID))279/* HPTE matches */280return slot;281++slot;282}283284return -1;285}286287/*288* Update the page protection bits. Intended to be used to create289* guard pages for kernel data structures on pages which are bolted290* in the HPT. Assumes pages being operated on will not be stolen.291*292* No need to lock here because we should be the only user.293*/294static void native_hpte_updateboltedpp(unsigned long newpp, unsigned long ea,295int psize, int ssize)296{297unsigned long vsid, va;298long slot;299struct hash_pte *hptep;300301vsid = get_kernel_vsid(ea, ssize);302va = hpt_va(ea, vsid, ssize);303304slot = native_hpte_find(va, psize, ssize);305if (slot == -1)306panic("could not find page to bolt\n");307hptep = htab_address + slot;308309/* Update the HPTE */310hptep->r = (hptep->r & ~(HPTE_R_PP | HPTE_R_N)) |311(newpp & (HPTE_R_PP | HPTE_R_N));312313/* Ensure it is out of the tlb too. */314tlbie(va, psize, ssize, 0);315}316317static void native_hpte_invalidate(unsigned long slot, unsigned long va,318int psize, int ssize, int local)319{320struct hash_pte *hptep = htab_address + slot;321unsigned long hpte_v;322unsigned long want_v;323unsigned long flags;324325local_irq_save(flags);326327DBG_LOW(" invalidate(va=%016lx, hash: %x)\n", va, slot);328329want_v = hpte_encode_v(va, psize, ssize);330native_lock_hpte(hptep);331hpte_v = hptep->v;332333/* Even if we miss, we need to invalidate the TLB */334if (!HPTE_V_COMPARE(hpte_v, want_v) || !(hpte_v & HPTE_V_VALID))335native_unlock_hpte(hptep);336else337/* Invalidate the hpte. NOTE: this also unlocks it */338hptep->v = 0;339340/* Invalidate the TLB */341tlbie(va, psize, ssize, local);342343local_irq_restore(flags);344}345346#define LP_SHIFT 12347#define LP_BITS 8348#define LP_MASK(i) ((0xFF >> (i)) << LP_SHIFT)349350static void hpte_decode(struct hash_pte *hpte, unsigned long slot,351int *psize, int *ssize, unsigned long *va)352{353unsigned long hpte_r = hpte->r;354unsigned long hpte_v = hpte->v;355unsigned long avpn;356int i, size, shift, penc;357358if (!(hpte_v & HPTE_V_LARGE))359size = MMU_PAGE_4K;360else {361for (i = 0; i < LP_BITS; i++) {362if ((hpte_r & LP_MASK(i+1)) == LP_MASK(i+1))363break;364}365penc = LP_MASK(i+1) >> LP_SHIFT;366for (size = 0; size < MMU_PAGE_COUNT; size++) {367368/* 4K pages are not represented by LP */369if (size == MMU_PAGE_4K)370continue;371372/* valid entries have a shift value */373if (!mmu_psize_defs[size].shift)374continue;375376if (penc == mmu_psize_defs[size].penc)377break;378}379}380381/* This works for all page sizes, and for 256M and 1T segments */382shift = mmu_psize_defs[size].shift;383avpn = (HPTE_V_AVPN_VAL(hpte_v) & ~mmu_psize_defs[size].avpnm) << 23;384385if (shift < 23) {386unsigned long vpi, vsid, pteg;387388pteg = slot / HPTES_PER_GROUP;389if (hpte_v & HPTE_V_SECONDARY)390pteg = ~pteg;391switch (hpte_v >> HPTE_V_SSIZE_SHIFT) {392case MMU_SEGSIZE_256M:393vpi = ((avpn >> 28) ^ pteg) & htab_hash_mask;394break;395case MMU_SEGSIZE_1T:396vsid = avpn >> 40;397vpi = (vsid ^ (vsid << 25) ^ pteg) & htab_hash_mask;398break;399default:400avpn = vpi = size = 0;401}402avpn |= (vpi << mmu_psize_defs[size].shift);403}404405*va = avpn;406*psize = size;407*ssize = hpte_v >> HPTE_V_SSIZE_SHIFT;408}409410/*411* clear all mappings on kexec. All cpus are in real mode (or they will412* be when they isi), and we are the only one left. We rely on our kernel413* mapping being 0xC0's and the hardware ignoring those two real bits.414*415* TODO: add batching support when enabled. remember, no dynamic memory here,416* athough there is the control page available...417*/418static void native_hpte_clear(void)419{420unsigned long slot, slots, flags;421struct hash_pte *hptep = htab_address;422unsigned long hpte_v, va;423unsigned long pteg_count;424int psize, ssize;425426pteg_count = htab_hash_mask + 1;427428local_irq_save(flags);429430/* we take the tlbie lock and hold it. Some hardware will431* deadlock if we try to tlbie from two processors at once.432*/433raw_spin_lock(&native_tlbie_lock);434435slots = pteg_count * HPTES_PER_GROUP;436437for (slot = 0; slot < slots; slot++, hptep++) {438/*439* we could lock the pte here, but we are the only cpu440* running, right? and for crash dump, we probably441* don't want to wait for a maybe bad cpu.442*/443hpte_v = hptep->v;444445/*446* Call __tlbie() here rather than tlbie() since we447* already hold the native_tlbie_lock.448*/449if (hpte_v & HPTE_V_VALID) {450hpte_decode(hptep, slot, &psize, &ssize, &va);451hptep->v = 0;452__tlbie(va, psize, ssize);453}454}455456asm volatile("eieio; tlbsync; ptesync":::"memory");457raw_spin_unlock(&native_tlbie_lock);458local_irq_restore(flags);459}460461/*462* Batched hash table flush, we batch the tlbie's to avoid taking/releasing463* the lock all the time464*/465static void native_flush_hash_range(unsigned long number, int local)466{467unsigned long va, hash, index, hidx, shift, slot;468struct hash_pte *hptep;469unsigned long hpte_v;470unsigned long want_v;471unsigned long flags;472real_pte_t pte;473struct ppc64_tlb_batch *batch = &__get_cpu_var(ppc64_tlb_batch);474unsigned long psize = batch->psize;475int ssize = batch->ssize;476int i;477478local_irq_save(flags);479480for (i = 0; i < number; i++) {481va = batch->vaddr[i];482pte = batch->pte[i];483484pte_iterate_hashed_subpages(pte, psize, va, index, shift) {485hash = hpt_hash(va, shift, ssize);486hidx = __rpte_to_hidx(pte, index);487if (hidx & _PTEIDX_SECONDARY)488hash = ~hash;489slot = (hash & htab_hash_mask) * HPTES_PER_GROUP;490slot += hidx & _PTEIDX_GROUP_IX;491hptep = htab_address + slot;492want_v = hpte_encode_v(va, psize, ssize);493native_lock_hpte(hptep);494hpte_v = hptep->v;495if (!HPTE_V_COMPARE(hpte_v, want_v) ||496!(hpte_v & HPTE_V_VALID))497native_unlock_hpte(hptep);498else499hptep->v = 0;500} pte_iterate_hashed_end();501}502503if (mmu_has_feature(MMU_FTR_TLBIEL) &&504mmu_psize_defs[psize].tlbiel && local) {505asm volatile("ptesync":::"memory");506for (i = 0; i < number; i++) {507va = batch->vaddr[i];508pte = batch->pte[i];509510pte_iterate_hashed_subpages(pte, psize, va, index,511shift) {512__tlbiel(va, psize, ssize);513} pte_iterate_hashed_end();514}515asm volatile("ptesync":::"memory");516} else {517int lock_tlbie = !mmu_has_feature(MMU_FTR_LOCKLESS_TLBIE);518519if (lock_tlbie)520raw_spin_lock(&native_tlbie_lock);521522asm volatile("ptesync":::"memory");523for (i = 0; i < number; i++) {524va = batch->vaddr[i];525pte = batch->pte[i];526527pte_iterate_hashed_subpages(pte, psize, va, index,528shift) {529__tlbie(va, psize, ssize);530} pte_iterate_hashed_end();531}532asm volatile("eieio; tlbsync; ptesync":::"memory");533534if (lock_tlbie)535raw_spin_unlock(&native_tlbie_lock);536}537538local_irq_restore(flags);539}540541#ifdef CONFIG_PPC_PSERIES542/* Disable TLB batching on nighthawk */543static inline int tlb_batching_enabled(void)544{545struct device_node *root = of_find_node_by_path("/");546int enabled = 1;547548if (root) {549const char *model = of_get_property(root, "model", NULL);550if (model && !strcmp(model, "IBM,9076-N81"))551enabled = 0;552of_node_put(root);553}554555return enabled;556}557#else558static inline int tlb_batching_enabled(void)559{560return 1;561}562#endif563564void __init hpte_init_native(void)565{566ppc_md.hpte_invalidate = native_hpte_invalidate;567ppc_md.hpte_updatepp = native_hpte_updatepp;568ppc_md.hpte_updateboltedpp = native_hpte_updateboltedpp;569ppc_md.hpte_insert = native_hpte_insert;570ppc_md.hpte_remove = native_hpte_remove;571ppc_md.hpte_clear_all = native_hpte_clear;572if (tlb_batching_enabled())573ppc_md.flush_hash_range = native_flush_hash_range;574}575576577