pci.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Code borrowed from powerpc/kernel/pci-common.c
  3. *
  4. * Copyright (C) 2003 Anton Blanchard <anton@au.ibm.com>, IBM
  5. * Copyright (C) 2014 ARM Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/of_pci.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/pci.h>
  20. #include <linux/pci-acpi.h>
  21. #include <linux/pci-ecam.h>
  22. #include <linux/slab.h>
  23. #ifdef CONFIG_ACPI
  24. /*
  25. * Try to assign the IRQ number when probing a new device
  26. */
  27. int pcibios_alloc_irq(struct pci_dev *dev)
  28. {
  29. if (!acpi_disabled)
  30. acpi_pci_irq_enable(dev);
  31. return 0;
  32. }
  33. #endif
  34. /*
  35. * raw_pci_read/write - Platform-specific PCI config space access.
  36. */
  37. int raw_pci_read(unsigned int domain, unsigned int bus,
  38. unsigned int devfn, int reg, int len, u32 *val)
  39. {
  40. struct pci_bus *b = pci_find_bus(domain, bus);
  41. if (!b)
  42. return PCIBIOS_DEVICE_NOT_FOUND;
  43. return b->ops->read(b, devfn, reg, len, val);
  44. }
  45. int raw_pci_write(unsigned int domain, unsigned int bus,
  46. unsigned int devfn, int reg, int len, u32 val)
  47. {
  48. struct pci_bus *b = pci_find_bus(domain, bus);
  49. if (!b)
  50. return PCIBIOS_DEVICE_NOT_FOUND;
  51. return b->ops->write(b, devfn, reg, len, val);
  52. }
  53. #ifdef CONFIG_NUMA
  54. int pcibus_to_node(struct pci_bus *bus)
  55. {
  56. return dev_to_node(&bus->dev);
  57. }
  58. EXPORT_SYMBOL(pcibus_to_node);
  59. #endif
  60. #ifdef CONFIG_ACPI
  61. struct acpi_pci_generic_root_info {
  62. struct acpi_pci_root_info common;
  63. struct pci_config_window *cfg; /* config space mapping */
  64. };
  65. int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
  66. {
  67. struct pci_config_window *cfg = bus->sysdata;
  68. struct acpi_device *adev = to_acpi_device(cfg->parent);
  69. struct acpi_pci_root *root = acpi_driver_data(adev);
  70. return root->segment;
  71. }
  72. int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  73. {
  74. if (!acpi_disabled) {
  75. struct pci_config_window *cfg = bridge->bus->sysdata;
  76. struct acpi_device *adev = to_acpi_device(cfg->parent);
  77. struct device *bus_dev = &bridge->bus->dev;
  78. ACPI_COMPANION_SET(&bridge->dev, adev);
  79. set_dev_node(bus_dev, acpi_get_node(acpi_device_handle(adev)));
  80. }
  81. return 0;
  82. }
  83. static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
  84. {
  85. struct resource_entry *entry, *tmp;
  86. int status;
  87. status = acpi_pci_probe_root_resources(ci);
  88. resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
  89. if (!(entry->res->flags & IORESOURCE_WINDOW))
  90. resource_list_destroy_entry(entry);
  91. }
  92. return status;
  93. }
  94. /*
  95. * Lookup the bus range for the domain in MCFG, and set up config space
  96. * mapping.
  97. */
  98. static struct pci_config_window *
  99. pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
  100. {
  101. struct device *dev = &root->device->dev;
  102. struct resource *bus_res = &root->secondary;
  103. u16 seg = root->segment;
  104. struct pci_ecam_ops *ecam_ops;
  105. struct resource cfgres;
  106. struct acpi_device *adev;
  107. struct pci_config_window *cfg;
  108. int ret;
  109. ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
  110. if (ret) {
  111. dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res);
  112. return NULL;
  113. }
  114. adev = acpi_resource_consumer(&cfgres);
  115. if (adev)
  116. dev_info(dev, "ECAM area %pR reserved by %s\n", &cfgres,
  117. dev_name(&adev->dev));
  118. else
  119. dev_warn(dev, FW_BUG "ECAM area %pR not reserved in ACPI namespace\n",
  120. &cfgres);
  121. cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
  122. if (IS_ERR(cfg)) {
  123. dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res,
  124. PTR_ERR(cfg));
  125. return NULL;
  126. }
  127. return cfg;
  128. }
  129. /* release_info: free resources allocated by init_info */
  130. static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci)
  131. {
  132. struct acpi_pci_generic_root_info *ri;
  133. ri = container_of(ci, struct acpi_pci_generic_root_info, common);
  134. pci_ecam_free(ri->cfg);
  135. kfree(ci->ops);
  136. kfree(ri);
  137. }
  138. /* Interface called from ACPI code to setup PCI host controller */
  139. struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
  140. {
  141. int node = acpi_get_node(root->device->handle);
  142. struct acpi_pci_generic_root_info *ri;
  143. struct pci_bus *bus, *child;
  144. struct acpi_pci_root_ops *root_ops;
  145. ri = kzalloc_node(sizeof(*ri), GFP_KERNEL, node);
  146. if (!ri)
  147. return NULL;
  148. root_ops = kzalloc_node(sizeof(*root_ops), GFP_KERNEL, node);
  149. if (!root_ops) {
  150. kfree(ri);
  151. return NULL;
  152. }
  153. ri->cfg = pci_acpi_setup_ecam_mapping(root);
  154. if (!ri->cfg) {
  155. kfree(ri);
  156. kfree(root_ops);
  157. return NULL;
  158. }
  159. root_ops->release_info = pci_acpi_generic_release_info;
  160. root_ops->prepare_resources = pci_acpi_root_prepare_resources;
  161. root_ops->pci_ops = &ri->cfg->ops->pci_ops;
  162. bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg);
  163. if (!bus)
  164. return NULL;
  165. pci_bus_size_bridges(bus);
  166. pci_bus_assign_resources(bus);
  167. list_for_each_entry(child, &bus->children, node)
  168. pcie_bus_configure_settings(child);
  169. return bus;
  170. }
  171. void pcibios_add_bus(struct pci_bus *bus)
  172. {
  173. acpi_pci_add_bus(bus);
  174. }
  175. void pcibios_remove_bus(struct pci_bus *bus)
  176. {
  177. acpi_pci_remove_bus(bus);
  178. }
  179. #endif