api.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. int cxl_release_context(struct cxl_context *ctx)
  60. {
  61. if (ctx->status >= STARTED)
  62. return -EBUSY;
  63. cxl_context_free(ctx);
  64. return 0;
  65. }
  66. EXPORT_SYMBOL_GPL(cxl_release_context);
  67. static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
  68. {
  69. __u16 range;
  70. int r;
  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_allocate_afu_irqs(struct cxl_context *ctx, int num)
  81. {
  82. int res;
  83. irq_hw_number_t hwirq;
  84. if (num == 0)
  85. num = ctx->afu->pp_irqs;
  86. res = afu_allocate_irqs(ctx, num);
  87. if (res)
  88. return res;
  89. if (!cpu_has_feature(CPU_FTR_HVMODE)) {
  90. /* In a guest, the PSL interrupt is not multiplexed. It was
  91. * allocated above, and we need to set its handler
  92. */
  93. hwirq = cxl_find_afu_irq(ctx, 0);
  94. if (hwirq)
  95. cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
  96. }
  97. if (ctx->status == STARTED) {
  98. if (cxl_ops->update_ivtes)
  99. cxl_ops->update_ivtes(ctx);
  100. else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
  101. }
  102. return res;
  103. }
  104. EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
  105. void cxl_free_afu_irqs(struct cxl_context *ctx)
  106. {
  107. irq_hw_number_t hwirq;
  108. unsigned int virq;
  109. if (!cpu_has_feature(CPU_FTR_HVMODE)) {
  110. hwirq = cxl_find_afu_irq(ctx, 0);
  111. if (hwirq) {
  112. virq = irq_find_mapping(NULL, hwirq);
  113. if (virq)
  114. cxl_unmap_irq(virq, ctx);
  115. }
  116. }
  117. afu_irq_name_free(ctx);
  118. cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  119. }
  120. EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
  121. int cxl_map_afu_irq(struct cxl_context *ctx, int num,
  122. irq_handler_t handler, void *cookie, char *name)
  123. {
  124. irq_hw_number_t hwirq;
  125. /*
  126. * Find interrupt we are to register.
  127. */
  128. hwirq = cxl_find_afu_irq(ctx, num);
  129. if (!hwirq)
  130. return -ENOENT;
  131. return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
  132. }
  133. EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
  134. void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
  135. {
  136. irq_hw_number_t hwirq;
  137. unsigned int virq;
  138. hwirq = cxl_find_afu_irq(ctx, num);
  139. if (!hwirq)
  140. return;
  141. virq = irq_find_mapping(NULL, hwirq);
  142. if (virq)
  143. cxl_unmap_irq(virq, cookie);
  144. }
  145. EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
  146. /*
  147. * Start a context
  148. * Code here similar to afu_ioctl_start_work().
  149. */
  150. int cxl_start_context(struct cxl_context *ctx, u64 wed,
  151. struct task_struct *task)
  152. {
  153. int rc = 0;
  154. bool kernel = true;
  155. pr_devel("%s: pe: %i\n", __func__, ctx->pe);
  156. mutex_lock(&ctx->status_mutex);
  157. if (ctx->status == STARTED)
  158. goto out; /* already started */
  159. if (task) {
  160. ctx->pid = get_task_pid(task, PIDTYPE_PID);
  161. ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
  162. kernel = false;
  163. ctx->real_mode = false;
  164. }
  165. cxl_ctx_get();
  166. if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
  167. put_pid(ctx->pid);
  168. cxl_ctx_put();
  169. goto out;
  170. }
  171. ctx->status = STARTED;
  172. out:
  173. mutex_unlock(&ctx->status_mutex);
  174. return rc;
  175. }
  176. EXPORT_SYMBOL_GPL(cxl_start_context);
  177. int cxl_process_element(struct cxl_context *ctx)
  178. {
  179. return ctx->external_pe;
  180. }
  181. EXPORT_SYMBOL_GPL(cxl_process_element);
  182. /* Stop a context. Returns 0 on success, otherwise -Errno */
  183. int cxl_stop_context(struct cxl_context *ctx)
  184. {
  185. return __detach_context(ctx);
  186. }
  187. EXPORT_SYMBOL_GPL(cxl_stop_context);
  188. void cxl_set_master(struct cxl_context *ctx)
  189. {
  190. ctx->master = true;
  191. }
  192. EXPORT_SYMBOL_GPL(cxl_set_master);
  193. int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
  194. {
  195. if (ctx->status == STARTED) {
  196. /*
  197. * We could potentially update the PE and issue an update LLCMD
  198. * to support this, but it doesn't seem to have a good use case
  199. * since it's trivial to just create a second kernel context
  200. * with different translation modes, so until someone convinces
  201. * me otherwise:
  202. */
  203. return -EBUSY;
  204. }
  205. ctx->real_mode = real_mode;
  206. return 0;
  207. }
  208. EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
  209. /* wrappers around afu_* file ops which are EXPORTED */
  210. int cxl_fd_open(struct inode *inode, struct file *file)
  211. {
  212. return afu_open(inode, file);
  213. }
  214. EXPORT_SYMBOL_GPL(cxl_fd_open);
  215. int cxl_fd_release(struct inode *inode, struct file *file)
  216. {
  217. return afu_release(inode, file);
  218. }
  219. EXPORT_SYMBOL_GPL(cxl_fd_release);
  220. long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  221. {
  222. return afu_ioctl(file, cmd, arg);
  223. }
  224. EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
  225. int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
  226. {
  227. return afu_mmap(file, vm);
  228. }
  229. EXPORT_SYMBOL_GPL(cxl_fd_mmap);
  230. unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
  231. {
  232. return afu_poll(file, poll);
  233. }
  234. EXPORT_SYMBOL_GPL(cxl_fd_poll);
  235. ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
  236. loff_t *off)
  237. {
  238. return afu_read(file, buf, count, off);
  239. }
  240. EXPORT_SYMBOL_GPL(cxl_fd_read);
  241. #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
  242. /* Get a struct file and fd for a context and attach the ops */
  243. struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
  244. int *fd)
  245. {
  246. struct file *file;
  247. int rc, flags, fdtmp;
  248. flags = O_RDWR | O_CLOEXEC;
  249. /* This code is similar to anon_inode_getfd() */
  250. rc = get_unused_fd_flags(flags);
  251. if (rc < 0)
  252. return ERR_PTR(rc);
  253. fdtmp = rc;
  254. /*
  255. * Patch the file ops. Needs to be careful that this is rentrant safe.
  256. */
  257. if (fops) {
  258. PATCH_FOPS(open);
  259. PATCH_FOPS(poll);
  260. PATCH_FOPS(read);
  261. PATCH_FOPS(release);
  262. PATCH_FOPS(unlocked_ioctl);
  263. PATCH_FOPS(compat_ioctl);
  264. PATCH_FOPS(mmap);
  265. } else /* use default ops */
  266. fops = (struct file_operations *)&afu_fops;
  267. file = anon_inode_getfile("cxl", fops, ctx, flags);
  268. if (IS_ERR(file))
  269. goto err_fd;
  270. file->f_mapping = ctx->mapping;
  271. *fd = fdtmp;
  272. return file;
  273. err_fd:
  274. put_unused_fd(fdtmp);
  275. return NULL;
  276. }
  277. EXPORT_SYMBOL_GPL(cxl_get_fd);
  278. struct cxl_context *cxl_fops_get_context(struct file *file)
  279. {
  280. return file->private_data;
  281. }
  282. EXPORT_SYMBOL_GPL(cxl_fops_get_context);
  283. void cxl_set_driver_ops(struct cxl_context *ctx,
  284. struct cxl_afu_driver_ops *ops)
  285. {
  286. WARN_ON(!ops->fetch_event || !ops->event_delivered);
  287. atomic_set(&ctx->afu_driver_events, 0);
  288. ctx->afu_driver_ops = ops;
  289. }
  290. EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
  291. void cxl_context_events_pending(struct cxl_context *ctx,
  292. unsigned int new_events)
  293. {
  294. atomic_add(new_events, &ctx->afu_driver_events);
  295. wake_up_all(&ctx->wq);
  296. }
  297. EXPORT_SYMBOL_GPL(cxl_context_events_pending);
  298. int cxl_start_work(struct cxl_context *ctx,
  299. struct cxl_ioctl_start_work *work)
  300. {
  301. int rc;
  302. /* code taken from afu_ioctl_start_work */
  303. if (!(work->flags & CXL_START_WORK_NUM_IRQS))
  304. work->num_interrupts = ctx->afu->pp_irqs;
  305. else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
  306. (work->num_interrupts > ctx->afu->irqs_max)) {
  307. return -EINVAL;
  308. }
  309. rc = afu_register_irqs(ctx, work->num_interrupts);
  310. if (rc)
  311. return rc;
  312. rc = cxl_start_context(ctx, work->work_element_descriptor, current);
  313. if (rc < 0) {
  314. afu_release_irqs(ctx, ctx);
  315. return rc;
  316. }
  317. return 0;
  318. }
  319. EXPORT_SYMBOL_GPL(cxl_start_work);
  320. void __iomem *cxl_psa_map(struct cxl_context *ctx)
  321. {
  322. if (ctx->status != STARTED)
  323. return NULL;
  324. pr_devel("%s: psn_phys%llx size:%llx\n",
  325. __func__, ctx->psn_phys, ctx->psn_size);
  326. return ioremap(ctx->psn_phys, ctx->psn_size);
  327. }
  328. EXPORT_SYMBOL_GPL(cxl_psa_map);
  329. void cxl_psa_unmap(void __iomem *addr)
  330. {
  331. iounmap(addr);
  332. }
  333. EXPORT_SYMBOL_GPL(cxl_psa_unmap);
  334. int cxl_afu_reset(struct cxl_context *ctx)
  335. {
  336. struct cxl_afu *afu = ctx->afu;
  337. int rc;
  338. rc = cxl_ops->afu_reset(afu);
  339. if (rc)
  340. return rc;
  341. return cxl_ops->afu_check_and_enable(afu);
  342. }
  343. EXPORT_SYMBOL_GPL(cxl_afu_reset);
  344. void cxl_perst_reloads_same_image(struct cxl_afu *afu,
  345. bool perst_reloads_same_image)
  346. {
  347. afu->adapter->perst_same_image = perst_reloads_same_image;
  348. }
  349. EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
  350. ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
  351. {
  352. struct cxl_afu *afu = cxl_pci_to_afu(dev);
  353. return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
  354. }
  355. EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);