Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

uacce: add uacce driver

Uacce (Unified/User-space-access-intended Accelerator Framework) targets to
provide Shared Virtual Addressing (SVA) between accelerators and processes.
So accelerator can access any data structure of the main cpu.
This differs from the data sharing between cpu and io device, which share
only data content rather than address.
Since unified address, hardware and user space of process can share the
same virtual address in the communication.

Uacce create a chrdev for every registration, the queue is allocated to
the process when the chrdev is opened. Then the process can access the
hardware resource by interact with the queue file. By mmap the queue
file space to user space, the process can directly put requests to the
hardware without syscall to the kernel space.

The IOMMU core only tracks mm<->device bonds at the moment, because it
only needs to handle IOTLB invalidation and PASID table entries. However
uacce needs a finer granularity since multiple queues from the same
device can be bound to an mm. When the mm exits, all bound queues must
be stopped so that the IOMMU can safely clear the PASID table entry and
reallocate the PASID.

An intermediate struct uacce_mm links uacce devices and queues.
Note that an mm may be bound to multiple devices but an uacce_mm
structure only ever belongs to a single device, because we don't need
anything more complex (if multiple devices are bound to one mm, then
we'll create one uacce_mm for each bond).

uacce_device --+-- uacce_mm --+-- uacce_queue
| '-- uacce_queue
|
'-- uacce_mm --+-- uacce_queue
+-- uacce_queue
'-- uacce_queue

Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
Signed-off-by: Kenneth Lee <liguozhu@hisilicon.com>
Signed-off-by: Zaibo Xu <xuzaibo@huawei.com>
Signed-off-by: Zhou Wang <wangzhou1@hisilicon.com>
Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Signed-off-by: Zhangfei Gao <zhangfei.gao@linaro.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Kenneth Lee and committed by
Herbert Xu
015d239a aa017ab9

+872
+39
Documentation/ABI/testing/sysfs-driver-uacce
··· 1 + What: /sys/class/uacce/<dev_name>/api 2 + Date: Feb 2020 3 + KernelVersion: 5.7 4 + Contact: linux-accelerators@lists.ozlabs.org 5 + Description: Api of the device 6 + Can be any string and up to userspace to parse. 7 + Application use the api to match the correct driver 8 + 9 + What: /sys/class/uacce/<dev_name>/flags 10 + Date: Feb 2020 11 + KernelVersion: 5.7 12 + Contact: linux-accelerators@lists.ozlabs.org 13 + Description: Attributes of the device, see UACCE_DEV_xxx flag defined in uacce.h 14 + 15 + What: /sys/class/uacce/<dev_name>/available_instances 16 + Date: Feb 2020 17 + KernelVersion: 5.7 18 + Contact: linux-accelerators@lists.ozlabs.org 19 + Description: Available instances left of the device 20 + Return -ENODEV if uacce_ops get_available_instances is not provided 21 + 22 + What: /sys/class/uacce/<dev_name>/algorithms 23 + Date: Feb 2020 24 + KernelVersion: 5.7 25 + Contact: linux-accelerators@lists.ozlabs.org 26 + Description: Algorithms supported by this accelerator, separated by new line. 27 + Can be any string and up to userspace to parse. 28 + 29 + What: /sys/class/uacce/<dev_name>/region_mmio_size 30 + Date: Feb 2020 31 + KernelVersion: 5.7 32 + Contact: linux-accelerators@lists.ozlabs.org 33 + Description: Size (bytes) of mmio region queue file 34 + 35 + What: /sys/class/uacce/<dev_name>/region_dus_size 36 + Date: Feb 2020 37 + KernelVersion: 5.7 38 + Contact: linux-accelerators@lists.ozlabs.org 39 + Description: Size (bytes) of dus region queue file
+1
drivers/misc/Kconfig
··· 480 480 source "drivers/misc/ocxl/Kconfig" 481 481 source "drivers/misc/cardreader/Kconfig" 482 482 source "drivers/misc/habanalabs/Kconfig" 483 + source "drivers/misc/uacce/Kconfig" 483 484 endmenu
+1
drivers/misc/Makefile
··· 56 56 obj-y += cardreader/ 57 57 obj-$(CONFIG_PVPANIC) += pvpanic.o 58 58 obj-$(CONFIG_HABANA_AI) += habanalabs/ 59 + obj-$(CONFIG_UACCE) += uacce/ 59 60 obj-$(CONFIG_XILINX_SDFEC) += xilinx_sdfec.o
+13
drivers/misc/uacce/Kconfig
··· 1 + config UACCE 2 + tristate "Accelerator Framework for User Land" 3 + depends on IOMMU_API 4 + help 5 + UACCE provides interface for the user process to access the hardware 6 + without interaction with the kernel space in data path. 7 + 8 + The user-space interface is described in 9 + include/uapi/misc/uacce/uacce.h 10 + 11 + See Documentation/misc-devices/uacce.rst for more details. 12 + 13 + If you don't know what to do here, say N.
+2
drivers/misc/uacce/Makefile
··· 1 + # SPDX-License-Identifier: GPL-2.0-or-later 2 + obj-$(CONFIG_UACCE) += uacce.o
+617
drivers/misc/uacce/uacce.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-or-later 2 + #include <linux/compat.h> 3 + #include <linux/dma-mapping.h> 4 + #include <linux/iommu.h> 5 + #include <linux/module.h> 6 + #include <linux/poll.h> 7 + #include <linux/uacce.h> 8 + 9 + static struct class *uacce_class; 10 + static dev_t uacce_devt; 11 + static DEFINE_MUTEX(uacce_mutex); 12 + static DEFINE_XARRAY_ALLOC(uacce_xa); 13 + 14 + static int uacce_start_queue(struct uacce_queue *q) 15 + { 16 + int ret = 0; 17 + 18 + mutex_lock(&uacce_mutex); 19 + 20 + if (q->state != UACCE_Q_INIT) { 21 + ret = -EINVAL; 22 + goto out_with_lock; 23 + } 24 + 25 + if (q->uacce->ops->start_queue) { 26 + ret = q->uacce->ops->start_queue(q); 27 + if (ret < 0) 28 + goto out_with_lock; 29 + } 30 + 31 + q->state = UACCE_Q_STARTED; 32 + 33 + out_with_lock: 34 + mutex_unlock(&uacce_mutex); 35 + 36 + return ret; 37 + } 38 + 39 + static int uacce_put_queue(struct uacce_queue *q) 40 + { 41 + struct uacce_device *uacce = q->uacce; 42 + 43 + mutex_lock(&uacce_mutex); 44 + 45 + if (q->state == UACCE_Q_ZOMBIE) 46 + goto out; 47 + 48 + if ((q->state == UACCE_Q_STARTED) && uacce->ops->stop_queue) 49 + uacce->ops->stop_queue(q); 50 + 51 + if ((q->state == UACCE_Q_INIT || q->state == UACCE_Q_STARTED) && 52 + uacce->ops->put_queue) 53 + uacce->ops->put_queue(q); 54 + 55 + q->state = UACCE_Q_ZOMBIE; 56 + out: 57 + mutex_unlock(&uacce_mutex); 58 + 59 + return 0; 60 + } 61 + 62 + static long uacce_fops_unl_ioctl(struct file *filep, 63 + unsigned int cmd, unsigned long arg) 64 + { 65 + struct uacce_queue *q = filep->private_data; 66 + struct uacce_device *uacce = q->uacce; 67 + 68 + switch (cmd) { 69 + case UACCE_CMD_START_Q: 70 + return uacce_start_queue(q); 71 + 72 + case UACCE_CMD_PUT_Q: 73 + return uacce_put_queue(q); 74 + 75 + default: 76 + if (!uacce->ops->ioctl) 77 + return -EINVAL; 78 + 79 + return uacce->ops->ioctl(q, cmd, arg); 80 + } 81 + } 82 + 83 + #ifdef CONFIG_COMPAT 84 + static long uacce_fops_compat_ioctl(struct file *filep, 85 + unsigned int cmd, unsigned long arg) 86 + { 87 + arg = (unsigned long)compat_ptr(arg); 88 + 89 + return uacce_fops_unl_ioctl(filep, cmd, arg); 90 + } 91 + #endif 92 + 93 + static int uacce_sva_exit(struct device *dev, struct iommu_sva *handle, 94 + void *data) 95 + { 96 + struct uacce_mm *uacce_mm = data; 97 + struct uacce_queue *q; 98 + 99 + /* 100 + * No new queue can be added concurrently because no caller can have a 101 + * reference to this mm. But there may be concurrent calls to 102 + * uacce_mm_put(), so we need the lock. 103 + */ 104 + mutex_lock(&uacce_mm->lock); 105 + list_for_each_entry(q, &uacce_mm->queues, list) 106 + uacce_put_queue(q); 107 + uacce_mm->mm = NULL; 108 + mutex_unlock(&uacce_mm->lock); 109 + 110 + return 0; 111 + } 112 + 113 + static struct iommu_sva_ops uacce_sva_ops = { 114 + .mm_exit = uacce_sva_exit, 115 + }; 116 + 117 + static struct uacce_mm *uacce_mm_get(struct uacce_device *uacce, 118 + struct uacce_queue *q, 119 + struct mm_struct *mm) 120 + { 121 + struct uacce_mm *uacce_mm = NULL; 122 + struct iommu_sva *handle = NULL; 123 + int ret; 124 + 125 + lockdep_assert_held(&uacce->mm_lock); 126 + 127 + list_for_each_entry(uacce_mm, &uacce->mm_list, list) { 128 + if (uacce_mm->mm == mm) { 129 + mutex_lock(&uacce_mm->lock); 130 + list_add(&q->list, &uacce_mm->queues); 131 + mutex_unlock(&uacce_mm->lock); 132 + return uacce_mm; 133 + } 134 + } 135 + 136 + uacce_mm = kzalloc(sizeof(*uacce_mm), GFP_KERNEL); 137 + if (!uacce_mm) 138 + return NULL; 139 + 140 + if (uacce->flags & UACCE_DEV_SVA) { 141 + /* 142 + * Safe to pass an incomplete uacce_mm, since mm_exit cannot 143 + * fire while we hold a reference to the mm. 144 + */ 145 + handle = iommu_sva_bind_device(uacce->parent, mm, uacce_mm); 146 + if (IS_ERR(handle)) 147 + goto err_free; 148 + 149 + ret = iommu_sva_set_ops(handle, &uacce_sva_ops); 150 + if (ret) 151 + goto err_unbind; 152 + 153 + uacce_mm->pasid = iommu_sva_get_pasid(handle); 154 + if (uacce_mm->pasid == IOMMU_PASID_INVALID) 155 + goto err_unbind; 156 + } 157 + 158 + uacce_mm->mm = mm; 159 + uacce_mm->handle = handle; 160 + INIT_LIST_HEAD(&uacce_mm->queues); 161 + mutex_init(&uacce_mm->lock); 162 + list_add(&q->list, &uacce_mm->queues); 163 + list_add(&uacce_mm->list, &uacce->mm_list); 164 + 165 + return uacce_mm; 166 + 167 + err_unbind: 168 + if (handle) 169 + iommu_sva_unbind_device(handle); 170 + err_free: 171 + kfree(uacce_mm); 172 + return NULL; 173 + } 174 + 175 + static void uacce_mm_put(struct uacce_queue *q) 176 + { 177 + struct uacce_mm *uacce_mm = q->uacce_mm; 178 + 179 + lockdep_assert_held(&q->uacce->mm_lock); 180 + 181 + mutex_lock(&uacce_mm->lock); 182 + list_del(&q->list); 183 + mutex_unlock(&uacce_mm->lock); 184 + 185 + if (list_empty(&uacce_mm->queues)) { 186 + if (uacce_mm->handle) 187 + iommu_sva_unbind_device(uacce_mm->handle); 188 + list_del(&uacce_mm->list); 189 + kfree(uacce_mm); 190 + } 191 + } 192 + 193 + static int uacce_fops_open(struct inode *inode, struct file *filep) 194 + { 195 + struct uacce_mm *uacce_mm = NULL; 196 + struct uacce_device *uacce; 197 + struct uacce_queue *q; 198 + int ret = 0; 199 + 200 + uacce = xa_load(&uacce_xa, iminor(inode)); 201 + if (!uacce) 202 + return -ENODEV; 203 + 204 + q = kzalloc(sizeof(struct uacce_queue), GFP_KERNEL); 205 + if (!q) 206 + return -ENOMEM; 207 + 208 + mutex_lock(&uacce->mm_lock); 209 + uacce_mm = uacce_mm_get(uacce, q, current->mm); 210 + mutex_unlock(&uacce->mm_lock); 211 + if (!uacce_mm) { 212 + ret = -ENOMEM; 213 + goto out_with_mem; 214 + } 215 + 216 + q->uacce = uacce; 217 + q->uacce_mm = uacce_mm; 218 + 219 + if (uacce->ops->get_queue) { 220 + ret = uacce->ops->get_queue(uacce, uacce_mm->pasid, q); 221 + if (ret < 0) 222 + goto out_with_mm; 223 + } 224 + 225 + init_waitqueue_head(&q->wait); 226 + filep->private_data = q; 227 + q->state = UACCE_Q_INIT; 228 + 229 + return 0; 230 + 231 + out_with_mm: 232 + mutex_lock(&uacce->mm_lock); 233 + uacce_mm_put(q); 234 + mutex_unlock(&uacce->mm_lock); 235 + out_with_mem: 236 + kfree(q); 237 + return ret; 238 + } 239 + 240 + static int uacce_fops_release(struct inode *inode, struct file *filep) 241 + { 242 + struct uacce_queue *q = filep->private_data; 243 + struct uacce_device *uacce = q->uacce; 244 + 245 + uacce_put_queue(q); 246 + 247 + mutex_lock(&uacce->mm_lock); 248 + uacce_mm_put(q); 249 + mutex_unlock(&uacce->mm_lock); 250 + 251 + kfree(q); 252 + 253 + return 0; 254 + } 255 + 256 + static void uacce_vma_close(struct vm_area_struct *vma) 257 + { 258 + struct uacce_queue *q = vma->vm_private_data; 259 + struct uacce_qfile_region *qfr = NULL; 260 + 261 + if (vma->vm_pgoff < UACCE_MAX_REGION) 262 + qfr = q->qfrs[vma->vm_pgoff]; 263 + 264 + kfree(qfr); 265 + } 266 + 267 + static const struct vm_operations_struct uacce_vm_ops = { 268 + .close = uacce_vma_close, 269 + }; 270 + 271 + static int uacce_fops_mmap(struct file *filep, struct vm_area_struct *vma) 272 + { 273 + struct uacce_queue *q = filep->private_data; 274 + struct uacce_device *uacce = q->uacce; 275 + struct uacce_qfile_region *qfr; 276 + enum uacce_qfrt type = UACCE_MAX_REGION; 277 + int ret = 0; 278 + 279 + if (vma->vm_pgoff < UACCE_MAX_REGION) 280 + type = vma->vm_pgoff; 281 + else 282 + return -EINVAL; 283 + 284 + qfr = kzalloc(sizeof(*qfr), GFP_KERNEL); 285 + if (!qfr) 286 + return -ENOMEM; 287 + 288 + vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_WIPEONFORK; 289 + vma->vm_ops = &uacce_vm_ops; 290 + vma->vm_private_data = q; 291 + qfr->type = type; 292 + 293 + mutex_lock(&uacce_mutex); 294 + 295 + if (q->state != UACCE_Q_INIT && q->state != UACCE_Q_STARTED) { 296 + ret = -EINVAL; 297 + goto out_with_lock; 298 + } 299 + 300 + if (q->qfrs[type]) { 301 + ret = -EEXIST; 302 + goto out_with_lock; 303 + } 304 + 305 + switch (type) { 306 + case UACCE_QFRT_MMIO: 307 + if (!uacce->ops->mmap) { 308 + ret = -EINVAL; 309 + goto out_with_lock; 310 + } 311 + 312 + ret = uacce->ops->mmap(q, vma, qfr); 313 + if (ret) 314 + goto out_with_lock; 315 + 316 + break; 317 + 318 + case UACCE_QFRT_DUS: 319 + if (!uacce->ops->mmap) { 320 + ret = -EINVAL; 321 + goto out_with_lock; 322 + } 323 + 324 + ret = uacce->ops->mmap(q, vma, qfr); 325 + if (ret) 326 + goto out_with_lock; 327 + break; 328 + 329 + default: 330 + ret = -EINVAL; 331 + goto out_with_lock; 332 + } 333 + 334 + q->qfrs[type] = qfr; 335 + mutex_unlock(&uacce_mutex); 336 + 337 + return ret; 338 + 339 + out_with_lock: 340 + mutex_unlock(&uacce_mutex); 341 + kfree(qfr); 342 + return ret; 343 + } 344 + 345 + static __poll_t uacce_fops_poll(struct file *file, poll_table *wait) 346 + { 347 + struct uacce_queue *q = file->private_data; 348 + struct uacce_device *uacce = q->uacce; 349 + 350 + poll_wait(file, &q->wait, wait); 351 + if (uacce->ops->is_q_updated && uacce->ops->is_q_updated(q)) 352 + return EPOLLIN | EPOLLRDNORM; 353 + 354 + return 0; 355 + } 356 + 357 + static const struct file_operations uacce_fops = { 358 + .owner = THIS_MODULE, 359 + .open = uacce_fops_open, 360 + .release = uacce_fops_release, 361 + .unlocked_ioctl = uacce_fops_unl_ioctl, 362 + #ifdef CONFIG_COMPAT 363 + .compat_ioctl = uacce_fops_compat_ioctl, 364 + #endif 365 + .mmap = uacce_fops_mmap, 366 + .poll = uacce_fops_poll, 367 + }; 368 + 369 + #define to_uacce_device(dev) container_of(dev, struct uacce_device, dev) 370 + 371 + static ssize_t api_show(struct device *dev, 372 + struct device_attribute *attr, char *buf) 373 + { 374 + struct uacce_device *uacce = to_uacce_device(dev); 375 + 376 + return sprintf(buf, "%s\n", uacce->api_ver); 377 + } 378 + 379 + static ssize_t flags_show(struct device *dev, 380 + struct device_attribute *attr, char *buf) 381 + { 382 + struct uacce_device *uacce = to_uacce_device(dev); 383 + 384 + return sprintf(buf, "%u\n", uacce->flags); 385 + } 386 + 387 + static ssize_t available_instances_show(struct device *dev, 388 + struct device_attribute *attr, 389 + char *buf) 390 + { 391 + struct uacce_device *uacce = to_uacce_device(dev); 392 + 393 + if (!uacce->ops->get_available_instances) 394 + return -ENODEV; 395 + 396 + return sprintf(buf, "%d\n", 397 + uacce->ops->get_available_instances(uacce)); 398 + } 399 + 400 + static ssize_t algorithms_show(struct device *dev, 401 + struct device_attribute *attr, char *buf) 402 + { 403 + struct uacce_device *uacce = to_uacce_device(dev); 404 + 405 + return sprintf(buf, "%s\n", uacce->algs); 406 + } 407 + 408 + static ssize_t region_mmio_size_show(struct device *dev, 409 + struct device_attribute *attr, char *buf) 410 + { 411 + struct uacce_device *uacce = to_uacce_device(dev); 412 + 413 + return sprintf(buf, "%lu\n", 414 + uacce->qf_pg_num[UACCE_QFRT_MMIO] << PAGE_SHIFT); 415 + } 416 + 417 + static ssize_t region_dus_size_show(struct device *dev, 418 + struct device_attribute *attr, char *buf) 419 + { 420 + struct uacce_device *uacce = to_uacce_device(dev); 421 + 422 + return sprintf(buf, "%lu\n", 423 + uacce->qf_pg_num[UACCE_QFRT_DUS] << PAGE_SHIFT); 424 + } 425 + 426 + static DEVICE_ATTR_RO(api); 427 + static DEVICE_ATTR_RO(flags); 428 + static DEVICE_ATTR_RO(available_instances); 429 + static DEVICE_ATTR_RO(algorithms); 430 + static DEVICE_ATTR_RO(region_mmio_size); 431 + static DEVICE_ATTR_RO(region_dus_size); 432 + 433 + static struct attribute *uacce_dev_attrs[] = { 434 + &dev_attr_api.attr, 435 + &dev_attr_flags.attr, 436 + &dev_attr_available_instances.attr, 437 + &dev_attr_algorithms.attr, 438 + &dev_attr_region_mmio_size.attr, 439 + &dev_attr_region_dus_size.attr, 440 + NULL, 441 + }; 442 + 443 + static umode_t uacce_dev_is_visible(struct kobject *kobj, 444 + struct attribute *attr, int n) 445 + { 446 + struct device *dev = container_of(kobj, struct device, kobj); 447 + struct uacce_device *uacce = to_uacce_device(dev); 448 + 449 + if (((attr == &dev_attr_region_mmio_size.attr) && 450 + (!uacce->qf_pg_num[UACCE_QFRT_MMIO])) || 451 + ((attr == &dev_attr_region_dus_size.attr) && 452 + (!uacce->qf_pg_num[UACCE_QFRT_DUS]))) 453 + return 0; 454 + 455 + return attr->mode; 456 + } 457 + 458 + static struct attribute_group uacce_dev_group = { 459 + .is_visible = uacce_dev_is_visible, 460 + .attrs = uacce_dev_attrs, 461 + }; 462 + 463 + __ATTRIBUTE_GROUPS(uacce_dev); 464 + 465 + static void uacce_release(struct device *dev) 466 + { 467 + struct uacce_device *uacce = to_uacce_device(dev); 468 + 469 + kfree(uacce); 470 + } 471 + 472 + /** 473 + * uacce_alloc() - alloc an accelerator 474 + * @parent: pointer of uacce parent device 475 + * @interface: pointer of uacce_interface for register 476 + * 477 + * Returns uacce pointer if success and ERR_PTR if not 478 + * Need check returned negotiated uacce->flags 479 + */ 480 + struct uacce_device *uacce_alloc(struct device *parent, 481 + struct uacce_interface *interface) 482 + { 483 + unsigned int flags = interface->flags; 484 + struct uacce_device *uacce; 485 + int ret; 486 + 487 + uacce = kzalloc(sizeof(struct uacce_device), GFP_KERNEL); 488 + if (!uacce) 489 + return ERR_PTR(-ENOMEM); 490 + 491 + if (flags & UACCE_DEV_SVA) { 492 + ret = iommu_dev_enable_feature(parent, IOMMU_DEV_FEAT_SVA); 493 + if (ret) 494 + flags &= ~UACCE_DEV_SVA; 495 + } 496 + 497 + uacce->parent = parent; 498 + uacce->flags = flags; 499 + uacce->ops = interface->ops; 500 + 501 + ret = xa_alloc(&uacce_xa, &uacce->dev_id, uacce, xa_limit_32b, 502 + GFP_KERNEL); 503 + if (ret < 0) 504 + goto err_with_uacce; 505 + 506 + INIT_LIST_HEAD(&uacce->mm_list); 507 + mutex_init(&uacce->mm_lock); 508 + device_initialize(&uacce->dev); 509 + uacce->dev.devt = MKDEV(MAJOR(uacce_devt), uacce->dev_id); 510 + uacce->dev.class = uacce_class; 511 + uacce->dev.groups = uacce_dev_groups; 512 + uacce->dev.parent = uacce->parent; 513 + uacce->dev.release = uacce_release; 514 + dev_set_name(&uacce->dev, "%s-%d", interface->name, uacce->dev_id); 515 + 516 + return uacce; 517 + 518 + err_with_uacce: 519 + if (flags & UACCE_DEV_SVA) 520 + iommu_dev_disable_feature(uacce->parent, IOMMU_DEV_FEAT_SVA); 521 + kfree(uacce); 522 + return ERR_PTR(ret); 523 + } 524 + EXPORT_SYMBOL_GPL(uacce_alloc); 525 + 526 + /** 527 + * uacce_register() - add the accelerator to cdev and export to user space 528 + * @uacce: The initialized uacce device 529 + * 530 + * Return 0 if register succeeded, or an error. 531 + */ 532 + int uacce_register(struct uacce_device *uacce) 533 + { 534 + if (!uacce) 535 + return -ENODEV; 536 + 537 + uacce->cdev = cdev_alloc(); 538 + if (!uacce->cdev) 539 + return -ENOMEM; 540 + 541 + uacce->cdev->ops = &uacce_fops; 542 + uacce->cdev->owner = THIS_MODULE; 543 + 544 + return cdev_device_add(uacce->cdev, &uacce->dev); 545 + } 546 + EXPORT_SYMBOL_GPL(uacce_register); 547 + 548 + /** 549 + * uacce_remove() - remove the accelerator 550 + * @uacce: the accelerator to remove 551 + */ 552 + void uacce_remove(struct uacce_device *uacce) 553 + { 554 + struct uacce_mm *uacce_mm; 555 + struct uacce_queue *q; 556 + 557 + if (!uacce) 558 + return; 559 + 560 + /* ensure no open queue remains */ 561 + mutex_lock(&uacce->mm_lock); 562 + list_for_each_entry(uacce_mm, &uacce->mm_list, list) { 563 + /* 564 + * We don't take the uacce_mm->lock here. Since we hold the 565 + * device's mm_lock, no queue can be added to or removed from 566 + * this uacce_mm. We may run concurrently with mm_exit, but 567 + * uacce_put_queue() is serialized and iommu_sva_unbind_device() 568 + * waits for the lock that mm_exit is holding. 569 + */ 570 + list_for_each_entry(q, &uacce_mm->queues, list) 571 + uacce_put_queue(q); 572 + 573 + if (uacce->flags & UACCE_DEV_SVA) { 574 + iommu_sva_unbind_device(uacce_mm->handle); 575 + uacce_mm->handle = NULL; 576 + } 577 + } 578 + mutex_unlock(&uacce->mm_lock); 579 + 580 + /* disable sva now since no opened queues */ 581 + if (uacce->flags & UACCE_DEV_SVA) 582 + iommu_dev_disable_feature(uacce->parent, IOMMU_DEV_FEAT_SVA); 583 + 584 + if (uacce->cdev) 585 + cdev_device_del(uacce->cdev, &uacce->dev); 586 + xa_erase(&uacce_xa, uacce->dev_id); 587 + put_device(&uacce->dev); 588 + } 589 + EXPORT_SYMBOL_GPL(uacce_remove); 590 + 591 + static int __init uacce_init(void) 592 + { 593 + int ret; 594 + 595 + uacce_class = class_create(THIS_MODULE, UACCE_NAME); 596 + if (IS_ERR(uacce_class)) 597 + return PTR_ERR(uacce_class); 598 + 599 + ret = alloc_chrdev_region(&uacce_devt, 0, MINORMASK, UACCE_NAME); 600 + if (ret) 601 + class_destroy(uacce_class); 602 + 603 + return ret; 604 + } 605 + 606 + static __exit void uacce_exit(void) 607 + { 608 + unregister_chrdev_region(uacce_devt, MINORMASK); 609 + class_destroy(uacce_class); 610 + } 611 + 612 + subsys_initcall(uacce_init); 613 + module_exit(uacce_exit); 614 + 615 + MODULE_LICENSE("GPL"); 616 + MODULE_AUTHOR("Hisilicon Tech. Co., Ltd."); 617 + MODULE_DESCRIPTION("Accelerator interface for Userland applications");
+161
include/linux/uacce.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 + #ifndef _LINUX_UACCE_H 3 + #define _LINUX_UACCE_H 4 + 5 + #include <linux/cdev.h> 6 + #include <uapi/misc/uacce/uacce.h> 7 + 8 + #define UACCE_NAME "uacce" 9 + #define UACCE_MAX_REGION 2 10 + #define UACCE_MAX_NAME_SIZE 64 11 + 12 + struct uacce_queue; 13 + struct uacce_device; 14 + 15 + /** 16 + * struct uacce_qfile_region - structure of queue file region 17 + * @type: type of the region 18 + */ 19 + struct uacce_qfile_region { 20 + enum uacce_qfrt type; 21 + }; 22 + 23 + /** 24 + * struct uacce_ops - uacce device operations 25 + * @get_available_instances: get available instances left of the device 26 + * @get_queue: get a queue from the device 27 + * @put_queue: free a queue to the device 28 + * @start_queue: make the queue start work after get_queue 29 + * @stop_queue: make the queue stop work before put_queue 30 + * @is_q_updated: check whether the task is finished 31 + * @mmap: mmap addresses of queue to user space 32 + * @ioctl: ioctl for user space users of the queue 33 + */ 34 + struct uacce_ops { 35 + int (*get_available_instances)(struct uacce_device *uacce); 36 + int (*get_queue)(struct uacce_device *uacce, unsigned long arg, 37 + struct uacce_queue *q); 38 + void (*put_queue)(struct uacce_queue *q); 39 + int (*start_queue)(struct uacce_queue *q); 40 + void (*stop_queue)(struct uacce_queue *q); 41 + int (*is_q_updated)(struct uacce_queue *q); 42 + int (*mmap)(struct uacce_queue *q, struct vm_area_struct *vma, 43 + struct uacce_qfile_region *qfr); 44 + long (*ioctl)(struct uacce_queue *q, unsigned int cmd, 45 + unsigned long arg); 46 + }; 47 + 48 + /** 49 + * struct uacce_interface - interface required for uacce_register() 50 + * @name: the uacce device name. Will show up in sysfs 51 + * @flags: uacce device attributes 52 + * @ops: pointer to the struct uacce_ops 53 + */ 54 + struct uacce_interface { 55 + char name[UACCE_MAX_NAME_SIZE]; 56 + unsigned int flags; 57 + const struct uacce_ops *ops; 58 + }; 59 + 60 + enum uacce_q_state { 61 + UACCE_Q_ZOMBIE = 0, 62 + UACCE_Q_INIT, 63 + UACCE_Q_STARTED, 64 + }; 65 + 66 + /** 67 + * struct uacce_queue 68 + * @uacce: pointer to uacce 69 + * @priv: private pointer 70 + * @wait: wait queue head 71 + * @list: index into uacce_mm 72 + * @uacce_mm: the corresponding mm 73 + * @qfrs: pointer of qfr regions 74 + * @state: queue state machine 75 + */ 76 + struct uacce_queue { 77 + struct uacce_device *uacce; 78 + void *priv; 79 + wait_queue_head_t wait; 80 + struct list_head list; 81 + struct uacce_mm *uacce_mm; 82 + struct uacce_qfile_region *qfrs[UACCE_MAX_REGION]; 83 + enum uacce_q_state state; 84 + }; 85 + 86 + /** 87 + * struct uacce_device 88 + * @algs: supported algorithms 89 + * @api_ver: api version 90 + * @ops: pointer to the struct uacce_ops 91 + * @qf_pg_num: page numbers of the queue file regions 92 + * @parent: pointer to the parent device 93 + * @is_vf: whether virtual function 94 + * @flags: uacce attributes 95 + * @dev_id: id of the uacce device 96 + * @cdev: cdev of the uacce 97 + * @dev: dev of the uacce 98 + * @priv: private pointer of the uacce 99 + * @mm_list: list head of uacce_mm->list 100 + * @mm_lock: lock for mm_list 101 + */ 102 + struct uacce_device { 103 + const char *algs; 104 + const char *api_ver; 105 + const struct uacce_ops *ops; 106 + unsigned long qf_pg_num[UACCE_MAX_REGION]; 107 + struct device *parent; 108 + bool is_vf; 109 + u32 flags; 110 + u32 dev_id; 111 + struct cdev *cdev; 112 + struct device dev; 113 + void *priv; 114 + struct list_head mm_list; 115 + struct mutex mm_lock; 116 + }; 117 + 118 + /** 119 + * struct uacce_mm - keep track of queues bound to a process 120 + * @list: index into uacce_device 121 + * @queues: list of queues 122 + * @mm: the mm struct 123 + * @lock: protects the list of queues 124 + * @pasid: pasid of the uacce_mm 125 + * @handle: iommu_sva handle return from iommu_sva_bind_device 126 + */ 127 + struct uacce_mm { 128 + struct list_head list; 129 + struct list_head queues; 130 + struct mm_struct *mm; 131 + struct mutex lock; 132 + int pasid; 133 + struct iommu_sva *handle; 134 + }; 135 + 136 + #if IS_ENABLED(CONFIG_UACCE) 137 + 138 + struct uacce_device *uacce_alloc(struct device *parent, 139 + struct uacce_interface *interface); 140 + int uacce_register(struct uacce_device *uacce); 141 + void uacce_remove(struct uacce_device *uacce); 142 + 143 + #else /* CONFIG_UACCE */ 144 + 145 + static inline 146 + struct uacce_device *uacce_alloc(struct device *parent, 147 + struct uacce_interface *interface) 148 + { 149 + return ERR_PTR(-ENODEV); 150 + } 151 + 152 + static inline int uacce_register(struct uacce_device *uacce) 153 + { 154 + return -EINVAL; 155 + } 156 + 157 + static inline void uacce_remove(struct uacce_device *uacce) {} 158 + 159 + #endif /* CONFIG_UACCE */ 160 + 161 + #endif /* _LINUX_UACCE_H */
+38
include/uapi/misc/uacce/uacce.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0+ WITH Linux-syscall-note */ 2 + #ifndef _UAPIUUACCE_H 3 + #define _UAPIUUACCE_H 4 + 5 + #include <linux/types.h> 6 + #include <linux/ioctl.h> 7 + 8 + /* 9 + * UACCE_CMD_START_Q: Start queue 10 + */ 11 + #define UACCE_CMD_START_Q _IO('W', 0) 12 + 13 + /* 14 + * UACCE_CMD_PUT_Q: 15 + * User actively stop queue and free queue resource immediately 16 + * Optimization method since close fd may delay 17 + */ 18 + #define UACCE_CMD_PUT_Q _IO('W', 1) 19 + 20 + /* 21 + * UACCE Device flags: 22 + * UACCE_DEV_SVA: Shared Virtual Addresses 23 + * Support PASID 24 + * Support device page faults (PCI PRI or SMMU Stall) 25 + */ 26 + #define UACCE_DEV_SVA BIT(0) 27 + 28 + /** 29 + * enum uacce_qfrt: queue file region type 30 + * @UACCE_QFRT_MMIO: device mmio region 31 + * @UACCE_QFRT_DUS: device user share region 32 + */ 33 + enum uacce_qfrt { 34 + UACCE_QFRT_MMIO = 0, 35 + UACCE_QFRT_DUS = 1, 36 + }; 37 + 38 + #endif