blob: 6e1c8ffc0c5bd8b16e0e5ebedf841bb8a137e9f5 [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 Roedelf6e2e6b2008-06-26 21:27:39 +020025#include <asm/pci-direct.h>
26#include <asm/amd_iommu_types.h>
Joerg Roedelc6da9922008-06-26 21:28:06 +020027#include <asm/amd_iommu.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020028#include <asm/gart.h>
29
30/*
31 * definitions for the ACPI scanning code
32 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020033#define DEVID(bus, devfn) (((bus) << 8) | (devfn))
34#define PCI_BUS(x) (((x) >> 8) & 0xff)
35#define IVRS_HEADER_LENGTH 48
36#define TBL_SIZE(x) (1 << (PAGE_SHIFT + get_order(amd_iommu_last_bdf * (x))))
37
38#define ACPI_IVHD_TYPE 0x10
39#define ACPI_IVMD_TYPE_ALL 0x20
40#define ACPI_IVMD_TYPE 0x21
41#define ACPI_IVMD_TYPE_RANGE 0x22
42
43#define IVHD_DEV_ALL 0x01
44#define IVHD_DEV_SELECT 0x02
45#define IVHD_DEV_SELECT_RANGE_START 0x03
46#define IVHD_DEV_RANGE_END 0x04
47#define IVHD_DEV_ALIAS 0x42
48#define IVHD_DEV_ALIAS_RANGE 0x43
49#define IVHD_DEV_EXT_SELECT 0x46
50#define IVHD_DEV_EXT_SELECT_RANGE 0x47
51
52#define IVHD_FLAG_HT_TUN_EN 0x00
53#define IVHD_FLAG_PASSPW_EN 0x01
54#define IVHD_FLAG_RESPASSPW_EN 0x02
55#define IVHD_FLAG_ISOC_EN 0x03
56
57#define IVMD_FLAG_EXCL_RANGE 0x08
58#define IVMD_FLAG_UNITY_MAP 0x01
59
60#define ACPI_DEVFLAG_INITPASS 0x01
61#define ACPI_DEVFLAG_EXTINT 0x02
62#define ACPI_DEVFLAG_NMI 0x04
63#define ACPI_DEVFLAG_SYSMGT1 0x10
64#define ACPI_DEVFLAG_SYSMGT2 0x20
65#define ACPI_DEVFLAG_LINT0 0x40
66#define ACPI_DEVFLAG_LINT1 0x80
67#define ACPI_DEVFLAG_ATSDIS 0x10000000
68
Joerg Roedelb65233a2008-07-11 17:14:21 +020069/*
70 * ACPI table definitions
71 *
72 * These data structures are laid over the table to parse the important values
73 * out of it.
74 */
75
76/*
77 * structure describing one IOMMU in the ACPI table. Typically followed by one
78 * or more ivhd_entrys.
79 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020080struct ivhd_header {
81 u8 type;
82 u8 flags;
83 u16 length;
84 u16 devid;
85 u16 cap_ptr;
86 u64 mmio_phys;
87 u16 pci_seg;
88 u16 info;
89 u32 reserved;
90} __attribute__((packed));
91
Joerg Roedelb65233a2008-07-11 17:14:21 +020092/*
93 * A device entry describing which devices a specific IOMMU translates and
94 * which requestor ids they use.
95 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020096struct ivhd_entry {
97 u8 type;
98 u16 devid;
99 u8 flags;
100 u32 ext;
101} __attribute__((packed));
102
Joerg Roedelb65233a2008-07-11 17:14:21 +0200103/*
104 * An AMD IOMMU memory definition structure. It defines things like exclusion
105 * ranges for devices and regions that should be unity mapped.
106 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200107struct ivmd_header {
108 u8 type;
109 u8 flags;
110 u16 length;
111 u16 devid;
112 u16 aux;
113 u64 resv;
114 u64 range_start;
115 u64 range_length;
116} __attribute__((packed));
117
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200118static int __initdata amd_iommu_detected;
119
Joerg Roedelb65233a2008-07-11 17:14:21 +0200120u16 amd_iommu_last_bdf; /* largest PCI device id we have
121 to handle */
122struct list_head amd_iommu_unity_map; /* a list of required unity mappings
123 we find in ACPI */
124unsigned amd_iommu_aperture_order = 26; /* size of aperture in power of 2 */
125int amd_iommu_isolate; /* if 1, device isolation is enabled */
Joerg Roedel928abd22008-06-26 21:27:40 +0200126
Joerg Roedelb65233a2008-07-11 17:14:21 +0200127struct list_head amd_iommu_list; /* list of all AMD IOMMUs in the
128 system */
129
130/*
131 * Pointer to the device table which is shared by all AMD IOMMUs
132 * it is indexed by the PCI device id or the HT unit id and contains
133 * information about the domain the device belongs to as well as the
134 * page table root pointer.
135 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200136struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200137
138/*
139 * The alias table is a driver specific data structure which contains the
140 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
141 * More than one device can share the same requestor id.
142 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200143u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200144
145/*
146 * The rlookup table is used to find the IOMMU which is responsible
147 * for a specific device. It is also indexed by the PCI device id.
148 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200149struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200150
151/*
152 * The pd table (protection domain table) is used to find the protection domain
153 * data structure a device belongs to. Indexed with the PCI device id too.
154 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200155struct protection_domain **amd_iommu_pd_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200156
157/*
158 * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
159 * to know which ones are already in use.
160 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200161unsigned long *amd_iommu_pd_alloc_bitmap;
162
Joerg Roedelb65233a2008-07-11 17:14:21 +0200163static u32 dev_table_size; /* size of the device table */
164static u32 alias_table_size; /* size of the alias table */
165static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200166
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200167static inline void update_last_devid(u16 devid)
168{
169 if (devid > amd_iommu_last_bdf)
170 amd_iommu_last_bdf = devid;
171}
172
Joerg Roedelb65233a2008-07-11 17:14:21 +0200173/****************************************************************************
174 *
175 * AMD IOMMU MMIO register space handling functions
176 *
177 * These functions are used to program the IOMMU device registers in
178 * MMIO space required for that driver.
179 *
180 ****************************************************************************/
181
182/*
183 * This function set the exclusion range in the IOMMU. DMA accesses to the
184 * exclusion range are passed through untranslated
185 */
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200186static void __init iommu_set_exclusion_range(struct amd_iommu *iommu)
187{
188 u64 start = iommu->exclusion_start & PAGE_MASK;
189 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
190 u64 entry;
191
192 if (!iommu->exclusion_start)
193 return;
194
195 entry = start | MMIO_EXCL_ENABLE_MASK;
196 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
197 &entry, sizeof(entry));
198
199 entry = limit;
200 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
201 &entry, sizeof(entry));
202}
203
Joerg Roedelb65233a2008-07-11 17:14:21 +0200204/* Programs the physical address of the device table into the IOMMU hardware */
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200205static void __init iommu_set_device_table(struct amd_iommu *iommu)
206{
207 u32 entry;
208
209 BUG_ON(iommu->mmio_base == NULL);
210
211 entry = virt_to_phys(amd_iommu_dev_table);
212 entry |= (dev_table_size >> 12) - 1;
213 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
214 &entry, sizeof(entry));
215}
216
Joerg Roedelb65233a2008-07-11 17:14:21 +0200217/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200218static void __init iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
219{
220 u32 ctrl;
221
222 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
223 ctrl |= (1 << bit);
224 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
225}
226
227static void __init iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
228{
229 u32 ctrl;
230
231 ctrl = (u64)readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
232 ctrl &= ~(1 << bit);
233 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
234}
235
Joerg Roedelb65233a2008-07-11 17:14:21 +0200236/* Function to enable the hardware */
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200237void __init iommu_enable(struct amd_iommu *iommu)
238{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200239 printk(KERN_INFO "AMD IOMMU: Enabling IOMMU at ");
240 print_devid(iommu->devid, 0);
241 printk(" cap 0x%hx\n", iommu->cap_ptr);
242
243 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200244}
245
Joerg Roedelb65233a2008-07-11 17:14:21 +0200246/*
247 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
248 * the system has one.
249 */
Joerg Roedel6c567472008-06-26 21:27:43 +0200250static u8 * __init iommu_map_mmio_space(u64 address)
251{
252 u8 *ret;
253
254 if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu"))
255 return NULL;
256
257 ret = ioremap_nocache(address, MMIO_REGION_LENGTH);
258 if (ret != NULL)
259 return ret;
260
261 release_mem_region(address, MMIO_REGION_LENGTH);
262
263 return NULL;
264}
265
266static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
267{
268 if (iommu->mmio_base)
269 iounmap(iommu->mmio_base);
270 release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
271}
272
Joerg Roedelb65233a2008-07-11 17:14:21 +0200273/****************************************************************************
274 *
275 * The functions below belong to the first pass of AMD IOMMU ACPI table
276 * parsing. In this pass we try to find out the highest device id this
277 * code has to handle. Upon this information the size of the shared data
278 * structures is determined later.
279 *
280 ****************************************************************************/
281
282/*
283 * This function reads the last device id the IOMMU has to handle from the PCI
284 * capability header for this IOMMU
285 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200286static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
287{
288 u32 cap;
289
290 cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200291 update_last_devid(DEVID(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200292
293 return 0;
294}
295
Joerg Roedelb65233a2008-07-11 17:14:21 +0200296/*
297 * After reading the highest device id from the IOMMU PCI capability header
298 * this function looks if there is a higher device id defined in the ACPI table
299 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200300static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
301{
302 u8 *p = (void *)h, *end = (void *)h;
303 struct ivhd_entry *dev;
304
305 p += sizeof(*h);
306 end += h->length;
307
308 find_last_devid_on_pci(PCI_BUS(h->devid),
309 PCI_SLOT(h->devid),
310 PCI_FUNC(h->devid),
311 h->cap_ptr);
312
313 while (p < end) {
314 dev = (struct ivhd_entry *)p;
315 switch (dev->type) {
316 case IVHD_DEV_SELECT:
317 case IVHD_DEV_RANGE_END:
318 case IVHD_DEV_ALIAS:
319 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200320 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200321 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200322 break;
323 default:
324 break;
325 }
326 p += 0x04 << (*p >> 6);
327 }
328
329 WARN_ON(p != end);
330
331 return 0;
332}
333
Joerg Roedelb65233a2008-07-11 17:14:21 +0200334/*
335 * Iterate over all IVHD entries in the ACPI table and find the highest device
336 * id which we need to handle. This is the first of three functions which parse
337 * the ACPI table. So we check the checksum here.
338 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200339static int __init find_last_devid_acpi(struct acpi_table_header *table)
340{
341 int i;
342 u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
343 struct ivhd_header *h;
344
345 /*
346 * Validate checksum here so we don't need to do it when
347 * we actually parse the table
348 */
349 for (i = 0; i < table->length; ++i)
350 checksum += p[i];
351 if (checksum != 0)
352 /* ACPI table corrupt */
353 return -ENODEV;
354
355 p += IVRS_HEADER_LENGTH;
356
357 end += table->length;
358 while (p < end) {
359 h = (struct ivhd_header *)p;
360 switch (h->type) {
361 case ACPI_IVHD_TYPE:
362 find_last_devid_from_ivhd(h);
363 break;
364 default:
365 break;
366 }
367 p += h->length;
368 }
369 WARN_ON(p != end);
370
371 return 0;
372}
373
Joerg Roedelb65233a2008-07-11 17:14:21 +0200374/****************************************************************************
375 *
376 * The following functions belong the the code path which parses the ACPI table
377 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
378 * data structures, initialize the device/alias/rlookup table and also
379 * basically initialize the hardware.
380 *
381 ****************************************************************************/
382
383/*
384 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
385 * write commands to that buffer later and the IOMMU will execute them
386 * asynchronously
387 */
Joerg Roedelb36ca912008-06-26 21:27:45 +0200388static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
389{
390 u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL,
391 get_order(CMD_BUFFER_SIZE));
392 u64 entry = 0;
393
394 if (cmd_buf == NULL)
395 return NULL;
396
397 iommu->cmd_buf_size = CMD_BUFFER_SIZE;
398
399 memset(cmd_buf, 0, CMD_BUFFER_SIZE);
400
401 entry = (u64)virt_to_phys(cmd_buf);
402 entry |= MMIO_CMD_SIZE_512;
403 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
404 &entry, sizeof(entry));
405
406 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
407
408 return cmd_buf;
409}
410
411static void __init free_command_buffer(struct amd_iommu *iommu)
412{
413 if (iommu->cmd_buf)
414 free_pages((unsigned long)iommu->cmd_buf,
415 get_order(CMD_BUFFER_SIZE));
416}
417
Joerg Roedelb65233a2008-07-11 17:14:21 +0200418/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200419static void set_dev_entry_bit(u16 devid, u8 bit)
420{
421 int i = (bit >> 5) & 0x07;
422 int _bit = bit & 0x1f;
423
424 amd_iommu_dev_table[devid].data[i] |= (1 << _bit);
425}
426
Joerg Roedelb65233a2008-07-11 17:14:21 +0200427/*
428 * This function takes the device specific flags read from the ACPI
429 * table and sets up the device table entry with that information
430 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200431static void __init set_dev_entry_from_acpi(u16 devid, u32 flags, u32 ext_flags)
432{
433 if (flags & ACPI_DEVFLAG_INITPASS)
434 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
435 if (flags & ACPI_DEVFLAG_EXTINT)
436 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
437 if (flags & ACPI_DEVFLAG_NMI)
438 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
439 if (flags & ACPI_DEVFLAG_SYSMGT1)
440 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
441 if (flags & ACPI_DEVFLAG_SYSMGT2)
442 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
443 if (flags & ACPI_DEVFLAG_LINT0)
444 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
445 if (flags & ACPI_DEVFLAG_LINT1)
446 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
447}
448
Joerg Roedelb65233a2008-07-11 17:14:21 +0200449/* Writes the specific IOMMU for a device into the rlookup table */
Joerg Roedel3566b772008-06-26 21:27:46 +0200450static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
451{
452 amd_iommu_rlookup_table[devid] = iommu;
453}
454
Joerg Roedelb65233a2008-07-11 17:14:21 +0200455/*
456 * Reads the device exclusion range from ACPI and initialize IOMMU with
457 * it
458 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200459static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
460{
461 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
462
463 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
464 return;
465
466 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200467 /*
468 * We only can configure exclusion ranges per IOMMU, not
469 * per device. But we can enable the exclusion range per
470 * device. This is done here
471 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200472 set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
473 iommu->exclusion_start = m->range_start;
474 iommu->exclusion_length = m->range_length;
475 }
476}
477
Joerg Roedelb65233a2008-07-11 17:14:21 +0200478/*
479 * This function reads some important data from the IOMMU PCI space and
480 * initializes the driver data structure with it. It reads the hardware
481 * capabilities and the first/last device entries
482 */
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200483static void __init init_iommu_from_pci(struct amd_iommu *iommu)
484{
485 int bus = PCI_BUS(iommu->devid);
486 int dev = PCI_SLOT(iommu->devid);
487 int fn = PCI_FUNC(iommu->devid);
488 int cap_ptr = iommu->cap_ptr;
489 u32 range;
490
491 iommu->cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_CAP_HDR_OFFSET);
492
493 range = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
494 iommu->first_device = DEVID(MMIO_GET_BUS(range), MMIO_GET_FD(range));
495 iommu->last_device = DEVID(MMIO_GET_BUS(range), MMIO_GET_LD(range));
496}
497
Joerg Roedelb65233a2008-07-11 17:14:21 +0200498/*
499 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
500 * initializes the hardware and our data structures with it.
501 */
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200502static void __init init_iommu_from_acpi(struct amd_iommu *iommu,
503 struct ivhd_header *h)
504{
505 u8 *p = (u8 *)h;
506 u8 *end = p, flags = 0;
507 u16 dev_i, devid = 0, devid_start = 0, devid_to = 0;
508 u32 ext_flags = 0;
509 bool alias = 0;
510 struct ivhd_entry *e;
511
512 /*
513 * First set the recommended feature enable bits from ACPI
514 * into the IOMMU control registers
515 */
516 h->flags & IVHD_FLAG_HT_TUN_EN ?
517 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
518 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
519
520 h->flags & IVHD_FLAG_PASSPW_EN ?
521 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
522 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
523
524 h->flags & IVHD_FLAG_RESPASSPW_EN ?
525 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
526 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
527
528 h->flags & IVHD_FLAG_ISOC_EN ?
529 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
530 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
531
532 /*
533 * make IOMMU memory accesses cache coherent
534 */
535 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
536
537 /*
538 * Done. Now parse the device entries
539 */
540 p += sizeof(struct ivhd_header);
541 end += h->length;
542
543 while (p < end) {
544 e = (struct ivhd_entry *)p;
545 switch (e->type) {
546 case IVHD_DEV_ALL:
547 for (dev_i = iommu->first_device;
548 dev_i <= iommu->last_device; ++dev_i)
549 set_dev_entry_from_acpi(dev_i, e->flags, 0);
550 break;
551 case IVHD_DEV_SELECT:
552 devid = e->devid;
553 set_dev_entry_from_acpi(devid, e->flags, 0);
554 break;
555 case IVHD_DEV_SELECT_RANGE_START:
556 devid_start = e->devid;
557 flags = e->flags;
558 ext_flags = 0;
559 alias = 0;
560 break;
561 case IVHD_DEV_ALIAS:
562 devid = e->devid;
563 devid_to = e->ext >> 8;
564 set_dev_entry_from_acpi(devid, e->flags, 0);
565 amd_iommu_alias_table[devid] = devid_to;
566 break;
567 case IVHD_DEV_ALIAS_RANGE:
568 devid_start = e->devid;
569 flags = e->flags;
570 devid_to = e->ext >> 8;
571 ext_flags = 0;
572 alias = 1;
573 break;
574 case IVHD_DEV_EXT_SELECT:
575 devid = e->devid;
576 set_dev_entry_from_acpi(devid, e->flags, e->ext);
577 break;
578 case IVHD_DEV_EXT_SELECT_RANGE:
579 devid_start = e->devid;
580 flags = e->flags;
581 ext_flags = e->ext;
582 alias = 0;
583 break;
584 case IVHD_DEV_RANGE_END:
585 devid = e->devid;
586 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
587 if (alias)
588 amd_iommu_alias_table[dev_i] = devid_to;
589 set_dev_entry_from_acpi(
590 amd_iommu_alias_table[dev_i],
591 flags, ext_flags);
592 }
593 break;
594 default:
595 break;
596 }
597
598 p += 0x04 << (e->type >> 6);
599 }
600}
601
Joerg Roedelb65233a2008-07-11 17:14:21 +0200602/* Initializes the device->iommu mapping for the driver */
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200603static int __init init_iommu_devices(struct amd_iommu *iommu)
604{
605 u16 i;
606
607 for (i = iommu->first_device; i <= iommu->last_device; ++i)
608 set_iommu_for_device(iommu, i);
609
610 return 0;
611}
612
Joerg Roedele47d4022008-06-26 21:27:48 +0200613static void __init free_iommu_one(struct amd_iommu *iommu)
614{
615 free_command_buffer(iommu);
616 iommu_unmap_mmio_space(iommu);
617}
618
619static void __init free_iommu_all(void)
620{
621 struct amd_iommu *iommu, *next;
622
623 list_for_each_entry_safe(iommu, next, &amd_iommu_list, list) {
624 list_del(&iommu->list);
625 free_iommu_one(iommu);
626 kfree(iommu);
627 }
628}
629
Joerg Roedelb65233a2008-07-11 17:14:21 +0200630/*
631 * This function clues the initialization function for one IOMMU
632 * together and also allocates the command buffer and programs the
633 * hardware. It does NOT enable the IOMMU. This is done afterwards.
634 */
Joerg Roedele47d4022008-06-26 21:27:48 +0200635static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
636{
637 spin_lock_init(&iommu->lock);
638 list_add_tail(&iommu->list, &amd_iommu_list);
639
640 /*
641 * Copy data from ACPI table entry to the iommu struct
642 */
643 iommu->devid = h->devid;
644 iommu->cap_ptr = h->cap_ptr;
645 iommu->mmio_phys = h->mmio_phys;
646 iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
647 if (!iommu->mmio_base)
648 return -ENOMEM;
649
650 iommu_set_device_table(iommu);
651 iommu->cmd_buf = alloc_command_buffer(iommu);
652 if (!iommu->cmd_buf)
653 return -ENOMEM;
654
655 init_iommu_from_pci(iommu);
656 init_iommu_from_acpi(iommu, h);
657 init_iommu_devices(iommu);
658
659 return 0;
660}
661
Joerg Roedelb65233a2008-07-11 17:14:21 +0200662/*
663 * Iterates over all IOMMU entries in the ACPI table, allocates the
664 * IOMMU structure and initializes it with init_iommu_one()
665 */
Joerg Roedele47d4022008-06-26 21:27:48 +0200666static int __init init_iommu_all(struct acpi_table_header *table)
667{
668 u8 *p = (u8 *)table, *end = (u8 *)table;
669 struct ivhd_header *h;
670 struct amd_iommu *iommu;
671 int ret;
672
673 INIT_LIST_HEAD(&amd_iommu_list);
674
675 end += table->length;
676 p += IVRS_HEADER_LENGTH;
677
678 while (p < end) {
679 h = (struct ivhd_header *)p;
680 switch (*p) {
681 case ACPI_IVHD_TYPE:
682 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
683 if (iommu == NULL)
684 return -ENOMEM;
685 ret = init_iommu_one(iommu, h);
686 if (ret)
687 return ret;
688 break;
689 default:
690 break;
691 }
692 p += h->length;
693
694 }
695 WARN_ON(p != end);
696
697 return 0;
698}
699
Joerg Roedelb65233a2008-07-11 17:14:21 +0200700/****************************************************************************
701 *
702 * The next functions belong to the third pass of parsing the ACPI
703 * table. In this last pass the memory mapping requirements are
704 * gathered (like exclusion and unity mapping reanges).
705 *
706 ****************************************************************************/
707
Joerg Roedelbe2a0222008-06-26 21:27:49 +0200708static void __init free_unity_maps(void)
709{
710 struct unity_map_entry *entry, *next;
711
712 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
713 list_del(&entry->list);
714 kfree(entry);
715 }
716}
717
Joerg Roedelb65233a2008-07-11 17:14:21 +0200718/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +0200719static int __init init_exclusion_range(struct ivmd_header *m)
720{
721 int i;
722
723 switch (m->type) {
724 case ACPI_IVMD_TYPE:
725 set_device_exclusion_range(m->devid, m);
726 break;
727 case ACPI_IVMD_TYPE_ALL:
728 for (i = 0; i < amd_iommu_last_bdf; ++i)
729 set_device_exclusion_range(i, m);
730 break;
731 case ACPI_IVMD_TYPE_RANGE:
732 for (i = m->devid; i <= m->aux; ++i)
733 set_device_exclusion_range(i, m);
734 break;
735 default:
736 break;
737 }
738
739 return 0;
740}
741
Joerg Roedelb65233a2008-07-11 17:14:21 +0200742/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +0200743static int __init init_unity_map_range(struct ivmd_header *m)
744{
745 struct unity_map_entry *e = 0;
746
747 e = kzalloc(sizeof(*e), GFP_KERNEL);
748 if (e == NULL)
749 return -ENOMEM;
750
751 switch (m->type) {
752 default:
753 case ACPI_IVMD_TYPE:
754 e->devid_start = e->devid_end = m->devid;
755 break;
756 case ACPI_IVMD_TYPE_ALL:
757 e->devid_start = 0;
758 e->devid_end = amd_iommu_last_bdf;
759 break;
760 case ACPI_IVMD_TYPE_RANGE:
761 e->devid_start = m->devid;
762 e->devid_end = m->aux;
763 break;
764 }
765 e->address_start = PAGE_ALIGN(m->range_start);
766 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
767 e->prot = m->flags >> 1;
768
769 list_add_tail(&e->list, &amd_iommu_unity_map);
770
771 return 0;
772}
773
Joerg Roedelb65233a2008-07-11 17:14:21 +0200774/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +0200775static int __init init_memory_definitions(struct acpi_table_header *table)
776{
777 u8 *p = (u8 *)table, *end = (u8 *)table;
778 struct ivmd_header *m;
779
780 INIT_LIST_HEAD(&amd_iommu_unity_map);
781
782 end += table->length;
783 p += IVRS_HEADER_LENGTH;
784
785 while (p < end) {
786 m = (struct ivmd_header *)p;
787 if (m->flags & IVMD_FLAG_EXCL_RANGE)
788 init_exclusion_range(m);
789 else if (m->flags & IVMD_FLAG_UNITY_MAP)
790 init_unity_map_range(m);
791
792 p += m->length;
793 }
794
795 return 0;
796}
797
Joerg Roedelb65233a2008-07-11 17:14:21 +0200798/*
799 * This function finally enables all IOMMUs found in the system after
800 * they have been initialized
801 */
Joerg Roedel87361972008-06-26 21:28:07 +0200802static void __init enable_iommus(void)
803{
804 struct amd_iommu *iommu;
805
806 list_for_each_entry(iommu, &amd_iommu_list, list) {
807 iommu_set_exclusion_range(iommu);
808 iommu_enable(iommu);
809 }
810}
811
Joerg Roedel7441e9c2008-06-30 20:18:02 +0200812/*
813 * Suspend/Resume support
814 * disable suspend until real resume implemented
815 */
816
817static int amd_iommu_resume(struct sys_device *dev)
818{
819 return 0;
820}
821
822static int amd_iommu_suspend(struct sys_device *dev, pm_message_t state)
823{
824 return -EINVAL;
825}
826
827static struct sysdev_class amd_iommu_sysdev_class = {
828 .name = "amd_iommu",
829 .suspend = amd_iommu_suspend,
830 .resume = amd_iommu_resume,
831};
832
833static struct sys_device device_amd_iommu = {
834 .id = 0,
835 .cls = &amd_iommu_sysdev_class,
836};
837
Joerg Roedelb65233a2008-07-11 17:14:21 +0200838/*
839 * This is the core init function for AMD IOMMU hardware in the system.
840 * This function is called from the generic x86 DMA layer initialization
841 * code.
842 *
843 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
844 * three times:
845 *
846 * 1 pass) Find the highest PCI device id the driver has to handle.
847 * Upon this information the size of the data structures is
848 * determined that needs to be allocated.
849 *
850 * 2 pass) Initialize the data structures just allocated with the
851 * information in the ACPI table about available AMD IOMMUs
852 * in the system. It also maps the PCI devices in the
853 * system to specific IOMMUs
854 *
855 * 3 pass) After the basic data structures are allocated and
856 * initialized we update them with information about memory
857 * remapping requirements parsed out of the ACPI table in
858 * this last pass.
859 *
860 * After that the hardware is initialized and ready to go. In the last
861 * step we do some Linux specific things like registering the driver in
862 * the dma_ops interface and initializing the suspend/resume support
863 * functions. Finally it prints some information about AMD IOMMUs and
864 * the driver state and enables the hardware.
865 */
Joerg Roedelfe74c9c2008-06-26 21:27:50 +0200866int __init amd_iommu_init(void)
867{
868 int i, ret = 0;
869
870
Joerg Roedel8b145182008-07-03 19:35:09 +0200871 if (no_iommu) {
Joerg Roedelfe74c9c2008-06-26 21:27:50 +0200872 printk(KERN_INFO "AMD IOMMU disabled by kernel command line\n");
873 return 0;
874 }
875
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200876 if (!amd_iommu_detected)
877 return -ENODEV;
878
Joerg Roedelfe74c9c2008-06-26 21:27:50 +0200879 /*
880 * First parse ACPI tables to find the largest Bus/Dev/Func
881 * we need to handle. Upon this information the shared data
882 * structures for the IOMMUs in the system will be allocated
883 */
884 if (acpi_table_parse("IVRS", find_last_devid_acpi) != 0)
885 return -ENODEV;
886
887 dev_table_size = TBL_SIZE(DEV_TABLE_ENTRY_SIZE);
888 alias_table_size = TBL_SIZE(ALIAS_TABLE_ENTRY_SIZE);
889 rlookup_table_size = TBL_SIZE(RLOOKUP_TABLE_ENTRY_SIZE);
890
891 ret = -ENOMEM;
892
893 /* Device table - directly used by all IOMMUs */
894 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL,
895 get_order(dev_table_size));
896 if (amd_iommu_dev_table == NULL)
897 goto out;
898
899 /*
900 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
901 * IOMMU see for that device
902 */
903 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
904 get_order(alias_table_size));
905 if (amd_iommu_alias_table == NULL)
906 goto free;
907
908 /* IOMMU rlookup table - find the IOMMU for a specific device */
909 amd_iommu_rlookup_table = (void *)__get_free_pages(GFP_KERNEL,
910 get_order(rlookup_table_size));
911 if (amd_iommu_rlookup_table == NULL)
912 goto free;
913
914 /*
915 * Protection Domain table - maps devices to protection domains
916 * This table has the same size as the rlookup_table
917 */
918 amd_iommu_pd_table = (void *)__get_free_pages(GFP_KERNEL,
919 get_order(rlookup_table_size));
920 if (amd_iommu_pd_table == NULL)
921 goto free;
922
923 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(GFP_KERNEL,
924 get_order(MAX_DOMAIN_ID/8));
925 if (amd_iommu_pd_alloc_bitmap == NULL)
926 goto free;
927
928 /*
929 * memory is allocated now; initialize the device table with all zeroes
930 * and let all alias entries point to itself
931 */
932 memset(amd_iommu_dev_table, 0, dev_table_size);
933 for (i = 0; i < amd_iommu_last_bdf; ++i)
934 amd_iommu_alias_table[i] = i;
935
936 memset(amd_iommu_pd_table, 0, rlookup_table_size);
937 memset(amd_iommu_pd_alloc_bitmap, 0, MAX_DOMAIN_ID / 8);
938
939 /*
940 * never allocate domain 0 because its used as the non-allocated and
941 * error value placeholder
942 */
943 amd_iommu_pd_alloc_bitmap[0] = 1;
944
945 /*
946 * now the data structures are allocated and basically initialized
947 * start the real acpi table scan
948 */
949 ret = -ENODEV;
950 if (acpi_table_parse("IVRS", init_iommu_all) != 0)
951 goto free;
952
953 if (acpi_table_parse("IVRS", init_memory_definitions) != 0)
954 goto free;
955
Joerg Roedel87361972008-06-26 21:28:07 +0200956 ret = amd_iommu_init_dma_ops();
957 if (ret)
958 goto free;
959
Joerg Roedel7441e9c2008-06-30 20:18:02 +0200960 ret = sysdev_class_register(&amd_iommu_sysdev_class);
961 if (ret)
962 goto free;
963
964 ret = sysdev_register(&device_amd_iommu);
965 if (ret)
966 goto free;
967
Joerg Roedel87361972008-06-26 21:28:07 +0200968 enable_iommus();
969
Joerg Roedelfe74c9c2008-06-26 21:27:50 +0200970 printk(KERN_INFO "AMD IOMMU: aperture size is %d MB\n",
971 (1 << (amd_iommu_aperture_order-20)));
972
973 printk(KERN_INFO "AMD IOMMU: device isolation ");
974 if (amd_iommu_isolate)
975 printk("enabled\n");
976 else
977 printk("disabled\n");
978
979out:
980 return ret;
981
982free:
983 if (amd_iommu_pd_alloc_bitmap)
984 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap, 1);
985
986 if (amd_iommu_pd_table)
987 free_pages((unsigned long)amd_iommu_pd_table,
988 get_order(rlookup_table_size));
989
990 if (amd_iommu_rlookup_table)
991 free_pages((unsigned long)amd_iommu_rlookup_table,
992 get_order(rlookup_table_size));
993
994 if (amd_iommu_alias_table)
995 free_pages((unsigned long)amd_iommu_alias_table,
996 get_order(alias_table_size));
997
998 if (amd_iommu_dev_table)
999 free_pages((unsigned long)amd_iommu_dev_table,
1000 get_order(dev_table_size));
1001
1002 free_iommu_all();
1003
1004 free_unity_maps();
1005
1006 goto out;
1007}
1008
Joerg Roedelb65233a2008-07-11 17:14:21 +02001009/****************************************************************************
1010 *
1011 * Early detect code. This code runs at IOMMU detection time in the DMA
1012 * layer. It just looks if there is an IVRS ACPI table to detect AMD
1013 * IOMMUs
1014 *
1015 ****************************************************************************/
Joerg Roedelae7877d2008-06-26 21:27:51 +02001016static int __init early_amd_iommu_detect(struct acpi_table_header *table)
1017{
1018 return 0;
1019}
1020
1021void __init amd_iommu_detect(void)
1022{
Joerg Roedel299a1402008-07-08 14:47:16 +02001023 if (swiotlb || no_iommu || (iommu_detected && !gart_iommu_aperture))
Joerg Roedelae7877d2008-06-26 21:27:51 +02001024 return;
1025
Joerg Roedelae7877d2008-06-26 21:27:51 +02001026 if (acpi_table_parse("IVRS", early_amd_iommu_detect) == 0) {
1027 iommu_detected = 1;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +02001028 amd_iommu_detected = 1;
Ingo Molnar92af4e22008-06-27 10:48:16 +02001029#ifdef CONFIG_GART_IOMMU
Joerg Roedelae7877d2008-06-26 21:27:51 +02001030 gart_iommu_aperture_disabled = 1;
1031 gart_iommu_aperture = 0;
Ingo Molnar92af4e22008-06-27 10:48:16 +02001032#endif
Joerg Roedelae7877d2008-06-26 21:27:51 +02001033 }
1034}
1035
Joerg Roedelb65233a2008-07-11 17:14:21 +02001036/****************************************************************************
1037 *
1038 * Parsing functions for the AMD IOMMU specific kernel command line
1039 * options.
1040 *
1041 ****************************************************************************/
1042
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001043static int __init parse_amd_iommu_options(char *str)
1044{
1045 for (; *str; ++str) {
Joerg Roedel918ad6c2008-06-26 21:27:52 +02001046 if (strcmp(str, "isolate") == 0)
1047 amd_iommu_isolate = 1;
1048 }
1049
1050 return 1;
1051}
1052
1053static int __init parse_amd_iommu_size_options(char *str)
1054{
1055 for (; *str; ++str) {
1056 if (strcmp(str, "32M") == 0)
1057 amd_iommu_aperture_order = 25;
1058 if (strcmp(str, "64M") == 0)
1059 amd_iommu_aperture_order = 26;
1060 if (strcmp(str, "128M") == 0)
1061 amd_iommu_aperture_order = 27;
1062 if (strcmp(str, "256M") == 0)
1063 amd_iommu_aperture_order = 28;
1064 if (strcmp(str, "512M") == 0)
1065 amd_iommu_aperture_order = 29;
1066 if (strcmp(str, "1G") == 0)
1067 amd_iommu_aperture_order = 30;
1068 }
1069
1070 return 1;
1071}
1072
1073__setup("amd_iommu=", parse_amd_iommu_options);
1074__setup("amd_iommu_size=", parse_amd_iommu_size_options);