grumain.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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] || is_kernel_context(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. while (n--) {
  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. }
  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. struct gru_thread_state *gru_alloc_gts(struct vm_area_struct *vma,
  271. int cbr_au_count, int dsr_au_count, int options, int tsid)
  272. {
  273. struct gru_thread_state *gts;
  274. int bytes;
  275. bytes = DSR_BYTES(dsr_au_count) + CBR_BYTES(cbr_au_count);
  276. bytes += sizeof(struct gru_thread_state);
  277. gts = kmalloc(bytes, GFP_KERNEL);
  278. if (!gts)
  279. return NULL;
  280. STAT(gts_alloc);
  281. memset(gts, 0, sizeof(struct gru_thread_state)); /* zero out header */
  282. atomic_set(&gts->ts_refcnt, 1);
  283. mutex_init(&gts->ts_ctxlock);
  284. gts->ts_cbr_au_count = cbr_au_count;
  285. gts->ts_dsr_au_count = dsr_au_count;
  286. gts->ts_user_options = options;
  287. gts->ts_tsid = tsid;
  288. gts->ts_ctxnum = NULLCTX;
  289. gts->ts_tlb_int_select = -1;
  290. gts->ts_sizeavail = GRU_SIZEAVAIL(PAGE_SHIFT);
  291. if (vma) {
  292. gts->ts_mm = current->mm;
  293. gts->ts_vma = vma;
  294. gts->ts_gms = gru_register_mmu_notifier();
  295. if (!gts->ts_gms)
  296. goto err;
  297. }
  298. gru_dbg(grudev, "alloc gts %p\n", gts);
  299. return gts;
  300. err:
  301. gts_drop(gts);
  302. return NULL;
  303. }
  304. /*
  305. * Allocate a vma private data structure.
  306. */
  307. struct gru_vma_data *gru_alloc_vma_data(struct vm_area_struct *vma, int tsid)
  308. {
  309. struct gru_vma_data *vdata = NULL;
  310. vdata = kmalloc(sizeof(*vdata), GFP_KERNEL);
  311. if (!vdata)
  312. return NULL;
  313. INIT_LIST_HEAD(&vdata->vd_head);
  314. spin_lock_init(&vdata->vd_lock);
  315. gru_dbg(grudev, "alloc vdata %p\n", vdata);
  316. return vdata;
  317. }
  318. /*
  319. * Find the thread state structure for the current thread.
  320. */
  321. struct gru_thread_state *gru_find_thread_state(struct vm_area_struct *vma,
  322. int tsid)
  323. {
  324. struct gru_vma_data *vdata = vma->vm_private_data;
  325. struct gru_thread_state *gts;
  326. spin_lock(&vdata->vd_lock);
  327. gts = gru_find_current_gts_nolock(vdata, tsid);
  328. spin_unlock(&vdata->vd_lock);
  329. gru_dbg(grudev, "vma %p, gts %p\n", vma, gts);
  330. return gts;
  331. }
  332. /*
  333. * Allocate a new thread state for a GSEG. Note that races may allow
  334. * another thread to race to create a gts.
  335. */
  336. struct gru_thread_state *gru_alloc_thread_state(struct vm_area_struct *vma,
  337. int tsid)
  338. {
  339. struct gru_vma_data *vdata = vma->vm_private_data;
  340. struct gru_thread_state *gts, *ngts;
  341. gts = gru_alloc_gts(vma, vdata->vd_cbr_au_count, vdata->vd_dsr_au_count,
  342. vdata->vd_user_options, tsid);
  343. if (!gts)
  344. return NULL;
  345. spin_lock(&vdata->vd_lock);
  346. ngts = gru_find_current_gts_nolock(vdata, tsid);
  347. if (ngts) {
  348. gts_drop(gts);
  349. gts = ngts;
  350. STAT(gts_double_allocate);
  351. } else {
  352. list_add(&gts->ts_next, &vdata->vd_head);
  353. }
  354. spin_unlock(&vdata->vd_lock);
  355. gru_dbg(grudev, "vma %p, gts %p\n", vma, gts);
  356. return gts;
  357. }
  358. /*
  359. * Free the GRU context assigned to the thread state.
  360. */
  361. static void gru_free_gru_context(struct gru_thread_state *gts)
  362. {
  363. struct gru_state *gru;
  364. gru = gts->ts_gru;
  365. gru_dbg(grudev, "gts %p, gid %d\n", gts, gru->gs_gid);
  366. spin_lock(&gru->gs_lock);
  367. gru->gs_gts[gts->ts_ctxnum] = NULL;
  368. free_gru_resources(gru, gts);
  369. BUG_ON(test_bit(gts->ts_ctxnum, &gru->gs_context_map) == 0);
  370. __clear_bit(gts->ts_ctxnum, &gru->gs_context_map);
  371. gts->ts_ctxnum = NULLCTX;
  372. gts->ts_gru = NULL;
  373. gts->ts_blade = -1;
  374. spin_unlock(&gru->gs_lock);
  375. gts_drop(gts);
  376. STAT(free_context);
  377. }
  378. /*
  379. * Prefetching cachelines help hardware performance.
  380. * (Strictly a performance enhancement. Not functionally required).
  381. */
  382. static void prefetch_data(void *p, int num, int stride)
  383. {
  384. while (num-- > 0) {
  385. prefetchw(p);
  386. p += stride;
  387. }
  388. }
  389. static inline long gru_copy_handle(void *d, void *s)
  390. {
  391. memcpy(d, s, GRU_HANDLE_BYTES);
  392. return GRU_HANDLE_BYTES;
  393. }
  394. static void gru_prefetch_context(void *gseg, void *cb, void *cbe,
  395. unsigned long cbrmap, unsigned long length)
  396. {
  397. int i, scr;
  398. prefetch_data(gseg + GRU_DS_BASE, length / GRU_CACHE_LINE_BYTES,
  399. GRU_CACHE_LINE_BYTES);
  400. for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
  401. prefetch_data(cb, 1, GRU_CACHE_LINE_BYTES);
  402. prefetch_data(cbe + i * GRU_HANDLE_STRIDE, 1,
  403. GRU_CACHE_LINE_BYTES);
  404. cb += GRU_HANDLE_STRIDE;
  405. }
  406. }
  407. static void gru_load_context_data(void *save, void *grubase, int ctxnum,
  408. unsigned long cbrmap, unsigned long dsrmap,
  409. int data_valid)
  410. {
  411. void *gseg, *cb, *cbe;
  412. unsigned long length;
  413. int i, scr;
  414. gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
  415. cb = gseg + GRU_CB_BASE;
  416. cbe = grubase + GRU_CBE_BASE;
  417. length = hweight64(dsrmap) * GRU_DSR_AU_BYTES;
  418. gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
  419. for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
  420. if (data_valid) {
  421. save += gru_copy_handle(cb, save);
  422. save += gru_copy_handle(cbe + i * GRU_HANDLE_STRIDE,
  423. save);
  424. } else {
  425. memset(cb, 0, GRU_CACHE_LINE_BYTES);
  426. memset(cbe + i * GRU_HANDLE_STRIDE, 0,
  427. GRU_CACHE_LINE_BYTES);
  428. }
  429. cb += GRU_HANDLE_STRIDE;
  430. }
  431. if (data_valid)
  432. memcpy(gseg + GRU_DS_BASE, save, length);
  433. else
  434. memset(gseg + GRU_DS_BASE, 0, length);
  435. }
  436. static void gru_unload_context_data(void *save, void *grubase, int ctxnum,
  437. unsigned long cbrmap, unsigned long dsrmap)
  438. {
  439. void *gseg, *cb, *cbe;
  440. unsigned long length;
  441. int i, scr;
  442. gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
  443. cb = gseg + GRU_CB_BASE;
  444. cbe = grubase + GRU_CBE_BASE;
  445. length = hweight64(dsrmap) * GRU_DSR_AU_BYTES;
  446. gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
  447. for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
  448. save += gru_copy_handle(save, cb);
  449. save += gru_copy_handle(save, cbe + i * GRU_HANDLE_STRIDE);
  450. cb += GRU_HANDLE_STRIDE;
  451. }
  452. memcpy(save, gseg + GRU_DS_BASE, length);
  453. }
  454. void gru_unload_context(struct gru_thread_state *gts, int savestate)
  455. {
  456. struct gru_state *gru = gts->ts_gru;
  457. struct gru_context_configuration_handle *cch;
  458. int ctxnum = gts->ts_ctxnum;
  459. if (!is_kernel_context(gts))
  460. zap_vma_ptes(gts->ts_vma, UGRUADDR(gts), GRU_GSEG_PAGESIZE);
  461. cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
  462. gru_dbg(grudev, "gts %p\n", gts);
  463. lock_cch_handle(cch);
  464. if (cch_interrupt_sync(cch))
  465. BUG();
  466. if (!is_kernel_context(gts))
  467. gru_unload_mm_tracker(gru, gts);
  468. if (savestate) {
  469. gru_unload_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr,
  470. ctxnum, gts->ts_cbr_map,
  471. gts->ts_dsr_map);
  472. gts->ts_data_valid = 1;
  473. }
  474. if (cch_deallocate(cch))
  475. BUG();
  476. gts->ts_force_unload = 0; /* ts_force_unload locked by CCH lock */
  477. unlock_cch_handle(cch);
  478. gru_free_gru_context(gts);
  479. }
  480. /*
  481. * Load a GRU context by copying it from the thread data structure in memory
  482. * to the GRU.
  483. */
  484. void gru_load_context(struct gru_thread_state *gts)
  485. {
  486. struct gru_state *gru = gts->ts_gru;
  487. struct gru_context_configuration_handle *cch;
  488. int i, err, asid, ctxnum = gts->ts_ctxnum;
  489. gru_dbg(grudev, "gts %p\n", gts);
  490. cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
  491. lock_cch_handle(cch);
  492. cch->tfm_fault_bit_enable =
  493. (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
  494. || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
  495. cch->tlb_int_enable = (gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
  496. if (cch->tlb_int_enable) {
  497. gts->ts_tlb_int_select = gru_cpu_fault_map_id();
  498. cch->tlb_int_select = gts->ts_tlb_int_select;
  499. }
  500. cch->tfm_done_bit_enable = 0;
  501. cch->dsr_allocation_map = gts->ts_dsr_map;
  502. cch->cbr_allocation_map = gts->ts_cbr_map;
  503. if (is_kernel_context(gts)) {
  504. cch->unmap_enable = 1;
  505. cch->tfm_done_bit_enable = 1;
  506. cch->cb_int_enable = 1;
  507. } else {
  508. cch->unmap_enable = 0;
  509. cch->tfm_done_bit_enable = 0;
  510. cch->cb_int_enable = 0;
  511. asid = gru_load_mm_tracker(gru, gts);
  512. for (i = 0; i < 8; i++) {
  513. cch->asid[i] = asid + i;
  514. cch->sizeavail[i] = gts->ts_sizeavail;
  515. }
  516. }
  517. err = cch_allocate(cch);
  518. if (err) {
  519. gru_dbg(grudev,
  520. "err %d: cch %p, gts %p, cbr 0x%lx, dsr 0x%lx\n",
  521. err, cch, gts, gts->ts_cbr_map, gts->ts_dsr_map);
  522. BUG();
  523. }
  524. gru_load_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, ctxnum,
  525. gts->ts_cbr_map, gts->ts_dsr_map, gts->ts_data_valid);
  526. if (cch_start(cch))
  527. BUG();
  528. unlock_cch_handle(cch);
  529. }
  530. /*
  531. * Update fields in an active CCH:
  532. * - retarget interrupts on local blade
  533. * - update sizeavail mask
  534. * - force a delayed context unload by clearing the CCH asids. This
  535. * forces TLB misses for new GRU instructions. The context is unloaded
  536. * when the next TLB miss occurs.
  537. */
  538. int gru_update_cch(struct gru_thread_state *gts, int force_unload)
  539. {
  540. struct gru_context_configuration_handle *cch;
  541. struct gru_state *gru = gts->ts_gru;
  542. int i, ctxnum = gts->ts_ctxnum, ret = 0;
  543. cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
  544. lock_cch_handle(cch);
  545. if (cch->state == CCHSTATE_ACTIVE) {
  546. if (gru->gs_gts[gts->ts_ctxnum] != gts)
  547. goto exit;
  548. if (cch_interrupt(cch))
  549. BUG();
  550. if (!force_unload) {
  551. for (i = 0; i < 8; i++)
  552. cch->sizeavail[i] = gts->ts_sizeavail;
  553. gts->ts_tlb_int_select = gru_cpu_fault_map_id();
  554. cch->tlb_int_select = gru_cpu_fault_map_id();
  555. cch->tfm_fault_bit_enable =
  556. (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
  557. || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
  558. } else {
  559. for (i = 0; i < 8; i++)
  560. cch->asid[i] = 0;
  561. cch->tfm_fault_bit_enable = 0;
  562. cch->tlb_int_enable = 0;
  563. gts->ts_force_unload = 1;
  564. }
  565. if (cch_start(cch))
  566. BUG();
  567. ret = 1;
  568. }
  569. exit:
  570. unlock_cch_handle(cch);
  571. return ret;
  572. }
  573. /*
  574. * Update CCH tlb interrupt select. Required when all the following is true:
  575. * - task's GRU context is loaded into a GRU
  576. * - task is using interrupt notification for TLB faults
  577. * - task has migrated to a different cpu on the same blade where
  578. * it was previously running.
  579. */
  580. static int gru_retarget_intr(struct gru_thread_state *gts)
  581. {
  582. if (gts->ts_tlb_int_select < 0
  583. || gts->ts_tlb_int_select == gru_cpu_fault_map_id())
  584. return 0;
  585. gru_dbg(grudev, "retarget from %d to %d\n", gts->ts_tlb_int_select,
  586. gru_cpu_fault_map_id());
  587. return gru_update_cch(gts, 0);
  588. }
  589. /*
  590. * Insufficient GRU resources available on the local blade. Steal a context from
  591. * a process. This is a hack until a _real_ resource scheduler is written....
  592. */
  593. #define next_ctxnum(n) ((n) < GRU_NUM_CCH - 2 ? (n) + 1 : 0)
  594. #define next_gru(b, g) (((g) < &(b)->bs_grus[GRU_CHIPLETS_PER_BLADE - 1]) ? \
  595. ((g)+1) : &(b)->bs_grus[0])
  596. static int is_gts_stealable(struct gru_thread_state *gts,
  597. struct gru_blade_state *bs)
  598. {
  599. if (is_kernel_context(gts))
  600. return down_write_trylock(&bs->bs_kgts_sema);
  601. else
  602. return mutex_trylock(&gts->ts_ctxlock);
  603. }
  604. static void gts_stolen(struct gru_thread_state *gts,
  605. struct gru_blade_state *bs)
  606. {
  607. if (is_kernel_context(gts)) {
  608. up_write(&bs->bs_kgts_sema);
  609. STAT(steal_kernel_context);
  610. } else {
  611. mutex_unlock(&gts->ts_ctxlock);
  612. STAT(steal_user_context);
  613. }
  614. }
  615. void gru_steal_context(struct gru_thread_state *gts, int blade_id)
  616. {
  617. struct gru_blade_state *blade;
  618. struct gru_state *gru, *gru0;
  619. struct gru_thread_state *ngts = NULL;
  620. int ctxnum, ctxnum0, flag = 0, cbr, dsr;
  621. cbr = gts->ts_cbr_au_count;
  622. dsr = gts->ts_dsr_au_count;
  623. blade = gru_base[blade_id];
  624. spin_lock(&blade->bs_lock);
  625. ctxnum = next_ctxnum(blade->bs_lru_ctxnum);
  626. gru = blade->bs_lru_gru;
  627. if (ctxnum == 0)
  628. gru = next_gru(blade, gru);
  629. ctxnum0 = ctxnum;
  630. gru0 = gru;
  631. while (1) {
  632. if (check_gru_resources(gru, cbr, dsr, GRU_NUM_CCH))
  633. break;
  634. spin_lock(&gru->gs_lock);
  635. for (; ctxnum < GRU_NUM_CCH; ctxnum++) {
  636. if (flag && gru == gru0 && ctxnum == ctxnum0)
  637. break;
  638. ngts = gru->gs_gts[ctxnum];
  639. /*
  640. * We are grabbing locks out of order, so trylock is
  641. * needed. GTSs are usually not locked, so the odds of
  642. * success are high. If trylock fails, try to steal a
  643. * different GSEG.
  644. */
  645. if (ngts && is_gts_stealable(ngts, blade))
  646. break;
  647. ngts = NULL;
  648. flag = 1;
  649. }
  650. spin_unlock(&gru->gs_lock);
  651. if (ngts || (flag && gru == gru0 && ctxnum == ctxnum0))
  652. break;
  653. ctxnum = 0;
  654. gru = next_gru(blade, gru);
  655. }
  656. blade->bs_lru_gru = gru;
  657. blade->bs_lru_ctxnum = ctxnum;
  658. spin_unlock(&blade->bs_lock);
  659. if (ngts) {
  660. gts->ustats.context_stolen++;
  661. ngts->ts_steal_jiffies = jiffies;
  662. gru_unload_context(ngts, is_kernel_context(ngts) ? 0 : 1);
  663. gts_stolen(ngts, blade);
  664. } else {
  665. STAT(steal_context_failed);
  666. }
  667. gru_dbg(grudev,
  668. "stole gid %d, ctxnum %d from gts %p. Need cb %d, ds %d;"
  669. " avail cb %ld, ds %ld\n",
  670. gru->gs_gid, ctxnum, ngts, cbr, dsr, hweight64(gru->gs_cbr_map),
  671. hweight64(gru->gs_dsr_map));
  672. }
  673. /*
  674. * Scan the GRUs on the local blade & assign a GRU context.
  675. */
  676. struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts,
  677. int blade)
  678. {
  679. struct gru_state *gru, *grux;
  680. int i, max_active_contexts;
  681. again:
  682. gru = NULL;
  683. max_active_contexts = GRU_NUM_CCH;
  684. for_each_gru_on_blade(grux, blade, i) {
  685. if (check_gru_resources(grux, gts->ts_cbr_au_count,
  686. gts->ts_dsr_au_count,
  687. max_active_contexts)) {
  688. gru = grux;
  689. max_active_contexts = grux->gs_active_contexts;
  690. if (max_active_contexts == 0)
  691. break;
  692. }
  693. }
  694. if (gru) {
  695. spin_lock(&gru->gs_lock);
  696. if (!check_gru_resources(gru, gts->ts_cbr_au_count,
  697. gts->ts_dsr_au_count, GRU_NUM_CCH)) {
  698. spin_unlock(&gru->gs_lock);
  699. goto again;
  700. }
  701. reserve_gru_resources(gru, gts);
  702. gts->ts_gru = gru;
  703. gts->ts_blade = gru->gs_blade_id;
  704. gts->ts_ctxnum =
  705. find_first_zero_bit(&gru->gs_context_map, GRU_NUM_CCH);
  706. BUG_ON(gts->ts_ctxnum == GRU_NUM_CCH);
  707. atomic_inc(&gts->ts_refcnt);
  708. gru->gs_gts[gts->ts_ctxnum] = gts;
  709. __set_bit(gts->ts_ctxnum, &gru->gs_context_map);
  710. spin_unlock(&gru->gs_lock);
  711. STAT(assign_context);
  712. gru_dbg(grudev,
  713. "gseg %p, gts %p, gid %d, ctx %d, cbr %d, dsr %d\n",
  714. gseg_virtual_address(gts->ts_gru, gts->ts_ctxnum), gts,
  715. gts->ts_gru->gs_gid, gts->ts_ctxnum,
  716. gts->ts_cbr_au_count, gts->ts_dsr_au_count);
  717. } else {
  718. gru_dbg(grudev, "failed to allocate a GTS %s\n", "");
  719. STAT(assign_context_failed);
  720. }
  721. return gru;
  722. }
  723. /*
  724. * gru_nopage
  725. *
  726. * Map the user's GRU segment
  727. *
  728. * Note: gru segments alway mmaped on GRU_GSEG_PAGESIZE boundaries.
  729. */
  730. int gru_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  731. {
  732. struct gru_thread_state *gts;
  733. unsigned long paddr, vaddr;
  734. int blade_id;
  735. vaddr = (unsigned long)vmf->virtual_address;
  736. gru_dbg(grudev, "vma %p, vaddr 0x%lx (0x%lx)\n",
  737. vma, vaddr, GSEG_BASE(vaddr));
  738. STAT(nopfn);
  739. /* The following check ensures vaddr is a valid address in the VMA */
  740. gts = gru_find_thread_state(vma, TSID(vaddr, vma));
  741. if (!gts)
  742. return VM_FAULT_SIGBUS;
  743. again:
  744. mutex_lock(&gts->ts_ctxlock);
  745. preempt_disable();
  746. blade_id = uv_numa_blade_id();
  747. if (gts->ts_gru) {
  748. if (gts->ts_gru->gs_blade_id != blade_id) {
  749. STAT(migrated_nopfn_unload);
  750. gru_unload_context(gts, 1);
  751. } else {
  752. if (gru_retarget_intr(gts))
  753. STAT(migrated_nopfn_retarget);
  754. }
  755. }
  756. if (!gts->ts_gru) {
  757. STAT(load_user_context);
  758. if (!gru_assign_gru_context(gts, blade_id)) {
  759. preempt_enable();
  760. mutex_unlock(&gts->ts_ctxlock);
  761. set_current_state(TASK_INTERRUPTIBLE);
  762. schedule_timeout(GRU_ASSIGN_DELAY); /* true hack ZZZ */
  763. blade_id = uv_numa_blade_id();
  764. if (gts->ts_steal_jiffies + GRU_STEAL_DELAY < jiffies)
  765. gru_steal_context(gts, blade_id);
  766. goto again;
  767. }
  768. gru_load_context(gts);
  769. paddr = gseg_physical_address(gts->ts_gru, gts->ts_ctxnum);
  770. remap_pfn_range(vma, vaddr & ~(GRU_GSEG_PAGESIZE - 1),
  771. paddr >> PAGE_SHIFT, GRU_GSEG_PAGESIZE,
  772. vma->vm_page_prot);
  773. }
  774. preempt_enable();
  775. mutex_unlock(&gts->ts_ctxlock);
  776. return VM_FAULT_NOPAGE;
  777. }