aperture_64.c 14 KB

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