grufault.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. /*
  2. * SN Platform GRU Driver
  3. *
  4. * FAULT HANDLER FOR GRU DETECTED TLB MISSES
  5. *
  6. * This file contains code that handles TLB misses within the GRU.
  7. * These misses are reported either via interrupts or user polling of
  8. * the user CB.
  9. *
  10. * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/mm.h>
  30. #include <linux/hugetlb.h>
  31. #include <linux/device.h>
  32. #include <linux/io.h>
  33. #include <linux/uaccess.h>
  34. #include <linux/security.h>
  35. #include <linux/prefetch.h>
  36. #include <asm/pgtable.h>
  37. #include "gru.h"
  38. #include "grutables.h"
  39. #include "grulib.h"
  40. #include "gru_instructions.h"
  41. #include <asm/uv/uv_hub.h>
  42. /* Return codes for vtop functions */
  43. #define VTOP_SUCCESS 0
  44. #define VTOP_INVALID -1
  45. #define VTOP_RETRY -2
  46. /*
  47. * Test if a physical address is a valid GRU GSEG address
  48. */
  49. static inline int is_gru_paddr(unsigned long paddr)
  50. {
  51. return paddr >= gru_start_paddr && paddr < gru_end_paddr;
  52. }
  53. /*
  54. * Find the vma of a GRU segment. Caller must hold mmap_sem.
  55. */
  56. struct vm_area_struct *gru_find_vma(unsigned long vaddr)
  57. {
  58. struct vm_area_struct *vma;
  59. vma = find_vma(current->mm, vaddr);
  60. if (vma && vma->vm_start <= vaddr && vma->vm_ops == &gru_vm_ops)
  61. return vma;
  62. return NULL;
  63. }
  64. /*
  65. * Find and lock the gts that contains the specified user vaddr.
  66. *
  67. * Returns:
  68. * - *gts with the mmap_sem locked for read and the GTS locked.
  69. * - NULL if vaddr invalid OR is not a valid GSEG vaddr.
  70. */
  71. static struct gru_thread_state *gru_find_lock_gts(unsigned long vaddr)
  72. {
  73. struct mm_struct *mm = current->mm;
  74. struct vm_area_struct *vma;
  75. struct gru_thread_state *gts = NULL;
  76. down_read(&mm->mmap_sem);
  77. vma = gru_find_vma(vaddr);
  78. if (vma)
  79. gts = gru_find_thread_state(vma, TSID(vaddr, vma));
  80. if (gts)
  81. mutex_lock(&gts->ts_ctxlock);
  82. else
  83. up_read(&mm->mmap_sem);
  84. return gts;
  85. }
  86. static struct gru_thread_state *gru_alloc_locked_gts(unsigned long vaddr)
  87. {
  88. struct mm_struct *mm = current->mm;
  89. struct vm_area_struct *vma;
  90. struct gru_thread_state *gts = ERR_PTR(-EINVAL);
  91. down_write(&mm->mmap_sem);
  92. vma = gru_find_vma(vaddr);
  93. if (!vma)
  94. goto err;
  95. gts = gru_alloc_thread_state(vma, TSID(vaddr, vma));
  96. if (IS_ERR(gts))
  97. goto err;
  98. mutex_lock(&gts->ts_ctxlock);
  99. downgrade_write(&mm->mmap_sem);
  100. return gts;
  101. err:
  102. up_write(&mm->mmap_sem);
  103. return gts;
  104. }
  105. /*
  106. * Unlock a GTS that was previously locked with gru_find_lock_gts().
  107. */
  108. static void gru_unlock_gts(struct gru_thread_state *gts)
  109. {
  110. mutex_unlock(&gts->ts_ctxlock);
  111. up_read(&current->mm->mmap_sem);
  112. }
  113. /*
  114. * Set a CB.istatus to active using a user virtual address. This must be done
  115. * just prior to a TFH RESTART. The new cb.istatus is an in-cache status ONLY.
  116. * If the line is evicted, the status may be lost. The in-cache update
  117. * is necessary to prevent the user from seeing a stale cb.istatus that will
  118. * change as soon as the TFH restart is complete. Races may cause an
  119. * occasional failure to clear the cb.istatus, but that is ok.
  120. */
  121. static void gru_cb_set_istatus_active(struct gru_instruction_bits *cbk)
  122. {
  123. if (cbk) {
  124. cbk->istatus = CBS_ACTIVE;
  125. }
  126. }
  127. /*
  128. * Read & clear a TFM
  129. *
  130. * The GRU has an array of fault maps. A map is private to a cpu
  131. * Only one cpu will be accessing a cpu's fault map.
  132. *
  133. * This function scans the cpu-private fault map & clears all bits that
  134. * are set. The function returns a bitmap that indicates the bits that
  135. * were cleared. Note that sense the maps may be updated asynchronously by
  136. * the GRU, atomic operations must be used to clear bits.
  137. */
  138. static void get_clear_fault_map(struct gru_state *gru,
  139. struct gru_tlb_fault_map *imap,
  140. struct gru_tlb_fault_map *dmap)
  141. {
  142. unsigned long i, k;
  143. struct gru_tlb_fault_map *tfm;
  144. tfm = get_tfm_for_cpu(gru, gru_cpu_fault_map_id());
  145. prefetchw(tfm); /* Helps on hardware, required for emulator */
  146. for (i = 0; i < BITS_TO_LONGS(GRU_NUM_CBE); i++) {
  147. k = tfm->fault_bits[i];
  148. if (k)
  149. k = xchg(&tfm->fault_bits[i], 0UL);
  150. imap->fault_bits[i] = k;
  151. k = tfm->done_bits[i];
  152. if (k)
  153. k = xchg(&tfm->done_bits[i], 0UL);
  154. dmap->fault_bits[i] = k;
  155. }
  156. /*
  157. * Not functionally required but helps performance. (Required
  158. * on emulator)
  159. */
  160. gru_flush_cache(tfm);
  161. }
  162. /*
  163. * Atomic (interrupt context) & non-atomic (user context) functions to
  164. * convert a vaddr into a physical address. The size of the page
  165. * is returned in pageshift.
  166. * returns:
  167. * 0 - successful
  168. * < 0 - error code
  169. * 1 - (atomic only) try again in non-atomic context
  170. */
  171. static int non_atomic_pte_lookup(struct vm_area_struct *vma,
  172. unsigned long vaddr, int write,
  173. unsigned long *paddr, int *pageshift)
  174. {
  175. struct page *page;
  176. #ifdef CONFIG_HUGETLB_PAGE
  177. *pageshift = is_vm_hugetlb_page(vma) ? HPAGE_SHIFT : PAGE_SHIFT;
  178. #else
  179. *pageshift = PAGE_SHIFT;
  180. #endif
  181. if (get_user_pages(vaddr, 1, write ? FOLL_WRITE : 0, &page, NULL) <= 0)
  182. return -EFAULT;
  183. *paddr = page_to_phys(page);
  184. put_page(page);
  185. return 0;
  186. }
  187. /*
  188. * atomic_pte_lookup
  189. *
  190. * Convert a user virtual address to a physical address
  191. * Only supports Intel large pages (2MB only) on x86_64.
  192. * ZZZ - hugepage support is incomplete
  193. *
  194. * NOTE: mmap_sem is already held on entry to this function. This
  195. * guarantees existence of the page tables.
  196. */
  197. static int atomic_pte_lookup(struct vm_area_struct *vma, unsigned long vaddr,
  198. int write, unsigned long *paddr, int *pageshift)
  199. {
  200. pgd_t *pgdp;
  201. p4d_t *p4dp;
  202. pud_t *pudp;
  203. pmd_t *pmdp;
  204. pte_t pte;
  205. pgdp = pgd_offset(vma->vm_mm, vaddr);
  206. if (unlikely(pgd_none(*pgdp)))
  207. goto err;
  208. p4dp = p4d_offset(pgdp, vaddr);
  209. if (unlikely(p4d_none(*p4dp)))
  210. goto err;
  211. pudp = pud_offset(p4dp, vaddr);
  212. if (unlikely(pud_none(*pudp)))
  213. goto err;
  214. pmdp = pmd_offset(pudp, vaddr);
  215. if (unlikely(pmd_none(*pmdp)))
  216. goto err;
  217. #ifdef CONFIG_X86_64
  218. if (unlikely(pmd_large(*pmdp)))
  219. pte = *(pte_t *) pmdp;
  220. else
  221. #endif
  222. pte = *pte_offset_kernel(pmdp, vaddr);
  223. if (unlikely(!pte_present(pte) ||
  224. (write && (!pte_write(pte) || !pte_dirty(pte)))))
  225. return 1;
  226. *paddr = pte_pfn(pte) << PAGE_SHIFT;
  227. #ifdef CONFIG_HUGETLB_PAGE
  228. *pageshift = is_vm_hugetlb_page(vma) ? HPAGE_SHIFT : PAGE_SHIFT;
  229. #else
  230. *pageshift = PAGE_SHIFT;
  231. #endif
  232. return 0;
  233. err:
  234. return 1;
  235. }
  236. static int gru_vtop(struct gru_thread_state *gts, unsigned long vaddr,
  237. int write, int atomic, unsigned long *gpa, int *pageshift)
  238. {
  239. struct mm_struct *mm = gts->ts_mm;
  240. struct vm_area_struct *vma;
  241. unsigned long paddr;
  242. int ret, ps;
  243. vma = find_vma(mm, vaddr);
  244. if (!vma)
  245. goto inval;
  246. /*
  247. * Atomic lookup is faster & usually works even if called in non-atomic
  248. * context.
  249. */
  250. rmb(); /* Must/check ms_range_active before loading PTEs */
  251. ret = atomic_pte_lookup(vma, vaddr, write, &paddr, &ps);
  252. if (ret) {
  253. if (atomic)
  254. goto upm;
  255. if (non_atomic_pte_lookup(vma, vaddr, write, &paddr, &ps))
  256. goto inval;
  257. }
  258. if (is_gru_paddr(paddr))
  259. goto inval;
  260. paddr = paddr & ~((1UL << ps) - 1);
  261. *gpa = uv_soc_phys_ram_to_gpa(paddr);
  262. *pageshift = ps;
  263. return VTOP_SUCCESS;
  264. inval:
  265. return VTOP_INVALID;
  266. upm:
  267. return VTOP_RETRY;
  268. }
  269. /*
  270. * Flush a CBE from cache. The CBE is clean in the cache. Dirty the
  271. * CBE cacheline so that the line will be written back to home agent.
  272. * Otherwise the line may be silently dropped. This has no impact
  273. * except on performance.
  274. */
  275. static void gru_flush_cache_cbe(struct gru_control_block_extended *cbe)
  276. {
  277. if (unlikely(cbe)) {
  278. cbe->cbrexecstatus = 0; /* make CL dirty */
  279. gru_flush_cache(cbe);
  280. }
  281. }
  282. /*
  283. * Preload the TLB with entries that may be required. Currently, preloading
  284. * is implemented only for BCOPY. Preload <tlb_preload_count> pages OR to
  285. * the end of the bcopy tranfer, whichever is smaller.
  286. */
  287. static void gru_preload_tlb(struct gru_state *gru,
  288. struct gru_thread_state *gts, int atomic,
  289. unsigned long fault_vaddr, int asid, int write,
  290. unsigned char tlb_preload_count,
  291. struct gru_tlb_fault_handle *tfh,
  292. struct gru_control_block_extended *cbe)
  293. {
  294. unsigned long vaddr = 0, gpa;
  295. int ret, pageshift;
  296. if (cbe->opccpy != OP_BCOPY)
  297. return;
  298. if (fault_vaddr == cbe->cbe_baddr0)
  299. vaddr = fault_vaddr + GRU_CACHE_LINE_BYTES * cbe->cbe_src_cl - 1;
  300. else if (fault_vaddr == cbe->cbe_baddr1)
  301. vaddr = fault_vaddr + (1 << cbe->xtypecpy) * cbe->cbe_nelemcur - 1;
  302. fault_vaddr &= PAGE_MASK;
  303. vaddr &= PAGE_MASK;
  304. vaddr = min(vaddr, fault_vaddr + tlb_preload_count * PAGE_SIZE);
  305. while (vaddr > fault_vaddr) {
  306. ret = gru_vtop(gts, vaddr, write, atomic, &gpa, &pageshift);
  307. if (ret || tfh_write_only(tfh, gpa, GAA_RAM, vaddr, asid, write,
  308. GRU_PAGESIZE(pageshift)))
  309. return;
  310. gru_dbg(grudev,
  311. "%s: gid %d, gts 0x%p, tfh 0x%p, vaddr 0x%lx, asid 0x%x, rw %d, ps %d, gpa 0x%lx\n",
  312. atomic ? "atomic" : "non-atomic", gru->gs_gid, gts, tfh,
  313. vaddr, asid, write, pageshift, gpa);
  314. vaddr -= PAGE_SIZE;
  315. STAT(tlb_preload_page);
  316. }
  317. }
  318. /*
  319. * Drop a TLB entry into the GRU. The fault is described by info in an TFH.
  320. * Input:
  321. * cb Address of user CBR. Null if not running in user context
  322. * Return:
  323. * 0 = dropin, exception, or switch to UPM successful
  324. * 1 = range invalidate active
  325. * < 0 = error code
  326. *
  327. */
  328. static int gru_try_dropin(struct gru_state *gru,
  329. struct gru_thread_state *gts,
  330. struct gru_tlb_fault_handle *tfh,
  331. struct gru_instruction_bits *cbk)
  332. {
  333. struct gru_control_block_extended *cbe = NULL;
  334. unsigned char tlb_preload_count = gts->ts_tlb_preload_count;
  335. int pageshift = 0, asid, write, ret, atomic = !cbk, indexway;
  336. unsigned long gpa = 0, vaddr = 0;
  337. /*
  338. * NOTE: The GRU contains magic hardware that eliminates races between
  339. * TLB invalidates and TLB dropins. If an invalidate occurs
  340. * in the window between reading the TFH and the subsequent TLB dropin,
  341. * the dropin is ignored. This eliminates the need for additional locks.
  342. */
  343. /*
  344. * Prefetch the CBE if doing TLB preloading
  345. */
  346. if (unlikely(tlb_preload_count)) {
  347. cbe = gru_tfh_to_cbe(tfh);
  348. prefetchw(cbe);
  349. }
  350. /*
  351. * Error if TFH state is IDLE or FMM mode & the user issuing a UPM call.
  352. * Might be a hardware race OR a stupid user. Ignore FMM because FMM
  353. * is a transient state.
  354. */
  355. if (tfh->status != TFHSTATUS_EXCEPTION) {
  356. gru_flush_cache(tfh);
  357. sync_core();
  358. if (tfh->status != TFHSTATUS_EXCEPTION)
  359. goto failnoexception;
  360. STAT(tfh_stale_on_fault);
  361. }
  362. if (tfh->state == TFHSTATE_IDLE)
  363. goto failidle;
  364. if (tfh->state == TFHSTATE_MISS_FMM && cbk)
  365. goto failfmm;
  366. write = (tfh->cause & TFHCAUSE_TLB_MOD) != 0;
  367. vaddr = tfh->missvaddr;
  368. asid = tfh->missasid;
  369. indexway = tfh->indexway;
  370. if (asid == 0)
  371. goto failnoasid;
  372. rmb(); /* TFH must be cache resident before reading ms_range_active */
  373. /*
  374. * TFH is cache resident - at least briefly. Fail the dropin
  375. * if a range invalidate is active.
  376. */
  377. if (atomic_read(&gts->ts_gms->ms_range_active))
  378. goto failactive;
  379. ret = gru_vtop(gts, vaddr, write, atomic, &gpa, &pageshift);
  380. if (ret == VTOP_INVALID)
  381. goto failinval;
  382. if (ret == VTOP_RETRY)
  383. goto failupm;
  384. if (!(gts->ts_sizeavail & GRU_SIZEAVAIL(pageshift))) {
  385. gts->ts_sizeavail |= GRU_SIZEAVAIL(pageshift);
  386. if (atomic || !gru_update_cch(gts)) {
  387. gts->ts_force_cch_reload = 1;
  388. goto failupm;
  389. }
  390. }
  391. if (unlikely(cbe) && pageshift == PAGE_SHIFT) {
  392. gru_preload_tlb(gru, gts, atomic, vaddr, asid, write, tlb_preload_count, tfh, cbe);
  393. gru_flush_cache_cbe(cbe);
  394. }
  395. gru_cb_set_istatus_active(cbk);
  396. gts->ustats.tlbdropin++;
  397. tfh_write_restart(tfh, gpa, GAA_RAM, vaddr, asid, write,
  398. GRU_PAGESIZE(pageshift));
  399. gru_dbg(grudev,
  400. "%s: gid %d, gts 0x%p, tfh 0x%p, vaddr 0x%lx, asid 0x%x, indexway 0x%x,"
  401. " rw %d, ps %d, gpa 0x%lx\n",
  402. atomic ? "atomic" : "non-atomic", gru->gs_gid, gts, tfh, vaddr, asid,
  403. indexway, write, pageshift, gpa);
  404. STAT(tlb_dropin);
  405. return 0;
  406. failnoasid:
  407. /* No asid (delayed unload). */
  408. STAT(tlb_dropin_fail_no_asid);
  409. gru_dbg(grudev, "FAILED no_asid tfh: 0x%p, vaddr 0x%lx\n", tfh, vaddr);
  410. if (!cbk)
  411. tfh_user_polling_mode(tfh);
  412. else
  413. gru_flush_cache(tfh);
  414. gru_flush_cache_cbe(cbe);
  415. return -EAGAIN;
  416. failupm:
  417. /* Atomic failure switch CBR to UPM */
  418. tfh_user_polling_mode(tfh);
  419. gru_flush_cache_cbe(cbe);
  420. STAT(tlb_dropin_fail_upm);
  421. gru_dbg(grudev, "FAILED upm tfh: 0x%p, vaddr 0x%lx\n", tfh, vaddr);
  422. return 1;
  423. failfmm:
  424. /* FMM state on UPM call */
  425. gru_flush_cache(tfh);
  426. gru_flush_cache_cbe(cbe);
  427. STAT(tlb_dropin_fail_fmm);
  428. gru_dbg(grudev, "FAILED fmm tfh: 0x%p, state %d\n", tfh, tfh->state);
  429. return 0;
  430. failnoexception:
  431. /* TFH status did not show exception pending */
  432. gru_flush_cache(tfh);
  433. gru_flush_cache_cbe(cbe);
  434. if (cbk)
  435. gru_flush_cache(cbk);
  436. STAT(tlb_dropin_fail_no_exception);
  437. gru_dbg(grudev, "FAILED non-exception tfh: 0x%p, status %d, state %d\n",
  438. tfh, tfh->status, tfh->state);
  439. return 0;
  440. failidle:
  441. /* TFH state was idle - no miss pending */
  442. gru_flush_cache(tfh);
  443. gru_flush_cache_cbe(cbe);
  444. if (cbk)
  445. gru_flush_cache(cbk);
  446. STAT(tlb_dropin_fail_idle);
  447. gru_dbg(grudev, "FAILED idle tfh: 0x%p, state %d\n", tfh, tfh->state);
  448. return 0;
  449. failinval:
  450. /* All errors (atomic & non-atomic) switch CBR to EXCEPTION state */
  451. tfh_exception(tfh);
  452. gru_flush_cache_cbe(cbe);
  453. STAT(tlb_dropin_fail_invalid);
  454. gru_dbg(grudev, "FAILED inval tfh: 0x%p, vaddr 0x%lx\n", tfh, vaddr);
  455. return -EFAULT;
  456. failactive:
  457. /* Range invalidate active. Switch to UPM iff atomic */
  458. if (!cbk)
  459. tfh_user_polling_mode(tfh);
  460. else
  461. gru_flush_cache(tfh);
  462. gru_flush_cache_cbe(cbe);
  463. STAT(tlb_dropin_fail_range_active);
  464. gru_dbg(grudev, "FAILED range active: tfh 0x%p, vaddr 0x%lx\n",
  465. tfh, vaddr);
  466. return 1;
  467. }
  468. /*
  469. * Process an external interrupt from the GRU. This interrupt is
  470. * caused by a TLB miss.
  471. * Note that this is the interrupt handler that is registered with linux
  472. * interrupt handlers.
  473. */
  474. static irqreturn_t gru_intr(int chiplet, int blade)
  475. {
  476. struct gru_state *gru;
  477. struct gru_tlb_fault_map imap, dmap;
  478. struct gru_thread_state *gts;
  479. struct gru_tlb_fault_handle *tfh = NULL;
  480. struct completion *cmp;
  481. int cbrnum, ctxnum;
  482. STAT(intr);
  483. gru = &gru_base[blade]->bs_grus[chiplet];
  484. if (!gru) {
  485. dev_err(grudev, "GRU: invalid interrupt: cpu %d, chiplet %d\n",
  486. raw_smp_processor_id(), chiplet);
  487. return IRQ_NONE;
  488. }
  489. get_clear_fault_map(gru, &imap, &dmap);
  490. gru_dbg(grudev,
  491. "cpu %d, chiplet %d, gid %d, imap %016lx %016lx, dmap %016lx %016lx\n",
  492. smp_processor_id(), chiplet, gru->gs_gid,
  493. imap.fault_bits[0], imap.fault_bits[1],
  494. dmap.fault_bits[0], dmap.fault_bits[1]);
  495. for_each_cbr_in_tfm(cbrnum, dmap.fault_bits) {
  496. STAT(intr_cbr);
  497. cmp = gru->gs_blade->bs_async_wq;
  498. if (cmp)
  499. complete(cmp);
  500. gru_dbg(grudev, "gid %d, cbr_done %d, done %d\n",
  501. gru->gs_gid, cbrnum, cmp ? cmp->done : -1);
  502. }
  503. for_each_cbr_in_tfm(cbrnum, imap.fault_bits) {
  504. STAT(intr_tfh);
  505. tfh = get_tfh_by_index(gru, cbrnum);
  506. prefetchw(tfh); /* Helps on hdw, required for emulator */
  507. /*
  508. * When hardware sets a bit in the faultmap, it implicitly
  509. * locks the GRU context so that it cannot be unloaded.
  510. * The gts cannot change until a TFH start/writestart command
  511. * is issued.
  512. */
  513. ctxnum = tfh->ctxnum;
  514. gts = gru->gs_gts[ctxnum];
  515. /* Spurious interrupts can cause this. Ignore. */
  516. if (!gts) {
  517. STAT(intr_spurious);
  518. continue;
  519. }
  520. /*
  521. * This is running in interrupt context. Trylock the mmap_sem.
  522. * If it fails, retry the fault in user context.
  523. */
  524. gts->ustats.fmm_tlbmiss++;
  525. if (!gts->ts_force_cch_reload &&
  526. down_read_trylock(&gts->ts_mm->mmap_sem)) {
  527. gru_try_dropin(gru, gts, tfh, NULL);
  528. up_read(&gts->ts_mm->mmap_sem);
  529. } else {
  530. tfh_user_polling_mode(tfh);
  531. STAT(intr_mm_lock_failed);
  532. }
  533. }
  534. return IRQ_HANDLED;
  535. }
  536. irqreturn_t gru0_intr(int irq, void *dev_id)
  537. {
  538. return gru_intr(0, uv_numa_blade_id());
  539. }
  540. irqreturn_t gru1_intr(int irq, void *dev_id)
  541. {
  542. return gru_intr(1, uv_numa_blade_id());
  543. }
  544. irqreturn_t gru_intr_mblade(int irq, void *dev_id)
  545. {
  546. int blade;
  547. for_each_possible_blade(blade) {
  548. if (uv_blade_nr_possible_cpus(blade))
  549. continue;
  550. gru_intr(0, blade);
  551. gru_intr(1, blade);
  552. }
  553. return IRQ_HANDLED;
  554. }
  555. static int gru_user_dropin(struct gru_thread_state *gts,
  556. struct gru_tlb_fault_handle *tfh,
  557. void *cb)
  558. {
  559. struct gru_mm_struct *gms = gts->ts_gms;
  560. int ret;
  561. gts->ustats.upm_tlbmiss++;
  562. while (1) {
  563. wait_event(gms->ms_wait_queue,
  564. atomic_read(&gms->ms_range_active) == 0);
  565. prefetchw(tfh); /* Helps on hdw, required for emulator */
  566. ret = gru_try_dropin(gts->ts_gru, gts, tfh, cb);
  567. if (ret <= 0)
  568. return ret;
  569. STAT(call_os_wait_queue);
  570. }
  571. }
  572. /*
  573. * This interface is called as a result of a user detecting a "call OS" bit
  574. * in a user CB. Normally means that a TLB fault has occurred.
  575. * cb - user virtual address of the CB
  576. */
  577. int gru_handle_user_call_os(unsigned long cb)
  578. {
  579. struct gru_tlb_fault_handle *tfh;
  580. struct gru_thread_state *gts;
  581. void *cbk;
  582. int ucbnum, cbrnum, ret = -EINVAL;
  583. STAT(call_os);
  584. /* sanity check the cb pointer */
  585. ucbnum = get_cb_number((void *)cb);
  586. if ((cb & (GRU_HANDLE_STRIDE - 1)) || ucbnum >= GRU_NUM_CB)
  587. return -EINVAL;
  588. gts = gru_find_lock_gts(cb);
  589. if (!gts)
  590. return -EINVAL;
  591. gru_dbg(grudev, "address 0x%lx, gid %d, gts 0x%p\n", cb, gts->ts_gru ? gts->ts_gru->gs_gid : -1, gts);
  592. if (ucbnum >= gts->ts_cbr_au_count * GRU_CBR_AU_SIZE)
  593. goto exit;
  594. gru_check_context_placement(gts);
  595. /*
  596. * CCH may contain stale data if ts_force_cch_reload is set.
  597. */
  598. if (gts->ts_gru && gts->ts_force_cch_reload) {
  599. gts->ts_force_cch_reload = 0;
  600. gru_update_cch(gts);
  601. }
  602. ret = -EAGAIN;
  603. cbrnum = thread_cbr_number(gts, ucbnum);
  604. if (gts->ts_gru) {
  605. tfh = get_tfh_by_index(gts->ts_gru, cbrnum);
  606. cbk = get_gseg_base_address_cb(gts->ts_gru->gs_gru_base_vaddr,
  607. gts->ts_ctxnum, ucbnum);
  608. ret = gru_user_dropin(gts, tfh, cbk);
  609. }
  610. exit:
  611. gru_unlock_gts(gts);
  612. return ret;
  613. }
  614. /*
  615. * Fetch the exception detail information for a CB that terminated with
  616. * an exception.
  617. */
  618. int gru_get_exception_detail(unsigned long arg)
  619. {
  620. struct control_block_extended_exc_detail excdet;
  621. struct gru_control_block_extended *cbe;
  622. struct gru_thread_state *gts;
  623. int ucbnum, cbrnum, ret;
  624. STAT(user_exception);
  625. if (copy_from_user(&excdet, (void __user *)arg, sizeof(excdet)))
  626. return -EFAULT;
  627. gts = gru_find_lock_gts(excdet.cb);
  628. if (!gts)
  629. return -EINVAL;
  630. gru_dbg(grudev, "address 0x%lx, gid %d, gts 0x%p\n", excdet.cb, gts->ts_gru ? gts->ts_gru->gs_gid : -1, gts);
  631. ucbnum = get_cb_number((void *)excdet.cb);
  632. if (ucbnum >= gts->ts_cbr_au_count * GRU_CBR_AU_SIZE) {
  633. ret = -EINVAL;
  634. } else if (gts->ts_gru) {
  635. cbrnum = thread_cbr_number(gts, ucbnum);
  636. cbe = get_cbe_by_index(gts->ts_gru, cbrnum);
  637. gru_flush_cache(cbe); /* CBE not coherent */
  638. sync_core(); /* make sure we are have current data */
  639. excdet.opc = cbe->opccpy;
  640. excdet.exopc = cbe->exopccpy;
  641. excdet.ecause = cbe->ecause;
  642. excdet.exceptdet0 = cbe->idef1upd;
  643. excdet.exceptdet1 = cbe->idef3upd;
  644. excdet.cbrstate = cbe->cbrstate;
  645. excdet.cbrexecstatus = cbe->cbrexecstatus;
  646. gru_flush_cache_cbe(cbe);
  647. ret = 0;
  648. } else {
  649. ret = -EAGAIN;
  650. }
  651. gru_unlock_gts(gts);
  652. gru_dbg(grudev,
  653. "cb 0x%lx, op %d, exopc %d, cbrstate %d, cbrexecstatus 0x%x, ecause 0x%x, "
  654. "exdet0 0x%lx, exdet1 0x%x\n",
  655. excdet.cb, excdet.opc, excdet.exopc, excdet.cbrstate, excdet.cbrexecstatus,
  656. excdet.ecause, excdet.exceptdet0, excdet.exceptdet1);
  657. if (!ret && copy_to_user((void __user *)arg, &excdet, sizeof(excdet)))
  658. ret = -EFAULT;
  659. return ret;
  660. }
  661. /*
  662. * User request to unload a context. Content is saved for possible reload.
  663. */
  664. static int gru_unload_all_contexts(void)
  665. {
  666. struct gru_thread_state *gts;
  667. struct gru_state *gru;
  668. int gid, ctxnum;
  669. if (!capable(CAP_SYS_ADMIN))
  670. return -EPERM;
  671. foreach_gid(gid) {
  672. gru = GID_TO_GRU(gid);
  673. spin_lock(&gru->gs_lock);
  674. for (ctxnum = 0; ctxnum < GRU_NUM_CCH; ctxnum++) {
  675. gts = gru->gs_gts[ctxnum];
  676. if (gts && mutex_trylock(&gts->ts_ctxlock)) {
  677. spin_unlock(&gru->gs_lock);
  678. gru_unload_context(gts, 1);
  679. mutex_unlock(&gts->ts_ctxlock);
  680. spin_lock(&gru->gs_lock);
  681. }
  682. }
  683. spin_unlock(&gru->gs_lock);
  684. }
  685. return 0;
  686. }
  687. int gru_user_unload_context(unsigned long arg)
  688. {
  689. struct gru_thread_state *gts;
  690. struct gru_unload_context_req req;
  691. STAT(user_unload_context);
  692. if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
  693. return -EFAULT;
  694. gru_dbg(grudev, "gseg 0x%lx\n", req.gseg);
  695. if (!req.gseg)
  696. return gru_unload_all_contexts();
  697. gts = gru_find_lock_gts(req.gseg);
  698. if (!gts)
  699. return -EINVAL;
  700. if (gts->ts_gru)
  701. gru_unload_context(gts, 1);
  702. gru_unlock_gts(gts);
  703. return 0;
  704. }
  705. /*
  706. * User request to flush a range of virtual addresses from the GRU TLB
  707. * (Mainly for testing).
  708. */
  709. int gru_user_flush_tlb(unsigned long arg)
  710. {
  711. struct gru_thread_state *gts;
  712. struct gru_flush_tlb_req req;
  713. struct gru_mm_struct *gms;
  714. STAT(user_flush_tlb);
  715. if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
  716. return -EFAULT;
  717. gru_dbg(grudev, "gseg 0x%lx, vaddr 0x%lx, len 0x%lx\n", req.gseg,
  718. req.vaddr, req.len);
  719. gts = gru_find_lock_gts(req.gseg);
  720. if (!gts)
  721. return -EINVAL;
  722. gms = gts->ts_gms;
  723. gru_unlock_gts(gts);
  724. gru_flush_tlb_range(gms, req.vaddr, req.len);
  725. return 0;
  726. }
  727. /*
  728. * Fetch GSEG statisticss
  729. */
  730. long gru_get_gseg_statistics(unsigned long arg)
  731. {
  732. struct gru_thread_state *gts;
  733. struct gru_get_gseg_statistics_req req;
  734. if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
  735. return -EFAULT;
  736. /*
  737. * The library creates arrays of contexts for threaded programs.
  738. * If no gts exists in the array, the context has never been used & all
  739. * statistics are implicitly 0.
  740. */
  741. gts = gru_find_lock_gts(req.gseg);
  742. if (gts) {
  743. memcpy(&req.stats, &gts->ustats, sizeof(gts->ustats));
  744. gru_unlock_gts(gts);
  745. } else {
  746. memset(&req.stats, 0, sizeof(gts->ustats));
  747. }
  748. if (copy_to_user((void __user *)arg, &req, sizeof(req)))
  749. return -EFAULT;
  750. return 0;
  751. }
  752. /*
  753. * Register the current task as the user of the GSEG slice.
  754. * Needed for TLB fault interrupt targeting.
  755. */
  756. int gru_set_context_option(unsigned long arg)
  757. {
  758. struct gru_thread_state *gts;
  759. struct gru_set_context_option_req req;
  760. int ret = 0;
  761. STAT(set_context_option);
  762. if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
  763. return -EFAULT;
  764. gru_dbg(grudev, "op %d, gseg 0x%lx, value1 0x%lx\n", req.op, req.gseg, req.val1);
  765. gts = gru_find_lock_gts(req.gseg);
  766. if (!gts) {
  767. gts = gru_alloc_locked_gts(req.gseg);
  768. if (IS_ERR(gts))
  769. return PTR_ERR(gts);
  770. }
  771. switch (req.op) {
  772. case sco_blade_chiplet:
  773. /* Select blade/chiplet for GRU context */
  774. if (req.val0 < -1 || req.val0 >= GRU_CHIPLETS_PER_HUB ||
  775. req.val1 < -1 || req.val1 >= GRU_MAX_BLADES ||
  776. (req.val1 >= 0 && !gru_base[req.val1])) {
  777. ret = -EINVAL;
  778. } else {
  779. gts->ts_user_blade_id = req.val1;
  780. gts->ts_user_chiplet_id = req.val0;
  781. gru_check_context_placement(gts);
  782. }
  783. break;
  784. case sco_gseg_owner:
  785. /* Register the current task as the GSEG owner */
  786. gts->ts_tgid_owner = current->tgid;
  787. break;
  788. case sco_cch_req_slice:
  789. /* Set the CCH slice option */
  790. gts->ts_cch_req_slice = req.val1 & 3;
  791. break;
  792. default:
  793. ret = -EINVAL;
  794. }
  795. gru_unlock_gts(gts);
  796. return ret;
  797. }