file.c 13 KB

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