19a0deeccSJack Steiner /* 29a0deeccSJack Steiner * SN Platform GRU Driver 39a0deeccSJack Steiner * 49a0deeccSJack Steiner * DRIVER TABLE MANAGER + GRU CONTEXT LOAD/UNLOAD 59a0deeccSJack Steiner * 69a0deeccSJack Steiner * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved. 78820f27aSJack Steiner * 88820f27aSJack Steiner * This program is free software; you can redistribute it and/or modify 98820f27aSJack Steiner * it under the terms of the GNU General Public License as published by 108820f27aSJack Steiner * the Free Software Foundation; either version 2 of the License, or 118820f27aSJack Steiner * (at your option) any later version. 128820f27aSJack Steiner * 138820f27aSJack Steiner * This program is distributed in the hope that it will be useful, 148820f27aSJack Steiner * but WITHOUT ANY WARRANTY; without even the implied warranty of 158820f27aSJack Steiner * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 168820f27aSJack Steiner * GNU General Public License for more details. 178820f27aSJack Steiner * 188820f27aSJack Steiner * You should have received a copy of the GNU General Public License 198820f27aSJack Steiner * along with this program; if not, write to the Free Software 208820f27aSJack Steiner * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 219a0deeccSJack Steiner */ 229a0deeccSJack Steiner 239a0deeccSJack Steiner #include <linux/kernel.h> 249a0deeccSJack Steiner #include <linux/slab.h> 259a0deeccSJack Steiner #include <linux/mm.h> 269a0deeccSJack Steiner #include <linux/spinlock.h> 279a0deeccSJack Steiner #include <linux/sched.h> 289a0deeccSJack Steiner #include <linux/device.h> 299a0deeccSJack Steiner #include <linux/list.h> 30*7f2251b1SJack Steiner #include <linux/err.h> 319a0deeccSJack Steiner #include <asm/uv/uv_hub.h> 329a0deeccSJack Steiner #include "gru.h" 339a0deeccSJack Steiner #include "grutables.h" 349a0deeccSJack Steiner #include "gruhandles.h" 359a0deeccSJack Steiner 369ca8e40cSJack Steiner unsigned long gru_options __read_mostly; 379a0deeccSJack Steiner 389a0deeccSJack Steiner static struct device_driver gru_driver = { 399a0deeccSJack Steiner .name = "gru" 409a0deeccSJack Steiner }; 419a0deeccSJack Steiner 429a0deeccSJack Steiner static struct device gru_device = { 43bb0dc43eSKay Sievers .init_name = "", 449a0deeccSJack Steiner .driver = &gru_driver, 459a0deeccSJack Steiner }; 469a0deeccSJack Steiner 479a0deeccSJack Steiner struct device *grudev = &gru_device; 489a0deeccSJack Steiner 499a0deeccSJack Steiner /* 509a0deeccSJack Steiner * Select a gru fault map to be used by the current cpu. Note that 519a0deeccSJack Steiner * multiple cpus may be using the same map. 529a0deeccSJack Steiner * ZZZ should "shift" be used?? Depends on HT cpu numbering 539a0deeccSJack Steiner * ZZZ should be inline but did not work on emulator 549a0deeccSJack Steiner */ 559a0deeccSJack Steiner int gru_cpu_fault_map_id(void) 569a0deeccSJack Steiner { 579a0deeccSJack Steiner return uv_blade_processor_id() % GRU_NUM_TFM; 589a0deeccSJack Steiner } 599a0deeccSJack Steiner 609a0deeccSJack Steiner /*--------- ASID Management ------------------------------------------- 619a0deeccSJack Steiner * 629a0deeccSJack Steiner * Initially, assign asids sequentially from MIN_ASID .. MAX_ASID. 639a0deeccSJack Steiner * Once MAX is reached, flush the TLB & start over. However, 649a0deeccSJack Steiner * some asids may still be in use. There won't be many (percentage wise) still 659a0deeccSJack Steiner * in use. Search active contexts & determine the value of the first 669a0deeccSJack Steiner * asid in use ("x"s below). Set "limit" to this value. 679a0deeccSJack Steiner * This defines a block of assignable asids. 689a0deeccSJack Steiner * 699a0deeccSJack Steiner * When "limit" is reached, search forward from limit+1 and determine the 709a0deeccSJack Steiner * next block of assignable asids. 719a0deeccSJack Steiner * 729a0deeccSJack Steiner * Repeat until MAX_ASID is reached, then start over again. 739a0deeccSJack Steiner * 749a0deeccSJack Steiner * Each time MAX_ASID is reached, increment the asid generation. Since 759a0deeccSJack Steiner * the search for in-use asids only checks contexts with GRUs currently 769a0deeccSJack Steiner * assigned, asids in some contexts will be missed. Prior to loading 779a0deeccSJack Steiner * a context, the asid generation of the GTS asid is rechecked. If it 789a0deeccSJack Steiner * doesn't match the current generation, a new asid will be assigned. 799a0deeccSJack Steiner * 809a0deeccSJack Steiner * 0---------------x------------x---------------------x----| 819a0deeccSJack Steiner * ^-next ^-limit ^-MAX_ASID 829a0deeccSJack Steiner * 839a0deeccSJack Steiner * All asid manipulation & context loading/unloading is protected by the 849a0deeccSJack Steiner * gs_lock. 859a0deeccSJack Steiner */ 869a0deeccSJack Steiner 879a0deeccSJack Steiner /* Hit the asid limit. Start over */ 889a0deeccSJack Steiner static int gru_wrap_asid(struct gru_state *gru) 899a0deeccSJack Steiner { 9043884604SJack Steiner gru_dbg(grudev, "gid %d\n", gru->gs_gid); 919a0deeccSJack Steiner STAT(asid_wrap); 929a0deeccSJack Steiner gru->gs_asid_gen++; 939a0deeccSJack Steiner return MIN_ASID; 949a0deeccSJack Steiner } 959a0deeccSJack Steiner 969a0deeccSJack Steiner /* Find the next chunk of unused asids */ 979a0deeccSJack Steiner static int gru_reset_asid_limit(struct gru_state *gru, int asid) 989a0deeccSJack Steiner { 999a0deeccSJack Steiner int i, gid, inuse_asid, limit; 1009a0deeccSJack Steiner 10143884604SJack Steiner gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid); 1029a0deeccSJack Steiner STAT(asid_next); 1039a0deeccSJack Steiner limit = MAX_ASID; 1049a0deeccSJack Steiner if (asid >= limit) 1059a0deeccSJack Steiner asid = gru_wrap_asid(gru); 10687419412SJack Steiner gru_flush_all_tlb(gru); 1079a0deeccSJack Steiner gid = gru->gs_gid; 1089a0deeccSJack Steiner again: 1099a0deeccSJack Steiner for (i = 0; i < GRU_NUM_CCH; i++) { 110836ce679SJack Steiner if (!gru->gs_gts[i] || is_kernel_context(gru->gs_gts[i])) 1119a0deeccSJack Steiner continue; 1129a0deeccSJack Steiner inuse_asid = gru->gs_gts[i]->ts_gms->ms_asids[gid].mt_asid; 11343884604SJack Steiner gru_dbg(grudev, "gid %d, gts %p, gms %p, inuse 0x%x, cxt %d\n", 11443884604SJack Steiner gru->gs_gid, gru->gs_gts[i], gru->gs_gts[i]->ts_gms, 11543884604SJack Steiner inuse_asid, i); 1169a0deeccSJack Steiner if (inuse_asid == asid) { 1179a0deeccSJack Steiner asid += ASID_INC; 1189a0deeccSJack Steiner if (asid >= limit) { 1199a0deeccSJack Steiner /* 1209a0deeccSJack Steiner * empty range: reset the range limit and 1219a0deeccSJack Steiner * start over 1229a0deeccSJack Steiner */ 1239a0deeccSJack Steiner limit = MAX_ASID; 1249a0deeccSJack Steiner if (asid >= MAX_ASID) 1259a0deeccSJack Steiner asid = gru_wrap_asid(gru); 1269a0deeccSJack Steiner goto again; 1279a0deeccSJack Steiner } 1289a0deeccSJack Steiner } 1299a0deeccSJack Steiner 1309a0deeccSJack Steiner if ((inuse_asid > asid) && (inuse_asid < limit)) 1319a0deeccSJack Steiner limit = inuse_asid; 1329a0deeccSJack Steiner } 1339a0deeccSJack Steiner gru->gs_asid_limit = limit; 1349a0deeccSJack Steiner gru->gs_asid = asid; 13543884604SJack Steiner gru_dbg(grudev, "gid %d, new asid 0x%x, new_limit 0x%x\n", gru->gs_gid, 13643884604SJack Steiner asid, limit); 1379a0deeccSJack Steiner return asid; 1389a0deeccSJack Steiner } 1399a0deeccSJack Steiner 1409a0deeccSJack Steiner /* Assign a new ASID to a thread context. */ 1419a0deeccSJack Steiner static int gru_assign_asid(struct gru_state *gru) 1429a0deeccSJack Steiner { 1439a0deeccSJack Steiner int asid; 1449a0deeccSJack Steiner 1459a0deeccSJack Steiner gru->gs_asid += ASID_INC; 1469a0deeccSJack Steiner asid = gru->gs_asid; 1479a0deeccSJack Steiner if (asid >= gru->gs_asid_limit) 1489a0deeccSJack Steiner asid = gru_reset_asid_limit(gru, asid); 1499a0deeccSJack Steiner 15043884604SJack Steiner gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid); 1519a0deeccSJack Steiner return asid; 1529a0deeccSJack Steiner } 1539a0deeccSJack Steiner 1549a0deeccSJack Steiner /* 1559a0deeccSJack Steiner * Clear n bits in a word. Return a word indicating the bits that were cleared. 1569a0deeccSJack Steiner * Optionally, build an array of chars that contain the bit numbers allocated. 1579a0deeccSJack Steiner */ 1589a0deeccSJack Steiner static unsigned long reserve_resources(unsigned long *p, int n, int mmax, 1599a0deeccSJack Steiner char *idx) 1609a0deeccSJack Steiner { 1619a0deeccSJack Steiner unsigned long bits = 0; 1629a0deeccSJack Steiner int i; 1639a0deeccSJack Steiner 1643eac2e95SJack Steiner while (n--) { 1659a0deeccSJack Steiner i = find_first_bit(p, mmax); 1669a0deeccSJack Steiner if (i == mmax) 1679a0deeccSJack Steiner BUG(); 1689a0deeccSJack Steiner __clear_bit(i, p); 1699a0deeccSJack Steiner __set_bit(i, &bits); 1709a0deeccSJack Steiner if (idx) 1719a0deeccSJack Steiner *idx++ = i; 1723eac2e95SJack Steiner } 1739a0deeccSJack Steiner return bits; 1749a0deeccSJack Steiner } 1759a0deeccSJack Steiner 1769ca8e40cSJack Steiner unsigned long gru_reserve_cb_resources(struct gru_state *gru, int cbr_au_count, 1779a0deeccSJack Steiner char *cbmap) 1789a0deeccSJack Steiner { 1799a0deeccSJack Steiner return reserve_resources(&gru->gs_cbr_map, cbr_au_count, GRU_CBR_AU, 1809a0deeccSJack Steiner cbmap); 1819a0deeccSJack Steiner } 1829a0deeccSJack Steiner 1839ca8e40cSJack Steiner unsigned long gru_reserve_ds_resources(struct gru_state *gru, int dsr_au_count, 1849a0deeccSJack Steiner char *dsmap) 1859a0deeccSJack Steiner { 1869a0deeccSJack Steiner return reserve_resources(&gru->gs_dsr_map, dsr_au_count, GRU_DSR_AU, 1879a0deeccSJack Steiner dsmap); 1889a0deeccSJack Steiner } 1899a0deeccSJack Steiner 1909a0deeccSJack Steiner static void reserve_gru_resources(struct gru_state *gru, 1919a0deeccSJack Steiner struct gru_thread_state *gts) 1929a0deeccSJack Steiner { 1939a0deeccSJack Steiner gru->gs_active_contexts++; 1949a0deeccSJack Steiner gts->ts_cbr_map = 1959ca8e40cSJack Steiner gru_reserve_cb_resources(gru, gts->ts_cbr_au_count, 1969a0deeccSJack Steiner gts->ts_cbr_idx); 1979a0deeccSJack Steiner gts->ts_dsr_map = 1989ca8e40cSJack Steiner gru_reserve_ds_resources(gru, gts->ts_dsr_au_count, NULL); 1999a0deeccSJack Steiner } 2009a0deeccSJack Steiner 2019a0deeccSJack Steiner static void free_gru_resources(struct gru_state *gru, 2029a0deeccSJack Steiner struct gru_thread_state *gts) 2039a0deeccSJack Steiner { 2049a0deeccSJack Steiner gru->gs_active_contexts--; 2059a0deeccSJack Steiner gru->gs_cbr_map |= gts->ts_cbr_map; 2069a0deeccSJack Steiner gru->gs_dsr_map |= gts->ts_dsr_map; 2079a0deeccSJack Steiner } 2089a0deeccSJack Steiner 2099a0deeccSJack Steiner /* 2109a0deeccSJack Steiner * Check if a GRU has sufficient free resources to satisfy an allocation 2119a0deeccSJack Steiner * request. Note: GRU locks may or may not be held when this is called. If 2129a0deeccSJack Steiner * not held, recheck after acquiring the appropriate locks. 2139a0deeccSJack Steiner * 2149a0deeccSJack Steiner * Returns 1 if sufficient resources, 0 if not 2159a0deeccSJack Steiner */ 2169a0deeccSJack Steiner static int check_gru_resources(struct gru_state *gru, int cbr_au_count, 2179a0deeccSJack Steiner int dsr_au_count, int max_active_contexts) 2189a0deeccSJack Steiner { 2199a0deeccSJack Steiner return hweight64(gru->gs_cbr_map) >= cbr_au_count 2209a0deeccSJack Steiner && hweight64(gru->gs_dsr_map) >= dsr_au_count 2219a0deeccSJack Steiner && gru->gs_active_contexts < max_active_contexts; 2229a0deeccSJack Steiner } 2239a0deeccSJack Steiner 2249a0deeccSJack Steiner /* 2259a0deeccSJack Steiner * TLB manangment requires tracking all GRU chiplets that have loaded a GSEG 2269a0deeccSJack Steiner * context. 2279a0deeccSJack Steiner */ 22843884604SJack Steiner static int gru_load_mm_tracker(struct gru_state *gru, 22943884604SJack Steiner struct gru_thread_state *gts) 2309a0deeccSJack Steiner { 23143884604SJack Steiner struct gru_mm_struct *gms = gts->ts_gms; 2329a0deeccSJack Steiner struct gru_mm_tracker *asids = &gms->ms_asids[gru->gs_gid]; 23343884604SJack Steiner unsigned short ctxbitmap = (1 << gts->ts_ctxnum); 2349a0deeccSJack Steiner int asid; 2359a0deeccSJack Steiner 2369a0deeccSJack Steiner spin_lock(&gms->ms_asid_lock); 2379a0deeccSJack Steiner asid = asids->mt_asid; 2389a0deeccSJack Steiner 23987419412SJack Steiner spin_lock(&gru->gs_asid_lock); 24087419412SJack Steiner if (asid == 0 || (asids->mt_ctxbitmap == 0 && asids->mt_asid_gen != 24187419412SJack Steiner gru->gs_asid_gen)) { 2429a0deeccSJack Steiner asid = gru_assign_asid(gru); 2439a0deeccSJack Steiner asids->mt_asid = asid; 2449a0deeccSJack Steiner asids->mt_asid_gen = gru->gs_asid_gen; 2459a0deeccSJack Steiner STAT(asid_new); 2469a0deeccSJack Steiner } else { 2479a0deeccSJack Steiner STAT(asid_reuse); 2489a0deeccSJack Steiner } 24987419412SJack Steiner spin_unlock(&gru->gs_asid_lock); 2509a0deeccSJack Steiner 2519a0deeccSJack Steiner BUG_ON(asids->mt_ctxbitmap & ctxbitmap); 2529a0deeccSJack Steiner asids->mt_ctxbitmap |= ctxbitmap; 2539a0deeccSJack Steiner if (!test_bit(gru->gs_gid, gms->ms_asidmap)) 2549a0deeccSJack Steiner __set_bit(gru->gs_gid, gms->ms_asidmap); 2559a0deeccSJack Steiner spin_unlock(&gms->ms_asid_lock); 2569a0deeccSJack Steiner 2579a0deeccSJack Steiner gru_dbg(grudev, 25843884604SJack Steiner "gid %d, gts %p, gms %p, ctxnum %d, asid 0x%x, asidmap 0x%lx\n", 25943884604SJack Steiner gru->gs_gid, gts, gms, gts->ts_ctxnum, asid, 26043884604SJack Steiner gms->ms_asidmap[0]); 2619a0deeccSJack Steiner return asid; 2629a0deeccSJack Steiner } 2639a0deeccSJack Steiner 2649a0deeccSJack Steiner static void gru_unload_mm_tracker(struct gru_state *gru, 26543884604SJack Steiner struct gru_thread_state *gts) 2669a0deeccSJack Steiner { 26743884604SJack Steiner struct gru_mm_struct *gms = gts->ts_gms; 2689a0deeccSJack Steiner struct gru_mm_tracker *asids; 2699a0deeccSJack Steiner unsigned short ctxbitmap; 2709a0deeccSJack Steiner 2719a0deeccSJack Steiner asids = &gms->ms_asids[gru->gs_gid]; 27243884604SJack Steiner ctxbitmap = (1 << gts->ts_ctxnum); 2739a0deeccSJack Steiner spin_lock(&gms->ms_asid_lock); 27487419412SJack Steiner spin_lock(&gru->gs_asid_lock); 2759a0deeccSJack Steiner BUG_ON((asids->mt_ctxbitmap & ctxbitmap) != ctxbitmap); 2769a0deeccSJack Steiner asids->mt_ctxbitmap ^= ctxbitmap; 27743884604SJack Steiner gru_dbg(grudev, "gid %d, gts %p, gms %p, ctxnum 0x%d, asidmap 0x%lx\n", 27843884604SJack Steiner gru->gs_gid, gts, gms, gts->ts_ctxnum, gms->ms_asidmap[0]); 27987419412SJack Steiner spin_unlock(&gru->gs_asid_lock); 2809a0deeccSJack Steiner spin_unlock(&gms->ms_asid_lock); 2819a0deeccSJack Steiner } 2829a0deeccSJack Steiner 2839a0deeccSJack Steiner /* 2849a0deeccSJack Steiner * Decrement the reference count on a GTS structure. Free the structure 2859a0deeccSJack Steiner * if the reference count goes to zero. 2869a0deeccSJack Steiner */ 2879a0deeccSJack Steiner void gts_drop(struct gru_thread_state *gts) 2889a0deeccSJack Steiner { 2899a0deeccSJack Steiner if (gts && atomic_dec_return(>s->ts_refcnt) == 0) { 290*7f2251b1SJack Steiner if (gts->ts_gms) 2919a0deeccSJack Steiner gru_drop_mmu_notifier(gts->ts_gms); 2929a0deeccSJack Steiner kfree(gts); 2939a0deeccSJack Steiner STAT(gts_free); 2949a0deeccSJack Steiner } 2959a0deeccSJack Steiner } 2969a0deeccSJack Steiner 2979a0deeccSJack Steiner /* 2989a0deeccSJack Steiner * Locate the GTS structure for the current thread. 2999a0deeccSJack Steiner */ 3009a0deeccSJack Steiner static struct gru_thread_state *gru_find_current_gts_nolock(struct gru_vma_data 3019a0deeccSJack Steiner *vdata, int tsid) 3029a0deeccSJack Steiner { 3039a0deeccSJack Steiner struct gru_thread_state *gts; 3049a0deeccSJack Steiner 3059a0deeccSJack Steiner list_for_each_entry(gts, &vdata->vd_head, ts_next) 3069a0deeccSJack Steiner if (gts->ts_tsid == tsid) 3079a0deeccSJack Steiner return gts; 3089a0deeccSJack Steiner return NULL; 3099a0deeccSJack Steiner } 3109a0deeccSJack Steiner 3119a0deeccSJack Steiner /* 3129a0deeccSJack Steiner * Allocate a thread state structure. 3139a0deeccSJack Steiner */ 314364b76dfSJack Steiner struct gru_thread_state *gru_alloc_gts(struct vm_area_struct *vma, 315364b76dfSJack Steiner int cbr_au_count, int dsr_au_count, int options, int tsid) 3169a0deeccSJack Steiner { 3179a0deeccSJack Steiner struct gru_thread_state *gts; 318*7f2251b1SJack Steiner struct gru_mm_struct *gms; 3199a0deeccSJack Steiner int bytes; 3209a0deeccSJack Steiner 321364b76dfSJack Steiner bytes = DSR_BYTES(dsr_au_count) + CBR_BYTES(cbr_au_count); 3229a0deeccSJack Steiner bytes += sizeof(struct gru_thread_state); 323940229b9SJack Steiner gts = kmalloc(bytes, GFP_KERNEL); 3249a0deeccSJack Steiner if (!gts) 325*7f2251b1SJack Steiner return ERR_PTR(-ENOMEM); 3269a0deeccSJack Steiner 3279a0deeccSJack Steiner STAT(gts_alloc); 328940229b9SJack Steiner memset(gts, 0, sizeof(struct gru_thread_state)); /* zero out header */ 3299a0deeccSJack Steiner atomic_set(>s->ts_refcnt, 1); 3309a0deeccSJack Steiner mutex_init(>s->ts_ctxlock); 331364b76dfSJack Steiner gts->ts_cbr_au_count = cbr_au_count; 332364b76dfSJack Steiner gts->ts_dsr_au_count = dsr_au_count; 333364b76dfSJack Steiner gts->ts_user_options = options; 334518e5cd4SJack Steiner gts->ts_user_blade_id = -1; 335518e5cd4SJack Steiner gts->ts_user_chiplet_id = -1; 3369a0deeccSJack Steiner gts->ts_tsid = tsid; 3379a0deeccSJack Steiner gts->ts_ctxnum = NULLCTX; 338364b76dfSJack Steiner gts->ts_tlb_int_select = -1; 339b1b19fcfSJack Steiner gts->ts_cch_req_slice = -1; 340364b76dfSJack Steiner gts->ts_sizeavail = GRU_SIZEAVAIL(PAGE_SHIFT); 341364b76dfSJack Steiner if (vma) { 3429a0deeccSJack Steiner gts->ts_mm = current->mm; 3439a0deeccSJack Steiner gts->ts_vma = vma; 344*7f2251b1SJack Steiner gms = gru_register_mmu_notifier(); 345*7f2251b1SJack Steiner if (IS_ERR(gms)) 3469a0deeccSJack Steiner goto err; 347*7f2251b1SJack Steiner gts->ts_gms = gms; 348364b76dfSJack Steiner } 3499a0deeccSJack Steiner 350364b76dfSJack Steiner gru_dbg(grudev, "alloc gts %p\n", gts); 3519a0deeccSJack Steiner return gts; 3529a0deeccSJack Steiner 3539a0deeccSJack Steiner err: 3549a0deeccSJack Steiner gts_drop(gts); 355*7f2251b1SJack Steiner return ERR_CAST(gms); 3569a0deeccSJack Steiner } 3579a0deeccSJack Steiner 3589a0deeccSJack Steiner /* 3599a0deeccSJack Steiner * Allocate a vma private data structure. 3609a0deeccSJack Steiner */ 3619a0deeccSJack Steiner struct gru_vma_data *gru_alloc_vma_data(struct vm_area_struct *vma, int tsid) 3629a0deeccSJack Steiner { 3639a0deeccSJack Steiner struct gru_vma_data *vdata = NULL; 3649a0deeccSJack Steiner 3659a0deeccSJack Steiner vdata = kmalloc(sizeof(*vdata), GFP_KERNEL); 3669a0deeccSJack Steiner if (!vdata) 3679a0deeccSJack Steiner return NULL; 3689a0deeccSJack Steiner 3699a0deeccSJack Steiner INIT_LIST_HEAD(&vdata->vd_head); 3709a0deeccSJack Steiner spin_lock_init(&vdata->vd_lock); 3719a0deeccSJack Steiner gru_dbg(grudev, "alloc vdata %p\n", vdata); 3729a0deeccSJack Steiner return vdata; 3739a0deeccSJack Steiner } 3749a0deeccSJack Steiner 3759a0deeccSJack Steiner /* 3769a0deeccSJack Steiner * Find the thread state structure for the current thread. 3779a0deeccSJack Steiner */ 3789a0deeccSJack Steiner struct gru_thread_state *gru_find_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; 3839a0deeccSJack Steiner 3849a0deeccSJack Steiner spin_lock(&vdata->vd_lock); 3859a0deeccSJack Steiner gts = gru_find_current_gts_nolock(vdata, tsid); 3869a0deeccSJack Steiner spin_unlock(&vdata->vd_lock); 3879a0deeccSJack Steiner gru_dbg(grudev, "vma %p, gts %p\n", vma, gts); 3889a0deeccSJack Steiner return gts; 3899a0deeccSJack Steiner } 3909a0deeccSJack Steiner 3919a0deeccSJack Steiner /* 3929a0deeccSJack Steiner * Allocate a new thread state for a GSEG. Note that races may allow 3939a0deeccSJack Steiner * another thread to race to create a gts. 3949a0deeccSJack Steiner */ 3959a0deeccSJack Steiner struct gru_thread_state *gru_alloc_thread_state(struct vm_area_struct *vma, 3969a0deeccSJack Steiner int tsid) 3979a0deeccSJack Steiner { 3989a0deeccSJack Steiner struct gru_vma_data *vdata = vma->vm_private_data; 3999a0deeccSJack Steiner struct gru_thread_state *gts, *ngts; 4009a0deeccSJack Steiner 401364b76dfSJack Steiner gts = gru_alloc_gts(vma, vdata->vd_cbr_au_count, vdata->vd_dsr_au_count, 402364b76dfSJack Steiner vdata->vd_user_options, tsid); 403*7f2251b1SJack Steiner if (IS_ERR(gts)) 404*7f2251b1SJack Steiner return gts; 4059a0deeccSJack Steiner 4069a0deeccSJack Steiner spin_lock(&vdata->vd_lock); 4079a0deeccSJack Steiner ngts = gru_find_current_gts_nolock(vdata, tsid); 4089a0deeccSJack Steiner if (ngts) { 4099a0deeccSJack Steiner gts_drop(gts); 4109a0deeccSJack Steiner gts = ngts; 4119a0deeccSJack Steiner STAT(gts_double_allocate); 4129a0deeccSJack Steiner } else { 4139a0deeccSJack Steiner list_add(>s->ts_next, &vdata->vd_head); 4149a0deeccSJack Steiner } 4159a0deeccSJack Steiner spin_unlock(&vdata->vd_lock); 4169a0deeccSJack Steiner gru_dbg(grudev, "vma %p, gts %p\n", vma, gts); 4179a0deeccSJack Steiner return gts; 4189a0deeccSJack Steiner } 4199a0deeccSJack Steiner 4209a0deeccSJack Steiner /* 4219a0deeccSJack Steiner * Free the GRU context assigned to the thread state. 4229a0deeccSJack Steiner */ 4239a0deeccSJack Steiner static void gru_free_gru_context(struct gru_thread_state *gts) 4249a0deeccSJack Steiner { 4259a0deeccSJack Steiner struct gru_state *gru; 4269a0deeccSJack Steiner 4279a0deeccSJack Steiner gru = gts->ts_gru; 42843884604SJack Steiner gru_dbg(grudev, "gts %p, gid %d\n", gts, gru->gs_gid); 4299a0deeccSJack Steiner 4309a0deeccSJack Steiner spin_lock(&gru->gs_lock); 4319a0deeccSJack Steiner gru->gs_gts[gts->ts_ctxnum] = NULL; 4329a0deeccSJack Steiner free_gru_resources(gru, gts); 4339a0deeccSJack Steiner BUG_ON(test_bit(gts->ts_ctxnum, &gru->gs_context_map) == 0); 4349a0deeccSJack Steiner __clear_bit(gts->ts_ctxnum, &gru->gs_context_map); 4359a0deeccSJack Steiner gts->ts_ctxnum = NULLCTX; 4369a0deeccSJack Steiner gts->ts_gru = NULL; 43787419412SJack Steiner gts->ts_blade = -1; 4389a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 4399a0deeccSJack Steiner 4409a0deeccSJack Steiner gts_drop(gts); 4419a0deeccSJack Steiner STAT(free_context); 4429a0deeccSJack Steiner } 4439a0deeccSJack Steiner 4449a0deeccSJack Steiner /* 4459a0deeccSJack Steiner * Prefetching cachelines help hardware performance. 4469ca8e40cSJack Steiner * (Strictly a performance enhancement. Not functionally required). 4479a0deeccSJack Steiner */ 4489a0deeccSJack Steiner static void prefetch_data(void *p, int num, int stride) 4499a0deeccSJack Steiner { 4509a0deeccSJack Steiner while (num-- > 0) { 4519a0deeccSJack Steiner prefetchw(p); 4529a0deeccSJack Steiner p += stride; 4539a0deeccSJack Steiner } 4549a0deeccSJack Steiner } 4559a0deeccSJack Steiner 4569a0deeccSJack Steiner static inline long gru_copy_handle(void *d, void *s) 4579a0deeccSJack Steiner { 4589a0deeccSJack Steiner memcpy(d, s, GRU_HANDLE_BYTES); 4599a0deeccSJack Steiner return GRU_HANDLE_BYTES; 4609a0deeccSJack Steiner } 4619a0deeccSJack Steiner 462fe5bb6b0SJack Steiner static void gru_prefetch_context(void *gseg, void *cb, void *cbe, 463fe5bb6b0SJack Steiner unsigned long cbrmap, unsigned long length) 464923f7f69SJack Steiner { 465923f7f69SJack Steiner int i, scr; 466923f7f69SJack Steiner 467923f7f69SJack Steiner prefetch_data(gseg + GRU_DS_BASE, length / GRU_CACHE_LINE_BYTES, 468923f7f69SJack Steiner GRU_CACHE_LINE_BYTES); 469923f7f69SJack Steiner 470923f7f69SJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) { 471923f7f69SJack Steiner prefetch_data(cb, 1, GRU_CACHE_LINE_BYTES); 472923f7f69SJack Steiner prefetch_data(cbe + i * GRU_HANDLE_STRIDE, 1, 473923f7f69SJack Steiner GRU_CACHE_LINE_BYTES); 474923f7f69SJack Steiner cb += GRU_HANDLE_STRIDE; 475923f7f69SJack Steiner } 476923f7f69SJack Steiner } 477923f7f69SJack Steiner 4789a0deeccSJack Steiner static void gru_load_context_data(void *save, void *grubase, int ctxnum, 479940229b9SJack Steiner unsigned long cbrmap, unsigned long dsrmap, 480940229b9SJack Steiner int data_valid) 4819a0deeccSJack Steiner { 4829a0deeccSJack Steiner void *gseg, *cb, *cbe; 4839a0deeccSJack Steiner unsigned long length; 4849a0deeccSJack Steiner int i, scr; 4859a0deeccSJack Steiner 4869a0deeccSJack Steiner gseg = grubase + ctxnum * GRU_GSEG_STRIDE; 4879a0deeccSJack Steiner cb = gseg + GRU_CB_BASE; 4889a0deeccSJack Steiner cbe = grubase + GRU_CBE_BASE; 489923f7f69SJack Steiner length = hweight64(dsrmap) * GRU_DSR_AU_BYTES; 490923f7f69SJack Steiner gru_prefetch_context(gseg, cb, cbe, cbrmap, length); 4919a0deeccSJack Steiner 4929a0deeccSJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) { 493940229b9SJack Steiner if (data_valid) { 4949a0deeccSJack Steiner save += gru_copy_handle(cb, save); 495940229b9SJack Steiner save += gru_copy_handle(cbe + i * GRU_HANDLE_STRIDE, 496940229b9SJack Steiner save); 497940229b9SJack Steiner } else { 498940229b9SJack Steiner memset(cb, 0, GRU_CACHE_LINE_BYTES); 499940229b9SJack Steiner memset(cbe + i * GRU_HANDLE_STRIDE, 0, 500940229b9SJack Steiner GRU_CACHE_LINE_BYTES); 501940229b9SJack Steiner } 5029a0deeccSJack Steiner cb += GRU_HANDLE_STRIDE; 5039a0deeccSJack Steiner } 5049a0deeccSJack Steiner 505940229b9SJack Steiner if (data_valid) 5069a0deeccSJack Steiner memcpy(gseg + GRU_DS_BASE, save, length); 507940229b9SJack Steiner else 508940229b9SJack Steiner memset(gseg + GRU_DS_BASE, 0, length); 5099a0deeccSJack Steiner } 5109a0deeccSJack Steiner 5119a0deeccSJack Steiner static void gru_unload_context_data(void *save, void *grubase, int ctxnum, 5129a0deeccSJack Steiner unsigned long cbrmap, unsigned long dsrmap) 5139a0deeccSJack Steiner { 5149a0deeccSJack Steiner void *gseg, *cb, *cbe; 5159a0deeccSJack Steiner unsigned long length; 5169a0deeccSJack Steiner int i, scr; 5179a0deeccSJack Steiner 5189a0deeccSJack Steiner gseg = grubase + ctxnum * GRU_GSEG_STRIDE; 5199a0deeccSJack Steiner cb = gseg + GRU_CB_BASE; 5209a0deeccSJack Steiner cbe = grubase + GRU_CBE_BASE; 521923f7f69SJack Steiner length = hweight64(dsrmap) * GRU_DSR_AU_BYTES; 522923f7f69SJack Steiner gru_prefetch_context(gseg, cb, cbe, cbrmap, length); 523923f7f69SJack Steiner 5249a0deeccSJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) { 5259a0deeccSJack Steiner save += gru_copy_handle(save, cb); 5269a0deeccSJack Steiner save += gru_copy_handle(save, cbe + i * GRU_HANDLE_STRIDE); 5279a0deeccSJack Steiner cb += GRU_HANDLE_STRIDE; 5289a0deeccSJack Steiner } 5299a0deeccSJack Steiner memcpy(save, gseg + GRU_DS_BASE, length); 5309a0deeccSJack Steiner } 5319a0deeccSJack Steiner 5329a0deeccSJack Steiner void gru_unload_context(struct gru_thread_state *gts, int savestate) 5339a0deeccSJack Steiner { 5349a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru; 5359a0deeccSJack Steiner struct gru_context_configuration_handle *cch; 5369a0deeccSJack Steiner int ctxnum = gts->ts_ctxnum; 5379a0deeccSJack Steiner 538836ce679SJack Steiner if (!is_kernel_context(gts)) 5399a0deeccSJack Steiner zap_vma_ptes(gts->ts_vma, UGRUADDR(gts), GRU_GSEG_PAGESIZE); 5409a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum); 5419a0deeccSJack Steiner 54243884604SJack Steiner gru_dbg(grudev, "gts %p\n", gts); 5439a0deeccSJack Steiner lock_cch_handle(cch); 5449a0deeccSJack Steiner if (cch_interrupt_sync(cch)) 5459a0deeccSJack Steiner BUG(); 5469a0deeccSJack Steiner 547836ce679SJack Steiner if (!is_kernel_context(gts)) 54843884604SJack Steiner gru_unload_mm_tracker(gru, gts); 549940229b9SJack Steiner if (savestate) { 5509a0deeccSJack Steiner gru_unload_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, 5519a0deeccSJack Steiner ctxnum, gts->ts_cbr_map, 5529a0deeccSJack Steiner gts->ts_dsr_map); 553940229b9SJack Steiner gts->ts_data_valid = 1; 554940229b9SJack Steiner } 5559a0deeccSJack Steiner 5569a0deeccSJack Steiner if (cch_deallocate(cch)) 5579a0deeccSJack Steiner BUG(); 5589a0deeccSJack Steiner unlock_cch_handle(cch); 5599a0deeccSJack Steiner 5609a0deeccSJack Steiner gru_free_gru_context(gts); 5619a0deeccSJack Steiner } 5629a0deeccSJack Steiner 5639a0deeccSJack Steiner /* 5649a0deeccSJack Steiner * Load a GRU context by copying it from the thread data structure in memory 5659a0deeccSJack Steiner * to the GRU. 5669a0deeccSJack Steiner */ 567d57c82b1SJack Steiner void gru_load_context(struct gru_thread_state *gts) 5689a0deeccSJack Steiner { 5699a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru; 5709a0deeccSJack Steiner struct gru_context_configuration_handle *cch; 5716e910074SJack Steiner int i, err, asid, ctxnum = gts->ts_ctxnum; 5729a0deeccSJack Steiner 5739a0deeccSJack Steiner gru_dbg(grudev, "gts %p\n", gts); 5749a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum); 5759a0deeccSJack Steiner 5769a0deeccSJack Steiner lock_cch_handle(cch); 5779a0deeccSJack Steiner cch->tfm_fault_bit_enable = 5789a0deeccSJack Steiner (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL 5799a0deeccSJack Steiner || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR); 5809a0deeccSJack Steiner cch->tlb_int_enable = (gts->ts_user_options == GRU_OPT_MISS_FMM_INTR); 5819a0deeccSJack Steiner if (cch->tlb_int_enable) { 5829a0deeccSJack Steiner gts->ts_tlb_int_select = gru_cpu_fault_map_id(); 5839a0deeccSJack Steiner cch->tlb_int_select = gts->ts_tlb_int_select; 5849a0deeccSJack Steiner } 585b1b19fcfSJack Steiner if (gts->ts_cch_req_slice >= 0) { 586b1b19fcfSJack Steiner cch->req_slice_set_enable = 1; 587b1b19fcfSJack Steiner cch->req_slice = gts->ts_cch_req_slice; 588b1b19fcfSJack Steiner } else { 589b1b19fcfSJack Steiner cch->req_slice_set_enable =0; 590b1b19fcfSJack Steiner } 5919a0deeccSJack Steiner cch->tfm_done_bit_enable = 0; 5926e910074SJack Steiner cch->dsr_allocation_map = gts->ts_dsr_map; 5936e910074SJack Steiner cch->cbr_allocation_map = gts->ts_cbr_map; 594836ce679SJack Steiner 595836ce679SJack Steiner if (is_kernel_context(gts)) { 596836ce679SJack Steiner cch->unmap_enable = 1; 5974a7a17c1SJack Steiner cch->tfm_done_bit_enable = 1; 5984a7a17c1SJack Steiner cch->cb_int_enable = 1; 599836ce679SJack Steiner } else { 6006e910074SJack Steiner cch->unmap_enable = 0; 6014a7a17c1SJack Steiner cch->tfm_done_bit_enable = 0; 6024a7a17c1SJack Steiner cch->cb_int_enable = 0; 603836ce679SJack Steiner asid = gru_load_mm_tracker(gru, gts); 6046e910074SJack Steiner for (i = 0; i < 8; i++) { 6056e910074SJack Steiner cch->asid[i] = asid + i; 6066e910074SJack Steiner cch->sizeavail[i] = gts->ts_sizeavail; 6076e910074SJack Steiner } 608836ce679SJack Steiner } 6096e910074SJack Steiner 6106e910074SJack Steiner err = cch_allocate(cch); 6119a0deeccSJack Steiner if (err) { 6129a0deeccSJack Steiner gru_dbg(grudev, 6139a0deeccSJack Steiner "err %d: cch %p, gts %p, cbr 0x%lx, dsr 0x%lx\n", 6149a0deeccSJack Steiner err, cch, gts, gts->ts_cbr_map, gts->ts_dsr_map); 6159a0deeccSJack Steiner BUG(); 6169a0deeccSJack Steiner } 6179a0deeccSJack Steiner 6189a0deeccSJack Steiner gru_load_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, ctxnum, 619940229b9SJack Steiner gts->ts_cbr_map, gts->ts_dsr_map, gts->ts_data_valid); 6209a0deeccSJack Steiner 6219a0deeccSJack Steiner if (cch_start(cch)) 6229a0deeccSJack Steiner BUG(); 6239a0deeccSJack Steiner unlock_cch_handle(cch); 6249a0deeccSJack Steiner } 6259a0deeccSJack Steiner 6269a0deeccSJack Steiner /* 6279a0deeccSJack Steiner * Update fields in an active CCH: 6289a0deeccSJack Steiner * - retarget interrupts on local blade 6297b8274e9SJack Steiner * - update sizeavail mask 6309a0deeccSJack Steiner */ 63199f7c229SJack Steiner int gru_update_cch(struct gru_thread_state *gts) 6329a0deeccSJack Steiner { 6339a0deeccSJack Steiner struct gru_context_configuration_handle *cch; 6349a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru; 6359a0deeccSJack Steiner int i, ctxnum = gts->ts_ctxnum, ret = 0; 6369a0deeccSJack Steiner 6379a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum); 6389a0deeccSJack Steiner 6399a0deeccSJack Steiner lock_cch_handle(cch); 6409a0deeccSJack Steiner if (cch->state == CCHSTATE_ACTIVE) { 6419a0deeccSJack Steiner if (gru->gs_gts[gts->ts_ctxnum] != gts) 6429a0deeccSJack Steiner goto exit; 6439a0deeccSJack Steiner if (cch_interrupt(cch)) 6449a0deeccSJack Steiner BUG(); 6457b8274e9SJack Steiner for (i = 0; i < 8; i++) 6467b8274e9SJack Steiner cch->sizeavail[i] = gts->ts_sizeavail; 6477b8274e9SJack Steiner gts->ts_tlb_int_select = gru_cpu_fault_map_id(); 6487b8274e9SJack Steiner cch->tlb_int_select = gru_cpu_fault_map_id(); 649cd1334f0SJack Steiner cch->tfm_fault_bit_enable = 650cd1334f0SJack Steiner (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL 651cd1334f0SJack Steiner || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR); 6529a0deeccSJack Steiner if (cch_start(cch)) 6539a0deeccSJack Steiner BUG(); 6549a0deeccSJack Steiner ret = 1; 6559a0deeccSJack Steiner } 6569a0deeccSJack Steiner exit: 6579a0deeccSJack Steiner unlock_cch_handle(cch); 6589a0deeccSJack Steiner return ret; 6599a0deeccSJack Steiner } 6609a0deeccSJack Steiner 6619a0deeccSJack Steiner /* 6629a0deeccSJack Steiner * Update CCH tlb interrupt select. Required when all the following is true: 6639a0deeccSJack Steiner * - task's GRU context is loaded into a GRU 6649a0deeccSJack Steiner * - task is using interrupt notification for TLB faults 6659a0deeccSJack Steiner * - task has migrated to a different cpu on the same blade where 6669a0deeccSJack Steiner * it was previously running. 6679a0deeccSJack Steiner */ 6689a0deeccSJack Steiner static int gru_retarget_intr(struct gru_thread_state *gts) 6699a0deeccSJack Steiner { 6709a0deeccSJack Steiner if (gts->ts_tlb_int_select < 0 6719a0deeccSJack Steiner || gts->ts_tlb_int_select == gru_cpu_fault_map_id()) 6729a0deeccSJack Steiner return 0; 6739a0deeccSJack Steiner 6749a0deeccSJack Steiner gru_dbg(grudev, "retarget from %d to %d\n", gts->ts_tlb_int_select, 6759a0deeccSJack Steiner gru_cpu_fault_map_id()); 67699f7c229SJack Steiner return gru_update_cch(gts); 6779a0deeccSJack Steiner } 6789a0deeccSJack Steiner 67955484c45SJack Steiner /* 68055484c45SJack Steiner * Unload the gru context if it is not assigned to the correct blade or 68155484c45SJack Steiner * chiplet. Misassignment can occur if the process migrates to a different 68255484c45SJack Steiner * blade or if the user changes the selected blade/chiplet. 68355484c45SJack Steiner * Return 0 if context correct placed, otherwise 1 68455484c45SJack Steiner */ 68555484c45SJack Steiner void gru_check_context_placement(struct gru_thread_state *gts) 68655484c45SJack Steiner { 68755484c45SJack Steiner struct gru_state *gru; 68855484c45SJack Steiner int blade_id, chiplet_id; 68955484c45SJack Steiner 69055484c45SJack Steiner /* 69155484c45SJack Steiner * If the current task is the context owner, verify that the 69255484c45SJack Steiner * context is correctly placed. This test is skipped for non-owner 69355484c45SJack Steiner * references. Pthread apps use non-owner references to the CBRs. 69455484c45SJack Steiner */ 69555484c45SJack Steiner gru = gts->ts_gru; 69655484c45SJack Steiner if (!gru || gts->ts_tgid_owner != current->tgid) 69755484c45SJack Steiner return; 69855484c45SJack Steiner 69955484c45SJack Steiner blade_id = gts->ts_user_blade_id; 70055484c45SJack Steiner if (blade_id < 0) 70155484c45SJack Steiner blade_id = uv_numa_blade_id(); 70255484c45SJack Steiner 70355484c45SJack Steiner chiplet_id = gts->ts_user_chiplet_id; 70455484c45SJack Steiner if (gru->gs_blade_id != blade_id || 70555484c45SJack Steiner (chiplet_id >= 0 && chiplet_id != gru->gs_chiplet_id)) { 70655484c45SJack Steiner STAT(check_context_unload); 70755484c45SJack Steiner gru_unload_context(gts, 1); 70855484c45SJack Steiner } else if (gru_retarget_intr(gts)) { 70955484c45SJack Steiner STAT(check_context_retarget_intr); 71055484c45SJack Steiner } 71155484c45SJack Steiner } 71255484c45SJack Steiner 7139a0deeccSJack Steiner 7149a0deeccSJack Steiner /* 7159a0deeccSJack Steiner * Insufficient GRU resources available on the local blade. Steal a context from 7169a0deeccSJack Steiner * a process. This is a hack until a _real_ resource scheduler is written.... 7179a0deeccSJack Steiner */ 7189a0deeccSJack Steiner #define next_ctxnum(n) ((n) < GRU_NUM_CCH - 2 ? (n) + 1 : 0) 7199a0deeccSJack Steiner #define next_gru(b, g) (((g) < &(b)->bs_grus[GRU_CHIPLETS_PER_BLADE - 1]) ? \ 7209a0deeccSJack Steiner ((g)+1) : &(b)->bs_grus[0]) 7219a0deeccSJack Steiner 722836ce679SJack Steiner static int is_gts_stealable(struct gru_thread_state *gts, 723836ce679SJack Steiner struct gru_blade_state *bs) 724836ce679SJack Steiner { 725836ce679SJack Steiner if (is_kernel_context(gts)) 726836ce679SJack Steiner return down_write_trylock(&bs->bs_kgts_sema); 727836ce679SJack Steiner else 728836ce679SJack Steiner return mutex_trylock(>s->ts_ctxlock); 729836ce679SJack Steiner } 730836ce679SJack Steiner 731836ce679SJack Steiner static void gts_stolen(struct gru_thread_state *gts, 732836ce679SJack Steiner struct gru_blade_state *bs) 733836ce679SJack Steiner { 734836ce679SJack Steiner if (is_kernel_context(gts)) { 735836ce679SJack Steiner up_write(&bs->bs_kgts_sema); 736836ce679SJack Steiner STAT(steal_kernel_context); 737836ce679SJack Steiner } else { 738836ce679SJack Steiner mutex_unlock(>s->ts_ctxlock); 739836ce679SJack Steiner STAT(steal_user_context); 740836ce679SJack Steiner } 741836ce679SJack Steiner } 742836ce679SJack Steiner 74355484c45SJack Steiner void gru_steal_context(struct gru_thread_state *gts) 7449a0deeccSJack Steiner { 7459a0deeccSJack Steiner struct gru_blade_state *blade; 7469a0deeccSJack Steiner struct gru_state *gru, *gru0; 7479a0deeccSJack Steiner struct gru_thread_state *ngts = NULL; 7489a0deeccSJack Steiner int ctxnum, ctxnum0, flag = 0, cbr, dsr; 74955484c45SJack Steiner int blade_id = gts->ts_user_blade_id; 75055484c45SJack Steiner int chiplet_id = gts->ts_user_chiplet_id; 7519a0deeccSJack Steiner 75255484c45SJack Steiner if (blade_id < 0) 75355484c45SJack Steiner blade_id = uv_numa_blade_id(); 7549a0deeccSJack Steiner cbr = gts->ts_cbr_au_count; 7559a0deeccSJack Steiner dsr = gts->ts_dsr_au_count; 7569a0deeccSJack Steiner 757364b76dfSJack Steiner blade = gru_base[blade_id]; 7589a0deeccSJack Steiner spin_lock(&blade->bs_lock); 7599a0deeccSJack Steiner 7609a0deeccSJack Steiner ctxnum = next_ctxnum(blade->bs_lru_ctxnum); 7619a0deeccSJack Steiner gru = blade->bs_lru_gru; 7629a0deeccSJack Steiner if (ctxnum == 0) 7639a0deeccSJack Steiner gru = next_gru(blade, gru); 76455484c45SJack Steiner blade->bs_lru_gru = gru; 76555484c45SJack Steiner blade->bs_lru_ctxnum = ctxnum; 7669a0deeccSJack Steiner ctxnum0 = ctxnum; 7679a0deeccSJack Steiner gru0 = gru; 7689a0deeccSJack Steiner while (1) { 76955484c45SJack Steiner if (chiplet_id < 0 || chiplet_id == gru->gs_chiplet_id) { 7709a0deeccSJack Steiner if (check_gru_resources(gru, cbr, dsr, GRU_NUM_CCH)) 7719a0deeccSJack Steiner break; 7729a0deeccSJack Steiner spin_lock(&gru->gs_lock); 7739a0deeccSJack Steiner for (; ctxnum < GRU_NUM_CCH; ctxnum++) { 7749a0deeccSJack Steiner if (flag && gru == gru0 && ctxnum == ctxnum0) 7759a0deeccSJack Steiner break; 7769a0deeccSJack Steiner ngts = gru->gs_gts[ctxnum]; 7779a0deeccSJack Steiner /* 7789a0deeccSJack Steiner * We are grabbing locks out of order, so trylock is 7799a0deeccSJack Steiner * needed. GTSs are usually not locked, so the odds of 7809a0deeccSJack Steiner * success are high. If trylock fails, try to steal a 7819a0deeccSJack Steiner * different GSEG. 7829a0deeccSJack Steiner */ 783836ce679SJack Steiner if (ngts && is_gts_stealable(ngts, blade)) 7849a0deeccSJack Steiner break; 7859a0deeccSJack Steiner ngts = NULL; 7869a0deeccSJack Steiner } 7879a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 7889a0deeccSJack Steiner if (ngts || (flag && gru == gru0 && ctxnum == ctxnum0)) 7899a0deeccSJack Steiner break; 79055484c45SJack Steiner } 79155484c45SJack Steiner if (flag && gru == gru0) 79255484c45SJack Steiner break; 79355484c45SJack Steiner flag = 1; 7949a0deeccSJack Steiner ctxnum = 0; 7959a0deeccSJack Steiner gru = next_gru(blade, gru); 7969a0deeccSJack Steiner } 7979a0deeccSJack Steiner spin_unlock(&blade->bs_lock); 7989a0deeccSJack Steiner 7999a0deeccSJack Steiner if (ngts) { 8007e796a72SJack Steiner gts->ustats.context_stolen++; 8019a0deeccSJack Steiner ngts->ts_steal_jiffies = jiffies; 802836ce679SJack Steiner gru_unload_context(ngts, is_kernel_context(ngts) ? 0 : 1); 803836ce679SJack Steiner gts_stolen(ngts, blade); 8049a0deeccSJack Steiner } else { 8059a0deeccSJack Steiner STAT(steal_context_failed); 8069a0deeccSJack Steiner } 8079a0deeccSJack Steiner gru_dbg(grudev, 80843884604SJack Steiner "stole gid %d, ctxnum %d from gts %p. Need cb %d, ds %d;" 8099a0deeccSJack Steiner " avail cb %ld, ds %ld\n", 8109a0deeccSJack Steiner gru->gs_gid, ctxnum, ngts, cbr, dsr, hweight64(gru->gs_cbr_map), 8119a0deeccSJack Steiner hweight64(gru->gs_dsr_map)); 8129a0deeccSJack Steiner } 8139a0deeccSJack Steiner 8149a0deeccSJack Steiner /* 81555484c45SJack Steiner * Assign a gru context. 81655484c45SJack Steiner */ 81755484c45SJack Steiner static int gru_assign_context_number(struct gru_state *gru) 81855484c45SJack Steiner { 81955484c45SJack Steiner int ctxnum; 82055484c45SJack Steiner 82155484c45SJack Steiner ctxnum = find_first_zero_bit(&gru->gs_context_map, GRU_NUM_CCH); 82255484c45SJack Steiner __set_bit(ctxnum, &gru->gs_context_map); 82355484c45SJack Steiner return ctxnum; 82455484c45SJack Steiner } 82555484c45SJack Steiner 82655484c45SJack Steiner /* 8279a0deeccSJack Steiner * Scan the GRUs on the local blade & assign a GRU context. 8289a0deeccSJack Steiner */ 82955484c45SJack Steiner struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts) 8309a0deeccSJack Steiner { 8319a0deeccSJack Steiner struct gru_state *gru, *grux; 8329a0deeccSJack Steiner int i, max_active_contexts; 83355484c45SJack Steiner int blade_id = gts->ts_user_blade_id; 83455484c45SJack Steiner int chiplet_id = gts->ts_user_chiplet_id; 8359a0deeccSJack Steiner 83655484c45SJack Steiner if (blade_id < 0) 83755484c45SJack Steiner blade_id = uv_numa_blade_id(); 8389a0deeccSJack Steiner again: 8399a0deeccSJack Steiner gru = NULL; 8409a0deeccSJack Steiner max_active_contexts = GRU_NUM_CCH; 84155484c45SJack Steiner for_each_gru_on_blade(grux, blade_id, i) { 84255484c45SJack Steiner if (chiplet_id >= 0 && chiplet_id != grux->gs_chiplet_id) 84355484c45SJack Steiner continue; 8449a0deeccSJack Steiner if (check_gru_resources(grux, gts->ts_cbr_au_count, 8459a0deeccSJack Steiner gts->ts_dsr_au_count, 8469a0deeccSJack Steiner max_active_contexts)) { 8479a0deeccSJack Steiner gru = grux; 8489a0deeccSJack Steiner max_active_contexts = grux->gs_active_contexts; 8499a0deeccSJack Steiner if (max_active_contexts == 0) 8509a0deeccSJack Steiner break; 8519a0deeccSJack Steiner } 8529a0deeccSJack Steiner } 8539a0deeccSJack Steiner 8549a0deeccSJack Steiner if (gru) { 8559a0deeccSJack Steiner spin_lock(&gru->gs_lock); 8569a0deeccSJack Steiner if (!check_gru_resources(gru, gts->ts_cbr_au_count, 8579a0deeccSJack Steiner gts->ts_dsr_au_count, GRU_NUM_CCH)) { 8589a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 8599a0deeccSJack Steiner goto again; 8609a0deeccSJack Steiner } 8619a0deeccSJack Steiner reserve_gru_resources(gru, gts); 8629a0deeccSJack Steiner gts->ts_gru = gru; 86387419412SJack Steiner gts->ts_blade = gru->gs_blade_id; 86455484c45SJack Steiner gts->ts_ctxnum = gru_assign_context_number(gru); 8659a0deeccSJack Steiner atomic_inc(>s->ts_refcnt); 8669a0deeccSJack Steiner gru->gs_gts[gts->ts_ctxnum] = gts; 8679a0deeccSJack Steiner spin_unlock(&gru->gs_lock); 8689a0deeccSJack Steiner 8699a0deeccSJack Steiner STAT(assign_context); 8709a0deeccSJack Steiner gru_dbg(grudev, 87143884604SJack Steiner "gseg %p, gts %p, gid %d, ctx %d, cbr %d, dsr %d\n", 8729a0deeccSJack Steiner gseg_virtual_address(gts->ts_gru, gts->ts_ctxnum), gts, 8739a0deeccSJack Steiner gts->ts_gru->gs_gid, gts->ts_ctxnum, 8749a0deeccSJack Steiner gts->ts_cbr_au_count, gts->ts_dsr_au_count); 8759a0deeccSJack Steiner } else { 8769a0deeccSJack Steiner gru_dbg(grudev, "failed to allocate a GTS %s\n", ""); 8779a0deeccSJack Steiner STAT(assign_context_failed); 8789a0deeccSJack Steiner } 8799a0deeccSJack Steiner 8809a0deeccSJack Steiner return gru; 8819a0deeccSJack Steiner } 8829a0deeccSJack Steiner 8839a0deeccSJack Steiner /* 8849a0deeccSJack Steiner * gru_nopage 8859a0deeccSJack Steiner * 8869a0deeccSJack Steiner * Map the user's GRU segment 8879ca8e40cSJack Steiner * 8889ca8e40cSJack Steiner * Note: gru segments alway mmaped on GRU_GSEG_PAGESIZE boundaries. 8899a0deeccSJack Steiner */ 8909a0deeccSJack Steiner int gru_fault(struct vm_area_struct *vma, struct vm_fault *vmf) 8919a0deeccSJack Steiner { 8929a0deeccSJack Steiner struct gru_thread_state *gts; 8939a0deeccSJack Steiner unsigned long paddr, vaddr; 8949a0deeccSJack Steiner 8959a0deeccSJack Steiner vaddr = (unsigned long)vmf->virtual_address; 8969a0deeccSJack Steiner gru_dbg(grudev, "vma %p, vaddr 0x%lx (0x%lx)\n", 8979a0deeccSJack Steiner vma, vaddr, GSEG_BASE(vaddr)); 8989a0deeccSJack Steiner STAT(nopfn); 8999a0deeccSJack Steiner 9009ca8e40cSJack Steiner /* The following check ensures vaddr is a valid address in the VMA */ 9019a0deeccSJack Steiner gts = gru_find_thread_state(vma, TSID(vaddr, vma)); 9029a0deeccSJack Steiner if (!gts) 9039a0deeccSJack Steiner return VM_FAULT_SIGBUS; 9049a0deeccSJack Steiner 9059a0deeccSJack Steiner again: 9069a0deeccSJack Steiner mutex_lock(>s->ts_ctxlock); 907fe5bb6b0SJack Steiner preempt_disable(); 908364b76dfSJack Steiner 90955484c45SJack Steiner gru_check_context_placement(gts); 9109a0deeccSJack Steiner 9119a0deeccSJack Steiner if (!gts->ts_gru) { 912836ce679SJack Steiner STAT(load_user_context); 91355484c45SJack Steiner if (!gru_assign_gru_context(gts)) { 9149a0deeccSJack Steiner preempt_enable(); 915364b76dfSJack Steiner mutex_unlock(>s->ts_ctxlock); 916364b76dfSJack Steiner set_current_state(TASK_INTERRUPTIBLE); 9179a0deeccSJack Steiner schedule_timeout(GRU_ASSIGN_DELAY); /* true hack ZZZ */ 9189a0deeccSJack Steiner if (gts->ts_steal_jiffies + GRU_STEAL_DELAY < jiffies) 91955484c45SJack Steiner gru_steal_context(gts); 9209a0deeccSJack Steiner goto again; 9219a0deeccSJack Steiner } 9229a0deeccSJack Steiner gru_load_context(gts); 9239a0deeccSJack Steiner paddr = gseg_physical_address(gts->ts_gru, gts->ts_ctxnum); 9249a0deeccSJack Steiner remap_pfn_range(vma, vaddr & ~(GRU_GSEG_PAGESIZE - 1), 9259a0deeccSJack Steiner paddr >> PAGE_SHIFT, GRU_GSEG_PAGESIZE, 9269a0deeccSJack Steiner vma->vm_page_prot); 9279a0deeccSJack Steiner } 9289a0deeccSJack Steiner 9299a0deeccSJack Steiner preempt_enable(); 930364b76dfSJack Steiner mutex_unlock(>s->ts_ctxlock); 9319a0deeccSJack Steiner 9329a0deeccSJack Steiner return VM_FAULT_NOPAGE; 9339a0deeccSJack Steiner } 9349a0deeccSJack Steiner 935