blob: 294ed4392a0ecd965b6b527ba499d3c1be1d1fce [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Ingo Molnarc140df92008-01-30 13:30:09 +01002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Firmware replacement code.
Ingo Molnarc140df92008-01-30 13:30:09 +01004 *
Pavel Machek8caac562008-11-26 17:15:27 +01005 * 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 *
Ingo Molnarc140df92008-01-30 13:30:09 +01008 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Copyright 2002 Andi Kleen, SuSE Labs.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Bjorn Helgaasa5d32442014-04-28 15:16:33 -060014#define pr_fmt(fmt) "AGP: " fmt
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
Kairui Songffc85992019-03-08 11:05:08 +080017#include <linux/kcore.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/types.h>
19#include <linux/init.h>
Yinghai Lu32e3f2b2010-12-17 16:58:40 -080020#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/mmzone.h>
22#include <linux/pci_ids.h>
23#include <linux/pci.h>
24#include <linux/bitops.h>
Pavel Machek2050d452008-03-13 23:05:41 +010025#include <linux/suspend.h>
Ingo Molnar66441bd2017-01-27 10:27:10 +010026#include <asm/e820/api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/io.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090028#include <asm/iommu.h>
Joerg Roedel395624f2007-10-24 12:49:47 +020029#include <asm/gart.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/pci-direct.h>
Andi Kleenca8642f2006-01-11 22:44:27 +010031#include <asm/dma.h>
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +020032#include <asm/amd_nb.h>
FUJITA Tomonoride957622009-11-10 19:46:14 +090033#include <asm/x86_init.h>
Jiri Bohac2a3e83c2018-01-06 02:00:13 +010034#include <linux/crash_dump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Joerg Roedelc387aa32011-04-18 15:45:43 +020036/*
37 * Using 512M as goal, in case kexec will load kernel_big
38 * that will do the on-position decompress, and could overlap with
39 * with the gart aperture that is used.
40 * Sequence:
41 * kernel_small
42 * ==> kexec (with kdump trigger path or gart still enabled)
43 * ==> kernel_small (gart area become e820_reserved)
44 * ==> kexec (with kdump trigger path or gart still enabled)
45 * ==> kerne_big (uncompressed size will be big than 64M or 128M)
46 * So don't use 512M below as gart iommu, leave the space for kernel
47 * code for safe.
48 */
49#define GART_MIN_ADDR (512ULL << 20)
50#define GART_MAX_ADDR (1ULL << 32)
51
Joerg Roedel0440d4c2007-10-24 12:49:50 +020052int gart_iommu_aperture;
Pavel Machek7de6a4c2008-03-13 11:03:58 +010053int gart_iommu_aperture_disabled __initdata;
54int gart_iommu_aperture_allowed __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56int fallback_aper_order __initdata = 1; /* 64MB */
Pavel Machek7de6a4c2008-03-13 11:03:58 +010057int fallback_aper_force __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59int fix_aperture __initdata = 1;
60
Kairui Songffc85992019-03-08 11:05:08 +080061#if defined(CONFIG_PROC_VMCORE) || defined(CONFIG_PROC_KCORE)
Jiri Bohac2a3e83c2018-01-06 02:00:13 +010062/*
63 * If the first kernel maps the aperture over e820 RAM, the kdump kernel will
64 * use the same range because it will remain configured in the northbridge.
65 * Trying to dump this area via /proc/vmcore may crash the machine, so exclude
66 * it from vmcore.
67 */
68static unsigned long aperture_pfn_start, aperture_page_count;
69
Kairui Songffc85992019-03-08 11:05:08 +080070static int gart_mem_pfn_is_ram(unsigned long pfn)
Jiri Bohac2a3e83c2018-01-06 02:00:13 +010071{
72 return likely((pfn < aperture_pfn_start) ||
73 (pfn >= aperture_pfn_start + aperture_page_count));
74}
75
Kairui Songffc85992019-03-08 11:05:08 +080076static void __init exclude_from_core(u64 aper_base, u32 aper_order)
Jiri Bohac2a3e83c2018-01-06 02:00:13 +010077{
78 aperture_pfn_start = aper_base >> PAGE_SHIFT;
79 aperture_page_count = (32 * 1024 * 1024) << aper_order >> PAGE_SHIFT;
Kairui Songffc85992019-03-08 11:05:08 +080080#ifdef CONFIG_PROC_VMCORE
81 WARN_ON(register_oldmem_pfn_is_ram(&gart_mem_pfn_is_ram));
82#endif
83#ifdef CONFIG_PROC_KCORE
84 WARN_ON(register_mem_pfn_is_ram(&gart_mem_pfn_is_ram));
85#endif
Jiri Bohac2a3e83c2018-01-06 02:00:13 +010086}
87#else
Kairui Songffc85992019-03-08 11:05:08 +080088static void exclude_from_core(u64 aper_base, u32 aper_order)
Jiri Bohac2a3e83c2018-01-06 02:00:13 +010089{
90}
91#endif
92
Andrew Morton42442ed2005-06-08 15:49:25 -070093/* This code runs before the PCI subsystem is initialized, so just
94 access the northbridge directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Ingo Molnarc140df92008-01-30 13:30:09 +010096static u32 __init allocate_aperture(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 u32 aper_size;
Yinghai Lu32e3f2b2010-12-17 16:58:40 -080099 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Yinghai Lu7677b2e2008-04-14 20:40:37 -0700101 /* aper_size should <= 1G */
102 if (fallback_aper_order > 5)
103 fallback_aper_order = 5;
Ingo Molnarc140df92008-01-30 13:30:09 +0100104 aper_size = (32 * 1024 * 1024) << fallback_aper_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Ingo Molnarc140df92008-01-30 13:30:09 +0100106 /*
107 * Aperture has to be naturally aligned. This means a 2GB aperture
108 * won't have much chance of finding a place in the lower 4GB of
109 * memory. Unfortunately we cannot move it up because that would
110 * make the IOMMU useless.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Joerg Roedelc387aa32011-04-18 15:45:43 +0200112 addr = memblock_find_in_range(GART_MIN_ADDR, GART_MAX_ADDR,
113 aper_size, aper_size);
Wang YanQing26bfc542013-04-16 09:37:34 +0800114 if (!addr) {
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600115 pr_err("Cannot allocate aperture memory hole [mem %#010lx-%#010lx] (%uKB)\n",
116 addr, addr + aper_size - 1, aper_size >> 10);
Yinghai Lu32e3f2b2010-12-17 16:58:40 -0800117 return 0;
118 }
Tejun Heo24aa0782011-07-12 11:16:06 +0200119 memblock_reserve(addr, aper_size);
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600120 pr_info("Mapping aperture over RAM [mem %#010lx-%#010lx] (%uKB)\n",
121 addr, addr + aper_size - 1, aper_size >> 10);
Yinghai Lu32e3f2b2010-12-17 16:58:40 -0800122 register_nosave_region(addr >> PAGE_SHIFT,
123 (addr+aper_size) >> PAGE_SHIFT);
Ingo Molnarc140df92008-01-30 13:30:09 +0100124
Yinghai Lu32e3f2b2010-12-17 16:58:40 -0800125 return (u32)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Andrew Morton42442ed2005-06-08 15:49:25 -0700129/* Find a PCI capability */
Pavel Machekdd564d02008-05-27 18:03:56 +0200130static u32 __init find_cap(int bus, int slot, int func, int cap)
Ingo Molnarc140df92008-01-30 13:30:09 +0100131{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 int bytes;
Ingo Molnarc140df92008-01-30 13:30:09 +0100133 u8 pos;
134
Yinghai Lu55c0d722008-04-19 01:31:11 -0700135 if (!(read_pci_config_16(bus, slot, func, PCI_STATUS) &
Ingo Molnarc140df92008-01-30 13:30:09 +0100136 PCI_STATUS_CAP_LIST))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100138
Yinghai Lu55c0d722008-04-19 01:31:11 -0700139 pos = read_pci_config_byte(bus, slot, func, PCI_CAPABILITY_LIST);
Ingo Molnarc140df92008-01-30 13:30:09 +0100140 for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 u8 id;
Ingo Molnarc140df92008-01-30 13:30:09 +0100142
143 pos &= ~3;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700144 id = read_pci_config_byte(bus, slot, func, pos+PCI_CAP_LIST_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (id == 0xff)
146 break;
Ingo Molnarc140df92008-01-30 13:30:09 +0100147 if (id == cap)
148 return pos;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700149 pos = read_pci_config_byte(bus, slot, func,
Ingo Molnarc140df92008-01-30 13:30:09 +0100150 pos+PCI_CAP_LIST_NEXT);
151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100153}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155/* Read a standard AGPv3 bridge header */
Pavel Machekdd564d02008-05-27 18:03:56 +0200156static u32 __init read_agp(int bus, int slot, int func, int cap, u32 *order)
Ingo Molnarc140df92008-01-30 13:30:09 +0100157{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 u32 apsize;
159 u32 apsizereg;
160 int nbits;
161 u32 aper_low, aper_hi;
162 u64 aper;
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700163 u32 old_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600165 pr_info("pci 0000:%02x:%02x:%02x: AGP bridge\n", bus, slot, func);
Yinghai Lu55c0d722008-04-19 01:31:11 -0700166 apsizereg = read_pci_config_16(bus, slot, func, cap + 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (apsizereg == 0xffffffff) {
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600168 pr_err("pci 0000:%02x:%02x.%d: APSIZE unreadable\n",
169 bus, slot, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return 0;
171 }
172
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700173 /* old_order could be the value from NB gart setting */
174 old_order = *order;
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 apsize = apsizereg & 0xfff;
177 /* Some BIOS use weird encodings not in the AGPv3 table. */
Ingo Molnarc140df92008-01-30 13:30:09 +0100178 if (apsize & 0xff)
179 apsize |= 0xf00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 nbits = hweight16(apsize);
181 *order = 7 - nbits;
182 if ((int)*order < 0) /* < 32MB */
183 *order = 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100184
Yinghai Lu55c0d722008-04-19 01:31:11 -0700185 aper_low = read_pci_config(bus, slot, func, 0x10);
186 aper_hi = read_pci_config(bus, slot, func, 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
188
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700189 /*
190 * On some sick chips, APSIZE is 0. It means it wants 4G
191 * so let double check that order, and lets trust AMD NB settings:
192 */
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600193 pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (old size %uMB)\n",
194 bus, slot, func, aper, aper + (32ULL << (old_order + 20)) - 1,
195 32 << old_order);
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700196 if (aper + (32ULL<<(20 + *order)) > 0x100000000ULL) {
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600197 pr_info("pci 0000:%02x:%02x.%d: AGP aperture size %uMB (APSIZE %#x) is not right, using settings from NB\n",
198 bus, slot, func, 32 << *order, apsizereg);
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700199 *order = old_order;
200 }
201
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600202 pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (%uMB, APSIZE %#x)\n",
203 bus, slot, func, aper, aper + (32ULL << (*order + 20)) - 1,
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600204 32 << *order, apsizereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700206 if (!aperture_valid(aper, (32*1024*1024) << *order, 32<<20))
Ingo Molnarc140df92008-01-30 13:30:09 +0100207 return 0;
208 return (u32)aper;
209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Ingo Molnarc140df92008-01-30 13:30:09 +0100211/*
212 * Look for an AGP bridge. Windows only expects the aperture in the
213 * AGP bridge and some BIOS forget to initialize the Northbridge too.
214 * Work around this here.
215 *
216 * Do an PCI bus scan by hand because we're running before the PCI
217 * subsystem.
218 *
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200219 * All AMD AGP bridges are AGPv3 compliant, so we can do this scan
Ingo Molnarc140df92008-01-30 13:30:09 +0100220 * generically. It's probably overkill to always scan all slots because
221 * the AGP bridges should be always an own bus on the HT hierarchy,
222 * but do it here for future safety.
223 */
Pavel Machekdd564d02008-05-27 18:03:56 +0200224static u32 __init search_agp_bridge(u32 *order, int *valid_agp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Yinghai Lu55c0d722008-04-19 01:31:11 -0700226 int bus, slot, func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* Poor man's PCI discovery */
Yinghai Lu55c0d722008-04-19 01:31:11 -0700229 for (bus = 0; bus < 256; bus++) {
Ingo Molnarc140df92008-01-30 13:30:09 +0100230 for (slot = 0; slot < 32; slot++) {
231 for (func = 0; func < 8; func++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 u32 class, cap;
233 u8 type;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700234 class = read_pci_config(bus, slot, func,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 PCI_CLASS_REVISION);
236 if (class == 0xffffffff)
Ingo Molnarc140df92008-01-30 13:30:09 +0100237 break;
238
239 switch (class >> 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 case PCI_CLASS_BRIDGE_HOST:
241 case PCI_CLASS_BRIDGE_OTHER: /* needed? */
242 /* AGP bridge? */
Yinghai Lu55c0d722008-04-19 01:31:11 -0700243 cap = find_cap(bus, slot, func,
Ingo Molnarc140df92008-01-30 13:30:09 +0100244 PCI_CAP_ID_AGP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (!cap)
246 break;
Ingo Molnarc140df92008-01-30 13:30:09 +0100247 *valid_agp = 1;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700248 return read_agp(bus, slot, func, cap,
Ingo Molnarc140df92008-01-30 13:30:09 +0100249 order);
250 }
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 /* No multi-function device? */
Yinghai Lu55c0d722008-04-19 01:31:11 -0700253 type = read_pci_config_byte(bus, slot, func,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 PCI_HEADER_TYPE);
255 if (!(type & 0x80))
256 break;
Ingo Molnarc140df92008-01-30 13:30:09 +0100257 }
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600260 pr_info("No AGP bridge found\n");
Ingo Molnarc140df92008-01-30 13:30:09 +0100261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
263}
264
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700265static bool gart_fix_e820 __initdata = true;
Yinghai Luaaf23042008-01-30 13:33:09 +0100266
267static int __init parse_gart_mem(char *p)
268{
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700269 return kstrtobool(p, &gart_fix_e820);
Yinghai Luaaf23042008-01-30 13:33:09 +0100270}
271early_param("gart_fix_e820", parse_gart_mem);
272
Borislav Petkov63ecd3b2018-11-01 16:24:43 +0100273/*
274 * With kexec/kdump, if the first kernel doesn't shut down the GART and the
275 * second kernel allocates a different GART region, there might be two
276 * overlapping GART regions present:
277 *
278 * - the first still used by the GART initialized in the first kernel.
279 * - (sub-)set of it used as normal RAM by the second kernel.
280 *
281 * which leads to memory corruptions and a kernel panic eventually.
282 *
283 * This can also happen if the BIOS has forgotten to mark the GART region
284 * as reserved.
285 *
286 * Try to update the e820 map to mark that new region as reserved.
287 */
Yinghai Luaaf23042008-01-30 13:33:09 +0100288void __init early_gart_iommu_check(void)
289{
Andi Kleenfa10ba62010-07-20 15:19:49 -0700290 u32 agp_aper_order = 0;
Yinghai Luf3eee542009-12-14 11:52:15 +0900291 int i, fix, slot, valid_agp = 0;
Yinghai Luaaf23042008-01-30 13:33:09 +0100292 u32 ctl;
293 u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
294 u64 aper_base = 0, last_aper_base = 0;
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200295 int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0;
Yinghai Luaaf23042008-01-30 13:33:09 +0100296
Aravind Gopalakrishnan1b457422015-04-07 16:46:37 -0500297 if (!amd_gart_present())
298 return;
299
Yinghai Luaaf23042008-01-30 13:33:09 +0100300 if (!early_pci_allowed())
301 return;
302
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200303 /* This is mostly duplicate of iommu_hole_init */
Andi Kleenfa10ba62010-07-20 15:19:49 -0700304 search_agp_bridge(&agp_aper_order, &valid_agp);
Yinghai Luf3eee542009-12-14 11:52:15 +0900305
Yinghai Luaaf23042008-01-30 13:33:09 +0100306 fix = 0;
Jan Beulich24d9b702011-01-10 16:20:23 +0000307 for (i = 0; amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Yinghai Lu55c0d722008-04-19 01:31:11 -0700308 int bus;
309 int dev_base, dev_limit;
Yinghai Luaaf23042008-01-30 13:33:09 +0100310
Jan Beulich24d9b702011-01-10 16:20:23 +0000311 bus = amd_nb_bus_dev_ranges[i].bus;
312 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
313 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Yinghai Luaaf23042008-01-30 13:33:09 +0100314
Yinghai Lu55c0d722008-04-19 01:31:11 -0700315 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200316 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700317 continue;
318
319 ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
Borislav Petkov57ab43e2010-09-03 18:39:39 +0200320 aper_enabled = ctl & GARTEN;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700321 aper_order = (ctl >> 1) & 7;
322 aper_size = (32 * 1024 * 1024) << aper_order;
323 aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
324 aper_base <<= 25;
325
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200326 if (last_valid) {
327 if ((aper_order != last_aper_order) ||
328 (aper_base != last_aper_base) ||
329 (aper_enabled != last_aper_enabled)) {
330 fix = 1;
331 break;
332 }
Yinghai Lu55c0d722008-04-19 01:31:11 -0700333 }
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200334
Yinghai Lu55c0d722008-04-19 01:31:11 -0700335 last_aper_order = aper_order;
336 last_aper_base = aper_base;
337 last_aper_enabled = aper_enabled;
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200338 last_valid = 1;
Yinghai Luaaf23042008-01-30 13:33:09 +0100339 }
Yinghai Luaaf23042008-01-30 13:33:09 +0100340 }
341
342 if (!fix && !aper_enabled)
343 return;
344
345 if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
346 fix = 1;
347
348 if (gart_fix_e820 && !fix && aper_enabled) {
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100349 if (e820__mapped_any(aper_base, aper_base + aper_size,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100350 E820_TYPE_RAM)) {
Pavel Machek0abbc782008-05-20 16:27:17 +0200351 /* reserve it, so we can reuse it in second kernel */
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600352 pr_info("e820: reserve [mem %#010Lx-%#010Lx] for GART\n",
353 aper_base, aper_base + aper_size - 1);
Ingo Molnar09821ff2017-01-28 17:09:33 +0100354 e820__range_add(aper_base, aper_size, E820_TYPE_RESERVED);
Ingo Molnar6464d292017-01-28 14:03:04 +0100355 e820__update_table_print();
Yinghai Luaaf23042008-01-30 13:33:09 +0100356 }
Yinghai Luaaf23042008-01-30 13:33:09 +0100357 }
358
Yinghai Luf3eee542009-12-14 11:52:15 +0900359 if (valid_agp)
Pavel Machek4f384f82008-05-26 21:17:30 +0200360 return;
361
Yinghai Luf3eee542009-12-14 11:52:15 +0900362 /* disable them all at first */
Jan Beulich24d9b702011-01-10 16:20:23 +0000363 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Yinghai Lu55c0d722008-04-19 01:31:11 -0700364 int bus;
365 int dev_base, dev_limit;
Yinghai Luaaf23042008-01-30 13:33:09 +0100366
Jan Beulich24d9b702011-01-10 16:20:23 +0000367 bus = amd_nb_bus_dev_ranges[i].bus;
368 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
369 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700370
371 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200372 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700373 continue;
374
375 ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
Borislav Petkov57ab43e2010-09-03 18:39:39 +0200376 ctl &= ~GARTEN;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700377 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
378 }
Yinghai Luaaf23042008-01-30 13:33:09 +0100379 }
380
381}
382
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700383static int __initdata printed_gart_size_msg;
384
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400385int __init gart_iommu_hole_init(void)
Ingo Molnarc140df92008-01-30 13:30:09 +0100386{
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700387 u32 agp_aper_base = 0, agp_aper_order = 0;
Andi Kleen50895c52005-11-05 17:25:53 +0100388 u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 u64 aper_base, last_aper_base = 0;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700390 int fix, slot, valid_agp = 0;
391 int i, node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Aravind Gopalakrishnan1b457422015-04-07 16:46:37 -0500393 if (!amd_gart_present())
394 return -ENODEV;
395
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200396 if (gart_iommu_aperture_disabled || !fix_aperture ||
397 !early_pci_allowed())
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400398 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600400 pr_info("Checking aperture...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700402 if (!fallback_aper_force)
403 agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp);
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 fix = 0;
Yinghai Lu47db4c32008-01-30 13:33:18 +0100406 node = 0;
Jan Beulich24d9b702011-01-10 16:20:23 +0000407 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Yinghai Lu55c0d722008-04-19 01:31:11 -0700408 int bus;
409 int dev_base, dev_limit;
Joerg Roedel4b838732010-04-07 12:57:35 +0200410 u32 ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Jan Beulich24d9b702011-01-10 16:20:23 +0000412 bus = amd_nb_bus_dev_ranges[i].bus;
413 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
414 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Yinghai Lu55c0d722008-04-19 01:31:11 -0700416 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200417 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700418 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Yinghai Lu55c0d722008-04-19 01:31:11 -0700420 iommu_detected = 1;
421 gart_iommu_aperture = 1;
FUJITA Tomonoride957622009-11-10 19:46:14 +0900422 x86_init.iommu.iommu_init = gart_iommu_init;
Ingo Molnarc140df92008-01-30 13:30:09 +0100423
Joerg Roedel4b838732010-04-07 12:57:35 +0200424 ctl = read_pci_config(bus, slot, 3,
425 AMD64_GARTAPERTURECTL);
426
427 /*
428 * Before we do anything else disable the GART. It may
429 * still be enabled if we boot into a crash-kernel here.
430 * Reconfiguring the GART while it is enabled could have
431 * unknown side-effects.
432 */
433 ctl &= ~GARTEN;
434 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
435
436 aper_order = (ctl >> 1) & 7;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700437 aper_size = (32 * 1024 * 1024) << aper_order;
438 aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
439 aper_base <<= 25;
440
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600441 pr_info("Node %d: aperture [bus addr %#010Lx-%#010Lx] (%uMB)\n",
442 node, aper_base, aper_base + aper_size - 1,
443 aper_size >> 20);
Yinghai Lu55c0d722008-04-19 01:31:11 -0700444 node++;
445
446 if (!aperture_valid(aper_base, aper_size, 64<<20)) {
447 if (valid_agp && agp_aper_base &&
448 agp_aper_base == aper_base &&
449 agp_aper_order == aper_order) {
450 /* the same between two setting from NB and agp */
Yinghai Luc987d122008-06-24 22:14:09 -0700451 if (!no_iommu &&
452 max_pfn > MAX_DMA32_PFN &&
453 !printed_gart_size_msg) {
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600454 pr_err("you are using iommu with agp, but GART size is less than 64MB\n");
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600455 pr_err("please increase GART size in your BIOS setup\n");
456 pr_err("if BIOS doesn't have that option, contact your HW vendor!\n");
Yinghai Lu55c0d722008-04-19 01:31:11 -0700457 printed_gart_size_msg = 1;
458 }
459 } else {
460 fix = 1;
461 goto out;
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700462 }
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Yinghai Lu55c0d722008-04-19 01:31:11 -0700465 if ((last_aper_order && aper_order != last_aper_order) ||
466 (last_aper_base && aper_base != last_aper_base)) {
467 fix = 1;
468 goto out;
469 }
470 last_aper_order = aper_order;
471 last_aper_base = aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Yinghai Lu55c0d722008-04-19 01:31:11 -0700475out:
Aaron Durbin56dd6692006-09-26 10:52:40 +0200476 if (!fix && !fallback_aper_force) {
Jiri Bohac2a3e83c2018-01-06 02:00:13 +0100477 if (last_aper_base) {
478 /*
479 * If this is the kdump kernel, the first kernel
480 * may have allocated the range over its e820 RAM
481 * and fixed up the northbridge
482 */
Kairui Songffc85992019-03-08 11:05:08 +0800483 exclude_from_core(last_aper_base, last_aper_order);
Jiri Bohac2a3e83c2018-01-06 02:00:13 +0100484
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400485 return 1;
Jiri Bohac2a3e83c2018-01-06 02:00:13 +0100486 }
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400487 return 0;
Aaron Durbin56dd6692006-09-26 10:52:40 +0200488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700490 if (!fallback_aper_force) {
491 aper_alloc = agp_aper_base;
492 aper_order = agp_aper_order;
493 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100494
495 if (aper_alloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* Got the aperture from the AGP bridge */
Yinghai Luc987d122008-06-24 22:14:09 -0700497 } else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 force_iommu ||
499 valid_agp ||
Ingo Molnarc140df92008-01-30 13:30:09 +0100500 fallback_aper_force) {
Aravind Gopalakrishnan1b457422015-04-07 16:46:37 -0500501 pr_info("Your BIOS doesn't leave an aperture memory hole\n");
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600502 pr_info("Please enable the IOMMU option in the BIOS setup\n");
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600503 pr_info("This costs you %dMB of RAM\n",
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600504 32 << fallback_aper_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
506 aper_order = fallback_aper_order;
507 aper_alloc = allocate_aperture();
Ingo Molnarc140df92008-01-30 13:30:09 +0100508 if (!aper_alloc) {
509 /*
510 * Could disable AGP and IOMMU here, but it's
511 * probably not worth it. But the later users
512 * cannot deal with bad apertures and turning
513 * on the aperture over memory causes very
514 * strange problems, so it's better to panic
515 * early.
516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 panic("Not enough memory for aperture");
518 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100519 } else {
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400520 return 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100521 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
Jiri Bohac2a3e83c2018-01-06 02:00:13 +0100523 /*
524 * If this is the kdump kernel _and_ the first kernel did not
525 * configure the aperture in the northbridge, this range may
526 * overlap with the first kernel's memory. We can't access the
527 * range through vmcore even though it should be part of the dump.
528 */
Kairui Songffc85992019-03-08 11:05:08 +0800529 exclude_from_core(aper_alloc, aper_order);
Jiri Bohac2a3e83c2018-01-06 02:00:13 +0100530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* Fix up the north bridges */
Jan Beulich24d9b702011-01-10 16:20:23 +0000532 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Borislav Petkov260133a2010-09-03 18:39:40 +0200533 int bus, dev_base, dev_limit;
534
535 /*
536 * Don't enable translation yet but enable GART IO and CPU
537 * accesses and set DISTLBWALKPRB since GART table memory is UC.
538 */
Joerg Roedelc34151a2011-04-18 15:45:45 +0200539 u32 ctl = aper_order << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Jan Beulich24d9b702011-01-10 16:20:23 +0000541 bus = amd_nb_bus_dev_ranges[i].bus;
542 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
543 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700544 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200545 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700546 continue;
547
Borislav Petkov260133a2010-09-03 18:39:40 +0200548 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
Yinghai Lu55c0d722008-04-19 01:31:11 -0700549 write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25);
550 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100551 }
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200552
553 set_up_gart_resume(aper_order, aper_alloc);
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400554
555 return 1;
Ingo Molnarc140df92008-01-30 13:30:09 +0100556}