aperture_64.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Firmware replacement code.
  3. *
  4. * Work around broken BIOSes that don't set an aperture or only set the
  5. * aperture in the AGP bridge.
  6. * If all fails map the aperture over some low memory. This is cheaper than
  7. * doing bounce buffering. The memory is lost. This is done at early boot
  8. * because only the bootmem allocator can allocate 32+MB.
  9. *
  10. * Copyright 2002 Andi Kleen, SuSE Labs.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/init.h>
  15. #include <linux/bootmem.h>
  16. #include <linux/mmzone.h>
  17. #include <linux/pci_ids.h>
  18. #include <linux/pci.h>
  19. #include <linux/bitops.h>
  20. #include <linux/ioport.h>
  21. #include <asm/e820.h>
  22. #include <asm/io.h>
  23. #include <asm/gart.h>
  24. #include <asm/pci-direct.h>
  25. #include <asm/dma.h>
  26. #include <asm/k8.h>
  27. int gart_iommu_aperture;
  28. int gart_iommu_aperture_disabled __initdata = 0;
  29. int gart_iommu_aperture_allowed __initdata = 0;
  30. int fallback_aper_order __initdata = 1; /* 64MB */
  31. int fallback_aper_force __initdata = 0;
  32. int fix_aperture __initdata = 1;
  33. static struct resource gart_resource = {
  34. .name = "GART",
  35. .flags = IORESOURCE_MEM,
  36. };
  37. static void __init insert_aperture_resource(u32 aper_base, u32 aper_size)
  38. {
  39. gart_resource.start = aper_base;
  40. gart_resource.end = aper_base + aper_size - 1;
  41. insert_resource(&iomem_resource, &gart_resource);
  42. }
  43. /* This code runs before the PCI subsystem is initialized, so just
  44. access the northbridge directly. */
  45. static u32 __init allocate_aperture(void)
  46. {
  47. u32 aper_size;
  48. void *p;
  49. if (fallback_aper_order > 7)
  50. fallback_aper_order = 7;
  51. aper_size = (32 * 1024 * 1024) << fallback_aper_order;
  52. /*
  53. * Aperture has to be naturally aligned. This means a 2GB aperture
  54. * won't have much chance of finding a place in the lower 4GB of
  55. * memory. Unfortunately we cannot move it up because that would
  56. * make the IOMMU useless.
  57. */
  58. p = __alloc_bootmem_nopanic(aper_size, aper_size, 0);
  59. if (!p || __pa(p)+aper_size > 0xffffffff) {
  60. printk("Cannot allocate aperture memory hole (%p,%uK)\n",
  61. p, aper_size>>10);
  62. if (p)
  63. free_bootmem(__pa(p), aper_size);
  64. return 0;
  65. }
  66. printk("Mapping aperture over %d KB of RAM @ %lx\n",
  67. aper_size >> 10, __pa(p));
  68. insert_aperture_resource((u32)__pa(p), aper_size);
  69. return (u32)__pa(p);
  70. }
  71. static int __init aperture_valid(u64 aper_base, u32 aper_size)
  72. {
  73. if (!aper_base)
  74. return 0;
  75. if (aper_size < 64*1024*1024) {
  76. printk("Aperture too small (%d MB)\n", aper_size>>20);
  77. return 0;
  78. }
  79. if (aper_base + aper_size > 0x100000000UL) {
  80. printk("Aperture beyond 4GB. Ignoring.\n");
  81. return 0;
  82. }
  83. if (e820_any_mapped(aper_base, aper_base + aper_size, E820_RAM)) {
  84. printk("Aperture pointing to e820 RAM. Ignoring.\n");
  85. return 0;
  86. }
  87. return 1;
  88. }
  89. /* Find a PCI capability */
  90. static __u32 __init find_cap(int num, int slot, int func, int cap)
  91. {
  92. int bytes;
  93. u8 pos;
  94. if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
  95. PCI_STATUS_CAP_LIST))
  96. return 0;
  97. pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
  98. for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
  99. u8 id;
  100. pos &= ~3;
  101. id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
  102. if (id == 0xff)
  103. break;
  104. if (id == cap)
  105. return pos;
  106. pos = read_pci_config_byte(num, slot, func,
  107. pos+PCI_CAP_LIST_NEXT);
  108. }
  109. return 0;
  110. }
  111. /* Read a standard AGPv3 bridge header */
  112. static __u32 __init read_agp(int num, int slot, int func, int cap, u32 *order)
  113. {
  114. u32 apsize;
  115. u32 apsizereg;
  116. int nbits;
  117. u32 aper_low, aper_hi;
  118. u64 aper;
  119. printk("AGP bridge at %02x:%02x:%02x\n", num, slot, func);
  120. apsizereg = read_pci_config_16(num, slot, func, cap + 0x14);
  121. if (apsizereg == 0xffffffff) {
  122. printk("APSIZE in AGP bridge unreadable\n");
  123. return 0;
  124. }
  125. apsize = apsizereg & 0xfff;
  126. /* Some BIOS use weird encodings not in the AGPv3 table. */
  127. if (apsize & 0xff)
  128. apsize |= 0xf00;
  129. nbits = hweight16(apsize);
  130. *order = 7 - nbits;
  131. if ((int)*order < 0) /* < 32MB */
  132. *order = 0;
  133. aper_low = read_pci_config(num, slot, func, 0x10);
  134. aper_hi = read_pci_config(num, slot, func, 0x14);
  135. aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
  136. printk("Aperture from AGP @ %Lx size %u MB (APSIZE %x)\n",
  137. aper, 32 << *order, apsizereg);
  138. if (!aperture_valid(aper, (32*1024*1024) << *order))
  139. return 0;
  140. return (u32)aper;
  141. }
  142. /*
  143. * Look for an AGP bridge. Windows only expects the aperture in the
  144. * AGP bridge and some BIOS forget to initialize the Northbridge too.
  145. * Work around this here.
  146. *
  147. * Do an PCI bus scan by hand because we're running before the PCI
  148. * subsystem.
  149. *
  150. * All K8 AGP bridges are AGPv3 compliant, so we can do this scan
  151. * generically. It's probably overkill to always scan all slots because
  152. * the AGP bridges should be always an own bus on the HT hierarchy,
  153. * but do it here for future safety.
  154. */
  155. static __u32 __init search_agp_bridge(u32 *order, int *valid_agp)
  156. {
  157. int num, slot, func;
  158. /* Poor man's PCI discovery */
  159. for (num = 0; num < 256; num++) {
  160. for (slot = 0; slot < 32; slot++) {
  161. for (func = 0; func < 8; func++) {
  162. u32 class, cap;
  163. u8 type;
  164. class = read_pci_config(num, slot, func,
  165. PCI_CLASS_REVISION);
  166. if (class == 0xffffffff)
  167. break;
  168. switch (class >> 16) {
  169. case PCI_CLASS_BRIDGE_HOST:
  170. case PCI_CLASS_BRIDGE_OTHER: /* needed? */
  171. /* AGP bridge? */
  172. cap = find_cap(num, slot, func,
  173. PCI_CAP_ID_AGP);
  174. if (!cap)
  175. break;
  176. *valid_agp = 1;
  177. return read_agp(num, slot, func, cap,
  178. order);
  179. }
  180. /* No multi-function device? */
  181. type = read_pci_config_byte(num, slot, func,
  182. PCI_HEADER_TYPE);
  183. if (!(type & 0x80))
  184. break;
  185. }
  186. }
  187. }
  188. printk("No AGP bridge found\n");
  189. return 0;
  190. }
  191. void __init gart_iommu_hole_init(void)
  192. {
  193. u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0;
  194. u64 aper_base, last_aper_base = 0;
  195. int fix, num, valid_agp = 0;
  196. if (gart_iommu_aperture_disabled || !fix_aperture ||
  197. !early_pci_allowed())
  198. return;
  199. printk(KERN_INFO "Checking aperture...\n");
  200. fix = 0;
  201. for (num = 24; num < 32; num++) {
  202. if (!early_is_k8_nb(read_pci_config(0, num, 3, 0x00)))
  203. continue;
  204. iommu_detected = 1;
  205. gart_iommu_aperture = 1;
  206. aper_order = (read_pci_config(0, num, 3, 0x90) >> 1) & 7;
  207. aper_size = (32 * 1024 * 1024) << aper_order;
  208. aper_base = read_pci_config(0, num, 3, 0x94) & 0x7fff;
  209. aper_base <<= 25;
  210. printk("CPU %d: aperture @ %Lx size %u MB\n", num-24,
  211. aper_base, aper_size>>20);
  212. if (!aperture_valid(aper_base, aper_size)) {
  213. fix = 1;
  214. break;
  215. }
  216. if ((last_aper_order && aper_order != last_aper_order) ||
  217. (last_aper_base && aper_base != last_aper_base)) {
  218. fix = 1;
  219. break;
  220. }
  221. last_aper_order = aper_order;
  222. last_aper_base = aper_base;
  223. }
  224. if (!fix && !fallback_aper_force) {
  225. if (last_aper_base) {
  226. unsigned long n = (32 * 1024 * 1024) << last_aper_order;
  227. insert_aperture_resource((u32)last_aper_base, n);
  228. }
  229. return;
  230. }
  231. if (!fallback_aper_force)
  232. aper_alloc = search_agp_bridge(&aper_order, &valid_agp);
  233. if (aper_alloc) {
  234. /* Got the aperture from the AGP bridge */
  235. } else if (swiotlb && !valid_agp) {
  236. /* Do nothing */
  237. } else if ((!no_iommu && end_pfn > MAX_DMA32_PFN) ||
  238. force_iommu ||
  239. valid_agp ||
  240. fallback_aper_force) {
  241. printk("Your BIOS doesn't leave a aperture memory hole\n");
  242. printk("Please enable the IOMMU option in the BIOS setup\n");
  243. printk("This costs you %d MB of RAM\n",
  244. 32 << fallback_aper_order);
  245. aper_order = fallback_aper_order;
  246. aper_alloc = allocate_aperture();
  247. if (!aper_alloc) {
  248. /*
  249. * Could disable AGP and IOMMU here, but it's
  250. * probably not worth it. But the later users
  251. * cannot deal with bad apertures and turning
  252. * on the aperture over memory causes very
  253. * strange problems, so it's better to panic
  254. * early.
  255. */
  256. panic("Not enough memory for aperture");
  257. }
  258. } else {
  259. return;
  260. }
  261. /* Fix up the north bridges */
  262. for (num = 24; num < 32; num++) {
  263. if (!early_is_k8_nb(read_pci_config(0, num, 3, 0x00)))
  264. continue;
  265. /*
  266. * Don't enable translation yet. That is done later.
  267. * Assume this BIOS didn't initialise the GART so
  268. * just overwrite all previous bits
  269. */
  270. write_pci_config(0, num, 3, 0x90, aper_order<<1);
  271. write_pci_config(0, num, 3, 0x94, aper_alloc>>25);
  272. }
  273. }