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