i386.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Low-Level PCI Access for i386 machines
  3. *
  4. * Copyright 1993, 1994 Drew Eckhardt
  5. * Visionary Computing
  6. * (Unix and Linux consulting and custom programming)
  7. * Drew@Colorado.EDU
  8. * +1 (303) 786-7975
  9. *
  10. * Drew's work was sponsored by:
  11. * iX Multiuser Multitasking Magazine
  12. * Hannover, Germany
  13. * hm@ix.de
  14. *
  15. * Copyright 1997--2000 Martin Mares <mj@ucw.cz>
  16. *
  17. * For more information, please consult the following manuals (look at
  18. * http://www.pcisig.com/ for how to get them):
  19. *
  20. * PCI BIOS Specification
  21. * PCI Local Bus Specification
  22. * PCI to PCI Bridge Specification
  23. * PCI System Design Guide
  24. *
  25. */
  26. #include <linux/types.h>
  27. #include <linux/kernel.h>
  28. #include <linux/pci.h>
  29. #include <linux/init.h>
  30. #include <linux/ioport.h>
  31. #include <linux/errno.h>
  32. #include <linux/bootmem.h>
  33. #include <linux/acpi.h>
  34. #include <asm/pat.h>
  35. #include <asm/hpet.h>
  36. #include <asm/io_apic.h>
  37. #include "pci.h"
  38. static int
  39. skip_isa_ioresource_align(struct pci_dev *dev) {
  40. if ((pci_probe & PCI_CAN_SKIP_ISA_ALIGN) &&
  41. !(dev->bus->bridge_ctl & PCI_BRIDGE_CTL_ISA))
  42. return 1;
  43. return 0;
  44. }
  45. /*
  46. * We need to avoid collisions with `mirrored' VGA ports
  47. * and other strange ISA hardware, so we always want the
  48. * addresses to be allocated in the 0x000-0x0ff region
  49. * modulo 0x400.
  50. *
  51. * Why? Because some silly external IO cards only decode
  52. * the low 10 bits of the IO address. The 0x00-0xff region
  53. * is reserved for motherboard devices that decode all 16
  54. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  55. * but we want to try to avoid allocating at 0x2900-0x2bff
  56. * which might have be mirrored at 0x0100-0x03ff..
  57. */
  58. void
  59. pcibios_align_resource(void *data, struct resource *res,
  60. resource_size_t size, resource_size_t align)
  61. {
  62. struct pci_dev *dev = data;
  63. if (res->flags & IORESOURCE_IO) {
  64. resource_size_t start = res->start;
  65. if (skip_isa_ioresource_align(dev))
  66. return;
  67. if (start & 0x300) {
  68. start = (start + 0x3ff) & ~0x3ff;
  69. res->start = start;
  70. }
  71. }
  72. }
  73. EXPORT_SYMBOL(pcibios_align_resource);
  74. static int check_res_with_valid(struct pci_dev *dev, struct resource *res)
  75. {
  76. unsigned long base;
  77. unsigned long size;
  78. int i;
  79. base = res->start;
  80. size = (res->start == 0 && res->end == res->start) ? 0 :
  81. (res->end - res->start + 1);
  82. if (!base || !size)
  83. return 0;
  84. #ifdef CONFIG_HPET_TIMER
  85. /* for hpet */
  86. if (base == hpet_address && (res->flags & IORESOURCE_MEM)) {
  87. dev_info(&dev->dev, "BAR has HPET at %08lx-%08lx\n",
  88. base, base + size - 1);
  89. return 1;
  90. }
  91. #endif
  92. #ifdef CONFIG_X86_IO_APIC
  93. for (i = 0; i < nr_ioapics; i++) {
  94. unsigned long ioapic_phys = mp_ioapics[i].mp_apicaddr;
  95. if (base == ioapic_phys && (res->flags & IORESOURCE_MEM)) {
  96. dev_info(&dev->dev, "BAR has ioapic at %08lx-%08lx\n",
  97. base, base + size - 1);
  98. return 1;
  99. }
  100. }
  101. #endif
  102. #ifdef CONFIG_PCI_MMCONFIG
  103. for (i = 0; i < pci_mmcfg_config_num; i++) {
  104. unsigned long addr;
  105. addr = pci_mmcfg_config[i].address;
  106. if (base == addr && (res->flags & IORESOURCE_MEM)) {
  107. dev_info(&dev->dev, "BAR has MMCONFIG at %08lx-%08lx\n",
  108. base, base + size - 1);
  109. return 1;
  110. }
  111. }
  112. #endif
  113. return 0;
  114. }
  115. static int check_platform(struct pci_dev *dev, struct resource *res)
  116. {
  117. struct resource *root = NULL;
  118. /*
  119. * forcibly insert it into the
  120. * resource tree
  121. */
  122. if (res->flags & IORESOURCE_MEM)
  123. root = &iomem_resource;
  124. else if (res->flags & IORESOURCE_IO)
  125. root = &ioport_resource;
  126. if (root && check_res_with_valid(dev, res)) {
  127. insert_resource(root, res);
  128. return 1;
  129. }
  130. return 0;
  131. }
  132. /*
  133. * Handle resources of PCI devices. If the world were perfect, we could
  134. * just allocate all the resource regions and do nothing more. It isn't.
  135. * On the other hand, we cannot just re-allocate all devices, as it would
  136. * require us to know lots of host bridge internals. So we attempt to
  137. * keep as much of the original configuration as possible, but tweak it
  138. * when it's found to be wrong.
  139. *
  140. * Known BIOS problems we have to work around:
  141. * - I/O or memory regions not configured
  142. * - regions configured, but not enabled in the command register
  143. * - bogus I/O addresses above 64K used
  144. * - expansion ROMs left enabled (this may sound harmless, but given
  145. * the fact the PCI specs explicitly allow address decoders to be
  146. * shared between expansion ROMs and other resource regions, it's
  147. * at least dangerous)
  148. *
  149. * Our solution:
  150. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  151. * This gives us fixed barriers on where we can allocate.
  152. * (2) Allocate resources for all enabled devices. If there is
  153. * a collision, just mark the resource as unallocated. Also
  154. * disable expansion ROMs during this step.
  155. * (3) Try to allocate resources for disabled devices. If the
  156. * resources were assigned correctly, everything goes well,
  157. * if they weren't, they won't disturb allocation of other
  158. * resources.
  159. * (4) Assign new addresses to resources which were either
  160. * not configured at all or misconfigured. If explicitly
  161. * requested by the user, configure expansion ROM address
  162. * as well.
  163. */
  164. static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
  165. {
  166. struct pci_bus *bus;
  167. struct pci_dev *dev;
  168. int idx;
  169. struct resource *r, *pr;
  170. /* Depth-First Search on bus tree */
  171. list_for_each_entry(bus, bus_list, node) {
  172. if ((dev = bus->self)) {
  173. for (idx = PCI_BRIDGE_RESOURCES;
  174. idx < PCI_NUM_RESOURCES; idx++) {
  175. r = &dev->resource[idx];
  176. if (!r->flags)
  177. continue;
  178. pr = pci_find_parent_resource(dev, r);
  179. if (!r->start || !pr ||
  180. request_resource(pr, r) < 0) {
  181. if (check_platform(dev, r))
  182. continue;
  183. dev_err(&dev->dev, "BAR %d: can't "
  184. "allocate resource\n", idx);
  185. /*
  186. * Something is wrong with the region.
  187. * Invalidate the resource to prevent
  188. * child resource allocations in this
  189. * range.
  190. */
  191. r->flags = 0;
  192. }
  193. }
  194. }
  195. pcibios_allocate_bus_resources(&bus->children);
  196. }
  197. }
  198. static void __init pcibios_allocate_resources(int pass)
  199. {
  200. struct pci_dev *dev = NULL;
  201. int idx, disabled;
  202. u16 command;
  203. struct resource *r, *pr;
  204. for_each_pci_dev(dev) {
  205. pci_read_config_word(dev, PCI_COMMAND, &command);
  206. for (idx = 0; idx < PCI_ROM_RESOURCE; idx++) {
  207. r = &dev->resource[idx];
  208. if (r->parent) /* Already allocated */
  209. continue;
  210. if (!r->start) /* Address not assigned at all */
  211. continue;
  212. if (r->flags & IORESOURCE_IO)
  213. disabled = !(command & PCI_COMMAND_IO);
  214. else
  215. disabled = !(command & PCI_COMMAND_MEMORY);
  216. if (pass == disabled) {
  217. dev_dbg(&dev->dev, "resource %#08llx-%#08llx "
  218. "(f=%lx, d=%d, p=%d)\n",
  219. (unsigned long long) r->start,
  220. (unsigned long long) r->end,
  221. r->flags, disabled, pass);
  222. pr = pci_find_parent_resource(dev, r);
  223. if (!pr || request_resource(pr, r) < 0) {
  224. if (check_platform(dev, r))
  225. continue;
  226. dev_err(&dev->dev, "BAR %d: can't "
  227. "allocate resource\n", idx);
  228. /* We'll assign a new address later */
  229. r->end -= r->start;
  230. r->start = 0;
  231. }
  232. }
  233. }
  234. if (!pass) {
  235. r = &dev->resource[PCI_ROM_RESOURCE];
  236. if (r->flags & IORESOURCE_ROM_ENABLE) {
  237. /* Turn the ROM off, leave the resource region,
  238. * but keep it unregistered. */
  239. u32 reg;
  240. dev_dbg(&dev->dev, "disabling ROM\n");
  241. r->flags &= ~IORESOURCE_ROM_ENABLE;
  242. pci_read_config_dword(dev,
  243. dev->rom_base_reg, &reg);
  244. pci_write_config_dword(dev, dev->rom_base_reg,
  245. reg & ~PCI_ROM_ADDRESS_ENABLE);
  246. }
  247. }
  248. }
  249. }
  250. static int __init pcibios_assign_resources(void)
  251. {
  252. struct pci_dev *dev = NULL;
  253. struct resource *r, *pr;
  254. if (!(pci_probe & PCI_ASSIGN_ROMS)) {
  255. /*
  256. * Try to use BIOS settings for ROMs, otherwise let
  257. * pci_assign_unassigned_resources() allocate the new
  258. * addresses.
  259. */
  260. for_each_pci_dev(dev) {
  261. r = &dev->resource[PCI_ROM_RESOURCE];
  262. if (!r->flags || !r->start)
  263. continue;
  264. pr = pci_find_parent_resource(dev, r);
  265. if (!pr || request_resource(pr, r) < 0) {
  266. r->end -= r->start;
  267. r->start = 0;
  268. }
  269. }
  270. }
  271. pci_assign_unassigned_resources();
  272. return 0;
  273. }
  274. void __init pcibios_resource_survey(void)
  275. {
  276. DBG("PCI: Allocating resources\n");
  277. pcibios_allocate_bus_resources(&pci_root_buses);
  278. pcibios_allocate_resources(0);
  279. pcibios_allocate_resources(1);
  280. }
  281. /**
  282. * called in fs_initcall (one below subsys_initcall),
  283. * give a chance for motherboard reserve resources
  284. */
  285. fs_initcall(pcibios_assign_resources);
  286. /*
  287. * If we set up a device for bus mastering, we need to check the latency
  288. * timer as certain crappy BIOSes forget to set it properly.
  289. */
  290. unsigned int pcibios_max_latency = 255;
  291. void pcibios_set_master(struct pci_dev *dev)
  292. {
  293. u8 lat;
  294. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  295. if (lat < 16)
  296. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  297. else if (lat > pcibios_max_latency)
  298. lat = pcibios_max_latency;
  299. else
  300. return;
  301. dev_printk(KERN_DEBUG, &dev->dev, "setting latency timer to %d\n", lat);
  302. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  303. }
  304. static void pci_unmap_page_range(struct vm_area_struct *vma)
  305. {
  306. u64 addr = (u64)vma->vm_pgoff << PAGE_SHIFT;
  307. free_memtype(addr, addr + vma->vm_end - vma->vm_start);
  308. }
  309. static void pci_track_mmap_page_range(struct vm_area_struct *vma)
  310. {
  311. u64 addr = (u64)vma->vm_pgoff << PAGE_SHIFT;
  312. unsigned long flags = pgprot_val(vma->vm_page_prot)
  313. & _PAGE_CACHE_MASK;
  314. reserve_memtype(addr, addr + vma->vm_end - vma->vm_start, flags, NULL);
  315. }
  316. static struct vm_operations_struct pci_mmap_ops = {
  317. .open = pci_track_mmap_page_range,
  318. .close = pci_unmap_page_range,
  319. .access = generic_access_phys,
  320. };
  321. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  322. enum pci_mmap_state mmap_state, int write_combine)
  323. {
  324. unsigned long prot;
  325. u64 addr = vma->vm_pgoff << PAGE_SHIFT;
  326. unsigned long len = vma->vm_end - vma->vm_start;
  327. unsigned long flags;
  328. unsigned long new_flags;
  329. int retval;
  330. /* I/O space cannot be accessed via normal processor loads and
  331. * stores on this platform.
  332. */
  333. if (mmap_state == pci_mmap_io)
  334. return -EINVAL;
  335. prot = pgprot_val(vma->vm_page_prot);
  336. if (pat_enabled && write_combine)
  337. prot |= _PAGE_CACHE_WC;
  338. else if (pat_enabled || boot_cpu_data.x86 > 3)
  339. /*
  340. * ioremap() and ioremap_nocache() defaults to UC MINUS for now.
  341. * To avoid attribute conflicts, request UC MINUS here
  342. * aswell.
  343. */
  344. prot |= _PAGE_CACHE_UC_MINUS;
  345. vma->vm_page_prot = __pgprot(prot);
  346. flags = pgprot_val(vma->vm_page_prot) & _PAGE_CACHE_MASK;
  347. retval = reserve_memtype(addr, addr + len, flags, &new_flags);
  348. if (retval)
  349. return retval;
  350. if (flags != new_flags) {
  351. /*
  352. * Do not fallback to certain memory types with certain
  353. * requested type:
  354. * - request is uncached, return cannot be write-back
  355. * - request is uncached, return cannot be write-combine
  356. * - request is write-combine, return cannot be write-back
  357. */
  358. if ((flags == _PAGE_CACHE_UC_MINUS &&
  359. (new_flags == _PAGE_CACHE_WB)) ||
  360. (flags == _PAGE_CACHE_WC &&
  361. new_flags == _PAGE_CACHE_WB)) {
  362. free_memtype(addr, addr+len);
  363. return -EINVAL;
  364. }
  365. flags = new_flags;
  366. }
  367. if (((vma->vm_pgoff < max_low_pfn_mapped) ||
  368. (vma->vm_pgoff >= (1UL<<(32 - PAGE_SHIFT)) &&
  369. vma->vm_pgoff < max_pfn_mapped)) &&
  370. ioremap_change_attr((unsigned long)__va(addr), len, flags)) {
  371. free_memtype(addr, addr + len);
  372. return -EINVAL;
  373. }
  374. if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  375. vma->vm_end - vma->vm_start,
  376. vma->vm_page_prot))
  377. return -EAGAIN;
  378. vma->vm_ops = &pci_mmap_ops;
  379. return 0;
  380. }