pci.c 5.4 KB

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