Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Firmware replacement code. |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 4 | * |
Pavel Machek | 8caac56 | 2008-11-26 17:15:27 +0100 | [diff] [blame] | 5 | * Work around broken BIOSes that don't set an aperture, only set the |
| 6 | * aperture in the AGP bridge, or set too small aperture. |
| 7 | * |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 8 | * If all fails map the aperture over some low memory. This is cheaper than |
| 9 | * doing bounce buffering. The memory is lost. This is done at early boot |
| 10 | * because only the bootmem allocator can allocate 32+MB. |
| 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Copyright 2002 Andi Kleen, SuSE Labs. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
Bjorn Helgaas | a5d3244 | 2014-04-28 15:16:33 -0600 | [diff] [blame] | 14 | #define pr_fmt(fmt) "AGP: " fmt |
| 15 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/kernel.h> |
Kairui Song | ffc8599 | 2019-03-08 11:05:08 +0800 | [diff] [blame] | 17 | #include <linux/kcore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | #include <linux/init.h> |
Yinghai Lu | 32e3f2b | 2010-12-17 16:58:40 -0800 | [diff] [blame] | 20 | #include <linux/memblock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/mmzone.h> |
| 22 | #include <linux/pci_ids.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/bitops.h> |
Pavel Machek | 2050d45 | 2008-03-13 23:05:41 +0100 | [diff] [blame] | 25 | #include <linux/suspend.h> |
Ingo Molnar | 66441bd | 2017-01-27 10:27:10 +0100 | [diff] [blame] | 26 | #include <asm/e820/api.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/io.h> |
FUJITA Tomonori | 46a7fa2 | 2008-07-11 10:23:42 +0900 | [diff] [blame] | 28 | #include <asm/iommu.h> |
Joerg Roedel | 395624f | 2007-10-24 12:49:47 +0200 | [diff] [blame] | 29 | #include <asm/gart.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <asm/pci-direct.h> |
Andi Kleen | ca8642f | 2006-01-11 22:44:27 +0100 | [diff] [blame] | 31 | #include <asm/dma.h> |
Andreas Herrmann | 23ac4ae | 2010-09-17 18:03:43 +0200 | [diff] [blame] | 32 | #include <asm/amd_nb.h> |
FUJITA Tomonori | de95762 | 2009-11-10 19:46:14 +0900 | [diff] [blame] | 33 | #include <asm/x86_init.h> |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 34 | #include <linux/crash_dump.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Joerg Roedel | c387aa3 | 2011-04-18 15:45:43 +0200 | [diff] [blame] | 36 | /* |
| 37 | * Using 512M as goal, in case kexec will load kernel_big |
| 38 | * that will do the on-position decompress, and could overlap with |
| 39 | * with the gart aperture that is used. |
| 40 | * Sequence: |
| 41 | * kernel_small |
| 42 | * ==> kexec (with kdump trigger path or gart still enabled) |
| 43 | * ==> kernel_small (gart area become e820_reserved) |
| 44 | * ==> kexec (with kdump trigger path or gart still enabled) |
| 45 | * ==> kerne_big (uncompressed size will be big than 64M or 128M) |
| 46 | * So don't use 512M below as gart iommu, leave the space for kernel |
| 47 | * code for safe. |
| 48 | */ |
| 49 | #define GART_MIN_ADDR (512ULL << 20) |
| 50 | #define GART_MAX_ADDR (1ULL << 32) |
| 51 | |
Joerg Roedel | 0440d4c | 2007-10-24 12:49:50 +0200 | [diff] [blame] | 52 | int gart_iommu_aperture; |
Pavel Machek | 7de6a4c | 2008-03-13 11:03:58 +0100 | [diff] [blame] | 53 | int gart_iommu_aperture_disabled __initdata; |
| 54 | int gart_iommu_aperture_allowed __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | int fallback_aper_order __initdata = 1; /* 64MB */ |
Pavel Machek | 7de6a4c | 2008-03-13 11:03:58 +0100 | [diff] [blame] | 57 | int fallback_aper_force __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | int fix_aperture __initdata = 1; |
| 60 | |
Kairui Song | ffc8599 | 2019-03-08 11:05:08 +0800 | [diff] [blame] | 61 | #if defined(CONFIG_PROC_VMCORE) || defined(CONFIG_PROC_KCORE) |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 62 | /* |
| 63 | * If the first kernel maps the aperture over e820 RAM, the kdump kernel will |
| 64 | * use the same range because it will remain configured in the northbridge. |
| 65 | * Trying to dump this area via /proc/vmcore may crash the machine, so exclude |
| 66 | * it from vmcore. |
| 67 | */ |
| 68 | static unsigned long aperture_pfn_start, aperture_page_count; |
| 69 | |
Kairui Song | ffc8599 | 2019-03-08 11:05:08 +0800 | [diff] [blame] | 70 | static int gart_mem_pfn_is_ram(unsigned long pfn) |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 71 | { |
| 72 | return likely((pfn < aperture_pfn_start) || |
| 73 | (pfn >= aperture_pfn_start + aperture_page_count)); |
| 74 | } |
| 75 | |
Kairui Song | ffc8599 | 2019-03-08 11:05:08 +0800 | [diff] [blame] | 76 | static void __init exclude_from_core(u64 aper_base, u32 aper_order) |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 77 | { |
| 78 | aperture_pfn_start = aper_base >> PAGE_SHIFT; |
| 79 | aperture_page_count = (32 * 1024 * 1024) << aper_order >> PAGE_SHIFT; |
Kairui Song | ffc8599 | 2019-03-08 11:05:08 +0800 | [diff] [blame] | 80 | #ifdef CONFIG_PROC_VMCORE |
| 81 | WARN_ON(register_oldmem_pfn_is_ram(&gart_mem_pfn_is_ram)); |
| 82 | #endif |
| 83 | #ifdef CONFIG_PROC_KCORE |
| 84 | WARN_ON(register_mem_pfn_is_ram(&gart_mem_pfn_is_ram)); |
| 85 | #endif |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 86 | } |
| 87 | #else |
Kairui Song | ffc8599 | 2019-03-08 11:05:08 +0800 | [diff] [blame] | 88 | static void exclude_from_core(u64 aper_base, u32 aper_order) |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 89 | { |
| 90 | } |
| 91 | #endif |
| 92 | |
Andrew Morton | 42442ed | 2005-06-08 15:49:25 -0700 | [diff] [blame] | 93 | /* This code runs before the PCI subsystem is initialized, so just |
| 94 | access the northbridge directly. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 96 | static u32 __init allocate_aperture(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | u32 aper_size; |
Yinghai Lu | 32e3f2b | 2010-12-17 16:58:40 -0800 | [diff] [blame] | 99 | unsigned long addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Yinghai Lu | 7677b2e | 2008-04-14 20:40:37 -0700 | [diff] [blame] | 101 | /* aper_size should <= 1G */ |
| 102 | if (fallback_aper_order > 5) |
| 103 | fallback_aper_order = 5; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 104 | aper_size = (32 * 1024 * 1024) << fallback_aper_order; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 106 | /* |
| 107 | * Aperture has to be naturally aligned. This means a 2GB aperture |
| 108 | * won't have much chance of finding a place in the lower 4GB of |
| 109 | * memory. Unfortunately we cannot move it up because that would |
| 110 | * make the IOMMU useless. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | */ |
Joerg Roedel | c387aa3 | 2011-04-18 15:45:43 +0200 | [diff] [blame] | 112 | addr = memblock_find_in_range(GART_MIN_ADDR, GART_MAX_ADDR, |
| 113 | aper_size, aper_size); |
Wang YanQing | 26bfc54 | 2013-04-16 09:37:34 +0800 | [diff] [blame] | 114 | if (!addr) { |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 115 | pr_err("Cannot allocate aperture memory hole [mem %#010lx-%#010lx] (%uKB)\n", |
| 116 | addr, addr + aper_size - 1, aper_size >> 10); |
Yinghai Lu | 32e3f2b | 2010-12-17 16:58:40 -0800 | [diff] [blame] | 117 | return 0; |
| 118 | } |
Tejun Heo | 24aa078 | 2011-07-12 11:16:06 +0200 | [diff] [blame] | 119 | memblock_reserve(addr, aper_size); |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 120 | pr_info("Mapping aperture over RAM [mem %#010lx-%#010lx] (%uKB)\n", |
| 121 | addr, addr + aper_size - 1, aper_size >> 10); |
Yinghai Lu | 32e3f2b | 2010-12-17 16:58:40 -0800 | [diff] [blame] | 122 | register_nosave_region(addr >> PAGE_SHIFT, |
| 123 | (addr+aper_size) >> PAGE_SHIFT); |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 124 | |
Yinghai Lu | 32e3f2b | 2010-12-17 16:58:40 -0800 | [diff] [blame] | 125 | return (u32)addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Andrew Morton | 42442ed | 2005-06-08 15:49:25 -0700 | [diff] [blame] | 129 | /* Find a PCI capability */ |
Pavel Machek | dd564d0 | 2008-05-27 18:03:56 +0200 | [diff] [blame] | 130 | static u32 __init find_cap(int bus, int slot, int func, int cap) |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 131 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | int bytes; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 133 | u8 pos; |
| 134 | |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 135 | if (!(read_pci_config_16(bus, slot, func, PCI_STATUS) & |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 136 | PCI_STATUS_CAP_LIST)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | return 0; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 138 | |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 139 | pos = read_pci_config_byte(bus, slot, func, PCI_CAPABILITY_LIST); |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 140 | for (bytes = 0; bytes < 48 && pos >= 0x40; bytes++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | u8 id; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 142 | |
| 143 | pos &= ~3; |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 144 | id = read_pci_config_byte(bus, slot, func, pos+PCI_CAP_LIST_ID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (id == 0xff) |
| 146 | break; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 147 | if (id == cap) |
| 148 | return pos; |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 149 | pos = read_pci_config_byte(bus, slot, func, |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 150 | pos+PCI_CAP_LIST_NEXT); |
| 151 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return 0; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 153 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | /* Read a standard AGPv3 bridge header */ |
Pavel Machek | dd564d0 | 2008-05-27 18:03:56 +0200 | [diff] [blame] | 156 | static u32 __init read_agp(int bus, int slot, int func, int cap, u32 *order) |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 157 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | u32 apsize; |
| 159 | u32 apsizereg; |
| 160 | int nbits; |
| 161 | u32 aper_low, aper_hi; |
| 162 | u64 aper; |
Yinghai Lu | 1edc1ab | 2008-04-13 01:11:41 -0700 | [diff] [blame] | 163 | u32 old_order; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 165 | pr_info("pci 0000:%02x:%02x:%02x: AGP bridge\n", bus, slot, func); |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 166 | apsizereg = read_pci_config_16(bus, slot, func, cap + 0x14); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | if (apsizereg == 0xffffffff) { |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 168 | pr_err("pci 0000:%02x:%02x.%d: APSIZE unreadable\n", |
| 169 | bus, slot, func); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
Yinghai Lu | 1edc1ab | 2008-04-13 01:11:41 -0700 | [diff] [blame] | 173 | /* old_order could be the value from NB gart setting */ |
| 174 | old_order = *order; |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | apsize = apsizereg & 0xfff; |
| 177 | /* Some BIOS use weird encodings not in the AGPv3 table. */ |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 178 | if (apsize & 0xff) |
| 179 | apsize |= 0xf00; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | nbits = hweight16(apsize); |
| 181 | *order = 7 - nbits; |
| 182 | if ((int)*order < 0) /* < 32MB */ |
| 183 | *order = 0; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 184 | |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 185 | aper_low = read_pci_config(bus, slot, func, 0x10); |
| 186 | aper_hi = read_pci_config(bus, slot, func, 0x14); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32); |
| 188 | |
Yinghai Lu | 1edc1ab | 2008-04-13 01:11:41 -0700 | [diff] [blame] | 189 | /* |
| 190 | * On some sick chips, APSIZE is 0. It means it wants 4G |
| 191 | * so let double check that order, and lets trust AMD NB settings: |
| 192 | */ |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 193 | pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (old size %uMB)\n", |
| 194 | bus, slot, func, aper, aper + (32ULL << (old_order + 20)) - 1, |
| 195 | 32 << old_order); |
Yinghai Lu | 8c9fd91 | 2008-04-13 18:42:31 -0700 | [diff] [blame] | 196 | if (aper + (32ULL<<(20 + *order)) > 0x100000000ULL) { |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 197 | pr_info("pci 0000:%02x:%02x.%d: AGP aperture size %uMB (APSIZE %#x) is not right, using settings from NB\n", |
| 198 | bus, slot, func, 32 << *order, apsizereg); |
Yinghai Lu | 1edc1ab | 2008-04-13 01:11:41 -0700 | [diff] [blame] | 199 | *order = old_order; |
| 200 | } |
| 201 | |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 202 | pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (%uMB, APSIZE %#x)\n", |
| 203 | bus, slot, func, aper, aper + (32ULL << (*order + 20)) - 1, |
Bjorn Helgaas | a5d3244 | 2014-04-28 15:16:33 -0600 | [diff] [blame] | 204 | 32 << *order, apsizereg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Yinghai Lu | 8c9fd91 | 2008-04-13 18:42:31 -0700 | [diff] [blame] | 206 | if (!aperture_valid(aper, (32*1024*1024) << *order, 32<<20)) |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 207 | return 0; |
| 208 | return (u32)aper; |
| 209 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 211 | /* |
| 212 | * Look for an AGP bridge. Windows only expects the aperture in the |
| 213 | * AGP bridge and some BIOS forget to initialize the Northbridge too. |
| 214 | * Work around this here. |
| 215 | * |
| 216 | * Do an PCI bus scan by hand because we're running before the PCI |
| 217 | * subsystem. |
| 218 | * |
Hans Rosenfeld | eec1d4f | 2010-10-29 17:14:30 +0200 | [diff] [blame] | 219 | * All AMD AGP bridges are AGPv3 compliant, so we can do this scan |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 220 | * generically. It's probably overkill to always scan all slots because |
| 221 | * the AGP bridges should be always an own bus on the HT hierarchy, |
| 222 | * but do it here for future safety. |
| 223 | */ |
Pavel Machek | dd564d0 | 2008-05-27 18:03:56 +0200 | [diff] [blame] | 224 | static u32 __init search_agp_bridge(u32 *order, int *valid_agp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 226 | int bus, slot, func; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | |
| 228 | /* Poor man's PCI discovery */ |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 229 | for (bus = 0; bus < 256; bus++) { |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 230 | for (slot = 0; slot < 32; slot++) { |
| 231 | for (func = 0; func < 8; func++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | u32 class, cap; |
| 233 | u8 type; |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 234 | class = read_pci_config(bus, slot, func, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | PCI_CLASS_REVISION); |
| 236 | if (class == 0xffffffff) |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 237 | break; |
| 238 | |
| 239 | switch (class >> 16) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | case PCI_CLASS_BRIDGE_HOST: |
| 241 | case PCI_CLASS_BRIDGE_OTHER: /* needed? */ |
| 242 | /* AGP bridge? */ |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 243 | cap = find_cap(bus, slot, func, |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 244 | PCI_CAP_ID_AGP); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (!cap) |
| 246 | break; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 247 | *valid_agp = 1; |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 248 | return read_agp(bus, slot, func, cap, |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 249 | order); |
| 250 | } |
| 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | /* No multi-function device? */ |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 253 | type = read_pci_config_byte(bus, slot, func, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | PCI_HEADER_TYPE); |
| 255 | if (!(type & 0x80)) |
| 256 | break; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
Bjorn Helgaas | a5d3244 | 2014-04-28 15:16:33 -0600 | [diff] [blame] | 260 | pr_info("No AGP bridge found\n"); |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | return 0; |
| 263 | } |
| 264 | |
Kees Cook | 4cc7ecb7 | 2016-03-17 14:23:00 -0700 | [diff] [blame] | 265 | static bool gart_fix_e820 __initdata = true; |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 266 | |
| 267 | static int __init parse_gart_mem(char *p) |
| 268 | { |
Kees Cook | 4cc7ecb7 | 2016-03-17 14:23:00 -0700 | [diff] [blame] | 269 | return kstrtobool(p, &gart_fix_e820); |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 270 | } |
| 271 | early_param("gart_fix_e820", parse_gart_mem); |
| 272 | |
Borislav Petkov | 63ecd3b | 2018-11-01 16:24:43 +0100 | [diff] [blame] | 273 | /* |
| 274 | * With kexec/kdump, if the first kernel doesn't shut down the GART and the |
| 275 | * second kernel allocates a different GART region, there might be two |
| 276 | * overlapping GART regions present: |
| 277 | * |
| 278 | * - the first still used by the GART initialized in the first kernel. |
| 279 | * - (sub-)set of it used as normal RAM by the second kernel. |
| 280 | * |
| 281 | * which leads to memory corruptions and a kernel panic eventually. |
| 282 | * |
| 283 | * This can also happen if the BIOS has forgotten to mark the GART region |
| 284 | * as reserved. |
| 285 | * |
| 286 | * Try to update the e820 map to mark that new region as reserved. |
| 287 | */ |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 288 | void __init early_gart_iommu_check(void) |
| 289 | { |
Andi Kleen | fa10ba6 | 2010-07-20 15:19:49 -0700 | [diff] [blame] | 290 | u32 agp_aper_order = 0; |
Yinghai Lu | f3eee54 | 2009-12-14 11:52:15 +0900 | [diff] [blame] | 291 | int i, fix, slot, valid_agp = 0; |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 292 | u32 ctl; |
| 293 | u32 aper_size = 0, aper_order = 0, last_aper_order = 0; |
| 294 | u64 aper_base = 0, last_aper_base = 0; |
Pavel Machek | fa5b8a3 | 2008-05-26 20:40:47 +0200 | [diff] [blame] | 295 | int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0; |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 296 | |
Aravind Gopalakrishnan | 1b45742 | 2015-04-07 16:46:37 -0500 | [diff] [blame] | 297 | if (!amd_gart_present()) |
| 298 | return; |
| 299 | |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 300 | if (!early_pci_allowed()) |
| 301 | return; |
| 302 | |
Pavel Machek | fa5b8a3 | 2008-05-26 20:40:47 +0200 | [diff] [blame] | 303 | /* This is mostly duplicate of iommu_hole_init */ |
Andi Kleen | fa10ba6 | 2010-07-20 15:19:49 -0700 | [diff] [blame] | 304 | search_agp_bridge(&agp_aper_order, &valid_agp); |
Yinghai Lu | f3eee54 | 2009-12-14 11:52:15 +0900 | [diff] [blame] | 305 | |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 306 | fix = 0; |
Jan Beulich | 24d9b70 | 2011-01-10 16:20:23 +0000 | [diff] [blame] | 307 | for (i = 0; amd_nb_bus_dev_ranges[i].dev_limit; i++) { |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 308 | int bus; |
| 309 | int dev_base, dev_limit; |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 310 | |
Jan Beulich | 24d9b70 | 2011-01-10 16:20:23 +0000 | [diff] [blame] | 311 | bus = amd_nb_bus_dev_ranges[i].bus; |
| 312 | dev_base = amd_nb_bus_dev_ranges[i].dev_base; |
| 313 | dev_limit = amd_nb_bus_dev_ranges[i].dev_limit; |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 314 | |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 315 | for (slot = dev_base; slot < dev_limit; slot++) { |
Hans Rosenfeld | eec1d4f | 2010-10-29 17:14:30 +0200 | [diff] [blame] | 316 | if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00))) |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 317 | continue; |
| 318 | |
| 319 | ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL); |
Borislav Petkov | 57ab43e | 2010-09-03 18:39:39 +0200 | [diff] [blame] | 320 | aper_enabled = ctl & GARTEN; |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 321 | aper_order = (ctl >> 1) & 7; |
| 322 | aper_size = (32 * 1024 * 1024) << aper_order; |
| 323 | aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff; |
| 324 | aper_base <<= 25; |
| 325 | |
Pavel Machek | fa5b8a3 | 2008-05-26 20:40:47 +0200 | [diff] [blame] | 326 | if (last_valid) { |
| 327 | if ((aper_order != last_aper_order) || |
| 328 | (aper_base != last_aper_base) || |
| 329 | (aper_enabled != last_aper_enabled)) { |
| 330 | fix = 1; |
| 331 | break; |
| 332 | } |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 333 | } |
Pavel Machek | fa5b8a3 | 2008-05-26 20:40:47 +0200 | [diff] [blame] | 334 | |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 335 | last_aper_order = aper_order; |
| 336 | last_aper_base = aper_base; |
| 337 | last_aper_enabled = aper_enabled; |
Pavel Machek | fa5b8a3 | 2008-05-26 20:40:47 +0200 | [diff] [blame] | 338 | last_valid = 1; |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 339 | } |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if (!fix && !aper_enabled) |
| 343 | return; |
| 344 | |
| 345 | if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL) |
| 346 | fix = 1; |
| 347 | |
| 348 | if (gart_fix_e820 && !fix && aper_enabled) { |
Ingo Molnar | 3bce64f | 2017-01-28 14:14:25 +0100 | [diff] [blame] | 349 | if (e820__mapped_any(aper_base, aper_base + aper_size, |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 350 | E820_TYPE_RAM)) { |
Pavel Machek | 0abbc78 | 2008-05-20 16:27:17 +0200 | [diff] [blame] | 351 | /* reserve it, so we can reuse it in second kernel */ |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 352 | pr_info("e820: reserve [mem %#010Lx-%#010Lx] for GART\n", |
| 353 | aper_base, aper_base + aper_size - 1); |
Ingo Molnar | 09821ff | 2017-01-28 17:09:33 +0100 | [diff] [blame] | 354 | e820__range_add(aper_base, aper_size, E820_TYPE_RESERVED); |
Ingo Molnar | 6464d29 | 2017-01-28 14:03:04 +0100 | [diff] [blame] | 355 | e820__update_table_print(); |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 356 | } |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 357 | } |
| 358 | |
Yinghai Lu | f3eee54 | 2009-12-14 11:52:15 +0900 | [diff] [blame] | 359 | if (valid_agp) |
Pavel Machek | 4f384f8 | 2008-05-26 21:17:30 +0200 | [diff] [blame] | 360 | return; |
| 361 | |
Yinghai Lu | f3eee54 | 2009-12-14 11:52:15 +0900 | [diff] [blame] | 362 | /* disable them all at first */ |
Jan Beulich | 24d9b70 | 2011-01-10 16:20:23 +0000 | [diff] [blame] | 363 | for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) { |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 364 | int bus; |
| 365 | int dev_base, dev_limit; |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 366 | |
Jan Beulich | 24d9b70 | 2011-01-10 16:20:23 +0000 | [diff] [blame] | 367 | bus = amd_nb_bus_dev_ranges[i].bus; |
| 368 | dev_base = amd_nb_bus_dev_ranges[i].dev_base; |
| 369 | dev_limit = amd_nb_bus_dev_ranges[i].dev_limit; |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 370 | |
| 371 | for (slot = dev_base; slot < dev_limit; slot++) { |
Hans Rosenfeld | eec1d4f | 2010-10-29 17:14:30 +0200 | [diff] [blame] | 372 | if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00))) |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 373 | continue; |
| 374 | |
| 375 | ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL); |
Borislav Petkov | 57ab43e | 2010-09-03 18:39:39 +0200 | [diff] [blame] | 376 | ctl &= ~GARTEN; |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 377 | write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl); |
| 378 | } |
Yinghai Lu | aaf2304 | 2008-01-30 13:33:09 +0100 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | } |
| 382 | |
Yinghai Lu | 8c9fd91 | 2008-04-13 18:42:31 -0700 | [diff] [blame] | 383 | static int __initdata printed_gart_size_msg; |
| 384 | |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 385 | int __init gart_iommu_hole_init(void) |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 386 | { |
Yinghai Lu | 8c9fd91 | 2008-04-13 18:42:31 -0700 | [diff] [blame] | 387 | u32 agp_aper_base = 0, agp_aper_order = 0; |
Andi Kleen | 50895c5 | 2005-11-05 17:25:53 +0100 | [diff] [blame] | 388 | u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | u64 aper_base, last_aper_base = 0; |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 390 | int fix, slot, valid_agp = 0; |
| 391 | int i, node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Aravind Gopalakrishnan | 1b45742 | 2015-04-07 16:46:37 -0500 | [diff] [blame] | 393 | if (!amd_gart_present()) |
| 394 | return -ENODEV; |
| 395 | |
Joerg Roedel | 0440d4c | 2007-10-24 12:49:50 +0200 | [diff] [blame] | 396 | if (gart_iommu_aperture_disabled || !fix_aperture || |
| 397 | !early_pci_allowed()) |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 398 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | |
Bjorn Helgaas | a5d3244 | 2014-04-28 15:16:33 -0600 | [diff] [blame] | 400 | pr_info("Checking aperture...\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | |
Yinghai Lu | 8c9fd91 | 2008-04-13 18:42:31 -0700 | [diff] [blame] | 402 | if (!fallback_aper_force) |
| 403 | agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp); |
| 404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | fix = 0; |
Yinghai Lu | 47db4c3 | 2008-01-30 13:33:18 +0100 | [diff] [blame] | 406 | node = 0; |
Jan Beulich | 24d9b70 | 2011-01-10 16:20:23 +0000 | [diff] [blame] | 407 | for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) { |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 408 | int bus; |
| 409 | int dev_base, dev_limit; |
Joerg Roedel | 4b83873 | 2010-04-07 12:57:35 +0200 | [diff] [blame] | 410 | u32 ctl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
Jan Beulich | 24d9b70 | 2011-01-10 16:20:23 +0000 | [diff] [blame] | 412 | bus = amd_nb_bus_dev_ranges[i].bus; |
| 413 | dev_base = amd_nb_bus_dev_ranges[i].dev_base; |
| 414 | dev_limit = amd_nb_bus_dev_ranges[i].dev_limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 416 | for (slot = dev_base; slot < dev_limit; slot++) { |
Hans Rosenfeld | eec1d4f | 2010-10-29 17:14:30 +0200 | [diff] [blame] | 417 | if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00))) |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 418 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 420 | iommu_detected = 1; |
| 421 | gart_iommu_aperture = 1; |
FUJITA Tomonori | de95762 | 2009-11-10 19:46:14 +0900 | [diff] [blame] | 422 | x86_init.iommu.iommu_init = gart_iommu_init; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 423 | |
Joerg Roedel | 4b83873 | 2010-04-07 12:57:35 +0200 | [diff] [blame] | 424 | ctl = read_pci_config(bus, slot, 3, |
| 425 | AMD64_GARTAPERTURECTL); |
| 426 | |
| 427 | /* |
| 428 | * Before we do anything else disable the GART. It may |
| 429 | * still be enabled if we boot into a crash-kernel here. |
| 430 | * Reconfiguring the GART while it is enabled could have |
| 431 | * unknown side-effects. |
| 432 | */ |
| 433 | ctl &= ~GARTEN; |
| 434 | write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl); |
| 435 | |
| 436 | aper_order = (ctl >> 1) & 7; |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 437 | aper_size = (32 * 1024 * 1024) << aper_order; |
| 438 | aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff; |
| 439 | aper_base <<= 25; |
| 440 | |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 441 | pr_info("Node %d: aperture [bus addr %#010Lx-%#010Lx] (%uMB)\n", |
| 442 | node, aper_base, aper_base + aper_size - 1, |
| 443 | aper_size >> 20); |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 444 | node++; |
| 445 | |
| 446 | if (!aperture_valid(aper_base, aper_size, 64<<20)) { |
| 447 | if (valid_agp && agp_aper_base && |
| 448 | agp_aper_base == aper_base && |
| 449 | agp_aper_order == aper_order) { |
| 450 | /* the same between two setting from NB and agp */ |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 451 | if (!no_iommu && |
| 452 | max_pfn > MAX_DMA32_PFN && |
| 453 | !printed_gart_size_msg) { |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 454 | pr_err("you are using iommu with agp, but GART size is less than 64MB\n"); |
Bjorn Helgaas | a5d3244 | 2014-04-28 15:16:33 -0600 | [diff] [blame] | 455 | pr_err("please increase GART size in your BIOS setup\n"); |
| 456 | pr_err("if BIOS doesn't have that option, contact your HW vendor!\n"); |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 457 | printed_gart_size_msg = 1; |
| 458 | } |
| 459 | } else { |
| 460 | fix = 1; |
| 461 | goto out; |
Yinghai Lu | 8c9fd91 | 2008-04-13 18:42:31 -0700 | [diff] [blame] | 462 | } |
Yinghai Lu | 8c9fd91 | 2008-04-13 18:42:31 -0700 | [diff] [blame] | 463 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 465 | if ((last_aper_order && aper_order != last_aper_order) || |
| 466 | (last_aper_base && aper_base != last_aper_base)) { |
| 467 | fix = 1; |
| 468 | goto out; |
| 469 | } |
| 470 | last_aper_order = aper_order; |
| 471 | last_aper_base = aper_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 473 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 475 | out: |
Aaron Durbin | 56dd669 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 476 | if (!fix && !fallback_aper_force) { |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 477 | if (last_aper_base) { |
| 478 | /* |
| 479 | * If this is the kdump kernel, the first kernel |
| 480 | * may have allocated the range over its e820 RAM |
| 481 | * and fixed up the northbridge |
| 482 | */ |
Kairui Song | ffc8599 | 2019-03-08 11:05:08 +0800 | [diff] [blame] | 483 | exclude_from_core(last_aper_base, last_aper_order); |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 484 | |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 485 | return 1; |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 486 | } |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 487 | return 0; |
Aaron Durbin | 56dd669 | 2006-09-26 10:52:40 +0200 | [diff] [blame] | 488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | |
Yinghai Lu | 8c9fd91 | 2008-04-13 18:42:31 -0700 | [diff] [blame] | 490 | if (!fallback_aper_force) { |
| 491 | aper_alloc = agp_aper_base; |
| 492 | aper_order = agp_aper_order; |
| 493 | } |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 494 | |
| 495 | if (aper_alloc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | /* Got the aperture from the AGP bridge */ |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 497 | } else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | force_iommu || |
| 499 | valid_agp || |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 500 | fallback_aper_force) { |
Aravind Gopalakrishnan | 1b45742 | 2015-04-07 16:46:37 -0500 | [diff] [blame] | 501 | pr_info("Your BIOS doesn't leave an aperture memory hole\n"); |
Bjorn Helgaas | a5d3244 | 2014-04-28 15:16:33 -0600 | [diff] [blame] | 502 | pr_info("Please enable the IOMMU option in the BIOS setup\n"); |
Bjorn Helgaas | c96ec95 | 2014-04-14 15:29:19 -0600 | [diff] [blame] | 503 | pr_info("This costs you %dMB of RAM\n", |
Bjorn Helgaas | a5d3244 | 2014-04-28 15:16:33 -0600 | [diff] [blame] | 504 | 32 << fallback_aper_order); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | aper_order = fallback_aper_order; |
| 507 | aper_alloc = allocate_aperture(); |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 508 | if (!aper_alloc) { |
| 509 | /* |
| 510 | * Could disable AGP and IOMMU here, but it's |
| 511 | * probably not worth it. But the later users |
| 512 | * cannot deal with bad apertures and turning |
| 513 | * on the aperture over memory causes very |
| 514 | * strange problems, so it's better to panic |
| 515 | * early. |
| 516 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | panic("Not enough memory for aperture"); |
| 518 | } |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 519 | } else { |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 520 | return 0; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 523 | /* |
| 524 | * If this is the kdump kernel _and_ the first kernel did not |
| 525 | * configure the aperture in the northbridge, this range may |
| 526 | * overlap with the first kernel's memory. We can't access the |
| 527 | * range through vmcore even though it should be part of the dump. |
| 528 | */ |
Kairui Song | ffc8599 | 2019-03-08 11:05:08 +0800 | [diff] [blame] | 529 | exclude_from_core(aper_alloc, aper_order); |
Jiri Bohac | 2a3e83c | 2018-01-06 02:00:13 +0100 | [diff] [blame] | 530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | /* Fix up the north bridges */ |
Jan Beulich | 24d9b70 | 2011-01-10 16:20:23 +0000 | [diff] [blame] | 532 | for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) { |
Borislav Petkov | 260133a | 2010-09-03 18:39:40 +0200 | [diff] [blame] | 533 | int bus, dev_base, dev_limit; |
| 534 | |
| 535 | /* |
| 536 | * Don't enable translation yet but enable GART IO and CPU |
| 537 | * accesses and set DISTLBWALKPRB since GART table memory is UC. |
| 538 | */ |
Joerg Roedel | c34151a | 2011-04-18 15:45:45 +0200 | [diff] [blame] | 539 | u32 ctl = aper_order << 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | |
Jan Beulich | 24d9b70 | 2011-01-10 16:20:23 +0000 | [diff] [blame] | 541 | bus = amd_nb_bus_dev_ranges[i].bus; |
| 542 | dev_base = amd_nb_bus_dev_ranges[i].dev_base; |
| 543 | dev_limit = amd_nb_bus_dev_ranges[i].dev_limit; |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 544 | for (slot = dev_base; slot < dev_limit; slot++) { |
Hans Rosenfeld | eec1d4f | 2010-10-29 17:14:30 +0200 | [diff] [blame] | 545 | if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00))) |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 546 | continue; |
| 547 | |
Borislav Petkov | 260133a | 2010-09-03 18:39:40 +0200 | [diff] [blame] | 548 | write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl); |
Yinghai Lu | 55c0d72 | 2008-04-19 01:31:11 -0700 | [diff] [blame] | 549 | write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25); |
| 550 | } |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 551 | } |
Rafael J. Wysocki | 6703f6d | 2008-06-10 00:10:48 +0200 | [diff] [blame] | 552 | |
| 553 | set_up_gart_resume(aper_order, aper_alloc); |
Konrad Rzeszutek Wilk | 480125b | 2010-08-26 13:57:57 -0400 | [diff] [blame] | 554 | |
| 555 | return 1; |
Ingo Molnar | c140df9 | 2008-01-30 13:30:09 +0100 | [diff] [blame] | 556 | } |