grumain.c 21 KB

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