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