// SPDX-License-Identifier: GPL-2.0-or-later /* * SN Platform GRU Driver * * FAULT HANDLER FOR GRU DETECTED TLB MISSES * * This file contains code that handles TLB misses within the GRU. * These misses are reported either via interrupts or user polling of * the user CB. * * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
*/
/* * Test if a physical address is a valid GRU GSEG address
*/ staticinlineint is_gru_paddr(unsignedlong paddr)
{ return paddr >= gru_start_paddr && paddr < gru_end_paddr;
}
/* * Find the vma of a GRU segment. Caller must hold mmap_lock.
*/ struct vm_area_struct *gru_find_vma(unsignedlong vaddr)
{ struct vm_area_struct *vma;
/* * Find and lock the gts that contains the specified user vaddr. * * Returns: * - *gts with the mmap_lock locked for read and the GTS locked. * - NULL if vaddr invalid OR is not a valid GSEG vaddr.
*/
/* * Unlock a GTS that was previously locked with gru_find_lock_gts().
*/ staticvoid gru_unlock_gts(struct gru_thread_state *gts)
{
mutex_unlock(>s->ts_ctxlock);
mmap_read_unlock(current->mm);
}
/* * Set a CB.istatus to active using a user virtual address. This must be done * just prior to a TFH RESTART. The new cb.istatus is an in-cache status ONLY. * If the line is evicted, the status may be lost. The in-cache update * is necessary to prevent the user from seeing a stale cb.istatus that will * change as soon as the TFH restart is complete. Races may cause an * occasional failure to clear the cb.istatus, but that is ok.
*/ staticvoid gru_cb_set_istatus_active(struct gru_instruction_bits *cbk)
{ if (cbk) {
cbk->istatus = CBS_ACTIVE;
}
}
/* * Read & clear a TFM * * The GRU has an array of fault maps. A map is private to a cpu * Only one cpu will be accessing a cpu's fault map. * * This function scans the cpu-private fault map & clears all bits that * are set. The function returns a bitmap that indicates the bits that * were cleared. Note that sense the maps may be updated asynchronously by * the GRU, atomic operations must be used to clear bits.
*/ staticvoid get_clear_fault_map(struct gru_state *gru, struct gru_tlb_fault_map *imap, struct gru_tlb_fault_map *dmap)
{ unsignedlong i, k; struct gru_tlb_fault_map *tfm;
tfm = get_tfm_for_cpu(gru, gru_cpu_fault_map_id());
prefetchw(tfm); /* Helps on hardware, required for emulator */ for (i = 0; i < BITS_TO_LONGS(GRU_NUM_CBE); i++) {
k = tfm->fault_bits[i]; if (k)
k = xchg(&tfm->fault_bits[i], 0UL);
imap->fault_bits[i] = k;
k = tfm->done_bits[i]; if (k)
k = xchg(&tfm->done_bits[i], 0UL);
dmap->fault_bits[i] = k;
}
/* * Not functionally required but helps performance. (Required * on emulator)
*/
gru_flush_cache(tfm);
}
/* * Atomic (interrupt context) & non-atomic (user context) functions to * convert a vaddr into a physical address. The size of the page * is returned in pageshift. * returns: * 0 - successful * < 0 - error code * 1 - (atomic only) try again in non-atomic context
*/ staticint non_atomic_pte_lookup(struct vm_area_struct *vma, unsignedlong vaddr, int write, unsignedlong *paddr, int *pageshift)
{ struct page *page;
/* * atomic_pte_lookup * * Convert a user virtual address to a physical address * Only supports Intel large pages (2MB only) on x86_64. * ZZZ - hugepage support is incomplete * * NOTE: mmap_lock is already held on entry to this function. This * guarantees existence of the page tables.
*/ staticint atomic_pte_lookup(struct vm_area_struct *vma, unsignedlong vaddr, int write, unsignedlong *paddr, int *pageshift)
{
pgd_t *pgdp;
p4d_t *p4dp;
pud_t *pudp;
pmd_t *pmdp;
pte_t pte;
pgdp = pgd_offset(vma->vm_mm, vaddr); if (unlikely(pgd_none(*pgdp))) goto err;
p4dp = p4d_offset(pgdp, vaddr); if (unlikely(p4d_none(*p4dp))) goto err;
pudp = pud_offset(p4dp, vaddr); if (unlikely(pud_none(*pudp))) goto err;
/* * Flush a CBE from cache. The CBE is clean in the cache. Dirty the * CBE cacheline so that the line will be written back to home agent. * Otherwise the line may be silently dropped. This has no impact * except on performance.
*/ staticvoid gru_flush_cache_cbe(struct gru_control_block_extended *cbe)
{ if (unlikely(cbe)) {
cbe->cbrexecstatus = 0; /* make CL dirty */
gru_flush_cache(cbe);
}
}
/* * Preload the TLB with entries that may be required. Currently, preloading * is implemented only for BCOPY. Preload <tlb_preload_count> pages OR to * the end of the bcopy tranfer, whichever is smaller.
*/ staticvoid gru_preload_tlb(struct gru_state *gru, struct gru_thread_state *gts, int atomic, unsignedlong fault_vaddr, int asid, int write, unsignedchar tlb_preload_count, struct gru_tlb_fault_handle *tfh, struct gru_control_block_extended *cbe)
{ unsignedlong vaddr = 0, gpa; int ret, pageshift;
/* * Drop a TLB entry into the GRU. The fault is described by info in an TFH. * Input: * cb Address of user CBR. Null if not running in user context * Return: * 0 = dropin, exception, or switch to UPM successful * 1 = range invalidate active * < 0 = error code *
*/ staticint gru_try_dropin(struct gru_state *gru, struct gru_thread_state *gts, struct gru_tlb_fault_handle *tfh, struct gru_instruction_bits *cbk)
{ struct gru_control_block_extended *cbe = NULL; unsignedchar tlb_preload_count = gts->ts_tlb_preload_count; int pageshift = 0, asid, write, ret, atomic = !cbk, indexway; unsignedlong gpa = 0, vaddr = 0;
/* * NOTE: The GRU contains magic hardware that eliminates races between * TLB invalidates and TLB dropins. If an invalidate occurs * in the window between reading the TFH and the subsequent TLB dropin, * the dropin is ignored. This eliminates the need for additional locks.
*/
/* * Prefetch the CBE if doing TLB preloading
*/ if (unlikely(tlb_preload_count)) {
cbe = gru_tfh_to_cbe(tfh);
prefetchw(cbe);
}
/* * Error if TFH state is IDLE or FMM mode & the user issuing a UPM call. * Might be a hardware race OR a stupid user. Ignore FMM because FMM * is a transient state.
*/ if (tfh->status != TFHSTATUS_EXCEPTION) {
gru_flush_cache(tfh);
sync_core(); if (tfh->status != TFHSTATUS_EXCEPTION) goto failnoexception;
STAT(tfh_stale_on_fault);
} if (tfh->state == TFHSTATE_IDLE) goto failidle; if (tfh->state == TFHSTATE_MISS_FMM && cbk) goto failfmm;
rmb(); /* TFH must be cache resident before reading ms_range_active */
/* * TFH is cache resident - at least briefly. Fail the dropin * if a range invalidate is active.
*/ if (atomic_read(>s->ts_gms->ms_range_active)) goto failactive;
ret = gru_vtop(gts, vaddr, write, atomic, &gpa, &pageshift); if (ret == VTOP_INVALID) goto failinval; if (ret == VTOP_RETRY) goto failupm;
if (!(gts->ts_sizeavail & GRU_SIZEAVAIL(pageshift))) {
gts->ts_sizeavail |= GRU_SIZEAVAIL(pageshift); if (atomic || !gru_update_cch(gts)) {
gts->ts_force_cch_reload = 1; goto failupm;
}
}
failfmm: /* FMM state on UPM call */
gru_flush_cache(tfh);
gru_flush_cache_cbe(cbe);
STAT(tlb_dropin_fail_fmm);
gru_dbg(grudev, "FAILED fmm tfh: 0x%p, state %d\n", tfh, tfh->state); return 0;
failnoexception: /* TFH status did not show exception pending */
gru_flush_cache(tfh);
gru_flush_cache_cbe(cbe); if (cbk)
gru_flush_cache(cbk);
STAT(tlb_dropin_fail_no_exception);
gru_dbg(grudev, "FAILED non-exception tfh: 0x%p, status %d, state %d\n",
tfh, tfh->status, tfh->state); return 0;
failidle: /* TFH state was idle - no miss pending */
gru_flush_cache(tfh);
gru_flush_cache_cbe(cbe); if (cbk)
gru_flush_cache(cbk);
STAT(tlb_dropin_fail_idle);
gru_dbg(grudev, "FAILED idle tfh: 0x%p, state %d\n", tfh, tfh->state); return 0;
failinval: /* All errors (atomic & non-atomic) switch CBR to EXCEPTION state */
tfh_exception(tfh);
gru_flush_cache_cbe(cbe);
STAT(tlb_dropin_fail_invalid);
gru_dbg(grudev, "FAILED inval tfh: 0x%p, vaddr 0x%lx\n", tfh, vaddr); return -EFAULT;
failactive: /* Range invalidate active. Switch to UPM iff atomic */ if (!cbk)
tfh_user_polling_mode(tfh); else
gru_flush_cache(tfh);
gru_flush_cache_cbe(cbe);
STAT(tlb_dropin_fail_range_active);
gru_dbg(grudev, "FAILED range active: tfh 0x%p, vaddr 0x%lx\n",
tfh, vaddr); return 1;
}
/* * Process an external interrupt from the GRU. This interrupt is * caused by a TLB miss. * Note that this is the interrupt handler that is registered with linux * interrupt handlers.
*/ static irqreturn_t gru_intr(int chiplet, int blade)
{ struct gru_state *gru; struct gru_tlb_fault_map imap, dmap; struct gru_thread_state *gts; struct gru_tlb_fault_handle *tfh = NULL; struct completion *cmp; int cbrnum, ctxnum;
for_each_cbr_in_tfm(cbrnum, imap.fault_bits) {
STAT(intr_tfh);
tfh = get_tfh_by_index(gru, cbrnum);
prefetchw(tfh); /* Helps on hdw, required for emulator */
/* * When hardware sets a bit in the faultmap, it implicitly * locks the GRU context so that it cannot be unloaded. * The gts cannot change until a TFH start/writestart command * is issued.
*/
ctxnum = tfh->ctxnum;
gts = gru->gs_gts[ctxnum];
/* Spurious interrupts can cause this. Ignore. */ if (!gts) {
STAT(intr_spurious); continue;
}
/* * This is running in interrupt context. Trylock the mmap_lock. * If it fails, retry the fault in user context.
*/
gts->ustats.fmm_tlbmiss++; if (!gts->ts_force_cch_reload &&
mmap_read_trylock(gts->ts_mm)) {
gru_try_dropin(gru, gts, tfh, NULL);
mmap_read_unlock(gts->ts_mm);
} else {
tfh_user_polling_mode(tfh);
STAT(intr_mm_lock_failed);
}
} return IRQ_HANDLED;
}
gts->ustats.upm_tlbmiss++; while (1) {
wait_event(gms->ms_wait_queue,
atomic_read(&gms->ms_range_active) == 0);
prefetchw(tfh); /* Helps on hdw, required for emulator */
ret = gru_try_dropin(gts->ts_gru, gts, tfh, cb); if (ret <= 0) return ret;
STAT(call_os_wait_queue);
}
}
/* * This interface is called as a result of a user detecting a "call OS" bit * in a user CB. Normally means that a TLB fault has occurred. * cb - user virtual address of the CB
*/ int gru_handle_user_call_os(unsignedlong cb)
{ struct gru_tlb_fault_handle *tfh; struct gru_thread_state *gts; void *cbk; int ucbnum, cbrnum, ret = -EINVAL;
if (ucbnum >= gts->ts_cbr_au_count * GRU_CBR_AU_SIZE) gotoexit;
if (gru_check_context_placement(gts)) {
gru_unlock_gts(gts);
gru_unload_context(gts, 1); goto again;
}
/* * CCH may contain stale data if ts_force_cch_reload is set.
*/ if (gts->ts_gru && gts->ts_force_cch_reload) {
gts->ts_force_cch_reload = 0;
gru_update_cch(gts);
}
ret = -EAGAIN;
cbrnum = thread_cbr_number(gts, ucbnum); if (gts->ts_gru) {
tfh = get_tfh_by_index(gts->ts_gru, cbrnum);
cbk = get_gseg_base_address_cb(gts->ts_gru->gs_gru_base_vaddr,
gts->ts_ctxnum, ucbnum);
ret = gru_user_dropin(gts, tfh, cbk);
} exit:
gru_unlock_gts(gts); return ret;
}
/* * Fetch the exception detail information for a CB that terminated with * an exception.
*/ int gru_get_exception_detail(unsignedlong arg)
{ struct control_block_extended_exc_detail excdet; struct gru_control_block_extended *cbe; struct gru_thread_state *gts; int ucbnum, cbrnum, ret;
STAT(user_exception); if (copy_from_user(&excdet, (void __user *)arg, sizeof(excdet))) return -EFAULT;
gts = gru_find_lock_gts(excdet.cb); if (!gts) return -EINVAL;
gru_dbg(grudev, "address 0x%lx, gid %d, gts 0x%p\n", excdet.cb, gts->ts_gru ? gts->ts_gru->gs_gid : -1, gts);
ucbnum = get_cb_number((void *)excdet.cb); if (ucbnum >= gts->ts_cbr_au_count * GRU_CBR_AU_SIZE) {
ret = -EINVAL;
} elseif (gts->ts_gru) {
cbrnum = thread_cbr_number(gts, ucbnum);
cbe = get_cbe_by_index(gts->ts_gru, cbrnum);
gru_flush_cache(cbe); /* CBE not coherent */
sync_core(); /* make sure we are have current data */
excdet.opc = cbe->opccpy;
excdet.exopc = cbe->exopccpy;
excdet.ecause = cbe->ecause;
excdet.exceptdet0 = cbe->idef1upd;
excdet.exceptdet1 = cbe->idef3upd;
excdet.cbrstate = cbe->cbrstate;
excdet.cbrexecstatus = cbe->cbrexecstatus;
gru_flush_cache_cbe(cbe);
ret = 0;
} else {
ret = -EAGAIN;
}
gru_unlock_gts(gts);
/* * User request to unload a context. Content is saved for possible reload.
*/ staticint gru_unload_all_contexts(void)
{ struct gru_thread_state *gts; struct gru_state *gru; int gid, ctxnum;
int gru_user_unload_context(unsignedlong arg)
{ struct gru_thread_state *gts; struct gru_unload_context_req req;
STAT(user_unload_context); if (copy_from_user(&req, (void __user *)arg, sizeof(req))) return -EFAULT;
gru_dbg(grudev, "gseg 0x%lx\n", req.gseg);
if (!req.gseg) return gru_unload_all_contexts();
gts = gru_find_lock_gts(req.gseg); if (!gts) return -EINVAL;
if (gts->ts_gru)
gru_unload_context(gts, 1);
gru_unlock_gts(gts);
return 0;
}
/* * User request to flush a range of virtual addresses from the GRU TLB * (Mainly for testing).
*/ int gru_user_flush_tlb(unsignedlong arg)
{ struct gru_thread_state *gts; struct gru_flush_tlb_req req; struct gru_mm_struct *gms;
STAT(user_flush_tlb); if (copy_from_user(&req, (void __user *)arg, sizeof(req))) return -EFAULT;
gru_dbg(grudev, "gseg 0x%lx, vaddr 0x%lx, len 0x%lx\n", req.gseg,
req.vaddr, req.len);
gts = gru_find_lock_gts(req.gseg); if (!gts) return -EINVAL;
if (copy_from_user(&req, (void __user *)arg, sizeof(req))) return -EFAULT;
/* * The library creates arrays of contexts for threaded programs. * If no gts exists in the array, the context has never been used & all * statistics are implicitly 0.
*/
gts = gru_find_lock_gts(req.gseg); if (gts) {
memcpy(&req.stats, >s->ustats, sizeof(gts->ustats));
gru_unlock_gts(gts);
} else {
memset(&req.stats, 0, sizeof(gts->ustats));
}
if (copy_to_user((void __user *)arg, &req, sizeof(req))) return -EFAULT;
return 0;
}
/* * Register the current task as the user of the GSEG slice. * Needed for TLB fault interrupt targeting.
*/ int gru_set_context_option(unsignedlong arg)
{ struct gru_thread_state *gts; struct gru_set_context_option_req req; int ret = 0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.