pci-legacy.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation; either version 2 of the License, or (at your
  5. * option) any later version.
  6. *
  7. * Copyright (C) 2003, 04, 11 Ralf Baechle (ralf@linux-mips.org)
  8. * Copyright (C) 2011 Wind River Systems,
  9. * written by Ralf Baechle (ralf@linux-mips.org)
  10. */
  11. #include <linux/bug.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/export.h>
  16. #include <linux/init.h>
  17. #include <linux/types.h>
  18. #include <linux/pci.h>
  19. #include <linux/of_address.h>
  20. #include <asm/cpu-info.h>
  21. /*
  22. * If PCI_PROBE_ONLY in pci_flags is set, we don't change any PCI resource
  23. * assignments.
  24. */
  25. /*
  26. * The PCI controller list.
  27. */
  28. static LIST_HEAD(controllers);
  29. static int pci_initialized;
  30. /*
  31. * We need to avoid collisions with `mirrored' VGA ports
  32. * and other strange ISA hardware, so we always want the
  33. * addresses to be allocated in the 0x000-0x0ff region
  34. * modulo 0x400.
  35. *
  36. * Why? Because some silly external IO cards only decode
  37. * the low 10 bits of the IO address. The 0x00-0xff region
  38. * is reserved for motherboard devices that decode all 16
  39. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  40. * but we want to try to avoid allocating at 0x2900-0x2bff
  41. * which might have be mirrored at 0x0100-0x03ff..
  42. */
  43. resource_size_t
  44. pcibios_align_resource(void *data, const struct resource *res,
  45. resource_size_t size, resource_size_t align)
  46. {
  47. struct pci_dev *dev = data;
  48. struct pci_controller *hose = dev->sysdata;
  49. resource_size_t start = res->start;
  50. if (res->flags & IORESOURCE_IO) {
  51. /* Make sure we start at our min on all hoses */
  52. if (start < PCIBIOS_MIN_IO + hose->io_resource->start)
  53. start = PCIBIOS_MIN_IO + hose->io_resource->start;
  54. /*
  55. * Put everything into 0x00-0xff region modulo 0x400
  56. */
  57. if (start & 0x300)
  58. start = (start + 0x3ff) & ~0x3ff;
  59. } else if (res->flags & IORESOURCE_MEM) {
  60. /* Make sure we start at our min on all hoses */
  61. if (start < PCIBIOS_MIN_MEM + hose->mem_resource->start)
  62. start = PCIBIOS_MIN_MEM + hose->mem_resource->start;
  63. }
  64. return start;
  65. }
  66. static void pcibios_scanbus(struct pci_controller *hose)
  67. {
  68. static int next_busno;
  69. static int need_domain_info;
  70. LIST_HEAD(resources);
  71. struct pci_bus *bus;
  72. struct pci_host_bridge *bridge;
  73. int ret;
  74. bridge = pci_alloc_host_bridge(0);
  75. if (!bridge)
  76. return;
  77. if (hose->get_busno && pci_has_flag(PCI_PROBE_ONLY))
  78. next_busno = (*hose->get_busno)();
  79. pci_add_resource_offset(&resources,
  80. hose->mem_resource, hose->mem_offset);
  81. pci_add_resource_offset(&resources,
  82. hose->io_resource, hose->io_offset);
  83. pci_add_resource(&resources, hose->busn_resource);
  84. list_splice_init(&resources, &bridge->windows);
  85. bridge->dev.parent = NULL;
  86. bridge->sysdata = hose;
  87. bridge->busnr = next_busno;
  88. bridge->ops = hose->pci_ops;
  89. bridge->swizzle_irq = pci_common_swizzle;
  90. bridge->map_irq = pcibios_map_irq;
  91. ret = pci_scan_root_bus_bridge(bridge);
  92. if (ret) {
  93. pci_free_host_bridge(bridge);
  94. return;
  95. }
  96. hose->bus = bus = bridge->bus;
  97. need_domain_info = need_domain_info || pci_domain_nr(bus);
  98. set_pci_need_domain_info(hose, need_domain_info);
  99. next_busno = bus->busn_res.end + 1;
  100. /* Don't allow 8-bit bus number overflow inside the hose -
  101. reserve some space for bridges. */
  102. if (next_busno > 224) {
  103. next_busno = 0;
  104. need_domain_info = 1;
  105. }
  106. /*
  107. * We insert PCI resources into the iomem_resource and
  108. * ioport_resource trees in either pci_bus_claim_resources()
  109. * or pci_bus_assign_resources().
  110. */
  111. if (pci_has_flag(PCI_PROBE_ONLY)) {
  112. pci_bus_claim_resources(bus);
  113. } else {
  114. struct pci_bus *child;
  115. pci_bus_size_bridges(bus);
  116. pci_bus_assign_resources(bus);
  117. list_for_each_entry(child, &bus->children, node)
  118. pcie_bus_configure_settings(child);
  119. }
  120. pci_bus_add_devices(bus);
  121. }
  122. #ifdef CONFIG_OF
  123. void pci_load_of_ranges(struct pci_controller *hose, struct device_node *node)
  124. {
  125. struct of_pci_range range;
  126. struct of_pci_range_parser parser;
  127. pr_info("PCI host bridge %pOF ranges:\n", node);
  128. hose->of_node = node;
  129. if (of_pci_range_parser_init(&parser, node))
  130. return;
  131. for_each_of_pci_range(&parser, &range) {
  132. struct resource *res = NULL;
  133. switch (range.flags & IORESOURCE_TYPE_BITS) {
  134. case IORESOURCE_IO:
  135. pr_info(" IO 0x%016llx..0x%016llx\n",
  136. range.cpu_addr,
  137. range.cpu_addr + range.size - 1);
  138. hose->io_map_base =
  139. (unsigned long)ioremap(range.cpu_addr,
  140. range.size);
  141. res = hose->io_resource;
  142. break;
  143. case IORESOURCE_MEM:
  144. pr_info(" MEM 0x%016llx..0x%016llx\n",
  145. range.cpu_addr,
  146. range.cpu_addr + range.size - 1);
  147. res = hose->mem_resource;
  148. break;
  149. }
  150. if (res != NULL) {
  151. res->name = node->full_name;
  152. res->flags = range.flags;
  153. res->start = range.cpu_addr;
  154. res->end = range.cpu_addr + range.size - 1;
  155. res->parent = res->child = res->sibling = NULL;
  156. }
  157. }
  158. }
  159. struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
  160. {
  161. struct pci_controller *hose = bus->sysdata;
  162. return of_node_get(hose->of_node);
  163. }
  164. #endif
  165. static DEFINE_MUTEX(pci_scan_mutex);
  166. void register_pci_controller(struct pci_controller *hose)
  167. {
  168. struct resource *parent;
  169. parent = hose->mem_resource->parent;
  170. if (!parent)
  171. parent = &iomem_resource;
  172. if (request_resource(parent, hose->mem_resource) < 0)
  173. goto out;
  174. parent = hose->io_resource->parent;
  175. if (!parent)
  176. parent = &ioport_resource;
  177. if (request_resource(parent, hose->io_resource) < 0) {
  178. release_resource(hose->mem_resource);
  179. goto out;
  180. }
  181. INIT_LIST_HEAD(&hose->list);
  182. list_add_tail(&hose->list, &controllers);
  183. /*
  184. * Do not panic here but later - this might happen before console init.
  185. */
  186. if (!hose->io_map_base) {
  187. printk(KERN_WARNING
  188. "registering PCI controller with io_map_base unset\n");
  189. }
  190. /*
  191. * Scan the bus if it is register after the PCI subsystem
  192. * initialization.
  193. */
  194. if (pci_initialized) {
  195. mutex_lock(&pci_scan_mutex);
  196. pcibios_scanbus(hose);
  197. mutex_unlock(&pci_scan_mutex);
  198. }
  199. return;
  200. out:
  201. printk(KERN_WARNING
  202. "Skipping PCI bus scan due to resource conflict\n");
  203. }
  204. static int __init pcibios_init(void)
  205. {
  206. struct pci_controller *hose;
  207. /* Scan all of the recorded PCI controllers. */
  208. list_for_each_entry(hose, &controllers, list)
  209. pcibios_scanbus(hose);
  210. pci_initialized = 1;
  211. return 0;
  212. }
  213. subsys_initcall(pcibios_init);
  214. static int pcibios_enable_resources(struct pci_dev *dev, int mask)
  215. {
  216. u16 cmd, old_cmd;
  217. int idx;
  218. struct resource *r;
  219. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  220. old_cmd = cmd;
  221. for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
  222. /* Only set up the requested stuff */
  223. if (!(mask & (1<<idx)))
  224. continue;
  225. r = &dev->resource[idx];
  226. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  227. continue;
  228. if ((idx == PCI_ROM_RESOURCE) &&
  229. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  230. continue;
  231. if (!r->start && r->end) {
  232. pci_err(dev,
  233. "can't enable device: resource collisions\n");
  234. return -EINVAL;
  235. }
  236. if (r->flags & IORESOURCE_IO)
  237. cmd |= PCI_COMMAND_IO;
  238. if (r->flags & IORESOURCE_MEM)
  239. cmd |= PCI_COMMAND_MEMORY;
  240. }
  241. if (cmd != old_cmd) {
  242. pci_info(dev, "enabling device (%04x -> %04x)\n", old_cmd, cmd);
  243. pci_write_config_word(dev, PCI_COMMAND, cmd);
  244. }
  245. return 0;
  246. }
  247. int pcibios_enable_device(struct pci_dev *dev, int mask)
  248. {
  249. int err;
  250. if ((err = pcibios_enable_resources(dev, mask)) < 0)
  251. return err;
  252. return pcibios_plat_dev_init(dev);
  253. }
  254. void pcibios_fixup_bus(struct pci_bus *bus)
  255. {
  256. struct pci_dev *dev = bus->self;
  257. if (pci_has_flag(PCI_PROBE_ONLY) && dev &&
  258. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  259. pci_read_bridge_bases(bus);
  260. }
  261. }
  262. char * (*pcibios_plat_setup)(char *str) __initdata;
  263. char *__init pcibios_setup(char *str)
  264. {
  265. if (pcibios_plat_setup)
  266. return pcibios_plat_setup(str);
  267. return str;
  268. }