api.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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/pci.h>
  10. #include <linux/slab.h>
  11. #include <linux/anon_inodes.h>
  12. #include <linux/file.h>
  13. #include <misc/cxl.h>
  14. #include "cxl.h"
  15. struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
  16. {
  17. struct cxl_afu *afu;
  18. struct cxl_context *ctx;
  19. int rc;
  20. afu = cxl_pci_to_afu(dev);
  21. ctx = cxl_context_alloc();
  22. if (IS_ERR(ctx))
  23. return ctx;
  24. /* Make it a slave context. We can promote it later? */
  25. rc = cxl_context_init(ctx, afu, false, NULL);
  26. if (rc) {
  27. kfree(ctx);
  28. return ERR_PTR(-ENOMEM);
  29. }
  30. cxl_assign_psn_space(ctx);
  31. return ctx;
  32. }
  33. EXPORT_SYMBOL_GPL(cxl_dev_context_init);
  34. struct cxl_context *cxl_get_context(struct pci_dev *dev)
  35. {
  36. return dev->dev.archdata.cxl_ctx;
  37. }
  38. EXPORT_SYMBOL_GPL(cxl_get_context);
  39. struct device *cxl_get_phys_dev(struct pci_dev *dev)
  40. {
  41. struct cxl_afu *afu;
  42. afu = cxl_pci_to_afu(dev);
  43. return afu->adapter->dev.parent;
  44. }
  45. EXPORT_SYMBOL_GPL(cxl_get_phys_dev);
  46. int cxl_release_context(struct cxl_context *ctx)
  47. {
  48. if (ctx->status != CLOSED)
  49. return -EBUSY;
  50. cxl_context_free(ctx);
  51. return 0;
  52. }
  53. EXPORT_SYMBOL_GPL(cxl_release_context);
  54. int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
  55. {
  56. if (num == 0)
  57. num = ctx->afu->pp_irqs;
  58. return afu_allocate_irqs(ctx, num);
  59. }
  60. EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
  61. void cxl_free_afu_irqs(struct cxl_context *ctx)
  62. {
  63. cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  64. }
  65. EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
  66. static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
  67. {
  68. __u16 range;
  69. int r;
  70. WARN_ON(num == 0);
  71. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  72. range = ctx->irqs.range[r];
  73. if (num < range) {
  74. return ctx->irqs.offset[r] + num;
  75. }
  76. num -= range;
  77. }
  78. return 0;
  79. }
  80. int cxl_map_afu_irq(struct cxl_context *ctx, int num,
  81. irq_handler_t handler, void *cookie, char *name)
  82. {
  83. irq_hw_number_t hwirq;
  84. /*
  85. * Find interrupt we are to register.
  86. */
  87. hwirq = cxl_find_afu_irq(ctx, num);
  88. if (!hwirq)
  89. return -ENOENT;
  90. return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
  91. }
  92. EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
  93. void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
  94. {
  95. irq_hw_number_t hwirq;
  96. unsigned int virq;
  97. hwirq = cxl_find_afu_irq(ctx, num);
  98. if (!hwirq)
  99. return;
  100. virq = irq_find_mapping(NULL, hwirq);
  101. if (virq)
  102. cxl_unmap_irq(virq, cookie);
  103. }
  104. EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
  105. /*
  106. * Start a context
  107. * Code here similar to afu_ioctl_start_work().
  108. */
  109. int cxl_start_context(struct cxl_context *ctx, u64 wed,
  110. struct task_struct *task)
  111. {
  112. int rc = 0;
  113. bool kernel = true;
  114. pr_devel("%s: pe: %i\n", __func__, ctx->pe);
  115. mutex_lock(&ctx->status_mutex);
  116. if (ctx->status == STARTED)
  117. goto out; /* already started */
  118. if (task) {
  119. ctx->pid = get_task_pid(task, PIDTYPE_PID);
  120. get_pid(ctx->pid);
  121. kernel = false;
  122. }
  123. cxl_ctx_get();
  124. if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
  125. put_pid(ctx->pid);
  126. cxl_ctx_put();
  127. goto out;
  128. }
  129. ctx->status = STARTED;
  130. get_device(&ctx->afu->dev);
  131. out:
  132. mutex_unlock(&ctx->status_mutex);
  133. return rc;
  134. }
  135. EXPORT_SYMBOL_GPL(cxl_start_context);
  136. int cxl_process_element(struct cxl_context *ctx)
  137. {
  138. return ctx->pe;
  139. }
  140. EXPORT_SYMBOL_GPL(cxl_process_element);
  141. /* Stop a context. Returns 0 on success, otherwise -Errno */
  142. int cxl_stop_context(struct cxl_context *ctx)
  143. {
  144. int rc;
  145. rc = __detach_context(ctx);
  146. if (!rc)
  147. put_device(&ctx->afu->dev);
  148. return rc;
  149. }
  150. EXPORT_SYMBOL_GPL(cxl_stop_context);
  151. void cxl_set_master(struct cxl_context *ctx)
  152. {
  153. ctx->master = true;
  154. cxl_assign_psn_space(ctx);
  155. }
  156. EXPORT_SYMBOL_GPL(cxl_set_master);
  157. /* wrappers around afu_* file ops which are EXPORTED */
  158. int cxl_fd_open(struct inode *inode, struct file *file)
  159. {
  160. return afu_open(inode, file);
  161. }
  162. EXPORT_SYMBOL_GPL(cxl_fd_open);
  163. int cxl_fd_release(struct inode *inode, struct file *file)
  164. {
  165. return afu_release(inode, file);
  166. }
  167. EXPORT_SYMBOL_GPL(cxl_fd_release);
  168. long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  169. {
  170. return afu_ioctl(file, cmd, arg);
  171. }
  172. EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
  173. int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
  174. {
  175. return afu_mmap(file, vm);
  176. }
  177. EXPORT_SYMBOL_GPL(cxl_fd_mmap);
  178. unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
  179. {
  180. return afu_poll(file, poll);
  181. }
  182. EXPORT_SYMBOL_GPL(cxl_fd_poll);
  183. ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
  184. loff_t *off)
  185. {
  186. return afu_read(file, buf, count, off);
  187. }
  188. EXPORT_SYMBOL_GPL(cxl_fd_read);
  189. #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
  190. /* Get a struct file and fd for a context and attach the ops */
  191. struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
  192. int *fd)
  193. {
  194. struct file *file;
  195. int rc, flags, fdtmp;
  196. flags = O_RDWR | O_CLOEXEC;
  197. /* This code is similar to anon_inode_getfd() */
  198. rc = get_unused_fd_flags(flags);
  199. if (rc < 0)
  200. return ERR_PTR(rc);
  201. fdtmp = rc;
  202. /*
  203. * Patch the file ops. Needs to be careful that this is rentrant safe.
  204. */
  205. if (fops) {
  206. PATCH_FOPS(open);
  207. PATCH_FOPS(poll);
  208. PATCH_FOPS(read);
  209. PATCH_FOPS(release);
  210. PATCH_FOPS(unlocked_ioctl);
  211. PATCH_FOPS(compat_ioctl);
  212. PATCH_FOPS(mmap);
  213. } else /* use default ops */
  214. fops = (struct file_operations *)&afu_fops;
  215. file = anon_inode_getfile("cxl", fops, ctx, flags);
  216. if (IS_ERR(file))
  217. put_unused_fd(fdtmp);
  218. *fd = fdtmp;
  219. return file;
  220. }
  221. EXPORT_SYMBOL_GPL(cxl_get_fd);
  222. struct cxl_context *cxl_fops_get_context(struct file *file)
  223. {
  224. return file->private_data;
  225. }
  226. EXPORT_SYMBOL_GPL(cxl_fops_get_context);
  227. int cxl_start_work(struct cxl_context *ctx,
  228. struct cxl_ioctl_start_work *work)
  229. {
  230. int rc;
  231. /* code taken from afu_ioctl_start_work */
  232. if (!(work->flags & CXL_START_WORK_NUM_IRQS))
  233. work->num_interrupts = ctx->afu->pp_irqs;
  234. else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
  235. (work->num_interrupts > ctx->afu->irqs_max)) {
  236. return -EINVAL;
  237. }
  238. rc = afu_register_irqs(ctx, work->num_interrupts);
  239. if (rc)
  240. return rc;
  241. rc = cxl_start_context(ctx, work->work_element_descriptor, current);
  242. if (rc < 0) {
  243. afu_release_irqs(ctx, ctx);
  244. return rc;
  245. }
  246. return 0;
  247. }
  248. EXPORT_SYMBOL_GPL(cxl_start_work);
  249. void __iomem *cxl_psa_map(struct cxl_context *ctx)
  250. {
  251. struct cxl_afu *afu = ctx->afu;
  252. int rc;
  253. rc = cxl_afu_check_and_enable(afu);
  254. if (rc)
  255. return NULL;
  256. pr_devel("%s: psn_phys%llx size:%llx\n",
  257. __func__, afu->psn_phys, afu->adapter->ps_size);
  258. return ioremap(ctx->psn_phys, ctx->psn_size);
  259. }
  260. EXPORT_SYMBOL_GPL(cxl_psa_map);
  261. void cxl_psa_unmap(void __iomem *addr)
  262. {
  263. iounmap(addr);
  264. }
  265. EXPORT_SYMBOL_GPL(cxl_psa_unmap);
  266. int cxl_afu_reset(struct cxl_context *ctx)
  267. {
  268. struct cxl_afu *afu = ctx->afu;
  269. int rc;
  270. rc = __cxl_afu_reset(afu);
  271. if (rc)
  272. return rc;
  273. return cxl_afu_check_and_enable(afu);
  274. }
  275. EXPORT_SYMBOL_GPL(cxl_afu_reset);