d0b421f49b3976180be65c5beb4c60d17c0e32fb
[cascardo/linux.git] / drivers / misc / cxl / file.c
1 /*
2  * Copyright 2014 IBM Corp.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version
7  * 2 of the License, or (at your option) any later version.
8  */
9
10 #include <linux/spinlock.h>
11 #include <linux/module.h>
12 #include <linux/export.h>
13 #include <linux/kernel.h>
14 #include <linux/bitmap.h>
15 #include <linux/sched.h>
16 #include <linux/poll.h>
17 #include <linux/pid.h>
18 #include <linux/fs.h>
19 #include <linux/mm.h>
20 #include <linux/slab.h>
21 #include <asm/cputable.h>
22 #include <asm/current.h>
23 #include <asm/copro.h>
24
25 #include "cxl.h"
26 #include "trace.h"
27
28 #define CXL_NUM_MINORS 256 /* Total to reserve */
29
30 #define CXL_AFU_MINOR_D(afu) (CXL_CARD_MINOR(afu->adapter) + 1 + (3 * afu->slice))
31 #define CXL_AFU_MINOR_M(afu) (CXL_AFU_MINOR_D(afu) + 1)
32 #define CXL_AFU_MINOR_S(afu) (CXL_AFU_MINOR_D(afu) + 2)
33 #define CXL_AFU_MKDEV_D(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_D(afu))
34 #define CXL_AFU_MKDEV_M(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_M(afu))
35 #define CXL_AFU_MKDEV_S(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_S(afu))
36
37 #define CXL_DEVT_AFU(dev) ((MINOR(dev) % CXL_DEV_MINORS - 1) / 3)
38
39 #define CXL_DEVT_IS_CARD(dev) (MINOR(dev) % CXL_DEV_MINORS == 0)
40
41 static dev_t cxl_dev;
42
43 static struct class *cxl_class;
44
45 static int __afu_open(struct inode *inode, struct file *file, bool master)
46 {
47         struct cxl *adapter;
48         struct cxl_afu *afu;
49         struct cxl_context *ctx;
50         int adapter_num = CXL_DEVT_ADAPTER(inode->i_rdev);
51         int slice = CXL_DEVT_AFU(inode->i_rdev);
52         int rc = -ENODEV;
53
54         pr_devel("afu_open afu%i.%i\n", slice, adapter_num);
55
56         if (!(adapter = get_cxl_adapter(adapter_num)))
57                 return -ENODEV;
58
59         if (slice > adapter->slices)
60                 goto err_put_adapter;
61
62         spin_lock(&adapter->afu_list_lock);
63         if (!(afu = adapter->afu[slice])) {
64                 spin_unlock(&adapter->afu_list_lock);
65                 goto err_put_adapter;
66         }
67
68         /*
69          * taking a ref to the afu so that it doesn't go away
70          * for rest of the function. This ref is released before
71          * we return.
72          */
73         cxl_afu_get(afu);
74         spin_unlock(&adapter->afu_list_lock);
75
76         if (!afu->current_mode)
77                 goto err_put_afu;
78
79         if (!cxl_ops->link_ok(adapter, afu)) {
80                 rc = -EIO;
81                 goto err_put_afu;
82         }
83
84         if (!(ctx = cxl_context_alloc())) {
85                 rc = -ENOMEM;
86                 goto err_put_afu;
87         }
88
89         if ((rc = cxl_context_init(ctx, afu, master, inode->i_mapping)))
90                 goto err_put_afu;
91
92         pr_devel("afu_open pe: %i\n", ctx->pe);
93         file->private_data = ctx;
94         cxl_ctx_get();
95
96         /* indicate success */
97         rc = 0;
98
99 err_put_afu:
100         /* release the ref taken earlier */
101         cxl_afu_put(afu);
102 err_put_adapter:
103         put_device(&adapter->dev);
104         return rc;
105 }
106
107 int afu_open(struct inode *inode, struct file *file)
108 {
109         return __afu_open(inode, file, false);
110 }
111
112 static int afu_master_open(struct inode *inode, struct file *file)
113 {
114         return __afu_open(inode, file, true);
115 }
116
117 int afu_release(struct inode *inode, struct file *file)
118 {
119         struct cxl_context *ctx = file->private_data;
120
121         pr_devel("%s: closing cxl file descriptor. pe: %i\n",
122                  __func__, ctx->pe);
123         cxl_context_detach(ctx);
124
125
126         /*
127          * Delete the context's mapping pointer, unless it's created by the
128          * kernel API, in which case leave it so it can be freed by reclaim_ctx()
129          */
130         if (!ctx->kernelapi) {
131                 mutex_lock(&ctx->mapping_lock);
132                 ctx->mapping = NULL;
133                 mutex_unlock(&ctx->mapping_lock);
134         }
135
136         /*
137          * At this this point all bottom halfs have finished and we should be
138          * getting no more IRQs from the hardware for this context.  Once it's
139          * removed from the IDR (and RCU synchronised) it's safe to free the
140          * sstp and context.
141          */
142         cxl_context_free(ctx);
143
144         return 0;
145 }
146
147 static long afu_ioctl_start_work(struct cxl_context *ctx,
148                                  struct cxl_ioctl_start_work __user *uwork)
149 {
150         struct cxl_ioctl_start_work work;
151         u64 amr = 0;
152         int rc;
153
154         pr_devel("%s: pe: %i\n", __func__, ctx->pe);
155
156         /* Do this outside the status_mutex to avoid a circular dependency with
157          * the locking in cxl_mmap_fault() */
158         if (copy_from_user(&work, uwork,
159                            sizeof(struct cxl_ioctl_start_work))) {
160                 rc = -EFAULT;
161                 goto out;
162         }
163
164         mutex_lock(&ctx->status_mutex);
165         if (ctx->status != OPENED) {
166                 rc = -EIO;
167                 goto out;
168         }
169
170         /*
171          * if any of the reserved fields are set or any of the unused
172          * flags are set it's invalid
173          */
174         if (work.reserved1 || work.reserved2 || work.reserved3 ||
175             work.reserved4 || work.reserved5 || work.reserved6 ||
176             (work.flags & ~CXL_START_WORK_ALL)) {
177                 rc = -EINVAL;
178                 goto out;
179         }
180
181         if (!(work.flags & CXL_START_WORK_NUM_IRQS))
182                 work.num_interrupts = ctx->afu->pp_irqs;
183         else if ((work.num_interrupts < ctx->afu->pp_irqs) ||
184                  (work.num_interrupts > ctx->afu->irqs_max)) {
185                 rc =  -EINVAL;
186                 goto out;
187         }
188         if ((rc = afu_register_irqs(ctx, work.num_interrupts)))
189                 goto out;
190
191         if (work.flags & CXL_START_WORK_AMR)
192                 amr = work.amr & mfspr(SPRN_UAMOR);
193
194         ctx->mmio_err_ff = !!(work.flags & CXL_START_WORK_ERR_FF);
195
196         /*
197          * We grab the PID here and not in the file open to allow for the case
198          * where a process (master, some daemon, etc) has opened the chardev on
199          * behalf of another process, so the AFU's mm gets bound to the process
200          * that performs this ioctl and not the process that opened the file.
201          * Also we grab the PID of the group leader so that if the task that
202          * has performed the attach operation exits the mm context of the
203          * process is still accessible.
204          */
205         ctx->pid = get_task_pid(current, PIDTYPE_PID);
206         ctx->glpid = get_task_pid(current->group_leader, PIDTYPE_PID);
207
208         /*
209          * Increment the mapped context count for adapter. This also checks
210          * if adapter_context_lock is taken.
211          */
212         rc = cxl_adapter_context_get(ctx->afu->adapter);
213         if (rc) {
214                 afu_release_irqs(ctx, ctx);
215                 goto out;
216         }
217
218         trace_cxl_attach(ctx, work.work_element_descriptor, work.num_interrupts, amr);
219
220         if ((rc = cxl_ops->attach_process(ctx, false, work.work_element_descriptor,
221                                                         amr))) {
222                 afu_release_irqs(ctx, ctx);
223                 cxl_adapter_context_put(ctx->afu->adapter);
224                 goto out;
225         }
226
227         ctx->status = STARTED;
228         rc = 0;
229 out:
230         mutex_unlock(&ctx->status_mutex);
231         return rc;
232 }
233
234 static long afu_ioctl_process_element(struct cxl_context *ctx,
235                                       int __user *upe)
236 {
237         pr_devel("%s: pe: %i\n", __func__, ctx->pe);
238
239         if (copy_to_user(upe, &ctx->external_pe, sizeof(__u32)))
240                 return -EFAULT;
241
242         return 0;
243 }
244
245 static long afu_ioctl_get_afu_id(struct cxl_context *ctx,
246                                  struct cxl_afu_id __user *upafuid)
247 {
248         struct cxl_afu_id afuid = { 0 };
249
250         afuid.card_id = ctx->afu->adapter->adapter_num;
251         afuid.afu_offset = ctx->afu->slice;
252         afuid.afu_mode = ctx->afu->current_mode;
253
254         /* set the flag bit in case the afu is a slave */
255         if (ctx->afu->current_mode == CXL_MODE_DIRECTED && !ctx->master)
256                 afuid.flags |= CXL_AFUID_FLAG_SLAVE;
257
258         if (copy_to_user(upafuid, &afuid, sizeof(afuid)))
259                 return -EFAULT;
260
261         return 0;
262 }
263
264 long afu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
265 {
266         struct cxl_context *ctx = file->private_data;
267
268         if (ctx->status == CLOSED)
269                 return -EIO;
270
271         if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
272                 return -EIO;
273
274         pr_devel("afu_ioctl\n");
275         switch (cmd) {
276         case CXL_IOCTL_START_WORK:
277                 return afu_ioctl_start_work(ctx, (struct cxl_ioctl_start_work __user *)arg);
278         case CXL_IOCTL_GET_PROCESS_ELEMENT:
279                 return afu_ioctl_process_element(ctx, (__u32 __user *)arg);
280         case CXL_IOCTL_GET_AFU_ID:
281                 return afu_ioctl_get_afu_id(ctx, (struct cxl_afu_id __user *)
282                                             arg);
283         }
284         return -EINVAL;
285 }
286
287 static long afu_compat_ioctl(struct file *file, unsigned int cmd,
288                              unsigned long arg)
289 {
290         return afu_ioctl(file, cmd, arg);
291 }
292
293 int afu_mmap(struct file *file, struct vm_area_struct *vm)
294 {
295         struct cxl_context *ctx = file->private_data;
296
297         /* AFU must be started before we can MMIO */
298         if (ctx->status != STARTED)
299                 return -EIO;
300
301         if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
302                 return -EIO;
303
304         return cxl_context_iomap(ctx, vm);
305 }
306
307 static inline bool ctx_event_pending(struct cxl_context *ctx)
308 {
309         if (ctx->pending_irq || ctx->pending_fault || ctx->pending_afu_err)
310                 return true;
311
312         if (ctx->afu_driver_ops && atomic_read(&ctx->afu_driver_events))
313                 return true;
314
315         return false;
316 }
317
318 unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
319 {
320         struct cxl_context *ctx = file->private_data;
321         int mask = 0;
322         unsigned long flags;
323
324
325         poll_wait(file, &ctx->wq, poll);
326
327         pr_devel("afu_poll wait done pe: %i\n", ctx->pe);
328
329         spin_lock_irqsave(&ctx->lock, flags);
330         if (ctx_event_pending(ctx))
331                 mask |= POLLIN | POLLRDNORM;
332         else if (ctx->status == CLOSED)
333                 /* Only error on closed when there are no futher events pending
334                  */
335                 mask |= POLLERR;
336         spin_unlock_irqrestore(&ctx->lock, flags);
337
338         pr_devel("afu_poll pe: %i returning %#x\n", ctx->pe, mask);
339
340         return mask;
341 }
342
343 static ssize_t afu_driver_event_copy(struct cxl_context *ctx,
344                                      char __user *buf,
345                                      struct cxl_event *event,
346                                      struct cxl_event_afu_driver_reserved *pl)
347 {
348         /* Check event */
349         if (!pl) {
350                 ctx->afu_driver_ops->event_delivered(ctx, pl, -EINVAL);
351                 return -EFAULT;
352         }
353
354         /* Check event size */
355         event->header.size += pl->data_size;
356         if (event->header.size > CXL_READ_MIN_SIZE) {
357                 ctx->afu_driver_ops->event_delivered(ctx, pl, -EINVAL);
358                 return -EFAULT;
359         }
360
361         /* Copy event header */
362         if (copy_to_user(buf, event, sizeof(struct cxl_event_header))) {
363                 ctx->afu_driver_ops->event_delivered(ctx, pl, -EFAULT);
364                 return -EFAULT;
365         }
366
367         /* Copy event data */
368         buf += sizeof(struct cxl_event_header);
369         if (copy_to_user(buf, &pl->data, pl->data_size)) {
370                 ctx->afu_driver_ops->event_delivered(ctx, pl, -EFAULT);
371                 return -EFAULT;
372         }
373
374         ctx->afu_driver_ops->event_delivered(ctx, pl, 0); /* Success */
375         return event->header.size;
376 }
377
378 ssize_t afu_read(struct file *file, char __user *buf, size_t count,
379                         loff_t *off)
380 {
381         struct cxl_context *ctx = file->private_data;
382         struct cxl_event_afu_driver_reserved *pl = NULL;
383         struct cxl_event event;
384         unsigned long flags;
385         int rc;
386         DEFINE_WAIT(wait);
387
388         if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
389                 return -EIO;
390
391         if (count < CXL_READ_MIN_SIZE)
392                 return -EINVAL;
393
394         spin_lock_irqsave(&ctx->lock, flags);
395
396         for (;;) {
397                 prepare_to_wait(&ctx->wq, &wait, TASK_INTERRUPTIBLE);
398                 if (ctx_event_pending(ctx) || (ctx->status == CLOSED))
399                         break;
400
401                 if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
402                         rc = -EIO;
403                         goto out;
404                 }
405
406                 if (file->f_flags & O_NONBLOCK) {
407                         rc = -EAGAIN;
408                         goto out;
409                 }
410
411                 if (signal_pending(current)) {
412                         rc = -ERESTARTSYS;
413                         goto out;
414                 }
415
416                 spin_unlock_irqrestore(&ctx->lock, flags);
417                 pr_devel("afu_read going to sleep...\n");
418                 schedule();
419                 pr_devel("afu_read woken up\n");
420                 spin_lock_irqsave(&ctx->lock, flags);
421         }
422
423         finish_wait(&ctx->wq, &wait);
424
425         memset(&event, 0, sizeof(event));
426         event.header.process_element = ctx->pe;
427         event.header.size = sizeof(struct cxl_event_header);
428         if (ctx->afu_driver_ops && atomic_read(&ctx->afu_driver_events)) {
429                 pr_devel("afu_read delivering AFU driver specific event\n");
430                 pl = ctx->afu_driver_ops->fetch_event(ctx);
431                 atomic_dec(&ctx->afu_driver_events);
432                 event.header.type = CXL_EVENT_AFU_DRIVER;
433         } else if (ctx->pending_irq) {
434                 pr_devel("afu_read delivering AFU interrupt\n");
435                 event.header.size += sizeof(struct cxl_event_afu_interrupt);
436                 event.header.type = CXL_EVENT_AFU_INTERRUPT;
437                 event.irq.irq = find_first_bit(ctx->irq_bitmap, ctx->irq_count) + 1;
438                 clear_bit(event.irq.irq - 1, ctx->irq_bitmap);
439                 if (bitmap_empty(ctx->irq_bitmap, ctx->irq_count))
440                         ctx->pending_irq = false;
441         } else if (ctx->pending_fault) {
442                 pr_devel("afu_read delivering data storage fault\n");
443                 event.header.size += sizeof(struct cxl_event_data_storage);
444                 event.header.type = CXL_EVENT_DATA_STORAGE;
445                 event.fault.addr = ctx->fault_addr;
446                 event.fault.dsisr = ctx->fault_dsisr;
447                 ctx->pending_fault = false;
448         } else if (ctx->pending_afu_err) {
449                 pr_devel("afu_read delivering afu error\n");
450                 event.header.size += sizeof(struct cxl_event_afu_error);
451                 event.header.type = CXL_EVENT_AFU_ERROR;
452                 event.afu_error.error = ctx->afu_err;
453                 ctx->pending_afu_err = false;
454         } else if (ctx->status == CLOSED) {
455                 pr_devel("afu_read fatal error\n");
456                 spin_unlock_irqrestore(&ctx->lock, flags);
457                 return -EIO;
458         } else
459                 WARN(1, "afu_read must be buggy\n");
460
461         spin_unlock_irqrestore(&ctx->lock, flags);
462
463         if (event.header.type == CXL_EVENT_AFU_DRIVER)
464                 return afu_driver_event_copy(ctx, buf, &event, pl);
465
466         if (copy_to_user(buf, &event, event.header.size))
467                 return -EFAULT;
468         return event.header.size;
469
470 out:
471         finish_wait(&ctx->wq, &wait);
472         spin_unlock_irqrestore(&ctx->lock, flags);
473         return rc;
474 }
475
476 /* 
477  * Note: if this is updated, we need to update api.c to patch the new ones in
478  * too
479  */
480 const struct file_operations afu_fops = {
481         .owner          = THIS_MODULE,
482         .open           = afu_open,
483         .poll           = afu_poll,
484         .read           = afu_read,
485         .release        = afu_release,
486         .unlocked_ioctl = afu_ioctl,
487         .compat_ioctl   = afu_compat_ioctl,
488         .mmap           = afu_mmap,
489 };
490
491 static const struct file_operations afu_master_fops = {
492         .owner          = THIS_MODULE,
493         .open           = afu_master_open,
494         .poll           = afu_poll,
495         .read           = afu_read,
496         .release        = afu_release,
497         .unlocked_ioctl = afu_ioctl,
498         .compat_ioctl   = afu_compat_ioctl,
499         .mmap           = afu_mmap,
500 };
501
502
503 static char *cxl_devnode(struct device *dev, umode_t *mode)
504 {
505         if (cpu_has_feature(CPU_FTR_HVMODE) &&
506             CXL_DEVT_IS_CARD(dev->devt)) {
507                 /*
508                  * These minor numbers will eventually be used to program the
509                  * PSL and AFUs once we have dynamic reprogramming support
510                  */
511                 return NULL;
512         }
513         return kasprintf(GFP_KERNEL, "cxl/%s", dev_name(dev));
514 }
515
516 extern struct class *cxl_class;
517
518 static int cxl_add_chardev(struct cxl_afu *afu, dev_t devt, struct cdev *cdev,
519                            struct device **chardev, char *postfix, char *desc,
520                            const struct file_operations *fops)
521 {
522         struct device *dev;
523         int rc;
524
525         cdev_init(cdev, fops);
526         if ((rc = cdev_add(cdev, devt, 1))) {
527                 dev_err(&afu->dev, "Unable to add %s chardev: %i\n", desc, rc);
528                 return rc;
529         }
530
531         dev = device_create(cxl_class, &afu->dev, devt, afu,
532                         "afu%i.%i%s", afu->adapter->adapter_num, afu->slice, postfix);
533         if (IS_ERR(dev)) {
534                 dev_err(&afu->dev, "Unable to create %s chardev in sysfs: %i\n", desc, rc);
535                 rc = PTR_ERR(dev);
536                 goto err;
537         }
538
539         *chardev = dev;
540
541         return 0;
542 err:
543         cdev_del(cdev);
544         return rc;
545 }
546
547 int cxl_chardev_d_afu_add(struct cxl_afu *afu)
548 {
549         return cxl_add_chardev(afu, CXL_AFU_MKDEV_D(afu), &afu->afu_cdev_d,
550                                &afu->chardev_d, "d", "dedicated",
551                                &afu_master_fops); /* Uses master fops */
552 }
553
554 int cxl_chardev_m_afu_add(struct cxl_afu *afu)
555 {
556         return cxl_add_chardev(afu, CXL_AFU_MKDEV_M(afu), &afu->afu_cdev_m,
557                                &afu->chardev_m, "m", "master",
558                                &afu_master_fops);
559 }
560
561 int cxl_chardev_s_afu_add(struct cxl_afu *afu)
562 {
563         return cxl_add_chardev(afu, CXL_AFU_MKDEV_S(afu), &afu->afu_cdev_s,
564                                &afu->chardev_s, "s", "shared",
565                                &afu_fops);
566 }
567
568 void cxl_chardev_afu_remove(struct cxl_afu *afu)
569 {
570         if (afu->chardev_d) {
571                 cdev_del(&afu->afu_cdev_d);
572                 device_unregister(afu->chardev_d);
573                 afu->chardev_d = NULL;
574         }
575         if (afu->chardev_m) {
576                 cdev_del(&afu->afu_cdev_m);
577                 device_unregister(afu->chardev_m);
578                 afu->chardev_m = NULL;
579         }
580         if (afu->chardev_s) {
581                 cdev_del(&afu->afu_cdev_s);
582                 device_unregister(afu->chardev_s);
583                 afu->chardev_s = NULL;
584         }
585 }
586
587 int cxl_register_afu(struct cxl_afu *afu)
588 {
589         afu->dev.class = cxl_class;
590
591         return device_register(&afu->dev);
592 }
593
594 int cxl_register_adapter(struct cxl *adapter)
595 {
596         adapter->dev.class = cxl_class;
597
598         /*
599          * Future: When we support dynamically reprogramming the PSL & AFU we
600          * will expose the interface to do that via a chardev:
601          * adapter->dev.devt = CXL_CARD_MKDEV(adapter);
602          */
603
604         return device_register(&adapter->dev);
605 }
606
607 dev_t cxl_get_dev(void)
608 {
609         return cxl_dev;
610 }
611
612 int __init cxl_file_init(void)
613 {
614         int rc;
615
616         /*
617          * If these change we really need to update API.  Either change some
618          * flags or update API version number CXL_API_VERSION.
619          */
620         BUILD_BUG_ON(CXL_API_VERSION != 3);
621         BUILD_BUG_ON(sizeof(struct cxl_ioctl_start_work) != 64);
622         BUILD_BUG_ON(sizeof(struct cxl_event_header) != 8);
623         BUILD_BUG_ON(sizeof(struct cxl_event_afu_interrupt) != 8);
624         BUILD_BUG_ON(sizeof(struct cxl_event_data_storage) != 32);
625         BUILD_BUG_ON(sizeof(struct cxl_event_afu_error) != 16);
626
627         if ((rc = alloc_chrdev_region(&cxl_dev, 0, CXL_NUM_MINORS, "cxl"))) {
628                 pr_err("Unable to allocate CXL major number: %i\n", rc);
629                 return rc;
630         }
631
632         pr_devel("CXL device allocated, MAJOR %i\n", MAJOR(cxl_dev));
633
634         cxl_class = class_create(THIS_MODULE, "cxl");
635         if (IS_ERR(cxl_class)) {
636                 pr_err("Unable to create CXL class\n");
637                 rc = PTR_ERR(cxl_class);
638                 goto err;
639         }
640         cxl_class->devnode = cxl_devnode;
641
642         return 0;
643
644 err:
645         unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
646         return rc;
647 }
648
649 void cxl_file_exit(void)
650 {
651         unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
652         class_destroy(cxl_class);
653 }