pci.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 <anton@au.ibm.com>, 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. if (!acpi_disabled) {
  71. struct pci_config_window *cfg = bridge->bus->sysdata;
  72. struct acpi_device *adev = to_acpi_device(cfg->parent);
  73. struct device *bus_dev = &bridge->bus->dev;
  74. ACPI_COMPANION_SET(&bridge->dev, adev);
  75. set_dev_node(bus_dev, acpi_get_node(acpi_device_handle(adev)));
  76. }
  77. return 0;
  78. }
  79. static int pci_acpi_root_prepare_resources(struct acpi_pci_root_info *ci)
  80. {
  81. struct resource_entry *entry, *tmp;
  82. int status;
  83. status = acpi_pci_probe_root_resources(ci);
  84. resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
  85. if (!(entry->res->flags & IORESOURCE_WINDOW))
  86. resource_list_destroy_entry(entry);
  87. }
  88. return status;
  89. }
  90. /*
  91. * Lookup the bus range for the domain in MCFG, and set up config space
  92. * mapping.
  93. */
  94. static struct pci_config_window *
  95. pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
  96. {
  97. struct device *dev = &root->device->dev;
  98. struct resource *bus_res = &root->secondary;
  99. u16 seg = root->segment;
  100. const struct pci_ecam_ops *ecam_ops;
  101. struct resource cfgres;
  102. struct acpi_device *adev;
  103. struct pci_config_window *cfg;
  104. int ret;
  105. ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
  106. if (ret) {
  107. dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res);
  108. return NULL;
  109. }
  110. adev = acpi_resource_consumer(&cfgres);
  111. if (adev)
  112. dev_info(dev, "ECAM area %pR reserved by %s\n", &cfgres,
  113. dev_name(&adev->dev));
  114. else
  115. dev_warn(dev, FW_BUG "ECAM area %pR not reserved in ACPI namespace\n",
  116. &cfgres);
  117. cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
  118. if (IS_ERR(cfg)) {
  119. dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res,
  120. PTR_ERR(cfg));
  121. return NULL;
  122. }
  123. return cfg;
  124. }
  125. /* release_info: free resources allocated by init_info */
  126. static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci)
  127. {
  128. struct acpi_pci_generic_root_info *ri;
  129. ri = container_of(ci, struct acpi_pci_generic_root_info, common);
  130. pci_ecam_free(ri->cfg);
  131. kfree(ci->ops);
  132. kfree(ri);
  133. }
  134. /* Interface called from ACPI code to setup PCI host controller */
  135. struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
  136. {
  137. struct acpi_pci_generic_root_info *ri;
  138. struct pci_bus *bus, *child;
  139. struct acpi_pci_root_ops *root_ops;
  140. struct pci_host_bridge *host;
  141. ri = kzalloc(sizeof(*ri), GFP_KERNEL);
  142. if (!ri)
  143. return NULL;
  144. root_ops = kzalloc(sizeof(*root_ops), GFP_KERNEL);
  145. if (!root_ops) {
  146. kfree(ri);
  147. return NULL;
  148. }
  149. ri->cfg = pci_acpi_setup_ecam_mapping(root);
  150. if (!ri->cfg) {
  151. kfree(ri);
  152. kfree(root_ops);
  153. return NULL;
  154. }
  155. root_ops->release_info = pci_acpi_generic_release_info;
  156. root_ops->prepare_resources = pci_acpi_root_prepare_resources;
  157. root_ops->pci_ops = (struct pci_ops *)&ri->cfg->ops->pci_ops;
  158. bus = acpi_pci_root_create(root, root_ops, &ri->common, ri->cfg);
  159. if (!bus)
  160. return NULL;
  161. /* If we must preserve the resource configuration, claim now */
  162. host = pci_find_host_bridge(bus);
  163. if (host->preserve_config)
  164. pci_bus_claim_resources(bus);
  165. /*
  166. * Assign whatever was left unassigned. If we didn't claim above,
  167. * this will reassign everything.
  168. */
  169. pci_assign_unassigned_root_bus_resources(bus);
  170. list_for_each_entry(child, &bus->children, node)
  171. pcie_bus_configure_settings(child);
  172. return bus;
  173. }
  174. void pcibios_add_bus(struct pci_bus *bus)
  175. {
  176. acpi_pci_add_bus(bus);
  177. }
  178. void pcibios_remove_bus(struct pci_bus *bus)
  179. {
  180. acpi_pci_remove_bus(bus);
  181. }
  182. #endif