aperture_64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 bool gart_fix_e820 __initdata = true;
  202. static int __init parse_gart_mem(char *p)
  203. {
  204. return kstrtobool(p, &gart_fix_e820);
  205. }
  206. early_param("gart_fix_e820", parse_gart_mem);
  207. void __init early_gart_iommu_check(void)
  208. {
  209. /*
  210. * in case it is enabled before, esp for kexec/kdump,
  211. * previous kernel already enable that. memset called
  212. * by allocate_aperture/__alloc_bootmem_nopanic cause restart.
  213. * or second kernel have different position for GART hole. and new
  214. * kernel could use hole as RAM that is still used by GART set by
  215. * first kernel
  216. * or BIOS forget to put that in reserved.
  217. * try to update e820 to make that region as reserved.
  218. */
  219. u32 agp_aper_order = 0;
  220. int i, fix, slot, valid_agp = 0;
  221. u32 ctl;
  222. u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
  223. u64 aper_base = 0, last_aper_base = 0;
  224. int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0;
  225. if (!amd_gart_present())
  226. return;
  227. if (!early_pci_allowed())
  228. return;
  229. /* This is mostly duplicate of iommu_hole_init */
  230. search_agp_bridge(&agp_aper_order, &valid_agp);
  231. fix = 0;
  232. for (i = 0; amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  233. int bus;
  234. int dev_base, dev_limit;
  235. bus = amd_nb_bus_dev_ranges[i].bus;
  236. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  237. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  238. for (slot = dev_base; slot < dev_limit; slot++) {
  239. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  240. continue;
  241. ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
  242. aper_enabled = ctl & GARTEN;
  243. aper_order = (ctl >> 1) & 7;
  244. aper_size = (32 * 1024 * 1024) << aper_order;
  245. aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
  246. aper_base <<= 25;
  247. if (last_valid) {
  248. if ((aper_order != last_aper_order) ||
  249. (aper_base != last_aper_base) ||
  250. (aper_enabled != last_aper_enabled)) {
  251. fix = 1;
  252. break;
  253. }
  254. }
  255. last_aper_order = aper_order;
  256. last_aper_base = aper_base;
  257. last_aper_enabled = aper_enabled;
  258. last_valid = 1;
  259. }
  260. }
  261. if (!fix && !aper_enabled)
  262. return;
  263. if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
  264. fix = 1;
  265. if (gart_fix_e820 && !fix && aper_enabled) {
  266. if (e820_any_mapped(aper_base, aper_base + aper_size,
  267. E820_RAM)) {
  268. /* reserve it, so we can reuse it in second kernel */
  269. pr_info("e820: reserve [mem %#010Lx-%#010Lx] for GART\n",
  270. aper_base, aper_base + aper_size - 1);
  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 (!amd_gart_present())
  302. return -ENODEV;
  303. if (gart_iommu_aperture_disabled || !fix_aperture ||
  304. !early_pci_allowed())
  305. return -ENODEV;
  306. pr_info("Checking aperture...\n");
  307. if (!fallback_aper_force)
  308. agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp);
  309. fix = 0;
  310. node = 0;
  311. for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  312. int bus;
  313. int dev_base, dev_limit;
  314. u32 ctl;
  315. bus = amd_nb_bus_dev_ranges[i].bus;
  316. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  317. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  318. for (slot = dev_base; slot < dev_limit; slot++) {
  319. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  320. continue;
  321. iommu_detected = 1;
  322. gart_iommu_aperture = 1;
  323. x86_init.iommu.iommu_init = gart_iommu_init;
  324. ctl = read_pci_config(bus, slot, 3,
  325. AMD64_GARTAPERTURECTL);
  326. /*
  327. * Before we do anything else disable the GART. It may
  328. * still be enabled if we boot into a crash-kernel here.
  329. * Reconfiguring the GART while it is enabled could have
  330. * unknown side-effects.
  331. */
  332. ctl &= ~GARTEN;
  333. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  334. aper_order = (ctl >> 1) & 7;
  335. aper_size = (32 * 1024 * 1024) << aper_order;
  336. aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
  337. aper_base <<= 25;
  338. pr_info("Node %d: aperture [bus addr %#010Lx-%#010Lx] (%uMB)\n",
  339. node, aper_base, aper_base + aper_size - 1,
  340. aper_size >> 20);
  341. node++;
  342. if (!aperture_valid(aper_base, aper_size, 64<<20)) {
  343. if (valid_agp && agp_aper_base &&
  344. agp_aper_base == aper_base &&
  345. agp_aper_order == aper_order) {
  346. /* the same between two setting from NB and agp */
  347. if (!no_iommu &&
  348. max_pfn > MAX_DMA32_PFN &&
  349. !printed_gart_size_msg) {
  350. pr_err("you are using iommu with agp, but GART size is less than 64MB\n");
  351. pr_err("please increase GART size in your BIOS setup\n");
  352. pr_err("if BIOS doesn't have that option, contact your HW vendor!\n");
  353. printed_gart_size_msg = 1;
  354. }
  355. } else {
  356. fix = 1;
  357. goto out;
  358. }
  359. }
  360. if ((last_aper_order && aper_order != last_aper_order) ||
  361. (last_aper_base && aper_base != last_aper_base)) {
  362. fix = 1;
  363. goto out;
  364. }
  365. last_aper_order = aper_order;
  366. last_aper_base = aper_base;
  367. }
  368. }
  369. out:
  370. if (!fix && !fallback_aper_force) {
  371. if (last_aper_base)
  372. return 1;
  373. return 0;
  374. }
  375. if (!fallback_aper_force) {
  376. aper_alloc = agp_aper_base;
  377. aper_order = agp_aper_order;
  378. }
  379. if (aper_alloc) {
  380. /* Got the aperture from the AGP bridge */
  381. } else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) ||
  382. force_iommu ||
  383. valid_agp ||
  384. fallback_aper_force) {
  385. pr_info("Your BIOS doesn't leave an aperture memory hole\n");
  386. pr_info("Please enable the IOMMU option in the BIOS setup\n");
  387. pr_info("This costs you %dMB 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. }