Path: blob/master/drivers/misc/sgi-gru/grutlbpurge.c
15111 views
/*1* SN Platform GRU Driver2*3* MMUOPS callbacks + TLB flushing4*5* This file handles emu notifier callbacks from the core kernel. The callbacks6* are used to update the TLB in the GRU as a result of changes in the7* state of a process address space. This file also handles TLB invalidates8* from the GRU driver.9*10* Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.11*12* This program is free software; you can redistribute it and/or modify13* it under the terms of the GNU General Public License as published by14* the Free Software Foundation; either version 2 of the License, or15* (at your option) any later version.16*17* This program is distributed in the hope that it will be useful,18* but WITHOUT ANY WARRANTY; without even the implied warranty of19* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the20* GNU General Public License for more details.21*22* You should have received a copy of the GNU General Public License23* along with this program; if not, write to the Free Software24* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA25*/2627#include <linux/kernel.h>28#include <linux/list.h>29#include <linux/spinlock.h>30#include <linux/mm.h>31#include <linux/slab.h>32#include <linux/device.h>33#include <linux/hugetlb.h>34#include <linux/delay.h>35#include <linux/timex.h>36#include <linux/srcu.h>37#include <asm/processor.h>38#include "gru.h"39#include "grutables.h"40#include <asm/uv/uv_hub.h>4142#define gru_random() get_cycles()4344/* ---------------------------------- TLB Invalidation functions --------45* get_tgh_handle46*47* Find a TGH to use for issuing a TLB invalidate. For GRUs that are on the48* local blade, use a fixed TGH that is a function of the blade-local cpu49* number. Normally, this TGH is private to the cpu & no contention occurs for50* the TGH. For offblade GRUs, select a random TGH in the range above the51* private TGHs. A spinlock is required to access this TGH & the lock must be52* released when the invalidate is completes. This sucks, but it is the best we53* can do.54*55* Note that the spinlock is IN the TGH handle so locking does not involve56* additional cache lines.57*58*/59static inline int get_off_blade_tgh(struct gru_state *gru)60{61int n;6263n = GRU_NUM_TGH - gru->gs_tgh_first_remote;64n = gru_random() % n;65n += gru->gs_tgh_first_remote;66return n;67}6869static inline int get_on_blade_tgh(struct gru_state *gru)70{71return uv_blade_processor_id() >> gru->gs_tgh_local_shift;72}7374static struct gru_tlb_global_handle *get_lock_tgh_handle(struct gru_state75*gru)76{77struct gru_tlb_global_handle *tgh;78int n;7980preempt_disable();81if (uv_numa_blade_id() == gru->gs_blade_id)82n = get_on_blade_tgh(gru);83else84n = get_off_blade_tgh(gru);85tgh = get_tgh_by_index(gru, n);86lock_tgh_handle(tgh);8788return tgh;89}9091static void get_unlock_tgh_handle(struct gru_tlb_global_handle *tgh)92{93unlock_tgh_handle(tgh);94preempt_enable();95}9697/*98* gru_flush_tlb_range99*100* General purpose TLB invalidation function. This function scans every GRU in101* the ENTIRE system (partition) looking for GRUs where the specified MM has102* been accessed by the GRU. For each GRU found, the TLB must be invalidated OR103* the ASID invalidated. Invalidating an ASID causes a new ASID to be assigned104* on the next fault. This effectively flushes the ENTIRE TLB for the MM at the105* cost of (possibly) a large number of future TLBmisses.106*107* The current algorithm is optimized based on the following (somewhat true)108* assumptions:109* - GRU contexts are not loaded into a GRU unless a reference is made to110* the data segment or control block (this is true, not an assumption).111* If a DS/CB is referenced, the user will also issue instructions that112* cause TLBmisses. It is not necessary to optimize for the case where113* contexts are loaded but no instructions cause TLB misses. (I know114* this will happen but I'm not optimizing for it).115* - GRU instructions to invalidate TLB entries are SLOOOOWWW - normally116* a few usec but in unusual cases, it could be longer. Avoid if117* possible.118* - intrablade process migration between cpus is not frequent but is119* common.120* - a GRU context is not typically migrated to a different GRU on the121* blade because of intrablade migration122* - interblade migration is rare. Processes migrate their GRU context to123* the new blade.124* - if interblade migration occurs, migration back to the original blade125* is very very rare (ie., no optimization for this case)126* - most GRU instruction operate on a subset of the user REGIONS. Code127* & shared library regions are not likely targets of GRU instructions.128*129* To help improve the efficiency of TLB invalidation, the GMS data130* structure is maintained for EACH address space (MM struct). The GMS is131* also the structure that contains the pointer to the mmu callout132* functions. This structure is linked to the mm_struct for the address space133* using the mmu "register" function. The mmu interfaces are used to134* provide the callbacks for TLB invalidation. The GMS contains:135*136* - asid[maxgrus] array. ASIDs are assigned to a GRU when a context is137* loaded into the GRU.138* - asidmap[maxgrus]. bitmap to make it easier to find non-zero asids in139* the above array140* - ctxbitmap[maxgrus]. Indicates the contexts that are currently active141* in the GRU for the address space. This bitmap must be passed to the142* GRU to do an invalidate.143*144* The current algorithm for invalidating TLBs is:145* - scan the asidmap for GRUs where the context has been loaded, ie,146* asid is non-zero.147* - for each gru found:148* - if the ctxtmap is non-zero, there are active contexts in the149* GRU. TLB invalidate instructions must be issued to the GRU.150* - if the ctxtmap is zero, no context is active. Set the ASID to151* zero to force a full TLB invalidation. This is fast but will152* cause a lot of TLB misses if the context is reloaded onto the153* GRU154*155*/156157void gru_flush_tlb_range(struct gru_mm_struct *gms, unsigned long start,158unsigned long len)159{160struct gru_state *gru;161struct gru_mm_tracker *asids;162struct gru_tlb_global_handle *tgh;163unsigned long num;164int grupagesize, pagesize, pageshift, gid, asid;165166/* ZZZ TODO - handle huge pages */167pageshift = PAGE_SHIFT;168pagesize = (1UL << pageshift);169grupagesize = GRU_PAGESIZE(pageshift);170num = min(((len + pagesize - 1) >> pageshift), GRUMAXINVAL);171172STAT(flush_tlb);173gru_dbg(grudev, "gms %p, start 0x%lx, len 0x%lx, asidmap 0x%lx\n", gms,174start, len, gms->ms_asidmap[0]);175176spin_lock(&gms->ms_asid_lock);177for_each_gru_in_bitmap(gid, gms->ms_asidmap) {178STAT(flush_tlb_gru);179gru = GID_TO_GRU(gid);180asids = gms->ms_asids + gid;181asid = asids->mt_asid;182if (asids->mt_ctxbitmap && asid) {183STAT(flush_tlb_gru_tgh);184asid = GRUASID(asid, start);185gru_dbg(grudev,186" FLUSH gruid %d, asid 0x%x, vaddr 0x%lx, vamask 0x%x, num %ld, cbmap 0x%x\n",187gid, asid, start, grupagesize, num, asids->mt_ctxbitmap);188tgh = get_lock_tgh_handle(gru);189tgh_invalidate(tgh, start, ~0, asid, grupagesize, 0,190num - 1, asids->mt_ctxbitmap);191get_unlock_tgh_handle(tgh);192} else {193STAT(flush_tlb_gru_zero_asid);194asids->mt_asid = 0;195__clear_bit(gru->gs_gid, gms->ms_asidmap);196gru_dbg(grudev,197" CLEARASID gruid %d, asid 0x%x, cbtmap 0x%x, asidmap 0x%lx\n",198gid, asid, asids->mt_ctxbitmap,199gms->ms_asidmap[0]);200}201}202spin_unlock(&gms->ms_asid_lock);203}204205/*206* Flush the entire TLB on a chiplet.207*/208void gru_flush_all_tlb(struct gru_state *gru)209{210struct gru_tlb_global_handle *tgh;211212gru_dbg(grudev, "gid %d\n", gru->gs_gid);213tgh = get_lock_tgh_handle(gru);214tgh_invalidate(tgh, 0, ~0, 0, 1, 1, GRUMAXINVAL - 1, 0xffff);215get_unlock_tgh_handle(tgh);216}217218/*219* MMUOPS notifier callout functions220*/221static void gru_invalidate_range_start(struct mmu_notifier *mn,222struct mm_struct *mm,223unsigned long start, unsigned long end)224{225struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct,226ms_notifier);227228STAT(mmu_invalidate_range);229atomic_inc(&gms->ms_range_active);230gru_dbg(grudev, "gms %p, start 0x%lx, end 0x%lx, act %d\n", gms,231start, end, atomic_read(&gms->ms_range_active));232gru_flush_tlb_range(gms, start, end - start);233}234235static void gru_invalidate_range_end(struct mmu_notifier *mn,236struct mm_struct *mm, unsigned long start,237unsigned long end)238{239struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct,240ms_notifier);241242/* ..._and_test() provides needed barrier */243(void)atomic_dec_and_test(&gms->ms_range_active);244245wake_up_all(&gms->ms_wait_queue);246gru_dbg(grudev, "gms %p, start 0x%lx, end 0x%lx\n", gms, start, end);247}248249static void gru_invalidate_page(struct mmu_notifier *mn, struct mm_struct *mm,250unsigned long address)251{252struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct,253ms_notifier);254255STAT(mmu_invalidate_page);256gru_flush_tlb_range(gms, address, PAGE_SIZE);257gru_dbg(grudev, "gms %p, address 0x%lx\n", gms, address);258}259260static void gru_release(struct mmu_notifier *mn, struct mm_struct *mm)261{262struct gru_mm_struct *gms = container_of(mn, struct gru_mm_struct,263ms_notifier);264265gms->ms_released = 1;266gru_dbg(grudev, "gms %p\n", gms);267}268269270static const struct mmu_notifier_ops gru_mmuops = {271.invalidate_page = gru_invalidate_page,272.invalidate_range_start = gru_invalidate_range_start,273.invalidate_range_end = gru_invalidate_range_end,274.release = gru_release,275};276277/* Move this to the basic mmu_notifier file. But for now... */278static struct mmu_notifier *mmu_find_ops(struct mm_struct *mm,279const struct mmu_notifier_ops *ops)280{281struct mmu_notifier *mn, *gru_mn = NULL;282struct hlist_node *n;283284if (mm->mmu_notifier_mm) {285rcu_read_lock();286hlist_for_each_entry_rcu(mn, n, &mm->mmu_notifier_mm->list,287hlist)288if (mn->ops == ops) {289gru_mn = mn;290break;291}292rcu_read_unlock();293}294return gru_mn;295}296297struct gru_mm_struct *gru_register_mmu_notifier(void)298{299struct gru_mm_struct *gms;300struct mmu_notifier *mn;301int err;302303mn = mmu_find_ops(current->mm, &gru_mmuops);304if (mn) {305gms = container_of(mn, struct gru_mm_struct, ms_notifier);306atomic_inc(&gms->ms_refcnt);307} else {308gms = kzalloc(sizeof(*gms), GFP_KERNEL);309if (gms) {310STAT(gms_alloc);311spin_lock_init(&gms->ms_asid_lock);312gms->ms_notifier.ops = &gru_mmuops;313atomic_set(&gms->ms_refcnt, 1);314init_waitqueue_head(&gms->ms_wait_queue);315err = __mmu_notifier_register(&gms->ms_notifier, current->mm);316if (err)317goto error;318}319}320gru_dbg(grudev, "gms %p, refcnt %d\n", gms,321atomic_read(&gms->ms_refcnt));322return gms;323error:324kfree(gms);325return ERR_PTR(err);326}327328void gru_drop_mmu_notifier(struct gru_mm_struct *gms)329{330gru_dbg(grudev, "gms %p, refcnt %d, released %d\n", gms,331atomic_read(&gms->ms_refcnt), gms->ms_released);332if (atomic_dec_return(&gms->ms_refcnt) == 0) {333if (!gms->ms_released)334mmu_notifier_unregister(&gms->ms_notifier, current->mm);335kfree(gms);336STAT(gms_free);337}338}339340/*341* Setup TGH parameters. There are:342* - 24 TGH handles per GRU chiplet343* - a portion (MAX_LOCAL_TGH) of the handles are reserved for344* use by blade-local cpus345* - the rest are used by off-blade cpus. This usage is346* less frequent than blade-local usage.347*348* For now, use 16 handles for local flushes, 8 for remote flushes. If the blade349* has less tan or equal to 16 cpus, each cpu has a unique handle that it can350* use.351*/352#define MAX_LOCAL_TGH 16353354void gru_tgh_flush_init(struct gru_state *gru)355{356int cpus, shift = 0, n;357358cpus = uv_blade_nr_possible_cpus(gru->gs_blade_id);359360/* n = cpus rounded up to next power of 2 */361if (cpus) {362n = 1 << fls(cpus - 1);363364/*365* shift count for converting local cpu# to TGH index366* 0 if cpus <= MAX_LOCAL_TGH,367* 1 if cpus <= 2*MAX_LOCAL_TGH,368* etc369*/370shift = max(0, fls(n - 1) - fls(MAX_LOCAL_TGH - 1));371}372gru->gs_tgh_local_shift = shift;373374/* first starting TGH index to use for remote purges */375gru->gs_tgh_first_remote = (cpus + (1 << shift) - 1) >> shift;376377}378379380