file.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  2. * Copyright 2014 IBM Corp.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #include <linux/spinlock.h>
  10. #include <linux/module.h>
  11. #include <linux/export.h>
  12. #include <linux/kernel.h>
  13. #include <linux/bitmap.h>
  14. #include <linux/sched.h>
  15. #include <linux/poll.h>
  16. #include <linux/pid.h>
  17. #include <linux/fs.h>
  18. #include <linux/mm.h>
  19. #include <linux/slab.h>
  20. #include <asm/cputable.h>
  21. #include <asm/current.h>
  22. #include <asm/copro.h>
  23. #include "cxl.h"
  24. #include "trace.h"
  25. #define CXL_NUM_MINORS 256 /* Total to reserve */
  26. #define CXL_DEV_MINORS 13 /* 1 control + 4 AFUs * 3 (dedicated/master/shared) */
  27. #define CXL_CARD_MINOR(adapter) (adapter->adapter_num * CXL_DEV_MINORS)
  28. #define CXL_AFU_MINOR_D(afu) (CXL_CARD_MINOR(afu->adapter) + 1 + (3 * afu->slice))
  29. #define CXL_AFU_MINOR_M(afu) (CXL_AFU_MINOR_D(afu) + 1)
  30. #define CXL_AFU_MINOR_S(afu) (CXL_AFU_MINOR_D(afu) + 2)
  31. #define CXL_AFU_MKDEV_D(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_D(afu))
  32. #define CXL_AFU_MKDEV_M(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_M(afu))
  33. #define CXL_AFU_MKDEV_S(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_S(afu))
  34. #define CXL_DEVT_ADAPTER(dev) (MINOR(dev) / CXL_DEV_MINORS)
  35. #define CXL_DEVT_AFU(dev) ((MINOR(dev) % CXL_DEV_MINORS - 1) / 3)
  36. #define CXL_DEVT_IS_CARD(dev) (MINOR(dev) % CXL_DEV_MINORS == 0)
  37. static dev_t cxl_dev;
  38. static struct class *cxl_class;
  39. static int __afu_open(struct inode *inode, struct file *file, bool master)
  40. {
  41. struct cxl *adapter;
  42. struct cxl_afu *afu;
  43. struct cxl_context *ctx;
  44. int adapter_num = CXL_DEVT_ADAPTER(inode->i_rdev);
  45. int slice = CXL_DEVT_AFU(inode->i_rdev);
  46. int rc = -ENODEV;
  47. pr_devel("afu_open afu%i.%i\n", slice, adapter_num);
  48. if (!(adapter = get_cxl_adapter(adapter_num)))
  49. return -ENODEV;
  50. if (slice > adapter->slices)
  51. goto err_put_adapter;
  52. spin_lock(&adapter->afu_list_lock);
  53. if (!(afu = adapter->afu[slice])) {
  54. spin_unlock(&adapter->afu_list_lock);
  55. goto err_put_adapter;
  56. }
  57. get_device(&afu->dev);
  58. spin_unlock(&adapter->afu_list_lock);
  59. if (!afu->current_mode)
  60. goto err_put_afu;
  61. if (!(ctx = cxl_context_alloc())) {
  62. rc = -ENOMEM;
  63. goto err_put_afu;
  64. }
  65. if ((rc = cxl_context_init(ctx, afu, master, inode->i_mapping)))
  66. goto err_put_afu;
  67. pr_devel("afu_open pe: %i\n", ctx->pe);
  68. file->private_data = ctx;
  69. cxl_ctx_get();
  70. /* Our ref on the AFU will now hold the adapter */
  71. put_device(&adapter->dev);
  72. return 0;
  73. err_put_afu:
  74. put_device(&afu->dev);
  75. err_put_adapter:
  76. put_device(&adapter->dev);
  77. return rc;
  78. }
  79. static int afu_open(struct inode *inode, struct file *file)
  80. {
  81. return __afu_open(inode, file, false);
  82. }
  83. static int afu_master_open(struct inode *inode, struct file *file)
  84. {
  85. return __afu_open(inode, file, true);
  86. }
  87. static int afu_release(struct inode *inode, struct file *file)
  88. {
  89. struct cxl_context *ctx = file->private_data;
  90. pr_devel("%s: closing cxl file descriptor. pe: %i\n",
  91. __func__, ctx->pe);
  92. cxl_context_detach(ctx);
  93. mutex_lock(&ctx->mapping_lock);
  94. ctx->mapping = NULL;
  95. mutex_unlock(&ctx->mapping_lock);
  96. put_device(&ctx->afu->dev);
  97. /*
  98. * At this this point all bottom halfs have finished and we should be
  99. * getting no more IRQs from the hardware for this context. Once it's
  100. * removed from the IDR (and RCU synchronised) it's safe to free the
  101. * sstp and context.
  102. */
  103. cxl_context_free(ctx);
  104. cxl_ctx_put();
  105. return 0;
  106. }
  107. static long afu_ioctl_start_work(struct cxl_context *ctx,
  108. struct cxl_ioctl_start_work __user *uwork)
  109. {
  110. struct cxl_ioctl_start_work work;
  111. u64 amr = 0;
  112. int rc;
  113. pr_devel("%s: pe: %i\n", __func__, ctx->pe);
  114. /* Do this outside the status_mutex to avoid a circular dependency with
  115. * the locking in cxl_mmap_fault() */
  116. if (copy_from_user(&work, uwork,
  117. sizeof(struct cxl_ioctl_start_work))) {
  118. rc = -EFAULT;
  119. goto out;
  120. }
  121. mutex_lock(&ctx->status_mutex);
  122. if (ctx->status != OPENED) {
  123. rc = -EIO;
  124. goto out;
  125. }
  126. /*
  127. * if any of the reserved fields are set or any of the unused
  128. * flags are set it's invalid
  129. */
  130. if (work.reserved1 || work.reserved2 || work.reserved3 ||
  131. work.reserved4 || work.reserved5 || work.reserved6 ||
  132. (work.flags & ~CXL_START_WORK_ALL)) {
  133. rc = -EINVAL;
  134. goto out;
  135. }
  136. if (!(work.flags & CXL_START_WORK_NUM_IRQS))
  137. work.num_interrupts = ctx->afu->pp_irqs;
  138. else if ((work.num_interrupts < ctx->afu->pp_irqs) ||
  139. (work.num_interrupts > ctx->afu->irqs_max)) {
  140. rc = -EINVAL;
  141. goto out;
  142. }
  143. if ((rc = afu_register_irqs(ctx, work.num_interrupts)))
  144. goto out;
  145. if (work.flags & CXL_START_WORK_AMR)
  146. amr = work.amr & mfspr(SPRN_UAMOR);
  147. /*
  148. * We grab the PID here and not in the file open to allow for the case
  149. * where a process (master, some daemon, etc) has opened the chardev on
  150. * behalf of another process, so the AFU's mm gets bound to the process
  151. * that performs this ioctl and not the process that opened the file.
  152. */
  153. ctx->pid = get_pid(get_task_pid(current, PIDTYPE_PID));
  154. trace_cxl_attach(ctx, work.work_element_descriptor, work.num_interrupts, amr);
  155. if ((rc = cxl_attach_process(ctx, false, work.work_element_descriptor,
  156. amr))) {
  157. afu_release_irqs(ctx);
  158. goto out;
  159. }
  160. ctx->status = STARTED;
  161. rc = 0;
  162. out:
  163. mutex_unlock(&ctx->status_mutex);
  164. return rc;
  165. }
  166. static long afu_ioctl_process_element(struct cxl_context *ctx,
  167. int __user *upe)
  168. {
  169. pr_devel("%s: pe: %i\n", __func__, ctx->pe);
  170. if (copy_to_user(upe, &ctx->pe, sizeof(__u32)))
  171. return -EFAULT;
  172. return 0;
  173. }
  174. static long afu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  175. {
  176. struct cxl_context *ctx = file->private_data;
  177. if (ctx->status == CLOSED)
  178. return -EIO;
  179. pr_devel("afu_ioctl\n");
  180. switch (cmd) {
  181. case CXL_IOCTL_START_WORK:
  182. return afu_ioctl_start_work(ctx, (struct cxl_ioctl_start_work __user *)arg);
  183. case CXL_IOCTL_GET_PROCESS_ELEMENT:
  184. return afu_ioctl_process_element(ctx, (__u32 __user *)arg);
  185. }
  186. return -EINVAL;
  187. }
  188. static long afu_compat_ioctl(struct file *file, unsigned int cmd,
  189. unsigned long arg)
  190. {
  191. return afu_ioctl(file, cmd, arg);
  192. }
  193. static int afu_mmap(struct file *file, struct vm_area_struct *vm)
  194. {
  195. struct cxl_context *ctx = file->private_data;
  196. /* AFU must be started before we can MMIO */
  197. if (ctx->status != STARTED)
  198. return -EIO;
  199. return cxl_context_iomap(ctx, vm);
  200. }
  201. static unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
  202. {
  203. struct cxl_context *ctx = file->private_data;
  204. int mask = 0;
  205. unsigned long flags;
  206. poll_wait(file, &ctx->wq, poll);
  207. pr_devel("afu_poll wait done pe: %i\n", ctx->pe);
  208. spin_lock_irqsave(&ctx->lock, flags);
  209. if (ctx->pending_irq || ctx->pending_fault ||
  210. ctx->pending_afu_err)
  211. mask |= POLLIN | POLLRDNORM;
  212. else if (ctx->status == CLOSED)
  213. /* Only error on closed when there are no futher events pending
  214. */
  215. mask |= POLLERR;
  216. spin_unlock_irqrestore(&ctx->lock, flags);
  217. pr_devel("afu_poll pe: %i returning %#x\n", ctx->pe, mask);
  218. return mask;
  219. }
  220. static inline int ctx_event_pending(struct cxl_context *ctx)
  221. {
  222. return (ctx->pending_irq || ctx->pending_fault ||
  223. ctx->pending_afu_err || (ctx->status == CLOSED));
  224. }
  225. static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
  226. loff_t *off)
  227. {
  228. struct cxl_context *ctx = file->private_data;
  229. struct cxl_event event;
  230. unsigned long flags;
  231. int rc;
  232. DEFINE_WAIT(wait);
  233. if (count < CXL_READ_MIN_SIZE)
  234. return -EINVAL;
  235. spin_lock_irqsave(&ctx->lock, flags);
  236. for (;;) {
  237. prepare_to_wait(&ctx->wq, &wait, TASK_INTERRUPTIBLE);
  238. if (ctx_event_pending(ctx))
  239. break;
  240. if (file->f_flags & O_NONBLOCK) {
  241. rc = -EAGAIN;
  242. goto out;
  243. }
  244. if (signal_pending(current)) {
  245. rc = -ERESTARTSYS;
  246. goto out;
  247. }
  248. spin_unlock_irqrestore(&ctx->lock, flags);
  249. pr_devel("afu_read going to sleep...\n");
  250. schedule();
  251. pr_devel("afu_read woken up\n");
  252. spin_lock_irqsave(&ctx->lock, flags);
  253. }
  254. finish_wait(&ctx->wq, &wait);
  255. memset(&event, 0, sizeof(event));
  256. event.header.process_element = ctx->pe;
  257. event.header.size = sizeof(struct cxl_event_header);
  258. if (ctx->pending_irq) {
  259. pr_devel("afu_read delivering AFU interrupt\n");
  260. event.header.size += sizeof(struct cxl_event_afu_interrupt);
  261. event.header.type = CXL_EVENT_AFU_INTERRUPT;
  262. event.irq.irq = find_first_bit(ctx->irq_bitmap, ctx->irq_count) + 1;
  263. clear_bit(event.irq.irq - 1, ctx->irq_bitmap);
  264. if (bitmap_empty(ctx->irq_bitmap, ctx->irq_count))
  265. ctx->pending_irq = false;
  266. } else if (ctx->pending_fault) {
  267. pr_devel("afu_read delivering data storage fault\n");
  268. event.header.size += sizeof(struct cxl_event_data_storage);
  269. event.header.type = CXL_EVENT_DATA_STORAGE;
  270. event.fault.addr = ctx->fault_addr;
  271. event.fault.dsisr = ctx->fault_dsisr;
  272. ctx->pending_fault = false;
  273. } else if (ctx->pending_afu_err) {
  274. pr_devel("afu_read delivering afu error\n");
  275. event.header.size += sizeof(struct cxl_event_afu_error);
  276. event.header.type = CXL_EVENT_AFU_ERROR;
  277. event.afu_error.error = ctx->afu_err;
  278. ctx->pending_afu_err = false;
  279. } else if (ctx->status == CLOSED) {
  280. pr_devel("afu_read fatal error\n");
  281. spin_unlock_irqrestore(&ctx->lock, flags);
  282. return -EIO;
  283. } else
  284. WARN(1, "afu_read must be buggy\n");
  285. spin_unlock_irqrestore(&ctx->lock, flags);
  286. if (copy_to_user(buf, &event, event.header.size))
  287. return -EFAULT;
  288. return event.header.size;
  289. out:
  290. finish_wait(&ctx->wq, &wait);
  291. spin_unlock_irqrestore(&ctx->lock, flags);
  292. return rc;
  293. }
  294. static const struct file_operations afu_fops = {
  295. .owner = THIS_MODULE,
  296. .open = afu_open,
  297. .poll = afu_poll,
  298. .read = afu_read,
  299. .release = afu_release,
  300. .unlocked_ioctl = afu_ioctl,
  301. .compat_ioctl = afu_compat_ioctl,
  302. .mmap = afu_mmap,
  303. };
  304. static const struct file_operations afu_master_fops = {
  305. .owner = THIS_MODULE,
  306. .open = afu_master_open,
  307. .poll = afu_poll,
  308. .read = afu_read,
  309. .release = afu_release,
  310. .unlocked_ioctl = afu_ioctl,
  311. .compat_ioctl = afu_compat_ioctl,
  312. .mmap = afu_mmap,
  313. };
  314. static char *cxl_devnode(struct device *dev, umode_t *mode)
  315. {
  316. if (CXL_DEVT_IS_CARD(dev->devt)) {
  317. /*
  318. * These minor numbers will eventually be used to program the
  319. * PSL and AFUs once we have dynamic reprogramming support
  320. */
  321. return NULL;
  322. }
  323. return kasprintf(GFP_KERNEL, "cxl/%s", dev_name(dev));
  324. }
  325. extern struct class *cxl_class;
  326. static int cxl_add_chardev(struct cxl_afu *afu, dev_t devt, struct cdev *cdev,
  327. struct device **chardev, char *postfix, char *desc,
  328. const struct file_operations *fops)
  329. {
  330. struct device *dev;
  331. int rc;
  332. cdev_init(cdev, fops);
  333. if ((rc = cdev_add(cdev, devt, 1))) {
  334. dev_err(&afu->dev, "Unable to add %s chardev: %i\n", desc, rc);
  335. return rc;
  336. }
  337. dev = device_create(cxl_class, &afu->dev, devt, afu,
  338. "afu%i.%i%s", afu->adapter->adapter_num, afu->slice, postfix);
  339. if (IS_ERR(dev)) {
  340. dev_err(&afu->dev, "Unable to create %s chardev in sysfs: %i\n", desc, rc);
  341. rc = PTR_ERR(dev);
  342. goto err;
  343. }
  344. *chardev = dev;
  345. return 0;
  346. err:
  347. cdev_del(cdev);
  348. return rc;
  349. }
  350. int cxl_chardev_d_afu_add(struct cxl_afu *afu)
  351. {
  352. return cxl_add_chardev(afu, CXL_AFU_MKDEV_D(afu), &afu->afu_cdev_d,
  353. &afu->chardev_d, "d", "dedicated",
  354. &afu_master_fops); /* Uses master fops */
  355. }
  356. int cxl_chardev_m_afu_add(struct cxl_afu *afu)
  357. {
  358. return cxl_add_chardev(afu, CXL_AFU_MKDEV_M(afu), &afu->afu_cdev_m,
  359. &afu->chardev_m, "m", "master",
  360. &afu_master_fops);
  361. }
  362. int cxl_chardev_s_afu_add(struct cxl_afu *afu)
  363. {
  364. return cxl_add_chardev(afu, CXL_AFU_MKDEV_S(afu), &afu->afu_cdev_s,
  365. &afu->chardev_s, "s", "shared",
  366. &afu_fops);
  367. }
  368. void cxl_chardev_afu_remove(struct cxl_afu *afu)
  369. {
  370. if (afu->chardev_d) {
  371. cdev_del(&afu->afu_cdev_d);
  372. device_unregister(afu->chardev_d);
  373. afu->chardev_d = NULL;
  374. }
  375. if (afu->chardev_m) {
  376. cdev_del(&afu->afu_cdev_m);
  377. device_unregister(afu->chardev_m);
  378. afu->chardev_m = NULL;
  379. }
  380. if (afu->chardev_s) {
  381. cdev_del(&afu->afu_cdev_s);
  382. device_unregister(afu->chardev_s);
  383. afu->chardev_s = NULL;
  384. }
  385. }
  386. int cxl_register_afu(struct cxl_afu *afu)
  387. {
  388. afu->dev.class = cxl_class;
  389. return device_register(&afu->dev);
  390. }
  391. int cxl_register_adapter(struct cxl *adapter)
  392. {
  393. adapter->dev.class = cxl_class;
  394. /*
  395. * Future: When we support dynamically reprogramming the PSL & AFU we
  396. * will expose the interface to do that via a chardev:
  397. * adapter->dev.devt = CXL_CARD_MKDEV(adapter);
  398. */
  399. return device_register(&adapter->dev);
  400. }
  401. int __init cxl_file_init(void)
  402. {
  403. int rc;
  404. /*
  405. * If these change we really need to update API. Either change some
  406. * flags or update API version number CXL_API_VERSION.
  407. */
  408. BUILD_BUG_ON(CXL_API_VERSION != 1);
  409. BUILD_BUG_ON(sizeof(struct cxl_ioctl_start_work) != 64);
  410. BUILD_BUG_ON(sizeof(struct cxl_event_header) != 8);
  411. BUILD_BUG_ON(sizeof(struct cxl_event_afu_interrupt) != 8);
  412. BUILD_BUG_ON(sizeof(struct cxl_event_data_storage) != 32);
  413. BUILD_BUG_ON(sizeof(struct cxl_event_afu_error) != 16);
  414. if ((rc = alloc_chrdev_region(&cxl_dev, 0, CXL_NUM_MINORS, "cxl"))) {
  415. pr_err("Unable to allocate CXL major number: %i\n", rc);
  416. return rc;
  417. }
  418. pr_devel("CXL device allocated, MAJOR %i\n", MAJOR(cxl_dev));
  419. cxl_class = class_create(THIS_MODULE, "cxl");
  420. if (IS_ERR(cxl_class)) {
  421. pr_err("Unable to create CXL class\n");
  422. rc = PTR_ERR(cxl_class);
  423. goto err;
  424. }
  425. cxl_class->devnode = cxl_devnode;
  426. return 0;
  427. err:
  428. unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
  429. return rc;
  430. }
  431. void cxl_file_exit(void)
  432. {
  433. unregister_chrdev_region(cxl_dev, CXL_NUM_MINORS);
  434. class_destroy(cxl_class);
  435. }