aperture_64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. type = read_pci_config_byte(bus, slot, func,
  231. PCI_HEADER_TYPE);
  232. if (!(type & PCI_HEADER_TYPE_MFD))
  233. break;
  234. }
  235. }
  236. }
  237. pr_info("No AGP bridge found\n");
  238. return 0;
  239. }
  240. static bool gart_fix_e820 __initdata = true;
  241. static int __init parse_gart_mem(char *p)
  242. {
  243. return kstrtobool(p, &gart_fix_e820);
  244. }
  245. early_param("gart_fix_e820", parse_gart_mem);
  246. /*
  247. * With kexec/kdump, if the first kernel doesn't shut down the GART and the
  248. * second kernel allocates a different GART region, there might be two
  249. * overlapping GART regions present:
  250. *
  251. * - the first still used by the GART initialized in the first kernel.
  252. * - (sub-)set of it used as normal RAM by the second kernel.
  253. *
  254. * which leads to memory corruptions and a kernel panic eventually.
  255. *
  256. * This can also happen if the BIOS has forgotten to mark the GART region
  257. * as reserved.
  258. *
  259. * Try to update the e820 map to mark that new region as reserved.
  260. */
  261. void __init early_gart_iommu_check(void)
  262. {
  263. u32 agp_aper_order = 0;
  264. int i, fix, slot, valid_agp = 0;
  265. u32 ctl;
  266. u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
  267. u64 aper_base = 0, last_aper_base = 0;
  268. int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0;
  269. if (!amd_gart_present())
  270. return;
  271. if (!early_pci_allowed())
  272. return;
  273. /* This is mostly duplicate of iommu_hole_init */
  274. search_agp_bridge(&agp_aper_order, &valid_agp);
  275. fix = 0;
  276. for (i = 0; amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  277. int bus;
  278. int dev_base, dev_limit;
  279. bus = amd_nb_bus_dev_ranges[i].bus;
  280. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  281. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  282. for (slot = dev_base; slot < dev_limit; slot++) {
  283. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  284. continue;
  285. ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
  286. aper_enabled = ctl & GARTEN;
  287. aper_order = (ctl >> 1) & 7;
  288. aper_size = (32 * 1024 * 1024) << aper_order;
  289. aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
  290. aper_base <<= 25;
  291. if (last_valid) {
  292. if ((aper_order != last_aper_order) ||
  293. (aper_base != last_aper_base) ||
  294. (aper_enabled != last_aper_enabled)) {
  295. fix = 1;
  296. break;
  297. }
  298. }
  299. last_aper_order = aper_order;
  300. last_aper_base = aper_base;
  301. last_aper_enabled = aper_enabled;
  302. last_valid = 1;
  303. }
  304. }
  305. if (!fix && !aper_enabled)
  306. return;
  307. if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
  308. fix = 1;
  309. if (gart_fix_e820 && !fix && aper_enabled) {
  310. if (e820__mapped_any(aper_base, aper_base + aper_size,
  311. E820_TYPE_RAM)) {
  312. /* reserve it, so we can reuse it in second kernel */
  313. pr_info("e820: reserve [mem %#010Lx-%#010Lx] for GART\n",
  314. aper_base, aper_base + aper_size - 1);
  315. e820__range_add(aper_base, aper_size, E820_TYPE_RESERVED);
  316. e820__update_table_print();
  317. }
  318. }
  319. if (valid_agp)
  320. return;
  321. /* disable them all at first */
  322. for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  323. int bus;
  324. int dev_base, dev_limit;
  325. bus = amd_nb_bus_dev_ranges[i].bus;
  326. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  327. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  328. for (slot = dev_base; slot < dev_limit; slot++) {
  329. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  330. continue;
  331. ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
  332. ctl &= ~GARTEN;
  333. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  334. }
  335. }
  336. }
  337. static int __initdata printed_gart_size_msg;
  338. void __init gart_iommu_hole_init(void)
  339. {
  340. u32 agp_aper_base = 0, agp_aper_order = 0;
  341. u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0;
  342. u64 aper_base, last_aper_base = 0;
  343. int fix, slot, valid_agp = 0;
  344. int i, node;
  345. if (!amd_gart_present())
  346. return;
  347. if (gart_iommu_aperture_disabled || !fix_aperture ||
  348. !early_pci_allowed())
  349. return;
  350. pr_info("Checking aperture...\n");
  351. if (!fallback_aper_force)
  352. agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp);
  353. fix = 0;
  354. node = 0;
  355. for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  356. int bus;
  357. int dev_base, dev_limit;
  358. u32 ctl;
  359. bus = amd_nb_bus_dev_ranges[i].bus;
  360. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  361. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  362. for (slot = dev_base; slot < dev_limit; slot++) {
  363. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  364. continue;
  365. iommu_detected = 1;
  366. gart_iommu_aperture = 1;
  367. x86_init.iommu.iommu_init = gart_iommu_init;
  368. ctl = read_pci_config(bus, slot, 3,
  369. AMD64_GARTAPERTURECTL);
  370. /*
  371. * Before we do anything else disable the GART. It may
  372. * still be enabled if we boot into a crash-kernel here.
  373. * Reconfiguring the GART while it is enabled could have
  374. * unknown side-effects.
  375. */
  376. ctl &= ~GARTEN;
  377. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  378. aper_order = (ctl >> 1) & 7;
  379. aper_size = (32 * 1024 * 1024) << aper_order;
  380. aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
  381. aper_base <<= 25;
  382. pr_info("Node %d: aperture [bus addr %#010Lx-%#010Lx] (%uMB)\n",
  383. node, aper_base, aper_base + aper_size - 1,
  384. aper_size >> 20);
  385. node++;
  386. if (!aperture_valid(aper_base, aper_size, 64<<20)) {
  387. if (valid_agp && agp_aper_base &&
  388. agp_aper_base == aper_base &&
  389. agp_aper_order == aper_order) {
  390. /* the same between two setting from NB and agp */
  391. if (!no_iommu &&
  392. max_pfn > MAX_DMA32_PFN &&
  393. !printed_gart_size_msg) {
  394. pr_err("you are using iommu with agp, but GART size is less than 64MB\n");
  395. pr_err("please increase GART size in your BIOS setup\n");
  396. pr_err("if BIOS doesn't have that option, contact your HW vendor!\n");
  397. printed_gart_size_msg = 1;
  398. }
  399. } else {
  400. fix = 1;
  401. goto out;
  402. }
  403. }
  404. if ((last_aper_order && aper_order != last_aper_order) ||
  405. (last_aper_base && aper_base != last_aper_base)) {
  406. fix = 1;
  407. goto out;
  408. }
  409. last_aper_order = aper_order;
  410. last_aper_base = aper_base;
  411. }
  412. }
  413. out:
  414. if (!fix && !fallback_aper_force) {
  415. if (last_aper_base) {
  416. /*
  417. * If this is the kdump kernel, the first kernel
  418. * may have allocated the range over its e820 RAM
  419. * and fixed up the northbridge
  420. */
  421. exclude_from_core(last_aper_base, last_aper_order);
  422. }
  423. return;
  424. }
  425. if (!fallback_aper_force) {
  426. aper_alloc = agp_aper_base;
  427. aper_order = agp_aper_order;
  428. }
  429. if (aper_alloc) {
  430. /* Got the aperture from the AGP bridge */
  431. } else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) ||
  432. force_iommu ||
  433. valid_agp ||
  434. fallback_aper_force) {
  435. pr_info("Your BIOS doesn't leave an aperture memory hole\n");
  436. pr_info("Please enable the IOMMU option in the BIOS setup\n");
  437. pr_info("This costs you %dMB of RAM\n",
  438. 32 << fallback_aper_order);
  439. aper_order = fallback_aper_order;
  440. aper_alloc = allocate_aperture();
  441. if (!aper_alloc) {
  442. /*
  443. * Could disable AGP and IOMMU here, but it's
  444. * probably not worth it. But the later users
  445. * cannot deal with bad apertures and turning
  446. * on the aperture over memory causes very
  447. * strange problems, so it's better to panic
  448. * early.
  449. */
  450. panic("Not enough memory for aperture");
  451. }
  452. } else {
  453. return;
  454. }
  455. /*
  456. * If this is the kdump kernel _and_ the first kernel did not
  457. * configure the aperture in the northbridge, this range may
  458. * overlap with the first kernel's memory. We can't access the
  459. * range through vmcore even though it should be part of the dump.
  460. */
  461. exclude_from_core(aper_alloc, aper_order);
  462. /* Fix up the north bridges */
  463. for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
  464. int bus, dev_base, dev_limit;
  465. /*
  466. * Don't enable translation yet but enable GART IO and CPU
  467. * accesses and set DISTLBWALKPRB since GART table memory is UC.
  468. */
  469. u32 ctl = aper_order << 1;
  470. bus = amd_nb_bus_dev_ranges[i].bus;
  471. dev_base = amd_nb_bus_dev_ranges[i].dev_base;
  472. dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
  473. for (slot = dev_base; slot < dev_limit; slot++) {
  474. if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
  475. continue;
  476. write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
  477. write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25);
  478. }
  479. }
  480. set_up_gart_resume(aper_order, aper_alloc);
  481. }