aperture_64.c 16 KB

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