file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. mutex_unlock(&ctx->status_mutex);
  115. return -ENOENT;
  116. }
  117. ctx->tidr = current->thread.tidr;
  118. }
  119. status = ctx->status;
  120. mutex_unlock(&ctx->status_mutex);
  121. if (status == ATTACHED) {
  122. int rc;
  123. struct link *link = ctx->afu->fn->link;
  124. rc = ocxl_link_update_pe(link, ctx->pasid, ctx->tidr);
  125. if (rc)
  126. return rc;
  127. }
  128. arg.thread_id = ctx->tidr;
  129. } else
  130. return -ENOENT;
  131. if (copy_to_user(uarg, &arg, sizeof(arg)))
  132. return -EFAULT;
  133. return 0;
  134. }
  135. #endif
  136. static long afu_ioctl_get_features(struct ocxl_context *ctx,
  137. struct ocxl_ioctl_features __user *uarg)
  138. {
  139. struct ocxl_ioctl_features arg;
  140. memset(&arg, 0, sizeof(arg));
  141. #ifdef CONFIG_PPC64
  142. if (cpu_has_feature(CPU_FTR_P9_TIDR))
  143. arg.flags[0] |= OCXL_IOCTL_FEATURES_FLAGS0_P9_WAIT;
  144. #endif
  145. if (copy_to_user(uarg, &arg, sizeof(arg)))
  146. return -EFAULT;
  147. return 0;
  148. }
  149. #define CMD_STR(x) (x == OCXL_IOCTL_ATTACH ? "ATTACH" : \
  150. x == OCXL_IOCTL_IRQ_ALLOC ? "IRQ_ALLOC" : \
  151. x == OCXL_IOCTL_IRQ_FREE ? "IRQ_FREE" : \
  152. x == OCXL_IOCTL_IRQ_SET_FD ? "IRQ_SET_FD" : \
  153. x == OCXL_IOCTL_GET_METADATA ? "GET_METADATA" : \
  154. x == OCXL_IOCTL_ENABLE_P9_WAIT ? "ENABLE_P9_WAIT" : \
  155. x == OCXL_IOCTL_GET_FEATURES ? "GET_FEATURES" : \
  156. "UNKNOWN")
  157. static long afu_ioctl(struct file *file, unsigned int cmd,
  158. unsigned long args)
  159. {
  160. struct ocxl_context *ctx = file->private_data;
  161. struct ocxl_ioctl_irq_fd irq_fd;
  162. u64 irq_offset;
  163. long rc;
  164. pr_debug("%s for context %d, command %s\n", __func__, ctx->pasid,
  165. CMD_STR(cmd));
  166. if (ctx->status == CLOSED)
  167. return -EIO;
  168. switch (cmd) {
  169. case OCXL_IOCTL_ATTACH:
  170. rc = afu_ioctl_attach(ctx,
  171. (struct ocxl_ioctl_attach __user *) args);
  172. break;
  173. case OCXL_IOCTL_IRQ_ALLOC:
  174. rc = ocxl_afu_irq_alloc(ctx, &irq_offset);
  175. if (!rc) {
  176. rc = copy_to_user((u64 __user *) args, &irq_offset,
  177. sizeof(irq_offset));
  178. if (rc) {
  179. ocxl_afu_irq_free(ctx, irq_offset);
  180. return -EFAULT;
  181. }
  182. }
  183. break;
  184. case OCXL_IOCTL_IRQ_FREE:
  185. rc = copy_from_user(&irq_offset, (u64 __user *) args,
  186. sizeof(irq_offset));
  187. if (rc)
  188. return -EFAULT;
  189. rc = ocxl_afu_irq_free(ctx, irq_offset);
  190. break;
  191. case OCXL_IOCTL_IRQ_SET_FD:
  192. rc = copy_from_user(&irq_fd, (u64 __user *) args,
  193. sizeof(irq_fd));
  194. if (rc)
  195. return -EFAULT;
  196. if (irq_fd.reserved)
  197. return -EINVAL;
  198. rc = ocxl_afu_irq_set_fd(ctx, irq_fd.irq_offset,
  199. irq_fd.eventfd);
  200. break;
  201. case OCXL_IOCTL_GET_METADATA:
  202. rc = afu_ioctl_get_metadata(ctx,
  203. (struct ocxl_ioctl_metadata __user *) args);
  204. break;
  205. #ifdef CONFIG_PPC64
  206. case OCXL_IOCTL_ENABLE_P9_WAIT:
  207. rc = afu_ioctl_enable_p9_wait(ctx,
  208. (struct ocxl_ioctl_p9_wait __user *) args);
  209. break;
  210. #endif
  211. case OCXL_IOCTL_GET_FEATURES:
  212. rc = afu_ioctl_get_features(ctx,
  213. (struct ocxl_ioctl_features __user *) args);
  214. break;
  215. default:
  216. rc = -EINVAL;
  217. }
  218. return rc;
  219. }
  220. static long afu_compat_ioctl(struct file *file, unsigned int cmd,
  221. unsigned long args)
  222. {
  223. return afu_ioctl(file, cmd, args);
  224. }
  225. static int afu_mmap(struct file *file, struct vm_area_struct *vma)
  226. {
  227. struct ocxl_context *ctx = file->private_data;
  228. pr_debug("%s for context %d\n", __func__, ctx->pasid);
  229. return ocxl_context_mmap(ctx, vma);
  230. }
  231. static bool has_xsl_error(struct ocxl_context *ctx)
  232. {
  233. bool ret;
  234. mutex_lock(&ctx->xsl_error_lock);
  235. ret = !!ctx->xsl_error.addr;
  236. mutex_unlock(&ctx->xsl_error_lock);
  237. return ret;
  238. }
  239. /*
  240. * Are there any events pending on the AFU
  241. * ctx: The AFU context
  242. * Returns: true if there are events pending
  243. */
  244. static bool afu_events_pending(struct ocxl_context *ctx)
  245. {
  246. if (has_xsl_error(ctx))
  247. return true;
  248. return false;
  249. }
  250. static unsigned int afu_poll(struct file *file, struct poll_table_struct *wait)
  251. {
  252. struct ocxl_context *ctx = file->private_data;
  253. unsigned int mask = 0;
  254. bool closed;
  255. pr_debug("%s for context %d\n", __func__, ctx->pasid);
  256. poll_wait(file, &ctx->events_wq, wait);
  257. mutex_lock(&ctx->status_mutex);
  258. closed = (ctx->status == CLOSED);
  259. mutex_unlock(&ctx->status_mutex);
  260. if (afu_events_pending(ctx))
  261. mask = EPOLLIN | EPOLLRDNORM;
  262. else if (closed)
  263. mask = EPOLLERR;
  264. return mask;
  265. }
  266. /*
  267. * Populate the supplied buffer with a single XSL error
  268. * ctx: The AFU context to report the error from
  269. * header: the event header to populate
  270. * buf: The buffer to write the body into (should be at least
  271. * AFU_EVENT_BODY_XSL_ERROR_SIZE)
  272. * Return: the amount of buffer that was populated
  273. */
  274. static ssize_t append_xsl_error(struct ocxl_context *ctx,
  275. struct ocxl_kernel_event_header *header,
  276. char __user *buf)
  277. {
  278. struct ocxl_kernel_event_xsl_fault_error body;
  279. memset(&body, 0, sizeof(body));
  280. mutex_lock(&ctx->xsl_error_lock);
  281. if (!ctx->xsl_error.addr) {
  282. mutex_unlock(&ctx->xsl_error_lock);
  283. return 0;
  284. }
  285. body.addr = ctx->xsl_error.addr;
  286. body.dsisr = ctx->xsl_error.dsisr;
  287. body.count = ctx->xsl_error.count;
  288. ctx->xsl_error.addr = 0;
  289. ctx->xsl_error.dsisr = 0;
  290. ctx->xsl_error.count = 0;
  291. mutex_unlock(&ctx->xsl_error_lock);
  292. header->type = OCXL_AFU_EVENT_XSL_FAULT_ERROR;
  293. if (copy_to_user(buf, &body, sizeof(body)))
  294. return -EFAULT;
  295. return sizeof(body);
  296. }
  297. #define AFU_EVENT_BODY_MAX_SIZE sizeof(struct ocxl_kernel_event_xsl_fault_error)
  298. /*
  299. * Reports events on the AFU
  300. * Format:
  301. * Header (struct ocxl_kernel_event_header)
  302. * Body (struct ocxl_kernel_event_*)
  303. * Header...
  304. */
  305. static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
  306. loff_t *off)
  307. {
  308. struct ocxl_context *ctx = file->private_data;
  309. struct ocxl_kernel_event_header header;
  310. ssize_t rc;
  311. ssize_t used = 0;
  312. DEFINE_WAIT(event_wait);
  313. memset(&header, 0, sizeof(header));
  314. /* Require offset to be 0 */
  315. if (*off != 0)
  316. return -EINVAL;
  317. if (count < (sizeof(struct ocxl_kernel_event_header) +
  318. AFU_EVENT_BODY_MAX_SIZE))
  319. return -EINVAL;
  320. for (;;) {
  321. prepare_to_wait(&ctx->events_wq, &event_wait,
  322. TASK_INTERRUPTIBLE);
  323. if (afu_events_pending(ctx))
  324. break;
  325. if (ctx->status == CLOSED)
  326. break;
  327. if (file->f_flags & O_NONBLOCK) {
  328. finish_wait(&ctx->events_wq, &event_wait);
  329. return -EAGAIN;
  330. }
  331. if (signal_pending(current)) {
  332. finish_wait(&ctx->events_wq, &event_wait);
  333. return -ERESTARTSYS;
  334. }
  335. schedule();
  336. }
  337. finish_wait(&ctx->events_wq, &event_wait);
  338. if (has_xsl_error(ctx)) {
  339. used = append_xsl_error(ctx, &header, buf + sizeof(header));
  340. if (used < 0)
  341. return used;
  342. }
  343. if (!afu_events_pending(ctx))
  344. header.flags |= OCXL_KERNEL_EVENT_FLAG_LAST;
  345. if (copy_to_user(buf, &header, sizeof(header)))
  346. return -EFAULT;
  347. used += sizeof(header);
  348. rc = used;
  349. return rc;
  350. }
  351. static int afu_release(struct inode *inode, struct file *file)
  352. {
  353. struct ocxl_context *ctx = file->private_data;
  354. int rc;
  355. pr_debug("%s for device %x\n", __func__, inode->i_rdev);
  356. rc = ocxl_context_detach(ctx);
  357. mutex_lock(&ctx->mapping_lock);
  358. ctx->mapping = NULL;
  359. mutex_unlock(&ctx->mapping_lock);
  360. wake_up_all(&ctx->events_wq);
  361. if (rc != -EBUSY)
  362. ocxl_context_free(ctx);
  363. return 0;
  364. }
  365. static const struct file_operations ocxl_afu_fops = {
  366. .owner = THIS_MODULE,
  367. .open = afu_open,
  368. .unlocked_ioctl = afu_ioctl,
  369. .compat_ioctl = afu_compat_ioctl,
  370. .mmap = afu_mmap,
  371. .poll = afu_poll,
  372. .read = afu_read,
  373. .release = afu_release,
  374. };
  375. int ocxl_create_cdev(struct ocxl_afu *afu)
  376. {
  377. int rc;
  378. cdev_init(&afu->cdev, &ocxl_afu_fops);
  379. rc = cdev_add(&afu->cdev, afu->dev.devt, 1);
  380. if (rc) {
  381. dev_err(&afu->dev, "Unable to add afu char device: %d\n", rc);
  382. return rc;
  383. }
  384. return 0;
  385. }
  386. void ocxl_destroy_cdev(struct ocxl_afu *afu)
  387. {
  388. cdev_del(&afu->cdev);
  389. }
  390. int ocxl_register_afu(struct ocxl_afu *afu)
  391. {
  392. int minor;
  393. minor = allocate_afu_minor(afu);
  394. if (minor < 0)
  395. return minor;
  396. afu->dev.devt = MKDEV(MAJOR(ocxl_dev), minor);
  397. afu->dev.class = ocxl_class;
  398. return device_register(&afu->dev);
  399. }
  400. void ocxl_unregister_afu(struct ocxl_afu *afu)
  401. {
  402. free_afu_minor(afu);
  403. }
  404. static char *ocxl_devnode(struct device *dev, umode_t *mode)
  405. {
  406. return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
  407. }
  408. int ocxl_file_init(void)
  409. {
  410. int rc;
  411. mutex_init(&minors_idr_lock);
  412. idr_init(&minors_idr);
  413. rc = alloc_chrdev_region(&ocxl_dev, 0, OCXL_NUM_MINORS, "ocxl");
  414. if (rc) {
  415. pr_err("Unable to allocate ocxl major number: %d\n", rc);
  416. return rc;
  417. }
  418. ocxl_class = class_create(THIS_MODULE, "ocxl");
  419. if (IS_ERR(ocxl_class)) {
  420. pr_err("Unable to create ocxl class\n");
  421. unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
  422. return PTR_ERR(ocxl_class);
  423. }
  424. ocxl_class->devnode = ocxl_devnode;
  425. return 0;
  426. }
  427. void ocxl_file_exit(void)
  428. {
  429. class_destroy(ocxl_class);
  430. unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
  431. idr_destroy(&minors_idr);
  432. }