pci_dn.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. /*
  34. * The function is used to find the firmware data of one
  35. * specific PCI device, which is attached to the indicated
  36. * PCI bus. For VFs, their firmware data is linked to that
  37. * one of PF's bridge. For other devices, their firmware
  38. * data is linked to that of their bridge.
  39. */
  40. static struct pci_dn *pci_bus_to_pdn(struct pci_bus *bus)
  41. {
  42. struct pci_bus *pbus;
  43. struct device_node *dn;
  44. struct pci_dn *pdn;
  45. /*
  46. * We probably have virtual bus which doesn't
  47. * have associated bridge.
  48. */
  49. pbus = bus;
  50. while (pbus) {
  51. if (pci_is_root_bus(pbus) || pbus->self)
  52. break;
  53. pbus = pbus->parent;
  54. }
  55. /*
  56. * Except virtual bus, all PCI buses should
  57. * have device nodes.
  58. */
  59. dn = pci_bus_to_OF_node(pbus);
  60. pdn = dn ? PCI_DN(dn) : NULL;
  61. return pdn;
  62. }
  63. struct pci_dn *pci_get_pdn_by_devfn(struct pci_bus *bus,
  64. int devfn)
  65. {
  66. struct device_node *dn = NULL;
  67. struct pci_dn *parent, *pdn;
  68. struct pci_dev *pdev = NULL;
  69. /* Fast path: fetch from PCI device */
  70. list_for_each_entry(pdev, &bus->devices, bus_list) {
  71. if (pdev->devfn == devfn) {
  72. if (pdev->dev.archdata.pci_data)
  73. return pdev->dev.archdata.pci_data;
  74. dn = pci_device_to_OF_node(pdev);
  75. break;
  76. }
  77. }
  78. /* Fast path: fetch from device node */
  79. pdn = dn ? PCI_DN(dn) : NULL;
  80. if (pdn)
  81. return pdn;
  82. /* Slow path: fetch from firmware data hierarchy */
  83. parent = pci_bus_to_pdn(bus);
  84. if (!parent)
  85. return NULL;
  86. list_for_each_entry(pdn, &parent->child_list, list) {
  87. if (pdn->busno == bus->number &&
  88. pdn->devfn == devfn)
  89. return pdn;
  90. }
  91. return NULL;
  92. }
  93. struct pci_dn *pci_get_pdn(struct pci_dev *pdev)
  94. {
  95. struct device_node *dn;
  96. struct pci_dn *parent, *pdn;
  97. /* Search device directly */
  98. if (pdev->dev.archdata.pci_data)
  99. return pdev->dev.archdata.pci_data;
  100. /* Check device node */
  101. dn = pci_device_to_OF_node(pdev);
  102. pdn = dn ? PCI_DN(dn) : NULL;
  103. if (pdn)
  104. return pdn;
  105. /*
  106. * VFs don't have device nodes. We hook their
  107. * firmware data to PF's bridge.
  108. */
  109. parent = pci_bus_to_pdn(pdev->bus);
  110. if (!parent)
  111. return NULL;
  112. list_for_each_entry(pdn, &parent->child_list, list) {
  113. if (pdn->busno == pdev->bus->number &&
  114. pdn->devfn == pdev->devfn)
  115. return pdn;
  116. }
  117. return NULL;
  118. }
  119. #ifdef CONFIG_PCI_IOV
  120. static struct pci_dn *add_one_dev_pci_data(struct pci_dn *parent,
  121. struct pci_dev *pdev,
  122. int busno, int devfn)
  123. {
  124. struct pci_dn *pdn;
  125. /* Except PHB, we always have the parent */
  126. if (!parent)
  127. return NULL;
  128. pdn = kzalloc(sizeof(*pdn), GFP_KERNEL);
  129. if (!pdn) {
  130. dev_warn(&pdev->dev, "%s: Out of memory!\n", __func__);
  131. return NULL;
  132. }
  133. pdn->phb = parent->phb;
  134. pdn->parent = parent;
  135. pdn->busno = busno;
  136. pdn->devfn = devfn;
  137. #ifdef CONFIG_PPC_POWERNV
  138. pdn->pe_number = IODA_INVALID_PE;
  139. #endif
  140. INIT_LIST_HEAD(&pdn->child_list);
  141. INIT_LIST_HEAD(&pdn->list);
  142. list_add_tail(&pdn->list, &parent->child_list);
  143. /*
  144. * If we already have PCI device instance, lets
  145. * bind them.
  146. */
  147. if (pdev)
  148. pdev->dev.archdata.pci_data = pdn;
  149. return pdn;
  150. }
  151. #endif
  152. struct pci_dn *add_dev_pci_data(struct pci_dev *pdev)
  153. {
  154. #ifdef CONFIG_PCI_IOV
  155. struct pci_dn *parent, *pdn;
  156. int i;
  157. /* Only support IOV for now */
  158. if (!pdev->is_physfn)
  159. return pci_get_pdn(pdev);
  160. /* Check if VFs have been populated */
  161. pdn = pci_get_pdn(pdev);
  162. if (!pdn || (pdn->flags & PCI_DN_FLAG_IOV_VF))
  163. return NULL;
  164. pdn->flags |= PCI_DN_FLAG_IOV_VF;
  165. parent = pci_bus_to_pdn(pdev->bus);
  166. if (!parent)
  167. return NULL;
  168. for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
  169. pdn = add_one_dev_pci_data(parent, NULL,
  170. pci_iov_virtfn_bus(pdev, i),
  171. pci_iov_virtfn_devfn(pdev, i));
  172. if (!pdn) {
  173. dev_warn(&pdev->dev, "%s: Cannot create firmware data for VF#%d\n",
  174. __func__, i);
  175. return NULL;
  176. }
  177. }
  178. #endif /* CONFIG_PCI_IOV */
  179. return pci_get_pdn(pdev);
  180. }
  181. void remove_dev_pci_data(struct pci_dev *pdev)
  182. {
  183. #ifdef CONFIG_PCI_IOV
  184. struct pci_dn *parent;
  185. struct pci_dn *pdn, *tmp;
  186. int i;
  187. /*
  188. * VF and VF PE are created/released dynamically, so we need to
  189. * bind/unbind them. Otherwise the VF and VF PE would be mismatched
  190. * when re-enabling SR-IOV.
  191. */
  192. if (pdev->is_virtfn) {
  193. pdn = pci_get_pdn(pdev);
  194. #ifdef CONFIG_PPC_POWERNV
  195. pdn->pe_number = IODA_INVALID_PE;
  196. #endif
  197. return;
  198. }
  199. /* Only support IOV PF for now */
  200. if (!pdev->is_physfn)
  201. return;
  202. /* Check if VFs have been populated */
  203. pdn = pci_get_pdn(pdev);
  204. if (!pdn || !(pdn->flags & PCI_DN_FLAG_IOV_VF))
  205. return;
  206. pdn->flags &= ~PCI_DN_FLAG_IOV_VF;
  207. parent = pci_bus_to_pdn(pdev->bus);
  208. if (!parent)
  209. return;
  210. /*
  211. * We might introduce flag to pci_dn in future
  212. * so that we can release VF's firmware data in
  213. * a batch mode.
  214. */
  215. for (i = 0; i < pci_sriov_get_totalvfs(pdev); i++) {
  216. list_for_each_entry_safe(pdn, tmp,
  217. &parent->child_list, list) {
  218. if (pdn->busno != pci_iov_virtfn_bus(pdev, i) ||
  219. pdn->devfn != pci_iov_virtfn_devfn(pdev, i))
  220. continue;
  221. if (!list_empty(&pdn->list))
  222. list_del(&pdn->list);
  223. kfree(pdn);
  224. }
  225. }
  226. #endif /* CONFIG_PCI_IOV */
  227. }
  228. /*
  229. * Traverse_func that inits the PCI fields of the device node.
  230. * NOTE: this *must* be done before read/write config to the device.
  231. */
  232. void *update_dn_pci_info(struct device_node *dn, void *data)
  233. {
  234. struct pci_controller *phb = data;
  235. const __be32 *type = of_get_property(dn, "ibm,pci-config-space-type", NULL);
  236. const __be32 *regs;
  237. struct device_node *parent;
  238. struct pci_dn *pdn;
  239. pdn = zalloc_maybe_bootmem(sizeof(*pdn), GFP_KERNEL);
  240. if (pdn == NULL)
  241. return NULL;
  242. dn->data = pdn;
  243. pdn->node = dn;
  244. pdn->phb = phb;
  245. #ifdef CONFIG_PPC_POWERNV
  246. pdn->pe_number = IODA_INVALID_PE;
  247. #endif
  248. regs = of_get_property(dn, "reg", NULL);
  249. if (regs) {
  250. u32 addr = of_read_number(regs, 1);
  251. /* First register entry is addr (00BBSS00) */
  252. pdn->busno = (addr >> 16) & 0xff;
  253. pdn->devfn = (addr >> 8) & 0xff;
  254. }
  255. /* vendor/device IDs and class code */
  256. regs = of_get_property(dn, "vendor-id", NULL);
  257. pdn->vendor_id = regs ? of_read_number(regs, 1) : 0;
  258. regs = of_get_property(dn, "device-id", NULL);
  259. pdn->device_id = regs ? of_read_number(regs, 1) : 0;
  260. regs = of_get_property(dn, "class-code", NULL);
  261. pdn->class_code = regs ? of_read_number(regs, 1) : 0;
  262. /* Extended config space */
  263. pdn->pci_ext_config_space = (type && of_read_number(type, 1) == 1);
  264. /* Attach to parent node */
  265. INIT_LIST_HEAD(&pdn->child_list);
  266. INIT_LIST_HEAD(&pdn->list);
  267. parent = of_get_parent(dn);
  268. pdn->parent = parent ? PCI_DN(parent) : NULL;
  269. if (pdn->parent)
  270. list_add_tail(&pdn->list, &pdn->parent->child_list);
  271. return NULL;
  272. }
  273. /*
  274. * Traverse a device tree stopping each PCI device in the tree.
  275. * This is done depth first. As each node is processed, a "pre"
  276. * function is called and the children are processed recursively.
  277. *
  278. * The "pre" func returns a value. If non-zero is returned from
  279. * the "pre" func, the traversal stops and this value is returned.
  280. * This return value is useful when using traverse as a method of
  281. * finding a device.
  282. *
  283. * NOTE: we do not run the func for devices that do not appear to
  284. * be PCI except for the start node which we assume (this is good
  285. * because the start node is often a phb which may be missing PCI
  286. * properties).
  287. * We use the class-code as an indicator. If we run into
  288. * one of these nodes we also assume its siblings are non-pci for
  289. * performance.
  290. */
  291. void *traverse_pci_devices(struct device_node *start, traverse_func pre,
  292. void *data)
  293. {
  294. struct device_node *dn, *nextdn;
  295. void *ret;
  296. /* We started with a phb, iterate all childs */
  297. for (dn = start->child; dn; dn = nextdn) {
  298. const __be32 *classp;
  299. u32 class = 0;
  300. nextdn = NULL;
  301. classp = of_get_property(dn, "class-code", NULL);
  302. if (classp)
  303. class = of_read_number(classp, 1);
  304. if (pre && ((ret = pre(dn, data)) != NULL))
  305. return ret;
  306. /* If we are a PCI bridge, go down */
  307. if (dn->child && ((class >> 8) == PCI_CLASS_BRIDGE_PCI ||
  308. (class >> 8) == PCI_CLASS_BRIDGE_CARDBUS))
  309. /* Depth first...do children */
  310. nextdn = dn->child;
  311. else if (dn->sibling)
  312. /* ok, try next sibling instead. */
  313. nextdn = dn->sibling;
  314. if (!nextdn) {
  315. /* Walk up to next valid sibling. */
  316. do {
  317. dn = dn->parent;
  318. if (dn == start)
  319. return NULL;
  320. } while (dn->sibling == NULL);
  321. nextdn = dn->sibling;
  322. }
  323. }
  324. return NULL;
  325. }
  326. static struct pci_dn *pci_dn_next_one(struct pci_dn *root,
  327. struct pci_dn *pdn)
  328. {
  329. struct list_head *next = pdn->child_list.next;
  330. if (next != &pdn->child_list)
  331. return list_entry(next, struct pci_dn, list);
  332. while (1) {
  333. if (pdn == root)
  334. return NULL;
  335. next = pdn->list.next;
  336. if (next != &pdn->parent->child_list)
  337. break;
  338. pdn = pdn->parent;
  339. }
  340. return list_entry(next, struct pci_dn, list);
  341. }
  342. void *traverse_pci_dn(struct pci_dn *root,
  343. void *(*fn)(struct pci_dn *, void *),
  344. void *data)
  345. {
  346. struct pci_dn *pdn = root;
  347. void *ret;
  348. /* Only scan the child nodes */
  349. for (pdn = pci_dn_next_one(root, pdn); pdn;
  350. pdn = pci_dn_next_one(root, pdn)) {
  351. ret = fn(pdn, data);
  352. if (ret)
  353. return ret;
  354. }
  355. return NULL;
  356. }
  357. /**
  358. * pci_devs_phb_init_dynamic - setup pci devices under this PHB
  359. * phb: pci-to-host bridge (top-level bridge connecting to cpu)
  360. *
  361. * This routine is called both during boot, (before the memory
  362. * subsystem is set up, before kmalloc is valid) and during the
  363. * dynamic lpar operation of adding a PHB to a running system.
  364. */
  365. void pci_devs_phb_init_dynamic(struct pci_controller *phb)
  366. {
  367. struct device_node *dn = phb->dn;
  368. struct pci_dn *pdn;
  369. /* PHB nodes themselves must not match */
  370. update_dn_pci_info(dn, phb);
  371. pdn = dn->data;
  372. if (pdn) {
  373. pdn->devfn = pdn->busno = -1;
  374. pdn->vendor_id = pdn->device_id = pdn->class_code = 0;
  375. pdn->phb = phb;
  376. phb->pci_data = pdn;
  377. }
  378. /* Update dn->phb ptrs for new phb and children devices */
  379. traverse_pci_devices(dn, update_dn_pci_info, phb);
  380. }
  381. /**
  382. * pci_devs_phb_init - Initialize phbs and pci devs under them.
  383. *
  384. * This routine walks over all phb's (pci-host bridges) on the
  385. * system, and sets up assorted pci-related structures
  386. * (including pci info in the device node structs) for each
  387. * pci device found underneath. This routine runs once,
  388. * early in the boot sequence.
  389. */
  390. void __init pci_devs_phb_init(void)
  391. {
  392. struct pci_controller *phb, *tmp;
  393. /* This must be done first so the device nodes have valid pci info! */
  394. list_for_each_entry_safe(phb, tmp, &hose_list, list_node)
  395. pci_devs_phb_init_dynamic(phb);
  396. }
  397. static void pci_dev_pdn_setup(struct pci_dev *pdev)
  398. {
  399. struct pci_dn *pdn;
  400. if (pdev->dev.archdata.pci_data)
  401. return;
  402. /* Setup the fast path */
  403. pdn = pci_get_pdn(pdev);
  404. pdev->dev.archdata.pci_data = pdn;
  405. }
  406. DECLARE_PCI_FIXUP_EARLY(PCI_ANY_ID, PCI_ANY_ID, pci_dev_pdn_setup);