grumain.c 24 KB

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