gru: dynamic allocation of kernel contexts
[linux-2.6-block.git] / drivers / misc / sgi-gru / grumain.c
CommitLineData
9a0deecc
JS
1/*
2 * SN Platform GRU Driver
3 *
4 * DRIVER TABLE MANAGER + GRU CONTEXT LOAD/UNLOAD
5 *
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file "COPYING" in the main directory of this archive
8 * for more details.
9 *
10 * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
11 */
12
13#include <linux/kernel.h>
14#include <linux/slab.h>
15#include <linux/mm.h>
16#include <linux/spinlock.h>
17#include <linux/sched.h>
18#include <linux/device.h>
19#include <linux/list.h>
20#include <asm/uv/uv_hub.h>
21#include "gru.h"
22#include "grutables.h"
23#include "gruhandles.h"
24
9ca8e40c 25unsigned long gru_options __read_mostly;
9a0deecc
JS
26
27static struct device_driver gru_driver = {
28 .name = "gru"
29};
30
31static struct device gru_device = {
bb0dc43e 32 .init_name = "",
9a0deecc
JS
33 .driver = &gru_driver,
34};
35
36struct device *grudev = &gru_device;
37
38/*
39 * Select a gru fault map to be used by the current cpu. Note that
40 * multiple cpus may be using the same map.
41 * ZZZ should "shift" be used?? Depends on HT cpu numbering
42 * ZZZ should be inline but did not work on emulator
43 */
44int gru_cpu_fault_map_id(void)
45{
46 return uv_blade_processor_id() % GRU_NUM_TFM;
47}
48
49/*--------- ASID Management -------------------------------------------
50 *
51 * Initially, assign asids sequentially from MIN_ASID .. MAX_ASID.
52 * Once MAX is reached, flush the TLB & start over. However,
53 * some asids may still be in use. There won't be many (percentage wise) still
54 * in use. Search active contexts & determine the value of the first
55 * asid in use ("x"s below). Set "limit" to this value.
56 * This defines a block of assignable asids.
57 *
58 * When "limit" is reached, search forward from limit+1 and determine the
59 * next block of assignable asids.
60 *
61 * Repeat until MAX_ASID is reached, then start over again.
62 *
63 * Each time MAX_ASID is reached, increment the asid generation. Since
64 * the search for in-use asids only checks contexts with GRUs currently
65 * assigned, asids in some contexts will be missed. Prior to loading
66 * a context, the asid generation of the GTS asid is rechecked. If it
67 * doesn't match the current generation, a new asid will be assigned.
68 *
69 * 0---------------x------------x---------------------x----|
70 * ^-next ^-limit ^-MAX_ASID
71 *
72 * All asid manipulation & context loading/unloading is protected by the
73 * gs_lock.
74 */
75
76/* Hit the asid limit. Start over */
77static int gru_wrap_asid(struct gru_state *gru)
78{
43884604 79 gru_dbg(grudev, "gid %d\n", gru->gs_gid);
9a0deecc
JS
80 STAT(asid_wrap);
81 gru->gs_asid_gen++;
9a0deecc
JS
82 return MIN_ASID;
83}
84
85/* Find the next chunk of unused asids */
86static int gru_reset_asid_limit(struct gru_state *gru, int asid)
87{
88 int i, gid, inuse_asid, limit;
89
43884604 90 gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid);
9a0deecc
JS
91 STAT(asid_next);
92 limit = MAX_ASID;
93 if (asid >= limit)
94 asid = gru_wrap_asid(gru);
87419412 95 gru_flush_all_tlb(gru);
9a0deecc
JS
96 gid = gru->gs_gid;
97again:
98 for (i = 0; i < GRU_NUM_CCH; i++) {
99 if (!gru->gs_gts[i])
100 continue;
101 inuse_asid = gru->gs_gts[i]->ts_gms->ms_asids[gid].mt_asid;
43884604
JS
102 gru_dbg(grudev, "gid %d, gts %p, gms %p, inuse 0x%x, cxt %d\n",
103 gru->gs_gid, gru->gs_gts[i], gru->gs_gts[i]->ts_gms,
104 inuse_asid, i);
9a0deecc
JS
105 if (inuse_asid == asid) {
106 asid += ASID_INC;
107 if (asid >= limit) {
108 /*
109 * empty range: reset the range limit and
110 * start over
111 */
112 limit = MAX_ASID;
113 if (asid >= MAX_ASID)
114 asid = gru_wrap_asid(gru);
115 goto again;
116 }
117 }
118
119 if ((inuse_asid > asid) && (inuse_asid < limit))
120 limit = inuse_asid;
121 }
122 gru->gs_asid_limit = limit;
123 gru->gs_asid = asid;
43884604
JS
124 gru_dbg(grudev, "gid %d, new asid 0x%x, new_limit 0x%x\n", gru->gs_gid,
125 asid, limit);
9a0deecc
JS
126 return asid;
127}
128
129/* Assign a new ASID to a thread context. */
130static int gru_assign_asid(struct gru_state *gru)
131{
132 int asid;
133
9a0deecc
JS
134 gru->gs_asid += ASID_INC;
135 asid = gru->gs_asid;
136 if (asid >= gru->gs_asid_limit)
137 asid = gru_reset_asid_limit(gru, asid);
9a0deecc 138
43884604 139 gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid);
9a0deecc
JS
140 return asid;
141}
142
143/*
144 * Clear n bits in a word. Return a word indicating the bits that were cleared.
145 * Optionally, build an array of chars that contain the bit numbers allocated.
146 */
147static unsigned long reserve_resources(unsigned long *p, int n, int mmax,
148 char *idx)
149{
150 unsigned long bits = 0;
151 int i;
152
153 do {
154 i = find_first_bit(p, mmax);
155 if (i == mmax)
156 BUG();
157 __clear_bit(i, p);
158 __set_bit(i, &bits);
159 if (idx)
160 *idx++ = i;
161 } while (--n);
162 return bits;
163}
164
9ca8e40c 165unsigned long gru_reserve_cb_resources(struct gru_state *gru, int cbr_au_count,
9a0deecc
JS
166 char *cbmap)
167{
168 return reserve_resources(&gru->gs_cbr_map, cbr_au_count, GRU_CBR_AU,
169 cbmap);
170}
171
9ca8e40c 172unsigned long gru_reserve_ds_resources(struct gru_state *gru, int dsr_au_count,
9a0deecc
JS
173 char *dsmap)
174{
175 return reserve_resources(&gru->gs_dsr_map, dsr_au_count, GRU_DSR_AU,
176 dsmap);
177}
178
179static void reserve_gru_resources(struct gru_state *gru,
180 struct gru_thread_state *gts)
181{
182 gru->gs_active_contexts++;
183 gts->ts_cbr_map =
9ca8e40c 184 gru_reserve_cb_resources(gru, gts->ts_cbr_au_count,
9a0deecc
JS
185 gts->ts_cbr_idx);
186 gts->ts_dsr_map =
9ca8e40c 187 gru_reserve_ds_resources(gru, gts->ts_dsr_au_count, NULL);
9a0deecc
JS
188}
189
190static void free_gru_resources(struct gru_state *gru,
191 struct gru_thread_state *gts)
192{
193 gru->gs_active_contexts--;
194 gru->gs_cbr_map |= gts->ts_cbr_map;
195 gru->gs_dsr_map |= gts->ts_dsr_map;
196}
197
198/*
199 * Check if a GRU has sufficient free resources to satisfy an allocation
200 * request. Note: GRU locks may or may not be held when this is called. If
201 * not held, recheck after acquiring the appropriate locks.
202 *
203 * Returns 1 if sufficient resources, 0 if not
204 */
205static int check_gru_resources(struct gru_state *gru, int cbr_au_count,
206 int dsr_au_count, int max_active_contexts)
207{
208 return hweight64(gru->gs_cbr_map) >= cbr_au_count
209 && hweight64(gru->gs_dsr_map) >= dsr_au_count
210 && gru->gs_active_contexts < max_active_contexts;
211}
212
213/*
214 * TLB manangment requires tracking all GRU chiplets that have loaded a GSEG
215 * context.
216 */
43884604
JS
217static int gru_load_mm_tracker(struct gru_state *gru,
218 struct gru_thread_state *gts)
9a0deecc 219{
43884604 220 struct gru_mm_struct *gms = gts->ts_gms;
9a0deecc 221 struct gru_mm_tracker *asids = &gms->ms_asids[gru->gs_gid];
43884604 222 unsigned short ctxbitmap = (1 << gts->ts_ctxnum);
9a0deecc
JS
223 int asid;
224
225 spin_lock(&gms->ms_asid_lock);
226 asid = asids->mt_asid;
227
87419412
JS
228 spin_lock(&gru->gs_asid_lock);
229 if (asid == 0 || (asids->mt_ctxbitmap == 0 && asids->mt_asid_gen !=
230 gru->gs_asid_gen)) {
9a0deecc
JS
231 asid = gru_assign_asid(gru);
232 asids->mt_asid = asid;
233 asids->mt_asid_gen = gru->gs_asid_gen;
234 STAT(asid_new);
235 } else {
236 STAT(asid_reuse);
237 }
87419412 238 spin_unlock(&gru->gs_asid_lock);
9a0deecc
JS
239
240 BUG_ON(asids->mt_ctxbitmap & ctxbitmap);
241 asids->mt_ctxbitmap |= ctxbitmap;
242 if (!test_bit(gru->gs_gid, gms->ms_asidmap))
243 __set_bit(gru->gs_gid, gms->ms_asidmap);
244 spin_unlock(&gms->ms_asid_lock);
245
246 gru_dbg(grudev,
43884604
JS
247 "gid %d, gts %p, gms %p, ctxnum %d, asid 0x%x, asidmap 0x%lx\n",
248 gru->gs_gid, gts, gms, gts->ts_ctxnum, asid,
249 gms->ms_asidmap[0]);
9a0deecc
JS
250 return asid;
251}
252
253static void gru_unload_mm_tracker(struct gru_state *gru,
43884604 254 struct gru_thread_state *gts)
9a0deecc 255{
43884604 256 struct gru_mm_struct *gms = gts->ts_gms;
9a0deecc
JS
257 struct gru_mm_tracker *asids;
258 unsigned short ctxbitmap;
259
260 asids = &gms->ms_asids[gru->gs_gid];
43884604 261 ctxbitmap = (1 << gts->ts_ctxnum);
9a0deecc 262 spin_lock(&gms->ms_asid_lock);
87419412 263 spin_lock(&gru->gs_asid_lock);
9a0deecc
JS
264 BUG_ON((asids->mt_ctxbitmap & ctxbitmap) != ctxbitmap);
265 asids->mt_ctxbitmap ^= ctxbitmap;
43884604
JS
266 gru_dbg(grudev, "gid %d, gts %p, gms %p, ctxnum 0x%d, asidmap 0x%lx\n",
267 gru->gs_gid, gts, gms, gts->ts_ctxnum, gms->ms_asidmap[0]);
87419412 268 spin_unlock(&gru->gs_asid_lock);
9a0deecc
JS
269 spin_unlock(&gms->ms_asid_lock);
270}
271
272/*
273 * Decrement the reference count on a GTS structure. Free the structure
274 * if the reference count goes to zero.
275 */
276void gts_drop(struct gru_thread_state *gts)
277{
278 if (gts && atomic_dec_return(&gts->ts_refcnt) == 0) {
279 gru_drop_mmu_notifier(gts->ts_gms);
280 kfree(gts);
281 STAT(gts_free);
282 }
283}
284
285/*
286 * Locate the GTS structure for the current thread.
287 */
288static struct gru_thread_state *gru_find_current_gts_nolock(struct gru_vma_data
289 *vdata, int tsid)
290{
291 struct gru_thread_state *gts;
292
293 list_for_each_entry(gts, &vdata->vd_head, ts_next)
294 if (gts->ts_tsid == tsid)
295 return gts;
296 return NULL;
297}
298
299/*
300 * Allocate a thread state structure.
301 */
364b76df
JS
302struct gru_thread_state *gru_alloc_gts(struct vm_area_struct *vma,
303 int cbr_au_count, int dsr_au_count, int options, int tsid)
9a0deecc
JS
304{
305 struct gru_thread_state *gts;
306 int bytes;
307
364b76df 308 bytes = DSR_BYTES(dsr_au_count) + CBR_BYTES(cbr_au_count);
9a0deecc
JS
309 bytes += sizeof(struct gru_thread_state);
310 gts = kzalloc(bytes, GFP_KERNEL);
311 if (!gts)
312 return NULL;
313
314 STAT(gts_alloc);
315 atomic_set(&gts->ts_refcnt, 1);
316 mutex_init(&gts->ts_ctxlock);
364b76df
JS
317 gts->ts_cbr_au_count = cbr_au_count;
318 gts->ts_dsr_au_count = dsr_au_count;
319 gts->ts_user_options = options;
9a0deecc 320 gts->ts_tsid = tsid;
9a0deecc 321 gts->ts_ctxnum = NULLCTX;
9a0deecc 322 gts->ts_tlb_int_select = -1;
7b8274e9 323 gts->ts_sizeavail = GRU_SIZEAVAIL(PAGE_SHIFT);
364b76df
JS
324 if (vma) {
325 gts->ts_mm = current->mm;
326 gts->ts_vma = vma;
327 gts->ts_gms = gru_register_mmu_notifier();
328 if (!gts->ts_gms)
329 goto err;
330 }
9a0deecc 331
364b76df 332 gru_dbg(grudev, "alloc gts %p\n", gts);
9a0deecc
JS
333 return gts;
334
335err:
336 gts_drop(gts);
337 return NULL;
338}
339
340/*
341 * Allocate a vma private data structure.
342 */
343struct gru_vma_data *gru_alloc_vma_data(struct vm_area_struct *vma, int tsid)
344{
345 struct gru_vma_data *vdata = NULL;
346
347 vdata = kmalloc(sizeof(*vdata), GFP_KERNEL);
348 if (!vdata)
349 return NULL;
350
351 INIT_LIST_HEAD(&vdata->vd_head);
352 spin_lock_init(&vdata->vd_lock);
353 gru_dbg(grudev, "alloc vdata %p\n", vdata);
354 return vdata;
355}
356
357/*
358 * Find the thread state structure for the current thread.
359 */
360struct gru_thread_state *gru_find_thread_state(struct vm_area_struct *vma,
361 int tsid)
362{
363 struct gru_vma_data *vdata = vma->vm_private_data;
364 struct gru_thread_state *gts;
365
366 spin_lock(&vdata->vd_lock);
367 gts = gru_find_current_gts_nolock(vdata, tsid);
368 spin_unlock(&vdata->vd_lock);
369 gru_dbg(grudev, "vma %p, gts %p\n", vma, gts);
370 return gts;
371}
372
373/*
374 * Allocate a new thread state for a GSEG. Note that races may allow
375 * another thread to race to create a gts.
376 */
377struct gru_thread_state *gru_alloc_thread_state(struct vm_area_struct *vma,
378 int tsid)
379{
380 struct gru_vma_data *vdata = vma->vm_private_data;
381 struct gru_thread_state *gts, *ngts;
382
364b76df
JS
383 gts = gru_alloc_gts(vma, vdata->vd_cbr_au_count, vdata->vd_dsr_au_count,
384 vdata->vd_user_options, tsid);
9a0deecc
JS
385 if (!gts)
386 return NULL;
387
388 spin_lock(&vdata->vd_lock);
389 ngts = gru_find_current_gts_nolock(vdata, tsid);
390 if (ngts) {
391 gts_drop(gts);
392 gts = ngts;
393 STAT(gts_double_allocate);
394 } else {
395 list_add(&gts->ts_next, &vdata->vd_head);
396 }
397 spin_unlock(&vdata->vd_lock);
398 gru_dbg(grudev, "vma %p, gts %p\n", vma, gts);
399 return gts;
400}
401
402/*
403 * Free the GRU context assigned to the thread state.
404 */
405static void gru_free_gru_context(struct gru_thread_state *gts)
406{
407 struct gru_state *gru;
408
409 gru = gts->ts_gru;
43884604 410 gru_dbg(grudev, "gts %p, gid %d\n", gts, gru->gs_gid);
9a0deecc
JS
411
412 spin_lock(&gru->gs_lock);
413 gru->gs_gts[gts->ts_ctxnum] = NULL;
414 free_gru_resources(gru, gts);
415 BUG_ON(test_bit(gts->ts_ctxnum, &gru->gs_context_map) == 0);
416 __clear_bit(gts->ts_ctxnum, &gru->gs_context_map);
417 gts->ts_ctxnum = NULLCTX;
418 gts->ts_gru = NULL;
87419412 419 gts->ts_blade = -1;
9a0deecc
JS
420 spin_unlock(&gru->gs_lock);
421
422 gts_drop(gts);
423 STAT(free_context);
424}
425
426/*
427 * Prefetching cachelines help hardware performance.
9ca8e40c 428 * (Strictly a performance enhancement. Not functionally required).
9a0deecc
JS
429 */
430static void prefetch_data(void *p, int num, int stride)
431{
432 while (num-- > 0) {
433 prefetchw(p);
434 p += stride;
435 }
436}
437
438static inline long gru_copy_handle(void *d, void *s)
439{
440 memcpy(d, s, GRU_HANDLE_BYTES);
441 return GRU_HANDLE_BYTES;
442}
443
fe5bb6b0
JS
444static void gru_prefetch_context(void *gseg, void *cb, void *cbe,
445 unsigned long cbrmap, unsigned long length)
9a0deecc 446{
9a0deecc
JS
447 int i, scr;
448
9a0deecc
JS
449 prefetch_data(gseg + GRU_DS_BASE, length / GRU_CACHE_LINE_BYTES,
450 GRU_CACHE_LINE_BYTES);
451
9a0deecc
JS
452 for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
453 prefetch_data(cb, 1, GRU_CACHE_LINE_BYTES);
454 prefetch_data(cbe + i * GRU_HANDLE_STRIDE, 1,
455 GRU_CACHE_LINE_BYTES);
456 cb += GRU_HANDLE_STRIDE;
457 }
923f7f69
JS
458}
459
460static void gru_load_context_data(void *save, void *grubase, int ctxnum,
461 unsigned long cbrmap, unsigned long dsrmap)
462{
463 void *gseg, *cb, *cbe;
464 unsigned long length;
465 int i, scr;
9a0deecc 466
923f7f69 467 gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
9a0deecc 468 cb = gseg + GRU_CB_BASE;
923f7f69
JS
469 cbe = grubase + GRU_CBE_BASE;
470 length = hweight64(dsrmap) * GRU_DSR_AU_BYTES;
471 gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
472
9a0deecc
JS
473 for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
474 save += gru_copy_handle(cb, save);
475 save += gru_copy_handle(cbe + i * GRU_HANDLE_STRIDE, save);
476 cb += GRU_HANDLE_STRIDE;
477 }
478
479 memcpy(gseg + GRU_DS_BASE, save, length);
480}
481
482static void gru_unload_context_data(void *save, void *grubase, int ctxnum,
483 unsigned long cbrmap, unsigned long dsrmap)
484{
485 void *gseg, *cb, *cbe;
486 unsigned long length;
487 int i, scr;
488
489 gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
9a0deecc
JS
490 cb = gseg + GRU_CB_BASE;
491 cbe = grubase + GRU_CBE_BASE;
923f7f69
JS
492 length = hweight64(dsrmap) * GRU_DSR_AU_BYTES;
493 gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
494
9a0deecc
JS
495 for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
496 save += gru_copy_handle(save, cb);
497 save += gru_copy_handle(save, cbe + i * GRU_HANDLE_STRIDE);
498 cb += GRU_HANDLE_STRIDE;
499 }
9a0deecc
JS
500 memcpy(save, gseg + GRU_DS_BASE, length);
501}
502
503void gru_unload_context(struct gru_thread_state *gts, int savestate)
504{
505 struct gru_state *gru = gts->ts_gru;
506 struct gru_context_configuration_handle *cch;
507 int ctxnum = gts->ts_ctxnum;
508
509 zap_vma_ptes(gts->ts_vma, UGRUADDR(gts), GRU_GSEG_PAGESIZE);
510 cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
511
43884604 512 gru_dbg(grudev, "gts %p\n", gts);
9a0deecc
JS
513 lock_cch_handle(cch);
514 if (cch_interrupt_sync(cch))
515 BUG();
9a0deecc 516
43884604 517 gru_unload_mm_tracker(gru, gts);
9a0deecc
JS
518 if (savestate)
519 gru_unload_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr,
520 ctxnum, gts->ts_cbr_map,
521 gts->ts_dsr_map);
522
523 if (cch_deallocate(cch))
524 BUG();
525 gts->ts_force_unload = 0; /* ts_force_unload locked by CCH lock */
526 unlock_cch_handle(cch);
527
528 gru_free_gru_context(gts);
529 STAT(unload_context);
530}
531
532/*
533 * Load a GRU context by copying it from the thread data structure in memory
534 * to the GRU.
535 */
536static void gru_load_context(struct gru_thread_state *gts)
537{
538 struct gru_state *gru = gts->ts_gru;
539 struct gru_context_configuration_handle *cch;
540 int err, asid, ctxnum = gts->ts_ctxnum;
541
542 gru_dbg(grudev, "gts %p\n", gts);
543 cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
544
545 lock_cch_handle(cch);
43884604 546 asid = gru_load_mm_tracker(gru, gts);
9a0deecc
JS
547 cch->tfm_fault_bit_enable =
548 (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
549 || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
550 cch->tlb_int_enable = (gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
551 if (cch->tlb_int_enable) {
552 gts->ts_tlb_int_select = gru_cpu_fault_map_id();
553 cch->tlb_int_select = gts->ts_tlb_int_select;
554 }
555 cch->tfm_done_bit_enable = 0;
7b8274e9
JS
556 err = cch_allocate(cch, asid, gts->ts_sizeavail, gts->ts_cbr_map,
557 gts->ts_dsr_map);
9a0deecc
JS
558 if (err) {
559 gru_dbg(grudev,
560 "err %d: cch %p, gts %p, cbr 0x%lx, dsr 0x%lx\n",
561 err, cch, gts, gts->ts_cbr_map, gts->ts_dsr_map);
562 BUG();
563 }
564
565 gru_load_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, ctxnum,
566 gts->ts_cbr_map, gts->ts_dsr_map);
567
568 if (cch_start(cch))
569 BUG();
570 unlock_cch_handle(cch);
571
572 STAT(load_context);
573}
574
575/*
576 * Update fields in an active CCH:
577 * - retarget interrupts on local blade
7b8274e9 578 * - update sizeavail mask
9a0deecc
JS
579 * - force a delayed context unload by clearing the CCH asids. This
580 * forces TLB misses for new GRU instructions. The context is unloaded
581 * when the next TLB miss occurs.
582 */
7b8274e9 583int gru_update_cch(struct gru_thread_state *gts, int force_unload)
9a0deecc
JS
584{
585 struct gru_context_configuration_handle *cch;
586 struct gru_state *gru = gts->ts_gru;
587 int i, ctxnum = gts->ts_ctxnum, ret = 0;
588
589 cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
590
591 lock_cch_handle(cch);
592 if (cch->state == CCHSTATE_ACTIVE) {
593 if (gru->gs_gts[gts->ts_ctxnum] != gts)
594 goto exit;
595 if (cch_interrupt(cch))
596 BUG();
7b8274e9
JS
597 if (!force_unload) {
598 for (i = 0; i < 8; i++)
599 cch->sizeavail[i] = gts->ts_sizeavail;
600 gts->ts_tlb_int_select = gru_cpu_fault_map_id();
601 cch->tlb_int_select = gru_cpu_fault_map_id();
cd1334f0
JS
602 cch->tfm_fault_bit_enable =
603 (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
604 || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
9a0deecc
JS
605 } else {
606 for (i = 0; i < 8; i++)
607 cch->asid[i] = 0;
608 cch->tfm_fault_bit_enable = 0;
609 cch->tlb_int_enable = 0;
610 gts->ts_force_unload = 1;
611 }
612 if (cch_start(cch))
613 BUG();
614 ret = 1;
615 }
616exit:
617 unlock_cch_handle(cch);
618 return ret;
619}
620
621/*
622 * Update CCH tlb interrupt select. Required when all the following is true:
623 * - task's GRU context is loaded into a GRU
624 * - task is using interrupt notification for TLB faults
625 * - task has migrated to a different cpu on the same blade where
626 * it was previously running.
627 */
628static int gru_retarget_intr(struct gru_thread_state *gts)
629{
630 if (gts->ts_tlb_int_select < 0
631 || gts->ts_tlb_int_select == gru_cpu_fault_map_id())
632 return 0;
633
634 gru_dbg(grudev, "retarget from %d to %d\n", gts->ts_tlb_int_select,
635 gru_cpu_fault_map_id());
7b8274e9 636 return gru_update_cch(gts, 0);
9a0deecc
JS
637}
638
639
640/*
641 * Insufficient GRU resources available on the local blade. Steal a context from
642 * a process. This is a hack until a _real_ resource scheduler is written....
643 */
644#define next_ctxnum(n) ((n) < GRU_NUM_CCH - 2 ? (n) + 1 : 0)
645#define next_gru(b, g) (((g) < &(b)->bs_grus[GRU_CHIPLETS_PER_BLADE - 1]) ? \
646 ((g)+1) : &(b)->bs_grus[0])
647
364b76df 648static void gru_steal_context(struct gru_thread_state *gts, int blade_id)
9a0deecc
JS
649{
650 struct gru_blade_state *blade;
651 struct gru_state *gru, *gru0;
652 struct gru_thread_state *ngts = NULL;
653 int ctxnum, ctxnum0, flag = 0, cbr, dsr;
654
655 cbr = gts->ts_cbr_au_count;
656 dsr = gts->ts_dsr_au_count;
657
364b76df 658 blade = gru_base[blade_id];
9a0deecc
JS
659 spin_lock(&blade->bs_lock);
660
661 ctxnum = next_ctxnum(blade->bs_lru_ctxnum);
662 gru = blade->bs_lru_gru;
663 if (ctxnum == 0)
664 gru = next_gru(blade, gru);
665 ctxnum0 = ctxnum;
666 gru0 = gru;
667 while (1) {
668 if (check_gru_resources(gru, cbr, dsr, GRU_NUM_CCH))
669 break;
670 spin_lock(&gru->gs_lock);
671 for (; ctxnum < GRU_NUM_CCH; ctxnum++) {
672 if (flag && gru == gru0 && ctxnum == ctxnum0)
673 break;
674 ngts = gru->gs_gts[ctxnum];
675 /*
676 * We are grabbing locks out of order, so trylock is
677 * needed. GTSs are usually not locked, so the odds of
678 * success are high. If trylock fails, try to steal a
679 * different GSEG.
680 */
681 if (ngts && mutex_trylock(&ngts->ts_ctxlock))
682 break;
683 ngts = NULL;
684 flag = 1;
685 }
686 spin_unlock(&gru->gs_lock);
687 if (ngts || (flag && gru == gru0 && ctxnum == ctxnum0))
688 break;
689 ctxnum = 0;
690 gru = next_gru(blade, gru);
691 }
692 blade->bs_lru_gru = gru;
693 blade->bs_lru_ctxnum = ctxnum;
694 spin_unlock(&blade->bs_lock);
9a0deecc
JS
695
696 if (ngts) {
697 STAT(steal_context);
698 ngts->ts_steal_jiffies = jiffies;
699 gru_unload_context(ngts, 1);
700 mutex_unlock(&ngts->ts_ctxlock);
701 } else {
702 STAT(steal_context_failed);
703 }
704 gru_dbg(grudev,
43884604 705 "stole gid %d, ctxnum %d from gts %p. Need cb %d, ds %d;"
9a0deecc
JS
706 " avail cb %ld, ds %ld\n",
707 gru->gs_gid, ctxnum, ngts, cbr, dsr, hweight64(gru->gs_cbr_map),
708 hweight64(gru->gs_dsr_map));
709}
710
711/*
712 * Scan the GRUs on the local blade & assign a GRU context.
713 */
364b76df
JS
714static struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts,
715 int blade)
9a0deecc
JS
716{
717 struct gru_state *gru, *grux;
718 int i, max_active_contexts;
719
9a0deecc
JS
720
721again:
722 gru = NULL;
723 max_active_contexts = GRU_NUM_CCH;
364b76df 724 for_each_gru_on_blade(grux, blade, i) {
9a0deecc
JS
725 if (check_gru_resources(grux, gts->ts_cbr_au_count,
726 gts->ts_dsr_au_count,
727 max_active_contexts)) {
728 gru = grux;
729 max_active_contexts = grux->gs_active_contexts;
730 if (max_active_contexts == 0)
731 break;
732 }
733 }
734
735 if (gru) {
736 spin_lock(&gru->gs_lock);
737 if (!check_gru_resources(gru, gts->ts_cbr_au_count,
738 gts->ts_dsr_au_count, GRU_NUM_CCH)) {
739 spin_unlock(&gru->gs_lock);
740 goto again;
741 }
742 reserve_gru_resources(gru, gts);
743 gts->ts_gru = gru;
87419412 744 gts->ts_blade = gru->gs_blade_id;
9a0deecc
JS
745 gts->ts_ctxnum =
746 find_first_zero_bit(&gru->gs_context_map, GRU_NUM_CCH);
747 BUG_ON(gts->ts_ctxnum == GRU_NUM_CCH);
748 atomic_inc(&gts->ts_refcnt);
749 gru->gs_gts[gts->ts_ctxnum] = gts;
750 __set_bit(gts->ts_ctxnum, &gru->gs_context_map);
751 spin_unlock(&gru->gs_lock);
752
753 STAT(assign_context);
754 gru_dbg(grudev,
43884604 755 "gseg %p, gts %p, gid %d, ctx %d, cbr %d, dsr %d\n",
9a0deecc
JS
756 gseg_virtual_address(gts->ts_gru, gts->ts_ctxnum), gts,
757 gts->ts_gru->gs_gid, gts->ts_ctxnum,
758 gts->ts_cbr_au_count, gts->ts_dsr_au_count);
759 } else {
760 gru_dbg(grudev, "failed to allocate a GTS %s\n", "");
761 STAT(assign_context_failed);
762 }
763
9a0deecc
JS
764 return gru;
765}
766
767/*
768 * gru_nopage
769 *
770 * Map the user's GRU segment
9ca8e40c
JS
771 *
772 * Note: gru segments alway mmaped on GRU_GSEG_PAGESIZE boundaries.
9a0deecc
JS
773 */
774int gru_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
775{
776 struct gru_thread_state *gts;
777 unsigned long paddr, vaddr;
364b76df 778 int blade_id;
9a0deecc
JS
779
780 vaddr = (unsigned long)vmf->virtual_address;
781 gru_dbg(grudev, "vma %p, vaddr 0x%lx (0x%lx)\n",
782 vma, vaddr, GSEG_BASE(vaddr));
783 STAT(nopfn);
784
9ca8e40c 785 /* The following check ensures vaddr is a valid address in the VMA */
9a0deecc
JS
786 gts = gru_find_thread_state(vma, TSID(vaddr, vma));
787 if (!gts)
788 return VM_FAULT_SIGBUS;
789
790again:
9a0deecc 791 mutex_lock(&gts->ts_ctxlock);
fe5bb6b0 792 preempt_disable();
364b76df
JS
793 blade_id = uv_numa_blade_id();
794
9a0deecc 795 if (gts->ts_gru) {
364b76df 796 if (gts->ts_gru->gs_blade_id != blade_id) {
9a0deecc
JS
797 STAT(migrated_nopfn_unload);
798 gru_unload_context(gts, 1);
799 } else {
800 if (gru_retarget_intr(gts))
801 STAT(migrated_nopfn_retarget);
802 }
803 }
804
805 if (!gts->ts_gru) {
364b76df 806 if (!gru_assign_gru_context(gts, blade_id)) {
9a0deecc 807 preempt_enable();
364b76df
JS
808 mutex_unlock(&gts->ts_ctxlock);
809 set_current_state(TASK_INTERRUPTIBLE);
9a0deecc 810 schedule_timeout(GRU_ASSIGN_DELAY); /* true hack ZZZ */
364b76df 811 blade_id = uv_numa_blade_id();
9a0deecc 812 if (gts->ts_steal_jiffies + GRU_STEAL_DELAY < jiffies)
364b76df 813 gru_steal_context(gts, blade_id);
9a0deecc
JS
814 goto again;
815 }
816 gru_load_context(gts);
817 paddr = gseg_physical_address(gts->ts_gru, gts->ts_ctxnum);
818 remap_pfn_range(vma, vaddr & ~(GRU_GSEG_PAGESIZE - 1),
819 paddr >> PAGE_SHIFT, GRU_GSEG_PAGESIZE,
820 vma->vm_page_prot);
821 }
822
9a0deecc 823 preempt_enable();
364b76df 824 mutex_unlock(&gts->ts_ctxlock);
9a0deecc
JS
825
826 return VM_FAULT_NOPAGE;
827}
828