]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/iommu/amd_iommu_init.c
Merge tag 'io_uring-5.7-2020-05-22' of git://git.kernel.dk/linux-block
[thirdparty/linux.git] / drivers / iommu / amd_iommu_init.c
CommitLineData
45051539 1// SPDX-License-Identifier: GPL-2.0-only
f6e2e6b6 2/*
5d0d7156 3 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
63ce3ae8 4 * Author: Joerg Roedel <jroedel@suse.de>
f6e2e6b6 5 * Leo Duran <leo.duran@amd.com>
f6e2e6b6
JR
6 */
7
101fa037 8#define pr_fmt(fmt) "AMD-Vi: " fmt
5f226da1 9#define dev_fmt(fmt) pr_fmt(fmt)
101fa037 10
f6e2e6b6
JR
11#include <linux/pci.h>
12#include <linux/acpi.h>
f6e2e6b6 13#include <linux/list.h>
5c87f62d 14#include <linux/bitmap.h>
5a0e3ad6 15#include <linux/slab.h>
f3c6ea1b 16#include <linux/syscore_ops.h>
a80dc3e0
JR
17#include <linux/interrupt.h>
18#include <linux/msi.h>
403f81d8 19#include <linux/amd-iommu.h>
400a28a0 20#include <linux/export.h>
066f2e98 21#include <linux/iommu.h>
ebcfa284 22#include <linux/kmemleak.h>
2543a786 23#include <linux/mem_encrypt.h>
f6e2e6b6 24#include <asm/pci-direct.h>
46a7fa27 25#include <asm/iommu.h>
66929812
SS
26#include <asm/apic.h>
27#include <asm/msidef.h>
1d9b16d1 28#include <asm/gart.h>
ea1b0d39 29#include <asm/x86_init.h>
22e6daf4 30#include <asm/iommu_table.h>
eb1eb7ae 31#include <asm/io_apic.h>
6b474b82 32#include <asm/irq_remapping.h>
403f81d8 33
3ac3e5ee 34#include <linux/crash_dump.h>
93d05155 35#include "amd_iommu.h"
403f81d8
JR
36#include "amd_iommu_proto.h"
37#include "amd_iommu_types.h"
05152a04 38#include "irq_remapping.h"
403f81d8 39
f6e2e6b6
JR
40/*
41 * definitions for the ACPI scanning code
42 */
f6e2e6b6 43#define IVRS_HEADER_LENGTH 48
f6e2e6b6 44
8c7142f5 45#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
f6e2e6b6
JR
46#define ACPI_IVMD_TYPE_ALL 0x20
47#define ACPI_IVMD_TYPE 0x21
48#define ACPI_IVMD_TYPE_RANGE 0x22
49
50#define IVHD_DEV_ALL 0x01
51#define IVHD_DEV_SELECT 0x02
52#define IVHD_DEV_SELECT_RANGE_START 0x03
53#define IVHD_DEV_RANGE_END 0x04
54#define IVHD_DEV_ALIAS 0x42
55#define IVHD_DEV_ALIAS_RANGE 0x43
56#define IVHD_DEV_EXT_SELECT 0x46
57#define IVHD_DEV_EXT_SELECT_RANGE 0x47
6efed63b 58#define IVHD_DEV_SPECIAL 0x48
8c7142f5 59#define IVHD_DEV_ACPI_HID 0xf0
6efed63b 60
2a0cb4e2
WZ
61#define UID_NOT_PRESENT 0
62#define UID_IS_INTEGER 1
63#define UID_IS_CHARACTER 2
64
6efed63b
JR
65#define IVHD_SPECIAL_IOAPIC 1
66#define IVHD_SPECIAL_HPET 2
f6e2e6b6 67
6da7342f
JR
68#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
69#define IVHD_FLAG_PASSPW_EN_MASK 0x02
70#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
71#define IVHD_FLAG_ISOC_EN_MASK 0x08
f6e2e6b6
JR
72
73#define IVMD_FLAG_EXCL_RANGE 0x08
387caf0b
AH
74#define IVMD_FLAG_IW 0x04
75#define IVMD_FLAG_IR 0x02
f6e2e6b6
JR
76#define IVMD_FLAG_UNITY_MAP 0x01
77
78#define ACPI_DEVFLAG_INITPASS 0x01
79#define ACPI_DEVFLAG_EXTINT 0x02
80#define ACPI_DEVFLAG_NMI 0x04
81#define ACPI_DEVFLAG_SYSMGT1 0x10
82#define ACPI_DEVFLAG_SYSMGT2 0x20
83#define ACPI_DEVFLAG_LINT0 0x40
84#define ACPI_DEVFLAG_LINT1 0x80
85#define ACPI_DEVFLAG_ATSDIS 0x10000000
86
8bda0cfb 87#define LOOP_TIMEOUT 100000
b65233a9
JR
88/*
89 * ACPI table definitions
90 *
91 * These data structures are laid over the table to parse the important values
92 * out of it.
93 */
94
b0119e87
JR
95extern const struct iommu_ops amd_iommu_ops;
96
b65233a9
JR
97/*
98 * structure describing one IOMMU in the ACPI table. Typically followed by one
99 * or more ivhd_entrys.
100 */
f6e2e6b6
JR
101struct ivhd_header {
102 u8 type;
103 u8 flags;
104 u16 length;
105 u16 devid;
106 u16 cap_ptr;
107 u64 mmio_phys;
108 u16 pci_seg;
109 u16 info;
7d7d38af
SS
110 u32 efr_attr;
111
112 /* Following only valid on IVHD type 11h and 40h */
113 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
114 u64 res;
f6e2e6b6
JR
115} __attribute__((packed));
116
b65233a9
JR
117/*
118 * A device entry describing which devices a specific IOMMU translates and
119 * which requestor ids they use.
120 */
f6e2e6b6
JR
121struct ivhd_entry {
122 u8 type;
123 u16 devid;
124 u8 flags;
125 u32 ext;
2a0cb4e2
WZ
126 u32 hidh;
127 u64 cid;
128 u8 uidf;
129 u8 uidl;
130 u8 uid;
f6e2e6b6
JR
131} __attribute__((packed));
132
b65233a9
JR
133/*
134 * An AMD IOMMU memory definition structure. It defines things like exclusion
135 * ranges for devices and regions that should be unity mapped.
136 */
f6e2e6b6
JR
137struct ivmd_header {
138 u8 type;
139 u8 flags;
140 u16 length;
141 u16 devid;
142 u16 aux;
143 u64 resv;
144 u64 range_start;
145 u64 range_length;
146} __attribute__((packed));
147
fefda117 148bool amd_iommu_dump;
05152a04 149bool amd_iommu_irq_remap __read_mostly;
fefda117 150
d98de49a 151int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
81307143 152static int amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
3928aa3f 153
02f3b3f5 154static bool amd_iommu_detected;
a5235725 155static bool __initdata amd_iommu_disabled;
8c7142f5 156static int amd_iommu_target_ivhd_type;
c1cbebee 157
b65233a9
JR
158u16 amd_iommu_last_bdf; /* largest PCI device id we have
159 to handle */
2e22847f 160LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
b65233a9 161 we find in ACPI */
621a5f7a 162bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
928abd25 163
2e22847f 164LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
b65233a9 165 system */
928abd25 166
bb52777e
JR
167/* Array to assign indices to IOMMUs*/
168struct amd_iommu *amd_iommus[MAX_IOMMUS];
6b9376e3
SS
169
170/* Number of IOMMUs present in the system */
171static int amd_iommus_present;
bb52777e 172
318afd41
JR
173/* IOMMUs have a non-present cache? */
174bool amd_iommu_np_cache __read_mostly;
60f723b4 175bool amd_iommu_iotlb_sup __read_mostly = true;
318afd41 176
a919a018 177u32 amd_iommu_max_pasid __read_mostly = ~0;
62f71abb 178
400a28a0 179bool amd_iommu_v2_present __read_mostly;
4160cd9e 180static bool amd_iommu_pc_present __read_mostly;
400a28a0 181
5abcdba4
JR
182bool amd_iommu_force_isolation __read_mostly;
183
b65233a9
JR
184/*
185 * Pointer to the device table which is shared by all AMD IOMMUs
186 * it is indexed by the PCI device id or the HT unit id and contains
187 * information about the domain the device belongs to as well as the
188 * page table root pointer.
189 */
928abd25 190struct dev_table_entry *amd_iommu_dev_table;
45a01c42
BH
191/*
192 * Pointer to a device table which the content of old device table
193 * will be copied to. It's only be used in kdump kernel.
194 */
195static struct dev_table_entry *old_dev_tbl_cpy;
b65233a9
JR
196
197/*
198 * The alias table is a driver specific data structure which contains the
199 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
200 * More than one device can share the same requestor id.
201 */
928abd25 202u16 *amd_iommu_alias_table;
b65233a9
JR
203
204/*
205 * The rlookup table is used to find the IOMMU which is responsible
206 * for a specific device. It is also indexed by the PCI device id.
207 */
928abd25 208struct amd_iommu **amd_iommu_rlookup_table;
daae2d25 209EXPORT_SYMBOL(amd_iommu_rlookup_table);
b65233a9 210
b65233a9 211/*
0ea2c422
JR
212 * This table is used to find the irq remapping table for a given device id
213 * quickly.
214 */
215struct irq_remap_table **irq_lookup_table;
216
b65233a9 217/*
df805abb 218 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
b65233a9
JR
219 * to know which ones are already in use.
220 */
928abd25
JR
221unsigned long *amd_iommu_pd_alloc_bitmap;
222
b65233a9
JR
223static u32 dev_table_size; /* size of the device table */
224static u32 alias_table_size; /* size of the alias table */
225static u32 rlookup_table_size; /* size if the rlookup table */
3e8064ba 226
2c0ae172
JR
227enum iommu_init_state {
228 IOMMU_START_STATE,
229 IOMMU_IVRS_DETECTED,
230 IOMMU_ACPI_FINISHED,
231 IOMMU_ENABLED,
232 IOMMU_PCI_INIT,
233 IOMMU_INTERRUPTS_EN,
234 IOMMU_DMA_OPS,
235 IOMMU_INITIALIZED,
236 IOMMU_NOT_FOUND,
237 IOMMU_INIT_ERROR,
1b1e942e 238 IOMMU_CMDLINE_DISABLED,
2c0ae172
JR
239};
240
235dacbc
JR
241/* Early ioapic and hpet maps from kernel command line */
242#define EARLY_MAP_SIZE 4
243static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
244static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
2a0cb4e2
WZ
245static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
246
235dacbc
JR
247static int __initdata early_ioapic_map_size;
248static int __initdata early_hpet_map_size;
2a0cb4e2
WZ
249static int __initdata early_acpihid_map_size;
250
dfbb6d47 251static bool __initdata cmdline_maps;
235dacbc 252
2c0ae172
JR
253static enum iommu_init_state init_state = IOMMU_START_STATE;
254
ae295142 255static int amd_iommu_enable_interrupts(void);
2c0ae172 256static int __init iommu_go_to_state(enum iommu_init_state state);
aafd8ba0 257static void init_device_table_dma(void);
3d9761e7 258
2479c631 259static bool amd_iommu_pre_enabled = true;
3ac3e5ee 260
4c232a70
BH
261bool translation_pre_enabled(struct amd_iommu *iommu)
262{
263 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
264}
daae2d25 265EXPORT_SYMBOL(translation_pre_enabled);
4c232a70
BH
266
267static void clear_translation_pre_enabled(struct amd_iommu *iommu)
268{
269 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
270}
271
272static void init_translation_status(struct amd_iommu *iommu)
273{
e881dbd5 274 u64 ctrl;
4c232a70 275
e881dbd5 276 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
4c232a70
BH
277 if (ctrl & (1<<CONTROL_IOMMU_EN))
278 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
279}
280
208ec8c9
JR
281static inline void update_last_devid(u16 devid)
282{
283 if (devid > amd_iommu_last_bdf)
284 amd_iommu_last_bdf = devid;
285}
286
c571484e
JR
287static inline unsigned long tbl_size(int entry_size)
288{
289 unsigned shift = PAGE_SHIFT +
421f909c 290 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
c571484e
JR
291
292 return 1UL << shift;
293}
294
6b9376e3
SS
295int amd_iommu_get_num_iommus(void)
296{
297 return amd_iommus_present;
298}
299
5bcd757f
MG
300/* Access to l1 and l2 indexed register spaces */
301
302static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
303{
304 u32 val;
305
306 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
307 pci_read_config_dword(iommu->dev, 0xfc, &val);
308 return val;
309}
310
311static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
312{
313 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
314 pci_write_config_dword(iommu->dev, 0xfc, val);
315 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
316}
317
318static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
319{
320 u32 val;
321
322 pci_write_config_dword(iommu->dev, 0xf0, address);
323 pci_read_config_dword(iommu->dev, 0xf4, &val);
324 return val;
325}
326
327static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
328{
329 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
330 pci_write_config_dword(iommu->dev, 0xf4, val);
331}
332
b65233a9
JR
333/****************************************************************************
334 *
335 * AMD IOMMU MMIO register space handling functions
336 *
337 * These functions are used to program the IOMMU device registers in
338 * MMIO space required for that driver.
339 *
340 ****************************************************************************/
3e8064ba 341
b65233a9
JR
342/*
343 * This function set the exclusion range in the IOMMU. DMA accesses to the
344 * exclusion range are passed through untranslated
345 */
05f92db9 346static void iommu_set_exclusion_range(struct amd_iommu *iommu)
b2026aa2
JR
347{
348 u64 start = iommu->exclusion_start & PAGE_MASK;
3c677d20 349 u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
b2026aa2
JR
350 u64 entry;
351
352 if (!iommu->exclusion_start)
353 return;
354
355 entry = start | MMIO_EXCL_ENABLE_MASK;
356 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
357 &entry, sizeof(entry));
358
359 entry = limit;
360 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
361 &entry, sizeof(entry));
362}
363
b65233a9 364/* Programs the physical address of the device table into the IOMMU hardware */
6b7f000e 365static void iommu_set_device_table(struct amd_iommu *iommu)
b2026aa2 366{
f609891f 367 u64 entry;
b2026aa2
JR
368
369 BUG_ON(iommu->mmio_base == NULL);
370
2543a786 371 entry = iommu_virt_to_phys(amd_iommu_dev_table);
b2026aa2
JR
372 entry |= (dev_table_size >> 12) - 1;
373 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
374 &entry, sizeof(entry));
375}
376
b65233a9 377/* Generic functions to enable/disable certain features of the IOMMU. */
05f92db9 378static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
b2026aa2 379{
e881dbd5 380 u64 ctrl;
b2026aa2 381
e881dbd5
SS
382 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
383 ctrl |= (1ULL << bit);
384 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
b2026aa2
JR
385}
386
ca020711 387static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
b2026aa2 388{
e881dbd5 389 u64 ctrl;
b2026aa2 390
e881dbd5
SS
391 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
392 ctrl &= ~(1ULL << bit);
393 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
b2026aa2
JR
394}
395
1456e9d2
JR
396static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
397{
e881dbd5 398 u64 ctrl;
1456e9d2 399
e881dbd5 400 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
1456e9d2
JR
401 ctrl &= ~CTRL_INV_TO_MASK;
402 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
e881dbd5 403 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
1456e9d2
JR
404}
405
b65233a9 406/* Function to enable the hardware */
05f92db9 407static void iommu_enable(struct amd_iommu *iommu)
b2026aa2 408{
b2026aa2 409 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
b2026aa2
JR
410}
411
92ac4320 412static void iommu_disable(struct amd_iommu *iommu)
126c52be 413{
3ddbe913
KM
414 if (!iommu->mmio_base)
415 return;
416
a8c485bb
CW
417 /* Disable command buffer */
418 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
419
420 /* Disable event logging and event interrupts */
421 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
422 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
423
8bda0cfb
SS
424 /* Disable IOMMU GA_LOG */
425 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
426 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
427
a8c485bb 428 /* Disable IOMMU hardware itself */
92ac4320 429 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
126c52be
JR
430}
431
b65233a9
JR
432/*
433 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
434 * the system has one.
435 */
30861ddc 436static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
6c56747b 437{
30861ddc 438 if (!request_mem_region(address, end, "amd_iommu")) {
101fa037 439 pr_err("Can not reserve memory region %llx-%llx for mmio\n",
30861ddc 440 address, end);
101fa037 441 pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
6c56747b 442 return NULL;
e82752d8 443 }
6c56747b 444
4bdc0d67 445 return (u8 __iomem *)ioremap(address, end);
6c56747b
JR
446}
447
448static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
449{
450 if (iommu->mmio_base)
451 iounmap(iommu->mmio_base);
30861ddc 452 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
6c56747b
JR
453}
454
ac7ccf67
SS
455static inline u32 get_ivhd_header_size(struct ivhd_header *h)
456{
457 u32 size = 0;
458
459 switch (h->type) {
460 case 0x10:
461 size = 24;
462 break;
463 case 0x11:
464 case 0x40:
465 size = 40;
466 break;
467 }
468 return size;
469}
470
b65233a9
JR
471/****************************************************************************
472 *
473 * The functions below belong to the first pass of AMD IOMMU ACPI table
474 * parsing. In this pass we try to find out the highest device id this
475 * code has to handle. Upon this information the size of the shared data
476 * structures is determined later.
477 *
478 ****************************************************************************/
479
b514e555
JR
480/*
481 * This function calculates the length of a given IVHD entry
482 */
483static inline int ivhd_entry_length(u8 *ivhd)
484{
8c7142f5
SS
485 u32 type = ((struct ivhd_entry *)ivhd)->type;
486
487 if (type < 0x80) {
488 return 0x04 << (*ivhd >> 6);
489 } else if (type == IVHD_DEV_ACPI_HID) {
490 /* For ACPI_HID, offset 21 is uid len */
491 return *((u8 *)ivhd + 21) + 22;
492 }
493 return 0;
b514e555
JR
494}
495
b65233a9
JR
496/*
497 * After reading the highest device id from the IOMMU PCI capability header
498 * this function looks if there is a higher device id defined in the ACPI table
499 */
3e8064ba
JR
500static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
501{
502 u8 *p = (void *)h, *end = (void *)h;
503 struct ivhd_entry *dev;
504
ac7ccf67
SS
505 u32 ivhd_size = get_ivhd_header_size(h);
506
507 if (!ivhd_size) {
101fa037 508 pr_err("Unsupported IVHD type %#x\n", h->type);
ac7ccf67
SS
509 return -EINVAL;
510 }
511
512 p += ivhd_size;
3e8064ba
JR
513 end += h->length;
514
3e8064ba
JR
515 while (p < end) {
516 dev = (struct ivhd_entry *)p;
517 switch (dev->type) {
d1259416
JR
518 case IVHD_DEV_ALL:
519 /* Use maximum BDF value for DEV_ALL */
520 update_last_devid(0xffff);
521 break;
3e8064ba
JR
522 case IVHD_DEV_SELECT:
523 case IVHD_DEV_RANGE_END:
524 case IVHD_DEV_ALIAS:
525 case IVHD_DEV_EXT_SELECT:
b65233a9 526 /* all the above subfield types refer to device ids */
208ec8c9 527 update_last_devid(dev->devid);
3e8064ba
JR
528 break;
529 default:
530 break;
531 }
b514e555 532 p += ivhd_entry_length(p);
3e8064ba
JR
533 }
534
535 WARN_ON(p != end);
536
537 return 0;
538}
539
8c7142f5
SS
540static int __init check_ivrs_checksum(struct acpi_table_header *table)
541{
542 int i;
543 u8 checksum = 0, *p = (u8 *)table;
544
545 for (i = 0; i < table->length; ++i)
546 checksum += p[i];
547 if (checksum != 0) {
548 /* ACPI table corrupt */
101fa037 549 pr_err(FW_BUG "IVRS invalid checksum\n");
8c7142f5
SS
550 return -ENODEV;
551 }
552
553 return 0;
554}
555
b65233a9
JR
556/*
557 * Iterate over all IVHD entries in the ACPI table and find the highest device
558 * id which we need to handle. This is the first of three functions which parse
559 * the ACPI table. So we check the checksum here.
560 */
3e8064ba
JR
561static int __init find_last_devid_acpi(struct acpi_table_header *table)
562{
8c7142f5 563 u8 *p = (u8 *)table, *end = (u8 *)table;
3e8064ba
JR
564 struct ivhd_header *h;
565
3e8064ba
JR
566 p += IVRS_HEADER_LENGTH;
567
568 end += table->length;
569 while (p < end) {
570 h = (struct ivhd_header *)p;
8c7142f5
SS
571 if (h->type == amd_iommu_target_ivhd_type) {
572 int ret = find_last_devid_from_ivhd(h);
573
574 if (ret)
575 return ret;
3e8064ba
JR
576 }
577 p += h->length;
578 }
579 WARN_ON(p != end);
580
581 return 0;
582}
583
b65233a9
JR
584/****************************************************************************
585 *
df805abb 586 * The following functions belong to the code path which parses the ACPI table
b65233a9
JR
587 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
588 * data structures, initialize the device/alias/rlookup table and also
589 * basically initialize the hardware.
590 *
591 ****************************************************************************/
592
593/*
594 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
595 * write commands to that buffer later and the IOMMU will execute them
596 * asynchronously
597 */
f2c2db53 598static int __init alloc_command_buffer(struct amd_iommu *iommu)
b36ca91e 599{
f2c2db53
JR
600 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
601 get_order(CMD_BUFFER_SIZE));
b36ca91e 602
f2c2db53 603 return iommu->cmd_buf ? 0 : -ENOMEM;
58492e12
JR
604}
605
93f1cc67
JR
606/*
607 * This function resets the command buffer if the IOMMU stopped fetching
608 * commands from it.
609 */
610void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
611{
612 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
613
614 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
615 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
d334a563
TL
616 iommu->cmd_buf_head = 0;
617 iommu->cmd_buf_tail = 0;
93f1cc67
JR
618
619 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
620}
621
58492e12
JR
622/*
623 * This function writes the command buffer address to the hardware and
624 * enables it.
625 */
626static void iommu_enable_command_buffer(struct amd_iommu *iommu)
627{
628 u64 entry;
629
630 BUG_ON(iommu->cmd_buf == NULL);
631
2543a786 632 entry = iommu_virt_to_phys(iommu->cmd_buf);
b36ca91e 633 entry |= MMIO_CMD_SIZE_512;
58492e12 634
b36ca91e 635 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
58492e12 636 &entry, sizeof(entry));
b36ca91e 637
93f1cc67 638 amd_iommu_reset_cmd_buffer(iommu);
b36ca91e
JR
639}
640
78d313c6
BH
641/*
642 * This function disables the command buffer
643 */
644static void iommu_disable_command_buffer(struct amd_iommu *iommu)
645{
646 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
647}
648
b36ca91e
JR
649static void __init free_command_buffer(struct amd_iommu *iommu)
650{
deba4bce 651 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
b36ca91e
JR
652}
653
335503e5 654/* allocates the memory where the IOMMU will log its events to */
f2c2db53 655static int __init alloc_event_buffer(struct amd_iommu *iommu)
335503e5 656{
f2c2db53
JR
657 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
658 get_order(EVT_BUFFER_SIZE));
335503e5 659
f2c2db53 660 return iommu->evt_buf ? 0 : -ENOMEM;
58492e12
JR
661}
662
663static void iommu_enable_event_buffer(struct amd_iommu *iommu)
664{
665 u64 entry;
666
667 BUG_ON(iommu->evt_buf == NULL);
668
2543a786 669 entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
58492e12 670
335503e5
JR
671 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
672 &entry, sizeof(entry));
673
09067207
JR
674 /* set head and tail to zero manually */
675 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
676 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
677
58492e12 678 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
335503e5
JR
679}
680
78d313c6
BH
681/*
682 * This function disables the event log buffer
683 */
684static void iommu_disable_event_buffer(struct amd_iommu *iommu)
685{
686 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
687}
688
335503e5
JR
689static void __init free_event_buffer(struct amd_iommu *iommu)
690{
691 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
692}
693
1a29ac01 694/* allocates the memory where the IOMMU will log its events to */
f2c2db53 695static int __init alloc_ppr_log(struct amd_iommu *iommu)
1a29ac01 696{
f2c2db53
JR
697 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
698 get_order(PPR_LOG_SIZE));
1a29ac01 699
f2c2db53 700 return iommu->ppr_log ? 0 : -ENOMEM;
1a29ac01
JR
701}
702
703static void iommu_enable_ppr_log(struct amd_iommu *iommu)
704{
705 u64 entry;
706
707 if (iommu->ppr_log == NULL)
708 return;
709
2543a786 710 entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
1a29ac01
JR
711
712 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
713 &entry, sizeof(entry));
714
715 /* set head and tail to zero manually */
716 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
717 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
718
bde9e6b9 719 iommu_feature_enable(iommu, CONTROL_PPRLOG_EN);
1a29ac01
JR
720 iommu_feature_enable(iommu, CONTROL_PPR_EN);
721}
722
723static void __init free_ppr_log(struct amd_iommu *iommu)
724{
725 if (iommu->ppr_log == NULL)
726 return;
727
728 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
729}
730
8bda0cfb
SS
731static void free_ga_log(struct amd_iommu *iommu)
732{
733#ifdef CONFIG_IRQ_REMAP
734 if (iommu->ga_log)
735 free_pages((unsigned long)iommu->ga_log,
736 get_order(GA_LOG_SIZE));
737 if (iommu->ga_log_tail)
738 free_pages((unsigned long)iommu->ga_log_tail,
739 get_order(8));
740#endif
741}
742
743static int iommu_ga_log_enable(struct amd_iommu *iommu)
744{
745#ifdef CONFIG_IRQ_REMAP
746 u32 status, i;
747
748 if (!iommu->ga_log)
749 return -EINVAL;
750
751 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
752
753 /* Check if already running */
754 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
755 return 0;
756
757 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
758 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
759
760 for (i = 0; i < LOOP_TIMEOUT; ++i) {
761 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
762 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
763 break;
764 }
765
766 if (i >= LOOP_TIMEOUT)
767 return -EINVAL;
768#endif /* CONFIG_IRQ_REMAP */
769 return 0;
770}
771
772#ifdef CONFIG_IRQ_REMAP
773static int iommu_init_ga_log(struct amd_iommu *iommu)
774{
775 u64 entry;
776
777 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
778 return 0;
779
780 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
781 get_order(GA_LOG_SIZE));
782 if (!iommu->ga_log)
783 goto err_out;
784
785 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
786 get_order(8));
787 if (!iommu->ga_log_tail)
788 goto err_out;
789
2543a786 790 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
8bda0cfb
SS
791 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
792 &entry, sizeof(entry));
ab99be46
FS
793 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
794 (BIT_ULL(52)-1)) & ~7ULL;
8bda0cfb
SS
795 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
796 &entry, sizeof(entry));
797 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
798 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
799
800 return 0;
801err_out:
802 free_ga_log(iommu);
803 return -EINVAL;
804}
805#endif /* CONFIG_IRQ_REMAP */
806
807static int iommu_init_ga(struct amd_iommu *iommu)
808{
809 int ret = 0;
810
811#ifdef CONFIG_IRQ_REMAP
812 /* Note: We have already checked GASup from IVRS table.
813 * Now, we need to make sure that GAMSup is set.
814 */
815 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
816 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
817 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
818
819 ret = iommu_init_ga_log(iommu);
820#endif /* CONFIG_IRQ_REMAP */
821
822 return ret;
823}
824
90fcffd9
SS
825static void iommu_enable_xt(struct amd_iommu *iommu)
826{
827#ifdef CONFIG_IRQ_REMAP
828 /*
829 * XT mode (32-bit APIC destination ID) requires
830 * GA mode (128-bit IRTE support) as a prerequisite.
831 */
832 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
833 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
834 iommu_feature_enable(iommu, CONTROL_XT_EN);
835#endif /* CONFIG_IRQ_REMAP */
836}
837
cbc33a90
JR
838static void iommu_enable_gt(struct amd_iommu *iommu)
839{
840 if (!iommu_feature(iommu, FEATURE_GT))
841 return;
842
843 iommu_feature_enable(iommu, CONTROL_GT_EN);
844}
845
b65233a9 846/* sets a specific bit in the device table entry. */
3566b778
JR
847static void set_dev_entry_bit(u16 devid, u8 bit)
848{
ee6c2868
JR
849 int i = (bit >> 6) & 0x03;
850 int _bit = bit & 0x3f;
3566b778 851
ee6c2868 852 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
3566b778
JR
853}
854
c5cca146
JR
855static int get_dev_entry_bit(u16 devid, u8 bit)
856{
ee6c2868
JR
857 int i = (bit >> 6) & 0x03;
858 int _bit = bit & 0x3f;
c5cca146 859
ee6c2868 860 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
c5cca146
JR
861}
862
863
45a01c42
BH
864static bool copy_device_table(void)
865{
ae162efb 866 u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
45a01c42
BH
867 struct dev_table_entry *old_devtb = NULL;
868 u32 lo, hi, devid, old_devtb_size;
869 phys_addr_t old_devtb_phys;
45a01c42 870 struct amd_iommu *iommu;
53019a9e 871 u16 dom_id, dte_v, irq_v;
45a01c42 872 gfp_t gfp_flag;
daae2d25 873 u64 tmp;
45a01c42 874
3ac3e5ee
BH
875 if (!amd_iommu_pre_enabled)
876 return false;
45a01c42
BH
877
878 pr_warn("Translation is already enabled - trying to copy translation structures\n");
879 for_each_iommu(iommu) {
880 /* All IOMMUs should use the same device table with the same size */
881 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
882 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
883 entry = (((u64) hi) << 32) + lo;
884 if (last_entry && last_entry != entry) {
3c6bae62 885 pr_err("IOMMU:%d should use the same dev table as others!\n",
45a01c42
BH
886 iommu->index);
887 return false;
888 }
889 last_entry = entry;
890
891 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
892 if (old_devtb_size != dev_table_size) {
3c6bae62 893 pr_err("The device table size of IOMMU:%d is not expected!\n",
45a01c42
BH
894 iommu->index);
895 return false;
896 }
897 }
898
8780158c
LJ
899 /*
900 * When SME is enabled in the first kernel, the entry includes the
901 * memory encryption mask(sme_me_mask), we must remove the memory
902 * encryption mask to obtain the true physical address in kdump kernel.
903 */
904 old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
905
b336781b 906 if (old_devtb_phys >= 0x100000000ULL) {
3c6bae62 907 pr_err("The address of old device table is above 4G, not trustworthy!\n");
b336781b
BH
908 return false;
909 }
8780158c
LJ
910 old_devtb = (sme_active() && is_kdump_kernel())
911 ? (__force void *)ioremap_encrypted(old_devtb_phys,
912 dev_table_size)
913 : memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
914
45a01c42
BH
915 if (!old_devtb)
916 return false;
917
b336781b 918 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
45a01c42
BH
919 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
920 get_order(dev_table_size));
921 if (old_dev_tbl_cpy == NULL) {
3c6bae62 922 pr_err("Failed to allocate memory for copying old device table!\n");
45a01c42
BH
923 return false;
924 }
925
926 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
927 old_dev_tbl_cpy[devid] = old_devtb[devid];
928 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
929 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
53019a9e
BH
930
931 if (dte_v && dom_id) {
932 old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
933 old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
45a01c42 934 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
daae2d25
BH
935 /* If gcr3 table existed, mask it out */
936 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
937 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
938 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
939 old_dev_tbl_cpy[devid].data[1] &= ~tmp;
940 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
941 tmp |= DTE_FLAG_GV;
942 old_dev_tbl_cpy[devid].data[0] &= ~tmp;
943 }
53019a9e
BH
944 }
945
946 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
947 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
948 int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK;
949 if (irq_v && (int_ctl || int_tab_len)) {
950 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
951 (int_tab_len != DTE_IRQ_TABLE_LEN)) {
952 pr_err("Wrong old irq remapping flag: %#x\n", devid);
953 return false;
954 }
955
956 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
957 }
45a01c42
BH
958 }
959 memunmap(old_devtb);
960
961 return true;
962}
963
c5cca146
JR
964void amd_iommu_apply_erratum_63(u16 devid)
965{
966 int sysmgt;
967
968 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
969 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
970
971 if (sysmgt == 0x01)
972 set_dev_entry_bit(devid, DEV_ENTRY_IW);
973}
974
5ff4789d
JR
975/* Writes the specific IOMMU for a device into the rlookup table */
976static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
977{
978 amd_iommu_rlookup_table[devid] = iommu;
979}
980
b65233a9
JR
981/*
982 * This function takes the device specific flags read from the ACPI
983 * table and sets up the device table entry with that information
984 */
5ff4789d
JR
985static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
986 u16 devid, u32 flags, u32 ext_flags)
3566b778
JR
987{
988 if (flags & ACPI_DEVFLAG_INITPASS)
989 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
990 if (flags & ACPI_DEVFLAG_EXTINT)
991 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
992 if (flags & ACPI_DEVFLAG_NMI)
993 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
994 if (flags & ACPI_DEVFLAG_SYSMGT1)
995 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
996 if (flags & ACPI_DEVFLAG_SYSMGT2)
997 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
998 if (flags & ACPI_DEVFLAG_LINT0)
999 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
1000 if (flags & ACPI_DEVFLAG_LINT1)
1001 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
3566b778 1002
c5cca146
JR
1003 amd_iommu_apply_erratum_63(devid);
1004
5ff4789d 1005 set_iommu_for_device(iommu, devid);
3566b778
JR
1006}
1007
93d05155 1008int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
6efed63b
JR
1009{
1010 struct devid_map *entry;
1011 struct list_head *list;
1012
31cff67f
JR
1013 if (type == IVHD_SPECIAL_IOAPIC)
1014 list = &ioapic_map;
1015 else if (type == IVHD_SPECIAL_HPET)
1016 list = &hpet_map;
1017 else
6efed63b
JR
1018 return -EINVAL;
1019
31cff67f
JR
1020 list_for_each_entry(entry, list, list) {
1021 if (!(entry->id == id && entry->cmd_line))
1022 continue;
1023
101fa037 1024 pr_info("Command-line override present for %s id %d - ignoring\n",
31cff67f
JR
1025 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1026
c50e3247
JR
1027 *devid = entry->devid;
1028
31cff67f
JR
1029 return 0;
1030 }
1031
6efed63b
JR
1032 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1033 if (!entry)
1034 return -ENOMEM;
1035
31cff67f 1036 entry->id = id;
c50e3247 1037 entry->devid = *devid;
31cff67f 1038 entry->cmd_line = cmd_line;
6efed63b
JR
1039
1040 list_add_tail(&entry->list, list);
1041
1042 return 0;
1043}
1044
2a0cb4e2
WZ
1045static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1046 bool cmd_line)
1047{
1048 struct acpihid_map_entry *entry;
1049 struct list_head *list = &acpihid_map;
1050
1051 list_for_each_entry(entry, list, list) {
1052 if (strcmp(entry->hid, hid) ||
1053 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1054 !entry->cmd_line)
1055 continue;
1056
101fa037 1057 pr_info("Command-line override for hid:%s uid:%s\n",
2a0cb4e2
WZ
1058 hid, uid);
1059 *devid = entry->devid;
1060 return 0;
1061 }
1062
1063 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1064 if (!entry)
1065 return -ENOMEM;
1066
1067 memcpy(entry->uid, uid, strlen(uid));
1068 memcpy(entry->hid, hid, strlen(hid));
1069 entry->devid = *devid;
1070 entry->cmd_line = cmd_line;
1071 entry->root_devid = (entry->devid & (~0x7));
1072
101fa037 1073 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
2a0cb4e2
WZ
1074 entry->cmd_line ? "cmd" : "ivrs",
1075 entry->hid, entry->uid, entry->root_devid);
1076
1077 list_add_tail(&entry->list, list);
1078 return 0;
1079}
1080
235dacbc
JR
1081static int __init add_early_maps(void)
1082{
1083 int i, ret;
1084
1085 for (i = 0; i < early_ioapic_map_size; ++i) {
1086 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1087 early_ioapic_map[i].id,
c50e3247 1088 &early_ioapic_map[i].devid,
235dacbc
JR
1089 early_ioapic_map[i].cmd_line);
1090 if (ret)
1091 return ret;
1092 }
1093
1094 for (i = 0; i < early_hpet_map_size; ++i) {
1095 ret = add_special_device(IVHD_SPECIAL_HPET,
1096 early_hpet_map[i].id,
c50e3247 1097 &early_hpet_map[i].devid,
235dacbc
JR
1098 early_hpet_map[i].cmd_line);
1099 if (ret)
1100 return ret;
1101 }
1102
2a0cb4e2
WZ
1103 for (i = 0; i < early_acpihid_map_size; ++i) {
1104 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1105 early_acpihid_map[i].uid,
1106 &early_acpihid_map[i].devid,
1107 early_acpihid_map[i].cmd_line);
1108 if (ret)
1109 return ret;
1110 }
1111
235dacbc
JR
1112 return 0;
1113}
1114
b65233a9 1115/*
df805abb 1116 * Reads the device exclusion range from ACPI and initializes the IOMMU with
b65233a9
JR
1117 * it
1118 */
3566b778
JR
1119static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
1120{
3566b778
JR
1121 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
1122 return;
1123
387caf0b
AH
1124 /*
1125 * Treat per-device exclusion ranges as r/w unity-mapped regions
1126 * since some buggy BIOSes might lead to the overwritten exclusion
1127 * range (exclusion_start and exclusion_length members). This
1128 * happens when there are multiple exclusion ranges (IVMD entries)
1129 * defined in ACPI table.
1130 */
1131 m->flags = (IVMD_FLAG_IW | IVMD_FLAG_IR | IVMD_FLAG_UNITY_MAP);
3566b778
JR
1132}
1133
b65233a9
JR
1134/*
1135 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1136 * initializes the hardware and our data structures with it.
1137 */
6efed63b 1138static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
5d0c8e49
JR
1139 struct ivhd_header *h)
1140{
1141 u8 *p = (u8 *)h;
1142 u8 *end = p, flags = 0;
0de66d5b
JR
1143 u16 devid = 0, devid_start = 0, devid_to = 0;
1144 u32 dev_i, ext_flags = 0;
58a3bee5 1145 bool alias = false;
5d0c8e49 1146 struct ivhd_entry *e;
ac7ccf67 1147 u32 ivhd_size;
235dacbc
JR
1148 int ret;
1149
1150
1151 ret = add_early_maps();
1152 if (ret)
1153 return ret;
5d0c8e49 1154
93d05155
KHF
1155 amd_iommu_apply_ivrs_quirks();
1156
5d0c8e49 1157 /*
e9bf5197 1158 * First save the recommended feature enable bits from ACPI
5d0c8e49 1159 */
e9bf5197 1160 iommu->acpi_flags = h->flags;
5d0c8e49
JR
1161
1162 /*
1163 * Done. Now parse the device entries
1164 */
ac7ccf67
SS
1165 ivhd_size = get_ivhd_header_size(h);
1166 if (!ivhd_size) {
101fa037 1167 pr_err("Unsupported IVHD type %#x\n", h->type);
ac7ccf67
SS
1168 return -EINVAL;
1169 }
1170
1171 p += ivhd_size;
1172
5d0c8e49
JR
1173 end += h->length;
1174
42a698f4 1175
5d0c8e49
JR
1176 while (p < end) {
1177 e = (struct ivhd_entry *)p;
1178 switch (e->type) {
1179 case IVHD_DEV_ALL:
42a698f4 1180
226e889b 1181 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
42a698f4 1182
226e889b
JR
1183 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1184 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
5d0c8e49
JR
1185 break;
1186 case IVHD_DEV_SELECT:
42a698f4
JR
1187
1188 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1189 "flags: %02x\n",
c5081cd7 1190 PCI_BUS_NUM(e->devid),
42a698f4
JR
1191 PCI_SLOT(e->devid),
1192 PCI_FUNC(e->devid),
1193 e->flags);
1194
5d0c8e49 1195 devid = e->devid;
5ff4789d 1196 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
5d0c8e49
JR
1197 break;
1198 case IVHD_DEV_SELECT_RANGE_START:
42a698f4
JR
1199
1200 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1201 "devid: %02x:%02x.%x flags: %02x\n",
c5081cd7 1202 PCI_BUS_NUM(e->devid),
42a698f4
JR
1203 PCI_SLOT(e->devid),
1204 PCI_FUNC(e->devid),
1205 e->flags);
1206
5d0c8e49
JR
1207 devid_start = e->devid;
1208 flags = e->flags;
1209 ext_flags = 0;
58a3bee5 1210 alias = false;
5d0c8e49
JR
1211 break;
1212 case IVHD_DEV_ALIAS:
42a698f4
JR
1213
1214 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1215 "flags: %02x devid_to: %02x:%02x.%x\n",
c5081cd7 1216 PCI_BUS_NUM(e->devid),
42a698f4
JR
1217 PCI_SLOT(e->devid),
1218 PCI_FUNC(e->devid),
1219 e->flags,
c5081cd7 1220 PCI_BUS_NUM(e->ext >> 8),
42a698f4
JR
1221 PCI_SLOT(e->ext >> 8),
1222 PCI_FUNC(e->ext >> 8));
1223
5d0c8e49
JR
1224 devid = e->devid;
1225 devid_to = e->ext >> 8;
7a6a3a08 1226 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
7455aab1 1227 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
5d0c8e49
JR
1228 amd_iommu_alias_table[devid] = devid_to;
1229 break;
1230 case IVHD_DEV_ALIAS_RANGE:
42a698f4
JR
1231
1232 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1233 "devid: %02x:%02x.%x flags: %02x "
1234 "devid_to: %02x:%02x.%x\n",
c5081cd7 1235 PCI_BUS_NUM(e->devid),
42a698f4
JR
1236 PCI_SLOT(e->devid),
1237 PCI_FUNC(e->devid),
1238 e->flags,
c5081cd7 1239 PCI_BUS_NUM(e->ext >> 8),
42a698f4
JR
1240 PCI_SLOT(e->ext >> 8),
1241 PCI_FUNC(e->ext >> 8));
1242
5d0c8e49
JR
1243 devid_start = e->devid;
1244 flags = e->flags;
1245 devid_to = e->ext >> 8;
1246 ext_flags = 0;
58a3bee5 1247 alias = true;
5d0c8e49
JR
1248 break;
1249 case IVHD_DEV_EXT_SELECT:
42a698f4
JR
1250
1251 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1252 "flags: %02x ext: %08x\n",
c5081cd7 1253 PCI_BUS_NUM(e->devid),
42a698f4
JR
1254 PCI_SLOT(e->devid),
1255 PCI_FUNC(e->devid),
1256 e->flags, e->ext);
1257
5d0c8e49 1258 devid = e->devid;
5ff4789d
JR
1259 set_dev_entry_from_acpi(iommu, devid, e->flags,
1260 e->ext);
5d0c8e49
JR
1261 break;
1262 case IVHD_DEV_EXT_SELECT_RANGE:
42a698f4
JR
1263
1264 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1265 "%02x:%02x.%x flags: %02x ext: %08x\n",
c5081cd7 1266 PCI_BUS_NUM(e->devid),
42a698f4
JR
1267 PCI_SLOT(e->devid),
1268 PCI_FUNC(e->devid),
1269 e->flags, e->ext);
1270
5d0c8e49
JR
1271 devid_start = e->devid;
1272 flags = e->flags;
1273 ext_flags = e->ext;
58a3bee5 1274 alias = false;
5d0c8e49
JR
1275 break;
1276 case IVHD_DEV_RANGE_END:
42a698f4
JR
1277
1278 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
c5081cd7 1279 PCI_BUS_NUM(e->devid),
42a698f4
JR
1280 PCI_SLOT(e->devid),
1281 PCI_FUNC(e->devid));
1282
5d0c8e49
JR
1283 devid = e->devid;
1284 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
7a6a3a08 1285 if (alias) {
5d0c8e49 1286 amd_iommu_alias_table[dev_i] = devid_to;
7a6a3a08
JR
1287 set_dev_entry_from_acpi(iommu,
1288 devid_to, flags, ext_flags);
1289 }
1290 set_dev_entry_from_acpi(iommu, dev_i,
1291 flags, ext_flags);
5d0c8e49
JR
1292 }
1293 break;
6efed63b
JR
1294 case IVHD_DEV_SPECIAL: {
1295 u8 handle, type;
1296 const char *var;
1297 u16 devid;
1298 int ret;
1299
1300 handle = e->ext & 0xff;
1301 devid = (e->ext >> 8) & 0xffff;
1302 type = (e->ext >> 24) & 0xff;
1303
1304 if (type == IVHD_SPECIAL_IOAPIC)
1305 var = "IOAPIC";
1306 else if (type == IVHD_SPECIAL_HPET)
1307 var = "HPET";
1308 else
1309 var = "UNKNOWN";
1310
1311 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1312 var, (int)handle,
c5081cd7 1313 PCI_BUS_NUM(devid),
6efed63b
JR
1314 PCI_SLOT(devid),
1315 PCI_FUNC(devid));
1316
c50e3247 1317 ret = add_special_device(type, handle, &devid, false);
6efed63b
JR
1318 if (ret)
1319 return ret;
c50e3247
JR
1320
1321 /*
1322 * add_special_device might update the devid in case a
1323 * command-line override is present. So call
1324 * set_dev_entry_from_acpi after add_special_device.
1325 */
1326 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1327
6efed63b
JR
1328 break;
1329 }
2a0cb4e2
WZ
1330 case IVHD_DEV_ACPI_HID: {
1331 u16 devid;
e461b8c9
AM
1332 u8 hid[ACPIHID_HID_LEN];
1333 u8 uid[ACPIHID_UID_LEN];
2a0cb4e2
WZ
1334 int ret;
1335
1336 if (h->type != 0x40) {
1337 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1338 e->type);
1339 break;
1340 }
1341
1342 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1343 hid[ACPIHID_HID_LEN - 1] = '\0';
1344
1345 if (!(*hid)) {
1346 pr_err(FW_BUG "Invalid HID.\n");
1347 break;
1348 }
1349
e461b8c9 1350 uid[0] = '\0';
2a0cb4e2
WZ
1351 switch (e->uidf) {
1352 case UID_NOT_PRESENT:
1353
1354 if (e->uidl != 0)
1355 pr_warn(FW_BUG "Invalid UID length.\n");
1356
1357 break;
1358 case UID_IS_INTEGER:
1359
1360 sprintf(uid, "%d", e->uid);
1361
1362 break;
1363 case UID_IS_CHARACTER:
1364
e461b8c9
AM
1365 memcpy(uid, &e->uid, e->uidl);
1366 uid[e->uidl] = '\0';
2a0cb4e2
WZ
1367
1368 break;
1369 default:
1370 break;
1371 }
1372
6082ee72 1373 devid = e->devid;
2a0cb4e2
WZ
1374 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1375 hid, uid,
1376 PCI_BUS_NUM(devid),
1377 PCI_SLOT(devid),
1378 PCI_FUNC(devid));
1379
2a0cb4e2
WZ
1380 flags = e->flags;
1381
1382 ret = add_acpi_hid_device(hid, uid, &devid, false);
1383 if (ret)
1384 return ret;
1385
1386 /*
1387 * add_special_device might update the devid in case a
1388 * command-line override is present. So call
1389 * set_dev_entry_from_acpi after add_special_device.
1390 */
1391 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1392
1393 break;
1394 }
5d0c8e49
JR
1395 default:
1396 break;
1397 }
1398
b514e555 1399 p += ivhd_entry_length(p);
5d0c8e49 1400 }
6efed63b
JR
1401
1402 return 0;
5d0c8e49
JR
1403}
1404
e47d402d
JR
1405static void __init free_iommu_one(struct amd_iommu *iommu)
1406{
1407 free_command_buffer(iommu);
335503e5 1408 free_event_buffer(iommu);
1a29ac01 1409 free_ppr_log(iommu);
8bda0cfb 1410 free_ga_log(iommu);
e47d402d
JR
1411 iommu_unmap_mmio_space(iommu);
1412}
1413
1414static void __init free_iommu_all(void)
1415{
1416 struct amd_iommu *iommu, *next;
1417
3bd22172 1418 for_each_iommu_safe(iommu, next) {
e47d402d
JR
1419 list_del(&iommu->list);
1420 free_iommu_one(iommu);
1421 kfree(iommu);
1422 }
1423}
1424
318fe782
SS
1425/*
1426 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1427 * Workaround:
1428 * BIOS should disable L2B micellaneous clock gating by setting
1429 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1430 */
e2f1a3bd 1431static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
318fe782
SS
1432{
1433 u32 value;
1434
1435 if ((boot_cpu_data.x86 != 0x15) ||
1436 (boot_cpu_data.x86_model < 0x10) ||
1437 (boot_cpu_data.x86_model > 0x1f))
1438 return;
1439
1440 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1441 pci_read_config_dword(iommu->dev, 0xf4, &value);
1442
1443 if (value & BIT(2))
1444 return;
1445
1446 /* Select NB indirect register 0x90 and enable writing */
1447 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1448
1449 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
5f226da1 1450 pci_info(iommu->dev, "Applying erratum 746 workaround\n");
318fe782
SS
1451
1452 /* Clear the enable writing bit */
1453 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1454}
1455
358875fd
JC
1456/*
1457 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1458 * Workaround:
1459 * BIOS should enable ATS write permission check by setting
1460 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1461 */
1462static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1463{
1464 u32 value;
1465
1466 if ((boot_cpu_data.x86 != 0x15) ||
1467 (boot_cpu_data.x86_model < 0x30) ||
1468 (boot_cpu_data.x86_model > 0x3f))
1469 return;
1470
1471 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1472 value = iommu_read_l2(iommu, 0x47);
1473
1474 if (value & BIT(0))
1475 return;
1476
1477 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1478 iommu_write_l2(iommu, 0x47, value | BIT(0));
1479
5f226da1 1480 pci_info(iommu->dev, "Applying ATS write check workaround\n");
358875fd
JC
1481}
1482
b65233a9
JR
1483/*
1484 * This function clues the initialization function for one IOMMU
1485 * together and also allocates the command buffer and programs the
1486 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1487 */
e47d402d
JR
1488static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1489{
6efed63b
JR
1490 int ret;
1491
27790398 1492 raw_spin_lock_init(&iommu->lock);
bb52777e
JR
1493
1494 /* Add IOMMU to internal data structures */
e47d402d 1495 list_add_tail(&iommu->list, &amd_iommu_list);
6b9376e3 1496 iommu->index = amd_iommus_present++;
bb52777e
JR
1497
1498 if (unlikely(iommu->index >= MAX_IOMMUS)) {
101fa037 1499 WARN(1, "System has more IOMMUs than supported by this driver\n");
bb52777e
JR
1500 return -ENOSYS;
1501 }
1502
1503 /* Index is fine - add IOMMU to the array */
1504 amd_iommus[iommu->index] = iommu;
e47d402d
JR
1505
1506 /*
1507 * Copy data from ACPI table entry to the iommu struct
1508 */
23c742db 1509 iommu->devid = h->devid;
e47d402d 1510 iommu->cap_ptr = h->cap_ptr;
ee893c24 1511 iommu->pci_seg = h->pci_seg;
e47d402d 1512 iommu->mmio_phys = h->mmio_phys;
30861ddc 1513
7d7d38af
SS
1514 switch (h->type) {
1515 case 0x10:
1516 /* Check if IVHD EFR contains proper max banks/counters */
1517 if ((h->efr_attr != 0) &&
1518 ((h->efr_attr & (0xF << 13)) != 0) &&
1519 ((h->efr_attr & (0x3F << 17)) != 0))
1520 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1521 else
1522 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
3928aa3f
SS
1523 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1524 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
7d7d38af
SS
1525 break;
1526 case 0x11:
1527 case 0x40:
1528 if (h->efr_reg & (1 << 9))
1529 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1530 else
1531 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
3928aa3f
SS
1532 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1533 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
81307143
SS
1534 /*
1535 * Note: Since iommu_update_intcapxt() leverages
1536 * the IOMMU MMIO access to MSI capability block registers
1537 * for MSI address lo/hi/data, we need to check both
1538 * EFR[XtSup] and EFR[MsiCapMmioSup] for x2APIC support.
1539 */
1540 if ((h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT)) &&
1541 (h->efr_reg & BIT(IOMMU_EFR_MSICAPMMIOSUP_SHIFT)))
1542 amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
7d7d38af
SS
1543 break;
1544 default:
1545 return -EINVAL;
30861ddc
SK
1546 }
1547
1548 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1549 iommu->mmio_phys_end);
e47d402d
JR
1550 if (!iommu->mmio_base)
1551 return -ENOMEM;
1552
f2c2db53 1553 if (alloc_command_buffer(iommu))
e47d402d
JR
1554 return -ENOMEM;
1555
f2c2db53 1556 if (alloc_event_buffer(iommu))
335503e5
JR
1557 return -ENOMEM;
1558
a80dc3e0
JR
1559 iommu->int_enabled = false;
1560
4c232a70 1561 init_translation_status(iommu);
3ac3e5ee
BH
1562 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1563 iommu_disable(iommu);
1564 clear_translation_pre_enabled(iommu);
1565 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1566 iommu->index);
1567 }
1568 if (amd_iommu_pre_enabled)
1569 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
4c232a70 1570
6efed63b
JR
1571 ret = init_iommu_from_acpi(iommu, h);
1572 if (ret)
1573 return ret;
f6fec00a 1574
7c71d306
JL
1575 ret = amd_iommu_create_irq_domain(iommu);
1576 if (ret)
1577 return ret;
1578
f6fec00a
JR
1579 /*
1580 * Make sure IOMMU is not considered to translate itself. The IVRS
1581 * table tells us so, but this is a lie!
1582 */
1583 amd_iommu_rlookup_table[iommu->devid] = NULL;
1584
23c742db 1585 return 0;
e47d402d
JR
1586}
1587
8c7142f5
SS
1588/**
1589 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1590 * @ivrs Pointer to the IVRS header
1591 *
1592 * This function search through all IVDB of the maximum supported IVHD
1593 */
1594static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1595{
1596 u8 *base = (u8 *)ivrs;
1597 struct ivhd_header *ivhd = (struct ivhd_header *)
1598 (base + IVRS_HEADER_LENGTH);
1599 u8 last_type = ivhd->type;
1600 u16 devid = ivhd->devid;
1601
1602 while (((u8 *)ivhd - base < ivrs->length) &&
1603 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1604 u8 *p = (u8 *) ivhd;
1605
1606 if (ivhd->devid == devid)
1607 last_type = ivhd->type;
1608 ivhd = (struct ivhd_header *)(p + ivhd->length);
1609 }
1610
1611 return last_type;
1612}
1613
b65233a9
JR
1614/*
1615 * Iterates over all IOMMU entries in the ACPI table, allocates the
1616 * IOMMU structure and initializes it with init_iommu_one()
1617 */
e47d402d
JR
1618static int __init init_iommu_all(struct acpi_table_header *table)
1619{
1620 u8 *p = (u8 *)table, *end = (u8 *)table;
1621 struct ivhd_header *h;
1622 struct amd_iommu *iommu;
1623 int ret;
1624
e47d402d
JR
1625 end += table->length;
1626 p += IVRS_HEADER_LENGTH;
1627
1628 while (p < end) {
1629 h = (struct ivhd_header *)p;
8c7142f5 1630 if (*p == amd_iommu_target_ivhd_type) {
9c72041f 1631
ae908c22 1632 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
9c72041f 1633 "seg: %d flags: %01x info %04x\n",
c5081cd7 1634 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
9c72041f
JR
1635 PCI_FUNC(h->devid), h->cap_ptr,
1636 h->pci_seg, h->flags, h->info);
1637 DUMP_printk(" mmio-addr: %016llx\n",
1638 h->mmio_phys);
1639
e47d402d 1640 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
02f3b3f5
JR
1641 if (iommu == NULL)
1642 return -ENOMEM;
3551a708 1643
e47d402d 1644 ret = init_iommu_one(iommu, h);
02f3b3f5
JR
1645 if (ret)
1646 return ret;
e47d402d
JR
1647 }
1648 p += h->length;
1649
1650 }
1651 WARN_ON(p != end);
1652
1653 return 0;
1654}
1655
1650dfd1
SS
1656static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1657 u8 fxn, u64 *value, bool is_write);
30861ddc
SK
1658
1659static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1660{
5f226da1 1661 struct pci_dev *pdev = iommu->dev;
8c17bbf6 1662 u64 val = 0xabcd, val2 = 0, save_reg = 0;
30861ddc
SK
1663
1664 if (!iommu_feature(iommu, FEATURE_PC))
1665 return;
1666
1667 amd_iommu_pc_present = true;
1668
8c17bbf6
SK
1669 /* save the value to restore, if writable */
1670 if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, false))
1671 goto pc_false;
1672
30861ddc 1673 /* Check if the performance counters can be written to */
1650dfd1
SS
1674 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1675 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
8c17bbf6
SK
1676 (val != val2))
1677 goto pc_false;
1678
1679 /* restore */
1680 if (iommu_pc_get_set_reg(iommu, 0, 0, 0, &save_reg, true))
1681 goto pc_false;
30861ddc 1682
5f226da1 1683 pci_info(pdev, "IOMMU performance counters supported\n");
30861ddc
SK
1684
1685 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1686 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1687 iommu->max_counters = (u8) ((val >> 7) & 0xf);
8c17bbf6
SK
1688
1689 return;
1690
1691pc_false:
1692 pci_err(pdev, "Unable to read/write to IOMMU perf counter.\n");
1693 amd_iommu_pc_present = false;
1694 return;
30861ddc
SK
1695}
1696
066f2e98
AW
1697static ssize_t amd_iommu_show_cap(struct device *dev,
1698 struct device_attribute *attr,
1699 char *buf)
1700{
b7a42b9d 1701 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
066f2e98
AW
1702 return sprintf(buf, "%x\n", iommu->cap);
1703}
1704static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1705
1706static ssize_t amd_iommu_show_features(struct device *dev,
1707 struct device_attribute *attr,
1708 char *buf)
1709{
b7a42b9d 1710 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
066f2e98
AW
1711 return sprintf(buf, "%llx\n", iommu->features);
1712}
1713static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1714
1715static struct attribute *amd_iommu_attrs[] = {
1716 &dev_attr_cap.attr,
1717 &dev_attr_features.attr,
1718 NULL,
1719};
1720
1721static struct attribute_group amd_iommu_group = {
1722 .name = "amd-iommu",
1723 .attrs = amd_iommu_attrs,
1724};
1725
1726static const struct attribute_group *amd_iommu_groups[] = {
1727 &amd_iommu_group,
1728 NULL,
1729};
30861ddc 1730
24d2c521 1731static int __init iommu_init_pci(struct amd_iommu *iommu)
23c742db
JR
1732{
1733 int cap_ptr = iommu->cap_ptr;
8bda0cfb 1734 int ret;
23c742db 1735
d5bf0f4f
SK
1736 iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
1737 iommu->devid & 0xff);
23c742db
JR
1738 if (!iommu->dev)
1739 return -ENODEV;
1740
cbbc00be
JL
1741 /* Prevent binding other PCI device drivers to IOMMU devices */
1742 iommu->dev->match_driver = false;
1743
23c742db
JR
1744 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1745 &iommu->cap);
23c742db 1746
23c742db
JR
1747 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1748 amd_iommu_iotlb_sup = false;
1749
1750 /* read extended feature bits */
62dcee71 1751 iommu->features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
23c742db
JR
1752
1753 if (iommu_feature(iommu, FEATURE_GT)) {
1754 int glxval;
a919a018
SS
1755 u32 max_pasid;
1756 u64 pasmax;
23c742db 1757
a919a018
SS
1758 pasmax = iommu->features & FEATURE_PASID_MASK;
1759 pasmax >>= FEATURE_PASID_SHIFT;
1760 max_pasid = (1 << (pasmax + 1)) - 1;
23c742db 1761
a919a018
SS
1762 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1763
1764 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
23c742db
JR
1765
1766 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1767 glxval >>= FEATURE_GLXVAL_SHIFT;
1768
1769 if (amd_iommu_max_glx_val == -1)
1770 amd_iommu_max_glx_val = glxval;
1771 else
1772 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1773 }
1774
1775 if (iommu_feature(iommu, FEATURE_GT) &&
1776 iommu_feature(iommu, FEATURE_PPR)) {
1777 iommu->is_iommu_v2 = true;
1778 amd_iommu_v2_present = true;
1779 }
1780
f2c2db53
JR
1781 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1782 return -ENOMEM;
23c742db 1783
8bda0cfb
SS
1784 ret = iommu_init_ga(iommu);
1785 if (ret)
1786 return ret;
3928aa3f 1787
23c742db
JR
1788 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1789 amd_iommu_np_cache = true;
1790
30861ddc
SK
1791 init_iommu_perf_ctr(iommu);
1792
23c742db
JR
1793 if (is_rd890_iommu(iommu->dev)) {
1794 int i, j;
1795
d5bf0f4f
SK
1796 iommu->root_pdev =
1797 pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
1798 PCI_DEVFN(0, 0));
23c742db
JR
1799
1800 /*
1801 * Some rd890 systems may not be fully reconfigured by the
1802 * BIOS, so it's necessary for us to store this information so
1803 * it can be reprogrammed on resume
1804 */
1805 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1806 &iommu->stored_addr_lo);
1807 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1808 &iommu->stored_addr_hi);
1809
1810 /* Low bit locks writes to configuration space */
1811 iommu->stored_addr_lo &= ~1;
1812
1813 for (i = 0; i < 6; i++)
1814 for (j = 0; j < 0x12; j++)
1815 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1816
1817 for (i = 0; i < 0x83; i++)
1818 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1819 }
1820
318fe782 1821 amd_iommu_erratum_746_workaround(iommu);
358875fd 1822 amd_iommu_ats_write_check_workaround(iommu);
318fe782 1823
39ab9555
JR
1824 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1825 amd_iommu_groups, "ivhd%d", iommu->index);
b0119e87
JR
1826 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1827 iommu_device_register(&iommu->iommu);
066f2e98 1828
23c742db
JR
1829 return pci_enable_device(iommu->dev);
1830}
1831
4d121c32
JR
1832static void print_iommu_info(void)
1833{
1834 static const char * const feat_str[] = {
1835 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1836 "IA", "GA", "HE", "PC"
1837 };
1838 struct amd_iommu *iommu;
1839
1840 for_each_iommu(iommu) {
5f226da1 1841 struct pci_dev *pdev = iommu->dev;
4d121c32
JR
1842 int i;
1843
5f226da1 1844 pci_info(pdev, "Found IOMMU cap 0x%hx\n", iommu->cap_ptr);
4d121c32
JR
1845
1846 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
5f226da1
BH
1847 pci_info(pdev, "Extended features (%#llx):\n",
1848 iommu->features);
2bd5ed00 1849 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
4d121c32
JR
1850 if (iommu_feature(iommu, (1ULL << i)))
1851 pr_cont(" %s", feat_str[i]);
1852 }
3928aa3f
SS
1853
1854 if (iommu->features & FEATURE_GAM_VAPIC)
1855 pr_cont(" GA_vAPIC");
1856
30861ddc 1857 pr_cont("\n");
500c25ed 1858 }
4d121c32 1859 }
3928aa3f 1860 if (irq_remapping_enabled) {
101fa037 1861 pr_info("Interrupt remapping enabled\n");
3928aa3f 1862 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
101fa037 1863 pr_info("Virtual APIC enabled\n");
90fcffd9 1864 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
101fa037 1865 pr_info("X2APIC enabled\n");
3928aa3f 1866 }
4d121c32
JR
1867}
1868
2c0ae172 1869static int __init amd_iommu_init_pci(void)
23c742db
JR
1870{
1871 struct amd_iommu *iommu;
1872 int ret = 0;
1873
1874 for_each_iommu(iommu) {
1875 ret = iommu_init_pci(iommu);
1876 if (ret)
1877 break;
1878 }
1879
522e5cb7
JR
1880 /*
1881 * Order is important here to make sure any unity map requirements are
1882 * fulfilled. The unity mappings are created and written to the device
1883 * table during the amd_iommu_init_api() call.
1884 *
1885 * After that we call init_device_table_dma() to make sure any
1886 * uninitialized DTE will block DMA, and in the end we flush the caches
1887 * of all IOMMUs to make sure the changes to the device table are
1888 * active.
1889 */
1890 ret = amd_iommu_init_api();
1891
aafd8ba0
JR
1892 init_device_table_dma();
1893
1894 for_each_iommu(iommu)
1895 iommu_flush_all_caches(iommu);
1896
3a18404c
JR
1897 if (!ret)
1898 print_iommu_info();
4d121c32 1899
23c742db
JR
1900 return ret;
1901}
1902
a80dc3e0
JR
1903/****************************************************************************
1904 *
1905 * The following functions initialize the MSI interrupts for all IOMMUs
df805abb 1906 * in the system. It's a bit challenging because there could be multiple
a80dc3e0
JR
1907 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1908 * pci_dev.
1909 *
1910 ****************************************************************************/
1911
9f800de3 1912static int iommu_setup_msi(struct amd_iommu *iommu)
a80dc3e0
JR
1913{
1914 int r;
a80dc3e0 1915
9ddd592a
JR
1916 r = pci_enable_msi(iommu->dev);
1917 if (r)
1918 return r;
a80dc3e0 1919
72fe00f0
JR
1920 r = request_threaded_irq(iommu->dev->irq,
1921 amd_iommu_int_handler,
1922 amd_iommu_int_thread,
1923 0, "AMD-Vi",
3f398bc7 1924 iommu);
a80dc3e0
JR
1925
1926 if (r) {
1927 pci_disable_msi(iommu->dev);
9ddd592a 1928 return r;
a80dc3e0
JR
1929 }
1930
fab6afa3 1931 iommu->int_enabled = true;
1a29ac01 1932
a80dc3e0
JR
1933 return 0;
1934}
1935
66929812
SS
1936#define XT_INT_DEST_MODE(x) (((x) & 0x1ULL) << 2)
1937#define XT_INT_DEST_LO(x) (((x) & 0xFFFFFFULL) << 8)
1938#define XT_INT_VEC(x) (((x) & 0xFFULL) << 32)
1939#define XT_INT_DEST_HI(x) ((((x) >> 24) & 0xFFULL) << 56)
1940
1941/**
1942 * Setup the IntCapXT registers with interrupt routing information
1943 * based on the PCI MSI capability block registers, accessed via
1944 * MMIO MSI address low/hi and MSI data registers.
1945 */
1946static void iommu_update_intcapxt(struct amd_iommu *iommu)
1947{
1948 u64 val;
1949 u32 addr_lo = readl(iommu->mmio_base + MMIO_MSI_ADDR_LO_OFFSET);
1950 u32 addr_hi = readl(iommu->mmio_base + MMIO_MSI_ADDR_HI_OFFSET);
1951 u32 data = readl(iommu->mmio_base + MMIO_MSI_DATA_OFFSET);
1952 bool dm = (addr_lo >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
1953 u32 dest = ((addr_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xFF);
1954
1955 if (x2apic_enabled())
1956 dest |= MSI_ADDR_EXT_DEST_ID(addr_hi);
1957
1958 val = XT_INT_VEC(data & 0xFF) |
1959 XT_INT_DEST_MODE(dm) |
1960 XT_INT_DEST_LO(dest) |
1961 XT_INT_DEST_HI(dest);
1962
1963 /**
1964 * Current IOMMU implemtation uses the same IRQ for all
1965 * 3 IOMMU interrupts.
1966 */
1967 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
1968 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
1969 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
1970}
1971
1972static void _irq_notifier_notify(struct irq_affinity_notify *notify,
1973 const cpumask_t *mask)
1974{
1975 struct amd_iommu *iommu;
1976
1977 for_each_iommu(iommu) {
1978 if (iommu->dev->irq == notify->irq) {
1979 iommu_update_intcapxt(iommu);
1980 break;
1981 }
1982 }
1983}
1984
1985static void _irq_notifier_release(struct kref *ref)
1986{
1987}
1988
1989static int iommu_init_intcapxt(struct amd_iommu *iommu)
1990{
1991 int ret;
1992 struct irq_affinity_notify *notify = &iommu->intcapxt_notify;
1993
1994 /**
81307143
SS
1995 * IntCapXT requires XTSup=1 and MsiCapMmioSup=1,
1996 * which can be inferred from amd_iommu_xt_mode.
66929812
SS
1997 */
1998 if (amd_iommu_xt_mode != IRQ_REMAP_X2APIC_MODE)
1999 return 0;
2000
2001 /**
2002 * Also, we need to setup notifier to update the IntCapXT registers
2003 * whenever the irq affinity is changed from user-space.
2004 */
2005 notify->irq = iommu->dev->irq;
2006 notify->notify = _irq_notifier_notify,
2007 notify->release = _irq_notifier_release,
2008 ret = irq_set_affinity_notifier(iommu->dev->irq, notify);
2009 if (ret) {
2010 pr_err("Failed to register irq affinity notifier (devid=%#x, irq %d)\n",
2011 iommu->devid, iommu->dev->irq);
2012 return ret;
2013 }
2014
2015 iommu_update_intcapxt(iommu);
2016 iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
2017 return ret;
2018}
2019
05f92db9 2020static int iommu_init_msi(struct amd_iommu *iommu)
a80dc3e0 2021{
9ddd592a
JR
2022 int ret;
2023
a80dc3e0 2024 if (iommu->int_enabled)
9ddd592a 2025 goto enable_faults;
a80dc3e0 2026
82fcfc67 2027 if (iommu->dev->msi_cap)
9ddd592a
JR
2028 ret = iommu_setup_msi(iommu);
2029 else
2030 ret = -ENODEV;
2031
2032 if (ret)
2033 return ret;
a80dc3e0 2034
9ddd592a 2035enable_faults:
66929812
SS
2036 ret = iommu_init_intcapxt(iommu);
2037 if (ret)
2038 return ret;
2039
9ddd592a 2040 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
a80dc3e0 2041
9ddd592a 2042 if (iommu->ppr_log != NULL)
bde9e6b9 2043 iommu_feature_enable(iommu, CONTROL_PPRINT_EN);
9ddd592a 2044
8bda0cfb
SS
2045 iommu_ga_log_enable(iommu);
2046
9ddd592a 2047 return 0;
a80dc3e0
JR
2048}
2049
b65233a9
JR
2050/****************************************************************************
2051 *
2052 * The next functions belong to the third pass of parsing the ACPI
2053 * table. In this last pass the memory mapping requirements are
df805abb 2054 * gathered (like exclusion and unity mapping ranges).
b65233a9
JR
2055 *
2056 ****************************************************************************/
2057
be2a022c
JR
2058static void __init free_unity_maps(void)
2059{
2060 struct unity_map_entry *entry, *next;
2061
2062 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
2063 list_del(&entry->list);
2064 kfree(entry);
2065 }
2066}
2067
b65233a9 2068/* called when we find an exclusion range definition in ACPI */
be2a022c
JR
2069static int __init init_exclusion_range(struct ivmd_header *m)
2070{
2071 int i;
2072
2073 switch (m->type) {
2074 case ACPI_IVMD_TYPE:
2075 set_device_exclusion_range(m->devid, m);
2076 break;
2077 case ACPI_IVMD_TYPE_ALL:
3a61ec38 2078 for (i = 0; i <= amd_iommu_last_bdf; ++i)
be2a022c
JR
2079 set_device_exclusion_range(i, m);
2080 break;
2081 case ACPI_IVMD_TYPE_RANGE:
2082 for (i = m->devid; i <= m->aux; ++i)
2083 set_device_exclusion_range(i, m);
2084 break;
2085 default:
2086 break;
2087 }
2088
2089 return 0;
2090}
2091
b65233a9 2092/* called for unity map ACPI definition */
be2a022c
JR
2093static int __init init_unity_map_range(struct ivmd_header *m)
2094{
98f1ad25 2095 struct unity_map_entry *e = NULL;
02acc43a 2096 char *s;
be2a022c
JR
2097
2098 e = kzalloc(sizeof(*e), GFP_KERNEL);
2099 if (e == NULL)
2100 return -ENOMEM;
2101
8aafaaf2
JR
2102 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2103 init_exclusion_range(m);
2104
be2a022c
JR
2105 switch (m->type) {
2106 default:
0bc252f4
JR
2107 kfree(e);
2108 return 0;
be2a022c 2109 case ACPI_IVMD_TYPE:
02acc43a 2110 s = "IVMD_TYPEi\t\t\t";
be2a022c
JR
2111 e->devid_start = e->devid_end = m->devid;
2112 break;
2113 case ACPI_IVMD_TYPE_ALL:
02acc43a 2114 s = "IVMD_TYPE_ALL\t\t";
be2a022c
JR
2115 e->devid_start = 0;
2116 e->devid_end = amd_iommu_last_bdf;
2117 break;
2118 case ACPI_IVMD_TYPE_RANGE:
02acc43a 2119 s = "IVMD_TYPE_RANGE\t\t";
be2a022c
JR
2120 e->devid_start = m->devid;
2121 e->devid_end = m->aux;
2122 break;
2123 }
2124 e->address_start = PAGE_ALIGN(m->range_start);
2125 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2126 e->prot = m->flags >> 1;
2127
02acc43a
JR
2128 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2129 " range_start: %016llx range_end: %016llx flags: %x\n", s,
c5081cd7
SK
2130 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2131 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
02acc43a
JR
2132 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2133 e->address_start, e->address_end, m->flags);
2134
be2a022c
JR
2135 list_add_tail(&e->list, &amd_iommu_unity_map);
2136
2137 return 0;
2138}
2139
b65233a9 2140/* iterates over all memory definitions we find in the ACPI table */
be2a022c
JR
2141static int __init init_memory_definitions(struct acpi_table_header *table)
2142{
2143 u8 *p = (u8 *)table, *end = (u8 *)table;
2144 struct ivmd_header *m;
2145
be2a022c
JR
2146 end += table->length;
2147 p += IVRS_HEADER_LENGTH;
2148
2149 while (p < end) {
2150 m = (struct ivmd_header *)p;
8aafaaf2 2151 if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
be2a022c
JR
2152 init_unity_map_range(m);
2153
2154 p += m->length;
2155 }
2156
2157 return 0;
2158}
2159
9f5f5fb3 2160/*
3ac3e5ee 2161 * Init the device table to not allow DMA access for devices
9f5f5fb3 2162 */
33f28c59 2163static void init_device_table_dma(void)
9f5f5fb3 2164{
0de66d5b 2165 u32 devid;
9f5f5fb3
JR
2166
2167 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2168 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2169 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
9f5f5fb3
JR
2170 }
2171}
2172
d04e0ba3
JR
2173static void __init uninit_device_table_dma(void)
2174{
2175 u32 devid;
2176
2177 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2178 amd_iommu_dev_table[devid].data[0] = 0ULL;
2179 amd_iommu_dev_table[devid].data[1] = 0ULL;
2180 }
2181}
2182
33f28c59
JR
2183static void init_device_table(void)
2184{
2185 u32 devid;
2186
2187 if (!amd_iommu_irq_remap)
2188 return;
2189
2190 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2191 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2192}
2193
e9bf5197
JR
2194static void iommu_init_flags(struct amd_iommu *iommu)
2195{
2196 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2197 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2198 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2199
2200 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2201 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2202 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2203
2204 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2205 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2206 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2207
2208 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2209 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2210 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2211
2212 /*
2213 * make IOMMU memory accesses cache coherent
2214 */
2215 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
1456e9d2
JR
2216
2217 /* Set IOTLB invalidation timeout to 1s */
2218 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
e9bf5197
JR
2219}
2220
5bcd757f 2221static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
4c894f47 2222{
5bcd757f
MG
2223 int i, j;
2224 u32 ioc_feature_control;
c1bf94ec 2225 struct pci_dev *pdev = iommu->root_pdev;
5bcd757f
MG
2226
2227 /* RD890 BIOSes may not have completely reconfigured the iommu */
c1bf94ec 2228 if (!is_rd890_iommu(iommu->dev) || !pdev)
5bcd757f
MG
2229 return;
2230
2231 /*
2232 * First, we need to ensure that the iommu is enabled. This is
2233 * controlled by a register in the northbridge
2234 */
5bcd757f
MG
2235
2236 /* Select Northbridge indirect register 0x75 and enable writing */
2237 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2238 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2239
2240 /* Enable the iommu */
2241 if (!(ioc_feature_control & 0x1))
2242 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2243
5bcd757f
MG
2244 /* Restore the iommu BAR */
2245 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2246 iommu->stored_addr_lo);
2247 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2248 iommu->stored_addr_hi);
2249
2250 /* Restore the l1 indirect regs for each of the 6 l1s */
2251 for (i = 0; i < 6; i++)
2252 for (j = 0; j < 0x12; j++)
2253 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2254
2255 /* Restore the l2 indirect regs */
2256 for (i = 0; i < 0x83; i++)
2257 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2258
2259 /* Lock PCI setup registers */
2260 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2261 iommu->stored_addr_lo | 1);
4c894f47
JR
2262}
2263
3928aa3f
SS
2264static void iommu_enable_ga(struct amd_iommu *iommu)
2265{
2266#ifdef CONFIG_IRQ_REMAP
2267 switch (amd_iommu_guest_ir) {
2268 case AMD_IOMMU_GUEST_IR_VAPIC:
2269 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2270 /* Fall through */
2271 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2272 iommu_feature_enable(iommu, CONTROL_GA_EN);
77bdab46 2273 iommu->irte_ops = &irte_128_ops;
3928aa3f
SS
2274 break;
2275 default:
77bdab46 2276 iommu->irte_ops = &irte_32_ops;
3928aa3f
SS
2277 break;
2278 }
2279#endif
2280}
2281
78d313c6
BH
2282static void early_enable_iommu(struct amd_iommu *iommu)
2283{
2284 iommu_disable(iommu);
2285 iommu_init_flags(iommu);
2286 iommu_set_device_table(iommu);
2287 iommu_enable_command_buffer(iommu);
2288 iommu_enable_event_buffer(iommu);
2289 iommu_set_exclusion_range(iommu);
2290 iommu_enable_ga(iommu);
90fcffd9 2291 iommu_enable_xt(iommu);
78d313c6
BH
2292 iommu_enable(iommu);
2293 iommu_flush_all_caches(iommu);
2294}
2295
b65233a9
JR
2296/*
2297 * This function finally enables all IOMMUs found in the system after
3ac3e5ee
BH
2298 * they have been initialized.
2299 *
2300 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2301 * the old content of device table entries. Not this case or copy failed,
2302 * just continue as normal kernel does.
b65233a9 2303 */
11ee5ac4 2304static void early_enable_iommus(void)
8736197b
JR
2305{
2306 struct amd_iommu *iommu;
2307
3ac3e5ee
BH
2308
2309 if (!copy_device_table()) {
2310 /*
2311 * If come here because of failure in copying device table from old
2312 * kernel with all IOMMUs enabled, print error message and try to
2313 * free allocated old_dev_tbl_cpy.
2314 */
2315 if (amd_iommu_pre_enabled)
2316 pr_err("Failed to copy DEV table from previous kernel.\n");
2317 if (old_dev_tbl_cpy != NULL)
2318 free_pages((unsigned long)old_dev_tbl_cpy,
2319 get_order(dev_table_size));
2320
2321 for_each_iommu(iommu) {
2322 clear_translation_pre_enabled(iommu);
2323 early_enable_iommu(iommu);
2324 }
2325 } else {
2326 pr_info("Copied DEV table from previous kernel.\n");
2327 free_pages((unsigned long)amd_iommu_dev_table,
2328 get_order(dev_table_size));
2329 amd_iommu_dev_table = old_dev_tbl_cpy;
2330 for_each_iommu(iommu) {
2331 iommu_disable_command_buffer(iommu);
2332 iommu_disable_event_buffer(iommu);
2333 iommu_enable_command_buffer(iommu);
2334 iommu_enable_event_buffer(iommu);
2335 iommu_enable_ga(iommu);
90fcffd9 2336 iommu_enable_xt(iommu);
3ac3e5ee
BH
2337 iommu_set_device_table(iommu);
2338 iommu_flush_all_caches(iommu);
2339 }
8736197b 2340 }
d98de49a
SS
2341
2342#ifdef CONFIG_IRQ_REMAP
2343 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2344 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2345#endif
8736197b
JR
2346}
2347
11ee5ac4
JR
2348static void enable_iommus_v2(void)
2349{
2350 struct amd_iommu *iommu;
2351
2352 for_each_iommu(iommu) {
2353 iommu_enable_ppr_log(iommu);
2354 iommu_enable_gt(iommu);
2355 }
2356}
2357
2358static void enable_iommus(void)
2359{
2360 early_enable_iommus();
2361
2362 enable_iommus_v2();
2363}
2364
92ac4320
JR
2365static void disable_iommus(void)
2366{
2367 struct amd_iommu *iommu;
2368
2369 for_each_iommu(iommu)
2370 iommu_disable(iommu);
d98de49a
SS
2371
2372#ifdef CONFIG_IRQ_REMAP
2373 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2374 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2375#endif
92ac4320
JR
2376}
2377
7441e9cb
JR
2378/*
2379 * Suspend/Resume support
2380 * disable suspend until real resume implemented
2381 */
2382
f3c6ea1b 2383static void amd_iommu_resume(void)
7441e9cb 2384{
5bcd757f
MG
2385 struct amd_iommu *iommu;
2386
2387 for_each_iommu(iommu)
2388 iommu_apply_resume_quirks(iommu);
2389
736501ee
JR
2390 /* re-load the hardware */
2391 enable_iommus();
3d9761e7
JR
2392
2393 amd_iommu_enable_interrupts();
7441e9cb
JR
2394}
2395
f3c6ea1b 2396static int amd_iommu_suspend(void)
7441e9cb 2397{
736501ee
JR
2398 /* disable IOMMUs to go out of the way for BIOS */
2399 disable_iommus();
2400
2401 return 0;
7441e9cb
JR
2402}
2403
f3c6ea1b 2404static struct syscore_ops amd_iommu_syscore_ops = {
7441e9cb
JR
2405 .suspend = amd_iommu_suspend,
2406 .resume = amd_iommu_resume,
2407};
2408
90b3eb03 2409static void __init free_iommu_resources(void)
8704a1ba 2410{
ebcfa284 2411 kmemleak_free(irq_lookup_table);
0ea2c422
JR
2412 free_pages((unsigned long)irq_lookup_table,
2413 get_order(rlookup_table_size));
f6019271 2414 irq_lookup_table = NULL;
8704a1ba 2415
a591989a
JL
2416 kmem_cache_destroy(amd_iommu_irq_cache);
2417 amd_iommu_irq_cache = NULL;
8704a1ba
JR
2418
2419 free_pages((unsigned long)amd_iommu_rlookup_table,
2420 get_order(rlookup_table_size));
f6019271 2421 amd_iommu_rlookup_table = NULL;
8704a1ba
JR
2422
2423 free_pages((unsigned long)amd_iommu_alias_table,
2424 get_order(alias_table_size));
f6019271 2425 amd_iommu_alias_table = NULL;
8704a1ba
JR
2426
2427 free_pages((unsigned long)amd_iommu_dev_table,
2428 get_order(dev_table_size));
f6019271 2429 amd_iommu_dev_table = NULL;
8704a1ba
JR
2430
2431 free_iommu_all();
8704a1ba
JR
2432}
2433
c2ff5cf5
JR
2434/* SB IOAPIC is always on this device in AMD systems */
2435#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2436
eb1eb7ae
JR
2437static bool __init check_ioapic_information(void)
2438{
dfbb6d47 2439 const char *fw_bug = FW_BUG;
c2ff5cf5 2440 bool ret, has_sb_ioapic;
eb1eb7ae
JR
2441 int idx;
2442
c2ff5cf5
JR
2443 has_sb_ioapic = false;
2444 ret = false;
eb1eb7ae 2445
dfbb6d47
JR
2446 /*
2447 * If we have map overrides on the kernel command line the
2448 * messages in this function might not describe firmware bugs
2449 * anymore - so be careful
2450 */
2451 if (cmdline_maps)
2452 fw_bug = "";
2453
c2ff5cf5
JR
2454 for (idx = 0; idx < nr_ioapics; idx++) {
2455 int devid, id = mpc_ioapic_id(idx);
2456
2457 devid = get_ioapic_devid(id);
2458 if (devid < 0) {
101fa037 2459 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
dfbb6d47 2460 fw_bug, id);
c2ff5cf5
JR
2461 ret = false;
2462 } else if (devid == IOAPIC_SB_DEVID) {
2463 has_sb_ioapic = true;
2464 ret = true;
eb1eb7ae
JR
2465 }
2466 }
2467
c2ff5cf5
JR
2468 if (!has_sb_ioapic) {
2469 /*
2470 * We expect the SB IOAPIC to be listed in the IVRS
2471 * table. The system timer is connected to the SB IOAPIC
2472 * and if we don't have it in the list the system will
2473 * panic at boot time. This situation usually happens
2474 * when the BIOS is buggy and provides us the wrong
2475 * device id for the IOAPIC in the system.
2476 */
101fa037 2477 pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
c2ff5cf5
JR
2478 }
2479
2480 if (!ret)
101fa037 2481 pr_err("Disabling interrupt remapping\n");
c2ff5cf5
JR
2482
2483 return ret;
eb1eb7ae
JR
2484}
2485
d04e0ba3
JR
2486static void __init free_dma_resources(void)
2487{
d04e0ba3
JR
2488 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2489 get_order(MAX_DOMAIN_ID/8));
f6019271 2490 amd_iommu_pd_alloc_bitmap = NULL;
d04e0ba3
JR
2491
2492 free_unity_maps();
2493}
2494
b65233a9 2495/*
8704a1ba
JR
2496 * This is the hardware init function for AMD IOMMU in the system.
2497 * This function is called either from amd_iommu_init or from the interrupt
2498 * remapping setup code.
b65233a9
JR
2499 *
2500 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
8c7142f5 2501 * four times:
b65233a9 2502 *
8c7142f5
SS
2503 * 1 pass) Discover the most comprehensive IVHD type to use.
2504 *
2505 * 2 pass) Find the highest PCI device id the driver has to handle.
b65233a9
JR
2506 * Upon this information the size of the data structures is
2507 * determined that needs to be allocated.
2508 *
8c7142f5 2509 * 3 pass) Initialize the data structures just allocated with the
b65233a9
JR
2510 * information in the ACPI table about available AMD IOMMUs
2511 * in the system. It also maps the PCI devices in the
2512 * system to specific IOMMUs
2513 *
8c7142f5 2514 * 4 pass) After the basic data structures are allocated and
b65233a9
JR
2515 * initialized we update them with information about memory
2516 * remapping requirements parsed out of the ACPI table in
2517 * this last pass.
2518 *
8704a1ba
JR
2519 * After everything is set up the IOMMUs are enabled and the necessary
2520 * hotplug and suspend notifiers are registered.
b65233a9 2521 */
643511b3 2522static int __init early_amd_iommu_init(void)
fe74c9cf 2523{
02f3b3f5 2524 struct acpi_table_header *ivrs_base;
02f3b3f5 2525 acpi_status status;
3928aa3f 2526 int i, remap_cache_sz, ret = 0;
3dfee47b 2527 u32 pci_id;
fe74c9cf 2528
643511b3 2529 if (!amd_iommu_detected)
8704a1ba
JR
2530 return -ENODEV;
2531
6b11d1d6 2532 status = acpi_get_table("IVRS", 0, &ivrs_base);
02f3b3f5
JR
2533 if (status == AE_NOT_FOUND)
2534 return -ENODEV;
2535 else if (ACPI_FAILURE(status)) {
2536 const char *err = acpi_format_exception(status);
101fa037 2537 pr_err("IVRS table error: %s\n", err);
02f3b3f5
JR
2538 return -EINVAL;
2539 }
2540
8c7142f5
SS
2541 /*
2542 * Validate checksum here so we don't need to do it when
2543 * we actually parse the table
2544 */
2545 ret = check_ivrs_checksum(ivrs_base);
2546 if (ret)
99e8ccd3 2547 goto out;
8c7142f5
SS
2548
2549 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2550 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2551
fe74c9cf
JR
2552 /*
2553 * First parse ACPI tables to find the largest Bus/Dev/Func
2554 * we need to handle. Upon this information the shared data
2555 * structures for the IOMMUs in the system will be allocated
2556 */
2c0ae172
JR
2557 ret = find_last_devid_acpi(ivrs_base);
2558 if (ret)
3551a708
JR
2559 goto out;
2560
c571484e
JR
2561 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2562 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2563 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
fe74c9cf 2564
fe74c9cf 2565 /* Device table - directly used by all IOMMUs */
8704a1ba 2566 ret = -ENOMEM;
b336781b
BH
2567 amd_iommu_dev_table = (void *)__get_free_pages(
2568 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
fe74c9cf
JR
2569 get_order(dev_table_size));
2570 if (amd_iommu_dev_table == NULL)
2571 goto out;
2572
2573 /*
2574 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2575 * IOMMU see for that device
2576 */
2577 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2578 get_order(alias_table_size));
2579 if (amd_iommu_alias_table == NULL)
2c0ae172 2580 goto out;
fe74c9cf
JR
2581
2582 /* IOMMU rlookup table - find the IOMMU for a specific device */
83fd5cc6
JR
2583 amd_iommu_rlookup_table = (void *)__get_free_pages(
2584 GFP_KERNEL | __GFP_ZERO,
fe74c9cf
JR
2585 get_order(rlookup_table_size));
2586 if (amd_iommu_rlookup_table == NULL)
2c0ae172 2587 goto out;
fe74c9cf 2588
5dc8bff0
JR
2589 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2590 GFP_KERNEL | __GFP_ZERO,
fe74c9cf
JR
2591 get_order(MAX_DOMAIN_ID/8));
2592 if (amd_iommu_pd_alloc_bitmap == NULL)
2c0ae172 2593 goto out;
fe74c9cf
JR
2594
2595 /*
5dc8bff0 2596 * let all alias entries point to itself
fe74c9cf 2597 */
3a61ec38 2598 for (i = 0; i <= amd_iommu_last_bdf; ++i)
fe74c9cf
JR
2599 amd_iommu_alias_table[i] = i;
2600
fe74c9cf
JR
2601 /*
2602 * never allocate domain 0 because its used as the non-allocated and
2603 * error value placeholder
2604 */
5c87f62d 2605 __set_bit(0, amd_iommu_pd_alloc_bitmap);
fe74c9cf
JR
2606
2607 /*
2608 * now the data structures are allocated and basically initialized
2609 * start the real acpi table scan
2610 */
02f3b3f5
JR
2611 ret = init_iommu_all(ivrs_base);
2612 if (ret)
2c0ae172 2613 goto out;
fe74c9cf 2614
3dfee47b
KHF
2615 /* Disable IOMMU if there's Stoney Ridge graphics */
2616 for (i = 0; i < 32; i++) {
2617 pci_id = read_pci_config(0, i, 0, 0);
2618 if ((pci_id & 0xffff) == 0x1002 && (pci_id >> 16) == 0x98e4) {
2619 pr_info("Disable IOMMU on Stoney Ridge\n");
2620 amd_iommu_disabled = true;
2621 break;
2622 }
2623 }
2624
11123741 2625 /* Disable any previously enabled IOMMUs */
20b46dff
BH
2626 if (!is_kdump_kernel() || amd_iommu_disabled)
2627 disable_iommus();
11123741 2628
eb1eb7ae
JR
2629 if (amd_iommu_irq_remap)
2630 amd_iommu_irq_remap = check_ioapic_information();
2631
05152a04
JR
2632 if (amd_iommu_irq_remap) {
2633 /*
2634 * Interrupt remapping enabled, create kmem_cache for the
2635 * remapping tables.
2636 */
83ed9c13 2637 ret = -ENOMEM;
3928aa3f
SS
2638 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2639 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2640 else
2641 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
05152a04 2642 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
3928aa3f
SS
2643 remap_cache_sz,
2644 IRQ_TABLE_ALIGNMENT,
2645 0, NULL);
05152a04
JR
2646 if (!amd_iommu_irq_cache)
2647 goto out;
0ea2c422
JR
2648
2649 irq_lookup_table = (void *)__get_free_pages(
2650 GFP_KERNEL | __GFP_ZERO,
2651 get_order(rlookup_table_size));
ebcfa284
LS
2652 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2653 1, GFP_KERNEL);
0ea2c422
JR
2654 if (!irq_lookup_table)
2655 goto out;
05152a04
JR
2656 }
2657
02f3b3f5
JR
2658 ret = init_memory_definitions(ivrs_base);
2659 if (ret)
2c0ae172 2660 goto out;
3551a708 2661
eb1eb7ae
JR
2662 /* init the device table */
2663 init_device_table();
2664
8704a1ba 2665out:
02f3b3f5 2666 /* Don't leak any ACPI memory */
6b11d1d6 2667 acpi_put_table(ivrs_base);
02f3b3f5
JR
2668 ivrs_base = NULL;
2669
643511b3
JR
2670 return ret;
2671}
2672
ae295142 2673static int amd_iommu_enable_interrupts(void)
3d9761e7
JR
2674{
2675 struct amd_iommu *iommu;
2676 int ret = 0;
2677
2678 for_each_iommu(iommu) {
2679 ret = iommu_init_msi(iommu);
2680 if (ret)
2681 goto out;
2682 }
2683
2684out:
2685 return ret;
2686}
2687
02f3b3f5
JR
2688static bool detect_ivrs(void)
2689{
2690 struct acpi_table_header *ivrs_base;
02f3b3f5
JR
2691 acpi_status status;
2692
6b11d1d6 2693 status = acpi_get_table("IVRS", 0, &ivrs_base);
02f3b3f5
JR
2694 if (status == AE_NOT_FOUND)
2695 return false;
2696 else if (ACPI_FAILURE(status)) {
2697 const char *err = acpi_format_exception(status);
101fa037 2698 pr_err("IVRS table error: %s\n", err);
02f3b3f5
JR
2699 return false;
2700 }
2701
6b11d1d6 2702 acpi_put_table(ivrs_base);
02f3b3f5 2703
1adb7d31
JR
2704 /* Make sure ACS will be enabled during PCI probe */
2705 pci_request_acs();
2706
02f3b3f5
JR
2707 return true;
2708}
2709
2c0ae172 2710/****************************************************************************
8704a1ba 2711 *
2c0ae172
JR
2712 * AMD IOMMU Initialization State Machine
2713 *
2714 ****************************************************************************/
2715
2716static int __init state_next(void)
8704a1ba
JR
2717{
2718 int ret = 0;
2719
2c0ae172
JR
2720 switch (init_state) {
2721 case IOMMU_START_STATE:
2722 if (!detect_ivrs()) {
2723 init_state = IOMMU_NOT_FOUND;
2724 ret = -ENODEV;
2725 } else {
2726 init_state = IOMMU_IVRS_DETECTED;
2727 }
2728 break;
2729 case IOMMU_IVRS_DETECTED:
2730 ret = early_amd_iommu_init();
2731 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
7ad820e4 2732 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
3dfee47b 2733 pr_info("AMD IOMMU disabled\n");
7ad820e4
JR
2734 init_state = IOMMU_CMDLINE_DISABLED;
2735 ret = -EINVAL;
2736 }
2c0ae172
JR
2737 break;
2738 case IOMMU_ACPI_FINISHED:
2739 early_enable_iommus();
2c0ae172
JR
2740 x86_platform.iommu_shutdown = disable_iommus;
2741 init_state = IOMMU_ENABLED;
2742 break;
2743 case IOMMU_ENABLED:
74ddda71 2744 register_syscore_ops(&amd_iommu_syscore_ops);
2c0ae172
JR
2745 ret = amd_iommu_init_pci();
2746 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2747 enable_iommus_v2();
2748 break;
2749 case IOMMU_PCI_INIT:
2750 ret = amd_iommu_enable_interrupts();
2751 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2752 break;
2753 case IOMMU_INTERRUPTS_EN:
1e6a7b04 2754 ret = amd_iommu_init_dma_ops();
2c0ae172
JR
2755 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2756 break;
2757 case IOMMU_DMA_OPS:
2758 init_state = IOMMU_INITIALIZED;
2759 break;
2760 case IOMMU_INITIALIZED:
2761 /* Nothing to do */
2762 break;
2763 case IOMMU_NOT_FOUND:
2764 case IOMMU_INIT_ERROR:
1b1e942e 2765 case IOMMU_CMDLINE_DISABLED:
2c0ae172
JR
2766 /* Error states => do nothing */
2767 ret = -EINVAL;
2768 break;
2769 default:
2770 /* Unknown state */
2771 BUG();
2772 }
3d9761e7 2773
5c90501a
KM
2774 if (ret) {
2775 free_dma_resources();
2776 if (!irq_remapping_enabled) {
2777 disable_iommus();
2778 free_iommu_resources();
2779 } else {
2780 struct amd_iommu *iommu;
2781
2782 uninit_device_table_dma();
2783 for_each_iommu(iommu)
2784 iommu_flush_all_caches(iommu);
2785 }
2786 }
2c0ae172
JR
2787 return ret;
2788}
7441e9cb 2789
2c0ae172
JR
2790static int __init iommu_go_to_state(enum iommu_init_state state)
2791{
151b0903 2792 int ret = -EINVAL;
f5325094 2793
2c0ae172 2794 while (init_state != state) {
1b1e942e
JR
2795 if (init_state == IOMMU_NOT_FOUND ||
2796 init_state == IOMMU_INIT_ERROR ||
2797 init_state == IOMMU_CMDLINE_DISABLED)
2c0ae172 2798 break;
151b0903 2799 ret = state_next();
2c0ae172 2800 }
f2f12b6f 2801
fe74c9cf 2802 return ret;
2c0ae172 2803}
fe74c9cf 2804
6b474b82
JR
2805#ifdef CONFIG_IRQ_REMAP
2806int __init amd_iommu_prepare(void)
2807{
3f4cb7c0
TG
2808 int ret;
2809
7fa1c842 2810 amd_iommu_irq_remap = true;
84d07793 2811
3f4cb7c0
TG
2812 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2813 if (ret)
2814 return ret;
2815 return amd_iommu_irq_remap ? 0 : -ENODEV;
6b474b82 2816}
d7f07769 2817
6b474b82
JR
2818int __init amd_iommu_enable(void)
2819{
2820 int ret;
2821
2822 ret = iommu_go_to_state(IOMMU_ENABLED);
2823 if (ret)
2824 return ret;
d7f07769 2825
6b474b82 2826 irq_remapping_enabled = 1;
90fcffd9 2827 return amd_iommu_xt_mode;
6b474b82
JR
2828}
2829
2830void amd_iommu_disable(void)
2831{
2832 amd_iommu_suspend();
2833}
2834
2835int amd_iommu_reenable(int mode)
2836{
2837 amd_iommu_resume();
2838
2839 return 0;
2840}
d7f07769 2841
6b474b82
JR
2842int __init amd_iommu_enable_faulting(void)
2843{
2844 /* We enable MSI later when PCI is initialized */
2845 return 0;
2846}
2847#endif
d7f07769 2848
2c0ae172
JR
2849/*
2850 * This is the core init function for AMD IOMMU hardware in the system.
2851 * This function is called from the generic x86 DMA layer initialization
2852 * code.
2853 */
2854static int __init amd_iommu_init(void)
2855{
7d0f5fd3 2856 struct amd_iommu *iommu;
2c0ae172
JR
2857 int ret;
2858
2859 ret = iommu_go_to_state(IOMMU_INITIALIZED);
bf4bff46
KM
2860#ifdef CONFIG_GART_IOMMU
2861 if (ret && list_empty(&amd_iommu_list)) {
2862 /*
2863 * We failed to initialize the AMD IOMMU - try fallback
2864 * to GART if possible.
2865 */
2866 gart_iommu_init();
2c0ae172 2867 }
bf4bff46 2868#endif
2c0ae172 2869
7d0f5fd3
GH
2870 for_each_iommu(iommu)
2871 amd_iommu_debugfs_setup(iommu);
2872
2c0ae172 2873 return ret;
fe74c9cf
JR
2874}
2875
2543a786
TL
2876static bool amd_iommu_sme_check(void)
2877{
2878 if (!sme_active() || (boot_cpu_data.x86 != 0x17))
2879 return true;
2880
2881 /* For Fam17h, a specific level of support is required */
2882 if (boot_cpu_data.microcode >= 0x08001205)
2883 return true;
2884
2885 if ((boot_cpu_data.microcode >= 0x08001126) &&
2886 (boot_cpu_data.microcode <= 0x080011ff))
2887 return true;
2888
101fa037 2889 pr_notice("IOMMU not currently supported when SME is active\n");
2543a786
TL
2890
2891 return false;
2892}
2893
b65233a9
JR
2894/****************************************************************************
2895 *
2896 * Early detect code. This code runs at IOMMU detection time in the DMA
2897 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2898 * IOMMUs
2899 *
2900 ****************************************************************************/
480125ba 2901int __init amd_iommu_detect(void)
ae7877de 2902{
2c0ae172 2903 int ret;
02f3b3f5 2904
75f1cdf1 2905 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
480125ba 2906 return -ENODEV;
ae7877de 2907
2543a786
TL
2908 if (!amd_iommu_sme_check())
2909 return -ENODEV;
2910
2c0ae172
JR
2911 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2912 if (ret)
2913 return ret;
11bd04f6 2914
02f3b3f5
JR
2915 amd_iommu_detected = true;
2916 iommu_detected = 1;
2917 x86_init.iommu.iommu_init = amd_iommu_init;
2918
4781bc42 2919 return 1;
ae7877de
JR
2920}
2921
b65233a9
JR
2922/****************************************************************************
2923 *
2924 * Parsing functions for the AMD IOMMU specific kernel command line
2925 * options.
2926 *
2927 ****************************************************************************/
2928
fefda117
JR
2929static int __init parse_amd_iommu_dump(char *str)
2930{
2931 amd_iommu_dump = true;
2932
2933 return 1;
2934}
2935
3928aa3f
SS
2936static int __init parse_amd_iommu_intr(char *str)
2937{
2938 for (; *str; ++str) {
2939 if (strncmp(str, "legacy", 6) == 0) {
b74aa02d 2940 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
3928aa3f
SS
2941 break;
2942 }
2943 if (strncmp(str, "vapic", 5) == 0) {
2944 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2945 break;
2946 }
2947 }
2948 return 1;
2949}
2950
918ad6c5
JR
2951static int __init parse_amd_iommu_options(char *str)
2952{
2953 for (; *str; ++str) {
695b5676 2954 if (strncmp(str, "fullflush", 9) == 0)
afa9fdc2 2955 amd_iommu_unmap_flush = true;
a5235725
JR
2956 if (strncmp(str, "off", 3) == 0)
2957 amd_iommu_disabled = true;
5abcdba4
JR
2958 if (strncmp(str, "force_isolation", 15) == 0)
2959 amd_iommu_force_isolation = true;
918ad6c5
JR
2960 }
2961
2962 return 1;
2963}
2964
440e8998
JR
2965static int __init parse_ivrs_ioapic(char *str)
2966{
2967 unsigned int bus, dev, fn;
2968 int ret, id, i;
2969 u16 devid;
2970
2971 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2972
2973 if (ret != 4) {
101fa037 2974 pr_err("Invalid command line: ivrs_ioapic%s\n", str);
440e8998
JR
2975 return 1;
2976 }
2977
2978 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
101fa037 2979 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
440e8998
JR
2980 str);
2981 return 1;
2982 }
2983
2984 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2985
dfbb6d47 2986 cmdline_maps = true;
440e8998
JR
2987 i = early_ioapic_map_size++;
2988 early_ioapic_map[i].id = id;
2989 early_ioapic_map[i].devid = devid;
2990 early_ioapic_map[i].cmd_line = true;
2991
2992 return 1;
2993}
2994
2995static int __init parse_ivrs_hpet(char *str)
2996{
2997 unsigned int bus, dev, fn;
2998 int ret, id, i;
2999 u16 devid;
3000
3001 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
3002
3003 if (ret != 4) {
101fa037 3004 pr_err("Invalid command line: ivrs_hpet%s\n", str);
440e8998
JR
3005 return 1;
3006 }
3007
3008 if (early_hpet_map_size == EARLY_MAP_SIZE) {
101fa037 3009 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
440e8998
JR
3010 str);
3011 return 1;
3012 }
3013
3014 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3015
dfbb6d47 3016 cmdline_maps = true;
440e8998
JR
3017 i = early_hpet_map_size++;
3018 early_hpet_map[i].id = id;
3019 early_hpet_map[i].devid = devid;
3020 early_hpet_map[i].cmd_line = true;
3021
3022 return 1;
3023}
3024
ca3bf5d4
SS
3025static int __init parse_ivrs_acpihid(char *str)
3026{
3027 u32 bus, dev, fn;
3028 char *hid, *uid, *p;
3029 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
3030 int ret, i;
3031
3032 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
3033 if (ret != 4) {
101fa037 3034 pr_err("Invalid command line: ivrs_acpihid(%s)\n", str);
ca3bf5d4
SS
3035 return 1;
3036 }
3037
3038 p = acpiid;
3039 hid = strsep(&p, ":");
3040 uid = p;
3041
3042 if (!hid || !(*hid) || !uid) {
101fa037 3043 pr_err("Invalid command line: hid or uid\n");
ca3bf5d4
SS
3044 return 1;
3045 }
3046
3047 i = early_acpihid_map_size++;
3048 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
3049 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
3050 early_acpihid_map[i].devid =
3051 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3052 early_acpihid_map[i].cmd_line = true;
3053
3054 return 1;
3055}
3056
440e8998
JR
3057__setup("amd_iommu_dump", parse_amd_iommu_dump);
3058__setup("amd_iommu=", parse_amd_iommu_options);
3928aa3f 3059__setup("amd_iommu_intr=", parse_amd_iommu_intr);
440e8998
JR
3060__setup("ivrs_ioapic", parse_ivrs_ioapic);
3061__setup("ivrs_hpet", parse_ivrs_hpet);
ca3bf5d4 3062__setup("ivrs_acpihid", parse_ivrs_acpihid);
22e6daf4
KRW
3063
3064IOMMU_INIT_FINISH(amd_iommu_detect,
3065 gart_iommu_hole_init,
98f1ad25
JR
3066 NULL,
3067 NULL);
400a28a0
JR
3068
3069bool amd_iommu_v2_supported(void)
3070{
3071 return amd_iommu_v2_present;
3072}
3073EXPORT_SYMBOL(amd_iommu_v2_supported);
30861ddc 3074
f5863a00
SS
3075struct amd_iommu *get_amd_iommu(unsigned int idx)
3076{
3077 unsigned int i = 0;
3078 struct amd_iommu *iommu;
3079
3080 for_each_iommu(iommu)
3081 if (i++ == idx)
3082 return iommu;
3083 return NULL;
3084}
3085EXPORT_SYMBOL(get_amd_iommu);
3086
30861ddc
SK
3087/****************************************************************************
3088 *
3089 * IOMMU EFR Performance Counter support functionality. This code allows
3090 * access to the IOMMU PC functionality.
3091 *
3092 ****************************************************************************/
3093
f5863a00 3094u8 amd_iommu_pc_get_max_banks(unsigned int idx)
30861ddc 3095{
f5863a00 3096 struct amd_iommu *iommu = get_amd_iommu(idx);
30861ddc 3097
30861ddc 3098 if (iommu)
f5863a00 3099 return iommu->max_banks;
30861ddc 3100
f5863a00 3101 return 0;
30861ddc
SK
3102}
3103EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
3104
3105bool amd_iommu_pc_supported(void)
3106{
3107 return amd_iommu_pc_present;
3108}
3109EXPORT_SYMBOL(amd_iommu_pc_supported);
3110
f5863a00 3111u8 amd_iommu_pc_get_max_counters(unsigned int idx)
30861ddc 3112{
f5863a00 3113 struct amd_iommu *iommu = get_amd_iommu(idx);
30861ddc 3114
30861ddc 3115 if (iommu)
f5863a00 3116 return iommu->max_counters;
30861ddc 3117
f5863a00 3118 return 0;
30861ddc
SK
3119}
3120EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3121
1650dfd1
SS
3122static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3123 u8 fxn, u64 *value, bool is_write)
30861ddc 3124{
30861ddc
SK
3125 u32 offset;
3126 u32 max_offset_lim;
3127
1650dfd1
SS
3128 /* Make sure the IOMMU PC resource is available */
3129 if (!amd_iommu_pc_present)
3130 return -ENODEV;
3131
30861ddc 3132 /* Check for valid iommu and pc register indexing */
1650dfd1 3133 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
30861ddc
SK
3134 return -ENODEV;
3135
0a6d80c7 3136 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
30861ddc
SK
3137
3138 /* Limit the offset to the hw defined mmio region aperture */
0a6d80c7 3139 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
30861ddc
SK
3140 (iommu->max_counters << 8) | 0x28);
3141 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3142 (offset > max_offset_lim))
3143 return -EINVAL;
3144
3145 if (is_write) {
0a6d80c7
SS
3146 u64 val = *value & GENMASK_ULL(47, 0);
3147
3148 writel((u32)val, iommu->mmio_base + offset);
3149 writel((val >> 32), iommu->mmio_base + offset + 4);
30861ddc
SK
3150 } else {
3151 *value = readl(iommu->mmio_base + offset + 4);
3152 *value <<= 32;
0a6d80c7
SS
3153 *value |= readl(iommu->mmio_base + offset);
3154 *value &= GENMASK_ULL(47, 0);
30861ddc
SK
3155 }
3156
3157 return 0;
3158}
38e45d02 3159
1650dfd1 3160int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
38e45d02 3161{
1650dfd1
SS
3162 if (!iommu)
3163 return -EINVAL;
38e45d02 3164
1650dfd1
SS
3165 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
3166}
3167EXPORT_SYMBOL(amd_iommu_pc_get_reg);
3168
3169int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3170{
3171 if (!iommu)
3172 return -EINVAL;
38e45d02 3173
1650dfd1 3174 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
38e45d02 3175}
1650dfd1 3176EXPORT_SYMBOL(amd_iommu_pc_set_reg);