blob: 3d6e7c5471f2a6517b71223bae70d3e20b2badf7 [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 Roedelf6e2e6b2008-06-26 21:27:39 +02003 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * 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>
Joerg Roedel02f3b3f2012-06-11 17:45:25 +020029#include <linux/acpi.h>
30#include <acpi/acpi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020031#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090032#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010033#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090034#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040035#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020036#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020037#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020038
39#include "amd_iommu_proto.h"
40#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020041#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020042
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020043/*
44 * definitions for the ACPI scanning code
45 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020046#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020047
48#define ACPI_IVHD_TYPE 0x10
49#define ACPI_IVMD_TYPE_ALL 0x20
50#define ACPI_IVMD_TYPE 0x21
51#define ACPI_IVMD_TYPE_RANGE 0x22
52
53#define IVHD_DEV_ALL 0x01
54#define IVHD_DEV_SELECT 0x02
55#define IVHD_DEV_SELECT_RANGE_START 0x03
56#define IVHD_DEV_RANGE_END 0x04
57#define IVHD_DEV_ALIAS 0x42
58#define IVHD_DEV_ALIAS_RANGE 0x43
59#define IVHD_DEV_EXT_SELECT 0x46
60#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020061#define IVHD_DEV_SPECIAL 0x48
62
63#define IVHD_SPECIAL_IOAPIC 1
64#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020065
Joerg Roedel6da73422009-05-04 11:44:38 +020066#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
67#define IVHD_FLAG_PASSPW_EN_MASK 0x02
68#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
69#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020070
71#define IVMD_FLAG_EXCL_RANGE 0x08
72#define IVMD_FLAG_UNITY_MAP 0x01
73
74#define ACPI_DEVFLAG_INITPASS 0x01
75#define ACPI_DEVFLAG_EXTINT 0x02
76#define ACPI_DEVFLAG_NMI 0x04
77#define ACPI_DEVFLAG_SYSMGT1 0x10
78#define ACPI_DEVFLAG_SYSMGT2 0x20
79#define ACPI_DEVFLAG_LINT0 0x40
80#define ACPI_DEVFLAG_LINT1 0x80
81#define ACPI_DEVFLAG_ATSDIS 0x10000000
82
Joerg Roedelb65233a2008-07-11 17:14:21 +020083/*
84 * ACPI table definitions
85 *
86 * These data structures are laid over the table to parse the important values
87 * out of it.
88 */
89
90/*
91 * structure describing one IOMMU in the ACPI table. Typically followed by one
92 * or more ivhd_entrys.
93 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020094struct ivhd_header {
95 u8 type;
96 u8 flags;
97 u16 length;
98 u16 devid;
99 u16 cap_ptr;
100 u64 mmio_phys;
101 u16 pci_seg;
102 u16 info;
103 u32 reserved;
104} __attribute__((packed));
105
Joerg Roedelb65233a2008-07-11 17:14:21 +0200106/*
107 * A device entry describing which devices a specific IOMMU translates and
108 * which requestor ids they use.
109 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200110struct ivhd_entry {
111 u8 type;
112 u16 devid;
113 u8 flags;
114 u32 ext;
115} __attribute__((packed));
116
Joerg Roedelb65233a2008-07-11 17:14:21 +0200117/*
118 * An AMD IOMMU memory definition structure. It defines things like exclusion
119 * ranges for devices and regions that should be unity mapped.
120 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200121struct ivmd_header {
122 u8 type;
123 u8 flags;
124 u16 length;
125 u16 devid;
126 u16 aux;
127 u64 resv;
128 u64 range_start;
129 u64 range_length;
130} __attribute__((packed));
131
Joerg Roedelfefda112009-05-20 12:21:42 +0200132bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200133bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200134
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200135static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200136static bool __initdata amd_iommu_disabled;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200137
Joerg Roedelb65233a2008-07-11 17:14:21 +0200138u16 amd_iommu_last_bdf; /* largest PCI device id we have
139 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200140LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200141 we find in ACPI */
Dan Carpenter3775d482012-06-27 12:09:18 +0300142u32 amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200143
Joerg Roedel2e228472008-07-11 17:14:31 +0200144LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200145 system */
146
Joerg Roedelbb527772009-11-20 14:31:51 +0100147/* Array to assign indices to IOMMUs*/
148struct amd_iommu *amd_iommus[MAX_IOMMUS];
149int amd_iommus_present;
150
Joerg Roedel318afd42009-11-23 18:32:38 +0100151/* IOMMUs have a non-present cache? */
152bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200153bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100154
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100155u32 amd_iommu_max_pasids __read_mostly = ~0;
156
Joerg Roedel400a28a2011-11-28 15:11:02 +0100157bool amd_iommu_v2_present __read_mostly;
158
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100159bool amd_iommu_force_isolation __read_mostly;
160
Joerg Roedelb65233a2008-07-11 17:14:21 +0200161/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100162 * List of protection domains - used during resume
163 */
164LIST_HEAD(amd_iommu_pd_list);
165spinlock_t amd_iommu_pd_lock;
166
167/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200168 * Pointer to the device table which is shared by all AMD IOMMUs
169 * it is indexed by the PCI device id or the HT unit id and contains
170 * information about the domain the device belongs to as well as the
171 * page table root pointer.
172 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200173struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200174
175/*
176 * The alias table is a driver specific data structure which contains the
177 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
178 * More than one device can share the same requestor id.
179 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200180u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200181
182/*
183 * The rlookup table is used to find the IOMMU which is responsible
184 * for a specific device. It is also indexed by the PCI device id.
185 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200186struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200187
188/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200189 * This table is used to find the irq remapping table for a given device id
190 * quickly.
191 */
192struct irq_remap_table **irq_lookup_table;
193
194/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200195 * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
196 * to know which ones are already in use.
197 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200198unsigned long *amd_iommu_pd_alloc_bitmap;
199
Joerg Roedelb65233a2008-07-11 17:14:21 +0200200static u32 dev_table_size; /* size of the device table */
201static u32 alias_table_size; /* size of the alias table */
202static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200203
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200204enum iommu_init_state {
205 IOMMU_START_STATE,
206 IOMMU_IVRS_DETECTED,
207 IOMMU_ACPI_FINISHED,
208 IOMMU_ENABLED,
209 IOMMU_PCI_INIT,
210 IOMMU_INTERRUPTS_EN,
211 IOMMU_DMA_OPS,
212 IOMMU_INITIALIZED,
213 IOMMU_NOT_FOUND,
214 IOMMU_INIT_ERROR,
215};
216
217static enum iommu_init_state init_state = IOMMU_START_STATE;
218
Gerard Snitselaarae295142012-03-16 11:38:22 -0700219static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200220static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100221
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200222static inline void update_last_devid(u16 devid)
223{
224 if (devid > amd_iommu_last_bdf)
225 amd_iommu_last_bdf = devid;
226}
227
Joerg Roedelc5714842008-07-11 17:14:25 +0200228static inline unsigned long tbl_size(int entry_size)
229{
230 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100231 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200232
233 return 1UL << shift;
234}
235
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400236/* Access to l1 and l2 indexed register spaces */
237
238static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
239{
240 u32 val;
241
242 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
243 pci_read_config_dword(iommu->dev, 0xfc, &val);
244 return val;
245}
246
247static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
248{
249 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
250 pci_write_config_dword(iommu->dev, 0xfc, val);
251 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
252}
253
254static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
255{
256 u32 val;
257
258 pci_write_config_dword(iommu->dev, 0xf0, address);
259 pci_read_config_dword(iommu->dev, 0xf4, &val);
260 return val;
261}
262
263static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
264{
265 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
266 pci_write_config_dword(iommu->dev, 0xf4, val);
267}
268
Joerg Roedelb65233a2008-07-11 17:14:21 +0200269/****************************************************************************
270 *
271 * AMD IOMMU MMIO register space handling functions
272 *
273 * These functions are used to program the IOMMU device registers in
274 * MMIO space required for that driver.
275 *
276 ****************************************************************************/
277
278/*
279 * This function set the exclusion range in the IOMMU. DMA accesses to the
280 * exclusion range are passed through untranslated
281 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200282static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200283{
284 u64 start = iommu->exclusion_start & PAGE_MASK;
285 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
286 u64 entry;
287
288 if (!iommu->exclusion_start)
289 return;
290
291 entry = start | MMIO_EXCL_ENABLE_MASK;
292 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
293 &entry, sizeof(entry));
294
295 entry = limit;
296 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
297 &entry, sizeof(entry));
298}
299
Joerg Roedelb65233a2008-07-11 17:14:21 +0200300/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000301static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200302{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200303 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200304
305 BUG_ON(iommu->mmio_base == NULL);
306
307 entry = virt_to_phys(amd_iommu_dev_table);
308 entry |= (dev_table_size >> 12) - 1;
309 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
310 &entry, sizeof(entry));
311}
312
Joerg Roedelb65233a2008-07-11 17:14:21 +0200313/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200314static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200315{
316 u32 ctrl;
317
318 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
319 ctrl |= (1 << bit);
320 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
321}
322
Joerg Roedelca0207112009-10-28 18:02:26 +0100323static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200324{
325 u32 ctrl;
326
Joerg Roedel199d0d52008-09-17 16:45:59 +0200327 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200328 ctrl &= ~(1 << bit);
329 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
330}
331
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100332static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
333{
334 u32 ctrl;
335
336 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
337 ctrl &= ~CTRL_INV_TO_MASK;
338 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
339 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
340}
341
Joerg Roedelb65233a2008-07-11 17:14:21 +0200342/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200343static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200344{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200345 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200346}
347
Joerg Roedel92ac4322009-05-19 19:06:27 +0200348static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200349{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200350 /* Disable command buffer */
351 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
352
353 /* Disable event logging and event interrupts */
354 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
355 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
356
357 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200358 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200359}
360
Joerg Roedelb65233a2008-07-11 17:14:21 +0200361/*
362 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
363 * the system has one.
364 */
Joerg Roedel98f1ad22012-07-06 13:28:37 +0200365static u8 __iomem * __init iommu_map_mmio_space(u64 address)
Joerg Roedel6c567472008-06-26 21:27:43 +0200366{
Joerg Roedele82752d2010-05-28 14:26:48 +0200367 if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu")) {
368 pr_err("AMD-Vi: Can not reserve memory region %llx for mmio\n",
369 address);
370 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200371 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200372 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200373
Joerg Roedel98f1ad22012-07-06 13:28:37 +0200374 return (u8 __iomem *)ioremap_nocache(address, MMIO_REGION_LENGTH);
Joerg Roedel6c567472008-06-26 21:27:43 +0200375}
376
377static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
378{
379 if (iommu->mmio_base)
380 iounmap(iommu->mmio_base);
381 release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
382}
383
Joerg Roedelb65233a2008-07-11 17:14:21 +0200384/****************************************************************************
385 *
386 * The functions below belong to the first pass of AMD IOMMU ACPI table
387 * parsing. In this pass we try to find out the highest device id this
388 * code has to handle. Upon this information the size of the shared data
389 * structures is determined later.
390 *
391 ****************************************************************************/
392
393/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200394 * This function calculates the length of a given IVHD entry
395 */
396static inline int ivhd_entry_length(u8 *ivhd)
397{
398 return 0x04 << (*ivhd >> 6);
399}
400
401/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200402 * This function reads the last device id the IOMMU has to handle from the PCI
403 * capability header for this IOMMU
404 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200405static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
406{
407 u32 cap;
408
409 cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
Joerg Roedeld591b0a2008-07-11 17:14:35 +0200410 update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200411
412 return 0;
413}
414
Joerg Roedelb65233a2008-07-11 17:14:21 +0200415/*
416 * After reading the highest device id from the IOMMU PCI capability header
417 * this function looks if there is a higher device id defined in the ACPI table
418 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200419static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
420{
421 u8 *p = (void *)h, *end = (void *)h;
422 struct ivhd_entry *dev;
423
424 p += sizeof(*h);
425 end += h->length;
426
427 find_last_devid_on_pci(PCI_BUS(h->devid),
428 PCI_SLOT(h->devid),
429 PCI_FUNC(h->devid),
430 h->cap_ptr);
431
432 while (p < end) {
433 dev = (struct ivhd_entry *)p;
434 switch (dev->type) {
435 case IVHD_DEV_SELECT:
436 case IVHD_DEV_RANGE_END:
437 case IVHD_DEV_ALIAS:
438 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200439 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200440 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200441 break;
442 default:
443 break;
444 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200445 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200446 }
447
448 WARN_ON(p != end);
449
450 return 0;
451}
452
Joerg Roedelb65233a2008-07-11 17:14:21 +0200453/*
454 * Iterate over all IVHD entries in the ACPI table and find the highest device
455 * id which we need to handle. This is the first of three functions which parse
456 * the ACPI table. So we check the checksum here.
457 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200458static int __init find_last_devid_acpi(struct acpi_table_header *table)
459{
460 int i;
461 u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
462 struct ivhd_header *h;
463
464 /*
465 * Validate checksum here so we don't need to do it when
466 * we actually parse the table
467 */
468 for (i = 0; i < table->length; ++i)
469 checksum += p[i];
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200470 if (checksum != 0)
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200471 /* ACPI table corrupt */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200472 return -ENODEV;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200473
474 p += IVRS_HEADER_LENGTH;
475
476 end += table->length;
477 while (p < end) {
478 h = (struct ivhd_header *)p;
479 switch (h->type) {
480 case ACPI_IVHD_TYPE:
481 find_last_devid_from_ivhd(h);
482 break;
483 default:
484 break;
485 }
486 p += h->length;
487 }
488 WARN_ON(p != end);
489
490 return 0;
491}
492
Joerg Roedelb65233a2008-07-11 17:14:21 +0200493/****************************************************************************
494 *
495 * The following functions belong the the code path which parses the ACPI table
496 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
497 * data structures, initialize the device/alias/rlookup table and also
498 * basically initialize the hardware.
499 *
500 ****************************************************************************/
501
502/*
503 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
504 * write commands to that buffer later and the IOMMU will execute them
505 * asynchronously
506 */
Joerg Roedelb36ca912008-06-26 21:27:45 +0200507static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
508{
Joerg Roedeld0312b212008-07-11 17:14:29 +0200509 u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelb36ca912008-06-26 21:27:45 +0200510 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200511
512 if (cmd_buf == NULL)
513 return NULL;
514
Chris Wright549c90dc2010-04-02 18:27:53 -0700515 iommu->cmd_buf_size = CMD_BUFFER_SIZE | CMD_BUFFER_UNINITIALIZED;
Joerg Roedelb36ca912008-06-26 21:27:45 +0200516
Joerg Roedel58492e12009-05-04 18:41:16 +0200517 return cmd_buf;
518}
519
520/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200521 * This function resets the command buffer if the IOMMU stopped fetching
522 * commands from it.
523 */
524void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
525{
526 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
527
528 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
529 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
530
531 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
532}
533
534/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200535 * This function writes the command buffer address to the hardware and
536 * enables it.
537 */
538static void iommu_enable_command_buffer(struct amd_iommu *iommu)
539{
540 u64 entry;
541
542 BUG_ON(iommu->cmd_buf == NULL);
543
544 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200545 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200546
Joerg Roedelb36ca912008-06-26 21:27:45 +0200547 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200548 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200549
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200550 amd_iommu_reset_cmd_buffer(iommu);
Chris Wright549c90dc2010-04-02 18:27:53 -0700551 iommu->cmd_buf_size &= ~(CMD_BUFFER_UNINITIALIZED);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200552}
553
554static void __init free_command_buffer(struct amd_iommu *iommu)
555{
Joerg Roedel23c17132008-09-17 17:18:17 +0200556 free_pages((unsigned long)iommu->cmd_buf,
Chris Wright549c90dc2010-04-02 18:27:53 -0700557 get_order(iommu->cmd_buf_size & ~(CMD_BUFFER_UNINITIALIZED)));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200558}
559
Joerg Roedel335503e2008-09-05 14:29:07 +0200560/* allocates the memory where the IOMMU will log its events to */
561static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
562{
Joerg Roedel335503e2008-09-05 14:29:07 +0200563 iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
564 get_order(EVT_BUFFER_SIZE));
565
566 if (iommu->evt_buf == NULL)
567 return NULL;
568
Joerg Roedel1bc6f832009-07-02 18:32:05 +0200569 iommu->evt_buf_size = EVT_BUFFER_SIZE;
570
Joerg Roedel58492e12009-05-04 18:41:16 +0200571 return iommu->evt_buf;
572}
573
574static void iommu_enable_event_buffer(struct amd_iommu *iommu)
575{
576 u64 entry;
577
578 BUG_ON(iommu->evt_buf == NULL);
579
Joerg Roedel335503e2008-09-05 14:29:07 +0200580 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200581
Joerg Roedel335503e2008-09-05 14:29:07 +0200582 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
583 &entry, sizeof(entry));
584
Joerg Roedel090672072009-06-15 16:06:48 +0200585 /* set head and tail to zero manually */
586 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
587 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
588
Joerg Roedel58492e12009-05-04 18:41:16 +0200589 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200590}
591
592static void __init free_event_buffer(struct amd_iommu *iommu)
593{
594 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
595}
596
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100597/* allocates the memory where the IOMMU will log its events to */
598static u8 * __init alloc_ppr_log(struct amd_iommu *iommu)
599{
600 iommu->ppr_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
601 get_order(PPR_LOG_SIZE));
602
603 if (iommu->ppr_log == NULL)
604 return NULL;
605
606 return iommu->ppr_log;
607}
608
609static void iommu_enable_ppr_log(struct amd_iommu *iommu)
610{
611 u64 entry;
612
613 if (iommu->ppr_log == NULL)
614 return;
615
616 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
617
618 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
619 &entry, sizeof(entry));
620
621 /* set head and tail to zero manually */
622 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
623 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
624
625 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
626 iommu_feature_enable(iommu, CONTROL_PPR_EN);
627}
628
629static void __init free_ppr_log(struct amd_iommu *iommu)
630{
631 if (iommu->ppr_log == NULL)
632 return;
633
634 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
635}
636
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100637static void iommu_enable_gt(struct amd_iommu *iommu)
638{
639 if (!iommu_feature(iommu, FEATURE_GT))
640 return;
641
642 iommu_feature_enable(iommu, CONTROL_GT_EN);
643}
644
Joerg Roedelb65233a2008-07-11 17:14:21 +0200645/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200646static void set_dev_entry_bit(u16 devid, u8 bit)
647{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100648 int i = (bit >> 6) & 0x03;
649 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200650
Joerg Roedelee6c2862011-11-09 12:06:03 +0100651 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200652}
653
Joerg Roedelc5cca142009-10-09 18:31:20 +0200654static int get_dev_entry_bit(u16 devid, u8 bit)
655{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100656 int i = (bit >> 6) & 0x03;
657 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200658
Joerg Roedelee6c2862011-11-09 12:06:03 +0100659 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200660}
661
662
663void amd_iommu_apply_erratum_63(u16 devid)
664{
665 int sysmgt;
666
667 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
668 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
669
670 if (sysmgt == 0x01)
671 set_dev_entry_bit(devid, DEV_ENTRY_IW);
672}
673
Joerg Roedel5ff47892008-07-14 20:11:18 +0200674/* Writes the specific IOMMU for a device into the rlookup table */
675static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
676{
677 amd_iommu_rlookup_table[devid] = iommu;
678}
679
Joerg Roedelb65233a2008-07-11 17:14:21 +0200680/*
681 * This function takes the device specific flags read from the ACPI
682 * table and sets up the device table entry with that information
683 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200684static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
685 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200686{
687 if (flags & ACPI_DEVFLAG_INITPASS)
688 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
689 if (flags & ACPI_DEVFLAG_EXTINT)
690 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
691 if (flags & ACPI_DEVFLAG_NMI)
692 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
693 if (flags & ACPI_DEVFLAG_SYSMGT1)
694 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
695 if (flags & ACPI_DEVFLAG_SYSMGT2)
696 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
697 if (flags & ACPI_DEVFLAG_LINT0)
698 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
699 if (flags & ACPI_DEVFLAG_LINT1)
700 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200701
Joerg Roedelc5cca142009-10-09 18:31:20 +0200702 amd_iommu_apply_erratum_63(devid);
703
Joerg Roedel5ff47892008-07-14 20:11:18 +0200704 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200705}
706
Joerg Roedel6efed632012-06-14 15:52:58 +0200707static int add_special_device(u8 type, u8 id, u16 devid)
708{
709 struct devid_map *entry;
710 struct list_head *list;
711
712 if (type != IVHD_SPECIAL_IOAPIC && type != IVHD_SPECIAL_HPET)
713 return -EINVAL;
714
715 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
716 if (!entry)
717 return -ENOMEM;
718
719 entry->id = id;
720 entry->devid = devid;
721
722 if (type == IVHD_SPECIAL_IOAPIC)
723 list = &ioapic_map;
724 else
725 list = &hpet_map;
726
727 list_add_tail(&entry->list, list);
728
729 return 0;
730}
731
Joerg Roedelb65233a2008-07-11 17:14:21 +0200732/*
733 * Reads the device exclusion range from ACPI and initialize IOMMU with
734 * it
735 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200736static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
737{
738 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
739
740 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
741 return;
742
743 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200744 /*
745 * We only can configure exclusion ranges per IOMMU, not
746 * per device. But we can enable the exclusion range per
747 * device. This is done here
748 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200749 set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
750 iommu->exclusion_start = m->range_start;
751 iommu->exclusion_length = m->range_length;
752 }
753}
754
Joerg Roedelb65233a2008-07-11 17:14:21 +0200755/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200756 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
757 * initializes the hardware and our data structures with it.
758 */
Joerg Roedel6efed632012-06-14 15:52:58 +0200759static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200760 struct ivhd_header *h)
761{
762 u8 *p = (u8 *)h;
763 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +0200764 u16 devid = 0, devid_start = 0, devid_to = 0;
765 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200766 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200767 struct ivhd_entry *e;
768
769 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +0200770 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200771 */
Joerg Roedele9bf5192010-09-20 14:33:07 +0200772 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200773
774 /*
775 * Done. Now parse the device entries
776 */
777 p += sizeof(struct ivhd_header);
778 end += h->length;
779
Joerg Roedel42a698f2009-05-20 15:41:28 +0200780
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200781 while (p < end) {
782 e = (struct ivhd_entry *)p;
783 switch (e->type) {
784 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200785
786 DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
787 " last device %02x:%02x.%x flags: %02x\n",
788 PCI_BUS(iommu->first_device),
789 PCI_SLOT(iommu->first_device),
790 PCI_FUNC(iommu->first_device),
791 PCI_BUS(iommu->last_device),
792 PCI_SLOT(iommu->last_device),
793 PCI_FUNC(iommu->last_device),
794 e->flags);
795
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200796 for (dev_i = iommu->first_device;
797 dev_i <= iommu->last_device; ++dev_i)
Joerg Roedel5ff47892008-07-14 20:11:18 +0200798 set_dev_entry_from_acpi(iommu, dev_i,
799 e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200800 break;
801 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200802
803 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
804 "flags: %02x\n",
805 PCI_BUS(e->devid),
806 PCI_SLOT(e->devid),
807 PCI_FUNC(e->devid),
808 e->flags);
809
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200810 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +0200811 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200812 break;
813 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200814
815 DUMP_printk(" DEV_SELECT_RANGE_START\t "
816 "devid: %02x:%02x.%x flags: %02x\n",
817 PCI_BUS(e->devid),
818 PCI_SLOT(e->devid),
819 PCI_FUNC(e->devid),
820 e->flags);
821
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200822 devid_start = e->devid;
823 flags = e->flags;
824 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200825 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200826 break;
827 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200828
829 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
830 "flags: %02x devid_to: %02x:%02x.%x\n",
831 PCI_BUS(e->devid),
832 PCI_SLOT(e->devid),
833 PCI_FUNC(e->devid),
834 e->flags,
835 PCI_BUS(e->ext >> 8),
836 PCI_SLOT(e->ext >> 8),
837 PCI_FUNC(e->ext >> 8));
838
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200839 devid = e->devid;
840 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200841 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +0100842 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200843 amd_iommu_alias_table[devid] = devid_to;
844 break;
845 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200846
847 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
848 "devid: %02x:%02x.%x flags: %02x "
849 "devid_to: %02x:%02x.%x\n",
850 PCI_BUS(e->devid),
851 PCI_SLOT(e->devid),
852 PCI_FUNC(e->devid),
853 e->flags,
854 PCI_BUS(e->ext >> 8),
855 PCI_SLOT(e->ext >> 8),
856 PCI_FUNC(e->ext >> 8));
857
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200858 devid_start = e->devid;
859 flags = e->flags;
860 devid_to = e->ext >> 8;
861 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200862 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200863 break;
864 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200865
866 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
867 "flags: %02x ext: %08x\n",
868 PCI_BUS(e->devid),
869 PCI_SLOT(e->devid),
870 PCI_FUNC(e->devid),
871 e->flags, e->ext);
872
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200873 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +0200874 set_dev_entry_from_acpi(iommu, devid, e->flags,
875 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200876 break;
877 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200878
879 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
880 "%02x:%02x.%x flags: %02x ext: %08x\n",
881 PCI_BUS(e->devid),
882 PCI_SLOT(e->devid),
883 PCI_FUNC(e->devid),
884 e->flags, e->ext);
885
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200886 devid_start = e->devid;
887 flags = e->flags;
888 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200889 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200890 break;
891 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200892
893 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
894 PCI_BUS(e->devid),
895 PCI_SLOT(e->devid),
896 PCI_FUNC(e->devid));
897
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200898 devid = e->devid;
899 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200900 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200901 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200902 set_dev_entry_from_acpi(iommu,
903 devid_to, flags, ext_flags);
904 }
905 set_dev_entry_from_acpi(iommu, dev_i,
906 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200907 }
908 break;
Joerg Roedel6efed632012-06-14 15:52:58 +0200909 case IVHD_DEV_SPECIAL: {
910 u8 handle, type;
911 const char *var;
912 u16 devid;
913 int ret;
914
915 handle = e->ext & 0xff;
916 devid = (e->ext >> 8) & 0xffff;
917 type = (e->ext >> 24) & 0xff;
918
919 if (type == IVHD_SPECIAL_IOAPIC)
920 var = "IOAPIC";
921 else if (type == IVHD_SPECIAL_HPET)
922 var = "HPET";
923 else
924 var = "UNKNOWN";
925
926 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
927 var, (int)handle,
928 PCI_BUS(devid),
929 PCI_SLOT(devid),
930 PCI_FUNC(devid));
931
932 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
933 ret = add_special_device(type, handle, devid);
934 if (ret)
935 return ret;
936 break;
937 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200938 default:
939 break;
940 }
941
Joerg Roedelb514e552008-09-17 17:14:27 +0200942 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200943 }
Joerg Roedel6efed632012-06-14 15:52:58 +0200944
945 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200946}
947
Joerg Roedelb65233a2008-07-11 17:14:21 +0200948/* Initializes the device->iommu mapping for the driver */
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200949static int __init init_iommu_devices(struct amd_iommu *iommu)
950{
Joerg Roedel0de66d52011-06-06 16:04:02 +0200951 u32 i;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200952
953 for (i = iommu->first_device; i <= iommu->last_device; ++i)
954 set_iommu_for_device(iommu, i);
955
956 return 0;
957}
958
Joerg Roedele47d4022008-06-26 21:27:48 +0200959static void __init free_iommu_one(struct amd_iommu *iommu)
960{
961 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +0200962 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100963 free_ppr_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +0200964 iommu_unmap_mmio_space(iommu);
965}
966
967static void __init free_iommu_all(void)
968{
969 struct amd_iommu *iommu, *next;
970
Joerg Roedel3bd22172009-05-04 15:06:20 +0200971 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +0200972 list_del(&iommu->list);
973 free_iommu_one(iommu);
974 kfree(iommu);
975 }
976}
977
Joerg Roedelb65233a2008-07-11 17:14:21 +0200978/*
979 * This function clues the initialization function for one IOMMU
980 * together and also allocates the command buffer and programs the
981 * hardware. It does NOT enable the IOMMU. This is done afterwards.
982 */
Joerg Roedele47d4022008-06-26 21:27:48 +0200983static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
984{
Joerg Roedel6efed632012-06-14 15:52:58 +0200985 int ret;
986
Joerg Roedele47d4022008-06-26 21:27:48 +0200987 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +0100988
989 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +0200990 list_add_tail(&iommu->list, &amd_iommu_list);
Joerg Roedelbb527772009-11-20 14:31:51 +0100991 iommu->index = amd_iommus_present++;
992
993 if (unlikely(iommu->index >= MAX_IOMMUS)) {
994 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
995 return -ENOSYS;
996 }
997
998 /* Index is fine - add IOMMU to the array */
999 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001000
1001 /*
1002 * Copy data from ACPI table entry to the iommu struct
1003 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001004 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001005 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001006 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001007 iommu->mmio_phys = h->mmio_phys;
1008 iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
1009 if (!iommu->mmio_base)
1010 return -ENOMEM;
1011
Joerg Roedele47d4022008-06-26 21:27:48 +02001012 iommu->cmd_buf = alloc_command_buffer(iommu);
1013 if (!iommu->cmd_buf)
1014 return -ENOMEM;
1015
Joerg Roedel335503e2008-09-05 14:29:07 +02001016 iommu->evt_buf = alloc_event_buffer(iommu);
1017 if (!iommu->evt_buf)
1018 return -ENOMEM;
1019
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001020 iommu->int_enabled = false;
1021
Joerg Roedel6efed632012-06-14 15:52:58 +02001022 ret = init_iommu_from_acpi(iommu, h);
1023 if (ret)
1024 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001025
1026 /*
1027 * Make sure IOMMU is not considered to translate itself. The IVRS
1028 * table tells us so, but this is a lie!
1029 */
1030 amd_iommu_rlookup_table[iommu->devid] = NULL;
1031
Joerg Roedele47d4022008-06-26 21:27:48 +02001032 init_iommu_devices(iommu);
1033
Joerg Roedel23c742d2012-06-12 11:47:34 +02001034 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001035}
1036
Joerg Roedelb65233a2008-07-11 17:14:21 +02001037/*
1038 * Iterates over all IOMMU entries in the ACPI table, allocates the
1039 * IOMMU structure and initializes it with init_iommu_one()
1040 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001041static int __init init_iommu_all(struct acpi_table_header *table)
1042{
1043 u8 *p = (u8 *)table, *end = (u8 *)table;
1044 struct ivhd_header *h;
1045 struct amd_iommu *iommu;
1046 int ret;
1047
Joerg Roedele47d4022008-06-26 21:27:48 +02001048 end += table->length;
1049 p += IVRS_HEADER_LENGTH;
1050
1051 while (p < end) {
1052 h = (struct ivhd_header *)p;
1053 switch (*p) {
1054 case ACPI_IVHD_TYPE:
Joerg Roedel9c720412009-05-20 13:53:57 +02001055
Joerg Roedelae908c22009-09-01 16:52:16 +02001056 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001057 "seg: %d flags: %01x info %04x\n",
1058 PCI_BUS(h->devid), PCI_SLOT(h->devid),
1059 PCI_FUNC(h->devid), h->cap_ptr,
1060 h->pci_seg, h->flags, h->info);
1061 DUMP_printk(" mmio-addr: %016llx\n",
1062 h->mmio_phys);
1063
Joerg Roedele47d4022008-06-26 21:27:48 +02001064 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001065 if (iommu == NULL)
1066 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001067
Joerg Roedele47d4022008-06-26 21:27:48 +02001068 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001069 if (ret)
1070 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001071 break;
1072 default:
1073 break;
1074 }
1075 p += h->length;
1076
1077 }
1078 WARN_ON(p != end);
1079
1080 return 0;
1081}
1082
Joerg Roedel23c742d2012-06-12 11:47:34 +02001083static int iommu_init_pci(struct amd_iommu *iommu)
1084{
1085 int cap_ptr = iommu->cap_ptr;
1086 u32 range, misc, low, high;
1087
1088 iommu->dev = pci_get_bus_and_slot(PCI_BUS(iommu->devid),
1089 iommu->devid & 0xff);
1090 if (!iommu->dev)
1091 return -ENODEV;
1092
1093 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1094 &iommu->cap);
1095 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1096 &range);
1097 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1098 &misc);
1099
1100 iommu->first_device = calc_devid(MMIO_GET_BUS(range),
1101 MMIO_GET_FD(range));
1102 iommu->last_device = calc_devid(MMIO_GET_BUS(range),
1103 MMIO_GET_LD(range));
1104
1105 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1106 amd_iommu_iotlb_sup = false;
1107
1108 /* read extended feature bits */
1109 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1110 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1111
1112 iommu->features = ((u64)high << 32) | low;
1113
1114 if (iommu_feature(iommu, FEATURE_GT)) {
1115 int glxval;
1116 u32 pasids;
1117 u64 shift;
1118
1119 shift = iommu->features & FEATURE_PASID_MASK;
1120 shift >>= FEATURE_PASID_SHIFT;
1121 pasids = (1 << shift);
1122
1123 amd_iommu_max_pasids = min(amd_iommu_max_pasids, pasids);
1124
1125 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1126 glxval >>= FEATURE_GLXVAL_SHIFT;
1127
1128 if (amd_iommu_max_glx_val == -1)
1129 amd_iommu_max_glx_val = glxval;
1130 else
1131 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1132 }
1133
1134 if (iommu_feature(iommu, FEATURE_GT) &&
1135 iommu_feature(iommu, FEATURE_PPR)) {
1136 iommu->is_iommu_v2 = true;
1137 amd_iommu_v2_present = true;
1138 }
1139
1140 if (iommu_feature(iommu, FEATURE_PPR)) {
1141 iommu->ppr_log = alloc_ppr_log(iommu);
1142 if (!iommu->ppr_log)
1143 return -ENOMEM;
1144 }
1145
1146 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1147 amd_iommu_np_cache = true;
1148
1149 if (is_rd890_iommu(iommu->dev)) {
1150 int i, j;
1151
1152 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1153 PCI_DEVFN(0, 0));
1154
1155 /*
1156 * Some rd890 systems may not be fully reconfigured by the
1157 * BIOS, so it's necessary for us to store this information so
1158 * it can be reprogrammed on resume
1159 */
1160 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1161 &iommu->stored_addr_lo);
1162 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1163 &iommu->stored_addr_hi);
1164
1165 /* Low bit locks writes to configuration space */
1166 iommu->stored_addr_lo &= ~1;
1167
1168 for (i = 0; i < 6; i++)
1169 for (j = 0; j < 0x12; j++)
1170 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1171
1172 for (i = 0; i < 0x83; i++)
1173 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1174 }
1175
1176 return pci_enable_device(iommu->dev);
1177}
1178
Joerg Roedel4d121c32012-06-14 12:21:55 +02001179static void print_iommu_info(void)
1180{
1181 static const char * const feat_str[] = {
1182 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1183 "IA", "GA", "HE", "PC"
1184 };
1185 struct amd_iommu *iommu;
1186
1187 for_each_iommu(iommu) {
1188 int i;
1189
1190 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1191 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1192
1193 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1194 pr_info("AMD-Vi: Extended features: ");
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001195 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001196 if (iommu_feature(iommu, (1ULL << i)))
1197 pr_cont(" %s", feat_str[i]);
1198 }
1199 }
1200 pr_cont("\n");
1201 }
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001202 if (irq_remapping_enabled)
1203 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Joerg Roedel4d121c32012-06-14 12:21:55 +02001204}
1205
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001206static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001207{
1208 struct amd_iommu *iommu;
1209 int ret = 0;
1210
1211 for_each_iommu(iommu) {
1212 ret = iommu_init_pci(iommu);
1213 if (ret)
1214 break;
1215 }
1216
Joerg Roedel23c742d2012-06-12 11:47:34 +02001217 ret = amd_iommu_init_devices();
1218
Joerg Roedel4d121c32012-06-14 12:21:55 +02001219 print_iommu_info();
1220
Joerg Roedel23c742d2012-06-12 11:47:34 +02001221 return ret;
1222}
1223
Joerg Roedelb65233a2008-07-11 17:14:21 +02001224/****************************************************************************
1225 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001226 * The following functions initialize the MSI interrupts for all IOMMUs
1227 * in the system. Its a bit challenging because there could be multiple
1228 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1229 * pci_dev.
1230 *
1231 ****************************************************************************/
1232
Joerg Roedel9f800de2009-11-23 12:45:25 +01001233static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001234{
1235 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001236
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001237 r = pci_enable_msi(iommu->dev);
1238 if (r)
1239 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001240
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001241 r = request_threaded_irq(iommu->dev->irq,
1242 amd_iommu_int_handler,
1243 amd_iommu_int_thread,
1244 0, "AMD-Vi",
1245 iommu->dev);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001246
1247 if (r) {
1248 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001249 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001250 }
1251
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001252 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001253
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001254 return 0;
1255}
1256
Joerg Roedel05f92db2009-05-12 09:52:46 +02001257static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001258{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001259 int ret;
1260
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001261 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001262 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001263
Joerg Roedeld91cecd2009-05-04 18:51:00 +02001264 if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001265 ret = iommu_setup_msi(iommu);
1266 else
1267 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001268
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001269 if (ret)
1270 return ret;
1271
1272enable_faults:
1273 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1274
1275 if (iommu->ppr_log != NULL)
1276 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1277
1278 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001279}
1280
1281/****************************************************************************
1282 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001283 * The next functions belong to the third pass of parsing the ACPI
1284 * table. In this last pass the memory mapping requirements are
1285 * gathered (like exclusion and unity mapping reanges).
1286 *
1287 ****************************************************************************/
1288
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001289static void __init free_unity_maps(void)
1290{
1291 struct unity_map_entry *entry, *next;
1292
1293 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1294 list_del(&entry->list);
1295 kfree(entry);
1296 }
1297}
1298
Joerg Roedelb65233a2008-07-11 17:14:21 +02001299/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001300static int __init init_exclusion_range(struct ivmd_header *m)
1301{
1302 int i;
1303
1304 switch (m->type) {
1305 case ACPI_IVMD_TYPE:
1306 set_device_exclusion_range(m->devid, m);
1307 break;
1308 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001309 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001310 set_device_exclusion_range(i, m);
1311 break;
1312 case ACPI_IVMD_TYPE_RANGE:
1313 for (i = m->devid; i <= m->aux; ++i)
1314 set_device_exclusion_range(i, m);
1315 break;
1316 default:
1317 break;
1318 }
1319
1320 return 0;
1321}
1322
Joerg Roedelb65233a2008-07-11 17:14:21 +02001323/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001324static int __init init_unity_map_range(struct ivmd_header *m)
1325{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001326 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001327 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001328
1329 e = kzalloc(sizeof(*e), GFP_KERNEL);
1330 if (e == NULL)
1331 return -ENOMEM;
1332
1333 switch (m->type) {
1334 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001335 kfree(e);
1336 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001337 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001338 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001339 e->devid_start = e->devid_end = m->devid;
1340 break;
1341 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001342 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001343 e->devid_start = 0;
1344 e->devid_end = amd_iommu_last_bdf;
1345 break;
1346 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001347 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001348 e->devid_start = m->devid;
1349 e->devid_end = m->aux;
1350 break;
1351 }
1352 e->address_start = PAGE_ALIGN(m->range_start);
1353 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1354 e->prot = m->flags >> 1;
1355
Joerg Roedel02acc432009-05-20 16:24:21 +02001356 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1357 " range_start: %016llx range_end: %016llx flags: %x\n", s,
1358 PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
1359 PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
1360 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1361 e->address_start, e->address_end, m->flags);
1362
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001363 list_add_tail(&e->list, &amd_iommu_unity_map);
1364
1365 return 0;
1366}
1367
Joerg Roedelb65233a2008-07-11 17:14:21 +02001368/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001369static int __init init_memory_definitions(struct acpi_table_header *table)
1370{
1371 u8 *p = (u8 *)table, *end = (u8 *)table;
1372 struct ivmd_header *m;
1373
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001374 end += table->length;
1375 p += IVRS_HEADER_LENGTH;
1376
1377 while (p < end) {
1378 m = (struct ivmd_header *)p;
1379 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1380 init_exclusion_range(m);
1381 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1382 init_unity_map_range(m);
1383
1384 p += m->length;
1385 }
1386
1387 return 0;
1388}
1389
Joerg Roedelb65233a2008-07-11 17:14:21 +02001390/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001391 * Init the device table to not allow DMA access for devices and
1392 * suppress all page faults
1393 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02001394static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001395{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001396 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001397
1398 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1399 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1400 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001401 }
1402}
1403
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001404static void __init uninit_device_table_dma(void)
1405{
1406 u32 devid;
1407
1408 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1409 amd_iommu_dev_table[devid].data[0] = 0ULL;
1410 amd_iommu_dev_table[devid].data[1] = 0ULL;
1411 }
1412}
1413
Joerg Roedel33f28c52012-06-15 18:03:31 +02001414static void init_device_table(void)
1415{
1416 u32 devid;
1417
1418 if (!amd_iommu_irq_remap)
1419 return;
1420
1421 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1422 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1423}
1424
Joerg Roedele9bf5192010-09-20 14:33:07 +02001425static void iommu_init_flags(struct amd_iommu *iommu)
1426{
1427 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1428 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1429 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1430
1431 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1432 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1433 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1434
1435 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1436 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1437 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1438
1439 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1440 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1441 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1442
1443 /*
1444 * make IOMMU memory accesses cache coherent
1445 */
1446 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001447
1448 /* Set IOTLB invalidation timeout to 1s */
1449 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001450}
1451
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001452static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001453{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001454 int i, j;
1455 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001456 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001457
1458 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001459 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001460 return;
1461
1462 /*
1463 * First, we need to ensure that the iommu is enabled. This is
1464 * controlled by a register in the northbridge
1465 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001466
1467 /* Select Northbridge indirect register 0x75 and enable writing */
1468 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1469 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1470
1471 /* Enable the iommu */
1472 if (!(ioc_feature_control & 0x1))
1473 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1474
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001475 /* Restore the iommu BAR */
1476 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1477 iommu->stored_addr_lo);
1478 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1479 iommu->stored_addr_hi);
1480
1481 /* Restore the l1 indirect regs for each of the 6 l1s */
1482 for (i = 0; i < 6; i++)
1483 for (j = 0; j < 0x12; j++)
1484 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1485
1486 /* Restore the l2 indirect regs */
1487 for (i = 0; i < 0x83; i++)
1488 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
1489
1490 /* Lock PCI setup registers */
1491 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1492 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02001493}
1494
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001495/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001496 * This function finally enables all IOMMUs found in the system after
1497 * they have been initialized
1498 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02001499static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02001500{
1501 struct amd_iommu *iommu;
1502
Joerg Roedel3bd22172009-05-04 15:06:20 +02001503 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02001504 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001505 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02001506 iommu_set_device_table(iommu);
1507 iommu_enable_command_buffer(iommu);
1508 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001509 iommu_set_exclusion_range(iommu);
1510 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001511 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001512 }
1513}
1514
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02001515static void enable_iommus_v2(void)
1516{
1517 struct amd_iommu *iommu;
1518
1519 for_each_iommu(iommu) {
1520 iommu_enable_ppr_log(iommu);
1521 iommu_enable_gt(iommu);
1522 }
1523}
1524
1525static void enable_iommus(void)
1526{
1527 early_enable_iommus();
1528
1529 enable_iommus_v2();
1530}
1531
Joerg Roedel92ac4322009-05-19 19:06:27 +02001532static void disable_iommus(void)
1533{
1534 struct amd_iommu *iommu;
1535
1536 for_each_iommu(iommu)
1537 iommu_disable(iommu);
1538}
1539
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001540/*
1541 * Suspend/Resume support
1542 * disable suspend until real resume implemented
1543 */
1544
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001545static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001546{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001547 struct amd_iommu *iommu;
1548
1549 for_each_iommu(iommu)
1550 iommu_apply_resume_quirks(iommu);
1551
Joerg Roedel736501e2009-05-12 09:56:12 +02001552 /* re-load the hardware */
1553 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01001554
1555 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001556}
1557
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001558static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001559{
Joerg Roedel736501e2009-05-12 09:56:12 +02001560 /* disable IOMMUs to go out of the way for BIOS */
1561 disable_iommus();
1562
1563 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001564}
1565
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001566static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001567 .suspend = amd_iommu_suspend,
1568 .resume = amd_iommu_resume,
1569};
1570
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001571static void __init free_on_init_error(void)
1572{
Joerg Roedel0ea2c422012-06-15 18:05:20 +02001573 free_pages((unsigned long)irq_lookup_table,
1574 get_order(rlookup_table_size));
1575
Joerg Roedel05152a02012-06-15 16:53:51 +02001576 if (amd_iommu_irq_cache) {
1577 kmem_cache_destroy(amd_iommu_irq_cache);
1578 amd_iommu_irq_cache = NULL;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02001579
Joerg Roedel05152a02012-06-15 16:53:51 +02001580 }
1581
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001582 free_pages((unsigned long)amd_iommu_rlookup_table,
1583 get_order(rlookup_table_size));
1584
1585 free_pages((unsigned long)amd_iommu_alias_table,
1586 get_order(alias_table_size));
1587
1588 free_pages((unsigned long)amd_iommu_dev_table,
1589 get_order(dev_table_size));
1590
1591 free_iommu_all();
1592
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001593#ifdef CONFIG_GART_IOMMU
1594 /*
1595 * We failed to initialize the AMD IOMMU - try fallback to GART
1596 * if possible.
1597 */
1598 gart_iommu_init();
1599
1600#endif
1601}
1602
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02001603static bool __init check_ioapic_information(void)
1604{
1605 int idx;
1606
1607 for (idx = 0; idx < nr_ioapics; idx++) {
1608 int id = mpc_ioapic_id(idx);
1609
1610 if (get_ioapic_devid(id) < 0) {
1611 pr_err(FW_BUG "AMD-Vi: IO-APIC[%d] not in IVRS table\n", id);
1612 pr_err("AMD-Vi: Disabling interrupt remapping due to BIOS Bug\n");
1613 return false;
1614 }
1615 }
1616
1617 return true;
1618}
1619
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001620static void __init free_dma_resources(void)
1621{
1622 amd_iommu_uninit_devices();
1623
1624 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
1625 get_order(MAX_DOMAIN_ID/8));
1626
1627 free_unity_maps();
1628}
1629
Joerg Roedelb65233a2008-07-11 17:14:21 +02001630/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001631 * This is the hardware init function for AMD IOMMU in the system.
1632 * This function is called either from amd_iommu_init or from the interrupt
1633 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02001634 *
1635 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
1636 * three times:
1637 *
1638 * 1 pass) Find the highest PCI device id the driver has to handle.
1639 * Upon this information the size of the data structures is
1640 * determined that needs to be allocated.
1641 *
1642 * 2 pass) Initialize the data structures just allocated with the
1643 * information in the ACPI table about available AMD IOMMUs
1644 * in the system. It also maps the PCI devices in the
1645 * system to specific IOMMUs
1646 *
1647 * 3 pass) After the basic data structures are allocated and
1648 * initialized we update them with information about memory
1649 * remapping requirements parsed out of the ACPI table in
1650 * this last pass.
1651 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001652 * After everything is set up the IOMMUs are enabled and the necessary
1653 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02001654 */
Joerg Roedel643511b2012-06-12 12:09:35 +02001655static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001656{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001657 struct acpi_table_header *ivrs_base;
1658 acpi_size ivrs_size;
1659 acpi_status status;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001660 int i, ret = 0;
1661
Joerg Roedel643511b2012-06-12 12:09:35 +02001662 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001663 return -ENODEV;
1664
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001665 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
1666 if (status == AE_NOT_FOUND)
1667 return -ENODEV;
1668 else if (ACPI_FAILURE(status)) {
1669 const char *err = acpi_format_exception(status);
1670 pr_err("AMD-Vi: IVRS table error: %s\n", err);
1671 return -EINVAL;
1672 }
1673
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001674 /*
1675 * First parse ACPI tables to find the largest Bus/Dev/Func
1676 * we need to handle. Upon this information the shared data
1677 * structures for the IOMMUs in the system will be allocated
1678 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001679 ret = find_last_devid_acpi(ivrs_base);
1680 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01001681 goto out;
1682
Joerg Roedelc5714842008-07-11 17:14:25 +02001683 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
1684 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
1685 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001686
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001687 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001688 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001689 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001690 get_order(dev_table_size));
1691 if (amd_iommu_dev_table == NULL)
1692 goto out;
1693
1694 /*
1695 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
1696 * IOMMU see for that device
1697 */
1698 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
1699 get_order(alias_table_size));
1700 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001701 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001702
1703 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01001704 amd_iommu_rlookup_table = (void *)__get_free_pages(
1705 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001706 get_order(rlookup_table_size));
1707 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001708 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001709
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001710 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
1711 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001712 get_order(MAX_DOMAIN_ID/8));
1713 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001714 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001715
1716 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001717 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001718 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001719 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001720 amd_iommu_alias_table[i] = i;
1721
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001722 /*
1723 * never allocate domain 0 because its used as the non-allocated and
1724 * error value placeholder
1725 */
1726 amd_iommu_pd_alloc_bitmap[0] = 1;
1727
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001728 spin_lock_init(&amd_iommu_pd_lock);
1729
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001730 /*
1731 * now the data structures are allocated and basically initialized
1732 * start the real acpi table scan
1733 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001734 ret = init_iommu_all(ivrs_base);
1735 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001736 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001737
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02001738 if (amd_iommu_irq_remap)
1739 amd_iommu_irq_remap = check_ioapic_information();
1740
Joerg Roedel05152a02012-06-15 16:53:51 +02001741 if (amd_iommu_irq_remap) {
1742 /*
1743 * Interrupt remapping enabled, create kmem_cache for the
1744 * remapping tables.
1745 */
1746 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
1747 MAX_IRQS_PER_TABLE * sizeof(u32),
1748 IRQ_TABLE_ALIGNMENT,
1749 0, NULL);
1750 if (!amd_iommu_irq_cache)
1751 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02001752
1753 irq_lookup_table = (void *)__get_free_pages(
1754 GFP_KERNEL | __GFP_ZERO,
1755 get_order(rlookup_table_size));
1756 if (!irq_lookup_table)
1757 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02001758 }
1759
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001760 ret = init_memory_definitions(ivrs_base);
1761 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001762 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01001763
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02001764 /* init the device table */
1765 init_device_table();
1766
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001767out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001768 /* Don't leak any ACPI memory */
1769 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
1770 ivrs_base = NULL;
1771
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001772 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02001773}
1774
Gerard Snitselaarae295142012-03-16 11:38:22 -07001775static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01001776{
1777 struct amd_iommu *iommu;
1778 int ret = 0;
1779
1780 for_each_iommu(iommu) {
1781 ret = iommu_init_msi(iommu);
1782 if (ret)
1783 goto out;
1784 }
1785
1786out:
1787 return ret;
1788}
1789
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001790static bool detect_ivrs(void)
1791{
1792 struct acpi_table_header *ivrs_base;
1793 acpi_size ivrs_size;
1794 acpi_status status;
1795
1796 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
1797 if (status == AE_NOT_FOUND)
1798 return false;
1799 else if (ACPI_FAILURE(status)) {
1800 const char *err = acpi_format_exception(status);
1801 pr_err("AMD-Vi: IVRS table error: %s\n", err);
1802 return false;
1803 }
1804
1805 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
1806
Joerg Roedel1adb7d32012-08-06 14:18:42 +02001807 /* Make sure ACS will be enabled during PCI probe */
1808 pci_request_acs();
1809
Joerg Roedel05152a02012-06-15 16:53:51 +02001810 if (!disable_irq_remap)
1811 amd_iommu_irq_remap = true;
1812
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001813 return true;
1814}
1815
Joerg Roedelb9b1ce702012-06-12 16:51:12 +02001816static int amd_iommu_init_dma(void)
1817{
Joerg Roedel33f28c52012-06-15 18:03:31 +02001818 struct amd_iommu *iommu;
Joerg Roedelb9b1ce702012-06-12 16:51:12 +02001819 int ret;
1820
Joerg Roedel33f28c52012-06-15 18:03:31 +02001821 init_device_table_dma();
1822
1823 for_each_iommu(iommu)
1824 iommu_flush_all_caches(iommu);
1825
Joerg Roedelb9b1ce702012-06-12 16:51:12 +02001826 if (iommu_pass_through)
1827 ret = amd_iommu_init_passthrough();
1828 else
1829 ret = amd_iommu_init_dma_ops();
1830
1831 if (ret)
1832 return ret;
1833
1834 amd_iommu_init_api();
1835
1836 amd_iommu_init_notifier();
1837
1838 return 0;
1839}
1840
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001841/****************************************************************************
1842 *
1843 * AMD IOMMU Initialization State Machine
1844 *
1845 ****************************************************************************/
1846
1847static int __init state_next(void)
1848{
1849 int ret = 0;
1850
1851 switch (init_state) {
1852 case IOMMU_START_STATE:
1853 if (!detect_ivrs()) {
1854 init_state = IOMMU_NOT_FOUND;
1855 ret = -ENODEV;
1856 } else {
1857 init_state = IOMMU_IVRS_DETECTED;
1858 }
1859 break;
1860 case IOMMU_IVRS_DETECTED:
1861 ret = early_amd_iommu_init();
1862 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
1863 break;
1864 case IOMMU_ACPI_FINISHED:
1865 early_enable_iommus();
1866 register_syscore_ops(&amd_iommu_syscore_ops);
1867 x86_platform.iommu_shutdown = disable_iommus;
1868 init_state = IOMMU_ENABLED;
1869 break;
1870 case IOMMU_ENABLED:
1871 ret = amd_iommu_init_pci();
1872 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
1873 enable_iommus_v2();
1874 break;
1875 case IOMMU_PCI_INIT:
1876 ret = amd_iommu_enable_interrupts();
1877 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
1878 break;
1879 case IOMMU_INTERRUPTS_EN:
1880 ret = amd_iommu_init_dma();
1881 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
1882 break;
1883 case IOMMU_DMA_OPS:
1884 init_state = IOMMU_INITIALIZED;
1885 break;
1886 case IOMMU_INITIALIZED:
1887 /* Nothing to do */
1888 break;
1889 case IOMMU_NOT_FOUND:
1890 case IOMMU_INIT_ERROR:
1891 /* Error states => do nothing */
1892 ret = -EINVAL;
1893 break;
1894 default:
1895 /* Unknown state */
1896 BUG();
1897 }
1898
1899 return ret;
1900}
1901
1902static int __init iommu_go_to_state(enum iommu_init_state state)
1903{
1904 int ret = 0;
1905
1906 while (init_state != state) {
1907 ret = state_next();
1908 if (init_state == IOMMU_NOT_FOUND ||
1909 init_state == IOMMU_INIT_ERROR)
1910 break;
1911 }
1912
1913 return ret;
1914}
1915
Joerg Roedel6b474b82012-06-26 16:46:04 +02001916#ifdef CONFIG_IRQ_REMAP
1917int __init amd_iommu_prepare(void)
1918{
1919 return iommu_go_to_state(IOMMU_ACPI_FINISHED);
1920}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001921
Joerg Roedel6b474b82012-06-26 16:46:04 +02001922int __init amd_iommu_supported(void)
1923{
1924 return amd_iommu_irq_remap ? 1 : 0;
1925}
1926
1927int __init amd_iommu_enable(void)
1928{
1929 int ret;
1930
1931 ret = iommu_go_to_state(IOMMU_ENABLED);
1932 if (ret)
1933 return ret;
1934
1935 irq_remapping_enabled = 1;
1936
1937 return 0;
1938}
1939
1940void amd_iommu_disable(void)
1941{
1942 amd_iommu_suspend();
1943}
1944
1945int amd_iommu_reenable(int mode)
1946{
1947 amd_iommu_resume();
1948
1949 return 0;
1950}
1951
1952int __init amd_iommu_enable_faulting(void)
1953{
1954 /* We enable MSI later when PCI is initialized */
1955 return 0;
1956}
1957#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001958
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001959/*
1960 * This is the core init function for AMD IOMMU hardware in the system.
1961 * This function is called from the generic x86 DMA layer initialization
1962 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001963 */
1964static int __init amd_iommu_init(void)
1965{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001966 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001967
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001968 ret = iommu_go_to_state(IOMMU_INITIALIZED);
1969 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001970 free_dma_resources();
1971 if (!irq_remapping_enabled) {
1972 disable_iommus();
1973 free_on_init_error();
1974 } else {
1975 struct amd_iommu *iommu;
1976
1977 uninit_device_table_dma();
1978 for_each_iommu(iommu)
1979 iommu_flush_all_caches(iommu);
1980 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001981 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001982
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001983 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001984}
1985
Joerg Roedelb65233a2008-07-11 17:14:21 +02001986/****************************************************************************
1987 *
1988 * Early detect code. This code runs at IOMMU detection time in the DMA
1989 * layer. It just looks if there is an IVRS ACPI table to detect AMD
1990 * IOMMUs
1991 *
1992 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04001993int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02001994{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001995 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001996
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09001997 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04001998 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02001999
Joerg Roedela5235722010-05-11 17:12:33 +02002000 if (amd_iommu_disabled)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002001 return -ENODEV;
Joerg Roedela5235722010-05-11 17:12:33 +02002002
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002003 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2004 if (ret)
2005 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002006
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002007 amd_iommu_detected = true;
2008 iommu_detected = 1;
2009 x86_init.iommu.iommu_init = amd_iommu_init;
2010
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002011 return 0;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002012}
2013
Joerg Roedelb65233a2008-07-11 17:14:21 +02002014/****************************************************************************
2015 *
2016 * Parsing functions for the AMD IOMMU specific kernel command line
2017 * options.
2018 *
2019 ****************************************************************************/
2020
Joerg Roedelfefda112009-05-20 12:21:42 +02002021static int __init parse_amd_iommu_dump(char *str)
2022{
2023 amd_iommu_dump = true;
2024
2025 return 1;
2026}
2027
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002028static int __init parse_amd_iommu_options(char *str)
2029{
2030 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002031 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002032 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002033 if (strncmp(str, "off", 3) == 0)
2034 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002035 if (strncmp(str, "force_isolation", 15) == 0)
2036 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002037 }
2038
2039 return 1;
2040}
2041
Joerg Roedelfefda112009-05-20 12:21:42 +02002042__setup("amd_iommu_dump", parse_amd_iommu_dump);
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002043__setup("amd_iommu=", parse_amd_iommu_options);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002044
2045IOMMU_INIT_FINISH(amd_iommu_detect,
2046 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002047 NULL,
2048 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002049
2050bool amd_iommu_v2_supported(void)
2051{
2052 return amd_iommu_v2_present;
2053}
2054EXPORT_SYMBOL(amd_iommu_v2_supported);