1413d6ed3SHaren Myneni // SPDX-License-Identifier: GPL-2.0-or-later
2413d6ed3SHaren Myneni /*
3413d6ed3SHaren Myneni * VAS user space API for its accelerators (Only NX-GZIP is supported now)
4413d6ed3SHaren Myneni * Copyright (C) 2019 Haren Myneni, IBM Corp
5413d6ed3SHaren Myneni */
6413d6ed3SHaren Myneni
76d21fb7dSHaren Myneni #define pr_fmt(fmt) "vas-api: " fmt
86d21fb7dSHaren Myneni
9413d6ed3SHaren Myneni #include <linux/kernel.h>
10413d6ed3SHaren Myneni #include <linux/device.h>
11413d6ed3SHaren Myneni #include <linux/cdev.h>
12413d6ed3SHaren Myneni #include <linux/fs.h>
13413d6ed3SHaren Myneni #include <linux/slab.h>
14413d6ed3SHaren Myneni #include <linux/uaccess.h>
153b267973SHaren Myneni #include <linux/kthread.h>
163b267973SHaren Myneni #include <linux/sched/signal.h>
173b267973SHaren Myneni #include <linux/mmu_context.h>
18413d6ed3SHaren Myneni #include <linux/io.h>
19413d6ed3SHaren Myneni #include <asm/vas.h>
20413d6ed3SHaren Myneni #include <uapi/asm/vas-api.h>
21413d6ed3SHaren Myneni
22413d6ed3SHaren Myneni /*
23413d6ed3SHaren Myneni * The driver creates the device node that can be used as follows:
24413d6ed3SHaren Myneni * For NX-GZIP
25413d6ed3SHaren Myneni *
26413d6ed3SHaren Myneni * fd = open("/dev/crypto/nx-gzip", O_RDWR);
27413d6ed3SHaren Myneni * rc = ioctl(fd, VAS_TX_WIN_OPEN, &attr);
28413d6ed3SHaren Myneni * paste_addr = mmap(NULL, PAGE_SIZE, prot, MAP_SHARED, fd, 0ULL).
29413d6ed3SHaren Myneni * vas_copy(&crb, 0, 1);
30413d6ed3SHaren Myneni * vas_paste(paste_addr, 0, 1);
31413d6ed3SHaren Myneni * close(fd) or exit process to close window.
32413d6ed3SHaren Myneni *
33413d6ed3SHaren Myneni * where "vas_copy" and "vas_paste" are defined in copy-paste.h.
34413d6ed3SHaren Myneni * copy/paste returns to the user space directly. So refer NX hardware
351fd02f66SJulia Lawall * documentation for exact copy/paste usage and completion / error
36413d6ed3SHaren Myneni * conditions.
37413d6ed3SHaren Myneni */
38413d6ed3SHaren Myneni
39413d6ed3SHaren Myneni /*
40413d6ed3SHaren Myneni * Wrapper object for the nx-gzip device - there is just one instance of
41413d6ed3SHaren Myneni * this node for the whole system.
42413d6ed3SHaren Myneni */
43413d6ed3SHaren Myneni static struct coproc_dev {
44413d6ed3SHaren Myneni struct cdev cdev;
45413d6ed3SHaren Myneni struct device *device;
46413d6ed3SHaren Myneni char *name;
47413d6ed3SHaren Myneni dev_t devt;
48413d6ed3SHaren Myneni struct class *class;
49413d6ed3SHaren Myneni enum vas_cop_type cop_type;
501a0d0d5eSHaren Myneni const struct vas_user_win_ops *vops;
51413d6ed3SHaren Myneni } coproc_device;
52413d6ed3SHaren Myneni
53413d6ed3SHaren Myneni struct coproc_instance {
54413d6ed3SHaren Myneni struct coproc_dev *coproc;
55413d6ed3SHaren Myneni struct vas_window *txwin;
56413d6ed3SHaren Myneni };
57413d6ed3SHaren Myneni
coproc_devnode(const struct device * dev,umode_t * mode)58fb12940fSGreg Kroah-Hartman static char *coproc_devnode(const struct device *dev, umode_t *mode)
59413d6ed3SHaren Myneni {
60413d6ed3SHaren Myneni return kasprintf(GFP_KERNEL, "crypto/%s", dev_name(dev));
61413d6ed3SHaren Myneni }
62413d6ed3SHaren Myneni
633856aa54SHaren Myneni /*
643856aa54SHaren Myneni * Take reference to pid and mm
653856aa54SHaren Myneni */
get_vas_user_win_ref(struct vas_user_win_ref * task_ref)663856aa54SHaren Myneni int get_vas_user_win_ref(struct vas_user_win_ref *task_ref)
673856aa54SHaren Myneni {
683856aa54SHaren Myneni /*
693856aa54SHaren Myneni * Window opened by a child thread may not be closed when
703856aa54SHaren Myneni * it exits. So take reference to its pid and release it
713856aa54SHaren Myneni * when the window is free by parent thread.
723856aa54SHaren Myneni * Acquire a reference to the task's pid to make sure
733856aa54SHaren Myneni * pid will not be re-used - needed only for multithread
743856aa54SHaren Myneni * applications.
753856aa54SHaren Myneni */
763856aa54SHaren Myneni task_ref->pid = get_task_pid(current, PIDTYPE_PID);
773856aa54SHaren Myneni /*
783856aa54SHaren Myneni * Acquire a reference to the task's mm.
793856aa54SHaren Myneni */
803856aa54SHaren Myneni task_ref->mm = get_task_mm(current);
813856aa54SHaren Myneni if (!task_ref->mm) {
823856aa54SHaren Myneni put_pid(task_ref->pid);
836d21fb7dSHaren Myneni pr_err("pid(%d): mm_struct is not found\n",
843856aa54SHaren Myneni current->pid);
853856aa54SHaren Myneni return -EPERM;
863856aa54SHaren Myneni }
873856aa54SHaren Myneni
883856aa54SHaren Myneni mmgrab(task_ref->mm);
893856aa54SHaren Myneni mmput(task_ref->mm);
903856aa54SHaren Myneni /*
913856aa54SHaren Myneni * Process closes window during exit. In the case of
923856aa54SHaren Myneni * multithread application, the child thread can open
933856aa54SHaren Myneni * window and can exit without closing it. So takes tgid
943856aa54SHaren Myneni * reference until window closed to make sure tgid is not
953856aa54SHaren Myneni * reused.
963856aa54SHaren Myneni */
973856aa54SHaren Myneni task_ref->tgid = find_get_pid(task_tgid_vnr(current));
983856aa54SHaren Myneni
993856aa54SHaren Myneni return 0;
1003856aa54SHaren Myneni }
1013856aa54SHaren Myneni
1023b267973SHaren Myneni /*
1033b267973SHaren Myneni * Successful return must release the task reference with
1043b267973SHaren Myneni * put_task_struct
1053b267973SHaren Myneni */
ref_get_pid_and_task(struct vas_user_win_ref * task_ref,struct task_struct ** tskp,struct pid ** pidp)1063b267973SHaren Myneni static bool ref_get_pid_and_task(struct vas_user_win_ref *task_ref,
1073b267973SHaren Myneni struct task_struct **tskp, struct pid **pidp)
1083b267973SHaren Myneni {
1093b267973SHaren Myneni struct task_struct *tsk;
1103b267973SHaren Myneni struct pid *pid;
1113b267973SHaren Myneni
1123b267973SHaren Myneni pid = task_ref->pid;
1133b267973SHaren Myneni tsk = get_pid_task(pid, PIDTYPE_PID);
1143b267973SHaren Myneni if (!tsk) {
1153b267973SHaren Myneni pid = task_ref->tgid;
1163b267973SHaren Myneni tsk = get_pid_task(pid, PIDTYPE_PID);
1173b267973SHaren Myneni /*
1183b267973SHaren Myneni * Parent thread (tgid) will be closing window when it
1193b267973SHaren Myneni * exits. So should not get here.
1203b267973SHaren Myneni */
1213b267973SHaren Myneni if (WARN_ON_ONCE(!tsk))
1223b267973SHaren Myneni return false;
1233b267973SHaren Myneni }
1243b267973SHaren Myneni
1253b267973SHaren Myneni /* Return if the task is exiting. */
1263b267973SHaren Myneni if (tsk->flags & PF_EXITING) {
1273b267973SHaren Myneni put_task_struct(tsk);
1283b267973SHaren Myneni return false;
1293b267973SHaren Myneni }
1303b267973SHaren Myneni
1313b267973SHaren Myneni *tskp = tsk;
1323b267973SHaren Myneni *pidp = pid;
1333b267973SHaren Myneni
1343b267973SHaren Myneni return true;
1353b267973SHaren Myneni }
1363b267973SHaren Myneni
1373b267973SHaren Myneni /*
1383b267973SHaren Myneni * Update the CSB to indicate a translation error.
1393b267973SHaren Myneni *
1403b267973SHaren Myneni * User space will be polling on CSB after the request is issued.
1413b267973SHaren Myneni * If NX can handle the request without any issues, it updates CSB.
1423b267973SHaren Myneni * Whereas if NX encounters page fault, the kernel will handle the
1433b267973SHaren Myneni * fault and update CSB with translation error.
1443b267973SHaren Myneni *
1453b267973SHaren Myneni * If we are unable to update the CSB means copy_to_user failed due to
1463b267973SHaren Myneni * invalid csb_addr, send a signal to the process.
1473b267973SHaren Myneni */
vas_update_csb(struct coprocessor_request_block * crb,struct vas_user_win_ref * task_ref)1483b267973SHaren Myneni void vas_update_csb(struct coprocessor_request_block *crb,
1493b267973SHaren Myneni struct vas_user_win_ref *task_ref)
1503b267973SHaren Myneni {
1513b267973SHaren Myneni struct coprocessor_status_block csb;
1523b267973SHaren Myneni struct kernel_siginfo info;
1533b267973SHaren Myneni struct task_struct *tsk;
1543b267973SHaren Myneni void __user *csb_addr;
1553b267973SHaren Myneni struct pid *pid;
1563b267973SHaren Myneni int rc;
1573b267973SHaren Myneni
1583b267973SHaren Myneni /*
1593b267973SHaren Myneni * NX user space windows can not be opened for task->mm=NULL
1603b267973SHaren Myneni * and faults will not be generated for kernel requests.
1613b267973SHaren Myneni */
1623b267973SHaren Myneni if (WARN_ON_ONCE(!task_ref->mm))
1633b267973SHaren Myneni return;
1643b267973SHaren Myneni
1653b267973SHaren Myneni csb_addr = (void __user *)be64_to_cpu(crb->csb_addr);
1663b267973SHaren Myneni
1673b267973SHaren Myneni memset(&csb, 0, sizeof(csb));
1683b267973SHaren Myneni csb.cc = CSB_CC_FAULT_ADDRESS;
1693b267973SHaren Myneni csb.ce = CSB_CE_TERMINATION;
1703b267973SHaren Myneni csb.cs = 0;
1713b267973SHaren Myneni csb.count = 0;
1723b267973SHaren Myneni
1733b267973SHaren Myneni /*
1743b267973SHaren Myneni * NX operates and returns in BE format as defined CRB struct.
1753b267973SHaren Myneni * So saves fault_storage_addr in BE as NX pastes in FIFO and
1763b267973SHaren Myneni * expects user space to convert to CPU format.
1773b267973SHaren Myneni */
1783b267973SHaren Myneni csb.address = crb->stamp.nx.fault_storage_addr;
1793b267973SHaren Myneni csb.flags = 0;
1803b267973SHaren Myneni
1813b267973SHaren Myneni /*
1823b267973SHaren Myneni * Process closes send window after all pending NX requests are
1833b267973SHaren Myneni * completed. In multi-thread applications, a child thread can
1843b267973SHaren Myneni * open a window and can exit without closing it. May be some
1853b267973SHaren Myneni * requests are pending or this window can be used by other
1863b267973SHaren Myneni * threads later. We should handle faults if NX encounters
1873b267973SHaren Myneni * pages faults on these requests. Update CSB with translation
1883b267973SHaren Myneni * error and fault address. If csb_addr passed by user space is
1893b267973SHaren Myneni * invalid, send SEGV signal to pid saved in window. If the
1903b267973SHaren Myneni * child thread is not running, send the signal to tgid.
1913b267973SHaren Myneni * Parent thread (tgid) will close this window upon its exit.
1923b267973SHaren Myneni *
1933b267973SHaren Myneni * pid and mm references are taken when window is opened by
1943b267973SHaren Myneni * process (pid). So tgid is used only when child thread opens
1953b267973SHaren Myneni * a window and exits without closing it.
1963b267973SHaren Myneni */
1973b267973SHaren Myneni
1983b267973SHaren Myneni if (!ref_get_pid_and_task(task_ref, &tsk, &pid))
1993b267973SHaren Myneni return;
2003b267973SHaren Myneni
2013b267973SHaren Myneni kthread_use_mm(task_ref->mm);
2023b267973SHaren Myneni rc = copy_to_user(csb_addr, &csb, sizeof(csb));
2033b267973SHaren Myneni /*
2043b267973SHaren Myneni * User space polls on csb.flags (first byte). So add barrier
2053b267973SHaren Myneni * then copy first byte with csb flags update.
2063b267973SHaren Myneni */
2073b267973SHaren Myneni if (!rc) {
2083b267973SHaren Myneni csb.flags = CSB_V;
2093b267973SHaren Myneni /* Make sure update to csb.flags is visible now */
2103b267973SHaren Myneni smp_mb();
2113b267973SHaren Myneni rc = copy_to_user(csb_addr, &csb, sizeof(u8));
2123b267973SHaren Myneni }
2133b267973SHaren Myneni kthread_unuse_mm(task_ref->mm);
2143b267973SHaren Myneni put_task_struct(tsk);
2153b267973SHaren Myneni
2163b267973SHaren Myneni /* Success */
2173b267973SHaren Myneni if (!rc)
2183b267973SHaren Myneni return;
2193b267973SHaren Myneni
2203b267973SHaren Myneni
2213b267973SHaren Myneni pr_debug("Invalid CSB address 0x%p signalling pid(%d)\n",
2223b267973SHaren Myneni csb_addr, pid_vnr(pid));
2233b267973SHaren Myneni
2243b267973SHaren Myneni clear_siginfo(&info);
2253b267973SHaren Myneni info.si_signo = SIGSEGV;
2263b267973SHaren Myneni info.si_errno = EFAULT;
2273b267973SHaren Myneni info.si_code = SEGV_MAPERR;
2283b267973SHaren Myneni info.si_addr = csb_addr;
2293b267973SHaren Myneni /*
2303b267973SHaren Myneni * process will be polling on csb.flags after request is sent to
2313b267973SHaren Myneni * NX. So generally CSB update should not fail except when an
2323b267973SHaren Myneni * application passes invalid csb_addr. So an error message will
2333b267973SHaren Myneni * be displayed and leave it to user space whether to ignore or
2343b267973SHaren Myneni * handle this signal.
2353b267973SHaren Myneni */
2363b267973SHaren Myneni rcu_read_lock();
2373b267973SHaren Myneni rc = kill_pid_info(SIGSEGV, &info, pid);
2383b267973SHaren Myneni rcu_read_unlock();
2393b267973SHaren Myneni
2406d21fb7dSHaren Myneni pr_devel("pid %d kill_proc_info() rc %d\n", pid_vnr(pid), rc);
2413b267973SHaren Myneni }
2423b267973SHaren Myneni
vas_dump_crb(struct coprocessor_request_block * crb)2433b267973SHaren Myneni void vas_dump_crb(struct coprocessor_request_block *crb)
2443b267973SHaren Myneni {
2453b267973SHaren Myneni struct data_descriptor_entry *dde;
2463b267973SHaren Myneni struct nx_fault_stamp *nx;
2473b267973SHaren Myneni
2483b267973SHaren Myneni dde = &crb->source;
2493b267973SHaren Myneni pr_devel("SrcDDE: addr 0x%llx, len %d, count %d, idx %d, flags %d\n",
2503b267973SHaren Myneni be64_to_cpu(dde->address), be32_to_cpu(dde->length),
2513b267973SHaren Myneni dde->count, dde->index, dde->flags);
2523b267973SHaren Myneni
2533b267973SHaren Myneni dde = &crb->target;
2543b267973SHaren Myneni pr_devel("TgtDDE: addr 0x%llx, len %d, count %d, idx %d, flags %d\n",
2553b267973SHaren Myneni be64_to_cpu(dde->address), be32_to_cpu(dde->length),
2563b267973SHaren Myneni dde->count, dde->index, dde->flags);
2573b267973SHaren Myneni
2583b267973SHaren Myneni nx = &crb->stamp.nx;
2593b267973SHaren Myneni pr_devel("NX Stamp: PSWID 0x%x, FSA 0x%llx, flags 0x%x, FS 0x%x\n",
2603b267973SHaren Myneni be32_to_cpu(nx->pswid),
2613b267973SHaren Myneni be64_to_cpu(crb->stamp.nx.fault_storage_addr),
2623b267973SHaren Myneni nx->flags, nx->fault_status);
2633b267973SHaren Myneni }
2643b267973SHaren Myneni
coproc_open(struct inode * inode,struct file * fp)265413d6ed3SHaren Myneni static int coproc_open(struct inode *inode, struct file *fp)
266413d6ed3SHaren Myneni {
267413d6ed3SHaren Myneni struct coproc_instance *cp_inst;
268413d6ed3SHaren Myneni
269413d6ed3SHaren Myneni cp_inst = kzalloc(sizeof(*cp_inst), GFP_KERNEL);
270413d6ed3SHaren Myneni if (!cp_inst)
271413d6ed3SHaren Myneni return -ENOMEM;
272413d6ed3SHaren Myneni
273413d6ed3SHaren Myneni cp_inst->coproc = container_of(inode->i_cdev, struct coproc_dev,
274413d6ed3SHaren Myneni cdev);
275413d6ed3SHaren Myneni fp->private_data = cp_inst;
276413d6ed3SHaren Myneni
277413d6ed3SHaren Myneni return 0;
278413d6ed3SHaren Myneni }
279413d6ed3SHaren Myneni
coproc_ioc_tx_win_open(struct file * fp,unsigned long arg)280413d6ed3SHaren Myneni static int coproc_ioc_tx_win_open(struct file *fp, unsigned long arg)
281413d6ed3SHaren Myneni {
282413d6ed3SHaren Myneni void __user *uptr = (void __user *)arg;
283413d6ed3SHaren Myneni struct vas_tx_win_open_attr uattr;
284413d6ed3SHaren Myneni struct coproc_instance *cp_inst;
285413d6ed3SHaren Myneni struct vas_window *txwin;
2861a0d0d5eSHaren Myneni int rc;
287413d6ed3SHaren Myneni
288413d6ed3SHaren Myneni cp_inst = fp->private_data;
289413d6ed3SHaren Myneni
290413d6ed3SHaren Myneni /*
291413d6ed3SHaren Myneni * One window for file descriptor
292413d6ed3SHaren Myneni */
293413d6ed3SHaren Myneni if (cp_inst->txwin)
294413d6ed3SHaren Myneni return -EEXIST;
295413d6ed3SHaren Myneni
296413d6ed3SHaren Myneni rc = copy_from_user(&uattr, uptr, sizeof(uattr));
297413d6ed3SHaren Myneni if (rc) {
2986d21fb7dSHaren Myneni pr_err("copy_from_user() returns %d\n", rc);
299413d6ed3SHaren Myneni return -EFAULT;
300413d6ed3SHaren Myneni }
301413d6ed3SHaren Myneni
302413d6ed3SHaren Myneni if (uattr.version != 1) {
3031a0d0d5eSHaren Myneni pr_err("Invalid window open API version\n");
304413d6ed3SHaren Myneni return -EINVAL;
305413d6ed3SHaren Myneni }
306413d6ed3SHaren Myneni
30761cb9ac6SGustavo A. R. Silva if (!cp_inst->coproc->vops || !cp_inst->coproc->vops->open_win) {
3081a0d0d5eSHaren Myneni pr_err("VAS API is not registered\n");
3091a0d0d5eSHaren Myneni return -EACCES;
3101a0d0d5eSHaren Myneni }
311413d6ed3SHaren Myneni
3121a0d0d5eSHaren Myneni txwin = cp_inst->coproc->vops->open_win(uattr.vas_id, uattr.flags,
3131a0d0d5eSHaren Myneni cp_inst->coproc->cop_type);
314413d6ed3SHaren Myneni if (IS_ERR(txwin)) {
3156d21fb7dSHaren Myneni pr_err_ratelimited("VAS window open failed rc=%ld\n",
316413d6ed3SHaren Myneni PTR_ERR(txwin));
317413d6ed3SHaren Myneni return PTR_ERR(txwin);
318413d6ed3SHaren Myneni }
319413d6ed3SHaren Myneni
3201fe3a33bSHaren Myneni mutex_init(&txwin->task_ref.mmap_mutex);
321413d6ed3SHaren Myneni cp_inst->txwin = txwin;
322413d6ed3SHaren Myneni
323413d6ed3SHaren Myneni return 0;
324413d6ed3SHaren Myneni }
325413d6ed3SHaren Myneni
coproc_release(struct inode * inode,struct file * fp)326413d6ed3SHaren Myneni static int coproc_release(struct inode *inode, struct file *fp)
327413d6ed3SHaren Myneni {
328413d6ed3SHaren Myneni struct coproc_instance *cp_inst = fp->private_data;
3291a0d0d5eSHaren Myneni int rc;
330413d6ed3SHaren Myneni
331413d6ed3SHaren Myneni if (cp_inst->txwin) {
3321a0d0d5eSHaren Myneni if (cp_inst->coproc->vops &&
3331a0d0d5eSHaren Myneni cp_inst->coproc->vops->close_win) {
3341a0d0d5eSHaren Myneni rc = cp_inst->coproc->vops->close_win(cp_inst->txwin);
3351a0d0d5eSHaren Myneni if (rc)
3361a0d0d5eSHaren Myneni return rc;
3371a0d0d5eSHaren Myneni }
338413d6ed3SHaren Myneni cp_inst->txwin = NULL;
339413d6ed3SHaren Myneni }
340413d6ed3SHaren Myneni
341413d6ed3SHaren Myneni kfree(cp_inst);
342413d6ed3SHaren Myneni fp->private_data = NULL;
343413d6ed3SHaren Myneni
344413d6ed3SHaren Myneni /*
345413d6ed3SHaren Myneni * We don't know here if user has other receive windows
346413d6ed3SHaren Myneni * open, so we can't really call clear_thread_tidr().
347413d6ed3SHaren Myneni * So, once the process calls set_thread_tidr(), the
348413d6ed3SHaren Myneni * TIDR value sticks around until process exits, resulting
349413d6ed3SHaren Myneni * in an extra copy in restore_sprs().
350413d6ed3SHaren Myneni */
351413d6ed3SHaren Myneni
352413d6ed3SHaren Myneni return 0;
353413d6ed3SHaren Myneni }
354413d6ed3SHaren Myneni
3551fe3a33bSHaren Myneni /*
356b5c63d90SHaren Myneni * If the executed instruction that caused the fault was a paste, then
357b5c63d90SHaren Myneni * clear regs CR0[EQ], advance NIP, and return 0. Else return error code.
358b5c63d90SHaren Myneni */
do_fail_paste(void)359b5c63d90SHaren Myneni static int do_fail_paste(void)
360b5c63d90SHaren Myneni {
361b5c63d90SHaren Myneni struct pt_regs *regs = current->thread.regs;
362b5c63d90SHaren Myneni u32 instword;
363b5c63d90SHaren Myneni
364b5c63d90SHaren Myneni if (WARN_ON_ONCE(!regs))
365b5c63d90SHaren Myneni return -EINVAL;
366b5c63d90SHaren Myneni
367b5c63d90SHaren Myneni if (WARN_ON_ONCE(!user_mode(regs)))
368b5c63d90SHaren Myneni return -EINVAL;
369b5c63d90SHaren Myneni
370b5c63d90SHaren Myneni /*
371b5c63d90SHaren Myneni * If we couldn't translate the instruction, the driver should
372b5c63d90SHaren Myneni * return success without handling the fault, it will be retried
373b5c63d90SHaren Myneni * or the instruction fetch will fault.
374b5c63d90SHaren Myneni */
375b5c63d90SHaren Myneni if (get_user(instword, (u32 __user *)(regs->nip)))
376b5c63d90SHaren Myneni return -EAGAIN;
377b5c63d90SHaren Myneni
378b5c63d90SHaren Myneni /*
379b5c63d90SHaren Myneni * Not a paste instruction, driver may fail the fault.
380b5c63d90SHaren Myneni */
381b5c63d90SHaren Myneni if ((instword & PPC_INST_PASTE_MASK) != PPC_INST_PASTE)
382b5c63d90SHaren Myneni return -ENOENT;
383b5c63d90SHaren Myneni
384b5c63d90SHaren Myneni regs->ccr &= ~0xe0000000; /* Clear CR0[0-2] to fail paste */
385b5c63d90SHaren Myneni regs_add_return_ip(regs, 4); /* Emulate the paste */
386b5c63d90SHaren Myneni
387b5c63d90SHaren Myneni return 0;
388b5c63d90SHaren Myneni }
389b5c63d90SHaren Myneni
390b5c63d90SHaren Myneni /*
3911fe3a33bSHaren Myneni * This fault handler is invoked when the core generates page fault on
3921fe3a33bSHaren Myneni * the paste address. Happens if the kernel closes window in hypervisor
3931fe3a33bSHaren Myneni * (on pseries) due to lost credit or the paste address is not mapped.
3941fe3a33bSHaren Myneni */
vas_mmap_fault(struct vm_fault * vmf)3951fe3a33bSHaren Myneni static vm_fault_t vas_mmap_fault(struct vm_fault *vmf)
3961fe3a33bSHaren Myneni {
3971fe3a33bSHaren Myneni struct vm_area_struct *vma = vmf->vma;
3981fe3a33bSHaren Myneni struct file *fp = vma->vm_file;
3991fe3a33bSHaren Myneni struct coproc_instance *cp_inst = fp->private_data;
4001fe3a33bSHaren Myneni struct vas_window *txwin;
4011fe3a33bSHaren Myneni vm_fault_t fault;
4021fe3a33bSHaren Myneni u64 paste_addr;
403b5c63d90SHaren Myneni int ret;
4041fe3a33bSHaren Myneni
4051fe3a33bSHaren Myneni /*
4061fe3a33bSHaren Myneni * window is not opened. Shouldn't expect this error.
4071fe3a33bSHaren Myneni */
4081fe3a33bSHaren Myneni if (!cp_inst || !cp_inst->txwin) {
4096d21fb7dSHaren Myneni pr_err("Unexpected fault on paste address with TX window closed\n");
4101fe3a33bSHaren Myneni return VM_FAULT_SIGBUS;
4111fe3a33bSHaren Myneni }
4121fe3a33bSHaren Myneni
4131fe3a33bSHaren Myneni txwin = cp_inst->txwin;
4141fe3a33bSHaren Myneni /*
4151fe3a33bSHaren Myneni * When the LPAR lost credits due to core removal or during
4161fe3a33bSHaren Myneni * migration, invalidate the existing mapping for the current
417e9adcfecSMike Kravetz * paste addresses and set windows in-active (zap_vma_pages in
4181fe3a33bSHaren Myneni * reconfig_close_windows()).
4191fe3a33bSHaren Myneni * New mapping will be done later after migration or new credits
4201fe3a33bSHaren Myneni * available. So continue to receive faults if the user space
4211fe3a33bSHaren Myneni * issue NX request.
4221fe3a33bSHaren Myneni */
4231fe3a33bSHaren Myneni if (txwin->task_ref.vma != vmf->vma) {
4246d21fb7dSHaren Myneni pr_err("No previous mapping with paste address\n");
4251fe3a33bSHaren Myneni return VM_FAULT_SIGBUS;
4261fe3a33bSHaren Myneni }
4271fe3a33bSHaren Myneni
4281fe3a33bSHaren Myneni mutex_lock(&txwin->task_ref.mmap_mutex);
4291fe3a33bSHaren Myneni /*
4301fe3a33bSHaren Myneni * The window may be inactive due to lost credit (Ex: core
4311fe3a33bSHaren Myneni * removal with DLPAR). If the window is active again when
4321fe3a33bSHaren Myneni * the credit is available, map the new paste address at the
4330d4bb5e4SJilin Yuan * window virtual address.
4341fe3a33bSHaren Myneni */
4351fe3a33bSHaren Myneni if (txwin->status == VAS_WIN_ACTIVE) {
4361fe3a33bSHaren Myneni paste_addr = cp_inst->coproc->vops->paste_addr(txwin);
4371fe3a33bSHaren Myneni if (paste_addr) {
4381fe3a33bSHaren Myneni fault = vmf_insert_pfn(vma, vma->vm_start,
4391fe3a33bSHaren Myneni (paste_addr >> PAGE_SHIFT));
4401fe3a33bSHaren Myneni mutex_unlock(&txwin->task_ref.mmap_mutex);
4411fe3a33bSHaren Myneni return fault;
4421fe3a33bSHaren Myneni }
4431fe3a33bSHaren Myneni }
4441fe3a33bSHaren Myneni mutex_unlock(&txwin->task_ref.mmap_mutex);
4451fe3a33bSHaren Myneni
446b5c63d90SHaren Myneni /*
447b5c63d90SHaren Myneni * Received this fault due to closing the actual window.
448b5c63d90SHaren Myneni * It can happen during migration or lost credits.
449b5c63d90SHaren Myneni * Since no mapping, return the paste instruction failure
450b5c63d90SHaren Myneni * to the user space.
451b5c63d90SHaren Myneni */
452b5c63d90SHaren Myneni ret = do_fail_paste();
453b5c63d90SHaren Myneni /*
454b5c63d90SHaren Myneni * The user space can retry several times until success (needed
455b5c63d90SHaren Myneni * for migration) or should fallback to SW compression or
456b5c63d90SHaren Myneni * manage with the existing open windows if available.
457b5c63d90SHaren Myneni * Looking at sysfs interface, it can determine whether these
458b5c63d90SHaren Myneni * failures are coming during migration or core removal:
459b5c63d90SHaren Myneni * nr_used_credits > nr_total_credits when lost credits
460b5c63d90SHaren Myneni */
461b5c63d90SHaren Myneni if (!ret || (ret == -EAGAIN))
462b5c63d90SHaren Myneni return VM_FAULT_NOPAGE;
463b5c63d90SHaren Myneni
4641fe3a33bSHaren Myneni return VM_FAULT_SIGBUS;
4651fe3a33bSHaren Myneni }
4661fe3a33bSHaren Myneni
467*b7f60ffdSHaren Myneni /*
468*b7f60ffdSHaren Myneni * During mmap() paste address, mapping VMA is saved in VAS window
469*b7f60ffdSHaren Myneni * struct which is used to unmap during migration if the window is
470*b7f60ffdSHaren Myneni * still open. But the user space can remove this mapping with
471*b7f60ffdSHaren Myneni * munmap() before closing the window and the VMA address will
472*b7f60ffdSHaren Myneni * be invalid. Set VAS window VMA to NULL in this function which
473*b7f60ffdSHaren Myneni * is called before VMA free.
474*b7f60ffdSHaren Myneni */
vas_mmap_close(struct vm_area_struct * vma)475*b7f60ffdSHaren Myneni static void vas_mmap_close(struct vm_area_struct *vma)
476*b7f60ffdSHaren Myneni {
477*b7f60ffdSHaren Myneni struct file *fp = vma->vm_file;
478*b7f60ffdSHaren Myneni struct coproc_instance *cp_inst = fp->private_data;
479*b7f60ffdSHaren Myneni struct vas_window *txwin;
480*b7f60ffdSHaren Myneni
481*b7f60ffdSHaren Myneni /* Should not happen */
482*b7f60ffdSHaren Myneni if (!cp_inst || !cp_inst->txwin) {
483*b7f60ffdSHaren Myneni pr_err("No attached VAS window for the paste address mmap\n");
484*b7f60ffdSHaren Myneni return;
485*b7f60ffdSHaren Myneni }
486*b7f60ffdSHaren Myneni
487*b7f60ffdSHaren Myneni txwin = cp_inst->txwin;
488*b7f60ffdSHaren Myneni /*
489*b7f60ffdSHaren Myneni * task_ref.vma is set in coproc_mmap() during mmap paste
490*b7f60ffdSHaren Myneni * address. So it has to be the same VMA that is getting freed.
491*b7f60ffdSHaren Myneni */
492*b7f60ffdSHaren Myneni if (WARN_ON(txwin->task_ref.vma != vma)) {
493*b7f60ffdSHaren Myneni pr_err("Invalid paste address mmaping\n");
494*b7f60ffdSHaren Myneni return;
495*b7f60ffdSHaren Myneni }
496*b7f60ffdSHaren Myneni
497*b7f60ffdSHaren Myneni mutex_lock(&txwin->task_ref.mmap_mutex);
498*b7f60ffdSHaren Myneni txwin->task_ref.vma = NULL;
499*b7f60ffdSHaren Myneni mutex_unlock(&txwin->task_ref.mmap_mutex);
500*b7f60ffdSHaren Myneni }
501*b7f60ffdSHaren Myneni
5021fe3a33bSHaren Myneni static const struct vm_operations_struct vas_vm_ops = {
503*b7f60ffdSHaren Myneni .close = vas_mmap_close,
5041fe3a33bSHaren Myneni .fault = vas_mmap_fault,
5051fe3a33bSHaren Myneni };
5061fe3a33bSHaren Myneni
coproc_mmap(struct file * fp,struct vm_area_struct * vma)507413d6ed3SHaren Myneni static int coproc_mmap(struct file *fp, struct vm_area_struct *vma)
508413d6ed3SHaren Myneni {
509413d6ed3SHaren Myneni struct coproc_instance *cp_inst = fp->private_data;
510413d6ed3SHaren Myneni struct vas_window *txwin;
511413d6ed3SHaren Myneni unsigned long pfn;
512413d6ed3SHaren Myneni u64 paste_addr;
513413d6ed3SHaren Myneni pgprot_t prot;
514413d6ed3SHaren Myneni int rc;
515413d6ed3SHaren Myneni
516413d6ed3SHaren Myneni txwin = cp_inst->txwin;
517413d6ed3SHaren Myneni
518413d6ed3SHaren Myneni if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
5196d21fb7dSHaren Myneni pr_debug("size 0x%zx, PAGE_SIZE 0x%zx\n",
520413d6ed3SHaren Myneni (vma->vm_end - vma->vm_start), PAGE_SIZE);
521413d6ed3SHaren Myneni return -EINVAL;
522413d6ed3SHaren Myneni }
523413d6ed3SHaren Myneni
524413d6ed3SHaren Myneni /* Ensure instance has an open send window */
525413d6ed3SHaren Myneni if (!txwin) {
5266d21fb7dSHaren Myneni pr_err("No send window open?\n");
527413d6ed3SHaren Myneni return -EINVAL;
528413d6ed3SHaren Myneni }
529413d6ed3SHaren Myneni
53061cb9ac6SGustavo A. R. Silva if (!cp_inst->coproc->vops || !cp_inst->coproc->vops->paste_addr) {
5316d21fb7dSHaren Myneni pr_err("VAS API is not registered\n");
5321a0d0d5eSHaren Myneni return -EACCES;
5331a0d0d5eSHaren Myneni }
5341a0d0d5eSHaren Myneni
5356a8d4ca8SHaren Myneni /*
5366a8d4ca8SHaren Myneni * The initial mmap is done after the window is opened
5376a8d4ca8SHaren Myneni * with ioctl. But before mmap(), this window can be closed in
5386a8d4ca8SHaren Myneni * the hypervisor due to lost credit (core removal on pseries).
5396a8d4ca8SHaren Myneni * So if the window is not active, return mmap() failure with
5406a8d4ca8SHaren Myneni * -EACCES and expects the user space reissue mmap() when it
5416a8d4ca8SHaren Myneni * is active again or open new window when the credit is available.
5426a8d4ca8SHaren Myneni * mmap_mutex protects the paste address mmap() with DLPAR
5436a8d4ca8SHaren Myneni * close/open event and allows mmap() only when the window is
5446a8d4ca8SHaren Myneni * active.
5456a8d4ca8SHaren Myneni */
5466a8d4ca8SHaren Myneni mutex_lock(&txwin->task_ref.mmap_mutex);
5476a8d4ca8SHaren Myneni if (txwin->status != VAS_WIN_ACTIVE) {
5486d21fb7dSHaren Myneni pr_err("Window is not active\n");
5496a8d4ca8SHaren Myneni rc = -EACCES;
5506a8d4ca8SHaren Myneni goto out;
5516a8d4ca8SHaren Myneni }
5526a8d4ca8SHaren Myneni
5531a0d0d5eSHaren Myneni paste_addr = cp_inst->coproc->vops->paste_addr(txwin);
5541a0d0d5eSHaren Myneni if (!paste_addr) {
5556d21fb7dSHaren Myneni pr_err("Window paste address failed\n");
5566a8d4ca8SHaren Myneni rc = -EINVAL;
5576a8d4ca8SHaren Myneni goto out;
5581a0d0d5eSHaren Myneni }
5591a0d0d5eSHaren Myneni
560413d6ed3SHaren Myneni pfn = paste_addr >> PAGE_SHIFT;
561413d6ed3SHaren Myneni
562413d6ed3SHaren Myneni /* flags, page_prot from cxl_mmap(), except we want cachable */
5631c71222eSSuren Baghdasaryan vm_flags_set(vma, VM_IO | VM_PFNMAP);
564413d6ed3SHaren Myneni vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
565413d6ed3SHaren Myneni
566413d6ed3SHaren Myneni prot = __pgprot(pgprot_val(vma->vm_page_prot) | _PAGE_DIRTY);
567413d6ed3SHaren Myneni
568413d6ed3SHaren Myneni rc = remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
569413d6ed3SHaren Myneni vma->vm_end - vma->vm_start, prot);
570413d6ed3SHaren Myneni
5716d21fb7dSHaren Myneni pr_devel("paste addr %llx at %lx, rc %d\n", paste_addr,
5726d21fb7dSHaren Myneni vma->vm_start, rc);
573413d6ed3SHaren Myneni
5741fe3a33bSHaren Myneni txwin->task_ref.vma = vma;
5751fe3a33bSHaren Myneni vma->vm_ops = &vas_vm_ops;
5761fe3a33bSHaren Myneni
5776a8d4ca8SHaren Myneni out:
5786a8d4ca8SHaren Myneni mutex_unlock(&txwin->task_ref.mmap_mutex);
579413d6ed3SHaren Myneni return rc;
580413d6ed3SHaren Myneni }
581413d6ed3SHaren Myneni
coproc_ioctl(struct file * fp,unsigned int cmd,unsigned long arg)582413d6ed3SHaren Myneni static long coproc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
583413d6ed3SHaren Myneni {
584413d6ed3SHaren Myneni switch (cmd) {
585413d6ed3SHaren Myneni case VAS_TX_WIN_OPEN:
586413d6ed3SHaren Myneni return coproc_ioc_tx_win_open(fp, arg);
587413d6ed3SHaren Myneni default:
588413d6ed3SHaren Myneni return -EINVAL;
589413d6ed3SHaren Myneni }
590413d6ed3SHaren Myneni }
591413d6ed3SHaren Myneni
592413d6ed3SHaren Myneni static struct file_operations coproc_fops = {
593413d6ed3SHaren Myneni .open = coproc_open,
594413d6ed3SHaren Myneni .release = coproc_release,
595413d6ed3SHaren Myneni .mmap = coproc_mmap,
596413d6ed3SHaren Myneni .unlocked_ioctl = coproc_ioctl,
597413d6ed3SHaren Myneni };
598413d6ed3SHaren Myneni
599413d6ed3SHaren Myneni /*
600413d6ed3SHaren Myneni * Supporting only nx-gzip coprocessor type now, but this API code
601413d6ed3SHaren Myneni * extended to other coprocessor types later.
602413d6ed3SHaren Myneni */
vas_register_coproc_api(struct module * mod,enum vas_cop_type cop_type,const char * name,const struct vas_user_win_ops * vops)603413d6ed3SHaren Myneni int vas_register_coproc_api(struct module *mod, enum vas_cop_type cop_type,
6041a0d0d5eSHaren Myneni const char *name,
6051a0d0d5eSHaren Myneni const struct vas_user_win_ops *vops)
606413d6ed3SHaren Myneni {
607413d6ed3SHaren Myneni int rc = -EINVAL;
608413d6ed3SHaren Myneni dev_t devno;
609413d6ed3SHaren Myneni
610413d6ed3SHaren Myneni rc = alloc_chrdev_region(&coproc_device.devt, 1, 1, name);
611413d6ed3SHaren Myneni if (rc) {
612413d6ed3SHaren Myneni pr_err("Unable to allocate coproc major number: %i\n", rc);
613413d6ed3SHaren Myneni return rc;
614413d6ed3SHaren Myneni }
615413d6ed3SHaren Myneni
616413d6ed3SHaren Myneni pr_devel("%s device allocated, dev [%i,%i]\n", name,
617413d6ed3SHaren Myneni MAJOR(coproc_device.devt), MINOR(coproc_device.devt));
618413d6ed3SHaren Myneni
6191aaba11dSGreg Kroah-Hartman coproc_device.class = class_create(name);
620413d6ed3SHaren Myneni if (IS_ERR(coproc_device.class)) {
621413d6ed3SHaren Myneni rc = PTR_ERR(coproc_device.class);
622413d6ed3SHaren Myneni pr_err("Unable to create %s class %d\n", name, rc);
623413d6ed3SHaren Myneni goto err_class;
624413d6ed3SHaren Myneni }
625413d6ed3SHaren Myneni coproc_device.class->devnode = coproc_devnode;
626413d6ed3SHaren Myneni coproc_device.cop_type = cop_type;
6271a0d0d5eSHaren Myneni coproc_device.vops = vops;
628413d6ed3SHaren Myneni
629413d6ed3SHaren Myneni coproc_fops.owner = mod;
630413d6ed3SHaren Myneni cdev_init(&coproc_device.cdev, &coproc_fops);
631413d6ed3SHaren Myneni
632413d6ed3SHaren Myneni devno = MKDEV(MAJOR(coproc_device.devt), 0);
633413d6ed3SHaren Myneni rc = cdev_add(&coproc_device.cdev, devno, 1);
634413d6ed3SHaren Myneni if (rc) {
635413d6ed3SHaren Myneni pr_err("cdev_add() failed %d\n", rc);
636413d6ed3SHaren Myneni goto err_cdev;
637413d6ed3SHaren Myneni }
638413d6ed3SHaren Myneni
639413d6ed3SHaren Myneni coproc_device.device = device_create(coproc_device.class, NULL,
640413d6ed3SHaren Myneni devno, NULL, name, MINOR(devno));
641413d6ed3SHaren Myneni if (IS_ERR(coproc_device.device)) {
642413d6ed3SHaren Myneni rc = PTR_ERR(coproc_device.device);
643413d6ed3SHaren Myneni pr_err("Unable to create coproc-%d %d\n", MINOR(devno), rc);
644413d6ed3SHaren Myneni goto err;
645413d6ed3SHaren Myneni }
646413d6ed3SHaren Myneni
6476d21fb7dSHaren Myneni pr_devel("Added dev [%d,%d]\n", MAJOR(devno), MINOR(devno));
648413d6ed3SHaren Myneni
649413d6ed3SHaren Myneni return 0;
650413d6ed3SHaren Myneni
651413d6ed3SHaren Myneni err:
652413d6ed3SHaren Myneni cdev_del(&coproc_device.cdev);
653413d6ed3SHaren Myneni err_cdev:
654413d6ed3SHaren Myneni class_destroy(coproc_device.class);
655413d6ed3SHaren Myneni err_class:
656413d6ed3SHaren Myneni unregister_chrdev_region(coproc_device.devt, 1);
657413d6ed3SHaren Myneni return rc;
658413d6ed3SHaren Myneni }
659413d6ed3SHaren Myneni
vas_unregister_coproc_api(void)660413d6ed3SHaren Myneni void vas_unregister_coproc_api(void)
661413d6ed3SHaren Myneni {
662413d6ed3SHaren Myneni dev_t devno;
663413d6ed3SHaren Myneni
664413d6ed3SHaren Myneni cdev_del(&coproc_device.cdev);
665413d6ed3SHaren Myneni devno = MKDEV(MAJOR(coproc_device.devt), 0);
666413d6ed3SHaren Myneni device_destroy(coproc_device.class, devno);
667413d6ed3SHaren Myneni
668413d6ed3SHaren Myneni class_destroy(coproc_device.class);
669413d6ed3SHaren Myneni unregister_chrdev_region(coproc_device.devt, 1);
670413d6ed3SHaren Myneni }
671