blob: e062ab9687c753337add4554f6ad4c990f7ccad9 [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 He3ac3e5e2017-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 He3ac3e5e2017-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));
Filippo Sironi821d0b92018-11-12 12:26:30 +0000800 entry = (iommu_virt_to_phys(iommu->ga_log_tail) &
801 (BIT_ULL(52)-1)) & ~7ULL;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500802 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
803 &entry, sizeof(entry));
804 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
805 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
806
807 return 0;
808err_out:
809 free_ga_log(iommu);
810 return -EINVAL;
811}
812#endif /* CONFIG_IRQ_REMAP */
813
814static int iommu_init_ga(struct amd_iommu *iommu)
815{
816 int ret = 0;
817
818#ifdef CONFIG_IRQ_REMAP
819 /* Note: We have already checked GASup from IVRS table.
820 * Now, we need to make sure that GAMSup is set.
821 */
822 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
823 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
824 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
825
826 ret = iommu_init_ga_log(iommu);
827#endif /* CONFIG_IRQ_REMAP */
828
829 return ret;
830}
831
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -0500832static void iommu_enable_xt(struct amd_iommu *iommu)
833{
834#ifdef CONFIG_IRQ_REMAP
835 /*
836 * XT mode (32-bit APIC destination ID) requires
837 * GA mode (128-bit IRTE support) as a prerequisite.
838 */
839 if (AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir) &&
840 amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
841 iommu_feature_enable(iommu, CONTROL_XT_EN);
842#endif /* CONFIG_IRQ_REMAP */
843}
844
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100845static void iommu_enable_gt(struct amd_iommu *iommu)
846{
847 if (!iommu_feature(iommu, FEATURE_GT))
848 return;
849
850 iommu_feature_enable(iommu, CONTROL_GT_EN);
851}
852
Joerg Roedelb65233a2008-07-11 17:14:21 +0200853/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200854static void set_dev_entry_bit(u16 devid, u8 bit)
855{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100856 int i = (bit >> 6) & 0x03;
857 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200858
Joerg Roedelee6c2862011-11-09 12:06:03 +0100859 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200860}
861
Joerg Roedelc5cca142009-10-09 18:31:20 +0200862static int get_dev_entry_bit(u16 devid, u8 bit)
863{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100864 int i = (bit >> 6) & 0x03;
865 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200866
Joerg Roedelee6c2862011-11-09 12:06:03 +0100867 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200868}
869
870
Baoquan He45a01c42017-08-09 16:33:37 +0800871static bool copy_device_table(void)
872{
Joerg Roedelae162ef2017-08-19 00:28:02 +0200873 u64 int_ctl, int_tab_len, entry = 0, last_entry = 0;
Baoquan He45a01c42017-08-09 16:33:37 +0800874 struct dev_table_entry *old_devtb = NULL;
875 u32 lo, hi, devid, old_devtb_size;
876 phys_addr_t old_devtb_phys;
Baoquan He45a01c42017-08-09 16:33:37 +0800877 struct amd_iommu *iommu;
Baoquan He53019a92017-08-09 16:33:39 +0800878 u16 dom_id, dte_v, irq_v;
Baoquan He45a01c42017-08-09 16:33:37 +0800879 gfp_t gfp_flag;
Baoquan Hedaae2d22017-08-09 16:33:43 +0800880 u64 tmp;
Baoquan He45a01c42017-08-09 16:33:37 +0800881
Baoquan He3ac3e5e2017-08-09 16:33:38 +0800882 if (!amd_iommu_pre_enabled)
883 return false;
Baoquan He45a01c42017-08-09 16:33:37 +0800884
885 pr_warn("Translation is already enabled - trying to copy translation structures\n");
886 for_each_iommu(iommu) {
887 /* All IOMMUs should use the same device table with the same size */
888 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
889 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
890 entry = (((u64) hi) << 32) + lo;
891 if (last_entry && last_entry != entry) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530892 pr_err("IOMMU:%d should use the same dev table as others!\n",
Baoquan He45a01c42017-08-09 16:33:37 +0800893 iommu->index);
894 return false;
895 }
896 last_entry = entry;
897
898 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
899 if (old_devtb_size != dev_table_size) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530900 pr_err("The device table size of IOMMU:%d is not expected!\n",
Baoquan He45a01c42017-08-09 16:33:37 +0800901 iommu->index);
902 return false;
903 }
904 }
905
906 old_devtb_phys = entry & PAGE_MASK;
Baoquan Heb3367812017-08-09 16:33:42 +0800907 if (old_devtb_phys >= 0x100000000ULL) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530908 pr_err("The address of old device table is above 4G, not trustworthy!\n");
Baoquan Heb3367812017-08-09 16:33:42 +0800909 return false;
910 }
Baoquan He45a01c42017-08-09 16:33:37 +0800911 old_devtb = memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
912 if (!old_devtb)
913 return false;
914
Baoquan Heb3367812017-08-09 16:33:42 +0800915 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
Baoquan He45a01c42017-08-09 16:33:37 +0800916 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
917 get_order(dev_table_size));
918 if (old_dev_tbl_cpy == NULL) {
Arvind Yadav3c6bae62017-09-26 13:07:46 +0530919 pr_err("Failed to allocate memory for copying old device table!\n");
Baoquan He45a01c42017-08-09 16:33:37 +0800920 return false;
921 }
922
923 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
924 old_dev_tbl_cpy[devid] = old_devtb[devid];
925 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
926 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
Baoquan He53019a92017-08-09 16:33:39 +0800927
928 if (dte_v && dom_id) {
929 old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
930 old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
Baoquan He45a01c42017-08-09 16:33:37 +0800931 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
Baoquan Hedaae2d22017-08-09 16:33:43 +0800932 /* If gcr3 table existed, mask it out */
933 if (old_devtb[devid].data[0] & DTE_FLAG_GV) {
934 tmp = DTE_GCR3_VAL_B(~0ULL) << DTE_GCR3_SHIFT_B;
935 tmp |= DTE_GCR3_VAL_C(~0ULL) << DTE_GCR3_SHIFT_C;
936 old_dev_tbl_cpy[devid].data[1] &= ~tmp;
937 tmp = DTE_GCR3_VAL_A(~0ULL) << DTE_GCR3_SHIFT_A;
938 tmp |= DTE_FLAG_GV;
939 old_dev_tbl_cpy[devid].data[0] &= ~tmp;
940 }
Baoquan He53019a92017-08-09 16:33:39 +0800941 }
942
943 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
944 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
945 int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK;
946 if (irq_v && (int_ctl || int_tab_len)) {
947 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
948 (int_tab_len != DTE_IRQ_TABLE_LEN)) {
949 pr_err("Wrong old irq remapping flag: %#x\n", devid);
950 return false;
951 }
952
953 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
954 }
Baoquan He45a01c42017-08-09 16:33:37 +0800955 }
956 memunmap(old_devtb);
957
958 return true;
959}
960
Joerg Roedelc5cca142009-10-09 18:31:20 +0200961void amd_iommu_apply_erratum_63(u16 devid)
962{
963 int sysmgt;
964
965 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
966 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
967
968 if (sysmgt == 0x01)
969 set_dev_entry_bit(devid, DEV_ENTRY_IW);
970}
971
Joerg Roedel5ff47892008-07-14 20:11:18 +0200972/* Writes the specific IOMMU for a device into the rlookup table */
973static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
974{
975 amd_iommu_rlookup_table[devid] = iommu;
976}
977
Joerg Roedelb65233a2008-07-11 17:14:21 +0200978/*
979 * This function takes the device specific flags read from the ACPI
980 * table and sets up the device table entry with that information
981 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200982static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
983 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200984{
985 if (flags & ACPI_DEVFLAG_INITPASS)
986 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
987 if (flags & ACPI_DEVFLAG_EXTINT)
988 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
989 if (flags & ACPI_DEVFLAG_NMI)
990 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
991 if (flags & ACPI_DEVFLAG_SYSMGT1)
992 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
993 if (flags & ACPI_DEVFLAG_SYSMGT2)
994 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
995 if (flags & ACPI_DEVFLAG_LINT0)
996 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
997 if (flags & ACPI_DEVFLAG_LINT1)
998 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200999
Joerg Roedelc5cca142009-10-09 18:31:20 +02001000 amd_iommu_apply_erratum_63(devid);
1001
Joerg Roedel5ff47892008-07-14 20:11:18 +02001002 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +02001003}
1004
Joerg Roedelc50e3242014-09-09 15:59:37 +02001005static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +02001006{
1007 struct devid_map *entry;
1008 struct list_head *list;
1009
Joerg Roedel31cff672013-04-09 16:53:58 +02001010 if (type == IVHD_SPECIAL_IOAPIC)
1011 list = &ioapic_map;
1012 else if (type == IVHD_SPECIAL_HPET)
1013 list = &hpet_map;
1014 else
Joerg Roedel6efed632012-06-14 15:52:58 +02001015 return -EINVAL;
1016
Joerg Roedel31cff672013-04-09 16:53:58 +02001017 list_for_each_entry(entry, list, list) {
1018 if (!(entry->id == id && entry->cmd_line))
1019 continue;
1020
1021 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
1022 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
1023
Joerg Roedelc50e3242014-09-09 15:59:37 +02001024 *devid = entry->devid;
1025
Joerg Roedel31cff672013-04-09 16:53:58 +02001026 return 0;
1027 }
1028
Joerg Roedel6efed632012-06-14 15:52:58 +02001029 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1030 if (!entry)
1031 return -ENOMEM;
1032
Joerg Roedel31cff672013-04-09 16:53:58 +02001033 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001034 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +02001035 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +02001036
1037 list_add_tail(&entry->list, list);
1038
1039 return 0;
1040}
1041
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001042static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1043 bool cmd_line)
1044{
1045 struct acpihid_map_entry *entry;
1046 struct list_head *list = &acpihid_map;
1047
1048 list_for_each_entry(entry, list, list) {
1049 if (strcmp(entry->hid, hid) ||
1050 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1051 !entry->cmd_line)
1052 continue;
1053
1054 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
1055 hid, uid);
1056 *devid = entry->devid;
1057 return 0;
1058 }
1059
1060 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1061 if (!entry)
1062 return -ENOMEM;
1063
1064 memcpy(entry->uid, uid, strlen(uid));
1065 memcpy(entry->hid, hid, strlen(hid));
1066 entry->devid = *devid;
1067 entry->cmd_line = cmd_line;
1068 entry->root_devid = (entry->devid & (~0x7));
1069
1070 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
1071 entry->cmd_line ? "cmd" : "ivrs",
1072 entry->hid, entry->uid, entry->root_devid);
1073
1074 list_add_tail(&entry->list, list);
1075 return 0;
1076}
1077
Joerg Roedel235dacb2013-04-09 17:53:14 +02001078static int __init add_early_maps(void)
1079{
1080 int i, ret;
1081
1082 for (i = 0; i < early_ioapic_map_size; ++i) {
1083 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1084 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +02001085 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +02001086 early_ioapic_map[i].cmd_line);
1087 if (ret)
1088 return ret;
1089 }
1090
1091 for (i = 0; i < early_hpet_map_size; ++i) {
1092 ret = add_special_device(IVHD_SPECIAL_HPET,
1093 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +02001094 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +02001095 early_hpet_map[i].cmd_line);
1096 if (ret)
1097 return ret;
1098 }
1099
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001100 for (i = 0; i < early_acpihid_map_size; ++i) {
1101 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1102 early_acpihid_map[i].uid,
1103 &early_acpihid_map[i].devid,
1104 early_acpihid_map[i].cmd_line);
1105 if (ret)
1106 return ret;
1107 }
1108
Joerg Roedel235dacb2013-04-09 17:53:14 +02001109 return 0;
1110}
1111
Joerg Roedelb65233a2008-07-11 17:14:21 +02001112/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02001113 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +02001114 * it
1115 */
Joerg Roedel3566b772008-06-26 21:27:46 +02001116static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
1117{
1118 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1119
1120 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
1121 return;
1122
1123 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +02001124 /*
1125 * We only can configure exclusion ranges per IOMMU, not
1126 * per device. But we can enable the exclusion range per
1127 * device. This is done here
1128 */
Su Friendy2c16c9f2014-05-07 13:54:52 +08001129 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +02001130 iommu->exclusion_start = m->range_start;
1131 iommu->exclusion_length = m->range_length;
1132 }
1133}
1134
Joerg Roedelb65233a2008-07-11 17:14:21 +02001135/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001136 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1137 * initializes the hardware and our data structures with it.
1138 */
Joerg Roedel6efed632012-06-14 15:52:58 +02001139static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001140 struct ivhd_header *h)
1141{
1142 u8 *p = (u8 *)h;
1143 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +02001144 u16 devid = 0, devid_start = 0, devid_to = 0;
1145 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001146 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001147 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001148 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +02001149 int ret;
1150
1151
1152 ret = add_early_maps();
1153 if (ret)
1154 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001155
1156 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001157 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001158 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001159 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001160
1161 /*
1162 * Done. Now parse the device entries
1163 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001164 ivhd_size = get_ivhd_header_size(h);
1165 if (!ivhd_size) {
1166 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1167 return -EINVAL;
1168 }
1169
1170 p += ivhd_size;
1171
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001172 end += h->length;
1173
Joerg Roedel42a698f2009-05-20 15:41:28 +02001174
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001175 while (p < end) {
1176 e = (struct ivhd_entry *)p;
1177 switch (e->type) {
1178 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001179
Joerg Roedel226e8892015-10-20 17:33:44 +02001180 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001181
Joerg Roedel226e8892015-10-20 17:33:44 +02001182 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1183 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001184 break;
1185 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001186
1187 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1188 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001189 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001190 PCI_SLOT(e->devid),
1191 PCI_FUNC(e->devid),
1192 e->flags);
1193
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001194 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001195 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001196 break;
1197 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001198
1199 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1200 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001201 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001202 PCI_SLOT(e->devid),
1203 PCI_FUNC(e->devid),
1204 e->flags);
1205
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001206 devid_start = e->devid;
1207 flags = e->flags;
1208 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001209 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001210 break;
1211 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001212
1213 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1214 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001215 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001216 PCI_SLOT(e->devid),
1217 PCI_FUNC(e->devid),
1218 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001219 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001220 PCI_SLOT(e->ext >> 8),
1221 PCI_FUNC(e->ext >> 8));
1222
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001223 devid = e->devid;
1224 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001225 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001226 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001227 amd_iommu_alias_table[devid] = devid_to;
1228 break;
1229 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001230
1231 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1232 "devid: %02x:%02x.%x flags: %02x "
1233 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001234 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001235 PCI_SLOT(e->devid),
1236 PCI_FUNC(e->devid),
1237 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001238 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001239 PCI_SLOT(e->ext >> 8),
1240 PCI_FUNC(e->ext >> 8));
1241
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001242 devid_start = e->devid;
1243 flags = e->flags;
1244 devid_to = e->ext >> 8;
1245 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001246 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001247 break;
1248 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001249
1250 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1251 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001252 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001253 PCI_SLOT(e->devid),
1254 PCI_FUNC(e->devid),
1255 e->flags, e->ext);
1256
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001257 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001258 set_dev_entry_from_acpi(iommu, devid, e->flags,
1259 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001260 break;
1261 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001262
1263 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1264 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001265 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001266 PCI_SLOT(e->devid),
1267 PCI_FUNC(e->devid),
1268 e->flags, e->ext);
1269
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001270 devid_start = e->devid;
1271 flags = e->flags;
1272 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001273 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001274 break;
1275 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001276
1277 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001278 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001279 PCI_SLOT(e->devid),
1280 PCI_FUNC(e->devid));
1281
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001282 devid = e->devid;
1283 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001284 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001285 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001286 set_dev_entry_from_acpi(iommu,
1287 devid_to, flags, ext_flags);
1288 }
1289 set_dev_entry_from_acpi(iommu, dev_i,
1290 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001291 }
1292 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001293 case IVHD_DEV_SPECIAL: {
1294 u8 handle, type;
1295 const char *var;
1296 u16 devid;
1297 int ret;
1298
1299 handle = e->ext & 0xff;
1300 devid = (e->ext >> 8) & 0xffff;
1301 type = (e->ext >> 24) & 0xff;
1302
1303 if (type == IVHD_SPECIAL_IOAPIC)
1304 var = "IOAPIC";
1305 else if (type == IVHD_SPECIAL_HPET)
1306 var = "HPET";
1307 else
1308 var = "UNKNOWN";
1309
1310 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1311 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001312 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001313 PCI_SLOT(devid),
1314 PCI_FUNC(devid));
1315
Joerg Roedelc50e3242014-09-09 15:59:37 +02001316 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001317 if (ret)
1318 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001319
1320 /*
1321 * add_special_device might update the devid in case a
1322 * command-line override is present. So call
1323 * set_dev_entry_from_acpi after add_special_device.
1324 */
1325 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1326
Joerg Roedel6efed632012-06-14 15:52:58 +02001327 break;
1328 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001329 case IVHD_DEV_ACPI_HID: {
1330 u16 devid;
1331 u8 hid[ACPIHID_HID_LEN] = {0};
1332 u8 uid[ACPIHID_UID_LEN] = {0};
1333 int ret;
1334
1335 if (h->type != 0x40) {
1336 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1337 e->type);
1338 break;
1339 }
1340
1341 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1342 hid[ACPIHID_HID_LEN - 1] = '\0';
1343
1344 if (!(*hid)) {
1345 pr_err(FW_BUG "Invalid HID.\n");
1346 break;
1347 }
1348
1349 switch (e->uidf) {
1350 case UID_NOT_PRESENT:
1351
1352 if (e->uidl != 0)
1353 pr_warn(FW_BUG "Invalid UID length.\n");
1354
1355 break;
1356 case UID_IS_INTEGER:
1357
1358 sprintf(uid, "%d", e->uid);
1359
1360 break;
1361 case UID_IS_CHARACTER:
1362
1363 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1364 uid[ACPIHID_UID_LEN - 1] = '\0';
1365
1366 break;
1367 default:
1368 break;
1369 }
1370
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001371 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001372 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1373 hid, uid,
1374 PCI_BUS_NUM(devid),
1375 PCI_SLOT(devid),
1376 PCI_FUNC(devid));
1377
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001378 flags = e->flags;
1379
1380 ret = add_acpi_hid_device(hid, uid, &devid, false);
1381 if (ret)
1382 return ret;
1383
1384 /*
1385 * add_special_device might update the devid in case a
1386 * command-line override is present. So call
1387 * set_dev_entry_from_acpi after add_special_device.
1388 */
1389 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1390
1391 break;
1392 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001393 default:
1394 break;
1395 }
1396
Joerg Roedelb514e552008-09-17 17:14:27 +02001397 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001398 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001399
1400 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001401}
1402
Joerg Roedele47d4022008-06-26 21:27:48 +02001403static void __init free_iommu_one(struct amd_iommu *iommu)
1404{
1405 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001406 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001407 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001408 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001409 iommu_unmap_mmio_space(iommu);
1410}
1411
1412static void __init free_iommu_all(void)
1413{
1414 struct amd_iommu *iommu, *next;
1415
Joerg Roedel3bd22172009-05-04 15:06:20 +02001416 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001417 list_del(&iommu->list);
1418 free_iommu_one(iommu);
1419 kfree(iommu);
1420 }
1421}
1422
Joerg Roedelb65233a2008-07-11 17:14:21 +02001423/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001424 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1425 * Workaround:
1426 * BIOS should disable L2B micellaneous clock gating by setting
1427 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1428 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001429static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001430{
1431 u32 value;
1432
1433 if ((boot_cpu_data.x86 != 0x15) ||
1434 (boot_cpu_data.x86_model < 0x10) ||
1435 (boot_cpu_data.x86_model > 0x1f))
1436 return;
1437
1438 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1439 pci_read_config_dword(iommu->dev, 0xf4, &value);
1440
1441 if (value & BIT(2))
1442 return;
1443
1444 /* Select NB indirect register 0x90 and enable writing */
1445 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1446
1447 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1448 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1449 dev_name(&iommu->dev->dev));
1450
1451 /* Clear the enable writing bit */
1452 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1453}
1454
1455/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001456 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1457 * Workaround:
1458 * BIOS should enable ATS write permission check by setting
1459 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1460 */
1461static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1462{
1463 u32 value;
1464
1465 if ((boot_cpu_data.x86 != 0x15) ||
1466 (boot_cpu_data.x86_model < 0x30) ||
1467 (boot_cpu_data.x86_model > 0x3f))
1468 return;
1469
1470 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1471 value = iommu_read_l2(iommu, 0x47);
1472
1473 if (value & BIT(0))
1474 return;
1475
1476 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1477 iommu_write_l2(iommu, 0x47, value | BIT(0));
1478
1479 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1480 dev_name(&iommu->dev->dev));
1481}
1482
1483/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001484 * This function clues the initialization function for one IOMMU
1485 * together and also allocates the command buffer and programs the
1486 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1487 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001488static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1489{
Joerg Roedel6efed632012-06-14 15:52:58 +02001490 int ret;
1491
Scott Wood27790392018-01-21 03:28:54 -06001492 raw_spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001493
1494 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001495 list_add_tail(&iommu->list, &amd_iommu_list);
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001496 iommu->index = amd_iommus_present++;
Joerg Roedelbb527772009-11-20 14:31:51 +01001497
1498 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1499 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1500 return -ENOSYS;
1501 }
1502
1503 /* Index is fine - add IOMMU to the array */
1504 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001505
1506 /*
1507 * Copy data from ACPI table entry to the iommu struct
1508 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001509 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001510 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001511 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001512 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001513
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001514 switch (h->type) {
1515 case 0x10:
1516 /* Check if IVHD EFR contains proper max banks/counters */
1517 if ((h->efr_attr != 0) &&
1518 ((h->efr_attr & (0xF << 13)) != 0) &&
1519 ((h->efr_attr & (0x3F << 17)) != 0))
1520 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1521 else
1522 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001523 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1524 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05001525 if (((h->efr_attr & (0x1 << IOMMU_FEAT_XTSUP_SHIFT)) == 0))
1526 amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001527 break;
1528 case 0x11:
1529 case 0x40:
1530 if (h->efr_reg & (1 << 9))
1531 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1532 else
1533 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001534 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1535 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05001536 if (((h->efr_reg & (0x1 << IOMMU_EFR_XTSUP_SHIFT)) == 0))
1537 amd_iommu_xt_mode = IRQ_REMAP_XAPIC_MODE;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001538 break;
1539 default:
1540 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001541 }
1542
1543 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1544 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001545 if (!iommu->mmio_base)
1546 return -ENOMEM;
1547
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001548 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001549 return -ENOMEM;
1550
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001551 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001552 return -ENOMEM;
1553
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001554 iommu->int_enabled = false;
1555
Baoquan He4c232a72017-08-09 16:33:33 +08001556 init_translation_status(iommu);
Baoquan He3ac3e5e2017-08-09 16:33:38 +08001557 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1558 iommu_disable(iommu);
1559 clear_translation_pre_enabled(iommu);
1560 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1561 iommu->index);
1562 }
1563 if (amd_iommu_pre_enabled)
1564 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
Baoquan He4c232a72017-08-09 16:33:33 +08001565
Joerg Roedel6efed632012-06-14 15:52:58 +02001566 ret = init_iommu_from_acpi(iommu, h);
1567 if (ret)
1568 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001569
Jiang Liu7c71d302015-04-13 14:11:33 +08001570 ret = amd_iommu_create_irq_domain(iommu);
1571 if (ret)
1572 return ret;
1573
Joerg Roedelf6fec002012-06-21 16:51:25 +02001574 /*
1575 * Make sure IOMMU is not considered to translate itself. The IVRS
1576 * table tells us so, but this is a lie!
1577 */
1578 amd_iommu_rlookup_table[iommu->devid] = NULL;
1579
Joerg Roedel23c742d2012-06-12 11:47:34 +02001580 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001581}
1582
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001583/**
1584 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1585 * @ivrs Pointer to the IVRS header
1586 *
1587 * This function search through all IVDB of the maximum supported IVHD
1588 */
1589static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1590{
1591 u8 *base = (u8 *)ivrs;
1592 struct ivhd_header *ivhd = (struct ivhd_header *)
1593 (base + IVRS_HEADER_LENGTH);
1594 u8 last_type = ivhd->type;
1595 u16 devid = ivhd->devid;
1596
1597 while (((u8 *)ivhd - base < ivrs->length) &&
1598 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1599 u8 *p = (u8 *) ivhd;
1600
1601 if (ivhd->devid == devid)
1602 last_type = ivhd->type;
1603 ivhd = (struct ivhd_header *)(p + ivhd->length);
1604 }
1605
1606 return last_type;
1607}
1608
Joerg Roedelb65233a2008-07-11 17:14:21 +02001609/*
1610 * Iterates over all IOMMU entries in the ACPI table, allocates the
1611 * IOMMU structure and initializes it with init_iommu_one()
1612 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001613static int __init init_iommu_all(struct acpi_table_header *table)
1614{
1615 u8 *p = (u8 *)table, *end = (u8 *)table;
1616 struct ivhd_header *h;
1617 struct amd_iommu *iommu;
1618 int ret;
1619
Joerg Roedele47d4022008-06-26 21:27:48 +02001620 end += table->length;
1621 p += IVRS_HEADER_LENGTH;
1622
1623 while (p < end) {
1624 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001625 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001626
Joerg Roedelae908c22009-09-01 16:52:16 +02001627 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001628 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001629 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001630 PCI_FUNC(h->devid), h->cap_ptr,
1631 h->pci_seg, h->flags, h->info);
1632 DUMP_printk(" mmio-addr: %016llx\n",
1633 h->mmio_phys);
1634
Joerg Roedele47d4022008-06-26 21:27:48 +02001635 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001636 if (iommu == NULL)
1637 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001638
Joerg Roedele47d4022008-06-26 21:27:48 +02001639 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001640 if (ret)
1641 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001642 }
1643 p += h->length;
1644
1645 }
1646 WARN_ON(p != end);
1647
1648 return 0;
1649}
1650
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001651static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1652 u8 fxn, u64 *value, bool is_write);
Steven L Kinney30861dd2013-06-05 16:11:48 -05001653
1654static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1655{
1656 u64 val = 0xabcd, val2 = 0;
1657
1658 if (!iommu_feature(iommu, FEATURE_PC))
1659 return;
1660
1661 amd_iommu_pc_present = true;
1662
1663 /* Check if the performance counters can be written to */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001664 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1665 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001666 (val != val2)) {
1667 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1668 amd_iommu_pc_present = false;
1669 return;
1670 }
1671
1672 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1673
1674 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1675 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1676 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1677}
1678
Alex Williamson066f2e92014-06-12 16:12:37 -06001679static ssize_t amd_iommu_show_cap(struct device *dev,
1680 struct device_attribute *attr,
1681 char *buf)
1682{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001683 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001684 return sprintf(buf, "%x\n", iommu->cap);
1685}
1686static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1687
1688static ssize_t amd_iommu_show_features(struct device *dev,
1689 struct device_attribute *attr,
1690 char *buf)
1691{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001692 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001693 return sprintf(buf, "%llx\n", iommu->features);
1694}
1695static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1696
1697static struct attribute *amd_iommu_attrs[] = {
1698 &dev_attr_cap.attr,
1699 &dev_attr_features.attr,
1700 NULL,
1701};
1702
1703static struct attribute_group amd_iommu_group = {
1704 .name = "amd-iommu",
1705 .attrs = amd_iommu_attrs,
1706};
1707
1708static const struct attribute_group *amd_iommu_groups[] = {
1709 &amd_iommu_group,
1710 NULL,
1711};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001712
Joerg Roedel23c742d2012-06-12 11:47:34 +02001713static int iommu_init_pci(struct amd_iommu *iommu)
1714{
1715 int cap_ptr = iommu->cap_ptr;
1716 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001717 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001718
Sinan Kayad5bf0f42017-12-19 00:37:47 -05001719 iommu->dev = pci_get_domain_bus_and_slot(0, PCI_BUS_NUM(iommu->devid),
1720 iommu->devid & 0xff);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001721 if (!iommu->dev)
1722 return -ENODEV;
1723
Jiang Liucbbc00b2015-10-09 22:07:31 +08001724 /* Prevent binding other PCI device drivers to IOMMU devices */
1725 iommu->dev->match_driver = false;
1726
Joerg Roedel23c742d2012-06-12 11:47:34 +02001727 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1728 &iommu->cap);
1729 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1730 &range);
1731 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1732 &misc);
1733
Joerg Roedel23c742d2012-06-12 11:47:34 +02001734 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1735 amd_iommu_iotlb_sup = false;
1736
1737 /* read extended feature bits */
1738 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1739 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1740
1741 iommu->features = ((u64)high << 32) | low;
1742
1743 if (iommu_feature(iommu, FEATURE_GT)) {
1744 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001745 u32 max_pasid;
1746 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001747
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001748 pasmax = iommu->features & FEATURE_PASID_MASK;
1749 pasmax >>= FEATURE_PASID_SHIFT;
1750 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001751
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001752 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1753
1754 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001755
1756 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1757 glxval >>= FEATURE_GLXVAL_SHIFT;
1758
1759 if (amd_iommu_max_glx_val == -1)
1760 amd_iommu_max_glx_val = glxval;
1761 else
1762 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1763 }
1764
1765 if (iommu_feature(iommu, FEATURE_GT) &&
1766 iommu_feature(iommu, FEATURE_PPR)) {
1767 iommu->is_iommu_v2 = true;
1768 amd_iommu_v2_present = true;
1769 }
1770
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001771 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1772 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001773
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001774 ret = iommu_init_ga(iommu);
1775 if (ret)
1776 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001777
Joerg Roedel23c742d2012-06-12 11:47:34 +02001778 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1779 amd_iommu_np_cache = true;
1780
Steven L Kinney30861dd2013-06-05 16:11:48 -05001781 init_iommu_perf_ctr(iommu);
1782
Joerg Roedel23c742d2012-06-12 11:47:34 +02001783 if (is_rd890_iommu(iommu->dev)) {
1784 int i, j;
1785
Sinan Kayad5bf0f42017-12-19 00:37:47 -05001786 iommu->root_pdev =
1787 pci_get_domain_bus_and_slot(0, iommu->dev->bus->number,
1788 PCI_DEVFN(0, 0));
Joerg Roedel23c742d2012-06-12 11:47:34 +02001789
1790 /*
1791 * Some rd890 systems may not be fully reconfigured by the
1792 * BIOS, so it's necessary for us to store this information so
1793 * it can be reprogrammed on resume
1794 */
1795 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1796 &iommu->stored_addr_lo);
1797 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1798 &iommu->stored_addr_hi);
1799
1800 /* Low bit locks writes to configuration space */
1801 iommu->stored_addr_lo &= ~1;
1802
1803 for (i = 0; i < 6; i++)
1804 for (j = 0; j < 0x12; j++)
1805 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1806
1807 for (i = 0; i < 0x83; i++)
1808 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1809 }
1810
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001811 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001812 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001813
Joerg Roedel39ab9552017-02-01 16:56:46 +01001814 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1815 amd_iommu_groups, "ivhd%d", iommu->index);
Joerg Roedelb0119e82017-02-01 13:23:08 +01001816 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1817 iommu_device_register(&iommu->iommu);
Alex Williamson066f2e92014-06-12 16:12:37 -06001818
Joerg Roedel23c742d2012-06-12 11:47:34 +02001819 return pci_enable_device(iommu->dev);
1820}
1821
Joerg Roedel4d121c32012-06-14 12:21:55 +02001822static void print_iommu_info(void)
1823{
1824 static const char * const feat_str[] = {
1825 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1826 "IA", "GA", "HE", "PC"
1827 };
1828 struct amd_iommu *iommu;
1829
1830 for_each_iommu(iommu) {
1831 int i;
1832
1833 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1834 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1835
1836 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001837 pr_info("AMD-Vi: Extended features (%#llx):\n",
1838 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001839 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001840 if (iommu_feature(iommu, (1ULL << i)))
1841 pr_cont(" %s", feat_str[i]);
1842 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001843
1844 if (iommu->features & FEATURE_GAM_VAPIC)
1845 pr_cont(" GA_vAPIC");
1846
Steven L Kinney30861dd2013-06-05 16:11:48 -05001847 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001848 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001849 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001850 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001851 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001852 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1853 pr_info("AMD-Vi: virtual APIC enabled\n");
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05001854 if (amd_iommu_xt_mode == IRQ_REMAP_X2APIC_MODE)
1855 pr_info("AMD-Vi: X2APIC enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001856 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001857}
1858
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001859static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001860{
1861 struct amd_iommu *iommu;
1862 int ret = 0;
1863
1864 for_each_iommu(iommu) {
1865 ret = iommu_init_pci(iommu);
1866 if (ret)
1867 break;
1868 }
1869
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001870 /*
1871 * Order is important here to make sure any unity map requirements are
1872 * fulfilled. The unity mappings are created and written to the device
1873 * table during the amd_iommu_init_api() call.
1874 *
1875 * After that we call init_device_table_dma() to make sure any
1876 * uninitialized DTE will block DMA, and in the end we flush the caches
1877 * of all IOMMUs to make sure the changes to the device table are
1878 * active.
1879 */
1880 ret = amd_iommu_init_api();
1881
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001882 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001883
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001884 for_each_iommu(iommu)
1885 iommu_flush_all_caches(iommu);
1886
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001887 if (!ret)
1888 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001889
Joerg Roedel23c742d2012-06-12 11:47:34 +02001890 return ret;
1891}
1892
Joerg Roedelb65233a2008-07-11 17:14:21 +02001893/****************************************************************************
1894 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001895 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001896 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001897 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1898 * pci_dev.
1899 *
1900 ****************************************************************************/
1901
Joerg Roedel9f800de2009-11-23 12:45:25 +01001902static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001903{
1904 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001905
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001906 r = pci_enable_msi(iommu->dev);
1907 if (r)
1908 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001909
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001910 r = request_threaded_irq(iommu->dev->irq,
1911 amd_iommu_int_handler,
1912 amd_iommu_int_thread,
1913 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001914 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001915
1916 if (r) {
1917 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001918 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001919 }
1920
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001921 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001922
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001923 return 0;
1924}
1925
Joerg Roedel05f92db2009-05-12 09:52:46 +02001926static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001927{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001928 int ret;
1929
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001930 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001931 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001932
Yijing Wang82fcfc62013-08-08 21:12:36 +08001933 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001934 ret = iommu_setup_msi(iommu);
1935 else
1936 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001937
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001938 if (ret)
1939 return ret;
1940
1941enable_faults:
1942 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1943
1944 if (iommu->ppr_log != NULL)
1945 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1946
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001947 iommu_ga_log_enable(iommu);
1948
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001949 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001950}
1951
1952/****************************************************************************
1953 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001954 * The next functions belong to the third pass of parsing the ACPI
1955 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001956 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001957 *
1958 ****************************************************************************/
1959
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001960static void __init free_unity_maps(void)
1961{
1962 struct unity_map_entry *entry, *next;
1963
1964 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1965 list_del(&entry->list);
1966 kfree(entry);
1967 }
1968}
1969
Joerg Roedelb65233a2008-07-11 17:14:21 +02001970/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001971static int __init init_exclusion_range(struct ivmd_header *m)
1972{
1973 int i;
1974
1975 switch (m->type) {
1976 case ACPI_IVMD_TYPE:
1977 set_device_exclusion_range(m->devid, m);
1978 break;
1979 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001980 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001981 set_device_exclusion_range(i, m);
1982 break;
1983 case ACPI_IVMD_TYPE_RANGE:
1984 for (i = m->devid; i <= m->aux; ++i)
1985 set_device_exclusion_range(i, m);
1986 break;
1987 default:
1988 break;
1989 }
1990
1991 return 0;
1992}
1993
Joerg Roedelb65233a2008-07-11 17:14:21 +02001994/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001995static int __init init_unity_map_range(struct ivmd_header *m)
1996{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001997 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001998 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001999
2000 e = kzalloc(sizeof(*e), GFP_KERNEL);
2001 if (e == NULL)
2002 return -ENOMEM;
2003
2004 switch (m->type) {
2005 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02002006 kfree(e);
2007 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002008 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02002009 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002010 e->devid_start = e->devid_end = m->devid;
2011 break;
2012 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02002013 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002014 e->devid_start = 0;
2015 e->devid_end = amd_iommu_last_bdf;
2016 break;
2017 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02002018 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002019 e->devid_start = m->devid;
2020 e->devid_end = m->aux;
2021 break;
2022 }
2023 e->address_start = PAGE_ALIGN(m->range_start);
2024 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
2025 e->prot = m->flags >> 1;
2026
Joerg Roedel02acc432009-05-20 16:24:21 +02002027 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
2028 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07002029 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
2030 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02002031 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
2032 e->address_start, e->address_end, m->flags);
2033
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002034 list_add_tail(&e->list, &amd_iommu_unity_map);
2035
2036 return 0;
2037}
2038
Joerg Roedelb65233a2008-07-11 17:14:21 +02002039/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002040static int __init init_memory_definitions(struct acpi_table_header *table)
2041{
2042 u8 *p = (u8 *)table, *end = (u8 *)table;
2043 struct ivmd_header *m;
2044
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002045 end += table->length;
2046 p += IVRS_HEADER_LENGTH;
2047
2048 while (p < end) {
2049 m = (struct ivmd_header *)p;
2050 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2051 init_exclusion_range(m);
2052 else if (m->flags & IVMD_FLAG_UNITY_MAP)
2053 init_unity_map_range(m);
2054
2055 p += m->length;
2056 }
2057
2058 return 0;
2059}
2060
Joerg Roedelb65233a2008-07-11 17:14:21 +02002061/*
Baoquan He3ac3e5e2017-08-09 16:33:38 +08002062 * Init the device table to not allow DMA access for devices
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002063 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02002064static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002065{
Joerg Roedel0de66d52011-06-06 16:04:02 +02002066 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002067
2068 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2069 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2070 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002071 }
2072}
2073
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002074static void __init uninit_device_table_dma(void)
2075{
2076 u32 devid;
2077
2078 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2079 amd_iommu_dev_table[devid].data[0] = 0ULL;
2080 amd_iommu_dev_table[devid].data[1] = 0ULL;
2081 }
2082}
2083
Joerg Roedel33f28c52012-06-15 18:03:31 +02002084static void init_device_table(void)
2085{
2086 u32 devid;
2087
2088 if (!amd_iommu_irq_remap)
2089 return;
2090
2091 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2092 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2093}
2094
Joerg Roedele9bf5192010-09-20 14:33:07 +02002095static void iommu_init_flags(struct amd_iommu *iommu)
2096{
2097 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2098 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2099 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2100
2101 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2102 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2103 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2104
2105 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2106 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2107 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2108
2109 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2110 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2111 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2112
2113 /*
2114 * make IOMMU memory accesses cache coherent
2115 */
2116 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01002117
2118 /* Set IOTLB invalidation timeout to 1s */
2119 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002120}
2121
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002122static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02002123{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002124 int i, j;
2125 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002126 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002127
2128 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002129 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002130 return;
2131
2132 /*
2133 * First, we need to ensure that the iommu is enabled. This is
2134 * controlled by a register in the northbridge
2135 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002136
2137 /* Select Northbridge indirect register 0x75 and enable writing */
2138 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2139 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2140
2141 /* Enable the iommu */
2142 if (!(ioc_feature_control & 0x1))
2143 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2144
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002145 /* Restore the iommu BAR */
2146 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2147 iommu->stored_addr_lo);
2148 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2149 iommu->stored_addr_hi);
2150
2151 /* Restore the l1 indirect regs for each of the 6 l1s */
2152 for (i = 0; i < 6; i++)
2153 for (j = 0; j < 0x12; j++)
2154 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2155
2156 /* Restore the l2 indirect regs */
2157 for (i = 0; i < 0x83; i++)
2158 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2159
2160 /* Lock PCI setup registers */
2161 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2162 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02002163}
2164
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002165static void iommu_enable_ga(struct amd_iommu *iommu)
2166{
2167#ifdef CONFIG_IRQ_REMAP
2168 switch (amd_iommu_guest_ir) {
2169 case AMD_IOMMU_GUEST_IR_VAPIC:
2170 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2171 /* Fall through */
2172 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2173 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002174 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002175 break;
2176 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002177 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002178 break;
2179 }
2180#endif
2181}
2182
Baoquan He78d313c2017-08-09 16:33:34 +08002183static void early_enable_iommu(struct amd_iommu *iommu)
2184{
2185 iommu_disable(iommu);
2186 iommu_init_flags(iommu);
2187 iommu_set_device_table(iommu);
2188 iommu_enable_command_buffer(iommu);
2189 iommu_enable_event_buffer(iommu);
2190 iommu_set_exclusion_range(iommu);
2191 iommu_enable_ga(iommu);
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05002192 iommu_enable_xt(iommu);
Baoquan He78d313c2017-08-09 16:33:34 +08002193 iommu_enable(iommu);
2194 iommu_flush_all_caches(iommu);
2195}
2196
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002197/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002198 * This function finally enables all IOMMUs found in the system after
Baoquan He3ac3e5e2017-08-09 16:33:38 +08002199 * they have been initialized.
2200 *
2201 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2202 * the old content of device table entries. Not this case or copy failed,
2203 * just continue as normal kernel does.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002204 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002205static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002206{
2207 struct amd_iommu *iommu;
2208
Baoquan He3ac3e5e2017-08-09 16:33:38 +08002209
2210 if (!copy_device_table()) {
2211 /*
2212 * If come here because of failure in copying device table from old
2213 * kernel with all IOMMUs enabled, print error message and try to
2214 * free allocated old_dev_tbl_cpy.
2215 */
2216 if (amd_iommu_pre_enabled)
2217 pr_err("Failed to copy DEV table from previous kernel.\n");
2218 if (old_dev_tbl_cpy != NULL)
2219 free_pages((unsigned long)old_dev_tbl_cpy,
2220 get_order(dev_table_size));
2221
2222 for_each_iommu(iommu) {
2223 clear_translation_pre_enabled(iommu);
2224 early_enable_iommu(iommu);
2225 }
2226 } else {
2227 pr_info("Copied DEV table from previous kernel.\n");
2228 free_pages((unsigned long)amd_iommu_dev_table,
2229 get_order(dev_table_size));
2230 amd_iommu_dev_table = old_dev_tbl_cpy;
2231 for_each_iommu(iommu) {
2232 iommu_disable_command_buffer(iommu);
2233 iommu_disable_event_buffer(iommu);
2234 iommu_enable_command_buffer(iommu);
2235 iommu_enable_event_buffer(iommu);
2236 iommu_enable_ga(iommu);
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05002237 iommu_enable_xt(iommu);
Baoquan He3ac3e5e2017-08-09 16:33:38 +08002238 iommu_set_device_table(iommu);
2239 iommu_flush_all_caches(iommu);
2240 }
Joerg Roedel87361972008-06-26 21:28:07 +02002241 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002242
2243#ifdef CONFIG_IRQ_REMAP
2244 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2245 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2246#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002247}
2248
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002249static void enable_iommus_v2(void)
2250{
2251 struct amd_iommu *iommu;
2252
2253 for_each_iommu(iommu) {
2254 iommu_enable_ppr_log(iommu);
2255 iommu_enable_gt(iommu);
2256 }
2257}
2258
2259static void enable_iommus(void)
2260{
2261 early_enable_iommus();
2262
2263 enable_iommus_v2();
2264}
2265
Joerg Roedel92ac4322009-05-19 19:06:27 +02002266static void disable_iommus(void)
2267{
2268 struct amd_iommu *iommu;
2269
2270 for_each_iommu(iommu)
2271 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002272
2273#ifdef CONFIG_IRQ_REMAP
2274 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2275 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2276#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002277}
2278
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002279/*
2280 * Suspend/Resume support
2281 * disable suspend until real resume implemented
2282 */
2283
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002284static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002285{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002286 struct amd_iommu *iommu;
2287
2288 for_each_iommu(iommu)
2289 iommu_apply_resume_quirks(iommu);
2290
Joerg Roedel736501e2009-05-12 09:56:12 +02002291 /* re-load the hardware */
2292 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002293
2294 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002295}
2296
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002297static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002298{
Joerg Roedel736501e2009-05-12 09:56:12 +02002299 /* disable IOMMUs to go out of the way for BIOS */
2300 disable_iommus();
2301
2302 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002303}
2304
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002305static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002306 .suspend = amd_iommu_suspend,
2307 .resume = amd_iommu_resume,
2308};
2309
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002310static void __init free_iommu_resources(void)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002311{
Lucas Stachebcfa282016-10-26 13:09:53 +02002312 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002313 free_pages((unsigned long)irq_lookup_table,
2314 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002315 irq_lookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002316
Julia Lawalla5919892015-09-13 14:15:31 +02002317 kmem_cache_destroy(amd_iommu_irq_cache);
2318 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002319
2320 free_pages((unsigned long)amd_iommu_rlookup_table,
2321 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002322 amd_iommu_rlookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002323
2324 free_pages((unsigned long)amd_iommu_alias_table,
2325 get_order(alias_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002326 amd_iommu_alias_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002327
2328 free_pages((unsigned long)amd_iommu_dev_table,
2329 get_order(dev_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002330 amd_iommu_dev_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002331
2332 free_iommu_all();
2333
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002334#ifdef CONFIG_GART_IOMMU
2335 /*
2336 * We failed to initialize the AMD IOMMU - try fallback to GART
2337 * if possible.
2338 */
2339 gart_iommu_init();
2340
2341#endif
2342}
2343
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002344/* SB IOAPIC is always on this device in AMD systems */
2345#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2346
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002347static bool __init check_ioapic_information(void)
2348{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002349 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002350 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002351 int idx;
2352
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002353 has_sb_ioapic = false;
2354 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002355
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002356 /*
2357 * If we have map overrides on the kernel command line the
2358 * messages in this function might not describe firmware bugs
2359 * anymore - so be careful
2360 */
2361 if (cmdline_maps)
2362 fw_bug = "";
2363
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002364 for (idx = 0; idx < nr_ioapics; idx++) {
2365 int devid, id = mpc_ioapic_id(idx);
2366
2367 devid = get_ioapic_devid(id);
2368 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002369 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2370 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002371 ret = false;
2372 } else if (devid == IOAPIC_SB_DEVID) {
2373 has_sb_ioapic = true;
2374 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002375 }
2376 }
2377
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002378 if (!has_sb_ioapic) {
2379 /*
2380 * We expect the SB IOAPIC to be listed in the IVRS
2381 * table. The system timer is connected to the SB IOAPIC
2382 * and if we don't have it in the list the system will
2383 * panic at boot time. This situation usually happens
2384 * when the BIOS is buggy and provides us the wrong
2385 * device id for the IOAPIC in the system.
2386 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002387 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002388 }
2389
2390 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002391 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002392
2393 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002394}
2395
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002396static void __init free_dma_resources(void)
2397{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002398 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2399 get_order(MAX_DOMAIN_ID/8));
Joerg Roedelf6019272017-06-16 16:09:58 +02002400 amd_iommu_pd_alloc_bitmap = NULL;
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002401
2402 free_unity_maps();
2403}
2404
Joerg Roedelb65233a2008-07-11 17:14:21 +02002405/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002406 * This is the hardware init function for AMD IOMMU in the system.
2407 * This function is called either from amd_iommu_init or from the interrupt
2408 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002409 *
2410 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002411 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002412 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002413 * 1 pass) Discover the most comprehensive IVHD type to use.
2414 *
2415 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002416 * Upon this information the size of the data structures is
2417 * determined that needs to be allocated.
2418 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002419 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002420 * information in the ACPI table about available AMD IOMMUs
2421 * in the system. It also maps the PCI devices in the
2422 * system to specific IOMMUs
2423 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002424 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002425 * initialized we update them with information about memory
2426 * remapping requirements parsed out of the ACPI table in
2427 * this last pass.
2428 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002429 * After everything is set up the IOMMUs are enabled and the necessary
2430 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002431 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002432static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002433{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002434 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002435 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002436 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002437
Joerg Roedel643511b2012-06-12 12:09:35 +02002438 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002439 return -ENODEV;
2440
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002441 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002442 if (status == AE_NOT_FOUND)
2443 return -ENODEV;
2444 else if (ACPI_FAILURE(status)) {
2445 const char *err = acpi_format_exception(status);
2446 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2447 return -EINVAL;
2448 }
2449
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002450 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002451 * Validate checksum here so we don't need to do it when
2452 * we actually parse the table
2453 */
2454 ret = check_ivrs_checksum(ivrs_base);
2455 if (ret)
Rafael J. Wysocki99e8ccd2017-01-10 14:57:28 +01002456 goto out;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002457
2458 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2459 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2460
2461 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002462 * First parse ACPI tables to find the largest Bus/Dev/Func
2463 * we need to handle. Upon this information the shared data
2464 * structures for the IOMMUs in the system will be allocated
2465 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002466 ret = find_last_devid_acpi(ivrs_base);
2467 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002468 goto out;
2469
Joerg Roedelc5714842008-07-11 17:14:25 +02002470 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2471 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2472 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002473
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002474 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002475 ret = -ENOMEM;
Baoquan Heb3367812017-08-09 16:33:42 +08002476 amd_iommu_dev_table = (void *)__get_free_pages(
2477 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002478 get_order(dev_table_size));
2479 if (amd_iommu_dev_table == NULL)
2480 goto out;
2481
2482 /*
2483 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2484 * IOMMU see for that device
2485 */
2486 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2487 get_order(alias_table_size));
2488 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002489 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002490
2491 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002492 amd_iommu_rlookup_table = (void *)__get_free_pages(
2493 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002494 get_order(rlookup_table_size));
2495 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002496 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002497
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002498 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2499 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002500 get_order(MAX_DOMAIN_ID/8));
2501 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002502 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002503
2504 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002505 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002506 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002507 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002508 amd_iommu_alias_table[i] = i;
2509
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002510 /*
2511 * never allocate domain 0 because its used as the non-allocated and
2512 * error value placeholder
2513 */
Baoquan He5c87f622016-09-15 16:50:51 +08002514 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002515
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002516 spin_lock_init(&amd_iommu_pd_lock);
2517
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002518 /*
2519 * now the data structures are allocated and basically initialized
2520 * start the real acpi table scan
2521 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002522 ret = init_iommu_all(ivrs_base);
2523 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002524 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002525
Joerg Roedel11123742017-06-16 16:09:54 +02002526 /* Disable any previously enabled IOMMUs */
Baoquan He20b46df2017-08-09 16:33:44 +08002527 if (!is_kdump_kernel() || amd_iommu_disabled)
2528 disable_iommus();
Joerg Roedel11123742017-06-16 16:09:54 +02002529
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002530 if (amd_iommu_irq_remap)
2531 amd_iommu_irq_remap = check_ioapic_information();
2532
Joerg Roedel05152a02012-06-15 16:53:51 +02002533 if (amd_iommu_irq_remap) {
2534 /*
2535 * Interrupt remapping enabled, create kmem_cache for the
2536 * remapping tables.
2537 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002538 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002539 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2540 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2541 else
2542 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002543 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002544 remap_cache_sz,
2545 IRQ_TABLE_ALIGNMENT,
2546 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002547 if (!amd_iommu_irq_cache)
2548 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002549
2550 irq_lookup_table = (void *)__get_free_pages(
2551 GFP_KERNEL | __GFP_ZERO,
2552 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002553 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2554 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002555 if (!irq_lookup_table)
2556 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002557 }
2558
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002559 ret = init_memory_definitions(ivrs_base);
2560 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002561 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002562
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002563 /* init the device table */
2564 init_device_table();
2565
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002566out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002567 /* Don't leak any ACPI memory */
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002568 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002569 ivrs_base = NULL;
2570
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002571 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002572}
2573
Gerard Snitselaarae295142012-03-16 11:38:22 -07002574static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002575{
2576 struct amd_iommu *iommu;
2577 int ret = 0;
2578
2579 for_each_iommu(iommu) {
2580 ret = iommu_init_msi(iommu);
2581 if (ret)
2582 goto out;
2583 }
2584
2585out:
2586 return ret;
2587}
2588
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002589static bool detect_ivrs(void)
2590{
2591 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002592 acpi_status status;
2593
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002594 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002595 if (status == AE_NOT_FOUND)
2596 return false;
2597 else if (ACPI_FAILURE(status)) {
2598 const char *err = acpi_format_exception(status);
2599 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2600 return false;
2601 }
2602
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002603 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002604
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002605 /* Make sure ACS will be enabled during PCI probe */
2606 pci_request_acs();
2607
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002608 return true;
2609}
2610
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002611/****************************************************************************
2612 *
2613 * AMD IOMMU Initialization State Machine
2614 *
2615 ****************************************************************************/
2616
2617static int __init state_next(void)
2618{
2619 int ret = 0;
2620
2621 switch (init_state) {
2622 case IOMMU_START_STATE:
2623 if (!detect_ivrs()) {
2624 init_state = IOMMU_NOT_FOUND;
2625 ret = -ENODEV;
2626 } else {
2627 init_state = IOMMU_IVRS_DETECTED;
2628 }
2629 break;
2630 case IOMMU_IVRS_DETECTED:
2631 ret = early_amd_iommu_init();
2632 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
Joerg Roedel7ad820e2017-06-16 16:09:59 +02002633 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2634 pr_info("AMD-Vi: AMD IOMMU disabled on kernel command-line\n");
2635 free_dma_resources();
2636 free_iommu_resources();
2637 init_state = IOMMU_CMDLINE_DISABLED;
2638 ret = -EINVAL;
2639 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002640 break;
2641 case IOMMU_ACPI_FINISHED:
2642 early_enable_iommus();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002643 x86_platform.iommu_shutdown = disable_iommus;
2644 init_state = IOMMU_ENABLED;
2645 break;
2646 case IOMMU_ENABLED:
Joerg Roedel74ddda72017-07-26 14:17:55 +02002647 register_syscore_ops(&amd_iommu_syscore_ops);
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002648 ret = amd_iommu_init_pci();
2649 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2650 enable_iommus_v2();
2651 break;
2652 case IOMMU_PCI_INIT:
2653 ret = amd_iommu_enable_interrupts();
2654 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2655 break;
2656 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002657 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002658 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2659 break;
2660 case IOMMU_DMA_OPS:
2661 init_state = IOMMU_INITIALIZED;
2662 break;
2663 case IOMMU_INITIALIZED:
2664 /* Nothing to do */
2665 break;
2666 case IOMMU_NOT_FOUND:
2667 case IOMMU_INIT_ERROR:
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002668 case IOMMU_CMDLINE_DISABLED:
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002669 /* Error states => do nothing */
2670 ret = -EINVAL;
2671 break;
2672 default:
2673 /* Unknown state */
2674 BUG();
2675 }
2676
2677 return ret;
2678}
2679
2680static int __init iommu_go_to_state(enum iommu_init_state state)
2681{
Joerg Roedel151b0902017-06-16 16:09:57 +02002682 int ret = -EINVAL;
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002683
2684 while (init_state != state) {
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002685 if (init_state == IOMMU_NOT_FOUND ||
2686 init_state == IOMMU_INIT_ERROR ||
2687 init_state == IOMMU_CMDLINE_DISABLED)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002688 break;
Joerg Roedel151b0902017-06-16 16:09:57 +02002689 ret = state_next();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002690 }
2691
2692 return ret;
2693}
2694
Joerg Roedel6b474b82012-06-26 16:46:04 +02002695#ifdef CONFIG_IRQ_REMAP
2696int __init amd_iommu_prepare(void)
2697{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002698 int ret;
2699
Jiang Liu7fa1c842015-01-07 15:31:42 +08002700 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002701
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002702 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2703 if (ret)
2704 return ret;
2705 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002706}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002707
Joerg Roedel6b474b82012-06-26 16:46:04 +02002708int __init amd_iommu_enable(void)
2709{
2710 int ret;
2711
2712 ret = iommu_go_to_state(IOMMU_ENABLED);
2713 if (ret)
2714 return ret;
2715
2716 irq_remapping_enabled = 1;
Suravee Suthikulpanit90fcffd2018-06-27 10:31:22 -05002717 return amd_iommu_xt_mode;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002718}
2719
2720void amd_iommu_disable(void)
2721{
2722 amd_iommu_suspend();
2723}
2724
2725int amd_iommu_reenable(int mode)
2726{
2727 amd_iommu_resume();
2728
2729 return 0;
2730}
2731
2732int __init amd_iommu_enable_faulting(void)
2733{
2734 /* We enable MSI later when PCI is initialized */
2735 return 0;
2736}
2737#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002738
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002739/*
2740 * This is the core init function for AMD IOMMU hardware in the system.
2741 * This function is called from the generic x86 DMA layer initialization
2742 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002743 */
2744static int __init amd_iommu_init(void)
2745{
Gary R Hook7d0f5fd2018-06-12 16:41:30 -05002746 struct amd_iommu *iommu;
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002747 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002748
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002749 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2750 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002751 free_dma_resources();
2752 if (!irq_remapping_enabled) {
2753 disable_iommus();
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002754 free_iommu_resources();
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002755 } else {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002756 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
Gary R Hook7d0f5fd2018-06-12 16:41:30 -05002762 for_each_iommu(iommu)
2763 amd_iommu_debugfs_setup(iommu);
2764
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002765 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002766}
2767
Tom Lendacky2543a782017-07-17 16:10:24 -05002768static bool amd_iommu_sme_check(void)
2769{
2770 if (!sme_active() || (boot_cpu_data.x86 != 0x17))
2771 return true;
2772
2773 /* For Fam17h, a specific level of support is required */
2774 if (boot_cpu_data.microcode >= 0x08001205)
2775 return true;
2776
2777 if ((boot_cpu_data.microcode >= 0x08001126) &&
2778 (boot_cpu_data.microcode <= 0x080011ff))
2779 return true;
2780
2781 pr_notice("AMD-Vi: IOMMU not currently supported when SME is active\n");
2782
2783 return false;
2784}
2785
Joerg Roedelb65233a2008-07-11 17:14:21 +02002786/****************************************************************************
2787 *
2788 * Early detect code. This code runs at IOMMU detection time in the DMA
2789 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2790 * IOMMUs
2791 *
2792 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002793int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002794{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002795 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002796
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002797 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002798 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002799
Tom Lendacky2543a782017-07-17 16:10:24 -05002800 if (!amd_iommu_sme_check())
2801 return -ENODEV;
2802
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002803 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2804 if (ret)
2805 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002806
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002807 amd_iommu_detected = true;
2808 iommu_detected = 1;
2809 x86_init.iommu.iommu_init = amd_iommu_init;
2810
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002811 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002812}
2813
Joerg Roedelb65233a2008-07-11 17:14:21 +02002814/****************************************************************************
2815 *
2816 * Parsing functions for the AMD IOMMU specific kernel command line
2817 * options.
2818 *
2819 ****************************************************************************/
2820
Joerg Roedelfefda112009-05-20 12:21:42 +02002821static int __init parse_amd_iommu_dump(char *str)
2822{
2823 amd_iommu_dump = true;
2824
2825 return 1;
2826}
2827
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002828static int __init parse_amd_iommu_intr(char *str)
2829{
2830 for (; *str; ++str) {
2831 if (strncmp(str, "legacy", 6) == 0) {
2832 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2833 break;
2834 }
2835 if (strncmp(str, "vapic", 5) == 0) {
2836 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2837 break;
2838 }
2839 }
2840 return 1;
2841}
2842
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002843static int __init parse_amd_iommu_options(char *str)
2844{
2845 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002846 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002847 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002848 if (strncmp(str, "off", 3) == 0)
2849 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002850 if (strncmp(str, "force_isolation", 15) == 0)
2851 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002852 }
2853
2854 return 1;
2855}
2856
Joerg Roedel440e89982013-04-09 16:35:28 +02002857static int __init parse_ivrs_ioapic(char *str)
2858{
2859 unsigned int bus, dev, fn;
2860 int ret, id, i;
2861 u16 devid;
2862
2863 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2864
2865 if (ret != 4) {
2866 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2867 return 1;
2868 }
2869
2870 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2871 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2872 str);
2873 return 1;
2874 }
2875
2876 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2877
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002878 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002879 i = early_ioapic_map_size++;
2880 early_ioapic_map[i].id = id;
2881 early_ioapic_map[i].devid = devid;
2882 early_ioapic_map[i].cmd_line = true;
2883
2884 return 1;
2885}
2886
2887static int __init parse_ivrs_hpet(char *str)
2888{
2889 unsigned int bus, dev, fn;
2890 int ret, id, i;
2891 u16 devid;
2892
2893 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2894
2895 if (ret != 4) {
2896 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2897 return 1;
2898 }
2899
2900 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2901 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2902 str);
2903 return 1;
2904 }
2905
2906 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2907
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002908 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002909 i = early_hpet_map_size++;
2910 early_hpet_map[i].id = id;
2911 early_hpet_map[i].devid = devid;
2912 early_hpet_map[i].cmd_line = true;
2913
2914 return 1;
2915}
2916
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002917static int __init parse_ivrs_acpihid(char *str)
2918{
2919 u32 bus, dev, fn;
2920 char *hid, *uid, *p;
2921 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2922 int ret, i;
2923
2924 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2925 if (ret != 4) {
2926 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2927 return 1;
2928 }
2929
2930 p = acpiid;
2931 hid = strsep(&p, ":");
2932 uid = p;
2933
2934 if (!hid || !(*hid) || !uid) {
2935 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2936 return 1;
2937 }
2938
2939 i = early_acpihid_map_size++;
2940 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2941 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2942 early_acpihid_map[i].devid =
2943 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2944 early_acpihid_map[i].cmd_line = true;
2945
2946 return 1;
2947}
2948
Joerg Roedel440e89982013-04-09 16:35:28 +02002949__setup("amd_iommu_dump", parse_amd_iommu_dump);
2950__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002951__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002952__setup("ivrs_ioapic", parse_ivrs_ioapic);
2953__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002954__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002955
2956IOMMU_INIT_FINISH(amd_iommu_detect,
2957 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002958 NULL,
2959 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002960
2961bool amd_iommu_v2_supported(void)
2962{
2963 return amd_iommu_v2_present;
2964}
2965EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002966
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002967struct amd_iommu *get_amd_iommu(unsigned int idx)
2968{
2969 unsigned int i = 0;
2970 struct amd_iommu *iommu;
2971
2972 for_each_iommu(iommu)
2973 if (i++ == idx)
2974 return iommu;
2975 return NULL;
2976}
2977EXPORT_SYMBOL(get_amd_iommu);
2978
Steven L Kinney30861dd2013-06-05 16:11:48 -05002979/****************************************************************************
2980 *
2981 * IOMMU EFR Performance Counter support functionality. This code allows
2982 * access to the IOMMU PC functionality.
2983 *
2984 ****************************************************************************/
2985
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002986u8 amd_iommu_pc_get_max_banks(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002987{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002988 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002989
Steven L Kinney30861dd2013-06-05 16:11:48 -05002990 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002991 return iommu->max_banks;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002992
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002993 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002994}
2995EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2996
2997bool amd_iommu_pc_supported(void)
2998{
2999 return amd_iommu_pc_present;
3000}
3001EXPORT_SYMBOL(amd_iommu_pc_supported);
3002
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003003u8 amd_iommu_pc_get_max_counters(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05003004{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003005 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003006
Steven L Kinney30861dd2013-06-05 16:11:48 -05003007 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003008 return iommu->max_counters;
Steven L Kinney30861dd2013-06-05 16:11:48 -05003009
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06003010 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05003011}
3012EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
3013
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003014static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
3015 u8 fxn, u64 *value, bool is_write)
Steven L Kinney30861dd2013-06-05 16:11:48 -05003016{
Steven L Kinney30861dd2013-06-05 16:11:48 -05003017 u32 offset;
3018 u32 max_offset_lim;
3019
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003020 /* Make sure the IOMMU PC resource is available */
3021 if (!amd_iommu_pc_present)
3022 return -ENODEV;
3023
Steven L Kinney30861dd2013-06-05 16:11:48 -05003024 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003025 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05003026 return -ENODEV;
3027
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003028 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003029
3030 /* Limit the offset to the hw defined mmio region aperture */
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003031 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
Steven L Kinney30861dd2013-06-05 16:11:48 -05003032 (iommu->max_counters << 8) | 0x28);
3033 if ((offset < MMIO_CNTR_REG_OFFSET) ||
3034 (offset > max_offset_lim))
3035 return -EINVAL;
3036
3037 if (is_write) {
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003038 u64 val = *value & GENMASK_ULL(47, 0);
3039
3040 writel((u32)val, iommu->mmio_base + offset);
3041 writel((val >> 32), iommu->mmio_base + offset + 4);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003042 } else {
3043 *value = readl(iommu->mmio_base + offset + 4);
3044 *value <<= 32;
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06003045 *value |= readl(iommu->mmio_base + offset);
3046 *value &= GENMASK_ULL(47, 0);
Steven L Kinney30861dd2013-06-05 16:11:48 -05003047 }
3048
3049 return 0;
3050}
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003051
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003052int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003053{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003054 if (!iommu)
3055 return -EINVAL;
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003056
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003057 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01003058}
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06003059EXPORT_SYMBOL(amd_iommu_pc_get_reg);
3060
3061int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3062{
3063 if (!iommu)
3064 return -EINVAL;
3065
3066 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3067}
3068EXPORT_SYMBOL(amd_iommu_pc_set_reg);