vphb.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 inline struct cxl_afu *pci_bus_to_afu(struct pci_bus *bus)
  65. {
  66. struct pci_controller *phb = bus ? pci_bus_to_host(bus) : NULL;
  67. return phb ? phb->private_data : NULL;
  68. }
  69. static void cxl_afu_configured_put(struct cxl_afu *afu)
  70. {
  71. atomic_dec_if_positive(&afu->configured_state);
  72. }
  73. static bool cxl_afu_configured_get(struct cxl_afu *afu)
  74. {
  75. return atomic_inc_unless_negative(&afu->configured_state);
  76. }
  77. static inline int cxl_pcie_config_info(struct pci_bus *bus, unsigned int devfn,
  78. struct cxl_afu *afu, int *_record)
  79. {
  80. int record;
  81. record = cxl_pcie_cfg_record(bus->number, devfn);
  82. if (record > afu->crs_num)
  83. return PCIBIOS_DEVICE_NOT_FOUND;
  84. *_record = record;
  85. return 0;
  86. }
  87. static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
  88. int offset, int len, u32 *val)
  89. {
  90. int rc, record;
  91. struct cxl_afu *afu;
  92. u8 val8;
  93. u16 val16;
  94. u32 val32;
  95. afu = pci_bus_to_afu(bus);
  96. /* Grab a reader lock on afu. */
  97. if (afu == NULL || !cxl_afu_configured_get(afu))
  98. return PCIBIOS_DEVICE_NOT_FOUND;
  99. rc = cxl_pcie_config_info(bus, devfn, afu, &record);
  100. if (rc)
  101. goto out;
  102. switch (len) {
  103. case 1:
  104. rc = cxl_ops->afu_cr_read8(afu, record, offset, &val8);
  105. *val = val8;
  106. break;
  107. case 2:
  108. rc = cxl_ops->afu_cr_read16(afu, record, offset, &val16);
  109. *val = val16;
  110. break;
  111. case 4:
  112. rc = cxl_ops->afu_cr_read32(afu, record, offset, &val32);
  113. *val = val32;
  114. break;
  115. default:
  116. WARN_ON(1);
  117. }
  118. out:
  119. cxl_afu_configured_put(afu);
  120. return rc ? PCIBIOS_DEVICE_NOT_FOUND : PCIBIOS_SUCCESSFUL;
  121. }
  122. static int cxl_pcie_write_config(struct pci_bus *bus, unsigned int devfn,
  123. int offset, int len, u32 val)
  124. {
  125. int rc, record;
  126. struct cxl_afu *afu;
  127. afu = pci_bus_to_afu(bus);
  128. /* Grab a reader lock on afu. */
  129. if (afu == NULL || !cxl_afu_configured_get(afu))
  130. return PCIBIOS_DEVICE_NOT_FOUND;
  131. rc = cxl_pcie_config_info(bus, devfn, afu, &record);
  132. if (rc)
  133. goto out;
  134. switch (len) {
  135. case 1:
  136. rc = cxl_ops->afu_cr_write8(afu, record, offset, val & 0xff);
  137. break;
  138. case 2:
  139. rc = cxl_ops->afu_cr_write16(afu, record, offset, val & 0xffff);
  140. break;
  141. case 4:
  142. rc = cxl_ops->afu_cr_write32(afu, record, offset, val);
  143. break;
  144. default:
  145. WARN_ON(1);
  146. }
  147. out:
  148. cxl_afu_configured_put(afu);
  149. return rc ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL;
  150. }
  151. static struct pci_ops cxl_pcie_pci_ops =
  152. {
  153. .read = cxl_pcie_read_config,
  154. .write = cxl_pcie_write_config,
  155. };
  156. static struct pci_controller_ops cxl_pci_controller_ops =
  157. {
  158. .probe_mode = cxl_pci_probe_mode,
  159. .enable_device_hook = cxl_pci_enable_device_hook,
  160. .disable_device = _cxl_pci_disable_device,
  161. .release_device = _cxl_pci_disable_device,
  162. .window_alignment = cxl_pci_window_alignment,
  163. .reset_secondary_bus = cxl_pci_reset_secondary_bus,
  164. .setup_msi_irqs = cxl_setup_msi_irqs,
  165. .teardown_msi_irqs = cxl_teardown_msi_irqs,
  166. .dma_set_mask = cxl_dma_set_mask,
  167. };
  168. int cxl_pci_vphb_add(struct cxl_afu *afu)
  169. {
  170. struct pci_controller *phb;
  171. struct device_node *vphb_dn;
  172. struct device *parent;
  173. /*
  174. * If there are no AFU configuration records we won't have anything to
  175. * expose under the vPHB, so skip creating one, returning success since
  176. * this is still a valid case. This will also opt us out of EEH
  177. * handling since we won't have anything special to do if there are no
  178. * kernel drivers attached to the vPHB, and EEH handling is not yet
  179. * supported in the peer model.
  180. */
  181. if (!afu->crs_num)
  182. return 0;
  183. /* The parent device is the adapter. Reuse the device node of
  184. * the adapter.
  185. * We don't seem to care what device node is used for the vPHB,
  186. * but tools such as lsvpd walk up the device parents looking
  187. * for a valid location code, so we might as well show devices
  188. * attached to the adapter as being located on that adapter.
  189. */
  190. parent = afu->adapter->dev.parent;
  191. vphb_dn = parent->of_node;
  192. /* Alloc and setup PHB data structure */
  193. phb = pcibios_alloc_controller(vphb_dn);
  194. if (!phb)
  195. return -ENODEV;
  196. /* Setup parent in sysfs */
  197. phb->parent = parent;
  198. /* Setup the PHB using arch provided callback */
  199. phb->ops = &cxl_pcie_pci_ops;
  200. phb->cfg_addr = NULL;
  201. phb->cfg_data = NULL;
  202. phb->private_data = afu;
  203. phb->controller_ops = cxl_pci_controller_ops;
  204. /* Scan the bus */
  205. pcibios_scan_phb(phb);
  206. if (phb->bus == NULL)
  207. return -ENXIO;
  208. /* Set release hook on root bus */
  209. pci_set_host_bridge_release(to_pci_host_bridge(phb->bus->bridge),
  210. pcibios_free_controller_deferred,
  211. (void *) phb);
  212. /* Claim resources. This might need some rework as well depending
  213. * whether we are doing probe-only or not, like assigning unassigned
  214. * resources etc...
  215. */
  216. pcibios_claim_one_bus(phb->bus);
  217. /* Add probed PCI devices to the device model */
  218. pci_bus_add_devices(phb->bus);
  219. afu->phb = phb;
  220. return 0;
  221. }
  222. void cxl_pci_vphb_remove(struct cxl_afu *afu)
  223. {
  224. struct pci_controller *phb;
  225. /* If there is no configuration record we won't have one of these */
  226. if (!afu || !afu->phb)
  227. return;
  228. phb = afu->phb;
  229. afu->phb = NULL;
  230. pci_remove_root_bus(phb->bus);
  231. /*
  232. * We don't free phb here - that's handled by
  233. * pcibios_free_controller_deferred()
  234. */
  235. }
  236. static bool _cxl_pci_is_vphb_device(struct pci_controller *phb)
  237. {
  238. return (phb->ops == &cxl_pcie_pci_ops);
  239. }
  240. bool cxl_pci_is_vphb_device(struct pci_dev *dev)
  241. {
  242. struct pci_controller *phb;
  243. phb = pci_bus_to_host(dev->bus);
  244. return _cxl_pci_is_vphb_device(phb);
  245. }
  246. struct cxl_afu *cxl_pci_to_afu(struct pci_dev *dev)
  247. {
  248. struct pci_controller *phb;
  249. phb = pci_bus_to_host(dev->bus);
  250. if (_cxl_pci_is_vphb_device(phb))
  251. return (struct cxl_afu *)phb->private_data;
  252. if (pnv_pci_on_cxl_phb(dev))
  253. return pnv_cxl_phb_to_afu(phb);
  254. return ERR_PTR(-ENODEV);
  255. }
  256. EXPORT_SYMBOL_GPL(cxl_pci_to_afu);
  257. unsigned int cxl_pci_to_cfg_record(struct pci_dev *dev)
  258. {
  259. return cxl_pcie_cfg_record(dev->bus->number, dev->devfn);
  260. }
  261. EXPORT_SYMBOL_GPL(cxl_pci_to_cfg_record);