api.c 9.8 KB

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