vphb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 <misc/cxl.h>
  11. #include "cxl.h"
  12. static int cxl_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
  13. {
  14. if (dma_mask < DMA_BIT_MASK(64)) {
  15. pr_info("%s only 64bit DMA supported on CXL", __func__);
  16. return -EIO;
  17. }
  18. *(pdev->dev.dma_mask) = dma_mask;
  19. return 0;
  20. }
  21. static int cxl_pci_probe_mode(struct pci_bus *bus)
  22. {
  23. return PCI_PROBE_NORMAL;
  24. }
  25. static int cxl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  26. {
  27. return -ENODEV;
  28. }
  29. static void cxl_teardown_msi_irqs(struct pci_dev *pdev)
  30. {
  31. /*
  32. * MSI should never be set but need still need to provide this call
  33. * back.
  34. */
  35. }
  36. static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
  37. {
  38. struct pci_controller *phb;
  39. struct cxl_afu *afu;
  40. struct cxl_context *ctx;
  41. phb = pci_bus_to_host(dev->bus);
  42. afu = (struct cxl_afu *)phb->private_data;
  43. if (!cxl_ops->link_ok(afu->adapter, afu)) {
  44. dev_warn(&dev->dev, "%s: Device link is down, refusing to enable AFU\n", __func__);
  45. return false;
  46. }
  47. set_dma_ops(&dev->dev, &dma_direct_ops);
  48. set_dma_offset(&dev->dev, PAGE_OFFSET);
  49. /*
  50. * Allocate a context to do cxl things too. If we eventually do real
  51. * DMA ops, we'll need a default context to attach them to
  52. */
  53. ctx = cxl_dev_context_init(dev);
  54. if (!ctx)
  55. return false;
  56. dev->dev.archdata.cxl_ctx = ctx;
  57. return (cxl_ops->afu_check_and_enable(afu) == 0);
  58. }
  59. static void cxl_pci_disable_device(struct pci_dev *dev)
  60. {
  61. struct cxl_context *ctx = cxl_get_context(dev);
  62. if (ctx) {
  63. if (ctx->status == STARTED) {
  64. dev_err(&dev->dev, "Default context started\n");
  65. return;
  66. }
  67. dev->dev.archdata.cxl_ctx = NULL;
  68. cxl_release_context(ctx);
  69. }
  70. }
  71. static resource_size_t cxl_pci_window_alignment(struct pci_bus *bus,
  72. unsigned long type)
  73. {
  74. return 1;
  75. }
  76. static void cxl_pci_reset_secondary_bus(struct pci_dev *dev)
  77. {
  78. /* Should we do an AFU reset here ? */
  79. }
  80. static int cxl_pcie_cfg_record(u8 bus, u8 devfn)
  81. {
  82. return (bus << 8) + devfn;
  83. }
  84. static int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
  85. struct cxl_afu **_afu, int *_record)
  86. {
  87. struct pci_controller *phb;
  88. struct cxl_afu *afu;
  89. int record;
  90. phb = pci_bus_to_host(bus);
  91. if (phb == NULL)
  92. return PCIBIOS_DEVICE_NOT_FOUND;
  93. afu = (struct cxl_afu *)phb->private_data;
  94. record = cxl_pcie_cfg_record(bus->number, devfn);
  95. if (record > afu->crs_num)
  96. return PCIBIOS_DEVICE_NOT_FOUND;
  97. *_afu = afu;
  98. *_record = record;
  99. return 0;
  100. }
  101. static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  102. int offset, int len, u32 *val)
  103. {
  104. int rc, record;
  105. struct cxl_afu *afu;
  106. u8 val8;
  107. u16 val16;
  108. u32 val32;
  109. rc = cxl_pcie_config_info(bus, devfn, &afu, &record);
  110. if (rc)
  111. return rc;
  112. switch (len) {
  113. case 1:
  114. rc = cxl_ops->afu_cr_read8(afu, record, offset, &val8);
  115. *val = val8;
  116. break;
  117. case 2:
  118. rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
  119. *val = val16;
  120. break;
  121. case 4:
  122. rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
  123. *val = val32;
  124. break;
  125. default:
  126. WARN_ON(1);
  127. }
  128. if (rc)
  129. return PCIBIOS_DEVICE_NOT_FOUND;
  130. return PCIBIOS_SUCCESSFUL;
  131. }
  132. static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  133. int offset, int len, u32 val)
  134. {
  135. int rc, record;
  136. struct cxl_afu *afu;
  137. rc = cxl_pcie_config_info(bus, devfn, &afu, &record);
  138. if (rc)
  139. return rc;
  140. switch (len) {
  141. case 1:
  142. rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
  143. break;
  144. case 2:
  145. rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
  146. break;
  147. case 4:
  148. rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
  149. break;
  150. default:
  151. WARN_ON(1);
  152. }
  153. if (rc)
  154. return PCIBIOS_SET_FAILED;
  155. return PCIBIOS_SUCCESSFUL;
  156. }
  157. static struct pci_ops cxl_pcie_pci_ops =
  158. {
  159. .read = cxl_pcie_read_config,
  160. .write = cxl_pcie_write_config,
  161. };
  162. static struct pci_controller_ops cxl_pci_controller_ops =
  163. {
  164. .probe_mode = cxl_pci_probe_mode,
  165. .enable_device_hook = cxl_pci_enable_device_hook,
  166. .disable_device = cxl_pci_disable_device,
  167. .release_device = cxl_pci_disable_device,
  168. .window_alignment = cxl_pci_window_alignment,
  169. .reset_secondary_bus = cxl_pci_reset_secondary_bus,
  170. .setup_msi_irqs = cxl_setup_msi_irqs,
  171. .teardown_msi_irqs = cxl_teardown_msi_irqs,
  172. .dma_set_mask = cxl_dma_set_mask,
  173. };
  174. int cxl_pci_vphb_add(struct cxl_afu *afu)
  175. {
  176. struct pci_dev *phys_dev;
  177. struct pci_controller *phb, *phys_phb;
  178. struct device_node *vphb_dn;
  179. struct device *parent;
  180. if (cpu_has_feature(CPU_FTR_HVMODE)) {
  181. phys_dev = to_pci_dev(afu->adapter->dev.parent);
  182. phys_phb = pci_bus_to_host(phys_dev->bus);
  183. vphb_dn = phys_phb->dn;
  184. parent = &phys_dev->dev;
  185. } else {
  186. vphb_dn = afu->adapter->dev.parent->of_node;
  187. parent = afu->adapter->dev.parent;
  188. }
  189. /* Alloc and setup PHB data structure */
  190. phb = pcibios_alloc_controller(vphb_dn);
  191. if (!phb)
  192. return -ENODEV;
  193. /* Setup parent in sysfs */
  194. phb->parent = parent;
  195. /* Setup the PHB using arch provided callback */
  196. phb->ops = &cxl_pcie_pci_ops;
  197. phb->cfg_addr = NULL;
  198. phb->cfg_data = 0;
  199. phb->private_data = afu;
  200. phb->controller_ops = cxl_pci_controller_ops;
  201. /* Scan the bus */
  202. pcibios_scan_phb(phb);
  203. if (phb->bus == NULL)
  204. return -ENXIO;
  205. /* Claim resources. This might need some rework as well depending
  206. * whether we are doing probe-only or not, like assigning unassigned
  207. * resources etc...
  208. */
  209. pcibios_claim_one_bus(phb->bus);
  210. /* Add probed PCI devices to the device model */
  211. pci_bus_add_devices(phb->bus);
  212. afu->phb = phb;
  213. return 0;
  214. }
  215. void cxl_pci_vphb_remove(struct cxl_afu *afu)
  216. {
  217. struct pci_controller *phb;
  218. /* If there is no configuration record we won't have one of these */
  219. if (!afu || !afu->phb)
  220. return;
  221. phb = afu->phb;
  222. afu->phb = NULL;
  223. pci_remove_root_bus(phb->bus);
  224. pcibios_free_controller(phb);
  225. }
  226. bool cxl_pci_is_vphb_device(struct pci_dev *dev)
  227. {
  228. struct pci_controller *phb;
  229. phb = pci_bus_to_host(dev->bus);
  230. return (phb->ops == &cxl_pcie_pci_ops);
  231. }
  232. struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
  233. {
  234. struct pci_controller *phb;
  235. phb = pci_bus_to_host(dev->bus);
  236. return (struct cxl_afu *)phb->private_data;
  237. }
  238. EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
  239. unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
  240. {
  241. return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
  242. }
  243. EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);