]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/x86/kernel/aperture_64.c
x86/speculation: Consolidate CPU whitelists
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / aperture_64.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
c140df97 2/*
1da177e4 3 * Firmware replacement code.
c140df97 4 *
8caac563
PM
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 *
c140df97
IM
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 *
1da177e4 12 * Copyright 2002 Andi Kleen, SuSE Labs.
1da177e4 13 */
a5d3244a
BH
14#define pr_fmt(fmt) "AGP: " fmt
15
1da177e4
LT
16#include <linux/kernel.h>
17#include <linux/types.h>
18#include <linux/init.h>
32e3f2b0 19#include <linux/memblock.h>
1da177e4
LT
20#include <linux/mmzone.h>
21#include <linux/pci_ids.h>
22#include <linux/pci.h>
23#include <linux/bitops.h>
2050d45d 24#include <linux/suspend.h>
66441bd3 25#include <asm/e820/api.h>
1da177e4 26#include <asm/io.h>
46a7fa27 27#include <asm/iommu.h>
395624fc 28#include <asm/gart.h>
1da177e4 29#include <asm/pci-direct.h>
ca8642f6 30#include <asm/dma.h>
23ac4ae8 31#include <asm/amd_nb.h>
de957628 32#include <asm/x86_init.h>
2fdc5c53 33#include <linux/crash_dump.h>
1da177e4 34
c387aa3a
JR
35/*
36 * Using 512M as goal, in case kexec will load kernel_big
37 * that will do the on-position decompress, and could overlap with
38 * with the gart aperture that is used.
39 * Sequence:
40 * kernel_small
41 * ==> kexec (with kdump trigger path or gart still enabled)
42 * ==> kernel_small (gart area become e820_reserved)
43 * ==> kexec (with kdump trigger path or gart still enabled)
44 * ==> kerne_big (uncompressed size will be big than 64M or 128M)
45 * So don't use 512M below as gart iommu, leave the space for kernel
46 * code for safe.
47 */
48#define GART_MIN_ADDR (512ULL << 20)
49#define GART_MAX_ADDR (1ULL << 32)
50
0440d4c0 51int gart_iommu_aperture;
7de6a4cd
PM
52int gart_iommu_aperture_disabled __initdata;
53int gart_iommu_aperture_allowed __initdata;
1da177e4
LT
54
55int fallback_aper_order __initdata = 1; /* 64MB */
7de6a4cd 56int fallback_aper_force __initdata;
1da177e4
LT
57
58int fix_aperture __initdata = 1;
59
2fdc5c53
JB
60#ifdef CONFIG_PROC_VMCORE
61/*
62 * If the first kernel maps the aperture over e820 RAM, the kdump kernel will
63 * use the same range because it will remain configured in the northbridge.
64 * Trying to dump this area via /proc/vmcore may crash the machine, so exclude
65 * it from vmcore.
66 */
67static unsigned long aperture_pfn_start, aperture_page_count;
68
69static int gart_oldmem_pfn_is_ram(unsigned long pfn)
70{
71 return likely((pfn < aperture_pfn_start) ||
72 (pfn >= aperture_pfn_start + aperture_page_count));
73}
74
75static void exclude_from_vmcore(u64 aper_base, u32 aper_order)
76{
77 aperture_pfn_start = aper_base >> PAGE_SHIFT;
78 aperture_page_count = (32 * 1024 * 1024) << aper_order >> PAGE_SHIFT;
79 WARN_ON(register_oldmem_pfn_is_ram(&gart_oldmem_pfn_is_ram));
80}
81#else
82static void exclude_from_vmcore(u64 aper_base, u32 aper_order)
83{
84}
85#endif
86
42442ed5
AM
87/* This code runs before the PCI subsystem is initialized, so just
88 access the northbridge directly. */
1da177e4 89
c140df97 90static u32 __init allocate_aperture(void)
1da177e4 91{
1da177e4 92 u32 aper_size;
32e3f2b0 93 unsigned long addr;
1da177e4 94
7677b2ef
YL
95 /* aper_size should <= 1G */
96 if (fallback_aper_order > 5)
97 fallback_aper_order = 5;
c140df97 98 aper_size = (32 * 1024 * 1024) << fallback_aper_order;
1da177e4 99
c140df97
IM
100 /*
101 * Aperture has to be naturally aligned. This means a 2GB aperture
102 * won't have much chance of finding a place in the lower 4GB of
103 * memory. Unfortunately we cannot move it up because that would
104 * make the IOMMU useless.
1da177e4 105 */
c387aa3a
JR
106 addr = memblock_find_in_range(GART_MIN_ADDR, GART_MAX_ADDR,
107 aper_size, aper_size);
26bfc540 108 if (!addr) {
c96ec953
BH
109 pr_err("Cannot allocate aperture memory hole [mem %#010lx-%#010lx] (%uKB)\n",
110 addr, addr + aper_size - 1, aper_size >> 10);
32e3f2b0
YL
111 return 0;
112 }
24aa0788 113 memblock_reserve(addr, aper_size);
c96ec953
BH
114 pr_info("Mapping aperture over RAM [mem %#010lx-%#010lx] (%uKB)\n",
115 addr, addr + aper_size - 1, aper_size >> 10);
32e3f2b0
YL
116 register_nosave_region(addr >> PAGE_SHIFT,
117 (addr+aper_size) >> PAGE_SHIFT);
c140df97 118
32e3f2b0 119 return (u32)addr;
1da177e4
LT
120}
121
1da177e4 122
1da177e4 123/* Read a standard AGPv3 bridge header */
dd564d0c 124static u32 __init read_agp(int bus, int slot, int func, int cap, u32 *order)
c140df97 125{
1da177e4
LT
126 u32 apsize;
127 u32 apsizereg;
128 int nbits;
129 u32 aper_low, aper_hi;
130 u64 aper;
1edc1ab3 131 u32 old_order;
1da177e4 132
c96ec953 133 pr_info("pci 0000:%02x:%02x:%02x: AGP bridge\n", bus, slot, func);
55c0d721 134 apsizereg = read_pci_config_16(bus, slot, func, cap + 0x14);
1da177e4 135 if (apsizereg == 0xffffffff) {
c96ec953
BH
136 pr_err("pci 0000:%02x:%02x.%d: APSIZE unreadable\n",
137 bus, slot, func);
1da177e4
LT
138 return 0;
139 }
140
1edc1ab3
YL
141 /* old_order could be the value from NB gart setting */
142 old_order = *order;
143
1da177e4
LT
144 apsize = apsizereg & 0xfff;
145 /* Some BIOS use weird encodings not in the AGPv3 table. */
c140df97
IM
146 if (apsize & 0xff)
147 apsize |= 0xf00;
1da177e4
LT
148 nbits = hweight16(apsize);
149 *order = 7 - nbits;
150 if ((int)*order < 0) /* < 32MB */
151 *order = 0;
c140df97 152
55c0d721
YL
153 aper_low = read_pci_config(bus, slot, func, 0x10);
154 aper_hi = read_pci_config(bus, slot, func, 0x14);
1da177e4
LT
155 aper = (aper_low & ~((1<<22)-1)) | ((u64)aper_hi << 32);
156
1edc1ab3
YL
157 /*
158 * On some sick chips, APSIZE is 0. It means it wants 4G
159 * so let double check that order, and lets trust AMD NB settings:
160 */
c96ec953
BH
161 pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (old size %uMB)\n",
162 bus, slot, func, aper, aper + (32ULL << (old_order + 20)) - 1,
163 32 << old_order);
8c9fd91a 164 if (aper + (32ULL<<(20 + *order)) > 0x100000000ULL) {
c96ec953
BH
165 pr_info("pci 0000:%02x:%02x.%d: AGP aperture size %uMB (APSIZE %#x) is not right, using settings from NB\n",
166 bus, slot, func, 32 << *order, apsizereg);
1edc1ab3
YL
167 *order = old_order;
168 }
169
c96ec953
BH
170 pr_info("pci 0000:%02x:%02x.%d: AGP aperture [bus addr %#010Lx-%#010Lx] (%uMB, APSIZE %#x)\n",
171 bus, slot, func, aper, aper + (32ULL << (*order + 20)) - 1,
a5d3244a 172 32 << *order, apsizereg);
1da177e4 173
8c9fd91a 174 if (!aperture_valid(aper, (32*1024*1024) << *order, 32<<20))
c140df97
IM
175 return 0;
176 return (u32)aper;
177}
1da177e4 178
c140df97
IM
179/*
180 * Look for an AGP bridge. Windows only expects the aperture in the
181 * AGP bridge and some BIOS forget to initialize the Northbridge too.
182 * Work around this here.
183 *
184 * Do an PCI bus scan by hand because we're running before the PCI
185 * subsystem.
186 *
eec1d4fa 187 * All AMD AGP bridges are AGPv3 compliant, so we can do this scan
c140df97
IM
188 * generically. It's probably overkill to always scan all slots because
189 * the AGP bridges should be always an own bus on the HT hierarchy,
190 * but do it here for future safety.
191 */
dd564d0c 192static u32 __init search_agp_bridge(u32 *order, int *valid_agp)
1da177e4 193{
55c0d721 194 int bus, slot, func;
1da177e4
LT
195
196 /* Poor man's PCI discovery */
55c0d721 197 for (bus = 0; bus < 256; bus++) {
c140df97
IM
198 for (slot = 0; slot < 32; slot++) {
199 for (func = 0; func < 8; func++) {
1da177e4
LT
200 u32 class, cap;
201 u8 type;
55c0d721 202 class = read_pci_config(bus, slot, func,
1da177e4
LT
203 PCI_CLASS_REVISION);
204 if (class == 0xffffffff)
c140df97
IM
205 break;
206
207 switch (class >> 16) {
1da177e4
LT
208 case PCI_CLASS_BRIDGE_HOST:
209 case PCI_CLASS_BRIDGE_OTHER: /* needed? */
210 /* AGP bridge? */
12c961ac
GP
211 cap = pci_early_find_cap(bus, slot,
212 func, PCI_CAP_ID_AGP);
1da177e4
LT
213 if (!cap)
214 break;
c140df97 215 *valid_agp = 1;
55c0d721 216 return read_agp(bus, slot, func, cap,
c140df97
IM
217 order);
218 }
219
1da177e4 220 /* No multi-function device? */
55c0d721 221 type = read_pci_config_byte(bus, slot, func,
1da177e4
LT
222 PCI_HEADER_TYPE);
223 if (!(type & 0x80))
224 break;
c140df97
IM
225 }
226 }
1da177e4 227 }
a5d3244a 228 pr_info("No AGP bridge found\n");
c140df97 229
1da177e4
LT
230 return 0;
231}
232
4cc7ecb7 233static bool gart_fix_e820 __initdata = true;
aaf23042
YL
234
235static int __init parse_gart_mem(char *p)
236{
4cc7ecb7 237 return kstrtobool(p, &gart_fix_e820);
aaf23042
YL
238}
239early_param("gart_fix_e820", parse_gart_mem);
240
241void __init early_gart_iommu_check(void)
242{
243 /*
244 * in case it is enabled before, esp for kexec/kdump,
245 * previous kernel already enable that. memset called
246 * by allocate_aperture/__alloc_bootmem_nopanic cause restart.
247 * or second kernel have different position for GART hole. and new
248 * kernel could use hole as RAM that is still used by GART set by
249 * first kernel
250 * or BIOS forget to put that in reserved.
251 * try to update e820 to make that region as reserved.
252 */
fa10ba64 253 u32 agp_aper_order = 0;
f3eee542 254 int i, fix, slot, valid_agp = 0;
aaf23042
YL
255 u32 ctl;
256 u32 aper_size = 0, aper_order = 0, last_aper_order = 0;
257 u64 aper_base = 0, last_aper_base = 0;
fa5b8a30 258 int aper_enabled = 0, last_aper_enabled = 0, last_valid = 0;
aaf23042 259
1b457429
AG
260 if (!amd_gart_present())
261 return;
262
aaf23042
YL
263 if (!early_pci_allowed())
264 return;
265
fa5b8a30 266 /* This is mostly duplicate of iommu_hole_init */
fa10ba64 267 search_agp_bridge(&agp_aper_order, &valid_agp);
f3eee542 268
aaf23042 269 fix = 0;
24d9b70b 270 for (i = 0; amd_nb_bus_dev_ranges[i].dev_limit; i++) {
55c0d721
YL
271 int bus;
272 int dev_base, dev_limit;
273
24d9b70b
JB
274 bus = amd_nb_bus_dev_ranges[i].bus;
275 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
276 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
55c0d721
YL
277
278 for (slot = dev_base; slot < dev_limit; slot++) {
eec1d4fa 279 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
55c0d721
YL
280 continue;
281
282 ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
57ab43e3 283 aper_enabled = ctl & GARTEN;
55c0d721
YL
284 aper_order = (ctl >> 1) & 7;
285 aper_size = (32 * 1024 * 1024) << aper_order;
286 aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
287 aper_base <<= 25;
288
fa5b8a30
PM
289 if (last_valid) {
290 if ((aper_order != last_aper_order) ||
291 (aper_base != last_aper_base) ||
292 (aper_enabled != last_aper_enabled)) {
293 fix = 1;
294 break;
295 }
55c0d721 296 }
fa5b8a30 297
55c0d721
YL
298 last_aper_order = aper_order;
299 last_aper_base = aper_base;
300 last_aper_enabled = aper_enabled;
fa5b8a30 301 last_valid = 1;
aaf23042 302 }
aaf23042
YL
303 }
304
305 if (!fix && !aper_enabled)
306 return;
307
308 if (!aper_base || !aper_size || aper_base + aper_size > 0x100000000UL)
309 fix = 1;
310
311 if (gart_fix_e820 && !fix && aper_enabled) {
3bce64f0 312 if (e820__mapped_any(aper_base, aper_base + aper_size,
09821ff1 313 E820_TYPE_RAM)) {
0abbc78a 314 /* reserve it, so we can reuse it in second kernel */
c96ec953
BH
315 pr_info("e820: reserve [mem %#010Lx-%#010Lx] for GART\n",
316 aper_base, aper_base + aper_size - 1);
09821ff1 317 e820__range_add(aper_base, aper_size, E820_TYPE_RESERVED);
6464d294 318 e820__update_table_print();
aaf23042 319 }
aaf23042
YL
320 }
321
f3eee542 322 if (valid_agp)
4f384f8b
PM
323 return;
324
f3eee542 325 /* disable them all at first */
24d9b70b 326 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
55c0d721
YL
327 int bus;
328 int dev_base, dev_limit;
329
24d9b70b
JB
330 bus = amd_nb_bus_dev_ranges[i].bus;
331 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
332 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
aaf23042 333
55c0d721 334 for (slot = dev_base; slot < dev_limit; slot++) {
eec1d4fa 335 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
55c0d721
YL
336 continue;
337
338 ctl = read_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL);
57ab43e3 339 ctl &= ~GARTEN;
55c0d721
YL
340 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
341 }
aaf23042
YL
342 }
343
344}
345
8c9fd91a
YL
346static int __initdata printed_gart_size_msg;
347
480125ba 348int __init gart_iommu_hole_init(void)
c140df97 349{
8c9fd91a 350 u32 agp_aper_base = 0, agp_aper_order = 0;
50895c5d 351 u32 aper_size, aper_alloc = 0, aper_order = 0, last_aper_order = 0;
1da177e4 352 u64 aper_base, last_aper_base = 0;
55c0d721
YL
353 int fix, slot, valid_agp = 0;
354 int i, node;
1da177e4 355
1b457429
AG
356 if (!amd_gart_present())
357 return -ENODEV;
358
0440d4c0
JR
359 if (gart_iommu_aperture_disabled || !fix_aperture ||
360 !early_pci_allowed())
480125ba 361 return -ENODEV;
1da177e4 362
a5d3244a 363 pr_info("Checking aperture...\n");
1da177e4 364
8c9fd91a
YL
365 if (!fallback_aper_force)
366 agp_aper_base = search_agp_bridge(&agp_aper_order, &valid_agp);
367
1da177e4 368 fix = 0;
47db4c3e 369 node = 0;
24d9b70b 370 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
55c0d721
YL
371 int bus;
372 int dev_base, dev_limit;
4b83873d 373 u32 ctl;
55c0d721 374
24d9b70b
JB
375 bus = amd_nb_bus_dev_ranges[i].bus;
376 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
377 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
55c0d721
YL
378
379 for (slot = dev_base; slot < dev_limit; slot++) {
eec1d4fa 380 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
55c0d721
YL
381 continue;
382
383 iommu_detected = 1;
384 gart_iommu_aperture = 1;
de957628 385 x86_init.iommu.iommu_init = gart_iommu_init;
55c0d721 386
4b83873d
JR
387 ctl = read_pci_config(bus, slot, 3,
388 AMD64_GARTAPERTURECTL);
389
390 /*
391 * Before we do anything else disable the GART. It may
392 * still be enabled if we boot into a crash-kernel here.
393 * Reconfiguring the GART while it is enabled could have
394 * unknown side-effects.
395 */
396 ctl &= ~GARTEN;
397 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
398
399 aper_order = (ctl >> 1) & 7;
55c0d721
YL
400 aper_size = (32 * 1024 * 1024) << aper_order;
401 aper_base = read_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE) & 0x7fff;
402 aper_base <<= 25;
403
c96ec953
BH
404 pr_info("Node %d: aperture [bus addr %#010Lx-%#010Lx] (%uMB)\n",
405 node, aper_base, aper_base + aper_size - 1,
406 aper_size >> 20);
55c0d721
YL
407 node++;
408
409 if (!aperture_valid(aper_base, aper_size, 64<<20)) {
410 if (valid_agp && agp_aper_base &&
411 agp_aper_base == aper_base &&
412 agp_aper_order == aper_order) {
413 /* the same between two setting from NB and agp */
c987d12f
YL
414 if (!no_iommu &&
415 max_pfn > MAX_DMA32_PFN &&
416 !printed_gart_size_msg) {
c96ec953 417 pr_err("you are using iommu with agp, but GART size is less than 64MB\n");
a5d3244a
BH
418 pr_err("please increase GART size in your BIOS setup\n");
419 pr_err("if BIOS doesn't have that option, contact your HW vendor!\n");
55c0d721
YL
420 printed_gart_size_msg = 1;
421 }
422 } else {
423 fix = 1;
424 goto out;
8c9fd91a 425 }
8c9fd91a 426 }
1da177e4 427
55c0d721
YL
428 if ((last_aper_order && aper_order != last_aper_order) ||
429 (last_aper_base && aper_base != last_aper_base)) {
430 fix = 1;
431 goto out;
432 }
433 last_aper_order = aper_order;
434 last_aper_base = aper_base;
1da177e4 435 }
c140df97 436 }
1da177e4 437
55c0d721 438out:
56dd669a 439 if (!fix && !fallback_aper_force) {
2fdc5c53
JB
440 if (last_aper_base) {
441 /*
442 * If this is the kdump kernel, the first kernel
443 * may have allocated the range over its e820 RAM
444 * and fixed up the northbridge
445 */
446 exclude_from_vmcore(last_aper_base, last_aper_order);
447
480125ba 448 return 1;
2fdc5c53 449 }
480125ba 450 return 0;
56dd669a 451 }
1da177e4 452
8c9fd91a
YL
453 if (!fallback_aper_force) {
454 aper_alloc = agp_aper_base;
455 aper_order = agp_aper_order;
456 }
c140df97
IM
457
458 if (aper_alloc) {
1da177e4 459 /* Got the aperture from the AGP bridge */
c987d12f 460 } else if ((!no_iommu && max_pfn > MAX_DMA32_PFN) ||
1da177e4
LT
461 force_iommu ||
462 valid_agp ||
c140df97 463 fallback_aper_force) {
1b457429 464 pr_info("Your BIOS doesn't leave an aperture memory hole\n");
a5d3244a 465 pr_info("Please enable the IOMMU option in the BIOS setup\n");
c96ec953 466 pr_info("This costs you %dMB of RAM\n",
a5d3244a 467 32 << fallback_aper_order);
1da177e4
LT
468
469 aper_order = fallback_aper_order;
470 aper_alloc = allocate_aperture();
c140df97
IM
471 if (!aper_alloc) {
472 /*
473 * Could disable AGP and IOMMU here, but it's
474 * probably not worth it. But the later users
475 * cannot deal with bad apertures and turning
476 * on the aperture over memory causes very
477 * strange problems, so it's better to panic
478 * early.
479 */
1da177e4
LT
480 panic("Not enough memory for aperture");
481 }
c140df97 482 } else {
480125ba 483 return 0;
c140df97 484 }
1da177e4 485
2fdc5c53
JB
486 /*
487 * If this is the kdump kernel _and_ the first kernel did not
488 * configure the aperture in the northbridge, this range may
489 * overlap with the first kernel's memory. We can't access the
490 * range through vmcore even though it should be part of the dump.
491 */
492 exclude_from_vmcore(aper_alloc, aper_order);
493
1da177e4 494 /* Fix up the north bridges */
24d9b70b 495 for (i = 0; i < amd_nb_bus_dev_ranges[i].dev_limit; i++) {
260133ab
BP
496 int bus, dev_base, dev_limit;
497
498 /*
499 * Don't enable translation yet but enable GART IO and CPU
500 * accesses and set DISTLBWALKPRB since GART table memory is UC.
501 */
c34151a7 502 u32 ctl = aper_order << 1;
55c0d721 503
24d9b70b
JB
504 bus = amd_nb_bus_dev_ranges[i].bus;
505 dev_base = amd_nb_bus_dev_ranges[i].dev_base;
506 dev_limit = amd_nb_bus_dev_ranges[i].dev_limit;
55c0d721 507 for (slot = dev_base; slot < dev_limit; slot++) {
eec1d4fa 508 if (!early_is_amd_nb(read_pci_config(bus, slot, 3, 0x00)))
55c0d721
YL
509 continue;
510
260133ab 511 write_pci_config(bus, slot, 3, AMD64_GARTAPERTURECTL, ctl);
55c0d721
YL
512 write_pci_config(bus, slot, 3, AMD64_GARTAPERTUREBASE, aper_alloc >> 25);
513 }
c140df97 514 }
6703f6d1
RW
515
516 set_up_gart_resume(aper_order, aper_alloc);
480125ba
KRW
517
518 return 1;
c140df97 519}