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 #define CMD_STR(x) (x == OCXL_IOCTL_ATTACH ? "ATTACH" : \
106 x == OCXL_IOCTL_IRQ_ALLOC ? "IRQ_ALLOC" : \
107 x == OCXL_IOCTL_IRQ_FREE ? "IRQ_FREE" : \
108 x == OCXL_IOCTL_IRQ_SET_FD ? "IRQ_SET_FD" : \
111 static long afu_ioctl(struct file *file, unsigned int cmd,
114 struct ocxl_context *ctx = file->private_data;
115 struct ocxl_ioctl_irq_fd irq_fd;
119 pr_debug("%s for context %d, command %s\n", __func__, ctx->pasid,
122 if (ctx->status == CLOSED)
126 case OCXL_IOCTL_ATTACH:
127 rc = afu_ioctl_attach(ctx,
128 (struct ocxl_ioctl_attach __user *) args);
131 case OCXL_IOCTL_IRQ_ALLOC:
132 rc = ocxl_afu_irq_alloc(ctx, &irq_offset);
134 rc = copy_to_user((u64 __user *) args, &irq_offset,
137 ocxl_afu_irq_free(ctx, irq_offset);
141 case OCXL_IOCTL_IRQ_FREE:
142 rc = copy_from_user(&irq_offset, (u64 __user *) args,
146 rc = ocxl_afu_irq_free(ctx, irq_offset);
149 case OCXL_IOCTL_IRQ_SET_FD:
150 rc = copy_from_user(&irq_fd, (u64 __user *) args,
156 rc = ocxl_afu_irq_set_fd(ctx, irq_fd.irq_offset,
166 static long afu_compat_ioctl(struct file *file, unsigned int cmd,
169 return afu_ioctl(file, cmd, args);
172 static int afu_mmap(struct file *file, struct vm_area_struct *vma)
174 struct ocxl_context *ctx = file->private_data;
176 pr_debug("%s for context %d\n", __func__, ctx->pasid);
177 return ocxl_context_mmap(ctx, vma);
180 static bool has_xsl_error(struct ocxl_context *ctx)
184 mutex_lock(&ctx->xsl_error_lock);
185 ret = !!ctx->xsl_error.addr;
186 mutex_unlock(&ctx->xsl_error_lock);
192 * Are there any events pending on the AFU
193 * ctx: The AFU context
194 * Returns: true if there are events pending
196 static bool afu_events_pending(struct ocxl_context *ctx)
198 if (has_xsl_error(ctx))
203 static unsigned int afu_poll(struct file *file, struct poll_table_struct *wait)
205 struct ocxl_context *ctx = file->private_data;
206 unsigned int mask = 0;
209 pr_debug("%s for context %d\n", __func__, ctx->pasid);
211 poll_wait(file, &ctx->events_wq, wait);
213 mutex_lock(&ctx->status_mutex);
214 closed = (ctx->status == CLOSED);
215 mutex_unlock(&ctx->status_mutex);
217 if (afu_events_pending(ctx))
218 mask = EPOLLIN | EPOLLRDNORM;
226 * Populate the supplied buffer with a single XSL error
227 * ctx: The AFU context to report the error from
228 * header: the event header to populate
229 * buf: The buffer to write the body into (should be at least
230 * AFU_EVENT_BODY_XSL_ERROR_SIZE)
231 * Return: the amount of buffer that was populated
233 static ssize_t append_xsl_error(struct ocxl_context *ctx,
234 struct ocxl_kernel_event_header *header,
237 struct ocxl_kernel_event_xsl_fault_error body;
239 memset(&body, 0, sizeof(body));
241 mutex_lock(&ctx->xsl_error_lock);
242 if (!ctx->xsl_error.addr) {
243 mutex_unlock(&ctx->xsl_error_lock);
247 body.addr = ctx->xsl_error.addr;
248 body.dsisr = ctx->xsl_error.dsisr;
249 body.count = ctx->xsl_error.count;
251 ctx->xsl_error.addr = 0;
252 ctx->xsl_error.dsisr = 0;
253 ctx->xsl_error.count = 0;
255 mutex_unlock(&ctx->xsl_error_lock);
257 header->type = OCXL_AFU_EVENT_XSL_FAULT_ERROR;
259 if (copy_to_user(buf, &body, sizeof(body)))
265 #define AFU_EVENT_BODY_MAX_SIZE sizeof(struct ocxl_kernel_event_xsl_fault_error)
268 * Reports events on the AFU
270 * Header (struct ocxl_kernel_event_header)
271 * Body (struct ocxl_kernel_event_*)
274 static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
277 struct ocxl_context *ctx = file->private_data;
278 struct ocxl_kernel_event_header header;
281 DEFINE_WAIT(event_wait);
283 memset(&header, 0, sizeof(header));
285 /* Require offset to be 0 */
289 if (count < (sizeof(struct ocxl_kernel_event_header) +
290 AFU_EVENT_BODY_MAX_SIZE))
294 prepare_to_wait(&ctx->events_wq, &event_wait,
297 if (afu_events_pending(ctx))
300 if (ctx->status == CLOSED)
303 if (file->f_flags & O_NONBLOCK) {
304 finish_wait(&ctx->events_wq, &event_wait);
308 if (signal_pending(current)) {
309 finish_wait(&ctx->events_wq, &event_wait);
316 finish_wait(&ctx->events_wq, &event_wait);
318 if (has_xsl_error(ctx)) {
319 used = append_xsl_error(ctx, &header, buf + sizeof(header));
324 if (!afu_events_pending(ctx))
325 header.flags |= OCXL_KERNEL_EVENT_FLAG_LAST;
327 if (copy_to_user(buf, &header, sizeof(header)))
330 used += sizeof(header);
336 static int afu_release(struct inode *inode, struct file *file)
338 struct ocxl_context *ctx = file->private_data;
341 pr_debug("%s for device %x\n", __func__, inode->i_rdev);
342 rc = ocxl_context_detach(ctx);
343 mutex_lock(&ctx->mapping_lock);
345 mutex_unlock(&ctx->mapping_lock);
346 wake_up_all(&ctx->events_wq);
348 ocxl_context_free(ctx);
352 static const struct file_operations ocxl_afu_fops = {
353 .owner = THIS_MODULE,
355 .unlocked_ioctl = afu_ioctl,
356 .compat_ioctl = afu_compat_ioctl,
360 .release = afu_release,
363 int ocxl_create_cdev(struct ocxl_afu *afu)
367 cdev_init(&afu->cdev, &ocxl_afu_fops);
368 rc = cdev_add(&afu->cdev, afu->dev.devt, 1);
370 dev_err(&afu->dev, "Unable to add afu char device: %d\n", rc);
376 void ocxl_destroy_cdev(struct ocxl_afu *afu)
378 cdev_del(&afu->cdev);
381 int ocxl_register_afu(struct ocxl_afu *afu)
385 minor = allocate_afu_minor(afu);
388 afu->dev.devt = MKDEV(MAJOR(ocxl_dev), minor);
389 afu->dev.class = ocxl_class;
390 return device_register(&afu->dev);
393 void ocxl_unregister_afu(struct ocxl_afu *afu)
398 static char *ocxl_devnode(struct device *dev, umode_t *mode)
400 return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
403 int ocxl_file_init(void)
407 mutex_init(&minors_idr_lock);
408 idr_init(&minors_idr);
410 rc = alloc_chrdev_region(&ocxl_dev, 0, OCXL_NUM_MINORS, "ocxl");
412 pr_err("Unable to allocate ocxl major number: %d\n", rc);
416 ocxl_class = class_create(THIS_MODULE, "ocxl");
417 if (IS_ERR(ocxl_class)) {
418 pr_err("Unable to create ocxl class\n");
419 unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
420 return PTR_ERR(ocxl_class);
423 ocxl_class->devnode = ocxl_devnode;
427 void ocxl_file_exit(void)
429 class_destroy(ocxl_class);
430 unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
431 idr_destroy(&minors_idr);