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