2 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
3 * Author: Joerg Roedel <joerg.roedel@amd.com>
4 * Leo Duran <leo.duran@amd.com>
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.
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.
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
20 #include <linux/pci.h>
21 #include <linux/acpi.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
24 #include <linux/syscore_ops.h>
25 #include <linux/interrupt.h>
26 #include <linux/msi.h>
27 #include <linux/amd-iommu.h>
28 #include <linux/export.h>
29 #include <linux/acpi.h>
30 #include <acpi/acpi.h>
31 #include <asm/pci-direct.h>
32 #include <asm/iommu.h>
34 #include <asm/x86_init.h>
35 #include <asm/iommu_table.h>
36 #include <asm/io_apic.h>
37 #include <asm/irq_remapping.h>
39 #include "amd_iommu_proto.h"
40 #include "amd_iommu_types.h"
41 #include "irq_remapping.h"
44 * definitions for the ACPI scanning code
46 #define IVRS_HEADER_LENGTH 48
48 #define ACPI_IVHD_TYPE 0x10
49 #define ACPI_IVMD_TYPE_ALL 0x20
50 #define ACPI_IVMD_TYPE 0x21
51 #define ACPI_IVMD_TYPE_RANGE 0x22
53 #define IVHD_DEV_ALL 0x01
54 #define IVHD_DEV_SELECT 0x02
55 #define IVHD_DEV_SELECT_RANGE_START 0x03
56 #define IVHD_DEV_RANGE_END 0x04
57 #define IVHD_DEV_ALIAS 0x42
58 #define IVHD_DEV_ALIAS_RANGE 0x43
59 #define IVHD_DEV_EXT_SELECT 0x46
60 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
61 #define IVHD_DEV_SPECIAL 0x48
63 #define IVHD_SPECIAL_IOAPIC 1
64 #define IVHD_SPECIAL_HPET 2
66 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
67 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
68 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
69 #define IVHD_FLAG_ISOC_EN_MASK 0x08
71 #define IVMD_FLAG_EXCL_RANGE 0x08
72 #define IVMD_FLAG_UNITY_MAP 0x01
74 #define ACPI_DEVFLAG_INITPASS 0x01
75 #define ACPI_DEVFLAG_EXTINT 0x02
76 #define ACPI_DEVFLAG_NMI 0x04
77 #define ACPI_DEVFLAG_SYSMGT1 0x10
78 #define ACPI_DEVFLAG_SYSMGT2 0x20
79 #define ACPI_DEVFLAG_LINT0 0x40
80 #define ACPI_DEVFLAG_LINT1 0x80
81 #define ACPI_DEVFLAG_ATSDIS 0x10000000
84 * ACPI table definitions
86 * These data structures are laid over the table to parse the important values
91 * structure describing one IOMMU in the ACPI table. Typically followed by one
92 * or more ivhd_entrys.
104 } __attribute__((packed
));
107 * A device entry describing which devices a specific IOMMU translates and
108 * which requestor ids they use.
115 } __attribute__((packed
));
118 * An AMD IOMMU memory definition structure. It defines things like exclusion
119 * ranges for devices and regions that should be unity mapped.
130 } __attribute__((packed
));
133 bool amd_iommu_irq_remap __read_mostly
;
135 static bool amd_iommu_detected
;
136 static bool __initdata amd_iommu_disabled
;
138 u16 amd_iommu_last_bdf
; /* largest PCI device id we have
140 LIST_HEAD(amd_iommu_unity_map
); /* a list of required unity mappings
142 u32 amd_iommu_unmap_flush
; /* if true, flush on every unmap */
144 LIST_HEAD(amd_iommu_list
); /* list of all AMD IOMMUs in the
147 /* Array to assign indices to IOMMUs*/
148 struct amd_iommu
*amd_iommus
[MAX_IOMMUS
];
149 int amd_iommus_present
;
151 /* IOMMUs have a non-present cache? */
152 bool amd_iommu_np_cache __read_mostly
;
153 bool amd_iommu_iotlb_sup __read_mostly
= true;
155 u32 amd_iommu_max_pasids __read_mostly
= ~0;
157 bool amd_iommu_v2_present __read_mostly
;
159 bool amd_iommu_force_isolation __read_mostly
;
162 * List of protection domains - used during resume
164 LIST_HEAD(amd_iommu_pd_list
);
165 spinlock_t amd_iommu_pd_lock
;
168 * Pointer to the device table which is shared by all AMD IOMMUs
169 * it is indexed by the PCI device id or the HT unit id and contains
170 * information about the domain the device belongs to as well as the
171 * page table root pointer.
173 struct dev_table_entry
*amd_iommu_dev_table
;
176 * The alias table is a driver specific data structure which contains the
177 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
178 * More than one device can share the same requestor id.
180 u16
*amd_iommu_alias_table
;
183 * The rlookup table is used to find the IOMMU which is responsible
184 * for a specific device. It is also indexed by the PCI device id.
186 struct amd_iommu
**amd_iommu_rlookup_table
;
189 * This table is used to find the irq remapping table for a given device id
192 struct irq_remap_table
**irq_lookup_table
;
195 * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
196 * to know which ones are already in use.
198 unsigned long *amd_iommu_pd_alloc_bitmap
;
200 static u32 dev_table_size
; /* size of the device table */
201 static u32 alias_table_size
; /* size of the alias table */
202 static u32 rlookup_table_size
; /* size if the rlookup table */
204 enum iommu_init_state
{
217 static enum iommu_init_state init_state
= IOMMU_START_STATE
;
219 static int amd_iommu_enable_interrupts(void);
220 static int __init
iommu_go_to_state(enum iommu_init_state state
);
222 static inline void update_last_devid(u16 devid
)
224 if (devid
> amd_iommu_last_bdf
)
225 amd_iommu_last_bdf
= devid
;
228 static inline unsigned long tbl_size(int entry_size
)
230 unsigned shift
= PAGE_SHIFT
+
231 get_order(((int)amd_iommu_last_bdf
+ 1) * entry_size
);
236 /* Access to l1 and l2 indexed register spaces */
238 static u32
iommu_read_l1(struct amd_iommu
*iommu
, u16 l1
, u8 address
)
242 pci_write_config_dword(iommu
->dev
, 0xf8, (address
| l1
<< 16));
243 pci_read_config_dword(iommu
->dev
, 0xfc, &val
);
247 static void iommu_write_l1(struct amd_iommu
*iommu
, u16 l1
, u8 address
, u32 val
)
249 pci_write_config_dword(iommu
->dev
, 0xf8, (address
| l1
<< 16 | 1 << 31));
250 pci_write_config_dword(iommu
->dev
, 0xfc, val
);
251 pci_write_config_dword(iommu
->dev
, 0xf8, (address
| l1
<< 16));
254 static u32
iommu_read_l2(struct amd_iommu
*iommu
, u8 address
)
258 pci_write_config_dword(iommu
->dev
, 0xf0, address
);
259 pci_read_config_dword(iommu
->dev
, 0xf4, &val
);
263 static void iommu_write_l2(struct amd_iommu
*iommu
, u8 address
, u32 val
)
265 pci_write_config_dword(iommu
->dev
, 0xf0, (address
| 1 << 8));
266 pci_write_config_dword(iommu
->dev
, 0xf4, val
);
269 /****************************************************************************
271 * AMD IOMMU MMIO register space handling functions
273 * These functions are used to program the IOMMU device registers in
274 * MMIO space required for that driver.
276 ****************************************************************************/
279 * This function set the exclusion range in the IOMMU. DMA accesses to the
280 * exclusion range are passed through untranslated
282 static void iommu_set_exclusion_range(struct amd_iommu
*iommu
)
284 u64 start
= iommu
->exclusion_start
& PAGE_MASK
;
285 u64 limit
= (start
+ iommu
->exclusion_length
) & PAGE_MASK
;
288 if (!iommu
->exclusion_start
)
291 entry
= start
| MMIO_EXCL_ENABLE_MASK
;
292 memcpy_toio(iommu
->mmio_base
+ MMIO_EXCL_BASE_OFFSET
,
293 &entry
, sizeof(entry
));
296 memcpy_toio(iommu
->mmio_base
+ MMIO_EXCL_LIMIT_OFFSET
,
297 &entry
, sizeof(entry
));
300 /* Programs the physical address of the device table into the IOMMU hardware */
301 static void iommu_set_device_table(struct amd_iommu
*iommu
)
305 BUG_ON(iommu
->mmio_base
== NULL
);
307 entry
= virt_to_phys(amd_iommu_dev_table
);
308 entry
|= (dev_table_size
>> 12) - 1;
309 memcpy_toio(iommu
->mmio_base
+ MMIO_DEV_TABLE_OFFSET
,
310 &entry
, sizeof(entry
));
313 /* Generic functions to enable/disable certain features of the IOMMU. */
314 static void iommu_feature_enable(struct amd_iommu
*iommu
, u8 bit
)
318 ctrl
= readl(iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
320 writel(ctrl
, iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
323 static void iommu_feature_disable(struct amd_iommu
*iommu
, u8 bit
)
327 ctrl
= readl(iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
329 writel(ctrl
, iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
332 static void iommu_set_inv_tlb_timeout(struct amd_iommu
*iommu
, int timeout
)
336 ctrl
= readl(iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
337 ctrl
&= ~CTRL_INV_TO_MASK
;
338 ctrl
|= (timeout
<< CONTROL_INV_TIMEOUT
) & CTRL_INV_TO_MASK
;
339 writel(ctrl
, iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
342 /* Function to enable the hardware */
343 static void iommu_enable(struct amd_iommu
*iommu
)
345 iommu_feature_enable(iommu
, CONTROL_IOMMU_EN
);
348 static void iommu_disable(struct amd_iommu
*iommu
)
350 /* Disable command buffer */
351 iommu_feature_disable(iommu
, CONTROL_CMDBUF_EN
);
353 /* Disable event logging and event interrupts */
354 iommu_feature_disable(iommu
, CONTROL_EVT_INT_EN
);
355 iommu_feature_disable(iommu
, CONTROL_EVT_LOG_EN
);
357 /* Disable IOMMU hardware itself */
358 iommu_feature_disable(iommu
, CONTROL_IOMMU_EN
);
362 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
363 * the system has one.
365 static u8 __iomem
* __init
iommu_map_mmio_space(u64 address
)
367 if (!request_mem_region(address
, MMIO_REGION_LENGTH
, "amd_iommu")) {
368 pr_err("AMD-Vi: Can not reserve memory region %llx for mmio\n",
370 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
374 return (u8 __iomem
*)ioremap_nocache(address
, MMIO_REGION_LENGTH
);
377 static void __init
iommu_unmap_mmio_space(struct amd_iommu
*iommu
)
379 if (iommu
->mmio_base
)
380 iounmap(iommu
->mmio_base
);
381 release_mem_region(iommu
->mmio_phys
, MMIO_REGION_LENGTH
);
384 /****************************************************************************
386 * The functions below belong to the first pass of AMD IOMMU ACPI table
387 * parsing. In this pass we try to find out the highest device id this
388 * code has to handle. Upon this information the size of the shared data
389 * structures is determined later.
391 ****************************************************************************/
394 * This function calculates the length of a given IVHD entry
396 static inline int ivhd_entry_length(u8
*ivhd
)
398 return 0x04 << (*ivhd
>> 6);
402 * This function reads the last device id the IOMMU has to handle from the PCI
403 * capability header for this IOMMU
405 static int __init
find_last_devid_on_pci(int bus
, int dev
, int fn
, int cap_ptr
)
409 cap
= read_pci_config(bus
, dev
, fn
, cap_ptr
+MMIO_RANGE_OFFSET
);
410 update_last_devid(calc_devid(MMIO_GET_BUS(cap
), MMIO_GET_LD(cap
)));
416 * After reading the highest device id from the IOMMU PCI capability header
417 * this function looks if there is a higher device id defined in the ACPI table
419 static int __init
find_last_devid_from_ivhd(struct ivhd_header
*h
)
421 u8
*p
= (void *)h
, *end
= (void *)h
;
422 struct ivhd_entry
*dev
;
427 find_last_devid_on_pci(PCI_BUS(h
->devid
),
433 dev
= (struct ivhd_entry
*)p
;
435 case IVHD_DEV_SELECT
:
436 case IVHD_DEV_RANGE_END
:
438 case IVHD_DEV_EXT_SELECT
:
439 /* all the above subfield types refer to device ids */
440 update_last_devid(dev
->devid
);
445 p
+= ivhd_entry_length(p
);
454 * Iterate over all IVHD entries in the ACPI table and find the highest device
455 * id which we need to handle. This is the first of three functions which parse
456 * the ACPI table. So we check the checksum here.
458 static int __init
find_last_devid_acpi(struct acpi_table_header
*table
)
461 u8 checksum
= 0, *p
= (u8
*)table
, *end
= (u8
*)table
;
462 struct ivhd_header
*h
;
465 * Validate checksum here so we don't need to do it when
466 * we actually parse the table
468 for (i
= 0; i
< table
->length
; ++i
)
471 /* ACPI table corrupt */
474 p
+= IVRS_HEADER_LENGTH
;
476 end
+= table
->length
;
478 h
= (struct ivhd_header
*)p
;
481 find_last_devid_from_ivhd(h
);
493 /****************************************************************************
495 * The following functions belong the the code path which parses the ACPI table
496 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
497 * data structures, initialize the device/alias/rlookup table and also
498 * basically initialize the hardware.
500 ****************************************************************************/
503 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
504 * write commands to that buffer later and the IOMMU will execute them
507 static u8
* __init
alloc_command_buffer(struct amd_iommu
*iommu
)
509 u8
*cmd_buf
= (u8
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
510 get_order(CMD_BUFFER_SIZE
));
515 iommu
->cmd_buf_size
= CMD_BUFFER_SIZE
| CMD_BUFFER_UNINITIALIZED
;
521 * This function resets the command buffer if the IOMMU stopped fetching
524 void amd_iommu_reset_cmd_buffer(struct amd_iommu
*iommu
)
526 iommu_feature_disable(iommu
, CONTROL_CMDBUF_EN
);
528 writel(0x00, iommu
->mmio_base
+ MMIO_CMD_HEAD_OFFSET
);
529 writel(0x00, iommu
->mmio_base
+ MMIO_CMD_TAIL_OFFSET
);
531 iommu_feature_enable(iommu
, CONTROL_CMDBUF_EN
);
535 * This function writes the command buffer address to the hardware and
538 static void iommu_enable_command_buffer(struct amd_iommu
*iommu
)
542 BUG_ON(iommu
->cmd_buf
== NULL
);
544 entry
= (u64
)virt_to_phys(iommu
->cmd_buf
);
545 entry
|= MMIO_CMD_SIZE_512
;
547 memcpy_toio(iommu
->mmio_base
+ MMIO_CMD_BUF_OFFSET
,
548 &entry
, sizeof(entry
));
550 amd_iommu_reset_cmd_buffer(iommu
);
551 iommu
->cmd_buf_size
&= ~(CMD_BUFFER_UNINITIALIZED
);
554 static void __init
free_command_buffer(struct amd_iommu
*iommu
)
556 free_pages((unsigned long)iommu
->cmd_buf
,
557 get_order(iommu
->cmd_buf_size
& ~(CMD_BUFFER_UNINITIALIZED
)));
560 /* allocates the memory where the IOMMU will log its events to */
561 static u8
* __init
alloc_event_buffer(struct amd_iommu
*iommu
)
563 iommu
->evt_buf
= (u8
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
564 get_order(EVT_BUFFER_SIZE
));
566 if (iommu
->evt_buf
== NULL
)
569 iommu
->evt_buf_size
= EVT_BUFFER_SIZE
;
571 return iommu
->evt_buf
;
574 static void iommu_enable_event_buffer(struct amd_iommu
*iommu
)
578 BUG_ON(iommu
->evt_buf
== NULL
);
580 entry
= (u64
)virt_to_phys(iommu
->evt_buf
) | EVT_LEN_MASK
;
582 memcpy_toio(iommu
->mmio_base
+ MMIO_EVT_BUF_OFFSET
,
583 &entry
, sizeof(entry
));
585 /* set head and tail to zero manually */
586 writel(0x00, iommu
->mmio_base
+ MMIO_EVT_HEAD_OFFSET
);
587 writel(0x00, iommu
->mmio_base
+ MMIO_EVT_TAIL_OFFSET
);
589 iommu_feature_enable(iommu
, CONTROL_EVT_LOG_EN
);
592 static void __init
free_event_buffer(struct amd_iommu
*iommu
)
594 free_pages((unsigned long)iommu
->evt_buf
, get_order(EVT_BUFFER_SIZE
));
597 /* allocates the memory where the IOMMU will log its events to */
598 static u8
* __init
alloc_ppr_log(struct amd_iommu
*iommu
)
600 iommu
->ppr_log
= (u8
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
601 get_order(PPR_LOG_SIZE
));
603 if (iommu
->ppr_log
== NULL
)
606 return iommu
->ppr_log
;
609 static void iommu_enable_ppr_log(struct amd_iommu
*iommu
)
613 if (iommu
->ppr_log
== NULL
)
616 entry
= (u64
)virt_to_phys(iommu
->ppr_log
) | PPR_LOG_SIZE_512
;
618 memcpy_toio(iommu
->mmio_base
+ MMIO_PPR_LOG_OFFSET
,
619 &entry
, sizeof(entry
));
621 /* set head and tail to zero manually */
622 writel(0x00, iommu
->mmio_base
+ MMIO_PPR_HEAD_OFFSET
);
623 writel(0x00, iommu
->mmio_base
+ MMIO_PPR_TAIL_OFFSET
);
625 iommu_feature_enable(iommu
, CONTROL_PPFLOG_EN
);
626 iommu_feature_enable(iommu
, CONTROL_PPR_EN
);
629 static void __init
free_ppr_log(struct amd_iommu
*iommu
)
631 if (iommu
->ppr_log
== NULL
)
634 free_pages((unsigned long)iommu
->ppr_log
, get_order(PPR_LOG_SIZE
));
637 static void iommu_enable_gt(struct amd_iommu
*iommu
)
639 if (!iommu_feature(iommu
, FEATURE_GT
))
642 iommu_feature_enable(iommu
, CONTROL_GT_EN
);
645 /* sets a specific bit in the device table entry. */
646 static void set_dev_entry_bit(u16 devid
, u8 bit
)
648 int i
= (bit
>> 6) & 0x03;
649 int _bit
= bit
& 0x3f;
651 amd_iommu_dev_table
[devid
].data
[i
] |= (1UL << _bit
);
654 static int get_dev_entry_bit(u16 devid
, u8 bit
)
656 int i
= (bit
>> 6) & 0x03;
657 int _bit
= bit
& 0x3f;
659 return (amd_iommu_dev_table
[devid
].data
[i
] & (1UL << _bit
)) >> _bit
;
663 void amd_iommu_apply_erratum_63(u16 devid
)
667 sysmgt
= get_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT1
) |
668 (get_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT2
) << 1);
671 set_dev_entry_bit(devid
, DEV_ENTRY_IW
);
674 /* Writes the specific IOMMU for a device into the rlookup table */
675 static void __init
set_iommu_for_device(struct amd_iommu
*iommu
, u16 devid
)
677 amd_iommu_rlookup_table
[devid
] = iommu
;
681 * This function takes the device specific flags read from the ACPI
682 * table and sets up the device table entry with that information
684 static void __init
set_dev_entry_from_acpi(struct amd_iommu
*iommu
,
685 u16 devid
, u32 flags
, u32 ext_flags
)
687 if (flags
& ACPI_DEVFLAG_INITPASS
)
688 set_dev_entry_bit(devid
, DEV_ENTRY_INIT_PASS
);
689 if (flags
& ACPI_DEVFLAG_EXTINT
)
690 set_dev_entry_bit(devid
, DEV_ENTRY_EINT_PASS
);
691 if (flags
& ACPI_DEVFLAG_NMI
)
692 set_dev_entry_bit(devid
, DEV_ENTRY_NMI_PASS
);
693 if (flags
& ACPI_DEVFLAG_SYSMGT1
)
694 set_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT1
);
695 if (flags
& ACPI_DEVFLAG_SYSMGT2
)
696 set_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT2
);
697 if (flags
& ACPI_DEVFLAG_LINT0
)
698 set_dev_entry_bit(devid
, DEV_ENTRY_LINT0_PASS
);
699 if (flags
& ACPI_DEVFLAG_LINT1
)
700 set_dev_entry_bit(devid
, DEV_ENTRY_LINT1_PASS
);
702 amd_iommu_apply_erratum_63(devid
);
704 set_iommu_for_device(iommu
, devid
);
707 static int add_special_device(u8 type
, u8 id
, u16 devid
)
709 struct devid_map
*entry
;
710 struct list_head
*list
;
712 if (type
!= IVHD_SPECIAL_IOAPIC
&& type
!= IVHD_SPECIAL_HPET
)
715 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
720 entry
->devid
= devid
;
722 if (type
== IVHD_SPECIAL_IOAPIC
)
727 list_add_tail(&entry
->list
, list
);
733 * Reads the device exclusion range from ACPI and initialize IOMMU with
736 static void __init
set_device_exclusion_range(u16 devid
, struct ivmd_header
*m
)
738 struct amd_iommu
*iommu
= amd_iommu_rlookup_table
[devid
];
740 if (!(m
->flags
& IVMD_FLAG_EXCL_RANGE
))
745 * We only can configure exclusion ranges per IOMMU, not
746 * per device. But we can enable the exclusion range per
747 * device. This is done here
749 set_dev_entry_bit(m
->devid
, DEV_ENTRY_EX
);
750 iommu
->exclusion_start
= m
->range_start
;
751 iommu
->exclusion_length
= m
->range_length
;
756 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
757 * initializes the hardware and our data structures with it.
759 static int __init
init_iommu_from_acpi(struct amd_iommu
*iommu
,
760 struct ivhd_header
*h
)
763 u8
*end
= p
, flags
= 0;
764 u16 devid
= 0, devid_start
= 0, devid_to
= 0;
765 u32 dev_i
, ext_flags
= 0;
767 struct ivhd_entry
*e
;
770 * First save the recommended feature enable bits from ACPI
772 iommu
->acpi_flags
= h
->flags
;
775 * Done. Now parse the device entries
777 p
+= sizeof(struct ivhd_header
);
782 e
= (struct ivhd_entry
*)p
;
786 DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
787 " last device %02x:%02x.%x flags: %02x\n",
788 PCI_BUS(iommu
->first_device
),
789 PCI_SLOT(iommu
->first_device
),
790 PCI_FUNC(iommu
->first_device
),
791 PCI_BUS(iommu
->last_device
),
792 PCI_SLOT(iommu
->last_device
),
793 PCI_FUNC(iommu
->last_device
),
796 for (dev_i
= iommu
->first_device
;
797 dev_i
<= iommu
->last_device
; ++dev_i
)
798 set_dev_entry_from_acpi(iommu
, dev_i
,
801 case IVHD_DEV_SELECT
:
803 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
811 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
, 0);
813 case IVHD_DEV_SELECT_RANGE_START
:
815 DUMP_printk(" DEV_SELECT_RANGE_START\t "
816 "devid: %02x:%02x.%x flags: %02x\n",
822 devid_start
= e
->devid
;
829 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
830 "flags: %02x devid_to: %02x:%02x.%x\n",
835 PCI_BUS(e
->ext
>> 8),
836 PCI_SLOT(e
->ext
>> 8),
837 PCI_FUNC(e
->ext
>> 8));
840 devid_to
= e
->ext
>> 8;
841 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
, 0);
842 set_dev_entry_from_acpi(iommu
, devid_to
, e
->flags
, 0);
843 amd_iommu_alias_table
[devid
] = devid_to
;
845 case IVHD_DEV_ALIAS_RANGE
:
847 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
848 "devid: %02x:%02x.%x flags: %02x "
849 "devid_to: %02x:%02x.%x\n",
854 PCI_BUS(e
->ext
>> 8),
855 PCI_SLOT(e
->ext
>> 8),
856 PCI_FUNC(e
->ext
>> 8));
858 devid_start
= e
->devid
;
860 devid_to
= e
->ext
>> 8;
864 case IVHD_DEV_EXT_SELECT
:
866 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
867 "flags: %02x ext: %08x\n",
874 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
,
877 case IVHD_DEV_EXT_SELECT_RANGE
:
879 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
880 "%02x:%02x.%x flags: %02x ext: %08x\n",
886 devid_start
= e
->devid
;
891 case IVHD_DEV_RANGE_END
:
893 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
899 for (dev_i
= devid_start
; dev_i
<= devid
; ++dev_i
) {
901 amd_iommu_alias_table
[dev_i
] = devid_to
;
902 set_dev_entry_from_acpi(iommu
,
903 devid_to
, flags
, ext_flags
);
905 set_dev_entry_from_acpi(iommu
, dev_i
,
909 case IVHD_DEV_SPECIAL
: {
915 handle
= e
->ext
& 0xff;
916 devid
= (e
->ext
>> 8) & 0xffff;
917 type
= (e
->ext
>> 24) & 0xff;
919 if (type
== IVHD_SPECIAL_IOAPIC
)
921 else if (type
== IVHD_SPECIAL_HPET
)
926 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
932 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
, 0);
933 ret
= add_special_device(type
, handle
, devid
);
942 p
+= ivhd_entry_length(p
);
948 /* Initializes the device->iommu mapping for the driver */
949 static int __init
init_iommu_devices(struct amd_iommu
*iommu
)
953 for (i
= iommu
->first_device
; i
<= iommu
->last_device
; ++i
)
954 set_iommu_for_device(iommu
, i
);
959 static void __init
free_iommu_one(struct amd_iommu
*iommu
)
961 free_command_buffer(iommu
);
962 free_event_buffer(iommu
);
964 iommu_unmap_mmio_space(iommu
);
967 static void __init
free_iommu_all(void)
969 struct amd_iommu
*iommu
, *next
;
971 for_each_iommu_safe(iommu
, next
) {
972 list_del(&iommu
->list
);
973 free_iommu_one(iommu
);
979 * This function clues the initialization function for one IOMMU
980 * together and also allocates the command buffer and programs the
981 * hardware. It does NOT enable the IOMMU. This is done afterwards.
983 static int __init
init_iommu_one(struct amd_iommu
*iommu
, struct ivhd_header
*h
)
987 spin_lock_init(&iommu
->lock
);
989 /* Add IOMMU to internal data structures */
990 list_add_tail(&iommu
->list
, &amd_iommu_list
);
991 iommu
->index
= amd_iommus_present
++;
993 if (unlikely(iommu
->index
>= MAX_IOMMUS
)) {
994 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
998 /* Index is fine - add IOMMU to the array */
999 amd_iommus
[iommu
->index
] = iommu
;
1002 * Copy data from ACPI table entry to the iommu struct
1004 iommu
->devid
= h
->devid
;
1005 iommu
->cap_ptr
= h
->cap_ptr
;
1006 iommu
->pci_seg
= h
->pci_seg
;
1007 iommu
->mmio_phys
= h
->mmio_phys
;
1008 iommu
->mmio_base
= iommu_map_mmio_space(h
->mmio_phys
);
1009 if (!iommu
->mmio_base
)
1012 iommu
->cmd_buf
= alloc_command_buffer(iommu
);
1013 if (!iommu
->cmd_buf
)
1016 iommu
->evt_buf
= alloc_event_buffer(iommu
);
1017 if (!iommu
->evt_buf
)
1020 iommu
->int_enabled
= false;
1022 ret
= init_iommu_from_acpi(iommu
, h
);
1027 * Make sure IOMMU is not considered to translate itself. The IVRS
1028 * table tells us so, but this is a lie!
1030 amd_iommu_rlookup_table
[iommu
->devid
] = NULL
;
1032 init_iommu_devices(iommu
);
1038 * Iterates over all IOMMU entries in the ACPI table, allocates the
1039 * IOMMU structure and initializes it with init_iommu_one()
1041 static int __init
init_iommu_all(struct acpi_table_header
*table
)
1043 u8
*p
= (u8
*)table
, *end
= (u8
*)table
;
1044 struct ivhd_header
*h
;
1045 struct amd_iommu
*iommu
;
1048 end
+= table
->length
;
1049 p
+= IVRS_HEADER_LENGTH
;
1052 h
= (struct ivhd_header
*)p
;
1054 case ACPI_IVHD_TYPE
:
1056 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
1057 "seg: %d flags: %01x info %04x\n",
1058 PCI_BUS(h
->devid
), PCI_SLOT(h
->devid
),
1059 PCI_FUNC(h
->devid
), h
->cap_ptr
,
1060 h
->pci_seg
, h
->flags
, h
->info
);
1061 DUMP_printk(" mmio-addr: %016llx\n",
1064 iommu
= kzalloc(sizeof(struct amd_iommu
), GFP_KERNEL
);
1068 ret
= init_iommu_one(iommu
, h
);
1083 static int iommu_init_pci(struct amd_iommu
*iommu
)
1085 int cap_ptr
= iommu
->cap_ptr
;
1086 u32 range
, misc
, low
, high
;
1088 iommu
->dev
= pci_get_bus_and_slot(PCI_BUS(iommu
->devid
),
1089 iommu
->devid
& 0xff);
1093 pci_read_config_dword(iommu
->dev
, cap_ptr
+ MMIO_CAP_HDR_OFFSET
,
1095 pci_read_config_dword(iommu
->dev
, cap_ptr
+ MMIO_RANGE_OFFSET
,
1097 pci_read_config_dword(iommu
->dev
, cap_ptr
+ MMIO_MISC_OFFSET
,
1100 iommu
->first_device
= calc_devid(MMIO_GET_BUS(range
),
1101 MMIO_GET_FD(range
));
1102 iommu
->last_device
= calc_devid(MMIO_GET_BUS(range
),
1103 MMIO_GET_LD(range
));
1105 if (!(iommu
->cap
& (1 << IOMMU_CAP_IOTLB
)))
1106 amd_iommu_iotlb_sup
= false;
1108 /* read extended feature bits */
1109 low
= readl(iommu
->mmio_base
+ MMIO_EXT_FEATURES
);
1110 high
= readl(iommu
->mmio_base
+ MMIO_EXT_FEATURES
+ 4);
1112 iommu
->features
= ((u64
)high
<< 32) | low
;
1114 if (iommu_feature(iommu
, FEATURE_GT
)) {
1119 shift
= iommu
->features
& FEATURE_PASID_MASK
;
1120 shift
>>= FEATURE_PASID_SHIFT
;
1121 pasids
= (1 << shift
);
1123 amd_iommu_max_pasids
= min(amd_iommu_max_pasids
, pasids
);
1125 glxval
= iommu
->features
& FEATURE_GLXVAL_MASK
;
1126 glxval
>>= FEATURE_GLXVAL_SHIFT
;
1128 if (amd_iommu_max_glx_val
== -1)
1129 amd_iommu_max_glx_val
= glxval
;
1131 amd_iommu_max_glx_val
= min(amd_iommu_max_glx_val
, glxval
);
1134 if (iommu_feature(iommu
, FEATURE_GT
) &&
1135 iommu_feature(iommu
, FEATURE_PPR
)) {
1136 iommu
->is_iommu_v2
= true;
1137 amd_iommu_v2_present
= true;
1140 if (iommu_feature(iommu
, FEATURE_PPR
)) {
1141 iommu
->ppr_log
= alloc_ppr_log(iommu
);
1142 if (!iommu
->ppr_log
)
1146 if (iommu
->cap
& (1UL << IOMMU_CAP_NPCACHE
))
1147 amd_iommu_np_cache
= true;
1149 if (is_rd890_iommu(iommu
->dev
)) {
1152 iommu
->root_pdev
= pci_get_bus_and_slot(iommu
->dev
->bus
->number
,
1156 * Some rd890 systems may not be fully reconfigured by the
1157 * BIOS, so it's necessary for us to store this information so
1158 * it can be reprogrammed on resume
1160 pci_read_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 4,
1161 &iommu
->stored_addr_lo
);
1162 pci_read_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 8,
1163 &iommu
->stored_addr_hi
);
1165 /* Low bit locks writes to configuration space */
1166 iommu
->stored_addr_lo
&= ~1;
1168 for (i
= 0; i
< 6; i
++)
1169 for (j
= 0; j
< 0x12; j
++)
1170 iommu
->stored_l1
[i
][j
] = iommu_read_l1(iommu
, i
, j
);
1172 for (i
= 0; i
< 0x83; i
++)
1173 iommu
->stored_l2
[i
] = iommu_read_l2(iommu
, i
);
1176 return pci_enable_device(iommu
->dev
);
1179 static void print_iommu_info(void)
1181 static const char * const feat_str
[] = {
1182 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1183 "IA", "GA", "HE", "PC"
1185 struct amd_iommu
*iommu
;
1187 for_each_iommu(iommu
) {
1190 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1191 dev_name(&iommu
->dev
->dev
), iommu
->cap_ptr
);
1193 if (iommu
->cap
& (1 << IOMMU_CAP_EFR
)) {
1194 pr_info("AMD-Vi: Extended features: ");
1195 for (i
= 0; i
< ARRAY_SIZE(feat_str
); ++i
) {
1196 if (iommu_feature(iommu
, (1ULL << i
)))
1197 pr_cont(" %s", feat_str
[i
]);
1204 static int __init
amd_iommu_init_pci(void)
1206 struct amd_iommu
*iommu
;
1209 for_each_iommu(iommu
) {
1210 ret
= iommu_init_pci(iommu
);
1215 ret
= amd_iommu_init_devices();
1222 /****************************************************************************
1224 * The following functions initialize the MSI interrupts for all IOMMUs
1225 * in the system. Its a bit challenging because there could be multiple
1226 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1229 ****************************************************************************/
1231 static int iommu_setup_msi(struct amd_iommu
*iommu
)
1235 r
= pci_enable_msi(iommu
->dev
);
1239 r
= request_threaded_irq(iommu
->dev
->irq
,
1240 amd_iommu_int_handler
,
1241 amd_iommu_int_thread
,
1246 pci_disable_msi(iommu
->dev
);
1250 iommu
->int_enabled
= true;
1255 static int iommu_init_msi(struct amd_iommu
*iommu
)
1259 if (iommu
->int_enabled
)
1262 if (pci_find_capability(iommu
->dev
, PCI_CAP_ID_MSI
))
1263 ret
= iommu_setup_msi(iommu
);
1271 iommu_feature_enable(iommu
, CONTROL_EVT_INT_EN
);
1273 if (iommu
->ppr_log
!= NULL
)
1274 iommu_feature_enable(iommu
, CONTROL_PPFINT_EN
);
1279 /****************************************************************************
1281 * The next functions belong to the third pass of parsing the ACPI
1282 * table. In this last pass the memory mapping requirements are
1283 * gathered (like exclusion and unity mapping reanges).
1285 ****************************************************************************/
1287 static void __init
free_unity_maps(void)
1289 struct unity_map_entry
*entry
, *next
;
1291 list_for_each_entry_safe(entry
, next
, &amd_iommu_unity_map
, list
) {
1292 list_del(&entry
->list
);
1297 /* called when we find an exclusion range definition in ACPI */
1298 static int __init
init_exclusion_range(struct ivmd_header
*m
)
1303 case ACPI_IVMD_TYPE
:
1304 set_device_exclusion_range(m
->devid
, m
);
1306 case ACPI_IVMD_TYPE_ALL
:
1307 for (i
= 0; i
<= amd_iommu_last_bdf
; ++i
)
1308 set_device_exclusion_range(i
, m
);
1310 case ACPI_IVMD_TYPE_RANGE
:
1311 for (i
= m
->devid
; i
<= m
->aux
; ++i
)
1312 set_device_exclusion_range(i
, m
);
1321 /* called for unity map ACPI definition */
1322 static int __init
init_unity_map_range(struct ivmd_header
*m
)
1324 struct unity_map_entry
*e
= NULL
;
1327 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1335 case ACPI_IVMD_TYPE
:
1336 s
= "IVMD_TYPEi\t\t\t";
1337 e
->devid_start
= e
->devid_end
= m
->devid
;
1339 case ACPI_IVMD_TYPE_ALL
:
1340 s
= "IVMD_TYPE_ALL\t\t";
1342 e
->devid_end
= amd_iommu_last_bdf
;
1344 case ACPI_IVMD_TYPE_RANGE
:
1345 s
= "IVMD_TYPE_RANGE\t\t";
1346 e
->devid_start
= m
->devid
;
1347 e
->devid_end
= m
->aux
;
1350 e
->address_start
= PAGE_ALIGN(m
->range_start
);
1351 e
->address_end
= e
->address_start
+ PAGE_ALIGN(m
->range_length
);
1352 e
->prot
= m
->flags
>> 1;
1354 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1355 " range_start: %016llx range_end: %016llx flags: %x\n", s
,
1356 PCI_BUS(e
->devid_start
), PCI_SLOT(e
->devid_start
),
1357 PCI_FUNC(e
->devid_start
), PCI_BUS(e
->devid_end
),
1358 PCI_SLOT(e
->devid_end
), PCI_FUNC(e
->devid_end
),
1359 e
->address_start
, e
->address_end
, m
->flags
);
1361 list_add_tail(&e
->list
, &amd_iommu_unity_map
);
1366 /* iterates over all memory definitions we find in the ACPI table */
1367 static int __init
init_memory_definitions(struct acpi_table_header
*table
)
1369 u8
*p
= (u8
*)table
, *end
= (u8
*)table
;
1370 struct ivmd_header
*m
;
1372 end
+= table
->length
;
1373 p
+= IVRS_HEADER_LENGTH
;
1376 m
= (struct ivmd_header
*)p
;
1377 if (m
->flags
& IVMD_FLAG_EXCL_RANGE
)
1378 init_exclusion_range(m
);
1379 else if (m
->flags
& IVMD_FLAG_UNITY_MAP
)
1380 init_unity_map_range(m
);
1389 * Init the device table to not allow DMA access for devices and
1390 * suppress all page faults
1392 static void init_device_table_dma(void)
1396 for (devid
= 0; devid
<= amd_iommu_last_bdf
; ++devid
) {
1397 set_dev_entry_bit(devid
, DEV_ENTRY_VALID
);
1398 set_dev_entry_bit(devid
, DEV_ENTRY_TRANSLATION
);
1402 static void init_device_table(void)
1406 if (!amd_iommu_irq_remap
)
1409 for (devid
= 0; devid
<= amd_iommu_last_bdf
; ++devid
)
1410 set_dev_entry_bit(devid
, DEV_ENTRY_IRQ_TBL_EN
);
1413 static void iommu_init_flags(struct amd_iommu
*iommu
)
1415 iommu
->acpi_flags
& IVHD_FLAG_HT_TUN_EN_MASK
?
1416 iommu_feature_enable(iommu
, CONTROL_HT_TUN_EN
) :
1417 iommu_feature_disable(iommu
, CONTROL_HT_TUN_EN
);
1419 iommu
->acpi_flags
& IVHD_FLAG_PASSPW_EN_MASK
?
1420 iommu_feature_enable(iommu
, CONTROL_PASSPW_EN
) :
1421 iommu_feature_disable(iommu
, CONTROL_PASSPW_EN
);
1423 iommu
->acpi_flags
& IVHD_FLAG_RESPASSPW_EN_MASK
?
1424 iommu_feature_enable(iommu
, CONTROL_RESPASSPW_EN
) :
1425 iommu_feature_disable(iommu
, CONTROL_RESPASSPW_EN
);
1427 iommu
->acpi_flags
& IVHD_FLAG_ISOC_EN_MASK
?
1428 iommu_feature_enable(iommu
, CONTROL_ISOC_EN
) :
1429 iommu_feature_disable(iommu
, CONTROL_ISOC_EN
);
1432 * make IOMMU memory accesses cache coherent
1434 iommu_feature_enable(iommu
, CONTROL_COHERENT_EN
);
1436 /* Set IOTLB invalidation timeout to 1s */
1437 iommu_set_inv_tlb_timeout(iommu
, CTRL_INV_TO_1S
);
1440 static void iommu_apply_resume_quirks(struct amd_iommu
*iommu
)
1443 u32 ioc_feature_control
;
1444 struct pci_dev
*pdev
= iommu
->root_pdev
;
1446 /* RD890 BIOSes may not have completely reconfigured the iommu */
1447 if (!is_rd890_iommu(iommu
->dev
) || !pdev
)
1451 * First, we need to ensure that the iommu is enabled. This is
1452 * controlled by a register in the northbridge
1455 /* Select Northbridge indirect register 0x75 and enable writing */
1456 pci_write_config_dword(pdev
, 0x60, 0x75 | (1 << 7));
1457 pci_read_config_dword(pdev
, 0x64, &ioc_feature_control
);
1459 /* Enable the iommu */
1460 if (!(ioc_feature_control
& 0x1))
1461 pci_write_config_dword(pdev
, 0x64, ioc_feature_control
| 1);
1463 /* Restore the iommu BAR */
1464 pci_write_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 4,
1465 iommu
->stored_addr_lo
);
1466 pci_write_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 8,
1467 iommu
->stored_addr_hi
);
1469 /* Restore the l1 indirect regs for each of the 6 l1s */
1470 for (i
= 0; i
< 6; i
++)
1471 for (j
= 0; j
< 0x12; j
++)
1472 iommu_write_l1(iommu
, i
, j
, iommu
->stored_l1
[i
][j
]);
1474 /* Restore the l2 indirect regs */
1475 for (i
= 0; i
< 0x83; i
++)
1476 iommu_write_l2(iommu
, i
, iommu
->stored_l2
[i
]);
1478 /* Lock PCI setup registers */
1479 pci_write_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 4,
1480 iommu
->stored_addr_lo
| 1);
1484 * This function finally enables all IOMMUs found in the system after
1485 * they have been initialized
1487 static void early_enable_iommus(void)
1489 struct amd_iommu
*iommu
;
1491 for_each_iommu(iommu
) {
1492 iommu_disable(iommu
);
1493 iommu_init_flags(iommu
);
1494 iommu_set_device_table(iommu
);
1495 iommu_enable_command_buffer(iommu
);
1496 iommu_enable_event_buffer(iommu
);
1497 iommu_set_exclusion_range(iommu
);
1498 iommu_enable(iommu
);
1499 iommu_flush_all_caches(iommu
);
1503 static void enable_iommus_v2(void)
1505 struct amd_iommu
*iommu
;
1507 for_each_iommu(iommu
) {
1508 iommu_enable_ppr_log(iommu
);
1509 iommu_enable_gt(iommu
);
1513 static void enable_iommus(void)
1515 early_enable_iommus();
1520 static void disable_iommus(void)
1522 struct amd_iommu
*iommu
;
1524 for_each_iommu(iommu
)
1525 iommu_disable(iommu
);
1529 * Suspend/Resume support
1530 * disable suspend until real resume implemented
1533 static void amd_iommu_resume(void)
1535 struct amd_iommu
*iommu
;
1537 for_each_iommu(iommu
)
1538 iommu_apply_resume_quirks(iommu
);
1540 /* re-load the hardware */
1543 amd_iommu_enable_interrupts();
1546 static int amd_iommu_suspend(void)
1548 /* disable IOMMUs to go out of the way for BIOS */
1554 static struct syscore_ops amd_iommu_syscore_ops
= {
1555 .suspend
= amd_iommu_suspend
,
1556 .resume
= amd_iommu_resume
,
1559 static void __init
free_on_init_error(void)
1561 free_pages((unsigned long)irq_lookup_table
,
1562 get_order(rlookup_table_size
));
1564 if (amd_iommu_irq_cache
) {
1565 kmem_cache_destroy(amd_iommu_irq_cache
);
1566 amd_iommu_irq_cache
= NULL
;
1570 amd_iommu_uninit_devices();
1572 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap
,
1573 get_order(MAX_DOMAIN_ID
/8));
1575 free_pages((unsigned long)amd_iommu_rlookup_table
,
1576 get_order(rlookup_table_size
));
1578 free_pages((unsigned long)amd_iommu_alias_table
,
1579 get_order(alias_table_size
));
1581 free_pages((unsigned long)amd_iommu_dev_table
,
1582 get_order(dev_table_size
));
1588 #ifdef CONFIG_GART_IOMMU
1590 * We failed to initialize the AMD IOMMU - try fallback to GART
1598 static bool __init
check_ioapic_information(void)
1602 for (idx
= 0; idx
< nr_ioapics
; idx
++) {
1603 int id
= mpc_ioapic_id(idx
);
1605 if (get_ioapic_devid(id
) < 0) {
1606 pr_err(FW_BUG
"AMD-Vi: IO-APIC[%d] not in IVRS table\n", id
);
1607 pr_err("AMD-Vi: Disabling interrupt remapping due to BIOS Bug\n");
1616 * This is the hardware init function for AMD IOMMU in the system.
1617 * This function is called either from amd_iommu_init or from the interrupt
1618 * remapping setup code.
1620 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
1623 * 1 pass) Find the highest PCI device id the driver has to handle.
1624 * Upon this information the size of the data structures is
1625 * determined that needs to be allocated.
1627 * 2 pass) Initialize the data structures just allocated with the
1628 * information in the ACPI table about available AMD IOMMUs
1629 * in the system. It also maps the PCI devices in the
1630 * system to specific IOMMUs
1632 * 3 pass) After the basic data structures are allocated and
1633 * initialized we update them with information about memory
1634 * remapping requirements parsed out of the ACPI table in
1637 * After everything is set up the IOMMUs are enabled and the necessary
1638 * hotplug and suspend notifiers are registered.
1640 static int __init
early_amd_iommu_init(void)
1642 struct acpi_table_header
*ivrs_base
;
1643 acpi_size ivrs_size
;
1647 if (!amd_iommu_detected
)
1650 status
= acpi_get_table_with_size("IVRS", 0, &ivrs_base
, &ivrs_size
);
1651 if (status
== AE_NOT_FOUND
)
1653 else if (ACPI_FAILURE(status
)) {
1654 const char *err
= acpi_format_exception(status
);
1655 pr_err("AMD-Vi: IVRS table error: %s\n", err
);
1660 * First parse ACPI tables to find the largest Bus/Dev/Func
1661 * we need to handle. Upon this information the shared data
1662 * structures for the IOMMUs in the system will be allocated
1664 ret
= find_last_devid_acpi(ivrs_base
);
1668 dev_table_size
= tbl_size(DEV_TABLE_ENTRY_SIZE
);
1669 alias_table_size
= tbl_size(ALIAS_TABLE_ENTRY_SIZE
);
1670 rlookup_table_size
= tbl_size(RLOOKUP_TABLE_ENTRY_SIZE
);
1672 /* Device table - directly used by all IOMMUs */
1674 amd_iommu_dev_table
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
1675 get_order(dev_table_size
));
1676 if (amd_iommu_dev_table
== NULL
)
1680 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
1681 * IOMMU see for that device
1683 amd_iommu_alias_table
= (void *)__get_free_pages(GFP_KERNEL
,
1684 get_order(alias_table_size
));
1685 if (amd_iommu_alias_table
== NULL
)
1688 /* IOMMU rlookup table - find the IOMMU for a specific device */
1689 amd_iommu_rlookup_table
= (void *)__get_free_pages(
1690 GFP_KERNEL
| __GFP_ZERO
,
1691 get_order(rlookup_table_size
));
1692 if (amd_iommu_rlookup_table
== NULL
)
1695 amd_iommu_pd_alloc_bitmap
= (void *)__get_free_pages(
1696 GFP_KERNEL
| __GFP_ZERO
,
1697 get_order(MAX_DOMAIN_ID
/8));
1698 if (amd_iommu_pd_alloc_bitmap
== NULL
)
1702 * let all alias entries point to itself
1704 for (i
= 0; i
<= amd_iommu_last_bdf
; ++i
)
1705 amd_iommu_alias_table
[i
] = i
;
1708 * never allocate domain 0 because its used as the non-allocated and
1709 * error value placeholder
1711 amd_iommu_pd_alloc_bitmap
[0] = 1;
1713 spin_lock_init(&amd_iommu_pd_lock
);
1716 * now the data structures are allocated and basically initialized
1717 * start the real acpi table scan
1719 ret
= init_iommu_all(ivrs_base
);
1723 if (amd_iommu_irq_remap
)
1724 amd_iommu_irq_remap
= check_ioapic_information();
1726 if (amd_iommu_irq_remap
) {
1728 * Interrupt remapping enabled, create kmem_cache for the
1731 amd_iommu_irq_cache
= kmem_cache_create("irq_remap_cache",
1732 MAX_IRQS_PER_TABLE
* sizeof(u32
),
1733 IRQ_TABLE_ALIGNMENT
,
1735 if (!amd_iommu_irq_cache
)
1738 irq_lookup_table
= (void *)__get_free_pages(
1739 GFP_KERNEL
| __GFP_ZERO
,
1740 get_order(rlookup_table_size
));
1741 if (!irq_lookup_table
)
1745 ret
= init_memory_definitions(ivrs_base
);
1749 /* init the device table */
1750 init_device_table();
1753 /* Don't leak any ACPI memory */
1754 early_acpi_os_unmap_memory((char __iomem
*)ivrs_base
, ivrs_size
);
1760 static int amd_iommu_enable_interrupts(void)
1762 struct amd_iommu
*iommu
;
1765 for_each_iommu(iommu
) {
1766 ret
= iommu_init_msi(iommu
);
1775 static bool detect_ivrs(void)
1777 struct acpi_table_header
*ivrs_base
;
1778 acpi_size ivrs_size
;
1781 status
= acpi_get_table_with_size("IVRS", 0, &ivrs_base
, &ivrs_size
);
1782 if (status
== AE_NOT_FOUND
)
1784 else if (ACPI_FAILURE(status
)) {
1785 const char *err
= acpi_format_exception(status
);
1786 pr_err("AMD-Vi: IVRS table error: %s\n", err
);
1790 early_acpi_os_unmap_memory((char __iomem
*)ivrs_base
, ivrs_size
);
1792 /* Make sure ACS will be enabled during PCI probe */
1795 if (!disable_irq_remap
)
1796 amd_iommu_irq_remap
= true;
1801 static int amd_iommu_init_dma(void)
1803 struct amd_iommu
*iommu
;
1806 init_device_table_dma();
1808 for_each_iommu(iommu
)
1809 iommu_flush_all_caches(iommu
);
1811 if (iommu_pass_through
)
1812 ret
= amd_iommu_init_passthrough();
1814 ret
= amd_iommu_init_dma_ops();
1819 amd_iommu_init_api();
1821 amd_iommu_init_notifier();
1826 /****************************************************************************
1828 * AMD IOMMU Initialization State Machine
1830 ****************************************************************************/
1832 static int __init
state_next(void)
1836 switch (init_state
) {
1837 case IOMMU_START_STATE
:
1838 if (!detect_ivrs()) {
1839 init_state
= IOMMU_NOT_FOUND
;
1842 init_state
= IOMMU_IVRS_DETECTED
;
1845 case IOMMU_IVRS_DETECTED
:
1846 ret
= early_amd_iommu_init();
1847 init_state
= ret
? IOMMU_INIT_ERROR
: IOMMU_ACPI_FINISHED
;
1849 case IOMMU_ACPI_FINISHED
:
1850 early_enable_iommus();
1851 register_syscore_ops(&amd_iommu_syscore_ops
);
1852 x86_platform
.iommu_shutdown
= disable_iommus
;
1853 init_state
= IOMMU_ENABLED
;
1856 ret
= amd_iommu_init_pci();
1857 init_state
= ret
? IOMMU_INIT_ERROR
: IOMMU_PCI_INIT
;
1860 case IOMMU_PCI_INIT
:
1861 ret
= amd_iommu_enable_interrupts();
1862 init_state
= ret
? IOMMU_INIT_ERROR
: IOMMU_INTERRUPTS_EN
;
1864 case IOMMU_INTERRUPTS_EN
:
1865 ret
= amd_iommu_init_dma();
1866 init_state
= ret
? IOMMU_INIT_ERROR
: IOMMU_DMA_OPS
;
1869 init_state
= IOMMU_INITIALIZED
;
1871 case IOMMU_INITIALIZED
:
1874 case IOMMU_NOT_FOUND
:
1875 case IOMMU_INIT_ERROR
:
1876 /* Error states => do nothing */
1887 static int __init
iommu_go_to_state(enum iommu_init_state state
)
1891 while (init_state
!= state
) {
1893 if (init_state
== IOMMU_NOT_FOUND
||
1894 init_state
== IOMMU_INIT_ERROR
)
1901 #ifdef CONFIG_IRQ_REMAP
1902 int __init
amd_iommu_prepare(void)
1904 return iommu_go_to_state(IOMMU_ACPI_FINISHED
);
1907 int __init
amd_iommu_supported(void)
1909 return amd_iommu_irq_remap
? 1 : 0;
1912 int __init
amd_iommu_enable(void)
1916 ret
= iommu_go_to_state(IOMMU_ENABLED
);
1920 irq_remapping_enabled
= 1;
1925 void amd_iommu_disable(void)
1927 amd_iommu_suspend();
1930 int amd_iommu_reenable(int mode
)
1937 int __init
amd_iommu_enable_faulting(void)
1939 /* We enable MSI later when PCI is initialized */
1945 * This is the core init function for AMD IOMMU hardware in the system.
1946 * This function is called from the generic x86 DMA layer initialization
1949 static int __init
amd_iommu_init(void)
1953 ret
= iommu_go_to_state(IOMMU_INITIALIZED
);
1956 free_on_init_error();
1962 /****************************************************************************
1964 * Early detect code. This code runs at IOMMU detection time in the DMA
1965 * layer. It just looks if there is an IVRS ACPI table to detect AMD
1968 ****************************************************************************/
1969 int __init
amd_iommu_detect(void)
1973 if (no_iommu
|| (iommu_detected
&& !gart_iommu_aperture
))
1976 if (amd_iommu_disabled
)
1979 ret
= iommu_go_to_state(IOMMU_IVRS_DETECTED
);
1983 amd_iommu_detected
= true;
1985 x86_init
.iommu
.iommu_init
= amd_iommu_init
;
1990 /****************************************************************************
1992 * Parsing functions for the AMD IOMMU specific kernel command line
1995 ****************************************************************************/
1997 static int __init
parse_amd_iommu_dump(char *str
)
1999 amd_iommu_dump
= true;
2004 static int __init
parse_amd_iommu_options(char *str
)
2006 for (; *str
; ++str
) {
2007 if (strncmp(str
, "fullflush", 9) == 0)
2008 amd_iommu_unmap_flush
= true;
2009 if (strncmp(str
, "off", 3) == 0)
2010 amd_iommu_disabled
= true;
2011 if (strncmp(str
, "force_isolation", 15) == 0)
2012 amd_iommu_force_isolation
= true;
2018 __setup("amd_iommu_dump", parse_amd_iommu_dump
);
2019 __setup("amd_iommu=", parse_amd_iommu_options
);
2021 IOMMU_INIT_FINISH(amd_iommu_detect
,
2022 gart_iommu_hole_init
,
2026 bool amd_iommu_v2_supported(void)
2028 return amd_iommu_v2_present
;
2030 EXPORT_SYMBOL(amd_iommu_v2_supported
);