pci.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. u16 cmd, old_cmd;
  194. int idx;
  195. struct resource *r;
  196. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  197. old_cmd = cmd;
  198. for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
  199. /* Only set up the requested stuff */
  200. if (!(mask & (1<<idx)))
  201. continue;
  202. r = &dev->resource[idx];
  203. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  204. continue;
  205. if ((idx == PCI_ROM_RESOURCE) &&
  206. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  207. continue;
  208. if (!r->start && r->end) {
  209. printk(KERN_ERR "PCI: Device %s not available "
  210. "because of resource collisions\n",
  211. pci_name(dev));
  212. return -EINVAL;
  213. }
  214. if (r->flags & IORESOURCE_IO)
  215. cmd |= PCI_COMMAND_IO;
  216. if (r->flags & IORESOURCE_MEM)
  217. cmd |= PCI_COMMAND_MEMORY;
  218. }
  219. if (cmd != old_cmd) {
  220. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  221. pci_name(dev), old_cmd, cmd);
  222. pci_write_config_word(dev, PCI_COMMAND, cmd);
  223. }
  224. return 0;
  225. }
  226. /*
  227. * If we set up a device for bus mastering, we need to check and set
  228. * the latency timer as it may not be properly set.
  229. */
  230. static unsigned int pcibios_max_latency = 255;
  231. void pcibios_set_master(struct pci_dev *dev)
  232. {
  233. u8 lat;
  234. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  235. if (lat < 16)
  236. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  237. else if (lat > pcibios_max_latency)
  238. lat = pcibios_max_latency;
  239. else
  240. return;
  241. printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
  242. pci_name(dev), lat);
  243. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  244. }
  245. void __init pcibios_update_irq(struct pci_dev *dev, int irq)
  246. {
  247. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  248. }
  249. char * __devinit pcibios_setup(char *str)
  250. {
  251. return str;
  252. }
  253. static void __init
  254. pcibios_bus_report_status_early(struct pci_channel *hose,
  255. int top_bus, int current_bus,
  256. unsigned int status_mask, int warn)
  257. {
  258. unsigned int pci_devfn;
  259. u16 status;
  260. int ret;
  261. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
  262. if (PCI_FUNC(pci_devfn))
  263. continue;
  264. ret = early_read_config_word(hose, top_bus, current_bus,
  265. pci_devfn, PCI_STATUS, &status);
  266. if (ret != PCIBIOS_SUCCESSFUL)
  267. continue;
  268. if (status == 0xffff)
  269. continue;
  270. early_write_config_word(hose, top_bus, current_bus,
  271. pci_devfn, PCI_STATUS,
  272. status & status_mask);
  273. if (warn)
  274. printk("(%02x:%02x: %04X) ", current_bus,
  275. pci_devfn, status);
  276. }
  277. }
  278. /*
  279. * We can't use pci_find_device() here since we are
  280. * called from interrupt context.
  281. */
  282. static void __init_refok
  283. pcibios_bus_report_status(struct pci_bus *bus, unsigned int status_mask,
  284. int warn)
  285. {
  286. struct pci_dev *dev;
  287. list_for_each_entry(dev, &bus->devices, bus_list) {
  288. u16 status;
  289. /*
  290. * ignore host bridge - we handle
  291. * that separately
  292. */
  293. if (dev->bus->number == 0 && dev->devfn == 0)
  294. continue;
  295. pci_read_config_word(dev, PCI_STATUS, &status);
  296. if (status == 0xffff)
  297. continue;
  298. if ((status & status_mask) == 0)
  299. continue;
  300. /* clear the status errors */
  301. pci_write_config_word(dev, PCI_STATUS, status & status_mask);
  302. if (warn)
  303. printk("(%s: %04X) ", pci_name(dev), status);
  304. }
  305. list_for_each_entry(dev, &bus->devices, bus_list)
  306. if (dev->subordinate)
  307. pcibios_bus_report_status(dev->subordinate, status_mask, warn);
  308. }
  309. void __init_refok pcibios_report_status(unsigned int status_mask, int warn)
  310. {
  311. struct pci_channel *hose;
  312. for (hose = hose_head; hose; hose = hose->next) {
  313. if (unlikely(!hose->bus))
  314. pcibios_bus_report_status_early(hose, hose_head->index,
  315. hose->index, status_mask, warn);
  316. else
  317. pcibios_bus_report_status(hose->bus, status_mask, warn);
  318. }
  319. }
  320. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  321. enum pci_mmap_state mmap_state, int write_combine)
  322. {
  323. /*
  324. * I/O space can be accessed via normal processor loads and stores on
  325. * this platform but for now we elect not to do this and portable
  326. * drivers should not do this anyway.
  327. */
  328. if (mmap_state == pci_mmap_io)
  329. return -EINVAL;
  330. /*
  331. * Ignore write-combine; for now only return uncached mappings.
  332. */
  333. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  334. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  335. vma->vm_end - vma->vm_start,
  336. vma->vm_page_prot);
  337. }
  338. #ifndef CONFIG_GENERIC_IOMAP
  339. static void __iomem *ioport_map_pci(struct pci_dev *dev,
  340. unsigned long port, unsigned int nr)
  341. {
  342. struct pci_channel *chan = dev->sysdata;
  343. if (unlikely(!chan->io_map_base)) {
  344. chan->io_map_base = generic_io_base;
  345. if (pci_domains_supported)
  346. panic("To avoid data corruption io_map_base MUST be "
  347. "set with multiple PCI domains.");
  348. }
  349. return (void __iomem *)(chan->io_map_base + port);
  350. }
  351. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  352. {
  353. resource_size_t start = pci_resource_start(dev, bar);
  354. resource_size_t len = pci_resource_len(dev, bar);
  355. unsigned long flags = pci_resource_flags(dev, bar);
  356. if (unlikely(!len || !start))
  357. return NULL;
  358. if (maxlen && len > maxlen)
  359. len = maxlen;
  360. if (flags & IORESOURCE_IO)
  361. return ioport_map_pci(dev, start, len);
  362. if (flags & IORESOURCE_MEM) {
  363. if (flags & IORESOURCE_CACHEABLE)
  364. return ioremap(start, len);
  365. return ioremap_nocache(start, len);
  366. }
  367. return NULL;
  368. }
  369. EXPORT_SYMBOL(pci_iomap);
  370. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  371. {
  372. iounmap(addr);
  373. }
  374. EXPORT_SYMBOL(pci_iounmap);
  375. #endif /* CONFIG_GENERIC_IOMAP */
  376. #ifdef CONFIG_HOTPLUG
  377. EXPORT_SYMBOL(pcibios_resource_to_bus);
  378. EXPORT_SYMBOL(pcibios_bus_to_resource);
  379. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  380. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  381. #endif