pci.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. pci_enable_bridges(bus);
  100. }
  101. }
  102. }
  103. #ifdef CONFIG_OF
  104. void pci_load_of_ranges(struct pci_controller *hose, struct device_node *node)
  105. {
  106. const __be32 *ranges;
  107. int rlen;
  108. int pna = of_n_addr_cells(node);
  109. int np = pna + 5;
  110. pr_info("PCI host bridge %s ranges:\n", node->full_name);
  111. ranges = of_get_property(node, "ranges", &rlen);
  112. if (ranges == NULL)
  113. return;
  114. hose->of_node = node;
  115. while ((rlen -= np * 4) >= 0) {
  116. u32 pci_space;
  117. struct resource *res = NULL;
  118. u64 addr, size;
  119. pci_space = be32_to_cpup(&ranges[0]);
  120. addr = of_translate_address(node, ranges + 3);
  121. size = of_read_number(ranges + pna + 3, 2);
  122. ranges += np;
  123. switch ((pci_space >> 24) & 0x3) {
  124. case 1: /* PCI IO space */
  125. pr_info(" IO 0x%016llx..0x%016llx\n",
  126. addr, addr + size - 1);
  127. hose->io_map_base =
  128. (unsigned long)ioremap(addr, size);
  129. res = hose->io_resource;
  130. res->flags = IORESOURCE_IO;
  131. break;
  132. case 2: /* PCI Memory space */
  133. case 3: /* PCI 64 bits Memory space */
  134. pr_info(" MEM 0x%016llx..0x%016llx\n",
  135. addr, addr + size - 1);
  136. res = hose->mem_resource;
  137. res->flags = IORESOURCE_MEM;
  138. break;
  139. }
  140. if (res != NULL) {
  141. res->start = addr;
  142. res->name = node->full_name;
  143. res->end = res->start + size - 1;
  144. res->parent = NULL;
  145. res->sibling = NULL;
  146. res->child = NULL;
  147. }
  148. }
  149. }
  150. struct device_node *pcibios_get_phb_of_node(struct pci_bus *bus)
  151. {
  152. struct pci_controller *hose = bus->sysdata;
  153. return of_node_get(hose->of_node);
  154. }
  155. #endif
  156. static DEFINE_MUTEX(pci_scan_mutex);
  157. void register_pci_controller(struct pci_controller *hose)
  158. {
  159. struct resource *parent;
  160. parent = hose->mem_resource->parent;
  161. if (!parent)
  162. parent = &iomem_resource;
  163. if (request_resource(parent, hose->mem_resource) < 0)
  164. goto out;
  165. parent = hose->io_resource->parent;
  166. if (!parent)
  167. parent = &ioport_resource;
  168. if (request_resource(parent, hose->io_resource) < 0) {
  169. release_resource(hose->mem_resource);
  170. goto out;
  171. }
  172. *hose_tail = hose;
  173. hose_tail = &hose->next;
  174. /*
  175. * Do not panic here but later - this might happen before console init.
  176. */
  177. if (!hose->io_map_base) {
  178. printk(KERN_WARNING
  179. "registering PCI controller with io_map_base unset\n");
  180. }
  181. /*
  182. * Scan the bus if it is register after the PCI subsystem
  183. * initialization.
  184. */
  185. if (pci_initialized) {
  186. mutex_lock(&pci_scan_mutex);
  187. pcibios_scanbus(hose);
  188. mutex_unlock(&pci_scan_mutex);
  189. }
  190. return;
  191. out:
  192. printk(KERN_WARNING
  193. "Skipping PCI bus scan due to resource conflict\n");
  194. }
  195. static void __init pcibios_set_cache_line_size(void)
  196. {
  197. struct cpuinfo_mips *c = &current_cpu_data;
  198. unsigned int lsize;
  199. /*
  200. * Set PCI cacheline size to that of the highest level in the
  201. * cache hierarchy.
  202. */
  203. lsize = c->dcache.linesz;
  204. lsize = c->scache.linesz ? : lsize;
  205. lsize = c->tcache.linesz ? : lsize;
  206. BUG_ON(!lsize);
  207. pci_dfl_cache_line_size = lsize >> 2;
  208. pr_debug("PCI: pci_cache_line_size set to %d bytes\n", lsize);
  209. }
  210. static int __init pcibios_init(void)
  211. {
  212. struct pci_controller *hose;
  213. pcibios_set_cache_line_size();
  214. /* Scan all of the recorded PCI controllers. */
  215. for (hose = hose_head; hose; hose = hose->next)
  216. pcibios_scanbus(hose);
  217. pci_fixup_irqs(pci_common_swizzle, pcibios_map_irq);
  218. pci_initialized = 1;
  219. return 0;
  220. }
  221. subsys_initcall(pcibios_init);
  222. static int pcibios_enable_resources(struct pci_dev *dev, int mask)
  223. {
  224. u16 cmd, old_cmd;
  225. int idx;
  226. struct resource *r;
  227. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  228. old_cmd = cmd;
  229. for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
  230. /* Only set up the requested stuff */
  231. if (!(mask & (1<<idx)))
  232. continue;
  233. r = &dev->resource[idx];
  234. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  235. continue;
  236. if ((idx == PCI_ROM_RESOURCE) &&
  237. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  238. continue;
  239. if (!r->start && r->end) {
  240. printk(KERN_ERR "PCI: Device %s not available "
  241. "because of resource collisions\n",
  242. pci_name(dev));
  243. return -EINVAL;
  244. }
  245. if (r->flags & IORESOURCE_IO)
  246. cmd |= PCI_COMMAND_IO;
  247. if (r->flags & IORESOURCE_MEM)
  248. cmd |= PCI_COMMAND_MEMORY;
  249. }
  250. if (cmd != old_cmd) {
  251. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  252. pci_name(dev), old_cmd, cmd);
  253. pci_write_config_word(dev, PCI_COMMAND, cmd);
  254. }
  255. return 0;
  256. }
  257. unsigned int pcibios_assign_all_busses(void)
  258. {
  259. return 1;
  260. }
  261. int pcibios_enable_device(struct pci_dev *dev, int mask)
  262. {
  263. int err;
  264. if ((err = pcibios_enable_resources(dev, mask)) < 0)
  265. return err;
  266. return pcibios_plat_dev_init(dev);
  267. }
  268. void pcibios_fixup_bus(struct pci_bus *bus)
  269. {
  270. struct pci_dev *dev = bus->self;
  271. if (pci_has_flag(PCI_PROBE_ONLY) && dev &&
  272. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  273. pci_read_bridge_bases(bus);
  274. }
  275. }
  276. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  277. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  278. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  279. enum pci_mmap_state mmap_state, int write_combine)
  280. {
  281. unsigned long prot;
  282. /*
  283. * I/O space can be accessed via normal processor loads and stores on
  284. * this platform but for now we elect not to do this and portable
  285. * drivers should not do this anyway.
  286. */
  287. if (mmap_state == pci_mmap_io)
  288. return -EINVAL;
  289. /*
  290. * Ignore write-combine; for now only return uncached mappings.
  291. */
  292. prot = pgprot_val(vma->vm_page_prot);
  293. prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED;
  294. vma->vm_page_prot = __pgprot(prot);
  295. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  296. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  297. }
  298. char * (*pcibios_plat_setup)(char *str) __initdata;
  299. char *__init pcibios_setup(char *str)
  300. {
  301. if (pcibios_plat_setup)
  302. return pcibios_plat_setup(str);
  303. return str;
  304. }