vphb.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 <asm/pnv-pci.h>
  12. #include "cxl.h"
  13. static int cxl_dma_set_mask(struct pci_dev *pdev, u64 dma_mask)
  14. {
  15. if (dma_mask < DMA_BIT_MASK(64)) {
  16. pr_info("%s only 64bit DMA supported on CXL", __func__);
  17. return -EIO;
  18. }
  19. *(pdev->dev.dma_mask) = dma_mask;
  20. return 0;
  21. }
  22. static int cxl_pci_probe_mode(struct pci_bus *bus)
  23. {
  24. return PCI_PROBE_NORMAL;
  25. }
  26. static int cxl_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  27. {
  28. return -ENODEV;
  29. }
  30. static void cxl_teardown_msi_irqs(struct pci_dev *pdev)
  31. {
  32. /*
  33. * MSI should never be set but need still need to provide this call
  34. * back.
  35. */
  36. }
  37. static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
  38. {
  39. struct pci_controller *phb;
  40. struct cxl_afu *afu;
  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. return _cxl_pci_associate_default_context(dev, afu);
  50. }
  51. static resource_size_t cxl_pci_window_alignment(struct pci_bus *bus,
  52. unsigned long type)
  53. {
  54. return 1;
  55. }
  56. static void cxl_pci_reset_secondary_bus(struct pci_dev *dev)
  57. {
  58. /* Should we do an AFU reset here ? */
  59. }
  60. static int cxl_pcie_cfg_record(u8 bus, u8 devfn)
  61. {
  62. return (bus << 8) + devfn;
  63. }
  64. static int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
  65. struct cxl_afu **_afu, int *_record)
  66. {
  67. struct pci_controller *phb;
  68. struct cxl_afu *afu;
  69. int record;
  70. phb = pci_bus_to_host(bus);
  71. if (phb == NULL)
  72. return PCIBIOS_DEVICE_NOT_FOUND;
  73. afu = (struct cxl_afu *)phb->private_data;
  74. record = cxl_pcie_cfg_record(bus->number, devfn);
  75. if (record > afu->crs_num)
  76. return PCIBIOS_DEVICE_NOT_FOUND;
  77. *_afu = afu;
  78. *_record = record;
  79. return 0;
  80. }
  81. static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  82. int offset, int len, u32 *val)
  83. {
  84. int rc, record;
  85. struct cxl_afu *afu;
  86. u8 val8;
  87. u16 val16;
  88. u32 val32;
  89. rc = cxl_pcie_config_info(bus, devfn, &afu, &record);
  90. if (rc)
  91. return rc;
  92. switch (len) {
  93. case 1:
  94. rc = cxl_ops->afu_cr_read8(afu, record, offset, &val8);
  95. *val = val8;
  96. break;
  97. case 2:
  98. rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
  99. *val = val16;
  100. break;
  101. case 4:
  102. rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
  103. *val = val32;
  104. break;
  105. default:
  106. WARN_ON(1);
  107. }
  108. if (rc)
  109. return PCIBIOS_DEVICE_NOT_FOUND;
  110. return PCIBIOS_SUCCESSFUL;
  111. }
  112. static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  113. int offset, int len, u32 val)
  114. {
  115. int rc, record;
  116. struct cxl_afu *afu;
  117. rc = cxl_pcie_config_info(bus, devfn, &afu, &record);
  118. if (rc)
  119. return rc;
  120. switch (len) {
  121. case 1:
  122. rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
  123. break;
  124. case 2:
  125. rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
  126. break;
  127. case 4:
  128. rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
  129. break;
  130. default:
  131. WARN_ON(1);
  132. }
  133. if (rc)
  134. return PCIBIOS_SET_FAILED;
  135. return PCIBIOS_SUCCESSFUL;
  136. }
  137. static struct pci_ops cxl_pcie_pci_ops =
  138. {
  139. .read = cxl_pcie_read_config,
  140. .write = cxl_pcie_write_config,
  141. };
  142. static struct pci_controller_ops cxl_pci_controller_ops =
  143. {
  144. .probe_mode = cxl_pci_probe_mode,
  145. .enable_device_hook = cxl_pci_enable_device_hook,
  146. .disable_device = _cxl_pci_disable_device,
  147. .release_device = _cxl_pci_disable_device,
  148. .window_alignment = cxl_pci_window_alignment,
  149. .reset_secondary_bus = cxl_pci_reset_secondary_bus,
  150. .setup_msi_irqs = cxl_setup_msi_irqs,
  151. .teardown_msi_irqs = cxl_teardown_msi_irqs,
  152. .dma_set_mask = cxl_dma_set_mask,
  153. };
  154. int cxl_pci_vphb_add(struct cxl_afu *afu)
  155. {
  156. struct pci_controller *phb;
  157. struct device_node *vphb_dn;
  158. struct device *parent;
  159. /*
  160. * If there are no AFU configuration records we won't have anything to
  161. * expose under the vPHB, so skip creating one, returning success since
  162. * this is still a valid case. This will also opt us out of EEH
  163. * handling since we won't have anything special to do if there are no
  164. * kernel drivers attached to the vPHB, and EEH handling is not yet
  165. * supported in the peer model.
  166. */
  167. if (!afu->crs_num)
  168. return 0;
  169. /* The parent device is the adapter. Reuse the device node of
  170. * the adapter.
  171. * We don't seem to care what device node is used for the vPHB,
  172. * but tools such as lsvpd walk up the device parents looking
  173. * for a valid location code, so we might as well show devices
  174. * attached to the adapter as being located on that adapter.
  175. */
  176. parent = afu->adapter->dev.parent;
  177. vphb_dn = parent->of_node;
  178. /* Alloc and setup PHB data structure */
  179. phb = pcibios_alloc_controller(vphb_dn);
  180. if (!phb)
  181. return -ENODEV;
  182. /* Setup parent in sysfs */
  183. phb->parent = parent;
  184. /* Setup the PHB using arch provided callback */
  185. phb->ops = &cxl_pcie_pci_ops;
  186. phb->cfg_addr = NULL;
  187. phb->cfg_data = NULL;
  188. phb->private_data = afu;
  189. phb->controller_ops = cxl_pci_controller_ops;
  190. /* Scan the bus */
  191. pcibios_scan_phb(phb);
  192. if (phb->bus == NULL)
  193. return -ENXIO;
  194. /* Set release hook on root bus */
  195. pci_set_host_bridge_release(to_pci_host_bridge(phb->bus->bridge),
  196. pcibios_free_controller_deferred,
  197. (void *) phb);
  198. /* Claim resources. This might need some rework as well depending
  199. * whether we are doing probe-only or not, like assigning unassigned
  200. * resources etc...
  201. */
  202. pcibios_claim_one_bus(phb->bus);
  203. /* Add probed PCI devices to the device model */
  204. pci_bus_add_devices(phb->bus);
  205. afu->phb = phb;
  206. return 0;
  207. }
  208. void cxl_pci_vphb_remove(struct cxl_afu *afu)
  209. {
  210. struct pci_controller *phb;
  211. /* If there is no configuration record we won't have one of these */
  212. if (!afu || !afu->phb)
  213. return;
  214. phb = afu->phb;
  215. afu->phb = NULL;
  216. pci_remove_root_bus(phb->bus);
  217. /*
  218. * We don't free phb here - that's handled by
  219. * pcibios_free_controller_deferred()
  220. */
  221. }
  222. static bool _cxl_pci_is_vphb_device(struct pci_controller *phb)
  223. {
  224. return (phb->ops == &cxl_pcie_pci_ops);
  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 _cxl_pci_is_vphb_device(phb);
  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. if (_cxl_pci_is_vphb_device(phb))
  237. return (struct cxl_afu *)phb->private_data;
  238. if (pnv_pci_on_cxl_phb(dev))
  239. return pnv_cxl_phb_to_afu(phb);
  240. return ERR_PTR(-ENODEV);
  241. }
  242. EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
  243. unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
  244. {
  245. return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
  246. }
  247. EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);