pci.c 10 KB

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