blob: c17febb823ec237e8574bd2f109cbc3156b813e4 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010024#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020025#include <linux/interrupt.h>
26#include <linux/msi.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020027#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010028#include <linux/export.h>
Alex Williamson066f2e92014-06-12 16:12:37 -060029#include <linux/iommu.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020030#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090031#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010032#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090033#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040034#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020035#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020036#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020037
38#include "amd_iommu_proto.h"
39#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020040#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020041
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020042/*
43 * definitions for the ACPI scanning code
44 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020045#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020046
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040047#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020048#define ACPI_IVMD_TYPE_ALL 0x20
49#define ACPI_IVMD_TYPE 0x21
50#define ACPI_IVMD_TYPE_RANGE 0x22
51
52#define IVHD_DEV_ALL 0x01
53#define IVHD_DEV_SELECT 0x02
54#define IVHD_DEV_SELECT_RANGE_START 0x03
55#define IVHD_DEV_RANGE_END 0x04
56#define IVHD_DEV_ALIAS 0x42
57#define IVHD_DEV_ALIAS_RANGE 0x43
58#define IVHD_DEV_EXT_SELECT 0x46
59#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020060#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040061#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020062
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040063#define UID_NOT_PRESENT 0
64#define UID_IS_INTEGER 1
65#define UID_IS_CHARACTER 2
66
Joerg Roedel6efed632012-06-14 15:52:58 +020067#define IVHD_SPECIAL_IOAPIC 1
68#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020069
Joerg Roedel6da73422009-05-04 11:44:38 +020070#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
71#define IVHD_FLAG_PASSPW_EN_MASK 0x02
72#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
73#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020074
75#define IVMD_FLAG_EXCL_RANGE 0x08
76#define IVMD_FLAG_UNITY_MAP 0x01
77
78#define ACPI_DEVFLAG_INITPASS 0x01
79#define ACPI_DEVFLAG_EXTINT 0x02
80#define ACPI_DEVFLAG_NMI 0x04
81#define ACPI_DEVFLAG_SYSMGT1 0x10
82#define ACPI_DEVFLAG_SYSMGT2 0x20
83#define ACPI_DEVFLAG_LINT0 0x40
84#define ACPI_DEVFLAG_LINT1 0x80
85#define ACPI_DEVFLAG_ATSDIS 0x10000000
86
Joerg Roedelb65233a2008-07-11 17:14:21 +020087/*
88 * ACPI table definitions
89 *
90 * These data structures are laid over the table to parse the important values
91 * out of it.
92 */
93
94/*
95 * structure describing one IOMMU in the ACPI table. Typically followed by one
96 * or more ivhd_entrys.
97 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020098struct ivhd_header {
99 u8 type;
100 u8 flags;
101 u16 length;
102 u16 devid;
103 u16 cap_ptr;
104 u64 mmio_phys;
105 u16 pci_seg;
106 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400107 u32 efr_attr;
108
109 /* Following only valid on IVHD type 11h and 40h */
110 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
111 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200112} __attribute__((packed));
113
Joerg Roedelb65233a2008-07-11 17:14:21 +0200114/*
115 * A device entry describing which devices a specific IOMMU translates and
116 * which requestor ids they use.
117 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200118struct ivhd_entry {
119 u8 type;
120 u16 devid;
121 u8 flags;
122 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400123 u32 hidh;
124 u64 cid;
125 u8 uidf;
126 u8 uidl;
127 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200128} __attribute__((packed));
129
Joerg Roedelb65233a2008-07-11 17:14:21 +0200130/*
131 * An AMD IOMMU memory definition structure. It defines things like exclusion
132 * ranges for devices and regions that should be unity mapped.
133 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200134struct ivmd_header {
135 u8 type;
136 u8 flags;
137 u16 length;
138 u16 devid;
139 u16 aux;
140 u64 resv;
141 u64 range_start;
142 u64 range_length;
143} __attribute__((packed));
144
Joerg Roedelfefda112009-05-20 12:21:42 +0200145bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200146bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200147
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500148int amd_iommu_guest_ir;
149
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200150static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200151static bool __initdata amd_iommu_disabled;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400152static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200153
Joerg Roedelb65233a2008-07-11 17:14:21 +0200154u16 amd_iommu_last_bdf; /* largest PCI device id we have
155 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200156LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200157 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700158bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200159
Joerg Roedel2e228472008-07-11 17:14:31 +0200160LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200161 system */
162
Joerg Roedelbb527772009-11-20 14:31:51 +0100163/* Array to assign indices to IOMMUs*/
164struct amd_iommu *amd_iommus[MAX_IOMMUS];
165int amd_iommus_present;
166
Joerg Roedel318afd42009-11-23 18:32:38 +0100167/* IOMMUs have a non-present cache? */
168bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200169bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100170
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600171u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100172
Joerg Roedel400a28a2011-11-28 15:11:02 +0100173bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200174static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100175
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100176bool amd_iommu_force_isolation __read_mostly;
177
Joerg Roedelb65233a2008-07-11 17:14:21 +0200178/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100179 * List of protection domains - used during resume
180 */
181LIST_HEAD(amd_iommu_pd_list);
182spinlock_t amd_iommu_pd_lock;
183
184/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200185 * Pointer to the device table which is shared by all AMD IOMMUs
186 * it is indexed by the PCI device id or the HT unit id and contains
187 * information about the domain the device belongs to as well as the
188 * page table root pointer.
189 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200190struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200191
192/*
193 * The alias table is a driver specific data structure which contains the
194 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
195 * More than one device can share the same requestor id.
196 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200197u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200198
199/*
200 * The rlookup table is used to find the IOMMU which is responsible
201 * for a specific device. It is also indexed by the PCI device id.
202 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200203struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200204
205/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200206 * This table is used to find the irq remapping table for a given device id
207 * quickly.
208 */
209struct irq_remap_table **irq_lookup_table;
210
211/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200212 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200213 * to know which ones are already in use.
214 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200215unsigned long *amd_iommu_pd_alloc_bitmap;
216
Joerg Roedelb65233a2008-07-11 17:14:21 +0200217static u32 dev_table_size; /* size of the device table */
218static u32 alias_table_size; /* size of the alias table */
219static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200220
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200221enum iommu_init_state {
222 IOMMU_START_STATE,
223 IOMMU_IVRS_DETECTED,
224 IOMMU_ACPI_FINISHED,
225 IOMMU_ENABLED,
226 IOMMU_PCI_INIT,
227 IOMMU_INTERRUPTS_EN,
228 IOMMU_DMA_OPS,
229 IOMMU_INITIALIZED,
230 IOMMU_NOT_FOUND,
231 IOMMU_INIT_ERROR,
232};
233
Joerg Roedel235dacb2013-04-09 17:53:14 +0200234/* Early ioapic and hpet maps from kernel command line */
235#define EARLY_MAP_SIZE 4
236static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
237static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400238static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
239
Joerg Roedel235dacb2013-04-09 17:53:14 +0200240static int __initdata early_ioapic_map_size;
241static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400242static int __initdata early_acpihid_map_size;
243
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200244static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200245
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200246static enum iommu_init_state init_state = IOMMU_START_STATE;
247
Gerard Snitselaarae295142012-03-16 11:38:22 -0700248static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200249static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200250static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100251
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +0100252static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu,
253 u8 bank, u8 cntr, u8 fxn,
254 u64 *value, bool is_write);
255
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200256static inline void update_last_devid(u16 devid)
257{
258 if (devid > amd_iommu_last_bdf)
259 amd_iommu_last_bdf = devid;
260}
261
Joerg Roedelc5714842008-07-11 17:14:25 +0200262static inline unsigned long tbl_size(int entry_size)
263{
264 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100265 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200266
267 return 1UL << shift;
268}
269
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400270/* Access to l1 and l2 indexed register spaces */
271
272static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
273{
274 u32 val;
275
276 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
277 pci_read_config_dword(iommu->dev, 0xfc, &val);
278 return val;
279}
280
281static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
282{
283 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
284 pci_write_config_dword(iommu->dev, 0xfc, val);
285 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
286}
287
288static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
289{
290 u32 val;
291
292 pci_write_config_dword(iommu->dev, 0xf0, address);
293 pci_read_config_dword(iommu->dev, 0xf4, &val);
294 return val;
295}
296
297static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
298{
299 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
300 pci_write_config_dword(iommu->dev, 0xf4, val);
301}
302
Joerg Roedelb65233a2008-07-11 17:14:21 +0200303/****************************************************************************
304 *
305 * AMD IOMMU MMIO register space handling functions
306 *
307 * These functions are used to program the IOMMU device registers in
308 * MMIO space required for that driver.
309 *
310 ****************************************************************************/
311
312/*
313 * This function set the exclusion range in the IOMMU. DMA accesses to the
314 * exclusion range are passed through untranslated
315 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200316static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200317{
318 u64 start = iommu->exclusion_start & PAGE_MASK;
319 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
320 u64 entry;
321
322 if (!iommu->exclusion_start)
323 return;
324
325 entry = start | MMIO_EXCL_ENABLE_MASK;
326 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
327 &entry, sizeof(entry));
328
329 entry = limit;
330 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
331 &entry, sizeof(entry));
332}
333
Joerg Roedelb65233a2008-07-11 17:14:21 +0200334/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000335static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200336{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200337 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200338
339 BUG_ON(iommu->mmio_base == NULL);
340
341 entry = virt_to_phys(amd_iommu_dev_table);
342 entry |= (dev_table_size >> 12) - 1;
343 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
344 &entry, sizeof(entry));
345}
346
Joerg Roedelb65233a2008-07-11 17:14:21 +0200347/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200348static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200349{
350 u32 ctrl;
351
352 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
353 ctrl |= (1 << bit);
354 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
355}
356
Joerg Roedelca0207112009-10-28 18:02:26 +0100357static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200358{
359 u32 ctrl;
360
Joerg Roedel199d0d52008-09-17 16:45:59 +0200361 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200362 ctrl &= ~(1 << bit);
363 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
364}
365
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100366static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
367{
368 u32 ctrl;
369
370 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
371 ctrl &= ~CTRL_INV_TO_MASK;
372 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
373 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
374}
375
Joerg Roedelb65233a2008-07-11 17:14:21 +0200376/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200377static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200378{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200379 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200380}
381
Joerg Roedel92ac4322009-05-19 19:06:27 +0200382static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200383{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200384 /* Disable command buffer */
385 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
386
387 /* Disable event logging and event interrupts */
388 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
389 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
390
391 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200392 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200393}
394
Joerg Roedelb65233a2008-07-11 17:14:21 +0200395/*
396 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
397 * the system has one.
398 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500399static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200400{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500401 if (!request_mem_region(address, end, "amd_iommu")) {
402 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
403 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200404 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200405 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200406 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200407
Steven L Kinney30861dd2013-06-05 16:11:48 -0500408 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200409}
410
411static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
412{
413 if (iommu->mmio_base)
414 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500415 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200416}
417
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400418static inline u32 get_ivhd_header_size(struct ivhd_header *h)
419{
420 u32 size = 0;
421
422 switch (h->type) {
423 case 0x10:
424 size = 24;
425 break;
426 case 0x11:
427 case 0x40:
428 size = 40;
429 break;
430 }
431 return size;
432}
433
Joerg Roedelb65233a2008-07-11 17:14:21 +0200434/****************************************************************************
435 *
436 * The functions below belong to the first pass of AMD IOMMU ACPI table
437 * parsing. In this pass we try to find out the highest device id this
438 * code has to handle. Upon this information the size of the shared data
439 * structures is determined later.
440 *
441 ****************************************************************************/
442
443/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200444 * This function calculates the length of a given IVHD entry
445 */
446static inline int ivhd_entry_length(u8 *ivhd)
447{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400448 u32 type = ((struct ivhd_entry *)ivhd)->type;
449
450 if (type < 0x80) {
451 return 0x04 << (*ivhd >> 6);
452 } else if (type == IVHD_DEV_ACPI_HID) {
453 /* For ACPI_HID, offset 21 is uid len */
454 return *((u8 *)ivhd + 21) + 22;
455 }
456 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200457}
458
459/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200460 * After reading the highest device id from the IOMMU PCI capability header
461 * this function looks if there is a higher device id defined in the ACPI table
462 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200463static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
464{
465 u8 *p = (void *)h, *end = (void *)h;
466 struct ivhd_entry *dev;
467
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400468 u32 ivhd_size = get_ivhd_header_size(h);
469
470 if (!ivhd_size) {
471 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
472 return -EINVAL;
473 }
474
475 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200476 end += h->length;
477
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200478 while (p < end) {
479 dev = (struct ivhd_entry *)p;
480 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200481 case IVHD_DEV_ALL:
482 /* Use maximum BDF value for DEV_ALL */
483 update_last_devid(0xffff);
484 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200485 case IVHD_DEV_SELECT:
486 case IVHD_DEV_RANGE_END:
487 case IVHD_DEV_ALIAS:
488 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200489 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200490 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200491 break;
492 default:
493 break;
494 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200495 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200496 }
497
498 WARN_ON(p != end);
499
500 return 0;
501}
502
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400503static int __init check_ivrs_checksum(struct acpi_table_header *table)
504{
505 int i;
506 u8 checksum = 0, *p = (u8 *)table;
507
508 for (i = 0; i < table->length; ++i)
509 checksum += p[i];
510 if (checksum != 0) {
511 /* ACPI table corrupt */
512 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
513 return -ENODEV;
514 }
515
516 return 0;
517}
518
Joerg Roedelb65233a2008-07-11 17:14:21 +0200519/*
520 * Iterate over all IVHD entries in the ACPI table and find the highest device
521 * id which we need to handle. This is the first of three functions which parse
522 * the ACPI table. So we check the checksum here.
523 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200524static int __init find_last_devid_acpi(struct acpi_table_header *table)
525{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400526 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200527 struct ivhd_header *h;
528
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200529 p += IVRS_HEADER_LENGTH;
530
531 end += table->length;
532 while (p < end) {
533 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400534 if (h->type == amd_iommu_target_ivhd_type) {
535 int ret = find_last_devid_from_ivhd(h);
536
537 if (ret)
538 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200539 }
540 p += h->length;
541 }
542 WARN_ON(p != end);
543
544 return 0;
545}
546
Joerg Roedelb65233a2008-07-11 17:14:21 +0200547/****************************************************************************
548 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200549 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200550 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
551 * data structures, initialize the device/alias/rlookup table and also
552 * basically initialize the hardware.
553 *
554 ****************************************************************************/
555
556/*
557 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
558 * write commands to that buffer later and the IOMMU will execute them
559 * asynchronously
560 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200561static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200562{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200563 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
564 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200565
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200566 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200567}
568
569/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200570 * This function resets the command buffer if the IOMMU stopped fetching
571 * commands from it.
572 */
573void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
574{
575 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
576
577 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
578 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
579
580 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
581}
582
583/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200584 * This function writes the command buffer address to the hardware and
585 * enables it.
586 */
587static void iommu_enable_command_buffer(struct amd_iommu *iommu)
588{
589 u64 entry;
590
591 BUG_ON(iommu->cmd_buf == NULL);
592
593 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200594 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200595
Joerg Roedelb36ca912008-06-26 21:27:45 +0200596 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200597 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200598
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200599 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200600}
601
602static void __init free_command_buffer(struct amd_iommu *iommu)
603{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200604 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200605}
606
Joerg Roedel335503e2008-09-05 14:29:07 +0200607/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200608static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200609{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200610 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
611 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200612
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200613 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200614}
615
616static void iommu_enable_event_buffer(struct amd_iommu *iommu)
617{
618 u64 entry;
619
620 BUG_ON(iommu->evt_buf == NULL);
621
Joerg Roedel335503e2008-09-05 14:29:07 +0200622 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200623
Joerg Roedel335503e2008-09-05 14:29:07 +0200624 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
625 &entry, sizeof(entry));
626
Joerg Roedel090672072009-06-15 16:06:48 +0200627 /* set head and tail to zero manually */
628 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
629 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
630
Joerg Roedel58492e12009-05-04 18:41:16 +0200631 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200632}
633
634static void __init free_event_buffer(struct amd_iommu *iommu)
635{
636 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
637}
638
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100639/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200640static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100641{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200642 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
643 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100644
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200645 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100646}
647
648static void iommu_enable_ppr_log(struct amd_iommu *iommu)
649{
650 u64 entry;
651
652 if (iommu->ppr_log == NULL)
653 return;
654
655 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
656
657 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
658 &entry, sizeof(entry));
659
660 /* set head and tail to zero manually */
661 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
662 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
663
664 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
665 iommu_feature_enable(iommu, CONTROL_PPR_EN);
666}
667
668static void __init free_ppr_log(struct amd_iommu *iommu)
669{
670 if (iommu->ppr_log == NULL)
671 return;
672
673 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
674}
675
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100676static void iommu_enable_gt(struct amd_iommu *iommu)
677{
678 if (!iommu_feature(iommu, FEATURE_GT))
679 return;
680
681 iommu_feature_enable(iommu, CONTROL_GT_EN);
682}
683
Joerg Roedelb65233a2008-07-11 17:14:21 +0200684/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200685static void set_dev_entry_bit(u16 devid, u8 bit)
686{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100687 int i = (bit >> 6) & 0x03;
688 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200689
Joerg Roedelee6c2862011-11-09 12:06:03 +0100690 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200691}
692
Joerg Roedelc5cca142009-10-09 18:31:20 +0200693static int get_dev_entry_bit(u16 devid, u8 bit)
694{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100695 int i = (bit >> 6) & 0x03;
696 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200697
Joerg Roedelee6c2862011-11-09 12:06:03 +0100698 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200699}
700
701
702void amd_iommu_apply_erratum_63(u16 devid)
703{
704 int sysmgt;
705
706 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
707 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
708
709 if (sysmgt == 0x01)
710 set_dev_entry_bit(devid, DEV_ENTRY_IW);
711}
712
Joerg Roedel5ff47892008-07-14 20:11:18 +0200713/* Writes the specific IOMMU for a device into the rlookup table */
714static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
715{
716 amd_iommu_rlookup_table[devid] = iommu;
717}
718
Joerg Roedelb65233a2008-07-11 17:14:21 +0200719/*
720 * This function takes the device specific flags read from the ACPI
721 * table and sets up the device table entry with that information
722 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200723static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
724 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200725{
726 if (flags & ACPI_DEVFLAG_INITPASS)
727 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
728 if (flags & ACPI_DEVFLAG_EXTINT)
729 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
730 if (flags & ACPI_DEVFLAG_NMI)
731 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
732 if (flags & ACPI_DEVFLAG_SYSMGT1)
733 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
734 if (flags & ACPI_DEVFLAG_SYSMGT2)
735 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
736 if (flags & ACPI_DEVFLAG_LINT0)
737 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
738 if (flags & ACPI_DEVFLAG_LINT1)
739 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200740
Joerg Roedelc5cca142009-10-09 18:31:20 +0200741 amd_iommu_apply_erratum_63(devid);
742
Joerg Roedel5ff47892008-07-14 20:11:18 +0200743 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200744}
745
Joerg Roedelc50e3242014-09-09 15:59:37 +0200746static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200747{
748 struct devid_map *entry;
749 struct list_head *list;
750
Joerg Roedel31cff672013-04-09 16:53:58 +0200751 if (type == IVHD_SPECIAL_IOAPIC)
752 list = &ioapic_map;
753 else if (type == IVHD_SPECIAL_HPET)
754 list = &hpet_map;
755 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200756 return -EINVAL;
757
Joerg Roedel31cff672013-04-09 16:53:58 +0200758 list_for_each_entry(entry, list, list) {
759 if (!(entry->id == id && entry->cmd_line))
760 continue;
761
762 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
763 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
764
Joerg Roedelc50e3242014-09-09 15:59:37 +0200765 *devid = entry->devid;
766
Joerg Roedel31cff672013-04-09 16:53:58 +0200767 return 0;
768 }
769
Joerg Roedel6efed632012-06-14 15:52:58 +0200770 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
771 if (!entry)
772 return -ENOMEM;
773
Joerg Roedel31cff672013-04-09 16:53:58 +0200774 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +0200775 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +0200776 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +0200777
778 list_add_tail(&entry->list, list);
779
780 return 0;
781}
782
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400783static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
784 bool cmd_line)
785{
786 struct acpihid_map_entry *entry;
787 struct list_head *list = &acpihid_map;
788
789 list_for_each_entry(entry, list, list) {
790 if (strcmp(entry->hid, hid) ||
791 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
792 !entry->cmd_line)
793 continue;
794
795 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
796 hid, uid);
797 *devid = entry->devid;
798 return 0;
799 }
800
801 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
802 if (!entry)
803 return -ENOMEM;
804
805 memcpy(entry->uid, uid, strlen(uid));
806 memcpy(entry->hid, hid, strlen(hid));
807 entry->devid = *devid;
808 entry->cmd_line = cmd_line;
809 entry->root_devid = (entry->devid & (~0x7));
810
811 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
812 entry->cmd_line ? "cmd" : "ivrs",
813 entry->hid, entry->uid, entry->root_devid);
814
815 list_add_tail(&entry->list, list);
816 return 0;
817}
818
Joerg Roedel235dacb2013-04-09 17:53:14 +0200819static int __init add_early_maps(void)
820{
821 int i, ret;
822
823 for (i = 0; i < early_ioapic_map_size; ++i) {
824 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
825 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200826 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200827 early_ioapic_map[i].cmd_line);
828 if (ret)
829 return ret;
830 }
831
832 for (i = 0; i < early_hpet_map_size; ++i) {
833 ret = add_special_device(IVHD_SPECIAL_HPET,
834 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200835 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200836 early_hpet_map[i].cmd_line);
837 if (ret)
838 return ret;
839 }
840
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400841 for (i = 0; i < early_acpihid_map_size; ++i) {
842 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
843 early_acpihid_map[i].uid,
844 &early_acpihid_map[i].devid,
845 early_acpihid_map[i].cmd_line);
846 if (ret)
847 return ret;
848 }
849
Joerg Roedel235dacb2013-04-09 17:53:14 +0200850 return 0;
851}
852
Joerg Roedelb65233a2008-07-11 17:14:21 +0200853/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200854 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +0200855 * it
856 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200857static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
858{
859 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
860
861 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
862 return;
863
864 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200865 /*
866 * We only can configure exclusion ranges per IOMMU, not
867 * per device. But we can enable the exclusion range per
868 * device. This is done here
869 */
Su Friendy2c16c9f2014-05-07 13:54:52 +0800870 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +0200871 iommu->exclusion_start = m->range_start;
872 iommu->exclusion_length = m->range_length;
873 }
874}
875
Joerg Roedelb65233a2008-07-11 17:14:21 +0200876/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200877 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
878 * initializes the hardware and our data structures with it.
879 */
Joerg Roedel6efed632012-06-14 15:52:58 +0200880static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200881 struct ivhd_header *h)
882{
883 u8 *p = (u8 *)h;
884 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +0200885 u16 devid = 0, devid_start = 0, devid_to = 0;
886 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200887 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200888 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400889 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200890 int ret;
891
892
893 ret = add_early_maps();
894 if (ret)
895 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200896
897 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +0200898 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200899 */
Joerg Roedele9bf5192010-09-20 14:33:07 +0200900 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200901
902 /*
903 * Done. Now parse the device entries
904 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400905 ivhd_size = get_ivhd_header_size(h);
906 if (!ivhd_size) {
907 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
908 return -EINVAL;
909 }
910
911 p += ivhd_size;
912
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200913 end += h->length;
914
Joerg Roedel42a698f2009-05-20 15:41:28 +0200915
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200916 while (p < end) {
917 e = (struct ivhd_entry *)p;
918 switch (e->type) {
919 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200920
Joerg Roedel226e8892015-10-20 17:33:44 +0200921 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +0200922
Joerg Roedel226e8892015-10-20 17:33:44 +0200923 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
924 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200925 break;
926 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200927
928 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
929 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700930 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +0200931 PCI_SLOT(e->devid),
932 PCI_FUNC(e->devid),
933 e->flags);
934
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200935 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +0200936 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200937 break;
938 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200939
940 DUMP_printk(" DEV_SELECT_RANGE_START\t "
941 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700942 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +0200943 PCI_SLOT(e->devid),
944 PCI_FUNC(e->devid),
945 e->flags);
946
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200947 devid_start = e->devid;
948 flags = e->flags;
949 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200950 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200951 break;
952 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200953
954 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
955 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700956 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +0200957 PCI_SLOT(e->devid),
958 PCI_FUNC(e->devid),
959 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -0700960 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +0200961 PCI_SLOT(e->ext >> 8),
962 PCI_FUNC(e->ext >> 8));
963
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200964 devid = e->devid;
965 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200966 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +0100967 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200968 amd_iommu_alias_table[devid] = devid_to;
969 break;
970 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200971
972 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
973 "devid: %02x:%02x.%x flags: %02x "
974 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700975 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +0200976 PCI_SLOT(e->devid),
977 PCI_FUNC(e->devid),
978 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -0700979 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +0200980 PCI_SLOT(e->ext >> 8),
981 PCI_FUNC(e->ext >> 8));
982
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200983 devid_start = e->devid;
984 flags = e->flags;
985 devid_to = e->ext >> 8;
986 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200987 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200988 break;
989 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200990
991 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
992 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -0700993 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +0200994 PCI_SLOT(e->devid),
995 PCI_FUNC(e->devid),
996 e->flags, e->ext);
997
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200998 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +0200999 set_dev_entry_from_acpi(iommu, devid, e->flags,
1000 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001001 break;
1002 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001003
1004 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1005 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001006 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001007 PCI_SLOT(e->devid),
1008 PCI_FUNC(e->devid),
1009 e->flags, e->ext);
1010
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001011 devid_start = e->devid;
1012 flags = e->flags;
1013 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001014 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001015 break;
1016 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001017
1018 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001019 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001020 PCI_SLOT(e->devid),
1021 PCI_FUNC(e->devid));
1022
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001023 devid = e->devid;
1024 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001025 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001026 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001027 set_dev_entry_from_acpi(iommu,
1028 devid_to, flags, ext_flags);
1029 }
1030 set_dev_entry_from_acpi(iommu, dev_i,
1031 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001032 }
1033 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001034 case IVHD_DEV_SPECIAL: {
1035 u8 handle, type;
1036 const char *var;
1037 u16 devid;
1038 int ret;
1039
1040 handle = e->ext & 0xff;
1041 devid = (e->ext >> 8) & 0xffff;
1042 type = (e->ext >> 24) & 0xff;
1043
1044 if (type == IVHD_SPECIAL_IOAPIC)
1045 var = "IOAPIC";
1046 else if (type == IVHD_SPECIAL_HPET)
1047 var = "HPET";
1048 else
1049 var = "UNKNOWN";
1050
1051 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1052 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001053 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001054 PCI_SLOT(devid),
1055 PCI_FUNC(devid));
1056
Joerg Roedelc50e3242014-09-09 15:59:37 +02001057 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001058 if (ret)
1059 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001060
1061 /*
1062 * add_special_device might update the devid in case a
1063 * command-line override is present. So call
1064 * set_dev_entry_from_acpi after add_special_device.
1065 */
1066 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1067
Joerg Roedel6efed632012-06-14 15:52:58 +02001068 break;
1069 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001070 case IVHD_DEV_ACPI_HID: {
1071 u16 devid;
1072 u8 hid[ACPIHID_HID_LEN] = {0};
1073 u8 uid[ACPIHID_UID_LEN] = {0};
1074 int ret;
1075
1076 if (h->type != 0x40) {
1077 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1078 e->type);
1079 break;
1080 }
1081
1082 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1083 hid[ACPIHID_HID_LEN - 1] = '\0';
1084
1085 if (!(*hid)) {
1086 pr_err(FW_BUG "Invalid HID.\n");
1087 break;
1088 }
1089
1090 switch (e->uidf) {
1091 case UID_NOT_PRESENT:
1092
1093 if (e->uidl != 0)
1094 pr_warn(FW_BUG "Invalid UID length.\n");
1095
1096 break;
1097 case UID_IS_INTEGER:
1098
1099 sprintf(uid, "%d", e->uid);
1100
1101 break;
1102 case UID_IS_CHARACTER:
1103
1104 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1105 uid[ACPIHID_UID_LEN - 1] = '\0';
1106
1107 break;
1108 default:
1109 break;
1110 }
1111
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001112 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001113 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1114 hid, uid,
1115 PCI_BUS_NUM(devid),
1116 PCI_SLOT(devid),
1117 PCI_FUNC(devid));
1118
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001119 flags = e->flags;
1120
1121 ret = add_acpi_hid_device(hid, uid, &devid, false);
1122 if (ret)
1123 return ret;
1124
1125 /*
1126 * add_special_device might update the devid in case a
1127 * command-line override is present. So call
1128 * set_dev_entry_from_acpi after add_special_device.
1129 */
1130 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1131
1132 break;
1133 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001134 default:
1135 break;
1136 }
1137
Joerg Roedelb514e552008-09-17 17:14:27 +02001138 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001139 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001140
1141 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001142}
1143
Joerg Roedele47d4022008-06-26 21:27:48 +02001144static void __init free_iommu_one(struct amd_iommu *iommu)
1145{
1146 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001147 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001148 free_ppr_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001149 iommu_unmap_mmio_space(iommu);
1150}
1151
1152static void __init free_iommu_all(void)
1153{
1154 struct amd_iommu *iommu, *next;
1155
Joerg Roedel3bd22172009-05-04 15:06:20 +02001156 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001157 list_del(&iommu->list);
1158 free_iommu_one(iommu);
1159 kfree(iommu);
1160 }
1161}
1162
Joerg Roedelb65233a2008-07-11 17:14:21 +02001163/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001164 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1165 * Workaround:
1166 * BIOS should disable L2B micellaneous clock gating by setting
1167 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1168 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001169static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001170{
1171 u32 value;
1172
1173 if ((boot_cpu_data.x86 != 0x15) ||
1174 (boot_cpu_data.x86_model < 0x10) ||
1175 (boot_cpu_data.x86_model > 0x1f))
1176 return;
1177
1178 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1179 pci_read_config_dword(iommu->dev, 0xf4, &value);
1180
1181 if (value & BIT(2))
1182 return;
1183
1184 /* Select NB indirect register 0x90 and enable writing */
1185 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1186
1187 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1188 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1189 dev_name(&iommu->dev->dev));
1190
1191 /* Clear the enable writing bit */
1192 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1193}
1194
1195/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001196 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1197 * Workaround:
1198 * BIOS should enable ATS write permission check by setting
1199 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1200 */
1201static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1202{
1203 u32 value;
1204
1205 if ((boot_cpu_data.x86 != 0x15) ||
1206 (boot_cpu_data.x86_model < 0x30) ||
1207 (boot_cpu_data.x86_model > 0x3f))
1208 return;
1209
1210 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1211 value = iommu_read_l2(iommu, 0x47);
1212
1213 if (value & BIT(0))
1214 return;
1215
1216 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1217 iommu_write_l2(iommu, 0x47, value | BIT(0));
1218
1219 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1220 dev_name(&iommu->dev->dev));
1221}
1222
1223/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001224 * This function clues the initialization function for one IOMMU
1225 * together and also allocates the command buffer and programs the
1226 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1227 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001228static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1229{
Joerg Roedel6efed632012-06-14 15:52:58 +02001230 int ret;
1231
Joerg Roedele47d4022008-06-26 21:27:48 +02001232 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001233
1234 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001235 list_add_tail(&iommu->list, &amd_iommu_list);
Joerg Roedelbb527772009-11-20 14:31:51 +01001236 iommu->index = amd_iommus_present++;
1237
1238 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1239 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1240 return -ENOSYS;
1241 }
1242
1243 /* Index is fine - add IOMMU to the array */
1244 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001245
1246 /*
1247 * Copy data from ACPI table entry to the iommu struct
1248 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001249 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001250 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001251 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001252 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001253
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001254 switch (h->type) {
1255 case 0x10:
1256 /* Check if IVHD EFR contains proper max banks/counters */
1257 if ((h->efr_attr != 0) &&
1258 ((h->efr_attr & (0xF << 13)) != 0) &&
1259 ((h->efr_attr & (0x3F << 17)) != 0))
1260 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1261 else
1262 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001263 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1264 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001265 break;
1266 case 0x11:
1267 case 0x40:
1268 if (h->efr_reg & (1 << 9))
1269 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1270 else
1271 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001272 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1273 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001274 break;
1275 default:
1276 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001277 }
1278
1279 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1280 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001281 if (!iommu->mmio_base)
1282 return -ENOMEM;
1283
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001284 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001285 return -ENOMEM;
1286
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001287 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001288 return -ENOMEM;
1289
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001290 iommu->int_enabled = false;
1291
Joerg Roedel6efed632012-06-14 15:52:58 +02001292 ret = init_iommu_from_acpi(iommu, h);
1293 if (ret)
1294 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001295
Jiang Liu7c71d302015-04-13 14:11:33 +08001296 ret = amd_iommu_create_irq_domain(iommu);
1297 if (ret)
1298 return ret;
1299
Joerg Roedelf6fec002012-06-21 16:51:25 +02001300 /*
1301 * Make sure IOMMU is not considered to translate itself. The IVRS
1302 * table tells us so, but this is a lie!
1303 */
1304 amd_iommu_rlookup_table[iommu->devid] = NULL;
1305
Joerg Roedel23c742d2012-06-12 11:47:34 +02001306 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001307}
1308
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001309/**
1310 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1311 * @ivrs Pointer to the IVRS header
1312 *
1313 * This function search through all IVDB of the maximum supported IVHD
1314 */
1315static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1316{
1317 u8 *base = (u8 *)ivrs;
1318 struct ivhd_header *ivhd = (struct ivhd_header *)
1319 (base + IVRS_HEADER_LENGTH);
1320 u8 last_type = ivhd->type;
1321 u16 devid = ivhd->devid;
1322
1323 while (((u8 *)ivhd - base < ivrs->length) &&
1324 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1325 u8 *p = (u8 *) ivhd;
1326
1327 if (ivhd->devid == devid)
1328 last_type = ivhd->type;
1329 ivhd = (struct ivhd_header *)(p + ivhd->length);
1330 }
1331
1332 return last_type;
1333}
1334
Joerg Roedelb65233a2008-07-11 17:14:21 +02001335/*
1336 * Iterates over all IOMMU entries in the ACPI table, allocates the
1337 * IOMMU structure and initializes it with init_iommu_one()
1338 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001339static int __init init_iommu_all(struct acpi_table_header *table)
1340{
1341 u8 *p = (u8 *)table, *end = (u8 *)table;
1342 struct ivhd_header *h;
1343 struct amd_iommu *iommu;
1344 int ret;
1345
Joerg Roedele47d4022008-06-26 21:27:48 +02001346 end += table->length;
1347 p += IVRS_HEADER_LENGTH;
1348
1349 while (p < end) {
1350 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001351 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001352
Joerg Roedelae908c22009-09-01 16:52:16 +02001353 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001354 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001355 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001356 PCI_FUNC(h->devid), h->cap_ptr,
1357 h->pci_seg, h->flags, h->info);
1358 DUMP_printk(" mmio-addr: %016llx\n",
1359 h->mmio_phys);
1360
Joerg Roedele47d4022008-06-26 21:27:48 +02001361 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001362 if (iommu == NULL)
1363 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001364
Joerg Roedele47d4022008-06-26 21:27:48 +02001365 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001366 if (ret)
1367 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001368 }
1369 p += h->length;
1370
1371 }
1372 WARN_ON(p != end);
1373
1374 return 0;
1375}
1376
Steven L Kinney30861dd2013-06-05 16:11:48 -05001377
1378static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1379{
1380 u64 val = 0xabcd, val2 = 0;
1381
1382 if (!iommu_feature(iommu, FEATURE_PC))
1383 return;
1384
1385 amd_iommu_pc_present = true;
1386
1387 /* Check if the performance counters can be written to */
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01001388 if ((0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val, true)) ||
1389 (0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001390 (val != val2)) {
1391 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1392 amd_iommu_pc_present = false;
1393 return;
1394 }
1395
1396 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1397
1398 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1399 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1400 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1401}
1402
Alex Williamson066f2e92014-06-12 16:12:37 -06001403static ssize_t amd_iommu_show_cap(struct device *dev,
1404 struct device_attribute *attr,
1405 char *buf)
1406{
1407 struct amd_iommu *iommu = dev_get_drvdata(dev);
1408 return sprintf(buf, "%x\n", iommu->cap);
1409}
1410static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1411
1412static ssize_t amd_iommu_show_features(struct device *dev,
1413 struct device_attribute *attr,
1414 char *buf)
1415{
1416 struct amd_iommu *iommu = dev_get_drvdata(dev);
1417 return sprintf(buf, "%llx\n", iommu->features);
1418}
1419static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1420
1421static struct attribute *amd_iommu_attrs[] = {
1422 &dev_attr_cap.attr,
1423 &dev_attr_features.attr,
1424 NULL,
1425};
1426
1427static struct attribute_group amd_iommu_group = {
1428 .name = "amd-iommu",
1429 .attrs = amd_iommu_attrs,
1430};
1431
1432static const struct attribute_group *amd_iommu_groups[] = {
1433 &amd_iommu_group,
1434 NULL,
1435};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001436
Joerg Roedel23c742d2012-06-12 11:47:34 +02001437static int iommu_init_pci(struct amd_iommu *iommu)
1438{
1439 int cap_ptr = iommu->cap_ptr;
1440 u32 range, misc, low, high;
1441
Shuah Khanc5081cd2013-02-27 17:07:19 -07001442 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001443 iommu->devid & 0xff);
1444 if (!iommu->dev)
1445 return -ENODEV;
1446
Jiang Liucbbc00b2015-10-09 22:07:31 +08001447 /* Prevent binding other PCI device drivers to IOMMU devices */
1448 iommu->dev->match_driver = false;
1449
Joerg Roedel23c742d2012-06-12 11:47:34 +02001450 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1451 &iommu->cap);
1452 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1453 &range);
1454 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1455 &misc);
1456
Joerg Roedel23c742d2012-06-12 11:47:34 +02001457 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1458 amd_iommu_iotlb_sup = false;
1459
1460 /* read extended feature bits */
1461 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1462 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1463
1464 iommu->features = ((u64)high << 32) | low;
1465
1466 if (iommu_feature(iommu, FEATURE_GT)) {
1467 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001468 u32 max_pasid;
1469 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001470
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001471 pasmax = iommu->features & FEATURE_PASID_MASK;
1472 pasmax >>= FEATURE_PASID_SHIFT;
1473 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001474
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001475 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1476
1477 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001478
1479 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1480 glxval >>= FEATURE_GLXVAL_SHIFT;
1481
1482 if (amd_iommu_max_glx_val == -1)
1483 amd_iommu_max_glx_val = glxval;
1484 else
1485 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1486 }
1487
1488 if (iommu_feature(iommu, FEATURE_GT) &&
1489 iommu_feature(iommu, FEATURE_PPR)) {
1490 iommu->is_iommu_v2 = true;
1491 amd_iommu_v2_present = true;
1492 }
1493
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001494 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1495 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001496
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001497 /* Note: We have already checked GASup from IVRS table.
1498 * Now, we need to make sure that GAMSup is set.
1499 */
1500 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
1501 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
1502 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
1503
1504
Joerg Roedel23c742d2012-06-12 11:47:34 +02001505 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1506 amd_iommu_np_cache = true;
1507
Steven L Kinney30861dd2013-06-05 16:11:48 -05001508 init_iommu_perf_ctr(iommu);
1509
Joerg Roedel23c742d2012-06-12 11:47:34 +02001510 if (is_rd890_iommu(iommu->dev)) {
1511 int i, j;
1512
1513 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1514 PCI_DEVFN(0, 0));
1515
1516 /*
1517 * Some rd890 systems may not be fully reconfigured by the
1518 * BIOS, so it's necessary for us to store this information so
1519 * it can be reprogrammed on resume
1520 */
1521 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1522 &iommu->stored_addr_lo);
1523 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1524 &iommu->stored_addr_hi);
1525
1526 /* Low bit locks writes to configuration space */
1527 iommu->stored_addr_lo &= ~1;
1528
1529 for (i = 0; i < 6; i++)
1530 for (j = 0; j < 0x12; j++)
1531 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1532
1533 for (i = 0; i < 0x83; i++)
1534 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1535 }
1536
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001537 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001538 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001539
Alex Williamson066f2e92014-06-12 16:12:37 -06001540 iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu,
1541 amd_iommu_groups, "ivhd%d",
1542 iommu->index);
1543
Joerg Roedel23c742d2012-06-12 11:47:34 +02001544 return pci_enable_device(iommu->dev);
1545}
1546
Joerg Roedel4d121c32012-06-14 12:21:55 +02001547static void print_iommu_info(void)
1548{
1549 static const char * const feat_str[] = {
1550 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1551 "IA", "GA", "HE", "PC"
1552 };
1553 struct amd_iommu *iommu;
1554
1555 for_each_iommu(iommu) {
1556 int i;
1557
1558 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1559 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1560
1561 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001562 pr_info("AMD-Vi: Extended features (%#llx):\n",
1563 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001564 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001565 if (iommu_feature(iommu, (1ULL << i)))
1566 pr_cont(" %s", feat_str[i]);
1567 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001568
1569 if (iommu->features & FEATURE_GAM_VAPIC)
1570 pr_cont(" GA_vAPIC");
1571
Steven L Kinney30861dd2013-06-05 16:11:48 -05001572 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001573 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001574 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001575 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001576 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001577 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1578 pr_info("AMD-Vi: virtual APIC enabled\n");
1579 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001580}
1581
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001582static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001583{
1584 struct amd_iommu *iommu;
1585 int ret = 0;
1586
1587 for_each_iommu(iommu) {
1588 ret = iommu_init_pci(iommu);
1589 if (ret)
1590 break;
1591 }
1592
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001593 /*
1594 * Order is important here to make sure any unity map requirements are
1595 * fulfilled. The unity mappings are created and written to the device
1596 * table during the amd_iommu_init_api() call.
1597 *
1598 * After that we call init_device_table_dma() to make sure any
1599 * uninitialized DTE will block DMA, and in the end we flush the caches
1600 * of all IOMMUs to make sure the changes to the device table are
1601 * active.
1602 */
1603 ret = amd_iommu_init_api();
1604
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001605 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001606
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001607 for_each_iommu(iommu)
1608 iommu_flush_all_caches(iommu);
1609
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001610 if (!ret)
1611 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001612
Joerg Roedel23c742d2012-06-12 11:47:34 +02001613 return ret;
1614}
1615
Joerg Roedelb65233a2008-07-11 17:14:21 +02001616/****************************************************************************
1617 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001618 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001619 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001620 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1621 * pci_dev.
1622 *
1623 ****************************************************************************/
1624
Joerg Roedel9f800de2009-11-23 12:45:25 +01001625static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001626{
1627 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001628
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001629 r = pci_enable_msi(iommu->dev);
1630 if (r)
1631 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001632
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001633 r = request_threaded_irq(iommu->dev->irq,
1634 amd_iommu_int_handler,
1635 amd_iommu_int_thread,
1636 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001637 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001638
1639 if (r) {
1640 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001641 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001642 }
1643
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001644 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001645
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001646 return 0;
1647}
1648
Joerg Roedel05f92db2009-05-12 09:52:46 +02001649static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001650{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001651 int ret;
1652
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001653 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001654 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001655
Yijing Wang82fcfc62013-08-08 21:12:36 +08001656 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001657 ret = iommu_setup_msi(iommu);
1658 else
1659 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001660
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001661 if (ret)
1662 return ret;
1663
1664enable_faults:
1665 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1666
1667 if (iommu->ppr_log != NULL)
1668 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1669
1670 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001671}
1672
1673/****************************************************************************
1674 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001675 * The next functions belong to the third pass of parsing the ACPI
1676 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001677 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001678 *
1679 ****************************************************************************/
1680
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001681static void __init free_unity_maps(void)
1682{
1683 struct unity_map_entry *entry, *next;
1684
1685 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1686 list_del(&entry->list);
1687 kfree(entry);
1688 }
1689}
1690
Joerg Roedelb65233a2008-07-11 17:14:21 +02001691/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001692static int __init init_exclusion_range(struct ivmd_header *m)
1693{
1694 int i;
1695
1696 switch (m->type) {
1697 case ACPI_IVMD_TYPE:
1698 set_device_exclusion_range(m->devid, m);
1699 break;
1700 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001701 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001702 set_device_exclusion_range(i, m);
1703 break;
1704 case ACPI_IVMD_TYPE_RANGE:
1705 for (i = m->devid; i <= m->aux; ++i)
1706 set_device_exclusion_range(i, m);
1707 break;
1708 default:
1709 break;
1710 }
1711
1712 return 0;
1713}
1714
Joerg Roedelb65233a2008-07-11 17:14:21 +02001715/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001716static int __init init_unity_map_range(struct ivmd_header *m)
1717{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001718 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001719 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001720
1721 e = kzalloc(sizeof(*e), GFP_KERNEL);
1722 if (e == NULL)
1723 return -ENOMEM;
1724
1725 switch (m->type) {
1726 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001727 kfree(e);
1728 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001729 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001730 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001731 e->devid_start = e->devid_end = m->devid;
1732 break;
1733 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001734 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001735 e->devid_start = 0;
1736 e->devid_end = amd_iommu_last_bdf;
1737 break;
1738 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001739 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001740 e->devid_start = m->devid;
1741 e->devid_end = m->aux;
1742 break;
1743 }
1744 e->address_start = PAGE_ALIGN(m->range_start);
1745 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1746 e->prot = m->flags >> 1;
1747
Joerg Roedel02acc432009-05-20 16:24:21 +02001748 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1749 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001750 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1751 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02001752 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1753 e->address_start, e->address_end, m->flags);
1754
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001755 list_add_tail(&e->list, &amd_iommu_unity_map);
1756
1757 return 0;
1758}
1759
Joerg Roedelb65233a2008-07-11 17:14:21 +02001760/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001761static int __init init_memory_definitions(struct acpi_table_header *table)
1762{
1763 u8 *p = (u8 *)table, *end = (u8 *)table;
1764 struct ivmd_header *m;
1765
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001766 end += table->length;
1767 p += IVRS_HEADER_LENGTH;
1768
1769 while (p < end) {
1770 m = (struct ivmd_header *)p;
1771 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1772 init_exclusion_range(m);
1773 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1774 init_unity_map_range(m);
1775
1776 p += m->length;
1777 }
1778
1779 return 0;
1780}
1781
Joerg Roedelb65233a2008-07-11 17:14:21 +02001782/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001783 * Init the device table to not allow DMA access for devices and
1784 * suppress all page faults
1785 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02001786static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001787{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001788 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001789
1790 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1791 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1792 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001793 }
1794}
1795
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001796static void __init uninit_device_table_dma(void)
1797{
1798 u32 devid;
1799
1800 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1801 amd_iommu_dev_table[devid].data[0] = 0ULL;
1802 amd_iommu_dev_table[devid].data[1] = 0ULL;
1803 }
1804}
1805
Joerg Roedel33f28c52012-06-15 18:03:31 +02001806static void init_device_table(void)
1807{
1808 u32 devid;
1809
1810 if (!amd_iommu_irq_remap)
1811 return;
1812
1813 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1814 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1815}
1816
Joerg Roedele9bf5192010-09-20 14:33:07 +02001817static void iommu_init_flags(struct amd_iommu *iommu)
1818{
1819 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1820 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1821 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1822
1823 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1824 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1825 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1826
1827 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1828 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1829 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1830
1831 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1832 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1833 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1834
1835 /*
1836 * make IOMMU memory accesses cache coherent
1837 */
1838 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001839
1840 /* Set IOTLB invalidation timeout to 1s */
1841 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001842}
1843
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001844static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001845{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001846 int i, j;
1847 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001848 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001849
1850 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001851 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001852 return;
1853
1854 /*
1855 * First, we need to ensure that the iommu is enabled. This is
1856 * controlled by a register in the northbridge
1857 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001858
1859 /* Select Northbridge indirect register 0x75 and enable writing */
1860 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1861 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1862
1863 /* Enable the iommu */
1864 if (!(ioc_feature_control & 0x1))
1865 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1866
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001867 /* Restore the iommu BAR */
1868 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1869 iommu->stored_addr_lo);
1870 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1871 iommu->stored_addr_hi);
1872
1873 /* Restore the l1 indirect regs for each of the 6 l1s */
1874 for (i = 0; i < 6; i++)
1875 for (j = 0; j < 0x12; j++)
1876 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1877
1878 /* Restore the l2 indirect regs */
1879 for (i = 0; i < 0x83; i++)
1880 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
1881
1882 /* Lock PCI setup registers */
1883 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1884 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02001885}
1886
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001887static void iommu_enable_ga(struct amd_iommu *iommu)
1888{
1889#ifdef CONFIG_IRQ_REMAP
1890 switch (amd_iommu_guest_ir) {
1891 case AMD_IOMMU_GUEST_IR_VAPIC:
1892 iommu_feature_enable(iommu, CONTROL_GAM_EN);
1893 /* Fall through */
1894 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
1895 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05001896 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001897 break;
1898 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05001899 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001900 break;
1901 }
1902#endif
1903}
1904
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001905/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001906 * This function finally enables all IOMMUs found in the system after
1907 * they have been initialized
1908 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02001909static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02001910{
1911 struct amd_iommu *iommu;
1912
Joerg Roedel3bd22172009-05-04 15:06:20 +02001913 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02001914 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001915 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02001916 iommu_set_device_table(iommu);
1917 iommu_enable_command_buffer(iommu);
1918 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001919 iommu_set_exclusion_range(iommu);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001920 iommu_enable_ga(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001921 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001922 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001923 }
1924}
1925
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02001926static void enable_iommus_v2(void)
1927{
1928 struct amd_iommu *iommu;
1929
1930 for_each_iommu(iommu) {
1931 iommu_enable_ppr_log(iommu);
1932 iommu_enable_gt(iommu);
1933 }
1934}
1935
1936static void enable_iommus(void)
1937{
1938 early_enable_iommus();
1939
1940 enable_iommus_v2();
1941}
1942
Joerg Roedel92ac4322009-05-19 19:06:27 +02001943static void disable_iommus(void)
1944{
1945 struct amd_iommu *iommu;
1946
1947 for_each_iommu(iommu)
1948 iommu_disable(iommu);
1949}
1950
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001951/*
1952 * Suspend/Resume support
1953 * disable suspend until real resume implemented
1954 */
1955
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001956static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001957{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001958 struct amd_iommu *iommu;
1959
1960 for_each_iommu(iommu)
1961 iommu_apply_resume_quirks(iommu);
1962
Joerg Roedel736501e2009-05-12 09:56:12 +02001963 /* re-load the hardware */
1964 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01001965
1966 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001967}
1968
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001969static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001970{
Joerg Roedel736501e2009-05-12 09:56:12 +02001971 /* disable IOMMUs to go out of the way for BIOS */
1972 disable_iommus();
1973
1974 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001975}
1976
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001977static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001978 .suspend = amd_iommu_suspend,
1979 .resume = amd_iommu_resume,
1980};
1981
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001982static void __init free_on_init_error(void)
1983{
Joerg Roedel0ea2c422012-06-15 18:05:20 +02001984 free_pages((unsigned long)irq_lookup_table,
1985 get_order(rlookup_table_size));
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001986
Julia Lawalla5919892015-09-13 14:15:31 +02001987 kmem_cache_destroy(amd_iommu_irq_cache);
1988 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001989
1990 free_pages((unsigned long)amd_iommu_rlookup_table,
1991 get_order(rlookup_table_size));
1992
1993 free_pages((unsigned long)amd_iommu_alias_table,
1994 get_order(alias_table_size));
1995
1996 free_pages((unsigned long)amd_iommu_dev_table,
1997 get_order(dev_table_size));
1998
1999 free_iommu_all();
2000
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002001#ifdef CONFIG_GART_IOMMU
2002 /*
2003 * We failed to initialize the AMD IOMMU - try fallback to GART
2004 * if possible.
2005 */
2006 gart_iommu_init();
2007
2008#endif
2009}
2010
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002011/* SB IOAPIC is always on this device in AMD systems */
2012#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2013
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002014static bool __init check_ioapic_information(void)
2015{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002016 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002017 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002018 int idx;
2019
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002020 has_sb_ioapic = false;
2021 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002022
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002023 /*
2024 * If we have map overrides on the kernel command line the
2025 * messages in this function might not describe firmware bugs
2026 * anymore - so be careful
2027 */
2028 if (cmdline_maps)
2029 fw_bug = "";
2030
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002031 for (idx = 0; idx < nr_ioapics; idx++) {
2032 int devid, id = mpc_ioapic_id(idx);
2033
2034 devid = get_ioapic_devid(id);
2035 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002036 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2037 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002038 ret = false;
2039 } else if (devid == IOAPIC_SB_DEVID) {
2040 has_sb_ioapic = true;
2041 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002042 }
2043 }
2044
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002045 if (!has_sb_ioapic) {
2046 /*
2047 * We expect the SB IOAPIC to be listed in the IVRS
2048 * table. The system timer is connected to the SB IOAPIC
2049 * and if we don't have it in the list the system will
2050 * panic at boot time. This situation usually happens
2051 * when the BIOS is buggy and provides us the wrong
2052 * device id for the IOAPIC in the system.
2053 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002054 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002055 }
2056
2057 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002058 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002059
2060 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002061}
2062
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002063static void __init free_dma_resources(void)
2064{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002065 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2066 get_order(MAX_DOMAIN_ID/8));
2067
2068 free_unity_maps();
2069}
2070
Joerg Roedelb65233a2008-07-11 17:14:21 +02002071/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002072 * This is the hardware init function for AMD IOMMU in the system.
2073 * This function is called either from amd_iommu_init or from the interrupt
2074 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002075 *
2076 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002077 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002078 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002079 * 1 pass) Discover the most comprehensive IVHD type to use.
2080 *
2081 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002082 * Upon this information the size of the data structures is
2083 * determined that needs to be allocated.
2084 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002085 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002086 * information in the ACPI table about available AMD IOMMUs
2087 * in the system. It also maps the PCI devices in the
2088 * system to specific IOMMUs
2089 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002090 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002091 * initialized we update them with information about memory
2092 * remapping requirements parsed out of the ACPI table in
2093 * this last pass.
2094 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002095 * After everything is set up the IOMMUs are enabled and the necessary
2096 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002097 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002098static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002099{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002100 struct acpi_table_header *ivrs_base;
2101 acpi_size ivrs_size;
2102 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002103 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002104
Joerg Roedel643511b2012-06-12 12:09:35 +02002105 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002106 return -ENODEV;
2107
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002108 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
2109 if (status == AE_NOT_FOUND)
2110 return -ENODEV;
2111 else if (ACPI_FAILURE(status)) {
2112 const char *err = acpi_format_exception(status);
2113 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2114 return -EINVAL;
2115 }
2116
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002117 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002118 * Validate checksum here so we don't need to do it when
2119 * we actually parse the table
2120 */
2121 ret = check_ivrs_checksum(ivrs_base);
2122 if (ret)
2123 return ret;
2124
2125 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2126 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2127
2128 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002129 * First parse ACPI tables to find the largest Bus/Dev/Func
2130 * we need to handle. Upon this information the shared data
2131 * structures for the IOMMUs in the system will be allocated
2132 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002133 ret = find_last_devid_acpi(ivrs_base);
2134 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002135 goto out;
2136
Joerg Roedelc5714842008-07-11 17:14:25 +02002137 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2138 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2139 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002140
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002141 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002142 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002143 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002144 get_order(dev_table_size));
2145 if (amd_iommu_dev_table == NULL)
2146 goto out;
2147
2148 /*
2149 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2150 * IOMMU see for that device
2151 */
2152 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2153 get_order(alias_table_size));
2154 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002155 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002156
2157 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002158 amd_iommu_rlookup_table = (void *)__get_free_pages(
2159 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002160 get_order(rlookup_table_size));
2161 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002162 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002163
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002164 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2165 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002166 get_order(MAX_DOMAIN_ID/8));
2167 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002168 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002169
2170 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002171 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002172 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002173 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002174 amd_iommu_alias_table[i] = i;
2175
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002176 /*
2177 * never allocate domain 0 because its used as the non-allocated and
2178 * error value placeholder
2179 */
2180 amd_iommu_pd_alloc_bitmap[0] = 1;
2181
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002182 spin_lock_init(&amd_iommu_pd_lock);
2183
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002184 /*
2185 * now the data structures are allocated and basically initialized
2186 * start the real acpi table scan
2187 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002188 ret = init_iommu_all(ivrs_base);
2189 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002190 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002191
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002192 if (amd_iommu_irq_remap)
2193 amd_iommu_irq_remap = check_ioapic_information();
2194
Joerg Roedel05152a02012-06-15 16:53:51 +02002195 if (amd_iommu_irq_remap) {
2196 /*
2197 * Interrupt remapping enabled, create kmem_cache for the
2198 * remapping tables.
2199 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002200 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002201 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2202 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2203 else
2204 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002205 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002206 remap_cache_sz,
2207 IRQ_TABLE_ALIGNMENT,
2208 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002209 if (!amd_iommu_irq_cache)
2210 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002211
2212 irq_lookup_table = (void *)__get_free_pages(
2213 GFP_KERNEL | __GFP_ZERO,
2214 get_order(rlookup_table_size));
2215 if (!irq_lookup_table)
2216 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002217 }
2218
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002219 ret = init_memory_definitions(ivrs_base);
2220 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002221 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002222
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002223 /* init the device table */
2224 init_device_table();
2225
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002226out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002227 /* Don't leak any ACPI memory */
2228 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
2229 ivrs_base = NULL;
2230
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002231 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002232}
2233
Gerard Snitselaarae295142012-03-16 11:38:22 -07002234static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002235{
2236 struct amd_iommu *iommu;
2237 int ret = 0;
2238
2239 for_each_iommu(iommu) {
2240 ret = iommu_init_msi(iommu);
2241 if (ret)
2242 goto out;
2243 }
2244
2245out:
2246 return ret;
2247}
2248
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002249static bool detect_ivrs(void)
2250{
2251 struct acpi_table_header *ivrs_base;
2252 acpi_size ivrs_size;
2253 acpi_status status;
2254
2255 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
2256 if (status == AE_NOT_FOUND)
2257 return false;
2258 else if (ACPI_FAILURE(status)) {
2259 const char *err = acpi_format_exception(status);
2260 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2261 return false;
2262 }
2263
2264 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
2265
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002266 /* Make sure ACS will be enabled during PCI probe */
2267 pci_request_acs();
2268
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002269 return true;
2270}
2271
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002272/****************************************************************************
2273 *
2274 * AMD IOMMU Initialization State Machine
2275 *
2276 ****************************************************************************/
2277
2278static int __init state_next(void)
2279{
2280 int ret = 0;
2281
2282 switch (init_state) {
2283 case IOMMU_START_STATE:
2284 if (!detect_ivrs()) {
2285 init_state = IOMMU_NOT_FOUND;
2286 ret = -ENODEV;
2287 } else {
2288 init_state = IOMMU_IVRS_DETECTED;
2289 }
2290 break;
2291 case IOMMU_IVRS_DETECTED:
2292 ret = early_amd_iommu_init();
2293 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2294 break;
2295 case IOMMU_ACPI_FINISHED:
2296 early_enable_iommus();
2297 register_syscore_ops(&amd_iommu_syscore_ops);
2298 x86_platform.iommu_shutdown = disable_iommus;
2299 init_state = IOMMU_ENABLED;
2300 break;
2301 case IOMMU_ENABLED:
2302 ret = amd_iommu_init_pci();
2303 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2304 enable_iommus_v2();
2305 break;
2306 case IOMMU_PCI_INIT:
2307 ret = amd_iommu_enable_interrupts();
2308 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2309 break;
2310 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002311 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002312 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2313 break;
2314 case IOMMU_DMA_OPS:
2315 init_state = IOMMU_INITIALIZED;
2316 break;
2317 case IOMMU_INITIALIZED:
2318 /* Nothing to do */
2319 break;
2320 case IOMMU_NOT_FOUND:
2321 case IOMMU_INIT_ERROR:
2322 /* Error states => do nothing */
2323 ret = -EINVAL;
2324 break;
2325 default:
2326 /* Unknown state */
2327 BUG();
2328 }
2329
2330 return ret;
2331}
2332
2333static int __init iommu_go_to_state(enum iommu_init_state state)
2334{
2335 int ret = 0;
2336
2337 while (init_state != state) {
2338 ret = state_next();
2339 if (init_state == IOMMU_NOT_FOUND ||
2340 init_state == IOMMU_INIT_ERROR)
2341 break;
2342 }
2343
2344 return ret;
2345}
2346
Joerg Roedel6b474b82012-06-26 16:46:04 +02002347#ifdef CONFIG_IRQ_REMAP
2348int __init amd_iommu_prepare(void)
2349{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002350 int ret;
2351
Jiang Liu7fa1c842015-01-07 15:31:42 +08002352 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002353
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002354 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2355 if (ret)
2356 return ret;
2357 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002358}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002359
Joerg Roedel6b474b82012-06-26 16:46:04 +02002360int __init amd_iommu_enable(void)
2361{
2362 int ret;
2363
2364 ret = iommu_go_to_state(IOMMU_ENABLED);
2365 if (ret)
2366 return ret;
2367
2368 irq_remapping_enabled = 1;
2369
2370 return 0;
2371}
2372
2373void amd_iommu_disable(void)
2374{
2375 amd_iommu_suspend();
2376}
2377
2378int amd_iommu_reenable(int mode)
2379{
2380 amd_iommu_resume();
2381
2382 return 0;
2383}
2384
2385int __init amd_iommu_enable_faulting(void)
2386{
2387 /* We enable MSI later when PCI is initialized */
2388 return 0;
2389}
2390#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002391
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002392/*
2393 * This is the core init function for AMD IOMMU hardware in the system.
2394 * This function is called from the generic x86 DMA layer initialization
2395 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002396 */
2397static int __init amd_iommu_init(void)
2398{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002399 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002400
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002401 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2402 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002403 free_dma_resources();
2404 if (!irq_remapping_enabled) {
2405 disable_iommus();
2406 free_on_init_error();
2407 } else {
2408 struct amd_iommu *iommu;
2409
2410 uninit_device_table_dma();
2411 for_each_iommu(iommu)
2412 iommu_flush_all_caches(iommu);
2413 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002414 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002415
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002416 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002417}
2418
Joerg Roedelb65233a2008-07-11 17:14:21 +02002419/****************************************************************************
2420 *
2421 * Early detect code. This code runs at IOMMU detection time in the DMA
2422 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2423 * IOMMUs
2424 *
2425 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002426int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002427{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002428 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002429
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002430 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002431 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002432
Joerg Roedela5235722010-05-11 17:12:33 +02002433 if (amd_iommu_disabled)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002434 return -ENODEV;
Joerg Roedela5235722010-05-11 17:12:33 +02002435
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002436 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2437 if (ret)
2438 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002439
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002440 amd_iommu_detected = true;
2441 iommu_detected = 1;
2442 x86_init.iommu.iommu_init = amd_iommu_init;
2443
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002444 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002445}
2446
Joerg Roedelb65233a2008-07-11 17:14:21 +02002447/****************************************************************************
2448 *
2449 * Parsing functions for the AMD IOMMU specific kernel command line
2450 * options.
2451 *
2452 ****************************************************************************/
2453
Joerg Roedelfefda112009-05-20 12:21:42 +02002454static int __init parse_amd_iommu_dump(char *str)
2455{
2456 amd_iommu_dump = true;
2457
2458 return 1;
2459}
2460
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002461static int __init parse_amd_iommu_intr(char *str)
2462{
2463 for (; *str; ++str) {
2464 if (strncmp(str, "legacy", 6) == 0) {
2465 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2466 break;
2467 }
2468 if (strncmp(str, "vapic", 5) == 0) {
2469 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2470 break;
2471 }
2472 }
2473 return 1;
2474}
2475
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002476static int __init parse_amd_iommu_options(char *str)
2477{
2478 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002479 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002480 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002481 if (strncmp(str, "off", 3) == 0)
2482 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002483 if (strncmp(str, "force_isolation", 15) == 0)
2484 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002485 }
2486
2487 return 1;
2488}
2489
Joerg Roedel440e89982013-04-09 16:35:28 +02002490static int __init parse_ivrs_ioapic(char *str)
2491{
2492 unsigned int bus, dev, fn;
2493 int ret, id, i;
2494 u16 devid;
2495
2496 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2497
2498 if (ret != 4) {
2499 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2500 return 1;
2501 }
2502
2503 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2504 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2505 str);
2506 return 1;
2507 }
2508
2509 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2510
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002511 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002512 i = early_ioapic_map_size++;
2513 early_ioapic_map[i].id = id;
2514 early_ioapic_map[i].devid = devid;
2515 early_ioapic_map[i].cmd_line = true;
2516
2517 return 1;
2518}
2519
2520static int __init parse_ivrs_hpet(char *str)
2521{
2522 unsigned int bus, dev, fn;
2523 int ret, id, i;
2524 u16 devid;
2525
2526 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2527
2528 if (ret != 4) {
2529 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2530 return 1;
2531 }
2532
2533 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2534 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2535 str);
2536 return 1;
2537 }
2538
2539 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2540
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002541 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002542 i = early_hpet_map_size++;
2543 early_hpet_map[i].id = id;
2544 early_hpet_map[i].devid = devid;
2545 early_hpet_map[i].cmd_line = true;
2546
2547 return 1;
2548}
2549
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002550static int __init parse_ivrs_acpihid(char *str)
2551{
2552 u32 bus, dev, fn;
2553 char *hid, *uid, *p;
2554 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2555 int ret, i;
2556
2557 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2558 if (ret != 4) {
2559 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2560 return 1;
2561 }
2562
2563 p = acpiid;
2564 hid = strsep(&p, ":");
2565 uid = p;
2566
2567 if (!hid || !(*hid) || !uid) {
2568 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2569 return 1;
2570 }
2571
2572 i = early_acpihid_map_size++;
2573 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2574 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2575 early_acpihid_map[i].devid =
2576 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2577 early_acpihid_map[i].cmd_line = true;
2578
2579 return 1;
2580}
2581
Joerg Roedel440e89982013-04-09 16:35:28 +02002582__setup("amd_iommu_dump", parse_amd_iommu_dump);
2583__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002584__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002585__setup("ivrs_ioapic", parse_ivrs_ioapic);
2586__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002587__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002588
2589IOMMU_INIT_FINISH(amd_iommu_detect,
2590 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002591 NULL,
2592 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002593
2594bool amd_iommu_v2_supported(void)
2595{
2596 return amd_iommu_v2_present;
2597}
2598EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002599
2600/****************************************************************************
2601 *
2602 * IOMMU EFR Performance Counter support functionality. This code allows
2603 * access to the IOMMU PC functionality.
2604 *
2605 ****************************************************************************/
2606
2607u8 amd_iommu_pc_get_max_banks(u16 devid)
2608{
2609 struct amd_iommu *iommu;
2610 u8 ret = 0;
2611
2612 /* locate the iommu governing the devid */
2613 iommu = amd_iommu_rlookup_table[devid];
2614 if (iommu)
2615 ret = iommu->max_banks;
2616
2617 return ret;
2618}
2619EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2620
2621bool amd_iommu_pc_supported(void)
2622{
2623 return amd_iommu_pc_present;
2624}
2625EXPORT_SYMBOL(amd_iommu_pc_supported);
2626
2627u8 amd_iommu_pc_get_max_counters(u16 devid)
2628{
2629 struct amd_iommu *iommu;
2630 u8 ret = 0;
2631
2632 /* locate the iommu governing the devid */
2633 iommu = amd_iommu_rlookup_table[devid];
2634 if (iommu)
2635 ret = iommu->max_counters;
2636
2637 return ret;
2638}
2639EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2640
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002641static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu,
2642 u8 bank, u8 cntr, u8 fxn,
Steven L Kinney30861dd2013-06-05 16:11:48 -05002643 u64 *value, bool is_write)
2644{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002645 u32 offset;
2646 u32 max_offset_lim;
2647
Steven L Kinney30861dd2013-06-05 16:11:48 -05002648 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002649 if (WARN_ON((fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002650 return -ENODEV;
2651
2652 offset = (u32)(((0x40|bank) << 12) | (cntr << 8) | fxn);
2653
2654 /* Limit the offset to the hw defined mmio region aperture */
2655 max_offset_lim = (u32)(((0x40|iommu->max_banks) << 12) |
2656 (iommu->max_counters << 8) | 0x28);
2657 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2658 (offset > max_offset_lim))
2659 return -EINVAL;
2660
2661 if (is_write) {
2662 writel((u32)*value, iommu->mmio_base + offset);
2663 writel((*value >> 32), iommu->mmio_base + offset + 4);
2664 } else {
2665 *value = readl(iommu->mmio_base + offset + 4);
2666 *value <<= 32;
2667 *value = readl(iommu->mmio_base + offset);
2668 }
2669
2670 return 0;
2671}
2672EXPORT_SYMBOL(amd_iommu_pc_get_set_reg_val);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002673
2674int amd_iommu_pc_get_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn,
2675 u64 *value, bool is_write)
2676{
2677 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
2678
2679 /* Make sure the IOMMU PC resource is available */
2680 if (!amd_iommu_pc_present || iommu == NULL)
2681 return -ENODEV;
2682
2683 return iommu_pc_get_set_reg_val(iommu, bank, cntr, fxn,
2684 value, is_write);
2685}