pci-host-generic.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 int gen_pci_config_read(struct pci_bus *bus, unsigned int devfn,
  68. int where, int size, u32 *val)
  69. {
  70. void __iomem *addr;
  71. struct pci_sys_data *sys = bus->sysdata;
  72. struct gen_pci *pci = sys->private_data;
  73. addr = pci->cfg.ops->map_bus(bus, devfn, where);
  74. switch (size) {
  75. case 1:
  76. *val = readb(addr);
  77. break;
  78. case 2:
  79. *val = readw(addr);
  80. break;
  81. default:
  82. *val = readl(addr);
  83. }
  84. return PCIBIOS_SUCCESSFUL;
  85. }
  86. static int gen_pci_config_write(struct pci_bus *bus, unsigned int devfn,
  87. int where, int size, u32 val)
  88. {
  89. void __iomem *addr;
  90. struct pci_sys_data *sys = bus->sysdata;
  91. struct gen_pci *pci = sys->private_data;
  92. addr = pci->cfg.ops->map_bus(bus, devfn, where);
  93. switch (size) {
  94. case 1:
  95. writeb(val, addr);
  96. break;
  97. case 2:
  98. writew(val, addr);
  99. break;
  100. default:
  101. writel(val, addr);
  102. }
  103. return PCIBIOS_SUCCESSFUL;
  104. }
  105. static struct pci_ops gen_pci_ops = {
  106. .read = gen_pci_config_read,
  107. .write = gen_pci_config_write,
  108. };
  109. static const struct of_device_id gen_pci_of_match[] = {
  110. { .compatible = "pci-host-cam-generic",
  111. .data = &gen_pci_cfg_cam_bus_ops },
  112. { .compatible = "pci-host-ecam-generic",
  113. .data = &gen_pci_cfg_ecam_bus_ops },
  114. { },
  115. };
  116. MODULE_DEVICE_TABLE(of, gen_pci_of_match);
  117. static int gen_pci_calc_io_offset(struct device *dev,
  118. struct of_pci_range *range,
  119. struct resource *res,
  120. resource_size_t *offset)
  121. {
  122. static atomic_t wins = ATOMIC_INIT(0);
  123. int err, idx, max_win;
  124. unsigned int window;
  125. if (!PAGE_ALIGNED(range->cpu_addr))
  126. return -EINVAL;
  127. max_win = (IO_SPACE_LIMIT + 1) / SZ_64K;
  128. idx = atomic_inc_return(&wins);
  129. if (idx > max_win)
  130. return -ENOSPC;
  131. window = (idx - 1) * SZ_64K;
  132. err = pci_ioremap_io(window, range->cpu_addr);
  133. if (err)
  134. return err;
  135. of_pci_range_to_resource(range, dev->of_node, res);
  136. res->start = window;
  137. res->end = res->start + range->size - 1;
  138. *offset = window - range->pci_addr;
  139. return 0;
  140. }
  141. static int gen_pci_calc_mem_offset(struct device *dev,
  142. struct of_pci_range *range,
  143. struct resource *res,
  144. resource_size_t *offset)
  145. {
  146. of_pci_range_to_resource(range, dev->of_node, res);
  147. *offset = range->cpu_addr - range->pci_addr;
  148. return 0;
  149. }
  150. static void gen_pci_release_of_pci_ranges(struct gen_pci *pci)
  151. {
  152. struct pci_host_bridge_window *win;
  153. list_for_each_entry(win, &pci->resources, list)
  154. release_resource(win->res);
  155. pci_free_resource_list(&pci->resources);
  156. }
  157. static int gen_pci_parse_request_of_pci_ranges(struct gen_pci *pci)
  158. {
  159. struct of_pci_range range;
  160. struct of_pci_range_parser parser;
  161. int err, res_valid = 0;
  162. struct device *dev = pci->host.dev.parent;
  163. struct device_node *np = dev->of_node;
  164. if (of_pci_range_parser_init(&parser, np)) {
  165. dev_err(dev, "missing \"ranges\" property\n");
  166. return -EINVAL;
  167. }
  168. for_each_of_pci_range(&parser, &range) {
  169. struct resource *parent, *res;
  170. resource_size_t offset;
  171. u32 restype = range.flags & IORESOURCE_TYPE_BITS;
  172. res = devm_kmalloc(dev, sizeof(*res), GFP_KERNEL);
  173. if (!res) {
  174. err = -ENOMEM;
  175. goto out_release_res;
  176. }
  177. switch (restype) {
  178. case IORESOURCE_IO:
  179. parent = &ioport_resource;
  180. err = gen_pci_calc_io_offset(dev, &range, res, &offset);
  181. break;
  182. case IORESOURCE_MEM:
  183. parent = &iomem_resource;
  184. err = gen_pci_calc_mem_offset(dev, &range, res, &offset);
  185. res_valid |= !(res->flags & IORESOURCE_PREFETCH || err);
  186. break;
  187. default:
  188. err = -EINVAL;
  189. continue;
  190. }
  191. if (err) {
  192. dev_warn(dev,
  193. "error %d: failed to add resource [type 0x%x, %lld bytes]\n",
  194. err, restype, range.size);
  195. continue;
  196. }
  197. err = request_resource(parent, res);
  198. if (err)
  199. goto out_release_res;
  200. pci_add_resource_offset(&pci->resources, res, offset);
  201. }
  202. if (!res_valid) {
  203. dev_err(dev, "non-prefetchable memory resource required\n");
  204. err = -EINVAL;
  205. goto out_release_res;
  206. }
  207. return 0;
  208. out_release_res:
  209. gen_pci_release_of_pci_ranges(pci);
  210. return err;
  211. }
  212. static int gen_pci_parse_map_cfg_windows(struct gen_pci *pci)
  213. {
  214. int err;
  215. u8 bus_max;
  216. resource_size_t busn;
  217. struct resource *bus_range;
  218. struct device *dev = pci->host.dev.parent;
  219. struct device_node *np = dev->of_node;
  220. if (of_pci_parse_bus_range(np, &pci->cfg.bus_range))
  221. pci->cfg.bus_range = (struct resource) {
  222. .name = np->name,
  223. .start = 0,
  224. .end = 0xff,
  225. .flags = IORESOURCE_BUS,
  226. };
  227. err = of_address_to_resource(np, 0, &pci->cfg.res);
  228. if (err) {
  229. dev_err(dev, "missing \"reg\" property\n");
  230. return err;
  231. }
  232. pci->cfg.win = devm_kcalloc(dev, resource_size(&pci->cfg.bus_range),
  233. sizeof(*pci->cfg.win), GFP_KERNEL);
  234. if (!pci->cfg.win)
  235. return -ENOMEM;
  236. /* Limit the bus-range to fit within reg */
  237. bus_max = pci->cfg.bus_range.start +
  238. (resource_size(&pci->cfg.res) >> pci->cfg.ops->bus_shift) - 1;
  239. pci->cfg.bus_range.end = min_t(resource_size_t, pci->cfg.bus_range.end,
  240. bus_max);
  241. /* Map our Configuration Space windows */
  242. if (!devm_request_mem_region(dev, pci->cfg.res.start,
  243. resource_size(&pci->cfg.res),
  244. "Configuration Space"))
  245. return -ENOMEM;
  246. bus_range = &pci->cfg.bus_range;
  247. for (busn = bus_range->start; busn <= bus_range->end; ++busn) {
  248. u32 idx = busn - bus_range->start;
  249. u32 sz = 1 << pci->cfg.ops->bus_shift;
  250. pci->cfg.win[idx] = devm_ioremap(dev,
  251. pci->cfg.res.start + busn * sz,
  252. sz);
  253. if (!pci->cfg.win[idx])
  254. return -ENOMEM;
  255. }
  256. /* Register bus resource */
  257. pci_add_resource(&pci->resources, bus_range);
  258. return 0;
  259. }
  260. static int gen_pci_setup(int nr, struct pci_sys_data *sys)
  261. {
  262. struct gen_pci *pci = sys->private_data;
  263. list_splice_init(&pci->resources, &sys->resources);
  264. return 1;
  265. }
  266. static int gen_pci_probe(struct platform_device *pdev)
  267. {
  268. int err;
  269. const char *type;
  270. const struct of_device_id *of_id;
  271. const int *prop;
  272. struct device *dev = &pdev->dev;
  273. struct device_node *np = dev->of_node;
  274. struct gen_pci *pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
  275. struct hw_pci hw = {
  276. .nr_controllers = 1,
  277. .private_data = (void **)&pci,
  278. .setup = gen_pci_setup,
  279. .map_irq = of_irq_parse_and_map_pci,
  280. .ops = &gen_pci_ops,
  281. };
  282. if (!pci)
  283. return -ENOMEM;
  284. type = of_get_property(np, "device_type", NULL);
  285. if (!type || strcmp(type, "pci")) {
  286. dev_err(dev, "invalid \"device_type\" %s\n", type);
  287. return -EINVAL;
  288. }
  289. prop = of_get_property(of_chosen, "linux,pci-probe-only", NULL);
  290. if (prop) {
  291. if (*prop)
  292. pci_add_flags(PCI_PROBE_ONLY);
  293. else
  294. pci_clear_flags(PCI_PROBE_ONLY);
  295. }
  296. of_id = of_match_node(gen_pci_of_match, np);
  297. pci->cfg.ops = of_id->data;
  298. pci->host.dev.parent = dev;
  299. INIT_LIST_HEAD(&pci->host.windows);
  300. INIT_LIST_HEAD(&pci->resources);
  301. /* Parse our PCI ranges and request their resources */
  302. err = gen_pci_parse_request_of_pci_ranges(pci);
  303. if (err)
  304. return err;
  305. /* Parse and map our Configuration Space windows */
  306. err = gen_pci_parse_map_cfg_windows(pci);
  307. if (err) {
  308. gen_pci_release_of_pci_ranges(pci);
  309. return err;
  310. }
  311. pci_common_init_dev(dev, &hw);
  312. return 0;
  313. }
  314. static struct platform_driver gen_pci_driver = {
  315. .driver = {
  316. .name = "pci-host-generic",
  317. .owner = THIS_MODULE,
  318. .of_match_table = gen_pci_of_match,
  319. },
  320. .probe = gen_pci_probe,
  321. };
  322. module_platform_driver(gen_pci_driver);
  323. MODULE_DESCRIPTION("Generic PCI host driver");
  324. MODULE_AUTHOR("Will Deacon <will.deacon@arm.com>");
  325. MODULE_LICENSE("GPLv2");