Path: blob/master/arch/x86/kernel/amd_iommu_init.c
10817 views
/*1* Copyright (C) 2007-2010 Advanced Micro Devices, Inc.2* Author: Joerg Roedel <[email protected]>3* Leo Duran <[email protected]>4*5* This program is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 as published7* by the Free Software Foundation.8*9* This program is distributed in the hope that it will be useful,10* but WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12* GNU General Public License for more details.13*14* You should have received a copy of the GNU General Public License15* along with this program; if not, write to the Free Software16* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA17*/1819#include <linux/pci.h>20#include <linux/acpi.h>21#include <linux/list.h>22#include <linux/slab.h>23#include <linux/syscore_ops.h>24#include <linux/interrupt.h>25#include <linux/msi.h>26#include <asm/pci-direct.h>27#include <asm/amd_iommu_proto.h>28#include <asm/amd_iommu_types.h>29#include <asm/amd_iommu.h>30#include <asm/iommu.h>31#include <asm/gart.h>32#include <asm/x86_init.h>33#include <asm/iommu_table.h>34/*35* definitions for the ACPI scanning code36*/37#define IVRS_HEADER_LENGTH 483839#define ACPI_IVHD_TYPE 0x1040#define ACPI_IVMD_TYPE_ALL 0x2041#define ACPI_IVMD_TYPE 0x2142#define ACPI_IVMD_TYPE_RANGE 0x224344#define IVHD_DEV_ALL 0x0145#define IVHD_DEV_SELECT 0x0246#define IVHD_DEV_SELECT_RANGE_START 0x0347#define IVHD_DEV_RANGE_END 0x0448#define IVHD_DEV_ALIAS 0x4249#define IVHD_DEV_ALIAS_RANGE 0x4350#define IVHD_DEV_EXT_SELECT 0x4651#define IVHD_DEV_EXT_SELECT_RANGE 0x475253#define IVHD_FLAG_HT_TUN_EN_MASK 0x0154#define IVHD_FLAG_PASSPW_EN_MASK 0x0255#define IVHD_FLAG_RESPASSPW_EN_MASK 0x0456#define IVHD_FLAG_ISOC_EN_MASK 0x085758#define IVMD_FLAG_EXCL_RANGE 0x0859#define IVMD_FLAG_UNITY_MAP 0x016061#define ACPI_DEVFLAG_INITPASS 0x0162#define ACPI_DEVFLAG_EXTINT 0x0263#define ACPI_DEVFLAG_NMI 0x0464#define ACPI_DEVFLAG_SYSMGT1 0x1065#define ACPI_DEVFLAG_SYSMGT2 0x2066#define ACPI_DEVFLAG_LINT0 0x4067#define ACPI_DEVFLAG_LINT1 0x8068#define ACPI_DEVFLAG_ATSDIS 0x100000006970/*71* ACPI table definitions72*73* These data structures are laid over the table to parse the important values74* out of it.75*/7677/*78* structure describing one IOMMU in the ACPI table. Typically followed by one79* or more ivhd_entrys.80*/81struct ivhd_header {82u8 type;83u8 flags;84u16 length;85u16 devid;86u16 cap_ptr;87u64 mmio_phys;88u16 pci_seg;89u16 info;90u32 reserved;91} __attribute__((packed));9293/*94* A device entry describing which devices a specific IOMMU translates and95* which requestor ids they use.96*/97struct ivhd_entry {98u8 type;99u16 devid;100u8 flags;101u32 ext;102} __attribute__((packed));103104/*105* An AMD IOMMU memory definition structure. It defines things like exclusion106* ranges for devices and regions that should be unity mapped.107*/108struct ivmd_header {109u8 type;110u8 flags;111u16 length;112u16 devid;113u16 aux;114u64 resv;115u64 range_start;116u64 range_length;117} __attribute__((packed));118119bool amd_iommu_dump;120121static int __initdata amd_iommu_detected;122static bool __initdata amd_iommu_disabled;123124u16 amd_iommu_last_bdf; /* largest PCI device id we have125to handle */126LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings127we find in ACPI */128bool amd_iommu_unmap_flush; /* if true, flush on every unmap */129130LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the131system */132133/* Array to assign indices to IOMMUs*/134struct amd_iommu *amd_iommus[MAX_IOMMUS];135int amd_iommus_present;136137/* IOMMUs have a non-present cache? */138bool amd_iommu_np_cache __read_mostly;139bool amd_iommu_iotlb_sup __read_mostly = true;140141/*142* The ACPI table parsing functions set this variable on an error143*/144static int __initdata amd_iommu_init_err;145146/*147* List of protection domains - used during resume148*/149LIST_HEAD(amd_iommu_pd_list);150spinlock_t amd_iommu_pd_lock;151152/*153* Pointer to the device table which is shared by all AMD IOMMUs154* it is indexed by the PCI device id or the HT unit id and contains155* information about the domain the device belongs to as well as the156* page table root pointer.157*/158struct dev_table_entry *amd_iommu_dev_table;159160/*161* The alias table is a driver specific data structure which contains the162* 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.164*/165u16 *amd_iommu_alias_table;166167/*168* The rlookup table is used to find the IOMMU which is responsible169* for a specific device. It is also indexed by the PCI device id.170*/171struct amd_iommu **amd_iommu_rlookup_table;172173/*174* AMD IOMMU allows up to 2^16 differend protection domains. This is a bitmap175* to know which ones are already in use.176*/177unsigned long *amd_iommu_pd_alloc_bitmap;178179static u32 dev_table_size; /* size of the device table */180static u32 alias_table_size; /* size of the alias table */181static u32 rlookup_table_size; /* size if the rlookup table */182183/*184* This function flushes all internal caches of185* the IOMMU used by this driver.186*/187extern void iommu_flush_all_caches(struct amd_iommu *iommu);188189static inline void update_last_devid(u16 devid)190{191if (devid > amd_iommu_last_bdf)192amd_iommu_last_bdf = devid;193}194195static inline unsigned long tbl_size(int entry_size)196{197unsigned shift = PAGE_SHIFT +198get_order(((int)amd_iommu_last_bdf + 1) * entry_size);199200return 1UL << shift;201}202203/* Access to l1 and l2 indexed register spaces */204205static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)206{207u32 val;208209pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));210pci_read_config_dword(iommu->dev, 0xfc, &val);211return val;212}213214static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)215{216pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));217pci_write_config_dword(iommu->dev, 0xfc, val);218pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));219}220221static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)222{223u32 val;224225pci_write_config_dword(iommu->dev, 0xf0, address);226pci_read_config_dword(iommu->dev, 0xf4, &val);227return val;228}229230static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)231{232pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));233pci_write_config_dword(iommu->dev, 0xf4, val);234}235236/****************************************************************************237*238* AMD IOMMU MMIO register space handling functions239*240* These functions are used to program the IOMMU device registers in241* MMIO space required for that driver.242*243****************************************************************************/244245/*246* This function set the exclusion range in the IOMMU. DMA accesses to the247* exclusion range are passed through untranslated248*/249static void iommu_set_exclusion_range(struct amd_iommu *iommu)250{251u64 start = iommu->exclusion_start & PAGE_MASK;252u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;253u64 entry;254255if (!iommu->exclusion_start)256return;257258entry = start | MMIO_EXCL_ENABLE_MASK;259memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,260&entry, sizeof(entry));261262entry = limit;263memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,264&entry, sizeof(entry));265}266267/* Programs the physical address of the device table into the IOMMU hardware */268static void __init iommu_set_device_table(struct amd_iommu *iommu)269{270u64 entry;271272BUG_ON(iommu->mmio_base == NULL);273274entry = virt_to_phys(amd_iommu_dev_table);275entry |= (dev_table_size >> 12) - 1;276memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,277&entry, sizeof(entry));278}279280/* Generic functions to enable/disable certain features of the IOMMU. */281static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)282{283u32 ctrl;284285ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);286ctrl |= (1 << bit);287writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);288}289290static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)291{292u32 ctrl;293294ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);295ctrl &= ~(1 << bit);296writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);297}298299/* Function to enable the hardware */300static void iommu_enable(struct amd_iommu *iommu)301{302static const char * const feat_str[] = {303"PreF", "PPR", "X2APIC", "NX", "GT", "[5]",304"IA", "GA", "HE", "PC", NULL305};306int i;307308printk(KERN_INFO "AMD-Vi: Enabling IOMMU at %s cap 0x%hx",309dev_name(&iommu->dev->dev), iommu->cap_ptr);310311if (iommu->cap & (1 << IOMMU_CAP_EFR)) {312printk(KERN_CONT " extended features: ");313for (i = 0; feat_str[i]; ++i)314if (iommu_feature(iommu, (1ULL << i)))315printk(KERN_CONT " %s", feat_str[i]);316}317printk(KERN_CONT "\n");318319iommu_feature_enable(iommu, CONTROL_IOMMU_EN);320}321322static void iommu_disable(struct amd_iommu *iommu)323{324/* Disable command buffer */325iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);326327/* Disable event logging and event interrupts */328iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);329iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);330331/* Disable IOMMU hardware itself */332iommu_feature_disable(iommu, CONTROL_IOMMU_EN);333}334335/*336* mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in337* the system has one.338*/339static u8 * __init iommu_map_mmio_space(u64 address)340{341u8 *ret;342343if (!request_mem_region(address, MMIO_REGION_LENGTH, "amd_iommu")) {344pr_err("AMD-Vi: Can not reserve memory region %llx for mmio\n",345address);346pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");347return NULL;348}349350ret = ioremap_nocache(address, MMIO_REGION_LENGTH);351if (ret != NULL)352return ret;353354release_mem_region(address, MMIO_REGION_LENGTH);355356return NULL;357}358359static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)360{361if (iommu->mmio_base)362iounmap(iommu->mmio_base);363release_mem_region(iommu->mmio_phys, MMIO_REGION_LENGTH);364}365366/****************************************************************************367*368* The functions below belong to the first pass of AMD IOMMU ACPI table369* parsing. In this pass we try to find out the highest device id this370* code has to handle. Upon this information the size of the shared data371* structures is determined later.372*373****************************************************************************/374375/*376* This function calculates the length of a given IVHD entry377*/378static inline int ivhd_entry_length(u8 *ivhd)379{380return 0x04 << (*ivhd >> 6);381}382383/*384* This function reads the last device id the IOMMU has to handle from the PCI385* capability header for this IOMMU386*/387static int __init find_last_devid_on_pci(int bus, int dev, int fn, int cap_ptr)388{389u32 cap;390391cap = read_pci_config(bus, dev, fn, cap_ptr+MMIO_RANGE_OFFSET);392update_last_devid(calc_devid(MMIO_GET_BUS(cap), MMIO_GET_LD(cap)));393394return 0;395}396397/*398* After reading the highest device id from the IOMMU PCI capability header399* this function looks if there is a higher device id defined in the ACPI table400*/401static int __init find_last_devid_from_ivhd(struct ivhd_header *h)402{403u8 *p = (void *)h, *end = (void *)h;404struct ivhd_entry *dev;405406p += sizeof(*h);407end += h->length;408409find_last_devid_on_pci(PCI_BUS(h->devid),410PCI_SLOT(h->devid),411PCI_FUNC(h->devid),412h->cap_ptr);413414while (p < end) {415dev = (struct ivhd_entry *)p;416switch (dev->type) {417case IVHD_DEV_SELECT:418case IVHD_DEV_RANGE_END:419case IVHD_DEV_ALIAS:420case IVHD_DEV_EXT_SELECT:421/* all the above subfield types refer to device ids */422update_last_devid(dev->devid);423break;424default:425break;426}427p += ivhd_entry_length(p);428}429430WARN_ON(p != end);431432return 0;433}434435/*436* Iterate over all IVHD entries in the ACPI table and find the highest device437* id which we need to handle. This is the first of three functions which parse438* the ACPI table. So we check the checksum here.439*/440static int __init find_last_devid_acpi(struct acpi_table_header *table)441{442int i;443u8 checksum = 0, *p = (u8 *)table, *end = (u8 *)table;444struct ivhd_header *h;445446/*447* Validate checksum here so we don't need to do it when448* we actually parse the table449*/450for (i = 0; i < table->length; ++i)451checksum += p[i];452if (checksum != 0) {453/* ACPI table corrupt */454amd_iommu_init_err = -ENODEV;455return 0;456}457458p += IVRS_HEADER_LENGTH;459460end += table->length;461while (p < end) {462h = (struct ivhd_header *)p;463switch (h->type) {464case ACPI_IVHD_TYPE:465find_last_devid_from_ivhd(h);466break;467default:468break;469}470p += h->length;471}472WARN_ON(p != end);473474return 0;475}476477/****************************************************************************478*479* The following functions belong the the code path which parses the ACPI table480* the second time. In this ACPI parsing iteration we allocate IOMMU specific481* data structures, initialize the device/alias/rlookup table and also482* basically initialize the hardware.483*484****************************************************************************/485486/*487* Allocates the command buffer. This buffer is per AMD IOMMU. We can488* write commands to that buffer later and the IOMMU will execute them489* asynchronously490*/491static u8 * __init alloc_command_buffer(struct amd_iommu *iommu)492{493u8 *cmd_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,494get_order(CMD_BUFFER_SIZE));495496if (cmd_buf == NULL)497return NULL;498499iommu->cmd_buf_size = CMD_BUFFER_SIZE | CMD_BUFFER_UNINITIALIZED;500501return cmd_buf;502}503504/*505* This function resets the command buffer if the IOMMU stopped fetching506* commands from it.507*/508void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)509{510iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);511512writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);513writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);514515iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);516}517518/*519* This function writes the command buffer address to the hardware and520* enables it.521*/522static void iommu_enable_command_buffer(struct amd_iommu *iommu)523{524u64 entry;525526BUG_ON(iommu->cmd_buf == NULL);527528entry = (u64)virt_to_phys(iommu->cmd_buf);529entry |= MMIO_CMD_SIZE_512;530531memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,532&entry, sizeof(entry));533534amd_iommu_reset_cmd_buffer(iommu);535iommu->cmd_buf_size &= ~(CMD_BUFFER_UNINITIALIZED);536}537538static void __init free_command_buffer(struct amd_iommu *iommu)539{540free_pages((unsigned long)iommu->cmd_buf,541get_order(iommu->cmd_buf_size & ~(CMD_BUFFER_UNINITIALIZED)));542}543544/* allocates the memory where the IOMMU will log its events to */545static u8 * __init alloc_event_buffer(struct amd_iommu *iommu)546{547iommu->evt_buf = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,548get_order(EVT_BUFFER_SIZE));549550if (iommu->evt_buf == NULL)551return NULL;552553iommu->evt_buf_size = EVT_BUFFER_SIZE;554555return iommu->evt_buf;556}557558static void iommu_enable_event_buffer(struct amd_iommu *iommu)559{560u64 entry;561562BUG_ON(iommu->evt_buf == NULL);563564entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;565566memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,567&entry, sizeof(entry));568569/* set head and tail to zero manually */570writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);571writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);572573iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);574}575576static void __init free_event_buffer(struct amd_iommu *iommu)577{578free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));579}580581/* sets a specific bit in the device table entry. */582static void set_dev_entry_bit(u16 devid, u8 bit)583{584int i = (bit >> 5) & 0x07;585int _bit = bit & 0x1f;586587amd_iommu_dev_table[devid].data[i] |= (1 << _bit);588}589590static int get_dev_entry_bit(u16 devid, u8 bit)591{592int i = (bit >> 5) & 0x07;593int _bit = bit & 0x1f;594595return (amd_iommu_dev_table[devid].data[i] & (1 << _bit)) >> _bit;596}597598599void amd_iommu_apply_erratum_63(u16 devid)600{601int sysmgt;602603sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |604(get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);605606if (sysmgt == 0x01)607set_dev_entry_bit(devid, DEV_ENTRY_IW);608}609610/* Writes the specific IOMMU for a device into the rlookup table */611static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)612{613amd_iommu_rlookup_table[devid] = iommu;614}615616/*617* This function takes the device specific flags read from the ACPI618* table and sets up the device table entry with that information619*/620static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,621u16 devid, u32 flags, u32 ext_flags)622{623if (flags & ACPI_DEVFLAG_INITPASS)624set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);625if (flags & ACPI_DEVFLAG_EXTINT)626set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);627if (flags & ACPI_DEVFLAG_NMI)628set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);629if (flags & ACPI_DEVFLAG_SYSMGT1)630set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);631if (flags & ACPI_DEVFLAG_SYSMGT2)632set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);633if (flags & ACPI_DEVFLAG_LINT0)634set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);635if (flags & ACPI_DEVFLAG_LINT1)636set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);637638amd_iommu_apply_erratum_63(devid);639640set_iommu_for_device(iommu, devid);641}642643/*644* Reads the device exclusion range from ACPI and initialize IOMMU with645* it646*/647static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)648{649struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];650651if (!(m->flags & IVMD_FLAG_EXCL_RANGE))652return;653654if (iommu) {655/*656* We only can configure exclusion ranges per IOMMU, not657* per device. But we can enable the exclusion range per658* device. This is done here659*/660set_dev_entry_bit(m->devid, DEV_ENTRY_EX);661iommu->exclusion_start = m->range_start;662iommu->exclusion_length = m->range_length;663}664}665666/*667* This function reads some important data from the IOMMU PCI space and668* initializes the driver data structure with it. It reads the hardware669* capabilities and the first/last device entries670*/671static void __init init_iommu_from_pci(struct amd_iommu *iommu)672{673int cap_ptr = iommu->cap_ptr;674u32 range, misc, low, high;675int i, j;676677pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,678&iommu->cap);679pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,680&range);681pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,682&misc);683684iommu->first_device = calc_devid(MMIO_GET_BUS(range),685MMIO_GET_FD(range));686iommu->last_device = calc_devid(MMIO_GET_BUS(range),687MMIO_GET_LD(range));688iommu->evt_msi_num = MMIO_MSI_NUM(misc);689690if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))691amd_iommu_iotlb_sup = false;692693/* read extended feature bits */694low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);695high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);696697iommu->features = ((u64)high << 32) | low;698699if (!is_rd890_iommu(iommu->dev))700return;701702/*703* Some rd890 systems may not be fully reconfigured by the BIOS, so704* it's necessary for us to store this information so it can be705* reprogrammed on resume706*/707708pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,709&iommu->stored_addr_lo);710pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,711&iommu->stored_addr_hi);712713/* Low bit locks writes to configuration space */714iommu->stored_addr_lo &= ~1;715716for (i = 0; i < 6; i++)717for (j = 0; j < 0x12; j++)718iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);719720for (i = 0; i < 0x83; i++)721iommu->stored_l2[i] = iommu_read_l2(iommu, i);722}723724/*725* Takes a pointer to an AMD IOMMU entry in the ACPI table and726* initializes the hardware and our data structures with it.727*/728static void __init init_iommu_from_acpi(struct amd_iommu *iommu,729struct ivhd_header *h)730{731u8 *p = (u8 *)h;732u8 *end = p, flags = 0;733u16 devid = 0, devid_start = 0, devid_to = 0;734u32 dev_i, ext_flags = 0;735bool alias = false;736struct ivhd_entry *e;737738/*739* First save the recommended feature enable bits from ACPI740*/741iommu->acpi_flags = h->flags;742743/*744* Done. Now parse the device entries745*/746p += sizeof(struct ivhd_header);747end += h->length;748749750while (p < end) {751e = (struct ivhd_entry *)p;752switch (e->type) {753case IVHD_DEV_ALL:754755DUMP_printk(" DEV_ALL\t\t\t first devid: %02x:%02x.%x"756" last device %02x:%02x.%x flags: %02x\n",757PCI_BUS(iommu->first_device),758PCI_SLOT(iommu->first_device),759PCI_FUNC(iommu->first_device),760PCI_BUS(iommu->last_device),761PCI_SLOT(iommu->last_device),762PCI_FUNC(iommu->last_device),763e->flags);764765for (dev_i = iommu->first_device;766dev_i <= iommu->last_device; ++dev_i)767set_dev_entry_from_acpi(iommu, dev_i,768e->flags, 0);769break;770case IVHD_DEV_SELECT:771772DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "773"flags: %02x\n",774PCI_BUS(e->devid),775PCI_SLOT(e->devid),776PCI_FUNC(e->devid),777e->flags);778779devid = e->devid;780set_dev_entry_from_acpi(iommu, devid, e->flags, 0);781break;782case IVHD_DEV_SELECT_RANGE_START:783784DUMP_printk(" DEV_SELECT_RANGE_START\t "785"devid: %02x:%02x.%x flags: %02x\n",786PCI_BUS(e->devid),787PCI_SLOT(e->devid),788PCI_FUNC(e->devid),789e->flags);790791devid_start = e->devid;792flags = e->flags;793ext_flags = 0;794alias = false;795break;796case IVHD_DEV_ALIAS:797798DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "799"flags: %02x devid_to: %02x:%02x.%x\n",800PCI_BUS(e->devid),801PCI_SLOT(e->devid),802PCI_FUNC(e->devid),803e->flags,804PCI_BUS(e->ext >> 8),805PCI_SLOT(e->ext >> 8),806PCI_FUNC(e->ext >> 8));807808devid = e->devid;809devid_to = e->ext >> 8;810set_dev_entry_from_acpi(iommu, devid , e->flags, 0);811set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);812amd_iommu_alias_table[devid] = devid_to;813break;814case IVHD_DEV_ALIAS_RANGE:815816DUMP_printk(" DEV_ALIAS_RANGE\t\t "817"devid: %02x:%02x.%x flags: %02x "818"devid_to: %02x:%02x.%x\n",819PCI_BUS(e->devid),820PCI_SLOT(e->devid),821PCI_FUNC(e->devid),822e->flags,823PCI_BUS(e->ext >> 8),824PCI_SLOT(e->ext >> 8),825PCI_FUNC(e->ext >> 8));826827devid_start = e->devid;828flags = e->flags;829devid_to = e->ext >> 8;830ext_flags = 0;831alias = true;832break;833case IVHD_DEV_EXT_SELECT:834835DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "836"flags: %02x ext: %08x\n",837PCI_BUS(e->devid),838PCI_SLOT(e->devid),839PCI_FUNC(e->devid),840e->flags, e->ext);841842devid = e->devid;843set_dev_entry_from_acpi(iommu, devid, e->flags,844e->ext);845break;846case IVHD_DEV_EXT_SELECT_RANGE:847848DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "849"%02x:%02x.%x flags: %02x ext: %08x\n",850PCI_BUS(e->devid),851PCI_SLOT(e->devid),852PCI_FUNC(e->devid),853e->flags, e->ext);854855devid_start = e->devid;856flags = e->flags;857ext_flags = e->ext;858alias = false;859break;860case IVHD_DEV_RANGE_END:861862DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",863PCI_BUS(e->devid),864PCI_SLOT(e->devid),865PCI_FUNC(e->devid));866867devid = e->devid;868for (dev_i = devid_start; dev_i <= devid; ++dev_i) {869if (alias) {870amd_iommu_alias_table[dev_i] = devid_to;871set_dev_entry_from_acpi(iommu,872devid_to, flags, ext_flags);873}874set_dev_entry_from_acpi(iommu, dev_i,875flags, ext_flags);876}877break;878default:879break;880}881882p += ivhd_entry_length(p);883}884}885886/* Initializes the device->iommu mapping for the driver */887static int __init init_iommu_devices(struct amd_iommu *iommu)888{889u32 i;890891for (i = iommu->first_device; i <= iommu->last_device; ++i)892set_iommu_for_device(iommu, i);893894return 0;895}896897static void __init free_iommu_one(struct amd_iommu *iommu)898{899free_command_buffer(iommu);900free_event_buffer(iommu);901iommu_unmap_mmio_space(iommu);902}903904static void __init free_iommu_all(void)905{906struct amd_iommu *iommu, *next;907908for_each_iommu_safe(iommu, next) {909list_del(&iommu->list);910free_iommu_one(iommu);911kfree(iommu);912}913}914915/*916* This function clues the initialization function for one IOMMU917* together and also allocates the command buffer and programs the918* hardware. It does NOT enable the IOMMU. This is done afterwards.919*/920static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)921{922spin_lock_init(&iommu->lock);923924/* Add IOMMU to internal data structures */925list_add_tail(&iommu->list, &amd_iommu_list);926iommu->index = amd_iommus_present++;927928if (unlikely(iommu->index >= MAX_IOMMUS)) {929WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");930return -ENOSYS;931}932933/* Index is fine - add IOMMU to the array */934amd_iommus[iommu->index] = iommu;935936/*937* Copy data from ACPI table entry to the iommu struct938*/939iommu->dev = pci_get_bus_and_slot(PCI_BUS(h->devid), h->devid & 0xff);940if (!iommu->dev)941return 1;942943iommu->cap_ptr = h->cap_ptr;944iommu->pci_seg = h->pci_seg;945iommu->mmio_phys = h->mmio_phys;946iommu->mmio_base = iommu_map_mmio_space(h->mmio_phys);947if (!iommu->mmio_base)948return -ENOMEM;949950iommu->cmd_buf = alloc_command_buffer(iommu);951if (!iommu->cmd_buf)952return -ENOMEM;953954iommu->evt_buf = alloc_event_buffer(iommu);955if (!iommu->evt_buf)956return -ENOMEM;957958iommu->int_enabled = false;959960init_iommu_from_pci(iommu);961init_iommu_from_acpi(iommu, h);962init_iommu_devices(iommu);963964if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))965amd_iommu_np_cache = true;966967return pci_enable_device(iommu->dev);968}969970/*971* Iterates over all IOMMU entries in the ACPI table, allocates the972* IOMMU structure and initializes it with init_iommu_one()973*/974static int __init init_iommu_all(struct acpi_table_header *table)975{976u8 *p = (u8 *)table, *end = (u8 *)table;977struct ivhd_header *h;978struct amd_iommu *iommu;979int ret;980981end += table->length;982p += IVRS_HEADER_LENGTH;983984while (p < end) {985h = (struct ivhd_header *)p;986switch (*p) {987case ACPI_IVHD_TYPE:988989DUMP_printk("device: %02x:%02x.%01x cap: %04x "990"seg: %d flags: %01x info %04x\n",991PCI_BUS(h->devid), PCI_SLOT(h->devid),992PCI_FUNC(h->devid), h->cap_ptr,993h->pci_seg, h->flags, h->info);994DUMP_printk(" mmio-addr: %016llx\n",995h->mmio_phys);996997iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);998if (iommu == NULL) {999amd_iommu_init_err = -ENOMEM;1000return 0;1001}10021003ret = init_iommu_one(iommu, h);1004if (ret) {1005amd_iommu_init_err = ret;1006return 0;1007}1008break;1009default:1010break;1011}1012p += h->length;10131014}1015WARN_ON(p != end);10161017return 0;1018}10191020/****************************************************************************1021*1022* The following functions initialize the MSI interrupts for all IOMMUs1023* in the system. Its a bit challenging because there could be multiple1024* IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per1025* pci_dev.1026*1027****************************************************************************/10281029static int iommu_setup_msi(struct amd_iommu *iommu)1030{1031int r;10321033if (pci_enable_msi(iommu->dev))1034return 1;10351036r = request_threaded_irq(iommu->dev->irq,1037amd_iommu_int_handler,1038amd_iommu_int_thread,10390, "AMD-Vi",1040iommu->dev);10411042if (r) {1043pci_disable_msi(iommu->dev);1044return 1;1045}10461047iommu->int_enabled = true;1048iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);10491050return 0;1051}10521053static int iommu_init_msi(struct amd_iommu *iommu)1054{1055if (iommu->int_enabled)1056return 0;10571058if (pci_find_capability(iommu->dev, PCI_CAP_ID_MSI))1059return iommu_setup_msi(iommu);10601061return 1;1062}10631064/****************************************************************************1065*1066* The next functions belong to the third pass of parsing the ACPI1067* table. In this last pass the memory mapping requirements are1068* gathered (like exclusion and unity mapping reanges).1069*1070****************************************************************************/10711072static void __init free_unity_maps(void)1073{1074struct unity_map_entry *entry, *next;10751076list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {1077list_del(&entry->list);1078kfree(entry);1079}1080}10811082/* called when we find an exclusion range definition in ACPI */1083static int __init init_exclusion_range(struct ivmd_header *m)1084{1085int i;10861087switch (m->type) {1088case ACPI_IVMD_TYPE:1089set_device_exclusion_range(m->devid, m);1090break;1091case ACPI_IVMD_TYPE_ALL:1092for (i = 0; i <= amd_iommu_last_bdf; ++i)1093set_device_exclusion_range(i, m);1094break;1095case ACPI_IVMD_TYPE_RANGE:1096for (i = m->devid; i <= m->aux; ++i)1097set_device_exclusion_range(i, m);1098break;1099default:1100break;1101}11021103return 0;1104}11051106/* called for unity map ACPI definition */1107static int __init init_unity_map_range(struct ivmd_header *m)1108{1109struct unity_map_entry *e = 0;1110char *s;11111112e = kzalloc(sizeof(*e), GFP_KERNEL);1113if (e == NULL)1114return -ENOMEM;11151116switch (m->type) {1117default:1118kfree(e);1119return 0;1120case ACPI_IVMD_TYPE:1121s = "IVMD_TYPEi\t\t\t";1122e->devid_start = e->devid_end = m->devid;1123break;1124case ACPI_IVMD_TYPE_ALL:1125s = "IVMD_TYPE_ALL\t\t";1126e->devid_start = 0;1127e->devid_end = amd_iommu_last_bdf;1128break;1129case ACPI_IVMD_TYPE_RANGE:1130s = "IVMD_TYPE_RANGE\t\t";1131e->devid_start = m->devid;1132e->devid_end = m->aux;1133break;1134}1135e->address_start = PAGE_ALIGN(m->range_start);1136e->address_end = e->address_start + PAGE_ALIGN(m->range_length);1137e->prot = m->flags >> 1;11381139DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"1140" range_start: %016llx range_end: %016llx flags: %x\n", s,1141PCI_BUS(e->devid_start), PCI_SLOT(e->devid_start),1142PCI_FUNC(e->devid_start), PCI_BUS(e->devid_end),1143PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),1144e->address_start, e->address_end, m->flags);11451146list_add_tail(&e->list, &amd_iommu_unity_map);11471148return 0;1149}11501151/* iterates over all memory definitions we find in the ACPI table */1152static int __init init_memory_definitions(struct acpi_table_header *table)1153{1154u8 *p = (u8 *)table, *end = (u8 *)table;1155struct ivmd_header *m;11561157end += table->length;1158p += IVRS_HEADER_LENGTH;11591160while (p < end) {1161m = (struct ivmd_header *)p;1162if (m->flags & IVMD_FLAG_EXCL_RANGE)1163init_exclusion_range(m);1164else if (m->flags & IVMD_FLAG_UNITY_MAP)1165init_unity_map_range(m);11661167p += m->length;1168}11691170return 0;1171}11721173/*1174* Init the device table to not allow DMA access for devices and1175* suppress all page faults1176*/1177static void init_device_table(void)1178{1179u32 devid;11801181for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {1182set_dev_entry_bit(devid, DEV_ENTRY_VALID);1183set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);1184}1185}11861187static void iommu_init_flags(struct amd_iommu *iommu)1188{1189iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?1190iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :1191iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);11921193iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?1194iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :1195iommu_feature_disable(iommu, CONTROL_PASSPW_EN);11961197iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?1198iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :1199iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);12001201iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?1202iommu_feature_enable(iommu, CONTROL_ISOC_EN) :1203iommu_feature_disable(iommu, CONTROL_ISOC_EN);12041205/*1206* make IOMMU memory accesses cache coherent1207*/1208iommu_feature_enable(iommu, CONTROL_COHERENT_EN);1209}12101211static void iommu_apply_resume_quirks(struct amd_iommu *iommu)1212{1213int i, j;1214u32 ioc_feature_control;1215struct pci_dev *pdev = NULL;12161217/* RD890 BIOSes may not have completely reconfigured the iommu */1218if (!is_rd890_iommu(iommu->dev))1219return;12201221/*1222* First, we need to ensure that the iommu is enabled. This is1223* controlled by a register in the northbridge1224*/1225pdev = pci_get_bus_and_slot(iommu->dev->bus->number, PCI_DEVFN(0, 0));12261227if (!pdev)1228return;12291230/* Select Northbridge indirect register 0x75 and enable writing */1231pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));1232pci_read_config_dword(pdev, 0x64, &ioc_feature_control);12331234/* Enable the iommu */1235if (!(ioc_feature_control & 0x1))1236pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);12371238pci_dev_put(pdev);12391240/* Restore the iommu BAR */1241pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,1242iommu->stored_addr_lo);1243pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,1244iommu->stored_addr_hi);12451246/* Restore the l1 indirect regs for each of the 6 l1s */1247for (i = 0; i < 6; i++)1248for (j = 0; j < 0x12; j++)1249iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);12501251/* Restore the l2 indirect regs */1252for (i = 0; i < 0x83; i++)1253iommu_write_l2(iommu, i, iommu->stored_l2[i]);12541255/* Lock PCI setup registers */1256pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,1257iommu->stored_addr_lo | 1);1258}12591260/*1261* This function finally enables all IOMMUs found in the system after1262* they have been initialized1263*/1264static void enable_iommus(void)1265{1266struct amd_iommu *iommu;12671268for_each_iommu(iommu) {1269iommu_disable(iommu);1270iommu_init_flags(iommu);1271iommu_set_device_table(iommu);1272iommu_enable_command_buffer(iommu);1273iommu_enable_event_buffer(iommu);1274iommu_set_exclusion_range(iommu);1275iommu_init_msi(iommu);1276iommu_enable(iommu);1277iommu_flush_all_caches(iommu);1278}1279}12801281static void disable_iommus(void)1282{1283struct amd_iommu *iommu;12841285for_each_iommu(iommu)1286iommu_disable(iommu);1287}12881289/*1290* Suspend/Resume support1291* disable suspend until real resume implemented1292*/12931294static void amd_iommu_resume(void)1295{1296struct amd_iommu *iommu;12971298for_each_iommu(iommu)1299iommu_apply_resume_quirks(iommu);13001301/* re-load the hardware */1302enable_iommus();13031304/*1305* we have to flush after the IOMMUs are enabled because a1306* disabled IOMMU will never execute the commands we send1307*/1308for_each_iommu(iommu)1309iommu_flush_all_caches(iommu);1310}13111312static int amd_iommu_suspend(void)1313{1314/* disable IOMMUs to go out of the way for BIOS */1315disable_iommus();13161317return 0;1318}13191320static struct syscore_ops amd_iommu_syscore_ops = {1321.suspend = amd_iommu_suspend,1322.resume = amd_iommu_resume,1323};13241325/*1326* This is the core init function for AMD IOMMU hardware in the system.1327* This function is called from the generic x86 DMA layer initialization1328* code.1329*1330* This function basically parses the ACPI table for AMD IOMMU (IVRS)1331* three times:1332*1333* 1 pass) Find the highest PCI device id the driver has to handle.1334* Upon this information the size of the data structures is1335* determined that needs to be allocated.1336*1337* 2 pass) Initialize the data structures just allocated with the1338* information in the ACPI table about available AMD IOMMUs1339* in the system. It also maps the PCI devices in the1340* system to specific IOMMUs1341*1342* 3 pass) After the basic data structures are allocated and1343* initialized we update them with information about memory1344* remapping requirements parsed out of the ACPI table in1345* this last pass.1346*1347* After that the hardware is initialized and ready to go. In the last1348* step we do some Linux specific things like registering the driver in1349* the dma_ops interface and initializing the suspend/resume support1350* functions. Finally it prints some information about AMD IOMMUs and1351* the driver state and enables the hardware.1352*/1353static int __init amd_iommu_init(void)1354{1355int i, ret = 0;13561357/*1358* First parse ACPI tables to find the largest Bus/Dev/Func1359* we need to handle. Upon this information the shared data1360* structures for the IOMMUs in the system will be allocated1361*/1362if (acpi_table_parse("IVRS", find_last_devid_acpi) != 0)1363return -ENODEV;13641365ret = amd_iommu_init_err;1366if (ret)1367goto out;13681369dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);1370alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);1371rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);13721373ret = -ENOMEM;13741375/* Device table - directly used by all IOMMUs */1376amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,1377get_order(dev_table_size));1378if (amd_iommu_dev_table == NULL)1379goto out;13801381/*1382* Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the1383* IOMMU see for that device1384*/1385amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,1386get_order(alias_table_size));1387if (amd_iommu_alias_table == NULL)1388goto free;13891390/* IOMMU rlookup table - find the IOMMU for a specific device */1391amd_iommu_rlookup_table = (void *)__get_free_pages(1392GFP_KERNEL | __GFP_ZERO,1393get_order(rlookup_table_size));1394if (amd_iommu_rlookup_table == NULL)1395goto free;13961397amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(1398GFP_KERNEL | __GFP_ZERO,1399get_order(MAX_DOMAIN_ID/8));1400if (amd_iommu_pd_alloc_bitmap == NULL)1401goto free;14021403/* init the device table */1404init_device_table();14051406/*1407* let all alias entries point to itself1408*/1409for (i = 0; i <= amd_iommu_last_bdf; ++i)1410amd_iommu_alias_table[i] = i;14111412/*1413* never allocate domain 0 because its used as the non-allocated and1414* error value placeholder1415*/1416amd_iommu_pd_alloc_bitmap[0] = 1;14171418spin_lock_init(&amd_iommu_pd_lock);14191420/*1421* now the data structures are allocated and basically initialized1422* start the real acpi table scan1423*/1424ret = -ENODEV;1425if (acpi_table_parse("IVRS", init_iommu_all) != 0)1426goto free;14271428if (amd_iommu_init_err) {1429ret = amd_iommu_init_err;1430goto free;1431}14321433if (acpi_table_parse("IVRS", init_memory_definitions) != 0)1434goto free;14351436if (amd_iommu_init_err) {1437ret = amd_iommu_init_err;1438goto free;1439}14401441ret = amd_iommu_init_devices();1442if (ret)1443goto free;14441445enable_iommus();14461447if (iommu_pass_through)1448ret = amd_iommu_init_passthrough();1449else1450ret = amd_iommu_init_dma_ops();14511452if (ret)1453goto free_disable;14541455amd_iommu_init_api();14561457amd_iommu_init_notifier();14581459register_syscore_ops(&amd_iommu_syscore_ops);14601461if (iommu_pass_through)1462goto out;14631464if (amd_iommu_unmap_flush)1465printk(KERN_INFO "AMD-Vi: IO/TLB flush on unmap enabled\n");1466else1467printk(KERN_INFO "AMD-Vi: Lazy IO/TLB flushing enabled\n");14681469x86_platform.iommu_shutdown = disable_iommus;1470out:1471return ret;14721473free_disable:1474disable_iommus();14751476free:1477amd_iommu_uninit_devices();14781479free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,1480get_order(MAX_DOMAIN_ID/8));14811482free_pages((unsigned long)amd_iommu_rlookup_table,1483get_order(rlookup_table_size));14841485free_pages((unsigned long)amd_iommu_alias_table,1486get_order(alias_table_size));14871488free_pages((unsigned long)amd_iommu_dev_table,1489get_order(dev_table_size));14901491free_iommu_all();14921493free_unity_maps();14941495#ifdef CONFIG_GART_IOMMU1496/*1497* We failed to initialize the AMD IOMMU - try fallback to GART1498* if possible.1499*/1500gart_iommu_init();15011502#endif15031504goto out;1505}15061507/****************************************************************************1508*1509* Early detect code. This code runs at IOMMU detection time in the DMA1510* layer. It just looks if there is an IVRS ACPI table to detect AMD1511* IOMMUs1512*1513****************************************************************************/1514static int __init early_amd_iommu_detect(struct acpi_table_header *table)1515{1516return 0;1517}15181519int __init amd_iommu_detect(void)1520{1521if (no_iommu || (iommu_detected && !gart_iommu_aperture))1522return -ENODEV;15231524if (amd_iommu_disabled)1525return -ENODEV;15261527if (acpi_table_parse("IVRS", early_amd_iommu_detect) == 0) {1528iommu_detected = 1;1529amd_iommu_detected = 1;1530x86_init.iommu.iommu_init = amd_iommu_init;15311532/* Make sure ACS will be enabled */1533pci_request_acs();1534return 1;1535}1536return -ENODEV;1537}15381539/****************************************************************************1540*1541* Parsing functions for the AMD IOMMU specific kernel command line1542* options.1543*1544****************************************************************************/15451546static int __init parse_amd_iommu_dump(char *str)1547{1548amd_iommu_dump = true;15491550return 1;1551}15521553static int __init parse_amd_iommu_options(char *str)1554{1555for (; *str; ++str) {1556if (strncmp(str, "fullflush", 9) == 0)1557amd_iommu_unmap_flush = true;1558if (strncmp(str, "off", 3) == 0)1559amd_iommu_disabled = true;1560}15611562return 1;1563}15641565__setup("amd_iommu_dump", parse_amd_iommu_dump);1566__setup("amd_iommu=", parse_amd_iommu_options);15671568IOMMU_INIT_FINISH(amd_iommu_detect,1569gart_iommu_hole_init,15700,15710);157215731574