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