aperture_64.c 11 KB

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