pci.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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, ret;
  39. struct pci_host_bridge *bridge;
  40. bridge = pci_alloc_host_bridge(0);
  41. if (!bridge)
  42. return;
  43. for (i = 0; i < hose->nr_resources; i++) {
  44. res = hose->resources + i;
  45. offset = 0;
  46. if (res->flags & IORESOURCE_DISABLED)
  47. continue;
  48. if (res->flags & IORESOURCE_IO)
  49. offset = hose->io_offset;
  50. else if (res->flags & IORESOURCE_MEM)
  51. offset = hose->mem_offset;
  52. pci_add_resource_offset(&resources, res, offset);
  53. }
  54. list_splice_init(&resources, &bridge->windows);
  55. bridge->dev.parent = NULL;
  56. bridge->sysdata = hose;
  57. bridge->busnr = next_busno;
  58. bridge->ops = hose->pci_ops;
  59. bridge->swizzle_irq = pci_common_swizzle;
  60. bridge->map_irq = pcibios_map_platform_irq;
  61. ret = pci_scan_root_bus_bridge(bridge);
  62. if (ret) {
  63. pci_free_host_bridge(bridge);
  64. return;
  65. }
  66. hose->bus = bridge->bus;
  67. need_domain_info = need_domain_info || hose->index;
  68. hose->need_domain_info = need_domain_info;
  69. next_busno = hose->bus->busn_res.end + 1;
  70. /* Don't allow 8-bit bus number overflow inside the hose -
  71. reserve some space for bridges. */
  72. if (next_busno > 224) {
  73. next_busno = 0;
  74. need_domain_info = 1;
  75. }
  76. pci_bus_size_bridges(hose->bus);
  77. pci_bus_assign_resources(hose->bus);
  78. pci_bus_add_devices(hose->bus);
  79. }
  80. /*
  81. * This interrupt-safe spinlock protects all accesses to PCI
  82. * configuration space.
  83. */
  84. DEFINE_RAW_SPINLOCK(pci_config_lock);
  85. static DEFINE_MUTEX(pci_scan_mutex);
  86. int register_pci_controller(struct pci_channel *hose)
  87. {
  88. int i;
  89. for (i = 0; i < hose->nr_resources; i++) {
  90. struct resource *res = hose->resources + i;
  91. if (res->flags & IORESOURCE_DISABLED)
  92. continue;
  93. if (res->flags & IORESOURCE_IO) {
  94. if (request_resource(&ioport_resource, res) < 0)
  95. goto out;
  96. } else {
  97. if (request_resource(&iomem_resource, res) < 0)
  98. goto out;
  99. }
  100. }
  101. *hose_tail = hose;
  102. hose_tail = &hose->next;
  103. /*
  104. * Do not panic here but later - this might happen before console init.
  105. */
  106. if (!hose->io_map_base) {
  107. printk(KERN_WARNING
  108. "registering PCI controller with io_map_base unset\n");
  109. }
  110. /*
  111. * Setup the ERR/PERR and SERR timers, if available.
  112. */
  113. pcibios_enable_timers(hose);
  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 0;
  124. out:
  125. for (--i; i >= 0; i--)
  126. release_resource(&hose->resources[i]);
  127. printk(KERN_WARNING "Skipping PCI bus scan due to resource conflict\n");
  128. return -1;
  129. }
  130. static int __init pcibios_init(void)
  131. {
  132. struct pci_channel *hose;
  133. /* Scan all of the recorded PCI controllers. */
  134. for (hose = hose_head; hose; hose = hose->next)
  135. pcibios_scanbus(hose);
  136. pci_initialized = 1;
  137. return 0;
  138. }
  139. subsys_initcall(pcibios_init);
  140. /*
  141. * We need to avoid collisions with `mirrored' VGA ports
  142. * and other strange ISA hardware, so we always want the
  143. * addresses to be allocated in the 0x000-0x0ff region
  144. * modulo 0x400.
  145. */
  146. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  147. resource_size_t size, resource_size_t align)
  148. {
  149. struct pci_dev *dev = data;
  150. struct pci_channel *hose = dev->sysdata;
  151. resource_size_t start = res->start;
  152. if (res->flags & IORESOURCE_IO) {
  153. if (start < PCIBIOS_MIN_IO + hose->resources[0].start)
  154. start = PCIBIOS_MIN_IO + hose->resources[0].start;
  155. /*
  156. * Put everything into 0x00-0xff region modulo 0x400.
  157. */
  158. if (start & 0x300)
  159. start = (start + 0x3ff) & ~0x3ff;
  160. }
  161. return start;
  162. }
  163. static void __init
  164. pcibios_bus_report_status_early(struct pci_channel *hose,
  165. int top_bus, int current_bus,
  166. unsigned int status_mask, int warn)
  167. {
  168. unsigned int pci_devfn;
  169. u16 status;
  170. int ret;
  171. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
  172. if (PCI_FUNC(pci_devfn))
  173. continue;
  174. ret = early_read_config_word(hose, top_bus, current_bus,
  175. pci_devfn, PCI_STATUS, &status);
  176. if (ret != PCIBIOS_SUCCESSFUL)
  177. continue;
  178. if (status == 0xffff)
  179. continue;
  180. early_write_config_word(hose, top_bus, current_bus,
  181. pci_devfn, PCI_STATUS,
  182. status & status_mask);
  183. if (warn)
  184. printk("(%02x:%02x: %04X) ", current_bus,
  185. pci_devfn, status);
  186. }
  187. }
  188. /*
  189. * We can't use pci_find_device() here since we are
  190. * called from interrupt context.
  191. */
  192. static void __ref
  193. pcibios_bus_report_status(struct pci_bus *bus, unsigned int status_mask,
  194. int warn)
  195. {
  196. struct pci_dev *dev;
  197. list_for_each_entry(dev, &bus->devices, bus_list) {
  198. u16 status;
  199. /*
  200. * ignore host bridge - we handle
  201. * that separately
  202. */
  203. if (dev->bus->number == 0 && dev->devfn == 0)
  204. continue;
  205. pci_read_config_word(dev, PCI_STATUS, &status);
  206. if (status == 0xffff)
  207. continue;
  208. if ((status & status_mask) == 0)
  209. continue;
  210. /* clear the status errors */
  211. pci_write_config_word(dev, PCI_STATUS, status & status_mask);
  212. if (warn)
  213. printk("(%s: %04X) ", pci_name(dev), status);
  214. }
  215. list_for_each_entry(dev, &bus->devices, bus_list)
  216. if (dev->subordinate)
  217. pcibios_bus_report_status(dev->subordinate, status_mask, warn);
  218. }
  219. void __ref pcibios_report_status(unsigned int status_mask, int warn)
  220. {
  221. struct pci_channel *hose;
  222. for (hose = hose_head; hose; hose = hose->next) {
  223. if (unlikely(!hose->bus))
  224. pcibios_bus_report_status_early(hose, hose_head->index,
  225. hose->index, status_mask, warn);
  226. else
  227. pcibios_bus_report_status(hose->bus, status_mask, warn);
  228. }
  229. }
  230. #ifndef CONFIG_GENERIC_IOMAP
  231. void __iomem *__pci_ioport_map(struct pci_dev *dev,
  232. unsigned long port, unsigned int nr)
  233. {
  234. struct pci_channel *chan = dev->sysdata;
  235. if (unlikely(!chan->io_map_base)) {
  236. chan->io_map_base = sh_io_port_base;
  237. if (pci_domains_supported)
  238. panic("To avoid data corruption io_map_base MUST be "
  239. "set with multiple PCI domains.");
  240. }
  241. return (void __iomem *)(chan->io_map_base + port);
  242. }
  243. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  244. {
  245. iounmap(addr);
  246. }
  247. EXPORT_SYMBOL(pci_iounmap);
  248. #endif /* CONFIG_GENERIC_IOMAP */
  249. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  250. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);