uacce.c 15 KB

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