api.c 13 KB

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