aperture_64.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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(KERN_ERR
  61. "Cannot allocate aperture memory hole (%p,%uK)\n",
  62. p, aper_size>>10);
  63. if (p)
  64. free_bootmem(__pa(p), aper_size);
  65. return 0;
  66. }
  67. printk(KERN_INFO "Mapping aperture over %d KB of RAM @ %lx\n",
  68. aper_size >> 10, __pa(p));
  69. insert_aperture_resource((u32)__pa(p), aper_size);
  70. return (u32)__pa(p);
  71. }
  72. static int __init aperture_valid(u64 aper_base, u32 aper_size)
  73. {
  74. if (!aper_base)
  75. return 0;
  76. if (aper_base + aper_size > 0x100000000UL) {
  77. printk(KERN_ERR "Aperture beyond 4GB. Ignoring.\n");
  78. return 0;
  79. }
  80. if (e820_any_mapped(aper_base, aper_base + aper_size, E820_RAM)) {
  81. printk(KERN_ERR "Aperture pointing to e820 RAM. Ignoring.\n");
  82. return 0;
  83. }
  84. if (aper_size < 64*1024*1024) {
  85. printk(KERN_ERR "Aperture too small (%d MB)\n", aper_size>>20);
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. /* Find a PCI capability */
  91. static __u32 __init find_cap(int num, int slot, int func, int cap)
  92. {
  93. int bytes;
  94. u8 pos;
  95. if (!(read_pci_config_16(num, slot, func, PCI_STATUS) &
  96. PCI_STATUS_CAP_LIST))
  97. return 0;
  98. pos = read_pci_config_byte(num, slot, func, PCI_CAPABILITY_LIST);
  99. for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
  100. u8 id;
  101. pos &= ~3;
  102. id = read_pci_config_byte(num, slot, func, pos+PCI_CAP_LIST_ID);
  103. if (id == 0xff)
  104. break;
  105. if (id == cap)
  106. return pos;
  107. pos = read_pci_config_byte(num, slot, func,
  108. pos+PCI_CAP_LIST_NEXT);
  109. }
  110. return 0;
  111. }
  112. /* Read a standard AGPv3 bridge header */
  113. static __u32 __init read_agp(int num, int slot, int func, int cap, u32 *order)
  114. {
  115. u32 apsize;
  116. u32 apsizereg;
  117. int nbits;
  118. u32 aper_low, aper_hi;
  119. u64 aper;
  120. printk(KERN_INFO "AGP bridge at %02x:%02x:%02x\n", num, slot, func);
  121. apsizereg = read_pci_config_16(num, slot, func, cap + 0x14);
  122. if (apsizereg == 0xffffffff) {
  123. printk(KERN_ERR "APSIZE in AGP bridge unreadable\n");
  124. return 0;
  125. }
  126. apsize = apsizereg & 0xfff;
  127. /* Some BIOS use weird encodings not in the AGPv3 table. */
  128. if (apsize & 0xff)
  129. apsize |= 0xf00;
  130. nbits = hweight16(apsize);
  131. *order = 7 - nbits;
  132. if ((int)*order < 0) /* < 32MB */
  133. *order = 0;
  134. aper_low = read_pci_config(num, slot, func, 0x10);
  135. aper_hi = read_pci_config(num, slot, func, 0x14);
  136. aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
  137. printk(KERN_INFO "Aperture from AGP @ %Lx size %u MB (APSIZE %x)\n",
  138. aper, 32 << *order, apsizereg);
  139. if (!aperture_valid(aper, (32*1024*1024) << *order))
  140. return 0;
  141. return (u32)aper;
  142. }
  143. /*
  144. * Look for an AGP bridge. Windows only expects the aperture in the
  145. * AGP bridge and some BIOS forget to initialize the Northbridge too.
  146. * Work around this here.
  147. *
  148. * Do an PCI bus scan by hand because we're running before the PCI
  149. * subsystem.
  150. *
  151. * All K8 AGP bridges are AGPv3 compliant, so we can do this scan
  152. * generically. It's probably overkill to always scan all slots because
  153. * the AGP bridges should be always an own bus on the HT hierarchy,
  154. * but do it here for future safety.
  155. */
  156. static __u32 __init search_agp_bridge(u32 *order, int *valid_agp)
  157. {
  158. int num, slot, func;
  159. /* Poor man's PCI discovery */
  160. for (num = 0; num < 256; num++) {
  161. for (slot = 0; slot < 32; slot++) {
  162. for (func = 0; func < 8; func++) {
  163. u32 class, cap;
  164. u8 type;
  165. class = read_pci_config(num, slot, func,
  166. PCI_CLASS_REVISION);
  167. if (class == 0xffffffff)
  168. break;
  169. switch (class >> 16) {
  170. case PCI_CLASS_BRIDGE_HOST:
  171. case PCI_CLASS_BRIDGE_OTHER: /* needed? */
  172. /* AGP bridge? */
  173. cap = find_cap(num, slot, func,
  174. PCI_CAP_ID_AGP);
  175. if (!cap)
  176. break;
  177. *valid_agp = 1;
  178. return read_agp(num, slot, func, cap,
  179. order);
  180. }
  181. /* No multi-function device? */
  182. type = read_pci_config_byte(num, slot, func,
  183. PCI_HEADER_TYPE);
  184. if (!(type & 0x80))
  185. break;
  186. }
  187. }
  188. }
  189. printk(KERN_INFO "No AGP bridge found\n");
  190. return 0;
  191. }
  192. static int gart_fix_e820 __initdata = 1;
  193. static int __init parse_gart_mem(char *p)
  194. {
  195. if (!p)
  196. return -EINVAL;
  197. if (!strncmp(p, "off", 3))
  198. gart_fix_e820 = 0;
  199. else if (!strncmp(p, "on", 2))
  200. gart_fix_e820 = 1;
  201. return 0;
  202. }
  203. early_param("gart_fix_e820", parse_gart_mem);
  204. void __init early_gart_iommu_check(void)
  205. {
  206. /*
  207. * in case it is enabled before, esp for kexec/kdump,
  208. * previous kernel already enable that. memset called
  209. * by allocate_aperture/__alloc_bootmem_nopanic cause restart.
  210. * or second kernel have different position for GART hole. and new
  211. * kernel could use hole as RAM that is still used by GART set by
  212. * first kernel
  213. * or BIOS forget to put that in reserved.
  214. * try to update e820 to make that region as reserved.
  215. */
  216. int fix, num;
  217. u32 ctl;
  218. u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
  219. u64 aper_base = 0, last_aper_base = 0;
  220. int aper_enabled = 0, last_aper_enabled = 0;
  221. if (!early_pci_allowed())
  222. return;
  223. fix = 0;
  224. for (num = 24; num < 32; num++) {
  225. if (!early_is_k8_nb(read_pci_config(0, num, 3, 0x00)))
  226. continue;
  227. ctl = read_pci_config(0, num, 3, 0x90);
  228. aper_enabled = ctl & 1;
  229. aper_order = (ctl >> 1) & 7;
  230. aper_size = (32 * 1024 * 1024) << aper_order;
  231. aper_base = read_pci_config(0, num, 3, 0x94) & 0x7fff;
  232. aper_base <<= 25;
  233. if ((last_aper_order && aper_order != last_aper_order) ||
  234. (last_aper_base && aper_base != last_aper_base) ||
  235. (last_aper_enabled && aper_enabled != last_aper_enabled)) {
  236. fix = 1;
  237. break;
  238. }
  239. last_aper_order = aper_order;
  240. last_aper_base = aper_base;
  241. last_aper_enabled = aper_enabled;
  242. }
  243. if (!fix && !aper_enabled)
  244. return;
  245. if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
  246. fix = 1;
  247. if (gart_fix_e820 && !fix && aper_enabled) {
  248. if (e820_any_mapped(aper_base, aper_base + aper_size,
  249. E820_RAM)) {
  250. /* reserved it, so we can resuse it in second kernel */
  251. printk(KERN_INFO "update e820 for GART\n");
  252. add_memory_region(aper_base, aper_size, E820_RESERVED);
  253. update_e820();
  254. }
  255. return;
  256. }
  257. /* different nodes have different setting, disable them all at first*/
  258. for (num = 24; num < 32; num++) {
  259. if (!early_is_k8_nb(read_pci_config(0, num, 3, 0x00)))
  260. continue;
  261. ctl = read_pci_config(0, num, 3, 0x90);
  262. ctl &= ~1;
  263. write_pci_config(0, num, 3, 0x90, ctl);
  264. }
  265. }
  266. void __init gart_iommu_hole_init(void)
  267. {
  268. u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0;
  269. u64 aper_base, last_aper_base = 0;
  270. int fix, num, valid_agp = 0;
  271. int node;
  272. if (gart_iommu_aperture_disabled || !fix_aperture ||
  273. !early_pci_allowed())
  274. return;
  275. printk(KERN_INFO "Checking aperture...\n");
  276. fix = 0;
  277. node = 0;
  278. for (num = 24; num < 32; num++) {
  279. if (!early_is_k8_nb(read_pci_config(0, num, 3, 0x00)))
  280. continue;
  281. iommu_detected = 1;
  282. gart_iommu_aperture = 1;
  283. aper_order = (read_pci_config(0, num, 3, 0x90) >> 1) & 7;
  284. aper_size = (32 * 1024 * 1024) << aper_order;
  285. aper_base = read_pci_config(0, num, 3, 0x94) & 0x7fff;
  286. aper_base <<= 25;
  287. printk(KERN_INFO "Node %d: aperture @ %Lx size %u MB\n",
  288. node, aper_base, aper_size >> 20);
  289. node++;
  290. if (!aperture_valid(aper_base, aper_size)) {
  291. fix = 1;
  292. break;
  293. }
  294. if ((last_aper_order && aper_order != last_aper_order) ||
  295. (last_aper_base && aper_base != last_aper_base)) {
  296. fix = 1;
  297. break;
  298. }
  299. last_aper_order = aper_order;
  300. last_aper_base = aper_base;
  301. }
  302. if (!fix && !fallback_aper_force) {
  303. if (last_aper_base) {
  304. unsigned long n = (32 * 1024 * 1024) << last_aper_order;
  305. insert_aperture_resource((u32)last_aper_base, n);
  306. }
  307. return;
  308. }
  309. if (!fallback_aper_force)
  310. aper_alloc = search_agp_bridge(&aper_order, &valid_agp);
  311. if (aper_alloc) {
  312. /* Got the aperture from the AGP bridge */
  313. } else if (swiotlb && !valid_agp) {
  314. /* Do nothing */
  315. } else if ((!no_iommu && end_pfn > MAX_DMA32_PFN) ||
  316. force_iommu ||
  317. valid_agp ||
  318. fallback_aper_force) {
  319. printk(KERN_ERR
  320. "Your BIOS doesn't leave a aperture memory hole\n");
  321. printk(KERN_ERR
  322. "Please enable the IOMMU option in the BIOS setup\n");
  323. printk(KERN_ERR
  324. "This costs you %d MB of RAM\n",
  325. 32 << fallback_aper_order);
  326. aper_order = fallback_aper_order;
  327. aper_alloc = allocate_aperture();
  328. if (!aper_alloc) {
  329. /*
  330. * Could disable AGP and IOMMU here, but it's
  331. * probably not worth it. But the later users
  332. * cannot deal with bad apertures and turning
  333. * on the aperture over memory causes very
  334. * strange problems, so it's better to panic
  335. * early.
  336. */
  337. panic("Not enough memory for aperture");
  338. }
  339. } else {
  340. return;
  341. }
  342. /* Fix up the north bridges */
  343. for (num = 24; num < 32; num++) {
  344. if (!early_is_k8_nb(read_pci_config(0, num, 3, 0x00)))
  345. continue;
  346. /*
  347. * Don't enable translation yet. That is done later.
  348. * Assume this BIOS didn't initialise the GART so
  349. * just overwrite all previous bits
  350. */
  351. write_pci_config(0, num, 3, 0x90, aper_order<<1);
  352. write_pci_config(0, num, 3, 0x94, aper_alloc>>25);
  353. }
  354. }