pci.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /*
  24. * Called after each bus is probed, but before its children are examined
  25. */
  26. void pcibios_fixup_bus(struct pci_bus *bus)
  27. {
  28. /* nothing to do, expected to be removed in the future */
  29. }
  30. /*
  31. * We don't have to worry about legacy ISA devices, so nothing to do here
  32. */
  33. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  34. resource_size_t size, resource_size_t align)
  35. {
  36. return res->start;
  37. }
  38. /*
  39. * Try to assign the IRQ number when probing a new device
  40. */
  41. int pcibios_alloc_irq(struct pci_dev *dev)
  42. {
  43. if (acpi_disabled)
  44. dev->irq = of_irq_parse_and_map_pci(dev, 0, 0);
  45. #ifdef CONFIG_ACPI
  46. else
  47. return acpi_pci_irq_enable(dev);
  48. #endif
  49. return 0;
  50. }
  51. /*
  52. * raw_pci_read/write - Platform-specific PCI config space access.
  53. */
  54. int raw_pci_read(unsigned int domain, unsigned int bus,
  55. unsigned int devfn, int reg, int len, u32 *val)
  56. {
  57. struct pci_bus *b = pci_find_bus(domain, bus);
  58. if (!b)
  59. return PCIBIOS_DEVICE_NOT_FOUND;
  60. return b->ops->read(b, devfn, reg, len, val);
  61. }
  62. int raw_pci_write(unsigned int domain, unsigned int bus,
  63. unsigned int devfn, int reg, int len, u32 val)
  64. {
  65. struct pci_bus *b = pci_find_bus(domain, bus);
  66. if (!b)
  67. return PCIBIOS_DEVICE_NOT_FOUND;
  68. return b->ops->write(b, devfn, reg, len, val);
  69. }
  70. #ifdef CONFIG_NUMA
  71. int pcibus_to_node(struct pci_bus *bus)
  72. {
  73. return dev_to_node(&bus->dev);
  74. }
  75. EXPORT_SYMBOL(pcibus_to_node);
  76. #endif
  77. #ifdef CONFIG_ACPI
  78. struct acpi_pci_generic_root_info {
  79. struct acpi_pci_root_info common;
  80. struct pci_config_window *cfg; /* config space mapping */
  81. };
  82. int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
  83. {
  84. struct pci_config_window *cfg = bus->sysdata;
  85. struct acpi_device *adev = to_acpi_device(cfg->parent);
  86. struct acpi_pci_root *root = acpi_driver_data(adev);
  87. return root->segment;
  88. }
  89. int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  90. {
  91. if (!acpi_disabled) {
  92. struct pci_config_window *cfg = bridge->bus->sysdata;
  93. struct acpi_device *adev = to_acpi_device(cfg->parent);
  94. ACPI_COMPANION_SET(&bridge->dev, adev);
  95. }
  96. return 0;
  97. }
  98. /*
  99. * Lookup the bus range for the domain in MCFG, and set up config space
  100. * mapping.
  101. */
  102. static struct pci_config_window *
  103. pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
  104. {
  105. struct device *dev = &root->device->dev;
  106. struct resource *bus_res = &root->secondary;
  107. u16 seg = root->segment;
  108. struct pci_config_window *cfg;
  109. struct resource cfgres;
  110. unsigned int bsz;
  111. /* Use address from _CBA if present, otherwise lookup MCFG */
  112. if (!root->mcfg_addr)
  113. root->mcfg_addr = pci_mcfg_lookup(seg, bus_res);
  114. if (!root->mcfg_addr) {
  115. dev_err(dev, "%04x:%pR ECAM region not found\n", seg, bus_res);
  116. return NULL;
  117. }
  118. bsz = 1 << pci_generic_ecam_ops.bus_shift;
  119. cfgres.start = root->mcfg_addr + bus_res->start * bsz;
  120. cfgres.end = cfgres.start + resource_size(bus_res) * bsz - 1;
  121. cfgres.flags = IORESOURCE_MEM;
  122. cfg = pci_ecam_create(dev, &cfgres, bus_res, &pci_generic_ecam_ops);
  123. if (IS_ERR(cfg)) {
  124. dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res,
  125. PTR_ERR(cfg));
  126. return NULL;
  127. }
  128. return cfg;
  129. }
  130. /* release_info: free resources allocated by init_info */
  131. static void pci_acpi_generic_release_info(struct acpi_pci_root_info *ci)
  132. {
  133. struct acpi_pci_generic_root_info *ri;
  134. ri = container_of(ci, struct acpi_pci_generic_root_info, common);
  135. pci_ecam_free(ri->cfg);
  136. kfree(ci->ops);
  137. kfree(ri);
  138. }
  139. /* Interface called from ACPI code to setup PCI host controller */
  140. struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
  141. {
  142. int node = acpi_get_node(root->device->handle);
  143. struct acpi_pci_generic_root_info *ri;
  144. struct pci_bus *bus, *child;
  145. struct acpi_pci_root_ops *root_ops;
  146. ri = kzalloc_node(sizeof(*ri), GFP_KERNEL, node);
  147. if (!ri)
  148. return NULL;
  149. root_ops = kzalloc_node(sizeof(*root_ops), GFP_KERNEL, node);
  150. if (!root_ops) {
  151. kfree(ri);
  152. return NULL;
  153. }
  154. ri->cfg = pci_acpi_setup_ecam_mapping(root);
  155. if (!ri->cfg) {
  156. kfree(ri);
  157. kfree(root_ops);
  158. return NULL;
  159. }
  160. root_ops->release_info = pci_acpi_generic_release_info;
  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