xsk: Add overflow check for u64 division, stored into u32
[linux/fpc-iii.git] / drivers / misc / ocxl / file.c
blobe6a607488f8afef0097d98e98cd60ac5b6a4ca0c
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
3 #include <linux/fs.h>
4 #include <linux/poll.h>
5 #include <linux/sched/signal.h>
6 #include <linux/uaccess.h>
7 #include <uapi/misc/ocxl.h>
8 #include <asm/reg.h>
9 #include <asm/switch_to.h>
10 #include "ocxl_internal.h"
13 #define OCXL_NUM_MINORS 256 /* Total to reserve */
15 static dev_t ocxl_dev;
16 static struct class *ocxl_class;
17 static struct mutex minors_idr_lock;
18 static struct idr minors_idr;
20 static struct ocxl_afu *find_and_get_afu(dev_t devno)
22 struct ocxl_afu *afu;
23 int afu_minor;
25 afu_minor = MINOR(devno);
27 * We don't declare an RCU critical section here, as our AFU
28 * is protected by a reference counter on the device. By the time the
29 * minor number of a device is removed from the idr, the ref count of
30 * the device is already at 0, so no user API will access that AFU and
31 * this function can't return it.
33 afu = idr_find(&minors_idr, afu_minor);
34 if (afu)
35 ocxl_afu_get(afu);
36 return afu;
39 static int allocate_afu_minor(struct ocxl_afu *afu)
41 int minor;
43 mutex_lock(&minors_idr_lock);
44 minor = idr_alloc(&minors_idr, afu, 0, OCXL_NUM_MINORS, GFP_KERNEL);
45 mutex_unlock(&minors_idr_lock);
46 return minor;
49 static void free_afu_minor(struct ocxl_afu *afu)
51 mutex_lock(&minors_idr_lock);
52 idr_remove(&minors_idr, MINOR(afu->dev.devt));
53 mutex_unlock(&minors_idr_lock);
56 static int afu_open(struct inode *inode, struct file *file)
58 struct ocxl_afu *afu;
59 struct ocxl_context *ctx;
60 int rc;
62 pr_debug("%s for device %x\n", __func__, inode->i_rdev);
64 afu = find_and_get_afu(inode->i_rdev);
65 if (!afu)
66 return -ENODEV;
68 ctx = ocxl_context_alloc();
69 if (!ctx) {
70 rc = -ENOMEM;
71 goto put_afu;
74 rc = ocxl_context_init(ctx, afu, inode->i_mapping);
75 if (rc)
76 goto put_afu;
77 file->private_data = ctx;
78 ocxl_afu_put(afu);
79 return 0;
81 put_afu:
82 ocxl_afu_put(afu);
83 return rc;
86 static long afu_ioctl_attach(struct ocxl_context *ctx,
87 struct ocxl_ioctl_attach __user *uarg)
89 struct ocxl_ioctl_attach arg;
90 u64 amr = 0;
91 int rc;
93 pr_debug("%s for context %d\n", __func__, ctx->pasid);
95 if (copy_from_user(&arg, uarg, sizeof(arg)))
96 return -EFAULT;
98 /* Make sure reserved fields are not set for forward compatibility */
99 if (arg.reserved1 || arg.reserved2 || arg.reserved3)
100 return -EINVAL;
102 amr = arg.amr & mfspr(SPRN_UAMOR);
103 rc = ocxl_context_attach(ctx, amr);
104 return rc;
107 static long afu_ioctl_get_metadata(struct ocxl_context *ctx,
108 struct ocxl_ioctl_metadata __user *uarg)
110 struct ocxl_ioctl_metadata arg;
112 memset(&arg, 0, sizeof(arg));
114 arg.version = 0;
116 arg.afu_version_major = ctx->afu->config.version_major;
117 arg.afu_version_minor = ctx->afu->config.version_minor;
118 arg.pasid = ctx->pasid;
119 arg.pp_mmio_size = ctx->afu->config.pp_mmio_stride;
120 arg.global_mmio_size = ctx->afu->config.global_mmio_size;
122 if (copy_to_user(uarg, &arg, sizeof(arg)))
123 return -EFAULT;
125 return 0;
128 #ifdef CONFIG_PPC64
129 static long afu_ioctl_enable_p9_wait(struct ocxl_context *ctx,
130 struct ocxl_ioctl_p9_wait __user *uarg)
132 struct ocxl_ioctl_p9_wait arg;
134 memset(&arg, 0, sizeof(arg));
136 if (cpu_has_feature(CPU_FTR_P9_TIDR)) {
137 enum ocxl_context_status status;
139 // Locks both status & tidr
140 mutex_lock(&ctx->status_mutex);
141 if (!ctx->tidr) {
142 if (set_thread_tidr(current)) {
143 mutex_unlock(&ctx->status_mutex);
144 return -ENOENT;
147 ctx->tidr = current->thread.tidr;
150 status = ctx->status;
151 mutex_unlock(&ctx->status_mutex);
153 if (status == ATTACHED) {
154 int rc;
155 struct link *link = ctx->afu->fn->link;
157 rc = ocxl_link_update_pe(link, ctx->pasid, ctx->tidr);
158 if (rc)
159 return rc;
162 arg.thread_id = ctx->tidr;
163 } else
164 return -ENOENT;
166 if (copy_to_user(uarg, &arg, sizeof(arg)))
167 return -EFAULT;
169 return 0;
171 #endif
174 static long afu_ioctl_get_features(struct ocxl_context *ctx,
175 struct ocxl_ioctl_features __user *uarg)
177 struct ocxl_ioctl_features arg;
179 memset(&arg, 0, sizeof(arg));
181 #ifdef CONFIG_PPC64
182 if (cpu_has_feature(CPU_FTR_P9_TIDR))
183 arg.flags[0] |= OCXL_IOCTL_FEATURES_FLAGS0_P9_WAIT;
184 #endif
186 if (copy_to_user(uarg, &arg, sizeof(arg)))
187 return -EFAULT;
189 return 0;
192 #define CMD_STR(x) (x == OCXL_IOCTL_ATTACH ? "ATTACH" : \
193 x == OCXL_IOCTL_IRQ_ALLOC ? "IRQ_ALLOC" : \
194 x == OCXL_IOCTL_IRQ_FREE ? "IRQ_FREE" : \
195 x == OCXL_IOCTL_IRQ_SET_FD ? "IRQ_SET_FD" : \
196 x == OCXL_IOCTL_GET_METADATA ? "GET_METADATA" : \
197 x == OCXL_IOCTL_ENABLE_P9_WAIT ? "ENABLE_P9_WAIT" : \
198 x == OCXL_IOCTL_GET_FEATURES ? "GET_FEATURES" : \
199 "UNKNOWN")
201 static long afu_ioctl(struct file *file, unsigned int cmd,
202 unsigned long args)
204 struct ocxl_context *ctx = file->private_data;
205 struct ocxl_ioctl_irq_fd irq_fd;
206 u64 irq_offset;
207 long rc;
209 pr_debug("%s for context %d, command %s\n", __func__, ctx->pasid,
210 CMD_STR(cmd));
212 if (ctx->status == CLOSED)
213 return -EIO;
215 switch (cmd) {
216 case OCXL_IOCTL_ATTACH:
217 rc = afu_ioctl_attach(ctx,
218 (struct ocxl_ioctl_attach __user *) args);
219 break;
221 case OCXL_IOCTL_IRQ_ALLOC:
222 rc = ocxl_afu_irq_alloc(ctx, &irq_offset);
223 if (!rc) {
224 rc = copy_to_user((u64 __user *) args, &irq_offset,
225 sizeof(irq_offset));
226 if (rc) {
227 ocxl_afu_irq_free(ctx, irq_offset);
228 return -EFAULT;
231 break;
233 case OCXL_IOCTL_IRQ_FREE:
234 rc = copy_from_user(&irq_offset, (u64 __user *) args,
235 sizeof(irq_offset));
236 if (rc)
237 return -EFAULT;
238 rc = ocxl_afu_irq_free(ctx, irq_offset);
239 break;
241 case OCXL_IOCTL_IRQ_SET_FD:
242 rc = copy_from_user(&irq_fd, (u64 __user *) args,
243 sizeof(irq_fd));
244 if (rc)
245 return -EFAULT;
246 if (irq_fd.reserved)
247 return -EINVAL;
248 rc = ocxl_afu_irq_set_fd(ctx, irq_fd.irq_offset,
249 irq_fd.eventfd);
250 break;
252 case OCXL_IOCTL_GET_METADATA:
253 rc = afu_ioctl_get_metadata(ctx,
254 (struct ocxl_ioctl_metadata __user *) args);
255 break;
257 #ifdef CONFIG_PPC64
258 case OCXL_IOCTL_ENABLE_P9_WAIT:
259 rc = afu_ioctl_enable_p9_wait(ctx,
260 (struct ocxl_ioctl_p9_wait __user *) args);
261 break;
262 #endif
264 case OCXL_IOCTL_GET_FEATURES:
265 rc = afu_ioctl_get_features(ctx,
266 (struct ocxl_ioctl_features __user *) args);
267 break;
269 default:
270 rc = -EINVAL;
272 return rc;
275 static long afu_compat_ioctl(struct file *file, unsigned int cmd,
276 unsigned long args)
278 return afu_ioctl(file, cmd, args);
281 static int afu_mmap(struct file *file, struct vm_area_struct *vma)
283 struct ocxl_context *ctx = file->private_data;
285 pr_debug("%s for context %d\n", __func__, ctx->pasid);
286 return ocxl_context_mmap(ctx, vma);
289 static bool has_xsl_error(struct ocxl_context *ctx)
291 bool ret;
293 mutex_lock(&ctx->xsl_error_lock);
294 ret = !!ctx->xsl_error.addr;
295 mutex_unlock(&ctx->xsl_error_lock);
297 return ret;
301 * Are there any events pending on the AFU
302 * ctx: The AFU context
303 * Returns: true if there are events pending
305 static bool afu_events_pending(struct ocxl_context *ctx)
307 if (has_xsl_error(ctx))
308 return true;
309 return false;
312 static unsigned int afu_poll(struct file *file, struct poll_table_struct *wait)
314 struct ocxl_context *ctx = file->private_data;
315 unsigned int mask = 0;
316 bool closed;
318 pr_debug("%s for context %d\n", __func__, ctx->pasid);
320 poll_wait(file, &ctx->events_wq, wait);
322 mutex_lock(&ctx->status_mutex);
323 closed = (ctx->status == CLOSED);
324 mutex_unlock(&ctx->status_mutex);
326 if (afu_events_pending(ctx))
327 mask = EPOLLIN | EPOLLRDNORM;
328 else if (closed)
329 mask = EPOLLERR;
331 return mask;
335 * Populate the supplied buffer with a single XSL error
336 * ctx: The AFU context to report the error from
337 * header: the event header to populate
338 * buf: The buffer to write the body into (should be at least
339 * AFU_EVENT_BODY_XSL_ERROR_SIZE)
340 * Return: the amount of buffer that was populated
342 static ssize_t append_xsl_error(struct ocxl_context *ctx,
343 struct ocxl_kernel_event_header *header,
344 char __user *buf)
346 struct ocxl_kernel_event_xsl_fault_error body;
348 memset(&body, 0, sizeof(body));
350 mutex_lock(&ctx->xsl_error_lock);
351 if (!ctx->xsl_error.addr) {
352 mutex_unlock(&ctx->xsl_error_lock);
353 return 0;
356 body.addr = ctx->xsl_error.addr;
357 body.dsisr = ctx->xsl_error.dsisr;
358 body.count = ctx->xsl_error.count;
360 ctx->xsl_error.addr = 0;
361 ctx->xsl_error.dsisr = 0;
362 ctx->xsl_error.count = 0;
364 mutex_unlock(&ctx->xsl_error_lock);
366 header->type = OCXL_AFU_EVENT_XSL_FAULT_ERROR;
368 if (copy_to_user(buf, &body, sizeof(body)))
369 return -EFAULT;
371 return sizeof(body);
374 #define AFU_EVENT_BODY_MAX_SIZE sizeof(struct ocxl_kernel_event_xsl_fault_error)
377 * Reports events on the AFU
378 * Format:
379 * Header (struct ocxl_kernel_event_header)
380 * Body (struct ocxl_kernel_event_*)
381 * Header...
383 static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
384 loff_t *off)
386 struct ocxl_context *ctx = file->private_data;
387 struct ocxl_kernel_event_header header;
388 ssize_t rc;
389 ssize_t used = 0;
390 DEFINE_WAIT(event_wait);
392 memset(&header, 0, sizeof(header));
394 /* Require offset to be 0 */
395 if (*off != 0)
396 return -EINVAL;
398 if (count < (sizeof(struct ocxl_kernel_event_header) +
399 AFU_EVENT_BODY_MAX_SIZE))
400 return -EINVAL;
402 for (;;) {
403 prepare_to_wait(&ctx->events_wq, &event_wait,
404 TASK_INTERRUPTIBLE);
406 if (afu_events_pending(ctx))
407 break;
409 if (ctx->status == CLOSED)
410 break;
412 if (file->f_flags & O_NONBLOCK) {
413 finish_wait(&ctx->events_wq, &event_wait);
414 return -EAGAIN;
417 if (signal_pending(current)) {
418 finish_wait(&ctx->events_wq, &event_wait);
419 return -ERESTARTSYS;
422 schedule();
425 finish_wait(&ctx->events_wq, &event_wait);
427 if (has_xsl_error(ctx)) {
428 used = append_xsl_error(ctx, &header, buf + sizeof(header));
429 if (used < 0)
430 return used;
433 if (!afu_events_pending(ctx))
434 header.flags |= OCXL_KERNEL_EVENT_FLAG_LAST;
436 if (copy_to_user(buf, &header, sizeof(header)))
437 return -EFAULT;
439 used += sizeof(header);
441 rc = used;
442 return rc;
445 static int afu_release(struct inode *inode, struct file *file)
447 struct ocxl_context *ctx = file->private_data;
448 int rc;
450 pr_debug("%s for device %x\n", __func__, inode->i_rdev);
451 rc = ocxl_context_detach(ctx);
452 mutex_lock(&ctx->mapping_lock);
453 ctx->mapping = NULL;
454 mutex_unlock(&ctx->mapping_lock);
455 wake_up_all(&ctx->events_wq);
456 if (rc != -EBUSY)
457 ocxl_context_free(ctx);
458 return 0;
461 static const struct file_operations ocxl_afu_fops = {
462 .owner = THIS_MODULE,
463 .open = afu_open,
464 .unlocked_ioctl = afu_ioctl,
465 .compat_ioctl = afu_compat_ioctl,
466 .mmap = afu_mmap,
467 .poll = afu_poll,
468 .read = afu_read,
469 .release = afu_release,
472 int ocxl_create_cdev(struct ocxl_afu *afu)
474 int rc;
476 cdev_init(&afu->cdev, &ocxl_afu_fops);
477 rc = cdev_add(&afu->cdev, afu->dev.devt, 1);
478 if (rc) {
479 dev_err(&afu->dev, "Unable to add afu char device: %d\n", rc);
480 return rc;
482 return 0;
485 void ocxl_destroy_cdev(struct ocxl_afu *afu)
487 cdev_del(&afu->cdev);
490 int ocxl_register_afu(struct ocxl_afu *afu)
492 int minor;
494 minor = allocate_afu_minor(afu);
495 if (minor < 0)
496 return minor;
497 afu->dev.devt = MKDEV(MAJOR(ocxl_dev), minor);
498 afu->dev.class = ocxl_class;
499 return device_register(&afu->dev);
502 void ocxl_unregister_afu(struct ocxl_afu *afu)
504 free_afu_minor(afu);
507 static char *ocxl_devnode(struct device *dev, umode_t *mode)
509 return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
512 int ocxl_file_init(void)
514 int rc;
516 mutex_init(&minors_idr_lock);
517 idr_init(&minors_idr);
519 rc = alloc_chrdev_region(&ocxl_dev, 0, OCXL_NUM_MINORS, "ocxl");
520 if (rc) {
521 pr_err("Unable to allocate ocxl major number: %d\n", rc);
522 return rc;
525 ocxl_class = class_create(THIS_MODULE, "ocxl");
526 if (IS_ERR(ocxl_class)) {
527 pr_err("Unable to create ocxl class\n");
528 unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
529 return PTR_ERR(ocxl_class);
532 ocxl_class->devnode = ocxl_devnode;
533 return 0;
536 void ocxl_file_exit(void)
538 class_destroy(ocxl_class);
539 unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
540 idr_destroy(&minors_idr);