blob: ef2859f9fccefca9cc48b22410b51c88c0e29ca2 [file] [log] [blame]
Ingo Molnarc140df92008-01-30 13:30:09 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Firmware replacement code.
Ingo Molnarc140df92008-01-30 13:30:09 +01003 *
Pavel Machek8caac562008-11-26 17:15:27 +01004 * 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 *
Ingo Molnarc140df92008-01-30 13:30:09 +01007 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * Copyright 2002 Andi Kleen, SuSE Labs.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
Bjorn Helgaasa5d32442014-04-28 15:16:33 -060013#define pr_fmt(fmt) "AGP: " fmt
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kernel.h>
16#include <linux/types.h>
17#include <linux/init.h>
Yinghai Lu32e3f2b2010-12-17 16:58:40 -080018#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mmzone.h>
20#include <linux/pci_ids.h>
21#include <linux/pci.h>
22#include <linux/bitops.h>
Pavel Machek2050d452008-03-13 23:05:41 +010023#include <linux/suspend.h>
Ingo Molnar66441bd2017-01-27 10:27:10 +010024#include <asm/e820/api.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/io.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090026#include <asm/iommu.h>
Joerg Roedel395624f2007-10-24 12:49:47 +020027#include <asm/gart.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <asm/pci-direct.h>
Andi Kleenca8642f2006-01-11 22:44:27 +010029#include <asm/dma.h>
Andreas Herrmann23ac4ae2010-09-17 18:03:43 +020030#include <asm/amd_nb.h>
FUJITA Tomonoride957622009-11-10 19:46:14 +090031#include <asm/x86_init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Joerg Roedelc387aa32011-04-18 15:45:43 +020033/*
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
Joerg Roedel0440d4c2007-10-24 12:49:50 +020049int gart_iommu_aperture;
Pavel Machek7de6a4c2008-03-13 11:03:58 +010050int gart_iommu_aperture_disabled __initdata;
51int gart_iommu_aperture_allowed __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53int fallback_aper_order __initdata = 1; /* 64MB */
Pavel Machek7de6a4c2008-03-13 11:03:58 +010054int fallback_aper_force __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56int fix_aperture __initdata = 1;
57
Andrew Morton42442ed2005-06-08 15:49:25 -070058/* This code runs before the PCI subsystem is initialized, so just
59 access the northbridge directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Ingo Molnarc140df92008-01-30 13:30:09 +010061static u32 __init allocate_aperture(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 u32 aper_size;
Yinghai Lu32e3f2b2010-12-17 16:58:40 -080064 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Yinghai Lu7677b2e2008-04-14 20:40:37 -070066 /* aper_size should <= 1G */
67 if (fallback_aper_order > 5)
68 fallback_aper_order = 5;
Ingo Molnarc140df92008-01-30 13:30:09 +010069 aper_size = (32 * 1024 * 1024) << fallback_aper_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Ingo Molnarc140df92008-01-30 13:30:09 +010071 /*
72 * Aperture has to be naturally aligned. This means a 2GB aperture
73 * won't have much chance of finding a place in the lower 4GB of
74 * memory. Unfortunately we cannot move it up because that would
75 * make the IOMMU useless.
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 */
Joerg Roedelc387aa32011-04-18 15:45:43 +020077 addr = memblock_find_in_range(GART_MIN_ADDR, GART_MAX_ADDR,
78 aper_size, aper_size);
Wang YanQing26bfc542013-04-16 09:37:34 +080079 if (!addr) {
Bjorn Helgaasc96ec952014-04-14 15:29:19 -060080 pr_err("Cannot allocate aperture memory hole [mem %#010lx-%#010lx] (%uKB)\n",
81 addr, addr + aper_size - 1, aper_size >> 10);
Yinghai Lu32e3f2b2010-12-17 16:58:40 -080082 return 0;
83 }
Tejun Heo24aa0782011-07-12 11:16:06 +020084 memblock_reserve(addr, aper_size);
Bjorn Helgaasc96ec952014-04-14 15:29:19 -060085 pr_info("Mapping aperture over RAM [mem %#010lx-%#010lx] (%uKB)\n",
86 addr, addr + aper_size - 1, aper_size >> 10);
Yinghai Lu32e3f2b2010-12-17 16:58:40 -080087 register_nosave_region(addr >> PAGE_SHIFT,
88 (addr+aper_size) >> PAGE_SHIFT);
Ingo Molnarc140df92008-01-30 13:30:09 +010089
Yinghai Lu32e3f2b2010-12-17 16:58:40 -080090 return (u32)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Andrew Morton42442ed2005-06-08 15:49:25 -070094/* Find a PCI capability */
Pavel Machekdd564d02008-05-27 18:03:56 +020095static u32 __init find_cap(int bus, int slot, int func, int cap)
Ingo Molnarc140df92008-01-30 13:30:09 +010096{
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 int bytes;
Ingo Molnarc140df92008-01-30 13:30:09 +010098 u8 pos;
99
Yinghai Lu55c0d722008-04-19 01:31:11 -0700100 if (!(read_pci_config_16(bus, slot, func, PCI_STATUS) &
Ingo Molnarc140df92008-01-30 13:30:09 +0100101 PCI_STATUS_CAP_LIST))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100103
Yinghai Lu55c0d722008-04-19 01:31:11 -0700104 pos = read_pci_config_byte(bus, slot, func, PCI_CAPABILITY_LIST);
Ingo Molnarc140df92008-01-30 13:30:09 +0100105 for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 u8 id;
Ingo Molnarc140df92008-01-30 13:30:09 +0100107
108 pos &= ~3;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700109 id = read_pci_config_byte(bus, slot, func, pos+PCI_CAP_LIST_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (id == 0xff)
111 break;
Ingo Molnarc140df92008-01-30 13:30:09 +0100112 if (id == cap)
113 return pos;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700114 pos = read_pci_config_byte(bus, slot, func,
Ingo Molnarc140df92008-01-30 13:30:09 +0100115 pos+PCI_CAP_LIST_NEXT);
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100118}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/* Read a standard AGPv3 bridge header */
Pavel Machekdd564d02008-05-27 18:03:56 +0200121static u32 __init read_agp(int bus, int slot, int func, int cap, u32 *order)
Ingo Molnarc140df92008-01-30 13:30:09 +0100122{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 u32 apsize;
124 u32 apsizereg;
125 int nbits;
126 u32 aper_low, aper_hi;
127 u64 aper;
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700128 u32 old_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600130 pr_info("pci 0000:%02x:%02x:%02x: AGP bridge\n", bus, slot, func);
Yinghai Lu55c0d722008-04-19 01:31:11 -0700131 apsizereg = read_pci_config_16(bus, slot, func, cap + 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (apsizereg == 0xffffffff) {
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600133 pr_err("pci 0000:%02x:%02x.%d: APSIZE unreadable\n",
134 bus, slot, func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 return 0;
136 }
137
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700138 /* old_order could be the value from NB gart setting */
139 old_order = *order;
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 apsize = apsizereg & 0xfff;
142 /* Some BIOS use weird encodings not in the AGPv3 table. */
Ingo Molnarc140df92008-01-30 13:30:09 +0100143 if (apsize & 0xff)
144 apsize |= 0xf00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 nbits = hweight16(apsize);
146 *order = 7 - nbits;
147 if ((int)*order < 0) /* < 32MB */
148 *order = 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100149
Yinghai Lu55c0d722008-04-19 01:31:11 -0700150 aper_low = read_pci_config(bus, slot, func, 0x10);
151 aper_hi = read_pci_config(bus, slot, func, 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
153
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700154 /*
155 * On some sick chips, APSIZE is 0. It means it wants 4G
156 * so let double check that order, and lets trust AMD NB settings:
157 */
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600158 pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (old size %uMB)\n",
159 bus, slot, func, aper, aper + (32ULL << (old_order + 20)) - 1,
160 32 << old_order);
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700161 if (aper + (32ULL<<(20 + *order)) > 0x100000000ULL) {
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600162 pr_info("pci 0000:%02x:%02x.%d: AGP aperture size %uMB (APSIZE %#x) is not right, using settings from NB\n",
163 bus, slot, func, 32 << *order, apsizereg);
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700164 *order = old_order;
165 }
166
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600167 pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (%uMB, APSIZE %#x)\n",
168 bus, slot, func, aper, aper + (32ULL << (*order + 20)) - 1,
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600169 32 << *order, apsizereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700171 if (!aperture_valid(aper, (32*1024*1024) << *order, 32<<20))
Ingo Molnarc140df92008-01-30 13:30:09 +0100172 return 0;
173 return (u32)aper;
174}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Ingo Molnarc140df92008-01-30 13:30:09 +0100176/*
177 * Look for an AGP bridge. Windows only expects the aperture in the
178 * AGP bridge and some BIOS forget to initialize the Northbridge too.
179 * Work around this here.
180 *
181 * Do an PCI bus scan by hand because we're running before the PCI
182 * subsystem.
183 *
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200184 * All AMD AGP bridges are AGPv3 compliant, so we can do this scan
Ingo Molnarc140df92008-01-30 13:30:09 +0100185 * generically. It's probably overkill to always scan all slots because
186 * the AGP bridges should be always an own bus on the HT hierarchy,
187 * but do it here for future safety.
188 */
Pavel Machekdd564d02008-05-27 18:03:56 +0200189static u32 __init search_agp_bridge(u32 *order, int *valid_agp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Yinghai Lu55c0d722008-04-19 01:31:11 -0700191 int bus, slot, func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 /* Poor man's PCI discovery */
Yinghai Lu55c0d722008-04-19 01:31:11 -0700194 for (bus = 0; bus < 256; bus++) {
Ingo Molnarc140df92008-01-30 13:30:09 +0100195 for (slot = 0; slot < 32; slot++) {
196 for (func = 0; func < 8; func++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 u32 class, cap;
198 u8 type;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700199 class = read_pci_config(bus, slot, func,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 PCI_CLASS_REVISION);
201 if (class == 0xffffffff)
Ingo Molnarc140df92008-01-30 13:30:09 +0100202 break;
203
204 switch (class >> 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 case PCI_CLASS_BRIDGE_HOST:
206 case PCI_CLASS_BRIDGE_OTHER: /* needed? */
207 /* AGP bridge? */
Yinghai Lu55c0d722008-04-19 01:31:11 -0700208 cap = find_cap(bus, slot, func,
Ingo Molnarc140df92008-01-30 13:30:09 +0100209 PCI_CAP_ID_AGP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (!cap)
211 break;
Ingo Molnarc140df92008-01-30 13:30:09 +0100212 *valid_agp = 1;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700213 return read_agp(bus, slot, func, cap,
Ingo Molnarc140df92008-01-30 13:30:09 +0100214 order);
215 }
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* No multi-function device? */
Yinghai Lu55c0d722008-04-19 01:31:11 -0700218 type = read_pci_config_byte(bus, slot, func,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 PCI_HEADER_TYPE);
220 if (!(type & 0x80))
221 break;
Ingo Molnarc140df92008-01-30 13:30:09 +0100222 }
223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600225 pr_info("No AGP bridge found\n");
Ingo Molnarc140df92008-01-30 13:30:09 +0100226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return 0;
228}
229
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700230static bool gart_fix_e820 __initdata = true;
Yinghai Luaaf23042008-01-30 13:33:09 +0100231
232static int __init parse_gart_mem(char *p)
233{
Kees Cook4cc7ecb72016-03-17 14:23:00 -0700234 return kstrtobool(p, &gart_fix_e820);
Yinghai Luaaf23042008-01-30 13:33:09 +0100235}
236early_param("gart_fix_e820", parse_gart_mem);
237
238void __init early_gart_iommu_check(void)
239{
240 /*
241 * in case it is enabled before, esp for kexec/kdump,
242 * previous kernel already enable that. memset called
243 * by allocate_aperture/__alloc_bootmem_nopanic cause restart.
244 * or second kernel have different position for GART hole. and new
245 * kernel could use hole as RAM that is still used by GART set by
246 * first kernel
247 * or BIOS forget to put that in reserved.
248 * try to update e820 to make that region as reserved.
249 */
Andi Kleenfa10ba62010-07-20 15:19:49 -0700250 u32 agp_aper_order = 0;
Yinghai Luf3eee542009-12-14 11:52:15 +0900251 int i, fix, slot, valid_agp = 0;
Yinghai Luaaf23042008-01-30 13:33:09 +0100252 u32 ctl;
253 u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
254 u64 aper_base = 0, last_aper_base = 0;
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200255 int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0;
Yinghai Luaaf23042008-01-30 13:33:09 +0100256
Aravind Gopalakrishnan1b457422015-04-07 16:46:37 -0500257 if (!amd_gart_present())
258 return;
259
Yinghai Luaaf23042008-01-30 13:33:09 +0100260 if (!early_pci_allowed())
261 return;
262
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200263 /* This is mostly duplicate of iommu_hole_init */
Andi Kleenfa10ba62010-07-20 15:19:49 -0700264 search_agp_bridge(&agp_aper_order, &valid_agp);
Yinghai Luf3eee542009-12-14 11:52:15 +0900265
Yinghai Luaaf23042008-01-30 13:33:09 +0100266 fix = 0;
Jan Beulich24d9b702011-01-10 16:20:23 +0000267 for (i = 0; amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Yinghai Lu55c0d722008-04-19 01:31:11 -0700268 int bus;
269 int dev_base, dev_limit;
Yinghai Luaaf23042008-01-30 13:33:09 +0100270
Jan Beulich24d9b702011-01-10 16:20:23 +0000271 bus = amd_nb_bus_dev_ranges[i].bus;
272 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
273 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Yinghai Luaaf23042008-01-30 13:33:09 +0100274
Yinghai Lu55c0d722008-04-19 01:31:11 -0700275 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200276 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700277 continue;
278
279 ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
Borislav Petkov57ab43e2010-09-03 18:39:39 +0200280 aper_enabled = ctl & GARTEN;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700281 aper_order = (ctl >> 1) & 7;
282 aper_size = (32 * 1024 * 1024) << aper_order;
283 aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
284 aper_base <<= 25;
285
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200286 if (last_valid) {
287 if ((aper_order != last_aper_order) ||
288 (aper_base != last_aper_base) ||
289 (aper_enabled != last_aper_enabled)) {
290 fix = 1;
291 break;
292 }
Yinghai Lu55c0d722008-04-19 01:31:11 -0700293 }
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200294
Yinghai Lu55c0d722008-04-19 01:31:11 -0700295 last_aper_order = aper_order;
296 last_aper_base = aper_base;
297 last_aper_enabled = aper_enabled;
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200298 last_valid = 1;
Yinghai Luaaf23042008-01-30 13:33:09 +0100299 }
Yinghai Luaaf23042008-01-30 13:33:09 +0100300 }
301
302 if (!fix && !aper_enabled)
303 return;
304
305 if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
306 fix = 1;
307
308 if (gart_fix_e820 && !fix && aper_enabled) {
Ingo Molnar3bce64f2017-01-28 14:14:25 +0100309 if (e820__mapped_any(aper_base, aper_base + aper_size,
Ingo Molnar09821ff2017-01-28 17:09:33 +0100310 E820_TYPE_RAM)) {
Pavel Machek0abbc782008-05-20 16:27:17 +0200311 /* reserve it, so we can reuse it in second kernel */
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600312 pr_info("e820: reserve [mem %#010Lx-%#010Lx] for GART\n",
313 aper_base, aper_base + aper_size - 1);
Ingo Molnar09821ff2017-01-28 17:09:33 +0100314 e820__range_add(aper_base, aper_size, E820_TYPE_RESERVED);
Ingo Molnar6464d292017-01-28 14:03:04 +0100315 e820__update_table_print();
Yinghai Luaaf23042008-01-30 13:33:09 +0100316 }
Yinghai Luaaf23042008-01-30 13:33:09 +0100317 }
318
Yinghai Luf3eee542009-12-14 11:52:15 +0900319 if (valid_agp)
Pavel Machek4f384f82008-05-26 21:17:30 +0200320 return;
321
Yinghai Luf3eee542009-12-14 11:52:15 +0900322 /* disable them all at first */
Jan Beulich24d9b702011-01-10 16:20:23 +0000323 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Yinghai Lu55c0d722008-04-19 01:31:11 -0700324 int bus;
325 int dev_base, dev_limit;
Yinghai Luaaf23042008-01-30 13:33:09 +0100326
Jan Beulich24d9b702011-01-10 16:20:23 +0000327 bus = amd_nb_bus_dev_ranges[i].bus;
328 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
329 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700330
331 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200332 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700333 continue;
334
335 ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
Borislav Petkov57ab43e2010-09-03 18:39:39 +0200336 ctl &= ~GARTEN;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700337 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
338 }
Yinghai Luaaf23042008-01-30 13:33:09 +0100339 }
340
341}
342
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700343static int __initdata printed_gart_size_msg;
344
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400345int __init gart_iommu_hole_init(void)
Ingo Molnarc140df92008-01-30 13:30:09 +0100346{
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700347 u32 agp_aper_base = 0, agp_aper_order = 0;
Andi Kleen50895c52005-11-05 17:25:53 +0100348 u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 u64 aper_base, last_aper_base = 0;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700350 int fix, slot, valid_agp = 0;
351 int i, node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Aravind Gopalakrishnan1b457422015-04-07 16:46:37 -0500353 if (!amd_gart_present())
354 return -ENODEV;
355
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200356 if (gart_iommu_aperture_disabled || !fix_aperture ||
357 !early_pci_allowed())
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400358 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600360 pr_info("Checking aperture...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700362 if (!fallback_aper_force)
363 agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 fix = 0;
Yinghai Lu47db4c32008-01-30 13:33:18 +0100366 node = 0;
Jan Beulich24d9b702011-01-10 16:20:23 +0000367 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Yinghai Lu55c0d722008-04-19 01:31:11 -0700368 int bus;
369 int dev_base, dev_limit;
Joerg Roedel4b838732010-04-07 12:57:35 +0200370 u32 ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Jan Beulich24d9b702011-01-10 16:20:23 +0000372 bus = amd_nb_bus_dev_ranges[i].bus;
373 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
374 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Yinghai Lu55c0d722008-04-19 01:31:11 -0700376 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200377 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700378 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Yinghai Lu55c0d722008-04-19 01:31:11 -0700380 iommu_detected = 1;
381 gart_iommu_aperture = 1;
FUJITA Tomonoride957622009-11-10 19:46:14 +0900382 x86_init.iommu.iommu_init = gart_iommu_init;
Ingo Molnarc140df92008-01-30 13:30:09 +0100383
Joerg Roedel4b838732010-04-07 12:57:35 +0200384 ctl = read_pci_config(bus, slot, 3,
385 AMD64_GARTAPERTURECTL);
386
387 /*
388 * Before we do anything else disable the GART. It may
389 * still be enabled if we boot into a crash-kernel here.
390 * Reconfiguring the GART while it is enabled could have
391 * unknown side-effects.
392 */
393 ctl &= ~GARTEN;
394 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
395
396 aper_order = (ctl >> 1) & 7;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700397 aper_size = (32 * 1024 * 1024) << aper_order;
398 aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
399 aper_base <<= 25;
400
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600401 pr_info("Node %d: aperture [bus addr %#010Lx-%#010Lx] (%uMB)\n",
402 node, aper_base, aper_base + aper_size - 1,
403 aper_size >> 20);
Yinghai Lu55c0d722008-04-19 01:31:11 -0700404 node++;
405
406 if (!aperture_valid(aper_base, aper_size, 64<<20)) {
407 if (valid_agp && agp_aper_base &&
408 agp_aper_base == aper_base &&
409 agp_aper_order == aper_order) {
410 /* the same between two setting from NB and agp */
Yinghai Luc987d122008-06-24 22:14:09 -0700411 if (!no_iommu &&
412 max_pfn > MAX_DMA32_PFN &&
413 !printed_gart_size_msg) {
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600414 pr_err("you are using iommu with agp, but GART size is less than 64MB\n");
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600415 pr_err("please increase GART size in your BIOS setup\n");
416 pr_err("if BIOS doesn't have that option, contact your HW vendor!\n");
Yinghai Lu55c0d722008-04-19 01:31:11 -0700417 printed_gart_size_msg = 1;
418 }
419 } else {
420 fix = 1;
421 goto out;
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700422 }
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Yinghai Lu55c0d722008-04-19 01:31:11 -0700425 if ((last_aper_order && aper_order != last_aper_order) ||
426 (last_aper_base && aper_base != last_aper_base)) {
427 fix = 1;
428 goto out;
429 }
430 last_aper_order = aper_order;
431 last_aper_base = aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Yinghai Lu55c0d722008-04-19 01:31:11 -0700435out:
Aaron Durbin56dd6692006-09-26 10:52:40 +0200436 if (!fix && !fallback_aper_force) {
Bjorn Helgaas707d4ee2014-03-18 14:26:12 -0600437 if (last_aper_base)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400438 return 1;
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400439 return 0;
Aaron Durbin56dd6692006-09-26 10:52:40 +0200440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700442 if (!fallback_aper_force) {
443 aper_alloc = agp_aper_base;
444 aper_order = agp_aper_order;
445 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100446
447 if (aper_alloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* Got the aperture from the AGP bridge */
Yinghai Luc987d122008-06-24 22:14:09 -0700449 } else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 force_iommu ||
451 valid_agp ||
Ingo Molnarc140df92008-01-30 13:30:09 +0100452 fallback_aper_force) {
Aravind Gopalakrishnan1b457422015-04-07 16:46:37 -0500453 pr_info("Your BIOS doesn't leave an aperture memory hole\n");
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600454 pr_info("Please enable the IOMMU option in the BIOS setup\n");
Bjorn Helgaasc96ec952014-04-14 15:29:19 -0600455 pr_info("This costs you %dMB of RAM\n",
Bjorn Helgaasa5d32442014-04-28 15:16:33 -0600456 32 << fallback_aper_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 aper_order = fallback_aper_order;
459 aper_alloc = allocate_aperture();
Ingo Molnarc140df92008-01-30 13:30:09 +0100460 if (!aper_alloc) {
461 /*
462 * Could disable AGP and IOMMU here, but it's
463 * probably not worth it. But the later users
464 * cannot deal with bad apertures and turning
465 * on the aperture over memory causes very
466 * strange problems, so it's better to panic
467 * early.
468 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 panic("Not enough memory for aperture");
470 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100471 } else {
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400472 return 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* Fix up the north bridges */
Jan Beulich24d9b702011-01-10 16:20:23 +0000476 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Borislav Petkov260133a2010-09-03 18:39:40 +0200477 int bus, dev_base, dev_limit;
478
479 /*
480 * Don't enable translation yet but enable GART IO and CPU
481 * accesses and set DISTLBWALKPRB since GART table memory is UC.
482 */
Joerg Roedelc34151a2011-04-18 15:45:45 +0200483 u32 ctl = aper_order << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Jan Beulich24d9b702011-01-10 16:20:23 +0000485 bus = amd_nb_bus_dev_ranges[i].bus;
486 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
487 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700488 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200489 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700490 continue;
491
Borislav Petkov260133a2010-09-03 18:39:40 +0200492 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
Yinghai Lu55c0d722008-04-19 01:31:11 -0700493 write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25);
494 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100495 }
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200496
497 set_up_gart_resume(aper_order, aper_alloc);
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400498
499 return 1;
Ingo Molnarc140df92008-01-30 13:30:09 +0100500}