aperture_64.c 14 KB

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