aperture_64.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Firmware replacement code.
  4. *
  5. * Work around broken BIOSes that don't set an aperture, only set the
  6. * aperture in the AGP bridge, or set too small aperture.
  7. *
  8. * If all fails map the aperture over some low memory. This is cheaper than
  9. * doing bounce buffering. The memory is lost. This is done at early boot
  10. * because only the bootmem allocator can allocate 32+MB.
  11. *
  12. * Copyright 2002 Andi Kleen, SuSE Labs.
  13. */
  14. #define pr_fmt(fmt) "AGP: " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/init.h>
  18. #include <linux/memblock.h>
  19. #include <linux/mmzone.h>
  20. #include <linux/pci_ids.h>
  21. #include <linux/pci.h>
  22. #include <linux/bitops.h>
  23. #include <linux/suspend.h>
  24. #include <asm/e820/api.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/amd_nb.h>
  31. #include <asm/x86_init.h>
  32. #include <linux/crash_dump.h>
  33. /*
  34. * Using 512M as goal, in case kexec will load kernel_big
  35. * that will do the on-position decompress, and could overlap with
  36. * with the gart aperture that is used.
  37. * Sequence:
  38. * kernel_small
  39. * ==> kexec (with kdump trigger path or gart still enabled)
  40. * ==> kernel_small (gart area become e820_reserved)
  41. * ==> kexec (with kdump trigger path or gart still enabled)
  42. * ==> kerne_big (uncompressed size will be big than 64M or 128M)
  43. * So don't use 512M below as gart iommu, leave the space for kernel
  44. * code for safe.
  45. */
  46. #define GART_MIN_ADDR (512ULL << 20)
  47. #define GART_MAX_ADDR (1ULL << 32)
  48. int gart_iommu_aperture;
  49. int gart_iommu_aperture_disabled __initdata;
  50. int gart_iommu_aperture_allowed __initdata;
  51. int fallback_aper_order __initdata = 1; /* 64MB */
  52. int fallback_aper_force __initdata;
  53. int fix_aperture __initdata = 1;
  54. #ifdef CONFIG_PROC_VMCORE
  55. /*
  56. * If the first kernel maps the aperture over e820 RAM, the kdump kernel will
  57. * use the same range because it will remain configured in the northbridge.
  58. * Trying to dump this area via /proc/vmcore may crash the machine, so exclude
  59. * it from vmcore.
  60. */
  61. static unsigned long aperture_pfn_start, aperture_page_count;
  62. static int gart_oldmem_pfn_is_ram(unsigned long pfn)
  63. {
  64. return likely((pfn < aperture_pfn_start) ||
  65. (pfn >= aperture_pfn_start + aperture_page_count));
  66. }
  67. static void exclude_from_vmcore(u64 aper_base, u32 aper_order)
  68. {
  69. aperture_pfn_start = aper_base >> PAGE_SHIFT;
  70. aperture_page_count = (32 * 1024 * 1024) << aper_order >> PAGE_SHIFT;
  71. WARN_ON(register_oldmem_pfn_is_ram(&gart_oldmem_pfn_is_ram));
  72. }
  73. #else
  74. static void exclude_from_vmcore(u64 aper_base, u32 aper_order)
  75. {
  76. }
  77. #endif
  78. /* This code runs before the PCI subsystem is initialized, so just
  79. access the northbridge directly. */
  80. static u32 __init allocate_aperture(void)
  81. {
  82. u32 aper_size;
  83. unsigned long addr;
  84. /* aper_size should <= 1G */
  85. if (fallback_aper_order > 5)
  86. fallback_aper_order = 5;
  87. aper_size = (32 * 1024 * 1024) << fallback_aper_order;
  88. /*
  89. * Aperture has to be naturally aligned. This means a 2GB aperture
  90. * won't have much chance of finding a place in the lower 4GB of
  91. * memory. Unfortunately we cannot move it up because that would
  92. * make the IOMMU useless.
  93. */
  94. addr = memblock_find_in_range(GART_MIN_ADDR, GART_MAX_ADDR,
  95. aper_size, aper_size);
  96. if (!addr) {
  97. pr_err("Cannot allocate aperture memory hole [mem %#010lx-%#010lx] (%uKB)\n",
  98. addr, addr + aper_size - 1, aper_size >> 10);
  99. return 0;
  100. }
  101. memblock_reserve(addr, aper_size);
  102. pr_info("Mapping aperture over RAM [mem %#010lx-%#010lx] (%uKB)\n",
  103. addr, addr + aper_size - 1, aper_size >> 10);
  104. register_nosave_region(addr >> PAGE_SHIFT,
  105. (addr+aper_size) >> PAGE_SHIFT);
  106. return (u32)addr;
  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. pr_info("pci 0000:%02x:%02x:%02x: AGP bridge\n", bus, slot, func);
  140. apsizereg = read_pci_config_16(bus, slot, func, cap + 0x14);
  141. if (apsizereg == 0xffffffff) {
  142. pr_err("pci 0000:%02x:%02x.%d: APSIZE unreadable\n",
  143. bus, slot, func);
  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. pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (old size %uMB)\n",
  164. bus, slot, func, aper, aper + (32ULL << (old_order + 20)) - 1,
  165. 32 << old_order);
  166. if (aper + (32ULL<<(20 + *order)) > 0x100000000ULL) {
  167. pr_info("pci 0000:%02x:%02x.%d: AGP aperture size %uMB (APSIZE %#x) is not right, using settings from NB\n",
  168. bus, slot, func, 32 << *order, apsizereg);
  169. *order = old_order;
  170. }
  171. pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (%uMB, APSIZE %#x)\n",
  172. bus, slot, func, aper, aper + (32ULL << (*order + 20)) - 1,
  173. 32 << *order, apsizereg);
  174. if (!aperture_valid(aper, (32*1024*1024) << *order, 32<<20))
  175. return 0;
  176. return (u32)aper;
  177. }
  178. /*
  179. * Look for an AGP bridge. Windows only expects the aperture in the
  180. * AGP bridge and some BIOS forget to initialize the Northbridge too.
  181. * Work around this here.
  182. *
  183. * Do an PCI bus scan by hand because we're running before the PCI
  184. * subsystem.
  185. *
  186. * All AMD AGP bridges are AGPv3 compliant, so we can do this scan
  187. * generically. It's probably overkill to always scan all slots because
  188. * the AGP bridges should be always an own bus on the HT hierarchy,
  189. * but do it here for future safety.
  190. */
  191. static u32 __init search_agp_bridge(u32 *order, int *valid_agp)
  192. {
  193. int bus, slot, func;
  194. /* Poor man's PCI discovery */
  195. for (bus = 0; bus < 256; bus++) {
  196. for (slot = 0; slot < 32; slot++) {
  197. for (func = 0; func < 8; func++) {
  198. u32 class, cap;
  199. u8 type;
  200. class = read_pci_config(bus, slot, func,
  201. PCI_CLASS_REVISION);
  202. if (class == 0xffffffff)
  203. break;
  204. switch (class >> 16) {
  205. case PCI_CLASS_BRIDGE_HOST:
  206. case PCI_CLASS_BRIDGE_OTHER: /* needed? */
  207. /* AGP bridge? */
  208. cap = find_cap(bus, slot, func,
  209. PCI_CAP_ID_AGP);
  210. if (!cap)
  211. break;
  212. *valid_agp = 1;
  213. return read_agp(bus, slot, func, cap,
  214. order);
  215. }
  216. /* No multi-function device? */
  217. type = read_pci_config_byte(bus, slot, func,
  218. PCI_HEADER_TYPE);
  219. if (!(type & 0x80))
  220. break;
  221. }
  222. }
  223. }
  224. pr_info("No AGP bridge found\n");
  225. return 0;
  226. }
  227. static bool gart_fix_e820 __initdata = true;
  228. static int __init parse_gart_mem(char *p)
  229. {
  230. return kstrtobool(p, &gart_fix_e820);
  231. }
  232. early_param("gart_fix_e820", parse_gart_mem);
  233. void __init early_gart_iommu_check(void)
  234. {
  235. /*
  236. * in case it is enabled before, esp for kexec/kdump,
  237. * previous kernel already enable that. memset called
  238. * by allocate_aperture/__alloc_bootmem_nopanic cause restart.
  239. * or second kernel have different position for GART hole. and new
  240. * kernel could use hole as RAM that is still used by GART set by
  241. * first kernel
  242. * or BIOS forget to put that in reserved.
  243. * try to update e820 to make that region as reserved.
  244. */
  245. u32 agp_aper_order = 0;
  246. int i, fix, slot, valid_agp = 0;
  247. u32 ctl;
  248. u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
  249. u64 aper_base = 0, last_aper_base = 0;
  250. int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0;
  251. if (!amd_gart_present())
  252. return;
  253. if (!early_pci_allowed())
  254. return;
  255. /* This is mostly duplicate of iommu_hole_init */
  256. search_agp_bridge(&agp_aper_order, &valid_agp);
  257. fix = 0;
  258. for (i = 0; amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  259. int bus;
  260. int dev_base, dev_limit;
  261. bus = amd_nb_bus_dev_ranges[i].bus;
  262. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  263. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  264. for (slot = dev_base; slot < dev_limit; slot++) {
  265. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  266. continue;
  267. ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
  268. aper_enabled = ctl & GARTEN;
  269. aper_order = (ctl >> 1) & 7;
  270. aper_size = (32 * 1024 * 1024) << aper_order;
  271. aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
  272. aper_base <<= 25;
  273. if (last_valid) {
  274. if ((aper_order != last_aper_order) ||
  275. (aper_base != last_aper_base) ||
  276. (aper_enabled != last_aper_enabled)) {
  277. fix = 1;
  278. break;
  279. }
  280. }
  281. last_aper_order = aper_order;
  282. last_aper_base = aper_base;
  283. last_aper_enabled = aper_enabled;
  284. last_valid = 1;
  285. }
  286. }
  287. if (!fix && !aper_enabled)
  288. return;
  289. if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
  290. fix = 1;
  291. if (gart_fix_e820 && !fix && aper_enabled) {
  292. if (e820__mapped_any(aper_base, aper_base + aper_size,
  293. E820_TYPE_RAM)) {
  294. /* reserve it, so we can reuse it in second kernel */
  295. pr_info("e820: reserve [mem %#010Lx-%#010Lx] for GART\n",
  296. aper_base, aper_base + aper_size - 1);
  297. e820__range_add(aper_base, aper_size, E820_TYPE_RESERVED);
  298. e820__update_table_print();
  299. }
  300. }
  301. if (valid_agp)
  302. return;
  303. /* disable them all at first */
  304. for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  305. int bus;
  306. int dev_base, dev_limit;
  307. bus = amd_nb_bus_dev_ranges[i].bus;
  308. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  309. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  310. for (slot = dev_base; slot < dev_limit; slot++) {
  311. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  312. continue;
  313. ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
  314. ctl &= ~GARTEN;
  315. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  316. }
  317. }
  318. }
  319. static int __initdata printed_gart_size_msg;
  320. int __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 (!amd_gart_present())
  328. return -ENODEV;
  329. if (gart_iommu_aperture_disabled || !fix_aperture ||
  330. !early_pci_allowed())
  331. return -ENODEV;
  332. pr_info("Checking aperture...\n");
  333. if (!fallback_aper_force)
  334. agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp);
  335. fix = 0;
  336. node = 0;
  337. for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  338. int bus;
  339. int dev_base, dev_limit;
  340. u32 ctl;
  341. bus = amd_nb_bus_dev_ranges[i].bus;
  342. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  343. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  344. for (slot = dev_base; slot < dev_limit; slot++) {
  345. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  346. continue;
  347. iommu_detected = 1;
  348. gart_iommu_aperture = 1;
  349. x86_init.iommu.iommu_init = gart_iommu_init;
  350. ctl = read_pci_config(bus, slot, 3,
  351. AMD64_GARTAPERTURECTL);
  352. /*
  353. * Before we do anything else disable the GART. It may
  354. * still be enabled if we boot into a crash-kernel here.
  355. * Reconfiguring the GART while it is enabled could have
  356. * unknown side-effects.
  357. */
  358. ctl &= ~GARTEN;
  359. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  360. aper_order = (ctl >> 1) & 7;
  361. aper_size = (32 * 1024 * 1024) << aper_order;
  362. aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
  363. aper_base <<= 25;
  364. pr_info("Node %d: aperture [bus addr %#010Lx-%#010Lx] (%uMB)\n",
  365. node, aper_base, aper_base + aper_size - 1,
  366. aper_size >> 20);
  367. node++;
  368. if (!aperture_valid(aper_base, aper_size, 64<<20)) {
  369. if (valid_agp && agp_aper_base &&
  370. agp_aper_base == aper_base &&
  371. agp_aper_order == aper_order) {
  372. /* the same between two setting from NB and agp */
  373. if (!no_iommu &&
  374. max_pfn > MAX_DMA32_PFN &&
  375. !printed_gart_size_msg) {
  376. pr_err("you are using iommu with agp, but GART size is less than 64MB\n");
  377. pr_err("please increase GART size in your BIOS setup\n");
  378. pr_err("if BIOS doesn't have that option, contact your HW vendor!\n");
  379. printed_gart_size_msg = 1;
  380. }
  381. } else {
  382. fix = 1;
  383. goto out;
  384. }
  385. }
  386. if ((last_aper_order && aper_order != last_aper_order) ||
  387. (last_aper_base && aper_base != last_aper_base)) {
  388. fix = 1;
  389. goto out;
  390. }
  391. last_aper_order = aper_order;
  392. last_aper_base = aper_base;
  393. }
  394. }
  395. out:
  396. if (!fix && !fallback_aper_force) {
  397. if (last_aper_base) {
  398. /*
  399. * If this is the kdump kernel, the first kernel
  400. * may have allocated the range over its e820 RAM
  401. * and fixed up the northbridge
  402. */
  403. exclude_from_vmcore(last_aper_base, last_aper_order);
  404. return 1;
  405. }
  406. return 0;
  407. }
  408. if (!fallback_aper_force) {
  409. aper_alloc = agp_aper_base;
  410. aper_order = agp_aper_order;
  411. }
  412. if (aper_alloc) {
  413. /* Got the aperture from the AGP bridge */
  414. } else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) ||
  415. force_iommu ||
  416. valid_agp ||
  417. fallback_aper_force) {
  418. pr_info("Your BIOS doesn't leave an aperture memory hole\n");
  419. pr_info("Please enable the IOMMU option in the BIOS setup\n");
  420. pr_info("This costs you %dMB of RAM\n",
  421. 32 << fallback_aper_order);
  422. aper_order = fallback_aper_order;
  423. aper_alloc = allocate_aperture();
  424. if (!aper_alloc) {
  425. /*
  426. * Could disable AGP and IOMMU here, but it's
  427. * probably not worth it. But the later users
  428. * cannot deal with bad apertures and turning
  429. * on the aperture over memory causes very
  430. * strange problems, so it's better to panic
  431. * early.
  432. */
  433. panic("Not enough memory for aperture");
  434. }
  435. } else {
  436. return 0;
  437. }
  438. /*
  439. * If this is the kdump kernel _and_ the first kernel did not
  440. * configure the aperture in the northbridge, this range may
  441. * overlap with the first kernel's memory. We can't access the
  442. * range through vmcore even though it should be part of the dump.
  443. */
  444. exclude_from_vmcore(aper_alloc, aper_order);
  445. /* Fix up the north bridges */
  446. for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  447. int bus, dev_base, dev_limit;
  448. /*
  449. * Don't enable translation yet but enable GART IO and CPU
  450. * accesses and set DISTLBWALKPRB since GART table memory is UC.
  451. */
  452. u32 ctl = aper_order << 1;
  453. bus = amd_nb_bus_dev_ranges[i].bus;
  454. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  455. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  456. for (slot = dev_base; slot < dev_limit; slot++) {
  457. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  458. continue;
  459. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  460. write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25);
  461. }
  462. }
  463. set_up_gart_resume(aper_order, aper_alloc);
  464. return 1;
  465. }