blob: 1772b3cbebf479a3dd4d95ce8d088daacc76f6aa [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 <acpi/acpi.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 Roedel403f81d2011-06-14 16:44:25 +020035
36#include "amd_iommu_proto.h"
37#include "amd_iommu_types.h"
38
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020039/*
40 * definitions for the ACPI scanning code
41 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020042#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020043
44#define ACPI_IVHD_TYPE 0x10
45#define ACPI_IVMD_TYPE_ALL 0x20
46#define ACPI_IVMD_TYPE 0x21
47#define ACPI_IVMD_TYPE_RANGE 0x22
48
49#define IVHD_DEV_ALL 0x01
50#define IVHD_DEV_SELECT 0x02
51#define IVHD_DEV_SELECT_RANGE_START 0x03
52#define IVHD_DEV_RANGE_END 0x04
53#define IVHD_DEV_ALIAS 0x42
54#define IVHD_DEV_ALIAS_RANGE 0x43
55#define IVHD_DEV_EXT_SELECT 0x46
56#define IVHD_DEV_EXT_SELECT_RANGE 0x47
57
Joerg Roedel6da73422009-05-04 11:44:38 +020058#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
59#define IVHD_FLAG_PASSPW_EN_MASK 0x02
60#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
61#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020062
63#define IVMD_FLAG_EXCL_RANGE 0x08
64#define IVMD_FLAG_UNITY_MAP 0x01
65
66#define ACPI_DEVFLAG_INITPASS 0x01
67#define ACPI_DEVFLAG_EXTINT 0x02
68#define ACPI_DEVFLAG_NMI 0x04
69#define ACPI_DEVFLAG_SYSMGT1 0x10
70#define ACPI_DEVFLAG_SYSMGT2 0x20
71#define ACPI_DEVFLAG_LINT0 0x40
72#define ACPI_DEVFLAG_LINT1 0x80
73#define ACPI_DEVFLAG_ATSDIS 0x10000000
74
Joerg Roedelb65233a2008-07-11 17:14:21 +020075/*
76 * ACPI table definitions
77 *
78 * These data structures are laid over the table to parse the important values
79 * out of it.
80 */
81
82/*
83 * structure describing one IOMMU in the ACPI table. Typically followed by one
84 * or more ivhd_entrys.
85 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020086struct ivhd_header {
87 u8 type;
88 u8 flags;
89 u16 length;
90 u16 devid;
91 u16 cap_ptr;
92 u64 mmio_phys;
93 u16 pci_seg;
94 u16 info;
95 u32 reserved;
96} __attribute__((packed));
97
Joerg Roedelb65233a2008-07-11 17:14:21 +020098/*
99 * A device entry describing which devices a specific IOMMU translates and
100 * which requestor ids they use.
101 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200102struct ivhd_entry {
103 u8 type;
104 u16 devid;
105 u8 flags;
106 u32 ext;
107} __attribute__((packed));
108
Joerg Roedelb65233a2008-07-11 17:14:21 +0200109/*
110 * An AMD IOMMU memory definition structure. It defines things like exclusion
111 * ranges for devices and regions that should be unity mapped.
112 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200113struct ivmd_header {
114 u8 type;
115 u8 flags;
116 u16 length;
117 u16 devid;
118 u16 aux;
119 u64 resv;
120 u64 range_start;
121 u64 range_length;
122} __attribute__((packed));
123
Joerg Roedelfefda112009-05-20 12:21:42 +0200124bool amd_iommu_dump;
125
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200126static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200127static bool __initdata amd_iommu_disabled;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200128
Joerg Roedelb65233a2008-07-11 17:14:21 +0200129u16 amd_iommu_last_bdf; /* largest PCI device id we have
130 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200131LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200132 we find in ACPI */
Dan Carpenter3775d482012-06-27 12:09:18 +0300133u32 amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200134
Joerg Roedel2e228472008-07-11 17:14:31 +0200135LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200136 system */
137
Joerg Roedelbb527772009-11-20 14:31:51 +0100138/* Array to assign indices to IOMMUs*/
139struct amd_iommu *amd_iommus[MAX_IOMMUS];
140int amd_iommus_present;
141
Joerg Roedel318afd42009-11-23 18:32:38 +0100142/* IOMMUs have a non-present cache? */
143bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200144bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100145
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100146u32 amd_iommu_max_pasids __read_mostly = ~0;
147
Joerg Roedel400a28a2011-11-28 15:11:02 +0100148bool amd_iommu_v2_present __read_mostly;
149
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100150bool amd_iommu_force_isolation __read_mostly;
151
Joerg Roedelb65233a2008-07-11 17:14:21 +0200152/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100153 * List of protection domains - used during resume
154 */
155LIST_HEAD(amd_iommu_pd_list);
156spinlock_t amd_iommu_pd_lock;
157
158/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200159 * Pointer to the device table which is shared by all AMD IOMMUs
160 * it is indexed by the PCI device id or the HT unit id and contains
161 * information about the domain the device belongs to as well as the
162 * page table root pointer.
163 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200164struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200165
166/*
167 * The alias table is a driver specific data structure which contains the
168 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
169 * More than one device can share the same requestor id.
170 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200171u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200172
173/*
174 * The rlookup table is used to find the IOMMU which is responsible
175 * for a specific device. It is also indexed by the PCI device id.
176 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200177struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200178
179/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200180 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200181 * to know which ones are already in use.
182 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200183unsigned long *amd_iommu_pd_alloc_bitmap;
184
Joerg Roedelb65233a2008-07-11 17:14:21 +0200185static u32 dev_table_size; /* size of the device table */
186static u32 alias_table_size; /* size of the alias table */
187static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200188
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200189enum iommu_init_state {
190 IOMMU_START_STATE,
191 IOMMU_IVRS_DETECTED,
192 IOMMU_ACPI_FINISHED,
193 IOMMU_ENABLED,
194 IOMMU_PCI_INIT,
195 IOMMU_INTERRUPTS_EN,
196 IOMMU_DMA_OPS,
197 IOMMU_INITIALIZED,
198 IOMMU_NOT_FOUND,
199 IOMMU_INIT_ERROR,
200};
201
202static enum iommu_init_state init_state = IOMMU_START_STATE;
203
Gerard Snitselaarae295142012-03-16 11:38:22 -0700204static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200205static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100206
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200207static inline void update_last_devid(u16 devid)
208{
209 if (devid > amd_iommu_last_bdf)
210 amd_iommu_last_bdf = devid;
211}
212
Joerg Roedelc5714842008-07-11 17:14:25 +0200213static inline unsigned long tbl_size(int entry_size)
214{
215 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100216 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200217
218 return 1UL << shift;
219}
220
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400221/* Access to l1 and l2 indexed register spaces */
222
223static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
224{
225 u32 val;
226
227 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
228 pci_read_config_dword(iommu->dev, 0xfc, &val);
229 return val;
230}
231
232static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
233{
234 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
235 pci_write_config_dword(iommu->dev, 0xfc, val);
236 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
237}
238
239static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
240{
241 u32 val;
242
243 pci_write_config_dword(iommu->dev, 0xf0, address);
244 pci_read_config_dword(iommu->dev, 0xf4, &val);
245 return val;
246}
247
248static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
249{
250 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
251 pci_write_config_dword(iommu->dev, 0xf4, val);
252}
253
Joerg Roedelb65233a2008-07-11 17:14:21 +0200254/****************************************************************************
255 *
256 * AMD IOMMU MMIO register space handling functions
257 *
258 * These functions are used to program the IOMMU device registers in
259 * MMIO space required for that driver.
260 *
261 ****************************************************************************/
262
263/*
264 * This function set the exclusion range in the IOMMU. DMA accesses to the
265 * exclusion range are passed through untranslated
266 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200267static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200268{
269 u64 start = iommu->exclusion_start & PAGE_MASK;
270 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
271 u64 entry;
272
273 if (!iommu->exclusion_start)
274 return;
275
276 entry = start | MMIO_EXCL_ENABLE_MASK;
277 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
278 &entry, sizeof(entry));
279
280 entry = limit;
281 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
282 &entry, sizeof(entry));
283}
284
Joerg Roedelb65233a2008-07-11 17:14:21 +0200285/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000286static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200287{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200288 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200289
290 BUG_ON(iommu->mmio_base == NULL);
291
292 entry = virt_to_phys(amd_iommu_dev_table);
293 entry |= (dev_table_size >> 12) - 1;
294 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
295 &entry, sizeof(entry));
296}
297
Joerg Roedelb65233a2008-07-11 17:14:21 +0200298/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200299static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200300{
301 u32 ctrl;
302
303 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
304 ctrl |= (1 << bit);
305 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
306}
307
Joerg Roedelca0207112009-10-28 18:02:26 +0100308static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200309{
310 u32 ctrl;
311
Joerg Roedel199d0d52008-09-17 16:45:59 +0200312 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200313 ctrl &= ~(1 << bit);
314 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
315}
316
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100317static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
318{
319 u32 ctrl;
320
321 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
322 ctrl &= ~CTRL_INV_TO_MASK;
323 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
324 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
325}
326
Joerg Roedelb65233a2008-07-11 17:14:21 +0200327/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200328static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200329{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200330 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200331}
332
Joerg Roedel92ac4322009-05-19 19:06:27 +0200333static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200334{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200335 /* Disable command buffer */
336 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
337
338 /* Disable event logging and event interrupts */
339 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
340 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
341
342 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200343 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200344}
345
Joerg Roedelb65233a2008-07-11 17:14:21 +0200346/*
347 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
348 * the system has one.
349 */
Joerg Roedel98f1ad22012-07-06 13:28:37 +0200350static u8 __iomem * __init iommu_map_mmio_space(u64 address)
Joerg Roedel6c567472008-06-26 21:27:43 +0200351{
Joerg Roedele82752d2010-05-28 14:26:48 +0200352 if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu")) {
353 pr_err("AMD-Vi: Can not reserve memory region %llx for mmio\n",
354 address);
355 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200356 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200357 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200358
Joerg Roedel98f1ad22012-07-06 13:28:37 +0200359 return (u8 __iomem *)ioremap_nocache(address, MMIO_REGION_LENGTH);
Joerg Roedel6c567472008-06-26 21:27:43 +0200360}
361
362static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
363{
364 if (iommu->mmio_base)
365 iounmap(iommu->mmio_base);
366 release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
367}
368
Joerg Roedelb65233a2008-07-11 17:14:21 +0200369/****************************************************************************
370 *
371 * The functions below belong to the first pass of AMD IOMMU ACPI table
372 * parsing. In this pass we try to find out the highest device id this
373 * code has to handle. Upon this information the size of the shared data
374 * structures is determined later.
375 *
376 ****************************************************************************/
377
378/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200379 * This function calculates the length of a given IVHD entry
380 */
381static inline int ivhd_entry_length(u8 *ivhd)
382{
383 return 0x04 << (*ivhd >> 6);
384}
385
386/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200387 * This function reads the last device id the IOMMU has to handle from the PCI
388 * capability header for this IOMMU
389 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200390static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
391{
392 u32 cap;
393
394 cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
Joerg Roedeld591b0a2008-07-11 17:14:35 +0200395 update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200396
397 return 0;
398}
399
Joerg Roedelb65233a2008-07-11 17:14:21 +0200400/*
401 * After reading the highest device id from the IOMMU PCI capability header
402 * this function looks if there is a higher device id defined in the ACPI table
403 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200404static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
405{
406 u8 *p = (void *)h, *end = (void *)h;
407 struct ivhd_entry *dev;
408
409 p += sizeof(*h);
410 end += h->length;
411
412 find_last_devid_on_pci(PCI_BUS(h->devid),
413 PCI_SLOT(h->devid),
414 PCI_FUNC(h->devid),
415 h->cap_ptr);
416
417 while (p < end) {
418 dev = (struct ivhd_entry *)p;
419 switch (dev->type) {
420 case IVHD_DEV_SELECT:
421 case IVHD_DEV_RANGE_END:
422 case IVHD_DEV_ALIAS:
423 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200424 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200425 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200426 break;
427 default:
428 break;
429 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200430 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200431 }
432
433 WARN_ON(p != end);
434
435 return 0;
436}
437
Joerg Roedelb65233a2008-07-11 17:14:21 +0200438/*
439 * Iterate over all IVHD entries in the ACPI table and find the highest device
440 * id which we need to handle. This is the first of three functions which parse
441 * the ACPI table. So we check the checksum here.
442 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200443static int __init find_last_devid_acpi(struct acpi_table_header *table)
444{
445 int i;
446 u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
447 struct ivhd_header *h;
448
449 /*
450 * Validate checksum here so we don't need to do it when
451 * we actually parse the table
452 */
453 for (i = 0; i < table->length; ++i)
454 checksum += p[i];
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200455 if (checksum != 0)
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200456 /* ACPI table corrupt */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200457 return -ENODEV;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200458
459 p += IVRS_HEADER_LENGTH;
460
461 end += table->length;
462 while (p < end) {
463 h = (struct ivhd_header *)p;
464 switch (h->type) {
465 case ACPI_IVHD_TYPE:
466 find_last_devid_from_ivhd(h);
467 break;
468 default:
469 break;
470 }
471 p += h->length;
472 }
473 WARN_ON(p != end);
474
475 return 0;
476}
477
Joerg Roedelb65233a2008-07-11 17:14:21 +0200478/****************************************************************************
479 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200480 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200481 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
482 * data structures, initialize the device/alias/rlookup table and also
483 * basically initialize the hardware.
484 *
485 ****************************************************************************/
486
487/*
488 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
489 * write commands to that buffer later and the IOMMU will execute them
490 * asynchronously
491 */
Joerg Roedelb36ca912008-06-26 21:27:45 +0200492static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
493{
Joerg Roedeld0312b212008-07-11 17:14:29 +0200494 u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelb36ca912008-06-26 21:27:45 +0200495 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200496
497 if (cmd_buf == NULL)
498 return NULL;
499
Chris Wright549c90dc2010-04-02 18:27:53 -0700500 iommu->cmd_buf_size = CMD_BUFFER_SIZE | CMD_BUFFER_UNINITIALIZED;
Joerg Roedelb36ca912008-06-26 21:27:45 +0200501
Joerg Roedel58492e12009-05-04 18:41:16 +0200502 return cmd_buf;
503}
504
505/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200506 * This function resets the command buffer if the IOMMU stopped fetching
507 * commands from it.
508 */
509void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
510{
511 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
512
513 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
514 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
515
516 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
517}
518
519/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200520 * This function writes the command buffer address to the hardware and
521 * enables it.
522 */
523static void iommu_enable_command_buffer(struct amd_iommu *iommu)
524{
525 u64 entry;
526
527 BUG_ON(iommu->cmd_buf == NULL);
528
529 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200530 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200531
Joerg Roedelb36ca912008-06-26 21:27:45 +0200532 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200533 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200534
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200535 amd_iommu_reset_cmd_buffer(iommu);
Chris Wright549c90dc2010-04-02 18:27:53 -0700536 iommu->cmd_buf_size &= ~(CMD_BUFFER_UNINITIALIZED);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200537}
538
539static void __init free_command_buffer(struct amd_iommu *iommu)
540{
Joerg Roedel23c17132008-09-17 17:18:17 +0200541 free_pages((unsigned long)iommu->cmd_buf,
Chris Wright549c90dc2010-04-02 18:27:53 -0700542 get_order(iommu->cmd_buf_size & ~(CMD_BUFFER_UNINITIALIZED)));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200543}
544
Joerg Roedel335503e2008-09-05 14:29:07 +0200545/* allocates the memory where the IOMMU will log its events to */
546static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
547{
Joerg Roedel335503e2008-09-05 14:29:07 +0200548 iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
549 get_order(EVT_BUFFER_SIZE));
550
551 if (iommu->evt_buf == NULL)
552 return NULL;
553
Joerg Roedel1bc6f832009-07-02 18:32:05 +0200554 iommu->evt_buf_size = EVT_BUFFER_SIZE;
555
Joerg Roedel58492e12009-05-04 18:41:16 +0200556 return iommu->evt_buf;
557}
558
559static void iommu_enable_event_buffer(struct amd_iommu *iommu)
560{
561 u64 entry;
562
563 BUG_ON(iommu->evt_buf == NULL);
564
Joerg Roedel335503e2008-09-05 14:29:07 +0200565 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200566
Joerg Roedel335503e2008-09-05 14:29:07 +0200567 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
568 &entry, sizeof(entry));
569
Joerg Roedel090672072009-06-15 16:06:48 +0200570 /* set head and tail to zero manually */
571 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
572 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
573
Joerg Roedel58492e12009-05-04 18:41:16 +0200574 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200575}
576
577static void __init free_event_buffer(struct amd_iommu *iommu)
578{
579 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
580}
581
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100582/* allocates the memory where the IOMMU will log its events to */
583static u8 * __init alloc_ppr_log(struct amd_iommu *iommu)
584{
585 iommu->ppr_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
586 get_order(PPR_LOG_SIZE));
587
588 if (iommu->ppr_log == NULL)
589 return NULL;
590
591 return iommu->ppr_log;
592}
593
594static void iommu_enable_ppr_log(struct amd_iommu *iommu)
595{
596 u64 entry;
597
598 if (iommu->ppr_log == NULL)
599 return;
600
601 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
602
603 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
604 &entry, sizeof(entry));
605
606 /* set head and tail to zero manually */
607 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
608 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
609
610 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
611 iommu_feature_enable(iommu, CONTROL_PPR_EN);
612}
613
614static void __init free_ppr_log(struct amd_iommu *iommu)
615{
616 if (iommu->ppr_log == NULL)
617 return;
618
619 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
620}
621
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100622static void iommu_enable_gt(struct amd_iommu *iommu)
623{
624 if (!iommu_feature(iommu, FEATURE_GT))
625 return;
626
627 iommu_feature_enable(iommu, CONTROL_GT_EN);
628}
629
Joerg Roedelb65233a2008-07-11 17:14:21 +0200630/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200631static void set_dev_entry_bit(u16 devid, u8 bit)
632{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100633 int i = (bit >> 6) & 0x03;
634 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200635
Joerg Roedelee6c2862011-11-09 12:06:03 +0100636 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200637}
638
Joerg Roedelc5cca142009-10-09 18:31:20 +0200639static int get_dev_entry_bit(u16 devid, u8 bit)
640{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100641 int i = (bit >> 6) & 0x03;
642 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200643
Joerg Roedelee6c2862011-11-09 12:06:03 +0100644 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200645}
646
647
648void amd_iommu_apply_erratum_63(u16 devid)
649{
650 int sysmgt;
651
652 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
653 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
654
655 if (sysmgt == 0x01)
656 set_dev_entry_bit(devid, DEV_ENTRY_IW);
657}
658
Joerg Roedel5ff47892008-07-14 20:11:18 +0200659/* Writes the specific IOMMU for a device into the rlookup table */
660static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
661{
662 amd_iommu_rlookup_table[devid] = iommu;
663}
664
Joerg Roedelb65233a2008-07-11 17:14:21 +0200665/*
666 * This function takes the device specific flags read from the ACPI
667 * table and sets up the device table entry with that information
668 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200669static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
670 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200671{
672 if (flags & ACPI_DEVFLAG_INITPASS)
673 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
674 if (flags & ACPI_DEVFLAG_EXTINT)
675 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
676 if (flags & ACPI_DEVFLAG_NMI)
677 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
678 if (flags & ACPI_DEVFLAG_SYSMGT1)
679 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
680 if (flags & ACPI_DEVFLAG_SYSMGT2)
681 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
682 if (flags & ACPI_DEVFLAG_LINT0)
683 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
684 if (flags & ACPI_DEVFLAG_LINT1)
685 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200686
Joerg Roedelc5cca142009-10-09 18:31:20 +0200687 amd_iommu_apply_erratum_63(devid);
688
Joerg Roedel5ff47892008-07-14 20:11:18 +0200689 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200690}
691
Joerg Roedelb65233a2008-07-11 17:14:21 +0200692/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200693 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +0200694 * it
695 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200696static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
697{
698 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
699
700 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
701 return;
702
703 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200704 /*
705 * We only can configure exclusion ranges per IOMMU, not
706 * per device. But we can enable the exclusion range per
707 * device. This is done here
708 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200709 set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
710 iommu->exclusion_start = m->range_start;
711 iommu->exclusion_length = m->range_length;
712 }
713}
714
Joerg Roedelb65233a2008-07-11 17:14:21 +0200715/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200716 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
717 * initializes the hardware and our data structures with it.
718 */
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200719static void __init init_iommu_from_acpi(struct amd_iommu *iommu,
720 struct ivhd_header *h)
721{
722 u8 *p = (u8 *)h;
723 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +0200724 u16 devid = 0, devid_start = 0, devid_to = 0;
725 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200726 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200727 struct ivhd_entry *e;
728
729 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +0200730 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200731 */
Joerg Roedele9bf5192010-09-20 14:33:07 +0200732 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200733
734 /*
735 * Done. Now parse the device entries
736 */
737 p += sizeof(struct ivhd_header);
738 end += h->length;
739
Joerg Roedel42a698f2009-05-20 15:41:28 +0200740
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200741 while (p < end) {
742 e = (struct ivhd_entry *)p;
743 switch (e->type) {
744 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200745
746 DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
747 " last device %02x:%02x.%x flags: %02x\n",
748 PCI_BUS(iommu->first_device),
749 PCI_SLOT(iommu->first_device),
750 PCI_FUNC(iommu->first_device),
751 PCI_BUS(iommu->last_device),
752 PCI_SLOT(iommu->last_device),
753 PCI_FUNC(iommu->last_device),
754 e->flags);
755
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200756 for (dev_i = iommu->first_device;
757 dev_i <= iommu->last_device; ++dev_i)
Joerg Roedel5ff47892008-07-14 20:11:18 +0200758 set_dev_entry_from_acpi(iommu, dev_i,
759 e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200760 break;
761 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200762
763 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
764 "flags: %02x\n",
765 PCI_BUS(e->devid),
766 PCI_SLOT(e->devid),
767 PCI_FUNC(e->devid),
768 e->flags);
769
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200770 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +0200771 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200772 break;
773 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200774
775 DUMP_printk(" DEV_SELECT_RANGE_START\t "
776 "devid: %02x:%02x.%x flags: %02x\n",
777 PCI_BUS(e->devid),
778 PCI_SLOT(e->devid),
779 PCI_FUNC(e->devid),
780 e->flags);
781
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200782 devid_start = e->devid;
783 flags = e->flags;
784 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200785 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200786 break;
787 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200788
789 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
790 "flags: %02x devid_to: %02x:%02x.%x\n",
791 PCI_BUS(e->devid),
792 PCI_SLOT(e->devid),
793 PCI_FUNC(e->devid),
794 e->flags,
795 PCI_BUS(e->ext >> 8),
796 PCI_SLOT(e->ext >> 8),
797 PCI_FUNC(e->ext >> 8));
798
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200799 devid = e->devid;
800 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200801 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +0100802 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200803 amd_iommu_alias_table[devid] = devid_to;
804 break;
805 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200806
807 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
808 "devid: %02x:%02x.%x flags: %02x "
809 "devid_to: %02x:%02x.%x\n",
810 PCI_BUS(e->devid),
811 PCI_SLOT(e->devid),
812 PCI_FUNC(e->devid),
813 e->flags,
814 PCI_BUS(e->ext >> 8),
815 PCI_SLOT(e->ext >> 8),
816 PCI_FUNC(e->ext >> 8));
817
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200818 devid_start = e->devid;
819 flags = e->flags;
820 devid_to = e->ext >> 8;
821 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200822 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200823 break;
824 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200825
826 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
827 "flags: %02x ext: %08x\n",
828 PCI_BUS(e->devid),
829 PCI_SLOT(e->devid),
830 PCI_FUNC(e->devid),
831 e->flags, e->ext);
832
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200833 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +0200834 set_dev_entry_from_acpi(iommu, devid, e->flags,
835 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200836 break;
837 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200838
839 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
840 "%02x:%02x.%x flags: %02x ext: %08x\n",
841 PCI_BUS(e->devid),
842 PCI_SLOT(e->devid),
843 PCI_FUNC(e->devid),
844 e->flags, e->ext);
845
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200846 devid_start = e->devid;
847 flags = e->flags;
848 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200849 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200850 break;
851 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200852
853 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
854 PCI_BUS(e->devid),
855 PCI_SLOT(e->devid),
856 PCI_FUNC(e->devid));
857
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200858 devid = e->devid;
859 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200860 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200861 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200862 set_dev_entry_from_acpi(iommu,
863 devid_to, flags, ext_flags);
864 }
865 set_dev_entry_from_acpi(iommu, dev_i,
866 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200867 }
868 break;
869 default:
870 break;
871 }
872
Joerg Roedelb514e552008-09-17 17:14:27 +0200873 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200874 }
875}
876
Joerg Roedelb65233a2008-07-11 17:14:21 +0200877/* Initializes the device->iommu mapping for the driver */
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200878static int __init init_iommu_devices(struct amd_iommu *iommu)
879{
Joerg Roedel0de66d52011-06-06 16:04:02 +0200880 u32 i;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200881
882 for (i = iommu->first_device; i <= iommu->last_device; ++i)
883 set_iommu_for_device(iommu, i);
884
885 return 0;
886}
887
Joerg Roedele47d4022008-06-26 21:27:48 +0200888static void __init free_iommu_one(struct amd_iommu *iommu)
889{
890 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +0200891 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100892 free_ppr_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +0200893 iommu_unmap_mmio_space(iommu);
894}
895
896static void __init free_iommu_all(void)
897{
898 struct amd_iommu *iommu, *next;
899
Joerg Roedel3bd22172009-05-04 15:06:20 +0200900 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +0200901 list_del(&iommu->list);
902 free_iommu_one(iommu);
903 kfree(iommu);
904 }
905}
906
Joerg Roedelb65233a2008-07-11 17:14:21 +0200907/*
908 * This function clues the initialization function for one IOMMU
909 * together and also allocates the command buffer and programs the
910 * hardware. It does NOT enable the IOMMU. This is done afterwards.
911 */
Joerg Roedele47d4022008-06-26 21:27:48 +0200912static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
913{
914 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +0100915
916 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +0200917 list_add_tail(&iommu->list, &amd_iommu_list);
Joerg Roedelbb527772009-11-20 14:31:51 +0100918 iommu->index = amd_iommus_present++;
919
920 if (unlikely(iommu->index >= MAX_IOMMUS)) {
921 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
922 return -ENOSYS;
923 }
924
925 /* Index is fine - add IOMMU to the array */
926 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +0200927
928 /*
929 * Copy data from ACPI table entry to the iommu struct
930 */
Joerg Roedel23c742d2012-06-12 11:47:34 +0200931 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +0200932 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +0200933 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +0200934 iommu->mmio_phys = h->mmio_phys;
935 iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
936 if (!iommu->mmio_base)
937 return -ENOMEM;
938
Joerg Roedele47d4022008-06-26 21:27:48 +0200939 iommu->cmd_buf = alloc_command_buffer(iommu);
940 if (!iommu->cmd_buf)
941 return -ENOMEM;
942
Joerg Roedel335503e2008-09-05 14:29:07 +0200943 iommu->evt_buf = alloc_event_buffer(iommu);
944 if (!iommu->evt_buf)
945 return -ENOMEM;
946
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200947 iommu->int_enabled = false;
948
Joerg Roedele47d4022008-06-26 21:27:48 +0200949 init_iommu_from_acpi(iommu, h);
950 init_iommu_devices(iommu);
951
Joerg Roedel23c742d2012-06-12 11:47:34 +0200952 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +0200953}
954
Joerg Roedelb65233a2008-07-11 17:14:21 +0200955/*
956 * Iterates over all IOMMU entries in the ACPI table, allocates the
957 * IOMMU structure and initializes it with init_iommu_one()
958 */
Joerg Roedele47d4022008-06-26 21:27:48 +0200959static int __init init_iommu_all(struct acpi_table_header *table)
960{
961 u8 *p = (u8 *)table, *end = (u8 *)table;
962 struct ivhd_header *h;
963 struct amd_iommu *iommu;
964 int ret;
965
Joerg Roedele47d4022008-06-26 21:27:48 +0200966 end += table->length;
967 p += IVRS_HEADER_LENGTH;
968
969 while (p < end) {
970 h = (struct ivhd_header *)p;
971 switch (*p) {
972 case ACPI_IVHD_TYPE:
Joerg Roedel9c720412009-05-20 13:53:57 +0200973
Joerg Roedelae908c22009-09-01 16:52:16 +0200974 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +0200975 "seg: %d flags: %01x info %04x\n",
976 PCI_BUS(h->devid), PCI_SLOT(h->devid),
977 PCI_FUNC(h->devid), h->cap_ptr,
978 h->pci_seg, h->flags, h->info);
979 DUMP_printk(" mmio-addr: %016llx\n",
980 h->mmio_phys);
981
Joerg Roedele47d4022008-06-26 21:27:48 +0200982 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200983 if (iommu == NULL)
984 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +0100985
Joerg Roedele47d4022008-06-26 21:27:48 +0200986 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200987 if (ret)
988 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +0200989 break;
990 default:
991 break;
992 }
993 p += h->length;
994
995 }
996 WARN_ON(p != end);
997
998 return 0;
999}
1000
Joerg Roedel23c742d2012-06-12 11:47:34 +02001001static int iommu_init_pci(struct amd_iommu *iommu)
1002{
1003 int cap_ptr = iommu->cap_ptr;
1004 u32 range, misc, low, high;
1005
1006 iommu->dev = pci_get_bus_and_slot(PCI_BUS(iommu->devid),
1007 iommu->devid & 0xff);
1008 if (!iommu->dev)
1009 return -ENODEV;
1010
1011 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1012 &iommu->cap);
1013 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1014 &range);
1015 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1016 &misc);
1017
1018 iommu->first_device = calc_devid(MMIO_GET_BUS(range),
1019 MMIO_GET_FD(range));
1020 iommu->last_device = calc_devid(MMIO_GET_BUS(range),
1021 MMIO_GET_LD(range));
1022
1023 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1024 amd_iommu_iotlb_sup = false;
1025
1026 /* read extended feature bits */
1027 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1028 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1029
1030 iommu->features = ((u64)high << 32) | low;
1031
1032 if (iommu_feature(iommu, FEATURE_GT)) {
1033 int glxval;
1034 u32 pasids;
1035 u64 shift;
1036
1037 shift = iommu->features & FEATURE_PASID_MASK;
1038 shift >>= FEATURE_PASID_SHIFT;
1039 pasids = (1 << shift);
1040
1041 amd_iommu_max_pasids = min(amd_iommu_max_pasids, pasids);
1042
1043 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1044 glxval >>= FEATURE_GLXVAL_SHIFT;
1045
1046 if (amd_iommu_max_glx_val == -1)
1047 amd_iommu_max_glx_val = glxval;
1048 else
1049 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1050 }
1051
1052 if (iommu_feature(iommu, FEATURE_GT) &&
1053 iommu_feature(iommu, FEATURE_PPR)) {
1054 iommu->is_iommu_v2 = true;
1055 amd_iommu_v2_present = true;
1056 }
1057
1058 if (iommu_feature(iommu, FEATURE_PPR)) {
1059 iommu->ppr_log = alloc_ppr_log(iommu);
1060 if (!iommu->ppr_log)
1061 return -ENOMEM;
1062 }
1063
1064 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1065 amd_iommu_np_cache = true;
1066
1067 if (is_rd890_iommu(iommu->dev)) {
1068 int i, j;
1069
1070 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1071 PCI_DEVFN(0, 0));
1072
1073 /*
1074 * Some rd890 systems may not be fully reconfigured by the
1075 * BIOS, so it's necessary for us to store this information so
1076 * it can be reprogrammed on resume
1077 */
1078 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1079 &iommu->stored_addr_lo);
1080 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1081 &iommu->stored_addr_hi);
1082
1083 /* Low bit locks writes to configuration space */
1084 iommu->stored_addr_lo &= ~1;
1085
1086 for (i = 0; i < 6; i++)
1087 for (j = 0; j < 0x12; j++)
1088 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1089
1090 for (i = 0; i < 0x83; i++)
1091 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1092 }
1093
1094 return pci_enable_device(iommu->dev);
1095}
1096
Joerg Roedel4d121c32012-06-14 12:21:55 +02001097static void print_iommu_info(void)
1098{
1099 static const char * const feat_str[] = {
1100 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1101 "IA", "GA", "HE", "PC"
1102 };
1103 struct amd_iommu *iommu;
1104
1105 for_each_iommu(iommu) {
1106 int i;
1107
1108 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1109 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1110
1111 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1112 pr_info("AMD-Vi: Extended features: ");
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001113 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001114 if (iommu_feature(iommu, (1ULL << i)))
1115 pr_cont(" %s", feat_str[i]);
1116 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001117 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001118 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001119 }
1120}
1121
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001122static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001123{
1124 struct amd_iommu *iommu;
1125 int ret = 0;
1126
1127 for_each_iommu(iommu) {
1128 ret = iommu_init_pci(iommu);
1129 if (ret)
1130 break;
1131 }
1132
Joerg Roedel23c742d2012-06-12 11:47:34 +02001133 ret = amd_iommu_init_devices();
1134
Joerg Roedel4d121c32012-06-14 12:21:55 +02001135 print_iommu_info();
1136
Joerg Roedel23c742d2012-06-12 11:47:34 +02001137 return ret;
1138}
1139
Joerg Roedelb65233a2008-07-11 17:14:21 +02001140/****************************************************************************
1141 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001142 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001143 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001144 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1145 * pci_dev.
1146 *
1147 ****************************************************************************/
1148
Joerg Roedel9f800de2009-11-23 12:45:25 +01001149static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001150{
1151 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001152
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001153 r = pci_enable_msi(iommu->dev);
1154 if (r)
1155 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001156
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001157 r = request_threaded_irq(iommu->dev->irq,
1158 amd_iommu_int_handler,
1159 amd_iommu_int_thread,
1160 0, "AMD-Vi",
1161 iommu->dev);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001162
1163 if (r) {
1164 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001165 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001166 }
1167
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001168 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001169
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001170 return 0;
1171}
1172
Joerg Roedel05f92db2009-05-12 09:52:46 +02001173static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001174{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001175 int ret;
1176
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001177 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001178 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001179
Joerg Roedeld91cecd2009-05-04 18:51:00 +02001180 if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001181 ret = iommu_setup_msi(iommu);
1182 else
1183 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001184
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001185 if (ret)
1186 return ret;
1187
1188enable_faults:
1189 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1190
1191 if (iommu->ppr_log != NULL)
1192 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1193
1194 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001195}
1196
1197/****************************************************************************
1198 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001199 * The next functions belong to the third pass of parsing the ACPI
1200 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001201 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001202 *
1203 ****************************************************************************/
1204
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001205static void __init free_unity_maps(void)
1206{
1207 struct unity_map_entry *entry, *next;
1208
1209 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1210 list_del(&entry->list);
1211 kfree(entry);
1212 }
1213}
1214
Joerg Roedelb65233a2008-07-11 17:14:21 +02001215/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001216static int __init init_exclusion_range(struct ivmd_header *m)
1217{
1218 int i;
1219
1220 switch (m->type) {
1221 case ACPI_IVMD_TYPE:
1222 set_device_exclusion_range(m->devid, m);
1223 break;
1224 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001225 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001226 set_device_exclusion_range(i, m);
1227 break;
1228 case ACPI_IVMD_TYPE_RANGE:
1229 for (i = m->devid; i <= m->aux; ++i)
1230 set_device_exclusion_range(i, m);
1231 break;
1232 default:
1233 break;
1234 }
1235
1236 return 0;
1237}
1238
Joerg Roedelb65233a2008-07-11 17:14:21 +02001239/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001240static int __init init_unity_map_range(struct ivmd_header *m)
1241{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001242 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001243 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001244
1245 e = kzalloc(sizeof(*e), GFP_KERNEL);
1246 if (e == NULL)
1247 return -ENOMEM;
1248
1249 switch (m->type) {
1250 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001251 kfree(e);
1252 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001253 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001254 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001255 e->devid_start = e->devid_end = m->devid;
1256 break;
1257 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001258 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001259 e->devid_start = 0;
1260 e->devid_end = amd_iommu_last_bdf;
1261 break;
1262 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001263 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001264 e->devid_start = m->devid;
1265 e->devid_end = m->aux;
1266 break;
1267 }
1268 e->address_start = PAGE_ALIGN(m->range_start);
1269 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1270 e->prot = m->flags >> 1;
1271
Joerg Roedel02acc432009-05-20 16:24:21 +02001272 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1273 " range_start: %016llx range_end: %016llx flags: %x\n", s,
1274 PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
1275 PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
1276 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1277 e->address_start, e->address_end, m->flags);
1278
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001279 list_add_tail(&e->list, &amd_iommu_unity_map);
1280
1281 return 0;
1282}
1283
Joerg Roedelb65233a2008-07-11 17:14:21 +02001284/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001285static int __init init_memory_definitions(struct acpi_table_header *table)
1286{
1287 u8 *p = (u8 *)table, *end = (u8 *)table;
1288 struct ivmd_header *m;
1289
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001290 end += table->length;
1291 p += IVRS_HEADER_LENGTH;
1292
1293 while (p < end) {
1294 m = (struct ivmd_header *)p;
1295 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1296 init_exclusion_range(m);
1297 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1298 init_unity_map_range(m);
1299
1300 p += m->length;
1301 }
1302
1303 return 0;
1304}
1305
Joerg Roedelb65233a2008-07-11 17:14:21 +02001306/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001307 * Init the device table to not allow DMA access for devices and
1308 * suppress all page faults
1309 */
1310static void init_device_table(void)
1311{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001312 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001313
1314 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1315 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1316 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001317 }
1318}
1319
Joerg Roedele9bf5192010-09-20 14:33:07 +02001320static void iommu_init_flags(struct amd_iommu *iommu)
1321{
1322 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1323 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1324 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1325
1326 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1327 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1328 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1329
1330 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1331 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1332 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1333
1334 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1335 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1336 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1337
1338 /*
1339 * make IOMMU memory accesses cache coherent
1340 */
1341 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001342
1343 /* Set IOTLB invalidation timeout to 1s */
1344 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001345}
1346
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001347static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001348{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001349 int i, j;
1350 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001351 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001352
1353 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001354 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001355 return;
1356
1357 /*
1358 * First, we need to ensure that the iommu is enabled. This is
1359 * controlled by a register in the northbridge
1360 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001361
1362 /* Select Northbridge indirect register 0x75 and enable writing */
1363 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1364 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1365
1366 /* Enable the iommu */
1367 if (!(ioc_feature_control & 0x1))
1368 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1369
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001370 /* Restore the iommu BAR */
1371 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1372 iommu->stored_addr_lo);
1373 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1374 iommu->stored_addr_hi);
1375
1376 /* Restore the l1 indirect regs for each of the 6 l1s */
1377 for (i = 0; i < 6; i++)
1378 for (j = 0; j < 0x12; j++)
1379 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1380
1381 /* Restore the l2 indirect regs */
1382 for (i = 0; i < 0x83; i++)
1383 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
1384
1385 /* Lock PCI setup registers */
1386 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1387 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02001388}
1389
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001390/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001391 * This function finally enables all IOMMUs found in the system after
1392 * they have been initialized
1393 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02001394static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02001395{
1396 struct amd_iommu *iommu;
1397
Joerg Roedel3bd22172009-05-04 15:06:20 +02001398 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02001399 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001400 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02001401 iommu_set_device_table(iommu);
1402 iommu_enable_command_buffer(iommu);
1403 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001404 iommu_set_exclusion_range(iommu);
1405 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001406 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001407 }
1408}
1409
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02001410static void enable_iommus_v2(void)
1411{
1412 struct amd_iommu *iommu;
1413
1414 for_each_iommu(iommu) {
1415 iommu_enable_ppr_log(iommu);
1416 iommu_enable_gt(iommu);
1417 }
1418}
1419
1420static void enable_iommus(void)
1421{
1422 early_enable_iommus();
1423
1424 enable_iommus_v2();
1425}
1426
Joerg Roedel92ac4322009-05-19 19:06:27 +02001427static void disable_iommus(void)
1428{
1429 struct amd_iommu *iommu;
1430
1431 for_each_iommu(iommu)
1432 iommu_disable(iommu);
1433}
1434
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001435/*
1436 * Suspend/Resume support
1437 * disable suspend until real resume implemented
1438 */
1439
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001440static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001441{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001442 struct amd_iommu *iommu;
1443
1444 for_each_iommu(iommu)
1445 iommu_apply_resume_quirks(iommu);
1446
Joerg Roedel736501e2009-05-12 09:56:12 +02001447 /* re-load the hardware */
1448 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01001449
1450 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001451}
1452
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001453static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001454{
Joerg Roedel736501e2009-05-12 09:56:12 +02001455 /* disable IOMMUs to go out of the way for BIOS */
1456 disable_iommus();
1457
1458 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001459}
1460
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001461static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001462 .suspend = amd_iommu_suspend,
1463 .resume = amd_iommu_resume,
1464};
1465
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001466static void __init free_on_init_error(void)
1467{
1468 amd_iommu_uninit_devices();
1469
1470 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
1471 get_order(MAX_DOMAIN_ID/8));
1472
1473 free_pages((unsigned long)amd_iommu_rlookup_table,
1474 get_order(rlookup_table_size));
1475
1476 free_pages((unsigned long)amd_iommu_alias_table,
1477 get_order(alias_table_size));
1478
1479 free_pages((unsigned long)amd_iommu_dev_table,
1480 get_order(dev_table_size));
1481
1482 free_iommu_all();
1483
1484 free_unity_maps();
1485
1486#ifdef CONFIG_GART_IOMMU
1487 /*
1488 * We failed to initialize the AMD IOMMU - try fallback to GART
1489 * if possible.
1490 */
1491 gart_iommu_init();
1492
1493#endif
1494}
1495
Joerg Roedelb65233a2008-07-11 17:14:21 +02001496/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001497 * This is the hardware init function for AMD IOMMU in the system.
1498 * This function is called either from amd_iommu_init or from the interrupt
1499 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02001500 *
1501 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
1502 * three times:
1503 *
1504 * 1 pass) Find the highest PCI device id the driver has to handle.
1505 * Upon this information the size of the data structures is
1506 * determined that needs to be allocated.
1507 *
1508 * 2 pass) Initialize the data structures just allocated with the
1509 * information in the ACPI table about available AMD IOMMUs
1510 * in the system. It also maps the PCI devices in the
1511 * system to specific IOMMUs
1512 *
1513 * 3 pass) After the basic data structures are allocated and
1514 * initialized we update them with information about memory
1515 * remapping requirements parsed out of the ACPI table in
1516 * this last pass.
1517 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001518 * After everything is set up the IOMMUs are enabled and the necessary
1519 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02001520 */
Joerg Roedel643511b2012-06-12 12:09:35 +02001521static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001522{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001523 struct acpi_table_header *ivrs_base;
1524 acpi_size ivrs_size;
1525 acpi_status status;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001526 int i, ret = 0;
1527
Joerg Roedel643511b2012-06-12 12:09:35 +02001528 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001529 return -ENODEV;
1530
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001531 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
1532 if (status == AE_NOT_FOUND)
1533 return -ENODEV;
1534 else if (ACPI_FAILURE(status)) {
1535 const char *err = acpi_format_exception(status);
1536 pr_err("AMD-Vi: IVRS table error: %s\n", err);
1537 return -EINVAL;
1538 }
1539
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001540 /*
1541 * First parse ACPI tables to find the largest Bus/Dev/Func
1542 * we need to handle. Upon this information the shared data
1543 * structures for the IOMMUs in the system will be allocated
1544 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001545 ret = find_last_devid_acpi(ivrs_base);
1546 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01001547 goto out;
1548
Joerg Roedelc5714842008-07-11 17:14:25 +02001549 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
1550 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
1551 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001552
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001553 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001554 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001555 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001556 get_order(dev_table_size));
1557 if (amd_iommu_dev_table == NULL)
1558 goto out;
1559
1560 /*
1561 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
1562 * IOMMU see for that device
1563 */
1564 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
1565 get_order(alias_table_size));
1566 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001567 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001568
1569 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01001570 amd_iommu_rlookup_table = (void *)__get_free_pages(
1571 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001572 get_order(rlookup_table_size));
1573 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001574 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001575
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001576 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
1577 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001578 get_order(MAX_DOMAIN_ID/8));
1579 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001580 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001581
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001582 /* init the device table */
1583 init_device_table();
1584
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001585 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001586 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001587 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001588 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001589 amd_iommu_alias_table[i] = i;
1590
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001591 /*
1592 * never allocate domain 0 because its used as the non-allocated and
1593 * error value placeholder
1594 */
1595 amd_iommu_pd_alloc_bitmap[0] = 1;
1596
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001597 spin_lock_init(&amd_iommu_pd_lock);
1598
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001599 /*
1600 * now the data structures are allocated and basically initialized
1601 * start the real acpi table scan
1602 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001603 ret = init_iommu_all(ivrs_base);
1604 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001605 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001606
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001607 ret = init_memory_definitions(ivrs_base);
1608 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001609 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01001610
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001611out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001612 /* Don't leak any ACPI memory */
1613 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
1614 ivrs_base = NULL;
1615
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001616 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02001617}
1618
Gerard Snitselaarae295142012-03-16 11:38:22 -07001619static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01001620{
1621 struct amd_iommu *iommu;
1622 int ret = 0;
1623
1624 for_each_iommu(iommu) {
1625 ret = iommu_init_msi(iommu);
1626 if (ret)
1627 goto out;
1628 }
1629
1630out:
1631 return ret;
1632}
1633
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001634static bool detect_ivrs(void)
1635{
1636 struct acpi_table_header *ivrs_base;
1637 acpi_size ivrs_size;
1638 acpi_status status;
1639
1640 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
1641 if (status == AE_NOT_FOUND)
1642 return false;
1643 else if (ACPI_FAILURE(status)) {
1644 const char *err = acpi_format_exception(status);
1645 pr_err("AMD-Vi: IVRS table error: %s\n", err);
1646 return false;
1647 }
1648
1649 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
1650
Joerg Roedel1adb7d32012-08-06 14:18:42 +02001651 /* Make sure ACS will be enabled during PCI probe */
1652 pci_request_acs();
1653
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001654 return true;
1655}
1656
Joerg Roedelb9b1ce702012-06-12 16:51:12 +02001657static int amd_iommu_init_dma(void)
1658{
1659 int ret;
1660
1661 if (iommu_pass_through)
1662 ret = amd_iommu_init_passthrough();
1663 else
1664 ret = amd_iommu_init_dma_ops();
1665
1666 if (ret)
1667 return ret;
1668
1669 amd_iommu_init_api();
1670
1671 amd_iommu_init_notifier();
1672
1673 return 0;
1674}
1675
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001676/****************************************************************************
1677 *
1678 * AMD IOMMU Initialization State Machine
1679 *
1680 ****************************************************************************/
1681
1682static int __init state_next(void)
1683{
1684 int ret = 0;
1685
1686 switch (init_state) {
1687 case IOMMU_START_STATE:
1688 if (!detect_ivrs()) {
1689 init_state = IOMMU_NOT_FOUND;
1690 ret = -ENODEV;
1691 } else {
1692 init_state = IOMMU_IVRS_DETECTED;
1693 }
1694 break;
1695 case IOMMU_IVRS_DETECTED:
1696 ret = early_amd_iommu_init();
1697 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
1698 break;
1699 case IOMMU_ACPI_FINISHED:
1700 early_enable_iommus();
1701 register_syscore_ops(&amd_iommu_syscore_ops);
1702 x86_platform.iommu_shutdown = disable_iommus;
1703 init_state = IOMMU_ENABLED;
1704 break;
1705 case IOMMU_ENABLED:
1706 ret = amd_iommu_init_pci();
1707 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
1708 enable_iommus_v2();
1709 break;
1710 case IOMMU_PCI_INIT:
1711 ret = amd_iommu_enable_interrupts();
1712 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
1713 break;
1714 case IOMMU_INTERRUPTS_EN:
1715 ret = amd_iommu_init_dma();
1716 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
1717 break;
1718 case IOMMU_DMA_OPS:
1719 init_state = IOMMU_INITIALIZED;
1720 break;
1721 case IOMMU_INITIALIZED:
1722 /* Nothing to do */
1723 break;
1724 case IOMMU_NOT_FOUND:
1725 case IOMMU_INIT_ERROR:
1726 /* Error states => do nothing */
1727 ret = -EINVAL;
1728 break;
1729 default:
1730 /* Unknown state */
1731 BUG();
1732 }
1733
1734 return ret;
1735}
1736
1737static int __init iommu_go_to_state(enum iommu_init_state state)
1738{
1739 int ret = 0;
1740
1741 while (init_state != state) {
1742 ret = state_next();
1743 if (init_state == IOMMU_NOT_FOUND ||
1744 init_state == IOMMU_INIT_ERROR)
1745 break;
1746 }
1747
1748 return ret;
1749}
1750
1751
1752
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001753/*
1754 * This is the core init function for AMD IOMMU hardware in the system.
1755 * This function is called from the generic x86 DMA layer initialization
1756 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001757 */
1758static int __init amd_iommu_init(void)
1759{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001760 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001761
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001762 ret = iommu_go_to_state(IOMMU_INITIALIZED);
1763 if (ret) {
1764 disable_iommus();
1765 free_on_init_error();
1766 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001767
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001768 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001769}
1770
Joerg Roedelb65233a2008-07-11 17:14:21 +02001771/****************************************************************************
1772 *
1773 * Early detect code. This code runs at IOMMU detection time in the DMA
1774 * layer. It just looks if there is an IVRS ACPI table to detect AMD
1775 * IOMMUs
1776 *
1777 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04001778int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02001779{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001780 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001781
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09001782 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04001783 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02001784
Joerg Roedela5235722010-05-11 17:12:33 +02001785 if (amd_iommu_disabled)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04001786 return -ENODEV;
Joerg Roedela5235722010-05-11 17:12:33 +02001787
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001788 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
1789 if (ret)
1790 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08001791
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001792 amd_iommu_detected = true;
1793 iommu_detected = 1;
1794 x86_init.iommu.iommu_init = amd_iommu_init;
1795
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001796 return 0;
Joerg Roedelae7877d2008-06-26 21:27:51 +02001797}
1798
Joerg Roedelb65233a2008-07-11 17:14:21 +02001799/****************************************************************************
1800 *
1801 * Parsing functions for the AMD IOMMU specific kernel command line
1802 * options.
1803 *
1804 ****************************************************************************/
1805
Joerg Roedelfefda112009-05-20 12:21:42 +02001806static int __init parse_amd_iommu_dump(char *str)
1807{
1808 amd_iommu_dump = true;
1809
1810 return 1;
1811}
1812
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001813static int __init parse_amd_iommu_options(char *str)
1814{
1815 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01001816 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09001817 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02001818 if (strncmp(str, "off", 3) == 0)
1819 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001820 if (strncmp(str, "force_isolation", 15) == 0)
1821 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001822 }
1823
1824 return 1;
1825}
1826
Joerg Roedelfefda112009-05-20 12:21:42 +02001827__setup("amd_iommu_dump", parse_amd_iommu_dump);
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001828__setup("amd_iommu=", parse_amd_iommu_options);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04001829
1830IOMMU_INIT_FINISH(amd_iommu_detect,
1831 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001832 NULL,
1833 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01001834
1835bool amd_iommu_v2_supported(void)
1836{
1837 return amd_iommu_v2_present;
1838}
1839EXPORT_SYMBOL(amd_iommu_v2_supported);