aperture_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Firmware replacement code.
  3. *
  4. * Work around broken BIOSes that don't set an aperture, only set the
  5. * aperture in the AGP bridge, or set too small aperture.
  6. *
  7. * If all fails map the aperture over some low memory. This is cheaper than
  8. * doing bounce buffering. The memory is lost. This is done at early boot
  9. * because only the bootmem allocator can allocate 32+MB.
  10. *
  11. * Copyright 2002 Andi Kleen, SuSE Labs.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/init.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/mmzone.h>
  18. #include <linux/pci_ids.h>
  19. #include <linux/pci.h>
  20. #include <linux/bitops.h>
  21. #include <linux/ioport.h>
  22. #include <linux/suspend.h>
  23. #include <linux/kmemleak.h>
  24. #include <asm/e820.h>
  25. #include <asm/io.h>
  26. #include <asm/iommu.h>
  27. #include <asm/gart.h>
  28. #include <asm/pci-direct.h>
  29. #include <asm/dma.h>
  30. #include <asm/k8.h>
  31. int gart_iommu_aperture;
  32. int gart_iommu_aperture_disabled __initdata;
  33. int gart_iommu_aperture_allowed __initdata;
  34. int fallback_aper_order __initdata = 1; /* 64MB */
  35. int fallback_aper_force __initdata;
  36. int fix_aperture __initdata = 1;
  37. struct bus_dev_range {
  38. int bus;
  39. int dev_base;
  40. int dev_limit;
  41. };
  42. static struct bus_dev_range bus_dev_ranges[] __initdata = {
  43. { 0x00, 0x18, 0x20},
  44. { 0xff, 0x00, 0x20},
  45. { 0xfe, 0x00, 0x20}
  46. };
  47. static struct resource gart_resource = {
  48. .name = "GART",
  49. .flags = IORESOURCE_MEM,
  50. };
  51. static void __init insert_aperture_resource(u32 aper_base, u32 aper_size)
  52. {
  53. gart_resource.start = aper_base;
  54. gart_resource.end = aper_base + aper_size - 1;
  55. insert_resource(&iomem_resource, &gart_resource);
  56. }
  57. /* This code runs before the PCI subsystem is initialized, so just
  58. access the northbridge directly. */
  59. static u32 __init allocate_aperture(void)
  60. {
  61. u32 aper_size;
  62. void *p;
  63. /* aper_size should <= 1G */
  64. if (fallback_aper_order > 5)
  65. fallback_aper_order = 5;
  66. aper_size = (32 * 1024 * 1024) << fallback_aper_order;
  67. /*
  68. * Aperture has to be naturally aligned. This means a 2GB aperture
  69. * won't have much chance of finding a place in the lower 4GB of
  70. * memory. Unfortunately we cannot move it up because that would
  71. * make the IOMMU useless.
  72. */
  73. /*
  74. * using 512M as goal, in case kexec will load kernel_big
  75. * that will do the on position decompress, and could overlap with
  76. * that positon with gart that is used.
  77. * sequende:
  78. * kernel_small
  79. * ==> kexec (with kdump trigger path or previous doesn't shutdown gart)
  80. * ==> kernel_small(gart area become e820_reserved)
  81. * ==> kexec (with kdump trigger path or previous doesn't shutdown gart)
  82. * ==> kerne_big (uncompressed size will be big than 64M or 128M)
  83. * so don't use 512M below as gart iommu, leave the space for kernel
  84. * code for safe
  85. */
  86. p = __alloc_bootmem_nopanic(aper_size, aper_size, 512ULL<<20);
  87. /*
  88. * Kmemleak should not scan this block as it may not be mapped via the
  89. * kernel direct mapping.
  90. */
  91. kmemleak_ignore(p);
  92. if (!p || __pa(p)+aper_size > 0xffffffff) {
  93. printk(KERN_ERR
  94. "Cannot allocate aperture memory hole (%p,%uK)\n",
  95. p, aper_size>>10);
  96. if (p)
  97. free_bootmem(__pa(p), aper_size);
  98. return 0;
  99. }
  100. printk(KERN_INFO "Mapping aperture over %d KB of RAM @ %lx\n",
  101. aper_size >> 10, __pa(p));
  102. insert_aperture_resource((u32)__pa(p), aper_size);
  103. register_nosave_region((u32)__pa(p) >> PAGE_SHIFT,
  104. (u32)__pa(p+aper_size) >> PAGE_SHIFT);
  105. return (u32)__pa(p);
  106. }
  107. /* Find a PCI capability */
  108. static u32 __init find_cap(int bus, int slot, int func, int cap)
  109. {
  110. int bytes;
  111. u8 pos;
  112. if (!(read_pci_config_16(bus, slot, func, PCI_STATUS) &
  113. PCI_STATUS_CAP_LIST))
  114. return 0;
  115. pos = read_pci_config_byte(bus, slot, func, PCI_CAPABILITY_LIST);
  116. for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
  117. u8 id;
  118. pos &= ~3;
  119. id = read_pci_config_byte(bus, slot, func, pos+PCI_CAP_LIST_ID);
  120. if (id == 0xff)
  121. break;
  122. if (id == cap)
  123. return pos;
  124. pos = read_pci_config_byte(bus, slot, func,
  125. pos+PCI_CAP_LIST_NEXT);
  126. }
  127. return 0;
  128. }
  129. /* Read a standard AGPv3 bridge header */
  130. static u32 __init read_agp(int bus, int slot, int func, int cap, u32 *order)
  131. {
  132. u32 apsize;
  133. u32 apsizereg;
  134. int nbits;
  135. u32 aper_low, aper_hi;
  136. u64 aper;
  137. u32 old_order;
  138. printk(KERN_INFO "AGP bridge at %02x:%02x:%02x\n", bus, slot, func);
  139. apsizereg = read_pci_config_16(bus, slot, func, cap + 0x14);
  140. if (apsizereg == 0xffffffff) {
  141. printk(KERN_ERR "APSIZE in AGP bridge unreadable\n");
  142. return 0;
  143. }
  144. /* old_order could be the value from NB gart setting */
  145. old_order = *order;
  146. apsize = apsizereg & 0xfff;
  147. /* Some BIOS use weird encodings not in the AGPv3 table. */
  148. if (apsize & 0xff)
  149. apsize |= 0xf00;
  150. nbits = hweight16(apsize);
  151. *order = 7 - nbits;
  152. if ((int)*order < 0) /* < 32MB */
  153. *order = 0;
  154. aper_low = read_pci_config(bus, slot, func, 0x10);
  155. aper_hi = read_pci_config(bus, slot, func, 0x14);
  156. aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
  157. /*
  158. * On some sick chips, APSIZE is 0. It means it wants 4G
  159. * so let double check that order, and lets trust AMD NB settings:
  160. */
  161. printk(KERN_INFO "Aperture from AGP @ %Lx old size %u MB\n",
  162. aper, 32 << old_order);
  163. if (aper + (32ULL<<(20 + *order)) > 0x100000000ULL) {
  164. printk(KERN_INFO "Aperture size %u MB (APSIZE %x) is not right, using settings from NB\n",
  165. 32 << *order, apsizereg);
  166. *order = old_order;
  167. }
  168. printk(KERN_INFO "Aperture from AGP @ %Lx size %u MB (APSIZE %x)\n",
  169. aper, 32 << *order, apsizereg);
  170. if (!aperture_valid(aper, (32*1024*1024) << *order, 32<<20))
  171. return 0;
  172. return (u32)aper;
  173. }
  174. /*
  175. * Look for an AGP bridge. Windows only expects the aperture in the
  176. * AGP bridge and some BIOS forget to initialize the Northbridge too.
  177. * Work around this here.
  178. *
  179. * Do an PCI bus scan by hand because we're running before the PCI
  180. * subsystem.
  181. *
  182. * All K8 AGP bridges are AGPv3 compliant, so we can do this scan
  183. * generically. It's probably overkill to always scan all slots because
  184. * the AGP bridges should be always an own bus on the HT hierarchy,
  185. * but do it here for future safety.
  186. */
  187. static u32 __init search_agp_bridge(u32 *order, int *valid_agp)
  188. {
  189. int bus, slot, func;
  190. /* Poor man's PCI discovery */
  191. for (bus = 0; bus < 256; bus++) {
  192. for (slot = 0; slot < 32; slot++) {
  193. for (func = 0; func < 8; func++) {
  194. u32 class, cap;
  195. u8 type;
  196. class = read_pci_config(bus, slot, func,
  197. PCI_CLASS_REVISION);
  198. if (class == 0xffffffff)
  199. break;
  200. switch (class >> 16) {
  201. case PCI_CLASS_BRIDGE_HOST:
  202. case PCI_CLASS_BRIDGE_OTHER: /* needed? */
  203. /* AGP bridge? */
  204. cap = find_cap(bus, slot, func,
  205. PCI_CAP_ID_AGP);
  206. if (!cap)
  207. break;
  208. *valid_agp = 1;
  209. return read_agp(bus, slot, func, cap,
  210. order);
  211. }
  212. /* No multi-function device? */
  213. type = read_pci_config_byte(bus, slot, func,
  214. PCI_HEADER_TYPE);
  215. if (!(type & 0x80))
  216. break;
  217. }
  218. }
  219. }
  220. printk(KERN_INFO "No AGP bridge found\n");
  221. return 0;
  222. }
  223. static int gart_fix_e820 __initdata = 1;
  224. static int __init parse_gart_mem(char *p)
  225. {
  226. if (!p)
  227. return -EINVAL;
  228. if (!strncmp(p, "off", 3))
  229. gart_fix_e820 = 0;
  230. else if (!strncmp(p, "on", 2))
  231. gart_fix_e820 = 1;
  232. return 0;
  233. }
  234. early_param("gart_fix_e820", parse_gart_mem);
  235. void __init early_gart_iommu_check(void)
  236. {
  237. /*
  238. * in case it is enabled before, esp for kexec/kdump,
  239. * previous kernel already enable that. memset called
  240. * by allocate_aperture/__alloc_bootmem_nopanic cause restart.
  241. * or second kernel have different position for GART hole. and new
  242. * kernel could use hole as RAM that is still used by GART set by
  243. * first kernel
  244. * or BIOS forget to put that in reserved.
  245. * try to update e820 to make that region as reserved.
  246. */
  247. int i, fix, slot;
  248. u32 ctl;
  249. u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
  250. u64 aper_base = 0, last_aper_base = 0;
  251. int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0;
  252. if (!early_pci_allowed())
  253. return;
  254. /* This is mostly duplicate of iommu_hole_init */
  255. fix = 0;
  256. for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) {
  257. int bus;
  258. int dev_base, dev_limit;
  259. bus = bus_dev_ranges[i].bus;
  260. dev_base = bus_dev_ranges[i].dev_base;
  261. dev_limit = bus_dev_ranges[i].dev_limit;
  262. for (slot = dev_base; slot < dev_limit; slot++) {
  263. if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00)))
  264. continue;
  265. ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
  266. aper_enabled = ctl & AMD64_GARTEN;
  267. aper_order = (ctl >> 1) & 7;
  268. aper_size = (32 * 1024 * 1024) << aper_order;
  269. aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
  270. aper_base <<= 25;
  271. if (last_valid) {
  272. if ((aper_order != last_aper_order) ||
  273. (aper_base != last_aper_base) ||
  274. (aper_enabled != last_aper_enabled)) {
  275. fix = 1;
  276. break;
  277. }
  278. }
  279. last_aper_order = aper_order;
  280. last_aper_base = aper_base;
  281. last_aper_enabled = aper_enabled;
  282. last_valid = 1;
  283. }
  284. }
  285. if (!fix && !aper_enabled)
  286. return;
  287. if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
  288. fix = 1;
  289. if (gart_fix_e820 && !fix && aper_enabled) {
  290. if (e820_any_mapped(aper_base, aper_base + aper_size,
  291. E820_RAM)) {
  292. /* reserve it, so we can reuse it in second kernel */
  293. printk(KERN_INFO "update e820 for GART\n");
  294. e820_add_region(aper_base, aper_size, E820_RESERVED);
  295. update_e820();
  296. }
  297. }
  298. if (!fix)
  299. return;
  300. /* different nodes have different setting, disable them all at first*/
  301. for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) {
  302. int bus;
  303. int dev_base, dev_limit;
  304. bus = bus_dev_ranges[i].bus;
  305. dev_base = bus_dev_ranges[i].dev_base;
  306. dev_limit = bus_dev_ranges[i].dev_limit;
  307. for (slot = dev_base; slot < dev_limit; slot++) {
  308. if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00)))
  309. continue;
  310. ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
  311. ctl &= ~AMD64_GARTEN;
  312. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  313. }
  314. }
  315. }
  316. static int __initdata printed_gart_size_msg;
  317. void __init gart_iommu_hole_init(void)
  318. {
  319. u32 agp_aper_base = 0, agp_aper_order = 0;
  320. u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0;
  321. u64 aper_base, last_aper_base = 0;
  322. int fix, slot, valid_agp = 0;
  323. int i, node;
  324. if (gart_iommu_aperture_disabled || !fix_aperture ||
  325. !early_pci_allowed())
  326. return;
  327. printk(KERN_INFO "Checking aperture...\n");
  328. if (!fallback_aper_force)
  329. agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp);
  330. fix = 0;
  331. node = 0;
  332. for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) {
  333. int bus;
  334. int dev_base, dev_limit;
  335. bus = bus_dev_ranges[i].bus;
  336. dev_base = bus_dev_ranges[i].dev_base;
  337. dev_limit = bus_dev_ranges[i].dev_limit;
  338. for (slot = dev_base; slot < dev_limit; slot++) {
  339. if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00)))
  340. continue;
  341. iommu_detected = 1;
  342. gart_iommu_aperture = 1;
  343. aper_order = (read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL) >> 1) & 7;
  344. aper_size = (32 * 1024 * 1024) << aper_order;
  345. aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
  346. aper_base <<= 25;
  347. printk(KERN_INFO "Node %d: aperture @ %Lx size %u MB\n",
  348. node, aper_base, aper_size >> 20);
  349. node++;
  350. if (!aperture_valid(aper_base, aper_size, 64<<20)) {
  351. if (valid_agp && agp_aper_base &&
  352. agp_aper_base == aper_base &&
  353. agp_aper_order == aper_order) {
  354. /* the same between two setting from NB and agp */
  355. if (!no_iommu &&
  356. max_pfn > MAX_DMA32_PFN &&
  357. !printed_gart_size_msg) {
  358. printk(KERN_ERR "you are using iommu with agp, but GART size is less than 64M\n");
  359. printk(KERN_ERR "please increase GART size in your BIOS setup\n");
  360. printk(KERN_ERR "if BIOS doesn't have that option, contact your HW vendor!\n");
  361. printed_gart_size_msg = 1;
  362. }
  363. } else {
  364. fix = 1;
  365. goto out;
  366. }
  367. }
  368. if ((last_aper_order && aper_order != last_aper_order) ||
  369. (last_aper_base && aper_base != last_aper_base)) {
  370. fix = 1;
  371. goto out;
  372. }
  373. last_aper_order = aper_order;
  374. last_aper_base = aper_base;
  375. }
  376. }
  377. out:
  378. if (!fix && !fallback_aper_force) {
  379. if (last_aper_base) {
  380. unsigned long n = (32 * 1024 * 1024) << last_aper_order;
  381. insert_aperture_resource((u32)last_aper_base, n);
  382. }
  383. return;
  384. }
  385. if (!fallback_aper_force) {
  386. aper_alloc = agp_aper_base;
  387. aper_order = agp_aper_order;
  388. }
  389. if (aper_alloc) {
  390. /* Got the aperture from the AGP bridge */
  391. } else if (swiotlb && !valid_agp) {
  392. /* Do nothing */
  393. } else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) ||
  394. force_iommu ||
  395. valid_agp ||
  396. fallback_aper_force) {
  397. printk(KERN_INFO
  398. "Your BIOS doesn't leave a aperture memory hole\n");
  399. printk(KERN_INFO
  400. "Please enable the IOMMU option in the BIOS setup\n");
  401. printk(KERN_INFO
  402. "This costs you %d MB of RAM\n",
  403. 32 << fallback_aper_order);
  404. aper_order = fallback_aper_order;
  405. aper_alloc = allocate_aperture();
  406. if (!aper_alloc) {
  407. /*
  408. * Could disable AGP and IOMMU here, but it's
  409. * probably not worth it. But the later users
  410. * cannot deal with bad apertures and turning
  411. * on the aperture over memory causes very
  412. * strange problems, so it's better to panic
  413. * early.
  414. */
  415. panic("Not enough memory for aperture");
  416. }
  417. } else {
  418. return;
  419. }
  420. /* Fix up the north bridges */
  421. for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) {
  422. int bus;
  423. int dev_base, dev_limit;
  424. bus = bus_dev_ranges[i].bus;
  425. dev_base = bus_dev_ranges[i].dev_base;
  426. dev_limit = bus_dev_ranges[i].dev_limit;
  427. for (slot = dev_base; slot < dev_limit; slot++) {
  428. if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00)))
  429. continue;
  430. /* Don't enable translation yet. That is done later.
  431. Assume this BIOS didn't initialise the GART so
  432. just overwrite all previous bits */
  433. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, aper_order << 1);
  434. write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25);
  435. }
  436. }
  437. set_up_gart_resume(aper_order, aper_alloc);
  438. }