api.c 7.8 KB

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