grufault.c 20 KB

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