xref: /linux/arch/x86/kernel/aperture_64.c (revision 480125ba49ba62be93beea37770f266846e077ab)
1250c2277SThomas Gleixner /*
2250c2277SThomas Gleixner  * Firmware replacement code.
3250c2277SThomas Gleixner  *
48caac563SPavel Machek  * Work around broken BIOSes that don't set an aperture, only set the
58caac563SPavel Machek  * aperture in the AGP bridge, or set too small aperture.
68caac563SPavel Machek  *
7250c2277SThomas Gleixner  * If all fails map the aperture over some low memory.  This is cheaper than
8250c2277SThomas Gleixner  * doing bounce buffering. The memory is lost. This is done at early boot
9250c2277SThomas Gleixner  * because only the bootmem allocator can allocate 32+MB.
10250c2277SThomas Gleixner  *
11250c2277SThomas Gleixner  * Copyright 2002 Andi Kleen, SuSE Labs.
12250c2277SThomas Gleixner  */
13250c2277SThomas Gleixner #include <linux/kernel.h>
14250c2277SThomas Gleixner #include <linux/types.h>
15250c2277SThomas Gleixner #include <linux/init.h>
16250c2277SThomas Gleixner #include <linux/bootmem.h>
17250c2277SThomas Gleixner #include <linux/mmzone.h>
18250c2277SThomas Gleixner #include <linux/pci_ids.h>
19250c2277SThomas Gleixner #include <linux/pci.h>
20250c2277SThomas Gleixner #include <linux/bitops.h>
21250c2277SThomas Gleixner #include <linux/ioport.h>
222050d45dSPavel Machek #include <linux/suspend.h>
23acde31dcSCatalin Marinas #include <linux/kmemleak.h>
24250c2277SThomas Gleixner #include <asm/e820.h>
25250c2277SThomas Gleixner #include <asm/io.h>
2646a7fa27SFUJITA Tomonori #include <asm/iommu.h>
27395624fcSJoerg Roedel #include <asm/gart.h>
28250c2277SThomas Gleixner #include <asm/pci-direct.h>
29250c2277SThomas Gleixner #include <asm/dma.h>
30250c2277SThomas Gleixner #include <asm/k8.h>
31de957628SFUJITA Tomonori #include <asm/x86_init.h>
32250c2277SThomas Gleixner 
330440d4c0SJoerg Roedel int gart_iommu_aperture;
347de6a4cdSPavel Machek int gart_iommu_aperture_disabled __initdata;
357de6a4cdSPavel Machek int gart_iommu_aperture_allowed __initdata;
36250c2277SThomas Gleixner 
37250c2277SThomas Gleixner int fallback_aper_order __initdata = 1; /* 64MB */
387de6a4cdSPavel Machek int fallback_aper_force __initdata;
39250c2277SThomas Gleixner 
40250c2277SThomas Gleixner int fix_aperture __initdata = 1;
41250c2277SThomas Gleixner 
4255c0d721SYinghai Lu struct bus_dev_range {
4355c0d721SYinghai Lu 	int bus;
4455c0d721SYinghai Lu 	int dev_base;
4555c0d721SYinghai Lu 	int dev_limit;
4655c0d721SYinghai Lu };
4755c0d721SYinghai Lu 
4855c0d721SYinghai Lu static struct bus_dev_range bus_dev_ranges[] __initdata = {
4955c0d721SYinghai Lu 	{ 0x00, 0x18, 0x20},
5055c0d721SYinghai Lu 	{ 0xff, 0x00, 0x20},
5155c0d721SYinghai Lu 	{ 0xfe, 0x00, 0x20}
5255c0d721SYinghai Lu };
5355c0d721SYinghai Lu 
54250c2277SThomas Gleixner static struct resource gart_resource = {
55250c2277SThomas Gleixner 	.name	= "GART",
56250c2277SThomas Gleixner 	.flags	= IORESOURCE_MEM,
57250c2277SThomas Gleixner };
58250c2277SThomas Gleixner 
59250c2277SThomas Gleixner static void __init insert_aperture_resource(u32 aper_base, u32 aper_size)
60250c2277SThomas Gleixner {
61250c2277SThomas Gleixner 	gart_resource.start = aper_base;
62250c2277SThomas Gleixner 	gart_resource.end = aper_base + aper_size - 1;
63250c2277SThomas Gleixner 	insert_resource(&iomem_resource, &gart_resource);
64250c2277SThomas Gleixner }
65250c2277SThomas Gleixner 
66250c2277SThomas Gleixner /* This code runs before the PCI subsystem is initialized, so just
67250c2277SThomas Gleixner    access the northbridge directly. */
68250c2277SThomas Gleixner 
69250c2277SThomas Gleixner static u32 __init allocate_aperture(void)
70250c2277SThomas Gleixner {
71250c2277SThomas Gleixner 	u32 aper_size;
72250c2277SThomas Gleixner 	void *p;
73250c2277SThomas Gleixner 
747677b2efSYinghai Lu 	/* aper_size should <= 1G */
757677b2efSYinghai Lu 	if (fallback_aper_order > 5)
767677b2efSYinghai Lu 		fallback_aper_order = 5;
77250c2277SThomas Gleixner 	aper_size = (32 * 1024 * 1024) << fallback_aper_order;
78250c2277SThomas Gleixner 
79250c2277SThomas Gleixner 	/*
80c140df97SIngo Molnar 	 * Aperture has to be naturally aligned. This means a 2GB aperture
81c140df97SIngo Molnar 	 * won't have much chance of finding a place in the lower 4GB of
82c140df97SIngo Molnar 	 * memory. Unfortunately we cannot move it up because that would
83c140df97SIngo Molnar 	 * make the IOMMU useless.
84250c2277SThomas Gleixner 	 */
857677b2efSYinghai Lu 	/*
867677b2efSYinghai Lu 	 * using 512M as goal, in case kexec will load kernel_big
877677b2efSYinghai Lu 	 * that will do the on position decompress, and  could overlap with
887677b2efSYinghai Lu 	 * that positon with gart that is used.
897677b2efSYinghai Lu 	 * sequende:
907677b2efSYinghai Lu 	 * kernel_small
917677b2efSYinghai Lu 	 * ==> kexec (with kdump trigger path or previous doesn't shutdown gart)
927677b2efSYinghai Lu 	 * ==> kernel_small(gart area become e820_reserved)
937677b2efSYinghai Lu 	 * ==> kexec (with kdump trigger path or previous doesn't shutdown gart)
947677b2efSYinghai Lu 	 * ==> kerne_big (uncompressed size will be big than 64M or 128M)
957677b2efSYinghai Lu 	 * so don't use 512M below as gart iommu, leave the space for kernel
967677b2efSYinghai Lu 	 * code for safe
977677b2efSYinghai Lu 	 */
987677b2efSYinghai Lu 	p = __alloc_bootmem_nopanic(aper_size, aper_size, 512ULL<<20);
99acde31dcSCatalin Marinas 	/*
100acde31dcSCatalin Marinas 	 * Kmemleak should not scan this block as it may not be mapped via the
101acde31dcSCatalin Marinas 	 * kernel direct mapping.
102acde31dcSCatalin Marinas 	 */
103acde31dcSCatalin Marinas 	kmemleak_ignore(p);
104250c2277SThomas Gleixner 	if (!p || __pa(p)+aper_size > 0xffffffff) {
10531183ba8SIngo Molnar 		printk(KERN_ERR
10631183ba8SIngo Molnar 			"Cannot allocate aperture memory hole (%p,%uK)\n",
107250c2277SThomas Gleixner 				p, aper_size>>10);
108250c2277SThomas Gleixner 		if (p)
109250c2277SThomas Gleixner 			free_bootmem(__pa(p), aper_size);
110250c2277SThomas Gleixner 		return 0;
111250c2277SThomas Gleixner 	}
11231183ba8SIngo Molnar 	printk(KERN_INFO "Mapping aperture over %d KB of RAM @ %lx\n",
113250c2277SThomas Gleixner 			aper_size >> 10, __pa(p));
114250c2277SThomas Gleixner 	insert_aperture_resource((u32)__pa(p), aper_size);
1152050d45dSPavel Machek 	register_nosave_region((u32)__pa(p) >> PAGE_SHIFT,
1162050d45dSPavel Machek 				(u32)__pa(p+aper_size) >> PAGE_SHIFT);
117c140df97SIngo Molnar 
118250c2277SThomas Gleixner 	return (u32)__pa(p);
119250c2277SThomas Gleixner }
120250c2277SThomas Gleixner 
121250c2277SThomas Gleixner 
122250c2277SThomas Gleixner /* Find a PCI capability */
123dd564d0cSPavel Machek static u32 __init find_cap(int bus, int slot, int func, int cap)
124250c2277SThomas Gleixner {
125250c2277SThomas Gleixner 	int bytes;
126c140df97SIngo Molnar 	u8 pos;
127c140df97SIngo Molnar 
12855c0d721SYinghai Lu 	if (!(read_pci_config_16(bus, slot, func, PCI_STATUS) &
129c140df97SIngo Molnar 						PCI_STATUS_CAP_LIST))
130250c2277SThomas Gleixner 		return 0;
131c140df97SIngo Molnar 
13255c0d721SYinghai Lu 	pos = read_pci_config_byte(bus, slot, func, PCI_CAPABILITY_LIST);
133250c2277SThomas Gleixner 	for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) {
134250c2277SThomas Gleixner 		u8 id;
135c140df97SIngo Molnar 
136250c2277SThomas Gleixner 		pos &= ~3;
13755c0d721SYinghai Lu 		id = read_pci_config_byte(bus, slot, func, pos+PCI_CAP_LIST_ID);
138250c2277SThomas Gleixner 		if (id == 0xff)
139250c2277SThomas Gleixner 			break;
140250c2277SThomas Gleixner 		if (id == cap)
141250c2277SThomas Gleixner 			return pos;
14255c0d721SYinghai Lu 		pos = read_pci_config_byte(bus, slot, func,
143c140df97SIngo Molnar 						pos+PCI_CAP_LIST_NEXT);
144250c2277SThomas Gleixner 	}
145250c2277SThomas Gleixner 	return 0;
146250c2277SThomas Gleixner }
147250c2277SThomas Gleixner 
148250c2277SThomas Gleixner /* Read a standard AGPv3 bridge header */
149dd564d0cSPavel Machek static u32 __init read_agp(int bus, int slot, int func, int cap, u32 *order)
150250c2277SThomas Gleixner {
151250c2277SThomas Gleixner 	u32 apsize;
152250c2277SThomas Gleixner 	u32 apsizereg;
153250c2277SThomas Gleixner 	int nbits;
154250c2277SThomas Gleixner 	u32 aper_low, aper_hi;
155250c2277SThomas Gleixner 	u64 aper;
1561edc1ab3SYinghai Lu 	u32 old_order;
157250c2277SThomas Gleixner 
15855c0d721SYinghai Lu 	printk(KERN_INFO "AGP bridge at %02x:%02x:%02x\n", bus, slot, func);
15955c0d721SYinghai Lu 	apsizereg = read_pci_config_16(bus, slot, func, cap + 0x14);
160250c2277SThomas Gleixner 	if (apsizereg == 0xffffffff) {
16131183ba8SIngo Molnar 		printk(KERN_ERR "APSIZE in AGP bridge unreadable\n");
162250c2277SThomas Gleixner 		return 0;
163250c2277SThomas Gleixner 	}
164250c2277SThomas Gleixner 
1651edc1ab3SYinghai Lu 	/* old_order could be the value from NB gart setting */
1661edc1ab3SYinghai Lu 	old_order = *order;
1671edc1ab3SYinghai Lu 
168250c2277SThomas Gleixner 	apsize = apsizereg & 0xfff;
169250c2277SThomas Gleixner 	/* Some BIOS use weird encodings not in the AGPv3 table. */
170250c2277SThomas Gleixner 	if (apsize & 0xff)
171250c2277SThomas Gleixner 		apsize |= 0xf00;
172250c2277SThomas Gleixner 	nbits = hweight16(apsize);
173250c2277SThomas Gleixner 	*order = 7 - nbits;
174250c2277SThomas Gleixner 	if ((int)*order < 0) /* < 32MB */
175250c2277SThomas Gleixner 		*order = 0;
176250c2277SThomas Gleixner 
17755c0d721SYinghai Lu 	aper_low = read_pci_config(bus, slot, func, 0x10);
17855c0d721SYinghai Lu 	aper_hi = read_pci_config(bus, slot, func, 0x14);
179250c2277SThomas Gleixner 	aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
180250c2277SThomas Gleixner 
1811edc1ab3SYinghai Lu 	/*
1821edc1ab3SYinghai Lu 	 * On some sick chips, APSIZE is 0. It means it wants 4G
1831edc1ab3SYinghai Lu 	 * so let double check that order, and lets trust AMD NB settings:
1841edc1ab3SYinghai Lu 	 */
1858c9fd91aSYinghai Lu 	printk(KERN_INFO "Aperture from AGP @ %Lx old size %u MB\n",
1868c9fd91aSYinghai Lu 			aper, 32 << old_order);
1878c9fd91aSYinghai Lu 	if (aper + (32ULL<<(20 + *order)) > 0x100000000ULL) {
1881edc1ab3SYinghai Lu 		printk(KERN_INFO "Aperture size %u MB (APSIZE %x) is not right, using settings from NB\n",
1891edc1ab3SYinghai Lu 				32 << *order, apsizereg);
1901edc1ab3SYinghai Lu 		*order = old_order;
1911edc1ab3SYinghai Lu 	}
1921edc1ab3SYinghai Lu 
19331183ba8SIngo Molnar 	printk(KERN_INFO "Aperture from AGP @ %Lx size %u MB (APSIZE %x)\n",
194250c2277SThomas Gleixner 			aper, 32 << *order, apsizereg);
195250c2277SThomas Gleixner 
1968c9fd91aSYinghai Lu 	if (!aperture_valid(aper, (32*1024*1024) << *order, 32<<20))
197250c2277SThomas Gleixner 		return 0;
198250c2277SThomas Gleixner 	return (u32)aper;
199250c2277SThomas Gleixner }
200250c2277SThomas Gleixner 
201c140df97SIngo Molnar /*
202c140df97SIngo Molnar  * Look for an AGP bridge. Windows only expects the aperture in the
203c140df97SIngo Molnar  * AGP bridge and some BIOS forget to initialize the Northbridge too.
204c140df97SIngo Molnar  * Work around this here.
205c140df97SIngo Molnar  *
206c140df97SIngo Molnar  * Do an PCI bus scan by hand because we're running before the PCI
207c140df97SIngo Molnar  * subsystem.
208c140df97SIngo Molnar  *
209c140df97SIngo Molnar  * All K8 AGP bridges are AGPv3 compliant, so we can do this scan
210c140df97SIngo Molnar  * generically. It's probably overkill to always scan all slots because
211c140df97SIngo Molnar  * the AGP bridges should be always an own bus on the HT hierarchy,
212c140df97SIngo Molnar  * but do it here for future safety.
213c140df97SIngo Molnar  */
214dd564d0cSPavel Machek static u32 __init search_agp_bridge(u32 *order, int *valid_agp)
215250c2277SThomas Gleixner {
21655c0d721SYinghai Lu 	int bus, slot, func;
217250c2277SThomas Gleixner 
218250c2277SThomas Gleixner 	/* Poor man's PCI discovery */
21955c0d721SYinghai Lu 	for (bus = 0; bus < 256; bus++) {
220250c2277SThomas Gleixner 		for (slot = 0; slot < 32; slot++) {
221250c2277SThomas Gleixner 			for (func = 0; func < 8; func++) {
222250c2277SThomas Gleixner 				u32 class, cap;
223250c2277SThomas Gleixner 				u8 type;
22455c0d721SYinghai Lu 				class = read_pci_config(bus, slot, func,
225250c2277SThomas Gleixner 							PCI_CLASS_REVISION);
226250c2277SThomas Gleixner 				if (class == 0xffffffff)
227250c2277SThomas Gleixner 					break;
228250c2277SThomas Gleixner 
229250c2277SThomas Gleixner 				switch (class >> 16) {
230250c2277SThomas Gleixner 				case PCI_CLASS_BRIDGE_HOST:
231250c2277SThomas Gleixner 				case PCI_CLASS_BRIDGE_OTHER: /* needed? */
232250c2277SThomas Gleixner 					/* AGP bridge? */
23355c0d721SYinghai Lu 					cap = find_cap(bus, slot, func,
234c140df97SIngo Molnar 							PCI_CAP_ID_AGP);
235250c2277SThomas Gleixner 					if (!cap)
236250c2277SThomas Gleixner 						break;
237250c2277SThomas Gleixner 					*valid_agp = 1;
23855c0d721SYinghai Lu 					return read_agp(bus, slot, func, cap,
239c140df97SIngo Molnar 							order);
240250c2277SThomas Gleixner 				}
241250c2277SThomas Gleixner 
242250c2277SThomas Gleixner 				/* No multi-function device? */
24355c0d721SYinghai Lu 				type = read_pci_config_byte(bus, slot, func,
244250c2277SThomas Gleixner 							       PCI_HEADER_TYPE);
245250c2277SThomas Gleixner 				if (!(type & 0x80))
246250c2277SThomas Gleixner 					break;
247250c2277SThomas Gleixner 			}
248250c2277SThomas Gleixner 		}
249250c2277SThomas Gleixner 	}
25031183ba8SIngo Molnar 	printk(KERN_INFO "No AGP bridge found\n");
251c140df97SIngo Molnar 
252250c2277SThomas Gleixner 	return 0;
253250c2277SThomas Gleixner }
254250c2277SThomas Gleixner 
255aaf23042SYinghai Lu static int gart_fix_e820 __initdata = 1;
256aaf23042SYinghai Lu 
257aaf23042SYinghai Lu static int __init parse_gart_mem(char *p)
258aaf23042SYinghai Lu {
259aaf23042SYinghai Lu 	if (!p)
260aaf23042SYinghai Lu 		return -EINVAL;
261aaf23042SYinghai Lu 
262aaf23042SYinghai Lu 	if (!strncmp(p, "off", 3))
263aaf23042SYinghai Lu 		gart_fix_e820 = 0;
264aaf23042SYinghai Lu 	else if (!strncmp(p, "on", 2))
265aaf23042SYinghai Lu 		gart_fix_e820 = 1;
266aaf23042SYinghai Lu 
267aaf23042SYinghai Lu 	return 0;
268aaf23042SYinghai Lu }
269aaf23042SYinghai Lu early_param("gart_fix_e820", parse_gart_mem);
270aaf23042SYinghai Lu 
271aaf23042SYinghai Lu void __init early_gart_iommu_check(void)
272aaf23042SYinghai Lu {
273aaf23042SYinghai Lu 	/*
274aaf23042SYinghai Lu 	 * in case it is enabled before, esp for kexec/kdump,
275aaf23042SYinghai Lu 	 * previous kernel already enable that. memset called
276aaf23042SYinghai Lu 	 * by allocate_aperture/__alloc_bootmem_nopanic cause restart.
277aaf23042SYinghai Lu 	 * or second kernel have different position for GART hole. and new
278aaf23042SYinghai Lu 	 * kernel could use hole as RAM that is still used by GART set by
279aaf23042SYinghai Lu 	 * first kernel
280aaf23042SYinghai Lu 	 * or BIOS forget to put that in reserved.
281aaf23042SYinghai Lu 	 * try to update e820 to make that region as reserved.
282aaf23042SYinghai Lu 	 */
283fa10ba64SAndi Kleen 	u32 agp_aper_order = 0;
284f3eee542SYinghai Lu 	int i, fix, slot, valid_agp = 0;
285aaf23042SYinghai Lu 	u32 ctl;
286aaf23042SYinghai Lu 	u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
287aaf23042SYinghai Lu 	u64 aper_base = 0, last_aper_base = 0;
288fa5b8a30SPavel Machek 	int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0;
289aaf23042SYinghai Lu 
290aaf23042SYinghai Lu 	if (!early_pci_allowed())
291aaf23042SYinghai Lu 		return;
292aaf23042SYinghai Lu 
293fa5b8a30SPavel Machek 	/* This is mostly duplicate of iommu_hole_init */
294fa10ba64SAndi Kleen 	search_agp_bridge(&agp_aper_order, &valid_agp);
295f3eee542SYinghai Lu 
296aaf23042SYinghai Lu 	fix = 0;
29755c0d721SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) {
29855c0d721SYinghai Lu 		int bus;
29955c0d721SYinghai Lu 		int dev_base, dev_limit;
30055c0d721SYinghai Lu 
30155c0d721SYinghai Lu 		bus = bus_dev_ranges[i].bus;
30255c0d721SYinghai Lu 		dev_base = bus_dev_ranges[i].dev_base;
30355c0d721SYinghai Lu 		dev_limit = bus_dev_ranges[i].dev_limit;
30455c0d721SYinghai Lu 
30555c0d721SYinghai Lu 		for (slot = dev_base; slot < dev_limit; slot++) {
30655c0d721SYinghai Lu 			if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00)))
307aaf23042SYinghai Lu 				continue;
308aaf23042SYinghai Lu 
30955c0d721SYinghai Lu 			ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
31055c0d721SYinghai Lu 			aper_enabled = ctl & AMD64_GARTEN;
311aaf23042SYinghai Lu 			aper_order = (ctl >> 1) & 7;
312aaf23042SYinghai Lu 			aper_size = (32 * 1024 * 1024) << aper_order;
31355c0d721SYinghai Lu 			aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
314aaf23042SYinghai Lu 			aper_base <<= 25;
315aaf23042SYinghai Lu 
316fa5b8a30SPavel Machek 			if (last_valid) {
317fa5b8a30SPavel Machek 				if ((aper_order != last_aper_order) ||
318fa5b8a30SPavel Machek 				    (aper_base != last_aper_base) ||
319fa5b8a30SPavel Machek 				    (aper_enabled != last_aper_enabled)) {
320aaf23042SYinghai Lu 					fix = 1;
321fa5b8a30SPavel Machek 					break;
322aaf23042SYinghai Lu 				}
32355c0d721SYinghai Lu 			}
324aaf23042SYinghai Lu 
325fa5b8a30SPavel Machek 			last_aper_order = aper_order;
326fa5b8a30SPavel Machek 			last_aper_base = aper_base;
327fa5b8a30SPavel Machek 			last_aper_enabled = aper_enabled;
328fa5b8a30SPavel Machek 			last_valid = 1;
329fa5b8a30SPavel Machek 		}
330fa5b8a30SPavel Machek 	}
331fa5b8a30SPavel Machek 
332aaf23042SYinghai Lu 	if (!fix && !aper_enabled)
333aaf23042SYinghai Lu 		return;
334aaf23042SYinghai Lu 
335aaf23042SYinghai Lu 	if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
336aaf23042SYinghai Lu 		fix = 1;
337aaf23042SYinghai Lu 
338aaf23042SYinghai Lu 	if (gart_fix_e820 && !fix && aper_enabled) {
3390754557dSYinghai Lu 		if (e820_any_mapped(aper_base, aper_base + aper_size,
3400754557dSYinghai Lu 				    E820_RAM)) {
3410abbc78aSPavel Machek 			/* reserve it, so we can reuse it in second kernel */
342aaf23042SYinghai Lu 			printk(KERN_INFO "update e820 for GART\n");
343d0be6bdeSYinghai Lu 			e820_add_region(aper_base, aper_size, E820_RESERVED);
344aaf23042SYinghai Lu 			update_e820();
345aaf23042SYinghai Lu 		}
346aaf23042SYinghai Lu 	}
347aaf23042SYinghai Lu 
348f3eee542SYinghai Lu 	if (valid_agp)
3494f384f8bSPavel Machek 		return;
3504f384f8bSPavel Machek 
351f3eee542SYinghai Lu 	/* disable them all at first */
35255c0d721SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) {
35355c0d721SYinghai Lu 		int bus;
35455c0d721SYinghai Lu 		int dev_base, dev_limit;
35555c0d721SYinghai Lu 
35655c0d721SYinghai Lu 		bus = bus_dev_ranges[i].bus;
35755c0d721SYinghai Lu 		dev_base = bus_dev_ranges[i].dev_base;
35855c0d721SYinghai Lu 		dev_limit = bus_dev_ranges[i].dev_limit;
35955c0d721SYinghai Lu 
36055c0d721SYinghai Lu 		for (slot = dev_base; slot < dev_limit; slot++) {
36155c0d721SYinghai Lu 			if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00)))
362aaf23042SYinghai Lu 				continue;
363aaf23042SYinghai Lu 
36455c0d721SYinghai Lu 			ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
36555c0d721SYinghai Lu 			ctl &= ~AMD64_GARTEN;
36655c0d721SYinghai Lu 			write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
36755c0d721SYinghai Lu 		}
368aaf23042SYinghai Lu 	}
369aaf23042SYinghai Lu 
370aaf23042SYinghai Lu }
371aaf23042SYinghai Lu 
3728c9fd91aSYinghai Lu static int __initdata printed_gart_size_msg;
3738c9fd91aSYinghai Lu 
374*480125baSKonrad Rzeszutek Wilk int __init gart_iommu_hole_init(void)
375250c2277SThomas Gleixner {
3768c9fd91aSYinghai Lu 	u32 agp_aper_base = 0, agp_aper_order = 0;
377250c2277SThomas Gleixner 	u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0;
378250c2277SThomas Gleixner 	u64 aper_base, last_aper_base = 0;
37955c0d721SYinghai Lu 	int fix, slot, valid_agp = 0;
38055c0d721SYinghai Lu 	int i, node;
381250c2277SThomas Gleixner 
3820440d4c0SJoerg Roedel 	if (gart_iommu_aperture_disabled || !fix_aperture ||
3830440d4c0SJoerg Roedel 	    !early_pci_allowed())
384*480125baSKonrad Rzeszutek Wilk 		return -ENODEV;
385250c2277SThomas Gleixner 
386250c2277SThomas Gleixner 	printk(KERN_INFO  "Checking aperture...\n");
387250c2277SThomas Gleixner 
3888c9fd91aSYinghai Lu 	if (!fallback_aper_force)
3898c9fd91aSYinghai Lu 		agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp);
3908c9fd91aSYinghai Lu 
391250c2277SThomas Gleixner 	fix = 0;
39247db4c3eSYinghai Lu 	node = 0;
39355c0d721SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) {
39455c0d721SYinghai Lu 		int bus;
39555c0d721SYinghai Lu 		int dev_base, dev_limit;
3964b83873dSJoerg Roedel 		u32 ctl;
39755c0d721SYinghai Lu 
39855c0d721SYinghai Lu 		bus = bus_dev_ranges[i].bus;
39955c0d721SYinghai Lu 		dev_base = bus_dev_ranges[i].dev_base;
40055c0d721SYinghai Lu 		dev_limit = bus_dev_ranges[i].dev_limit;
40155c0d721SYinghai Lu 
40255c0d721SYinghai Lu 		for (slot = dev_base; slot < dev_limit; slot++) {
40355c0d721SYinghai Lu 			if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00)))
404250c2277SThomas Gleixner 				continue;
405250c2277SThomas Gleixner 
406250c2277SThomas Gleixner 			iommu_detected = 1;
4070440d4c0SJoerg Roedel 			gart_iommu_aperture = 1;
408de957628SFUJITA Tomonori 			x86_init.iommu.iommu_init = gart_iommu_init;
409250c2277SThomas Gleixner 
4104b83873dSJoerg Roedel 			ctl = read_pci_config(bus, slot, 3,
4114b83873dSJoerg Roedel 					      AMD64_GARTAPERTURECTL);
4124b83873dSJoerg Roedel 
4134b83873dSJoerg Roedel 			/*
4144b83873dSJoerg Roedel 			 * Before we do anything else disable the GART. It may
4154b83873dSJoerg Roedel 			 * still be enabled if we boot into a crash-kernel here.
4164b83873dSJoerg Roedel 			 * Reconfiguring the GART while it is enabled could have
4174b83873dSJoerg Roedel 			 * unknown side-effects.
4184b83873dSJoerg Roedel 			 */
4194b83873dSJoerg Roedel 			ctl &= ~GARTEN;
4204b83873dSJoerg Roedel 			write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
4214b83873dSJoerg Roedel 
4224b83873dSJoerg Roedel 			aper_order = (ctl >> 1) & 7;
423250c2277SThomas Gleixner 			aper_size = (32 * 1024 * 1024) << aper_order;
42455c0d721SYinghai Lu 			aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
425250c2277SThomas Gleixner 			aper_base <<= 25;
426250c2277SThomas Gleixner 
42747db4c3eSYinghai Lu 			printk(KERN_INFO "Node %d: aperture @ %Lx size %u MB\n",
42847db4c3eSYinghai Lu 					node, aper_base, aper_size >> 20);
42947db4c3eSYinghai Lu 			node++;
430250c2277SThomas Gleixner 
4318c9fd91aSYinghai Lu 			if (!aperture_valid(aper_base, aper_size, 64<<20)) {
4328c9fd91aSYinghai Lu 				if (valid_agp && agp_aper_base &&
4338c9fd91aSYinghai Lu 				    agp_aper_base == aper_base &&
4348c9fd91aSYinghai Lu 				    agp_aper_order == aper_order) {
4358c9fd91aSYinghai Lu 					/* the same between two setting from NB and agp */
436c987d12fSYinghai Lu 					if (!no_iommu &&
437c987d12fSYinghai Lu 					    max_pfn > MAX_DMA32_PFN &&
438c987d12fSYinghai Lu 					    !printed_gart_size_msg) {
4398c9fd91aSYinghai Lu 						printk(KERN_ERR "you are using iommu with agp, but GART size is less than 64M\n");
4408c9fd91aSYinghai Lu 						printk(KERN_ERR "please increase GART size in your BIOS setup\n");
4418c9fd91aSYinghai Lu 						printk(KERN_ERR "if BIOS doesn't have that option, contact your HW vendor!\n");
4428c9fd91aSYinghai Lu 						printed_gart_size_msg = 1;
4438c9fd91aSYinghai Lu 					}
4448c9fd91aSYinghai Lu 				} else {
445250c2277SThomas Gleixner 					fix = 1;
44655c0d721SYinghai Lu 					goto out;
447250c2277SThomas Gleixner 				}
4488c9fd91aSYinghai Lu 			}
449250c2277SThomas Gleixner 
450250c2277SThomas Gleixner 			if ((last_aper_order && aper_order != last_aper_order) ||
451250c2277SThomas Gleixner 			    (last_aper_base && aper_base != last_aper_base)) {
452250c2277SThomas Gleixner 				fix = 1;
45355c0d721SYinghai Lu 				goto out;
454250c2277SThomas Gleixner 			}
455250c2277SThomas Gleixner 			last_aper_order = aper_order;
456250c2277SThomas Gleixner 			last_aper_base = aper_base;
457250c2277SThomas Gleixner 		}
45855c0d721SYinghai Lu 	}
459250c2277SThomas Gleixner 
46055c0d721SYinghai Lu out:
461250c2277SThomas Gleixner 	if (!fix && !fallback_aper_force) {
462250c2277SThomas Gleixner 		if (last_aper_base) {
463250c2277SThomas Gleixner 			unsigned long n = (32 * 1024 * 1024) << last_aper_order;
464c140df97SIngo Molnar 
465250c2277SThomas Gleixner 			insert_aperture_resource((u32)last_aper_base, n);
466*480125baSKonrad Rzeszutek Wilk 			return 1;
467250c2277SThomas Gleixner 		}
468*480125baSKonrad Rzeszutek Wilk 		return 0;
469250c2277SThomas Gleixner 	}
470250c2277SThomas Gleixner 
4718c9fd91aSYinghai Lu 	if (!fallback_aper_force) {
4728c9fd91aSYinghai Lu 		aper_alloc = agp_aper_base;
4738c9fd91aSYinghai Lu 		aper_order = agp_aper_order;
4748c9fd91aSYinghai Lu 	}
475250c2277SThomas Gleixner 
476250c2277SThomas Gleixner 	if (aper_alloc) {
477250c2277SThomas Gleixner 		/* Got the aperture from the AGP bridge */
478c987d12fSYinghai Lu 	} else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) ||
479250c2277SThomas Gleixner 		   force_iommu ||
480250c2277SThomas Gleixner 		   valid_agp ||
481250c2277SThomas Gleixner 		   fallback_aper_force) {
4829b156845SAdam Jackson 		printk(KERN_INFO
48331183ba8SIngo Molnar 			"Your BIOS doesn't leave a aperture memory hole\n");
4849b156845SAdam Jackson 		printk(KERN_INFO
48531183ba8SIngo Molnar 			"Please enable the IOMMU option in the BIOS setup\n");
4869b156845SAdam Jackson 		printk(KERN_INFO
48731183ba8SIngo Molnar 			"This costs you %d MB of RAM\n",
488250c2277SThomas Gleixner 				32 << fallback_aper_order);
489250c2277SThomas Gleixner 
490250c2277SThomas Gleixner 		aper_order = fallback_aper_order;
491250c2277SThomas Gleixner 		aper_alloc = allocate_aperture();
492250c2277SThomas Gleixner 		if (!aper_alloc) {
493c140df97SIngo Molnar 			/*
494c140df97SIngo Molnar 			 * Could disable AGP and IOMMU here, but it's
495c140df97SIngo Molnar 			 * probably not worth it. But the later users
496c140df97SIngo Molnar 			 * cannot deal with bad apertures and turning
497c140df97SIngo Molnar 			 * on the aperture over memory causes very
498c140df97SIngo Molnar 			 * strange problems, so it's better to panic
499c140df97SIngo Molnar 			 * early.
500c140df97SIngo Molnar 			 */
501250c2277SThomas Gleixner 			panic("Not enough memory for aperture");
502250c2277SThomas Gleixner 		}
503250c2277SThomas Gleixner 	} else {
504*480125baSKonrad Rzeszutek Wilk 		return 0;
505250c2277SThomas Gleixner 	}
506250c2277SThomas Gleixner 
507250c2277SThomas Gleixner 	/* Fix up the north bridges */
50855c0d721SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) {
50955c0d721SYinghai Lu 		int bus;
51055c0d721SYinghai Lu 		int dev_base, dev_limit;
51155c0d721SYinghai Lu 
51255c0d721SYinghai Lu 		bus = bus_dev_ranges[i].bus;
51355c0d721SYinghai Lu 		dev_base = bus_dev_ranges[i].dev_base;
51455c0d721SYinghai Lu 		dev_limit = bus_dev_ranges[i].dev_limit;
51555c0d721SYinghai Lu 		for (slot = dev_base; slot < dev_limit; slot++) {
51655c0d721SYinghai Lu 			if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00)))
517250c2277SThomas Gleixner 				continue;
518250c2277SThomas Gleixner 
51955c0d721SYinghai Lu 			/* Don't enable translation yet. That is done later.
52055c0d721SYinghai Lu 			   Assume this BIOS didn't initialise the GART so
52155c0d721SYinghai Lu 			   just overwrite all previous bits */
52255c0d721SYinghai Lu 			write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, aper_order << 1);
52355c0d721SYinghai Lu 			write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25);
52455c0d721SYinghai Lu 		}
525250c2277SThomas Gleixner 	}
5266703f6d1SRafael J. Wysocki 
5276703f6d1SRafael J. Wysocki 	set_up_gart_resume(aper_order, aper_alloc);
528*480125baSKonrad Rzeszutek Wilk 
529*480125baSKonrad Rzeszutek Wilk 	return 1;
530250c2277SThomas Gleixner }
531