18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * VAS Fault handling.
48c2ecf20Sopenharmony_ci * Copyright 2019, IBM Corporation
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#define pr_fmt(fmt) "vas: " fmt
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/kernel.h>
108c2ecf20Sopenharmony_ci#include <linux/types.h>
118c2ecf20Sopenharmony_ci#include <linux/slab.h>
128c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
138c2ecf20Sopenharmony_ci#include <linux/kthread.h>
148c2ecf20Sopenharmony_ci#include <linux/sched/signal.h>
158c2ecf20Sopenharmony_ci#include <linux/mmu_context.h>
168c2ecf20Sopenharmony_ci#include <asm/icswx.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include "vas.h"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * The maximum FIFO size for fault window can be 8MB
228c2ecf20Sopenharmony_ci * (VAS_RX_FIFO_SIZE_MAX). Using 4MB FIFO since each VAS
238c2ecf20Sopenharmony_ci * instance will be having fault window.
248c2ecf20Sopenharmony_ci * 8MB FIFO can be used if expects more faults for each VAS
258c2ecf20Sopenharmony_ci * instance.
268c2ecf20Sopenharmony_ci */
278c2ecf20Sopenharmony_ci#define VAS_FAULT_WIN_FIFO_SIZE	(4 << 20)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic void dump_crb(struct coprocessor_request_block *crb)
308c2ecf20Sopenharmony_ci{
318c2ecf20Sopenharmony_ci	struct data_descriptor_entry *dde;
328c2ecf20Sopenharmony_ci	struct nx_fault_stamp *nx;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci	dde = &crb->source;
358c2ecf20Sopenharmony_ci	pr_devel("SrcDDE: addr 0x%llx, len %d, count %d, idx %d, flags %d\n",
368c2ecf20Sopenharmony_ci		be64_to_cpu(dde->address), be32_to_cpu(dde->length),
378c2ecf20Sopenharmony_ci		dde->count, dde->index, dde->flags);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci	dde = &crb->target;
408c2ecf20Sopenharmony_ci	pr_devel("TgtDDE: addr 0x%llx, len %d, count %d, idx %d, flags %d\n",
418c2ecf20Sopenharmony_ci		be64_to_cpu(dde->address), be32_to_cpu(dde->length),
428c2ecf20Sopenharmony_ci		dde->count, dde->index, dde->flags);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	nx = &crb->stamp.nx;
458c2ecf20Sopenharmony_ci	pr_devel("NX Stamp: PSWID 0x%x, FSA 0x%llx, flags 0x%x, FS 0x%x\n",
468c2ecf20Sopenharmony_ci		be32_to_cpu(nx->pswid),
478c2ecf20Sopenharmony_ci		be64_to_cpu(crb->stamp.nx.fault_storage_addr),
488c2ecf20Sopenharmony_ci		nx->flags, nx->fault_status);
498c2ecf20Sopenharmony_ci}
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/*
528c2ecf20Sopenharmony_ci * Update the CSB to indicate a translation error.
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * User space will be polling on CSB after the request is issued.
558c2ecf20Sopenharmony_ci * If NX can handle the request without any issues, it updates CSB.
568c2ecf20Sopenharmony_ci * Whereas if NX encounters page fault, the kernel will handle the
578c2ecf20Sopenharmony_ci * fault and update CSB with translation error.
588c2ecf20Sopenharmony_ci *
598c2ecf20Sopenharmony_ci * If we are unable to update the CSB means copy_to_user failed due to
608c2ecf20Sopenharmony_ci * invalid csb_addr, send a signal to the process.
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_cistatic void update_csb(struct vas_window *window,
638c2ecf20Sopenharmony_ci			struct coprocessor_request_block *crb)
648c2ecf20Sopenharmony_ci{
658c2ecf20Sopenharmony_ci	struct coprocessor_status_block csb;
668c2ecf20Sopenharmony_ci	struct kernel_siginfo info;
678c2ecf20Sopenharmony_ci	struct task_struct *tsk;
688c2ecf20Sopenharmony_ci	void __user *csb_addr;
698c2ecf20Sopenharmony_ci	struct pid *pid;
708c2ecf20Sopenharmony_ci	int rc;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/*
738c2ecf20Sopenharmony_ci	 * NX user space windows can not be opened for task->mm=NULL
748c2ecf20Sopenharmony_ci	 * and faults will not be generated for kernel requests.
758c2ecf20Sopenharmony_ci	 */
768c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!window->mm || !window->user_win))
778c2ecf20Sopenharmony_ci		return;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	csb_addr = (void __user *)be64_to_cpu(crb->csb_addr);
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	memset(&csb, 0, sizeof(csb));
828c2ecf20Sopenharmony_ci	csb.cc = CSB_CC_FAULT_ADDRESS;
838c2ecf20Sopenharmony_ci	csb.ce = CSB_CE_TERMINATION;
848c2ecf20Sopenharmony_ci	csb.cs = 0;
858c2ecf20Sopenharmony_ci	csb.count = 0;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	/*
888c2ecf20Sopenharmony_ci	 * NX operates and returns in BE format as defined CRB struct.
898c2ecf20Sopenharmony_ci	 * So saves fault_storage_addr in BE as NX pastes in FIFO and
908c2ecf20Sopenharmony_ci	 * expects user space to convert to CPU format.
918c2ecf20Sopenharmony_ci	 */
928c2ecf20Sopenharmony_ci	csb.address = crb->stamp.nx.fault_storage_addr;
938c2ecf20Sopenharmony_ci	csb.flags = 0;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	pid = window->pid;
968c2ecf20Sopenharmony_ci	tsk = get_pid_task(pid, PIDTYPE_PID);
978c2ecf20Sopenharmony_ci	/*
988c2ecf20Sopenharmony_ci	 * Process closes send window after all pending NX requests are
998c2ecf20Sopenharmony_ci	 * completed. In multi-thread applications, a child thread can
1008c2ecf20Sopenharmony_ci	 * open a window and can exit without closing it. May be some
1018c2ecf20Sopenharmony_ci	 * requests are pending or this window can be used by other
1028c2ecf20Sopenharmony_ci	 * threads later. We should handle faults if NX encounters
1038c2ecf20Sopenharmony_ci	 * pages faults on these requests. Update CSB with translation
1048c2ecf20Sopenharmony_ci	 * error and fault address. If csb_addr passed by user space is
1058c2ecf20Sopenharmony_ci	 * invalid, send SEGV signal to pid saved in window. If the
1068c2ecf20Sopenharmony_ci	 * child thread is not running, send the signal to tgid.
1078c2ecf20Sopenharmony_ci	 * Parent thread (tgid) will close this window upon its exit.
1088c2ecf20Sopenharmony_ci	 *
1098c2ecf20Sopenharmony_ci	 * pid and mm references are taken when window is opened by
1108c2ecf20Sopenharmony_ci	 * process (pid). So tgid is used only when child thread opens
1118c2ecf20Sopenharmony_ci	 * a window and exits without closing it.
1128c2ecf20Sopenharmony_ci	 */
1138c2ecf20Sopenharmony_ci	if (!tsk) {
1148c2ecf20Sopenharmony_ci		pid = window->tgid;
1158c2ecf20Sopenharmony_ci		tsk = get_pid_task(pid, PIDTYPE_PID);
1168c2ecf20Sopenharmony_ci		/*
1178c2ecf20Sopenharmony_ci		 * Parent thread (tgid) will be closing window when it
1188c2ecf20Sopenharmony_ci		 * exits. So should not get here.
1198c2ecf20Sopenharmony_ci		 */
1208c2ecf20Sopenharmony_ci		if (WARN_ON_ONCE(!tsk))
1218c2ecf20Sopenharmony_ci			return;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/* Return if the task is exiting. */
1258c2ecf20Sopenharmony_ci	if (tsk->flags & PF_EXITING) {
1268c2ecf20Sopenharmony_ci		put_task_struct(tsk);
1278c2ecf20Sopenharmony_ci		return;
1288c2ecf20Sopenharmony_ci	}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	kthread_use_mm(window->mm);
1318c2ecf20Sopenharmony_ci	rc = copy_to_user(csb_addr, &csb, sizeof(csb));
1328c2ecf20Sopenharmony_ci	/*
1338c2ecf20Sopenharmony_ci	 * User space polls on csb.flags (first byte). So add barrier
1348c2ecf20Sopenharmony_ci	 * then copy first byte with csb flags update.
1358c2ecf20Sopenharmony_ci	 */
1368c2ecf20Sopenharmony_ci	if (!rc) {
1378c2ecf20Sopenharmony_ci		csb.flags = CSB_V;
1388c2ecf20Sopenharmony_ci		/* Make sure update to csb.flags is visible now */
1398c2ecf20Sopenharmony_ci		smp_mb();
1408c2ecf20Sopenharmony_ci		rc = copy_to_user(csb_addr, &csb, sizeof(u8));
1418c2ecf20Sopenharmony_ci	}
1428c2ecf20Sopenharmony_ci	kthread_unuse_mm(window->mm);
1438c2ecf20Sopenharmony_ci	put_task_struct(tsk);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* Success */
1468c2ecf20Sopenharmony_ci	if (!rc)
1478c2ecf20Sopenharmony_ci		return;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	pr_debug("Invalid CSB address 0x%p signalling pid(%d)\n",
1508c2ecf20Sopenharmony_ci			csb_addr, pid_vnr(pid));
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	clear_siginfo(&info);
1538c2ecf20Sopenharmony_ci	info.si_signo = SIGSEGV;
1548c2ecf20Sopenharmony_ci	info.si_errno = EFAULT;
1558c2ecf20Sopenharmony_ci	info.si_code = SEGV_MAPERR;
1568c2ecf20Sopenharmony_ci	info.si_addr = csb_addr;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	/*
1598c2ecf20Sopenharmony_ci	 * process will be polling on csb.flags after request is sent to
1608c2ecf20Sopenharmony_ci	 * NX. So generally CSB update should not fail except when an
1618c2ecf20Sopenharmony_ci	 * application passes invalid csb_addr. So an error message will
1628c2ecf20Sopenharmony_ci	 * be displayed and leave it to user space whether to ignore or
1638c2ecf20Sopenharmony_ci	 * handle this signal.
1648c2ecf20Sopenharmony_ci	 */
1658c2ecf20Sopenharmony_ci	rcu_read_lock();
1668c2ecf20Sopenharmony_ci	rc = kill_pid_info(SIGSEGV, &info, pid);
1678c2ecf20Sopenharmony_ci	rcu_read_unlock();
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	pr_devel("%s(): pid %d kill_proc_info() rc %d\n", __func__,
1708c2ecf20Sopenharmony_ci			pid_vnr(pid), rc);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic void dump_fifo(struct vas_instance *vinst, void *entry)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	unsigned long *end = vinst->fault_fifo + vinst->fault_fifo_size;
1768c2ecf20Sopenharmony_ci	unsigned long *fifo = entry;
1778c2ecf20Sopenharmony_ci	int i;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	pr_err("Fault fifo size %d, Max crbs %d\n", vinst->fault_fifo_size,
1808c2ecf20Sopenharmony_ci			vinst->fault_fifo_size / CRB_SIZE);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	/* Dump 10 CRB entries or until end of FIFO */
1838c2ecf20Sopenharmony_ci	pr_err("Fault FIFO Dump:\n");
1848c2ecf20Sopenharmony_ci	for (i = 0; i < 10*(CRB_SIZE/8) && fifo < end; i += 4, fifo += 4) {
1858c2ecf20Sopenharmony_ci		pr_err("[%.3d, %p]: 0x%.16lx 0x%.16lx 0x%.16lx 0x%.16lx\n",
1868c2ecf20Sopenharmony_ci			i, fifo, *fifo, *(fifo+1), *(fifo+2), *(fifo+3));
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci/*
1918c2ecf20Sopenharmony_ci * Process valid CRBs in fault FIFO.
1928c2ecf20Sopenharmony_ci * NX process user space requests, return credit and update the status
1938c2ecf20Sopenharmony_ci * in CRB. If it encounters transalation error when accessing CRB or
1948c2ecf20Sopenharmony_ci * request buffers, raises interrupt on the CPU to handle the fault.
1958c2ecf20Sopenharmony_ci * It takes credit on fault window, updates nx_fault_stamp in CRB with
1968c2ecf20Sopenharmony_ci * the following information and pastes CRB in fault FIFO.
1978c2ecf20Sopenharmony_ci *
1988c2ecf20Sopenharmony_ci * pswid - window ID of the window on which the request is sent.
1998c2ecf20Sopenharmony_ci * fault_storage_addr - fault address
2008c2ecf20Sopenharmony_ci *
2018c2ecf20Sopenharmony_ci * It can raise a single interrupt for multiple faults. Expects OS to
2028c2ecf20Sopenharmony_ci * process all valid faults and return credit for each fault on user
2038c2ecf20Sopenharmony_ci * space and fault windows. This fault FIFO control will be done with
2048c2ecf20Sopenharmony_ci * credit mechanism. NX can continuously paste CRBs until credits are not
2058c2ecf20Sopenharmony_ci * available on fault window. Otherwise, returns with RMA_reject.
2068c2ecf20Sopenharmony_ci *
2078c2ecf20Sopenharmony_ci * Total credits available on fault window: FIFO_SIZE(4MB)/CRBS_SIZE(128)
2088c2ecf20Sopenharmony_ci *
2098c2ecf20Sopenharmony_ci */
2108c2ecf20Sopenharmony_ciirqreturn_t vas_fault_thread_fn(int irq, void *data)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	struct vas_instance *vinst = data;
2138c2ecf20Sopenharmony_ci	struct coprocessor_request_block *crb, *entry;
2148c2ecf20Sopenharmony_ci	struct coprocessor_request_block buf;
2158c2ecf20Sopenharmony_ci	struct vas_window *window;
2168c2ecf20Sopenharmony_ci	unsigned long flags;
2178c2ecf20Sopenharmony_ci	void *fifo;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	crb = &buf;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	/*
2228c2ecf20Sopenharmony_ci	 * VAS can interrupt with multiple page faults. So process all
2238c2ecf20Sopenharmony_ci	 * valid CRBs within fault FIFO until reaches invalid CRB.
2248c2ecf20Sopenharmony_ci	 * We use CCW[0] and pswid to validate validate CRBs:
2258c2ecf20Sopenharmony_ci	 *
2268c2ecf20Sopenharmony_ci	 * CCW[0]	Reserved bit. When NX pastes CRB, CCW[0]=0
2278c2ecf20Sopenharmony_ci	 *		OS sets this bit to 1 after reading CRB.
2288c2ecf20Sopenharmony_ci	 * pswid	NX assigns window ID. Set pswid to -1 after
2298c2ecf20Sopenharmony_ci	 *		reading CRB from fault FIFO.
2308c2ecf20Sopenharmony_ci	 *
2318c2ecf20Sopenharmony_ci	 * We exit this function if no valid CRBs are available to process.
2328c2ecf20Sopenharmony_ci	 * So acquire fault_lock and reset fifo_in_progress to 0 before
2338c2ecf20Sopenharmony_ci	 * exit.
2348c2ecf20Sopenharmony_ci	 * In case kernel receives another interrupt with different page
2358c2ecf20Sopenharmony_ci	 * fault, interrupt handler returns with IRQ_HANDLED if
2368c2ecf20Sopenharmony_ci	 * fifo_in_progress is set. Means these new faults will be
2378c2ecf20Sopenharmony_ci	 * handled by the current thread. Otherwise set fifo_in_progress
2388c2ecf20Sopenharmony_ci	 * and return IRQ_WAKE_THREAD to wake up thread.
2398c2ecf20Sopenharmony_ci	 */
2408c2ecf20Sopenharmony_ci	while (true) {
2418c2ecf20Sopenharmony_ci		spin_lock_irqsave(&vinst->fault_lock, flags);
2428c2ecf20Sopenharmony_ci		/*
2438c2ecf20Sopenharmony_ci		 * Advance the fault fifo pointer to next CRB.
2448c2ecf20Sopenharmony_ci		 * Use CRB_SIZE rather than sizeof(*crb) since the latter is
2458c2ecf20Sopenharmony_ci		 * aligned to CRB_ALIGN (256) but the CRB written to by VAS is
2468c2ecf20Sopenharmony_ci		 * only CRB_SIZE in len.
2478c2ecf20Sopenharmony_ci		 */
2488c2ecf20Sopenharmony_ci		fifo = vinst->fault_fifo + (vinst->fault_crbs * CRB_SIZE);
2498c2ecf20Sopenharmony_ci		entry = fifo;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci		if ((entry->stamp.nx.pswid == cpu_to_be32(FIFO_INVALID_ENTRY))
2528c2ecf20Sopenharmony_ci			|| (entry->ccw & cpu_to_be32(CCW0_INVALID))) {
2538c2ecf20Sopenharmony_ci			vinst->fifo_in_progress = 0;
2548c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&vinst->fault_lock, flags);
2558c2ecf20Sopenharmony_ci			return IRQ_HANDLED;
2568c2ecf20Sopenharmony_ci		}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&vinst->fault_lock, flags);
2598c2ecf20Sopenharmony_ci		vinst->fault_crbs++;
2608c2ecf20Sopenharmony_ci		if (vinst->fault_crbs == (vinst->fault_fifo_size / CRB_SIZE))
2618c2ecf20Sopenharmony_ci			vinst->fault_crbs = 0;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci		memcpy(crb, fifo, CRB_SIZE);
2648c2ecf20Sopenharmony_ci		entry->stamp.nx.pswid = cpu_to_be32(FIFO_INVALID_ENTRY);
2658c2ecf20Sopenharmony_ci		entry->ccw |= cpu_to_be32(CCW0_INVALID);
2668c2ecf20Sopenharmony_ci		/*
2678c2ecf20Sopenharmony_ci		 * Return credit for the fault window.
2688c2ecf20Sopenharmony_ci		 */
2698c2ecf20Sopenharmony_ci		vas_return_credit(vinst->fault_win, false);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		pr_devel("VAS[%d] fault_fifo %p, fifo %p, fault_crbs %d\n",
2728c2ecf20Sopenharmony_ci				vinst->vas_id, vinst->fault_fifo, fifo,
2738c2ecf20Sopenharmony_ci				vinst->fault_crbs);
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci		dump_crb(crb);
2768c2ecf20Sopenharmony_ci		window = vas_pswid_to_window(vinst,
2778c2ecf20Sopenharmony_ci				be32_to_cpu(crb->stamp.nx.pswid));
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci		if (IS_ERR(window)) {
2808c2ecf20Sopenharmony_ci			/*
2818c2ecf20Sopenharmony_ci			 * We got an interrupt about a specific send
2828c2ecf20Sopenharmony_ci			 * window but we can't find that window and we can't
2838c2ecf20Sopenharmony_ci			 * even clean it up (return credit on user space
2848c2ecf20Sopenharmony_ci			 * window).
2858c2ecf20Sopenharmony_ci			 * But we should not get here.
2868c2ecf20Sopenharmony_ci			 * TODO: Disable IRQ.
2878c2ecf20Sopenharmony_ci			 */
2888c2ecf20Sopenharmony_ci			dump_fifo(vinst, (void *)entry);
2898c2ecf20Sopenharmony_ci			pr_err("VAS[%d] fault_fifo %p, fifo %p, pswid 0x%x, fault_crbs %d bad CRB?\n",
2908c2ecf20Sopenharmony_ci				vinst->vas_id, vinst->fault_fifo, fifo,
2918c2ecf20Sopenharmony_ci				be32_to_cpu(crb->stamp.nx.pswid),
2928c2ecf20Sopenharmony_ci				vinst->fault_crbs);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci			WARN_ON_ONCE(1);
2958c2ecf20Sopenharmony_ci		} else {
2968c2ecf20Sopenharmony_ci			update_csb(window, crb);
2978c2ecf20Sopenharmony_ci			/*
2988c2ecf20Sopenharmony_ci			 * Return credit for send window after processing
2998c2ecf20Sopenharmony_ci			 * fault CRB.
3008c2ecf20Sopenharmony_ci			 */
3018c2ecf20Sopenharmony_ci			vas_return_credit(window, true);
3028c2ecf20Sopenharmony_ci		}
3038c2ecf20Sopenharmony_ci	}
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ciirqreturn_t vas_fault_handler(int irq, void *dev_id)
3078c2ecf20Sopenharmony_ci{
3088c2ecf20Sopenharmony_ci	struct vas_instance *vinst = dev_id;
3098c2ecf20Sopenharmony_ci	irqreturn_t ret = IRQ_WAKE_THREAD;
3108c2ecf20Sopenharmony_ci	unsigned long flags;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/*
3138c2ecf20Sopenharmony_ci	 * NX can generate an interrupt for multiple faults. So the
3148c2ecf20Sopenharmony_ci	 * fault handler thread process all CRBs until finds invalid
3158c2ecf20Sopenharmony_ci	 * entry. In case if NX sees continuous faults, it is possible
3168c2ecf20Sopenharmony_ci	 * that the thread function entered with the first interrupt
3178c2ecf20Sopenharmony_ci	 * can execute and process all valid CRBs.
3188c2ecf20Sopenharmony_ci	 * So wake up thread only if the fault thread is not in progress.
3198c2ecf20Sopenharmony_ci	 */
3208c2ecf20Sopenharmony_ci	spin_lock_irqsave(&vinst->fault_lock, flags);
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	if (vinst->fifo_in_progress)
3238c2ecf20Sopenharmony_ci		ret = IRQ_HANDLED;
3248c2ecf20Sopenharmony_ci	else
3258c2ecf20Sopenharmony_ci		vinst->fifo_in_progress = 1;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&vinst->fault_lock, flags);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	return ret;
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci/*
3338c2ecf20Sopenharmony_ci * Fault window is opened per VAS instance. NX pastes fault CRB in fault
3348c2ecf20Sopenharmony_ci * FIFO upon page faults.
3358c2ecf20Sopenharmony_ci */
3368c2ecf20Sopenharmony_ciint vas_setup_fault_window(struct vas_instance *vinst)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	struct vas_rx_win_attr attr;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	vinst->fault_fifo_size = VAS_FAULT_WIN_FIFO_SIZE;
3418c2ecf20Sopenharmony_ci	vinst->fault_fifo = kzalloc(vinst->fault_fifo_size, GFP_KERNEL);
3428c2ecf20Sopenharmony_ci	if (!vinst->fault_fifo) {
3438c2ecf20Sopenharmony_ci		pr_err("Unable to alloc %d bytes for fault_fifo\n",
3448c2ecf20Sopenharmony_ci				vinst->fault_fifo_size);
3458c2ecf20Sopenharmony_ci		return -ENOMEM;
3468c2ecf20Sopenharmony_ci	}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	/*
3498c2ecf20Sopenharmony_ci	 * Invalidate all CRB entries. NX pastes valid entry for each fault.
3508c2ecf20Sopenharmony_ci	 */
3518c2ecf20Sopenharmony_ci	memset(vinst->fault_fifo, FIFO_INVALID_ENTRY, vinst->fault_fifo_size);
3528c2ecf20Sopenharmony_ci	vas_init_rx_win_attr(&attr, VAS_COP_TYPE_FAULT);
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	attr.rx_fifo_size = vinst->fault_fifo_size;
3558c2ecf20Sopenharmony_ci	attr.rx_fifo = __pa(vinst->fault_fifo);
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	/*
3588c2ecf20Sopenharmony_ci	 * Max creds is based on number of CRBs can fit in the FIFO.
3598c2ecf20Sopenharmony_ci	 * (fault_fifo_size/CRB_SIZE). If 8MB FIFO is used, max creds
3608c2ecf20Sopenharmony_ci	 * will be 0xffff since the receive creds field is 16bits wide.
3618c2ecf20Sopenharmony_ci	 */
3628c2ecf20Sopenharmony_ci	attr.wcreds_max = vinst->fault_fifo_size / CRB_SIZE;
3638c2ecf20Sopenharmony_ci	attr.lnotify_lpid = 0;
3648c2ecf20Sopenharmony_ci	attr.lnotify_pid = mfspr(SPRN_PID);
3658c2ecf20Sopenharmony_ci	attr.lnotify_tid = mfspr(SPRN_PID);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	vinst->fault_win = vas_rx_win_open(vinst->vas_id, VAS_COP_TYPE_FAULT,
3688c2ecf20Sopenharmony_ci					&attr);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	if (IS_ERR(vinst->fault_win)) {
3718c2ecf20Sopenharmony_ci		pr_err("VAS: Error %ld opening FaultWin\n",
3728c2ecf20Sopenharmony_ci			PTR_ERR(vinst->fault_win));
3738c2ecf20Sopenharmony_ci		kfree(vinst->fault_fifo);
3748c2ecf20Sopenharmony_ci		return PTR_ERR(vinst->fault_win);
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	pr_devel("VAS: Created FaultWin %d, LPID/PID/TID [%d/%d/%d]\n",
3788c2ecf20Sopenharmony_ci			vinst->fault_win->winid, attr.lnotify_lpid,
3798c2ecf20Sopenharmony_ci			attr.lnotify_pid, attr.lnotify_tid);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	return 0;
3828c2ecf20Sopenharmony_ci}
383