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); 310940229b9SJack Steiner gts = kmalloc(bytes, GFP_KERNEL); 3119a0deeccSJack Steiner if (!gts) 3129a0deeccSJack Steiner return NULL; 3139a0deeccSJack Steiner 3149a0deeccSJack Steiner STAT(gts_alloc); 315940229b9SJack 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; 324*b1b19fcfSJack Steiner gts->ts_cch_req_slice = -1; 325364b76dfSJack Steiner gts->ts_sizeavail = GRU_SIZEAVAIL(PAGE_SHIFT); 326364b76dfSJack Steiner if (vma) { 3279a0deeccSJack Steiner gts->ts_mm = current->mm; 3289a0deeccSJack Steiner gts->ts_vma = vma; 3299a0deeccSJack Steiner gts->ts_gms = gru_register_mmu_notifier(); 3309a0deeccSJack Steiner if (!gts->ts_gms) 3319a0deeccSJack Steiner goto err; 332364b76dfSJack Steiner } 3339a0deeccSJack Steiner 334364b76dfSJack Steiner gru_dbg(grudev, "alloc gts %p\n", gts); 3359a0deeccSJack Steiner return gts; 3369a0deeccSJack Steiner 3379a0deeccSJack Steiner err: 3389a0deeccSJack Steiner gts_drop(gts); 3399a0deeccSJack Steiner return NULL; 3409a0deeccSJack Steiner } 3419a0deeccSJack Steiner 3429a0deeccSJack Steiner /* 3439a0deeccSJack Steiner * Allocate a vma private data structure. 3449a0deeccSJack Steiner */ 3459a0deeccSJack Steiner struct gru_vma_data *gru_alloc_vma_data(struct vm_area_struct *vma, int tsid) 3469a0deeccSJack Steiner { 3479a0deeccSJack Steiner struct gru_vma_data *vdata = NULL; 3489a0deeccSJack Steiner 3499a0deeccSJack Steiner vdata = kmalloc(sizeof(*vdata), GFP_KERNEL); 3509a0deeccSJack Steiner if (!vdata) 3519a0deeccSJack Steiner return NULL; 3529a0deeccSJack Steiner 3539a0deeccSJack Steiner INIT_LIST_HEAD(&vdata->vd_head); 3549a0deeccSJack Steiner spin_lock_init(&vdata->vd_lock); 3559a0deeccSJack Steiner gru_dbg(grudev, "alloc vdata %p\n", vdata); 3569a0deeccSJack Steiner return vdata; 3579a0deeccSJack Steiner } 3589a0deeccSJack Steiner 3599a0deeccSJack Steiner /* 3609a0deeccSJack Steiner * Find the thread state structure for the current thread. 3619a0deeccSJack Steiner */ 3629a0deeccSJack Steiner struct gru_thread_state *gru_find_thread_state(struct vm_area_struct *vma, 3639a0deeccSJack Steiner int tsid) 3649a0deeccSJack Steiner { 3659a0deeccSJack Steiner struct gru_vma_data *vdata = vma->vm_private_data; 3669a0deeccSJack Steiner struct gru_thread_state *gts; 3679a0deeccSJack Steiner 3689a0deeccSJack Steiner spin_lock(&vdata->vd_lock); 3699a0deeccSJack Steiner gts = gru_find_current_gts_nolock(vdata, tsid); 3709a0deeccSJack Steiner spin_unlock(&vdata->vd_lock); 3719a0deeccSJack Steiner gru_dbg(grudev, "vma %p, gts %p\n", vma, gts); 3729a0deeccSJack Steiner return gts; 3739a0deeccSJack Steiner } 3749a0deeccSJack Steiner 3759a0deeccSJack Steiner /* 3769a0deeccSJack Steiner * Allocate a new thread state for a GSEG. Note that races may allow 3779a0deeccSJack Steiner * another thread to race to create a gts. 3789a0deeccSJack Steiner */ 3799a0deeccSJack Steiner struct gru_thread_state *gru_alloc_thread_state(struct vm_area_struct *vma, 3809a0deeccSJack Steiner int tsid) 3819a0deeccSJack Steiner { 3829a0deeccSJack Steiner struct gru_vma_data *vdata = vma->vm_private_data; 3839a0deeccSJack Steiner struct gru_thread_state *gts, *ngts; 3849a0deeccSJack Steiner 385364b76dfSJack Steiner gts = gru_alloc_gts(vma, vdata->vd_cbr_au_count, vdata->vd_dsr_au_count, 386364b76dfSJack Steiner vdata->vd_user_options, tsid); 3879a0deeccSJack Steiner if (!gts) 3889a0deeccSJack Steiner return NULL; 3899a0deeccSJack Steiner 3909a0deeccSJack Steiner spin_lock(&vdata->vd_lock); 3919a0deeccSJack Steiner ngts = gru_find_current_gts_nolock(vdata, tsid); 3929a0deeccSJack Steiner if (ngts) { 3939a0deeccSJack Steiner gts_drop(gts); 3949a0deeccSJack Steiner gts = ngts; 3959a0deeccSJack Steiner STAT(gts_double_allocate); 3969a0deeccSJack Steiner } else { 3979a0deeccSJack Steiner list_add(>s->ts_next, &vdata->vd_head); 3989a0deeccSJack Steiner } 3999a0deeccSJack Steiner spin_unlock(&vdata->vd_lock); 4009a0deeccSJack Steiner gru_dbg(grudev, "vma %p, gts %p\n", vma, gts); 4019a0deeccSJack Steiner return gts; 4029a0deeccSJack Steiner } 4039a0deeccSJack Steiner 4049a0deeccSJack Steiner /* 4059a0deeccSJack Steiner * Free the GRU context assigned to the thread state. 4069a0deeccSJack Steiner */ 4079a0deeccSJack Steiner static void gru_free_gru_context(struct gru_thread_state *gts) 4089a0deeccSJack Steiner { 4099a0deeccSJack Steiner struct gru_state *gru; 4109a0deeccSJack Steiner 4119a0deeccSJack Steiner gru = gts->ts_gru; 41243884604SJack Steiner gru_dbg(grudev, "gts %p, gid %d\n", gts, gru->gs_gid); 4139a0deeccSJack Steiner 4149a0deeccSJack Steiner spin_lock(&gru->gs_lock); 4159a0deeccSJack Steiner gru->gs_gts[gts->ts_ctxnum] = NULL; 4169a0deeccSJack Steiner free_gru_resources(gru, gts); 4179a0deeccSJack Steiner BUG_ON(test_bit(gts->ts_ctxnum, &gru->gs_context_map) == 0); 4189a0deeccSJack Steiner __clear_bit(gts->ts_ctxnum, &gru->gs_context_map); 4199a0deeccSJack Steiner gts->ts_ctxnum = NULLCTX; 4209a0deeccSJack Steiner gts->ts_gru = NULL; 42187419412SJack Steiner gts->ts_blade = -1; 4229a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 4239a0deeccSJack Steiner 4249a0deeccSJack Steiner gts_drop(gts); 4259a0deeccSJack Steiner STAT(free_context); 4269a0deeccSJack Steiner } 4279a0deeccSJack Steiner 4289a0deeccSJack Steiner /* 4299a0deeccSJack Steiner * Prefetching cachelines help hardware performance. 4309ca8e40cSJack Steiner * (Strictly a performance enhancement. Not functionally required). 4319a0deeccSJack Steiner */ 4329a0deeccSJack Steiner static void prefetch_data(void *p, int num, int stride) 4339a0deeccSJack Steiner { 4349a0deeccSJack Steiner while (num-- > 0) { 4359a0deeccSJack Steiner prefetchw(p); 4369a0deeccSJack Steiner p += stride; 4379a0deeccSJack Steiner } 4389a0deeccSJack Steiner } 4399a0deeccSJack Steiner 4409a0deeccSJack Steiner static inline long gru_copy_handle(void *d, void *s) 4419a0deeccSJack Steiner { 4429a0deeccSJack Steiner memcpy(d, s, GRU_HANDLE_BYTES); 4439a0deeccSJack Steiner return GRU_HANDLE_BYTES; 4449a0deeccSJack Steiner } 4459a0deeccSJack Steiner 446fe5bb6b0SJack Steiner static void gru_prefetch_context(void *gseg, void *cb, void *cbe, 447fe5bb6b0SJack Steiner unsigned long cbrmap, unsigned long length) 448923f7f69SJack Steiner { 449923f7f69SJack Steiner int i, scr; 450923f7f69SJack Steiner 451923f7f69SJack Steiner prefetch_data(gseg + GRU_DS_BASE, length / GRU_CACHE_LINE_BYTES, 452923f7f69SJack Steiner GRU_CACHE_LINE_BYTES); 453923f7f69SJack Steiner 454923f7f69SJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) { 455923f7f69SJack Steiner prefetch_data(cb, 1, GRU_CACHE_LINE_BYTES); 456923f7f69SJack Steiner prefetch_data(cbe + i * GRU_HANDLE_STRIDE, 1, 457923f7f69SJack Steiner GRU_CACHE_LINE_BYTES); 458923f7f69SJack Steiner cb += GRU_HANDLE_STRIDE; 459923f7f69SJack Steiner } 460923f7f69SJack Steiner } 461923f7f69SJack Steiner 4629a0deeccSJack Steiner static void gru_load_context_data(void *save, void *grubase, int ctxnum, 463940229b9SJack Steiner unsigned long cbrmap, unsigned long dsrmap, 464940229b9SJack Steiner int data_valid) 4659a0deeccSJack Steiner { 4669a0deeccSJack Steiner void *gseg, *cb, *cbe; 4679a0deeccSJack Steiner unsigned long length; 4689a0deeccSJack Steiner int i, scr; 4699a0deeccSJack Steiner 4709a0deeccSJack Steiner gseg = grubase + ctxnum * GRU_GSEG_STRIDE; 4719a0deeccSJack Steiner cb = gseg + GRU_CB_BASE; 4729a0deeccSJack Steiner cbe = grubase + GRU_CBE_BASE; 473923f7f69SJack Steiner length = hweight64(dsrmap) * GRU_DSR_AU_BYTES; 474923f7f69SJack Steiner gru_prefetch_context(gseg, cb, cbe, cbrmap, length); 4759a0deeccSJack Steiner 4769a0deeccSJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) { 477940229b9SJack Steiner if (data_valid) { 4789a0deeccSJack Steiner save += gru_copy_handle(cb, save); 479940229b9SJack Steiner save += gru_copy_handle(cbe + i * GRU_HANDLE_STRIDE, 480940229b9SJack Steiner save); 481940229b9SJack Steiner } else { 482940229b9SJack Steiner memset(cb, 0, GRU_CACHE_LINE_BYTES); 483940229b9SJack Steiner memset(cbe + i * GRU_HANDLE_STRIDE, 0, 484940229b9SJack Steiner GRU_CACHE_LINE_BYTES); 485940229b9SJack Steiner } 4869a0deeccSJack Steiner cb += GRU_HANDLE_STRIDE; 4879a0deeccSJack Steiner } 4889a0deeccSJack Steiner 489940229b9SJack Steiner if (data_valid) 4909a0deeccSJack Steiner memcpy(gseg + GRU_DS_BASE, save, length); 491940229b9SJack Steiner else 492940229b9SJack Steiner memset(gseg + GRU_DS_BASE, 0, length); 4939a0deeccSJack Steiner } 4949a0deeccSJack Steiner 4959a0deeccSJack Steiner static void gru_unload_context_data(void *save, void *grubase, int ctxnum, 4969a0deeccSJack Steiner unsigned long cbrmap, unsigned long dsrmap) 4979a0deeccSJack Steiner { 4989a0deeccSJack Steiner void *gseg, *cb, *cbe; 4999a0deeccSJack Steiner unsigned long length; 5009a0deeccSJack Steiner int i, scr; 5019a0deeccSJack Steiner 5029a0deeccSJack Steiner gseg = grubase + ctxnum * GRU_GSEG_STRIDE; 5039a0deeccSJack Steiner cb = gseg + GRU_CB_BASE; 5049a0deeccSJack Steiner cbe = grubase + GRU_CBE_BASE; 505923f7f69SJack Steiner length = hweight64(dsrmap) * GRU_DSR_AU_BYTES; 506923f7f69SJack Steiner gru_prefetch_context(gseg, cb, cbe, cbrmap, length); 507923f7f69SJack Steiner 5089a0deeccSJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) { 5099a0deeccSJack Steiner save += gru_copy_handle(save, cb); 5109a0deeccSJack Steiner save += gru_copy_handle(save, cbe + i * GRU_HANDLE_STRIDE); 5119a0deeccSJack Steiner cb += GRU_HANDLE_STRIDE; 5129a0deeccSJack Steiner } 5139a0deeccSJack Steiner memcpy(save, gseg + GRU_DS_BASE, length); 5149a0deeccSJack Steiner } 5159a0deeccSJack Steiner 5169a0deeccSJack Steiner void gru_unload_context(struct gru_thread_state *gts, int savestate) 5179a0deeccSJack Steiner { 5189a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru; 5199a0deeccSJack Steiner struct gru_context_configuration_handle *cch; 5209a0deeccSJack Steiner int ctxnum = gts->ts_ctxnum; 5219a0deeccSJack Steiner 522836ce679SJack Steiner if (!is_kernel_context(gts)) 5239a0deeccSJack Steiner zap_vma_ptes(gts->ts_vma, UGRUADDR(gts), GRU_GSEG_PAGESIZE); 5249a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum); 5259a0deeccSJack Steiner 52643884604SJack Steiner gru_dbg(grudev, "gts %p\n", gts); 5279a0deeccSJack Steiner lock_cch_handle(cch); 5289a0deeccSJack Steiner if (cch_interrupt_sync(cch)) 5299a0deeccSJack Steiner BUG(); 5309a0deeccSJack Steiner 531836ce679SJack Steiner if (!is_kernel_context(gts)) 53243884604SJack Steiner gru_unload_mm_tracker(gru, gts); 533940229b9SJack Steiner if (savestate) { 5349a0deeccSJack Steiner gru_unload_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, 5359a0deeccSJack Steiner ctxnum, gts->ts_cbr_map, 5369a0deeccSJack Steiner gts->ts_dsr_map); 537940229b9SJack Steiner gts->ts_data_valid = 1; 538940229b9SJack Steiner } 5399a0deeccSJack Steiner 5409a0deeccSJack Steiner if (cch_deallocate(cch)) 5419a0deeccSJack Steiner BUG(); 5429a0deeccSJack Steiner gts->ts_force_unload = 0; /* ts_force_unload locked by CCH lock */ 5439a0deeccSJack Steiner unlock_cch_handle(cch); 5449a0deeccSJack Steiner 5459a0deeccSJack Steiner gru_free_gru_context(gts); 5469a0deeccSJack Steiner } 5479a0deeccSJack Steiner 5489a0deeccSJack Steiner /* 5499a0deeccSJack Steiner * Load a GRU context by copying it from the thread data structure in memory 5509a0deeccSJack Steiner * to the GRU. 5519a0deeccSJack Steiner */ 552d57c82b1SJack Steiner void gru_load_context(struct gru_thread_state *gts) 5539a0deeccSJack Steiner { 5549a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru; 5559a0deeccSJack Steiner struct gru_context_configuration_handle *cch; 5566e910074SJack Steiner int i, err, asid, ctxnum = gts->ts_ctxnum; 5579a0deeccSJack Steiner 5589a0deeccSJack Steiner gru_dbg(grudev, "gts %p\n", gts); 5599a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum); 5609a0deeccSJack Steiner 5619a0deeccSJack Steiner lock_cch_handle(cch); 5629a0deeccSJack Steiner cch->tfm_fault_bit_enable = 5639a0deeccSJack Steiner (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL 5649a0deeccSJack Steiner || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR); 5659a0deeccSJack Steiner cch->tlb_int_enable = (gts->ts_user_options == GRU_OPT_MISS_FMM_INTR); 5669a0deeccSJack Steiner if (cch->tlb_int_enable) { 5679a0deeccSJack Steiner gts->ts_tlb_int_select = gru_cpu_fault_map_id(); 5689a0deeccSJack Steiner cch->tlb_int_select = gts->ts_tlb_int_select; 5699a0deeccSJack Steiner } 570*b1b19fcfSJack Steiner if (gts->ts_cch_req_slice >= 0) { 571*b1b19fcfSJack Steiner cch->req_slice_set_enable = 1; 572*b1b19fcfSJack Steiner cch->req_slice = gts->ts_cch_req_slice; 573*b1b19fcfSJack Steiner } else { 574*b1b19fcfSJack Steiner cch->req_slice_set_enable =0; 575*b1b19fcfSJack Steiner } 5769a0deeccSJack Steiner cch->tfm_done_bit_enable = 0; 5776e910074SJack Steiner cch->dsr_allocation_map = gts->ts_dsr_map; 5786e910074SJack Steiner cch->cbr_allocation_map = gts->ts_cbr_map; 579836ce679SJack Steiner 580836ce679SJack Steiner if (is_kernel_context(gts)) { 581836ce679SJack Steiner cch->unmap_enable = 1; 5824a7a17c1SJack Steiner cch->tfm_done_bit_enable = 1; 5834a7a17c1SJack Steiner cch->cb_int_enable = 1; 584836ce679SJack Steiner } else { 5856e910074SJack Steiner cch->unmap_enable = 0; 5864a7a17c1SJack Steiner cch->tfm_done_bit_enable = 0; 5874a7a17c1SJack Steiner cch->cb_int_enable = 0; 588836ce679SJack Steiner asid = gru_load_mm_tracker(gru, gts); 5896e910074SJack Steiner for (i = 0; i < 8; i++) { 5906e910074SJack Steiner cch->asid[i] = asid + i; 5916e910074SJack Steiner cch->sizeavail[i] = gts->ts_sizeavail; 5926e910074SJack Steiner } 593836ce679SJack Steiner } 5946e910074SJack Steiner 5956e910074SJack Steiner err = cch_allocate(cch); 5969a0deeccSJack Steiner if (err) { 5979a0deeccSJack Steiner gru_dbg(grudev, 5989a0deeccSJack Steiner "err %d: cch %p, gts %p, cbr 0x%lx, dsr 0x%lx\n", 5999a0deeccSJack Steiner err, cch, gts, gts->ts_cbr_map, gts->ts_dsr_map); 6009a0deeccSJack Steiner BUG(); 6019a0deeccSJack Steiner } 6029a0deeccSJack Steiner 6039a0deeccSJack Steiner gru_load_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, ctxnum, 604940229b9SJack Steiner gts->ts_cbr_map, gts->ts_dsr_map, gts->ts_data_valid); 6059a0deeccSJack Steiner 6069a0deeccSJack Steiner if (cch_start(cch)) 6079a0deeccSJack Steiner BUG(); 6089a0deeccSJack Steiner unlock_cch_handle(cch); 6099a0deeccSJack Steiner } 6109a0deeccSJack Steiner 6119a0deeccSJack Steiner /* 6129a0deeccSJack Steiner * Update fields in an active CCH: 6139a0deeccSJack Steiner * - retarget interrupts on local blade 6147b8274e9SJack Steiner * - update sizeavail mask 6159a0deeccSJack Steiner * - force a delayed context unload by clearing the CCH asids. This 6169a0deeccSJack Steiner * forces TLB misses for new GRU instructions. The context is unloaded 6179a0deeccSJack Steiner * when the next TLB miss occurs. 6189a0deeccSJack Steiner */ 6197b8274e9SJack Steiner int gru_update_cch(struct gru_thread_state *gts, int force_unload) 6209a0deeccSJack Steiner { 6219a0deeccSJack Steiner struct gru_context_configuration_handle *cch; 6229a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru; 6239a0deeccSJack Steiner int i, ctxnum = gts->ts_ctxnum, ret = 0; 6249a0deeccSJack Steiner 6259a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum); 6269a0deeccSJack Steiner 6279a0deeccSJack Steiner lock_cch_handle(cch); 6289a0deeccSJack Steiner if (cch->state == CCHSTATE_ACTIVE) { 6299a0deeccSJack Steiner if (gru->gs_gts[gts->ts_ctxnum] != gts) 6309a0deeccSJack Steiner goto exit; 6319a0deeccSJack Steiner if (cch_interrupt(cch)) 6329a0deeccSJack Steiner BUG(); 6337b8274e9SJack Steiner if (!force_unload) { 6347b8274e9SJack Steiner for (i = 0; i < 8; i++) 6357b8274e9SJack Steiner cch->sizeavail[i] = gts->ts_sizeavail; 6367b8274e9SJack Steiner gts->ts_tlb_int_select = gru_cpu_fault_map_id(); 6377b8274e9SJack Steiner cch->tlb_int_select = gru_cpu_fault_map_id(); 638cd1334f0SJack Steiner cch->tfm_fault_bit_enable = 639cd1334f0SJack Steiner (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL 640cd1334f0SJack Steiner || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR); 6419a0deeccSJack Steiner } else { 6429a0deeccSJack Steiner for (i = 0; i < 8; i++) 6439a0deeccSJack Steiner cch->asid[i] = 0; 6449a0deeccSJack Steiner cch->tfm_fault_bit_enable = 0; 6459a0deeccSJack Steiner cch->tlb_int_enable = 0; 6469a0deeccSJack Steiner gts->ts_force_unload = 1; 6479a0deeccSJack Steiner } 6489a0deeccSJack Steiner if (cch_start(cch)) 6499a0deeccSJack Steiner BUG(); 6509a0deeccSJack Steiner ret = 1; 6519a0deeccSJack Steiner } 6529a0deeccSJack Steiner exit: 6539a0deeccSJack Steiner unlock_cch_handle(cch); 6549a0deeccSJack Steiner return ret; 6559a0deeccSJack Steiner } 6569a0deeccSJack Steiner 6579a0deeccSJack Steiner /* 6589a0deeccSJack Steiner * Update CCH tlb interrupt select. Required when all the following is true: 6599a0deeccSJack Steiner * - task's GRU context is loaded into a GRU 6609a0deeccSJack Steiner * - task is using interrupt notification for TLB faults 6619a0deeccSJack Steiner * - task has migrated to a different cpu on the same blade where 6629a0deeccSJack Steiner * it was previously running. 6639a0deeccSJack Steiner */ 6649a0deeccSJack Steiner static int gru_retarget_intr(struct gru_thread_state *gts) 6659a0deeccSJack Steiner { 6669a0deeccSJack Steiner if (gts->ts_tlb_int_select < 0 6679a0deeccSJack Steiner || gts->ts_tlb_int_select == gru_cpu_fault_map_id()) 6689a0deeccSJack Steiner return 0; 6699a0deeccSJack Steiner 6709a0deeccSJack Steiner gru_dbg(grudev, "retarget from %d to %d\n", gts->ts_tlb_int_select, 6719a0deeccSJack Steiner gru_cpu_fault_map_id()); 6727b8274e9SJack Steiner return gru_update_cch(gts, 0); 6739a0deeccSJack Steiner } 6749a0deeccSJack Steiner 6759a0deeccSJack Steiner 6769a0deeccSJack Steiner /* 6779a0deeccSJack Steiner * Insufficient GRU resources available on the local blade. Steal a context from 6789a0deeccSJack Steiner * a process. This is a hack until a _real_ resource scheduler is written.... 6799a0deeccSJack Steiner */ 6809a0deeccSJack Steiner #define next_ctxnum(n) ((n) < GRU_NUM_CCH - 2 ? (n) + 1 : 0) 6819a0deeccSJack Steiner #define next_gru(b, g) (((g) < &(b)->bs_grus[GRU_CHIPLETS_PER_BLADE - 1]) ? \ 6829a0deeccSJack Steiner ((g)+1) : &(b)->bs_grus[0]) 6839a0deeccSJack Steiner 684836ce679SJack Steiner static int is_gts_stealable(struct gru_thread_state *gts, 685836ce679SJack Steiner struct gru_blade_state *bs) 686836ce679SJack Steiner { 687836ce679SJack Steiner if (is_kernel_context(gts)) 688836ce679SJack Steiner return down_write_trylock(&bs->bs_kgts_sema); 689836ce679SJack Steiner else 690836ce679SJack Steiner return mutex_trylock(>s->ts_ctxlock); 691836ce679SJack Steiner } 692836ce679SJack Steiner 693836ce679SJack Steiner static void gts_stolen(struct gru_thread_state *gts, 694836ce679SJack Steiner struct gru_blade_state *bs) 695836ce679SJack Steiner { 696836ce679SJack Steiner if (is_kernel_context(gts)) { 697836ce679SJack Steiner up_write(&bs->bs_kgts_sema); 698836ce679SJack Steiner STAT(steal_kernel_context); 699836ce679SJack Steiner } else { 700836ce679SJack Steiner mutex_unlock(>s->ts_ctxlock); 701836ce679SJack Steiner STAT(steal_user_context); 702836ce679SJack Steiner } 703836ce679SJack Steiner } 704836ce679SJack Steiner 705d57c82b1SJack Steiner void gru_steal_context(struct gru_thread_state *gts, int blade_id) 7069a0deeccSJack Steiner { 7079a0deeccSJack Steiner struct gru_blade_state *blade; 7089a0deeccSJack Steiner struct gru_state *gru, *gru0; 7099a0deeccSJack Steiner struct gru_thread_state *ngts = NULL; 7109a0deeccSJack Steiner int ctxnum, ctxnum0, flag = 0, cbr, dsr; 7119a0deeccSJack Steiner 7129a0deeccSJack Steiner cbr = gts->ts_cbr_au_count; 7139a0deeccSJack Steiner dsr = gts->ts_dsr_au_count; 7149a0deeccSJack Steiner 715364b76dfSJack Steiner blade = gru_base[blade_id]; 7169a0deeccSJack Steiner spin_lock(&blade->bs_lock); 7179a0deeccSJack Steiner 7189a0deeccSJack Steiner ctxnum = next_ctxnum(blade->bs_lru_ctxnum); 7199a0deeccSJack Steiner gru = blade->bs_lru_gru; 7209a0deeccSJack Steiner if (ctxnum == 0) 7219a0deeccSJack Steiner gru = next_gru(blade, gru); 7229a0deeccSJack Steiner ctxnum0 = ctxnum; 7239a0deeccSJack Steiner gru0 = gru; 7249a0deeccSJack Steiner while (1) { 7259a0deeccSJack Steiner if (check_gru_resources(gru, cbr, dsr, GRU_NUM_CCH)) 7269a0deeccSJack Steiner break; 7279a0deeccSJack Steiner spin_lock(&gru->gs_lock); 7289a0deeccSJack Steiner for (; ctxnum < GRU_NUM_CCH; ctxnum++) { 7299a0deeccSJack Steiner if (flag && gru == gru0 && ctxnum == ctxnum0) 7309a0deeccSJack Steiner break; 7319a0deeccSJack Steiner ngts = gru->gs_gts[ctxnum]; 7329a0deeccSJack Steiner /* 7339a0deeccSJack Steiner * We are grabbing locks out of order, so trylock is 7349a0deeccSJack Steiner * needed. GTSs are usually not locked, so the odds of 7359a0deeccSJack Steiner * success are high. If trylock fails, try to steal a 7369a0deeccSJack Steiner * different GSEG. 7379a0deeccSJack Steiner */ 738836ce679SJack Steiner if (ngts && is_gts_stealable(ngts, blade)) 7399a0deeccSJack Steiner break; 7409a0deeccSJack Steiner ngts = NULL; 7419a0deeccSJack Steiner flag = 1; 7429a0deeccSJack Steiner } 7439a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 7449a0deeccSJack Steiner if (ngts || (flag && gru == gru0 && ctxnum == ctxnum0)) 7459a0deeccSJack Steiner break; 7469a0deeccSJack Steiner ctxnum = 0; 7479a0deeccSJack Steiner gru = next_gru(blade, gru); 7489a0deeccSJack Steiner } 7499a0deeccSJack Steiner blade->bs_lru_gru = gru; 7509a0deeccSJack Steiner blade->bs_lru_ctxnum = ctxnum; 7519a0deeccSJack Steiner spin_unlock(&blade->bs_lock); 7529a0deeccSJack Steiner 7539a0deeccSJack Steiner if (ngts) { 7547e796a72SJack Steiner gts->ustats.context_stolen++; 7559a0deeccSJack Steiner ngts->ts_steal_jiffies = jiffies; 756836ce679SJack Steiner gru_unload_context(ngts, is_kernel_context(ngts) ? 0 : 1); 757836ce679SJack Steiner gts_stolen(ngts, blade); 7589a0deeccSJack Steiner } else { 7599a0deeccSJack Steiner STAT(steal_context_failed); 7609a0deeccSJack Steiner } 7619a0deeccSJack Steiner gru_dbg(grudev, 76243884604SJack Steiner "stole gid %d, ctxnum %d from gts %p. Need cb %d, ds %d;" 7639a0deeccSJack Steiner " avail cb %ld, ds %ld\n", 7649a0deeccSJack Steiner gru->gs_gid, ctxnum, ngts, cbr, dsr, hweight64(gru->gs_cbr_map), 7659a0deeccSJack Steiner hweight64(gru->gs_dsr_map)); 7669a0deeccSJack Steiner } 7679a0deeccSJack Steiner 7689a0deeccSJack Steiner /* 7699a0deeccSJack Steiner * Scan the GRUs on the local blade & assign a GRU context. 7709a0deeccSJack Steiner */ 771d57c82b1SJack Steiner struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts, 772364b76dfSJack Steiner int blade) 7739a0deeccSJack Steiner { 7749a0deeccSJack Steiner struct gru_state *gru, *grux; 7759a0deeccSJack Steiner int i, max_active_contexts; 7769a0deeccSJack Steiner 7779a0deeccSJack Steiner 7789a0deeccSJack Steiner again: 7799a0deeccSJack Steiner gru = NULL; 7809a0deeccSJack Steiner max_active_contexts = GRU_NUM_CCH; 781364b76dfSJack Steiner for_each_gru_on_blade(grux, blade, i) { 7829a0deeccSJack Steiner if (check_gru_resources(grux, gts->ts_cbr_au_count, 7839a0deeccSJack Steiner gts->ts_dsr_au_count, 7849a0deeccSJack Steiner max_active_contexts)) { 7859a0deeccSJack Steiner gru = grux; 7869a0deeccSJack Steiner max_active_contexts = grux->gs_active_contexts; 7879a0deeccSJack Steiner if (max_active_contexts == 0) 7889a0deeccSJack Steiner break; 7899a0deeccSJack Steiner } 7909a0deeccSJack Steiner } 7919a0deeccSJack Steiner 7929a0deeccSJack Steiner if (gru) { 7939a0deeccSJack Steiner spin_lock(&gru->gs_lock); 7949a0deeccSJack Steiner if (!check_gru_resources(gru, gts->ts_cbr_au_count, 7959a0deeccSJack Steiner gts->ts_dsr_au_count, GRU_NUM_CCH)) { 7969a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 7979a0deeccSJack Steiner goto again; 7989a0deeccSJack Steiner } 7999a0deeccSJack Steiner reserve_gru_resources(gru, gts); 8009a0deeccSJack Steiner gts->ts_gru = gru; 80187419412SJack Steiner gts->ts_blade = gru->gs_blade_id; 8029a0deeccSJack Steiner gts->ts_ctxnum = 8039a0deeccSJack Steiner find_first_zero_bit(&gru->gs_context_map, GRU_NUM_CCH); 8049a0deeccSJack Steiner BUG_ON(gts->ts_ctxnum == GRU_NUM_CCH); 8059a0deeccSJack Steiner atomic_inc(>s->ts_refcnt); 8069a0deeccSJack Steiner gru->gs_gts[gts->ts_ctxnum] = gts; 8079a0deeccSJack Steiner __set_bit(gts->ts_ctxnum, &gru->gs_context_map); 8089a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 8099a0deeccSJack Steiner 8109a0deeccSJack Steiner STAT(assign_context); 8119a0deeccSJack Steiner gru_dbg(grudev, 81243884604SJack Steiner "gseg %p, gts %p, gid %d, ctx %d, cbr %d, dsr %d\n", 8139a0deeccSJack Steiner gseg_virtual_address(gts->ts_gru, gts->ts_ctxnum), gts, 8149a0deeccSJack Steiner gts->ts_gru->gs_gid, gts->ts_ctxnum, 8159a0deeccSJack Steiner gts->ts_cbr_au_count, gts->ts_dsr_au_count); 8169a0deeccSJack Steiner } else { 8179a0deeccSJack Steiner gru_dbg(grudev, "failed to allocate a GTS %s\n", ""); 8189a0deeccSJack Steiner STAT(assign_context_failed); 8199a0deeccSJack Steiner } 8209a0deeccSJack Steiner 8219a0deeccSJack Steiner return gru; 8229a0deeccSJack Steiner } 8239a0deeccSJack Steiner 8249a0deeccSJack Steiner /* 8259a0deeccSJack Steiner * gru_nopage 8269a0deeccSJack Steiner * 8279a0deeccSJack Steiner * Map the user's GRU segment 8289ca8e40cSJack Steiner * 8299ca8e40cSJack Steiner * Note: gru segments alway mmaped on GRU_GSEG_PAGESIZE boundaries. 8309a0deeccSJack Steiner */ 8319a0deeccSJack Steiner int gru_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 8329a0deeccSJack Steiner { 8339a0deeccSJack Steiner struct gru_thread_state *gts; 8349a0deeccSJack Steiner unsigned long paddr, vaddr; 835364b76dfSJack Steiner int blade_id; 8369a0deeccSJack Steiner 8379a0deeccSJack Steiner vaddr = (unsigned long)vmf->virtual_address; 8389a0deeccSJack Steiner gru_dbg(grudev, "vma %p, vaddr 0x%lx (0x%lx)\n", 8399a0deeccSJack Steiner vma, vaddr, GSEG_BASE(vaddr)); 8409a0deeccSJack Steiner STAT(nopfn); 8419a0deeccSJack Steiner 8429ca8e40cSJack Steiner /* The following check ensures vaddr is a valid address in the VMA */ 8439a0deeccSJack Steiner gts = gru_find_thread_state(vma, TSID(vaddr, vma)); 8449a0deeccSJack Steiner if (!gts) 8459a0deeccSJack Steiner return VM_FAULT_SIGBUS; 8469a0deeccSJack Steiner 8479a0deeccSJack Steiner again: 8489a0deeccSJack Steiner mutex_lock(>s->ts_ctxlock); 849fe5bb6b0SJack Steiner preempt_disable(); 850364b76dfSJack Steiner blade_id = uv_numa_blade_id(); 851364b76dfSJack Steiner 8529a0deeccSJack Steiner if (gts->ts_gru) { 853364b76dfSJack Steiner if (gts->ts_gru->gs_blade_id != blade_id) { 8549a0deeccSJack Steiner STAT(migrated_nopfn_unload); 8559a0deeccSJack Steiner gru_unload_context(gts, 1); 8569a0deeccSJack Steiner } else { 8579a0deeccSJack Steiner if (gru_retarget_intr(gts)) 8589a0deeccSJack Steiner STAT(migrated_nopfn_retarget); 8599a0deeccSJack Steiner } 8609a0deeccSJack Steiner } 8619a0deeccSJack Steiner 8629a0deeccSJack Steiner if (!gts->ts_gru) { 863836ce679SJack Steiner STAT(load_user_context); 864364b76dfSJack Steiner if (!gru_assign_gru_context(gts, blade_id)) { 8659a0deeccSJack Steiner preempt_enable(); 866364b76dfSJack Steiner mutex_unlock(>s->ts_ctxlock); 867364b76dfSJack Steiner set_current_state(TASK_INTERRUPTIBLE); 8689a0deeccSJack Steiner schedule_timeout(GRU_ASSIGN_DELAY); /* true hack ZZZ */ 869364b76dfSJack Steiner blade_id = uv_numa_blade_id(); 8709a0deeccSJack Steiner if (gts->ts_steal_jiffies + GRU_STEAL_DELAY < jiffies) 871364b76dfSJack Steiner gru_steal_context(gts, blade_id); 8729a0deeccSJack Steiner goto again; 8739a0deeccSJack Steiner } 8749a0deeccSJack Steiner gru_load_context(gts); 8759a0deeccSJack Steiner paddr = gseg_physical_address(gts->ts_gru, gts->ts_ctxnum); 8769a0deeccSJack Steiner remap_pfn_range(vma, vaddr & ~(GRU_GSEG_PAGESIZE - 1), 8779a0deeccSJack Steiner paddr >> PAGE_SHIFT, GRU_GSEG_PAGESIZE, 8789a0deeccSJack Steiner vma->vm_page_prot); 8799a0deeccSJack Steiner } 8809a0deeccSJack Steiner 8819a0deeccSJack Steiner preempt_enable(); 882364b76dfSJack Steiner mutex_unlock(>s->ts_ctxlock); 8839a0deeccSJack Steiner 8849a0deeccSJack Steiner return VM_FAULT_NOPAGE; 8859a0deeccSJack Steiner } 8869a0deeccSJack Steiner 887