pci_dn.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * pci_dn.c
  3. *
  4. * Copyright (C) 2001 Todd Inglett, IBM Corporation
  5. *
  6. * PCI manipulation via device_nodes.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/pci.h>
  24. #include <linux/string.h>
  25. #include <linux/export.h>
  26. #include <linux/init.h>
  27. #include <linux/gfp.h>
  28. #include <asm/io.h>
  29. #include <asm/prom.h>
  30. #include <asm/pci-bridge.h>
  31. #include <asm/ppc-pci.h>
  32. #include <asm/firmware.h>
  33. #include <asm/eeh.h>
  34. /*
  35. * The function is used to find the firmware data of one
  36. * specific PCI device, which is attached to the indicated
  37. * PCI bus. For VFs, their firmware data is linked to that
  38. * one of PF's bridge. For other devices, their firmware
  39. * data is linked to that of their bridge.
  40. */
  41. static struct pci_dn *pci_bus_to_pdn(struct pci_bus *bus)
  42. {
  43. struct pci_bus *pbus;
  44. struct device_node *dn;
  45. struct pci_dn *pdn;
  46. /*
  47. * We probably have virtual bus which doesn't
  48. * have associated bridge.
  49. */
  50. pbus = bus;
  51. while (pbus) {
  52. if (pci_is_root_bus(pbus) || pbus->self)
  53. break;
  54. pbus = pbus->parent;
  55. }
  56. /*
  57. * Except virtual bus, all PCI buses should
  58. * have device nodes.
  59. */
  60. dn = pci_bus_to_OF_node(pbus);
  61. pdn = dn ? PCI_DN(dn) : NULL;
  62. return pdn;
  63. }
  64. struct pci_dn *pci_get_pdn_by_devfn(struct pci_bus *bus,
  65. int devfn)
  66. {
  67. struct device_node *dn = NULL;
  68. struct pci_dn *parent, *pdn;
  69. struct pci_dev *pdev = NULL;
  70. /* Fast path: fetch from PCI device */
  71. list_for_each_entry(pdev, &bus->devices, bus_list) {
  72. if (pdev->devfn == devfn) {
  73. if (pdev->dev.archdata.pci_data)
  74. return pdev->dev.archdata.pci_data;
  75. dn = pci_device_to_OF_node(pdev);
  76. break;
  77. }
  78. }
  79. /* Fast path: fetch from device node */
  80. pdn = dn ? PCI_DN(dn) : NULL;
  81. if (pdn)
  82. return pdn;
  83. /* Slow path: fetch from firmware data hierarchy */
  84. parent = pci_bus_to_pdn(bus);
  85. if (!parent)
  86. return NULL;
  87. list_for_each_entry(pdn, &parent->child_list, list) {
  88. if (pdn->busno == bus->number &&
  89. pdn->devfn == devfn)
  90. return pdn;
  91. }
  92. return NULL;
  93. }
  94. struct pci_dn *pci_get_pdn(struct pci_dev *pdev)
  95. {
  96. struct device_node *dn;
  97. struct pci_dn *parent, *pdn;
  98. /* Search device directly */
  99. if (pdev->dev.archdata.pci_data)
  100. return pdev->dev.archdata.pci_data;
  101. /* Check device node */
  102. dn = pci_device_to_OF_node(pdev);
  103. pdn = dn ? PCI_DN(dn) : NULL;
  104. if (pdn)
  105. return pdn;
  106. /*
  107. * VFs don't have device nodes. We hook their
  108. * firmware data to PF's bridge.
  109. */
  110. parent = pci_bus_to_pdn(pdev->bus);
  111. if (!parent)
  112. return NULL;
  113. list_for_each_entry(pdn, &parent->child_list, list) {
  114. if (pdn->busno == pdev->bus->number &&
  115. pdn->devfn == pdev->devfn)
  116. return pdn;
  117. }
  118. return NULL;
  119. }
  120. #ifdef CONFIG_PCI_IOV
  121. static struct pci_dn *add_one_dev_pci_data(struct pci_dn *parent,
  122. struct pci_dev *pdev,
  123. int vf_index,
  124. int busno, int devfn)
  125. {
  126. struct pci_dn *pdn;
  127. /* Except PHB, we always have the parent */
  128. if (!parent)
  129. return NULL;
  130. pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
  131. if (!pdn) {
  132. dev_warn(&pdev->dev, "%s: Out of memory!\n", __func__);
  133. return NULL;
  134. }
  135. pdn->phb = parent->phb;
  136. pdn->parent = parent;
  137. pdn->busno = busno;
  138. pdn->devfn = devfn;
  139. #ifdef CONFIG_PPC_POWERNV
  140. pdn->vf_index = vf_index;
  141. pdn->pe_number = IODA_INVALID_PE;
  142. #endif
  143. INIT_LIST_HEAD(&pdn->child_list);
  144. INIT_LIST_HEAD(&pdn->list);
  145. list_add_tail(&pdn->list, &parent->child_list);
  146. /*
  147. * If we already have PCI device instance, lets
  148. * bind them.
  149. */
  150. if (pdev)
  151. pdev->dev.archdata.pci_data = pdn;
  152. return pdn;
  153. }
  154. #endif
  155. struct pci_dn *add_dev_pci_data(struct pci_dev *pdev)
  156. {
  157. #ifdef CONFIG_PCI_IOV
  158. struct pci_dn *parent, *pdn;
  159. int i;
  160. /* Only support IOV for now */
  161. if (!pdev->is_physfn)
  162. return pci_get_pdn(pdev);
  163. /* Check if VFs have been populated */
  164. pdn = pci_get_pdn(pdev);
  165. if (!pdn || (pdn->flags & PCI_DN_FLAG_IOV_VF))
  166. return NULL;
  167. pdn->flags |= PCI_DN_FLAG_IOV_VF;
  168. parent = pci_bus_to_pdn(pdev->bus);
  169. if (!parent)
  170. return NULL;
  171. for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
  172. struct eeh_dev *edev __maybe_unused;
  173. pdn = add_one_dev_pci_data(parent, NULL, i,
  174. pci_iov_virtfn_bus(pdev, i),
  175. pci_iov_virtfn_devfn(pdev, i));
  176. if (!pdn) {
  177. dev_warn(&pdev->dev, "%s: Cannot create firmware data for VF#%d\n",
  178. __func__, i);
  179. return NULL;
  180. }
  181. #ifdef CONFIG_EEH
  182. /* Create the EEH device for the VF */
  183. edev = eeh_dev_init(pdn);
  184. BUG_ON(!edev);
  185. edev->physfn = pdev;
  186. #endif /* CONFIG_EEH */
  187. }
  188. #endif /* CONFIG_PCI_IOV */
  189. return pci_get_pdn(pdev);
  190. }
  191. void remove_dev_pci_data(struct pci_dev *pdev)
  192. {
  193. #ifdef CONFIG_PCI_IOV
  194. struct pci_dn *parent;
  195. struct pci_dn *pdn, *tmp;
  196. int i;
  197. /*
  198. * VF and VF PE are created/released dynamically, so we need to
  199. * bind/unbind them. Otherwise the VF and VF PE would be mismatched
  200. * when re-enabling SR-IOV.
  201. */
  202. if (pdev->is_virtfn) {
  203. pdn = pci_get_pdn(pdev);
  204. #ifdef CONFIG_PPC_POWERNV
  205. pdn->pe_number = IODA_INVALID_PE;
  206. #endif
  207. return;
  208. }
  209. /* Only support IOV PF for now */
  210. if (!pdev->is_physfn)
  211. return;
  212. /* Check if VFs have been populated */
  213. pdn = pci_get_pdn(pdev);
  214. if (!pdn || !(pdn->flags & PCI_DN_FLAG_IOV_VF))
  215. return;
  216. pdn->flags &= ~PCI_DN_FLAG_IOV_VF;
  217. parent = pci_bus_to_pdn(pdev->bus);
  218. if (!parent)
  219. return;
  220. /*
  221. * We might introduce flag to pci_dn in future
  222. * so that we can release VF's firmware data in
  223. * a batch mode.
  224. */
  225. for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
  226. struct eeh_dev *edev __maybe_unused;
  227. list_for_each_entry_safe(pdn, tmp,
  228. &parent->child_list, list) {
  229. if (pdn->busno != pci_iov_virtfn_bus(pdev, i) ||
  230. pdn->devfn != pci_iov_virtfn_devfn(pdev, i))
  231. continue;
  232. #ifdef CONFIG_EEH
  233. /* Release EEH device for the VF */
  234. edev = pdn_to_eeh_dev(pdn);
  235. if (edev) {
  236. pdn->edev = NULL;
  237. kfree(edev);
  238. }
  239. #endif /* CONFIG_EEH */
  240. if (!list_empty(&pdn->list))
  241. list_del(&pdn->list);
  242. kfree(pdn);
  243. }
  244. }
  245. #endif /* CONFIG_PCI_IOV */
  246. }
  247. struct pci_dn *pci_add_device_node_info(struct pci_controller *hose,
  248. struct device_node *dn)
  249. {
  250. const __be32 *type = of_get_property(dn, "ibm,pci-config-space-type", NULL);
  251. const __be32 *regs;
  252. struct device_node *parent;
  253. struct pci_dn *pdn;
  254. #ifdef CONFIG_EEH
  255. struct eeh_dev *edev;
  256. #endif
  257. pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
  258. if (pdn == NULL)
  259. return NULL;
  260. dn->data = pdn;
  261. pdn->node = dn;
  262. pdn->phb = hose;
  263. #ifdef CONFIG_PPC_POWERNV
  264. pdn->pe_number = IODA_INVALID_PE;
  265. #endif
  266. regs = of_get_property(dn, "reg", NULL);
  267. if (regs) {
  268. u32 addr = of_read_number(regs, 1);
  269. /* First register entry is addr (00BBSS00) */
  270. pdn->busno = (addr >> 16) & 0xff;
  271. pdn->devfn = (addr >> 8) & 0xff;
  272. }
  273. /* vendor/device IDs and class code */
  274. regs = of_get_property(dn, "vendor-id", NULL);
  275. pdn->vendor_id = regs ? of_read_number(regs, 1) : 0;
  276. regs = of_get_property(dn, "device-id", NULL);
  277. pdn->device_id = regs ? of_read_number(regs, 1) : 0;
  278. regs = of_get_property(dn, "class-code", NULL);
  279. pdn->class_code = regs ? of_read_number(regs, 1) : 0;
  280. /* Extended config space */
  281. pdn->pci_ext_config_space = (type && of_read_number(type, 1) == 1);
  282. /* Create EEH device */
  283. #ifdef CONFIG_EEH
  284. edev = eeh_dev_init(pdn);
  285. if (!edev) {
  286. kfree(pdn);
  287. return NULL;
  288. }
  289. #endif
  290. /* Attach to parent node */
  291. INIT_LIST_HEAD(&pdn->child_list);
  292. INIT_LIST_HEAD(&pdn->list);
  293. parent = of_get_parent(dn);
  294. pdn->parent = parent ? PCI_DN(parent) : NULL;
  295. if (pdn->parent)
  296. list_add_tail(&pdn->list, &pdn->parent->child_list);
  297. return pdn;
  298. }
  299. EXPORT_SYMBOL_GPL(pci_add_device_node_info);
  300. void pci_remove_device_node_info(struct device_node *dn)
  301. {
  302. struct pci_dn *pdn = dn ? PCI_DN(dn) : NULL;
  303. #ifdef CONFIG_EEH
  304. struct eeh_dev *edev = pdn_to_eeh_dev(pdn);
  305. if (edev)
  306. edev->pdn = NULL;
  307. #endif
  308. if (!pdn)
  309. return;
  310. WARN_ON(!list_empty(&pdn->child_list));
  311. list_del(&pdn->list);
  312. if (pdn->parent)
  313. of_node_put(pdn->parent->node);
  314. dn->data = NULL;
  315. kfree(pdn);
  316. }
  317. EXPORT_SYMBOL_GPL(pci_remove_device_node_info);
  318. /*
  319. * Traverse a device tree stopping each PCI device in the tree.
  320. * This is done depth first. As each node is processed, a "pre"
  321. * function is called and the children are processed recursively.
  322. *
  323. * The "pre" func returns a value. If non-zero is returned from
  324. * the "pre" func, the traversal stops and this value is returned.
  325. * This return value is useful when using traverse as a method of
  326. * finding a device.
  327. *
  328. * NOTE: we do not run the func for devices that do not appear to
  329. * be PCI except for the start node which we assume (this is good
  330. * because the start node is often a phb which may be missing PCI
  331. * properties).
  332. * We use the class-code as an indicator. If we run into
  333. * one of these nodes we also assume its siblings are non-pci for
  334. * performance.
  335. */
  336. void *pci_traverse_device_nodes(struct device_node *start,
  337. void *(*fn)(struct device_node *, void *),
  338. void *data)
  339. {
  340. struct device_node *dn, *nextdn;
  341. void *ret;
  342. /* We started with a phb, iterate all childs */
  343. for (dn = start->child; dn; dn = nextdn) {
  344. const __be32 *classp;
  345. u32 class = 0;
  346. nextdn = NULL;
  347. classp = of_get_property(dn, "class-code", NULL);
  348. if (classp)
  349. class = of_read_number(classp, 1);
  350. if (fn) {
  351. ret = fn(dn, data);
  352. if (ret)
  353. return ret;
  354. }
  355. /* If we are a PCI bridge, go down */
  356. if (dn->child && ((class >> 8) == PCI_CLASS_BRIDGE_PCI ||
  357. (class >> 8) == PCI_CLASS_BRIDGE_CARDBUS))
  358. /* Depth first...do children */
  359. nextdn = dn->child;
  360. else if (dn->sibling)
  361. /* ok, try next sibling instead. */
  362. nextdn = dn->sibling;
  363. if (!nextdn) {
  364. /* Walk up to next valid sibling. */
  365. do {
  366. dn = dn->parent;
  367. if (dn == start)
  368. return NULL;
  369. } while (dn->sibling == NULL);
  370. nextdn = dn->sibling;
  371. }
  372. }
  373. return NULL;
  374. }
  375. EXPORT_SYMBOL_GPL(pci_traverse_device_nodes);
  376. static struct pci_dn *pci_dn_next_one(struct pci_dn *root,
  377. struct pci_dn *pdn)
  378. {
  379. struct list_head *next = pdn->child_list.next;
  380. if (next != &pdn->child_list)
  381. return list_entry(next, struct pci_dn, list);
  382. while (1) {
  383. if (pdn == root)
  384. return NULL;
  385. next = pdn->list.next;
  386. if (next != &pdn->parent->child_list)
  387. break;
  388. pdn = pdn->parent;
  389. }
  390. return list_entry(next, struct pci_dn, list);
  391. }
  392. void *traverse_pci_dn(struct pci_dn *root,
  393. void *(*fn)(struct pci_dn *, void *),
  394. void *data)
  395. {
  396. struct pci_dn *pdn = root;
  397. void *ret;
  398. /* Only scan the child nodes */
  399. for (pdn = pci_dn_next_one(root, pdn); pdn;
  400. pdn = pci_dn_next_one(root, pdn)) {
  401. ret = fn(pdn, data);
  402. if (ret)
  403. return ret;
  404. }
  405. return NULL;
  406. }
  407. static void *add_pdn(struct device_node *dn, void *data)
  408. {
  409. struct pci_controller *hose = data;
  410. struct pci_dn *pdn;
  411. pdn = pci_add_device_node_info(hose, dn);
  412. if (!pdn)
  413. return ERR_PTR(-ENOMEM);
  414. return NULL;
  415. }
  416. /**
  417. * pci_devs_phb_init_dynamic - setup pci devices under this PHB
  418. * phb: pci-to-host bridge (top-level bridge connecting to cpu)
  419. *
  420. * This routine is called both during boot, (before the memory
  421. * subsystem is set up, before kmalloc is valid) and during the
  422. * dynamic lpar operation of adding a PHB to a running system.
  423. */
  424. void pci_devs_phb_init_dynamic(struct pci_controller *phb)
  425. {
  426. struct device_node *dn = phb->dn;
  427. struct pci_dn *pdn;
  428. /* PHB nodes themselves must not match */
  429. pdn = pci_add_device_node_info(phb, dn);
  430. if (pdn) {
  431. pdn->devfn = pdn->busno = -1;
  432. pdn->vendor_id = pdn->device_id = pdn->class_code = 0;
  433. pdn->phb = phb;
  434. phb->pci_data = pdn;
  435. }
  436. /* Update dn->phb ptrs for new phb and children devices */
  437. pci_traverse_device_nodes(dn, add_pdn, phb);
  438. }
  439. /**
  440. * pci_devs_phb_init - Initialize phbs and pci devs under them.
  441. *
  442. * This routine walks over all phb's (pci-host bridges) on the
  443. * system, and sets up assorted pci-related structures
  444. * (including pci info in the device node structs) for each
  445. * pci device found underneath. This routine runs once,
  446. * early in the boot sequence.
  447. */
  448. static int __init pci_devs_phb_init(void)
  449. {
  450. struct pci_controller *phb, *tmp;
  451. /* This must be done first so the device nodes have valid pci info! */
  452. list_for_each_entry_safe(phb, tmp, &hose_list, list_node)
  453. pci_devs_phb_init_dynamic(phb);
  454. return 0;
  455. }
  456. core_initcall(pci_devs_phb_init);
  457. static void pci_dev_pdn_setup(struct pci_dev *pdev)
  458. {
  459. struct pci_dn *pdn;
  460. if (pdev->dev.archdata.pci_data)
  461. return;
  462. /* Setup the fast path */
  463. pdn = pci_get_pdn(pdev);
  464. pdev->dev.archdata.pci_data = pdn;
  465. }
  466. DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pci_dev_pdn_setup);