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