1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * VAS user space API for its accelerators (Only NX-GZIP is supported now)
4 * Copyright (C) 2019 Haren Myneni, IBM Corp
7 #include <linux/kernel.h>
8 #include <linux/device.h>
9 #include <linux/cdev.h>
11 #include <linux/slab.h>
12 #include <linux/uaccess.h>
13 #include <linux/kthread.h>
14 #include <linux/sched/signal.h>
15 #include <linux/mmu_context.h>
18 #include <uapi/asm/vas-api.h>
21 * The driver creates the device node that can be used as follows:
24 * fd = open("/dev/crypto/nx-gzip", O_RDWR);
25 * rc = ioctl(fd, VAS_TX_WIN_OPEN, &attr);
26 * paste_addr = mmap(NULL, PAGE_SIZE, prot, MAP_SHARED, fd, 0ULL).
27 * vas_copy(&crb, 0, 1);
28 * vas_paste(paste_addr, 0, 1);
29 * close(fd) or exit process to close window.
31 * where "vas_copy" and "vas_paste" are defined in copy-paste.h.
32 * copy/paste returns to the user space directly. So refer NX hardware
33 * documententation for exact copy/paste usage and completion / error
38 * Wrapper object for the nx-gzip device - there is just one instance of
39 * this node for the whole system.
41 static struct coproc_dev {
43 struct device *device;
47 enum vas_cop_type cop_type;
48 const struct vas_user_win_ops *vops;
51 struct coproc_instance {
52 struct coproc_dev *coproc;
53 struct vas_window *txwin;
56 static char *coproc_devnode(struct device *dev, umode_t *mode)
58 return kasprintf(GFP_KERNEL, "crypto/%s", dev_name(dev));
62 * Take reference to pid and mm
64 int get_vas_user_win_ref(struct vas_user_win_ref *task_ref)
67 * Window opened by a child thread may not be closed when
68 * it exits. So take reference to its pid and release it
69 * when the window is free by parent thread.
70 * Acquire a reference to the task's pid to make sure
71 * pid will not be re-used - needed only for multithread
74 task_ref->pid = get_task_pid(current, PIDTYPE_PID);
76 * Acquire a reference to the task's mm.
78 task_ref->mm = get_task_mm(current);
80 put_pid(task_ref->pid);
81 pr_err("VAS: pid(%d): mm_struct is not found\n",
89 * Process closes window during exit. In the case of
90 * multithread application, the child thread can open
91 * window and can exit without closing it. So takes tgid
92 * reference until window closed to make sure tgid is not
95 task_ref->tgid = find_get_pid(task_tgid_vnr(current));
101 * Successful return must release the task reference with
104 static bool ref_get_pid_and_task(struct vas_user_win_ref *task_ref,
105 struct task_struct **tskp, struct pid **pidp)
107 struct task_struct *tsk;
111 tsk = get_pid_task(pid, PIDTYPE_PID);
113 pid = task_ref->tgid;
114 tsk = get_pid_task(pid, PIDTYPE_PID);
116 * Parent thread (tgid) will be closing window when it
117 * exits. So should not get here.
119 if (WARN_ON_ONCE(!tsk))
123 /* Return if the task is exiting. */
124 if (tsk->flags & PF_EXITING) {
125 put_task_struct(tsk);
136 * Update the CSB to indicate a translation error.
138 * User space will be polling on CSB after the request is issued.
139 * If NX can handle the request without any issues, it updates CSB.
140 * Whereas if NX encounters page fault, the kernel will handle the
141 * fault and update CSB with translation error.
143 * If we are unable to update the CSB means copy_to_user failed due to
144 * invalid csb_addr, send a signal to the process.
146 void vas_update_csb(struct coprocessor_request_block *crb,
147 struct vas_user_win_ref *task_ref)
149 struct coprocessor_status_block csb;
150 struct kernel_siginfo info;
151 struct task_struct *tsk;
152 void __user *csb_addr;
157 * NX user space windows can not be opened for task->mm=NULL
158 * and faults will not be generated for kernel requests.
160 if (WARN_ON_ONCE(!task_ref->mm))
163 csb_addr = (void __user *)be64_to_cpu(crb->csb_addr);
165 memset(&csb, 0, sizeof(csb));
166 csb.cc = CSB_CC_FAULT_ADDRESS;
167 csb.ce = CSB_CE_TERMINATION;
172 * NX operates and returns in BE format as defined CRB struct.
173 * So saves fault_storage_addr in BE as NX pastes in FIFO and
174 * expects user space to convert to CPU format.
176 csb.address = crb->stamp.nx.fault_storage_addr;
180 * Process closes send window after all pending NX requests are
181 * completed. In multi-thread applications, a child thread can
182 * open a window and can exit without closing it. May be some
183 * requests are pending or this window can be used by other
184 * threads later. We should handle faults if NX encounters
185 * pages faults on these requests. Update CSB with translation
186 * error and fault address. If csb_addr passed by user space is
187 * invalid, send SEGV signal to pid saved in window. If the
188 * child thread is not running, send the signal to tgid.
189 * Parent thread (tgid) will close this window upon its exit.
191 * pid and mm references are taken when window is opened by
192 * process (pid). So tgid is used only when child thread opens
193 * a window and exits without closing it.
196 if (!ref_get_pid_and_task(task_ref, &tsk, &pid))
199 kthread_use_mm(task_ref->mm);
200 rc = copy_to_user(csb_addr, &csb, sizeof(csb));
202 * User space polls on csb.flags (first byte). So add barrier
203 * then copy first byte with csb flags update.
207 /* Make sure update to csb.flags is visible now */
209 rc = copy_to_user(csb_addr, &csb, sizeof(u8));
211 kthread_unuse_mm(task_ref->mm);
212 put_task_struct(tsk);
219 pr_debug("Invalid CSB address 0x%p signalling pid(%d)\n",
220 csb_addr, pid_vnr(pid));
222 clear_siginfo(&info);
223 info.si_signo = SIGSEGV;
224 info.si_errno = EFAULT;
225 info.si_code = SEGV_MAPERR;
226 info.si_addr = csb_addr;
228 * process will be polling on csb.flags after request is sent to
229 * NX. So generally CSB update should not fail except when an
230 * application passes invalid csb_addr. So an error message will
231 * be displayed and leave it to user space whether to ignore or
232 * handle this signal.
235 rc = kill_pid_info(SIGSEGV, &info, pid);
238 pr_devel("%s(): pid %d kill_proc_info() rc %d\n", __func__,
242 void vas_dump_crb(struct coprocessor_request_block *crb)
244 struct data_descriptor_entry *dde;
245 struct nx_fault_stamp *nx;
248 pr_devel("SrcDDE: addr 0x%llx, len %d, count %d, idx %d, flags %d\n",
249 be64_to_cpu(dde->address), be32_to_cpu(dde->length),
250 dde->count, dde->index, dde->flags);
253 pr_devel("TgtDDE: addr 0x%llx, len %d, count %d, idx %d, flags %d\n",
254 be64_to_cpu(dde->address), be32_to_cpu(dde->length),
255 dde->count, dde->index, dde->flags);
258 pr_devel("NX Stamp: PSWID 0x%x, FSA 0x%llx, flags 0x%x, FS 0x%x\n",
259 be32_to_cpu(nx->pswid),
260 be64_to_cpu(crb->stamp.nx.fault_storage_addr),
261 nx->flags, nx->fault_status);
264 static int coproc_open(struct inode *inode, struct file *fp)
266 struct coproc_instance *cp_inst;
268 cp_inst = kzalloc(sizeof(*cp_inst), GFP_KERNEL);
272 cp_inst->coproc = container_of(inode->i_cdev, struct coproc_dev,
274 fp->private_data = cp_inst;
279 static int coproc_ioc_tx_win_open(struct file *fp, unsigned long arg)
281 void __user *uptr = (void __user *)arg;
282 struct vas_tx_win_open_attr uattr;
283 struct coproc_instance *cp_inst;
284 struct vas_window *txwin;
287 cp_inst = fp->private_data;
290 * One window for file descriptor
295 rc = copy_from_user(&uattr, uptr, sizeof(uattr));
297 pr_err("%s(): copy_from_user() returns %d\n", __func__, rc);
301 if (uattr.version != 1) {
302 pr_err("Invalid window open API version\n");
306 if (!cp_inst->coproc->vops || !cp_inst->coproc->vops->open_win) {
307 pr_err("VAS API is not registered\n");
311 txwin = cp_inst->coproc->vops->open_win(uattr.vas_id, uattr.flags,
312 cp_inst->coproc->cop_type);
314 pr_err("%s() VAS window open failed, %ld\n", __func__,
316 return PTR_ERR(txwin);
319 cp_inst->txwin = txwin;
324 static int coproc_release(struct inode *inode, struct file *fp)
326 struct coproc_instance *cp_inst = fp->private_data;
329 if (cp_inst->txwin) {
330 if (cp_inst->coproc->vops &&
331 cp_inst->coproc->vops->close_win) {
332 rc = cp_inst->coproc->vops->close_win(cp_inst->txwin);
336 cp_inst->txwin = NULL;
340 fp->private_data = NULL;
343 * We don't know here if user has other receive windows
344 * open, so we can't really call clear_thread_tidr().
345 * So, once the process calls set_thread_tidr(), the
346 * TIDR value sticks around until process exits, resulting
347 * in an extra copy in restore_sprs().
353 static int coproc_mmap(struct file *fp, struct vm_area_struct *vma)
355 struct coproc_instance *cp_inst = fp->private_data;
356 struct vas_window *txwin;
362 txwin = cp_inst->txwin;
364 if ((vma->vm_end - vma->vm_start) > PAGE_SIZE) {
365 pr_debug("%s(): size 0x%zx, PAGE_SIZE 0x%zx\n", __func__,
366 (vma->vm_end - vma->vm_start), PAGE_SIZE);
370 /* Ensure instance has an open send window */
372 pr_err("%s(): No send window open?\n", __func__);
376 if (!cp_inst->coproc->vops || !cp_inst->coproc->vops->paste_addr) {
377 pr_err("%s(): VAS API is not registered\n", __func__);
381 paste_addr = cp_inst->coproc->vops->paste_addr(txwin);
383 pr_err("%s(): Window paste address failed\n", __func__);
387 pfn = paste_addr >> PAGE_SHIFT;
389 /* flags, page_prot from cxl_mmap(), except we want cachable */
390 vma->vm_flags |= VM_IO | VM_PFNMAP;
391 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
393 prot = __pgprot(pgprot_val(vma->vm_page_prot) | _PAGE_DIRTY);
395 rc = remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
396 vma->vm_end - vma->vm_start, prot);
398 pr_devel("%s(): paste addr %llx at %lx, rc %d\n", __func__,
399 paste_addr, vma->vm_start, rc);
404 static long coproc_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
407 case VAS_TX_WIN_OPEN:
408 return coproc_ioc_tx_win_open(fp, arg);
414 static struct file_operations coproc_fops = {
416 .release = coproc_release,
418 .unlocked_ioctl = coproc_ioctl,
422 * Supporting only nx-gzip coprocessor type now, but this API code
423 * extended to other coprocessor types later.
425 int vas_register_coproc_api(struct module *mod, enum vas_cop_type cop_type,
427 const struct vas_user_win_ops *vops)
432 rc = alloc_chrdev_region(&coproc_device.devt, 1, 1, name);
434 pr_err("Unable to allocate coproc major number: %i\n", rc);
438 pr_devel("%s device allocated, dev [%i,%i]\n", name,
439 MAJOR(coproc_device.devt), MINOR(coproc_device.devt));
441 coproc_device.class = class_create(mod, name);
442 if (IS_ERR(coproc_device.class)) {
443 rc = PTR_ERR(coproc_device.class);
444 pr_err("Unable to create %s class %d\n", name, rc);
447 coproc_device.class->devnode = coproc_devnode;
448 coproc_device.cop_type = cop_type;
449 coproc_device.vops = vops;
451 coproc_fops.owner = mod;
452 cdev_init(&coproc_device.cdev, &coproc_fops);
454 devno = MKDEV(MAJOR(coproc_device.devt), 0);
455 rc = cdev_add(&coproc_device.cdev, devno, 1);
457 pr_err("cdev_add() failed %d\n", rc);
461 coproc_device.device = device_create(coproc_device.class, NULL,
462 devno, NULL, name, MINOR(devno));
463 if (IS_ERR(coproc_device.device)) {
464 rc = PTR_ERR(coproc_device.device);
465 pr_err("Unable to create coproc-%d %d\n", MINOR(devno), rc);
469 pr_devel("%s: Added dev [%d,%d]\n", __func__, MAJOR(devno),
475 cdev_del(&coproc_device.cdev);
477 class_destroy(coproc_device.class);
479 unregister_chrdev_region(coproc_device.devt, 1);
483 void vas_unregister_coproc_api(void)
487 cdev_del(&coproc_device.cdev);
488 devno = MKDEV(MAJOR(coproc_device.devt), 0);
489 device_destroy(coproc_device.class, devno);
491 class_destroy(coproc_device.class);
492 unregister_chrdev_region(coproc_device.devt, 1);