pci.c 9.1 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 Ralf Baechle (ralf@linux-mips.org)
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/bootmem.h>
  12. #include <linux/export.h>
  13. #include <linux/init.h>
  14. #include <linux/types.h>
  15. #include <linux/pci.h>
  16. /*
  17. * Indicate whether we respect the PCI setup left by the firmware.
  18. *
  19. * Make this long-lived so that we know when shutting down
  20. * whether we probed only or not.
  21. */
  22. int pci_probe_only;
  23. #define PCI_ASSIGN_ALL_BUSSES 1
  24. unsigned int pci_probe = PCI_ASSIGN_ALL_BUSSES;
  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 __devinit pcibios_scanbus(struct pci_controller *hose)
  69. {
  70. static int next_busno;
  71. static int need_domain_info;
  72. struct pci_bus *bus;
  73. if (!hose->iommu)
  74. PCI_DMA_BUS_IS_PHYS = 1;
  75. if (hose->get_busno && pci_probe_only)
  76. next_busno = (*hose->get_busno)();
  77. bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
  78. hose->bus = bus;
  79. need_domain_info = need_domain_info || hose->index;
  80. hose->need_domain_info = need_domain_info;
  81. if (bus) {
  82. next_busno = bus->subordinate + 1;
  83. /* Don't allow 8-bit bus number overflow inside the hose -
  84. reserve some space for bridges. */
  85. if (next_busno > 224) {
  86. next_busno = 0;
  87. need_domain_info = 1;
  88. }
  89. if (!pci_probe_only) {
  90. pci_bus_size_bridges(bus);
  91. pci_bus_assign_resources(bus);
  92. pci_enable_bridges(bus);
  93. }
  94. }
  95. }
  96. static DEFINE_MUTEX(pci_scan_mutex);
  97. void __devinit register_pci_controller(struct pci_controller *hose)
  98. {
  99. if (request_resource(&iomem_resource, hose->mem_resource) < 0)
  100. goto out;
  101. if (request_resource(&ioport_resource, hose->io_resource) < 0) {
  102. release_resource(hose->mem_resource);
  103. goto out;
  104. }
  105. *hose_tail = hose;
  106. hose_tail = &hose->next;
  107. /*
  108. * Do not panic here but later - this might happen before console init.
  109. */
  110. if (!hose->io_map_base) {
  111. printk(KERN_WARNING
  112. "registering PCI controller with io_map_base unset\n");
  113. }
  114. /*
  115. * Scan the bus if it is register after the PCI subsystem
  116. * initialization.
  117. */
  118. if (pci_initialized) {
  119. mutex_lock(&pci_scan_mutex);
  120. pcibios_scanbus(hose);
  121. mutex_unlock(&pci_scan_mutex);
  122. }
  123. return;
  124. out:
  125. printk(KERN_WARNING
  126. "Skipping PCI bus scan due to resource conflict\n");
  127. }
  128. static int __init pcibios_init(void)
  129. {
  130. struct pci_controller *hose;
  131. /* Scan all of the recorded PCI controllers. */
  132. for (hose = hose_head; hose; hose = hose->next)
  133. pcibios_scanbus(hose);
  134. pci_fixup_irqs(pci_common_swizzle, pcibios_map_irq);
  135. pci_initialized = 1;
  136. return 0;
  137. }
  138. subsys_initcall(pcibios_init);
  139. static int pcibios_enable_resources(struct pci_dev *dev, int mask)
  140. {
  141. u16 cmd, old_cmd;
  142. int idx;
  143. struct resource *r;
  144. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  145. old_cmd = cmd;
  146. for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
  147. /* Only set up the requested stuff */
  148. if (!(mask & (1<<idx)))
  149. continue;
  150. r = &dev->resource[idx];
  151. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  152. continue;
  153. if ((idx == PCI_ROM_RESOURCE) &&
  154. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  155. continue;
  156. if (!r->start && r->end) {
  157. printk(KERN_ERR "PCI: Device %s not available "
  158. "because of resource collisions\n",
  159. pci_name(dev));
  160. return -EINVAL;
  161. }
  162. if (r->flags & IORESOURCE_IO)
  163. cmd |= PCI_COMMAND_IO;
  164. if (r->flags & IORESOURCE_MEM)
  165. cmd |= PCI_COMMAND_MEMORY;
  166. }
  167. if (cmd != old_cmd) {
  168. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  169. pci_name(dev), old_cmd, cmd);
  170. pci_write_config_word(dev, PCI_COMMAND, cmd);
  171. }
  172. return 0;
  173. }
  174. /*
  175. * If we set up a device for bus mastering, we need to check the latency
  176. * timer as certain crappy BIOSes forget to set it properly.
  177. */
  178. static unsigned int pcibios_max_latency = 255;
  179. void pcibios_set_master(struct pci_dev *dev)
  180. {
  181. u8 lat;
  182. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  183. if (lat < 16)
  184. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  185. else if (lat > pcibios_max_latency)
  186. lat = pcibios_max_latency;
  187. else
  188. return;
  189. printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n",
  190. pci_name(dev), lat);
  191. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  192. }
  193. unsigned int pcibios_assign_all_busses(void)
  194. {
  195. return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
  196. }
  197. int pcibios_enable_device(struct pci_dev *dev, int mask)
  198. {
  199. int err;
  200. if ((err = pcibios_enable_resources(dev, mask)) < 0)
  201. return err;
  202. return pcibios_plat_dev_init(dev);
  203. }
  204. static void pcibios_fixup_device_resources(struct pci_dev *dev,
  205. struct pci_bus *bus)
  206. {
  207. /* Update device resources. */
  208. struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
  209. unsigned long offset = 0;
  210. int i;
  211. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  212. if (!dev->resource[i].start)
  213. continue;
  214. if (dev->resource[i].flags & IORESOURCE_IO)
  215. offset = hose->io_offset;
  216. else if (dev->resource[i].flags & IORESOURCE_MEM)
  217. offset = hose->mem_offset;
  218. dev->resource[i].start += offset;
  219. dev->resource[i].end += offset;
  220. }
  221. }
  222. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  223. {
  224. /* Propagate hose info into the subordinate devices. */
  225. struct pci_controller *hose = bus->sysdata;
  226. struct list_head *ln;
  227. struct pci_dev *dev = bus->self;
  228. if (!dev) {
  229. bus->resource[0] = hose->io_resource;
  230. bus->resource[1] = hose->mem_resource;
  231. } else if (pci_probe_only &&
  232. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  233. pci_read_bridge_bases(bus);
  234. pcibios_fixup_device_resources(dev, bus);
  235. }
  236. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  237. dev = pci_dev_b(ln);
  238. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  239. pcibios_fixup_device_resources(dev, bus);
  240. }
  241. }
  242. void __init
  243. pcibios_update_irq(struct pci_dev *dev, int irq)
  244. {
  245. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  246. }
  247. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  248. struct resource *res)
  249. {
  250. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  251. unsigned long offset = 0;
  252. if (res->flags & IORESOURCE_IO)
  253. offset = hose->io_offset;
  254. else if (res->flags & IORESOURCE_MEM)
  255. offset = hose->mem_offset;
  256. region->start = res->start - offset;
  257. region->end = res->end - offset;
  258. }
  259. void __devinit
  260. pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  261. struct pci_bus_region *region)
  262. {
  263. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  264. unsigned long offset = 0;
  265. if (res->flags & IORESOURCE_IO)
  266. offset = hose->io_offset;
  267. else if (res->flags & IORESOURCE_MEM)
  268. offset = hose->mem_offset;
  269. res->start = region->start + offset;
  270. res->end = region->end + offset;
  271. }
  272. #ifdef CONFIG_HOTPLUG
  273. EXPORT_SYMBOL(pcibios_resource_to_bus);
  274. EXPORT_SYMBOL(pcibios_bus_to_resource);
  275. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  276. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  277. #endif
  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) __devinitdata;
  299. char *__devinit pcibios_setup(char *str)
  300. {
  301. if (pcibios_plat_setup)
  302. return pcibios_plat_setup(str);
  303. return str;
  304. }