pci.c 10 KB

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