file.c 13 KB

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