pci.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. int i;
  37. struct pci_bus *bus;
  38. for (i = 0; i < hose->nr_resources; i++)
  39. pci_add_resource(&resources, hose->resources + i);
  40. bus = pci_scan_root_bus(NULL, next_busno, hose->pci_ops, hose,
  41. &resources);
  42. hose->bus = bus;
  43. need_domain_info = need_domain_info || hose->index;
  44. hose->need_domain_info = need_domain_info;
  45. if (bus) {
  46. next_busno = bus->subordinate + 1;
  47. /* Don't allow 8-bit bus number overflow inside the hose -
  48. reserve some space for bridges. */
  49. if (next_busno > 224) {
  50. next_busno = 0;
  51. need_domain_info = 1;
  52. }
  53. pci_bus_size_bridges(bus);
  54. pci_bus_assign_resources(bus);
  55. pci_enable_bridges(bus);
  56. } else {
  57. pci_free_resource_list(&resources);
  58. }
  59. }
  60. /*
  61. * This interrupt-safe spinlock protects all accesses to PCI
  62. * configuration space.
  63. */
  64. DEFINE_RAW_SPINLOCK(pci_config_lock);
  65. static DEFINE_MUTEX(pci_scan_mutex);
  66. int __devinit register_pci_controller(struct pci_channel *hose)
  67. {
  68. int i;
  69. for (i = 0; i < hose->nr_resources; i++) {
  70. struct resource *res = hose->resources + i;
  71. if (res->flags & IORESOURCE_IO) {
  72. if (request_resource(&ioport_resource, res) < 0)
  73. goto out;
  74. } else {
  75. if (request_resource(&iomem_resource, res) < 0)
  76. goto out;
  77. }
  78. }
  79. *hose_tail = hose;
  80. hose_tail = &hose->next;
  81. /*
  82. * Do not panic here but later - this might happen before console init.
  83. */
  84. if (!hose->io_map_base) {
  85. printk(KERN_WARNING
  86. "registering PCI controller with io_map_base unset\n");
  87. }
  88. /*
  89. * Setup the ERR/PERR and SERR timers, if available.
  90. */
  91. pcibios_enable_timers(hose);
  92. /*
  93. * Scan the bus if it is register after the PCI subsystem
  94. * initialization.
  95. */
  96. if (pci_initialized) {
  97. mutex_lock(&pci_scan_mutex);
  98. pcibios_scanbus(hose);
  99. mutex_unlock(&pci_scan_mutex);
  100. }
  101. return 0;
  102. out:
  103. for (--i; i >= 0; i--)
  104. release_resource(&hose->resources[i]);
  105. printk(KERN_WARNING "Skipping PCI bus scan due to resource conflict\n");
  106. return -1;
  107. }
  108. static int __init pcibios_init(void)
  109. {
  110. struct pci_channel *hose;
  111. /* Scan all of the recorded PCI controllers. */
  112. for (hose = hose_head; hose; hose = hose->next)
  113. pcibios_scanbus(hose);
  114. pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
  115. dma_debug_add_bus(&pci_bus_type);
  116. pci_initialized = 1;
  117. return 0;
  118. }
  119. subsys_initcall(pcibios_init);
  120. static void pcibios_fixup_device_resources(struct pci_dev *dev,
  121. struct pci_bus *bus)
  122. {
  123. /* Update device resources. */
  124. struct pci_channel *hose = bus->sysdata;
  125. unsigned long offset = 0;
  126. int i;
  127. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  128. if (!dev->resource[i].start)
  129. continue;
  130. if (dev->resource[i].flags & IORESOURCE_IO)
  131. offset = hose->io_offset;
  132. else if (dev->resource[i].flags & IORESOURCE_MEM)
  133. offset = hose->mem_offset;
  134. dev->resource[i].start += offset;
  135. dev->resource[i].end += offset;
  136. }
  137. }
  138. /*
  139. * Called after each bus is probed, but before its children
  140. * are examined.
  141. */
  142. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  143. {
  144. struct pci_dev *dev;
  145. struct list_head *ln;
  146. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  147. dev = pci_dev_b(ln);
  148. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  149. pcibios_fixup_device_resources(dev, bus);
  150. }
  151. }
  152. /*
  153. * We need to avoid collisions with `mirrored' VGA ports
  154. * and other strange ISA hardware, so we always want the
  155. * addresses to be allocated in the 0x000-0x0ff region
  156. * modulo 0x400.
  157. */
  158. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  159. resource_size_t size, resource_size_t align)
  160. {
  161. struct pci_dev *dev = data;
  162. struct pci_channel *hose = dev->sysdata;
  163. resource_size_t start = res->start;
  164. if (res->flags & IORESOURCE_IO) {
  165. if (start < PCIBIOS_MIN_IO + hose->resources[0].start)
  166. start = PCIBIOS_MIN_IO + hose->resources[0].start;
  167. /*
  168. * Put everything into 0x00-0xff region modulo 0x400.
  169. */
  170. if (start & 0x300)
  171. start = (start + 0x3ff) & ~0x3ff;
  172. }
  173. return start;
  174. }
  175. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  176. struct resource *res)
  177. {
  178. struct pci_channel *hose = dev->sysdata;
  179. unsigned long offset = 0;
  180. if (res->flags & IORESOURCE_IO)
  181. offset = hose->io_offset;
  182. else if (res->flags & IORESOURCE_MEM)
  183. offset = hose->mem_offset;
  184. region->start = res->start - offset;
  185. region->end = res->end - offset;
  186. }
  187. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  188. struct pci_bus_region *region)
  189. {
  190. struct pci_channel *hose = dev->sysdata;
  191. unsigned long offset = 0;
  192. if (res->flags & IORESOURCE_IO)
  193. offset = hose->io_offset;
  194. else if (res->flags & IORESOURCE_MEM)
  195. offset = hose->mem_offset;
  196. res->start = region->start + offset;
  197. res->end = region->end + offset;
  198. }
  199. int pcibios_enable_device(struct pci_dev *dev, int mask)
  200. {
  201. return pci_enable_resources(dev, mask);
  202. }
  203. void __init pcibios_update_irq(struct pci_dev *dev, int irq)
  204. {
  205. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  206. }
  207. char * __devinit __weak pcibios_setup(char *str)
  208. {
  209. return str;
  210. }
  211. static void __init
  212. pcibios_bus_report_status_early(struct pci_channel *hose,
  213. int top_bus, int current_bus,
  214. unsigned int status_mask, int warn)
  215. {
  216. unsigned int pci_devfn;
  217. u16 status;
  218. int ret;
  219. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
  220. if (PCI_FUNC(pci_devfn))
  221. continue;
  222. ret = early_read_config_word(hose, top_bus, current_bus,
  223. pci_devfn, PCI_STATUS, &status);
  224. if (ret != PCIBIOS_SUCCESSFUL)
  225. continue;
  226. if (status == 0xffff)
  227. continue;
  228. early_write_config_word(hose, top_bus, current_bus,
  229. pci_devfn, PCI_STATUS,
  230. status & status_mask);
  231. if (warn)
  232. printk("(%02x:%02x: %04X) ", current_bus,
  233. pci_devfn, status);
  234. }
  235. }
  236. /*
  237. * We can't use pci_find_device() here since we are
  238. * called from interrupt context.
  239. */
  240. static void __init_refok
  241. pcibios_bus_report_status(struct pci_bus *bus, unsigned int status_mask,
  242. int warn)
  243. {
  244. struct pci_dev *dev;
  245. list_for_each_entry(dev, &bus->devices, bus_list) {
  246. u16 status;
  247. /*
  248. * ignore host bridge - we handle
  249. * that separately
  250. */
  251. if (dev->bus->number == 0 && dev->devfn == 0)
  252. continue;
  253. pci_read_config_word(dev, PCI_STATUS, &status);
  254. if (status == 0xffff)
  255. continue;
  256. if ((status & status_mask) == 0)
  257. continue;
  258. /* clear the status errors */
  259. pci_write_config_word(dev, PCI_STATUS, status & status_mask);
  260. if (warn)
  261. printk("(%s: %04X) ", pci_name(dev), status);
  262. }
  263. list_for_each_entry(dev, &bus->devices, bus_list)
  264. if (dev->subordinate)
  265. pcibios_bus_report_status(dev->subordinate, status_mask, warn);
  266. }
  267. void __init_refok pcibios_report_status(unsigned int status_mask, int warn)
  268. {
  269. struct pci_channel *hose;
  270. for (hose = hose_head; hose; hose = hose->next) {
  271. if (unlikely(!hose->bus))
  272. pcibios_bus_report_status_early(hose, hose_head->index,
  273. hose->index, status_mask, warn);
  274. else
  275. pcibios_bus_report_status(hose->bus, status_mask, warn);
  276. }
  277. }
  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. /*
  282. * I/O space can be accessed via normal processor loads and stores on
  283. * this platform but for now we elect not to do this and portable
  284. * drivers should not do this anyway.
  285. */
  286. if (mmap_state == pci_mmap_io)
  287. return -EINVAL;
  288. /*
  289. * Ignore write-combine; for now only return uncached mappings.
  290. */
  291. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  292. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  293. vma->vm_end - vma->vm_start,
  294. vma->vm_page_prot);
  295. }
  296. #ifndef CONFIG_GENERIC_IOMAP
  297. void __iomem *__pci_ioport_map(struct pci_dev *dev,
  298. unsigned long port, unsigned int nr)
  299. {
  300. struct pci_channel *chan = dev->sysdata;
  301. if (unlikely(!chan->io_map_base)) {
  302. chan->io_map_base = sh_io_port_base;
  303. if (pci_domains_supported)
  304. panic("To avoid data corruption io_map_base MUST be "
  305. "set with multiple PCI domains.");
  306. }
  307. return (void __iomem *)(chan->io_map_base + port);
  308. }
  309. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  310. {
  311. iounmap(addr);
  312. }
  313. EXPORT_SYMBOL(pci_iounmap);
  314. #endif /* CONFIG_GENERIC_IOMAP */
  315. #ifdef CONFIG_HOTPLUG
  316. EXPORT_SYMBOL(pcibios_resource_to_bus);
  317. EXPORT_SYMBOL(pcibios_bus_to_resource);
  318. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  319. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  320. #endif