162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * VAS Fault handling. 462306a36Sopenharmony_ci * Copyright 2019, IBM Corporation 562306a36Sopenharmony_ci */ 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci#define pr_fmt(fmt) "vas: " fmt 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#include <linux/kernel.h> 1062306a36Sopenharmony_ci#include <linux/types.h> 1162306a36Sopenharmony_ci#include <linux/slab.h> 1262306a36Sopenharmony_ci#include <linux/uaccess.h> 1362306a36Sopenharmony_ci#include <linux/kthread.h> 1462306a36Sopenharmony_ci#include <linux/sched/signal.h> 1562306a36Sopenharmony_ci#include <linux/mmu_context.h> 1662306a36Sopenharmony_ci#include <asm/icswx.h> 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci#include "vas.h" 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci/* 2162306a36Sopenharmony_ci * The maximum FIFO size for fault window can be 8MB 2262306a36Sopenharmony_ci * (VAS_RX_FIFO_SIZE_MAX). Using 4MB FIFO since each VAS 2362306a36Sopenharmony_ci * instance will be having fault window. 2462306a36Sopenharmony_ci * 8MB FIFO can be used if expects more faults for each VAS 2562306a36Sopenharmony_ci * instance. 2662306a36Sopenharmony_ci */ 2762306a36Sopenharmony_ci#define VAS_FAULT_WIN_FIFO_SIZE (4 << 20) 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_cistatic void dump_fifo(struct vas_instance *vinst, void *entry) 3062306a36Sopenharmony_ci{ 3162306a36Sopenharmony_ci unsigned long *end = vinst->fault_fifo + vinst->fault_fifo_size; 3262306a36Sopenharmony_ci unsigned long *fifo = entry; 3362306a36Sopenharmony_ci int i; 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci pr_err("Fault fifo size %d, Max crbs %d\n", vinst->fault_fifo_size, 3662306a36Sopenharmony_ci vinst->fault_fifo_size / CRB_SIZE); 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci /* Dump 10 CRB entries or until end of FIFO */ 3962306a36Sopenharmony_ci pr_err("Fault FIFO Dump:\n"); 4062306a36Sopenharmony_ci for (i = 0; i < 10*(CRB_SIZE/8) && fifo < end; i += 4, fifo += 4) { 4162306a36Sopenharmony_ci pr_err("[%.3d, %p]: 0x%.16lx 0x%.16lx 0x%.16lx 0x%.16lx\n", 4262306a36Sopenharmony_ci i, fifo, *fifo, *(fifo+1), *(fifo+2), *(fifo+3)); 4362306a36Sopenharmony_ci } 4462306a36Sopenharmony_ci} 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_ci/* 4762306a36Sopenharmony_ci * Process valid CRBs in fault FIFO. 4862306a36Sopenharmony_ci * NX process user space requests, return credit and update the status 4962306a36Sopenharmony_ci * in CRB. If it encounters transalation error when accessing CRB or 5062306a36Sopenharmony_ci * request buffers, raises interrupt on the CPU to handle the fault. 5162306a36Sopenharmony_ci * It takes credit on fault window, updates nx_fault_stamp in CRB with 5262306a36Sopenharmony_ci * the following information and pastes CRB in fault FIFO. 5362306a36Sopenharmony_ci * 5462306a36Sopenharmony_ci * pswid - window ID of the window on which the request is sent. 5562306a36Sopenharmony_ci * fault_storage_addr - fault address 5662306a36Sopenharmony_ci * 5762306a36Sopenharmony_ci * It can raise a single interrupt for multiple faults. Expects OS to 5862306a36Sopenharmony_ci * process all valid faults and return credit for each fault on user 5962306a36Sopenharmony_ci * space and fault windows. This fault FIFO control will be done with 6062306a36Sopenharmony_ci * credit mechanism. NX can continuously paste CRBs until credits are not 6162306a36Sopenharmony_ci * available on fault window. Otherwise, returns with RMA_reject. 6262306a36Sopenharmony_ci * 6362306a36Sopenharmony_ci * Total credits available on fault window: FIFO_SIZE(4MB)/CRBS_SIZE(128) 6462306a36Sopenharmony_ci * 6562306a36Sopenharmony_ci */ 6662306a36Sopenharmony_ciirqreturn_t vas_fault_thread_fn(int irq, void *data) 6762306a36Sopenharmony_ci{ 6862306a36Sopenharmony_ci struct vas_instance *vinst = data; 6962306a36Sopenharmony_ci struct coprocessor_request_block *crb, *entry; 7062306a36Sopenharmony_ci struct coprocessor_request_block buf; 7162306a36Sopenharmony_ci struct pnv_vas_window *window; 7262306a36Sopenharmony_ci unsigned long flags; 7362306a36Sopenharmony_ci void *fifo; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci crb = &buf; 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci /* 7862306a36Sopenharmony_ci * VAS can interrupt with multiple page faults. So process all 7962306a36Sopenharmony_ci * valid CRBs within fault FIFO until reaches invalid CRB. 8062306a36Sopenharmony_ci * We use CCW[0] and pswid to validate CRBs: 8162306a36Sopenharmony_ci * 8262306a36Sopenharmony_ci * CCW[0] Reserved bit. When NX pastes CRB, CCW[0]=0 8362306a36Sopenharmony_ci * OS sets this bit to 1 after reading CRB. 8462306a36Sopenharmony_ci * pswid NX assigns window ID. Set pswid to -1 after 8562306a36Sopenharmony_ci * reading CRB from fault FIFO. 8662306a36Sopenharmony_ci * 8762306a36Sopenharmony_ci * We exit this function if no valid CRBs are available to process. 8862306a36Sopenharmony_ci * So acquire fault_lock and reset fifo_in_progress to 0 before 8962306a36Sopenharmony_ci * exit. 9062306a36Sopenharmony_ci * In case kernel receives another interrupt with different page 9162306a36Sopenharmony_ci * fault, interrupt handler returns with IRQ_HANDLED if 9262306a36Sopenharmony_ci * fifo_in_progress is set. Means these new faults will be 9362306a36Sopenharmony_ci * handled by the current thread. Otherwise set fifo_in_progress 9462306a36Sopenharmony_ci * and return IRQ_WAKE_THREAD to wake up thread. 9562306a36Sopenharmony_ci */ 9662306a36Sopenharmony_ci while (true) { 9762306a36Sopenharmony_ci spin_lock_irqsave(&vinst->fault_lock, flags); 9862306a36Sopenharmony_ci /* 9962306a36Sopenharmony_ci * Advance the fault fifo pointer to next CRB. 10062306a36Sopenharmony_ci * Use CRB_SIZE rather than sizeof(*crb) since the latter is 10162306a36Sopenharmony_ci * aligned to CRB_ALIGN (256) but the CRB written to by VAS is 10262306a36Sopenharmony_ci * only CRB_SIZE in len. 10362306a36Sopenharmony_ci */ 10462306a36Sopenharmony_ci fifo = vinst->fault_fifo + (vinst->fault_crbs * CRB_SIZE); 10562306a36Sopenharmony_ci entry = fifo; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci if ((entry->stamp.nx.pswid == cpu_to_be32(FIFO_INVALID_ENTRY)) 10862306a36Sopenharmony_ci || (entry->ccw & cpu_to_be32(CCW0_INVALID))) { 10962306a36Sopenharmony_ci vinst->fifo_in_progress = 0; 11062306a36Sopenharmony_ci spin_unlock_irqrestore(&vinst->fault_lock, flags); 11162306a36Sopenharmony_ci return IRQ_HANDLED; 11262306a36Sopenharmony_ci } 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ci spin_unlock_irqrestore(&vinst->fault_lock, flags); 11562306a36Sopenharmony_ci vinst->fault_crbs++; 11662306a36Sopenharmony_ci if (vinst->fault_crbs == (vinst->fault_fifo_size / CRB_SIZE)) 11762306a36Sopenharmony_ci vinst->fault_crbs = 0; 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci memcpy(crb, fifo, CRB_SIZE); 12062306a36Sopenharmony_ci entry->stamp.nx.pswid = cpu_to_be32(FIFO_INVALID_ENTRY); 12162306a36Sopenharmony_ci entry->ccw |= cpu_to_be32(CCW0_INVALID); 12262306a36Sopenharmony_ci /* 12362306a36Sopenharmony_ci * Return credit for the fault window. 12462306a36Sopenharmony_ci */ 12562306a36Sopenharmony_ci vas_return_credit(vinst->fault_win, false); 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci pr_devel("VAS[%d] fault_fifo %p, fifo %p, fault_crbs %d\n", 12862306a36Sopenharmony_ci vinst->vas_id, vinst->fault_fifo, fifo, 12962306a36Sopenharmony_ci vinst->fault_crbs); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci vas_dump_crb(crb); 13262306a36Sopenharmony_ci window = vas_pswid_to_window(vinst, 13362306a36Sopenharmony_ci be32_to_cpu(crb->stamp.nx.pswid)); 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci if (IS_ERR(window)) { 13662306a36Sopenharmony_ci /* 13762306a36Sopenharmony_ci * We got an interrupt about a specific send 13862306a36Sopenharmony_ci * window but we can't find that window and we can't 13962306a36Sopenharmony_ci * even clean it up (return credit on user space 14062306a36Sopenharmony_ci * window). 14162306a36Sopenharmony_ci * But we should not get here. 14262306a36Sopenharmony_ci * TODO: Disable IRQ. 14362306a36Sopenharmony_ci */ 14462306a36Sopenharmony_ci dump_fifo(vinst, (void *)entry); 14562306a36Sopenharmony_ci pr_err("VAS[%d] fault_fifo %p, fifo %p, pswid 0x%x, fault_crbs %d bad CRB?\n", 14662306a36Sopenharmony_ci vinst->vas_id, vinst->fault_fifo, fifo, 14762306a36Sopenharmony_ci be32_to_cpu(crb->stamp.nx.pswid), 14862306a36Sopenharmony_ci vinst->fault_crbs); 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci WARN_ON_ONCE(1); 15162306a36Sopenharmony_ci } else { 15262306a36Sopenharmony_ci /* 15362306a36Sopenharmony_ci * NX sees faults only with user space windows. 15462306a36Sopenharmony_ci */ 15562306a36Sopenharmony_ci if (window->user_win) 15662306a36Sopenharmony_ci vas_update_csb(crb, &window->vas_win.task_ref); 15762306a36Sopenharmony_ci else 15862306a36Sopenharmony_ci WARN_ON_ONCE(!window->user_win); 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci /* 16162306a36Sopenharmony_ci * Return credit for send window after processing 16262306a36Sopenharmony_ci * fault CRB. 16362306a36Sopenharmony_ci */ 16462306a36Sopenharmony_ci vas_return_credit(window, true); 16562306a36Sopenharmony_ci } 16662306a36Sopenharmony_ci } 16762306a36Sopenharmony_ci} 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ciirqreturn_t vas_fault_handler(int irq, void *dev_id) 17062306a36Sopenharmony_ci{ 17162306a36Sopenharmony_ci struct vas_instance *vinst = dev_id; 17262306a36Sopenharmony_ci irqreturn_t ret = IRQ_WAKE_THREAD; 17362306a36Sopenharmony_ci unsigned long flags; 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci /* 17662306a36Sopenharmony_ci * NX can generate an interrupt for multiple faults. So the 17762306a36Sopenharmony_ci * fault handler thread process all CRBs until finds invalid 17862306a36Sopenharmony_ci * entry. In case if NX sees continuous faults, it is possible 17962306a36Sopenharmony_ci * that the thread function entered with the first interrupt 18062306a36Sopenharmony_ci * can execute and process all valid CRBs. 18162306a36Sopenharmony_ci * So wake up thread only if the fault thread is not in progress. 18262306a36Sopenharmony_ci */ 18362306a36Sopenharmony_ci spin_lock_irqsave(&vinst->fault_lock, flags); 18462306a36Sopenharmony_ci 18562306a36Sopenharmony_ci if (vinst->fifo_in_progress) 18662306a36Sopenharmony_ci ret = IRQ_HANDLED; 18762306a36Sopenharmony_ci else 18862306a36Sopenharmony_ci vinst->fifo_in_progress = 1; 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ci spin_unlock_irqrestore(&vinst->fault_lock, flags); 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci return ret; 19362306a36Sopenharmony_ci} 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci/* 19662306a36Sopenharmony_ci * Fault window is opened per VAS instance. NX pastes fault CRB in fault 19762306a36Sopenharmony_ci * FIFO upon page faults. 19862306a36Sopenharmony_ci */ 19962306a36Sopenharmony_ciint vas_setup_fault_window(struct vas_instance *vinst) 20062306a36Sopenharmony_ci{ 20162306a36Sopenharmony_ci struct vas_rx_win_attr attr; 20262306a36Sopenharmony_ci struct vas_window *win; 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci vinst->fault_fifo_size = VAS_FAULT_WIN_FIFO_SIZE; 20562306a36Sopenharmony_ci vinst->fault_fifo = kzalloc(vinst->fault_fifo_size, GFP_KERNEL); 20662306a36Sopenharmony_ci if (!vinst->fault_fifo) { 20762306a36Sopenharmony_ci pr_err("Unable to alloc %d bytes for fault_fifo\n", 20862306a36Sopenharmony_ci vinst->fault_fifo_size); 20962306a36Sopenharmony_ci return -ENOMEM; 21062306a36Sopenharmony_ci } 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci /* 21362306a36Sopenharmony_ci * Invalidate all CRB entries. NX pastes valid entry for each fault. 21462306a36Sopenharmony_ci */ 21562306a36Sopenharmony_ci memset(vinst->fault_fifo, FIFO_INVALID_ENTRY, vinst->fault_fifo_size); 21662306a36Sopenharmony_ci vas_init_rx_win_attr(&attr, VAS_COP_TYPE_FAULT); 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci attr.rx_fifo_size = vinst->fault_fifo_size; 21962306a36Sopenharmony_ci attr.rx_fifo = __pa(vinst->fault_fifo); 22062306a36Sopenharmony_ci 22162306a36Sopenharmony_ci /* 22262306a36Sopenharmony_ci * Max creds is based on number of CRBs can fit in the FIFO. 22362306a36Sopenharmony_ci * (fault_fifo_size/CRB_SIZE). If 8MB FIFO is used, max creds 22462306a36Sopenharmony_ci * will be 0xffff since the receive creds field is 16bits wide. 22562306a36Sopenharmony_ci */ 22662306a36Sopenharmony_ci attr.wcreds_max = vinst->fault_fifo_size / CRB_SIZE; 22762306a36Sopenharmony_ci attr.lnotify_lpid = 0; 22862306a36Sopenharmony_ci attr.lnotify_pid = mfspr(SPRN_PID); 22962306a36Sopenharmony_ci attr.lnotify_tid = mfspr(SPRN_PID); 23062306a36Sopenharmony_ci 23162306a36Sopenharmony_ci win = vas_rx_win_open(vinst->vas_id, VAS_COP_TYPE_FAULT, &attr); 23262306a36Sopenharmony_ci if (IS_ERR(win)) { 23362306a36Sopenharmony_ci pr_err("VAS: Error %ld opening FaultWin\n", PTR_ERR(win)); 23462306a36Sopenharmony_ci kfree(vinst->fault_fifo); 23562306a36Sopenharmony_ci return PTR_ERR(win); 23662306a36Sopenharmony_ci } 23762306a36Sopenharmony_ci 23862306a36Sopenharmony_ci vinst->fault_win = container_of(win, struct pnv_vas_window, vas_win); 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci pr_devel("VAS: Created FaultWin %d, LPID/PID/TID [%d/%d/%d]\n", 24162306a36Sopenharmony_ci vinst->fault_win->vas_win.winid, attr.lnotify_lpid, 24262306a36Sopenharmony_ci attr.lnotify_pid, attr.lnotify_tid); 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci return 0; 24562306a36Sopenharmony_ci} 246