pci.c 9.6 KB

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