11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29a0deeccSJack Steiner /*
39a0deeccSJack Steiner * SN Platform GRU Driver
49a0deeccSJack Steiner *
59a0deeccSJack Steiner * DRIVER TABLE MANAGER + GRU CONTEXT LOAD/UNLOAD
69a0deeccSJack Steiner *
79a0deeccSJack Steiner * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
89a0deeccSJack Steiner */
99a0deeccSJack Steiner
109a0deeccSJack Steiner #include <linux/kernel.h>
119a0deeccSJack Steiner #include <linux/slab.h>
129a0deeccSJack Steiner #include <linux/mm.h>
139a0deeccSJack Steiner #include <linux/spinlock.h>
149a0deeccSJack Steiner #include <linux/sched.h>
159a0deeccSJack Steiner #include <linux/device.h>
169a0deeccSJack Steiner #include <linux/list.h>
177f2251b1SJack Steiner #include <linux/err.h>
18268bb0ceSLinus Torvalds #include <linux/prefetch.h>
199a0deeccSJack Steiner #include <asm/uv/uv_hub.h>
209a0deeccSJack Steiner #include "gru.h"
219a0deeccSJack Steiner #include "grutables.h"
229a0deeccSJack Steiner #include "gruhandles.h"
239a0deeccSJack Steiner
249ca8e40cSJack Steiner unsigned long gru_options __read_mostly;
259a0deeccSJack Steiner
269a0deeccSJack Steiner static struct device_driver gru_driver = {
279a0deeccSJack Steiner .name = "gru"
289a0deeccSJack Steiner };
299a0deeccSJack Steiner
309a0deeccSJack Steiner static struct device gru_device = {
31bb0dc43eSKay Sievers .init_name = "",
329a0deeccSJack Steiner .driver = &gru_driver,
339a0deeccSJack Steiner };
349a0deeccSJack Steiner
359a0deeccSJack Steiner struct device *grudev = &gru_device;
369a0deeccSJack Steiner
379a0deeccSJack Steiner /*
389a0deeccSJack Steiner * Select a gru fault map to be used by the current cpu. Note that
399a0deeccSJack Steiner * multiple cpus may be using the same map.
409a0deeccSJack Steiner * ZZZ should be inline but did not work on emulator
419a0deeccSJack Steiner */
gru_cpu_fault_map_id(void)429a0deeccSJack Steiner int gru_cpu_fault_map_id(void)
439a0deeccSJack Steiner {
4441314790SJack Steiner #ifdef CONFIG_IA64
4541314790SJack Steiner return uv_blade_processor_id() % GRU_NUM_TFM;
4641314790SJack Steiner #else
474107e1d3SJack Steiner int cpu = smp_processor_id();
484107e1d3SJack Steiner int id, core;
494107e1d3SJack Steiner
504107e1d3SJack Steiner core = uv_cpu_core_number(cpu);
514107e1d3SJack Steiner id = core + UV_MAX_INT_CORES * uv_cpu_socket_number(cpu);
524107e1d3SJack Steiner return id;
5341314790SJack Steiner #endif
549a0deeccSJack Steiner }
559a0deeccSJack Steiner
569a0deeccSJack Steiner /*--------- ASID Management -------------------------------------------
579a0deeccSJack Steiner *
589a0deeccSJack Steiner * Initially, assign asids sequentially from MIN_ASID .. MAX_ASID.
599a0deeccSJack Steiner * Once MAX is reached, flush the TLB & start over. However,
609a0deeccSJack Steiner * some asids may still be in use. There won't be many (percentage wise) still
619a0deeccSJack Steiner * in use. Search active contexts & determine the value of the first
629a0deeccSJack Steiner * asid in use ("x"s below). Set "limit" to this value.
639a0deeccSJack Steiner * This defines a block of assignable asids.
649a0deeccSJack Steiner *
659a0deeccSJack Steiner * When "limit" is reached, search forward from limit+1 and determine the
669a0deeccSJack Steiner * next block of assignable asids.
679a0deeccSJack Steiner *
689a0deeccSJack Steiner * Repeat until MAX_ASID is reached, then start over again.
699a0deeccSJack Steiner *
709a0deeccSJack Steiner * Each time MAX_ASID is reached, increment the asid generation. Since
719a0deeccSJack Steiner * the search for in-use asids only checks contexts with GRUs currently
729a0deeccSJack Steiner * assigned, asids in some contexts will be missed. Prior to loading
739a0deeccSJack Steiner * a context, the asid generation of the GTS asid is rechecked. If it
749a0deeccSJack Steiner * doesn't match the current generation, a new asid will be assigned.
759a0deeccSJack Steiner *
769a0deeccSJack Steiner * 0---------------x------------x---------------------x----|
779a0deeccSJack Steiner * ^-next ^-limit ^-MAX_ASID
789a0deeccSJack Steiner *
799a0deeccSJack Steiner * All asid manipulation & context loading/unloading is protected by the
809a0deeccSJack Steiner * gs_lock.
819a0deeccSJack Steiner */
829a0deeccSJack Steiner
839a0deeccSJack Steiner /* Hit the asid limit. Start over */
gru_wrap_asid(struct gru_state * gru)849a0deeccSJack Steiner static int gru_wrap_asid(struct gru_state *gru)
859a0deeccSJack Steiner {
8643884604SJack Steiner gru_dbg(grudev, "gid %d\n", gru->gs_gid);
879a0deeccSJack Steiner STAT(asid_wrap);
889a0deeccSJack Steiner gru->gs_asid_gen++;
899a0deeccSJack Steiner return MIN_ASID;
909a0deeccSJack Steiner }
919a0deeccSJack Steiner
929a0deeccSJack Steiner /* Find the next chunk of unused asids */
gru_reset_asid_limit(struct gru_state * gru,int asid)939a0deeccSJack Steiner static int gru_reset_asid_limit(struct gru_state *gru, int asid)
949a0deeccSJack Steiner {
959a0deeccSJack Steiner int i, gid, inuse_asid, limit;
969a0deeccSJack Steiner
9743884604SJack Steiner gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid);
989a0deeccSJack Steiner STAT(asid_next);
999a0deeccSJack Steiner limit = MAX_ASID;
1009a0deeccSJack Steiner if (asid >= limit)
1019a0deeccSJack Steiner asid = gru_wrap_asid(gru);
10287419412SJack Steiner gru_flush_all_tlb(gru);
1039a0deeccSJack Steiner gid = gru->gs_gid;
1049a0deeccSJack Steiner again:
1059a0deeccSJack Steiner for (i = 0; i < GRU_NUM_CCH; i++) {
106836ce679SJack Steiner if (!gru->gs_gts[i] || is_kernel_context(gru->gs_gts[i]))
1079a0deeccSJack Steiner continue;
1089a0deeccSJack Steiner inuse_asid = gru->gs_gts[i]->ts_gms->ms_asids[gid].mt_asid;
10943884604SJack Steiner gru_dbg(grudev, "gid %d, gts %p, gms %p, inuse 0x%x, cxt %d\n",
11043884604SJack Steiner gru->gs_gid, gru->gs_gts[i], gru->gs_gts[i]->ts_gms,
11143884604SJack Steiner inuse_asid, i);
1129a0deeccSJack Steiner if (inuse_asid == asid) {
1139a0deeccSJack Steiner asid += ASID_INC;
1149a0deeccSJack Steiner if (asid >= limit) {
1159a0deeccSJack Steiner /*
1169a0deeccSJack Steiner * empty range: reset the range limit and
1179a0deeccSJack Steiner * start over
1189a0deeccSJack Steiner */
1199a0deeccSJack Steiner limit = MAX_ASID;
1209a0deeccSJack Steiner if (asid >= MAX_ASID)
1219a0deeccSJack Steiner asid = gru_wrap_asid(gru);
1229a0deeccSJack Steiner goto again;
1239a0deeccSJack Steiner }
1249a0deeccSJack Steiner }
1259a0deeccSJack Steiner
1269a0deeccSJack Steiner if ((inuse_asid > asid) && (inuse_asid < limit))
1279a0deeccSJack Steiner limit = inuse_asid;
1289a0deeccSJack Steiner }
1299a0deeccSJack Steiner gru->gs_asid_limit = limit;
1309a0deeccSJack Steiner gru->gs_asid = asid;
13143884604SJack Steiner gru_dbg(grudev, "gid %d, new asid 0x%x, new_limit 0x%x\n", gru->gs_gid,
13243884604SJack Steiner asid, limit);
1339a0deeccSJack Steiner return asid;
1349a0deeccSJack Steiner }
1359a0deeccSJack Steiner
1369a0deeccSJack Steiner /* Assign a new ASID to a thread context. */
gru_assign_asid(struct gru_state * gru)1379a0deeccSJack Steiner static int gru_assign_asid(struct gru_state *gru)
1389a0deeccSJack Steiner {
1399a0deeccSJack Steiner int asid;
1409a0deeccSJack Steiner
1419a0deeccSJack Steiner gru->gs_asid += ASID_INC;
1429a0deeccSJack Steiner asid = gru->gs_asid;
1439a0deeccSJack Steiner if (asid >= gru->gs_asid_limit)
1449a0deeccSJack Steiner asid = gru_reset_asid_limit(gru, asid);
1459a0deeccSJack Steiner
14643884604SJack Steiner gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid);
1479a0deeccSJack Steiner return asid;
1489a0deeccSJack Steiner }
1499a0deeccSJack Steiner
1509a0deeccSJack Steiner /*
1519a0deeccSJack Steiner * Clear n bits in a word. Return a word indicating the bits that were cleared.
1529a0deeccSJack Steiner * Optionally, build an array of chars that contain the bit numbers allocated.
1539a0deeccSJack Steiner */
reserve_resources(unsigned long * p,int n,int mmax,signed char * idx)1549a0deeccSJack Steiner static unsigned long reserve_resources(unsigned long *p, int n, int mmax,
15567704738SJason A. Donenfeld signed char *idx)
1569a0deeccSJack Steiner {
1579a0deeccSJack Steiner unsigned long bits = 0;
1589a0deeccSJack Steiner int i;
1599a0deeccSJack Steiner
1603eac2e95SJack Steiner while (n--) {
1619a0deeccSJack Steiner i = find_first_bit(p, mmax);
1629a0deeccSJack Steiner if (i == mmax)
1639a0deeccSJack Steiner BUG();
1649a0deeccSJack Steiner __clear_bit(i, p);
1659a0deeccSJack Steiner __set_bit(i, &bits);
1669a0deeccSJack Steiner if (idx)
1679a0deeccSJack Steiner *idx++ = i;
1683eac2e95SJack Steiner }
1699a0deeccSJack Steiner return bits;
1709a0deeccSJack Steiner }
1719a0deeccSJack Steiner
gru_reserve_cb_resources(struct gru_state * gru,int cbr_au_count,signed char * cbmap)1729ca8e40cSJack Steiner unsigned long gru_reserve_cb_resources(struct gru_state *gru, int cbr_au_count,
17367704738SJason A. Donenfeld signed char *cbmap)
1749a0deeccSJack Steiner {
1759a0deeccSJack Steiner return reserve_resources(&gru->gs_cbr_map, cbr_au_count, GRU_CBR_AU,
1769a0deeccSJack Steiner cbmap);
1779a0deeccSJack Steiner }
1789a0deeccSJack Steiner
gru_reserve_ds_resources(struct gru_state * gru,int dsr_au_count,signed char * dsmap)1799ca8e40cSJack Steiner unsigned long gru_reserve_ds_resources(struct gru_state *gru, int dsr_au_count,
18067704738SJason A. Donenfeld signed char *dsmap)
1819a0deeccSJack Steiner {
1829a0deeccSJack Steiner return reserve_resources(&gru->gs_dsr_map, dsr_au_count, GRU_DSR_AU,
1839a0deeccSJack Steiner dsmap);
1849a0deeccSJack Steiner }
1859a0deeccSJack Steiner
reserve_gru_resources(struct gru_state * gru,struct gru_thread_state * gts)1869a0deeccSJack Steiner static void reserve_gru_resources(struct gru_state *gru,
1879a0deeccSJack Steiner struct gru_thread_state *gts)
1889a0deeccSJack Steiner {
1899a0deeccSJack Steiner gru->gs_active_contexts++;
1909a0deeccSJack Steiner gts->ts_cbr_map =
1919ca8e40cSJack Steiner gru_reserve_cb_resources(gru, gts->ts_cbr_au_count,
1929a0deeccSJack Steiner gts->ts_cbr_idx);
1939a0deeccSJack Steiner gts->ts_dsr_map =
1949ca8e40cSJack Steiner gru_reserve_ds_resources(gru, gts->ts_dsr_au_count, NULL);
1959a0deeccSJack Steiner }
1969a0deeccSJack Steiner
free_gru_resources(struct gru_state * gru,struct gru_thread_state * gts)1979a0deeccSJack Steiner static void free_gru_resources(struct gru_state *gru,
1989a0deeccSJack Steiner struct gru_thread_state *gts)
1999a0deeccSJack Steiner {
2009a0deeccSJack Steiner gru->gs_active_contexts--;
2019a0deeccSJack Steiner gru->gs_cbr_map |= gts->ts_cbr_map;
2029a0deeccSJack Steiner gru->gs_dsr_map |= gts->ts_dsr_map;
2039a0deeccSJack Steiner }
2049a0deeccSJack Steiner
2059a0deeccSJack Steiner /*
2069a0deeccSJack Steiner * Check if a GRU has sufficient free resources to satisfy an allocation
2079a0deeccSJack Steiner * request. Note: GRU locks may or may not be held when this is called. If
2089a0deeccSJack Steiner * not held, recheck after acquiring the appropriate locks.
2099a0deeccSJack Steiner *
2109a0deeccSJack Steiner * Returns 1 if sufficient resources, 0 if not
2119a0deeccSJack Steiner */
check_gru_resources(struct gru_state * gru,int cbr_au_count,int dsr_au_count,int max_active_contexts)2129a0deeccSJack Steiner static int check_gru_resources(struct gru_state *gru, int cbr_au_count,
2139a0deeccSJack Steiner int dsr_au_count, int max_active_contexts)
2149a0deeccSJack Steiner {
2159a0deeccSJack Steiner return hweight64(gru->gs_cbr_map) >= cbr_au_count
2169a0deeccSJack Steiner && hweight64(gru->gs_dsr_map) >= dsr_au_count
2179a0deeccSJack Steiner && gru->gs_active_contexts < max_active_contexts;
2189a0deeccSJack Steiner }
2199a0deeccSJack Steiner
2209a0deeccSJack Steiner /*
2219a0deeccSJack Steiner * TLB manangment requires tracking all GRU chiplets that have loaded a GSEG
2229a0deeccSJack Steiner * context.
2239a0deeccSJack Steiner */
gru_load_mm_tracker(struct gru_state * gru,struct gru_thread_state * gts)22443884604SJack Steiner static int gru_load_mm_tracker(struct gru_state *gru,
22543884604SJack Steiner struct gru_thread_state *gts)
2269a0deeccSJack Steiner {
22743884604SJack Steiner struct gru_mm_struct *gms = gts->ts_gms;
2289a0deeccSJack Steiner struct gru_mm_tracker *asids = &gms->ms_asids[gru->gs_gid];
22943884604SJack Steiner unsigned short ctxbitmap = (1 << gts->ts_ctxnum);
2309a0deeccSJack Steiner int asid;
2319a0deeccSJack Steiner
2329a0deeccSJack Steiner spin_lock(&gms->ms_asid_lock);
2339a0deeccSJack Steiner asid = asids->mt_asid;
2349a0deeccSJack Steiner
23587419412SJack Steiner spin_lock(&gru->gs_asid_lock);
23687419412SJack Steiner if (asid == 0 || (asids->mt_ctxbitmap == 0 && asids->mt_asid_gen !=
23787419412SJack Steiner gru->gs_asid_gen)) {
2389a0deeccSJack Steiner asid = gru_assign_asid(gru);
2399a0deeccSJack Steiner asids->mt_asid = asid;
2409a0deeccSJack Steiner asids->mt_asid_gen = gru->gs_asid_gen;
2419a0deeccSJack Steiner STAT(asid_new);
2429a0deeccSJack Steiner } else {
2439a0deeccSJack Steiner STAT(asid_reuse);
2449a0deeccSJack Steiner }
24587419412SJack Steiner spin_unlock(&gru->gs_asid_lock);
2469a0deeccSJack Steiner
2479a0deeccSJack Steiner BUG_ON(asids->mt_ctxbitmap & ctxbitmap);
2489a0deeccSJack Steiner asids->mt_ctxbitmap |= ctxbitmap;
2499a0deeccSJack Steiner if (!test_bit(gru->gs_gid, gms->ms_asidmap))
2509a0deeccSJack Steiner __set_bit(gru->gs_gid, gms->ms_asidmap);
2519a0deeccSJack Steiner spin_unlock(&gms->ms_asid_lock);
2529a0deeccSJack Steiner
2539a0deeccSJack Steiner gru_dbg(grudev,
25443884604SJack Steiner "gid %d, gts %p, gms %p, ctxnum %d, asid 0x%x, asidmap 0x%lx\n",
25543884604SJack Steiner gru->gs_gid, gts, gms, gts->ts_ctxnum, asid,
25643884604SJack Steiner gms->ms_asidmap[0]);
2579a0deeccSJack Steiner return asid;
2589a0deeccSJack Steiner }
2599a0deeccSJack Steiner
gru_unload_mm_tracker(struct gru_state * gru,struct gru_thread_state * gts)2609a0deeccSJack Steiner static void gru_unload_mm_tracker(struct gru_state *gru,
26143884604SJack Steiner struct gru_thread_state *gts)
2629a0deeccSJack Steiner {
26343884604SJack Steiner struct gru_mm_struct *gms = gts->ts_gms;
2649a0deeccSJack Steiner struct gru_mm_tracker *asids;
2659a0deeccSJack Steiner unsigned short ctxbitmap;
2669a0deeccSJack Steiner
2679a0deeccSJack Steiner asids = &gms->ms_asids[gru->gs_gid];
26843884604SJack Steiner ctxbitmap = (1 << gts->ts_ctxnum);
2699a0deeccSJack Steiner spin_lock(&gms->ms_asid_lock);
27087419412SJack Steiner spin_lock(&gru->gs_asid_lock);
2719a0deeccSJack Steiner BUG_ON((asids->mt_ctxbitmap & ctxbitmap) != ctxbitmap);
2729a0deeccSJack Steiner asids->mt_ctxbitmap ^= ctxbitmap;
2738e819101SDimitri Sivanich gru_dbg(grudev, "gid %d, gts %p, gms %p, ctxnum %d, asidmap 0x%lx\n",
27443884604SJack Steiner gru->gs_gid, gts, gms, gts->ts_ctxnum, gms->ms_asidmap[0]);
27587419412SJack Steiner spin_unlock(&gru->gs_asid_lock);
2769a0deeccSJack Steiner spin_unlock(&gms->ms_asid_lock);
2779a0deeccSJack Steiner }
2789a0deeccSJack Steiner
2799a0deeccSJack Steiner /*
2809a0deeccSJack Steiner * Decrement the reference count on a GTS structure. Free the structure
2819a0deeccSJack Steiner * if the reference count goes to zero.
2829a0deeccSJack Steiner */
gts_drop(struct gru_thread_state * gts)2839a0deeccSJack Steiner void gts_drop(struct gru_thread_state *gts)
2849a0deeccSJack Steiner {
28503acb0c5SXiyu Yang if (gts && refcount_dec_and_test(>s->ts_refcnt)) {
2867f2251b1SJack Steiner if (gts->ts_gms)
2879a0deeccSJack Steiner gru_drop_mmu_notifier(gts->ts_gms);
2889a0deeccSJack Steiner kfree(gts);
2899a0deeccSJack Steiner STAT(gts_free);
2909a0deeccSJack Steiner }
2919a0deeccSJack Steiner }
2929a0deeccSJack Steiner
2939a0deeccSJack Steiner /*
2949a0deeccSJack Steiner * Locate the GTS structure for the current thread.
2959a0deeccSJack Steiner */
gru_find_current_gts_nolock(struct gru_vma_data * vdata,int tsid)2969a0deeccSJack Steiner static struct gru_thread_state *gru_find_current_gts_nolock(struct gru_vma_data
2979a0deeccSJack Steiner *vdata, int tsid)
2989a0deeccSJack Steiner {
2999a0deeccSJack Steiner struct gru_thread_state *gts;
3009a0deeccSJack Steiner
3019a0deeccSJack Steiner list_for_each_entry(gts, &vdata->vd_head, ts_next)
3029a0deeccSJack Steiner if (gts->ts_tsid == tsid)
3039a0deeccSJack Steiner return gts;
3049a0deeccSJack Steiner return NULL;
3059a0deeccSJack Steiner }
3069a0deeccSJack Steiner
3079a0deeccSJack Steiner /*
3089a0deeccSJack Steiner * Allocate a thread state structure.
3099a0deeccSJack Steiner */
gru_alloc_gts(struct vm_area_struct * vma,int cbr_au_count,int dsr_au_count,unsigned char tlb_preload_count,int options,int tsid)310364b76dfSJack Steiner struct gru_thread_state *gru_alloc_gts(struct vm_area_struct *vma,
311c550222fSJack Steiner int cbr_au_count, int dsr_au_count,
312c550222fSJack Steiner unsigned char tlb_preload_count, int options, int tsid)
3139a0deeccSJack Steiner {
3149a0deeccSJack Steiner struct gru_thread_state *gts;
3157f2251b1SJack Steiner struct gru_mm_struct *gms;
3169a0deeccSJack Steiner int bytes;
3179a0deeccSJack Steiner
318364b76dfSJack Steiner bytes = DSR_BYTES(dsr_au_count) + CBR_BYTES(cbr_au_count);
3199a0deeccSJack Steiner bytes += sizeof(struct gru_thread_state);
320940229b9SJack Steiner gts = kmalloc(bytes, GFP_KERNEL);
3219a0deeccSJack Steiner if (!gts)
3227f2251b1SJack Steiner return ERR_PTR(-ENOMEM);
3239a0deeccSJack Steiner
3249a0deeccSJack Steiner STAT(gts_alloc);
325940229b9SJack Steiner memset(gts, 0, sizeof(struct gru_thread_state)); /* zero out header */
32603acb0c5SXiyu Yang refcount_set(>s->ts_refcnt, 1);
3279a0deeccSJack Steiner mutex_init(>s->ts_ctxlock);
328364b76dfSJack Steiner gts->ts_cbr_au_count = cbr_au_count;
329364b76dfSJack Steiner gts->ts_dsr_au_count = dsr_au_count;
330c550222fSJack Steiner gts->ts_tlb_preload_count = tlb_preload_count;
331364b76dfSJack Steiner gts->ts_user_options = options;
332518e5cd4SJack Steiner gts->ts_user_blade_id = -1;
333518e5cd4SJack Steiner gts->ts_user_chiplet_id = -1;
3349a0deeccSJack Steiner gts->ts_tsid = tsid;
3359a0deeccSJack Steiner gts->ts_ctxnum = NULLCTX;
336364b76dfSJack Steiner gts->ts_tlb_int_select = -1;
337b1b19fcfSJack Steiner gts->ts_cch_req_slice = -1;
338364b76dfSJack Steiner gts->ts_sizeavail = GRU_SIZEAVAIL(PAGE_SHIFT);
339364b76dfSJack Steiner if (vma) {
3409a0deeccSJack Steiner gts->ts_mm = current->mm;
3419a0deeccSJack Steiner gts->ts_vma = vma;
3427f2251b1SJack Steiner gms = gru_register_mmu_notifier();
3437f2251b1SJack Steiner if (IS_ERR(gms))
3449a0deeccSJack Steiner goto err;
3457f2251b1SJack Steiner gts->ts_gms = gms;
346364b76dfSJack Steiner }
3479a0deeccSJack Steiner
348364b76dfSJack Steiner gru_dbg(grudev, "alloc gts %p\n", gts);
3499a0deeccSJack Steiner return gts;
3509a0deeccSJack Steiner
3519a0deeccSJack Steiner err:
3529a0deeccSJack Steiner gts_drop(gts);
3537f2251b1SJack Steiner return ERR_CAST(gms);
3549a0deeccSJack Steiner }
3559a0deeccSJack Steiner
3569a0deeccSJack Steiner /*
3579a0deeccSJack Steiner * Allocate a vma private data structure.
3589a0deeccSJack Steiner */
gru_alloc_vma_data(struct vm_area_struct * vma,int tsid)3599a0deeccSJack Steiner struct gru_vma_data *gru_alloc_vma_data(struct vm_area_struct *vma, int tsid)
3609a0deeccSJack Steiner {
3619a0deeccSJack Steiner struct gru_vma_data *vdata = NULL;
3629a0deeccSJack Steiner
3639a0deeccSJack Steiner vdata = kmalloc(sizeof(*vdata), GFP_KERNEL);
3649a0deeccSJack Steiner if (!vdata)
3659a0deeccSJack Steiner return NULL;
3669a0deeccSJack Steiner
367563447d7SJack Steiner STAT(vdata_alloc);
3689a0deeccSJack Steiner INIT_LIST_HEAD(&vdata->vd_head);
3699a0deeccSJack Steiner spin_lock_init(&vdata->vd_lock);
3709a0deeccSJack Steiner gru_dbg(grudev, "alloc vdata %p\n", vdata);
3719a0deeccSJack Steiner return vdata;
3729a0deeccSJack Steiner }
3739a0deeccSJack Steiner
3749a0deeccSJack Steiner /*
3759a0deeccSJack Steiner * Find the thread state structure for the current thread.
3769a0deeccSJack Steiner */
gru_find_thread_state(struct vm_area_struct * vma,int tsid)3779a0deeccSJack Steiner struct gru_thread_state *gru_find_thread_state(struct vm_area_struct *vma,
3789a0deeccSJack Steiner int tsid)
3799a0deeccSJack Steiner {
3809a0deeccSJack Steiner struct gru_vma_data *vdata = vma->vm_private_data;
3819a0deeccSJack Steiner struct gru_thread_state *gts;
3829a0deeccSJack Steiner
3839a0deeccSJack Steiner spin_lock(&vdata->vd_lock);
3849a0deeccSJack Steiner gts = gru_find_current_gts_nolock(vdata, tsid);
3859a0deeccSJack Steiner spin_unlock(&vdata->vd_lock);
3869a0deeccSJack Steiner gru_dbg(grudev, "vma %p, gts %p\n", vma, gts);
3879a0deeccSJack Steiner return gts;
3889a0deeccSJack Steiner }
3899a0deeccSJack Steiner
3909a0deeccSJack Steiner /*
3919a0deeccSJack Steiner * Allocate a new thread state for a GSEG. Note that races may allow
3929a0deeccSJack Steiner * another thread to race to create a gts.
3939a0deeccSJack Steiner */
gru_alloc_thread_state(struct vm_area_struct * vma,int tsid)3949a0deeccSJack Steiner struct gru_thread_state *gru_alloc_thread_state(struct vm_area_struct *vma,
3959a0deeccSJack Steiner int tsid)
3969a0deeccSJack Steiner {
3979a0deeccSJack Steiner struct gru_vma_data *vdata = vma->vm_private_data;
3989a0deeccSJack Steiner struct gru_thread_state *gts, *ngts;
3999a0deeccSJack Steiner
400c550222fSJack Steiner gts = gru_alloc_gts(vma, vdata->vd_cbr_au_count,
401c550222fSJack Steiner vdata->vd_dsr_au_count,
402c550222fSJack Steiner vdata->vd_tlb_preload_count,
403364b76dfSJack Steiner vdata->vd_user_options, tsid);
4047f2251b1SJack Steiner if (IS_ERR(gts))
4057f2251b1SJack Steiner return gts;
4069a0deeccSJack Steiner
4079a0deeccSJack Steiner spin_lock(&vdata->vd_lock);
4089a0deeccSJack Steiner ngts = gru_find_current_gts_nolock(vdata, tsid);
4099a0deeccSJack Steiner if (ngts) {
4109a0deeccSJack Steiner gts_drop(gts);
4119a0deeccSJack Steiner gts = ngts;
4129a0deeccSJack Steiner STAT(gts_double_allocate);
4139a0deeccSJack Steiner } else {
4149a0deeccSJack Steiner list_add(>s->ts_next, &vdata->vd_head);
4159a0deeccSJack Steiner }
4169a0deeccSJack Steiner spin_unlock(&vdata->vd_lock);
4179a0deeccSJack Steiner gru_dbg(grudev, "vma %p, gts %p\n", vma, gts);
4189a0deeccSJack Steiner return gts;
4199a0deeccSJack Steiner }
4209a0deeccSJack Steiner
4219a0deeccSJack Steiner /*
4229a0deeccSJack Steiner * Free the GRU context assigned to the thread state.
4239a0deeccSJack Steiner */
gru_free_gru_context(struct gru_thread_state * gts)4249a0deeccSJack Steiner static void gru_free_gru_context(struct gru_thread_state *gts)
4259a0deeccSJack Steiner {
4269a0deeccSJack Steiner struct gru_state *gru;
4279a0deeccSJack Steiner
4289a0deeccSJack Steiner gru = gts->ts_gru;
42943884604SJack Steiner gru_dbg(grudev, "gts %p, gid %d\n", gts, gru->gs_gid);
4309a0deeccSJack Steiner
4319a0deeccSJack Steiner spin_lock(&gru->gs_lock);
4329a0deeccSJack Steiner gru->gs_gts[gts->ts_ctxnum] = NULL;
4339a0deeccSJack Steiner free_gru_resources(gru, gts);
4349a0deeccSJack Steiner BUG_ON(test_bit(gts->ts_ctxnum, &gru->gs_context_map) == 0);
4359a0deeccSJack Steiner __clear_bit(gts->ts_ctxnum, &gru->gs_context_map);
4369a0deeccSJack Steiner gts->ts_ctxnum = NULLCTX;
4379a0deeccSJack Steiner gts->ts_gru = NULL;
43887419412SJack Steiner gts->ts_blade = -1;
4399a0deeccSJack Steiner spin_unlock(&gru->gs_lock);
4409a0deeccSJack Steiner
4419a0deeccSJack Steiner gts_drop(gts);
4429a0deeccSJack Steiner STAT(free_context);
4439a0deeccSJack Steiner }
4449a0deeccSJack Steiner
4459a0deeccSJack Steiner /*
4469a0deeccSJack Steiner * Prefetching cachelines help hardware performance.
4479ca8e40cSJack Steiner * (Strictly a performance enhancement. Not functionally required).
4489a0deeccSJack Steiner */
prefetch_data(void * p,int num,int stride)4499a0deeccSJack Steiner static void prefetch_data(void *p, int num, int stride)
4509a0deeccSJack Steiner {
4519a0deeccSJack Steiner while (num-- > 0) {
4529a0deeccSJack Steiner prefetchw(p);
4539a0deeccSJack Steiner p += stride;
4549a0deeccSJack Steiner }
4559a0deeccSJack Steiner }
4569a0deeccSJack Steiner
gru_copy_handle(void * d,void * s)4579a0deeccSJack Steiner static inline long gru_copy_handle(void *d, void *s)
4589a0deeccSJack Steiner {
4599a0deeccSJack Steiner memcpy(d, s, GRU_HANDLE_BYTES);
4609a0deeccSJack Steiner return GRU_HANDLE_BYTES;
4619a0deeccSJack Steiner }
4629a0deeccSJack Steiner
gru_prefetch_context(void * gseg,void * cb,void * cbe,unsigned long cbrmap,unsigned long length)463fe5bb6b0SJack Steiner static void gru_prefetch_context(void *gseg, void *cb, void *cbe,
464fe5bb6b0SJack Steiner unsigned long cbrmap, unsigned long length)
465923f7f69SJack Steiner {
466923f7f69SJack Steiner int i, scr;
467923f7f69SJack Steiner
468923f7f69SJack Steiner prefetch_data(gseg + GRU_DS_BASE, length / GRU_CACHE_LINE_BYTES,
469923f7f69SJack Steiner GRU_CACHE_LINE_BYTES);
470923f7f69SJack Steiner
471923f7f69SJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
472923f7f69SJack Steiner prefetch_data(cb, 1, GRU_CACHE_LINE_BYTES);
473923f7f69SJack Steiner prefetch_data(cbe + i * GRU_HANDLE_STRIDE, 1,
474923f7f69SJack Steiner GRU_CACHE_LINE_BYTES);
475923f7f69SJack Steiner cb += GRU_HANDLE_STRIDE;
476923f7f69SJack Steiner }
477923f7f69SJack Steiner }
478923f7f69SJack Steiner
gru_load_context_data(void * save,void * grubase,int ctxnum,unsigned long cbrmap,unsigned long dsrmap,int data_valid)4799a0deeccSJack Steiner static void gru_load_context_data(void *save, void *grubase, int ctxnum,
480940229b9SJack Steiner unsigned long cbrmap, unsigned long dsrmap,
481940229b9SJack Steiner int data_valid)
4829a0deeccSJack Steiner {
4839a0deeccSJack Steiner void *gseg, *cb, *cbe;
4849a0deeccSJack Steiner unsigned long length;
4859a0deeccSJack Steiner int i, scr;
4869a0deeccSJack Steiner
4879a0deeccSJack Steiner gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
4889a0deeccSJack Steiner cb = gseg + GRU_CB_BASE;
4899a0deeccSJack Steiner cbe = grubase + GRU_CBE_BASE;
490923f7f69SJack Steiner length = hweight64(dsrmap) * GRU_DSR_AU_BYTES;
491923f7f69SJack Steiner gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
4929a0deeccSJack Steiner
4939a0deeccSJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
494940229b9SJack Steiner if (data_valid) {
4959a0deeccSJack Steiner save += gru_copy_handle(cb, save);
496940229b9SJack Steiner save += gru_copy_handle(cbe + i * GRU_HANDLE_STRIDE,
497940229b9SJack Steiner save);
498940229b9SJack Steiner } else {
499940229b9SJack Steiner memset(cb, 0, GRU_CACHE_LINE_BYTES);
500940229b9SJack Steiner memset(cbe + i * GRU_HANDLE_STRIDE, 0,
501940229b9SJack Steiner GRU_CACHE_LINE_BYTES);
502940229b9SJack Steiner }
50367bf04a5SJack Steiner /* Flush CBE to hide race in context restart */
50467bf04a5SJack Steiner mb();
50567bf04a5SJack Steiner gru_flush_cache(cbe + i * GRU_HANDLE_STRIDE);
5069a0deeccSJack Steiner cb += GRU_HANDLE_STRIDE;
5079a0deeccSJack Steiner }
5089a0deeccSJack Steiner
509940229b9SJack Steiner if (data_valid)
5109a0deeccSJack Steiner memcpy(gseg + GRU_DS_BASE, save, length);
511940229b9SJack Steiner else
512940229b9SJack Steiner memset(gseg + GRU_DS_BASE, 0, length);
5139a0deeccSJack Steiner }
5149a0deeccSJack Steiner
gru_unload_context_data(void * save,void * grubase,int ctxnum,unsigned long cbrmap,unsigned long dsrmap)5159a0deeccSJack Steiner static void gru_unload_context_data(void *save, void *grubase, int ctxnum,
5169a0deeccSJack Steiner unsigned long cbrmap, unsigned long dsrmap)
5179a0deeccSJack Steiner {
5189a0deeccSJack Steiner void *gseg, *cb, *cbe;
5199a0deeccSJack Steiner unsigned long length;
5209a0deeccSJack Steiner int i, scr;
5219a0deeccSJack Steiner
5229a0deeccSJack Steiner gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
5239a0deeccSJack Steiner cb = gseg + GRU_CB_BASE;
5249a0deeccSJack Steiner cbe = grubase + GRU_CBE_BASE;
525923f7f69SJack Steiner length = hweight64(dsrmap) * GRU_DSR_AU_BYTES;
52667bf04a5SJack Steiner
52767bf04a5SJack Steiner /* CBEs may not be coherent. Flush them from cache */
52867bf04a5SJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr)
52967bf04a5SJack Steiner gru_flush_cache(cbe + i * GRU_HANDLE_STRIDE);
53067bf04a5SJack Steiner mb(); /* Let the CL flush complete */
53167bf04a5SJack Steiner
532923f7f69SJack Steiner gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
533923f7f69SJack Steiner
5349a0deeccSJack Steiner for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
5359a0deeccSJack Steiner save += gru_copy_handle(save, cb);
5369a0deeccSJack Steiner save += gru_copy_handle(save, cbe + i * GRU_HANDLE_STRIDE);
5379a0deeccSJack Steiner cb += GRU_HANDLE_STRIDE;
5389a0deeccSJack Steiner }
5399a0deeccSJack Steiner memcpy(save, gseg + GRU_DS_BASE, length);
5409a0deeccSJack Steiner }
5419a0deeccSJack Steiner
gru_unload_context(struct gru_thread_state * gts,int savestate)5429a0deeccSJack Steiner void gru_unload_context(struct gru_thread_state *gts, int savestate)
5439a0deeccSJack Steiner {
5449a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru;
5459a0deeccSJack Steiner struct gru_context_configuration_handle *cch;
5469a0deeccSJack Steiner int ctxnum = gts->ts_ctxnum;
5479a0deeccSJack Steiner
548836ce679SJack Steiner if (!is_kernel_context(gts))
5499a0deeccSJack Steiner zap_vma_ptes(gts->ts_vma, UGRUADDR(gts), GRU_GSEG_PAGESIZE);
5509a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
5519a0deeccSJack Steiner
552563447d7SJack Steiner gru_dbg(grudev, "gts %p, cbrmap 0x%lx, dsrmap 0x%lx\n",
553563447d7SJack Steiner gts, gts->ts_cbr_map, gts->ts_dsr_map);
5549a0deeccSJack Steiner lock_cch_handle(cch);
5559a0deeccSJack Steiner if (cch_interrupt_sync(cch))
5569a0deeccSJack Steiner BUG();
5579a0deeccSJack Steiner
558836ce679SJack Steiner if (!is_kernel_context(gts))
55943884604SJack Steiner gru_unload_mm_tracker(gru, gts);
560940229b9SJack Steiner if (savestate) {
5619a0deeccSJack Steiner gru_unload_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr,
5629a0deeccSJack Steiner ctxnum, gts->ts_cbr_map,
5639a0deeccSJack Steiner gts->ts_dsr_map);
564940229b9SJack Steiner gts->ts_data_valid = 1;
565940229b9SJack Steiner }
5669a0deeccSJack Steiner
5679a0deeccSJack Steiner if (cch_deallocate(cch))
5689a0deeccSJack Steiner BUG();
5699a0deeccSJack Steiner unlock_cch_handle(cch);
5709a0deeccSJack Steiner
5719a0deeccSJack Steiner gru_free_gru_context(gts);
5729a0deeccSJack Steiner }
5739a0deeccSJack Steiner
5749a0deeccSJack Steiner /*
5759a0deeccSJack Steiner * Load a GRU context by copying it from the thread data structure in memory
5769a0deeccSJack Steiner * to the GRU.
5779a0deeccSJack Steiner */
gru_load_context(struct gru_thread_state * gts)578d57c82b1SJack Steiner void gru_load_context(struct gru_thread_state *gts)
5799a0deeccSJack Steiner {
5809a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru;
5819a0deeccSJack Steiner struct gru_context_configuration_handle *cch;
5826e910074SJack Steiner int i, err, asid, ctxnum = gts->ts_ctxnum;
5839a0deeccSJack Steiner
5849a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
5859a0deeccSJack Steiner lock_cch_handle(cch);
5869a0deeccSJack Steiner cch->tfm_fault_bit_enable =
5879a0deeccSJack Steiner (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
5889a0deeccSJack Steiner || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
5899a0deeccSJack Steiner cch->tlb_int_enable = (gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
5909a0deeccSJack Steiner if (cch->tlb_int_enable) {
5919a0deeccSJack Steiner gts->ts_tlb_int_select = gru_cpu_fault_map_id();
5929a0deeccSJack Steiner cch->tlb_int_select = gts->ts_tlb_int_select;
5939a0deeccSJack Steiner }
594b1b19fcfSJack Steiner if (gts->ts_cch_req_slice >= 0) {
595b1b19fcfSJack Steiner cch->req_slice_set_enable = 1;
596b1b19fcfSJack Steiner cch->req_slice = gts->ts_cch_req_slice;
597b1b19fcfSJack Steiner } else {
598b1b19fcfSJack Steiner cch->req_slice_set_enable =0;
599b1b19fcfSJack Steiner }
6009a0deeccSJack Steiner cch->tfm_done_bit_enable = 0;
6016e910074SJack Steiner cch->dsr_allocation_map = gts->ts_dsr_map;
6026e910074SJack Steiner cch->cbr_allocation_map = gts->ts_cbr_map;
603836ce679SJack Steiner
604836ce679SJack Steiner if (is_kernel_context(gts)) {
605836ce679SJack Steiner cch->unmap_enable = 1;
6064a7a17c1SJack Steiner cch->tfm_done_bit_enable = 1;
6074a7a17c1SJack Steiner cch->cb_int_enable = 1;
6084107e1d3SJack Steiner cch->tlb_int_select = 0; /* For now, ints go to cpu 0 */
609836ce679SJack Steiner } else {
6106e910074SJack Steiner cch->unmap_enable = 0;
6114a7a17c1SJack Steiner cch->tfm_done_bit_enable = 0;
6124a7a17c1SJack Steiner cch->cb_int_enable = 0;
613836ce679SJack Steiner asid = gru_load_mm_tracker(gru, gts);
6146e910074SJack Steiner for (i = 0; i < 8; i++) {
6156e910074SJack Steiner cch->asid[i] = asid + i;
6166e910074SJack Steiner cch->sizeavail[i] = gts->ts_sizeavail;
6176e910074SJack Steiner }
618836ce679SJack Steiner }
6196e910074SJack Steiner
6206e910074SJack Steiner err = cch_allocate(cch);
6219a0deeccSJack Steiner if (err) {
6229a0deeccSJack Steiner gru_dbg(grudev,
6239a0deeccSJack Steiner "err %d: cch %p, gts %p, cbr 0x%lx, dsr 0x%lx\n",
6249a0deeccSJack Steiner err, cch, gts, gts->ts_cbr_map, gts->ts_dsr_map);
6259a0deeccSJack Steiner BUG();
6269a0deeccSJack Steiner }
6279a0deeccSJack Steiner
6289a0deeccSJack Steiner gru_load_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, ctxnum,
629940229b9SJack Steiner gts->ts_cbr_map, gts->ts_dsr_map, gts->ts_data_valid);
6309a0deeccSJack Steiner
6319a0deeccSJack Steiner if (cch_start(cch))
6329a0deeccSJack Steiner BUG();
6339a0deeccSJack Steiner unlock_cch_handle(cch);
634563447d7SJack Steiner
635563447d7SJack Steiner gru_dbg(grudev, "gid %d, gts %p, cbrmap 0x%lx, dsrmap 0x%lx, tie %d, tis %d\n",
636563447d7SJack Steiner gts->ts_gru->gs_gid, gts, gts->ts_cbr_map, gts->ts_dsr_map,
637563447d7SJack Steiner (gts->ts_user_options == GRU_OPT_MISS_FMM_INTR), gts->ts_tlb_int_select);
6389a0deeccSJack Steiner }
6399a0deeccSJack Steiner
6409a0deeccSJack Steiner /*
6419a0deeccSJack Steiner * Update fields in an active CCH:
6429a0deeccSJack Steiner * - retarget interrupts on local blade
6437b8274e9SJack Steiner * - update sizeavail mask
6449a0deeccSJack Steiner */
gru_update_cch(struct gru_thread_state * gts)64599f7c229SJack Steiner int gru_update_cch(struct gru_thread_state *gts)
6469a0deeccSJack Steiner {
6479a0deeccSJack Steiner struct gru_context_configuration_handle *cch;
6489a0deeccSJack Steiner struct gru_state *gru = gts->ts_gru;
6499a0deeccSJack Steiner int i, ctxnum = gts->ts_ctxnum, ret = 0;
6509a0deeccSJack Steiner
6519a0deeccSJack Steiner cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
6529a0deeccSJack Steiner
6539a0deeccSJack Steiner lock_cch_handle(cch);
6549a0deeccSJack Steiner if (cch->state == CCHSTATE_ACTIVE) {
6559a0deeccSJack Steiner if (gru->gs_gts[gts->ts_ctxnum] != gts)
6569a0deeccSJack Steiner goto exit;
6579a0deeccSJack Steiner if (cch_interrupt(cch))
6589a0deeccSJack Steiner BUG();
6597b8274e9SJack Steiner for (i = 0; i < 8; i++)
6607b8274e9SJack Steiner cch->sizeavail[i] = gts->ts_sizeavail;
6617b8274e9SJack Steiner gts->ts_tlb_int_select = gru_cpu_fault_map_id();
6627b8274e9SJack Steiner cch->tlb_int_select = gru_cpu_fault_map_id();
663cd1334f0SJack Steiner cch->tfm_fault_bit_enable =
664cd1334f0SJack Steiner (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
665cd1334f0SJack Steiner || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
6669a0deeccSJack Steiner if (cch_start(cch))
6679a0deeccSJack Steiner BUG();
6689a0deeccSJack Steiner ret = 1;
6699a0deeccSJack Steiner }
6709a0deeccSJack Steiner exit:
6719a0deeccSJack Steiner unlock_cch_handle(cch);
6729a0deeccSJack Steiner return ret;
6739a0deeccSJack Steiner }
6749a0deeccSJack Steiner
6759a0deeccSJack Steiner /*
6769a0deeccSJack Steiner * Update CCH tlb interrupt select. Required when all the following is true:
6779a0deeccSJack Steiner * - task's GRU context is loaded into a GRU
6789a0deeccSJack Steiner * - task is using interrupt notification for TLB faults
6799a0deeccSJack Steiner * - task has migrated to a different cpu on the same blade where
6809a0deeccSJack Steiner * it was previously running.
6819a0deeccSJack Steiner */
gru_retarget_intr(struct gru_thread_state * gts)6829a0deeccSJack Steiner static int gru_retarget_intr(struct gru_thread_state *gts)
6839a0deeccSJack Steiner {
6849a0deeccSJack Steiner if (gts->ts_tlb_int_select < 0
6859a0deeccSJack Steiner || gts->ts_tlb_int_select == gru_cpu_fault_map_id())
6869a0deeccSJack Steiner return 0;
6879a0deeccSJack Steiner
6889a0deeccSJack Steiner gru_dbg(grudev, "retarget from %d to %d\n", gts->ts_tlb_int_select,
6899a0deeccSJack Steiner gru_cpu_fault_map_id());
69099f7c229SJack Steiner return gru_update_cch(gts);
6919a0deeccSJack Steiner }
6929a0deeccSJack Steiner
69355484c45SJack Steiner /*
69441314790SJack Steiner * Check if a GRU context is allowed to use a specific chiplet. By default
69541314790SJack Steiner * a context is assigned to any blade-local chiplet. However, users can
69641314790SJack Steiner * override this.
69741314790SJack Steiner * Returns 1 if assignment allowed, 0 otherwise
69841314790SJack Steiner */
gru_check_chiplet_assignment(struct gru_state * gru,struct gru_thread_state * gts)69941314790SJack Steiner static int gru_check_chiplet_assignment(struct gru_state *gru,
70041314790SJack Steiner struct gru_thread_state *gts)
70141314790SJack Steiner {
70241314790SJack Steiner int blade_id;
70341314790SJack Steiner int chiplet_id;
70441314790SJack Steiner
70541314790SJack Steiner blade_id = gts->ts_user_blade_id;
70641314790SJack Steiner if (blade_id < 0)
70741314790SJack Steiner blade_id = uv_numa_blade_id();
70841314790SJack Steiner
70941314790SJack Steiner chiplet_id = gts->ts_user_chiplet_id;
71041314790SJack Steiner return gru->gs_blade_id == blade_id &&
71141314790SJack Steiner (chiplet_id < 0 || chiplet_id == gru->gs_chiplet_id);
71241314790SJack Steiner }
71341314790SJack Steiner
71441314790SJack Steiner /*
71555484c45SJack Steiner * Unload the gru context if it is not assigned to the correct blade or
71655484c45SJack Steiner * chiplet. Misassignment can occur if the process migrates to a different
71755484c45SJack Steiner * blade or if the user changes the selected blade/chiplet.
71855484c45SJack Steiner */
gru_check_context_placement(struct gru_thread_state * gts)719*643a16a0SZheng Wang int gru_check_context_placement(struct gru_thread_state *gts)
72055484c45SJack Steiner {
72155484c45SJack Steiner struct gru_state *gru;
722*643a16a0SZheng Wang int ret = 0;
72355484c45SJack Steiner
72455484c45SJack Steiner /*
72555484c45SJack Steiner * If the current task is the context owner, verify that the
72655484c45SJack Steiner * context is correctly placed. This test is skipped for non-owner
72755484c45SJack Steiner * references. Pthread apps use non-owner references to the CBRs.
72855484c45SJack Steiner */
72955484c45SJack Steiner gru = gts->ts_gru;
730*643a16a0SZheng Wang /*
731*643a16a0SZheng Wang * If gru or gts->ts_tgid_owner isn't initialized properly, return
732*643a16a0SZheng Wang * success to indicate that the caller does not need to unload the
733*643a16a0SZheng Wang * gru context.The caller is responsible for their inspection and
734*643a16a0SZheng Wang * reinitialization if needed.
735*643a16a0SZheng Wang */
73655484c45SJack Steiner if (!gru || gts->ts_tgid_owner != current->tgid)
737*643a16a0SZheng Wang return ret;
73855484c45SJack Steiner
73941314790SJack Steiner if (!gru_check_chiplet_assignment(gru, gts)) {
74055484c45SJack Steiner STAT(check_context_unload);
741*643a16a0SZheng Wang ret = -EINVAL;
74255484c45SJack Steiner } else if (gru_retarget_intr(gts)) {
74355484c45SJack Steiner STAT(check_context_retarget_intr);
74455484c45SJack Steiner }
745*643a16a0SZheng Wang
746*643a16a0SZheng Wang return ret;
74755484c45SJack Steiner }
74855484c45SJack Steiner
7499a0deeccSJack Steiner
7509a0deeccSJack Steiner /*
7519a0deeccSJack Steiner * Insufficient GRU resources available on the local blade. Steal a context from
7529a0deeccSJack Steiner * a process. This is a hack until a _real_ resource scheduler is written....
7539a0deeccSJack Steiner */
7549a0deeccSJack Steiner #define next_ctxnum(n) ((n) < GRU_NUM_CCH - 2 ? (n) + 1 : 0)
7559a0deeccSJack Steiner #define next_gru(b, g) (((g) < &(b)->bs_grus[GRU_CHIPLETS_PER_BLADE - 1]) ? \
7569a0deeccSJack Steiner ((g)+1) : &(b)->bs_grus[0])
7579a0deeccSJack Steiner
is_gts_stealable(struct gru_thread_state * gts,struct gru_blade_state * bs)758836ce679SJack Steiner static int is_gts_stealable(struct gru_thread_state *gts,
759836ce679SJack Steiner struct gru_blade_state *bs)
760836ce679SJack Steiner {
761836ce679SJack Steiner if (is_kernel_context(gts))
762836ce679SJack Steiner return down_write_trylock(&bs->bs_kgts_sema);
763836ce679SJack Steiner else
764836ce679SJack Steiner return mutex_trylock(>s->ts_ctxlock);
765836ce679SJack Steiner }
766836ce679SJack Steiner
gts_stolen(struct gru_thread_state * gts,struct gru_blade_state * bs)767836ce679SJack Steiner static void gts_stolen(struct gru_thread_state *gts,
768836ce679SJack Steiner struct gru_blade_state *bs)
769836ce679SJack Steiner {
770836ce679SJack Steiner if (is_kernel_context(gts)) {
771836ce679SJack Steiner up_write(&bs->bs_kgts_sema);
772836ce679SJack Steiner STAT(steal_kernel_context);
773836ce679SJack Steiner } else {
774836ce679SJack Steiner mutex_unlock(>s->ts_ctxlock);
775836ce679SJack Steiner STAT(steal_user_context);
776836ce679SJack Steiner }
777836ce679SJack Steiner }
778836ce679SJack Steiner
gru_steal_context(struct gru_thread_state * gts)77955484c45SJack Steiner void gru_steal_context(struct gru_thread_state *gts)
7809a0deeccSJack Steiner {
7819a0deeccSJack Steiner struct gru_blade_state *blade;
7829a0deeccSJack Steiner struct gru_state *gru, *gru0;
7839a0deeccSJack Steiner struct gru_thread_state *ngts = NULL;
7849a0deeccSJack Steiner int ctxnum, ctxnum0, flag = 0, cbr, dsr;
78541314790SJack Steiner int blade_id;
7869a0deeccSJack Steiner
78741314790SJack Steiner blade_id = gts->ts_user_blade_id;
78855484c45SJack Steiner if (blade_id < 0)
78955484c45SJack Steiner blade_id = uv_numa_blade_id();
7909a0deeccSJack Steiner cbr = gts->ts_cbr_au_count;
7919a0deeccSJack Steiner dsr = gts->ts_dsr_au_count;
7929a0deeccSJack Steiner
793364b76dfSJack Steiner blade = gru_base[blade_id];
7949a0deeccSJack Steiner spin_lock(&blade->bs_lock);
7959a0deeccSJack Steiner
7969a0deeccSJack Steiner ctxnum = next_ctxnum(blade->bs_lru_ctxnum);
7979a0deeccSJack Steiner gru = blade->bs_lru_gru;
7989a0deeccSJack Steiner if (ctxnum == 0)
7999a0deeccSJack Steiner gru = next_gru(blade, gru);
80055484c45SJack Steiner blade->bs_lru_gru = gru;
80155484c45SJack Steiner blade->bs_lru_ctxnum = ctxnum;
8029a0deeccSJack Steiner ctxnum0 = ctxnum;
8039a0deeccSJack Steiner gru0 = gru;
8049a0deeccSJack Steiner while (1) {
80541314790SJack Steiner if (gru_check_chiplet_assignment(gru, gts)) {
8069a0deeccSJack Steiner if (check_gru_resources(gru, cbr, dsr, GRU_NUM_CCH))
8079a0deeccSJack Steiner break;
8089a0deeccSJack Steiner spin_lock(&gru->gs_lock);
8099a0deeccSJack Steiner for (; ctxnum < GRU_NUM_CCH; ctxnum++) {
8109a0deeccSJack Steiner if (flag && gru == gru0 && ctxnum == ctxnum0)
8119a0deeccSJack Steiner break;
8129a0deeccSJack Steiner ngts = gru->gs_gts[ctxnum];
8139a0deeccSJack Steiner /*
8149a0deeccSJack Steiner * We are grabbing locks out of order, so trylock is
8159a0deeccSJack Steiner * needed. GTSs are usually not locked, so the odds of
8169a0deeccSJack Steiner * success are high. If trylock fails, try to steal a
8179a0deeccSJack Steiner * different GSEG.
8189a0deeccSJack Steiner */
819836ce679SJack Steiner if (ngts && is_gts_stealable(ngts, blade))
8209a0deeccSJack Steiner break;
8219a0deeccSJack Steiner ngts = NULL;
8229a0deeccSJack Steiner }
8239a0deeccSJack Steiner spin_unlock(&gru->gs_lock);
8249a0deeccSJack Steiner if (ngts || (flag && gru == gru0 && ctxnum == ctxnum0))
8259a0deeccSJack Steiner break;
82655484c45SJack Steiner }
82755484c45SJack Steiner if (flag && gru == gru0)
82855484c45SJack Steiner break;
82955484c45SJack Steiner flag = 1;
8309a0deeccSJack Steiner ctxnum = 0;
8319a0deeccSJack Steiner gru = next_gru(blade, gru);
8329a0deeccSJack Steiner }
8339a0deeccSJack Steiner spin_unlock(&blade->bs_lock);
8349a0deeccSJack Steiner
8359a0deeccSJack Steiner if (ngts) {
8367e796a72SJack Steiner gts->ustats.context_stolen++;
8379a0deeccSJack Steiner ngts->ts_steal_jiffies = jiffies;
838836ce679SJack Steiner gru_unload_context(ngts, is_kernel_context(ngts) ? 0 : 1);
839836ce679SJack Steiner gts_stolen(ngts, blade);
8409a0deeccSJack Steiner } else {
8419a0deeccSJack Steiner STAT(steal_context_failed);
8429a0deeccSJack Steiner }
8439a0deeccSJack Steiner gru_dbg(grudev,
84443884604SJack Steiner "stole gid %d, ctxnum %d from gts %p. Need cb %d, ds %d;"
8459a0deeccSJack Steiner " avail cb %ld, ds %ld\n",
8469a0deeccSJack Steiner gru->gs_gid, ctxnum, ngts, cbr, dsr, hweight64(gru->gs_cbr_map),
8479a0deeccSJack Steiner hweight64(gru->gs_dsr_map));
8489a0deeccSJack Steiner }
8499a0deeccSJack Steiner
8509a0deeccSJack Steiner /*
85155484c45SJack Steiner * Assign a gru context.
85255484c45SJack Steiner */
gru_assign_context_number(struct gru_state * gru)85355484c45SJack Steiner static int gru_assign_context_number(struct gru_state *gru)
85455484c45SJack Steiner {
85555484c45SJack Steiner int ctxnum;
85655484c45SJack Steiner
85755484c45SJack Steiner ctxnum = find_first_zero_bit(&gru->gs_context_map, GRU_NUM_CCH);
85855484c45SJack Steiner __set_bit(ctxnum, &gru->gs_context_map);
85955484c45SJack Steiner return ctxnum;
86055484c45SJack Steiner }
86155484c45SJack Steiner
86255484c45SJack Steiner /*
8639a0deeccSJack Steiner * Scan the GRUs on the local blade & assign a GRU context.
8649a0deeccSJack Steiner */
gru_assign_gru_context(struct gru_thread_state * gts)86555484c45SJack Steiner struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts)
8669a0deeccSJack Steiner {
8679a0deeccSJack Steiner struct gru_state *gru, *grux;
8689a0deeccSJack Steiner int i, max_active_contexts;
86955484c45SJack Steiner int blade_id = gts->ts_user_blade_id;
8709a0deeccSJack Steiner
87155484c45SJack Steiner if (blade_id < 0)
87255484c45SJack Steiner blade_id = uv_numa_blade_id();
8739a0deeccSJack Steiner again:
8749a0deeccSJack Steiner gru = NULL;
8759a0deeccSJack Steiner max_active_contexts = GRU_NUM_CCH;
87655484c45SJack Steiner for_each_gru_on_blade(grux, blade_id, i) {
87741314790SJack Steiner if (!gru_check_chiplet_assignment(grux, gts))
87855484c45SJack Steiner continue;
8799a0deeccSJack Steiner if (check_gru_resources(grux, gts->ts_cbr_au_count,
8809a0deeccSJack Steiner gts->ts_dsr_au_count,
8819a0deeccSJack Steiner max_active_contexts)) {
8829a0deeccSJack Steiner gru = grux;
8839a0deeccSJack Steiner max_active_contexts = grux->gs_active_contexts;
8849a0deeccSJack Steiner if (max_active_contexts == 0)
8859a0deeccSJack Steiner break;
8869a0deeccSJack Steiner }
8879a0deeccSJack Steiner }
8889a0deeccSJack Steiner
8899a0deeccSJack Steiner if (gru) {
8909a0deeccSJack Steiner spin_lock(&gru->gs_lock);
8919a0deeccSJack Steiner if (!check_gru_resources(gru, gts->ts_cbr_au_count,
8929a0deeccSJack Steiner gts->ts_dsr_au_count, GRU_NUM_CCH)) {
8939a0deeccSJack Steiner spin_unlock(&gru->gs_lock);
8949a0deeccSJack Steiner goto again;
8959a0deeccSJack Steiner }
8969a0deeccSJack Steiner reserve_gru_resources(gru, gts);
8979a0deeccSJack Steiner gts->ts_gru = gru;
89887419412SJack Steiner gts->ts_blade = gru->gs_blade_id;
89955484c45SJack Steiner gts->ts_ctxnum = gru_assign_context_number(gru);
90003acb0c5SXiyu Yang refcount_inc(>s->ts_refcnt);
9019a0deeccSJack Steiner gru->gs_gts[gts->ts_ctxnum] = gts;
9029a0deeccSJack Steiner spin_unlock(&gru->gs_lock);
9039a0deeccSJack Steiner
9049a0deeccSJack Steiner STAT(assign_context);
9059a0deeccSJack Steiner gru_dbg(grudev,
90643884604SJack Steiner "gseg %p, gts %p, gid %d, ctx %d, cbr %d, dsr %d\n",
9079a0deeccSJack Steiner gseg_virtual_address(gts->ts_gru, gts->ts_ctxnum), gts,
9089a0deeccSJack Steiner gts->ts_gru->gs_gid, gts->ts_ctxnum,
9099a0deeccSJack Steiner gts->ts_cbr_au_count, gts->ts_dsr_au_count);
9109a0deeccSJack Steiner } else {
9119a0deeccSJack Steiner gru_dbg(grudev, "failed to allocate a GTS %s\n", "");
9129a0deeccSJack Steiner STAT(assign_context_failed);
9139a0deeccSJack Steiner }
9149a0deeccSJack Steiner
9159a0deeccSJack Steiner return gru;
9169a0deeccSJack Steiner }
9179a0deeccSJack Steiner
9189a0deeccSJack Steiner /*
9199a0deeccSJack Steiner * gru_nopage
9209a0deeccSJack Steiner *
9219a0deeccSJack Steiner * Map the user's GRU segment
9229ca8e40cSJack Steiner *
9239ca8e40cSJack Steiner * Note: gru segments alway mmaped on GRU_GSEG_PAGESIZE boundaries.
9249a0deeccSJack Steiner */
gru_fault(struct vm_fault * vmf)9251770a80fSSouptick Joarder vm_fault_t gru_fault(struct vm_fault *vmf)
9269a0deeccSJack Steiner {
92711bac800SDave Jiang struct vm_area_struct *vma = vmf->vma;
9289a0deeccSJack Steiner struct gru_thread_state *gts;
9299a0deeccSJack Steiner unsigned long paddr, vaddr;
930b74afe58SManuel Schölling unsigned long expires;
9319a0deeccSJack Steiner
9321a29d85eSJan Kara vaddr = vmf->address;
9339a0deeccSJack Steiner gru_dbg(grudev, "vma %p, vaddr 0x%lx (0x%lx)\n",
9349a0deeccSJack Steiner vma, vaddr, GSEG_BASE(vaddr));
9359a0deeccSJack Steiner STAT(nopfn);
9369a0deeccSJack Steiner
9379ca8e40cSJack Steiner /* The following check ensures vaddr is a valid address in the VMA */
9389a0deeccSJack Steiner gts = gru_find_thread_state(vma, TSID(vaddr, vma));
9399a0deeccSJack Steiner if (!gts)
9409a0deeccSJack Steiner return VM_FAULT_SIGBUS;
9419a0deeccSJack Steiner
9429a0deeccSJack Steiner again:
9439a0deeccSJack Steiner mutex_lock(>s->ts_ctxlock);
944364b76dfSJack Steiner
945*643a16a0SZheng Wang if (gru_check_context_placement(gts)) {
946*643a16a0SZheng Wang mutex_unlock(>s->ts_ctxlock);
947*643a16a0SZheng Wang gru_unload_context(gts, 1);
948*643a16a0SZheng Wang return VM_FAULT_NOPAGE;
949*643a16a0SZheng Wang }
9509a0deeccSJack Steiner
9519a0deeccSJack Steiner if (!gts->ts_gru) {
952836ce679SJack Steiner STAT(load_user_context);
95355484c45SJack Steiner if (!gru_assign_gru_context(gts)) {
954364b76dfSJack Steiner mutex_unlock(>s->ts_ctxlock);
955364b76dfSJack Steiner set_current_state(TASK_INTERRUPTIBLE);
9569a0deeccSJack Steiner schedule_timeout(GRU_ASSIGN_DELAY); /* true hack ZZZ */
957b74afe58SManuel Schölling expires = gts->ts_steal_jiffies + GRU_STEAL_DELAY;
958b74afe58SManuel Schölling if (time_before(expires, jiffies))
95955484c45SJack Steiner gru_steal_context(gts);
9609a0deeccSJack Steiner goto again;
9619a0deeccSJack Steiner }
9629a0deeccSJack Steiner gru_load_context(gts);
9639a0deeccSJack Steiner paddr = gseg_physical_address(gts->ts_gru, gts->ts_ctxnum);
9649a0deeccSJack Steiner remap_pfn_range(vma, vaddr & ~(GRU_GSEG_PAGESIZE - 1),
9659a0deeccSJack Steiner paddr >> PAGE_SHIFT, GRU_GSEG_PAGESIZE,
9669a0deeccSJack Steiner vma->vm_page_prot);
9679a0deeccSJack Steiner }
9689a0deeccSJack Steiner
969364b76dfSJack Steiner mutex_unlock(>s->ts_ctxlock);
9709a0deeccSJack Steiner
9719a0deeccSJack Steiner return VM_FAULT_NOPAGE;
9729a0deeccSJack Steiner }
9739a0deeccSJack Steiner
974