pci.c 9.1 KB

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