ecam.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright 2016 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License, version 2, as
  6. * published by the Free Software Foundation (the "GPL").
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License version 2 (GPLv2) for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * version 2 (GPLv2) along with this source code.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/pci.h>
  21. #include <linux/pci-ecam.h>
  22. #include <linux/slab.h>
  23. /*
  24. * On 64-bit systems, we do a single ioremap for the whole config space
  25. * since we have enough virtual address range available. On 32-bit, we
  26. * ioremap the config space for each bus individually.
  27. */
  28. static const bool per_bus_mapping = !IS_ENABLED(CONFIG_64BIT);
  29. /*
  30. * Create a PCI config space window
  31. * - reserve mem region
  32. * - alloc struct pci_config_window with space for all mappings
  33. * - ioremap the config space
  34. */
  35. struct pci_config_window *pci_ecam_create(struct device *dev,
  36. struct resource *cfgres, struct resource *busr,
  37. struct pci_ecam_ops *ops)
  38. {
  39. struct pci_config_window *cfg;
  40. unsigned int bus_range, bus_range_max, bsz;
  41. struct resource *conflict;
  42. int i, err;
  43. if (busr->start > busr->end)
  44. return ERR_PTR(-EINVAL);
  45. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  46. if (!cfg)
  47. return ERR_PTR(-ENOMEM);
  48. cfg->parent = dev;
  49. cfg->ops = ops;
  50. cfg->busr.start = busr->start;
  51. cfg->busr.end = busr->end;
  52. cfg->busr.flags = IORESOURCE_BUS;
  53. bus_range = resource_size(&cfg->busr);
  54. bus_range_max = resource_size(cfgres) >> ops->bus_shift;
  55. if (bus_range > bus_range_max) {
  56. bus_range = bus_range_max;
  57. cfg->busr.end = busr->start + bus_range - 1;
  58. dev_warn(dev, "ECAM area %pR can only accommodate %pR (reduced from %pR desired)\n",
  59. cfgres, &cfg->busr, busr);
  60. }
  61. bsz = 1 << ops->bus_shift;
  62. cfg->res.start = cfgres->start;
  63. cfg->res.end = cfgres->end;
  64. cfg->res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  65. cfg->res.name = "PCI ECAM";
  66. conflict = request_resource_conflict(&iomem_resource, &cfg->res);
  67. if (conflict) {
  68. err = -EBUSY;
  69. dev_err(dev, "can't claim ECAM area %pR: address conflict with %s %pR\n",
  70. &cfg->res, conflict->name, conflict);
  71. goto err_exit;
  72. }
  73. if (per_bus_mapping) {
  74. cfg->winp = kcalloc(bus_range, sizeof(*cfg->winp), GFP_KERNEL);
  75. if (!cfg->winp)
  76. goto err_exit_malloc;
  77. for (i = 0; i < bus_range; i++) {
  78. cfg->winp[i] =
  79. pci_remap_cfgspace(cfgres->start + i * bsz,
  80. bsz);
  81. if (!cfg->winp[i])
  82. goto err_exit_iomap;
  83. }
  84. } else {
  85. cfg->win = pci_remap_cfgspace(cfgres->start, bus_range * bsz);
  86. if (!cfg->win)
  87. goto err_exit_iomap;
  88. }
  89. if (ops->init) {
  90. err = ops->init(cfg);
  91. if (err)
  92. goto err_exit;
  93. }
  94. dev_info(dev, "ECAM at %pR for %pR\n", &cfg->res, &cfg->busr);
  95. return cfg;
  96. err_exit_iomap:
  97. dev_err(dev, "ECAM ioremap failed\n");
  98. err_exit_malloc:
  99. err = -ENOMEM;
  100. err_exit:
  101. pci_ecam_free(cfg);
  102. return ERR_PTR(err);
  103. }
  104. void pci_ecam_free(struct pci_config_window *cfg)
  105. {
  106. int i;
  107. if (per_bus_mapping) {
  108. if (cfg->winp) {
  109. for (i = 0; i < resource_size(&cfg->busr); i++)
  110. if (cfg->winp[i])
  111. iounmap(cfg->winp[i]);
  112. kfree(cfg->winp);
  113. }
  114. } else {
  115. if (cfg->win)
  116. iounmap(cfg->win);
  117. }
  118. if (cfg->res.parent)
  119. release_resource(&cfg->res);
  120. kfree(cfg);
  121. }
  122. /*
  123. * Function to implement the pci_ops ->map_bus method
  124. */
  125. void __iomem *pci_ecam_map_bus(struct pci_bus *bus, unsigned int devfn,
  126. int where)
  127. {
  128. struct pci_config_window *cfg = bus->sysdata;
  129. unsigned int devfn_shift = cfg->ops->bus_shift - 8;
  130. unsigned int busn = bus->number;
  131. void __iomem *base;
  132. if (busn < cfg->busr.start || busn > cfg->busr.end)
  133. return NULL;
  134. busn -= cfg->busr.start;
  135. if (per_bus_mapping)
  136. base = cfg->winp[busn];
  137. else
  138. base = cfg->win + (busn << cfg->ops->bus_shift);
  139. return base + (devfn << devfn_shift) + where;
  140. }
  141. /* ECAM ops */
  142. struct pci_ecam_ops pci_generic_ecam_ops = {
  143. .bus_shift = 20,
  144. .pci_ops = {
  145. .map_bus = pci_ecam_map_bus,
  146. .read = pci_generic_config_read,
  147. .write = pci_generic_config_write,
  148. }
  149. };
  150. #if defined(CONFIG_ACPI) && defined(CONFIG_PCI_QUIRKS)
  151. /* ECAM ops for 32-bit access only (non-compliant) */
  152. struct pci_ecam_ops pci_32b_ops = {
  153. .bus_shift = 20,
  154. .pci_ops = {
  155. .map_bus = pci_ecam_map_bus,
  156. .read = pci_generic_config_read32,
  157. .write = pci_generic_config_write32,
  158. }
  159. };
  160. #endif