blob: a6b81a05a0d1e9cfc75a6a719443debf8b21141a [file] [log] [blame]
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
21#include <linux/acpi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020022#include <linux/list.h>
Baoquan He5c87f622016-09-15 16:50:51 +080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010025#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020026#include <linux/interrupt.h>
27#include <linux/msi.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020028#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010029#include <linux/export.h>
Alex Williamson066f2e92014-06-12 16:12:37 -060030#include <linux/iommu.h>
Lucas Stachebcfa282016-10-26 13:09:53 +020031#include <linux/kmemleak.h>
Joerg Roedel54bd6352017-06-15 10:36:22 +020032#include <linux/crash_dump.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020033#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090034#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010035#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090036#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040037#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020038#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020039#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020040
41#include "amd_iommu_proto.h"
42#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020043#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020044
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020045/*
46 * definitions for the ACPI scanning code
47 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020048#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020049
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040050#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020051#define ACPI_IVMD_TYPE_ALL 0x20
52#define ACPI_IVMD_TYPE 0x21
53#define ACPI_IVMD_TYPE_RANGE 0x22
54
55#define IVHD_DEV_ALL 0x01
56#define IVHD_DEV_SELECT 0x02
57#define IVHD_DEV_SELECT_RANGE_START 0x03
58#define IVHD_DEV_RANGE_END 0x04
59#define IVHD_DEV_ALIAS 0x42
60#define IVHD_DEV_ALIAS_RANGE 0x43
61#define IVHD_DEV_EXT_SELECT 0x46
62#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020063#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040064#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020065
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040066#define UID_NOT_PRESENT 0
67#define UID_IS_INTEGER 1
68#define UID_IS_CHARACTER 2
69
Joerg Roedel6efed632012-06-14 15:52:58 +020070#define IVHD_SPECIAL_IOAPIC 1
71#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020072
Joerg Roedel6da73422009-05-04 11:44:38 +020073#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
74#define IVHD_FLAG_PASSPW_EN_MASK 0x02
75#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
76#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020077
78#define IVMD_FLAG_EXCL_RANGE 0x08
79#define IVMD_FLAG_UNITY_MAP 0x01
80
81#define ACPI_DEVFLAG_INITPASS 0x01
82#define ACPI_DEVFLAG_EXTINT 0x02
83#define ACPI_DEVFLAG_NMI 0x04
84#define ACPI_DEVFLAG_SYSMGT1 0x10
85#define ACPI_DEVFLAG_SYSMGT2 0x20
86#define ACPI_DEVFLAG_LINT0 0x40
87#define ACPI_DEVFLAG_LINT1 0x80
88#define ACPI_DEVFLAG_ATSDIS 0x10000000
89
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050090#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020091/*
92 * ACPI table definitions
93 *
94 * These data structures are laid over the table to parse the important values
95 * out of it.
96 */
97
Joerg Roedelb0119e82017-02-01 13:23:08 +010098extern const struct iommu_ops amd_iommu_ops;
99
Joerg Roedelb65233a2008-07-11 17:14:21 +0200100/*
101 * structure describing one IOMMU in the ACPI table. Typically followed by one
102 * or more ivhd_entrys.
103 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200104struct ivhd_header {
105 u8 type;
106 u8 flags;
107 u16 length;
108 u16 devid;
109 u16 cap_ptr;
110 u64 mmio_phys;
111 u16 pci_seg;
112 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400113 u32 efr_attr;
114
115 /* Following only valid on IVHD type 11h and 40h */
116 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
117 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200118} __attribute__((packed));
119
Joerg Roedelb65233a2008-07-11 17:14:21 +0200120/*
121 * A device entry describing which devices a specific IOMMU translates and
122 * which requestor ids they use.
123 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200124struct ivhd_entry {
125 u8 type;
126 u16 devid;
127 u8 flags;
128 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400129 u32 hidh;
130 u64 cid;
131 u8 uidf;
132 u8 uidl;
133 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200134} __attribute__((packed));
135
Joerg Roedelb65233a2008-07-11 17:14:21 +0200136/*
137 * An AMD IOMMU memory definition structure. It defines things like exclusion
138 * ranges for devices and regions that should be unity mapped.
139 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200140struct ivmd_header {
141 u8 type;
142 u8 flags;
143 u16 length;
144 u16 devid;
145 u16 aux;
146 u64 resv;
147 u64 range_start;
148 u64 range_length;
149} __attribute__((packed));
150
Joerg Roedelfefda112009-05-20 12:21:42 +0200151bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200152bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200153
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500154int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500155
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200156static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200157static bool __initdata amd_iommu_disabled;
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/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100187 * List of protection domains - used during resume
188 */
189LIST_HEAD(amd_iommu_pd_list);
190spinlock_t amd_iommu_pd_lock;
191
192/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200193 * Pointer to the device table which is shared by all AMD IOMMUs
194 * it is indexed by the PCI device id or the HT unit id and contains
195 * information about the domain the device belongs to as well as the
196 * page table root pointer.
197 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200198struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200199
200/*
201 * The alias table is a driver specific data structure which contains the
202 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
203 * More than one device can share the same requestor id.
204 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200205u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200206
207/*
208 * The rlookup table is used to find the IOMMU which is responsible
209 * for a specific device. It is also indexed by the PCI device id.
210 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200211struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200212
213/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200214 * This table is used to find the irq remapping table for a given device id
215 * quickly.
216 */
217struct irq_remap_table **irq_lookup_table;
218
219/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200220 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200221 * to know which ones are already in use.
222 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200223unsigned long *amd_iommu_pd_alloc_bitmap;
224
Joerg Roedelb65233a2008-07-11 17:14:21 +0200225static u32 dev_table_size; /* size of the device table */
226static u32 alias_table_size; /* size of the alias table */
227static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200228
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200229enum iommu_init_state {
230 IOMMU_START_STATE,
231 IOMMU_IVRS_DETECTED,
232 IOMMU_ACPI_FINISHED,
233 IOMMU_ENABLED,
234 IOMMU_PCI_INIT,
235 IOMMU_INTERRUPTS_EN,
236 IOMMU_DMA_OPS,
237 IOMMU_INITIALIZED,
238 IOMMU_NOT_FOUND,
239 IOMMU_INIT_ERROR,
Joerg Roedel1b1e9422017-06-16 16:09:56 +0200240 IOMMU_CMDLINE_DISABLED,
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200241};
242
Joerg Roedel235dacb2013-04-09 17:53:14 +0200243/* Early ioapic and hpet maps from kernel command line */
244#define EARLY_MAP_SIZE 4
245static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
246static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400247static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
248
Joerg Roedel235dacb2013-04-09 17:53:14 +0200249static int __initdata early_ioapic_map_size;
250static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400251static int __initdata early_acpihid_map_size;
252
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200253static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200254
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200255static enum iommu_init_state init_state = IOMMU_START_STATE;
256
Gerard Snitselaarae295142012-03-16 11:38:22 -0700257static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200258static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200259static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100260
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200261static inline void update_last_devid(u16 devid)
262{
263 if (devid > amd_iommu_last_bdf)
264 amd_iommu_last_bdf = devid;
265}
266
Joerg Roedelc5714842008-07-11 17:14:25 +0200267static inline unsigned long tbl_size(int entry_size)
268{
269 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100270 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200271
272 return 1UL << shift;
273}
274
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600275int amd_iommu_get_num_iommus(void)
276{
277 return amd_iommus_present;
278}
279
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400280/* Access to l1 and l2 indexed register spaces */
281
282static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
283{
284 u32 val;
285
286 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
287 pci_read_config_dword(iommu->dev, 0xfc, &val);
288 return val;
289}
290
291static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
292{
293 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
294 pci_write_config_dword(iommu->dev, 0xfc, val);
295 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
296}
297
298static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
299{
300 u32 val;
301
302 pci_write_config_dword(iommu->dev, 0xf0, address);
303 pci_read_config_dword(iommu->dev, 0xf4, &val);
304 return val;
305}
306
307static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
308{
309 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
310 pci_write_config_dword(iommu->dev, 0xf4, val);
311}
312
Joerg Roedelb65233a2008-07-11 17:14:21 +0200313/****************************************************************************
314 *
315 * AMD IOMMU MMIO register space handling functions
316 *
317 * These functions are used to program the IOMMU device registers in
318 * MMIO space required for that driver.
319 *
320 ****************************************************************************/
321
322/*
323 * This function set the exclusion range in the IOMMU. DMA accesses to the
324 * exclusion range are passed through untranslated
325 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200326static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200327{
328 u64 start = iommu->exclusion_start & PAGE_MASK;
329 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
330 u64 entry;
331
332 if (!iommu->exclusion_start)
333 return;
334
335 entry = start | MMIO_EXCL_ENABLE_MASK;
336 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
337 &entry, sizeof(entry));
338
339 entry = limit;
340 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
341 &entry, sizeof(entry));
342}
343
Joerg Roedelb65233a2008-07-11 17:14:21 +0200344/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000345static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200346{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200347 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200348
349 BUG_ON(iommu->mmio_base == NULL);
350
351 entry = virt_to_phys(amd_iommu_dev_table);
352 entry |= (dev_table_size >> 12) - 1;
353 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
354 &entry, sizeof(entry));
355}
356
Joerg Roedelb65233a2008-07-11 17:14:21 +0200357/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200358static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200359{
360 u32 ctrl;
361
362 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
363 ctrl |= (1 << bit);
364 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
365}
366
Joerg Roedelca0207112009-10-28 18:02:26 +0100367static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200368{
369 u32 ctrl;
370
Joerg Roedel199d0d52008-09-17 16:45:59 +0200371 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200372 ctrl &= ~(1 << bit);
373 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
374}
375
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100376static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
377{
378 u32 ctrl;
379
380 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
381 ctrl &= ~CTRL_INV_TO_MASK;
382 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
383 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
384}
385
Joerg Roedelb65233a2008-07-11 17:14:21 +0200386/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200387static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200388{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200389 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200390}
391
Joerg Roedel92ac4322009-05-19 19:06:27 +0200392static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200393{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200394 /* Disable command buffer */
395 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
396
397 /* Disable event logging and event interrupts */
398 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
399 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
400
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500401 /* Disable IOMMU GA_LOG */
402 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
403 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
404
Chris Wrighta8c485b2009-06-15 15:53:45 +0200405 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200406 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200407}
408
Joerg Roedelb65233a2008-07-11 17:14:21 +0200409/*
410 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
411 * the system has one.
412 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500413static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200414{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500415 if (!request_mem_region(address, end, "amd_iommu")) {
416 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
417 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200418 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200419 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200420 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200421
Steven L Kinney30861dd2013-06-05 16:11:48 -0500422 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200423}
424
425static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
426{
427 if (iommu->mmio_base)
428 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500429 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200430}
431
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400432static inline u32 get_ivhd_header_size(struct ivhd_header *h)
433{
434 u32 size = 0;
435
436 switch (h->type) {
437 case 0x10:
438 size = 24;
439 break;
440 case 0x11:
441 case 0x40:
442 size = 40;
443 break;
444 }
445 return size;
446}
447
Joerg Roedelb65233a2008-07-11 17:14:21 +0200448/****************************************************************************
449 *
450 * The functions below belong to the first pass of AMD IOMMU ACPI table
451 * parsing. In this pass we try to find out the highest device id this
452 * code has to handle. Upon this information the size of the shared data
453 * structures is determined later.
454 *
455 ****************************************************************************/
456
457/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200458 * This function calculates the length of a given IVHD entry
459 */
460static inline int ivhd_entry_length(u8 *ivhd)
461{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400462 u32 type = ((struct ivhd_entry *)ivhd)->type;
463
464 if (type < 0x80) {
465 return 0x04 << (*ivhd >> 6);
466 } else if (type == IVHD_DEV_ACPI_HID) {
467 /* For ACPI_HID, offset 21 is uid len */
468 return *((u8 *)ivhd + 21) + 22;
469 }
470 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200471}
472
473/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200474 * After reading the highest device id from the IOMMU PCI capability header
475 * this function looks if there is a higher device id defined in the ACPI table
476 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200477static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
478{
479 u8 *p = (void *)h, *end = (void *)h;
480 struct ivhd_entry *dev;
481
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400482 u32 ivhd_size = get_ivhd_header_size(h);
483
484 if (!ivhd_size) {
485 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
486 return -EINVAL;
487 }
488
489 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200490 end += h->length;
491
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200492 while (p < end) {
493 dev = (struct ivhd_entry *)p;
494 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200495 case IVHD_DEV_ALL:
496 /* Use maximum BDF value for DEV_ALL */
497 update_last_devid(0xffff);
498 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200499 case IVHD_DEV_SELECT:
500 case IVHD_DEV_RANGE_END:
501 case IVHD_DEV_ALIAS:
502 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200503 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200504 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200505 break;
506 default:
507 break;
508 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200509 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200510 }
511
512 WARN_ON(p != end);
513
514 return 0;
515}
516
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400517static int __init check_ivrs_checksum(struct acpi_table_header *table)
518{
519 int i;
520 u8 checksum = 0, *p = (u8 *)table;
521
522 for (i = 0; i < table->length; ++i)
523 checksum += p[i];
524 if (checksum != 0) {
525 /* ACPI table corrupt */
526 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
527 return -ENODEV;
528 }
529
530 return 0;
531}
532
Joerg Roedelb65233a2008-07-11 17:14:21 +0200533/*
534 * Iterate over all IVHD entries in the ACPI table and find the highest device
535 * id which we need to handle. This is the first of three functions which parse
536 * the ACPI table. So we check the checksum here.
537 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200538static int __init find_last_devid_acpi(struct acpi_table_header *table)
539{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400540 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200541 struct ivhd_header *h;
542
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200543 p += IVRS_HEADER_LENGTH;
544
545 end += table->length;
546 while (p < end) {
547 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400548 if (h->type == amd_iommu_target_ivhd_type) {
549 int ret = find_last_devid_from_ivhd(h);
550
551 if (ret)
552 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200553 }
554 p += h->length;
555 }
556 WARN_ON(p != end);
557
558 return 0;
559}
560
Joerg Roedelb65233a2008-07-11 17:14:21 +0200561/****************************************************************************
562 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200563 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200564 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
565 * data structures, initialize the device/alias/rlookup table and also
566 * basically initialize the hardware.
567 *
568 ****************************************************************************/
569
570/*
571 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
572 * write commands to that buffer later and the IOMMU will execute them
573 * asynchronously
574 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200575static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200576{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200577 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
578 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200579
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200580 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200581}
582
583/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200584 * This function resets the command buffer if the IOMMU stopped fetching
585 * commands from it.
586 */
587void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
588{
589 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
590
591 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
592 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Tom Lendackyd334a562017-06-05 14:52:12 -0500593 iommu->cmd_buf_head = 0;
594 iommu->cmd_buf_tail = 0;
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200595
596 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
597}
598
599/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200600 * This function writes the command buffer address to the hardware and
601 * enables it.
602 */
603static void iommu_enable_command_buffer(struct amd_iommu *iommu)
604{
605 u64 entry;
606
607 BUG_ON(iommu->cmd_buf == NULL);
608
609 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200610 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200611
Joerg Roedelb36ca912008-06-26 21:27:45 +0200612 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200613 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200614
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200615 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200616}
617
618static void __init free_command_buffer(struct amd_iommu *iommu)
619{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200620 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200621}
622
Joerg Roedel335503e2008-09-05 14:29:07 +0200623/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200624static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200625{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200626 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
627 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200628
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200629 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200630}
631
632static void iommu_enable_event_buffer(struct amd_iommu *iommu)
633{
634 u64 entry;
635
636 BUG_ON(iommu->evt_buf == NULL);
637
Joerg Roedel335503e2008-09-05 14:29:07 +0200638 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200639
Joerg Roedel335503e2008-09-05 14:29:07 +0200640 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
641 &entry, sizeof(entry));
642
Joerg Roedel090672072009-06-15 16:06:48 +0200643 /* set head and tail to zero manually */
644 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
645 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
646
Joerg Roedel58492e12009-05-04 18:41:16 +0200647 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200648}
649
650static void __init free_event_buffer(struct amd_iommu *iommu)
651{
652 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
653}
654
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100655/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200656static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100657{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200658 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
659 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100660
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200661 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100662}
663
664static void iommu_enable_ppr_log(struct amd_iommu *iommu)
665{
666 u64 entry;
667
668 if (iommu->ppr_log == NULL)
669 return;
670
671 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
672
673 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
674 &entry, sizeof(entry));
675
676 /* set head and tail to zero manually */
677 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
678 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
679
680 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
681 iommu_feature_enable(iommu, CONTROL_PPR_EN);
682}
683
684static void __init free_ppr_log(struct amd_iommu *iommu)
685{
686 if (iommu->ppr_log == NULL)
687 return;
688
689 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
690}
691
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500692static void free_ga_log(struct amd_iommu *iommu)
693{
694#ifdef CONFIG_IRQ_REMAP
695 if (iommu->ga_log)
696 free_pages((unsigned long)iommu->ga_log,
697 get_order(GA_LOG_SIZE));
698 if (iommu->ga_log_tail)
699 free_pages((unsigned long)iommu->ga_log_tail,
700 get_order(8));
701#endif
702}
703
704static int iommu_ga_log_enable(struct amd_iommu *iommu)
705{
706#ifdef CONFIG_IRQ_REMAP
707 u32 status, i;
708
709 if (!iommu->ga_log)
710 return -EINVAL;
711
712 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
713
714 /* Check if already running */
715 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
716 return 0;
717
718 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
719 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
720
721 for (i = 0; i < LOOP_TIMEOUT; ++i) {
722 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
723 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
724 break;
725 }
726
727 if (i >= LOOP_TIMEOUT)
728 return -EINVAL;
729#endif /* CONFIG_IRQ_REMAP */
730 return 0;
731}
732
733#ifdef CONFIG_IRQ_REMAP
734static int iommu_init_ga_log(struct amd_iommu *iommu)
735{
736 u64 entry;
737
738 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
739 return 0;
740
741 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
742 get_order(GA_LOG_SIZE));
743 if (!iommu->ga_log)
744 goto err_out;
745
746 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
747 get_order(8));
748 if (!iommu->ga_log_tail)
749 goto err_out;
750
751 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
752 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
753 &entry, sizeof(entry));
754 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
755 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
756 &entry, sizeof(entry));
757 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
758 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
759
760 return 0;
761err_out:
762 free_ga_log(iommu);
763 return -EINVAL;
764}
765#endif /* CONFIG_IRQ_REMAP */
766
767static int iommu_init_ga(struct amd_iommu *iommu)
768{
769 int ret = 0;
770
771#ifdef CONFIG_IRQ_REMAP
772 /* Note: We have already checked GASup from IVRS table.
773 * Now, we need to make sure that GAMSup is set.
774 */
775 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
776 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
777 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
778
779 ret = iommu_init_ga_log(iommu);
780#endif /* CONFIG_IRQ_REMAP */
781
782 return ret;
783}
784
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100785static void iommu_enable_gt(struct amd_iommu *iommu)
786{
787 if (!iommu_feature(iommu, FEATURE_GT))
788 return;
789
790 iommu_feature_enable(iommu, CONTROL_GT_EN);
791}
792
Joerg Roedelb65233a2008-07-11 17:14:21 +0200793/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200794static void set_dev_entry_bit(u16 devid, u8 bit)
795{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100796 int i = (bit >> 6) & 0x03;
797 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200798
Joerg Roedelee6c2862011-11-09 12:06:03 +0100799 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200800}
801
Joerg Roedelc5cca142009-10-09 18:31:20 +0200802static int get_dev_entry_bit(u16 devid, u8 bit)
803{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100804 int i = (bit >> 6) & 0x03;
805 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200806
Joerg Roedelee6c2862011-11-09 12:06:03 +0100807 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200808}
809
810
811void amd_iommu_apply_erratum_63(u16 devid)
812{
813 int sysmgt;
814
815 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
816 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
817
818 if (sysmgt == 0x01)
819 set_dev_entry_bit(devid, DEV_ENTRY_IW);
820}
821
Joerg Roedel5ff47892008-07-14 20:11:18 +0200822/* Writes the specific IOMMU for a device into the rlookup table */
823static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
824{
825 amd_iommu_rlookup_table[devid] = iommu;
826}
827
Joerg Roedelb65233a2008-07-11 17:14:21 +0200828/*
829 * This function takes the device specific flags read from the ACPI
830 * table and sets up the device table entry with that information
831 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200832static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
833 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200834{
835 if (flags & ACPI_DEVFLAG_INITPASS)
836 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
837 if (flags & ACPI_DEVFLAG_EXTINT)
838 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
839 if (flags & ACPI_DEVFLAG_NMI)
840 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
841 if (flags & ACPI_DEVFLAG_SYSMGT1)
842 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
843 if (flags & ACPI_DEVFLAG_SYSMGT2)
844 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
845 if (flags & ACPI_DEVFLAG_LINT0)
846 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
847 if (flags & ACPI_DEVFLAG_LINT1)
848 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200849
Joerg Roedelc5cca142009-10-09 18:31:20 +0200850 amd_iommu_apply_erratum_63(devid);
851
Joerg Roedel5ff47892008-07-14 20:11:18 +0200852 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200853}
854
Joerg Roedelc50e3242014-09-09 15:59:37 +0200855static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200856{
857 struct devid_map *entry;
858 struct list_head *list;
859
Joerg Roedel31cff672013-04-09 16:53:58 +0200860 if (type == IVHD_SPECIAL_IOAPIC)
861 list = &ioapic_map;
862 else if (type == IVHD_SPECIAL_HPET)
863 list = &hpet_map;
864 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200865 return -EINVAL;
866
Joerg Roedel31cff672013-04-09 16:53:58 +0200867 list_for_each_entry(entry, list, list) {
868 if (!(entry->id == id && entry->cmd_line))
869 continue;
870
871 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
872 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
873
Joerg Roedelc50e3242014-09-09 15:59:37 +0200874 *devid = entry->devid;
875
Joerg Roedel31cff672013-04-09 16:53:58 +0200876 return 0;
877 }
878
Joerg Roedel6efed632012-06-14 15:52:58 +0200879 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
880 if (!entry)
881 return -ENOMEM;
882
Joerg Roedel31cff672013-04-09 16:53:58 +0200883 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +0200884 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +0200885 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +0200886
887 list_add_tail(&entry->list, list);
888
889 return 0;
890}
891
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400892static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
893 bool cmd_line)
894{
895 struct acpihid_map_entry *entry;
896 struct list_head *list = &acpihid_map;
897
898 list_for_each_entry(entry, list, list) {
899 if (strcmp(entry->hid, hid) ||
900 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
901 !entry->cmd_line)
902 continue;
903
904 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
905 hid, uid);
906 *devid = entry->devid;
907 return 0;
908 }
909
910 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
911 if (!entry)
912 return -ENOMEM;
913
914 memcpy(entry->uid, uid, strlen(uid));
915 memcpy(entry->hid, hid, strlen(hid));
916 entry->devid = *devid;
917 entry->cmd_line = cmd_line;
918 entry->root_devid = (entry->devid & (~0x7));
919
920 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
921 entry->cmd_line ? "cmd" : "ivrs",
922 entry->hid, entry->uid, entry->root_devid);
923
924 list_add_tail(&entry->list, list);
925 return 0;
926}
927
Joerg Roedel235dacb2013-04-09 17:53:14 +0200928static int __init add_early_maps(void)
929{
930 int i, ret;
931
932 for (i = 0; i < early_ioapic_map_size; ++i) {
933 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
934 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200935 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200936 early_ioapic_map[i].cmd_line);
937 if (ret)
938 return ret;
939 }
940
941 for (i = 0; i < early_hpet_map_size; ++i) {
942 ret = add_special_device(IVHD_SPECIAL_HPET,
943 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200944 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200945 early_hpet_map[i].cmd_line);
946 if (ret)
947 return ret;
948 }
949
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400950 for (i = 0; i < early_acpihid_map_size; ++i) {
951 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
952 early_acpihid_map[i].uid,
953 &early_acpihid_map[i].devid,
954 early_acpihid_map[i].cmd_line);
955 if (ret)
956 return ret;
957 }
958
Joerg Roedel235dacb2013-04-09 17:53:14 +0200959 return 0;
960}
961
Joerg Roedelb65233a2008-07-11 17:14:21 +0200962/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200963 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +0200964 * it
965 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200966static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
967{
968 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
969
970 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
971 return;
972
973 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200974 /*
975 * We only can configure exclusion ranges per IOMMU, not
976 * per device. But we can enable the exclusion range per
977 * device. This is done here
978 */
Su Friendy2c16c9f2014-05-07 13:54:52 +0800979 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +0200980 iommu->exclusion_start = m->range_start;
981 iommu->exclusion_length = m->range_length;
982 }
983}
984
Joerg Roedelb65233a2008-07-11 17:14:21 +0200985/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200986 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
987 * initializes the hardware and our data structures with it.
988 */
Joerg Roedel6efed632012-06-14 15:52:58 +0200989static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200990 struct ivhd_header *h)
991{
992 u8 *p = (u8 *)h;
993 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +0200994 u16 devid = 0, devid_start = 0, devid_to = 0;
995 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200996 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200997 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400998 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200999 int ret;
1000
1001
1002 ret = add_early_maps();
1003 if (ret)
1004 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001005
1006 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001007 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001008 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001009 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001010
1011 /*
1012 * Done. Now parse the device entries
1013 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001014 ivhd_size = get_ivhd_header_size(h);
1015 if (!ivhd_size) {
1016 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1017 return -EINVAL;
1018 }
1019
1020 p += ivhd_size;
1021
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001022 end += h->length;
1023
Joerg Roedel42a698f2009-05-20 15:41:28 +02001024
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001025 while (p < end) {
1026 e = (struct ivhd_entry *)p;
1027 switch (e->type) {
1028 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001029
Joerg Roedel226e8892015-10-20 17:33:44 +02001030 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001031
Joerg Roedel226e8892015-10-20 17:33:44 +02001032 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1033 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001034 break;
1035 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001036
1037 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1038 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001039 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001040 PCI_SLOT(e->devid),
1041 PCI_FUNC(e->devid),
1042 e->flags);
1043
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001044 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001045 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001046 break;
1047 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001048
1049 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1050 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001051 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001052 PCI_SLOT(e->devid),
1053 PCI_FUNC(e->devid),
1054 e->flags);
1055
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001056 devid_start = e->devid;
1057 flags = e->flags;
1058 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001059 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001060 break;
1061 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001062
1063 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1064 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001065 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001066 PCI_SLOT(e->devid),
1067 PCI_FUNC(e->devid),
1068 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001069 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001070 PCI_SLOT(e->ext >> 8),
1071 PCI_FUNC(e->ext >> 8));
1072
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001073 devid = e->devid;
1074 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001075 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001076 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001077 amd_iommu_alias_table[devid] = devid_to;
1078 break;
1079 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001080
1081 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1082 "devid: %02x:%02x.%x flags: %02x "
1083 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001084 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001085 PCI_SLOT(e->devid),
1086 PCI_FUNC(e->devid),
1087 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001088 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001089 PCI_SLOT(e->ext >> 8),
1090 PCI_FUNC(e->ext >> 8));
1091
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001092 devid_start = e->devid;
1093 flags = e->flags;
1094 devid_to = e->ext >> 8;
1095 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001096 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001097 break;
1098 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001099
1100 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1101 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001102 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001103 PCI_SLOT(e->devid),
1104 PCI_FUNC(e->devid),
1105 e->flags, e->ext);
1106
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001107 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001108 set_dev_entry_from_acpi(iommu, devid, e->flags,
1109 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001110 break;
1111 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001112
1113 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1114 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001115 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001116 PCI_SLOT(e->devid),
1117 PCI_FUNC(e->devid),
1118 e->flags, e->ext);
1119
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001120 devid_start = e->devid;
1121 flags = e->flags;
1122 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001123 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001124 break;
1125 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001126
1127 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001128 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001129 PCI_SLOT(e->devid),
1130 PCI_FUNC(e->devid));
1131
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001132 devid = e->devid;
1133 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001134 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001135 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001136 set_dev_entry_from_acpi(iommu,
1137 devid_to, flags, ext_flags);
1138 }
1139 set_dev_entry_from_acpi(iommu, dev_i,
1140 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001141 }
1142 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001143 case IVHD_DEV_SPECIAL: {
1144 u8 handle, type;
1145 const char *var;
1146 u16 devid;
1147 int ret;
1148
1149 handle = e->ext & 0xff;
1150 devid = (e->ext >> 8) & 0xffff;
1151 type = (e->ext >> 24) & 0xff;
1152
1153 if (type == IVHD_SPECIAL_IOAPIC)
1154 var = "IOAPIC";
1155 else if (type == IVHD_SPECIAL_HPET)
1156 var = "HPET";
1157 else
1158 var = "UNKNOWN";
1159
1160 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1161 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001162 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001163 PCI_SLOT(devid),
1164 PCI_FUNC(devid));
1165
Joerg Roedelc50e3242014-09-09 15:59:37 +02001166 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001167 if (ret)
1168 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001169
1170 /*
1171 * add_special_device might update the devid in case a
1172 * command-line override is present. So call
1173 * set_dev_entry_from_acpi after add_special_device.
1174 */
1175 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1176
Joerg Roedel6efed632012-06-14 15:52:58 +02001177 break;
1178 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001179 case IVHD_DEV_ACPI_HID: {
1180 u16 devid;
1181 u8 hid[ACPIHID_HID_LEN] = {0};
1182 u8 uid[ACPIHID_UID_LEN] = {0};
1183 int ret;
1184
1185 if (h->type != 0x40) {
1186 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1187 e->type);
1188 break;
1189 }
1190
1191 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1192 hid[ACPIHID_HID_LEN - 1] = '\0';
1193
1194 if (!(*hid)) {
1195 pr_err(FW_BUG "Invalid HID.\n");
1196 break;
1197 }
1198
1199 switch (e->uidf) {
1200 case UID_NOT_PRESENT:
1201
1202 if (e->uidl != 0)
1203 pr_warn(FW_BUG "Invalid UID length.\n");
1204
1205 break;
1206 case UID_IS_INTEGER:
1207
1208 sprintf(uid, "%d", e->uid);
1209
1210 break;
1211 case UID_IS_CHARACTER:
1212
1213 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1214 uid[ACPIHID_UID_LEN - 1] = '\0';
1215
1216 break;
1217 default:
1218 break;
1219 }
1220
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001221 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001222 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1223 hid, uid,
1224 PCI_BUS_NUM(devid),
1225 PCI_SLOT(devid),
1226 PCI_FUNC(devid));
1227
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001228 flags = e->flags;
1229
1230 ret = add_acpi_hid_device(hid, uid, &devid, false);
1231 if (ret)
1232 return ret;
1233
1234 /*
1235 * add_special_device might update the devid in case a
1236 * command-line override is present. So call
1237 * set_dev_entry_from_acpi after add_special_device.
1238 */
1239 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1240
1241 break;
1242 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001243 default:
1244 break;
1245 }
1246
Joerg Roedelb514e552008-09-17 17:14:27 +02001247 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001248 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001249
1250 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001251}
1252
Joerg Roedele47d4022008-06-26 21:27:48 +02001253static void __init free_iommu_one(struct amd_iommu *iommu)
1254{
1255 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001256 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001257 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001258 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001259 iommu_unmap_mmio_space(iommu);
1260}
1261
1262static void __init free_iommu_all(void)
1263{
1264 struct amd_iommu *iommu, *next;
1265
Joerg Roedel3bd22172009-05-04 15:06:20 +02001266 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001267 list_del(&iommu->list);
1268 free_iommu_one(iommu);
1269 kfree(iommu);
1270 }
1271}
1272
Joerg Roedelb65233a2008-07-11 17:14:21 +02001273/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001274 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1275 * Workaround:
1276 * BIOS should disable L2B micellaneous clock gating by setting
1277 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1278 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001279static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001280{
1281 u32 value;
1282
1283 if ((boot_cpu_data.x86 != 0x15) ||
1284 (boot_cpu_data.x86_model < 0x10) ||
1285 (boot_cpu_data.x86_model > 0x1f))
1286 return;
1287
1288 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1289 pci_read_config_dword(iommu->dev, 0xf4, &value);
1290
1291 if (value & BIT(2))
1292 return;
1293
1294 /* Select NB indirect register 0x90 and enable writing */
1295 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1296
1297 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1298 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1299 dev_name(&iommu->dev->dev));
1300
1301 /* Clear the enable writing bit */
1302 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1303}
1304
1305/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001306 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1307 * Workaround:
1308 * BIOS should enable ATS write permission check by setting
1309 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1310 */
1311static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1312{
1313 u32 value;
1314
1315 if ((boot_cpu_data.x86 != 0x15) ||
1316 (boot_cpu_data.x86_model < 0x30) ||
1317 (boot_cpu_data.x86_model > 0x3f))
1318 return;
1319
1320 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1321 value = iommu_read_l2(iommu, 0x47);
1322
1323 if (value & BIT(0))
1324 return;
1325
1326 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1327 iommu_write_l2(iommu, 0x47, value | BIT(0));
1328
1329 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1330 dev_name(&iommu->dev->dev));
1331}
1332
1333/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001334 * This function clues the initialization function for one IOMMU
1335 * together and also allocates the command buffer and programs the
1336 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1337 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001338static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1339{
Joerg Roedel6efed632012-06-14 15:52:58 +02001340 int ret;
1341
Joerg Roedele47d4022008-06-26 21:27:48 +02001342 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001343
1344 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001345 list_add_tail(&iommu->list, &amd_iommu_list);
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001346 iommu->index = amd_iommus_present++;
Joerg Roedelbb527772009-11-20 14:31:51 +01001347
1348 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1349 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1350 return -ENOSYS;
1351 }
1352
1353 /* Index is fine - add IOMMU to the array */
1354 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001355
1356 /*
1357 * Copy data from ACPI table entry to the iommu struct
1358 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001359 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001360 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001361 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001362 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001363
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001364 switch (h->type) {
1365 case 0x10:
1366 /* Check if IVHD EFR contains proper max banks/counters */
1367 if ((h->efr_attr != 0) &&
1368 ((h->efr_attr & (0xF << 13)) != 0) &&
1369 ((h->efr_attr & (0x3F << 17)) != 0))
1370 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1371 else
1372 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001373 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1374 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001375 break;
1376 case 0x11:
1377 case 0x40:
1378 if (h->efr_reg & (1 << 9))
1379 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1380 else
1381 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001382 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1383 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001384 break;
1385 default:
1386 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001387 }
1388
1389 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1390 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001391 if (!iommu->mmio_base)
1392 return -ENOMEM;
1393
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001394 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001395 return -ENOMEM;
1396
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001397 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001398 return -ENOMEM;
1399
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001400 iommu->int_enabled = false;
1401
Joerg Roedel6efed632012-06-14 15:52:58 +02001402 ret = init_iommu_from_acpi(iommu, h);
1403 if (ret)
1404 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001405
Jiang Liu7c71d302015-04-13 14:11:33 +08001406 ret = amd_iommu_create_irq_domain(iommu);
1407 if (ret)
1408 return ret;
1409
Joerg Roedelf6fec002012-06-21 16:51:25 +02001410 /*
1411 * Make sure IOMMU is not considered to translate itself. The IVRS
1412 * table tells us so, but this is a lie!
1413 */
1414 amd_iommu_rlookup_table[iommu->devid] = NULL;
1415
Joerg Roedel23c742d2012-06-12 11:47:34 +02001416 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001417}
1418
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001419/**
1420 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1421 * @ivrs Pointer to the IVRS header
1422 *
1423 * This function search through all IVDB of the maximum supported IVHD
1424 */
1425static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1426{
1427 u8 *base = (u8 *)ivrs;
1428 struct ivhd_header *ivhd = (struct ivhd_header *)
1429 (base + IVRS_HEADER_LENGTH);
1430 u8 last_type = ivhd->type;
1431 u16 devid = ivhd->devid;
1432
1433 while (((u8 *)ivhd - base < ivrs->length) &&
1434 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1435 u8 *p = (u8 *) ivhd;
1436
1437 if (ivhd->devid == devid)
1438 last_type = ivhd->type;
1439 ivhd = (struct ivhd_header *)(p + ivhd->length);
1440 }
1441
1442 return last_type;
1443}
1444
Joerg Roedelb65233a2008-07-11 17:14:21 +02001445/*
1446 * Iterates over all IOMMU entries in the ACPI table, allocates the
1447 * IOMMU structure and initializes it with init_iommu_one()
1448 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001449static int __init init_iommu_all(struct acpi_table_header *table)
1450{
1451 u8 *p = (u8 *)table, *end = (u8 *)table;
1452 struct ivhd_header *h;
1453 struct amd_iommu *iommu;
1454 int ret;
1455
Joerg Roedele47d4022008-06-26 21:27:48 +02001456 end += table->length;
1457 p += IVRS_HEADER_LENGTH;
1458
1459 while (p < end) {
1460 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001461 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001462
Joerg Roedelae908c22009-09-01 16:52:16 +02001463 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001464 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001465 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001466 PCI_FUNC(h->devid), h->cap_ptr,
1467 h->pci_seg, h->flags, h->info);
1468 DUMP_printk(" mmio-addr: %016llx\n",
1469 h->mmio_phys);
1470
Joerg Roedele47d4022008-06-26 21:27:48 +02001471 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001472 if (iommu == NULL)
1473 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001474
Joerg Roedele47d4022008-06-26 21:27:48 +02001475 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001476 if (ret)
1477 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001478 }
1479 p += h->length;
1480
1481 }
1482 WARN_ON(p != end);
1483
1484 return 0;
1485}
1486
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001487static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1488 u8 fxn, u64 *value, bool is_write);
Steven L Kinney30861dd2013-06-05 16:11:48 -05001489
1490static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1491{
1492 u64 val = 0xabcd, val2 = 0;
1493
1494 if (!iommu_feature(iommu, FEATURE_PC))
1495 return;
1496
1497 amd_iommu_pc_present = true;
1498
1499 /* Check if the performance counters can be written to */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001500 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1501 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001502 (val != val2)) {
1503 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1504 amd_iommu_pc_present = false;
1505 return;
1506 }
1507
1508 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1509
1510 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1511 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1512 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1513}
1514
Alex Williamson066f2e92014-06-12 16:12:37 -06001515static ssize_t amd_iommu_show_cap(struct device *dev,
1516 struct device_attribute *attr,
1517 char *buf)
1518{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001519 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001520 return sprintf(buf, "%x\n", iommu->cap);
1521}
1522static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1523
1524static ssize_t amd_iommu_show_features(struct device *dev,
1525 struct device_attribute *attr,
1526 char *buf)
1527{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001528 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001529 return sprintf(buf, "%llx\n", iommu->features);
1530}
1531static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1532
1533static struct attribute *amd_iommu_attrs[] = {
1534 &dev_attr_cap.attr,
1535 &dev_attr_features.attr,
1536 NULL,
1537};
1538
1539static struct attribute_group amd_iommu_group = {
1540 .name = "amd-iommu",
1541 .attrs = amd_iommu_attrs,
1542};
1543
1544static const struct attribute_group *amd_iommu_groups[] = {
1545 &amd_iommu_group,
1546 NULL,
1547};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001548
Joerg Roedel23c742d2012-06-12 11:47:34 +02001549static int iommu_init_pci(struct amd_iommu *iommu)
1550{
1551 int cap_ptr = iommu->cap_ptr;
1552 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001553 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001554
Shuah Khanc5081cd2013-02-27 17:07:19 -07001555 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001556 iommu->devid & 0xff);
1557 if (!iommu->dev)
1558 return -ENODEV;
1559
Jiang Liucbbc00b2015-10-09 22:07:31 +08001560 /* Prevent binding other PCI device drivers to IOMMU devices */
1561 iommu->dev->match_driver = false;
1562
Joerg Roedel23c742d2012-06-12 11:47:34 +02001563 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1564 &iommu->cap);
1565 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1566 &range);
1567 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1568 &misc);
1569
Joerg Roedel23c742d2012-06-12 11:47:34 +02001570 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1571 amd_iommu_iotlb_sup = false;
1572
1573 /* read extended feature bits */
1574 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1575 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1576
1577 iommu->features = ((u64)high << 32) | low;
1578
1579 if (iommu_feature(iommu, FEATURE_GT)) {
1580 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001581 u32 max_pasid;
1582 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001583
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001584 pasmax = iommu->features & FEATURE_PASID_MASK;
1585 pasmax >>= FEATURE_PASID_SHIFT;
1586 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001587
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001588 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1589
1590 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001591
1592 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1593 glxval >>= FEATURE_GLXVAL_SHIFT;
1594
1595 if (amd_iommu_max_glx_val == -1)
1596 amd_iommu_max_glx_val = glxval;
1597 else
1598 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1599 }
1600
1601 if (iommu_feature(iommu, FEATURE_GT) &&
1602 iommu_feature(iommu, FEATURE_PPR)) {
1603 iommu->is_iommu_v2 = true;
1604 amd_iommu_v2_present = true;
1605 }
1606
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001607 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1608 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001609
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001610 ret = iommu_init_ga(iommu);
1611 if (ret)
1612 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001613
Joerg Roedel23c742d2012-06-12 11:47:34 +02001614 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1615 amd_iommu_np_cache = true;
1616
Steven L Kinney30861dd2013-06-05 16:11:48 -05001617 init_iommu_perf_ctr(iommu);
1618
Joerg Roedel23c742d2012-06-12 11:47:34 +02001619 if (is_rd890_iommu(iommu->dev)) {
1620 int i, j;
1621
1622 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1623 PCI_DEVFN(0, 0));
1624
1625 /*
1626 * Some rd890 systems may not be fully reconfigured by the
1627 * BIOS, so it's necessary for us to store this information so
1628 * it can be reprogrammed on resume
1629 */
1630 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1631 &iommu->stored_addr_lo);
1632 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1633 &iommu->stored_addr_hi);
1634
1635 /* Low bit locks writes to configuration space */
1636 iommu->stored_addr_lo &= ~1;
1637
1638 for (i = 0; i < 6; i++)
1639 for (j = 0; j < 0x12; j++)
1640 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1641
1642 for (i = 0; i < 0x83; i++)
1643 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1644 }
1645
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001646 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001647 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001648
Joerg Roedel39ab9552017-02-01 16:56:46 +01001649 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1650 amd_iommu_groups, "ivhd%d", iommu->index);
Joerg Roedelb0119e82017-02-01 13:23:08 +01001651 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1652 iommu_device_register(&iommu->iommu);
Alex Williamson066f2e92014-06-12 16:12:37 -06001653
Joerg Roedel23c742d2012-06-12 11:47:34 +02001654 return pci_enable_device(iommu->dev);
1655}
1656
Joerg Roedel4d121c32012-06-14 12:21:55 +02001657static void print_iommu_info(void)
1658{
1659 static const char * const feat_str[] = {
1660 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1661 "IA", "GA", "HE", "PC"
1662 };
1663 struct amd_iommu *iommu;
1664
1665 for_each_iommu(iommu) {
1666 int i;
1667
1668 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1669 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1670
1671 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001672 pr_info("AMD-Vi: Extended features (%#llx):\n",
1673 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001674 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001675 if (iommu_feature(iommu, (1ULL << i)))
1676 pr_cont(" %s", feat_str[i]);
1677 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001678
1679 if (iommu->features & FEATURE_GAM_VAPIC)
1680 pr_cont(" GA_vAPIC");
1681
Steven L Kinney30861dd2013-06-05 16:11:48 -05001682 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001683 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001684 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001685 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001686 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001687 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1688 pr_info("AMD-Vi: virtual APIC enabled\n");
1689 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001690}
1691
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001692static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001693{
1694 struct amd_iommu *iommu;
1695 int ret = 0;
1696
1697 for_each_iommu(iommu) {
1698 ret = iommu_init_pci(iommu);
1699 if (ret)
1700 break;
1701 }
1702
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001703 /*
1704 * Order is important here to make sure any unity map requirements are
1705 * fulfilled. The unity mappings are created and written to the device
1706 * table during the amd_iommu_init_api() call.
1707 *
1708 * After that we call init_device_table_dma() to make sure any
1709 * uninitialized DTE will block DMA, and in the end we flush the caches
1710 * of all IOMMUs to make sure the changes to the device table are
1711 * active.
1712 */
1713 ret = amd_iommu_init_api();
1714
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001715 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001716
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001717 for_each_iommu(iommu)
1718 iommu_flush_all_caches(iommu);
1719
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001720 if (!ret)
1721 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001722
Joerg Roedel23c742d2012-06-12 11:47:34 +02001723 return ret;
1724}
1725
Joerg Roedelb65233a2008-07-11 17:14:21 +02001726/****************************************************************************
1727 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001728 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001729 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001730 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1731 * pci_dev.
1732 *
1733 ****************************************************************************/
1734
Joerg Roedel9f800de2009-11-23 12:45:25 +01001735static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001736{
1737 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001738
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001739 r = pci_enable_msi(iommu->dev);
1740 if (r)
1741 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001742
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001743 r = request_threaded_irq(iommu->dev->irq,
1744 amd_iommu_int_handler,
1745 amd_iommu_int_thread,
1746 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001747 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001748
1749 if (r) {
1750 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001751 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001752 }
1753
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001754 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001755
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001756 return 0;
1757}
1758
Joerg Roedel05f92db2009-05-12 09:52:46 +02001759static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001760{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001761 int ret;
1762
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001763 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001764 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001765
Yijing Wang82fcfc62013-08-08 21:12:36 +08001766 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001767 ret = iommu_setup_msi(iommu);
1768 else
1769 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001770
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001771 if (ret)
1772 return ret;
1773
1774enable_faults:
1775 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1776
1777 if (iommu->ppr_log != NULL)
1778 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1779
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001780 iommu_ga_log_enable(iommu);
1781
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001782 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001783}
1784
1785/****************************************************************************
1786 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001787 * The next functions belong to the third pass of parsing the ACPI
1788 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001789 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001790 *
1791 ****************************************************************************/
1792
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001793static void __init free_unity_maps(void)
1794{
1795 struct unity_map_entry *entry, *next;
1796
1797 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1798 list_del(&entry->list);
1799 kfree(entry);
1800 }
1801}
1802
Joerg Roedelb65233a2008-07-11 17:14:21 +02001803/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001804static int __init init_exclusion_range(struct ivmd_header *m)
1805{
1806 int i;
1807
1808 switch (m->type) {
1809 case ACPI_IVMD_TYPE:
1810 set_device_exclusion_range(m->devid, m);
1811 break;
1812 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001813 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001814 set_device_exclusion_range(i, m);
1815 break;
1816 case ACPI_IVMD_TYPE_RANGE:
1817 for (i = m->devid; i <= m->aux; ++i)
1818 set_device_exclusion_range(i, m);
1819 break;
1820 default:
1821 break;
1822 }
1823
1824 return 0;
1825}
1826
Joerg Roedelb65233a2008-07-11 17:14:21 +02001827/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001828static int __init init_unity_map_range(struct ivmd_header *m)
1829{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001830 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001831 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001832
1833 e = kzalloc(sizeof(*e), GFP_KERNEL);
1834 if (e == NULL)
1835 return -ENOMEM;
1836
1837 switch (m->type) {
1838 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001839 kfree(e);
1840 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001841 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001842 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001843 e->devid_start = e->devid_end = m->devid;
1844 break;
1845 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001846 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001847 e->devid_start = 0;
1848 e->devid_end = amd_iommu_last_bdf;
1849 break;
1850 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001851 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001852 e->devid_start = m->devid;
1853 e->devid_end = m->aux;
1854 break;
1855 }
1856 e->address_start = PAGE_ALIGN(m->range_start);
1857 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1858 e->prot = m->flags >> 1;
1859
Joerg Roedel02acc432009-05-20 16:24:21 +02001860 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1861 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001862 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1863 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02001864 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1865 e->address_start, e->address_end, m->flags);
1866
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001867 list_add_tail(&e->list, &amd_iommu_unity_map);
1868
1869 return 0;
1870}
1871
Joerg Roedelb65233a2008-07-11 17:14:21 +02001872/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001873static int __init init_memory_definitions(struct acpi_table_header *table)
1874{
1875 u8 *p = (u8 *)table, *end = (u8 *)table;
1876 struct ivmd_header *m;
1877
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001878 end += table->length;
1879 p += IVRS_HEADER_LENGTH;
1880
1881 while (p < end) {
1882 m = (struct ivmd_header *)p;
1883 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1884 init_exclusion_range(m);
1885 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1886 init_unity_map_range(m);
1887
1888 p += m->length;
1889 }
1890
1891 return 0;
1892}
1893
Joerg Roedelb65233a2008-07-11 17:14:21 +02001894/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001895 * Init the device table to not allow DMA access for devices and
1896 * suppress all page faults
1897 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02001898static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001899{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001900 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001901
1902 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1903 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1904 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel54bd6352017-06-15 10:36:22 +02001905 /*
1906 * In kdump kernels in-flight DMA from the old kernel might
1907 * cause IO_PAGE_FAULTs. There are no reports that a kdump
1908 * actually failed because of that, so just disable fault
1909 * reporting in the hardware to get rid of the messages
1910 */
1911 if (is_kdump_kernel())
1912 set_dev_entry_bit(devid, DEV_ENTRY_NO_PAGE_FAULT);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001913 }
1914}
1915
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001916static void __init uninit_device_table_dma(void)
1917{
1918 u32 devid;
1919
1920 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1921 amd_iommu_dev_table[devid].data[0] = 0ULL;
1922 amd_iommu_dev_table[devid].data[1] = 0ULL;
1923 }
1924}
1925
Joerg Roedel33f28c52012-06-15 18:03:31 +02001926static void init_device_table(void)
1927{
1928 u32 devid;
1929
1930 if (!amd_iommu_irq_remap)
1931 return;
1932
1933 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1934 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1935}
1936
Joerg Roedele9bf5192010-09-20 14:33:07 +02001937static void iommu_init_flags(struct amd_iommu *iommu)
1938{
1939 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1940 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1941 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1942
1943 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1944 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1945 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1946
1947 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1948 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1949 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1950
1951 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1952 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1953 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1954
1955 /*
1956 * make IOMMU memory accesses cache coherent
1957 */
1958 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001959
1960 /* Set IOTLB invalidation timeout to 1s */
1961 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001962}
1963
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001964static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001965{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001966 int i, j;
1967 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001968 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001969
1970 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001971 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001972 return;
1973
1974 /*
1975 * First, we need to ensure that the iommu is enabled. This is
1976 * controlled by a register in the northbridge
1977 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001978
1979 /* Select Northbridge indirect register 0x75 and enable writing */
1980 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1981 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1982
1983 /* Enable the iommu */
1984 if (!(ioc_feature_control & 0x1))
1985 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1986
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001987 /* Restore the iommu BAR */
1988 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1989 iommu->stored_addr_lo);
1990 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1991 iommu->stored_addr_hi);
1992
1993 /* Restore the l1 indirect regs for each of the 6 l1s */
1994 for (i = 0; i < 6; i++)
1995 for (j = 0; j < 0x12; j++)
1996 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1997
1998 /* Restore the l2 indirect regs */
1999 for (i = 0; i < 0x83; i++)
2000 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2001
2002 /* Lock PCI setup registers */
2003 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2004 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02002005}
2006
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002007static void iommu_enable_ga(struct amd_iommu *iommu)
2008{
2009#ifdef CONFIG_IRQ_REMAP
2010 switch (amd_iommu_guest_ir) {
2011 case AMD_IOMMU_GUEST_IR_VAPIC:
2012 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2013 /* Fall through */
2014 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2015 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002016 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002017 break;
2018 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002019 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002020 break;
2021 }
2022#endif
2023}
2024
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002025/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002026 * This function finally enables all IOMMUs found in the system after
2027 * they have been initialized
2028 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002029static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002030{
2031 struct amd_iommu *iommu;
2032
Joerg Roedel3bd22172009-05-04 15:06:20 +02002033 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02002034 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002035 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02002036 iommu_set_device_table(iommu);
2037 iommu_enable_command_buffer(iommu);
2038 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002039 iommu_set_exclusion_range(iommu);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002040 iommu_enable_ga(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002041 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02002042 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002043 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002044
2045#ifdef CONFIG_IRQ_REMAP
2046 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2047 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2048#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002049}
2050
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002051static void enable_iommus_v2(void)
2052{
2053 struct amd_iommu *iommu;
2054
2055 for_each_iommu(iommu) {
2056 iommu_enable_ppr_log(iommu);
2057 iommu_enable_gt(iommu);
2058 }
2059}
2060
2061static void enable_iommus(void)
2062{
2063 early_enable_iommus();
2064
2065 enable_iommus_v2();
2066}
2067
Joerg Roedel92ac4322009-05-19 19:06:27 +02002068static void disable_iommus(void)
2069{
2070 struct amd_iommu *iommu;
2071
2072 for_each_iommu(iommu)
2073 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002074
2075#ifdef CONFIG_IRQ_REMAP
2076 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2077 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2078#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002079}
2080
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002081/*
2082 * Suspend/Resume support
2083 * disable suspend until real resume implemented
2084 */
2085
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002086static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002087{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002088 struct amd_iommu *iommu;
2089
2090 for_each_iommu(iommu)
2091 iommu_apply_resume_quirks(iommu);
2092
Joerg Roedel736501e2009-05-12 09:56:12 +02002093 /* re-load the hardware */
2094 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002095
2096 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002097}
2098
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002099static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002100{
Joerg Roedel736501e2009-05-12 09:56:12 +02002101 /* disable IOMMUs to go out of the way for BIOS */
2102 disable_iommus();
2103
2104 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002105}
2106
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002107static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002108 .suspend = amd_iommu_suspend,
2109 .resume = amd_iommu_resume,
2110};
2111
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002112static void __init free_iommu_resources(void)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002113{
Lucas Stachebcfa282016-10-26 13:09:53 +02002114 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002115 free_pages((unsigned long)irq_lookup_table,
2116 get_order(rlookup_table_size));
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002117
Julia Lawalla5919892015-09-13 14:15:31 +02002118 kmem_cache_destroy(amd_iommu_irq_cache);
2119 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002120
2121 free_pages((unsigned long)amd_iommu_rlookup_table,
2122 get_order(rlookup_table_size));
2123
2124 free_pages((unsigned long)amd_iommu_alias_table,
2125 get_order(alias_table_size));
2126
2127 free_pages((unsigned long)amd_iommu_dev_table,
2128 get_order(dev_table_size));
2129
2130 free_iommu_all();
2131
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002132#ifdef CONFIG_GART_IOMMU
2133 /*
2134 * We failed to initialize the AMD IOMMU - try fallback to GART
2135 * if possible.
2136 */
2137 gart_iommu_init();
2138
2139#endif
2140}
2141
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002142/* SB IOAPIC is always on this device in AMD systems */
2143#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2144
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002145static bool __init check_ioapic_information(void)
2146{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002147 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002148 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002149 int idx;
2150
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002151 has_sb_ioapic = false;
2152 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002153
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002154 /*
2155 * If we have map overrides on the kernel command line the
2156 * messages in this function might not describe firmware bugs
2157 * anymore - so be careful
2158 */
2159 if (cmdline_maps)
2160 fw_bug = "";
2161
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002162 for (idx = 0; idx < nr_ioapics; idx++) {
2163 int devid, id = mpc_ioapic_id(idx);
2164
2165 devid = get_ioapic_devid(id);
2166 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002167 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2168 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002169 ret = false;
2170 } else if (devid == IOAPIC_SB_DEVID) {
2171 has_sb_ioapic = true;
2172 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002173 }
2174 }
2175
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002176 if (!has_sb_ioapic) {
2177 /*
2178 * We expect the SB IOAPIC to be listed in the IVRS
2179 * table. The system timer is connected to the SB IOAPIC
2180 * and if we don't have it in the list the system will
2181 * panic at boot time. This situation usually happens
2182 * when the BIOS is buggy and provides us the wrong
2183 * device id for the IOAPIC in the system.
2184 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002185 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002186 }
2187
2188 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002189 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002190
2191 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002192}
2193
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002194static void __init free_dma_resources(void)
2195{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002196 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2197 get_order(MAX_DOMAIN_ID/8));
2198
2199 free_unity_maps();
2200}
2201
Joerg Roedelb65233a2008-07-11 17:14:21 +02002202/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002203 * This is the hardware init function for AMD IOMMU in the system.
2204 * This function is called either from amd_iommu_init or from the interrupt
2205 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002206 *
2207 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002208 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002209 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002210 * 1 pass) Discover the most comprehensive IVHD type to use.
2211 *
2212 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002213 * Upon this information the size of the data structures is
2214 * determined that needs to be allocated.
2215 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002216 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002217 * information in the ACPI table about available AMD IOMMUs
2218 * in the system. It also maps the PCI devices in the
2219 * system to specific IOMMUs
2220 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002221 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002222 * initialized we update them with information about memory
2223 * remapping requirements parsed out of the ACPI table in
2224 * this last pass.
2225 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002226 * After everything is set up the IOMMUs are enabled and the necessary
2227 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002228 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002229static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002230{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002231 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002232 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002233 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002234
Joerg Roedel643511b2012-06-12 12:09:35 +02002235 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002236 return -ENODEV;
2237
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002238 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002239 if (status == AE_NOT_FOUND)
2240 return -ENODEV;
2241 else if (ACPI_FAILURE(status)) {
2242 const char *err = acpi_format_exception(status);
2243 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2244 return -EINVAL;
2245 }
2246
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002247 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002248 * Validate checksum here so we don't need to do it when
2249 * we actually parse the table
2250 */
2251 ret = check_ivrs_checksum(ivrs_base);
2252 if (ret)
Rafael J. Wysocki99e8ccd2017-01-10 14:57:28 +01002253 goto out;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002254
2255 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2256 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2257
2258 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002259 * First parse ACPI tables to find the largest Bus/Dev/Func
2260 * we need to handle. Upon this information the shared data
2261 * structures for the IOMMUs in the system will be allocated
2262 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002263 ret = find_last_devid_acpi(ivrs_base);
2264 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002265 goto out;
2266
Joerg Roedelc5714842008-07-11 17:14:25 +02002267 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2268 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2269 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002270
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002271 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002272 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002273 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002274 get_order(dev_table_size));
2275 if (amd_iommu_dev_table == NULL)
2276 goto out;
2277
2278 /*
2279 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2280 * IOMMU see for that device
2281 */
2282 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2283 get_order(alias_table_size));
2284 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002285 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002286
2287 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002288 amd_iommu_rlookup_table = (void *)__get_free_pages(
2289 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002290 get_order(rlookup_table_size));
2291 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002292 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002293
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002294 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2295 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002296 get_order(MAX_DOMAIN_ID/8));
2297 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002298 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002299
2300 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002301 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002302 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002303 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002304 amd_iommu_alias_table[i] = i;
2305
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002306 /*
2307 * never allocate domain 0 because its used as the non-allocated and
2308 * error value placeholder
2309 */
Baoquan He5c87f622016-09-15 16:50:51 +08002310 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002311
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002312 spin_lock_init(&amd_iommu_pd_lock);
2313
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002314 /*
2315 * now the data structures are allocated and basically initialized
2316 * start the real acpi table scan
2317 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002318 ret = init_iommu_all(ivrs_base);
2319 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002320 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002321
Joerg Roedel11123742017-06-16 16:09:54 +02002322 /* Disable any previously enabled IOMMUs */
2323 disable_iommus();
2324
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002325 if (amd_iommu_irq_remap)
2326 amd_iommu_irq_remap = check_ioapic_information();
2327
Joerg Roedel05152a02012-06-15 16:53:51 +02002328 if (amd_iommu_irq_remap) {
2329 /*
2330 * Interrupt remapping enabled, create kmem_cache for the
2331 * remapping tables.
2332 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002333 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002334 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2335 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2336 else
2337 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002338 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002339 remap_cache_sz,
2340 IRQ_TABLE_ALIGNMENT,
2341 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002342 if (!amd_iommu_irq_cache)
2343 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002344
2345 irq_lookup_table = (void *)__get_free_pages(
2346 GFP_KERNEL | __GFP_ZERO,
2347 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002348 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2349 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002350 if (!irq_lookup_table)
2351 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002352 }
2353
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002354 ret = init_memory_definitions(ivrs_base);
2355 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002356 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002357
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002358 /* init the device table */
2359 init_device_table();
2360
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002361out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002362 /* Don't leak any ACPI memory */
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002363 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002364 ivrs_base = NULL;
2365
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002366 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002367}
2368
Gerard Snitselaarae295142012-03-16 11:38:22 -07002369static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002370{
2371 struct amd_iommu *iommu;
2372 int ret = 0;
2373
2374 for_each_iommu(iommu) {
2375 ret = iommu_init_msi(iommu);
2376 if (ret)
2377 goto out;
2378 }
2379
2380out:
2381 return ret;
2382}
2383
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002384static bool detect_ivrs(void)
2385{
2386 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002387 acpi_status status;
2388
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002389 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002390 if (status == AE_NOT_FOUND)
2391 return false;
2392 else if (ACPI_FAILURE(status)) {
2393 const char *err = acpi_format_exception(status);
2394 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2395 return false;
2396 }
2397
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002398 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002399
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002400 /* Make sure ACS will be enabled during PCI probe */
2401 pci_request_acs();
2402
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002403 return true;
2404}
2405
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002406/****************************************************************************
2407 *
2408 * AMD IOMMU Initialization State Machine
2409 *
2410 ****************************************************************************/
2411
2412static int __init state_next(void)
2413{
2414 int ret = 0;
2415
2416 switch (init_state) {
2417 case IOMMU_START_STATE:
2418 if (!detect_ivrs()) {
2419 init_state = IOMMU_NOT_FOUND;
2420 ret = -ENODEV;
2421 } else {
2422 init_state = IOMMU_IVRS_DETECTED;
2423 }
2424 break;
2425 case IOMMU_IVRS_DETECTED:
2426 ret = early_amd_iommu_init();
2427 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2428 break;
2429 case IOMMU_ACPI_FINISHED:
2430 early_enable_iommus();
2431 register_syscore_ops(&amd_iommu_syscore_ops);
2432 x86_platform.iommu_shutdown = disable_iommus;
2433 init_state = IOMMU_ENABLED;
2434 break;
2435 case IOMMU_ENABLED:
2436 ret = amd_iommu_init_pci();
2437 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2438 enable_iommus_v2();
2439 break;
2440 case IOMMU_PCI_INIT:
2441 ret = amd_iommu_enable_interrupts();
2442 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2443 break;
2444 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002445 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002446 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2447 break;
2448 case IOMMU_DMA_OPS:
2449 init_state = IOMMU_INITIALIZED;
2450 break;
2451 case IOMMU_INITIALIZED:
2452 /* Nothing to do */
2453 break;
2454 case IOMMU_NOT_FOUND:
2455 case IOMMU_INIT_ERROR:
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002456 case IOMMU_CMDLINE_DISABLED:
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002457 /* Error states => do nothing */
2458 ret = -EINVAL;
2459 break;
2460 default:
2461 /* Unknown state */
2462 BUG();
2463 }
2464
2465 return ret;
2466}
2467
2468static int __init iommu_go_to_state(enum iommu_init_state state)
2469{
Joerg Roedel151b0902017-06-16 16:09:57 +02002470 int ret = -EINVAL;
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002471
2472 while (init_state != state) {
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002473 if (init_state == IOMMU_NOT_FOUND ||
2474 init_state == IOMMU_INIT_ERROR ||
2475 init_state == IOMMU_CMDLINE_DISABLED)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002476 break;
Joerg Roedel151b0902017-06-16 16:09:57 +02002477 ret = state_next();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002478 }
2479
2480 return ret;
2481}
2482
Joerg Roedel6b474b82012-06-26 16:46:04 +02002483#ifdef CONFIG_IRQ_REMAP
2484int __init amd_iommu_prepare(void)
2485{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002486 int ret;
2487
Jiang Liu7fa1c842015-01-07 15:31:42 +08002488 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002489
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002490 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2491 if (ret)
2492 return ret;
2493 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002494}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002495
Joerg Roedel6b474b82012-06-26 16:46:04 +02002496int __init amd_iommu_enable(void)
2497{
2498 int ret;
2499
2500 ret = iommu_go_to_state(IOMMU_ENABLED);
2501 if (ret)
2502 return ret;
2503
2504 irq_remapping_enabled = 1;
2505
2506 return 0;
2507}
2508
2509void amd_iommu_disable(void)
2510{
2511 amd_iommu_suspend();
2512}
2513
2514int amd_iommu_reenable(int mode)
2515{
2516 amd_iommu_resume();
2517
2518 return 0;
2519}
2520
2521int __init amd_iommu_enable_faulting(void)
2522{
2523 /* We enable MSI later when PCI is initialized */
2524 return 0;
2525}
2526#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002527
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002528/*
2529 * This is the core init function for AMD IOMMU hardware in the system.
2530 * This function is called from the generic x86 DMA layer initialization
2531 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002532 */
2533static int __init amd_iommu_init(void)
2534{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002535 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002536
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002537 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2538 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002539 free_dma_resources();
2540 if (!irq_remapping_enabled) {
2541 disable_iommus();
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002542 free_iommu_resources();
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002543 } else {
2544 struct amd_iommu *iommu;
2545
2546 uninit_device_table_dma();
2547 for_each_iommu(iommu)
2548 iommu_flush_all_caches(iommu);
2549 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002550 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002551
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002552 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002553}
2554
Joerg Roedelb65233a2008-07-11 17:14:21 +02002555/****************************************************************************
2556 *
2557 * Early detect code. This code runs at IOMMU detection time in the DMA
2558 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2559 * IOMMUs
2560 *
2561 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002562int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002563{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002564 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002565
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002566 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002567 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002568
Joerg Roedela5235722010-05-11 17:12:33 +02002569 if (amd_iommu_disabled)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002570 return -ENODEV;
Joerg Roedela5235722010-05-11 17:12:33 +02002571
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002572 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2573 if (ret)
2574 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002575
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002576 amd_iommu_detected = true;
2577 iommu_detected = 1;
2578 x86_init.iommu.iommu_init = amd_iommu_init;
2579
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002580 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002581}
2582
Joerg Roedelb65233a2008-07-11 17:14:21 +02002583/****************************************************************************
2584 *
2585 * Parsing functions for the AMD IOMMU specific kernel command line
2586 * options.
2587 *
2588 ****************************************************************************/
2589
Joerg Roedelfefda112009-05-20 12:21:42 +02002590static int __init parse_amd_iommu_dump(char *str)
2591{
2592 amd_iommu_dump = true;
2593
2594 return 1;
2595}
2596
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002597static int __init parse_amd_iommu_intr(char *str)
2598{
2599 for (; *str; ++str) {
2600 if (strncmp(str, "legacy", 6) == 0) {
2601 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2602 break;
2603 }
2604 if (strncmp(str, "vapic", 5) == 0) {
2605 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2606 break;
2607 }
2608 }
2609 return 1;
2610}
2611
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002612static int __init parse_amd_iommu_options(char *str)
2613{
2614 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002615 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002616 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002617 if (strncmp(str, "off", 3) == 0)
2618 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002619 if (strncmp(str, "force_isolation", 15) == 0)
2620 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002621 }
2622
2623 return 1;
2624}
2625
Joerg Roedel440e89982013-04-09 16:35:28 +02002626static int __init parse_ivrs_ioapic(char *str)
2627{
2628 unsigned int bus, dev, fn;
2629 int ret, id, i;
2630 u16 devid;
2631
2632 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2633
2634 if (ret != 4) {
2635 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2636 return 1;
2637 }
2638
2639 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2640 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2641 str);
2642 return 1;
2643 }
2644
2645 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2646
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002647 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002648 i = early_ioapic_map_size++;
2649 early_ioapic_map[i].id = id;
2650 early_ioapic_map[i].devid = devid;
2651 early_ioapic_map[i].cmd_line = true;
2652
2653 return 1;
2654}
2655
2656static int __init parse_ivrs_hpet(char *str)
2657{
2658 unsigned int bus, dev, fn;
2659 int ret, id, i;
2660 u16 devid;
2661
2662 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2663
2664 if (ret != 4) {
2665 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2666 return 1;
2667 }
2668
2669 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2670 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2671 str);
2672 return 1;
2673 }
2674
2675 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2676
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002677 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002678 i = early_hpet_map_size++;
2679 early_hpet_map[i].id = id;
2680 early_hpet_map[i].devid = devid;
2681 early_hpet_map[i].cmd_line = true;
2682
2683 return 1;
2684}
2685
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002686static int __init parse_ivrs_acpihid(char *str)
2687{
2688 u32 bus, dev, fn;
2689 char *hid, *uid, *p;
2690 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2691 int ret, i;
2692
2693 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2694 if (ret != 4) {
2695 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2696 return 1;
2697 }
2698
2699 p = acpiid;
2700 hid = strsep(&p, ":");
2701 uid = p;
2702
2703 if (!hid || !(*hid) || !uid) {
2704 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2705 return 1;
2706 }
2707
2708 i = early_acpihid_map_size++;
2709 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2710 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2711 early_acpihid_map[i].devid =
2712 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2713 early_acpihid_map[i].cmd_line = true;
2714
2715 return 1;
2716}
2717
Joerg Roedel440e89982013-04-09 16:35:28 +02002718__setup("amd_iommu_dump", parse_amd_iommu_dump);
2719__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002720__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002721__setup("ivrs_ioapic", parse_ivrs_ioapic);
2722__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002723__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002724
2725IOMMU_INIT_FINISH(amd_iommu_detect,
2726 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002727 NULL,
2728 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002729
2730bool amd_iommu_v2_supported(void)
2731{
2732 return amd_iommu_v2_present;
2733}
2734EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002735
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002736struct amd_iommu *get_amd_iommu(unsigned int idx)
2737{
2738 unsigned int i = 0;
2739 struct amd_iommu *iommu;
2740
2741 for_each_iommu(iommu)
2742 if (i++ == idx)
2743 return iommu;
2744 return NULL;
2745}
2746EXPORT_SYMBOL(get_amd_iommu);
2747
Steven L Kinney30861dd2013-06-05 16:11:48 -05002748/****************************************************************************
2749 *
2750 * IOMMU EFR Performance Counter support functionality. This code allows
2751 * access to the IOMMU PC functionality.
2752 *
2753 ****************************************************************************/
2754
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002755u8 amd_iommu_pc_get_max_banks(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002756{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002757 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002758
Steven L Kinney30861dd2013-06-05 16:11:48 -05002759 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002760 return iommu->max_banks;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002761
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002762 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002763}
2764EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2765
2766bool amd_iommu_pc_supported(void)
2767{
2768 return amd_iommu_pc_present;
2769}
2770EXPORT_SYMBOL(amd_iommu_pc_supported);
2771
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002772u8 amd_iommu_pc_get_max_counters(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002773{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002774 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002775
Steven L Kinney30861dd2013-06-05 16:11:48 -05002776 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002777 return iommu->max_counters;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002778
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002779 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002780}
2781EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2782
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002783static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
2784 u8 fxn, u64 *value, bool is_write)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002785{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002786 u32 offset;
2787 u32 max_offset_lim;
2788
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002789 /* Make sure the IOMMU PC resource is available */
2790 if (!amd_iommu_pc_present)
2791 return -ENODEV;
2792
Steven L Kinney30861dd2013-06-05 16:11:48 -05002793 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002794 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002795 return -ENODEV;
2796
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002797 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002798
2799 /* Limit the offset to the hw defined mmio region aperture */
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002800 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
Steven L Kinney30861dd2013-06-05 16:11:48 -05002801 (iommu->max_counters << 8) | 0x28);
2802 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2803 (offset > max_offset_lim))
2804 return -EINVAL;
2805
2806 if (is_write) {
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002807 u64 val = *value & GENMASK_ULL(47, 0);
2808
2809 writel((u32)val, iommu->mmio_base + offset);
2810 writel((val >> 32), iommu->mmio_base + offset + 4);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002811 } else {
2812 *value = readl(iommu->mmio_base + offset + 4);
2813 *value <<= 32;
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002814 *value |= readl(iommu->mmio_base + offset);
2815 *value &= GENMASK_ULL(47, 0);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002816 }
2817
2818 return 0;
2819}
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002820
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002821int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002822{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002823 if (!iommu)
2824 return -EINVAL;
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002825
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002826 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002827}
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002828EXPORT_SYMBOL(amd_iommu_pc_get_reg);
2829
2830int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
2831{
2832 if (!iommu)
2833 return -EINVAL;
2834
2835 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
2836}
2837EXPORT_SYMBOL(amd_iommu_pc_set_reg);