file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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 DEFINE_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. pr_debug("%s for context %d\n", __func__, ctx->pasid);
  65. if (copy_from_user(&arg, uarg, sizeof(arg)))
  66. return -EFAULT;
  67. /* Make sure reserved fields are not set for forward compatibility */
  68. if (arg.reserved1 || arg.reserved2 || arg.reserved3)
  69. return -EINVAL;
  70. amr = arg.amr & mfspr(SPRN_UAMOR);
  71. return ocxl_context_attach(ctx, amr, current->mm);
  72. }
  73. static long afu_ioctl_get_metadata(struct ocxl_context *ctx,
  74. struct ocxl_ioctl_metadata __user *uarg)
  75. {
  76. struct ocxl_ioctl_metadata arg;
  77. memset(&arg, 0, sizeof(arg));
  78. arg.version = 0;
  79. arg.afu_version_major = ctx->afu->config.version_major;
  80. arg.afu_version_minor = ctx->afu->config.version_minor;
  81. arg.pasid = ctx->pasid;
  82. arg.pp_mmio_size = ctx->afu->config.pp_mmio_stride;
  83. arg.global_mmio_size = ctx->afu->config.global_mmio_size;
  84. if (copy_to_user(uarg, &arg, sizeof(arg)))
  85. return -EFAULT;
  86. return 0;
  87. }
  88. #ifdef CONFIG_PPC64
  89. static long afu_ioctl_enable_p9_wait(struct ocxl_context *ctx,
  90. struct ocxl_ioctl_p9_wait __user *uarg)
  91. {
  92. struct ocxl_ioctl_p9_wait arg;
  93. memset(&arg, 0, sizeof(arg));
  94. if (cpu_has_feature(CPU_FTR_P9_TIDR)) {
  95. enum ocxl_context_status status;
  96. // Locks both status & tidr
  97. mutex_lock(&ctx->status_mutex);
  98. if (!ctx->tidr) {
  99. if (set_thread_tidr(current)) {
  100. mutex_unlock(&ctx->status_mutex);
  101. return -ENOENT;
  102. }
  103. ctx->tidr = current->thread.tidr;
  104. }
  105. status = ctx->status;
  106. mutex_unlock(&ctx->status_mutex);
  107. if (status == ATTACHED) {
  108. int rc = ocxl_link_update_pe(ctx->afu->fn->link,
  109. ctx->pasid, ctx->tidr);
  110. if (rc)
  111. return rc;
  112. }
  113. arg.thread_id = ctx->tidr;
  114. } else
  115. return -ENOENT;
  116. if (copy_to_user(uarg, &arg, sizeof(arg)))
  117. return -EFAULT;
  118. return 0;
  119. }
  120. #endif
  121. static long afu_ioctl_get_features(struct ocxl_context *ctx,
  122. struct ocxl_ioctl_features __user *uarg)
  123. {
  124. struct ocxl_ioctl_features arg;
  125. memset(&arg, 0, sizeof(arg));
  126. #ifdef CONFIG_PPC64
  127. if (cpu_has_feature(CPU_FTR_P9_TIDR))
  128. arg.flags[0] |= OCXL_IOCTL_FEATURES_FLAGS0_P9_WAIT;
  129. #endif
  130. if (copy_to_user(uarg, &arg, sizeof(arg)))
  131. return -EFAULT;
  132. return 0;
  133. }
  134. #define CMD_STR(x) (x == OCXL_IOCTL_ATTACH ? "ATTACH" : \
  135. x == OCXL_IOCTL_IRQ_ALLOC ? "IRQ_ALLOC" : \
  136. x == OCXL_IOCTL_IRQ_FREE ? "IRQ_FREE" : \
  137. x == OCXL_IOCTL_IRQ_SET_FD ? "IRQ_SET_FD" : \
  138. x == OCXL_IOCTL_GET_METADATA ? "GET_METADATA" : \
  139. x == OCXL_IOCTL_ENABLE_P9_WAIT ? "ENABLE_P9_WAIT" : \
  140. x == OCXL_IOCTL_GET_FEATURES ? "GET_FEATURES" : \
  141. "UNKNOWN")
  142. static irqreturn_t irq_handler(void *private)
  143. {
  144. struct eventfd_ctx *ev_ctx = private;
  145. eventfd_signal(ev_ctx, 1);
  146. return IRQ_HANDLED;
  147. }
  148. static void irq_free(void *private)
  149. {
  150. struct eventfd_ctx *ev_ctx = private;
  151. eventfd_ctx_put(ev_ctx);
  152. }
  153. static long afu_ioctl(struct file *file, unsigned int cmd,
  154. unsigned long args)
  155. {
  156. struct ocxl_context *ctx = file->private_data;
  157. struct ocxl_ioctl_irq_fd irq_fd;
  158. struct eventfd_ctx *ev_ctx;
  159. int irq_id;
  160. u64 irq_offset;
  161. long rc;
  162. bool closed;
  163. pr_debug("%s for context %d, command %s\n", __func__, ctx->pasid,
  164. CMD_STR(cmd));
  165. mutex_lock(&ctx->status_mutex);
  166. closed = (ctx->status == CLOSED);
  167. mutex_unlock(&ctx->status_mutex);
  168. if (closed)
  169. return -EIO;
  170. switch (cmd) {
  171. case OCXL_IOCTL_ATTACH:
  172. rc = afu_ioctl_attach(ctx,
  173. (struct ocxl_ioctl_attach __user *) args);
  174. break;
  175. case OCXL_IOCTL_IRQ_ALLOC:
  176. rc = ocxl_afu_irq_alloc(ctx, &irq_id);
  177. if (!rc) {
  178. irq_offset = ocxl_irq_id_to_offset(ctx, irq_id);
  179. rc = copy_to_user((u64 __user *) args, &irq_offset,
  180. sizeof(irq_offset));
  181. if (rc) {
  182. ocxl_afu_irq_free(ctx, irq_id);
  183. return -EFAULT;
  184. }
  185. }
  186. break;
  187. case OCXL_IOCTL_IRQ_FREE:
  188. rc = copy_from_user(&irq_offset, (u64 __user *) args,
  189. sizeof(irq_offset));
  190. if (rc)
  191. return -EFAULT;
  192. irq_id = ocxl_irq_offset_to_id(ctx, irq_offset);
  193. rc = ocxl_afu_irq_free(ctx, irq_id);
  194. break;
  195. case OCXL_IOCTL_IRQ_SET_FD:
  196. rc = copy_from_user(&irq_fd, (u64 __user *) args,
  197. sizeof(irq_fd));
  198. if (rc)
  199. return -EFAULT;
  200. if (irq_fd.reserved)
  201. return -EINVAL;
  202. irq_id = ocxl_irq_offset_to_id(ctx, irq_fd.irq_offset);
  203. ev_ctx = eventfd_ctx_fdget(irq_fd.eventfd);
  204. if (IS_ERR(ev_ctx))
  205. return PTR_ERR(ev_ctx);
  206. rc = ocxl_irq_set_handler(ctx, irq_id, irq_handler, irq_free, ev_ctx);
  207. if (rc)
  208. eventfd_ctx_put(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. free_minor(info);
  434. put_device(&info->dev);
  435. return rc;
  436. }
  437. rc = ocxl_sysfs_register_afu(info);
  438. if (rc)
  439. goto err_unregister;
  440. rc = ocxl_file_make_visible(info);
  441. if (rc)
  442. goto err_unregister;
  443. ocxl_afu_set_private(afu, info);
  444. return 0;
  445. err_unregister:
  446. ocxl_sysfs_unregister_afu(info); // safe to call even if register failed
  447. free_minor(info);
  448. device_unregister(&info->dev);
  449. return rc;
  450. err_put:
  451. ocxl_afu_put(afu);
  452. free_minor(info);
  453. kfree(info);
  454. return rc;
  455. }
  456. void ocxl_file_unregister_afu(struct ocxl_afu *afu)
  457. {
  458. struct ocxl_file_info *info = ocxl_afu_get_private(afu);
  459. if (!info)
  460. return;
  461. ocxl_file_make_invisible(info);
  462. ocxl_sysfs_unregister_afu(info);
  463. free_minor(info);
  464. device_unregister(&info->dev);
  465. }
  466. static char *ocxl_devnode(struct device *dev, umode_t *mode)
  467. {
  468. return kasprintf(GFP_KERNEL, "ocxl/%s", dev_name(dev));
  469. }
  470. int ocxl_file_init(void)
  471. {
  472. int rc;
  473. idr_init(&minors_idr);
  474. rc = alloc_chrdev_region(&ocxl_dev, 0, OCXL_NUM_MINORS, "ocxl");
  475. if (rc) {
  476. pr_err("Unable to allocate ocxl major number: %d\n", rc);
  477. return rc;
  478. }
  479. ocxl_class = class_create(THIS_MODULE, "ocxl");
  480. if (IS_ERR(ocxl_class)) {
  481. pr_err("Unable to create ocxl class\n");
  482. unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
  483. return PTR_ERR(ocxl_class);
  484. }
  485. ocxl_class->devnode = ocxl_devnode;
  486. return 0;
  487. }
  488. void ocxl_file_exit(void)
  489. {
  490. class_destroy(ocxl_class);
  491. unregister_chrdev_region(ocxl_dev, OCXL_NUM_MINORS);
  492. idr_destroy(&minors_idr);
  493. }