api.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2014 IBM Corp.
  4. */
  5. #include <linux/pci.h>
  6. #include <linux/slab.h>
  7. #include <linux/file.h>
  8. #include <misc/cxl.h>
  9. #include <linux/module.h>
  10. #include <linux/mount.h>
  11. #include <linux/pseudo_fs.h>
  12. #include <linux/sched/mm.h>
  13. #include <linux/mmu_context.h>
  14. #include <linux/irqdomain.h>
  15. #include "cxl.h"
  16. /*
  17. * Since we want to track memory mappings to be able to force-unmap
  18. * when the AFU is no longer reachable, we need an inode. For devices
  19. * opened through the cxl user API, this is not a problem, but a
  20. * userland process can also get a cxl fd through the cxl_get_fd()
  21. * API, which is used by the cxlflash driver.
  22. *
  23. * Therefore we implement our own simple pseudo-filesystem and inode
  24. * allocator. We don't use the anonymous inode, as we need the
  25. * meta-data associated with it (address_space) and it is shared by
  26. * other drivers/processes, so it could lead to cxl unmapping VMAs
  27. * from random processes.
  28. */
  29. #define CXL_PSEUDO_FS_MAGIC 0x1697697f
  30. static int cxl_fs_cnt;
  31. static struct vfsmount *cxl_vfs_mount;
  32. static int cxl_fs_init_fs_context(struct fs_context *fc)
  33. {
  34. return init_pseudo(fc, CXL_PSEUDO_FS_MAGIC) ? 0 : -ENOMEM;
  35. }
  36. static struct file_system_type cxl_fs_type = {
  37. .name = "cxl",
  38. .owner = THIS_MODULE,
  39. .init_fs_context = cxl_fs_init_fs_context,
  40. .kill_sb = kill_anon_super,
  41. };
  42. void cxl_release_mapping(struct cxl_context *ctx)
  43. {
  44. if (ctx->kernelapi && ctx->mapping)
  45. simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
  46. }
  47. static struct file *cxl_getfile(const char *name,
  48. const struct file_operations *fops,
  49. void *priv, int flags)
  50. {
  51. struct file *file;
  52. struct inode *inode;
  53. int rc;
  54. /* strongly inspired by anon_inode_getfile() */
  55. if (fops->owner && !try_module_get(fops->owner))
  56. return ERR_PTR(-ENOENT);
  57. rc = simple_pin_fs(&cxl_fs_type, &cxl_vfs_mount, &cxl_fs_cnt);
  58. if (rc < 0) {
  59. pr_err("Cannot mount cxl pseudo filesystem: %d\n", rc);
  60. file = ERR_PTR(rc);
  61. goto err_module;
  62. }
  63. inode = alloc_anon_inode(cxl_vfs_mount->mnt_sb);
  64. if (IS_ERR(inode)) {
  65. file = ERR_CAST(inode);
  66. goto err_fs;
  67. }
  68. file = alloc_file_pseudo(inode, cxl_vfs_mount, name,
  69. flags & (O_ACCMODE | O_NONBLOCK), fops);
  70. if (IS_ERR(file))
  71. goto err_inode;
  72. file->private_data = priv;
  73. return file;
  74. err_inode:
  75. iput(inode);
  76. err_fs:
  77. simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
  78. err_module:
  79. module_put(fops->owner);
  80. return file;
  81. }
  82. struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
  83. {
  84. struct cxl_afu *afu;
  85. struct cxl_context *ctx;
  86. int rc;
  87. afu = cxl_pci_to_afu(dev);
  88. if (IS_ERR(afu))
  89. return ERR_CAST(afu);
  90. ctx = cxl_context_alloc();
  91. if (!ctx)
  92. return ERR_PTR(-ENOMEM);
  93. ctx->kernelapi = true;
  94. /* Make it a slave context. We can promote it later? */
  95. rc = cxl_context_init(ctx, afu, false);
  96. if (rc)
  97. goto err_ctx;
  98. return ctx;
  99. err_ctx:
  100. kfree(ctx);
  101. return ERR_PTR(rc);
  102. }
  103. EXPORT_SYMBOL_GPL(cxl_dev_context_init);
  104. struct cxl_context *cxl_get_context(struct pci_dev *dev)
  105. {
  106. return dev->dev.archdata.cxl_ctx;
  107. }
  108. EXPORT_SYMBOL_GPL(cxl_get_context);
  109. int cxl_release_context(struct cxl_context *ctx)
  110. {
  111. if (ctx->status >= STARTED)
  112. return -EBUSY;
  113. cxl_context_free(ctx);
  114. return 0;
  115. }
  116. EXPORT_SYMBOL_GPL(cxl_release_context);
  117. static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
  118. {
  119. __u16 range;
  120. int r;
  121. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  122. range = ctx->irqs.range[r];
  123. if (num < range) {
  124. return ctx->irqs.offset[r] + num;
  125. }
  126. num -= range;
  127. }
  128. return 0;
  129. }
  130. int cxl_set_priv(struct cxl_context *ctx, void *priv)
  131. {
  132. if (!ctx)
  133. return -EINVAL;
  134. ctx->priv = priv;
  135. return 0;
  136. }
  137. EXPORT_SYMBOL_GPL(cxl_set_priv);
  138. void *cxl_get_priv(struct cxl_context *ctx)
  139. {
  140. if (!ctx)
  141. return ERR_PTR(-EINVAL);
  142. return ctx->priv;
  143. }
  144. EXPORT_SYMBOL_GPL(cxl_get_priv);
  145. int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
  146. {
  147. int res;
  148. irq_hw_number_t hwirq;
  149. if (num == 0)
  150. num = ctx->afu->pp_irqs;
  151. res = afu_allocate_irqs(ctx, num);
  152. if (res)
  153. return res;
  154. if (!cpu_has_feature(CPU_FTR_HVMODE)) {
  155. /* In a guest, the PSL interrupt is not multiplexed. It was
  156. * allocated above, and we need to set its handler
  157. */
  158. hwirq = cxl_find_afu_irq(ctx, 0);
  159. if (hwirq)
  160. cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
  161. }
  162. if (ctx->status == STARTED) {
  163. if (cxl_ops->update_ivtes)
  164. cxl_ops->update_ivtes(ctx);
  165. else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
  166. }
  167. return res;
  168. }
  169. EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
  170. void cxl_free_afu_irqs(struct cxl_context *ctx)
  171. {
  172. irq_hw_number_t hwirq;
  173. unsigned int virq;
  174. if (!cpu_has_feature(CPU_FTR_HVMODE)) {
  175. hwirq = cxl_find_afu_irq(ctx, 0);
  176. if (hwirq) {
  177. virq = irq_find_mapping(NULL, hwirq);
  178. if (virq)
  179. cxl_unmap_irq(virq, ctx);
  180. }
  181. }
  182. afu_irq_name_free(ctx);
  183. cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  184. }
  185. EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
  186. int cxl_map_afu_irq(struct cxl_context *ctx, int num,
  187. irq_handler_t handler, void *cookie, char *name)
  188. {
  189. irq_hw_number_t hwirq;
  190. /*
  191. * Find interrupt we are to register.
  192. */
  193. hwirq = cxl_find_afu_irq(ctx, num);
  194. if (!hwirq)
  195. return -ENOENT;
  196. return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
  197. }
  198. EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
  199. void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
  200. {
  201. irq_hw_number_t hwirq;
  202. unsigned int virq;
  203. hwirq = cxl_find_afu_irq(ctx, num);
  204. if (!hwirq)
  205. return;
  206. virq = irq_find_mapping(NULL, hwirq);
  207. if (virq)
  208. cxl_unmap_irq(virq, cookie);
  209. }
  210. EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
  211. /*
  212. * Start a context
  213. * Code here similar to afu_ioctl_start_work().
  214. */
  215. int cxl_start_context(struct cxl_context *ctx, u64 wed,
  216. struct task_struct *task)
  217. {
  218. int rc = 0;
  219. bool kernel = true;
  220. pr_devel("%s: pe: %i\n", __func__, ctx->pe);
  221. mutex_lock(&ctx->status_mutex);
  222. if (ctx->status == STARTED)
  223. goto out; /* already started */
  224. /*
  225. * Increment the mapped context count for adapter. This also checks
  226. * if adapter_context_lock is taken.
  227. */
  228. rc = cxl_adapter_context_get(ctx->afu->adapter);
  229. if (rc)
  230. goto out;
  231. if (task) {
  232. ctx->pid = get_task_pid(task, PIDTYPE_PID);
  233. kernel = false;
  234. /* acquire a reference to the task's mm */
  235. ctx->mm = get_task_mm(current);
  236. /* ensure this mm_struct can't be freed */
  237. cxl_context_mm_count_get(ctx);
  238. if (ctx->mm) {
  239. /* decrement the use count from above */
  240. mmput(ctx->mm);
  241. /* make TLBIs for this context global */
  242. mm_context_add_copro(ctx->mm);
  243. }
  244. }
  245. /*
  246. * Increment driver use count. Enables global TLBIs for hash
  247. * and callbacks to handle the segment table
  248. */
  249. cxl_ctx_get();
  250. /* See the comment in afu_ioctl_start_work() */
  251. smp_mb();
  252. if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
  253. put_pid(ctx->pid);
  254. ctx->pid = NULL;
  255. cxl_adapter_context_put(ctx->afu->adapter);
  256. cxl_ctx_put();
  257. if (task) {
  258. cxl_context_mm_count_put(ctx);
  259. if (ctx->mm)
  260. mm_context_remove_copro(ctx->mm);
  261. }
  262. goto out;
  263. }
  264. ctx->status = STARTED;
  265. out:
  266. mutex_unlock(&ctx->status_mutex);
  267. return rc;
  268. }
  269. EXPORT_SYMBOL_GPL(cxl_start_context);
  270. int cxl_process_element(struct cxl_context *ctx)
  271. {
  272. return ctx->external_pe;
  273. }
  274. EXPORT_SYMBOL_GPL(cxl_process_element);
  275. /* Stop a context. Returns 0 on success, otherwise -Errno */
  276. int cxl_stop_context(struct cxl_context *ctx)
  277. {
  278. return __detach_context(ctx);
  279. }
  280. EXPORT_SYMBOL_GPL(cxl_stop_context);
  281. void cxl_set_master(struct cxl_context *ctx)
  282. {
  283. ctx->master = true;
  284. }
  285. EXPORT_SYMBOL_GPL(cxl_set_master);
  286. /* wrappers around afu_* file ops which are EXPORTED */
  287. int cxl_fd_open(struct inode *inode, struct file *file)
  288. {
  289. return afu_open(inode, file);
  290. }
  291. EXPORT_SYMBOL_GPL(cxl_fd_open);
  292. int cxl_fd_release(struct inode *inode, struct file *file)
  293. {
  294. return afu_release(inode, file);
  295. }
  296. EXPORT_SYMBOL_GPL(cxl_fd_release);
  297. long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  298. {
  299. return afu_ioctl(file, cmd, arg);
  300. }
  301. EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
  302. int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
  303. {
  304. return afu_mmap(file, vm);
  305. }
  306. EXPORT_SYMBOL_GPL(cxl_fd_mmap);
  307. __poll_t cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
  308. {
  309. return afu_poll(file, poll);
  310. }
  311. EXPORT_SYMBOL_GPL(cxl_fd_poll);
  312. ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
  313. loff_t *off)
  314. {
  315. return afu_read(file, buf, count, off);
  316. }
  317. EXPORT_SYMBOL_GPL(cxl_fd_read);
  318. #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
  319. /* Get a struct file and fd for a context and attach the ops */
  320. struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
  321. int *fd)
  322. {
  323. struct file *file;
  324. int rc, flags, fdtmp;
  325. char *name = NULL;
  326. /* only allow one per context */
  327. if (ctx->mapping)
  328. return ERR_PTR(-EEXIST);
  329. flags = O_RDWR | O_CLOEXEC;
  330. /* This code is similar to anon_inode_getfd() */
  331. rc = get_unused_fd_flags(flags);
  332. if (rc < 0)
  333. return ERR_PTR(rc);
  334. fdtmp = rc;
  335. /*
  336. * Patch the file ops. Needs to be careful that this is rentrant safe.
  337. */
  338. if (fops) {
  339. PATCH_FOPS(open);
  340. PATCH_FOPS(poll);
  341. PATCH_FOPS(read);
  342. PATCH_FOPS(release);
  343. PATCH_FOPS(unlocked_ioctl);
  344. PATCH_FOPS(compat_ioctl);
  345. PATCH_FOPS(mmap);
  346. } else /* use default ops */
  347. fops = (struct file_operations *)&afu_fops;
  348. name = kasprintf(GFP_KERNEL, "cxl:%d", ctx->pe);
  349. file = cxl_getfile(name, fops, ctx, flags);
  350. kfree(name);
  351. if (IS_ERR(file))
  352. goto err_fd;
  353. cxl_context_set_mapping(ctx, file->f_mapping);
  354. *fd = fdtmp;
  355. return file;
  356. err_fd:
  357. put_unused_fd(fdtmp);
  358. return NULL;
  359. }
  360. EXPORT_SYMBOL_GPL(cxl_get_fd);
  361. struct cxl_context *cxl_fops_get_context(struct file *file)
  362. {
  363. return file->private_data;
  364. }
  365. EXPORT_SYMBOL_GPL(cxl_fops_get_context);
  366. void cxl_set_driver_ops(struct cxl_context *ctx,
  367. struct cxl_afu_driver_ops *ops)
  368. {
  369. WARN_ON(!ops->fetch_event || !ops->event_delivered);
  370. atomic_set(&ctx->afu_driver_events, 0);
  371. ctx->afu_driver_ops = ops;
  372. }
  373. EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
  374. void cxl_context_events_pending(struct cxl_context *ctx,
  375. unsigned int new_events)
  376. {
  377. atomic_add(new_events, &ctx->afu_driver_events);
  378. wake_up_all(&ctx->wq);
  379. }
  380. EXPORT_SYMBOL_GPL(cxl_context_events_pending);
  381. int cxl_start_work(struct cxl_context *ctx,
  382. struct cxl_ioctl_start_work *work)
  383. {
  384. int rc;
  385. /* code taken from afu_ioctl_start_work */
  386. if (!(work->flags & CXL_START_WORK_NUM_IRQS))
  387. work->num_interrupts = ctx->afu->pp_irqs;
  388. else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
  389. (work->num_interrupts > ctx->afu->irqs_max)) {
  390. return -EINVAL;
  391. }
  392. rc = afu_register_irqs(ctx, work->num_interrupts);
  393. if (rc)
  394. return rc;
  395. rc = cxl_start_context(ctx, work->work_element_descriptor, current);
  396. if (rc < 0) {
  397. afu_release_irqs(ctx, ctx);
  398. return rc;
  399. }
  400. return 0;
  401. }
  402. EXPORT_SYMBOL_GPL(cxl_start_work);
  403. void __iomem *cxl_psa_map(struct cxl_context *ctx)
  404. {
  405. if (ctx->status != STARTED)
  406. return NULL;
  407. pr_devel("%s: psn_phys%llx size:%llx\n",
  408. __func__, ctx->psn_phys, ctx->psn_size);
  409. return ioremap(ctx->psn_phys, ctx->psn_size);
  410. }
  411. EXPORT_SYMBOL_GPL(cxl_psa_map);
  412. void cxl_psa_unmap(void __iomem *addr)
  413. {
  414. iounmap(addr);
  415. }
  416. EXPORT_SYMBOL_GPL(cxl_psa_unmap);
  417. int cxl_afu_reset(struct cxl_context *ctx)
  418. {
  419. struct cxl_afu *afu = ctx->afu;
  420. int rc;
  421. rc = cxl_ops->afu_reset(afu);
  422. if (rc)
  423. return rc;
  424. return cxl_ops->afu_check_and_enable(afu);
  425. }
  426. EXPORT_SYMBOL_GPL(cxl_afu_reset);
  427. void cxl_perst_reloads_same_image(struct cxl_afu *afu,
  428. bool perst_reloads_same_image)
  429. {
  430. afu->adapter->perst_same_image = perst_reloads_same_image;
  431. }
  432. EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
  433. ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
  434. {
  435. struct cxl_afu *afu = cxl_pci_to_afu(dev);
  436. if (IS_ERR(afu))
  437. return -ENODEV;
  438. return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
  439. }
  440. EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);