blob: d3cacd696b38df6764c8cc1e961b5cf3eb9626d1 [file] [log] [blame]
Jack Steiner14258642008-07-29 22:33:57 -07001/*
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
27#include <linux/kernel.h>
28#include <linux/errno.h>
29#include <linux/spinlock.h>
30#include <linux/mm.h>
31#include <linux/hugetlb.h>
32#include <linux/device.h>
33#include <linux/io.h>
34#include <linux/uaccess.h>
Jack Steinerbb04aa72009-04-02 16:59:07 -070035#include <linux/security.h>
Jack Steiner14258642008-07-29 22:33:57 -070036#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
43/*
44 * Test if a physical address is a valid GRU GSEG address
45 */
46static inline int is_gru_paddr(unsigned long paddr)
47{
48 return paddr >= gru_start_paddr && paddr < gru_end_paddr;
49}
50
51/*
52 * Find the vma of a GRU segment. Caller must hold mmap_sem.
53 */
54struct vm_area_struct *gru_find_vma(unsigned long vaddr)
55{
56 struct vm_area_struct *vma;
57
58 vma = find_vma(current->mm, vaddr);
59 if (vma && vma->vm_start <= vaddr && vma->vm_ops == &gru_vm_ops)
60 return vma;
61 return NULL;
62}
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
72static struct gru_thread_state *gru_find_lock_gts(unsigned long vaddr)
73{
74 struct mm_struct *mm = current->mm;
75 struct vm_area_struct *vma;
76 struct gru_thread_state *gts = NULL;
77
78 down_read(&mm->mmap_sem);
79 vma = gru_find_vma(vaddr);
80 if (vma)
81 gts = gru_find_thread_state(vma, TSID(vaddr, vma));
82 if (gts)
83 mutex_lock(&gts->ts_ctxlock);
84 else
85 up_read(&mm->mmap_sem);
86 return gts;
87}
88
89static struct gru_thread_state *gru_alloc_locked_gts(unsigned long vaddr)
90{
91 struct mm_struct *mm = current->mm;
92 struct vm_area_struct *vma;
Jack Steinere0060432009-12-15 16:48:10 -080093 struct gru_thread_state *gts = ERR_PTR(-EINVAL);
Jack Steiner14258642008-07-29 22:33:57 -070094
95 down_write(&mm->mmap_sem);
96 vma = gru_find_vma(vaddr);
Jack Steinere0060432009-12-15 16:48:10 -080097 if (!vma)
98 goto err;
Jack Steiner14258642008-07-29 22:33:57 -070099
Jack Steinere0060432009-12-15 16:48:10 -0800100 gts = gru_alloc_thread_state(vma, TSID(vaddr, vma));
101 if (IS_ERR(gts))
102 goto err;
103 mutex_lock(&gts->ts_ctxlock);
104 downgrade_write(&mm->mmap_sem);
105 return gts;
106
107err:
108 up_write(&mm->mmap_sem);
Jack Steiner14258642008-07-29 22:33:57 -0700109 return gts;
110}
111
112/*
113 * Unlock a GTS that was previously locked with gru_find_lock_gts().
114 */
115static void gru_unlock_gts(struct gru_thread_state *gts)
116{
117 mutex_unlock(&gts->ts_ctxlock);
118 up_read(&current->mm->mmap_sem);
119}
120
121/*
122 * Set a CB.istatus to active using a user virtual address. This must be done
123 * just prior to a TFH RESTART. The new cb.istatus is an in-cache status ONLY.
124 * If the line is evicted, the status may be lost. The in-cache update
125 * is necessary to prevent the user from seeing a stale cb.istatus that will
126 * change as soon as the TFH restart is complete. Races may cause an
127 * occasional failure to clear the cb.istatus, but that is ok.
Jack Steiner14258642008-07-29 22:33:57 -0700128 */
Jack Steinerb61fc692009-12-15 16:48:03 -0800129static void gru_cb_set_istatus_active(struct gru_instruction_bits *cbk)
Jack Steiner14258642008-07-29 22:33:57 -0700130{
Jack Steinerb61fc692009-12-15 16:48:03 -0800131 if (cbk) {
132 cbk->istatus = CBS_ACTIVE;
Jack Steiner14258642008-07-29 22:33:57 -0700133 }
134}
135
136/*
137 * Convert a interrupt IRQ to a pointer to the GRU GTS that caused the
138 * interrupt. Interrupts are always sent to a cpu on the blade that contains the
139 * GRU (except for headless blades which are not currently supported). A blade
140 * has N grus; a block of N consecutive IRQs is assigned to the GRUs. The IRQ
141 * number uniquely identifies the GRU chiplet on the local blade that caused the
142 * interrupt. Always called in interrupt context.
143 */
144static inline struct gru_state *irq_to_gru(int irq)
145{
146 return &gru_base[uv_numa_blade_id()]->bs_grus[irq - IRQ_GRU];
147}
148
149/*
150 * Read & clear a TFM
151 *
152 * The GRU has an array of fault maps. A map is private to a cpu
153 * Only one cpu will be accessing a cpu's fault map.
154 *
155 * This function scans the cpu-private fault map & clears all bits that
156 * are set. The function returns a bitmap that indicates the bits that
157 * were cleared. Note that sense the maps may be updated asynchronously by
158 * the GRU, atomic operations must be used to clear bits.
159 */
160static void get_clear_fault_map(struct gru_state *gru,
Jack Steiner4a7a17c2009-06-17 16:28:25 -0700161 struct gru_tlb_fault_map *imap,
162 struct gru_tlb_fault_map *dmap)
Jack Steiner14258642008-07-29 22:33:57 -0700163{
164 unsigned long i, k;
165 struct gru_tlb_fault_map *tfm;
166
167 tfm = get_tfm_for_cpu(gru, gru_cpu_fault_map_id());
168 prefetchw(tfm); /* Helps on hardware, required for emulator */
169 for (i = 0; i < BITS_TO_LONGS(GRU_NUM_CBE); i++) {
170 k = tfm->fault_bits[i];
171 if (k)
172 k = xchg(&tfm->fault_bits[i], 0UL);
Jack Steiner4a7a17c2009-06-17 16:28:25 -0700173 imap->fault_bits[i] = k;
174 k = tfm->done_bits[i];
175 if (k)
176 k = xchg(&tfm->done_bits[i], 0UL);
177 dmap->fault_bits[i] = k;
Jack Steiner14258642008-07-29 22:33:57 -0700178 }
179
180 /*
181 * Not functionally required but helps performance. (Required
182 * on emulator)
183 */
184 gru_flush_cache(tfm);
185}
186
187/*
188 * Atomic (interrupt context) & non-atomic (user context) functions to
189 * convert a vaddr into a physical address. The size of the page
190 * is returned in pageshift.
191 * returns:
192 * 0 - successful
193 * < 0 - error code
194 * 1 - (atomic only) try again in non-atomic context
195 */
196static int non_atomic_pte_lookup(struct vm_area_struct *vma,
197 unsigned long vaddr, int write,
198 unsigned long *paddr, int *pageshift)
199{
200 struct page *page;
201
202 /* ZZZ Need to handle HUGE pages */
203 if (is_vm_hugetlb_page(vma))
204 return -EFAULT;
205 *pageshift = PAGE_SHIFT;
206 if (get_user_pages
207 (current, current->mm, vaddr, 1, write, 0, &page, NULL) <= 0)
208 return -EFAULT;
209 *paddr = page_to_phys(page);
210 put_page(page);
211 return 0;
212}
213
214/*
Jack Steiner14258642008-07-29 22:33:57 -0700215 * atomic_pte_lookup
216 *
217 * Convert a user virtual address to a physical address
218 * Only supports Intel large pages (2MB only) on x86_64.
219 * ZZZ - hugepage support is incomplete
Jack Steiner923f7f62008-10-15 22:05:13 -0700220 *
221 * NOTE: mmap_sem is already held on entry to this function. This
222 * guarantees existence of the page tables.
Jack Steiner14258642008-07-29 22:33:57 -0700223 */
224static int atomic_pte_lookup(struct vm_area_struct *vma, unsigned long vaddr,
225 int write, unsigned long *paddr, int *pageshift)
226{
227 pgd_t *pgdp;
228 pmd_t *pmdp;
229 pud_t *pudp;
230 pte_t pte;
231
Jack Steiner14258642008-07-29 22:33:57 -0700232 pgdp = pgd_offset(vma->vm_mm, vaddr);
233 if (unlikely(pgd_none(*pgdp)))
234 goto err;
235
236 pudp = pud_offset(pgdp, vaddr);
237 if (unlikely(pud_none(*pudp)))
238 goto err;
239
240 pmdp = pmd_offset(pudp, vaddr);
241 if (unlikely(pmd_none(*pmdp)))
242 goto err;
243#ifdef CONFIG_X86_64
244 if (unlikely(pmd_large(*pmdp)))
245 pte = *(pte_t *) pmdp;
246 else
247#endif
248 pte = *pte_offset_kernel(pmdp, vaddr);
249
Jack Steiner14258642008-07-29 22:33:57 -0700250 if (unlikely(!pte_present(pte) ||
251 (write && (!pte_write(pte) || !pte_dirty(pte)))))
252 return 1;
253
254 *paddr = pte_pfn(pte) << PAGE_SHIFT;
Jack Steiner023a4072008-12-09 10:51:32 -0600255#ifdef CONFIG_HUGETLB_PAGE
Jack Steiner14258642008-07-29 22:33:57 -0700256 *pageshift = is_vm_hugetlb_page(vma) ? HPAGE_SHIFT : PAGE_SHIFT;
Jack Steiner023a4072008-12-09 10:51:32 -0600257#else
258 *pageshift = PAGE_SHIFT;
259#endif
Jack Steiner14258642008-07-29 22:33:57 -0700260 return 0;
261
262err:
263 local_irq_enable();
264 return 1;
265}
266
Jack Steinerecdaf2b2009-04-02 16:59:09 -0700267static int gru_vtop(struct gru_thread_state *gts, unsigned long vaddr,
268 int write, int atomic, unsigned long *gpa, int *pageshift)
269{
270 struct mm_struct *mm = gts->ts_mm;
271 struct vm_area_struct *vma;
272 unsigned long paddr;
273 int ret, ps;
274
275 vma = find_vma(mm, vaddr);
276 if (!vma)
277 goto inval;
278
279 /*
280 * Atomic lookup is faster & usually works even if called in non-atomic
281 * context.
282 */
283 rmb(); /* Must/check ms_range_active before loading PTEs */
284 ret = atomic_pte_lookup(vma, vaddr, write, &paddr, &ps);
285 if (ret) {
286 if (atomic)
287 goto upm;
288 if (non_atomic_pte_lookup(vma, vaddr, write, &paddr, &ps))
289 goto inval;
290 }
291 if (is_gru_paddr(paddr))
292 goto inval;
293 paddr = paddr & ~((1UL << ps) - 1);
294 *gpa = uv_soc_phys_ram_to_gpa(paddr);
295 *pageshift = ps;
296 return 0;
297
298inval:
299 return -1;
300upm:
301 return -2;
302}
303
304
Jack Steiner14258642008-07-29 22:33:57 -0700305/*
306 * Drop a TLB entry into the GRU. The fault is described by info in an TFH.
307 * Input:
308 * cb Address of user CBR. Null if not running in user context
309 * Return:
310 * 0 = dropin, exception, or switch to UPM successful
311 * 1 = range invalidate active
312 * < 0 = error code
313 *
314 */
315static int gru_try_dropin(struct gru_thread_state *gts,
316 struct gru_tlb_fault_handle *tfh,
Jack Steinerb61fc692009-12-15 16:48:03 -0800317 struct gru_instruction_bits *cbk)
Jack Steiner14258642008-07-29 22:33:57 -0700318{
Jack Steinerb61fc692009-12-15 16:48:03 -0800319 int pageshift = 0, asid, write, ret, atomic = !cbk;
Jack Steinerecdaf2b2009-04-02 16:59:09 -0700320 unsigned long gpa = 0, vaddr = 0;
Jack Steiner14258642008-07-29 22:33:57 -0700321
322 /*
323 * NOTE: The GRU contains magic hardware that eliminates races between
324 * TLB invalidates and TLB dropins. If an invalidate occurs
325 * in the window between reading the TFH and the subsequent TLB dropin,
326 * the dropin is ignored. This eliminates the need for additional locks.
327 */
328
329 /*
330 * Error if TFH state is IDLE or FMM mode & the user issuing a UPM call.
331 * Might be a hardware race OR a stupid user. Ignore FMM because FMM
332 * is a transient state.
333 */
Jack Steiner270952a2009-06-17 16:28:27 -0700334 if (tfh->status != TFHSTATUS_EXCEPTION) {
335 gru_flush_cache(tfh);
Jack Steiner67bf04a2009-12-15 16:48:11 -0800336 sync_core();
Jack Steiner270952a2009-06-17 16:28:27 -0700337 if (tfh->status != TFHSTATUS_EXCEPTION)
338 goto failnoexception;
339 STAT(tfh_stale_on_fault);
340 }
Jack Steiner14258642008-07-29 22:33:57 -0700341 if (tfh->state == TFHSTATE_IDLE)
342 goto failidle;
Jack Steinerb61fc692009-12-15 16:48:03 -0800343 if (tfh->state == TFHSTATE_MISS_FMM && cbk)
Jack Steiner14258642008-07-29 22:33:57 -0700344 goto failfmm;
345
346 write = (tfh->cause & TFHCAUSE_TLB_MOD) != 0;
347 vaddr = tfh->missvaddr;
348 asid = tfh->missasid;
349 if (asid == 0)
350 goto failnoasid;
351
352 rmb(); /* TFH must be cache resident before reading ms_range_active */
353
354 /*
355 * TFH is cache resident - at least briefly. Fail the dropin
356 * if a range invalidate is active.
357 */
358 if (atomic_read(&gts->ts_gms->ms_range_active))
359 goto failactive;
360
Jack Steinerecdaf2b2009-04-02 16:59:09 -0700361 ret = gru_vtop(gts, vaddr, write, atomic, &gpa, &pageshift);
362 if (ret == -1)
Jack Steiner14258642008-07-29 22:33:57 -0700363 goto failinval;
Jack Steinerecdaf2b2009-04-02 16:59:09 -0700364 if (ret == -2)
365 goto failupm;
Jack Steiner14258642008-07-29 22:33:57 -0700366
Jack Steiner7b8274e2009-04-02 16:59:12 -0700367 if (!(gts->ts_sizeavail & GRU_SIZEAVAIL(pageshift))) {
368 gts->ts_sizeavail |= GRU_SIZEAVAIL(pageshift);
Jack Steiner99f7c222009-12-15 16:48:06 -0800369 if (atomic || !gru_update_cch(gts)) {
Jack Steiner7b8274e2009-04-02 16:59:12 -0700370 gts->ts_force_cch_reload = 1;
371 goto failupm;
372 }
373 }
Jack Steinerb61fc692009-12-15 16:48:03 -0800374 gru_cb_set_istatus_active(cbk);
Jack Steiner14258642008-07-29 22:33:57 -0700375 tfh_write_restart(tfh, gpa, GAA_RAM, vaddr, asid, write,
376 GRU_PAGESIZE(pageshift));
377 STAT(tlb_dropin);
378 gru_dbg(grudev,
379 "%s: tfh 0x%p, vaddr 0x%lx, asid 0x%x, ps %d, gpa 0x%lx\n",
380 ret ? "non-atomic" : "atomic", tfh, vaddr, asid,
381 pageshift, gpa);
382 return 0;
383
384failnoasid:
385 /* No asid (delayed unload). */
386 STAT(tlb_dropin_fail_no_asid);
387 gru_dbg(grudev, "FAILED no_asid tfh: 0x%p, vaddr 0x%lx\n", tfh, vaddr);
Jack Steinerb61fc692009-12-15 16:48:03 -0800388 if (!cbk)
Jack Steiner14258642008-07-29 22:33:57 -0700389 tfh_user_polling_mode(tfh);
390 else
391 gru_flush_cache(tfh);
392 return -EAGAIN;
393
394failupm:
395 /* Atomic failure switch CBR to UPM */
396 tfh_user_polling_mode(tfh);
397 STAT(tlb_dropin_fail_upm);
398 gru_dbg(grudev, "FAILED upm tfh: 0x%p, vaddr 0x%lx\n", tfh, vaddr);
399 return 1;
400
401failfmm:
402 /* FMM state on UPM call */
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700403 gru_flush_cache(tfh);
Jack Steiner14258642008-07-29 22:33:57 -0700404 STAT(tlb_dropin_fail_fmm);
405 gru_dbg(grudev, "FAILED fmm tfh: 0x%p, state %d\n", tfh, tfh->state);
406 return 0;
407
Jack Steinercd1334f2009-06-17 16:28:19 -0700408failnoexception:
409 /* TFH status did not show exception pending */
410 gru_flush_cache(tfh);
Jack Steinerb61fc692009-12-15 16:48:03 -0800411 if (cbk)
412 gru_flush_cache(cbk);
Jack Steinercd1334f2009-06-17 16:28:19 -0700413 STAT(tlb_dropin_fail_no_exception);
Jack Steinerb61fc692009-12-15 16:48:03 -0800414 gru_dbg(grudev, "FAILED non-exception tfh: 0x%p, status %d, state %d\n",
415 tfh, tfh->status, tfh->state);
Jack Steinercd1334f2009-06-17 16:28:19 -0700416 return 0;
417
Jack Steiner14258642008-07-29 22:33:57 -0700418failidle:
Jack Steinercd1334f2009-06-17 16:28:19 -0700419 /* TFH state was idle - no miss pending */
Jack Steiner14258642008-07-29 22:33:57 -0700420 gru_flush_cache(tfh);
Jack Steinerb61fc692009-12-15 16:48:03 -0800421 if (cbk)
422 gru_flush_cache(cbk);
Jack Steiner14258642008-07-29 22:33:57 -0700423 STAT(tlb_dropin_fail_idle);
424 gru_dbg(grudev, "FAILED idle tfh: 0x%p, state %d\n", tfh, tfh->state);
425 return 0;
426
427failinval:
428 /* All errors (atomic & non-atomic) switch CBR to EXCEPTION state */
429 tfh_exception(tfh);
430 STAT(tlb_dropin_fail_invalid);
431 gru_dbg(grudev, "FAILED inval tfh: 0x%p, vaddr 0x%lx\n", tfh, vaddr);
432 return -EFAULT;
433
434failactive:
435 /* Range invalidate active. Switch to UPM iff atomic */
Jack Steinerb61fc692009-12-15 16:48:03 -0800436 if (!cbk)
Jack Steiner14258642008-07-29 22:33:57 -0700437 tfh_user_polling_mode(tfh);
438 else
439 gru_flush_cache(tfh);
440 STAT(tlb_dropin_fail_range_active);
441 gru_dbg(grudev, "FAILED range active: tfh 0x%p, vaddr 0x%lx\n",
442 tfh, vaddr);
443 return 1;
444}
445
446/*
447 * Process an external interrupt from the GRU. This interrupt is
448 * caused by a TLB miss.
449 * Note that this is the interrupt handler that is registered with linux
450 * interrupt handlers.
451 */
452irqreturn_t gru_intr(int irq, void *dev_id)
453{
454 struct gru_state *gru;
Jack Steiner4a7a17c2009-06-17 16:28:25 -0700455 struct gru_tlb_fault_map imap, dmap;
Jack Steiner14258642008-07-29 22:33:57 -0700456 struct gru_thread_state *gts;
457 struct gru_tlb_fault_handle *tfh = NULL;
458 int cbrnum, ctxnum;
459
460 STAT(intr);
461
462 gru = irq_to_gru(irq);
463 if (!gru) {
464 dev_err(grudev, "GRU: invalid interrupt: cpu %d, irq %d\n",
465 raw_smp_processor_id(), irq);
466 return IRQ_NONE;
467 }
Jack Steiner4a7a17c2009-06-17 16:28:25 -0700468 get_clear_fault_map(gru, &imap, &dmap);
Jack Steiner14258642008-07-29 22:33:57 -0700469
Jack Steiner4a7a17c2009-06-17 16:28:25 -0700470 for_each_cbr_in_tfm(cbrnum, dmap.fault_bits) {
471 complete(gru->gs_blade->bs_async_wq);
472 gru_dbg(grudev, "gid %d, cbr_done %d, done %d\n",
473 gru->gs_gid, cbrnum, gru->gs_blade->bs_async_wq->done);
474 }
475
476 for_each_cbr_in_tfm(cbrnum, imap.fault_bits) {
Jack Steiner14258642008-07-29 22:33:57 -0700477 tfh = get_tfh_by_index(gru, cbrnum);
478 prefetchw(tfh); /* Helps on hdw, required for emulator */
479
480 /*
481 * When hardware sets a bit in the faultmap, it implicitly
482 * locks the GRU context so that it cannot be unloaded.
483 * The gts cannot change until a TFH start/writestart command
484 * is issued.
485 */
486 ctxnum = tfh->ctxnum;
487 gts = gru->gs_gts[ctxnum];
488
489 /*
490 * This is running in interrupt context. Trylock the mmap_sem.
491 * If it fails, retry the fault in user context.
492 */
Jack Steinercd1334f2009-06-17 16:28:19 -0700493 if (!gts->ts_force_cch_reload &&
494 down_read_trylock(&gts->ts_mm->mmap_sem)) {
Jack Steiner7e796a72009-06-17 16:28:30 -0700495 gts->ustats.fmm_tlbdropin++;
Jack Steiner14258642008-07-29 22:33:57 -0700496 gru_try_dropin(gts, tfh, NULL);
497 up_read(&gts->ts_mm->mmap_sem);
498 } else {
499 tfh_user_polling_mode(tfh);
Jack Steiner43884602009-04-02 16:59:05 -0700500 STAT(intr_mm_lock_failed);
Jack Steiner14258642008-07-29 22:33:57 -0700501 }
502 }
503 return IRQ_HANDLED;
504}
505
506
507static int gru_user_dropin(struct gru_thread_state *gts,
508 struct gru_tlb_fault_handle *tfh,
Jack Steinerb61fc692009-12-15 16:48:03 -0800509 void *cb)
Jack Steiner14258642008-07-29 22:33:57 -0700510{
511 struct gru_mm_struct *gms = gts->ts_gms;
512 int ret;
513
Jack Steiner7e796a72009-06-17 16:28:30 -0700514 gts->ustats.upm_tlbdropin++;
Jack Steiner14258642008-07-29 22:33:57 -0700515 while (1) {
516 wait_event(gms->ms_wait_queue,
517 atomic_read(&gms->ms_range_active) == 0);
518 prefetchw(tfh); /* Helps on hdw, required for emulator */
519 ret = gru_try_dropin(gts, tfh, cb);
520 if (ret <= 0)
521 return ret;
522 STAT(call_os_wait_queue);
523 }
524}
525
526/*
527 * This interface is called as a result of a user detecting a "call OS" bit
528 * in a user CB. Normally means that a TLB fault has occurred.
529 * cb - user virtual address of the CB
530 */
531int gru_handle_user_call_os(unsigned long cb)
532{
533 struct gru_tlb_fault_handle *tfh;
534 struct gru_thread_state *gts;
Jack Steinerb61fc692009-12-15 16:48:03 -0800535 void *cbk;
Jack Steiner14258642008-07-29 22:33:57 -0700536 int ucbnum, cbrnum, ret = -EINVAL;
537
538 STAT(call_os);
539 gru_dbg(grudev, "address 0x%lx\n", cb);
540
541 /* sanity check the cb pointer */
542 ucbnum = get_cb_number((void *)cb);
543 if ((cb & (GRU_HANDLE_STRIDE - 1)) || ucbnum >= GRU_NUM_CB)
544 return -EINVAL;
Jack Steiner14258642008-07-29 22:33:57 -0700545
546 gts = gru_find_lock_gts(cb);
547 if (!gts)
548 return -EINVAL;
549
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700550 if (ucbnum >= gts->ts_cbr_au_count * GRU_CBR_AU_SIZE)
Jack Steiner14258642008-07-29 22:33:57 -0700551 goto exit;
Jack Steiner14258642008-07-29 22:33:57 -0700552
Jack Steiner55484c42009-12-15 16:48:05 -0800553 gru_check_context_placement(gts);
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700554
Jack Steiner7b8274e2009-04-02 16:59:12 -0700555 /*
556 * CCH may contain stale data if ts_force_cch_reload is set.
557 */
558 if (gts->ts_gru && gts->ts_force_cch_reload) {
Jack Steiner7b8274e2009-04-02 16:59:12 -0700559 gts->ts_force_cch_reload = 0;
Jack Steiner99f7c222009-12-15 16:48:06 -0800560 gru_update_cch(gts);
Jack Steiner7b8274e2009-04-02 16:59:12 -0700561 }
562
Jack Steiner14258642008-07-29 22:33:57 -0700563 ret = -EAGAIN;
564 cbrnum = thread_cbr_number(gts, ucbnum);
Jack Steiner99f7c222009-12-15 16:48:06 -0800565 if (gts->ts_gru) {
Jack Steiner14258642008-07-29 22:33:57 -0700566 tfh = get_tfh_by_index(gts->ts_gru, cbrnum);
Jack Steinerb61fc692009-12-15 16:48:03 -0800567 cbk = get_gseg_base_address_cb(gts->ts_gru->gs_gru_base_vaddr,
568 gts->ts_ctxnum, ucbnum);
569 ret = gru_user_dropin(gts, tfh, cbk);
Jack Steiner14258642008-07-29 22:33:57 -0700570 }
571exit:
572 gru_unlock_gts(gts);
573 return ret;
574}
575
576/*
577 * Fetch the exception detail information for a CB that terminated with
578 * an exception.
579 */
580int gru_get_exception_detail(unsigned long arg)
581{
582 struct control_block_extended_exc_detail excdet;
583 struct gru_control_block_extended *cbe;
584 struct gru_thread_state *gts;
585 int ucbnum, cbrnum, ret;
586
587 STAT(user_exception);
588 if (copy_from_user(&excdet, (void __user *)arg, sizeof(excdet)))
589 return -EFAULT;
590
591 gru_dbg(grudev, "address 0x%lx\n", excdet.cb);
592 gts = gru_find_lock_gts(excdet.cb);
593 if (!gts)
594 return -EINVAL;
595
Jack Steinerfe5bb6b2009-04-02 16:59:04 -0700596 ucbnum = get_cb_number((void *)excdet.cb);
597 if (ucbnum >= gts->ts_cbr_au_count * GRU_CBR_AU_SIZE) {
598 ret = -EINVAL;
599 } else if (gts->ts_gru) {
Jack Steiner14258642008-07-29 22:33:57 -0700600 cbrnum = thread_cbr_number(gts, ucbnum);
601 cbe = get_cbe_by_index(gts->ts_gru, cbrnum);
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700602 gru_flush_cache(cbe); /* CBE not coherent */
Jack Steiner67bf04a2009-12-15 16:48:11 -0800603 sync_core(); /* make sure we are have current data */
Jack Steiner14258642008-07-29 22:33:57 -0700604 excdet.opc = cbe->opccpy;
605 excdet.exopc = cbe->exopccpy;
606 excdet.ecause = cbe->ecause;
607 excdet.exceptdet0 = cbe->idef1upd;
608 excdet.exceptdet1 = cbe->idef3upd;
Jack Steinercd1334f2009-06-17 16:28:19 -0700609 excdet.cbrstate = cbe->cbrstate;
610 excdet.cbrexecstatus = cbe->cbrexecstatus;
Jack Steiner1a2c09e2009-06-17 16:28:28 -0700611 gru_flush_cache(cbe);
Jack Steiner14258642008-07-29 22:33:57 -0700612 ret = 0;
613 } else {
614 ret = -EAGAIN;
615 }
616 gru_unlock_gts(gts);
617
Jack Steinercd1334f2009-06-17 16:28:19 -0700618 gru_dbg(grudev,
619 "cb 0x%lx, op %d, exopc %d, cbrstate %d, cbrexecstatus 0x%x, ecause 0x%x, "
620 "exdet0 0x%lx, exdet1 0x%x\n",
621 excdet.cb, excdet.opc, excdet.exopc, excdet.cbrstate, excdet.cbrexecstatus,
622 excdet.ecause, excdet.exceptdet0, excdet.exceptdet1);
Jack Steiner14258642008-07-29 22:33:57 -0700623 if (!ret && copy_to_user((void __user *)arg, &excdet, sizeof(excdet)))
624 ret = -EFAULT;
625 return ret;
626}
627
628/*
629 * User request to unload a context. Content is saved for possible reload.
630 */
Jack Steinerbb04aa72009-04-02 16:59:07 -0700631static int gru_unload_all_contexts(void)
632{
633 struct gru_thread_state *gts;
634 struct gru_state *gru;
Jack Steinere1c32192009-04-02 16:59:10 -0700635 int gid, ctxnum;
Jack Steinerbb04aa72009-04-02 16:59:07 -0700636
637 if (!capable(CAP_SYS_ADMIN))
638 return -EPERM;
Jack Steinere1c32192009-04-02 16:59:10 -0700639 foreach_gid(gid) {
Jack Steinerbb04aa72009-04-02 16:59:07 -0700640 gru = GID_TO_GRU(gid);
641 spin_lock(&gru->gs_lock);
642 for (ctxnum = 0; ctxnum < GRU_NUM_CCH; ctxnum++) {
643 gts = gru->gs_gts[ctxnum];
644 if (gts && mutex_trylock(&gts->ts_ctxlock)) {
645 spin_unlock(&gru->gs_lock);
646 gru_unload_context(gts, 1);
Jack Steinerd57c82b2009-06-17 16:28:20 -0700647 mutex_unlock(&gts->ts_ctxlock);
Jack Steinerbb04aa72009-04-02 16:59:07 -0700648 spin_lock(&gru->gs_lock);
649 }
650 }
651 spin_unlock(&gru->gs_lock);
652 }
653 return 0;
654}
655
Jack Steiner14258642008-07-29 22:33:57 -0700656int gru_user_unload_context(unsigned long arg)
657{
658 struct gru_thread_state *gts;
659 struct gru_unload_context_req req;
660
661 STAT(user_unload_context);
662 if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
663 return -EFAULT;
664
665 gru_dbg(grudev, "gseg 0x%lx\n", req.gseg);
666
Jack Steinerbb04aa72009-04-02 16:59:07 -0700667 if (!req.gseg)
668 return gru_unload_all_contexts();
669
Jack Steiner14258642008-07-29 22:33:57 -0700670 gts = gru_find_lock_gts(req.gseg);
671 if (!gts)
672 return -EINVAL;
673
674 if (gts->ts_gru)
675 gru_unload_context(gts, 1);
676 gru_unlock_gts(gts);
677
678 return 0;
679}
680
681/*
682 * User request to flush a range of virtual addresses from the GRU TLB
683 * (Mainly for testing).
684 */
685int gru_user_flush_tlb(unsigned long arg)
686{
687 struct gru_thread_state *gts;
688 struct gru_flush_tlb_req req;
Jack Steiner1926ee82009-06-17 16:28:33 -0700689 struct gru_mm_struct *gms;
Jack Steiner14258642008-07-29 22:33:57 -0700690
691 STAT(user_flush_tlb);
692 if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
693 return -EFAULT;
694
695 gru_dbg(grudev, "gseg 0x%lx, vaddr 0x%lx, len 0x%lx\n", req.gseg,
696 req.vaddr, req.len);
697
698 gts = gru_find_lock_gts(req.gseg);
699 if (!gts)
700 return -EINVAL;
701
Jack Steiner1926ee82009-06-17 16:28:33 -0700702 gms = gts->ts_gms;
Jack Steiner14258642008-07-29 22:33:57 -0700703 gru_unlock_gts(gts);
Jack Steiner1926ee82009-06-17 16:28:33 -0700704 gru_flush_tlb_range(gms, req.vaddr, req.len);
Jack Steiner14258642008-07-29 22:33:57 -0700705
706 return 0;
707}
708
709/*
Jack Steiner7e796a72009-06-17 16:28:30 -0700710 * Fetch GSEG statisticss
711 */
712long gru_get_gseg_statistics(unsigned long arg)
713{
714 struct gru_thread_state *gts;
715 struct gru_get_gseg_statistics_req req;
716
717 if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
718 return -EFAULT;
719
Jack Steiner091f1a12009-12-15 16:48:02 -0800720 /*
721 * The library creates arrays of contexts for threaded programs.
722 * If no gts exists in the array, the context has never been used & all
723 * statistics are implicitly 0.
724 */
Jack Steiner7e796a72009-06-17 16:28:30 -0700725 gts = gru_find_lock_gts(req.gseg);
726 if (gts) {
727 memcpy(&req.stats, &gts->ustats, sizeof(gts->ustats));
728 gru_unlock_gts(gts);
729 } else {
730 memset(&req.stats, 0, sizeof(gts->ustats));
731 }
732
733 if (copy_to_user((void __user *)arg, &req, sizeof(req)))
734 return -EFAULT;
735
736 return 0;
737}
738
739/*
Jack Steiner14258642008-07-29 22:33:57 -0700740 * Register the current task as the user of the GSEG slice.
741 * Needed for TLB fault interrupt targeting.
742 */
Jack Steiner92b39382009-06-17 16:28:32 -0700743int gru_set_context_option(unsigned long arg)
Jack Steiner14258642008-07-29 22:33:57 -0700744{
745 struct gru_thread_state *gts;
Jack Steiner92b39382009-06-17 16:28:32 -0700746 struct gru_set_context_option_req req;
747 int ret = 0;
Jack Steiner14258642008-07-29 22:33:57 -0700748
Jack Steiner92b39382009-06-17 16:28:32 -0700749 STAT(set_context_option);
750 if (copy_from_user(&req, (void __user *)arg, sizeof(req)))
751 return -EFAULT;
752 gru_dbg(grudev, "op %d, gseg 0x%lx, value1 0x%lx\n", req.op, req.gseg, req.val1);
753
754 gts = gru_alloc_locked_gts(req.gseg);
Jack Steiner7f2251b2009-12-15 16:48:08 -0800755 if (IS_ERR(gts))
756 return PTR_ERR(gts);
Jack Steiner14258642008-07-29 22:33:57 -0700757
Jack Steiner92b39382009-06-17 16:28:32 -0700758 switch (req.op) {
Jack Steiner518e5cd2009-12-15 16:48:04 -0800759 case sco_blade_chiplet:
760 /* Select blade/chiplet for GRU context */
761 if (req.val1 < -1 || req.val1 >= GRU_MAX_BLADES || !gru_base[req.val1] ||
762 req.val0 < -1 || req.val0 >= GRU_CHIPLETS_PER_HUB) {
763 ret = -EINVAL;
764 } else {
765 gts->ts_user_blade_id = req.val1;
766 gts->ts_user_chiplet_id = req.val0;
Jack Steiner55484c42009-12-15 16:48:05 -0800767 gru_check_context_placement(gts);
Jack Steiner518e5cd2009-12-15 16:48:04 -0800768 }
769 break;
Jack Steiner92b39382009-06-17 16:28:32 -0700770 case sco_gseg_owner:
771 /* Register the current task as the GSEG owner */
772 gts->ts_tgid_owner = current->tgid;
773 break;
Jack Steinerb1b19fc2009-06-17 16:28:33 -0700774 case sco_cch_req_slice:
775 /* Set the CCH slice option */
776 gts->ts_cch_req_slice = req.val1 & 3;
777 break;
Jack Steiner92b39382009-06-17 16:28:32 -0700778 default:
779 ret = -EINVAL;
780 }
Jack Steiner14258642008-07-29 22:33:57 -0700781 gru_unlock_gts(gts);
782
Jack Steiner92b39382009-06-17 16:28:32 -0700783 return ret;
Jack Steiner14258642008-07-29 22:33:57 -0700784}