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