pci.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. 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. } else {
  65. pci_free_resource_list(&resources);
  66. }
  67. }
  68. /*
  69. * This interrupt-safe spinlock protects all accesses to PCI
  70. * configuration space.
  71. */
  72. DEFINE_RAW_SPINLOCK(pci_config_lock);
  73. static DEFINE_MUTEX(pci_scan_mutex);
  74. int register_pci_controller(struct pci_channel *hose)
  75. {
  76. int i;
  77. for (i = 0; i < hose->nr_resources; i++) {
  78. struct resource *res = hose->resources + i;
  79. if (res->flags & IORESOURCE_IO) {
  80. if (request_resource(&ioport_resource, res) < 0)
  81. goto out;
  82. } else {
  83. if (request_resource(&iomem_resource, res) < 0)
  84. goto out;
  85. }
  86. }
  87. *hose_tail = hose;
  88. hose_tail = &hose->next;
  89. /*
  90. * Do not panic here but later - this might happen before console init.
  91. */
  92. if (!hose->io_map_base) {
  93. printk(KERN_WARNING
  94. "registering PCI controller with io_map_base unset\n");
  95. }
  96. /*
  97. * Setup the ERR/PERR and SERR timers, if available.
  98. */
  99. pcibios_enable_timers(hose);
  100. /*
  101. * Scan the bus if it is register after the PCI subsystem
  102. * initialization.
  103. */
  104. if (pci_initialized) {
  105. mutex_lock(&pci_scan_mutex);
  106. pcibios_scanbus(hose);
  107. mutex_unlock(&pci_scan_mutex);
  108. }
  109. return 0;
  110. out:
  111. for (--i; i >= 0; i--)
  112. release_resource(&hose->resources[i]);
  113. printk(KERN_WARNING "Skipping PCI bus scan due to resource conflict\n");
  114. return -1;
  115. }
  116. static int __init pcibios_init(void)
  117. {
  118. struct pci_channel *hose;
  119. /* Scan all of the recorded PCI controllers. */
  120. for (hose = hose_head; hose; hose = hose->next)
  121. pcibios_scanbus(hose);
  122. pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
  123. dma_debug_add_bus(&pci_bus_type);
  124. pci_initialized = 1;
  125. return 0;
  126. }
  127. subsys_initcall(pcibios_init);
  128. /*
  129. * Called after each bus is probed, but before its children
  130. * are examined.
  131. */
  132. void pcibios_fixup_bus(struct pci_bus *bus)
  133. {
  134. }
  135. /*
  136. * We need to avoid collisions with `mirrored' VGA ports
  137. * and other strange ISA hardware, so we always want the
  138. * addresses to be allocated in the 0x000-0x0ff region
  139. * modulo 0x400.
  140. */
  141. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  142. resource_size_t size, resource_size_t align)
  143. {
  144. struct pci_dev *dev = data;
  145. struct pci_channel *hose = dev->sysdata;
  146. resource_size_t start = res->start;
  147. if (res->flags & IORESOURCE_IO) {
  148. if (start < PCIBIOS_MIN_IO + hose->resources[0].start)
  149. start = PCIBIOS_MIN_IO + hose->resources[0].start;
  150. /*
  151. * Put everything into 0x00-0xff region modulo 0x400.
  152. */
  153. if (start & 0x300)
  154. start = (start + 0x3ff) & ~0x3ff;
  155. }
  156. return start;
  157. }
  158. int pcibios_enable_device(struct pci_dev *dev, int mask)
  159. {
  160. return pci_enable_resources(dev, mask);
  161. }
  162. static void __init
  163. pcibios_bus_report_status_early(struct pci_channel *hose,
  164. int top_bus, int current_bus,
  165. unsigned int status_mask, int warn)
  166. {
  167. unsigned int pci_devfn;
  168. u16 status;
  169. int ret;
  170. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
  171. if (PCI_FUNC(pci_devfn))
  172. continue;
  173. ret = early_read_config_word(hose, top_bus, current_bus,
  174. pci_devfn, PCI_STATUS, &status);
  175. if (ret != PCIBIOS_SUCCESSFUL)
  176. continue;
  177. if (status == 0xffff)
  178. continue;
  179. early_write_config_word(hose, top_bus, current_bus,
  180. pci_devfn, PCI_STATUS,
  181. status & status_mask);
  182. if (warn)
  183. printk("(%02x:%02x: %04X) ", current_bus,
  184. pci_devfn, status);
  185. }
  186. }
  187. /*
  188. * We can't use pci_find_device() here since we are
  189. * called from interrupt context.
  190. */
  191. static void __init_refok
  192. pcibios_bus_report_status(struct pci_bus *bus, unsigned int status_mask,
  193. int warn)
  194. {
  195. struct pci_dev *dev;
  196. list_for_each_entry(dev, &bus->devices, bus_list) {
  197. u16 status;
  198. /*
  199. * ignore host bridge - we handle
  200. * that separately
  201. */
  202. if (dev->bus->number == 0 && dev->devfn == 0)
  203. continue;
  204. pci_read_config_word(dev, PCI_STATUS, &status);
  205. if (status == 0xffff)
  206. continue;
  207. if ((status & status_mask) == 0)
  208. continue;
  209. /* clear the status errors */
  210. pci_write_config_word(dev, PCI_STATUS, status & status_mask);
  211. if (warn)
  212. printk("(%s: %04X) ", pci_name(dev), status);
  213. }
  214. list_for_each_entry(dev, &bus->devices, bus_list)
  215. if (dev->subordinate)
  216. pcibios_bus_report_status(dev->subordinate, status_mask, warn);
  217. }
  218. void __init_refok pcibios_report_status(unsigned int status_mask, int warn)
  219. {
  220. struct pci_channel *hose;
  221. for (hose = hose_head; hose; hose = hose->next) {
  222. if (unlikely(!hose->bus))
  223. pcibios_bus_report_status_early(hose, hose_head->index,
  224. hose->index, status_mask, warn);
  225. else
  226. pcibios_bus_report_status(hose->bus, status_mask, warn);
  227. }
  228. }
  229. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  230. enum pci_mmap_state mmap_state, int write_combine)
  231. {
  232. /*
  233. * I/O space can be accessed via normal processor loads and stores on
  234. * this platform but for now we elect not to do this and portable
  235. * drivers should not do this anyway.
  236. */
  237. if (mmap_state == pci_mmap_io)
  238. return -EINVAL;
  239. /*
  240. * Ignore write-combine; for now only return uncached mappings.
  241. */
  242. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  243. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  244. vma->vm_end - vma->vm_start,
  245. vma->vm_page_prot);
  246. }
  247. #ifndef CONFIG_GENERIC_IOMAP
  248. void __iomem *__pci_ioport_map(struct pci_dev *dev,
  249. unsigned long port, unsigned int nr)
  250. {
  251. struct pci_channel *chan = dev->sysdata;
  252. if (unlikely(!chan->io_map_base)) {
  253. chan->io_map_base = sh_io_port_base;
  254. if (pci_domains_supported)
  255. panic("To avoid data corruption io_map_base MUST be "
  256. "set with multiple PCI domains.");
  257. }
  258. return (void __iomem *)(chan->io_map_base + port);
  259. }
  260. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  261. {
  262. iounmap(addr);
  263. }
  264. EXPORT_SYMBOL(pci_iounmap);
  265. #endif /* CONFIG_GENERIC_IOMAP */
  266. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  267. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);