pci.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 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. pci_free_resource_list(&resources);
  56. return;
  57. }
  58. next_busno = bus->busn_res.end + 1;
  59. /* Don't allow 8-bit bus number overflow inside the hose -
  60. reserve some space for bridges. */
  61. if (next_busno > 224) {
  62. next_busno = 0;
  63. need_domain_info = 1;
  64. }
  65. pci_bus_size_bridges(bus);
  66. pci_bus_assign_resources(bus);
  67. pci_bus_add_devices(bus);
  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 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 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. static void __init
  160. pcibios_bus_report_status_early(struct pci_channel *hose,
  161. int top_bus, int current_bus,
  162. unsigned int status_mask, int warn)
  163. {
  164. unsigned int pci_devfn;
  165. u16 status;
  166. int ret;
  167. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
  168. if (PCI_FUNC(pci_devfn))
  169. continue;
  170. ret = early_read_config_word(hose, top_bus, current_bus,
  171. pci_devfn, PCI_STATUS, &status);
  172. if (ret != PCIBIOS_SUCCESSFUL)
  173. continue;
  174. if (status == 0xffff)
  175. continue;
  176. early_write_config_word(hose, top_bus, current_bus,
  177. pci_devfn, PCI_STATUS,
  178. status & status_mask);
  179. if (warn)
  180. printk("(%02x:%02x: %04X) ", current_bus,
  181. pci_devfn, status);
  182. }
  183. }
  184. /*
  185. * We can't use pci_find_device() here since we are
  186. * called from interrupt context.
  187. */
  188. static void __init_refok
  189. pcibios_bus_report_status(struct pci_bus *bus, unsigned int status_mask,
  190. int warn)
  191. {
  192. struct pci_dev *dev;
  193. list_for_each_entry(dev, &bus->devices, bus_list) {
  194. u16 status;
  195. /*
  196. * ignore host bridge - we handle
  197. * that separately
  198. */
  199. if (dev->bus->number == 0 && dev->devfn == 0)
  200. continue;
  201. pci_read_config_word(dev, PCI_STATUS, &status);
  202. if (status == 0xffff)
  203. continue;
  204. if ((status & status_mask) == 0)
  205. continue;
  206. /* clear the status errors */
  207. pci_write_config_word(dev, PCI_STATUS, status & status_mask);
  208. if (warn)
  209. printk("(%s: %04X) ", pci_name(dev), status);
  210. }
  211. list_for_each_entry(dev, &bus->devices, bus_list)
  212. if (dev->subordinate)
  213. pcibios_bus_report_status(dev->subordinate, status_mask, warn);
  214. }
  215. void __init_refok pcibios_report_status(unsigned int status_mask, int warn)
  216. {
  217. struct pci_channel *hose;
  218. for (hose = hose_head; hose; hose = hose->next) {
  219. if (unlikely(!hose->bus))
  220. pcibios_bus_report_status_early(hose, hose_head->index,
  221. hose->index, status_mask, warn);
  222. else
  223. pcibios_bus_report_status(hose->bus, status_mask, warn);
  224. }
  225. }
  226. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  227. enum pci_mmap_state mmap_state, int write_combine)
  228. {
  229. /*
  230. * I/O space can be accessed via normal processor loads and stores on
  231. * this platform but for now we elect not to do this and portable
  232. * drivers should not do this anyway.
  233. */
  234. if (mmap_state == pci_mmap_io)
  235. return -EINVAL;
  236. /*
  237. * Ignore write-combine; for now only return uncached mappings.
  238. */
  239. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  240. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  241. vma->vm_end - vma->vm_start,
  242. vma->vm_page_prot);
  243. }
  244. #ifndef CONFIG_GENERIC_IOMAP
  245. void __iomem *__pci_ioport_map(struct pci_dev *dev,
  246. unsigned long port, unsigned int nr)
  247. {
  248. struct pci_channel *chan = dev->sysdata;
  249. if (unlikely(!chan->io_map_base)) {
  250. chan->io_map_base = sh_io_port_base;
  251. if (pci_domains_supported)
  252. panic("To avoid data corruption io_map_base MUST be "
  253. "set with multiple PCI domains.");
  254. }
  255. return (void __iomem *)(chan->io_map_base + port);
  256. }
  257. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  258. {
  259. iounmap(addr);
  260. }
  261. EXPORT_SYMBOL(pci_iounmap);
  262. #endif /* CONFIG_GENERIC_IOMAP */
  263. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  264. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);