1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
4 #include <linux/poll.h>
5 #include <linux/sched/signal.h>
6 #include <linux/eventfd.h>
7 #include <linux/uaccess.h>
8 #include <uapi/misc/ocxl.h>
10 #include <asm/switch_to.h>
11 #include "ocxl_internal.h"
14 #define OCXL_NUM_MINORS 256 /* Total to reserve */
16 static dev_t ocxl_dev;
17 static struct class *ocxl_class;
18 static DEFINE_MUTEX(minors_idr_lock);
19 static struct idr minors_idr;
21 static struct ocxl_file_info *find_and_get_file_info(dev_t devno)
23 struct ocxl_file_info *info;
25 mutex_lock(&minors_idr_lock);
26 info = idr_find(&minors_idr, MINOR(devno));
28 get_device(&info->dev);
29 mutex_unlock(&minors_idr_lock);
33 static int allocate_minor(struct ocxl_file_info *info)
37 mutex_lock(&minors_idr_lock);
38 minor = idr_alloc(&minors_idr, info, 0, OCXL_NUM_MINORS, GFP_KERNEL);
39 mutex_unlock(&minors_idr_lock);
43 static void free_minor(struct ocxl_file_info *info)
45 mutex_lock(&minors_idr_lock);
46 idr_remove(&minors_idr, MINOR(info->dev.devt));
47 mutex_unlock(&minors_idr_lock);
50 static int afu_open(struct inode *inode, struct file *file)
52 struct ocxl_file_info *info;
53 struct ocxl_context *ctx;
56 pr_debug("%s for device %x\n", __func__, inode->i_rdev);
58 info = find_and_get_file_info(inode->i_rdev);
62 rc = ocxl_context_alloc(&ctx, info->afu, inode->i_mapping);
64 put_device(&info->dev);
67 put_device(&info->dev);
68 file->private_data = ctx;
72 static long afu_ioctl_attach(struct ocxl_context *ctx,
73 struct ocxl_ioctl_attach __user *uarg)
75 struct ocxl_ioctl_attach arg;
78 pr_debug("%s for context %d\n", __func__, ctx->pasid);
80 if (copy_from_user(&arg, uarg, sizeof(arg)))
83 /* Make sure reserved fields are not set for forward compatibility */
84 if (arg.reserved1 || arg.reserved2 || arg.reserved3)
87 amr = arg.amr & mfspr(SPRN_UAMOR);
88 return ocxl_context_attach(ctx, amr, current->mm);
91 static long afu_ioctl_get_metadata(struct ocxl_context *ctx,
92 struct ocxl_ioctl_metadata __user *uarg)
94 struct ocxl_ioctl_metadata arg;
96 memset(&arg, 0, sizeof(arg));
100 arg.afu_version_major = ctx->afu->config.version_major;
101 arg.afu_version_minor = ctx->afu->config.version_minor;
102 arg.pasid = ctx->pasid;
103 arg.pp_mmio_size = ctx->afu->config.pp_mmio_stride;
104 arg.global_mmio_size = ctx->afu->config.global_mmio_size;
106 if (copy_to_user(uarg, &arg, sizeof(arg)))
113 static long afu_ioctl_enable_p9_wait(struct ocxl_context *ctx,
114 struct ocxl_ioctl_p9_wait __user *uarg)
116 struct ocxl_ioctl_p9_wait arg;
118 memset(&arg, 0, sizeof(arg));
120 if (cpu_has_feature(CPU_FTR_P9_TIDR)) {
121 enum ocxl_context_status status;
123 // Locks both status & tidr
124 mutex_lock(&ctx->status_mutex);
126 if (set_thread_tidr(current)) {
127 mutex_unlock(&ctx->status_mutex);
131 ctx->tidr = current->thread.tidr;
134 status = ctx->status;
135 mutex_unlock(&ctx->status_mutex);
137 if (status == ATTACHED) {
138 int rc = ocxl_link_update_pe(ctx->afu->fn->link,
139 ctx->pasid, ctx->tidr);
145 arg.thread_id = ctx->tidr;
149 if (copy_to_user(uarg, &arg, sizeof(arg)))
157 static long afu_ioctl_get_features(struct ocxl_context *ctx,
158 struct ocxl_ioctl_features __user *uarg)
160 struct ocxl_ioctl_features arg;
162 memset(&arg, 0, sizeof(arg));
165 if (cpu_has_feature(CPU_FTR_P9_TIDR))
166 arg.flags[0] |= OCXL_IOCTL_FEATURES_FLAGS0_P9_WAIT;
169 if (copy_to_user(uarg, &arg, sizeof(arg)))
175 #define CMD_STR(x) (x == OCXL_IOCTL_ATTACH ? "ATTACH" : \
176 x == OCXL_IOCTL_IRQ_ALLOC ? "IRQ_ALLOC" : \
177 x == OCXL_IOCTL_IRQ_FREE ? "IRQ_FREE" : \
178 x == OCXL_IOCTL_IRQ_SET_FD ? "IRQ_SET_FD" : \
179 x == OCXL_IOCTL_GET_METADATA ? "GET_METADATA" : \
180 x == OCXL_IOCTL_ENABLE_P9_WAIT ? "ENABLE_P9_WAIT" : \
181 x == OCXL_IOCTL_GET_FEATURES ? "GET_FEATURES" : \
184 static irqreturn_t irq_handler(void *private)
186 struct eventfd_ctx *ev_ctx = private;
188 eventfd_signal(ev_ctx, 1);
192 static void irq_free(void *private)
194 struct eventfd_ctx *ev_ctx = private;
196 eventfd_ctx_put(ev_ctx);
199 static long afu_ioctl(struct file *file, unsigned int cmd,
202 struct ocxl_context *ctx = file->private_data;
203 struct ocxl_ioctl_irq_fd irq_fd;
204 struct eventfd_ctx *ev_ctx;
210 pr_debug("%s for context %d, command %s\n", __func__, ctx->pasid,
213 mutex_lock(&ctx->status_mutex);
214 closed = (ctx->status == CLOSED);
215 mutex_unlock(&ctx->status_mutex);
221 case OCXL_IOCTL_ATTACH:
222 rc = afu_ioctl_attach(ctx,
223 (struct ocxl_ioctl_attach __user *) args);
226 case OCXL_IOCTL_IRQ_ALLOC:
227 rc = ocxl_afu_irq_alloc(ctx, &irq_id);
229 irq_offset = ocxl_irq_id_to_offset(ctx, irq_id);
230 rc = copy_to_user((u64 __user *) args, &irq_offset,
233 ocxl_afu_irq_free(ctx, irq_id);
239 case OCXL_IOCTL_IRQ_FREE:
240 rc = copy_from_user(&irq_offset, (u64 __user *) args,
244 irq_id = ocxl_irq_offset_to_id(ctx, irq_offset);
245 rc = ocxl_afu_irq_free(ctx, irq_id);
248 case OCXL_IOCTL_IRQ_SET_FD:
249 rc = copy_from_user(&irq_fd, (u64 __user *) args,
255 irq_id = ocxl_irq_offset_to_id(ctx, irq_fd.irq_offset);
256 ev_ctx = eventfd_ctx_fdget(irq_fd.eventfd);
258 return PTR_ERR(ev_ctx);
259 rc = ocxl_irq_set_handler(ctx, irq_id, irq_handler, irq_free, ev_ctx);
262 case OCXL_IOCTL_GET_METADATA:
263 rc = afu_ioctl_get_metadata(ctx,
264 (struct ocxl_ioctl_metadata __user *) args);
268 case OCXL_IOCTL_ENABLE_P9_WAIT:
269 rc = afu_ioctl_enable_p9_wait(ctx,
270 (struct ocxl_ioctl_p9_wait __user *) args);
274 case OCXL_IOCTL_GET_FEATURES:
275 rc = afu_ioctl_get_features(ctx,
276 (struct ocxl_ioctl_features __user *) args);
285 static long afu_compat_ioctl(struct file *file, unsigned int cmd,
288 return afu_ioctl(file, cmd, args);
291 static int afu_mmap(struct file *file, struct vm_area_struct *vma)
293 struct ocxl_context *ctx = file->private_data;
295 pr_debug("%s for context %d\n", __func__, ctx->pasid);
296 return ocxl_context_mmap(ctx, vma);
299 static bool has_xsl_error(struct ocxl_context *ctx)
303 mutex_lock(&ctx->xsl_error_lock);
304 ret = !!ctx->xsl_error.addr;
305 mutex_unlock(&ctx->xsl_error_lock);
311 * Are there any events pending on the AFU
312 * ctx: The AFU context
313 * Returns: true if there are events pending
315 static bool afu_events_pending(struct ocxl_context *ctx)
317 if (has_xsl_error(ctx))
322 static unsigned int afu_poll(struct file *file, struct poll_table_struct *wait)
324 struct ocxl_context *ctx = file->private_data;
325 unsigned int mask = 0;
328 pr_debug("%s for context %d\n", __func__, ctx->pasid);
330 poll_wait(file, &ctx->events_wq, wait);
332 mutex_lock(&ctx->status_mutex);
333 closed = (ctx->status == CLOSED);
334 mutex_unlock(&ctx->status_mutex);
336 if (afu_events_pending(ctx))
337 mask = EPOLLIN | EPOLLRDNORM;
345 * Populate the supplied buffer with a single XSL error
346 * ctx: The AFU context to report the error from
347 * header: the event header to populate
348 * buf: The buffer to write the body into (should be at least
349 * AFU_EVENT_BODY_XSL_ERROR_SIZE)
350 * Return: the amount of buffer that was populated
352 static ssize_t append_xsl_error(struct ocxl_context *ctx,
353 struct ocxl_kernel_event_header *header,
356 struct ocxl_kernel_event_xsl_fault_error body;
358 memset(&body, 0, sizeof(body));
360 mutex_lock(&ctx->xsl_error_lock);
361 if (!ctx->xsl_error.addr) {
362 mutex_unlock(&ctx->xsl_error_lock);
366 body.addr = ctx->xsl_error.addr;
367 body.dsisr = ctx->xsl_error.dsisr;
368 body.count = ctx->xsl_error.count;
370 ctx->xsl_error.addr = 0;
371 ctx->xsl_error.dsisr = 0;
372 ctx->xsl_error.count = 0;
374 mutex_unlock(&ctx->xsl_error_lock);
376 header->type = OCXL_AFU_EVENT_XSL_FAULT_ERROR;
378 if (copy_to_user(buf, &body, sizeof(body)))
384 #define AFU_EVENT_BODY_MAX_SIZE sizeof(struct ocxl_kernel_event_xsl_fault_error)
387 * Reports events on the AFU
389 * Header (struct ocxl_kernel_event_header)
390 * Body (struct ocxl_kernel_event_*)
393 static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
396 struct ocxl_context *ctx = file->private_data;
397 struct ocxl_kernel_event_header header;
400 DEFINE_WAIT(event_wait);
402 memset(&header, 0, sizeof(header));
404 /* Require offset to be 0 */
408 if (count < (sizeof(struct ocxl_kernel_event_header) +
409 AFU_EVENT_BODY_MAX_SIZE))
413 prepare_to_wait(&ctx->events_wq, &event_wait,
416 if (afu_events_pending(ctx))
419 if (ctx->status == CLOSED)
422 if (file->f_flags & O_NONBLOCK) {
423 finish_wait(&ctx->events_wq, &event_wait);
427 if (signal_pending(current)) {
428 finish_wait(&ctx->events_wq, &event_wait);
435 finish_wait(&ctx->events_wq, &event_wait);
437 if (has_xsl_error(ctx)) {
438 used = append_xsl_error(ctx, &header, buf + sizeof(header));
443 if (!afu_events_pending(ctx))
444 header.flags |= OCXL_KERNEL_EVENT_FLAG_LAST;
446 if (copy_to_user(buf, &header, sizeof(header)))
449 used += sizeof(header);
455 static int afu_release(struct inode *inode, struct file *file)
457 struct ocxl_context *ctx = file->private_data;
460 pr_debug("%s for device %x\n", __func__, inode->i_rdev);
461 rc = ocxl_context_detach(ctx);
462 mutex_lock(&ctx->mapping_lock);
464 mutex_unlock(&ctx->mapping_lock);
465 wake_up_all(&ctx->events_wq);
467 ocxl_context_free(ctx);
471 static const struct file_operations ocxl_afu_fops = {
472 .owner = THIS_MODULE,
474 .unlocked_ioctl = afu_ioctl,
475 .compat_ioctl = afu_compat_ioctl,
479 .release = afu_release,
482 // Free the info struct
483 static void info_release(struct device *dev)
485 struct ocxl_file_info *info = container_of(dev, struct ocxl_file_info, dev);
487 ocxl_afu_put(info->afu);
491 static int ocxl_file_make_visible(struct ocxl_file_info *info)
495 cdev_init(&info->cdev, &ocxl_afu_fops);
496 rc = cdev_add(&info->cdev, info->dev.devt, 1);
498 dev_err(&info->dev, "Unable to add afu char device: %d\n", rc);
505 static void ocxl_file_make_invisible(struct ocxl_file_info *info)
507 cdev_del(&info->cdev);
510 int ocxl_file_register_afu(struct ocxl_afu *afu)
514 struct ocxl_file_info *info;
515 struct ocxl_fn *fn = afu->fn;
516 struct pci_dev *pci_dev = to_pci_dev(fn->dev.parent);
518 info = kzalloc(sizeof(*info), GFP_KERNEL);
522 minor = allocate_minor(info);
528 info->dev.parent = &fn->dev;
529 info->dev.devt = MKDEV(MAJOR(ocxl_dev), minor);
530 info->dev.class = ocxl_class;
531 info->dev.release = info_release;
536 rc = dev_set_name(&info->dev, "%s.%s.%hhu",
537 afu->config.name, dev_name(&pci_dev->dev), afu->config.idx);
541 rc = device_register(&info->dev);
545 rc = ocxl_sysfs_register_afu(info);
549 rc = ocxl_file_make_visible(info);
553 ocxl_afu_set_private(afu, info);
558 ocxl_sysfs_unregister_afu(info); // safe to call even if register failed
560 device_unregister(&info->dev);
569 void ocxl_file_unregister_afu(struct ocxl_afu *afu)
571 struct ocxl_file_info *info = ocxl_afu_get_private(afu);
576 ocxl_file_make_invisible(info);
577 ocxl_sysfs_unregister_afu(info);
579 device_unregister(&info->dev);
582 static char *ocxl_devnode(struct device *dev, umode_t *mode)
584 return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
587 int ocxl_file_init(void)
591 idr_init(&minors_idr);
593 rc = alloc_chrdev_region(&ocxl_dev, 0, OCXL_NUM_MINORS, "ocxl");
595 pr_err("Unable to allocate ocxl major number: %d\n", rc);
599 ocxl_class = class_create(THIS_MODULE, "ocxl");
600 if (IS_ERR(ocxl_class)) {
601 pr_err("Unable to create ocxl class\n");
602 unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
603 return PTR_ERR(ocxl_class);
606 ocxl_class->devnode = ocxl_devnode;
610 void ocxl_file_exit(void)
612 class_destroy(ocxl_class);
613 unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
614 idr_destroy(&minors_idr);