blob: 617985961195c870157f6a0a490682e5c788dddb [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 Roedel403f81d2011-06-14 16:44:25 +020036
37#include "amd_iommu_proto.h"
38#include "amd_iommu_types.h"
39
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020040/*
41 * definitions for the ACPI scanning code
42 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020043#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020044
45#define ACPI_IVHD_TYPE 0x10
46#define ACPI_IVMD_TYPE_ALL 0x20
47#define ACPI_IVMD_TYPE 0x21
48#define ACPI_IVMD_TYPE_RANGE 0x22
49
50#define IVHD_DEV_ALL 0x01
51#define IVHD_DEV_SELECT 0x02
52#define IVHD_DEV_SELECT_RANGE_START 0x03
53#define IVHD_DEV_RANGE_END 0x04
54#define IVHD_DEV_ALIAS 0x42
55#define IVHD_DEV_ALIAS_RANGE 0x43
56#define IVHD_DEV_EXT_SELECT 0x46
57#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020058#define IVHD_DEV_SPECIAL 0x48
59
60#define IVHD_SPECIAL_IOAPIC 1
61#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020062
Joerg Roedel6da73422009-05-04 11:44:38 +020063#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
64#define IVHD_FLAG_PASSPW_EN_MASK 0x02
65#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
66#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020067
68#define IVMD_FLAG_EXCL_RANGE 0x08
69#define IVMD_FLAG_UNITY_MAP 0x01
70
71#define ACPI_DEVFLAG_INITPASS 0x01
72#define ACPI_DEVFLAG_EXTINT 0x02
73#define ACPI_DEVFLAG_NMI 0x04
74#define ACPI_DEVFLAG_SYSMGT1 0x10
75#define ACPI_DEVFLAG_SYSMGT2 0x20
76#define ACPI_DEVFLAG_LINT0 0x40
77#define ACPI_DEVFLAG_LINT1 0x80
78#define ACPI_DEVFLAG_ATSDIS 0x10000000
79
Joerg Roedelb65233a2008-07-11 17:14:21 +020080/*
81 * ACPI table definitions
82 *
83 * These data structures are laid over the table to parse the important values
84 * out of it.
85 */
86
87/*
88 * structure describing one IOMMU in the ACPI table. Typically followed by one
89 * or more ivhd_entrys.
90 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020091struct ivhd_header {
92 u8 type;
93 u8 flags;
94 u16 length;
95 u16 devid;
96 u16 cap_ptr;
97 u64 mmio_phys;
98 u16 pci_seg;
99 u16 info;
100 u32 reserved;
101} __attribute__((packed));
102
Joerg Roedelb65233a2008-07-11 17:14:21 +0200103/*
104 * A device entry describing which devices a specific IOMMU translates and
105 * which requestor ids they use.
106 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200107struct ivhd_entry {
108 u8 type;
109 u16 devid;
110 u8 flags;
111 u32 ext;
112} __attribute__((packed));
113
Joerg Roedelb65233a2008-07-11 17:14:21 +0200114/*
115 * An AMD IOMMU memory definition structure. It defines things like exclusion
116 * ranges for devices and regions that should be unity mapped.
117 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200118struct ivmd_header {
119 u8 type;
120 u8 flags;
121 u16 length;
122 u16 devid;
123 u16 aux;
124 u64 resv;
125 u64 range_start;
126 u64 range_length;
127} __attribute__((packed));
128
Joerg Roedelfefda112009-05-20 12:21:42 +0200129bool amd_iommu_dump;
130
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200131static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200132static bool __initdata amd_iommu_disabled;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200133
Joerg Roedelb65233a2008-07-11 17:14:21 +0200134u16 amd_iommu_last_bdf; /* largest PCI device id we have
135 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200136LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200137 we find in ACPI */
Dan Carpenter3775d482012-06-27 12:09:18 +0300138u32 amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200139
Joerg Roedel2e228472008-07-11 17:14:31 +0200140LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200141 system */
142
Joerg Roedelbb527772009-11-20 14:31:51 +0100143/* Array to assign indices to IOMMUs*/
144struct amd_iommu *amd_iommus[MAX_IOMMUS];
145int amd_iommus_present;
146
Joerg Roedel318afd42009-11-23 18:32:38 +0100147/* IOMMUs have a non-present cache? */
148bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200149bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100150
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100151u32 amd_iommu_max_pasids __read_mostly = ~0;
152
Joerg Roedel400a28a2011-11-28 15:11:02 +0100153bool amd_iommu_v2_present __read_mostly;
154
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100155bool amd_iommu_force_isolation __read_mostly;
156
Joerg Roedelb65233a2008-07-11 17:14:21 +0200157/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100158 * List of protection domains - used during resume
159 */
160LIST_HEAD(amd_iommu_pd_list);
161spinlock_t amd_iommu_pd_lock;
162
163/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200164 * Pointer to the device table which is shared by all AMD IOMMUs
165 * it is indexed by the PCI device id or the HT unit id and contains
166 * information about the domain the device belongs to as well as the
167 * page table root pointer.
168 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200169struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200170
171/*
172 * The alias table is a driver specific data structure which contains the
173 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
174 * More than one device can share the same requestor id.
175 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200176u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200177
178/*
179 * The rlookup table is used to find the IOMMU which is responsible
180 * for a specific device. It is also indexed by the PCI device id.
181 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200182struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200183
184/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200185 * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
186 * to know which ones are already in use.
187 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200188unsigned long *amd_iommu_pd_alloc_bitmap;
189
Joerg Roedelb65233a2008-07-11 17:14:21 +0200190static u32 dev_table_size; /* size of the device table */
191static u32 alias_table_size; /* size of the alias table */
192static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200193
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200194enum iommu_init_state {
195 IOMMU_START_STATE,
196 IOMMU_IVRS_DETECTED,
197 IOMMU_ACPI_FINISHED,
198 IOMMU_ENABLED,
199 IOMMU_PCI_INIT,
200 IOMMU_INTERRUPTS_EN,
201 IOMMU_DMA_OPS,
202 IOMMU_INITIALIZED,
203 IOMMU_NOT_FOUND,
204 IOMMU_INIT_ERROR,
205};
206
207static enum iommu_init_state init_state = IOMMU_START_STATE;
208
Gerard Snitselaarae295142012-03-16 11:38:22 -0700209static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200210static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100211
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200212static inline void update_last_devid(u16 devid)
213{
214 if (devid > amd_iommu_last_bdf)
215 amd_iommu_last_bdf = devid;
216}
217
Joerg Roedelc5714842008-07-11 17:14:25 +0200218static inline unsigned long tbl_size(int entry_size)
219{
220 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100221 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200222
223 return 1UL << shift;
224}
225
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400226/* Access to l1 and l2 indexed register spaces */
227
228static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
229{
230 u32 val;
231
232 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
233 pci_read_config_dword(iommu->dev, 0xfc, &val);
234 return val;
235}
236
237static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
238{
239 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
240 pci_write_config_dword(iommu->dev, 0xfc, val);
241 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
242}
243
244static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
245{
246 u32 val;
247
248 pci_write_config_dword(iommu->dev, 0xf0, address);
249 pci_read_config_dword(iommu->dev, 0xf4, &val);
250 return val;
251}
252
253static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
254{
255 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
256 pci_write_config_dword(iommu->dev, 0xf4, val);
257}
258
Joerg Roedelb65233a2008-07-11 17:14:21 +0200259/****************************************************************************
260 *
261 * AMD IOMMU MMIO register space handling functions
262 *
263 * These functions are used to program the IOMMU device registers in
264 * MMIO space required for that driver.
265 *
266 ****************************************************************************/
267
268/*
269 * This function set the exclusion range in the IOMMU. DMA accesses to the
270 * exclusion range are passed through untranslated
271 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200272static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200273{
274 u64 start = iommu->exclusion_start & PAGE_MASK;
275 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
276 u64 entry;
277
278 if (!iommu->exclusion_start)
279 return;
280
281 entry = start | MMIO_EXCL_ENABLE_MASK;
282 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
283 &entry, sizeof(entry));
284
285 entry = limit;
286 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
287 &entry, sizeof(entry));
288}
289
Joerg Roedelb65233a2008-07-11 17:14:21 +0200290/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000291static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200292{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200293 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200294
295 BUG_ON(iommu->mmio_base == NULL);
296
297 entry = virt_to_phys(amd_iommu_dev_table);
298 entry |= (dev_table_size >> 12) - 1;
299 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
300 &entry, sizeof(entry));
301}
302
Joerg Roedelb65233a2008-07-11 17:14:21 +0200303/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200304static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200305{
306 u32 ctrl;
307
308 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
309 ctrl |= (1 << bit);
310 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
311}
312
Joerg Roedelca0207112009-10-28 18:02:26 +0100313static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200314{
315 u32 ctrl;
316
Joerg Roedel199d0d52008-09-17 16:45:59 +0200317 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200318 ctrl &= ~(1 << bit);
319 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
320}
321
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100322static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
323{
324 u32 ctrl;
325
326 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
327 ctrl &= ~CTRL_INV_TO_MASK;
328 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
329 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
330}
331
Joerg Roedelb65233a2008-07-11 17:14:21 +0200332/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200333static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200334{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200335 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200336}
337
Joerg Roedel92ac4322009-05-19 19:06:27 +0200338static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200339{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200340 /* Disable command buffer */
341 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
342
343 /* Disable event logging and event interrupts */
344 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
345 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
346
347 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200348 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200349}
350
Joerg Roedelb65233a2008-07-11 17:14:21 +0200351/*
352 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
353 * the system has one.
354 */
Joerg Roedel98f1ad22012-07-06 13:28:37 +0200355static u8 __iomem * __init iommu_map_mmio_space(u64 address)
Joerg Roedel6c567472008-06-26 21:27:43 +0200356{
Joerg Roedele82752d2010-05-28 14:26:48 +0200357 if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu")) {
358 pr_err("AMD-Vi: Can not reserve memory region %llx for mmio\n",
359 address);
360 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200361 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200362 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200363
Joerg Roedel98f1ad22012-07-06 13:28:37 +0200364 return (u8 __iomem *)ioremap_nocache(address, MMIO_REGION_LENGTH);
Joerg Roedel6c567472008-06-26 21:27:43 +0200365}
366
367static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
368{
369 if (iommu->mmio_base)
370 iounmap(iommu->mmio_base);
371 release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
372}
373
Joerg Roedelb65233a2008-07-11 17:14:21 +0200374/****************************************************************************
375 *
376 * The functions below belong to the first pass of AMD IOMMU ACPI table
377 * parsing. In this pass we try to find out the highest device id this
378 * code has to handle. Upon this information the size of the shared data
379 * structures is determined later.
380 *
381 ****************************************************************************/
382
383/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200384 * This function calculates the length of a given IVHD entry
385 */
386static inline int ivhd_entry_length(u8 *ivhd)
387{
388 return 0x04 << (*ivhd >> 6);
389}
390
391/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200392 * This function reads the last device id the IOMMU has to handle from the PCI
393 * capability header for this IOMMU
394 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200395static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
396{
397 u32 cap;
398
399 cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
Joerg Roedeld591b0a2008-07-11 17:14:35 +0200400 update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200401
402 return 0;
403}
404
Joerg Roedelb65233a2008-07-11 17:14:21 +0200405/*
406 * After reading the highest device id from the IOMMU PCI capability header
407 * this function looks if there is a higher device id defined in the ACPI table
408 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200409static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
410{
411 u8 *p = (void *)h, *end = (void *)h;
412 struct ivhd_entry *dev;
413
414 p += sizeof(*h);
415 end += h->length;
416
417 find_last_devid_on_pci(PCI_BUS(h->devid),
418 PCI_SLOT(h->devid),
419 PCI_FUNC(h->devid),
420 h->cap_ptr);
421
422 while (p < end) {
423 dev = (struct ivhd_entry *)p;
424 switch (dev->type) {
425 case IVHD_DEV_SELECT:
426 case IVHD_DEV_RANGE_END:
427 case IVHD_DEV_ALIAS:
428 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200429 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200430 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200431 break;
432 default:
433 break;
434 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200435 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200436 }
437
438 WARN_ON(p != end);
439
440 return 0;
441}
442
Joerg Roedelb65233a2008-07-11 17:14:21 +0200443/*
444 * Iterate over all IVHD entries in the ACPI table and find the highest device
445 * id which we need to handle. This is the first of three functions which parse
446 * the ACPI table. So we check the checksum here.
447 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200448static int __init find_last_devid_acpi(struct acpi_table_header *table)
449{
450 int i;
451 u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
452 struct ivhd_header *h;
453
454 /*
455 * Validate checksum here so we don't need to do it when
456 * we actually parse the table
457 */
458 for (i = 0; i < table->length; ++i)
459 checksum += p[i];
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200460 if (checksum != 0)
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200461 /* ACPI table corrupt */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200462 return -ENODEV;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200463
464 p += IVRS_HEADER_LENGTH;
465
466 end += table->length;
467 while (p < end) {
468 h = (struct ivhd_header *)p;
469 switch (h->type) {
470 case ACPI_IVHD_TYPE:
471 find_last_devid_from_ivhd(h);
472 break;
473 default:
474 break;
475 }
476 p += h->length;
477 }
478 WARN_ON(p != end);
479
480 return 0;
481}
482
Joerg Roedelb65233a2008-07-11 17:14:21 +0200483/****************************************************************************
484 *
485 * The following functions belong the the code path which parses the ACPI table
486 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
487 * data structures, initialize the device/alias/rlookup table and also
488 * basically initialize the hardware.
489 *
490 ****************************************************************************/
491
492/*
493 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
494 * write commands to that buffer later and the IOMMU will execute them
495 * asynchronously
496 */
Joerg Roedelb36ca912008-06-26 21:27:45 +0200497static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
498{
Joerg Roedeld0312b22008-07-11 17:14:29 +0200499 u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelb36ca912008-06-26 21:27:45 +0200500 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200501
502 if (cmd_buf == NULL)
503 return NULL;
504
Chris Wright549c90d2010-04-02 18:27:53 -0700505 iommu->cmd_buf_size = CMD_BUFFER_SIZE | CMD_BUFFER_UNINITIALIZED;
Joerg Roedelb36ca912008-06-26 21:27:45 +0200506
Joerg Roedel58492e12009-05-04 18:41:16 +0200507 return cmd_buf;
508}
509
510/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200511 * This function resets the command buffer if the IOMMU stopped fetching
512 * commands from it.
513 */
514void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
515{
516 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
517
518 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
519 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
520
521 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
522}
523
524/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200525 * This function writes the command buffer address to the hardware and
526 * enables it.
527 */
528static void iommu_enable_command_buffer(struct amd_iommu *iommu)
529{
530 u64 entry;
531
532 BUG_ON(iommu->cmd_buf == NULL);
533
534 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200535 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200536
Joerg Roedelb36ca912008-06-26 21:27:45 +0200537 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200538 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200539
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200540 amd_iommu_reset_cmd_buffer(iommu);
Chris Wright549c90d2010-04-02 18:27:53 -0700541 iommu->cmd_buf_size &= ~(CMD_BUFFER_UNINITIALIZED);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200542}
543
544static void __init free_command_buffer(struct amd_iommu *iommu)
545{
Joerg Roedel23c17132008-09-17 17:18:17 +0200546 free_pages((unsigned long)iommu->cmd_buf,
Chris Wright549c90d2010-04-02 18:27:53 -0700547 get_order(iommu->cmd_buf_size & ~(CMD_BUFFER_UNINITIALIZED)));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200548}
549
Joerg Roedel335503e2008-09-05 14:29:07 +0200550/* allocates the memory where the IOMMU will log its events to */
551static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
552{
Joerg Roedel335503e2008-09-05 14:29:07 +0200553 iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
554 get_order(EVT_BUFFER_SIZE));
555
556 if (iommu->evt_buf == NULL)
557 return NULL;
558
Joerg Roedel1bc6f832009-07-02 18:32:05 +0200559 iommu->evt_buf_size = EVT_BUFFER_SIZE;
560
Joerg Roedel58492e12009-05-04 18:41:16 +0200561 return iommu->evt_buf;
562}
563
564static void iommu_enable_event_buffer(struct amd_iommu *iommu)
565{
566 u64 entry;
567
568 BUG_ON(iommu->evt_buf == NULL);
569
Joerg Roedel335503e2008-09-05 14:29:07 +0200570 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200571
Joerg Roedel335503e2008-09-05 14:29:07 +0200572 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
573 &entry, sizeof(entry));
574
Joerg Roedel090672072009-06-15 16:06:48 +0200575 /* set head and tail to zero manually */
576 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
577 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
578
Joerg Roedel58492e12009-05-04 18:41:16 +0200579 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200580}
581
582static void __init free_event_buffer(struct amd_iommu *iommu)
583{
584 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
585}
586
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100587/* allocates the memory where the IOMMU will log its events to */
588static u8 * __init alloc_ppr_log(struct amd_iommu *iommu)
589{
590 iommu->ppr_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
591 get_order(PPR_LOG_SIZE));
592
593 if (iommu->ppr_log == NULL)
594 return NULL;
595
596 return iommu->ppr_log;
597}
598
599static void iommu_enable_ppr_log(struct amd_iommu *iommu)
600{
601 u64 entry;
602
603 if (iommu->ppr_log == NULL)
604 return;
605
606 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
607
608 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
609 &entry, sizeof(entry));
610
611 /* set head and tail to zero manually */
612 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
613 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
614
615 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
616 iommu_feature_enable(iommu, CONTROL_PPR_EN);
617}
618
619static void __init free_ppr_log(struct amd_iommu *iommu)
620{
621 if (iommu->ppr_log == NULL)
622 return;
623
624 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
625}
626
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100627static void iommu_enable_gt(struct amd_iommu *iommu)
628{
629 if (!iommu_feature(iommu, FEATURE_GT))
630 return;
631
632 iommu_feature_enable(iommu, CONTROL_GT_EN);
633}
634
Joerg Roedelb65233a2008-07-11 17:14:21 +0200635/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200636static void set_dev_entry_bit(u16 devid, u8 bit)
637{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100638 int i = (bit >> 6) & 0x03;
639 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200640
Joerg Roedelee6c2862011-11-09 12:06:03 +0100641 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200642}
643
Joerg Roedelc5cca142009-10-09 18:31:20 +0200644static int get_dev_entry_bit(u16 devid, u8 bit)
645{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100646 int i = (bit >> 6) & 0x03;
647 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200648
Joerg Roedelee6c2862011-11-09 12:06:03 +0100649 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200650}
651
652
653void amd_iommu_apply_erratum_63(u16 devid)
654{
655 int sysmgt;
656
657 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
658 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
659
660 if (sysmgt == 0x01)
661 set_dev_entry_bit(devid, DEV_ENTRY_IW);
662}
663
Joerg Roedel5ff47892008-07-14 20:11:18 +0200664/* Writes the specific IOMMU for a device into the rlookup table */
665static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
666{
667 amd_iommu_rlookup_table[devid] = iommu;
668}
669
Joerg Roedelb65233a2008-07-11 17:14:21 +0200670/*
671 * This function takes the device specific flags read from the ACPI
672 * table and sets up the device table entry with that information
673 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200674static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
675 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200676{
677 if (flags & ACPI_DEVFLAG_INITPASS)
678 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
679 if (flags & ACPI_DEVFLAG_EXTINT)
680 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
681 if (flags & ACPI_DEVFLAG_NMI)
682 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
683 if (flags & ACPI_DEVFLAG_SYSMGT1)
684 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
685 if (flags & ACPI_DEVFLAG_SYSMGT2)
686 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
687 if (flags & ACPI_DEVFLAG_LINT0)
688 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
689 if (flags & ACPI_DEVFLAG_LINT1)
690 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200691
Joerg Roedelc5cca142009-10-09 18:31:20 +0200692 amd_iommu_apply_erratum_63(devid);
693
Joerg Roedel5ff47892008-07-14 20:11:18 +0200694 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200695}
696
Joerg Roedel6efed632012-06-14 15:52:58 +0200697static int add_special_device(u8 type, u8 id, u16 devid)
698{
699 struct devid_map *entry;
700 struct list_head *list;
701
702 if (type != IVHD_SPECIAL_IOAPIC && type != IVHD_SPECIAL_HPET)
703 return -EINVAL;
704
705 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
706 if (!entry)
707 return -ENOMEM;
708
709 entry->id = id;
710 entry->devid = devid;
711
712 if (type == IVHD_SPECIAL_IOAPIC)
713 list = &ioapic_map;
714 else
715 list = &hpet_map;
716
717 list_add_tail(&entry->list, list);
718
719 return 0;
720}
721
Joerg Roedelb65233a2008-07-11 17:14:21 +0200722/*
723 * Reads the device exclusion range from ACPI and initialize IOMMU with
724 * it
725 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200726static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
727{
728 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
729
730 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
731 return;
732
733 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200734 /*
735 * We only can configure exclusion ranges per IOMMU, not
736 * per device. But we can enable the exclusion range per
737 * device. This is done here
738 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200739 set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
740 iommu->exclusion_start = m->range_start;
741 iommu->exclusion_length = m->range_length;
742 }
743}
744
Joerg Roedelb65233a2008-07-11 17:14:21 +0200745/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200746 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
747 * initializes the hardware and our data structures with it.
748 */
Joerg Roedel6efed632012-06-14 15:52:58 +0200749static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200750 struct ivhd_header *h)
751{
752 u8 *p = (u8 *)h;
753 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +0200754 u16 devid = 0, devid_start = 0, devid_to = 0;
755 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200756 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200757 struct ivhd_entry *e;
758
759 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +0200760 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200761 */
Joerg Roedele9bf5192010-09-20 14:33:07 +0200762 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200763
764 /*
765 * Done. Now parse the device entries
766 */
767 p += sizeof(struct ivhd_header);
768 end += h->length;
769
Joerg Roedel42a698f2009-05-20 15:41:28 +0200770
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200771 while (p < end) {
772 e = (struct ivhd_entry *)p;
773 switch (e->type) {
774 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200775
776 DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
777 " last device %02x:%02x.%x flags: %02x\n",
778 PCI_BUS(iommu->first_device),
779 PCI_SLOT(iommu->first_device),
780 PCI_FUNC(iommu->first_device),
781 PCI_BUS(iommu->last_device),
782 PCI_SLOT(iommu->last_device),
783 PCI_FUNC(iommu->last_device),
784 e->flags);
785
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200786 for (dev_i = iommu->first_device;
787 dev_i <= iommu->last_device; ++dev_i)
Joerg Roedel5ff47892008-07-14 20:11:18 +0200788 set_dev_entry_from_acpi(iommu, dev_i,
789 e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200790 break;
791 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200792
793 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
794 "flags: %02x\n",
795 PCI_BUS(e->devid),
796 PCI_SLOT(e->devid),
797 PCI_FUNC(e->devid),
798 e->flags);
799
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200800 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +0200801 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200802 break;
803 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200804
805 DUMP_printk(" DEV_SELECT_RANGE_START\t "
806 "devid: %02x:%02x.%x flags: %02x\n",
807 PCI_BUS(e->devid),
808 PCI_SLOT(e->devid),
809 PCI_FUNC(e->devid),
810 e->flags);
811
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200812 devid_start = e->devid;
813 flags = e->flags;
814 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200815 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200816 break;
817 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200818
819 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
820 "flags: %02x devid_to: %02x:%02x.%x\n",
821 PCI_BUS(e->devid),
822 PCI_SLOT(e->devid),
823 PCI_FUNC(e->devid),
824 e->flags,
825 PCI_BUS(e->ext >> 8),
826 PCI_SLOT(e->ext >> 8),
827 PCI_FUNC(e->ext >> 8));
828
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200829 devid = e->devid;
830 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200831 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +0100832 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200833 amd_iommu_alias_table[devid] = devid_to;
834 break;
835 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200836
837 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
838 "devid: %02x:%02x.%x flags: %02x "
839 "devid_to: %02x:%02x.%x\n",
840 PCI_BUS(e->devid),
841 PCI_SLOT(e->devid),
842 PCI_FUNC(e->devid),
843 e->flags,
844 PCI_BUS(e->ext >> 8),
845 PCI_SLOT(e->ext >> 8),
846 PCI_FUNC(e->ext >> 8));
847
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200848 devid_start = e->devid;
849 flags = e->flags;
850 devid_to = e->ext >> 8;
851 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200852 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200853 break;
854 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200855
856 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
857 "flags: %02x ext: %08x\n",
858 PCI_BUS(e->devid),
859 PCI_SLOT(e->devid),
860 PCI_FUNC(e->devid),
861 e->flags, e->ext);
862
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200863 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +0200864 set_dev_entry_from_acpi(iommu, devid, e->flags,
865 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200866 break;
867 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200868
869 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
870 "%02x:%02x.%x flags: %02x ext: %08x\n",
871 PCI_BUS(e->devid),
872 PCI_SLOT(e->devid),
873 PCI_FUNC(e->devid),
874 e->flags, e->ext);
875
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200876 devid_start = e->devid;
877 flags = e->flags;
878 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200879 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200880 break;
881 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200882
883 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
884 PCI_BUS(e->devid),
885 PCI_SLOT(e->devid),
886 PCI_FUNC(e->devid));
887
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200888 devid = e->devid;
889 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200890 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200891 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200892 set_dev_entry_from_acpi(iommu,
893 devid_to, flags, ext_flags);
894 }
895 set_dev_entry_from_acpi(iommu, dev_i,
896 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200897 }
898 break;
Joerg Roedel6efed632012-06-14 15:52:58 +0200899 case IVHD_DEV_SPECIAL: {
900 u8 handle, type;
901 const char *var;
902 u16 devid;
903 int ret;
904
905 handle = e->ext & 0xff;
906 devid = (e->ext >> 8) & 0xffff;
907 type = (e->ext >> 24) & 0xff;
908
909 if (type == IVHD_SPECIAL_IOAPIC)
910 var = "IOAPIC";
911 else if (type == IVHD_SPECIAL_HPET)
912 var = "HPET";
913 else
914 var = "UNKNOWN";
915
916 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
917 var, (int)handle,
918 PCI_BUS(devid),
919 PCI_SLOT(devid),
920 PCI_FUNC(devid));
921
922 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
923 ret = add_special_device(type, handle, devid);
924 if (ret)
925 return ret;
926 break;
927 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200928 default:
929 break;
930 }
931
Joerg Roedelb514e552008-09-17 17:14:27 +0200932 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200933 }
Joerg Roedel6efed632012-06-14 15:52:58 +0200934
935 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200936}
937
Joerg Roedelb65233a2008-07-11 17:14:21 +0200938/* Initializes the device->iommu mapping for the driver */
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200939static int __init init_iommu_devices(struct amd_iommu *iommu)
940{
Joerg Roedel0de66d52011-06-06 16:04:02 +0200941 u32 i;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200942
943 for (i = iommu->first_device; i <= iommu->last_device; ++i)
944 set_iommu_for_device(iommu, i);
945
946 return 0;
947}
948
Joerg Roedele47d4022008-06-26 21:27:48 +0200949static void __init free_iommu_one(struct amd_iommu *iommu)
950{
951 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +0200952 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100953 free_ppr_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +0200954 iommu_unmap_mmio_space(iommu);
955}
956
957static void __init free_iommu_all(void)
958{
959 struct amd_iommu *iommu, *next;
960
Joerg Roedel3bd22172009-05-04 15:06:20 +0200961 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +0200962 list_del(&iommu->list);
963 free_iommu_one(iommu);
964 kfree(iommu);
965 }
966}
967
Joerg Roedelb65233a2008-07-11 17:14:21 +0200968/*
969 * This function clues the initialization function for one IOMMU
970 * together and also allocates the command buffer and programs the
971 * hardware. It does NOT enable the IOMMU. This is done afterwards.
972 */
Joerg Roedele47d4022008-06-26 21:27:48 +0200973static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
974{
Joerg Roedel6efed632012-06-14 15:52:58 +0200975 int ret;
976
Joerg Roedele47d4022008-06-26 21:27:48 +0200977 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +0100978
979 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +0200980 list_add_tail(&iommu->list, &amd_iommu_list);
Joerg Roedelbb527772009-11-20 14:31:51 +0100981 iommu->index = amd_iommus_present++;
982
983 if (unlikely(iommu->index >= MAX_IOMMUS)) {
984 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
985 return -ENOSYS;
986 }
987
988 /* Index is fine - add IOMMU to the array */
989 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +0200990
991 /*
992 * Copy data from ACPI table entry to the iommu struct
993 */
Joerg Roedel23c742d2012-06-12 11:47:34 +0200994 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +0200995 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +0200996 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +0200997 iommu->mmio_phys = h->mmio_phys;
998 iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
999 if (!iommu->mmio_base)
1000 return -ENOMEM;
1001
Joerg Roedele47d4022008-06-26 21:27:48 +02001002 iommu->cmd_buf = alloc_command_buffer(iommu);
1003 if (!iommu->cmd_buf)
1004 return -ENOMEM;
1005
Joerg Roedel335503e2008-09-05 14:29:07 +02001006 iommu->evt_buf = alloc_event_buffer(iommu);
1007 if (!iommu->evt_buf)
1008 return -ENOMEM;
1009
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001010 iommu->int_enabled = false;
1011
Joerg Roedel6efed632012-06-14 15:52:58 +02001012 ret = init_iommu_from_acpi(iommu, h);
1013 if (ret)
1014 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001015 init_iommu_devices(iommu);
1016
Joerg Roedel23c742d2012-06-12 11:47:34 +02001017 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001018}
1019
Joerg Roedelb65233a2008-07-11 17:14:21 +02001020/*
1021 * Iterates over all IOMMU entries in the ACPI table, allocates the
1022 * IOMMU structure and initializes it with init_iommu_one()
1023 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001024static int __init init_iommu_all(struct acpi_table_header *table)
1025{
1026 u8 *p = (u8 *)table, *end = (u8 *)table;
1027 struct ivhd_header *h;
1028 struct amd_iommu *iommu;
1029 int ret;
1030
Joerg Roedele47d4022008-06-26 21:27:48 +02001031 end += table->length;
1032 p += IVRS_HEADER_LENGTH;
1033
1034 while (p < end) {
1035 h = (struct ivhd_header *)p;
1036 switch (*p) {
1037 case ACPI_IVHD_TYPE:
Joerg Roedel9c720412009-05-20 13:53:57 +02001038
Joerg Roedelae908c22009-09-01 16:52:16 +02001039 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001040 "seg: %d flags: %01x info %04x\n",
1041 PCI_BUS(h->devid), PCI_SLOT(h->devid),
1042 PCI_FUNC(h->devid), h->cap_ptr,
1043 h->pci_seg, h->flags, h->info);
1044 DUMP_printk(" mmio-addr: %016llx\n",
1045 h->mmio_phys);
1046
Joerg Roedele47d4022008-06-26 21:27:48 +02001047 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001048 if (iommu == NULL)
1049 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001050
Joerg Roedele47d4022008-06-26 21:27:48 +02001051 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001052 if (ret)
1053 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001054 break;
1055 default:
1056 break;
1057 }
1058 p += h->length;
1059
1060 }
1061 WARN_ON(p != end);
1062
1063 return 0;
1064}
1065
Joerg Roedel23c742d2012-06-12 11:47:34 +02001066static int iommu_init_pci(struct amd_iommu *iommu)
1067{
1068 int cap_ptr = iommu->cap_ptr;
1069 u32 range, misc, low, high;
1070
1071 iommu->dev = pci_get_bus_and_slot(PCI_BUS(iommu->devid),
1072 iommu->devid & 0xff);
1073 if (!iommu->dev)
1074 return -ENODEV;
1075
1076 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1077 &iommu->cap);
1078 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1079 &range);
1080 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1081 &misc);
1082
1083 iommu->first_device = calc_devid(MMIO_GET_BUS(range),
1084 MMIO_GET_FD(range));
1085 iommu->last_device = calc_devid(MMIO_GET_BUS(range),
1086 MMIO_GET_LD(range));
1087
1088 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1089 amd_iommu_iotlb_sup = false;
1090
1091 /* read extended feature bits */
1092 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1093 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1094
1095 iommu->features = ((u64)high << 32) | low;
1096
1097 if (iommu_feature(iommu, FEATURE_GT)) {
1098 int glxval;
1099 u32 pasids;
1100 u64 shift;
1101
1102 shift = iommu->features & FEATURE_PASID_MASK;
1103 shift >>= FEATURE_PASID_SHIFT;
1104 pasids = (1 << shift);
1105
1106 amd_iommu_max_pasids = min(amd_iommu_max_pasids, pasids);
1107
1108 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1109 glxval >>= FEATURE_GLXVAL_SHIFT;
1110
1111 if (amd_iommu_max_glx_val == -1)
1112 amd_iommu_max_glx_val = glxval;
1113 else
1114 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1115 }
1116
1117 if (iommu_feature(iommu, FEATURE_GT) &&
1118 iommu_feature(iommu, FEATURE_PPR)) {
1119 iommu->is_iommu_v2 = true;
1120 amd_iommu_v2_present = true;
1121 }
1122
1123 if (iommu_feature(iommu, FEATURE_PPR)) {
1124 iommu->ppr_log = alloc_ppr_log(iommu);
1125 if (!iommu->ppr_log)
1126 return -ENOMEM;
1127 }
1128
1129 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1130 amd_iommu_np_cache = true;
1131
1132 if (is_rd890_iommu(iommu->dev)) {
1133 int i, j;
1134
1135 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1136 PCI_DEVFN(0, 0));
1137
1138 /*
1139 * Some rd890 systems may not be fully reconfigured by the
1140 * BIOS, so it's necessary for us to store this information so
1141 * it can be reprogrammed on resume
1142 */
1143 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1144 &iommu->stored_addr_lo);
1145 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1146 &iommu->stored_addr_hi);
1147
1148 /* Low bit locks writes to configuration space */
1149 iommu->stored_addr_lo &= ~1;
1150
1151 for (i = 0; i < 6; i++)
1152 for (j = 0; j < 0x12; j++)
1153 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1154
1155 for (i = 0; i < 0x83; i++)
1156 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1157 }
1158
1159 return pci_enable_device(iommu->dev);
1160}
1161
Joerg Roedel4d121c32012-06-14 12:21:55 +02001162static void print_iommu_info(void)
1163{
1164 static const char * const feat_str[] = {
1165 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1166 "IA", "GA", "HE", "PC"
1167 };
1168 struct amd_iommu *iommu;
1169
1170 for_each_iommu(iommu) {
1171 int i;
1172
1173 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1174 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1175
1176 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1177 pr_info("AMD-Vi: Extended features: ");
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001178 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001179 if (iommu_feature(iommu, (1ULL << i)))
1180 pr_cont(" %s", feat_str[i]);
1181 }
1182 }
1183 pr_cont("\n");
1184 }
1185}
1186
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001187static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001188{
1189 struct amd_iommu *iommu;
1190 int ret = 0;
1191
1192 for_each_iommu(iommu) {
1193 ret = iommu_init_pci(iommu);
1194 if (ret)
1195 break;
1196 }
1197
Joerg Roedel23c742d2012-06-12 11:47:34 +02001198 ret = amd_iommu_init_devices();
1199
Joerg Roedel4d121c32012-06-14 12:21:55 +02001200 print_iommu_info();
1201
Joerg Roedel23c742d2012-06-12 11:47:34 +02001202 return ret;
1203}
1204
Joerg Roedelb65233a2008-07-11 17:14:21 +02001205/****************************************************************************
1206 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001207 * The following functions initialize the MSI interrupts for all IOMMUs
1208 * in the system. Its a bit challenging because there could be multiple
1209 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1210 * pci_dev.
1211 *
1212 ****************************************************************************/
1213
Joerg Roedel9f800de2009-11-23 12:45:25 +01001214static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001215{
1216 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001217
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001218 r = pci_enable_msi(iommu->dev);
1219 if (r)
1220 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001221
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001222 r = request_threaded_irq(iommu->dev->irq,
1223 amd_iommu_int_handler,
1224 amd_iommu_int_thread,
1225 0, "AMD-Vi",
1226 iommu->dev);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001227
1228 if (r) {
1229 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001230 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001231 }
1232
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001233 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001234
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001235 return 0;
1236}
1237
Joerg Roedel05f92db2009-05-12 09:52:46 +02001238static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001239{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001240 int ret;
1241
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001242 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001243 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001244
Joerg Roedeld91cecd2009-05-04 18:51:00 +02001245 if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001246 ret = iommu_setup_msi(iommu);
1247 else
1248 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001249
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001250 if (ret)
1251 return ret;
1252
1253enable_faults:
1254 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1255
1256 if (iommu->ppr_log != NULL)
1257 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1258
1259 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001260}
1261
1262/****************************************************************************
1263 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001264 * The next functions belong to the third pass of parsing the ACPI
1265 * table. In this last pass the memory mapping requirements are
1266 * gathered (like exclusion and unity mapping reanges).
1267 *
1268 ****************************************************************************/
1269
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001270static void __init free_unity_maps(void)
1271{
1272 struct unity_map_entry *entry, *next;
1273
1274 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1275 list_del(&entry->list);
1276 kfree(entry);
1277 }
1278}
1279
Joerg Roedelb65233a2008-07-11 17:14:21 +02001280/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001281static int __init init_exclusion_range(struct ivmd_header *m)
1282{
1283 int i;
1284
1285 switch (m->type) {
1286 case ACPI_IVMD_TYPE:
1287 set_device_exclusion_range(m->devid, m);
1288 break;
1289 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001290 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001291 set_device_exclusion_range(i, m);
1292 break;
1293 case ACPI_IVMD_TYPE_RANGE:
1294 for (i = m->devid; i <= m->aux; ++i)
1295 set_device_exclusion_range(i, m);
1296 break;
1297 default:
1298 break;
1299 }
1300
1301 return 0;
1302}
1303
Joerg Roedelb65233a2008-07-11 17:14:21 +02001304/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001305static int __init init_unity_map_range(struct ivmd_header *m)
1306{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001307 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001308 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001309
1310 e = kzalloc(sizeof(*e), GFP_KERNEL);
1311 if (e == NULL)
1312 return -ENOMEM;
1313
1314 switch (m->type) {
1315 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001316 kfree(e);
1317 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001318 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001319 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001320 e->devid_start = e->devid_end = m->devid;
1321 break;
1322 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001323 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001324 e->devid_start = 0;
1325 e->devid_end = amd_iommu_last_bdf;
1326 break;
1327 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001328 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001329 e->devid_start = m->devid;
1330 e->devid_end = m->aux;
1331 break;
1332 }
1333 e->address_start = PAGE_ALIGN(m->range_start);
1334 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1335 e->prot = m->flags >> 1;
1336
Joerg Roedel02acc432009-05-20 16:24:21 +02001337 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1338 " range_start: %016llx range_end: %016llx flags: %x\n", s,
1339 PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
1340 PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
1341 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1342 e->address_start, e->address_end, m->flags);
1343
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001344 list_add_tail(&e->list, &amd_iommu_unity_map);
1345
1346 return 0;
1347}
1348
Joerg Roedelb65233a2008-07-11 17:14:21 +02001349/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001350static int __init init_memory_definitions(struct acpi_table_header *table)
1351{
1352 u8 *p = (u8 *)table, *end = (u8 *)table;
1353 struct ivmd_header *m;
1354
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001355 end += table->length;
1356 p += IVRS_HEADER_LENGTH;
1357
1358 while (p < end) {
1359 m = (struct ivmd_header *)p;
1360 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1361 init_exclusion_range(m);
1362 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1363 init_unity_map_range(m);
1364
1365 p += m->length;
1366 }
1367
1368 return 0;
1369}
1370
Joerg Roedelb65233a2008-07-11 17:14:21 +02001371/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001372 * Init the device table to not allow DMA access for devices and
1373 * suppress all page faults
1374 */
1375static void init_device_table(void)
1376{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001377 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001378
1379 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1380 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1381 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001382 }
1383}
1384
Joerg Roedele9bf5192010-09-20 14:33:07 +02001385static void iommu_init_flags(struct amd_iommu *iommu)
1386{
1387 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1388 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1389 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1390
1391 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1392 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1393 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1394
1395 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1396 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1397 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1398
1399 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1400 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1401 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1402
1403 /*
1404 * make IOMMU memory accesses cache coherent
1405 */
1406 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001407
1408 /* Set IOTLB invalidation timeout to 1s */
1409 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001410}
1411
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001412static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001413{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001414 int i, j;
1415 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001416 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001417
1418 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001419 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001420 return;
1421
1422 /*
1423 * First, we need to ensure that the iommu is enabled. This is
1424 * controlled by a register in the northbridge
1425 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001426
1427 /* Select Northbridge indirect register 0x75 and enable writing */
1428 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1429 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1430
1431 /* Enable the iommu */
1432 if (!(ioc_feature_control & 0x1))
1433 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1434
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001435 /* Restore the iommu BAR */
1436 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1437 iommu->stored_addr_lo);
1438 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1439 iommu->stored_addr_hi);
1440
1441 /* Restore the l1 indirect regs for each of the 6 l1s */
1442 for (i = 0; i < 6; i++)
1443 for (j = 0; j < 0x12; j++)
1444 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1445
1446 /* Restore the l2 indirect regs */
1447 for (i = 0; i < 0x83; i++)
1448 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
1449
1450 /* Lock PCI setup registers */
1451 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1452 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02001453}
1454
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001455/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001456 * This function finally enables all IOMMUs found in the system after
1457 * they have been initialized
1458 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02001459static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02001460{
1461 struct amd_iommu *iommu;
1462
Joerg Roedel3bd22172009-05-04 15:06:20 +02001463 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02001464 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001465 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02001466 iommu_set_device_table(iommu);
1467 iommu_enable_command_buffer(iommu);
1468 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001469 iommu_set_exclusion_range(iommu);
1470 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02001471 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001472 }
1473}
1474
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02001475static void enable_iommus_v2(void)
1476{
1477 struct amd_iommu *iommu;
1478
1479 for_each_iommu(iommu) {
1480 iommu_enable_ppr_log(iommu);
1481 iommu_enable_gt(iommu);
1482 }
1483}
1484
1485static void enable_iommus(void)
1486{
1487 early_enable_iommus();
1488
1489 enable_iommus_v2();
1490}
1491
Joerg Roedel92ac4322009-05-19 19:06:27 +02001492static void disable_iommus(void)
1493{
1494 struct amd_iommu *iommu;
1495
1496 for_each_iommu(iommu)
1497 iommu_disable(iommu);
1498}
1499
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001500/*
1501 * Suspend/Resume support
1502 * disable suspend until real resume implemented
1503 */
1504
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001505static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001506{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001507 struct amd_iommu *iommu;
1508
1509 for_each_iommu(iommu)
1510 iommu_apply_resume_quirks(iommu);
1511
Joerg Roedel736501e2009-05-12 09:56:12 +02001512 /* re-load the hardware */
1513 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01001514
1515 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001516}
1517
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001518static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001519{
Joerg Roedel736501e2009-05-12 09:56:12 +02001520 /* disable IOMMUs to go out of the way for BIOS */
1521 disable_iommus();
1522
1523 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001524}
1525
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01001526static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001527 .suspend = amd_iommu_suspend,
1528 .resume = amd_iommu_resume,
1529};
1530
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001531static void __init free_on_init_error(void)
1532{
1533 amd_iommu_uninit_devices();
1534
1535 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
1536 get_order(MAX_DOMAIN_ID/8));
1537
1538 free_pages((unsigned long)amd_iommu_rlookup_table,
1539 get_order(rlookup_table_size));
1540
1541 free_pages((unsigned long)amd_iommu_alias_table,
1542 get_order(alias_table_size));
1543
1544 free_pages((unsigned long)amd_iommu_dev_table,
1545 get_order(dev_table_size));
1546
1547 free_iommu_all();
1548
1549 free_unity_maps();
1550
1551#ifdef CONFIG_GART_IOMMU
1552 /*
1553 * We failed to initialize the AMD IOMMU - try fallback to GART
1554 * if possible.
1555 */
1556 gart_iommu_init();
1557
1558#endif
1559}
1560
Joerg Roedelb65233a2008-07-11 17:14:21 +02001561/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001562 * This is the hardware init function for AMD IOMMU in the system.
1563 * This function is called either from amd_iommu_init or from the interrupt
1564 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02001565 *
1566 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
1567 * three times:
1568 *
1569 * 1 pass) Find the highest PCI device id the driver has to handle.
1570 * Upon this information the size of the data structures is
1571 * determined that needs to be allocated.
1572 *
1573 * 2 pass) Initialize the data structures just allocated with the
1574 * information in the ACPI table about available AMD IOMMUs
1575 * in the system. It also maps the PCI devices in the
1576 * system to specific IOMMUs
1577 *
1578 * 3 pass) After the basic data structures are allocated and
1579 * initialized we update them with information about memory
1580 * remapping requirements parsed out of the ACPI table in
1581 * this last pass.
1582 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001583 * After everything is set up the IOMMUs are enabled and the necessary
1584 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02001585 */
Joerg Roedel643511b2012-06-12 12:09:35 +02001586static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001587{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001588 struct acpi_table_header *ivrs_base;
1589 acpi_size ivrs_size;
1590 acpi_status status;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001591 int i, ret = 0;
1592
Joerg Roedel643511b2012-06-12 12:09:35 +02001593 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001594 return -ENODEV;
1595
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001596 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
1597 if (status == AE_NOT_FOUND)
1598 return -ENODEV;
1599 else if (ACPI_FAILURE(status)) {
1600 const char *err = acpi_format_exception(status);
1601 pr_err("AMD-Vi: IVRS table error: %s\n", err);
1602 return -EINVAL;
1603 }
1604
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001605 /*
1606 * First parse ACPI tables to find the largest Bus/Dev/Func
1607 * we need to handle. Upon this information the shared data
1608 * structures for the IOMMUs in the system will be allocated
1609 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001610 ret = find_last_devid_acpi(ivrs_base);
1611 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01001612 goto out;
1613
Joerg Roedelc5714842008-07-11 17:14:25 +02001614 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
1615 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
1616 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001617
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001618 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001619 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001620 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001621 get_order(dev_table_size));
1622 if (amd_iommu_dev_table == NULL)
1623 goto out;
1624
1625 /*
1626 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
1627 * IOMMU see for that device
1628 */
1629 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
1630 get_order(alias_table_size));
1631 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001632 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001633
1634 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01001635 amd_iommu_rlookup_table = (void *)__get_free_pages(
1636 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001637 get_order(rlookup_table_size));
1638 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001639 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001640
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001641 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
1642 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001643 get_order(MAX_DOMAIN_ID/8));
1644 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001645 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001646
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001647 /* init the device table */
1648 init_device_table();
1649
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001650 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001651 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001652 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001653 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001654 amd_iommu_alias_table[i] = i;
1655
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001656 /*
1657 * never allocate domain 0 because its used as the non-allocated and
1658 * error value placeholder
1659 */
1660 amd_iommu_pd_alloc_bitmap[0] = 1;
1661
Joerg Roedelaeb26f52009-11-20 16:44:01 +01001662 spin_lock_init(&amd_iommu_pd_lock);
1663
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001664 /*
1665 * now the data structures are allocated and basically initialized
1666 * start the real acpi table scan
1667 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001668 ret = init_iommu_all(ivrs_base);
1669 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001670 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001671
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001672 ret = init_memory_definitions(ivrs_base);
1673 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001674 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01001675
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001676out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001677 /* Don't leak any ACPI memory */
1678 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
1679 ivrs_base = NULL;
1680
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001681 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02001682}
1683
Gerard Snitselaarae295142012-03-16 11:38:22 -07001684static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01001685{
1686 struct amd_iommu *iommu;
1687 int ret = 0;
1688
1689 for_each_iommu(iommu) {
1690 ret = iommu_init_msi(iommu);
1691 if (ret)
1692 goto out;
1693 }
1694
1695out:
1696 return ret;
1697}
1698
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001699static bool detect_ivrs(void)
1700{
1701 struct acpi_table_header *ivrs_base;
1702 acpi_size ivrs_size;
1703 acpi_status status;
1704
1705 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
1706 if (status == AE_NOT_FOUND)
1707 return false;
1708 else if (ACPI_FAILURE(status)) {
1709 const char *err = acpi_format_exception(status);
1710 pr_err("AMD-Vi: IVRS table error: %s\n", err);
1711 return false;
1712 }
1713
1714 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
1715
Joerg Roedel1adb7d32012-08-06 14:18:42 +02001716 /* Make sure ACS will be enabled during PCI probe */
1717 pci_request_acs();
1718
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001719 return true;
1720}
1721
Joerg Roedelb9b1ce702012-06-12 16:51:12 +02001722static int amd_iommu_init_dma(void)
1723{
1724 int ret;
1725
1726 if (iommu_pass_through)
1727 ret = amd_iommu_init_passthrough();
1728 else
1729 ret = amd_iommu_init_dma_ops();
1730
1731 if (ret)
1732 return ret;
1733
1734 amd_iommu_init_api();
1735
1736 amd_iommu_init_notifier();
1737
1738 return 0;
1739}
1740
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001741/****************************************************************************
1742 *
1743 * AMD IOMMU Initialization State Machine
1744 *
1745 ****************************************************************************/
1746
1747static int __init state_next(void)
1748{
1749 int ret = 0;
1750
1751 switch (init_state) {
1752 case IOMMU_START_STATE:
1753 if (!detect_ivrs()) {
1754 init_state = IOMMU_NOT_FOUND;
1755 ret = -ENODEV;
1756 } else {
1757 init_state = IOMMU_IVRS_DETECTED;
1758 }
1759 break;
1760 case IOMMU_IVRS_DETECTED:
1761 ret = early_amd_iommu_init();
1762 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
1763 break;
1764 case IOMMU_ACPI_FINISHED:
1765 early_enable_iommus();
1766 register_syscore_ops(&amd_iommu_syscore_ops);
1767 x86_platform.iommu_shutdown = disable_iommus;
1768 init_state = IOMMU_ENABLED;
1769 break;
1770 case IOMMU_ENABLED:
1771 ret = amd_iommu_init_pci();
1772 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
1773 enable_iommus_v2();
1774 break;
1775 case IOMMU_PCI_INIT:
1776 ret = amd_iommu_enable_interrupts();
1777 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
1778 break;
1779 case IOMMU_INTERRUPTS_EN:
1780 ret = amd_iommu_init_dma();
1781 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
1782 break;
1783 case IOMMU_DMA_OPS:
1784 init_state = IOMMU_INITIALIZED;
1785 break;
1786 case IOMMU_INITIALIZED:
1787 /* Nothing to do */
1788 break;
1789 case IOMMU_NOT_FOUND:
1790 case IOMMU_INIT_ERROR:
1791 /* Error states => do nothing */
1792 ret = -EINVAL;
1793 break;
1794 default:
1795 /* Unknown state */
1796 BUG();
1797 }
1798
1799 return ret;
1800}
1801
1802static int __init iommu_go_to_state(enum iommu_init_state state)
1803{
1804 int ret = 0;
1805
1806 while (init_state != state) {
1807 ret = state_next();
1808 if (init_state == IOMMU_NOT_FOUND ||
1809 init_state == IOMMU_INIT_ERROR)
1810 break;
1811 }
1812
1813 return ret;
1814}
1815
1816
1817
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001818/*
1819 * This is the core init function for AMD IOMMU hardware in the system.
1820 * This function is called from the generic x86 DMA layer initialization
1821 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001822 */
1823static int __init amd_iommu_init(void)
1824{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001825 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001826
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001827 ret = iommu_go_to_state(IOMMU_INITIALIZED);
1828 if (ret) {
1829 disable_iommus();
1830 free_on_init_error();
1831 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01001832
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001833 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001834}
1835
Joerg Roedelb65233a2008-07-11 17:14:21 +02001836/****************************************************************************
1837 *
1838 * Early detect code. This code runs at IOMMU detection time in the DMA
1839 * layer. It just looks if there is an IVRS ACPI table to detect AMD
1840 * IOMMUs
1841 *
1842 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04001843int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02001844{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001845 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001846
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09001847 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04001848 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02001849
Joerg Roedela5235722010-05-11 17:12:33 +02001850 if (amd_iommu_disabled)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04001851 return -ENODEV;
Joerg Roedela5235722010-05-11 17:12:33 +02001852
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001853 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
1854 if (ret)
1855 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08001856
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001857 amd_iommu_detected = true;
1858 iommu_detected = 1;
1859 x86_init.iommu.iommu_init = amd_iommu_init;
1860
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001861 return 0;
Joerg Roedelae7877d2008-06-26 21:27:51 +02001862}
1863
Joerg Roedelb65233a2008-07-11 17:14:21 +02001864/****************************************************************************
1865 *
1866 * Parsing functions for the AMD IOMMU specific kernel command line
1867 * options.
1868 *
1869 ****************************************************************************/
1870
Joerg Roedelfefda112009-05-20 12:21:42 +02001871static int __init parse_amd_iommu_dump(char *str)
1872{
1873 amd_iommu_dump = true;
1874
1875 return 1;
1876}
1877
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001878static int __init parse_amd_iommu_options(char *str)
1879{
1880 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01001881 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09001882 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02001883 if (strncmp(str, "off", 3) == 0)
1884 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01001885 if (strncmp(str, "force_isolation", 15) == 0)
1886 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001887 }
1888
1889 return 1;
1890}
1891
Joerg Roedelfefda112009-05-20 12:21:42 +02001892__setup("amd_iommu_dump", parse_amd_iommu_dump);
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001893__setup("amd_iommu=", parse_amd_iommu_options);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04001894
1895IOMMU_INIT_FINISH(amd_iommu_detect,
1896 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001897 NULL,
1898 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01001899
1900bool amd_iommu_v2_supported(void)
1901{
1902 return amd_iommu_v2_present;
1903}
1904EXPORT_SYMBOL(amd_iommu_v2_supported);