]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/misc/cxl/file.c
cxl: Move include file cxl.h -> cxl-base.h
[mirror_ubuntu-artful-kernel.git] / drivers / misc / cxl / file.c
CommitLineData
f204e0b8
IM
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"
9bcf28cd 26#include "trace.h"
f204e0b8
IM
27
28#define CXL_NUM_MINORS 256 /* Total to reserve */
29#define CXL_DEV_MINORS 13 /* 1 control + 4 AFUs * 3 (dedicated/master/shared) */
30
31#define CXL_CARD_MINOR(adapter) (adapter->adapter_num * CXL_DEV_MINORS)
32#define CXL_AFU_MINOR_D(afu) (CXL_CARD_MINOR(afu->adapter) + 1 + (3 * afu->slice))
33#define CXL_AFU_MINOR_M(afu) (CXL_AFU_MINOR_D(afu) + 1)
34#define CXL_AFU_MINOR_S(afu) (CXL_AFU_MINOR_D(afu) + 2)
35#define CXL_AFU_MKDEV_D(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_D(afu))
36#define CXL_AFU_MKDEV_M(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_M(afu))
37#define CXL_AFU_MKDEV_S(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_S(afu))
38
39#define CXL_DEVT_ADAPTER(dev) (MINOR(dev) / CXL_DEV_MINORS)
40#define CXL_DEVT_AFU(dev) ((MINOR(dev) % CXL_DEV_MINORS - 1) / 3)
41
42#define CXL_DEVT_IS_CARD(dev) (MINOR(dev) % CXL_DEV_MINORS == 0)
43
44static dev_t cxl_dev;
45
46static struct class *cxl_class;
47
48static int __afu_open(struct inode *inode, struct file *file, bool master)
49{
50 struct cxl *adapter;
51 struct cxl_afu *afu;
52 struct cxl_context *ctx;
53 int adapter_num = CXL_DEVT_ADAPTER(inode->i_rdev);
54 int slice = CXL_DEVT_AFU(inode->i_rdev);
55 int rc = -ENODEV;
56
57 pr_devel("afu_open afu%i.%i\n", slice, adapter_num);
58
59 if (!(adapter = get_cxl_adapter(adapter_num)))
60 return -ENODEV;
61
62 if (slice > adapter->slices)
63 goto err_put_adapter;
64
65 spin_lock(&adapter->afu_list_lock);
66 if (!(afu = adapter->afu[slice])) {
67 spin_unlock(&adapter->afu_list_lock);
68 goto err_put_adapter;
69 }
70 get_device(&afu->dev);
71 spin_unlock(&adapter->afu_list_lock);
72
73 if (!afu->current_mode)
74 goto err_put_afu;
75
76 if (!(ctx = cxl_context_alloc())) {
77 rc = -ENOMEM;
78 goto err_put_afu;
79 }
80
b123429e 81 if ((rc = cxl_context_init(ctx, afu, master, inode->i_mapping)))
f204e0b8
IM
82 goto err_put_afu;
83
84 pr_devel("afu_open pe: %i\n", ctx->pe);
85 file->private_data = ctx;
86 cxl_ctx_get();
87
88 /* Our ref on the AFU will now hold the adapter */
89 put_device(&adapter->dev);
90
91 return 0;
92
93err_put_afu:
94 put_device(&afu->dev);
95err_put_adapter:
96 put_device(&adapter->dev);
97 return rc;
98}
99static int afu_open(struct inode *inode, struct file *file)
100{
101 return __afu_open(inode, file, false);
102}
103
104static int afu_master_open(struct inode *inode, struct file *file)
105{
106 return __afu_open(inode, file, true);
107}
108
109static int afu_release(struct inode *inode, struct file *file)
110{
111 struct cxl_context *ctx = file->private_data;
112
113 pr_devel("%s: closing cxl file descriptor. pe: %i\n",
114 __func__, ctx->pe);
115 cxl_context_detach(ctx);
116
b123429e
IM
117 mutex_lock(&ctx->mapping_lock);
118 ctx->mapping = NULL;
119 mutex_unlock(&ctx->mapping_lock);
120
f204e0b8
IM
121 put_device(&ctx->afu->dev);
122
123 /*
124 * At this this point all bottom halfs have finished and we should be
125 * getting no more IRQs from the hardware for this context. Once it's
126 * removed from the IDR (and RCU synchronised) it's safe to free the
127 * sstp and context.
128 */
129 cxl_context_free(ctx);
130
f204e0b8
IM
131 return 0;
132}
133
134static long afu_ioctl_start_work(struct cxl_context *ctx,
135 struct cxl_ioctl_start_work __user *uwork)
136{
137 struct cxl_ioctl_start_work work;
138 u64 amr = 0;
139 int rc;
140
141 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
142
0712dc7e
IM
143 /* Do this outside the status_mutex to avoid a circular dependency with
144 * the locking in cxl_mmap_fault() */
f204e0b8
IM
145 if (copy_from_user(&work, uwork,
146 sizeof(struct cxl_ioctl_start_work))) {
147 rc = -EFAULT;
148 goto out;
149 }
150
0712dc7e
IM
151 mutex_lock(&ctx->status_mutex);
152 if (ctx->status != OPENED) {
153 rc = -EIO;
154 goto out;
155 }
156
f204e0b8
IM
157 /*
158 * if any of the reserved fields are set or any of the unused
159 * flags are set it's invalid
160 */
161 if (work.reserved1 || work.reserved2 || work.reserved3 ||
162 work.reserved4 || work.reserved5 || work.reserved6 ||
163 (work.flags & ~CXL_START_WORK_ALL)) {
164 rc = -EINVAL;
165 goto out;
166 }
167
168 if (!(work.flags & CXL_START_WORK_NUM_IRQS))
169 work.num_interrupts = ctx->afu->pp_irqs;
170 else if ((work.num_interrupts < ctx->afu->pp_irqs) ||
171 (work.num_interrupts > ctx->afu->irqs_max)) {
172 rc = -EINVAL;
173 goto out;
174 }
175 if ((rc = afu_register_irqs(ctx, work.num_interrupts)))
176 goto out;
177
178 if (work.flags & CXL_START_WORK_AMR)
179 amr = work.amr & mfspr(SPRN_UAMOR);
180
181 /*
182 * We grab the PID here and not in the file open to allow for the case
183 * where a process (master, some daemon, etc) has opened the chardev on
184 * behalf of another process, so the AFU's mm gets bound to the process
185 * that performs this ioctl and not the process that opened the file.
186 */
187 ctx->pid = get_pid(get_task_pid(current, PIDTYPE_PID));
188
9bcf28cd
IM
189 trace_cxl_attach(ctx, work.work_element_descriptor, work.num_interrupts, amr);
190
f204e0b8 191 if ((rc = cxl_attach_process(ctx, false, work.work_element_descriptor,
456295e2 192 amr))) {
6428832a 193 afu_release_irqs(ctx, ctx);
f204e0b8 194 goto out;
456295e2 195 }
f204e0b8
IM
196
197 ctx->status = STARTED;
198 rc = 0;
199out:
200 mutex_unlock(&ctx->status_mutex);
201 return rc;
202}
203static long afu_ioctl_process_element(struct cxl_context *ctx,
204 int __user *upe)
205{
206 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
207
208 if (copy_to_user(upe, &ctx->pe, sizeof(__u32)))
209 return -EFAULT;
210
211 return 0;
212}
213
27d4dc71
VJ
214static long afu_ioctl_get_afu_id(struct cxl_context *ctx,
215 struct cxl_afu_id __user *upafuid)
216{
217 struct cxl_afu_id afuid = { 0 };
218
219 afuid.card_id = ctx->afu->adapter->adapter_num;
220 afuid.afu_offset = ctx->afu->slice;
221 afuid.afu_mode = ctx->afu->current_mode;
222
223 /* set the flag bit in case the afu is a slave */
224 if (ctx->afu->current_mode == CXL_MODE_DIRECTED && !ctx->master)
225 afuid.flags |= CXL_AFUID_FLAG_SLAVE;
226
227 if (copy_to_user(upafuid, &afuid, sizeof(afuid)))
228 return -EFAULT;
229
230 return 0;
231}
232
f204e0b8
IM
233static long afu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
234{
235 struct cxl_context *ctx = file->private_data;
236
237 if (ctx->status == CLOSED)
238 return -EIO;
239
240 pr_devel("afu_ioctl\n");
241 switch (cmd) {
242 case CXL_IOCTL_START_WORK:
243 return afu_ioctl_start_work(ctx, (struct cxl_ioctl_start_work __user *)arg);
244 case CXL_IOCTL_GET_PROCESS_ELEMENT:
245 return afu_ioctl_process_element(ctx, (__u32 __user *)arg);
27d4dc71
VJ
246 case CXL_IOCTL_GET_AFU_ID:
247 return afu_ioctl_get_afu_id(ctx, (struct cxl_afu_id __user *)
248 arg);
f204e0b8
IM
249 }
250 return -EINVAL;
251}
252
253static long afu_compat_ioctl(struct file *file, unsigned int cmd,
254 unsigned long arg)
255{
256 return afu_ioctl(file, cmd, arg);
257}
258
259static int afu_mmap(struct file *file, struct vm_area_struct *vm)
260{
261 struct cxl_context *ctx = file->private_data;
262
263 /* AFU must be started before we can MMIO */
264 if (ctx->status != STARTED)
265 return -EIO;
266
267 return cxl_context_iomap(ctx, vm);
268}
269
270static unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
271{
272 struct cxl_context *ctx = file->private_data;
273 int mask = 0;
274 unsigned long flags;
275
276
277 poll_wait(file, &ctx->wq, poll);
278
279 pr_devel("afu_poll wait done pe: %i\n", ctx->pe);
280
281 spin_lock_irqsave(&ctx->lock, flags);
282 if (ctx->pending_irq || ctx->pending_fault ||
283 ctx->pending_afu_err)
284 mask |= POLLIN | POLLRDNORM;
285 else if (ctx->status == CLOSED)
286 /* Only error on closed when there are no futher events pending
287 */
288 mask |= POLLERR;
289 spin_unlock_irqrestore(&ctx->lock, flags);
290
291 pr_devel("afu_poll pe: %i returning %#x\n", ctx->pe, mask);
292
293 return mask;
294}
295
296static inline int ctx_event_pending(struct cxl_context *ctx)
297{
298 return (ctx->pending_irq || ctx->pending_fault ||
299 ctx->pending_afu_err || (ctx->status == CLOSED));
300}
301
302static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
303 loff_t *off)
304{
305 struct cxl_context *ctx = file->private_data;
306 struct cxl_event event;
307 unsigned long flags;
d53ba6b3 308 int rc;
f204e0b8
IM
309 DEFINE_WAIT(wait);
310
311 if (count < CXL_READ_MIN_SIZE)
312 return -EINVAL;
313
314 spin_lock_irqsave(&ctx->lock, flags);
315
316 for (;;) {
317 prepare_to_wait(&ctx->wq, &wait, TASK_INTERRUPTIBLE);
318 if (ctx_event_pending(ctx))
319 break;
320
d53ba6b3
IM
321 if (file->f_flags & O_NONBLOCK) {
322 rc = -EAGAIN;
323 goto out;
324 }
f204e0b8 325
d53ba6b3
IM
326 if (signal_pending(current)) {
327 rc = -ERESTARTSYS;
328 goto out;
329 }
f204e0b8 330
d53ba6b3 331 spin_unlock_irqrestore(&ctx->lock, flags);
f204e0b8
IM
332 pr_devel("afu_read going to sleep...\n");
333 schedule();
334 pr_devel("afu_read woken up\n");
335 spin_lock_irqsave(&ctx->lock, flags);
336 }
337
338 finish_wait(&ctx->wq, &wait);
339
340 memset(&event, 0, sizeof(event));
341 event.header.process_element = ctx->pe;
342 event.header.size = sizeof(struct cxl_event_header);
343 if (ctx->pending_irq) {
344 pr_devel("afu_read delivering AFU interrupt\n");
345 event.header.size += sizeof(struct cxl_event_afu_interrupt);
346 event.header.type = CXL_EVENT_AFU_INTERRUPT;
347 event.irq.irq = find_first_bit(ctx->irq_bitmap, ctx->irq_count) + 1;
348 clear_bit(event.irq.irq - 1, ctx->irq_bitmap);
349 if (bitmap_empty(ctx->irq_bitmap, ctx->irq_count))
350 ctx->pending_irq = false;
351 } else if (ctx->pending_fault) {
352 pr_devel("afu_read delivering data storage fault\n");
353 event.header.size += sizeof(struct cxl_event_data_storage);
354 event.header.type = CXL_EVENT_DATA_STORAGE;
355 event.fault.addr = ctx->fault_addr;
356 event.fault.dsisr = ctx->fault_dsisr;
357 ctx->pending_fault = false;
358 } else if (ctx->pending_afu_err) {
359 pr_devel("afu_read delivering afu error\n");
360 event.header.size += sizeof(struct cxl_event_afu_error);
361 event.header.type = CXL_EVENT_AFU_ERROR;
362 event.afu_error.error = ctx->afu_err;
363 ctx->pending_afu_err = false;
364 } else if (ctx->status == CLOSED) {
365 pr_devel("afu_read fatal error\n");
366 spin_unlock_irqrestore(&ctx->lock, flags);
367 return -EIO;
368 } else
369 WARN(1, "afu_read must be buggy\n");
370
371 spin_unlock_irqrestore(&ctx->lock, flags);
372
373 if (copy_to_user(buf, &event, event.header.size))
374 return -EFAULT;
375 return event.header.size;
d53ba6b3
IM
376
377out:
378 finish_wait(&ctx->wq, &wait);
379 spin_unlock_irqrestore(&ctx->lock, flags);
380 return rc;
f204e0b8
IM
381}
382
383static const struct file_operations afu_fops = {
384 .owner = THIS_MODULE,
385 .open = afu_open,
386 .poll = afu_poll,
387 .read = afu_read,
388 .release = afu_release,
389 .unlocked_ioctl = afu_ioctl,
390 .compat_ioctl = afu_compat_ioctl,
391 .mmap = afu_mmap,
392};
393
394static const struct file_operations afu_master_fops = {
395 .owner = THIS_MODULE,
396 .open = afu_master_open,
397 .poll = afu_poll,
398 .read = afu_read,
399 .release = afu_release,
400 .unlocked_ioctl = afu_ioctl,
401 .compat_ioctl = afu_compat_ioctl,
402 .mmap = afu_mmap,
403};
404
405
406static char *cxl_devnode(struct device *dev, umode_t *mode)
407{
408 if (CXL_DEVT_IS_CARD(dev->devt)) {
409 /*
410 * These minor numbers will eventually be used to program the
411 * PSL and AFUs once we have dynamic reprogramming support
412 */
413 return NULL;
414 }
415 return kasprintf(GFP_KERNEL, "cxl/%s", dev_name(dev));
416}
417
418extern struct class *cxl_class;
419
420static int cxl_add_chardev(struct cxl_afu *afu, dev_t devt, struct cdev *cdev,
421 struct device **chardev, char *postfix, char *desc,
422 const struct file_operations *fops)
423{
424 struct device *dev;
425 int rc;
426
427 cdev_init(cdev, fops);
428 if ((rc = cdev_add(cdev, devt, 1))) {
429 dev_err(&afu->dev, "Unable to add %s chardev: %i\n", desc, rc);
430 return rc;
431 }
432
433 dev = device_create(cxl_class, &afu->dev, devt, afu,
434 "afu%i.%i%s", afu->adapter->adapter_num, afu->slice, postfix);
435 if (IS_ERR(dev)) {
436 dev_err(&afu->dev, "Unable to create %s chardev in sysfs: %i\n", desc, rc);
437 rc = PTR_ERR(dev);
438 goto err;
439 }
440
441 *chardev = dev;
442
443 return 0;
444err:
445 cdev_del(cdev);
446 return rc;
447}
448
449int cxl_chardev_d_afu_add(struct cxl_afu *afu)
450{
451 return cxl_add_chardev(afu, CXL_AFU_MKDEV_D(afu), &afu->afu_cdev_d,
452 &afu->chardev_d, "d", "dedicated",
453 &afu_master_fops); /* Uses master fops */
454}
455
456int cxl_chardev_m_afu_add(struct cxl_afu *afu)
457{
458 return cxl_add_chardev(afu, CXL_AFU_MKDEV_M(afu), &afu->afu_cdev_m,
459 &afu->chardev_m, "m", "master",
460 &afu_master_fops);
461}
462
463int cxl_chardev_s_afu_add(struct cxl_afu *afu)
464{
465 return cxl_add_chardev(afu, CXL_AFU_MKDEV_S(afu), &afu->afu_cdev_s,
466 &afu->chardev_s, "s", "shared",
467 &afu_fops);
468}
469
470void cxl_chardev_afu_remove(struct cxl_afu *afu)
471{
472 if (afu->chardev_d) {
473 cdev_del(&afu->afu_cdev_d);
474 device_unregister(afu->chardev_d);
475 afu->chardev_d = NULL;
476 }
477 if (afu->chardev_m) {
478 cdev_del(&afu->afu_cdev_m);
479 device_unregister(afu->chardev_m);
480 afu->chardev_m = NULL;
481 }
482 if (afu->chardev_s) {
483 cdev_del(&afu->afu_cdev_s);
484 device_unregister(afu->chardev_s);
485 afu->chardev_s = NULL;
486 }
487}
488
489int cxl_register_afu(struct cxl_afu *afu)
490{
491 afu->dev.class = cxl_class;
492
493 return device_register(&afu->dev);
494}
495
496int cxl_register_adapter(struct cxl *adapter)
497{
498 adapter->dev.class = cxl_class;
499
500 /*
501 * Future: When we support dynamically reprogramming the PSL & AFU we
502 * will expose the interface to do that via a chardev:
503 * adapter->dev.devt = CXL_CARD_MKDEV(adapter);
504 */
505
506 return device_register(&adapter->dev);
507}
508
509int __init cxl_file_init(void)
510{
511 int rc;
512
513 /*
514 * If these change we really need to update API. Either change some
515 * flags or update API version number CXL_API_VERSION.
516 */
517 BUILD_BUG_ON(CXL_API_VERSION != 1);
518 BUILD_BUG_ON(sizeof(struct cxl_ioctl_start_work) != 64);
519 BUILD_BUG_ON(sizeof(struct cxl_event_header) != 8);
520 BUILD_BUG_ON(sizeof(struct cxl_event_afu_interrupt) != 8);
521 BUILD_BUG_ON(sizeof(struct cxl_event_data_storage) != 32);
522 BUILD_BUG_ON(sizeof(struct cxl_event_afu_error) != 16);
523
524 if ((rc = alloc_chrdev_region(&cxl_dev, 0, CXL_NUM_MINORS, "cxl"))) {
525 pr_err("Unable to allocate CXL major number: %i\n", rc);
526 return rc;
527 }
528
529 pr_devel("CXL device allocated, MAJOR %i\n", MAJOR(cxl_dev));
530
531 cxl_class = class_create(THIS_MODULE, "cxl");
532 if (IS_ERR(cxl_class)) {
533 pr_err("Unable to create CXL class\n");
534 rc = PTR_ERR(cxl_class);
535 goto err;
536 }
537 cxl_class->devnode = cxl_devnode;
538
539 return 0;
540
541err:
542 unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
543 return rc;
544}
545
546void cxl_file_exit(void)
547{
548 unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
549 class_destroy(cxl_class);
550}