api.c 13 KB

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