file.c 13 KB

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