file.c 14 KB

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