pci.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * New-style PCI core.
  3. *
  4. * Copyright (c) 2004 - 2009 Paul Mundt
  5. * Copyright (c) 2002 M. R. Brown
  6. *
  7. * Modelled after arch/mips/pci/pci.c:
  8. * Copyright (C) 2003, 04 Ralf Baechle (ralf@linux-mips.org)
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/pci.h>
  17. #include <linux/init.h>
  18. #include <linux/types.h>
  19. #include <linux/dma-debug.h>
  20. #include <linux/io.h>
  21. #include <linux/mutex.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/export.h>
  24. unsigned long PCIBIOS_MIN_IO = 0x0000;
  25. unsigned long PCIBIOS_MIN_MEM = 0;
  26. /*
  27. * The PCI controller list.
  28. */
  29. static struct pci_channel *hose_head, **hose_tail = &hose_head;
  30. static int pci_initialized;
  31. static void __devinit pcibios_scanbus(struct pci_channel *hose)
  32. {
  33. static int next_busno;
  34. static int need_domain_info;
  35. LIST_HEAD(resources);
  36. struct resource *res;
  37. resource_size_t offset;
  38. int i;
  39. struct pci_bus *bus;
  40. for (i = 0; i < hose->nr_resources; i++) {
  41. res = hose->resources + i;
  42. offset = 0;
  43. if (res->flags & IORESOURCE_IO)
  44. offset = hose->io_offset;
  45. else if (res->flags & IORESOURCE_MEM)
  46. offset = hose->mem_offset;
  47. pci_add_resource_offset(&resources, res, offset);
  48. }
  49. bus = pci_scan_root_bus(NULL, next_busno, hose->pci_ops, hose,
  50. &resources);
  51. hose->bus = bus;
  52. need_domain_info = need_domain_info || hose->index;
  53. hose->need_domain_info = need_domain_info;
  54. if (bus) {
  55. next_busno = bus->busn_res.end + 1;
  56. /* Don't allow 8-bit bus number overflow inside the hose -
  57. reserve some space for bridges. */
  58. if (next_busno > 224) {
  59. next_busno = 0;
  60. need_domain_info = 1;
  61. }
  62. pci_bus_size_bridges(bus);
  63. pci_bus_assign_resources(bus);
  64. pci_enable_bridges(bus);
  65. } else {
  66. pci_free_resource_list(&resources);
  67. }
  68. }
  69. /*
  70. * This interrupt-safe spinlock protects all accesses to PCI
  71. * configuration space.
  72. */
  73. DEFINE_RAW_SPINLOCK(pci_config_lock);
  74. static DEFINE_MUTEX(pci_scan_mutex);
  75. int __devinit register_pci_controller(struct pci_channel *hose)
  76. {
  77. int i;
  78. for (i = 0; i < hose->nr_resources; i++) {
  79. struct resource *res = hose->resources + i;
  80. if (res->flags & IORESOURCE_IO) {
  81. if (request_resource(&ioport_resource, res) < 0)
  82. goto out;
  83. } else {
  84. if (request_resource(&iomem_resource, res) < 0)
  85. goto out;
  86. }
  87. }
  88. *hose_tail = hose;
  89. hose_tail = &hose->next;
  90. /*
  91. * Do not panic here but later - this might happen before console init.
  92. */
  93. if (!hose->io_map_base) {
  94. printk(KERN_WARNING
  95. "registering PCI controller with io_map_base unset\n");
  96. }
  97. /*
  98. * Setup the ERR/PERR and SERR timers, if available.
  99. */
  100. pcibios_enable_timers(hose);
  101. /*
  102. * Scan the bus if it is register after the PCI subsystem
  103. * initialization.
  104. */
  105. if (pci_initialized) {
  106. mutex_lock(&pci_scan_mutex);
  107. pcibios_scanbus(hose);
  108. mutex_unlock(&pci_scan_mutex);
  109. }
  110. return 0;
  111. out:
  112. for (--i; i >= 0; i--)
  113. release_resource(&hose->resources[i]);
  114. printk(KERN_WARNING "Skipping PCI bus scan due to resource conflict\n");
  115. return -1;
  116. }
  117. static int __init pcibios_init(void)
  118. {
  119. struct pci_channel *hose;
  120. /* Scan all of the recorded PCI controllers. */
  121. for (hose = hose_head; hose; hose = hose->next)
  122. pcibios_scanbus(hose);
  123. pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
  124. dma_debug_add_bus(&pci_bus_type);
  125. pci_initialized = 1;
  126. return 0;
  127. }
  128. subsys_initcall(pcibios_init);
  129. /*
  130. * Called after each bus is probed, but before its children
  131. * are examined.
  132. */
  133. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  134. {
  135. }
  136. /*
  137. * We need to avoid collisions with `mirrored' VGA ports
  138. * and other strange ISA hardware, so we always want the
  139. * addresses to be allocated in the 0x000-0x0ff region
  140. * modulo 0x400.
  141. */
  142. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  143. resource_size_t size, resource_size_t align)
  144. {
  145. struct pci_dev *dev = data;
  146. struct pci_channel *hose = dev->sysdata;
  147. resource_size_t start = res->start;
  148. if (res->flags & IORESOURCE_IO) {
  149. if (start < PCIBIOS_MIN_IO + hose->resources[0].start)
  150. start = PCIBIOS_MIN_IO + hose->resources[0].start;
  151. /*
  152. * Put everything into 0x00-0xff region modulo 0x400.
  153. */
  154. if (start & 0x300)
  155. start = (start + 0x3ff) & ~0x3ff;
  156. }
  157. return start;
  158. }
  159. int pcibios_enable_device(struct pci_dev *dev, int mask)
  160. {
  161. return pci_enable_resources(dev, mask);
  162. }
  163. void __init pcibios_update_irq(struct pci_dev *dev, int irq)
  164. {
  165. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  166. }
  167. static void __init
  168. pcibios_bus_report_status_early(struct pci_channel *hose,
  169. int top_bus, int current_bus,
  170. unsigned int status_mask, int warn)
  171. {
  172. unsigned int pci_devfn;
  173. u16 status;
  174. int ret;
  175. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
  176. if (PCI_FUNC(pci_devfn))
  177. continue;
  178. ret = early_read_config_word(hose, top_bus, current_bus,
  179. pci_devfn, PCI_STATUS, &status);
  180. if (ret != PCIBIOS_SUCCESSFUL)
  181. continue;
  182. if (status == 0xffff)
  183. continue;
  184. early_write_config_word(hose, top_bus, current_bus,
  185. pci_devfn, PCI_STATUS,
  186. status & status_mask);
  187. if (warn)
  188. printk("(%02x:%02x: %04X) ", current_bus,
  189. pci_devfn, status);
  190. }
  191. }
  192. /*
  193. * We can't use pci_find_device() here since we are
  194. * called from interrupt context.
  195. */
  196. static void __init_refok
  197. pcibios_bus_report_status(struct pci_bus *bus, unsigned int status_mask,
  198. int warn)
  199. {
  200. struct pci_dev *dev;
  201. list_for_each_entry(dev, &bus->devices, bus_list) {
  202. u16 status;
  203. /*
  204. * ignore host bridge - we handle
  205. * that separately
  206. */
  207. if (dev->bus->number == 0 && dev->devfn == 0)
  208. continue;
  209. pci_read_config_word(dev, PCI_STATUS, &status);
  210. if (status == 0xffff)
  211. continue;
  212. if ((status & status_mask) == 0)
  213. continue;
  214. /* clear the status errors */
  215. pci_write_config_word(dev, PCI_STATUS, status & status_mask);
  216. if (warn)
  217. printk("(%s: %04X) ", pci_name(dev), status);
  218. }
  219. list_for_each_entry(dev, &bus->devices, bus_list)
  220. if (dev->subordinate)
  221. pcibios_bus_report_status(dev->subordinate, status_mask, warn);
  222. }
  223. void __init_refok pcibios_report_status(unsigned int status_mask, int warn)
  224. {
  225. struct pci_channel *hose;
  226. for (hose = hose_head; hose; hose = hose->next) {
  227. if (unlikely(!hose->bus))
  228. pcibios_bus_report_status_early(hose, hose_head->index,
  229. hose->index, status_mask, warn);
  230. else
  231. pcibios_bus_report_status(hose->bus, status_mask, warn);
  232. }
  233. }
  234. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  235. enum pci_mmap_state mmap_state, int write_combine)
  236. {
  237. /*
  238. * I/O space can be accessed via normal processor loads and stores on
  239. * this platform but for now we elect not to do this and portable
  240. * drivers should not do this anyway.
  241. */
  242. if (mmap_state == pci_mmap_io)
  243. return -EINVAL;
  244. /*
  245. * Ignore write-combine; for now only return uncached mappings.
  246. */
  247. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  248. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  249. vma->vm_end - vma->vm_start,
  250. vma->vm_page_prot);
  251. }
  252. #ifndef CONFIG_GENERIC_IOMAP
  253. void __iomem *__pci_ioport_map(struct pci_dev *dev,
  254. unsigned long port, unsigned int nr)
  255. {
  256. struct pci_channel *chan = dev->sysdata;
  257. if (unlikely(!chan->io_map_base)) {
  258. chan->io_map_base = sh_io_port_base;
  259. if (pci_domains_supported)
  260. panic("To avoid data corruption io_map_base MUST be "
  261. "set with multiple PCI domains.");
  262. }
  263. return (void __iomem *)(chan->io_map_base + port);
  264. }
  265. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  266. {
  267. iounmap(addr);
  268. }
  269. EXPORT_SYMBOL(pci_iounmap);
  270. #endif /* CONFIG_GENERIC_IOMAP */
  271. #ifdef CONFIG_HOTPLUG
  272. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  273. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  274. #endif