api.c 7.6 KB

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