uacce.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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/slab.h>
  8. #include <linux/uacce.h>
  9. static struct class *uacce_class;
  10. static dev_t uacce_devt;
  11. static DEFINE_XARRAY_ALLOC(uacce_xa);
  12. /*
  13. * If the parent driver or the device disappears, the queue state is invalid and
  14. * ops are not usable anymore.
  15. */
  16. static bool uacce_queue_is_valid(struct uacce_queue *q)
  17. {
  18. return q->state == UACCE_Q_INIT || q->state == UACCE_Q_STARTED;
  19. }
  20. static int uacce_start_queue(struct uacce_queue *q)
  21. {
  22. int ret;
  23. if (q->state != UACCE_Q_INIT)
  24. return -EINVAL;
  25. if (q->uacce->ops->start_queue) {
  26. ret = q->uacce->ops->start_queue(q);
  27. if (ret < 0)
  28. return ret;
  29. }
  30. q->state = UACCE_Q_STARTED;
  31. return 0;
  32. }
  33. static int uacce_put_queue(struct uacce_queue *q)
  34. {
  35. struct uacce_device *uacce = q->uacce;
  36. if ((q->state == UACCE_Q_STARTED) && uacce->ops->stop_queue)
  37. uacce->ops->stop_queue(q);
  38. if ((q->state == UACCE_Q_INIT || q->state == UACCE_Q_STARTED) &&
  39. uacce->ops->put_queue)
  40. uacce->ops->put_queue(q);
  41. q->state = UACCE_Q_ZOMBIE;
  42. return 0;
  43. }
  44. static long uacce_fops_unl_ioctl(struct file *filep,
  45. unsigned int cmd, unsigned long arg)
  46. {
  47. struct uacce_queue *q = filep->private_data;
  48. struct uacce_device *uacce = q->uacce;
  49. long ret = -ENXIO;
  50. /*
  51. * uacce->ops->ioctl() may take the mmap_lock when copying arg to/from
  52. * user. Avoid a circular lock dependency with uacce_fops_mmap(), which
  53. * gets called with mmap_lock held, by taking uacce->mutex instead of
  54. * q->mutex. Doing this in uacce_fops_mmap() is not possible because
  55. * uacce_fops_open() calls iommu_sva_bind_device(), which takes
  56. * mmap_lock, while holding uacce->mutex.
  57. */
  58. mutex_lock(&uacce->mutex);
  59. if (!uacce_queue_is_valid(q))
  60. goto out_unlock;
  61. switch (cmd) {
  62. case UACCE_CMD_START_Q:
  63. ret = uacce_start_queue(q);
  64. break;
  65. case UACCE_CMD_PUT_Q:
  66. ret = uacce_put_queue(q);
  67. break;
  68. default:
  69. if (uacce->ops->ioctl)
  70. ret = uacce->ops->ioctl(q, cmd, arg);
  71. else
  72. ret = -EINVAL;
  73. }
  74. out_unlock:
  75. mutex_unlock(&uacce->mutex);
  76. return ret;
  77. }
  78. #ifdef CONFIG_COMPAT
  79. static long uacce_fops_compat_ioctl(struct file *filep,
  80. unsigned int cmd, unsigned long arg)
  81. {
  82. arg = (unsigned long)compat_ptr(arg);
  83. return uacce_fops_unl_ioctl(filep, cmd, arg);
  84. }
  85. #endif
  86. static int uacce_bind_queue(struct uacce_device *uacce, struct uacce_queue *q)
  87. {
  88. u32 pasid;
  89. struct iommu_sva *handle;
  90. if (!(uacce->flags & UACCE_DEV_SVA))
  91. return 0;
  92. handle = iommu_sva_bind_device(uacce->parent, current->mm);
  93. if (IS_ERR(handle))
  94. return PTR_ERR(handle);
  95. pasid = iommu_sva_get_pasid(handle);
  96. if (pasid == IOMMU_PASID_INVALID) {
  97. iommu_sva_unbind_device(handle);
  98. return -ENODEV;
  99. }
  100. q->handle = handle;
  101. q->pasid = pasid;
  102. return 0;
  103. }
  104. static void uacce_unbind_queue(struct uacce_queue *q)
  105. {
  106. if (!q->handle)
  107. return;
  108. iommu_sva_unbind_device(q->handle);
  109. q->handle = NULL;
  110. }
  111. static int uacce_fops_open(struct inode *inode, struct file *filep)
  112. {
  113. struct uacce_device *uacce;
  114. struct uacce_queue *q;
  115. int ret;
  116. uacce = xa_load(&uacce_xa, iminor(inode));
  117. if (!uacce)
  118. return -ENODEV;
  119. q = kzalloc(sizeof(struct uacce_queue), GFP_KERNEL);
  120. if (!q)
  121. return -ENOMEM;
  122. mutex_lock(&uacce->mutex);
  123. if (!uacce->parent) {
  124. ret = -EINVAL;
  125. goto out_with_mem;
  126. }
  127. ret = uacce_bind_queue(uacce, q);
  128. if (ret)
  129. goto out_with_mem;
  130. q->uacce = uacce;
  131. if (uacce->ops->get_queue) {
  132. ret = uacce->ops->get_queue(uacce, q->pasid, q);
  133. if (ret < 0)
  134. goto out_with_bond;
  135. }
  136. init_waitqueue_head(&q->wait);
  137. filep->private_data = q;
  138. uacce->inode = inode;
  139. q->state = UACCE_Q_INIT;
  140. mutex_init(&q->mutex);
  141. list_add(&q->list, &uacce->queues);
  142. mutex_unlock(&uacce->mutex);
  143. return 0;
  144. out_with_bond:
  145. uacce_unbind_queue(q);
  146. out_with_mem:
  147. kfree(q);
  148. mutex_unlock(&uacce->mutex);
  149. return ret;
  150. }
  151. static int uacce_fops_release(struct inode *inode, struct file *filep)
  152. {
  153. struct uacce_queue *q = filep->private_data;
  154. struct uacce_device *uacce = q->uacce;
  155. mutex_lock(&uacce->mutex);
  156. uacce_put_queue(q);
  157. uacce_unbind_queue(q);
  158. list_del(&q->list);
  159. mutex_unlock(&uacce->mutex);
  160. kfree(q);
  161. return 0;
  162. }
  163. static void uacce_vma_close(struct vm_area_struct *vma)
  164. {
  165. struct uacce_queue *q = vma->vm_private_data;
  166. struct uacce_qfile_region *qfr = NULL;
  167. if (vma->vm_pgoff < UACCE_MAX_REGION)
  168. qfr = q->qfrs[vma->vm_pgoff];
  169. kfree(qfr);
  170. }
  171. static const struct vm_operations_struct uacce_vm_ops = {
  172. .close = uacce_vma_close,
  173. };
  174. static int uacce_fops_mmap(struct file *filep, struct vm_area_struct *vma)
  175. {
  176. struct uacce_queue *q = filep->private_data;
  177. struct uacce_device *uacce = q->uacce;
  178. struct uacce_qfile_region *qfr;
  179. enum uacce_qfrt type = UACCE_MAX_REGION;
  180. int ret = 0;
  181. if (vma->vm_pgoff < UACCE_MAX_REGION)
  182. type = vma->vm_pgoff;
  183. else
  184. return -EINVAL;
  185. qfr = kzalloc(sizeof(*qfr), GFP_KERNEL);
  186. if (!qfr)
  187. return -ENOMEM;
  188. vm_flags_set(vma, VM_DONTCOPY | VM_DONTEXPAND | VM_WIPEONFORK);
  189. vma->vm_ops = &uacce_vm_ops;
  190. vma->vm_private_data = q;
  191. qfr->type = type;
  192. mutex_lock(&q->mutex);
  193. if (!uacce_queue_is_valid(q)) {
  194. ret = -ENXIO;
  195. goto out_with_lock;
  196. }
  197. if (q->qfrs[type]) {
  198. ret = -EEXIST;
  199. goto out_with_lock;
  200. }
  201. switch (type) {
  202. case UACCE_QFRT_MMIO:
  203. case UACCE_QFRT_DUS:
  204. if (!uacce->ops->mmap) {
  205. ret = -EINVAL;
  206. goto out_with_lock;
  207. }
  208. ret = uacce->ops->mmap(q, vma, qfr);
  209. if (ret)
  210. goto out_with_lock;
  211. break;
  212. default:
  213. ret = -EINVAL;
  214. goto out_with_lock;
  215. }
  216. q->qfrs[type] = qfr;
  217. mutex_unlock(&q->mutex);
  218. return ret;
  219. out_with_lock:
  220. mutex_unlock(&q->mutex);
  221. kfree(qfr);
  222. return ret;
  223. }
  224. static __poll_t uacce_fops_poll(struct file *file, poll_table *wait)
  225. {
  226. struct uacce_queue *q = file->private_data;
  227. struct uacce_device *uacce = q->uacce;
  228. __poll_t ret = 0;
  229. mutex_lock(&q->mutex);
  230. if (!uacce_queue_is_valid(q))
  231. goto out_unlock;
  232. poll_wait(file, &q->wait, wait);
  233. if (uacce->ops->is_q_updated && uacce->ops->is_q_updated(q))
  234. ret = EPOLLIN | EPOLLRDNORM;
  235. out_unlock:
  236. mutex_unlock(&q->mutex);
  237. return ret;
  238. }
  239. static const struct file_operations uacce_fops = {
  240. .owner = THIS_MODULE,
  241. .open = uacce_fops_open,
  242. .release = uacce_fops_release,
  243. .unlocked_ioctl = uacce_fops_unl_ioctl,
  244. #ifdef CONFIG_COMPAT
  245. .compat_ioctl = uacce_fops_compat_ioctl,
  246. #endif
  247. .mmap = uacce_fops_mmap,
  248. .poll = uacce_fops_poll,
  249. };
  250. #define to_uacce_device(dev) container_of(dev, struct uacce_device, dev)
  251. static ssize_t api_show(struct device *dev,
  252. struct device_attribute *attr, char *buf)
  253. {
  254. struct uacce_device *uacce = to_uacce_device(dev);
  255. return sysfs_emit(buf, "%s\n", uacce->api_ver);
  256. }
  257. static ssize_t flags_show(struct device *dev,
  258. struct device_attribute *attr, char *buf)
  259. {
  260. struct uacce_device *uacce = to_uacce_device(dev);
  261. return sysfs_emit(buf, "%u\n", uacce->flags);
  262. }
  263. static ssize_t available_instances_show(struct device *dev,
  264. struct device_attribute *attr,
  265. char *buf)
  266. {
  267. struct uacce_device *uacce = to_uacce_device(dev);
  268. if (!uacce->ops->get_available_instances)
  269. return -ENODEV;
  270. return sysfs_emit(buf, "%d\n",
  271. uacce->ops->get_available_instances(uacce));
  272. }
  273. static ssize_t algorithms_show(struct device *dev,
  274. struct device_attribute *attr, char *buf)
  275. {
  276. struct uacce_device *uacce = to_uacce_device(dev);
  277. return sysfs_emit(buf, "%s\n", uacce->algs);
  278. }
  279. static ssize_t region_mmio_size_show(struct device *dev,
  280. struct device_attribute *attr, char *buf)
  281. {
  282. struct uacce_device *uacce = to_uacce_device(dev);
  283. return sysfs_emit(buf, "%lu\n",
  284. uacce->qf_pg_num[UACCE_QFRT_MMIO] << PAGE_SHIFT);
  285. }
  286. static ssize_t region_dus_size_show(struct device *dev,
  287. struct device_attribute *attr, char *buf)
  288. {
  289. struct uacce_device *uacce = to_uacce_device(dev);
  290. return sysfs_emit(buf, "%lu\n",
  291. uacce->qf_pg_num[UACCE_QFRT_DUS] << PAGE_SHIFT);
  292. }
  293. static DEVICE_ATTR_RO(api);
  294. static DEVICE_ATTR_RO(flags);
  295. static DEVICE_ATTR_RO(available_instances);
  296. static DEVICE_ATTR_RO(algorithms);
  297. static DEVICE_ATTR_RO(region_mmio_size);
  298. static DEVICE_ATTR_RO(region_dus_size);
  299. static struct attribute *uacce_dev_attrs[] = {
  300. &dev_attr_api.attr,
  301. &dev_attr_flags.attr,
  302. &dev_attr_available_instances.attr,
  303. &dev_attr_algorithms.attr,
  304. &dev_attr_region_mmio_size.attr,
  305. &dev_attr_region_dus_size.attr,
  306. NULL,
  307. };
  308. static umode_t uacce_dev_is_visible(struct kobject *kobj,
  309. struct attribute *attr, int n)
  310. {
  311. struct device *dev = kobj_to_dev(kobj);
  312. struct uacce_device *uacce = to_uacce_device(dev);
  313. if (((attr == &dev_attr_region_mmio_size.attr) &&
  314. (!uacce->qf_pg_num[UACCE_QFRT_MMIO])) ||
  315. ((attr == &dev_attr_region_dus_size.attr) &&
  316. (!uacce->qf_pg_num[UACCE_QFRT_DUS])))
  317. return 0;
  318. return attr->mode;
  319. }
  320. static struct attribute_group uacce_dev_group = {
  321. .is_visible = uacce_dev_is_visible,
  322. .attrs = uacce_dev_attrs,
  323. };
  324. __ATTRIBUTE_GROUPS(uacce_dev);
  325. static void uacce_release(struct device *dev)
  326. {
  327. struct uacce_device *uacce = to_uacce_device(dev);
  328. kfree(uacce);
  329. }
  330. static unsigned int uacce_enable_sva(struct device *parent, unsigned int flags)
  331. {
  332. int ret;
  333. if (!(flags & UACCE_DEV_SVA))
  334. return flags;
  335. flags &= ~UACCE_DEV_SVA;
  336. ret = iommu_dev_enable_feature(parent, IOMMU_DEV_FEAT_IOPF);
  337. if (ret) {
  338. dev_err(parent, "failed to enable IOPF feature! ret = %pe\n", ERR_PTR(ret));
  339. return flags;
  340. }
  341. ret = iommu_dev_enable_feature(parent, IOMMU_DEV_FEAT_SVA);
  342. if (ret) {
  343. dev_err(parent, "failed to enable SVA feature! ret = %pe\n", ERR_PTR(ret));
  344. iommu_dev_disable_feature(parent, IOMMU_DEV_FEAT_IOPF);
  345. return flags;
  346. }
  347. return flags | UACCE_DEV_SVA;
  348. }
  349. static void uacce_disable_sva(struct uacce_device *uacce)
  350. {
  351. if (!(uacce->flags & UACCE_DEV_SVA))
  352. return;
  353. iommu_dev_disable_feature(uacce->parent, IOMMU_DEV_FEAT_SVA);
  354. iommu_dev_disable_feature(uacce->parent, IOMMU_DEV_FEAT_IOPF);
  355. }
  356. /**
  357. * uacce_alloc() - alloc an accelerator
  358. * @parent: pointer of uacce parent device
  359. * @interface: pointer of uacce_interface for register
  360. *
  361. * Returns uacce pointer if success and ERR_PTR if not
  362. * Need check returned negotiated uacce->flags
  363. */
  364. struct uacce_device *uacce_alloc(struct device *parent,
  365. struct uacce_interface *interface)
  366. {
  367. unsigned int flags = interface->flags;
  368. struct uacce_device *uacce;
  369. int ret;
  370. uacce = kzalloc(sizeof(struct uacce_device), GFP_KERNEL);
  371. if (!uacce)
  372. return ERR_PTR(-ENOMEM);
  373. flags = uacce_enable_sva(parent, flags);
  374. uacce->parent = parent;
  375. uacce->flags = flags;
  376. uacce->ops = interface->ops;
  377. ret = xa_alloc(&uacce_xa, &uacce->dev_id, uacce, xa_limit_32b,
  378. GFP_KERNEL);
  379. if (ret < 0)
  380. goto err_with_uacce;
  381. INIT_LIST_HEAD(&uacce->queues);
  382. mutex_init(&uacce->mutex);
  383. device_initialize(&uacce->dev);
  384. uacce->dev.devt = MKDEV(MAJOR(uacce_devt), uacce->dev_id);
  385. uacce->dev.class = uacce_class;
  386. uacce->dev.groups = uacce_dev_groups;
  387. uacce->dev.parent = uacce->parent;
  388. uacce->dev.release = uacce_release;
  389. dev_set_name(&uacce->dev, "%s-%d", interface->name, uacce->dev_id);
  390. return uacce;
  391. err_with_uacce:
  392. uacce_disable_sva(uacce);
  393. kfree(uacce);
  394. return ERR_PTR(ret);
  395. }
  396. EXPORT_SYMBOL_GPL(uacce_alloc);
  397. /**
  398. * uacce_register() - add the accelerator to cdev and export to user space
  399. * @uacce: The initialized uacce device
  400. *
  401. * Return 0 if register succeeded, or an error.
  402. */
  403. int uacce_register(struct uacce_device *uacce)
  404. {
  405. if (!uacce)
  406. return -ENODEV;
  407. uacce->cdev = cdev_alloc();
  408. if (!uacce->cdev)
  409. return -ENOMEM;
  410. uacce->cdev->ops = &uacce_fops;
  411. uacce->cdev->owner = THIS_MODULE;
  412. return cdev_device_add(uacce->cdev, &uacce->dev);
  413. }
  414. EXPORT_SYMBOL_GPL(uacce_register);
  415. /**
  416. * uacce_remove() - remove the accelerator
  417. * @uacce: the accelerator to remove
  418. */
  419. void uacce_remove(struct uacce_device *uacce)
  420. {
  421. struct uacce_queue *q, *next_q;
  422. if (!uacce)
  423. return;
  424. /*
  425. * unmap remaining mapping from user space, preventing user still
  426. * access the mmaped area while parent device is already removed
  427. */
  428. if (uacce->inode)
  429. unmap_mapping_range(uacce->inode->i_mapping, 0, 0, 1);
  430. /*
  431. * uacce_fops_open() may be running concurrently, even after we remove
  432. * the cdev. Holding uacce->mutex ensures that open() does not obtain a
  433. * removed uacce device.
  434. */
  435. mutex_lock(&uacce->mutex);
  436. /* ensure no open queue remains */
  437. list_for_each_entry_safe(q, next_q, &uacce->queues, list) {
  438. /*
  439. * Taking q->mutex ensures that fops do not use the defunct
  440. * uacce->ops after the queue is disabled.
  441. */
  442. mutex_lock(&q->mutex);
  443. uacce_put_queue(q);
  444. mutex_unlock(&q->mutex);
  445. uacce_unbind_queue(q);
  446. }
  447. /* disable sva now since no opened queues */
  448. uacce_disable_sva(uacce);
  449. if (uacce->cdev)
  450. cdev_device_del(uacce->cdev, &uacce->dev);
  451. xa_erase(&uacce_xa, uacce->dev_id);
  452. /*
  453. * uacce exists as long as there are open fds, but ops will be freed
  454. * now. Ensure that bugs cause NULL deref rather than use-after-free.
  455. */
  456. uacce->ops = NULL;
  457. uacce->parent = NULL;
  458. mutex_unlock(&uacce->mutex);
  459. put_device(&uacce->dev);
  460. }
  461. EXPORT_SYMBOL_GPL(uacce_remove);
  462. static int __init uacce_init(void)
  463. {
  464. int ret;
  465. uacce_class = class_create(THIS_MODULE, UACCE_NAME);
  466. if (IS_ERR(uacce_class))
  467. return PTR_ERR(uacce_class);
  468. ret = alloc_chrdev_region(&uacce_devt, 0, MINORMASK, UACCE_NAME);
  469. if (ret)
  470. class_destroy(uacce_class);
  471. return ret;
  472. }
  473. static __exit void uacce_exit(void)
  474. {
  475. unregister_chrdev_region(uacce_devt, MINORMASK);
  476. class_destroy(uacce_class);
  477. }
  478. subsys_initcall(uacce_init);
  479. module_exit(uacce_exit);
  480. MODULE_LICENSE("GPL");
  481. MODULE_AUTHOR("HiSilicon Tech. Co., Ltd.");
  482. MODULE_DESCRIPTION("Accelerator interface for Userland applications");