blob: 120c70c5a28a1c45bcad5d9e89161cc45d6f952a [file] [log] [blame]
Jack Steiner9a0deec2008-07-29 22:33:58 -07001/*
2 * SN Platform GRU Driver
3 *
4 * DRIVER TABLE MANAGER + GRU CONTEXT LOAD/UNLOAD
5 *
Jack Steiner8820f272009-06-17 16:28:36 -07006 * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
Jack Steiner9a0deec2008-07-29 22:33:58 -07007 *
Jack Steiner8820f272009-06-17 16:28:36 -07008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Jack Steiner9a0deec2008-07-29 22:33:58 -070021 */
22
23#include <linux/kernel.h>
24#include <linux/slab.h>
25#include <linux/mm.h>
26#include <linux/spinlock.h>
27#include <linux/sched.h>
28#include <linux/device.h>
29#include <linux/list.h>
Jack Steiner7f2251b2009-12-15 16:48:08 -080030#include <linux/err.h>
Jack Steiner9a0deec2008-07-29 22:33:58 -070031#include <asm/uv/uv_hub.h>
32#include "gru.h"
33#include "grutables.h"
34#include "gruhandles.h"
35
Jack Steiner9ca8e40c12008-07-29 22:34:02 -070036unsigned long gru_options __read_mostly;
Jack Steiner9a0deec2008-07-29 22:33:58 -070037
38static struct device_driver gru_driver = {
39 .name = "gru"
40};
41
42static struct device gru_device = {
Kay Sieversbb0dc432009-01-06 10:44:37 -080043 .init_name = "",
Jack Steiner9a0deec2008-07-29 22:33:58 -070044 .driver = &gru_driver,
45};
46
47struct device *grudev = &gru_device;
48
49/*
50 * Select a gru fault map to be used by the current cpu. Note that
51 * multiple cpus may be using the same map.
Jack Steiner9a0deec2008-07-29 22:33:58 -070052 * ZZZ should be inline but did not work on emulator
53 */
54int gru_cpu_fault_map_id(void)
55{
Jack Steiner4107e1d2009-12-15 16:48:11 -080056 int cpu = smp_processor_id();
57 int id, core;
58
59 core = uv_cpu_core_number(cpu);
60 id = core + UV_MAX_INT_CORES * uv_cpu_socket_number(cpu);
61 return id;
Jack Steiner9a0deec2008-07-29 22:33:58 -070062}
63
64/*--------- ASID Management -------------------------------------------
65 *
66 * Initially, assign asids sequentially from MIN_ASID .. MAX_ASID.
67 * Once MAX is reached, flush the TLB & start over. However,
68 * some asids may still be in use. There won't be many (percentage wise) still
69 * in use. Search active contexts & determine the value of the first
70 * asid in use ("x"s below). Set "limit" to this value.
71 * This defines a block of assignable asids.
72 *
73 * When "limit" is reached, search forward from limit+1 and determine the
74 * next block of assignable asids.
75 *
76 * Repeat until MAX_ASID is reached, then start over again.
77 *
78 * Each time MAX_ASID is reached, increment the asid generation. Since
79 * the search for in-use asids only checks contexts with GRUs currently
80 * assigned, asids in some contexts will be missed. Prior to loading
81 * a context, the asid generation of the GTS asid is rechecked. If it
82 * doesn't match the current generation, a new asid will be assigned.
83 *
84 * 0---------------x------------x---------------------x----|
85 * ^-next ^-limit ^-MAX_ASID
86 *
87 * All asid manipulation & context loading/unloading is protected by the
88 * gs_lock.
89 */
90
91/* Hit the asid limit. Start over */
92static int gru_wrap_asid(struct gru_state *gru)
93{
Jack Steiner43884602009-04-02 16:59:05 -070094 gru_dbg(grudev, "gid %d\n", gru->gs_gid);
Jack Steiner9a0deec2008-07-29 22:33:58 -070095 STAT(asid_wrap);
96 gru->gs_asid_gen++;
Jack Steiner9a0deec2008-07-29 22:33:58 -070097 return MIN_ASID;
98}
99
100/* Find the next chunk of unused asids */
101static int gru_reset_asid_limit(struct gru_state *gru, int asid)
102{
103 int i, gid, inuse_asid, limit;
104
Jack Steiner43884602009-04-02 16:59:05 -0700105 gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700106 STAT(asid_next);
107 limit = MAX_ASID;
108 if (asid >= limit)
109 asid = gru_wrap_asid(gru);
Jack Steiner87419412009-04-02 16:59:08 -0700110 gru_flush_all_tlb(gru);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700111 gid = gru->gs_gid;
112again:
113 for (i = 0; i < GRU_NUM_CCH; i++) {
Jack Steiner836ce672009-06-17 16:28:22 -0700114 if (!gru->gs_gts[i] || is_kernel_context(gru->gs_gts[i]))
Jack Steiner9a0deec2008-07-29 22:33:58 -0700115 continue;
116 inuse_asid = gru->gs_gts[i]->ts_gms->ms_asids[gid].mt_asid;
Jack Steiner43884602009-04-02 16:59:05 -0700117 gru_dbg(grudev, "gid %d, gts %p, gms %p, inuse 0x%x, cxt %d\n",
118 gru->gs_gid, gru->gs_gts[i], gru->gs_gts[i]->ts_gms,
119 inuse_asid, i);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700120 if (inuse_asid == asid) {
121 asid += ASID_INC;
122 if (asid >= limit) {
123 /*
124 * empty range: reset the range limit and
125 * start over
126 */
127 limit = MAX_ASID;
128 if (asid >= MAX_ASID)
129 asid = gru_wrap_asid(gru);
130 goto again;
131 }
132 }
133
134 if ((inuse_asid > asid) && (inuse_asid < limit))
135 limit = inuse_asid;
136 }
137 gru->gs_asid_limit = limit;
138 gru->gs_asid = asid;
Jack Steiner43884602009-04-02 16:59:05 -0700139 gru_dbg(grudev, "gid %d, new asid 0x%x, new_limit 0x%x\n", gru->gs_gid,
140 asid, limit);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700141 return asid;
142}
143
144/* Assign a new ASID to a thread context. */
145static int gru_assign_asid(struct gru_state *gru)
146{
147 int asid;
148
Jack Steiner9a0deec2008-07-29 22:33:58 -0700149 gru->gs_asid += ASID_INC;
150 asid = gru->gs_asid;
151 if (asid >= gru->gs_asid_limit)
152 asid = gru_reset_asid_limit(gru, asid);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700153
Jack Steiner43884602009-04-02 16:59:05 -0700154 gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700155 return asid;
156}
157
158/*
159 * Clear n bits in a word. Return a word indicating the bits that were cleared.
160 * Optionally, build an array of chars that contain the bit numbers allocated.
161 */
162static unsigned long reserve_resources(unsigned long *p, int n, int mmax,
163 char *idx)
164{
165 unsigned long bits = 0;
166 int i;
167
Jack Steiner3eac2e92009-06-17 16:28:23 -0700168 while (n--) {
Jack Steiner9a0deec2008-07-29 22:33:58 -0700169 i = find_first_bit(p, mmax);
170 if (i == mmax)
171 BUG();
172 __clear_bit(i, p);
173 __set_bit(i, &bits);
174 if (idx)
175 *idx++ = i;
Jack Steiner3eac2e92009-06-17 16:28:23 -0700176 }
Jack Steiner9a0deec2008-07-29 22:33:58 -0700177 return bits;
178}
179
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700180unsigned long gru_reserve_cb_resources(struct gru_state *gru, int cbr_au_count,
Jack Steiner9a0deec2008-07-29 22:33:58 -0700181 char *cbmap)
182{
183 return reserve_resources(&gru->gs_cbr_map, cbr_au_count, GRU_CBR_AU,
184 cbmap);
185}
186
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700187unsigned long gru_reserve_ds_resources(struct gru_state *gru, int dsr_au_count,
Jack Steiner9a0deec2008-07-29 22:33:58 -0700188 char *dsmap)
189{
190 return reserve_resources(&gru->gs_dsr_map, dsr_au_count, GRU_DSR_AU,
191 dsmap);
192}
193
194static void reserve_gru_resources(struct gru_state *gru,
195 struct gru_thread_state *gts)
196{
197 gru->gs_active_contexts++;
198 gts->ts_cbr_map =
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700199 gru_reserve_cb_resources(gru, gts->ts_cbr_au_count,
Jack Steiner9a0deec2008-07-29 22:33:58 -0700200 gts->ts_cbr_idx);
201 gts->ts_dsr_map =
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700202 gru_reserve_ds_resources(gru, gts->ts_dsr_au_count, NULL);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700203}
204
205static void free_gru_resources(struct gru_state *gru,
206 struct gru_thread_state *gts)
207{
208 gru->gs_active_contexts--;
209 gru->gs_cbr_map |= gts->ts_cbr_map;
210 gru->gs_dsr_map |= gts->ts_dsr_map;
211}
212
213/*
214 * Check if a GRU has sufficient free resources to satisfy an allocation
215 * request. Note: GRU locks may or may not be held when this is called. If
216 * not held, recheck after acquiring the appropriate locks.
217 *
218 * Returns 1 if sufficient resources, 0 if not
219 */
220static int check_gru_resources(struct gru_state *gru, int cbr_au_count,
221 int dsr_au_count, int max_active_contexts)
222{
223 return hweight64(gru->gs_cbr_map) >= cbr_au_count
224 && hweight64(gru->gs_dsr_map) >= dsr_au_count
225 && gru->gs_active_contexts < max_active_contexts;
226}
227
228/*
229 * TLB manangment requires tracking all GRU chiplets that have loaded a GSEG
230 * context.
231 */
Jack Steiner43884602009-04-02 16:59:05 -0700232static int gru_load_mm_tracker(struct gru_state *gru,
233 struct gru_thread_state *gts)
Jack Steiner9a0deec2008-07-29 22:33:58 -0700234{
Jack Steiner43884602009-04-02 16:59:05 -0700235 struct gru_mm_struct *gms = gts->ts_gms;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700236 struct gru_mm_tracker *asids = &gms->ms_asids[gru->gs_gid];
Jack Steiner43884602009-04-02 16:59:05 -0700237 unsigned short ctxbitmap = (1 << gts->ts_ctxnum);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700238 int asid;
239
240 spin_lock(&gms->ms_asid_lock);
241 asid = asids->mt_asid;
242
Jack Steiner87419412009-04-02 16:59:08 -0700243 spin_lock(&gru->gs_asid_lock);
244 if (asid == 0 || (asids->mt_ctxbitmap == 0 && asids->mt_asid_gen !=
245 gru->gs_asid_gen)) {
Jack Steiner9a0deec2008-07-29 22:33:58 -0700246 asid = gru_assign_asid(gru);
247 asids->mt_asid = asid;
248 asids->mt_asid_gen = gru->gs_asid_gen;
249 STAT(asid_new);
250 } else {
251 STAT(asid_reuse);
252 }
Jack Steiner87419412009-04-02 16:59:08 -0700253 spin_unlock(&gru->gs_asid_lock);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700254
255 BUG_ON(asids->mt_ctxbitmap & ctxbitmap);
256 asids->mt_ctxbitmap |= ctxbitmap;
257 if (!test_bit(gru->gs_gid, gms->ms_asidmap))
258 __set_bit(gru->gs_gid, gms->ms_asidmap);
259 spin_unlock(&gms->ms_asid_lock);
260
261 gru_dbg(grudev,
Jack Steiner43884602009-04-02 16:59:05 -0700262 "gid %d, gts %p, gms %p, ctxnum %d, asid 0x%x, asidmap 0x%lx\n",
263 gru->gs_gid, gts, gms, gts->ts_ctxnum, asid,
264 gms->ms_asidmap[0]);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700265 return asid;
266}
267
268static void gru_unload_mm_tracker(struct gru_state *gru,
Jack Steiner43884602009-04-02 16:59:05 -0700269 struct gru_thread_state *gts)
Jack Steiner9a0deec2008-07-29 22:33:58 -0700270{
Jack Steiner43884602009-04-02 16:59:05 -0700271 struct gru_mm_struct *gms = gts->ts_gms;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700272 struct gru_mm_tracker *asids;
273 unsigned short ctxbitmap;
274
275 asids = &gms->ms_asids[gru->gs_gid];
Jack Steiner43884602009-04-02 16:59:05 -0700276 ctxbitmap = (1 << gts->ts_ctxnum);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700277 spin_lock(&gms->ms_asid_lock);
Jack Steiner87419412009-04-02 16:59:08 -0700278 spin_lock(&gru->gs_asid_lock);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700279 BUG_ON((asids->mt_ctxbitmap & ctxbitmap) != ctxbitmap);
280 asids->mt_ctxbitmap ^= ctxbitmap;
Jack Steiner43884602009-04-02 16:59:05 -0700281 gru_dbg(grudev, "gid %d, gts %p, gms %p, ctxnum 0x%d, asidmap 0x%lx\n",
282 gru->gs_gid, gts, gms, gts->ts_ctxnum, gms->ms_asidmap[0]);
Jack Steiner87419412009-04-02 16:59:08 -0700283 spin_unlock(&gru->gs_asid_lock);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700284 spin_unlock(&gms->ms_asid_lock);
285}
286
287/*
288 * Decrement the reference count on a GTS structure. Free the structure
289 * if the reference count goes to zero.
290 */
291void gts_drop(struct gru_thread_state *gts)
292{
293 if (gts && atomic_dec_return(&gts->ts_refcnt) == 0) {
Jack Steiner7f2251b2009-12-15 16:48:08 -0800294 if (gts->ts_gms)
295 gru_drop_mmu_notifier(gts->ts_gms);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700296 kfree(gts);
297 STAT(gts_free);
298 }
299}
300
301/*
302 * Locate the GTS structure for the current thread.
303 */
304static struct gru_thread_state *gru_find_current_gts_nolock(struct gru_vma_data
305 *vdata, int tsid)
306{
307 struct gru_thread_state *gts;
308
309 list_for_each_entry(gts, &vdata->vd_head, ts_next)
310 if (gts->ts_tsid == tsid)
311 return gts;
312 return NULL;
313}
314
315/*
316 * Allocate a thread state structure.
317 */
Jack Steiner364b76d2009-06-17 16:28:20 -0700318struct gru_thread_state *gru_alloc_gts(struct vm_area_struct *vma,
319 int cbr_au_count, int dsr_au_count, int options, int tsid)
Jack Steiner9a0deec2008-07-29 22:33:58 -0700320{
321 struct gru_thread_state *gts;
Jack Steiner7f2251b2009-12-15 16:48:08 -0800322 struct gru_mm_struct *gms;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700323 int bytes;
324
Jack Steiner364b76d2009-06-17 16:28:20 -0700325 bytes = DSR_BYTES(dsr_au_count) + CBR_BYTES(cbr_au_count);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700326 bytes += sizeof(struct gru_thread_state);
Jack Steiner940229b2009-06-17 16:28:24 -0700327 gts = kmalloc(bytes, GFP_KERNEL);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700328 if (!gts)
Jack Steiner7f2251b2009-12-15 16:48:08 -0800329 return ERR_PTR(-ENOMEM);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700330
331 STAT(gts_alloc);
Jack Steiner940229b2009-06-17 16:28:24 -0700332 memset(gts, 0, sizeof(struct gru_thread_state)); /* zero out header */
Jack Steiner9a0deec2008-07-29 22:33:58 -0700333 atomic_set(&gts->ts_refcnt, 1);
334 mutex_init(&gts->ts_ctxlock);
Jack Steiner364b76d2009-06-17 16:28:20 -0700335 gts->ts_cbr_au_count = cbr_au_count;
336 gts->ts_dsr_au_count = dsr_au_count;
337 gts->ts_user_options = options;
Jack Steiner518e5cd2009-12-15 16:48:04 -0800338 gts->ts_user_blade_id = -1;
339 gts->ts_user_chiplet_id = -1;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700340 gts->ts_tsid = tsid;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700341 gts->ts_ctxnum = NULLCTX;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700342 gts->ts_tlb_int_select = -1;
Jack Steinerb1b19fc2009-06-17 16:28:33 -0700343 gts->ts_cch_req_slice = -1;
Jack Steiner7b8274e2009-04-02 16:59:12 -0700344 gts->ts_sizeavail = GRU_SIZEAVAIL(PAGE_SHIFT);
Jack Steiner364b76d2009-06-17 16:28:20 -0700345 if (vma) {
346 gts->ts_mm = current->mm;
347 gts->ts_vma = vma;
Jack Steiner7f2251b2009-12-15 16:48:08 -0800348 gms = gru_register_mmu_notifier();
349 if (IS_ERR(gms))
Jack Steiner364b76d2009-06-17 16:28:20 -0700350 goto err;
Jack Steiner7f2251b2009-12-15 16:48:08 -0800351 gts->ts_gms = gms;
Jack Steiner364b76d2009-06-17 16:28:20 -0700352 }
Jack Steiner9a0deec2008-07-29 22:33:58 -0700353
Jack Steiner364b76d2009-06-17 16:28:20 -0700354 gru_dbg(grudev, "alloc gts %p\n", gts);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700355 return gts;
356
357err:
358 gts_drop(gts);
Jack Steiner7f2251b2009-12-15 16:48:08 -0800359 return ERR_CAST(gms);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700360}
361
362/*
363 * Allocate a vma private data structure.
364 */
365struct gru_vma_data *gru_alloc_vma_data(struct vm_area_struct *vma, int tsid)
366{
367 struct gru_vma_data *vdata = NULL;
368
369 vdata = kmalloc(sizeof(*vdata), GFP_KERNEL);
370 if (!vdata)
371 return NULL;
372
373 INIT_LIST_HEAD(&vdata->vd_head);
374 spin_lock_init(&vdata->vd_lock);
375 gru_dbg(grudev, "alloc vdata %p\n", vdata);
376 return vdata;
377}
378
379/*
380 * Find the thread state structure for the current thread.
381 */
382struct gru_thread_state *gru_find_thread_state(struct vm_area_struct *vma,
383 int tsid)
384{
385 struct gru_vma_data *vdata = vma->vm_private_data;
386 struct gru_thread_state *gts;
387
388 spin_lock(&vdata->vd_lock);
389 gts = gru_find_current_gts_nolock(vdata, tsid);
390 spin_unlock(&vdata->vd_lock);
391 gru_dbg(grudev, "vma %p, gts %p\n", vma, gts);
392 return gts;
393}
394
395/*
396 * Allocate a new thread state for a GSEG. Note that races may allow
397 * another thread to race to create a gts.
398 */
399struct gru_thread_state *gru_alloc_thread_state(struct vm_area_struct *vma,
400 int tsid)
401{
402 struct gru_vma_data *vdata = vma->vm_private_data;
403 struct gru_thread_state *gts, *ngts;
404
Jack Steiner364b76d2009-06-17 16:28:20 -0700405 gts = gru_alloc_gts(vma, vdata->vd_cbr_au_count, vdata->vd_dsr_au_count,
406 vdata->vd_user_options, tsid);
Jack Steiner7f2251b2009-12-15 16:48:08 -0800407 if (IS_ERR(gts))
408 return gts;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700409
410 spin_lock(&vdata->vd_lock);
411 ngts = gru_find_current_gts_nolock(vdata, tsid);
412 if (ngts) {
413 gts_drop(gts);
414 gts = ngts;
415 STAT(gts_double_allocate);
416 } else {
417 list_add(&gts->ts_next, &vdata->vd_head);
418 }
419 spin_unlock(&vdata->vd_lock);
420 gru_dbg(grudev, "vma %p, gts %p\n", vma, gts);
421 return gts;
422}
423
424/*
425 * Free the GRU context assigned to the thread state.
426 */
427static void gru_free_gru_context(struct gru_thread_state *gts)
428{
429 struct gru_state *gru;
430
431 gru = gts->ts_gru;
Jack Steiner43884602009-04-02 16:59:05 -0700432 gru_dbg(grudev, "gts %p, gid %d\n", gts, gru->gs_gid);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700433
434 spin_lock(&gru->gs_lock);
435 gru->gs_gts[gts->ts_ctxnum] = NULL;
436 free_gru_resources(gru, gts);
437 BUG_ON(test_bit(gts->ts_ctxnum, &gru->gs_context_map) == 0);
438 __clear_bit(gts->ts_ctxnum, &gru->gs_context_map);
439 gts->ts_ctxnum = NULLCTX;
440 gts->ts_gru = NULL;
Jack Steiner87419412009-04-02 16:59:08 -0700441 gts->ts_blade = -1;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700442 spin_unlock(&gru->gs_lock);
443
444 gts_drop(gts);
445 STAT(free_context);
446}
447
448/*
449 * Prefetching cachelines help hardware performance.
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700450 * (Strictly a performance enhancement. Not functionally required).
Jack Steiner9a0deec2008-07-29 22:33:58 -0700451 */
452static void prefetch_data(void *p, int num, int stride)
453{
454 while (num-- > 0) {
455 prefetchw(p);
456 p += stride;
457 }
458}
459
460static inline long gru_copy_handle(void *d, void *s)
461{
462 memcpy(d, s, GRU_HANDLE_BYTES);
463 return GRU_HANDLE_BYTES;
464}
465
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700466static void gru_prefetch_context(void *gseg, void *cb, void *cbe,
467 unsigned long cbrmap, unsigned long length)
Jack Steiner923f7f62008-10-15 22:05:13 -0700468{
469 int i, scr;
470
471 prefetch_data(gseg + GRU_DS_BASE, length / GRU_CACHE_LINE_BYTES,
472 GRU_CACHE_LINE_BYTES);
473
474 for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
475 prefetch_data(cb, 1, GRU_CACHE_LINE_BYTES);
476 prefetch_data(cbe + i * GRU_HANDLE_STRIDE, 1,
477 GRU_CACHE_LINE_BYTES);
478 cb += GRU_HANDLE_STRIDE;
479 }
480}
481
Jack Steiner9a0deec2008-07-29 22:33:58 -0700482static void gru_load_context_data(void *save, void *grubase, int ctxnum,
Jack Steiner940229b2009-06-17 16:28:24 -0700483 unsigned long cbrmap, unsigned long dsrmap,
484 int data_valid)
Jack Steiner9a0deec2008-07-29 22:33:58 -0700485{
486 void *gseg, *cb, *cbe;
487 unsigned long length;
488 int i, scr;
489
490 gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700491 cb = gseg + GRU_CB_BASE;
492 cbe = grubase + GRU_CBE_BASE;
Jack Steiner923f7f62008-10-15 22:05:13 -0700493 length = hweight64(dsrmap) * GRU_DSR_AU_BYTES;
494 gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700495
Jack Steiner9a0deec2008-07-29 22:33:58 -0700496 for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
Jack Steiner940229b2009-06-17 16:28:24 -0700497 if (data_valid) {
498 save += gru_copy_handle(cb, save);
499 save += gru_copy_handle(cbe + i * GRU_HANDLE_STRIDE,
500 save);
501 } else {
502 memset(cb, 0, GRU_CACHE_LINE_BYTES);
503 memset(cbe + i * GRU_HANDLE_STRIDE, 0,
504 GRU_CACHE_LINE_BYTES);
505 }
Jack Steiner67bf04a2009-12-15 16:48:11 -0800506 /* Flush CBE to hide race in context restart */
507 mb();
508 gru_flush_cache(cbe + i * GRU_HANDLE_STRIDE);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700509 cb += GRU_HANDLE_STRIDE;
510 }
511
Jack Steiner940229b2009-06-17 16:28:24 -0700512 if (data_valid)
513 memcpy(gseg + GRU_DS_BASE, save, length);
514 else
515 memset(gseg + GRU_DS_BASE, 0, length);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700516}
517
518static void gru_unload_context_data(void *save, void *grubase, int ctxnum,
519 unsigned long cbrmap, unsigned long dsrmap)
520{
521 void *gseg, *cb, *cbe;
522 unsigned long length;
523 int i, scr;
524
525 gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700526 cb = gseg + GRU_CB_BASE;
527 cbe = grubase + GRU_CBE_BASE;
Jack Steiner923f7f62008-10-15 22:05:13 -0700528 length = hweight64(dsrmap) * GRU_DSR_AU_BYTES;
Jack Steiner67bf04a2009-12-15 16:48:11 -0800529
530 /* CBEs may not be coherent. Flush them from cache */
531 for_each_cbr_in_allocation_map(i, &cbrmap, scr)
532 gru_flush_cache(cbe + i * GRU_HANDLE_STRIDE);
533 mb(); /* Let the CL flush complete */
534
Jack Steiner923f7f62008-10-15 22:05:13 -0700535 gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
536
Jack Steiner9a0deec2008-07-29 22:33:58 -0700537 for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
538 save += gru_copy_handle(save, cb);
539 save += gru_copy_handle(save, cbe + i * GRU_HANDLE_STRIDE);
540 cb += GRU_HANDLE_STRIDE;
541 }
Jack Steiner9a0deec2008-07-29 22:33:58 -0700542 memcpy(save, gseg + GRU_DS_BASE, length);
543}
544
545void gru_unload_context(struct gru_thread_state *gts, int savestate)
546{
547 struct gru_state *gru = gts->ts_gru;
548 struct gru_context_configuration_handle *cch;
549 int ctxnum = gts->ts_ctxnum;
550
Jack Steiner836ce672009-06-17 16:28:22 -0700551 if (!is_kernel_context(gts))
552 zap_vma_ptes(gts->ts_vma, UGRUADDR(gts), GRU_GSEG_PAGESIZE);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700553 cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
554
Jack Steiner43884602009-04-02 16:59:05 -0700555 gru_dbg(grudev, "gts %p\n", gts);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700556 lock_cch_handle(cch);
557 if (cch_interrupt_sync(cch))
558 BUG();
Jack Steiner9a0deec2008-07-29 22:33:58 -0700559
Jack Steiner836ce672009-06-17 16:28:22 -0700560 if (!is_kernel_context(gts))
561 gru_unload_mm_tracker(gru, gts);
Jack Steiner940229b2009-06-17 16:28:24 -0700562 if (savestate) {
Jack Steiner9a0deec2008-07-29 22:33:58 -0700563 gru_unload_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr,
564 ctxnum, gts->ts_cbr_map,
565 gts->ts_dsr_map);
Jack Steiner940229b2009-06-17 16:28:24 -0700566 gts->ts_data_valid = 1;
567 }
Jack Steiner9a0deec2008-07-29 22:33:58 -0700568
569 if (cch_deallocate(cch))
570 BUG();
Jack Steiner9a0deec2008-07-29 22:33:58 -0700571 unlock_cch_handle(cch);
572
573 gru_free_gru_context(gts);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700574}
575
576/*
577 * Load a GRU context by copying it from the thread data structure in memory
578 * to the GRU.
579 */
Jack Steinerd57c82b2009-06-17 16:28:20 -0700580void gru_load_context(struct gru_thread_state *gts)
Jack Steiner9a0deec2008-07-29 22:33:58 -0700581{
582 struct gru_state *gru = gts->ts_gru;
583 struct gru_context_configuration_handle *cch;
Jack Steiner6e910072009-06-17 16:28:21 -0700584 int i, err, asid, ctxnum = gts->ts_ctxnum;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700585
586 gru_dbg(grudev, "gts %p\n", gts);
587 cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
588
589 lock_cch_handle(cch);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700590 cch->tfm_fault_bit_enable =
591 (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
592 || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
593 cch->tlb_int_enable = (gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
594 if (cch->tlb_int_enable) {
595 gts->ts_tlb_int_select = gru_cpu_fault_map_id();
596 cch->tlb_int_select = gts->ts_tlb_int_select;
597 }
Jack Steinerb1b19fc2009-06-17 16:28:33 -0700598 if (gts->ts_cch_req_slice >= 0) {
599 cch->req_slice_set_enable = 1;
600 cch->req_slice = gts->ts_cch_req_slice;
601 } else {
602 cch->req_slice_set_enable =0;
603 }
Jack Steiner9a0deec2008-07-29 22:33:58 -0700604 cch->tfm_done_bit_enable = 0;
Jack Steiner6e910072009-06-17 16:28:21 -0700605 cch->dsr_allocation_map = gts->ts_dsr_map;
606 cch->cbr_allocation_map = gts->ts_cbr_map;
Jack Steiner836ce672009-06-17 16:28:22 -0700607
608 if (is_kernel_context(gts)) {
609 cch->unmap_enable = 1;
Jack Steiner4a7a17c2009-06-17 16:28:25 -0700610 cch->tfm_done_bit_enable = 1;
611 cch->cb_int_enable = 1;
Jack Steiner4107e1d2009-12-15 16:48:11 -0800612 cch->tlb_int_select = 0; /* For now, ints go to cpu 0 */
Jack Steiner836ce672009-06-17 16:28:22 -0700613 } else {
614 cch->unmap_enable = 0;
Jack Steiner4a7a17c2009-06-17 16:28:25 -0700615 cch->tfm_done_bit_enable = 0;
616 cch->cb_int_enable = 0;
Jack Steiner836ce672009-06-17 16:28:22 -0700617 asid = gru_load_mm_tracker(gru, gts);
618 for (i = 0; i < 8; i++) {
619 cch->asid[i] = asid + i;
620 cch->sizeavail[i] = gts->ts_sizeavail;
621 }
Jack Steiner6e910072009-06-17 16:28:21 -0700622 }
623
624 err = cch_allocate(cch);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700625 if (err) {
626 gru_dbg(grudev,
627 "err %d: cch %p, gts %p, cbr 0x%lx, dsr 0x%lx\n",
628 err, cch, gts, gts->ts_cbr_map, gts->ts_dsr_map);
629 BUG();
630 }
631
632 gru_load_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, ctxnum,
Jack Steiner940229b2009-06-17 16:28:24 -0700633 gts->ts_cbr_map, gts->ts_dsr_map, gts->ts_data_valid);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700634
635 if (cch_start(cch))
636 BUG();
637 unlock_cch_handle(cch);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700638}
639
640/*
641 * Update fields in an active CCH:
642 * - retarget interrupts on local blade
Jack Steiner7b8274e2009-04-02 16:59:12 -0700643 * - update sizeavail mask
Jack Steiner9a0deec2008-07-29 22:33:58 -0700644 */
Jack Steiner99f7c222009-12-15 16:48:06 -0800645int gru_update_cch(struct gru_thread_state *gts)
Jack Steiner9a0deec2008-07-29 22:33:58 -0700646{
647 struct gru_context_configuration_handle *cch;
648 struct gru_state *gru = gts->ts_gru;
649 int i, ctxnum = gts->ts_ctxnum, ret = 0;
650
651 cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
652
653 lock_cch_handle(cch);
654 if (cch->state == CCHSTATE_ACTIVE) {
655 if (gru->gs_gts[gts->ts_ctxnum] != gts)
656 goto exit;
657 if (cch_interrupt(cch))
658 BUG();
Jack Steiner99f7c222009-12-15 16:48:06 -0800659 for (i = 0; i < 8; i++)
660 cch->sizeavail[i] = gts->ts_sizeavail;
661 gts->ts_tlb_int_select = gru_cpu_fault_map_id();
662 cch->tlb_int_select = gru_cpu_fault_map_id();
663 cch->tfm_fault_bit_enable =
664 (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
665 || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700666 if (cch_start(cch))
667 BUG();
668 ret = 1;
669 }
670exit:
671 unlock_cch_handle(cch);
672 return ret;
673}
674
675/*
676 * Update CCH tlb interrupt select. Required when all the following is true:
677 * - task's GRU context is loaded into a GRU
678 * - task is using interrupt notification for TLB faults
679 * - task has migrated to a different cpu on the same blade where
680 * it was previously running.
681 */
682static int gru_retarget_intr(struct gru_thread_state *gts)
683{
684 if (gts->ts_tlb_int_select < 0
685 || gts->ts_tlb_int_select == gru_cpu_fault_map_id())
686 return 0;
687
688 gru_dbg(grudev, "retarget from %d to %d\n", gts->ts_tlb_int_select,
689 gru_cpu_fault_map_id());
Jack Steiner99f7c222009-12-15 16:48:06 -0800690 return gru_update_cch(gts);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700691}
692
Jack Steiner55484c42009-12-15 16:48:05 -0800693/*
694 * Unload the gru context if it is not assigned to the correct blade or
695 * chiplet. Misassignment can occur if the process migrates to a different
696 * blade or if the user changes the selected blade/chiplet.
697 * Return 0 if context correct placed, otherwise 1
698 */
699void gru_check_context_placement(struct gru_thread_state *gts)
700{
701 struct gru_state *gru;
702 int blade_id, chiplet_id;
703
704 /*
705 * If the current task is the context owner, verify that the
706 * context is correctly placed. This test is skipped for non-owner
707 * references. Pthread apps use non-owner references to the CBRs.
708 */
709 gru = gts->ts_gru;
710 if (!gru || gts->ts_tgid_owner != current->tgid)
711 return;
712
713 blade_id = gts->ts_user_blade_id;
714 if (blade_id < 0)
715 blade_id = uv_numa_blade_id();
716
717 chiplet_id = gts->ts_user_chiplet_id;
718 if (gru->gs_blade_id != blade_id ||
719 (chiplet_id >= 0 && chiplet_id != gru->gs_chiplet_id)) {
720 STAT(check_context_unload);
721 gru_unload_context(gts, 1);
722 } else if (gru_retarget_intr(gts)) {
723 STAT(check_context_retarget_intr);
724 }
725}
726
Jack Steiner9a0deec2008-07-29 22:33:58 -0700727
728/*
729 * Insufficient GRU resources available on the local blade. Steal a context from
730 * a process. This is a hack until a _real_ resource scheduler is written....
731 */
732#define next_ctxnum(n) ((n) < GRU_NUM_CCH - 2 ? (n) + 1 : 0)
733#define next_gru(b, g) (((g) < &(b)->bs_grus[GRU_CHIPLETS_PER_BLADE - 1]) ? \
734 ((g)+1) : &(b)->bs_grus[0])
735
Jack Steiner836ce672009-06-17 16:28:22 -0700736static int is_gts_stealable(struct gru_thread_state *gts,
737 struct gru_blade_state *bs)
738{
739 if (is_kernel_context(gts))
740 return down_write_trylock(&bs->bs_kgts_sema);
741 else
742 return mutex_trylock(&gts->ts_ctxlock);
743}
744
745static void gts_stolen(struct gru_thread_state *gts,
746 struct gru_blade_state *bs)
747{
748 if (is_kernel_context(gts)) {
749 up_write(&bs->bs_kgts_sema);
750 STAT(steal_kernel_context);
751 } else {
752 mutex_unlock(&gts->ts_ctxlock);
753 STAT(steal_user_context);
754 }
755}
756
Jack Steiner55484c42009-12-15 16:48:05 -0800757void gru_steal_context(struct gru_thread_state *gts)
Jack Steiner9a0deec2008-07-29 22:33:58 -0700758{
759 struct gru_blade_state *blade;
760 struct gru_state *gru, *gru0;
761 struct gru_thread_state *ngts = NULL;
762 int ctxnum, ctxnum0, flag = 0, cbr, dsr;
Jack Steiner55484c42009-12-15 16:48:05 -0800763 int blade_id = gts->ts_user_blade_id;
764 int chiplet_id = gts->ts_user_chiplet_id;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700765
Jack Steiner55484c42009-12-15 16:48:05 -0800766 if (blade_id < 0)
767 blade_id = uv_numa_blade_id();
Jack Steiner9a0deec2008-07-29 22:33:58 -0700768 cbr = gts->ts_cbr_au_count;
769 dsr = gts->ts_dsr_au_count;
770
Jack Steiner364b76d2009-06-17 16:28:20 -0700771 blade = gru_base[blade_id];
Jack Steiner9a0deec2008-07-29 22:33:58 -0700772 spin_lock(&blade->bs_lock);
773
774 ctxnum = next_ctxnum(blade->bs_lru_ctxnum);
775 gru = blade->bs_lru_gru;
776 if (ctxnum == 0)
777 gru = next_gru(blade, gru);
Jack Steiner55484c42009-12-15 16:48:05 -0800778 blade->bs_lru_gru = gru;
779 blade->bs_lru_ctxnum = ctxnum;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700780 ctxnum0 = ctxnum;
781 gru0 = gru;
782 while (1) {
Jack Steiner55484c42009-12-15 16:48:05 -0800783 if (chiplet_id < 0 || chiplet_id == gru->gs_chiplet_id) {
784 if (check_gru_resources(gru, cbr, dsr, GRU_NUM_CCH))
Jack Steiner9a0deec2008-07-29 22:33:58 -0700785 break;
Jack Steiner55484c42009-12-15 16:48:05 -0800786 spin_lock(&gru->gs_lock);
787 for (; ctxnum < GRU_NUM_CCH; ctxnum++) {
788 if (flag && gru == gru0 && ctxnum == ctxnum0)
789 break;
790 ngts = gru->gs_gts[ctxnum];
791 /*
792 * We are grabbing locks out of order, so trylock is
793 * needed. GTSs are usually not locked, so the odds of
794 * success are high. If trylock fails, try to steal a
795 * different GSEG.
796 */
797 if (ngts && is_gts_stealable(ngts, blade))
798 break;
799 ngts = NULL;
800 }
801 spin_unlock(&gru->gs_lock);
802 if (ngts || (flag && gru == gru0 && ctxnum == ctxnum0))
Jack Steiner9a0deec2008-07-29 22:33:58 -0700803 break;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700804 }
Jack Steiner55484c42009-12-15 16:48:05 -0800805 if (flag && gru == gru0)
Jack Steiner9a0deec2008-07-29 22:33:58 -0700806 break;
Jack Steiner55484c42009-12-15 16:48:05 -0800807 flag = 1;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700808 ctxnum = 0;
809 gru = next_gru(blade, gru);
810 }
Jack Steiner9a0deec2008-07-29 22:33:58 -0700811 spin_unlock(&blade->bs_lock);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700812
813 if (ngts) {
Jack Steiner7e796a72009-06-17 16:28:30 -0700814 gts->ustats.context_stolen++;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700815 ngts->ts_steal_jiffies = jiffies;
Jack Steiner836ce672009-06-17 16:28:22 -0700816 gru_unload_context(ngts, is_kernel_context(ngts) ? 0 : 1);
817 gts_stolen(ngts, blade);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700818 } else {
819 STAT(steal_context_failed);
820 }
821 gru_dbg(grudev,
Jack Steiner43884602009-04-02 16:59:05 -0700822 "stole gid %d, ctxnum %d from gts %p. Need cb %d, ds %d;"
Jack Steiner9a0deec2008-07-29 22:33:58 -0700823 " avail cb %ld, ds %ld\n",
824 gru->gs_gid, ctxnum, ngts, cbr, dsr, hweight64(gru->gs_cbr_map),
825 hweight64(gru->gs_dsr_map));
826}
827
828/*
Jack Steiner55484c42009-12-15 16:48:05 -0800829 * Assign a gru context.
830 */
831static int gru_assign_context_number(struct gru_state *gru)
832{
833 int ctxnum;
834
835 ctxnum = find_first_zero_bit(&gru->gs_context_map, GRU_NUM_CCH);
836 __set_bit(ctxnum, &gru->gs_context_map);
837 return ctxnum;
838}
839
840/*
Jack Steiner9a0deec2008-07-29 22:33:58 -0700841 * Scan the GRUs on the local blade & assign a GRU context.
842 */
Jack Steiner55484c42009-12-15 16:48:05 -0800843struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts)
Jack Steiner9a0deec2008-07-29 22:33:58 -0700844{
845 struct gru_state *gru, *grux;
846 int i, max_active_contexts;
Jack Steiner55484c42009-12-15 16:48:05 -0800847 int blade_id = gts->ts_user_blade_id;
848 int chiplet_id = gts->ts_user_chiplet_id;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700849
Jack Steiner55484c42009-12-15 16:48:05 -0800850 if (blade_id < 0)
851 blade_id = uv_numa_blade_id();
Jack Steiner9a0deec2008-07-29 22:33:58 -0700852again:
853 gru = NULL;
854 max_active_contexts = GRU_NUM_CCH;
Jack Steiner55484c42009-12-15 16:48:05 -0800855 for_each_gru_on_blade(grux, blade_id, i) {
856 if (chiplet_id >= 0 && chiplet_id != grux->gs_chiplet_id)
857 continue;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700858 if (check_gru_resources(grux, gts->ts_cbr_au_count,
859 gts->ts_dsr_au_count,
860 max_active_contexts)) {
861 gru = grux;
862 max_active_contexts = grux->gs_active_contexts;
863 if (max_active_contexts == 0)
864 break;
865 }
866 }
867
868 if (gru) {
869 spin_lock(&gru->gs_lock);
870 if (!check_gru_resources(gru, gts->ts_cbr_au_count,
871 gts->ts_dsr_au_count, GRU_NUM_CCH)) {
872 spin_unlock(&gru->gs_lock);
873 goto again;
874 }
875 reserve_gru_resources(gru, gts);
876 gts->ts_gru = gru;
Jack Steiner87419412009-04-02 16:59:08 -0700877 gts->ts_blade = gru->gs_blade_id;
Jack Steiner55484c42009-12-15 16:48:05 -0800878 gts->ts_ctxnum = gru_assign_context_number(gru);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700879 atomic_inc(&gts->ts_refcnt);
880 gru->gs_gts[gts->ts_ctxnum] = gts;
Jack Steiner9a0deec2008-07-29 22:33:58 -0700881 spin_unlock(&gru->gs_lock);
882
883 STAT(assign_context);
884 gru_dbg(grudev,
Jack Steiner43884602009-04-02 16:59:05 -0700885 "gseg %p, gts %p, gid %d, ctx %d, cbr %d, dsr %d\n",
Jack Steiner9a0deec2008-07-29 22:33:58 -0700886 gseg_virtual_address(gts->ts_gru, gts->ts_ctxnum), gts,
887 gts->ts_gru->gs_gid, gts->ts_ctxnum,
888 gts->ts_cbr_au_count, gts->ts_dsr_au_count);
889 } else {
890 gru_dbg(grudev, "failed to allocate a GTS %s\n", "");
891 STAT(assign_context_failed);
892 }
893
Jack Steiner9a0deec2008-07-29 22:33:58 -0700894 return gru;
895}
896
897/*
898 * gru_nopage
899 *
900 * Map the user's GRU segment
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700901 *
902 * Note: gru segments alway mmaped on GRU_GSEG_PAGESIZE boundaries.
Jack Steiner9a0deec2008-07-29 22:33:58 -0700903 */
904int gru_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
905{
906 struct gru_thread_state *gts;
907 unsigned long paddr, vaddr;
908
909 vaddr = (unsigned long)vmf->virtual_address;
910 gru_dbg(grudev, "vma %p, vaddr 0x%lx (0x%lx)\n",
911 vma, vaddr, GSEG_BASE(vaddr));
912 STAT(nopfn);
913
Jack Steiner9ca8e40c12008-07-29 22:34:02 -0700914 /* The following check ensures vaddr is a valid address in the VMA */
Jack Steiner9a0deec2008-07-29 22:33:58 -0700915 gts = gru_find_thread_state(vma, TSID(vaddr, vma));
916 if (!gts)
917 return VM_FAULT_SIGBUS;
918
919again:
Jack Steiner9a0deec2008-07-29 22:33:58 -0700920 mutex_lock(&gts->ts_ctxlock);
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700921 preempt_disable();
Jack Steiner364b76d2009-06-17 16:28:20 -0700922
Jack Steiner55484c42009-12-15 16:48:05 -0800923 gru_check_context_placement(gts);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700924
925 if (!gts->ts_gru) {
Jack Steiner836ce672009-06-17 16:28:22 -0700926 STAT(load_user_context);
Jack Steiner55484c42009-12-15 16:48:05 -0800927 if (!gru_assign_gru_context(gts)) {
Jack Steiner9a0deec2008-07-29 22:33:58 -0700928 preempt_enable();
Jack Steiner364b76d2009-06-17 16:28:20 -0700929 mutex_unlock(&gts->ts_ctxlock);
930 set_current_state(TASK_INTERRUPTIBLE);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700931 schedule_timeout(GRU_ASSIGN_DELAY); /* true hack ZZZ */
932 if (gts->ts_steal_jiffies + GRU_STEAL_DELAY < jiffies)
Jack Steiner55484c42009-12-15 16:48:05 -0800933 gru_steal_context(gts);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700934 goto again;
935 }
936 gru_load_context(gts);
937 paddr = gseg_physical_address(gts->ts_gru, gts->ts_ctxnum);
938 remap_pfn_range(vma, vaddr & ~(GRU_GSEG_PAGESIZE - 1),
939 paddr >> PAGE_SHIFT, GRU_GSEG_PAGESIZE,
940 vma->vm_page_prot);
941 }
942
Jack Steiner9a0deec2008-07-29 22:33:58 -0700943 preempt_enable();
Jack Steiner364b76d2009-06-17 16:28:20 -0700944 mutex_unlock(&gts->ts_ctxlock);
Jack Steiner9a0deec2008-07-29 22:33:58 -0700945
946 return VM_FAULT_NOPAGE;
947}
948