]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/iommu/amd_iommu_init.c
iommu/amd: Fix some typos
[mirror_ubuntu-hirsute-kernel.git] / drivers / iommu / amd_iommu_init.c
CommitLineData
f6e2e6b6 1/*
5d0d7156 2 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
f6e2e6b6
JR
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>
f6e2e6b6 22#include <linux/list.h>
5a0e3ad6 23#include <linux/slab.h>
f3c6ea1b 24#include <linux/syscore_ops.h>
a80dc3e0
JR
25#include <linux/interrupt.h>
26#include <linux/msi.h>
403f81d8 27#include <linux/amd-iommu.h>
400a28a0 28#include <linux/export.h>
02f3b3f5 29#include <acpi/acpi.h>
f6e2e6b6 30#include <asm/pci-direct.h>
46a7fa27 31#include <asm/iommu.h>
1d9b16d1 32#include <asm/gart.h>
ea1b0d39 33#include <asm/x86_init.h>
22e6daf4 34#include <asm/iommu_table.h>
403f81d8
JR
35
36#include "amd_iommu_proto.h"
37#include "amd_iommu_types.h"
38
f6e2e6b6
JR
39/*
40 * definitions for the ACPI scanning code
41 */
f6e2e6b6 42#define IVRS_HEADER_LENGTH 48
f6e2e6b6
JR
43
44#define ACPI_IVHD_TYPE 0x10
45#define ACPI_IVMD_TYPE_ALL 0x20
46#define ACPI_IVMD_TYPE 0x21
47#define ACPI_IVMD_TYPE_RANGE 0x22
48
49#define IVHD_DEV_ALL 0x01
50#define IVHD_DEV_SELECT 0x02
51#define IVHD_DEV_SELECT_RANGE_START 0x03
52#define IVHD_DEV_RANGE_END 0x04
53#define IVHD_DEV_ALIAS 0x42
54#define IVHD_DEV_ALIAS_RANGE 0x43
55#define IVHD_DEV_EXT_SELECT 0x46
56#define IVHD_DEV_EXT_SELECT_RANGE 0x47
57
6da7342f
JR
58#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
59#define IVHD_FLAG_PASSPW_EN_MASK 0x02
60#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
61#define IVHD_FLAG_ISOC_EN_MASK 0x08
f6e2e6b6
JR
62
63#define IVMD_FLAG_EXCL_RANGE 0x08
64#define IVMD_FLAG_UNITY_MAP 0x01
65
66#define ACPI_DEVFLAG_INITPASS 0x01
67#define ACPI_DEVFLAG_EXTINT 0x02
68#define ACPI_DEVFLAG_NMI 0x04
69#define ACPI_DEVFLAG_SYSMGT1 0x10
70#define ACPI_DEVFLAG_SYSMGT2 0x20
71#define ACPI_DEVFLAG_LINT0 0x40
72#define ACPI_DEVFLAG_LINT1 0x80
73#define ACPI_DEVFLAG_ATSDIS 0x10000000
74
b65233a9
JR
75/*
76 * ACPI table definitions
77 *
78 * These data structures are laid over the table to parse the important values
79 * out of it.
80 */
81
82/*
83 * structure describing one IOMMU in the ACPI table. Typically followed by one
84 * or more ivhd_entrys.
85 */
f6e2e6b6
JR
86struct ivhd_header {
87 u8 type;
88 u8 flags;
89 u16 length;
90 u16 devid;
91 u16 cap_ptr;
92 u64 mmio_phys;
93 u16 pci_seg;
94 u16 info;
95 u32 reserved;
96} __attribute__((packed));
97
b65233a9
JR
98/*
99 * A device entry describing which devices a specific IOMMU translates and
100 * which requestor ids they use.
101 */
f6e2e6b6
JR
102struct ivhd_entry {
103 u8 type;
104 u16 devid;
105 u8 flags;
106 u32 ext;
107} __attribute__((packed));
108
b65233a9
JR
109/*
110 * An AMD IOMMU memory definition structure. It defines things like exclusion
111 * ranges for devices and regions that should be unity mapped.
112 */
f6e2e6b6
JR
113struct ivmd_header {
114 u8 type;
115 u8 flags;
116 u16 length;
117 u16 devid;
118 u16 aux;
119 u64 resv;
120 u64 range_start;
121 u64 range_length;
122} __attribute__((packed));
123
fefda117
JR
124bool amd_iommu_dump;
125
02f3b3f5 126static bool amd_iommu_detected;
a5235725 127static bool __initdata amd_iommu_disabled;
c1cbebee 128
b65233a9
JR
129u16 amd_iommu_last_bdf; /* largest PCI device id we have
130 to handle */
2e22847f 131LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
b65233a9 132 we find in ACPI */
3775d481 133u32 amd_iommu_unmap_flush; /* if true, flush on every unmap */
928abd25 134
2e22847f 135LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
b65233a9 136 system */
928abd25 137
bb52777e
JR
138/* Array to assign indices to IOMMUs*/
139struct amd_iommu *amd_iommus[MAX_IOMMUS];
140int amd_iommus_present;
141
318afd41
JR
142/* IOMMUs have a non-present cache? */
143bool amd_iommu_np_cache __read_mostly;
60f723b4 144bool amd_iommu_iotlb_sup __read_mostly = true;
318afd41 145
62f71abb
JR
146u32 amd_iommu_max_pasids __read_mostly = ~0;
147
400a28a0
JR
148bool amd_iommu_v2_present __read_mostly;
149
5abcdba4
JR
150bool amd_iommu_force_isolation __read_mostly;
151
aeb26f55
JR
152/*
153 * List of protection domains - used during resume
154 */
155LIST_HEAD(amd_iommu_pd_list);
156spinlock_t amd_iommu_pd_lock;
157
b65233a9
JR
158/*
159 * Pointer to the device table which is shared by all AMD IOMMUs
160 * it is indexed by the PCI device id or the HT unit id and contains
161 * information about the domain the device belongs to as well as the
162 * page table root pointer.
163 */
928abd25 164struct dev_table_entry *amd_iommu_dev_table;
b65233a9
JR
165
166/*
167 * The alias table is a driver specific data structure which contains the
168 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
169 * More than one device can share the same requestor id.
170 */
928abd25 171u16 *amd_iommu_alias_table;
b65233a9
JR
172
173/*
174 * The rlookup table is used to find the IOMMU which is responsible
175 * for a specific device. It is also indexed by the PCI device id.
176 */
928abd25 177struct amd_iommu **amd_iommu_rlookup_table;
b65233a9 178
b65233a9 179/*
df805abb 180 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
b65233a9
JR
181 * to know which ones are already in use.
182 */
928abd25
JR
183unsigned long *amd_iommu_pd_alloc_bitmap;
184
b65233a9
JR
185static u32 dev_table_size; /* size of the device table */
186static u32 alias_table_size; /* size of the alias table */
187static u32 rlookup_table_size; /* size if the rlookup table */
3e8064ba 188
2c0ae172
JR
189enum iommu_init_state {
190 IOMMU_START_STATE,
191 IOMMU_IVRS_DETECTED,
192 IOMMU_ACPI_FINISHED,
193 IOMMU_ENABLED,
194 IOMMU_PCI_INIT,
195 IOMMU_INTERRUPTS_EN,
196 IOMMU_DMA_OPS,
197 IOMMU_INITIALIZED,
198 IOMMU_NOT_FOUND,
199 IOMMU_INIT_ERROR,
200};
201
202static enum iommu_init_state init_state = IOMMU_START_STATE;
203
ae295142 204static int amd_iommu_enable_interrupts(void);
2c0ae172 205static int __init iommu_go_to_state(enum iommu_init_state state);
3d9761e7 206
208ec8c9
JR
207static inline void update_last_devid(u16 devid)
208{
209 if (devid > amd_iommu_last_bdf)
210 amd_iommu_last_bdf = devid;
211}
212
c571484e
JR
213static inline unsigned long tbl_size(int entry_size)
214{
215 unsigned shift = PAGE_SHIFT +
421f909c 216 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
c571484e
JR
217
218 return 1UL << shift;
219}
220
5bcd757f
MG
221/* Access to l1 and l2 indexed register spaces */
222
223static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
224{
225 u32 val;
226
227 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
228 pci_read_config_dword(iommu->dev, 0xfc, &val);
229 return val;
230}
231
232static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
233{
234 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
235 pci_write_config_dword(iommu->dev, 0xfc, val);
236 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
237}
238
239static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
240{
241 u32 val;
242
243 pci_write_config_dword(iommu->dev, 0xf0, address);
244 pci_read_config_dword(iommu->dev, 0xf4, &val);
245 return val;
246}
247
248static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
249{
250 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
251 pci_write_config_dword(iommu->dev, 0xf4, val);
252}
253
b65233a9
JR
254/****************************************************************************
255 *
256 * AMD IOMMU MMIO register space handling functions
257 *
258 * These functions are used to program the IOMMU device registers in
259 * MMIO space required for that driver.
260 *
261 ****************************************************************************/
3e8064ba 262
b65233a9
JR
263/*
264 * This function set the exclusion range in the IOMMU. DMA accesses to the
265 * exclusion range are passed through untranslated
266 */
05f92db9 267static void iommu_set_exclusion_range(struct amd_iommu *iommu)
b2026aa2
JR
268{
269 u64 start = iommu->exclusion_start & PAGE_MASK;
270 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
271 u64 entry;
272
273 if (!iommu->exclusion_start)
274 return;
275
276 entry = start | MMIO_EXCL_ENABLE_MASK;
277 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
278 &entry, sizeof(entry));
279
280 entry = limit;
281 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
282 &entry, sizeof(entry));
283}
284
b65233a9 285/* Programs the physical address of the device table into the IOMMU hardware */
6b7f000e 286static void iommu_set_device_table(struct amd_iommu *iommu)
b2026aa2 287{
f609891f 288 u64 entry;
b2026aa2
JR
289
290 BUG_ON(iommu->mmio_base == NULL);
291
292 entry = virt_to_phys(amd_iommu_dev_table);
293 entry |= (dev_table_size >> 12) - 1;
294 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
295 &entry, sizeof(entry));
296}
297
b65233a9 298/* Generic functions to enable/disable certain features of the IOMMU. */
05f92db9 299static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
b2026aa2
JR
300{
301 u32 ctrl;
302
303 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
304 ctrl |= (1 << bit);
305 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
306}
307
ca020711 308static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
b2026aa2
JR
309{
310 u32 ctrl;
311
199d0d50 312 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
b2026aa2
JR
313 ctrl &= ~(1 << bit);
314 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
315}
316
1456e9d2
JR
317static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
318{
319 u32 ctrl;
320
321 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
322 ctrl &= ~CTRL_INV_TO_MASK;
323 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
324 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
325}
326
b65233a9 327/* Function to enable the hardware */
05f92db9 328static void iommu_enable(struct amd_iommu *iommu)
b2026aa2 329{
b2026aa2 330 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
b2026aa2
JR
331}
332
92ac4320 333static void iommu_disable(struct amd_iommu *iommu)
126c52be 334{
a8c485bb
CW
335 /* Disable command buffer */
336 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
337
338 /* Disable event logging and event interrupts */
339 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
340 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
341
342 /* Disable IOMMU hardware itself */
92ac4320 343 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
126c52be
JR
344}
345
b65233a9
JR
346/*
347 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
348 * the system has one.
349 */
98f1ad25 350static u8 __iomem * __init iommu_map_mmio_space(u64 address)
6c56747b 351{
e82752d8
JR
352 if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu")) {
353 pr_err("AMD-Vi: Can not reserve memory region %llx for mmio\n",
354 address);
355 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
6c56747b 356 return NULL;
e82752d8 357 }
6c56747b 358
98f1ad25 359 return (u8 __iomem *)ioremap_nocache(address, MMIO_REGION_LENGTH);
6c56747b
JR
360}
361
362static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
363{
364 if (iommu->mmio_base)
365 iounmap(iommu->mmio_base);
366 release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);
367}
368
b65233a9
JR
369/****************************************************************************
370 *
371 * The functions below belong to the first pass of AMD IOMMU ACPI table
372 * parsing. In this pass we try to find out the highest device id this
373 * code has to handle. Upon this information the size of the shared data
374 * structures is determined later.
375 *
376 ****************************************************************************/
377
b514e555
JR
378/*
379 * This function calculates the length of a given IVHD entry
380 */
381static inline int ivhd_entry_length(u8 *ivhd)
382{
383 return 0x04 << (*ivhd >> 6);
384}
385
b65233a9
JR
386/*
387 * This function reads the last device id the IOMMU has to handle from the PCI
388 * capability header for this IOMMU
389 */
3e8064ba
JR
390static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)
391{
392 u32 cap;
393
394 cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);
d591b0a3 395 update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));
3e8064ba
JR
396
397 return 0;
398}
399
b65233a9
JR
400/*
401 * After reading the highest device id from the IOMMU PCI capability header
402 * this function looks if there is a higher device id defined in the ACPI table
403 */
3e8064ba
JR
404static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
405{
406 u8 *p = (void *)h, *end = (void *)h;
407 struct ivhd_entry *dev;
408
409 p += sizeof(*h);
410 end += h->length;
411
412 find_last_devid_on_pci(PCI_BUS(h->devid),
413 PCI_SLOT(h->devid),
414 PCI_FUNC(h->devid),
415 h->cap_ptr);
416
417 while (p < end) {
418 dev = (struct ivhd_entry *)p;
419 switch (dev->type) {
420 case IVHD_DEV_SELECT:
421 case IVHD_DEV_RANGE_END:
422 case IVHD_DEV_ALIAS:
423 case IVHD_DEV_EXT_SELECT:
b65233a9 424 /* all the above subfield types refer to device ids */
208ec8c9 425 update_last_devid(dev->devid);
3e8064ba
JR
426 break;
427 default:
428 break;
429 }
b514e555 430 p += ivhd_entry_length(p);
3e8064ba
JR
431 }
432
433 WARN_ON(p != end);
434
435 return 0;
436}
437
b65233a9
JR
438/*
439 * Iterate over all IVHD entries in the ACPI table and find the highest device
440 * id which we need to handle. This is the first of three functions which parse
441 * the ACPI table. So we check the checksum here.
442 */
3e8064ba
JR
443static int __init find_last_devid_acpi(struct acpi_table_header *table)
444{
445 int i;
446 u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;
447 struct ivhd_header *h;
448
449 /*
450 * Validate checksum here so we don't need to do it when
451 * we actually parse the table
452 */
453 for (i = 0; i < table->length; ++i)
454 checksum += p[i];
02f3b3f5 455 if (checksum != 0)
3e8064ba 456 /* ACPI table corrupt */
02f3b3f5 457 return -ENODEV;
3e8064ba
JR
458
459 p += IVRS_HEADER_LENGTH;
460
461 end += table->length;
462 while (p < end) {
463 h = (struct ivhd_header *)p;
464 switch (h->type) {
465 case ACPI_IVHD_TYPE:
466 find_last_devid_from_ivhd(h);
467 break;
468 default:
469 break;
470 }
471 p += h->length;
472 }
473 WARN_ON(p != end);
474
475 return 0;
476}
477
b65233a9
JR
478/****************************************************************************
479 *
df805abb 480 * The following functions belong to the code path which parses the ACPI table
b65233a9
JR
481 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
482 * data structures, initialize the device/alias/rlookup table and also
483 * basically initialize the hardware.
484 *
485 ****************************************************************************/
486
487/*
488 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
489 * write commands to that buffer later and the IOMMU will execute them
490 * asynchronously
491 */
b36ca91e
JR
492static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)
493{
d0312b21 494 u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
b36ca91e 495 get_order(CMD_BUFFER_SIZE));
b36ca91e
JR
496
497 if (cmd_buf == NULL)
498 return NULL;
499
549c90dc 500 iommu->cmd_buf_size = CMD_BUFFER_SIZE | CMD_BUFFER_UNINITIALIZED;
b36ca91e 501
58492e12
JR
502 return cmd_buf;
503}
504
93f1cc67
JR
505/*
506 * This function resets the command buffer if the IOMMU stopped fetching
507 * commands from it.
508 */
509void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
510{
511 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
512
513 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
514 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
515
516 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
517}
518
58492e12
JR
519/*
520 * This function writes the command buffer address to the hardware and
521 * enables it.
522 */
523static void iommu_enable_command_buffer(struct amd_iommu *iommu)
524{
525 u64 entry;
526
527 BUG_ON(iommu->cmd_buf == NULL);
528
529 entry = (u64)virt_to_phys(iommu->cmd_buf);
b36ca91e 530 entry |= MMIO_CMD_SIZE_512;
58492e12 531
b36ca91e 532 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
58492e12 533 &entry, sizeof(entry));
b36ca91e 534
93f1cc67 535 amd_iommu_reset_cmd_buffer(iommu);
549c90dc 536 iommu->cmd_buf_size &= ~(CMD_BUFFER_UNINITIALIZED);
b36ca91e
JR
537}
538
539static void __init free_command_buffer(struct amd_iommu *iommu)
540{
23c1713f 541 free_pages((unsigned long)iommu->cmd_buf,
549c90dc 542 get_order(iommu->cmd_buf_size & ~(CMD_BUFFER_UNINITIALIZED)));
b36ca91e
JR
543}
544
335503e5
JR
545/* allocates the memory where the IOMMU will log its events to */
546static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)
547{
335503e5
JR
548 iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
549 get_order(EVT_BUFFER_SIZE));
550
551 if (iommu->evt_buf == NULL)
552 return NULL;
553
1bc6f838
JR
554 iommu->evt_buf_size = EVT_BUFFER_SIZE;
555
58492e12
JR
556 return iommu->evt_buf;
557}
558
559static void iommu_enable_event_buffer(struct amd_iommu *iommu)
560{
561 u64 entry;
562
563 BUG_ON(iommu->evt_buf == NULL);
564
335503e5 565 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
58492e12 566
335503e5
JR
567 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
568 &entry, sizeof(entry));
569
09067207
JR
570 /* set head and tail to zero manually */
571 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
572 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
573
58492e12 574 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
335503e5
JR
575}
576
577static void __init free_event_buffer(struct amd_iommu *iommu)
578{
579 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
580}
581
1a29ac01
JR
582/* allocates the memory where the IOMMU will log its events to */
583static u8 * __init alloc_ppr_log(struct amd_iommu *iommu)
584{
585 iommu->ppr_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
586 get_order(PPR_LOG_SIZE));
587
588 if (iommu->ppr_log == NULL)
589 return NULL;
590
591 return iommu->ppr_log;
592}
593
594static void iommu_enable_ppr_log(struct amd_iommu *iommu)
595{
596 u64 entry;
597
598 if (iommu->ppr_log == NULL)
599 return;
600
601 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
602
603 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
604 &entry, sizeof(entry));
605
606 /* set head and tail to zero manually */
607 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
608 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
609
610 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
611 iommu_feature_enable(iommu, CONTROL_PPR_EN);
612}
613
614static void __init free_ppr_log(struct amd_iommu *iommu)
615{
616 if (iommu->ppr_log == NULL)
617 return;
618
619 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
620}
621
cbc33a90
JR
622static void iommu_enable_gt(struct amd_iommu *iommu)
623{
624 if (!iommu_feature(iommu, FEATURE_GT))
625 return;
626
627 iommu_feature_enable(iommu, CONTROL_GT_EN);
628}
629
b65233a9 630/* sets a specific bit in the device table entry. */
3566b778
JR
631static void set_dev_entry_bit(u16 devid, u8 bit)
632{
ee6c2868
JR
633 int i = (bit >> 6) & 0x03;
634 int _bit = bit & 0x3f;
3566b778 635
ee6c2868 636 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
3566b778
JR
637}
638
c5cca146
JR
639static int get_dev_entry_bit(u16 devid, u8 bit)
640{
ee6c2868
JR
641 int i = (bit >> 6) & 0x03;
642 int _bit = bit & 0x3f;
c5cca146 643
ee6c2868 644 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
c5cca146
JR
645}
646
647
648void amd_iommu_apply_erratum_63(u16 devid)
649{
650 int sysmgt;
651
652 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
653 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
654
655 if (sysmgt == 0x01)
656 set_dev_entry_bit(devid, DEV_ENTRY_IW);
657}
658
5ff4789d
JR
659/* Writes the specific IOMMU for a device into the rlookup table */
660static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
661{
662 amd_iommu_rlookup_table[devid] = iommu;
663}
664
b65233a9
JR
665/*
666 * This function takes the device specific flags read from the ACPI
667 * table and sets up the device table entry with that information
668 */
5ff4789d
JR
669static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
670 u16 devid, u32 flags, u32 ext_flags)
3566b778
JR
671{
672 if (flags & ACPI_DEVFLAG_INITPASS)
673 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
674 if (flags & ACPI_DEVFLAG_EXTINT)
675 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
676 if (flags & ACPI_DEVFLAG_NMI)
677 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
678 if (flags & ACPI_DEVFLAG_SYSMGT1)
679 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
680 if (flags & ACPI_DEVFLAG_SYSMGT2)
681 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
682 if (flags & ACPI_DEVFLAG_LINT0)
683 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
684 if (flags & ACPI_DEVFLAG_LINT1)
685 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
3566b778 686
c5cca146
JR
687 amd_iommu_apply_erratum_63(devid);
688
5ff4789d 689 set_iommu_for_device(iommu, devid);
3566b778
JR
690}
691
b65233a9 692/*
df805abb 693 * Reads the device exclusion range from ACPI and initializes the IOMMU with
b65233a9
JR
694 * it
695 */
3566b778
JR
696static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
697{
698 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
699
700 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
701 return;
702
703 if (iommu) {
b65233a9
JR
704 /*
705 * We only can configure exclusion ranges per IOMMU, not
706 * per device. But we can enable the exclusion range per
707 * device. This is done here
708 */
3566b778
JR
709 set_dev_entry_bit(m->devid, DEV_ENTRY_EX);
710 iommu->exclusion_start = m->range_start;
711 iommu->exclusion_length = m->range_length;
712 }
713}
714
b65233a9
JR
715/*
716 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
717 * initializes the hardware and our data structures with it.
718 */
5d0c8e49
JR
719static void __init init_iommu_from_acpi(struct amd_iommu *iommu,
720 struct ivhd_header *h)
721{
722 u8 *p = (u8 *)h;
723 u8 *end = p, flags = 0;
0de66d5b
JR
724 u16 devid = 0, devid_start = 0, devid_to = 0;
725 u32 dev_i, ext_flags = 0;
58a3bee5 726 bool alias = false;
5d0c8e49
JR
727 struct ivhd_entry *e;
728
729 /*
e9bf5197 730 * First save the recommended feature enable bits from ACPI
5d0c8e49 731 */
e9bf5197 732 iommu->acpi_flags = h->flags;
5d0c8e49
JR
733
734 /*
735 * Done. Now parse the device entries
736 */
737 p += sizeof(struct ivhd_header);
738 end += h->length;
739
42a698f4 740
5d0c8e49
JR
741 while (p < end) {
742 e = (struct ivhd_entry *)p;
743 switch (e->type) {
744 case IVHD_DEV_ALL:
42a698f4
JR
745
746 DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
747 " last device %02x:%02x.%x flags: %02x\n",
748 PCI_BUS(iommu->first_device),
749 PCI_SLOT(iommu->first_device),
750 PCI_FUNC(iommu->first_device),
751 PCI_BUS(iommu->last_device),
752 PCI_SLOT(iommu->last_device),
753 PCI_FUNC(iommu->last_device),
754 e->flags);
755
5d0c8e49
JR
756 for (dev_i = iommu->first_device;
757 dev_i <= iommu->last_device; ++dev_i)
5ff4789d
JR
758 set_dev_entry_from_acpi(iommu, dev_i,
759 e->flags, 0);
5d0c8e49
JR
760 break;
761 case IVHD_DEV_SELECT:
42a698f4
JR
762
763 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
764 "flags: %02x\n",
765 PCI_BUS(e->devid),
766 PCI_SLOT(e->devid),
767 PCI_FUNC(e->devid),
768 e->flags);
769
5d0c8e49 770 devid = e->devid;
5ff4789d 771 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
5d0c8e49
JR
772 break;
773 case IVHD_DEV_SELECT_RANGE_START:
42a698f4
JR
774
775 DUMP_printk(" DEV_SELECT_RANGE_START\t "
776 "devid: %02x:%02x.%x flags: %02x\n",
777 PCI_BUS(e->devid),
778 PCI_SLOT(e->devid),
779 PCI_FUNC(e->devid),
780 e->flags);
781
5d0c8e49
JR
782 devid_start = e->devid;
783 flags = e->flags;
784 ext_flags = 0;
58a3bee5 785 alias = false;
5d0c8e49
JR
786 break;
787 case IVHD_DEV_ALIAS:
42a698f4
JR
788
789 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
790 "flags: %02x devid_to: %02x:%02x.%x\n",
791 PCI_BUS(e->devid),
792 PCI_SLOT(e->devid),
793 PCI_FUNC(e->devid),
794 e->flags,
795 PCI_BUS(e->ext >> 8),
796 PCI_SLOT(e->ext >> 8),
797 PCI_FUNC(e->ext >> 8));
798
5d0c8e49
JR
799 devid = e->devid;
800 devid_to = e->ext >> 8;
7a6a3a08 801 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
7455aab1 802 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
5d0c8e49
JR
803 amd_iommu_alias_table[devid] = devid_to;
804 break;
805 case IVHD_DEV_ALIAS_RANGE:
42a698f4
JR
806
807 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
808 "devid: %02x:%02x.%x flags: %02x "
809 "devid_to: %02x:%02x.%x\n",
810 PCI_BUS(e->devid),
811 PCI_SLOT(e->devid),
812 PCI_FUNC(e->devid),
813 e->flags,
814 PCI_BUS(e->ext >> 8),
815 PCI_SLOT(e->ext >> 8),
816 PCI_FUNC(e->ext >> 8));
817
5d0c8e49
JR
818 devid_start = e->devid;
819 flags = e->flags;
820 devid_to = e->ext >> 8;
821 ext_flags = 0;
58a3bee5 822 alias = true;
5d0c8e49
JR
823 break;
824 case IVHD_DEV_EXT_SELECT:
42a698f4
JR
825
826 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
827 "flags: %02x ext: %08x\n",
828 PCI_BUS(e->devid),
829 PCI_SLOT(e->devid),
830 PCI_FUNC(e->devid),
831 e->flags, e->ext);
832
5d0c8e49 833 devid = e->devid;
5ff4789d
JR
834 set_dev_entry_from_acpi(iommu, devid, e->flags,
835 e->ext);
5d0c8e49
JR
836 break;
837 case IVHD_DEV_EXT_SELECT_RANGE:
42a698f4
JR
838
839 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
840 "%02x:%02x.%x flags: %02x ext: %08x\n",
841 PCI_BUS(e->devid),
842 PCI_SLOT(e->devid),
843 PCI_FUNC(e->devid),
844 e->flags, e->ext);
845
5d0c8e49
JR
846 devid_start = e->devid;
847 flags = e->flags;
848 ext_flags = e->ext;
58a3bee5 849 alias = false;
5d0c8e49
JR
850 break;
851 case IVHD_DEV_RANGE_END:
42a698f4
JR
852
853 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
854 PCI_BUS(e->devid),
855 PCI_SLOT(e->devid),
856 PCI_FUNC(e->devid));
857
5d0c8e49
JR
858 devid = e->devid;
859 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
7a6a3a08 860 if (alias) {
5d0c8e49 861 amd_iommu_alias_table[dev_i] = devid_to;
7a6a3a08
JR
862 set_dev_entry_from_acpi(iommu,
863 devid_to, flags, ext_flags);
864 }
865 set_dev_entry_from_acpi(iommu, dev_i,
866 flags, ext_flags);
5d0c8e49
JR
867 }
868 break;
869 default:
870 break;
871 }
872
b514e555 873 p += ivhd_entry_length(p);
5d0c8e49
JR
874 }
875}
876
b65233a9 877/* Initializes the device->iommu mapping for the driver */
5d0c8e49
JR
878static int __init init_iommu_devices(struct amd_iommu *iommu)
879{
0de66d5b 880 u32 i;
5d0c8e49
JR
881
882 for (i = iommu->first_device; i <= iommu->last_device; ++i)
883 set_iommu_for_device(iommu, i);
884
885 return 0;
886}
887
e47d402d
JR
888static void __init free_iommu_one(struct amd_iommu *iommu)
889{
890 free_command_buffer(iommu);
335503e5 891 free_event_buffer(iommu);
1a29ac01 892 free_ppr_log(iommu);
e47d402d
JR
893 iommu_unmap_mmio_space(iommu);
894}
895
896static void __init free_iommu_all(void)
897{
898 struct amd_iommu *iommu, *next;
899
3bd22172 900 for_each_iommu_safe(iommu, next) {
e47d402d
JR
901 list_del(&iommu->list);
902 free_iommu_one(iommu);
903 kfree(iommu);
904 }
905}
906
b65233a9
JR
907/*
908 * This function clues the initialization function for one IOMMU
909 * together and also allocates the command buffer and programs the
910 * hardware. It does NOT enable the IOMMU. This is done afterwards.
911 */
e47d402d
JR
912static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
913{
914 spin_lock_init(&iommu->lock);
bb52777e
JR
915
916 /* Add IOMMU to internal data structures */
e47d402d 917 list_add_tail(&iommu->list, &amd_iommu_list);
bb52777e
JR
918 iommu->index = amd_iommus_present++;
919
920 if (unlikely(iommu->index >= MAX_IOMMUS)) {
921 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
922 return -ENOSYS;
923 }
924
925 /* Index is fine - add IOMMU to the array */
926 amd_iommus[iommu->index] = iommu;
e47d402d
JR
927
928 /*
929 * Copy data from ACPI table entry to the iommu struct
930 */
23c742db 931 iommu->devid = h->devid;
e47d402d 932 iommu->cap_ptr = h->cap_ptr;
ee893c24 933 iommu->pci_seg = h->pci_seg;
e47d402d
JR
934 iommu->mmio_phys = h->mmio_phys;
935 iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);
936 if (!iommu->mmio_base)
937 return -ENOMEM;
938
e47d402d
JR
939 iommu->cmd_buf = alloc_command_buffer(iommu);
940 if (!iommu->cmd_buf)
941 return -ENOMEM;
942
335503e5
JR
943 iommu->evt_buf = alloc_event_buffer(iommu);
944 if (!iommu->evt_buf)
945 return -ENOMEM;
946
a80dc3e0
JR
947 iommu->int_enabled = false;
948
e47d402d
JR
949 init_iommu_from_acpi(iommu, h);
950 init_iommu_devices(iommu);
951
23c742db 952 return 0;
e47d402d
JR
953}
954
b65233a9
JR
955/*
956 * Iterates over all IOMMU entries in the ACPI table, allocates the
957 * IOMMU structure and initializes it with init_iommu_one()
958 */
e47d402d
JR
959static int __init init_iommu_all(struct acpi_table_header *table)
960{
961 u8 *p = (u8 *)table, *end = (u8 *)table;
962 struct ivhd_header *h;
963 struct amd_iommu *iommu;
964 int ret;
965
e47d402d
JR
966 end += table->length;
967 p += IVRS_HEADER_LENGTH;
968
969 while (p < end) {
970 h = (struct ivhd_header *)p;
971 switch (*p) {
972 case ACPI_IVHD_TYPE:
9c72041f 973
ae908c22 974 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
9c72041f
JR
975 "seg: %d flags: %01x info %04x\n",
976 PCI_BUS(h->devid), PCI_SLOT(h->devid),
977 PCI_FUNC(h->devid), h->cap_ptr,
978 h->pci_seg, h->flags, h->info);
979 DUMP_printk(" mmio-addr: %016llx\n",
980 h->mmio_phys);
981
e47d402d 982 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
02f3b3f5
JR
983 if (iommu == NULL)
984 return -ENOMEM;
3551a708 985
e47d402d 986 ret = init_iommu_one(iommu, h);
02f3b3f5
JR
987 if (ret)
988 return ret;
e47d402d
JR
989 break;
990 default:
991 break;
992 }
993 p += h->length;
994
995 }
996 WARN_ON(p != end);
997
998 return 0;
999}
1000
23c742db
JR
1001static int iommu_init_pci(struct amd_iommu *iommu)
1002{
1003 int cap_ptr = iommu->cap_ptr;
1004 u32 range, misc, low, high;
1005
1006 iommu->dev = pci_get_bus_and_slot(PCI_BUS(iommu->devid),
1007 iommu->devid & 0xff);
1008 if (!iommu->dev)
1009 return -ENODEV;
1010
1011 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1012 &iommu->cap);
1013 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1014 &range);
1015 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1016 &misc);
1017
1018 iommu->first_device = calc_devid(MMIO_GET_BUS(range),
1019 MMIO_GET_FD(range));
1020 iommu->last_device = calc_devid(MMIO_GET_BUS(range),
1021 MMIO_GET_LD(range));
1022
1023 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1024 amd_iommu_iotlb_sup = false;
1025
1026 /* read extended feature bits */
1027 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1028 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1029
1030 iommu->features = ((u64)high << 32) | low;
1031
1032 if (iommu_feature(iommu, FEATURE_GT)) {
1033 int glxval;
1034 u32 pasids;
1035 u64 shift;
1036
1037 shift = iommu->features & FEATURE_PASID_MASK;
1038 shift >>= FEATURE_PASID_SHIFT;
1039 pasids = (1 << shift);
1040
1041 amd_iommu_max_pasids = min(amd_iommu_max_pasids, pasids);
1042
1043 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1044 glxval >>= FEATURE_GLXVAL_SHIFT;
1045
1046 if (amd_iommu_max_glx_val == -1)
1047 amd_iommu_max_glx_val = glxval;
1048 else
1049 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1050 }
1051
1052 if (iommu_feature(iommu, FEATURE_GT) &&
1053 iommu_feature(iommu, FEATURE_PPR)) {
1054 iommu->is_iommu_v2 = true;
1055 amd_iommu_v2_present = true;
1056 }
1057
1058 if (iommu_feature(iommu, FEATURE_PPR)) {
1059 iommu->ppr_log = alloc_ppr_log(iommu);
1060 if (!iommu->ppr_log)
1061 return -ENOMEM;
1062 }
1063
1064 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1065 amd_iommu_np_cache = true;
1066
1067 if (is_rd890_iommu(iommu->dev)) {
1068 int i, j;
1069
1070 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1071 PCI_DEVFN(0, 0));
1072
1073 /*
1074 * Some rd890 systems may not be fully reconfigured by the
1075 * BIOS, so it's necessary for us to store this information so
1076 * it can be reprogrammed on resume
1077 */
1078 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1079 &iommu->stored_addr_lo);
1080 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1081 &iommu->stored_addr_hi);
1082
1083 /* Low bit locks writes to configuration space */
1084 iommu->stored_addr_lo &= ~1;
1085
1086 for (i = 0; i < 6; i++)
1087 for (j = 0; j < 0x12; j++)
1088 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1089
1090 for (i = 0; i < 0x83; i++)
1091 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1092 }
1093
1094 return pci_enable_device(iommu->dev);
1095}
1096
4d121c32
JR
1097static void print_iommu_info(void)
1098{
1099 static const char * const feat_str[] = {
1100 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1101 "IA", "GA", "HE", "PC"
1102 };
1103 struct amd_iommu *iommu;
1104
1105 for_each_iommu(iommu) {
1106 int i;
1107
1108 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1109 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1110
1111 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
1112 pr_info("AMD-Vi: Extended features: ");
2bd5ed00 1113 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
4d121c32
JR
1114 if (iommu_feature(iommu, (1ULL << i)))
1115 pr_cont(" %s", feat_str[i]);
1116 }
1117 }
1118 pr_cont("\n");
1119 }
1120}
1121
2c0ae172 1122static int __init amd_iommu_init_pci(void)
23c742db
JR
1123{
1124 struct amd_iommu *iommu;
1125 int ret = 0;
1126
1127 for_each_iommu(iommu) {
1128 ret = iommu_init_pci(iommu);
1129 if (ret)
1130 break;
1131 }
1132
23c742db
JR
1133 ret = amd_iommu_init_devices();
1134
4d121c32
JR
1135 print_iommu_info();
1136
23c742db
JR
1137 return ret;
1138}
1139
a80dc3e0
JR
1140/****************************************************************************
1141 *
1142 * The following functions initialize the MSI interrupts for all IOMMUs
df805abb 1143 * in the system. It's a bit challenging because there could be multiple
a80dc3e0
JR
1144 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1145 * pci_dev.
1146 *
1147 ****************************************************************************/
1148
9f800de3 1149static int iommu_setup_msi(struct amd_iommu *iommu)
a80dc3e0
JR
1150{
1151 int r;
a80dc3e0 1152
9ddd592a
JR
1153 r = pci_enable_msi(iommu->dev);
1154 if (r)
1155 return r;
a80dc3e0 1156
72fe00f0
JR
1157 r = request_threaded_irq(iommu->dev->irq,
1158 amd_iommu_int_handler,
1159 amd_iommu_int_thread,
1160 0, "AMD-Vi",
1161 iommu->dev);
a80dc3e0
JR
1162
1163 if (r) {
1164 pci_disable_msi(iommu->dev);
9ddd592a 1165 return r;
a80dc3e0
JR
1166 }
1167
fab6afa3 1168 iommu->int_enabled = true;
1a29ac01 1169
a80dc3e0
JR
1170 return 0;
1171}
1172
05f92db9 1173static int iommu_init_msi(struct amd_iommu *iommu)
a80dc3e0 1174{
9ddd592a
JR
1175 int ret;
1176
a80dc3e0 1177 if (iommu->int_enabled)
9ddd592a 1178 goto enable_faults;
a80dc3e0 1179
d91cecdd 1180 if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))
9ddd592a
JR
1181 ret = iommu_setup_msi(iommu);
1182 else
1183 ret = -ENODEV;
1184
1185 if (ret)
1186 return ret;
a80dc3e0 1187
9ddd592a
JR
1188enable_faults:
1189 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
a80dc3e0 1190
9ddd592a
JR
1191 if (iommu->ppr_log != NULL)
1192 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1193
1194 return 0;
a80dc3e0
JR
1195}
1196
b65233a9
JR
1197/****************************************************************************
1198 *
1199 * The next functions belong to the third pass of parsing the ACPI
1200 * table. In this last pass the memory mapping requirements are
df805abb 1201 * gathered (like exclusion and unity mapping ranges).
b65233a9
JR
1202 *
1203 ****************************************************************************/
1204
be2a022c
JR
1205static void __init free_unity_maps(void)
1206{
1207 struct unity_map_entry *entry, *next;
1208
1209 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1210 list_del(&entry->list);
1211 kfree(entry);
1212 }
1213}
1214
b65233a9 1215/* called when we find an exclusion range definition in ACPI */
be2a022c
JR
1216static int __init init_exclusion_range(struct ivmd_header *m)
1217{
1218 int i;
1219
1220 switch (m->type) {
1221 case ACPI_IVMD_TYPE:
1222 set_device_exclusion_range(m->devid, m);
1223 break;
1224 case ACPI_IVMD_TYPE_ALL:
3a61ec38 1225 for (i = 0; i <= amd_iommu_last_bdf; ++i)
be2a022c
JR
1226 set_device_exclusion_range(i, m);
1227 break;
1228 case ACPI_IVMD_TYPE_RANGE:
1229 for (i = m->devid; i <= m->aux; ++i)
1230 set_device_exclusion_range(i, m);
1231 break;
1232 default:
1233 break;
1234 }
1235
1236 return 0;
1237}
1238
b65233a9 1239/* called for unity map ACPI definition */
be2a022c
JR
1240static int __init init_unity_map_range(struct ivmd_header *m)
1241{
98f1ad25 1242 struct unity_map_entry *e = NULL;
02acc43a 1243 char *s;
be2a022c
JR
1244
1245 e = kzalloc(sizeof(*e), GFP_KERNEL);
1246 if (e == NULL)
1247 return -ENOMEM;
1248
1249 switch (m->type) {
1250 default:
0bc252f4
JR
1251 kfree(e);
1252 return 0;
be2a022c 1253 case ACPI_IVMD_TYPE:
02acc43a 1254 s = "IVMD_TYPEi\t\t\t";
be2a022c
JR
1255 e->devid_start = e->devid_end = m->devid;
1256 break;
1257 case ACPI_IVMD_TYPE_ALL:
02acc43a 1258 s = "IVMD_TYPE_ALL\t\t";
be2a022c
JR
1259 e->devid_start = 0;
1260 e->devid_end = amd_iommu_last_bdf;
1261 break;
1262 case ACPI_IVMD_TYPE_RANGE:
02acc43a 1263 s = "IVMD_TYPE_RANGE\t\t";
be2a022c
JR
1264 e->devid_start = m->devid;
1265 e->devid_end = m->aux;
1266 break;
1267 }
1268 e->address_start = PAGE_ALIGN(m->range_start);
1269 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1270 e->prot = m->flags >> 1;
1271
02acc43a
JR
1272 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1273 " range_start: %016llx range_end: %016llx flags: %x\n", s,
1274 PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),
1275 PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),
1276 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1277 e->address_start, e->address_end, m->flags);
1278
be2a022c
JR
1279 list_add_tail(&e->list, &amd_iommu_unity_map);
1280
1281 return 0;
1282}
1283
b65233a9 1284/* iterates over all memory definitions we find in the ACPI table */
be2a022c
JR
1285static int __init init_memory_definitions(struct acpi_table_header *table)
1286{
1287 u8 *p = (u8 *)table, *end = (u8 *)table;
1288 struct ivmd_header *m;
1289
be2a022c
JR
1290 end += table->length;
1291 p += IVRS_HEADER_LENGTH;
1292
1293 while (p < end) {
1294 m = (struct ivmd_header *)p;
1295 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1296 init_exclusion_range(m);
1297 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1298 init_unity_map_range(m);
1299
1300 p += m->length;
1301 }
1302
1303 return 0;
1304}
1305
9f5f5fb3
JR
1306/*
1307 * Init the device table to not allow DMA access for devices and
1308 * suppress all page faults
1309 */
1310static void init_device_table(void)
1311{
0de66d5b 1312 u32 devid;
9f5f5fb3
JR
1313
1314 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1315 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1316 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
9f5f5fb3
JR
1317 }
1318}
1319
e9bf5197
JR
1320static void iommu_init_flags(struct amd_iommu *iommu)
1321{
1322 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1323 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1324 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1325
1326 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1327 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1328 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1329
1330 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1331 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1332 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1333
1334 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1335 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1336 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1337
1338 /*
1339 * make IOMMU memory accesses cache coherent
1340 */
1341 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
1456e9d2
JR
1342
1343 /* Set IOTLB invalidation timeout to 1s */
1344 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
e9bf5197
JR
1345}
1346
5bcd757f 1347static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
4c894f47 1348{
5bcd757f
MG
1349 int i, j;
1350 u32 ioc_feature_control;
c1bf94ec 1351 struct pci_dev *pdev = iommu->root_pdev;
5bcd757f
MG
1352
1353 /* RD890 BIOSes may not have completely reconfigured the iommu */
c1bf94ec 1354 if (!is_rd890_iommu(iommu->dev) || !pdev)
5bcd757f
MG
1355 return;
1356
1357 /*
1358 * First, we need to ensure that the iommu is enabled. This is
1359 * controlled by a register in the northbridge
1360 */
5bcd757f
MG
1361
1362 /* Select Northbridge indirect register 0x75 and enable writing */
1363 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1364 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1365
1366 /* Enable the iommu */
1367 if (!(ioc_feature_control & 0x1))
1368 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1369
5bcd757f
MG
1370 /* Restore the iommu BAR */
1371 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1372 iommu->stored_addr_lo);
1373 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1374 iommu->stored_addr_hi);
1375
1376 /* Restore the l1 indirect regs for each of the 6 l1s */
1377 for (i = 0; i < 6; i++)
1378 for (j = 0; j < 0x12; j++)
1379 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1380
1381 /* Restore the l2 indirect regs */
1382 for (i = 0; i < 0x83; i++)
1383 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
1384
1385 /* Lock PCI setup registers */
1386 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1387 iommu->stored_addr_lo | 1);
4c894f47
JR
1388}
1389
b65233a9
JR
1390/*
1391 * This function finally enables all IOMMUs found in the system after
1392 * they have been initialized
1393 */
11ee5ac4 1394static void early_enable_iommus(void)
8736197b
JR
1395{
1396 struct amd_iommu *iommu;
1397
3bd22172 1398 for_each_iommu(iommu) {
a8c485bb 1399 iommu_disable(iommu);
e9bf5197 1400 iommu_init_flags(iommu);
58492e12
JR
1401 iommu_set_device_table(iommu);
1402 iommu_enable_command_buffer(iommu);
1403 iommu_enable_event_buffer(iommu);
8736197b
JR
1404 iommu_set_exclusion_range(iommu);
1405 iommu_enable(iommu);
7d0c5cc5 1406 iommu_flush_all_caches(iommu);
8736197b
JR
1407 }
1408}
1409
11ee5ac4
JR
1410static void enable_iommus_v2(void)
1411{
1412 struct amd_iommu *iommu;
1413
1414 for_each_iommu(iommu) {
1415 iommu_enable_ppr_log(iommu);
1416 iommu_enable_gt(iommu);
1417 }
1418}
1419
1420static void enable_iommus(void)
1421{
1422 early_enable_iommus();
1423
1424 enable_iommus_v2();
1425}
1426
92ac4320
JR
1427static void disable_iommus(void)
1428{
1429 struct amd_iommu *iommu;
1430
1431 for_each_iommu(iommu)
1432 iommu_disable(iommu);
1433}
1434
7441e9cb
JR
1435/*
1436 * Suspend/Resume support
1437 * disable suspend until real resume implemented
1438 */
1439
f3c6ea1b 1440static void amd_iommu_resume(void)
7441e9cb 1441{
5bcd757f
MG
1442 struct amd_iommu *iommu;
1443
1444 for_each_iommu(iommu)
1445 iommu_apply_resume_quirks(iommu);
1446
736501ee
JR
1447 /* re-load the hardware */
1448 enable_iommus();
3d9761e7
JR
1449
1450 amd_iommu_enable_interrupts();
7441e9cb
JR
1451}
1452
f3c6ea1b 1453static int amd_iommu_suspend(void)
7441e9cb 1454{
736501ee
JR
1455 /* disable IOMMUs to go out of the way for BIOS */
1456 disable_iommus();
1457
1458 return 0;
7441e9cb
JR
1459}
1460
f3c6ea1b 1461static struct syscore_ops amd_iommu_syscore_ops = {
7441e9cb
JR
1462 .suspend = amd_iommu_suspend,
1463 .resume = amd_iommu_resume,
1464};
1465
8704a1ba
JR
1466static void __init free_on_init_error(void)
1467{
1468 amd_iommu_uninit_devices();
1469
1470 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
1471 get_order(MAX_DOMAIN_ID/8));
1472
1473 free_pages((unsigned long)amd_iommu_rlookup_table,
1474 get_order(rlookup_table_size));
1475
1476 free_pages((unsigned long)amd_iommu_alias_table,
1477 get_order(alias_table_size));
1478
1479 free_pages((unsigned long)amd_iommu_dev_table,
1480 get_order(dev_table_size));
1481
1482 free_iommu_all();
1483
1484 free_unity_maps();
1485
1486#ifdef CONFIG_GART_IOMMU
1487 /*
1488 * We failed to initialize the AMD IOMMU - try fallback to GART
1489 * if possible.
1490 */
1491 gart_iommu_init();
1492
1493#endif
1494}
1495
b65233a9 1496/*
8704a1ba
JR
1497 * This is the hardware init function for AMD IOMMU in the system.
1498 * This function is called either from amd_iommu_init or from the interrupt
1499 * remapping setup code.
b65233a9
JR
1500 *
1501 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
1502 * three times:
1503 *
1504 * 1 pass) Find the highest PCI device id the driver has to handle.
1505 * Upon this information the size of the data structures is
1506 * determined that needs to be allocated.
1507 *
1508 * 2 pass) Initialize the data structures just allocated with the
1509 * information in the ACPI table about available AMD IOMMUs
1510 * in the system. It also maps the PCI devices in the
1511 * system to specific IOMMUs
1512 *
1513 * 3 pass) After the basic data structures are allocated and
1514 * initialized we update them with information about memory
1515 * remapping requirements parsed out of the ACPI table in
1516 * this last pass.
1517 *
8704a1ba
JR
1518 * After everything is set up the IOMMUs are enabled and the necessary
1519 * hotplug and suspend notifiers are registered.
b65233a9 1520 */
643511b3 1521static int __init early_amd_iommu_init(void)
fe74c9cf 1522{
02f3b3f5
JR
1523 struct acpi_table_header *ivrs_base;
1524 acpi_size ivrs_size;
1525 acpi_status status;
fe74c9cf
JR
1526 int i, ret = 0;
1527
643511b3 1528 if (!amd_iommu_detected)
8704a1ba
JR
1529 return -ENODEV;
1530
02f3b3f5
JR
1531 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
1532 if (status == AE_NOT_FOUND)
1533 return -ENODEV;
1534 else if (ACPI_FAILURE(status)) {
1535 const char *err = acpi_format_exception(status);
1536 pr_err("AMD-Vi: IVRS table error: %s\n", err);
1537 return -EINVAL;
1538 }
1539
fe74c9cf
JR
1540 /*
1541 * First parse ACPI tables to find the largest Bus/Dev/Func
1542 * we need to handle. Upon this information the shared data
1543 * structures for the IOMMUs in the system will be allocated
1544 */
2c0ae172
JR
1545 ret = find_last_devid_acpi(ivrs_base);
1546 if (ret)
3551a708
JR
1547 goto out;
1548
c571484e
JR
1549 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
1550 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
1551 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
fe74c9cf 1552
fe74c9cf 1553 /* Device table - directly used by all IOMMUs */
8704a1ba 1554 ret = -ENOMEM;
5dc8bff0 1555 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
fe74c9cf
JR
1556 get_order(dev_table_size));
1557 if (amd_iommu_dev_table == NULL)
1558 goto out;
1559
1560 /*
1561 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
1562 * IOMMU see for that device
1563 */
1564 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
1565 get_order(alias_table_size));
1566 if (amd_iommu_alias_table == NULL)
2c0ae172 1567 goto out;
fe74c9cf
JR
1568
1569 /* IOMMU rlookup table - find the IOMMU for a specific device */
83fd5cc6
JR
1570 amd_iommu_rlookup_table = (void *)__get_free_pages(
1571 GFP_KERNEL | __GFP_ZERO,
fe74c9cf
JR
1572 get_order(rlookup_table_size));
1573 if (amd_iommu_rlookup_table == NULL)
2c0ae172 1574 goto out;
fe74c9cf 1575
5dc8bff0
JR
1576 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
1577 GFP_KERNEL | __GFP_ZERO,
fe74c9cf
JR
1578 get_order(MAX_DOMAIN_ID/8));
1579 if (amd_iommu_pd_alloc_bitmap == NULL)
2c0ae172 1580 goto out;
fe74c9cf 1581
9f5f5fb3
JR
1582 /* init the device table */
1583 init_device_table();
1584
fe74c9cf 1585 /*
5dc8bff0 1586 * let all alias entries point to itself
fe74c9cf 1587 */
3a61ec38 1588 for (i = 0; i <= amd_iommu_last_bdf; ++i)
fe74c9cf
JR
1589 amd_iommu_alias_table[i] = i;
1590
fe74c9cf
JR
1591 /*
1592 * never allocate domain 0 because its used as the non-allocated and
1593 * error value placeholder
1594 */
1595 amd_iommu_pd_alloc_bitmap[0] = 1;
1596
aeb26f55
JR
1597 spin_lock_init(&amd_iommu_pd_lock);
1598
fe74c9cf
JR
1599 /*
1600 * now the data structures are allocated and basically initialized
1601 * start the real acpi table scan
1602 */
02f3b3f5
JR
1603 ret = init_iommu_all(ivrs_base);
1604 if (ret)
2c0ae172 1605 goto out;
fe74c9cf 1606
02f3b3f5
JR
1607 ret = init_memory_definitions(ivrs_base);
1608 if (ret)
2c0ae172 1609 goto out;
3551a708 1610
8704a1ba 1611out:
02f3b3f5
JR
1612 /* Don't leak any ACPI memory */
1613 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
1614 ivrs_base = NULL;
1615
643511b3
JR
1616 return ret;
1617}
1618
ae295142 1619static int amd_iommu_enable_interrupts(void)
3d9761e7
JR
1620{
1621 struct amd_iommu *iommu;
1622 int ret = 0;
1623
1624 for_each_iommu(iommu) {
1625 ret = iommu_init_msi(iommu);
1626 if (ret)
1627 goto out;
1628 }
1629
1630out:
1631 return ret;
1632}
1633
02f3b3f5
JR
1634static bool detect_ivrs(void)
1635{
1636 struct acpi_table_header *ivrs_base;
1637 acpi_size ivrs_size;
1638 acpi_status status;
1639
1640 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
1641 if (status == AE_NOT_FOUND)
1642 return false;
1643 else if (ACPI_FAILURE(status)) {
1644 const char *err = acpi_format_exception(status);
1645 pr_err("AMD-Vi: IVRS table error: %s\n", err);
1646 return false;
1647 }
1648
1649 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
1650
1adb7d31
JR
1651 /* Make sure ACS will be enabled during PCI probe */
1652 pci_request_acs();
1653
02f3b3f5
JR
1654 return true;
1655}
1656
b9b1ce70
JR
1657static int amd_iommu_init_dma(void)
1658{
1659 int ret;
1660
1661 if (iommu_pass_through)
1662 ret = amd_iommu_init_passthrough();
1663 else
1664 ret = amd_iommu_init_dma_ops();
1665
1666 if (ret)
1667 return ret;
1668
1669 amd_iommu_init_api();
1670
1671 amd_iommu_init_notifier();
1672
1673 return 0;
1674}
1675
2c0ae172 1676/****************************************************************************
8704a1ba 1677 *
2c0ae172
JR
1678 * AMD IOMMU Initialization State Machine
1679 *
1680 ****************************************************************************/
1681
1682static int __init state_next(void)
8704a1ba
JR
1683{
1684 int ret = 0;
1685
2c0ae172
JR
1686 switch (init_state) {
1687 case IOMMU_START_STATE:
1688 if (!detect_ivrs()) {
1689 init_state = IOMMU_NOT_FOUND;
1690 ret = -ENODEV;
1691 } else {
1692 init_state = IOMMU_IVRS_DETECTED;
1693 }
1694 break;
1695 case IOMMU_IVRS_DETECTED:
1696 ret = early_amd_iommu_init();
1697 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
1698 break;
1699 case IOMMU_ACPI_FINISHED:
1700 early_enable_iommus();
1701 register_syscore_ops(&amd_iommu_syscore_ops);
1702 x86_platform.iommu_shutdown = disable_iommus;
1703 init_state = IOMMU_ENABLED;
1704 break;
1705 case IOMMU_ENABLED:
1706 ret = amd_iommu_init_pci();
1707 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
1708 enable_iommus_v2();
1709 break;
1710 case IOMMU_PCI_INIT:
1711 ret = amd_iommu_enable_interrupts();
1712 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
1713 break;
1714 case IOMMU_INTERRUPTS_EN:
1715 ret = amd_iommu_init_dma();
1716 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
1717 break;
1718 case IOMMU_DMA_OPS:
1719 init_state = IOMMU_INITIALIZED;
1720 break;
1721 case IOMMU_INITIALIZED:
1722 /* Nothing to do */
1723 break;
1724 case IOMMU_NOT_FOUND:
1725 case IOMMU_INIT_ERROR:
1726 /* Error states => do nothing */
1727 ret = -EINVAL;
1728 break;
1729 default:
1730 /* Unknown state */
1731 BUG();
1732 }
3d9761e7 1733
2c0ae172
JR
1734 return ret;
1735}
7441e9cb 1736
2c0ae172
JR
1737static int __init iommu_go_to_state(enum iommu_init_state state)
1738{
1739 int ret = 0;
f5325094 1740
2c0ae172
JR
1741 while (init_state != state) {
1742 ret = state_next();
1743 if (init_state == IOMMU_NOT_FOUND ||
1744 init_state == IOMMU_INIT_ERROR)
1745 break;
1746 }
f2f12b6f 1747
fe74c9cf 1748 return ret;
2c0ae172 1749}
fe74c9cf 1750
d7f07769 1751
d7f07769 1752
2c0ae172
JR
1753/*
1754 * This is the core init function for AMD IOMMU hardware in the system.
1755 * This function is called from the generic x86 DMA layer initialization
1756 * code.
1757 */
1758static int __init amd_iommu_init(void)
1759{
1760 int ret;
1761
1762 ret = iommu_go_to_state(IOMMU_INITIALIZED);
1763 if (ret) {
1764 disable_iommus();
1765 free_on_init_error();
1766 }
1767
1768 return ret;
fe74c9cf
JR
1769}
1770
b65233a9
JR
1771/****************************************************************************
1772 *
1773 * Early detect code. This code runs at IOMMU detection time in the DMA
1774 * layer. It just looks if there is an IVRS ACPI table to detect AMD
1775 * IOMMUs
1776 *
1777 ****************************************************************************/
480125ba 1778int __init amd_iommu_detect(void)
ae7877de 1779{
2c0ae172 1780 int ret;
02f3b3f5 1781
75f1cdf1 1782 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
480125ba 1783 return -ENODEV;
ae7877de 1784
a5235725 1785 if (amd_iommu_disabled)
480125ba 1786 return -ENODEV;
a5235725 1787
2c0ae172
JR
1788 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
1789 if (ret)
1790 return ret;
11bd04f6 1791
02f3b3f5
JR
1792 amd_iommu_detected = true;
1793 iommu_detected = 1;
1794 x86_init.iommu.iommu_init = amd_iommu_init;
1795
02f3b3f5 1796 return 0;
ae7877de
JR
1797}
1798
b65233a9
JR
1799/****************************************************************************
1800 *
1801 * Parsing functions for the AMD IOMMU specific kernel command line
1802 * options.
1803 *
1804 ****************************************************************************/
1805
fefda117
JR
1806static int __init parse_amd_iommu_dump(char *str)
1807{
1808 amd_iommu_dump = true;
1809
1810 return 1;
1811}
1812
918ad6c5
JR
1813static int __init parse_amd_iommu_options(char *str)
1814{
1815 for (; *str; ++str) {
695b5676 1816 if (strncmp(str, "fullflush", 9) == 0)
afa9fdc2 1817 amd_iommu_unmap_flush = true;
a5235725
JR
1818 if (strncmp(str, "off", 3) == 0)
1819 amd_iommu_disabled = true;
5abcdba4
JR
1820 if (strncmp(str, "force_isolation", 15) == 0)
1821 amd_iommu_force_isolation = true;
918ad6c5
JR
1822 }
1823
1824 return 1;
1825}
1826
fefda117 1827__setup("amd_iommu_dump", parse_amd_iommu_dump);
918ad6c5 1828__setup("amd_iommu=", parse_amd_iommu_options);
22e6daf4
KRW
1829
1830IOMMU_INIT_FINISH(amd_iommu_detect,
1831 gart_iommu_hole_init,
98f1ad25
JR
1832 NULL,
1833 NULL);
400a28a0
JR
1834
1835bool amd_iommu_v2_supported(void)
1836{
1837 return amd_iommu_v2_present;
1838}
1839EXPORT_SYMBOL(amd_iommu_v2_supported);