blob: 971154cbbb03eb36efc7fb60608afe23d48f38a2 [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>
Baoquan He5c87f622016-09-15 16:50:51 +080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010025#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020026#include <linux/interrupt.h>
27#include <linux/msi.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020028#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010029#include <linux/export.h>
Alex Williamson066f2e92014-06-12 16:12:37 -060030#include <linux/iommu.h>
Lucas Stachebcfa282016-10-26 13:09:53 +020031#include <linux/kmemleak.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020032#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090033#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010034#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090035#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040036#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020037#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020038#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020039
40#include "amd_iommu_proto.h"
41#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020042#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020043
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020044/*
45 * definitions for the ACPI scanning code
46 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020047#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020048
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040049#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020050#define ACPI_IVMD_TYPE_ALL 0x20
51#define ACPI_IVMD_TYPE 0x21
52#define ACPI_IVMD_TYPE_RANGE 0x22
53
54#define IVHD_DEV_ALL 0x01
55#define IVHD_DEV_SELECT 0x02
56#define IVHD_DEV_SELECT_RANGE_START 0x03
57#define IVHD_DEV_RANGE_END 0x04
58#define IVHD_DEV_ALIAS 0x42
59#define IVHD_DEV_ALIAS_RANGE 0x43
60#define IVHD_DEV_EXT_SELECT 0x46
61#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020062#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040063#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020064
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040065#define UID_NOT_PRESENT 0
66#define UID_IS_INTEGER 1
67#define UID_IS_CHARACTER 2
68
Joerg Roedel6efed632012-06-14 15:52:58 +020069#define IVHD_SPECIAL_IOAPIC 1
70#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020071
Joerg Roedel6da73422009-05-04 11:44:38 +020072#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
73#define IVHD_FLAG_PASSPW_EN_MASK 0x02
74#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
75#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020076
77#define IVMD_FLAG_EXCL_RANGE 0x08
78#define IVMD_FLAG_UNITY_MAP 0x01
79
80#define ACPI_DEVFLAG_INITPASS 0x01
81#define ACPI_DEVFLAG_EXTINT 0x02
82#define ACPI_DEVFLAG_NMI 0x04
83#define ACPI_DEVFLAG_SYSMGT1 0x10
84#define ACPI_DEVFLAG_SYSMGT2 0x20
85#define ACPI_DEVFLAG_LINT0 0x40
86#define ACPI_DEVFLAG_LINT1 0x80
87#define ACPI_DEVFLAG_ATSDIS 0x10000000
88
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050089#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020090/*
91 * ACPI table definitions
92 *
93 * These data structures are laid over the table to parse the important values
94 * out of it.
95 */
96
97/*
98 * structure describing one IOMMU in the ACPI table. Typically followed by one
99 * or more ivhd_entrys.
100 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200101struct ivhd_header {
102 u8 type;
103 u8 flags;
104 u16 length;
105 u16 devid;
106 u16 cap_ptr;
107 u64 mmio_phys;
108 u16 pci_seg;
109 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400110 u32 efr_attr;
111
112 /* Following only valid on IVHD type 11h and 40h */
113 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
114 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200115} __attribute__((packed));
116
Joerg Roedelb65233a2008-07-11 17:14:21 +0200117/*
118 * A device entry describing which devices a specific IOMMU translates and
119 * which requestor ids they use.
120 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200121struct ivhd_entry {
122 u8 type;
123 u16 devid;
124 u8 flags;
125 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400126 u32 hidh;
127 u64 cid;
128 u8 uidf;
129 u8 uidl;
130 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200131} __attribute__((packed));
132
Joerg Roedelb65233a2008-07-11 17:14:21 +0200133/*
134 * An AMD IOMMU memory definition structure. It defines things like exclusion
135 * ranges for devices and regions that should be unity mapped.
136 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200137struct ivmd_header {
138 u8 type;
139 u8 flags;
140 u16 length;
141 u16 devid;
142 u16 aux;
143 u64 resv;
144 u64 range_start;
145 u64 range_length;
146} __attribute__((packed));
147
Joerg Roedelfefda112009-05-20 12:21:42 +0200148bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200149bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200150
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500151int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500152
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200153static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200154static bool __initdata amd_iommu_disabled;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400155static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200156
Joerg Roedelb65233a2008-07-11 17:14:21 +0200157u16 amd_iommu_last_bdf; /* largest PCI device id we have
158 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200159LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200160 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700161bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200162
Joerg Roedel2e228472008-07-11 17:14:31 +0200163LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200164 system */
165
Joerg Roedelbb527772009-11-20 14:31:51 +0100166/* Array to assign indices to IOMMUs*/
167struct amd_iommu *amd_iommus[MAX_IOMMUS];
168int amd_iommus_present;
169
Joerg Roedel318afd42009-11-23 18:32:38 +0100170/* IOMMUs have a non-present cache? */
171bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200172bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100173
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600174u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100175
Joerg Roedel400a28a2011-11-28 15:11:02 +0100176bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200177static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100178
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100179bool amd_iommu_force_isolation __read_mostly;
180
Joerg Roedelb65233a2008-07-11 17:14:21 +0200181/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100182 * List of protection domains - used during resume
183 */
184LIST_HEAD(amd_iommu_pd_list);
185spinlock_t amd_iommu_pd_lock;
186
187/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200188 * Pointer to the device table which is shared by all AMD IOMMUs
189 * it is indexed by the PCI device id or the HT unit id and contains
190 * information about the domain the device belongs to as well as the
191 * page table root pointer.
192 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200193struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200194
195/*
196 * The alias table is a driver specific data structure which contains the
197 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
198 * More than one device can share the same requestor id.
199 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200200u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200201
202/*
203 * The rlookup table is used to find the IOMMU which is responsible
204 * for a specific device. It is also indexed by the PCI device id.
205 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200206struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200207
208/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200209 * This table is used to find the irq remapping table for a given device id
210 * quickly.
211 */
212struct irq_remap_table **irq_lookup_table;
213
214/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200215 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200216 * to know which ones are already in use.
217 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200218unsigned long *amd_iommu_pd_alloc_bitmap;
219
Joerg Roedelb65233a2008-07-11 17:14:21 +0200220static u32 dev_table_size; /* size of the device table */
221static u32 alias_table_size; /* size of the alias table */
222static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200223
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200224enum iommu_init_state {
225 IOMMU_START_STATE,
226 IOMMU_IVRS_DETECTED,
227 IOMMU_ACPI_FINISHED,
228 IOMMU_ENABLED,
229 IOMMU_PCI_INIT,
230 IOMMU_INTERRUPTS_EN,
231 IOMMU_DMA_OPS,
232 IOMMU_INITIALIZED,
233 IOMMU_NOT_FOUND,
234 IOMMU_INIT_ERROR,
235};
236
Joerg Roedel235dacb2013-04-09 17:53:14 +0200237/* Early ioapic and hpet maps from kernel command line */
238#define EARLY_MAP_SIZE 4
239static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
240static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400241static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
242
Joerg Roedel235dacb2013-04-09 17:53:14 +0200243static int __initdata early_ioapic_map_size;
244static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400245static int __initdata early_acpihid_map_size;
246
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200247static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200248
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200249static enum iommu_init_state init_state = IOMMU_START_STATE;
250
Gerard Snitselaarae295142012-03-16 11:38:22 -0700251static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200252static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200253static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100254
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +0100255static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu,
256 u8 bank, u8 cntr, u8 fxn,
257 u64 *value, bool is_write);
258
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200259static inline void update_last_devid(u16 devid)
260{
261 if (devid > amd_iommu_last_bdf)
262 amd_iommu_last_bdf = devid;
263}
264
Joerg Roedelc5714842008-07-11 17:14:25 +0200265static inline unsigned long tbl_size(int entry_size)
266{
267 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100268 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200269
270 return 1UL << shift;
271}
272
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400273/* Access to l1 and l2 indexed register spaces */
274
275static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
276{
277 u32 val;
278
279 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
280 pci_read_config_dword(iommu->dev, 0xfc, &val);
281 return val;
282}
283
284static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
285{
286 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
287 pci_write_config_dword(iommu->dev, 0xfc, val);
288 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
289}
290
291static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
292{
293 u32 val;
294
295 pci_write_config_dword(iommu->dev, 0xf0, address);
296 pci_read_config_dword(iommu->dev, 0xf4, &val);
297 return val;
298}
299
300static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
301{
302 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
303 pci_write_config_dword(iommu->dev, 0xf4, val);
304}
305
Joerg Roedelb65233a2008-07-11 17:14:21 +0200306/****************************************************************************
307 *
308 * AMD IOMMU MMIO register space handling functions
309 *
310 * These functions are used to program the IOMMU device registers in
311 * MMIO space required for that driver.
312 *
313 ****************************************************************************/
314
315/*
316 * This function set the exclusion range in the IOMMU. DMA accesses to the
317 * exclusion range are passed through untranslated
318 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200319static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200320{
321 u64 start = iommu->exclusion_start & PAGE_MASK;
322 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
323 u64 entry;
324
325 if (!iommu->exclusion_start)
326 return;
327
328 entry = start | MMIO_EXCL_ENABLE_MASK;
329 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
330 &entry, sizeof(entry));
331
332 entry = limit;
333 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
334 &entry, sizeof(entry));
335}
336
Joerg Roedelb65233a2008-07-11 17:14:21 +0200337/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000338static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200339{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200340 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200341
342 BUG_ON(iommu->mmio_base == NULL);
343
344 entry = virt_to_phys(amd_iommu_dev_table);
345 entry |= (dev_table_size >> 12) - 1;
346 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
347 &entry, sizeof(entry));
348}
349
Joerg Roedelb65233a2008-07-11 17:14:21 +0200350/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200351static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200352{
353 u32 ctrl;
354
355 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
356 ctrl |= (1 << bit);
357 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
358}
359
Joerg Roedelca0207112009-10-28 18:02:26 +0100360static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200361{
362 u32 ctrl;
363
Joerg Roedel199d0d52008-09-17 16:45:59 +0200364 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200365 ctrl &= ~(1 << bit);
366 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
367}
368
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100369static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
370{
371 u32 ctrl;
372
373 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
374 ctrl &= ~CTRL_INV_TO_MASK;
375 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
376 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
377}
378
Joerg Roedelb65233a2008-07-11 17:14:21 +0200379/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200380static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200381{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200382 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200383}
384
Joerg Roedel92ac4322009-05-19 19:06:27 +0200385static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200386{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200387 /* Disable command buffer */
388 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
389
390 /* Disable event logging and event interrupts */
391 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
392 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
393
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500394 /* Disable IOMMU GA_LOG */
395 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
396 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
397
Chris Wrighta8c485b2009-06-15 15:53:45 +0200398 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200399 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200400}
401
Joerg Roedelb65233a2008-07-11 17:14:21 +0200402/*
403 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
404 * the system has one.
405 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500406static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200407{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500408 if (!request_mem_region(address, end, "amd_iommu")) {
409 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
410 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200411 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200412 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200413 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200414
Steven L Kinney30861dd2013-06-05 16:11:48 -0500415 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200416}
417
418static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
419{
420 if (iommu->mmio_base)
421 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500422 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200423}
424
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400425static inline u32 get_ivhd_header_size(struct ivhd_header *h)
426{
427 u32 size = 0;
428
429 switch (h->type) {
430 case 0x10:
431 size = 24;
432 break;
433 case 0x11:
434 case 0x40:
435 size = 40;
436 break;
437 }
438 return size;
439}
440
Joerg Roedelb65233a2008-07-11 17:14:21 +0200441/****************************************************************************
442 *
443 * The functions below belong to the first pass of AMD IOMMU ACPI table
444 * parsing. In this pass we try to find out the highest device id this
445 * code has to handle. Upon this information the size of the shared data
446 * structures is determined later.
447 *
448 ****************************************************************************/
449
450/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200451 * This function calculates the length of a given IVHD entry
452 */
453static inline int ivhd_entry_length(u8 *ivhd)
454{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400455 u32 type = ((struct ivhd_entry *)ivhd)->type;
456
457 if (type < 0x80) {
458 return 0x04 << (*ivhd >> 6);
459 } else if (type == IVHD_DEV_ACPI_HID) {
460 /* For ACPI_HID, offset 21 is uid len */
461 return *((u8 *)ivhd + 21) + 22;
462 }
463 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200464}
465
466/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200467 * After reading the highest device id from the IOMMU PCI capability header
468 * this function looks if there is a higher device id defined in the ACPI table
469 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200470static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
471{
472 u8 *p = (void *)h, *end = (void *)h;
473 struct ivhd_entry *dev;
474
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400475 u32 ivhd_size = get_ivhd_header_size(h);
476
477 if (!ivhd_size) {
478 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
479 return -EINVAL;
480 }
481
482 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200483 end += h->length;
484
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200485 while (p < end) {
486 dev = (struct ivhd_entry *)p;
487 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200488 case IVHD_DEV_ALL:
489 /* Use maximum BDF value for DEV_ALL */
490 update_last_devid(0xffff);
491 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200492 case IVHD_DEV_SELECT:
493 case IVHD_DEV_RANGE_END:
494 case IVHD_DEV_ALIAS:
495 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200496 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200497 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200498 break;
499 default:
500 break;
501 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200502 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200503 }
504
505 WARN_ON(p != end);
506
507 return 0;
508}
509
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400510static int __init check_ivrs_checksum(struct acpi_table_header *table)
511{
512 int i;
513 u8 checksum = 0, *p = (u8 *)table;
514
515 for (i = 0; i < table->length; ++i)
516 checksum += p[i];
517 if (checksum != 0) {
518 /* ACPI table corrupt */
519 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
520 return -ENODEV;
521 }
522
523 return 0;
524}
525
Joerg Roedelb65233a2008-07-11 17:14:21 +0200526/*
527 * Iterate over all IVHD entries in the ACPI table and find the highest device
528 * id which we need to handle. This is the first of three functions which parse
529 * the ACPI table. So we check the checksum here.
530 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200531static int __init find_last_devid_acpi(struct acpi_table_header *table)
532{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400533 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200534 struct ivhd_header *h;
535
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200536 p += IVRS_HEADER_LENGTH;
537
538 end += table->length;
539 while (p < end) {
540 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400541 if (h->type == amd_iommu_target_ivhd_type) {
542 int ret = find_last_devid_from_ivhd(h);
543
544 if (ret)
545 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200546 }
547 p += h->length;
548 }
549 WARN_ON(p != end);
550
551 return 0;
552}
553
Joerg Roedelb65233a2008-07-11 17:14:21 +0200554/****************************************************************************
555 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200556 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200557 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
558 * data structures, initialize the device/alias/rlookup table and also
559 * basically initialize the hardware.
560 *
561 ****************************************************************************/
562
563/*
564 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
565 * write commands to that buffer later and the IOMMU will execute them
566 * asynchronously
567 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200568static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200569{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200570 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
571 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200572
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200573 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200574}
575
576/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200577 * This function resets the command buffer if the IOMMU stopped fetching
578 * commands from it.
579 */
580void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
581{
582 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
583
584 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
585 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
586
587 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
588}
589
590/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200591 * This function writes the command buffer address to the hardware and
592 * enables it.
593 */
594static void iommu_enable_command_buffer(struct amd_iommu *iommu)
595{
596 u64 entry;
597
598 BUG_ON(iommu->cmd_buf == NULL);
599
600 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200601 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200602
Joerg Roedelb36ca912008-06-26 21:27:45 +0200603 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200604 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200605
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200606 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200607}
608
609static void __init free_command_buffer(struct amd_iommu *iommu)
610{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200611 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200612}
613
Joerg Roedel335503e2008-09-05 14:29:07 +0200614/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200615static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200616{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200617 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
618 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200619
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200620 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200621}
622
623static void iommu_enable_event_buffer(struct amd_iommu *iommu)
624{
625 u64 entry;
626
627 BUG_ON(iommu->evt_buf == NULL);
628
Joerg Roedel335503e2008-09-05 14:29:07 +0200629 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200630
Joerg Roedel335503e2008-09-05 14:29:07 +0200631 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
632 &entry, sizeof(entry));
633
Joerg Roedel090672072009-06-15 16:06:48 +0200634 /* set head and tail to zero manually */
635 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
636 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
637
Joerg Roedel58492e12009-05-04 18:41:16 +0200638 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200639}
640
641static void __init free_event_buffer(struct amd_iommu *iommu)
642{
643 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
644}
645
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100646/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200647static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100648{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200649 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
650 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100651
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200652 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100653}
654
655static void iommu_enable_ppr_log(struct amd_iommu *iommu)
656{
657 u64 entry;
658
659 if (iommu->ppr_log == NULL)
660 return;
661
662 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
663
664 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
665 &entry, sizeof(entry));
666
667 /* set head and tail to zero manually */
668 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
669 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
670
671 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
672 iommu_feature_enable(iommu, CONTROL_PPR_EN);
673}
674
675static void __init free_ppr_log(struct amd_iommu *iommu)
676{
677 if (iommu->ppr_log == NULL)
678 return;
679
680 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
681}
682
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500683static void free_ga_log(struct amd_iommu *iommu)
684{
685#ifdef CONFIG_IRQ_REMAP
686 if (iommu->ga_log)
687 free_pages((unsigned long)iommu->ga_log,
688 get_order(GA_LOG_SIZE));
689 if (iommu->ga_log_tail)
690 free_pages((unsigned long)iommu->ga_log_tail,
691 get_order(8));
692#endif
693}
694
695static int iommu_ga_log_enable(struct amd_iommu *iommu)
696{
697#ifdef CONFIG_IRQ_REMAP
698 u32 status, i;
699
700 if (!iommu->ga_log)
701 return -EINVAL;
702
703 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
704
705 /* Check if already running */
706 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
707 return 0;
708
709 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
710 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
711
712 for (i = 0; i < LOOP_TIMEOUT; ++i) {
713 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
714 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
715 break;
716 }
717
718 if (i >= LOOP_TIMEOUT)
719 return -EINVAL;
720#endif /* CONFIG_IRQ_REMAP */
721 return 0;
722}
723
724#ifdef CONFIG_IRQ_REMAP
725static int iommu_init_ga_log(struct amd_iommu *iommu)
726{
727 u64 entry;
728
729 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
730 return 0;
731
732 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
733 get_order(GA_LOG_SIZE));
734 if (!iommu->ga_log)
735 goto err_out;
736
737 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
738 get_order(8));
739 if (!iommu->ga_log_tail)
740 goto err_out;
741
742 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
743 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
744 &entry, sizeof(entry));
745 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
746 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
747 &entry, sizeof(entry));
748 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
749 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
750
751 return 0;
752err_out:
753 free_ga_log(iommu);
754 return -EINVAL;
755}
756#endif /* CONFIG_IRQ_REMAP */
757
758static int iommu_init_ga(struct amd_iommu *iommu)
759{
760 int ret = 0;
761
762#ifdef CONFIG_IRQ_REMAP
763 /* Note: We have already checked GASup from IVRS table.
764 * Now, we need to make sure that GAMSup is set.
765 */
766 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
767 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
768 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
769
770 ret = iommu_init_ga_log(iommu);
771#endif /* CONFIG_IRQ_REMAP */
772
773 return ret;
774}
775
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100776static void iommu_enable_gt(struct amd_iommu *iommu)
777{
778 if (!iommu_feature(iommu, FEATURE_GT))
779 return;
780
781 iommu_feature_enable(iommu, CONTROL_GT_EN);
782}
783
Joerg Roedelb65233a2008-07-11 17:14:21 +0200784/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200785static void set_dev_entry_bit(u16 devid, u8 bit)
786{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100787 int i = (bit >> 6) & 0x03;
788 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200789
Joerg Roedelee6c2862011-11-09 12:06:03 +0100790 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200791}
792
Joerg Roedelc5cca142009-10-09 18:31:20 +0200793static int get_dev_entry_bit(u16 devid, u8 bit)
794{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100795 int i = (bit >> 6) & 0x03;
796 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200797
Joerg Roedelee6c2862011-11-09 12:06:03 +0100798 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200799}
800
801
802void amd_iommu_apply_erratum_63(u16 devid)
803{
804 int sysmgt;
805
806 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
807 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
808
809 if (sysmgt == 0x01)
810 set_dev_entry_bit(devid, DEV_ENTRY_IW);
811}
812
Joerg Roedel5ff47892008-07-14 20:11:18 +0200813/* Writes the specific IOMMU for a device into the rlookup table */
814static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
815{
816 amd_iommu_rlookup_table[devid] = iommu;
817}
818
Joerg Roedelb65233a2008-07-11 17:14:21 +0200819/*
820 * This function takes the device specific flags read from the ACPI
821 * table and sets up the device table entry with that information
822 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200823static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
824 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200825{
826 if (flags & ACPI_DEVFLAG_INITPASS)
827 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
828 if (flags & ACPI_DEVFLAG_EXTINT)
829 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
830 if (flags & ACPI_DEVFLAG_NMI)
831 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
832 if (flags & ACPI_DEVFLAG_SYSMGT1)
833 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
834 if (flags & ACPI_DEVFLAG_SYSMGT2)
835 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
836 if (flags & ACPI_DEVFLAG_LINT0)
837 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
838 if (flags & ACPI_DEVFLAG_LINT1)
839 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200840
Joerg Roedelc5cca142009-10-09 18:31:20 +0200841 amd_iommu_apply_erratum_63(devid);
842
Joerg Roedel5ff47892008-07-14 20:11:18 +0200843 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200844}
845
Joerg Roedelc50e3242014-09-09 15:59:37 +0200846static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200847{
848 struct devid_map *entry;
849 struct list_head *list;
850
Joerg Roedel31cff672013-04-09 16:53:58 +0200851 if (type == IVHD_SPECIAL_IOAPIC)
852 list = &ioapic_map;
853 else if (type == IVHD_SPECIAL_HPET)
854 list = &hpet_map;
855 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200856 return -EINVAL;
857
Joerg Roedel31cff672013-04-09 16:53:58 +0200858 list_for_each_entry(entry, list, list) {
859 if (!(entry->id == id && entry->cmd_line))
860 continue;
861
862 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
863 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
864
Joerg Roedelc50e3242014-09-09 15:59:37 +0200865 *devid = entry->devid;
866
Joerg Roedel31cff672013-04-09 16:53:58 +0200867 return 0;
868 }
869
Joerg Roedel6efed632012-06-14 15:52:58 +0200870 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
871 if (!entry)
872 return -ENOMEM;
873
Joerg Roedel31cff672013-04-09 16:53:58 +0200874 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +0200875 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +0200876 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +0200877
878 list_add_tail(&entry->list, list);
879
880 return 0;
881}
882
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400883static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
884 bool cmd_line)
885{
886 struct acpihid_map_entry *entry;
887 struct list_head *list = &acpihid_map;
888
889 list_for_each_entry(entry, list, list) {
890 if (strcmp(entry->hid, hid) ||
891 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
892 !entry->cmd_line)
893 continue;
894
895 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
896 hid, uid);
897 *devid = entry->devid;
898 return 0;
899 }
900
901 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
902 if (!entry)
903 return -ENOMEM;
904
905 memcpy(entry->uid, uid, strlen(uid));
906 memcpy(entry->hid, hid, strlen(hid));
907 entry->devid = *devid;
908 entry->cmd_line = cmd_line;
909 entry->root_devid = (entry->devid & (~0x7));
910
911 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
912 entry->cmd_line ? "cmd" : "ivrs",
913 entry->hid, entry->uid, entry->root_devid);
914
915 list_add_tail(&entry->list, list);
916 return 0;
917}
918
Joerg Roedel235dacb2013-04-09 17:53:14 +0200919static int __init add_early_maps(void)
920{
921 int i, ret;
922
923 for (i = 0; i < early_ioapic_map_size; ++i) {
924 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
925 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200926 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200927 early_ioapic_map[i].cmd_line);
928 if (ret)
929 return ret;
930 }
931
932 for (i = 0; i < early_hpet_map_size; ++i) {
933 ret = add_special_device(IVHD_SPECIAL_HPET,
934 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200935 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200936 early_hpet_map[i].cmd_line);
937 if (ret)
938 return ret;
939 }
940
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400941 for (i = 0; i < early_acpihid_map_size; ++i) {
942 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
943 early_acpihid_map[i].uid,
944 &early_acpihid_map[i].devid,
945 early_acpihid_map[i].cmd_line);
946 if (ret)
947 return ret;
948 }
949
Joerg Roedel235dacb2013-04-09 17:53:14 +0200950 return 0;
951}
952
Joerg Roedelb65233a2008-07-11 17:14:21 +0200953/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200954 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +0200955 * it
956 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200957static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
958{
959 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
960
961 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
962 return;
963
964 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200965 /*
966 * We only can configure exclusion ranges per IOMMU, not
967 * per device. But we can enable the exclusion range per
968 * device. This is done here
969 */
Su Friendy2c16c9f2014-05-07 13:54:52 +0800970 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +0200971 iommu->exclusion_start = m->range_start;
972 iommu->exclusion_length = m->range_length;
973 }
974}
975
Joerg Roedelb65233a2008-07-11 17:14:21 +0200976/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200977 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
978 * initializes the hardware and our data structures with it.
979 */
Joerg Roedel6efed632012-06-14 15:52:58 +0200980static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200981 struct ivhd_header *h)
982{
983 u8 *p = (u8 *)h;
984 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +0200985 u16 devid = 0, devid_start = 0, devid_to = 0;
986 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200987 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200988 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400989 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200990 int ret;
991
992
993 ret = add_early_maps();
994 if (ret)
995 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200996
997 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +0200998 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200999 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001000 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001001
1002 /*
1003 * Done. Now parse the device entries
1004 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001005 ivhd_size = get_ivhd_header_size(h);
1006 if (!ivhd_size) {
1007 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1008 return -EINVAL;
1009 }
1010
1011 p += ivhd_size;
1012
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001013 end += h->length;
1014
Joerg Roedel42a698f2009-05-20 15:41:28 +02001015
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001016 while (p < end) {
1017 e = (struct ivhd_entry *)p;
1018 switch (e->type) {
1019 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001020
Joerg Roedel226e8892015-10-20 17:33:44 +02001021 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001022
Joerg Roedel226e8892015-10-20 17:33:44 +02001023 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1024 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001025 break;
1026 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001027
1028 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1029 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001030 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001031 PCI_SLOT(e->devid),
1032 PCI_FUNC(e->devid),
1033 e->flags);
1034
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001035 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001036 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001037 break;
1038 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001039
1040 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1041 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001042 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001043 PCI_SLOT(e->devid),
1044 PCI_FUNC(e->devid),
1045 e->flags);
1046
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001047 devid_start = e->devid;
1048 flags = e->flags;
1049 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001050 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001051 break;
1052 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001053
1054 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1055 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001056 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001057 PCI_SLOT(e->devid),
1058 PCI_FUNC(e->devid),
1059 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001060 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001061 PCI_SLOT(e->ext >> 8),
1062 PCI_FUNC(e->ext >> 8));
1063
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001064 devid = e->devid;
1065 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001066 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001067 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001068 amd_iommu_alias_table[devid] = devid_to;
1069 break;
1070 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001071
1072 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1073 "devid: %02x:%02x.%x flags: %02x "
1074 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001075 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001076 PCI_SLOT(e->devid),
1077 PCI_FUNC(e->devid),
1078 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001079 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001080 PCI_SLOT(e->ext >> 8),
1081 PCI_FUNC(e->ext >> 8));
1082
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001083 devid_start = e->devid;
1084 flags = e->flags;
1085 devid_to = e->ext >> 8;
1086 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001087 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001088 break;
1089 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001090
1091 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1092 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001093 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001094 PCI_SLOT(e->devid),
1095 PCI_FUNC(e->devid),
1096 e->flags, e->ext);
1097
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001098 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001099 set_dev_entry_from_acpi(iommu, devid, e->flags,
1100 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001101 break;
1102 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001103
1104 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1105 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001106 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001107 PCI_SLOT(e->devid),
1108 PCI_FUNC(e->devid),
1109 e->flags, e->ext);
1110
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001111 devid_start = e->devid;
1112 flags = e->flags;
1113 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001114 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001115 break;
1116 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001117
1118 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001119 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001120 PCI_SLOT(e->devid),
1121 PCI_FUNC(e->devid));
1122
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001123 devid = e->devid;
1124 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001125 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001126 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001127 set_dev_entry_from_acpi(iommu,
1128 devid_to, flags, ext_flags);
1129 }
1130 set_dev_entry_from_acpi(iommu, dev_i,
1131 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001132 }
1133 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001134 case IVHD_DEV_SPECIAL: {
1135 u8 handle, type;
1136 const char *var;
1137 u16 devid;
1138 int ret;
1139
1140 handle = e->ext & 0xff;
1141 devid = (e->ext >> 8) & 0xffff;
1142 type = (e->ext >> 24) & 0xff;
1143
1144 if (type == IVHD_SPECIAL_IOAPIC)
1145 var = "IOAPIC";
1146 else if (type == IVHD_SPECIAL_HPET)
1147 var = "HPET";
1148 else
1149 var = "UNKNOWN";
1150
1151 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1152 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001153 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001154 PCI_SLOT(devid),
1155 PCI_FUNC(devid));
1156
Joerg Roedelc50e3242014-09-09 15:59:37 +02001157 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001158 if (ret)
1159 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001160
1161 /*
1162 * add_special_device might update the devid in case a
1163 * command-line override is present. So call
1164 * set_dev_entry_from_acpi after add_special_device.
1165 */
1166 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1167
Joerg Roedel6efed632012-06-14 15:52:58 +02001168 break;
1169 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001170 case IVHD_DEV_ACPI_HID: {
1171 u16 devid;
1172 u8 hid[ACPIHID_HID_LEN] = {0};
1173 u8 uid[ACPIHID_UID_LEN] = {0};
1174 int ret;
1175
1176 if (h->type != 0x40) {
1177 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1178 e->type);
1179 break;
1180 }
1181
1182 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1183 hid[ACPIHID_HID_LEN - 1] = '\0';
1184
1185 if (!(*hid)) {
1186 pr_err(FW_BUG "Invalid HID.\n");
1187 break;
1188 }
1189
1190 switch (e->uidf) {
1191 case UID_NOT_PRESENT:
1192
1193 if (e->uidl != 0)
1194 pr_warn(FW_BUG "Invalid UID length.\n");
1195
1196 break;
1197 case UID_IS_INTEGER:
1198
1199 sprintf(uid, "%d", e->uid);
1200
1201 break;
1202 case UID_IS_CHARACTER:
1203
1204 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1205 uid[ACPIHID_UID_LEN - 1] = '\0';
1206
1207 break;
1208 default:
1209 break;
1210 }
1211
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001212 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001213 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1214 hid, uid,
1215 PCI_BUS_NUM(devid),
1216 PCI_SLOT(devid),
1217 PCI_FUNC(devid));
1218
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001219 flags = e->flags;
1220
1221 ret = add_acpi_hid_device(hid, uid, &devid, false);
1222 if (ret)
1223 return ret;
1224
1225 /*
1226 * add_special_device might update the devid in case a
1227 * command-line override is present. So call
1228 * set_dev_entry_from_acpi after add_special_device.
1229 */
1230 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1231
1232 break;
1233 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001234 default:
1235 break;
1236 }
1237
Joerg Roedelb514e552008-09-17 17:14:27 +02001238 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001239 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001240
1241 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001242}
1243
Joerg Roedele47d4022008-06-26 21:27:48 +02001244static void __init free_iommu_one(struct amd_iommu *iommu)
1245{
1246 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001247 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001248 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001249 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001250 iommu_unmap_mmio_space(iommu);
1251}
1252
1253static void __init free_iommu_all(void)
1254{
1255 struct amd_iommu *iommu, *next;
1256
Joerg Roedel3bd22172009-05-04 15:06:20 +02001257 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001258 list_del(&iommu->list);
1259 free_iommu_one(iommu);
1260 kfree(iommu);
1261 }
1262}
1263
Joerg Roedelb65233a2008-07-11 17:14:21 +02001264/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001265 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1266 * Workaround:
1267 * BIOS should disable L2B micellaneous clock gating by setting
1268 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1269 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001270static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001271{
1272 u32 value;
1273
1274 if ((boot_cpu_data.x86 != 0x15) ||
1275 (boot_cpu_data.x86_model < 0x10) ||
1276 (boot_cpu_data.x86_model > 0x1f))
1277 return;
1278
1279 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1280 pci_read_config_dword(iommu->dev, 0xf4, &value);
1281
1282 if (value & BIT(2))
1283 return;
1284
1285 /* Select NB indirect register 0x90 and enable writing */
1286 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1287
1288 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1289 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1290 dev_name(&iommu->dev->dev));
1291
1292 /* Clear the enable writing bit */
1293 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1294}
1295
1296/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001297 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1298 * Workaround:
1299 * BIOS should enable ATS write permission check by setting
1300 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1301 */
1302static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1303{
1304 u32 value;
1305
1306 if ((boot_cpu_data.x86 != 0x15) ||
1307 (boot_cpu_data.x86_model < 0x30) ||
1308 (boot_cpu_data.x86_model > 0x3f))
1309 return;
1310
1311 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1312 value = iommu_read_l2(iommu, 0x47);
1313
1314 if (value & BIT(0))
1315 return;
1316
1317 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1318 iommu_write_l2(iommu, 0x47, value | BIT(0));
1319
1320 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1321 dev_name(&iommu->dev->dev));
1322}
1323
1324/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001325 * This function clues the initialization function for one IOMMU
1326 * together and also allocates the command buffer and programs the
1327 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1328 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001329static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1330{
Joerg Roedel6efed632012-06-14 15:52:58 +02001331 int ret;
1332
Joerg Roedele47d4022008-06-26 21:27:48 +02001333 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001334
1335 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001336 list_add_tail(&iommu->list, &amd_iommu_list);
Joerg Roedelbb527772009-11-20 14:31:51 +01001337 iommu->index = amd_iommus_present++;
1338
1339 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1340 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1341 return -ENOSYS;
1342 }
1343
1344 /* Index is fine - add IOMMU to the array */
1345 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001346
1347 /*
1348 * Copy data from ACPI table entry to the iommu struct
1349 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001350 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001351 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001352 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001353 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001354
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001355 switch (h->type) {
1356 case 0x10:
1357 /* Check if IVHD EFR contains proper max banks/counters */
1358 if ((h->efr_attr != 0) &&
1359 ((h->efr_attr & (0xF << 13)) != 0) &&
1360 ((h->efr_attr & (0x3F << 17)) != 0))
1361 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1362 else
1363 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001364 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1365 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001366 break;
1367 case 0x11:
1368 case 0x40:
1369 if (h->efr_reg & (1 << 9))
1370 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1371 else
1372 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001373 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1374 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001375 break;
1376 default:
1377 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001378 }
1379
1380 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1381 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001382 if (!iommu->mmio_base)
1383 return -ENOMEM;
1384
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001385 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001386 return -ENOMEM;
1387
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001388 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001389 return -ENOMEM;
1390
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001391 iommu->int_enabled = false;
1392
Joerg Roedel6efed632012-06-14 15:52:58 +02001393 ret = init_iommu_from_acpi(iommu, h);
1394 if (ret)
1395 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001396
Jiang Liu7c71d302015-04-13 14:11:33 +08001397 ret = amd_iommu_create_irq_domain(iommu);
1398 if (ret)
1399 return ret;
1400
Joerg Roedelf6fec002012-06-21 16:51:25 +02001401 /*
1402 * Make sure IOMMU is not considered to translate itself. The IVRS
1403 * table tells us so, but this is a lie!
1404 */
1405 amd_iommu_rlookup_table[iommu->devid] = NULL;
1406
Joerg Roedel23c742d2012-06-12 11:47:34 +02001407 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001408}
1409
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001410/**
1411 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1412 * @ivrs Pointer to the IVRS header
1413 *
1414 * This function search through all IVDB of the maximum supported IVHD
1415 */
1416static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1417{
1418 u8 *base = (u8 *)ivrs;
1419 struct ivhd_header *ivhd = (struct ivhd_header *)
1420 (base + IVRS_HEADER_LENGTH);
1421 u8 last_type = ivhd->type;
1422 u16 devid = ivhd->devid;
1423
1424 while (((u8 *)ivhd - base < ivrs->length) &&
1425 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1426 u8 *p = (u8 *) ivhd;
1427
1428 if (ivhd->devid == devid)
1429 last_type = ivhd->type;
1430 ivhd = (struct ivhd_header *)(p + ivhd->length);
1431 }
1432
1433 return last_type;
1434}
1435
Joerg Roedelb65233a2008-07-11 17:14:21 +02001436/*
1437 * Iterates over all IOMMU entries in the ACPI table, allocates the
1438 * IOMMU structure and initializes it with init_iommu_one()
1439 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001440static int __init init_iommu_all(struct acpi_table_header *table)
1441{
1442 u8 *p = (u8 *)table, *end = (u8 *)table;
1443 struct ivhd_header *h;
1444 struct amd_iommu *iommu;
1445 int ret;
1446
Joerg Roedele47d4022008-06-26 21:27:48 +02001447 end += table->length;
1448 p += IVRS_HEADER_LENGTH;
1449
1450 while (p < end) {
1451 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001452 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001453
Joerg Roedelae908c22009-09-01 16:52:16 +02001454 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001455 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001456 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001457 PCI_FUNC(h->devid), h->cap_ptr,
1458 h->pci_seg, h->flags, h->info);
1459 DUMP_printk(" mmio-addr: %016llx\n",
1460 h->mmio_phys);
1461
Joerg Roedele47d4022008-06-26 21:27:48 +02001462 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001463 if (iommu == NULL)
1464 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001465
Joerg Roedele47d4022008-06-26 21:27:48 +02001466 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001467 if (ret)
1468 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001469 }
1470 p += h->length;
1471
1472 }
1473 WARN_ON(p != end);
1474
1475 return 0;
1476}
1477
Steven L Kinney30861dd2013-06-05 16:11:48 -05001478
1479static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1480{
1481 u64 val = 0xabcd, val2 = 0;
1482
1483 if (!iommu_feature(iommu, FEATURE_PC))
1484 return;
1485
1486 amd_iommu_pc_present = true;
1487
1488 /* Check if the performance counters can be written to */
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01001489 if ((0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val, true)) ||
1490 (0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001491 (val != val2)) {
1492 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1493 amd_iommu_pc_present = false;
1494 return;
1495 }
1496
1497 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1498
1499 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1500 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1501 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1502}
1503
Alex Williamson066f2e92014-06-12 16:12:37 -06001504static ssize_t amd_iommu_show_cap(struct device *dev,
1505 struct device_attribute *attr,
1506 char *buf)
1507{
1508 struct amd_iommu *iommu = dev_get_drvdata(dev);
1509 return sprintf(buf, "%x\n", iommu->cap);
1510}
1511static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1512
1513static ssize_t amd_iommu_show_features(struct device *dev,
1514 struct device_attribute *attr,
1515 char *buf)
1516{
1517 struct amd_iommu *iommu = dev_get_drvdata(dev);
1518 return sprintf(buf, "%llx\n", iommu->features);
1519}
1520static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1521
1522static struct attribute *amd_iommu_attrs[] = {
1523 &dev_attr_cap.attr,
1524 &dev_attr_features.attr,
1525 NULL,
1526};
1527
1528static struct attribute_group amd_iommu_group = {
1529 .name = "amd-iommu",
1530 .attrs = amd_iommu_attrs,
1531};
1532
1533static const struct attribute_group *amd_iommu_groups[] = {
1534 &amd_iommu_group,
1535 NULL,
1536};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001537
Joerg Roedel23c742d2012-06-12 11:47:34 +02001538static int iommu_init_pci(struct amd_iommu *iommu)
1539{
1540 int cap_ptr = iommu->cap_ptr;
1541 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001542 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001543
Shuah Khanc5081cd2013-02-27 17:07:19 -07001544 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001545 iommu->devid & 0xff);
1546 if (!iommu->dev)
1547 return -ENODEV;
1548
Jiang Liucbbc00b2015-10-09 22:07:31 +08001549 /* Prevent binding other PCI device drivers to IOMMU devices */
1550 iommu->dev->match_driver = false;
1551
Joerg Roedel23c742d2012-06-12 11:47:34 +02001552 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1553 &iommu->cap);
1554 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1555 &range);
1556 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1557 &misc);
1558
Joerg Roedel23c742d2012-06-12 11:47:34 +02001559 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1560 amd_iommu_iotlb_sup = false;
1561
1562 /* read extended feature bits */
1563 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1564 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1565
1566 iommu->features = ((u64)high << 32) | low;
1567
1568 if (iommu_feature(iommu, FEATURE_GT)) {
1569 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001570 u32 max_pasid;
1571 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001572
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001573 pasmax = iommu->features & FEATURE_PASID_MASK;
1574 pasmax >>= FEATURE_PASID_SHIFT;
1575 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001576
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001577 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1578
1579 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001580
1581 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1582 glxval >>= FEATURE_GLXVAL_SHIFT;
1583
1584 if (amd_iommu_max_glx_val == -1)
1585 amd_iommu_max_glx_val = glxval;
1586 else
1587 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1588 }
1589
1590 if (iommu_feature(iommu, FEATURE_GT) &&
1591 iommu_feature(iommu, FEATURE_PPR)) {
1592 iommu->is_iommu_v2 = true;
1593 amd_iommu_v2_present = true;
1594 }
1595
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001596 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1597 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001598
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001599 ret = iommu_init_ga(iommu);
1600 if (ret)
1601 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001602
Joerg Roedel23c742d2012-06-12 11:47:34 +02001603 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1604 amd_iommu_np_cache = true;
1605
Steven L Kinney30861dd2013-06-05 16:11:48 -05001606 init_iommu_perf_ctr(iommu);
1607
Joerg Roedel23c742d2012-06-12 11:47:34 +02001608 if (is_rd890_iommu(iommu->dev)) {
1609 int i, j;
1610
1611 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1612 PCI_DEVFN(0, 0));
1613
1614 /*
1615 * Some rd890 systems may not be fully reconfigured by the
1616 * BIOS, so it's necessary for us to store this information so
1617 * it can be reprogrammed on resume
1618 */
1619 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1620 &iommu->stored_addr_lo);
1621 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1622 &iommu->stored_addr_hi);
1623
1624 /* Low bit locks writes to configuration space */
1625 iommu->stored_addr_lo &= ~1;
1626
1627 for (i = 0; i < 6; i++)
1628 for (j = 0; j < 0x12; j++)
1629 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1630
1631 for (i = 0; i < 0x83; i++)
1632 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1633 }
1634
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001635 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001636 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001637
Alex Williamson066f2e92014-06-12 16:12:37 -06001638 iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu,
1639 amd_iommu_groups, "ivhd%d",
1640 iommu->index);
1641
Joerg Roedel23c742d2012-06-12 11:47:34 +02001642 return pci_enable_device(iommu->dev);
1643}
1644
Joerg Roedel4d121c32012-06-14 12:21:55 +02001645static void print_iommu_info(void)
1646{
1647 static const char * const feat_str[] = {
1648 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1649 "IA", "GA", "HE", "PC"
1650 };
1651 struct amd_iommu *iommu;
1652
1653 for_each_iommu(iommu) {
1654 int i;
1655
1656 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1657 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1658
1659 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001660 pr_info("AMD-Vi: Extended features (%#llx):\n",
1661 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001662 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001663 if (iommu_feature(iommu, (1ULL << i)))
1664 pr_cont(" %s", feat_str[i]);
1665 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001666
1667 if (iommu->features & FEATURE_GAM_VAPIC)
1668 pr_cont(" GA_vAPIC");
1669
Steven L Kinney30861dd2013-06-05 16:11:48 -05001670 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001671 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001672 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001673 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001674 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001675 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1676 pr_info("AMD-Vi: virtual APIC enabled\n");
1677 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001678}
1679
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001680static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001681{
1682 struct amd_iommu *iommu;
1683 int ret = 0;
1684
1685 for_each_iommu(iommu) {
1686 ret = iommu_init_pci(iommu);
1687 if (ret)
1688 break;
1689 }
1690
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001691 /*
1692 * Order is important here to make sure any unity map requirements are
1693 * fulfilled. The unity mappings are created and written to the device
1694 * table during the amd_iommu_init_api() call.
1695 *
1696 * After that we call init_device_table_dma() to make sure any
1697 * uninitialized DTE will block DMA, and in the end we flush the caches
1698 * of all IOMMUs to make sure the changes to the device table are
1699 * active.
1700 */
1701 ret = amd_iommu_init_api();
1702
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001703 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001704
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001705 for_each_iommu(iommu)
1706 iommu_flush_all_caches(iommu);
1707
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001708 if (!ret)
1709 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001710
Joerg Roedel23c742d2012-06-12 11:47:34 +02001711 return ret;
1712}
1713
Joerg Roedelb65233a2008-07-11 17:14:21 +02001714/****************************************************************************
1715 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001716 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001717 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001718 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1719 * pci_dev.
1720 *
1721 ****************************************************************************/
1722
Joerg Roedel9f800de2009-11-23 12:45:25 +01001723static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001724{
1725 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001726
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001727 r = pci_enable_msi(iommu->dev);
1728 if (r)
1729 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001730
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001731 r = request_threaded_irq(iommu->dev->irq,
1732 amd_iommu_int_handler,
1733 amd_iommu_int_thread,
1734 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001735 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001736
1737 if (r) {
1738 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001739 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001740 }
1741
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001742 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001743
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001744 return 0;
1745}
1746
Joerg Roedel05f92db2009-05-12 09:52:46 +02001747static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001748{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001749 int ret;
1750
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001751 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001752 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001753
Yijing Wang82fcfc62013-08-08 21:12:36 +08001754 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001755 ret = iommu_setup_msi(iommu);
1756 else
1757 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001758
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001759 if (ret)
1760 return ret;
1761
1762enable_faults:
1763 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1764
1765 if (iommu->ppr_log != NULL)
1766 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1767
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001768 iommu_ga_log_enable(iommu);
1769
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001770 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001771}
1772
1773/****************************************************************************
1774 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001775 * The next functions belong to the third pass of parsing the ACPI
1776 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001777 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001778 *
1779 ****************************************************************************/
1780
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001781static void __init free_unity_maps(void)
1782{
1783 struct unity_map_entry *entry, *next;
1784
1785 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1786 list_del(&entry->list);
1787 kfree(entry);
1788 }
1789}
1790
Joerg Roedelb65233a2008-07-11 17:14:21 +02001791/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001792static int __init init_exclusion_range(struct ivmd_header *m)
1793{
1794 int i;
1795
1796 switch (m->type) {
1797 case ACPI_IVMD_TYPE:
1798 set_device_exclusion_range(m->devid, m);
1799 break;
1800 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001801 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001802 set_device_exclusion_range(i, m);
1803 break;
1804 case ACPI_IVMD_TYPE_RANGE:
1805 for (i = m->devid; i <= m->aux; ++i)
1806 set_device_exclusion_range(i, m);
1807 break;
1808 default:
1809 break;
1810 }
1811
1812 return 0;
1813}
1814
Joerg Roedelb65233a2008-07-11 17:14:21 +02001815/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001816static int __init init_unity_map_range(struct ivmd_header *m)
1817{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001818 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001819 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001820
1821 e = kzalloc(sizeof(*e), GFP_KERNEL);
1822 if (e == NULL)
1823 return -ENOMEM;
1824
1825 switch (m->type) {
1826 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001827 kfree(e);
1828 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001829 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001830 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001831 e->devid_start = e->devid_end = m->devid;
1832 break;
1833 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001834 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001835 e->devid_start = 0;
1836 e->devid_end = amd_iommu_last_bdf;
1837 break;
1838 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001839 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001840 e->devid_start = m->devid;
1841 e->devid_end = m->aux;
1842 break;
1843 }
1844 e->address_start = PAGE_ALIGN(m->range_start);
1845 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1846 e->prot = m->flags >> 1;
1847
Joerg Roedel02acc432009-05-20 16:24:21 +02001848 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1849 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001850 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1851 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02001852 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1853 e->address_start, e->address_end, m->flags);
1854
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001855 list_add_tail(&e->list, &amd_iommu_unity_map);
1856
1857 return 0;
1858}
1859
Joerg Roedelb65233a2008-07-11 17:14:21 +02001860/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001861static int __init init_memory_definitions(struct acpi_table_header *table)
1862{
1863 u8 *p = (u8 *)table, *end = (u8 *)table;
1864 struct ivmd_header *m;
1865
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001866 end += table->length;
1867 p += IVRS_HEADER_LENGTH;
1868
1869 while (p < end) {
1870 m = (struct ivmd_header *)p;
1871 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1872 init_exclusion_range(m);
1873 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1874 init_unity_map_range(m);
1875
1876 p += m->length;
1877 }
1878
1879 return 0;
1880}
1881
Joerg Roedelb65233a2008-07-11 17:14:21 +02001882/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001883 * Init the device table to not allow DMA access for devices and
1884 * suppress all page faults
1885 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02001886static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001887{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001888 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001889
1890 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1891 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1892 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001893 }
1894}
1895
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001896static void __init uninit_device_table_dma(void)
1897{
1898 u32 devid;
1899
1900 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1901 amd_iommu_dev_table[devid].data[0] = 0ULL;
1902 amd_iommu_dev_table[devid].data[1] = 0ULL;
1903 }
1904}
1905
Joerg Roedel33f28c52012-06-15 18:03:31 +02001906static void init_device_table(void)
1907{
1908 u32 devid;
1909
1910 if (!amd_iommu_irq_remap)
1911 return;
1912
1913 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1914 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1915}
1916
Joerg Roedele9bf5192010-09-20 14:33:07 +02001917static void iommu_init_flags(struct amd_iommu *iommu)
1918{
1919 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1920 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1921 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1922
1923 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1924 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1925 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1926
1927 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1928 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1929 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1930
1931 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1932 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1933 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1934
1935 /*
1936 * make IOMMU memory accesses cache coherent
1937 */
1938 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001939
1940 /* Set IOTLB invalidation timeout to 1s */
1941 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001942}
1943
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001944static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001945{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001946 int i, j;
1947 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001948 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001949
1950 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001951 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001952 return;
1953
1954 /*
1955 * First, we need to ensure that the iommu is enabled. This is
1956 * controlled by a register in the northbridge
1957 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001958
1959 /* Select Northbridge indirect register 0x75 and enable writing */
1960 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1961 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1962
1963 /* Enable the iommu */
1964 if (!(ioc_feature_control & 0x1))
1965 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1966
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001967 /* Restore the iommu BAR */
1968 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1969 iommu->stored_addr_lo);
1970 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1971 iommu->stored_addr_hi);
1972
1973 /* Restore the l1 indirect regs for each of the 6 l1s */
1974 for (i = 0; i < 6; i++)
1975 for (j = 0; j < 0x12; j++)
1976 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1977
1978 /* Restore the l2 indirect regs */
1979 for (i = 0; i < 0x83; i++)
1980 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
1981
1982 /* Lock PCI setup registers */
1983 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1984 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02001985}
1986
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001987static void iommu_enable_ga(struct amd_iommu *iommu)
1988{
1989#ifdef CONFIG_IRQ_REMAP
1990 switch (amd_iommu_guest_ir) {
1991 case AMD_IOMMU_GUEST_IR_VAPIC:
1992 iommu_feature_enable(iommu, CONTROL_GAM_EN);
1993 /* Fall through */
1994 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
1995 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05001996 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001997 break;
1998 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05001999 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002000 break;
2001 }
2002#endif
2003}
2004
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002005/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002006 * This function finally enables all IOMMUs found in the system after
2007 * they have been initialized
2008 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002009static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002010{
2011 struct amd_iommu *iommu;
2012
Joerg Roedel3bd22172009-05-04 15:06:20 +02002013 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02002014 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002015 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02002016 iommu_set_device_table(iommu);
2017 iommu_enable_command_buffer(iommu);
2018 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002019 iommu_set_exclusion_range(iommu);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002020 iommu_enable_ga(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002021 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02002022 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002023 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002024
2025#ifdef CONFIG_IRQ_REMAP
2026 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2027 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2028#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002029}
2030
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002031static void enable_iommus_v2(void)
2032{
2033 struct amd_iommu *iommu;
2034
2035 for_each_iommu(iommu) {
2036 iommu_enable_ppr_log(iommu);
2037 iommu_enable_gt(iommu);
2038 }
2039}
2040
2041static void enable_iommus(void)
2042{
2043 early_enable_iommus();
2044
2045 enable_iommus_v2();
2046}
2047
Joerg Roedel92ac4322009-05-19 19:06:27 +02002048static void disable_iommus(void)
2049{
2050 struct amd_iommu *iommu;
2051
2052 for_each_iommu(iommu)
2053 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002054
2055#ifdef CONFIG_IRQ_REMAP
2056 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2057 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2058#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002059}
2060
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002061/*
2062 * Suspend/Resume support
2063 * disable suspend until real resume implemented
2064 */
2065
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002066static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002067{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002068 struct amd_iommu *iommu;
2069
2070 for_each_iommu(iommu)
2071 iommu_apply_resume_quirks(iommu);
2072
Joerg Roedel736501e2009-05-12 09:56:12 +02002073 /* re-load the hardware */
2074 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002075
2076 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002077}
2078
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002079static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002080{
Joerg Roedel736501e2009-05-12 09:56:12 +02002081 /* disable IOMMUs to go out of the way for BIOS */
2082 disable_iommus();
2083
2084 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002085}
2086
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002087static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002088 .suspend = amd_iommu_suspend,
2089 .resume = amd_iommu_resume,
2090};
2091
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002092static void __init free_on_init_error(void)
2093{
Lucas Stachebcfa282016-10-26 13:09:53 +02002094 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002095 free_pages((unsigned long)irq_lookup_table,
2096 get_order(rlookup_table_size));
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002097
Julia Lawalla5919892015-09-13 14:15:31 +02002098 kmem_cache_destroy(amd_iommu_irq_cache);
2099 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002100
2101 free_pages((unsigned long)amd_iommu_rlookup_table,
2102 get_order(rlookup_table_size));
2103
2104 free_pages((unsigned long)amd_iommu_alias_table,
2105 get_order(alias_table_size));
2106
2107 free_pages((unsigned long)amd_iommu_dev_table,
2108 get_order(dev_table_size));
2109
2110 free_iommu_all();
2111
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002112#ifdef CONFIG_GART_IOMMU
2113 /*
2114 * We failed to initialize the AMD IOMMU - try fallback to GART
2115 * if possible.
2116 */
2117 gart_iommu_init();
2118
2119#endif
2120}
2121
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002122/* SB IOAPIC is always on this device in AMD systems */
2123#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2124
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002125static bool __init check_ioapic_information(void)
2126{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002127 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002128 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002129 int idx;
2130
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002131 has_sb_ioapic = false;
2132 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002133
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002134 /*
2135 * If we have map overrides on the kernel command line the
2136 * messages in this function might not describe firmware bugs
2137 * anymore - so be careful
2138 */
2139 if (cmdline_maps)
2140 fw_bug = "";
2141
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002142 for (idx = 0; idx < nr_ioapics; idx++) {
2143 int devid, id = mpc_ioapic_id(idx);
2144
2145 devid = get_ioapic_devid(id);
2146 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002147 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2148 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002149 ret = false;
2150 } else if (devid == IOAPIC_SB_DEVID) {
2151 has_sb_ioapic = true;
2152 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002153 }
2154 }
2155
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002156 if (!has_sb_ioapic) {
2157 /*
2158 * We expect the SB IOAPIC to be listed in the IVRS
2159 * table. The system timer is connected to the SB IOAPIC
2160 * and if we don't have it in the list the system will
2161 * panic at boot time. This situation usually happens
2162 * when the BIOS is buggy and provides us the wrong
2163 * device id for the IOAPIC in the system.
2164 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002165 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002166 }
2167
2168 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002169 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002170
2171 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002172}
2173
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002174static void __init free_dma_resources(void)
2175{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002176 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2177 get_order(MAX_DOMAIN_ID/8));
2178
2179 free_unity_maps();
2180}
2181
Joerg Roedelb65233a2008-07-11 17:14:21 +02002182/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002183 * This is the hardware init function for AMD IOMMU in the system.
2184 * This function is called either from amd_iommu_init or from the interrupt
2185 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002186 *
2187 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002188 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002189 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002190 * 1 pass) Discover the most comprehensive IVHD type to use.
2191 *
2192 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002193 * Upon this information the size of the data structures is
2194 * determined that needs to be allocated.
2195 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002196 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002197 * information in the ACPI table about available AMD IOMMUs
2198 * in the system. It also maps the PCI devices in the
2199 * system to specific IOMMUs
2200 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002201 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002202 * initialized we update them with information about memory
2203 * remapping requirements parsed out of the ACPI table in
2204 * this last pass.
2205 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002206 * After everything is set up the IOMMUs are enabled and the necessary
2207 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002208 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002209static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002210{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002211 struct acpi_table_header *ivrs_base;
2212 acpi_size ivrs_size;
2213 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002214 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002215
Joerg Roedel643511b2012-06-12 12:09:35 +02002216 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002217 return -ENODEV;
2218
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002219 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
2220 if (status == AE_NOT_FOUND)
2221 return -ENODEV;
2222 else if (ACPI_FAILURE(status)) {
2223 const char *err = acpi_format_exception(status);
2224 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2225 return -EINVAL;
2226 }
2227
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002228 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002229 * Validate checksum here so we don't need to do it when
2230 * we actually parse the table
2231 */
2232 ret = check_ivrs_checksum(ivrs_base);
2233 if (ret)
2234 return ret;
2235
2236 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2237 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2238
2239 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002240 * First parse ACPI tables to find the largest Bus/Dev/Func
2241 * we need to handle. Upon this information the shared data
2242 * structures for the IOMMUs in the system will be allocated
2243 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002244 ret = find_last_devid_acpi(ivrs_base);
2245 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002246 goto out;
2247
Joerg Roedelc5714842008-07-11 17:14:25 +02002248 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2249 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2250 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002251
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002252 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002253 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002254 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002255 get_order(dev_table_size));
2256 if (amd_iommu_dev_table == NULL)
2257 goto out;
2258
2259 /*
2260 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2261 * IOMMU see for that device
2262 */
2263 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2264 get_order(alias_table_size));
2265 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002266 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002267
2268 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002269 amd_iommu_rlookup_table = (void *)__get_free_pages(
2270 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002271 get_order(rlookup_table_size));
2272 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002273 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002274
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002275 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2276 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002277 get_order(MAX_DOMAIN_ID/8));
2278 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002279 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002280
2281 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002282 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002283 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002284 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002285 amd_iommu_alias_table[i] = i;
2286
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002287 /*
2288 * never allocate domain 0 because its used as the non-allocated and
2289 * error value placeholder
2290 */
Baoquan He5c87f622016-09-15 16:50:51 +08002291 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002292
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002293 spin_lock_init(&amd_iommu_pd_lock);
2294
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002295 /*
2296 * now the data structures are allocated and basically initialized
2297 * start the real acpi table scan
2298 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002299 ret = init_iommu_all(ivrs_base);
2300 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002301 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002302
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002303 if (amd_iommu_irq_remap)
2304 amd_iommu_irq_remap = check_ioapic_information();
2305
Joerg Roedel05152a02012-06-15 16:53:51 +02002306 if (amd_iommu_irq_remap) {
2307 /*
2308 * Interrupt remapping enabled, create kmem_cache for the
2309 * remapping tables.
2310 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002311 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002312 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2313 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2314 else
2315 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002316 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002317 remap_cache_sz,
2318 IRQ_TABLE_ALIGNMENT,
2319 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002320 if (!amd_iommu_irq_cache)
2321 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002322
2323 irq_lookup_table = (void *)__get_free_pages(
2324 GFP_KERNEL | __GFP_ZERO,
2325 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002326 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2327 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002328 if (!irq_lookup_table)
2329 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002330 }
2331
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002332 ret = init_memory_definitions(ivrs_base);
2333 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002334 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002335
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002336 /* init the device table */
2337 init_device_table();
2338
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002339out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002340 /* Don't leak any ACPI memory */
2341 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
2342 ivrs_base = NULL;
2343
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002344 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002345}
2346
Gerard Snitselaarae295142012-03-16 11:38:22 -07002347static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002348{
2349 struct amd_iommu *iommu;
2350 int ret = 0;
2351
2352 for_each_iommu(iommu) {
2353 ret = iommu_init_msi(iommu);
2354 if (ret)
2355 goto out;
2356 }
2357
2358out:
2359 return ret;
2360}
2361
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002362static bool detect_ivrs(void)
2363{
2364 struct acpi_table_header *ivrs_base;
2365 acpi_size ivrs_size;
2366 acpi_status status;
2367
2368 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
2369 if (status == AE_NOT_FOUND)
2370 return false;
2371 else if (ACPI_FAILURE(status)) {
2372 const char *err = acpi_format_exception(status);
2373 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2374 return false;
2375 }
2376
2377 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
2378
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002379 /* Make sure ACS will be enabled during PCI probe */
2380 pci_request_acs();
2381
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002382 return true;
2383}
2384
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002385/****************************************************************************
2386 *
2387 * AMD IOMMU Initialization State Machine
2388 *
2389 ****************************************************************************/
2390
2391static int __init state_next(void)
2392{
2393 int ret = 0;
2394
2395 switch (init_state) {
2396 case IOMMU_START_STATE:
2397 if (!detect_ivrs()) {
2398 init_state = IOMMU_NOT_FOUND;
2399 ret = -ENODEV;
2400 } else {
2401 init_state = IOMMU_IVRS_DETECTED;
2402 }
2403 break;
2404 case IOMMU_IVRS_DETECTED:
2405 ret = early_amd_iommu_init();
2406 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2407 break;
2408 case IOMMU_ACPI_FINISHED:
2409 early_enable_iommus();
2410 register_syscore_ops(&amd_iommu_syscore_ops);
2411 x86_platform.iommu_shutdown = disable_iommus;
2412 init_state = IOMMU_ENABLED;
2413 break;
2414 case IOMMU_ENABLED:
2415 ret = amd_iommu_init_pci();
2416 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2417 enable_iommus_v2();
2418 break;
2419 case IOMMU_PCI_INIT:
2420 ret = amd_iommu_enable_interrupts();
2421 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2422 break;
2423 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002424 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002425 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2426 break;
2427 case IOMMU_DMA_OPS:
2428 init_state = IOMMU_INITIALIZED;
2429 break;
2430 case IOMMU_INITIALIZED:
2431 /* Nothing to do */
2432 break;
2433 case IOMMU_NOT_FOUND:
2434 case IOMMU_INIT_ERROR:
2435 /* Error states => do nothing */
2436 ret = -EINVAL;
2437 break;
2438 default:
2439 /* Unknown state */
2440 BUG();
2441 }
2442
2443 return ret;
2444}
2445
2446static int __init iommu_go_to_state(enum iommu_init_state state)
2447{
2448 int ret = 0;
2449
2450 while (init_state != state) {
2451 ret = state_next();
2452 if (init_state == IOMMU_NOT_FOUND ||
2453 init_state == IOMMU_INIT_ERROR)
2454 break;
2455 }
2456
2457 return ret;
2458}
2459
Joerg Roedel6b474b82012-06-26 16:46:04 +02002460#ifdef CONFIG_IRQ_REMAP
2461int __init amd_iommu_prepare(void)
2462{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002463 int ret;
2464
Jiang Liu7fa1c842015-01-07 15:31:42 +08002465 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002466
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002467 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2468 if (ret)
2469 return ret;
2470 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002471}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002472
Joerg Roedel6b474b82012-06-26 16:46:04 +02002473int __init amd_iommu_enable(void)
2474{
2475 int ret;
2476
2477 ret = iommu_go_to_state(IOMMU_ENABLED);
2478 if (ret)
2479 return ret;
2480
2481 irq_remapping_enabled = 1;
2482
2483 return 0;
2484}
2485
2486void amd_iommu_disable(void)
2487{
2488 amd_iommu_suspend();
2489}
2490
2491int amd_iommu_reenable(int mode)
2492{
2493 amd_iommu_resume();
2494
2495 return 0;
2496}
2497
2498int __init amd_iommu_enable_faulting(void)
2499{
2500 /* We enable MSI later when PCI is initialized */
2501 return 0;
2502}
2503#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002504
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002505/*
2506 * This is the core init function for AMD IOMMU hardware in the system.
2507 * This function is called from the generic x86 DMA layer initialization
2508 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002509 */
2510static int __init amd_iommu_init(void)
2511{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002512 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002513
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002514 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2515 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002516 free_dma_resources();
2517 if (!irq_remapping_enabled) {
2518 disable_iommus();
2519 free_on_init_error();
2520 } else {
2521 struct amd_iommu *iommu;
2522
2523 uninit_device_table_dma();
2524 for_each_iommu(iommu)
2525 iommu_flush_all_caches(iommu);
2526 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002527 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002528
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002529 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002530}
2531
Joerg Roedelb65233a2008-07-11 17:14:21 +02002532/****************************************************************************
2533 *
2534 * Early detect code. This code runs at IOMMU detection time in the DMA
2535 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2536 * IOMMUs
2537 *
2538 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002539int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002540{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002541 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002542
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002543 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002544 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002545
Joerg Roedela5235722010-05-11 17:12:33 +02002546 if (amd_iommu_disabled)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002547 return -ENODEV;
Joerg Roedela5235722010-05-11 17:12:33 +02002548
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002549 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2550 if (ret)
2551 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002552
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002553 amd_iommu_detected = true;
2554 iommu_detected = 1;
2555 x86_init.iommu.iommu_init = amd_iommu_init;
2556
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002557 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002558}
2559
Joerg Roedelb65233a2008-07-11 17:14:21 +02002560/****************************************************************************
2561 *
2562 * Parsing functions for the AMD IOMMU specific kernel command line
2563 * options.
2564 *
2565 ****************************************************************************/
2566
Joerg Roedelfefda112009-05-20 12:21:42 +02002567static int __init parse_amd_iommu_dump(char *str)
2568{
2569 amd_iommu_dump = true;
2570
2571 return 1;
2572}
2573
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002574static int __init parse_amd_iommu_intr(char *str)
2575{
2576 for (; *str; ++str) {
2577 if (strncmp(str, "legacy", 6) == 0) {
2578 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2579 break;
2580 }
2581 if (strncmp(str, "vapic", 5) == 0) {
2582 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2583 break;
2584 }
2585 }
2586 return 1;
2587}
2588
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002589static int __init parse_amd_iommu_options(char *str)
2590{
2591 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002592 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002593 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002594 if (strncmp(str, "off", 3) == 0)
2595 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002596 if (strncmp(str, "force_isolation", 15) == 0)
2597 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002598 }
2599
2600 return 1;
2601}
2602
Joerg Roedel440e89982013-04-09 16:35:28 +02002603static int __init parse_ivrs_ioapic(char *str)
2604{
2605 unsigned int bus, dev, fn;
2606 int ret, id, i;
2607 u16 devid;
2608
2609 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2610
2611 if (ret != 4) {
2612 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2613 return 1;
2614 }
2615
2616 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2617 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2618 str);
2619 return 1;
2620 }
2621
2622 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2623
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002624 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002625 i = early_ioapic_map_size++;
2626 early_ioapic_map[i].id = id;
2627 early_ioapic_map[i].devid = devid;
2628 early_ioapic_map[i].cmd_line = true;
2629
2630 return 1;
2631}
2632
2633static int __init parse_ivrs_hpet(char *str)
2634{
2635 unsigned int bus, dev, fn;
2636 int ret, id, i;
2637 u16 devid;
2638
2639 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2640
2641 if (ret != 4) {
2642 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2643 return 1;
2644 }
2645
2646 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2647 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2648 str);
2649 return 1;
2650 }
2651
2652 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2653
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002654 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002655 i = early_hpet_map_size++;
2656 early_hpet_map[i].id = id;
2657 early_hpet_map[i].devid = devid;
2658 early_hpet_map[i].cmd_line = true;
2659
2660 return 1;
2661}
2662
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002663static int __init parse_ivrs_acpihid(char *str)
2664{
2665 u32 bus, dev, fn;
2666 char *hid, *uid, *p;
2667 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2668 int ret, i;
2669
2670 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2671 if (ret != 4) {
2672 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2673 return 1;
2674 }
2675
2676 p = acpiid;
2677 hid = strsep(&p, ":");
2678 uid = p;
2679
2680 if (!hid || !(*hid) || !uid) {
2681 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2682 return 1;
2683 }
2684
2685 i = early_acpihid_map_size++;
2686 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2687 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2688 early_acpihid_map[i].devid =
2689 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2690 early_acpihid_map[i].cmd_line = true;
2691
2692 return 1;
2693}
2694
Joerg Roedel440e89982013-04-09 16:35:28 +02002695__setup("amd_iommu_dump", parse_amd_iommu_dump);
2696__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002697__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002698__setup("ivrs_ioapic", parse_ivrs_ioapic);
2699__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002700__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002701
2702IOMMU_INIT_FINISH(amd_iommu_detect,
2703 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002704 NULL,
2705 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002706
2707bool amd_iommu_v2_supported(void)
2708{
2709 return amd_iommu_v2_present;
2710}
2711EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002712
2713/****************************************************************************
2714 *
2715 * IOMMU EFR Performance Counter support functionality. This code allows
2716 * access to the IOMMU PC functionality.
2717 *
2718 ****************************************************************************/
2719
2720u8 amd_iommu_pc_get_max_banks(u16 devid)
2721{
2722 struct amd_iommu *iommu;
2723 u8 ret = 0;
2724
2725 /* locate the iommu governing the devid */
2726 iommu = amd_iommu_rlookup_table[devid];
2727 if (iommu)
2728 ret = iommu->max_banks;
2729
2730 return ret;
2731}
2732EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2733
2734bool amd_iommu_pc_supported(void)
2735{
2736 return amd_iommu_pc_present;
2737}
2738EXPORT_SYMBOL(amd_iommu_pc_supported);
2739
2740u8 amd_iommu_pc_get_max_counters(u16 devid)
2741{
2742 struct amd_iommu *iommu;
2743 u8 ret = 0;
2744
2745 /* locate the iommu governing the devid */
2746 iommu = amd_iommu_rlookup_table[devid];
2747 if (iommu)
2748 ret = iommu->max_counters;
2749
2750 return ret;
2751}
2752EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2753
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002754static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu,
2755 u8 bank, u8 cntr, u8 fxn,
Steven L Kinney30861dd2013-06-05 16:11:48 -05002756 u64 *value, bool is_write)
2757{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002758 u32 offset;
2759 u32 max_offset_lim;
2760
Steven L Kinney30861dd2013-06-05 16:11:48 -05002761 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002762 if (WARN_ON((fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002763 return -ENODEV;
2764
2765 offset = (u32)(((0x40|bank) << 12) | (cntr << 8) | fxn);
2766
2767 /* Limit the offset to the hw defined mmio region aperture */
2768 max_offset_lim = (u32)(((0x40|iommu->max_banks) << 12) |
2769 (iommu->max_counters << 8) | 0x28);
2770 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2771 (offset > max_offset_lim))
2772 return -EINVAL;
2773
2774 if (is_write) {
2775 writel((u32)*value, iommu->mmio_base + offset);
2776 writel((*value >> 32), iommu->mmio_base + offset + 4);
2777 } else {
2778 *value = readl(iommu->mmio_base + offset + 4);
2779 *value <<= 32;
2780 *value = readl(iommu->mmio_base + offset);
2781 }
2782
2783 return 0;
2784}
2785EXPORT_SYMBOL(amd_iommu_pc_get_set_reg_val);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002786
2787int amd_iommu_pc_get_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn,
2788 u64 *value, bool is_write)
2789{
2790 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
2791
2792 /* Make sure the IOMMU PC resource is available */
2793 if (!amd_iommu_pc_present || iommu == NULL)
2794 return -ENODEV;
2795
2796 return iommu_pc_get_set_reg_val(iommu, bank, cntr, fxn,
2797 value, is_write);
2798}