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