grufault.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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
  182. (current, current->mm, vaddr, 1, write ? FOLL_WRITE : 0, &page, NULL) <= 0)
  183. return -EFAULT;
  184. *paddr = page_to_phys(page);
  185. put_page(page);
  186. return 0;
  187. }
  188. /*
  189. * atomic_pte_lookup
  190. *
  191. * Convert a user virtual address to a physical address
  192. * Only supports Intel large pages (2MB only) on x86_64.
  193. * ZZZ - hugepage support is incomplete
  194. *
  195. * NOTE: mmap_sem is already held on entry to this function. This
  196. * guarantees existence of the page tables.
  197. */
  198. static int atomic_pte_lookup(struct vm_area_struct *vma, unsigned long vaddr,
  199. int write, unsigned long *paddr, int *pageshift)
  200. {
  201. pgd_t *pgdp;
  202. pmd_t *pmdp;
  203. pud_t *pudp;
  204. pte_t pte;
  205. pgdp = pgd_offset(vma->vm_mm, vaddr);
  206. if (unlikely(pgd_none(*pgdp)))
  207. goto err;
  208. pudp = pud_offset(pgdp, vaddr);
  209. if (unlikely(pud_none(*pudp)))
  210. goto err;
  211. pmdp = pmd_offset(pudp, vaddr);
  212. if (unlikely(pmd_none(*pmdp)))
  213. goto err;
  214. #ifdef CONFIG_X86_64
  215. if (unlikely(pmd_large(*pmdp)))
  216. pte = *(pte_t *) pmdp;
  217. else
  218. #endif
  219. pte = *pte_offset_kernel(pmdp, vaddr);
  220. if (unlikely(!pte_present(pte) ||
  221. (write && (!pte_write(pte) || !pte_dirty(pte)))))
  222. return 1;
  223. *paddr = pte_pfn(pte) << PAGE_SHIFT;
  224. #ifdef CONFIG_HUGETLB_PAGE
  225. *pageshift = is_vm_hugetlb_page(vma) ? HPAGE_SHIFT : PAGE_SHIFT;
  226. #else
  227. *pageshift = PAGE_SHIFT;
  228. #endif
  229. return 0;
  230. err:
  231. return 1;
  232. }
  233. static int gru_vtop(struct gru_thread_state *gts, unsigned long vaddr,
  234. int write, int atomic, unsigned long *gpa, int *pageshift)
  235. {
  236. struct mm_struct *mm = gts->ts_mm;
  237. struct vm_area_struct *vma;
  238. unsigned long paddr;
  239. int ret, ps;
  240. vma = find_vma(mm, vaddr);
  241. if (!vma)
  242. goto inval;
  243. /*
  244. * Atomic lookup is faster & usually works even if called in non-atomic
  245. * context.
  246. */
  247. rmb(); /* Must/check ms_range_active before loading PTEs */
  248. ret = atomic_pte_lookup(vma, vaddr, write, &paddr, &ps);
  249. if (ret) {
  250. if (atomic)
  251. goto upm;
  252. if (non_atomic_pte_lookup(vma, vaddr, write, &paddr, &ps))
  253. goto inval;
  254. }
  255. if (is_gru_paddr(paddr))
  256. goto inval;
  257. paddr = paddr & ~((1UL << ps) - 1);
  258. *gpa = uv_soc_phys_ram_to_gpa(paddr);
  259. *pageshift = ps;
  260. return VTOP_SUCCESS;
  261. inval:
  262. return VTOP_INVALID;
  263. upm:
  264. return VTOP_RETRY;
  265. }
  266. /*
  267. * Flush a CBE from cache. The CBE is clean in the cache. Dirty the
  268. * CBE cacheline so that the line will be written back to home agent.
  269. * Otherwise the line may be silently dropped. This has no impact
  270. * except on performance.
  271. */
  272. static void gru_flush_cache_cbe(struct gru_control_block_extended *cbe)
  273. {
  274. if (unlikely(cbe)) {
  275. cbe->cbrexecstatus = 0; /* make CL dirty */
  276. gru_flush_cache(cbe);
  277. }
  278. }
  279. /*
  280. * Preload the TLB with entries that may be required. Currently, preloading
  281. * is implemented only for BCOPY. Preload <tlb_preload_count> pages OR to
  282. * the end of the bcopy tranfer, whichever is smaller.
  283. */
  284. static void gru_preload_tlb(struct gru_state *gru,
  285. struct gru_thread_state *gts, int atomic,
  286. unsigned long fault_vaddr, int asid, int write,
  287. unsigned char tlb_preload_count,
  288. struct gru_tlb_fault_handle *tfh,
  289. struct gru_control_block_extended *cbe)
  290. {
  291. unsigned long vaddr = 0, gpa;
  292. int ret, pageshift;
  293. if (cbe->opccpy != OP_BCOPY)
  294. return;
  295. if (fault_vaddr == cbe->cbe_baddr0)
  296. vaddr = fault_vaddr + GRU_CACHE_LINE_BYTES * cbe->cbe_src_cl - 1;
  297. else if (fault_vaddr == cbe->cbe_baddr1)
  298. vaddr = fault_vaddr + (1 << cbe->xtypecpy) * cbe->cbe_nelemcur - 1;
  299. fault_vaddr &= PAGE_MASK;
  300. vaddr &= PAGE_MASK;
  301. vaddr = min(vaddr, fault_vaddr + tlb_preload_count * PAGE_SIZE);
  302. while (vaddr > fault_vaddr) {
  303. ret = gru_vtop(gts, vaddr, write, atomic, &gpa, &pageshift);
  304. if (ret || tfh_write_only(tfh, gpa, GAA_RAM, vaddr, asid, write,
  305. GRU_PAGESIZE(pageshift)))
  306. return;
  307. gru_dbg(grudev,
  308. "%s: gid %d, gts 0x%p, tfh 0x%p, vaddr 0x%lx, asid 0x%x, rw %d, ps %d, gpa 0x%lx\n",
  309. atomic ? "atomic" : "non-atomic", gru->gs_gid, gts, tfh,
  310. vaddr, asid, write, pageshift, gpa);
  311. vaddr -= PAGE_SIZE;
  312. STAT(tlb_preload_page);
  313. }
  314. }
  315. /*
  316. * Drop a TLB entry into the GRU. The fault is described by info in an TFH.
  317. * Input:
  318. * cb Address of user CBR. Null if not running in user context
  319. * Return:
  320. * 0 = dropin, exception, or switch to UPM successful
  321. * 1 = range invalidate active
  322. * < 0 = error code
  323. *
  324. */
  325. static int gru_try_dropin(struct gru_state *gru,
  326. struct gru_thread_state *gts,
  327. struct gru_tlb_fault_handle *tfh,
  328. struct gru_instruction_bits *cbk)
  329. {
  330. struct gru_control_block_extended *cbe = NULL;
  331. unsigned char tlb_preload_count = gts->ts_tlb_preload_count;
  332. int pageshift = 0, asid, write, ret, atomic = !cbk, indexway;
  333. unsigned long gpa = 0, vaddr = 0;
  334. /*
  335. * NOTE: The GRU contains magic hardware that eliminates races between
  336. * TLB invalidates and TLB dropins. If an invalidate occurs
  337. * in the window between reading the TFH and the subsequent TLB dropin,
  338. * the dropin is ignored. This eliminates the need for additional locks.
  339. */
  340. /*
  341. * Prefetch the CBE if doing TLB preloading
  342. */
  343. if (unlikely(tlb_preload_count)) {
  344. cbe = gru_tfh_to_cbe(tfh);
  345. prefetchw(cbe);
  346. }
  347. /*
  348. * Error if TFH state is IDLE or FMM mode & the user issuing a UPM call.
  349. * Might be a hardware race OR a stupid user. Ignore FMM because FMM
  350. * is a transient state.
  351. */
  352. if (tfh->status != TFHSTATUS_EXCEPTION) {
  353. gru_flush_cache(tfh);
  354. sync_core();
  355. if (tfh->status != TFHSTATUS_EXCEPTION)
  356. goto failnoexception;
  357. STAT(tfh_stale_on_fault);
  358. }
  359. if (tfh->state == TFHSTATE_IDLE)
  360. goto failidle;
  361. if (tfh->state == TFHSTATE_MISS_FMM && cbk)
  362. goto failfmm;
  363. write = (tfh->cause & TFHCAUSE_TLB_MOD) != 0;
  364. vaddr = tfh->missvaddr;
  365. asid = tfh->missasid;
  366. indexway = tfh->indexway;
  367. if (asid == 0)
  368. goto failnoasid;
  369. rmb(); /* TFH must be cache resident before reading ms_range_active */
  370. /*
  371. * TFH is cache resident - at least briefly. Fail the dropin
  372. * if a range invalidate is active.
  373. */
  374. if (atomic_read(&gts->ts_gms->ms_range_active))
  375. goto failactive;
  376. ret = gru_vtop(gts, vaddr, write, atomic, &gpa, &pageshift);
  377. if (ret == VTOP_INVALID)
  378. goto failinval;
  379. if (ret == VTOP_RETRY)
  380. goto failupm;
  381. if (!(gts->ts_sizeavail & GRU_SIZEAVAIL(pageshift))) {
  382. gts->ts_sizeavail |= GRU_SIZEAVAIL(pageshift);
  383. if (atomic || !gru_update_cch(gts)) {
  384. gts->ts_force_cch_reload = 1;
  385. goto failupm;
  386. }
  387. }
  388. if (unlikely(cbe) && pageshift == PAGE_SHIFT) {
  389. gru_preload_tlb(gru, gts, atomic, vaddr, asid, write, tlb_preload_count, tfh, cbe);
  390. gru_flush_cache_cbe(cbe);
  391. }
  392. gru_cb_set_istatus_active(cbk);
  393. gts->ustats.tlbdropin++;
  394. tfh_write_restart(tfh, gpa, GAA_RAM, vaddr, asid, write,
  395. GRU_PAGESIZE(pageshift));
  396. gru_dbg(grudev,
  397. "%s: gid %d, gts 0x%p, tfh 0x%p, vaddr 0x%lx, asid 0x%x, indexway 0x%x,"
  398. " rw %d, ps %d, gpa 0x%lx\n",
  399. atomic ? "atomic" : "non-atomic", gru->gs_gid, gts, tfh, vaddr, asid,
  400. indexway, write, pageshift, gpa);
  401. STAT(tlb_dropin);
  402. return 0;
  403. failnoasid:
  404. /* No asid (delayed unload). */
  405. STAT(tlb_dropin_fail_no_asid);
  406. gru_dbg(grudev, "FAILED no_asid tfh: 0x%p, vaddr 0x%lx\n", tfh, vaddr);
  407. if (!cbk)
  408. tfh_user_polling_mode(tfh);
  409. else
  410. gru_flush_cache(tfh);
  411. gru_flush_cache_cbe(cbe);
  412. return -EAGAIN;
  413. failupm:
  414. /* Atomic failure switch CBR to UPM */
  415. tfh_user_polling_mode(tfh);
  416. gru_flush_cache_cbe(cbe);
  417. STAT(tlb_dropin_fail_upm);
  418. gru_dbg(grudev, "FAILED upm tfh: 0x%p, vaddr 0x%lx\n", tfh, vaddr);
  419. return 1;
  420. failfmm:
  421. /* FMM state on UPM call */
  422. gru_flush_cache(tfh);
  423. gru_flush_cache_cbe(cbe);
  424. STAT(tlb_dropin_fail_fmm);
  425. gru_dbg(grudev, "FAILED fmm tfh: 0x%p, state %d\n", tfh, tfh->state);
  426. return 0;
  427. failnoexception:
  428. /* TFH status did not show exception pending */
  429. gru_flush_cache(tfh);
  430. gru_flush_cache_cbe(cbe);
  431. if (cbk)
  432. gru_flush_cache(cbk);
  433. STAT(tlb_dropin_fail_no_exception);
  434. gru_dbg(grudev, "FAILED non-exception tfh: 0x%p, status %d, state %d\n",
  435. tfh, tfh->status, tfh->state);
  436. return 0;
  437. failidle:
  438. /* TFH state was idle - no miss pending */
  439. gru_flush_cache(tfh);
  440. gru_flush_cache_cbe(cbe);
  441. if (cbk)
  442. gru_flush_cache(cbk);
  443. STAT(tlb_dropin_fail_idle);
  444. gru_dbg(grudev, "FAILED idle tfh: 0x%p, state %d\n", tfh, tfh->state);
  445. return 0;
  446. failinval:
  447. /* All errors (atomic & non-atomic) switch CBR to EXCEPTION state */
  448. tfh_exception(tfh);
  449. gru_flush_cache_cbe(cbe);
  450. STAT(tlb_dropin_fail_invalid);
  451. gru_dbg(grudev, "FAILED inval tfh: 0x%p, vaddr 0x%lx\n", tfh, vaddr);
  452. return -EFAULT;
  453. failactive:
  454. /* Range invalidate active. Switch to UPM iff atomic */
  455. if (!cbk)
  456. tfh_user_polling_mode(tfh);
  457. else
  458. gru_flush_cache(tfh);
  459. gru_flush_cache_cbe(cbe);
  460. STAT(tlb_dropin_fail_range_active);
  461. gru_dbg(grudev, "FAILED range active: tfh 0x%p, vaddr 0x%lx\n",
  462. tfh, vaddr);
  463. return 1;
  464. }
  465. /*
  466. * Process an external interrupt from the GRU. This interrupt is
  467. * caused by a TLB miss.
  468. * Note that this is the interrupt handler that is registered with linux
  469. * interrupt handlers.
  470. */
  471. static irqreturn_t gru_intr(int chiplet, int blade)
  472. {
  473. struct gru_state *gru;
  474. struct gru_tlb_fault_map imap, dmap;
  475. struct gru_thread_state *gts;
  476. struct gru_tlb_fault_handle *tfh = NULL;
  477. struct completion *cmp;
  478. int cbrnum, ctxnum;
  479. STAT(intr);
  480. gru = &gru_base[blade]->bs_grus[chiplet];
  481. if (!gru) {
  482. dev_err(grudev, "GRU: invalid interrupt: cpu %d, chiplet %d\n",
  483. raw_smp_processor_id(), chiplet);
  484. return IRQ_NONE;
  485. }
  486. get_clear_fault_map(gru, &imap, &dmap);
  487. gru_dbg(grudev,
  488. "cpu %d, chiplet %d, gid %d, imap %016lx %016lx, dmap %016lx %016lx\n",
  489. smp_processor_id(), chiplet, gru->gs_gid,
  490. imap.fault_bits[0], imap.fault_bits[1],
  491. dmap.fault_bits[0], dmap.fault_bits[1]);
  492. for_each_cbr_in_tfm(cbrnum, dmap.fault_bits) {
  493. STAT(intr_cbr);
  494. cmp = gru->gs_blade->bs_async_wq;
  495. if (cmp)
  496. complete(cmp);
  497. gru_dbg(grudev, "gid %d, cbr_done %d, done %d\n",
  498. gru->gs_gid, cbrnum, cmp ? cmp->done : -1);
  499. }
  500. for_each_cbr_in_tfm(cbrnum, imap.fault_bits) {
  501. STAT(intr_tfh);
  502. tfh = get_tfh_by_index(gru, cbrnum);
  503. prefetchw(tfh); /* Helps on hdw, required for emulator */
  504. /*
  505. * When hardware sets a bit in the faultmap, it implicitly
  506. * locks the GRU context so that it cannot be unloaded.
  507. * The gts cannot change until a TFH start/writestart command
  508. * is issued.
  509. */
  510. ctxnum = tfh->ctxnum;
  511. gts = gru->gs_gts[ctxnum];
  512. /* Spurious interrupts can cause this. Ignore. */
  513. if (!gts) {
  514. STAT(intr_spurious);
  515. continue;
  516. }
  517. /*
  518. * This is running in interrupt context. Trylock the mmap_sem.
  519. * If it fails, retry the fault in user context.
  520. */
  521. gts->ustats.fmm_tlbmiss++;
  522. if (!gts->ts_force_cch_reload &&
  523. down_read_trylock(&gts->ts_mm->mmap_sem)) {
  524. gru_try_dropin(gru, gts, tfh, NULL);
  525. up_read(&gts->ts_mm->mmap_sem);
  526. } else {
  527. tfh_user_polling_mode(tfh);
  528. STAT(intr_mm_lock_failed);
  529. }
  530. }
  531. return IRQ_HANDLED;
  532. }
  533. irqreturn_t gru0_intr(int irq, void *dev_id)
  534. {
  535. return gru_intr(0, uv_numa_blade_id());
  536. }
  537. irqreturn_t gru1_intr(int irq, void *dev_id)
  538. {
  539. return gru_intr(1, uv_numa_blade_id());
  540. }
  541. irqreturn_t gru_intr_mblade(int irq, void *dev_id)
  542. {
  543. int blade;
  544. for_each_possible_blade(blade) {
  545. if (uv_blade_nr_possible_cpus(blade))
  546. continue;
  547. gru_intr(0, blade);
  548. gru_intr(1, blade);
  549. }
  550. return IRQ_HANDLED;
  551. }
  552. static int gru_user_dropin(struct gru_thread_state *gts,
  553. struct gru_tlb_fault_handle *tfh,
  554. void *cb)
  555. {
  556. struct gru_mm_struct *gms = gts->ts_gms;
  557. int ret;
  558. gts->ustats.upm_tlbmiss++;
  559. while (1) {
  560. wait_event(gms->ms_wait_queue,
  561. atomic_read(&gms->ms_range_active) == 0);
  562. prefetchw(tfh); /* Helps on hdw, required for emulator */
  563. ret = gru_try_dropin(gts->ts_gru, gts, tfh, cb);
  564. if (ret <= 0)
  565. return ret;
  566. STAT(call_os_wait_queue);
  567. }
  568. }
  569. /*
  570. * This interface is called as a result of a user detecting a "call OS" bit
  571. * in a user CB. Normally means that a TLB fault has occurred.
  572. * cb - user virtual address of the CB
  573. */
  574. int gru_handle_user_call_os(unsigned long cb)
  575. {
  576. struct gru_tlb_fault_handle *tfh;
  577. struct gru_thread_state *gts;
  578. void *cbk;
  579. int ucbnum, cbrnum, ret = -EINVAL;
  580. STAT(call_os);
  581. /* sanity check the cb pointer */
  582. ucbnum = get_cb_number((void *)cb);
  583. if ((cb & (GRU_HANDLE_STRIDE - 1)) || ucbnum >= GRU_NUM_CB)
  584. return -EINVAL;
  585. gts = gru_find_lock_gts(cb);
  586. if (!gts)
  587. return -EINVAL;
  588. gru_dbg(grudev, "address 0x%lx, gid %d, gts 0x%p\n", cb, gts->ts_gru ? gts->ts_gru->gs_gid : -1, gts);
  589. if (ucbnum >= gts->ts_cbr_au_count * GRU_CBR_AU_SIZE)
  590. goto exit;
  591. gru_check_context_placement(gts);
  592. /*
  593. * CCH may contain stale data if ts_force_cch_reload is set.
  594. */
  595. if (gts->ts_gru && gts->ts_force_cch_reload) {
  596. gts->ts_force_cch_reload = 0;
  597. gru_update_cch(gts);
  598. }
  599. ret = -EAGAIN;
  600. cbrnum = thread_cbr_number(gts, ucbnum);
  601. if (gts->ts_gru) {
  602. tfh = get_tfh_by_index(gts->ts_gru, cbrnum);
  603. cbk = get_gseg_base_address_cb(gts->ts_gru->gs_gru_base_vaddr,
  604. gts->ts_ctxnum, ucbnum);
  605. ret = gru_user_dropin(gts, tfh, cbk);
  606. }
  607. exit:
  608. gru_unlock_gts(gts);
  609. return ret;
  610. }
  611. /*
  612. * Fetch the exception detail information for a CB that terminated with
  613. * an exception.
  614. */
  615. int gru_get_exception_detail(unsigned long arg)
  616. {
  617. struct control_block_extended_exc_detail excdet;
  618. struct gru_control_block_extended *cbe;
  619. struct gru_thread_state *gts;
  620. int ucbnum, cbrnum, ret;
  621. STAT(user_exception);
  622. if (copy_from_user(&excdet, (void __user *)arg, sizeof(excdet)))
  623. return -EFAULT;
  624. gts = gru_find_lock_gts(excdet.cb);
  625. if (!gts)
  626. return -EINVAL;
  627. gru_dbg(grudev, "address 0x%lx, gid %d, gts 0x%p\n", excdet.cb, gts->ts_gru ? gts->ts_gru->gs_gid : -1, gts);
  628. ucbnum = get_cb_number((void *)excdet.cb);
  629. if (ucbnum >= gts->ts_cbr_au_count * GRU_CBR_AU_SIZE) {
  630. ret = -EINVAL;
  631. } else if (gts->ts_gru) {
  632. cbrnum = thread_cbr_number(gts, ucbnum);
  633. cbe = get_cbe_by_index(gts->ts_gru, cbrnum);
  634. gru_flush_cache(cbe); /* CBE not coherent */
  635. sync_core(); /* make sure we are have current data */
  636. excdet.opc = cbe->opccpy;
  637. excdet.exopc = cbe->exopccpy;
  638. excdet.ecause = cbe->ecause;
  639. excdet.exceptdet0 = cbe->idef1upd;
  640. excdet.exceptdet1 = cbe->idef3upd;
  641. excdet.cbrstate = cbe->cbrstate;
  642. excdet.cbrexecstatus = cbe->cbrexecstatus;
  643. gru_flush_cache_cbe(cbe);
  644. ret = 0;
  645. } else {
  646. ret = -EAGAIN;
  647. }
  648. gru_unlock_gts(gts);
  649. gru_dbg(grudev,
  650. "cb 0x%lx, op %d, exopc %d, cbrstate %d, cbrexecstatus 0x%x, ecause 0x%x, "
  651. "exdet0 0x%lx, exdet1 0x%x\n",
  652. excdet.cb, excdet.opc, excdet.exopc, excdet.cbrstate, excdet.cbrexecstatus,
  653. excdet.ecause, excdet.exceptdet0, excdet.exceptdet1);
  654. if (!ret && copy_to_user((void __user *)arg, &excdet, sizeof(excdet)))
  655. ret = -EFAULT;
  656. return ret;
  657. }
  658. /*
  659. * User request to unload a context. Content is saved for possible reload.
  660. */
  661. static int gru_unload_all_contexts(void)
  662. {
  663. struct gru_thread_state *gts;
  664. struct gru_state *gru;
  665. int gid, ctxnum;
  666. if (!capable(CAP_SYS_ADMIN))
  667. return -EPERM;
  668. foreach_gid(gid) {
  669. gru = GID_TO_GRU(gid);
  670. spin_lock(&gru->gs_lock);
  671. for (ctxnum = 0; ctxnum < GRU_NUM_CCH; ctxnum++) {
  672. gts = gru->gs_gts[ctxnum];
  673. if (gts && mutex_trylock(&gts->ts_ctxlock)) {
  674. spin_unlock(&gru->gs_lock);
  675. gru_unload_context(gts, 1);
  676. mutex_unlock(&gts->ts_ctxlock);
  677. spin_lock(&gru->gs_lock);
  678. }
  679. }
  680. spin_unlock(&gru->gs_lock);
  681. }
  682. return 0;
  683. }
  684. int gru_user_unload_context(unsigned long arg)
  685. {
  686. struct gru_thread_state *gts;
  687. struct gru_unload_context_req req;
  688. STAT(user_unload_context);
  689. if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
  690. return -EFAULT;
  691. gru_dbg(grudev, "gseg 0x%lx\n", req.gseg);
  692. if (!req.gseg)
  693. return gru_unload_all_contexts();
  694. gts = gru_find_lock_gts(req.gseg);
  695. if (!gts)
  696. return -EINVAL;
  697. if (gts->ts_gru)
  698. gru_unload_context(gts, 1);
  699. gru_unlock_gts(gts);
  700. return 0;
  701. }
  702. /*
  703. * User request to flush a range of virtual addresses from the GRU TLB
  704. * (Mainly for testing).
  705. */
  706. int gru_user_flush_tlb(unsigned long arg)
  707. {
  708. struct gru_thread_state *gts;
  709. struct gru_flush_tlb_req req;
  710. struct gru_mm_struct *gms;
  711. STAT(user_flush_tlb);
  712. if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
  713. return -EFAULT;
  714. gru_dbg(grudev, "gseg 0x%lx, vaddr 0x%lx, len 0x%lx\n", req.gseg,
  715. req.vaddr, req.len);
  716. gts = gru_find_lock_gts(req.gseg);
  717. if (!gts)
  718. return -EINVAL;
  719. gms = gts->ts_gms;
  720. gru_unlock_gts(gts);
  721. gru_flush_tlb_range(gms, req.vaddr, req.len);
  722. return 0;
  723. }
  724. /*
  725. * Fetch GSEG statisticss
  726. */
  727. long gru_get_gseg_statistics(unsigned long arg)
  728. {
  729. struct gru_thread_state *gts;
  730. struct gru_get_gseg_statistics_req req;
  731. if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
  732. return -EFAULT;
  733. /*
  734. * The library creates arrays of contexts for threaded programs.
  735. * If no gts exists in the array, the context has never been used & all
  736. * statistics are implicitly 0.
  737. */
  738. gts = gru_find_lock_gts(req.gseg);
  739. if (gts) {
  740. memcpy(&req.stats, &gts->ustats, sizeof(gts->ustats));
  741. gru_unlock_gts(gts);
  742. } else {
  743. memset(&req.stats, 0, sizeof(gts->ustats));
  744. }
  745. if (copy_to_user((void __user *)arg, &req, sizeof(req)))
  746. return -EFAULT;
  747. return 0;
  748. }
  749. /*
  750. * Register the current task as the user of the GSEG slice.
  751. * Needed for TLB fault interrupt targeting.
  752. */
  753. int gru_set_context_option(unsigned long arg)
  754. {
  755. struct gru_thread_state *gts;
  756. struct gru_set_context_option_req req;
  757. int ret = 0;
  758. STAT(set_context_option);
  759. if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
  760. return -EFAULT;
  761. gru_dbg(grudev, "op %d, gseg 0x%lx, value1 0x%lx\n", req.op, req.gseg, req.val1);
  762. gts = gru_find_lock_gts(req.gseg);
  763. if (!gts) {
  764. gts = gru_alloc_locked_gts(req.gseg);
  765. if (IS_ERR(gts))
  766. return PTR_ERR(gts);
  767. }
  768. switch (req.op) {
  769. case sco_blade_chiplet:
  770. /* Select blade/chiplet for GRU context */
  771. if (req.val0 < -1 || req.val0 >= GRU_CHIPLETS_PER_HUB ||
  772. req.val1 < -1 || req.val1 >= GRU_MAX_BLADES ||
  773. (req.val1 >= 0 && !gru_base[req.val1])) {
  774. ret = -EINVAL;
  775. } else {
  776. gts->ts_user_blade_id = req.val1;
  777. gts->ts_user_chiplet_id = req.val0;
  778. gru_check_context_placement(gts);
  779. }
  780. break;
  781. case sco_gseg_owner:
  782. /* Register the current task as the GSEG owner */
  783. gts->ts_tgid_owner = current->tgid;
  784. break;
  785. case sco_cch_req_slice:
  786. /* Set the CCH slice option */
  787. gts->ts_cch_req_slice = req.val1 & 3;
  788. break;
  789. default:
  790. ret = -EINVAL;
  791. }
  792. gru_unlock_gts(gts);
  793. return ret;
  794. }