1250c2277SThomas Gleixner /* 2250c2277SThomas Gleixner * Firmware replacement code. 3250c2277SThomas Gleixner * 4250c2277SThomas Gleixner * Work around broken BIOSes that don't set an aperture or only set the 5250c2277SThomas Gleixner * aperture in the AGP bridge. 6250c2277SThomas Gleixner * If all fails map the aperture over some low memory. This is cheaper than 7250c2277SThomas Gleixner * doing bounce buffering. The memory is lost. This is done at early boot 8250c2277SThomas Gleixner * because only the bootmem allocator can allocate 32+MB. 9250c2277SThomas Gleixner * 10250c2277SThomas Gleixner * Copyright 2002 Andi Kleen, SuSE Labs. 11250c2277SThomas Gleixner */ 12250c2277SThomas Gleixner #include <linux/kernel.h> 13250c2277SThomas Gleixner #include <linux/types.h> 14250c2277SThomas Gleixner #include <linux/init.h> 15250c2277SThomas Gleixner #include <linux/bootmem.h> 16250c2277SThomas Gleixner #include <linux/mmzone.h> 17250c2277SThomas Gleixner #include <linux/pci_ids.h> 18250c2277SThomas Gleixner #include <linux/pci.h> 19250c2277SThomas Gleixner #include <linux/bitops.h> 20250c2277SThomas Gleixner #include <linux/ioport.h> 212050d45dSPavel Machek #include <linux/suspend.h> 22250c2277SThomas Gleixner #include <asm/e820.h> 23250c2277SThomas Gleixner #include <asm/io.h> 24395624fcSJoerg Roedel #include <asm/gart.h> 25250c2277SThomas Gleixner #include <asm/pci-direct.h> 26250c2277SThomas Gleixner #include <asm/dma.h> 27250c2277SThomas Gleixner #include <asm/k8.h> 28250c2277SThomas Gleixner 290440d4c0SJoerg Roedel int gart_iommu_aperture; 307de6a4cdSPavel Machek int gart_iommu_aperture_disabled __initdata; 317de6a4cdSPavel Machek int gart_iommu_aperture_allowed __initdata; 32250c2277SThomas Gleixner 33250c2277SThomas Gleixner int fallback_aper_order __initdata = 1; /* 64MB */ 347de6a4cdSPavel Machek int fallback_aper_force __initdata; 35250c2277SThomas Gleixner 36250c2277SThomas Gleixner int fix_aperture __initdata = 1; 37250c2277SThomas Gleixner 3855c0d721SYinghai Lu struct bus_dev_range { 3955c0d721SYinghai Lu int bus; 4055c0d721SYinghai Lu int dev_base; 4155c0d721SYinghai Lu int dev_limit; 4255c0d721SYinghai Lu }; 4355c0d721SYinghai Lu 4455c0d721SYinghai Lu static struct bus_dev_range bus_dev_ranges[] __initdata = { 4555c0d721SYinghai Lu { 0x00, 0x18, 0x20}, 4655c0d721SYinghai Lu { 0xff, 0x00, 0x20}, 4755c0d721SYinghai Lu { 0xfe, 0x00, 0x20} 4855c0d721SYinghai Lu }; 4955c0d721SYinghai Lu 50250c2277SThomas Gleixner static struct resource gart_resource = { 51250c2277SThomas Gleixner .name = "GART", 52250c2277SThomas Gleixner .flags = IORESOURCE_MEM, 53250c2277SThomas Gleixner }; 54250c2277SThomas Gleixner 55250c2277SThomas Gleixner static void __init insert_aperture_resource(u32 aper_base, u32 aper_size) 56250c2277SThomas Gleixner { 57250c2277SThomas Gleixner gart_resource.start = aper_base; 58250c2277SThomas Gleixner gart_resource.end = aper_base + aper_size - 1; 59250c2277SThomas Gleixner insert_resource(&iomem_resource, &gart_resource); 60250c2277SThomas Gleixner } 61250c2277SThomas Gleixner 62250c2277SThomas Gleixner /* This code runs before the PCI subsystem is initialized, so just 63250c2277SThomas Gleixner access the northbridge directly. */ 64250c2277SThomas Gleixner 65250c2277SThomas Gleixner static u32 __init allocate_aperture(void) 66250c2277SThomas Gleixner { 67250c2277SThomas Gleixner u32 aper_size; 68250c2277SThomas Gleixner void *p; 69250c2277SThomas Gleixner 707677b2efSYinghai Lu /* aper_size should <= 1G */ 717677b2efSYinghai Lu if (fallback_aper_order > 5) 727677b2efSYinghai Lu fallback_aper_order = 5; 73250c2277SThomas Gleixner aper_size = (32 * 1024 * 1024) << fallback_aper_order; 74250c2277SThomas Gleixner 75250c2277SThomas Gleixner /* 76c140df97SIngo Molnar * Aperture has to be naturally aligned. This means a 2GB aperture 77c140df97SIngo Molnar * won't have much chance of finding a place in the lower 4GB of 78c140df97SIngo Molnar * memory. Unfortunately we cannot move it up because that would 79c140df97SIngo Molnar * make the IOMMU useless. 80250c2277SThomas Gleixner */ 817677b2efSYinghai Lu /* 827677b2efSYinghai Lu * using 512M as goal, in case kexec will load kernel_big 837677b2efSYinghai Lu * that will do the on position decompress, and could overlap with 847677b2efSYinghai Lu * that positon with gart that is used. 857677b2efSYinghai Lu * sequende: 867677b2efSYinghai Lu * kernel_small 877677b2efSYinghai Lu * ==> kexec (with kdump trigger path or previous doesn't shutdown gart) 887677b2efSYinghai Lu * ==> kernel_small(gart area become e820_reserved) 897677b2efSYinghai Lu * ==> kexec (with kdump trigger path or previous doesn't shutdown gart) 907677b2efSYinghai Lu * ==> kerne_big (uncompressed size will be big than 64M or 128M) 917677b2efSYinghai Lu * so don't use 512M below as gart iommu, leave the space for kernel 927677b2efSYinghai Lu * code for safe 937677b2efSYinghai Lu */ 947677b2efSYinghai Lu p = __alloc_bootmem_nopanic(aper_size, aper_size, 512ULL<<20); 95250c2277SThomas Gleixner if (!p || __pa(p)+aper_size > 0xffffffff) { 9631183ba8SIngo Molnar printk(KERN_ERR 9731183ba8SIngo Molnar "Cannot allocate aperture memory hole (%p,%uK)\n", 98250c2277SThomas Gleixner p, aper_size>>10); 99250c2277SThomas Gleixner if (p) 100250c2277SThomas Gleixner free_bootmem(__pa(p), aper_size); 101250c2277SThomas Gleixner return 0; 102250c2277SThomas Gleixner } 10331183ba8SIngo Molnar printk(KERN_INFO "Mapping aperture over %d KB of RAM @ %lx\n", 104250c2277SThomas Gleixner aper_size >> 10, __pa(p)); 105250c2277SThomas Gleixner insert_aperture_resource((u32)__pa(p), aper_size); 1062050d45dSPavel Machek register_nosave_region((u32)__pa(p) >> PAGE_SHIFT, 1072050d45dSPavel Machek (u32)__pa(p+aper_size) >> PAGE_SHIFT); 108c140df97SIngo Molnar 109250c2277SThomas Gleixner return (u32)__pa(p); 110250c2277SThomas Gleixner } 111250c2277SThomas Gleixner 112250c2277SThomas Gleixner 113250c2277SThomas Gleixner /* Find a PCI capability */ 114*dd564d0cSPavel Machek static u32 __init find_cap(int bus, int slot, int func, int cap) 115250c2277SThomas Gleixner { 116250c2277SThomas Gleixner int bytes; 117c140df97SIngo Molnar u8 pos; 118c140df97SIngo Molnar 11955c0d721SYinghai Lu if (!(read_pci_config_16(bus, slot, func, PCI_STATUS) & 120c140df97SIngo Molnar PCI_STATUS_CAP_LIST)) 121250c2277SThomas Gleixner return 0; 122c140df97SIngo Molnar 12355c0d721SYinghai Lu pos = read_pci_config_byte(bus, slot, func, PCI_CAPABILITY_LIST); 124250c2277SThomas Gleixner for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) { 125250c2277SThomas Gleixner u8 id; 126c140df97SIngo Molnar 127250c2277SThomas Gleixner pos &= ~3; 12855c0d721SYinghai Lu id = read_pci_config_byte(bus, slot, func, pos+PCI_CAP_LIST_ID); 129250c2277SThomas Gleixner if (id == 0xff) 130250c2277SThomas Gleixner break; 131250c2277SThomas Gleixner if (id == cap) 132250c2277SThomas Gleixner return pos; 13355c0d721SYinghai Lu pos = read_pci_config_byte(bus, slot, func, 134c140df97SIngo Molnar pos+PCI_CAP_LIST_NEXT); 135250c2277SThomas Gleixner } 136250c2277SThomas Gleixner return 0; 137250c2277SThomas Gleixner } 138250c2277SThomas Gleixner 139250c2277SThomas Gleixner /* Read a standard AGPv3 bridge header */ 140*dd564d0cSPavel Machek static u32 __init read_agp(int bus, int slot, int func, int cap, u32 *order) 141250c2277SThomas Gleixner { 142250c2277SThomas Gleixner u32 apsize; 143250c2277SThomas Gleixner u32 apsizereg; 144250c2277SThomas Gleixner int nbits; 145250c2277SThomas Gleixner u32 aper_low, aper_hi; 146250c2277SThomas Gleixner u64 aper; 1471edc1ab3SYinghai Lu u32 old_order; 148250c2277SThomas Gleixner 14955c0d721SYinghai Lu printk(KERN_INFO "AGP bridge at %02x:%02x:%02x\n", bus, slot, func); 15055c0d721SYinghai Lu apsizereg = read_pci_config_16(bus, slot, func, cap + 0x14); 151250c2277SThomas Gleixner if (apsizereg == 0xffffffff) { 15231183ba8SIngo Molnar printk(KERN_ERR "APSIZE in AGP bridge unreadable\n"); 153250c2277SThomas Gleixner return 0; 154250c2277SThomas Gleixner } 155250c2277SThomas Gleixner 1561edc1ab3SYinghai Lu /* old_order could be the value from NB gart setting */ 1571edc1ab3SYinghai Lu old_order = *order; 1581edc1ab3SYinghai Lu 159250c2277SThomas Gleixner apsize = apsizereg & 0xfff; 160250c2277SThomas Gleixner /* Some BIOS use weird encodings not in the AGPv3 table. */ 161250c2277SThomas Gleixner if (apsize & 0xff) 162250c2277SThomas Gleixner apsize |= 0xf00; 163250c2277SThomas Gleixner nbits = hweight16(apsize); 164250c2277SThomas Gleixner *order = 7 - nbits; 165250c2277SThomas Gleixner if ((int)*order < 0) /* < 32MB */ 166250c2277SThomas Gleixner *order = 0; 167250c2277SThomas Gleixner 16855c0d721SYinghai Lu aper_low = read_pci_config(bus, slot, func, 0x10); 16955c0d721SYinghai Lu aper_hi = read_pci_config(bus, slot, func, 0x14); 170250c2277SThomas Gleixner aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32); 171250c2277SThomas Gleixner 1721edc1ab3SYinghai Lu /* 1731edc1ab3SYinghai Lu * On some sick chips, APSIZE is 0. It means it wants 4G 1741edc1ab3SYinghai Lu * so let double check that order, and lets trust AMD NB settings: 1751edc1ab3SYinghai Lu */ 1768c9fd91aSYinghai Lu printk(KERN_INFO "Aperture from AGP @ %Lx old size %u MB\n", 1778c9fd91aSYinghai Lu aper, 32 << old_order); 1788c9fd91aSYinghai Lu if (aper + (32ULL<<(20 + *order)) > 0x100000000ULL) { 1791edc1ab3SYinghai Lu printk(KERN_INFO "Aperture size %u MB (APSIZE %x) is not right, using settings from NB\n", 1801edc1ab3SYinghai Lu 32 << *order, apsizereg); 1811edc1ab3SYinghai Lu *order = old_order; 1821edc1ab3SYinghai Lu } 1831edc1ab3SYinghai Lu 18431183ba8SIngo Molnar printk(KERN_INFO "Aperture from AGP @ %Lx size %u MB (APSIZE %x)\n", 185250c2277SThomas Gleixner aper, 32 << *order, apsizereg); 186250c2277SThomas Gleixner 1878c9fd91aSYinghai Lu if (!aperture_valid(aper, (32*1024*1024) << *order, 32<<20)) 188250c2277SThomas Gleixner return 0; 189250c2277SThomas Gleixner return (u32)aper; 190250c2277SThomas Gleixner } 191250c2277SThomas Gleixner 192c140df97SIngo Molnar /* 193c140df97SIngo Molnar * Look for an AGP bridge. Windows only expects the aperture in the 194c140df97SIngo Molnar * AGP bridge and some BIOS forget to initialize the Northbridge too. 195c140df97SIngo Molnar * Work around this here. 196c140df97SIngo Molnar * 197c140df97SIngo Molnar * Do an PCI bus scan by hand because we're running before the PCI 198c140df97SIngo Molnar * subsystem. 199c140df97SIngo Molnar * 200c140df97SIngo Molnar * All K8 AGP bridges are AGPv3 compliant, so we can do this scan 201c140df97SIngo Molnar * generically. It's probably overkill to always scan all slots because 202c140df97SIngo Molnar * the AGP bridges should be always an own bus on the HT hierarchy, 203c140df97SIngo Molnar * but do it here for future safety. 204c140df97SIngo Molnar */ 205*dd564d0cSPavel Machek static u32 __init search_agp_bridge(u32 *order, int *valid_agp) 206250c2277SThomas Gleixner { 20755c0d721SYinghai Lu int bus, slot, func; 208250c2277SThomas Gleixner 209250c2277SThomas Gleixner /* Poor man's PCI discovery */ 21055c0d721SYinghai Lu for (bus = 0; bus < 256; bus++) { 211250c2277SThomas Gleixner for (slot = 0; slot < 32; slot++) { 212250c2277SThomas Gleixner for (func = 0; func < 8; func++) { 213250c2277SThomas Gleixner u32 class, cap; 214250c2277SThomas Gleixner u8 type; 21555c0d721SYinghai Lu class = read_pci_config(bus, slot, func, 216250c2277SThomas Gleixner PCI_CLASS_REVISION); 217250c2277SThomas Gleixner if (class == 0xffffffff) 218250c2277SThomas Gleixner break; 219250c2277SThomas Gleixner 220250c2277SThomas Gleixner switch (class >> 16) { 221250c2277SThomas Gleixner case PCI_CLASS_BRIDGE_HOST: 222250c2277SThomas Gleixner case PCI_CLASS_BRIDGE_OTHER: /* needed? */ 223250c2277SThomas Gleixner /* AGP bridge? */ 22455c0d721SYinghai Lu cap = find_cap(bus, slot, func, 225c140df97SIngo Molnar PCI_CAP_ID_AGP); 226250c2277SThomas Gleixner if (!cap) 227250c2277SThomas Gleixner break; 228250c2277SThomas Gleixner *valid_agp = 1; 22955c0d721SYinghai Lu return read_agp(bus, slot, func, cap, 230c140df97SIngo Molnar order); 231250c2277SThomas Gleixner } 232250c2277SThomas Gleixner 233250c2277SThomas Gleixner /* No multi-function device? */ 23455c0d721SYinghai Lu type = read_pci_config_byte(bus, slot, func, 235250c2277SThomas Gleixner PCI_HEADER_TYPE); 236250c2277SThomas Gleixner if (!(type & 0x80)) 237250c2277SThomas Gleixner break; 238250c2277SThomas Gleixner } 239250c2277SThomas Gleixner } 240250c2277SThomas Gleixner } 24131183ba8SIngo Molnar printk(KERN_INFO "No AGP bridge found\n"); 242c140df97SIngo Molnar 243250c2277SThomas Gleixner return 0; 244250c2277SThomas Gleixner } 245250c2277SThomas Gleixner 246aaf23042SYinghai Lu static int gart_fix_e820 __initdata = 1; 247aaf23042SYinghai Lu 248aaf23042SYinghai Lu static int __init parse_gart_mem(char *p) 249aaf23042SYinghai Lu { 250aaf23042SYinghai Lu if (!p) 251aaf23042SYinghai Lu return -EINVAL; 252aaf23042SYinghai Lu 253aaf23042SYinghai Lu if (!strncmp(p, "off", 3)) 254aaf23042SYinghai Lu gart_fix_e820 = 0; 255aaf23042SYinghai Lu else if (!strncmp(p, "on", 2)) 256aaf23042SYinghai Lu gart_fix_e820 = 1; 257aaf23042SYinghai Lu 258aaf23042SYinghai Lu return 0; 259aaf23042SYinghai Lu } 260aaf23042SYinghai Lu early_param("gart_fix_e820", parse_gart_mem); 261aaf23042SYinghai Lu 262aaf23042SYinghai Lu void __init early_gart_iommu_check(void) 263aaf23042SYinghai Lu { 264aaf23042SYinghai Lu /* 265aaf23042SYinghai Lu * in case it is enabled before, esp for kexec/kdump, 266aaf23042SYinghai Lu * previous kernel already enable that. memset called 267aaf23042SYinghai Lu * by allocate_aperture/__alloc_bootmem_nopanic cause restart. 268aaf23042SYinghai Lu * or second kernel have different position for GART hole. and new 269aaf23042SYinghai Lu * kernel could use hole as RAM that is still used by GART set by 270aaf23042SYinghai Lu * first kernel 271aaf23042SYinghai Lu * or BIOS forget to put that in reserved. 272aaf23042SYinghai Lu * try to update e820 to make that region as reserved. 273aaf23042SYinghai Lu */ 27455c0d721SYinghai Lu int fix, slot; 275aaf23042SYinghai Lu u32 ctl; 276aaf23042SYinghai Lu u32 aper_size = 0, aper_order = 0, last_aper_order = 0; 277aaf23042SYinghai Lu u64 aper_base = 0, last_aper_base = 0; 278aaf23042SYinghai Lu int aper_enabled = 0, last_aper_enabled = 0; 27955c0d721SYinghai Lu int i; 280aaf23042SYinghai Lu 281aaf23042SYinghai Lu if (!early_pci_allowed()) 282aaf23042SYinghai Lu return; 283aaf23042SYinghai Lu 284aaf23042SYinghai Lu fix = 0; 28555c0d721SYinghai Lu for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) { 28655c0d721SYinghai Lu int bus; 28755c0d721SYinghai Lu int dev_base, dev_limit; 28855c0d721SYinghai Lu 28955c0d721SYinghai Lu bus = bus_dev_ranges[i].bus; 29055c0d721SYinghai Lu dev_base = bus_dev_ranges[i].dev_base; 29155c0d721SYinghai Lu dev_limit = bus_dev_ranges[i].dev_limit; 29255c0d721SYinghai Lu 29355c0d721SYinghai Lu for (slot = dev_base; slot < dev_limit; slot++) { 29455c0d721SYinghai Lu if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00))) 295aaf23042SYinghai Lu continue; 296aaf23042SYinghai Lu 29755c0d721SYinghai Lu ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL); 29855c0d721SYinghai Lu aper_enabled = ctl & AMD64_GARTEN; 299aaf23042SYinghai Lu aper_order = (ctl >> 1) & 7; 300aaf23042SYinghai Lu aper_size = (32 * 1024 * 1024) << aper_order; 30155c0d721SYinghai Lu aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff; 302aaf23042SYinghai Lu aper_base <<= 25; 303aaf23042SYinghai Lu 304aaf23042SYinghai Lu if ((last_aper_order && aper_order != last_aper_order) || 305aaf23042SYinghai Lu (last_aper_base && aper_base != last_aper_base) || 306aaf23042SYinghai Lu (last_aper_enabled && aper_enabled != last_aper_enabled)) { 307aaf23042SYinghai Lu fix = 1; 30855c0d721SYinghai Lu goto out; 309aaf23042SYinghai Lu } 310aaf23042SYinghai Lu last_aper_order = aper_order; 311aaf23042SYinghai Lu last_aper_base = aper_base; 312aaf23042SYinghai Lu last_aper_enabled = aper_enabled; 313aaf23042SYinghai Lu } 31455c0d721SYinghai Lu } 315aaf23042SYinghai Lu 31655c0d721SYinghai Lu out: 317aaf23042SYinghai Lu if (!fix && !aper_enabled) 318aaf23042SYinghai Lu return; 319aaf23042SYinghai Lu 320aaf23042SYinghai Lu if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL) 321aaf23042SYinghai Lu fix = 1; 322aaf23042SYinghai Lu 323aaf23042SYinghai Lu if (gart_fix_e820 && !fix && aper_enabled) { 3248c9fd91aSYinghai Lu if (!e820_all_mapped(aper_base, aper_base + aper_size, 3258c9fd91aSYinghai Lu E820_RESERVED)) { 3260abbc78aSPavel Machek /* reserve it, so we can reuse it in second kernel */ 327aaf23042SYinghai Lu printk(KERN_INFO "update e820 for GART\n"); 328aaf23042SYinghai Lu add_memory_region(aper_base, aper_size, E820_RESERVED); 329aaf23042SYinghai Lu update_e820(); 330aaf23042SYinghai Lu } 331aaf23042SYinghai Lu return; 332aaf23042SYinghai Lu } 333aaf23042SYinghai Lu 334aaf23042SYinghai Lu /* different nodes have different setting, disable them all at first*/ 33555c0d721SYinghai Lu for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) { 33655c0d721SYinghai Lu int bus; 33755c0d721SYinghai Lu int dev_base, dev_limit; 33855c0d721SYinghai Lu 33955c0d721SYinghai Lu bus = bus_dev_ranges[i].bus; 34055c0d721SYinghai Lu dev_base = bus_dev_ranges[i].dev_base; 34155c0d721SYinghai Lu dev_limit = bus_dev_ranges[i].dev_limit; 34255c0d721SYinghai Lu 34355c0d721SYinghai Lu for (slot = dev_base; slot < dev_limit; slot++) { 34455c0d721SYinghai Lu if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00))) 345aaf23042SYinghai Lu continue; 346aaf23042SYinghai Lu 34755c0d721SYinghai Lu ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL); 34855c0d721SYinghai Lu ctl &= ~AMD64_GARTEN; 34955c0d721SYinghai Lu write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl); 35055c0d721SYinghai Lu } 351aaf23042SYinghai Lu } 352aaf23042SYinghai Lu 353aaf23042SYinghai Lu } 354aaf23042SYinghai Lu 3558c9fd91aSYinghai Lu static int __initdata printed_gart_size_msg; 3568c9fd91aSYinghai Lu 3570440d4c0SJoerg Roedel void __init gart_iommu_hole_init(void) 358250c2277SThomas Gleixner { 3598c9fd91aSYinghai Lu u32 agp_aper_base = 0, agp_aper_order = 0; 360250c2277SThomas Gleixner u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0; 361250c2277SThomas Gleixner u64 aper_base, last_aper_base = 0; 36255c0d721SYinghai Lu int fix, slot, valid_agp = 0; 36355c0d721SYinghai Lu int i, node; 364250c2277SThomas Gleixner 3650440d4c0SJoerg Roedel if (gart_iommu_aperture_disabled || !fix_aperture || 3660440d4c0SJoerg Roedel !early_pci_allowed()) 367250c2277SThomas Gleixner return; 368250c2277SThomas Gleixner 369250c2277SThomas Gleixner printk(KERN_INFO "Checking aperture...\n"); 370250c2277SThomas Gleixner 3718c9fd91aSYinghai Lu if (!fallback_aper_force) 3728c9fd91aSYinghai Lu agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp); 3738c9fd91aSYinghai Lu 374250c2277SThomas Gleixner fix = 0; 37547db4c3eSYinghai Lu node = 0; 37655c0d721SYinghai Lu for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) { 37755c0d721SYinghai Lu int bus; 37855c0d721SYinghai Lu int dev_base, dev_limit; 37955c0d721SYinghai Lu 38055c0d721SYinghai Lu bus = bus_dev_ranges[i].bus; 38155c0d721SYinghai Lu dev_base = bus_dev_ranges[i].dev_base; 38255c0d721SYinghai Lu dev_limit = bus_dev_ranges[i].dev_limit; 38355c0d721SYinghai Lu 38455c0d721SYinghai Lu for (slot = dev_base; slot < dev_limit; slot++) { 38555c0d721SYinghai Lu if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00))) 386250c2277SThomas Gleixner continue; 387250c2277SThomas Gleixner 388250c2277SThomas Gleixner iommu_detected = 1; 3890440d4c0SJoerg Roedel gart_iommu_aperture = 1; 390250c2277SThomas Gleixner 39155c0d721SYinghai Lu aper_order = (read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL) >> 1) & 7; 392250c2277SThomas Gleixner aper_size = (32 * 1024 * 1024) << aper_order; 39355c0d721SYinghai Lu aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff; 394250c2277SThomas Gleixner aper_base <<= 25; 395250c2277SThomas Gleixner 39647db4c3eSYinghai Lu printk(KERN_INFO "Node %d: aperture @ %Lx size %u MB\n", 39747db4c3eSYinghai Lu node, aper_base, aper_size >> 20); 39847db4c3eSYinghai Lu node++; 399250c2277SThomas Gleixner 4008c9fd91aSYinghai Lu if (!aperture_valid(aper_base, aper_size, 64<<20)) { 4018c9fd91aSYinghai Lu if (valid_agp && agp_aper_base && 4028c9fd91aSYinghai Lu agp_aper_base == aper_base && 4038c9fd91aSYinghai Lu agp_aper_order == aper_order) { 4048c9fd91aSYinghai Lu /* the same between two setting from NB and agp */ 4058c9fd91aSYinghai Lu if (!no_iommu && end_pfn > MAX_DMA32_PFN && !printed_gart_size_msg) { 4068c9fd91aSYinghai Lu printk(KERN_ERR "you are using iommu with agp, but GART size is less than 64M\n"); 4078c9fd91aSYinghai Lu printk(KERN_ERR "please increase GART size in your BIOS setup\n"); 4088c9fd91aSYinghai Lu printk(KERN_ERR "if BIOS doesn't have that option, contact your HW vendor!\n"); 4098c9fd91aSYinghai Lu printed_gart_size_msg = 1; 4108c9fd91aSYinghai Lu } 4118c9fd91aSYinghai Lu } else { 412250c2277SThomas Gleixner fix = 1; 41355c0d721SYinghai Lu goto out; 414250c2277SThomas Gleixner } 4158c9fd91aSYinghai Lu } 416250c2277SThomas Gleixner 417250c2277SThomas Gleixner if ((last_aper_order && aper_order != last_aper_order) || 418250c2277SThomas Gleixner (last_aper_base && aper_base != last_aper_base)) { 419250c2277SThomas Gleixner fix = 1; 42055c0d721SYinghai Lu goto out; 421250c2277SThomas Gleixner } 422250c2277SThomas Gleixner last_aper_order = aper_order; 423250c2277SThomas Gleixner last_aper_base = aper_base; 424250c2277SThomas Gleixner } 42555c0d721SYinghai Lu } 426250c2277SThomas Gleixner 42755c0d721SYinghai Lu out: 428250c2277SThomas Gleixner if (!fix && !fallback_aper_force) { 429250c2277SThomas Gleixner if (last_aper_base) { 430250c2277SThomas Gleixner unsigned long n = (32 * 1024 * 1024) << last_aper_order; 431c140df97SIngo Molnar 432250c2277SThomas Gleixner insert_aperture_resource((u32)last_aper_base, n); 433250c2277SThomas Gleixner } 434250c2277SThomas Gleixner return; 435250c2277SThomas Gleixner } 436250c2277SThomas Gleixner 4378c9fd91aSYinghai Lu if (!fallback_aper_force) { 4388c9fd91aSYinghai Lu aper_alloc = agp_aper_base; 4398c9fd91aSYinghai Lu aper_order = agp_aper_order; 4408c9fd91aSYinghai Lu } 441250c2277SThomas Gleixner 442250c2277SThomas Gleixner if (aper_alloc) { 443250c2277SThomas Gleixner /* Got the aperture from the AGP bridge */ 444250c2277SThomas Gleixner } else if (swiotlb && !valid_agp) { 445250c2277SThomas Gleixner /* Do nothing */ 446250c2277SThomas Gleixner } else if ((!no_iommu && end_pfn > MAX_DMA32_PFN) || 447250c2277SThomas Gleixner force_iommu || 448250c2277SThomas Gleixner valid_agp || 449250c2277SThomas Gleixner fallback_aper_force) { 45031183ba8SIngo Molnar printk(KERN_ERR 45131183ba8SIngo Molnar "Your BIOS doesn't leave a aperture memory hole\n"); 45231183ba8SIngo Molnar printk(KERN_ERR 45331183ba8SIngo Molnar "Please enable the IOMMU option in the BIOS setup\n"); 45431183ba8SIngo Molnar printk(KERN_ERR 45531183ba8SIngo Molnar "This costs you %d MB of RAM\n", 456250c2277SThomas Gleixner 32 << fallback_aper_order); 457250c2277SThomas Gleixner 458250c2277SThomas Gleixner aper_order = fallback_aper_order; 459250c2277SThomas Gleixner aper_alloc = allocate_aperture(); 460250c2277SThomas Gleixner if (!aper_alloc) { 461c140df97SIngo Molnar /* 462c140df97SIngo Molnar * Could disable AGP and IOMMU here, but it's 463c140df97SIngo Molnar * probably not worth it. But the later users 464c140df97SIngo Molnar * cannot deal with bad apertures and turning 465c140df97SIngo Molnar * on the aperture over memory causes very 466c140df97SIngo Molnar * strange problems, so it's better to panic 467c140df97SIngo Molnar * early. 468c140df97SIngo Molnar */ 469250c2277SThomas Gleixner panic("Not enough memory for aperture"); 470250c2277SThomas Gleixner } 471250c2277SThomas Gleixner } else { 472250c2277SThomas Gleixner return; 473250c2277SThomas Gleixner } 474250c2277SThomas Gleixner 475250c2277SThomas Gleixner /* Fix up the north bridges */ 47655c0d721SYinghai Lu for (i = 0; i < ARRAY_SIZE(bus_dev_ranges); i++) { 47755c0d721SYinghai Lu int bus; 47855c0d721SYinghai Lu int dev_base, dev_limit; 47955c0d721SYinghai Lu 48055c0d721SYinghai Lu bus = bus_dev_ranges[i].bus; 48155c0d721SYinghai Lu dev_base = bus_dev_ranges[i].dev_base; 48255c0d721SYinghai Lu dev_limit = bus_dev_ranges[i].dev_limit; 48355c0d721SYinghai Lu for (slot = dev_base; slot < dev_limit; slot++) { 48455c0d721SYinghai Lu if (!early_is_k8_nb(read_pci_config(bus, slot, 3, 0x00))) 485250c2277SThomas Gleixner continue; 486250c2277SThomas Gleixner 48755c0d721SYinghai Lu /* Don't enable translation yet. That is done later. 48855c0d721SYinghai Lu Assume this BIOS didn't initialise the GART so 48955c0d721SYinghai Lu just overwrite all previous bits */ 49055c0d721SYinghai Lu write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, aper_order << 1); 49155c0d721SYinghai Lu write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25); 49255c0d721SYinghai Lu } 493250c2277SThomas Gleixner } 494250c2277SThomas Gleixner } 495