blob: cf7896550e758535aa1d6579feef316d57b734c7 [file] [log] [blame]
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
21#include <linux/acpi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020022#include <linux/list.h>
Baoquan He5c87f622016-09-15 16:50:51 +080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010025#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020026#include <linux/interrupt.h>
27#include <linux/msi.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020028#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010029#include <linux/export.h>
Alex Williamson066f2e92014-06-12 16:12:37 -060030#include <linux/iommu.h>
Lucas Stachebcfa282016-10-26 13:09:53 +020031#include <linux/kmemleak.h>
Joerg Roedel54bd6352017-06-15 10:36:22 +020032#include <linux/crash_dump.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020033#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090034#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010035#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090036#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040037#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020038#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020039#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020040
41#include "amd_iommu_proto.h"
42#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020043#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020044
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020045/*
46 * definitions for the ACPI scanning code
47 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020048#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020049
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040050#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020051#define ACPI_IVMD_TYPE_ALL 0x20
52#define ACPI_IVMD_TYPE 0x21
53#define ACPI_IVMD_TYPE_RANGE 0x22
54
55#define IVHD_DEV_ALL 0x01
56#define IVHD_DEV_SELECT 0x02
57#define IVHD_DEV_SELECT_RANGE_START 0x03
58#define IVHD_DEV_RANGE_END 0x04
59#define IVHD_DEV_ALIAS 0x42
60#define IVHD_DEV_ALIAS_RANGE 0x43
61#define IVHD_DEV_EXT_SELECT 0x46
62#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020063#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040064#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020065
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040066#define UID_NOT_PRESENT 0
67#define UID_IS_INTEGER 1
68#define UID_IS_CHARACTER 2
69
Joerg Roedel6efed632012-06-14 15:52:58 +020070#define IVHD_SPECIAL_IOAPIC 1
71#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020072
Joerg Roedel6da73422009-05-04 11:44:38 +020073#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
74#define IVHD_FLAG_PASSPW_EN_MASK 0x02
75#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
76#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020077
78#define IVMD_FLAG_EXCL_RANGE 0x08
79#define IVMD_FLAG_UNITY_MAP 0x01
80
81#define ACPI_DEVFLAG_INITPASS 0x01
82#define ACPI_DEVFLAG_EXTINT 0x02
83#define ACPI_DEVFLAG_NMI 0x04
84#define ACPI_DEVFLAG_SYSMGT1 0x10
85#define ACPI_DEVFLAG_SYSMGT2 0x20
86#define ACPI_DEVFLAG_LINT0 0x40
87#define ACPI_DEVFLAG_LINT1 0x80
88#define ACPI_DEVFLAG_ATSDIS 0x10000000
89
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050090#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020091/*
92 * ACPI table definitions
93 *
94 * These data structures are laid over the table to parse the important values
95 * out of it.
96 */
97
Joerg Roedelb0119e82017-02-01 13:23:08 +010098extern const struct iommu_ops amd_iommu_ops;
99
Joerg Roedelb65233a2008-07-11 17:14:21 +0200100/*
101 * structure describing one IOMMU in the ACPI table. Typically followed by one
102 * or more ivhd_entrys.
103 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200104struct ivhd_header {
105 u8 type;
106 u8 flags;
107 u16 length;
108 u16 devid;
109 u16 cap_ptr;
110 u64 mmio_phys;
111 u16 pci_seg;
112 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400113 u32 efr_attr;
114
115 /* Following only valid on IVHD type 11h and 40h */
116 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
117 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200118} __attribute__((packed));
119
Joerg Roedelb65233a2008-07-11 17:14:21 +0200120/*
121 * A device entry describing which devices a specific IOMMU translates and
122 * which requestor ids they use.
123 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200124struct ivhd_entry {
125 u8 type;
126 u16 devid;
127 u8 flags;
128 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400129 u32 hidh;
130 u64 cid;
131 u8 uidf;
132 u8 uidl;
133 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200134} __attribute__((packed));
135
Joerg Roedelb65233a2008-07-11 17:14:21 +0200136/*
137 * An AMD IOMMU memory definition structure. It defines things like exclusion
138 * ranges for devices and regions that should be unity mapped.
139 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200140struct ivmd_header {
141 u8 type;
142 u8 flags;
143 u16 length;
144 u16 devid;
145 u16 aux;
146 u64 resv;
147 u64 range_start;
148 u64 range_length;
149} __attribute__((packed));
150
Joerg Roedelfefda112009-05-20 12:21:42 +0200151bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200152bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200153
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500154int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500155
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200156static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200157static bool __initdata amd_iommu_disabled;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400158static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200159
Joerg Roedelb65233a2008-07-11 17:14:21 +0200160u16 amd_iommu_last_bdf; /* largest PCI device id we have
161 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200162LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200163 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700164bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200165
Joerg Roedel2e228472008-07-11 17:14:31 +0200166LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200167 system */
168
Joerg Roedelbb527772009-11-20 14:31:51 +0100169/* Array to assign indices to IOMMUs*/
170struct amd_iommu *amd_iommus[MAX_IOMMUS];
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600171
172/* Number of IOMMUs present in the system */
173static int amd_iommus_present;
Joerg Roedelbb527772009-11-20 14:31:51 +0100174
Joerg Roedel318afd42009-11-23 18:32:38 +0100175/* IOMMUs have a non-present cache? */
176bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200177bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100178
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600179u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100180
Joerg Roedel400a28a2011-11-28 15:11:02 +0100181bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200182static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100183
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100184bool amd_iommu_force_isolation __read_mostly;
185
Joerg Roedelb65233a2008-07-11 17:14:21 +0200186/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100187 * List of protection domains - used during resume
188 */
189LIST_HEAD(amd_iommu_pd_list);
190spinlock_t amd_iommu_pd_lock;
191
192/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200193 * Pointer to the device table which is shared by all AMD IOMMUs
194 * it is indexed by the PCI device id or the HT unit id and contains
195 * information about the domain the device belongs to as well as the
196 * page table root pointer.
197 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200198struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200199
200/*
201 * The alias table is a driver specific data structure which contains the
202 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
203 * More than one device can share the same requestor id.
204 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200205u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200206
207/*
208 * The rlookup table is used to find the IOMMU which is responsible
209 * for a specific device. It is also indexed by the PCI device id.
210 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200211struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200212
213/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200214 * This table is used to find the irq remapping table for a given device id
215 * quickly.
216 */
217struct irq_remap_table **irq_lookup_table;
218
219/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200220 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200221 * to know which ones are already in use.
222 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200223unsigned long *amd_iommu_pd_alloc_bitmap;
224
Joerg Roedelb65233a2008-07-11 17:14:21 +0200225static u32 dev_table_size; /* size of the device table */
226static u32 alias_table_size; /* size of the alias table */
227static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200228
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200229enum iommu_init_state {
230 IOMMU_START_STATE,
231 IOMMU_IVRS_DETECTED,
232 IOMMU_ACPI_FINISHED,
233 IOMMU_ENABLED,
234 IOMMU_PCI_INIT,
235 IOMMU_INTERRUPTS_EN,
236 IOMMU_DMA_OPS,
237 IOMMU_INITIALIZED,
238 IOMMU_NOT_FOUND,
239 IOMMU_INIT_ERROR,
240};
241
Joerg Roedel235dacb2013-04-09 17:53:14 +0200242/* Early ioapic and hpet maps from kernel command line */
243#define EARLY_MAP_SIZE 4
244static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
245static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400246static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
247
Joerg Roedel235dacb2013-04-09 17:53:14 +0200248static int __initdata early_ioapic_map_size;
249static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400250static int __initdata early_acpihid_map_size;
251
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200252static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200253
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200254static enum iommu_init_state init_state = IOMMU_START_STATE;
255
Gerard Snitselaarae295142012-03-16 11:38:22 -0700256static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200257static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200258static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100259
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200260static inline void update_last_devid(u16 devid)
261{
262 if (devid > amd_iommu_last_bdf)
263 amd_iommu_last_bdf = devid;
264}
265
Joerg Roedelc5714842008-07-11 17:14:25 +0200266static inline unsigned long tbl_size(int entry_size)
267{
268 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100269 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200270
271 return 1UL << shift;
272}
273
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600274int amd_iommu_get_num_iommus(void)
275{
276 return amd_iommus_present;
277}
278
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400279/* Access to l1 and l2 indexed register spaces */
280
281static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
282{
283 u32 val;
284
285 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
286 pci_read_config_dword(iommu->dev, 0xfc, &val);
287 return val;
288}
289
290static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
291{
292 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
293 pci_write_config_dword(iommu->dev, 0xfc, val);
294 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
295}
296
297static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
298{
299 u32 val;
300
301 pci_write_config_dword(iommu->dev, 0xf0, address);
302 pci_read_config_dword(iommu->dev, 0xf4, &val);
303 return val;
304}
305
306static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
307{
308 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
309 pci_write_config_dword(iommu->dev, 0xf4, val);
310}
311
Joerg Roedelb65233a2008-07-11 17:14:21 +0200312/****************************************************************************
313 *
314 * AMD IOMMU MMIO register space handling functions
315 *
316 * These functions are used to program the IOMMU device registers in
317 * MMIO space required for that driver.
318 *
319 ****************************************************************************/
320
321/*
322 * This function set the exclusion range in the IOMMU. DMA accesses to the
323 * exclusion range are passed through untranslated
324 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200325static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200326{
327 u64 start = iommu->exclusion_start & PAGE_MASK;
328 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
329 u64 entry;
330
331 if (!iommu->exclusion_start)
332 return;
333
334 entry = start | MMIO_EXCL_ENABLE_MASK;
335 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
336 &entry, sizeof(entry));
337
338 entry = limit;
339 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
340 &entry, sizeof(entry));
341}
342
Joerg Roedelb65233a2008-07-11 17:14:21 +0200343/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000344static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200345{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200346 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200347
348 BUG_ON(iommu->mmio_base == NULL);
349
350 entry = virt_to_phys(amd_iommu_dev_table);
351 entry |= (dev_table_size >> 12) - 1;
352 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
353 &entry, sizeof(entry));
354}
355
Joerg Roedelb65233a2008-07-11 17:14:21 +0200356/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200357static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200358{
359 u32 ctrl;
360
361 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
362 ctrl |= (1 << bit);
363 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
364}
365
Joerg Roedelca0207112009-10-28 18:02:26 +0100366static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200367{
368 u32 ctrl;
369
Joerg Roedel199d0d52008-09-17 16:45:59 +0200370 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200371 ctrl &= ~(1 << bit);
372 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
373}
374
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100375static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
376{
377 u32 ctrl;
378
379 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
380 ctrl &= ~CTRL_INV_TO_MASK;
381 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
382 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
383}
384
Joerg Roedelb65233a2008-07-11 17:14:21 +0200385/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200386static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200387{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200388 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200389}
390
Joerg Roedel92ac4322009-05-19 19:06:27 +0200391static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200392{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200393 /* Disable command buffer */
394 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
395
396 /* Disable event logging and event interrupts */
397 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
398 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
399
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500400 /* Disable IOMMU GA_LOG */
401 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
402 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
403
Chris Wrighta8c485b2009-06-15 15:53:45 +0200404 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200405 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200406}
407
Joerg Roedelb65233a2008-07-11 17:14:21 +0200408/*
409 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
410 * the system has one.
411 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500412static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200413{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500414 if (!request_mem_region(address, end, "amd_iommu")) {
415 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
416 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200417 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200418 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200419 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200420
Steven L Kinney30861dd2013-06-05 16:11:48 -0500421 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200422}
423
424static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
425{
426 if (iommu->mmio_base)
427 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500428 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200429}
430
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400431static inline u32 get_ivhd_header_size(struct ivhd_header *h)
432{
433 u32 size = 0;
434
435 switch (h->type) {
436 case 0x10:
437 size = 24;
438 break;
439 case 0x11:
440 case 0x40:
441 size = 40;
442 break;
443 }
444 return size;
445}
446
Joerg Roedelb65233a2008-07-11 17:14:21 +0200447/****************************************************************************
448 *
449 * The functions below belong to the first pass of AMD IOMMU ACPI table
450 * parsing. In this pass we try to find out the highest device id this
451 * code has to handle. Upon this information the size of the shared data
452 * structures is determined later.
453 *
454 ****************************************************************************/
455
456/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200457 * This function calculates the length of a given IVHD entry
458 */
459static inline int ivhd_entry_length(u8 *ivhd)
460{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400461 u32 type = ((struct ivhd_entry *)ivhd)->type;
462
463 if (type < 0x80) {
464 return 0x04 << (*ivhd >> 6);
465 } else if (type == IVHD_DEV_ACPI_HID) {
466 /* For ACPI_HID, offset 21 is uid len */
467 return *((u8 *)ivhd + 21) + 22;
468 }
469 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200470}
471
472/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200473 * After reading the highest device id from the IOMMU PCI capability header
474 * this function looks if there is a higher device id defined in the ACPI table
475 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200476static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
477{
478 u8 *p = (void *)h, *end = (void *)h;
479 struct ivhd_entry *dev;
480
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400481 u32 ivhd_size = get_ivhd_header_size(h);
482
483 if (!ivhd_size) {
484 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
485 return -EINVAL;
486 }
487
488 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200489 end += h->length;
490
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200491 while (p < end) {
492 dev = (struct ivhd_entry *)p;
493 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200494 case IVHD_DEV_ALL:
495 /* Use maximum BDF value for DEV_ALL */
496 update_last_devid(0xffff);
497 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200498 case IVHD_DEV_SELECT:
499 case IVHD_DEV_RANGE_END:
500 case IVHD_DEV_ALIAS:
501 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200502 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200503 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200504 break;
505 default:
506 break;
507 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200508 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200509 }
510
511 WARN_ON(p != end);
512
513 return 0;
514}
515
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400516static int __init check_ivrs_checksum(struct acpi_table_header *table)
517{
518 int i;
519 u8 checksum = 0, *p = (u8 *)table;
520
521 for (i = 0; i < table->length; ++i)
522 checksum += p[i];
523 if (checksum != 0) {
524 /* ACPI table corrupt */
525 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
526 return -ENODEV;
527 }
528
529 return 0;
530}
531
Joerg Roedelb65233a2008-07-11 17:14:21 +0200532/*
533 * Iterate over all IVHD entries in the ACPI table and find the highest device
534 * id which we need to handle. This is the first of three functions which parse
535 * the ACPI table. So we check the checksum here.
536 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200537static int __init find_last_devid_acpi(struct acpi_table_header *table)
538{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400539 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200540 struct ivhd_header *h;
541
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200542 p += IVRS_HEADER_LENGTH;
543
544 end += table->length;
545 while (p < end) {
546 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400547 if (h->type == amd_iommu_target_ivhd_type) {
548 int ret = find_last_devid_from_ivhd(h);
549
550 if (ret)
551 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200552 }
553 p += h->length;
554 }
555 WARN_ON(p != end);
556
557 return 0;
558}
559
Joerg Roedelb65233a2008-07-11 17:14:21 +0200560/****************************************************************************
561 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200562 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200563 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
564 * data structures, initialize the device/alias/rlookup table and also
565 * basically initialize the hardware.
566 *
567 ****************************************************************************/
568
569/*
570 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
571 * write commands to that buffer later and the IOMMU will execute them
572 * asynchronously
573 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200574static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200575{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200576 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
577 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200578
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200579 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200580}
581
582/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200583 * This function resets the command buffer if the IOMMU stopped fetching
584 * commands from it.
585 */
586void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
587{
588 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
589
590 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
591 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Tom Lendackyd334a562017-06-05 14:52:12 -0500592 iommu->cmd_buf_head = 0;
593 iommu->cmd_buf_tail = 0;
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200594
595 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
596}
597
598/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200599 * This function writes the command buffer address to the hardware and
600 * enables it.
601 */
602static void iommu_enable_command_buffer(struct amd_iommu *iommu)
603{
604 u64 entry;
605
606 BUG_ON(iommu->cmd_buf == NULL);
607
608 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200609 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200610
Joerg Roedelb36ca912008-06-26 21:27:45 +0200611 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200612 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200613
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200614 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200615}
616
617static void __init free_command_buffer(struct amd_iommu *iommu)
618{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200619 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200620}
621
Joerg Roedel335503e2008-09-05 14:29:07 +0200622/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200623static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200624{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200625 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
626 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200627
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200628 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200629}
630
631static void iommu_enable_event_buffer(struct amd_iommu *iommu)
632{
633 u64 entry;
634
635 BUG_ON(iommu->evt_buf == NULL);
636
Joerg Roedel335503e2008-09-05 14:29:07 +0200637 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200638
Joerg Roedel335503e2008-09-05 14:29:07 +0200639 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
640 &entry, sizeof(entry));
641
Joerg Roedel090672072009-06-15 16:06:48 +0200642 /* set head and tail to zero manually */
643 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
644 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
645
Joerg Roedel58492e12009-05-04 18:41:16 +0200646 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200647}
648
649static void __init free_event_buffer(struct amd_iommu *iommu)
650{
651 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
652}
653
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100654/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200655static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100656{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200657 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
658 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100659
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200660 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100661}
662
663static void iommu_enable_ppr_log(struct amd_iommu *iommu)
664{
665 u64 entry;
666
667 if (iommu->ppr_log == NULL)
668 return;
669
670 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
671
672 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
673 &entry, sizeof(entry));
674
675 /* set head and tail to zero manually */
676 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
677 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
678
679 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
680 iommu_feature_enable(iommu, CONTROL_PPR_EN);
681}
682
683static void __init free_ppr_log(struct amd_iommu *iommu)
684{
685 if (iommu->ppr_log == NULL)
686 return;
687
688 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
689}
690
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500691static void free_ga_log(struct amd_iommu *iommu)
692{
693#ifdef CONFIG_IRQ_REMAP
694 if (iommu->ga_log)
695 free_pages((unsigned long)iommu->ga_log,
696 get_order(GA_LOG_SIZE));
697 if (iommu->ga_log_tail)
698 free_pages((unsigned long)iommu->ga_log_tail,
699 get_order(8));
700#endif
701}
702
703static int iommu_ga_log_enable(struct amd_iommu *iommu)
704{
705#ifdef CONFIG_IRQ_REMAP
706 u32 status, i;
707
708 if (!iommu->ga_log)
709 return -EINVAL;
710
711 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
712
713 /* Check if already running */
714 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
715 return 0;
716
717 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
718 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
719
720 for (i = 0; i < LOOP_TIMEOUT; ++i) {
721 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
722 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
723 break;
724 }
725
726 if (i >= LOOP_TIMEOUT)
727 return -EINVAL;
728#endif /* CONFIG_IRQ_REMAP */
729 return 0;
730}
731
732#ifdef CONFIG_IRQ_REMAP
733static int iommu_init_ga_log(struct amd_iommu *iommu)
734{
735 u64 entry;
736
737 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
738 return 0;
739
740 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
741 get_order(GA_LOG_SIZE));
742 if (!iommu->ga_log)
743 goto err_out;
744
745 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
746 get_order(8));
747 if (!iommu->ga_log_tail)
748 goto err_out;
749
750 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
751 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
752 &entry, sizeof(entry));
753 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
754 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
755 &entry, sizeof(entry));
756 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
757 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
758
759 return 0;
760err_out:
761 free_ga_log(iommu);
762 return -EINVAL;
763}
764#endif /* CONFIG_IRQ_REMAP */
765
766static int iommu_init_ga(struct amd_iommu *iommu)
767{
768 int ret = 0;
769
770#ifdef CONFIG_IRQ_REMAP
771 /* Note: We have already checked GASup from IVRS table.
772 * Now, we need to make sure that GAMSup is set.
773 */
774 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
775 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
776 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
777
778 ret = iommu_init_ga_log(iommu);
779#endif /* CONFIG_IRQ_REMAP */
780
781 return ret;
782}
783
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100784static void iommu_enable_gt(struct amd_iommu *iommu)
785{
786 if (!iommu_feature(iommu, FEATURE_GT))
787 return;
788
789 iommu_feature_enable(iommu, CONTROL_GT_EN);
790}
791
Joerg Roedelb65233a2008-07-11 17:14:21 +0200792/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200793static void set_dev_entry_bit(u16 devid, u8 bit)
794{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100795 int i = (bit >> 6) & 0x03;
796 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200797
Joerg Roedelee6c2862011-11-09 12:06:03 +0100798 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200799}
800
Joerg Roedelc5cca142009-10-09 18:31:20 +0200801static int get_dev_entry_bit(u16 devid, u8 bit)
802{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100803 int i = (bit >> 6) & 0x03;
804 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200805
Joerg Roedelee6c2862011-11-09 12:06:03 +0100806 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200807}
808
809
810void amd_iommu_apply_erratum_63(u16 devid)
811{
812 int sysmgt;
813
814 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
815 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
816
817 if (sysmgt == 0x01)
818 set_dev_entry_bit(devid, DEV_ENTRY_IW);
819}
820
Joerg Roedel5ff47892008-07-14 20:11:18 +0200821/* Writes the specific IOMMU for a device into the rlookup table */
822static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
823{
824 amd_iommu_rlookup_table[devid] = iommu;
825}
826
Joerg Roedelb65233a2008-07-11 17:14:21 +0200827/*
828 * This function takes the device specific flags read from the ACPI
829 * table and sets up the device table entry with that information
830 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200831static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
832 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200833{
834 if (flags & ACPI_DEVFLAG_INITPASS)
835 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
836 if (flags & ACPI_DEVFLAG_EXTINT)
837 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
838 if (flags & ACPI_DEVFLAG_NMI)
839 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
840 if (flags & ACPI_DEVFLAG_SYSMGT1)
841 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
842 if (flags & ACPI_DEVFLAG_SYSMGT2)
843 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
844 if (flags & ACPI_DEVFLAG_LINT0)
845 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
846 if (flags & ACPI_DEVFLAG_LINT1)
847 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200848
Joerg Roedelc5cca142009-10-09 18:31:20 +0200849 amd_iommu_apply_erratum_63(devid);
850
Joerg Roedel5ff47892008-07-14 20:11:18 +0200851 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200852}
853
Joerg Roedelc50e3242014-09-09 15:59:37 +0200854static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200855{
856 struct devid_map *entry;
857 struct list_head *list;
858
Joerg Roedel31cff672013-04-09 16:53:58 +0200859 if (type == IVHD_SPECIAL_IOAPIC)
860 list = &ioapic_map;
861 else if (type == IVHD_SPECIAL_HPET)
862 list = &hpet_map;
863 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200864 return -EINVAL;
865
Joerg Roedel31cff672013-04-09 16:53:58 +0200866 list_for_each_entry(entry, list, list) {
867 if (!(entry->id == id && entry->cmd_line))
868 continue;
869
870 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
871 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
872
Joerg Roedelc50e3242014-09-09 15:59:37 +0200873 *devid = entry->devid;
874
Joerg Roedel31cff672013-04-09 16:53:58 +0200875 return 0;
876 }
877
Joerg Roedel6efed632012-06-14 15:52:58 +0200878 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
879 if (!entry)
880 return -ENOMEM;
881
Joerg Roedel31cff672013-04-09 16:53:58 +0200882 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +0200883 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +0200884 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +0200885
886 list_add_tail(&entry->list, list);
887
888 return 0;
889}
890
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400891static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
892 bool cmd_line)
893{
894 struct acpihid_map_entry *entry;
895 struct list_head *list = &acpihid_map;
896
897 list_for_each_entry(entry, list, list) {
898 if (strcmp(entry->hid, hid) ||
899 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
900 !entry->cmd_line)
901 continue;
902
903 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
904 hid, uid);
905 *devid = entry->devid;
906 return 0;
907 }
908
909 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
910 if (!entry)
911 return -ENOMEM;
912
913 memcpy(entry->uid, uid, strlen(uid));
914 memcpy(entry->hid, hid, strlen(hid));
915 entry->devid = *devid;
916 entry->cmd_line = cmd_line;
917 entry->root_devid = (entry->devid & (~0x7));
918
919 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
920 entry->cmd_line ? "cmd" : "ivrs",
921 entry->hid, entry->uid, entry->root_devid);
922
923 list_add_tail(&entry->list, list);
924 return 0;
925}
926
Joerg Roedel235dacb2013-04-09 17:53:14 +0200927static int __init add_early_maps(void)
928{
929 int i, ret;
930
931 for (i = 0; i < early_ioapic_map_size; ++i) {
932 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
933 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200934 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200935 early_ioapic_map[i].cmd_line);
936 if (ret)
937 return ret;
938 }
939
940 for (i = 0; i < early_hpet_map_size; ++i) {
941 ret = add_special_device(IVHD_SPECIAL_HPET,
942 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200943 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200944 early_hpet_map[i].cmd_line);
945 if (ret)
946 return ret;
947 }
948
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400949 for (i = 0; i < early_acpihid_map_size; ++i) {
950 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
951 early_acpihid_map[i].uid,
952 &early_acpihid_map[i].devid,
953 early_acpihid_map[i].cmd_line);
954 if (ret)
955 return ret;
956 }
957
Joerg Roedel235dacb2013-04-09 17:53:14 +0200958 return 0;
959}
960
Joerg Roedelb65233a2008-07-11 17:14:21 +0200961/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200962 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +0200963 * it
964 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200965static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
966{
967 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
968
969 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
970 return;
971
972 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200973 /*
974 * We only can configure exclusion ranges per IOMMU, not
975 * per device. But we can enable the exclusion range per
976 * device. This is done here
977 */
Su Friendy2c16c9f2014-05-07 13:54:52 +0800978 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +0200979 iommu->exclusion_start = m->range_start;
980 iommu->exclusion_length = m->range_length;
981 }
982}
983
Joerg Roedelb65233a2008-07-11 17:14:21 +0200984/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200985 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
986 * initializes the hardware and our data structures with it.
987 */
Joerg Roedel6efed632012-06-14 15:52:58 +0200988static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200989 struct ivhd_header *h)
990{
991 u8 *p = (u8 *)h;
992 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +0200993 u16 devid = 0, devid_start = 0, devid_to = 0;
994 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200995 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200996 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400997 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200998 int ret;
999
1000
1001 ret = add_early_maps();
1002 if (ret)
1003 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001004
1005 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001006 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001007 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001008 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001009
1010 /*
1011 * Done. Now parse the device entries
1012 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001013 ivhd_size = get_ivhd_header_size(h);
1014 if (!ivhd_size) {
1015 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1016 return -EINVAL;
1017 }
1018
1019 p += ivhd_size;
1020
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001021 end += h->length;
1022
Joerg Roedel42a698f2009-05-20 15:41:28 +02001023
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001024 while (p < end) {
1025 e = (struct ivhd_entry *)p;
1026 switch (e->type) {
1027 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001028
Joerg Roedel226e8892015-10-20 17:33:44 +02001029 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001030
Joerg Roedel226e8892015-10-20 17:33:44 +02001031 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1032 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001033 break;
1034 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001035
1036 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1037 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001038 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001039 PCI_SLOT(e->devid),
1040 PCI_FUNC(e->devid),
1041 e->flags);
1042
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001043 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001044 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001045 break;
1046 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001047
1048 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1049 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001050 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001051 PCI_SLOT(e->devid),
1052 PCI_FUNC(e->devid),
1053 e->flags);
1054
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001055 devid_start = e->devid;
1056 flags = e->flags;
1057 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001058 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001059 break;
1060 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001061
1062 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1063 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001064 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001065 PCI_SLOT(e->devid),
1066 PCI_FUNC(e->devid),
1067 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001068 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001069 PCI_SLOT(e->ext >> 8),
1070 PCI_FUNC(e->ext >> 8));
1071
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001072 devid = e->devid;
1073 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001074 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001075 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001076 amd_iommu_alias_table[devid] = devid_to;
1077 break;
1078 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001079
1080 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1081 "devid: %02x:%02x.%x flags: %02x "
1082 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001083 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001084 PCI_SLOT(e->devid),
1085 PCI_FUNC(e->devid),
1086 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001087 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001088 PCI_SLOT(e->ext >> 8),
1089 PCI_FUNC(e->ext >> 8));
1090
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001091 devid_start = e->devid;
1092 flags = e->flags;
1093 devid_to = e->ext >> 8;
1094 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001095 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001096 break;
1097 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001098
1099 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1100 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001101 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001102 PCI_SLOT(e->devid),
1103 PCI_FUNC(e->devid),
1104 e->flags, e->ext);
1105
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001106 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001107 set_dev_entry_from_acpi(iommu, devid, e->flags,
1108 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001109 break;
1110 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001111
1112 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1113 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001114 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001115 PCI_SLOT(e->devid),
1116 PCI_FUNC(e->devid),
1117 e->flags, e->ext);
1118
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001119 devid_start = e->devid;
1120 flags = e->flags;
1121 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001122 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001123 break;
1124 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001125
1126 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001127 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001128 PCI_SLOT(e->devid),
1129 PCI_FUNC(e->devid));
1130
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001131 devid = e->devid;
1132 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001133 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001134 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001135 set_dev_entry_from_acpi(iommu,
1136 devid_to, flags, ext_flags);
1137 }
1138 set_dev_entry_from_acpi(iommu, dev_i,
1139 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001140 }
1141 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001142 case IVHD_DEV_SPECIAL: {
1143 u8 handle, type;
1144 const char *var;
1145 u16 devid;
1146 int ret;
1147
1148 handle = e->ext & 0xff;
1149 devid = (e->ext >> 8) & 0xffff;
1150 type = (e->ext >> 24) & 0xff;
1151
1152 if (type == IVHD_SPECIAL_IOAPIC)
1153 var = "IOAPIC";
1154 else if (type == IVHD_SPECIAL_HPET)
1155 var = "HPET";
1156 else
1157 var = "UNKNOWN";
1158
1159 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1160 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001161 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001162 PCI_SLOT(devid),
1163 PCI_FUNC(devid));
1164
Joerg Roedelc50e3242014-09-09 15:59:37 +02001165 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001166 if (ret)
1167 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001168
1169 /*
1170 * add_special_device might update the devid in case a
1171 * command-line override is present. So call
1172 * set_dev_entry_from_acpi after add_special_device.
1173 */
1174 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1175
Joerg Roedel6efed632012-06-14 15:52:58 +02001176 break;
1177 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001178 case IVHD_DEV_ACPI_HID: {
1179 u16 devid;
1180 u8 hid[ACPIHID_HID_LEN] = {0};
1181 u8 uid[ACPIHID_UID_LEN] = {0};
1182 int ret;
1183
1184 if (h->type != 0x40) {
1185 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1186 e->type);
1187 break;
1188 }
1189
1190 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1191 hid[ACPIHID_HID_LEN - 1] = '\0';
1192
1193 if (!(*hid)) {
1194 pr_err(FW_BUG "Invalid HID.\n");
1195 break;
1196 }
1197
1198 switch (e->uidf) {
1199 case UID_NOT_PRESENT:
1200
1201 if (e->uidl != 0)
1202 pr_warn(FW_BUG "Invalid UID length.\n");
1203
1204 break;
1205 case UID_IS_INTEGER:
1206
1207 sprintf(uid, "%d", e->uid);
1208
1209 break;
1210 case UID_IS_CHARACTER:
1211
1212 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1213 uid[ACPIHID_UID_LEN - 1] = '\0';
1214
1215 break;
1216 default:
1217 break;
1218 }
1219
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001220 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001221 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1222 hid, uid,
1223 PCI_BUS_NUM(devid),
1224 PCI_SLOT(devid),
1225 PCI_FUNC(devid));
1226
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001227 flags = e->flags;
1228
1229 ret = add_acpi_hid_device(hid, uid, &devid, false);
1230 if (ret)
1231 return ret;
1232
1233 /*
1234 * add_special_device might update the devid in case a
1235 * command-line override is present. So call
1236 * set_dev_entry_from_acpi after add_special_device.
1237 */
1238 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1239
1240 break;
1241 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001242 default:
1243 break;
1244 }
1245
Joerg Roedelb514e552008-09-17 17:14:27 +02001246 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001247 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001248
1249 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001250}
1251
Joerg Roedele47d4022008-06-26 21:27:48 +02001252static void __init free_iommu_one(struct amd_iommu *iommu)
1253{
1254 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001255 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001256 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001257 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001258 iommu_unmap_mmio_space(iommu);
1259}
1260
1261static void __init free_iommu_all(void)
1262{
1263 struct amd_iommu *iommu, *next;
1264
Joerg Roedel3bd22172009-05-04 15:06:20 +02001265 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001266 list_del(&iommu->list);
1267 free_iommu_one(iommu);
1268 kfree(iommu);
1269 }
1270}
1271
Joerg Roedelb65233a2008-07-11 17:14:21 +02001272/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001273 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1274 * Workaround:
1275 * BIOS should disable L2B micellaneous clock gating by setting
1276 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1277 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001278static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001279{
1280 u32 value;
1281
1282 if ((boot_cpu_data.x86 != 0x15) ||
1283 (boot_cpu_data.x86_model < 0x10) ||
1284 (boot_cpu_data.x86_model > 0x1f))
1285 return;
1286
1287 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1288 pci_read_config_dword(iommu->dev, 0xf4, &value);
1289
1290 if (value & BIT(2))
1291 return;
1292
1293 /* Select NB indirect register 0x90 and enable writing */
1294 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1295
1296 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1297 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1298 dev_name(&iommu->dev->dev));
1299
1300 /* Clear the enable writing bit */
1301 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1302}
1303
1304/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001305 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1306 * Workaround:
1307 * BIOS should enable ATS write permission check by setting
1308 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1309 */
1310static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1311{
1312 u32 value;
1313
1314 if ((boot_cpu_data.x86 != 0x15) ||
1315 (boot_cpu_data.x86_model < 0x30) ||
1316 (boot_cpu_data.x86_model > 0x3f))
1317 return;
1318
1319 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1320 value = iommu_read_l2(iommu, 0x47);
1321
1322 if (value & BIT(0))
1323 return;
1324
1325 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1326 iommu_write_l2(iommu, 0x47, value | BIT(0));
1327
1328 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1329 dev_name(&iommu->dev->dev));
1330}
1331
1332/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001333 * This function clues the initialization function for one IOMMU
1334 * together and also allocates the command buffer and programs the
1335 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1336 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001337static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1338{
Joerg Roedel6efed632012-06-14 15:52:58 +02001339 int ret;
1340
Joerg Roedele47d4022008-06-26 21:27:48 +02001341 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001342
1343 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001344 list_add_tail(&iommu->list, &amd_iommu_list);
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001345 iommu->index = amd_iommus_present++;
Joerg Roedelbb527772009-11-20 14:31:51 +01001346
1347 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1348 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1349 return -ENOSYS;
1350 }
1351
1352 /* Index is fine - add IOMMU to the array */
1353 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001354
1355 /*
1356 * Copy data from ACPI table entry to the iommu struct
1357 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001358 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001359 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001360 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001361 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001362
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001363 switch (h->type) {
1364 case 0x10:
1365 /* Check if IVHD EFR contains proper max banks/counters */
1366 if ((h->efr_attr != 0) &&
1367 ((h->efr_attr & (0xF << 13)) != 0) &&
1368 ((h->efr_attr & (0x3F << 17)) != 0))
1369 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1370 else
1371 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001372 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1373 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001374 break;
1375 case 0x11:
1376 case 0x40:
1377 if (h->efr_reg & (1 << 9))
1378 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1379 else
1380 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001381 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1382 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001383 break;
1384 default:
1385 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001386 }
1387
1388 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1389 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001390 if (!iommu->mmio_base)
1391 return -ENOMEM;
1392
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001393 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001394 return -ENOMEM;
1395
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001396 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001397 return -ENOMEM;
1398
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001399 iommu->int_enabled = false;
1400
Joerg Roedel6efed632012-06-14 15:52:58 +02001401 ret = init_iommu_from_acpi(iommu, h);
1402 if (ret)
1403 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001404
Jiang Liu7c71d302015-04-13 14:11:33 +08001405 ret = amd_iommu_create_irq_domain(iommu);
1406 if (ret)
1407 return ret;
1408
Joerg Roedelf6fec002012-06-21 16:51:25 +02001409 /*
1410 * Make sure IOMMU is not considered to translate itself. The IVRS
1411 * table tells us so, but this is a lie!
1412 */
1413 amd_iommu_rlookup_table[iommu->devid] = NULL;
1414
Joerg Roedel23c742d2012-06-12 11:47:34 +02001415 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001416}
1417
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001418/**
1419 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1420 * @ivrs Pointer to the IVRS header
1421 *
1422 * This function search through all IVDB of the maximum supported IVHD
1423 */
1424static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1425{
1426 u8 *base = (u8 *)ivrs;
1427 struct ivhd_header *ivhd = (struct ivhd_header *)
1428 (base + IVRS_HEADER_LENGTH);
1429 u8 last_type = ivhd->type;
1430 u16 devid = ivhd->devid;
1431
1432 while (((u8 *)ivhd - base < ivrs->length) &&
1433 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1434 u8 *p = (u8 *) ivhd;
1435
1436 if (ivhd->devid == devid)
1437 last_type = ivhd->type;
1438 ivhd = (struct ivhd_header *)(p + ivhd->length);
1439 }
1440
1441 return last_type;
1442}
1443
Joerg Roedelb65233a2008-07-11 17:14:21 +02001444/*
1445 * Iterates over all IOMMU entries in the ACPI table, allocates the
1446 * IOMMU structure and initializes it with init_iommu_one()
1447 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001448static int __init init_iommu_all(struct acpi_table_header *table)
1449{
1450 u8 *p = (u8 *)table, *end = (u8 *)table;
1451 struct ivhd_header *h;
1452 struct amd_iommu *iommu;
1453 int ret;
1454
Joerg Roedele47d4022008-06-26 21:27:48 +02001455 end += table->length;
1456 p += IVRS_HEADER_LENGTH;
1457
1458 while (p < end) {
1459 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001460 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001461
Joerg Roedelae908c22009-09-01 16:52:16 +02001462 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001463 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001464 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001465 PCI_FUNC(h->devid), h->cap_ptr,
1466 h->pci_seg, h->flags, h->info);
1467 DUMP_printk(" mmio-addr: %016llx\n",
1468 h->mmio_phys);
1469
Joerg Roedele47d4022008-06-26 21:27:48 +02001470 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001471 if (iommu == NULL)
1472 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001473
Joerg Roedele47d4022008-06-26 21:27:48 +02001474 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001475 if (ret)
1476 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001477 }
1478 p += h->length;
1479
1480 }
1481 WARN_ON(p != end);
1482
1483 return 0;
1484}
1485
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001486static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1487 u8 fxn, u64 *value, bool is_write);
Steven L Kinney30861dd2013-06-05 16:11:48 -05001488
1489static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1490{
1491 u64 val = 0xabcd, val2 = 0;
1492
1493 if (!iommu_feature(iommu, FEATURE_PC))
1494 return;
1495
1496 amd_iommu_pc_present = true;
1497
1498 /* Check if the performance counters can be written to */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001499 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1500 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001501 (val != val2)) {
1502 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1503 amd_iommu_pc_present = false;
1504 return;
1505 }
1506
1507 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1508
1509 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1510 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1511 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1512}
1513
Alex Williamson066f2e92014-06-12 16:12:37 -06001514static ssize_t amd_iommu_show_cap(struct device *dev,
1515 struct device_attribute *attr,
1516 char *buf)
1517{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001518 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001519 return sprintf(buf, "%x\n", iommu->cap);
1520}
1521static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1522
1523static ssize_t amd_iommu_show_features(struct device *dev,
1524 struct device_attribute *attr,
1525 char *buf)
1526{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001527 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001528 return sprintf(buf, "%llx\n", iommu->features);
1529}
1530static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1531
1532static struct attribute *amd_iommu_attrs[] = {
1533 &dev_attr_cap.attr,
1534 &dev_attr_features.attr,
1535 NULL,
1536};
1537
1538static struct attribute_group amd_iommu_group = {
1539 .name = "amd-iommu",
1540 .attrs = amd_iommu_attrs,
1541};
1542
1543static const struct attribute_group *amd_iommu_groups[] = {
1544 &amd_iommu_group,
1545 NULL,
1546};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001547
Joerg Roedel23c742d2012-06-12 11:47:34 +02001548static int iommu_init_pci(struct amd_iommu *iommu)
1549{
1550 int cap_ptr = iommu->cap_ptr;
1551 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001552 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001553
Shuah Khanc5081cd2013-02-27 17:07:19 -07001554 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001555 iommu->devid & 0xff);
1556 if (!iommu->dev)
1557 return -ENODEV;
1558
Jiang Liucbbc00b2015-10-09 22:07:31 +08001559 /* Prevent binding other PCI device drivers to IOMMU devices */
1560 iommu->dev->match_driver = false;
1561
Joerg Roedel23c742d2012-06-12 11:47:34 +02001562 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1563 &iommu->cap);
1564 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1565 &range);
1566 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1567 &misc);
1568
Joerg Roedel23c742d2012-06-12 11:47:34 +02001569 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1570 amd_iommu_iotlb_sup = false;
1571
1572 /* read extended feature bits */
1573 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1574 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1575
1576 iommu->features = ((u64)high << 32) | low;
1577
1578 if (iommu_feature(iommu, FEATURE_GT)) {
1579 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001580 u32 max_pasid;
1581 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001582
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001583 pasmax = iommu->features & FEATURE_PASID_MASK;
1584 pasmax >>= FEATURE_PASID_SHIFT;
1585 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001586
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001587 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1588
1589 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001590
1591 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1592 glxval >>= FEATURE_GLXVAL_SHIFT;
1593
1594 if (amd_iommu_max_glx_val == -1)
1595 amd_iommu_max_glx_val = glxval;
1596 else
1597 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1598 }
1599
1600 if (iommu_feature(iommu, FEATURE_GT) &&
1601 iommu_feature(iommu, FEATURE_PPR)) {
1602 iommu->is_iommu_v2 = true;
1603 amd_iommu_v2_present = true;
1604 }
1605
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001606 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1607 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001608
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001609 ret = iommu_init_ga(iommu);
1610 if (ret)
1611 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001612
Joerg Roedel23c742d2012-06-12 11:47:34 +02001613 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1614 amd_iommu_np_cache = true;
1615
Steven L Kinney30861dd2013-06-05 16:11:48 -05001616 init_iommu_perf_ctr(iommu);
1617
Joerg Roedel23c742d2012-06-12 11:47:34 +02001618 if (is_rd890_iommu(iommu->dev)) {
1619 int i, j;
1620
1621 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1622 PCI_DEVFN(0, 0));
1623
1624 /*
1625 * Some rd890 systems may not be fully reconfigured by the
1626 * BIOS, so it's necessary for us to store this information so
1627 * it can be reprogrammed on resume
1628 */
1629 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1630 &iommu->stored_addr_lo);
1631 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1632 &iommu->stored_addr_hi);
1633
1634 /* Low bit locks writes to configuration space */
1635 iommu->stored_addr_lo &= ~1;
1636
1637 for (i = 0; i < 6; i++)
1638 for (j = 0; j < 0x12; j++)
1639 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1640
1641 for (i = 0; i < 0x83; i++)
1642 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1643 }
1644
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001645 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001646 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001647
Joerg Roedel39ab9552017-02-01 16:56:46 +01001648 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1649 amd_iommu_groups, "ivhd%d", iommu->index);
Joerg Roedelb0119e82017-02-01 13:23:08 +01001650 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1651 iommu_device_register(&iommu->iommu);
Alex Williamson066f2e92014-06-12 16:12:37 -06001652
Joerg Roedel23c742d2012-06-12 11:47:34 +02001653 return pci_enable_device(iommu->dev);
1654}
1655
Joerg Roedel4d121c32012-06-14 12:21:55 +02001656static void print_iommu_info(void)
1657{
1658 static const char * const feat_str[] = {
1659 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1660 "IA", "GA", "HE", "PC"
1661 };
1662 struct amd_iommu *iommu;
1663
1664 for_each_iommu(iommu) {
1665 int i;
1666
1667 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1668 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1669
1670 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001671 pr_info("AMD-Vi: Extended features (%#llx):\n",
1672 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001673 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001674 if (iommu_feature(iommu, (1ULL << i)))
1675 pr_cont(" %s", feat_str[i]);
1676 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001677
1678 if (iommu->features & FEATURE_GAM_VAPIC)
1679 pr_cont(" GA_vAPIC");
1680
Steven L Kinney30861dd2013-06-05 16:11:48 -05001681 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001682 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001683 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001684 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001685 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001686 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1687 pr_info("AMD-Vi: virtual APIC enabled\n");
1688 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001689}
1690
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001691static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001692{
1693 struct amd_iommu *iommu;
1694 int ret = 0;
1695
1696 for_each_iommu(iommu) {
1697 ret = iommu_init_pci(iommu);
1698 if (ret)
1699 break;
1700 }
1701
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001702 /*
1703 * Order is important here to make sure any unity map requirements are
1704 * fulfilled. The unity mappings are created and written to the device
1705 * table during the amd_iommu_init_api() call.
1706 *
1707 * After that we call init_device_table_dma() to make sure any
1708 * uninitialized DTE will block DMA, and in the end we flush the caches
1709 * of all IOMMUs to make sure the changes to the device table are
1710 * active.
1711 */
1712 ret = amd_iommu_init_api();
1713
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001714 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001715
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001716 for_each_iommu(iommu)
1717 iommu_flush_all_caches(iommu);
1718
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001719 if (!ret)
1720 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001721
Joerg Roedel23c742d2012-06-12 11:47:34 +02001722 return ret;
1723}
1724
Joerg Roedelb65233a2008-07-11 17:14:21 +02001725/****************************************************************************
1726 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001727 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001728 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001729 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1730 * pci_dev.
1731 *
1732 ****************************************************************************/
1733
Joerg Roedel9f800de2009-11-23 12:45:25 +01001734static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001735{
1736 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001737
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001738 r = pci_enable_msi(iommu->dev);
1739 if (r)
1740 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001741
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001742 r = request_threaded_irq(iommu->dev->irq,
1743 amd_iommu_int_handler,
1744 amd_iommu_int_thread,
1745 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001746 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001747
1748 if (r) {
1749 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001750 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001751 }
1752
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001753 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001754
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001755 return 0;
1756}
1757
Joerg Roedel05f92db2009-05-12 09:52:46 +02001758static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001759{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001760 int ret;
1761
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001762 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001763 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001764
Yijing Wang82fcfc62013-08-08 21:12:36 +08001765 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001766 ret = iommu_setup_msi(iommu);
1767 else
1768 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001769
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001770 if (ret)
1771 return ret;
1772
1773enable_faults:
1774 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1775
1776 if (iommu->ppr_log != NULL)
1777 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1778
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001779 iommu_ga_log_enable(iommu);
1780
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001781 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001782}
1783
1784/****************************************************************************
1785 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001786 * The next functions belong to the third pass of parsing the ACPI
1787 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001788 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001789 *
1790 ****************************************************************************/
1791
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001792static void __init free_unity_maps(void)
1793{
1794 struct unity_map_entry *entry, *next;
1795
1796 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1797 list_del(&entry->list);
1798 kfree(entry);
1799 }
1800}
1801
Joerg Roedelb65233a2008-07-11 17:14:21 +02001802/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001803static int __init init_exclusion_range(struct ivmd_header *m)
1804{
1805 int i;
1806
1807 switch (m->type) {
1808 case ACPI_IVMD_TYPE:
1809 set_device_exclusion_range(m->devid, m);
1810 break;
1811 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001812 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001813 set_device_exclusion_range(i, m);
1814 break;
1815 case ACPI_IVMD_TYPE_RANGE:
1816 for (i = m->devid; i <= m->aux; ++i)
1817 set_device_exclusion_range(i, m);
1818 break;
1819 default:
1820 break;
1821 }
1822
1823 return 0;
1824}
1825
Joerg Roedelb65233a2008-07-11 17:14:21 +02001826/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001827static int __init init_unity_map_range(struct ivmd_header *m)
1828{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001829 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001830 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001831
1832 e = kzalloc(sizeof(*e), GFP_KERNEL);
1833 if (e == NULL)
1834 return -ENOMEM;
1835
1836 switch (m->type) {
1837 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001838 kfree(e);
1839 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001840 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001841 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001842 e->devid_start = e->devid_end = m->devid;
1843 break;
1844 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001845 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001846 e->devid_start = 0;
1847 e->devid_end = amd_iommu_last_bdf;
1848 break;
1849 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001850 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001851 e->devid_start = m->devid;
1852 e->devid_end = m->aux;
1853 break;
1854 }
1855 e->address_start = PAGE_ALIGN(m->range_start);
1856 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1857 e->prot = m->flags >> 1;
1858
Joerg Roedel02acc432009-05-20 16:24:21 +02001859 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1860 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001861 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1862 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02001863 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1864 e->address_start, e->address_end, m->flags);
1865
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001866 list_add_tail(&e->list, &amd_iommu_unity_map);
1867
1868 return 0;
1869}
1870
Joerg Roedelb65233a2008-07-11 17:14:21 +02001871/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001872static int __init init_memory_definitions(struct acpi_table_header *table)
1873{
1874 u8 *p = (u8 *)table, *end = (u8 *)table;
1875 struct ivmd_header *m;
1876
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001877 end += table->length;
1878 p += IVRS_HEADER_LENGTH;
1879
1880 while (p < end) {
1881 m = (struct ivmd_header *)p;
1882 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1883 init_exclusion_range(m);
1884 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1885 init_unity_map_range(m);
1886
1887 p += m->length;
1888 }
1889
1890 return 0;
1891}
1892
Joerg Roedelb65233a2008-07-11 17:14:21 +02001893/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001894 * Init the device table to not allow DMA access for devices and
1895 * suppress all page faults
1896 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02001897static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001898{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001899 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001900
1901 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1902 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1903 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel54bd6352017-06-15 10:36:22 +02001904 /*
1905 * In kdump kernels in-flight DMA from the old kernel might
1906 * cause IO_PAGE_FAULTs. There are no reports that a kdump
1907 * actually failed because of that, so just disable fault
1908 * reporting in the hardware to get rid of the messages
1909 */
1910 if (is_kdump_kernel())
1911 set_dev_entry_bit(devid, DEV_ENTRY_NO_PAGE_FAULT);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001912 }
1913}
1914
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001915static void __init uninit_device_table_dma(void)
1916{
1917 u32 devid;
1918
1919 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1920 amd_iommu_dev_table[devid].data[0] = 0ULL;
1921 amd_iommu_dev_table[devid].data[1] = 0ULL;
1922 }
1923}
1924
Joerg Roedel33f28c52012-06-15 18:03:31 +02001925static void init_device_table(void)
1926{
1927 u32 devid;
1928
1929 if (!amd_iommu_irq_remap)
1930 return;
1931
1932 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1933 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1934}
1935
Joerg Roedele9bf5192010-09-20 14:33:07 +02001936static void iommu_init_flags(struct amd_iommu *iommu)
1937{
1938 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1939 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1940 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1941
1942 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1943 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1944 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1945
1946 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1947 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1948 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1949
1950 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1951 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1952 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1953
1954 /*
1955 * make IOMMU memory accesses cache coherent
1956 */
1957 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001958
1959 /* Set IOTLB invalidation timeout to 1s */
1960 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001961}
1962
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001963static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001964{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001965 int i, j;
1966 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001967 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001968
1969 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001970 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001971 return;
1972
1973 /*
1974 * First, we need to ensure that the iommu is enabled. This is
1975 * controlled by a register in the northbridge
1976 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001977
1978 /* Select Northbridge indirect register 0x75 and enable writing */
1979 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1980 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1981
1982 /* Enable the iommu */
1983 if (!(ioc_feature_control & 0x1))
1984 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1985
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001986 /* Restore the iommu BAR */
1987 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1988 iommu->stored_addr_lo);
1989 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1990 iommu->stored_addr_hi);
1991
1992 /* Restore the l1 indirect regs for each of the 6 l1s */
1993 for (i = 0; i < 6; i++)
1994 for (j = 0; j < 0x12; j++)
1995 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1996
1997 /* Restore the l2 indirect regs */
1998 for (i = 0; i < 0x83; i++)
1999 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2000
2001 /* Lock PCI setup registers */
2002 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2003 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02002004}
2005
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002006static void iommu_enable_ga(struct amd_iommu *iommu)
2007{
2008#ifdef CONFIG_IRQ_REMAP
2009 switch (amd_iommu_guest_ir) {
2010 case AMD_IOMMU_GUEST_IR_VAPIC:
2011 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2012 /* Fall through */
2013 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2014 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002015 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002016 break;
2017 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002018 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002019 break;
2020 }
2021#endif
2022}
2023
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002024/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002025 * This function finally enables all IOMMUs found in the system after
2026 * they have been initialized
2027 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002028static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002029{
2030 struct amd_iommu *iommu;
2031
Joerg Roedel3bd22172009-05-04 15:06:20 +02002032 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02002033 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002034 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02002035 iommu_set_device_table(iommu);
2036 iommu_enable_command_buffer(iommu);
2037 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002038 iommu_set_exclusion_range(iommu);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002039 iommu_enable_ga(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002040 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02002041 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002042 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002043
2044#ifdef CONFIG_IRQ_REMAP
2045 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2046 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2047#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002048}
2049
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002050static void enable_iommus_v2(void)
2051{
2052 struct amd_iommu *iommu;
2053
2054 for_each_iommu(iommu) {
2055 iommu_enable_ppr_log(iommu);
2056 iommu_enable_gt(iommu);
2057 }
2058}
2059
2060static void enable_iommus(void)
2061{
2062 early_enable_iommus();
2063
2064 enable_iommus_v2();
2065}
2066
Joerg Roedel92ac4322009-05-19 19:06:27 +02002067static void disable_iommus(void)
2068{
2069 struct amd_iommu *iommu;
2070
2071 for_each_iommu(iommu)
2072 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002073
2074#ifdef CONFIG_IRQ_REMAP
2075 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2076 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2077#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002078}
2079
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002080/*
2081 * Suspend/Resume support
2082 * disable suspend until real resume implemented
2083 */
2084
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002085static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002086{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002087 struct amd_iommu *iommu;
2088
2089 for_each_iommu(iommu)
2090 iommu_apply_resume_quirks(iommu);
2091
Joerg Roedel736501e2009-05-12 09:56:12 +02002092 /* re-load the hardware */
2093 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002094
2095 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002096}
2097
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002098static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002099{
Joerg Roedel736501e2009-05-12 09:56:12 +02002100 /* disable IOMMUs to go out of the way for BIOS */
2101 disable_iommus();
2102
2103 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002104}
2105
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002106static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002107 .suspend = amd_iommu_suspend,
2108 .resume = amd_iommu_resume,
2109};
2110
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002111static void __init free_on_init_error(void)
2112{
Lucas Stachebcfa282016-10-26 13:09:53 +02002113 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002114 free_pages((unsigned long)irq_lookup_table,
2115 get_order(rlookup_table_size));
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002116
Julia Lawalla5919892015-09-13 14:15:31 +02002117 kmem_cache_destroy(amd_iommu_irq_cache);
2118 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002119
2120 free_pages((unsigned long)amd_iommu_rlookup_table,
2121 get_order(rlookup_table_size));
2122
2123 free_pages((unsigned long)amd_iommu_alias_table,
2124 get_order(alias_table_size));
2125
2126 free_pages((unsigned long)amd_iommu_dev_table,
2127 get_order(dev_table_size));
2128
2129 free_iommu_all();
2130
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002131#ifdef CONFIG_GART_IOMMU
2132 /*
2133 * We failed to initialize the AMD IOMMU - try fallback to GART
2134 * if possible.
2135 */
2136 gart_iommu_init();
2137
2138#endif
2139}
2140
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002141/* SB IOAPIC is always on this device in AMD systems */
2142#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2143
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002144static bool __init check_ioapic_information(void)
2145{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002146 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002147 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002148 int idx;
2149
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002150 has_sb_ioapic = false;
2151 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002152
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002153 /*
2154 * If we have map overrides on the kernel command line the
2155 * messages in this function might not describe firmware bugs
2156 * anymore - so be careful
2157 */
2158 if (cmdline_maps)
2159 fw_bug = "";
2160
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002161 for (idx = 0; idx < nr_ioapics; idx++) {
2162 int devid, id = mpc_ioapic_id(idx);
2163
2164 devid = get_ioapic_devid(id);
2165 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002166 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2167 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002168 ret = false;
2169 } else if (devid == IOAPIC_SB_DEVID) {
2170 has_sb_ioapic = true;
2171 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002172 }
2173 }
2174
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002175 if (!has_sb_ioapic) {
2176 /*
2177 * We expect the SB IOAPIC to be listed in the IVRS
2178 * table. The system timer is connected to the SB IOAPIC
2179 * and if we don't have it in the list the system will
2180 * panic at boot time. This situation usually happens
2181 * when the BIOS is buggy and provides us the wrong
2182 * device id for the IOAPIC in the system.
2183 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002184 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002185 }
2186
2187 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002188 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002189
2190 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002191}
2192
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002193static void __init free_dma_resources(void)
2194{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002195 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2196 get_order(MAX_DOMAIN_ID/8));
2197
2198 free_unity_maps();
2199}
2200
Joerg Roedelb65233a2008-07-11 17:14:21 +02002201/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002202 * This is the hardware init function for AMD IOMMU in the system.
2203 * This function is called either from amd_iommu_init or from the interrupt
2204 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002205 *
2206 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002207 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002208 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002209 * 1 pass) Discover the most comprehensive IVHD type to use.
2210 *
2211 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002212 * Upon this information the size of the data structures is
2213 * determined that needs to be allocated.
2214 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002215 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002216 * information in the ACPI table about available AMD IOMMUs
2217 * in the system. It also maps the PCI devices in the
2218 * system to specific IOMMUs
2219 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002220 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002221 * initialized we update them with information about memory
2222 * remapping requirements parsed out of the ACPI table in
2223 * this last pass.
2224 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002225 * After everything is set up the IOMMUs are enabled and the necessary
2226 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002227 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002228static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002229{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002230 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002231 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002232 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002233
Joerg Roedel643511b2012-06-12 12:09:35 +02002234 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002235 return -ENODEV;
2236
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002237 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002238 if (status == AE_NOT_FOUND)
2239 return -ENODEV;
2240 else if (ACPI_FAILURE(status)) {
2241 const char *err = acpi_format_exception(status);
2242 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2243 return -EINVAL;
2244 }
2245
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002246 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002247 * Validate checksum here so we don't need to do it when
2248 * we actually parse the table
2249 */
2250 ret = check_ivrs_checksum(ivrs_base);
2251 if (ret)
Rafael J. Wysocki99e8ccd2017-01-10 14:57:28 +01002252 goto out;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002253
2254 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2255 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2256
2257 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002258 * First parse ACPI tables to find the largest Bus/Dev/Func
2259 * we need to handle. Upon this information the shared data
2260 * structures for the IOMMUs in the system will be allocated
2261 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002262 ret = find_last_devid_acpi(ivrs_base);
2263 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002264 goto out;
2265
Joerg Roedelc5714842008-07-11 17:14:25 +02002266 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2267 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2268 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002269
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002270 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002271 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002272 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002273 get_order(dev_table_size));
2274 if (amd_iommu_dev_table == NULL)
2275 goto out;
2276
2277 /*
2278 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2279 * IOMMU see for that device
2280 */
2281 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2282 get_order(alias_table_size));
2283 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002284 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002285
2286 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002287 amd_iommu_rlookup_table = (void *)__get_free_pages(
2288 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002289 get_order(rlookup_table_size));
2290 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002291 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002292
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002293 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2294 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002295 get_order(MAX_DOMAIN_ID/8));
2296 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002297 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002298
2299 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002300 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002301 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002302 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002303 amd_iommu_alias_table[i] = i;
2304
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002305 /*
2306 * never allocate domain 0 because its used as the non-allocated and
2307 * error value placeholder
2308 */
Baoquan He5c87f622016-09-15 16:50:51 +08002309 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002310
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002311 spin_lock_init(&amd_iommu_pd_lock);
2312
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002313 /*
2314 * now the data structures are allocated and basically initialized
2315 * start the real acpi table scan
2316 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002317 ret = init_iommu_all(ivrs_base);
2318 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002319 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002320
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002321 if (amd_iommu_irq_remap)
2322 amd_iommu_irq_remap = check_ioapic_information();
2323
Joerg Roedel05152a02012-06-15 16:53:51 +02002324 if (amd_iommu_irq_remap) {
2325 /*
2326 * Interrupt remapping enabled, create kmem_cache for the
2327 * remapping tables.
2328 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002329 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002330 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2331 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2332 else
2333 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002334 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002335 remap_cache_sz,
2336 IRQ_TABLE_ALIGNMENT,
2337 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002338 if (!amd_iommu_irq_cache)
2339 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002340
2341 irq_lookup_table = (void *)__get_free_pages(
2342 GFP_KERNEL | __GFP_ZERO,
2343 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002344 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2345 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002346 if (!irq_lookup_table)
2347 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002348 }
2349
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002350 ret = init_memory_definitions(ivrs_base);
2351 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002352 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002353
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002354 /* init the device table */
2355 init_device_table();
2356
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002357out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002358 /* Don't leak any ACPI memory */
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002359 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002360 ivrs_base = NULL;
2361
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002362 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002363}
2364
Gerard Snitselaarae295142012-03-16 11:38:22 -07002365static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002366{
2367 struct amd_iommu *iommu;
2368 int ret = 0;
2369
2370 for_each_iommu(iommu) {
2371 ret = iommu_init_msi(iommu);
2372 if (ret)
2373 goto out;
2374 }
2375
2376out:
2377 return ret;
2378}
2379
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002380static bool detect_ivrs(void)
2381{
2382 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002383 acpi_status status;
2384
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002385 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002386 if (status == AE_NOT_FOUND)
2387 return false;
2388 else if (ACPI_FAILURE(status)) {
2389 const char *err = acpi_format_exception(status);
2390 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2391 return false;
2392 }
2393
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002394 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002395
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002396 /* Make sure ACS will be enabled during PCI probe */
2397 pci_request_acs();
2398
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002399 return true;
2400}
2401
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002402/****************************************************************************
2403 *
2404 * AMD IOMMU Initialization State Machine
2405 *
2406 ****************************************************************************/
2407
2408static int __init state_next(void)
2409{
2410 int ret = 0;
2411
2412 switch (init_state) {
2413 case IOMMU_START_STATE:
2414 if (!detect_ivrs()) {
2415 init_state = IOMMU_NOT_FOUND;
2416 ret = -ENODEV;
2417 } else {
2418 init_state = IOMMU_IVRS_DETECTED;
2419 }
2420 break;
2421 case IOMMU_IVRS_DETECTED:
2422 ret = early_amd_iommu_init();
2423 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2424 break;
2425 case IOMMU_ACPI_FINISHED:
2426 early_enable_iommus();
2427 register_syscore_ops(&amd_iommu_syscore_ops);
2428 x86_platform.iommu_shutdown = disable_iommus;
2429 init_state = IOMMU_ENABLED;
2430 break;
2431 case IOMMU_ENABLED:
2432 ret = amd_iommu_init_pci();
2433 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2434 enable_iommus_v2();
2435 break;
2436 case IOMMU_PCI_INIT:
2437 ret = amd_iommu_enable_interrupts();
2438 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2439 break;
2440 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002441 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002442 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2443 break;
2444 case IOMMU_DMA_OPS:
2445 init_state = IOMMU_INITIALIZED;
2446 break;
2447 case IOMMU_INITIALIZED:
2448 /* Nothing to do */
2449 break;
2450 case IOMMU_NOT_FOUND:
2451 case IOMMU_INIT_ERROR:
2452 /* Error states => do nothing */
2453 ret = -EINVAL;
2454 break;
2455 default:
2456 /* Unknown state */
2457 BUG();
2458 }
2459
2460 return ret;
2461}
2462
2463static int __init iommu_go_to_state(enum iommu_init_state state)
2464{
2465 int ret = 0;
2466
2467 while (init_state != state) {
2468 ret = state_next();
2469 if (init_state == IOMMU_NOT_FOUND ||
2470 init_state == IOMMU_INIT_ERROR)
2471 break;
2472 }
2473
2474 return ret;
2475}
2476
Joerg Roedel6b474b82012-06-26 16:46:04 +02002477#ifdef CONFIG_IRQ_REMAP
2478int __init amd_iommu_prepare(void)
2479{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002480 int ret;
2481
Jiang Liu7fa1c842015-01-07 15:31:42 +08002482 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002483
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002484 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2485 if (ret)
2486 return ret;
2487 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002488}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002489
Joerg Roedel6b474b82012-06-26 16:46:04 +02002490int __init amd_iommu_enable(void)
2491{
2492 int ret;
2493
2494 ret = iommu_go_to_state(IOMMU_ENABLED);
2495 if (ret)
2496 return ret;
2497
2498 irq_remapping_enabled = 1;
2499
2500 return 0;
2501}
2502
2503void amd_iommu_disable(void)
2504{
2505 amd_iommu_suspend();
2506}
2507
2508int amd_iommu_reenable(int mode)
2509{
2510 amd_iommu_resume();
2511
2512 return 0;
2513}
2514
2515int __init amd_iommu_enable_faulting(void)
2516{
2517 /* We enable MSI later when PCI is initialized */
2518 return 0;
2519}
2520#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002521
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002522/*
2523 * This is the core init function for AMD IOMMU hardware in the system.
2524 * This function is called from the generic x86 DMA layer initialization
2525 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002526 */
2527static int __init amd_iommu_init(void)
2528{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002529 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002530
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002531 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2532 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002533 free_dma_resources();
2534 if (!irq_remapping_enabled) {
2535 disable_iommus();
2536 free_on_init_error();
2537 } else {
2538 struct amd_iommu *iommu;
2539
2540 uninit_device_table_dma();
2541 for_each_iommu(iommu)
2542 iommu_flush_all_caches(iommu);
2543 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002544 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002545
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002546 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002547}
2548
Joerg Roedelb65233a2008-07-11 17:14:21 +02002549/****************************************************************************
2550 *
2551 * Early detect code. This code runs at IOMMU detection time in the DMA
2552 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2553 * IOMMUs
2554 *
2555 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002556int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002557{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002558 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002559
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002560 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002561 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002562
Joerg Roedela5235722010-05-11 17:12:33 +02002563 if (amd_iommu_disabled)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002564 return -ENODEV;
Joerg Roedela5235722010-05-11 17:12:33 +02002565
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002566 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2567 if (ret)
2568 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002569
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002570 amd_iommu_detected = true;
2571 iommu_detected = 1;
2572 x86_init.iommu.iommu_init = amd_iommu_init;
2573
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002574 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002575}
2576
Joerg Roedelb65233a2008-07-11 17:14:21 +02002577/****************************************************************************
2578 *
2579 * Parsing functions for the AMD IOMMU specific kernel command line
2580 * options.
2581 *
2582 ****************************************************************************/
2583
Joerg Roedelfefda112009-05-20 12:21:42 +02002584static int __init parse_amd_iommu_dump(char *str)
2585{
2586 amd_iommu_dump = true;
2587
2588 return 1;
2589}
2590
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002591static int __init parse_amd_iommu_intr(char *str)
2592{
2593 for (; *str; ++str) {
2594 if (strncmp(str, "legacy", 6) == 0) {
2595 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2596 break;
2597 }
2598 if (strncmp(str, "vapic", 5) == 0) {
2599 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2600 break;
2601 }
2602 }
2603 return 1;
2604}
2605
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002606static int __init parse_amd_iommu_options(char *str)
2607{
2608 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002609 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002610 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002611 if (strncmp(str, "off", 3) == 0)
2612 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002613 if (strncmp(str, "force_isolation", 15) == 0)
2614 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002615 }
2616
2617 return 1;
2618}
2619
Joerg Roedel440e89982013-04-09 16:35:28 +02002620static int __init parse_ivrs_ioapic(char *str)
2621{
2622 unsigned int bus, dev, fn;
2623 int ret, id, i;
2624 u16 devid;
2625
2626 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2627
2628 if (ret != 4) {
2629 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2630 return 1;
2631 }
2632
2633 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2634 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2635 str);
2636 return 1;
2637 }
2638
2639 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2640
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002641 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002642 i = early_ioapic_map_size++;
2643 early_ioapic_map[i].id = id;
2644 early_ioapic_map[i].devid = devid;
2645 early_ioapic_map[i].cmd_line = true;
2646
2647 return 1;
2648}
2649
2650static int __init parse_ivrs_hpet(char *str)
2651{
2652 unsigned int bus, dev, fn;
2653 int ret, id, i;
2654 u16 devid;
2655
2656 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2657
2658 if (ret != 4) {
2659 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2660 return 1;
2661 }
2662
2663 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2664 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2665 str);
2666 return 1;
2667 }
2668
2669 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2670
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002671 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002672 i = early_hpet_map_size++;
2673 early_hpet_map[i].id = id;
2674 early_hpet_map[i].devid = devid;
2675 early_hpet_map[i].cmd_line = true;
2676
2677 return 1;
2678}
2679
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002680static int __init parse_ivrs_acpihid(char *str)
2681{
2682 u32 bus, dev, fn;
2683 char *hid, *uid, *p;
2684 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2685 int ret, i;
2686
2687 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2688 if (ret != 4) {
2689 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2690 return 1;
2691 }
2692
2693 p = acpiid;
2694 hid = strsep(&p, ":");
2695 uid = p;
2696
2697 if (!hid || !(*hid) || !uid) {
2698 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2699 return 1;
2700 }
2701
2702 i = early_acpihid_map_size++;
2703 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2704 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2705 early_acpihid_map[i].devid =
2706 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2707 early_acpihid_map[i].cmd_line = true;
2708
2709 return 1;
2710}
2711
Joerg Roedel440e89982013-04-09 16:35:28 +02002712__setup("amd_iommu_dump", parse_amd_iommu_dump);
2713__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002714__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002715__setup("ivrs_ioapic", parse_ivrs_ioapic);
2716__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002717__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002718
2719IOMMU_INIT_FINISH(amd_iommu_detect,
2720 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002721 NULL,
2722 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002723
2724bool amd_iommu_v2_supported(void)
2725{
2726 return amd_iommu_v2_present;
2727}
2728EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002729
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002730struct amd_iommu *get_amd_iommu(unsigned int idx)
2731{
2732 unsigned int i = 0;
2733 struct amd_iommu *iommu;
2734
2735 for_each_iommu(iommu)
2736 if (i++ == idx)
2737 return iommu;
2738 return NULL;
2739}
2740EXPORT_SYMBOL(get_amd_iommu);
2741
Steven L Kinney30861dd2013-06-05 16:11:48 -05002742/****************************************************************************
2743 *
2744 * IOMMU EFR Performance Counter support functionality. This code allows
2745 * access to the IOMMU PC functionality.
2746 *
2747 ****************************************************************************/
2748
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002749u8 amd_iommu_pc_get_max_banks(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002750{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002751 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002752
Steven L Kinney30861dd2013-06-05 16:11:48 -05002753 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002754 return iommu->max_banks;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002755
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002756 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002757}
2758EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2759
2760bool amd_iommu_pc_supported(void)
2761{
2762 return amd_iommu_pc_present;
2763}
2764EXPORT_SYMBOL(amd_iommu_pc_supported);
2765
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002766u8 amd_iommu_pc_get_max_counters(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002767{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002768 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002769
Steven L Kinney30861dd2013-06-05 16:11:48 -05002770 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002771 return iommu->max_counters;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002772
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002773 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002774}
2775EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2776
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002777static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
2778 u8 fxn, u64 *value, bool is_write)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002779{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002780 u32 offset;
2781 u32 max_offset_lim;
2782
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002783 /* Make sure the IOMMU PC resource is available */
2784 if (!amd_iommu_pc_present)
2785 return -ENODEV;
2786
Steven L Kinney30861dd2013-06-05 16:11:48 -05002787 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002788 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002789 return -ENODEV;
2790
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002791 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002792
2793 /* Limit the offset to the hw defined mmio region aperture */
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002794 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
Steven L Kinney30861dd2013-06-05 16:11:48 -05002795 (iommu->max_counters << 8) | 0x28);
2796 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2797 (offset > max_offset_lim))
2798 return -EINVAL;
2799
2800 if (is_write) {
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002801 u64 val = *value & GENMASK_ULL(47, 0);
2802
2803 writel((u32)val, iommu->mmio_base + offset);
2804 writel((val >> 32), iommu->mmio_base + offset + 4);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002805 } else {
2806 *value = readl(iommu->mmio_base + offset + 4);
2807 *value <<= 32;
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002808 *value |= readl(iommu->mmio_base + offset);
2809 *value &= GENMASK_ULL(47, 0);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002810 }
2811
2812 return 0;
2813}
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002814
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002815int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002816{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002817 if (!iommu)
2818 return -EINVAL;
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002819
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002820 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002821}
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002822EXPORT_SYMBOL(amd_iommu_pc_get_reg);
2823
2824int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
2825{
2826 if (!iommu)
2827 return -EINVAL;
2828
2829 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
2830}
2831EXPORT_SYMBOL(amd_iommu_pc_set_reg);