blob: 3f72f44fa2df22eea26e0b9f860ad4a20d38a3d5 [file] [log] [blame]
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
21#include <linux/acpi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020022#include <linux/list.h>
Baoquan He5c87f622016-09-15 16:50:51 +080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010025#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020026#include <linux/interrupt.h>
27#include <linux/msi.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020028#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010029#include <linux/export.h>
Alex Williamson066f2e92014-06-12 16:12:37 -060030#include <linux/iommu.h>
Lucas Stachebcfa282016-10-26 13:09:53 +020031#include <linux/kmemleak.h>
Joerg Roedel54bd6352017-06-15 10:36:22 +020032#include <linux/crash_dump.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020033#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090034#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010035#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090036#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040037#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020038#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020039#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020040
41#include "amd_iommu_proto.h"
42#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020043#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020044
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020045/*
46 * definitions for the ACPI scanning code
47 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020048#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020049
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040050#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020051#define ACPI_IVMD_TYPE_ALL 0x20
52#define ACPI_IVMD_TYPE 0x21
53#define ACPI_IVMD_TYPE_RANGE 0x22
54
55#define IVHD_DEV_ALL 0x01
56#define IVHD_DEV_SELECT 0x02
57#define IVHD_DEV_SELECT_RANGE_START 0x03
58#define IVHD_DEV_RANGE_END 0x04
59#define IVHD_DEV_ALIAS 0x42
60#define IVHD_DEV_ALIAS_RANGE 0x43
61#define IVHD_DEV_EXT_SELECT 0x46
62#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020063#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040064#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020065
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040066#define UID_NOT_PRESENT 0
67#define UID_IS_INTEGER 1
68#define UID_IS_CHARACTER 2
69
Joerg Roedel6efed632012-06-14 15:52:58 +020070#define IVHD_SPECIAL_IOAPIC 1
71#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020072
Joerg Roedel6da73422009-05-04 11:44:38 +020073#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
74#define IVHD_FLAG_PASSPW_EN_MASK 0x02
75#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
76#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020077
78#define IVMD_FLAG_EXCL_RANGE 0x08
79#define IVMD_FLAG_UNITY_MAP 0x01
80
81#define ACPI_DEVFLAG_INITPASS 0x01
82#define ACPI_DEVFLAG_EXTINT 0x02
83#define ACPI_DEVFLAG_NMI 0x04
84#define ACPI_DEVFLAG_SYSMGT1 0x10
85#define ACPI_DEVFLAG_SYSMGT2 0x20
86#define ACPI_DEVFLAG_LINT0 0x40
87#define ACPI_DEVFLAG_LINT1 0x80
88#define ACPI_DEVFLAG_ATSDIS 0x10000000
89
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050090#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020091/*
92 * ACPI table definitions
93 *
94 * These data structures are laid over the table to parse the important values
95 * out of it.
96 */
97
Joerg Roedelb0119e82017-02-01 13:23:08 +010098extern const struct iommu_ops amd_iommu_ops;
99
Joerg Roedelb65233a2008-07-11 17:14:21 +0200100/*
101 * structure describing one IOMMU in the ACPI table. Typically followed by one
102 * or more ivhd_entrys.
103 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200104struct ivhd_header {
105 u8 type;
106 u8 flags;
107 u16 length;
108 u16 devid;
109 u16 cap_ptr;
110 u64 mmio_phys;
111 u16 pci_seg;
112 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400113 u32 efr_attr;
114
115 /* Following only valid on IVHD type 11h and 40h */
116 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
117 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200118} __attribute__((packed));
119
Joerg Roedelb65233a2008-07-11 17:14:21 +0200120/*
121 * A device entry describing which devices a specific IOMMU translates and
122 * which requestor ids they use.
123 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200124struct ivhd_entry {
125 u8 type;
126 u16 devid;
127 u8 flags;
128 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400129 u32 hidh;
130 u64 cid;
131 u8 uidf;
132 u8 uidl;
133 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200134} __attribute__((packed));
135
Joerg Roedelb65233a2008-07-11 17:14:21 +0200136/*
137 * An AMD IOMMU memory definition structure. It defines things like exclusion
138 * ranges for devices and regions that should be unity mapped.
139 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200140struct ivmd_header {
141 u8 type;
142 u8 flags;
143 u16 length;
144 u16 devid;
145 u16 aux;
146 u64 resv;
147 u64 range_start;
148 u64 range_length;
149} __attribute__((packed));
150
Joerg Roedelfefda112009-05-20 12:21:42 +0200151bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200152bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200153
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500154int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500155
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200156static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200157static bool __initdata amd_iommu_disabled;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400158static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200159
Joerg Roedelb65233a2008-07-11 17:14:21 +0200160u16 amd_iommu_last_bdf; /* largest PCI device id we have
161 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200162LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200163 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700164bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200165
Joerg Roedel2e228472008-07-11 17:14:31 +0200166LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200167 system */
168
Joerg Roedelbb527772009-11-20 14:31:51 +0100169/* Array to assign indices to IOMMUs*/
170struct amd_iommu *amd_iommus[MAX_IOMMUS];
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600171
172/* Number of IOMMUs present in the system */
173static int amd_iommus_present;
Joerg Roedelbb527772009-11-20 14:31:51 +0100174
Joerg Roedel318afd42009-11-23 18:32:38 +0100175/* IOMMUs have a non-present cache? */
176bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200177bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100178
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600179u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100180
Joerg Roedel400a28a2011-11-28 15:11:02 +0100181bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200182static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100183
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100184bool amd_iommu_force_isolation __read_mostly;
185
Joerg Roedelb65233a2008-07-11 17:14:21 +0200186/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100187 * List of protection domains - used during resume
188 */
189LIST_HEAD(amd_iommu_pd_list);
190spinlock_t amd_iommu_pd_lock;
191
192/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200193 * Pointer to the device table which is shared by all AMD IOMMUs
194 * it is indexed by the PCI device id or the HT unit id and contains
195 * information about the domain the device belongs to as well as the
196 * page table root pointer.
197 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200198struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200199
200/*
201 * The alias table is a driver specific data structure which contains the
202 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
203 * More than one device can share the same requestor id.
204 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200205u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200206
207/*
208 * The rlookup table is used to find the IOMMU which is responsible
209 * for a specific device. It is also indexed by the PCI device id.
210 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200211struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200212
213/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200214 * This table is used to find the irq remapping table for a given device id
215 * quickly.
216 */
217struct irq_remap_table **irq_lookup_table;
218
219/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200220 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200221 * to know which ones are already in use.
222 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200223unsigned long *amd_iommu_pd_alloc_bitmap;
224
Joerg Roedelb65233a2008-07-11 17:14:21 +0200225static u32 dev_table_size; /* size of the device table */
226static u32 alias_table_size; /* size of the alias table */
227static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200228
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200229enum iommu_init_state {
230 IOMMU_START_STATE,
231 IOMMU_IVRS_DETECTED,
232 IOMMU_ACPI_FINISHED,
233 IOMMU_ENABLED,
234 IOMMU_PCI_INIT,
235 IOMMU_INTERRUPTS_EN,
236 IOMMU_DMA_OPS,
237 IOMMU_INITIALIZED,
238 IOMMU_NOT_FOUND,
239 IOMMU_INIT_ERROR,
Joerg Roedel1b1e9422017-06-16 16:09:56 +0200240 IOMMU_CMDLINE_DISABLED,
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200241};
242
Joerg Roedel235dacb2013-04-09 17:53:14 +0200243/* Early ioapic and hpet maps from kernel command line */
244#define EARLY_MAP_SIZE 4
245static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
246static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400247static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
248
Joerg Roedel235dacb2013-04-09 17:53:14 +0200249static int __initdata early_ioapic_map_size;
250static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400251static int __initdata early_acpihid_map_size;
252
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200253static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200254
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200255static enum iommu_init_state init_state = IOMMU_START_STATE;
256
Gerard Snitselaarae295142012-03-16 11:38:22 -0700257static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200258static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200259static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100260
Baoquan He4c232a72017-08-09 16:33:33 +0800261bool translation_pre_enabled(struct amd_iommu *iommu)
262{
263 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
264}
265
266static void clear_translation_pre_enabled(struct amd_iommu *iommu)
267{
268 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
269}
270
271static void init_translation_status(struct amd_iommu *iommu)
272{
273 u32 ctrl;
274
275 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
276 if (ctrl & (1<<CONTROL_IOMMU_EN))
277 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
278}
279
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200280static inline void update_last_devid(u16 devid)
281{
282 if (devid > amd_iommu_last_bdf)
283 amd_iommu_last_bdf = devid;
284}
285
Joerg Roedelc5714842008-07-11 17:14:25 +0200286static inline unsigned long tbl_size(int entry_size)
287{
288 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100289 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200290
291 return 1UL << shift;
292}
293
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600294int amd_iommu_get_num_iommus(void)
295{
296 return amd_iommus_present;
297}
298
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400299/* Access to l1 and l2 indexed register spaces */
300
301static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
302{
303 u32 val;
304
305 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
306 pci_read_config_dword(iommu->dev, 0xfc, &val);
307 return val;
308}
309
310static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
311{
312 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
313 pci_write_config_dword(iommu->dev, 0xfc, val);
314 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
315}
316
317static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
318{
319 u32 val;
320
321 pci_write_config_dword(iommu->dev, 0xf0, address);
322 pci_read_config_dword(iommu->dev, 0xf4, &val);
323 return val;
324}
325
326static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
327{
328 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
329 pci_write_config_dword(iommu->dev, 0xf4, val);
330}
331
Joerg Roedelb65233a2008-07-11 17:14:21 +0200332/****************************************************************************
333 *
334 * AMD IOMMU MMIO register space handling functions
335 *
336 * These functions are used to program the IOMMU device registers in
337 * MMIO space required for that driver.
338 *
339 ****************************************************************************/
340
341/*
342 * This function set the exclusion range in the IOMMU. DMA accesses to the
343 * exclusion range are passed through untranslated
344 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200345static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200346{
347 u64 start = iommu->exclusion_start & PAGE_MASK;
348 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
349 u64 entry;
350
351 if (!iommu->exclusion_start)
352 return;
353
354 entry = start | MMIO_EXCL_ENABLE_MASK;
355 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
356 &entry, sizeof(entry));
357
358 entry = limit;
359 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
360 &entry, sizeof(entry));
361}
362
Joerg Roedelb65233a2008-07-11 17:14:21 +0200363/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000364static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200365{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200366 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200367
368 BUG_ON(iommu->mmio_base == NULL);
369
370 entry = virt_to_phys(amd_iommu_dev_table);
371 entry |= (dev_table_size >> 12) - 1;
372 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
373 &entry, sizeof(entry));
374}
375
Joerg Roedelb65233a2008-07-11 17:14:21 +0200376/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200377static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200378{
379 u32 ctrl;
380
381 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
382 ctrl |= (1 << bit);
383 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
384}
385
Joerg Roedelca0207112009-10-28 18:02:26 +0100386static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200387{
388 u32 ctrl;
389
Joerg Roedel199d0d52008-09-17 16:45:59 +0200390 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200391 ctrl &= ~(1 << bit);
392 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
393}
394
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100395static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
396{
397 u32 ctrl;
398
399 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
400 ctrl &= ~CTRL_INV_TO_MASK;
401 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
402 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
403}
404
Joerg Roedelb65233a2008-07-11 17:14:21 +0200405/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200406static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200407{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200408 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200409}
410
Joerg Roedel92ac4322009-05-19 19:06:27 +0200411static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200412{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200413 /* Disable command buffer */
414 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
415
416 /* Disable event logging and event interrupts */
417 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
418 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
419
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500420 /* Disable IOMMU GA_LOG */
421 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
422 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
423
Chris Wrighta8c485b2009-06-15 15:53:45 +0200424 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200425 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200426}
427
Joerg Roedelb65233a2008-07-11 17:14:21 +0200428/*
429 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
430 * the system has one.
431 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500432static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200433{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500434 if (!request_mem_region(address, end, "amd_iommu")) {
435 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
436 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200437 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200438 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200439 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200440
Steven L Kinney30861dd2013-06-05 16:11:48 -0500441 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200442}
443
444static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
445{
446 if (iommu->mmio_base)
447 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500448 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200449}
450
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400451static inline u32 get_ivhd_header_size(struct ivhd_header *h)
452{
453 u32 size = 0;
454
455 switch (h->type) {
456 case 0x10:
457 size = 24;
458 break;
459 case 0x11:
460 case 0x40:
461 size = 40;
462 break;
463 }
464 return size;
465}
466
Joerg Roedelb65233a2008-07-11 17:14:21 +0200467/****************************************************************************
468 *
469 * The functions below belong to the first pass of AMD IOMMU ACPI table
470 * parsing. In this pass we try to find out the highest device id this
471 * code has to handle. Upon this information the size of the shared data
472 * structures is determined later.
473 *
474 ****************************************************************************/
475
476/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200477 * This function calculates the length of a given IVHD entry
478 */
479static inline int ivhd_entry_length(u8 *ivhd)
480{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400481 u32 type = ((struct ivhd_entry *)ivhd)->type;
482
483 if (type < 0x80) {
484 return 0x04 << (*ivhd >> 6);
485 } else if (type == IVHD_DEV_ACPI_HID) {
486 /* For ACPI_HID, offset 21 is uid len */
487 return *((u8 *)ivhd + 21) + 22;
488 }
489 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200490}
491
492/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200493 * After reading the highest device id from the IOMMU PCI capability header
494 * this function looks if there is a higher device id defined in the ACPI table
495 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200496static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
497{
498 u8 *p = (void *)h, *end = (void *)h;
499 struct ivhd_entry *dev;
500
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400501 u32 ivhd_size = get_ivhd_header_size(h);
502
503 if (!ivhd_size) {
504 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
505 return -EINVAL;
506 }
507
508 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200509 end += h->length;
510
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200511 while (p < end) {
512 dev = (struct ivhd_entry *)p;
513 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200514 case IVHD_DEV_ALL:
515 /* Use maximum BDF value for DEV_ALL */
516 update_last_devid(0xffff);
517 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200518 case IVHD_DEV_SELECT:
519 case IVHD_DEV_RANGE_END:
520 case IVHD_DEV_ALIAS:
521 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200522 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200523 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200524 break;
525 default:
526 break;
527 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200528 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200529 }
530
531 WARN_ON(p != end);
532
533 return 0;
534}
535
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400536static int __init check_ivrs_checksum(struct acpi_table_header *table)
537{
538 int i;
539 u8 checksum = 0, *p = (u8 *)table;
540
541 for (i = 0; i < table->length; ++i)
542 checksum += p[i];
543 if (checksum != 0) {
544 /* ACPI table corrupt */
545 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
546 return -ENODEV;
547 }
548
549 return 0;
550}
551
Joerg Roedelb65233a2008-07-11 17:14:21 +0200552/*
553 * Iterate over all IVHD entries in the ACPI table and find the highest device
554 * id which we need to handle. This is the first of three functions which parse
555 * the ACPI table. So we check the checksum here.
556 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200557static int __init find_last_devid_acpi(struct acpi_table_header *table)
558{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400559 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200560 struct ivhd_header *h;
561
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200562 p += IVRS_HEADER_LENGTH;
563
564 end += table->length;
565 while (p < end) {
566 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400567 if (h->type == amd_iommu_target_ivhd_type) {
568 int ret = find_last_devid_from_ivhd(h);
569
570 if (ret)
571 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200572 }
573 p += h->length;
574 }
575 WARN_ON(p != end);
576
577 return 0;
578}
579
Joerg Roedelb65233a2008-07-11 17:14:21 +0200580/****************************************************************************
581 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200582 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200583 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
584 * data structures, initialize the device/alias/rlookup table and also
585 * basically initialize the hardware.
586 *
587 ****************************************************************************/
588
589/*
590 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
591 * write commands to that buffer later and the IOMMU will execute them
592 * asynchronously
593 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200594static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200595{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200596 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
597 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200598
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200599 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200600}
601
602/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200603 * This function resets the command buffer if the IOMMU stopped fetching
604 * commands from it.
605 */
606void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
607{
608 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
609
610 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
611 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Tom Lendackyd334a562017-06-05 14:52:12 -0500612 iommu->cmd_buf_head = 0;
613 iommu->cmd_buf_tail = 0;
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200614
615 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
616}
617
618/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200619 * This function writes the command buffer address to the hardware and
620 * enables it.
621 */
622static void iommu_enable_command_buffer(struct amd_iommu *iommu)
623{
624 u64 entry;
625
626 BUG_ON(iommu->cmd_buf == NULL);
627
628 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200629 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200630
Joerg Roedelb36ca912008-06-26 21:27:45 +0200631 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200632 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200633
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200634 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200635}
636
637static void __init free_command_buffer(struct amd_iommu *iommu)
638{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200639 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200640}
641
Joerg Roedel335503e2008-09-05 14:29:07 +0200642/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200643static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200644{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200645 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
646 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200647
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200648 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200649}
650
651static void iommu_enable_event_buffer(struct amd_iommu *iommu)
652{
653 u64 entry;
654
655 BUG_ON(iommu->evt_buf == NULL);
656
Joerg Roedel335503e2008-09-05 14:29:07 +0200657 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200658
Joerg Roedel335503e2008-09-05 14:29:07 +0200659 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
660 &entry, sizeof(entry));
661
Joerg Roedel090672072009-06-15 16:06:48 +0200662 /* set head and tail to zero manually */
663 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
664 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
665
Joerg Roedel58492e12009-05-04 18:41:16 +0200666 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200667}
668
669static void __init free_event_buffer(struct amd_iommu *iommu)
670{
671 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
672}
673
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100674/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200675static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100676{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200677 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
678 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100679
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200680 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100681}
682
683static void iommu_enable_ppr_log(struct amd_iommu *iommu)
684{
685 u64 entry;
686
687 if (iommu->ppr_log == NULL)
688 return;
689
690 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
691
692 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
693 &entry, sizeof(entry));
694
695 /* set head and tail to zero manually */
696 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
697 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
698
699 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
700 iommu_feature_enable(iommu, CONTROL_PPR_EN);
701}
702
703static void __init free_ppr_log(struct amd_iommu *iommu)
704{
705 if (iommu->ppr_log == NULL)
706 return;
707
708 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
709}
710
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500711static void free_ga_log(struct amd_iommu *iommu)
712{
713#ifdef CONFIG_IRQ_REMAP
714 if (iommu->ga_log)
715 free_pages((unsigned long)iommu->ga_log,
716 get_order(GA_LOG_SIZE));
717 if (iommu->ga_log_tail)
718 free_pages((unsigned long)iommu->ga_log_tail,
719 get_order(8));
720#endif
721}
722
723static int iommu_ga_log_enable(struct amd_iommu *iommu)
724{
725#ifdef CONFIG_IRQ_REMAP
726 u32 status, i;
727
728 if (!iommu->ga_log)
729 return -EINVAL;
730
731 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
732
733 /* Check if already running */
734 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
735 return 0;
736
737 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
738 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
739
740 for (i = 0; i < LOOP_TIMEOUT; ++i) {
741 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
742 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
743 break;
744 }
745
746 if (i >= LOOP_TIMEOUT)
747 return -EINVAL;
748#endif /* CONFIG_IRQ_REMAP */
749 return 0;
750}
751
752#ifdef CONFIG_IRQ_REMAP
753static int iommu_init_ga_log(struct amd_iommu *iommu)
754{
755 u64 entry;
756
757 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
758 return 0;
759
760 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
761 get_order(GA_LOG_SIZE));
762 if (!iommu->ga_log)
763 goto err_out;
764
765 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
766 get_order(8));
767 if (!iommu->ga_log_tail)
768 goto err_out;
769
770 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
771 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
772 &entry, sizeof(entry));
773 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
774 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
775 &entry, sizeof(entry));
776 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
777 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
778
779 return 0;
780err_out:
781 free_ga_log(iommu);
782 return -EINVAL;
783}
784#endif /* CONFIG_IRQ_REMAP */
785
786static int iommu_init_ga(struct amd_iommu *iommu)
787{
788 int ret = 0;
789
790#ifdef CONFIG_IRQ_REMAP
791 /* Note: We have already checked GASup from IVRS table.
792 * Now, we need to make sure that GAMSup is set.
793 */
794 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
795 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
796 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
797
798 ret = iommu_init_ga_log(iommu);
799#endif /* CONFIG_IRQ_REMAP */
800
801 return ret;
802}
803
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100804static void iommu_enable_gt(struct amd_iommu *iommu)
805{
806 if (!iommu_feature(iommu, FEATURE_GT))
807 return;
808
809 iommu_feature_enable(iommu, CONTROL_GT_EN);
810}
811
Joerg Roedelb65233a2008-07-11 17:14:21 +0200812/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200813static void set_dev_entry_bit(u16 devid, u8 bit)
814{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100815 int i = (bit >> 6) & 0x03;
816 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200817
Joerg Roedelee6c2862011-11-09 12:06:03 +0100818 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200819}
820
Joerg Roedelc5cca142009-10-09 18:31:20 +0200821static int get_dev_entry_bit(u16 devid, u8 bit)
822{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100823 int i = (bit >> 6) & 0x03;
824 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200825
Joerg Roedelee6c2862011-11-09 12:06:03 +0100826 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200827}
828
829
830void amd_iommu_apply_erratum_63(u16 devid)
831{
832 int sysmgt;
833
834 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
835 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
836
837 if (sysmgt == 0x01)
838 set_dev_entry_bit(devid, DEV_ENTRY_IW);
839}
840
Joerg Roedel5ff47892008-07-14 20:11:18 +0200841/* Writes the specific IOMMU for a device into the rlookup table */
842static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
843{
844 amd_iommu_rlookup_table[devid] = iommu;
845}
846
Joerg Roedelb65233a2008-07-11 17:14:21 +0200847/*
848 * This function takes the device specific flags read from the ACPI
849 * table and sets up the device table entry with that information
850 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200851static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
852 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200853{
854 if (flags & ACPI_DEVFLAG_INITPASS)
855 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
856 if (flags & ACPI_DEVFLAG_EXTINT)
857 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
858 if (flags & ACPI_DEVFLAG_NMI)
859 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
860 if (flags & ACPI_DEVFLAG_SYSMGT1)
861 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
862 if (flags & ACPI_DEVFLAG_SYSMGT2)
863 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
864 if (flags & ACPI_DEVFLAG_LINT0)
865 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
866 if (flags & ACPI_DEVFLAG_LINT1)
867 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200868
Joerg Roedelc5cca142009-10-09 18:31:20 +0200869 amd_iommu_apply_erratum_63(devid);
870
Joerg Roedel5ff47892008-07-14 20:11:18 +0200871 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200872}
873
Joerg Roedelc50e3242014-09-09 15:59:37 +0200874static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200875{
876 struct devid_map *entry;
877 struct list_head *list;
878
Joerg Roedel31cff672013-04-09 16:53:58 +0200879 if (type == IVHD_SPECIAL_IOAPIC)
880 list = &ioapic_map;
881 else if (type == IVHD_SPECIAL_HPET)
882 list = &hpet_map;
883 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200884 return -EINVAL;
885
Joerg Roedel31cff672013-04-09 16:53:58 +0200886 list_for_each_entry(entry, list, list) {
887 if (!(entry->id == id && entry->cmd_line))
888 continue;
889
890 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
891 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
892
Joerg Roedelc50e3242014-09-09 15:59:37 +0200893 *devid = entry->devid;
894
Joerg Roedel31cff672013-04-09 16:53:58 +0200895 return 0;
896 }
897
Joerg Roedel6efed632012-06-14 15:52:58 +0200898 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
899 if (!entry)
900 return -ENOMEM;
901
Joerg Roedel31cff672013-04-09 16:53:58 +0200902 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +0200903 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +0200904 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +0200905
906 list_add_tail(&entry->list, list);
907
908 return 0;
909}
910
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400911static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
912 bool cmd_line)
913{
914 struct acpihid_map_entry *entry;
915 struct list_head *list = &acpihid_map;
916
917 list_for_each_entry(entry, list, list) {
918 if (strcmp(entry->hid, hid) ||
919 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
920 !entry->cmd_line)
921 continue;
922
923 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
924 hid, uid);
925 *devid = entry->devid;
926 return 0;
927 }
928
929 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
930 if (!entry)
931 return -ENOMEM;
932
933 memcpy(entry->uid, uid, strlen(uid));
934 memcpy(entry->hid, hid, strlen(hid));
935 entry->devid = *devid;
936 entry->cmd_line = cmd_line;
937 entry->root_devid = (entry->devid & (~0x7));
938
939 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
940 entry->cmd_line ? "cmd" : "ivrs",
941 entry->hid, entry->uid, entry->root_devid);
942
943 list_add_tail(&entry->list, list);
944 return 0;
945}
946
Joerg Roedel235dacb2013-04-09 17:53:14 +0200947static int __init add_early_maps(void)
948{
949 int i, ret;
950
951 for (i = 0; i < early_ioapic_map_size; ++i) {
952 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
953 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200954 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200955 early_ioapic_map[i].cmd_line);
956 if (ret)
957 return ret;
958 }
959
960 for (i = 0; i < early_hpet_map_size; ++i) {
961 ret = add_special_device(IVHD_SPECIAL_HPET,
962 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200963 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200964 early_hpet_map[i].cmd_line);
965 if (ret)
966 return ret;
967 }
968
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400969 for (i = 0; i < early_acpihid_map_size; ++i) {
970 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
971 early_acpihid_map[i].uid,
972 &early_acpihid_map[i].devid,
973 early_acpihid_map[i].cmd_line);
974 if (ret)
975 return ret;
976 }
977
Joerg Roedel235dacb2013-04-09 17:53:14 +0200978 return 0;
979}
980
Joerg Roedelb65233a2008-07-11 17:14:21 +0200981/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200982 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +0200983 * it
984 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200985static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
986{
987 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
988
989 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
990 return;
991
992 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200993 /*
994 * We only can configure exclusion ranges per IOMMU, not
995 * per device. But we can enable the exclusion range per
996 * device. This is done here
997 */
Su Friendy2c16c9f2014-05-07 13:54:52 +0800998 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +0200999 iommu->exclusion_start = m->range_start;
1000 iommu->exclusion_length = m->range_length;
1001 }
1002}
1003
Joerg Roedelb65233a2008-07-11 17:14:21 +02001004/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001005 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1006 * initializes the hardware and our data structures with it.
1007 */
Joerg Roedel6efed632012-06-14 15:52:58 +02001008static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001009 struct ivhd_header *h)
1010{
1011 u8 *p = (u8 *)h;
1012 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +02001013 u16 devid = 0, devid_start = 0, devid_to = 0;
1014 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001015 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001016 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001017 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +02001018 int ret;
1019
1020
1021 ret = add_early_maps();
1022 if (ret)
1023 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001024
1025 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001026 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001027 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001028 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001029
1030 /*
1031 * Done. Now parse the device entries
1032 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001033 ivhd_size = get_ivhd_header_size(h);
1034 if (!ivhd_size) {
1035 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1036 return -EINVAL;
1037 }
1038
1039 p += ivhd_size;
1040
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001041 end += h->length;
1042
Joerg Roedel42a698f2009-05-20 15:41:28 +02001043
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001044 while (p < end) {
1045 e = (struct ivhd_entry *)p;
1046 switch (e->type) {
1047 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001048
Joerg Roedel226e8892015-10-20 17:33:44 +02001049 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001050
Joerg Roedel226e8892015-10-20 17:33:44 +02001051 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1052 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001053 break;
1054 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001055
1056 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1057 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001058 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001059 PCI_SLOT(e->devid),
1060 PCI_FUNC(e->devid),
1061 e->flags);
1062
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001063 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001064 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001065 break;
1066 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001067
1068 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1069 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001070 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001071 PCI_SLOT(e->devid),
1072 PCI_FUNC(e->devid),
1073 e->flags);
1074
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001075 devid_start = e->devid;
1076 flags = e->flags;
1077 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001078 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001079 break;
1080 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001081
1082 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1083 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001084 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001085 PCI_SLOT(e->devid),
1086 PCI_FUNC(e->devid),
1087 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001088 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001089 PCI_SLOT(e->ext >> 8),
1090 PCI_FUNC(e->ext >> 8));
1091
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001092 devid = e->devid;
1093 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001094 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001095 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001096 amd_iommu_alias_table[devid] = devid_to;
1097 break;
1098 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001099
1100 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1101 "devid: %02x:%02x.%x flags: %02x "
1102 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001103 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001104 PCI_SLOT(e->devid),
1105 PCI_FUNC(e->devid),
1106 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001107 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001108 PCI_SLOT(e->ext >> 8),
1109 PCI_FUNC(e->ext >> 8));
1110
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001111 devid_start = e->devid;
1112 flags = e->flags;
1113 devid_to = e->ext >> 8;
1114 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001115 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001116 break;
1117 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001118
1119 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1120 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001121 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001122 PCI_SLOT(e->devid),
1123 PCI_FUNC(e->devid),
1124 e->flags, e->ext);
1125
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001126 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001127 set_dev_entry_from_acpi(iommu, devid, e->flags,
1128 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001129 break;
1130 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001131
1132 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1133 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001134 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001135 PCI_SLOT(e->devid),
1136 PCI_FUNC(e->devid),
1137 e->flags, e->ext);
1138
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001139 devid_start = e->devid;
1140 flags = e->flags;
1141 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001142 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001143 break;
1144 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001145
1146 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001147 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001148 PCI_SLOT(e->devid),
1149 PCI_FUNC(e->devid));
1150
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001151 devid = e->devid;
1152 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001153 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001154 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001155 set_dev_entry_from_acpi(iommu,
1156 devid_to, flags, ext_flags);
1157 }
1158 set_dev_entry_from_acpi(iommu, dev_i,
1159 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001160 }
1161 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001162 case IVHD_DEV_SPECIAL: {
1163 u8 handle, type;
1164 const char *var;
1165 u16 devid;
1166 int ret;
1167
1168 handle = e->ext & 0xff;
1169 devid = (e->ext >> 8) & 0xffff;
1170 type = (e->ext >> 24) & 0xff;
1171
1172 if (type == IVHD_SPECIAL_IOAPIC)
1173 var = "IOAPIC";
1174 else if (type == IVHD_SPECIAL_HPET)
1175 var = "HPET";
1176 else
1177 var = "UNKNOWN";
1178
1179 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1180 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001181 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001182 PCI_SLOT(devid),
1183 PCI_FUNC(devid));
1184
Joerg Roedelc50e3242014-09-09 15:59:37 +02001185 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001186 if (ret)
1187 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001188
1189 /*
1190 * add_special_device might update the devid in case a
1191 * command-line override is present. So call
1192 * set_dev_entry_from_acpi after add_special_device.
1193 */
1194 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1195
Joerg Roedel6efed632012-06-14 15:52:58 +02001196 break;
1197 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001198 case IVHD_DEV_ACPI_HID: {
1199 u16 devid;
1200 u8 hid[ACPIHID_HID_LEN] = {0};
1201 u8 uid[ACPIHID_UID_LEN] = {0};
1202 int ret;
1203
1204 if (h->type != 0x40) {
1205 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1206 e->type);
1207 break;
1208 }
1209
1210 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1211 hid[ACPIHID_HID_LEN - 1] = '\0';
1212
1213 if (!(*hid)) {
1214 pr_err(FW_BUG "Invalid HID.\n");
1215 break;
1216 }
1217
1218 switch (e->uidf) {
1219 case UID_NOT_PRESENT:
1220
1221 if (e->uidl != 0)
1222 pr_warn(FW_BUG "Invalid UID length.\n");
1223
1224 break;
1225 case UID_IS_INTEGER:
1226
1227 sprintf(uid, "%d", e->uid);
1228
1229 break;
1230 case UID_IS_CHARACTER:
1231
1232 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1233 uid[ACPIHID_UID_LEN - 1] = '\0';
1234
1235 break;
1236 default:
1237 break;
1238 }
1239
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001240 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001241 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1242 hid, uid,
1243 PCI_BUS_NUM(devid),
1244 PCI_SLOT(devid),
1245 PCI_FUNC(devid));
1246
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001247 flags = e->flags;
1248
1249 ret = add_acpi_hid_device(hid, uid, &devid, false);
1250 if (ret)
1251 return ret;
1252
1253 /*
1254 * add_special_device might update the devid in case a
1255 * command-line override is present. So call
1256 * set_dev_entry_from_acpi after add_special_device.
1257 */
1258 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1259
1260 break;
1261 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001262 default:
1263 break;
1264 }
1265
Joerg Roedelb514e552008-09-17 17:14:27 +02001266 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001267 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001268
1269 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001270}
1271
Joerg Roedele47d4022008-06-26 21:27:48 +02001272static void __init free_iommu_one(struct amd_iommu *iommu)
1273{
1274 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001275 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001276 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001277 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001278 iommu_unmap_mmio_space(iommu);
1279}
1280
1281static void __init free_iommu_all(void)
1282{
1283 struct amd_iommu *iommu, *next;
1284
Joerg Roedel3bd22172009-05-04 15:06:20 +02001285 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001286 list_del(&iommu->list);
1287 free_iommu_one(iommu);
1288 kfree(iommu);
1289 }
1290}
1291
Joerg Roedelb65233a2008-07-11 17:14:21 +02001292/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001293 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1294 * Workaround:
1295 * BIOS should disable L2B micellaneous clock gating by setting
1296 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1297 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001298static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001299{
1300 u32 value;
1301
1302 if ((boot_cpu_data.x86 != 0x15) ||
1303 (boot_cpu_data.x86_model < 0x10) ||
1304 (boot_cpu_data.x86_model > 0x1f))
1305 return;
1306
1307 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1308 pci_read_config_dword(iommu->dev, 0xf4, &value);
1309
1310 if (value & BIT(2))
1311 return;
1312
1313 /* Select NB indirect register 0x90 and enable writing */
1314 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1315
1316 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1317 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1318 dev_name(&iommu->dev->dev));
1319
1320 /* Clear the enable writing bit */
1321 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1322}
1323
1324/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001325 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1326 * Workaround:
1327 * BIOS should enable ATS write permission check by setting
1328 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1329 */
1330static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1331{
1332 u32 value;
1333
1334 if ((boot_cpu_data.x86 != 0x15) ||
1335 (boot_cpu_data.x86_model < 0x30) ||
1336 (boot_cpu_data.x86_model > 0x3f))
1337 return;
1338
1339 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1340 value = iommu_read_l2(iommu, 0x47);
1341
1342 if (value & BIT(0))
1343 return;
1344
1345 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1346 iommu_write_l2(iommu, 0x47, value | BIT(0));
1347
1348 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1349 dev_name(&iommu->dev->dev));
1350}
1351
1352/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001353 * This function clues the initialization function for one IOMMU
1354 * together and also allocates the command buffer and programs the
1355 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1356 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001357static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1358{
Joerg Roedel6efed632012-06-14 15:52:58 +02001359 int ret;
1360
Joerg Roedele47d4022008-06-26 21:27:48 +02001361 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001362
1363 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001364 list_add_tail(&iommu->list, &amd_iommu_list);
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001365 iommu->index = amd_iommus_present++;
Joerg Roedelbb527772009-11-20 14:31:51 +01001366
1367 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1368 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1369 return -ENOSYS;
1370 }
1371
1372 /* Index is fine - add IOMMU to the array */
1373 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001374
1375 /*
1376 * Copy data from ACPI table entry to the iommu struct
1377 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001378 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001379 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001380 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001381 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001382
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001383 switch (h->type) {
1384 case 0x10:
1385 /* Check if IVHD EFR contains proper max banks/counters */
1386 if ((h->efr_attr != 0) &&
1387 ((h->efr_attr & (0xF << 13)) != 0) &&
1388 ((h->efr_attr & (0x3F << 17)) != 0))
1389 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1390 else
1391 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001392 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1393 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001394 break;
1395 case 0x11:
1396 case 0x40:
1397 if (h->efr_reg & (1 << 9))
1398 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1399 else
1400 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001401 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1402 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001403 break;
1404 default:
1405 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001406 }
1407
1408 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1409 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001410 if (!iommu->mmio_base)
1411 return -ENOMEM;
1412
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001413 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001414 return -ENOMEM;
1415
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001416 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001417 return -ENOMEM;
1418
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001419 iommu->int_enabled = false;
1420
Baoquan He4c232a72017-08-09 16:33:33 +08001421 init_translation_status(iommu);
1422
1423 if (translation_pre_enabled(iommu))
1424 pr_warn("Translation is already enabled - trying to copy translation structures\n");
1425
Joerg Roedel6efed632012-06-14 15:52:58 +02001426 ret = init_iommu_from_acpi(iommu, h);
1427 if (ret)
1428 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001429
Jiang Liu7c71d302015-04-13 14:11:33 +08001430 ret = amd_iommu_create_irq_domain(iommu);
1431 if (ret)
1432 return ret;
1433
Joerg Roedelf6fec002012-06-21 16:51:25 +02001434 /*
1435 * Make sure IOMMU is not considered to translate itself. The IVRS
1436 * table tells us so, but this is a lie!
1437 */
1438 amd_iommu_rlookup_table[iommu->devid] = NULL;
1439
Joerg Roedel23c742d2012-06-12 11:47:34 +02001440 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001441}
1442
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001443/**
1444 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1445 * @ivrs Pointer to the IVRS header
1446 *
1447 * This function search through all IVDB of the maximum supported IVHD
1448 */
1449static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1450{
1451 u8 *base = (u8 *)ivrs;
1452 struct ivhd_header *ivhd = (struct ivhd_header *)
1453 (base + IVRS_HEADER_LENGTH);
1454 u8 last_type = ivhd->type;
1455 u16 devid = ivhd->devid;
1456
1457 while (((u8 *)ivhd - base < ivrs->length) &&
1458 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1459 u8 *p = (u8 *) ivhd;
1460
1461 if (ivhd->devid == devid)
1462 last_type = ivhd->type;
1463 ivhd = (struct ivhd_header *)(p + ivhd->length);
1464 }
1465
1466 return last_type;
1467}
1468
Joerg Roedelb65233a2008-07-11 17:14:21 +02001469/*
1470 * Iterates over all IOMMU entries in the ACPI table, allocates the
1471 * IOMMU structure and initializes it with init_iommu_one()
1472 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001473static int __init init_iommu_all(struct acpi_table_header *table)
1474{
1475 u8 *p = (u8 *)table, *end = (u8 *)table;
1476 struct ivhd_header *h;
1477 struct amd_iommu *iommu;
1478 int ret;
1479
Joerg Roedele47d4022008-06-26 21:27:48 +02001480 end += table->length;
1481 p += IVRS_HEADER_LENGTH;
1482
1483 while (p < end) {
1484 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001485 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001486
Joerg Roedelae908c22009-09-01 16:52:16 +02001487 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001488 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001489 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001490 PCI_FUNC(h->devid), h->cap_ptr,
1491 h->pci_seg, h->flags, h->info);
1492 DUMP_printk(" mmio-addr: %016llx\n",
1493 h->mmio_phys);
1494
Joerg Roedele47d4022008-06-26 21:27:48 +02001495 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001496 if (iommu == NULL)
1497 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001498
Joerg Roedele47d4022008-06-26 21:27:48 +02001499 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001500 if (ret)
1501 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001502 }
1503 p += h->length;
1504
1505 }
1506 WARN_ON(p != end);
1507
1508 return 0;
1509}
1510
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001511static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1512 u8 fxn, u64 *value, bool is_write);
Steven L Kinney30861dd2013-06-05 16:11:48 -05001513
1514static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1515{
1516 u64 val = 0xabcd, val2 = 0;
1517
1518 if (!iommu_feature(iommu, FEATURE_PC))
1519 return;
1520
1521 amd_iommu_pc_present = true;
1522
1523 /* Check if the performance counters can be written to */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001524 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1525 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001526 (val != val2)) {
1527 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1528 amd_iommu_pc_present = false;
1529 return;
1530 }
1531
1532 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1533
1534 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1535 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1536 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1537}
1538
Alex Williamson066f2e92014-06-12 16:12:37 -06001539static ssize_t amd_iommu_show_cap(struct device *dev,
1540 struct device_attribute *attr,
1541 char *buf)
1542{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001543 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001544 return sprintf(buf, "%x\n", iommu->cap);
1545}
1546static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1547
1548static ssize_t amd_iommu_show_features(struct device *dev,
1549 struct device_attribute *attr,
1550 char *buf)
1551{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001552 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001553 return sprintf(buf, "%llx\n", iommu->features);
1554}
1555static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1556
1557static struct attribute *amd_iommu_attrs[] = {
1558 &dev_attr_cap.attr,
1559 &dev_attr_features.attr,
1560 NULL,
1561};
1562
1563static struct attribute_group amd_iommu_group = {
1564 .name = "amd-iommu",
1565 .attrs = amd_iommu_attrs,
1566};
1567
1568static const struct attribute_group *amd_iommu_groups[] = {
1569 &amd_iommu_group,
1570 NULL,
1571};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001572
Joerg Roedel23c742d2012-06-12 11:47:34 +02001573static int iommu_init_pci(struct amd_iommu *iommu)
1574{
1575 int cap_ptr = iommu->cap_ptr;
1576 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001577 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001578
Shuah Khanc5081cd2013-02-27 17:07:19 -07001579 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001580 iommu->devid & 0xff);
1581 if (!iommu->dev)
1582 return -ENODEV;
1583
Jiang Liucbbc00b2015-10-09 22:07:31 +08001584 /* Prevent binding other PCI device drivers to IOMMU devices */
1585 iommu->dev->match_driver = false;
1586
Joerg Roedel23c742d2012-06-12 11:47:34 +02001587 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1588 &iommu->cap);
1589 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1590 &range);
1591 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1592 &misc);
1593
Joerg Roedel23c742d2012-06-12 11:47:34 +02001594 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1595 amd_iommu_iotlb_sup = false;
1596
1597 /* read extended feature bits */
1598 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1599 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1600
1601 iommu->features = ((u64)high << 32) | low;
1602
1603 if (iommu_feature(iommu, FEATURE_GT)) {
1604 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001605 u32 max_pasid;
1606 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001607
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001608 pasmax = iommu->features & FEATURE_PASID_MASK;
1609 pasmax >>= FEATURE_PASID_SHIFT;
1610 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001611
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001612 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1613
1614 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001615
1616 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1617 glxval >>= FEATURE_GLXVAL_SHIFT;
1618
1619 if (amd_iommu_max_glx_val == -1)
1620 amd_iommu_max_glx_val = glxval;
1621 else
1622 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1623 }
1624
1625 if (iommu_feature(iommu, FEATURE_GT) &&
1626 iommu_feature(iommu, FEATURE_PPR)) {
1627 iommu->is_iommu_v2 = true;
1628 amd_iommu_v2_present = true;
1629 }
1630
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001631 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1632 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001633
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001634 ret = iommu_init_ga(iommu);
1635 if (ret)
1636 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001637
Joerg Roedel23c742d2012-06-12 11:47:34 +02001638 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1639 amd_iommu_np_cache = true;
1640
Steven L Kinney30861dd2013-06-05 16:11:48 -05001641 init_iommu_perf_ctr(iommu);
1642
Joerg Roedel23c742d2012-06-12 11:47:34 +02001643 if (is_rd890_iommu(iommu->dev)) {
1644 int i, j;
1645
1646 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1647 PCI_DEVFN(0, 0));
1648
1649 /*
1650 * Some rd890 systems may not be fully reconfigured by the
1651 * BIOS, so it's necessary for us to store this information so
1652 * it can be reprogrammed on resume
1653 */
1654 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1655 &iommu->stored_addr_lo);
1656 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1657 &iommu->stored_addr_hi);
1658
1659 /* Low bit locks writes to configuration space */
1660 iommu->stored_addr_lo &= ~1;
1661
1662 for (i = 0; i < 6; i++)
1663 for (j = 0; j < 0x12; j++)
1664 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1665
1666 for (i = 0; i < 0x83; i++)
1667 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1668 }
1669
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001670 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001671 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001672
Joerg Roedel39ab9552017-02-01 16:56:46 +01001673 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1674 amd_iommu_groups, "ivhd%d", iommu->index);
Joerg Roedelb0119e82017-02-01 13:23:08 +01001675 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1676 iommu_device_register(&iommu->iommu);
Alex Williamson066f2e92014-06-12 16:12:37 -06001677
Joerg Roedel23c742d2012-06-12 11:47:34 +02001678 return pci_enable_device(iommu->dev);
1679}
1680
Joerg Roedel4d121c32012-06-14 12:21:55 +02001681static void print_iommu_info(void)
1682{
1683 static const char * const feat_str[] = {
1684 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1685 "IA", "GA", "HE", "PC"
1686 };
1687 struct amd_iommu *iommu;
1688
1689 for_each_iommu(iommu) {
1690 int i;
1691
1692 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1693 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1694
1695 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001696 pr_info("AMD-Vi: Extended features (%#llx):\n",
1697 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001698 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001699 if (iommu_feature(iommu, (1ULL << i)))
1700 pr_cont(" %s", feat_str[i]);
1701 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001702
1703 if (iommu->features & FEATURE_GAM_VAPIC)
1704 pr_cont(" GA_vAPIC");
1705
Steven L Kinney30861dd2013-06-05 16:11:48 -05001706 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001707 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001708 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001709 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001710 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001711 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1712 pr_info("AMD-Vi: virtual APIC enabled\n");
1713 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001714}
1715
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001716static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001717{
1718 struct amd_iommu *iommu;
1719 int ret = 0;
1720
1721 for_each_iommu(iommu) {
1722 ret = iommu_init_pci(iommu);
1723 if (ret)
1724 break;
1725 }
1726
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001727 /*
1728 * Order is important here to make sure any unity map requirements are
1729 * fulfilled. The unity mappings are created and written to the device
1730 * table during the amd_iommu_init_api() call.
1731 *
1732 * After that we call init_device_table_dma() to make sure any
1733 * uninitialized DTE will block DMA, and in the end we flush the caches
1734 * of all IOMMUs to make sure the changes to the device table are
1735 * active.
1736 */
1737 ret = amd_iommu_init_api();
1738
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001739 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001740
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001741 for_each_iommu(iommu)
1742 iommu_flush_all_caches(iommu);
1743
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001744 if (!ret)
1745 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001746
Joerg Roedel23c742d2012-06-12 11:47:34 +02001747 return ret;
1748}
1749
Joerg Roedelb65233a2008-07-11 17:14:21 +02001750/****************************************************************************
1751 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001752 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001753 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001754 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1755 * pci_dev.
1756 *
1757 ****************************************************************************/
1758
Joerg Roedel9f800de2009-11-23 12:45:25 +01001759static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001760{
1761 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001762
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001763 r = pci_enable_msi(iommu->dev);
1764 if (r)
1765 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001766
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001767 r = request_threaded_irq(iommu->dev->irq,
1768 amd_iommu_int_handler,
1769 amd_iommu_int_thread,
1770 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001771 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001772
1773 if (r) {
1774 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001775 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001776 }
1777
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001778 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001779
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001780 return 0;
1781}
1782
Joerg Roedel05f92db2009-05-12 09:52:46 +02001783static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001784{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001785 int ret;
1786
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001787 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001788 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001789
Yijing Wang82fcfc62013-08-08 21:12:36 +08001790 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001791 ret = iommu_setup_msi(iommu);
1792 else
1793 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001794
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001795 if (ret)
1796 return ret;
1797
1798enable_faults:
1799 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1800
1801 if (iommu->ppr_log != NULL)
1802 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1803
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001804 iommu_ga_log_enable(iommu);
1805
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001806 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001807}
1808
1809/****************************************************************************
1810 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001811 * The next functions belong to the third pass of parsing the ACPI
1812 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001813 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001814 *
1815 ****************************************************************************/
1816
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001817static void __init free_unity_maps(void)
1818{
1819 struct unity_map_entry *entry, *next;
1820
1821 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1822 list_del(&entry->list);
1823 kfree(entry);
1824 }
1825}
1826
Joerg Roedelb65233a2008-07-11 17:14:21 +02001827/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001828static int __init init_exclusion_range(struct ivmd_header *m)
1829{
1830 int i;
1831
1832 switch (m->type) {
1833 case ACPI_IVMD_TYPE:
1834 set_device_exclusion_range(m->devid, m);
1835 break;
1836 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001837 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001838 set_device_exclusion_range(i, m);
1839 break;
1840 case ACPI_IVMD_TYPE_RANGE:
1841 for (i = m->devid; i <= m->aux; ++i)
1842 set_device_exclusion_range(i, m);
1843 break;
1844 default:
1845 break;
1846 }
1847
1848 return 0;
1849}
1850
Joerg Roedelb65233a2008-07-11 17:14:21 +02001851/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001852static int __init init_unity_map_range(struct ivmd_header *m)
1853{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001854 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001855 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001856
1857 e = kzalloc(sizeof(*e), GFP_KERNEL);
1858 if (e == NULL)
1859 return -ENOMEM;
1860
1861 switch (m->type) {
1862 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001863 kfree(e);
1864 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001865 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001866 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001867 e->devid_start = e->devid_end = m->devid;
1868 break;
1869 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001870 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001871 e->devid_start = 0;
1872 e->devid_end = amd_iommu_last_bdf;
1873 break;
1874 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001875 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001876 e->devid_start = m->devid;
1877 e->devid_end = m->aux;
1878 break;
1879 }
1880 e->address_start = PAGE_ALIGN(m->range_start);
1881 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1882 e->prot = m->flags >> 1;
1883
Joerg Roedel02acc432009-05-20 16:24:21 +02001884 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1885 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001886 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1887 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02001888 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1889 e->address_start, e->address_end, m->flags);
1890
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001891 list_add_tail(&e->list, &amd_iommu_unity_map);
1892
1893 return 0;
1894}
1895
Joerg Roedelb65233a2008-07-11 17:14:21 +02001896/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001897static int __init init_memory_definitions(struct acpi_table_header *table)
1898{
1899 u8 *p = (u8 *)table, *end = (u8 *)table;
1900 struct ivmd_header *m;
1901
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001902 end += table->length;
1903 p += IVRS_HEADER_LENGTH;
1904
1905 while (p < end) {
1906 m = (struct ivmd_header *)p;
1907 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1908 init_exclusion_range(m);
1909 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1910 init_unity_map_range(m);
1911
1912 p += m->length;
1913 }
1914
1915 return 0;
1916}
1917
Joerg Roedelb65233a2008-07-11 17:14:21 +02001918/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001919 * Init the device table to not allow DMA access for devices and
1920 * suppress all page faults
1921 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02001922static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001923{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001924 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001925
1926 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1927 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1928 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel54bd6352017-06-15 10:36:22 +02001929 /*
1930 * In kdump kernels in-flight DMA from the old kernel might
1931 * cause IO_PAGE_FAULTs. There are no reports that a kdump
1932 * actually failed because of that, so just disable fault
1933 * reporting in the hardware to get rid of the messages
1934 */
1935 if (is_kdump_kernel())
1936 set_dev_entry_bit(devid, DEV_ENTRY_NO_PAGE_FAULT);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001937 }
1938}
1939
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001940static void __init uninit_device_table_dma(void)
1941{
1942 u32 devid;
1943
1944 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1945 amd_iommu_dev_table[devid].data[0] = 0ULL;
1946 amd_iommu_dev_table[devid].data[1] = 0ULL;
1947 }
1948}
1949
Joerg Roedel33f28c52012-06-15 18:03:31 +02001950static void init_device_table(void)
1951{
1952 u32 devid;
1953
1954 if (!amd_iommu_irq_remap)
1955 return;
1956
1957 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1958 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1959}
1960
Joerg Roedele9bf5192010-09-20 14:33:07 +02001961static void iommu_init_flags(struct amd_iommu *iommu)
1962{
1963 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1964 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1965 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1966
1967 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1968 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1969 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1970
1971 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1972 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1973 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1974
1975 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1976 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1977 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1978
1979 /*
1980 * make IOMMU memory accesses cache coherent
1981 */
1982 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001983
1984 /* Set IOTLB invalidation timeout to 1s */
1985 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001986}
1987
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001988static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001989{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001990 int i, j;
1991 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001992 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001993
1994 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001995 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001996 return;
1997
1998 /*
1999 * First, we need to ensure that the iommu is enabled. This is
2000 * controlled by a register in the northbridge
2001 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002002
2003 /* Select Northbridge indirect register 0x75 and enable writing */
2004 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2005 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2006
2007 /* Enable the iommu */
2008 if (!(ioc_feature_control & 0x1))
2009 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2010
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002011 /* Restore the iommu BAR */
2012 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2013 iommu->stored_addr_lo);
2014 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2015 iommu->stored_addr_hi);
2016
2017 /* Restore the l1 indirect regs for each of the 6 l1s */
2018 for (i = 0; i < 6; i++)
2019 for (j = 0; j < 0x12; j++)
2020 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2021
2022 /* Restore the l2 indirect regs */
2023 for (i = 0; i < 0x83; i++)
2024 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2025
2026 /* Lock PCI setup registers */
2027 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2028 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02002029}
2030
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002031static void iommu_enable_ga(struct amd_iommu *iommu)
2032{
2033#ifdef CONFIG_IRQ_REMAP
2034 switch (amd_iommu_guest_ir) {
2035 case AMD_IOMMU_GUEST_IR_VAPIC:
2036 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2037 /* Fall through */
2038 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2039 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002040 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002041 break;
2042 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002043 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002044 break;
2045 }
2046#endif
2047}
2048
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002049/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002050 * This function finally enables all IOMMUs found in the system after
2051 * they have been initialized
2052 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002053static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002054{
2055 struct amd_iommu *iommu;
2056
Joerg Roedel3bd22172009-05-04 15:06:20 +02002057 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02002058 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002059 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02002060 iommu_set_device_table(iommu);
2061 iommu_enable_command_buffer(iommu);
2062 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002063 iommu_set_exclusion_range(iommu);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002064 iommu_enable_ga(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002065 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02002066 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002067 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002068
2069#ifdef CONFIG_IRQ_REMAP
2070 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2071 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2072#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002073}
2074
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002075static void enable_iommus_v2(void)
2076{
2077 struct amd_iommu *iommu;
2078
2079 for_each_iommu(iommu) {
2080 iommu_enable_ppr_log(iommu);
2081 iommu_enable_gt(iommu);
2082 }
2083}
2084
2085static void enable_iommus(void)
2086{
2087 early_enable_iommus();
2088
2089 enable_iommus_v2();
2090}
2091
Joerg Roedel92ac4322009-05-19 19:06:27 +02002092static void disable_iommus(void)
2093{
2094 struct amd_iommu *iommu;
2095
2096 for_each_iommu(iommu)
2097 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002098
2099#ifdef CONFIG_IRQ_REMAP
2100 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2101 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2102#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002103}
2104
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002105/*
2106 * Suspend/Resume support
2107 * disable suspend until real resume implemented
2108 */
2109
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002110static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002111{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002112 struct amd_iommu *iommu;
2113
2114 for_each_iommu(iommu)
2115 iommu_apply_resume_quirks(iommu);
2116
Joerg Roedel736501e2009-05-12 09:56:12 +02002117 /* re-load the hardware */
2118 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002119
2120 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002121}
2122
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002123static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002124{
Joerg Roedel736501e2009-05-12 09:56:12 +02002125 /* disable IOMMUs to go out of the way for BIOS */
2126 disable_iommus();
2127
2128 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002129}
2130
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002131static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002132 .suspend = amd_iommu_suspend,
2133 .resume = amd_iommu_resume,
2134};
2135
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002136static void __init free_iommu_resources(void)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002137{
Lucas Stachebcfa282016-10-26 13:09:53 +02002138 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002139 free_pages((unsigned long)irq_lookup_table,
2140 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002141 irq_lookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002142
Julia Lawalla5919892015-09-13 14:15:31 +02002143 kmem_cache_destroy(amd_iommu_irq_cache);
2144 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002145
2146 free_pages((unsigned long)amd_iommu_rlookup_table,
2147 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002148 amd_iommu_rlookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002149
2150 free_pages((unsigned long)amd_iommu_alias_table,
2151 get_order(alias_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002152 amd_iommu_alias_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002153
2154 free_pages((unsigned long)amd_iommu_dev_table,
2155 get_order(dev_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002156 amd_iommu_dev_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002157
2158 free_iommu_all();
2159
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002160#ifdef CONFIG_GART_IOMMU
2161 /*
2162 * We failed to initialize the AMD IOMMU - try fallback to GART
2163 * if possible.
2164 */
2165 gart_iommu_init();
2166
2167#endif
2168}
2169
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002170/* SB IOAPIC is always on this device in AMD systems */
2171#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2172
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002173static bool __init check_ioapic_information(void)
2174{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002175 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002176 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002177 int idx;
2178
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002179 has_sb_ioapic = false;
2180 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002181
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002182 /*
2183 * If we have map overrides on the kernel command line the
2184 * messages in this function might not describe firmware bugs
2185 * anymore - so be careful
2186 */
2187 if (cmdline_maps)
2188 fw_bug = "";
2189
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002190 for (idx = 0; idx < nr_ioapics; idx++) {
2191 int devid, id = mpc_ioapic_id(idx);
2192
2193 devid = get_ioapic_devid(id);
2194 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002195 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2196 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002197 ret = false;
2198 } else if (devid == IOAPIC_SB_DEVID) {
2199 has_sb_ioapic = true;
2200 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002201 }
2202 }
2203
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002204 if (!has_sb_ioapic) {
2205 /*
2206 * We expect the SB IOAPIC to be listed in the IVRS
2207 * table. The system timer is connected to the SB IOAPIC
2208 * and if we don't have it in the list the system will
2209 * panic at boot time. This situation usually happens
2210 * when the BIOS is buggy and provides us the wrong
2211 * device id for the IOAPIC in the system.
2212 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002213 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002214 }
2215
2216 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002217 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002218
2219 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002220}
2221
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002222static void __init free_dma_resources(void)
2223{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002224 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2225 get_order(MAX_DOMAIN_ID/8));
Joerg Roedelf6019272017-06-16 16:09:58 +02002226 amd_iommu_pd_alloc_bitmap = NULL;
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002227
2228 free_unity_maps();
2229}
2230
Joerg Roedelb65233a2008-07-11 17:14:21 +02002231/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002232 * This is the hardware init function for AMD IOMMU in the system.
2233 * This function is called either from amd_iommu_init or from the interrupt
2234 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002235 *
2236 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002237 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002238 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002239 * 1 pass) Discover the most comprehensive IVHD type to use.
2240 *
2241 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002242 * Upon this information the size of the data structures is
2243 * determined that needs to be allocated.
2244 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002245 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002246 * information in the ACPI table about available AMD IOMMUs
2247 * in the system. It also maps the PCI devices in the
2248 * system to specific IOMMUs
2249 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002250 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002251 * initialized we update them with information about memory
2252 * remapping requirements parsed out of the ACPI table in
2253 * this last pass.
2254 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002255 * After everything is set up the IOMMUs are enabled and the necessary
2256 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002257 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002258static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002259{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002260 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002261 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002262 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002263
Joerg Roedel643511b2012-06-12 12:09:35 +02002264 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002265 return -ENODEV;
2266
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002267 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002268 if (status == AE_NOT_FOUND)
2269 return -ENODEV;
2270 else if (ACPI_FAILURE(status)) {
2271 const char *err = acpi_format_exception(status);
2272 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2273 return -EINVAL;
2274 }
2275
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002276 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002277 * Validate checksum here so we don't need to do it when
2278 * we actually parse the table
2279 */
2280 ret = check_ivrs_checksum(ivrs_base);
2281 if (ret)
Rafael J. Wysocki99e8ccd2017-01-10 14:57:28 +01002282 goto out;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002283
2284 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2285 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2286
2287 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002288 * First parse ACPI tables to find the largest Bus/Dev/Func
2289 * we need to handle. Upon this information the shared data
2290 * structures for the IOMMUs in the system will be allocated
2291 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002292 ret = find_last_devid_acpi(ivrs_base);
2293 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002294 goto out;
2295
Joerg Roedelc5714842008-07-11 17:14:25 +02002296 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2297 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2298 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002299
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002300 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002301 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002302 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002303 get_order(dev_table_size));
2304 if (amd_iommu_dev_table == NULL)
2305 goto out;
2306
2307 /*
2308 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2309 * IOMMU see for that device
2310 */
2311 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2312 get_order(alias_table_size));
2313 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002314 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002315
2316 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002317 amd_iommu_rlookup_table = (void *)__get_free_pages(
2318 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002319 get_order(rlookup_table_size));
2320 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002321 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002322
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002323 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2324 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002325 get_order(MAX_DOMAIN_ID/8));
2326 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002327 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002328
2329 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002330 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002331 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002332 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002333 amd_iommu_alias_table[i] = i;
2334
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002335 /*
2336 * never allocate domain 0 because its used as the non-allocated and
2337 * error value placeholder
2338 */
Baoquan He5c87f622016-09-15 16:50:51 +08002339 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002340
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002341 spin_lock_init(&amd_iommu_pd_lock);
2342
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002343 /*
2344 * now the data structures are allocated and basically initialized
2345 * start the real acpi table scan
2346 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002347 ret = init_iommu_all(ivrs_base);
2348 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002349 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002350
Joerg Roedel11123742017-06-16 16:09:54 +02002351 /* Disable any previously enabled IOMMUs */
2352 disable_iommus();
2353
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002354 if (amd_iommu_irq_remap)
2355 amd_iommu_irq_remap = check_ioapic_information();
2356
Joerg Roedel05152a02012-06-15 16:53:51 +02002357 if (amd_iommu_irq_remap) {
2358 /*
2359 * Interrupt remapping enabled, create kmem_cache for the
2360 * remapping tables.
2361 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002362 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002363 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2364 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2365 else
2366 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002367 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002368 remap_cache_sz,
2369 IRQ_TABLE_ALIGNMENT,
2370 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002371 if (!amd_iommu_irq_cache)
2372 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002373
2374 irq_lookup_table = (void *)__get_free_pages(
2375 GFP_KERNEL | __GFP_ZERO,
2376 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002377 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2378 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002379 if (!irq_lookup_table)
2380 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002381 }
2382
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002383 ret = init_memory_definitions(ivrs_base);
2384 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002385 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002386
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002387 /* init the device table */
2388 init_device_table();
2389
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002390out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002391 /* Don't leak any ACPI memory */
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002392 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002393 ivrs_base = NULL;
2394
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002395 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002396}
2397
Gerard Snitselaarae295142012-03-16 11:38:22 -07002398static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002399{
2400 struct amd_iommu *iommu;
2401 int ret = 0;
2402
2403 for_each_iommu(iommu) {
2404 ret = iommu_init_msi(iommu);
2405 if (ret)
2406 goto out;
2407 }
2408
2409out:
2410 return ret;
2411}
2412
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002413static bool detect_ivrs(void)
2414{
2415 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002416 acpi_status status;
2417
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002418 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002419 if (status == AE_NOT_FOUND)
2420 return false;
2421 else if (ACPI_FAILURE(status)) {
2422 const char *err = acpi_format_exception(status);
2423 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2424 return false;
2425 }
2426
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002427 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002428
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002429 /* Make sure ACS will be enabled during PCI probe */
2430 pci_request_acs();
2431
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002432 return true;
2433}
2434
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002435/****************************************************************************
2436 *
2437 * AMD IOMMU Initialization State Machine
2438 *
2439 ****************************************************************************/
2440
2441static int __init state_next(void)
2442{
2443 int ret = 0;
2444
2445 switch (init_state) {
2446 case IOMMU_START_STATE:
2447 if (!detect_ivrs()) {
2448 init_state = IOMMU_NOT_FOUND;
2449 ret = -ENODEV;
2450 } else {
2451 init_state = IOMMU_IVRS_DETECTED;
2452 }
2453 break;
2454 case IOMMU_IVRS_DETECTED:
2455 ret = early_amd_iommu_init();
2456 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
Joerg Roedel7ad820e2017-06-16 16:09:59 +02002457 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2458 pr_info("AMD-Vi: AMD IOMMU disabled on kernel command-line\n");
2459 free_dma_resources();
2460 free_iommu_resources();
2461 init_state = IOMMU_CMDLINE_DISABLED;
2462 ret = -EINVAL;
2463 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002464 break;
2465 case IOMMU_ACPI_FINISHED:
2466 early_enable_iommus();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002467 x86_platform.iommu_shutdown = disable_iommus;
2468 init_state = IOMMU_ENABLED;
2469 break;
2470 case IOMMU_ENABLED:
Joerg Roedel74ddda72017-07-26 14:17:55 +02002471 register_syscore_ops(&amd_iommu_syscore_ops);
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002472 ret = amd_iommu_init_pci();
2473 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2474 enable_iommus_v2();
2475 break;
2476 case IOMMU_PCI_INIT:
2477 ret = amd_iommu_enable_interrupts();
2478 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2479 break;
2480 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002481 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002482 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2483 break;
2484 case IOMMU_DMA_OPS:
2485 init_state = IOMMU_INITIALIZED;
2486 break;
2487 case IOMMU_INITIALIZED:
2488 /* Nothing to do */
2489 break;
2490 case IOMMU_NOT_FOUND:
2491 case IOMMU_INIT_ERROR:
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002492 case IOMMU_CMDLINE_DISABLED:
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002493 /* Error states => do nothing */
2494 ret = -EINVAL;
2495 break;
2496 default:
2497 /* Unknown state */
2498 BUG();
2499 }
2500
2501 return ret;
2502}
2503
2504static int __init iommu_go_to_state(enum iommu_init_state state)
2505{
Joerg Roedel151b0902017-06-16 16:09:57 +02002506 int ret = -EINVAL;
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002507
2508 while (init_state != state) {
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002509 if (init_state == IOMMU_NOT_FOUND ||
2510 init_state == IOMMU_INIT_ERROR ||
2511 init_state == IOMMU_CMDLINE_DISABLED)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002512 break;
Joerg Roedel151b0902017-06-16 16:09:57 +02002513 ret = state_next();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002514 }
2515
2516 return ret;
2517}
2518
Joerg Roedel6b474b82012-06-26 16:46:04 +02002519#ifdef CONFIG_IRQ_REMAP
2520int __init amd_iommu_prepare(void)
2521{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002522 int ret;
2523
Jiang Liu7fa1c842015-01-07 15:31:42 +08002524 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002525
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002526 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2527 if (ret)
2528 return ret;
2529 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002530}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002531
Joerg Roedel6b474b82012-06-26 16:46:04 +02002532int __init amd_iommu_enable(void)
2533{
2534 int ret;
2535
2536 ret = iommu_go_to_state(IOMMU_ENABLED);
2537 if (ret)
2538 return ret;
2539
2540 irq_remapping_enabled = 1;
2541
2542 return 0;
2543}
2544
2545void amd_iommu_disable(void)
2546{
2547 amd_iommu_suspend();
2548}
2549
2550int amd_iommu_reenable(int mode)
2551{
2552 amd_iommu_resume();
2553
2554 return 0;
2555}
2556
2557int __init amd_iommu_enable_faulting(void)
2558{
2559 /* We enable MSI later when PCI is initialized */
2560 return 0;
2561}
2562#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002563
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002564/*
2565 * This is the core init function for AMD IOMMU hardware in the system.
2566 * This function is called from the generic x86 DMA layer initialization
2567 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002568 */
2569static int __init amd_iommu_init(void)
2570{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002571 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002572
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002573 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2574 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002575 free_dma_resources();
2576 if (!irq_remapping_enabled) {
2577 disable_iommus();
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002578 free_iommu_resources();
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002579 } else {
2580 struct amd_iommu *iommu;
2581
2582 uninit_device_table_dma();
2583 for_each_iommu(iommu)
2584 iommu_flush_all_caches(iommu);
2585 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002586 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002587
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002588 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002589}
2590
Joerg Roedelb65233a2008-07-11 17:14:21 +02002591/****************************************************************************
2592 *
2593 * Early detect code. This code runs at IOMMU detection time in the DMA
2594 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2595 * IOMMUs
2596 *
2597 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002598int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002599{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002600 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002601
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002602 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002603 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002604
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002605 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2606 if (ret)
2607 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002608
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002609 amd_iommu_detected = true;
2610 iommu_detected = 1;
2611 x86_init.iommu.iommu_init = amd_iommu_init;
2612
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002613 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002614}
2615
Joerg Roedelb65233a2008-07-11 17:14:21 +02002616/****************************************************************************
2617 *
2618 * Parsing functions for the AMD IOMMU specific kernel command line
2619 * options.
2620 *
2621 ****************************************************************************/
2622
Joerg Roedelfefda112009-05-20 12:21:42 +02002623static int __init parse_amd_iommu_dump(char *str)
2624{
2625 amd_iommu_dump = true;
2626
2627 return 1;
2628}
2629
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002630static int __init parse_amd_iommu_intr(char *str)
2631{
2632 for (; *str; ++str) {
2633 if (strncmp(str, "legacy", 6) == 0) {
2634 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2635 break;
2636 }
2637 if (strncmp(str, "vapic", 5) == 0) {
2638 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2639 break;
2640 }
2641 }
2642 return 1;
2643}
2644
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002645static int __init parse_amd_iommu_options(char *str)
2646{
2647 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002648 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002649 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002650 if (strncmp(str, "off", 3) == 0)
2651 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002652 if (strncmp(str, "force_isolation", 15) == 0)
2653 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002654 }
2655
2656 return 1;
2657}
2658
Joerg Roedel440e89982013-04-09 16:35:28 +02002659static int __init parse_ivrs_ioapic(char *str)
2660{
2661 unsigned int bus, dev, fn;
2662 int ret, id, i;
2663 u16 devid;
2664
2665 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2666
2667 if (ret != 4) {
2668 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2669 return 1;
2670 }
2671
2672 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2673 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2674 str);
2675 return 1;
2676 }
2677
2678 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2679
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002680 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002681 i = early_ioapic_map_size++;
2682 early_ioapic_map[i].id = id;
2683 early_ioapic_map[i].devid = devid;
2684 early_ioapic_map[i].cmd_line = true;
2685
2686 return 1;
2687}
2688
2689static int __init parse_ivrs_hpet(char *str)
2690{
2691 unsigned int bus, dev, fn;
2692 int ret, id, i;
2693 u16 devid;
2694
2695 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2696
2697 if (ret != 4) {
2698 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2699 return 1;
2700 }
2701
2702 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2703 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2704 str);
2705 return 1;
2706 }
2707
2708 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2709
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002710 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002711 i = early_hpet_map_size++;
2712 early_hpet_map[i].id = id;
2713 early_hpet_map[i].devid = devid;
2714 early_hpet_map[i].cmd_line = true;
2715
2716 return 1;
2717}
2718
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002719static int __init parse_ivrs_acpihid(char *str)
2720{
2721 u32 bus, dev, fn;
2722 char *hid, *uid, *p;
2723 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2724 int ret, i;
2725
2726 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2727 if (ret != 4) {
2728 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2729 return 1;
2730 }
2731
2732 p = acpiid;
2733 hid = strsep(&p, ":");
2734 uid = p;
2735
2736 if (!hid || !(*hid) || !uid) {
2737 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2738 return 1;
2739 }
2740
2741 i = early_acpihid_map_size++;
2742 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2743 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2744 early_acpihid_map[i].devid =
2745 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2746 early_acpihid_map[i].cmd_line = true;
2747
2748 return 1;
2749}
2750
Joerg Roedel440e89982013-04-09 16:35:28 +02002751__setup("amd_iommu_dump", parse_amd_iommu_dump);
2752__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002753__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002754__setup("ivrs_ioapic", parse_ivrs_ioapic);
2755__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002756__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002757
2758IOMMU_INIT_FINISH(amd_iommu_detect,
2759 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002760 NULL,
2761 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002762
2763bool amd_iommu_v2_supported(void)
2764{
2765 return amd_iommu_v2_present;
2766}
2767EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002768
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002769struct amd_iommu *get_amd_iommu(unsigned int idx)
2770{
2771 unsigned int i = 0;
2772 struct amd_iommu *iommu;
2773
2774 for_each_iommu(iommu)
2775 if (i++ == idx)
2776 return iommu;
2777 return NULL;
2778}
2779EXPORT_SYMBOL(get_amd_iommu);
2780
Steven L Kinney30861dd2013-06-05 16:11:48 -05002781/****************************************************************************
2782 *
2783 * IOMMU EFR Performance Counter support functionality. This code allows
2784 * access to the IOMMU PC functionality.
2785 *
2786 ****************************************************************************/
2787
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002788u8 amd_iommu_pc_get_max_banks(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002789{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002790 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002791
Steven L Kinney30861dd2013-06-05 16:11:48 -05002792 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002793 return iommu->max_banks;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002794
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002795 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002796}
2797EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2798
2799bool amd_iommu_pc_supported(void)
2800{
2801 return amd_iommu_pc_present;
2802}
2803EXPORT_SYMBOL(amd_iommu_pc_supported);
2804
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002805u8 amd_iommu_pc_get_max_counters(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002806{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002807 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002808
Steven L Kinney30861dd2013-06-05 16:11:48 -05002809 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002810 return iommu->max_counters;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002811
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002812 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002813}
2814EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2815
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002816static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
2817 u8 fxn, u64 *value, bool is_write)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002818{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002819 u32 offset;
2820 u32 max_offset_lim;
2821
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002822 /* Make sure the IOMMU PC resource is available */
2823 if (!amd_iommu_pc_present)
2824 return -ENODEV;
2825
Steven L Kinney30861dd2013-06-05 16:11:48 -05002826 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002827 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002828 return -ENODEV;
2829
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002830 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002831
2832 /* Limit the offset to the hw defined mmio region aperture */
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002833 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
Steven L Kinney30861dd2013-06-05 16:11:48 -05002834 (iommu->max_counters << 8) | 0x28);
2835 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2836 (offset > max_offset_lim))
2837 return -EINVAL;
2838
2839 if (is_write) {
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002840 u64 val = *value & GENMASK_ULL(47, 0);
2841
2842 writel((u32)val, iommu->mmio_base + offset);
2843 writel((val >> 32), iommu->mmio_base + offset + 4);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002844 } else {
2845 *value = readl(iommu->mmio_base + offset + 4);
2846 *value <<= 32;
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002847 *value |= readl(iommu->mmio_base + offset);
2848 *value &= GENMASK_ULL(47, 0);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002849 }
2850
2851 return 0;
2852}
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002853
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002854int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002855{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002856 if (!iommu)
2857 return -EINVAL;
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002858
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002859 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002860}
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002861EXPORT_SYMBOL(amd_iommu_pc_get_reg);
2862
2863int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
2864{
2865 if (!iommu)
2866 return -EINVAL;
2867
2868 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
2869}
2870EXPORT_SYMBOL(amd_iommu_pc_set_reg);