pci.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 struct pci_controller *hose_head, **hose_tail = &hose_head;
  29. unsigned long PCIBIOS_MIN_IO;
  30. unsigned long PCIBIOS_MIN_MEM;
  31. static int pci_initialized;
  32. /*
  33. * We need to avoid collisions with `mirrored' VGA ports
  34. * and other strange ISA hardware, so we always want the
  35. * addresses to be allocated in the 0x000-0x0ff region
  36. * modulo 0x400.
  37. *
  38. * Why? Because some silly external IO cards only decode
  39. * the low 10 bits of the IO address. The 0x00-0xff region
  40. * is reserved for motherboard devices that decode all 16
  41. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  42. * but we want to try to avoid allocating at 0x2900-0x2bff
  43. * which might have be mirrored at 0x0100-0x03ff..
  44. */
  45. resource_size_t
  46. pcibios_align_resource(void *data, const struct resource *res,
  47. resource_size_t size, resource_size_t align)
  48. {
  49. struct pci_dev *dev = data;
  50. struct pci_controller *hose = dev->sysdata;
  51. resource_size_t start = res->start;
  52. if (res->flags & IORESOURCE_IO) {
  53. /* Make sure we start at our min on all hoses */
  54. if (start < PCIBIOS_MIN_IO + hose->io_resource->start)
  55. start = PCIBIOS_MIN_IO + hose->io_resource->start;
  56. /*
  57. * Put everything into 0x00-0xff region modulo 0x400
  58. */
  59. if (start & 0x300)
  60. start = (start + 0x3ff) & ~0x3ff;
  61. } else if (res->flags & IORESOURCE_MEM) {
  62. /* Make sure we start at our min on all hoses */
  63. if (start < PCIBIOS_MIN_MEM + hose->mem_resource->start)
  64. start = PCIBIOS_MIN_MEM + hose->mem_resource->start;
  65. }
  66. return start;
  67. }
  68. static void pcibios_scanbus(struct pci_controller *hose)
  69. {
  70. static int next_busno;
  71. static int need_domain_info;
  72. LIST_HEAD(resources);
  73. struct pci_bus *bus;
  74. if (!hose->iommu)
  75. PCI_DMA_BUS_IS_PHYS = 1;
  76. if (hose->get_busno && pci_has_flag(PCI_PROBE_ONLY))
  77. next_busno = (*hose->get_busno)();
  78. pci_add_resource_offset(&resources,
  79. hose->mem_resource, hose->mem_offset);
  80. pci_add_resource_offset(&resources, hose->io_resource, hose->io_offset);
  81. bus = pci_scan_root_bus(NULL, next_busno, hose->pci_ops, hose,
  82. &resources);
  83. if (!bus)
  84. pci_free_resource_list(&resources);
  85. hose->bus = bus;
  86. need_domain_info = need_domain_info || hose->index;
  87. hose->need_domain_info = need_domain_info;
  88. if (bus) {
  89. next_busno = bus->busn_res.end + 1;
  90. /* Don't allow 8-bit bus number overflow inside the hose -
  91. reserve some space for bridges. */
  92. if (next_busno > 224) {
  93. next_busno = 0;
  94. need_domain_info = 1;
  95. }
  96. if (!pci_has_flag(PCI_PROBE_ONLY)) {
  97. pci_bus_size_bridges(bus);
  98. pci_bus_assign_resources(bus);
  99. }
  100. }
  101. }
  102. #ifdef CONFIG_OF
  103. void pci_load_of_ranges(struct pci_controller *hose, struct device_node *node)
  104. {
  105. const __be32 *ranges;
  106. int rlen;
  107. int pna = of_n_addr_cells(node);
  108. int np = pna + 5;
  109. pr_info("PCI host bridge %s ranges:\n", node->full_name);
  110. ranges = of_get_property(node, "ranges", &rlen);
  111. if (ranges == NULL)
  112. return;
  113. hose->of_node = node;
  114. while ((rlen -= np * 4) >= 0) {
  115. u32 pci_space;
  116. struct resource *res = NULL;
  117. u64 addr, size;
  118. pci_space = be32_to_cpup(&ranges[0]);
  119. addr = of_translate_address(node, ranges + 3);
  120. size = of_read_number(ranges + pna + 3, 2);
  121. ranges += np;
  122. switch ((pci_space >> 24) & 0x3) {
  123. case 1: /* PCI IO space */
  124. pr_info(" IO 0x%016llx..0x%016llx\n",
  125. addr, addr + size - 1);
  126. hose->io_map_base =
  127. (unsigned long)ioremap(addr, size);
  128. res = hose->io_resource;
  129. res->flags = IORESOURCE_IO;
  130. break;
  131. case 2: /* PCI Memory space */
  132. case 3: /* PCI 64 bits Memory space */
  133. pr_info(" MEM 0x%016llx..0x%016llx\n",
  134. addr, addr + size - 1);
  135. res = hose->mem_resource;
  136. res->flags = IORESOURCE_MEM;
  137. break;
  138. }
  139. if (res != NULL) {
  140. res->start = addr;
  141. res->name = node->full_name;
  142. res->end = res->start + size - 1;
  143. res->parent = NULL;
  144. res->sibling = NULL;
  145. res->child = NULL;
  146. }
  147. }
  148. }
  149. struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
  150. {
  151. struct pci_controller *hose = bus->sysdata;
  152. return of_node_get(hose->of_node);
  153. }
  154. #endif
  155. static DEFINE_MUTEX(pci_scan_mutex);
  156. void register_pci_controller(struct pci_controller *hose)
  157. {
  158. struct resource *parent;
  159. parent = hose->mem_resource->parent;
  160. if (!parent)
  161. parent = &iomem_resource;
  162. if (request_resource(parent, hose->mem_resource) < 0)
  163. goto out;
  164. parent = hose->io_resource->parent;
  165. if (!parent)
  166. parent = &ioport_resource;
  167. if (request_resource(parent, hose->io_resource) < 0) {
  168. release_resource(hose->mem_resource);
  169. goto out;
  170. }
  171. *hose_tail = hose;
  172. hose_tail = &hose->next;
  173. /*
  174. * Do not panic here but later - this might happen before console init.
  175. */
  176. if (!hose->io_map_base) {
  177. printk(KERN_WARNING
  178. "registering PCI controller with io_map_base unset\n");
  179. }
  180. /*
  181. * Scan the bus if it is register after the PCI subsystem
  182. * initialization.
  183. */
  184. if (pci_initialized) {
  185. mutex_lock(&pci_scan_mutex);
  186. pcibios_scanbus(hose);
  187. mutex_unlock(&pci_scan_mutex);
  188. }
  189. return;
  190. out:
  191. printk(KERN_WARNING
  192. "Skipping PCI bus scan due to resource conflict\n");
  193. }
  194. static void __init pcibios_set_cache_line_size(void)
  195. {
  196. struct cpuinfo_mips *c = &current_cpu_data;
  197. unsigned int lsize;
  198. /*
  199. * Set PCI cacheline size to that of the highest level in the
  200. * cache hierarchy.
  201. */
  202. lsize = c->dcache.linesz;
  203. lsize = c->scache.linesz ? : lsize;
  204. lsize = c->tcache.linesz ? : lsize;
  205. BUG_ON(!lsize);
  206. pci_dfl_cache_line_size = lsize >> 2;
  207. pr_debug("PCI: pci_cache_line_size set to %d bytes\n", lsize);
  208. }
  209. static int __init pcibios_init(void)
  210. {
  211. struct pci_controller *hose;
  212. pcibios_set_cache_line_size();
  213. /* Scan all of the recorded PCI controllers. */
  214. for (hose = hose_head; hose; hose = hose->next)
  215. pcibios_scanbus(hose);
  216. pci_fixup_irqs(pci_common_swizzle, pcibios_map_irq);
  217. pci_initialized = 1;
  218. return 0;
  219. }
  220. subsys_initcall(pcibios_init);
  221. static int pcibios_enable_resources(struct pci_dev *dev, int mask)
  222. {
  223. u16 cmd, old_cmd;
  224. int idx;
  225. struct resource *r;
  226. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  227. old_cmd = cmd;
  228. for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
  229. /* Only set up the requested stuff */
  230. if (!(mask & (1<<idx)))
  231. continue;
  232. r = &dev->resource[idx];
  233. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  234. continue;
  235. if ((idx == PCI_ROM_RESOURCE) &&
  236. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  237. continue;
  238. if (!r->start && r->end) {
  239. printk(KERN_ERR "PCI: Device %s not available "
  240. "because of resource collisions\n",
  241. pci_name(dev));
  242. return -EINVAL;
  243. }
  244. if (r->flags & IORESOURCE_IO)
  245. cmd |= PCI_COMMAND_IO;
  246. if (r->flags & IORESOURCE_MEM)
  247. cmd |= PCI_COMMAND_MEMORY;
  248. }
  249. if (cmd != old_cmd) {
  250. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  251. pci_name(dev), old_cmd, cmd);
  252. pci_write_config_word(dev, PCI_COMMAND, cmd);
  253. }
  254. return 0;
  255. }
  256. unsigned int pcibios_assign_all_busses(void)
  257. {
  258. return 1;
  259. }
  260. int pcibios_enable_device(struct pci_dev *dev, int mask)
  261. {
  262. int err;
  263. if ((err = pcibios_enable_resources(dev, mask)) < 0)
  264. return err;
  265. return pcibios_plat_dev_init(dev);
  266. }
  267. void pcibios_fixup_bus(struct pci_bus *bus)
  268. {
  269. struct pci_dev *dev = bus->self;
  270. if (pci_has_flag(PCI_PROBE_ONLY) && dev &&
  271. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  272. pci_read_bridge_bases(bus);
  273. }
  274. }
  275. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  276. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  277. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  278. enum pci_mmap_state mmap_state, int write_combine)
  279. {
  280. unsigned long prot;
  281. /*
  282. * I/O space can be accessed via normal processor loads and stores on
  283. * this platform but for now we elect not to do this and portable
  284. * drivers should not do this anyway.
  285. */
  286. if (mmap_state == pci_mmap_io)
  287. return -EINVAL;
  288. /*
  289. * Ignore write-combine; for now only return uncached mappings.
  290. */
  291. prot = pgprot_val(vma->vm_page_prot);
  292. prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED;
  293. vma->vm_page_prot = __pgprot(prot);
  294. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  295. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  296. }
  297. char * (*pcibios_plat_setup)(char *str) __initdata;
  298. char *__init pcibios_setup(char *str)
  299. {
  300. if (pcibios_plat_setup)
  301. return pcibios_plat_setup(str);
  302. return str;
  303. }