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