aperture_64.c 13 KB

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