blob: 72bdbdac9b48f3e965778bf85c48c1991997f59e [file] [log] [blame]
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02001/*
2 * Copyright (C) 2007-2008 Advanced Micro Devices, Inc.
3 * 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>
22#include <linux/gfp.h>
23#include <linux/list.h>
Joerg Roedel7441e9c2008-06-30 20:18:02 +020024#include <linux/sysdev.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020025#include <linux/interrupt.h>
26#include <linux/msi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020027#include <asm/pci-direct.h>
28#include <asm/amd_iommu_types.h>
Joerg Roedelc6da9922008-06-26 21:28:06 +020029#include <asm/amd_iommu.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090030#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010031#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090032#include <asm/x86_init.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020033
34/*
35 * definitions for the ACPI scanning code
36 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020037#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020038
39#define ACPI_IVHD_TYPE 0x10
40#define ACPI_IVMD_TYPE_ALL 0x20
41#define ACPI_IVMD_TYPE 0x21
42#define ACPI_IVMD_TYPE_RANGE 0x22
43
44#define IVHD_DEV_ALL 0x01
45#define IVHD_DEV_SELECT 0x02
46#define IVHD_DEV_SELECT_RANGE_START 0x03
47#define IVHD_DEV_RANGE_END 0x04
48#define IVHD_DEV_ALIAS 0x42
49#define IVHD_DEV_ALIAS_RANGE 0x43
50#define IVHD_DEV_EXT_SELECT 0x46
51#define IVHD_DEV_EXT_SELECT_RANGE 0x47
52
Joerg Roedel6da73422009-05-04 11:44:38 +020053#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
54#define IVHD_FLAG_PASSPW_EN_MASK 0x02
55#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
56#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020057
58#define IVMD_FLAG_EXCL_RANGE 0x08
59#define IVMD_FLAG_UNITY_MAP 0x01
60
61#define ACPI_DEVFLAG_INITPASS 0x01
62#define ACPI_DEVFLAG_EXTINT 0x02
63#define ACPI_DEVFLAG_NMI 0x04
64#define ACPI_DEVFLAG_SYSMGT1 0x10
65#define ACPI_DEVFLAG_SYSMGT2 0x20
66#define ACPI_DEVFLAG_LINT0 0x40
67#define ACPI_DEVFLAG_LINT1 0x80
68#define ACPI_DEVFLAG_ATSDIS 0x10000000
69
Joerg Roedelb65233a2008-07-11 17:14:21 +020070/*
71 * ACPI table definitions
72 *
73 * These data structures are laid over the table to parse the important values
74 * out of it.
75 */
76
77/*
78 * structure describing one IOMMU in the ACPI table. Typically followed by one
79 * or more ivhd_entrys.
80 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020081struct ivhd_header {
82 u8 type;
83 u8 flags;
84 u16 length;
85 u16 devid;
86 u16 cap_ptr;
87 u64 mmio_phys;
88 u16 pci_seg;
89 u16 info;
90 u32 reserved;
91} __attribute__((packed));
92
Joerg Roedelb65233a2008-07-11 17:14:21 +020093/*
94 * A device entry describing which devices a specific IOMMU translates and
95 * which requestor ids they use.
96 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020097struct ivhd_entry {
98 u8 type;
99 u16 devid;
100 u8 flags;
101 u32 ext;
102} __attribute__((packed));
103
Joerg Roedelb65233a2008-07-11 17:14:21 +0200104/*
105 * An AMD IOMMU memory definition structure. It defines things like exclusion
106 * ranges for devices and regions that should be unity mapped.
107 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200108struct ivmd_header {
109 u8 type;
110 u8 flags;
111 u16 length;
112 u16 devid;
113 u16 aux;
114 u64 resv;
115 u64 range_start;
116 u64 range_length;
117} __attribute__((packed));
118
Joerg Roedelfefda112009-05-20 12:21:42 +0200119bool amd_iommu_dump;
120
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200121static int __initdata amd_iommu_detected;
122
Joerg Roedelb65233a2008-07-11 17:14:21 +0200123u16 amd_iommu_last_bdf; /* largest PCI device id we have
124 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200125LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200126 we find in ACPI */
Joerg Roedel2e8b5692009-05-22 12:44:03 +0200127#ifdef CONFIG_IOMMU_STRESS
128bool amd_iommu_isolate = false;
129#else
Joerg Roedelc226f852008-12-12 13:53:54 +0100130bool amd_iommu_isolate = true; /* if true, device isolation is
131 enabled */
Joerg Roedel2e8b5692009-05-22 12:44:03 +0200132#endif
133
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +0900134bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200135
Joerg Roedel2e228472008-07-11 17:14:31 +0200136LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200137 system */
138
139/*
140 * Pointer to the device table which is shared by all AMD IOMMUs
141 * it is indexed by the PCI device id or the HT unit id and contains
142 * information about the domain the device belongs to as well as the
143 * page table root pointer.
144 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200145struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200146
147/*
148 * The alias table is a driver specific data structure which contains the
149 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
150 * More than one device can share the same requestor id.
151 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200152u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200153
154/*
155 * The rlookup table is used to find the IOMMU which is responsible
156 * for a specific device. It is also indexed by the PCI device id.
157 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200158struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200159
160/*
161 * The pd table (protection domain table) is used to find the protection domain
162 * data structure a device belongs to. Indexed with the PCI device id too.
163 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200164struct protection_domain **amd_iommu_pd_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200165
166/*
167 * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
168 * to know which ones are already in use.
169 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200170unsigned long *amd_iommu_pd_alloc_bitmap;
171
Joerg Roedelb65233a2008-07-11 17:14:21 +0200172static u32 dev_table_size; /* size of the device table */
173static u32 alias_table_size; /* size of the alias table */
174static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200175
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200176static inline void update_last_devid(u16 devid)
177{
178 if (devid > amd_iommu_last_bdf)
179 amd_iommu_last_bdf = devid;
180}
181
Joerg Roedelc5714842008-07-11 17:14:25 +0200182static inline unsigned long tbl_size(int entry_size)
183{
184 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100185 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200186
187 return 1UL << shift;
188}
189
Joerg Roedelb65233a2008-07-11 17:14:21 +0200190/****************************************************************************
191 *
192 * AMD IOMMU MMIO register space handling functions
193 *
194 * These functions are used to program the IOMMU device registers in
195 * MMIO space required for that driver.
196 *
197 ****************************************************************************/
198
199/*
200 * This function set the exclusion range in the IOMMU. DMA accesses to the
201 * exclusion range are passed through untranslated
202 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200203static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200204{
205 u64 start = iommu->exclusion_start & PAGE_MASK;
206 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
207 u64 entry;
208
209 if (!iommu->exclusion_start)
210 return;
211
212 entry = start | MMIO_EXCL_ENABLE_MASK;
213 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
214 &entry, sizeof(entry));
215
216 entry = limit;
217 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
218 &entry, sizeof(entry));
219}
220
Joerg Roedelb65233a2008-07-11 17:14:21 +0200221/* Programs the physical address of the device table into the IOMMU hardware */
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200222static void __init iommu_set_device_table(struct amd_iommu *iommu)
223{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200224 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200225
226 BUG_ON(iommu->mmio_base == NULL);
227
228 entry = virt_to_phys(amd_iommu_dev_table);
229 entry |= (dev_table_size >> 12) - 1;
230 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
231 &entry, sizeof(entry));
232}
233
Joerg Roedelb65233a2008-07-11 17:14:21 +0200234/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200235static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200236{
237 u32 ctrl;
238
239 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
240 ctrl |= (1 << bit);
241 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
242}
243
Joerg Roedelca0207112009-10-28 18:02:26 +0100244static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200245{
246 u32 ctrl;
247
Joerg Roedel199d0d52008-09-17 16:45:59 +0200248 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200249 ctrl &= ~(1 << bit);
250 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
251}
252
Joerg Roedelb65233a2008-07-11 17:14:21 +0200253/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200254static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200255{
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200256 printk(KERN_INFO "AMD-Vi: Enabling IOMMU at %s cap 0x%hx\n",
Joerg Roedela4e267c2008-12-10 20:04:18 +0100257 dev_name(&iommu->dev->dev), iommu->cap_ptr);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200258
259 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200260}
261
Joerg Roedel92ac4322009-05-19 19:06:27 +0200262static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200263{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200264 /* Disable command buffer */
265 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
266
267 /* Disable event logging and event interrupts */
268 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
269 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
270
271 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200272 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200273}
274
Joerg Roedelb65233a2008-07-11 17:14:21 +0200275/*
276 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
277 * the system has one.
278 */
Joerg Roedel6c567472008-06-26 21:27:43 +0200279static u8 * __init iommu_map_mmio_space(u64 address)
280{
281 u8 *ret;
282
283 if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu"))
284 return NULL;
285
286 ret = ioremap_nocache(address, MMIO_REGION_LENGTH);
287 if (ret != NULL)
288 return ret;
289
290 release_mem_region(address, MMIO_REGION_LENGTH);
291
292 return NULL;
293}
294
295static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
296{
297 if (iommu->mmio_base)
298 iounmap(iommu->mmio_base);
299 release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
300}
301
Joerg Roedelb65233a2008-07-11 17:14:21 +0200302/****************************************************************************
303 *
304 * The functions below belong to the first pass of AMD IOMMU ACPI table
305 * parsing. In this pass we try to find out the highest device id this
306 * code has to handle. Upon this information the size of the shared data
307 * structures is determined later.
308 *
309 ****************************************************************************/
310
311/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200312 * This function calculates the length of a given IVHD entry
313 */
314static inline int ivhd_entry_length(u8 *ivhd)
315{
316 return 0x04 << (*ivhd >> 6);
317}
318
319/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200320 * This function reads the last device id the IOMMU has to handle from the PCI
321 * capability header for this IOMMU
322 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200323static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
324{
325 u32 cap;
326
327 cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
Joerg Roedeld591b0a2008-07-11 17:14:35 +0200328 update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200329
330 return 0;
331}
332
Joerg Roedelb65233a2008-07-11 17:14:21 +0200333/*
334 * After reading the highest device id from the IOMMU PCI capability header
335 * this function looks if there is a higher device id defined in the ACPI table
336 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200337static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
338{
339 u8 *p = (void *)h, *end = (void *)h;
340 struct ivhd_entry *dev;
341
342 p += sizeof(*h);
343 end += h->length;
344
345 find_last_devid_on_pci(PCI_BUS(h->devid),
346 PCI_SLOT(h->devid),
347 PCI_FUNC(h->devid),
348 h->cap_ptr);
349
350 while (p < end) {
351 dev = (struct ivhd_entry *)p;
352 switch (dev->type) {
353 case IVHD_DEV_SELECT:
354 case IVHD_DEV_RANGE_END:
355 case IVHD_DEV_ALIAS:
356 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200357 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200358 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200359 break;
360 default:
361 break;
362 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200363 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200364 }
365
366 WARN_ON(p != end);
367
368 return 0;
369}
370
Joerg Roedelb65233a2008-07-11 17:14:21 +0200371/*
372 * Iterate over all IVHD entries in the ACPI table and find the highest device
373 * id which we need to handle. This is the first of three functions which parse
374 * the ACPI table. So we check the checksum here.
375 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200376static int __init find_last_devid_acpi(struct acpi_table_header *table)
377{
378 int i;
379 u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
380 struct ivhd_header *h;
381
382 /*
383 * Validate checksum here so we don't need to do it when
384 * we actually parse the table
385 */
386 for (i = 0; i < table->length; ++i)
387 checksum += p[i];
388 if (checksum != 0)
389 /* ACPI table corrupt */
390 return -ENODEV;
391
392 p += IVRS_HEADER_LENGTH;
393
394 end += table->length;
395 while (p < end) {
396 h = (struct ivhd_header *)p;
397 switch (h->type) {
398 case ACPI_IVHD_TYPE:
399 find_last_devid_from_ivhd(h);
400 break;
401 default:
402 break;
403 }
404 p += h->length;
405 }
406 WARN_ON(p != end);
407
408 return 0;
409}
410
Joerg Roedelb65233a2008-07-11 17:14:21 +0200411/****************************************************************************
412 *
413 * The following functions belong the the code path which parses the ACPI table
414 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
415 * data structures, initialize the device/alias/rlookup table and also
416 * basically initialize the hardware.
417 *
418 ****************************************************************************/
419
420/*
421 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
422 * write commands to that buffer later and the IOMMU will execute them
423 * asynchronously
424 */
Joerg Roedelb36ca912008-06-26 21:27:45 +0200425static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
426{
Joerg Roedeld0312b22008-07-11 17:14:29 +0200427 u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelb36ca912008-06-26 21:27:45 +0200428 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200429
430 if (cmd_buf == NULL)
431 return NULL;
432
433 iommu->cmd_buf_size = CMD_BUFFER_SIZE;
434
Joerg Roedel58492e12009-05-04 18:41:16 +0200435 return cmd_buf;
436}
437
438/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200439 * This function resets the command buffer if the IOMMU stopped fetching
440 * commands from it.
441 */
442void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
443{
444 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
445
446 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
447 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
448
449 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
450}
451
452/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200453 * This function writes the command buffer address to the hardware and
454 * enables it.
455 */
456static void iommu_enable_command_buffer(struct amd_iommu *iommu)
457{
458 u64 entry;
459
460 BUG_ON(iommu->cmd_buf == NULL);
461
462 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200463 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200464
Joerg Roedelb36ca912008-06-26 21:27:45 +0200465 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200466 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200467
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200468 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200469}
470
471static void __init free_command_buffer(struct amd_iommu *iommu)
472{
Joerg Roedel23c17132008-09-17 17:18:17 +0200473 free_pages((unsigned long)iommu->cmd_buf,
474 get_order(iommu->cmd_buf_size));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200475}
476
Joerg Roedel335503e2008-09-05 14:29:07 +0200477/* allocates the memory where the IOMMU will log its events to */
478static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
479{
Joerg Roedel335503e2008-09-05 14:29:07 +0200480 iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
481 get_order(EVT_BUFFER_SIZE));
482
483 if (iommu->evt_buf == NULL)
484 return NULL;
485
Joerg Roedel1bc6f832009-07-02 18:32:05 +0200486 iommu->evt_buf_size = EVT_BUFFER_SIZE;
487
Joerg Roedel58492e12009-05-04 18:41:16 +0200488 return iommu->evt_buf;
489}
490
491static void iommu_enable_event_buffer(struct amd_iommu *iommu)
492{
493 u64 entry;
494
495 BUG_ON(iommu->evt_buf == NULL);
496
Joerg Roedel335503e2008-09-05 14:29:07 +0200497 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200498
Joerg Roedel335503e2008-09-05 14:29:07 +0200499 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
500 &entry, sizeof(entry));
501
Joerg Roedel090672072009-06-15 16:06:48 +0200502 /* set head and tail to zero manually */
503 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
504 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
505
Joerg Roedel58492e12009-05-04 18:41:16 +0200506 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200507}
508
509static void __init free_event_buffer(struct amd_iommu *iommu)
510{
511 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
512}
513
Joerg Roedelb65233a2008-07-11 17:14:21 +0200514/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200515static void set_dev_entry_bit(u16 devid, u8 bit)
516{
517 int i = (bit >> 5) & 0x07;
518 int _bit = bit & 0x1f;
519
520 amd_iommu_dev_table[devid].data[i] |= (1 << _bit);
521}
522
Joerg Roedelc5cca142009-10-09 18:31:20 +0200523static int get_dev_entry_bit(u16 devid, u8 bit)
524{
525 int i = (bit >> 5) & 0x07;
526 int _bit = bit & 0x1f;
527
528 return (amd_iommu_dev_table[devid].data[i] & (1 << _bit)) >> _bit;
529}
530
531
532void amd_iommu_apply_erratum_63(u16 devid)
533{
534 int sysmgt;
535
536 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
537 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
538
539 if (sysmgt == 0x01)
540 set_dev_entry_bit(devid, DEV_ENTRY_IW);
541}
542
Joerg Roedel5ff47892008-07-14 20:11:18 +0200543/* Writes the specific IOMMU for a device into the rlookup table */
544static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
545{
546 amd_iommu_rlookup_table[devid] = iommu;
547}
548
Joerg Roedelb65233a2008-07-11 17:14:21 +0200549/*
550 * This function takes the device specific flags read from the ACPI
551 * table and sets up the device table entry with that information
552 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200553static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
554 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200555{
556 if (flags & ACPI_DEVFLAG_INITPASS)
557 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
558 if (flags & ACPI_DEVFLAG_EXTINT)
559 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
560 if (flags & ACPI_DEVFLAG_NMI)
561 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
562 if (flags & ACPI_DEVFLAG_SYSMGT1)
563 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
564 if (flags & ACPI_DEVFLAG_SYSMGT2)
565 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
566 if (flags & ACPI_DEVFLAG_LINT0)
567 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
568 if (flags & ACPI_DEVFLAG_LINT1)
569 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200570
Joerg Roedelc5cca142009-10-09 18:31:20 +0200571 amd_iommu_apply_erratum_63(devid);
572
Joerg Roedel5ff47892008-07-14 20:11:18 +0200573 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200574}
575
Joerg Roedelb65233a2008-07-11 17:14:21 +0200576/*
577 * Reads the device exclusion range from ACPI and initialize IOMMU with
578 * it
579 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200580static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
581{
582 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
583
584 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
585 return;
586
587 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200588 /*
589 * We only can configure exclusion ranges per IOMMU, not
590 * per device. But we can enable the exclusion range per
591 * device. This is done here
592 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200593 set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
594 iommu->exclusion_start = m->range_start;
595 iommu->exclusion_length = m->range_length;
596 }
597}
598
Joerg Roedelb65233a2008-07-11 17:14:21 +0200599/*
600 * This function reads some important data from the IOMMU PCI space and
601 * initializes the driver data structure with it. It reads the hardware
602 * capabilities and the first/last device entries
603 */
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200604static void __init init_iommu_from_pci(struct amd_iommu *iommu)
605{
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200606 int cap_ptr = iommu->cap_ptr;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200607 u32 range, misc;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200608
Joerg Roedel3eaf28a2008-09-08 15:55:10 +0200609 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
610 &iommu->cap);
611 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
612 &range);
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200613 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
614 &misc);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200615
Joerg Roedeld591b0a2008-07-11 17:14:35 +0200616 iommu->first_device = calc_devid(MMIO_GET_BUS(range),
617 MMIO_GET_FD(range));
618 iommu->last_device = calc_devid(MMIO_GET_BUS(range),
619 MMIO_GET_LD(range));
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200620 iommu->evt_msi_num = MMIO_MSI_NUM(misc);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200621}
622
Joerg Roedelb65233a2008-07-11 17:14:21 +0200623/*
624 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
625 * initializes the hardware and our data structures with it.
626 */
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200627static void __init init_iommu_from_acpi(struct amd_iommu *iommu,
628 struct ivhd_header *h)
629{
630 u8 *p = (u8 *)h;
631 u8 *end = p, flags = 0;
632 u16 dev_i, devid = 0, devid_start = 0, devid_to = 0;
633 u32 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200634 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200635 struct ivhd_entry *e;
636
637 /*
638 * First set the recommended feature enable bits from ACPI
639 * into the IOMMU control registers
640 */
Joerg Roedel6da73422009-05-04 11:44:38 +0200641 h->flags & IVHD_FLAG_HT_TUN_EN_MASK ?
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200642 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
643 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
644
Joerg Roedel6da73422009-05-04 11:44:38 +0200645 h->flags & IVHD_FLAG_PASSPW_EN_MASK ?
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200646 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
647 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
648
Joerg Roedel6da73422009-05-04 11:44:38 +0200649 h->flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200650 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
651 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
652
Joerg Roedel6da73422009-05-04 11:44:38 +0200653 h->flags & IVHD_FLAG_ISOC_EN_MASK ?
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200654 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
655 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
656
657 /*
658 * make IOMMU memory accesses cache coherent
659 */
660 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
661
662 /*
663 * Done. Now parse the device entries
664 */
665 p += sizeof(struct ivhd_header);
666 end += h->length;
667
Joerg Roedel42a698f2009-05-20 15:41:28 +0200668
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200669 while (p < end) {
670 e = (struct ivhd_entry *)p;
671 switch (e->type) {
672 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200673
674 DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
675 " last device %02x:%02x.%x flags: %02x\n",
676 PCI_BUS(iommu->first_device),
677 PCI_SLOT(iommu->first_device),
678 PCI_FUNC(iommu->first_device),
679 PCI_BUS(iommu->last_device),
680 PCI_SLOT(iommu->last_device),
681 PCI_FUNC(iommu->last_device),
682 e->flags);
683
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200684 for (dev_i = iommu->first_device;
685 dev_i <= iommu->last_device; ++dev_i)
Joerg Roedel5ff47892008-07-14 20:11:18 +0200686 set_dev_entry_from_acpi(iommu, dev_i,
687 e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200688 break;
689 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200690
691 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
692 "flags: %02x\n",
693 PCI_BUS(e->devid),
694 PCI_SLOT(e->devid),
695 PCI_FUNC(e->devid),
696 e->flags);
697
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200698 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +0200699 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200700 break;
701 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200702
703 DUMP_printk(" DEV_SELECT_RANGE_START\t "
704 "devid: %02x:%02x.%x flags: %02x\n",
705 PCI_BUS(e->devid),
706 PCI_SLOT(e->devid),
707 PCI_FUNC(e->devid),
708 e->flags);
709
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200710 devid_start = e->devid;
711 flags = e->flags;
712 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200713 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200714 break;
715 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200716
717 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
718 "flags: %02x devid_to: %02x:%02x.%x\n",
719 PCI_BUS(e->devid),
720 PCI_SLOT(e->devid),
721 PCI_FUNC(e->devid),
722 e->flags,
723 PCI_BUS(e->ext >> 8),
724 PCI_SLOT(e->ext >> 8),
725 PCI_FUNC(e->ext >> 8));
726
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200727 devid = e->devid;
728 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200729 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +0100730 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200731 amd_iommu_alias_table[devid] = devid_to;
732 break;
733 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200734
735 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
736 "devid: %02x:%02x.%x flags: %02x "
737 "devid_to: %02x:%02x.%x\n",
738 PCI_BUS(e->devid),
739 PCI_SLOT(e->devid),
740 PCI_FUNC(e->devid),
741 e->flags,
742 PCI_BUS(e->ext >> 8),
743 PCI_SLOT(e->ext >> 8),
744 PCI_FUNC(e->ext >> 8));
745
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200746 devid_start = e->devid;
747 flags = e->flags;
748 devid_to = e->ext >> 8;
749 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200750 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200751 break;
752 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200753
754 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
755 "flags: %02x ext: %08x\n",
756 PCI_BUS(e->devid),
757 PCI_SLOT(e->devid),
758 PCI_FUNC(e->devid),
759 e->flags, e->ext);
760
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200761 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +0200762 set_dev_entry_from_acpi(iommu, devid, e->flags,
763 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200764 break;
765 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200766
767 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
768 "%02x:%02x.%x flags: %02x ext: %08x\n",
769 PCI_BUS(e->devid),
770 PCI_SLOT(e->devid),
771 PCI_FUNC(e->devid),
772 e->flags, e->ext);
773
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200774 devid_start = e->devid;
775 flags = e->flags;
776 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200777 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200778 break;
779 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +0200780
781 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
782 PCI_BUS(e->devid),
783 PCI_SLOT(e->devid),
784 PCI_FUNC(e->devid));
785
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200786 devid = e->devid;
787 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200788 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200789 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +0200790 set_dev_entry_from_acpi(iommu,
791 devid_to, flags, ext_flags);
792 }
793 set_dev_entry_from_acpi(iommu, dev_i,
794 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200795 }
796 break;
797 default:
798 break;
799 }
800
Joerg Roedelb514e552008-09-17 17:14:27 +0200801 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200802 }
803}
804
Joerg Roedelb65233a2008-07-11 17:14:21 +0200805/* Initializes the device->iommu mapping for the driver */
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200806static int __init init_iommu_devices(struct amd_iommu *iommu)
807{
808 u16 i;
809
810 for (i = iommu->first_device; i <= iommu->last_device; ++i)
811 set_iommu_for_device(iommu, i);
812
813 return 0;
814}
815
Joerg Roedele47d4022008-06-26 21:27:48 +0200816static void __init free_iommu_one(struct amd_iommu *iommu)
817{
818 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +0200819 free_event_buffer(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +0200820 iommu_unmap_mmio_space(iommu);
821}
822
823static void __init free_iommu_all(void)
824{
825 struct amd_iommu *iommu, *next;
826
Joerg Roedel3bd22172009-05-04 15:06:20 +0200827 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +0200828 list_del(&iommu->list);
829 free_iommu_one(iommu);
830 kfree(iommu);
831 }
832}
833
Joerg Roedelb65233a2008-07-11 17:14:21 +0200834/*
835 * This function clues the initialization function for one IOMMU
836 * together and also allocates the command buffer and programs the
837 * hardware. It does NOT enable the IOMMU. This is done afterwards.
838 */
Joerg Roedele47d4022008-06-26 21:27:48 +0200839static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
840{
841 spin_lock_init(&iommu->lock);
842 list_add_tail(&iommu->list, &amd_iommu_list);
843
844 /*
845 * Copy data from ACPI table entry to the iommu struct
846 */
Joerg Roedel3eaf28a2008-09-08 15:55:10 +0200847 iommu->dev = pci_get_bus_and_slot(PCI_BUS(h->devid), h->devid & 0xff);
848 if (!iommu->dev)
849 return 1;
850
Joerg Roedele47d4022008-06-26 21:27:48 +0200851 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +0200852 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +0200853 iommu->mmio_phys = h->mmio_phys;
854 iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
855 if (!iommu->mmio_base)
856 return -ENOMEM;
857
Joerg Roedele47d4022008-06-26 21:27:48 +0200858 iommu->cmd_buf = alloc_command_buffer(iommu);
859 if (!iommu->cmd_buf)
860 return -ENOMEM;
861
Joerg Roedel335503e2008-09-05 14:29:07 +0200862 iommu->evt_buf = alloc_event_buffer(iommu);
863 if (!iommu->evt_buf)
864 return -ENOMEM;
865
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200866 iommu->int_enabled = false;
867
Joerg Roedele47d4022008-06-26 21:27:48 +0200868 init_iommu_from_pci(iommu);
869 init_iommu_from_acpi(iommu, h);
870 init_iommu_devices(iommu);
871
Ingo Molnar8a667122008-10-12 15:24:53 +0200872 return pci_enable_device(iommu->dev);
Joerg Roedele47d4022008-06-26 21:27:48 +0200873}
874
Joerg Roedelb65233a2008-07-11 17:14:21 +0200875/*
876 * Iterates over all IOMMU entries in the ACPI table, allocates the
877 * IOMMU structure and initializes it with init_iommu_one()
878 */
Joerg Roedele47d4022008-06-26 21:27:48 +0200879static int __init init_iommu_all(struct acpi_table_header *table)
880{
881 u8 *p = (u8 *)table, *end = (u8 *)table;
882 struct ivhd_header *h;
883 struct amd_iommu *iommu;
884 int ret;
885
Joerg Roedele47d4022008-06-26 21:27:48 +0200886 end += table->length;
887 p += IVRS_HEADER_LENGTH;
888
889 while (p < end) {
890 h = (struct ivhd_header *)p;
891 switch (*p) {
892 case ACPI_IVHD_TYPE:
Joerg Roedel9c720412009-05-20 13:53:57 +0200893
Joerg Roedelae908c22009-09-01 16:52:16 +0200894 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +0200895 "seg: %d flags: %01x info %04x\n",
896 PCI_BUS(h->devid), PCI_SLOT(h->devid),
897 PCI_FUNC(h->devid), h->cap_ptr,
898 h->pci_seg, h->flags, h->info);
899 DUMP_printk(" mmio-addr: %016llx\n",
900 h->mmio_phys);
901
Joerg Roedele47d4022008-06-26 21:27:48 +0200902 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
903 if (iommu == NULL)
904 return -ENOMEM;
905 ret = init_iommu_one(iommu, h);
906 if (ret)
907 return ret;
908 break;
909 default:
910 break;
911 }
912 p += h->length;
913
914 }
915 WARN_ON(p != end);
916
917 return 0;
918}
919
Joerg Roedelb65233a2008-07-11 17:14:21 +0200920/****************************************************************************
921 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200922 * The following functions initialize the MSI interrupts for all IOMMUs
923 * in the system. Its a bit challenging because there could be multiple
924 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
925 * pci_dev.
926 *
927 ****************************************************************************/
928
Joerg Roedel9f800de2009-11-23 12:45:25 +0100929static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200930{
931 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200932
933 if (pci_enable_msi(iommu->dev))
934 return 1;
935
936 r = request_irq(iommu->dev->irq, amd_iommu_int_handler,
937 IRQF_SAMPLE_RANDOM,
Joerg Roedel4c6f40d2009-09-01 16:43:58 +0200938 "AMD-Vi",
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200939 NULL);
940
941 if (r) {
942 pci_disable_msi(iommu->dev);
943 return 1;
944 }
945
Joerg Roedelfab6afa2009-05-04 18:46:34 +0200946 iommu->int_enabled = true;
Joerg Roedel58492e12009-05-04 18:41:16 +0200947 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
948
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200949 return 0;
950}
951
Joerg Roedel05f92db2009-05-12 09:52:46 +0200952static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200953{
954 if (iommu->int_enabled)
955 return 0;
956
Joerg Roedeld91cecd2009-05-04 18:51:00 +0200957 if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
Joerg Roedela80dc3e2008-09-11 16:51:41 +0200958 return iommu_setup_msi(iommu);
959
960 return 1;
961}
962
963/****************************************************************************
964 *
Joerg Roedelb65233a2008-07-11 17:14:21 +0200965 * The next functions belong to the third pass of parsing the ACPI
966 * table. In this last pass the memory mapping requirements are
967 * gathered (like exclusion and unity mapping reanges).
968 *
969 ****************************************************************************/
970
Joerg Roedelbe2a0222008-06-26 21:27:49 +0200971static void __init free_unity_maps(void)
972{
973 struct unity_map_entry *entry, *next;
974
975 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
976 list_del(&entry->list);
977 kfree(entry);
978 }
979}
980
Joerg Roedelb65233a2008-07-11 17:14:21 +0200981/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +0200982static int __init init_exclusion_range(struct ivmd_header *m)
983{
984 int i;
985
986 switch (m->type) {
987 case ACPI_IVMD_TYPE:
988 set_device_exclusion_range(m->devid, m);
989 break;
990 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +0200991 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +0200992 set_device_exclusion_range(i, m);
993 break;
994 case ACPI_IVMD_TYPE_RANGE:
995 for (i = m->devid; i <= m->aux; ++i)
996 set_device_exclusion_range(i, m);
997 break;
998 default:
999 break;
1000 }
1001
1002 return 0;
1003}
1004
Joerg Roedelb65233a2008-07-11 17:14:21 +02001005/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001006static int __init init_unity_map_range(struct ivmd_header *m)
1007{
1008 struct unity_map_entry *e = 0;
Joerg Roedel02acc432009-05-20 16:24:21 +02001009 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001010
1011 e = kzalloc(sizeof(*e), GFP_KERNEL);
1012 if (e == NULL)
1013 return -ENOMEM;
1014
1015 switch (m->type) {
1016 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001017 kfree(e);
1018 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001019 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001020 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001021 e->devid_start = e->devid_end = m->devid;
1022 break;
1023 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001024 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001025 e->devid_start = 0;
1026 e->devid_end = amd_iommu_last_bdf;
1027 break;
1028 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001029 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001030 e->devid_start = m->devid;
1031 e->devid_end = m->aux;
1032 break;
1033 }
1034 e->address_start = PAGE_ALIGN(m->range_start);
1035 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1036 e->prot = m->flags >> 1;
1037
Joerg Roedel02acc432009-05-20 16:24:21 +02001038 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1039 " range_start: %016llx range_end: %016llx flags: %x\n", s,
1040 PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
1041 PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
1042 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1043 e->address_start, e->address_end, m->flags);
1044
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001045 list_add_tail(&e->list, &amd_iommu_unity_map);
1046
1047 return 0;
1048}
1049
Joerg Roedelb65233a2008-07-11 17:14:21 +02001050/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001051static int __init init_memory_definitions(struct acpi_table_header *table)
1052{
1053 u8 *p = (u8 *)table, *end = (u8 *)table;
1054 struct ivmd_header *m;
1055
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001056 end += table->length;
1057 p += IVRS_HEADER_LENGTH;
1058
1059 while (p < end) {
1060 m = (struct ivmd_header *)p;
1061 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1062 init_exclusion_range(m);
1063 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1064 init_unity_map_range(m);
1065
1066 p += m->length;
1067 }
1068
1069 return 0;
1070}
1071
Joerg Roedelb65233a2008-07-11 17:14:21 +02001072/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001073 * Init the device table to not allow DMA access for devices and
1074 * suppress all page faults
1075 */
1076static void init_device_table(void)
1077{
1078 u16 devid;
1079
1080 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1081 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1082 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001083 }
1084}
1085
1086/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001087 * This function finally enables all IOMMUs found in the system after
1088 * they have been initialized
1089 */
Joerg Roedel05f92db2009-05-12 09:52:46 +02001090static void enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02001091{
1092 struct amd_iommu *iommu;
1093
Joerg Roedel3bd22172009-05-04 15:06:20 +02001094 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02001095 iommu_disable(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02001096 iommu_set_device_table(iommu);
1097 iommu_enable_command_buffer(iommu);
1098 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001099 iommu_set_exclusion_range(iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001100 iommu_init_msi(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02001101 iommu_enable(iommu);
1102 }
1103}
1104
Joerg Roedel92ac4322009-05-19 19:06:27 +02001105static void disable_iommus(void)
1106{
1107 struct amd_iommu *iommu;
1108
1109 for_each_iommu(iommu)
1110 iommu_disable(iommu);
1111}
1112
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001113/*
1114 * Suspend/Resume support
1115 * disable suspend until real resume implemented
1116 */
1117
1118static int amd_iommu_resume(struct sys_device *dev)
1119{
Joerg Roedel736501e2009-05-12 09:56:12 +02001120 /* re-load the hardware */
1121 enable_iommus();
1122
1123 /*
1124 * we have to flush after the IOMMUs are enabled because a
1125 * disabled IOMMU will never execute the commands we send
1126 */
Joerg Roedel736501e2009-05-12 09:56:12 +02001127 amd_iommu_flush_all_devices();
Chris Wright6a047d82009-06-16 03:01:37 -04001128 amd_iommu_flush_all_domains();
Joerg Roedel736501e2009-05-12 09:56:12 +02001129
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001130 return 0;
1131}
1132
1133static int amd_iommu_suspend(struct sys_device *dev, pm_message_t state)
1134{
Joerg Roedel736501e2009-05-12 09:56:12 +02001135 /* disable IOMMUs to go out of the way for BIOS */
1136 disable_iommus();
1137
1138 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001139}
1140
1141static struct sysdev_class amd_iommu_sysdev_class = {
1142 .name = "amd_iommu",
1143 .suspend = amd_iommu_suspend,
1144 .resume = amd_iommu_resume,
1145};
1146
1147static struct sys_device device_amd_iommu = {
1148 .id = 0,
1149 .cls = &amd_iommu_sysdev_class,
1150};
1151
Joerg Roedelb65233a2008-07-11 17:14:21 +02001152/*
1153 * This is the core init function for AMD IOMMU hardware in the system.
1154 * This function is called from the generic x86 DMA layer initialization
1155 * code.
1156 *
1157 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
1158 * three times:
1159 *
1160 * 1 pass) Find the highest PCI device id the driver has to handle.
1161 * Upon this information the size of the data structures is
1162 * determined that needs to be allocated.
1163 *
1164 * 2 pass) Initialize the data structures just allocated with the
1165 * information in the ACPI table about available AMD IOMMUs
1166 * in the system. It also maps the PCI devices in the
1167 * system to specific IOMMUs
1168 *
1169 * 3 pass) After the basic data structures are allocated and
1170 * initialized we update them with information about memory
1171 * remapping requirements parsed out of the ACPI table in
1172 * this last pass.
1173 *
1174 * After that the hardware is initialized and ready to go. In the last
1175 * step we do some Linux specific things like registering the driver in
1176 * the dma_ops interface and initializing the suspend/resume support
1177 * functions. Finally it prints some information about AMD IOMMUs and
1178 * the driver state and enables the hardware.
1179 */
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +09001180static int __init amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001181{
1182 int i, ret = 0;
1183
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001184 /*
1185 * First parse ACPI tables to find the largest Bus/Dev/Func
1186 * we need to handle. Upon this information the shared data
1187 * structures for the IOMMUs in the system will be allocated
1188 */
1189 if (acpi_table_parse("IVRS", find_last_devid_acpi) != 0)
1190 return -ENODEV;
1191
Joerg Roedelc5714842008-07-11 17:14:25 +02001192 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
1193 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
1194 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001195
1196 ret = -ENOMEM;
1197
1198 /* Device table - directly used by all IOMMUs */
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001199 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001200 get_order(dev_table_size));
1201 if (amd_iommu_dev_table == NULL)
1202 goto out;
1203
1204 /*
1205 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
1206 * IOMMU see for that device
1207 */
1208 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
1209 get_order(alias_table_size));
1210 if (amd_iommu_alias_table == NULL)
1211 goto free;
1212
1213 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01001214 amd_iommu_rlookup_table = (void *)__get_free_pages(
1215 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001216 get_order(rlookup_table_size));
1217 if (amd_iommu_rlookup_table == NULL)
1218 goto free;
1219
1220 /*
1221 * Protection Domain table - maps devices to protection domains
1222 * This table has the same size as the rlookup_table
1223 */
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001224 amd_iommu_pd_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001225 get_order(rlookup_table_size));
1226 if (amd_iommu_pd_table == NULL)
1227 goto free;
1228
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001229 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
1230 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001231 get_order(MAX_DOMAIN_ID/8));
1232 if (amd_iommu_pd_alloc_bitmap == NULL)
1233 goto free;
1234
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001235 /* init the device table */
1236 init_device_table();
1237
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001238 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02001239 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001240 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001241 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001242 amd_iommu_alias_table[i] = i;
1243
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001244 /*
1245 * never allocate domain 0 because its used as the non-allocated and
1246 * error value placeholder
1247 */
1248 amd_iommu_pd_alloc_bitmap[0] = 1;
1249
1250 /*
1251 * now the data structures are allocated and basically initialized
1252 * start the real acpi table scan
1253 */
1254 ret = -ENODEV;
1255 if (acpi_table_parse("IVRS", init_iommu_all) != 0)
1256 goto free;
1257
1258 if (acpi_table_parse("IVRS", init_memory_definitions) != 0)
1259 goto free;
1260
Joerg Roedel7441e9c2008-06-30 20:18:02 +02001261 ret = sysdev_class_register(&amd_iommu_sysdev_class);
1262 if (ret)
1263 goto free;
1264
1265 ret = sysdev_register(&device_amd_iommu);
1266 if (ret)
1267 goto free;
1268
Joerg Roedel4751a952009-09-01 15:53:54 +02001269 if (iommu_pass_through)
1270 ret = amd_iommu_init_passthrough();
1271 else
1272 ret = amd_iommu_init_dma_ops();
Joerg Roedel129d6ab2008-08-14 19:55:18 +02001273 if (ret)
1274 goto free;
1275
Joerg Roedel87361972008-06-26 21:28:07 +02001276 enable_iommus();
1277
Joerg Roedel4751a952009-09-01 15:53:54 +02001278 if (iommu_pass_through)
1279 goto out;
1280
Joerg Roedel4c6f40d2009-09-01 16:43:58 +02001281 printk(KERN_INFO "AMD-Vi: device isolation ");
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001282 if (amd_iommu_isolate)
1283 printk("enabled\n");
1284 else
1285 printk("disabled\n");
1286
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09001287 if (amd_iommu_unmap_flush)
Joerg Roedel4c6f40d2009-09-01 16:43:58 +02001288 printk(KERN_INFO "AMD-Vi: IO/TLB flush on unmap enabled\n");
Joerg Roedel1c655772008-09-04 18:40:05 +02001289 else
Joerg Roedel4c6f40d2009-09-01 16:43:58 +02001290 printk(KERN_INFO "AMD-Vi: Lazy IO/TLB flushing enabled\n");
Joerg Roedel1c655772008-09-04 18:40:05 +02001291
FUJITA Tomonori338bac52009-10-27 16:34:44 +09001292 x86_platform.iommu_shutdown = disable_iommus;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001293out:
1294 return ret;
1295
1296free:
Joerg Roedeld58befd2008-09-17 12:19:58 +02001297 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
1298 get_order(MAX_DOMAIN_ID/8));
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001299
Joerg Roedel9a836de2008-07-11 17:14:26 +02001300 free_pages((unsigned long)amd_iommu_pd_table,
1301 get_order(rlookup_table_size));
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001302
Joerg Roedel9a836de2008-07-11 17:14:26 +02001303 free_pages((unsigned long)amd_iommu_rlookup_table,
1304 get_order(rlookup_table_size));
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001305
Joerg Roedel9a836de2008-07-11 17:14:26 +02001306 free_pages((unsigned long)amd_iommu_alias_table,
1307 get_order(alias_table_size));
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001308
Joerg Roedel9a836de2008-07-11 17:14:26 +02001309 free_pages((unsigned long)amd_iommu_dev_table,
1310 get_order(dev_table_size));
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02001311
1312 free_iommu_all();
1313
1314 free_unity_maps();
1315
1316 goto out;
1317}
1318
Joerg Roedelb65233a2008-07-11 17:14:21 +02001319/****************************************************************************
1320 *
1321 * Early detect code. This code runs at IOMMU detection time in the DMA
1322 * layer. It just looks if there is an IVRS ACPI table to detect AMD
1323 * IOMMUs
1324 *
1325 ****************************************************************************/
Joerg Roedelae7877d2008-06-26 21:27:51 +02001326static int __init early_amd_iommu_detect(struct acpi_table_header *table)
1327{
1328 return 0;
1329}
1330
1331void __init amd_iommu_detect(void)
1332{
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09001333 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Joerg Roedelae7877d2008-06-26 21:27:51 +02001334 return;
1335
Joerg Roedelae7877d2008-06-26 21:27:51 +02001336 if (acpi_table_parse("IVRS", early_amd_iommu_detect) == 0) {
1337 iommu_detected = 1;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +02001338 amd_iommu_detected = 1;
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +09001339 x86_init.iommu.iommu_init = amd_iommu_init;
Joerg Roedelae7877d2008-06-26 21:27:51 +02001340 }
1341}
1342
Joerg Roedelb65233a2008-07-11 17:14:21 +02001343/****************************************************************************
1344 *
1345 * Parsing functions for the AMD IOMMU specific kernel command line
1346 * options.
1347 *
1348 ****************************************************************************/
1349
Joerg Roedelfefda112009-05-20 12:21:42 +02001350static int __init parse_amd_iommu_dump(char *str)
1351{
1352 amd_iommu_dump = true;
1353
1354 return 1;
1355}
1356
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001357static int __init parse_amd_iommu_options(char *str)
1358{
1359 for (; *str; ++str) {
Joerg Roedel1c655772008-09-04 18:40:05 +02001360 if (strncmp(str, "isolate", 7) == 0)
Joerg Roedelc226f852008-12-12 13:53:54 +01001361 amd_iommu_isolate = true;
Joerg Roedele5e1f602008-11-17 15:07:17 +01001362 if (strncmp(str, "share", 5) == 0)
Joerg Roedelc226f852008-12-12 13:53:54 +01001363 amd_iommu_isolate = false;
Joerg Roedel695b5672008-11-17 15:16:43 +01001364 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09001365 amd_iommu_unmap_flush = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001366 }
1367
1368 return 1;
1369}
1370
Joerg Roedelfefda112009-05-20 12:21:42 +02001371__setup("amd_iommu_dump", parse_amd_iommu_dump);
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001372__setup("amd_iommu=", parse_amd_iommu_options);