grumain.c 21 KB

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