blob: 6e76c191a83572c07bf86c9bbc7c129c4bbc912c [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/init.h>
Yinghai Lu32e3f2b2010-12-17 16:58:40 -080016#include <linux/memblock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/mmzone.h>
18#include <linux/pci_ids.h>
19#include <linux/pci.h>
20#include <linux/bitops.h>
Aaron Durbin56dd6692006-09-26 10:52:40 +020021#include <linux/ioport.h>
Pavel Machek2050d452008-03-13 23:05:41 +010022#include <linux/suspend.h>
Catalin Marinasacde31d2009-08-27 14:29:20 +010023#include <linux/kmemleak.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/e820.h>
25#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
Aaron Durbin56dd6692006-09-26 10:52:40 +020058static struct resource gart_resource = {
59 .name = "GART",
60 .flags = IORESOURCE_MEM,
61};
62
63static void __init insert_aperture_resource(u32 aper_base, u32 aper_size)
64{
65 gart_resource.start = aper_base;
66 gart_resource.end = aper_base + aper_size - 1;
67 insert_resource(&iomem_resource, &gart_resource);
68}
69
Andrew Morton42442ed2005-06-08 15:49:25 -070070/* This code runs before the PCI subsystem is initialized, so just
71 access the northbridge directly. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Ingo Molnarc140df92008-01-30 13:30:09 +010073static u32 __init allocate_aperture(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 u32 aper_size;
Yinghai Lu32e3f2b2010-12-17 16:58:40 -080076 unsigned long addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Yinghai Lu7677b2e2008-04-14 20:40:37 -070078 /* aper_size should <= 1G */
79 if (fallback_aper_order > 5)
80 fallback_aper_order = 5;
Ingo Molnarc140df92008-01-30 13:30:09 +010081 aper_size = (32 * 1024 * 1024) << fallback_aper_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Ingo Molnarc140df92008-01-30 13:30:09 +010083 /*
84 * Aperture has to be naturally aligned. This means a 2GB aperture
85 * won't have much chance of finding a place in the lower 4GB of
86 * memory. Unfortunately we cannot move it up because that would
87 * make the IOMMU useless.
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
Joerg Roedelc387aa32011-04-18 15:45:43 +020089 addr = memblock_find_in_range(GART_MIN_ADDR, GART_MAX_ADDR,
90 aper_size, aper_size);
Tejun Heo1f5026a2011-07-12 09:58:09 +020091 if (!addr || addr + aper_size > GART_MAX_ADDR) {
Yinghai Lu32e3f2b2010-12-17 16:58:40 -080092 printk(KERN_ERR
93 "Cannot allocate aperture memory hole (%lx,%uK)\n",
94 addr, aper_size>>10);
95 return 0;
96 }
Tejun Heo24aa0782011-07-12 11:16:06 +020097 memblock_reserve(addr, aper_size);
Catalin Marinasacde31d2009-08-27 14:29:20 +010098 /*
99 * Kmemleak should not scan this block as it may not be mapped via the
100 * kernel direct mapping.
101 */
Yinghai Lu32e3f2b2010-12-17 16:58:40 -0800102 kmemleak_ignore(phys_to_virt(addr));
Ingo Molnar31183ba2008-01-30 13:30:10 +0100103 printk(KERN_INFO "Mapping aperture over %d KB of RAM @ %lx\n",
Yinghai Lu32e3f2b2010-12-17 16:58:40 -0800104 aper_size >> 10, addr);
105 insert_aperture_resource((u32)addr, aper_size);
106 register_nosave_region(addr >> PAGE_SHIFT,
107 (addr+aper_size) >> PAGE_SHIFT);
Ingo Molnarc140df92008-01-30 13:30:09 +0100108
Yinghai Lu32e3f2b2010-12-17 16:58:40 -0800109 return (u32)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Andrew Morton42442ed2005-06-08 15:49:25 -0700113/* Find a PCI capability */
Pavel Machekdd564d02008-05-27 18:03:56 +0200114static u32 __init find_cap(int bus, int slot, int func, int cap)
Ingo Molnarc140df92008-01-30 13:30:09 +0100115{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 int bytes;
Ingo Molnarc140df92008-01-30 13:30:09 +0100117 u8 pos;
118
Yinghai Lu55c0d722008-04-19 01:31:11 -0700119 if (!(read_pci_config_16(bus, slot, func, PCI_STATUS) &
Ingo Molnarc140df92008-01-30 13:30:09 +0100120 PCI_STATUS_CAP_LIST))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100122
Yinghai Lu55c0d722008-04-19 01:31:11 -0700123 pos = read_pci_config_byte(bus, slot, func, PCI_CAPABILITY_LIST);
Ingo Molnarc140df92008-01-30 13:30:09 +0100124 for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 u8 id;
Ingo Molnarc140df92008-01-30 13:30:09 +0100126
127 pos &= ~3;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700128 id = read_pci_config_byte(bus, slot, func, pos+PCI_CAP_LIST_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (id == 0xff)
130 break;
Ingo Molnarc140df92008-01-30 13:30:09 +0100131 if (id == cap)
132 return pos;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700133 pos = read_pci_config_byte(bus, slot, func,
Ingo Molnarc140df92008-01-30 13:30:09 +0100134 pos+PCI_CAP_LIST_NEXT);
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100137}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139/* Read a standard AGPv3 bridge header */
Pavel Machekdd564d02008-05-27 18:03:56 +0200140static u32 __init read_agp(int bus, int slot, int func, int cap, u32 *order)
Ingo Molnarc140df92008-01-30 13:30:09 +0100141{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 u32 apsize;
143 u32 apsizereg;
144 int nbits;
145 u32 aper_low, aper_hi;
146 u64 aper;
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700147 u32 old_order;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Yinghai Lu55c0d722008-04-19 01:31:11 -0700149 printk(KERN_INFO "AGP bridge at %02x:%02x:%02x\n", bus, slot, func);
150 apsizereg = read_pci_config_16(bus, slot, func, cap + 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (apsizereg == 0xffffffff) {
Ingo Molnar31183ba2008-01-30 13:30:10 +0100152 printk(KERN_ERR "APSIZE in AGP bridge unreadable\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return 0;
154 }
155
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700156 /* old_order could be the value from NB gart setting */
157 old_order = *order;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 apsize = apsizereg & 0xfff;
160 /* Some BIOS use weird encodings not in the AGPv3 table. */
Ingo Molnarc140df92008-01-30 13:30:09 +0100161 if (apsize & 0xff)
162 apsize |= 0xf00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 nbits = hweight16(apsize);
164 *order = 7 - nbits;
165 if ((int)*order < 0) /* < 32MB */
166 *order = 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100167
Yinghai Lu55c0d722008-04-19 01:31:11 -0700168 aper_low = read_pci_config(bus, slot, func, 0x10);
169 aper_hi = read_pci_config(bus, slot, func, 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
171
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700172 /*
173 * On some sick chips, APSIZE is 0. It means it wants 4G
174 * so let double check that order, and lets trust AMD NB settings:
175 */
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700176 printk(KERN_INFO "Aperture from AGP @ %Lx old size %u MB\n",
177 aper, 32 << old_order);
178 if (aper + (32ULL<<(20 + *order)) > 0x100000000ULL) {
Yinghai Lu1edc1ab2008-04-13 01:11:41 -0700179 printk(KERN_INFO "Aperture size %u MB (APSIZE %x) is not right, using settings from NB\n",
180 32 << *order, apsizereg);
181 *order = old_order;
182 }
183
Ingo Molnar31183ba2008-01-30 13:30:10 +0100184 printk(KERN_INFO "Aperture from AGP @ %Lx size %u MB (APSIZE %x)\n",
185 aper, 32 << *order, apsizereg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700187 if (!aperture_valid(aper, (32*1024*1024) << *order, 32<<20))
Ingo Molnarc140df92008-01-30 13:30:09 +0100188 return 0;
189 return (u32)aper;
190}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Ingo Molnarc140df92008-01-30 13:30:09 +0100192/*
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 *
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200200 * All AMD AGP bridges are AGPv3 compliant, so we can do this scan
Ingo Molnarc140df92008-01-30 13:30:09 +0100201 * 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 */
Pavel Machekdd564d02008-05-27 18:03:56 +0200205static u32 __init search_agp_bridge(u32 *order, int *valid_agp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Yinghai Lu55c0d722008-04-19 01:31:11 -0700207 int bus, slot, func;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* Poor man's PCI discovery */
Yinghai Lu55c0d722008-04-19 01:31:11 -0700210 for (bus = 0; bus < 256; bus++) {
Ingo Molnarc140df92008-01-30 13:30:09 +0100211 for (slot = 0; slot < 32; slot++) {
212 for (func = 0; func < 8; func++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 u32 class, cap;
214 u8 type;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700215 class = read_pci_config(bus, slot, func,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 PCI_CLASS_REVISION);
217 if (class == 0xffffffff)
Ingo Molnarc140df92008-01-30 13:30:09 +0100218 break;
219
220 switch (class >> 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 case PCI_CLASS_BRIDGE_HOST:
222 case PCI_CLASS_BRIDGE_OTHER: /* needed? */
223 /* AGP bridge? */
Yinghai Lu55c0d722008-04-19 01:31:11 -0700224 cap = find_cap(bus, slot, func,
Ingo Molnarc140df92008-01-30 13:30:09 +0100225 PCI_CAP_ID_AGP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (!cap)
227 break;
Ingo Molnarc140df92008-01-30 13:30:09 +0100228 *valid_agp = 1;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700229 return read_agp(bus, slot, func, cap,
Ingo Molnarc140df92008-01-30 13:30:09 +0100230 order);
231 }
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* No multi-function device? */
Yinghai Lu55c0d722008-04-19 01:31:11 -0700234 type = read_pci_config_byte(bus, slot, func,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 PCI_HEADER_TYPE);
236 if (!(type & 0x80))
237 break;
Ingo Molnarc140df92008-01-30 13:30:09 +0100238 }
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
Ingo Molnar31183ba2008-01-30 13:30:10 +0100241 printk(KERN_INFO "No AGP bridge found\n");
Ingo Molnarc140df92008-01-30 13:30:09 +0100242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return 0;
244}
245
Yinghai Luaaf23042008-01-30 13:33:09 +0100246static int gart_fix_e820 __initdata = 1;
247
248static int __init parse_gart_mem(char *p)
249{
250 if (!p)
251 return -EINVAL;
252
253 if (!strncmp(p, "off", 3))
254 gart_fix_e820 = 0;
255 else if (!strncmp(p, "on", 2))
256 gart_fix_e820 = 1;
257
258 return 0;
259}
260early_param("gart_fix_e820", parse_gart_mem);
261
262void __init early_gart_iommu_check(void)
263{
264 /*
265 * in case it is enabled before, esp for kexec/kdump,
266 * previous kernel already enable that. memset called
267 * by allocate_aperture/__alloc_bootmem_nopanic cause restart.
268 * or second kernel have different position for GART hole. and new
269 * kernel could use hole as RAM that is still used by GART set by
270 * first kernel
271 * or BIOS forget to put that in reserved.
272 * try to update e820 to make that region as reserved.
273 */
Andi Kleenfa10ba62010-07-20 15:19:49 -0700274 u32 agp_aper_order = 0;
Yinghai Luf3eee542009-12-14 11:52:15 +0900275 int i, fix, slot, valid_agp = 0;
Yinghai Luaaf23042008-01-30 13:33:09 +0100276 u32 ctl;
277 u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
278 u64 aper_base = 0, last_aper_base = 0;
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200279 int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0;
Yinghai Luaaf23042008-01-30 13:33:09 +0100280
281 if (!early_pci_allowed())
282 return;
283
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200284 /* This is mostly duplicate of iommu_hole_init */
Andi Kleenfa10ba62010-07-20 15:19:49 -0700285 search_agp_bridge(&agp_aper_order, &valid_agp);
Yinghai Luf3eee542009-12-14 11:52:15 +0900286
Yinghai Luaaf23042008-01-30 13:33:09 +0100287 fix = 0;
Jan Beulich24d9b702011-01-10 16:20:23 +0000288 for (i = 0; amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Yinghai Lu55c0d722008-04-19 01:31:11 -0700289 int bus;
290 int dev_base, dev_limit;
Yinghai Luaaf23042008-01-30 13:33:09 +0100291
Jan Beulich24d9b702011-01-10 16:20:23 +0000292 bus = amd_nb_bus_dev_ranges[i].bus;
293 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
294 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Yinghai Luaaf23042008-01-30 13:33:09 +0100295
Yinghai Lu55c0d722008-04-19 01:31:11 -0700296 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200297 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700298 continue;
299
300 ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
Borislav Petkov57ab43e2010-09-03 18:39:39 +0200301 aper_enabled = ctl & GARTEN;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700302 aper_order = (ctl >> 1) & 7;
303 aper_size = (32 * 1024 * 1024) << aper_order;
304 aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
305 aper_base <<= 25;
306
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200307 if (last_valid) {
308 if ((aper_order != last_aper_order) ||
309 (aper_base != last_aper_base) ||
310 (aper_enabled != last_aper_enabled)) {
311 fix = 1;
312 break;
313 }
Yinghai Lu55c0d722008-04-19 01:31:11 -0700314 }
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200315
Yinghai Lu55c0d722008-04-19 01:31:11 -0700316 last_aper_order = aper_order;
317 last_aper_base = aper_base;
318 last_aper_enabled = aper_enabled;
Pavel Machekfa5b8a32008-05-26 20:40:47 +0200319 last_valid = 1;
Yinghai Luaaf23042008-01-30 13:33:09 +0100320 }
Yinghai Luaaf23042008-01-30 13:33:09 +0100321 }
322
323 if (!fix && !aper_enabled)
324 return;
325
326 if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
327 fix = 1;
328
329 if (gart_fix_e820 && !fix && aper_enabled) {
Yinghai Lu07545572008-06-21 03:50:47 -0700330 if (e820_any_mapped(aper_base, aper_base + aper_size,
331 E820_RAM)) {
Pavel Machek0abbc782008-05-20 16:27:17 +0200332 /* reserve it, so we can reuse it in second kernel */
Yinghai Luaaf23042008-01-30 13:33:09 +0100333 printk(KERN_INFO "update e820 for GART\n");
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700334 e820_add_region(aper_base, aper_size, E820_RESERVED);
Yinghai Luaaf23042008-01-30 13:33:09 +0100335 update_e820();
336 }
Yinghai Luaaf23042008-01-30 13:33:09 +0100337 }
338
Yinghai Luf3eee542009-12-14 11:52:15 +0900339 if (valid_agp)
Pavel Machek4f384f82008-05-26 21:17:30 +0200340 return;
341
Yinghai Luf3eee542009-12-14 11:52:15 +0900342 /* disable them all at first */
Jan Beulich24d9b702011-01-10 16:20:23 +0000343 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Yinghai Lu55c0d722008-04-19 01:31:11 -0700344 int bus;
345 int dev_base, dev_limit;
Yinghai Luaaf23042008-01-30 13:33:09 +0100346
Jan Beulich24d9b702011-01-10 16:20:23 +0000347 bus = amd_nb_bus_dev_ranges[i].bus;
348 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
349 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700350
351 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200352 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700353 continue;
354
355 ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
Borislav Petkov57ab43e2010-09-03 18:39:39 +0200356 ctl &= ~GARTEN;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700357 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
358 }
Yinghai Luaaf23042008-01-30 13:33:09 +0100359 }
360
361}
362
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700363static int __initdata printed_gart_size_msg;
364
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400365int __init gart_iommu_hole_init(void)
Ingo Molnarc140df92008-01-30 13:30:09 +0100366{
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700367 u32 agp_aper_base = 0, agp_aper_order = 0;
Andi Kleen50895c52005-11-05 17:25:53 +0100368 u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 u64 aper_base, last_aper_base = 0;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700370 int fix, slot, valid_agp = 0;
371 int i, node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Joerg Roedel0440d4c2007-10-24 12:49:50 +0200373 if (gart_iommu_aperture_disabled || !fix_aperture ||
374 !early_pci_allowed())
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400375 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Dan Aloni753811d2007-07-21 17:11:36 +0200377 printk(KERN_INFO "Checking aperture...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700379 if (!fallback_aper_force)
380 agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp);
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 fix = 0;
Yinghai Lu47db4c32008-01-30 13:33:18 +0100383 node = 0;
Jan Beulich24d9b702011-01-10 16:20:23 +0000384 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Yinghai Lu55c0d722008-04-19 01:31:11 -0700385 int bus;
386 int dev_base, dev_limit;
Joerg Roedel4b838732010-04-07 12:57:35 +0200387 u32 ctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Jan Beulich24d9b702011-01-10 16:20:23 +0000389 bus = amd_nb_bus_dev_ranges[i].bus;
390 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
391 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Yinghai Lu55c0d722008-04-19 01:31:11 -0700393 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200394 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700395 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Yinghai Lu55c0d722008-04-19 01:31:11 -0700397 iommu_detected = 1;
398 gart_iommu_aperture = 1;
FUJITA Tomonoride957622009-11-10 19:46:14 +0900399 x86_init.iommu.iommu_init = gart_iommu_init;
Ingo Molnarc140df92008-01-30 13:30:09 +0100400
Joerg Roedel4b838732010-04-07 12:57:35 +0200401 ctl = read_pci_config(bus, slot, 3,
402 AMD64_GARTAPERTURECTL);
403
404 /*
405 * Before we do anything else disable the GART. It may
406 * still be enabled if we boot into a crash-kernel here.
407 * Reconfiguring the GART while it is enabled could have
408 * unknown side-effects.
409 */
410 ctl &= ~GARTEN;
411 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
412
413 aper_order = (ctl >> 1) & 7;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700414 aper_size = (32 * 1024 * 1024) << aper_order;
415 aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
416 aper_base <<= 25;
417
418 printk(KERN_INFO "Node %d: aperture @ %Lx size %u MB\n",
419 node, aper_base, aper_size >> 20);
420 node++;
421
422 if (!aperture_valid(aper_base, aper_size, 64<<20)) {
423 if (valid_agp && agp_aper_base &&
424 agp_aper_base == aper_base &&
425 agp_aper_order == aper_order) {
426 /* the same between two setting from NB and agp */
Yinghai Luc987d122008-06-24 22:14:09 -0700427 if (!no_iommu &&
428 max_pfn > MAX_DMA32_PFN &&
429 !printed_gart_size_msg) {
Yinghai Lu55c0d722008-04-19 01:31:11 -0700430 printk(KERN_ERR "you are using iommu with agp, but GART size is less than 64M\n");
431 printk(KERN_ERR "please increase GART size in your BIOS setup\n");
432 printk(KERN_ERR "if BIOS doesn't have that option, contact your HW vendor!\n");
433 printed_gart_size_msg = 1;
434 }
435 } else {
436 fix = 1;
437 goto out;
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700438 }
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Yinghai Lu55c0d722008-04-19 01:31:11 -0700441 if ((last_aper_order && aper_order != last_aper_order) ||
442 (last_aper_base && aper_base != last_aper_base)) {
443 fix = 1;
444 goto out;
445 }
446 last_aper_order = aper_order;
447 last_aper_base = aper_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Yinghai Lu55c0d722008-04-19 01:31:11 -0700451out:
Aaron Durbin56dd6692006-09-26 10:52:40 +0200452 if (!fix && !fallback_aper_force) {
453 if (last_aper_base) {
454 unsigned long n = (32 * 1024 * 1024) << last_aper_order;
Ingo Molnarc140df92008-01-30 13:30:09 +0100455
Aaron Durbin56dd6692006-09-26 10:52:40 +0200456 insert_aperture_resource((u32)last_aper_base, n);
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400457 return 1;
Aaron Durbin56dd6692006-09-26 10:52:40 +0200458 }
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400459 return 0;
Aaron Durbin56dd6692006-09-26 10:52:40 +0200460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Yinghai Lu8c9fd912008-04-13 18:42:31 -0700462 if (!fallback_aper_force) {
463 aper_alloc = agp_aper_base;
464 aper_order = agp_aper_order;
465 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100466
467 if (aper_alloc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* Got the aperture from the AGP bridge */
Yinghai Luc987d122008-06-24 22:14:09 -0700469 } else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 force_iommu ||
471 valid_agp ||
Ingo Molnarc140df92008-01-30 13:30:09 +0100472 fallback_aper_force) {
Adam Jackson9b156842008-09-29 14:52:03 -0400473 printk(KERN_INFO
Ingo Molnar31183ba2008-01-30 13:30:10 +0100474 "Your BIOS doesn't leave a aperture memory hole\n");
Adam Jackson9b156842008-09-29 14:52:03 -0400475 printk(KERN_INFO
Ingo Molnar31183ba2008-01-30 13:30:10 +0100476 "Please enable the IOMMU option in the BIOS setup\n");
Adam Jackson9b156842008-09-29 14:52:03 -0400477 printk(KERN_INFO
Ingo Molnar31183ba2008-01-30 13:30:10 +0100478 "This costs you %d MB of RAM\n",
479 32 << fallback_aper_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 aper_order = fallback_aper_order;
482 aper_alloc = allocate_aperture();
Ingo Molnarc140df92008-01-30 13:30:09 +0100483 if (!aper_alloc) {
484 /*
485 * Could disable AGP and IOMMU here, but it's
486 * probably not worth it. But the later users
487 * cannot deal with bad apertures and turning
488 * on the aperture over memory causes very
489 * strange problems, so it's better to panic
490 * early.
491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 panic("Not enough memory for aperture");
493 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100494 } else {
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400495 return 0;
Ingo Molnarc140df92008-01-30 13:30:09 +0100496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* Fix up the north bridges */
Jan Beulich24d9b702011-01-10 16:20:23 +0000499 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
Borislav Petkov260133a2010-09-03 18:39:40 +0200500 int bus, dev_base, dev_limit;
501
502 /*
503 * Don't enable translation yet but enable GART IO and CPU
504 * accesses and set DISTLBWALKPRB since GART table memory is UC.
505 */
Joerg Roedelc34151a2011-04-18 15:45:45 +0200506 u32 ctl = aper_order << 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Jan Beulich24d9b702011-01-10 16:20:23 +0000508 bus = amd_nb_bus_dev_ranges[i].bus;
509 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
510 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
Yinghai Lu55c0d722008-04-19 01:31:11 -0700511 for (slot = dev_base; slot < dev_limit; slot++) {
Hans Rosenfeldeec1d4f2010-10-29 17:14:30 +0200512 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
Yinghai Lu55c0d722008-04-19 01:31:11 -0700513 continue;
514
Borislav Petkov260133a2010-09-03 18:39:40 +0200515 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
Yinghai Lu55c0d722008-04-19 01:31:11 -0700516 write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25);
517 }
Ingo Molnarc140df92008-01-30 13:30:09 +0100518 }
Rafael J. Wysocki6703f6d2008-06-10 00:10:48 +0200519
520 set_up_gart_resume(aper_order, aper_alloc);
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -0400521
522 return 1;
Ingo Molnarc140df92008-01-30 13:30:09 +0100523}