blob: 156ab4b0f4bdc7e5f37c12a3d91bbaf422c8080f [file] [log] [blame]
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
21#include <linux/acpi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020022#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010024#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020025#include <linux/interrupt.h>
26#include <linux/msi.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020027#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010028#include <linux/export.h>
Alex Williamson066f2e92014-06-12 16:12:37 -060029#include <linux/iommu.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020030#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090031#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010032#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090033#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040034#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020035#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020036#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020037
38#include "amd_iommu_proto.h"
39#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020040#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020041
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020042/*
43 * definitions for the ACPI scanning code
44 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020045#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020046
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040047#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020048#define ACPI_IVMD_TYPE_ALL 0x20
49#define ACPI_IVMD_TYPE 0x21
50#define ACPI_IVMD_TYPE_RANGE 0x22
51
52#define IVHD_DEV_ALL 0x01
53#define IVHD_DEV_SELECT 0x02
54#define IVHD_DEV_SELECT_RANGE_START 0x03
55#define IVHD_DEV_RANGE_END 0x04
56#define IVHD_DEV_ALIAS 0x42
57#define IVHD_DEV_ALIAS_RANGE 0x43
58#define IVHD_DEV_EXT_SELECT 0x46
59#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020060#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040061#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020062
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040063#define UID_NOT_PRESENT 0
64#define UID_IS_INTEGER 1
65#define UID_IS_CHARACTER 2
66
Joerg Roedel6efed632012-06-14 15:52:58 +020067#define IVHD_SPECIAL_IOAPIC 1
68#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020069
Joerg Roedel6da73422009-05-04 11:44:38 +020070#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
71#define IVHD_FLAG_PASSPW_EN_MASK 0x02
72#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
73#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020074
75#define IVMD_FLAG_EXCL_RANGE 0x08
76#define IVMD_FLAG_UNITY_MAP 0x01
77
78#define ACPI_DEVFLAG_INITPASS 0x01
79#define ACPI_DEVFLAG_EXTINT 0x02
80#define ACPI_DEVFLAG_NMI 0x04
81#define ACPI_DEVFLAG_SYSMGT1 0x10
82#define ACPI_DEVFLAG_SYSMGT2 0x20
83#define ACPI_DEVFLAG_LINT0 0x40
84#define ACPI_DEVFLAG_LINT1 0x80
85#define ACPI_DEVFLAG_ATSDIS 0x10000000
86
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050087#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020088/*
89 * ACPI table definitions
90 *
91 * These data structures are laid over the table to parse the important values
92 * out of it.
93 */
94
95/*
96 * structure describing one IOMMU in the ACPI table. Typically followed by one
97 * or more ivhd_entrys.
98 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020099struct ivhd_header {
100 u8 type;
101 u8 flags;
102 u16 length;
103 u16 devid;
104 u16 cap_ptr;
105 u64 mmio_phys;
106 u16 pci_seg;
107 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400108 u32 efr_attr;
109
110 /* Following only valid on IVHD type 11h and 40h */
111 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
112 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200113} __attribute__((packed));
114
Joerg Roedelb65233a2008-07-11 17:14:21 +0200115/*
116 * A device entry describing which devices a specific IOMMU translates and
117 * which requestor ids they use.
118 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200119struct ivhd_entry {
120 u8 type;
121 u16 devid;
122 u8 flags;
123 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400124 u32 hidh;
125 u64 cid;
126 u8 uidf;
127 u8 uidl;
128 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200129} __attribute__((packed));
130
Joerg Roedelb65233a2008-07-11 17:14:21 +0200131/*
132 * An AMD IOMMU memory definition structure. It defines things like exclusion
133 * ranges for devices and regions that should be unity mapped.
134 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200135struct ivmd_header {
136 u8 type;
137 u8 flags;
138 u16 length;
139 u16 devid;
140 u16 aux;
141 u64 resv;
142 u64 range_start;
143 u64 range_length;
144} __attribute__((packed));
145
Joerg Roedelfefda112009-05-20 12:21:42 +0200146bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200147bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200148
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500149int amd_iommu_guest_ir;
150
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200151static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200152static bool __initdata amd_iommu_disabled;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400153static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200154
Joerg Roedelb65233a2008-07-11 17:14:21 +0200155u16 amd_iommu_last_bdf; /* largest PCI device id we have
156 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200157LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200158 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700159bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200160
Joerg Roedel2e228472008-07-11 17:14:31 +0200161LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200162 system */
163
Joerg Roedelbb527772009-11-20 14:31:51 +0100164/* Array to assign indices to IOMMUs*/
165struct amd_iommu *amd_iommus[MAX_IOMMUS];
166int amd_iommus_present;
167
Joerg Roedel318afd42009-11-23 18:32:38 +0100168/* IOMMUs have a non-present cache? */
169bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200170bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100171
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600172u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100173
Joerg Roedel400a28a2011-11-28 15:11:02 +0100174bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200175static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100176
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100177bool amd_iommu_force_isolation __read_mostly;
178
Joerg Roedelb65233a2008-07-11 17:14:21 +0200179/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100180 * List of protection domains - used during resume
181 */
182LIST_HEAD(amd_iommu_pd_list);
183spinlock_t amd_iommu_pd_lock;
184
185/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200186 * Pointer to the device table which is shared by all AMD IOMMUs
187 * it is indexed by the PCI device id or the HT unit id and contains
188 * information about the domain the device belongs to as well as the
189 * page table root pointer.
190 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200191struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200192
193/*
194 * The alias table is a driver specific data structure which contains the
195 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
196 * More than one device can share the same requestor id.
197 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200198u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200199
200/*
201 * The rlookup table is used to find the IOMMU which is responsible
202 * for a specific device. It is also indexed by the PCI device id.
203 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200204struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200205
206/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200207 * This table is used to find the irq remapping table for a given device id
208 * quickly.
209 */
210struct irq_remap_table **irq_lookup_table;
211
212/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200213 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200214 * to know which ones are already in use.
215 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200216unsigned long *amd_iommu_pd_alloc_bitmap;
217
Joerg Roedelb65233a2008-07-11 17:14:21 +0200218static u32 dev_table_size; /* size of the device table */
219static u32 alias_table_size; /* size of the alias table */
220static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200221
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200222enum iommu_init_state {
223 IOMMU_START_STATE,
224 IOMMU_IVRS_DETECTED,
225 IOMMU_ACPI_FINISHED,
226 IOMMU_ENABLED,
227 IOMMU_PCI_INIT,
228 IOMMU_INTERRUPTS_EN,
229 IOMMU_DMA_OPS,
230 IOMMU_INITIALIZED,
231 IOMMU_NOT_FOUND,
232 IOMMU_INIT_ERROR,
233};
234
Joerg Roedel235dacb2013-04-09 17:53:14 +0200235/* Early ioapic and hpet maps from kernel command line */
236#define EARLY_MAP_SIZE 4
237static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
238static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400239static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
240
Joerg Roedel235dacb2013-04-09 17:53:14 +0200241static int __initdata early_ioapic_map_size;
242static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400243static int __initdata early_acpihid_map_size;
244
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200245static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200246
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200247static enum iommu_init_state init_state = IOMMU_START_STATE;
248
Gerard Snitselaarae295142012-03-16 11:38:22 -0700249static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200250static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200251static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100252
Suravee Suthikulpanit38e45d022016-02-23 13:03:30 +0100253static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu,
254 u8 bank, u8 cntr, u8 fxn,
255 u64 *value, bool is_write);
256
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200257static inline void update_last_devid(u16 devid)
258{
259 if (devid > amd_iommu_last_bdf)
260 amd_iommu_last_bdf = devid;
261}
262
Joerg Roedelc5714842008-07-11 17:14:25 +0200263static inline unsigned long tbl_size(int entry_size)
264{
265 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100266 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200267
268 return 1UL << shift;
269}
270
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400271/* Access to l1 and l2 indexed register spaces */
272
273static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
274{
275 u32 val;
276
277 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
278 pci_read_config_dword(iommu->dev, 0xfc, &val);
279 return val;
280}
281
282static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
283{
284 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
285 pci_write_config_dword(iommu->dev, 0xfc, val);
286 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
287}
288
289static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
290{
291 u32 val;
292
293 pci_write_config_dword(iommu->dev, 0xf0, address);
294 pci_read_config_dword(iommu->dev, 0xf4, &val);
295 return val;
296}
297
298static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
299{
300 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
301 pci_write_config_dword(iommu->dev, 0xf4, val);
302}
303
Joerg Roedelb65233a2008-07-11 17:14:21 +0200304/****************************************************************************
305 *
306 * AMD IOMMU MMIO register space handling functions
307 *
308 * These functions are used to program the IOMMU device registers in
309 * MMIO space required for that driver.
310 *
311 ****************************************************************************/
312
313/*
314 * This function set the exclusion range in the IOMMU. DMA accesses to the
315 * exclusion range are passed through untranslated
316 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200317static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200318{
319 u64 start = iommu->exclusion_start & PAGE_MASK;
320 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
321 u64 entry;
322
323 if (!iommu->exclusion_start)
324 return;
325
326 entry = start | MMIO_EXCL_ENABLE_MASK;
327 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
328 &entry, sizeof(entry));
329
330 entry = limit;
331 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
332 &entry, sizeof(entry));
333}
334
Joerg Roedelb65233a2008-07-11 17:14:21 +0200335/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000336static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200337{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200338 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200339
340 BUG_ON(iommu->mmio_base == NULL);
341
342 entry = virt_to_phys(amd_iommu_dev_table);
343 entry |= (dev_table_size >> 12) - 1;
344 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
345 &entry, sizeof(entry));
346}
347
Joerg Roedelb65233a2008-07-11 17:14:21 +0200348/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200349static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200350{
351 u32 ctrl;
352
353 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
354 ctrl |= (1 << bit);
355 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
356}
357
Joerg Roedelca0207112009-10-28 18:02:26 +0100358static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200359{
360 u32 ctrl;
361
Joerg Roedel199d0d52008-09-17 16:45:59 +0200362 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200363 ctrl &= ~(1 << bit);
364 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
365}
366
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100367static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
368{
369 u32 ctrl;
370
371 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
372 ctrl &= ~CTRL_INV_TO_MASK;
373 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
374 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
375}
376
Joerg Roedelb65233a2008-07-11 17:14:21 +0200377/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200378static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200379{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200380 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200381}
382
Joerg Roedel92ac4322009-05-19 19:06:27 +0200383static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200384{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200385 /* Disable command buffer */
386 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
387
388 /* Disable event logging and event interrupts */
389 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
390 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
391
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500392 /* Disable IOMMU GA_LOG */
393 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
394 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
395
Chris Wrighta8c485b2009-06-15 15:53:45 +0200396 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200397 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200398}
399
Joerg Roedelb65233a2008-07-11 17:14:21 +0200400/*
401 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
402 * the system has one.
403 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500404static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200405{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500406 if (!request_mem_region(address, end, "amd_iommu")) {
407 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
408 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200409 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200410 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200411 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200412
Steven L Kinney30861dd2013-06-05 16:11:48 -0500413 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200414}
415
416static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
417{
418 if (iommu->mmio_base)
419 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500420 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200421}
422
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400423static inline u32 get_ivhd_header_size(struct ivhd_header *h)
424{
425 u32 size = 0;
426
427 switch (h->type) {
428 case 0x10:
429 size = 24;
430 break;
431 case 0x11:
432 case 0x40:
433 size = 40;
434 break;
435 }
436 return size;
437}
438
Joerg Roedelb65233a2008-07-11 17:14:21 +0200439/****************************************************************************
440 *
441 * The functions below belong to the first pass of AMD IOMMU ACPI table
442 * parsing. In this pass we try to find out the highest device id this
443 * code has to handle. Upon this information the size of the shared data
444 * structures is determined later.
445 *
446 ****************************************************************************/
447
448/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200449 * This function calculates the length of a given IVHD entry
450 */
451static inline int ivhd_entry_length(u8 *ivhd)
452{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400453 u32 type = ((struct ivhd_entry *)ivhd)->type;
454
455 if (type < 0x80) {
456 return 0x04 << (*ivhd >> 6);
457 } else if (type == IVHD_DEV_ACPI_HID) {
458 /* For ACPI_HID, offset 21 is uid len */
459 return *((u8 *)ivhd + 21) + 22;
460 }
461 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200462}
463
464/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200465 * After reading the highest device id from the IOMMU PCI capability header
466 * this function looks if there is a higher device id defined in the ACPI table
467 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200468static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
469{
470 u8 *p = (void *)h, *end = (void *)h;
471 struct ivhd_entry *dev;
472
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400473 u32 ivhd_size = get_ivhd_header_size(h);
474
475 if (!ivhd_size) {
476 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
477 return -EINVAL;
478 }
479
480 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200481 end += h->length;
482
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200483 while (p < end) {
484 dev = (struct ivhd_entry *)p;
485 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200486 case IVHD_DEV_ALL:
487 /* Use maximum BDF value for DEV_ALL */
488 update_last_devid(0xffff);
489 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200490 case IVHD_DEV_SELECT:
491 case IVHD_DEV_RANGE_END:
492 case IVHD_DEV_ALIAS:
493 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200494 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200495 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200496 break;
497 default:
498 break;
499 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200500 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200501 }
502
503 WARN_ON(p != end);
504
505 return 0;
506}
507
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400508static int __init check_ivrs_checksum(struct acpi_table_header *table)
509{
510 int i;
511 u8 checksum = 0, *p = (u8 *)table;
512
513 for (i = 0; i < table->length; ++i)
514 checksum += p[i];
515 if (checksum != 0) {
516 /* ACPI table corrupt */
517 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
518 return -ENODEV;
519 }
520
521 return 0;
522}
523
Joerg Roedelb65233a2008-07-11 17:14:21 +0200524/*
525 * Iterate over all IVHD entries in the ACPI table and find the highest device
526 * id which we need to handle. This is the first of three functions which parse
527 * the ACPI table. So we check the checksum here.
528 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200529static int __init find_last_devid_acpi(struct acpi_table_header *table)
530{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400531 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200532 struct ivhd_header *h;
533
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200534 p += IVRS_HEADER_LENGTH;
535
536 end += table->length;
537 while (p < end) {
538 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400539 if (h->type == amd_iommu_target_ivhd_type) {
540 int ret = find_last_devid_from_ivhd(h);
541
542 if (ret)
543 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200544 }
545 p += h->length;
546 }
547 WARN_ON(p != end);
548
549 return 0;
550}
551
Joerg Roedelb65233a2008-07-11 17:14:21 +0200552/****************************************************************************
553 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200554 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200555 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
556 * data structures, initialize the device/alias/rlookup table and also
557 * basically initialize the hardware.
558 *
559 ****************************************************************************/
560
561/*
562 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
563 * write commands to that buffer later and the IOMMU will execute them
564 * asynchronously
565 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200566static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200567{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200568 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
569 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200570
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200571 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200572}
573
574/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200575 * This function resets the command buffer if the IOMMU stopped fetching
576 * commands from it.
577 */
578void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
579{
580 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
581
582 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
583 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
584
585 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
586}
587
588/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200589 * This function writes the command buffer address to the hardware and
590 * enables it.
591 */
592static void iommu_enable_command_buffer(struct amd_iommu *iommu)
593{
594 u64 entry;
595
596 BUG_ON(iommu->cmd_buf == NULL);
597
598 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200599 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200600
Joerg Roedelb36ca912008-06-26 21:27:45 +0200601 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200602 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200603
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200604 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200605}
606
607static void __init free_command_buffer(struct amd_iommu *iommu)
608{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200609 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200610}
611
Joerg Roedel335503e2008-09-05 14:29:07 +0200612/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200613static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200614{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200615 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
616 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200617
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200618 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200619}
620
621static void iommu_enable_event_buffer(struct amd_iommu *iommu)
622{
623 u64 entry;
624
625 BUG_ON(iommu->evt_buf == NULL);
626
Joerg Roedel335503e2008-09-05 14:29:07 +0200627 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200628
Joerg Roedel335503e2008-09-05 14:29:07 +0200629 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
630 &entry, sizeof(entry));
631
Joerg Roedel090672072009-06-15 16:06:48 +0200632 /* set head and tail to zero manually */
633 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
634 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
635
Joerg Roedel58492e12009-05-04 18:41:16 +0200636 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200637}
638
639static void __init free_event_buffer(struct amd_iommu *iommu)
640{
641 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
642}
643
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100644/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200645static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100646{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200647 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
648 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100649
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200650 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100651}
652
653static void iommu_enable_ppr_log(struct amd_iommu *iommu)
654{
655 u64 entry;
656
657 if (iommu->ppr_log == NULL)
658 return;
659
660 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
661
662 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
663 &entry, sizeof(entry));
664
665 /* set head and tail to zero manually */
666 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
667 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
668
669 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
670 iommu_feature_enable(iommu, CONTROL_PPR_EN);
671}
672
673static void __init free_ppr_log(struct amd_iommu *iommu)
674{
675 if (iommu->ppr_log == NULL)
676 return;
677
678 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
679}
680
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500681static void free_ga_log(struct amd_iommu *iommu)
682{
683#ifdef CONFIG_IRQ_REMAP
684 if (iommu->ga_log)
685 free_pages((unsigned long)iommu->ga_log,
686 get_order(GA_LOG_SIZE));
687 if (iommu->ga_log_tail)
688 free_pages((unsigned long)iommu->ga_log_tail,
689 get_order(8));
690#endif
691}
692
693static int iommu_ga_log_enable(struct amd_iommu *iommu)
694{
695#ifdef CONFIG_IRQ_REMAP
696 u32 status, i;
697
698 if (!iommu->ga_log)
699 return -EINVAL;
700
701 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
702
703 /* Check if already running */
704 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
705 return 0;
706
707 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
708 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
709
710 for (i = 0; i < LOOP_TIMEOUT; ++i) {
711 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
712 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
713 break;
714 }
715
716 if (i >= LOOP_TIMEOUT)
717 return -EINVAL;
718#endif /* CONFIG_IRQ_REMAP */
719 return 0;
720}
721
722#ifdef CONFIG_IRQ_REMAP
723static int iommu_init_ga_log(struct amd_iommu *iommu)
724{
725 u64 entry;
726
727 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
728 return 0;
729
730 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
731 get_order(GA_LOG_SIZE));
732 if (!iommu->ga_log)
733 goto err_out;
734
735 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
736 get_order(8));
737 if (!iommu->ga_log_tail)
738 goto err_out;
739
740 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
741 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
742 &entry, sizeof(entry));
743 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
744 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
745 &entry, sizeof(entry));
746 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
747 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
748
749 return 0;
750err_out:
751 free_ga_log(iommu);
752 return -EINVAL;
753}
754#endif /* CONFIG_IRQ_REMAP */
755
756static int iommu_init_ga(struct amd_iommu *iommu)
757{
758 int ret = 0;
759
760#ifdef CONFIG_IRQ_REMAP
761 /* Note: We have already checked GASup from IVRS table.
762 * Now, we need to make sure that GAMSup is set.
763 */
764 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
765 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
766 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
767
768 ret = iommu_init_ga_log(iommu);
769#endif /* CONFIG_IRQ_REMAP */
770
771 return ret;
772}
773
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100774static void iommu_enable_gt(struct amd_iommu *iommu)
775{
776 if (!iommu_feature(iommu, FEATURE_GT))
777 return;
778
779 iommu_feature_enable(iommu, CONTROL_GT_EN);
780}
781
Joerg Roedelb65233a2008-07-11 17:14:21 +0200782/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200783static void set_dev_entry_bit(u16 devid, u8 bit)
784{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100785 int i = (bit >> 6) & 0x03;
786 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200787
Joerg Roedelee6c2862011-11-09 12:06:03 +0100788 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200789}
790
Joerg Roedelc5cca142009-10-09 18:31:20 +0200791static int get_dev_entry_bit(u16 devid, u8 bit)
792{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100793 int i = (bit >> 6) & 0x03;
794 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200795
Joerg Roedelee6c2862011-11-09 12:06:03 +0100796 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200797}
798
799
800void amd_iommu_apply_erratum_63(u16 devid)
801{
802 int sysmgt;
803
804 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
805 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
806
807 if (sysmgt == 0x01)
808 set_dev_entry_bit(devid, DEV_ENTRY_IW);
809}
810
Joerg Roedel5ff47892008-07-14 20:11:18 +0200811/* Writes the specific IOMMU for a device into the rlookup table */
812static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
813{
814 amd_iommu_rlookup_table[devid] = iommu;
815}
816
Joerg Roedelb65233a2008-07-11 17:14:21 +0200817/*
818 * This function takes the device specific flags read from the ACPI
819 * table and sets up the device table entry with that information
820 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200821static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
822 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200823{
824 if (flags & ACPI_DEVFLAG_INITPASS)
825 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
826 if (flags & ACPI_DEVFLAG_EXTINT)
827 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
828 if (flags & ACPI_DEVFLAG_NMI)
829 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
830 if (flags & ACPI_DEVFLAG_SYSMGT1)
831 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
832 if (flags & ACPI_DEVFLAG_SYSMGT2)
833 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
834 if (flags & ACPI_DEVFLAG_LINT0)
835 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
836 if (flags & ACPI_DEVFLAG_LINT1)
837 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200838
Joerg Roedelc5cca142009-10-09 18:31:20 +0200839 amd_iommu_apply_erratum_63(devid);
840
Joerg Roedel5ff47892008-07-14 20:11:18 +0200841 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200842}
843
Joerg Roedelc50e3242014-09-09 15:59:37 +0200844static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200845{
846 struct devid_map *entry;
847 struct list_head *list;
848
Joerg Roedel31cff672013-04-09 16:53:58 +0200849 if (type == IVHD_SPECIAL_IOAPIC)
850 list = &ioapic_map;
851 else if (type == IVHD_SPECIAL_HPET)
852 list = &hpet_map;
853 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200854 return -EINVAL;
855
Joerg Roedel31cff672013-04-09 16:53:58 +0200856 list_for_each_entry(entry, list, list) {
857 if (!(entry->id == id && entry->cmd_line))
858 continue;
859
860 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
861 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
862
Joerg Roedelc50e3242014-09-09 15:59:37 +0200863 *devid = entry->devid;
864
Joerg Roedel31cff672013-04-09 16:53:58 +0200865 return 0;
866 }
867
Joerg Roedel6efed632012-06-14 15:52:58 +0200868 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
869 if (!entry)
870 return -ENOMEM;
871
Joerg Roedel31cff672013-04-09 16:53:58 +0200872 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +0200873 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +0200874 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +0200875
876 list_add_tail(&entry->list, list);
877
878 return 0;
879}
880
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400881static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
882 bool cmd_line)
883{
884 struct acpihid_map_entry *entry;
885 struct list_head *list = &acpihid_map;
886
887 list_for_each_entry(entry, list, list) {
888 if (strcmp(entry->hid, hid) ||
889 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
890 !entry->cmd_line)
891 continue;
892
893 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
894 hid, uid);
895 *devid = entry->devid;
896 return 0;
897 }
898
899 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
900 if (!entry)
901 return -ENOMEM;
902
903 memcpy(entry->uid, uid, strlen(uid));
904 memcpy(entry->hid, hid, strlen(hid));
905 entry->devid = *devid;
906 entry->cmd_line = cmd_line;
907 entry->root_devid = (entry->devid & (~0x7));
908
909 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
910 entry->cmd_line ? "cmd" : "ivrs",
911 entry->hid, entry->uid, entry->root_devid);
912
913 list_add_tail(&entry->list, list);
914 return 0;
915}
916
Joerg Roedel235dacb2013-04-09 17:53:14 +0200917static int __init add_early_maps(void)
918{
919 int i, ret;
920
921 for (i = 0; i < early_ioapic_map_size; ++i) {
922 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
923 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200924 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200925 early_ioapic_map[i].cmd_line);
926 if (ret)
927 return ret;
928 }
929
930 for (i = 0; i < early_hpet_map_size; ++i) {
931 ret = add_special_device(IVHD_SPECIAL_HPET,
932 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200933 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200934 early_hpet_map[i].cmd_line);
935 if (ret)
936 return ret;
937 }
938
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400939 for (i = 0; i < early_acpihid_map_size; ++i) {
940 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
941 early_acpihid_map[i].uid,
942 &early_acpihid_map[i].devid,
943 early_acpihid_map[i].cmd_line);
944 if (ret)
945 return ret;
946 }
947
Joerg Roedel235dacb2013-04-09 17:53:14 +0200948 return 0;
949}
950
Joerg Roedelb65233a2008-07-11 17:14:21 +0200951/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200952 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +0200953 * it
954 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200955static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
956{
957 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
958
959 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
960 return;
961
962 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200963 /*
964 * We only can configure exclusion ranges per IOMMU, not
965 * per device. But we can enable the exclusion range per
966 * device. This is done here
967 */
Su Friendy2c16c9f2014-05-07 13:54:52 +0800968 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +0200969 iommu->exclusion_start = m->range_start;
970 iommu->exclusion_length = m->range_length;
971 }
972}
973
Joerg Roedelb65233a2008-07-11 17:14:21 +0200974/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200975 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
976 * initializes the hardware and our data structures with it.
977 */
Joerg Roedel6efed632012-06-14 15:52:58 +0200978static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200979 struct ivhd_header *h)
980{
981 u8 *p = (u8 *)h;
982 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +0200983 u16 devid = 0, devid_start = 0, devid_to = 0;
984 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200985 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200986 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400987 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200988 int ret;
989
990
991 ret = add_early_maps();
992 if (ret)
993 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200994
995 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +0200996 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200997 */
Joerg Roedele9bf5192010-09-20 14:33:07 +0200998 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200999
1000 /*
1001 * Done. Now parse the device entries
1002 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001003 ivhd_size = get_ivhd_header_size(h);
1004 if (!ivhd_size) {
1005 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1006 return -EINVAL;
1007 }
1008
1009 p += ivhd_size;
1010
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001011 end += h->length;
1012
Joerg Roedel42a698f2009-05-20 15:41:28 +02001013
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001014 while (p < end) {
1015 e = (struct ivhd_entry *)p;
1016 switch (e->type) {
1017 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001018
Joerg Roedel226e8892015-10-20 17:33:44 +02001019 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001020
Joerg Roedel226e8892015-10-20 17:33:44 +02001021 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1022 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001023 break;
1024 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001025
1026 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1027 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001028 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001029 PCI_SLOT(e->devid),
1030 PCI_FUNC(e->devid),
1031 e->flags);
1032
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001033 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001034 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001035 break;
1036 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001037
1038 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1039 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001040 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001041 PCI_SLOT(e->devid),
1042 PCI_FUNC(e->devid),
1043 e->flags);
1044
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001045 devid_start = e->devid;
1046 flags = e->flags;
1047 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001048 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001049 break;
1050 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001051
1052 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1053 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001054 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001055 PCI_SLOT(e->devid),
1056 PCI_FUNC(e->devid),
1057 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001058 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001059 PCI_SLOT(e->ext >> 8),
1060 PCI_FUNC(e->ext >> 8));
1061
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001062 devid = e->devid;
1063 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001064 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001065 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001066 amd_iommu_alias_table[devid] = devid_to;
1067 break;
1068 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001069
1070 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1071 "devid: %02x:%02x.%x flags: %02x "
1072 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001073 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001074 PCI_SLOT(e->devid),
1075 PCI_FUNC(e->devid),
1076 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001077 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001078 PCI_SLOT(e->ext >> 8),
1079 PCI_FUNC(e->ext >> 8));
1080
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001081 devid_start = e->devid;
1082 flags = e->flags;
1083 devid_to = e->ext >> 8;
1084 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001085 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001086 break;
1087 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001088
1089 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1090 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001091 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001092 PCI_SLOT(e->devid),
1093 PCI_FUNC(e->devid),
1094 e->flags, e->ext);
1095
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001096 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001097 set_dev_entry_from_acpi(iommu, devid, e->flags,
1098 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001099 break;
1100 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001101
1102 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1103 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001104 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001105 PCI_SLOT(e->devid),
1106 PCI_FUNC(e->devid),
1107 e->flags, e->ext);
1108
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001109 devid_start = e->devid;
1110 flags = e->flags;
1111 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001112 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001113 break;
1114 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001115
1116 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001117 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001118 PCI_SLOT(e->devid),
1119 PCI_FUNC(e->devid));
1120
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001121 devid = e->devid;
1122 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001123 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001124 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001125 set_dev_entry_from_acpi(iommu,
1126 devid_to, flags, ext_flags);
1127 }
1128 set_dev_entry_from_acpi(iommu, dev_i,
1129 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001130 }
1131 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001132 case IVHD_DEV_SPECIAL: {
1133 u8 handle, type;
1134 const char *var;
1135 u16 devid;
1136 int ret;
1137
1138 handle = e->ext & 0xff;
1139 devid = (e->ext >> 8) & 0xffff;
1140 type = (e->ext >> 24) & 0xff;
1141
1142 if (type == IVHD_SPECIAL_IOAPIC)
1143 var = "IOAPIC";
1144 else if (type == IVHD_SPECIAL_HPET)
1145 var = "HPET";
1146 else
1147 var = "UNKNOWN";
1148
1149 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1150 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001151 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001152 PCI_SLOT(devid),
1153 PCI_FUNC(devid));
1154
Joerg Roedelc50e3242014-09-09 15:59:37 +02001155 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001156 if (ret)
1157 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001158
1159 /*
1160 * add_special_device might update the devid in case a
1161 * command-line override is present. So call
1162 * set_dev_entry_from_acpi after add_special_device.
1163 */
1164 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1165
Joerg Roedel6efed632012-06-14 15:52:58 +02001166 break;
1167 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001168 case IVHD_DEV_ACPI_HID: {
1169 u16 devid;
1170 u8 hid[ACPIHID_HID_LEN] = {0};
1171 u8 uid[ACPIHID_UID_LEN] = {0};
1172 int ret;
1173
1174 if (h->type != 0x40) {
1175 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1176 e->type);
1177 break;
1178 }
1179
1180 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1181 hid[ACPIHID_HID_LEN - 1] = '\0';
1182
1183 if (!(*hid)) {
1184 pr_err(FW_BUG "Invalid HID.\n");
1185 break;
1186 }
1187
1188 switch (e->uidf) {
1189 case UID_NOT_PRESENT:
1190
1191 if (e->uidl != 0)
1192 pr_warn(FW_BUG "Invalid UID length.\n");
1193
1194 break;
1195 case UID_IS_INTEGER:
1196
1197 sprintf(uid, "%d", e->uid);
1198
1199 break;
1200 case UID_IS_CHARACTER:
1201
1202 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1203 uid[ACPIHID_UID_LEN - 1] = '\0';
1204
1205 break;
1206 default:
1207 break;
1208 }
1209
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001210 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001211 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1212 hid, uid,
1213 PCI_BUS_NUM(devid),
1214 PCI_SLOT(devid),
1215 PCI_FUNC(devid));
1216
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001217 flags = e->flags;
1218
1219 ret = add_acpi_hid_device(hid, uid, &devid, false);
1220 if (ret)
1221 return ret;
1222
1223 /*
1224 * add_special_device might update the devid in case a
1225 * command-line override is present. So call
1226 * set_dev_entry_from_acpi after add_special_device.
1227 */
1228 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1229
1230 break;
1231 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001232 default:
1233 break;
1234 }
1235
Joerg Roedelb514e552008-09-17 17:14:27 +02001236 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001237 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001238
1239 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001240}
1241
Joerg Roedele47d4022008-06-26 21:27:48 +02001242static void __init free_iommu_one(struct amd_iommu *iommu)
1243{
1244 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001245 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001246 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001247 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001248 iommu_unmap_mmio_space(iommu);
1249}
1250
1251static void __init free_iommu_all(void)
1252{
1253 struct amd_iommu *iommu, *next;
1254
Joerg Roedel3bd22172009-05-04 15:06:20 +02001255 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001256 list_del(&iommu->list);
1257 free_iommu_one(iommu);
1258 kfree(iommu);
1259 }
1260}
1261
Joerg Roedelb65233a2008-07-11 17:14:21 +02001262/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001263 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1264 * Workaround:
1265 * BIOS should disable L2B micellaneous clock gating by setting
1266 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1267 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001268static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001269{
1270 u32 value;
1271
1272 if ((boot_cpu_data.x86 != 0x15) ||
1273 (boot_cpu_data.x86_model < 0x10) ||
1274 (boot_cpu_data.x86_model > 0x1f))
1275 return;
1276
1277 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1278 pci_read_config_dword(iommu->dev, 0xf4, &value);
1279
1280 if (value & BIT(2))
1281 return;
1282
1283 /* Select NB indirect register 0x90 and enable writing */
1284 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1285
1286 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1287 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1288 dev_name(&iommu->dev->dev));
1289
1290 /* Clear the enable writing bit */
1291 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1292}
1293
1294/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001295 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1296 * Workaround:
1297 * BIOS should enable ATS write permission check by setting
1298 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1299 */
1300static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1301{
1302 u32 value;
1303
1304 if ((boot_cpu_data.x86 != 0x15) ||
1305 (boot_cpu_data.x86_model < 0x30) ||
1306 (boot_cpu_data.x86_model > 0x3f))
1307 return;
1308
1309 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1310 value = iommu_read_l2(iommu, 0x47);
1311
1312 if (value & BIT(0))
1313 return;
1314
1315 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1316 iommu_write_l2(iommu, 0x47, value | BIT(0));
1317
1318 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1319 dev_name(&iommu->dev->dev));
1320}
1321
1322/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001323 * This function clues the initialization function for one IOMMU
1324 * together and also allocates the command buffer and programs the
1325 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1326 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001327static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1328{
Joerg Roedel6efed632012-06-14 15:52:58 +02001329 int ret;
1330
Joerg Roedele47d4022008-06-26 21:27:48 +02001331 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001332
1333 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001334 list_add_tail(&iommu->list, &amd_iommu_list);
Joerg Roedelbb527772009-11-20 14:31:51 +01001335 iommu->index = amd_iommus_present++;
1336
1337 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1338 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1339 return -ENOSYS;
1340 }
1341
1342 /* Index is fine - add IOMMU to the array */
1343 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001344
1345 /*
1346 * Copy data from ACPI table entry to the iommu struct
1347 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001348 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001349 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001350 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001351 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001352
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001353 switch (h->type) {
1354 case 0x10:
1355 /* Check if IVHD EFR contains proper max banks/counters */
1356 if ((h->efr_attr != 0) &&
1357 ((h->efr_attr & (0xF << 13)) != 0) &&
1358 ((h->efr_attr & (0x3F << 17)) != 0))
1359 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1360 else
1361 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001362 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1363 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001364 break;
1365 case 0x11:
1366 case 0x40:
1367 if (h->efr_reg & (1 << 9))
1368 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1369 else
1370 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001371 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1372 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001373 break;
1374 default:
1375 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001376 }
1377
1378 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1379 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001380 if (!iommu->mmio_base)
1381 return -ENOMEM;
1382
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001383 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001384 return -ENOMEM;
1385
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001386 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001387 return -ENOMEM;
1388
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001389 iommu->int_enabled = false;
1390
Joerg Roedel6efed632012-06-14 15:52:58 +02001391 ret = init_iommu_from_acpi(iommu, h);
1392 if (ret)
1393 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001394
Jiang Liu7c71d302015-04-13 14:11:33 +08001395 ret = amd_iommu_create_irq_domain(iommu);
1396 if (ret)
1397 return ret;
1398
Joerg Roedelf6fec002012-06-21 16:51:25 +02001399 /*
1400 * Make sure IOMMU is not considered to translate itself. The IVRS
1401 * table tells us so, but this is a lie!
1402 */
1403 amd_iommu_rlookup_table[iommu->devid] = NULL;
1404
Joerg Roedel23c742d2012-06-12 11:47:34 +02001405 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001406}
1407
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001408/**
1409 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1410 * @ivrs Pointer to the IVRS header
1411 *
1412 * This function search through all IVDB of the maximum supported IVHD
1413 */
1414static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1415{
1416 u8 *base = (u8 *)ivrs;
1417 struct ivhd_header *ivhd = (struct ivhd_header *)
1418 (base + IVRS_HEADER_LENGTH);
1419 u8 last_type = ivhd->type;
1420 u16 devid = ivhd->devid;
1421
1422 while (((u8 *)ivhd - base < ivrs->length) &&
1423 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1424 u8 *p = (u8 *) ivhd;
1425
1426 if (ivhd->devid == devid)
1427 last_type = ivhd->type;
1428 ivhd = (struct ivhd_header *)(p + ivhd->length);
1429 }
1430
1431 return last_type;
1432}
1433
Joerg Roedelb65233a2008-07-11 17:14:21 +02001434/*
1435 * Iterates over all IOMMU entries in the ACPI table, allocates the
1436 * IOMMU structure and initializes it with init_iommu_one()
1437 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001438static int __init init_iommu_all(struct acpi_table_header *table)
1439{
1440 u8 *p = (u8 *)table, *end = (u8 *)table;
1441 struct ivhd_header *h;
1442 struct amd_iommu *iommu;
1443 int ret;
1444
Joerg Roedele47d4022008-06-26 21:27:48 +02001445 end += table->length;
1446 p += IVRS_HEADER_LENGTH;
1447
1448 while (p < end) {
1449 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001450 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001451
Joerg Roedelae908c22009-09-01 16:52:16 +02001452 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001453 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001454 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001455 PCI_FUNC(h->devid), h->cap_ptr,
1456 h->pci_seg, h->flags, h->info);
1457 DUMP_printk(" mmio-addr: %016llx\n",
1458 h->mmio_phys);
1459
Joerg Roedele47d4022008-06-26 21:27:48 +02001460 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001461 if (iommu == NULL)
1462 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001463
Joerg Roedele47d4022008-06-26 21:27:48 +02001464 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001465 if (ret)
1466 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001467 }
1468 p += h->length;
1469
1470 }
1471 WARN_ON(p != end);
1472
1473 return 0;
1474}
1475
Steven L Kinney30861dd2013-06-05 16:11:48 -05001476
1477static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1478{
1479 u64 val = 0xabcd, val2 = 0;
1480
1481 if (!iommu_feature(iommu, FEATURE_PC))
1482 return;
1483
1484 amd_iommu_pc_present = true;
1485
1486 /* Check if the performance counters can be written to */
Suravee Suthikulpanit38e45d022016-02-23 13:03:30 +01001487 if ((0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val, true)) ||
1488 (0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001489 (val != val2)) {
1490 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1491 amd_iommu_pc_present = false;
1492 return;
1493 }
1494
1495 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1496
1497 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1498 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1499 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1500}
1501
Alex Williamson066f2e92014-06-12 16:12:37 -06001502static ssize_t amd_iommu_show_cap(struct device *dev,
1503 struct device_attribute *attr,
1504 char *buf)
1505{
1506 struct amd_iommu *iommu = dev_get_drvdata(dev);
1507 return sprintf(buf, "%x\n", iommu->cap);
1508}
1509static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1510
1511static ssize_t amd_iommu_show_features(struct device *dev,
1512 struct device_attribute *attr,
1513 char *buf)
1514{
1515 struct amd_iommu *iommu = dev_get_drvdata(dev);
1516 return sprintf(buf, "%llx\n", iommu->features);
1517}
1518static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1519
1520static struct attribute *amd_iommu_attrs[] = {
1521 &dev_attr_cap.attr,
1522 &dev_attr_features.attr,
1523 NULL,
1524};
1525
1526static struct attribute_group amd_iommu_group = {
1527 .name = "amd-iommu",
1528 .attrs = amd_iommu_attrs,
1529};
1530
1531static const struct attribute_group *amd_iommu_groups[] = {
1532 &amd_iommu_group,
1533 NULL,
1534};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001535
Joerg Roedel23c742d2012-06-12 11:47:34 +02001536static int iommu_init_pci(struct amd_iommu *iommu)
1537{
1538 int cap_ptr = iommu->cap_ptr;
1539 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001540 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001541
Shuah Khanc5081cd2013-02-27 17:07:19 -07001542 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001543 iommu->devid & 0xff);
1544 if (!iommu->dev)
1545 return -ENODEV;
1546
Jiang Liucbbc00b2015-10-09 22:07:31 +08001547 /* Prevent binding other PCI device drivers to IOMMU devices */
1548 iommu->dev->match_driver = false;
1549
Joerg Roedel23c742d2012-06-12 11:47:34 +02001550 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1551 &iommu->cap);
1552 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1553 &range);
1554 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1555 &misc);
1556
Joerg Roedel23c742d2012-06-12 11:47:34 +02001557 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1558 amd_iommu_iotlb_sup = false;
1559
1560 /* read extended feature bits */
1561 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1562 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1563
1564 iommu->features = ((u64)high << 32) | low;
1565
1566 if (iommu_feature(iommu, FEATURE_GT)) {
1567 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001568 u32 max_pasid;
1569 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001570
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001571 pasmax = iommu->features & FEATURE_PASID_MASK;
1572 pasmax >>= FEATURE_PASID_SHIFT;
1573 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001574
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001575 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1576
1577 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001578
1579 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1580 glxval >>= FEATURE_GLXVAL_SHIFT;
1581
1582 if (amd_iommu_max_glx_val == -1)
1583 amd_iommu_max_glx_val = glxval;
1584 else
1585 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1586 }
1587
1588 if (iommu_feature(iommu, FEATURE_GT) &&
1589 iommu_feature(iommu, FEATURE_PPR)) {
1590 iommu->is_iommu_v2 = true;
1591 amd_iommu_v2_present = true;
1592 }
1593
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001594 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1595 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001596
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001597 ret = iommu_init_ga(iommu);
1598 if (ret)
1599 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001600
Joerg Roedel23c742d2012-06-12 11:47:34 +02001601 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1602 amd_iommu_np_cache = true;
1603
Steven L Kinney30861dd2013-06-05 16:11:48 -05001604 init_iommu_perf_ctr(iommu);
1605
Joerg Roedel23c742d2012-06-12 11:47:34 +02001606 if (is_rd890_iommu(iommu->dev)) {
1607 int i, j;
1608
1609 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1610 PCI_DEVFN(0, 0));
1611
1612 /*
1613 * Some rd890 systems may not be fully reconfigured by the
1614 * BIOS, so it's necessary for us to store this information so
1615 * it can be reprogrammed on resume
1616 */
1617 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1618 &iommu->stored_addr_lo);
1619 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1620 &iommu->stored_addr_hi);
1621
1622 /* Low bit locks writes to configuration space */
1623 iommu->stored_addr_lo &= ~1;
1624
1625 for (i = 0; i < 6; i++)
1626 for (j = 0; j < 0x12; j++)
1627 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1628
1629 for (i = 0; i < 0x83; i++)
1630 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1631 }
1632
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001633 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001634 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001635
Alex Williamson066f2e92014-06-12 16:12:37 -06001636 iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu,
1637 amd_iommu_groups, "ivhd%d",
1638 iommu->index);
1639
Joerg Roedel23c742d2012-06-12 11:47:34 +02001640 return pci_enable_device(iommu->dev);
1641}
1642
Joerg Roedel4d121c32012-06-14 12:21:55 +02001643static void print_iommu_info(void)
1644{
1645 static const char * const feat_str[] = {
1646 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1647 "IA", "GA", "HE", "PC"
1648 };
1649 struct amd_iommu *iommu;
1650
1651 for_each_iommu(iommu) {
1652 int i;
1653
1654 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1655 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1656
1657 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001658 pr_info("AMD-Vi: Extended features (%#llx):\n",
1659 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001660 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001661 if (iommu_feature(iommu, (1ULL << i)))
1662 pr_cont(" %s", feat_str[i]);
1663 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001664
1665 if (iommu->features & FEATURE_GAM_VAPIC)
1666 pr_cont(" GA_vAPIC");
1667
Steven L Kinney30861dd2013-06-05 16:11:48 -05001668 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001669 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001670 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001671 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001672 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001673 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1674 pr_info("AMD-Vi: virtual APIC enabled\n");
1675 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001676}
1677
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001678static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001679{
1680 struct amd_iommu *iommu;
1681 int ret = 0;
1682
1683 for_each_iommu(iommu) {
1684 ret = iommu_init_pci(iommu);
1685 if (ret)
1686 break;
1687 }
1688
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001689 /*
1690 * Order is important here to make sure any unity map requirements are
1691 * fulfilled. The unity mappings are created and written to the device
1692 * table during the amd_iommu_init_api() call.
1693 *
1694 * After that we call init_device_table_dma() to make sure any
1695 * uninitialized DTE will block DMA, and in the end we flush the caches
1696 * of all IOMMUs to make sure the changes to the device table are
1697 * active.
1698 */
1699 ret = amd_iommu_init_api();
1700
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001701 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001702
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001703 for_each_iommu(iommu)
1704 iommu_flush_all_caches(iommu);
1705
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001706 if (!ret)
1707 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001708
Joerg Roedel23c742d2012-06-12 11:47:34 +02001709 return ret;
1710}
1711
Joerg Roedelb65233a2008-07-11 17:14:21 +02001712/****************************************************************************
1713 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001714 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001715 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001716 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1717 * pci_dev.
1718 *
1719 ****************************************************************************/
1720
Joerg Roedel9f800de2009-11-23 12:45:25 +01001721static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001722{
1723 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001724
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001725 r = pci_enable_msi(iommu->dev);
1726 if (r)
1727 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001728
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001729 r = request_threaded_irq(iommu->dev->irq,
1730 amd_iommu_int_handler,
1731 amd_iommu_int_thread,
1732 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001733 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001734
1735 if (r) {
1736 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001737 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001738 }
1739
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001740 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001741
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001742 return 0;
1743}
1744
Joerg Roedel05f92db2009-05-12 09:52:46 +02001745static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001746{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001747 int ret;
1748
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001749 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001750 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001751
Yijing Wang82fcfc62013-08-08 21:12:36 +08001752 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001753 ret = iommu_setup_msi(iommu);
1754 else
1755 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001756
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001757 if (ret)
1758 return ret;
1759
1760enable_faults:
1761 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1762
1763 if (iommu->ppr_log != NULL)
1764 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1765
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001766 iommu_ga_log_enable(iommu);
1767
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001768 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001769}
1770
1771/****************************************************************************
1772 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001773 * The next functions belong to the third pass of parsing the ACPI
1774 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001775 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001776 *
1777 ****************************************************************************/
1778
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001779static void __init free_unity_maps(void)
1780{
1781 struct unity_map_entry *entry, *next;
1782
1783 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1784 list_del(&entry->list);
1785 kfree(entry);
1786 }
1787}
1788
Joerg Roedelb65233a2008-07-11 17:14:21 +02001789/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001790static int __init init_exclusion_range(struct ivmd_header *m)
1791{
1792 int i;
1793
1794 switch (m->type) {
1795 case ACPI_IVMD_TYPE:
1796 set_device_exclusion_range(m->devid, m);
1797 break;
1798 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001799 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001800 set_device_exclusion_range(i, m);
1801 break;
1802 case ACPI_IVMD_TYPE_RANGE:
1803 for (i = m->devid; i <= m->aux; ++i)
1804 set_device_exclusion_range(i, m);
1805 break;
1806 default:
1807 break;
1808 }
1809
1810 return 0;
1811}
1812
Joerg Roedelb65233a2008-07-11 17:14:21 +02001813/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001814static int __init init_unity_map_range(struct ivmd_header *m)
1815{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001816 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001817 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001818
1819 e = kzalloc(sizeof(*e), GFP_KERNEL);
1820 if (e == NULL)
1821 return -ENOMEM;
1822
1823 switch (m->type) {
1824 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001825 kfree(e);
1826 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001827 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001828 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001829 e->devid_start = e->devid_end = m->devid;
1830 break;
1831 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001832 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001833 e->devid_start = 0;
1834 e->devid_end = amd_iommu_last_bdf;
1835 break;
1836 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001837 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001838 e->devid_start = m->devid;
1839 e->devid_end = m->aux;
1840 break;
1841 }
1842 e->address_start = PAGE_ALIGN(m->range_start);
1843 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1844 e->prot = m->flags >> 1;
1845
Joerg Roedel02acc432009-05-20 16:24:21 +02001846 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1847 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001848 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1849 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02001850 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1851 e->address_start, e->address_end, m->flags);
1852
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001853 list_add_tail(&e->list, &amd_iommu_unity_map);
1854
1855 return 0;
1856}
1857
Joerg Roedelb65233a2008-07-11 17:14:21 +02001858/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001859static int __init init_memory_definitions(struct acpi_table_header *table)
1860{
1861 u8 *p = (u8 *)table, *end = (u8 *)table;
1862 struct ivmd_header *m;
1863
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001864 end += table->length;
1865 p += IVRS_HEADER_LENGTH;
1866
1867 while (p < end) {
1868 m = (struct ivmd_header *)p;
1869 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1870 init_exclusion_range(m);
1871 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1872 init_unity_map_range(m);
1873
1874 p += m->length;
1875 }
1876
1877 return 0;
1878}
1879
Joerg Roedelb65233a2008-07-11 17:14:21 +02001880/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001881 * Init the device table to not allow DMA access for devices and
1882 * suppress all page faults
1883 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02001884static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001885{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001886 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001887
1888 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1889 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1890 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001891 }
1892}
1893
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001894static void __init uninit_device_table_dma(void)
1895{
1896 u32 devid;
1897
1898 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1899 amd_iommu_dev_table[devid].data[0] = 0ULL;
1900 amd_iommu_dev_table[devid].data[1] = 0ULL;
1901 }
1902}
1903
Joerg Roedel33f28c52012-06-15 18:03:31 +02001904static void init_device_table(void)
1905{
1906 u32 devid;
1907
1908 if (!amd_iommu_irq_remap)
1909 return;
1910
1911 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1912 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1913}
1914
Joerg Roedele9bf5192010-09-20 14:33:07 +02001915static void iommu_init_flags(struct amd_iommu *iommu)
1916{
1917 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1918 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1919 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1920
1921 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1922 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1923 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1924
1925 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1926 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1927 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1928
1929 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1930 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1931 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1932
1933 /*
1934 * make IOMMU memory accesses cache coherent
1935 */
1936 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001937
1938 /* Set IOTLB invalidation timeout to 1s */
1939 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001940}
1941
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001942static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001943{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001944 int i, j;
1945 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001946 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001947
1948 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001949 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001950 return;
1951
1952 /*
1953 * First, we need to ensure that the iommu is enabled. This is
1954 * controlled by a register in the northbridge
1955 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001956
1957 /* Select Northbridge indirect register 0x75 and enable writing */
1958 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1959 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1960
1961 /* Enable the iommu */
1962 if (!(ioc_feature_control & 0x1))
1963 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1964
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001965 /* Restore the iommu BAR */
1966 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1967 iommu->stored_addr_lo);
1968 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1969 iommu->stored_addr_hi);
1970
1971 /* Restore the l1 indirect regs for each of the 6 l1s */
1972 for (i = 0; i < 6; i++)
1973 for (j = 0; j < 0x12; j++)
1974 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1975
1976 /* Restore the l2 indirect regs */
1977 for (i = 0; i < 0x83; i++)
1978 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
1979
1980 /* Lock PCI setup registers */
1981 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1982 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02001983}
1984
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001985static void iommu_enable_ga(struct amd_iommu *iommu)
1986{
1987#ifdef CONFIG_IRQ_REMAP
1988 switch (amd_iommu_guest_ir) {
1989 case AMD_IOMMU_GUEST_IR_VAPIC:
1990 iommu_feature_enable(iommu, CONTROL_GAM_EN);
1991 /* Fall through */
1992 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
1993 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05001994 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001995 break;
1996 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05001997 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001998 break;
1999 }
2000#endif
2001}
2002
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002003/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002004 * This function finally enables all IOMMUs found in the system after
2005 * they have been initialized
2006 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002007static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002008{
2009 struct amd_iommu *iommu;
2010
Joerg Roedel3bd22172009-05-04 15:06:20 +02002011 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02002012 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002013 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02002014 iommu_set_device_table(iommu);
2015 iommu_enable_command_buffer(iommu);
2016 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002017 iommu_set_exclusion_range(iommu);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002018 iommu_enable_ga(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002019 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02002020 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002021 }
2022}
2023
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002024static void enable_iommus_v2(void)
2025{
2026 struct amd_iommu *iommu;
2027
2028 for_each_iommu(iommu) {
2029 iommu_enable_ppr_log(iommu);
2030 iommu_enable_gt(iommu);
2031 }
2032}
2033
2034static void enable_iommus(void)
2035{
2036 early_enable_iommus();
2037
2038 enable_iommus_v2();
2039}
2040
Joerg Roedel92ac4322009-05-19 19:06:27 +02002041static void disable_iommus(void)
2042{
2043 struct amd_iommu *iommu;
2044
2045 for_each_iommu(iommu)
2046 iommu_disable(iommu);
2047}
2048
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002049/*
2050 * Suspend/Resume support
2051 * disable suspend until real resume implemented
2052 */
2053
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002054static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002055{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002056 struct amd_iommu *iommu;
2057
2058 for_each_iommu(iommu)
2059 iommu_apply_resume_quirks(iommu);
2060
Joerg Roedel736501e2009-05-12 09:56:12 +02002061 /* re-load the hardware */
2062 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002063
2064 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002065}
2066
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002067static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002068{
Joerg Roedel736501e2009-05-12 09:56:12 +02002069 /* disable IOMMUs to go out of the way for BIOS */
2070 disable_iommus();
2071
2072 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002073}
2074
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002075static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002076 .suspend = amd_iommu_suspend,
2077 .resume = amd_iommu_resume,
2078};
2079
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002080static void __init free_on_init_error(void)
2081{
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002082 free_pages((unsigned long)irq_lookup_table,
2083 get_order(rlookup_table_size));
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002084
Julia Lawalla5919892015-09-13 14:15:31 +02002085 kmem_cache_destroy(amd_iommu_irq_cache);
2086 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002087
2088 free_pages((unsigned long)amd_iommu_rlookup_table,
2089 get_order(rlookup_table_size));
2090
2091 free_pages((unsigned long)amd_iommu_alias_table,
2092 get_order(alias_table_size));
2093
2094 free_pages((unsigned long)amd_iommu_dev_table,
2095 get_order(dev_table_size));
2096
2097 free_iommu_all();
2098
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002099#ifdef CONFIG_GART_IOMMU
2100 /*
2101 * We failed to initialize the AMD IOMMU - try fallback to GART
2102 * if possible.
2103 */
2104 gart_iommu_init();
2105
2106#endif
2107}
2108
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002109/* SB IOAPIC is always on this device in AMD systems */
2110#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2111
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002112static bool __init check_ioapic_information(void)
2113{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002114 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002115 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002116 int idx;
2117
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002118 has_sb_ioapic = false;
2119 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002120
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002121 /*
2122 * If we have map overrides on the kernel command line the
2123 * messages in this function might not describe firmware bugs
2124 * anymore - so be careful
2125 */
2126 if (cmdline_maps)
2127 fw_bug = "";
2128
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002129 for (idx = 0; idx < nr_ioapics; idx++) {
2130 int devid, id = mpc_ioapic_id(idx);
2131
2132 devid = get_ioapic_devid(id);
2133 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002134 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2135 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002136 ret = false;
2137 } else if (devid == IOAPIC_SB_DEVID) {
2138 has_sb_ioapic = true;
2139 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002140 }
2141 }
2142
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002143 if (!has_sb_ioapic) {
2144 /*
2145 * We expect the SB IOAPIC to be listed in the IVRS
2146 * table. The system timer is connected to the SB IOAPIC
2147 * and if we don't have it in the list the system will
2148 * panic at boot time. This situation usually happens
2149 * when the BIOS is buggy and provides us the wrong
2150 * device id for the IOAPIC in the system.
2151 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002152 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002153 }
2154
2155 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002156 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002157
2158 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002159}
2160
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002161static void __init free_dma_resources(void)
2162{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002163 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2164 get_order(MAX_DOMAIN_ID/8));
2165
2166 free_unity_maps();
2167}
2168
Joerg Roedelb65233a2008-07-11 17:14:21 +02002169/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002170 * This is the hardware init function for AMD IOMMU in the system.
2171 * This function is called either from amd_iommu_init or from the interrupt
2172 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002173 *
2174 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002175 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002176 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002177 * 1 pass) Discover the most comprehensive IVHD type to use.
2178 *
2179 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002180 * Upon this information the size of the data structures is
2181 * determined that needs to be allocated.
2182 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002183 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002184 * information in the ACPI table about available AMD IOMMUs
2185 * in the system. It also maps the PCI devices in the
2186 * system to specific IOMMUs
2187 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002188 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002189 * initialized we update them with information about memory
2190 * remapping requirements parsed out of the ACPI table in
2191 * this last pass.
2192 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002193 * After everything is set up the IOMMUs are enabled and the necessary
2194 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002195 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002196static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002197{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002198 struct acpi_table_header *ivrs_base;
2199 acpi_size ivrs_size;
2200 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002201 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002202
Joerg Roedel643511b2012-06-12 12:09:35 +02002203 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002204 return -ENODEV;
2205
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002206 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
2207 if (status == AE_NOT_FOUND)
2208 return -ENODEV;
2209 else if (ACPI_FAILURE(status)) {
2210 const char *err = acpi_format_exception(status);
2211 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2212 return -EINVAL;
2213 }
2214
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002215 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002216 * Validate checksum here so we don't need to do it when
2217 * we actually parse the table
2218 */
2219 ret = check_ivrs_checksum(ivrs_base);
2220 if (ret)
2221 return ret;
2222
2223 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2224 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2225
2226 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002227 * First parse ACPI tables to find the largest Bus/Dev/Func
2228 * we need to handle. Upon this information the shared data
2229 * structures for the IOMMUs in the system will be allocated
2230 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002231 ret = find_last_devid_acpi(ivrs_base);
2232 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002233 goto out;
2234
Joerg Roedelc5714842008-07-11 17:14:25 +02002235 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2236 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2237 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002238
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002239 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002240 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002241 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002242 get_order(dev_table_size));
2243 if (amd_iommu_dev_table == NULL)
2244 goto out;
2245
2246 /*
2247 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2248 * IOMMU see for that device
2249 */
2250 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2251 get_order(alias_table_size));
2252 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002253 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002254
2255 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002256 amd_iommu_rlookup_table = (void *)__get_free_pages(
2257 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002258 get_order(rlookup_table_size));
2259 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002260 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002261
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002262 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2263 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002264 get_order(MAX_DOMAIN_ID/8));
2265 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002266 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002267
2268 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002269 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002270 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002271 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002272 amd_iommu_alias_table[i] = i;
2273
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002274 /*
2275 * never allocate domain 0 because its used as the non-allocated and
2276 * error value placeholder
2277 */
2278 amd_iommu_pd_alloc_bitmap[0] = 1;
2279
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002280 spin_lock_init(&amd_iommu_pd_lock);
2281
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002282 /*
2283 * now the data structures are allocated and basically initialized
2284 * start the real acpi table scan
2285 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002286 ret = init_iommu_all(ivrs_base);
2287 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002288 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002289
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002290 if (amd_iommu_irq_remap)
2291 amd_iommu_irq_remap = check_ioapic_information();
2292
Joerg Roedel05152a02012-06-15 16:53:51 +02002293 if (amd_iommu_irq_remap) {
2294 /*
2295 * Interrupt remapping enabled, create kmem_cache for the
2296 * remapping tables.
2297 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002298 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002299 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2300 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2301 else
2302 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002303 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002304 remap_cache_sz,
2305 IRQ_TABLE_ALIGNMENT,
2306 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002307 if (!amd_iommu_irq_cache)
2308 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002309
2310 irq_lookup_table = (void *)__get_free_pages(
2311 GFP_KERNEL | __GFP_ZERO,
2312 get_order(rlookup_table_size));
2313 if (!irq_lookup_table)
2314 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002315 }
2316
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002317 ret = init_memory_definitions(ivrs_base);
2318 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002319 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002320
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002321 /* init the device table */
2322 init_device_table();
2323
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002324out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002325 /* Don't leak any ACPI memory */
2326 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
2327 ivrs_base = NULL;
2328
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002329 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002330}
2331
Gerard Snitselaarae295142012-03-16 11:38:22 -07002332static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002333{
2334 struct amd_iommu *iommu;
2335 int ret = 0;
2336
2337 for_each_iommu(iommu) {
2338 ret = iommu_init_msi(iommu);
2339 if (ret)
2340 goto out;
2341 }
2342
2343out:
2344 return ret;
2345}
2346
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002347static bool detect_ivrs(void)
2348{
2349 struct acpi_table_header *ivrs_base;
2350 acpi_size ivrs_size;
2351 acpi_status status;
2352
2353 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
2354 if (status == AE_NOT_FOUND)
2355 return false;
2356 else if (ACPI_FAILURE(status)) {
2357 const char *err = acpi_format_exception(status);
2358 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2359 return false;
2360 }
2361
2362 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
2363
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002364 /* Make sure ACS will be enabled during PCI probe */
2365 pci_request_acs();
2366
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002367 return true;
2368}
2369
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002370/****************************************************************************
2371 *
2372 * AMD IOMMU Initialization State Machine
2373 *
2374 ****************************************************************************/
2375
2376static int __init state_next(void)
2377{
2378 int ret = 0;
2379
2380 switch (init_state) {
2381 case IOMMU_START_STATE:
2382 if (!detect_ivrs()) {
2383 init_state = IOMMU_NOT_FOUND;
2384 ret = -ENODEV;
2385 } else {
2386 init_state = IOMMU_IVRS_DETECTED;
2387 }
2388 break;
2389 case IOMMU_IVRS_DETECTED:
2390 ret = early_amd_iommu_init();
2391 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2392 break;
2393 case IOMMU_ACPI_FINISHED:
2394 early_enable_iommus();
2395 register_syscore_ops(&amd_iommu_syscore_ops);
2396 x86_platform.iommu_shutdown = disable_iommus;
2397 init_state = IOMMU_ENABLED;
2398 break;
2399 case IOMMU_ENABLED:
2400 ret = amd_iommu_init_pci();
2401 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2402 enable_iommus_v2();
2403 break;
2404 case IOMMU_PCI_INIT:
2405 ret = amd_iommu_enable_interrupts();
2406 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2407 break;
2408 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002409 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002410 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2411 break;
2412 case IOMMU_DMA_OPS:
2413 init_state = IOMMU_INITIALIZED;
2414 break;
2415 case IOMMU_INITIALIZED:
2416 /* Nothing to do */
2417 break;
2418 case IOMMU_NOT_FOUND:
2419 case IOMMU_INIT_ERROR:
2420 /* Error states => do nothing */
2421 ret = -EINVAL;
2422 break;
2423 default:
2424 /* Unknown state */
2425 BUG();
2426 }
2427
2428 return ret;
2429}
2430
2431static int __init iommu_go_to_state(enum iommu_init_state state)
2432{
2433 int ret = 0;
2434
2435 while (init_state != state) {
2436 ret = state_next();
2437 if (init_state == IOMMU_NOT_FOUND ||
2438 init_state == IOMMU_INIT_ERROR)
2439 break;
2440 }
2441
2442 return ret;
2443}
2444
Joerg Roedel6b474b82012-06-26 16:46:04 +02002445#ifdef CONFIG_IRQ_REMAP
2446int __init amd_iommu_prepare(void)
2447{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002448 int ret;
2449
Jiang Liu7fa1c842015-01-07 15:31:42 +08002450 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002451
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002452 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2453 if (ret)
2454 return ret;
2455 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002456}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002457
Joerg Roedel6b474b82012-06-26 16:46:04 +02002458int __init amd_iommu_enable(void)
2459{
2460 int ret;
2461
2462 ret = iommu_go_to_state(IOMMU_ENABLED);
2463 if (ret)
2464 return ret;
2465
2466 irq_remapping_enabled = 1;
2467
2468 return 0;
2469}
2470
2471void amd_iommu_disable(void)
2472{
2473 amd_iommu_suspend();
2474}
2475
2476int amd_iommu_reenable(int mode)
2477{
2478 amd_iommu_resume();
2479
2480 return 0;
2481}
2482
2483int __init amd_iommu_enable_faulting(void)
2484{
2485 /* We enable MSI later when PCI is initialized */
2486 return 0;
2487}
2488#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002489
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002490/*
2491 * This is the core init function for AMD IOMMU hardware in the system.
2492 * This function is called from the generic x86 DMA layer initialization
2493 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002494 */
2495static int __init amd_iommu_init(void)
2496{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002497 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002498
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002499 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2500 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002501 free_dma_resources();
2502 if (!irq_remapping_enabled) {
2503 disable_iommus();
2504 free_on_init_error();
2505 } else {
2506 struct amd_iommu *iommu;
2507
2508 uninit_device_table_dma();
2509 for_each_iommu(iommu)
2510 iommu_flush_all_caches(iommu);
2511 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002512 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002513
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002514 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002515}
2516
Joerg Roedelb65233a2008-07-11 17:14:21 +02002517/****************************************************************************
2518 *
2519 * Early detect code. This code runs at IOMMU detection time in the DMA
2520 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2521 * IOMMUs
2522 *
2523 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002524int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002525{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002526 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002527
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002528 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002529 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002530
Joerg Roedela5235722010-05-11 17:12:33 +02002531 if (amd_iommu_disabled)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002532 return -ENODEV;
Joerg Roedela5235722010-05-11 17:12:33 +02002533
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002534 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2535 if (ret)
2536 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002537
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002538 amd_iommu_detected = true;
2539 iommu_detected = 1;
2540 x86_init.iommu.iommu_init = amd_iommu_init;
2541
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002542 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002543}
2544
Joerg Roedelb65233a2008-07-11 17:14:21 +02002545/****************************************************************************
2546 *
2547 * Parsing functions for the AMD IOMMU specific kernel command line
2548 * options.
2549 *
2550 ****************************************************************************/
2551
Joerg Roedelfefda112009-05-20 12:21:42 +02002552static int __init parse_amd_iommu_dump(char *str)
2553{
2554 amd_iommu_dump = true;
2555
2556 return 1;
2557}
2558
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002559static int __init parse_amd_iommu_intr(char *str)
2560{
2561 for (; *str; ++str) {
2562 if (strncmp(str, "legacy", 6) == 0) {
2563 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2564 break;
2565 }
2566 if (strncmp(str, "vapic", 5) == 0) {
2567 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2568 break;
2569 }
2570 }
2571 return 1;
2572}
2573
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002574static int __init parse_amd_iommu_options(char *str)
2575{
2576 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002577 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002578 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002579 if (strncmp(str, "off", 3) == 0)
2580 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002581 if (strncmp(str, "force_isolation", 15) == 0)
2582 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002583 }
2584
2585 return 1;
2586}
2587
Joerg Roedel440e89982013-04-09 16:35:28 +02002588static int __init parse_ivrs_ioapic(char *str)
2589{
2590 unsigned int bus, dev, fn;
2591 int ret, id, i;
2592 u16 devid;
2593
2594 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2595
2596 if (ret != 4) {
2597 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2598 return 1;
2599 }
2600
2601 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2602 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2603 str);
2604 return 1;
2605 }
2606
2607 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2608
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002609 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002610 i = early_ioapic_map_size++;
2611 early_ioapic_map[i].id = id;
2612 early_ioapic_map[i].devid = devid;
2613 early_ioapic_map[i].cmd_line = true;
2614
2615 return 1;
2616}
2617
2618static int __init parse_ivrs_hpet(char *str)
2619{
2620 unsigned int bus, dev, fn;
2621 int ret, id, i;
2622 u16 devid;
2623
2624 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2625
2626 if (ret != 4) {
2627 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2628 return 1;
2629 }
2630
2631 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2632 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2633 str);
2634 return 1;
2635 }
2636
2637 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2638
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002639 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002640 i = early_hpet_map_size++;
2641 early_hpet_map[i].id = id;
2642 early_hpet_map[i].devid = devid;
2643 early_hpet_map[i].cmd_line = true;
2644
2645 return 1;
2646}
2647
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002648static int __init parse_ivrs_acpihid(char *str)
2649{
2650 u32 bus, dev, fn;
2651 char *hid, *uid, *p;
2652 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2653 int ret, i;
2654
2655 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2656 if (ret != 4) {
2657 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2658 return 1;
2659 }
2660
2661 p = acpiid;
2662 hid = strsep(&p, ":");
2663 uid = p;
2664
2665 if (!hid || !(*hid) || !uid) {
2666 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2667 return 1;
2668 }
2669
2670 i = early_acpihid_map_size++;
2671 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2672 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2673 early_acpihid_map[i].devid =
2674 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2675 early_acpihid_map[i].cmd_line = true;
2676
2677 return 1;
2678}
2679
Joerg Roedel440e89982013-04-09 16:35:28 +02002680__setup("amd_iommu_dump", parse_amd_iommu_dump);
2681__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002682__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002683__setup("ivrs_ioapic", parse_ivrs_ioapic);
2684__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002685__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002686
2687IOMMU_INIT_FINISH(amd_iommu_detect,
2688 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002689 NULL,
2690 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002691
2692bool amd_iommu_v2_supported(void)
2693{
2694 return amd_iommu_v2_present;
2695}
2696EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002697
2698/****************************************************************************
2699 *
2700 * IOMMU EFR Performance Counter support functionality. This code allows
2701 * access to the IOMMU PC functionality.
2702 *
2703 ****************************************************************************/
2704
2705u8 amd_iommu_pc_get_max_banks(u16 devid)
2706{
2707 struct amd_iommu *iommu;
2708 u8 ret = 0;
2709
2710 /* locate the iommu governing the devid */
2711 iommu = amd_iommu_rlookup_table[devid];
2712 if (iommu)
2713 ret = iommu->max_banks;
2714
2715 return ret;
2716}
2717EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2718
2719bool amd_iommu_pc_supported(void)
2720{
2721 return amd_iommu_pc_present;
2722}
2723EXPORT_SYMBOL(amd_iommu_pc_supported);
2724
2725u8 amd_iommu_pc_get_max_counters(u16 devid)
2726{
2727 struct amd_iommu *iommu;
2728 u8 ret = 0;
2729
2730 /* locate the iommu governing the devid */
2731 iommu = amd_iommu_rlookup_table[devid];
2732 if (iommu)
2733 ret = iommu->max_counters;
2734
2735 return ret;
2736}
2737EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2738
Suravee Suthikulpanit38e45d022016-02-23 13:03:30 +01002739static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu,
2740 u8 bank, u8 cntr, u8 fxn,
Steven L Kinney30861dd2013-06-05 16:11:48 -05002741 u64 *value, bool is_write)
2742{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002743 u32 offset;
2744 u32 max_offset_lim;
2745
Steven L Kinney30861dd2013-06-05 16:11:48 -05002746 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit38e45d022016-02-23 13:03:30 +01002747 if (WARN_ON((fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002748 return -ENODEV;
2749
2750 offset = (u32)(((0x40|bank) << 12) | (cntr << 8) | fxn);
2751
2752 /* Limit the offset to the hw defined mmio region aperture */
2753 max_offset_lim = (u32)(((0x40|iommu->max_banks) << 12) |
2754 (iommu->max_counters << 8) | 0x28);
2755 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2756 (offset > max_offset_lim))
2757 return -EINVAL;
2758
2759 if (is_write) {
2760 writel((u32)*value, iommu->mmio_base + offset);
2761 writel((*value >> 32), iommu->mmio_base + offset + 4);
2762 } else {
2763 *value = readl(iommu->mmio_base + offset + 4);
2764 *value <<= 32;
2765 *value = readl(iommu->mmio_base + offset);
2766 }
2767
2768 return 0;
2769}
2770EXPORT_SYMBOL(amd_iommu_pc_get_set_reg_val);
Suravee Suthikulpanit38e45d022016-02-23 13:03:30 +01002771
2772int amd_iommu_pc_get_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn,
2773 u64 *value, bool is_write)
2774{
2775 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
2776
2777 /* Make sure the IOMMU PC resource is available */
2778 if (!amd_iommu_pc_present || iommu == NULL)
2779 return -ENODEV;
2780
2781 return iommu_pc_get_set_reg_val(iommu, bank, cntr, fxn,
2782 value, is_write);
2783}