api.c 11 KB

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