blob: ff8887ac5555210f3f69a2676b6f954c1a381e92 [file] [log] [blame]
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
21#include <linux/acpi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020022#include <linux/list.h>
Baoquan He5c87f622016-09-15 16:50:51 +080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010025#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020026#include <linux/interrupt.h>
27#include <linux/msi.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020028#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010029#include <linux/export.h>
Alex Williamson066f2e92014-06-12 16:12:37 -060030#include <linux/iommu.h>
Lucas Stachebcfa282016-10-26 13:09:53 +020031#include <linux/kmemleak.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020032#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090033#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010034#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090035#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040036#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020037#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020038#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020039
Baoquan He3ac3e5ee2017-08-09 16:33:38 +080040#include <linux/crash_dump.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020041#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;
Baoquan He45a01c42017-08-09 16:33:37 +0800199/*
200 * Pointer to a device table which the content of old device table
201 * will be copied to. It's only be used in kdump kernel.
202 */
203static struct dev_table_entry *old_dev_tbl_cpy;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200204
205/*
206 * The alias table is a driver specific data structure which contains the
207 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
208 * More than one device can share the same requestor id.
209 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200210u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200211
212/*
213 * The rlookup table is used to find the IOMMU which is responsible
214 * for a specific device. It is also indexed by the PCI device id.
215 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200216struct amd_iommu **amd_iommu_rlookup_table;
Baoquan Hedaae2d22017-08-09 16:33:43 +0800217EXPORT_SYMBOL(amd_iommu_rlookup_table);
Joerg Roedelb65233a2008-07-11 17:14:21 +0200218
219/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200220 * This table is used to find the irq remapping table for a given device id
221 * quickly.
222 */
223struct irq_remap_table **irq_lookup_table;
224
225/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200226 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200227 * to know which ones are already in use.
228 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200229unsigned long *amd_iommu_pd_alloc_bitmap;
230
Joerg Roedelb65233a2008-07-11 17:14:21 +0200231static u32 dev_table_size; /* size of the device table */
232static u32 alias_table_size; /* size of the alias table */
233static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200234
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200235enum iommu_init_state {
236 IOMMU_START_STATE,
237 IOMMU_IVRS_DETECTED,
238 IOMMU_ACPI_FINISHED,
239 IOMMU_ENABLED,
240 IOMMU_PCI_INIT,
241 IOMMU_INTERRUPTS_EN,
242 IOMMU_DMA_OPS,
243 IOMMU_INITIALIZED,
244 IOMMU_NOT_FOUND,
245 IOMMU_INIT_ERROR,
Joerg Roedel1b1e9422017-06-16 16:09:56 +0200246 IOMMU_CMDLINE_DISABLED,
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200247};
248
Joerg Roedel235dacb2013-04-09 17:53:14 +0200249/* Early ioapic and hpet maps from kernel command line */
250#define EARLY_MAP_SIZE 4
251static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
252static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400253static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
254
Joerg Roedel235dacb2013-04-09 17:53:14 +0200255static int __initdata early_ioapic_map_size;
256static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400257static int __initdata early_acpihid_map_size;
258
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200259static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200260
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200261static enum iommu_init_state init_state = IOMMU_START_STATE;
262
Gerard Snitselaarae295142012-03-16 11:38:22 -0700263static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200264static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200265static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100266
Joerg Roedel2479c632017-08-19 00:35:40 +0200267static bool amd_iommu_pre_enabled = true;
Baoquan He3ac3e5ee2017-08-09 16:33:38 +0800268
Baoquan He4c232a72017-08-09 16:33:33 +0800269bool translation_pre_enabled(struct amd_iommu *iommu)
270{
271 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
272}
Baoquan Hedaae2d22017-08-09 16:33:43 +0800273EXPORT_SYMBOL(translation_pre_enabled);
Baoquan He4c232a72017-08-09 16:33:33 +0800274
275static void clear_translation_pre_enabled(struct amd_iommu *iommu)
276{
277 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
278}
279
280static void init_translation_status(struct amd_iommu *iommu)
281{
282 u32 ctrl;
283
284 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
285 if (ctrl & (1<<CONTROL_IOMMU_EN))
286 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
287}
288
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200289static inline void update_last_devid(u16 devid)
290{
291 if (devid > amd_iommu_last_bdf)
292 amd_iommu_last_bdf = devid;
293}
294
Joerg Roedelc5714842008-07-11 17:14:25 +0200295static inline unsigned long tbl_size(int entry_size)
296{
297 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100298 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200299
300 return 1UL << shift;
301}
302
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600303int amd_iommu_get_num_iommus(void)
304{
305 return amd_iommus_present;
306}
307
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400308/* Access to l1 and l2 indexed register spaces */
309
310static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
311{
312 u32 val;
313
314 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
315 pci_read_config_dword(iommu->dev, 0xfc, &val);
316 return val;
317}
318
319static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
320{
321 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
322 pci_write_config_dword(iommu->dev, 0xfc, val);
323 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
324}
325
326static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
327{
328 u32 val;
329
330 pci_write_config_dword(iommu->dev, 0xf0, address);
331 pci_read_config_dword(iommu->dev, 0xf4, &val);
332 return val;
333}
334
335static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
336{
337 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
338 pci_write_config_dword(iommu->dev, 0xf4, val);
339}
340
Joerg Roedelb65233a2008-07-11 17:14:21 +0200341/****************************************************************************
342 *
343 * AMD IOMMU MMIO register space handling functions
344 *
345 * These functions are used to program the IOMMU device registers in
346 * MMIO space required for that driver.
347 *
348 ****************************************************************************/
349
350/*
351 * This function set the exclusion range in the IOMMU. DMA accesses to the
352 * exclusion range are passed through untranslated
353 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200354static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200355{
356 u64 start = iommu->exclusion_start & PAGE_MASK;
357 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
358 u64 entry;
359
360 if (!iommu->exclusion_start)
361 return;
362
363 entry = start | MMIO_EXCL_ENABLE_MASK;
364 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
365 &entry, sizeof(entry));
366
367 entry = limit;
368 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
369 &entry, sizeof(entry));
370}
371
Joerg Roedelb65233a2008-07-11 17:14:21 +0200372/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000373static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200374{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200375 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200376
377 BUG_ON(iommu->mmio_base == NULL);
378
379 entry = virt_to_phys(amd_iommu_dev_table);
380 entry |= (dev_table_size >> 12) - 1;
381 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
382 &entry, sizeof(entry));
383}
384
Joerg Roedelb65233a2008-07-11 17:14:21 +0200385/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200386static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200387{
388 u32 ctrl;
389
390 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
391 ctrl |= (1 << bit);
392 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
393}
394
Joerg Roedelca0207112009-10-28 18:02:26 +0100395static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200396{
397 u32 ctrl;
398
Joerg Roedel199d0d52008-09-17 16:45:59 +0200399 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200400 ctrl &= ~(1 << bit);
401 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
402}
403
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100404static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
405{
406 u32 ctrl;
407
408 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
409 ctrl &= ~CTRL_INV_TO_MASK;
410 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
411 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
412}
413
Joerg Roedelb65233a2008-07-11 17:14:21 +0200414/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200415static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200416{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200417 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200418}
419
Joerg Roedel92ac4322009-05-19 19:06:27 +0200420static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200421{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200422 /* Disable command buffer */
423 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
424
425 /* Disable event logging and event interrupts */
426 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
427 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
428
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500429 /* Disable IOMMU GA_LOG */
430 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
431 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
432
Chris Wrighta8c485b2009-06-15 15:53:45 +0200433 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200434 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200435}
436
Joerg Roedelb65233a2008-07-11 17:14:21 +0200437/*
438 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
439 * the system has one.
440 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500441static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200442{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500443 if (!request_mem_region(address, end, "amd_iommu")) {
444 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
445 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200446 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200447 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200448 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200449
Steven L Kinney30861dd2013-06-05 16:11:48 -0500450 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200451}
452
453static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
454{
455 if (iommu->mmio_base)
456 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500457 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200458}
459
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400460static inline u32 get_ivhd_header_size(struct ivhd_header *h)
461{
462 u32 size = 0;
463
464 switch (h->type) {
465 case 0x10:
466 size = 24;
467 break;
468 case 0x11:
469 case 0x40:
470 size = 40;
471 break;
472 }
473 return size;
474}
475
Joerg Roedelb65233a2008-07-11 17:14:21 +0200476/****************************************************************************
477 *
478 * The functions below belong to the first pass of AMD IOMMU ACPI table
479 * parsing. In this pass we try to find out the highest device id this
480 * code has to handle. Upon this information the size of the shared data
481 * structures is determined later.
482 *
483 ****************************************************************************/
484
485/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200486 * This function calculates the length of a given IVHD entry
487 */
488static inline int ivhd_entry_length(u8 *ivhd)
489{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400490 u32 type = ((struct ivhd_entry *)ivhd)->type;
491
492 if (type < 0x80) {
493 return 0x04 << (*ivhd >> 6);
494 } else if (type == IVHD_DEV_ACPI_HID) {
495 /* For ACPI_HID, offset 21 is uid len */
496 return *((u8 *)ivhd + 21) + 22;
497 }
498 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200499}
500
501/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200502 * After reading the highest device id from the IOMMU PCI capability header
503 * this function looks if there is a higher device id defined in the ACPI table
504 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200505static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
506{
507 u8 *p = (void *)h, *end = (void *)h;
508 struct ivhd_entry *dev;
509
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400510 u32 ivhd_size = get_ivhd_header_size(h);
511
512 if (!ivhd_size) {
513 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
514 return -EINVAL;
515 }
516
517 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200518 end += h->length;
519
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200520 while (p < end) {
521 dev = (struct ivhd_entry *)p;
522 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200523 case IVHD_DEV_ALL:
524 /* Use maximum BDF value for DEV_ALL */
525 update_last_devid(0xffff);
526 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200527 case IVHD_DEV_SELECT:
528 case IVHD_DEV_RANGE_END:
529 case IVHD_DEV_ALIAS:
530 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200531 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200532 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200533 break;
534 default:
535 break;
536 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200537 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200538 }
539
540 WARN_ON(p != end);
541
542 return 0;
543}
544
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400545static int __init check_ivrs_checksum(struct acpi_table_header *table)
546{
547 int i;
548 u8 checksum = 0, *p = (u8 *)table;
549
550 for (i = 0; i < table->length; ++i)
551 checksum += p[i];
552 if (checksum != 0) {
553 /* ACPI table corrupt */
554 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
555 return -ENODEV;
556 }
557
558 return 0;
559}
560
Joerg Roedelb65233a2008-07-11 17:14:21 +0200561/*
562 * Iterate over all IVHD entries in the ACPI table and find the highest device
563 * id which we need to handle. This is the first of three functions which parse
564 * the ACPI table. So we check the checksum here.
565 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200566static int __init find_last_devid_acpi(struct acpi_table_header *table)
567{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400568 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200569 struct ivhd_header *h;
570
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200571 p += IVRS_HEADER_LENGTH;
572
573 end += table->length;
574 while (p < end) {
575 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400576 if (h->type == amd_iommu_target_ivhd_type) {
577 int ret = find_last_devid_from_ivhd(h);
578
579 if (ret)
580 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200581 }
582 p += h->length;
583 }
584 WARN_ON(p != end);
585
586 return 0;
587}
588
Joerg Roedelb65233a2008-07-11 17:14:21 +0200589/****************************************************************************
590 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200591 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200592 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
593 * data structures, initialize the device/alias/rlookup table and also
594 * basically initialize the hardware.
595 *
596 ****************************************************************************/
597
598/*
599 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
600 * write commands to that buffer later and the IOMMU will execute them
601 * asynchronously
602 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200603static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200604{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200605 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
606 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200607
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200608 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200609}
610
611/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200612 * This function resets the command buffer if the IOMMU stopped fetching
613 * commands from it.
614 */
615void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
616{
617 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
618
619 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
620 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Tom Lendackyd334a562017-06-05 14:52:12 -0500621 iommu->cmd_buf_head = 0;
622 iommu->cmd_buf_tail = 0;
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200623
624 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
625}
626
627/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200628 * This function writes the command buffer address to the hardware and
629 * enables it.
630 */
631static void iommu_enable_command_buffer(struct amd_iommu *iommu)
632{
633 u64 entry;
634
635 BUG_ON(iommu->cmd_buf == NULL);
636
637 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200638 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200639
Joerg Roedelb36ca912008-06-26 21:27:45 +0200640 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200641 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200642
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200643 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200644}
645
Baoquan He78d313c2017-08-09 16:33:34 +0800646/*
647 * This function disables the command buffer
648 */
649static void iommu_disable_command_buffer(struct amd_iommu *iommu)
650{
651 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
652}
653
Joerg Roedelb36ca912008-06-26 21:27:45 +0200654static void __init free_command_buffer(struct amd_iommu *iommu)
655{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200656 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200657}
658
Joerg Roedel335503e2008-09-05 14:29:07 +0200659/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200660static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200661{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200662 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
663 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200664
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200665 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200666}
667
668static void iommu_enable_event_buffer(struct amd_iommu *iommu)
669{
670 u64 entry;
671
672 BUG_ON(iommu->evt_buf == NULL);
673
Joerg Roedel335503e2008-09-05 14:29:07 +0200674 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200675
Joerg Roedel335503e2008-09-05 14:29:07 +0200676 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
677 &entry, sizeof(entry));
678
Joerg Roedel090672072009-06-15 16:06:48 +0200679 /* set head and tail to zero manually */
680 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
681 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
682
Joerg Roedel58492e12009-05-04 18:41:16 +0200683 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200684}
685
Baoquan He78d313c2017-08-09 16:33:34 +0800686/*
687 * This function disables the event log buffer
688 */
689static void iommu_disable_event_buffer(struct amd_iommu *iommu)
690{
691 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
692}
693
Joerg Roedel335503e2008-09-05 14:29:07 +0200694static void __init free_event_buffer(struct amd_iommu *iommu)
695{
696 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
697}
698
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100699/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200700static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100701{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200702 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
703 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100704
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200705 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100706}
707
708static void iommu_enable_ppr_log(struct amd_iommu *iommu)
709{
710 u64 entry;
711
712 if (iommu->ppr_log == NULL)
713 return;
714
715 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
716
717 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
718 &entry, sizeof(entry));
719
720 /* set head and tail to zero manually */
721 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
722 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
723
724 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
725 iommu_feature_enable(iommu, CONTROL_PPR_EN);
726}
727
728static void __init free_ppr_log(struct amd_iommu *iommu)
729{
730 if (iommu->ppr_log == NULL)
731 return;
732
733 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
734}
735
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500736static void free_ga_log(struct amd_iommu *iommu)
737{
738#ifdef CONFIG_IRQ_REMAP
739 if (iommu->ga_log)
740 free_pages((unsigned long)iommu->ga_log,
741 get_order(GA_LOG_SIZE));
742 if (iommu->ga_log_tail)
743 free_pages((unsigned long)iommu->ga_log_tail,
744 get_order(8));
745#endif
746}
747
748static int iommu_ga_log_enable(struct amd_iommu *iommu)
749{
750#ifdef CONFIG_IRQ_REMAP
751 u32 status, i;
752
753 if (!iommu->ga_log)
754 return -EINVAL;
755
756 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
757
758 /* Check if already running */
759 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
760 return 0;
761
762 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
763 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
764
765 for (i = 0; i < LOOP_TIMEOUT; ++i) {
766 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
767 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
768 break;
769 }
770
771 if (i >= LOOP_TIMEOUT)
772 return -EINVAL;
773#endif /* CONFIG_IRQ_REMAP */
774 return 0;
775}
776
777#ifdef CONFIG_IRQ_REMAP
778static int iommu_init_ga_log(struct amd_iommu *iommu)
779{
780 u64 entry;
781
782 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
783 return 0;
784
785 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
786 get_order(GA_LOG_SIZE));
787 if (!iommu->ga_log)
788 goto err_out;
789
790 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
791 get_order(8));
792 if (!iommu->ga_log_tail)
793 goto err_out;
794
795 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
796 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
797 &entry, sizeof(entry));
798 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
799 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
800 &entry, sizeof(entry));
801 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
802 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
803
804 return 0;
805err_out:
806 free_ga_log(iommu);
807 return -EINVAL;
808}
809#endif /* CONFIG_IRQ_REMAP */
810
811static int iommu_init_ga(struct amd_iommu *iommu)
812{
813 int ret = 0;
814
815#ifdef CONFIG_IRQ_REMAP
816 /* Note: We have already checked GASup from IVRS table.
817 * Now, we need to make sure that GAMSup is set.
818 */
819 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
820 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
821 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
822
823 ret = iommu_init_ga_log(iommu);
824#endif /* CONFIG_IRQ_REMAP */
825
826 return ret;
827}
828
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100829static void iommu_enable_gt(struct amd_iommu *iommu)
830{
831 if (!iommu_feature(iommu, FEATURE_GT))
832 return;
833
834 iommu_feature_enable(iommu, CONTROL_GT_EN);
835}
836
Joerg Roedelb65233a2008-07-11 17:14:21 +0200837/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200838static void set_dev_entry_bit(u16 devid, u8 bit)
839{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100840 int i = (bit >> 6) & 0x03;
841 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200842
Joerg Roedelee6c2862011-11-09 12:06:03 +0100843 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200844}
845
Joerg Roedelc5cca142009-10-09 18:31:20 +0200846static int get_dev_entry_bit(u16 devid, u8 bit)
847{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100848 int i = (bit >> 6) & 0x03;
849 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200850
Joerg Roedelee6c2862011-11-09 12:06:03 +0100851 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200852}
853
854
Baoquan He45a01c42017-08-09 16:33:37 +0800855static bool copy_device_table(void)
856{
Joerg Roedelae162ef2017-08-19 00:28:02 +0200857 u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
Baoquan He45a01c42017-08-09 16:33:37 +0800858 struct dev_table_entry *old_devtb = NULL;
859 u32 lo, hi, devid, old_devtb_size;
860 phys_addr_t old_devtb_phys;
Baoquan He45a01c42017-08-09 16:33:37 +0800861 struct amd_iommu *iommu;
Baoquan He53019a92017-08-09 16:33:39 +0800862 u16 dom_id, dte_v, irq_v;
Baoquan He45a01c42017-08-09 16:33:37 +0800863 gfp_t gfp_flag;
Baoquan Hedaae2d22017-08-09 16:33:43 +0800864 u64 tmp;
Baoquan He45a01c42017-08-09 16:33:37 +0800865
Baoquan He3ac3e5ee2017-08-09 16:33:38 +0800866 if (!amd_iommu_pre_enabled)
867 return false;
Baoquan He45a01c42017-08-09 16:33:37 +0800868
869 pr_warn("Translation is already enabled - trying to copy translation structures\n");
870 for_each_iommu(iommu) {
871 /* All IOMMUs should use the same device table with the same size */
872 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
873 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
874 entry = (((u64) hi) << 32) + lo;
875 if (last_entry && last_entry != entry) {
876 pr_err("IOMMU:%d should use the same dev table as others!/n",
877 iommu->index);
878 return false;
879 }
880 last_entry = entry;
881
882 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
883 if (old_devtb_size != dev_table_size) {
884 pr_err("The device table size of IOMMU:%d is not expected!/n",
885 iommu->index);
886 return false;
887 }
888 }
889
890 old_devtb_phys = entry & PAGE_MASK;
Baoquan Heb3367812017-08-09 16:33:42 +0800891 if (old_devtb_phys >= 0x100000000ULL) {
892 pr_err("The address of old device table is above 4G, not trustworthy!/n");
893 return false;
894 }
Baoquan He45a01c42017-08-09 16:33:37 +0800895 old_devtb = memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
896 if (!old_devtb)
897 return false;
898
Baoquan Heb3367812017-08-09 16:33:42 +0800899 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
Baoquan He45a01c42017-08-09 16:33:37 +0800900 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
901 get_order(dev_table_size));
902 if (old_dev_tbl_cpy == NULL) {
903 pr_err("Failed to allocate memory for copying old device table!/n");
904 return false;
905 }
906
907 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
908 old_dev_tbl_cpy[devid] = old_devtb[devid];
909 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
910 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
Baoquan He53019a92017-08-09 16:33:39 +0800911
912 if (dte_v && dom_id) {
913 old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
914 old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
Baoquan He45a01c42017-08-09 16:33:37 +0800915 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
Baoquan Hedaae2d22017-08-09 16:33:43 +0800916 /* If gcr3 table existed, mask it out */
917 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
918 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
919 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
920 old_dev_tbl_cpy[devid].data[1] &= ~tmp;
921 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
922 tmp |= DTE_FLAG_GV;
923 old_dev_tbl_cpy[devid].data[0] &= ~tmp;
924 }
Baoquan He53019a92017-08-09 16:33:39 +0800925 }
926
927 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
928 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
929 int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK;
930 if (irq_v && (int_ctl || int_tab_len)) {
931 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
932 (int_tab_len != DTE_IRQ_TABLE_LEN)) {
933 pr_err("Wrong old irq remapping flag: %#x\n", devid);
934 return false;
935 }
936
937 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
938 }
Baoquan He45a01c42017-08-09 16:33:37 +0800939 }
940 memunmap(old_devtb);
941
942 return true;
943}
944
Joerg Roedelc5cca142009-10-09 18:31:20 +0200945void amd_iommu_apply_erratum_63(u16 devid)
946{
947 int sysmgt;
948
949 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
950 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
951
952 if (sysmgt == 0x01)
953 set_dev_entry_bit(devid, DEV_ENTRY_IW);
954}
955
Joerg Roedel5ff47892008-07-14 20:11:18 +0200956/* Writes the specific IOMMU for a device into the rlookup table */
957static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
958{
959 amd_iommu_rlookup_table[devid] = iommu;
960}
961
Joerg Roedelb65233a2008-07-11 17:14:21 +0200962/*
963 * This function takes the device specific flags read from the ACPI
964 * table and sets up the device table entry with that information
965 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200966static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
967 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200968{
969 if (flags & ACPI_DEVFLAG_INITPASS)
970 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
971 if (flags & ACPI_DEVFLAG_EXTINT)
972 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
973 if (flags & ACPI_DEVFLAG_NMI)
974 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
975 if (flags & ACPI_DEVFLAG_SYSMGT1)
976 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
977 if (flags & ACPI_DEVFLAG_SYSMGT2)
978 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
979 if (flags & ACPI_DEVFLAG_LINT0)
980 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
981 if (flags & ACPI_DEVFLAG_LINT1)
982 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200983
Joerg Roedelc5cca142009-10-09 18:31:20 +0200984 amd_iommu_apply_erratum_63(devid);
985
Joerg Roedel5ff47892008-07-14 20:11:18 +0200986 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200987}
988
Joerg Roedelc50e3242014-09-09 15:59:37 +0200989static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200990{
991 struct devid_map *entry;
992 struct list_head *list;
993
Joerg Roedel31cff672013-04-09 16:53:58 +0200994 if (type == IVHD_SPECIAL_IOAPIC)
995 list = &ioapic_map;
996 else if (type == IVHD_SPECIAL_HPET)
997 list = &hpet_map;
998 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200999 return -EINVAL;
1000
Joerg Roedel31cff672013-04-09 16:53:58 +02001001 list_for_each_entry(entry, list, list) {
1002 if (!(entry->id == id && entry->cmd_line))
1003 continue;
1004
1005 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
1006 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1007
Joerg Roedelc50e3242014-09-09 15:59:37 +02001008 *devid = entry->devid;
1009
Joerg Roedel31cff672013-04-09 16:53:58 +02001010 return 0;
1011 }
1012
Joerg Roedel6efed632012-06-14 15:52:58 +02001013 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1014 if (!entry)
1015 return -ENOMEM;
1016
Joerg Roedel31cff672013-04-09 16:53:58 +02001017 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001018 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +02001019 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +02001020
1021 list_add_tail(&entry->list, list);
1022
1023 return 0;
1024}
1025
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001026static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1027 bool cmd_line)
1028{
1029 struct acpihid_map_entry *entry;
1030 struct list_head *list = &acpihid_map;
1031
1032 list_for_each_entry(entry, list, list) {
1033 if (strcmp(entry->hid, hid) ||
1034 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1035 !entry->cmd_line)
1036 continue;
1037
1038 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
1039 hid, uid);
1040 *devid = entry->devid;
1041 return 0;
1042 }
1043
1044 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1045 if (!entry)
1046 return -ENOMEM;
1047
1048 memcpy(entry->uid, uid, strlen(uid));
1049 memcpy(entry->hid, hid, strlen(hid));
1050 entry->devid = *devid;
1051 entry->cmd_line = cmd_line;
1052 entry->root_devid = (entry->devid & (~0x7));
1053
1054 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
1055 entry->cmd_line ? "cmd" : "ivrs",
1056 entry->hid, entry->uid, entry->root_devid);
1057
1058 list_add_tail(&entry->list, list);
1059 return 0;
1060}
1061
Joerg Roedel235dacb2013-04-09 17:53:14 +02001062static int __init add_early_maps(void)
1063{
1064 int i, ret;
1065
1066 for (i = 0; i < early_ioapic_map_size; ++i) {
1067 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1068 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +02001069 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +02001070 early_ioapic_map[i].cmd_line);
1071 if (ret)
1072 return ret;
1073 }
1074
1075 for (i = 0; i < early_hpet_map_size; ++i) {
1076 ret = add_special_device(IVHD_SPECIAL_HPET,
1077 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +02001078 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +02001079 early_hpet_map[i].cmd_line);
1080 if (ret)
1081 return ret;
1082 }
1083
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001084 for (i = 0; i < early_acpihid_map_size; ++i) {
1085 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1086 early_acpihid_map[i].uid,
1087 &early_acpihid_map[i].devid,
1088 early_acpihid_map[i].cmd_line);
1089 if (ret)
1090 return ret;
1091 }
1092
Joerg Roedel235dacb2013-04-09 17:53:14 +02001093 return 0;
1094}
1095
Joerg Roedelb65233a2008-07-11 17:14:21 +02001096/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02001097 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +02001098 * it
1099 */
Joerg Roedel3566b772008-06-26 21:27:46 +02001100static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
1101{
1102 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1103
1104 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
1105 return;
1106
1107 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +02001108 /*
1109 * We only can configure exclusion ranges per IOMMU, not
1110 * per device. But we can enable the exclusion range per
1111 * device. This is done here
1112 */
Su Friendy2c16c9f2014-05-07 13:54:52 +08001113 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +02001114 iommu->exclusion_start = m->range_start;
1115 iommu->exclusion_length = m->range_length;
1116 }
1117}
1118
Joerg Roedelb65233a2008-07-11 17:14:21 +02001119/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001120 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1121 * initializes the hardware and our data structures with it.
1122 */
Joerg Roedel6efed632012-06-14 15:52:58 +02001123static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001124 struct ivhd_header *h)
1125{
1126 u8 *p = (u8 *)h;
1127 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +02001128 u16 devid = 0, devid_start = 0, devid_to = 0;
1129 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001130 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001131 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001132 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +02001133 int ret;
1134
1135
1136 ret = add_early_maps();
1137 if (ret)
1138 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001139
1140 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001141 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001142 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001143 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001144
1145 /*
1146 * Done. Now parse the device entries
1147 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001148 ivhd_size = get_ivhd_header_size(h);
1149 if (!ivhd_size) {
1150 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1151 return -EINVAL;
1152 }
1153
1154 p += ivhd_size;
1155
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001156 end += h->length;
1157
Joerg Roedel42a698f2009-05-20 15:41:28 +02001158
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001159 while (p < end) {
1160 e = (struct ivhd_entry *)p;
1161 switch (e->type) {
1162 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001163
Joerg Roedel226e8892015-10-20 17:33:44 +02001164 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001165
Joerg Roedel226e8892015-10-20 17:33:44 +02001166 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1167 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001168 break;
1169 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001170
1171 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1172 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001173 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001174 PCI_SLOT(e->devid),
1175 PCI_FUNC(e->devid),
1176 e->flags);
1177
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001178 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001179 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001180 break;
1181 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001182
1183 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1184 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001185 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001186 PCI_SLOT(e->devid),
1187 PCI_FUNC(e->devid),
1188 e->flags);
1189
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001190 devid_start = e->devid;
1191 flags = e->flags;
1192 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001193 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001194 break;
1195 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001196
1197 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1198 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001199 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001200 PCI_SLOT(e->devid),
1201 PCI_FUNC(e->devid),
1202 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001203 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001204 PCI_SLOT(e->ext >> 8),
1205 PCI_FUNC(e->ext >> 8));
1206
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001207 devid = e->devid;
1208 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001209 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001210 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001211 amd_iommu_alias_table[devid] = devid_to;
1212 break;
1213 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001214
1215 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1216 "devid: %02x:%02x.%x flags: %02x "
1217 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001218 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001219 PCI_SLOT(e->devid),
1220 PCI_FUNC(e->devid),
1221 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001222 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001223 PCI_SLOT(e->ext >> 8),
1224 PCI_FUNC(e->ext >> 8));
1225
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001226 devid_start = e->devid;
1227 flags = e->flags;
1228 devid_to = e->ext >> 8;
1229 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001230 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001231 break;
1232 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001233
1234 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1235 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001236 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001237 PCI_SLOT(e->devid),
1238 PCI_FUNC(e->devid),
1239 e->flags, e->ext);
1240
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001241 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001242 set_dev_entry_from_acpi(iommu, devid, e->flags,
1243 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001244 break;
1245 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001246
1247 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1248 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001249 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001250 PCI_SLOT(e->devid),
1251 PCI_FUNC(e->devid),
1252 e->flags, e->ext);
1253
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001254 devid_start = e->devid;
1255 flags = e->flags;
1256 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001257 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001258 break;
1259 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001260
1261 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001262 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001263 PCI_SLOT(e->devid),
1264 PCI_FUNC(e->devid));
1265
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001266 devid = e->devid;
1267 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001268 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001269 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001270 set_dev_entry_from_acpi(iommu,
1271 devid_to, flags, ext_flags);
1272 }
1273 set_dev_entry_from_acpi(iommu, dev_i,
1274 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001275 }
1276 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001277 case IVHD_DEV_SPECIAL: {
1278 u8 handle, type;
1279 const char *var;
1280 u16 devid;
1281 int ret;
1282
1283 handle = e->ext & 0xff;
1284 devid = (e->ext >> 8) & 0xffff;
1285 type = (e->ext >> 24) & 0xff;
1286
1287 if (type == IVHD_SPECIAL_IOAPIC)
1288 var = "IOAPIC";
1289 else if (type == IVHD_SPECIAL_HPET)
1290 var = "HPET";
1291 else
1292 var = "UNKNOWN";
1293
1294 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1295 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001296 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001297 PCI_SLOT(devid),
1298 PCI_FUNC(devid));
1299
Joerg Roedelc50e3242014-09-09 15:59:37 +02001300 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001301 if (ret)
1302 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001303
1304 /*
1305 * add_special_device might update the devid in case a
1306 * command-line override is present. So call
1307 * set_dev_entry_from_acpi after add_special_device.
1308 */
1309 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1310
Joerg Roedel6efed632012-06-14 15:52:58 +02001311 break;
1312 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001313 case IVHD_DEV_ACPI_HID: {
1314 u16 devid;
1315 u8 hid[ACPIHID_HID_LEN] = {0};
1316 u8 uid[ACPIHID_UID_LEN] = {0};
1317 int ret;
1318
1319 if (h->type != 0x40) {
1320 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1321 e->type);
1322 break;
1323 }
1324
1325 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1326 hid[ACPIHID_HID_LEN - 1] = '\0';
1327
1328 if (!(*hid)) {
1329 pr_err(FW_BUG "Invalid HID.\n");
1330 break;
1331 }
1332
1333 switch (e->uidf) {
1334 case UID_NOT_PRESENT:
1335
1336 if (e->uidl != 0)
1337 pr_warn(FW_BUG "Invalid UID length.\n");
1338
1339 break;
1340 case UID_IS_INTEGER:
1341
1342 sprintf(uid, "%d", e->uid);
1343
1344 break;
1345 case UID_IS_CHARACTER:
1346
1347 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1348 uid[ACPIHID_UID_LEN - 1] = '\0';
1349
1350 break;
1351 default:
1352 break;
1353 }
1354
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001355 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001356 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1357 hid, uid,
1358 PCI_BUS_NUM(devid),
1359 PCI_SLOT(devid),
1360 PCI_FUNC(devid));
1361
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001362 flags = e->flags;
1363
1364 ret = add_acpi_hid_device(hid, uid, &devid, false);
1365 if (ret)
1366 return ret;
1367
1368 /*
1369 * add_special_device might update the devid in case a
1370 * command-line override is present. So call
1371 * set_dev_entry_from_acpi after add_special_device.
1372 */
1373 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1374
1375 break;
1376 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001377 default:
1378 break;
1379 }
1380
Joerg Roedelb514e552008-09-17 17:14:27 +02001381 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001382 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001383
1384 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001385}
1386
Joerg Roedele47d4022008-06-26 21:27:48 +02001387static void __init free_iommu_one(struct amd_iommu *iommu)
1388{
1389 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001390 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001391 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001392 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001393 iommu_unmap_mmio_space(iommu);
1394}
1395
1396static void __init free_iommu_all(void)
1397{
1398 struct amd_iommu *iommu, *next;
1399
Joerg Roedel3bd22172009-05-04 15:06:20 +02001400 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001401 list_del(&iommu->list);
1402 free_iommu_one(iommu);
1403 kfree(iommu);
1404 }
1405}
1406
Joerg Roedelb65233a2008-07-11 17:14:21 +02001407/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001408 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1409 * Workaround:
1410 * BIOS should disable L2B micellaneous clock gating by setting
1411 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1412 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001413static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001414{
1415 u32 value;
1416
1417 if ((boot_cpu_data.x86 != 0x15) ||
1418 (boot_cpu_data.x86_model < 0x10) ||
1419 (boot_cpu_data.x86_model > 0x1f))
1420 return;
1421
1422 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1423 pci_read_config_dword(iommu->dev, 0xf4, &value);
1424
1425 if (value & BIT(2))
1426 return;
1427
1428 /* Select NB indirect register 0x90 and enable writing */
1429 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1430
1431 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1432 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1433 dev_name(&iommu->dev->dev));
1434
1435 /* Clear the enable writing bit */
1436 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1437}
1438
1439/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001440 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1441 * Workaround:
1442 * BIOS should enable ATS write permission check by setting
1443 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1444 */
1445static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1446{
1447 u32 value;
1448
1449 if ((boot_cpu_data.x86 != 0x15) ||
1450 (boot_cpu_data.x86_model < 0x30) ||
1451 (boot_cpu_data.x86_model > 0x3f))
1452 return;
1453
1454 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1455 value = iommu_read_l2(iommu, 0x47);
1456
1457 if (value & BIT(0))
1458 return;
1459
1460 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1461 iommu_write_l2(iommu, 0x47, value | BIT(0));
1462
1463 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1464 dev_name(&iommu->dev->dev));
1465}
1466
1467/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001468 * This function clues the initialization function for one IOMMU
1469 * together and also allocates the command buffer and programs the
1470 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1471 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001472static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1473{
Joerg Roedel6efed632012-06-14 15:52:58 +02001474 int ret;
1475
Joerg Roedele47d4022008-06-26 21:27:48 +02001476 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001477
1478 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001479 list_add_tail(&iommu->list, &amd_iommu_list);
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001480 iommu->index = amd_iommus_present++;
Joerg Roedelbb527772009-11-20 14:31:51 +01001481
1482 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1483 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1484 return -ENOSYS;
1485 }
1486
1487 /* Index is fine - add IOMMU to the array */
1488 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001489
1490 /*
1491 * Copy data from ACPI table entry to the iommu struct
1492 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001493 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001494 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001495 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001496 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001497
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001498 switch (h->type) {
1499 case 0x10:
1500 /* Check if IVHD EFR contains proper max banks/counters */
1501 if ((h->efr_attr != 0) &&
1502 ((h->efr_attr & (0xF << 13)) != 0) &&
1503 ((h->efr_attr & (0x3F << 17)) != 0))
1504 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1505 else
1506 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001507 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1508 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001509 break;
1510 case 0x11:
1511 case 0x40:
1512 if (h->efr_reg & (1 << 9))
1513 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1514 else
1515 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001516 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1517 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001518 break;
1519 default:
1520 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001521 }
1522
1523 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1524 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001525 if (!iommu->mmio_base)
1526 return -ENOMEM;
1527
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001528 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001529 return -ENOMEM;
1530
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001531 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001532 return -ENOMEM;
1533
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001534 iommu->int_enabled = false;
1535
Baoquan He4c232a72017-08-09 16:33:33 +08001536 init_translation_status(iommu);
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08001537 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1538 iommu_disable(iommu);
1539 clear_translation_pre_enabled(iommu);
1540 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1541 iommu->index);
1542 }
1543 if (amd_iommu_pre_enabled)
1544 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
Baoquan He4c232a72017-08-09 16:33:33 +08001545
Joerg Roedel6efed632012-06-14 15:52:58 +02001546 ret = init_iommu_from_acpi(iommu, h);
1547 if (ret)
1548 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001549
Jiang Liu7c71d302015-04-13 14:11:33 +08001550 ret = amd_iommu_create_irq_domain(iommu);
1551 if (ret)
1552 return ret;
1553
Joerg Roedelf6fec002012-06-21 16:51:25 +02001554 /*
1555 * Make sure IOMMU is not considered to translate itself. The IVRS
1556 * table tells us so, but this is a lie!
1557 */
1558 amd_iommu_rlookup_table[iommu->devid] = NULL;
1559
Joerg Roedel23c742d2012-06-12 11:47:34 +02001560 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001561}
1562
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001563/**
1564 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1565 * @ivrs Pointer to the IVRS header
1566 *
1567 * This function search through all IVDB of the maximum supported IVHD
1568 */
1569static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1570{
1571 u8 *base = (u8 *)ivrs;
1572 struct ivhd_header *ivhd = (struct ivhd_header *)
1573 (base + IVRS_HEADER_LENGTH);
1574 u8 last_type = ivhd->type;
1575 u16 devid = ivhd->devid;
1576
1577 while (((u8 *)ivhd - base < ivrs->length) &&
1578 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1579 u8 *p = (u8 *) ivhd;
1580
1581 if (ivhd->devid == devid)
1582 last_type = ivhd->type;
1583 ivhd = (struct ivhd_header *)(p + ivhd->length);
1584 }
1585
1586 return last_type;
1587}
1588
Joerg Roedelb65233a2008-07-11 17:14:21 +02001589/*
1590 * Iterates over all IOMMU entries in the ACPI table, allocates the
1591 * IOMMU structure and initializes it with init_iommu_one()
1592 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001593static int __init init_iommu_all(struct acpi_table_header *table)
1594{
1595 u8 *p = (u8 *)table, *end = (u8 *)table;
1596 struct ivhd_header *h;
1597 struct amd_iommu *iommu;
1598 int ret;
1599
Joerg Roedele47d4022008-06-26 21:27:48 +02001600 end += table->length;
1601 p += IVRS_HEADER_LENGTH;
1602
1603 while (p < end) {
1604 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001605 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001606
Joerg Roedelae908c22009-09-01 16:52:16 +02001607 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001608 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001609 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001610 PCI_FUNC(h->devid), h->cap_ptr,
1611 h->pci_seg, h->flags, h->info);
1612 DUMP_printk(" mmio-addr: %016llx\n",
1613 h->mmio_phys);
1614
Joerg Roedele47d4022008-06-26 21:27:48 +02001615 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001616 if (iommu == NULL)
1617 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001618
Joerg Roedele47d4022008-06-26 21:27:48 +02001619 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001620 if (ret)
1621 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001622 }
1623 p += h->length;
1624
1625 }
1626 WARN_ON(p != end);
1627
1628 return 0;
1629}
1630
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001631static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1632 u8 fxn, u64 *value, bool is_write);
Steven L Kinney30861dd2013-06-05 16:11:48 -05001633
1634static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1635{
1636 u64 val = 0xabcd, val2 = 0;
1637
1638 if (!iommu_feature(iommu, FEATURE_PC))
1639 return;
1640
1641 amd_iommu_pc_present = true;
1642
1643 /* Check if the performance counters can be written to */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001644 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1645 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001646 (val != val2)) {
1647 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1648 amd_iommu_pc_present = false;
1649 return;
1650 }
1651
1652 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1653
1654 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1655 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1656 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1657}
1658
Alex Williamson066f2e92014-06-12 16:12:37 -06001659static ssize_t amd_iommu_show_cap(struct device *dev,
1660 struct device_attribute *attr,
1661 char *buf)
1662{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001663 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001664 return sprintf(buf, "%x\n", iommu->cap);
1665}
1666static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1667
1668static ssize_t amd_iommu_show_features(struct device *dev,
1669 struct device_attribute *attr,
1670 char *buf)
1671{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001672 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001673 return sprintf(buf, "%llx\n", iommu->features);
1674}
1675static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1676
1677static struct attribute *amd_iommu_attrs[] = {
1678 &dev_attr_cap.attr,
1679 &dev_attr_features.attr,
1680 NULL,
1681};
1682
1683static struct attribute_group amd_iommu_group = {
1684 .name = "amd-iommu",
1685 .attrs = amd_iommu_attrs,
1686};
1687
1688static const struct attribute_group *amd_iommu_groups[] = {
1689 &amd_iommu_group,
1690 NULL,
1691};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001692
Joerg Roedel23c742d2012-06-12 11:47:34 +02001693static int iommu_init_pci(struct amd_iommu *iommu)
1694{
1695 int cap_ptr = iommu->cap_ptr;
1696 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001697 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001698
Shuah Khanc5081cd2013-02-27 17:07:19 -07001699 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001700 iommu->devid & 0xff);
1701 if (!iommu->dev)
1702 return -ENODEV;
1703
Jiang Liucbbc00b2015-10-09 22:07:31 +08001704 /* Prevent binding other PCI device drivers to IOMMU devices */
1705 iommu->dev->match_driver = false;
1706
Joerg Roedel23c742d2012-06-12 11:47:34 +02001707 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1708 &iommu->cap);
1709 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1710 &range);
1711 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1712 &misc);
1713
Joerg Roedel23c742d2012-06-12 11:47:34 +02001714 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1715 amd_iommu_iotlb_sup = false;
1716
1717 /* read extended feature bits */
1718 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1719 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1720
1721 iommu->features = ((u64)high << 32) | low;
1722
1723 if (iommu_feature(iommu, FEATURE_GT)) {
1724 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001725 u32 max_pasid;
1726 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001727
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001728 pasmax = iommu->features & FEATURE_PASID_MASK;
1729 pasmax >>= FEATURE_PASID_SHIFT;
1730 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001731
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001732 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1733
1734 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001735
1736 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1737 glxval >>= FEATURE_GLXVAL_SHIFT;
1738
1739 if (amd_iommu_max_glx_val == -1)
1740 amd_iommu_max_glx_val = glxval;
1741 else
1742 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1743 }
1744
1745 if (iommu_feature(iommu, FEATURE_GT) &&
1746 iommu_feature(iommu, FEATURE_PPR)) {
1747 iommu->is_iommu_v2 = true;
1748 amd_iommu_v2_present = true;
1749 }
1750
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001751 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1752 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001753
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001754 ret = iommu_init_ga(iommu);
1755 if (ret)
1756 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001757
Joerg Roedel23c742d2012-06-12 11:47:34 +02001758 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1759 amd_iommu_np_cache = true;
1760
Steven L Kinney30861dd2013-06-05 16:11:48 -05001761 init_iommu_perf_ctr(iommu);
1762
Joerg Roedel23c742d2012-06-12 11:47:34 +02001763 if (is_rd890_iommu(iommu->dev)) {
1764 int i, j;
1765
1766 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1767 PCI_DEVFN(0, 0));
1768
1769 /*
1770 * Some rd890 systems may not be fully reconfigured by the
1771 * BIOS, so it's necessary for us to store this information so
1772 * it can be reprogrammed on resume
1773 */
1774 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1775 &iommu->stored_addr_lo);
1776 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1777 &iommu->stored_addr_hi);
1778
1779 /* Low bit locks writes to configuration space */
1780 iommu->stored_addr_lo &= ~1;
1781
1782 for (i = 0; i < 6; i++)
1783 for (j = 0; j < 0x12; j++)
1784 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1785
1786 for (i = 0; i < 0x83; i++)
1787 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1788 }
1789
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001790 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001791 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001792
Joerg Roedel39ab9552017-02-01 16:56:46 +01001793 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1794 amd_iommu_groups, "ivhd%d", iommu->index);
Joerg Roedelb0119e82017-02-01 13:23:08 +01001795 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1796 iommu_device_register(&iommu->iommu);
Alex Williamson066f2e92014-06-12 16:12:37 -06001797
Joerg Roedel23c742d2012-06-12 11:47:34 +02001798 return pci_enable_device(iommu->dev);
1799}
1800
Joerg Roedel4d121c32012-06-14 12:21:55 +02001801static void print_iommu_info(void)
1802{
1803 static const char * const feat_str[] = {
1804 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1805 "IA", "GA", "HE", "PC"
1806 };
1807 struct amd_iommu *iommu;
1808
1809 for_each_iommu(iommu) {
1810 int i;
1811
1812 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1813 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1814
1815 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001816 pr_info("AMD-Vi: Extended features (%#llx):\n",
1817 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001818 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001819 if (iommu_feature(iommu, (1ULL << i)))
1820 pr_cont(" %s", feat_str[i]);
1821 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001822
1823 if (iommu->features & FEATURE_GAM_VAPIC)
1824 pr_cont(" GA_vAPIC");
1825
Steven L Kinney30861dd2013-06-05 16:11:48 -05001826 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001827 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001828 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001829 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001830 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001831 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1832 pr_info("AMD-Vi: virtual APIC enabled\n");
1833 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001834}
1835
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001836static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001837{
1838 struct amd_iommu *iommu;
1839 int ret = 0;
1840
1841 for_each_iommu(iommu) {
1842 ret = iommu_init_pci(iommu);
1843 if (ret)
1844 break;
1845 }
1846
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001847 /*
1848 * Order is important here to make sure any unity map requirements are
1849 * fulfilled. The unity mappings are created and written to the device
1850 * table during the amd_iommu_init_api() call.
1851 *
1852 * After that we call init_device_table_dma() to make sure any
1853 * uninitialized DTE will block DMA, and in the end we flush the caches
1854 * of all IOMMUs to make sure the changes to the device table are
1855 * active.
1856 */
1857 ret = amd_iommu_init_api();
1858
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001859 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001860
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001861 for_each_iommu(iommu)
1862 iommu_flush_all_caches(iommu);
1863
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001864 if (!ret)
1865 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001866
Joerg Roedel23c742d2012-06-12 11:47:34 +02001867 return ret;
1868}
1869
Joerg Roedelb65233a2008-07-11 17:14:21 +02001870/****************************************************************************
1871 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001872 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001873 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001874 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1875 * pci_dev.
1876 *
1877 ****************************************************************************/
1878
Joerg Roedel9f800de2009-11-23 12:45:25 +01001879static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001880{
1881 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001882
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001883 r = pci_enable_msi(iommu->dev);
1884 if (r)
1885 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001886
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001887 r = request_threaded_irq(iommu->dev->irq,
1888 amd_iommu_int_handler,
1889 amd_iommu_int_thread,
1890 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001891 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001892
1893 if (r) {
1894 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001895 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001896 }
1897
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001898 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001899
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001900 return 0;
1901}
1902
Joerg Roedel05f92db2009-05-12 09:52:46 +02001903static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001904{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001905 int ret;
1906
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001907 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001908 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001909
Yijing Wang82fcfc62013-08-08 21:12:36 +08001910 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001911 ret = iommu_setup_msi(iommu);
1912 else
1913 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001914
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001915 if (ret)
1916 return ret;
1917
1918enable_faults:
1919 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1920
1921 if (iommu->ppr_log != NULL)
1922 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1923
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001924 iommu_ga_log_enable(iommu);
1925
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001926 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001927}
1928
1929/****************************************************************************
1930 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001931 * The next functions belong to the third pass of parsing the ACPI
1932 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001933 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001934 *
1935 ****************************************************************************/
1936
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001937static void __init free_unity_maps(void)
1938{
1939 struct unity_map_entry *entry, *next;
1940
1941 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1942 list_del(&entry->list);
1943 kfree(entry);
1944 }
1945}
1946
Joerg Roedelb65233a2008-07-11 17:14:21 +02001947/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001948static int __init init_exclusion_range(struct ivmd_header *m)
1949{
1950 int i;
1951
1952 switch (m->type) {
1953 case ACPI_IVMD_TYPE:
1954 set_device_exclusion_range(m->devid, m);
1955 break;
1956 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001957 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001958 set_device_exclusion_range(i, m);
1959 break;
1960 case ACPI_IVMD_TYPE_RANGE:
1961 for (i = m->devid; i <= m->aux; ++i)
1962 set_device_exclusion_range(i, m);
1963 break;
1964 default:
1965 break;
1966 }
1967
1968 return 0;
1969}
1970
Joerg Roedelb65233a2008-07-11 17:14:21 +02001971/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001972static int __init init_unity_map_range(struct ivmd_header *m)
1973{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001974 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001975 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001976
1977 e = kzalloc(sizeof(*e), GFP_KERNEL);
1978 if (e == NULL)
1979 return -ENOMEM;
1980
1981 switch (m->type) {
1982 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001983 kfree(e);
1984 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001985 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001986 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001987 e->devid_start = e->devid_end = m->devid;
1988 break;
1989 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001990 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001991 e->devid_start = 0;
1992 e->devid_end = amd_iommu_last_bdf;
1993 break;
1994 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001995 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001996 e->devid_start = m->devid;
1997 e->devid_end = m->aux;
1998 break;
1999 }
2000 e->address_start = PAGE_ALIGN(m->range_start);
2001 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2002 e->prot = m->flags >> 1;
2003
Joerg Roedel02acc432009-05-20 16:24:21 +02002004 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2005 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07002006 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2007 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02002008 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2009 e->address_start, e->address_end, m->flags);
2010
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002011 list_add_tail(&e->list, &amd_iommu_unity_map);
2012
2013 return 0;
2014}
2015
Joerg Roedelb65233a2008-07-11 17:14:21 +02002016/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002017static int __init init_memory_definitions(struct acpi_table_header *table)
2018{
2019 u8 *p = (u8 *)table, *end = (u8 *)table;
2020 struct ivmd_header *m;
2021
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002022 end += table->length;
2023 p += IVRS_HEADER_LENGTH;
2024
2025 while (p < end) {
2026 m = (struct ivmd_header *)p;
2027 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2028 init_exclusion_range(m);
2029 else if (m->flags & IVMD_FLAG_UNITY_MAP)
2030 init_unity_map_range(m);
2031
2032 p += m->length;
2033 }
2034
2035 return 0;
2036}
2037
Joerg Roedelb65233a2008-07-11 17:14:21 +02002038/*
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002039 * Init the device table to not allow DMA access for devices
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002040 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02002041static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002042{
Joerg Roedel0de66d52011-06-06 16:04:02 +02002043 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002044
2045 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2046 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2047 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002048 }
2049}
2050
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002051static void __init uninit_device_table_dma(void)
2052{
2053 u32 devid;
2054
2055 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2056 amd_iommu_dev_table[devid].data[0] = 0ULL;
2057 amd_iommu_dev_table[devid].data[1] = 0ULL;
2058 }
2059}
2060
Joerg Roedel33f28c52012-06-15 18:03:31 +02002061static void init_device_table(void)
2062{
2063 u32 devid;
2064
2065 if (!amd_iommu_irq_remap)
2066 return;
2067
2068 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2069 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2070}
2071
Joerg Roedele9bf5192010-09-20 14:33:07 +02002072static void iommu_init_flags(struct amd_iommu *iommu)
2073{
2074 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2075 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2076 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2077
2078 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2079 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2080 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2081
2082 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2083 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2084 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2085
2086 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2087 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2088 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2089
2090 /*
2091 * make IOMMU memory accesses cache coherent
2092 */
2093 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01002094
2095 /* Set IOTLB invalidation timeout to 1s */
2096 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002097}
2098
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002099static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02002100{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002101 int i, j;
2102 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002103 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002104
2105 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002106 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002107 return;
2108
2109 /*
2110 * First, we need to ensure that the iommu is enabled. This is
2111 * controlled by a register in the northbridge
2112 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002113
2114 /* Select Northbridge indirect register 0x75 and enable writing */
2115 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2116 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2117
2118 /* Enable the iommu */
2119 if (!(ioc_feature_control & 0x1))
2120 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2121
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002122 /* Restore the iommu BAR */
2123 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2124 iommu->stored_addr_lo);
2125 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2126 iommu->stored_addr_hi);
2127
2128 /* Restore the l1 indirect regs for each of the 6 l1s */
2129 for (i = 0; i < 6; i++)
2130 for (j = 0; j < 0x12; j++)
2131 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2132
2133 /* Restore the l2 indirect regs */
2134 for (i = 0; i < 0x83; i++)
2135 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2136
2137 /* Lock PCI setup registers */
2138 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2139 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02002140}
2141
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002142static void iommu_enable_ga(struct amd_iommu *iommu)
2143{
2144#ifdef CONFIG_IRQ_REMAP
2145 switch (amd_iommu_guest_ir) {
2146 case AMD_IOMMU_GUEST_IR_VAPIC:
2147 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2148 /* Fall through */
2149 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2150 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002151 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002152 break;
2153 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002154 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002155 break;
2156 }
2157#endif
2158}
2159
Baoquan He78d313c2017-08-09 16:33:34 +08002160static void early_enable_iommu(struct amd_iommu *iommu)
2161{
2162 iommu_disable(iommu);
2163 iommu_init_flags(iommu);
2164 iommu_set_device_table(iommu);
2165 iommu_enable_command_buffer(iommu);
2166 iommu_enable_event_buffer(iommu);
2167 iommu_set_exclusion_range(iommu);
2168 iommu_enable_ga(iommu);
2169 iommu_enable(iommu);
2170 iommu_flush_all_caches(iommu);
2171}
2172
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002173/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002174 * This function finally enables all IOMMUs found in the system after
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002175 * they have been initialized.
2176 *
2177 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2178 * the old content of device table entries. Not this case or copy failed,
2179 * just continue as normal kernel does.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002180 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002181static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002182{
2183 struct amd_iommu *iommu;
2184
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002185
2186 if (!copy_device_table()) {
2187 /*
2188 * If come here because of failure in copying device table from old
2189 * kernel with all IOMMUs enabled, print error message and try to
2190 * free allocated old_dev_tbl_cpy.
2191 */
2192 if (amd_iommu_pre_enabled)
2193 pr_err("Failed to copy DEV table from previous kernel.\n");
2194 if (old_dev_tbl_cpy != NULL)
2195 free_pages((unsigned long)old_dev_tbl_cpy,
2196 get_order(dev_table_size));
2197
2198 for_each_iommu(iommu) {
2199 clear_translation_pre_enabled(iommu);
2200 early_enable_iommu(iommu);
2201 }
2202 } else {
2203 pr_info("Copied DEV table from previous kernel.\n");
2204 free_pages((unsigned long)amd_iommu_dev_table,
2205 get_order(dev_table_size));
2206 amd_iommu_dev_table = old_dev_tbl_cpy;
2207 for_each_iommu(iommu) {
2208 iommu_disable_command_buffer(iommu);
2209 iommu_disable_event_buffer(iommu);
2210 iommu_enable_command_buffer(iommu);
2211 iommu_enable_event_buffer(iommu);
2212 iommu_enable_ga(iommu);
2213 iommu_set_device_table(iommu);
2214 iommu_flush_all_caches(iommu);
2215 }
2216 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002217
2218#ifdef CONFIG_IRQ_REMAP
2219 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2220 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2221#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002222}
2223
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002224static void enable_iommus_v2(void)
2225{
2226 struct amd_iommu *iommu;
2227
2228 for_each_iommu(iommu) {
2229 iommu_enable_ppr_log(iommu);
2230 iommu_enable_gt(iommu);
2231 }
2232}
2233
2234static void enable_iommus(void)
2235{
2236 early_enable_iommus();
2237
2238 enable_iommus_v2();
2239}
2240
Joerg Roedel92ac4322009-05-19 19:06:27 +02002241static void disable_iommus(void)
2242{
2243 struct amd_iommu *iommu;
2244
2245 for_each_iommu(iommu)
2246 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002247
2248#ifdef CONFIG_IRQ_REMAP
2249 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2250 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2251#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002252}
2253
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002254/*
2255 * Suspend/Resume support
2256 * disable suspend until real resume implemented
2257 */
2258
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002259static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002260{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002261 struct amd_iommu *iommu;
2262
2263 for_each_iommu(iommu)
2264 iommu_apply_resume_quirks(iommu);
2265
Joerg Roedel736501e2009-05-12 09:56:12 +02002266 /* re-load the hardware */
2267 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002268
2269 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002270}
2271
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002272static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002273{
Joerg Roedel736501e2009-05-12 09:56:12 +02002274 /* disable IOMMUs to go out of the way for BIOS */
2275 disable_iommus();
2276
2277 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002278}
2279
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002280static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002281 .suspend = amd_iommu_suspend,
2282 .resume = amd_iommu_resume,
2283};
2284
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002285static void __init free_iommu_resources(void)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002286{
Lucas Stachebcfa282016-10-26 13:09:53 +02002287 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002288 free_pages((unsigned long)irq_lookup_table,
2289 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002290 irq_lookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002291
Julia Lawalla5919892015-09-13 14:15:31 +02002292 kmem_cache_destroy(amd_iommu_irq_cache);
2293 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002294
2295 free_pages((unsigned long)amd_iommu_rlookup_table,
2296 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002297 amd_iommu_rlookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002298
2299 free_pages((unsigned long)amd_iommu_alias_table,
2300 get_order(alias_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002301 amd_iommu_alias_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002302
2303 free_pages((unsigned long)amd_iommu_dev_table,
2304 get_order(dev_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002305 amd_iommu_dev_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002306
2307 free_iommu_all();
2308
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002309#ifdef CONFIG_GART_IOMMU
2310 /*
2311 * We failed to initialize the AMD IOMMU - try fallback to GART
2312 * if possible.
2313 */
2314 gart_iommu_init();
2315
2316#endif
2317}
2318
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002319/* SB IOAPIC is always on this device in AMD systems */
2320#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2321
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002322static bool __init check_ioapic_information(void)
2323{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002324 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002325 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002326 int idx;
2327
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002328 has_sb_ioapic = false;
2329 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002330
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002331 /*
2332 * If we have map overrides on the kernel command line the
2333 * messages in this function might not describe firmware bugs
2334 * anymore - so be careful
2335 */
2336 if (cmdline_maps)
2337 fw_bug = "";
2338
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002339 for (idx = 0; idx < nr_ioapics; idx++) {
2340 int devid, id = mpc_ioapic_id(idx);
2341
2342 devid = get_ioapic_devid(id);
2343 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002344 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2345 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002346 ret = false;
2347 } else if (devid == IOAPIC_SB_DEVID) {
2348 has_sb_ioapic = true;
2349 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002350 }
2351 }
2352
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002353 if (!has_sb_ioapic) {
2354 /*
2355 * We expect the SB IOAPIC to be listed in the IVRS
2356 * table. The system timer is connected to the SB IOAPIC
2357 * and if we don't have it in the list the system will
2358 * panic at boot time. This situation usually happens
2359 * when the BIOS is buggy and provides us the wrong
2360 * device id for the IOAPIC in the system.
2361 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002362 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002363 }
2364
2365 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002366 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002367
2368 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002369}
2370
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002371static void __init free_dma_resources(void)
2372{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002373 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2374 get_order(MAX_DOMAIN_ID/8));
Joerg Roedelf6019272017-06-16 16:09:58 +02002375 amd_iommu_pd_alloc_bitmap = NULL;
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002376
2377 free_unity_maps();
2378}
2379
Joerg Roedelb65233a2008-07-11 17:14:21 +02002380/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002381 * This is the hardware init function for AMD IOMMU in the system.
2382 * This function is called either from amd_iommu_init or from the interrupt
2383 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002384 *
2385 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002386 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002387 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002388 * 1 pass) Discover the most comprehensive IVHD type to use.
2389 *
2390 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002391 * Upon this information the size of the data structures is
2392 * determined that needs to be allocated.
2393 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002394 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002395 * information in the ACPI table about available AMD IOMMUs
2396 * in the system. It also maps the PCI devices in the
2397 * system to specific IOMMUs
2398 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002399 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002400 * initialized we update them with information about memory
2401 * remapping requirements parsed out of the ACPI table in
2402 * this last pass.
2403 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002404 * After everything is set up the IOMMUs are enabled and the necessary
2405 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002406 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002407static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002408{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002409 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002410 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002411 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002412
Joerg Roedel643511b2012-06-12 12:09:35 +02002413 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002414 return -ENODEV;
2415
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002416 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002417 if (status == AE_NOT_FOUND)
2418 return -ENODEV;
2419 else if (ACPI_FAILURE(status)) {
2420 const char *err = acpi_format_exception(status);
2421 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2422 return -EINVAL;
2423 }
2424
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002425 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002426 * Validate checksum here so we don't need to do it when
2427 * we actually parse the table
2428 */
2429 ret = check_ivrs_checksum(ivrs_base);
2430 if (ret)
Rafael J. Wysocki99e8ccd2017-01-10 14:57:28 +01002431 goto out;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002432
2433 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2434 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2435
2436 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002437 * First parse ACPI tables to find the largest Bus/Dev/Func
2438 * we need to handle. Upon this information the shared data
2439 * structures for the IOMMUs in the system will be allocated
2440 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002441 ret = find_last_devid_acpi(ivrs_base);
2442 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002443 goto out;
2444
Joerg Roedelc5714842008-07-11 17:14:25 +02002445 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2446 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2447 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002448
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002449 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002450 ret = -ENOMEM;
Baoquan Heb3367812017-08-09 16:33:42 +08002451 amd_iommu_dev_table = (void *)__get_free_pages(
2452 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002453 get_order(dev_table_size));
2454 if (amd_iommu_dev_table == NULL)
2455 goto out;
2456
2457 /*
2458 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2459 * IOMMU see for that device
2460 */
2461 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2462 get_order(alias_table_size));
2463 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002464 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002465
2466 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002467 amd_iommu_rlookup_table = (void *)__get_free_pages(
2468 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002469 get_order(rlookup_table_size));
2470 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002471 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002472
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002473 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2474 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002475 get_order(MAX_DOMAIN_ID/8));
2476 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002477 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002478
2479 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002480 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002481 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002482 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002483 amd_iommu_alias_table[i] = i;
2484
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002485 /*
2486 * never allocate domain 0 because its used as the non-allocated and
2487 * error value placeholder
2488 */
Baoquan He5c87f622016-09-15 16:50:51 +08002489 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002490
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002491 spin_lock_init(&amd_iommu_pd_lock);
2492
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002493 /*
2494 * now the data structures are allocated and basically initialized
2495 * start the real acpi table scan
2496 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002497 ret = init_iommu_all(ivrs_base);
2498 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002499 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002500
Joerg Roedel11123742017-06-16 16:09:54 +02002501 /* Disable any previously enabled IOMMUs */
Baoquan He20b46df2017-08-09 16:33:44 +08002502 if (!is_kdump_kernel() || amd_iommu_disabled)
2503 disable_iommus();
Joerg Roedel11123742017-06-16 16:09:54 +02002504
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002505 if (amd_iommu_irq_remap)
2506 amd_iommu_irq_remap = check_ioapic_information();
2507
Joerg Roedel05152a02012-06-15 16:53:51 +02002508 if (amd_iommu_irq_remap) {
2509 /*
2510 * Interrupt remapping enabled, create kmem_cache for the
2511 * remapping tables.
2512 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002513 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002514 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2515 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2516 else
2517 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002518 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002519 remap_cache_sz,
2520 IRQ_TABLE_ALIGNMENT,
2521 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002522 if (!amd_iommu_irq_cache)
2523 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002524
2525 irq_lookup_table = (void *)__get_free_pages(
2526 GFP_KERNEL | __GFP_ZERO,
2527 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002528 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2529 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002530 if (!irq_lookup_table)
2531 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002532 }
2533
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002534 ret = init_memory_definitions(ivrs_base);
2535 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002536 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002537
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002538 /* init the device table */
2539 init_device_table();
2540
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002541out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002542 /* Don't leak any ACPI memory */
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002543 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002544 ivrs_base = NULL;
2545
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002546 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002547}
2548
Gerard Snitselaarae295142012-03-16 11:38:22 -07002549static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002550{
2551 struct amd_iommu *iommu;
2552 int ret = 0;
2553
2554 for_each_iommu(iommu) {
2555 ret = iommu_init_msi(iommu);
2556 if (ret)
2557 goto out;
2558 }
2559
2560out:
2561 return ret;
2562}
2563
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002564static bool detect_ivrs(void)
2565{
2566 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002567 acpi_status status;
2568
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002569 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002570 if (status == AE_NOT_FOUND)
2571 return false;
2572 else if (ACPI_FAILURE(status)) {
2573 const char *err = acpi_format_exception(status);
2574 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2575 return false;
2576 }
2577
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002578 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002579
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002580 /* Make sure ACS will be enabled during PCI probe */
2581 pci_request_acs();
2582
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002583 return true;
2584}
2585
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002586/****************************************************************************
2587 *
2588 * AMD IOMMU Initialization State Machine
2589 *
2590 ****************************************************************************/
2591
2592static int __init state_next(void)
2593{
2594 int ret = 0;
2595
2596 switch (init_state) {
2597 case IOMMU_START_STATE:
2598 if (!detect_ivrs()) {
2599 init_state = IOMMU_NOT_FOUND;
2600 ret = -ENODEV;
2601 } else {
2602 init_state = IOMMU_IVRS_DETECTED;
2603 }
2604 break;
2605 case IOMMU_IVRS_DETECTED:
2606 ret = early_amd_iommu_init();
2607 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
Joerg Roedel7ad820e2017-06-16 16:09:59 +02002608 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2609 pr_info("AMD-Vi: AMD IOMMU disabled on kernel command-line\n");
2610 free_dma_resources();
2611 free_iommu_resources();
2612 init_state = IOMMU_CMDLINE_DISABLED;
2613 ret = -EINVAL;
2614 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002615 break;
2616 case IOMMU_ACPI_FINISHED:
2617 early_enable_iommus();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002618 x86_platform.iommu_shutdown = disable_iommus;
2619 init_state = IOMMU_ENABLED;
2620 break;
2621 case IOMMU_ENABLED:
Joerg Roedel74ddda72017-07-26 14:17:55 +02002622 register_syscore_ops(&amd_iommu_syscore_ops);
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002623 ret = amd_iommu_init_pci();
2624 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2625 enable_iommus_v2();
2626 break;
2627 case IOMMU_PCI_INIT:
2628 ret = amd_iommu_enable_interrupts();
2629 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2630 break;
2631 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002632 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002633 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2634 break;
2635 case IOMMU_DMA_OPS:
2636 init_state = IOMMU_INITIALIZED;
2637 break;
2638 case IOMMU_INITIALIZED:
2639 /* Nothing to do */
2640 break;
2641 case IOMMU_NOT_FOUND:
2642 case IOMMU_INIT_ERROR:
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002643 case IOMMU_CMDLINE_DISABLED:
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002644 /* Error states => do nothing */
2645 ret = -EINVAL;
2646 break;
2647 default:
2648 /* Unknown state */
2649 BUG();
2650 }
2651
2652 return ret;
2653}
2654
2655static int __init iommu_go_to_state(enum iommu_init_state state)
2656{
Joerg Roedel151b0902017-06-16 16:09:57 +02002657 int ret = -EINVAL;
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002658
2659 while (init_state != state) {
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002660 if (init_state == IOMMU_NOT_FOUND ||
2661 init_state == IOMMU_INIT_ERROR ||
2662 init_state == IOMMU_CMDLINE_DISABLED)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002663 break;
Joerg Roedel151b0902017-06-16 16:09:57 +02002664 ret = state_next();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002665 }
2666
2667 return ret;
2668}
2669
Joerg Roedel6b474b82012-06-26 16:46:04 +02002670#ifdef CONFIG_IRQ_REMAP
2671int __init amd_iommu_prepare(void)
2672{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002673 int ret;
2674
Jiang Liu7fa1c842015-01-07 15:31:42 +08002675 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002676
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002677 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2678 if (ret)
2679 return ret;
2680 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002681}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002682
Joerg Roedel6b474b82012-06-26 16:46:04 +02002683int __init amd_iommu_enable(void)
2684{
2685 int ret;
2686
2687 ret = iommu_go_to_state(IOMMU_ENABLED);
2688 if (ret)
2689 return ret;
2690
2691 irq_remapping_enabled = 1;
2692
2693 return 0;
2694}
2695
2696void amd_iommu_disable(void)
2697{
2698 amd_iommu_suspend();
2699}
2700
2701int amd_iommu_reenable(int mode)
2702{
2703 amd_iommu_resume();
2704
2705 return 0;
2706}
2707
2708int __init amd_iommu_enable_faulting(void)
2709{
2710 /* We enable MSI later when PCI is initialized */
2711 return 0;
2712}
2713#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002714
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002715/*
2716 * This is the core init function for AMD IOMMU hardware in the system.
2717 * This function is called from the generic x86 DMA layer initialization
2718 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002719 */
2720static int __init amd_iommu_init(void)
2721{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002722 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002723
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002724 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2725 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002726 free_dma_resources();
2727 if (!irq_remapping_enabled) {
2728 disable_iommus();
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002729 free_iommu_resources();
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002730 } else {
2731 struct amd_iommu *iommu;
2732
2733 uninit_device_table_dma();
2734 for_each_iommu(iommu)
2735 iommu_flush_all_caches(iommu);
2736 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002737 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002738
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002739 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002740}
2741
Joerg Roedelb65233a2008-07-11 17:14:21 +02002742/****************************************************************************
2743 *
2744 * Early detect code. This code runs at IOMMU detection time in the DMA
2745 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2746 * IOMMUs
2747 *
2748 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002749int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002750{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002751 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002752
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002753 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002754 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002755
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002756 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2757 if (ret)
2758 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002759
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002760 amd_iommu_detected = true;
2761 iommu_detected = 1;
2762 x86_init.iommu.iommu_init = amd_iommu_init;
2763
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002764 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002765}
2766
Joerg Roedelb65233a2008-07-11 17:14:21 +02002767/****************************************************************************
2768 *
2769 * Parsing functions for the AMD IOMMU specific kernel command line
2770 * options.
2771 *
2772 ****************************************************************************/
2773
Joerg Roedelfefda112009-05-20 12:21:42 +02002774static int __init parse_amd_iommu_dump(char *str)
2775{
2776 amd_iommu_dump = true;
2777
2778 return 1;
2779}
2780
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002781static int __init parse_amd_iommu_intr(char *str)
2782{
2783 for (; *str; ++str) {
2784 if (strncmp(str, "legacy", 6) == 0) {
2785 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2786 break;
2787 }
2788 if (strncmp(str, "vapic", 5) == 0) {
2789 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2790 break;
2791 }
2792 }
2793 return 1;
2794}
2795
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002796static int __init parse_amd_iommu_options(char *str)
2797{
2798 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002799 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002800 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002801 if (strncmp(str, "off", 3) == 0)
2802 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002803 if (strncmp(str, "force_isolation", 15) == 0)
2804 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002805 }
2806
2807 return 1;
2808}
2809
Joerg Roedel440e89982013-04-09 16:35:28 +02002810static int __init parse_ivrs_ioapic(char *str)
2811{
2812 unsigned int bus, dev, fn;
2813 int ret, id, i;
2814 u16 devid;
2815
2816 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2817
2818 if (ret != 4) {
2819 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2820 return 1;
2821 }
2822
2823 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2824 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2825 str);
2826 return 1;
2827 }
2828
2829 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2830
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002831 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002832 i = early_ioapic_map_size++;
2833 early_ioapic_map[i].id = id;
2834 early_ioapic_map[i].devid = devid;
2835 early_ioapic_map[i].cmd_line = true;
2836
2837 return 1;
2838}
2839
2840static int __init parse_ivrs_hpet(char *str)
2841{
2842 unsigned int bus, dev, fn;
2843 int ret, id, i;
2844 u16 devid;
2845
2846 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2847
2848 if (ret != 4) {
2849 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2850 return 1;
2851 }
2852
2853 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2854 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2855 str);
2856 return 1;
2857 }
2858
2859 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2860
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002861 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002862 i = early_hpet_map_size++;
2863 early_hpet_map[i].id = id;
2864 early_hpet_map[i].devid = devid;
2865 early_hpet_map[i].cmd_line = true;
2866
2867 return 1;
2868}
2869
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002870static int __init parse_ivrs_acpihid(char *str)
2871{
2872 u32 bus, dev, fn;
2873 char *hid, *uid, *p;
2874 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2875 int ret, i;
2876
2877 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2878 if (ret != 4) {
2879 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2880 return 1;
2881 }
2882
2883 p = acpiid;
2884 hid = strsep(&p, ":");
2885 uid = p;
2886
2887 if (!hid || !(*hid) || !uid) {
2888 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2889 return 1;
2890 }
2891
2892 i = early_acpihid_map_size++;
2893 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2894 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2895 early_acpihid_map[i].devid =
2896 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2897 early_acpihid_map[i].cmd_line = true;
2898
2899 return 1;
2900}
2901
Joerg Roedel440e89982013-04-09 16:35:28 +02002902__setup("amd_iommu_dump", parse_amd_iommu_dump);
2903__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002904__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002905__setup("ivrs_ioapic", parse_ivrs_ioapic);
2906__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002907__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002908
2909IOMMU_INIT_FINISH(amd_iommu_detect,
2910 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002911 NULL,
2912 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002913
2914bool amd_iommu_v2_supported(void)
2915{
2916 return amd_iommu_v2_present;
2917}
2918EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002919
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002920struct amd_iommu *get_amd_iommu(unsigned int idx)
2921{
2922 unsigned int i = 0;
2923 struct amd_iommu *iommu;
2924
2925 for_each_iommu(iommu)
2926 if (i++ == idx)
2927 return iommu;
2928 return NULL;
2929}
2930EXPORT_SYMBOL(get_amd_iommu);
2931
Steven L Kinney30861dd2013-06-05 16:11:48 -05002932/****************************************************************************
2933 *
2934 * IOMMU EFR Performance Counter support functionality. This code allows
2935 * access to the IOMMU PC functionality.
2936 *
2937 ****************************************************************************/
2938
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002939u8 amd_iommu_pc_get_max_banks(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002940{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002941 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002942
Steven L Kinney30861dd2013-06-05 16:11:48 -05002943 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002944 return iommu->max_banks;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002945
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002946 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002947}
2948EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2949
2950bool amd_iommu_pc_supported(void)
2951{
2952 return amd_iommu_pc_present;
2953}
2954EXPORT_SYMBOL(amd_iommu_pc_supported);
2955
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002956u8 amd_iommu_pc_get_max_counters(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002957{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002958 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002959
Steven L Kinney30861dd2013-06-05 16:11:48 -05002960 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002961 return iommu->max_counters;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002962
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002963 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002964}
2965EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2966
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002967static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
2968 u8 fxn, u64 *value, bool is_write)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002969{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002970 u32 offset;
2971 u32 max_offset_lim;
2972
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002973 /* Make sure the IOMMU PC resource is available */
2974 if (!amd_iommu_pc_present)
2975 return -ENODEV;
2976
Steven L Kinney30861dd2013-06-05 16:11:48 -05002977 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002978 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002979 return -ENODEV;
2980
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002981 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002982
2983 /* Limit the offset to the hw defined mmio region aperture */
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002984 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
Steven L Kinney30861dd2013-06-05 16:11:48 -05002985 (iommu->max_counters << 8) | 0x28);
2986 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2987 (offset > max_offset_lim))
2988 return -EINVAL;
2989
2990 if (is_write) {
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002991 u64 val = *value & GENMASK_ULL(47, 0);
2992
2993 writel((u32)val, iommu->mmio_base + offset);
2994 writel((val >> 32), iommu->mmio_base + offset + 4);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002995 } else {
2996 *value = readl(iommu->mmio_base + offset + 4);
2997 *value <<= 32;
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002998 *value |= readl(iommu->mmio_base + offset);
2999 *value &= GENMASK_ULL(47, 0);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003000 }
3001
3002 return 0;
3003}
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003004
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003005int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003006{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003007 if (!iommu)
3008 return -EINVAL;
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003009
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003010 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003011}
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003012EXPORT_SYMBOL(amd_iommu_pc_get_reg);
3013
3014int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3015{
3016 if (!iommu)
3017 return -EINVAL;
3018
3019 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3020}
3021EXPORT_SYMBOL(amd_iommu_pc_set_reg);