api.c 7.7 KB

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