19a0deeccSJack Steiner /* 29a0deeccSJack Steiner * SN Platform GRU Driver 39a0deeccSJack Steiner * 49a0deeccSJack Steiner * DRIVER TABLE MANAGER + GRU CONTEXT LOAD/UNLOAD 59a0deeccSJack Steiner * 69a0deeccSJack Steiner * This file is subject to the terms and conditions of the GNU General Public 79a0deeccSJack Steiner * License. See the file "COPYING" in the main directory of this archive 89a0deeccSJack Steiner * for more details. 99a0deeccSJack Steiner * 109a0deeccSJack Steiner * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved. 119a0deeccSJack Steiner */ 129a0deeccSJack Steiner 139a0deeccSJack Steiner #include <linux/kernel.h> 149a0deeccSJack Steiner #include <linux/slab.h> 159a0deeccSJack Steiner #include <linux/mm.h> 169a0deeccSJack Steiner #include <linux/spinlock.h> 179a0deeccSJack Steiner #include <linux/sched.h> 189a0deeccSJack Steiner #include <linux/device.h> 199a0deeccSJack Steiner #include <linux/list.h> 209a0deeccSJack Steiner #include <asm/uv/uv_hub.h> 219a0deeccSJack Steiner #include "gru.h" 229a0deeccSJack Steiner #include "grutables.h" 239a0deeccSJack Steiner #include "gruhandles.h" 249a0deeccSJack Steiner 259ca8e40cSJack Steiner unsigned long gru_options __read_mostly; 269a0deeccSJack Steiner 279a0deeccSJack Steiner static struct device_driver gru_driver = { 289a0deeccSJack Steiner .name = "gru" 299a0deeccSJack Steiner }; 309a0deeccSJack Steiner 319a0deeccSJack Steiner static struct device gru_device = { 32bb0dc43eSKay Sievers .init_name = "", 339a0deeccSJack Steiner .driver = &gru_driver, 349a0deeccSJack Steiner }; 359a0deeccSJack Steiner 369a0deeccSJack Steiner struct device *grudev = &gru_device; 379a0deeccSJack Steiner 389a0deeccSJack Steiner /* 399a0deeccSJack Steiner * Select a gru fault map to be used by the current cpu. Note that 409a0deeccSJack Steiner * multiple cpus may be using the same map. 419a0deeccSJack Steiner * ZZZ should "shift" be used?? Depends on HT cpu numbering 429a0deeccSJack Steiner * ZZZ should be inline but did not work on emulator 439a0deeccSJack Steiner */ 449a0deeccSJack Steiner int gru_cpu_fault_map_id(void) 459a0deeccSJack Steiner { 469a0deeccSJack Steiner return uv_blade_processor_id() % GRU_NUM_TFM; 479a0deeccSJack Steiner } 489a0deeccSJack Steiner 499a0deeccSJack Steiner /*--------- ASID Management ------------------------------------------- 509a0deeccSJack Steiner * 519a0deeccSJack Steiner * Initially, assign asids sequentially from MIN_ASID .. MAX_ASID. 529a0deeccSJack Steiner * Once MAX is reached, flush the TLB & start over. However, 539a0deeccSJack Steiner * some asids may still be in use. There won't be many (percentage wise) still 549a0deeccSJack Steiner * in use. Search active contexts & determine the value of the first 559a0deeccSJack Steiner * asid in use ("x"s below). Set "limit" to this value. 569a0deeccSJack Steiner * This defines a block of assignable asids. 579a0deeccSJack Steiner * 589a0deeccSJack Steiner * When "limit" is reached, search forward from limit+1 and determine the 599a0deeccSJack Steiner * next block of assignable asids. 609a0deeccSJack Steiner * 619a0deeccSJack Steiner * Repeat until MAX_ASID is reached, then start over again. 629a0deeccSJack Steiner * 639a0deeccSJack Steiner * Each time MAX_ASID is reached, increment the asid generation. Since 649a0deeccSJack Steiner * the search for in-use asids only checks contexts with GRUs currently 659a0deeccSJack Steiner * assigned, asids in some contexts will be missed. Prior to loading 669a0deeccSJack Steiner * a context, the asid generation of the GTS asid is rechecked. If it 679a0deeccSJack Steiner * doesn't match the current generation, a new asid will be assigned. 689a0deeccSJack Steiner * 699a0deeccSJack Steiner * 0---------------x------------x---------------------x----| 709a0deeccSJack Steiner * ^-next ^-limit ^-MAX_ASID 719a0deeccSJack Steiner * 729a0deeccSJack Steiner * All asid manipulation & context loading/unloading is protected by the 739a0deeccSJack Steiner * gs_lock. 749a0deeccSJack Steiner */ 759a0deeccSJack Steiner 769a0deeccSJack Steiner /* Hit the asid limit. Start over */ 779a0deeccSJack Steiner static int gru_wrap_asid(struct gru_state *gru) 789a0deeccSJack Steiner { 7943884604SJack Steiner gru_dbg(grudev, "gid %d\n", gru->gs_gid); 809a0deeccSJack Steiner STAT(asid_wrap); 819a0deeccSJack Steiner gru->gs_asid_gen++; 829a0deeccSJack Steiner return MIN_ASID; 839a0deeccSJack Steiner } 849a0deeccSJack Steiner 859a0deeccSJack Steiner /* Find the next chunk of unused asids */ 869a0deeccSJack Steiner static int gru_reset_asid_limit(struct gru_state *gru, int asid) 879a0deeccSJack Steiner { 889a0deeccSJack Steiner int i, gid, inuse_asid, limit; 899a0deeccSJack Steiner 9043884604SJack Steiner gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid); 919a0deeccSJack Steiner STAT(asid_next); 929a0deeccSJack Steiner limit = MAX_ASID; 939a0deeccSJack Steiner if (asid >= limit) 949a0deeccSJack Steiner asid = gru_wrap_asid(gru); 9587419412SJack Steiner gru_flush_all_tlb(gru); 969a0deeccSJack Steiner gid = gru->gs_gid; 979a0deeccSJack Steiner again: 989a0deeccSJack Steiner for (i = 0; i < GRU_NUM_CCH; i++) { 99836ce679SJack Steiner if (!gru->gs_gts[i] || is_kernel_context(gru->gs_gts[i])) 1009a0deeccSJack Steiner continue; 1019a0deeccSJack Steiner inuse_asid = gru->gs_gts[i]->ts_gms->ms_asids[gid].mt_asid; 10243884604SJack Steiner gru_dbg(grudev, "gid %d, gts %p, gms %p, inuse 0x%x, cxt %d\n", 10343884604SJack Steiner gru->gs_gid, gru->gs_gts[i], gru->gs_gts[i]->ts_gms, 10443884604SJack Steiner inuse_asid, i); 1059a0deeccSJack Steiner if (inuse_asid == asid) { 1069a0deeccSJack Steiner asid += ASID_INC; 1079a0deeccSJack Steiner if (asid >= limit) { 1089a0deeccSJack Steiner /* 1099a0deeccSJack Steiner * empty range: reset the range limit and 1109a0deeccSJack Steiner * start over 1119a0deeccSJack Steiner */ 1129a0deeccSJack Steiner limit = MAX_ASID; 1139a0deeccSJack Steiner if (asid >= MAX_ASID) 1149a0deeccSJack Steiner asid = gru_wrap_asid(gru); 1159a0deeccSJack Steiner goto again; 1169a0deeccSJack Steiner } 1179a0deeccSJack Steiner } 1189a0deeccSJack Steiner 1199a0deeccSJack Steiner if ((inuse_asid > asid) && (inuse_asid < limit)) 1209a0deeccSJack Steiner limit = inuse_asid; 1219a0deeccSJack Steiner } 1229a0deeccSJack Steiner gru->gs_asid_limit = limit; 1239a0deeccSJack Steiner gru->gs_asid = asid; 12443884604SJack Steiner gru_dbg(grudev, "gid %d, new asid 0x%x, new_limit 0x%x\n", gru->gs_gid, 12543884604SJack Steiner asid, limit); 1269a0deeccSJack Steiner return asid; 1279a0deeccSJack Steiner } 1289a0deeccSJack Steiner 1299a0deeccSJack Steiner /* Assign a new ASID to a thread context. */ 1309a0deeccSJack Steiner static int gru_assign_asid(struct gru_state *gru) 1319a0deeccSJack Steiner { 1329a0deeccSJack Steiner int asid; 1339a0deeccSJack Steiner 1349a0deeccSJack Steiner gru->gs_asid += ASID_INC; 1359a0deeccSJack Steiner asid = gru->gs_asid; 1369a0deeccSJack Steiner if (asid >= gru->gs_asid_limit) 1379a0deeccSJack Steiner asid = gru_reset_asid_limit(gru, asid); 1389a0deeccSJack Steiner 13943884604SJack Steiner gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid); 1409a0deeccSJack Steiner return asid; 1419a0deeccSJack Steiner } 1429a0deeccSJack Steiner 1439a0deeccSJack Steiner /* 1449a0deeccSJack Steiner * Clear n bits in a word. Return a word indicating the bits that were cleared. 1459a0deeccSJack Steiner * Optionally, build an array of chars that contain the bit numbers allocated. 1469a0deeccSJack Steiner */ 1479a0deeccSJack Steiner static unsigned long reserve_resources(unsigned long *p, int n, int mmax, 1489a0deeccSJack Steiner char *idx) 1499a0deeccSJack Steiner { 1509a0deeccSJack Steiner unsigned long bits = 0; 1519a0deeccSJack Steiner int i; 1529a0deeccSJack Steiner 1533eac2e95SJack Steiner while (n--) { 1549a0deeccSJack Steiner i = find_first_bit(p, mmax); 1559a0deeccSJack Steiner if (i == mmax) 1569a0deeccSJack Steiner BUG(); 1579a0deeccSJack Steiner __clear_bit(i, p); 1589a0deeccSJack Steiner __set_bit(i, &bits); 1599a0deeccSJack Steiner if (idx) 1609a0deeccSJack Steiner *idx++ = i; 1613eac2e95SJack Steiner } 1629a0deeccSJack Steiner return bits; 1639a0deeccSJack Steiner } 1649a0deeccSJack Steiner 1659ca8e40cSJack Steiner unsigned long gru_reserve_cb_resources(struct gru_state *gru, int cbr_au_count, 1669a0deeccSJack Steiner char *cbmap) 1679a0deeccSJack Steiner { 1689a0deeccSJack Steiner return reserve_resources(&gru->gs_cbr_map, cbr_au_count, GRU_CBR_AU, 1699a0deeccSJack Steiner cbmap); 1709a0deeccSJack Steiner } 1719a0deeccSJack Steiner 1729ca8e40cSJack Steiner unsigned long gru_reserve_ds_resources(struct gru_state *gru, int dsr_au_count, 1739a0deeccSJack Steiner char *dsmap) 1749a0deeccSJack Steiner { 1759a0deeccSJack Steiner return reserve_resources(&gru->gs_dsr_map, dsr_au_count, GRU_DSR_AU, 1769a0deeccSJack Steiner dsmap); 1779a0deeccSJack Steiner } 1789a0deeccSJack Steiner 1799a0deeccSJack Steiner static void reserve_gru_resources(struct gru_state *gru, 1809a0deeccSJack Steiner struct gru_thread_state *gts) 1819a0deeccSJack Steiner { 1829a0deeccSJack Steiner gru->gs_active_contexts++; 1839a0deeccSJack Steiner gts->ts_cbr_map = 1849ca8e40cSJack Steiner gru_reserve_cb_resources(gru, gts->ts_cbr_au_count, 1859a0deeccSJack Steiner gts->ts_cbr_idx); 1869a0deeccSJack Steiner gts->ts_dsr_map = 1879ca8e40cSJack Steiner gru_reserve_ds_resources(gru, gts->ts_dsr_au_count, NULL); 1889a0deeccSJack Steiner } 1899a0deeccSJack Steiner 1909a0deeccSJack Steiner static void free_gru_resources(struct gru_state *gru, 1919a0deeccSJack Steiner struct gru_thread_state *gts) 1929a0deeccSJack Steiner { 1939a0deeccSJack Steiner gru->gs_active_contexts--; 1949a0deeccSJack Steiner gru->gs_cbr_map |= gts->ts_cbr_map; 1959a0deeccSJack Steiner gru->gs_dsr_map |= gts->ts_dsr_map; 1969a0deeccSJack Steiner } 1979a0deeccSJack Steiner 1989a0deeccSJack Steiner /* 1999a0deeccSJack Steiner * Check if a GRU has sufficient free resources to satisfy an allocation 2009a0deeccSJack Steiner * request. Note: GRU locks may or may not be held when this is called. If 2019a0deeccSJack Steiner * not held, recheck after acquiring the appropriate locks. 2029a0deeccSJack Steiner * 2039a0deeccSJack Steiner * Returns 1 if sufficient resources, 0 if not 2049a0deeccSJack Steiner */ 2059a0deeccSJack Steiner static int check_gru_resources(struct gru_state *gru, int cbr_au_count, 2069a0deeccSJack Steiner int dsr_au_count, int max_active_contexts) 2079a0deeccSJack Steiner { 2089a0deeccSJack Steiner return hweight64(gru->gs_cbr_map) >= cbr_au_count 2099a0deeccSJack Steiner && hweight64(gru->gs_dsr_map) >= dsr_au_count 2109a0deeccSJack Steiner && gru->gs_active_contexts < max_active_contexts; 2119a0deeccSJack Steiner } 2129a0deeccSJack Steiner 2139a0deeccSJack Steiner /* 2149a0deeccSJack Steiner * TLB manangment requires tracking all GRU chiplets that have loaded a GSEG 2159a0deeccSJack Steiner * context. 2169a0deeccSJack Steiner */ 21743884604SJack Steiner static int gru_load_mm_tracker(struct gru_state *gru, 21843884604SJack Steiner struct gru_thread_state *gts) 2199a0deeccSJack Steiner { 22043884604SJack Steiner struct gru_mm_struct *gms = gts->ts_gms; 2219a0deeccSJack Steiner struct gru_mm_tracker *asids = &gms->ms_asids[gru->gs_gid]; 22243884604SJack Steiner unsigned short ctxbitmap = (1 << gts->ts_ctxnum); 2239a0deeccSJack Steiner int asid; 2249a0deeccSJack Steiner 2259a0deeccSJack Steiner spin_lock(&gms->ms_asid_lock); 2269a0deeccSJack Steiner asid = asids->mt_asid; 2279a0deeccSJack Steiner 22887419412SJack Steiner spin_lock(&gru->gs_asid_lock); 22987419412SJack Steiner if (asid == 0 || (asids->mt_ctxbitmap == 0 && asids->mt_asid_gen != 23087419412SJack Steiner gru->gs_asid_gen)) { 2319a0deeccSJack Steiner asid = gru_assign_asid(gru); 2329a0deeccSJack Steiner asids->mt_asid = asid; 2339a0deeccSJack Steiner asids->mt_asid_gen = gru->gs_asid_gen; 2349a0deeccSJack Steiner STAT(asid_new); 2359a0deeccSJack Steiner } else { 2369a0deeccSJack Steiner STAT(asid_reuse); 2379a0deeccSJack Steiner } 23887419412SJack Steiner spin_unlock(&gru->gs_asid_lock); 2399a0deeccSJack Steiner 2409a0deeccSJack Steiner BUG_ON(asids->mt_ctxbitmap & ctxbitmap); 2419a0deeccSJack Steiner asids->mt_ctxbitmap |= ctxbitmap; 2429a0deeccSJack Steiner if (!test_bit(gru->gs_gid, gms->ms_asidmap)) 2439a0deeccSJack Steiner __set_bit(gru->gs_gid, gms->ms_asidmap); 2449a0deeccSJack Steiner spin_unlock(&gms->ms_asid_lock); 2459a0deeccSJack Steiner 2469a0deeccSJack Steiner gru_dbg(grudev, 24743884604SJack Steiner "gid %d, gts %p, gms %p, ctxnum %d, asid 0x%x, asidmap 0x%lx\n", 24843884604SJack Steiner gru->gs_gid, gts, gms, gts->ts_ctxnum, asid, 24943884604SJack Steiner gms->ms_asidmap[0]); 2509a0deeccSJack Steiner return asid; 2519a0deeccSJack Steiner } 2529a0deeccSJack Steiner 2539a0deeccSJack Steiner static void gru_unload_mm_tracker(struct gru_state *gru, 25443884604SJack Steiner struct gru_thread_state *gts) 2559a0deeccSJack Steiner { 25643884604SJack Steiner struct gru_mm_struct *gms = gts->ts_gms; 2579a0deeccSJack Steiner struct gru_mm_tracker *asids; 2589a0deeccSJack Steiner unsigned short ctxbitmap; 2599a0deeccSJack Steiner 2609a0deeccSJack Steiner asids = &gms->ms_asids[gru->gs_gid]; 26143884604SJack Steiner ctxbitmap = (1 << gts->ts_ctxnum); 2629a0deeccSJack Steiner spin_lock(&gms->ms_asid_lock); 26387419412SJack Steiner spin_lock(&gru->gs_asid_lock); 2649a0deeccSJack Steiner BUG_ON((asids->mt_ctxbitmap & ctxbitmap) != ctxbitmap); 2659a0deeccSJack Steiner asids->mt_ctxbitmap ^= ctxbitmap; 26643884604SJack Steiner gru_dbg(grudev, "gid %d, gts %p, gms %p, ctxnum 0x%d, asidmap 0x%lx\n", 26743884604SJack Steiner gru->gs_gid, gts, gms, gts->ts_ctxnum, gms->ms_asidmap[0]); 26887419412SJack Steiner spin_unlock(&gru->gs_asid_lock); 2699a0deeccSJack Steiner spin_unlock(&gms->ms_asid_lock); 2709a0deeccSJack Steiner } 2719a0deeccSJack Steiner 2729a0deeccSJack Steiner /* 2739a0deeccSJack Steiner * Decrement the reference count on a GTS structure. Free the structure 2749a0deeccSJack Steiner * if the reference count goes to zero. 2759a0deeccSJack Steiner */ 2769a0deeccSJack Steiner void gts_drop(struct gru_thread_state *gts) 2779a0deeccSJack Steiner { 2789a0deeccSJack Steiner if (gts && atomic_dec_return(>s->ts_refcnt) == 0) { 2799a0deeccSJack Steiner gru_drop_mmu_notifier(gts->ts_gms); 2809a0deeccSJack Steiner kfree(gts); 2819a0deeccSJack Steiner STAT(gts_free); 2829a0deeccSJack Steiner } 2839a0deeccSJack Steiner } 2849a0deeccSJack Steiner 2859a0deeccSJack Steiner /* 2869a0deeccSJack Steiner * Locate the GTS structure for the current thread. 2879a0deeccSJack Steiner */ 2889a0deeccSJack Steiner static struct gru_thread_state *gru_find_current_gts_nolock(struct gru_vma_data 2899a0deeccSJack Steiner *vdata, int tsid) 2909a0deeccSJack Steiner { 2919a0deeccSJack Steiner struct gru_thread_state *gts; 2929a0deeccSJack Steiner 2939a0deeccSJack Steiner list_for_each_entry(gts, &vdata->vd_head, ts_next) 2949a0deeccSJack Steiner if (gts->ts_tsid == tsid) 2959a0deeccSJack Steiner return gts; 2969a0deeccSJack Steiner return NULL; 2979a0deeccSJack Steiner } 2989a0deeccSJack Steiner 2999a0deeccSJack Steiner /* 3009a0deeccSJack Steiner * Allocate a thread state structure. 3019a0deeccSJack Steiner */ 302364b76dfSJack Steiner struct gru_thread_state *gru_alloc_gts(struct vm_area_struct *vma, 303364b76dfSJack Steiner int cbr_au_count, int dsr_au_count, int options, int tsid) 3049a0deeccSJack Steiner { 3059a0deeccSJack Steiner struct gru_thread_state *gts; 3069a0deeccSJack Steiner int bytes; 3079a0deeccSJack Steiner 308364b76dfSJack Steiner bytes = DSR_BYTES(dsr_au_count) + CBR_BYTES(cbr_au_count); 3099a0deeccSJack Steiner bytes += sizeof(struct gru_thread_state); 310*940229b9SJack Steiner gts = kmalloc(bytes, GFP_KERNEL); 3119a0deeccSJack Steiner if (!gts) 3129a0deeccSJack Steiner return NULL; 3139a0deeccSJack Steiner 3149a0deeccSJack Steiner STAT(gts_alloc); 315*940229b9SJack Steiner memset(gts, 0, sizeof(struct gru_thread_state)); /* zero out header */ 3169a0deeccSJack Steiner atomic_set(>s->ts_refcnt, 1); 3179a0deeccSJack Steiner mutex_init(>s->ts_ctxlock); 318364b76dfSJack Steiner gts->ts_cbr_au_count = cbr_au_count; 319364b76dfSJack Steiner gts->ts_dsr_au_count = dsr_au_count; 320364b76dfSJack Steiner gts->ts_user_options = options; 3219a0deeccSJack Steiner gts->ts_tsid = tsid; 3229a0deeccSJack Steiner gts->ts_ctxnum = NULLCTX; 323364b76dfSJack Steiner gts->ts_tlb_int_select = -1; 324364b76dfSJack Steiner gts->ts_sizeavail = GRU_SIZEAVAIL(PAGE_SHIFT); 325364b76dfSJack Steiner if (vma) { 3269a0deeccSJack Steiner gts->ts_mm = current->mm; 3279a0deeccSJack Steiner gts->ts_vma = vma; 3289a0deeccSJack Steiner gts->ts_gms = gru_register_mmu_notifier(); 3299a0deeccSJack Steiner if (!gts->ts_gms) 3309a0deeccSJack Steiner goto err; 331364b76dfSJack Steiner } 3329a0deeccSJack Steiner 333364b76dfSJack Steiner gru_dbg(grudev, "alloc gts %p\n", gts); 3349a0deeccSJack Steiner return gts; 3359a0deeccSJack Steiner 3369a0deeccSJack Steiner err: 3379a0deeccSJack Steiner gts_drop(gts); 3389a0deeccSJack Steiner return NULL; 3399a0deeccSJack Steiner } 3409a0deeccSJack Steiner 3419a0deeccSJack Steiner /* 3429a0deeccSJack Steiner * Allocate a vma private data structure. 3439a0deeccSJack Steiner */ 3449a0deeccSJack Steiner struct gru_vma_data *gru_alloc_vma_data(struct vm_area_struct *vma, int tsid) 3459a0deeccSJack Steiner { 3469a0deeccSJack Steiner struct gru_vma_data *vdata = NULL; 3479a0deeccSJack Steiner 3489a0deeccSJack Steiner vdata = kmalloc(sizeof(*vdata), GFP_KERNEL); 3499a0deeccSJack Steiner if (!vdata) 3509a0deeccSJack Steiner return NULL; 3519a0deeccSJack Steiner 3529a0deeccSJack Steiner INIT_LIST_HEAD(&vdata->vd_head); 3539a0deeccSJack Steiner spin_lock_init(&vdata->vd_lock); 3549a0deeccSJack Steiner gru_dbg(grudev, "alloc vdata %p\n", vdata); 3559a0deeccSJack Steiner return vdata; 3569a0deeccSJack Steiner } 3579a0deeccSJack Steiner 3589a0deeccSJack Steiner /* 3599a0deeccSJack Steiner * Find the thread state structure for the current thread. 3609a0deeccSJack Steiner */ 3619a0deeccSJack Steiner struct gru_thread_state *gru_find_thread_state(struct vm_area_struct *vma, 3629a0deeccSJack Steiner int tsid) 3639a0deeccSJack Steiner { 3649a0deeccSJack Steiner struct gru_vma_data *vdata = vma->vm_private_data; 3659a0deeccSJack Steiner struct gru_thread_state *gts; 3669a0deeccSJack Steiner 3679a0deeccSJack Steiner spin_lock(&vdata->vd_lock); 3689a0deeccSJack Steiner gts = gru_find_current_gts_nolock(vdata, tsid); 3699a0deeccSJack Steiner spin_unlock(&vdata->vd_lock); 3709a0deeccSJack Steiner gru_dbg(grudev, "vma %p, gts %p\n", vma, gts); 3719a0deeccSJack Steiner return gts; 3729a0deeccSJack Steiner } 3739a0deeccSJack Steiner 3749a0deeccSJack Steiner /* 3759a0deeccSJack Steiner * Allocate a new thread state for a GSEG. Note that races may allow 3769a0deeccSJack Steiner * another thread to race to create a gts. 3779a0deeccSJack Steiner */ 3789a0deeccSJack Steiner struct gru_thread_state *gru_alloc_thread_state(struct vm_area_struct *vma, 3799a0deeccSJack Steiner int tsid) 3809a0deeccSJack Steiner { 3819a0deeccSJack Steiner struct gru_vma_data *vdata = vma->vm_private_data; 3829a0deeccSJack Steiner struct gru_thread_state *gts, *ngts; 3839a0deeccSJack Steiner 384364b76dfSJack Steiner gts = gru_alloc_gts(vma, vdata->vd_cbr_au_count, vdata->vd_dsr_au_count, 385364b76dfSJack Steiner vdata->vd_user_options, tsid); 3869a0deeccSJack Steiner if (!gts) 3879a0deeccSJack Steiner return NULL; 3889a0deeccSJack Steiner 3899a0deeccSJack Steiner spin_lock(&vdata->vd_lock); 3909a0deeccSJack Steiner ngts = gru_find_current_gts_nolock(vdata, tsid); 3919a0deeccSJack Steiner if (ngts) { 3929a0deeccSJack Steiner gts_drop(gts); 3939a0deeccSJack Steiner gts = ngts; 3949a0deeccSJack Steiner STAT(gts_double_allocate); 3959a0deeccSJack Steiner } else { 3969a0deeccSJack Steiner list_add(>s->ts_next, &vdata->vd_head); 3979a0deeccSJack Steiner } 3989a0deeccSJack Steiner spin_unlock(&vdata->vd_lock); 3999a0deeccSJack Steiner gru_dbg(grudev, "vma %p, gts %p\n", vma, gts); 4009a0deeccSJack Steiner return gts; 4019a0deeccSJack Steiner } 4029a0deeccSJack Steiner 4039a0deeccSJack Steiner /* 4049a0deeccSJack Steiner * Free the GRU context assigned to the thread state. 4059a0deeccSJack Steiner */ 4069a0deeccSJack Steiner static void gru_free_gru_context(struct gru_thread_state *gts) 4079a0deeccSJack Steiner { 4089a0deeccSJack Steiner struct gru_state *gru; 4099a0deeccSJack Steiner 4109a0deeccSJack Steiner gru = gts->ts_gru; 41143884604SJack Steiner gru_dbg(grudev, "gts %p, gid %d\n", gts, gru->gs_gid); 4129a0deeccSJack Steiner 4139a0deeccSJack Steiner spin_lock(&gru->gs_lock); 4149a0deeccSJack Steiner gru->gs_gts[gts->ts_ctxnum] = NULL; 4159a0deeccSJack Steiner free_gru_resources(gru, gts); 4169a0deeccSJack Steiner BUG_ON(test_bit(gts->ts_ctxnum, &gru->gs_context_map) == 0); 4179a0deeccSJack Steiner __clear_bit(gts->ts_ctxnum, &gru->gs_context_map); 4189a0deeccSJack Steiner gts->ts_ctxnum = NULLCTX; 4199a0deeccSJack Steiner gts->ts_gru = NULL; 42087419412SJack Steiner gts->ts_blade = -1; 4219a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 4229a0deeccSJack Steiner 4239a0deeccSJack Steiner gts_drop(gts); 4249a0deeccSJack Steiner STAT(free_context); 4259a0deeccSJack Steiner } 4269a0deeccSJack Steiner 4279a0deeccSJack Steiner /* 4289a0deeccSJack Steiner * Prefetching cachelines help hardware performance. 4299ca8e40cSJack Steiner * (Strictly a performance enhancement. Not functionally required). 4309a0deeccSJack Steiner */ 4319a0deeccSJack Steiner static void prefetch_data(void *p, int num, int stride) 4329a0deeccSJack Steiner { 4339a0deeccSJack Steiner while (num-- > 0) { 4349a0deeccSJack Steiner prefetchw(p); 4359a0deeccSJack Steiner p += stride; 4369a0deeccSJack Steiner } 4379a0deeccSJack Steiner } 4389a0deeccSJack Steiner 4399a0deeccSJack Steiner static inline long gru_copy_handle(void *d, void *s) 4409a0deeccSJack Steiner { 4419a0deeccSJack Steiner memcpy(d, s, GRU_HANDLE_BYTES); 4429a0deeccSJack Steiner return GRU_HANDLE_BYTES; 4439a0deeccSJack Steiner } 4449a0deeccSJack Steiner 445fe5bb6b0SJack Steiner static void gru_prefetch_context(void *gseg, void *cb, void *cbe, 446fe5bb6b0SJack Steiner unsigned long cbrmap, unsigned long length) 447923f7f69SJack Steiner { 448923f7f69SJack Steiner int i, scr; 449923f7f69SJack Steiner 450923f7f69SJack Steiner prefetch_data(gseg + GRU_DS_BASE, length / GRU_CACHE_LINE_BYTES, 451923f7f69SJack Steiner GRU_CACHE_LINE_BYTES); 452923f7f69SJack Steiner 453923f7f69SJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) { 454923f7f69SJack Steiner prefetch_data(cb, 1, GRU_CACHE_LINE_BYTES); 455923f7f69SJack Steiner prefetch_data(cbe + i * GRU_HANDLE_STRIDE, 1, 456923f7f69SJack Steiner GRU_CACHE_LINE_BYTES); 457923f7f69SJack Steiner cb += GRU_HANDLE_STRIDE; 458923f7f69SJack Steiner } 459923f7f69SJack Steiner } 460923f7f69SJack Steiner 4619a0deeccSJack Steiner static void gru_load_context_data(void *save, void *grubase, int ctxnum, 462*940229b9SJack Steiner unsigned long cbrmap, unsigned long dsrmap, 463*940229b9SJack Steiner int data_valid) 4649a0deeccSJack Steiner { 4659a0deeccSJack Steiner void *gseg, *cb, *cbe; 4669a0deeccSJack Steiner unsigned long length; 4679a0deeccSJack Steiner int i, scr; 4689a0deeccSJack Steiner 4699a0deeccSJack Steiner gseg = grubase + ctxnum * GRU_GSEG_STRIDE; 4709a0deeccSJack Steiner cb = gseg + GRU_CB_BASE; 4719a0deeccSJack Steiner cbe = grubase + GRU_CBE_BASE; 472923f7f69SJack Steiner length = hweight64(dsrmap) * GRU_DSR_AU_BYTES; 473923f7f69SJack Steiner gru_prefetch_context(gseg, cb, cbe, cbrmap, length); 4749a0deeccSJack Steiner 4759a0deeccSJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) { 476*940229b9SJack Steiner if (data_valid) { 4779a0deeccSJack Steiner save += gru_copy_handle(cb, save); 478*940229b9SJack Steiner save += gru_copy_handle(cbe + i * GRU_HANDLE_STRIDE, 479*940229b9SJack Steiner save); 480*940229b9SJack Steiner } else { 481*940229b9SJack Steiner memset(cb, 0, GRU_CACHE_LINE_BYTES); 482*940229b9SJack Steiner memset(cbe + i * GRU_HANDLE_STRIDE, 0, 483*940229b9SJack Steiner GRU_CACHE_LINE_BYTES); 484*940229b9SJack Steiner } 4859a0deeccSJack Steiner cb += GRU_HANDLE_STRIDE; 4869a0deeccSJack Steiner } 4879a0deeccSJack Steiner 488*940229b9SJack Steiner if (data_valid) 4899a0deeccSJack Steiner memcpy(gseg + GRU_DS_BASE, save, length); 490*940229b9SJack Steiner else 491*940229b9SJack Steiner memset(gseg + GRU_DS_BASE, 0, length); 4929a0deeccSJack Steiner } 4939a0deeccSJack Steiner 4949a0deeccSJack Steiner static void gru_unload_context_data(void *save, void *grubase, int ctxnum, 4959a0deeccSJack Steiner unsigned long cbrmap, unsigned long dsrmap) 4969a0deeccSJack Steiner { 4979a0deeccSJack Steiner void *gseg, *cb, *cbe; 4989a0deeccSJack Steiner unsigned long length; 4999a0deeccSJack Steiner int i, scr; 5009a0deeccSJack Steiner 5019a0deeccSJack Steiner gseg = grubase + ctxnum * GRU_GSEG_STRIDE; 5029a0deeccSJack Steiner cb = gseg + GRU_CB_BASE; 5039a0deeccSJack Steiner cbe = grubase + GRU_CBE_BASE; 504923f7f69SJack Steiner length = hweight64(dsrmap) * GRU_DSR_AU_BYTES; 505923f7f69SJack Steiner gru_prefetch_context(gseg, cb, cbe, cbrmap, length); 506923f7f69SJack Steiner 5079a0deeccSJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) { 5089a0deeccSJack Steiner save += gru_copy_handle(save, cb); 5099a0deeccSJack Steiner save += gru_copy_handle(save, cbe + i * GRU_HANDLE_STRIDE); 5109a0deeccSJack Steiner cb += GRU_HANDLE_STRIDE; 5119a0deeccSJack Steiner } 5129a0deeccSJack Steiner memcpy(save, gseg + GRU_DS_BASE, length); 5139a0deeccSJack Steiner } 5149a0deeccSJack Steiner 5159a0deeccSJack Steiner void gru_unload_context(struct gru_thread_state *gts, int savestate) 5169a0deeccSJack Steiner { 5179a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru; 5189a0deeccSJack Steiner struct gru_context_configuration_handle *cch; 5199a0deeccSJack Steiner int ctxnum = gts->ts_ctxnum; 5209a0deeccSJack Steiner 521836ce679SJack Steiner if (!is_kernel_context(gts)) 5229a0deeccSJack Steiner zap_vma_ptes(gts->ts_vma, UGRUADDR(gts), GRU_GSEG_PAGESIZE); 5239a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum); 5249a0deeccSJack Steiner 52543884604SJack Steiner gru_dbg(grudev, "gts %p\n", gts); 5269a0deeccSJack Steiner lock_cch_handle(cch); 5279a0deeccSJack Steiner if (cch_interrupt_sync(cch)) 5289a0deeccSJack Steiner BUG(); 5299a0deeccSJack Steiner 530836ce679SJack Steiner if (!is_kernel_context(gts)) 53143884604SJack Steiner gru_unload_mm_tracker(gru, gts); 532*940229b9SJack Steiner if (savestate) { 5339a0deeccSJack Steiner gru_unload_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, 5349a0deeccSJack Steiner ctxnum, gts->ts_cbr_map, 5359a0deeccSJack Steiner gts->ts_dsr_map); 536*940229b9SJack Steiner gts->ts_data_valid = 1; 537*940229b9SJack Steiner } 5389a0deeccSJack Steiner 5399a0deeccSJack Steiner if (cch_deallocate(cch)) 5409a0deeccSJack Steiner BUG(); 5419a0deeccSJack Steiner gts->ts_force_unload = 0; /* ts_force_unload locked by CCH lock */ 5429a0deeccSJack Steiner unlock_cch_handle(cch); 5439a0deeccSJack Steiner 5449a0deeccSJack Steiner gru_free_gru_context(gts); 5459a0deeccSJack Steiner } 5469a0deeccSJack Steiner 5479a0deeccSJack Steiner /* 5489a0deeccSJack Steiner * Load a GRU context by copying it from the thread data structure in memory 5499a0deeccSJack Steiner * to the GRU. 5509a0deeccSJack Steiner */ 551d57c82b1SJack Steiner void gru_load_context(struct gru_thread_state *gts) 5529a0deeccSJack Steiner { 5539a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru; 5549a0deeccSJack Steiner struct gru_context_configuration_handle *cch; 5556e910074SJack Steiner int i, err, asid, ctxnum = gts->ts_ctxnum; 5569a0deeccSJack Steiner 5579a0deeccSJack Steiner gru_dbg(grudev, "gts %p\n", gts); 5589a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum); 5599a0deeccSJack Steiner 5609a0deeccSJack Steiner lock_cch_handle(cch); 5619a0deeccSJack Steiner cch->tfm_fault_bit_enable = 5629a0deeccSJack Steiner (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL 5639a0deeccSJack Steiner || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR); 5649a0deeccSJack Steiner cch->tlb_int_enable = (gts->ts_user_options == GRU_OPT_MISS_FMM_INTR); 5659a0deeccSJack Steiner if (cch->tlb_int_enable) { 5669a0deeccSJack Steiner gts->ts_tlb_int_select = gru_cpu_fault_map_id(); 5679a0deeccSJack Steiner cch->tlb_int_select = gts->ts_tlb_int_select; 5689a0deeccSJack Steiner } 5699a0deeccSJack Steiner cch->tfm_done_bit_enable = 0; 5706e910074SJack Steiner cch->dsr_allocation_map = gts->ts_dsr_map; 5716e910074SJack Steiner cch->cbr_allocation_map = gts->ts_cbr_map; 572836ce679SJack Steiner 573836ce679SJack Steiner if (is_kernel_context(gts)) { 574836ce679SJack Steiner cch->unmap_enable = 1; 575836ce679SJack Steiner } else { 5766e910074SJack Steiner cch->unmap_enable = 0; 577836ce679SJack Steiner asid = gru_load_mm_tracker(gru, gts); 5786e910074SJack Steiner for (i = 0; i < 8; i++) { 5796e910074SJack Steiner cch->asid[i] = asid + i; 5806e910074SJack Steiner cch->sizeavail[i] = gts->ts_sizeavail; 5816e910074SJack Steiner } 582836ce679SJack Steiner } 5836e910074SJack Steiner 5846e910074SJack Steiner err = cch_allocate(cch); 5859a0deeccSJack Steiner if (err) { 5869a0deeccSJack Steiner gru_dbg(grudev, 5879a0deeccSJack Steiner "err %d: cch %p, gts %p, cbr 0x%lx, dsr 0x%lx\n", 5889a0deeccSJack Steiner err, cch, gts, gts->ts_cbr_map, gts->ts_dsr_map); 5899a0deeccSJack Steiner BUG(); 5909a0deeccSJack Steiner } 5919a0deeccSJack Steiner 5929a0deeccSJack Steiner gru_load_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, ctxnum, 593*940229b9SJack Steiner gts->ts_cbr_map, gts->ts_dsr_map, gts->ts_data_valid); 5949a0deeccSJack Steiner 5959a0deeccSJack Steiner if (cch_start(cch)) 5969a0deeccSJack Steiner BUG(); 5979a0deeccSJack Steiner unlock_cch_handle(cch); 5989a0deeccSJack Steiner } 5999a0deeccSJack Steiner 6009a0deeccSJack Steiner /* 6019a0deeccSJack Steiner * Update fields in an active CCH: 6029a0deeccSJack Steiner * - retarget interrupts on local blade 6037b8274e9SJack Steiner * - update sizeavail mask 6049a0deeccSJack Steiner * - force a delayed context unload by clearing the CCH asids. This 6059a0deeccSJack Steiner * forces TLB misses for new GRU instructions. The context is unloaded 6069a0deeccSJack Steiner * when the next TLB miss occurs. 6079a0deeccSJack Steiner */ 6087b8274e9SJack Steiner int gru_update_cch(struct gru_thread_state *gts, int force_unload) 6099a0deeccSJack Steiner { 6109a0deeccSJack Steiner struct gru_context_configuration_handle *cch; 6119a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru; 6129a0deeccSJack Steiner int i, ctxnum = gts->ts_ctxnum, ret = 0; 6139a0deeccSJack Steiner 6149a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum); 6159a0deeccSJack Steiner 6169a0deeccSJack Steiner lock_cch_handle(cch); 6179a0deeccSJack Steiner if (cch->state == CCHSTATE_ACTIVE) { 6189a0deeccSJack Steiner if (gru->gs_gts[gts->ts_ctxnum] != gts) 6199a0deeccSJack Steiner goto exit; 6209a0deeccSJack Steiner if (cch_interrupt(cch)) 6219a0deeccSJack Steiner BUG(); 6227b8274e9SJack Steiner if (!force_unload) { 6237b8274e9SJack Steiner for (i = 0; i < 8; i++) 6247b8274e9SJack Steiner cch->sizeavail[i] = gts->ts_sizeavail; 6257b8274e9SJack Steiner gts->ts_tlb_int_select = gru_cpu_fault_map_id(); 6267b8274e9SJack Steiner cch->tlb_int_select = gru_cpu_fault_map_id(); 627cd1334f0SJack Steiner cch->tfm_fault_bit_enable = 628cd1334f0SJack Steiner (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL 629cd1334f0SJack Steiner || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR); 6309a0deeccSJack Steiner } else { 6319a0deeccSJack Steiner for (i = 0; i < 8; i++) 6329a0deeccSJack Steiner cch->asid[i] = 0; 6339a0deeccSJack Steiner cch->tfm_fault_bit_enable = 0; 6349a0deeccSJack Steiner cch->tlb_int_enable = 0; 6359a0deeccSJack Steiner gts->ts_force_unload = 1; 6369a0deeccSJack Steiner } 6379a0deeccSJack Steiner if (cch_start(cch)) 6389a0deeccSJack Steiner BUG(); 6399a0deeccSJack Steiner ret = 1; 6409a0deeccSJack Steiner } 6419a0deeccSJack Steiner exit: 6429a0deeccSJack Steiner unlock_cch_handle(cch); 6439a0deeccSJack Steiner return ret; 6449a0deeccSJack Steiner } 6459a0deeccSJack Steiner 6469a0deeccSJack Steiner /* 6479a0deeccSJack Steiner * Update CCH tlb interrupt select. Required when all the following is true: 6489a0deeccSJack Steiner * - task's GRU context is loaded into a GRU 6499a0deeccSJack Steiner * - task is using interrupt notification for TLB faults 6509a0deeccSJack Steiner * - task has migrated to a different cpu on the same blade where 6519a0deeccSJack Steiner * it was previously running. 6529a0deeccSJack Steiner */ 6539a0deeccSJack Steiner static int gru_retarget_intr(struct gru_thread_state *gts) 6549a0deeccSJack Steiner { 6559a0deeccSJack Steiner if (gts->ts_tlb_int_select < 0 6569a0deeccSJack Steiner || gts->ts_tlb_int_select == gru_cpu_fault_map_id()) 6579a0deeccSJack Steiner return 0; 6589a0deeccSJack Steiner 6599a0deeccSJack Steiner gru_dbg(grudev, "retarget from %d to %d\n", gts->ts_tlb_int_select, 6609a0deeccSJack Steiner gru_cpu_fault_map_id()); 6617b8274e9SJack Steiner return gru_update_cch(gts, 0); 6629a0deeccSJack Steiner } 6639a0deeccSJack Steiner 6649a0deeccSJack Steiner 6659a0deeccSJack Steiner /* 6669a0deeccSJack Steiner * Insufficient GRU resources available on the local blade. Steal a context from 6679a0deeccSJack Steiner * a process. This is a hack until a _real_ resource scheduler is written.... 6689a0deeccSJack Steiner */ 6699a0deeccSJack Steiner #define next_ctxnum(n) ((n) < GRU_NUM_CCH - 2 ? (n) + 1 : 0) 6709a0deeccSJack Steiner #define next_gru(b, g) (((g) < &(b)->bs_grus[GRU_CHIPLETS_PER_BLADE - 1]) ? \ 6719a0deeccSJack Steiner ((g)+1) : &(b)->bs_grus[0]) 6729a0deeccSJack Steiner 673836ce679SJack Steiner static int is_gts_stealable(struct gru_thread_state *gts, 674836ce679SJack Steiner struct gru_blade_state *bs) 675836ce679SJack Steiner { 676836ce679SJack Steiner if (is_kernel_context(gts)) 677836ce679SJack Steiner return down_write_trylock(&bs->bs_kgts_sema); 678836ce679SJack Steiner else 679836ce679SJack Steiner return mutex_trylock(>s->ts_ctxlock); 680836ce679SJack Steiner } 681836ce679SJack Steiner 682836ce679SJack Steiner static void gts_stolen(struct gru_thread_state *gts, 683836ce679SJack Steiner struct gru_blade_state *bs) 684836ce679SJack Steiner { 685836ce679SJack Steiner if (is_kernel_context(gts)) { 686836ce679SJack Steiner up_write(&bs->bs_kgts_sema); 687836ce679SJack Steiner STAT(steal_kernel_context); 688836ce679SJack Steiner } else { 689836ce679SJack Steiner mutex_unlock(>s->ts_ctxlock); 690836ce679SJack Steiner STAT(steal_user_context); 691836ce679SJack Steiner } 692836ce679SJack Steiner } 693836ce679SJack Steiner 694d57c82b1SJack Steiner void gru_steal_context(struct gru_thread_state *gts, int blade_id) 6959a0deeccSJack Steiner { 6969a0deeccSJack Steiner struct gru_blade_state *blade; 6979a0deeccSJack Steiner struct gru_state *gru, *gru0; 6989a0deeccSJack Steiner struct gru_thread_state *ngts = NULL; 6999a0deeccSJack Steiner int ctxnum, ctxnum0, flag = 0, cbr, dsr; 7009a0deeccSJack Steiner 7019a0deeccSJack Steiner cbr = gts->ts_cbr_au_count; 7029a0deeccSJack Steiner dsr = gts->ts_dsr_au_count; 7039a0deeccSJack Steiner 704364b76dfSJack Steiner blade = gru_base[blade_id]; 7059a0deeccSJack Steiner spin_lock(&blade->bs_lock); 7069a0deeccSJack Steiner 7079a0deeccSJack Steiner ctxnum = next_ctxnum(blade->bs_lru_ctxnum); 7089a0deeccSJack Steiner gru = blade->bs_lru_gru; 7099a0deeccSJack Steiner if (ctxnum == 0) 7109a0deeccSJack Steiner gru = next_gru(blade, gru); 7119a0deeccSJack Steiner ctxnum0 = ctxnum; 7129a0deeccSJack Steiner gru0 = gru; 7139a0deeccSJack Steiner while (1) { 7149a0deeccSJack Steiner if (check_gru_resources(gru, cbr, dsr, GRU_NUM_CCH)) 7159a0deeccSJack Steiner break; 7169a0deeccSJack Steiner spin_lock(&gru->gs_lock); 7179a0deeccSJack Steiner for (; ctxnum < GRU_NUM_CCH; ctxnum++) { 7189a0deeccSJack Steiner if (flag && gru == gru0 && ctxnum == ctxnum0) 7199a0deeccSJack Steiner break; 7209a0deeccSJack Steiner ngts = gru->gs_gts[ctxnum]; 7219a0deeccSJack Steiner /* 7229a0deeccSJack Steiner * We are grabbing locks out of order, so trylock is 7239a0deeccSJack Steiner * needed. GTSs are usually not locked, so the odds of 7249a0deeccSJack Steiner * success are high. If trylock fails, try to steal a 7259a0deeccSJack Steiner * different GSEG. 7269a0deeccSJack Steiner */ 727836ce679SJack Steiner if (ngts && is_gts_stealable(ngts, blade)) 7289a0deeccSJack Steiner break; 7299a0deeccSJack Steiner ngts = NULL; 7309a0deeccSJack Steiner flag = 1; 7319a0deeccSJack Steiner } 7329a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 7339a0deeccSJack Steiner if (ngts || (flag && gru == gru0 && ctxnum == ctxnum0)) 7349a0deeccSJack Steiner break; 7359a0deeccSJack Steiner ctxnum = 0; 7369a0deeccSJack Steiner gru = next_gru(blade, gru); 7379a0deeccSJack Steiner } 7389a0deeccSJack Steiner blade->bs_lru_gru = gru; 7399a0deeccSJack Steiner blade->bs_lru_ctxnum = ctxnum; 7409a0deeccSJack Steiner spin_unlock(&blade->bs_lock); 7419a0deeccSJack Steiner 7429a0deeccSJack Steiner if (ngts) { 7439a0deeccSJack Steiner ngts->ts_steal_jiffies = jiffies; 744836ce679SJack Steiner gru_unload_context(ngts, is_kernel_context(ngts) ? 0 : 1); 745836ce679SJack Steiner gts_stolen(ngts, blade); 7469a0deeccSJack Steiner } else { 7479a0deeccSJack Steiner STAT(steal_context_failed); 7489a0deeccSJack Steiner } 7499a0deeccSJack Steiner gru_dbg(grudev, 75043884604SJack Steiner "stole gid %d, ctxnum %d from gts %p. Need cb %d, ds %d;" 7519a0deeccSJack Steiner " avail cb %ld, ds %ld\n", 7529a0deeccSJack Steiner gru->gs_gid, ctxnum, ngts, cbr, dsr, hweight64(gru->gs_cbr_map), 7539a0deeccSJack Steiner hweight64(gru->gs_dsr_map)); 7549a0deeccSJack Steiner } 7559a0deeccSJack Steiner 7569a0deeccSJack Steiner /* 7579a0deeccSJack Steiner * Scan the GRUs on the local blade & assign a GRU context. 7589a0deeccSJack Steiner */ 759d57c82b1SJack Steiner struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts, 760364b76dfSJack Steiner int blade) 7619a0deeccSJack Steiner { 7629a0deeccSJack Steiner struct gru_state *gru, *grux; 7639a0deeccSJack Steiner int i, max_active_contexts; 7649a0deeccSJack Steiner 7659a0deeccSJack Steiner 7669a0deeccSJack Steiner again: 7679a0deeccSJack Steiner gru = NULL; 7689a0deeccSJack Steiner max_active_contexts = GRU_NUM_CCH; 769364b76dfSJack Steiner for_each_gru_on_blade(grux, blade, i) { 7709a0deeccSJack Steiner if (check_gru_resources(grux, gts->ts_cbr_au_count, 7719a0deeccSJack Steiner gts->ts_dsr_au_count, 7729a0deeccSJack Steiner max_active_contexts)) { 7739a0deeccSJack Steiner gru = grux; 7749a0deeccSJack Steiner max_active_contexts = grux->gs_active_contexts; 7759a0deeccSJack Steiner if (max_active_contexts == 0) 7769a0deeccSJack Steiner break; 7779a0deeccSJack Steiner } 7789a0deeccSJack Steiner } 7799a0deeccSJack Steiner 7809a0deeccSJack Steiner if (gru) { 7819a0deeccSJack Steiner spin_lock(&gru->gs_lock); 7829a0deeccSJack Steiner if (!check_gru_resources(gru, gts->ts_cbr_au_count, 7839a0deeccSJack Steiner gts->ts_dsr_au_count, GRU_NUM_CCH)) { 7849a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 7859a0deeccSJack Steiner goto again; 7869a0deeccSJack Steiner } 7879a0deeccSJack Steiner reserve_gru_resources(gru, gts); 7889a0deeccSJack Steiner gts->ts_gru = gru; 78987419412SJack Steiner gts->ts_blade = gru->gs_blade_id; 7909a0deeccSJack Steiner gts->ts_ctxnum = 7919a0deeccSJack Steiner find_first_zero_bit(&gru->gs_context_map, GRU_NUM_CCH); 7929a0deeccSJack Steiner BUG_ON(gts->ts_ctxnum == GRU_NUM_CCH); 7939a0deeccSJack Steiner atomic_inc(>s->ts_refcnt); 7949a0deeccSJack Steiner gru->gs_gts[gts->ts_ctxnum] = gts; 7959a0deeccSJack Steiner __set_bit(gts->ts_ctxnum, &gru->gs_context_map); 7969a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 7979a0deeccSJack Steiner 7989a0deeccSJack Steiner STAT(assign_context); 7999a0deeccSJack Steiner gru_dbg(grudev, 80043884604SJack Steiner "gseg %p, gts %p, gid %d, ctx %d, cbr %d, dsr %d\n", 8019a0deeccSJack Steiner gseg_virtual_address(gts->ts_gru, gts->ts_ctxnum), gts, 8029a0deeccSJack Steiner gts->ts_gru->gs_gid, gts->ts_ctxnum, 8039a0deeccSJack Steiner gts->ts_cbr_au_count, gts->ts_dsr_au_count); 8049a0deeccSJack Steiner } else { 8059a0deeccSJack Steiner gru_dbg(grudev, "failed to allocate a GTS %s\n", ""); 8069a0deeccSJack Steiner STAT(assign_context_failed); 8079a0deeccSJack Steiner } 8089a0deeccSJack Steiner 8099a0deeccSJack Steiner return gru; 8109a0deeccSJack Steiner } 8119a0deeccSJack Steiner 8129a0deeccSJack Steiner /* 8139a0deeccSJack Steiner * gru_nopage 8149a0deeccSJack Steiner * 8159a0deeccSJack Steiner * Map the user's GRU segment 8169ca8e40cSJack Steiner * 8179ca8e40cSJack Steiner * Note: gru segments alway mmaped on GRU_GSEG_PAGESIZE boundaries. 8189a0deeccSJack Steiner */ 8199a0deeccSJack Steiner int gru_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 8209a0deeccSJack Steiner { 8219a0deeccSJack Steiner struct gru_thread_state *gts; 8229a0deeccSJack Steiner unsigned long paddr, vaddr; 823364b76dfSJack Steiner int blade_id; 8249a0deeccSJack Steiner 8259a0deeccSJack Steiner vaddr = (unsigned long)vmf->virtual_address; 8269a0deeccSJack Steiner gru_dbg(grudev, "vma %p, vaddr 0x%lx (0x%lx)\n", 8279a0deeccSJack Steiner vma, vaddr, GSEG_BASE(vaddr)); 8289a0deeccSJack Steiner STAT(nopfn); 8299a0deeccSJack Steiner 8309ca8e40cSJack Steiner /* The following check ensures vaddr is a valid address in the VMA */ 8319a0deeccSJack Steiner gts = gru_find_thread_state(vma, TSID(vaddr, vma)); 8329a0deeccSJack Steiner if (!gts) 8339a0deeccSJack Steiner return VM_FAULT_SIGBUS; 8349a0deeccSJack Steiner 8359a0deeccSJack Steiner again: 8369a0deeccSJack Steiner mutex_lock(>s->ts_ctxlock); 837fe5bb6b0SJack Steiner preempt_disable(); 838364b76dfSJack Steiner blade_id = uv_numa_blade_id(); 839364b76dfSJack Steiner 8409a0deeccSJack Steiner if (gts->ts_gru) { 841364b76dfSJack Steiner if (gts->ts_gru->gs_blade_id != blade_id) { 8429a0deeccSJack Steiner STAT(migrated_nopfn_unload); 8439a0deeccSJack Steiner gru_unload_context(gts, 1); 8449a0deeccSJack Steiner } else { 8459a0deeccSJack Steiner if (gru_retarget_intr(gts)) 8469a0deeccSJack Steiner STAT(migrated_nopfn_retarget); 8479a0deeccSJack Steiner } 8489a0deeccSJack Steiner } 8499a0deeccSJack Steiner 8509a0deeccSJack Steiner if (!gts->ts_gru) { 851836ce679SJack Steiner STAT(load_user_context); 852364b76dfSJack Steiner if (!gru_assign_gru_context(gts, blade_id)) { 8539a0deeccSJack Steiner preempt_enable(); 854364b76dfSJack Steiner mutex_unlock(>s->ts_ctxlock); 855364b76dfSJack Steiner set_current_state(TASK_INTERRUPTIBLE); 8569a0deeccSJack Steiner schedule_timeout(GRU_ASSIGN_DELAY); /* true hack ZZZ */ 857364b76dfSJack Steiner blade_id = uv_numa_blade_id(); 8589a0deeccSJack Steiner if (gts->ts_steal_jiffies + GRU_STEAL_DELAY < jiffies) 859364b76dfSJack Steiner gru_steal_context(gts, blade_id); 8609a0deeccSJack Steiner goto again; 8619a0deeccSJack Steiner } 8629a0deeccSJack Steiner gru_load_context(gts); 8639a0deeccSJack Steiner paddr = gseg_physical_address(gts->ts_gru, gts->ts_ctxnum); 8649a0deeccSJack Steiner remap_pfn_range(vma, vaddr & ~(GRU_GSEG_PAGESIZE - 1), 8659a0deeccSJack Steiner paddr >> PAGE_SHIFT, GRU_GSEG_PAGESIZE, 8669a0deeccSJack Steiner vma->vm_page_prot); 8679a0deeccSJack Steiner } 8689a0deeccSJack Steiner 8699a0deeccSJack Steiner preempt_enable(); 870364b76dfSJack Steiner mutex_unlock(>s->ts_ctxlock); 8719a0deeccSJack Steiner 8729a0deeccSJack Steiner return VM_FAULT_NOPAGE; 8739a0deeccSJack Steiner } 8749a0deeccSJack Steiner 875