pci.c 7.4 KB

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