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