blob: 1c7ae7d3c55db26dbdff032d75907ffeca960986 [file] [log] [blame]
Thomas Gleixner45051532019-05-29 16:57:47 -07001// SPDX-License-Identifier: GPL-2.0-only
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02002/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02003 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01004 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02005 * Leo Duran <leo.duran@amd.com>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02006 */
7
Joerg Roedel101fa032018-11-27 16:22:31 +01008#define pr_fmt(fmt) "AMD-Vi: " fmt
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06009#define dev_fmt(fmt) pr_fmt(fmt)
Joerg Roedel101fa032018-11-27 16:22:31 +010010
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020011#include <linux/pci.h>
12#include <linux/acpi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020013#include <linux/list.h>
Baoquan He5c87f622016-09-15 16:50:51 +080014#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010016#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020017#include <linux/interrupt.h>
18#include <linux/msi.h>
David Woodhoused1adcfb2020-11-11 12:09:01 +000019#include <linux/irq.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020020#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010021#include <linux/export.h>
Lucas Stachebcfa282016-10-26 13:09:53 +020022#include <linux/kmemleak.h>
Tom Lendacky2543a782017-07-17 16:10:24 -050023#include <linux/mem_encrypt.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020024#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090025#include <asm/iommu.h>
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +000026#include <asm/apic.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010027#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090028#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040029#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020030#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020031#include <asm/irq_remapping.h>
Suravee Suthikulpanit6d39bde2020-11-05 14:58:32 +000032#include <asm/set_memory.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020033
Baoquan He3ac3e5ee2017-08-09 16:33:38 +080034#include <linux/crash_dump.h>
Joerg Roedel786dfe42020-05-27 13:53:11 +020035
Kai-Heng Feng93d05152019-08-21 13:10:04 +080036#include "amd_iommu.h"
Joerg Roedelad8694b2020-06-09 15:03:02 +020037#include "../irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020038
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020039/*
40 * definitions for the ACPI scanning code
41 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020042#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020043
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040044#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020045#define ACPI_IVMD_TYPE_ALL 0x20
46#define ACPI_IVMD_TYPE 0x21
47#define ACPI_IVMD_TYPE_RANGE 0x22
48
49#define IVHD_DEV_ALL 0x01
50#define IVHD_DEV_SELECT 0x02
51#define IVHD_DEV_SELECT_RANGE_START 0x03
52#define IVHD_DEV_RANGE_END 0x04
53#define IVHD_DEV_ALIAS 0x42
54#define IVHD_DEV_ALIAS_RANGE 0x43
55#define IVHD_DEV_EXT_SELECT 0x46
56#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020057#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040058#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020059
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040060#define UID_NOT_PRESENT 0
61#define UID_IS_INTEGER 1
62#define UID_IS_CHARACTER 2
63
Joerg Roedel6efed632012-06-14 15:52:58 +020064#define IVHD_SPECIAL_IOAPIC 1
65#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020066
Joerg Roedel6da73422009-05-04 11:44:38 +020067#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
68#define IVHD_FLAG_PASSPW_EN_MASK 0x02
69#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
70#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020071
72#define IVMD_FLAG_EXCL_RANGE 0x08
Adrian Huang387caf02019-11-14 14:14:47 +080073#define IVMD_FLAG_IW 0x04
74#define IVMD_FLAG_IR 0x02
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020075#define IVMD_FLAG_UNITY_MAP 0x01
76
77#define ACPI_DEVFLAG_INITPASS 0x01
78#define ACPI_DEVFLAG_EXTINT 0x02
79#define ACPI_DEVFLAG_NMI 0x04
80#define ACPI_DEVFLAG_SYSMGT1 0x10
81#define ACPI_DEVFLAG_SYSMGT2 0x20
82#define ACPI_DEVFLAG_LINT0 0x40
83#define ACPI_DEVFLAG_LINT1 0x80
84#define ACPI_DEVFLAG_ATSDIS 0x10000000
85
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050086#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020087/*
88 * ACPI table definitions
89 *
90 * These data structures are laid over the table to parse the important values
91 * out of it.
92 */
93
Joerg Roedelb0119e82017-02-01 13:23:08 +010094extern const struct iommu_ops amd_iommu_ops;
95
Joerg Roedelb65233a2008-07-11 17:14:21 +020096/*
97 * structure describing one IOMMU in the ACPI table. Typically followed by one
98 * or more ivhd_entrys.
99 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200100struct ivhd_header {
101 u8 type;
102 u8 flags;
103 u16 length;
104 u16 devid;
105 u16 cap_ptr;
106 u64 mmio_phys;
107 u16 pci_seg;
108 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400109 u32 efr_attr;
110
111 /* Following only valid on IVHD type 11h and 40h */
112 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
113 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200114} __attribute__((packed));
115
Joerg Roedelb65233a2008-07-11 17:14:21 +0200116/*
117 * A device entry describing which devices a specific IOMMU translates and
118 * which requestor ids they use.
119 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200120struct ivhd_entry {
121 u8 type;
122 u16 devid;
123 u8 flags;
124 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400125 u32 hidh;
126 u64 cid;
127 u8 uidf;
128 u8 uidl;
129 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200130} __attribute__((packed));
131
Joerg Roedelb65233a2008-07-11 17:14:21 +0200132/*
133 * An AMD IOMMU memory definition structure. It defines things like exclusion
134 * ranges for devices and regions that should be unity mapped.
135 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200136struct ivmd_header {
137 u8 type;
138 u8 flags;
139 u16 length;
140 u16 devid;
141 u16 aux;
142 u64 resv;
143 u64 range_start;
144 u64 range_length;
145} __attribute__((packed));
146
Joerg Roedelfefda112009-05-20 12:21:42 +0200147bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200148bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200149
Suravee Suthikulpanit89c9a092020-12-15 01:37:05 -0600150enum io_pgtable_fmt amd_iommu_pgtable = AMD_IOMMU_V1;
151
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500152int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
Suravee Suthikulpanit81307142019-11-20 07:55:48 -0600153static int amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500154
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200155static bool amd_iommu_detected;
Joerg Roedelb1e650d2021-06-03 15:02:03 +0200156static bool amd_iommu_disabled __initdata;
157static bool amd_iommu_force_enable __initdata;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400158static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200159
Joerg Roedelb65233a2008-07-11 17:14:21 +0200160u16 amd_iommu_last_bdf; /* largest PCI device id we have
161 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200162LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200163 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700164bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200165
Joerg Roedel2e228472008-07-11 17:14:31 +0200166LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200167 system */
168
Joerg Roedelbb527772009-11-20 14:31:51 +0100169/* Array to assign indices to IOMMUs*/
170struct amd_iommu *amd_iommus[MAX_IOMMUS];
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600171
172/* Number of IOMMUs present in the system */
173static int amd_iommus_present;
Joerg Roedelbb527772009-11-20 14:31:51 +0100174
Joerg Roedel318afd42009-11-23 18:32:38 +0100175/* IOMMUs have a non-present cache? */
176bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200177bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100178
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600179u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100180
Joerg Roedel400a28a2011-11-28 15:11:02 +0100181bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200182static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100183
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100184bool amd_iommu_force_isolation __read_mostly;
185
Joerg Roedelb65233a2008-07-11 17:14:21 +0200186/*
187 * Pointer to the device table which is shared by all AMD IOMMUs
188 * it is indexed by the PCI device id or the HT unit id and contains
189 * information about the domain the device belongs to as well as the
190 * page table root pointer.
191 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200192struct dev_table_entry *amd_iommu_dev_table;
Baoquan He45a01c42017-08-09 16:33:37 +0800193/*
194 * Pointer to a device table which the content of old device table
195 * will be copied to. It's only be used in kdump kernel.
196 */
197static struct dev_table_entry *old_dev_tbl_cpy;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200198
199/*
200 * The alias table is a driver specific data structure which contains the
201 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
202 * More than one device can share the same requestor id.
203 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200204u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200205
206/*
207 * The rlookup table is used to find the IOMMU which is responsible
208 * for a specific device. It is also indexed by the PCI device id.
209 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200210struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200211
212/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200213 * This table is used to find the irq remapping table for a given device id
214 * quickly.
215 */
216struct irq_remap_table **irq_lookup_table;
217
218/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200219 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200220 * to know which ones are already in use.
221 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200222unsigned long *amd_iommu_pd_alloc_bitmap;
223
Joerg Roedelb65233a2008-07-11 17:14:21 +0200224static u32 dev_table_size; /* size of the device table */
225static u32 alias_table_size; /* size of the alias table */
226static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200227
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200228enum iommu_init_state {
229 IOMMU_START_STATE,
230 IOMMU_IVRS_DETECTED,
231 IOMMU_ACPI_FINISHED,
232 IOMMU_ENABLED,
233 IOMMU_PCI_INIT,
234 IOMMU_INTERRUPTS_EN,
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200235 IOMMU_INITIALIZED,
236 IOMMU_NOT_FOUND,
237 IOMMU_INIT_ERROR,
Joerg Roedel1b1e9422017-06-16 16:09:56 +0200238 IOMMU_CMDLINE_DISABLED,
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200239};
240
Joerg Roedel235dacb2013-04-09 17:53:14 +0200241/* Early ioapic and hpet maps from kernel command line */
242#define EARLY_MAP_SIZE 4
243static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
244static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400245static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
246
Joerg Roedel235dacb2013-04-09 17:53:14 +0200247static int __initdata early_ioapic_map_size;
248static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400249static int __initdata early_acpihid_map_size;
250
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200251static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200252
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200253static enum iommu_init_state init_state = IOMMU_START_STATE;
254
Gerard Snitselaarae295142012-03-16 11:38:22 -0700255static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200256static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200257static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100258
Joerg Roedel2479c632017-08-19 00:35:40 +0200259static bool amd_iommu_pre_enabled = true;
Baoquan He3ac3e5ee2017-08-09 16:33:38 +0800260
Suravee Suthikulpanita44092e2021-01-20 07:50:02 -0600261static u32 amd_iommu_ivinfo __initdata;
262
Baoquan He4c232a72017-08-09 16:33:33 +0800263bool translation_pre_enabled(struct amd_iommu *iommu)
264{
265 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
266}
267
268static void clear_translation_pre_enabled(struct amd_iommu *iommu)
269{
270 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
271}
272
273static void init_translation_status(struct amd_iommu *iommu)
274{
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500275 u64 ctrl;
Baoquan He4c232a72017-08-09 16:33:33 +0800276
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500277 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Baoquan He4c232a72017-08-09 16:33:33 +0800278 if (ctrl & (1<<CONTROL_IOMMU_EN))
279 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
280}
281
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200282static inline void update_last_devid(u16 devid)
283{
284 if (devid > amd_iommu_last_bdf)
285 amd_iommu_last_bdf = devid;
286}
287
Joerg Roedelc5714842008-07-11 17:14:25 +0200288static inline unsigned long tbl_size(int entry_size)
289{
290 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100291 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200292
293 return 1UL << shift;
294}
295
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600296int amd_iommu_get_num_iommus(void)
297{
298 return amd_iommus_present;
299}
300
Suravee Suthikulpanita44092e2021-01-20 07:50:02 -0600301/*
302 * For IVHD type 0x11/0x40, EFR is also available via IVHD.
303 * Default to IVHD EFR since it is available sooner
304 * (i.e. before PCI init).
305 */
306static void __init early_iommu_features_init(struct amd_iommu *iommu,
307 struct ivhd_header *h)
308{
309 if (amd_iommu_ivinfo & IOMMU_IVINFO_EFRSUP)
310 iommu->features = h->efr_reg;
311}
312
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400313/* Access to l1 and l2 indexed register spaces */
314
315static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
316{
317 u32 val;
318
319 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
320 pci_read_config_dword(iommu->dev, 0xfc, &val);
321 return val;
322}
323
324static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
325{
326 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
327 pci_write_config_dword(iommu->dev, 0xfc, val);
328 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
329}
330
331static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
332{
333 u32 val;
334
335 pci_write_config_dword(iommu->dev, 0xf0, address);
336 pci_read_config_dword(iommu->dev, 0xf4, &val);
337 return val;
338}
339
340static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
341{
342 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
343 pci_write_config_dword(iommu->dev, 0xf4, val);
344}
345
Joerg Roedelb65233a2008-07-11 17:14:21 +0200346/****************************************************************************
347 *
348 * AMD IOMMU MMIO register space handling functions
349 *
350 * These functions are used to program the IOMMU device registers in
351 * MMIO space required for that driver.
352 *
353 ****************************************************************************/
354
355/*
356 * This function set the exclusion range in the IOMMU. DMA accesses to the
357 * exclusion range are passed through untranslated
358 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200359static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200360{
361 u64 start = iommu->exclusion_start & PAGE_MASK;
Joerg Roedel3c677d202019-04-12 12:50:31 +0200362 u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200363 u64 entry;
364
365 if (!iommu->exclusion_start)
366 return;
367
368 entry = start | MMIO_EXCL_ENABLE_MASK;
369 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
370 &entry, sizeof(entry));
371
372 entry = limit;
373 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
374 &entry, sizeof(entry));
375}
376
Suravee Suthikulpanit54ce12e2020-09-23 12:13:47 +0000377static void iommu_set_cwwb_range(struct amd_iommu *iommu)
378{
379 u64 start = iommu_virt_to_phys((void *)iommu->cmd_sem);
380 u64 entry = start & PM_ADDR_MASK;
381
382 if (!iommu_feature(iommu, FEATURE_SNP))
383 return;
384
385 /* Note:
386 * Re-purpose Exclusion base/limit registers for Completion wait
387 * write-back base/limit.
388 */
389 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
390 &entry, sizeof(entry));
391
392 /* Note:
393 * Default to 4 Kbytes, which can be specified by setting base
394 * address equal to the limit address.
395 */
396 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
397 &entry, sizeof(entry));
398}
399
Joerg Roedelb65233a2008-07-11 17:14:21 +0200400/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000401static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200402{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200403 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200404
405 BUG_ON(iommu->mmio_base == NULL);
406
Tom Lendacky2543a782017-07-17 16:10:24 -0500407 entry = iommu_virt_to_phys(amd_iommu_dev_table);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200408 entry |= (dev_table_size >> 12) - 1;
409 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
410 &entry, sizeof(entry));
411}
412
Joerg Roedelb65233a2008-07-11 17:14:21 +0200413/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200414static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200415{
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500416 u64 ctrl;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200417
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500418 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
419 ctrl |= (1ULL << bit);
420 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200421}
422
Joerg Roedelca0207112009-10-28 18:02:26 +0100423static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200424{
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500425 u64 ctrl;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200426
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500427 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
428 ctrl &= ~(1ULL << bit);
429 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200430}
431
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100432static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
433{
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500434 u64 ctrl;
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100435
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500436 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100437 ctrl &= ~CTRL_INV_TO_MASK;
438 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500439 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100440}
441
Joerg Roedelb65233a2008-07-11 17:14:21 +0200442/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200443static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200444{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200445 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200446}
447
Joerg Roedel92ac4322009-05-19 19:06:27 +0200448static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200449{
Kevin Mitchell3ddbe912019-06-12 14:52:03 -0700450 if (!iommu->mmio_base)
451 return;
452
Chris Wrighta8c485b2009-06-15 15:53:45 +0200453 /* Disable command buffer */
454 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
455
456 /* Disable event logging and event interrupts */
457 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
458 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
459
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500460 /* Disable IOMMU GA_LOG */
461 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
462 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
463
Chris Wrighta8c485b2009-06-15 15:53:45 +0200464 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200465 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200466}
467
Joerg Roedelb65233a2008-07-11 17:14:21 +0200468/*
469 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
470 * the system has one.
471 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500472static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200473{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500474 if (!request_mem_region(address, end, "amd_iommu")) {
Joerg Roedel101fa032018-11-27 16:22:31 +0100475 pr_err("Can not reserve memory region %llx-%llx for mmio\n",
Steven L Kinney30861dd2013-06-05 16:11:48 -0500476 address, end);
Joerg Roedel101fa032018-11-27 16:22:31 +0100477 pr_err("This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200478 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200479 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200480
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100481 return (u8 __iomem *)ioremap(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200482}
483
484static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
485{
486 if (iommu->mmio_base)
487 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500488 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200489}
490
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400491static inline u32 get_ivhd_header_size(struct ivhd_header *h)
492{
493 u32 size = 0;
494
495 switch (h->type) {
496 case 0x10:
497 size = 24;
498 break;
499 case 0x11:
500 case 0x40:
501 size = 40;
502 break;
503 }
504 return size;
505}
506
Joerg Roedelb65233a2008-07-11 17:14:21 +0200507/****************************************************************************
508 *
509 * The functions below belong to the first pass of AMD IOMMU ACPI table
510 * parsing. In this pass we try to find out the highest device id this
511 * code has to handle. Upon this information the size of the shared data
512 * structures is determined later.
513 *
514 ****************************************************************************/
515
516/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200517 * This function calculates the length of a given IVHD entry
518 */
519static inline int ivhd_entry_length(u8 *ivhd)
520{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400521 u32 type = ((struct ivhd_entry *)ivhd)->type;
522
523 if (type < 0x80) {
524 return 0x04 << (*ivhd >> 6);
525 } else if (type == IVHD_DEV_ACPI_HID) {
526 /* For ACPI_HID, offset 21 is uid len */
527 return *((u8 *)ivhd + 21) + 22;
528 }
529 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200530}
531
532/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200533 * After reading the highest device id from the IOMMU PCI capability header
534 * this function looks if there is a higher device id defined in the ACPI table
535 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200536static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
537{
538 u8 *p = (void *)h, *end = (void *)h;
539 struct ivhd_entry *dev;
540
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400541 u32 ivhd_size = get_ivhd_header_size(h);
542
543 if (!ivhd_size) {
Joerg Roedel101fa032018-11-27 16:22:31 +0100544 pr_err("Unsupported IVHD type %#x\n", h->type);
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400545 return -EINVAL;
546 }
547
548 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200549 end += h->length;
550
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200551 while (p < end) {
552 dev = (struct ivhd_entry *)p;
553 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200554 case IVHD_DEV_ALL:
555 /* Use maximum BDF value for DEV_ALL */
556 update_last_devid(0xffff);
557 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200558 case IVHD_DEV_SELECT:
559 case IVHD_DEV_RANGE_END:
560 case IVHD_DEV_ALIAS:
561 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200562 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200563 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200564 break;
565 default:
566 break;
567 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200568 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200569 }
570
571 WARN_ON(p != end);
572
573 return 0;
574}
575
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400576static int __init check_ivrs_checksum(struct acpi_table_header *table)
577{
578 int i;
579 u8 checksum = 0, *p = (u8 *)table;
580
581 for (i = 0; i < table->length; ++i)
582 checksum += p[i];
583 if (checksum != 0) {
584 /* ACPI table corrupt */
Joerg Roedel101fa032018-11-27 16:22:31 +0100585 pr_err(FW_BUG "IVRS invalid checksum\n");
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400586 return -ENODEV;
587 }
588
589 return 0;
590}
591
Joerg Roedelb65233a2008-07-11 17:14:21 +0200592/*
593 * Iterate over all IVHD entries in the ACPI table and find the highest device
594 * id which we need to handle. This is the first of three functions which parse
595 * the ACPI table. So we check the checksum here.
596 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200597static int __init find_last_devid_acpi(struct acpi_table_header *table)
598{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400599 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200600 struct ivhd_header *h;
601
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200602 p += IVRS_HEADER_LENGTH;
603
604 end += table->length;
605 while (p < end) {
606 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400607 if (h->type == amd_iommu_target_ivhd_type) {
608 int ret = find_last_devid_from_ivhd(h);
609
610 if (ret)
611 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200612 }
613 p += h->length;
614 }
615 WARN_ON(p != end);
616
617 return 0;
618}
619
Joerg Roedelb65233a2008-07-11 17:14:21 +0200620/****************************************************************************
621 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200622 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200623 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
624 * data structures, initialize the device/alias/rlookup table and also
625 * basically initialize the hardware.
626 *
627 ****************************************************************************/
628
629/*
630 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
631 * write commands to that buffer later and the IOMMU will execute them
632 * asynchronously
633 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200634static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200635{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200636 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
637 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200638
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200639 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200640}
641
642/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200643 * This function resets the command buffer if the IOMMU stopped fetching
644 * commands from it.
645 */
646void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
647{
648 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
649
650 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
651 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Tom Lendackyd334a562017-06-05 14:52:12 -0500652 iommu->cmd_buf_head = 0;
653 iommu->cmd_buf_tail = 0;
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200654
655 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
656}
657
658/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200659 * This function writes the command buffer address to the hardware and
660 * enables it.
661 */
662static void iommu_enable_command_buffer(struct amd_iommu *iommu)
663{
664 u64 entry;
665
666 BUG_ON(iommu->cmd_buf == NULL);
667
Tom Lendacky2543a782017-07-17 16:10:24 -0500668 entry = iommu_virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200669 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200670
Joerg Roedelb36ca912008-06-26 21:27:45 +0200671 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200672 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200673
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200674 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200675}
676
Baoquan He78d313c2017-08-09 16:33:34 +0800677/*
678 * This function disables the command buffer
679 */
680static void iommu_disable_command_buffer(struct amd_iommu *iommu)
681{
682 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
683}
684
Joerg Roedelb36ca912008-06-26 21:27:45 +0200685static void __init free_command_buffer(struct amd_iommu *iommu)
686{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200687 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200688}
689
Suravee Suthikulpanit6d39bde2020-11-05 14:58:32 +0000690static void *__init iommu_alloc_4k_pages(struct amd_iommu *iommu,
691 gfp_t gfp, size_t size)
692{
693 int order = get_order(size);
694 void *buf = (void *)__get_free_pages(gfp, order);
695
696 if (buf &&
697 iommu_feature(iommu, FEATURE_SNP) &&
698 set_memory_4k((unsigned long)buf, (1 << order))) {
699 free_pages((unsigned long)buf, order);
700 buf = NULL;
701 }
702
703 return buf;
704}
705
Joerg Roedel335503e2008-09-05 14:29:07 +0200706/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200707static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200708{
Suravee Suthikulpanit6d39bde2020-11-05 14:58:32 +0000709 iommu->evt_buf = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
710 EVT_BUFFER_SIZE);
Joerg Roedel335503e2008-09-05 14:29:07 +0200711
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200712 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200713}
714
715static void iommu_enable_event_buffer(struct amd_iommu *iommu)
716{
717 u64 entry;
718
719 BUG_ON(iommu->evt_buf == NULL);
720
Tom Lendacky2543a782017-07-17 16:10:24 -0500721 entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200722
Joerg Roedel335503e2008-09-05 14:29:07 +0200723 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
724 &entry, sizeof(entry));
725
Joerg Roedel090672072009-06-15 16:06:48 +0200726 /* set head and tail to zero manually */
727 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
728 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
729
Joerg Roedel58492e12009-05-04 18:41:16 +0200730 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200731}
732
Baoquan He78d313c2017-08-09 16:33:34 +0800733/*
734 * This function disables the event log buffer
735 */
736static void iommu_disable_event_buffer(struct amd_iommu *iommu)
737{
738 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
739}
740
Joerg Roedel335503e2008-09-05 14:29:07 +0200741static void __init free_event_buffer(struct amd_iommu *iommu)
742{
743 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
744}
745
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100746/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200747static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100748{
Suravee Suthikulpanit6d39bde2020-11-05 14:58:32 +0000749 iommu->ppr_log = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO,
750 PPR_LOG_SIZE);
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100751
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200752 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100753}
754
755static void iommu_enable_ppr_log(struct amd_iommu *iommu)
756{
757 u64 entry;
758
759 if (iommu->ppr_log == NULL)
760 return;
761
Tom Lendacky2543a782017-07-17 16:10:24 -0500762 entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100763
764 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
765 &entry, sizeof(entry));
766
767 /* set head and tail to zero manually */
768 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
769 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
770
Adrian Huangbde9e6b2019-12-30 13:56:54 +0800771 iommu_feature_enable(iommu, CONTROL_PPRLOG_EN);
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100772 iommu_feature_enable(iommu, CONTROL_PPR_EN);
773}
774
775static void __init free_ppr_log(struct amd_iommu *iommu)
776{
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100777 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
778}
779
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500780static void free_ga_log(struct amd_iommu *iommu)
781{
782#ifdef CONFIG_IRQ_REMAP
Libing Zhou092550e2020-07-22 14:44:50 +0800783 free_pages((unsigned long)iommu->ga_log, get_order(GA_LOG_SIZE));
784 free_pages((unsigned long)iommu->ga_log_tail, get_order(8));
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500785#endif
786}
787
788static int iommu_ga_log_enable(struct amd_iommu *iommu)
789{
790#ifdef CONFIG_IRQ_REMAP
791 u32 status, i;
792
793 if (!iommu->ga_log)
794 return -EINVAL;
795
796 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
797
798 /* Check if already running */
799 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
800 return 0;
801
802 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
803 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
804
805 for (i = 0; i < LOOP_TIMEOUT; ++i) {
806 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
807 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
808 break;
809 }
810
811 if (i >= LOOP_TIMEOUT)
812 return -EINVAL;
813#endif /* CONFIG_IRQ_REMAP */
814 return 0;
815}
816
817#ifdef CONFIG_IRQ_REMAP
818static int iommu_init_ga_log(struct amd_iommu *iommu)
819{
820 u64 entry;
821
822 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
823 return 0;
824
825 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
826 get_order(GA_LOG_SIZE));
827 if (!iommu->ga_log)
828 goto err_out;
829
830 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
831 get_order(8));
832 if (!iommu->ga_log_tail)
833 goto err_out;
834
Tom Lendacky2543a782017-07-17 16:10:24 -0500835 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500836 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
837 &entry, sizeof(entry));
Filippo Sironiab99be42018-11-12 12:26:30 +0000838 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
839 (BIT_ULL(52)-1)) & ~7ULL;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500840 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
841 &entry, sizeof(entry));
842 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
843 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
844
845 return 0;
846err_out:
847 free_ga_log(iommu);
848 return -EINVAL;
849}
850#endif /* CONFIG_IRQ_REMAP */
851
852static int iommu_init_ga(struct amd_iommu *iommu)
853{
854 int ret = 0;
855
856#ifdef CONFIG_IRQ_REMAP
857 /* Note: We have already checked GASup from IVRS table.
858 * Now, we need to make sure that GAMSup is set.
859 */
860 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
861 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
862 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
863
864 ret = iommu_init_ga_log(iommu);
865#endif /* CONFIG_IRQ_REMAP */
866
867 return ret;
868}
869
Suravee Suthikulpanitc69d89a2020-09-23 12:13:45 +0000870static int __init alloc_cwwb_sem(struct amd_iommu *iommu)
871{
Suravee Suthikulpanit6d39bde2020-11-05 14:58:32 +0000872 iommu->cmd_sem = iommu_alloc_4k_pages(iommu, GFP_KERNEL | __GFP_ZERO, 1);
Suravee Suthikulpanitc69d89a2020-09-23 12:13:45 +0000873
874 return iommu->cmd_sem ? 0 : -ENOMEM;
875}
876
877static void __init free_cwwb_sem(struct amd_iommu *iommu)
878{
879 if (iommu->cmd_sem)
880 free_page((unsigned long)iommu->cmd_sem);
881}
882
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -0500883static void iommu_enable_xt(struct amd_iommu *iommu)
884{
885#ifdef CONFIG_IRQ_REMAP
886 /*
887 * XT mode (32-bit APIC destination ID) requires
888 * GA mode (128-bit IRTE support) as a prerequisite.
889 */
890 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
891 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
892 iommu_feature_enable(iommu, CONTROL_XT_EN);
893#endif /* CONFIG_IRQ_REMAP */
894}
895
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100896static void iommu_enable_gt(struct amd_iommu *iommu)
897{
898 if (!iommu_feature(iommu, FEATURE_GT))
899 return;
900
901 iommu_feature_enable(iommu, CONTROL_GT_EN);
902}
903
Joerg Roedelb65233a2008-07-11 17:14:21 +0200904/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200905static void set_dev_entry_bit(u16 devid, u8 bit)
906{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100907 int i = (bit >> 6) & 0x03;
908 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200909
Joerg Roedelee6c2862011-11-09 12:06:03 +0100910 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200911}
912
Joerg Roedelc5cca142009-10-09 18:31:20 +0200913static int get_dev_entry_bit(u16 devid, u8 bit)
914{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100915 int i = (bit >> 6) & 0x03;
916 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200917
Joerg Roedelee6c2862011-11-09 12:06:03 +0100918 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200919}
920
921
Baoquan He45a01c42017-08-09 16:33:37 +0800922static bool copy_device_table(void)
923{
Joerg Roedelae162ef2017-08-19 00:28:02 +0200924 u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
Baoquan He45a01c42017-08-09 16:33:37 +0800925 struct dev_table_entry *old_devtb = NULL;
926 u32 lo, hi, devid, old_devtb_size;
927 phys_addr_t old_devtb_phys;
Baoquan He45a01c42017-08-09 16:33:37 +0800928 struct amd_iommu *iommu;
Baoquan He53019a92017-08-09 16:33:39 +0800929 u16 dom_id, dte_v, irq_v;
Baoquan He45a01c42017-08-09 16:33:37 +0800930 gfp_t gfp_flag;
Baoquan Hedaae2d22017-08-09 16:33:43 +0800931 u64 tmp;
Baoquan He45a01c42017-08-09 16:33:37 +0800932
Baoquan He3ac3e5ee2017-08-09 16:33:38 +0800933 if (!amd_iommu_pre_enabled)
934 return false;
Baoquan He45a01c42017-08-09 16:33:37 +0800935
936 pr_warn("Translation is already enabled - trying to copy translation structures\n");
937 for_each_iommu(iommu) {
938 /* All IOMMUs should use the same device table with the same size */
939 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
940 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
941 entry = (((u64) hi) << 32) + lo;
942 if (last_entry && last_entry != entry) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530943 pr_err("IOMMU:%d should use the same dev table as others!\n",
Baoquan He45a01c42017-08-09 16:33:37 +0800944 iommu->index);
945 return false;
946 }
947 last_entry = entry;
948
949 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
950 if (old_devtb_size != dev_table_size) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530951 pr_err("The device table size of IOMMU:%d is not expected!\n",
Baoquan He45a01c42017-08-09 16:33:37 +0800952 iommu->index);
953 return false;
954 }
955 }
956
Lianbo Jiang87801582018-09-30 11:10:32 +0800957 /*
958 * When SME is enabled in the first kernel, the entry includes the
959 * memory encryption mask(sme_me_mask), we must remove the memory
960 * encryption mask to obtain the true physical address in kdump kernel.
961 */
962 old_devtb_phys = __sme_clr(entry) & PAGE_MASK;
963
Baoquan Heb3367812017-08-09 16:33:42 +0800964 if (old_devtb_phys >= 0x100000000ULL) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530965 pr_err("The address of old device table is above 4G, not trustworthy!\n");
Baoquan Heb3367812017-08-09 16:33:42 +0800966 return false;
967 }
Lianbo Jiang87801582018-09-30 11:10:32 +0800968 old_devtb = (sme_active() && is_kdump_kernel())
969 ? (__force void *)ioremap_encrypted(old_devtb_phys,
970 dev_table_size)
971 : memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
972
Baoquan He45a01c42017-08-09 16:33:37 +0800973 if (!old_devtb)
974 return false;
975
Baoquan Heb3367812017-08-09 16:33:42 +0800976 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
Baoquan He45a01c42017-08-09 16:33:37 +0800977 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
978 get_order(dev_table_size));
979 if (old_dev_tbl_cpy == NULL) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530980 pr_err("Failed to allocate memory for copying old device table!\n");
Baoquan He45a01c42017-08-09 16:33:37 +0800981 return false;
982 }
983
984 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
985 old_dev_tbl_cpy[devid] = old_devtb[devid];
986 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
987 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
Baoquan He53019a92017-08-09 16:33:39 +0800988
989 if (dte_v && dom_id) {
990 old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
991 old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
Baoquan He45a01c42017-08-09 16:33:37 +0800992 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
Baoquan Hedaae2d22017-08-09 16:33:43 +0800993 /* If gcr3 table existed, mask it out */
994 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
995 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
996 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
997 old_dev_tbl_cpy[devid].data[1] &= ~tmp;
998 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
999 tmp |= DTE_FLAG_GV;
1000 old_dev_tbl_cpy[devid].data[0] &= ~tmp;
1001 }
Baoquan He53019a92017-08-09 16:33:39 +08001002 }
1003
1004 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
1005 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
Suravee Suthikulpanit5ae9a042020-12-10 10:24:36 -06001006 int_tab_len = old_devtb[devid].data[2] & DTE_INTTABLEN_MASK;
Baoquan He53019a92017-08-09 16:33:39 +08001007 if (irq_v && (int_ctl || int_tab_len)) {
1008 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
Suravee Suthikulpanit5ae9a042020-12-10 10:24:36 -06001009 (int_tab_len != DTE_INTTABLEN)) {
Baoquan He53019a92017-08-09 16:33:39 +08001010 pr_err("Wrong old irq remapping flag: %#x\n", devid);
1011 return false;
1012 }
1013
1014 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
1015 }
Baoquan He45a01c42017-08-09 16:33:37 +08001016 }
1017 memunmap(old_devtb);
1018
1019 return true;
1020}
1021
Joerg Roedelc5cca142009-10-09 18:31:20 +02001022void amd_iommu_apply_erratum_63(u16 devid)
1023{
1024 int sysmgt;
1025
1026 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
1027 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
1028
1029 if (sysmgt == 0x01)
1030 set_dev_entry_bit(devid, DEV_ENTRY_IW);
1031}
1032
Joerg Roedel5ff47892008-07-14 20:11:18 +02001033/* Writes the specific IOMMU for a device into the rlookup table */
1034static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
1035{
1036 amd_iommu_rlookup_table[devid] = iommu;
1037}
1038
Joerg Roedelb65233a2008-07-11 17:14:21 +02001039/*
1040 * This function takes the device specific flags read from the ACPI
1041 * table and sets up the device table entry with that information
1042 */
Joerg Roedel5ff47892008-07-14 20:11:18 +02001043static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
1044 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +02001045{
1046 if (flags & ACPI_DEVFLAG_INITPASS)
1047 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
1048 if (flags & ACPI_DEVFLAG_EXTINT)
1049 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
1050 if (flags & ACPI_DEVFLAG_NMI)
1051 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
1052 if (flags & ACPI_DEVFLAG_SYSMGT1)
1053 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
1054 if (flags & ACPI_DEVFLAG_SYSMGT2)
1055 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
1056 if (flags & ACPI_DEVFLAG_LINT0)
1057 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
1058 if (flags & ACPI_DEVFLAG_LINT1)
1059 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +02001060
Joerg Roedelc5cca142009-10-09 18:31:20 +02001061 amd_iommu_apply_erratum_63(devid);
1062
Joerg Roedel5ff47892008-07-14 20:11:18 +02001063 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +02001064}
1065
Kai-Heng Feng93d05152019-08-21 13:10:04 +08001066int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +02001067{
1068 struct devid_map *entry;
1069 struct list_head *list;
1070
Joerg Roedel31cff672013-04-09 16:53:58 +02001071 if (type == IVHD_SPECIAL_IOAPIC)
1072 list = &ioapic_map;
1073 else if (type == IVHD_SPECIAL_HPET)
1074 list = &hpet_map;
1075 else
Joerg Roedel6efed632012-06-14 15:52:58 +02001076 return -EINVAL;
1077
Joerg Roedel31cff672013-04-09 16:53:58 +02001078 list_for_each_entry(entry, list, list) {
1079 if (!(entry->id == id && entry->cmd_line))
1080 continue;
1081
Joerg Roedel101fa032018-11-27 16:22:31 +01001082 pr_info("Command-line override present for %s id %d - ignoring\n",
Joerg Roedel31cff672013-04-09 16:53:58 +02001083 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1084
Joerg Roedelc50e3242014-09-09 15:59:37 +02001085 *devid = entry->devid;
1086
Joerg Roedel31cff672013-04-09 16:53:58 +02001087 return 0;
1088 }
1089
Joerg Roedel6efed632012-06-14 15:52:58 +02001090 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1091 if (!entry)
1092 return -ENOMEM;
1093
Joerg Roedel31cff672013-04-09 16:53:58 +02001094 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001095 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +02001096 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +02001097
1098 list_add_tail(&entry->list, list);
1099
1100 return 0;
1101}
1102
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001103static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1104 bool cmd_line)
1105{
1106 struct acpihid_map_entry *entry;
1107 struct list_head *list = &acpihid_map;
1108
1109 list_for_each_entry(entry, list, list) {
1110 if (strcmp(entry->hid, hid) ||
1111 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1112 !entry->cmd_line)
1113 continue;
1114
Joerg Roedel101fa032018-11-27 16:22:31 +01001115 pr_info("Command-line override for hid:%s uid:%s\n",
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001116 hid, uid);
1117 *devid = entry->devid;
1118 return 0;
1119 }
1120
1121 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1122 if (!entry)
1123 return -ENOMEM;
1124
1125 memcpy(entry->uid, uid, strlen(uid));
1126 memcpy(entry->hid, hid, strlen(hid));
1127 entry->devid = *devid;
1128 entry->cmd_line = cmd_line;
1129 entry->root_devid = (entry->devid & (~0x7));
1130
Joerg Roedel101fa032018-11-27 16:22:31 +01001131 pr_info("%s, add hid:%s, uid:%s, rdevid:%d\n",
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001132 entry->cmd_line ? "cmd" : "ivrs",
1133 entry->hid, entry->uid, entry->root_devid);
1134
1135 list_add_tail(&entry->list, list);
1136 return 0;
1137}
1138
Joerg Roedel235dacb2013-04-09 17:53:14 +02001139static int __init add_early_maps(void)
1140{
1141 int i, ret;
1142
1143 for (i = 0; i < early_ioapic_map_size; ++i) {
1144 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1145 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +02001146 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +02001147 early_ioapic_map[i].cmd_line);
1148 if (ret)
1149 return ret;
1150 }
1151
1152 for (i = 0; i < early_hpet_map_size; ++i) {
1153 ret = add_special_device(IVHD_SPECIAL_HPET,
1154 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +02001155 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +02001156 early_hpet_map[i].cmd_line);
1157 if (ret)
1158 return ret;
1159 }
1160
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001161 for (i = 0; i < early_acpihid_map_size; ++i) {
1162 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1163 early_acpihid_map[i].uid,
1164 &early_acpihid_map[i].devid,
1165 early_acpihid_map[i].cmd_line);
1166 if (ret)
1167 return ret;
1168 }
1169
Joerg Roedel235dacb2013-04-09 17:53:14 +02001170 return 0;
1171}
1172
Joerg Roedelb65233a2008-07-11 17:14:21 +02001173/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001174 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1175 * initializes the hardware and our data structures with it.
1176 */
Joerg Roedel6efed632012-06-14 15:52:58 +02001177static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001178 struct ivhd_header *h)
1179{
1180 u8 *p = (u8 *)h;
1181 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +02001182 u16 devid = 0, devid_start = 0, devid_to = 0;
1183 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001184 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001185 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001186 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +02001187 int ret;
1188
1189
1190 ret = add_early_maps();
1191 if (ret)
1192 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001193
Kai-Heng Feng93d05152019-08-21 13:10:04 +08001194 amd_iommu_apply_ivrs_quirks();
1195
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001196 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001197 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001198 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001199 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001200
1201 /*
1202 * Done. Now parse the device entries
1203 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001204 ivhd_size = get_ivhd_header_size(h);
1205 if (!ivhd_size) {
Joerg Roedel101fa032018-11-27 16:22:31 +01001206 pr_err("Unsupported IVHD type %#x\n", h->type);
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001207 return -EINVAL;
1208 }
1209
1210 p += ivhd_size;
1211
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001212 end += h->length;
1213
Joerg Roedel42a698f2009-05-20 15:41:28 +02001214
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001215 while (p < end) {
1216 e = (struct ivhd_entry *)p;
1217 switch (e->type) {
1218 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001219
Joerg Roedel226e8892015-10-20 17:33:44 +02001220 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001221
Joerg Roedel226e8892015-10-20 17:33:44 +02001222 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1223 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001224 break;
1225 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001226
1227 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1228 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001229 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001230 PCI_SLOT(e->devid),
1231 PCI_FUNC(e->devid),
1232 e->flags);
1233
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001234 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001235 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001236 break;
1237 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001238
1239 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1240 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001241 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001242 PCI_SLOT(e->devid),
1243 PCI_FUNC(e->devid),
1244 e->flags);
1245
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001246 devid_start = e->devid;
1247 flags = e->flags;
1248 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001249 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001250 break;
1251 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001252
1253 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1254 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001255 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001256 PCI_SLOT(e->devid),
1257 PCI_FUNC(e->devid),
1258 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001259 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001260 PCI_SLOT(e->ext >> 8),
1261 PCI_FUNC(e->ext >> 8));
1262
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001263 devid = e->devid;
1264 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001265 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001266 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001267 amd_iommu_alias_table[devid] = devid_to;
1268 break;
1269 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001270
1271 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1272 "devid: %02x:%02x.%x flags: %02x "
1273 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001274 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001275 PCI_SLOT(e->devid),
1276 PCI_FUNC(e->devid),
1277 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001278 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001279 PCI_SLOT(e->ext >> 8),
1280 PCI_FUNC(e->ext >> 8));
1281
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001282 devid_start = e->devid;
1283 flags = e->flags;
1284 devid_to = e->ext >> 8;
1285 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001286 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001287 break;
1288 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001289
1290 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1291 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001292 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001293 PCI_SLOT(e->devid),
1294 PCI_FUNC(e->devid),
1295 e->flags, e->ext);
1296
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001297 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001298 set_dev_entry_from_acpi(iommu, devid, e->flags,
1299 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001300 break;
1301 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001302
1303 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1304 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001305 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001306 PCI_SLOT(e->devid),
1307 PCI_FUNC(e->devid),
1308 e->flags, e->ext);
1309
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001310 devid_start = e->devid;
1311 flags = e->flags;
1312 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001313 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001314 break;
1315 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001316
1317 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001318 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001319 PCI_SLOT(e->devid),
1320 PCI_FUNC(e->devid));
1321
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001322 devid = e->devid;
1323 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001324 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001325 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001326 set_dev_entry_from_acpi(iommu,
1327 devid_to, flags, ext_flags);
1328 }
1329 set_dev_entry_from_acpi(iommu, dev_i,
1330 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001331 }
1332 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001333 case IVHD_DEV_SPECIAL: {
1334 u8 handle, type;
1335 const char *var;
1336 u16 devid;
1337 int ret;
1338
1339 handle = e->ext & 0xff;
1340 devid = (e->ext >> 8) & 0xffff;
1341 type = (e->ext >> 24) & 0xff;
1342
1343 if (type == IVHD_SPECIAL_IOAPIC)
1344 var = "IOAPIC";
1345 else if (type == IVHD_SPECIAL_HPET)
1346 var = "HPET";
1347 else
1348 var = "UNKNOWN";
1349
1350 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1351 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001352 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001353 PCI_SLOT(devid),
1354 PCI_FUNC(devid));
1355
Joerg Roedelc50e3242014-09-09 15:59:37 +02001356 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001357 if (ret)
1358 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001359
1360 /*
1361 * add_special_device might update the devid in case a
1362 * command-line override is present. So call
1363 * set_dev_entry_from_acpi after add_special_device.
1364 */
1365 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1366
Joerg Roedel6efed632012-06-14 15:52:58 +02001367 break;
1368 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001369 case IVHD_DEV_ACPI_HID: {
1370 u16 devid;
Alexander Monakove461b8c2020-05-11 10:23:52 +00001371 u8 hid[ACPIHID_HID_LEN];
1372 u8 uid[ACPIHID_UID_LEN];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001373 int ret;
1374
1375 if (h->type != 0x40) {
1376 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1377 e->type);
1378 break;
1379 }
1380
1381 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1382 hid[ACPIHID_HID_LEN - 1] = '\0';
1383
1384 if (!(*hid)) {
1385 pr_err(FW_BUG "Invalid HID.\n");
1386 break;
1387 }
1388
Alexander Monakove461b8c2020-05-11 10:23:52 +00001389 uid[0] = '\0';
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001390 switch (e->uidf) {
1391 case UID_NOT_PRESENT:
1392
1393 if (e->uidl != 0)
1394 pr_warn(FW_BUG "Invalid UID length.\n");
1395
1396 break;
1397 case UID_IS_INTEGER:
1398
1399 sprintf(uid, "%d", e->uid);
1400
1401 break;
1402 case UID_IS_CHARACTER:
1403
Alexander Monakove461b8c2020-05-11 10:23:52 +00001404 memcpy(uid, &e->uid, e->uidl);
1405 uid[e->uidl] = '\0';
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001406
1407 break;
1408 default:
1409 break;
1410 }
1411
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001412 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001413 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1414 hid, uid,
1415 PCI_BUS_NUM(devid),
1416 PCI_SLOT(devid),
1417 PCI_FUNC(devid));
1418
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001419 flags = e->flags;
1420
1421 ret = add_acpi_hid_device(hid, uid, &devid, false);
1422 if (ret)
1423 return ret;
1424
1425 /*
1426 * add_special_device might update the devid in case a
1427 * command-line override is present. So call
1428 * set_dev_entry_from_acpi after add_special_device.
1429 */
1430 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1431
1432 break;
1433 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001434 default:
1435 break;
1436 }
1437
Joerg Roedelb514e552008-09-17 17:14:27 +02001438 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001439 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001440
1441 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001442}
1443
Joerg Roedele47d4022008-06-26 21:27:48 +02001444static void __init free_iommu_one(struct amd_iommu *iommu)
1445{
Suravee Suthikulpanitc69d89a2020-09-23 12:13:45 +00001446 free_cwwb_sem(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001447 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001448 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001449 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001450 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001451 iommu_unmap_mmio_space(iommu);
1452}
1453
1454static void __init free_iommu_all(void)
1455{
1456 struct amd_iommu *iommu, *next;
1457
Joerg Roedel3bd22172009-05-04 15:06:20 +02001458 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001459 list_del(&iommu->list);
1460 free_iommu_one(iommu);
1461 kfree(iommu);
1462 }
1463}
1464
Joerg Roedelb65233a2008-07-11 17:14:21 +02001465/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001466 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1467 * Workaround:
1468 * BIOS should disable L2B micellaneous clock gating by setting
1469 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1470 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001471static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001472{
1473 u32 value;
1474
1475 if ((boot_cpu_data.x86 != 0x15) ||
1476 (boot_cpu_data.x86_model < 0x10) ||
1477 (boot_cpu_data.x86_model > 0x1f))
1478 return;
1479
1480 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1481 pci_read_config_dword(iommu->dev, 0xf4, &value);
1482
1483 if (value & BIT(2))
1484 return;
1485
1486 /* Select NB indirect register 0x90 and enable writing */
1487 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1488
1489 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06001490 pci_info(iommu->dev, "Applying erratum 746 workaround\n");
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001491
1492 /* Clear the enable writing bit */
1493 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1494}
1495
1496/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001497 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1498 * Workaround:
1499 * BIOS should enable ATS write permission check by setting
1500 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1501 */
1502static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1503{
1504 u32 value;
1505
1506 if ((boot_cpu_data.x86 != 0x15) ||
1507 (boot_cpu_data.x86_model < 0x30) ||
1508 (boot_cpu_data.x86_model > 0x3f))
1509 return;
1510
1511 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1512 value = iommu_read_l2(iommu, 0x47);
1513
1514 if (value & BIT(0))
1515 return;
1516
1517 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1518 iommu_write_l2(iommu, 0x47, value | BIT(0));
1519
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06001520 pci_info(iommu->dev, "Applying ATS write check workaround\n");
Jay Cornwall358875f2016-02-10 15:48:01 -06001521}
1522
1523/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001524 * This function clues the initialization function for one IOMMU
1525 * together and also allocates the command buffer and programs the
1526 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1527 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001528static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1529{
Joerg Roedel6efed632012-06-14 15:52:58 +02001530 int ret;
1531
Scott Wood27790392018-01-21 03:28:54 -06001532 raw_spin_lock_init(&iommu->lock);
Suravee Suthikulpanitc69d89a2020-09-23 12:13:45 +00001533 iommu->cmd_sem_val = 0;
Joerg Roedelbb527772009-11-20 14:31:51 +01001534
1535 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001536 list_add_tail(&iommu->list, &amd_iommu_list);
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001537 iommu->index = amd_iommus_present++;
Joerg Roedelbb527772009-11-20 14:31:51 +01001538
1539 if (unlikely(iommu->index >= MAX_IOMMUS)) {
Joerg Roedel101fa032018-11-27 16:22:31 +01001540 WARN(1, "System has more IOMMUs than supported by this driver\n");
Joerg Roedelbb527772009-11-20 14:31:51 +01001541 return -ENOSYS;
1542 }
1543
1544 /* Index is fine - add IOMMU to the array */
1545 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001546
1547 /*
1548 * Copy data from ACPI table entry to the iommu struct
1549 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001550 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001551 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001552 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001553 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001554
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001555 switch (h->type) {
1556 case 0x10:
1557 /* Check if IVHD EFR contains proper max banks/counters */
1558 if ((h->efr_attr != 0) &&
1559 ((h->efr_attr & (0xF << 13)) != 0) &&
1560 ((h->efr_attr & (0x3F << 17)) != 0))
1561 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1562 else
1563 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanite52d58d2020-09-03 09:38:22 +00001564
1565 /*
1566 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1567 * GAM also requires GA mode. Therefore, we need to
1568 * check cmpxchg16b support before enabling it.
1569 */
1570 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1571 ((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001572 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001573 break;
1574 case 0x11:
1575 case 0x40:
1576 if (h->efr_reg & (1 << 9))
1577 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1578 else
1579 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanite52d58d2020-09-03 09:38:22 +00001580
1581 /*
1582 * Note: GA (128-bit IRTE) mode requires cmpxchg16b supports.
1583 * XT, GAM also requires GA mode. Therefore, we need to
1584 * check cmpxchg16b support before enabling them.
1585 */
1586 if (!boot_cpu_has(X86_FEATURE_CX16) ||
1587 ((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001588 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanite52d58d2020-09-03 09:38:22 +00001589 break;
1590 }
1591
David Woodhoused1adcfb2020-11-11 12:09:01 +00001592 if (h->efr_reg & BIT(IOMMU_EFR_XTSUP_SHIFT))
Suravee Suthikulpanit81307142019-11-20 07:55:48 -06001593 amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
Suravee Suthikulpanita44092e2021-01-20 07:50:02 -06001594
1595 early_iommu_features_init(iommu, h);
1596
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001597 break;
1598 default:
1599 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001600 }
1601
1602 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1603 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001604 if (!iommu->mmio_base)
1605 return -ENOMEM;
1606
Suravee Suthikulpanitc69d89a2020-09-23 12:13:45 +00001607 if (alloc_cwwb_sem(iommu))
1608 return -ENOMEM;
1609
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001610 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001611 return -ENOMEM;
1612
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001613 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001614 return -ENOMEM;
1615
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001616 iommu->int_enabled = false;
1617
Baoquan He4c232a72017-08-09 16:33:33 +08001618 init_translation_status(iommu);
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08001619 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1620 iommu_disable(iommu);
1621 clear_translation_pre_enabled(iommu);
1622 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1623 iommu->index);
1624 }
1625 if (amd_iommu_pre_enabled)
1626 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
Baoquan He4c232a72017-08-09 16:33:33 +08001627
Joerg Roedel6efed632012-06-14 15:52:58 +02001628 ret = init_iommu_from_acpi(iommu, h);
1629 if (ret)
1630 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001631
David Woodhouse2df985f2020-11-11 14:43:20 +00001632 if (amd_iommu_irq_remap) {
1633 ret = amd_iommu_create_irq_domain(iommu);
1634 if (ret)
1635 return ret;
1636 }
Jiang Liu7c71d302015-04-13 14:11:33 +08001637
Joerg Roedelf6fec002012-06-21 16:51:25 +02001638 /*
1639 * Make sure IOMMU is not considered to translate itself. The IVRS
1640 * table tells us so, but this is a lie!
1641 */
1642 amd_iommu_rlookup_table[iommu->devid] = NULL;
1643
Joerg Roedel23c742d2012-06-12 11:47:34 +02001644 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001645}
1646
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001647/**
1648 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
Krzysztof Kozlowski06ce8a62c2020-07-28 19:08:57 +02001649 * @ivrs: Pointer to the IVRS header
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001650 *
1651 * This function search through all IVDB of the maximum supported IVHD
1652 */
1653static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1654{
1655 u8 *base = (u8 *)ivrs;
1656 struct ivhd_header *ivhd = (struct ivhd_header *)
1657 (base + IVRS_HEADER_LENGTH);
1658 u8 last_type = ivhd->type;
1659 u16 devid = ivhd->devid;
1660
1661 while (((u8 *)ivhd - base < ivrs->length) &&
1662 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1663 u8 *p = (u8 *) ivhd;
1664
1665 if (ivhd->devid == devid)
1666 last_type = ivhd->type;
1667 ivhd = (struct ivhd_header *)(p + ivhd->length);
1668 }
1669
1670 return last_type;
1671}
1672
Joerg Roedelb65233a2008-07-11 17:14:21 +02001673/*
1674 * Iterates over all IOMMU entries in the ACPI table, allocates the
1675 * IOMMU structure and initializes it with init_iommu_one()
1676 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001677static int __init init_iommu_all(struct acpi_table_header *table)
1678{
1679 u8 *p = (u8 *)table, *end = (u8 *)table;
1680 struct ivhd_header *h;
1681 struct amd_iommu *iommu;
1682 int ret;
1683
Joerg Roedele47d4022008-06-26 21:27:48 +02001684 end += table->length;
1685 p += IVRS_HEADER_LENGTH;
1686
1687 while (p < end) {
1688 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001689 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001690
Joerg Roedelae908c22009-09-01 16:52:16 +02001691 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001692 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001693 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001694 PCI_FUNC(h->devid), h->cap_ptr,
1695 h->pci_seg, h->flags, h->info);
1696 DUMP_printk(" mmio-addr: %016llx\n",
1697 h->mmio_phys);
1698
Joerg Roedele47d4022008-06-26 21:27:48 +02001699 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001700 if (iommu == NULL)
1701 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001702
Joerg Roedele47d4022008-06-26 21:27:48 +02001703 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001704 if (ret)
1705 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001706 }
1707 p += h->length;
1708
1709 }
1710 WARN_ON(p != end);
1711
1712 return 0;
1713}
1714
Paul Menzel715601e2021-04-09 03:58:47 -05001715static void init_iommu_perf_ctr(struct amd_iommu *iommu)
Steven L Kinney30861dd2013-06-05 16:11:48 -05001716{
Suravee Suthikulpanit994d6602021-04-09 03:58:48 -05001717 u64 val;
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06001718 struct pci_dev *pdev = iommu->dev;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001719
1720 if (!iommu_feature(iommu, FEATURE_PC))
1721 return;
1722
1723 amd_iommu_pc_present = true;
1724
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06001725 pci_info(pdev, "IOMMU performance counters supported\n");
Steven L Kinney30861dd2013-06-05 16:11:48 -05001726
1727 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1728 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1729 iommu->max_counters = (u8) ((val >> 7) & 0xf);
Shuah Khan8c17bbf2020-01-23 15:32:14 -07001730
1731 return;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001732}
1733
Alex Williamson066f2e92014-06-12 16:12:37 -06001734static ssize_t amd_iommu_show_cap(struct device *dev,
1735 struct device_attribute *attr,
1736 char *buf)
1737{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001738 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001739 return sprintf(buf, "%x\n", iommu->cap);
1740}
1741static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1742
1743static ssize_t amd_iommu_show_features(struct device *dev,
1744 struct device_attribute *attr,
1745 char *buf)
1746{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001747 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001748 return sprintf(buf, "%llx\n", iommu->features);
1749}
1750static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1751
1752static struct attribute *amd_iommu_attrs[] = {
1753 &dev_attr_cap.attr,
1754 &dev_attr_features.attr,
1755 NULL,
1756};
1757
1758static struct attribute_group amd_iommu_group = {
1759 .name = "amd-iommu",
1760 .attrs = amd_iommu_attrs,
1761};
1762
1763static const struct attribute_group *amd_iommu_groups[] = {
1764 &amd_iommu_group,
1765 NULL,
1766};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001767
Suravee Suthikulpanita44092e2021-01-20 07:50:02 -06001768/*
1769 * Note: IVHD 0x11 and 0x40 also contains exact copy
1770 * of the IOMMU Extended Feature Register [MMIO Offset 0030h].
1771 * Default to EFR in IVHD since it is available sooner (i.e. before PCI init).
1772 */
1773static void __init late_iommu_features_init(struct amd_iommu *iommu)
1774{
1775 u64 features;
1776
1777 if (!(iommu->cap & (1 << IOMMU_CAP_EFR)))
1778 return;
1779
1780 /* read extended feature bits */
1781 features = readq(iommu->mmio_base + MMIO_EXT_FEATURES);
1782
1783 if (!iommu->features) {
1784 iommu->features = features;
1785 return;
1786 }
1787
1788 /*
1789 * Sanity check and warn if EFR values from
1790 * IVHD and MMIO conflict.
1791 */
1792 if (features != iommu->features)
Paul Menzel304c73b2021-04-12 20:01:41 +02001793 pr_warn(FW_WARN "EFR mismatch. Use IVHD EFR (%#llx : %#llx).\n",
Suravee Suthikulpanita44092e2021-01-20 07:50:02 -06001794 features, iommu->features);
1795}
1796
Joerg Roedel24d2c522018-10-05 12:32:46 +02001797static int __init iommu_init_pci(struct amd_iommu *iommu)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001798{
1799 int cap_ptr = iommu->cap_ptr;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001800 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001801
Sinan Kayad5bf0f42017-12-19 00:37:47 -05001802 iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
1803 iommu->devid & 0xff);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001804 if (!iommu->dev)
1805 return -ENODEV;
1806
Jiang Liucbbc00b2015-10-09 22:07:31 +08001807 /* Prevent binding other PCI device drivers to IOMMU devices */
1808 iommu->dev->match_driver = false;
1809
Joerg Roedel23c742d2012-06-12 11:47:34 +02001810 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1811 &iommu->cap);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001812
Joerg Roedel23c742d2012-06-12 11:47:34 +02001813 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1814 amd_iommu_iotlb_sup = false;
1815
Suravee Suthikulpanita44092e2021-01-20 07:50:02 -06001816 late_iommu_features_init(iommu);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001817
1818 if (iommu_feature(iommu, FEATURE_GT)) {
1819 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001820 u32 max_pasid;
1821 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001822
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001823 pasmax = iommu->features & FEATURE_PASID_MASK;
1824 pasmax >>= FEATURE_PASID_SHIFT;
1825 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001826
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001827 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1828
1829 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001830
1831 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1832 glxval >>= FEATURE_GLXVAL_SHIFT;
1833
1834 if (amd_iommu_max_glx_val == -1)
1835 amd_iommu_max_glx_val = glxval;
1836 else
1837 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1838 }
1839
1840 if (iommu_feature(iommu, FEATURE_GT) &&
1841 iommu_feature(iommu, FEATURE_PPR)) {
1842 iommu->is_iommu_v2 = true;
1843 amd_iommu_v2_present = true;
1844 }
1845
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001846 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1847 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001848
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001849 ret = iommu_init_ga(iommu);
1850 if (ret)
1851 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001852
Nadav Amit66643402021-07-23 02:32:04 -07001853 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE)) {
1854 if (!amd_iommu_unmap_flush)
1855 pr_info("IOMMU batching is disabled due to virtualization\n");
1856
Joerg Roedel23c742d2012-06-12 11:47:34 +02001857 amd_iommu_np_cache = true;
Nadav Amit66643402021-07-23 02:32:04 -07001858 amd_iommu_unmap_flush = true;
1859 }
Joerg Roedel23c742d2012-06-12 11:47:34 +02001860
Steven L Kinney30861dd2013-06-05 16:11:48 -05001861 init_iommu_perf_ctr(iommu);
1862
Joerg Roedel23c742d2012-06-12 11:47:34 +02001863 if (is_rd890_iommu(iommu->dev)) {
1864 int i, j;
1865
Sinan Kayad5bf0f42017-12-19 00:37:47 -05001866 iommu->root_pdev =
1867 pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
1868 PCI_DEVFN(0, 0));
Joerg Roedel23c742d2012-06-12 11:47:34 +02001869
1870 /*
1871 * Some rd890 systems may not be fully reconfigured by the
1872 * BIOS, so it's necessary for us to store this information so
1873 * it can be reprogrammed on resume
1874 */
1875 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1876 &iommu->stored_addr_lo);
1877 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1878 &iommu->stored_addr_hi);
1879
1880 /* Low bit locks writes to configuration space */
1881 iommu->stored_addr_lo &= ~1;
1882
1883 for (i = 0; i < 6; i++)
1884 for (j = 0; j < 0x12; j++)
1885 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1886
1887 for (i = 0; i < 0x83; i++)
1888 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1889 }
1890
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001891 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001892 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001893
Joerg Roedel39ab9552017-02-01 16:56:46 +01001894 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1895 amd_iommu_groups, "ivhd%d", iommu->index);
Robin Murphy2d471b22021-04-01 14:56:26 +01001896 iommu_device_register(&iommu->iommu, &amd_iommu_ops, NULL);
Alex Williamson066f2e92014-06-12 16:12:37 -06001897
Joerg Roedel23c742d2012-06-12 11:47:34 +02001898 return pci_enable_device(iommu->dev);
1899}
1900
Joerg Roedel4d121c32012-06-14 12:21:55 +02001901static void print_iommu_info(void)
1902{
1903 static const char * const feat_str[] = {
1904 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1905 "IA", "GA", "HE", "PC"
1906 };
1907 struct amd_iommu *iommu;
1908
1909 for_each_iommu(iommu) {
Bjorn Helgaas5f226da2019-02-08 16:05:53 -06001910 struct pci_dev *pdev = iommu->dev;
Joerg Roedel4d121c32012-06-14 12:21:55 +02001911 int i;
1912
Tom Rix3703c832020-12-15 13:30:21 -08001913 pci_info(pdev, "Found IOMMU cap 0x%x\n", iommu->cap_ptr);
Joerg Roedel4d121c32012-06-14 12:21:55 +02001914
1915 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Alexander Monakov4b21a502021-05-04 13:22:20 +03001916 pr_info("Extended features (%#llx):", iommu->features);
1917
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001918 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001919 if (iommu_feature(iommu, (1ULL << i)))
1920 pr_cont(" %s", feat_str[i]);
1921 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001922
1923 if (iommu->features & FEATURE_GAM_VAPIC)
1924 pr_cont(" GA_vAPIC");
1925
Steven L Kinney30861dd2013-06-05 16:11:48 -05001926 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001927 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001928 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001929 if (irq_remapping_enabled) {
Joerg Roedel101fa032018-11-27 16:22:31 +01001930 pr_info("Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001931 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
Joerg Roedel101fa032018-11-27 16:22:31 +01001932 pr_info("Virtual APIC enabled\n");
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05001933 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
Joerg Roedel101fa032018-11-27 16:22:31 +01001934 pr_info("X2APIC enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001935 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001936}
1937
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001938static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001939{
1940 struct amd_iommu *iommu;
Adrian Huangf8993dc2020-12-10 10:13:30 +08001941 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001942
1943 for_each_iommu(iommu) {
1944 ret = iommu_init_pci(iommu);
1945 if (ret)
1946 break;
Suravee Suthikulpanit54ce12e2020-09-23 12:13:47 +00001947
1948 /* Need to setup range after PCI init */
1949 iommu_set_cwwb_range(iommu);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001950 }
1951
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001952 /*
1953 * Order is important here to make sure any unity map requirements are
1954 * fulfilled. The unity mappings are created and written to the device
1955 * table during the amd_iommu_init_api() call.
1956 *
1957 * After that we call init_device_table_dma() to make sure any
1958 * uninitialized DTE will block DMA, and in the end we flush the caches
1959 * of all IOMMUs to make sure the changes to the device table are
1960 * active.
1961 */
1962 ret = amd_iommu_init_api();
1963
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001964 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001965
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001966 for_each_iommu(iommu)
1967 iommu_flush_all_caches(iommu);
1968
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001969 if (!ret)
1970 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001971
Joerg Roedel23c742d2012-06-12 11:47:34 +02001972 return ret;
1973}
1974
Joerg Roedelb65233a2008-07-11 17:14:21 +02001975/****************************************************************************
1976 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001977 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001978 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001979 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1980 * pci_dev.
1981 *
1982 ****************************************************************************/
1983
Joerg Roedel9f800de2009-11-23 12:45:25 +01001984static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001985{
1986 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001987
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001988 r = pci_enable_msi(iommu->dev);
1989 if (r)
1990 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001991
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001992 r = request_threaded_irq(iommu->dev->irq,
1993 amd_iommu_int_handler,
1994 amd_iommu_int_thread,
1995 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001996 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001997
1998 if (r) {
1999 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01002000 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02002001 }
2002
2003 return 0;
2004}
2005
Thomas Gleixnerb5c37862020-10-24 22:35:14 +01002006union intcapxt {
2007 u64 capxt;
David Woodhouse2fb6acf2020-11-11 14:43:21 +00002008 struct {
2009 u64 reserved_0 : 2,
2010 dest_mode_logical : 1,
2011 reserved_1 : 5,
2012 destid_0_23 : 24,
2013 vector : 8,
2014 reserved_2 : 16,
2015 destid_24_31 : 8;
2016 };
Thomas Gleixnerb5c37862020-10-24 22:35:14 +01002017} __attribute__ ((packed));
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002018
Krzysztof Kozlowski06ce8a62c2020-07-28 19:08:57 +02002019/*
David Woodhoused1adcfb2020-11-11 12:09:01 +00002020 * There isn't really any need to mask/unmask at the irqchip level because
2021 * the 64-bit INTCAPXT registers can be updated atomically without tearing
2022 * when the affinity is being updated.
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002023 */
David Woodhoused1adcfb2020-11-11 12:09:01 +00002024static void intcapxt_unmask_irq(struct irq_data *data)
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002025{
David Woodhoused1adcfb2020-11-11 12:09:01 +00002026}
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002027
David Woodhoused1adcfb2020-11-11 12:09:01 +00002028static void intcapxt_mask_irq(struct irq_data *data)
2029{
2030}
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002031
David Woodhoused1adcfb2020-11-11 12:09:01 +00002032static struct irq_chip intcapxt_controller;
2033
2034static int intcapxt_irqdomain_activate(struct irq_domain *domain,
2035 struct irq_data *irqd, bool reserve)
2036{
2037 struct amd_iommu *iommu = irqd->chip_data;
2038 struct irq_cfg *cfg = irqd_cfg(irqd);
Thomas Gleixnerb5c37862020-10-24 22:35:14 +01002039 union intcapxt xt;
Thomas Gleixnerb5c37862020-10-24 22:35:14 +01002040
2041 xt.capxt = 0ULL;
David Woodhoused1adcfb2020-11-11 12:09:01 +00002042 xt.dest_mode_logical = apic->dest_mode_logical;
2043 xt.vector = cfg->vector;
2044 xt.destid_0_23 = cfg->dest_apicid & GENMASK(23, 0);
2045 xt.destid_24_31 = cfg->dest_apicid >> 24;
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002046
2047 /**
2048 * Current IOMMU implemtation uses the same IRQ for all
2049 * 3 IOMMU interrupts.
2050 */
Thomas Gleixnerb5c37862020-10-24 22:35:14 +01002051 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_EVT_OFFSET);
2052 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_PPR_OFFSET);
2053 writeq(xt.capxt, iommu->mmio_base + MMIO_INTCAPXT_GALOG_OFFSET);
David Woodhoused1adcfb2020-11-11 12:09:01 +00002054 return 0;
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002055}
2056
David Woodhoused1adcfb2020-11-11 12:09:01 +00002057static void intcapxt_irqdomain_deactivate(struct irq_domain *domain,
2058 struct irq_data *irqd)
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002059{
David Woodhoused1adcfb2020-11-11 12:09:01 +00002060 intcapxt_mask_irq(irqd);
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002061}
2062
David Woodhoused1adcfb2020-11-11 12:09:01 +00002063
2064static int intcapxt_irqdomain_alloc(struct irq_domain *domain, unsigned int virq,
2065 unsigned int nr_irqs, void *arg)
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002066{
David Woodhoused1adcfb2020-11-11 12:09:01 +00002067 struct irq_alloc_info *info = arg;
2068 int i, ret;
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002069
David Woodhoused1adcfb2020-11-11 12:09:01 +00002070 if (!info || info->type != X86_IRQ_ALLOC_TYPE_AMDVI)
2071 return -EINVAL;
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002072
David Woodhoused1adcfb2020-11-11 12:09:01 +00002073 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, arg);
2074 if (ret < 0)
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002075 return ret;
David Woodhoused1adcfb2020-11-11 12:09:01 +00002076
2077 for (i = virq; i < virq + nr_irqs; i++) {
2078 struct irq_data *irqd = irq_domain_get_irq_data(domain, i);
2079
2080 irqd->chip = &intcapxt_controller;
2081 irqd->chip_data = info->data;
2082 __irq_set_handler(i, handle_edge_irq, 0, "edge");
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002083 }
2084
Suthikulpanit, Suravee66929812019-07-16 04:29:16 +00002085 return ret;
2086}
2087
David Woodhoused1adcfb2020-11-11 12:09:01 +00002088static void intcapxt_irqdomain_free(struct irq_domain *domain, unsigned int virq,
2089 unsigned int nr_irqs)
2090{
2091 irq_domain_free_irqs_top(domain, virq, nr_irqs);
2092}
2093
2094static int intcapxt_set_affinity(struct irq_data *irqd,
2095 const struct cpumask *mask, bool force)
2096{
2097 struct irq_data *parent = irqd->parent_data;
2098 int ret;
2099
2100 ret = parent->chip->irq_set_affinity(parent, mask, force);
2101 if (ret < 0 || ret == IRQ_SET_MASK_OK_DONE)
2102 return ret;
2103
2104 return intcapxt_irqdomain_activate(irqd->domain, irqd, false);
2105}
2106
2107static struct irq_chip intcapxt_controller = {
2108 .name = "IOMMU-MSI",
2109 .irq_unmask = intcapxt_unmask_irq,
2110 .irq_mask = intcapxt_mask_irq,
2111 .irq_ack = irq_chip_ack_parent,
2112 .irq_retrigger = irq_chip_retrigger_hierarchy,
2113 .irq_set_affinity = intcapxt_set_affinity,
2114 .flags = IRQCHIP_SKIP_SET_WAKE,
2115};
2116
2117static const struct irq_domain_ops intcapxt_domain_ops = {
2118 .alloc = intcapxt_irqdomain_alloc,
2119 .free = intcapxt_irqdomain_free,
2120 .activate = intcapxt_irqdomain_activate,
2121 .deactivate = intcapxt_irqdomain_deactivate,
2122};
2123
2124
2125static struct irq_domain *iommu_irqdomain;
2126
2127static struct irq_domain *iommu_get_irqdomain(void)
2128{
2129 struct fwnode_handle *fn;
2130
2131 /* No need for locking here (yet) as the init is single-threaded */
2132 if (iommu_irqdomain)
2133 return iommu_irqdomain;
2134
2135 fn = irq_domain_alloc_named_fwnode("AMD-Vi-MSI");
2136 if (!fn)
2137 return NULL;
2138
2139 iommu_irqdomain = irq_domain_create_hierarchy(x86_vector_domain, 0, 0,
2140 fn, &intcapxt_domain_ops,
2141 NULL);
2142 if (!iommu_irqdomain)
2143 irq_domain_free_fwnode(fn);
2144
2145 return iommu_irqdomain;
2146}
2147
2148static int iommu_setup_intcapxt(struct amd_iommu *iommu)
2149{
2150 struct irq_domain *domain;
2151 struct irq_alloc_info info;
2152 int irq, ret;
2153
2154 domain = iommu_get_irqdomain();
2155 if (!domain)
2156 return -ENXIO;
2157
2158 init_irq_alloc_info(&info, NULL);
2159 info.type = X86_IRQ_ALLOC_TYPE_AMDVI;
2160 info.data = iommu;
2161
2162 irq = irq_domain_alloc_irqs(domain, 1, NUMA_NO_NODE, &info);
2163 if (irq < 0) {
2164 irq_domain_remove(domain);
2165 return irq;
2166 }
2167
2168 ret = request_threaded_irq(irq, amd_iommu_int_handler,
2169 amd_iommu_int_thread, 0, "AMD-Vi", iommu);
2170 if (ret) {
2171 irq_domain_free_irqs(irq, 1);
2172 irq_domain_remove(domain);
2173 return ret;
2174 }
2175
2176 iommu_feature_enable(iommu, CONTROL_INTCAPXT_EN);
2177 return 0;
2178}
2179
2180static int iommu_init_irq(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02002181{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01002182 int ret;
2183
Joerg Roedela80dc3e2008-09-11 16:51:41 +02002184 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01002185 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02002186
David Woodhoused1adcfb2020-11-11 12:09:01 +00002187 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
2188 ret = iommu_setup_intcapxt(iommu);
2189 else if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01002190 ret = iommu_setup_msi(iommu);
2191 else
2192 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02002193
Joerg Roedel9ddd5922012-03-15 16:29:47 +01002194 if (ret)
2195 return ret;
2196
David Woodhouse12bc4572021-01-05 01:32:51 +00002197 iommu->int_enabled = true;
Joerg Roedel9ddd5922012-03-15 16:29:47 +01002198enable_faults:
2199 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
2200
2201 if (iommu->ppr_log != NULL)
Adrian Huangbde9e6b2019-12-30 13:56:54 +08002202 iommu_feature_enable(iommu, CONTROL_PPRINT_EN);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01002203
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05002204 iommu_ga_log_enable(iommu);
2205
Joerg Roedel9ddd5922012-03-15 16:29:47 +01002206 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02002207}
2208
2209/****************************************************************************
2210 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02002211 * The next functions belong to the third pass of parsing the ACPI
2212 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02002213 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02002214 *
2215 ****************************************************************************/
2216
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002217static void __init free_unity_maps(void)
2218{
2219 struct unity_map_entry *entry, *next;
2220
2221 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
2222 list_del(&entry->list);
2223 kfree(entry);
2224 }
2225}
2226
Joerg Roedelb65233a2008-07-11 17:14:21 +02002227/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002228static int __init init_unity_map_range(struct ivmd_header *m)
2229{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002230 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02002231 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002232
2233 e = kzalloc(sizeof(*e), GFP_KERNEL);
2234 if (e == NULL)
2235 return -ENOMEM;
2236
2237 switch (m->type) {
2238 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02002239 kfree(e);
2240 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002241 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02002242 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002243 e->devid_start = e->devid_end = m->devid;
2244 break;
2245 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02002246 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002247 e->devid_start = 0;
2248 e->devid_end = amd_iommu_last_bdf;
2249 break;
2250 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02002251 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002252 e->devid_start = m->devid;
2253 e->devid_end = m->aux;
2254 break;
2255 }
2256 e->address_start = PAGE_ALIGN(m->range_start);
2257 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2258 e->prot = m->flags >> 1;
2259
Adrian Huang0bbe4ce2020-09-26 18:26:02 +08002260 /*
2261 * Treat per-device exclusion ranges as r/w unity-mapped regions
2262 * since some buggy BIOSes might lead to the overwritten exclusion
2263 * range (exclusion_start and exclusion_length members). This
2264 * happens when there are multiple exclusion ranges (IVMD entries)
2265 * defined in ACPI table.
2266 */
2267 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2268 e->prot = (IVMD_FLAG_IW | IVMD_FLAG_IR) >> 1;
2269
Joerg Roedel02acc432009-05-20 16:24:21 +02002270 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2271 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07002272 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2273 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02002274 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2275 e->address_start, e->address_end, m->flags);
2276
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002277 list_add_tail(&e->list, &amd_iommu_unity_map);
2278
2279 return 0;
2280}
2281
Joerg Roedelb65233a2008-07-11 17:14:21 +02002282/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002283static int __init init_memory_definitions(struct acpi_table_header *table)
2284{
2285 u8 *p = (u8 *)table, *end = (u8 *)table;
2286 struct ivmd_header *m;
2287
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002288 end += table->length;
2289 p += IVRS_HEADER_LENGTH;
2290
2291 while (p < end) {
2292 m = (struct ivmd_header *)p;
Joerg Roedel8aafaaf2019-03-28 11:44:59 +01002293 if (m->flags & (IVMD_FLAG_UNITY_MAP | IVMD_FLAG_EXCL_RANGE))
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002294 init_unity_map_range(m);
2295
2296 p += m->length;
2297 }
2298
2299 return 0;
2300}
2301
Joerg Roedelb65233a2008-07-11 17:14:21 +02002302/*
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002303 * Init the device table to not allow DMA access for devices
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002304 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02002305static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002306{
Joerg Roedel0de66d52011-06-06 16:04:02 +02002307 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002308
2309 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2310 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2311 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002312 }
2313}
2314
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002315static void __init uninit_device_table_dma(void)
2316{
2317 u32 devid;
2318
2319 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2320 amd_iommu_dev_table[devid].data[0] = 0ULL;
2321 amd_iommu_dev_table[devid].data[1] = 0ULL;
2322 }
2323}
2324
Joerg Roedel33f28c52012-06-15 18:03:31 +02002325static void init_device_table(void)
2326{
2327 u32 devid;
2328
2329 if (!amd_iommu_irq_remap)
2330 return;
2331
2332 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2333 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2334}
2335
Joerg Roedele9bf5192010-09-20 14:33:07 +02002336static void iommu_init_flags(struct amd_iommu *iommu)
2337{
2338 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2339 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2340 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2341
2342 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2343 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2344 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2345
2346 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2347 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2348 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2349
2350 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2351 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2352 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2353
2354 /*
2355 * make IOMMU memory accesses cache coherent
2356 */
2357 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01002358
2359 /* Set IOTLB invalidation timeout to 1s */
2360 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002361}
2362
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002363static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02002364{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002365 int i, j;
2366 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002367 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002368
2369 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002370 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002371 return;
2372
2373 /*
2374 * First, we need to ensure that the iommu is enabled. This is
2375 * controlled by a register in the northbridge
2376 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002377
2378 /* Select Northbridge indirect register 0x75 and enable writing */
2379 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2380 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2381
2382 /* Enable the iommu */
2383 if (!(ioc_feature_control & 0x1))
2384 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2385
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002386 /* Restore the iommu BAR */
2387 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2388 iommu->stored_addr_lo);
2389 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2390 iommu->stored_addr_hi);
2391
2392 /* Restore the l1 indirect regs for each of the 6 l1s */
2393 for (i = 0; i < 6; i++)
2394 for (j = 0; j < 0x12; j++)
2395 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2396
2397 /* Restore the l2 indirect regs */
2398 for (i = 0; i < 0x83; i++)
2399 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2400
2401 /* Lock PCI setup registers */
2402 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2403 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02002404}
2405
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002406static void iommu_enable_ga(struct amd_iommu *iommu)
2407{
2408#ifdef CONFIG_IRQ_REMAP
2409 switch (amd_iommu_guest_ir) {
2410 case AMD_IOMMU_GUEST_IR_VAPIC:
2411 iommu_feature_enable(iommu, CONTROL_GAM_EN);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05002412 fallthrough;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002413 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2414 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002415 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002416 break;
2417 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002418 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002419 break;
2420 }
2421#endif
2422}
2423
Baoquan He78d313c2017-08-09 16:33:34 +08002424static void early_enable_iommu(struct amd_iommu *iommu)
2425{
2426 iommu_disable(iommu);
2427 iommu_init_flags(iommu);
2428 iommu_set_device_table(iommu);
2429 iommu_enable_command_buffer(iommu);
2430 iommu_enable_event_buffer(iommu);
2431 iommu_set_exclusion_range(iommu);
2432 iommu_enable_ga(iommu);
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05002433 iommu_enable_xt(iommu);
Baoquan He78d313c2017-08-09 16:33:34 +08002434 iommu_enable(iommu);
2435 iommu_flush_all_caches(iommu);
2436}
2437
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002438/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002439 * This function finally enables all IOMMUs found in the system after
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002440 * they have been initialized.
2441 *
2442 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2443 * the old content of device table entries. Not this case or copy failed,
2444 * just continue as normal kernel does.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002445 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002446static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002447{
2448 struct amd_iommu *iommu;
2449
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002450
2451 if (!copy_device_table()) {
2452 /*
2453 * If come here because of failure in copying device table from old
2454 * kernel with all IOMMUs enabled, print error message and try to
2455 * free allocated old_dev_tbl_cpy.
2456 */
2457 if (amd_iommu_pre_enabled)
2458 pr_err("Failed to copy DEV table from previous kernel.\n");
2459 if (old_dev_tbl_cpy != NULL)
2460 free_pages((unsigned long)old_dev_tbl_cpy,
2461 get_order(dev_table_size));
2462
2463 for_each_iommu(iommu) {
2464 clear_translation_pre_enabled(iommu);
2465 early_enable_iommu(iommu);
2466 }
2467 } else {
2468 pr_info("Copied DEV table from previous kernel.\n");
2469 free_pages((unsigned long)amd_iommu_dev_table,
2470 get_order(dev_table_size));
2471 amd_iommu_dev_table = old_dev_tbl_cpy;
2472 for_each_iommu(iommu) {
2473 iommu_disable_command_buffer(iommu);
2474 iommu_disable_event_buffer(iommu);
2475 iommu_enable_command_buffer(iommu);
2476 iommu_enable_event_buffer(iommu);
2477 iommu_enable_ga(iommu);
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05002478 iommu_enable_xt(iommu);
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002479 iommu_set_device_table(iommu);
2480 iommu_flush_all_caches(iommu);
2481 }
Joerg Roedel87361972008-06-26 21:28:07 +02002482 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002483
2484#ifdef CONFIG_IRQ_REMAP
2485 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2486 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2487#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002488}
2489
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002490static void enable_iommus_v2(void)
2491{
2492 struct amd_iommu *iommu;
2493
2494 for_each_iommu(iommu) {
2495 iommu_enable_ppr_log(iommu);
2496 iommu_enable_gt(iommu);
2497 }
2498}
2499
2500static void enable_iommus(void)
2501{
2502 early_enable_iommus();
2503
2504 enable_iommus_v2();
2505}
2506
Joerg Roedel92ac4322009-05-19 19:06:27 +02002507static void disable_iommus(void)
2508{
2509 struct amd_iommu *iommu;
2510
2511 for_each_iommu(iommu)
2512 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002513
2514#ifdef CONFIG_IRQ_REMAP
2515 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2516 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2517#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002518}
2519
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002520/*
2521 * Suspend/Resume support
2522 * disable suspend until real resume implemented
2523 */
2524
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002525static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002526{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002527 struct amd_iommu *iommu;
2528
2529 for_each_iommu(iommu)
2530 iommu_apply_resume_quirks(iommu);
2531
Joerg Roedel736501e2009-05-12 09:56:12 +02002532 /* re-load the hardware */
2533 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002534
2535 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002536}
2537
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002538static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002539{
Joerg Roedel736501e2009-05-12 09:56:12 +02002540 /* disable IOMMUs to go out of the way for BIOS */
2541 disable_iommus();
2542
2543 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002544}
2545
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002546static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002547 .suspend = amd_iommu_suspend,
2548 .resume = amd_iommu_resume,
2549};
2550
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002551static void __init free_iommu_resources(void)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002552{
Lucas Stachebcfa282016-10-26 13:09:53 +02002553 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002554 free_pages((unsigned long)irq_lookup_table,
2555 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002556 irq_lookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002557
Julia Lawalla5919892015-09-13 14:15:31 +02002558 kmem_cache_destroy(amd_iommu_irq_cache);
2559 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002560
2561 free_pages((unsigned long)amd_iommu_rlookup_table,
2562 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002563 amd_iommu_rlookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002564
2565 free_pages((unsigned long)amd_iommu_alias_table,
2566 get_order(alias_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002567 amd_iommu_alias_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002568
2569 free_pages((unsigned long)amd_iommu_dev_table,
2570 get_order(dev_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002571 amd_iommu_dev_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002572
2573 free_iommu_all();
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002574}
2575
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002576/* SB IOAPIC is always on this device in AMD systems */
2577#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2578
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002579static bool __init check_ioapic_information(void)
2580{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002581 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002582 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002583 int idx;
2584
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002585 has_sb_ioapic = false;
2586 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002587
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002588 /*
2589 * If we have map overrides on the kernel command line the
2590 * messages in this function might not describe firmware bugs
2591 * anymore - so be careful
2592 */
2593 if (cmdline_maps)
2594 fw_bug = "";
2595
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002596 for (idx = 0; idx < nr_ioapics; idx++) {
2597 int devid, id = mpc_ioapic_id(idx);
2598
2599 devid = get_ioapic_devid(id);
2600 if (devid < 0) {
Joerg Roedel101fa032018-11-27 16:22:31 +01002601 pr_err("%s: IOAPIC[%d] not in IVRS table\n",
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002602 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002603 ret = false;
2604 } else if (devid == IOAPIC_SB_DEVID) {
2605 has_sb_ioapic = true;
2606 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002607 }
2608 }
2609
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002610 if (!has_sb_ioapic) {
2611 /*
2612 * We expect the SB IOAPIC to be listed in the IVRS
2613 * table. The system timer is connected to the SB IOAPIC
2614 * and if we don't have it in the list the system will
2615 * panic at boot time. This situation usually happens
2616 * when the BIOS is buggy and provides us the wrong
2617 * device id for the IOAPIC in the system.
2618 */
Joerg Roedel101fa032018-11-27 16:22:31 +01002619 pr_err("%s: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002620 }
2621
2622 if (!ret)
Joerg Roedel101fa032018-11-27 16:22:31 +01002623 pr_err("Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002624
2625 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002626}
2627
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002628static void __init free_dma_resources(void)
2629{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002630 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2631 get_order(MAX_DOMAIN_ID/8));
Joerg Roedelf6019272017-06-16 16:09:58 +02002632 amd_iommu_pd_alloc_bitmap = NULL;
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002633
2634 free_unity_maps();
2635}
2636
Suravee Suthikulpanita44092e2021-01-20 07:50:02 -06002637static void __init ivinfo_init(void *ivrs)
2638{
2639 amd_iommu_ivinfo = *((u32 *)(ivrs + IOMMU_IVINFO_OFFSET));
2640}
2641
Joerg Roedelb65233a2008-07-11 17:14:21 +02002642/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002643 * This is the hardware init function for AMD IOMMU in the system.
2644 * This function is called either from amd_iommu_init or from the interrupt
2645 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002646 *
2647 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002648 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002649 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002650 * 1 pass) Discover the most comprehensive IVHD type to use.
2651 *
2652 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002653 * Upon this information the size of the data structures is
2654 * determined that needs to be allocated.
2655 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002656 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002657 * information in the ACPI table about available AMD IOMMUs
2658 * in the system. It also maps the PCI devices in the
2659 * system to specific IOMMUs
2660 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002661 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002662 * initialized we update them with information about memory
2663 * remapping requirements parsed out of the ACPI table in
2664 * this last pass.
2665 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002666 * After everything is set up the IOMMUs are enabled and the necessary
2667 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002668 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002669static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002670{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002671 struct acpi_table_header *ivrs_base;
Adrian Huangf8993dc2020-12-10 10:13:30 +08002672 int i, remap_cache_sz, ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002673 acpi_status status;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002674
Joerg Roedel643511b2012-06-12 12:09:35 +02002675 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002676 return -ENODEV;
2677
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002678 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002679 if (status == AE_NOT_FOUND)
2680 return -ENODEV;
2681 else if (ACPI_FAILURE(status)) {
2682 const char *err = acpi_format_exception(status);
Joerg Roedel101fa032018-11-27 16:22:31 +01002683 pr_err("IVRS table error: %s\n", err);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002684 return -EINVAL;
2685 }
2686
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002687 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002688 * Validate checksum here so we don't need to do it when
2689 * we actually parse the table
2690 */
2691 ret = check_ivrs_checksum(ivrs_base);
2692 if (ret)
Rafael J. Wysocki99e8ccd2017-01-10 14:57:28 +01002693 goto out;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002694
Suravee Suthikulpanita44092e2021-01-20 07:50:02 -06002695 ivinfo_init(ivrs_base);
2696
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002697 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2698 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2699
2700 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002701 * First parse ACPI tables to find the largest Bus/Dev/Func
2702 * we need to handle. Upon this information the shared data
2703 * structures for the IOMMUs in the system will be allocated
2704 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002705 ret = find_last_devid_acpi(ivrs_base);
2706 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002707 goto out;
2708
Joerg Roedelc5714842008-07-11 17:14:25 +02002709 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2710 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2711 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002712
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002713 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002714 ret = -ENOMEM;
Baoquan Heb3367812017-08-09 16:33:42 +08002715 amd_iommu_dev_table = (void *)__get_free_pages(
2716 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002717 get_order(dev_table_size));
2718 if (amd_iommu_dev_table == NULL)
2719 goto out;
2720
2721 /*
2722 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2723 * IOMMU see for that device
2724 */
2725 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2726 get_order(alias_table_size));
2727 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002728 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002729
2730 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002731 amd_iommu_rlookup_table = (void *)__get_free_pages(
2732 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002733 get_order(rlookup_table_size));
2734 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002735 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002736
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002737 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2738 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002739 get_order(MAX_DOMAIN_ID/8));
2740 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002741 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002742
2743 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002744 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002745 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002746 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002747 amd_iommu_alias_table[i] = i;
2748
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002749 /*
2750 * never allocate domain 0 because its used as the non-allocated and
2751 * error value placeholder
2752 */
Baoquan He5c87f622016-09-15 16:50:51 +08002753 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002754
2755 /*
2756 * now the data structures are allocated and basically initialized
2757 * start the real acpi table scan
2758 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002759 ret = init_iommu_all(ivrs_base);
2760 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002761 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002762
Joerg Roedel11123742017-06-16 16:09:54 +02002763 /* Disable any previously enabled IOMMUs */
Baoquan He20b46df2017-08-09 16:33:44 +08002764 if (!is_kdump_kernel() || amd_iommu_disabled)
2765 disable_iommus();
Joerg Roedel11123742017-06-16 16:09:54 +02002766
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002767 if (amd_iommu_irq_remap)
2768 amd_iommu_irq_remap = check_ioapic_information();
2769
Joerg Roedel05152a02012-06-15 16:53:51 +02002770 if (amd_iommu_irq_remap) {
2771 /*
2772 * Interrupt remapping enabled, create kmem_cache for the
2773 * remapping tables.
2774 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002775 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002776 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2777 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2778 else
2779 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002780 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002781 remap_cache_sz,
Suravee Suthikulpanit5ae9a042020-12-10 10:24:36 -06002782 DTE_INTTAB_ALIGNMENT,
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002783 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002784 if (!amd_iommu_irq_cache)
2785 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002786
2787 irq_lookup_table = (void *)__get_free_pages(
2788 GFP_KERNEL | __GFP_ZERO,
2789 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002790 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2791 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002792 if (!irq_lookup_table)
2793 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002794 }
2795
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002796 ret = init_memory_definitions(ivrs_base);
2797 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002798 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002799
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002800 /* init the device table */
2801 init_device_table();
2802
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002803out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002804 /* Don't leak any ACPI memory */
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002805 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002806
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002807 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002808}
2809
Gerard Snitselaarae295142012-03-16 11:38:22 -07002810static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002811{
2812 struct amd_iommu *iommu;
2813 int ret = 0;
2814
2815 for_each_iommu(iommu) {
David Woodhoused1adcfb2020-11-11 12:09:01 +00002816 ret = iommu_init_irq(iommu);
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002817 if (ret)
2818 goto out;
2819 }
2820
2821out:
2822 return ret;
2823}
2824
Joerg Roedelb65412c2021-06-08 14:28:43 +02002825static bool __init detect_ivrs(void)
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002826{
2827 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002828 acpi_status status;
Joerg Roedel072a03e2021-03-17 10:10:35 +01002829 int i;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002830
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002831 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002832 if (status == AE_NOT_FOUND)
2833 return false;
2834 else if (ACPI_FAILURE(status)) {
2835 const char *err = acpi_format_exception(status);
Joerg Roedel101fa032018-11-27 16:22:31 +01002836 pr_err("IVRS table error: %s\n", err);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002837 return false;
2838 }
2839
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002840 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002841
Joerg Roedelb1e650d2021-06-03 15:02:03 +02002842 if (amd_iommu_force_enable)
2843 goto out;
2844
Joerg Roedel072a03e2021-03-17 10:10:35 +01002845 /* Don't use IOMMU if there is Stoney Ridge graphics */
2846 for (i = 0; i < 32; i++) {
2847 u32 pci_id;
2848
2849 pci_id = read_pci_config(0, i, 0, 0);
2850 if ((pci_id & 0xffff) == 0x1002 && (pci_id >> 16) == 0x98e4) {
2851 pr_info("Disable IOMMU on Stoney Ridge\n");
2852 return false;
2853 }
2854 }
2855
Joerg Roedelb1e650d2021-06-03 15:02:03 +02002856out:
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002857 /* Make sure ACS will be enabled during PCI probe */
2858 pci_request_acs();
2859
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002860 return true;
2861}
2862
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002863/****************************************************************************
2864 *
2865 * AMD IOMMU Initialization State Machine
2866 *
2867 ****************************************************************************/
2868
2869static int __init state_next(void)
2870{
2871 int ret = 0;
2872
2873 switch (init_state) {
2874 case IOMMU_START_STATE:
2875 if (!detect_ivrs()) {
2876 init_state = IOMMU_NOT_FOUND;
2877 ret = -ENODEV;
2878 } else {
2879 init_state = IOMMU_IVRS_DETECTED;
2880 }
2881 break;
2882 case IOMMU_IVRS_DETECTED:
Joerg Roedel9f81ca82021-03-17 10:10:36 +01002883 if (amd_iommu_disabled) {
Joerg Roedel7ad820e2017-06-16 16:09:59 +02002884 init_state = IOMMU_CMDLINE_DISABLED;
2885 ret = -EINVAL;
Joerg Roedel9f81ca82021-03-17 10:10:36 +01002886 } else {
2887 ret = early_amd_iommu_init();
2888 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
Joerg Roedel7ad820e2017-06-16 16:09:59 +02002889 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002890 break;
2891 case IOMMU_ACPI_FINISHED:
2892 early_enable_iommus();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002893 x86_platform.iommu_shutdown = disable_iommus;
2894 init_state = IOMMU_ENABLED;
2895 break;
2896 case IOMMU_ENABLED:
Joerg Roedel74ddda72017-07-26 14:17:55 +02002897 register_syscore_ops(&amd_iommu_syscore_ops);
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002898 ret = amd_iommu_init_pci();
2899 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2900 enable_iommus_v2();
2901 break;
2902 case IOMMU_PCI_INIT:
2903 ret = amd_iommu_enable_interrupts();
2904 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2905 break;
2906 case IOMMU_INTERRUPTS_EN:
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002907 init_state = IOMMU_INITIALIZED;
2908 break;
2909 case IOMMU_INITIALIZED:
2910 /* Nothing to do */
2911 break;
2912 case IOMMU_NOT_FOUND:
2913 case IOMMU_INIT_ERROR:
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002914 case IOMMU_CMDLINE_DISABLED:
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002915 /* Error states => do nothing */
2916 ret = -EINVAL;
2917 break;
2918 default:
2919 /* Unknown state */
2920 BUG();
2921 }
2922
Kevin Mitchell5c905012019-06-12 14:52:05 -07002923 if (ret) {
2924 free_dma_resources();
2925 if (!irq_remapping_enabled) {
2926 disable_iommus();
2927 free_iommu_resources();
2928 } else {
2929 struct amd_iommu *iommu;
2930
2931 uninit_device_table_dma();
2932 for_each_iommu(iommu)
2933 iommu_flush_all_caches(iommu);
2934 }
2935 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002936 return ret;
2937}
2938
2939static int __init iommu_go_to_state(enum iommu_init_state state)
2940{
Joerg Roedel151b0902017-06-16 16:09:57 +02002941 int ret = -EINVAL;
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002942
2943 while (init_state != state) {
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002944 if (init_state == IOMMU_NOT_FOUND ||
2945 init_state == IOMMU_INIT_ERROR ||
2946 init_state == IOMMU_CMDLINE_DISABLED)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002947 break;
Joerg Roedel151b0902017-06-16 16:09:57 +02002948 ret = state_next();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002949 }
2950
2951 return ret;
2952}
2953
Joerg Roedel6b474b82012-06-26 16:46:04 +02002954#ifdef CONFIG_IRQ_REMAP
2955int __init amd_iommu_prepare(void)
2956{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002957 int ret;
2958
Jiang Liu7fa1c842015-01-07 15:31:42 +08002959 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002960
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002961 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
Joerg Roedel4b8ef152021-03-17 10:10:37 +01002962 if (ret) {
2963 amd_iommu_irq_remap = false;
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002964 return ret;
Joerg Roedel4b8ef152021-03-17 10:10:37 +01002965 }
2966
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002967 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002968}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002969
Joerg Roedel6b474b82012-06-26 16:46:04 +02002970int __init amd_iommu_enable(void)
2971{
2972 int ret;
2973
2974 ret = iommu_go_to_state(IOMMU_ENABLED);
2975 if (ret)
2976 return ret;
2977
2978 irq_remapping_enabled = 1;
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05002979 return amd_iommu_xt_mode;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002980}
2981
2982void amd_iommu_disable(void)
2983{
2984 amd_iommu_suspend();
2985}
2986
2987int amd_iommu_reenable(int mode)
2988{
2989 amd_iommu_resume();
2990
2991 return 0;
2992}
2993
2994int __init amd_iommu_enable_faulting(void)
2995{
2996 /* We enable MSI later when PCI is initialized */
2997 return 0;
2998}
2999#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02003000
Joerg Roedel8704a1b2012-03-01 15:57:53 +01003001/*
3002 * This is the core init function for AMD IOMMU hardware in the system.
3003 * This function is called from the generic x86 DMA layer initialization
3004 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01003005 */
3006static int __init amd_iommu_init(void)
3007{
Gary R Hook7d0f5fd2018-06-12 16:41:30 -05003008 struct amd_iommu *iommu;
Joerg Roedel2c0ae172012-06-12 15:59:30 +02003009 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01003010
Joerg Roedel2c0ae172012-06-12 15:59:30 +02003011 ret = iommu_go_to_state(IOMMU_INITIALIZED);
Kevin Mitchellbf4bff42019-06-12 14:52:04 -07003012#ifdef CONFIG_GART_IOMMU
3013 if (ret && list_empty(&amd_iommu_list)) {
3014 /*
3015 * We failed to initialize the AMD IOMMU - try fallback
3016 * to GART if possible.
3017 */
3018 gart_iommu_init();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02003019 }
Kevin Mitchellbf4bff42019-06-12 14:52:04 -07003020#endif
Joerg Roedel8704a1b2012-03-01 15:57:53 +01003021
Gary R Hook7d0f5fd2018-06-12 16:41:30 -05003022 for_each_iommu(iommu)
3023 amd_iommu_debugfs_setup(iommu);
3024
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02003025 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02003026}
3027
Tom Lendacky2543a782017-07-17 16:10:24 -05003028static bool amd_iommu_sme_check(void)
3029{
3030 if (!sme_active() || (boot_cpu_data.x86 != 0x17))
3031 return true;
3032
3033 /* For Fam17h, a specific level of support is required */
3034 if (boot_cpu_data.microcode >= 0x08001205)
3035 return true;
3036
3037 if ((boot_cpu_data.microcode >= 0x08001126) &&
3038 (boot_cpu_data.microcode <= 0x080011ff))
3039 return true;
3040
Joerg Roedel101fa032018-11-27 16:22:31 +01003041 pr_notice("IOMMU not currently supported when SME is active\n");
Tom Lendacky2543a782017-07-17 16:10:24 -05003042
3043 return false;
3044}
3045
Joerg Roedelb65233a2008-07-11 17:14:21 +02003046/****************************************************************************
3047 *
3048 * Early detect code. This code runs at IOMMU detection time in the DMA
3049 * layer. It just looks if there is an IVRS ACPI table to detect AMD
3050 * IOMMUs
3051 *
3052 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04003053int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02003054{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02003055 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02003056
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09003057 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04003058 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02003059
Tom Lendacky2543a782017-07-17 16:10:24 -05003060 if (!amd_iommu_sme_check())
3061 return -ENODEV;
3062
Joerg Roedel2c0ae172012-06-12 15:59:30 +02003063 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
3064 if (ret)
3065 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08003066
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02003067 amd_iommu_detected = true;
3068 iommu_detected = 1;
3069 x86_init.iommu.iommu_init = amd_iommu_init;
3070
Jérôme Glisse4781bc42015-08-31 18:13:03 -04003071 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02003072}
3073
Joerg Roedelb65233a2008-07-11 17:14:21 +02003074/****************************************************************************
3075 *
3076 * Parsing functions for the AMD IOMMU specific kernel command line
3077 * options.
3078 *
3079 ****************************************************************************/
3080
Joerg Roedelfefda112009-05-20 12:21:42 +02003081static int __init parse_amd_iommu_dump(char *str)
3082{
3083 amd_iommu_dump = true;
3084
3085 return 1;
3086}
3087
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05003088static int __init parse_amd_iommu_intr(char *str)
3089{
3090 for (; *str; ++str) {
3091 if (strncmp(str, "legacy", 6) == 0) {
Suravee Suthikulpanitb74aa022020-04-22 08:30:02 -05003092 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05003093 break;
3094 }
3095 if (strncmp(str, "vapic", 5) == 0) {
3096 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
3097 break;
3098 }
3099 }
3100 return 1;
3101}
3102
Joerg Roedel918ad6c2008-06-26 21:27:52 +02003103static int __init parse_amd_iommu_options(char *str)
3104{
3105 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01003106 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09003107 amd_iommu_unmap_flush = true;
Joerg Roedelb1e650d2021-06-03 15:02:03 +02003108 if (strncmp(str, "force_enable", 12) == 0)
3109 amd_iommu_force_enable = true;
Joerg Roedela5235722010-05-11 17:12:33 +02003110 if (strncmp(str, "off", 3) == 0)
3111 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01003112 if (strncmp(str, "force_isolation", 15) == 0)
3113 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02003114 }
3115
3116 return 1;
3117}
3118
Joerg Roedel440e89982013-04-09 16:35:28 +02003119static int __init parse_ivrs_ioapic(char *str)
3120{
3121 unsigned int bus, dev, fn;
3122 int ret, id, i;
3123 u16 devid;
3124
3125 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
3126
3127 if (ret != 4) {
Joerg Roedel101fa032018-11-27 16:22:31 +01003128 pr_err("Invalid command line: ivrs_ioapic%s\n", str);
Joerg Roedel440e89982013-04-09 16:35:28 +02003129 return 1;
3130 }
3131
3132 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
Joerg Roedel101fa032018-11-27 16:22:31 +01003133 pr_err("Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
Joerg Roedel440e89982013-04-09 16:35:28 +02003134 str);
3135 return 1;
3136 }
3137
3138 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3139
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02003140 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02003141 i = early_ioapic_map_size++;
3142 early_ioapic_map[i].id = id;
3143 early_ioapic_map[i].devid = devid;
3144 early_ioapic_map[i].cmd_line = true;
3145
3146 return 1;
3147}
3148
3149static int __init parse_ivrs_hpet(char *str)
3150{
3151 unsigned int bus, dev, fn;
3152 int ret, id, i;
3153 u16 devid;
3154
3155 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
3156
3157 if (ret != 4) {
Joerg Roedel101fa032018-11-27 16:22:31 +01003158 pr_err("Invalid command line: ivrs_hpet%s\n", str);
Joerg Roedel440e89982013-04-09 16:35:28 +02003159 return 1;
3160 }
3161
3162 if (early_hpet_map_size == EARLY_MAP_SIZE) {
Joerg Roedel101fa032018-11-27 16:22:31 +01003163 pr_err("Early HPET map overflow - ignoring ivrs_hpet%s\n",
Joerg Roedel440e89982013-04-09 16:35:28 +02003164 str);
3165 return 1;
3166 }
3167
3168 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3169
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02003170 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02003171 i = early_hpet_map_size++;
3172 early_hpet_map[i].id = id;
3173 early_hpet_map[i].devid = devid;
3174 early_hpet_map[i].cmd_line = true;
3175
3176 return 1;
3177}
3178
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04003179static int __init parse_ivrs_acpihid(char *str)
3180{
3181 u32 bus, dev, fn;
3182 char *hid, *uid, *p;
3183 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
3184 int ret, i;
3185
3186 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
3187 if (ret != 4) {
Joerg Roedel101fa032018-11-27 16:22:31 +01003188 pr_err("Invalid command line: ivrs_acpihid(%s)\n", str);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04003189 return 1;
3190 }
3191
3192 p = acpiid;
3193 hid = strsep(&p, ":");
3194 uid = p;
3195
3196 if (!hid || !(*hid) || !uid) {
Joerg Roedel101fa032018-11-27 16:22:31 +01003197 pr_err("Invalid command line: hid or uid\n");
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04003198 return 1;
3199 }
3200
3201 i = early_acpihid_map_size++;
3202 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
3203 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
3204 early_acpihid_map[i].devid =
3205 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
3206 early_acpihid_map[i].cmd_line = true;
3207
3208 return 1;
3209}
3210
Joerg Roedel440e89982013-04-09 16:35:28 +02003211__setup("amd_iommu_dump", parse_amd_iommu_dump);
3212__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05003213__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02003214__setup("ivrs_ioapic", parse_ivrs_ioapic);
3215__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04003216__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04003217
3218IOMMU_INIT_FINISH(amd_iommu_detect,
3219 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02003220 NULL,
3221 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01003222
3223bool amd_iommu_v2_supported(void)
3224{
3225 return amd_iommu_v2_present;
3226}
3227EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003228
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003229struct amd_iommu *get_amd_iommu(unsigned int idx)
3230{
3231 unsigned int i = 0;
3232 struct amd_iommu *iommu;
3233
3234 for_each_iommu(iommu)
3235 if (i++ == idx)
3236 return iommu;
3237 return NULL;
3238}
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003239
Steven L Kinney30861dd2013-06-05 16:11:48 -05003240/****************************************************************************
3241 *
3242 * IOMMU EFR Performance Counter support functionality. This code allows
3243 * access to the IOMMU PC functionality.
3244 *
3245 ****************************************************************************/
3246
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003247u8 amd_iommu_pc_get_max_banks(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05003248{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003249 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003250
Steven L Kinney30861dd2013-06-05 16:11:48 -05003251 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003252 return iommu->max_banks;
Steven L Kinney30861dd2013-06-05 16:11:48 -05003253
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003254 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05003255}
3256EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
3257
3258bool amd_iommu_pc_supported(void)
3259{
3260 return amd_iommu_pc_present;
3261}
3262EXPORT_SYMBOL(amd_iommu_pc_supported);
3263
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003264u8 amd_iommu_pc_get_max_counters(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05003265{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003266 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003267
Steven L Kinney30861dd2013-06-05 16:11:48 -05003268 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003269 return iommu->max_counters;
Steven L Kinney30861dd2013-06-05 16:11:48 -05003270
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003271 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05003272}
3273EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3274
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003275static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3276 u8 fxn, u64 *value, bool is_write)
Steven L Kinney30861dd2013-06-05 16:11:48 -05003277{
Steven L Kinney30861dd2013-06-05 16:11:48 -05003278 u32 offset;
3279 u32 max_offset_lim;
3280
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003281 /* Make sure the IOMMU PC resource is available */
3282 if (!amd_iommu_pc_present)
3283 return -ENODEV;
3284
Steven L Kinney30861dd2013-06-05 16:11:48 -05003285 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003286 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05003287 return -ENODEV;
3288
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003289 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003290
3291 /* Limit the offset to the hw defined mmio region aperture */
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003292 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
Steven L Kinney30861dd2013-06-05 16:11:48 -05003293 (iommu->max_counters << 8) | 0x28);
3294 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3295 (offset > max_offset_lim))
3296 return -EINVAL;
3297
3298 if (is_write) {
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003299 u64 val = *value & GENMASK_ULL(47, 0);
3300
3301 writel((u32)val, iommu->mmio_base + offset);
3302 writel((val >> 32), iommu->mmio_base + offset + 4);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003303 } else {
3304 *value = readl(iommu->mmio_base + offset + 4);
3305 *value <<= 32;
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003306 *value |= readl(iommu->mmio_base + offset);
3307 *value &= GENMASK_ULL(47, 0);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003308 }
3309
3310 return 0;
3311}
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003312
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003313int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003314{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003315 if (!iommu)
3316 return -EINVAL;
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003317
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003318 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003319}
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003320
3321int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3322{
3323 if (!iommu)
3324 return -EINVAL;
3325
3326 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3327}