uacce.c 14 KB

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