ecam.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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] = ioremap(cfgres->start + i * bsz, bsz);
  79. if (!cfg->winp[i])
  80. goto err_exit_iomap;
  81. }
  82. } else {
  83. cfg->win = ioremap(cfgres->start, bus_range * bsz);
  84. if (!cfg->win)
  85. goto err_exit_iomap;
  86. }
  87. if (ops->init) {
  88. err = ops->init(cfg);
  89. if (err)
  90. goto err_exit;
  91. }
  92. dev_info(dev, "ECAM at %pR for %pR\n", &cfg->res, &cfg->busr);
  93. return cfg;
  94. err_exit_iomap:
  95. dev_err(dev, "ECAM ioremap failed\n");
  96. err_exit_malloc:
  97. err = -ENOMEM;
  98. err_exit:
  99. pci_ecam_free(cfg);
  100. return ERR_PTR(err);
  101. }
  102. void pci_ecam_free(struct pci_config_window *cfg)
  103. {
  104. int i;
  105. if (per_bus_mapping) {
  106. if (cfg->winp) {
  107. for (i = 0; i < resource_size(&cfg->busr); i++)
  108. if (cfg->winp[i])
  109. iounmap(cfg->winp[i]);
  110. kfree(cfg->winp);
  111. }
  112. } else {
  113. if (cfg->win)
  114. iounmap(cfg->win);
  115. }
  116. if (cfg->res.parent)
  117. release_resource(&cfg->res);
  118. kfree(cfg);
  119. }
  120. /*
  121. * Function to implement the pci_ops ->map_bus method
  122. */
  123. void __iomem *pci_ecam_map_bus(struct pci_bus *bus, unsigned int devfn,
  124. int where)
  125. {
  126. struct pci_config_window *cfg = bus->sysdata;
  127. unsigned int devfn_shift = cfg->ops->bus_shift - 8;
  128. unsigned int busn = bus->number;
  129. void __iomem *base;
  130. if (busn < cfg->busr.start || busn > cfg->busr.end)
  131. return NULL;
  132. busn -= cfg->busr.start;
  133. if (per_bus_mapping)
  134. base = cfg->winp[busn];
  135. else
  136. base = cfg->win + (busn << cfg->ops->bus_shift);
  137. return base + (devfn << devfn_shift) + where;
  138. }
  139. /* ECAM ops */
  140. struct pci_ecam_ops pci_generic_ecam_ops = {
  141. .bus_shift = 20,
  142. .pci_ops = {
  143. .map_bus = pci_ecam_map_bus,
  144. .read = pci_generic_config_read,
  145. .write = pci_generic_config_write,
  146. }
  147. };