acpi.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
  4. */
  5. #include <linux/pci.h>
  6. #include <linux/acpi.h>
  7. #include <linux/init.h>
  8. #include <linux/irq.h>
  9. #include <linux/slab.h>
  10. #include <linux/pci-acpi.h>
  11. #include <linux/pci-ecam.h>
  12. #include <asm/pci.h>
  13. #include <asm/numa.h>
  14. #include <asm/loongson.h>
  15. struct pci_root_info {
  16. struct acpi_pci_root_info common;
  17. struct pci_config_window *cfg;
  18. };
  19. void pcibios_add_bus(struct pci_bus *bus)
  20. {
  21. acpi_pci_add_bus(bus);
  22. }
  23. int pcibios_root_bridge_prepare(struct pci_host_bridge *bridge)
  24. {
  25. struct acpi_device *adev = NULL;
  26. struct device *bus_dev = &bridge->bus->dev;
  27. struct pci_config_window *cfg = bridge->bus->sysdata;
  28. if (!acpi_disabled)
  29. adev = to_acpi_device(cfg->parent);
  30. ACPI_COMPANION_SET(&bridge->dev, adev);
  31. set_dev_node(bus_dev, pa_to_nid(cfg->res.start));
  32. return 0;
  33. }
  34. int acpi_pci_bus_find_domain_nr(struct pci_bus *bus)
  35. {
  36. struct pci_config_window *cfg = bus->sysdata;
  37. struct acpi_device *adev = to_acpi_device(cfg->parent);
  38. struct acpi_pci_root *root = acpi_driver_data(adev);
  39. return root->segment;
  40. }
  41. static void acpi_release_root_info(struct acpi_pci_root_info *ci)
  42. {
  43. struct pci_root_info *info;
  44. info = container_of(ci, struct pci_root_info, common);
  45. pci_ecam_free(info->cfg);
  46. kfree(ci->ops);
  47. kfree(info);
  48. }
  49. static int acpi_prepare_root_resources(struct acpi_pci_root_info *ci)
  50. {
  51. int status;
  52. struct resource_entry *entry, *tmp;
  53. struct acpi_device *device = ci->bridge;
  54. status = acpi_pci_probe_root_resources(ci);
  55. if (status > 0) {
  56. resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
  57. if (entry->res->flags & IORESOURCE_MEM) {
  58. entry->offset = ci->root->mcfg_addr & GENMASK_ULL(63, 40);
  59. entry->res->start |= entry->offset;
  60. entry->res->end |= entry->offset;
  61. }
  62. }
  63. return status;
  64. }
  65. resource_list_for_each_entry_safe(entry, tmp, &ci->resources) {
  66. dev_dbg(&device->dev,
  67. "host bridge window %pR (ignored)\n", entry->res);
  68. resource_list_destroy_entry(entry);
  69. }
  70. return 0;
  71. }
  72. /*
  73. * Create a PCI config space window
  74. * - reserve mem region
  75. * - alloc struct pci_config_window with space for all mappings
  76. * - ioremap the config space
  77. */
  78. static struct pci_config_window *arch_pci_ecam_create(struct device *dev,
  79. struct resource *cfgres, struct resource *busr, const struct pci_ecam_ops *ops)
  80. {
  81. int bsz, bus_range, err;
  82. struct resource *conflict;
  83. struct pci_config_window *cfg;
  84. if (busr->start > busr->end)
  85. return ERR_PTR(-EINVAL);
  86. cfg = kzalloc_obj(*cfg);
  87. if (!cfg)
  88. return ERR_PTR(-ENOMEM);
  89. cfg->parent = dev;
  90. cfg->ops = ops;
  91. cfg->busr.start = busr->start;
  92. cfg->busr.end = busr->end;
  93. cfg->busr.flags = IORESOURCE_BUS;
  94. bus_range = resource_size(cfgres) >> ops->bus_shift;
  95. bsz = 1 << ops->bus_shift;
  96. cfg->res.start = cfgres->start;
  97. cfg->res.end = cfgres->end;
  98. cfg->res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  99. cfg->res.name = "PCI ECAM";
  100. conflict = request_resource_conflict(&iomem_resource, &cfg->res);
  101. if (conflict) {
  102. err = -EBUSY;
  103. dev_err(dev, "can't claim ECAM area %pR: address conflict with %s %pR\n",
  104. &cfg->res, conflict->name, conflict);
  105. goto err_exit;
  106. }
  107. cfg->win = pci_remap_cfgspace(cfgres->start, bus_range * bsz);
  108. if (!cfg->win)
  109. goto err_exit_iomap;
  110. if (ops->init) {
  111. err = ops->init(cfg);
  112. if (err)
  113. goto err_exit;
  114. }
  115. dev_info(dev, "ECAM at %pR for %pR\n", &cfg->res, &cfg->busr);
  116. return cfg;
  117. err_exit_iomap:
  118. err = -ENOMEM;
  119. dev_err(dev, "ECAM ioremap failed\n");
  120. err_exit:
  121. pci_ecam_free(cfg);
  122. return ERR_PTR(err);
  123. }
  124. /*
  125. * Lookup the bus range for the domain in MCFG, and set up config space
  126. * mapping.
  127. */
  128. static struct pci_config_window *
  129. pci_acpi_setup_ecam_mapping(struct acpi_pci_root *root)
  130. {
  131. int ret, bus_shift;
  132. u16 seg = root->segment;
  133. struct device *dev = &root->device->dev;
  134. struct resource cfgres;
  135. struct resource *bus_res = &root->secondary;
  136. struct pci_config_window *cfg;
  137. const struct pci_ecam_ops *ecam_ops;
  138. ret = pci_mcfg_lookup(root, &cfgres, &ecam_ops);
  139. if (ret < 0) {
  140. dev_err(dev, "%04x:%pR ECAM region not found, use default value\n", seg, bus_res);
  141. ecam_ops = &loongson_pci_ecam_ops;
  142. root->mcfg_addr = mcfg_addr_init(0);
  143. }
  144. bus_shift = ecam_ops->bus_shift ? : 20;
  145. if (bus_shift == 20)
  146. cfg = pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
  147. else {
  148. cfgres.start = root->mcfg_addr + (bus_res->start << bus_shift);
  149. cfgres.end = cfgres.start + (resource_size(bus_res) << bus_shift) - 1;
  150. cfgres.end |= BIT(28) + (((PCI_CFG_SPACE_EXP_SIZE - 1) & 0xf00) << 16);
  151. cfgres.flags = IORESOURCE_MEM;
  152. cfg = arch_pci_ecam_create(dev, &cfgres, bus_res, ecam_ops);
  153. }
  154. if (IS_ERR(cfg)) {
  155. dev_err(dev, "%04x:%pR error %ld mapping ECAM\n", seg, bus_res, PTR_ERR(cfg));
  156. return NULL;
  157. }
  158. return cfg;
  159. }
  160. struct pci_bus *pci_acpi_scan_root(struct acpi_pci_root *root)
  161. {
  162. struct pci_bus *bus;
  163. struct pci_root_info *info;
  164. struct pci_host_bridge *host;
  165. struct acpi_pci_root_ops *root_ops;
  166. int domain = root->segment;
  167. int busnum = root->secondary.start;
  168. info = kzalloc_obj(*info);
  169. if (!info) {
  170. pr_warn("pci_bus %04x:%02x: ignored (out of memory)\n", domain, busnum);
  171. return NULL;
  172. }
  173. root_ops = kzalloc_obj(*root_ops);
  174. if (!root_ops) {
  175. kfree(info);
  176. return NULL;
  177. }
  178. info->cfg = pci_acpi_setup_ecam_mapping(root);
  179. if (!info->cfg) {
  180. kfree(info);
  181. kfree(root_ops);
  182. return NULL;
  183. }
  184. root_ops->release_info = acpi_release_root_info;
  185. root_ops->prepare_resources = acpi_prepare_root_resources;
  186. root_ops->pci_ops = (struct pci_ops *)&info->cfg->ops->pci_ops;
  187. bus = pci_find_bus(domain, busnum);
  188. if (bus) {
  189. memcpy(bus->sysdata, info->cfg, sizeof(struct pci_config_window));
  190. kfree(info);
  191. kfree(root_ops);
  192. } else {
  193. struct pci_bus *child;
  194. bus = acpi_pci_root_create(root, root_ops,
  195. &info->common, info->cfg);
  196. if (!bus) {
  197. kfree(info);
  198. kfree(root_ops);
  199. return NULL;
  200. }
  201. /* If we must preserve the resource configuration, claim now */
  202. host = pci_find_host_bridge(bus);
  203. if (host->preserve_config)
  204. pci_bus_claim_resources(bus);
  205. /*
  206. * Assign whatever was left unassigned. If we didn't claim above,
  207. * this will reassign everything.
  208. */
  209. pci_assign_unassigned_root_bus_resources(bus);
  210. list_for_each_entry(child, &bus->children, node)
  211. pcie_bus_configure_settings(child);
  212. }
  213. return bus;
  214. }