api.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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 <linux/msi.h>
  17. #include "cxl.h"
  18. struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
  19. {
  20. struct address_space *mapping;
  21. struct cxl_afu *afu;
  22. struct cxl_context *ctx;
  23. int rc;
  24. afu = cxl_pci_to_afu(dev);
  25. if (IS_ERR(afu))
  26. return ERR_CAST(afu);
  27. ctx = cxl_context_alloc();
  28. if (IS_ERR(ctx)) {
  29. rc = PTR_ERR(ctx);
  30. goto err_dev;
  31. }
  32. ctx->kernelapi = true;
  33. /*
  34. * Make our own address space since we won't have one from the
  35. * filesystem like the user api has, and even if we do associate a file
  36. * with this context we don't want to use the global anonymous inode's
  37. * address space as that can invalidate unrelated users:
  38. */
  39. mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
  40. if (!mapping) {
  41. rc = -ENOMEM;
  42. goto err_ctx;
  43. }
  44. address_space_init_once(mapping);
  45. /* Make it a slave context. We can promote it later? */
  46. rc = cxl_context_init(ctx, afu, false, mapping);
  47. if (rc)
  48. goto err_mapping;
  49. return ctx;
  50. err_mapping:
  51. kfree(mapping);
  52. err_ctx:
  53. kfree(ctx);
  54. err_dev:
  55. return ERR_PTR(rc);
  56. }
  57. EXPORT_SYMBOL_GPL(cxl_dev_context_init);
  58. struct cxl_context *cxl_get_context(struct pci_dev *dev)
  59. {
  60. return dev->dev.archdata.cxl_ctx;
  61. }
  62. EXPORT_SYMBOL_GPL(cxl_get_context);
  63. int cxl_release_context(struct cxl_context *ctx)
  64. {
  65. if (ctx->status >= STARTED)
  66. return -EBUSY;
  67. cxl_context_free(ctx);
  68. return 0;
  69. }
  70. EXPORT_SYMBOL_GPL(cxl_release_context);
  71. static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
  72. {
  73. __u16 range;
  74. int r;
  75. for (r = 0; r < CXL_IRQ_RANGES; r++) {
  76. range = ctx->irqs.range[r];
  77. if (num < range) {
  78. return ctx->irqs.offset[r] + num;
  79. }
  80. num -= range;
  81. }
  82. return 0;
  83. }
  84. int _cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq)
  85. {
  86. if (*ctx == NULL || *afu_irq == 0) {
  87. *afu_irq = 1;
  88. *ctx = cxl_get_context(pdev);
  89. } else {
  90. (*afu_irq)++;
  91. if (*afu_irq > cxl_get_max_irqs_per_process(pdev)) {
  92. *ctx = list_next_entry(*ctx, extra_irq_contexts);
  93. *afu_irq = 1;
  94. }
  95. }
  96. return cxl_find_afu_irq(*ctx, *afu_irq);
  97. }
  98. /* Exported via cxl_base */
  99. int cxl_set_priv(struct cxl_context *ctx, void *priv)
  100. {
  101. if (!ctx)
  102. return -EINVAL;
  103. ctx->priv = priv;
  104. return 0;
  105. }
  106. EXPORT_SYMBOL_GPL(cxl_set_priv);
  107. void *cxl_get_priv(struct cxl_context *ctx)
  108. {
  109. if (!ctx)
  110. return ERR_PTR(-EINVAL);
  111. return ctx->priv;
  112. }
  113. EXPORT_SYMBOL_GPL(cxl_get_priv);
  114. int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
  115. {
  116. int res;
  117. irq_hw_number_t hwirq;
  118. if (num == 0)
  119. num = ctx->afu->pp_irqs;
  120. res = afu_allocate_irqs(ctx, num);
  121. if (res)
  122. return res;
  123. if (!cpu_has_feature(CPU_FTR_HVMODE)) {
  124. /* In a guest, the PSL interrupt is not multiplexed. It was
  125. * allocated above, and we need to set its handler
  126. */
  127. hwirq = cxl_find_afu_irq(ctx, 0);
  128. if (hwirq)
  129. cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
  130. }
  131. if (ctx->status == STARTED) {
  132. if (cxl_ops->update_ivtes)
  133. cxl_ops->update_ivtes(ctx);
  134. else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
  135. }
  136. return res;
  137. }
  138. EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
  139. void cxl_free_afu_irqs(struct cxl_context *ctx)
  140. {
  141. irq_hw_number_t hwirq;
  142. unsigned int virq;
  143. if (!cpu_has_feature(CPU_FTR_HVMODE)) {
  144. hwirq = cxl_find_afu_irq(ctx, 0);
  145. if (hwirq) {
  146. virq = irq_find_mapping(NULL, hwirq);
  147. if (virq)
  148. cxl_unmap_irq(virq, ctx);
  149. }
  150. }
  151. afu_irq_name_free(ctx);
  152. cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  153. }
  154. EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
  155. int cxl_map_afu_irq(struct cxl_context *ctx, int num,
  156. irq_handler_t handler, void *cookie, char *name)
  157. {
  158. irq_hw_number_t hwirq;
  159. /*
  160. * Find interrupt we are to register.
  161. */
  162. hwirq = cxl_find_afu_irq(ctx, num);
  163. if (!hwirq)
  164. return -ENOENT;
  165. return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
  166. }
  167. EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
  168. void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
  169. {
  170. irq_hw_number_t hwirq;
  171. unsigned int virq;
  172. hwirq = cxl_find_afu_irq(ctx, num);
  173. if (!hwirq)
  174. return;
  175. virq = irq_find_mapping(NULL, hwirq);
  176. if (virq)
  177. cxl_unmap_irq(virq, cookie);
  178. }
  179. EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
  180. /*
  181. * Start a context
  182. * Code here similar to afu_ioctl_start_work().
  183. */
  184. int cxl_start_context(struct cxl_context *ctx, u64 wed,
  185. struct task_struct *task)
  186. {
  187. int rc = 0;
  188. bool kernel = true;
  189. pr_devel("%s: pe: %i\n", __func__, ctx->pe);
  190. mutex_lock(&ctx->status_mutex);
  191. if (ctx->status == STARTED)
  192. goto out; /* already started */
  193. /*
  194. * Increment the mapped context count for adapter. This also checks
  195. * if adapter_context_lock is taken.
  196. */
  197. rc = cxl_adapter_context_get(ctx->afu->adapter);
  198. if (rc)
  199. goto out;
  200. if (task) {
  201. ctx->pid = get_task_pid(task, PIDTYPE_PID);
  202. ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
  203. kernel = false;
  204. ctx->real_mode = false;
  205. }
  206. cxl_ctx_get();
  207. if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
  208. put_pid(ctx->pid);
  209. cxl_adapter_context_put(ctx->afu->adapter);
  210. cxl_ctx_put();
  211. goto out;
  212. }
  213. ctx->status = STARTED;
  214. out:
  215. mutex_unlock(&ctx->status_mutex);
  216. return rc;
  217. }
  218. EXPORT_SYMBOL_GPL(cxl_start_context);
  219. int cxl_process_element(struct cxl_context *ctx)
  220. {
  221. return ctx->external_pe;
  222. }
  223. EXPORT_SYMBOL_GPL(cxl_process_element);
  224. /* Stop a context. Returns 0 on success, otherwise -Errno */
  225. int cxl_stop_context(struct cxl_context *ctx)
  226. {
  227. return __detach_context(ctx);
  228. }
  229. EXPORT_SYMBOL_GPL(cxl_stop_context);
  230. void cxl_set_master(struct cxl_context *ctx)
  231. {
  232. ctx->master = true;
  233. }
  234. EXPORT_SYMBOL_GPL(cxl_set_master);
  235. int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
  236. {
  237. if (ctx->status == STARTED) {
  238. /*
  239. * We could potentially update the PE and issue an update LLCMD
  240. * to support this, but it doesn't seem to have a good use case
  241. * since it's trivial to just create a second kernel context
  242. * with different translation modes, so until someone convinces
  243. * me otherwise:
  244. */
  245. return -EBUSY;
  246. }
  247. ctx->real_mode = real_mode;
  248. return 0;
  249. }
  250. EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
  251. /* wrappers around afu_* file ops which are EXPORTED */
  252. int cxl_fd_open(struct inode *inode, struct file *file)
  253. {
  254. return afu_open(inode, file);
  255. }
  256. EXPORT_SYMBOL_GPL(cxl_fd_open);
  257. int cxl_fd_release(struct inode *inode, struct file *file)
  258. {
  259. return afu_release(inode, file);
  260. }
  261. EXPORT_SYMBOL_GPL(cxl_fd_release);
  262. long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  263. {
  264. return afu_ioctl(file, cmd, arg);
  265. }
  266. EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
  267. int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
  268. {
  269. return afu_mmap(file, vm);
  270. }
  271. EXPORT_SYMBOL_GPL(cxl_fd_mmap);
  272. unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
  273. {
  274. return afu_poll(file, poll);
  275. }
  276. EXPORT_SYMBOL_GPL(cxl_fd_poll);
  277. ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
  278. loff_t *off)
  279. {
  280. return afu_read(file, buf, count, off);
  281. }
  282. EXPORT_SYMBOL_GPL(cxl_fd_read);
  283. #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
  284. /* Get a struct file and fd for a context and attach the ops */
  285. struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
  286. int *fd)
  287. {
  288. struct file *file;
  289. int rc, flags, fdtmp;
  290. flags = O_RDWR | O_CLOEXEC;
  291. /* This code is similar to anon_inode_getfd() */
  292. rc = get_unused_fd_flags(flags);
  293. if (rc < 0)
  294. return ERR_PTR(rc);
  295. fdtmp = rc;
  296. /*
  297. * Patch the file ops. Needs to be careful that this is rentrant safe.
  298. */
  299. if (fops) {
  300. PATCH_FOPS(open);
  301. PATCH_FOPS(poll);
  302. PATCH_FOPS(read);
  303. PATCH_FOPS(release);
  304. PATCH_FOPS(unlocked_ioctl);
  305. PATCH_FOPS(compat_ioctl);
  306. PATCH_FOPS(mmap);
  307. } else /* use default ops */
  308. fops = (struct file_operations *)&afu_fops;
  309. file = anon_inode_getfile("cxl", fops, ctx, flags);
  310. if (IS_ERR(file))
  311. goto err_fd;
  312. file->f_mapping = ctx->mapping;
  313. *fd = fdtmp;
  314. return file;
  315. err_fd:
  316. put_unused_fd(fdtmp);
  317. return NULL;
  318. }
  319. EXPORT_SYMBOL_GPL(cxl_get_fd);
  320. struct cxl_context *cxl_fops_get_context(struct file *file)
  321. {
  322. return file->private_data;
  323. }
  324. EXPORT_SYMBOL_GPL(cxl_fops_get_context);
  325. void cxl_set_driver_ops(struct cxl_context *ctx,
  326. struct cxl_afu_driver_ops *ops)
  327. {
  328. WARN_ON(!ops->fetch_event || !ops->event_delivered);
  329. atomic_set(&ctx->afu_driver_events, 0);
  330. ctx->afu_driver_ops = ops;
  331. }
  332. EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
  333. void cxl_context_events_pending(struct cxl_context *ctx,
  334. unsigned int new_events)
  335. {
  336. atomic_add(new_events, &ctx->afu_driver_events);
  337. wake_up_all(&ctx->wq);
  338. }
  339. EXPORT_SYMBOL_GPL(cxl_context_events_pending);
  340. int cxl_start_work(struct cxl_context *ctx,
  341. struct cxl_ioctl_start_work *work)
  342. {
  343. int rc;
  344. /* code taken from afu_ioctl_start_work */
  345. if (!(work->flags & CXL_START_WORK_NUM_IRQS))
  346. work->num_interrupts = ctx->afu->pp_irqs;
  347. else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
  348. (work->num_interrupts > ctx->afu->irqs_max)) {
  349. return -EINVAL;
  350. }
  351. rc = afu_register_irqs(ctx, work->num_interrupts);
  352. if (rc)
  353. return rc;
  354. rc = cxl_start_context(ctx, work->work_element_descriptor, current);
  355. if (rc < 0) {
  356. afu_release_irqs(ctx, ctx);
  357. return rc;
  358. }
  359. return 0;
  360. }
  361. EXPORT_SYMBOL_GPL(cxl_start_work);
  362. void __iomem *cxl_psa_map(struct cxl_context *ctx)
  363. {
  364. if (ctx->status != STARTED)
  365. return NULL;
  366. pr_devel("%s: psn_phys%llx size:%llx\n",
  367. __func__, ctx->psn_phys, ctx->psn_size);
  368. return ioremap(ctx->psn_phys, ctx->psn_size);
  369. }
  370. EXPORT_SYMBOL_GPL(cxl_psa_map);
  371. void cxl_psa_unmap(void __iomem *addr)
  372. {
  373. iounmap(addr);
  374. }
  375. EXPORT_SYMBOL_GPL(cxl_psa_unmap);
  376. int cxl_afu_reset(struct cxl_context *ctx)
  377. {
  378. struct cxl_afu *afu = ctx->afu;
  379. int rc;
  380. rc = cxl_ops->afu_reset(afu);
  381. if (rc)
  382. return rc;
  383. return cxl_ops->afu_check_and_enable(afu);
  384. }
  385. EXPORT_SYMBOL_GPL(cxl_afu_reset);
  386. void cxl_perst_reloads_same_image(struct cxl_afu *afu,
  387. bool perst_reloads_same_image)
  388. {
  389. afu->adapter->perst_same_image = perst_reloads_same_image;
  390. }
  391. EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
  392. ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
  393. {
  394. struct cxl_afu *afu = cxl_pci_to_afu(dev);
  395. if (IS_ERR(afu))
  396. return -ENODEV;
  397. return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
  398. }
  399. EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
  400. int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs)
  401. {
  402. struct cxl_afu *afu = cxl_pci_to_afu(dev);
  403. if (IS_ERR(afu))
  404. return -ENODEV;
  405. if (irqs > afu->adapter->user_irqs)
  406. return -EINVAL;
  407. /* Limit user_irqs to prevent the user increasing this via sysfs */
  408. afu->adapter->user_irqs = irqs;
  409. afu->irqs_max = irqs;
  410. return 0;
  411. }
  412. EXPORT_SYMBOL_GPL(cxl_set_max_irqs_per_process);
  413. int cxl_get_max_irqs_per_process(struct pci_dev *dev)
  414. {
  415. struct cxl_afu *afu = cxl_pci_to_afu(dev);
  416. if (IS_ERR(afu))
  417. return -ENODEV;
  418. return afu->irqs_max;
  419. }
  420. EXPORT_SYMBOL_GPL(cxl_get_max_irqs_per_process);
  421. /*
  422. * This is a special interrupt allocation routine called from the PHB's MSI
  423. * setup function. When capi interrupts are allocated in this manner they must
  424. * still be associated with a running context, but since the MSI APIs have no
  425. * way to specify this we use the default context associated with the device.
  426. *
  427. * The Mellanox CX4 has a hardware limitation that restricts the maximum AFU
  428. * interrupt number, so in order to overcome this their driver informs us of
  429. * the restriction by setting the maximum interrupts per context, and we
  430. * allocate additional contexts as necessary so that we can keep the AFU
  431. * interrupt number within the supported range.
  432. */
  433. int _cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  434. {
  435. struct cxl_context *ctx, *new_ctx, *default_ctx;
  436. int remaining;
  437. int rc;
  438. ctx = default_ctx = cxl_get_context(pdev);
  439. if (WARN_ON(!default_ctx))
  440. return -ENODEV;
  441. remaining = nvec;
  442. while (remaining > 0) {
  443. rc = cxl_allocate_afu_irqs(ctx, min(remaining, ctx->afu->irqs_max));
  444. if (rc) {
  445. pr_warn("%s: Failed to find enough free MSIs\n", pci_name(pdev));
  446. return rc;
  447. }
  448. remaining -= ctx->afu->irqs_max;
  449. if (ctx != default_ctx && default_ctx->status == STARTED) {
  450. WARN_ON(cxl_start_context(ctx,
  451. be64_to_cpu(default_ctx->elem->common.wed),
  452. NULL));
  453. }
  454. if (remaining > 0) {
  455. new_ctx = cxl_dev_context_init(pdev);
  456. if (!new_ctx) {
  457. pr_warn("%s: Failed to allocate enough contexts for MSIs\n", pci_name(pdev));
  458. return -ENOSPC;
  459. }
  460. list_add(&new_ctx->extra_irq_contexts, &ctx->extra_irq_contexts);
  461. ctx = new_ctx;
  462. }
  463. }
  464. return 0;
  465. }
  466. /* Exported via cxl_base */
  467. void _cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
  468. {
  469. struct cxl_context *ctx, *pos, *tmp;
  470. ctx = cxl_get_context(pdev);
  471. if (WARN_ON(!ctx))
  472. return;
  473. cxl_free_afu_irqs(ctx);
  474. list_for_each_entry_safe(pos, tmp, &ctx->extra_irq_contexts, extra_irq_contexts) {
  475. cxl_stop_context(pos);
  476. cxl_free_afu_irqs(pos);
  477. list_del(&pos->extra_irq_contexts);
  478. cxl_release_context(pos);
  479. }
  480. }
  481. /* Exported via cxl_base */