grumain.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. * SN Platform GRU Driver
  3. *
  4. * DRIVER TABLE MANAGER + GRU CONTEXT LOAD/UNLOAD
  5. *
  6. * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/mm.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/sched.h>
  27. #include <linux/device.h>
  28. #include <linux/list.h>
  29. #include <asm/uv/uv_hub.h>
  30. #include "gru.h"
  31. #include "grutables.h"
  32. #include "gruhandles.h"
  33. unsigned long gru_options __read_mostly;
  34. static struct device_driver gru_driver = {
  35. .name = "gru"
  36. };
  37. static struct device gru_device = {
  38. .init_name = "",
  39. .driver = &gru_driver,
  40. };
  41. struct device *grudev = &gru_device;
  42. /*
  43. * Select a gru fault map to be used by the current cpu. Note that
  44. * multiple cpus may be using the same map.
  45. * ZZZ should "shift" be used?? Depends on HT cpu numbering
  46. * ZZZ should be inline but did not work on emulator
  47. */
  48. int gru_cpu_fault_map_id(void)
  49. {
  50. return uv_blade_processor_id() % GRU_NUM_TFM;
  51. }
  52. /*--------- ASID Management -------------------------------------------
  53. *
  54. * Initially, assign asids sequentially from MIN_ASID .. MAX_ASID.
  55. * Once MAX is reached, flush the TLB & start over. However,
  56. * some asids may still be in use. There won't be many (percentage wise) still
  57. * in use. Search active contexts & determine the value of the first
  58. * asid in use ("x"s below). Set "limit" to this value.
  59. * This defines a block of assignable asids.
  60. *
  61. * When "limit" is reached, search forward from limit+1 and determine the
  62. * next block of assignable asids.
  63. *
  64. * Repeat until MAX_ASID is reached, then start over again.
  65. *
  66. * Each time MAX_ASID is reached, increment the asid generation. Since
  67. * the search for in-use asids only checks contexts with GRUs currently
  68. * assigned, asids in some contexts will be missed. Prior to loading
  69. * a context, the asid generation of the GTS asid is rechecked. If it
  70. * doesn't match the current generation, a new asid will be assigned.
  71. *
  72. * 0---------------x------------x---------------------x----|
  73. * ^-next ^-limit ^-MAX_ASID
  74. *
  75. * All asid manipulation & context loading/unloading is protected by the
  76. * gs_lock.
  77. */
  78. /* Hit the asid limit. Start over */
  79. static int gru_wrap_asid(struct gru_state *gru)
  80. {
  81. gru_dbg(grudev, "gid %d\n", gru->gs_gid);
  82. STAT(asid_wrap);
  83. gru->gs_asid_gen++;
  84. return MIN_ASID;
  85. }
  86. /* Find the next chunk of unused asids */
  87. static int gru_reset_asid_limit(struct gru_state *gru, int asid)
  88. {
  89. int i, gid, inuse_asid, limit;
  90. gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid);
  91. STAT(asid_next);
  92. limit = MAX_ASID;
  93. if (asid >= limit)
  94. asid = gru_wrap_asid(gru);
  95. gru_flush_all_tlb(gru);
  96. gid = gru->gs_gid;
  97. again:
  98. for (i = 0; i < GRU_NUM_CCH; i++) {
  99. if (!gru->gs_gts[i] || is_kernel_context(gru->gs_gts[i]))
  100. continue;
  101. inuse_asid = gru->gs_gts[i]->ts_gms->ms_asids[gid].mt_asid;
  102. gru_dbg(grudev, "gid %d, gts %p, gms %p, inuse 0x%x, cxt %d\n",
  103. gru->gs_gid, gru->gs_gts[i], gru->gs_gts[i]->ts_gms,
  104. inuse_asid, i);
  105. if (inuse_asid == asid) {
  106. asid += ASID_INC;
  107. if (asid >= limit) {
  108. /*
  109. * empty range: reset the range limit and
  110. * start over
  111. */
  112. limit = MAX_ASID;
  113. if (asid >= MAX_ASID)
  114. asid = gru_wrap_asid(gru);
  115. goto again;
  116. }
  117. }
  118. if ((inuse_asid > asid) && (inuse_asid < limit))
  119. limit = inuse_asid;
  120. }
  121. gru->gs_asid_limit = limit;
  122. gru->gs_asid = asid;
  123. gru_dbg(grudev, "gid %d, new asid 0x%x, new_limit 0x%x\n", gru->gs_gid,
  124. asid, limit);
  125. return asid;
  126. }
  127. /* Assign a new ASID to a thread context. */
  128. static int gru_assign_asid(struct gru_state *gru)
  129. {
  130. int asid;
  131. gru->gs_asid += ASID_INC;
  132. asid = gru->gs_asid;
  133. if (asid >= gru->gs_asid_limit)
  134. asid = gru_reset_asid_limit(gru, asid);
  135. gru_dbg(grudev, "gid %d, asid 0x%x\n", gru->gs_gid, asid);
  136. return asid;
  137. }
  138. /*
  139. * Clear n bits in a word. Return a word indicating the bits that were cleared.
  140. * Optionally, build an array of chars that contain the bit numbers allocated.
  141. */
  142. static unsigned long reserve_resources(unsigned long *p, int n, int mmax,
  143. char *idx)
  144. {
  145. unsigned long bits = 0;
  146. int i;
  147. while (n--) {
  148. i = find_first_bit(p, mmax);
  149. if (i == mmax)
  150. BUG();
  151. __clear_bit(i, p);
  152. __set_bit(i, &bits);
  153. if (idx)
  154. *idx++ = i;
  155. }
  156. return bits;
  157. }
  158. unsigned long gru_reserve_cb_resources(struct gru_state *gru, int cbr_au_count,
  159. char *cbmap)
  160. {
  161. return reserve_resources(&gru->gs_cbr_map, cbr_au_count, GRU_CBR_AU,
  162. cbmap);
  163. }
  164. unsigned long gru_reserve_ds_resources(struct gru_state *gru, int dsr_au_count,
  165. char *dsmap)
  166. {
  167. return reserve_resources(&gru->gs_dsr_map, dsr_au_count, GRU_DSR_AU,
  168. dsmap);
  169. }
  170. static void reserve_gru_resources(struct gru_state *gru,
  171. struct gru_thread_state *gts)
  172. {
  173. gru->gs_active_contexts++;
  174. gts->ts_cbr_map =
  175. gru_reserve_cb_resources(gru, gts->ts_cbr_au_count,
  176. gts->ts_cbr_idx);
  177. gts->ts_dsr_map =
  178. gru_reserve_ds_resources(gru, gts->ts_dsr_au_count, NULL);
  179. }
  180. static void free_gru_resources(struct gru_state *gru,
  181. struct gru_thread_state *gts)
  182. {
  183. gru->gs_active_contexts--;
  184. gru->gs_cbr_map |= gts->ts_cbr_map;
  185. gru->gs_dsr_map |= gts->ts_dsr_map;
  186. }
  187. /*
  188. * Check if a GRU has sufficient free resources to satisfy an allocation
  189. * request. Note: GRU locks may or may not be held when this is called. If
  190. * not held, recheck after acquiring the appropriate locks.
  191. *
  192. * Returns 1 if sufficient resources, 0 if not
  193. */
  194. static int check_gru_resources(struct gru_state *gru, int cbr_au_count,
  195. int dsr_au_count, int max_active_contexts)
  196. {
  197. return hweight64(gru->gs_cbr_map) >= cbr_au_count
  198. && hweight64(gru->gs_dsr_map) >= dsr_au_count
  199. && gru->gs_active_contexts < max_active_contexts;
  200. }
  201. /*
  202. * TLB manangment requires tracking all GRU chiplets that have loaded a GSEG
  203. * context.
  204. */
  205. static int gru_load_mm_tracker(struct gru_state *gru,
  206. struct gru_thread_state *gts)
  207. {
  208. struct gru_mm_struct *gms = gts->ts_gms;
  209. struct gru_mm_tracker *asids = &gms->ms_asids[gru->gs_gid];
  210. unsigned short ctxbitmap = (1 << gts->ts_ctxnum);
  211. int asid;
  212. spin_lock(&gms->ms_asid_lock);
  213. asid = asids->mt_asid;
  214. spin_lock(&gru->gs_asid_lock);
  215. if (asid == 0 || (asids->mt_ctxbitmap == 0 && asids->mt_asid_gen !=
  216. gru->gs_asid_gen)) {
  217. asid = gru_assign_asid(gru);
  218. asids->mt_asid = asid;
  219. asids->mt_asid_gen = gru->gs_asid_gen;
  220. STAT(asid_new);
  221. } else {
  222. STAT(asid_reuse);
  223. }
  224. spin_unlock(&gru->gs_asid_lock);
  225. BUG_ON(asids->mt_ctxbitmap & ctxbitmap);
  226. asids->mt_ctxbitmap |= ctxbitmap;
  227. if (!test_bit(gru->gs_gid, gms->ms_asidmap))
  228. __set_bit(gru->gs_gid, gms->ms_asidmap);
  229. spin_unlock(&gms->ms_asid_lock);
  230. gru_dbg(grudev,
  231. "gid %d, gts %p, gms %p, ctxnum %d, asid 0x%x, asidmap 0x%lx\n",
  232. gru->gs_gid, gts, gms, gts->ts_ctxnum, asid,
  233. gms->ms_asidmap[0]);
  234. return asid;
  235. }
  236. static void gru_unload_mm_tracker(struct gru_state *gru,
  237. struct gru_thread_state *gts)
  238. {
  239. struct gru_mm_struct *gms = gts->ts_gms;
  240. struct gru_mm_tracker *asids;
  241. unsigned short ctxbitmap;
  242. asids = &gms->ms_asids[gru->gs_gid];
  243. ctxbitmap = (1 << gts->ts_ctxnum);
  244. spin_lock(&gms->ms_asid_lock);
  245. spin_lock(&gru->gs_asid_lock);
  246. BUG_ON((asids->mt_ctxbitmap & ctxbitmap) != ctxbitmap);
  247. asids->mt_ctxbitmap ^= ctxbitmap;
  248. gru_dbg(grudev, "gid %d, gts %p, gms %p, ctxnum 0x%d, asidmap 0x%lx\n",
  249. gru->gs_gid, gts, gms, gts->ts_ctxnum, gms->ms_asidmap[0]);
  250. spin_unlock(&gru->gs_asid_lock);
  251. spin_unlock(&gms->ms_asid_lock);
  252. }
  253. /*
  254. * Decrement the reference count on a GTS structure. Free the structure
  255. * if the reference count goes to zero.
  256. */
  257. void gts_drop(struct gru_thread_state *gts)
  258. {
  259. if (gts && atomic_dec_return(&gts->ts_refcnt) == 0) {
  260. gru_drop_mmu_notifier(gts->ts_gms);
  261. kfree(gts);
  262. STAT(gts_free);
  263. }
  264. }
  265. /*
  266. * Locate the GTS structure for the current thread.
  267. */
  268. static struct gru_thread_state *gru_find_current_gts_nolock(struct gru_vma_data
  269. *vdata, int tsid)
  270. {
  271. struct gru_thread_state *gts;
  272. list_for_each_entry(gts, &vdata->vd_head, ts_next)
  273. if (gts->ts_tsid == tsid)
  274. return gts;
  275. return NULL;
  276. }
  277. /*
  278. * Allocate a thread state structure.
  279. */
  280. struct gru_thread_state *gru_alloc_gts(struct vm_area_struct *vma,
  281. int cbr_au_count, int dsr_au_count, int options, int tsid)
  282. {
  283. struct gru_thread_state *gts;
  284. int bytes;
  285. bytes = DSR_BYTES(dsr_au_count) + CBR_BYTES(cbr_au_count);
  286. bytes += sizeof(struct gru_thread_state);
  287. gts = kmalloc(bytes, GFP_KERNEL);
  288. if (!gts)
  289. return NULL;
  290. STAT(gts_alloc);
  291. memset(gts, 0, sizeof(struct gru_thread_state)); /* zero out header */
  292. atomic_set(&gts->ts_refcnt, 1);
  293. mutex_init(&gts->ts_ctxlock);
  294. gts->ts_cbr_au_count = cbr_au_count;
  295. gts->ts_dsr_au_count = dsr_au_count;
  296. gts->ts_user_options = options;
  297. gts->ts_tsid = tsid;
  298. gts->ts_ctxnum = NULLCTX;
  299. gts->ts_tlb_int_select = -1;
  300. gts->ts_cch_req_slice = -1;
  301. gts->ts_sizeavail = GRU_SIZEAVAIL(PAGE_SHIFT);
  302. if (vma) {
  303. gts->ts_mm = current->mm;
  304. gts->ts_vma = vma;
  305. gts->ts_gms = gru_register_mmu_notifier();
  306. if (!gts->ts_gms)
  307. goto err;
  308. }
  309. gru_dbg(grudev, "alloc gts %p\n", gts);
  310. return gts;
  311. err:
  312. gts_drop(gts);
  313. return NULL;
  314. }
  315. /*
  316. * Allocate a vma private data structure.
  317. */
  318. struct gru_vma_data *gru_alloc_vma_data(struct vm_area_struct *vma, int tsid)
  319. {
  320. struct gru_vma_data *vdata = NULL;
  321. vdata = kmalloc(sizeof(*vdata), GFP_KERNEL);
  322. if (!vdata)
  323. return NULL;
  324. INIT_LIST_HEAD(&vdata->vd_head);
  325. spin_lock_init(&vdata->vd_lock);
  326. gru_dbg(grudev, "alloc vdata %p\n", vdata);
  327. return vdata;
  328. }
  329. /*
  330. * Find the thread state structure for the current thread.
  331. */
  332. struct gru_thread_state *gru_find_thread_state(struct vm_area_struct *vma,
  333. int tsid)
  334. {
  335. struct gru_vma_data *vdata = vma->vm_private_data;
  336. struct gru_thread_state *gts;
  337. spin_lock(&vdata->vd_lock);
  338. gts = gru_find_current_gts_nolock(vdata, tsid);
  339. spin_unlock(&vdata->vd_lock);
  340. gru_dbg(grudev, "vma %p, gts %p\n", vma, gts);
  341. return gts;
  342. }
  343. /*
  344. * Allocate a new thread state for a GSEG. Note that races may allow
  345. * another thread to race to create a gts.
  346. */
  347. struct gru_thread_state *gru_alloc_thread_state(struct vm_area_struct *vma,
  348. int tsid)
  349. {
  350. struct gru_vma_data *vdata = vma->vm_private_data;
  351. struct gru_thread_state *gts, *ngts;
  352. gts = gru_alloc_gts(vma, vdata->vd_cbr_au_count, vdata->vd_dsr_au_count,
  353. vdata->vd_user_options, tsid);
  354. if (!gts)
  355. return NULL;
  356. spin_lock(&vdata->vd_lock);
  357. ngts = gru_find_current_gts_nolock(vdata, tsid);
  358. if (ngts) {
  359. gts_drop(gts);
  360. gts = ngts;
  361. STAT(gts_double_allocate);
  362. } else {
  363. list_add(&gts->ts_next, &vdata->vd_head);
  364. }
  365. spin_unlock(&vdata->vd_lock);
  366. gru_dbg(grudev, "vma %p, gts %p\n", vma, gts);
  367. return gts;
  368. }
  369. /*
  370. * Free the GRU context assigned to the thread state.
  371. */
  372. static void gru_free_gru_context(struct gru_thread_state *gts)
  373. {
  374. struct gru_state *gru;
  375. gru = gts->ts_gru;
  376. gru_dbg(grudev, "gts %p, gid %d\n", gts, gru->gs_gid);
  377. spin_lock(&gru->gs_lock);
  378. gru->gs_gts[gts->ts_ctxnum] = NULL;
  379. free_gru_resources(gru, gts);
  380. BUG_ON(test_bit(gts->ts_ctxnum, &gru->gs_context_map) == 0);
  381. __clear_bit(gts->ts_ctxnum, &gru->gs_context_map);
  382. gts->ts_ctxnum = NULLCTX;
  383. gts->ts_gru = NULL;
  384. gts->ts_blade = -1;
  385. spin_unlock(&gru->gs_lock);
  386. gts_drop(gts);
  387. STAT(free_context);
  388. }
  389. /*
  390. * Prefetching cachelines help hardware performance.
  391. * (Strictly a performance enhancement. Not functionally required).
  392. */
  393. static void prefetch_data(void *p, int num, int stride)
  394. {
  395. while (num-- > 0) {
  396. prefetchw(p);
  397. p += stride;
  398. }
  399. }
  400. static inline long gru_copy_handle(void *d, void *s)
  401. {
  402. memcpy(d, s, GRU_HANDLE_BYTES);
  403. return GRU_HANDLE_BYTES;
  404. }
  405. static void gru_prefetch_context(void *gseg, void *cb, void *cbe,
  406. unsigned long cbrmap, unsigned long length)
  407. {
  408. int i, scr;
  409. prefetch_data(gseg + GRU_DS_BASE, length / GRU_CACHE_LINE_BYTES,
  410. GRU_CACHE_LINE_BYTES);
  411. for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
  412. prefetch_data(cb, 1, GRU_CACHE_LINE_BYTES);
  413. prefetch_data(cbe + i * GRU_HANDLE_STRIDE, 1,
  414. GRU_CACHE_LINE_BYTES);
  415. cb += GRU_HANDLE_STRIDE;
  416. }
  417. }
  418. static void gru_load_context_data(void *save, void *grubase, int ctxnum,
  419. unsigned long cbrmap, unsigned long dsrmap,
  420. int data_valid)
  421. {
  422. void *gseg, *cb, *cbe;
  423. unsigned long length;
  424. int i, scr;
  425. gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
  426. cb = gseg + GRU_CB_BASE;
  427. cbe = grubase + GRU_CBE_BASE;
  428. length = hweight64(dsrmap) * GRU_DSR_AU_BYTES;
  429. gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
  430. for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
  431. if (data_valid) {
  432. save += gru_copy_handle(cb, save);
  433. save += gru_copy_handle(cbe + i * GRU_HANDLE_STRIDE,
  434. save);
  435. } else {
  436. memset(cb, 0, GRU_CACHE_LINE_BYTES);
  437. memset(cbe + i * GRU_HANDLE_STRIDE, 0,
  438. GRU_CACHE_LINE_BYTES);
  439. }
  440. cb += GRU_HANDLE_STRIDE;
  441. }
  442. if (data_valid)
  443. memcpy(gseg + GRU_DS_BASE, save, length);
  444. else
  445. memset(gseg + GRU_DS_BASE, 0, length);
  446. }
  447. static void gru_unload_context_data(void *save, void *grubase, int ctxnum,
  448. unsigned long cbrmap, unsigned long dsrmap)
  449. {
  450. void *gseg, *cb, *cbe;
  451. unsigned long length;
  452. int i, scr;
  453. gseg = grubase + ctxnum * GRU_GSEG_STRIDE;
  454. cb = gseg + GRU_CB_BASE;
  455. cbe = grubase + GRU_CBE_BASE;
  456. length = hweight64(dsrmap) * GRU_DSR_AU_BYTES;
  457. gru_prefetch_context(gseg, cb, cbe, cbrmap, length);
  458. for_each_cbr_in_allocation_map(i, &cbrmap, scr) {
  459. save += gru_copy_handle(save, cb);
  460. save += gru_copy_handle(save, cbe + i * GRU_HANDLE_STRIDE);
  461. cb += GRU_HANDLE_STRIDE;
  462. }
  463. memcpy(save, gseg + GRU_DS_BASE, length);
  464. }
  465. void gru_unload_context(struct gru_thread_state *gts, int savestate)
  466. {
  467. struct gru_state *gru = gts->ts_gru;
  468. struct gru_context_configuration_handle *cch;
  469. int ctxnum = gts->ts_ctxnum;
  470. if (!is_kernel_context(gts))
  471. zap_vma_ptes(gts->ts_vma, UGRUADDR(gts), GRU_GSEG_PAGESIZE);
  472. cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
  473. gru_dbg(grudev, "gts %p\n", gts);
  474. lock_cch_handle(cch);
  475. if (cch_interrupt_sync(cch))
  476. BUG();
  477. if (!is_kernel_context(gts))
  478. gru_unload_mm_tracker(gru, gts);
  479. if (savestate) {
  480. gru_unload_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr,
  481. ctxnum, gts->ts_cbr_map,
  482. gts->ts_dsr_map);
  483. gts->ts_data_valid = 1;
  484. }
  485. if (cch_deallocate(cch))
  486. BUG();
  487. gts->ts_force_unload = 0; /* ts_force_unload locked by CCH lock */
  488. unlock_cch_handle(cch);
  489. gru_free_gru_context(gts);
  490. }
  491. /*
  492. * Load a GRU context by copying it from the thread data structure in memory
  493. * to the GRU.
  494. */
  495. void gru_load_context(struct gru_thread_state *gts)
  496. {
  497. struct gru_state *gru = gts->ts_gru;
  498. struct gru_context_configuration_handle *cch;
  499. int i, err, asid, ctxnum = gts->ts_ctxnum;
  500. gru_dbg(grudev, "gts %p\n", gts);
  501. cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
  502. lock_cch_handle(cch);
  503. cch->tfm_fault_bit_enable =
  504. (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
  505. || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
  506. cch->tlb_int_enable = (gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
  507. if (cch->tlb_int_enable) {
  508. gts->ts_tlb_int_select = gru_cpu_fault_map_id();
  509. cch->tlb_int_select = gts->ts_tlb_int_select;
  510. }
  511. if (gts->ts_cch_req_slice >= 0) {
  512. cch->req_slice_set_enable = 1;
  513. cch->req_slice = gts->ts_cch_req_slice;
  514. } else {
  515. cch->req_slice_set_enable =0;
  516. }
  517. cch->tfm_done_bit_enable = 0;
  518. cch->dsr_allocation_map = gts->ts_dsr_map;
  519. cch->cbr_allocation_map = gts->ts_cbr_map;
  520. if (is_kernel_context(gts)) {
  521. cch->unmap_enable = 1;
  522. cch->tfm_done_bit_enable = 1;
  523. cch->cb_int_enable = 1;
  524. } else {
  525. cch->unmap_enable = 0;
  526. cch->tfm_done_bit_enable = 0;
  527. cch->cb_int_enable = 0;
  528. asid = gru_load_mm_tracker(gru, gts);
  529. for (i = 0; i < 8; i++) {
  530. cch->asid[i] = asid + i;
  531. cch->sizeavail[i] = gts->ts_sizeavail;
  532. }
  533. }
  534. err = cch_allocate(cch);
  535. if (err) {
  536. gru_dbg(grudev,
  537. "err %d: cch %p, gts %p, cbr 0x%lx, dsr 0x%lx\n",
  538. err, cch, gts, gts->ts_cbr_map, gts->ts_dsr_map);
  539. BUG();
  540. }
  541. gru_load_context_data(gts->ts_gdata, gru->gs_gru_base_vaddr, ctxnum,
  542. gts->ts_cbr_map, gts->ts_dsr_map, gts->ts_data_valid);
  543. if (cch_start(cch))
  544. BUG();
  545. unlock_cch_handle(cch);
  546. }
  547. /*
  548. * Update fields in an active CCH:
  549. * - retarget interrupts on local blade
  550. * - update sizeavail mask
  551. * - force a delayed context unload by clearing the CCH asids. This
  552. * forces TLB misses for new GRU instructions. The context is unloaded
  553. * when the next TLB miss occurs.
  554. */
  555. int gru_update_cch(struct gru_thread_state *gts, int force_unload)
  556. {
  557. struct gru_context_configuration_handle *cch;
  558. struct gru_state *gru = gts->ts_gru;
  559. int i, ctxnum = gts->ts_ctxnum, ret = 0;
  560. cch = get_cch(gru->gs_gru_base_vaddr, ctxnum);
  561. lock_cch_handle(cch);
  562. if (cch->state == CCHSTATE_ACTIVE) {
  563. if (gru->gs_gts[gts->ts_ctxnum] != gts)
  564. goto exit;
  565. if (cch_interrupt(cch))
  566. BUG();
  567. if (!force_unload) {
  568. for (i = 0; i < 8; i++)
  569. cch->sizeavail[i] = gts->ts_sizeavail;
  570. gts->ts_tlb_int_select = gru_cpu_fault_map_id();
  571. cch->tlb_int_select = gru_cpu_fault_map_id();
  572. cch->tfm_fault_bit_enable =
  573. (gts->ts_user_options == GRU_OPT_MISS_FMM_POLL
  574. || gts->ts_user_options == GRU_OPT_MISS_FMM_INTR);
  575. } else {
  576. for (i = 0; i < 8; i++)
  577. cch->asid[i] = 0;
  578. cch->tfm_fault_bit_enable = 0;
  579. cch->tlb_int_enable = 0;
  580. gts->ts_force_unload = 1;
  581. }
  582. if (cch_start(cch))
  583. BUG();
  584. ret = 1;
  585. }
  586. exit:
  587. unlock_cch_handle(cch);
  588. return ret;
  589. }
  590. /*
  591. * Update CCH tlb interrupt select. Required when all the following is true:
  592. * - task's GRU context is loaded into a GRU
  593. * - task is using interrupt notification for TLB faults
  594. * - task has migrated to a different cpu on the same blade where
  595. * it was previously running.
  596. */
  597. static int gru_retarget_intr(struct gru_thread_state *gts)
  598. {
  599. if (gts->ts_tlb_int_select < 0
  600. || gts->ts_tlb_int_select == gru_cpu_fault_map_id())
  601. return 0;
  602. gru_dbg(grudev, "retarget from %d to %d\n", gts->ts_tlb_int_select,
  603. gru_cpu_fault_map_id());
  604. return gru_update_cch(gts, 0);
  605. }
  606. /*
  607. * Insufficient GRU resources available on the local blade. Steal a context from
  608. * a process. This is a hack until a _real_ resource scheduler is written....
  609. */
  610. #define next_ctxnum(n) ((n) < GRU_NUM_CCH - 2 ? (n) + 1 : 0)
  611. #define next_gru(b, g) (((g) < &(b)->bs_grus[GRU_CHIPLETS_PER_BLADE - 1]) ? \
  612. ((g)+1) : &(b)->bs_grus[0])
  613. static int is_gts_stealable(struct gru_thread_state *gts,
  614. struct gru_blade_state *bs)
  615. {
  616. if (is_kernel_context(gts))
  617. return down_write_trylock(&bs->bs_kgts_sema);
  618. else
  619. return mutex_trylock(&gts->ts_ctxlock);
  620. }
  621. static void gts_stolen(struct gru_thread_state *gts,
  622. struct gru_blade_state *bs)
  623. {
  624. if (is_kernel_context(gts)) {
  625. up_write(&bs->bs_kgts_sema);
  626. STAT(steal_kernel_context);
  627. } else {
  628. mutex_unlock(&gts->ts_ctxlock);
  629. STAT(steal_user_context);
  630. }
  631. }
  632. void gru_steal_context(struct gru_thread_state *gts, int blade_id)
  633. {
  634. struct gru_blade_state *blade;
  635. struct gru_state *gru, *gru0;
  636. struct gru_thread_state *ngts = NULL;
  637. int ctxnum, ctxnum0, flag = 0, cbr, dsr;
  638. cbr = gts->ts_cbr_au_count;
  639. dsr = gts->ts_dsr_au_count;
  640. blade = gru_base[blade_id];
  641. spin_lock(&blade->bs_lock);
  642. ctxnum = next_ctxnum(blade->bs_lru_ctxnum);
  643. gru = blade->bs_lru_gru;
  644. if (ctxnum == 0)
  645. gru = next_gru(blade, gru);
  646. ctxnum0 = ctxnum;
  647. gru0 = gru;
  648. while (1) {
  649. if (check_gru_resources(gru, cbr, dsr, GRU_NUM_CCH))
  650. break;
  651. spin_lock(&gru->gs_lock);
  652. for (; ctxnum < GRU_NUM_CCH; ctxnum++) {
  653. if (flag && gru == gru0 && ctxnum == ctxnum0)
  654. break;
  655. ngts = gru->gs_gts[ctxnum];
  656. /*
  657. * We are grabbing locks out of order, so trylock is
  658. * needed. GTSs are usually not locked, so the odds of
  659. * success are high. If trylock fails, try to steal a
  660. * different GSEG.
  661. */
  662. if (ngts && is_gts_stealable(ngts, blade))
  663. break;
  664. ngts = NULL;
  665. flag = 1;
  666. }
  667. spin_unlock(&gru->gs_lock);
  668. if (ngts || (flag && gru == gru0 && ctxnum == ctxnum0))
  669. break;
  670. ctxnum = 0;
  671. gru = next_gru(blade, gru);
  672. }
  673. blade->bs_lru_gru = gru;
  674. blade->bs_lru_ctxnum = ctxnum;
  675. spin_unlock(&blade->bs_lock);
  676. if (ngts) {
  677. gts->ustats.context_stolen++;
  678. ngts->ts_steal_jiffies = jiffies;
  679. gru_unload_context(ngts, is_kernel_context(ngts) ? 0 : 1);
  680. gts_stolen(ngts, blade);
  681. } else {
  682. STAT(steal_context_failed);
  683. }
  684. gru_dbg(grudev,
  685. "stole gid %d, ctxnum %d from gts %p. Need cb %d, ds %d;"
  686. " avail cb %ld, ds %ld\n",
  687. gru->gs_gid, ctxnum, ngts, cbr, dsr, hweight64(gru->gs_cbr_map),
  688. hweight64(gru->gs_dsr_map));
  689. }
  690. /*
  691. * Scan the GRUs on the local blade & assign a GRU context.
  692. */
  693. struct gru_state *gru_assign_gru_context(struct gru_thread_state *gts,
  694. int blade)
  695. {
  696. struct gru_state *gru, *grux;
  697. int i, max_active_contexts;
  698. again:
  699. gru = NULL;
  700. max_active_contexts = GRU_NUM_CCH;
  701. for_each_gru_on_blade(grux, blade, i) {
  702. if (check_gru_resources(grux, gts->ts_cbr_au_count,
  703. gts->ts_dsr_au_count,
  704. max_active_contexts)) {
  705. gru = grux;
  706. max_active_contexts = grux->gs_active_contexts;
  707. if (max_active_contexts == 0)
  708. break;
  709. }
  710. }
  711. if (gru) {
  712. spin_lock(&gru->gs_lock);
  713. if (!check_gru_resources(gru, gts->ts_cbr_au_count,
  714. gts->ts_dsr_au_count, GRU_NUM_CCH)) {
  715. spin_unlock(&gru->gs_lock);
  716. goto again;
  717. }
  718. reserve_gru_resources(gru, gts);
  719. gts->ts_gru = gru;
  720. gts->ts_blade = gru->gs_blade_id;
  721. gts->ts_ctxnum =
  722. find_first_zero_bit(&gru->gs_context_map, GRU_NUM_CCH);
  723. BUG_ON(gts->ts_ctxnum == GRU_NUM_CCH);
  724. atomic_inc(&gts->ts_refcnt);
  725. gru->gs_gts[gts->ts_ctxnum] = gts;
  726. __set_bit(gts->ts_ctxnum, &gru->gs_context_map);
  727. spin_unlock(&gru->gs_lock);
  728. STAT(assign_context);
  729. gru_dbg(grudev,
  730. "gseg %p, gts %p, gid %d, ctx %d, cbr %d, dsr %d\n",
  731. gseg_virtual_address(gts->ts_gru, gts->ts_ctxnum), gts,
  732. gts->ts_gru->gs_gid, gts->ts_ctxnum,
  733. gts->ts_cbr_au_count, gts->ts_dsr_au_count);
  734. } else {
  735. gru_dbg(grudev, "failed to allocate a GTS %s\n", "");
  736. STAT(assign_context_failed);
  737. }
  738. return gru;
  739. }
  740. /*
  741. * gru_nopage
  742. *
  743. * Map the user's GRU segment
  744. *
  745. * Note: gru segments alway mmaped on GRU_GSEG_PAGESIZE boundaries.
  746. */
  747. int gru_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  748. {
  749. struct gru_thread_state *gts;
  750. unsigned long paddr, vaddr;
  751. int blade_id;
  752. vaddr = (unsigned long)vmf->virtual_address;
  753. gru_dbg(grudev, "vma %p, vaddr 0x%lx (0x%lx)\n",
  754. vma, vaddr, GSEG_BASE(vaddr));
  755. STAT(nopfn);
  756. /* The following check ensures vaddr is a valid address in the VMA */
  757. gts = gru_find_thread_state(vma, TSID(vaddr, vma));
  758. if (!gts)
  759. return VM_FAULT_SIGBUS;
  760. again:
  761. mutex_lock(&gts->ts_ctxlock);
  762. preempt_disable();
  763. blade_id = uv_numa_blade_id();
  764. if (gts->ts_gru) {
  765. if (gts->ts_gru->gs_blade_id != blade_id) {
  766. STAT(migrated_nopfn_unload);
  767. gru_unload_context(gts, 1);
  768. } else {
  769. if (gru_retarget_intr(gts))
  770. STAT(migrated_nopfn_retarget);
  771. }
  772. }
  773. if (!gts->ts_gru) {
  774. STAT(load_user_context);
  775. if (!gru_assign_gru_context(gts, blade_id)) {
  776. preempt_enable();
  777. mutex_unlock(&gts->ts_ctxlock);
  778. set_current_state(TASK_INTERRUPTIBLE);
  779. schedule_timeout(GRU_ASSIGN_DELAY); /* true hack ZZZ */
  780. blade_id = uv_numa_blade_id();
  781. if (gts->ts_steal_jiffies + GRU_STEAL_DELAY < jiffies)
  782. gru_steal_context(gts, blade_id);
  783. goto again;
  784. }
  785. gru_load_context(gts);
  786. paddr = gseg_physical_address(gts->ts_gru, gts->ts_ctxnum);
  787. remap_pfn_range(vma, vaddr & ~(GRU_GSEG_PAGESIZE - 1),
  788. paddr >> PAGE_SHIFT, GRU_GSEG_PAGESIZE,
  789. vma->vm_page_prot);
  790. }
  791. preempt_enable();
  792. mutex_unlock(&gts->ts_ctxlock);
  793. return VM_FAULT_NOPAGE;
  794. }