grumain.c 21 KB

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