pci-host-generic.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Simple, generic PCI host controller driver targetting firmware-initialised
  3. * systems and virtual machines (e.g. the PCI emulation provided by kvmtool).
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Copyright (C) 2014 ARM Limited
  18. *
  19. * Author: Will Deacon <will.deacon@arm.com>
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of_address.h>
  24. #include <linux/of_pci.h>
  25. #include <linux/platform_device.h>
  26. struct gen_pci_cfg_bus_ops {
  27. u32 bus_shift;
  28. void __iomem *(*map_bus)(struct pci_bus *, unsigned int, int);
  29. };
  30. struct gen_pci_cfg_windows {
  31. struct resource res;
  32. struct resource *bus_range;
  33. void __iomem **win;
  34. const struct gen_pci_cfg_bus_ops *ops;
  35. };
  36. struct gen_pci {
  37. struct pci_host_bridge host;
  38. struct gen_pci_cfg_windows cfg;
  39. struct list_head resources;
  40. };
  41. static void __iomem *gen_pci_map_cfg_bus_cam(struct pci_bus *bus,
  42. unsigned int devfn,
  43. int where)
  44. {
  45. struct pci_sys_data *sys = bus->sysdata;
  46. struct gen_pci *pci = sys->private_data;
  47. resource_size_t idx = bus->number - pci->cfg.bus_range->start;
  48. return pci->cfg.win[idx] + ((devfn << 8) | where);
  49. }
  50. static struct gen_pci_cfg_bus_ops gen_pci_cfg_cam_bus_ops = {
  51. .bus_shift = 16,
  52. .map_bus = gen_pci_map_cfg_bus_cam,
  53. };
  54. static void __iomem *gen_pci_map_cfg_bus_ecam(struct pci_bus *bus,
  55. unsigned int devfn,
  56. int where)
  57. {
  58. struct pci_sys_data *sys = bus->sysdata;
  59. struct gen_pci *pci = sys->private_data;
  60. resource_size_t idx = bus->number - pci->cfg.bus_range->start;
  61. return pci->cfg.win[idx] + ((devfn << 12) | where);
  62. }
  63. static struct gen_pci_cfg_bus_ops gen_pci_cfg_ecam_bus_ops = {
  64. .bus_shift = 20,
  65. .map_bus = gen_pci_map_cfg_bus_ecam,
  66. };
  67. static struct pci_ops gen_pci_ops = {
  68. .read = pci_generic_config_read,
  69. .write = pci_generic_config_write,
  70. };
  71. static const struct of_device_id gen_pci_of_match[] = {
  72. { .compatible = "pci-host-cam-generic",
  73. .data = &gen_pci_cfg_cam_bus_ops },
  74. { .compatible = "pci-host-ecam-generic",
  75. .data = &gen_pci_cfg_ecam_bus_ops },
  76. { },
  77. };
  78. MODULE_DEVICE_TABLE(of, gen_pci_of_match);
  79. static void gen_pci_release_of_pci_ranges(struct gen_pci *pci)
  80. {
  81. pci_free_resource_list(&pci->resources);
  82. }
  83. static int gen_pci_parse_request_of_pci_ranges(struct gen_pci *pci)
  84. {
  85. int err, res_valid = 0;
  86. struct device *dev = pci->host.dev.parent;
  87. struct device_node *np = dev->of_node;
  88. resource_size_t iobase;
  89. struct resource_entry *win;
  90. err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources,
  91. &iobase);
  92. if (err)
  93. return err;
  94. resource_list_for_each_entry(win, &pci->resources) {
  95. struct resource *parent, *res = win->res;
  96. switch (resource_type(res)) {
  97. case IORESOURCE_IO:
  98. parent = &ioport_resource;
  99. err = pci_remap_iospace(res, iobase);
  100. if (err) {
  101. dev_warn(dev, "error %d: failed to map resource %pR\n",
  102. err, res);
  103. continue;
  104. }
  105. break;
  106. case IORESOURCE_MEM:
  107. parent = &iomem_resource;
  108. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  109. break;
  110. case IORESOURCE_BUS:
  111. pci->cfg.bus_range = res;
  112. default:
  113. continue;
  114. }
  115. err = devm_request_resource(dev, parent, res);
  116. if (err)
  117. goto out_release_res;
  118. }
  119. if (!res_valid) {
  120. dev_err(dev, "non-prefetchable memory resource required\n");
  121. err = -EINVAL;
  122. goto out_release_res;
  123. }
  124. return 0;
  125. out_release_res:
  126. gen_pci_release_of_pci_ranges(pci);
  127. return err;
  128. }
  129. static int gen_pci_parse_map_cfg_windows(struct gen_pci *pci)
  130. {
  131. int err;
  132. u8 bus_max;
  133. resource_size_t busn;
  134. struct resource *bus_range;
  135. struct device *dev = pci->host.dev.parent;
  136. struct device_node *np = dev->of_node;
  137. err = of_address_to_resource(np, 0, &pci->cfg.res);
  138. if (err) {
  139. dev_err(dev, "missing \"reg\" property\n");
  140. return err;
  141. }
  142. /* Limit the bus-range to fit within reg */
  143. bus_max = pci->cfg.bus_range->start +
  144. (resource_size(&pci->cfg.res) >> pci->cfg.ops->bus_shift) - 1;
  145. pci->cfg.bus_range->end = min_t(resource_size_t,
  146. pci->cfg.bus_range->end, bus_max);
  147. pci->cfg.win = devm_kcalloc(dev, resource_size(pci->cfg.bus_range),
  148. sizeof(*pci->cfg.win), GFP_KERNEL);
  149. if (!pci->cfg.win)
  150. return -ENOMEM;
  151. /* Map our Configuration Space windows */
  152. if (!devm_request_mem_region(dev, pci->cfg.res.start,
  153. resource_size(&pci->cfg.res),
  154. "Configuration Space"))
  155. return -ENOMEM;
  156. bus_range = pci->cfg.bus_range;
  157. for (busn = bus_range->start; busn <= bus_range->end; ++busn) {
  158. u32 idx = busn - bus_range->start;
  159. u32 sz = 1 << pci->cfg.ops->bus_shift;
  160. pci->cfg.win[idx] = devm_ioremap(dev,
  161. pci->cfg.res.start + busn * sz,
  162. sz);
  163. if (!pci->cfg.win[idx])
  164. return -ENOMEM;
  165. }
  166. return 0;
  167. }
  168. static int gen_pci_setup(int nr, struct pci_sys_data *sys)
  169. {
  170. struct gen_pci *pci = sys->private_data;
  171. list_splice_init(&pci->resources, &sys->resources);
  172. return 1;
  173. }
  174. static int gen_pci_probe(struct platform_device *pdev)
  175. {
  176. int err;
  177. const char *type;
  178. const struct of_device_id *of_id;
  179. const int *prop;
  180. struct device *dev = &pdev->dev;
  181. struct device_node *np = dev->of_node;
  182. struct gen_pci *pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  183. struct hw_pci hw = {
  184. .nr_controllers = 1,
  185. .private_data = (void **)&pci,
  186. .setup = gen_pci_setup,
  187. .map_irq = of_irq_parse_and_map_pci,
  188. .ops = &gen_pci_ops,
  189. };
  190. if (!pci)
  191. return -ENOMEM;
  192. type = of_get_property(np, "device_type", NULL);
  193. if (!type || strcmp(type, "pci")) {
  194. dev_err(dev, "invalid \"device_type\" %s\n", type);
  195. return -EINVAL;
  196. }
  197. prop = of_get_property(of_chosen, "linux,pci-probe-only", NULL);
  198. if (prop) {
  199. if (*prop)
  200. pci_add_flags(PCI_PROBE_ONLY);
  201. else
  202. pci_clear_flags(PCI_PROBE_ONLY);
  203. }
  204. of_id = of_match_node(gen_pci_of_match, np);
  205. pci->cfg.ops = of_id->data;
  206. gen_pci_ops.map_bus = pci->cfg.ops->map_bus;
  207. pci->host.dev.parent = dev;
  208. INIT_LIST_HEAD(&pci->host.windows);
  209. INIT_LIST_HEAD(&pci->resources);
  210. /* Parse our PCI ranges and request their resources */
  211. err = gen_pci_parse_request_of_pci_ranges(pci);
  212. if (err)
  213. return err;
  214. /* Parse and map our Configuration Space windows */
  215. err = gen_pci_parse_map_cfg_windows(pci);
  216. if (err) {
  217. gen_pci_release_of_pci_ranges(pci);
  218. return err;
  219. }
  220. pci_common_init_dev(dev, &hw);
  221. return 0;
  222. }
  223. static struct platform_driver gen_pci_driver = {
  224. .driver = {
  225. .name = "pci-host-generic",
  226. .of_match_table = gen_pci_of_match,
  227. },
  228. .probe = gen_pci_probe,
  229. };
  230. module_platform_driver(gen_pci_driver);
  231. MODULE_DESCRIPTION("Generic PCI host driver");
  232. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  233. MODULE_LICENSE("GPL v2");