api.c 12 KB

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