aperture_64.c 14 KB

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