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