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