pci.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation; either version 2 of the License, or (at your
  5. * option) any later version.
  6. *
  7. * Copyright (C) 2003, 04, 11 Ralf Baechle (ralf@linux-mips.org)
  8. * Copyright (C) 2011 Wind River Systems,
  9. * written by Ralf Baechle (ralf@linux-mips.org)
  10. */
  11. #include <linux/bug.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/export.h>
  16. #include <linux/init.h>
  17. #include <linux/types.h>
  18. #include <linux/pci.h>
  19. #include <asm/cpu-info.h>
  20. /*
  21. * Indicate whether we respect the PCI setup left by the firmware.
  22. *
  23. * Make this long-lived so that we know when shutting down
  24. * whether we probed only or not.
  25. */
  26. int pci_probe_only;
  27. #define PCI_ASSIGN_ALL_BUSSES 1
  28. unsigned int pci_probe = PCI_ASSIGN_ALL_BUSSES;
  29. /*
  30. * The PCI controller list.
  31. */
  32. static struct pci_controller *hose_head, **hose_tail = &hose_head;
  33. unsigned long PCIBIOS_MIN_IO;
  34. unsigned long PCIBIOS_MIN_MEM;
  35. static int pci_initialized;
  36. /*
  37. * We need to avoid collisions with `mirrored' VGA ports
  38. * and other strange ISA hardware, so we always want the
  39. * addresses to be allocated in the 0x000-0x0ff region
  40. * modulo 0x400.
  41. *
  42. * Why? Because some silly external IO cards only decode
  43. * the low 10 bits of the IO address. The 0x00-0xff region
  44. * is reserved for motherboard devices that decode all 16
  45. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  46. * but we want to try to avoid allocating at 0x2900-0x2bff
  47. * which might have be mirrored at 0x0100-0x03ff..
  48. */
  49. resource_size_t
  50. pcibios_align_resource(void *data, const struct resource *res,
  51. resource_size_t size, resource_size_t align)
  52. {
  53. struct pci_dev *dev = data;
  54. struct pci_controller *hose = dev->sysdata;
  55. resource_size_t start = res->start;
  56. if (res->flags & IORESOURCE_IO) {
  57. /* Make sure we start at our min on all hoses */
  58. if (start < PCIBIOS_MIN_IO + hose->io_resource->start)
  59. start = PCIBIOS_MIN_IO + hose->io_resource->start;
  60. /*
  61. * Put everything into 0x00-0xff region modulo 0x400
  62. */
  63. if (start & 0x300)
  64. start = (start + 0x3ff) & ~0x3ff;
  65. } else if (res->flags & IORESOURCE_MEM) {
  66. /* Make sure we start at our min on all hoses */
  67. if (start < PCIBIOS_MIN_MEM + hose->mem_resource->start)
  68. start = PCIBIOS_MIN_MEM + hose->mem_resource->start;
  69. }
  70. return start;
  71. }
  72. static void __devinit pcibios_scanbus(struct pci_controller *hose)
  73. {
  74. static int next_busno;
  75. static int need_domain_info;
  76. struct pci_bus *bus;
  77. if (!hose->iommu)
  78. PCI_DMA_BUS_IS_PHYS = 1;
  79. if (hose->get_busno && pci_probe_only)
  80. next_busno = (*hose->get_busno)();
  81. bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
  82. hose->bus = bus;
  83. need_domain_info = need_domain_info || hose->index;
  84. hose->need_domain_info = need_domain_info;
  85. if (bus) {
  86. next_busno = bus->subordinate + 1;
  87. /* Don't allow 8-bit bus number overflow inside the hose -
  88. reserve some space for bridges. */
  89. if (next_busno > 224) {
  90. next_busno = 0;
  91. need_domain_info = 1;
  92. }
  93. if (!pci_probe_only) {
  94. pci_bus_size_bridges(bus);
  95. pci_bus_assign_resources(bus);
  96. pci_enable_bridges(bus);
  97. }
  98. }
  99. }
  100. static DEFINE_MUTEX(pci_scan_mutex);
  101. void __devinit register_pci_controller(struct pci_controller *hose)
  102. {
  103. if (request_resource(&iomem_resource, hose->mem_resource) < 0)
  104. goto out;
  105. if (request_resource(&ioport_resource, hose->io_resource) < 0) {
  106. release_resource(hose->mem_resource);
  107. goto out;
  108. }
  109. *hose_tail = hose;
  110. hose_tail = &hose->next;
  111. /*
  112. * Do not panic here but later - this might happen before console init.
  113. */
  114. if (!hose->io_map_base) {
  115. printk(KERN_WARNING
  116. "registering PCI controller with io_map_base unset\n");
  117. }
  118. /*
  119. * Scan the bus if it is register after the PCI subsystem
  120. * initialization.
  121. */
  122. if (pci_initialized) {
  123. mutex_lock(&pci_scan_mutex);
  124. pcibios_scanbus(hose);
  125. mutex_unlock(&pci_scan_mutex);
  126. }
  127. return;
  128. out:
  129. printk(KERN_WARNING
  130. "Skipping PCI bus scan due to resource conflict\n");
  131. }
  132. static void __init pcibios_set_cache_line_size(void)
  133. {
  134. struct cpuinfo_mips *c = &current_cpu_data;
  135. unsigned int lsize;
  136. /*
  137. * Set PCI cacheline size to that of the highest level in the
  138. * cache hierarchy.
  139. */
  140. lsize = c->dcache.linesz;
  141. lsize = c->scache.linesz ? : lsize;
  142. lsize = c->tcache.linesz ? : lsize;
  143. BUG_ON(!lsize);
  144. pci_dfl_cache_line_size = lsize >> 2;
  145. pr_debug("PCI: pci_cache_line_size set to %d bytes\n", lsize);
  146. }
  147. static int __init pcibios_init(void)
  148. {
  149. struct pci_controller *hose;
  150. pcibios_set_cache_line_size();
  151. /* Scan all of the recorded PCI controllers. */
  152. for (hose = hose_head; hose; hose = hose->next)
  153. pcibios_scanbus(hose);
  154. pci_fixup_irqs(pci_common_swizzle, pcibios_map_irq);
  155. pci_initialized = 1;
  156. return 0;
  157. }
  158. subsys_initcall(pcibios_init);
  159. static int pcibios_enable_resources(struct pci_dev *dev, int mask)
  160. {
  161. u16 cmd, old_cmd;
  162. int idx;
  163. struct resource *r;
  164. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  165. old_cmd = cmd;
  166. for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
  167. /* Only set up the requested stuff */
  168. if (!(mask & (1<<idx)))
  169. continue;
  170. r = &dev->resource[idx];
  171. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  172. continue;
  173. if ((idx == PCI_ROM_RESOURCE) &&
  174. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  175. continue;
  176. if (!r->start && r->end) {
  177. printk(KERN_ERR "PCI: Device %s not available "
  178. "because of resource collisions\n",
  179. pci_name(dev));
  180. return -EINVAL;
  181. }
  182. if (r->flags & IORESOURCE_IO)
  183. cmd |= PCI_COMMAND_IO;
  184. if (r->flags & IORESOURCE_MEM)
  185. cmd |= PCI_COMMAND_MEMORY;
  186. }
  187. if (cmd != old_cmd) {
  188. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  189. pci_name(dev), old_cmd, cmd);
  190. pci_write_config_word(dev, PCI_COMMAND, cmd);
  191. }
  192. return 0;
  193. }
  194. /*
  195. * If we set up a device for bus mastering, we need to check the latency
  196. * timer as certain crappy BIOSes forget to set it properly.
  197. */
  198. static unsigned int pcibios_max_latency = 255;
  199. void pcibios_set_master(struct pci_dev *dev)
  200. {
  201. u8 lat;
  202. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  203. if (lat < 16)
  204. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  205. else if (lat > pcibios_max_latency)
  206. lat = pcibios_max_latency;
  207. else
  208. return;
  209. printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n",
  210. pci_name(dev), lat);
  211. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  212. }
  213. unsigned int pcibios_assign_all_busses(void)
  214. {
  215. return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
  216. }
  217. int pcibios_enable_device(struct pci_dev *dev, int mask)
  218. {
  219. int err;
  220. if ((err = pcibios_enable_resources(dev, mask)) < 0)
  221. return err;
  222. return pcibios_plat_dev_init(dev);
  223. }
  224. static void pcibios_fixup_device_resources(struct pci_dev *dev,
  225. struct pci_bus *bus)
  226. {
  227. /* Update device resources. */
  228. struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
  229. unsigned long offset = 0;
  230. int i;
  231. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  232. if (!dev->resource[i].start)
  233. continue;
  234. if (dev->resource[i].flags & IORESOURCE_IO)
  235. offset = hose->io_offset;
  236. else if (dev->resource[i].flags & IORESOURCE_MEM)
  237. offset = hose->mem_offset;
  238. dev->resource[i].start += offset;
  239. dev->resource[i].end += offset;
  240. }
  241. }
  242. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  243. {
  244. /* Propagate hose info into the subordinate devices. */
  245. struct pci_controller *hose = bus->sysdata;
  246. struct list_head *ln;
  247. struct pci_dev *dev = bus->self;
  248. if (!dev) {
  249. bus->resource[0] = hose->io_resource;
  250. bus->resource[1] = hose->mem_resource;
  251. } else if (pci_probe_only &&
  252. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  253. pci_read_bridge_bases(bus);
  254. pcibios_fixup_device_resources(dev, bus);
  255. }
  256. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  257. dev = pci_dev_b(ln);
  258. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  259. pcibios_fixup_device_resources(dev, bus);
  260. }
  261. }
  262. void __init
  263. pcibios_update_irq(struct pci_dev *dev, int irq)
  264. {
  265. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  266. }
  267. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  268. struct resource *res)
  269. {
  270. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  271. unsigned long offset = 0;
  272. if (res->flags & IORESOURCE_IO)
  273. offset = hose->io_offset;
  274. else if (res->flags & IORESOURCE_MEM)
  275. offset = hose->mem_offset;
  276. region->start = res->start - offset;
  277. region->end = res->end - offset;
  278. }
  279. void __devinit
  280. pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  281. struct pci_bus_region *region)
  282. {
  283. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  284. unsigned long offset = 0;
  285. if (res->flags & IORESOURCE_IO)
  286. offset = hose->io_offset;
  287. else if (res->flags & IORESOURCE_MEM)
  288. offset = hose->mem_offset;
  289. res->start = region->start + offset;
  290. res->end = region->end + offset;
  291. }
  292. #ifdef CONFIG_HOTPLUG
  293. EXPORT_SYMBOL(pcibios_resource_to_bus);
  294. EXPORT_SYMBOL(pcibios_bus_to_resource);
  295. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  296. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  297. #endif
  298. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  299. enum pci_mmap_state mmap_state, int write_combine)
  300. {
  301. unsigned long prot;
  302. /*
  303. * I/O space can be accessed via normal processor loads and stores on
  304. * this platform but for now we elect not to do this and portable
  305. * drivers should not do this anyway.
  306. */
  307. if (mmap_state == pci_mmap_io)
  308. return -EINVAL;
  309. /*
  310. * Ignore write-combine; for now only return uncached mappings.
  311. */
  312. prot = pgprot_val(vma->vm_page_prot);
  313. prot = (prot & ~_CACHE_MASK) | _CACHE_UNCACHED;
  314. vma->vm_page_prot = __pgprot(prot);
  315. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  316. vma->vm_end - vma->vm_start, vma->vm_page_prot);
  317. }
  318. char * (*pcibios_plat_setup)(char *str) __devinitdata;
  319. char *__devinit pcibios_setup(char *str)
  320. {
  321. if (pcibios_plat_setup)
  322. return pcibios_plat_setup(str);
  323. return str;
  324. }