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>
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
)
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
);
39 static int allocate_afu_minor(struct ocxl_afu
*afu
)
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
);
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
)
59 struct ocxl_context
*ctx
;
62 pr_debug("%s for device %x\n", __func__
, inode
->i_rdev
);
64 afu
= find_and_get_afu(inode
->i_rdev
);
68 ctx
= ocxl_context_alloc();
74 rc
= ocxl_context_init(ctx
, afu
, inode
->i_mapping
);
77 file
->private_data
= ctx
;
86 static long afu_ioctl_attach(struct ocxl_context
*ctx
,
87 struct ocxl_ioctl_attach __user
*uarg
)
89 struct ocxl_ioctl_attach arg
;
93 pr_debug("%s for context %d\n", __func__
, ctx
->pasid
);
95 if (copy_from_user(&arg
, uarg
, sizeof(arg
)))
98 /* Make sure reserved fields are not set for forward compatibility */
99 if (arg
.reserved1
|| arg
.reserved2
|| arg
.reserved3
)
102 amr
= arg
.amr
& mfspr(SPRN_UAMOR
);
103 rc
= ocxl_context_attach(ctx
, amr
);
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
));
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
)))
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
);
142 if (set_thread_tidr(current
)) {
143 mutex_unlock(&ctx
->status_mutex
);
147 ctx
->tidr
= current
->thread
.tidr
;
150 status
= ctx
->status
;
151 mutex_unlock(&ctx
->status_mutex
);
153 if (status
== ATTACHED
) {
155 struct link
*link
= ctx
->afu
->fn
->link
;
157 rc
= ocxl_link_update_pe(link
, ctx
->pasid
, ctx
->tidr
);
162 arg
.thread_id
= ctx
->tidr
;
166 if (copy_to_user(uarg
, &arg
, sizeof(arg
)))
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
));
182 if (cpu_has_feature(CPU_FTR_P9_TIDR
))
183 arg
.flags
[0] |= OCXL_IOCTL_FEATURES_FLAGS0_P9_WAIT
;
186 if (copy_to_user(uarg
, &arg
, sizeof(arg
)))
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" : \
201 static long afu_ioctl(struct file
*file
, unsigned int cmd
,
204 struct ocxl_context
*ctx
= file
->private_data
;
205 struct ocxl_ioctl_irq_fd irq_fd
;
209 pr_debug("%s for context %d, command %s\n", __func__
, ctx
->pasid
,
212 if (ctx
->status
== CLOSED
)
216 case OCXL_IOCTL_ATTACH
:
217 rc
= afu_ioctl_attach(ctx
,
218 (struct ocxl_ioctl_attach __user
*) args
);
221 case OCXL_IOCTL_IRQ_ALLOC
:
222 rc
= ocxl_afu_irq_alloc(ctx
, &irq_offset
);
224 rc
= copy_to_user((u64 __user
*) args
, &irq_offset
,
227 ocxl_afu_irq_free(ctx
, irq_offset
);
233 case OCXL_IOCTL_IRQ_FREE
:
234 rc
= copy_from_user(&irq_offset
, (u64 __user
*) args
,
238 rc
= ocxl_afu_irq_free(ctx
, irq_offset
);
241 case OCXL_IOCTL_IRQ_SET_FD
:
242 rc
= copy_from_user(&irq_fd
, (u64 __user
*) args
,
248 rc
= ocxl_afu_irq_set_fd(ctx
, irq_fd
.irq_offset
,
252 case OCXL_IOCTL_GET_METADATA
:
253 rc
= afu_ioctl_get_metadata(ctx
,
254 (struct ocxl_ioctl_metadata __user
*) args
);
258 case OCXL_IOCTL_ENABLE_P9_WAIT
:
259 rc
= afu_ioctl_enable_p9_wait(ctx
,
260 (struct ocxl_ioctl_p9_wait __user
*) args
);
264 case OCXL_IOCTL_GET_FEATURES
:
265 rc
= afu_ioctl_get_features(ctx
,
266 (struct ocxl_ioctl_features __user
*) args
);
275 static long afu_compat_ioctl(struct file
*file
, unsigned int cmd
,
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
)
293 mutex_lock(&ctx
->xsl_error_lock
);
294 ret
= !!ctx
->xsl_error
.addr
;
295 mutex_unlock(&ctx
->xsl_error_lock
);
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
))
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;
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
;
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
,
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
);
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
)))
374 #define AFU_EVENT_BODY_MAX_SIZE sizeof(struct ocxl_kernel_event_xsl_fault_error)
377 * Reports events on the AFU
379 * Header (struct ocxl_kernel_event_header)
380 * Body (struct ocxl_kernel_event_*)
383 static ssize_t
afu_read(struct file
*file
, char __user
*buf
, size_t count
,
386 struct ocxl_context
*ctx
= file
->private_data
;
387 struct ocxl_kernel_event_header header
;
390 DEFINE_WAIT(event_wait
);
392 memset(&header
, 0, sizeof(header
));
394 /* Require offset to be 0 */
398 if (count
< (sizeof(struct ocxl_kernel_event_header
) +
399 AFU_EVENT_BODY_MAX_SIZE
))
403 prepare_to_wait(&ctx
->events_wq
, &event_wait
,
406 if (afu_events_pending(ctx
))
409 if (ctx
->status
== CLOSED
)
412 if (file
->f_flags
& O_NONBLOCK
) {
413 finish_wait(&ctx
->events_wq
, &event_wait
);
417 if (signal_pending(current
)) {
418 finish_wait(&ctx
->events_wq
, &event_wait
);
425 finish_wait(&ctx
->events_wq
, &event_wait
);
427 if (has_xsl_error(ctx
)) {
428 used
= append_xsl_error(ctx
, &header
, buf
+ sizeof(header
));
433 if (!afu_events_pending(ctx
))
434 header
.flags
|= OCXL_KERNEL_EVENT_FLAG_LAST
;
436 if (copy_to_user(buf
, &header
, sizeof(header
)))
439 used
+= sizeof(header
);
445 static int afu_release(struct inode
*inode
, struct file
*file
)
447 struct ocxl_context
*ctx
= file
->private_data
;
450 pr_debug("%s for device %x\n", __func__
, inode
->i_rdev
);
451 rc
= ocxl_context_detach(ctx
);
452 mutex_lock(&ctx
->mapping_lock
);
454 mutex_unlock(&ctx
->mapping_lock
);
455 wake_up_all(&ctx
->events_wq
);
457 ocxl_context_free(ctx
);
461 static const struct file_operations ocxl_afu_fops
= {
462 .owner
= THIS_MODULE
,
464 .unlocked_ioctl
= afu_ioctl
,
465 .compat_ioctl
= afu_compat_ioctl
,
469 .release
= afu_release
,
472 int ocxl_create_cdev(struct ocxl_afu
*afu
)
476 cdev_init(&afu
->cdev
, &ocxl_afu_fops
);
477 rc
= cdev_add(&afu
->cdev
, afu
->dev
.devt
, 1);
479 dev_err(&afu
->dev
, "Unable to add afu char device: %d\n", rc
);
485 void ocxl_destroy_cdev(struct ocxl_afu
*afu
)
487 cdev_del(&afu
->cdev
);
490 int ocxl_register_afu(struct ocxl_afu
*afu
)
494 minor
= allocate_afu_minor(afu
);
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
)
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)
516 mutex_init(&minors_idr_lock
);
517 idr_init(&minors_idr
);
519 rc
= alloc_chrdev_region(&ocxl_dev
, 0, OCXL_NUM_MINORS
, "ocxl");
521 pr_err("Unable to allocate ocxl major number: %d\n", 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
;
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
);