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