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 u64 entry;
809
810 if (!iommu->ga_log)
811 return -EINVAL;
812
813 /* Check if already running */
814 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
815 if (WARN_ON(status & (MMIO_STATUS_GALOG_RUN_MASK)))
816 return 0;
817
818 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
819 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
820 &entry, sizeof(entry));
821 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
822 (BIT_ULL(52)-1)) & ~7ULL;
823 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
824 &entry, sizeof(entry));
825 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
826 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
827
828
829 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
830 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
831
832 for (i = 0; i < LOOP_TIMEOUT; ++i) {
833 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
834 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
835 break;
836 }
837
838 if (WARN_ON(i >= LOOP_TIMEOUT))
839 return -EINVAL;
840 #endif /* CONFIG_IRQ_REMAP */
841 return 0;
842 }
843
iommu_init_ga_log(struct amd_iommu * iommu)844 static int iommu_init_ga_log(struct amd_iommu *iommu)
845 {
846 #ifdef CONFIG_IRQ_REMAP
847 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
848 return 0;
849
850 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
851 get_order(GA_LOG_SIZE));
852 if (!iommu->ga_log)
853 goto err_out;
854
855 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
856 get_order(8));
857 if (!iommu->ga_log_tail)
858 goto err_out;
859
860 return 0;
861 err_out:
862 free_ga_log(iommu);
863 return -EINVAL;
864 #else
865 return 0;
866 #endif /* CONFIG_IRQ_REMAP */
867 }
868
alloc_cwwb_sem(struct amd_iommu * iommu)869 static int __init alloc_cwwb_sem(struct amd_iommu *iommu)
870 {
871 iommu->cmd_sem = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO, 1);
872
873 return iommu->cmd_sem ? 0 : -ENOMEM;
874 }
875
free_cwwb_sem(struct amd_iommu * iommu)876 static void __init free_cwwb_sem(struct amd_iommu *iommu)
877 {
878 if (iommu->cmd_sem)
879 free_page((unsigned long)iommu->cmd_sem);
880 }
881
iommu_enable_xt(struct amd_iommu * iommu)882 static void iommu_enable_xt(struct amd_iommu *iommu)
883 {
884 #ifdef CONFIG_IRQ_REMAP
885 /*
886 * XT mode (32-bit APIC destination ID) requires
887 * GA mode (128-bit IRTE support) as a prerequisite.
888 */
889 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
890 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
891 iommu_feature_enable(iommu, CONTROL_XT_EN);
892 #endif /* CONFIG_IRQ_REMAP */
893 }
894
iommu_enable_gt(struct amd_iommu * iommu)895 static void iommu_enable_gt(struct amd_iommu *iommu)
896 {
897 if (!iommu_feature(iommu, FEATURE_GT))
898 return;
899
900 iommu_feature_enable(iommu, CONTROL_GT_EN);
901 }
902
903 /* sets a specific bit in the device table entry. */
set_dev_entry_bit(u16 devid,u8 bit)904 static void set_dev_entry_bit(u16 devid, u8 bit)
905 {
906 int i = (bit >> 6) & 0x03;
907 int _bit = bit & 0x3f;
908
909 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
910 }
911
get_dev_entry_bit(u16 devid,u8 bit)912 static int get_dev_entry_bit(u16 devid, u8 bit)
913 {
914 int i = (bit >> 6) & 0x03;
915 int _bit = bit & 0x3f;
916
917 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
918 }
919
920
copy_device_table(void)921 static bool copy_device_table(void)
922 {
923 u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
924 struct dev_table_entry *old_devtb = NULL;
925 u32 lo, hi, devid, old_devtb_size;
926 phys_addr_t old_devtb_phys;
927 struct amd_iommu *iommu;
928 u16 dom_id, dte_v, irq_v;
929 gfp_t gfp_flag;
930 u64 tmp;
931
932 if (!amd_iommu_pre_enabled)
933 return false;
934
935 pr_warn("Translation is already enabled - trying to copy translation structures\n");
936 for_each_iommu(iommu) {
937 /* All IOMMUs should use the same device table with the same size */
938 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
939 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
940 entry = (((u64) hi) << 32) + lo;
941 if (last_entry && last_entry != entry) {
942 pr_err("IOMMU:%d should use the same dev table as others!\n",
943 iommu->index);
944 return false;
945 }
946 last_entry = entry;
947
948 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
949 if (old_devtb_size != dev_table_size) {
950 pr_err("The device table size of IOMMU:%d is not expected!\n",
951 iommu->index);
952 return false;
953 }
954 }
955
956 /*
957 * When SME is enabled in the first kernel, the entry includes the
958 * memory encryption mask(sme_me_mask), we must remove the memory
959 * encryption mask to obtain the true physical address in kdump kernel.
960 */
961 old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
962
963 if (old_devtb_phys >= 0x100000000ULL) {
964 pr_err("The address of old device table is above 4G, not trustworthy!\n");
965 return false;
966 }
967 old_devtb = (sme_active() && is_kdump_kernel())
968 ? (__force void *)ioremap_encrypted(old_devtb_phys,
969 dev_table_size)
970 : memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
971
972 if (!old_devtb)
973 return false;
974
975 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
976 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
977 get_order(dev_table_size));
978 if (old_dev_tbl_cpy == NULL) {
979 pr_err("Failed to allocate memory for copying old device table!\n");
980 return false;
981 }
982
983 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
984 old_dev_tbl_cpy[devid] = old_devtb[devid];
985 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
986 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
987
988 if (dte_v && dom_id) {
989 old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
990 old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
991 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
992 /* If gcr3 table existed, mask it out */
993 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
994 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
995 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
996 old_dev_tbl_cpy[devid].data[1] &= ~tmp;
997 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
998 tmp |= DTE_FLAG_GV;
999 old_dev_tbl_cpy[devid].data[0] &= ~tmp;
1000 }
1001 }
1002
1003 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
1004 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
1005 int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK;
1006 if (irq_v && (int_ctl || int_tab_len)) {
1007 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
1008 (int_tab_len != DTE_IRQ_TABLE_LEN)) {
1009 pr_err("Wrong old irq remapping flag: %#x\n", devid);
1010 return false;
1011 }
1012
1013 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
1014 }
1015 }
1016 memunmap(old_devtb);
1017
1018 return true;
1019 }
1020
amd_iommu_apply_erratum_63(u16 devid)1021 void amd_iommu_apply_erratum_63(u16 devid)
1022 {
1023 int sysmgt;
1024
1025 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
1026 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
1027
1028 if (sysmgt == 0x01)
1029 set_dev_entry_bit(devid, DEV_ENTRY_IW);
1030 }
1031
1032 /* Writes the specific IOMMU for a device into the rlookup table */
set_iommu_for_device(struct amd_iommu * iommu,u16 devid)1033 static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
1034 {
1035 amd_iommu_rlookup_table[devid] = iommu;
1036 }
1037
1038 /*
1039 * This function takes the device specific flags read from the ACPI
1040 * table and sets up the device table entry with that information
1041 */
set_dev_entry_from_acpi(struct amd_iommu * iommu,u16 devid,u32 flags,u32 ext_flags)1042 static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
1043 u16 devid, u32 flags, u32 ext_flags)
1044 {
1045 if (flags & ACPI_DEVFLAG_INITPASS)
1046 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
1047 if (flags & ACPI_DEVFLAG_EXTINT)
1048 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
1049 if (flags & ACPI_DEVFLAG_NMI)
1050 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
1051 if (flags & ACPI_DEVFLAG_SYSMGT1)
1052 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
1053 if (flags & ACPI_DEVFLAG_SYSMGT2)
1054 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
1055 if (flags & ACPI_DEVFLAG_LINT0)
1056 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
1057 if (flags & ACPI_DEVFLAG_LINT1)
1058 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
1059
1060 amd_iommu_apply_erratum_63(devid);
1061
1062 set_iommu_for_device(iommu, devid);
1063 }
1064
add_special_device(u8 type,u8 id,u16 * devid,bool cmd_line)1065 int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
1066 {
1067 struct devid_map *entry;
1068 struct list_head *list;
1069
1070 if (type == IVHD_SPECIAL_IOAPIC)
1071 list = &ioapic_map;
1072 else if (type == IVHD_SPECIAL_HPET)
1073 list = &hpet_map;
1074 else
1075 return -EINVAL;
1076
1077 list_for_each_entry(entry, list, list) {
1078 if (!(entry->id == id && entry->cmd_line))
1079 continue;
1080
1081 pr_info("Command-line override present for %s id %d - ignoring\n",
1082 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1083
1084 *devid = entry->devid;
1085
1086 return 0;
1087 }
1088
1089 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1090 if (!entry)
1091 return -ENOMEM;
1092
1093 entry->id = id;
1094 entry->devid = *devid;
1095 entry->cmd_line = cmd_line;
1096
1097 list_add_tail(&entry->list, list);
1098
1099 return 0;
1100 }
1101
add_acpi_hid_device(u8 * hid,u8 * uid,u16 * devid,bool cmd_line)1102 static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1103 bool cmd_line)
1104 {
1105 struct acpihid_map_entry *entry;
1106 struct list_head *list = &acpihid_map;
1107
1108 list_for_each_entry(entry, list, list) {
1109 if (strcmp(entry->hid, hid) ||
1110 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1111 !entry->cmd_line)
1112 continue;
1113
1114 pr_info("Command-line override for hid:%s uid:%s\n",
1115 hid, uid);
1116 *devid = entry->devid;
1117 return 0;
1118 }
1119
1120 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1121 if (!entry)
1122 return -ENOMEM;
1123
1124 memcpy(entry->uid, uid, strlen(uid));
1125 memcpy(entry->hid, hid, strlen(hid));
1126 entry->devid = *devid;
1127 entry->cmd_line = cmd_line;
1128 entry->root_devid = (entry->devid & (~0x7));
1129
1130 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
1131 entry->cmd_line ? "cmd" : "ivrs",
1132 entry->hid, entry->uid, entry->root_devid);
1133
1134 list_add_tail(&entry->list, list);
1135 return 0;
1136 }
1137
add_early_maps(void)1138 static int __init add_early_maps(void)
1139 {
1140 int i, ret;
1141
1142 for (i = 0; i < early_ioapic_map_size; ++i) {
1143 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1144 early_ioapic_map[i].id,
1145 &early_ioapic_map[i].devid,
1146 early_ioapic_map[i].cmd_line);
1147 if (ret)
1148 return ret;
1149 }
1150
1151 for (i = 0; i < early_hpet_map_size; ++i) {
1152 ret = add_special_device(IVHD_SPECIAL_HPET,
1153 early_hpet_map[i].id,
1154 &early_hpet_map[i].devid,
1155 early_hpet_map[i].cmd_line);
1156 if (ret)
1157 return ret;
1158 }
1159
1160 for (i = 0; i < early_acpihid_map_size; ++i) {
1161 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1162 early_acpihid_map[i].uid,
1163 &early_acpihid_map[i].devid,
1164 early_acpihid_map[i].cmd_line);
1165 if (ret)
1166 return ret;
1167 }
1168
1169 return 0;
1170 }
1171
1172 /*
1173 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1174 * initializes the hardware and our data structures with it.
1175 */
init_iommu_from_acpi(struct amd_iommu * iommu,struct ivhd_header * h)1176 static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
1177 struct ivhd_header *h)
1178 {
1179 u8 *p = (u8 *)h;
1180 u8 *end = p, flags = 0;
1181 u16 devid = 0, devid_start = 0, devid_to = 0;
1182 u32 dev_i, ext_flags = 0;
1183 bool alias = false;
1184 struct ivhd_entry *e;
1185 u32 ivhd_size;
1186 int ret;
1187
1188
1189 ret = add_early_maps();
1190 if (ret)
1191 return ret;
1192
1193 amd_iommu_apply_ivrs_quirks();
1194
1195 /*
1196 * First save the recommended feature enable bits from ACPI
1197 */
1198 iommu->acpi_flags = h->flags;
1199
1200 /*
1201 * Done. Now parse the device entries
1202 */
1203 ivhd_size = get_ivhd_header_size(h);
1204 if (!ivhd_size) {
1205 pr_err("Unsupported IVHD type %#x\n", h->type);
1206 return -EINVAL;
1207 }
1208
1209 p += ivhd_size;
1210
1211 end += h->length;
1212
1213
1214 while (p < end) {
1215 e = (struct ivhd_entry *)p;
1216 switch (e->type) {
1217 case IVHD_DEV_ALL:
1218
1219 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
1220
1221 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1222 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
1223 break;
1224 case IVHD_DEV_SELECT:
1225
1226 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1227 "flags: %02x\n",
1228 PCI_BUS_NUM(e->devid),
1229 PCI_SLOT(e->devid),
1230 PCI_FUNC(e->devid),
1231 e->flags);
1232
1233 devid = e->devid;
1234 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1235 break;
1236 case IVHD_DEV_SELECT_RANGE_START:
1237
1238 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1239 "devid: %02x:%02x.%x flags: %02x\n",
1240 PCI_BUS_NUM(e->devid),
1241 PCI_SLOT(e->devid),
1242 PCI_FUNC(e->devid),
1243 e->flags);
1244
1245 devid_start = e->devid;
1246 flags = e->flags;
1247 ext_flags = 0;
1248 alias = false;
1249 break;
1250 case IVHD_DEV_ALIAS:
1251
1252 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1253 "flags: %02x devid_to: %02x:%02x.%x\n",
1254 PCI_BUS_NUM(e->devid),
1255 PCI_SLOT(e->devid),
1256 PCI_FUNC(e->devid),
1257 e->flags,
1258 PCI_BUS_NUM(e->ext >> 8),
1259 PCI_SLOT(e->ext >> 8),
1260 PCI_FUNC(e->ext >> 8));
1261
1262 devid = e->devid;
1263 devid_to = e->ext >> 8;
1264 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
1265 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
1266 amd_iommu_alias_table[devid] = devid_to;
1267 break;
1268 case IVHD_DEV_ALIAS_RANGE:
1269
1270 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1271 "devid: %02x:%02x.%x flags: %02x "
1272 "devid_to: %02x:%02x.%x\n",
1273 PCI_BUS_NUM(e->devid),
1274 PCI_SLOT(e->devid),
1275 PCI_FUNC(e->devid),
1276 e->flags,
1277 PCI_BUS_NUM(e->ext >> 8),
1278 PCI_SLOT(e->ext >> 8),
1279 PCI_FUNC(e->ext >> 8));
1280
1281 devid_start = e->devid;
1282 flags = e->flags;
1283 devid_to = e->ext >> 8;
1284 ext_flags = 0;
1285 alias = true;
1286 break;
1287 case IVHD_DEV_EXT_SELECT:
1288
1289 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1290 "flags: %02x ext: %08x\n",
1291 PCI_BUS_NUM(e->devid),
1292 PCI_SLOT(e->devid),
1293 PCI_FUNC(e->devid),
1294 e->flags, e->ext);
1295
1296 devid = e->devid;
1297 set_dev_entry_from_acpi(iommu, devid, e->flags,
1298 e->ext);
1299 break;
1300 case IVHD_DEV_EXT_SELECT_RANGE:
1301
1302 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1303 "%02x:%02x.%x flags: %02x ext: %08x\n",
1304 PCI_BUS_NUM(e->devid),
1305 PCI_SLOT(e->devid),
1306 PCI_FUNC(e->devid),
1307 e->flags, e->ext);
1308
1309 devid_start = e->devid;
1310 flags = e->flags;
1311 ext_flags = e->ext;
1312 alias = false;
1313 break;
1314 case IVHD_DEV_RANGE_END:
1315
1316 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
1317 PCI_BUS_NUM(e->devid),
1318 PCI_SLOT(e->devid),
1319 PCI_FUNC(e->devid));
1320
1321 devid = e->devid;
1322 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
1323 if (alias) {
1324 amd_iommu_alias_table[dev_i] = devid_to;
1325 set_dev_entry_from_acpi(iommu,
1326 devid_to, flags, ext_flags);
1327 }
1328 set_dev_entry_from_acpi(iommu, dev_i,
1329 flags, ext_flags);
1330 }
1331 break;
1332 case IVHD_DEV_SPECIAL: {
1333 u8 handle, type;
1334 const char *var;
1335 u16 devid;
1336 int ret;
1337
1338 handle = e->ext & 0xff;
1339 devid = (e->ext >> 8) & 0xffff;
1340 type = (e->ext >> 24) & 0xff;
1341
1342 if (type == IVHD_SPECIAL_IOAPIC)
1343 var = "IOAPIC";
1344 else if (type == IVHD_SPECIAL_HPET)
1345 var = "HPET";
1346 else
1347 var = "UNKNOWN";
1348
1349 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1350 var, (int)handle,
1351 PCI_BUS_NUM(devid),
1352 PCI_SLOT(devid),
1353 PCI_FUNC(devid));
1354
1355 ret = add_special_device(type, handle, &devid, false);
1356 if (ret)
1357 return ret;
1358
1359 /*
1360 * add_special_device might update the devid in case a
1361 * command-line override is present. So call
1362 * set_dev_entry_from_acpi after add_special_device.
1363 */
1364 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1365
1366 break;
1367 }
1368 case IVHD_DEV_ACPI_HID: {
1369 u16 devid;
1370 u8 hid[ACPIHID_HID_LEN];
1371 u8 uid[ACPIHID_UID_LEN];
1372 int ret;
1373
1374 if (h->type != 0x40) {
1375 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1376 e->type);
1377 break;
1378 }
1379
1380 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1381 hid[ACPIHID_HID_LEN - 1] = '\0';
1382
1383 if (!(*hid)) {
1384 pr_err(FW_BUG "Invalid HID.\n");
1385 break;
1386 }
1387
1388 uid[0] = '\0';
1389 switch (e->uidf) {
1390 case UID_NOT_PRESENT:
1391
1392 if (e->uidl != 0)
1393 pr_warn(FW_BUG "Invalid UID length.\n");
1394
1395 break;
1396 case UID_IS_INTEGER:
1397
1398 sprintf(uid, "%d", e->uid);
1399
1400 break;
1401 case UID_IS_CHARACTER:
1402
1403 memcpy(uid, &e->uid, e->uidl);
1404 uid[e->uidl] = '\0';
1405
1406 break;
1407 default:
1408 break;
1409 }
1410
1411 devid = e->devid;
1412 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1413 hid, uid,
1414 PCI_BUS_NUM(devid),
1415 PCI_SLOT(devid),
1416 PCI_FUNC(devid));
1417
1418 flags = e->flags;
1419
1420 ret = add_acpi_hid_device(hid, uid, &devid, false);
1421 if (ret)
1422 return ret;
1423
1424 /*
1425 * add_special_device might update the devid in case a
1426 * command-line override is present. So call
1427 * set_dev_entry_from_acpi after add_special_device.
1428 */
1429 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1430
1431 break;
1432 }
1433 default:
1434 break;
1435 }
1436
1437 p += ivhd_entry_length(p);
1438 }
1439
1440 return 0;
1441 }
1442
free_iommu_one(struct amd_iommu * iommu)1443 static void __init free_iommu_one(struct amd_iommu *iommu)
1444 {
1445 free_cwwb_sem(iommu);
1446 free_command_buffer(iommu);
1447 free_event_buffer(iommu);
1448 free_ppr_log(iommu);
1449 free_ga_log(iommu);
1450 iommu_unmap_mmio_space(iommu);
1451 }
1452
free_iommu_all(void)1453 static void __init free_iommu_all(void)
1454 {
1455 struct amd_iommu *iommu, *next;
1456
1457 for_each_iommu_safe(iommu, next) {
1458 list_del(&iommu->list);
1459 free_iommu_one(iommu);
1460 kfree(iommu);
1461 }
1462 }
1463
1464 /*
1465 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1466 * Workaround:
1467 * BIOS should disable L2B micellaneous clock gating by setting
1468 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1469 */
amd_iommu_erratum_746_workaround(struct amd_iommu * iommu)1470 static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
1471 {
1472 u32 value;
1473
1474 if ((boot_cpu_data.x86 != 0x15) ||
1475 (boot_cpu_data.x86_model < 0x10) ||
1476 (boot_cpu_data.x86_model > 0x1f))
1477 return;
1478
1479 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1480 pci_read_config_dword(iommu->dev, 0xf4, &value);
1481
1482 if (value & BIT(2))
1483 return;
1484
1485 /* Select NB indirect register 0x90 and enable writing */
1486 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1487
1488 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1489 pci_info(iommu->dev, "Applying erratum 746 workaround\n");
1490
1491 /* Clear the enable writing bit */
1492 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1493 }
1494
1495 /*
1496 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1497 * Workaround:
1498 * BIOS should enable ATS write permission check by setting
1499 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1500 */
amd_iommu_ats_write_check_workaround(struct amd_iommu * iommu)1501 static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1502 {
1503 u32 value;
1504
1505 if ((boot_cpu_data.x86 != 0x15) ||
1506 (boot_cpu_data.x86_model < 0x30) ||
1507 (boot_cpu_data.x86_model > 0x3f))
1508 return;
1509
1510 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1511 value = iommu_read_l2(iommu, 0x47);
1512
1513 if (value & BIT(0))
1514 return;
1515
1516 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1517 iommu_write_l2(iommu, 0x47, value | BIT(0));
1518
1519 pci_info(iommu->dev, "Applying ATS write check workaround\n");
1520 }
1521
1522 /*
1523 * This function clues the initialization function for one IOMMU
1524 * together and also allocates the command buffer and programs the
1525 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1526 */
init_iommu_one(struct amd_iommu * iommu,struct ivhd_header * h)1527 static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1528 {
1529 int ret;
1530
1531 raw_spin_lock_init(&iommu->lock);
1532 iommu->cmd_sem_val = 0;
1533
1534 /* Add IOMMU to internal data structures */
1535 list_add_tail(&iommu->list, &amd_iommu_list);
1536 iommu->index = amd_iommus_present++;
1537
1538 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1539 WARN(1, "System has more IOMMUs than supported by this driver\n");
1540 return -ENOSYS;
1541 }
1542
1543 /* Index is fine - add IOMMU to the array */
1544 amd_iommus[iommu->index] = iommu;
1545
1546 /*
1547 * Copy data from ACPI table entry to the iommu struct
1548 */
1549 iommu->devid = h->devid;
1550 iommu->cap_ptr = h->cap_ptr;
1551 iommu->pci_seg = h->pci_seg;
1552 iommu->mmio_phys = h->mmio_phys;
1553
1554 switch (h->type) {
1555 case 0x10:
1556 /* Check if IVHD EFR contains proper max banks/counters */
1557 if ((h->efr_attr != 0) &&
1558 ((h->efr_attr & (0xF << 13)) != 0) &&
1559 ((h->efr_attr & (0x3F << 17)) != 0))
1560 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1561 else
1562 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1563
1564 /*
1565 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1566 * GAM also requires GA mode. Therefore, we need to
1567 * check cmpxchg16b support before enabling it.
1568 */
1569 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1570 ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1571 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1572 break;
1573 case 0x11:
1574 case 0x40:
1575 if (h->efr_reg & (1 << 9))
1576 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1577 else
1578 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
1579
1580 /*
1581 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1582 * XT, GAM also requires GA mode. Therefore, we need to
1583 * check cmpxchg16b support before enabling them.
1584 */
1585 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1586 ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0)) {
1587 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
1588 break;
1589 }
1590
1591 /*
1592 * Note: Since iommu_update_intcapxt() leverages
1593 * the IOMMU MMIO access to MSI capability block registers
1594 * for MSI address lo/hi/data, we need to check both
1595 * EFR[XtSup] and EFR[MsiCapMmioSup] for x2APIC support.
1596 */
1597 if ((h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT)) &&
1598 (h->efr_reg & BIT(IOMMU_EFR_MSICAPMMIOSUP_SHIFT)))
1599 amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
1600
1601 early_iommu_features_init(iommu, h);
1602
1603 break;
1604 default:
1605 return -EINVAL;
1606 }
1607
1608 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1609 iommu->mmio_phys_end);
1610 if (!iommu->mmio_base)
1611 return -ENOMEM;
1612
1613 if (alloc_cwwb_sem(iommu))
1614 return -ENOMEM;
1615
1616 if (alloc_command_buffer(iommu))
1617 return -ENOMEM;
1618
1619 if (alloc_event_buffer(iommu))
1620 return -ENOMEM;
1621
1622 iommu->int_enabled = false;
1623
1624 init_translation_status(iommu);
1625 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1626 iommu_disable(iommu);
1627 clear_translation_pre_enabled(iommu);
1628 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1629 iommu->index);
1630 }
1631 if (amd_iommu_pre_enabled)
1632 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
1633
1634 ret = init_iommu_from_acpi(iommu, h);
1635 if (ret)
1636 return ret;
1637
1638 ret = amd_iommu_create_irq_domain(iommu);
1639 if (ret)
1640 return ret;
1641
1642 /*
1643 * Make sure IOMMU is not considered to translate itself. The IVRS
1644 * table tells us so, but this is a lie!
1645 */
1646 amd_iommu_rlookup_table[iommu->devid] = NULL;
1647
1648 return 0;
1649 }
1650
1651 /**
1652 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1653 * @ivrs: Pointer to the IVRS header
1654 *
1655 * This function search through all IVDB of the maximum supported IVHD
1656 */
get_highest_supported_ivhd_type(struct acpi_table_header * ivrs)1657 static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1658 {
1659 u8 *base = (u8 *)ivrs;
1660 struct ivhd_header *ivhd = (struct ivhd_header *)
1661 (base + IVRS_HEADER_LENGTH);
1662 u8 last_type = ivhd->type;
1663 u16 devid = ivhd->devid;
1664
1665 while (((u8 *)ivhd - base < ivrs->length) &&
1666 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1667 u8 *p = (u8 *) ivhd;
1668
1669 if (ivhd->devid == devid)
1670 last_type = ivhd->type;
1671 ivhd = (struct ivhd_header *)(p + ivhd->length);
1672 }
1673
1674 return last_type;
1675 }
1676
1677 /*
1678 * Iterates over all IOMMU entries in the ACPI table, allocates the
1679 * IOMMU structure and initializes it with init_iommu_one()
1680 */
init_iommu_all(struct acpi_table_header * table)1681 static int __init init_iommu_all(struct acpi_table_header *table)
1682 {
1683 u8 *p = (u8 *)table, *end = (u8 *)table;
1684 struct ivhd_header *h;
1685 struct amd_iommu *iommu;
1686 int ret;
1687
1688 end += table->length;
1689 p += IVRS_HEADER_LENGTH;
1690
1691 while (p < end) {
1692 h = (struct ivhd_header *)p;
1693 if (*p == amd_iommu_target_ivhd_type) {
1694
1695 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
1696 "seg: %d flags: %01x info %04x\n",
1697 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
1698 PCI_FUNC(h->devid), h->cap_ptr,
1699 h->pci_seg, h->flags, h->info);
1700 DUMP_printk(" mmio-addr: %016llx\n",
1701 h->mmio_phys);
1702
1703 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
1704 if (iommu == NULL)
1705 return -ENOMEM;
1706
1707 ret = init_iommu_one(iommu, h);
1708 if (ret)
1709 return ret;
1710 }
1711 p += h->length;
1712
1713 }
1714 WARN_ON(p != end);
1715
1716 return 0;
1717 }
1718
init_iommu_perf_ctr(struct amd_iommu * iommu)1719 static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1720 {
1721 u64 val;
1722 struct pci_dev *pdev = iommu->dev;
1723
1724 if (!iommu_feature(iommu, FEATURE_PC))
1725 return;
1726
1727 amd_iommu_pc_present = true;
1728
1729 pci_info(pdev, "IOMMU performance counters supported\n");
1730
1731 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1732 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1733 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1734
1735 return;
1736 }
1737
amd_iommu_show_cap(struct device * dev,struct device_attribute * attr,char * buf)1738 static ssize_t amd_iommu_show_cap(struct device *dev,
1739 struct device_attribute *attr,
1740 char *buf)
1741 {
1742 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1743 return sprintf(buf, "%x\n", iommu->cap);
1744 }
1745 static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1746
amd_iommu_show_features(struct device * dev,struct device_attribute * attr,char * buf)1747 static ssize_t amd_iommu_show_features(struct device *dev,
1748 struct device_attribute *attr,
1749 char *buf)
1750 {
1751 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
1752 return sprintf(buf, "%llx\n", iommu->features);
1753 }
1754 static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1755
1756 static struct attribute *amd_iommu_attrs[] = {
1757 &dev_attr_cap.attr,
1758 &dev_attr_features.attr,
1759 NULL,
1760 };
1761
1762 static struct attribute_group amd_iommu_group = {
1763 .name = "amd-iommu",
1764 .attrs = amd_iommu_attrs,
1765 };
1766
1767 static const struct attribute_group *amd_iommu_groups[] = {
1768 &amd_iommu_group,
1769 NULL,
1770 };
1771
1772 /*
1773 * Note: IVHD 0x11 and 0x40 also contains exact copy
1774 * of the IOMMU Extended Feature Register [MMIO Offset 0030h].
1775 * Default to EFR in IVHD since it is available sooner (i.e. before PCI init).
1776 */
late_iommu_features_init(struct amd_iommu * iommu)1777 static void __init late_iommu_features_init(struct amd_iommu *iommu)
1778 {
1779 u64 features;
1780
1781 if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
1782 return;
1783
1784 /* read extended feature bits */
1785 features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
1786
1787 if (!iommu->features) {
1788 iommu->features = features;
1789 return;
1790 }
1791
1792 /*
1793 * Sanity check and warn if EFR values from
1794 * IVHD and MMIO conflict.
1795 */
1796 if (features != iommu->features)
1797 pr_warn(FW_WARN "EFR mismatch. Use IVHD EFR (%#llx : %#llx).\n",
1798 features, iommu->features);
1799 }
1800
iommu_init_pci(struct amd_iommu * iommu)1801 static int __init iommu_init_pci(struct amd_iommu *iommu)
1802 {
1803 int cap_ptr = iommu->cap_ptr;
1804 int ret;
1805
1806 iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
1807 iommu->devid & 0xff);
1808 if (!iommu->dev)
1809 return -ENODEV;
1810
1811 /* Prevent binding other PCI device drivers to IOMMU devices */
1812 iommu->dev->match_driver = false;
1813
1814 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1815 &iommu->cap);
1816
1817 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1818 amd_iommu_iotlb_sup = false;
1819
1820 late_iommu_features_init(iommu);
1821
1822 if (iommu_feature(iommu, FEATURE_GT)) {
1823 int glxval;
1824 u32 max_pasid;
1825 u64 pasmax;
1826
1827 pasmax = iommu->features & FEATURE_PASID_MASK;
1828 pasmax >>= FEATURE_PASID_SHIFT;
1829 max_pasid = (1 << (pasmax + 1)) - 1;
1830
1831 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1832
1833 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
1834
1835 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1836 glxval >>= FEATURE_GLXVAL_SHIFT;
1837
1838 if (amd_iommu_max_glx_val == -1)
1839 amd_iommu_max_glx_val = glxval;
1840 else
1841 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1842 }
1843
1844 if (iommu_feature(iommu, FEATURE_GT) &&
1845 iommu_feature(iommu, FEATURE_PPR)) {
1846 iommu->is_iommu_v2 = true;
1847 amd_iommu_v2_present = true;
1848 }
1849
1850 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1851 return -ENOMEM;
1852
1853 ret = iommu_init_ga_log(iommu);
1854 if (ret)
1855 return ret;
1856
1857 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1858 amd_iommu_np_cache = true;
1859
1860 init_iommu_perf_ctr(iommu);
1861
1862 if (is_rd890_iommu(iommu->dev)) {
1863 int i, j;
1864
1865 iommu->root_pdev =
1866 pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
1867 PCI_DEVFN(0, 0));
1868
1869 /*
1870 * Some rd890 systems may not be fully reconfigured by the
1871 * BIOS, so it's necessary for us to store this information so
1872 * it can be reprogrammed on resume
1873 */
1874 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1875 &iommu->stored_addr_lo);
1876 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1877 &iommu->stored_addr_hi);
1878
1879 /* Low bit locks writes to configuration space */
1880 iommu->stored_addr_lo &= ~1;
1881
1882 for (i = 0; i < 6; i++)
1883 for (j = 0; j < 0x12; j++)
1884 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1885
1886 for (i = 0; i < 0x83; i++)
1887 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1888 }
1889
1890 amd_iommu_erratum_746_workaround(iommu);
1891 amd_iommu_ats_write_check_workaround(iommu);
1892
1893 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1894 amd_iommu_groups, "ivhd%d", iommu->index);
1895 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1896 iommu_device_register(&iommu->iommu);
1897
1898 return pci_enable_device(iommu->dev);
1899 }
1900
print_iommu_info(void)1901 static void print_iommu_info(void)
1902 {
1903 static const char * const feat_str[] = {
1904 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1905 "IA", "GA", "HE", "PC"
1906 };
1907 struct amd_iommu *iommu;
1908
1909 for_each_iommu(iommu) {
1910 struct pci_dev *pdev = iommu->dev;
1911 int i;
1912
1913 pci_info(pdev, "Found IOMMU cap 0x%hx\n", iommu->cap_ptr);
1914
1915 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1916 pr_info("Extended features (%#llx):", iommu->features);
1917
1918 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
1919 if (iommu_feature(iommu, (1ULL << i)))
1920 pr_cont(" %s", feat_str[i]);
1921 }
1922
1923 if (iommu->features & FEATURE_GAM_VAPIC)
1924 pr_cont(" GA_vAPIC");
1925
1926 pr_cont("\n");
1927 }
1928 }
1929 if (irq_remapping_enabled) {
1930 pr_info("Interrupt remapping enabled\n");
1931 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1932 pr_info("Virtual APIC enabled\n");
1933 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
1934 pr_info("X2APIC enabled\n");
1935 }
1936 }
1937
amd_iommu_init_pci(void)1938 static int __init amd_iommu_init_pci(void)
1939 {
1940 struct amd_iommu *iommu;
1941 int ret = 0;
1942
1943 for_each_iommu(iommu) {
1944 ret = iommu_init_pci(iommu);
1945 if (ret)
1946 break;
1947
1948 /* Need to setup range after PCI init */
1949 iommu_set_cwwb_range(iommu);
1950 }
1951
1952 /*
1953 * Order is important here to make sure any unity map requirements are
1954 * fulfilled. The unity mappings are created and written to the device
1955 * table during the amd_iommu_init_api() call.
1956 *
1957 * After that we call init_device_table_dma() to make sure any
1958 * uninitialized DTE will block DMA, and in the end we flush the caches
1959 * of all IOMMUs to make sure the changes to the device table are
1960 * active.
1961 */
1962 ret = amd_iommu_init_api();
1963
1964 init_device_table_dma();
1965
1966 for_each_iommu(iommu)
1967 iommu_flush_all_caches(iommu);
1968
1969 if (!ret)
1970 print_iommu_info();
1971
1972 return ret;
1973 }
1974
1975 /****************************************************************************
1976 *
1977 * The following functions initialize the MSI interrupts for all IOMMUs
1978 * in the system. It's a bit challenging because there could be multiple
1979 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1980 * pci_dev.
1981 *
1982 ****************************************************************************/
1983
iommu_setup_msi(struct amd_iommu * iommu)1984 static int iommu_setup_msi(struct amd_iommu *iommu)
1985 {
1986 int r;
1987
1988 r = pci_enable_msi(iommu->dev);
1989 if (r)
1990 return r;
1991
1992 r = request_threaded_irq(iommu->dev->irq,
1993 amd_iommu_int_handler,
1994 amd_iommu_int_thread,
1995 0, "AMD-Vi",
1996 iommu);
1997
1998 if (r) {
1999 pci_disable_msi(iommu->dev);
2000 return r;
2001 }
2002
2003 iommu->int_enabled = true;
2004
2005 return 0;
2006 }
2007
2008 #define XT_INT_DEST_MODE(x) (((x) & 0x1ULL) << 2)
2009 #define XT_INT_DEST_LO(x) (((x) & 0xFFFFFFULL) << 8)
2010 #define XT_INT_VEC(x) (((x) & 0xFFULL) << 32)
2011 #define XT_INT_DEST_HI(x) ((((x) >> 24) & 0xFFULL) << 56)
2012
2013 /*
2014 * Setup the IntCapXT registers with interrupt routing information
2015 * based on the PCI MSI capability block registers, accessed via
2016 * MMIO MSI address low/hi and MSI data registers.
2017 */
iommu_update_intcapxt(struct amd_iommu * iommu)2018 static void iommu_update_intcapxt(struct amd_iommu *iommu)
2019 {
2020 u64 val;
2021 u32 addr_lo = readl(iommu->mmio_base + MMIO_MSI_ADDR_LO_OFFSET);
2022 u32 addr_hi = readl(iommu->mmio_base + MMIO_MSI_ADDR_HI_OFFSET);
2023 u32 data = readl(iommu->mmio_base + MMIO_MSI_DATA_OFFSET);
2024 bool dm = (addr_lo >> MSI_ADDR_DEST_MODE_SHIFT) & 0x1;
2025 u32 dest = ((addr_lo >> MSI_ADDR_DEST_ID_SHIFT) & 0xFF);
2026
2027 if (x2apic_enabled())
2028 dest |= MSI_ADDR_EXT_DEST_ID(addr_hi);
2029
2030 val = XT_INT_VEC(data & 0xFF) |
2031 XT_INT_DEST_MODE(dm) |
2032 XT_INT_DEST_LO(dest) |
2033 XT_INT_DEST_HI(dest);
2034
2035 /**
2036 * Current IOMMU implemtation uses the same IRQ for all
2037 * 3 IOMMU interrupts.
2038 */
2039 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
2040 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
2041 writeq(val, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
2042 }
2043
_irq_notifier_notify(struct irq_affinity_notify * notify,const cpumask_t * mask)2044 static void _irq_notifier_notify(struct irq_affinity_notify *notify,
2045 const cpumask_t *mask)
2046 {
2047 struct amd_iommu *iommu;
2048
2049 for_each_iommu(iommu) {
2050 if (iommu->dev->irq == notify->irq) {
2051 iommu_update_intcapxt(iommu);
2052 break;
2053 }
2054 }
2055 }
2056
_irq_notifier_release(struct kref * ref)2057 static void _irq_notifier_release(struct kref *ref)
2058 {
2059 }
2060
iommu_init_intcapxt(struct amd_iommu * iommu)2061 static int iommu_init_intcapxt(struct amd_iommu *iommu)
2062 {
2063 int ret;
2064 struct irq_affinity_notify *notify = &iommu->intcapxt_notify;
2065
2066 /**
2067 * IntCapXT requires XTSup=1 and MsiCapMmioSup=1,
2068 * which can be inferred from amd_iommu_xt_mode.
2069 */
2070 if (amd_iommu_xt_mode != IRQ_REMAP_X2APIC_MODE)
2071 return 0;
2072
2073 /**
2074 * Also, we need to setup notifier to update the IntCapXT registers
2075 * whenever the irq affinity is changed from user-space.
2076 */
2077 notify->irq = iommu->dev->irq;
2078 notify->notify = _irq_notifier_notify,
2079 notify->release = _irq_notifier_release,
2080 ret = irq_set_affinity_notifier(iommu->dev->irq, notify);
2081 if (ret) {
2082 pr_err("Failed to register irq affinity notifier (devid=%#x, irq %d)\n",
2083 iommu->devid, iommu->dev->irq);
2084 return ret;
2085 }
2086
2087 iommu_update_intcapxt(iommu);
2088 iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
2089 return ret;
2090 }
2091
iommu_init_msi(struct amd_iommu * iommu)2092 static int iommu_init_msi(struct amd_iommu *iommu)
2093 {
2094 int ret;
2095
2096 if (iommu->int_enabled)
2097 goto enable_faults;
2098
2099 if (iommu->dev->msi_cap)
2100 ret = iommu_setup_msi(iommu);
2101 else
2102 ret = -ENODEV;
2103
2104 if (ret)
2105 return ret;
2106
2107 enable_faults:
2108 ret = iommu_init_intcapxt(iommu);
2109 if (ret)
2110 return ret;
2111
2112 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
2113
2114 if (iommu->ppr_log != NULL)
2115 iommu_feature_enable(iommu, CONTROL_PPRINT_EN);
2116
2117 iommu_ga_log_enable(iommu);
2118
2119 return 0;
2120 }
2121
2122 /****************************************************************************
2123 *
2124 * The next functions belong to the third pass of parsing the ACPI
2125 * table. In this last pass the memory mapping requirements are
2126 * gathered (like exclusion and unity mapping ranges).
2127 *
2128 ****************************************************************************/
2129
free_unity_maps(void)2130 static void __init free_unity_maps(void)
2131 {
2132 struct unity_map_entry *entry, *next;
2133
2134 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
2135 list_del(&entry->list);
2136 kfree(entry);
2137 }
2138 }
2139
2140 /* called for unity map ACPI definition */
init_unity_map_range(struct ivmd_header * m)2141 static int __init init_unity_map_range(struct ivmd_header *m)
2142 {
2143 struct unity_map_entry *e = NULL;
2144 char *s;
2145
2146 e = kzalloc(sizeof(*e), GFP_KERNEL);
2147 if (e == NULL)
2148 return -ENOMEM;
2149
2150 switch (m->type) {
2151 default:
2152 kfree(e);
2153 return 0;
2154 case ACPI_IVMD_TYPE:
2155 s = "IVMD_TYPEi\t\t\t";
2156 e->devid_start = e->devid_end = m->devid;
2157 break;
2158 case ACPI_IVMD_TYPE_ALL:
2159 s = "IVMD_TYPE_ALL\t\t";
2160 e->devid_start = 0;
2161 e->devid_end = amd_iommu_last_bdf;
2162 break;
2163 case ACPI_IVMD_TYPE_RANGE:
2164 s = "IVMD_TYPE_RANGE\t\t";
2165 e->devid_start = m->devid;
2166 e->devid_end = m->aux;
2167 break;
2168 }
2169 e->address_start = PAGE_ALIGN(m->range_start);
2170 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2171 e->prot = m->flags >> 1;
2172
2173 /*
2174 * Treat per-device exclusion ranges as r/w unity-mapped regions
2175 * since some buggy BIOSes might lead to the overwritten exclusion
2176 * range (exclusion_start and exclusion_length members). This
2177 * happens when there are multiple exclusion ranges (IVMD entries)
2178 * defined in ACPI table.
2179 */
2180 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2181 e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
2182
2183 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2184 " range_start: %016llx range_end: %016llx flags: %x\n", s,
2185 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2186 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
2187 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2188 e->address_start, e->address_end, m->flags);
2189
2190 list_add_tail(&e->list, &amd_iommu_unity_map);
2191
2192 return 0;
2193 }
2194
2195 /* iterates over all memory definitions we find in the ACPI table */
init_memory_definitions(struct acpi_table_header * table)2196 static int __init init_memory_definitions(struct acpi_table_header *table)
2197 {
2198 u8 *p = (u8 *)table, *end = (u8 *)table;
2199 struct ivmd_header *m;
2200
2201 end += table->length;
2202 p += IVRS_HEADER_LENGTH;
2203
2204 while (p < end) {
2205 m = (struct ivmd_header *)p;
2206 if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
2207 init_unity_map_range(m);
2208
2209 p += m->length;
2210 }
2211
2212 return 0;
2213 }
2214
2215 /*
2216 * Init the device table to not allow DMA access for devices
2217 */
init_device_table_dma(void)2218 static void init_device_table_dma(void)
2219 {
2220 u32 devid;
2221
2222 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2223 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2224 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
2225 }
2226 }
2227
uninit_device_table_dma(void)2228 static void __init uninit_device_table_dma(void)
2229 {
2230 u32 devid;
2231
2232 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2233 amd_iommu_dev_table[devid].data[0] = 0ULL;
2234 amd_iommu_dev_table[devid].data[1] = 0ULL;
2235 }
2236 }
2237
init_device_table(void)2238 static void init_device_table(void)
2239 {
2240 u32 devid;
2241
2242 if (!amd_iommu_irq_remap)
2243 return;
2244
2245 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2246 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2247 }
2248
iommu_init_flags(struct amd_iommu * iommu)2249 static void iommu_init_flags(struct amd_iommu *iommu)
2250 {
2251 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2252 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2253 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2254
2255 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2256 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2257 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2258
2259 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2260 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2261 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2262
2263 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2264 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2265 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2266
2267 /*
2268 * make IOMMU memory accesses cache coherent
2269 */
2270 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
2271
2272 /* Set IOTLB invalidation timeout to 1s */
2273 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
2274 }
2275
iommu_apply_resume_quirks(struct amd_iommu * iommu)2276 static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
2277 {
2278 int i, j;
2279 u32 ioc_feature_control;
2280 struct pci_dev *pdev = iommu->root_pdev;
2281
2282 /* RD890 BIOSes may not have completely reconfigured the iommu */
2283 if (!is_rd890_iommu(iommu->dev) || !pdev)
2284 return;
2285
2286 /*
2287 * First, we need to ensure that the iommu is enabled. This is
2288 * controlled by a register in the northbridge
2289 */
2290
2291 /* Select Northbridge indirect register 0x75 and enable writing */
2292 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2293 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2294
2295 /* Enable the iommu */
2296 if (!(ioc_feature_control & 0x1))
2297 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2298
2299 /* Restore the iommu BAR */
2300 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2301 iommu->stored_addr_lo);
2302 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2303 iommu->stored_addr_hi);
2304
2305 /* Restore the l1 indirect regs for each of the 6 l1s */
2306 for (i = 0; i < 6; i++)
2307 for (j = 0; j < 0x12; j++)
2308 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2309
2310 /* Restore the l2 indirect regs */
2311 for (i = 0; i < 0x83; i++)
2312 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2313
2314 /* Lock PCI setup registers */
2315 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2316 iommu->stored_addr_lo | 1);
2317 }
2318
iommu_enable_ga(struct amd_iommu * iommu)2319 static void iommu_enable_ga(struct amd_iommu *iommu)
2320 {
2321 #ifdef CONFIG_IRQ_REMAP
2322 switch (amd_iommu_guest_ir) {
2323 case AMD_IOMMU_GUEST_IR_VAPIC:
2324 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2325 fallthrough;
2326 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2327 iommu_feature_enable(iommu, CONTROL_GA_EN);
2328 iommu->irte_ops = &irte_128_ops;
2329 break;
2330 default:
2331 iommu->irte_ops = &irte_32_ops;
2332 break;
2333 }
2334 #endif
2335 }
2336
early_enable_iommu(struct amd_iommu * iommu)2337 static void early_enable_iommu(struct amd_iommu *iommu)
2338 {
2339 iommu_disable(iommu);
2340 iommu_init_flags(iommu);
2341 iommu_set_device_table(iommu);
2342 iommu_enable_command_buffer(iommu);
2343 iommu_enable_event_buffer(iommu);
2344 iommu_set_exclusion_range(iommu);
2345 iommu_enable_ga(iommu);
2346 iommu_enable_xt(iommu);
2347 iommu_enable(iommu);
2348 iommu_flush_all_caches(iommu);
2349 }
2350
2351 /*
2352 * This function finally enables all IOMMUs found in the system after
2353 * they have been initialized.
2354 *
2355 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2356 * the old content of device table entries. Not this case or copy failed,
2357 * just continue as normal kernel does.
2358 */
early_enable_iommus(void)2359 static void early_enable_iommus(void)
2360 {
2361 struct amd_iommu *iommu;
2362
2363
2364 if (!copy_device_table()) {
2365 /*
2366 * If come here because of failure in copying device table from old
2367 * kernel with all IOMMUs enabled, print error message and try to
2368 * free allocated old_dev_tbl_cpy.
2369 */
2370 if (amd_iommu_pre_enabled)
2371 pr_err("Failed to copy DEV table from previous kernel.\n");
2372 if (old_dev_tbl_cpy != NULL)
2373 free_pages((unsigned long)old_dev_tbl_cpy,
2374 get_order(dev_table_size));
2375
2376 for_each_iommu(iommu) {
2377 clear_translation_pre_enabled(iommu);
2378 early_enable_iommu(iommu);
2379 }
2380 } else {
2381 pr_info("Copied DEV table from previous kernel.\n");
2382 free_pages((unsigned long)amd_iommu_dev_table,
2383 get_order(dev_table_size));
2384 amd_iommu_dev_table = old_dev_tbl_cpy;
2385 for_each_iommu(iommu) {
2386 iommu_disable_command_buffer(iommu);
2387 iommu_disable_event_buffer(iommu);
2388 iommu_enable_command_buffer(iommu);
2389 iommu_enable_event_buffer(iommu);
2390 iommu_enable_ga(iommu);
2391 iommu_enable_xt(iommu);
2392 iommu_set_device_table(iommu);
2393 iommu_flush_all_caches(iommu);
2394 }
2395 }
2396
2397 #ifdef CONFIG_IRQ_REMAP
2398 /*
2399 * Note: We have already checked GASup from IVRS table.
2400 * Now, we need to make sure that GAMSup is set.
2401 */
2402 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
2403 !check_feature_on_all_iommus(FEATURE_GAM_VAPIC))
2404 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
2405
2406 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2407 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2408 #endif
2409 }
2410
enable_iommus_v2(void)2411 static void enable_iommus_v2(void)
2412 {
2413 struct amd_iommu *iommu;
2414
2415 for_each_iommu(iommu) {
2416 iommu_enable_ppr_log(iommu);
2417 iommu_enable_gt(iommu);
2418 }
2419 }
2420
enable_iommus(void)2421 static void enable_iommus(void)
2422 {
2423 early_enable_iommus();
2424
2425 enable_iommus_v2();
2426 }
2427
disable_iommus(void)2428 static void disable_iommus(void)
2429 {
2430 struct amd_iommu *iommu;
2431
2432 for_each_iommu(iommu)
2433 iommu_disable(iommu);
2434
2435 #ifdef CONFIG_IRQ_REMAP
2436 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2437 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2438 #endif
2439 }
2440
2441 /*
2442 * Suspend/Resume support
2443 * disable suspend until real resume implemented
2444 */
2445
amd_iommu_resume(void)2446 static void amd_iommu_resume(void)
2447 {
2448 struct amd_iommu *iommu;
2449
2450 for_each_iommu(iommu)
2451 iommu_apply_resume_quirks(iommu);
2452
2453 /* re-load the hardware */
2454 enable_iommus();
2455
2456 amd_iommu_enable_interrupts();
2457 }
2458
amd_iommu_suspend(void)2459 static int amd_iommu_suspend(void)
2460 {
2461 /* disable IOMMUs to go out of the way for BIOS */
2462 disable_iommus();
2463
2464 return 0;
2465 }
2466
2467 static struct syscore_ops amd_iommu_syscore_ops = {
2468 .suspend = amd_iommu_suspend,
2469 .resume = amd_iommu_resume,
2470 };
2471
free_iommu_resources(void)2472 static void __init free_iommu_resources(void)
2473 {
2474 kmemleak_free(irq_lookup_table);
2475 free_pages((unsigned long)irq_lookup_table,
2476 get_order(rlookup_table_size));
2477 irq_lookup_table = NULL;
2478
2479 kmem_cache_destroy(amd_iommu_irq_cache);
2480 amd_iommu_irq_cache = NULL;
2481
2482 free_pages((unsigned long)amd_iommu_rlookup_table,
2483 get_order(rlookup_table_size));
2484 amd_iommu_rlookup_table = NULL;
2485
2486 free_pages((unsigned long)amd_iommu_alias_table,
2487 get_order(alias_table_size));
2488 amd_iommu_alias_table = NULL;
2489
2490 free_pages((unsigned long)amd_iommu_dev_table,
2491 get_order(dev_table_size));
2492 amd_iommu_dev_table = NULL;
2493
2494 free_iommu_all();
2495 }
2496
2497 /* SB IOAPIC is always on this device in AMD systems */
2498 #define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2499
check_ioapic_information(void)2500 static bool __init check_ioapic_information(void)
2501 {
2502 const char *fw_bug = FW_BUG;
2503 bool ret, has_sb_ioapic;
2504 int idx;
2505
2506 has_sb_ioapic = false;
2507 ret = false;
2508
2509 /*
2510 * If we have map overrides on the kernel command line the
2511 * messages in this function might not describe firmware bugs
2512 * anymore - so be careful
2513 */
2514 if (cmdline_maps)
2515 fw_bug = "";
2516
2517 for (idx = 0; idx < nr_ioapics; idx++) {
2518 int devid, id = mpc_ioapic_id(idx);
2519
2520 devid = get_ioapic_devid(id);
2521 if (devid < 0) {
2522 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
2523 fw_bug, id);
2524 ret = false;
2525 } else if (devid == IOAPIC_SB_DEVID) {
2526 has_sb_ioapic = true;
2527 ret = true;
2528 }
2529 }
2530
2531 if (!has_sb_ioapic) {
2532 /*
2533 * We expect the SB IOAPIC to be listed in the IVRS
2534 * table. The system timer is connected to the SB IOAPIC
2535 * and if we don't have it in the list the system will
2536 * panic at boot time. This situation usually happens
2537 * when the BIOS is buggy and provides us the wrong
2538 * device id for the IOAPIC in the system.
2539 */
2540 pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
2541 }
2542
2543 if (!ret)
2544 pr_err("Disabling interrupt remapping\n");
2545
2546 return ret;
2547 }
2548
free_dma_resources(void)2549 static void __init free_dma_resources(void)
2550 {
2551 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2552 get_order(MAX_DOMAIN_ID/8));
2553 amd_iommu_pd_alloc_bitmap = NULL;
2554
2555 free_unity_maps();
2556 }
2557
ivinfo_init(void * ivrs)2558 static void __init ivinfo_init(void *ivrs)
2559 {
2560 amd_iommu_ivinfo = *((u32 *)(ivrs + IOMMU_IVINFO_OFFSET));
2561 }
2562
2563 /*
2564 * This is the hardware init function for AMD IOMMU in the system.
2565 * This function is called either from amd_iommu_init or from the interrupt
2566 * remapping setup code.
2567 *
2568 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
2569 * four times:
2570 *
2571 * 1 pass) Discover the most comprehensive IVHD type to use.
2572 *
2573 * 2 pass) Find the highest PCI device id the driver has to handle.
2574 * Upon this information the size of the data structures is
2575 * determined that needs to be allocated.
2576 *
2577 * 3 pass) Initialize the data structures just allocated with the
2578 * information in the ACPI table about available AMD IOMMUs
2579 * in the system. It also maps the PCI devices in the
2580 * system to specific IOMMUs
2581 *
2582 * 4 pass) After the basic data structures are allocated and
2583 * initialized we update them with information about memory
2584 * remapping requirements parsed out of the ACPI table in
2585 * this last pass.
2586 *
2587 * After everything is set up the IOMMUs are enabled and the necessary
2588 * hotplug and suspend notifiers are registered.
2589 */
early_amd_iommu_init(void)2590 static int __init early_amd_iommu_init(void)
2591 {
2592 struct acpi_table_header *ivrs_base;
2593 acpi_status status;
2594 int i, remap_cache_sz, ret = 0;
2595 u32 pci_id;
2596
2597 if (!amd_iommu_detected)
2598 return -ENODEV;
2599
2600 status = acpi_get_table("IVRS", 0, &ivrs_base);
2601 if (status == AE_NOT_FOUND)
2602 return -ENODEV;
2603 else if (ACPI_FAILURE(status)) {
2604 const char *err = acpi_format_exception(status);
2605 pr_err("IVRS table error: %s\n", err);
2606 return -EINVAL;
2607 }
2608
2609 /*
2610 * Validate checksum here so we don't need to do it when
2611 * we actually parse the table
2612 */
2613 ret = check_ivrs_checksum(ivrs_base);
2614 if (ret)
2615 goto out;
2616
2617 ivinfo_init(ivrs_base);
2618
2619 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2620 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2621
2622 /*
2623 * First parse ACPI tables to find the largest Bus/Dev/Func
2624 * we need to handle. Upon this information the shared data
2625 * structures for the IOMMUs in the system will be allocated
2626 */
2627 ret = find_last_devid_acpi(ivrs_base);
2628 if (ret)
2629 goto out;
2630
2631 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2632 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2633 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
2634
2635 /* Device table - directly used by all IOMMUs */
2636 ret = -ENOMEM;
2637 amd_iommu_dev_table = (void *)__get_free_pages(
2638 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
2639 get_order(dev_table_size));
2640 if (amd_iommu_dev_table == NULL)
2641 goto out;
2642
2643 /*
2644 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2645 * IOMMU see for that device
2646 */
2647 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2648 get_order(alias_table_size));
2649 if (amd_iommu_alias_table == NULL)
2650 goto out;
2651
2652 /* IOMMU rlookup table - find the IOMMU for a specific device */
2653 amd_iommu_rlookup_table = (void *)__get_free_pages(
2654 GFP_KERNEL | __GFP_ZERO,
2655 get_order(rlookup_table_size));
2656 if (amd_iommu_rlookup_table == NULL)
2657 goto out;
2658
2659 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2660 GFP_KERNEL | __GFP_ZERO,
2661 get_order(MAX_DOMAIN_ID/8));
2662 if (amd_iommu_pd_alloc_bitmap == NULL)
2663 goto out;
2664
2665 /*
2666 * let all alias entries point to itself
2667 */
2668 for (i = 0; i <= amd_iommu_last_bdf; ++i)
2669 amd_iommu_alias_table[i] = i;
2670
2671 /*
2672 * never allocate domain 0 because its used as the non-allocated and
2673 * error value placeholder
2674 */
2675 __set_bit(0, amd_iommu_pd_alloc_bitmap);
2676
2677 /*
2678 * now the data structures are allocated and basically initialized
2679 * start the real acpi table scan
2680 */
2681 ret = init_iommu_all(ivrs_base);
2682 if (ret)
2683 goto out;
2684
2685 /* Disable IOMMU if there's Stoney Ridge graphics */
2686 for (i = 0; i < 32; i++) {
2687 pci_id = read_pci_config(0, i, 0, 0);
2688 if ((pci_id & 0xffff) == 0x1002 && (pci_id >> 16) == 0x98e4) {
2689 pr_info("Disable IOMMU on Stoney Ridge\n");
2690 amd_iommu_disabled = true;
2691 break;
2692 }
2693 }
2694
2695 /* Disable any previously enabled IOMMUs */
2696 if (!is_kdump_kernel() || amd_iommu_disabled)
2697 disable_iommus();
2698
2699 if (amd_iommu_irq_remap)
2700 amd_iommu_irq_remap = check_ioapic_information();
2701
2702 if (amd_iommu_irq_remap) {
2703 /*
2704 * Interrupt remapping enabled, create kmem_cache for the
2705 * remapping tables.
2706 */
2707 ret = -ENOMEM;
2708 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2709 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2710 else
2711 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
2712 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
2713 remap_cache_sz,
2714 IRQ_TABLE_ALIGNMENT,
2715 0, NULL);
2716 if (!amd_iommu_irq_cache)
2717 goto out;
2718
2719 irq_lookup_table = (void *)__get_free_pages(
2720 GFP_KERNEL | __GFP_ZERO,
2721 get_order(rlookup_table_size));
2722 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2723 1, GFP_KERNEL);
2724 if (!irq_lookup_table)
2725 goto out;
2726 }
2727
2728 ret = init_memory_definitions(ivrs_base);
2729 if (ret)
2730 goto out;
2731
2732 /* init the device table */
2733 init_device_table();
2734
2735 out:
2736 /* Don't leak any ACPI memory */
2737 acpi_put_table(ivrs_base);
2738 ivrs_base = NULL;
2739
2740 return ret;
2741 }
2742
amd_iommu_enable_interrupts(void)2743 static int amd_iommu_enable_interrupts(void)
2744 {
2745 struct amd_iommu *iommu;
2746 int ret = 0;
2747
2748 for_each_iommu(iommu) {
2749 ret = iommu_init_msi(iommu);
2750 if (ret)
2751 goto out;
2752 }
2753
2754 out:
2755 return ret;
2756 }
2757
detect_ivrs(void)2758 static bool detect_ivrs(void)
2759 {
2760 struct acpi_table_header *ivrs_base;
2761 acpi_status status;
2762
2763 status = acpi_get_table("IVRS", 0, &ivrs_base);
2764 if (status == AE_NOT_FOUND)
2765 return false;
2766 else if (ACPI_FAILURE(status)) {
2767 const char *err = acpi_format_exception(status);
2768 pr_err("IVRS table error: %s\n", err);
2769 return false;
2770 }
2771
2772 acpi_put_table(ivrs_base);
2773
2774 /* Make sure ACS will be enabled during PCI probe */
2775 pci_request_acs();
2776
2777 return true;
2778 }
2779
2780 /****************************************************************************
2781 *
2782 * AMD IOMMU Initialization State Machine
2783 *
2784 ****************************************************************************/
2785
state_next(void)2786 static int __init state_next(void)
2787 {
2788 int ret = 0;
2789
2790 switch (init_state) {
2791 case IOMMU_START_STATE:
2792 if (!detect_ivrs()) {
2793 init_state = IOMMU_NOT_FOUND;
2794 ret = -ENODEV;
2795 } else {
2796 init_state = IOMMU_IVRS_DETECTED;
2797 }
2798 break;
2799 case IOMMU_IVRS_DETECTED:
2800 ret = early_amd_iommu_init();
2801 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2802 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2803 pr_info("AMD IOMMU disabled\n");
2804 init_state = IOMMU_CMDLINE_DISABLED;
2805 ret = -EINVAL;
2806 }
2807 break;
2808 case IOMMU_ACPI_FINISHED:
2809 early_enable_iommus();
2810 x86_platform.iommu_shutdown = disable_iommus;
2811 init_state = IOMMU_ENABLED;
2812 break;
2813 case IOMMU_ENABLED:
2814 register_syscore_ops(&amd_iommu_syscore_ops);
2815 ret = amd_iommu_init_pci();
2816 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2817 enable_iommus_v2();
2818 break;
2819 case IOMMU_PCI_INIT:
2820 ret = amd_iommu_enable_interrupts();
2821 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2822 break;
2823 case IOMMU_INTERRUPTS_EN:
2824 ret = amd_iommu_init_dma_ops();
2825 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2826 break;
2827 case IOMMU_DMA_OPS:
2828 init_state = IOMMU_INITIALIZED;
2829 break;
2830 case IOMMU_INITIALIZED:
2831 /* Nothing to do */
2832 break;
2833 case IOMMU_NOT_FOUND:
2834 case IOMMU_INIT_ERROR:
2835 case IOMMU_CMDLINE_DISABLED:
2836 /* Error states => do nothing */
2837 ret = -EINVAL;
2838 break;
2839 default:
2840 /* Unknown state */
2841 BUG();
2842 }
2843
2844 if (ret) {
2845 free_dma_resources();
2846 if (!irq_remapping_enabled) {
2847 disable_iommus();
2848 free_iommu_resources();
2849 } else {
2850 struct amd_iommu *iommu;
2851
2852 uninit_device_table_dma();
2853 for_each_iommu(iommu)
2854 iommu_flush_all_caches(iommu);
2855 }
2856 }
2857 return ret;
2858 }
2859
iommu_go_to_state(enum iommu_init_state state)2860 static int __init iommu_go_to_state(enum iommu_init_state state)
2861 {
2862 int ret = -EINVAL;
2863
2864 while (init_state != state) {
2865 if (init_state == IOMMU_NOT_FOUND ||
2866 init_state == IOMMU_INIT_ERROR ||
2867 init_state == IOMMU_CMDLINE_DISABLED)
2868 break;
2869 ret = state_next();
2870 }
2871
2872 return ret;
2873 }
2874
2875 #ifdef CONFIG_IRQ_REMAP
amd_iommu_prepare(void)2876 int __init amd_iommu_prepare(void)
2877 {
2878 int ret;
2879
2880 amd_iommu_irq_remap = true;
2881
2882 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2883 if (ret)
2884 return ret;
2885 return amd_iommu_irq_remap ? 0 : -ENODEV;
2886 }
2887
amd_iommu_enable(void)2888 int __init amd_iommu_enable(void)
2889 {
2890 int ret;
2891
2892 ret = iommu_go_to_state(IOMMU_ENABLED);
2893 if (ret)
2894 return ret;
2895
2896 irq_remapping_enabled = 1;
2897 return amd_iommu_xt_mode;
2898 }
2899
amd_iommu_disable(void)2900 void amd_iommu_disable(void)
2901 {
2902 amd_iommu_suspend();
2903 }
2904
amd_iommu_reenable(int mode)2905 int amd_iommu_reenable(int mode)
2906 {
2907 amd_iommu_resume();
2908
2909 return 0;
2910 }
2911
amd_iommu_enable_faulting(void)2912 int __init amd_iommu_enable_faulting(void)
2913 {
2914 /* We enable MSI later when PCI is initialized */
2915 return 0;
2916 }
2917 #endif
2918
2919 /*
2920 * This is the core init function for AMD IOMMU hardware in the system.
2921 * This function is called from the generic x86 DMA layer initialization
2922 * code.
2923 */
amd_iommu_init(void)2924 static int __init amd_iommu_init(void)
2925 {
2926 struct amd_iommu *iommu;
2927 int ret;
2928
2929 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2930 #ifdef CONFIG_GART_IOMMU
2931 if (ret && list_empty(&amd_iommu_list)) {
2932 /*
2933 * We failed to initialize the AMD IOMMU - try fallback
2934 * to GART if possible.
2935 */
2936 gart_iommu_init();
2937 }
2938 #endif
2939
2940 for_each_iommu(iommu)
2941 amd_iommu_debugfs_setup(iommu);
2942
2943 return ret;
2944 }
2945
amd_iommu_sme_check(void)2946 static bool amd_iommu_sme_check(void)
2947 {
2948 if (!sme_active() || (boot_cpu_data.x86 != 0x17))
2949 return true;
2950
2951 /* For Fam17h, a specific level of support is required */
2952 if (boot_cpu_data.microcode >= 0x08001205)
2953 return true;
2954
2955 if ((boot_cpu_data.microcode >= 0x08001126) &&
2956 (boot_cpu_data.microcode <= 0x080011ff))
2957 return true;
2958
2959 pr_notice("IOMMU not currently supported when SME is active\n");
2960
2961 return false;
2962 }
2963
2964 /****************************************************************************
2965 *
2966 * Early detect code. This code runs at IOMMU detection time in the DMA
2967 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2968 * IOMMUs
2969 *
2970 ****************************************************************************/
amd_iommu_detect(void)2971 int __init amd_iommu_detect(void)
2972 {
2973 int ret;
2974
2975 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
2976 return -ENODEV;
2977
2978 if (!amd_iommu_sme_check())
2979 return -ENODEV;
2980
2981 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2982 if (ret)
2983 return ret;
2984
2985 amd_iommu_detected = true;
2986 iommu_detected = 1;
2987 x86_init.iommu.iommu_init = amd_iommu_init;
2988
2989 return 1;
2990 }
2991
2992 /****************************************************************************
2993 *
2994 * Parsing functions for the AMD IOMMU specific kernel command line
2995 * options.
2996 *
2997 ****************************************************************************/
2998
parse_amd_iommu_dump(char * str)2999 static int __init parse_amd_iommu_dump(char *str)
3000 {
3001 amd_iommu_dump = true;
3002
3003 return 1;
3004 }
3005
parse_amd_iommu_intr(char * str)3006 static int __init parse_amd_iommu_intr(char *str)
3007 {
3008 for (; *str; ++str) {
3009 if (strncmp(str, "legacy", 6) == 0) {
3010 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
3011 break;
3012 }
3013 if (strncmp(str, "vapic", 5) == 0) {
3014 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
3015 break;
3016 }
3017 }
3018 return 1;
3019 }
3020
parse_amd_iommu_options(char * str)3021 static int __init parse_amd_iommu_options(char *str)
3022 {
3023 for (; *str; ++str) {
3024 if (strncmp(str, "fullflush", 9) == 0)
3025 amd_iommu_unmap_flush = true;
3026 if (strncmp(str, "off", 3) == 0)
3027 amd_iommu_disabled = true;
3028 if (strncmp(str, "force_isolation", 15) == 0)
3029 amd_iommu_force_isolation = true;
3030 }
3031
3032 return 1;
3033 }
3034
parse_ivrs_ioapic(char * str)3035 static int __init parse_ivrs_ioapic(char *str)
3036 {
3037 unsigned int bus, dev, fn;
3038 int ret, id, i;
3039 u16 devid;
3040
3041 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
3042
3043 if (ret != 4) {
3044 pr_err("Invalid command line: ivrs_ioapic%s\n", str);
3045 return 1;
3046 }
3047
3048 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
3049 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
3050 str);
3051 return 1;
3052 }
3053
3054 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3055
3056 cmdline_maps = true;
3057 i = early_ioapic_map_size++;
3058 early_ioapic_map[i].id = id;
3059 early_ioapic_map[i].devid = devid;
3060 early_ioapic_map[i].cmd_line = true;
3061
3062 return 1;
3063 }
3064
parse_ivrs_hpet(char * str)3065 static int __init parse_ivrs_hpet(char *str)
3066 {
3067 unsigned int bus, dev, fn;
3068 int ret, id, i;
3069 u16 devid;
3070
3071 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
3072
3073 if (ret != 4) {
3074 pr_err("Invalid command line: ivrs_hpet%s\n", str);
3075 return 1;
3076 }
3077
3078 if (early_hpet_map_size == EARLY_MAP_SIZE) {
3079 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
3080 str);
3081 return 1;
3082 }
3083
3084 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3085
3086 cmdline_maps = true;
3087 i = early_hpet_map_size++;
3088 early_hpet_map[i].id = id;
3089 early_hpet_map[i].devid = devid;
3090 early_hpet_map[i].cmd_line = true;
3091
3092 return 1;
3093 }
3094
parse_ivrs_acpihid(char * str)3095 static int __init parse_ivrs_acpihid(char *str)
3096 {
3097 u32 bus, dev, fn;
3098 char *hid, *uid, *p;
3099 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
3100 int ret, i;
3101
3102 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
3103 if (ret != 4) {
3104 pr_err("Invalid command line: ivrs_acpihid(%s)\n", str);
3105 return 1;
3106 }
3107
3108 p = acpiid;
3109 hid = strsep(&p, ":");
3110 uid = p;
3111
3112 if (!hid || !(*hid) || !uid) {
3113 pr_err("Invalid command line: hid or uid\n");
3114 return 1;
3115 }
3116
3117 i = early_acpihid_map_size++;
3118 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
3119 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
3120 early_acpihid_map[i].devid =
3121 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3122 early_acpihid_map[i].cmd_line = true;
3123
3124 return 1;
3125 }
3126
3127 __setup("amd_iommu_dump", parse_amd_iommu_dump);
3128 __setup("amd_iommu=", parse_amd_iommu_options);
3129 __setup("amd_iommu_intr=", parse_amd_iommu_intr);
3130 __setup("ivrs_ioapic", parse_ivrs_ioapic);
3131 __setup("ivrs_hpet", parse_ivrs_hpet);
3132 __setup("ivrs_acpihid", parse_ivrs_acpihid);
3133
3134 IOMMU_INIT_FINISH(amd_iommu_detect,
3135 gart_iommu_hole_init,
3136 NULL,
3137 NULL);
3138
amd_iommu_v2_supported(void)3139 bool amd_iommu_v2_supported(void)
3140 {
3141 return amd_iommu_v2_present;
3142 }
3143 EXPORT_SYMBOL(amd_iommu_v2_supported);
3144
get_amd_iommu(unsigned int idx)3145 struct amd_iommu *get_amd_iommu(unsigned int idx)
3146 {
3147 unsigned int i = 0;
3148 struct amd_iommu *iommu;
3149
3150 for_each_iommu(iommu)
3151 if (i++ == idx)
3152 return iommu;
3153 return NULL;
3154 }
3155 EXPORT_SYMBOL(get_amd_iommu);
3156
3157 /****************************************************************************
3158 *
3159 * IOMMU EFR Performance Counter support functionality. This code allows
3160 * access to the IOMMU PC functionality.
3161 *
3162 ****************************************************************************/
3163
amd_iommu_pc_get_max_banks(unsigned int idx)3164 u8 amd_iommu_pc_get_max_banks(unsigned int idx)
3165 {
3166 struct amd_iommu *iommu = get_amd_iommu(idx);
3167
3168 if (iommu)
3169 return iommu->max_banks;
3170
3171 return 0;
3172 }
3173 EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
3174
amd_iommu_pc_supported(void)3175 bool amd_iommu_pc_supported(void)
3176 {
3177 return amd_iommu_pc_present;
3178 }
3179 EXPORT_SYMBOL(amd_iommu_pc_supported);
3180
amd_iommu_pc_get_max_counters(unsigned int idx)3181 u8 amd_iommu_pc_get_max_counters(unsigned int idx)
3182 {
3183 struct amd_iommu *iommu = get_amd_iommu(idx);
3184
3185 if (iommu)
3186 return iommu->max_counters;
3187
3188 return 0;
3189 }
3190 EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3191
iommu_pc_get_set_reg(struct amd_iommu * iommu,u8 bank,u8 cntr,u8 fxn,u64 * value,bool is_write)3192 static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3193 u8 fxn, u64 *value, bool is_write)
3194 {
3195 u32 offset;
3196 u32 max_offset_lim;
3197
3198 /* Make sure the IOMMU PC resource is available */
3199 if (!amd_iommu_pc_present)
3200 return -ENODEV;
3201
3202 /* Check for valid iommu and pc register indexing */
3203 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
3204 return -ENODEV;
3205
3206 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
3207
3208 /* Limit the offset to the hw defined mmio region aperture */
3209 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
3210 (iommu->max_counters << 8) | 0x28);
3211 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3212 (offset > max_offset_lim))
3213 return -EINVAL;
3214
3215 if (is_write) {
3216 u64 val = *value & GENMASK_ULL(47, 0);
3217
3218 writel((u32)val, iommu->mmio_base + offset);
3219 writel((val >> 32), iommu->mmio_base + offset + 4);
3220 } else {
3221 *value = readl(iommu->mmio_base + offset + 4);
3222 *value <<= 32;
3223 *value |= readl(iommu->mmio_base + offset);
3224 *value &= GENMASK_ULL(47, 0);
3225 }
3226
3227 return 0;
3228 }
3229
amd_iommu_pc_get_reg(struct amd_iommu * iommu,u8 bank,u8 cntr,u8 fxn,u64 * value)3230 int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3231 {
3232 if (!iommu)
3233 return -EINVAL;
3234
3235 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
3236 }
3237 EXPORT_SYMBOL(amd_iommu_pc_get_reg);
3238
amd_iommu_pc_set_reg(struct amd_iommu * iommu,u8 bank,u8 cntr,u8 fxn,u64 * value)3239 int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3240 {
3241 if (!iommu)
3242 return -EINVAL;
3243
3244 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3245 }
3246 EXPORT_SYMBOL(amd_iommu_pc_set_reg);
3247