file.c 13 KB

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