blob: c113e46fdc3ab9711d1cbec78d8dd24ee5949bab [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>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020031#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090032#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010033#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090034#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040035#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020036#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020037#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020038
39#include "amd_iommu_proto.h"
40#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020041#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020042
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020043/*
44 * definitions for the ACPI scanning code
45 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020046#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020047
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040048#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020049#define ACPI_IVMD_TYPE_ALL 0x20
50#define ACPI_IVMD_TYPE 0x21
51#define ACPI_IVMD_TYPE_RANGE 0x22
52
53#define IVHD_DEV_ALL 0x01
54#define IVHD_DEV_SELECT 0x02
55#define IVHD_DEV_SELECT_RANGE_START 0x03
56#define IVHD_DEV_RANGE_END 0x04
57#define IVHD_DEV_ALIAS 0x42
58#define IVHD_DEV_ALIAS_RANGE 0x43
59#define IVHD_DEV_EXT_SELECT 0x46
60#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020061#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040062#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020063
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040064#define UID_NOT_PRESENT 0
65#define UID_IS_INTEGER 1
66#define UID_IS_CHARACTER 2
67
Joerg Roedel6efed632012-06-14 15:52:58 +020068#define IVHD_SPECIAL_IOAPIC 1
69#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020070
Joerg Roedel6da73422009-05-04 11:44:38 +020071#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
72#define IVHD_FLAG_PASSPW_EN_MASK 0x02
73#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
74#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020075
76#define IVMD_FLAG_EXCL_RANGE 0x08
77#define IVMD_FLAG_UNITY_MAP 0x01
78
79#define ACPI_DEVFLAG_INITPASS 0x01
80#define ACPI_DEVFLAG_EXTINT 0x02
81#define ACPI_DEVFLAG_NMI 0x04
82#define ACPI_DEVFLAG_SYSMGT1 0x10
83#define ACPI_DEVFLAG_SYSMGT2 0x20
84#define ACPI_DEVFLAG_LINT0 0x40
85#define ACPI_DEVFLAG_LINT1 0x80
86#define ACPI_DEVFLAG_ATSDIS 0x10000000
87
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050088#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020089/*
90 * ACPI table definitions
91 *
92 * These data structures are laid over the table to parse the important values
93 * out of it.
94 */
95
96/*
97 * structure describing one IOMMU in the ACPI table. Typically followed by one
98 * or more ivhd_entrys.
99 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200100struct ivhd_header {
101 u8 type;
102 u8 flags;
103 u16 length;
104 u16 devid;
105 u16 cap_ptr;
106 u64 mmio_phys;
107 u16 pci_seg;
108 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400109 u32 efr_attr;
110
111 /* Following only valid on IVHD type 11h and 40h */
112 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
113 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200114} __attribute__((packed));
115
Joerg Roedelb65233a2008-07-11 17:14:21 +0200116/*
117 * A device entry describing which devices a specific IOMMU translates and
118 * which requestor ids they use.
119 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200120struct ivhd_entry {
121 u8 type;
122 u16 devid;
123 u8 flags;
124 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400125 u32 hidh;
126 u64 cid;
127 u8 uidf;
128 u8 uidl;
129 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200130} __attribute__((packed));
131
Joerg Roedelb65233a2008-07-11 17:14:21 +0200132/*
133 * An AMD IOMMU memory definition structure. It defines things like exclusion
134 * ranges for devices and regions that should be unity mapped.
135 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200136struct ivmd_header {
137 u8 type;
138 u8 flags;
139 u16 length;
140 u16 devid;
141 u16 aux;
142 u64 resv;
143 u64 range_start;
144 u64 range_length;
145} __attribute__((packed));
146
Joerg Roedelfefda112009-05-20 12:21:42 +0200147bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200148bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200149
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500150int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500151
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200152static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200153static bool __initdata amd_iommu_disabled;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400154static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200155
Joerg Roedelb65233a2008-07-11 17:14:21 +0200156u16 amd_iommu_last_bdf; /* largest PCI device id we have
157 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200158LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200159 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700160bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200161
Joerg Roedel2e228472008-07-11 17:14:31 +0200162LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200163 system */
164
Joerg Roedelbb527772009-11-20 14:31:51 +0100165/* Array to assign indices to IOMMUs*/
166struct amd_iommu *amd_iommus[MAX_IOMMUS];
167int amd_iommus_present;
168
Joerg Roedel318afd42009-11-23 18:32:38 +0100169/* IOMMUs have a non-present cache? */
170bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200171bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100172
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600173u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100174
Joerg Roedel400a28a2011-11-28 15:11:02 +0100175bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200176static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100177
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100178bool amd_iommu_force_isolation __read_mostly;
179
Joerg Roedelb65233a2008-07-11 17:14:21 +0200180/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100181 * List of protection domains - used during resume
182 */
183LIST_HEAD(amd_iommu_pd_list);
184spinlock_t amd_iommu_pd_lock;
185
186/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200187 * Pointer to the device table which is shared by all AMD IOMMUs
188 * it is indexed by the PCI device id or the HT unit id and contains
189 * information about the domain the device belongs to as well as the
190 * page table root pointer.
191 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200192struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200193
194/*
195 * The alias table is a driver specific data structure which contains the
196 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
197 * More than one device can share the same requestor id.
198 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200199u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200200
201/*
202 * The rlookup table is used to find the IOMMU which is responsible
203 * for a specific device. It is also indexed by the PCI device id.
204 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200205struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200206
207/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200208 * This table is used to find the irq remapping table for a given device id
209 * quickly.
210 */
211struct irq_remap_table **irq_lookup_table;
212
213/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200214 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200215 * to know which ones are already in use.
216 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200217unsigned long *amd_iommu_pd_alloc_bitmap;
218
Joerg Roedelb65233a2008-07-11 17:14:21 +0200219static u32 dev_table_size; /* size of the device table */
220static u32 alias_table_size; /* size of the alias table */
221static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200222
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200223enum iommu_init_state {
224 IOMMU_START_STATE,
225 IOMMU_IVRS_DETECTED,
226 IOMMU_ACPI_FINISHED,
227 IOMMU_ENABLED,
228 IOMMU_PCI_INIT,
229 IOMMU_INTERRUPTS_EN,
230 IOMMU_DMA_OPS,
231 IOMMU_INITIALIZED,
232 IOMMU_NOT_FOUND,
233 IOMMU_INIT_ERROR,
234};
235
Joerg Roedel235dacb2013-04-09 17:53:14 +0200236/* Early ioapic and hpet maps from kernel command line */
237#define EARLY_MAP_SIZE 4
238static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
239static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400240static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
241
Joerg Roedel235dacb2013-04-09 17:53:14 +0200242static int __initdata early_ioapic_map_size;
243static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400244static int __initdata early_acpihid_map_size;
245
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200246static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200247
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200248static enum iommu_init_state init_state = IOMMU_START_STATE;
249
Gerard Snitselaarae295142012-03-16 11:38:22 -0700250static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200251static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200252static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100253
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +0100254static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu,
255 u8 bank, u8 cntr, u8 fxn,
256 u64 *value, bool is_write);
257
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200258static inline void update_last_devid(u16 devid)
259{
260 if (devid > amd_iommu_last_bdf)
261 amd_iommu_last_bdf = devid;
262}
263
Joerg Roedelc5714842008-07-11 17:14:25 +0200264static inline unsigned long tbl_size(int entry_size)
265{
266 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100267 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200268
269 return 1UL << shift;
270}
271
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400272/* Access to l1 and l2 indexed register spaces */
273
274static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
275{
276 u32 val;
277
278 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
279 pci_read_config_dword(iommu->dev, 0xfc, &val);
280 return val;
281}
282
283static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
284{
285 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
286 pci_write_config_dword(iommu->dev, 0xfc, val);
287 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
288}
289
290static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
291{
292 u32 val;
293
294 pci_write_config_dword(iommu->dev, 0xf0, address);
295 pci_read_config_dword(iommu->dev, 0xf4, &val);
296 return val;
297}
298
299static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
300{
301 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
302 pci_write_config_dword(iommu->dev, 0xf4, val);
303}
304
Joerg Roedelb65233a2008-07-11 17:14:21 +0200305/****************************************************************************
306 *
307 * AMD IOMMU MMIO register space handling functions
308 *
309 * These functions are used to program the IOMMU device registers in
310 * MMIO space required for that driver.
311 *
312 ****************************************************************************/
313
314/*
315 * This function set the exclusion range in the IOMMU. DMA accesses to the
316 * exclusion range are passed through untranslated
317 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200318static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200319{
320 u64 start = iommu->exclusion_start & PAGE_MASK;
Joerg Roedel6f54cf12019-04-12 12:50:31 +0200321 u64 limit = (start + iommu->exclusion_length - 1) & PAGE_MASK;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200322 u64 entry;
323
324 if (!iommu->exclusion_start)
325 return;
326
327 entry = start | MMIO_EXCL_ENABLE_MASK;
328 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
329 &entry, sizeof(entry));
330
331 entry = limit;
332 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
333 &entry, sizeof(entry));
334}
335
Joerg Roedelb65233a2008-07-11 17:14:21 +0200336/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000337static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200338{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200339 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200340
341 BUG_ON(iommu->mmio_base == NULL);
342
343 entry = virt_to_phys(amd_iommu_dev_table);
344 entry |= (dev_table_size >> 12) - 1;
345 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
346 &entry, sizeof(entry));
347}
348
Joerg Roedelb65233a2008-07-11 17:14:21 +0200349/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200350static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200351{
352 u32 ctrl;
353
354 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
355 ctrl |= (1 << bit);
356 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
357}
358
Joerg Roedelca0207112009-10-28 18:02:26 +0100359static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200360{
361 u32 ctrl;
362
Joerg Roedel199d0d52008-09-17 16:45:59 +0200363 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200364 ctrl &= ~(1 << bit);
365 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
366}
367
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100368static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
369{
370 u32 ctrl;
371
372 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
373 ctrl &= ~CTRL_INV_TO_MASK;
374 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
375 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
376}
377
Joerg Roedelb65233a2008-07-11 17:14:21 +0200378/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200379static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200380{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200381 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200382}
383
Joerg Roedel92ac4322009-05-19 19:06:27 +0200384static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200385{
Kevin Mitchelld1b09d42019-06-12 14:52:03 -0700386 if (!iommu->mmio_base)
387 return;
388
Chris Wrighta8c485b2009-06-15 15:53:45 +0200389 /* Disable command buffer */
390 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
391
392 /* Disable event logging and event interrupts */
393 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
394 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
395
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500396 /* Disable IOMMU GA_LOG */
397 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
398 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
399
Chris Wrighta8c485b2009-06-15 15:53:45 +0200400 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200401 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200402}
403
Joerg Roedelb65233a2008-07-11 17:14:21 +0200404/*
405 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
406 * the system has one.
407 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500408static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200409{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500410 if (!request_mem_region(address, end, "amd_iommu")) {
411 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
412 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200413 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200414 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200415 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200416
Steven L Kinney30861dd2013-06-05 16:11:48 -0500417 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200418}
419
420static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
421{
422 if (iommu->mmio_base)
423 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500424 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200425}
426
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400427static inline u32 get_ivhd_header_size(struct ivhd_header *h)
428{
429 u32 size = 0;
430
431 switch (h->type) {
432 case 0x10:
433 size = 24;
434 break;
435 case 0x11:
436 case 0x40:
437 size = 40;
438 break;
439 }
440 return size;
441}
442
Joerg Roedelb65233a2008-07-11 17:14:21 +0200443/****************************************************************************
444 *
445 * The functions below belong to the first pass of AMD IOMMU ACPI table
446 * parsing. In this pass we try to find out the highest device id this
447 * code has to handle. Upon this information the size of the shared data
448 * structures is determined later.
449 *
450 ****************************************************************************/
451
452/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200453 * This function calculates the length of a given IVHD entry
454 */
455static inline int ivhd_entry_length(u8 *ivhd)
456{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400457 u32 type = ((struct ivhd_entry *)ivhd)->type;
458
459 if (type < 0x80) {
460 return 0x04 << (*ivhd >> 6);
461 } else if (type == IVHD_DEV_ACPI_HID) {
462 /* For ACPI_HID, offset 21 is uid len */
463 return *((u8 *)ivhd + 21) + 22;
464 }
465 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200466}
467
468/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200469 * After reading the highest device id from the IOMMU PCI capability header
470 * this function looks if there is a higher device id defined in the ACPI table
471 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200472static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
473{
474 u8 *p = (void *)h, *end = (void *)h;
475 struct ivhd_entry *dev;
476
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400477 u32 ivhd_size = get_ivhd_header_size(h);
478
479 if (!ivhd_size) {
480 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
481 return -EINVAL;
482 }
483
484 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200485 end += h->length;
486
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200487 while (p < end) {
488 dev = (struct ivhd_entry *)p;
489 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200490 case IVHD_DEV_ALL:
491 /* Use maximum BDF value for DEV_ALL */
492 update_last_devid(0xffff);
493 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200494 case IVHD_DEV_SELECT:
495 case IVHD_DEV_RANGE_END:
496 case IVHD_DEV_ALIAS:
497 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200498 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200499 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200500 break;
501 default:
502 break;
503 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200504 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200505 }
506
507 WARN_ON(p != end);
508
509 return 0;
510}
511
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400512static int __init check_ivrs_checksum(struct acpi_table_header *table)
513{
514 int i;
515 u8 checksum = 0, *p = (u8 *)table;
516
517 for (i = 0; i < table->length; ++i)
518 checksum += p[i];
519 if (checksum != 0) {
520 /* ACPI table corrupt */
521 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
522 return -ENODEV;
523 }
524
525 return 0;
526}
527
Joerg Roedelb65233a2008-07-11 17:14:21 +0200528/*
529 * Iterate over all IVHD entries in the ACPI table and find the highest device
530 * id which we need to handle. This is the first of three functions which parse
531 * the ACPI table. So we check the checksum here.
532 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200533static int __init find_last_devid_acpi(struct acpi_table_header *table)
534{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400535 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200536 struct ivhd_header *h;
537
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200538 p += IVRS_HEADER_LENGTH;
539
540 end += table->length;
541 while (p < end) {
542 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400543 if (h->type == amd_iommu_target_ivhd_type) {
544 int ret = find_last_devid_from_ivhd(h);
545
546 if (ret)
547 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200548 }
549 p += h->length;
550 }
551 WARN_ON(p != end);
552
553 return 0;
554}
555
Joerg Roedelb65233a2008-07-11 17:14:21 +0200556/****************************************************************************
557 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200558 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200559 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
560 * data structures, initialize the device/alias/rlookup table and also
561 * basically initialize the hardware.
562 *
563 ****************************************************************************/
564
565/*
566 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
567 * write commands to that buffer later and the IOMMU will execute them
568 * asynchronously
569 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200570static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200571{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200572 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
573 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200574
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200575 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200576}
577
578/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200579 * This function resets the command buffer if the IOMMU stopped fetching
580 * commands from it.
581 */
582void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
583{
584 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
585
586 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
587 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
588
589 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
590}
591
592/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200593 * This function writes the command buffer address to the hardware and
594 * enables it.
595 */
596static void iommu_enable_command_buffer(struct amd_iommu *iommu)
597{
598 u64 entry;
599
600 BUG_ON(iommu->cmd_buf == NULL);
601
602 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200603 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200604
Joerg Roedelb36ca912008-06-26 21:27:45 +0200605 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200606 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200607
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200608 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200609}
610
611static void __init free_command_buffer(struct amd_iommu *iommu)
612{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200613 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200614}
615
Joerg Roedel335503e2008-09-05 14:29:07 +0200616/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200617static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200618{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200619 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
620 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200621
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200622 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200623}
624
625static void iommu_enable_event_buffer(struct amd_iommu *iommu)
626{
627 u64 entry;
628
629 BUG_ON(iommu->evt_buf == NULL);
630
Joerg Roedel335503e2008-09-05 14:29:07 +0200631 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200632
Joerg Roedel335503e2008-09-05 14:29:07 +0200633 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
634 &entry, sizeof(entry));
635
Joerg Roedel090672072009-06-15 16:06:48 +0200636 /* set head and tail to zero manually */
637 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
638 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
639
Joerg Roedel58492e12009-05-04 18:41:16 +0200640 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200641}
642
643static void __init free_event_buffer(struct amd_iommu *iommu)
644{
645 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
646}
647
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100648/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200649static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100650{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200651 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
652 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100653
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200654 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100655}
656
657static void iommu_enable_ppr_log(struct amd_iommu *iommu)
658{
659 u64 entry;
660
661 if (iommu->ppr_log == NULL)
662 return;
663
664 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
665
666 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
667 &entry, sizeof(entry));
668
669 /* set head and tail to zero manually */
670 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
671 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
672
673 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
674 iommu_feature_enable(iommu, CONTROL_PPR_EN);
675}
676
677static void __init free_ppr_log(struct amd_iommu *iommu)
678{
679 if (iommu->ppr_log == NULL)
680 return;
681
682 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
683}
684
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500685static void free_ga_log(struct amd_iommu *iommu)
686{
687#ifdef CONFIG_IRQ_REMAP
688 if (iommu->ga_log)
689 free_pages((unsigned long)iommu->ga_log,
690 get_order(GA_LOG_SIZE));
691 if (iommu->ga_log_tail)
692 free_pages((unsigned long)iommu->ga_log_tail,
693 get_order(8));
694#endif
695}
696
697static int iommu_ga_log_enable(struct amd_iommu *iommu)
698{
699#ifdef CONFIG_IRQ_REMAP
700 u32 status, i;
701
702 if (!iommu->ga_log)
703 return -EINVAL;
704
705 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
706
707 /* Check if already running */
708 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
709 return 0;
710
711 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
712 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
713
714 for (i = 0; i < LOOP_TIMEOUT; ++i) {
715 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
716 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
717 break;
718 }
719
720 if (i >= LOOP_TIMEOUT)
721 return -EINVAL;
722#endif /* CONFIG_IRQ_REMAP */
723 return 0;
724}
725
726#ifdef CONFIG_IRQ_REMAP
727static int iommu_init_ga_log(struct amd_iommu *iommu)
728{
729 u64 entry;
730
731 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
732 return 0;
733
734 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
735 get_order(GA_LOG_SIZE));
736 if (!iommu->ga_log)
737 goto err_out;
738
739 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
740 get_order(8));
741 if (!iommu->ga_log_tail)
742 goto err_out;
743
744 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
745 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
746 &entry, sizeof(entry));
747 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
748 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
749 &entry, sizeof(entry));
750 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
751 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
752
753 return 0;
754err_out:
755 free_ga_log(iommu);
756 return -EINVAL;
757}
758#endif /* CONFIG_IRQ_REMAP */
759
760static int iommu_init_ga(struct amd_iommu *iommu)
761{
762 int ret = 0;
763
764#ifdef CONFIG_IRQ_REMAP
765 /* Note: We have already checked GASup from IVRS table.
766 * Now, we need to make sure that GAMSup is set.
767 */
768 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
769 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
770 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
771
772 ret = iommu_init_ga_log(iommu);
773#endif /* CONFIG_IRQ_REMAP */
774
775 return ret;
776}
777
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100778static void iommu_enable_gt(struct amd_iommu *iommu)
779{
780 if (!iommu_feature(iommu, FEATURE_GT))
781 return;
782
783 iommu_feature_enable(iommu, CONTROL_GT_EN);
784}
785
Joerg Roedelb65233a2008-07-11 17:14:21 +0200786/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200787static void set_dev_entry_bit(u16 devid, u8 bit)
788{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100789 int i = (bit >> 6) & 0x03;
790 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200791
Joerg Roedelee6c2862011-11-09 12:06:03 +0100792 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200793}
794
Joerg Roedelc5cca142009-10-09 18:31:20 +0200795static int get_dev_entry_bit(u16 devid, u8 bit)
796{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100797 int i = (bit >> 6) & 0x03;
798 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200799
Joerg Roedelee6c2862011-11-09 12:06:03 +0100800 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200801}
802
803
804void amd_iommu_apply_erratum_63(u16 devid)
805{
806 int sysmgt;
807
808 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
809 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
810
811 if (sysmgt == 0x01)
812 set_dev_entry_bit(devid, DEV_ENTRY_IW);
813}
814
Joerg Roedel5ff47892008-07-14 20:11:18 +0200815/* Writes the specific IOMMU for a device into the rlookup table */
816static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
817{
818 amd_iommu_rlookup_table[devid] = iommu;
819}
820
Joerg Roedelb65233a2008-07-11 17:14:21 +0200821/*
822 * This function takes the device specific flags read from the ACPI
823 * table and sets up the device table entry with that information
824 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200825static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
826 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200827{
828 if (flags & ACPI_DEVFLAG_INITPASS)
829 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
830 if (flags & ACPI_DEVFLAG_EXTINT)
831 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
832 if (flags & ACPI_DEVFLAG_NMI)
833 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
834 if (flags & ACPI_DEVFLAG_SYSMGT1)
835 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
836 if (flags & ACPI_DEVFLAG_SYSMGT2)
837 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
838 if (flags & ACPI_DEVFLAG_LINT0)
839 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
840 if (flags & ACPI_DEVFLAG_LINT1)
841 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200842
Joerg Roedelc5cca142009-10-09 18:31:20 +0200843 amd_iommu_apply_erratum_63(devid);
844
Joerg Roedel5ff47892008-07-14 20:11:18 +0200845 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200846}
847
Joerg Roedelc50e3242014-09-09 15:59:37 +0200848static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200849{
850 struct devid_map *entry;
851 struct list_head *list;
852
Joerg Roedel31cff672013-04-09 16:53:58 +0200853 if (type == IVHD_SPECIAL_IOAPIC)
854 list = &ioapic_map;
855 else if (type == IVHD_SPECIAL_HPET)
856 list = &hpet_map;
857 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200858 return -EINVAL;
859
Joerg Roedel31cff672013-04-09 16:53:58 +0200860 list_for_each_entry(entry, list, list) {
861 if (!(entry->id == id && entry->cmd_line))
862 continue;
863
864 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
865 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
866
Joerg Roedelc50e3242014-09-09 15:59:37 +0200867 *devid = entry->devid;
868
Joerg Roedel31cff672013-04-09 16:53:58 +0200869 return 0;
870 }
871
Joerg Roedel6efed632012-06-14 15:52:58 +0200872 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
873 if (!entry)
874 return -ENOMEM;
875
Joerg Roedel31cff672013-04-09 16:53:58 +0200876 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +0200877 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +0200878 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +0200879
880 list_add_tail(&entry->list, list);
881
882 return 0;
883}
884
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400885static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
886 bool cmd_line)
887{
888 struct acpihid_map_entry *entry;
889 struct list_head *list = &acpihid_map;
890
891 list_for_each_entry(entry, list, list) {
892 if (strcmp(entry->hid, hid) ||
893 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
894 !entry->cmd_line)
895 continue;
896
897 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
898 hid, uid);
899 *devid = entry->devid;
900 return 0;
901 }
902
903 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
904 if (!entry)
905 return -ENOMEM;
906
907 memcpy(entry->uid, uid, strlen(uid));
908 memcpy(entry->hid, hid, strlen(hid));
909 entry->devid = *devid;
910 entry->cmd_line = cmd_line;
911 entry->root_devid = (entry->devid & (~0x7));
912
913 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
914 entry->cmd_line ? "cmd" : "ivrs",
915 entry->hid, entry->uid, entry->root_devid);
916
917 list_add_tail(&entry->list, list);
918 return 0;
919}
920
Joerg Roedel235dacb2013-04-09 17:53:14 +0200921static int __init add_early_maps(void)
922{
923 int i, ret;
924
925 for (i = 0; i < early_ioapic_map_size; ++i) {
926 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
927 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200928 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200929 early_ioapic_map[i].cmd_line);
930 if (ret)
931 return ret;
932 }
933
934 for (i = 0; i < early_hpet_map_size; ++i) {
935 ret = add_special_device(IVHD_SPECIAL_HPET,
936 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200937 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200938 early_hpet_map[i].cmd_line);
939 if (ret)
940 return ret;
941 }
942
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400943 for (i = 0; i < early_acpihid_map_size; ++i) {
944 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
945 early_acpihid_map[i].uid,
946 &early_acpihid_map[i].devid,
947 early_acpihid_map[i].cmd_line);
948 if (ret)
949 return ret;
950 }
951
Joerg Roedel235dacb2013-04-09 17:53:14 +0200952 return 0;
953}
954
Joerg Roedelb65233a2008-07-11 17:14:21 +0200955/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200956 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +0200957 * it
958 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200959static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
960{
961 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
962
963 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
964 return;
965
966 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200967 /*
968 * We only can configure exclusion ranges per IOMMU, not
969 * per device. But we can enable the exclusion range per
970 * device. This is done here
971 */
Su Friendy2c16c9f2014-05-07 13:54:52 +0800972 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +0200973 iommu->exclusion_start = m->range_start;
974 iommu->exclusion_length = m->range_length;
975 }
976}
977
Joerg Roedelb65233a2008-07-11 17:14:21 +0200978/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200979 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
980 * initializes the hardware and our data structures with it.
981 */
Joerg Roedel6efed632012-06-14 15:52:58 +0200982static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200983 struct ivhd_header *h)
984{
985 u8 *p = (u8 *)h;
986 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +0200987 u16 devid = 0, devid_start = 0, devid_to = 0;
988 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200989 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200990 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400991 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200992 int ret;
993
994
995 ret = add_early_maps();
996 if (ret)
997 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200998
999 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001000 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001001 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001002 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001003
1004 /*
1005 * Done. Now parse the device entries
1006 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001007 ivhd_size = get_ivhd_header_size(h);
1008 if (!ivhd_size) {
1009 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1010 return -EINVAL;
1011 }
1012
1013 p += ivhd_size;
1014
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001015 end += h->length;
1016
Joerg Roedel42a698f2009-05-20 15:41:28 +02001017
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001018 while (p < end) {
1019 e = (struct ivhd_entry *)p;
1020 switch (e->type) {
1021 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001022
Joerg Roedel226e8892015-10-20 17:33:44 +02001023 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001024
Joerg Roedel226e8892015-10-20 17:33:44 +02001025 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1026 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001027 break;
1028 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001029
1030 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1031 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001032 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001033 PCI_SLOT(e->devid),
1034 PCI_FUNC(e->devid),
1035 e->flags);
1036
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001037 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001038 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001039 break;
1040 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001041
1042 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1043 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001044 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001045 PCI_SLOT(e->devid),
1046 PCI_FUNC(e->devid),
1047 e->flags);
1048
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001049 devid_start = e->devid;
1050 flags = e->flags;
1051 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001052 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001053 break;
1054 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001055
1056 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1057 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001058 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001059 PCI_SLOT(e->devid),
1060 PCI_FUNC(e->devid),
1061 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001062 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001063 PCI_SLOT(e->ext >> 8),
1064 PCI_FUNC(e->ext >> 8));
1065
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001066 devid = e->devid;
1067 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001068 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001069 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001070 amd_iommu_alias_table[devid] = devid_to;
1071 break;
1072 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001073
1074 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1075 "devid: %02x:%02x.%x flags: %02x "
1076 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001077 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001078 PCI_SLOT(e->devid),
1079 PCI_FUNC(e->devid),
1080 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001081 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001082 PCI_SLOT(e->ext >> 8),
1083 PCI_FUNC(e->ext >> 8));
1084
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001085 devid_start = e->devid;
1086 flags = e->flags;
1087 devid_to = e->ext >> 8;
1088 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001089 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001090 break;
1091 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001092
1093 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1094 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001095 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001096 PCI_SLOT(e->devid),
1097 PCI_FUNC(e->devid),
1098 e->flags, e->ext);
1099
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001100 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001101 set_dev_entry_from_acpi(iommu, devid, e->flags,
1102 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001103 break;
1104 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001105
1106 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1107 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001108 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001109 PCI_SLOT(e->devid),
1110 PCI_FUNC(e->devid),
1111 e->flags, e->ext);
1112
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001113 devid_start = e->devid;
1114 flags = e->flags;
1115 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001116 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001117 break;
1118 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001119
1120 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001121 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001122 PCI_SLOT(e->devid),
1123 PCI_FUNC(e->devid));
1124
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001125 devid = e->devid;
1126 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001127 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001128 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001129 set_dev_entry_from_acpi(iommu,
1130 devid_to, flags, ext_flags);
1131 }
1132 set_dev_entry_from_acpi(iommu, dev_i,
1133 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001134 }
1135 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001136 case IVHD_DEV_SPECIAL: {
1137 u8 handle, type;
1138 const char *var;
1139 u16 devid;
1140 int ret;
1141
1142 handle = e->ext & 0xff;
1143 devid = (e->ext >> 8) & 0xffff;
1144 type = (e->ext >> 24) & 0xff;
1145
1146 if (type == IVHD_SPECIAL_IOAPIC)
1147 var = "IOAPIC";
1148 else if (type == IVHD_SPECIAL_HPET)
1149 var = "HPET";
1150 else
1151 var = "UNKNOWN";
1152
1153 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1154 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001155 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001156 PCI_SLOT(devid),
1157 PCI_FUNC(devid));
1158
Joerg Roedelc50e3242014-09-09 15:59:37 +02001159 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001160 if (ret)
1161 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001162
1163 /*
1164 * add_special_device might update the devid in case a
1165 * command-line override is present. So call
1166 * set_dev_entry_from_acpi after add_special_device.
1167 */
1168 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1169
Joerg Roedel6efed632012-06-14 15:52:58 +02001170 break;
1171 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001172 case IVHD_DEV_ACPI_HID: {
1173 u16 devid;
1174 u8 hid[ACPIHID_HID_LEN] = {0};
1175 u8 uid[ACPIHID_UID_LEN] = {0};
1176 int ret;
1177
1178 if (h->type != 0x40) {
1179 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1180 e->type);
1181 break;
1182 }
1183
1184 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1185 hid[ACPIHID_HID_LEN - 1] = '\0';
1186
1187 if (!(*hid)) {
1188 pr_err(FW_BUG "Invalid HID.\n");
1189 break;
1190 }
1191
1192 switch (e->uidf) {
1193 case UID_NOT_PRESENT:
1194
1195 if (e->uidl != 0)
1196 pr_warn(FW_BUG "Invalid UID length.\n");
1197
1198 break;
1199 case UID_IS_INTEGER:
1200
1201 sprintf(uid, "%d", e->uid);
1202
1203 break;
1204 case UID_IS_CHARACTER:
1205
1206 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1207 uid[ACPIHID_UID_LEN - 1] = '\0';
1208
1209 break;
1210 default:
1211 break;
1212 }
1213
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001214 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001215 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1216 hid, uid,
1217 PCI_BUS_NUM(devid),
1218 PCI_SLOT(devid),
1219 PCI_FUNC(devid));
1220
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001221 flags = e->flags;
1222
1223 ret = add_acpi_hid_device(hid, uid, &devid, false);
1224 if (ret)
1225 return ret;
1226
1227 /*
1228 * add_special_device might update the devid in case a
1229 * command-line override is present. So call
1230 * set_dev_entry_from_acpi after add_special_device.
1231 */
1232 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1233
1234 break;
1235 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001236 default:
1237 break;
1238 }
1239
Joerg Roedelb514e552008-09-17 17:14:27 +02001240 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001241 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001242
1243 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001244}
1245
Joerg Roedele47d4022008-06-26 21:27:48 +02001246static void __init free_iommu_one(struct amd_iommu *iommu)
1247{
1248 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001249 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001250 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001251 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001252 iommu_unmap_mmio_space(iommu);
1253}
1254
1255static void __init free_iommu_all(void)
1256{
1257 struct amd_iommu *iommu, *next;
1258
Joerg Roedel3bd22172009-05-04 15:06:20 +02001259 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001260 list_del(&iommu->list);
1261 free_iommu_one(iommu);
1262 kfree(iommu);
1263 }
1264}
1265
Joerg Roedelb65233a2008-07-11 17:14:21 +02001266/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001267 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1268 * Workaround:
1269 * BIOS should disable L2B micellaneous clock gating by setting
1270 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1271 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001272static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001273{
1274 u32 value;
1275
1276 if ((boot_cpu_data.x86 != 0x15) ||
1277 (boot_cpu_data.x86_model < 0x10) ||
1278 (boot_cpu_data.x86_model > 0x1f))
1279 return;
1280
1281 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1282 pci_read_config_dword(iommu->dev, 0xf4, &value);
1283
1284 if (value & BIT(2))
1285 return;
1286
1287 /* Select NB indirect register 0x90 and enable writing */
1288 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1289
1290 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1291 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1292 dev_name(&iommu->dev->dev));
1293
1294 /* Clear the enable writing bit */
1295 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1296}
1297
1298/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001299 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1300 * Workaround:
1301 * BIOS should enable ATS write permission check by setting
1302 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1303 */
1304static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1305{
1306 u32 value;
1307
1308 if ((boot_cpu_data.x86 != 0x15) ||
1309 (boot_cpu_data.x86_model < 0x30) ||
1310 (boot_cpu_data.x86_model > 0x3f))
1311 return;
1312
1313 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1314 value = iommu_read_l2(iommu, 0x47);
1315
1316 if (value & BIT(0))
1317 return;
1318
1319 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1320 iommu_write_l2(iommu, 0x47, value | BIT(0));
1321
1322 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1323 dev_name(&iommu->dev->dev));
1324}
1325
1326/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001327 * This function clues the initialization function for one IOMMU
1328 * together and also allocates the command buffer and programs the
1329 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1330 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001331static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1332{
Joerg Roedel6efed632012-06-14 15:52:58 +02001333 int ret;
1334
Joerg Roedele47d4022008-06-26 21:27:48 +02001335 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001336
1337 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001338 list_add_tail(&iommu->list, &amd_iommu_list);
Joerg Roedelbb527772009-11-20 14:31:51 +01001339 iommu->index = amd_iommus_present++;
1340
1341 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1342 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1343 return -ENOSYS;
1344 }
1345
1346 /* Index is fine - add IOMMU to the array */
1347 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001348
1349 /*
1350 * Copy data from ACPI table entry to the iommu struct
1351 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001352 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001353 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001354 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001355 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001356
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001357 switch (h->type) {
1358 case 0x10:
1359 /* Check if IVHD EFR contains proper max banks/counters */
1360 if ((h->efr_attr != 0) &&
1361 ((h->efr_attr & (0xF << 13)) != 0) &&
1362 ((h->efr_attr & (0x3F << 17)) != 0))
1363 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1364 else
1365 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001366 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1367 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001368 break;
1369 case 0x11:
1370 case 0x40:
1371 if (h->efr_reg & (1 << 9))
1372 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1373 else
1374 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001375 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1376 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001377 break;
1378 default:
1379 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001380 }
1381
1382 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1383 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001384 if (!iommu->mmio_base)
1385 return -ENOMEM;
1386
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001387 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001388 return -ENOMEM;
1389
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001390 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001391 return -ENOMEM;
1392
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001393 iommu->int_enabled = false;
1394
Joerg Roedel6efed632012-06-14 15:52:58 +02001395 ret = init_iommu_from_acpi(iommu, h);
1396 if (ret)
1397 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001398
Jiang Liu7c71d302015-04-13 14:11:33 +08001399 ret = amd_iommu_create_irq_domain(iommu);
1400 if (ret)
1401 return ret;
1402
Joerg Roedelf6fec002012-06-21 16:51:25 +02001403 /*
1404 * Make sure IOMMU is not considered to translate itself. The IVRS
1405 * table tells us so, but this is a lie!
1406 */
1407 amd_iommu_rlookup_table[iommu->devid] = NULL;
1408
Joerg Roedel23c742d2012-06-12 11:47:34 +02001409 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001410}
1411
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001412/**
1413 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1414 * @ivrs Pointer to the IVRS header
1415 *
1416 * This function search through all IVDB of the maximum supported IVHD
1417 */
1418static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1419{
1420 u8 *base = (u8 *)ivrs;
1421 struct ivhd_header *ivhd = (struct ivhd_header *)
1422 (base + IVRS_HEADER_LENGTH);
1423 u8 last_type = ivhd->type;
1424 u16 devid = ivhd->devid;
1425
1426 while (((u8 *)ivhd - base < ivrs->length) &&
1427 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1428 u8 *p = (u8 *) ivhd;
1429
1430 if (ivhd->devid == devid)
1431 last_type = ivhd->type;
1432 ivhd = (struct ivhd_header *)(p + ivhd->length);
1433 }
1434
1435 return last_type;
1436}
1437
Joerg Roedelb65233a2008-07-11 17:14:21 +02001438/*
1439 * Iterates over all IOMMU entries in the ACPI table, allocates the
1440 * IOMMU structure and initializes it with init_iommu_one()
1441 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001442static int __init init_iommu_all(struct acpi_table_header *table)
1443{
1444 u8 *p = (u8 *)table, *end = (u8 *)table;
1445 struct ivhd_header *h;
1446 struct amd_iommu *iommu;
1447 int ret;
1448
Joerg Roedele47d4022008-06-26 21:27:48 +02001449 end += table->length;
1450 p += IVRS_HEADER_LENGTH;
1451
1452 while (p < end) {
1453 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001454 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001455
Joerg Roedelae908c22009-09-01 16:52:16 +02001456 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001457 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001458 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001459 PCI_FUNC(h->devid), h->cap_ptr,
1460 h->pci_seg, h->flags, h->info);
1461 DUMP_printk(" mmio-addr: %016llx\n",
1462 h->mmio_phys);
1463
Joerg Roedele47d4022008-06-26 21:27:48 +02001464 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001465 if (iommu == NULL)
1466 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001467
Joerg Roedele47d4022008-06-26 21:27:48 +02001468 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001469 if (ret)
1470 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001471 }
1472 p += h->length;
1473
1474 }
1475 WARN_ON(p != end);
1476
1477 return 0;
1478}
1479
Steven L Kinney30861dd2013-06-05 16:11:48 -05001480
1481static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1482{
1483 u64 val = 0xabcd, val2 = 0;
1484
1485 if (!iommu_feature(iommu, FEATURE_PC))
1486 return;
1487
1488 amd_iommu_pc_present = true;
1489
1490 /* Check if the performance counters can be written to */
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01001491 if ((0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val, true)) ||
1492 (0 != iommu_pc_get_set_reg_val(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001493 (val != val2)) {
1494 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1495 amd_iommu_pc_present = false;
1496 return;
1497 }
1498
1499 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1500
1501 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1502 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1503 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1504}
1505
Alex Williamson066f2e92014-06-12 16:12:37 -06001506static ssize_t amd_iommu_show_cap(struct device *dev,
1507 struct device_attribute *attr,
1508 char *buf)
1509{
1510 struct amd_iommu *iommu = dev_get_drvdata(dev);
1511 return sprintf(buf, "%x\n", iommu->cap);
1512}
1513static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1514
1515static ssize_t amd_iommu_show_features(struct device *dev,
1516 struct device_attribute *attr,
1517 char *buf)
1518{
1519 struct amd_iommu *iommu = dev_get_drvdata(dev);
1520 return sprintf(buf, "%llx\n", iommu->features);
1521}
1522static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1523
1524static struct attribute *amd_iommu_attrs[] = {
1525 &dev_attr_cap.attr,
1526 &dev_attr_features.attr,
1527 NULL,
1528};
1529
1530static struct attribute_group amd_iommu_group = {
1531 .name = "amd-iommu",
1532 .attrs = amd_iommu_attrs,
1533};
1534
1535static const struct attribute_group *amd_iommu_groups[] = {
1536 &amd_iommu_group,
1537 NULL,
1538};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001539
Joerg Roedel5f35f442018-10-05 12:32:46 +02001540static int __init iommu_init_pci(struct amd_iommu *iommu)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001541{
1542 int cap_ptr = iommu->cap_ptr;
1543 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001544 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001545
Shuah Khanc5081cd2013-02-27 17:07:19 -07001546 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001547 iommu->devid & 0xff);
1548 if (!iommu->dev)
1549 return -ENODEV;
1550
Jiang Liucbbc00b2015-10-09 22:07:31 +08001551 /* Prevent binding other PCI device drivers to IOMMU devices */
1552 iommu->dev->match_driver = false;
1553
Joerg Roedel23c742d2012-06-12 11:47:34 +02001554 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1555 &iommu->cap);
1556 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1557 &range);
1558 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1559 &misc);
1560
Joerg Roedel23c742d2012-06-12 11:47:34 +02001561 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1562 amd_iommu_iotlb_sup = false;
1563
1564 /* read extended feature bits */
1565 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1566 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1567
1568 iommu->features = ((u64)high << 32) | low;
1569
1570 if (iommu_feature(iommu, FEATURE_GT)) {
1571 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001572 u32 max_pasid;
1573 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001574
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001575 pasmax = iommu->features & FEATURE_PASID_MASK;
1576 pasmax >>= FEATURE_PASID_SHIFT;
1577 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001578
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001579 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1580
1581 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001582
1583 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1584 glxval >>= FEATURE_GLXVAL_SHIFT;
1585
1586 if (amd_iommu_max_glx_val == -1)
1587 amd_iommu_max_glx_val = glxval;
1588 else
1589 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1590 }
1591
1592 if (iommu_feature(iommu, FEATURE_GT) &&
1593 iommu_feature(iommu, FEATURE_PPR)) {
1594 iommu->is_iommu_v2 = true;
1595 amd_iommu_v2_present = true;
1596 }
1597
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001598 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1599 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001600
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001601 ret = iommu_init_ga(iommu);
1602 if (ret)
1603 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001604
Joerg Roedel23c742d2012-06-12 11:47:34 +02001605 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1606 amd_iommu_np_cache = true;
1607
Steven L Kinney30861dd2013-06-05 16:11:48 -05001608 init_iommu_perf_ctr(iommu);
1609
Joerg Roedel23c742d2012-06-12 11:47:34 +02001610 if (is_rd890_iommu(iommu->dev)) {
1611 int i, j;
1612
1613 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1614 PCI_DEVFN(0, 0));
1615
1616 /*
1617 * Some rd890 systems may not be fully reconfigured by the
1618 * BIOS, so it's necessary for us to store this information so
1619 * it can be reprogrammed on resume
1620 */
1621 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1622 &iommu->stored_addr_lo);
1623 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1624 &iommu->stored_addr_hi);
1625
1626 /* Low bit locks writes to configuration space */
1627 iommu->stored_addr_lo &= ~1;
1628
1629 for (i = 0; i < 6; i++)
1630 for (j = 0; j < 0x12; j++)
1631 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1632
1633 for (i = 0; i < 0x83; i++)
1634 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1635 }
1636
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001637 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001638 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001639
Alex Williamson066f2e92014-06-12 16:12:37 -06001640 iommu->iommu_dev = iommu_device_create(&iommu->dev->dev, iommu,
1641 amd_iommu_groups, "ivhd%d",
1642 iommu->index);
1643
Joerg Roedel23c742d2012-06-12 11:47:34 +02001644 return pci_enable_device(iommu->dev);
1645}
1646
Joerg Roedel4d121c32012-06-14 12:21:55 +02001647static void print_iommu_info(void)
1648{
1649 static const char * const feat_str[] = {
1650 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1651 "IA", "GA", "HE", "PC"
1652 };
1653 struct amd_iommu *iommu;
1654
1655 for_each_iommu(iommu) {
1656 int i;
1657
1658 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1659 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1660
1661 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001662 pr_info("AMD-Vi: Extended features (%#llx):\n",
1663 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001664 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001665 if (iommu_feature(iommu, (1ULL << i)))
1666 pr_cont(" %s", feat_str[i]);
1667 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001668
1669 if (iommu->features & FEATURE_GAM_VAPIC)
1670 pr_cont(" GA_vAPIC");
1671
Steven L Kinney30861dd2013-06-05 16:11:48 -05001672 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001673 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001674 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001675 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001676 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001677 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1678 pr_info("AMD-Vi: virtual APIC enabled\n");
1679 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001680}
1681
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001682static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001683{
1684 struct amd_iommu *iommu;
1685 int ret = 0;
1686
1687 for_each_iommu(iommu) {
1688 ret = iommu_init_pci(iommu);
1689 if (ret)
1690 break;
1691 }
1692
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001693 /*
1694 * Order is important here to make sure any unity map requirements are
1695 * fulfilled. The unity mappings are created and written to the device
1696 * table during the amd_iommu_init_api() call.
1697 *
1698 * After that we call init_device_table_dma() to make sure any
1699 * uninitialized DTE will block DMA, and in the end we flush the caches
1700 * of all IOMMUs to make sure the changes to the device table are
1701 * active.
1702 */
1703 ret = amd_iommu_init_api();
1704
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001705 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001706
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001707 for_each_iommu(iommu)
1708 iommu_flush_all_caches(iommu);
1709
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001710 if (!ret)
1711 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001712
Joerg Roedel23c742d2012-06-12 11:47:34 +02001713 return ret;
1714}
1715
Joerg Roedelb65233a2008-07-11 17:14:21 +02001716/****************************************************************************
1717 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001718 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001719 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001720 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1721 * pci_dev.
1722 *
1723 ****************************************************************************/
1724
Joerg Roedel9f800de2009-11-23 12:45:25 +01001725static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001726{
1727 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001728
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001729 r = pci_enable_msi(iommu->dev);
1730 if (r)
1731 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001732
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001733 r = request_threaded_irq(iommu->dev->irq,
1734 amd_iommu_int_handler,
1735 amd_iommu_int_thread,
1736 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001737 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001738
1739 if (r) {
1740 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001741 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001742 }
1743
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001744 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001745
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001746 return 0;
1747}
1748
Joerg Roedel05f92db2009-05-12 09:52:46 +02001749static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001750{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001751 int ret;
1752
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001753 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001754 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001755
Yijing Wang82fcfc62013-08-08 21:12:36 +08001756 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001757 ret = iommu_setup_msi(iommu);
1758 else
1759 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001760
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001761 if (ret)
1762 return ret;
1763
1764enable_faults:
1765 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1766
1767 if (iommu->ppr_log != NULL)
1768 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1769
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001770 iommu_ga_log_enable(iommu);
1771
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001772 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001773}
1774
1775/****************************************************************************
1776 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001777 * The next functions belong to the third pass of parsing the ACPI
1778 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001779 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001780 *
1781 ****************************************************************************/
1782
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001783static void __init free_unity_maps(void)
1784{
1785 struct unity_map_entry *entry, *next;
1786
1787 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1788 list_del(&entry->list);
1789 kfree(entry);
1790 }
1791}
1792
Joerg Roedelb65233a2008-07-11 17:14:21 +02001793/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001794static int __init init_exclusion_range(struct ivmd_header *m)
1795{
1796 int i;
1797
1798 switch (m->type) {
1799 case ACPI_IVMD_TYPE:
1800 set_device_exclusion_range(m->devid, m);
1801 break;
1802 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001803 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001804 set_device_exclusion_range(i, m);
1805 break;
1806 case ACPI_IVMD_TYPE_RANGE:
1807 for (i = m->devid; i <= m->aux; ++i)
1808 set_device_exclusion_range(i, m);
1809 break;
1810 default:
1811 break;
1812 }
1813
1814 return 0;
1815}
1816
Joerg Roedelb65233a2008-07-11 17:14:21 +02001817/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001818static int __init init_unity_map_range(struct ivmd_header *m)
1819{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001820 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001821 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001822
1823 e = kzalloc(sizeof(*e), GFP_KERNEL);
1824 if (e == NULL)
1825 return -ENOMEM;
1826
1827 switch (m->type) {
1828 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001829 kfree(e);
1830 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001831 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001832 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001833 e->devid_start = e->devid_end = m->devid;
1834 break;
1835 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001836 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001837 e->devid_start = 0;
1838 e->devid_end = amd_iommu_last_bdf;
1839 break;
1840 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001841 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001842 e->devid_start = m->devid;
1843 e->devid_end = m->aux;
1844 break;
1845 }
1846 e->address_start = PAGE_ALIGN(m->range_start);
1847 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1848 e->prot = m->flags >> 1;
1849
Joerg Roedel02acc432009-05-20 16:24:21 +02001850 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1851 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001852 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1853 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02001854 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1855 e->address_start, e->address_end, m->flags);
1856
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001857 list_add_tail(&e->list, &amd_iommu_unity_map);
1858
1859 return 0;
1860}
1861
Joerg Roedelb65233a2008-07-11 17:14:21 +02001862/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001863static int __init init_memory_definitions(struct acpi_table_header *table)
1864{
1865 u8 *p = (u8 *)table, *end = (u8 *)table;
1866 struct ivmd_header *m;
1867
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001868 end += table->length;
1869 p += IVRS_HEADER_LENGTH;
1870
1871 while (p < end) {
1872 m = (struct ivmd_header *)p;
1873 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1874 init_exclusion_range(m);
1875 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1876 init_unity_map_range(m);
1877
1878 p += m->length;
1879 }
1880
1881 return 0;
1882}
1883
Joerg Roedelb65233a2008-07-11 17:14:21 +02001884/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001885 * Init the device table to not allow DMA access for devices and
1886 * suppress all page faults
1887 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02001888static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001889{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001890 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001891
1892 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1893 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1894 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001895 }
1896}
1897
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001898static void __init uninit_device_table_dma(void)
1899{
1900 u32 devid;
1901
1902 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1903 amd_iommu_dev_table[devid].data[0] = 0ULL;
1904 amd_iommu_dev_table[devid].data[1] = 0ULL;
1905 }
1906}
1907
Joerg Roedel33f28c52012-06-15 18:03:31 +02001908static void init_device_table(void)
1909{
1910 u32 devid;
1911
1912 if (!amd_iommu_irq_remap)
1913 return;
1914
1915 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1916 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1917}
1918
Joerg Roedele9bf5192010-09-20 14:33:07 +02001919static void iommu_init_flags(struct amd_iommu *iommu)
1920{
1921 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1922 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1923 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1924
1925 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1926 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1927 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1928
1929 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1930 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1931 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1932
1933 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1934 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1935 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1936
1937 /*
1938 * make IOMMU memory accesses cache coherent
1939 */
1940 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001941
1942 /* Set IOTLB invalidation timeout to 1s */
1943 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001944}
1945
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001946static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001947{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001948 int i, j;
1949 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001950 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001951
1952 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001953 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001954 return;
1955
1956 /*
1957 * First, we need to ensure that the iommu is enabled. This is
1958 * controlled by a register in the northbridge
1959 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001960
1961 /* Select Northbridge indirect register 0x75 and enable writing */
1962 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1963 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1964
1965 /* Enable the iommu */
1966 if (!(ioc_feature_control & 0x1))
1967 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1968
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001969 /* Restore the iommu BAR */
1970 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1971 iommu->stored_addr_lo);
1972 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1973 iommu->stored_addr_hi);
1974
1975 /* Restore the l1 indirect regs for each of the 6 l1s */
1976 for (i = 0; i < 6; i++)
1977 for (j = 0; j < 0x12; j++)
1978 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1979
1980 /* Restore the l2 indirect regs */
1981 for (i = 0; i < 0x83; i++)
1982 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
1983
1984 /* Lock PCI setup registers */
1985 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1986 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02001987}
1988
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001989static void iommu_enable_ga(struct amd_iommu *iommu)
1990{
1991#ifdef CONFIG_IRQ_REMAP
1992 switch (amd_iommu_guest_ir) {
1993 case AMD_IOMMU_GUEST_IR_VAPIC:
1994 iommu_feature_enable(iommu, CONTROL_GAM_EN);
1995 /* Fall through */
1996 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
1997 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05001998 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001999 break;
2000 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002001 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002002 break;
2003 }
2004#endif
2005}
2006
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002007/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002008 * This function finally enables all IOMMUs found in the system after
2009 * they have been initialized
2010 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002011static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002012{
2013 struct amd_iommu *iommu;
2014
Joerg Roedel3bd22172009-05-04 15:06:20 +02002015 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02002016 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002017 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02002018 iommu_set_device_table(iommu);
2019 iommu_enable_command_buffer(iommu);
2020 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002021 iommu_set_exclusion_range(iommu);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002022 iommu_enable_ga(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002023 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02002024 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002025 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002026
2027#ifdef CONFIG_IRQ_REMAP
2028 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2029 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2030#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002031}
2032
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002033static void enable_iommus_v2(void)
2034{
2035 struct amd_iommu *iommu;
2036
2037 for_each_iommu(iommu) {
2038 iommu_enable_ppr_log(iommu);
2039 iommu_enable_gt(iommu);
2040 }
2041}
2042
2043static void enable_iommus(void)
2044{
2045 early_enable_iommus();
2046
2047 enable_iommus_v2();
2048}
2049
Joerg Roedel92ac4322009-05-19 19:06:27 +02002050static void disable_iommus(void)
2051{
2052 struct amd_iommu *iommu;
2053
2054 for_each_iommu(iommu)
2055 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002056
2057#ifdef CONFIG_IRQ_REMAP
2058 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2059 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2060#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002061}
2062
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002063/*
2064 * Suspend/Resume support
2065 * disable suspend until real resume implemented
2066 */
2067
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002068static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002069{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002070 struct amd_iommu *iommu;
2071
2072 for_each_iommu(iommu)
2073 iommu_apply_resume_quirks(iommu);
2074
Joerg Roedel736501e2009-05-12 09:56:12 +02002075 /* re-load the hardware */
2076 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002077
2078 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002079}
2080
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002081static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002082{
Joerg Roedel736501e2009-05-12 09:56:12 +02002083 /* disable IOMMUs to go out of the way for BIOS */
2084 disable_iommus();
2085
2086 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002087}
2088
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002089static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002090 .suspend = amd_iommu_suspend,
2091 .resume = amd_iommu_resume,
2092};
2093
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002094static void __init free_on_init_error(void)
2095{
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002096 free_pages((unsigned long)irq_lookup_table,
2097 get_order(rlookup_table_size));
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002098
Julia Lawalla5919892015-09-13 14:15:31 +02002099 kmem_cache_destroy(amd_iommu_irq_cache);
2100 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002101
2102 free_pages((unsigned long)amd_iommu_rlookup_table,
2103 get_order(rlookup_table_size));
2104
2105 free_pages((unsigned long)amd_iommu_alias_table,
2106 get_order(alias_table_size));
2107
2108 free_pages((unsigned long)amd_iommu_dev_table,
2109 get_order(dev_table_size));
2110
2111 free_iommu_all();
2112
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002113#ifdef CONFIG_GART_IOMMU
2114 /*
2115 * We failed to initialize the AMD IOMMU - try fallback to GART
2116 * if possible.
2117 */
2118 gart_iommu_init();
2119
2120#endif
2121}
2122
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002123/* SB IOAPIC is always on this device in AMD systems */
2124#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2125
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002126static bool __init check_ioapic_information(void)
2127{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002128 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002129 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002130 int idx;
2131
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002132 has_sb_ioapic = false;
2133 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002134
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002135 /*
2136 * If we have map overrides on the kernel command line the
2137 * messages in this function might not describe firmware bugs
2138 * anymore - so be careful
2139 */
2140 if (cmdline_maps)
2141 fw_bug = "";
2142
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002143 for (idx = 0; idx < nr_ioapics; idx++) {
2144 int devid, id = mpc_ioapic_id(idx);
2145
2146 devid = get_ioapic_devid(id);
2147 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002148 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2149 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002150 ret = false;
2151 } else if (devid == IOAPIC_SB_DEVID) {
2152 has_sb_ioapic = true;
2153 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002154 }
2155 }
2156
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002157 if (!has_sb_ioapic) {
2158 /*
2159 * We expect the SB IOAPIC to be listed in the IVRS
2160 * table. The system timer is connected to the SB IOAPIC
2161 * and if we don't have it in the list the system will
2162 * panic at boot time. This situation usually happens
2163 * when the BIOS is buggy and provides us the wrong
2164 * device id for the IOAPIC in the system.
2165 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002166 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002167 }
2168
2169 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002170 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002171
2172 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002173}
2174
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002175static void __init free_dma_resources(void)
2176{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002177 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2178 get_order(MAX_DOMAIN_ID/8));
2179
2180 free_unity_maps();
2181}
2182
Joerg Roedelb65233a2008-07-11 17:14:21 +02002183/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002184 * This is the hardware init function for AMD IOMMU in the system.
2185 * This function is called either from amd_iommu_init or from the interrupt
2186 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002187 *
2188 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002189 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002190 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002191 * 1 pass) Discover the most comprehensive IVHD type to use.
2192 *
2193 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002194 * Upon this information the size of the data structures is
2195 * determined that needs to be allocated.
2196 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002197 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002198 * information in the ACPI table about available AMD IOMMUs
2199 * in the system. It also maps the PCI devices in the
2200 * system to specific IOMMUs
2201 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002202 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002203 * initialized we update them with information about memory
2204 * remapping requirements parsed out of the ACPI table in
2205 * this last pass.
2206 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002207 * After everything is set up the IOMMUs are enabled and the necessary
2208 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002209 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002210static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002211{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002212 struct acpi_table_header *ivrs_base;
2213 acpi_size ivrs_size;
2214 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002215 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002216
Joerg Roedel643511b2012-06-12 12:09:35 +02002217 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002218 return -ENODEV;
2219
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002220 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
2221 if (status == AE_NOT_FOUND)
2222 return -ENODEV;
2223 else if (ACPI_FAILURE(status)) {
2224 const char *err = acpi_format_exception(status);
2225 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2226 return -EINVAL;
2227 }
2228
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002229 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002230 * Validate checksum here so we don't need to do it when
2231 * we actually parse the table
2232 */
2233 ret = check_ivrs_checksum(ivrs_base);
2234 if (ret)
2235 return ret;
2236
2237 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2238 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2239
2240 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002241 * First parse ACPI tables to find the largest Bus/Dev/Func
2242 * we need to handle. Upon this information the shared data
2243 * structures for the IOMMUs in the system will be allocated
2244 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002245 ret = find_last_devid_acpi(ivrs_base);
2246 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002247 goto out;
2248
Joerg Roedelc5714842008-07-11 17:14:25 +02002249 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2250 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2251 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002252
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002253 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002254 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002255 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002256 get_order(dev_table_size));
2257 if (amd_iommu_dev_table == NULL)
2258 goto out;
2259
2260 /*
2261 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2262 * IOMMU see for that device
2263 */
2264 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2265 get_order(alias_table_size));
2266 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002267 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002268
2269 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002270 amd_iommu_rlookup_table = (void *)__get_free_pages(
2271 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002272 get_order(rlookup_table_size));
2273 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002274 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002275
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002276 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2277 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002278 get_order(MAX_DOMAIN_ID/8));
2279 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002280 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002281
2282 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002283 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002284 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002285 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002286 amd_iommu_alias_table[i] = i;
2287
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002288 /*
2289 * never allocate domain 0 because its used as the non-allocated and
2290 * error value placeholder
2291 */
Baoquan He5c87f622016-09-15 16:50:51 +08002292 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002293
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002294 spin_lock_init(&amd_iommu_pd_lock);
2295
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002296 /*
2297 * now the data structures are allocated and basically initialized
2298 * start the real acpi table scan
2299 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002300 ret = init_iommu_all(ivrs_base);
2301 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002302 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002303
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002304 if (amd_iommu_irq_remap)
2305 amd_iommu_irq_remap = check_ioapic_information();
2306
Joerg Roedel05152a02012-06-15 16:53:51 +02002307 if (amd_iommu_irq_remap) {
2308 /*
2309 * Interrupt remapping enabled, create kmem_cache for the
2310 * remapping tables.
2311 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002312 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002313 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2314 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2315 else
2316 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002317 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002318 remap_cache_sz,
2319 IRQ_TABLE_ALIGNMENT,
2320 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002321 if (!amd_iommu_irq_cache)
2322 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002323
2324 irq_lookup_table = (void *)__get_free_pages(
2325 GFP_KERNEL | __GFP_ZERO,
2326 get_order(rlookup_table_size));
2327 if (!irq_lookup_table)
2328 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002329 }
2330
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002331 ret = init_memory_definitions(ivrs_base);
2332 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002333 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002334
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002335 /* init the device table */
2336 init_device_table();
2337
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002338out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002339 /* Don't leak any ACPI memory */
2340 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
2341 ivrs_base = NULL;
2342
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002343 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002344}
2345
Gerard Snitselaarae295142012-03-16 11:38:22 -07002346static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002347{
2348 struct amd_iommu *iommu;
2349 int ret = 0;
2350
2351 for_each_iommu(iommu) {
2352 ret = iommu_init_msi(iommu);
2353 if (ret)
2354 goto out;
2355 }
2356
2357out:
2358 return ret;
2359}
2360
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002361static bool detect_ivrs(void)
2362{
2363 struct acpi_table_header *ivrs_base;
2364 acpi_size ivrs_size;
2365 acpi_status status;
2366
2367 status = acpi_get_table_with_size("IVRS", 0, &ivrs_base, &ivrs_size);
2368 if (status == AE_NOT_FOUND)
2369 return false;
2370 else if (ACPI_FAILURE(status)) {
2371 const char *err = acpi_format_exception(status);
2372 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2373 return false;
2374 }
2375
2376 early_acpi_os_unmap_memory((char __iomem *)ivrs_base, ivrs_size);
2377
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002378 /* Make sure ACS will be enabled during PCI probe */
2379 pci_request_acs();
2380
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002381 return true;
2382}
2383
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002384/****************************************************************************
2385 *
2386 * AMD IOMMU Initialization State Machine
2387 *
2388 ****************************************************************************/
2389
2390static int __init state_next(void)
2391{
2392 int ret = 0;
2393
2394 switch (init_state) {
2395 case IOMMU_START_STATE:
2396 if (!detect_ivrs()) {
2397 init_state = IOMMU_NOT_FOUND;
2398 ret = -ENODEV;
2399 } else {
2400 init_state = IOMMU_IVRS_DETECTED;
2401 }
2402 break;
2403 case IOMMU_IVRS_DETECTED:
2404 ret = early_amd_iommu_init();
2405 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2406 break;
2407 case IOMMU_ACPI_FINISHED:
2408 early_enable_iommus();
2409 register_syscore_ops(&amd_iommu_syscore_ops);
2410 x86_platform.iommu_shutdown = disable_iommus;
2411 init_state = IOMMU_ENABLED;
2412 break;
2413 case IOMMU_ENABLED:
2414 ret = amd_iommu_init_pci();
2415 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2416 enable_iommus_v2();
2417 break;
2418 case IOMMU_PCI_INIT:
2419 ret = amd_iommu_enable_interrupts();
2420 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2421 break;
2422 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002423 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002424 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2425 break;
2426 case IOMMU_DMA_OPS:
2427 init_state = IOMMU_INITIALIZED;
2428 break;
2429 case IOMMU_INITIALIZED:
2430 /* Nothing to do */
2431 break;
2432 case IOMMU_NOT_FOUND:
2433 case IOMMU_INIT_ERROR:
2434 /* Error states => do nothing */
2435 ret = -EINVAL;
2436 break;
2437 default:
2438 /* Unknown state */
2439 BUG();
2440 }
2441
2442 return ret;
2443}
2444
2445static int __init iommu_go_to_state(enum iommu_init_state state)
2446{
2447 int ret = 0;
2448
2449 while (init_state != state) {
2450 ret = state_next();
2451 if (init_state == IOMMU_NOT_FOUND ||
2452 init_state == IOMMU_INIT_ERROR)
2453 break;
2454 }
2455
2456 return ret;
2457}
2458
Joerg Roedel6b474b82012-06-26 16:46:04 +02002459#ifdef CONFIG_IRQ_REMAP
2460int __init amd_iommu_prepare(void)
2461{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002462 int ret;
2463
Jiang Liu7fa1c842015-01-07 15:31:42 +08002464 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002465
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002466 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2467 if (ret)
2468 return ret;
2469 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002470}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002471
Joerg Roedel6b474b82012-06-26 16:46:04 +02002472int __init amd_iommu_enable(void)
2473{
2474 int ret;
2475
2476 ret = iommu_go_to_state(IOMMU_ENABLED);
2477 if (ret)
2478 return ret;
2479
2480 irq_remapping_enabled = 1;
2481
2482 return 0;
2483}
2484
2485void amd_iommu_disable(void)
2486{
2487 amd_iommu_suspend();
2488}
2489
2490int amd_iommu_reenable(int mode)
2491{
2492 amd_iommu_resume();
2493
2494 return 0;
2495}
2496
2497int __init amd_iommu_enable_faulting(void)
2498{
2499 /* We enable MSI later when PCI is initialized */
2500 return 0;
2501}
2502#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002503
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002504/*
2505 * This is the core init function for AMD IOMMU hardware in the system.
2506 * This function is called from the generic x86 DMA layer initialization
2507 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002508 */
2509static int __init amd_iommu_init(void)
2510{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002511 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002512
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002513 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2514 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002515 free_dma_resources();
2516 if (!irq_remapping_enabled) {
2517 disable_iommus();
2518 free_on_init_error();
2519 } else {
2520 struct amd_iommu *iommu;
2521
2522 uninit_device_table_dma();
2523 for_each_iommu(iommu)
2524 iommu_flush_all_caches(iommu);
2525 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002526 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002527
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002528 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002529}
2530
Joerg Roedelb65233a2008-07-11 17:14:21 +02002531/****************************************************************************
2532 *
2533 * Early detect code. This code runs at IOMMU detection time in the DMA
2534 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2535 * IOMMUs
2536 *
2537 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002538int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002539{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002540 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002541
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002542 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002543 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002544
Joerg Roedela5235722010-05-11 17:12:33 +02002545 if (amd_iommu_disabled)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002546 return -ENODEV;
Joerg Roedela5235722010-05-11 17:12:33 +02002547
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002548 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2549 if (ret)
2550 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002551
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002552 amd_iommu_detected = true;
2553 iommu_detected = 1;
2554 x86_init.iommu.iommu_init = amd_iommu_init;
2555
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002556 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002557}
2558
Joerg Roedelb65233a2008-07-11 17:14:21 +02002559/****************************************************************************
2560 *
2561 * Parsing functions for the AMD IOMMU specific kernel command line
2562 * options.
2563 *
2564 ****************************************************************************/
2565
Joerg Roedelfefda112009-05-20 12:21:42 +02002566static int __init parse_amd_iommu_dump(char *str)
2567{
2568 amd_iommu_dump = true;
2569
2570 return 1;
2571}
2572
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002573static int __init parse_amd_iommu_intr(char *str)
2574{
2575 for (; *str; ++str) {
2576 if (strncmp(str, "legacy", 6) == 0) {
2577 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2578 break;
2579 }
2580 if (strncmp(str, "vapic", 5) == 0) {
2581 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2582 break;
2583 }
2584 }
2585 return 1;
2586}
2587
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002588static int __init parse_amd_iommu_options(char *str)
2589{
2590 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002591 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002592 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002593 if (strncmp(str, "off", 3) == 0)
2594 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002595 if (strncmp(str, "force_isolation", 15) == 0)
2596 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002597 }
2598
2599 return 1;
2600}
2601
Joerg Roedel440e89982013-04-09 16:35:28 +02002602static int __init parse_ivrs_ioapic(char *str)
2603{
2604 unsigned int bus, dev, fn;
2605 int ret, id, i;
2606 u16 devid;
2607
2608 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2609
2610 if (ret != 4) {
2611 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2612 return 1;
2613 }
2614
2615 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2616 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2617 str);
2618 return 1;
2619 }
2620
2621 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2622
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002623 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002624 i = early_ioapic_map_size++;
2625 early_ioapic_map[i].id = id;
2626 early_ioapic_map[i].devid = devid;
2627 early_ioapic_map[i].cmd_line = true;
2628
2629 return 1;
2630}
2631
2632static int __init parse_ivrs_hpet(char *str)
2633{
2634 unsigned int bus, dev, fn;
2635 int ret, id, i;
2636 u16 devid;
2637
2638 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2639
2640 if (ret != 4) {
2641 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2642 return 1;
2643 }
2644
2645 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2646 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2647 str);
2648 return 1;
2649 }
2650
2651 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2652
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002653 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002654 i = early_hpet_map_size++;
2655 early_hpet_map[i].id = id;
2656 early_hpet_map[i].devid = devid;
2657 early_hpet_map[i].cmd_line = true;
2658
2659 return 1;
2660}
2661
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002662static int __init parse_ivrs_acpihid(char *str)
2663{
2664 u32 bus, dev, fn;
2665 char *hid, *uid, *p;
2666 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2667 int ret, i;
2668
2669 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2670 if (ret != 4) {
2671 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2672 return 1;
2673 }
2674
2675 p = acpiid;
2676 hid = strsep(&p, ":");
2677 uid = p;
2678
2679 if (!hid || !(*hid) || !uid) {
2680 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2681 return 1;
2682 }
2683
2684 i = early_acpihid_map_size++;
2685 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2686 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2687 early_acpihid_map[i].devid =
2688 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2689 early_acpihid_map[i].cmd_line = true;
2690
2691 return 1;
2692}
2693
Joerg Roedel440e89982013-04-09 16:35:28 +02002694__setup("amd_iommu_dump", parse_amd_iommu_dump);
2695__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002696__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002697__setup("ivrs_ioapic", parse_ivrs_ioapic);
2698__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002699__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002700
2701IOMMU_INIT_FINISH(amd_iommu_detect,
2702 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002703 NULL,
2704 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002705
2706bool amd_iommu_v2_supported(void)
2707{
2708 return amd_iommu_v2_present;
2709}
2710EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002711
2712/****************************************************************************
2713 *
2714 * IOMMU EFR Performance Counter support functionality. This code allows
2715 * access to the IOMMU PC functionality.
2716 *
2717 ****************************************************************************/
2718
2719u8 amd_iommu_pc_get_max_banks(u16 devid)
2720{
2721 struct amd_iommu *iommu;
2722 u8 ret = 0;
2723
2724 /* locate the iommu governing the devid */
2725 iommu = amd_iommu_rlookup_table[devid];
2726 if (iommu)
2727 ret = iommu->max_banks;
2728
2729 return ret;
2730}
2731EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2732
2733bool amd_iommu_pc_supported(void)
2734{
2735 return amd_iommu_pc_present;
2736}
2737EXPORT_SYMBOL(amd_iommu_pc_supported);
2738
2739u8 amd_iommu_pc_get_max_counters(u16 devid)
2740{
2741 struct amd_iommu *iommu;
2742 u8 ret = 0;
2743
2744 /* locate the iommu governing the devid */
2745 iommu = amd_iommu_rlookup_table[devid];
2746 if (iommu)
2747 ret = iommu->max_counters;
2748
2749 return ret;
2750}
2751EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2752
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002753static int iommu_pc_get_set_reg_val(struct amd_iommu *iommu,
2754 u8 bank, u8 cntr, u8 fxn,
Steven L Kinney30861dd2013-06-05 16:11:48 -05002755 u64 *value, bool is_write)
2756{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002757 u32 offset;
2758 u32 max_offset_lim;
2759
Steven L Kinney30861dd2013-06-05 16:11:48 -05002760 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002761 if (WARN_ON((fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002762 return -ENODEV;
2763
2764 offset = (u32)(((0x40|bank) << 12) | (cntr << 8) | fxn);
2765
2766 /* Limit the offset to the hw defined mmio region aperture */
2767 max_offset_lim = (u32)(((0x40|iommu->max_banks) << 12) |
2768 (iommu->max_counters << 8) | 0x28);
2769 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2770 (offset > max_offset_lim))
2771 return -EINVAL;
2772
2773 if (is_write) {
2774 writel((u32)*value, iommu->mmio_base + offset);
2775 writel((*value >> 32), iommu->mmio_base + offset + 4);
2776 } else {
2777 *value = readl(iommu->mmio_base + offset + 4);
2778 *value <<= 32;
2779 *value = readl(iommu->mmio_base + offset);
2780 }
2781
2782 return 0;
2783}
2784EXPORT_SYMBOL(amd_iommu_pc_get_set_reg_val);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002785
2786int amd_iommu_pc_get_set_reg_val(u16 devid, u8 bank, u8 cntr, u8 fxn,
2787 u64 *value, bool is_write)
2788{
2789 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
2790
2791 /* Make sure the IOMMU PC resource is available */
2792 if (!amd_iommu_pc_present || iommu == NULL)
2793 return -ENODEV;
2794
2795 return iommu_pc_get_set_reg_val(iommu, bank, cntr, fxn,
2796 value, is_write);
2797}