file.c 12 KB

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