ecam.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/slab.h>
  22. #include "ecam.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 = !config_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->ops = ops;
  49. cfg->busr.start = busr->start;
  50. cfg->busr.end = busr->end;
  51. cfg->busr.flags = IORESOURCE_BUS;
  52. bus_range = resource_size(&cfg->busr);
  53. bus_range_max = resource_size(cfgres) >> ops->bus_shift;
  54. if (bus_range > bus_range_max) {
  55. bus_range = bus_range_max;
  56. cfg->busr.end = busr->start + bus_range - 1;
  57. dev_warn(dev, "ECAM area %pR can only accommodate %pR (reduced from %pR desired)\n",
  58. cfgres, &cfg->busr, busr);
  59. }
  60. bsz = 1 << ops->bus_shift;
  61. cfg->res.start = cfgres->start;
  62. cfg->res.end = cfgres->end;
  63. cfg->res.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  64. cfg->res.name = "PCI ECAM";
  65. conflict = request_resource_conflict(&iomem_resource, &cfg->res);
  66. if (conflict) {
  67. err = -EBUSY;
  68. dev_err(dev, "can't claim ECAM area %pR: address conflict with %s %pR\n",
  69. &cfg->res, conflict->name, conflict);
  70. goto err_exit;
  71. }
  72. if (per_bus_mapping) {
  73. cfg->winp = kcalloc(bus_range, sizeof(*cfg->winp), GFP_KERNEL);
  74. if (!cfg->winp)
  75. goto err_exit_malloc;
  76. for (i = 0; i < bus_range; i++) {
  77. cfg->winp[i] = ioremap(cfgres->start + i * bsz, bsz);
  78. if (!cfg->winp[i])
  79. goto err_exit_iomap;
  80. }
  81. } else {
  82. cfg->win = ioremap(cfgres->start, bus_range * bsz);
  83. if (!cfg->win)
  84. goto err_exit_iomap;
  85. }
  86. if (ops->init) {
  87. err = ops->init(dev, cfg);
  88. if (err)
  89. goto err_exit;
  90. }
  91. dev_info(dev, "ECAM at %pR for %pR\n", &cfg->res, &cfg->busr);
  92. return cfg;
  93. err_exit_iomap:
  94. dev_err(dev, "ECAM ioremap failed\n");
  95. err_exit_malloc:
  96. err = -ENOMEM;
  97. err_exit:
  98. pci_ecam_free(cfg);
  99. return ERR_PTR(err);
  100. }
  101. void pci_ecam_free(struct pci_config_window *cfg)
  102. {
  103. int i;
  104. if (per_bus_mapping) {
  105. if (cfg->winp) {
  106. for (i = 0; i < resource_size(&cfg->busr); i++)
  107. if (cfg->winp[i])
  108. iounmap(cfg->winp[i]);
  109. kfree(cfg->winp);
  110. }
  111. } else {
  112. if (cfg->win)
  113. iounmap(cfg->win);
  114. }
  115. if (cfg->res.parent)
  116. release_resource(&cfg->res);
  117. kfree(cfg);
  118. }
  119. /*
  120. * Function to implement the pci_ops ->map_bus method
  121. */
  122. void __iomem *pci_ecam_map_bus(struct pci_bus *bus, unsigned int devfn,
  123. int where)
  124. {
  125. struct pci_config_window *cfg = bus->sysdata;
  126. unsigned int devfn_shift = cfg->ops->bus_shift - 8;
  127. unsigned int busn = bus->number;
  128. void __iomem *base;
  129. if (busn < cfg->busr.start || busn > cfg->busr.end)
  130. return NULL;
  131. busn -= cfg->busr.start;
  132. if (per_bus_mapping)
  133. base = cfg->winp[busn];
  134. else
  135. base = cfg->win + (busn << cfg->ops->bus_shift);
  136. return base + (devfn << devfn_shift) + where;
  137. }
  138. /* ECAM ops */
  139. struct pci_ecam_ops pci_generic_ecam_ops = {
  140. .bus_shift = 20,
  141. .pci_ops = {
  142. .map_bus = pci_ecam_map_bus,
  143. .read = pci_generic_config_read,
  144. .write = pci_generic_config_write,
  145. }
  146. };