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/uaccess.h>
7 #include <uapi/misc/ocxl.h>
8 #include "ocxl_internal.h"
11 #define OCXL_NUM_MINORS 256 /* Total to reserve */
13 static dev_t ocxl_dev;
14 static struct class *ocxl_class;
15 static struct mutex minors_idr_lock;
16 static struct idr minors_idr;
18 static struct ocxl_afu *find_and_get_afu(dev_t devno)
23 afu_minor = MINOR(devno);
25 * We don't declare an RCU critical section here, as our AFU
26 * is protected by a reference counter on the device. By the time the
27 * minor number of a device is removed from the idr, the ref count of
28 * the device is already at 0, so no user API will access that AFU and
29 * this function can't return it.
31 afu = idr_find(&minors_idr, afu_minor);
37 static int allocate_afu_minor(struct ocxl_afu *afu)
41 mutex_lock(&minors_idr_lock);
42 minor = idr_alloc(&minors_idr, afu, 0, OCXL_NUM_MINORS, GFP_KERNEL);
43 mutex_unlock(&minors_idr_lock);
47 static void free_afu_minor(struct ocxl_afu *afu)
49 mutex_lock(&minors_idr_lock);
50 idr_remove(&minors_idr, MINOR(afu->dev.devt));
51 mutex_unlock(&minors_idr_lock);
54 static int afu_open(struct inode *inode, struct file *file)
57 struct ocxl_context *ctx;
60 pr_debug("%s for device %x\n", __func__, inode->i_rdev);
62 afu = find_and_get_afu(inode->i_rdev);
66 ctx = ocxl_context_alloc();
72 rc = ocxl_context_init(ctx, afu, inode->i_mapping);
75 file->private_data = ctx;
84 static long afu_ioctl_attach(struct ocxl_context *ctx,
85 struct ocxl_ioctl_attach __user *uarg)
87 struct ocxl_ioctl_attach arg;
91 pr_debug("%s for context %d\n", __func__, ctx->pasid);
93 if (copy_from_user(&arg, uarg, sizeof(arg)))
96 /* Make sure reserved fields are not set for forward compatibility */
97 if (arg.reserved1 || arg.reserved2 || arg.reserved3)
100 amr = arg.amr & mfspr(SPRN_UAMOR);
101 rc = ocxl_context_attach(ctx, amr);
105 static long afu_ioctl_get_metadata(struct ocxl_context *ctx,
106 struct ocxl_ioctl_metadata __user *uarg)
108 struct ocxl_ioctl_metadata arg;
110 memset(&arg, 0, sizeof(arg));
114 arg.afu_version_major = ctx->afu->config.version_major;
115 arg.afu_version_minor = ctx->afu->config.version_minor;
116 arg.pasid = ctx->pasid;
117 arg.pp_mmio_size = ctx->afu->config.pp_mmio_stride;
118 arg.global_mmio_size = ctx->afu->config.global_mmio_size;
120 if (copy_to_user(uarg, &arg, sizeof(arg)))
126 #define CMD_STR(x) (x == OCXL_IOCTL_ATTACH ? "ATTACH" : \
127 x == OCXL_IOCTL_IRQ_ALLOC ? "IRQ_ALLOC" : \
128 x == OCXL_IOCTL_IRQ_FREE ? "IRQ_FREE" : \
129 x == OCXL_IOCTL_IRQ_SET_FD ? "IRQ_SET_FD" : \
130 x == OCXL_IOCTL_GET_METADATA ? "GET_METADATA" : \
133 static long afu_ioctl(struct file *file, unsigned int cmd,
136 struct ocxl_context *ctx = file->private_data;
137 struct ocxl_ioctl_irq_fd irq_fd;
141 pr_debug("%s for context %d, command %s\n", __func__, ctx->pasid,
144 if (ctx->status == CLOSED)
148 case OCXL_IOCTL_ATTACH:
149 rc = afu_ioctl_attach(ctx,
150 (struct ocxl_ioctl_attach __user *) args);
153 case OCXL_IOCTL_IRQ_ALLOC:
154 rc = ocxl_afu_irq_alloc(ctx, &irq_offset);
156 rc = copy_to_user((u64 __user *) args, &irq_offset,
159 ocxl_afu_irq_free(ctx, irq_offset);
165 case OCXL_IOCTL_IRQ_FREE:
166 rc = copy_from_user(&irq_offset, (u64 __user *) args,
170 rc = ocxl_afu_irq_free(ctx, irq_offset);
173 case OCXL_IOCTL_IRQ_SET_FD:
174 rc = copy_from_user(&irq_fd, (u64 __user *) args,
180 rc = ocxl_afu_irq_set_fd(ctx, irq_fd.irq_offset,
184 case OCXL_IOCTL_GET_METADATA:
185 rc = afu_ioctl_get_metadata(ctx,
186 (struct ocxl_ioctl_metadata __user *) args);
195 static long afu_compat_ioctl(struct file *file, unsigned int cmd,
198 return afu_ioctl(file, cmd, args);
201 static int afu_mmap(struct file *file, struct vm_area_struct *vma)
203 struct ocxl_context *ctx = file->private_data;
205 pr_debug("%s for context %d\n", __func__, ctx->pasid);
206 return ocxl_context_mmap(ctx, vma);
209 static bool has_xsl_error(struct ocxl_context *ctx)
213 mutex_lock(&ctx->xsl_error_lock);
214 ret = !!ctx->xsl_error.addr;
215 mutex_unlock(&ctx->xsl_error_lock);
221 * Are there any events pending on the AFU
222 * ctx: The AFU context
223 * Returns: true if there are events pending
225 static bool afu_events_pending(struct ocxl_context *ctx)
227 if (has_xsl_error(ctx))
232 static unsigned int afu_poll(struct file *file, struct poll_table_struct *wait)
234 struct ocxl_context *ctx = file->private_data;
235 unsigned int mask = 0;
238 pr_debug("%s for context %d\n", __func__, ctx->pasid);
240 poll_wait(file, &ctx->events_wq, wait);
242 mutex_lock(&ctx->status_mutex);
243 closed = (ctx->status == CLOSED);
244 mutex_unlock(&ctx->status_mutex);
246 if (afu_events_pending(ctx))
247 mask = EPOLLIN | EPOLLRDNORM;
255 * Populate the supplied buffer with a single XSL error
256 * ctx: The AFU context to report the error from
257 * header: the event header to populate
258 * buf: The buffer to write the body into (should be at least
259 * AFU_EVENT_BODY_XSL_ERROR_SIZE)
260 * Return: the amount of buffer that was populated
262 static ssize_t append_xsl_error(struct ocxl_context *ctx,
263 struct ocxl_kernel_event_header *header,
266 struct ocxl_kernel_event_xsl_fault_error body;
268 memset(&body, 0, sizeof(body));
270 mutex_lock(&ctx->xsl_error_lock);
271 if (!ctx->xsl_error.addr) {
272 mutex_unlock(&ctx->xsl_error_lock);
276 body.addr = ctx->xsl_error.addr;
277 body.dsisr = ctx->xsl_error.dsisr;
278 body.count = ctx->xsl_error.count;
280 ctx->xsl_error.addr = 0;
281 ctx->xsl_error.dsisr = 0;
282 ctx->xsl_error.count = 0;
284 mutex_unlock(&ctx->xsl_error_lock);
286 header->type = OCXL_AFU_EVENT_XSL_FAULT_ERROR;
288 if (copy_to_user(buf, &body, sizeof(body)))
294 #define AFU_EVENT_BODY_MAX_SIZE sizeof(struct ocxl_kernel_event_xsl_fault_error)
297 * Reports events on the AFU
299 * Header (struct ocxl_kernel_event_header)
300 * Body (struct ocxl_kernel_event_*)
303 static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
306 struct ocxl_context *ctx = file->private_data;
307 struct ocxl_kernel_event_header header;
310 DEFINE_WAIT(event_wait);
312 memset(&header, 0, sizeof(header));
314 /* Require offset to be 0 */
318 if (count < (sizeof(struct ocxl_kernel_event_header) +
319 AFU_EVENT_BODY_MAX_SIZE))
323 prepare_to_wait(&ctx->events_wq, &event_wait,
326 if (afu_events_pending(ctx))
329 if (ctx->status == CLOSED)
332 if (file->f_flags & O_NONBLOCK) {
333 finish_wait(&ctx->events_wq, &event_wait);
337 if (signal_pending(current)) {
338 finish_wait(&ctx->events_wq, &event_wait);
345 finish_wait(&ctx->events_wq, &event_wait);
347 if (has_xsl_error(ctx)) {
348 used = append_xsl_error(ctx, &header, buf + sizeof(header));
353 if (!afu_events_pending(ctx))
354 header.flags |= OCXL_KERNEL_EVENT_FLAG_LAST;
356 if (copy_to_user(buf, &header, sizeof(header)))
359 used += sizeof(header);
365 static int afu_release(struct inode *inode, struct file *file)
367 struct ocxl_context *ctx = file->private_data;
370 pr_debug("%s for device %x\n", __func__, inode->i_rdev);
371 rc = ocxl_context_detach(ctx);
372 mutex_lock(&ctx->mapping_lock);
374 mutex_unlock(&ctx->mapping_lock);
375 wake_up_all(&ctx->events_wq);
377 ocxl_context_free(ctx);
381 static const struct file_operations ocxl_afu_fops = {
382 .owner = THIS_MODULE,
384 .unlocked_ioctl = afu_ioctl,
385 .compat_ioctl = afu_compat_ioctl,
389 .release = afu_release,
392 int ocxl_create_cdev(struct ocxl_afu *afu)
396 cdev_init(&afu->cdev, &ocxl_afu_fops);
397 rc = cdev_add(&afu->cdev, afu->dev.devt, 1);
399 dev_err(&afu->dev, "Unable to add afu char device: %d\n", rc);
405 void ocxl_destroy_cdev(struct ocxl_afu *afu)
407 cdev_del(&afu->cdev);
410 int ocxl_register_afu(struct ocxl_afu *afu)
414 minor = allocate_afu_minor(afu);
417 afu->dev.devt = MKDEV(MAJOR(ocxl_dev), minor);
418 afu->dev.class = ocxl_class;
419 return device_register(&afu->dev);
422 void ocxl_unregister_afu(struct ocxl_afu *afu)
427 static char *ocxl_devnode(struct device *dev, umode_t *mode)
429 return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
432 int ocxl_file_init(void)
436 mutex_init(&minors_idr_lock);
437 idr_init(&minors_idr);
439 rc = alloc_chrdev_region(&ocxl_dev, 0, OCXL_NUM_MINORS, "ocxl");
441 pr_err("Unable to allocate ocxl major number: %d\n", rc);
445 ocxl_class = class_create(THIS_MODULE, "ocxl");
446 if (IS_ERR(ocxl_class)) {
447 pr_err("Unable to create ocxl class\n");
448 unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
449 return PTR_ERR(ocxl_class);
452 ocxl_class->devnode = ocxl_devnode;
456 void ocxl_file_exit(void)
458 class_destroy(ocxl_class);
459 unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
460 idr_destroy(&minors_idr);