grumain.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  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. gts->ts_sizeavail = GRU_SIZEAVAIL(PAGE_SHIFT);
  296. if (!gts->ts_gms)
  297. goto err;
  298. gru_dbg(grudev, "alloc vdata %p, new gts %p\n", vdata, 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, 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. } else {
  528. for (i = 0; i < 8; i++)
  529. cch->asid[i] = 0;
  530. cch->tfm_fault_bit_enable = 0;
  531. cch->tlb_int_enable = 0;
  532. gts->ts_force_unload = 1;
  533. }
  534. if (cch_start(cch))
  535. BUG();
  536. ret = 1;
  537. }
  538. exit:
  539. unlock_cch_handle(cch);
  540. return ret;
  541. }
  542. /*
  543. * Update CCH tlb interrupt select. Required when all the following is true:
  544. * - task's GRU context is loaded into a GRU
  545. * - task is using interrupt notification for TLB faults
  546. * - task has migrated to a different cpu on the same blade where
  547. * it was previously running.
  548. */
  549. static int gru_retarget_intr(struct gru_thread_state *gts)
  550. {
  551. if (gts->ts_tlb_int_select < 0
  552. || gts->ts_tlb_int_select == gru_cpu_fault_map_id())
  553. return 0;
  554. gru_dbg(grudev, "retarget from %d to %d\n", gts->ts_tlb_int_select,
  555. gru_cpu_fault_map_id());
  556. return gru_update_cch(gts, 0);
  557. }
  558. /*
  559. * Insufficient GRU resources available on the local blade. Steal a context from
  560. * a process. This is a hack until a _real_ resource scheduler is written....
  561. */
  562. #define next_ctxnum(n) ((n) < GRU_NUM_CCH - 2 ? (n) + 1 : 0)
  563. #define next_gru(b, g) (((g) < &(b)->bs_grus[GRU_CHIPLETS_PER_BLADE - 1]) ? \
  564. ((g)+1) : &(b)->bs_grus[0])
  565. static void gru_steal_context(struct gru_thread_state *gts)
  566. {
  567. struct gru_blade_state *blade;
  568. struct gru_state *gru, *gru0;
  569. struct gru_thread_state *ngts = NULL;
  570. int ctxnum, ctxnum0, flag = 0, cbr, dsr;
  571. cbr = gts->ts_cbr_au_count;
  572. dsr = gts->ts_dsr_au_count;
  573. preempt_disable();
  574. blade = gru_base[uv_numa_blade_id()];
  575. spin_lock(&blade->bs_lock);
  576. ctxnum = next_ctxnum(blade->bs_lru_ctxnum);
  577. gru = blade->bs_lru_gru;
  578. if (ctxnum == 0)
  579. gru = next_gru(blade, gru);
  580. ctxnum0 = ctxnum;
  581. gru0 = gru;
  582. while (1) {
  583. if (check_gru_resources(gru, cbr, dsr, GRU_NUM_CCH))
  584. break;
  585. spin_lock(&gru->gs_lock);
  586. for (; ctxnum < GRU_NUM_CCH; ctxnum++) {
  587. if (flag && gru == gru0 && ctxnum == ctxnum0)
  588. break;
  589. ngts = gru->gs_gts[ctxnum];
  590. /*
  591. * We are grabbing locks out of order, so trylock is
  592. * needed. GTSs are usually not locked, so the odds of
  593. * success are high. If trylock fails, try to steal a
  594. * different GSEG.
  595. */
  596. if (ngts && mutex_trylock(&ngts->ts_ctxlock))
  597. break;
  598. ngts = NULL;
  599. flag = 1;
  600. }
  601. spin_unlock(&gru->gs_lock);
  602. if (ngts || (flag && gru == gru0 && ctxnum == ctxnum0))
  603. break;
  604. ctxnum = 0;
  605. gru = next_gru(blade, gru);
  606. }
  607. blade->bs_lru_gru = gru;
  608. blade->bs_lru_ctxnum = ctxnum;
  609. spin_unlock(&blade->bs_lock);
  610. preempt_enable();
  611. if (ngts) {
  612. STAT(steal_context);
  613. ngts->ts_steal_jiffies = jiffies;
  614. gru_unload_context(ngts, 1);
  615. mutex_unlock(&ngts->ts_ctxlock);
  616. } else {
  617. STAT(steal_context_failed);
  618. }
  619. gru_dbg(grudev,
  620. "stole gid %d, ctxnum %d from gts %p. Need cb %d, ds %d;"
  621. " avail cb %ld, ds %ld\n",
  622. gru->gs_gid, ctxnum, ngts, cbr, dsr, hweight64(gru->gs_cbr_map),
  623. hweight64(gru->gs_dsr_map));
  624. }
  625. /*
  626. * Scan the GRUs on the local blade & assign a GRU context.
  627. */
  628. static struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts)
  629. {
  630. struct gru_state *gru, *grux;
  631. int i, max_active_contexts;
  632. preempt_disable();
  633. again:
  634. gru = NULL;
  635. max_active_contexts = GRU_NUM_CCH;
  636. for_each_gru_on_blade(grux, uv_numa_blade_id(), i) {
  637. if (check_gru_resources(grux, gts->ts_cbr_au_count,
  638. gts->ts_dsr_au_count,
  639. max_active_contexts)) {
  640. gru = grux;
  641. max_active_contexts = grux->gs_active_contexts;
  642. if (max_active_contexts == 0)
  643. break;
  644. }
  645. }
  646. if (gru) {
  647. spin_lock(&gru->gs_lock);
  648. if (!check_gru_resources(gru, gts->ts_cbr_au_count,
  649. gts->ts_dsr_au_count, GRU_NUM_CCH)) {
  650. spin_unlock(&gru->gs_lock);
  651. goto again;
  652. }
  653. reserve_gru_resources(gru, gts);
  654. gts->ts_gru = gru;
  655. gts->ts_blade = gru->gs_blade_id;
  656. gts->ts_ctxnum =
  657. find_first_zero_bit(&gru->gs_context_map, GRU_NUM_CCH);
  658. BUG_ON(gts->ts_ctxnum == GRU_NUM_CCH);
  659. atomic_inc(&gts->ts_refcnt);
  660. gru->gs_gts[gts->ts_ctxnum] = gts;
  661. __set_bit(gts->ts_ctxnum, &gru->gs_context_map);
  662. spin_unlock(&gru->gs_lock);
  663. STAT(assign_context);
  664. gru_dbg(grudev,
  665. "gseg %p, gts %p, gid %d, ctx %d, cbr %d, dsr %d\n",
  666. gseg_virtual_address(gts->ts_gru, gts->ts_ctxnum), gts,
  667. gts->ts_gru->gs_gid, gts->ts_ctxnum,
  668. gts->ts_cbr_au_count, gts->ts_dsr_au_count);
  669. } else {
  670. gru_dbg(grudev, "failed to allocate a GTS %s\n", "");
  671. STAT(assign_context_failed);
  672. }
  673. preempt_enable();
  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. vaddr = (unsigned long)vmf->virtual_address;
  688. gru_dbg(grudev, "vma %p, vaddr 0x%lx (0x%lx)\n",
  689. vma, vaddr, GSEG_BASE(vaddr));
  690. STAT(nopfn);
  691. /* The following check ensures vaddr is a valid address in the VMA */
  692. gts = gru_find_thread_state(vma, TSID(vaddr, vma));
  693. if (!gts)
  694. return VM_FAULT_SIGBUS;
  695. again:
  696. mutex_lock(&gts->ts_ctxlock);
  697. preempt_disable();
  698. if (gts->ts_gru) {
  699. if (gts->ts_gru->gs_blade_id != uv_numa_blade_id()) {
  700. STAT(migrated_nopfn_unload);
  701. gru_unload_context(gts, 1);
  702. } else {
  703. if (gru_retarget_intr(gts))
  704. STAT(migrated_nopfn_retarget);
  705. }
  706. }
  707. if (!gts->ts_gru) {
  708. if (!gru_assign_gru_context(gts)) {
  709. mutex_unlock(&gts->ts_ctxlock);
  710. preempt_enable();
  711. schedule_timeout(GRU_ASSIGN_DELAY); /* true hack ZZZ */
  712. if (gts->ts_steal_jiffies + GRU_STEAL_DELAY < jiffies)
  713. gru_steal_context(gts);
  714. goto again;
  715. }
  716. gru_load_context(gts);
  717. paddr = gseg_physical_address(gts->ts_gru, gts->ts_ctxnum);
  718. remap_pfn_range(vma, vaddr & ~(GRU_GSEG_PAGESIZE - 1),
  719. paddr >> PAGE_SHIFT, GRU_GSEG_PAGESIZE,
  720. vma->vm_page_prot);
  721. }
  722. mutex_unlock(&gts->ts_ctxlock);
  723. preempt_enable();
  724. return VM_FAULT_NOPAGE;
  725. }