blob: 7044510654fecfd01b4ee7894c8a3f005c4d1f21 [file] [log] [blame]
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
21#include <linux/acpi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020022#include <linux/list.h>
Baoquan He5c87f622016-09-15 16:50:51 +080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010025#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020026#include <linux/interrupt.h>
27#include <linux/msi.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020028#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010029#include <linux/export.h>
Alex Williamson066f2e92014-06-12 16:12:37 -060030#include <linux/iommu.h>
Lucas Stachebcfa282016-10-26 13:09:53 +020031#include <linux/kmemleak.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020032#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090033#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010034#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090035#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040036#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020037#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020038#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020039
40#include "amd_iommu_proto.h"
41#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020042#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020043
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020044/*
45 * definitions for the ACPI scanning code
46 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020047#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020048
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040049#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020050#define ACPI_IVMD_TYPE_ALL 0x20
51#define ACPI_IVMD_TYPE 0x21
52#define ACPI_IVMD_TYPE_RANGE 0x22
53
54#define IVHD_DEV_ALL 0x01
55#define IVHD_DEV_SELECT 0x02
56#define IVHD_DEV_SELECT_RANGE_START 0x03
57#define IVHD_DEV_RANGE_END 0x04
58#define IVHD_DEV_ALIAS 0x42
59#define IVHD_DEV_ALIAS_RANGE 0x43
60#define IVHD_DEV_EXT_SELECT 0x46
61#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020062#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040063#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020064
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040065#define UID_NOT_PRESENT 0
66#define UID_IS_INTEGER 1
67#define UID_IS_CHARACTER 2
68
Joerg Roedel6efed632012-06-14 15:52:58 +020069#define IVHD_SPECIAL_IOAPIC 1
70#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020071
Joerg Roedel6da73422009-05-04 11:44:38 +020072#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
73#define IVHD_FLAG_PASSPW_EN_MASK 0x02
74#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
75#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020076
77#define IVMD_FLAG_EXCL_RANGE 0x08
78#define IVMD_FLAG_UNITY_MAP 0x01
79
80#define ACPI_DEVFLAG_INITPASS 0x01
81#define ACPI_DEVFLAG_EXTINT 0x02
82#define ACPI_DEVFLAG_NMI 0x04
83#define ACPI_DEVFLAG_SYSMGT1 0x10
84#define ACPI_DEVFLAG_SYSMGT2 0x20
85#define ACPI_DEVFLAG_LINT0 0x40
86#define ACPI_DEVFLAG_LINT1 0x80
87#define ACPI_DEVFLAG_ATSDIS 0x10000000
88
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050089#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020090/*
91 * ACPI table definitions
92 *
93 * These data structures are laid over the table to parse the important values
94 * out of it.
95 */
96
Joerg Roedelb0119e82017-02-01 13:23:08 +010097extern const struct iommu_ops amd_iommu_ops;
98
Joerg Roedelb65233a2008-07-11 17:14:21 +020099/*
100 * structure describing one IOMMU in the ACPI table. Typically followed by one
101 * or more ivhd_entrys.
102 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200103struct ivhd_header {
104 u8 type;
105 u8 flags;
106 u16 length;
107 u16 devid;
108 u16 cap_ptr;
109 u64 mmio_phys;
110 u16 pci_seg;
111 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400112 u32 efr_attr;
113
114 /* Following only valid on IVHD type 11h and 40h */
115 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
116 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200117} __attribute__((packed));
118
Joerg Roedelb65233a2008-07-11 17:14:21 +0200119/*
120 * A device entry describing which devices a specific IOMMU translates and
121 * which requestor ids they use.
122 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200123struct ivhd_entry {
124 u8 type;
125 u16 devid;
126 u8 flags;
127 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400128 u32 hidh;
129 u64 cid;
130 u8 uidf;
131 u8 uidl;
132 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200133} __attribute__((packed));
134
Joerg Roedelb65233a2008-07-11 17:14:21 +0200135/*
136 * An AMD IOMMU memory definition structure. It defines things like exclusion
137 * ranges for devices and regions that should be unity mapped.
138 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200139struct ivmd_header {
140 u8 type;
141 u8 flags;
142 u16 length;
143 u16 devid;
144 u16 aux;
145 u64 resv;
146 u64 range_start;
147 u64 range_length;
148} __attribute__((packed));
149
Joerg Roedelfefda112009-05-20 12:21:42 +0200150bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200151bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200152
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500153int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500154
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200155static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200156static bool __initdata amd_iommu_disabled;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400157static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200158
Joerg Roedelb65233a2008-07-11 17:14:21 +0200159u16 amd_iommu_last_bdf; /* largest PCI device id we have
160 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200161LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200162 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700163bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200164
Joerg Roedel2e228472008-07-11 17:14:31 +0200165LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200166 system */
167
Joerg Roedelbb527772009-11-20 14:31:51 +0100168/* Array to assign indices to IOMMUs*/
169struct amd_iommu *amd_iommus[MAX_IOMMUS];
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600170
171/* Number of IOMMUs present in the system */
172static int amd_iommus_present;
Joerg Roedelbb527772009-11-20 14:31:51 +0100173
Joerg Roedel318afd42009-11-23 18:32:38 +0100174/* IOMMUs have a non-present cache? */
175bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200176bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100177
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600178u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100179
Joerg Roedel400a28a2011-11-28 15:11:02 +0100180bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200181static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100182
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100183bool amd_iommu_force_isolation __read_mostly;
184
Joerg Roedelb65233a2008-07-11 17:14:21 +0200185/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100186 * List of protection domains - used during resume
187 */
188LIST_HEAD(amd_iommu_pd_list);
189spinlock_t amd_iommu_pd_lock;
190
191/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200192 * Pointer to the device table which is shared by all AMD IOMMUs
193 * it is indexed by the PCI device id or the HT unit id and contains
194 * information about the domain the device belongs to as well as the
195 * page table root pointer.
196 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200197struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200198
199/*
200 * The alias table is a driver specific data structure which contains the
201 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
202 * More than one device can share the same requestor id.
203 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200204u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200205
206/*
207 * The rlookup table is used to find the IOMMU which is responsible
208 * for a specific device. It is also indexed by the PCI device id.
209 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200210struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200211
212/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200213 * This table is used to find the irq remapping table for a given device id
214 * quickly.
215 */
216struct irq_remap_table **irq_lookup_table;
217
218/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200219 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200220 * to know which ones are already in use.
221 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200222unsigned long *amd_iommu_pd_alloc_bitmap;
223
Joerg Roedelb65233a2008-07-11 17:14:21 +0200224static u32 dev_table_size; /* size of the device table */
225static u32 alias_table_size; /* size of the alias table */
226static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200227
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200228enum iommu_init_state {
229 IOMMU_START_STATE,
230 IOMMU_IVRS_DETECTED,
231 IOMMU_ACPI_FINISHED,
232 IOMMU_ENABLED,
233 IOMMU_PCI_INIT,
234 IOMMU_INTERRUPTS_EN,
235 IOMMU_DMA_OPS,
236 IOMMU_INITIALIZED,
237 IOMMU_NOT_FOUND,
238 IOMMU_INIT_ERROR,
Joerg Roedel1b1e9422017-06-16 16:09:56 +0200239 IOMMU_CMDLINE_DISABLED,
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200240};
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
Baoquan He4c232a72017-08-09 16:33:33 +0800260bool translation_pre_enabled(struct amd_iommu *iommu)
261{
262 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
263}
264
265static void clear_translation_pre_enabled(struct amd_iommu *iommu)
266{
267 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
268}
269
270static void init_translation_status(struct amd_iommu *iommu)
271{
272 u32 ctrl;
273
274 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
275 if (ctrl & (1<<CONTROL_IOMMU_EN))
276 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
277}
278
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200279static inline void update_last_devid(u16 devid)
280{
281 if (devid > amd_iommu_last_bdf)
282 amd_iommu_last_bdf = devid;
283}
284
Joerg Roedelc5714842008-07-11 17:14:25 +0200285static inline unsigned long tbl_size(int entry_size)
286{
287 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100288 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200289
290 return 1UL << shift;
291}
292
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600293int amd_iommu_get_num_iommus(void)
294{
295 return amd_iommus_present;
296}
297
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400298/* Access to l1 and l2 indexed register spaces */
299
300static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
301{
302 u32 val;
303
304 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
305 pci_read_config_dword(iommu->dev, 0xfc, &val);
306 return val;
307}
308
309static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
310{
311 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
312 pci_write_config_dword(iommu->dev, 0xfc, val);
313 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
314}
315
316static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
317{
318 u32 val;
319
320 pci_write_config_dword(iommu->dev, 0xf0, address);
321 pci_read_config_dword(iommu->dev, 0xf4, &val);
322 return val;
323}
324
325static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
326{
327 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
328 pci_write_config_dword(iommu->dev, 0xf4, val);
329}
330
Joerg Roedelb65233a2008-07-11 17:14:21 +0200331/****************************************************************************
332 *
333 * AMD IOMMU MMIO register space handling functions
334 *
335 * These functions are used to program the IOMMU device registers in
336 * MMIO space required for that driver.
337 *
338 ****************************************************************************/
339
340/*
341 * This function set the exclusion range in the IOMMU. DMA accesses to the
342 * exclusion range are passed through untranslated
343 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200344static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200345{
346 u64 start = iommu->exclusion_start & PAGE_MASK;
347 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
348 u64 entry;
349
350 if (!iommu->exclusion_start)
351 return;
352
353 entry = start | MMIO_EXCL_ENABLE_MASK;
354 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
355 &entry, sizeof(entry));
356
357 entry = limit;
358 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
359 &entry, sizeof(entry));
360}
361
Joerg Roedelb65233a2008-07-11 17:14:21 +0200362/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000363static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200364{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200365 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200366
367 BUG_ON(iommu->mmio_base == NULL);
368
369 entry = virt_to_phys(amd_iommu_dev_table);
370 entry |= (dev_table_size >> 12) - 1;
371 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
372 &entry, sizeof(entry));
373}
374
Joerg Roedelb65233a2008-07-11 17:14:21 +0200375/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200376static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200377{
378 u32 ctrl;
379
380 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
381 ctrl |= (1 << bit);
382 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
383}
384
Joerg Roedelca0207112009-10-28 18:02:26 +0100385static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200386{
387 u32 ctrl;
388
Joerg Roedel199d0d52008-09-17 16:45:59 +0200389 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200390 ctrl &= ~(1 << bit);
391 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
392}
393
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100394static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
395{
396 u32 ctrl;
397
398 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
399 ctrl &= ~CTRL_INV_TO_MASK;
400 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
401 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
402}
403
Joerg Roedelb65233a2008-07-11 17:14:21 +0200404/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200405static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200406{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200407 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200408}
409
Joerg Roedel92ac4322009-05-19 19:06:27 +0200410static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200411{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200412 /* Disable command buffer */
413 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
414
415 /* Disable event logging and event interrupts */
416 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
417 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
418
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500419 /* Disable IOMMU GA_LOG */
420 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
421 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
422
Chris Wrighta8c485b2009-06-15 15:53:45 +0200423 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200424 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200425}
426
Joerg Roedelb65233a2008-07-11 17:14:21 +0200427/*
428 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
429 * the system has one.
430 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500431static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200432{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500433 if (!request_mem_region(address, end, "amd_iommu")) {
434 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
435 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200436 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200437 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200438 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200439
Steven L Kinney30861dd2013-06-05 16:11:48 -0500440 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200441}
442
443static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
444{
445 if (iommu->mmio_base)
446 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500447 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200448}
449
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400450static inline u32 get_ivhd_header_size(struct ivhd_header *h)
451{
452 u32 size = 0;
453
454 switch (h->type) {
455 case 0x10:
456 size = 24;
457 break;
458 case 0x11:
459 case 0x40:
460 size = 40;
461 break;
462 }
463 return size;
464}
465
Joerg Roedelb65233a2008-07-11 17:14:21 +0200466/****************************************************************************
467 *
468 * The functions below belong to the first pass of AMD IOMMU ACPI table
469 * parsing. In this pass we try to find out the highest device id this
470 * code has to handle. Upon this information the size of the shared data
471 * structures is determined later.
472 *
473 ****************************************************************************/
474
475/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200476 * This function calculates the length of a given IVHD entry
477 */
478static inline int ivhd_entry_length(u8 *ivhd)
479{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400480 u32 type = ((struct ivhd_entry *)ivhd)->type;
481
482 if (type < 0x80) {
483 return 0x04 << (*ivhd >> 6);
484 } else if (type == IVHD_DEV_ACPI_HID) {
485 /* For ACPI_HID, offset 21 is uid len */
486 return *((u8 *)ivhd + 21) + 22;
487 }
488 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200489}
490
491/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200492 * After reading the highest device id from the IOMMU PCI capability header
493 * this function looks if there is a higher device id defined in the ACPI table
494 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200495static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
496{
497 u8 *p = (void *)h, *end = (void *)h;
498 struct ivhd_entry *dev;
499
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400500 u32 ivhd_size = get_ivhd_header_size(h);
501
502 if (!ivhd_size) {
503 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
504 return -EINVAL;
505 }
506
507 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200508 end += h->length;
509
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200510 while (p < end) {
511 dev = (struct ivhd_entry *)p;
512 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200513 case IVHD_DEV_ALL:
514 /* Use maximum BDF value for DEV_ALL */
515 update_last_devid(0xffff);
516 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200517 case IVHD_DEV_SELECT:
518 case IVHD_DEV_RANGE_END:
519 case IVHD_DEV_ALIAS:
520 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200521 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200522 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200523 break;
524 default:
525 break;
526 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200527 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200528 }
529
530 WARN_ON(p != end);
531
532 return 0;
533}
534
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400535static int __init check_ivrs_checksum(struct acpi_table_header *table)
536{
537 int i;
538 u8 checksum = 0, *p = (u8 *)table;
539
540 for (i = 0; i < table->length; ++i)
541 checksum += p[i];
542 if (checksum != 0) {
543 /* ACPI table corrupt */
544 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
545 return -ENODEV;
546 }
547
548 return 0;
549}
550
Joerg Roedelb65233a2008-07-11 17:14:21 +0200551/*
552 * Iterate over all IVHD entries in the ACPI table and find the highest device
553 * id which we need to handle. This is the first of three functions which parse
554 * the ACPI table. So we check the checksum here.
555 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200556static int __init find_last_devid_acpi(struct acpi_table_header *table)
557{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400558 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200559 struct ivhd_header *h;
560
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200561 p += IVRS_HEADER_LENGTH;
562
563 end += table->length;
564 while (p < end) {
565 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400566 if (h->type == amd_iommu_target_ivhd_type) {
567 int ret = find_last_devid_from_ivhd(h);
568
569 if (ret)
570 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200571 }
572 p += h->length;
573 }
574 WARN_ON(p != end);
575
576 return 0;
577}
578
Joerg Roedelb65233a2008-07-11 17:14:21 +0200579/****************************************************************************
580 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200581 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200582 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
583 * data structures, initialize the device/alias/rlookup table and also
584 * basically initialize the hardware.
585 *
586 ****************************************************************************/
587
588/*
589 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
590 * write commands to that buffer later and the IOMMU will execute them
591 * asynchronously
592 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200593static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200594{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200595 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
596 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200597
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200598 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200599}
600
601/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200602 * This function resets the command buffer if the IOMMU stopped fetching
603 * commands from it.
604 */
605void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
606{
607 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
608
609 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
610 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Tom Lendackyd334a562017-06-05 14:52:12 -0500611 iommu->cmd_buf_head = 0;
612 iommu->cmd_buf_tail = 0;
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200613
614 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
615}
616
617/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200618 * This function writes the command buffer address to the hardware and
619 * enables it.
620 */
621static void iommu_enable_command_buffer(struct amd_iommu *iommu)
622{
623 u64 entry;
624
625 BUG_ON(iommu->cmd_buf == NULL);
626
627 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200628 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200629
Joerg Roedelb36ca912008-06-26 21:27:45 +0200630 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200631 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200632
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200633 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200634}
635
Baoquan He78d313c2017-08-09 16:33:34 +0800636/*
637 * This function disables the command buffer
638 */
639static void iommu_disable_command_buffer(struct amd_iommu *iommu)
640{
641 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
642}
643
Joerg Roedelb36ca912008-06-26 21:27:45 +0200644static void __init free_command_buffer(struct amd_iommu *iommu)
645{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200646 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200647}
648
Joerg Roedel335503e2008-09-05 14:29:07 +0200649/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200650static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200651{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200652 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
653 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200654
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200655 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200656}
657
658static void iommu_enable_event_buffer(struct amd_iommu *iommu)
659{
660 u64 entry;
661
662 BUG_ON(iommu->evt_buf == NULL);
663
Joerg Roedel335503e2008-09-05 14:29:07 +0200664 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200665
Joerg Roedel335503e2008-09-05 14:29:07 +0200666 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
667 &entry, sizeof(entry));
668
Joerg Roedel090672072009-06-15 16:06:48 +0200669 /* set head and tail to zero manually */
670 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
671 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
672
Joerg Roedel58492e12009-05-04 18:41:16 +0200673 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200674}
675
Baoquan He78d313c2017-08-09 16:33:34 +0800676/*
677 * This function disables the event log buffer
678 */
679static void iommu_disable_event_buffer(struct amd_iommu *iommu)
680{
681 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
682}
683
Joerg Roedel335503e2008-09-05 14:29:07 +0200684static void __init free_event_buffer(struct amd_iommu *iommu)
685{
686 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
687}
688
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100689/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200690static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100691{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200692 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
693 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100694
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200695 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100696}
697
698static void iommu_enable_ppr_log(struct amd_iommu *iommu)
699{
700 u64 entry;
701
702 if (iommu->ppr_log == NULL)
703 return;
704
705 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
706
707 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
708 &entry, sizeof(entry));
709
710 /* set head and tail to zero manually */
711 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
712 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
713
714 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
715 iommu_feature_enable(iommu, CONTROL_PPR_EN);
716}
717
718static void __init free_ppr_log(struct amd_iommu *iommu)
719{
720 if (iommu->ppr_log == NULL)
721 return;
722
723 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
724}
725
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500726static void free_ga_log(struct amd_iommu *iommu)
727{
728#ifdef CONFIG_IRQ_REMAP
729 if (iommu->ga_log)
730 free_pages((unsigned long)iommu->ga_log,
731 get_order(GA_LOG_SIZE));
732 if (iommu->ga_log_tail)
733 free_pages((unsigned long)iommu->ga_log_tail,
734 get_order(8));
735#endif
736}
737
738static int iommu_ga_log_enable(struct amd_iommu *iommu)
739{
740#ifdef CONFIG_IRQ_REMAP
741 u32 status, i;
742
743 if (!iommu->ga_log)
744 return -EINVAL;
745
746 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
747
748 /* Check if already running */
749 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
750 return 0;
751
752 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
753 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
754
755 for (i = 0; i < LOOP_TIMEOUT; ++i) {
756 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
757 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
758 break;
759 }
760
761 if (i >= LOOP_TIMEOUT)
762 return -EINVAL;
763#endif /* CONFIG_IRQ_REMAP */
764 return 0;
765}
766
767#ifdef CONFIG_IRQ_REMAP
768static int iommu_init_ga_log(struct amd_iommu *iommu)
769{
770 u64 entry;
771
772 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
773 return 0;
774
775 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
776 get_order(GA_LOG_SIZE));
777 if (!iommu->ga_log)
778 goto err_out;
779
780 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
781 get_order(8));
782 if (!iommu->ga_log_tail)
783 goto err_out;
784
785 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
786 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
787 &entry, sizeof(entry));
788 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
789 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
790 &entry, sizeof(entry));
791 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
792 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
793
794 return 0;
795err_out:
796 free_ga_log(iommu);
797 return -EINVAL;
798}
799#endif /* CONFIG_IRQ_REMAP */
800
801static int iommu_init_ga(struct amd_iommu *iommu)
802{
803 int ret = 0;
804
805#ifdef CONFIG_IRQ_REMAP
806 /* Note: We have already checked GASup from IVRS table.
807 * Now, we need to make sure that GAMSup is set.
808 */
809 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
810 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
811 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
812
813 ret = iommu_init_ga_log(iommu);
814#endif /* CONFIG_IRQ_REMAP */
815
816 return ret;
817}
818
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100819static void iommu_enable_gt(struct amd_iommu *iommu)
820{
821 if (!iommu_feature(iommu, FEATURE_GT))
822 return;
823
824 iommu_feature_enable(iommu, CONTROL_GT_EN);
825}
826
Joerg Roedelb65233a2008-07-11 17:14:21 +0200827/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200828static void set_dev_entry_bit(u16 devid, u8 bit)
829{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100830 int i = (bit >> 6) & 0x03;
831 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200832
Joerg Roedelee6c2862011-11-09 12:06:03 +0100833 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200834}
835
Joerg Roedelc5cca142009-10-09 18:31:20 +0200836static int get_dev_entry_bit(u16 devid, u8 bit)
837{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100838 int i = (bit >> 6) & 0x03;
839 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200840
Joerg Roedelee6c2862011-11-09 12:06:03 +0100841 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200842}
843
844
845void amd_iommu_apply_erratum_63(u16 devid)
846{
847 int sysmgt;
848
849 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
850 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
851
852 if (sysmgt == 0x01)
853 set_dev_entry_bit(devid, DEV_ENTRY_IW);
854}
855
Joerg Roedel5ff47892008-07-14 20:11:18 +0200856/* Writes the specific IOMMU for a device into the rlookup table */
857static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
858{
859 amd_iommu_rlookup_table[devid] = iommu;
860}
861
Joerg Roedelb65233a2008-07-11 17:14:21 +0200862/*
863 * This function takes the device specific flags read from the ACPI
864 * table and sets up the device table entry with that information
865 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200866static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
867 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200868{
869 if (flags & ACPI_DEVFLAG_INITPASS)
870 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
871 if (flags & ACPI_DEVFLAG_EXTINT)
872 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
873 if (flags & ACPI_DEVFLAG_NMI)
874 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
875 if (flags & ACPI_DEVFLAG_SYSMGT1)
876 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
877 if (flags & ACPI_DEVFLAG_SYSMGT2)
878 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
879 if (flags & ACPI_DEVFLAG_LINT0)
880 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
881 if (flags & ACPI_DEVFLAG_LINT1)
882 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200883
Joerg Roedelc5cca142009-10-09 18:31:20 +0200884 amd_iommu_apply_erratum_63(devid);
885
Joerg Roedel5ff47892008-07-14 20:11:18 +0200886 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200887}
888
Joerg Roedelc50e3242014-09-09 15:59:37 +0200889static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200890{
891 struct devid_map *entry;
892 struct list_head *list;
893
Joerg Roedel31cff672013-04-09 16:53:58 +0200894 if (type == IVHD_SPECIAL_IOAPIC)
895 list = &ioapic_map;
896 else if (type == IVHD_SPECIAL_HPET)
897 list = &hpet_map;
898 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200899 return -EINVAL;
900
Joerg Roedel31cff672013-04-09 16:53:58 +0200901 list_for_each_entry(entry, list, list) {
902 if (!(entry->id == id && entry->cmd_line))
903 continue;
904
905 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
906 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
907
Joerg Roedelc50e3242014-09-09 15:59:37 +0200908 *devid = entry->devid;
909
Joerg Roedel31cff672013-04-09 16:53:58 +0200910 return 0;
911 }
912
Joerg Roedel6efed632012-06-14 15:52:58 +0200913 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
914 if (!entry)
915 return -ENOMEM;
916
Joerg Roedel31cff672013-04-09 16:53:58 +0200917 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +0200918 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +0200919 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +0200920
921 list_add_tail(&entry->list, list);
922
923 return 0;
924}
925
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400926static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
927 bool cmd_line)
928{
929 struct acpihid_map_entry *entry;
930 struct list_head *list = &acpihid_map;
931
932 list_for_each_entry(entry, list, list) {
933 if (strcmp(entry->hid, hid) ||
934 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
935 !entry->cmd_line)
936 continue;
937
938 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
939 hid, uid);
940 *devid = entry->devid;
941 return 0;
942 }
943
944 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
945 if (!entry)
946 return -ENOMEM;
947
948 memcpy(entry->uid, uid, strlen(uid));
949 memcpy(entry->hid, hid, strlen(hid));
950 entry->devid = *devid;
951 entry->cmd_line = cmd_line;
952 entry->root_devid = (entry->devid & (~0x7));
953
954 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
955 entry->cmd_line ? "cmd" : "ivrs",
956 entry->hid, entry->uid, entry->root_devid);
957
958 list_add_tail(&entry->list, list);
959 return 0;
960}
961
Joerg Roedel235dacb2013-04-09 17:53:14 +0200962static int __init add_early_maps(void)
963{
964 int i, ret;
965
966 for (i = 0; i < early_ioapic_map_size; ++i) {
967 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
968 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200969 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200970 early_ioapic_map[i].cmd_line);
971 if (ret)
972 return ret;
973 }
974
975 for (i = 0; i < early_hpet_map_size; ++i) {
976 ret = add_special_device(IVHD_SPECIAL_HPET,
977 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200978 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200979 early_hpet_map[i].cmd_line);
980 if (ret)
981 return ret;
982 }
983
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400984 for (i = 0; i < early_acpihid_map_size; ++i) {
985 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
986 early_acpihid_map[i].uid,
987 &early_acpihid_map[i].devid,
988 early_acpihid_map[i].cmd_line);
989 if (ret)
990 return ret;
991 }
992
Joerg Roedel235dacb2013-04-09 17:53:14 +0200993 return 0;
994}
995
Joerg Roedelb65233a2008-07-11 17:14:21 +0200996/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200997 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +0200998 * it
999 */
Joerg Roedel3566b772008-06-26 21:27:46 +02001000static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
1001{
1002 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1003
1004 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
1005 return;
1006
1007 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +02001008 /*
1009 * We only can configure exclusion ranges per IOMMU, not
1010 * per device. But we can enable the exclusion range per
1011 * device. This is done here
1012 */
Su Friendy2c16c9f2014-05-07 13:54:52 +08001013 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +02001014 iommu->exclusion_start = m->range_start;
1015 iommu->exclusion_length = m->range_length;
1016 }
1017}
1018
Joerg Roedelb65233a2008-07-11 17:14:21 +02001019/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001020 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1021 * initializes the hardware and our data structures with it.
1022 */
Joerg Roedel6efed632012-06-14 15:52:58 +02001023static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001024 struct ivhd_header *h)
1025{
1026 u8 *p = (u8 *)h;
1027 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +02001028 u16 devid = 0, devid_start = 0, devid_to = 0;
1029 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001030 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001031 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001032 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +02001033 int ret;
1034
1035
1036 ret = add_early_maps();
1037 if (ret)
1038 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001039
1040 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001041 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001042 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001043 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001044
1045 /*
1046 * Done. Now parse the device entries
1047 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001048 ivhd_size = get_ivhd_header_size(h);
1049 if (!ivhd_size) {
1050 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1051 return -EINVAL;
1052 }
1053
1054 p += ivhd_size;
1055
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001056 end += h->length;
1057
Joerg Roedel42a698f2009-05-20 15:41:28 +02001058
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001059 while (p < end) {
1060 e = (struct ivhd_entry *)p;
1061 switch (e->type) {
1062 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001063
Joerg Roedel226e8892015-10-20 17:33:44 +02001064 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001065
Joerg Roedel226e8892015-10-20 17:33:44 +02001066 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1067 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001068 break;
1069 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001070
1071 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1072 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001073 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001074 PCI_SLOT(e->devid),
1075 PCI_FUNC(e->devid),
1076 e->flags);
1077
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001078 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001079 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001080 break;
1081 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001082
1083 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1084 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001085 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001086 PCI_SLOT(e->devid),
1087 PCI_FUNC(e->devid),
1088 e->flags);
1089
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001090 devid_start = e->devid;
1091 flags = e->flags;
1092 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001093 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001094 break;
1095 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001096
1097 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1098 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001099 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001100 PCI_SLOT(e->devid),
1101 PCI_FUNC(e->devid),
1102 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001103 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001104 PCI_SLOT(e->ext >> 8),
1105 PCI_FUNC(e->ext >> 8));
1106
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001107 devid = e->devid;
1108 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001109 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001110 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001111 amd_iommu_alias_table[devid] = devid_to;
1112 break;
1113 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001114
1115 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1116 "devid: %02x:%02x.%x flags: %02x "
1117 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001118 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001119 PCI_SLOT(e->devid),
1120 PCI_FUNC(e->devid),
1121 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001122 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001123 PCI_SLOT(e->ext >> 8),
1124 PCI_FUNC(e->ext >> 8));
1125
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001126 devid_start = e->devid;
1127 flags = e->flags;
1128 devid_to = e->ext >> 8;
1129 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001130 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001131 break;
1132 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001133
1134 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1135 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001136 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001137 PCI_SLOT(e->devid),
1138 PCI_FUNC(e->devid),
1139 e->flags, e->ext);
1140
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001141 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001142 set_dev_entry_from_acpi(iommu, devid, e->flags,
1143 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001144 break;
1145 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001146
1147 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1148 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001149 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001150 PCI_SLOT(e->devid),
1151 PCI_FUNC(e->devid),
1152 e->flags, e->ext);
1153
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001154 devid_start = e->devid;
1155 flags = e->flags;
1156 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001157 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001158 break;
1159 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001160
1161 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001162 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001163 PCI_SLOT(e->devid),
1164 PCI_FUNC(e->devid));
1165
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001166 devid = e->devid;
1167 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001168 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001169 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001170 set_dev_entry_from_acpi(iommu,
1171 devid_to, flags, ext_flags);
1172 }
1173 set_dev_entry_from_acpi(iommu, dev_i,
1174 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001175 }
1176 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001177 case IVHD_DEV_SPECIAL: {
1178 u8 handle, type;
1179 const char *var;
1180 u16 devid;
1181 int ret;
1182
1183 handle = e->ext & 0xff;
1184 devid = (e->ext >> 8) & 0xffff;
1185 type = (e->ext >> 24) & 0xff;
1186
1187 if (type == IVHD_SPECIAL_IOAPIC)
1188 var = "IOAPIC";
1189 else if (type == IVHD_SPECIAL_HPET)
1190 var = "HPET";
1191 else
1192 var = "UNKNOWN";
1193
1194 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1195 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001196 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001197 PCI_SLOT(devid),
1198 PCI_FUNC(devid));
1199
Joerg Roedelc50e3242014-09-09 15:59:37 +02001200 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001201 if (ret)
1202 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001203
1204 /*
1205 * add_special_device might update the devid in case a
1206 * command-line override is present. So call
1207 * set_dev_entry_from_acpi after add_special_device.
1208 */
1209 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1210
Joerg Roedel6efed632012-06-14 15:52:58 +02001211 break;
1212 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001213 case IVHD_DEV_ACPI_HID: {
1214 u16 devid;
1215 u8 hid[ACPIHID_HID_LEN] = {0};
1216 u8 uid[ACPIHID_UID_LEN] = {0};
1217 int ret;
1218
1219 if (h->type != 0x40) {
1220 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1221 e->type);
1222 break;
1223 }
1224
1225 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1226 hid[ACPIHID_HID_LEN - 1] = '\0';
1227
1228 if (!(*hid)) {
1229 pr_err(FW_BUG "Invalid HID.\n");
1230 break;
1231 }
1232
1233 switch (e->uidf) {
1234 case UID_NOT_PRESENT:
1235
1236 if (e->uidl != 0)
1237 pr_warn(FW_BUG "Invalid UID length.\n");
1238
1239 break;
1240 case UID_IS_INTEGER:
1241
1242 sprintf(uid, "%d", e->uid);
1243
1244 break;
1245 case UID_IS_CHARACTER:
1246
1247 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1248 uid[ACPIHID_UID_LEN - 1] = '\0';
1249
1250 break;
1251 default:
1252 break;
1253 }
1254
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001255 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001256 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1257 hid, uid,
1258 PCI_BUS_NUM(devid),
1259 PCI_SLOT(devid),
1260 PCI_FUNC(devid));
1261
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001262 flags = e->flags;
1263
1264 ret = add_acpi_hid_device(hid, uid, &devid, false);
1265 if (ret)
1266 return ret;
1267
1268 /*
1269 * add_special_device might update the devid in case a
1270 * command-line override is present. So call
1271 * set_dev_entry_from_acpi after add_special_device.
1272 */
1273 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1274
1275 break;
1276 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001277 default:
1278 break;
1279 }
1280
Joerg Roedelb514e552008-09-17 17:14:27 +02001281 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001282 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001283
1284 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001285}
1286
Joerg Roedele47d4022008-06-26 21:27:48 +02001287static void __init free_iommu_one(struct amd_iommu *iommu)
1288{
1289 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001290 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001291 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001292 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001293 iommu_unmap_mmio_space(iommu);
1294}
1295
1296static void __init free_iommu_all(void)
1297{
1298 struct amd_iommu *iommu, *next;
1299
Joerg Roedel3bd22172009-05-04 15:06:20 +02001300 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001301 list_del(&iommu->list);
1302 free_iommu_one(iommu);
1303 kfree(iommu);
1304 }
1305}
1306
Joerg Roedelb65233a2008-07-11 17:14:21 +02001307/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001308 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1309 * Workaround:
1310 * BIOS should disable L2B micellaneous clock gating by setting
1311 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1312 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001313static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001314{
1315 u32 value;
1316
1317 if ((boot_cpu_data.x86 != 0x15) ||
1318 (boot_cpu_data.x86_model < 0x10) ||
1319 (boot_cpu_data.x86_model > 0x1f))
1320 return;
1321
1322 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1323 pci_read_config_dword(iommu->dev, 0xf4, &value);
1324
1325 if (value & BIT(2))
1326 return;
1327
1328 /* Select NB indirect register 0x90 and enable writing */
1329 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1330
1331 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1332 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1333 dev_name(&iommu->dev->dev));
1334
1335 /* Clear the enable writing bit */
1336 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1337}
1338
1339/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001340 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1341 * Workaround:
1342 * BIOS should enable ATS write permission check by setting
1343 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1344 */
1345static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1346{
1347 u32 value;
1348
1349 if ((boot_cpu_data.x86 != 0x15) ||
1350 (boot_cpu_data.x86_model < 0x30) ||
1351 (boot_cpu_data.x86_model > 0x3f))
1352 return;
1353
1354 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1355 value = iommu_read_l2(iommu, 0x47);
1356
1357 if (value & BIT(0))
1358 return;
1359
1360 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1361 iommu_write_l2(iommu, 0x47, value | BIT(0));
1362
1363 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1364 dev_name(&iommu->dev->dev));
1365}
1366
1367/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001368 * This function clues the initialization function for one IOMMU
1369 * together and also allocates the command buffer and programs the
1370 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1371 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001372static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1373{
Joerg Roedel6efed632012-06-14 15:52:58 +02001374 int ret;
1375
Joerg Roedele47d4022008-06-26 21:27:48 +02001376 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001377
1378 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001379 list_add_tail(&iommu->list, &amd_iommu_list);
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001380 iommu->index = amd_iommus_present++;
Joerg Roedelbb527772009-11-20 14:31:51 +01001381
1382 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1383 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1384 return -ENOSYS;
1385 }
1386
1387 /* Index is fine - add IOMMU to the array */
1388 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001389
1390 /*
1391 * Copy data from ACPI table entry to the iommu struct
1392 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001393 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001394 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001395 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001396 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001397
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001398 switch (h->type) {
1399 case 0x10:
1400 /* Check if IVHD EFR contains proper max banks/counters */
1401 if ((h->efr_attr != 0) &&
1402 ((h->efr_attr & (0xF << 13)) != 0) &&
1403 ((h->efr_attr & (0x3F << 17)) != 0))
1404 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1405 else
1406 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001407 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1408 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001409 break;
1410 case 0x11:
1411 case 0x40:
1412 if (h->efr_reg & (1 << 9))
1413 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1414 else
1415 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001416 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1417 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001418 break;
1419 default:
1420 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001421 }
1422
1423 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1424 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001425 if (!iommu->mmio_base)
1426 return -ENOMEM;
1427
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001428 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001429 return -ENOMEM;
1430
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001431 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001432 return -ENOMEM;
1433
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001434 iommu->int_enabled = false;
1435
Baoquan He4c232a72017-08-09 16:33:33 +08001436 init_translation_status(iommu);
1437
1438 if (translation_pre_enabled(iommu))
1439 pr_warn("Translation is already enabled - trying to copy translation structures\n");
1440
Joerg Roedel6efed632012-06-14 15:52:58 +02001441 ret = init_iommu_from_acpi(iommu, h);
1442 if (ret)
1443 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001444
Jiang Liu7c71d302015-04-13 14:11:33 +08001445 ret = amd_iommu_create_irq_domain(iommu);
1446 if (ret)
1447 return ret;
1448
Joerg Roedelf6fec002012-06-21 16:51:25 +02001449 /*
1450 * Make sure IOMMU is not considered to translate itself. The IVRS
1451 * table tells us so, but this is a lie!
1452 */
1453 amd_iommu_rlookup_table[iommu->devid] = NULL;
1454
Joerg Roedel23c742d2012-06-12 11:47:34 +02001455 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001456}
1457
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001458/**
1459 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1460 * @ivrs Pointer to the IVRS header
1461 *
1462 * This function search through all IVDB of the maximum supported IVHD
1463 */
1464static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1465{
1466 u8 *base = (u8 *)ivrs;
1467 struct ivhd_header *ivhd = (struct ivhd_header *)
1468 (base + IVRS_HEADER_LENGTH);
1469 u8 last_type = ivhd->type;
1470 u16 devid = ivhd->devid;
1471
1472 while (((u8 *)ivhd - base < ivrs->length) &&
1473 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1474 u8 *p = (u8 *) ivhd;
1475
1476 if (ivhd->devid == devid)
1477 last_type = ivhd->type;
1478 ivhd = (struct ivhd_header *)(p + ivhd->length);
1479 }
1480
1481 return last_type;
1482}
1483
Joerg Roedelb65233a2008-07-11 17:14:21 +02001484/*
1485 * Iterates over all IOMMU entries in the ACPI table, allocates the
1486 * IOMMU structure and initializes it with init_iommu_one()
1487 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001488static int __init init_iommu_all(struct acpi_table_header *table)
1489{
1490 u8 *p = (u8 *)table, *end = (u8 *)table;
1491 struct ivhd_header *h;
1492 struct amd_iommu *iommu;
1493 int ret;
1494
Joerg Roedele47d4022008-06-26 21:27:48 +02001495 end += table->length;
1496 p += IVRS_HEADER_LENGTH;
1497
1498 while (p < end) {
1499 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001500 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001501
Joerg Roedelae908c22009-09-01 16:52:16 +02001502 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001503 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001504 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001505 PCI_FUNC(h->devid), h->cap_ptr,
1506 h->pci_seg, h->flags, h->info);
1507 DUMP_printk(" mmio-addr: %016llx\n",
1508 h->mmio_phys);
1509
Joerg Roedele47d4022008-06-26 21:27:48 +02001510 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001511 if (iommu == NULL)
1512 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001513
Joerg Roedele47d4022008-06-26 21:27:48 +02001514 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001515 if (ret)
1516 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001517 }
1518 p += h->length;
1519
1520 }
1521 WARN_ON(p != end);
1522
1523 return 0;
1524}
1525
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001526static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1527 u8 fxn, u64 *value, bool is_write);
Steven L Kinney30861dd2013-06-05 16:11:48 -05001528
1529static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1530{
1531 u64 val = 0xabcd, val2 = 0;
1532
1533 if (!iommu_feature(iommu, FEATURE_PC))
1534 return;
1535
1536 amd_iommu_pc_present = true;
1537
1538 /* Check if the performance counters can be written to */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001539 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1540 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001541 (val != val2)) {
1542 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1543 amd_iommu_pc_present = false;
1544 return;
1545 }
1546
1547 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1548
1549 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1550 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1551 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1552}
1553
Alex Williamson066f2e92014-06-12 16:12:37 -06001554static ssize_t amd_iommu_show_cap(struct device *dev,
1555 struct device_attribute *attr,
1556 char *buf)
1557{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001558 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001559 return sprintf(buf, "%x\n", iommu->cap);
1560}
1561static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1562
1563static ssize_t amd_iommu_show_features(struct device *dev,
1564 struct device_attribute *attr,
1565 char *buf)
1566{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001567 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001568 return sprintf(buf, "%llx\n", iommu->features);
1569}
1570static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1571
1572static struct attribute *amd_iommu_attrs[] = {
1573 &dev_attr_cap.attr,
1574 &dev_attr_features.attr,
1575 NULL,
1576};
1577
1578static struct attribute_group amd_iommu_group = {
1579 .name = "amd-iommu",
1580 .attrs = amd_iommu_attrs,
1581};
1582
1583static const struct attribute_group *amd_iommu_groups[] = {
1584 &amd_iommu_group,
1585 NULL,
1586};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001587
Joerg Roedel23c742d2012-06-12 11:47:34 +02001588static int iommu_init_pci(struct amd_iommu *iommu)
1589{
1590 int cap_ptr = iommu->cap_ptr;
1591 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001592 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001593
Shuah Khanc5081cd2013-02-27 17:07:19 -07001594 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001595 iommu->devid & 0xff);
1596 if (!iommu->dev)
1597 return -ENODEV;
1598
Jiang Liucbbc00b2015-10-09 22:07:31 +08001599 /* Prevent binding other PCI device drivers to IOMMU devices */
1600 iommu->dev->match_driver = false;
1601
Joerg Roedel23c742d2012-06-12 11:47:34 +02001602 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1603 &iommu->cap);
1604 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1605 &range);
1606 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1607 &misc);
1608
Joerg Roedel23c742d2012-06-12 11:47:34 +02001609 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1610 amd_iommu_iotlb_sup = false;
1611
1612 /* read extended feature bits */
1613 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1614 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1615
1616 iommu->features = ((u64)high << 32) | low;
1617
1618 if (iommu_feature(iommu, FEATURE_GT)) {
1619 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001620 u32 max_pasid;
1621 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001622
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001623 pasmax = iommu->features & FEATURE_PASID_MASK;
1624 pasmax >>= FEATURE_PASID_SHIFT;
1625 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001626
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001627 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1628
1629 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001630
1631 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1632 glxval >>= FEATURE_GLXVAL_SHIFT;
1633
1634 if (amd_iommu_max_glx_val == -1)
1635 amd_iommu_max_glx_val = glxval;
1636 else
1637 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1638 }
1639
1640 if (iommu_feature(iommu, FEATURE_GT) &&
1641 iommu_feature(iommu, FEATURE_PPR)) {
1642 iommu->is_iommu_v2 = true;
1643 amd_iommu_v2_present = true;
1644 }
1645
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001646 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1647 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001648
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001649 ret = iommu_init_ga(iommu);
1650 if (ret)
1651 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001652
Joerg Roedel23c742d2012-06-12 11:47:34 +02001653 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1654 amd_iommu_np_cache = true;
1655
Steven L Kinney30861dd2013-06-05 16:11:48 -05001656 init_iommu_perf_ctr(iommu);
1657
Joerg Roedel23c742d2012-06-12 11:47:34 +02001658 if (is_rd890_iommu(iommu->dev)) {
1659 int i, j;
1660
1661 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1662 PCI_DEVFN(0, 0));
1663
1664 /*
1665 * Some rd890 systems may not be fully reconfigured by the
1666 * BIOS, so it's necessary for us to store this information so
1667 * it can be reprogrammed on resume
1668 */
1669 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1670 &iommu->stored_addr_lo);
1671 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1672 &iommu->stored_addr_hi);
1673
1674 /* Low bit locks writes to configuration space */
1675 iommu->stored_addr_lo &= ~1;
1676
1677 for (i = 0; i < 6; i++)
1678 for (j = 0; j < 0x12; j++)
1679 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1680
1681 for (i = 0; i < 0x83; i++)
1682 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1683 }
1684
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001685 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001686 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001687
Joerg Roedel39ab9552017-02-01 16:56:46 +01001688 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1689 amd_iommu_groups, "ivhd%d", iommu->index);
Joerg Roedelb0119e82017-02-01 13:23:08 +01001690 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1691 iommu_device_register(&iommu->iommu);
Alex Williamson066f2e92014-06-12 16:12:37 -06001692
Joerg Roedel23c742d2012-06-12 11:47:34 +02001693 return pci_enable_device(iommu->dev);
1694}
1695
Joerg Roedel4d121c32012-06-14 12:21:55 +02001696static void print_iommu_info(void)
1697{
1698 static const char * const feat_str[] = {
1699 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1700 "IA", "GA", "HE", "PC"
1701 };
1702 struct amd_iommu *iommu;
1703
1704 for_each_iommu(iommu) {
1705 int i;
1706
1707 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1708 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1709
1710 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001711 pr_info("AMD-Vi: Extended features (%#llx):\n",
1712 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001713 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001714 if (iommu_feature(iommu, (1ULL << i)))
1715 pr_cont(" %s", feat_str[i]);
1716 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001717
1718 if (iommu->features & FEATURE_GAM_VAPIC)
1719 pr_cont(" GA_vAPIC");
1720
Steven L Kinney30861dd2013-06-05 16:11:48 -05001721 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001722 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001723 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001724 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001725 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001726 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1727 pr_info("AMD-Vi: virtual APIC enabled\n");
1728 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001729}
1730
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001731static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001732{
1733 struct amd_iommu *iommu;
1734 int ret = 0;
1735
1736 for_each_iommu(iommu) {
1737 ret = iommu_init_pci(iommu);
1738 if (ret)
1739 break;
1740 }
1741
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001742 /*
1743 * Order is important here to make sure any unity map requirements are
1744 * fulfilled. The unity mappings are created and written to the device
1745 * table during the amd_iommu_init_api() call.
1746 *
1747 * After that we call init_device_table_dma() to make sure any
1748 * uninitialized DTE will block DMA, and in the end we flush the caches
1749 * of all IOMMUs to make sure the changes to the device table are
1750 * active.
1751 */
1752 ret = amd_iommu_init_api();
1753
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001754 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001755
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001756 for_each_iommu(iommu)
1757 iommu_flush_all_caches(iommu);
1758
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001759 if (!ret)
1760 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001761
Joerg Roedel23c742d2012-06-12 11:47:34 +02001762 return ret;
1763}
1764
Joerg Roedelb65233a2008-07-11 17:14:21 +02001765/****************************************************************************
1766 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001767 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001768 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001769 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1770 * pci_dev.
1771 *
1772 ****************************************************************************/
1773
Joerg Roedel9f800de2009-11-23 12:45:25 +01001774static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001775{
1776 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001777
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001778 r = pci_enable_msi(iommu->dev);
1779 if (r)
1780 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001781
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001782 r = request_threaded_irq(iommu->dev->irq,
1783 amd_iommu_int_handler,
1784 amd_iommu_int_thread,
1785 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001786 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001787
1788 if (r) {
1789 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001790 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001791 }
1792
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001793 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001794
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001795 return 0;
1796}
1797
Joerg Roedel05f92db2009-05-12 09:52:46 +02001798static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001799{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001800 int ret;
1801
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001802 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001803 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001804
Yijing Wang82fcfc62013-08-08 21:12:36 +08001805 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001806 ret = iommu_setup_msi(iommu);
1807 else
1808 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001809
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001810 if (ret)
1811 return ret;
1812
1813enable_faults:
1814 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1815
1816 if (iommu->ppr_log != NULL)
1817 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1818
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001819 iommu_ga_log_enable(iommu);
1820
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001821 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001822}
1823
1824/****************************************************************************
1825 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001826 * The next functions belong to the third pass of parsing the ACPI
1827 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001828 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001829 *
1830 ****************************************************************************/
1831
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001832static void __init free_unity_maps(void)
1833{
1834 struct unity_map_entry *entry, *next;
1835
1836 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1837 list_del(&entry->list);
1838 kfree(entry);
1839 }
1840}
1841
Joerg Roedelb65233a2008-07-11 17:14:21 +02001842/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001843static int __init init_exclusion_range(struct ivmd_header *m)
1844{
1845 int i;
1846
1847 switch (m->type) {
1848 case ACPI_IVMD_TYPE:
1849 set_device_exclusion_range(m->devid, m);
1850 break;
1851 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001852 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001853 set_device_exclusion_range(i, m);
1854 break;
1855 case ACPI_IVMD_TYPE_RANGE:
1856 for (i = m->devid; i <= m->aux; ++i)
1857 set_device_exclusion_range(i, m);
1858 break;
1859 default:
1860 break;
1861 }
1862
1863 return 0;
1864}
1865
Joerg Roedelb65233a2008-07-11 17:14:21 +02001866/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001867static int __init init_unity_map_range(struct ivmd_header *m)
1868{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001869 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001870 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001871
1872 e = kzalloc(sizeof(*e), GFP_KERNEL);
1873 if (e == NULL)
1874 return -ENOMEM;
1875
1876 switch (m->type) {
1877 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001878 kfree(e);
1879 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001880 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001881 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001882 e->devid_start = e->devid_end = m->devid;
1883 break;
1884 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001885 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001886 e->devid_start = 0;
1887 e->devid_end = amd_iommu_last_bdf;
1888 break;
1889 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001890 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001891 e->devid_start = m->devid;
1892 e->devid_end = m->aux;
1893 break;
1894 }
1895 e->address_start = PAGE_ALIGN(m->range_start);
1896 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1897 e->prot = m->flags >> 1;
1898
Joerg Roedel02acc432009-05-20 16:24:21 +02001899 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1900 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001901 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1902 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02001903 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1904 e->address_start, e->address_end, m->flags);
1905
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001906 list_add_tail(&e->list, &amd_iommu_unity_map);
1907
1908 return 0;
1909}
1910
Joerg Roedelb65233a2008-07-11 17:14:21 +02001911/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001912static int __init init_memory_definitions(struct acpi_table_header *table)
1913{
1914 u8 *p = (u8 *)table, *end = (u8 *)table;
1915 struct ivmd_header *m;
1916
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001917 end += table->length;
1918 p += IVRS_HEADER_LENGTH;
1919
1920 while (p < end) {
1921 m = (struct ivmd_header *)p;
1922 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1923 init_exclusion_range(m);
1924 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1925 init_unity_map_range(m);
1926
1927 p += m->length;
1928 }
1929
1930 return 0;
1931}
1932
Joerg Roedelb65233a2008-07-11 17:14:21 +02001933/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001934 * Init the device table to not allow DMA access for devices and
1935 * suppress all page faults
1936 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02001937static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001938{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001939 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001940
1941 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1942 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1943 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001944 }
1945}
1946
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001947static void __init uninit_device_table_dma(void)
1948{
1949 u32 devid;
1950
1951 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1952 amd_iommu_dev_table[devid].data[0] = 0ULL;
1953 amd_iommu_dev_table[devid].data[1] = 0ULL;
1954 }
1955}
1956
Joerg Roedel33f28c52012-06-15 18:03:31 +02001957static void init_device_table(void)
1958{
1959 u32 devid;
1960
1961 if (!amd_iommu_irq_remap)
1962 return;
1963
1964 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1965 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1966}
1967
Joerg Roedele9bf5192010-09-20 14:33:07 +02001968static void iommu_init_flags(struct amd_iommu *iommu)
1969{
1970 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1971 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1972 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1973
1974 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1975 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1976 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1977
1978 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1979 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1980 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1981
1982 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1983 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1984 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1985
1986 /*
1987 * make IOMMU memory accesses cache coherent
1988 */
1989 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001990
1991 /* Set IOTLB invalidation timeout to 1s */
1992 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001993}
1994
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001995static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001996{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001997 int i, j;
1998 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001999 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002000
2001 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002002 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002003 return;
2004
2005 /*
2006 * First, we need to ensure that the iommu is enabled. This is
2007 * controlled by a register in the northbridge
2008 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002009
2010 /* Select Northbridge indirect register 0x75 and enable writing */
2011 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2012 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2013
2014 /* Enable the iommu */
2015 if (!(ioc_feature_control & 0x1))
2016 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2017
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002018 /* Restore the iommu BAR */
2019 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2020 iommu->stored_addr_lo);
2021 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2022 iommu->stored_addr_hi);
2023
2024 /* Restore the l1 indirect regs for each of the 6 l1s */
2025 for (i = 0; i < 6; i++)
2026 for (j = 0; j < 0x12; j++)
2027 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2028
2029 /* Restore the l2 indirect regs */
2030 for (i = 0; i < 0x83; i++)
2031 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2032
2033 /* Lock PCI setup registers */
2034 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2035 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02002036}
2037
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002038static void iommu_enable_ga(struct amd_iommu *iommu)
2039{
2040#ifdef CONFIG_IRQ_REMAP
2041 switch (amd_iommu_guest_ir) {
2042 case AMD_IOMMU_GUEST_IR_VAPIC:
2043 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2044 /* Fall through */
2045 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2046 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002047 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002048 break;
2049 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002050 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002051 break;
2052 }
2053#endif
2054}
2055
Baoquan He78d313c2017-08-09 16:33:34 +08002056static void early_enable_iommu(struct amd_iommu *iommu)
2057{
2058 iommu_disable(iommu);
2059 iommu_init_flags(iommu);
2060 iommu_set_device_table(iommu);
2061 iommu_enable_command_buffer(iommu);
2062 iommu_enable_event_buffer(iommu);
2063 iommu_set_exclusion_range(iommu);
2064 iommu_enable_ga(iommu);
2065 iommu_enable(iommu);
2066 iommu_flush_all_caches(iommu);
2067}
2068
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002069/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002070 * This function finally enables all IOMMUs found in the system after
2071 * they have been initialized
2072 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002073static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002074{
2075 struct amd_iommu *iommu;
2076
Baoquan He78d313c2017-08-09 16:33:34 +08002077 for_each_iommu(iommu)
2078 early_enable_iommu(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002079
2080#ifdef CONFIG_IRQ_REMAP
2081 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2082 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2083#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002084}
2085
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002086static void enable_iommus_v2(void)
2087{
2088 struct amd_iommu *iommu;
2089
2090 for_each_iommu(iommu) {
2091 iommu_enable_ppr_log(iommu);
2092 iommu_enable_gt(iommu);
2093 }
2094}
2095
2096static void enable_iommus(void)
2097{
2098 early_enable_iommus();
2099
2100 enable_iommus_v2();
2101}
2102
Joerg Roedel92ac4322009-05-19 19:06:27 +02002103static void disable_iommus(void)
2104{
2105 struct amd_iommu *iommu;
2106
2107 for_each_iommu(iommu)
2108 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002109
2110#ifdef CONFIG_IRQ_REMAP
2111 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2112 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2113#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002114}
2115
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002116/*
2117 * Suspend/Resume support
2118 * disable suspend until real resume implemented
2119 */
2120
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002121static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002122{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002123 struct amd_iommu *iommu;
2124
2125 for_each_iommu(iommu)
2126 iommu_apply_resume_quirks(iommu);
2127
Joerg Roedel736501e2009-05-12 09:56:12 +02002128 /* re-load the hardware */
2129 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002130
2131 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002132}
2133
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002134static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002135{
Joerg Roedel736501e2009-05-12 09:56:12 +02002136 /* disable IOMMUs to go out of the way for BIOS */
2137 disable_iommus();
2138
2139 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002140}
2141
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002142static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002143 .suspend = amd_iommu_suspend,
2144 .resume = amd_iommu_resume,
2145};
2146
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002147static void __init free_iommu_resources(void)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002148{
Lucas Stachebcfa282016-10-26 13:09:53 +02002149 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002150 free_pages((unsigned long)irq_lookup_table,
2151 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002152 irq_lookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002153
Julia Lawalla5919892015-09-13 14:15:31 +02002154 kmem_cache_destroy(amd_iommu_irq_cache);
2155 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002156
2157 free_pages((unsigned long)amd_iommu_rlookup_table,
2158 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002159 amd_iommu_rlookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002160
2161 free_pages((unsigned long)amd_iommu_alias_table,
2162 get_order(alias_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002163 amd_iommu_alias_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002164
2165 free_pages((unsigned long)amd_iommu_dev_table,
2166 get_order(dev_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002167 amd_iommu_dev_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002168
2169 free_iommu_all();
2170
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002171#ifdef CONFIG_GART_IOMMU
2172 /*
2173 * We failed to initialize the AMD IOMMU - try fallback to GART
2174 * if possible.
2175 */
2176 gart_iommu_init();
2177
2178#endif
2179}
2180
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002181/* SB IOAPIC is always on this device in AMD systems */
2182#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2183
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002184static bool __init check_ioapic_information(void)
2185{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002186 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002187 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002188 int idx;
2189
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002190 has_sb_ioapic = false;
2191 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002192
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002193 /*
2194 * If we have map overrides on the kernel command line the
2195 * messages in this function might not describe firmware bugs
2196 * anymore - so be careful
2197 */
2198 if (cmdline_maps)
2199 fw_bug = "";
2200
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002201 for (idx = 0; idx < nr_ioapics; idx++) {
2202 int devid, id = mpc_ioapic_id(idx);
2203
2204 devid = get_ioapic_devid(id);
2205 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002206 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2207 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002208 ret = false;
2209 } else if (devid == IOAPIC_SB_DEVID) {
2210 has_sb_ioapic = true;
2211 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002212 }
2213 }
2214
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002215 if (!has_sb_ioapic) {
2216 /*
2217 * We expect the SB IOAPIC to be listed in the IVRS
2218 * table. The system timer is connected to the SB IOAPIC
2219 * and if we don't have it in the list the system will
2220 * panic at boot time. This situation usually happens
2221 * when the BIOS is buggy and provides us the wrong
2222 * device id for the IOAPIC in the system.
2223 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002224 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002225 }
2226
2227 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002228 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002229
2230 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002231}
2232
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002233static void __init free_dma_resources(void)
2234{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002235 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2236 get_order(MAX_DOMAIN_ID/8));
Joerg Roedelf6019272017-06-16 16:09:58 +02002237 amd_iommu_pd_alloc_bitmap = NULL;
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002238
2239 free_unity_maps();
2240}
2241
Joerg Roedelb65233a2008-07-11 17:14:21 +02002242/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002243 * This is the hardware init function for AMD IOMMU in the system.
2244 * This function is called either from amd_iommu_init or from the interrupt
2245 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002246 *
2247 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002248 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002249 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002250 * 1 pass) Discover the most comprehensive IVHD type to use.
2251 *
2252 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002253 * Upon this information the size of the data structures is
2254 * determined that needs to be allocated.
2255 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002256 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002257 * information in the ACPI table about available AMD IOMMUs
2258 * in the system. It also maps the PCI devices in the
2259 * system to specific IOMMUs
2260 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002261 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002262 * initialized we update them with information about memory
2263 * remapping requirements parsed out of the ACPI table in
2264 * this last pass.
2265 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002266 * After everything is set up the IOMMUs are enabled and the necessary
2267 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002268 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002269static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002270{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002271 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002272 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002273 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002274
Joerg Roedel643511b2012-06-12 12:09:35 +02002275 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002276 return -ENODEV;
2277
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002278 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002279 if (status == AE_NOT_FOUND)
2280 return -ENODEV;
2281 else if (ACPI_FAILURE(status)) {
2282 const char *err = acpi_format_exception(status);
2283 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2284 return -EINVAL;
2285 }
2286
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002287 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002288 * Validate checksum here so we don't need to do it when
2289 * we actually parse the table
2290 */
2291 ret = check_ivrs_checksum(ivrs_base);
2292 if (ret)
Rafael J. Wysocki99e8ccd2017-01-10 14:57:28 +01002293 goto out;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002294
2295 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2296 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2297
2298 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002299 * First parse ACPI tables to find the largest Bus/Dev/Func
2300 * we need to handle. Upon this information the shared data
2301 * structures for the IOMMUs in the system will be allocated
2302 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002303 ret = find_last_devid_acpi(ivrs_base);
2304 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002305 goto out;
2306
Joerg Roedelc5714842008-07-11 17:14:25 +02002307 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2308 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2309 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002310
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002311 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002312 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002313 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002314 get_order(dev_table_size));
2315 if (amd_iommu_dev_table == NULL)
2316 goto out;
2317
2318 /*
2319 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2320 * IOMMU see for that device
2321 */
2322 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2323 get_order(alias_table_size));
2324 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002325 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002326
2327 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002328 amd_iommu_rlookup_table = (void *)__get_free_pages(
2329 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002330 get_order(rlookup_table_size));
2331 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002332 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002333
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002334 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2335 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002336 get_order(MAX_DOMAIN_ID/8));
2337 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002338 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002339
2340 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002341 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002342 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002343 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002344 amd_iommu_alias_table[i] = i;
2345
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002346 /*
2347 * never allocate domain 0 because its used as the non-allocated and
2348 * error value placeholder
2349 */
Baoquan He5c87f622016-09-15 16:50:51 +08002350 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002351
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002352 spin_lock_init(&amd_iommu_pd_lock);
2353
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002354 /*
2355 * now the data structures are allocated and basically initialized
2356 * start the real acpi table scan
2357 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002358 ret = init_iommu_all(ivrs_base);
2359 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002360 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002361
Joerg Roedel11123742017-06-16 16:09:54 +02002362 /* Disable any previously enabled IOMMUs */
2363 disable_iommus();
2364
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002365 if (amd_iommu_irq_remap)
2366 amd_iommu_irq_remap = check_ioapic_information();
2367
Joerg Roedel05152a02012-06-15 16:53:51 +02002368 if (amd_iommu_irq_remap) {
2369 /*
2370 * Interrupt remapping enabled, create kmem_cache for the
2371 * remapping tables.
2372 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002373 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002374 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2375 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2376 else
2377 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002378 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002379 remap_cache_sz,
2380 IRQ_TABLE_ALIGNMENT,
2381 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002382 if (!amd_iommu_irq_cache)
2383 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002384
2385 irq_lookup_table = (void *)__get_free_pages(
2386 GFP_KERNEL | __GFP_ZERO,
2387 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002388 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2389 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002390 if (!irq_lookup_table)
2391 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002392 }
2393
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002394 ret = init_memory_definitions(ivrs_base);
2395 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002396 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002397
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002398 /* init the device table */
2399 init_device_table();
2400
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002401out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002402 /* Don't leak any ACPI memory */
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002403 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002404 ivrs_base = NULL;
2405
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002406 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002407}
2408
Gerard Snitselaarae295142012-03-16 11:38:22 -07002409static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002410{
2411 struct amd_iommu *iommu;
2412 int ret = 0;
2413
2414 for_each_iommu(iommu) {
2415 ret = iommu_init_msi(iommu);
2416 if (ret)
2417 goto out;
2418 }
2419
2420out:
2421 return ret;
2422}
2423
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002424static bool detect_ivrs(void)
2425{
2426 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002427 acpi_status status;
2428
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002429 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002430 if (status == AE_NOT_FOUND)
2431 return false;
2432 else if (ACPI_FAILURE(status)) {
2433 const char *err = acpi_format_exception(status);
2434 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2435 return false;
2436 }
2437
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002438 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002439
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002440 /* Make sure ACS will be enabled during PCI probe */
2441 pci_request_acs();
2442
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002443 return true;
2444}
2445
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002446/****************************************************************************
2447 *
2448 * AMD IOMMU Initialization State Machine
2449 *
2450 ****************************************************************************/
2451
2452static int __init state_next(void)
2453{
2454 int ret = 0;
2455
2456 switch (init_state) {
2457 case IOMMU_START_STATE:
2458 if (!detect_ivrs()) {
2459 init_state = IOMMU_NOT_FOUND;
2460 ret = -ENODEV;
2461 } else {
2462 init_state = IOMMU_IVRS_DETECTED;
2463 }
2464 break;
2465 case IOMMU_IVRS_DETECTED:
2466 ret = early_amd_iommu_init();
2467 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
Joerg Roedel7ad820e2017-06-16 16:09:59 +02002468 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2469 pr_info("AMD-Vi: AMD IOMMU disabled on kernel command-line\n");
2470 free_dma_resources();
2471 free_iommu_resources();
2472 init_state = IOMMU_CMDLINE_DISABLED;
2473 ret = -EINVAL;
2474 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002475 break;
2476 case IOMMU_ACPI_FINISHED:
2477 early_enable_iommus();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002478 x86_platform.iommu_shutdown = disable_iommus;
2479 init_state = IOMMU_ENABLED;
2480 break;
2481 case IOMMU_ENABLED:
Joerg Roedel74ddda72017-07-26 14:17:55 +02002482 register_syscore_ops(&amd_iommu_syscore_ops);
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002483 ret = amd_iommu_init_pci();
2484 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2485 enable_iommus_v2();
2486 break;
2487 case IOMMU_PCI_INIT:
2488 ret = amd_iommu_enable_interrupts();
2489 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2490 break;
2491 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002492 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002493 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2494 break;
2495 case IOMMU_DMA_OPS:
2496 init_state = IOMMU_INITIALIZED;
2497 break;
2498 case IOMMU_INITIALIZED:
2499 /* Nothing to do */
2500 break;
2501 case IOMMU_NOT_FOUND:
2502 case IOMMU_INIT_ERROR:
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002503 case IOMMU_CMDLINE_DISABLED:
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002504 /* Error states => do nothing */
2505 ret = -EINVAL;
2506 break;
2507 default:
2508 /* Unknown state */
2509 BUG();
2510 }
2511
2512 return ret;
2513}
2514
2515static int __init iommu_go_to_state(enum iommu_init_state state)
2516{
Joerg Roedel151b0902017-06-16 16:09:57 +02002517 int ret = -EINVAL;
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002518
2519 while (init_state != state) {
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002520 if (init_state == IOMMU_NOT_FOUND ||
2521 init_state == IOMMU_INIT_ERROR ||
2522 init_state == IOMMU_CMDLINE_DISABLED)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002523 break;
Joerg Roedel151b0902017-06-16 16:09:57 +02002524 ret = state_next();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002525 }
2526
2527 return ret;
2528}
2529
Joerg Roedel6b474b82012-06-26 16:46:04 +02002530#ifdef CONFIG_IRQ_REMAP
2531int __init amd_iommu_prepare(void)
2532{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002533 int ret;
2534
Jiang Liu7fa1c842015-01-07 15:31:42 +08002535 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002536
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002537 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2538 if (ret)
2539 return ret;
2540 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002541}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002542
Joerg Roedel6b474b82012-06-26 16:46:04 +02002543int __init amd_iommu_enable(void)
2544{
2545 int ret;
2546
2547 ret = iommu_go_to_state(IOMMU_ENABLED);
2548 if (ret)
2549 return ret;
2550
2551 irq_remapping_enabled = 1;
2552
2553 return 0;
2554}
2555
2556void amd_iommu_disable(void)
2557{
2558 amd_iommu_suspend();
2559}
2560
2561int amd_iommu_reenable(int mode)
2562{
2563 amd_iommu_resume();
2564
2565 return 0;
2566}
2567
2568int __init amd_iommu_enable_faulting(void)
2569{
2570 /* We enable MSI later when PCI is initialized */
2571 return 0;
2572}
2573#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002574
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002575/*
2576 * This is the core init function for AMD IOMMU hardware in the system.
2577 * This function is called from the generic x86 DMA layer initialization
2578 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002579 */
2580static int __init amd_iommu_init(void)
2581{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002582 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002583
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002584 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2585 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002586 free_dma_resources();
2587 if (!irq_remapping_enabled) {
2588 disable_iommus();
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002589 free_iommu_resources();
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002590 } else {
2591 struct amd_iommu *iommu;
2592
2593 uninit_device_table_dma();
2594 for_each_iommu(iommu)
2595 iommu_flush_all_caches(iommu);
2596 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002597 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002598
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002599 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002600}
2601
Joerg Roedelb65233a2008-07-11 17:14:21 +02002602/****************************************************************************
2603 *
2604 * Early detect code. This code runs at IOMMU detection time in the DMA
2605 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2606 * IOMMUs
2607 *
2608 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002609int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002610{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002611 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002612
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002613 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002614 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002615
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002616 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2617 if (ret)
2618 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002619
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002620 amd_iommu_detected = true;
2621 iommu_detected = 1;
2622 x86_init.iommu.iommu_init = amd_iommu_init;
2623
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002624 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002625}
2626
Joerg Roedelb65233a2008-07-11 17:14:21 +02002627/****************************************************************************
2628 *
2629 * Parsing functions for the AMD IOMMU specific kernel command line
2630 * options.
2631 *
2632 ****************************************************************************/
2633
Joerg Roedelfefda112009-05-20 12:21:42 +02002634static int __init parse_amd_iommu_dump(char *str)
2635{
2636 amd_iommu_dump = true;
2637
2638 return 1;
2639}
2640
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002641static int __init parse_amd_iommu_intr(char *str)
2642{
2643 for (; *str; ++str) {
2644 if (strncmp(str, "legacy", 6) == 0) {
2645 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2646 break;
2647 }
2648 if (strncmp(str, "vapic", 5) == 0) {
2649 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2650 break;
2651 }
2652 }
2653 return 1;
2654}
2655
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002656static int __init parse_amd_iommu_options(char *str)
2657{
2658 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002659 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002660 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002661 if (strncmp(str, "off", 3) == 0)
2662 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002663 if (strncmp(str, "force_isolation", 15) == 0)
2664 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002665 }
2666
2667 return 1;
2668}
2669
Joerg Roedel440e89982013-04-09 16:35:28 +02002670static int __init parse_ivrs_ioapic(char *str)
2671{
2672 unsigned int bus, dev, fn;
2673 int ret, id, i;
2674 u16 devid;
2675
2676 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2677
2678 if (ret != 4) {
2679 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2680 return 1;
2681 }
2682
2683 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2684 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2685 str);
2686 return 1;
2687 }
2688
2689 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2690
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002691 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002692 i = early_ioapic_map_size++;
2693 early_ioapic_map[i].id = id;
2694 early_ioapic_map[i].devid = devid;
2695 early_ioapic_map[i].cmd_line = true;
2696
2697 return 1;
2698}
2699
2700static int __init parse_ivrs_hpet(char *str)
2701{
2702 unsigned int bus, dev, fn;
2703 int ret, id, i;
2704 u16 devid;
2705
2706 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2707
2708 if (ret != 4) {
2709 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2710 return 1;
2711 }
2712
2713 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2714 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2715 str);
2716 return 1;
2717 }
2718
2719 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2720
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002721 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002722 i = early_hpet_map_size++;
2723 early_hpet_map[i].id = id;
2724 early_hpet_map[i].devid = devid;
2725 early_hpet_map[i].cmd_line = true;
2726
2727 return 1;
2728}
2729
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002730static int __init parse_ivrs_acpihid(char *str)
2731{
2732 u32 bus, dev, fn;
2733 char *hid, *uid, *p;
2734 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2735 int ret, i;
2736
2737 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2738 if (ret != 4) {
2739 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2740 return 1;
2741 }
2742
2743 p = acpiid;
2744 hid = strsep(&p, ":");
2745 uid = p;
2746
2747 if (!hid || !(*hid) || !uid) {
2748 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2749 return 1;
2750 }
2751
2752 i = early_acpihid_map_size++;
2753 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2754 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2755 early_acpihid_map[i].devid =
2756 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2757 early_acpihid_map[i].cmd_line = true;
2758
2759 return 1;
2760}
2761
Joerg Roedel440e89982013-04-09 16:35:28 +02002762__setup("amd_iommu_dump", parse_amd_iommu_dump);
2763__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002764__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002765__setup("ivrs_ioapic", parse_ivrs_ioapic);
2766__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002767__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002768
2769IOMMU_INIT_FINISH(amd_iommu_detect,
2770 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002771 NULL,
2772 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002773
2774bool amd_iommu_v2_supported(void)
2775{
2776 return amd_iommu_v2_present;
2777}
2778EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002779
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002780struct amd_iommu *get_amd_iommu(unsigned int idx)
2781{
2782 unsigned int i = 0;
2783 struct amd_iommu *iommu;
2784
2785 for_each_iommu(iommu)
2786 if (i++ == idx)
2787 return iommu;
2788 return NULL;
2789}
2790EXPORT_SYMBOL(get_amd_iommu);
2791
Steven L Kinney30861dd2013-06-05 16:11:48 -05002792/****************************************************************************
2793 *
2794 * IOMMU EFR Performance Counter support functionality. This code allows
2795 * access to the IOMMU PC functionality.
2796 *
2797 ****************************************************************************/
2798
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002799u8 amd_iommu_pc_get_max_banks(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002800{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002801 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002802
Steven L Kinney30861dd2013-06-05 16:11:48 -05002803 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002804 return iommu->max_banks;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002805
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002806 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002807}
2808EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2809
2810bool amd_iommu_pc_supported(void)
2811{
2812 return amd_iommu_pc_present;
2813}
2814EXPORT_SYMBOL(amd_iommu_pc_supported);
2815
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002816u8 amd_iommu_pc_get_max_counters(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002817{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002818 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002819
Steven L Kinney30861dd2013-06-05 16:11:48 -05002820 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002821 return iommu->max_counters;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002822
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002823 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002824}
2825EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2826
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002827static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
2828 u8 fxn, u64 *value, bool is_write)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002829{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002830 u32 offset;
2831 u32 max_offset_lim;
2832
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002833 /* Make sure the IOMMU PC resource is available */
2834 if (!amd_iommu_pc_present)
2835 return -ENODEV;
2836
Steven L Kinney30861dd2013-06-05 16:11:48 -05002837 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002838 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002839 return -ENODEV;
2840
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002841 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002842
2843 /* Limit the offset to the hw defined mmio region aperture */
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002844 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
Steven L Kinney30861dd2013-06-05 16:11:48 -05002845 (iommu->max_counters << 8) | 0x28);
2846 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2847 (offset > max_offset_lim))
2848 return -EINVAL;
2849
2850 if (is_write) {
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002851 u64 val = *value & GENMASK_ULL(47, 0);
2852
2853 writel((u32)val, iommu->mmio_base + offset);
2854 writel((val >> 32), iommu->mmio_base + offset + 4);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002855 } else {
2856 *value = readl(iommu->mmio_base + offset + 4);
2857 *value <<= 32;
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002858 *value |= readl(iommu->mmio_base + offset);
2859 *value &= GENMASK_ULL(47, 0);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002860 }
2861
2862 return 0;
2863}
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002864
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002865int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002866{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002867 if (!iommu)
2868 return -EINVAL;
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002869
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002870 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002871}
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002872EXPORT_SYMBOL(amd_iommu_pc_get_reg);
2873
2874int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
2875{
2876 if (!iommu)
2877 return -EINVAL;
2878
2879 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
2880}
2881EXPORT_SYMBOL(amd_iommu_pc_set_reg);