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