blob: 3fa7e3b35507d1f14406c16635a43188f1ec0bfd [file] [log] [blame]
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
21#include <linux/acpi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020022#include <linux/list.h>
Baoquan He5c87f622016-09-15 16:50:51 +080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010025#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020026#include <linux/interrupt.h>
27#include <linux/msi.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020028#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010029#include <linux/export.h>
Alex Williamson066f2e92014-06-12 16:12:37 -060030#include <linux/iommu.h>
Lucas Stachebcfa282016-10-26 13:09:53 +020031#include <linux/kmemleak.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020032#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090033#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010034#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090035#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040036#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020037#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020038#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020039
40#include "amd_iommu_proto.h"
41#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020042#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020043
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020044/*
45 * definitions for the ACPI scanning code
46 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020047#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020048
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040049#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020050#define ACPI_IVMD_TYPE_ALL 0x20
51#define ACPI_IVMD_TYPE 0x21
52#define ACPI_IVMD_TYPE_RANGE 0x22
53
54#define IVHD_DEV_ALL 0x01
55#define IVHD_DEV_SELECT 0x02
56#define IVHD_DEV_SELECT_RANGE_START 0x03
57#define IVHD_DEV_RANGE_END 0x04
58#define IVHD_DEV_ALIAS 0x42
59#define IVHD_DEV_ALIAS_RANGE 0x43
60#define IVHD_DEV_EXT_SELECT 0x46
61#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020062#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040063#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020064
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040065#define UID_NOT_PRESENT 0
66#define UID_IS_INTEGER 1
67#define UID_IS_CHARACTER 2
68
Joerg Roedel6efed632012-06-14 15:52:58 +020069#define IVHD_SPECIAL_IOAPIC 1
70#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020071
Joerg Roedel6da73422009-05-04 11:44:38 +020072#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
73#define IVHD_FLAG_PASSPW_EN_MASK 0x02
74#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
75#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020076
77#define IVMD_FLAG_EXCL_RANGE 0x08
78#define IVMD_FLAG_UNITY_MAP 0x01
79
80#define ACPI_DEVFLAG_INITPASS 0x01
81#define ACPI_DEVFLAG_EXTINT 0x02
82#define ACPI_DEVFLAG_NMI 0x04
83#define ACPI_DEVFLAG_SYSMGT1 0x10
84#define ACPI_DEVFLAG_SYSMGT2 0x20
85#define ACPI_DEVFLAG_LINT0 0x40
86#define ACPI_DEVFLAG_LINT1 0x80
87#define ACPI_DEVFLAG_ATSDIS 0x10000000
88
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050089#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020090/*
91 * ACPI table definitions
92 *
93 * These data structures are laid over the table to parse the important values
94 * out of it.
95 */
96
Joerg Roedelb0119e82017-02-01 13:23:08 +010097extern const struct iommu_ops amd_iommu_ops;
98
Joerg Roedelb65233a2008-07-11 17:14:21 +020099/*
100 * structure describing one IOMMU in the ACPI table. Typically followed by one
101 * or more ivhd_entrys.
102 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200103struct ivhd_header {
104 u8 type;
105 u8 flags;
106 u16 length;
107 u16 devid;
108 u16 cap_ptr;
109 u64 mmio_phys;
110 u16 pci_seg;
111 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400112 u32 efr_attr;
113
114 /* Following only valid on IVHD type 11h and 40h */
115 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
116 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200117} __attribute__((packed));
118
Joerg Roedelb65233a2008-07-11 17:14:21 +0200119/*
120 * A device entry describing which devices a specific IOMMU translates and
121 * which requestor ids they use.
122 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200123struct ivhd_entry {
124 u8 type;
125 u16 devid;
126 u8 flags;
127 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400128 u32 hidh;
129 u64 cid;
130 u8 uidf;
131 u8 uidl;
132 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200133} __attribute__((packed));
134
Joerg Roedelb65233a2008-07-11 17:14:21 +0200135/*
136 * An AMD IOMMU memory definition structure. It defines things like exclusion
137 * ranges for devices and regions that should be unity mapped.
138 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200139struct ivmd_header {
140 u8 type;
141 u8 flags;
142 u16 length;
143 u16 devid;
144 u16 aux;
145 u64 resv;
146 u64 range_start;
147 u64 range_length;
148} __attribute__((packed));
149
Joerg Roedelfefda112009-05-20 12:21:42 +0200150bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200151bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200152
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500153int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500154
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200155static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200156static bool __initdata amd_iommu_disabled;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400157static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200158
Joerg Roedelb65233a2008-07-11 17:14:21 +0200159u16 amd_iommu_last_bdf; /* largest PCI device id we have
160 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200161LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200162 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700163bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200164
Joerg Roedel2e228472008-07-11 17:14:31 +0200165LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200166 system */
167
Joerg Roedelbb527772009-11-20 14:31:51 +0100168/* Array to assign indices to IOMMUs*/
169struct amd_iommu *amd_iommus[MAX_IOMMUS];
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600170
171/* Number of IOMMUs present in the system */
172static int amd_iommus_present;
Joerg Roedelbb527772009-11-20 14:31:51 +0100173
Joerg Roedel318afd42009-11-23 18:32:38 +0100174/* IOMMUs have a non-present cache? */
175bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200176bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100177
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600178u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100179
Joerg Roedel400a28a2011-11-28 15:11:02 +0100180bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200181static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100182
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100183bool amd_iommu_force_isolation __read_mostly;
184
Joerg Roedelb65233a2008-07-11 17:14:21 +0200185/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100186 * List of protection domains - used during resume
187 */
188LIST_HEAD(amd_iommu_pd_list);
189spinlock_t amd_iommu_pd_lock;
190
191/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200192 * Pointer to the device table which is shared by all AMD IOMMUs
193 * it is indexed by the PCI device id or the HT unit id and contains
194 * information about the domain the device belongs to as well as the
195 * page table root pointer.
196 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200197struct dev_table_entry *amd_iommu_dev_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200198
199/*
200 * The alias table is a driver specific data structure which contains the
201 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
202 * More than one device can share the same requestor id.
203 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200204u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200205
206/*
207 * The rlookup table is used to find the IOMMU which is responsible
208 * for a specific device. It is also indexed by the PCI device id.
209 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200210struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200211
212/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200213 * This table is used to find the irq remapping table for a given device id
214 * quickly.
215 */
216struct irq_remap_table **irq_lookup_table;
217
218/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200219 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200220 * to know which ones are already in use.
221 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200222unsigned long *amd_iommu_pd_alloc_bitmap;
223
Joerg Roedelb65233a2008-07-11 17:14:21 +0200224static u32 dev_table_size; /* size of the device table */
225static u32 alias_table_size; /* size of the alias table */
226static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200227
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200228enum iommu_init_state {
229 IOMMU_START_STATE,
230 IOMMU_IVRS_DETECTED,
231 IOMMU_ACPI_FINISHED,
232 IOMMU_ENABLED,
233 IOMMU_PCI_INIT,
234 IOMMU_INTERRUPTS_EN,
235 IOMMU_DMA_OPS,
236 IOMMU_INITIALIZED,
237 IOMMU_NOT_FOUND,
238 IOMMU_INIT_ERROR,
239};
240
Joerg Roedel235dacb2013-04-09 17:53:14 +0200241/* Early ioapic and hpet maps from kernel command line */
242#define EARLY_MAP_SIZE 4
243static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
244static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400245static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
246
Joerg Roedel235dacb2013-04-09 17:53:14 +0200247static int __initdata early_ioapic_map_size;
248static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400249static int __initdata early_acpihid_map_size;
250
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200251static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200252
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200253static enum iommu_init_state init_state = IOMMU_START_STATE;
254
Gerard Snitselaarae295142012-03-16 11:38:22 -0700255static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200256static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200257static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100258
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200259static inline void update_last_devid(u16 devid)
260{
261 if (devid > amd_iommu_last_bdf)
262 amd_iommu_last_bdf = devid;
263}
264
Joerg Roedelc5714842008-07-11 17:14:25 +0200265static inline unsigned long tbl_size(int entry_size)
266{
267 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100268 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200269
270 return 1UL << shift;
271}
272
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600273int amd_iommu_get_num_iommus(void)
274{
275 return amd_iommus_present;
276}
277
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400278/* Access to l1 and l2 indexed register spaces */
279
280static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
281{
282 u32 val;
283
284 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
285 pci_read_config_dword(iommu->dev, 0xfc, &val);
286 return val;
287}
288
289static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
290{
291 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
292 pci_write_config_dword(iommu->dev, 0xfc, val);
293 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
294}
295
296static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
297{
298 u32 val;
299
300 pci_write_config_dword(iommu->dev, 0xf0, address);
301 pci_read_config_dword(iommu->dev, 0xf4, &val);
302 return val;
303}
304
305static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
306{
307 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
308 pci_write_config_dword(iommu->dev, 0xf4, val);
309}
310
Joerg Roedelb65233a2008-07-11 17:14:21 +0200311/****************************************************************************
312 *
313 * AMD IOMMU MMIO register space handling functions
314 *
315 * These functions are used to program the IOMMU device registers in
316 * MMIO space required for that driver.
317 *
318 ****************************************************************************/
319
320/*
321 * This function set the exclusion range in the IOMMU. DMA accesses to the
322 * exclusion range are passed through untranslated
323 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200324static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200325{
326 u64 start = iommu->exclusion_start & PAGE_MASK;
327 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
328 u64 entry;
329
330 if (!iommu->exclusion_start)
331 return;
332
333 entry = start | MMIO_EXCL_ENABLE_MASK;
334 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
335 &entry, sizeof(entry));
336
337 entry = limit;
338 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
339 &entry, sizeof(entry));
340}
341
Joerg Roedelb65233a2008-07-11 17:14:21 +0200342/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000343static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200344{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200345 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200346
347 BUG_ON(iommu->mmio_base == NULL);
348
349 entry = virt_to_phys(amd_iommu_dev_table);
350 entry |= (dev_table_size >> 12) - 1;
351 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
352 &entry, sizeof(entry));
353}
354
Joerg Roedelb65233a2008-07-11 17:14:21 +0200355/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200356static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200357{
358 u32 ctrl;
359
360 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
361 ctrl |= (1 << bit);
362 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
363}
364
Joerg Roedelca0207112009-10-28 18:02:26 +0100365static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200366{
367 u32 ctrl;
368
Joerg Roedel199d0d52008-09-17 16:45:59 +0200369 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200370 ctrl &= ~(1 << bit);
371 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
372}
373
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100374static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
375{
376 u32 ctrl;
377
378 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
379 ctrl &= ~CTRL_INV_TO_MASK;
380 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
381 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
382}
383
Joerg Roedelb65233a2008-07-11 17:14:21 +0200384/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200385static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200386{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200387 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200388}
389
Joerg Roedel92ac4322009-05-19 19:06:27 +0200390static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200391{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200392 /* Disable command buffer */
393 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
394
395 /* Disable event logging and event interrupts */
396 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
397 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
398
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500399 /* Disable IOMMU GA_LOG */
400 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
401 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
402
Chris Wrighta8c485b2009-06-15 15:53:45 +0200403 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200404 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200405}
406
Joerg Roedelb65233a2008-07-11 17:14:21 +0200407/*
408 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
409 * the system has one.
410 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500411static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200412{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500413 if (!request_mem_region(address, end, "amd_iommu")) {
414 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
415 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200416 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200417 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200418 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200419
Steven L Kinney30861dd2013-06-05 16:11:48 -0500420 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200421}
422
423static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
424{
425 if (iommu->mmio_base)
426 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500427 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200428}
429
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400430static inline u32 get_ivhd_header_size(struct ivhd_header *h)
431{
432 u32 size = 0;
433
434 switch (h->type) {
435 case 0x10:
436 size = 24;
437 break;
438 case 0x11:
439 case 0x40:
440 size = 40;
441 break;
442 }
443 return size;
444}
445
Joerg Roedelb65233a2008-07-11 17:14:21 +0200446/****************************************************************************
447 *
448 * The functions below belong to the first pass of AMD IOMMU ACPI table
449 * parsing. In this pass we try to find out the highest device id this
450 * code has to handle. Upon this information the size of the shared data
451 * structures is determined later.
452 *
453 ****************************************************************************/
454
455/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200456 * This function calculates the length of a given IVHD entry
457 */
458static inline int ivhd_entry_length(u8 *ivhd)
459{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400460 u32 type = ((struct ivhd_entry *)ivhd)->type;
461
462 if (type < 0x80) {
463 return 0x04 << (*ivhd >> 6);
464 } else if (type == IVHD_DEV_ACPI_HID) {
465 /* For ACPI_HID, offset 21 is uid len */
466 return *((u8 *)ivhd + 21) + 22;
467 }
468 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200469}
470
471/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200472 * After reading the highest device id from the IOMMU PCI capability header
473 * this function looks if there is a higher device id defined in the ACPI table
474 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200475static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
476{
477 u8 *p = (void *)h, *end = (void *)h;
478 struct ivhd_entry *dev;
479
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400480 u32 ivhd_size = get_ivhd_header_size(h);
481
482 if (!ivhd_size) {
483 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
484 return -EINVAL;
485 }
486
487 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200488 end += h->length;
489
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200490 while (p < end) {
491 dev = (struct ivhd_entry *)p;
492 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200493 case IVHD_DEV_ALL:
494 /* Use maximum BDF value for DEV_ALL */
495 update_last_devid(0xffff);
496 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200497 case IVHD_DEV_SELECT:
498 case IVHD_DEV_RANGE_END:
499 case IVHD_DEV_ALIAS:
500 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200501 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200502 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200503 break;
504 default:
505 break;
506 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200507 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200508 }
509
510 WARN_ON(p != end);
511
512 return 0;
513}
514
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400515static int __init check_ivrs_checksum(struct acpi_table_header *table)
516{
517 int i;
518 u8 checksum = 0, *p = (u8 *)table;
519
520 for (i = 0; i < table->length; ++i)
521 checksum += p[i];
522 if (checksum != 0) {
523 /* ACPI table corrupt */
524 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
525 return -ENODEV;
526 }
527
528 return 0;
529}
530
Joerg Roedelb65233a2008-07-11 17:14:21 +0200531/*
532 * Iterate over all IVHD entries in the ACPI table and find the highest device
533 * id which we need to handle. This is the first of three functions which parse
534 * the ACPI table. So we check the checksum here.
535 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200536static int __init find_last_devid_acpi(struct acpi_table_header *table)
537{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400538 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200539 struct ivhd_header *h;
540
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200541 p += IVRS_HEADER_LENGTH;
542
543 end += table->length;
544 while (p < end) {
545 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400546 if (h->type == amd_iommu_target_ivhd_type) {
547 int ret = find_last_devid_from_ivhd(h);
548
549 if (ret)
550 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200551 }
552 p += h->length;
553 }
554 WARN_ON(p != end);
555
556 return 0;
557}
558
Joerg Roedelb65233a2008-07-11 17:14:21 +0200559/****************************************************************************
560 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200561 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200562 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
563 * data structures, initialize the device/alias/rlookup table and also
564 * basically initialize the hardware.
565 *
566 ****************************************************************************/
567
568/*
569 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
570 * write commands to that buffer later and the IOMMU will execute them
571 * asynchronously
572 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200573static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200574{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200575 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
576 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200577
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200578 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200579}
580
581/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200582 * This function resets the command buffer if the IOMMU stopped fetching
583 * commands from it.
584 */
585void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
586{
587 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
588
589 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
590 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Tom Lendackyd334a562017-06-05 14:52:12 -0500591 iommu->cmd_buf_head = 0;
592 iommu->cmd_buf_tail = 0;
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200593
594 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
595}
596
597/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200598 * This function writes the command buffer address to the hardware and
599 * enables it.
600 */
601static void iommu_enable_command_buffer(struct amd_iommu *iommu)
602{
603 u64 entry;
604
605 BUG_ON(iommu->cmd_buf == NULL);
606
607 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200608 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200609
Joerg Roedelb36ca912008-06-26 21:27:45 +0200610 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200611 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200612
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200613 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200614}
615
616static void __init free_command_buffer(struct amd_iommu *iommu)
617{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200618 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200619}
620
Joerg Roedel335503e2008-09-05 14:29:07 +0200621/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200622static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200623{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200624 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
625 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200626
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200627 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200628}
629
630static void iommu_enable_event_buffer(struct amd_iommu *iommu)
631{
632 u64 entry;
633
634 BUG_ON(iommu->evt_buf == NULL);
635
Joerg Roedel335503e2008-09-05 14:29:07 +0200636 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200637
Joerg Roedel335503e2008-09-05 14:29:07 +0200638 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
639 &entry, sizeof(entry));
640
Joerg Roedel090672072009-06-15 16:06:48 +0200641 /* set head and tail to zero manually */
642 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
643 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
644
Joerg Roedel58492e12009-05-04 18:41:16 +0200645 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200646}
647
648static void __init free_event_buffer(struct amd_iommu *iommu)
649{
650 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
651}
652
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100653/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200654static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100655{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200656 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
657 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100658
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200659 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100660}
661
662static void iommu_enable_ppr_log(struct amd_iommu *iommu)
663{
664 u64 entry;
665
666 if (iommu->ppr_log == NULL)
667 return;
668
669 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
670
671 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
672 &entry, sizeof(entry));
673
674 /* set head and tail to zero manually */
675 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
676 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
677
678 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
679 iommu_feature_enable(iommu, CONTROL_PPR_EN);
680}
681
682static void __init free_ppr_log(struct amd_iommu *iommu)
683{
684 if (iommu->ppr_log == NULL)
685 return;
686
687 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
688}
689
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500690static void free_ga_log(struct amd_iommu *iommu)
691{
692#ifdef CONFIG_IRQ_REMAP
693 if (iommu->ga_log)
694 free_pages((unsigned long)iommu->ga_log,
695 get_order(GA_LOG_SIZE));
696 if (iommu->ga_log_tail)
697 free_pages((unsigned long)iommu->ga_log_tail,
698 get_order(8));
699#endif
700}
701
702static int iommu_ga_log_enable(struct amd_iommu *iommu)
703{
704#ifdef CONFIG_IRQ_REMAP
705 u32 status, i;
706
707 if (!iommu->ga_log)
708 return -EINVAL;
709
710 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
711
712 /* Check if already running */
713 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
714 return 0;
715
716 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
717 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
718
719 for (i = 0; i < LOOP_TIMEOUT; ++i) {
720 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
721 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
722 break;
723 }
724
725 if (i >= LOOP_TIMEOUT)
726 return -EINVAL;
727#endif /* CONFIG_IRQ_REMAP */
728 return 0;
729}
730
731#ifdef CONFIG_IRQ_REMAP
732static int iommu_init_ga_log(struct amd_iommu *iommu)
733{
734 u64 entry;
735
736 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
737 return 0;
738
739 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
740 get_order(GA_LOG_SIZE));
741 if (!iommu->ga_log)
742 goto err_out;
743
744 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
745 get_order(8));
746 if (!iommu->ga_log_tail)
747 goto err_out;
748
749 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
750 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
751 &entry, sizeof(entry));
752 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
753 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
754 &entry, sizeof(entry));
755 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
756 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
757
758 return 0;
759err_out:
760 free_ga_log(iommu);
761 return -EINVAL;
762}
763#endif /* CONFIG_IRQ_REMAP */
764
765static int iommu_init_ga(struct amd_iommu *iommu)
766{
767 int ret = 0;
768
769#ifdef CONFIG_IRQ_REMAP
770 /* Note: We have already checked GASup from IVRS table.
771 * Now, we need to make sure that GAMSup is set.
772 */
773 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
774 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
775 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
776
777 ret = iommu_init_ga_log(iommu);
778#endif /* CONFIG_IRQ_REMAP */
779
780 return ret;
781}
782
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100783static void iommu_enable_gt(struct amd_iommu *iommu)
784{
785 if (!iommu_feature(iommu, FEATURE_GT))
786 return;
787
788 iommu_feature_enable(iommu, CONTROL_GT_EN);
789}
790
Joerg Roedelb65233a2008-07-11 17:14:21 +0200791/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200792static void set_dev_entry_bit(u16 devid, u8 bit)
793{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100794 int i = (bit >> 6) & 0x03;
795 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200796
Joerg Roedelee6c2862011-11-09 12:06:03 +0100797 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200798}
799
Joerg Roedelc5cca142009-10-09 18:31:20 +0200800static int get_dev_entry_bit(u16 devid, u8 bit)
801{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100802 int i = (bit >> 6) & 0x03;
803 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200804
Joerg Roedelee6c2862011-11-09 12:06:03 +0100805 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200806}
807
808
809void amd_iommu_apply_erratum_63(u16 devid)
810{
811 int sysmgt;
812
813 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
814 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
815
816 if (sysmgt == 0x01)
817 set_dev_entry_bit(devid, DEV_ENTRY_IW);
818}
819
Joerg Roedel5ff47892008-07-14 20:11:18 +0200820/* Writes the specific IOMMU for a device into the rlookup table */
821static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
822{
823 amd_iommu_rlookup_table[devid] = iommu;
824}
825
Joerg Roedelb65233a2008-07-11 17:14:21 +0200826/*
827 * This function takes the device specific flags read from the ACPI
828 * table and sets up the device table entry with that information
829 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200830static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
831 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200832{
833 if (flags & ACPI_DEVFLAG_INITPASS)
834 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
835 if (flags & ACPI_DEVFLAG_EXTINT)
836 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
837 if (flags & ACPI_DEVFLAG_NMI)
838 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
839 if (flags & ACPI_DEVFLAG_SYSMGT1)
840 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
841 if (flags & ACPI_DEVFLAG_SYSMGT2)
842 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
843 if (flags & ACPI_DEVFLAG_LINT0)
844 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
845 if (flags & ACPI_DEVFLAG_LINT1)
846 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200847
Joerg Roedelc5cca142009-10-09 18:31:20 +0200848 amd_iommu_apply_erratum_63(devid);
849
Joerg Roedel5ff47892008-07-14 20:11:18 +0200850 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200851}
852
Joerg Roedelc50e3242014-09-09 15:59:37 +0200853static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200854{
855 struct devid_map *entry;
856 struct list_head *list;
857
Joerg Roedel31cff672013-04-09 16:53:58 +0200858 if (type == IVHD_SPECIAL_IOAPIC)
859 list = &ioapic_map;
860 else if (type == IVHD_SPECIAL_HPET)
861 list = &hpet_map;
862 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200863 return -EINVAL;
864
Joerg Roedel31cff672013-04-09 16:53:58 +0200865 list_for_each_entry(entry, list, list) {
866 if (!(entry->id == id && entry->cmd_line))
867 continue;
868
869 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
870 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
871
Joerg Roedelc50e3242014-09-09 15:59:37 +0200872 *devid = entry->devid;
873
Joerg Roedel31cff672013-04-09 16:53:58 +0200874 return 0;
875 }
876
Joerg Roedel6efed632012-06-14 15:52:58 +0200877 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
878 if (!entry)
879 return -ENOMEM;
880
Joerg Roedel31cff672013-04-09 16:53:58 +0200881 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +0200882 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +0200883 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +0200884
885 list_add_tail(&entry->list, list);
886
887 return 0;
888}
889
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400890static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
891 bool cmd_line)
892{
893 struct acpihid_map_entry *entry;
894 struct list_head *list = &acpihid_map;
895
896 list_for_each_entry(entry, list, list) {
897 if (strcmp(entry->hid, hid) ||
898 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
899 !entry->cmd_line)
900 continue;
901
902 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
903 hid, uid);
904 *devid = entry->devid;
905 return 0;
906 }
907
908 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
909 if (!entry)
910 return -ENOMEM;
911
912 memcpy(entry->uid, uid, strlen(uid));
913 memcpy(entry->hid, hid, strlen(hid));
914 entry->devid = *devid;
915 entry->cmd_line = cmd_line;
916 entry->root_devid = (entry->devid & (~0x7));
917
918 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
919 entry->cmd_line ? "cmd" : "ivrs",
920 entry->hid, entry->uid, entry->root_devid);
921
922 list_add_tail(&entry->list, list);
923 return 0;
924}
925
Joerg Roedel235dacb2013-04-09 17:53:14 +0200926static int __init add_early_maps(void)
927{
928 int i, ret;
929
930 for (i = 0; i < early_ioapic_map_size; ++i) {
931 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
932 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200933 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200934 early_ioapic_map[i].cmd_line);
935 if (ret)
936 return ret;
937 }
938
939 for (i = 0; i < early_hpet_map_size; ++i) {
940 ret = add_special_device(IVHD_SPECIAL_HPET,
941 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +0200942 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +0200943 early_hpet_map[i].cmd_line);
944 if (ret)
945 return ret;
946 }
947
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400948 for (i = 0; i < early_acpihid_map_size; ++i) {
949 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
950 early_acpihid_map[i].uid,
951 &early_acpihid_map[i].devid,
952 early_acpihid_map[i].cmd_line);
953 if (ret)
954 return ret;
955 }
956
Joerg Roedel235dacb2013-04-09 17:53:14 +0200957 return 0;
958}
959
Joerg Roedelb65233a2008-07-11 17:14:21 +0200960/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200961 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +0200962 * it
963 */
Joerg Roedel3566b772008-06-26 21:27:46 +0200964static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
965{
966 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
967
968 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
969 return;
970
971 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +0200972 /*
973 * We only can configure exclusion ranges per IOMMU, not
974 * per device. But we can enable the exclusion range per
975 * device. This is done here
976 */
Su Friendy2c16c9f2014-05-07 13:54:52 +0800977 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +0200978 iommu->exclusion_start = m->range_start;
979 iommu->exclusion_length = m->range_length;
980 }
981}
982
Joerg Roedelb65233a2008-07-11 17:14:21 +0200983/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200984 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
985 * initializes the hardware and our data structures with it.
986 */
Joerg Roedel6efed632012-06-14 15:52:58 +0200987static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200988 struct ivhd_header *h)
989{
990 u8 *p = (u8 *)h;
991 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +0200992 u16 devid = 0, devid_start = 0, devid_to = 0;
993 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +0200994 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +0200995 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400996 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200997 int ret;
998
999
1000 ret = add_early_maps();
1001 if (ret)
1002 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001003
1004 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001005 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001006 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001007 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001008
1009 /*
1010 * Done. Now parse the device entries
1011 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001012 ivhd_size = get_ivhd_header_size(h);
1013 if (!ivhd_size) {
1014 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1015 return -EINVAL;
1016 }
1017
1018 p += ivhd_size;
1019
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001020 end += h->length;
1021
Joerg Roedel42a698f2009-05-20 15:41:28 +02001022
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001023 while (p < end) {
1024 e = (struct ivhd_entry *)p;
1025 switch (e->type) {
1026 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001027
Joerg Roedel226e8892015-10-20 17:33:44 +02001028 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001029
Joerg Roedel226e8892015-10-20 17:33:44 +02001030 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1031 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001032 break;
1033 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001034
1035 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1036 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001037 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001038 PCI_SLOT(e->devid),
1039 PCI_FUNC(e->devid),
1040 e->flags);
1041
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001042 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001043 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001044 break;
1045 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001046
1047 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1048 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001049 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001050 PCI_SLOT(e->devid),
1051 PCI_FUNC(e->devid),
1052 e->flags);
1053
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001054 devid_start = e->devid;
1055 flags = e->flags;
1056 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001057 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001058 break;
1059 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001060
1061 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1062 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001063 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001064 PCI_SLOT(e->devid),
1065 PCI_FUNC(e->devid),
1066 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001067 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001068 PCI_SLOT(e->ext >> 8),
1069 PCI_FUNC(e->ext >> 8));
1070
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001071 devid = e->devid;
1072 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001073 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001074 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001075 amd_iommu_alias_table[devid] = devid_to;
1076 break;
1077 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001078
1079 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1080 "devid: %02x:%02x.%x flags: %02x "
1081 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001082 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001083 PCI_SLOT(e->devid),
1084 PCI_FUNC(e->devid),
1085 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001086 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001087 PCI_SLOT(e->ext >> 8),
1088 PCI_FUNC(e->ext >> 8));
1089
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001090 devid_start = e->devid;
1091 flags = e->flags;
1092 devid_to = e->ext >> 8;
1093 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001094 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001095 break;
1096 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001097
1098 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1099 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001100 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001101 PCI_SLOT(e->devid),
1102 PCI_FUNC(e->devid),
1103 e->flags, e->ext);
1104
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001105 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001106 set_dev_entry_from_acpi(iommu, devid, e->flags,
1107 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001108 break;
1109 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001110
1111 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1112 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001113 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001114 PCI_SLOT(e->devid),
1115 PCI_FUNC(e->devid),
1116 e->flags, e->ext);
1117
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001118 devid_start = e->devid;
1119 flags = e->flags;
1120 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001121 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001122 break;
1123 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001124
1125 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001126 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001127 PCI_SLOT(e->devid),
1128 PCI_FUNC(e->devid));
1129
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001130 devid = e->devid;
1131 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001132 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001133 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001134 set_dev_entry_from_acpi(iommu,
1135 devid_to, flags, ext_flags);
1136 }
1137 set_dev_entry_from_acpi(iommu, dev_i,
1138 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001139 }
1140 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001141 case IVHD_DEV_SPECIAL: {
1142 u8 handle, type;
1143 const char *var;
1144 u16 devid;
1145 int ret;
1146
1147 handle = e->ext & 0xff;
1148 devid = (e->ext >> 8) & 0xffff;
1149 type = (e->ext >> 24) & 0xff;
1150
1151 if (type == IVHD_SPECIAL_IOAPIC)
1152 var = "IOAPIC";
1153 else if (type == IVHD_SPECIAL_HPET)
1154 var = "HPET";
1155 else
1156 var = "UNKNOWN";
1157
1158 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1159 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001160 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001161 PCI_SLOT(devid),
1162 PCI_FUNC(devid));
1163
Joerg Roedelc50e3242014-09-09 15:59:37 +02001164 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001165 if (ret)
1166 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001167
1168 /*
1169 * add_special_device might update the devid in case a
1170 * command-line override is present. So call
1171 * set_dev_entry_from_acpi after add_special_device.
1172 */
1173 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1174
Joerg Roedel6efed632012-06-14 15:52:58 +02001175 break;
1176 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001177 case IVHD_DEV_ACPI_HID: {
1178 u16 devid;
1179 u8 hid[ACPIHID_HID_LEN] = {0};
1180 u8 uid[ACPIHID_UID_LEN] = {0};
1181 int ret;
1182
1183 if (h->type != 0x40) {
1184 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1185 e->type);
1186 break;
1187 }
1188
1189 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1190 hid[ACPIHID_HID_LEN - 1] = '\0';
1191
1192 if (!(*hid)) {
1193 pr_err(FW_BUG "Invalid HID.\n");
1194 break;
1195 }
1196
1197 switch (e->uidf) {
1198 case UID_NOT_PRESENT:
1199
1200 if (e->uidl != 0)
1201 pr_warn(FW_BUG "Invalid UID length.\n");
1202
1203 break;
1204 case UID_IS_INTEGER:
1205
1206 sprintf(uid, "%d", e->uid);
1207
1208 break;
1209 case UID_IS_CHARACTER:
1210
1211 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1212 uid[ACPIHID_UID_LEN - 1] = '\0';
1213
1214 break;
1215 default:
1216 break;
1217 }
1218
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001219 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001220 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1221 hid, uid,
1222 PCI_BUS_NUM(devid),
1223 PCI_SLOT(devid),
1224 PCI_FUNC(devid));
1225
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001226 flags = e->flags;
1227
1228 ret = add_acpi_hid_device(hid, uid, &devid, false);
1229 if (ret)
1230 return ret;
1231
1232 /*
1233 * add_special_device might update the devid in case a
1234 * command-line override is present. So call
1235 * set_dev_entry_from_acpi after add_special_device.
1236 */
1237 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1238
1239 break;
1240 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001241 default:
1242 break;
1243 }
1244
Joerg Roedelb514e552008-09-17 17:14:27 +02001245 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001246 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001247
1248 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001249}
1250
Joerg Roedele47d4022008-06-26 21:27:48 +02001251static void __init free_iommu_one(struct amd_iommu *iommu)
1252{
1253 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001254 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001255 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001256 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001257 iommu_unmap_mmio_space(iommu);
1258}
1259
1260static void __init free_iommu_all(void)
1261{
1262 struct amd_iommu *iommu, *next;
1263
Joerg Roedel3bd22172009-05-04 15:06:20 +02001264 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001265 list_del(&iommu->list);
1266 free_iommu_one(iommu);
1267 kfree(iommu);
1268 }
1269}
1270
Joerg Roedelb65233a2008-07-11 17:14:21 +02001271/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001272 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1273 * Workaround:
1274 * BIOS should disable L2B micellaneous clock gating by setting
1275 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1276 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001277static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001278{
1279 u32 value;
1280
1281 if ((boot_cpu_data.x86 != 0x15) ||
1282 (boot_cpu_data.x86_model < 0x10) ||
1283 (boot_cpu_data.x86_model > 0x1f))
1284 return;
1285
1286 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1287 pci_read_config_dword(iommu->dev, 0xf4, &value);
1288
1289 if (value & BIT(2))
1290 return;
1291
1292 /* Select NB indirect register 0x90 and enable writing */
1293 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1294
1295 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1296 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1297 dev_name(&iommu->dev->dev));
1298
1299 /* Clear the enable writing bit */
1300 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1301}
1302
1303/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001304 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1305 * Workaround:
1306 * BIOS should enable ATS write permission check by setting
1307 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1308 */
1309static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1310{
1311 u32 value;
1312
1313 if ((boot_cpu_data.x86 != 0x15) ||
1314 (boot_cpu_data.x86_model < 0x30) ||
1315 (boot_cpu_data.x86_model > 0x3f))
1316 return;
1317
1318 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1319 value = iommu_read_l2(iommu, 0x47);
1320
1321 if (value & BIT(0))
1322 return;
1323
1324 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1325 iommu_write_l2(iommu, 0x47, value | BIT(0));
1326
1327 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1328 dev_name(&iommu->dev->dev));
1329}
1330
1331/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001332 * This function clues the initialization function for one IOMMU
1333 * together and also allocates the command buffer and programs the
1334 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1335 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001336static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1337{
Joerg Roedel6efed632012-06-14 15:52:58 +02001338 int ret;
1339
Joerg Roedele47d4022008-06-26 21:27:48 +02001340 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001341
1342 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001343 list_add_tail(&iommu->list, &amd_iommu_list);
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001344 iommu->index = amd_iommus_present++;
Joerg Roedelbb527772009-11-20 14:31:51 +01001345
1346 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1347 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1348 return -ENOSYS;
1349 }
1350
1351 /* Index is fine - add IOMMU to the array */
1352 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001353
1354 /*
1355 * Copy data from ACPI table entry to the iommu struct
1356 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001357 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001358 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001359 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001360 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001361
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001362 switch (h->type) {
1363 case 0x10:
1364 /* Check if IVHD EFR contains proper max banks/counters */
1365 if ((h->efr_attr != 0) &&
1366 ((h->efr_attr & (0xF << 13)) != 0) &&
1367 ((h->efr_attr & (0x3F << 17)) != 0))
1368 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1369 else
1370 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001371 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1372 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001373 break;
1374 case 0x11:
1375 case 0x40:
1376 if (h->efr_reg & (1 << 9))
1377 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1378 else
1379 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001380 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1381 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001382 break;
1383 default:
1384 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001385 }
1386
1387 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1388 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001389 if (!iommu->mmio_base)
1390 return -ENOMEM;
1391
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001392 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001393 return -ENOMEM;
1394
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001395 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001396 return -ENOMEM;
1397
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001398 iommu->int_enabled = false;
1399
Joerg Roedel6efed632012-06-14 15:52:58 +02001400 ret = init_iommu_from_acpi(iommu, h);
1401 if (ret)
1402 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001403
Jiang Liu7c71d302015-04-13 14:11:33 +08001404 ret = amd_iommu_create_irq_domain(iommu);
1405 if (ret)
1406 return ret;
1407
Joerg Roedelf6fec002012-06-21 16:51:25 +02001408 /*
1409 * Make sure IOMMU is not considered to translate itself. The IVRS
1410 * table tells us so, but this is a lie!
1411 */
1412 amd_iommu_rlookup_table[iommu->devid] = NULL;
1413
Joerg Roedel23c742d2012-06-12 11:47:34 +02001414 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001415}
1416
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001417/**
1418 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1419 * @ivrs Pointer to the IVRS header
1420 *
1421 * This function search through all IVDB of the maximum supported IVHD
1422 */
1423static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1424{
1425 u8 *base = (u8 *)ivrs;
1426 struct ivhd_header *ivhd = (struct ivhd_header *)
1427 (base + IVRS_HEADER_LENGTH);
1428 u8 last_type = ivhd->type;
1429 u16 devid = ivhd->devid;
1430
1431 while (((u8 *)ivhd - base < ivrs->length) &&
1432 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1433 u8 *p = (u8 *) ivhd;
1434
1435 if (ivhd->devid == devid)
1436 last_type = ivhd->type;
1437 ivhd = (struct ivhd_header *)(p + ivhd->length);
1438 }
1439
1440 return last_type;
1441}
1442
Joerg Roedelb65233a2008-07-11 17:14:21 +02001443/*
1444 * Iterates over all IOMMU entries in the ACPI table, allocates the
1445 * IOMMU structure and initializes it with init_iommu_one()
1446 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001447static int __init init_iommu_all(struct acpi_table_header *table)
1448{
1449 u8 *p = (u8 *)table, *end = (u8 *)table;
1450 struct ivhd_header *h;
1451 struct amd_iommu *iommu;
1452 int ret;
1453
Joerg Roedele47d4022008-06-26 21:27:48 +02001454 end += table->length;
1455 p += IVRS_HEADER_LENGTH;
1456
1457 while (p < end) {
1458 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001459 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001460
Joerg Roedelae908c22009-09-01 16:52:16 +02001461 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001462 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001463 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001464 PCI_FUNC(h->devid), h->cap_ptr,
1465 h->pci_seg, h->flags, h->info);
1466 DUMP_printk(" mmio-addr: %016llx\n",
1467 h->mmio_phys);
1468
Joerg Roedele47d4022008-06-26 21:27:48 +02001469 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001470 if (iommu == NULL)
1471 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001472
Joerg Roedele47d4022008-06-26 21:27:48 +02001473 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001474 if (ret)
1475 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001476 }
1477 p += h->length;
1478
1479 }
1480 WARN_ON(p != end);
1481
1482 return 0;
1483}
1484
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001485static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1486 u8 fxn, u64 *value, bool is_write);
Steven L Kinney30861dd2013-06-05 16:11:48 -05001487
1488static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1489{
1490 u64 val = 0xabcd, val2 = 0;
1491
1492 if (!iommu_feature(iommu, FEATURE_PC))
1493 return;
1494
1495 amd_iommu_pc_present = true;
1496
1497 /* Check if the performance counters can be written to */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001498 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1499 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001500 (val != val2)) {
1501 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1502 amd_iommu_pc_present = false;
1503 return;
1504 }
1505
1506 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1507
1508 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1509 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1510 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1511}
1512
Alex Williamson066f2e92014-06-12 16:12:37 -06001513static ssize_t amd_iommu_show_cap(struct device *dev,
1514 struct device_attribute *attr,
1515 char *buf)
1516{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001517 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001518 return sprintf(buf, "%x\n", iommu->cap);
1519}
1520static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1521
1522static ssize_t amd_iommu_show_features(struct device *dev,
1523 struct device_attribute *attr,
1524 char *buf)
1525{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001526 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001527 return sprintf(buf, "%llx\n", iommu->features);
1528}
1529static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1530
1531static struct attribute *amd_iommu_attrs[] = {
1532 &dev_attr_cap.attr,
1533 &dev_attr_features.attr,
1534 NULL,
1535};
1536
1537static struct attribute_group amd_iommu_group = {
1538 .name = "amd-iommu",
1539 .attrs = amd_iommu_attrs,
1540};
1541
1542static const struct attribute_group *amd_iommu_groups[] = {
1543 &amd_iommu_group,
1544 NULL,
1545};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001546
Joerg Roedel23c742d2012-06-12 11:47:34 +02001547static int iommu_init_pci(struct amd_iommu *iommu)
1548{
1549 int cap_ptr = iommu->cap_ptr;
1550 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001551 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001552
Shuah Khanc5081cd2013-02-27 17:07:19 -07001553 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001554 iommu->devid & 0xff);
1555 if (!iommu->dev)
1556 return -ENODEV;
1557
Jiang Liucbbc00b2015-10-09 22:07:31 +08001558 /* Prevent binding other PCI device drivers to IOMMU devices */
1559 iommu->dev->match_driver = false;
1560
Joerg Roedel23c742d2012-06-12 11:47:34 +02001561 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1562 &iommu->cap);
1563 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1564 &range);
1565 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1566 &misc);
1567
Joerg Roedel23c742d2012-06-12 11:47:34 +02001568 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1569 amd_iommu_iotlb_sup = false;
1570
1571 /* read extended feature bits */
1572 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1573 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1574
1575 iommu->features = ((u64)high << 32) | low;
1576
1577 if (iommu_feature(iommu, FEATURE_GT)) {
1578 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001579 u32 max_pasid;
1580 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001581
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001582 pasmax = iommu->features & FEATURE_PASID_MASK;
1583 pasmax >>= FEATURE_PASID_SHIFT;
1584 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001585
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001586 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1587
1588 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001589
1590 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1591 glxval >>= FEATURE_GLXVAL_SHIFT;
1592
1593 if (amd_iommu_max_glx_val == -1)
1594 amd_iommu_max_glx_val = glxval;
1595 else
1596 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1597 }
1598
1599 if (iommu_feature(iommu, FEATURE_GT) &&
1600 iommu_feature(iommu, FEATURE_PPR)) {
1601 iommu->is_iommu_v2 = true;
1602 amd_iommu_v2_present = true;
1603 }
1604
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001605 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1606 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001607
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001608 ret = iommu_init_ga(iommu);
1609 if (ret)
1610 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001611
Joerg Roedel23c742d2012-06-12 11:47:34 +02001612 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1613 amd_iommu_np_cache = true;
1614
Steven L Kinney30861dd2013-06-05 16:11:48 -05001615 init_iommu_perf_ctr(iommu);
1616
Joerg Roedel23c742d2012-06-12 11:47:34 +02001617 if (is_rd890_iommu(iommu->dev)) {
1618 int i, j;
1619
1620 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1621 PCI_DEVFN(0, 0));
1622
1623 /*
1624 * Some rd890 systems may not be fully reconfigured by the
1625 * BIOS, so it's necessary for us to store this information so
1626 * it can be reprogrammed on resume
1627 */
1628 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1629 &iommu->stored_addr_lo);
1630 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1631 &iommu->stored_addr_hi);
1632
1633 /* Low bit locks writes to configuration space */
1634 iommu->stored_addr_lo &= ~1;
1635
1636 for (i = 0; i < 6; i++)
1637 for (j = 0; j < 0x12; j++)
1638 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1639
1640 for (i = 0; i < 0x83; i++)
1641 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1642 }
1643
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001644 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001645 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001646
Joerg Roedel39ab9552017-02-01 16:56:46 +01001647 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1648 amd_iommu_groups, "ivhd%d", iommu->index);
Joerg Roedelb0119e82017-02-01 13:23:08 +01001649 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1650 iommu_device_register(&iommu->iommu);
Alex Williamson066f2e92014-06-12 16:12:37 -06001651
Joerg Roedel23c742d2012-06-12 11:47:34 +02001652 return pci_enable_device(iommu->dev);
1653}
1654
Joerg Roedel4d121c32012-06-14 12:21:55 +02001655static void print_iommu_info(void)
1656{
1657 static const char * const feat_str[] = {
1658 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1659 "IA", "GA", "HE", "PC"
1660 };
1661 struct amd_iommu *iommu;
1662
1663 for_each_iommu(iommu) {
1664 int i;
1665
1666 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1667 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1668
1669 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001670 pr_info("AMD-Vi: Extended features (%#llx):\n",
1671 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001672 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001673 if (iommu_feature(iommu, (1ULL << i)))
1674 pr_cont(" %s", feat_str[i]);
1675 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001676
1677 if (iommu->features & FEATURE_GAM_VAPIC)
1678 pr_cont(" GA_vAPIC");
1679
Steven L Kinney30861dd2013-06-05 16:11:48 -05001680 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001681 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001682 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001683 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001684 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001685 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1686 pr_info("AMD-Vi: virtual APIC enabled\n");
1687 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001688}
1689
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001690static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001691{
1692 struct amd_iommu *iommu;
1693 int ret = 0;
1694
1695 for_each_iommu(iommu) {
1696 ret = iommu_init_pci(iommu);
1697 if (ret)
1698 break;
1699 }
1700
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001701 /*
1702 * Order is important here to make sure any unity map requirements are
1703 * fulfilled. The unity mappings are created and written to the device
1704 * table during the amd_iommu_init_api() call.
1705 *
1706 * After that we call init_device_table_dma() to make sure any
1707 * uninitialized DTE will block DMA, and in the end we flush the caches
1708 * of all IOMMUs to make sure the changes to the device table are
1709 * active.
1710 */
1711 ret = amd_iommu_init_api();
1712
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001713 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001714
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001715 for_each_iommu(iommu)
1716 iommu_flush_all_caches(iommu);
1717
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001718 if (!ret)
1719 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001720
Joerg Roedel23c742d2012-06-12 11:47:34 +02001721 return ret;
1722}
1723
Joerg Roedelb65233a2008-07-11 17:14:21 +02001724/****************************************************************************
1725 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001726 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001727 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001728 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1729 * pci_dev.
1730 *
1731 ****************************************************************************/
1732
Joerg Roedel9f800de2009-11-23 12:45:25 +01001733static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001734{
1735 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001736
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001737 r = pci_enable_msi(iommu->dev);
1738 if (r)
1739 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001740
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001741 r = request_threaded_irq(iommu->dev->irq,
1742 amd_iommu_int_handler,
1743 amd_iommu_int_thread,
1744 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001745 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001746
1747 if (r) {
1748 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001749 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001750 }
1751
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001752 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001753
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001754 return 0;
1755}
1756
Joerg Roedel05f92db2009-05-12 09:52:46 +02001757static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001758{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001759 int ret;
1760
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001761 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001762 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001763
Yijing Wang82fcfc62013-08-08 21:12:36 +08001764 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001765 ret = iommu_setup_msi(iommu);
1766 else
1767 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001768
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001769 if (ret)
1770 return ret;
1771
1772enable_faults:
1773 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1774
1775 if (iommu->ppr_log != NULL)
1776 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1777
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001778 iommu_ga_log_enable(iommu);
1779
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001780 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001781}
1782
1783/****************************************************************************
1784 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001785 * The next functions belong to the third pass of parsing the ACPI
1786 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001787 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001788 *
1789 ****************************************************************************/
1790
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001791static void __init free_unity_maps(void)
1792{
1793 struct unity_map_entry *entry, *next;
1794
1795 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1796 list_del(&entry->list);
1797 kfree(entry);
1798 }
1799}
1800
Joerg Roedelb65233a2008-07-11 17:14:21 +02001801/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001802static int __init init_exclusion_range(struct ivmd_header *m)
1803{
1804 int i;
1805
1806 switch (m->type) {
1807 case ACPI_IVMD_TYPE:
1808 set_device_exclusion_range(m->devid, m);
1809 break;
1810 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001811 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001812 set_device_exclusion_range(i, m);
1813 break;
1814 case ACPI_IVMD_TYPE_RANGE:
1815 for (i = m->devid; i <= m->aux; ++i)
1816 set_device_exclusion_range(i, m);
1817 break;
1818 default:
1819 break;
1820 }
1821
1822 return 0;
1823}
1824
Joerg Roedelb65233a2008-07-11 17:14:21 +02001825/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001826static int __init init_unity_map_range(struct ivmd_header *m)
1827{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001828 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001829 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001830
1831 e = kzalloc(sizeof(*e), GFP_KERNEL);
1832 if (e == NULL)
1833 return -ENOMEM;
1834
1835 switch (m->type) {
1836 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001837 kfree(e);
1838 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001839 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001840 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001841 e->devid_start = e->devid_end = m->devid;
1842 break;
1843 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001844 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001845 e->devid_start = 0;
1846 e->devid_end = amd_iommu_last_bdf;
1847 break;
1848 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001849 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001850 e->devid_start = m->devid;
1851 e->devid_end = m->aux;
1852 break;
1853 }
1854 e->address_start = PAGE_ALIGN(m->range_start);
1855 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1856 e->prot = m->flags >> 1;
1857
Joerg Roedel02acc432009-05-20 16:24:21 +02001858 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1859 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001860 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1861 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02001862 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1863 e->address_start, e->address_end, m->flags);
1864
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001865 list_add_tail(&e->list, &amd_iommu_unity_map);
1866
1867 return 0;
1868}
1869
Joerg Roedelb65233a2008-07-11 17:14:21 +02001870/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001871static int __init init_memory_definitions(struct acpi_table_header *table)
1872{
1873 u8 *p = (u8 *)table, *end = (u8 *)table;
1874 struct ivmd_header *m;
1875
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001876 end += table->length;
1877 p += IVRS_HEADER_LENGTH;
1878
1879 while (p < end) {
1880 m = (struct ivmd_header *)p;
1881 if (m->flags & IVMD_FLAG_EXCL_RANGE)
1882 init_exclusion_range(m);
1883 else if (m->flags & IVMD_FLAG_UNITY_MAP)
1884 init_unity_map_range(m);
1885
1886 p += m->length;
1887 }
1888
1889 return 0;
1890}
1891
Joerg Roedelb65233a2008-07-11 17:14:21 +02001892/*
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001893 * Init the device table to not allow DMA access for devices and
1894 * suppress all page faults
1895 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02001896static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001897{
Joerg Roedel0de66d52011-06-06 16:04:02 +02001898 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001899
1900 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1901 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
1902 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02001903 }
1904}
1905
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02001906static void __init uninit_device_table_dma(void)
1907{
1908 u32 devid;
1909
1910 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
1911 amd_iommu_dev_table[devid].data[0] = 0ULL;
1912 amd_iommu_dev_table[devid].data[1] = 0ULL;
1913 }
1914}
1915
Joerg Roedel33f28c52012-06-15 18:03:31 +02001916static void init_device_table(void)
1917{
1918 u32 devid;
1919
1920 if (!amd_iommu_irq_remap)
1921 return;
1922
1923 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
1924 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
1925}
1926
Joerg Roedele9bf5192010-09-20 14:33:07 +02001927static void iommu_init_flags(struct amd_iommu *iommu)
1928{
1929 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
1930 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
1931 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
1932
1933 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
1934 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
1935 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
1936
1937 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
1938 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
1939 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
1940
1941 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
1942 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
1943 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
1944
1945 /*
1946 * make IOMMU memory accesses cache coherent
1947 */
1948 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01001949
1950 /* Set IOTLB invalidation timeout to 1s */
1951 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02001952}
1953
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001954static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02001955{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001956 int i, j;
1957 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001958 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001959
1960 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02001961 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001962 return;
1963
1964 /*
1965 * First, we need to ensure that the iommu is enabled. This is
1966 * controlled by a register in the northbridge
1967 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001968
1969 /* Select Northbridge indirect register 0x75 and enable writing */
1970 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
1971 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
1972
1973 /* Enable the iommu */
1974 if (!(ioc_feature_control & 0x1))
1975 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
1976
Matthew Garrett5bcd7572010-10-04 14:59:31 -04001977 /* Restore the iommu BAR */
1978 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1979 iommu->stored_addr_lo);
1980 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
1981 iommu->stored_addr_hi);
1982
1983 /* Restore the l1 indirect regs for each of the 6 l1s */
1984 for (i = 0; i < 6; i++)
1985 for (j = 0; j < 0x12; j++)
1986 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
1987
1988 /* Restore the l2 indirect regs */
1989 for (i = 0; i < 0x83; i++)
1990 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
1991
1992 /* Lock PCI setup registers */
1993 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
1994 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02001995}
1996
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001997static void iommu_enable_ga(struct amd_iommu *iommu)
1998{
1999#ifdef CONFIG_IRQ_REMAP
2000 switch (amd_iommu_guest_ir) {
2001 case AMD_IOMMU_GUEST_IR_VAPIC:
2002 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2003 /* Fall through */
2004 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2005 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002006 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002007 break;
2008 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002009 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002010 break;
2011 }
2012#endif
2013}
2014
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002015/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002016 * This function finally enables all IOMMUs found in the system after
2017 * they have been initialized
2018 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002019static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002020{
2021 struct amd_iommu *iommu;
2022
Joerg Roedel3bd22172009-05-04 15:06:20 +02002023 for_each_iommu(iommu) {
Chris Wrighta8c485b2009-06-15 15:53:45 +02002024 iommu_disable(iommu);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002025 iommu_init_flags(iommu);
Joerg Roedel58492e12009-05-04 18:41:16 +02002026 iommu_set_device_table(iommu);
2027 iommu_enable_command_buffer(iommu);
2028 iommu_enable_event_buffer(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002029 iommu_set_exclusion_range(iommu);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002030 iommu_enable_ga(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002031 iommu_enable(iommu);
Joerg Roedel7d0c5cc2011-04-07 08:16:10 +02002032 iommu_flush_all_caches(iommu);
Joerg Roedel87361972008-06-26 21:28:07 +02002033 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002034
2035#ifdef CONFIG_IRQ_REMAP
2036 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2037 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2038#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002039}
2040
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002041static void enable_iommus_v2(void)
2042{
2043 struct amd_iommu *iommu;
2044
2045 for_each_iommu(iommu) {
2046 iommu_enable_ppr_log(iommu);
2047 iommu_enable_gt(iommu);
2048 }
2049}
2050
2051static void enable_iommus(void)
2052{
2053 early_enable_iommus();
2054
2055 enable_iommus_v2();
2056}
2057
Joerg Roedel92ac4322009-05-19 19:06:27 +02002058static void disable_iommus(void)
2059{
2060 struct amd_iommu *iommu;
2061
2062 for_each_iommu(iommu)
2063 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002064
2065#ifdef CONFIG_IRQ_REMAP
2066 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2067 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2068#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002069}
2070
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002071/*
2072 * Suspend/Resume support
2073 * disable suspend until real resume implemented
2074 */
2075
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002076static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002077{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002078 struct amd_iommu *iommu;
2079
2080 for_each_iommu(iommu)
2081 iommu_apply_resume_quirks(iommu);
2082
Joerg Roedel736501e2009-05-12 09:56:12 +02002083 /* re-load the hardware */
2084 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002085
2086 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002087}
2088
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002089static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002090{
Joerg Roedel736501e2009-05-12 09:56:12 +02002091 /* disable IOMMUs to go out of the way for BIOS */
2092 disable_iommus();
2093
2094 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002095}
2096
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002097static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002098 .suspend = amd_iommu_suspend,
2099 .resume = amd_iommu_resume,
2100};
2101
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002102static void __init free_on_init_error(void)
2103{
Lucas Stachebcfa282016-10-26 13:09:53 +02002104 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002105 free_pages((unsigned long)irq_lookup_table,
2106 get_order(rlookup_table_size));
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002107
Julia Lawalla5919892015-09-13 14:15:31 +02002108 kmem_cache_destroy(amd_iommu_irq_cache);
2109 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002110
2111 free_pages((unsigned long)amd_iommu_rlookup_table,
2112 get_order(rlookup_table_size));
2113
2114 free_pages((unsigned long)amd_iommu_alias_table,
2115 get_order(alias_table_size));
2116
2117 free_pages((unsigned long)amd_iommu_dev_table,
2118 get_order(dev_table_size));
2119
2120 free_iommu_all();
2121
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002122#ifdef CONFIG_GART_IOMMU
2123 /*
2124 * We failed to initialize the AMD IOMMU - try fallback to GART
2125 * if possible.
2126 */
2127 gart_iommu_init();
2128
2129#endif
2130}
2131
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002132/* SB IOAPIC is always on this device in AMD systems */
2133#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2134
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002135static bool __init check_ioapic_information(void)
2136{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002137 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002138 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002139 int idx;
2140
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002141 has_sb_ioapic = false;
2142 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002143
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002144 /*
2145 * If we have map overrides on the kernel command line the
2146 * messages in this function might not describe firmware bugs
2147 * anymore - so be careful
2148 */
2149 if (cmdline_maps)
2150 fw_bug = "";
2151
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002152 for (idx = 0; idx < nr_ioapics; idx++) {
2153 int devid, id = mpc_ioapic_id(idx);
2154
2155 devid = get_ioapic_devid(id);
2156 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002157 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2158 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002159 ret = false;
2160 } else if (devid == IOAPIC_SB_DEVID) {
2161 has_sb_ioapic = true;
2162 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002163 }
2164 }
2165
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002166 if (!has_sb_ioapic) {
2167 /*
2168 * We expect the SB IOAPIC to be listed in the IVRS
2169 * table. The system timer is connected to the SB IOAPIC
2170 * and if we don't have it in the list the system will
2171 * panic at boot time. This situation usually happens
2172 * when the BIOS is buggy and provides us the wrong
2173 * device id for the IOAPIC in the system.
2174 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002175 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002176 }
2177
2178 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002179 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002180
2181 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002182}
2183
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002184static void __init free_dma_resources(void)
2185{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002186 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2187 get_order(MAX_DOMAIN_ID/8));
2188
2189 free_unity_maps();
2190}
2191
Joerg Roedelb65233a2008-07-11 17:14:21 +02002192/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002193 * This is the hardware init function for AMD IOMMU in the system.
2194 * This function is called either from amd_iommu_init or from the interrupt
2195 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002196 *
2197 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002198 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002199 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002200 * 1 pass) Discover the most comprehensive IVHD type to use.
2201 *
2202 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002203 * Upon this information the size of the data structures is
2204 * determined that needs to be allocated.
2205 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002206 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002207 * information in the ACPI table about available AMD IOMMUs
2208 * in the system. It also maps the PCI devices in the
2209 * system to specific IOMMUs
2210 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002211 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002212 * initialized we update them with information about memory
2213 * remapping requirements parsed out of the ACPI table in
2214 * this last pass.
2215 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002216 * After everything is set up the IOMMUs are enabled and the necessary
2217 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002218 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002219static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002220{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002221 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002222 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002223 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002224
Joerg Roedel643511b2012-06-12 12:09:35 +02002225 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002226 return -ENODEV;
2227
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002228 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002229 if (status == AE_NOT_FOUND)
2230 return -ENODEV;
2231 else if (ACPI_FAILURE(status)) {
2232 const char *err = acpi_format_exception(status);
2233 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2234 return -EINVAL;
2235 }
2236
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002237 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002238 * Validate checksum here so we don't need to do it when
2239 * we actually parse the table
2240 */
2241 ret = check_ivrs_checksum(ivrs_base);
2242 if (ret)
Rafael J. Wysocki99e8ccd2017-01-10 14:57:28 +01002243 goto out;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002244
2245 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2246 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2247
2248 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002249 * First parse ACPI tables to find the largest Bus/Dev/Func
2250 * we need to handle. Upon this information the shared data
2251 * structures for the IOMMUs in the system will be allocated
2252 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002253 ret = find_last_devid_acpi(ivrs_base);
2254 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002255 goto out;
2256
Joerg Roedelc5714842008-07-11 17:14:25 +02002257 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2258 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2259 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002260
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002261 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002262 ret = -ENOMEM;
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002263 amd_iommu_dev_table = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002264 get_order(dev_table_size));
2265 if (amd_iommu_dev_table == NULL)
2266 goto out;
2267
2268 /*
2269 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2270 * IOMMU see for that device
2271 */
2272 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2273 get_order(alias_table_size));
2274 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002275 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002276
2277 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002278 amd_iommu_rlookup_table = (void *)__get_free_pages(
2279 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002280 get_order(rlookup_table_size));
2281 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002282 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002283
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002284 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2285 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002286 get_order(MAX_DOMAIN_ID/8));
2287 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002288 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002289
2290 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002291 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002292 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002293 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002294 amd_iommu_alias_table[i] = i;
2295
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002296 /*
2297 * never allocate domain 0 because its used as the non-allocated and
2298 * error value placeholder
2299 */
Baoquan He5c87f622016-09-15 16:50:51 +08002300 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002301
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002302 spin_lock_init(&amd_iommu_pd_lock);
2303
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002304 /*
2305 * now the data structures are allocated and basically initialized
2306 * start the real acpi table scan
2307 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002308 ret = init_iommu_all(ivrs_base);
2309 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002310 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002311
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002312 if (amd_iommu_irq_remap)
2313 amd_iommu_irq_remap = check_ioapic_information();
2314
Joerg Roedel05152a02012-06-15 16:53:51 +02002315 if (amd_iommu_irq_remap) {
2316 /*
2317 * Interrupt remapping enabled, create kmem_cache for the
2318 * remapping tables.
2319 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002320 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002321 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2322 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2323 else
2324 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002325 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002326 remap_cache_sz,
2327 IRQ_TABLE_ALIGNMENT,
2328 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002329 if (!amd_iommu_irq_cache)
2330 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002331
2332 irq_lookup_table = (void *)__get_free_pages(
2333 GFP_KERNEL | __GFP_ZERO,
2334 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002335 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2336 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002337 if (!irq_lookup_table)
2338 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002339 }
2340
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002341 ret = init_memory_definitions(ivrs_base);
2342 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002343 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002344
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002345 /* init the device table */
2346 init_device_table();
2347
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002348out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002349 /* Don't leak any ACPI memory */
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002350 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002351 ivrs_base = NULL;
2352
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002353 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002354}
2355
Gerard Snitselaarae295142012-03-16 11:38:22 -07002356static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002357{
2358 struct amd_iommu *iommu;
2359 int ret = 0;
2360
2361 for_each_iommu(iommu) {
2362 ret = iommu_init_msi(iommu);
2363 if (ret)
2364 goto out;
2365 }
2366
2367out:
2368 return ret;
2369}
2370
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002371static bool detect_ivrs(void)
2372{
2373 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002374 acpi_status status;
2375
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002376 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002377 if (status == AE_NOT_FOUND)
2378 return false;
2379 else if (ACPI_FAILURE(status)) {
2380 const char *err = acpi_format_exception(status);
2381 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2382 return false;
2383 }
2384
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002385 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002386
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002387 /* Make sure ACS will be enabled during PCI probe */
2388 pci_request_acs();
2389
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002390 return true;
2391}
2392
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002393/****************************************************************************
2394 *
2395 * AMD IOMMU Initialization State Machine
2396 *
2397 ****************************************************************************/
2398
2399static int __init state_next(void)
2400{
2401 int ret = 0;
2402
2403 switch (init_state) {
2404 case IOMMU_START_STATE:
2405 if (!detect_ivrs()) {
2406 init_state = IOMMU_NOT_FOUND;
2407 ret = -ENODEV;
2408 } else {
2409 init_state = IOMMU_IVRS_DETECTED;
2410 }
2411 break;
2412 case IOMMU_IVRS_DETECTED:
2413 ret = early_amd_iommu_init();
2414 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
2415 break;
2416 case IOMMU_ACPI_FINISHED:
2417 early_enable_iommus();
2418 register_syscore_ops(&amd_iommu_syscore_ops);
2419 x86_platform.iommu_shutdown = disable_iommus;
2420 init_state = IOMMU_ENABLED;
2421 break;
2422 case IOMMU_ENABLED:
2423 ret = amd_iommu_init_pci();
2424 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2425 enable_iommus_v2();
2426 break;
2427 case IOMMU_PCI_INIT:
2428 ret = amd_iommu_enable_interrupts();
2429 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2430 break;
2431 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002432 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002433 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2434 break;
2435 case IOMMU_DMA_OPS:
2436 init_state = IOMMU_INITIALIZED;
2437 break;
2438 case IOMMU_INITIALIZED:
2439 /* Nothing to do */
2440 break;
2441 case IOMMU_NOT_FOUND:
2442 case IOMMU_INIT_ERROR:
2443 /* Error states => do nothing */
2444 ret = -EINVAL;
2445 break;
2446 default:
2447 /* Unknown state */
2448 BUG();
2449 }
2450
2451 return ret;
2452}
2453
2454static int __init iommu_go_to_state(enum iommu_init_state state)
2455{
2456 int ret = 0;
2457
2458 while (init_state != state) {
2459 ret = state_next();
2460 if (init_state == IOMMU_NOT_FOUND ||
2461 init_state == IOMMU_INIT_ERROR)
2462 break;
2463 }
2464
2465 return ret;
2466}
2467
Joerg Roedel6b474b82012-06-26 16:46:04 +02002468#ifdef CONFIG_IRQ_REMAP
2469int __init amd_iommu_prepare(void)
2470{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002471 int ret;
2472
Jiang Liu7fa1c842015-01-07 15:31:42 +08002473 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002474
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002475 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2476 if (ret)
2477 return ret;
2478 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002479}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002480
Joerg Roedel6b474b82012-06-26 16:46:04 +02002481int __init amd_iommu_enable(void)
2482{
2483 int ret;
2484
2485 ret = iommu_go_to_state(IOMMU_ENABLED);
2486 if (ret)
2487 return ret;
2488
2489 irq_remapping_enabled = 1;
2490
2491 return 0;
2492}
2493
2494void amd_iommu_disable(void)
2495{
2496 amd_iommu_suspend();
2497}
2498
2499int amd_iommu_reenable(int mode)
2500{
2501 amd_iommu_resume();
2502
2503 return 0;
2504}
2505
2506int __init amd_iommu_enable_faulting(void)
2507{
2508 /* We enable MSI later when PCI is initialized */
2509 return 0;
2510}
2511#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002512
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002513/*
2514 * This is the core init function for AMD IOMMU hardware in the system.
2515 * This function is called from the generic x86 DMA layer initialization
2516 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002517 */
2518static int __init amd_iommu_init(void)
2519{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002520 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002521
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002522 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2523 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002524 free_dma_resources();
2525 if (!irq_remapping_enabled) {
2526 disable_iommus();
2527 free_on_init_error();
2528 } else {
2529 struct amd_iommu *iommu;
2530
2531 uninit_device_table_dma();
2532 for_each_iommu(iommu)
2533 iommu_flush_all_caches(iommu);
2534 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002535 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002536
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002537 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002538}
2539
Joerg Roedelb65233a2008-07-11 17:14:21 +02002540/****************************************************************************
2541 *
2542 * Early detect code. This code runs at IOMMU detection time in the DMA
2543 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2544 * IOMMUs
2545 *
2546 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002547int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002548{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002549 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002550
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002551 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002552 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002553
Joerg Roedela5235722010-05-11 17:12:33 +02002554 if (amd_iommu_disabled)
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002555 return -ENODEV;
Joerg Roedela5235722010-05-11 17:12:33 +02002556
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002557 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2558 if (ret)
2559 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002560
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002561 amd_iommu_detected = true;
2562 iommu_detected = 1;
2563 x86_init.iommu.iommu_init = amd_iommu_init;
2564
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002565 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002566}
2567
Joerg Roedelb65233a2008-07-11 17:14:21 +02002568/****************************************************************************
2569 *
2570 * Parsing functions for the AMD IOMMU specific kernel command line
2571 * options.
2572 *
2573 ****************************************************************************/
2574
Joerg Roedelfefda112009-05-20 12:21:42 +02002575static int __init parse_amd_iommu_dump(char *str)
2576{
2577 amd_iommu_dump = true;
2578
2579 return 1;
2580}
2581
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002582static int __init parse_amd_iommu_intr(char *str)
2583{
2584 for (; *str; ++str) {
2585 if (strncmp(str, "legacy", 6) == 0) {
2586 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2587 break;
2588 }
2589 if (strncmp(str, "vapic", 5) == 0) {
2590 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2591 break;
2592 }
2593 }
2594 return 1;
2595}
2596
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002597static int __init parse_amd_iommu_options(char *str)
2598{
2599 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002600 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002601 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002602 if (strncmp(str, "off", 3) == 0)
2603 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002604 if (strncmp(str, "force_isolation", 15) == 0)
2605 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002606 }
2607
2608 return 1;
2609}
2610
Joerg Roedel440e89982013-04-09 16:35:28 +02002611static int __init parse_ivrs_ioapic(char *str)
2612{
2613 unsigned int bus, dev, fn;
2614 int ret, id, i;
2615 u16 devid;
2616
2617 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2618
2619 if (ret != 4) {
2620 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2621 return 1;
2622 }
2623
2624 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2625 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2626 str);
2627 return 1;
2628 }
2629
2630 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2631
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002632 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002633 i = early_ioapic_map_size++;
2634 early_ioapic_map[i].id = id;
2635 early_ioapic_map[i].devid = devid;
2636 early_ioapic_map[i].cmd_line = true;
2637
2638 return 1;
2639}
2640
2641static int __init parse_ivrs_hpet(char *str)
2642{
2643 unsigned int bus, dev, fn;
2644 int ret, id, i;
2645 u16 devid;
2646
2647 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2648
2649 if (ret != 4) {
2650 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2651 return 1;
2652 }
2653
2654 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2655 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2656 str);
2657 return 1;
2658 }
2659
2660 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2661
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002662 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002663 i = early_hpet_map_size++;
2664 early_hpet_map[i].id = id;
2665 early_hpet_map[i].devid = devid;
2666 early_hpet_map[i].cmd_line = true;
2667
2668 return 1;
2669}
2670
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002671static int __init parse_ivrs_acpihid(char *str)
2672{
2673 u32 bus, dev, fn;
2674 char *hid, *uid, *p;
2675 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2676 int ret, i;
2677
2678 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2679 if (ret != 4) {
2680 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2681 return 1;
2682 }
2683
2684 p = acpiid;
2685 hid = strsep(&p, ":");
2686 uid = p;
2687
2688 if (!hid || !(*hid) || !uid) {
2689 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2690 return 1;
2691 }
2692
2693 i = early_acpihid_map_size++;
2694 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2695 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2696 early_acpihid_map[i].devid =
2697 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2698 early_acpihid_map[i].cmd_line = true;
2699
2700 return 1;
2701}
2702
Joerg Roedel440e89982013-04-09 16:35:28 +02002703__setup("amd_iommu_dump", parse_amd_iommu_dump);
2704__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002705__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002706__setup("ivrs_ioapic", parse_ivrs_ioapic);
2707__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002708__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002709
2710IOMMU_INIT_FINISH(amd_iommu_detect,
2711 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002712 NULL,
2713 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002714
2715bool amd_iommu_v2_supported(void)
2716{
2717 return amd_iommu_v2_present;
2718}
2719EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002720
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002721struct amd_iommu *get_amd_iommu(unsigned int idx)
2722{
2723 unsigned int i = 0;
2724 struct amd_iommu *iommu;
2725
2726 for_each_iommu(iommu)
2727 if (i++ == idx)
2728 return iommu;
2729 return NULL;
2730}
2731EXPORT_SYMBOL(get_amd_iommu);
2732
Steven L Kinney30861dd2013-06-05 16:11:48 -05002733/****************************************************************************
2734 *
2735 * IOMMU EFR Performance Counter support functionality. This code allows
2736 * access to the IOMMU PC functionality.
2737 *
2738 ****************************************************************************/
2739
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002740u8 amd_iommu_pc_get_max_banks(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002741{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002742 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002743
Steven L Kinney30861dd2013-06-05 16:11:48 -05002744 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002745 return iommu->max_banks;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002746
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002747 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002748}
2749EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2750
2751bool amd_iommu_pc_supported(void)
2752{
2753 return amd_iommu_pc_present;
2754}
2755EXPORT_SYMBOL(amd_iommu_pc_supported);
2756
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002757u8 amd_iommu_pc_get_max_counters(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002758{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002759 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002760
Steven L Kinney30861dd2013-06-05 16:11:48 -05002761 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002762 return iommu->max_counters;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002763
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002764 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002765}
2766EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2767
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002768static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
2769 u8 fxn, u64 *value, bool is_write)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002770{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002771 u32 offset;
2772 u32 max_offset_lim;
2773
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002774 /* Make sure the IOMMU PC resource is available */
2775 if (!amd_iommu_pc_present)
2776 return -ENODEV;
2777
Steven L Kinney30861dd2013-06-05 16:11:48 -05002778 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002779 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002780 return -ENODEV;
2781
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002782 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002783
2784 /* Limit the offset to the hw defined mmio region aperture */
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002785 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
Steven L Kinney30861dd2013-06-05 16:11:48 -05002786 (iommu->max_counters << 8) | 0x28);
2787 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2788 (offset > max_offset_lim))
2789 return -EINVAL;
2790
2791 if (is_write) {
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002792 u64 val = *value & GENMASK_ULL(47, 0);
2793
2794 writel((u32)val, iommu->mmio_base + offset);
2795 writel((val >> 32), iommu->mmio_base + offset + 4);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002796 } else {
2797 *value = readl(iommu->mmio_base + offset + 4);
2798 *value <<= 32;
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002799 *value |= readl(iommu->mmio_base + offset);
2800 *value &= GENMASK_ULL(47, 0);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002801 }
2802
2803 return 0;
2804}
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002805
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002806int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002807{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002808 if (!iommu)
2809 return -EINVAL;
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002810
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002811 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002812}
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002813EXPORT_SYMBOL(amd_iommu_pc_get_reg);
2814
2815int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
2816{
2817 if (!iommu)
2818 return -EINVAL;
2819
2820 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
2821}
2822EXPORT_SYMBOL(amd_iommu_pc_set_reg);