file.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // SPDX-License-Identifier: GPL-2.0+
  2. // Copyright 2017 IBM Corp.
  3. #include <linux/fs.h>
  4. #include <linux/poll.h>
  5. #include <linux/sched/signal.h>
  6. #include <linux/uaccess.h>
  7. #include <uapi/misc/ocxl.h>
  8. #include "ocxl_internal.h"
  9. #define OCXL_NUM_MINORS 256 /* Total to reserve */
  10. static dev_t ocxl_dev;
  11. static struct class *ocxl_class;
  12. static struct mutex minors_idr_lock;
  13. static struct idr minors_idr;
  14. static struct ocxl_afu *find_and_get_afu(dev_t devno)
  15. {
  16. struct ocxl_afu *afu;
  17. int afu_minor;
  18. afu_minor = MINOR(devno);
  19. /*
  20. * We don't declare an RCU critical section here, as our AFU
  21. * is protected by a reference counter on the device. By the time the
  22. * minor number of a device is removed from the idr, the ref count of
  23. * the device is already at 0, so no user API will access that AFU and
  24. * this function can't return it.
  25. */
  26. afu = idr_find(&minors_idr, afu_minor);
  27. if (afu)
  28. ocxl_afu_get(afu);
  29. return afu;
  30. }
  31. static int allocate_afu_minor(struct ocxl_afu *afu)
  32. {
  33. int minor;
  34. mutex_lock(&minors_idr_lock);
  35. minor = idr_alloc(&minors_idr, afu, 0, OCXL_NUM_MINORS, GFP_KERNEL);
  36. mutex_unlock(&minors_idr_lock);
  37. return minor;
  38. }
  39. static void free_afu_minor(struct ocxl_afu *afu)
  40. {
  41. mutex_lock(&minors_idr_lock);
  42. idr_remove(&minors_idr, MINOR(afu->dev.devt));
  43. mutex_unlock(&minors_idr_lock);
  44. }
  45. static int afu_open(struct inode *inode, struct file *file)
  46. {
  47. struct ocxl_afu *afu;
  48. struct ocxl_context *ctx;
  49. int rc;
  50. pr_debug("%s for device %x\n", __func__, inode->i_rdev);
  51. afu = find_and_get_afu(inode->i_rdev);
  52. if (!afu)
  53. return -ENODEV;
  54. ctx = ocxl_context_alloc();
  55. if (!ctx) {
  56. rc = -ENOMEM;
  57. goto put_afu;
  58. }
  59. rc = ocxl_context_init(ctx, afu, inode->i_mapping);
  60. if (rc)
  61. goto put_afu;
  62. file->private_data = ctx;
  63. ocxl_afu_put(afu);
  64. return 0;
  65. put_afu:
  66. ocxl_afu_put(afu);
  67. return rc;
  68. }
  69. static long afu_ioctl_attach(struct ocxl_context *ctx,
  70. struct ocxl_ioctl_attach __user *uarg)
  71. {
  72. struct ocxl_ioctl_attach arg;
  73. u64 amr = 0;
  74. int rc;
  75. pr_debug("%s for context %d\n", __func__, ctx->pasid);
  76. if (copy_from_user(&arg, uarg, sizeof(arg)))
  77. return -EFAULT;
  78. /* Make sure reserved fields are not set for forward compatibility */
  79. if (arg.reserved1 || arg.reserved2 || arg.reserved3)
  80. return -EINVAL;
  81. amr = arg.amr & mfspr(SPRN_UAMOR);
  82. rc = ocxl_context_attach(ctx, amr);
  83. return rc;
  84. }
  85. static long afu_ioctl_get_metadata(struct ocxl_context *ctx,
  86. struct ocxl_ioctl_metadata __user *uarg)
  87. {
  88. struct ocxl_ioctl_metadata arg;
  89. memset(&arg, 0, sizeof(arg));
  90. arg.version = 0;
  91. arg.afu_version_major = ctx->afu->config.version_major;
  92. arg.afu_version_minor = ctx->afu->config.version_minor;
  93. arg.pasid = ctx->pasid;
  94. arg.pp_mmio_size = ctx->afu->config.pp_mmio_stride;
  95. arg.global_mmio_size = ctx->afu->config.global_mmio_size;
  96. if (copy_to_user(uarg, &arg, sizeof(arg)))
  97. return -EFAULT;
  98. return 0;
  99. }
  100. #define CMD_STR(x) (x == OCXL_IOCTL_ATTACH ? "ATTACH" : \
  101. x == OCXL_IOCTL_IRQ_ALLOC ? "IRQ_ALLOC" : \
  102. x == OCXL_IOCTL_IRQ_FREE ? "IRQ_FREE" : \
  103. x == OCXL_IOCTL_IRQ_SET_FD ? "IRQ_SET_FD" : \
  104. x == OCXL_IOCTL_GET_METADATA ? "GET_METADATA" : \
  105. "UNKNOWN")
  106. static long afu_ioctl(struct file *file, unsigned int cmd,
  107. unsigned long args)
  108. {
  109. struct ocxl_context *ctx = file->private_data;
  110. struct ocxl_ioctl_irq_fd irq_fd;
  111. u64 irq_offset;
  112. long rc;
  113. pr_debug("%s for context %d, command %s\n", __func__, ctx->pasid,
  114. CMD_STR(cmd));
  115. if (ctx->status == CLOSED)
  116. return -EIO;
  117. switch (cmd) {
  118. case OCXL_IOCTL_ATTACH:
  119. rc = afu_ioctl_attach(ctx,
  120. (struct ocxl_ioctl_attach __user *) args);
  121. break;
  122. case OCXL_IOCTL_IRQ_ALLOC:
  123. rc = ocxl_afu_irq_alloc(ctx, &irq_offset);
  124. if (!rc) {
  125. rc = copy_to_user((u64 __user *) args, &irq_offset,
  126. sizeof(irq_offset));
  127. if (rc) {
  128. ocxl_afu_irq_free(ctx, irq_offset);
  129. return -EFAULT;
  130. }
  131. }
  132. break;
  133. case OCXL_IOCTL_IRQ_FREE:
  134. rc = copy_from_user(&irq_offset, (u64 __user *) args,
  135. sizeof(irq_offset));
  136. if (rc)
  137. return -EFAULT;
  138. rc = ocxl_afu_irq_free(ctx, irq_offset);
  139. break;
  140. case OCXL_IOCTL_IRQ_SET_FD:
  141. rc = copy_from_user(&irq_fd, (u64 __user *) args,
  142. sizeof(irq_fd));
  143. if (rc)
  144. return -EFAULT;
  145. if (irq_fd.reserved)
  146. return -EINVAL;
  147. rc = ocxl_afu_irq_set_fd(ctx, irq_fd.irq_offset,
  148. irq_fd.eventfd);
  149. break;
  150. case OCXL_IOCTL_GET_METADATA:
  151. rc = afu_ioctl_get_metadata(ctx,
  152. (struct ocxl_ioctl_metadata __user *) args);
  153. break;
  154. default:
  155. rc = -EINVAL;
  156. }
  157. return rc;
  158. }
  159. static long afu_compat_ioctl(struct file *file, unsigned int cmd,
  160. unsigned long args)
  161. {
  162. return afu_ioctl(file, cmd, args);
  163. }
  164. static int afu_mmap(struct file *file, struct vm_area_struct *vma)
  165. {
  166. struct ocxl_context *ctx = file->private_data;
  167. pr_debug("%s for context %d\n", __func__, ctx->pasid);
  168. return ocxl_context_mmap(ctx, vma);
  169. }
  170. static bool has_xsl_error(struct ocxl_context *ctx)
  171. {
  172. bool ret;
  173. mutex_lock(&ctx->xsl_error_lock);
  174. ret = !!ctx->xsl_error.addr;
  175. mutex_unlock(&ctx->xsl_error_lock);
  176. return ret;
  177. }
  178. /*
  179. * Are there any events pending on the AFU
  180. * ctx: The AFU context
  181. * Returns: true if there are events pending
  182. */
  183. static bool afu_events_pending(struct ocxl_context *ctx)
  184. {
  185. if (has_xsl_error(ctx))
  186. return true;
  187. return false;
  188. }
  189. static unsigned int afu_poll(struct file *file, struct poll_table_struct *wait)
  190. {
  191. struct ocxl_context *ctx = file->private_data;
  192. unsigned int mask = 0;
  193. bool closed;
  194. pr_debug("%s for context %d\n", __func__, ctx->pasid);
  195. poll_wait(file, &ctx->events_wq, wait);
  196. mutex_lock(&ctx->status_mutex);
  197. closed = (ctx->status == CLOSED);
  198. mutex_unlock(&ctx->status_mutex);
  199. if (afu_events_pending(ctx))
  200. mask = EPOLLIN | EPOLLRDNORM;
  201. else if (closed)
  202. mask = EPOLLERR;
  203. return mask;
  204. }
  205. /*
  206. * Populate the supplied buffer with a single XSL error
  207. * ctx: The AFU context to report the error from
  208. * header: the event header to populate
  209. * buf: The buffer to write the body into (should be at least
  210. * AFU_EVENT_BODY_XSL_ERROR_SIZE)
  211. * Return: the amount of buffer that was populated
  212. */
  213. static ssize_t append_xsl_error(struct ocxl_context *ctx,
  214. struct ocxl_kernel_event_header *header,
  215. char __user *buf)
  216. {
  217. struct ocxl_kernel_event_xsl_fault_error body;
  218. memset(&body, 0, sizeof(body));
  219. mutex_lock(&ctx->xsl_error_lock);
  220. if (!ctx->xsl_error.addr) {
  221. mutex_unlock(&ctx->xsl_error_lock);
  222. return 0;
  223. }
  224. body.addr = ctx->xsl_error.addr;
  225. body.dsisr = ctx->xsl_error.dsisr;
  226. body.count = ctx->xsl_error.count;
  227. ctx->xsl_error.addr = 0;
  228. ctx->xsl_error.dsisr = 0;
  229. ctx->xsl_error.count = 0;
  230. mutex_unlock(&ctx->xsl_error_lock);
  231. header->type = OCXL_AFU_EVENT_XSL_FAULT_ERROR;
  232. if (copy_to_user(buf, &body, sizeof(body)))
  233. return -EFAULT;
  234. return sizeof(body);
  235. }
  236. #define AFU_EVENT_BODY_MAX_SIZE sizeof(struct ocxl_kernel_event_xsl_fault_error)
  237. /*
  238. * Reports events on the AFU
  239. * Format:
  240. * Header (struct ocxl_kernel_event_header)
  241. * Body (struct ocxl_kernel_event_*)
  242. * Header...
  243. */
  244. static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
  245. loff_t *off)
  246. {
  247. struct ocxl_context *ctx = file->private_data;
  248. struct ocxl_kernel_event_header header;
  249. ssize_t rc;
  250. ssize_t used = 0;
  251. DEFINE_WAIT(event_wait);
  252. memset(&header, 0, sizeof(header));
  253. /* Require offset to be 0 */
  254. if (*off != 0)
  255. return -EINVAL;
  256. if (count < (sizeof(struct ocxl_kernel_event_header) +
  257. AFU_EVENT_BODY_MAX_SIZE))
  258. return -EINVAL;
  259. for (;;) {
  260. prepare_to_wait(&ctx->events_wq, &event_wait,
  261. TASK_INTERRUPTIBLE);
  262. if (afu_events_pending(ctx))
  263. break;
  264. if (ctx->status == CLOSED)
  265. break;
  266. if (file->f_flags & O_NONBLOCK) {
  267. finish_wait(&ctx->events_wq, &event_wait);
  268. return -EAGAIN;
  269. }
  270. if (signal_pending(current)) {
  271. finish_wait(&ctx->events_wq, &event_wait);
  272. return -ERESTARTSYS;
  273. }
  274. schedule();
  275. }
  276. finish_wait(&ctx->events_wq, &event_wait);
  277. if (has_xsl_error(ctx)) {
  278. used = append_xsl_error(ctx, &header, buf + sizeof(header));
  279. if (used < 0)
  280. return used;
  281. }
  282. if (!afu_events_pending(ctx))
  283. header.flags |= OCXL_KERNEL_EVENT_FLAG_LAST;
  284. if (copy_to_user(buf, &header, sizeof(header)))
  285. return -EFAULT;
  286. used += sizeof(header);
  287. rc = used;
  288. return rc;
  289. }
  290. static int afu_release(struct inode *inode, struct file *file)
  291. {
  292. struct ocxl_context *ctx = file->private_data;
  293. int rc;
  294. pr_debug("%s for device %x\n", __func__, inode->i_rdev);
  295. rc = ocxl_context_detach(ctx);
  296. mutex_lock(&ctx->mapping_lock);
  297. ctx->mapping = NULL;
  298. mutex_unlock(&ctx->mapping_lock);
  299. wake_up_all(&ctx->events_wq);
  300. if (rc != -EBUSY)
  301. ocxl_context_free(ctx);
  302. return 0;
  303. }
  304. static const struct file_operations ocxl_afu_fops = {
  305. .owner = THIS_MODULE,
  306. .open = afu_open,
  307. .unlocked_ioctl = afu_ioctl,
  308. .compat_ioctl = afu_compat_ioctl,
  309. .mmap = afu_mmap,
  310. .poll = afu_poll,
  311. .read = afu_read,
  312. .release = afu_release,
  313. };
  314. int ocxl_create_cdev(struct ocxl_afu *afu)
  315. {
  316. int rc;
  317. cdev_init(&afu->cdev, &ocxl_afu_fops);
  318. rc = cdev_add(&afu->cdev, afu->dev.devt, 1);
  319. if (rc) {
  320. dev_err(&afu->dev, "Unable to add afu char device: %d\n", rc);
  321. return rc;
  322. }
  323. return 0;
  324. }
  325. void ocxl_destroy_cdev(struct ocxl_afu *afu)
  326. {
  327. cdev_del(&afu->cdev);
  328. }
  329. int ocxl_register_afu(struct ocxl_afu *afu)
  330. {
  331. int minor;
  332. minor = allocate_afu_minor(afu);
  333. if (minor < 0)
  334. return minor;
  335. afu->dev.devt = MKDEV(MAJOR(ocxl_dev), minor);
  336. afu->dev.class = ocxl_class;
  337. return device_register(&afu->dev);
  338. }
  339. void ocxl_unregister_afu(struct ocxl_afu *afu)
  340. {
  341. free_afu_minor(afu);
  342. }
  343. static char *ocxl_devnode(struct device *dev, umode_t *mode)
  344. {
  345. return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
  346. }
  347. int ocxl_file_init(void)
  348. {
  349. int rc;
  350. mutex_init(&minors_idr_lock);
  351. idr_init(&minors_idr);
  352. rc = alloc_chrdev_region(&ocxl_dev, 0, OCXL_NUM_MINORS, "ocxl");
  353. if (rc) {
  354. pr_err("Unable to allocate ocxl major number: %d\n", rc);
  355. return rc;
  356. }
  357. ocxl_class = class_create(THIS_MODULE, "ocxl");
  358. if (IS_ERR(ocxl_class)) {
  359. pr_err("Unable to create ocxl class\n");
  360. unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
  361. return PTR_ERR(ocxl_class);
  362. }
  363. ocxl_class->devnode = ocxl_devnode;
  364. return 0;
  365. }
  366. void ocxl_file_exit(void)
  367. {
  368. class_destroy(ocxl_class);
  369. unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
  370. idr_destroy(&minors_idr);
  371. }