pci_dn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * pci_dn.c
  4. *
  5. * Copyright (C) 2001 Todd Inglett, IBM Corporation
  6. *
  7. * PCI manipulation via device_nodes.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/pci.h>
  11. #include <linux/string.h>
  12. #include <linux/export.h>
  13. #include <linux/init.h>
  14. #include <linux/gfp.h>
  15. #include <linux/of.h>
  16. #include <asm/io.h>
  17. #include <asm/pci-bridge.h>
  18. #include <asm/ppc-pci.h>
  19. #include <asm/firmware.h>
  20. #include <asm/eeh.h>
  21. /*
  22. * The function is used to find the firmware data of one
  23. * specific PCI device, which is attached to the indicated
  24. * PCI bus. For VFs, their firmware data is linked to that
  25. * one of PF's bridge. For other devices, their firmware
  26. * data is linked to that of their bridge.
  27. */
  28. static struct pci_dn *pci_bus_to_pdn(struct pci_bus *bus)
  29. {
  30. struct pci_bus *pbus;
  31. struct device_node *dn;
  32. struct pci_dn *pdn;
  33. /*
  34. * We probably have virtual bus which doesn't
  35. * have associated bridge.
  36. */
  37. pbus = bus;
  38. while (pbus) {
  39. if (pci_is_root_bus(pbus) || pbus->self)
  40. break;
  41. pbus = pbus->parent;
  42. }
  43. /*
  44. * Except virtual bus, all PCI buses should
  45. * have device nodes.
  46. */
  47. dn = pci_bus_to_OF_node(pbus);
  48. pdn = dn ? PCI_DN(dn) : NULL;
  49. return pdn;
  50. }
  51. struct pci_dn *pci_get_pdn_by_devfn(struct pci_bus *bus,
  52. int devfn)
  53. {
  54. struct device_node *dn = NULL;
  55. struct pci_dn *parent, *pdn;
  56. struct pci_dev *pdev = NULL;
  57. /* Fast path: fetch from PCI device */
  58. list_for_each_entry(pdev, &bus->devices, bus_list) {
  59. if (pdev->devfn == devfn) {
  60. if (pdev->dev.archdata.pci_data)
  61. return pdev->dev.archdata.pci_data;
  62. dn = pci_device_to_OF_node(pdev);
  63. break;
  64. }
  65. }
  66. /* Fast path: fetch from device node */
  67. pdn = dn ? PCI_DN(dn) : NULL;
  68. if (pdn)
  69. return pdn;
  70. /* Slow path: fetch from firmware data hierarchy */
  71. parent = pci_bus_to_pdn(bus);
  72. if (!parent)
  73. return NULL;
  74. list_for_each_entry(pdn, &parent->child_list, list) {
  75. if (pdn->busno == bus->number &&
  76. pdn->devfn == devfn)
  77. return pdn;
  78. }
  79. return NULL;
  80. }
  81. struct pci_dn *pci_get_pdn(struct pci_dev *pdev)
  82. {
  83. struct device_node *dn;
  84. struct pci_dn *parent, *pdn;
  85. /* Search device directly */
  86. if (pdev->dev.archdata.pci_data)
  87. return pdev->dev.archdata.pci_data;
  88. /* Check device node */
  89. dn = pci_device_to_OF_node(pdev);
  90. pdn = dn ? PCI_DN(dn) : NULL;
  91. if (pdn)
  92. return pdn;
  93. /*
  94. * VFs don't have device nodes. We hook their
  95. * firmware data to PF's bridge.
  96. */
  97. parent = pci_bus_to_pdn(pdev->bus);
  98. if (!parent)
  99. return NULL;
  100. list_for_each_entry(pdn, &parent->child_list, list) {
  101. if (pdn->busno == pdev->bus->number &&
  102. pdn->devfn == pdev->devfn)
  103. return pdn;
  104. }
  105. return NULL;
  106. }
  107. #ifdef CONFIG_EEH
  108. static struct eeh_dev *eeh_dev_init(struct pci_dn *pdn)
  109. {
  110. struct eeh_dev *edev;
  111. /* Allocate EEH device */
  112. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  113. if (!edev)
  114. return NULL;
  115. /* Associate EEH device with OF node */
  116. pdn->edev = edev;
  117. edev->pdn = pdn;
  118. edev->bdfn = (pdn->busno << 8) | pdn->devfn;
  119. edev->controller = pdn->phb;
  120. return edev;
  121. }
  122. #endif /* CONFIG_EEH */
  123. #ifdef CONFIG_PCI_IOV
  124. static struct pci_dn *add_one_sriov_vf_pdn(struct pci_dn *parent,
  125. int busno, int devfn)
  126. {
  127. struct pci_dn *pdn;
  128. /* Except PHB, we always have the parent */
  129. if (!parent)
  130. return NULL;
  131. pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
  132. if (!pdn)
  133. return NULL;
  134. pdn->phb = parent->phb;
  135. pdn->parent = parent;
  136. pdn->busno = busno;
  137. pdn->devfn = devfn;
  138. pdn->pe_number = IODA_INVALID_PE;
  139. INIT_LIST_HEAD(&pdn->child_list);
  140. INIT_LIST_HEAD(&pdn->list);
  141. list_add_tail(&pdn->list, &parent->child_list);
  142. return pdn;
  143. }
  144. struct pci_dn *add_sriov_vf_pdns(struct pci_dev *pdev)
  145. {
  146. struct pci_dn *parent, *pdn;
  147. int i;
  148. /* Only support IOV for now */
  149. if (WARN_ON(!pdev->is_physfn))
  150. return NULL;
  151. /* Check if VFs have been populated */
  152. pdn = pci_get_pdn(pdev);
  153. if (!pdn || (pdn->flags & PCI_DN_FLAG_IOV_VF))
  154. return NULL;
  155. pdn->flags |= PCI_DN_FLAG_IOV_VF;
  156. parent = pci_bus_to_pdn(pdev->bus);
  157. if (!parent)
  158. return NULL;
  159. for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
  160. struct eeh_dev *edev __maybe_unused;
  161. pdn = add_one_sriov_vf_pdn(parent,
  162. pci_iov_virtfn_bus(pdev, i),
  163. pci_iov_virtfn_devfn(pdev, i));
  164. if (!pdn) {
  165. dev_warn(&pdev->dev, "%s: Cannot create firmware data for VF#%d\n",
  166. __func__, i);
  167. return NULL;
  168. }
  169. #ifdef CONFIG_EEH
  170. /* Create the EEH device for the VF */
  171. edev = eeh_dev_init(pdn);
  172. BUG_ON(!edev);
  173. /* FIXME: these should probably be populated by the EEH probe */
  174. edev->physfn = pdev;
  175. edev->vf_index = i;
  176. #endif /* CONFIG_EEH */
  177. }
  178. return pci_get_pdn(pdev);
  179. }
  180. void remove_sriov_vf_pdns(struct pci_dev *pdev)
  181. {
  182. struct pci_dn *parent;
  183. struct pci_dn *pdn, *tmp;
  184. int i;
  185. /* Only support IOV PF for now */
  186. if (WARN_ON(!pdev->is_physfn))
  187. return;
  188. /* Check if VFs have been populated */
  189. pdn = pci_get_pdn(pdev);
  190. if (!pdn || !(pdn->flags & PCI_DN_FLAG_IOV_VF))
  191. return;
  192. pdn->flags &= ~PCI_DN_FLAG_IOV_VF;
  193. parent = pci_bus_to_pdn(pdev->bus);
  194. if (!parent)
  195. return;
  196. /*
  197. * We might introduce flag to pci_dn in future
  198. * so that we can release VF's firmware data in
  199. * a batch mode.
  200. */
  201. for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
  202. struct eeh_dev *edev __maybe_unused;
  203. list_for_each_entry_safe(pdn, tmp,
  204. &parent->child_list, list) {
  205. if (pdn->busno != pci_iov_virtfn_bus(pdev, i) ||
  206. pdn->devfn != pci_iov_virtfn_devfn(pdev, i))
  207. continue;
  208. #ifdef CONFIG_EEH
  209. /*
  210. * Release EEH state for this VF. The PCI core
  211. * has already torn down the pci_dev for this VF, but
  212. * we're responsible to removing the eeh_dev since it
  213. * has the same lifetime as the pci_dn that spawned it.
  214. */
  215. edev = pdn_to_eeh_dev(pdn);
  216. if (edev) {
  217. /*
  218. * We allocate pci_dn's for the totalvfs count,
  219. * but only the vfs that were activated
  220. * have a configured PE.
  221. */
  222. if (edev->pe)
  223. eeh_pe_tree_remove(edev);
  224. pdn->edev = NULL;
  225. kfree(edev);
  226. }
  227. #endif /* CONFIG_EEH */
  228. if (!list_empty(&pdn->list))
  229. list_del(&pdn->list);
  230. kfree(pdn);
  231. }
  232. }
  233. }
  234. #endif /* CONFIG_PCI_IOV */
  235. struct pci_dn *pci_add_device_node_info(struct pci_controller *hose,
  236. struct device_node *dn)
  237. {
  238. const __be32 *type = of_get_property(dn, "ibm,pci-config-space-type", NULL);
  239. const __be32 *regs;
  240. struct device_node *parent;
  241. struct pci_dn *pdn;
  242. #ifdef CONFIG_EEH
  243. struct eeh_dev *edev;
  244. #endif
  245. pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
  246. if (pdn == NULL)
  247. return NULL;
  248. dn->data = pdn;
  249. pdn->phb = hose;
  250. pdn->pe_number = IODA_INVALID_PE;
  251. regs = of_get_property(dn, "reg", NULL);
  252. if (regs) {
  253. u32 addr = of_read_number(regs, 1);
  254. /* First register entry is addr (00BBSS00) */
  255. pdn->busno = (addr >> 16) & 0xff;
  256. pdn->devfn = (addr >> 8) & 0xff;
  257. }
  258. /* vendor/device IDs and class code */
  259. regs = of_get_property(dn, "vendor-id", NULL);
  260. pdn->vendor_id = regs ? of_read_number(regs, 1) : 0;
  261. regs = of_get_property(dn, "device-id", NULL);
  262. pdn->device_id = regs ? of_read_number(regs, 1) : 0;
  263. regs = of_get_property(dn, "class-code", NULL);
  264. pdn->class_code = regs ? of_read_number(regs, 1) : 0;
  265. /* Extended config space */
  266. pdn->pci_ext_config_space = (type && of_read_number(type, 1) == 1);
  267. /* Create EEH device */
  268. #ifdef CONFIG_EEH
  269. edev = eeh_dev_init(pdn);
  270. if (!edev) {
  271. kfree(pdn);
  272. return NULL;
  273. }
  274. #endif
  275. /* Attach to parent node */
  276. INIT_LIST_HEAD(&pdn->child_list);
  277. INIT_LIST_HEAD(&pdn->list);
  278. parent = of_get_parent(dn);
  279. pdn->parent = parent ? PCI_DN(parent) : NULL;
  280. of_node_put(parent);
  281. if (pdn->parent)
  282. list_add_tail(&pdn->list, &pdn->parent->child_list);
  283. return pdn;
  284. }
  285. EXPORT_SYMBOL_GPL(pci_add_device_node_info);
  286. void pci_remove_device_node_info(struct device_node *dn)
  287. {
  288. struct pci_dn *pdn = dn ? PCI_DN(dn) : NULL;
  289. struct device_node *parent;
  290. struct pci_dev *pdev;
  291. #ifdef CONFIG_EEH
  292. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  293. if (edev)
  294. edev->pdn = NULL;
  295. #endif
  296. if (!pdn)
  297. return;
  298. WARN_ON(!list_empty(&pdn->child_list));
  299. list_del(&pdn->list);
  300. /* Drop the parent pci_dn's ref to our backing dt node */
  301. parent = of_get_parent(dn);
  302. if (parent)
  303. of_node_put(parent);
  304. /*
  305. * At this point we *might* still have a pci_dev that was
  306. * instantiated from this pci_dn. So defer free()ing it until
  307. * the pci_dev's release function is called.
  308. */
  309. pdev = pci_get_domain_bus_and_slot(pdn->phb->global_number,
  310. pdn->busno, pdn->devfn);
  311. if (pdev) {
  312. /* NB: pdev has a ref to dn */
  313. pci_dbg(pdev, "marked pdn (from %pOF) as dead\n", dn);
  314. pdn->flags |= PCI_DN_FLAG_DEAD;
  315. } else {
  316. dn->data = NULL;
  317. kfree(pdn);
  318. }
  319. pci_dev_put(pdev);
  320. }
  321. EXPORT_SYMBOL_GPL(pci_remove_device_node_info);
  322. /*
  323. * Traverse a device tree stopping each PCI device in the tree.
  324. * This is done depth first. As each node is processed, a "pre"
  325. * function is called and the children are processed recursively.
  326. *
  327. * The "pre" func returns a value. If non-zero is returned from
  328. * the "pre" func, the traversal stops and this value is returned.
  329. * This return value is useful when using traverse as a method of
  330. * finding a device.
  331. *
  332. * NOTE: we do not run the func for devices that do not appear to
  333. * be PCI except for the start node which we assume (this is good
  334. * because the start node is often a phb which may be missing PCI
  335. * properties).
  336. * We use the class-code as an indicator. If we run into
  337. * one of these nodes we also assume its siblings are non-pci for
  338. * performance.
  339. */
  340. void *pci_traverse_device_nodes(struct device_node *start,
  341. void *(*fn)(struct device_node *, void *),
  342. void *data)
  343. {
  344. struct device_node *dn, *nextdn;
  345. void *ret;
  346. /* We started with a phb, iterate all childs */
  347. for (dn = start->child; dn; dn = nextdn) {
  348. const __be32 *classp;
  349. u32 class = 0;
  350. nextdn = NULL;
  351. classp = of_get_property(dn, "class-code", NULL);
  352. if (classp)
  353. class = of_read_number(classp, 1);
  354. if (fn) {
  355. ret = fn(dn, data);
  356. if (ret)
  357. return ret;
  358. }
  359. /* If we are a PCI bridge, go down */
  360. if (dn->child && ((class >> 8) == PCI_CLASS_BRIDGE_PCI ||
  361. (class >> 8) == PCI_CLASS_BRIDGE_CARDBUS))
  362. /* Depth first...do children */
  363. nextdn = dn->child;
  364. else if (dn->sibling)
  365. /* ok, try next sibling instead. */
  366. nextdn = dn->sibling;
  367. if (!nextdn) {
  368. /* Walk up to next valid sibling. */
  369. do {
  370. dn = dn->parent;
  371. if (dn == start)
  372. return NULL;
  373. } while (dn->sibling == NULL);
  374. nextdn = dn->sibling;
  375. }
  376. }
  377. return NULL;
  378. }
  379. EXPORT_SYMBOL_GPL(pci_traverse_device_nodes);
  380. static void *add_pdn(struct device_node *dn, void *data)
  381. {
  382. struct pci_controller *hose = data;
  383. struct pci_dn *pdn;
  384. pdn = pci_add_device_node_info(hose, dn);
  385. if (!pdn)
  386. return ERR_PTR(-ENOMEM);
  387. return NULL;
  388. }
  389. /**
  390. * pci_devs_phb_init_dynamic - setup pci devices under this PHB
  391. * phb: pci-to-host bridge (top-level bridge connecting to cpu)
  392. *
  393. * This routine is called both during boot, (before the memory
  394. * subsystem is set up, before kmalloc is valid) and during the
  395. * dynamic lpar operation of adding a PHB to a running system.
  396. */
  397. void pci_devs_phb_init_dynamic(struct pci_controller *phb)
  398. {
  399. struct device_node *dn = phb->dn;
  400. struct pci_dn *pdn;
  401. /* PHB nodes themselves must not match */
  402. pdn = pci_add_device_node_info(phb, dn);
  403. if (pdn) {
  404. pdn->devfn = pdn->busno = -1;
  405. pdn->vendor_id = pdn->device_id = pdn->class_code = 0;
  406. pdn->phb = phb;
  407. phb->pci_data = pdn;
  408. }
  409. /* Update dn->phb ptrs for new phb and children devices */
  410. pci_traverse_device_nodes(dn, add_pdn, phb);
  411. }
  412. static void pci_dev_pdn_setup(struct pci_dev *pdev)
  413. {
  414. struct pci_dn *pdn;
  415. if (pdev->dev.archdata.pci_data)
  416. return;
  417. /* Setup the fast path */
  418. pdn = pci_get_pdn(pdev);
  419. pdev->dev.archdata.pci_data = pdn;
  420. }
  421. DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pci_dev_pdn_setup);