pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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_PCI_FIXED)
  117. continue;
  118. if (dev->resource[i].flags & IORESOURCE_IO)
  119. offset = hose->io_offset;
  120. else if (dev->resource[i].flags & IORESOURCE_MEM)
  121. offset = hose->mem_offset;
  122. dev->resource[i].start += offset;
  123. dev->resource[i].end += offset;
  124. }
  125. }
  126. /*
  127. * Called after each bus is probed, but before its children
  128. * are examined.
  129. */
  130. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  131. {
  132. struct pci_dev *dev = bus->self;
  133. struct list_head *ln;
  134. struct pci_channel *hose = bus->sysdata;
  135. if (!dev) {
  136. int i;
  137. for (i = 0; i < hose->nr_resources; i++)
  138. bus->resource[i] = hose->resources + i;
  139. }
  140. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  141. dev = pci_dev_b(ln);
  142. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  143. pcibios_fixup_device_resources(dev, bus);
  144. }
  145. }
  146. /*
  147. * We need to avoid collisions with `mirrored' VGA ports
  148. * and other strange ISA hardware, so we always want the
  149. * addresses to be allocated in the 0x000-0x0ff region
  150. * modulo 0x400.
  151. */
  152. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  153. resource_size_t size, resource_size_t align)
  154. {
  155. struct pci_dev *dev = data;
  156. struct pci_channel *hose = dev->sysdata;
  157. resource_size_t start = res->start;
  158. if (res->flags & IORESOURCE_IO) {
  159. if (start < PCIBIOS_MIN_IO + hose->resources[0].start)
  160. start = PCIBIOS_MIN_IO + hose->resources[0].start;
  161. /*
  162. * Put everything into 0x00-0xff region modulo 0x400.
  163. */
  164. if (start & 0x300)
  165. start = (start + 0x3ff) & ~0x3ff;
  166. }
  167. return start;
  168. }
  169. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  170. struct resource *res)
  171. {
  172. struct pci_channel *hose = dev->sysdata;
  173. unsigned long offset = 0;
  174. if (res->flags & IORESOURCE_IO)
  175. offset = hose->io_offset;
  176. else if (res->flags & IORESOURCE_MEM)
  177. offset = hose->mem_offset;
  178. region->start = res->start - offset;
  179. region->end = res->end - offset;
  180. }
  181. void pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  182. struct pci_bus_region *region)
  183. {
  184. struct pci_channel *hose = dev->sysdata;
  185. unsigned long offset = 0;
  186. if (res->flags & IORESOURCE_IO)
  187. offset = hose->io_offset;
  188. else if (res->flags & IORESOURCE_MEM)
  189. offset = hose->mem_offset;
  190. res->start = region->start + offset;
  191. res->end = region->end + offset;
  192. }
  193. int pcibios_enable_device(struct pci_dev *dev, int mask)
  194. {
  195. u16 cmd, old_cmd;
  196. int idx;
  197. struct resource *r;
  198. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  199. old_cmd = cmd;
  200. for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
  201. /* Only set up the requested stuff */
  202. if (!(mask & (1<<idx)))
  203. continue;
  204. r = &dev->resource[idx];
  205. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  206. continue;
  207. if ((idx == PCI_ROM_RESOURCE) &&
  208. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  209. continue;
  210. if (!r->start && r->end) {
  211. printk(KERN_ERR "PCI: Device %s not available "
  212. "because of resource collisions\n",
  213. pci_name(dev));
  214. return -EINVAL;
  215. }
  216. if (r->flags & IORESOURCE_IO)
  217. cmd |= PCI_COMMAND_IO;
  218. if (r->flags & IORESOURCE_MEM)
  219. cmd |= PCI_COMMAND_MEMORY;
  220. }
  221. if (cmd != old_cmd) {
  222. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  223. pci_name(dev), old_cmd, cmd);
  224. pci_write_config_word(dev, PCI_COMMAND, cmd);
  225. }
  226. return 0;
  227. }
  228. /*
  229. * If we set up a device for bus mastering, we need to check and set
  230. * the latency timer as it may not be properly set.
  231. */
  232. static unsigned int pcibios_max_latency = 255;
  233. void pcibios_set_master(struct pci_dev *dev)
  234. {
  235. u8 lat;
  236. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  237. if (lat < 16)
  238. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  239. else if (lat > pcibios_max_latency)
  240. lat = pcibios_max_latency;
  241. else
  242. return;
  243. printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
  244. pci_name(dev), lat);
  245. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  246. }
  247. void __init pcibios_update_irq(struct pci_dev *dev, int irq)
  248. {
  249. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  250. }
  251. char * __devinit pcibios_setup(char *str)
  252. {
  253. return str;
  254. }
  255. static void __init
  256. pcibios_bus_report_status_early(struct pci_channel *hose,
  257. int top_bus, int current_bus,
  258. unsigned int status_mask, int warn)
  259. {
  260. unsigned int pci_devfn;
  261. u16 status;
  262. int ret;
  263. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
  264. if (PCI_FUNC(pci_devfn))
  265. continue;
  266. ret = early_read_config_word(hose, top_bus, current_bus,
  267. pci_devfn, PCI_STATUS, &status);
  268. if (ret != PCIBIOS_SUCCESSFUL)
  269. continue;
  270. if (status == 0xffff)
  271. continue;
  272. early_write_config_word(hose, top_bus, current_bus,
  273. pci_devfn, PCI_STATUS,
  274. status & status_mask);
  275. if (warn)
  276. printk("(%02x:%02x: %04X) ", current_bus,
  277. pci_devfn, status);
  278. }
  279. }
  280. /*
  281. * We can't use pci_find_device() here since we are
  282. * called from interrupt context.
  283. */
  284. static void __init_refok
  285. pcibios_bus_report_status(struct pci_bus *bus, unsigned int status_mask,
  286. int warn)
  287. {
  288. struct pci_dev *dev;
  289. list_for_each_entry(dev, &bus->devices, bus_list) {
  290. u16 status;
  291. /*
  292. * ignore host bridge - we handle
  293. * that separately
  294. */
  295. if (dev->bus->number == 0 && dev->devfn == 0)
  296. continue;
  297. pci_read_config_word(dev, PCI_STATUS, &status);
  298. if (status == 0xffff)
  299. continue;
  300. if ((status & status_mask) == 0)
  301. continue;
  302. /* clear the status errors */
  303. pci_write_config_word(dev, PCI_STATUS, status & status_mask);
  304. if (warn)
  305. printk("(%s: %04X) ", pci_name(dev), status);
  306. }
  307. list_for_each_entry(dev, &bus->devices, bus_list)
  308. if (dev->subordinate)
  309. pcibios_bus_report_status(dev->subordinate, status_mask, warn);
  310. }
  311. void __init_refok pcibios_report_status(unsigned int status_mask, int warn)
  312. {
  313. struct pci_channel *hose;
  314. for (hose = hose_head; hose; hose = hose->next) {
  315. if (unlikely(!hose->bus))
  316. pcibios_bus_report_status_early(hose, hose_head->index,
  317. hose->index, status_mask, warn);
  318. else
  319. pcibios_bus_report_status(hose->bus, status_mask, warn);
  320. }
  321. }
  322. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  323. enum pci_mmap_state mmap_state, int write_combine)
  324. {
  325. /*
  326. * I/O space can be accessed via normal processor loads and stores on
  327. * this platform but for now we elect not to do this and portable
  328. * drivers should not do this anyway.
  329. */
  330. if (mmap_state == pci_mmap_io)
  331. return -EINVAL;
  332. /*
  333. * Ignore write-combine; for now only return uncached mappings.
  334. */
  335. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  336. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  337. vma->vm_end - vma->vm_start,
  338. vma->vm_page_prot);
  339. }
  340. #ifndef CONFIG_GENERIC_IOMAP
  341. static void __iomem *ioport_map_pci(struct pci_dev *dev,
  342. unsigned long port, unsigned int nr)
  343. {
  344. struct pci_channel *chan = dev->sysdata;
  345. if (unlikely(!chan->io_map_base)) {
  346. chan->io_map_base = generic_io_base;
  347. if (pci_domains_supported)
  348. panic("To avoid data corruption io_map_base MUST be "
  349. "set with multiple PCI domains.");
  350. }
  351. return (void __iomem *)(chan->io_map_base + port);
  352. }
  353. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  354. {
  355. resource_size_t start = pci_resource_start(dev, bar);
  356. resource_size_t len = pci_resource_len(dev, bar);
  357. unsigned long flags = pci_resource_flags(dev, bar);
  358. if (unlikely(!len || !start))
  359. return NULL;
  360. if (maxlen && len > maxlen)
  361. len = maxlen;
  362. if (flags & IORESOURCE_IO)
  363. return ioport_map_pci(dev, start, len);
  364. if (flags & IORESOURCE_MEM) {
  365. if (flags & IORESOURCE_CACHEABLE)
  366. return ioremap(start, len);
  367. return ioremap_nocache(start, len);
  368. }
  369. return NULL;
  370. }
  371. EXPORT_SYMBOL(pci_iomap);
  372. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  373. {
  374. iounmap(addr);
  375. }
  376. EXPORT_SYMBOL(pci_iounmap);
  377. #endif /* CONFIG_GENERIC_IOMAP */
  378. #ifdef CONFIG_HOTPLUG
  379. EXPORT_SYMBOL(pcibios_resource_to_bus);
  380. EXPORT_SYMBOL(pcibios_bus_to_resource);
  381. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  382. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  383. #endif