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