grumain.c 22 KB

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