file.c 15 KB

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