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 <asm/pci-direct.h>
28 #include <asm/amd_iommu_proto.h>
29 #include <asm/amd_iommu_types.h>
30 #include <asm/amd_iommu.h>
31 #include <asm/iommu.h>
33 #include <asm/x86_init.h>
34 #include <asm/iommu_table.h>
36 * definitions for the ACPI scanning code
38 #define IVRS_HEADER_LENGTH 48
40 #define ACPI_IVHD_TYPE 0x10
41 #define ACPI_IVMD_TYPE_ALL 0x20
42 #define ACPI_IVMD_TYPE 0x21
43 #define ACPI_IVMD_TYPE_RANGE 0x22
45 #define IVHD_DEV_ALL 0x01
46 #define IVHD_DEV_SELECT 0x02
47 #define IVHD_DEV_SELECT_RANGE_START 0x03
48 #define IVHD_DEV_RANGE_END 0x04
49 #define IVHD_DEV_ALIAS 0x42
50 #define IVHD_DEV_ALIAS_RANGE 0x43
51 #define IVHD_DEV_EXT_SELECT 0x46
52 #define IVHD_DEV_EXT_SELECT_RANGE 0x47
54 #define IVHD_FLAG_HT_TUN_EN_MASK 0x01
55 #define IVHD_FLAG_PASSPW_EN_MASK 0x02
56 #define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
57 #define IVHD_FLAG_ISOC_EN_MASK 0x08
59 #define IVMD_FLAG_EXCL_RANGE 0x08
60 #define IVMD_FLAG_UNITY_MAP 0x01
62 #define ACPI_DEVFLAG_INITPASS 0x01
63 #define ACPI_DEVFLAG_EXTINT 0x02
64 #define ACPI_DEVFLAG_NMI 0x04
65 #define ACPI_DEVFLAG_SYSMGT1 0x10
66 #define ACPI_DEVFLAG_SYSMGT2 0x20
67 #define ACPI_DEVFLAG_LINT0 0x40
68 #define ACPI_DEVFLAG_LINT1 0x80
69 #define ACPI_DEVFLAG_ATSDIS 0x10000000
72 * ACPI table definitions
74 * These data structures are laid over the table to parse the important values
79 * structure describing one IOMMU in the ACPI table. Typically followed by one
80 * or more ivhd_entrys.
92 } __attribute__((packed
));
95 * A device entry describing which devices a specific IOMMU translates and
96 * which requestor ids they use.
103 } __attribute__((packed
));
106 * An AMD IOMMU memory definition structure. It defines things like exclusion
107 * ranges for devices and regions that should be unity mapped.
118 } __attribute__((packed
));
122 static int __initdata amd_iommu_detected
;
123 static bool __initdata amd_iommu_disabled
;
125 u16 amd_iommu_last_bdf
; /* largest PCI device id we have
127 LIST_HEAD(amd_iommu_unity_map
); /* a list of required unity mappings
129 bool amd_iommu_unmap_flush
; /* if true, flush on every unmap */
131 LIST_HEAD(amd_iommu_list
); /* list of all AMD IOMMUs in the
134 /* Array to assign indices to IOMMUs*/
135 struct amd_iommu
*amd_iommus
[MAX_IOMMUS
];
136 int amd_iommus_present
;
138 /* IOMMUs have a non-present cache? */
139 bool amd_iommu_np_cache __read_mostly
;
142 * The ACPI table parsing functions set this variable on an error
144 static int __initdata amd_iommu_init_err
;
147 * List of protection domains - used during resume
149 LIST_HEAD(amd_iommu_pd_list
);
150 spinlock_t amd_iommu_pd_lock
;
153 * Pointer to the device table which is shared by all AMD IOMMUs
154 * it is indexed by the PCI device id or the HT unit id and contains
155 * information about the domain the device belongs to as well as the
156 * page table root pointer.
158 struct dev_table_entry
*amd_iommu_dev_table
;
161 * The alias table is a driver specific data structure which contains the
162 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
163 * More than one device can share the same requestor id.
165 u16
*amd_iommu_alias_table
;
168 * The rlookup table is used to find the IOMMU which is responsible
169 * for a specific device. It is also indexed by the PCI device id.
171 struct amd_iommu
**amd_iommu_rlookup_table
;
174 * AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap
175 * to know which ones are already in use.
177 unsigned long *amd_iommu_pd_alloc_bitmap
;
179 static u32 dev_table_size
; /* size of the device table */
180 static u32 alias_table_size
; /* size of the alias table */
181 static u32 rlookup_table_size
; /* size if the rlookup table */
183 static inline void update_last_devid(u16 devid
)
185 if (devid
> amd_iommu_last_bdf
)
186 amd_iommu_last_bdf
= devid
;
189 static inline unsigned long tbl_size(int entry_size
)
191 unsigned shift
= PAGE_SHIFT
+
192 get_order(((int)amd_iommu_last_bdf
+ 1) * entry_size
);
197 /* Access to l1 and l2 indexed register spaces */
199 static u32
iommu_read_l1(struct amd_iommu
*iommu
, u16 l1
, u8 address
)
203 pci_write_config_dword(iommu
->dev
, 0xf8, (address
| l1
<< 16));
204 pci_read_config_dword(iommu
->dev
, 0xfc, &val
);
208 static void iommu_write_l1(struct amd_iommu
*iommu
, u16 l1
, u8 address
, u32 val
)
210 pci_write_config_dword(iommu
->dev
, 0xf8, (address
| l1
<< 16 | 1 << 31));
211 pci_write_config_dword(iommu
->dev
, 0xfc, val
);
212 pci_write_config_dword(iommu
->dev
, 0xf8, (address
| l1
<< 16));
215 static u32
iommu_read_l2(struct amd_iommu
*iommu
, u8 address
)
219 pci_write_config_dword(iommu
->dev
, 0xf0, address
);
220 pci_read_config_dword(iommu
->dev
, 0xf4, &val
);
224 static void iommu_write_l2(struct amd_iommu
*iommu
, u8 address
, u32 val
)
226 pci_write_config_dword(iommu
->dev
, 0xf0, (address
| 1 << 8));
227 pci_write_config_dword(iommu
->dev
, 0xf4, val
);
230 /****************************************************************************
232 * AMD IOMMU MMIO register space handling functions
234 * These functions are used to program the IOMMU device registers in
235 * MMIO space required for that driver.
237 ****************************************************************************/
240 * This function set the exclusion range in the IOMMU. DMA accesses to the
241 * exclusion range are passed through untranslated
243 static void iommu_set_exclusion_range(struct amd_iommu
*iommu
)
245 u64 start
= iommu
->exclusion_start
& PAGE_MASK
;
246 u64 limit
= (start
+ iommu
->exclusion_length
) & PAGE_MASK
;
249 if (!iommu
->exclusion_start
)
252 entry
= start
| MMIO_EXCL_ENABLE_MASK
;
253 memcpy_toio(iommu
->mmio_base
+ MMIO_EXCL_BASE_OFFSET
,
254 &entry
, sizeof(entry
));
257 memcpy_toio(iommu
->mmio_base
+ MMIO_EXCL_LIMIT_OFFSET
,
258 &entry
, sizeof(entry
));
261 /* Programs the physical address of the device table into the IOMMU hardware */
262 static void __init
iommu_set_device_table(struct amd_iommu
*iommu
)
266 BUG_ON(iommu
->mmio_base
== NULL
);
268 entry
= virt_to_phys(amd_iommu_dev_table
);
269 entry
|= (dev_table_size
>> 12) - 1;
270 memcpy_toio(iommu
->mmio_base
+ MMIO_DEV_TABLE_OFFSET
,
271 &entry
, sizeof(entry
));
274 /* Generic functions to enable/disable certain features of the IOMMU. */
275 static void iommu_feature_enable(struct amd_iommu
*iommu
, u8 bit
)
279 ctrl
= readl(iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
281 writel(ctrl
, iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
284 static void iommu_feature_disable(struct amd_iommu
*iommu
, u8 bit
)
288 ctrl
= readl(iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
290 writel(ctrl
, iommu
->mmio_base
+ MMIO_CONTROL_OFFSET
);
293 /* Function to enable the hardware */
294 static void iommu_enable(struct amd_iommu
*iommu
)
296 printk(KERN_INFO
"AMD-Vi: Enabling IOMMU at %s cap 0x%hx\n",
297 dev_name(&iommu
->dev
->dev
), iommu
->cap_ptr
);
299 iommu_feature_enable(iommu
, CONTROL_IOMMU_EN
);
302 static void iommu_disable(struct amd_iommu
*iommu
)
304 /* Disable command buffer */
305 iommu_feature_disable(iommu
, CONTROL_CMDBUF_EN
);
307 /* Disable event logging and event interrupts */
308 iommu_feature_disable(iommu
, CONTROL_EVT_INT_EN
);
309 iommu_feature_disable(iommu
, CONTROL_EVT_LOG_EN
);
311 /* Disable IOMMU hardware itself */
312 iommu_feature_disable(iommu
, CONTROL_IOMMU_EN
);
316 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
317 * the system has one.
319 static u8
* __init
iommu_map_mmio_space(u64 address
)
323 if (!request_mem_region(address
, MMIO_REGION_LENGTH
, "amd_iommu")) {
324 pr_err("AMD-Vi: Can not reserve memory region %llx for mmio\n",
326 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
330 ret
= ioremap_nocache(address
, MMIO_REGION_LENGTH
);
334 release_mem_region(address
, MMIO_REGION_LENGTH
);
339 static void __init
iommu_unmap_mmio_space(struct amd_iommu
*iommu
)
341 if (iommu
->mmio_base
)
342 iounmap(iommu
->mmio_base
);
343 release_mem_region(iommu
->mmio_phys
, MMIO_REGION_LENGTH
);
346 /****************************************************************************
348 * The functions below belong to the first pass of AMD IOMMU ACPI table
349 * parsing. In this pass we try to find out the highest device id this
350 * code has to handle. Upon this information the size of the shared data
351 * structures is determined later.
353 ****************************************************************************/
356 * This function calculates the length of a given IVHD entry
358 static inline int ivhd_entry_length(u8
*ivhd
)
360 return 0x04 << (*ivhd
>> 6);
364 * This function reads the last device id the IOMMU has to handle from the PCI
365 * capability header for this IOMMU
367 static int __init
find_last_devid_on_pci(int bus
, int dev
, int fn
, int cap_ptr
)
371 cap
= read_pci_config(bus
, dev
, fn
, cap_ptr
+MMIO_RANGE_OFFSET
);
372 update_last_devid(calc_devid(MMIO_GET_BUS(cap
), MMIO_GET_LD(cap
)));
378 * After reading the highest device id from the IOMMU PCI capability header
379 * this function looks if there is a higher device id defined in the ACPI table
381 static int __init
find_last_devid_from_ivhd(struct ivhd_header
*h
)
383 u8
*p
= (void *)h
, *end
= (void *)h
;
384 struct ivhd_entry
*dev
;
389 find_last_devid_on_pci(PCI_BUS(h
->devid
),
395 dev
= (struct ivhd_entry
*)p
;
397 case IVHD_DEV_SELECT
:
398 case IVHD_DEV_RANGE_END
:
400 case IVHD_DEV_EXT_SELECT
:
401 /* all the above subfield types refer to device ids */
402 update_last_devid(dev
->devid
);
407 p
+= ivhd_entry_length(p
);
416 * Iterate over all IVHD entries in the ACPI table and find the highest device
417 * id which we need to handle. This is the first of three functions which parse
418 * the ACPI table. So we check the checksum here.
420 static int __init
find_last_devid_acpi(struct acpi_table_header
*table
)
423 u8 checksum
= 0, *p
= (u8
*)table
, *end
= (u8
*)table
;
424 struct ivhd_header
*h
;
427 * Validate checksum here so we don't need to do it when
428 * we actually parse the table
430 for (i
= 0; i
< table
->length
; ++i
)
433 /* ACPI table corrupt */
434 amd_iommu_init_err
= -ENODEV
;
438 p
+= IVRS_HEADER_LENGTH
;
440 end
+= table
->length
;
442 h
= (struct ivhd_header
*)p
;
445 find_last_devid_from_ivhd(h
);
457 /****************************************************************************
459 * The following functions belong the the code path which parses the ACPI table
460 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
461 * data structures, initialize the device/alias/rlookup table and also
462 * basically initialize the hardware.
464 ****************************************************************************/
467 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
468 * write commands to that buffer later and the IOMMU will execute them
471 static u8
* __init
alloc_command_buffer(struct amd_iommu
*iommu
)
473 u8
*cmd_buf
= (u8
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
474 get_order(CMD_BUFFER_SIZE
));
479 iommu
->cmd_buf_size
= CMD_BUFFER_SIZE
| CMD_BUFFER_UNINITIALIZED
;
485 * This function resets the command buffer if the IOMMU stopped fetching
488 void amd_iommu_reset_cmd_buffer(struct amd_iommu
*iommu
)
490 iommu_feature_disable(iommu
, CONTROL_CMDBUF_EN
);
492 writel(0x00, iommu
->mmio_base
+ MMIO_CMD_HEAD_OFFSET
);
493 writel(0x00, iommu
->mmio_base
+ MMIO_CMD_TAIL_OFFSET
);
495 iommu_feature_enable(iommu
, CONTROL_CMDBUF_EN
);
499 * This function writes the command buffer address to the hardware and
502 static void iommu_enable_command_buffer(struct amd_iommu
*iommu
)
506 BUG_ON(iommu
->cmd_buf
== NULL
);
508 entry
= (u64
)virt_to_phys(iommu
->cmd_buf
);
509 entry
|= MMIO_CMD_SIZE_512
;
511 memcpy_toio(iommu
->mmio_base
+ MMIO_CMD_BUF_OFFSET
,
512 &entry
, sizeof(entry
));
514 amd_iommu_reset_cmd_buffer(iommu
);
515 iommu
->cmd_buf_size
&= ~(CMD_BUFFER_UNINITIALIZED
);
518 static void __init
free_command_buffer(struct amd_iommu
*iommu
)
520 free_pages((unsigned long)iommu
->cmd_buf
,
521 get_order(iommu
->cmd_buf_size
& ~(CMD_BUFFER_UNINITIALIZED
)));
524 /* allocates the memory where the IOMMU will log its events to */
525 static u8
* __init
alloc_event_buffer(struct amd_iommu
*iommu
)
527 iommu
->evt_buf
= (u8
*)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
528 get_order(EVT_BUFFER_SIZE
));
530 if (iommu
->evt_buf
== NULL
)
533 iommu
->evt_buf_size
= EVT_BUFFER_SIZE
;
535 return iommu
->evt_buf
;
538 static void iommu_enable_event_buffer(struct amd_iommu
*iommu
)
542 BUG_ON(iommu
->evt_buf
== NULL
);
544 entry
= (u64
)virt_to_phys(iommu
->evt_buf
) | EVT_LEN_MASK
;
546 memcpy_toio(iommu
->mmio_base
+ MMIO_EVT_BUF_OFFSET
,
547 &entry
, sizeof(entry
));
549 /* set head and tail to zero manually */
550 writel(0x00, iommu
->mmio_base
+ MMIO_EVT_HEAD_OFFSET
);
551 writel(0x00, iommu
->mmio_base
+ MMIO_EVT_TAIL_OFFSET
);
553 iommu_feature_enable(iommu
, CONTROL_EVT_LOG_EN
);
556 static void __init
free_event_buffer(struct amd_iommu
*iommu
)
558 free_pages((unsigned long)iommu
->evt_buf
, get_order(EVT_BUFFER_SIZE
));
561 /* sets a specific bit in the device table entry. */
562 static void set_dev_entry_bit(u16 devid
, u8 bit
)
564 int i
= (bit
>> 5) & 0x07;
565 int _bit
= bit
& 0x1f;
567 amd_iommu_dev_table
[devid
].data
[i
] |= (1 << _bit
);
570 static int get_dev_entry_bit(u16 devid
, u8 bit
)
572 int i
= (bit
>> 5) & 0x07;
573 int _bit
= bit
& 0x1f;
575 return (amd_iommu_dev_table
[devid
].data
[i
] & (1 << _bit
)) >> _bit
;
579 void amd_iommu_apply_erratum_63(u16 devid
)
583 sysmgt
= get_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT1
) |
584 (get_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT2
) << 1);
587 set_dev_entry_bit(devid
, DEV_ENTRY_IW
);
590 /* Writes the specific IOMMU for a device into the rlookup table */
591 static void __init
set_iommu_for_device(struct amd_iommu
*iommu
, u16 devid
)
593 amd_iommu_rlookup_table
[devid
] = iommu
;
597 * This function takes the device specific flags read from the ACPI
598 * table and sets up the device table entry with that information
600 static void __init
set_dev_entry_from_acpi(struct amd_iommu
*iommu
,
601 u16 devid
, u32 flags
, u32 ext_flags
)
603 if (flags
& ACPI_DEVFLAG_INITPASS
)
604 set_dev_entry_bit(devid
, DEV_ENTRY_INIT_PASS
);
605 if (flags
& ACPI_DEVFLAG_EXTINT
)
606 set_dev_entry_bit(devid
, DEV_ENTRY_EINT_PASS
);
607 if (flags
& ACPI_DEVFLAG_NMI
)
608 set_dev_entry_bit(devid
, DEV_ENTRY_NMI_PASS
);
609 if (flags
& ACPI_DEVFLAG_SYSMGT1
)
610 set_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT1
);
611 if (flags
& ACPI_DEVFLAG_SYSMGT2
)
612 set_dev_entry_bit(devid
, DEV_ENTRY_SYSMGT2
);
613 if (flags
& ACPI_DEVFLAG_LINT0
)
614 set_dev_entry_bit(devid
, DEV_ENTRY_LINT0_PASS
);
615 if (flags
& ACPI_DEVFLAG_LINT1
)
616 set_dev_entry_bit(devid
, DEV_ENTRY_LINT1_PASS
);
618 amd_iommu_apply_erratum_63(devid
);
620 set_iommu_for_device(iommu
, devid
);
624 * Reads the device exclusion range from ACPI and initialize IOMMU with
627 static void __init
set_device_exclusion_range(u16 devid
, struct ivmd_header
*m
)
629 struct amd_iommu
*iommu
= amd_iommu_rlookup_table
[devid
];
631 if (!(m
->flags
& IVMD_FLAG_EXCL_RANGE
))
636 * We only can configure exclusion ranges per IOMMU, not
637 * per device. But we can enable the exclusion range per
638 * device. This is done here
640 set_dev_entry_bit(m
->devid
, DEV_ENTRY_EX
);
641 iommu
->exclusion_start
= m
->range_start
;
642 iommu
->exclusion_length
= m
->range_length
;
647 * This function reads some important data from the IOMMU PCI space and
648 * initializes the driver data structure with it. It reads the hardware
649 * capabilities and the first/last device entries
651 static void __init
init_iommu_from_pci(struct amd_iommu
*iommu
)
653 int cap_ptr
= iommu
->cap_ptr
;
657 pci_read_config_dword(iommu
->dev
, cap_ptr
+ MMIO_CAP_HDR_OFFSET
,
659 pci_read_config_dword(iommu
->dev
, cap_ptr
+ MMIO_RANGE_OFFSET
,
661 pci_read_config_dword(iommu
->dev
, cap_ptr
+ MMIO_MISC_OFFSET
,
664 iommu
->first_device
= calc_devid(MMIO_GET_BUS(range
),
666 iommu
->last_device
= calc_devid(MMIO_GET_BUS(range
),
668 iommu
->evt_msi_num
= MMIO_MSI_NUM(misc
);
670 if (!is_rd890_iommu(iommu
->dev
))
674 * Some rd890 systems may not be fully reconfigured by the BIOS, so
675 * it's necessary for us to store this information so it can be
676 * reprogrammed on resume
679 pci_read_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 4,
680 &iommu
->stored_addr_lo
);
681 pci_read_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 8,
682 &iommu
->stored_addr_hi
);
684 /* Low bit locks writes to configuration space */
685 iommu
->stored_addr_lo
&= ~1;
687 for (i
= 0; i
< 6; i
++)
688 for (j
= 0; j
< 0x12; j
++)
689 iommu
->stored_l1
[i
][j
] = iommu_read_l1(iommu
, i
, j
);
691 for (i
= 0; i
< 0x83; i
++)
692 iommu
->stored_l2
[i
] = iommu_read_l2(iommu
, i
);
696 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
697 * initializes the hardware and our data structures with it.
699 static void __init
init_iommu_from_acpi(struct amd_iommu
*iommu
,
700 struct ivhd_header
*h
)
703 u8
*end
= p
, flags
= 0;
704 u16 dev_i
, devid
= 0, devid_start
= 0, devid_to
= 0;
707 struct ivhd_entry
*e
;
710 * First save the recommended feature enable bits from ACPI
712 iommu
->acpi_flags
= h
->flags
;
715 * Done. Now parse the device entries
717 p
+= sizeof(struct ivhd_header
);
722 e
= (struct ivhd_entry
*)p
;
726 DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"
727 " last device %02x:%02x.%x flags: %02x\n",
728 PCI_BUS(iommu
->first_device
),
729 PCI_SLOT(iommu
->first_device
),
730 PCI_FUNC(iommu
->first_device
),
731 PCI_BUS(iommu
->last_device
),
732 PCI_SLOT(iommu
->last_device
),
733 PCI_FUNC(iommu
->last_device
),
736 for (dev_i
= iommu
->first_device
;
737 dev_i
<= iommu
->last_device
; ++dev_i
)
738 set_dev_entry_from_acpi(iommu
, dev_i
,
741 case IVHD_DEV_SELECT
:
743 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
751 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
, 0);
753 case IVHD_DEV_SELECT_RANGE_START
:
755 DUMP_printk(" DEV_SELECT_RANGE_START\t "
756 "devid: %02x:%02x.%x flags: %02x\n",
762 devid_start
= e
->devid
;
769 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
770 "flags: %02x devid_to: %02x:%02x.%x\n",
775 PCI_BUS(e
->ext
>> 8),
776 PCI_SLOT(e
->ext
>> 8),
777 PCI_FUNC(e
->ext
>> 8));
780 devid_to
= e
->ext
>> 8;
781 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
, 0);
782 set_dev_entry_from_acpi(iommu
, devid_to
, e
->flags
, 0);
783 amd_iommu_alias_table
[devid
] = devid_to
;
785 case IVHD_DEV_ALIAS_RANGE
:
787 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
788 "devid: %02x:%02x.%x flags: %02x "
789 "devid_to: %02x:%02x.%x\n",
794 PCI_BUS(e
->ext
>> 8),
795 PCI_SLOT(e
->ext
>> 8),
796 PCI_FUNC(e
->ext
>> 8));
798 devid_start
= e
->devid
;
800 devid_to
= e
->ext
>> 8;
804 case IVHD_DEV_EXT_SELECT
:
806 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
807 "flags: %02x ext: %08x\n",
814 set_dev_entry_from_acpi(iommu
, devid
, e
->flags
,
817 case IVHD_DEV_EXT_SELECT_RANGE
:
819 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
820 "%02x:%02x.%x flags: %02x ext: %08x\n",
826 devid_start
= e
->devid
;
831 case IVHD_DEV_RANGE_END
:
833 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
839 for (dev_i
= devid_start
; dev_i
<= devid
; ++dev_i
) {
841 amd_iommu_alias_table
[dev_i
] = devid_to
;
842 set_dev_entry_from_acpi(iommu
,
843 devid_to
, flags
, ext_flags
);
845 set_dev_entry_from_acpi(iommu
, dev_i
,
853 p
+= ivhd_entry_length(p
);
857 /* Initializes the device->iommu mapping for the driver */
858 static int __init
init_iommu_devices(struct amd_iommu
*iommu
)
862 for (i
= iommu
->first_device
; i
<= iommu
->last_device
; ++i
)
863 set_iommu_for_device(iommu
, i
);
868 static void __init
free_iommu_one(struct amd_iommu
*iommu
)
870 free_command_buffer(iommu
);
871 free_event_buffer(iommu
);
872 iommu_unmap_mmio_space(iommu
);
875 static void __init
free_iommu_all(void)
877 struct amd_iommu
*iommu
, *next
;
879 for_each_iommu_safe(iommu
, next
) {
880 list_del(&iommu
->list
);
881 free_iommu_one(iommu
);
887 * This function clues the initialization function for one IOMMU
888 * together and also allocates the command buffer and programs the
889 * hardware. It does NOT enable the IOMMU. This is done afterwards.
891 static int __init
init_iommu_one(struct amd_iommu
*iommu
, struct ivhd_header
*h
)
893 spin_lock_init(&iommu
->lock
);
895 /* Add IOMMU to internal data structures */
896 list_add_tail(&iommu
->list
, &amd_iommu_list
);
897 iommu
->index
= amd_iommus_present
++;
899 if (unlikely(iommu
->index
>= MAX_IOMMUS
)) {
900 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
904 /* Index is fine - add IOMMU to the array */
905 amd_iommus
[iommu
->index
] = iommu
;
908 * Copy data from ACPI table entry to the iommu struct
910 iommu
->dev
= pci_get_bus_and_slot(PCI_BUS(h
->devid
), h
->devid
& 0xff);
914 iommu
->cap_ptr
= h
->cap_ptr
;
915 iommu
->pci_seg
= h
->pci_seg
;
916 iommu
->mmio_phys
= h
->mmio_phys
;
917 iommu
->mmio_base
= iommu_map_mmio_space(h
->mmio_phys
);
918 if (!iommu
->mmio_base
)
921 iommu
->cmd_buf
= alloc_command_buffer(iommu
);
925 iommu
->evt_buf
= alloc_event_buffer(iommu
);
929 iommu
->int_enabled
= false;
931 init_iommu_from_pci(iommu
);
932 init_iommu_from_acpi(iommu
, h
);
933 init_iommu_devices(iommu
);
935 if (iommu
->cap
& (1UL << IOMMU_CAP_NPCACHE
))
936 amd_iommu_np_cache
= true;
938 return pci_enable_device(iommu
->dev
);
942 * Iterates over all IOMMU entries in the ACPI table, allocates the
943 * IOMMU structure and initializes it with init_iommu_one()
945 static int __init
init_iommu_all(struct acpi_table_header
*table
)
947 u8
*p
= (u8
*)table
, *end
= (u8
*)table
;
948 struct ivhd_header
*h
;
949 struct amd_iommu
*iommu
;
952 end
+= table
->length
;
953 p
+= IVRS_HEADER_LENGTH
;
956 h
= (struct ivhd_header
*)p
;
960 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
961 "seg: %d flags: %01x info %04x\n",
962 PCI_BUS(h
->devid
), PCI_SLOT(h
->devid
),
963 PCI_FUNC(h
->devid
), h
->cap_ptr
,
964 h
->pci_seg
, h
->flags
, h
->info
);
965 DUMP_printk(" mmio-addr: %016llx\n",
968 iommu
= kzalloc(sizeof(struct amd_iommu
), GFP_KERNEL
);
970 amd_iommu_init_err
= -ENOMEM
;
974 ret
= init_iommu_one(iommu
, h
);
976 amd_iommu_init_err
= ret
;
991 /****************************************************************************
993 * The following functions initialize the MSI interrupts for all IOMMUs
994 * in the system. Its a bit challenging because there could be multiple
995 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
998 ****************************************************************************/
1000 static int iommu_setup_msi(struct amd_iommu
*iommu
)
1004 if (pci_enable_msi(iommu
->dev
))
1007 r
= request_irq(iommu
->dev
->irq
, amd_iommu_int_handler
,
1013 pci_disable_msi(iommu
->dev
);
1017 iommu
->int_enabled
= true;
1018 iommu_feature_enable(iommu
, CONTROL_EVT_INT_EN
);
1023 static int iommu_init_msi(struct amd_iommu
*iommu
)
1025 if (iommu
->int_enabled
)
1028 if (pci_find_capability(iommu
->dev
, PCI_CAP_ID_MSI
))
1029 return iommu_setup_msi(iommu
);
1034 /****************************************************************************
1036 * The next functions belong to the third pass of parsing the ACPI
1037 * table. In this last pass the memory mapping requirements are
1038 * gathered (like exclusion and unity mapping reanges).
1040 ****************************************************************************/
1042 static void __init
free_unity_maps(void)
1044 struct unity_map_entry
*entry
, *next
;
1046 list_for_each_entry_safe(entry
, next
, &amd_iommu_unity_map
, list
) {
1047 list_del(&entry
->list
);
1052 /* called when we find an exclusion range definition in ACPI */
1053 static int __init
init_exclusion_range(struct ivmd_header
*m
)
1058 case ACPI_IVMD_TYPE
:
1059 set_device_exclusion_range(m
->devid
, m
);
1061 case ACPI_IVMD_TYPE_ALL
:
1062 for (i
= 0; i
<= amd_iommu_last_bdf
; ++i
)
1063 set_device_exclusion_range(i
, m
);
1065 case ACPI_IVMD_TYPE_RANGE
:
1066 for (i
= m
->devid
; i
<= m
->aux
; ++i
)
1067 set_device_exclusion_range(i
, m
);
1076 /* called for unity map ACPI definition */
1077 static int __init
init_unity_map_range(struct ivmd_header
*m
)
1079 struct unity_map_entry
*e
= 0;
1082 e
= kzalloc(sizeof(*e
), GFP_KERNEL
);
1090 case ACPI_IVMD_TYPE
:
1091 s
= "IVMD_TYPEi\t\t\t";
1092 e
->devid_start
= e
->devid_end
= m
->devid
;
1094 case ACPI_IVMD_TYPE_ALL
:
1095 s
= "IVMD_TYPE_ALL\t\t";
1097 e
->devid_end
= amd_iommu_last_bdf
;
1099 case ACPI_IVMD_TYPE_RANGE
:
1100 s
= "IVMD_TYPE_RANGE\t\t";
1101 e
->devid_start
= m
->devid
;
1102 e
->devid_end
= m
->aux
;
1105 e
->address_start
= PAGE_ALIGN(m
->range_start
);
1106 e
->address_end
= e
->address_start
+ PAGE_ALIGN(m
->range_length
);
1107 e
->prot
= m
->flags
>> 1;
1109 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1110 " range_start: %016llx range_end: %016llx flags: %x\n", s
,
1111 PCI_BUS(e
->devid_start
), PCI_SLOT(e
->devid_start
),
1112 PCI_FUNC(e
->devid_start
), PCI_BUS(e
->devid_end
),
1113 PCI_SLOT(e
->devid_end
), PCI_FUNC(e
->devid_end
),
1114 e
->address_start
, e
->address_end
, m
->flags
);
1116 list_add_tail(&e
->list
, &amd_iommu_unity_map
);
1121 /* iterates over all memory definitions we find in the ACPI table */
1122 static int __init
init_memory_definitions(struct acpi_table_header
*table
)
1124 u8
*p
= (u8
*)table
, *end
= (u8
*)table
;
1125 struct ivmd_header
*m
;
1127 end
+= table
->length
;
1128 p
+= IVRS_HEADER_LENGTH
;
1131 m
= (struct ivmd_header
*)p
;
1132 if (m
->flags
& IVMD_FLAG_EXCL_RANGE
)
1133 init_exclusion_range(m
);
1134 else if (m
->flags
& IVMD_FLAG_UNITY_MAP
)
1135 init_unity_map_range(m
);
1144 * Init the device table to not allow DMA access for devices and
1145 * suppress all page faults
1147 static void init_device_table(void)
1151 for (devid
= 0; devid
<= amd_iommu_last_bdf
; ++devid
) {
1152 set_dev_entry_bit(devid
, DEV_ENTRY_VALID
);
1153 set_dev_entry_bit(devid
, DEV_ENTRY_TRANSLATION
);
1157 static void iommu_init_flags(struct amd_iommu
*iommu
)
1159 iommu
->acpi_flags
& IVHD_FLAG_HT_TUN_EN_MASK
?
1160 iommu_feature_enable(iommu
, CONTROL_HT_TUN_EN
) :
1161 iommu_feature_disable(iommu
, CONTROL_HT_TUN_EN
);
1163 iommu
->acpi_flags
& IVHD_FLAG_PASSPW_EN_MASK
?
1164 iommu_feature_enable(iommu
, CONTROL_PASSPW_EN
) :
1165 iommu_feature_disable(iommu
, CONTROL_PASSPW_EN
);
1167 iommu
->acpi_flags
& IVHD_FLAG_RESPASSPW_EN_MASK
?
1168 iommu_feature_enable(iommu
, CONTROL_RESPASSPW_EN
) :
1169 iommu_feature_disable(iommu
, CONTROL_RESPASSPW_EN
);
1171 iommu
->acpi_flags
& IVHD_FLAG_ISOC_EN_MASK
?
1172 iommu_feature_enable(iommu
, CONTROL_ISOC_EN
) :
1173 iommu_feature_disable(iommu
, CONTROL_ISOC_EN
);
1176 * make IOMMU memory accesses cache coherent
1178 iommu_feature_enable(iommu
, CONTROL_COHERENT_EN
);
1181 static void iommu_apply_resume_quirks(struct amd_iommu
*iommu
)
1184 u32 ioc_feature_control
;
1185 struct pci_dev
*pdev
= NULL
;
1187 /* RD890 BIOSes may not have completely reconfigured the iommu */
1188 if (!is_rd890_iommu(iommu
->dev
))
1192 * First, we need to ensure that the iommu is enabled. This is
1193 * controlled by a register in the northbridge
1195 pdev
= pci_get_bus_and_slot(iommu
->dev
->bus
->number
, PCI_DEVFN(0, 0));
1200 /* Select Northbridge indirect register 0x75 and enable writing */
1201 pci_write_config_dword(pdev
, 0x60, 0x75 | (1 << 7));
1202 pci_read_config_dword(pdev
, 0x64, &ioc_feature_control
);
1204 /* Enable the iommu */
1205 if (!(ioc_feature_control
& 0x1))
1206 pci_write_config_dword(pdev
, 0x64, ioc_feature_control
| 1);
1210 /* Restore the iommu BAR */
1211 pci_write_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 4,
1212 iommu
->stored_addr_lo
);
1213 pci_write_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 8,
1214 iommu
->stored_addr_hi
);
1216 /* Restore the l1 indirect regs for each of the 6 l1s */
1217 for (i
= 0; i
< 6; i
++)
1218 for (j
= 0; j
< 0x12; j
++)
1219 iommu_write_l1(iommu
, i
, j
, iommu
->stored_l1
[i
][j
]);
1221 /* Restore the l2 indirect regs */
1222 for (i
= 0; i
< 0x83; i
++)
1223 iommu_write_l2(iommu
, i
, iommu
->stored_l2
[i
]);
1225 /* Lock PCI setup registers */
1226 pci_write_config_dword(iommu
->dev
, iommu
->cap_ptr
+ 4,
1227 iommu
->stored_addr_lo
| 1);
1231 * This function finally enables all IOMMUs found in the system after
1232 * they have been initialized
1234 static void enable_iommus(void)
1236 struct amd_iommu
*iommu
;
1238 for_each_iommu(iommu
) {
1239 iommu_disable(iommu
);
1240 iommu_init_flags(iommu
);
1241 iommu_set_device_table(iommu
);
1242 iommu_enable_command_buffer(iommu
);
1243 iommu_enable_event_buffer(iommu
);
1244 iommu_set_exclusion_range(iommu
);
1245 iommu_init_msi(iommu
);
1246 iommu_enable(iommu
);
1250 static void disable_iommus(void)
1252 struct amd_iommu
*iommu
;
1254 for_each_iommu(iommu
)
1255 iommu_disable(iommu
);
1259 * Suspend/Resume support
1260 * disable suspend until real resume implemented
1263 static void amd_iommu_resume(void)
1265 struct amd_iommu
*iommu
;
1267 for_each_iommu(iommu
)
1268 iommu_apply_resume_quirks(iommu
);
1270 /* re-load the hardware */
1274 * we have to flush after the IOMMUs are enabled because a
1275 * disabled IOMMU will never execute the commands we send
1277 amd_iommu_flush_all_devices();
1278 amd_iommu_flush_all_domains();
1281 static int amd_iommu_suspend(void)
1283 /* disable IOMMUs to go out of the way for BIOS */
1289 static struct syscore_ops amd_iommu_syscore_ops
= {
1290 .suspend
= amd_iommu_suspend
,
1291 .resume
= amd_iommu_resume
,
1295 * This is the core init function for AMD IOMMU hardware in the system.
1296 * This function is called from the generic x86 DMA layer initialization
1299 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
1302 * 1 pass) Find the highest PCI device id the driver has to handle.
1303 * Upon this information the size of the data structures is
1304 * determined that needs to be allocated.
1306 * 2 pass) Initialize the data structures just allocated with the
1307 * information in the ACPI table about available AMD IOMMUs
1308 * in the system. It also maps the PCI devices in the
1309 * system to specific IOMMUs
1311 * 3 pass) After the basic data structures are allocated and
1312 * initialized we update them with information about memory
1313 * remapping requirements parsed out of the ACPI table in
1316 * After that the hardware is initialized and ready to go. In the last
1317 * step we do some Linux specific things like registering the driver in
1318 * the dma_ops interface and initializing the suspend/resume support
1319 * functions. Finally it prints some information about AMD IOMMUs and
1320 * the driver state and enables the hardware.
1322 static int __init
amd_iommu_init(void)
1327 * First parse ACPI tables to find the largest Bus/Dev/Func
1328 * we need to handle. Upon this information the shared data
1329 * structures for the IOMMUs in the system will be allocated
1331 if (acpi_table_parse("IVRS", find_last_devid_acpi
) != 0)
1334 ret
= amd_iommu_init_err
;
1338 dev_table_size
= tbl_size(DEV_TABLE_ENTRY_SIZE
);
1339 alias_table_size
= tbl_size(ALIAS_TABLE_ENTRY_SIZE
);
1340 rlookup_table_size
= tbl_size(RLOOKUP_TABLE_ENTRY_SIZE
);
1344 /* Device table - directly used by all IOMMUs */
1345 amd_iommu_dev_table
= (void *)__get_free_pages(GFP_KERNEL
| __GFP_ZERO
,
1346 get_order(dev_table_size
));
1347 if (amd_iommu_dev_table
== NULL
)
1351 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
1352 * IOMMU see for that device
1354 amd_iommu_alias_table
= (void *)__get_free_pages(GFP_KERNEL
,
1355 get_order(alias_table_size
));
1356 if (amd_iommu_alias_table
== NULL
)
1359 /* IOMMU rlookup table - find the IOMMU for a specific device */
1360 amd_iommu_rlookup_table
= (void *)__get_free_pages(
1361 GFP_KERNEL
| __GFP_ZERO
,
1362 get_order(rlookup_table_size
));
1363 if (amd_iommu_rlookup_table
== NULL
)
1366 amd_iommu_pd_alloc_bitmap
= (void *)__get_free_pages(
1367 GFP_KERNEL
| __GFP_ZERO
,
1368 get_order(MAX_DOMAIN_ID
/8));
1369 if (amd_iommu_pd_alloc_bitmap
== NULL
)
1372 /* init the device table */
1373 init_device_table();
1376 * let all alias entries point to itself
1378 for (i
= 0; i
<= amd_iommu_last_bdf
; ++i
)
1379 amd_iommu_alias_table
[i
] = i
;
1382 * never allocate domain 0 because its used as the non-allocated and
1383 * error value placeholder
1385 amd_iommu_pd_alloc_bitmap
[0] = 1;
1387 spin_lock_init(&amd_iommu_pd_lock
);
1390 * now the data structures are allocated and basically initialized
1391 * start the real acpi table scan
1394 if (acpi_table_parse("IVRS", init_iommu_all
) != 0)
1397 if (amd_iommu_init_err
) {
1398 ret
= amd_iommu_init_err
;
1402 if (acpi_table_parse("IVRS", init_memory_definitions
) != 0)
1405 if (amd_iommu_init_err
) {
1406 ret
= amd_iommu_init_err
;
1410 ret
= amd_iommu_init_devices();
1416 if (iommu_pass_through
)
1417 ret
= amd_iommu_init_passthrough();
1419 ret
= amd_iommu_init_dma_ops();
1424 amd_iommu_init_api();
1426 amd_iommu_init_notifier();
1428 register_syscore_ops(&amd_iommu_syscore_ops
);
1430 if (iommu_pass_through
)
1433 if (amd_iommu_unmap_flush
)
1434 printk(KERN_INFO
"AMD-Vi: IO/TLB flush on unmap enabled\n");
1436 printk(KERN_INFO
"AMD-Vi: Lazy IO/TLB flushing enabled\n");
1438 x86_platform
.iommu_shutdown
= disable_iommus
;
1446 amd_iommu_uninit_devices();
1448 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap
,
1449 get_order(MAX_DOMAIN_ID
/8));
1451 free_pages((unsigned long)amd_iommu_rlookup_table
,
1452 get_order(rlookup_table_size
));
1454 free_pages((unsigned long)amd_iommu_alias_table
,
1455 get_order(alias_table_size
));
1457 free_pages((unsigned long)amd_iommu_dev_table
,
1458 get_order(dev_table_size
));
1464 #ifdef CONFIG_GART_IOMMU
1466 * We failed to initialize the AMD IOMMU - try fallback to GART
1476 /****************************************************************************
1478 * Early detect code. This code runs at IOMMU detection time in the DMA
1479 * layer. It just looks if there is an IVRS ACPI table to detect AMD
1482 ****************************************************************************/
1483 static int __init
early_amd_iommu_detect(struct acpi_table_header
*table
)
1488 int __init
amd_iommu_detect(void)
1490 if (no_iommu
|| (iommu_detected
&& !gart_iommu_aperture
))
1493 if (amd_iommu_disabled
)
1496 if (acpi_table_parse("IVRS", early_amd_iommu_detect
) == 0) {
1498 amd_iommu_detected
= 1;
1499 x86_init
.iommu
.iommu_init
= amd_iommu_init
;
1501 /* Make sure ACS will be enabled */
1508 /****************************************************************************
1510 * Parsing functions for the AMD IOMMU specific kernel command line
1513 ****************************************************************************/
1515 static int __init
parse_amd_iommu_dump(char *str
)
1517 amd_iommu_dump
= true;
1522 static int __init
parse_amd_iommu_options(char *str
)
1524 for (; *str
; ++str
) {
1525 if (strncmp(str
, "fullflush", 9) == 0)
1526 amd_iommu_unmap_flush
= true;
1527 if (strncmp(str
, "off", 3) == 0)
1528 amd_iommu_disabled
= true;
1534 __setup("amd_iommu_dump", parse_amd_iommu_dump
);
1535 __setup("amd_iommu=", parse_amd_iommu_options
);
1537 IOMMU_INIT_FINISH(amd_iommu_detect
,
1538 gart_iommu_hole_init
,