pci-host-generic.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. struct pci_ops ops;
  29. };
  30. struct gen_pci_cfg_windows {
  31. struct resource res;
  32. struct resource *bus_range;
  33. void __iomem **win;
  34. struct gen_pci_cfg_bus_ops *ops;
  35. };
  36. /*
  37. * ARM pcibios functions expect the ARM struct pci_sys_data as the PCI
  38. * sysdata. Add pci_sys_data as the first element in struct gen_pci so
  39. * that when we use a gen_pci pointer as sysdata, it is also a pointer to
  40. * a struct pci_sys_data.
  41. */
  42. struct gen_pci {
  43. #ifdef CONFIG_ARM
  44. struct pci_sys_data sys;
  45. #endif
  46. struct pci_host_bridge host;
  47. struct gen_pci_cfg_windows cfg;
  48. struct list_head resources;
  49. };
  50. static void __iomem *gen_pci_map_cfg_bus_cam(struct pci_bus *bus,
  51. unsigned int devfn,
  52. int where)
  53. {
  54. struct gen_pci *pci = bus->sysdata;
  55. resource_size_t idx = bus->number - pci->cfg.bus_range->start;
  56. return pci->cfg.win[idx] + ((devfn << 8) | where);
  57. }
  58. static struct gen_pci_cfg_bus_ops gen_pci_cfg_cam_bus_ops = {
  59. .bus_shift = 16,
  60. .ops = {
  61. .map_bus = gen_pci_map_cfg_bus_cam,
  62. .read = pci_generic_config_read,
  63. .write = pci_generic_config_write,
  64. }
  65. };
  66. static void __iomem *gen_pci_map_cfg_bus_ecam(struct pci_bus *bus,
  67. unsigned int devfn,
  68. int where)
  69. {
  70. struct gen_pci *pci = bus->sysdata;
  71. resource_size_t idx = bus->number - pci->cfg.bus_range->start;
  72. return pci->cfg.win[idx] + ((devfn << 12) | where);
  73. }
  74. static struct gen_pci_cfg_bus_ops gen_pci_cfg_ecam_bus_ops = {
  75. .bus_shift = 20,
  76. .ops = {
  77. .map_bus = gen_pci_map_cfg_bus_ecam,
  78. .read = pci_generic_config_read,
  79. .write = pci_generic_config_write,
  80. }
  81. };
  82. static const struct of_device_id gen_pci_of_match[] = {
  83. { .compatible = "pci-host-cam-generic",
  84. .data = &gen_pci_cfg_cam_bus_ops },
  85. { .compatible = "pci-host-ecam-generic",
  86. .data = &gen_pci_cfg_ecam_bus_ops },
  87. { },
  88. };
  89. MODULE_DEVICE_TABLE(of, gen_pci_of_match);
  90. static void gen_pci_release_of_pci_ranges(struct gen_pci *pci)
  91. {
  92. pci_free_resource_list(&pci->resources);
  93. }
  94. static int gen_pci_parse_request_of_pci_ranges(struct gen_pci *pci)
  95. {
  96. int err, res_valid = 0;
  97. struct device *dev = pci->host.dev.parent;
  98. struct device_node *np = dev->of_node;
  99. resource_size_t iobase;
  100. struct resource_entry *win;
  101. err = of_pci_get_host_bridge_resources(np, 0, 0xff, &pci->resources,
  102. &iobase);
  103. if (err)
  104. return err;
  105. resource_list_for_each_entry(win, &pci->resources) {
  106. struct resource *parent, *res = win->res;
  107. switch (resource_type(res)) {
  108. case IORESOURCE_IO:
  109. parent = &ioport_resource;
  110. err = pci_remap_iospace(res, iobase);
  111. if (err) {
  112. dev_warn(dev, "error %d: failed to map resource %pR\n",
  113. err, res);
  114. continue;
  115. }
  116. break;
  117. case IORESOURCE_MEM:
  118. parent = &iomem_resource;
  119. res_valid |= !(res->flags & IORESOURCE_PREFETCH);
  120. break;
  121. case IORESOURCE_BUS:
  122. pci->cfg.bus_range = res;
  123. default:
  124. continue;
  125. }
  126. err = devm_request_resource(dev, parent, res);
  127. if (err)
  128. goto out_release_res;
  129. }
  130. if (!res_valid) {
  131. dev_err(dev, "non-prefetchable memory resource required\n");
  132. err = -EINVAL;
  133. goto out_release_res;
  134. }
  135. return 0;
  136. out_release_res:
  137. gen_pci_release_of_pci_ranges(pci);
  138. return err;
  139. }
  140. static int gen_pci_parse_map_cfg_windows(struct gen_pci *pci)
  141. {
  142. int err;
  143. u8 bus_max;
  144. resource_size_t busn;
  145. struct resource *bus_range;
  146. struct device *dev = pci->host.dev.parent;
  147. struct device_node *np = dev->of_node;
  148. u32 sz = 1 << pci->cfg.ops->bus_shift;
  149. err = of_address_to_resource(np, 0, &pci->cfg.res);
  150. if (err) {
  151. dev_err(dev, "missing \"reg\" property\n");
  152. return err;
  153. }
  154. /* Limit the bus-range to fit within reg */
  155. bus_max = pci->cfg.bus_range->start +
  156. (resource_size(&pci->cfg.res) >> pci->cfg.ops->bus_shift) - 1;
  157. pci->cfg.bus_range->end = min_t(resource_size_t,
  158. pci->cfg.bus_range->end, bus_max);
  159. pci->cfg.win = devm_kcalloc(dev, resource_size(pci->cfg.bus_range),
  160. sizeof(*pci->cfg.win), GFP_KERNEL);
  161. if (!pci->cfg.win)
  162. return -ENOMEM;
  163. /* Map our Configuration Space windows */
  164. if (!devm_request_mem_region(dev, pci->cfg.res.start,
  165. resource_size(&pci->cfg.res),
  166. "Configuration Space"))
  167. return -ENOMEM;
  168. bus_range = pci->cfg.bus_range;
  169. for (busn = bus_range->start; busn <= bus_range->end; ++busn) {
  170. u32 idx = busn - bus_range->start;
  171. pci->cfg.win[idx] = devm_ioremap(dev,
  172. pci->cfg.res.start + idx * sz,
  173. sz);
  174. if (!pci->cfg.win[idx])
  175. return -ENOMEM;
  176. }
  177. return 0;
  178. }
  179. static int gen_pci_probe(struct platform_device *pdev)
  180. {
  181. int err;
  182. const char *type;
  183. const struct of_device_id *of_id;
  184. struct device *dev = &pdev->dev;
  185. struct device_node *np = dev->of_node;
  186. struct gen_pci *pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  187. struct pci_bus *bus, *child;
  188. if (!pci)
  189. return -ENOMEM;
  190. type = of_get_property(np, "device_type", NULL);
  191. if (!type || strcmp(type, "pci")) {
  192. dev_err(dev, "invalid \"device_type\" %s\n", type);
  193. return -EINVAL;
  194. }
  195. of_pci_check_probe_only();
  196. of_id = of_match_node(gen_pci_of_match, np);
  197. pci->cfg.ops = (struct gen_pci_cfg_bus_ops *)of_id->data;
  198. pci->host.dev.parent = dev;
  199. INIT_LIST_HEAD(&pci->host.windows);
  200. INIT_LIST_HEAD(&pci->resources);
  201. /* Parse our PCI ranges and request their resources */
  202. err = gen_pci_parse_request_of_pci_ranges(pci);
  203. if (err)
  204. return err;
  205. /* Parse and map our Configuration Space windows */
  206. err = gen_pci_parse_map_cfg_windows(pci);
  207. if (err) {
  208. gen_pci_release_of_pci_ranges(pci);
  209. return err;
  210. }
  211. /* Do not reassign resources if probe only */
  212. if (!pci_has_flag(PCI_PROBE_ONLY))
  213. pci_add_flags(PCI_REASSIGN_ALL_RSRC | PCI_REASSIGN_ALL_BUS);
  214. bus = pci_scan_root_bus(dev, pci->cfg.bus_range->start,
  215. &pci->cfg.ops->ops, pci, &pci->resources);
  216. if (!bus) {
  217. dev_err(dev, "Scanning rootbus failed");
  218. return -ENODEV;
  219. }
  220. pci_fixup_irqs(pci_common_swizzle, of_irq_parse_and_map_pci);
  221. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  222. pci_bus_size_bridges(bus);
  223. pci_bus_assign_resources(bus);
  224. list_for_each_entry(child, &bus->children, node)
  225. pcie_bus_configure_settings(child);
  226. }
  227. pci_bus_add_devices(bus);
  228. return 0;
  229. }
  230. static struct platform_driver gen_pci_driver = {
  231. .driver = {
  232. .name = "pci-host-generic",
  233. .of_match_table = gen_pci_of_match,
  234. },
  235. .probe = gen_pci_probe,
  236. };
  237. module_platform_driver(gen_pci_driver);
  238. MODULE_DESCRIPTION("Generic PCI host driver");
  239. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  240. MODULE_LICENSE("GPL v2");