blob: f6dd63fed21e89a435ab9404411e22bb26960727 [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>
Tom Lendacky2543a782017-07-17 16:10:24 -050032#include <linux/mem_encrypt.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020033#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090034#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010035#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090036#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040037#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020038#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020039#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020040
Baoquan He3ac3e5ee2017-08-09 16:33:38 +080041#include <linux/crash_dump.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020042#include "amd_iommu_proto.h"
43#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020044#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020045
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020046/*
47 * definitions for the ACPI scanning code
48 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020049#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020050
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040051#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020052#define ACPI_IVMD_TYPE_ALL 0x20
53#define ACPI_IVMD_TYPE 0x21
54#define ACPI_IVMD_TYPE_RANGE 0x22
55
56#define IVHD_DEV_ALL 0x01
57#define IVHD_DEV_SELECT 0x02
58#define IVHD_DEV_SELECT_RANGE_START 0x03
59#define IVHD_DEV_RANGE_END 0x04
60#define IVHD_DEV_ALIAS 0x42
61#define IVHD_DEV_ALIAS_RANGE 0x43
62#define IVHD_DEV_EXT_SELECT 0x46
63#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020064#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040065#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020066
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040067#define UID_NOT_PRESENT 0
68#define UID_IS_INTEGER 1
69#define UID_IS_CHARACTER 2
70
Joerg Roedel6efed632012-06-14 15:52:58 +020071#define IVHD_SPECIAL_IOAPIC 1
72#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020073
Joerg Roedel6da73422009-05-04 11:44:38 +020074#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
75#define IVHD_FLAG_PASSPW_EN_MASK 0x02
76#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
77#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020078
79#define IVMD_FLAG_EXCL_RANGE 0x08
80#define IVMD_FLAG_UNITY_MAP 0x01
81
82#define ACPI_DEVFLAG_INITPASS 0x01
83#define ACPI_DEVFLAG_EXTINT 0x02
84#define ACPI_DEVFLAG_NMI 0x04
85#define ACPI_DEVFLAG_SYSMGT1 0x10
86#define ACPI_DEVFLAG_SYSMGT2 0x20
87#define ACPI_DEVFLAG_LINT0 0x40
88#define ACPI_DEVFLAG_LINT1 0x80
89#define ACPI_DEVFLAG_ATSDIS 0x10000000
90
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050091#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020092/*
93 * ACPI table definitions
94 *
95 * These data structures are laid over the table to parse the important values
96 * out of it.
97 */
98
Joerg Roedelb0119e82017-02-01 13:23:08 +010099extern const struct iommu_ops amd_iommu_ops;
100
Joerg Roedelb65233a2008-07-11 17:14:21 +0200101/*
102 * structure describing one IOMMU in the ACPI table. Typically followed by one
103 * or more ivhd_entrys.
104 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200105struct ivhd_header {
106 u8 type;
107 u8 flags;
108 u16 length;
109 u16 devid;
110 u16 cap_ptr;
111 u64 mmio_phys;
112 u16 pci_seg;
113 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400114 u32 efr_attr;
115
116 /* Following only valid on IVHD type 11h and 40h */
117 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
118 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200119} __attribute__((packed));
120
Joerg Roedelb65233a2008-07-11 17:14:21 +0200121/*
122 * A device entry describing which devices a specific IOMMU translates and
123 * which requestor ids they use.
124 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200125struct ivhd_entry {
126 u8 type;
127 u16 devid;
128 u8 flags;
129 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400130 u32 hidh;
131 u64 cid;
132 u8 uidf;
133 u8 uidl;
134 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200135} __attribute__((packed));
136
Joerg Roedelb65233a2008-07-11 17:14:21 +0200137/*
138 * An AMD IOMMU memory definition structure. It defines things like exclusion
139 * ranges for devices and regions that should be unity mapped.
140 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200141struct ivmd_header {
142 u8 type;
143 u8 flags;
144 u16 length;
145 u16 devid;
146 u16 aux;
147 u64 resv;
148 u64 range_start;
149 u64 range_length;
150} __attribute__((packed));
151
Joerg Roedelfefda112009-05-20 12:21:42 +0200152bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200153bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200154
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500155int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -0500156static int amd_iommu_xt_mode = IRQ_REMAP_X2APIC_MODE;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500157
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200158static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200159static bool __initdata amd_iommu_disabled;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400160static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200161
Joerg Roedelb65233a2008-07-11 17:14:21 +0200162u16 amd_iommu_last_bdf; /* largest PCI device id we have
163 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200164LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200165 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700166bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200167
Joerg Roedel2e228472008-07-11 17:14:31 +0200168LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200169 system */
170
Joerg Roedelbb527772009-11-20 14:31:51 +0100171/* Array to assign indices to IOMMUs*/
172struct amd_iommu *amd_iommus[MAX_IOMMUS];
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600173
174/* Number of IOMMUs present in the system */
175static int amd_iommus_present;
Joerg Roedelbb527772009-11-20 14:31:51 +0100176
Joerg Roedel318afd42009-11-23 18:32:38 +0100177/* IOMMUs have a non-present cache? */
178bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200179bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100180
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600181u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100182
Joerg Roedel400a28a2011-11-28 15:11:02 +0100183bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200184static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100185
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100186bool amd_iommu_force_isolation __read_mostly;
187
Joerg Roedelb65233a2008-07-11 17:14:21 +0200188/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100189 * List of protection domains - used during resume
190 */
191LIST_HEAD(amd_iommu_pd_list);
192spinlock_t amd_iommu_pd_lock;
193
194/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200195 * Pointer to the device table which is shared by all AMD IOMMUs
196 * it is indexed by the PCI device id or the HT unit id and contains
197 * information about the domain the device belongs to as well as the
198 * page table root pointer.
199 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200200struct dev_table_entry *amd_iommu_dev_table;
Baoquan He45a01c42017-08-09 16:33:37 +0800201/*
202 * Pointer to a device table which the content of old device table
203 * will be copied to. It's only be used in kdump kernel.
204 */
205static struct dev_table_entry *old_dev_tbl_cpy;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200206
207/*
208 * The alias table is a driver specific data structure which contains the
209 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
210 * More than one device can share the same requestor id.
211 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200212u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200213
214/*
215 * The rlookup table is used to find the IOMMU which is responsible
216 * for a specific device. It is also indexed by the PCI device id.
217 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200218struct amd_iommu **amd_iommu_rlookup_table;
Baoquan Hedaae2d22017-08-09 16:33:43 +0800219EXPORT_SYMBOL(amd_iommu_rlookup_table);
Joerg Roedelb65233a2008-07-11 17:14:21 +0200220
221/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200222 * This table is used to find the irq remapping table for a given device id
223 * quickly.
224 */
225struct irq_remap_table **irq_lookup_table;
226
227/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200228 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200229 * to know which ones are already in use.
230 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200231unsigned long *amd_iommu_pd_alloc_bitmap;
232
Joerg Roedelb65233a2008-07-11 17:14:21 +0200233static u32 dev_table_size; /* size of the device table */
234static u32 alias_table_size; /* size of the alias table */
235static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200236
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200237enum iommu_init_state {
238 IOMMU_START_STATE,
239 IOMMU_IVRS_DETECTED,
240 IOMMU_ACPI_FINISHED,
241 IOMMU_ENABLED,
242 IOMMU_PCI_INIT,
243 IOMMU_INTERRUPTS_EN,
244 IOMMU_DMA_OPS,
245 IOMMU_INITIALIZED,
246 IOMMU_NOT_FOUND,
247 IOMMU_INIT_ERROR,
Joerg Roedel1b1e9422017-06-16 16:09:56 +0200248 IOMMU_CMDLINE_DISABLED,
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200249};
250
Joerg Roedel235dacb2013-04-09 17:53:14 +0200251/* Early ioapic and hpet maps from kernel command line */
252#define EARLY_MAP_SIZE 4
253static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
254static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400255static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
256
Joerg Roedel235dacb2013-04-09 17:53:14 +0200257static int __initdata early_ioapic_map_size;
258static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400259static int __initdata early_acpihid_map_size;
260
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200261static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200262
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200263static enum iommu_init_state init_state = IOMMU_START_STATE;
264
Gerard Snitselaarae295142012-03-16 11:38:22 -0700265static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200266static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200267static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100268
Joerg Roedel2479c632017-08-19 00:35:40 +0200269static bool amd_iommu_pre_enabled = true;
Baoquan He3ac3e5ee2017-08-09 16:33:38 +0800270
Baoquan He4c232a72017-08-09 16:33:33 +0800271bool translation_pre_enabled(struct amd_iommu *iommu)
272{
273 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
274}
Baoquan Hedaae2d22017-08-09 16:33:43 +0800275EXPORT_SYMBOL(translation_pre_enabled);
Baoquan He4c232a72017-08-09 16:33:33 +0800276
277static void clear_translation_pre_enabled(struct amd_iommu *iommu)
278{
279 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
280}
281
282static void init_translation_status(struct amd_iommu *iommu)
283{
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500284 u64 ctrl;
Baoquan He4c232a72017-08-09 16:33:33 +0800285
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500286 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Baoquan He4c232a72017-08-09 16:33:33 +0800287 if (ctrl & (1<<CONTROL_IOMMU_EN))
288 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
289}
290
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200291static inline void update_last_devid(u16 devid)
292{
293 if (devid > amd_iommu_last_bdf)
294 amd_iommu_last_bdf = devid;
295}
296
Joerg Roedelc5714842008-07-11 17:14:25 +0200297static inline unsigned long tbl_size(int entry_size)
298{
299 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100300 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200301
302 return 1UL << shift;
303}
304
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600305int amd_iommu_get_num_iommus(void)
306{
307 return amd_iommus_present;
308}
309
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400310/* Access to l1 and l2 indexed register spaces */
311
312static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
313{
314 u32 val;
315
316 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
317 pci_read_config_dword(iommu->dev, 0xfc, &val);
318 return val;
319}
320
321static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
322{
323 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
324 pci_write_config_dword(iommu->dev, 0xfc, val);
325 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
326}
327
328static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
329{
330 u32 val;
331
332 pci_write_config_dword(iommu->dev, 0xf0, address);
333 pci_read_config_dword(iommu->dev, 0xf4, &val);
334 return val;
335}
336
337static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
338{
339 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
340 pci_write_config_dword(iommu->dev, 0xf4, val);
341}
342
Joerg Roedelb65233a2008-07-11 17:14:21 +0200343/****************************************************************************
344 *
345 * AMD IOMMU MMIO register space handling functions
346 *
347 * These functions are used to program the IOMMU device registers in
348 * MMIO space required for that driver.
349 *
350 ****************************************************************************/
351
352/*
353 * This function set the exclusion range in the IOMMU. DMA accesses to the
354 * exclusion range are passed through untranslated
355 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200356static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200357{
358 u64 start = iommu->exclusion_start & PAGE_MASK;
359 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
360 u64 entry;
361
362 if (!iommu->exclusion_start)
363 return;
364
365 entry = start | MMIO_EXCL_ENABLE_MASK;
366 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
367 &entry, sizeof(entry));
368
369 entry = limit;
370 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
371 &entry, sizeof(entry));
372}
373
Joerg Roedelb65233a2008-07-11 17:14:21 +0200374/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000375static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200376{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200377 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200378
379 BUG_ON(iommu->mmio_base == NULL);
380
Tom Lendacky2543a782017-07-17 16:10:24 -0500381 entry = iommu_virt_to_phys(amd_iommu_dev_table);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200382 entry |= (dev_table_size >> 12) - 1;
383 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
384 &entry, sizeof(entry));
385}
386
Joerg Roedelb65233a2008-07-11 17:14:21 +0200387/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200388static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200389{
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500390 u64 ctrl;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200391
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500392 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
393 ctrl |= (1ULL << bit);
394 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200395}
396
Joerg Roedelca0207112009-10-28 18:02:26 +0100397static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200398{
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500399 u64 ctrl;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200400
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500401 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
402 ctrl &= ~(1ULL << bit);
403 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200404}
405
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100406static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
407{
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500408 u64 ctrl;
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100409
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500410 ctrl = readq(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100411 ctrl &= ~CTRL_INV_TO_MASK;
412 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
Suravee Suthikulpanite881dbd2018-06-27 10:31:21 -0500413 writeq(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100414}
415
Joerg Roedelb65233a2008-07-11 17:14:21 +0200416/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200417static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200418{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200419 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200420}
421
Joerg Roedel92ac4322009-05-19 19:06:27 +0200422static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200423{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200424 /* Disable command buffer */
425 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
426
427 /* Disable event logging and event interrupts */
428 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
429 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
430
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500431 /* Disable IOMMU GA_LOG */
432 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
433 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
434
Chris Wrighta8c485b2009-06-15 15:53:45 +0200435 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200436 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200437}
438
Joerg Roedelb65233a2008-07-11 17:14:21 +0200439/*
440 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
441 * the system has one.
442 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500443static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200444{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500445 if (!request_mem_region(address, end, "amd_iommu")) {
446 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
447 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200448 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200449 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200450 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200451
Steven L Kinney30861dd2013-06-05 16:11:48 -0500452 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200453}
454
455static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
456{
457 if (iommu->mmio_base)
458 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500459 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200460}
461
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400462static inline u32 get_ivhd_header_size(struct ivhd_header *h)
463{
464 u32 size = 0;
465
466 switch (h->type) {
467 case 0x10:
468 size = 24;
469 break;
470 case 0x11:
471 case 0x40:
472 size = 40;
473 break;
474 }
475 return size;
476}
477
Joerg Roedelb65233a2008-07-11 17:14:21 +0200478/****************************************************************************
479 *
480 * The functions below belong to the first pass of AMD IOMMU ACPI table
481 * parsing. In this pass we try to find out the highest device id this
482 * code has to handle. Upon this information the size of the shared data
483 * structures is determined later.
484 *
485 ****************************************************************************/
486
487/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200488 * This function calculates the length of a given IVHD entry
489 */
490static inline int ivhd_entry_length(u8 *ivhd)
491{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400492 u32 type = ((struct ivhd_entry *)ivhd)->type;
493
494 if (type < 0x80) {
495 return 0x04 << (*ivhd >> 6);
496 } else if (type == IVHD_DEV_ACPI_HID) {
497 /* For ACPI_HID, offset 21 is uid len */
498 return *((u8 *)ivhd + 21) + 22;
499 }
500 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200501}
502
503/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200504 * After reading the highest device id from the IOMMU PCI capability header
505 * this function looks if there is a higher device id defined in the ACPI table
506 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200507static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
508{
509 u8 *p = (void *)h, *end = (void *)h;
510 struct ivhd_entry *dev;
511
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400512 u32 ivhd_size = get_ivhd_header_size(h);
513
514 if (!ivhd_size) {
515 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
516 return -EINVAL;
517 }
518
519 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200520 end += h->length;
521
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200522 while (p < end) {
523 dev = (struct ivhd_entry *)p;
524 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200525 case IVHD_DEV_ALL:
526 /* Use maximum BDF value for DEV_ALL */
527 update_last_devid(0xffff);
528 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200529 case IVHD_DEV_SELECT:
530 case IVHD_DEV_RANGE_END:
531 case IVHD_DEV_ALIAS:
532 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200533 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200534 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200535 break;
536 default:
537 break;
538 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200539 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200540 }
541
542 WARN_ON(p != end);
543
544 return 0;
545}
546
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400547static int __init check_ivrs_checksum(struct acpi_table_header *table)
548{
549 int i;
550 u8 checksum = 0, *p = (u8 *)table;
551
552 for (i = 0; i < table->length; ++i)
553 checksum += p[i];
554 if (checksum != 0) {
555 /* ACPI table corrupt */
556 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
557 return -ENODEV;
558 }
559
560 return 0;
561}
562
Joerg Roedelb65233a2008-07-11 17:14:21 +0200563/*
564 * Iterate over all IVHD entries in the ACPI table and find the highest device
565 * id which we need to handle. This is the first of three functions which parse
566 * the ACPI table. So we check the checksum here.
567 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200568static int __init find_last_devid_acpi(struct acpi_table_header *table)
569{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400570 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200571 struct ivhd_header *h;
572
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200573 p += IVRS_HEADER_LENGTH;
574
575 end += table->length;
576 while (p < end) {
577 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400578 if (h->type == amd_iommu_target_ivhd_type) {
579 int ret = find_last_devid_from_ivhd(h);
580
581 if (ret)
582 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200583 }
584 p += h->length;
585 }
586 WARN_ON(p != end);
587
588 return 0;
589}
590
Joerg Roedelb65233a2008-07-11 17:14:21 +0200591/****************************************************************************
592 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200593 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200594 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
595 * data structures, initialize the device/alias/rlookup table and also
596 * basically initialize the hardware.
597 *
598 ****************************************************************************/
599
600/*
601 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
602 * write commands to that buffer later and the IOMMU will execute them
603 * asynchronously
604 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200605static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200606{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200607 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
608 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200609
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200610 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200611}
612
613/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200614 * This function resets the command buffer if the IOMMU stopped fetching
615 * commands from it.
616 */
617void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
618{
619 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
620
621 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
622 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Tom Lendackyd334a562017-06-05 14:52:12 -0500623 iommu->cmd_buf_head = 0;
624 iommu->cmd_buf_tail = 0;
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200625
626 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
627}
628
629/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200630 * This function writes the command buffer address to the hardware and
631 * enables it.
632 */
633static void iommu_enable_command_buffer(struct amd_iommu *iommu)
634{
635 u64 entry;
636
637 BUG_ON(iommu->cmd_buf == NULL);
638
Tom Lendacky2543a782017-07-17 16:10:24 -0500639 entry = iommu_virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200640 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200641
Joerg Roedelb36ca912008-06-26 21:27:45 +0200642 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200643 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200644
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200645 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200646}
647
Baoquan He78d313c2017-08-09 16:33:34 +0800648/*
649 * This function disables the command buffer
650 */
651static void iommu_disable_command_buffer(struct amd_iommu *iommu)
652{
653 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
654}
655
Joerg Roedelb36ca912008-06-26 21:27:45 +0200656static void __init free_command_buffer(struct amd_iommu *iommu)
657{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200658 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200659}
660
Joerg Roedel335503e2008-09-05 14:29:07 +0200661/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200662static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200663{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200664 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
665 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200666
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200667 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200668}
669
670static void iommu_enable_event_buffer(struct amd_iommu *iommu)
671{
672 u64 entry;
673
674 BUG_ON(iommu->evt_buf == NULL);
675
Tom Lendacky2543a782017-07-17 16:10:24 -0500676 entry = iommu_virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200677
Joerg Roedel335503e2008-09-05 14:29:07 +0200678 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
679 &entry, sizeof(entry));
680
Joerg Roedel090672072009-06-15 16:06:48 +0200681 /* set head and tail to zero manually */
682 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
683 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
684
Joerg Roedel58492e12009-05-04 18:41:16 +0200685 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200686}
687
Baoquan He78d313c2017-08-09 16:33:34 +0800688/*
689 * This function disables the event log buffer
690 */
691static void iommu_disable_event_buffer(struct amd_iommu *iommu)
692{
693 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
694}
695
Joerg Roedel335503e2008-09-05 14:29:07 +0200696static void __init free_event_buffer(struct amd_iommu *iommu)
697{
698 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
699}
700
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100701/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200702static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100703{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200704 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
705 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100706
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200707 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100708}
709
710static void iommu_enable_ppr_log(struct amd_iommu *iommu)
711{
712 u64 entry;
713
714 if (iommu->ppr_log == NULL)
715 return;
716
Tom Lendacky2543a782017-07-17 16:10:24 -0500717 entry = iommu_virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100718
719 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
720 &entry, sizeof(entry));
721
722 /* set head and tail to zero manually */
723 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
724 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
725
726 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
727 iommu_feature_enable(iommu, CONTROL_PPR_EN);
728}
729
730static void __init free_ppr_log(struct amd_iommu *iommu)
731{
732 if (iommu->ppr_log == NULL)
733 return;
734
735 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
736}
737
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500738static void free_ga_log(struct amd_iommu *iommu)
739{
740#ifdef CONFIG_IRQ_REMAP
741 if (iommu->ga_log)
742 free_pages((unsigned long)iommu->ga_log,
743 get_order(GA_LOG_SIZE));
744 if (iommu->ga_log_tail)
745 free_pages((unsigned long)iommu->ga_log_tail,
746 get_order(8));
747#endif
748}
749
750static int iommu_ga_log_enable(struct amd_iommu *iommu)
751{
752#ifdef CONFIG_IRQ_REMAP
753 u32 status, i;
754
755 if (!iommu->ga_log)
756 return -EINVAL;
757
758 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
759
760 /* Check if already running */
761 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
762 return 0;
763
764 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
765 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
766
767 for (i = 0; i < LOOP_TIMEOUT; ++i) {
768 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
769 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
770 break;
771 }
772
773 if (i >= LOOP_TIMEOUT)
774 return -EINVAL;
775#endif /* CONFIG_IRQ_REMAP */
776 return 0;
777}
778
779#ifdef CONFIG_IRQ_REMAP
780static int iommu_init_ga_log(struct amd_iommu *iommu)
781{
782 u64 entry;
783
784 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
785 return 0;
786
787 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
788 get_order(GA_LOG_SIZE));
789 if (!iommu->ga_log)
790 goto err_out;
791
792 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
793 get_order(8));
794 if (!iommu->ga_log_tail)
795 goto err_out;
796
Tom Lendacky2543a782017-07-17 16:10:24 -0500797 entry = iommu_virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500798 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
799 &entry, sizeof(entry));
Tom Lendacky2543a782017-07-17 16:10:24 -0500800 entry = (iommu_virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500801 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
802 &entry, sizeof(entry));
803 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
804 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
805
806 return 0;
807err_out:
808 free_ga_log(iommu);
809 return -EINVAL;
810}
811#endif /* CONFIG_IRQ_REMAP */
812
813static int iommu_init_ga(struct amd_iommu *iommu)
814{
815 int ret = 0;
816
817#ifdef CONFIG_IRQ_REMAP
818 /* Note: We have already checked GASup from IVRS table.
819 * Now, we need to make sure that GAMSup is set.
820 */
821 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
822 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
823 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
824
825 ret = iommu_init_ga_log(iommu);
826#endif /* CONFIG_IRQ_REMAP */
827
828 return ret;
829}
830
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -0500831static void iommu_enable_xt(struct amd_iommu *iommu)
832{
833#ifdef CONFIG_IRQ_REMAP
834 /*
835 * XT mode (32-bit APIC destination ID) requires
836 * GA mode (128-bit IRTE support) as a prerequisite.
837 */
838 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
839 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
840 iommu_feature_enable(iommu, CONTROL_XT_EN);
841#endif /* CONFIG_IRQ_REMAP */
842}
843
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100844static void iommu_enable_gt(struct amd_iommu *iommu)
845{
846 if (!iommu_feature(iommu, FEATURE_GT))
847 return;
848
849 iommu_feature_enable(iommu, CONTROL_GT_EN);
850}
851
Joerg Roedelb65233a2008-07-11 17:14:21 +0200852/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200853static void set_dev_entry_bit(u16 devid, u8 bit)
854{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100855 int i = (bit >> 6) & 0x03;
856 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200857
Joerg Roedelee6c2862011-11-09 12:06:03 +0100858 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200859}
860
Joerg Roedelc5cca142009-10-09 18:31:20 +0200861static int get_dev_entry_bit(u16 devid, u8 bit)
862{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100863 int i = (bit >> 6) & 0x03;
864 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200865
Joerg Roedelee6c2862011-11-09 12:06:03 +0100866 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200867}
868
869
Baoquan He45a01c42017-08-09 16:33:37 +0800870static bool copy_device_table(void)
871{
Joerg Roedelae162ef2017-08-19 00:28:02 +0200872 u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
Baoquan He45a01c42017-08-09 16:33:37 +0800873 struct dev_table_entry *old_devtb = NULL;
874 u32 lo, hi, devid, old_devtb_size;
875 phys_addr_t old_devtb_phys;
Baoquan He45a01c42017-08-09 16:33:37 +0800876 struct amd_iommu *iommu;
Baoquan He53019a92017-08-09 16:33:39 +0800877 u16 dom_id, dte_v, irq_v;
Baoquan He45a01c42017-08-09 16:33:37 +0800878 gfp_t gfp_flag;
Baoquan Hedaae2d22017-08-09 16:33:43 +0800879 u64 tmp;
Baoquan He45a01c42017-08-09 16:33:37 +0800880
Baoquan He3ac3e5ee2017-08-09 16:33:38 +0800881 if (!amd_iommu_pre_enabled)
882 return false;
Baoquan He45a01c42017-08-09 16:33:37 +0800883
884 pr_warn("Translation is already enabled - trying to copy translation structures\n");
885 for_each_iommu(iommu) {
886 /* All IOMMUs should use the same device table with the same size */
887 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
888 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
889 entry = (((u64) hi) << 32) + lo;
890 if (last_entry && last_entry != entry) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530891 pr_err("IOMMU:%d should use the same dev table as others!\n",
Baoquan He45a01c42017-08-09 16:33:37 +0800892 iommu->index);
893 return false;
894 }
895 last_entry = entry;
896
897 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
898 if (old_devtb_size != dev_table_size) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530899 pr_err("The device table size of IOMMU:%d is not expected!\n",
Baoquan He45a01c42017-08-09 16:33:37 +0800900 iommu->index);
901 return false;
902 }
903 }
904
905 old_devtb_phys = entry & PAGE_MASK;
Baoquan Heb3367812017-08-09 16:33:42 +0800906 if (old_devtb_phys >= 0x100000000ULL) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530907 pr_err("The address of old device table is above 4G, not trustworthy!\n");
Baoquan Heb3367812017-08-09 16:33:42 +0800908 return false;
909 }
Baoquan He45a01c42017-08-09 16:33:37 +0800910 old_devtb = memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
911 if (!old_devtb)
912 return false;
913
Baoquan Heb3367812017-08-09 16:33:42 +0800914 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
Baoquan He45a01c42017-08-09 16:33:37 +0800915 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
916 get_order(dev_table_size));
917 if (old_dev_tbl_cpy == NULL) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530918 pr_err("Failed to allocate memory for copying old device table!\n");
Baoquan He45a01c42017-08-09 16:33:37 +0800919 return false;
920 }
921
922 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
923 old_dev_tbl_cpy[devid] = old_devtb[devid];
924 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
925 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
Baoquan He53019a92017-08-09 16:33:39 +0800926
927 if (dte_v && dom_id) {
928 old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
929 old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
Baoquan He45a01c42017-08-09 16:33:37 +0800930 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
Baoquan Hedaae2d22017-08-09 16:33:43 +0800931 /* If gcr3 table existed, mask it out */
932 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
933 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
934 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
935 old_dev_tbl_cpy[devid].data[1] &= ~tmp;
936 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
937 tmp |= DTE_FLAG_GV;
938 old_dev_tbl_cpy[devid].data[0] &= ~tmp;
939 }
Baoquan He53019a92017-08-09 16:33:39 +0800940 }
941
942 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
943 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
944 int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK;
945 if (irq_v && (int_ctl || int_tab_len)) {
946 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
947 (int_tab_len != DTE_IRQ_TABLE_LEN)) {
948 pr_err("Wrong old irq remapping flag: %#x\n", devid);
949 return false;
950 }
951
952 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
953 }
Baoquan He45a01c42017-08-09 16:33:37 +0800954 }
955 memunmap(old_devtb);
956
957 return true;
958}
959
Joerg Roedelc5cca142009-10-09 18:31:20 +0200960void amd_iommu_apply_erratum_63(u16 devid)
961{
962 int sysmgt;
963
964 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
965 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
966
967 if (sysmgt == 0x01)
968 set_dev_entry_bit(devid, DEV_ENTRY_IW);
969}
970
Joerg Roedel5ff47892008-07-14 20:11:18 +0200971/* Writes the specific IOMMU for a device into the rlookup table */
972static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
973{
974 amd_iommu_rlookup_table[devid] = iommu;
975}
976
Joerg Roedelb65233a2008-07-11 17:14:21 +0200977/*
978 * This function takes the device specific flags read from the ACPI
979 * table and sets up the device table entry with that information
980 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200981static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
982 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200983{
984 if (flags & ACPI_DEVFLAG_INITPASS)
985 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
986 if (flags & ACPI_DEVFLAG_EXTINT)
987 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
988 if (flags & ACPI_DEVFLAG_NMI)
989 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
990 if (flags & ACPI_DEVFLAG_SYSMGT1)
991 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
992 if (flags & ACPI_DEVFLAG_SYSMGT2)
993 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
994 if (flags & ACPI_DEVFLAG_LINT0)
995 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
996 if (flags & ACPI_DEVFLAG_LINT1)
997 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200998
Joerg Roedelc5cca142009-10-09 18:31:20 +0200999 amd_iommu_apply_erratum_63(devid);
1000
Joerg Roedel5ff47892008-07-14 20:11:18 +02001001 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +02001002}
1003
Joerg Roedelc50e3242014-09-09 15:59:37 +02001004static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +02001005{
1006 struct devid_map *entry;
1007 struct list_head *list;
1008
Joerg Roedel31cff672013-04-09 16:53:58 +02001009 if (type == IVHD_SPECIAL_IOAPIC)
1010 list = &ioapic_map;
1011 else if (type == IVHD_SPECIAL_HPET)
1012 list = &hpet_map;
1013 else
Joerg Roedel6efed632012-06-14 15:52:58 +02001014 return -EINVAL;
1015
Joerg Roedel31cff672013-04-09 16:53:58 +02001016 list_for_each_entry(entry, list, list) {
1017 if (!(entry->id == id && entry->cmd_line))
1018 continue;
1019
1020 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
1021 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1022
Joerg Roedelc50e3242014-09-09 15:59:37 +02001023 *devid = entry->devid;
1024
Joerg Roedel31cff672013-04-09 16:53:58 +02001025 return 0;
1026 }
1027
Joerg Roedel6efed632012-06-14 15:52:58 +02001028 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1029 if (!entry)
1030 return -ENOMEM;
1031
Joerg Roedel31cff672013-04-09 16:53:58 +02001032 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001033 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +02001034 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +02001035
1036 list_add_tail(&entry->list, list);
1037
1038 return 0;
1039}
1040
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001041static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1042 bool cmd_line)
1043{
1044 struct acpihid_map_entry *entry;
1045 struct list_head *list = &acpihid_map;
1046
1047 list_for_each_entry(entry, list, list) {
1048 if (strcmp(entry->hid, hid) ||
1049 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1050 !entry->cmd_line)
1051 continue;
1052
1053 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
1054 hid, uid);
1055 *devid = entry->devid;
1056 return 0;
1057 }
1058
1059 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1060 if (!entry)
1061 return -ENOMEM;
1062
1063 memcpy(entry->uid, uid, strlen(uid));
1064 memcpy(entry->hid, hid, strlen(hid));
1065 entry->devid = *devid;
1066 entry->cmd_line = cmd_line;
1067 entry->root_devid = (entry->devid & (~0x7));
1068
1069 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
1070 entry->cmd_line ? "cmd" : "ivrs",
1071 entry->hid, entry->uid, entry->root_devid);
1072
1073 list_add_tail(&entry->list, list);
1074 return 0;
1075}
1076
Joerg Roedel235dacb2013-04-09 17:53:14 +02001077static int __init add_early_maps(void)
1078{
1079 int i, ret;
1080
1081 for (i = 0; i < early_ioapic_map_size; ++i) {
1082 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1083 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +02001084 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +02001085 early_ioapic_map[i].cmd_line);
1086 if (ret)
1087 return ret;
1088 }
1089
1090 for (i = 0; i < early_hpet_map_size; ++i) {
1091 ret = add_special_device(IVHD_SPECIAL_HPET,
1092 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +02001093 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +02001094 early_hpet_map[i].cmd_line);
1095 if (ret)
1096 return ret;
1097 }
1098
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001099 for (i = 0; i < early_acpihid_map_size; ++i) {
1100 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1101 early_acpihid_map[i].uid,
1102 &early_acpihid_map[i].devid,
1103 early_acpihid_map[i].cmd_line);
1104 if (ret)
1105 return ret;
1106 }
1107
Joerg Roedel235dacb2013-04-09 17:53:14 +02001108 return 0;
1109}
1110
Joerg Roedelb65233a2008-07-11 17:14:21 +02001111/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02001112 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +02001113 * it
1114 */
Joerg Roedel3566b772008-06-26 21:27:46 +02001115static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
1116{
1117 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1118
1119 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
1120 return;
1121
1122 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +02001123 /*
1124 * We only can configure exclusion ranges per IOMMU, not
1125 * per device. But we can enable the exclusion range per
1126 * device. This is done here
1127 */
Su Friendy2c16c9f2014-05-07 13:54:52 +08001128 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +02001129 iommu->exclusion_start = m->range_start;
1130 iommu->exclusion_length = m->range_length;
1131 }
1132}
1133
Joerg Roedelb65233a2008-07-11 17:14:21 +02001134/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001135 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1136 * initializes the hardware and our data structures with it.
1137 */
Joerg Roedel6efed632012-06-14 15:52:58 +02001138static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001139 struct ivhd_header *h)
1140{
1141 u8 *p = (u8 *)h;
1142 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +02001143 u16 devid = 0, devid_start = 0, devid_to = 0;
1144 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001145 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001146 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001147 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +02001148 int ret;
1149
1150
1151 ret = add_early_maps();
1152 if (ret)
1153 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001154
1155 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001156 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001157 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001158 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001159
1160 /*
1161 * Done. Now parse the device entries
1162 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001163 ivhd_size = get_ivhd_header_size(h);
1164 if (!ivhd_size) {
1165 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1166 return -EINVAL;
1167 }
1168
1169 p += ivhd_size;
1170
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001171 end += h->length;
1172
Joerg Roedel42a698f2009-05-20 15:41:28 +02001173
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001174 while (p < end) {
1175 e = (struct ivhd_entry *)p;
1176 switch (e->type) {
1177 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001178
Joerg Roedel226e8892015-10-20 17:33:44 +02001179 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001180
Joerg Roedel226e8892015-10-20 17:33:44 +02001181 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1182 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001183 break;
1184 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001185
1186 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1187 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001188 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001189 PCI_SLOT(e->devid),
1190 PCI_FUNC(e->devid),
1191 e->flags);
1192
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001193 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001194 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001195 break;
1196 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001197
1198 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1199 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001200 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001201 PCI_SLOT(e->devid),
1202 PCI_FUNC(e->devid),
1203 e->flags);
1204
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001205 devid_start = e->devid;
1206 flags = e->flags;
1207 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001208 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001209 break;
1210 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001211
1212 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1213 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001214 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001215 PCI_SLOT(e->devid),
1216 PCI_FUNC(e->devid),
1217 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001218 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001219 PCI_SLOT(e->ext >> 8),
1220 PCI_FUNC(e->ext >> 8));
1221
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001222 devid = e->devid;
1223 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001224 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001225 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001226 amd_iommu_alias_table[devid] = devid_to;
1227 break;
1228 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001229
1230 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1231 "devid: %02x:%02x.%x flags: %02x "
1232 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001233 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001234 PCI_SLOT(e->devid),
1235 PCI_FUNC(e->devid),
1236 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001237 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001238 PCI_SLOT(e->ext >> 8),
1239 PCI_FUNC(e->ext >> 8));
1240
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001241 devid_start = e->devid;
1242 flags = e->flags;
1243 devid_to = e->ext >> 8;
1244 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001245 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001246 break;
1247 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001248
1249 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1250 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001251 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001252 PCI_SLOT(e->devid),
1253 PCI_FUNC(e->devid),
1254 e->flags, e->ext);
1255
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001256 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001257 set_dev_entry_from_acpi(iommu, devid, e->flags,
1258 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001259 break;
1260 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001261
1262 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1263 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001264 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001265 PCI_SLOT(e->devid),
1266 PCI_FUNC(e->devid),
1267 e->flags, e->ext);
1268
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001269 devid_start = e->devid;
1270 flags = e->flags;
1271 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001272 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001273 break;
1274 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001275
1276 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001277 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001278 PCI_SLOT(e->devid),
1279 PCI_FUNC(e->devid));
1280
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001281 devid = e->devid;
1282 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001283 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001284 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001285 set_dev_entry_from_acpi(iommu,
1286 devid_to, flags, ext_flags);
1287 }
1288 set_dev_entry_from_acpi(iommu, dev_i,
1289 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001290 }
1291 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001292 case IVHD_DEV_SPECIAL: {
1293 u8 handle, type;
1294 const char *var;
1295 u16 devid;
1296 int ret;
1297
1298 handle = e->ext & 0xff;
1299 devid = (e->ext >> 8) & 0xffff;
1300 type = (e->ext >> 24) & 0xff;
1301
1302 if (type == IVHD_SPECIAL_IOAPIC)
1303 var = "IOAPIC";
1304 else if (type == IVHD_SPECIAL_HPET)
1305 var = "HPET";
1306 else
1307 var = "UNKNOWN";
1308
1309 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1310 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001311 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001312 PCI_SLOT(devid),
1313 PCI_FUNC(devid));
1314
Joerg Roedelc50e3242014-09-09 15:59:37 +02001315 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001316 if (ret)
1317 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001318
1319 /*
1320 * add_special_device might update the devid in case a
1321 * command-line override is present. So call
1322 * set_dev_entry_from_acpi after add_special_device.
1323 */
1324 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1325
Joerg Roedel6efed632012-06-14 15:52:58 +02001326 break;
1327 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001328 case IVHD_DEV_ACPI_HID: {
1329 u16 devid;
1330 u8 hid[ACPIHID_HID_LEN] = {0};
1331 u8 uid[ACPIHID_UID_LEN] = {0};
1332 int ret;
1333
1334 if (h->type != 0x40) {
1335 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1336 e->type);
1337 break;
1338 }
1339
1340 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1341 hid[ACPIHID_HID_LEN - 1] = '\0';
1342
1343 if (!(*hid)) {
1344 pr_err(FW_BUG "Invalid HID.\n");
1345 break;
1346 }
1347
1348 switch (e->uidf) {
1349 case UID_NOT_PRESENT:
1350
1351 if (e->uidl != 0)
1352 pr_warn(FW_BUG "Invalid UID length.\n");
1353
1354 break;
1355 case UID_IS_INTEGER:
1356
1357 sprintf(uid, "%d", e->uid);
1358
1359 break;
1360 case UID_IS_CHARACTER:
1361
1362 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1363 uid[ACPIHID_UID_LEN - 1] = '\0';
1364
1365 break;
1366 default:
1367 break;
1368 }
1369
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001370 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001371 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1372 hid, uid,
1373 PCI_BUS_NUM(devid),
1374 PCI_SLOT(devid),
1375 PCI_FUNC(devid));
1376
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001377 flags = e->flags;
1378
1379 ret = add_acpi_hid_device(hid, uid, &devid, false);
1380 if (ret)
1381 return ret;
1382
1383 /*
1384 * add_special_device might update the devid in case a
1385 * command-line override is present. So call
1386 * set_dev_entry_from_acpi after add_special_device.
1387 */
1388 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1389
1390 break;
1391 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001392 default:
1393 break;
1394 }
1395
Joerg Roedelb514e552008-09-17 17:14:27 +02001396 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001397 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001398
1399 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001400}
1401
Joerg Roedele47d4022008-06-26 21:27:48 +02001402static void __init free_iommu_one(struct amd_iommu *iommu)
1403{
1404 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001405 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001406 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001407 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001408 iommu_unmap_mmio_space(iommu);
1409}
1410
1411static void __init free_iommu_all(void)
1412{
1413 struct amd_iommu *iommu, *next;
1414
Joerg Roedel3bd22172009-05-04 15:06:20 +02001415 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001416 list_del(&iommu->list);
1417 free_iommu_one(iommu);
1418 kfree(iommu);
1419 }
1420}
1421
Joerg Roedelb65233a2008-07-11 17:14:21 +02001422/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001423 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1424 * Workaround:
1425 * BIOS should disable L2B micellaneous clock gating by setting
1426 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1427 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001428static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001429{
1430 u32 value;
1431
1432 if ((boot_cpu_data.x86 != 0x15) ||
1433 (boot_cpu_data.x86_model < 0x10) ||
1434 (boot_cpu_data.x86_model > 0x1f))
1435 return;
1436
1437 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1438 pci_read_config_dword(iommu->dev, 0xf4, &value);
1439
1440 if (value & BIT(2))
1441 return;
1442
1443 /* Select NB indirect register 0x90 and enable writing */
1444 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1445
1446 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1447 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1448 dev_name(&iommu->dev->dev));
1449
1450 /* Clear the enable writing bit */
1451 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1452}
1453
1454/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001455 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1456 * Workaround:
1457 * BIOS should enable ATS write permission check by setting
1458 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1459 */
1460static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1461{
1462 u32 value;
1463
1464 if ((boot_cpu_data.x86 != 0x15) ||
1465 (boot_cpu_data.x86_model < 0x30) ||
1466 (boot_cpu_data.x86_model > 0x3f))
1467 return;
1468
1469 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1470 value = iommu_read_l2(iommu, 0x47);
1471
1472 if (value & BIT(0))
1473 return;
1474
1475 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1476 iommu_write_l2(iommu, 0x47, value | BIT(0));
1477
1478 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1479 dev_name(&iommu->dev->dev));
1480}
1481
1482/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001483 * This function clues the initialization function for one IOMMU
1484 * together and also allocates the command buffer and programs the
1485 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1486 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001487static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1488{
Joerg Roedel6efed632012-06-14 15:52:58 +02001489 int ret;
1490
Scott Wood27790392018-01-21 03:28:54 -06001491 raw_spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001492
1493 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001494 list_add_tail(&iommu->list, &amd_iommu_list);
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001495 iommu->index = amd_iommus_present++;
Joerg Roedelbb527772009-11-20 14:31:51 +01001496
1497 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1498 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1499 return -ENOSYS;
1500 }
1501
1502 /* Index is fine - add IOMMU to the array */
1503 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001504
1505 /*
1506 * Copy data from ACPI table entry to the iommu struct
1507 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001508 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001509 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001510 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001511 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001512
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001513 switch (h->type) {
1514 case 0x10:
1515 /* Check if IVHD EFR contains proper max banks/counters */
1516 if ((h->efr_attr != 0) &&
1517 ((h->efr_attr & (0xF << 13)) != 0) &&
1518 ((h->efr_attr & (0x3F << 17)) != 0))
1519 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1520 else
1521 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001522 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1523 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05001524 if (((h->efr_attr & (0x1 << IOMMU_FEAT_XTSUP_SHIFT)) == 0))
1525 amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001526 break;
1527 case 0x11:
1528 case 0x40:
1529 if (h->efr_reg & (1 << 9))
1530 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1531 else
1532 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001533 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1534 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05001535 if (((h->efr_reg & (0x1 << IOMMU_EFR_XTSUP_SHIFT)) == 0))
1536 amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001537 break;
1538 default:
1539 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001540 }
1541
1542 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1543 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001544 if (!iommu->mmio_base)
1545 return -ENOMEM;
1546
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001547 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001548 return -ENOMEM;
1549
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001550 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001551 return -ENOMEM;
1552
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001553 iommu->int_enabled = false;
1554
Baoquan He4c232a72017-08-09 16:33:33 +08001555 init_translation_status(iommu);
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08001556 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1557 iommu_disable(iommu);
1558 clear_translation_pre_enabled(iommu);
1559 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1560 iommu->index);
1561 }
1562 if (amd_iommu_pre_enabled)
1563 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
Baoquan He4c232a72017-08-09 16:33:33 +08001564
Joerg Roedel6efed632012-06-14 15:52:58 +02001565 ret = init_iommu_from_acpi(iommu, h);
1566 if (ret)
1567 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001568
Jiang Liu7c71d302015-04-13 14:11:33 +08001569 ret = amd_iommu_create_irq_domain(iommu);
1570 if (ret)
1571 return ret;
1572
Joerg Roedelf6fec002012-06-21 16:51:25 +02001573 /*
1574 * Make sure IOMMU is not considered to translate itself. The IVRS
1575 * table tells us so, but this is a lie!
1576 */
1577 amd_iommu_rlookup_table[iommu->devid] = NULL;
1578
Joerg Roedel23c742d2012-06-12 11:47:34 +02001579 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001580}
1581
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001582/**
1583 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1584 * @ivrs Pointer to the IVRS header
1585 *
1586 * This function search through all IVDB of the maximum supported IVHD
1587 */
1588static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1589{
1590 u8 *base = (u8 *)ivrs;
1591 struct ivhd_header *ivhd = (struct ivhd_header *)
1592 (base + IVRS_HEADER_LENGTH);
1593 u8 last_type = ivhd->type;
1594 u16 devid = ivhd->devid;
1595
1596 while (((u8 *)ivhd - base < ivrs->length) &&
1597 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1598 u8 *p = (u8 *) ivhd;
1599
1600 if (ivhd->devid == devid)
1601 last_type = ivhd->type;
1602 ivhd = (struct ivhd_header *)(p + ivhd->length);
1603 }
1604
1605 return last_type;
1606}
1607
Joerg Roedelb65233a2008-07-11 17:14:21 +02001608/*
1609 * Iterates over all IOMMU entries in the ACPI table, allocates the
1610 * IOMMU structure and initializes it with init_iommu_one()
1611 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001612static int __init init_iommu_all(struct acpi_table_header *table)
1613{
1614 u8 *p = (u8 *)table, *end = (u8 *)table;
1615 struct ivhd_header *h;
1616 struct amd_iommu *iommu;
1617 int ret;
1618
Joerg Roedele47d4022008-06-26 21:27:48 +02001619 end += table->length;
1620 p += IVRS_HEADER_LENGTH;
1621
1622 while (p < end) {
1623 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001624 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001625
Joerg Roedelae908c22009-09-01 16:52:16 +02001626 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001627 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001628 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001629 PCI_FUNC(h->devid), h->cap_ptr,
1630 h->pci_seg, h->flags, h->info);
1631 DUMP_printk(" mmio-addr: %016llx\n",
1632 h->mmio_phys);
1633
Joerg Roedele47d4022008-06-26 21:27:48 +02001634 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001635 if (iommu == NULL)
1636 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001637
Joerg Roedele47d4022008-06-26 21:27:48 +02001638 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001639 if (ret)
1640 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001641 }
1642 p += h->length;
1643
1644 }
1645 WARN_ON(p != end);
1646
1647 return 0;
1648}
1649
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001650static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1651 u8 fxn, u64 *value, bool is_write);
Steven L Kinney30861dd2013-06-05 16:11:48 -05001652
1653static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1654{
1655 u64 val = 0xabcd, val2 = 0;
1656
1657 if (!iommu_feature(iommu, FEATURE_PC))
1658 return;
1659
1660 amd_iommu_pc_present = true;
1661
1662 /* Check if the performance counters can be written to */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001663 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1664 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001665 (val != val2)) {
1666 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1667 amd_iommu_pc_present = false;
1668 return;
1669 }
1670
1671 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1672
1673 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1674 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1675 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1676}
1677
Alex Williamson066f2e92014-06-12 16:12:37 -06001678static ssize_t amd_iommu_show_cap(struct device *dev,
1679 struct device_attribute *attr,
1680 char *buf)
1681{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001682 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001683 return sprintf(buf, "%x\n", iommu->cap);
1684}
1685static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1686
1687static ssize_t amd_iommu_show_features(struct device *dev,
1688 struct device_attribute *attr,
1689 char *buf)
1690{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001691 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001692 return sprintf(buf, "%llx\n", iommu->features);
1693}
1694static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1695
1696static struct attribute *amd_iommu_attrs[] = {
1697 &dev_attr_cap.attr,
1698 &dev_attr_features.attr,
1699 NULL,
1700};
1701
1702static struct attribute_group amd_iommu_group = {
1703 .name = "amd-iommu",
1704 .attrs = amd_iommu_attrs,
1705};
1706
1707static const struct attribute_group *amd_iommu_groups[] = {
1708 &amd_iommu_group,
1709 NULL,
1710};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001711
Joerg Roedel23c742d2012-06-12 11:47:34 +02001712static int iommu_init_pci(struct amd_iommu *iommu)
1713{
1714 int cap_ptr = iommu->cap_ptr;
1715 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001716 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001717
Sinan Kayad5bf0f42017-12-19 00:37:47 -05001718 iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
1719 iommu->devid & 0xff);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001720 if (!iommu->dev)
1721 return -ENODEV;
1722
Jiang Liucbbc00b2015-10-09 22:07:31 +08001723 /* Prevent binding other PCI device drivers to IOMMU devices */
1724 iommu->dev->match_driver = false;
1725
Joerg Roedel23c742d2012-06-12 11:47:34 +02001726 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1727 &iommu->cap);
1728 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1729 &range);
1730 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1731 &misc);
1732
Joerg Roedel23c742d2012-06-12 11:47:34 +02001733 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1734 amd_iommu_iotlb_sup = false;
1735
1736 /* read extended feature bits */
1737 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1738 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1739
1740 iommu->features = ((u64)high << 32) | low;
1741
1742 if (iommu_feature(iommu, FEATURE_GT)) {
1743 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001744 u32 max_pasid;
1745 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001746
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001747 pasmax = iommu->features & FEATURE_PASID_MASK;
1748 pasmax >>= FEATURE_PASID_SHIFT;
1749 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001750
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001751 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1752
1753 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001754
1755 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1756 glxval >>= FEATURE_GLXVAL_SHIFT;
1757
1758 if (amd_iommu_max_glx_val == -1)
1759 amd_iommu_max_glx_val = glxval;
1760 else
1761 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1762 }
1763
1764 if (iommu_feature(iommu, FEATURE_GT) &&
1765 iommu_feature(iommu, FEATURE_PPR)) {
1766 iommu->is_iommu_v2 = true;
1767 amd_iommu_v2_present = true;
1768 }
1769
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001770 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1771 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001772
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001773 ret = iommu_init_ga(iommu);
1774 if (ret)
1775 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001776
Joerg Roedel23c742d2012-06-12 11:47:34 +02001777 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1778 amd_iommu_np_cache = true;
1779
Steven L Kinney30861dd2013-06-05 16:11:48 -05001780 init_iommu_perf_ctr(iommu);
1781
Joerg Roedel23c742d2012-06-12 11:47:34 +02001782 if (is_rd890_iommu(iommu->dev)) {
1783 int i, j;
1784
Sinan Kayad5bf0f42017-12-19 00:37:47 -05001785 iommu->root_pdev =
1786 pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
1787 PCI_DEVFN(0, 0));
Joerg Roedel23c742d2012-06-12 11:47:34 +02001788
1789 /*
1790 * Some rd890 systems may not be fully reconfigured by the
1791 * BIOS, so it's necessary for us to store this information so
1792 * it can be reprogrammed on resume
1793 */
1794 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1795 &iommu->stored_addr_lo);
1796 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1797 &iommu->stored_addr_hi);
1798
1799 /* Low bit locks writes to configuration space */
1800 iommu->stored_addr_lo &= ~1;
1801
1802 for (i = 0; i < 6; i++)
1803 for (j = 0; j < 0x12; j++)
1804 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1805
1806 for (i = 0; i < 0x83; i++)
1807 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1808 }
1809
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001810 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001811 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001812
Joerg Roedel39ab9552017-02-01 16:56:46 +01001813 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1814 amd_iommu_groups, "ivhd%d", iommu->index);
Joerg Roedelb0119e82017-02-01 13:23:08 +01001815 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1816 iommu_device_register(&iommu->iommu);
Alex Williamson066f2e92014-06-12 16:12:37 -06001817
Joerg Roedel23c742d2012-06-12 11:47:34 +02001818 return pci_enable_device(iommu->dev);
1819}
1820
Joerg Roedel4d121c32012-06-14 12:21:55 +02001821static void print_iommu_info(void)
1822{
1823 static const char * const feat_str[] = {
1824 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1825 "IA", "GA", "HE", "PC"
1826 };
1827 struct amd_iommu *iommu;
1828
1829 for_each_iommu(iommu) {
1830 int i;
1831
1832 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1833 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1834
1835 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001836 pr_info("AMD-Vi: Extended features (%#llx):\n",
1837 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001838 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001839 if (iommu_feature(iommu, (1ULL << i)))
1840 pr_cont(" %s", feat_str[i]);
1841 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001842
1843 if (iommu->features & FEATURE_GAM_VAPIC)
1844 pr_cont(" GA_vAPIC");
1845
Steven L Kinney30861dd2013-06-05 16:11:48 -05001846 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001847 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001848 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001849 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001850 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001851 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1852 pr_info("AMD-Vi: virtual APIC enabled\n");
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05001853 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
1854 pr_info("AMD-Vi: X2APIC enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001855 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001856}
1857
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001858static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001859{
1860 struct amd_iommu *iommu;
1861 int ret = 0;
1862
1863 for_each_iommu(iommu) {
1864 ret = iommu_init_pci(iommu);
1865 if (ret)
1866 break;
1867 }
1868
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001869 /*
1870 * Order is important here to make sure any unity map requirements are
1871 * fulfilled. The unity mappings are created and written to the device
1872 * table during the amd_iommu_init_api() call.
1873 *
1874 * After that we call init_device_table_dma() to make sure any
1875 * uninitialized DTE will block DMA, and in the end we flush the caches
1876 * of all IOMMUs to make sure the changes to the device table are
1877 * active.
1878 */
1879 ret = amd_iommu_init_api();
1880
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001881 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001882
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001883 for_each_iommu(iommu)
1884 iommu_flush_all_caches(iommu);
1885
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001886 if (!ret)
1887 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001888
Joerg Roedel23c742d2012-06-12 11:47:34 +02001889 return ret;
1890}
1891
Joerg Roedelb65233a2008-07-11 17:14:21 +02001892/****************************************************************************
1893 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001894 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001895 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001896 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1897 * pci_dev.
1898 *
1899 ****************************************************************************/
1900
Joerg Roedel9f800de2009-11-23 12:45:25 +01001901static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001902{
1903 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001904
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001905 r = pci_enable_msi(iommu->dev);
1906 if (r)
1907 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001908
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001909 r = request_threaded_irq(iommu->dev->irq,
1910 amd_iommu_int_handler,
1911 amd_iommu_int_thread,
1912 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001913 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001914
1915 if (r) {
1916 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001917 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001918 }
1919
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001920 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001921
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001922 return 0;
1923}
1924
Joerg Roedel05f92db2009-05-12 09:52:46 +02001925static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001926{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001927 int ret;
1928
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001929 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001930 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001931
Yijing Wang82fcfc62013-08-08 21:12:36 +08001932 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001933 ret = iommu_setup_msi(iommu);
1934 else
1935 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001936
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001937 if (ret)
1938 return ret;
1939
1940enable_faults:
1941 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1942
1943 if (iommu->ppr_log != NULL)
1944 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1945
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001946 iommu_ga_log_enable(iommu);
1947
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001948 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001949}
1950
1951/****************************************************************************
1952 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001953 * The next functions belong to the third pass of parsing the ACPI
1954 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001955 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001956 *
1957 ****************************************************************************/
1958
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001959static void __init free_unity_maps(void)
1960{
1961 struct unity_map_entry *entry, *next;
1962
1963 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1964 list_del(&entry->list);
1965 kfree(entry);
1966 }
1967}
1968
Joerg Roedelb65233a2008-07-11 17:14:21 +02001969/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001970static int __init init_exclusion_range(struct ivmd_header *m)
1971{
1972 int i;
1973
1974 switch (m->type) {
1975 case ACPI_IVMD_TYPE:
1976 set_device_exclusion_range(m->devid, m);
1977 break;
1978 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001979 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001980 set_device_exclusion_range(i, m);
1981 break;
1982 case ACPI_IVMD_TYPE_RANGE:
1983 for (i = m->devid; i <= m->aux; ++i)
1984 set_device_exclusion_range(i, m);
1985 break;
1986 default:
1987 break;
1988 }
1989
1990 return 0;
1991}
1992
Joerg Roedelb65233a2008-07-11 17:14:21 +02001993/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001994static int __init init_unity_map_range(struct ivmd_header *m)
1995{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001996 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001997 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001998
1999 e = kzalloc(sizeof(*e), GFP_KERNEL);
2000 if (e == NULL)
2001 return -ENOMEM;
2002
2003 switch (m->type) {
2004 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02002005 kfree(e);
2006 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002007 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02002008 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002009 e->devid_start = e->devid_end = m->devid;
2010 break;
2011 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02002012 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002013 e->devid_start = 0;
2014 e->devid_end = amd_iommu_last_bdf;
2015 break;
2016 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02002017 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002018 e->devid_start = m->devid;
2019 e->devid_end = m->aux;
2020 break;
2021 }
2022 e->address_start = PAGE_ALIGN(m->range_start);
2023 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2024 e->prot = m->flags >> 1;
2025
Joerg Roedel02acc432009-05-20 16:24:21 +02002026 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2027 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07002028 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2029 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02002030 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2031 e->address_start, e->address_end, m->flags);
2032
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002033 list_add_tail(&e->list, &amd_iommu_unity_map);
2034
2035 return 0;
2036}
2037
Joerg Roedelb65233a2008-07-11 17:14:21 +02002038/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002039static int __init init_memory_definitions(struct acpi_table_header *table)
2040{
2041 u8 *p = (u8 *)table, *end = (u8 *)table;
2042 struct ivmd_header *m;
2043
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002044 end += table->length;
2045 p += IVRS_HEADER_LENGTH;
2046
2047 while (p < end) {
2048 m = (struct ivmd_header *)p;
2049 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2050 init_exclusion_range(m);
2051 else if (m->flags & IVMD_FLAG_UNITY_MAP)
2052 init_unity_map_range(m);
2053
2054 p += m->length;
2055 }
2056
2057 return 0;
2058}
2059
Joerg Roedelb65233a2008-07-11 17:14:21 +02002060/*
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002061 * Init the device table to not allow DMA access for devices
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002062 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02002063static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002064{
Joerg Roedel0de66d52011-06-06 16:04:02 +02002065 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002066
2067 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2068 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2069 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002070 }
2071}
2072
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002073static void __init uninit_device_table_dma(void)
2074{
2075 u32 devid;
2076
2077 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2078 amd_iommu_dev_table[devid].data[0] = 0ULL;
2079 amd_iommu_dev_table[devid].data[1] = 0ULL;
2080 }
2081}
2082
Joerg Roedel33f28c52012-06-15 18:03:31 +02002083static void init_device_table(void)
2084{
2085 u32 devid;
2086
2087 if (!amd_iommu_irq_remap)
2088 return;
2089
2090 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2091 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2092}
2093
Joerg Roedele9bf5192010-09-20 14:33:07 +02002094static void iommu_init_flags(struct amd_iommu *iommu)
2095{
2096 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2097 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2098 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2099
2100 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2101 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2102 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2103
2104 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2105 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2106 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2107
2108 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2109 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2110 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2111
2112 /*
2113 * make IOMMU memory accesses cache coherent
2114 */
2115 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01002116
2117 /* Set IOTLB invalidation timeout to 1s */
2118 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002119}
2120
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002121static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02002122{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002123 int i, j;
2124 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002125 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002126
2127 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002128 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002129 return;
2130
2131 /*
2132 * First, we need to ensure that the iommu is enabled. This is
2133 * controlled by a register in the northbridge
2134 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002135
2136 /* Select Northbridge indirect register 0x75 and enable writing */
2137 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2138 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2139
2140 /* Enable the iommu */
2141 if (!(ioc_feature_control & 0x1))
2142 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2143
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002144 /* Restore the iommu BAR */
2145 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2146 iommu->stored_addr_lo);
2147 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2148 iommu->stored_addr_hi);
2149
2150 /* Restore the l1 indirect regs for each of the 6 l1s */
2151 for (i = 0; i < 6; i++)
2152 for (j = 0; j < 0x12; j++)
2153 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2154
2155 /* Restore the l2 indirect regs */
2156 for (i = 0; i < 0x83; i++)
2157 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2158
2159 /* Lock PCI setup registers */
2160 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2161 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02002162}
2163
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002164static void iommu_enable_ga(struct amd_iommu *iommu)
2165{
2166#ifdef CONFIG_IRQ_REMAP
2167 switch (amd_iommu_guest_ir) {
2168 case AMD_IOMMU_GUEST_IR_VAPIC:
2169 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2170 /* Fall through */
2171 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2172 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002173 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002174 break;
2175 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002176 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002177 break;
2178 }
2179#endif
2180}
2181
Baoquan He78d313c2017-08-09 16:33:34 +08002182static void early_enable_iommu(struct amd_iommu *iommu)
2183{
2184 iommu_disable(iommu);
2185 iommu_init_flags(iommu);
2186 iommu_set_device_table(iommu);
2187 iommu_enable_command_buffer(iommu);
2188 iommu_enable_event_buffer(iommu);
2189 iommu_set_exclusion_range(iommu);
2190 iommu_enable_ga(iommu);
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05002191 iommu_enable_xt(iommu);
Baoquan He78d313c2017-08-09 16:33:34 +08002192 iommu_enable(iommu);
2193 iommu_flush_all_caches(iommu);
2194}
2195
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002196/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002197 * This function finally enables all IOMMUs found in the system after
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002198 * they have been initialized.
2199 *
2200 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2201 * the old content of device table entries. Not this case or copy failed,
2202 * just continue as normal kernel does.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002203 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002204static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002205{
2206 struct amd_iommu *iommu;
2207
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002208
2209 if (!copy_device_table()) {
2210 /*
2211 * If come here because of failure in copying device table from old
2212 * kernel with all IOMMUs enabled, print error message and try to
2213 * free allocated old_dev_tbl_cpy.
2214 */
2215 if (amd_iommu_pre_enabled)
2216 pr_err("Failed to copy DEV table from previous kernel.\n");
2217 if (old_dev_tbl_cpy != NULL)
2218 free_pages((unsigned long)old_dev_tbl_cpy,
2219 get_order(dev_table_size));
2220
2221 for_each_iommu(iommu) {
2222 clear_translation_pre_enabled(iommu);
2223 early_enable_iommu(iommu);
2224 }
2225 } else {
2226 pr_info("Copied DEV table from previous kernel.\n");
2227 free_pages((unsigned long)amd_iommu_dev_table,
2228 get_order(dev_table_size));
2229 amd_iommu_dev_table = old_dev_tbl_cpy;
2230 for_each_iommu(iommu) {
2231 iommu_disable_command_buffer(iommu);
2232 iommu_disable_event_buffer(iommu);
2233 iommu_enable_command_buffer(iommu);
2234 iommu_enable_event_buffer(iommu);
2235 iommu_enable_ga(iommu);
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05002236 iommu_enable_xt(iommu);
Baoquan He3ac3e5ee2017-08-09 16:33:38 +08002237 iommu_set_device_table(iommu);
2238 iommu_flush_all_caches(iommu);
2239 }
Joerg Roedel87361972008-06-26 21:28:07 +02002240 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002241
2242#ifdef CONFIG_IRQ_REMAP
2243 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2244 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2245#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002246}
2247
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002248static void enable_iommus_v2(void)
2249{
2250 struct amd_iommu *iommu;
2251
2252 for_each_iommu(iommu) {
2253 iommu_enable_ppr_log(iommu);
2254 iommu_enable_gt(iommu);
2255 }
2256}
2257
2258static void enable_iommus(void)
2259{
2260 early_enable_iommus();
2261
2262 enable_iommus_v2();
2263}
2264
Joerg Roedel92ac4322009-05-19 19:06:27 +02002265static void disable_iommus(void)
2266{
2267 struct amd_iommu *iommu;
2268
2269 for_each_iommu(iommu)
2270 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002271
2272#ifdef CONFIG_IRQ_REMAP
2273 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2274 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2275#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002276}
2277
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002278/*
2279 * Suspend/Resume support
2280 * disable suspend until real resume implemented
2281 */
2282
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002283static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002284{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002285 struct amd_iommu *iommu;
2286
2287 for_each_iommu(iommu)
2288 iommu_apply_resume_quirks(iommu);
2289
Joerg Roedel736501e2009-05-12 09:56:12 +02002290 /* re-load the hardware */
2291 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002292
2293 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002294}
2295
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002296static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002297{
Joerg Roedel736501e2009-05-12 09:56:12 +02002298 /* disable IOMMUs to go out of the way for BIOS */
2299 disable_iommus();
2300
2301 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002302}
2303
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002304static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002305 .suspend = amd_iommu_suspend,
2306 .resume = amd_iommu_resume,
2307};
2308
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002309static void __init free_iommu_resources(void)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002310{
Lucas Stachebcfa282016-10-26 13:09:53 +02002311 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002312 free_pages((unsigned long)irq_lookup_table,
2313 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002314 irq_lookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002315
Julia Lawalla5919892015-09-13 14:15:31 +02002316 kmem_cache_destroy(amd_iommu_irq_cache);
2317 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002318
2319 free_pages((unsigned long)amd_iommu_rlookup_table,
2320 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002321 amd_iommu_rlookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002322
2323 free_pages((unsigned long)amd_iommu_alias_table,
2324 get_order(alias_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002325 amd_iommu_alias_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002326
2327 free_pages((unsigned long)amd_iommu_dev_table,
2328 get_order(dev_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002329 amd_iommu_dev_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002330
2331 free_iommu_all();
2332
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002333#ifdef CONFIG_GART_IOMMU
2334 /*
2335 * We failed to initialize the AMD IOMMU - try fallback to GART
2336 * if possible.
2337 */
2338 gart_iommu_init();
2339
2340#endif
2341}
2342
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002343/* SB IOAPIC is always on this device in AMD systems */
2344#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2345
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002346static bool __init check_ioapic_information(void)
2347{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002348 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002349 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002350 int idx;
2351
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002352 has_sb_ioapic = false;
2353 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002354
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002355 /*
2356 * If we have map overrides on the kernel command line the
2357 * messages in this function might not describe firmware bugs
2358 * anymore - so be careful
2359 */
2360 if (cmdline_maps)
2361 fw_bug = "";
2362
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002363 for (idx = 0; idx < nr_ioapics; idx++) {
2364 int devid, id = mpc_ioapic_id(idx);
2365
2366 devid = get_ioapic_devid(id);
2367 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002368 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2369 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002370 ret = false;
2371 } else if (devid == IOAPIC_SB_DEVID) {
2372 has_sb_ioapic = true;
2373 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002374 }
2375 }
2376
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002377 if (!has_sb_ioapic) {
2378 /*
2379 * We expect the SB IOAPIC to be listed in the IVRS
2380 * table. The system timer is connected to the SB IOAPIC
2381 * and if we don't have it in the list the system will
2382 * panic at boot time. This situation usually happens
2383 * when the BIOS is buggy and provides us the wrong
2384 * device id for the IOAPIC in the system.
2385 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002386 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002387 }
2388
2389 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002390 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002391
2392 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002393}
2394
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002395static void __init free_dma_resources(void)
2396{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002397 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2398 get_order(MAX_DOMAIN_ID/8));
Joerg Roedelf6019272017-06-16 16:09:58 +02002399 amd_iommu_pd_alloc_bitmap = NULL;
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002400
2401 free_unity_maps();
2402}
2403
Joerg Roedelb65233a2008-07-11 17:14:21 +02002404/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002405 * This is the hardware init function for AMD IOMMU in the system.
2406 * This function is called either from amd_iommu_init or from the interrupt
2407 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002408 *
2409 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002410 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002411 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002412 * 1 pass) Discover the most comprehensive IVHD type to use.
2413 *
2414 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002415 * Upon this information the size of the data structures is
2416 * determined that needs to be allocated.
2417 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002418 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002419 * information in the ACPI table about available AMD IOMMUs
2420 * in the system. It also maps the PCI devices in the
2421 * system to specific IOMMUs
2422 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002423 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002424 * initialized we update them with information about memory
2425 * remapping requirements parsed out of the ACPI table in
2426 * this last pass.
2427 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002428 * After everything is set up the IOMMUs are enabled and the necessary
2429 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002430 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002431static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002432{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002433 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002434 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002435 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002436
Joerg Roedel643511b2012-06-12 12:09:35 +02002437 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002438 return -ENODEV;
2439
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002440 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002441 if (status == AE_NOT_FOUND)
2442 return -ENODEV;
2443 else if (ACPI_FAILURE(status)) {
2444 const char *err = acpi_format_exception(status);
2445 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2446 return -EINVAL;
2447 }
2448
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002449 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002450 * Validate checksum here so we don't need to do it when
2451 * we actually parse the table
2452 */
2453 ret = check_ivrs_checksum(ivrs_base);
2454 if (ret)
Rafael J. Wysocki99e8ccd2017-01-10 14:57:28 +01002455 goto out;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002456
2457 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2458 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2459
2460 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002461 * First parse ACPI tables to find the largest Bus/Dev/Func
2462 * we need to handle. Upon this information the shared data
2463 * structures for the IOMMUs in the system will be allocated
2464 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002465 ret = find_last_devid_acpi(ivrs_base);
2466 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002467 goto out;
2468
Joerg Roedelc5714842008-07-11 17:14:25 +02002469 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2470 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2471 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002472
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002473 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002474 ret = -ENOMEM;
Baoquan Heb3367812017-08-09 16:33:42 +08002475 amd_iommu_dev_table = (void *)__get_free_pages(
2476 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002477 get_order(dev_table_size));
2478 if (amd_iommu_dev_table == NULL)
2479 goto out;
2480
2481 /*
2482 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2483 * IOMMU see for that device
2484 */
2485 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2486 get_order(alias_table_size));
2487 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002488 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002489
2490 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002491 amd_iommu_rlookup_table = (void *)__get_free_pages(
2492 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002493 get_order(rlookup_table_size));
2494 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002495 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002496
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002497 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2498 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002499 get_order(MAX_DOMAIN_ID/8));
2500 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002501 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002502
2503 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002504 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002505 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002506 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002507 amd_iommu_alias_table[i] = i;
2508
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002509 /*
2510 * never allocate domain 0 because its used as the non-allocated and
2511 * error value placeholder
2512 */
Baoquan He5c87f622016-09-15 16:50:51 +08002513 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002514
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002515 spin_lock_init(&amd_iommu_pd_lock);
2516
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002517 /*
2518 * now the data structures are allocated and basically initialized
2519 * start the real acpi table scan
2520 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002521 ret = init_iommu_all(ivrs_base);
2522 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002523 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002524
Joerg Roedel11123742017-06-16 16:09:54 +02002525 /* Disable any previously enabled IOMMUs */
Baoquan He20b46df2017-08-09 16:33:44 +08002526 if (!is_kdump_kernel() || amd_iommu_disabled)
2527 disable_iommus();
Joerg Roedel11123742017-06-16 16:09:54 +02002528
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002529 if (amd_iommu_irq_remap)
2530 amd_iommu_irq_remap = check_ioapic_information();
2531
Joerg Roedel05152a02012-06-15 16:53:51 +02002532 if (amd_iommu_irq_remap) {
2533 /*
2534 * Interrupt remapping enabled, create kmem_cache for the
2535 * remapping tables.
2536 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002537 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002538 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2539 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2540 else
2541 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002542 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002543 remap_cache_sz,
2544 IRQ_TABLE_ALIGNMENT,
2545 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002546 if (!amd_iommu_irq_cache)
2547 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002548
2549 irq_lookup_table = (void *)__get_free_pages(
2550 GFP_KERNEL | __GFP_ZERO,
2551 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002552 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2553 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002554 if (!irq_lookup_table)
2555 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002556 }
2557
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002558 ret = init_memory_definitions(ivrs_base);
2559 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002560 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002561
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002562 /* init the device table */
2563 init_device_table();
2564
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002565out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002566 /* Don't leak any ACPI memory */
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002567 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002568 ivrs_base = NULL;
2569
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002570 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002571}
2572
Gerard Snitselaarae295142012-03-16 11:38:22 -07002573static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002574{
2575 struct amd_iommu *iommu;
2576 int ret = 0;
2577
2578 for_each_iommu(iommu) {
2579 ret = iommu_init_msi(iommu);
2580 if (ret)
2581 goto out;
2582 }
2583
2584out:
2585 return ret;
2586}
2587
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002588static bool detect_ivrs(void)
2589{
2590 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002591 acpi_status status;
2592
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002593 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002594 if (status == AE_NOT_FOUND)
2595 return false;
2596 else if (ACPI_FAILURE(status)) {
2597 const char *err = acpi_format_exception(status);
2598 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2599 return false;
2600 }
2601
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002602 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002603
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002604 /* Make sure ACS will be enabled during PCI probe */
2605 pci_request_acs();
2606
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002607 return true;
2608}
2609
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002610/****************************************************************************
2611 *
2612 * AMD IOMMU Initialization State Machine
2613 *
2614 ****************************************************************************/
2615
2616static int __init state_next(void)
2617{
2618 int ret = 0;
2619
2620 switch (init_state) {
2621 case IOMMU_START_STATE:
2622 if (!detect_ivrs()) {
2623 init_state = IOMMU_NOT_FOUND;
2624 ret = -ENODEV;
2625 } else {
2626 init_state = IOMMU_IVRS_DETECTED;
2627 }
2628 break;
2629 case IOMMU_IVRS_DETECTED:
2630 ret = early_amd_iommu_init();
2631 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
Joerg Roedel7ad820e2017-06-16 16:09:59 +02002632 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2633 pr_info("AMD-Vi: AMD IOMMU disabled on kernel command-line\n");
2634 free_dma_resources();
2635 free_iommu_resources();
2636 init_state = IOMMU_CMDLINE_DISABLED;
2637 ret = -EINVAL;
2638 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002639 break;
2640 case IOMMU_ACPI_FINISHED:
2641 early_enable_iommus();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002642 x86_platform.iommu_shutdown = disable_iommus;
2643 init_state = IOMMU_ENABLED;
2644 break;
2645 case IOMMU_ENABLED:
Joerg Roedel74ddda72017-07-26 14:17:55 +02002646 register_syscore_ops(&amd_iommu_syscore_ops);
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002647 ret = amd_iommu_init_pci();
2648 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2649 enable_iommus_v2();
2650 break;
2651 case IOMMU_PCI_INIT:
2652 ret = amd_iommu_enable_interrupts();
2653 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2654 break;
2655 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002656 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002657 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2658 break;
2659 case IOMMU_DMA_OPS:
2660 init_state = IOMMU_INITIALIZED;
2661 break;
2662 case IOMMU_INITIALIZED:
2663 /* Nothing to do */
2664 break;
2665 case IOMMU_NOT_FOUND:
2666 case IOMMU_INIT_ERROR:
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002667 case IOMMU_CMDLINE_DISABLED:
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002668 /* Error states => do nothing */
2669 ret = -EINVAL;
2670 break;
2671 default:
2672 /* Unknown state */
2673 BUG();
2674 }
2675
2676 return ret;
2677}
2678
2679static int __init iommu_go_to_state(enum iommu_init_state state)
2680{
Joerg Roedel151b0902017-06-16 16:09:57 +02002681 int ret = -EINVAL;
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002682
2683 while (init_state != state) {
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002684 if (init_state == IOMMU_NOT_FOUND ||
2685 init_state == IOMMU_INIT_ERROR ||
2686 init_state == IOMMU_CMDLINE_DISABLED)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002687 break;
Joerg Roedel151b0902017-06-16 16:09:57 +02002688 ret = state_next();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002689 }
2690
2691 return ret;
2692}
2693
Joerg Roedel6b474b82012-06-26 16:46:04 +02002694#ifdef CONFIG_IRQ_REMAP
2695int __init amd_iommu_prepare(void)
2696{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002697 int ret;
2698
Jiang Liu7fa1c842015-01-07 15:31:42 +08002699 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002700
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002701 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2702 if (ret)
2703 return ret;
2704 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002705}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002706
Joerg Roedel6b474b82012-06-26 16:46:04 +02002707int __init amd_iommu_enable(void)
2708{
2709 int ret;
2710
2711 ret = iommu_go_to_state(IOMMU_ENABLED);
2712 if (ret)
2713 return ret;
2714
2715 irq_remapping_enabled = 1;
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05002716 return amd_iommu_xt_mode;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002717}
2718
2719void amd_iommu_disable(void)
2720{
2721 amd_iommu_suspend();
2722}
2723
2724int amd_iommu_reenable(int mode)
2725{
2726 amd_iommu_resume();
2727
2728 return 0;
2729}
2730
2731int __init amd_iommu_enable_faulting(void)
2732{
2733 /* We enable MSI later when PCI is initialized */
2734 return 0;
2735}
2736#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002737
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002738/*
2739 * This is the core init function for AMD IOMMU hardware in the system.
2740 * This function is called from the generic x86 DMA layer initialization
2741 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002742 */
2743static int __init amd_iommu_init(void)
2744{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002745 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002746
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002747 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2748 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002749 free_dma_resources();
2750 if (!irq_remapping_enabled) {
2751 disable_iommus();
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002752 free_iommu_resources();
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002753 } else {
2754 struct amd_iommu *iommu;
2755
2756 uninit_device_table_dma();
2757 for_each_iommu(iommu)
2758 iommu_flush_all_caches(iommu);
2759 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002760 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002761
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002762 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002763}
2764
Tom Lendacky2543a782017-07-17 16:10:24 -05002765static bool amd_iommu_sme_check(void)
2766{
2767 if (!sme_active() || (boot_cpu_data.x86 != 0x17))
2768 return true;
2769
2770 /* For Fam17h, a specific level of support is required */
2771 if (boot_cpu_data.microcode >= 0x08001205)
2772 return true;
2773
2774 if ((boot_cpu_data.microcode >= 0x08001126) &&
2775 (boot_cpu_data.microcode <= 0x080011ff))
2776 return true;
2777
2778 pr_notice("AMD-Vi: IOMMU not currently supported when SME is active\n");
2779
2780 return false;
2781}
2782
Joerg Roedelb65233a2008-07-11 17:14:21 +02002783/****************************************************************************
2784 *
2785 * Early detect code. This code runs at IOMMU detection time in the DMA
2786 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2787 * IOMMUs
2788 *
2789 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002790int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002791{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002792 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002793
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002794 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002795 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002796
Tom Lendacky2543a782017-07-17 16:10:24 -05002797 if (!amd_iommu_sme_check())
2798 return -ENODEV;
2799
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002800 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2801 if (ret)
2802 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002803
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002804 amd_iommu_detected = true;
2805 iommu_detected = 1;
2806 x86_init.iommu.iommu_init = amd_iommu_init;
2807
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002808 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002809}
2810
Joerg Roedelb65233a2008-07-11 17:14:21 +02002811/****************************************************************************
2812 *
2813 * Parsing functions for the AMD IOMMU specific kernel command line
2814 * options.
2815 *
2816 ****************************************************************************/
2817
Joerg Roedelfefda112009-05-20 12:21:42 +02002818static int __init parse_amd_iommu_dump(char *str)
2819{
2820 amd_iommu_dump = true;
2821
2822 return 1;
2823}
2824
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002825static int __init parse_amd_iommu_intr(char *str)
2826{
2827 for (; *str; ++str) {
2828 if (strncmp(str, "legacy", 6) == 0) {
2829 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2830 break;
2831 }
2832 if (strncmp(str, "vapic", 5) == 0) {
2833 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2834 break;
2835 }
2836 }
2837 return 1;
2838}
2839
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002840static int __init parse_amd_iommu_options(char *str)
2841{
2842 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002843 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002844 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002845 if (strncmp(str, "off", 3) == 0)
2846 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002847 if (strncmp(str, "force_isolation", 15) == 0)
2848 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002849 }
2850
2851 return 1;
2852}
2853
Joerg Roedel440e89982013-04-09 16:35:28 +02002854static int __init parse_ivrs_ioapic(char *str)
2855{
2856 unsigned int bus, dev, fn;
2857 int ret, id, i;
2858 u16 devid;
2859
2860 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2861
2862 if (ret != 4) {
2863 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2864 return 1;
2865 }
2866
2867 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2868 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2869 str);
2870 return 1;
2871 }
2872
2873 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2874
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002875 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002876 i = early_ioapic_map_size++;
2877 early_ioapic_map[i].id = id;
2878 early_ioapic_map[i].devid = devid;
2879 early_ioapic_map[i].cmd_line = true;
2880
2881 return 1;
2882}
2883
2884static int __init parse_ivrs_hpet(char *str)
2885{
2886 unsigned int bus, dev, fn;
2887 int ret, id, i;
2888 u16 devid;
2889
2890 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2891
2892 if (ret != 4) {
2893 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2894 return 1;
2895 }
2896
2897 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2898 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2899 str);
2900 return 1;
2901 }
2902
2903 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2904
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002905 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002906 i = early_hpet_map_size++;
2907 early_hpet_map[i].id = id;
2908 early_hpet_map[i].devid = devid;
2909 early_hpet_map[i].cmd_line = true;
2910
2911 return 1;
2912}
2913
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002914static int __init parse_ivrs_acpihid(char *str)
2915{
2916 u32 bus, dev, fn;
2917 char *hid, *uid, *p;
2918 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2919 int ret, i;
2920
2921 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2922 if (ret != 4) {
2923 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2924 return 1;
2925 }
2926
2927 p = acpiid;
2928 hid = strsep(&p, ":");
2929 uid = p;
2930
2931 if (!hid || !(*hid) || !uid) {
2932 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2933 return 1;
2934 }
2935
2936 i = early_acpihid_map_size++;
2937 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2938 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2939 early_acpihid_map[i].devid =
2940 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2941 early_acpihid_map[i].cmd_line = true;
2942
2943 return 1;
2944}
2945
Joerg Roedel440e89982013-04-09 16:35:28 +02002946__setup("amd_iommu_dump", parse_amd_iommu_dump);
2947__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002948__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002949__setup("ivrs_ioapic", parse_ivrs_ioapic);
2950__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002951__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002952
2953IOMMU_INIT_FINISH(amd_iommu_detect,
2954 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002955 NULL,
2956 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002957
2958bool amd_iommu_v2_supported(void)
2959{
2960 return amd_iommu_v2_present;
2961}
2962EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002963
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002964struct amd_iommu *get_amd_iommu(unsigned int idx)
2965{
2966 unsigned int i = 0;
2967 struct amd_iommu *iommu;
2968
2969 for_each_iommu(iommu)
2970 if (i++ == idx)
2971 return iommu;
2972 return NULL;
2973}
2974EXPORT_SYMBOL(get_amd_iommu);
2975
Steven L Kinney30861dd2013-06-05 16:11:48 -05002976/****************************************************************************
2977 *
2978 * IOMMU EFR Performance Counter support functionality. This code allows
2979 * access to the IOMMU PC functionality.
2980 *
2981 ****************************************************************************/
2982
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002983u8 amd_iommu_pc_get_max_banks(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002984{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002985 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002986
Steven L Kinney30861dd2013-06-05 16:11:48 -05002987 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002988 return iommu->max_banks;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002989
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002990 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002991}
2992EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2993
2994bool amd_iommu_pc_supported(void)
2995{
2996 return amd_iommu_pc_present;
2997}
2998EXPORT_SYMBOL(amd_iommu_pc_supported);
2999
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003000u8 amd_iommu_pc_get_max_counters(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05003001{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003002 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003003
Steven L Kinney30861dd2013-06-05 16:11:48 -05003004 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003005 return iommu->max_counters;
Steven L Kinney30861dd2013-06-05 16:11:48 -05003006
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003007 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05003008}
3009EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3010
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003011static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3012 u8 fxn, u64 *value, bool is_write)
Steven L Kinney30861dd2013-06-05 16:11:48 -05003013{
Steven L Kinney30861dd2013-06-05 16:11:48 -05003014 u32 offset;
3015 u32 max_offset_lim;
3016
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003017 /* Make sure the IOMMU PC resource is available */
3018 if (!amd_iommu_pc_present)
3019 return -ENODEV;
3020
Steven L Kinney30861dd2013-06-05 16:11:48 -05003021 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003022 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05003023 return -ENODEV;
3024
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003025 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003026
3027 /* Limit the offset to the hw defined mmio region aperture */
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003028 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
Steven L Kinney30861dd2013-06-05 16:11:48 -05003029 (iommu->max_counters << 8) | 0x28);
3030 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3031 (offset > max_offset_lim))
3032 return -EINVAL;
3033
3034 if (is_write) {
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003035 u64 val = *value & GENMASK_ULL(47, 0);
3036
3037 writel((u32)val, iommu->mmio_base + offset);
3038 writel((val >> 32), iommu->mmio_base + offset + 4);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003039 } else {
3040 *value = readl(iommu->mmio_base + offset + 4);
3041 *value <<= 32;
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003042 *value |= readl(iommu->mmio_base + offset);
3043 *value &= GENMASK_ULL(47, 0);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003044 }
3045
3046 return 0;
3047}
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003048
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003049int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003050{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003051 if (!iommu)
3052 return -EINVAL;
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003053
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003054 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003055}
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003056EXPORT_SYMBOL(amd_iommu_pc_get_reg);
3057
3058int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3059{
3060 if (!iommu)
3061 return -EINVAL;
3062
3063 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3064}
3065EXPORT_SYMBOL(amd_iommu_pc_set_reg);