blob: c348732f27d7a1591f51ebaca407589441d8dac4 [file] [log] [blame]
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02001/*
Joerg Roedel5d0d7152010-10-13 11:13:21 +02002 * Copyright (C) 2007-2010 Advanced Micro Devices, Inc.
Joerg Roedel63ce3ae2015-02-04 16:12:55 +01003 * Author: Joerg Roedel <jroedel@suse.de>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +02004 * Leo Duran <leo.duran@amd.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/pci.h>
21#include <linux/acpi.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020022#include <linux/list.h>
Baoquan He5c87f622016-09-15 16:50:51 +080023#include <linux/bitmap.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +010025#include <linux/syscore_ops.h>
Joerg Roedela80dc3e2008-09-11 16:51:41 +020026#include <linux/interrupt.h>
27#include <linux/msi.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020028#include <linux/amd-iommu.h>
Joerg Roedel400a28a2011-11-28 15:11:02 +010029#include <linux/export.h>
Alex Williamson066f2e92014-06-12 16:12:37 -060030#include <linux/iommu.h>
Lucas Stachebcfa282016-10-26 13:09:53 +020031#include <linux/kmemleak.h>
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020032#include <asm/pci-direct.h>
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090033#include <asm/iommu.h>
Joerg Roedel1d9b16d2008-11-27 18:39:15 +010034#include <asm/gart.h>
FUJITA Tomonoriea1b0d32009-11-10 19:46:15 +090035#include <asm/x86_init.h>
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -040036#include <asm/iommu_table.h>
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +020037#include <asm/io_apic.h>
Joerg Roedel6b474b82012-06-26 16:46:04 +020038#include <asm/irq_remapping.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020039
Baoquan He3ac3e5e2017-08-09 16:33:38 +080040#include <linux/crash_dump.h>
Joerg Roedel403f81d2011-06-14 16:44:25 +020041#include "amd_iommu_proto.h"
42#include "amd_iommu_types.h"
Joerg Roedel05152a02012-06-15 16:53:51 +020043#include "irq_remapping.h"
Joerg Roedel403f81d2011-06-14 16:44:25 +020044
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020045/*
46 * definitions for the ACPI scanning code
47 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020048#define IVRS_HEADER_LENGTH 48
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020049
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040050#define ACPI_IVHD_TYPE_MAX_SUPPORTED 0x40
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020051#define ACPI_IVMD_TYPE_ALL 0x20
52#define ACPI_IVMD_TYPE 0x21
53#define ACPI_IVMD_TYPE_RANGE 0x22
54
55#define IVHD_DEV_ALL 0x01
56#define IVHD_DEV_SELECT 0x02
57#define IVHD_DEV_SELECT_RANGE_START 0x03
58#define IVHD_DEV_RANGE_END 0x04
59#define IVHD_DEV_ALIAS 0x42
60#define IVHD_DEV_ALIAS_RANGE 0x43
61#define IVHD_DEV_EXT_SELECT 0x46
62#define IVHD_DEV_EXT_SELECT_RANGE 0x47
Joerg Roedel6efed632012-06-14 15:52:58 +020063#define IVHD_DEV_SPECIAL 0x48
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -040064#define IVHD_DEV_ACPI_HID 0xf0
Joerg Roedel6efed632012-06-14 15:52:58 +020065
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -040066#define UID_NOT_PRESENT 0
67#define UID_IS_INTEGER 1
68#define UID_IS_CHARACTER 2
69
Joerg Roedel6efed632012-06-14 15:52:58 +020070#define IVHD_SPECIAL_IOAPIC 1
71#define IVHD_SPECIAL_HPET 2
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020072
Joerg Roedel6da73422009-05-04 11:44:38 +020073#define IVHD_FLAG_HT_TUN_EN_MASK 0x01
74#define IVHD_FLAG_PASSPW_EN_MASK 0x02
75#define IVHD_FLAG_RESPASSPW_EN_MASK 0x04
76#define IVHD_FLAG_ISOC_EN_MASK 0x08
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +020077
78#define IVMD_FLAG_EXCL_RANGE 0x08
79#define IVMD_FLAG_UNITY_MAP 0x01
80
81#define ACPI_DEVFLAG_INITPASS 0x01
82#define ACPI_DEVFLAG_EXTINT 0x02
83#define ACPI_DEVFLAG_NMI 0x04
84#define ACPI_DEVFLAG_SYSMGT1 0x10
85#define ACPI_DEVFLAG_SYSMGT2 0x20
86#define ACPI_DEVFLAG_LINT0 0x40
87#define ACPI_DEVFLAG_LINT1 0x80
88#define ACPI_DEVFLAG_ATSDIS 0x10000000
89
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -050090#define LOOP_TIMEOUT 100000
Joerg Roedelb65233a2008-07-11 17:14:21 +020091/*
92 * ACPI table definitions
93 *
94 * These data structures are laid over the table to parse the important values
95 * out of it.
96 */
97
Joerg Roedelb0119e82017-02-01 13:23:08 +010098extern const struct iommu_ops amd_iommu_ops;
99
Joerg Roedelb65233a2008-07-11 17:14:21 +0200100/*
101 * structure describing one IOMMU in the ACPI table. Typically followed by one
102 * or more ivhd_entrys.
103 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200104struct ivhd_header {
105 u8 type;
106 u8 flags;
107 u16 length;
108 u16 devid;
109 u16 cap_ptr;
110 u64 mmio_phys;
111 u16 pci_seg;
112 u16 info;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -0400113 u32 efr_attr;
114
115 /* Following only valid on IVHD type 11h and 40h */
116 u64 efr_reg; /* Exact copy of MMIO_EXT_FEATURES */
117 u64 res;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200118} __attribute__((packed));
119
Joerg Roedelb65233a2008-07-11 17:14:21 +0200120/*
121 * A device entry describing which devices a specific IOMMU translates and
122 * which requestor ids they use.
123 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200124struct ivhd_entry {
125 u8 type;
126 u16 devid;
127 u8 flags;
128 u32 ext;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400129 u32 hidh;
130 u64 cid;
131 u8 uidf;
132 u8 uidl;
133 u8 uid;
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200134} __attribute__((packed));
135
Joerg Roedelb65233a2008-07-11 17:14:21 +0200136/*
137 * An AMD IOMMU memory definition structure. It defines things like exclusion
138 * ranges for devices and regions that should be unity mapped.
139 */
Joerg Roedelf6e2e6b2008-06-26 21:27:39 +0200140struct ivmd_header {
141 u8 type;
142 u8 flags;
143 u16 length;
144 u16 devid;
145 u16 aux;
146 u64 resv;
147 u64 range_start;
148 u64 range_length;
149} __attribute__((packed));
150
Joerg Roedelfefda112009-05-20 12:21:42 +0200151bool amd_iommu_dump;
Joerg Roedel05152a02012-06-15 16:53:51 +0200152bool amd_iommu_irq_remap __read_mostly;
Joerg Roedelfefda112009-05-20 12:21:42 +0200153
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -0500154int amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -0500155
Joerg Roedel02f3b3f2012-06-11 17:45:25 +0200156static bool amd_iommu_detected;
Joerg Roedela5235722010-05-11 17:12:33 +0200157static bool __initdata amd_iommu_disabled;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400158static int amd_iommu_target_ivhd_type;
Joerg Roedelc1cbebe2008-07-03 19:35:10 +0200159
Joerg Roedelb65233a2008-07-11 17:14:21 +0200160u16 amd_iommu_last_bdf; /* largest PCI device id we have
161 to handle */
Joerg Roedel2e228472008-07-11 17:14:31 +0200162LIST_HEAD(amd_iommu_unity_map); /* a list of required unity mappings
Joerg Roedelb65233a2008-07-11 17:14:21 +0200163 we find in ACPI */
Viresh Kumar621a5f72015-09-26 15:04:07 -0700164bool amd_iommu_unmap_flush; /* if true, flush on every unmap */
Joerg Roedel928abd22008-06-26 21:27:40 +0200165
Joerg Roedel2e228472008-07-11 17:14:31 +0200166LIST_HEAD(amd_iommu_list); /* list of all AMD IOMMUs in the
Joerg Roedelb65233a2008-07-11 17:14:21 +0200167 system */
168
Joerg Roedelbb527772009-11-20 14:31:51 +0100169/* Array to assign indices to IOMMUs*/
170struct amd_iommu *amd_iommus[MAX_IOMMUS];
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600171
172/* Number of IOMMUs present in the system */
173static int amd_iommus_present;
Joerg Roedelbb527772009-11-20 14:31:51 +0100174
Joerg Roedel318afd42009-11-23 18:32:38 +0100175/* IOMMUs have a non-present cache? */
176bool amd_iommu_np_cache __read_mostly;
Joerg Roedel60f723b2011-04-05 12:50:24 +0200177bool amd_iommu_iotlb_sup __read_mostly = true;
Joerg Roedel318afd42009-11-23 18:32:38 +0100178
Suravee Suthikulpanita919a012014-03-05 18:54:18 -0600179u32 amd_iommu_max_pasid __read_mostly = ~0;
Joerg Roedel62f71ab2011-11-10 14:41:57 +0100180
Joerg Roedel400a28a2011-11-28 15:11:02 +0100181bool amd_iommu_v2_present __read_mostly;
Joerg Roedel4160cd92015-08-13 11:31:48 +0200182static bool amd_iommu_pc_present __read_mostly;
Joerg Roedel400a28a2011-11-28 15:11:02 +0100183
Joerg Roedel5abcdba2011-12-01 15:49:45 +0100184bool amd_iommu_force_isolation __read_mostly;
185
Joerg Roedelb65233a2008-07-11 17:14:21 +0200186/*
Joerg Roedelaeb26f52009-11-20 16:44:01 +0100187 * List of protection domains - used during resume
188 */
189LIST_HEAD(amd_iommu_pd_list);
190spinlock_t amd_iommu_pd_lock;
191
192/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200193 * Pointer to the device table which is shared by all AMD IOMMUs
194 * it is indexed by the PCI device id or the HT unit id and contains
195 * information about the domain the device belongs to as well as the
196 * page table root pointer.
197 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200198struct dev_table_entry *amd_iommu_dev_table;
Baoquan He45a01c42017-08-09 16:33:37 +0800199/*
200 * Pointer to a device table which the content of old device table
201 * will be copied to. It's only be used in kdump kernel.
202 */
203static struct dev_table_entry *old_dev_tbl_cpy;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200204
205/*
206 * The alias table is a driver specific data structure which contains the
207 * mappings of the PCI device ids to the actual requestor ids on the IOMMU.
208 * More than one device can share the same requestor id.
209 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200210u16 *amd_iommu_alias_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200211
212/*
213 * The rlookup table is used to find the IOMMU which is responsible
214 * for a specific device. It is also indexed by the PCI device id.
215 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200216struct amd_iommu **amd_iommu_rlookup_table;
Joerg Roedelb65233a2008-07-11 17:14:21 +0200217
218/*
Joerg Roedel0ea2c422012-06-15 18:05:20 +0200219 * This table is used to find the irq remapping table for a given device id
220 * quickly.
221 */
222struct irq_remap_table **irq_lookup_table;
223
224/*
Frank Arnolddf805ab2012-08-27 19:21:04 +0200225 * AMD IOMMU allows up to 2^16 different protection domains. This is a bitmap
Joerg Roedelb65233a2008-07-11 17:14:21 +0200226 * to know which ones are already in use.
227 */
Joerg Roedel928abd22008-06-26 21:27:40 +0200228unsigned long *amd_iommu_pd_alloc_bitmap;
229
Joerg Roedelb65233a2008-07-11 17:14:21 +0200230static u32 dev_table_size; /* size of the device table */
231static u32 alias_table_size; /* size of the alias table */
232static u32 rlookup_table_size; /* size if the rlookup table */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200233
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200234enum iommu_init_state {
235 IOMMU_START_STATE,
236 IOMMU_IVRS_DETECTED,
237 IOMMU_ACPI_FINISHED,
238 IOMMU_ENABLED,
239 IOMMU_PCI_INIT,
240 IOMMU_INTERRUPTS_EN,
241 IOMMU_DMA_OPS,
242 IOMMU_INITIALIZED,
243 IOMMU_NOT_FOUND,
244 IOMMU_INIT_ERROR,
Joerg Roedel1b1e9422017-06-16 16:09:56 +0200245 IOMMU_CMDLINE_DISABLED,
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200246};
247
Joerg Roedel235dacb2013-04-09 17:53:14 +0200248/* Early ioapic and hpet maps from kernel command line */
249#define EARLY_MAP_SIZE 4
250static struct devid_map __initdata early_ioapic_map[EARLY_MAP_SIZE];
251static struct devid_map __initdata early_hpet_map[EARLY_MAP_SIZE];
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400252static struct acpihid_map_entry __initdata early_acpihid_map[EARLY_MAP_SIZE];
253
Joerg Roedel235dacb2013-04-09 17:53:14 +0200254static int __initdata early_ioapic_map_size;
255static int __initdata early_hpet_map_size;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -0400256static int __initdata early_acpihid_map_size;
257
Joerg Roedeldfbb6d42013-04-09 19:06:18 +0200258static bool __initdata cmdline_maps;
Joerg Roedel235dacb2013-04-09 17:53:14 +0200259
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200260static enum iommu_init_state init_state = IOMMU_START_STATE;
261
Gerard Snitselaarae295142012-03-16 11:38:22 -0700262static int amd_iommu_enable_interrupts(void);
Joerg Roedel2c0ae172012-06-12 15:59:30 +0200263static int __init iommu_go_to_state(enum iommu_init_state state);
Joerg Roedelaafd8ba2015-05-28 18:41:39 +0200264static void init_device_table_dma(void);
Joerg Roedel3d9761e2012-03-15 16:39:21 +0100265
Baoquan He3ac3e5e2017-08-09 16:33:38 +0800266static bool __initdata amd_iommu_pre_enabled = true;
267
Baoquan He4c232a72017-08-09 16:33:33 +0800268bool translation_pre_enabled(struct amd_iommu *iommu)
269{
270 return (iommu->flags & AMD_IOMMU_FLAG_TRANS_PRE_ENABLED);
271}
272
273static void clear_translation_pre_enabled(struct amd_iommu *iommu)
274{
275 iommu->flags &= ~AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
276}
277
278static void init_translation_status(struct amd_iommu *iommu)
279{
280 u32 ctrl;
281
282 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
283 if (ctrl & (1<<CONTROL_IOMMU_EN))
284 iommu->flags |= AMD_IOMMU_FLAG_TRANS_PRE_ENABLED;
285}
286
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200287static inline void update_last_devid(u16 devid)
288{
289 if (devid > amd_iommu_last_bdf)
290 amd_iommu_last_bdf = devid;
291}
292
Joerg Roedelc5714842008-07-11 17:14:25 +0200293static inline unsigned long tbl_size(int entry_size)
294{
295 unsigned shift = PAGE_SHIFT +
Neil Turton421f9092009-05-14 14:00:35 +0100296 get_order(((int)amd_iommu_last_bdf + 1) * entry_size);
Joerg Roedelc5714842008-07-11 17:14:25 +0200297
298 return 1UL << shift;
299}
300
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -0600301int amd_iommu_get_num_iommus(void)
302{
303 return amd_iommus_present;
304}
305
Matthew Garrett5bcd7572010-10-04 14:59:31 -0400306/* Access to l1 and l2 indexed register spaces */
307
308static u32 iommu_read_l1(struct amd_iommu *iommu, u16 l1, u8 address)
309{
310 u32 val;
311
312 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
313 pci_read_config_dword(iommu->dev, 0xfc, &val);
314 return val;
315}
316
317static void iommu_write_l1(struct amd_iommu *iommu, u16 l1, u8 address, u32 val)
318{
319 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16 | 1 << 31));
320 pci_write_config_dword(iommu->dev, 0xfc, val);
321 pci_write_config_dword(iommu->dev, 0xf8, (address | l1 << 16));
322}
323
324static u32 iommu_read_l2(struct amd_iommu *iommu, u8 address)
325{
326 u32 val;
327
328 pci_write_config_dword(iommu->dev, 0xf0, address);
329 pci_read_config_dword(iommu->dev, 0xf4, &val);
330 return val;
331}
332
333static void iommu_write_l2(struct amd_iommu *iommu, u8 address, u32 val)
334{
335 pci_write_config_dword(iommu->dev, 0xf0, (address | 1 << 8));
336 pci_write_config_dword(iommu->dev, 0xf4, val);
337}
338
Joerg Roedelb65233a2008-07-11 17:14:21 +0200339/****************************************************************************
340 *
341 * AMD IOMMU MMIO register space handling functions
342 *
343 * These functions are used to program the IOMMU device registers in
344 * MMIO space required for that driver.
345 *
346 ****************************************************************************/
347
348/*
349 * This function set the exclusion range in the IOMMU. DMA accesses to the
350 * exclusion range are passed through untranslated
351 */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200352static void iommu_set_exclusion_range(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200353{
354 u64 start = iommu->exclusion_start & PAGE_MASK;
355 u64 limit = (start + iommu->exclusion_length) & PAGE_MASK;
356 u64 entry;
357
358 if (!iommu->exclusion_start)
359 return;
360
361 entry = start | MMIO_EXCL_ENABLE_MASK;
362 memcpy_toio(iommu->mmio_base + MMIO_EXCL_BASE_OFFSET,
363 &entry, sizeof(entry));
364
365 entry = limit;
366 memcpy_toio(iommu->mmio_base + MMIO_EXCL_LIMIT_OFFSET,
367 &entry, sizeof(entry));
368}
369
Joerg Roedelb65233a2008-07-11 17:14:21 +0200370/* Programs the physical address of the device table into the IOMMU hardware */
Jan Beulich6b7f0002012-03-08 08:58:13 +0000371static void iommu_set_device_table(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200372{
Andreas Herrmannf6098912008-10-16 16:27:36 +0200373 u64 entry;
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200374
375 BUG_ON(iommu->mmio_base == NULL);
376
377 entry = virt_to_phys(amd_iommu_dev_table);
378 entry |= (dev_table_size >> 12) - 1;
379 memcpy_toio(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET,
380 &entry, sizeof(entry));
381}
382
Joerg Roedelb65233a2008-07-11 17:14:21 +0200383/* Generic functions to enable/disable certain features of the IOMMU. */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200384static void iommu_feature_enable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200385{
386 u32 ctrl;
387
388 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
389 ctrl |= (1 << bit);
390 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
391}
392
Joerg Roedelca0207112009-10-28 18:02:26 +0100393static void iommu_feature_disable(struct amd_iommu *iommu, u8 bit)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200394{
395 u32 ctrl;
396
Joerg Roedel199d0d52008-09-17 16:45:59 +0200397 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200398 ctrl &= ~(1 << bit);
399 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
400}
401
Joerg Roedel1456e9d2011-12-22 14:51:53 +0100402static void iommu_set_inv_tlb_timeout(struct amd_iommu *iommu, int timeout)
403{
404 u32 ctrl;
405
406 ctrl = readl(iommu->mmio_base + MMIO_CONTROL_OFFSET);
407 ctrl &= ~CTRL_INV_TO_MASK;
408 ctrl |= (timeout << CONTROL_INV_TIMEOUT) & CTRL_INV_TO_MASK;
409 writel(ctrl, iommu->mmio_base + MMIO_CONTROL_OFFSET);
410}
411
Joerg Roedelb65233a2008-07-11 17:14:21 +0200412/* Function to enable the hardware */
Joerg Roedel05f92db2009-05-12 09:52:46 +0200413static void iommu_enable(struct amd_iommu *iommu)
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200414{
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200415 iommu_feature_enable(iommu, CONTROL_IOMMU_EN);
Joerg Roedelb2026aa2008-06-26 21:27:44 +0200416}
417
Joerg Roedel92ac4322009-05-19 19:06:27 +0200418static void iommu_disable(struct amd_iommu *iommu)
Joerg Roedel126c52b2008-09-09 16:47:35 +0200419{
Chris Wrighta8c485b2009-06-15 15:53:45 +0200420 /* Disable command buffer */
421 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
422
423 /* Disable event logging and event interrupts */
424 iommu_feature_disable(iommu, CONTROL_EVT_INT_EN);
425 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
426
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500427 /* Disable IOMMU GA_LOG */
428 iommu_feature_disable(iommu, CONTROL_GALOG_EN);
429 iommu_feature_disable(iommu, CONTROL_GAINT_EN);
430
Chris Wrighta8c485b2009-06-15 15:53:45 +0200431 /* Disable IOMMU hardware itself */
Joerg Roedel92ac4322009-05-19 19:06:27 +0200432 iommu_feature_disable(iommu, CONTROL_IOMMU_EN);
Joerg Roedel126c52b2008-09-09 16:47:35 +0200433}
434
Joerg Roedelb65233a2008-07-11 17:14:21 +0200435/*
436 * mapping and unmapping functions for the IOMMU MMIO space. Each AMD IOMMU in
437 * the system has one.
438 */
Steven L Kinney30861dd2013-06-05 16:11:48 -0500439static u8 __iomem * __init iommu_map_mmio_space(u64 address, u64 end)
Joerg Roedel6c567472008-06-26 21:27:43 +0200440{
Steven L Kinney30861dd2013-06-05 16:11:48 -0500441 if (!request_mem_region(address, end, "amd_iommu")) {
442 pr_err("AMD-Vi: Can not reserve memory region %llx-%llx for mmio\n",
443 address, end);
Joerg Roedele82752d2010-05-28 14:26:48 +0200444 pr_err("AMD-Vi: This is a BIOS bug. Please contact your hardware vendor\n");
Joerg Roedel6c567472008-06-26 21:27:43 +0200445 return NULL;
Joerg Roedele82752d2010-05-28 14:26:48 +0200446 }
Joerg Roedel6c567472008-06-26 21:27:43 +0200447
Steven L Kinney30861dd2013-06-05 16:11:48 -0500448 return (u8 __iomem *)ioremap_nocache(address, end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200449}
450
451static void __init iommu_unmap_mmio_space(struct amd_iommu *iommu)
452{
453 if (iommu->mmio_base)
454 iounmap(iommu->mmio_base);
Steven L Kinney30861dd2013-06-05 16:11:48 -0500455 release_mem_region(iommu->mmio_phys, iommu->mmio_phys_end);
Joerg Roedel6c567472008-06-26 21:27:43 +0200456}
457
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400458static inline u32 get_ivhd_header_size(struct ivhd_header *h)
459{
460 u32 size = 0;
461
462 switch (h->type) {
463 case 0x10:
464 size = 24;
465 break;
466 case 0x11:
467 case 0x40:
468 size = 40;
469 break;
470 }
471 return size;
472}
473
Joerg Roedelb65233a2008-07-11 17:14:21 +0200474/****************************************************************************
475 *
476 * The functions below belong to the first pass of AMD IOMMU ACPI table
477 * parsing. In this pass we try to find out the highest device id this
478 * code has to handle. Upon this information the size of the shared data
479 * structures is determined later.
480 *
481 ****************************************************************************/
482
483/*
Joerg Roedelb514e552008-09-17 17:14:27 +0200484 * This function calculates the length of a given IVHD entry
485 */
486static inline int ivhd_entry_length(u8 *ivhd)
487{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400488 u32 type = ((struct ivhd_entry *)ivhd)->type;
489
490 if (type < 0x80) {
491 return 0x04 << (*ivhd >> 6);
492 } else if (type == IVHD_DEV_ACPI_HID) {
493 /* For ACPI_HID, offset 21 is uid len */
494 return *((u8 *)ivhd + 21) + 22;
495 }
496 return 0;
Joerg Roedelb514e552008-09-17 17:14:27 +0200497}
498
499/*
Joerg Roedelb65233a2008-07-11 17:14:21 +0200500 * After reading the highest device id from the IOMMU PCI capability header
501 * this function looks if there is a higher device id defined in the ACPI table
502 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200503static int __init find_last_devid_from_ivhd(struct ivhd_header *h)
504{
505 u8 *p = (void *)h, *end = (void *)h;
506 struct ivhd_entry *dev;
507
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -0400508 u32 ivhd_size = get_ivhd_header_size(h);
509
510 if (!ivhd_size) {
511 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
512 return -EINVAL;
513 }
514
515 p += ivhd_size;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200516 end += h->length;
517
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200518 while (p < end) {
519 dev = (struct ivhd_entry *)p;
520 switch (dev->type) {
Joerg Roedeld1259412015-10-20 17:33:43 +0200521 case IVHD_DEV_ALL:
522 /* Use maximum BDF value for DEV_ALL */
523 update_last_devid(0xffff);
524 break;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200525 case IVHD_DEV_SELECT:
526 case IVHD_DEV_RANGE_END:
527 case IVHD_DEV_ALIAS:
528 case IVHD_DEV_EXT_SELECT:
Joerg Roedelb65233a2008-07-11 17:14:21 +0200529 /* all the above subfield types refer to device ids */
Joerg Roedel208ec8c2008-07-11 17:14:24 +0200530 update_last_devid(dev->devid);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200531 break;
532 default:
533 break;
534 }
Joerg Roedelb514e552008-09-17 17:14:27 +0200535 p += ivhd_entry_length(p);
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200536 }
537
538 WARN_ON(p != end);
539
540 return 0;
541}
542
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400543static int __init check_ivrs_checksum(struct acpi_table_header *table)
544{
545 int i;
546 u8 checksum = 0, *p = (u8 *)table;
547
548 for (i = 0; i < table->length; ++i)
549 checksum += p[i];
550 if (checksum != 0) {
551 /* ACPI table corrupt */
552 pr_err(FW_BUG "AMD-Vi: IVRS invalid checksum\n");
553 return -ENODEV;
554 }
555
556 return 0;
557}
558
Joerg Roedelb65233a2008-07-11 17:14:21 +0200559/*
560 * Iterate over all IVHD entries in the ACPI table and find the highest device
561 * id which we need to handle. This is the first of three functions which parse
562 * the ACPI table. So we check the checksum here.
563 */
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200564static int __init find_last_devid_acpi(struct acpi_table_header *table)
565{
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400566 u8 *p = (u8 *)table, *end = (u8 *)table;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200567 struct ivhd_header *h;
568
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200569 p += IVRS_HEADER_LENGTH;
570
571 end += table->length;
572 while (p < end) {
573 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -0400574 if (h->type == amd_iommu_target_ivhd_type) {
575 int ret = find_last_devid_from_ivhd(h);
576
577 if (ret)
578 return ret;
Joerg Roedel3e8064b2008-06-26 21:27:41 +0200579 }
580 p += h->length;
581 }
582 WARN_ON(p != end);
583
584 return 0;
585}
586
Joerg Roedelb65233a2008-07-11 17:14:21 +0200587/****************************************************************************
588 *
Frank Arnolddf805ab2012-08-27 19:21:04 +0200589 * The following functions belong to the code path which parses the ACPI table
Joerg Roedelb65233a2008-07-11 17:14:21 +0200590 * the second time. In this ACPI parsing iteration we allocate IOMMU specific
591 * data structures, initialize the device/alias/rlookup table and also
592 * basically initialize the hardware.
593 *
594 ****************************************************************************/
595
596/*
597 * Allocates the command buffer. This buffer is per AMD IOMMU. We can
598 * write commands to that buffer later and the IOMMU will execute them
599 * asynchronously
600 */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200601static int __init alloc_command_buffer(struct amd_iommu *iommu)
Joerg Roedelb36ca912008-06-26 21:27:45 +0200602{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200603 iommu->cmd_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
604 get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200605
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200606 return iommu->cmd_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200607}
608
609/*
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200610 * This function resets the command buffer if the IOMMU stopped fetching
611 * commands from it.
612 */
613void amd_iommu_reset_cmd_buffer(struct amd_iommu *iommu)
614{
615 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
616
617 writel(0x00, iommu->mmio_base + MMIO_CMD_HEAD_OFFSET);
618 writel(0x00, iommu->mmio_base + MMIO_CMD_TAIL_OFFSET);
Tom Lendackyd334a562017-06-05 14:52:12 -0500619 iommu->cmd_buf_head = 0;
620 iommu->cmd_buf_tail = 0;
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200621
622 iommu_feature_enable(iommu, CONTROL_CMDBUF_EN);
623}
624
625/*
Joerg Roedel58492e12009-05-04 18:41:16 +0200626 * This function writes the command buffer address to the hardware and
627 * enables it.
628 */
629static void iommu_enable_command_buffer(struct amd_iommu *iommu)
630{
631 u64 entry;
632
633 BUG_ON(iommu->cmd_buf == NULL);
634
635 entry = (u64)virt_to_phys(iommu->cmd_buf);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200636 entry |= MMIO_CMD_SIZE_512;
Joerg Roedel58492e12009-05-04 18:41:16 +0200637
Joerg Roedelb36ca912008-06-26 21:27:45 +0200638 memcpy_toio(iommu->mmio_base + MMIO_CMD_BUF_OFFSET,
Joerg Roedel58492e12009-05-04 18:41:16 +0200639 &entry, sizeof(entry));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200640
Joerg Roedel93f1cc672009-09-03 14:50:20 +0200641 amd_iommu_reset_cmd_buffer(iommu);
Joerg Roedelb36ca912008-06-26 21:27:45 +0200642}
643
Baoquan He78d313c2017-08-09 16:33:34 +0800644/*
645 * This function disables the command buffer
646 */
647static void iommu_disable_command_buffer(struct amd_iommu *iommu)
648{
649 iommu_feature_disable(iommu, CONTROL_CMDBUF_EN);
650}
651
Joerg Roedelb36ca912008-06-26 21:27:45 +0200652static void __init free_command_buffer(struct amd_iommu *iommu)
653{
Joerg Roedeldeba4bc2015-10-20 17:33:41 +0200654 free_pages((unsigned long)iommu->cmd_buf, get_order(CMD_BUFFER_SIZE));
Joerg Roedelb36ca912008-06-26 21:27:45 +0200655}
656
Joerg Roedel335503e2008-09-05 14:29:07 +0200657/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200658static int __init alloc_event_buffer(struct amd_iommu *iommu)
Joerg Roedel335503e2008-09-05 14:29:07 +0200659{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200660 iommu->evt_buf = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
661 get_order(EVT_BUFFER_SIZE));
Joerg Roedel335503e2008-09-05 14:29:07 +0200662
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200663 return iommu->evt_buf ? 0 : -ENOMEM;
Joerg Roedel58492e12009-05-04 18:41:16 +0200664}
665
666static void iommu_enable_event_buffer(struct amd_iommu *iommu)
667{
668 u64 entry;
669
670 BUG_ON(iommu->evt_buf == NULL);
671
Joerg Roedel335503e2008-09-05 14:29:07 +0200672 entry = (u64)virt_to_phys(iommu->evt_buf) | EVT_LEN_MASK;
Joerg Roedel58492e12009-05-04 18:41:16 +0200673
Joerg Roedel335503e2008-09-05 14:29:07 +0200674 memcpy_toio(iommu->mmio_base + MMIO_EVT_BUF_OFFSET,
675 &entry, sizeof(entry));
676
Joerg Roedel090672072009-06-15 16:06:48 +0200677 /* set head and tail to zero manually */
678 writel(0x00, iommu->mmio_base + MMIO_EVT_HEAD_OFFSET);
679 writel(0x00, iommu->mmio_base + MMIO_EVT_TAIL_OFFSET);
680
Joerg Roedel58492e12009-05-04 18:41:16 +0200681 iommu_feature_enable(iommu, CONTROL_EVT_LOG_EN);
Joerg Roedel335503e2008-09-05 14:29:07 +0200682}
683
Baoquan He78d313c2017-08-09 16:33:34 +0800684/*
685 * This function disables the event log buffer
686 */
687static void iommu_disable_event_buffer(struct amd_iommu *iommu)
688{
689 iommu_feature_disable(iommu, CONTROL_EVT_LOG_EN);
690}
691
Joerg Roedel335503e2008-09-05 14:29:07 +0200692static void __init free_event_buffer(struct amd_iommu *iommu)
693{
694 free_pages((unsigned long)iommu->evt_buf, get_order(EVT_BUFFER_SIZE));
695}
696
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100697/* allocates the memory where the IOMMU will log its events to */
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200698static int __init alloc_ppr_log(struct amd_iommu *iommu)
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100699{
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200700 iommu->ppr_log = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
701 get_order(PPR_LOG_SIZE));
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100702
Joerg Roedelf2c2db52015-10-20 17:33:42 +0200703 return iommu->ppr_log ? 0 : -ENOMEM;
Joerg Roedel1a29ac02011-11-10 15:41:40 +0100704}
705
706static void iommu_enable_ppr_log(struct amd_iommu *iommu)
707{
708 u64 entry;
709
710 if (iommu->ppr_log == NULL)
711 return;
712
713 entry = (u64)virt_to_phys(iommu->ppr_log) | PPR_LOG_SIZE_512;
714
715 memcpy_toio(iommu->mmio_base + MMIO_PPR_LOG_OFFSET,
716 &entry, sizeof(entry));
717
718 /* set head and tail to zero manually */
719 writel(0x00, iommu->mmio_base + MMIO_PPR_HEAD_OFFSET);
720 writel(0x00, iommu->mmio_base + MMIO_PPR_TAIL_OFFSET);
721
722 iommu_feature_enable(iommu, CONTROL_PPFLOG_EN);
723 iommu_feature_enable(iommu, CONTROL_PPR_EN);
724}
725
726static void __init free_ppr_log(struct amd_iommu *iommu)
727{
728 if (iommu->ppr_log == NULL)
729 return;
730
731 free_pages((unsigned long)iommu->ppr_log, get_order(PPR_LOG_SIZE));
732}
733
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -0500734static void free_ga_log(struct amd_iommu *iommu)
735{
736#ifdef CONFIG_IRQ_REMAP
737 if (iommu->ga_log)
738 free_pages((unsigned long)iommu->ga_log,
739 get_order(GA_LOG_SIZE));
740 if (iommu->ga_log_tail)
741 free_pages((unsigned long)iommu->ga_log_tail,
742 get_order(8));
743#endif
744}
745
746static int iommu_ga_log_enable(struct amd_iommu *iommu)
747{
748#ifdef CONFIG_IRQ_REMAP
749 u32 status, i;
750
751 if (!iommu->ga_log)
752 return -EINVAL;
753
754 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
755
756 /* Check if already running */
757 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
758 return 0;
759
760 iommu_feature_enable(iommu, CONTROL_GAINT_EN);
761 iommu_feature_enable(iommu, CONTROL_GALOG_EN);
762
763 for (i = 0; i < LOOP_TIMEOUT; ++i) {
764 status = readl(iommu->mmio_base + MMIO_STATUS_OFFSET);
765 if (status & (MMIO_STATUS_GALOG_RUN_MASK))
766 break;
767 }
768
769 if (i >= LOOP_TIMEOUT)
770 return -EINVAL;
771#endif /* CONFIG_IRQ_REMAP */
772 return 0;
773}
774
775#ifdef CONFIG_IRQ_REMAP
776static int iommu_init_ga_log(struct amd_iommu *iommu)
777{
778 u64 entry;
779
780 if (!AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
781 return 0;
782
783 iommu->ga_log = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
784 get_order(GA_LOG_SIZE));
785 if (!iommu->ga_log)
786 goto err_out;
787
788 iommu->ga_log_tail = (u8 *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
789 get_order(8));
790 if (!iommu->ga_log_tail)
791 goto err_out;
792
793 entry = (u64)virt_to_phys(iommu->ga_log) | GA_LOG_SIZE_512;
794 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_BASE_OFFSET,
795 &entry, sizeof(entry));
796 entry = ((u64)virt_to_phys(iommu->ga_log) & 0xFFFFFFFFFFFFFULL) & ~7ULL;
797 memcpy_toio(iommu->mmio_base + MMIO_GA_LOG_TAIL_OFFSET,
798 &entry, sizeof(entry));
799 writel(0x00, iommu->mmio_base + MMIO_GA_HEAD_OFFSET);
800 writel(0x00, iommu->mmio_base + MMIO_GA_TAIL_OFFSET);
801
802 return 0;
803err_out:
804 free_ga_log(iommu);
805 return -EINVAL;
806}
807#endif /* CONFIG_IRQ_REMAP */
808
809static int iommu_init_ga(struct amd_iommu *iommu)
810{
811 int ret = 0;
812
813#ifdef CONFIG_IRQ_REMAP
814 /* Note: We have already checked GASup from IVRS table.
815 * Now, we need to make sure that GAMSup is set.
816 */
817 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir) &&
818 !iommu_feature(iommu, FEATURE_GAM_VAPIC))
819 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY_GA;
820
821 ret = iommu_init_ga_log(iommu);
822#endif /* CONFIG_IRQ_REMAP */
823
824 return ret;
825}
826
Joerg Roedelcbc33a92011-11-25 11:41:31 +0100827static void iommu_enable_gt(struct amd_iommu *iommu)
828{
829 if (!iommu_feature(iommu, FEATURE_GT))
830 return;
831
832 iommu_feature_enable(iommu, CONTROL_GT_EN);
833}
834
Joerg Roedelb65233a2008-07-11 17:14:21 +0200835/* sets a specific bit in the device table entry. */
Joerg Roedel3566b772008-06-26 21:27:46 +0200836static void set_dev_entry_bit(u16 devid, u8 bit)
837{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100838 int i = (bit >> 6) & 0x03;
839 int _bit = bit & 0x3f;
Joerg Roedel3566b772008-06-26 21:27:46 +0200840
Joerg Roedelee6c2862011-11-09 12:06:03 +0100841 amd_iommu_dev_table[devid].data[i] |= (1UL << _bit);
Joerg Roedel3566b772008-06-26 21:27:46 +0200842}
843
Joerg Roedelc5cca142009-10-09 18:31:20 +0200844static int get_dev_entry_bit(u16 devid, u8 bit)
845{
Joerg Roedelee6c2862011-11-09 12:06:03 +0100846 int i = (bit >> 6) & 0x03;
847 int _bit = bit & 0x3f;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200848
Joerg Roedelee6c2862011-11-09 12:06:03 +0100849 return (amd_iommu_dev_table[devid].data[i] & (1UL << _bit)) >> _bit;
Joerg Roedelc5cca142009-10-09 18:31:20 +0200850}
851
852
Baoquan He45a01c42017-08-09 16:33:37 +0800853static bool copy_device_table(void)
854{
Baoquan He53019a92017-08-09 16:33:39 +0800855 u64 int_ctl, int_tab_len, entry, last_entry = 0;
Baoquan He45a01c42017-08-09 16:33:37 +0800856 struct dev_table_entry *old_devtb = NULL;
857 u32 lo, hi, devid, old_devtb_size;
858 phys_addr_t old_devtb_phys;
Baoquan He45a01c42017-08-09 16:33:37 +0800859 struct amd_iommu *iommu;
Baoquan He53019a92017-08-09 16:33:39 +0800860 u16 dom_id, dte_v, irq_v;
Baoquan He45a01c42017-08-09 16:33:37 +0800861 gfp_t gfp_flag;
862
Baoquan He3ac3e5e2017-08-09 16:33:38 +0800863 if (!amd_iommu_pre_enabled)
864 return false;
Baoquan He45a01c42017-08-09 16:33:37 +0800865
866 pr_warn("Translation is already enabled - trying to copy translation structures\n");
867 for_each_iommu(iommu) {
868 /* All IOMMUs should use the same device table with the same size */
869 lo = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET);
870 hi = readl(iommu->mmio_base + MMIO_DEV_TABLE_OFFSET + 4);
871 entry = (((u64) hi) << 32) + lo;
872 if (last_entry && last_entry != entry) {
873 pr_err("IOMMU:%d should use the same dev table as others!/n",
874 iommu->index);
875 return false;
876 }
877 last_entry = entry;
878
879 old_devtb_size = ((entry & ~PAGE_MASK) + 1) << 12;
880 if (old_devtb_size != dev_table_size) {
881 pr_err("The device table size of IOMMU:%d is not expected!/n",
882 iommu->index);
883 return false;
884 }
885 }
886
887 old_devtb_phys = entry & PAGE_MASK;
Baoquan Heb3367812017-08-09 16:33:42 +0800888 if (old_devtb_phys >= 0x100000000ULL) {
889 pr_err("The address of old device table is above 4G, not trustworthy!/n");
890 return false;
891 }
Baoquan He45a01c42017-08-09 16:33:37 +0800892 old_devtb = memremap(old_devtb_phys, dev_table_size, MEMREMAP_WB);
893 if (!old_devtb)
894 return false;
895
Baoquan Heb3367812017-08-09 16:33:42 +0800896 gfp_flag = GFP_KERNEL | __GFP_ZERO | GFP_DMA32;
Baoquan He45a01c42017-08-09 16:33:37 +0800897 old_dev_tbl_cpy = (void *)__get_free_pages(gfp_flag,
898 get_order(dev_table_size));
899 if (old_dev_tbl_cpy == NULL) {
900 pr_err("Failed to allocate memory for copying old device table!/n");
901 return false;
902 }
903
904 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
905 old_dev_tbl_cpy[devid] = old_devtb[devid];
906 dom_id = old_devtb[devid].data[1] & DEV_DOMID_MASK;
907 dte_v = old_devtb[devid].data[0] & DTE_FLAG_V;
Baoquan He53019a92017-08-09 16:33:39 +0800908
909 if (dte_v && dom_id) {
910 old_dev_tbl_cpy[devid].data[0] = old_devtb[devid].data[0];
911 old_dev_tbl_cpy[devid].data[1] = old_devtb[devid].data[1];
Baoquan He45a01c42017-08-09 16:33:37 +0800912 __set_bit(dom_id, amd_iommu_pd_alloc_bitmap);
Baoquan He53019a92017-08-09 16:33:39 +0800913 }
914
915 irq_v = old_devtb[devid].data[2] & DTE_IRQ_REMAP_ENABLE;
916 int_ctl = old_devtb[devid].data[2] & DTE_IRQ_REMAP_INTCTL_MASK;
917 int_tab_len = old_devtb[devid].data[2] & DTE_IRQ_TABLE_LEN_MASK;
918 if (irq_v && (int_ctl || int_tab_len)) {
919 if ((int_ctl != DTE_IRQ_REMAP_INTCTL) ||
920 (int_tab_len != DTE_IRQ_TABLE_LEN)) {
921 pr_err("Wrong old irq remapping flag: %#x\n", devid);
922 return false;
923 }
924
925 old_dev_tbl_cpy[devid].data[2] = old_devtb[devid].data[2];
926 }
Baoquan He45a01c42017-08-09 16:33:37 +0800927 }
928 memunmap(old_devtb);
929
930 return true;
931}
932
Joerg Roedelc5cca142009-10-09 18:31:20 +0200933void amd_iommu_apply_erratum_63(u16 devid)
934{
935 int sysmgt;
936
937 sysmgt = get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1) |
938 (get_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2) << 1);
939
940 if (sysmgt == 0x01)
941 set_dev_entry_bit(devid, DEV_ENTRY_IW);
942}
943
Joerg Roedel5ff47892008-07-14 20:11:18 +0200944/* Writes the specific IOMMU for a device into the rlookup table */
945static void __init set_iommu_for_device(struct amd_iommu *iommu, u16 devid)
946{
947 amd_iommu_rlookup_table[devid] = iommu;
948}
949
Joerg Roedelb65233a2008-07-11 17:14:21 +0200950/*
951 * This function takes the device specific flags read from the ACPI
952 * table and sets up the device table entry with that information
953 */
Joerg Roedel5ff47892008-07-14 20:11:18 +0200954static void __init set_dev_entry_from_acpi(struct amd_iommu *iommu,
955 u16 devid, u32 flags, u32 ext_flags)
Joerg Roedel3566b772008-06-26 21:27:46 +0200956{
957 if (flags & ACPI_DEVFLAG_INITPASS)
958 set_dev_entry_bit(devid, DEV_ENTRY_INIT_PASS);
959 if (flags & ACPI_DEVFLAG_EXTINT)
960 set_dev_entry_bit(devid, DEV_ENTRY_EINT_PASS);
961 if (flags & ACPI_DEVFLAG_NMI)
962 set_dev_entry_bit(devid, DEV_ENTRY_NMI_PASS);
963 if (flags & ACPI_DEVFLAG_SYSMGT1)
964 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT1);
965 if (flags & ACPI_DEVFLAG_SYSMGT2)
966 set_dev_entry_bit(devid, DEV_ENTRY_SYSMGT2);
967 if (flags & ACPI_DEVFLAG_LINT0)
968 set_dev_entry_bit(devid, DEV_ENTRY_LINT0_PASS);
969 if (flags & ACPI_DEVFLAG_LINT1)
970 set_dev_entry_bit(devid, DEV_ENTRY_LINT1_PASS);
Joerg Roedel3566b772008-06-26 21:27:46 +0200971
Joerg Roedelc5cca142009-10-09 18:31:20 +0200972 amd_iommu_apply_erratum_63(devid);
973
Joerg Roedel5ff47892008-07-14 20:11:18 +0200974 set_iommu_for_device(iommu, devid);
Joerg Roedel3566b772008-06-26 21:27:46 +0200975}
976
Joerg Roedelc50e3242014-09-09 15:59:37 +0200977static int __init add_special_device(u8 type, u8 id, u16 *devid, bool cmd_line)
Joerg Roedel6efed632012-06-14 15:52:58 +0200978{
979 struct devid_map *entry;
980 struct list_head *list;
981
Joerg Roedel31cff672013-04-09 16:53:58 +0200982 if (type == IVHD_SPECIAL_IOAPIC)
983 list = &ioapic_map;
984 else if (type == IVHD_SPECIAL_HPET)
985 list = &hpet_map;
986 else
Joerg Roedel6efed632012-06-14 15:52:58 +0200987 return -EINVAL;
988
Joerg Roedel31cff672013-04-09 16:53:58 +0200989 list_for_each_entry(entry, list, list) {
990 if (!(entry->id == id && entry->cmd_line))
991 continue;
992
993 pr_info("AMD-Vi: Command-line override present for %s id %d - ignoring\n",
994 type == IVHD_SPECIAL_IOAPIC ? "IOAPIC" : "HPET", id);
995
Joerg Roedelc50e3242014-09-09 15:59:37 +0200996 *devid = entry->devid;
997
Joerg Roedel31cff672013-04-09 16:53:58 +0200998 return 0;
999 }
1000
Joerg Roedel6efed632012-06-14 15:52:58 +02001001 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1002 if (!entry)
1003 return -ENOMEM;
1004
Joerg Roedel31cff672013-04-09 16:53:58 +02001005 entry->id = id;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001006 entry->devid = *devid;
Joerg Roedel31cff672013-04-09 16:53:58 +02001007 entry->cmd_line = cmd_line;
Joerg Roedel6efed632012-06-14 15:52:58 +02001008
1009 list_add_tail(&entry->list, list);
1010
1011 return 0;
1012}
1013
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001014static int __init add_acpi_hid_device(u8 *hid, u8 *uid, u16 *devid,
1015 bool cmd_line)
1016{
1017 struct acpihid_map_entry *entry;
1018 struct list_head *list = &acpihid_map;
1019
1020 list_for_each_entry(entry, list, list) {
1021 if (strcmp(entry->hid, hid) ||
1022 (*uid && *entry->uid && strcmp(entry->uid, uid)) ||
1023 !entry->cmd_line)
1024 continue;
1025
1026 pr_info("AMD-Vi: Command-line override for hid:%s uid:%s\n",
1027 hid, uid);
1028 *devid = entry->devid;
1029 return 0;
1030 }
1031
1032 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1033 if (!entry)
1034 return -ENOMEM;
1035
1036 memcpy(entry->uid, uid, strlen(uid));
1037 memcpy(entry->hid, hid, strlen(hid));
1038 entry->devid = *devid;
1039 entry->cmd_line = cmd_line;
1040 entry->root_devid = (entry->devid & (~0x7));
1041
1042 pr_info("AMD-Vi:%s, add hid:%s, uid:%s, rdevid:%d\n",
1043 entry->cmd_line ? "cmd" : "ivrs",
1044 entry->hid, entry->uid, entry->root_devid);
1045
1046 list_add_tail(&entry->list, list);
1047 return 0;
1048}
1049
Joerg Roedel235dacb2013-04-09 17:53:14 +02001050static int __init add_early_maps(void)
1051{
1052 int i, ret;
1053
1054 for (i = 0; i < early_ioapic_map_size; ++i) {
1055 ret = add_special_device(IVHD_SPECIAL_IOAPIC,
1056 early_ioapic_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +02001057 &early_ioapic_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +02001058 early_ioapic_map[i].cmd_line);
1059 if (ret)
1060 return ret;
1061 }
1062
1063 for (i = 0; i < early_hpet_map_size; ++i) {
1064 ret = add_special_device(IVHD_SPECIAL_HPET,
1065 early_hpet_map[i].id,
Joerg Roedelc50e3242014-09-09 15:59:37 +02001066 &early_hpet_map[i].devid,
Joerg Roedel235dacb2013-04-09 17:53:14 +02001067 early_hpet_map[i].cmd_line);
1068 if (ret)
1069 return ret;
1070 }
1071
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001072 for (i = 0; i < early_acpihid_map_size; ++i) {
1073 ret = add_acpi_hid_device(early_acpihid_map[i].hid,
1074 early_acpihid_map[i].uid,
1075 &early_acpihid_map[i].devid,
1076 early_acpihid_map[i].cmd_line);
1077 if (ret)
1078 return ret;
1079 }
1080
Joerg Roedel235dacb2013-04-09 17:53:14 +02001081 return 0;
1082}
1083
Joerg Roedelb65233a2008-07-11 17:14:21 +02001084/*
Frank Arnolddf805ab2012-08-27 19:21:04 +02001085 * Reads the device exclusion range from ACPI and initializes the IOMMU with
Joerg Roedelb65233a2008-07-11 17:14:21 +02001086 * it
1087 */
Joerg Roedel3566b772008-06-26 21:27:46 +02001088static void __init set_device_exclusion_range(u16 devid, struct ivmd_header *m)
1089{
1090 struct amd_iommu *iommu = amd_iommu_rlookup_table[devid];
1091
1092 if (!(m->flags & IVMD_FLAG_EXCL_RANGE))
1093 return;
1094
1095 if (iommu) {
Joerg Roedelb65233a2008-07-11 17:14:21 +02001096 /*
1097 * We only can configure exclusion ranges per IOMMU, not
1098 * per device. But we can enable the exclusion range per
1099 * device. This is done here
1100 */
Su Friendy2c16c9f2014-05-07 13:54:52 +08001101 set_dev_entry_bit(devid, DEV_ENTRY_EX);
Joerg Roedel3566b772008-06-26 21:27:46 +02001102 iommu->exclusion_start = m->range_start;
1103 iommu->exclusion_length = m->range_length;
1104 }
1105}
1106
Joerg Roedelb65233a2008-07-11 17:14:21 +02001107/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001108 * Takes a pointer to an AMD IOMMU entry in the ACPI table and
1109 * initializes the hardware and our data structures with it.
1110 */
Joerg Roedel6efed632012-06-14 15:52:58 +02001111static int __init init_iommu_from_acpi(struct amd_iommu *iommu,
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001112 struct ivhd_header *h)
1113{
1114 u8 *p = (u8 *)h;
1115 u8 *end = p, flags = 0;
Joerg Roedel0de66d52011-06-06 16:04:02 +02001116 u16 devid = 0, devid_start = 0, devid_to = 0;
1117 u32 dev_i, ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001118 bool alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001119 struct ivhd_entry *e;
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001120 u32 ivhd_size;
Joerg Roedel235dacb2013-04-09 17:53:14 +02001121 int ret;
1122
1123
1124 ret = add_early_maps();
1125 if (ret)
1126 return ret;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001127
1128 /*
Joerg Roedele9bf5192010-09-20 14:33:07 +02001129 * First save the recommended feature enable bits from ACPI
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001130 */
Joerg Roedele9bf5192010-09-20 14:33:07 +02001131 iommu->acpi_flags = h->flags;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001132
1133 /*
1134 * Done. Now parse the device entries
1135 */
Suravee Suthikulpanitac7ccf62016-04-01 09:05:58 -04001136 ivhd_size = get_ivhd_header_size(h);
1137 if (!ivhd_size) {
1138 pr_err("AMD-Vi: Unsupported IVHD type %#x\n", h->type);
1139 return -EINVAL;
1140 }
1141
1142 p += ivhd_size;
1143
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001144 end += h->length;
1145
Joerg Roedel42a698f2009-05-20 15:41:28 +02001146
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001147 while (p < end) {
1148 e = (struct ivhd_entry *)p;
1149 switch (e->type) {
1150 case IVHD_DEV_ALL:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001151
Joerg Roedel226e8892015-10-20 17:33:44 +02001152 DUMP_printk(" DEV_ALL\t\t\tflags: %02x\n", e->flags);
Joerg Roedel42a698f2009-05-20 15:41:28 +02001153
Joerg Roedel226e8892015-10-20 17:33:44 +02001154 for (dev_i = 0; dev_i <= amd_iommu_last_bdf; ++dev_i)
1155 set_dev_entry_from_acpi(iommu, dev_i, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001156 break;
1157 case IVHD_DEV_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001158
1159 DUMP_printk(" DEV_SELECT\t\t\t devid: %02x:%02x.%x "
1160 "flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001161 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001162 PCI_SLOT(e->devid),
1163 PCI_FUNC(e->devid),
1164 e->flags);
1165
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001166 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001167 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001168 break;
1169 case IVHD_DEV_SELECT_RANGE_START:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001170
1171 DUMP_printk(" DEV_SELECT_RANGE_START\t "
1172 "devid: %02x:%02x.%x flags: %02x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001173 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001174 PCI_SLOT(e->devid),
1175 PCI_FUNC(e->devid),
1176 e->flags);
1177
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001178 devid_start = e->devid;
1179 flags = e->flags;
1180 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001181 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001182 break;
1183 case IVHD_DEV_ALIAS:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001184
1185 DUMP_printk(" DEV_ALIAS\t\t\t devid: %02x:%02x.%x "
1186 "flags: %02x devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001187 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001188 PCI_SLOT(e->devid),
1189 PCI_FUNC(e->devid),
1190 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001191 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001192 PCI_SLOT(e->ext >> 8),
1193 PCI_FUNC(e->ext >> 8));
1194
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001195 devid = e->devid;
1196 devid_to = e->ext >> 8;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001197 set_dev_entry_from_acpi(iommu, devid , e->flags, 0);
Neil Turton7455aab2009-05-14 14:08:11 +01001198 set_dev_entry_from_acpi(iommu, devid_to, e->flags, 0);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001199 amd_iommu_alias_table[devid] = devid_to;
1200 break;
1201 case IVHD_DEV_ALIAS_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001202
1203 DUMP_printk(" DEV_ALIAS_RANGE\t\t "
1204 "devid: %02x:%02x.%x flags: %02x "
1205 "devid_to: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001206 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001207 PCI_SLOT(e->devid),
1208 PCI_FUNC(e->devid),
1209 e->flags,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001210 PCI_BUS_NUM(e->ext >> 8),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001211 PCI_SLOT(e->ext >> 8),
1212 PCI_FUNC(e->ext >> 8));
1213
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001214 devid_start = e->devid;
1215 flags = e->flags;
1216 devid_to = e->ext >> 8;
1217 ext_flags = 0;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001218 alias = true;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001219 break;
1220 case IVHD_DEV_EXT_SELECT:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001221
1222 DUMP_printk(" DEV_EXT_SELECT\t\t devid: %02x:%02x.%x "
1223 "flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001224 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001225 PCI_SLOT(e->devid),
1226 PCI_FUNC(e->devid),
1227 e->flags, e->ext);
1228
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001229 devid = e->devid;
Joerg Roedel5ff47892008-07-14 20:11:18 +02001230 set_dev_entry_from_acpi(iommu, devid, e->flags,
1231 e->ext);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001232 break;
1233 case IVHD_DEV_EXT_SELECT_RANGE:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001234
1235 DUMP_printk(" DEV_EXT_SELECT_RANGE\t devid: "
1236 "%02x:%02x.%x flags: %02x ext: %08x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001237 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001238 PCI_SLOT(e->devid),
1239 PCI_FUNC(e->devid),
1240 e->flags, e->ext);
1241
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001242 devid_start = e->devid;
1243 flags = e->flags;
1244 ext_flags = e->ext;
Joerg Roedel58a3bee2008-07-11 17:14:30 +02001245 alias = false;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001246 break;
1247 case IVHD_DEV_RANGE_END:
Joerg Roedel42a698f2009-05-20 15:41:28 +02001248
1249 DUMP_printk(" DEV_RANGE_END\t\t devid: %02x:%02x.%x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001250 PCI_BUS_NUM(e->devid),
Joerg Roedel42a698f2009-05-20 15:41:28 +02001251 PCI_SLOT(e->devid),
1252 PCI_FUNC(e->devid));
1253
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001254 devid = e->devid;
1255 for (dev_i = devid_start; dev_i <= devid; ++dev_i) {
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001256 if (alias) {
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001257 amd_iommu_alias_table[dev_i] = devid_to;
Joerg Roedel7a6a3a02009-07-02 12:23:23 +02001258 set_dev_entry_from_acpi(iommu,
1259 devid_to, flags, ext_flags);
1260 }
1261 set_dev_entry_from_acpi(iommu, dev_i,
1262 flags, ext_flags);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001263 }
1264 break;
Joerg Roedel6efed632012-06-14 15:52:58 +02001265 case IVHD_DEV_SPECIAL: {
1266 u8 handle, type;
1267 const char *var;
1268 u16 devid;
1269 int ret;
1270
1271 handle = e->ext & 0xff;
1272 devid = (e->ext >> 8) & 0xffff;
1273 type = (e->ext >> 24) & 0xff;
1274
1275 if (type == IVHD_SPECIAL_IOAPIC)
1276 var = "IOAPIC";
1277 else if (type == IVHD_SPECIAL_HPET)
1278 var = "HPET";
1279 else
1280 var = "UNKNOWN";
1281
1282 DUMP_printk(" DEV_SPECIAL(%s[%d])\t\tdevid: %02x:%02x.%x\n",
1283 var, (int)handle,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001284 PCI_BUS_NUM(devid),
Joerg Roedel6efed632012-06-14 15:52:58 +02001285 PCI_SLOT(devid),
1286 PCI_FUNC(devid));
1287
Joerg Roedelc50e3242014-09-09 15:59:37 +02001288 ret = add_special_device(type, handle, &devid, false);
Joerg Roedel6efed632012-06-14 15:52:58 +02001289 if (ret)
1290 return ret;
Joerg Roedelc50e3242014-09-09 15:59:37 +02001291
1292 /*
1293 * add_special_device might update the devid in case a
1294 * command-line override is present. So call
1295 * set_dev_entry_from_acpi after add_special_device.
1296 */
1297 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1298
Joerg Roedel6efed632012-06-14 15:52:58 +02001299 break;
1300 }
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001301 case IVHD_DEV_ACPI_HID: {
1302 u16 devid;
1303 u8 hid[ACPIHID_HID_LEN] = {0};
1304 u8 uid[ACPIHID_UID_LEN] = {0};
1305 int ret;
1306
1307 if (h->type != 0x40) {
1308 pr_err(FW_BUG "Invalid IVHD device type %#x\n",
1309 e->type);
1310 break;
1311 }
1312
1313 memcpy(hid, (u8 *)(&e->ext), ACPIHID_HID_LEN - 1);
1314 hid[ACPIHID_HID_LEN - 1] = '\0';
1315
1316 if (!(*hid)) {
1317 pr_err(FW_BUG "Invalid HID.\n");
1318 break;
1319 }
1320
1321 switch (e->uidf) {
1322 case UID_NOT_PRESENT:
1323
1324 if (e->uidl != 0)
1325 pr_warn(FW_BUG "Invalid UID length.\n");
1326
1327 break;
1328 case UID_IS_INTEGER:
1329
1330 sprintf(uid, "%d", e->uid);
1331
1332 break;
1333 case UID_IS_CHARACTER:
1334
1335 memcpy(uid, (u8 *)(&e->uid), ACPIHID_UID_LEN - 1);
1336 uid[ACPIHID_UID_LEN - 1] = '\0';
1337
1338 break;
1339 default:
1340 break;
1341 }
1342
Nicolas Iooss6082ee72016-06-26 10:33:29 +02001343 devid = e->devid;
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001344 DUMP_printk(" DEV_ACPI_HID(%s[%s])\t\tdevid: %02x:%02x.%x\n",
1345 hid, uid,
1346 PCI_BUS_NUM(devid),
1347 PCI_SLOT(devid),
1348 PCI_FUNC(devid));
1349
Wan Zongshun2a0cb4e2016-04-01 09:06:00 -04001350 flags = e->flags;
1351
1352 ret = add_acpi_hid_device(hid, uid, &devid, false);
1353 if (ret)
1354 return ret;
1355
1356 /*
1357 * add_special_device might update the devid in case a
1358 * command-line override is present. So call
1359 * set_dev_entry_from_acpi after add_special_device.
1360 */
1361 set_dev_entry_from_acpi(iommu, devid, e->flags, 0);
1362
1363 break;
1364 }
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001365 default:
1366 break;
1367 }
1368
Joerg Roedelb514e552008-09-17 17:14:27 +02001369 p += ivhd_entry_length(p);
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001370 }
Joerg Roedel6efed632012-06-14 15:52:58 +02001371
1372 return 0;
Joerg Roedel5d0c8e42008-06-26 21:27:47 +02001373}
1374
Joerg Roedele47d4022008-06-26 21:27:48 +02001375static void __init free_iommu_one(struct amd_iommu *iommu)
1376{
1377 free_command_buffer(iommu);
Joerg Roedel335503e2008-09-05 14:29:07 +02001378 free_event_buffer(iommu);
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001379 free_ppr_log(iommu);
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001380 free_ga_log(iommu);
Joerg Roedele47d4022008-06-26 21:27:48 +02001381 iommu_unmap_mmio_space(iommu);
1382}
1383
1384static void __init free_iommu_all(void)
1385{
1386 struct amd_iommu *iommu, *next;
1387
Joerg Roedel3bd22172009-05-04 15:06:20 +02001388 for_each_iommu_safe(iommu, next) {
Joerg Roedele47d4022008-06-26 21:27:48 +02001389 list_del(&iommu->list);
1390 free_iommu_one(iommu);
1391 kfree(iommu);
1392 }
1393}
1394
Joerg Roedelb65233a2008-07-11 17:14:21 +02001395/*
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001396 * Family15h Model 10h-1fh erratum 746 (IOMMU Logging May Stall Translations)
1397 * Workaround:
1398 * BIOS should disable L2B micellaneous clock gating by setting
1399 * L2_L2B_CK_GATE_CONTROL[CKGateL2BMiscDisable](D0F2xF4_x90[2]) = 1b
1400 */
Nikola Pajkovskye2f1a3b2013-02-26 16:12:05 +01001401static void amd_iommu_erratum_746_workaround(struct amd_iommu *iommu)
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001402{
1403 u32 value;
1404
1405 if ((boot_cpu_data.x86 != 0x15) ||
1406 (boot_cpu_data.x86_model < 0x10) ||
1407 (boot_cpu_data.x86_model > 0x1f))
1408 return;
1409
1410 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1411 pci_read_config_dword(iommu->dev, 0xf4, &value);
1412
1413 if (value & BIT(2))
1414 return;
1415
1416 /* Select NB indirect register 0x90 and enable writing */
1417 pci_write_config_dword(iommu->dev, 0xf0, 0x90 | (1 << 8));
1418
1419 pci_write_config_dword(iommu->dev, 0xf4, value | 0x4);
1420 pr_info("AMD-Vi: Applying erratum 746 workaround for IOMMU at %s\n",
1421 dev_name(&iommu->dev->dev));
1422
1423 /* Clear the enable writing bit */
1424 pci_write_config_dword(iommu->dev, 0xf0, 0x90);
1425}
1426
1427/*
Jay Cornwall358875f2016-02-10 15:48:01 -06001428 * Family15h Model 30h-3fh (IOMMU Mishandles ATS Write Permission)
1429 * Workaround:
1430 * BIOS should enable ATS write permission check by setting
1431 * L2_DEBUG_3[AtsIgnoreIWDis](D0F2xF4_x47[0]) = 1b
1432 */
1433static void amd_iommu_ats_write_check_workaround(struct amd_iommu *iommu)
1434{
1435 u32 value;
1436
1437 if ((boot_cpu_data.x86 != 0x15) ||
1438 (boot_cpu_data.x86_model < 0x30) ||
1439 (boot_cpu_data.x86_model > 0x3f))
1440 return;
1441
1442 /* Test L2_DEBUG_3[AtsIgnoreIWDis] == 1 */
1443 value = iommu_read_l2(iommu, 0x47);
1444
1445 if (value & BIT(0))
1446 return;
1447
1448 /* Set L2_DEBUG_3[AtsIgnoreIWDis] = 1 */
1449 iommu_write_l2(iommu, 0x47, value | BIT(0));
1450
1451 pr_info("AMD-Vi: Applying ATS write check workaround for IOMMU at %s\n",
1452 dev_name(&iommu->dev->dev));
1453}
1454
1455/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02001456 * This function clues the initialization function for one IOMMU
1457 * together and also allocates the command buffer and programs the
1458 * hardware. It does NOT enable the IOMMU. This is done afterwards.
1459 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001460static int __init init_iommu_one(struct amd_iommu *iommu, struct ivhd_header *h)
1461{
Joerg Roedel6efed632012-06-14 15:52:58 +02001462 int ret;
1463
Joerg Roedele47d4022008-06-26 21:27:48 +02001464 spin_lock_init(&iommu->lock);
Joerg Roedelbb527772009-11-20 14:31:51 +01001465
1466 /* Add IOMMU to internal data structures */
Joerg Roedele47d4022008-06-26 21:27:48 +02001467 list_add_tail(&iommu->list, &amd_iommu_list);
Suravee Suthikulpanit6b9376e2017-02-24 02:48:17 -06001468 iommu->index = amd_iommus_present++;
Joerg Roedelbb527772009-11-20 14:31:51 +01001469
1470 if (unlikely(iommu->index >= MAX_IOMMUS)) {
1471 WARN(1, "AMD-Vi: System has more IOMMUs than supported by this driver\n");
1472 return -ENOSYS;
1473 }
1474
1475 /* Index is fine - add IOMMU to the array */
1476 amd_iommus[iommu->index] = iommu;
Joerg Roedele47d4022008-06-26 21:27:48 +02001477
1478 /*
1479 * Copy data from ACPI table entry to the iommu struct
1480 */
Joerg Roedel23c742d2012-06-12 11:47:34 +02001481 iommu->devid = h->devid;
Joerg Roedele47d4022008-06-26 21:27:48 +02001482 iommu->cap_ptr = h->cap_ptr;
Joerg Roedelee893c22008-09-08 14:48:04 +02001483 iommu->pci_seg = h->pci_seg;
Joerg Roedele47d4022008-06-26 21:27:48 +02001484 iommu->mmio_phys = h->mmio_phys;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001485
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001486 switch (h->type) {
1487 case 0x10:
1488 /* Check if IVHD EFR contains proper max banks/counters */
1489 if ((h->efr_attr != 0) &&
1490 ((h->efr_attr & (0xF << 13)) != 0) &&
1491 ((h->efr_attr & (0x3F << 17)) != 0))
1492 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1493 else
1494 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001495 if (((h->efr_attr & (0x1 << IOMMU_FEAT_GASUP_SHIFT)) == 0))
1496 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001497 break;
1498 case 0x11:
1499 case 0x40:
1500 if (h->efr_reg & (1 << 9))
1501 iommu->mmio_phys_end = MMIO_REG_END_OFFSET;
1502 else
1503 iommu->mmio_phys_end = MMIO_CNTR_CONF_OFFSET;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001504 if (((h->efr_reg & (0x1 << IOMMU_EFR_GASUP_SHIFT)) == 0))
1505 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
Suravee Suthikulpanit7d7d38a2016-04-01 09:05:57 -04001506 break;
1507 default:
1508 return -EINVAL;
Steven L Kinney30861dd2013-06-05 16:11:48 -05001509 }
1510
1511 iommu->mmio_base = iommu_map_mmio_space(iommu->mmio_phys,
1512 iommu->mmio_phys_end);
Joerg Roedele47d4022008-06-26 21:27:48 +02001513 if (!iommu->mmio_base)
1514 return -ENOMEM;
1515
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001516 if (alloc_command_buffer(iommu))
Joerg Roedele47d4022008-06-26 21:27:48 +02001517 return -ENOMEM;
1518
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001519 if (alloc_event_buffer(iommu))
Joerg Roedel335503e2008-09-05 14:29:07 +02001520 return -ENOMEM;
1521
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001522 iommu->int_enabled = false;
1523
Baoquan He4c232a72017-08-09 16:33:33 +08001524 init_translation_status(iommu);
Baoquan He3ac3e5e2017-08-09 16:33:38 +08001525 if (translation_pre_enabled(iommu) && !is_kdump_kernel()) {
1526 iommu_disable(iommu);
1527 clear_translation_pre_enabled(iommu);
1528 pr_warn("Translation was enabled for IOMMU:%d but we are not in kdump mode\n",
1529 iommu->index);
1530 }
1531 if (amd_iommu_pre_enabled)
1532 amd_iommu_pre_enabled = translation_pre_enabled(iommu);
Baoquan He4c232a72017-08-09 16:33:33 +08001533
Joerg Roedel6efed632012-06-14 15:52:58 +02001534 ret = init_iommu_from_acpi(iommu, h);
1535 if (ret)
1536 return ret;
Joerg Roedelf6fec002012-06-21 16:51:25 +02001537
Jiang Liu7c71d302015-04-13 14:11:33 +08001538 ret = amd_iommu_create_irq_domain(iommu);
1539 if (ret)
1540 return ret;
1541
Joerg Roedelf6fec002012-06-21 16:51:25 +02001542 /*
1543 * Make sure IOMMU is not considered to translate itself. The IVRS
1544 * table tells us so, but this is a lie!
1545 */
1546 amd_iommu_rlookup_table[iommu->devid] = NULL;
1547
Joerg Roedel23c742d2012-06-12 11:47:34 +02001548 return 0;
Joerg Roedele47d4022008-06-26 21:27:48 +02001549}
1550
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001551/**
1552 * get_highest_supported_ivhd_type - Look up the appropriate IVHD type
1553 * @ivrs Pointer to the IVRS header
1554 *
1555 * This function search through all IVDB of the maximum supported IVHD
1556 */
1557static u8 get_highest_supported_ivhd_type(struct acpi_table_header *ivrs)
1558{
1559 u8 *base = (u8 *)ivrs;
1560 struct ivhd_header *ivhd = (struct ivhd_header *)
1561 (base + IVRS_HEADER_LENGTH);
1562 u8 last_type = ivhd->type;
1563 u16 devid = ivhd->devid;
1564
1565 while (((u8 *)ivhd - base < ivrs->length) &&
1566 (ivhd->type <= ACPI_IVHD_TYPE_MAX_SUPPORTED)) {
1567 u8 *p = (u8 *) ivhd;
1568
1569 if (ivhd->devid == devid)
1570 last_type = ivhd->type;
1571 ivhd = (struct ivhd_header *)(p + ivhd->length);
1572 }
1573
1574 return last_type;
1575}
1576
Joerg Roedelb65233a2008-07-11 17:14:21 +02001577/*
1578 * Iterates over all IOMMU entries in the ACPI table, allocates the
1579 * IOMMU structure and initializes it with init_iommu_one()
1580 */
Joerg Roedele47d4022008-06-26 21:27:48 +02001581static int __init init_iommu_all(struct acpi_table_header *table)
1582{
1583 u8 *p = (u8 *)table, *end = (u8 *)table;
1584 struct ivhd_header *h;
1585 struct amd_iommu *iommu;
1586 int ret;
1587
Joerg Roedele47d4022008-06-26 21:27:48 +02001588 end += table->length;
1589 p += IVRS_HEADER_LENGTH;
1590
1591 while (p < end) {
1592 h = (struct ivhd_header *)p;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04001593 if (*p == amd_iommu_target_ivhd_type) {
Joerg Roedel9c720412009-05-20 13:53:57 +02001594
Joerg Roedelae908c22009-09-01 16:52:16 +02001595 DUMP_printk("device: %02x:%02x.%01x cap: %04x "
Joerg Roedel9c720412009-05-20 13:53:57 +02001596 "seg: %d flags: %01x info %04x\n",
Shuah Khanc5081cd2013-02-27 17:07:19 -07001597 PCI_BUS_NUM(h->devid), PCI_SLOT(h->devid),
Joerg Roedel9c720412009-05-20 13:53:57 +02001598 PCI_FUNC(h->devid), h->cap_ptr,
1599 h->pci_seg, h->flags, h->info);
1600 DUMP_printk(" mmio-addr: %016llx\n",
1601 h->mmio_phys);
1602
Joerg Roedele47d4022008-06-26 21:27:48 +02001603 iommu = kzalloc(sizeof(struct amd_iommu), GFP_KERNEL);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001604 if (iommu == NULL)
1605 return -ENOMEM;
Joerg Roedel3551a702010-03-01 13:52:19 +01001606
Joerg Roedele47d4022008-06-26 21:27:48 +02001607 ret = init_iommu_one(iommu, h);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02001608 if (ret)
1609 return ret;
Joerg Roedele47d4022008-06-26 21:27:48 +02001610 }
1611 p += h->length;
1612
1613 }
1614 WARN_ON(p != end);
1615
1616 return 0;
1617}
1618
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001619static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
1620 u8 fxn, u64 *value, bool is_write);
Steven L Kinney30861dd2013-06-05 16:11:48 -05001621
1622static void init_iommu_perf_ctr(struct amd_iommu *iommu)
1623{
1624 u64 val = 0xabcd, val2 = 0;
1625
1626 if (!iommu_feature(iommu, FEATURE_PC))
1627 return;
1628
1629 amd_iommu_pc_present = true;
1630
1631 /* Check if the performance counters can be written to */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06001632 if ((iommu_pc_get_set_reg(iommu, 0, 0, 0, &val, true)) ||
1633 (iommu_pc_get_set_reg(iommu, 0, 0, 0, &val2, false)) ||
Steven L Kinney30861dd2013-06-05 16:11:48 -05001634 (val != val2)) {
1635 pr_err("AMD-Vi: Unable to write to IOMMU perf counter.\n");
1636 amd_iommu_pc_present = false;
1637 return;
1638 }
1639
1640 pr_info("AMD-Vi: IOMMU performance counters supported\n");
1641
1642 val = readl(iommu->mmio_base + MMIO_CNTR_CONF_OFFSET);
1643 iommu->max_banks = (u8) ((val >> 12) & 0x3f);
1644 iommu->max_counters = (u8) ((val >> 7) & 0xf);
1645}
1646
Alex Williamson066f2e92014-06-12 16:12:37 -06001647static ssize_t amd_iommu_show_cap(struct device *dev,
1648 struct device_attribute *attr,
1649 char *buf)
1650{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001651 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001652 return sprintf(buf, "%x\n", iommu->cap);
1653}
1654static DEVICE_ATTR(cap, S_IRUGO, amd_iommu_show_cap, NULL);
1655
1656static ssize_t amd_iommu_show_features(struct device *dev,
1657 struct device_attribute *attr,
1658 char *buf)
1659{
Joerg Roedelb7a42b92017-02-28 13:57:18 +01001660 struct amd_iommu *iommu = dev_to_amd_iommu(dev);
Alex Williamson066f2e92014-06-12 16:12:37 -06001661 return sprintf(buf, "%llx\n", iommu->features);
1662}
1663static DEVICE_ATTR(features, S_IRUGO, amd_iommu_show_features, NULL);
1664
1665static struct attribute *amd_iommu_attrs[] = {
1666 &dev_attr_cap.attr,
1667 &dev_attr_features.attr,
1668 NULL,
1669};
1670
1671static struct attribute_group amd_iommu_group = {
1672 .name = "amd-iommu",
1673 .attrs = amd_iommu_attrs,
1674};
1675
1676static const struct attribute_group *amd_iommu_groups[] = {
1677 &amd_iommu_group,
1678 NULL,
1679};
Steven L Kinney30861dd2013-06-05 16:11:48 -05001680
Joerg Roedel23c742d2012-06-12 11:47:34 +02001681static int iommu_init_pci(struct amd_iommu *iommu)
1682{
1683 int cap_ptr = iommu->cap_ptr;
1684 u32 range, misc, low, high;
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001685 int ret;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001686
Shuah Khanc5081cd2013-02-27 17:07:19 -07001687 iommu->dev = pci_get_bus_and_slot(PCI_BUS_NUM(iommu->devid),
Joerg Roedel23c742d2012-06-12 11:47:34 +02001688 iommu->devid & 0xff);
1689 if (!iommu->dev)
1690 return -ENODEV;
1691
Jiang Liucbbc00b2015-10-09 22:07:31 +08001692 /* Prevent binding other PCI device drivers to IOMMU devices */
1693 iommu->dev->match_driver = false;
1694
Joerg Roedel23c742d2012-06-12 11:47:34 +02001695 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_CAP_HDR_OFFSET,
1696 &iommu->cap);
1697 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_RANGE_OFFSET,
1698 &range);
1699 pci_read_config_dword(iommu->dev, cap_ptr + MMIO_MISC_OFFSET,
1700 &misc);
1701
Joerg Roedel23c742d2012-06-12 11:47:34 +02001702 if (!(iommu->cap & (1 << IOMMU_CAP_IOTLB)))
1703 amd_iommu_iotlb_sup = false;
1704
1705 /* read extended feature bits */
1706 low = readl(iommu->mmio_base + MMIO_EXT_FEATURES);
1707 high = readl(iommu->mmio_base + MMIO_EXT_FEATURES + 4);
1708
1709 iommu->features = ((u64)high << 32) | low;
1710
1711 if (iommu_feature(iommu, FEATURE_GT)) {
1712 int glxval;
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001713 u32 max_pasid;
1714 u64 pasmax;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001715
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001716 pasmax = iommu->features & FEATURE_PASID_MASK;
1717 pasmax >>= FEATURE_PASID_SHIFT;
1718 max_pasid = (1 << (pasmax + 1)) - 1;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001719
Suravee Suthikulpanita919a012014-03-05 18:54:18 -06001720 amd_iommu_max_pasid = min(amd_iommu_max_pasid, max_pasid);
1721
1722 BUG_ON(amd_iommu_max_pasid & ~PASID_MASK);
Joerg Roedel23c742d2012-06-12 11:47:34 +02001723
1724 glxval = iommu->features & FEATURE_GLXVAL_MASK;
1725 glxval >>= FEATURE_GLXVAL_SHIFT;
1726
1727 if (amd_iommu_max_glx_val == -1)
1728 amd_iommu_max_glx_val = glxval;
1729 else
1730 amd_iommu_max_glx_val = min(amd_iommu_max_glx_val, glxval);
1731 }
1732
1733 if (iommu_feature(iommu, FEATURE_GT) &&
1734 iommu_feature(iommu, FEATURE_PPR)) {
1735 iommu->is_iommu_v2 = true;
1736 amd_iommu_v2_present = true;
1737 }
1738
Joerg Roedelf2c2db52015-10-20 17:33:42 +02001739 if (iommu_feature(iommu, FEATURE_PPR) && alloc_ppr_log(iommu))
1740 return -ENOMEM;
Joerg Roedel23c742d2012-06-12 11:47:34 +02001741
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001742 ret = iommu_init_ga(iommu);
1743 if (ret)
1744 return ret;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001745
Joerg Roedel23c742d2012-06-12 11:47:34 +02001746 if (iommu->cap & (1UL << IOMMU_CAP_NPCACHE))
1747 amd_iommu_np_cache = true;
1748
Steven L Kinney30861dd2013-06-05 16:11:48 -05001749 init_iommu_perf_ctr(iommu);
1750
Joerg Roedel23c742d2012-06-12 11:47:34 +02001751 if (is_rd890_iommu(iommu->dev)) {
1752 int i, j;
1753
1754 iommu->root_pdev = pci_get_bus_and_slot(iommu->dev->bus->number,
1755 PCI_DEVFN(0, 0));
1756
1757 /*
1758 * Some rd890 systems may not be fully reconfigured by the
1759 * BIOS, so it's necessary for us to store this information so
1760 * it can be reprogrammed on resume
1761 */
1762 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 4,
1763 &iommu->stored_addr_lo);
1764 pci_read_config_dword(iommu->dev, iommu->cap_ptr + 8,
1765 &iommu->stored_addr_hi);
1766
1767 /* Low bit locks writes to configuration space */
1768 iommu->stored_addr_lo &= ~1;
1769
1770 for (i = 0; i < 6; i++)
1771 for (j = 0; j < 0x12; j++)
1772 iommu->stored_l1[i][j] = iommu_read_l1(iommu, i, j);
1773
1774 for (i = 0; i < 0x83; i++)
1775 iommu->stored_l2[i] = iommu_read_l2(iommu, i);
1776 }
1777
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001778 amd_iommu_erratum_746_workaround(iommu);
Jay Cornwall358875f2016-02-10 15:48:01 -06001779 amd_iommu_ats_write_check_workaround(iommu);
Suravee Suthikulpanit318fe782013-01-24 13:17:53 -06001780
Joerg Roedel39ab9552017-02-01 16:56:46 +01001781 iommu_device_sysfs_add(&iommu->iommu, &iommu->dev->dev,
1782 amd_iommu_groups, "ivhd%d", iommu->index);
Joerg Roedelb0119e82017-02-01 13:23:08 +01001783 iommu_device_set_ops(&iommu->iommu, &amd_iommu_ops);
1784 iommu_device_register(&iommu->iommu);
Alex Williamson066f2e92014-06-12 16:12:37 -06001785
Joerg Roedel23c742d2012-06-12 11:47:34 +02001786 return pci_enable_device(iommu->dev);
1787}
1788
Joerg Roedel4d121c32012-06-14 12:21:55 +02001789static void print_iommu_info(void)
1790{
1791 static const char * const feat_str[] = {
1792 "PreF", "PPR", "X2APIC", "NX", "GT", "[5]",
1793 "IA", "GA", "HE", "PC"
1794 };
1795 struct amd_iommu *iommu;
1796
1797 for_each_iommu(iommu) {
1798 int i;
1799
1800 pr_info("AMD-Vi: Found IOMMU at %s cap 0x%hx\n",
1801 dev_name(&iommu->dev->dev), iommu->cap_ptr);
1802
1803 if (iommu->cap & (1 << IOMMU_CAP_EFR)) {
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001804 pr_info("AMD-Vi: Extended features (%#llx):\n",
1805 iommu->features);
Joerg Roedel2bd5ed02012-08-10 11:34:08 +02001806 for (i = 0; i < ARRAY_SIZE(feat_str); ++i) {
Joerg Roedel4d121c32012-06-14 12:21:55 +02001807 if (iommu_feature(iommu, (1ULL << i)))
1808 pr_cont(" %s", feat_str[i]);
1809 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001810
1811 if (iommu->features & FEATURE_GAM_VAPIC)
1812 pr_cont(" GA_vAPIC");
1813
Steven L Kinney30861dd2013-06-05 16:11:48 -05001814 pr_cont("\n");
Borislav Petkov500c25e2012-09-28 16:22:26 +02001815 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001816 }
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001817 if (irq_remapping_enabled) {
Joerg Roedelebe60bb2012-07-02 18:36:03 +02001818 pr_info("AMD-Vi: Interrupt remapping enabled\n");
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05001819 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
1820 pr_info("AMD-Vi: virtual APIC enabled\n");
1821 }
Joerg Roedel4d121c32012-06-14 12:21:55 +02001822}
1823
Joerg Roedel2c0ae172012-06-12 15:59:30 +02001824static int __init amd_iommu_init_pci(void)
Joerg Roedel23c742d2012-06-12 11:47:34 +02001825{
1826 struct amd_iommu *iommu;
1827 int ret = 0;
1828
1829 for_each_iommu(iommu) {
1830 ret = iommu_init_pci(iommu);
1831 if (ret)
1832 break;
1833 }
1834
Joerg Roedel522e5cb72016-07-01 16:42:55 +02001835 /*
1836 * Order is important here to make sure any unity map requirements are
1837 * fulfilled. The unity mappings are created and written to the device
1838 * table during the amd_iommu_init_api() call.
1839 *
1840 * After that we call init_device_table_dma() to make sure any
1841 * uninitialized DTE will block DMA, and in the end we flush the caches
1842 * of all IOMMUs to make sure the changes to the device table are
1843 * active.
1844 */
1845 ret = amd_iommu_init_api();
1846
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001847 init_device_table_dma();
Joerg Roedel23c742d2012-06-12 11:47:34 +02001848
Joerg Roedelaafd8ba2015-05-28 18:41:39 +02001849 for_each_iommu(iommu)
1850 iommu_flush_all_caches(iommu);
1851
Joerg Roedel3a18404c2015-05-28 18:41:45 +02001852 if (!ret)
1853 print_iommu_info();
Joerg Roedel4d121c32012-06-14 12:21:55 +02001854
Joerg Roedel23c742d2012-06-12 11:47:34 +02001855 return ret;
1856}
1857
Joerg Roedelb65233a2008-07-11 17:14:21 +02001858/****************************************************************************
1859 *
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001860 * The following functions initialize the MSI interrupts for all IOMMUs
Frank Arnolddf805ab2012-08-27 19:21:04 +02001861 * in the system. It's a bit challenging because there could be multiple
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001862 * IOMMUs per PCI BDF but we can call pci_enable_msi(x) only once per
1863 * pci_dev.
1864 *
1865 ****************************************************************************/
1866
Joerg Roedel9f800de2009-11-23 12:45:25 +01001867static int iommu_setup_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001868{
1869 int r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001870
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001871 r = pci_enable_msi(iommu->dev);
1872 if (r)
1873 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001874
Joerg Roedel72fe00f2011-05-10 10:50:42 +02001875 r = request_threaded_irq(iommu->dev->irq,
1876 amd_iommu_int_handler,
1877 amd_iommu_int_thread,
1878 0, "AMD-Vi",
Suravee Suthikulpanit3f398bc2013-04-22 16:32:34 -05001879 iommu);
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001880
1881 if (r) {
1882 pci_disable_msi(iommu->dev);
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001883 return r;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001884 }
1885
Joerg Roedelfab6afa2009-05-04 18:46:34 +02001886 iommu->int_enabled = true;
Joerg Roedel1a29ac02011-11-10 15:41:40 +01001887
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001888 return 0;
1889}
1890
Joerg Roedel05f92db2009-05-12 09:52:46 +02001891static int iommu_init_msi(struct amd_iommu *iommu)
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001892{
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001893 int ret;
1894
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001895 if (iommu->int_enabled)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001896 goto enable_faults;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001897
Yijing Wang82fcfc62013-08-08 21:12:36 +08001898 if (iommu->dev->msi_cap)
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001899 ret = iommu_setup_msi(iommu);
1900 else
1901 ret = -ENODEV;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001902
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001903 if (ret)
1904 return ret;
1905
1906enable_faults:
1907 iommu_feature_enable(iommu, CONTROL_EVT_INT_EN);
1908
1909 if (iommu->ppr_log != NULL)
1910 iommu_feature_enable(iommu, CONTROL_PPFINT_EN);
1911
Suravee Suthikulpanit8bda0cf2016-08-23 13:52:36 -05001912 iommu_ga_log_enable(iommu);
1913
Joerg Roedel9ddd5922012-03-15 16:29:47 +01001914 return 0;
Joerg Roedela80dc3e2008-09-11 16:51:41 +02001915}
1916
1917/****************************************************************************
1918 *
Joerg Roedelb65233a2008-07-11 17:14:21 +02001919 * The next functions belong to the third pass of parsing the ACPI
1920 * table. In this last pass the memory mapping requirements are
Frank Arnolddf805ab2012-08-27 19:21:04 +02001921 * gathered (like exclusion and unity mapping ranges).
Joerg Roedelb65233a2008-07-11 17:14:21 +02001922 *
1923 ****************************************************************************/
1924
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001925static void __init free_unity_maps(void)
1926{
1927 struct unity_map_entry *entry, *next;
1928
1929 list_for_each_entry_safe(entry, next, &amd_iommu_unity_map, list) {
1930 list_del(&entry->list);
1931 kfree(entry);
1932 }
1933}
1934
Joerg Roedelb65233a2008-07-11 17:14:21 +02001935/* called when we find an exclusion range definition in ACPI */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001936static int __init init_exclusion_range(struct ivmd_header *m)
1937{
1938 int i;
1939
1940 switch (m->type) {
1941 case ACPI_IVMD_TYPE:
1942 set_device_exclusion_range(m->devid, m);
1943 break;
1944 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel3a61ec32008-07-25 13:07:50 +02001945 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001946 set_device_exclusion_range(i, m);
1947 break;
1948 case ACPI_IVMD_TYPE_RANGE:
1949 for (i = m->devid; i <= m->aux; ++i)
1950 set_device_exclusion_range(i, m);
1951 break;
1952 default:
1953 break;
1954 }
1955
1956 return 0;
1957}
1958
Joerg Roedelb65233a2008-07-11 17:14:21 +02001959/* called for unity map ACPI definition */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001960static int __init init_unity_map_range(struct ivmd_header *m)
1961{
Joerg Roedel98f1ad22012-07-06 13:28:37 +02001962 struct unity_map_entry *e = NULL;
Joerg Roedel02acc432009-05-20 16:24:21 +02001963 char *s;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001964
1965 e = kzalloc(sizeof(*e), GFP_KERNEL);
1966 if (e == NULL)
1967 return -ENOMEM;
1968
1969 switch (m->type) {
1970 default:
Joerg Roedel0bc252f2009-05-22 12:48:05 +02001971 kfree(e);
1972 return 0;
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001973 case ACPI_IVMD_TYPE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001974 s = "IVMD_TYPEi\t\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001975 e->devid_start = e->devid_end = m->devid;
1976 break;
1977 case ACPI_IVMD_TYPE_ALL:
Joerg Roedel02acc432009-05-20 16:24:21 +02001978 s = "IVMD_TYPE_ALL\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001979 e->devid_start = 0;
1980 e->devid_end = amd_iommu_last_bdf;
1981 break;
1982 case ACPI_IVMD_TYPE_RANGE:
Joerg Roedel02acc432009-05-20 16:24:21 +02001983 s = "IVMD_TYPE_RANGE\t\t";
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001984 e->devid_start = m->devid;
1985 e->devid_end = m->aux;
1986 break;
1987 }
1988 e->address_start = PAGE_ALIGN(m->range_start);
1989 e->address_end = e->address_start + PAGE_ALIGN(m->range_length);
1990 e->prot = m->flags >> 1;
1991
Joerg Roedel02acc432009-05-20 16:24:21 +02001992 DUMP_printk("%s devid_start: %02x:%02x.%x devid_end: %02x:%02x.%x"
1993 " range_start: %016llx range_end: %016llx flags: %x\n", s,
Shuah Khanc5081cd2013-02-27 17:07:19 -07001994 PCI_BUS_NUM(e->devid_start), PCI_SLOT(e->devid_start),
1995 PCI_FUNC(e->devid_start), PCI_BUS_NUM(e->devid_end),
Joerg Roedel02acc432009-05-20 16:24:21 +02001996 PCI_SLOT(e->devid_end), PCI_FUNC(e->devid_end),
1997 e->address_start, e->address_end, m->flags);
1998
Joerg Roedelbe2a0222008-06-26 21:27:49 +02001999 list_add_tail(&e->list, &amd_iommu_unity_map);
2000
2001 return 0;
2002}
2003
Joerg Roedelb65233a2008-07-11 17:14:21 +02002004/* iterates over all memory definitions we find in the ACPI table */
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002005static int __init init_memory_definitions(struct acpi_table_header *table)
2006{
2007 u8 *p = (u8 *)table, *end = (u8 *)table;
2008 struct ivmd_header *m;
2009
Joerg Roedelbe2a0222008-06-26 21:27:49 +02002010 end += table->length;
2011 p += IVRS_HEADER_LENGTH;
2012
2013 while (p < end) {
2014 m = (struct ivmd_header *)p;
2015 if (m->flags & IVMD_FLAG_EXCL_RANGE)
2016 init_exclusion_range(m);
2017 else if (m->flags & IVMD_FLAG_UNITY_MAP)
2018 init_unity_map_range(m);
2019
2020 p += m->length;
2021 }
2022
2023 return 0;
2024}
2025
Joerg Roedelb65233a2008-07-11 17:14:21 +02002026/*
Baoquan He3ac3e5e2017-08-09 16:33:38 +08002027 * Init the device table to not allow DMA access for devices
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002028 */
Joerg Roedel33f28c52012-06-15 18:03:31 +02002029static void init_device_table_dma(void)
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002030{
Joerg Roedel0de66d52011-06-06 16:04:02 +02002031 u32 devid;
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002032
2033 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2034 set_dev_entry_bit(devid, DEV_ENTRY_VALID);
2035 set_dev_entry_bit(devid, DEV_ENTRY_TRANSLATION);
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002036 }
2037}
2038
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002039static void __init uninit_device_table_dma(void)
2040{
2041 u32 devid;
2042
2043 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid) {
2044 amd_iommu_dev_table[devid].data[0] = 0ULL;
2045 amd_iommu_dev_table[devid].data[1] = 0ULL;
2046 }
2047}
2048
Joerg Roedel33f28c52012-06-15 18:03:31 +02002049static void init_device_table(void)
2050{
2051 u32 devid;
2052
2053 if (!amd_iommu_irq_remap)
2054 return;
2055
2056 for (devid = 0; devid <= amd_iommu_last_bdf; ++devid)
2057 set_dev_entry_bit(devid, DEV_ENTRY_IRQ_TBL_EN);
2058}
2059
Joerg Roedele9bf5192010-09-20 14:33:07 +02002060static void iommu_init_flags(struct amd_iommu *iommu)
2061{
2062 iommu->acpi_flags & IVHD_FLAG_HT_TUN_EN_MASK ?
2063 iommu_feature_enable(iommu, CONTROL_HT_TUN_EN) :
2064 iommu_feature_disable(iommu, CONTROL_HT_TUN_EN);
2065
2066 iommu->acpi_flags & IVHD_FLAG_PASSPW_EN_MASK ?
2067 iommu_feature_enable(iommu, CONTROL_PASSPW_EN) :
2068 iommu_feature_disable(iommu, CONTROL_PASSPW_EN);
2069
2070 iommu->acpi_flags & IVHD_FLAG_RESPASSPW_EN_MASK ?
2071 iommu_feature_enable(iommu, CONTROL_RESPASSPW_EN) :
2072 iommu_feature_disable(iommu, CONTROL_RESPASSPW_EN);
2073
2074 iommu->acpi_flags & IVHD_FLAG_ISOC_EN_MASK ?
2075 iommu_feature_enable(iommu, CONTROL_ISOC_EN) :
2076 iommu_feature_disable(iommu, CONTROL_ISOC_EN);
2077
2078 /*
2079 * make IOMMU memory accesses cache coherent
2080 */
2081 iommu_feature_enable(iommu, CONTROL_COHERENT_EN);
Joerg Roedel1456e9d2011-12-22 14:51:53 +01002082
2083 /* Set IOTLB invalidation timeout to 1s */
2084 iommu_set_inv_tlb_timeout(iommu, CTRL_INV_TO_1S);
Joerg Roedele9bf5192010-09-20 14:33:07 +02002085}
2086
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002087static void iommu_apply_resume_quirks(struct amd_iommu *iommu)
Joerg Roedel4c894f42010-09-23 15:15:19 +02002088{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002089 int i, j;
2090 u32 ioc_feature_control;
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002091 struct pci_dev *pdev = iommu->root_pdev;
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002092
2093 /* RD890 BIOSes may not have completely reconfigured the iommu */
Joerg Roedelc1bf94e2012-05-31 17:38:11 +02002094 if (!is_rd890_iommu(iommu->dev) || !pdev)
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002095 return;
2096
2097 /*
2098 * First, we need to ensure that the iommu is enabled. This is
2099 * controlled by a register in the northbridge
2100 */
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002101
2102 /* Select Northbridge indirect register 0x75 and enable writing */
2103 pci_write_config_dword(pdev, 0x60, 0x75 | (1 << 7));
2104 pci_read_config_dword(pdev, 0x64, &ioc_feature_control);
2105
2106 /* Enable the iommu */
2107 if (!(ioc_feature_control & 0x1))
2108 pci_write_config_dword(pdev, 0x64, ioc_feature_control | 1);
2109
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002110 /* Restore the iommu BAR */
2111 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2112 iommu->stored_addr_lo);
2113 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 8,
2114 iommu->stored_addr_hi);
2115
2116 /* Restore the l1 indirect regs for each of the 6 l1s */
2117 for (i = 0; i < 6; i++)
2118 for (j = 0; j < 0x12; j++)
2119 iommu_write_l1(iommu, i, j, iommu->stored_l1[i][j]);
2120
2121 /* Restore the l2 indirect regs */
2122 for (i = 0; i < 0x83; i++)
2123 iommu_write_l2(iommu, i, iommu->stored_l2[i]);
2124
2125 /* Lock PCI setup registers */
2126 pci_write_config_dword(iommu->dev, iommu->cap_ptr + 4,
2127 iommu->stored_addr_lo | 1);
Joerg Roedel4c894f42010-09-23 15:15:19 +02002128}
2129
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002130static void iommu_enable_ga(struct amd_iommu *iommu)
2131{
2132#ifdef CONFIG_IRQ_REMAP
2133 switch (amd_iommu_guest_ir) {
2134 case AMD_IOMMU_GUEST_IR_VAPIC:
2135 iommu_feature_enable(iommu, CONTROL_GAM_EN);
2136 /* Fall through */
2137 case AMD_IOMMU_GUEST_IR_LEGACY_GA:
2138 iommu_feature_enable(iommu, CONTROL_GA_EN);
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002139 iommu->irte_ops = &irte_128_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002140 break;
2141 default:
Suravee Suthikulpanit77bdab42016-08-23 13:52:35 -05002142 iommu->irte_ops = &irte_32_ops;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002143 break;
2144 }
2145#endif
2146}
2147
Baoquan He78d313c2017-08-09 16:33:34 +08002148static void early_enable_iommu(struct amd_iommu *iommu)
2149{
2150 iommu_disable(iommu);
2151 iommu_init_flags(iommu);
2152 iommu_set_device_table(iommu);
2153 iommu_enable_command_buffer(iommu);
2154 iommu_enable_event_buffer(iommu);
2155 iommu_set_exclusion_range(iommu);
2156 iommu_enable_ga(iommu);
2157 iommu_enable(iommu);
2158 iommu_flush_all_caches(iommu);
2159}
2160
Joerg Roedel9f5f5fb2008-08-14 19:55:16 +02002161/*
Joerg Roedelb65233a2008-07-11 17:14:21 +02002162 * This function finally enables all IOMMUs found in the system after
Baoquan He3ac3e5e2017-08-09 16:33:38 +08002163 * they have been initialized.
2164 *
2165 * Or if in kdump kernel and IOMMUs are all pre-enabled, try to copy
2166 * the old content of device table entries. Not this case or copy failed,
2167 * just continue as normal kernel does.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002168 */
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002169static void early_enable_iommus(void)
Joerg Roedel87361972008-06-26 21:28:07 +02002170{
2171 struct amd_iommu *iommu;
2172
Baoquan He3ac3e5e2017-08-09 16:33:38 +08002173
2174 if (!copy_device_table()) {
2175 /*
2176 * If come here because of failure in copying device table from old
2177 * kernel with all IOMMUs enabled, print error message and try to
2178 * free allocated old_dev_tbl_cpy.
2179 */
2180 if (amd_iommu_pre_enabled)
2181 pr_err("Failed to copy DEV table from previous kernel.\n");
2182 if (old_dev_tbl_cpy != NULL)
2183 free_pages((unsigned long)old_dev_tbl_cpy,
2184 get_order(dev_table_size));
2185
2186 for_each_iommu(iommu) {
2187 clear_translation_pre_enabled(iommu);
2188 early_enable_iommu(iommu);
2189 }
2190 } else {
2191 pr_info("Copied DEV table from previous kernel.\n");
2192 free_pages((unsigned long)amd_iommu_dev_table,
2193 get_order(dev_table_size));
2194 amd_iommu_dev_table = old_dev_tbl_cpy;
2195 for_each_iommu(iommu) {
2196 iommu_disable_command_buffer(iommu);
2197 iommu_disable_event_buffer(iommu);
2198 iommu_enable_command_buffer(iommu);
2199 iommu_enable_event_buffer(iommu);
2200 iommu_enable_ga(iommu);
2201 iommu_set_device_table(iommu);
2202 iommu_flush_all_caches(iommu);
2203 }
2204 }
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002205
2206#ifdef CONFIG_IRQ_REMAP
2207 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2208 amd_iommu_irq_ops.capability |= (1 << IRQ_POSTING_CAP);
2209#endif
Joerg Roedel87361972008-06-26 21:28:07 +02002210}
2211
Joerg Roedel11ee5ac2012-06-12 16:30:06 +02002212static void enable_iommus_v2(void)
2213{
2214 struct amd_iommu *iommu;
2215
2216 for_each_iommu(iommu) {
2217 iommu_enable_ppr_log(iommu);
2218 iommu_enable_gt(iommu);
2219 }
2220}
2221
2222static void enable_iommus(void)
2223{
2224 early_enable_iommus();
2225
2226 enable_iommus_v2();
2227}
2228
Joerg Roedel92ac4322009-05-19 19:06:27 +02002229static void disable_iommus(void)
2230{
2231 struct amd_iommu *iommu;
2232
2233 for_each_iommu(iommu)
2234 iommu_disable(iommu);
Suravee Suthikulpanitd98de492016-08-23 13:52:40 -05002235
2236#ifdef CONFIG_IRQ_REMAP
2237 if (AMD_IOMMU_GUEST_IR_VAPIC(amd_iommu_guest_ir))
2238 amd_iommu_irq_ops.capability &= ~(1 << IRQ_POSTING_CAP);
2239#endif
Joerg Roedel92ac4322009-05-19 19:06:27 +02002240}
2241
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002242/*
2243 * Suspend/Resume support
2244 * disable suspend until real resume implemented
2245 */
2246
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002247static void amd_iommu_resume(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002248{
Matthew Garrett5bcd7572010-10-04 14:59:31 -04002249 struct amd_iommu *iommu;
2250
2251 for_each_iommu(iommu)
2252 iommu_apply_resume_quirks(iommu);
2253
Joerg Roedel736501e2009-05-12 09:56:12 +02002254 /* re-load the hardware */
2255 enable_iommus();
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002256
2257 amd_iommu_enable_interrupts();
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002258}
2259
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002260static int amd_iommu_suspend(void)
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002261{
Joerg Roedel736501e2009-05-12 09:56:12 +02002262 /* disable IOMMUs to go out of the way for BIOS */
2263 disable_iommus();
2264
2265 return 0;
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002266}
2267
Rafael J. Wysockif3c6ea12011-03-23 22:15:54 +01002268static struct syscore_ops amd_iommu_syscore_ops = {
Joerg Roedel7441e9c2008-06-30 20:18:02 +02002269 .suspend = amd_iommu_suspend,
2270 .resume = amd_iommu_resume,
2271};
2272
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002273static void __init free_iommu_resources(void)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002274{
Lucas Stachebcfa282016-10-26 13:09:53 +02002275 kmemleak_free(irq_lookup_table);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002276 free_pages((unsigned long)irq_lookup_table,
2277 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002278 irq_lookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002279
Julia Lawalla5919892015-09-13 14:15:31 +02002280 kmem_cache_destroy(amd_iommu_irq_cache);
2281 amd_iommu_irq_cache = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002282
2283 free_pages((unsigned long)amd_iommu_rlookup_table,
2284 get_order(rlookup_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002285 amd_iommu_rlookup_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002286
2287 free_pages((unsigned long)amd_iommu_alias_table,
2288 get_order(alias_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002289 amd_iommu_alias_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002290
2291 free_pages((unsigned long)amd_iommu_dev_table,
2292 get_order(dev_table_size));
Joerg Roedelf6019272017-06-16 16:09:58 +02002293 amd_iommu_dev_table = NULL;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002294
2295 free_iommu_all();
2296
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002297#ifdef CONFIG_GART_IOMMU
2298 /*
2299 * We failed to initialize the AMD IOMMU - try fallback to GART
2300 * if possible.
2301 */
2302 gart_iommu_init();
2303
2304#endif
2305}
2306
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002307/* SB IOAPIC is always on this device in AMD systems */
2308#define IOAPIC_SB_DEVID ((0x00 << 8) | PCI_DEVFN(0x14, 0))
2309
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002310static bool __init check_ioapic_information(void)
2311{
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002312 const char *fw_bug = FW_BUG;
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002313 bool ret, has_sb_ioapic;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002314 int idx;
2315
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002316 has_sb_ioapic = false;
2317 ret = false;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002318
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002319 /*
2320 * If we have map overrides on the kernel command line the
2321 * messages in this function might not describe firmware bugs
2322 * anymore - so be careful
2323 */
2324 if (cmdline_maps)
2325 fw_bug = "";
2326
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002327 for (idx = 0; idx < nr_ioapics; idx++) {
2328 int devid, id = mpc_ioapic_id(idx);
2329
2330 devid = get_ioapic_devid(id);
2331 if (devid < 0) {
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002332 pr_err("%sAMD-Vi: IOAPIC[%d] not in IVRS table\n",
2333 fw_bug, id);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002334 ret = false;
2335 } else if (devid == IOAPIC_SB_DEVID) {
2336 has_sb_ioapic = true;
2337 ret = true;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002338 }
2339 }
2340
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002341 if (!has_sb_ioapic) {
2342 /*
2343 * We expect the SB IOAPIC to be listed in the IVRS
2344 * table. The system timer is connected to the SB IOAPIC
2345 * and if we don't have it in the list the system will
2346 * panic at boot time. This situation usually happens
2347 * when the BIOS is buggy and provides us the wrong
2348 * device id for the IOAPIC in the system.
2349 */
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002350 pr_err("%sAMD-Vi: No southbridge IOAPIC found\n", fw_bug);
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002351 }
2352
2353 if (!ret)
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002354 pr_err("AMD-Vi: Disabling interrupt remapping\n");
Joerg Roedelc2ff5cf52012-10-16 14:52:51 +02002355
2356 return ret;
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002357}
2358
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002359static void __init free_dma_resources(void)
2360{
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002361 free_pages((unsigned long)amd_iommu_pd_alloc_bitmap,
2362 get_order(MAX_DOMAIN_ID/8));
Joerg Roedelf6019272017-06-16 16:09:58 +02002363 amd_iommu_pd_alloc_bitmap = NULL;
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002364
2365 free_unity_maps();
2366}
2367
Joerg Roedelb65233a2008-07-11 17:14:21 +02002368/*
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002369 * This is the hardware init function for AMD IOMMU in the system.
2370 * This function is called either from amd_iommu_init or from the interrupt
2371 * remapping setup code.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002372 *
2373 * This function basically parses the ACPI table for AMD IOMMU (IVRS)
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002374 * four times:
Joerg Roedelb65233a2008-07-11 17:14:21 +02002375 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002376 * 1 pass) Discover the most comprehensive IVHD type to use.
2377 *
2378 * 2 pass) Find the highest PCI device id the driver has to handle.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002379 * Upon this information the size of the data structures is
2380 * determined that needs to be allocated.
2381 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002382 * 3 pass) Initialize the data structures just allocated with the
Joerg Roedelb65233a2008-07-11 17:14:21 +02002383 * information in the ACPI table about available AMD IOMMUs
2384 * in the system. It also maps the PCI devices in the
2385 * system to specific IOMMUs
2386 *
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002387 * 4 pass) After the basic data structures are allocated and
Joerg Roedelb65233a2008-07-11 17:14:21 +02002388 * initialized we update them with information about memory
2389 * remapping requirements parsed out of the ACPI table in
2390 * this last pass.
2391 *
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002392 * After everything is set up the IOMMUs are enabled and the necessary
2393 * hotplug and suspend notifiers are registered.
Joerg Roedelb65233a2008-07-11 17:14:21 +02002394 */
Joerg Roedel643511b2012-06-12 12:09:35 +02002395static int __init early_amd_iommu_init(void)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002396{
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002397 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002398 acpi_status status;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002399 int i, remap_cache_sz, ret = 0;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002400
Joerg Roedel643511b2012-06-12 12:09:35 +02002401 if (!amd_iommu_detected)
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002402 return -ENODEV;
2403
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002404 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002405 if (status == AE_NOT_FOUND)
2406 return -ENODEV;
2407 else if (ACPI_FAILURE(status)) {
2408 const char *err = acpi_format_exception(status);
2409 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2410 return -EINVAL;
2411 }
2412
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002413 /*
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002414 * Validate checksum here so we don't need to do it when
2415 * we actually parse the table
2416 */
2417 ret = check_ivrs_checksum(ivrs_base);
2418 if (ret)
Rafael J. Wysocki99e8ccd2017-01-10 14:57:28 +01002419 goto out;
Suravee Suthikulpanit8c7142f2016-04-01 09:05:59 -04002420
2421 amd_iommu_target_ivhd_type = get_highest_supported_ivhd_type(ivrs_base);
2422 DUMP_printk("Using IVHD type %#x\n", amd_iommu_target_ivhd_type);
2423
2424 /*
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002425 * First parse ACPI tables to find the largest Bus/Dev/Func
2426 * we need to handle. Upon this information the shared data
2427 * structures for the IOMMUs in the system will be allocated
2428 */
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002429 ret = find_last_devid_acpi(ivrs_base);
2430 if (ret)
Joerg Roedel3551a702010-03-01 13:52:19 +01002431 goto out;
2432
Joerg Roedelc5714842008-07-11 17:14:25 +02002433 dev_table_size = tbl_size(DEV_TABLE_ENTRY_SIZE);
2434 alias_table_size = tbl_size(ALIAS_TABLE_ENTRY_SIZE);
2435 rlookup_table_size = tbl_size(RLOOKUP_TABLE_ENTRY_SIZE);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002436
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002437 /* Device table - directly used by all IOMMUs */
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002438 ret = -ENOMEM;
Baoquan Heb3367812017-08-09 16:33:42 +08002439 amd_iommu_dev_table = (void *)__get_free_pages(
2440 GFP_KERNEL | __GFP_ZERO | GFP_DMA32,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002441 get_order(dev_table_size));
2442 if (amd_iommu_dev_table == NULL)
2443 goto out;
2444
2445 /*
2446 * Alias table - map PCI Bus/Dev/Func to Bus/Dev/Func the
2447 * IOMMU see for that device
2448 */
2449 amd_iommu_alias_table = (void *)__get_free_pages(GFP_KERNEL,
2450 get_order(alias_table_size));
2451 if (amd_iommu_alias_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002452 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002453
2454 /* IOMMU rlookup table - find the IOMMU for a specific device */
Joerg Roedel83fd5cc2008-12-16 19:17:11 +01002455 amd_iommu_rlookup_table = (void *)__get_free_pages(
2456 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002457 get_order(rlookup_table_size));
2458 if (amd_iommu_rlookup_table == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002459 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002460
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002461 amd_iommu_pd_alloc_bitmap = (void *)__get_free_pages(
2462 GFP_KERNEL | __GFP_ZERO,
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002463 get_order(MAX_DOMAIN_ID/8));
2464 if (amd_iommu_pd_alloc_bitmap == NULL)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002465 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002466
2467 /*
Joerg Roedel5dc8bff2008-07-11 17:14:32 +02002468 * let all alias entries point to itself
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002469 */
Joerg Roedel3a61ec32008-07-25 13:07:50 +02002470 for (i = 0; i <= amd_iommu_last_bdf; ++i)
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002471 amd_iommu_alias_table[i] = i;
2472
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002473 /*
2474 * never allocate domain 0 because its used as the non-allocated and
2475 * error value placeholder
2476 */
Baoquan He5c87f622016-09-15 16:50:51 +08002477 __set_bit(0, amd_iommu_pd_alloc_bitmap);
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002478
Joerg Roedelaeb26f52009-11-20 16:44:01 +01002479 spin_lock_init(&amd_iommu_pd_lock);
2480
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002481 /*
2482 * now the data structures are allocated and basically initialized
2483 * start the real acpi table scan
2484 */
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002485 ret = init_iommu_all(ivrs_base);
2486 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002487 goto out;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002488
Joerg Roedel11123742017-06-16 16:09:54 +02002489 /* Disable any previously enabled IOMMUs */
2490 disable_iommus();
2491
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002492 if (amd_iommu_irq_remap)
2493 amd_iommu_irq_remap = check_ioapic_information();
2494
Joerg Roedel05152a02012-06-15 16:53:51 +02002495 if (amd_iommu_irq_remap) {
2496 /*
2497 * Interrupt remapping enabled, create kmem_cache for the
2498 * remapping tables.
2499 */
Wei Yongjun83ed9c12013-04-23 10:47:44 +08002500 ret = -ENOMEM;
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002501 if (!AMD_IOMMU_GUEST_IR_GA(amd_iommu_guest_ir))
2502 remap_cache_sz = MAX_IRQS_PER_TABLE * sizeof(u32);
2503 else
2504 remap_cache_sz = MAX_IRQS_PER_TABLE * (sizeof(u64) * 2);
Joerg Roedel05152a02012-06-15 16:53:51 +02002505 amd_iommu_irq_cache = kmem_cache_create("irq_remap_cache",
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002506 remap_cache_sz,
2507 IRQ_TABLE_ALIGNMENT,
2508 0, NULL);
Joerg Roedel05152a02012-06-15 16:53:51 +02002509 if (!amd_iommu_irq_cache)
2510 goto out;
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002511
2512 irq_lookup_table = (void *)__get_free_pages(
2513 GFP_KERNEL | __GFP_ZERO,
2514 get_order(rlookup_table_size));
Lucas Stachebcfa282016-10-26 13:09:53 +02002515 kmemleak_alloc(irq_lookup_table, rlookup_table_size,
2516 1, GFP_KERNEL);
Joerg Roedel0ea2c422012-06-15 18:05:20 +02002517 if (!irq_lookup_table)
2518 goto out;
Joerg Roedel05152a02012-06-15 16:53:51 +02002519 }
2520
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002521 ret = init_memory_definitions(ivrs_base);
2522 if (ret)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002523 goto out;
Joerg Roedel3551a702010-03-01 13:52:19 +01002524
Joerg Roedeleb1eb7a2012-07-05 11:58:02 +02002525 /* init the device table */
2526 init_device_table();
2527
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002528out:
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002529 /* Don't leak any ACPI memory */
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002530 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002531 ivrs_base = NULL;
2532
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002533 return ret;
Joerg Roedel643511b2012-06-12 12:09:35 +02002534}
2535
Gerard Snitselaarae295142012-03-16 11:38:22 -07002536static int amd_iommu_enable_interrupts(void)
Joerg Roedel3d9761e2012-03-15 16:39:21 +01002537{
2538 struct amd_iommu *iommu;
2539 int ret = 0;
2540
2541 for_each_iommu(iommu) {
2542 ret = iommu_init_msi(iommu);
2543 if (ret)
2544 goto out;
2545 }
2546
2547out:
2548 return ret;
2549}
2550
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002551static bool detect_ivrs(void)
2552{
2553 struct acpi_table_header *ivrs_base;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002554 acpi_status status;
2555
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002556 status = acpi_get_table("IVRS", 0, &ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002557 if (status == AE_NOT_FOUND)
2558 return false;
2559 else if (ACPI_FAILURE(status)) {
2560 const char *err = acpi_format_exception(status);
2561 pr_err("AMD-Vi: IVRS table error: %s\n", err);
2562 return false;
2563 }
2564
Lv Zheng6b11d1d2016-12-14 15:04:39 +08002565 acpi_put_table(ivrs_base);
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002566
Joerg Roedel1adb7d32012-08-06 14:18:42 +02002567 /* Make sure ACS will be enabled during PCI probe */
2568 pci_request_acs();
2569
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002570 return true;
2571}
2572
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002573/****************************************************************************
2574 *
2575 * AMD IOMMU Initialization State Machine
2576 *
2577 ****************************************************************************/
2578
2579static int __init state_next(void)
2580{
2581 int ret = 0;
2582
2583 switch (init_state) {
2584 case IOMMU_START_STATE:
2585 if (!detect_ivrs()) {
2586 init_state = IOMMU_NOT_FOUND;
2587 ret = -ENODEV;
2588 } else {
2589 init_state = IOMMU_IVRS_DETECTED;
2590 }
2591 break;
2592 case IOMMU_IVRS_DETECTED:
2593 ret = early_amd_iommu_init();
2594 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_ACPI_FINISHED;
Joerg Roedel7ad820e2017-06-16 16:09:59 +02002595 if (init_state == IOMMU_ACPI_FINISHED && amd_iommu_disabled) {
2596 pr_info("AMD-Vi: AMD IOMMU disabled on kernel command-line\n");
2597 free_dma_resources();
2598 free_iommu_resources();
2599 init_state = IOMMU_CMDLINE_DISABLED;
2600 ret = -EINVAL;
2601 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002602 break;
2603 case IOMMU_ACPI_FINISHED:
2604 early_enable_iommus();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002605 x86_platform.iommu_shutdown = disable_iommus;
2606 init_state = IOMMU_ENABLED;
2607 break;
2608 case IOMMU_ENABLED:
Joerg Roedel74ddda72017-07-26 14:17:55 +02002609 register_syscore_ops(&amd_iommu_syscore_ops);
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002610 ret = amd_iommu_init_pci();
2611 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_PCI_INIT;
2612 enable_iommus_v2();
2613 break;
2614 case IOMMU_PCI_INIT:
2615 ret = amd_iommu_enable_interrupts();
2616 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_INTERRUPTS_EN;
2617 break;
2618 case IOMMU_INTERRUPTS_EN:
Joerg Roedel1e6a7b02015-07-28 16:58:48 +02002619 ret = amd_iommu_init_dma_ops();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002620 init_state = ret ? IOMMU_INIT_ERROR : IOMMU_DMA_OPS;
2621 break;
2622 case IOMMU_DMA_OPS:
2623 init_state = IOMMU_INITIALIZED;
2624 break;
2625 case IOMMU_INITIALIZED:
2626 /* Nothing to do */
2627 break;
2628 case IOMMU_NOT_FOUND:
2629 case IOMMU_INIT_ERROR:
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002630 case IOMMU_CMDLINE_DISABLED:
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002631 /* Error states => do nothing */
2632 ret = -EINVAL;
2633 break;
2634 default:
2635 /* Unknown state */
2636 BUG();
2637 }
2638
2639 return ret;
2640}
2641
2642static int __init iommu_go_to_state(enum iommu_init_state state)
2643{
Joerg Roedel151b0902017-06-16 16:09:57 +02002644 int ret = -EINVAL;
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002645
2646 while (init_state != state) {
Joerg Roedel1b1e9422017-06-16 16:09:56 +02002647 if (init_state == IOMMU_NOT_FOUND ||
2648 init_state == IOMMU_INIT_ERROR ||
2649 init_state == IOMMU_CMDLINE_DISABLED)
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002650 break;
Joerg Roedel151b0902017-06-16 16:09:57 +02002651 ret = state_next();
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002652 }
2653
2654 return ret;
2655}
2656
Joerg Roedel6b474b82012-06-26 16:46:04 +02002657#ifdef CONFIG_IRQ_REMAP
2658int __init amd_iommu_prepare(void)
2659{
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002660 int ret;
2661
Jiang Liu7fa1c842015-01-07 15:31:42 +08002662 amd_iommu_irq_remap = true;
Joerg Roedel84d07792015-01-07 15:31:39 +08002663
Thomas Gleixner3f4cb7c2015-01-23 14:32:46 +01002664 ret = iommu_go_to_state(IOMMU_ACPI_FINISHED);
2665 if (ret)
2666 return ret;
2667 return amd_iommu_irq_remap ? 0 : -ENODEV;
Joerg Roedel6b474b82012-06-26 16:46:04 +02002668}
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002669
Joerg Roedel6b474b82012-06-26 16:46:04 +02002670int __init amd_iommu_enable(void)
2671{
2672 int ret;
2673
2674 ret = iommu_go_to_state(IOMMU_ENABLED);
2675 if (ret)
2676 return ret;
2677
2678 irq_remapping_enabled = 1;
2679
2680 return 0;
2681}
2682
2683void amd_iommu_disable(void)
2684{
2685 amd_iommu_suspend();
2686}
2687
2688int amd_iommu_reenable(int mode)
2689{
2690 amd_iommu_resume();
2691
2692 return 0;
2693}
2694
2695int __init amd_iommu_enable_faulting(void)
2696{
2697 /* We enable MSI later when PCI is initialized */
2698 return 0;
2699}
2700#endif
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002701
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002702/*
2703 * This is the core init function for AMD IOMMU hardware in the system.
2704 * This function is called from the generic x86 DMA layer initialization
2705 * code.
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002706 */
2707static int __init amd_iommu_init(void)
2708{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002709 int ret;
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002710
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002711 ret = iommu_go_to_state(IOMMU_INITIALIZED);
2712 if (ret) {
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002713 free_dma_resources();
2714 if (!irq_remapping_enabled) {
2715 disable_iommus();
Joerg Roedel90b3eb02017-06-16 16:09:55 +02002716 free_iommu_resources();
Joerg Roedeld04e0ba2012-07-02 16:02:20 +02002717 } else {
2718 struct amd_iommu *iommu;
2719
2720 uninit_device_table_dma();
2721 for_each_iommu(iommu)
2722 iommu_flush_all_caches(iommu);
2723 }
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002724 }
Joerg Roedel8704a1b2012-03-01 15:57:53 +01002725
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002726 return ret;
Joerg Roedelfe74c9c2008-06-26 21:27:50 +02002727}
2728
Joerg Roedelb65233a2008-07-11 17:14:21 +02002729/****************************************************************************
2730 *
2731 * Early detect code. This code runs at IOMMU detection time in the DMA
2732 * layer. It just looks if there is an IVRS ACPI table to detect AMD
2733 * IOMMUs
2734 *
2735 ****************************************************************************/
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002736int __init amd_iommu_detect(void)
Joerg Roedelae7877d2008-06-26 21:27:51 +02002737{
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002738 int ret;
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002739
FUJITA Tomonori75f1cdf2009-11-10 19:46:20 +09002740 if (no_iommu || (iommu_detected && !gart_iommu_aperture))
Konrad Rzeszutek Wilk480125b2010-08-26 13:57:57 -04002741 return -ENODEV;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002742
Joerg Roedel2c0ae172012-06-12 15:59:30 +02002743 ret = iommu_go_to_state(IOMMU_IVRS_DETECTED);
2744 if (ret)
2745 return ret;
Linus Torvalds11bd04f2009-12-11 12:18:16 -08002746
Joerg Roedel02f3b3f2012-06-11 17:45:25 +02002747 amd_iommu_detected = true;
2748 iommu_detected = 1;
2749 x86_init.iommu.iommu_init = amd_iommu_init;
2750
Jérôme Glisse4781bc42015-08-31 18:13:03 -04002751 return 1;
Joerg Roedelae7877d2008-06-26 21:27:51 +02002752}
2753
Joerg Roedelb65233a2008-07-11 17:14:21 +02002754/****************************************************************************
2755 *
2756 * Parsing functions for the AMD IOMMU specific kernel command line
2757 * options.
2758 *
2759 ****************************************************************************/
2760
Joerg Roedelfefda112009-05-20 12:21:42 +02002761static int __init parse_amd_iommu_dump(char *str)
2762{
2763 amd_iommu_dump = true;
2764
2765 return 1;
2766}
2767
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002768static int __init parse_amd_iommu_intr(char *str)
2769{
2770 for (; *str; ++str) {
2771 if (strncmp(str, "legacy", 6) == 0) {
2772 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_LEGACY;
2773 break;
2774 }
2775 if (strncmp(str, "vapic", 5) == 0) {
2776 amd_iommu_guest_ir = AMD_IOMMU_GUEST_IR_VAPIC;
2777 break;
2778 }
2779 }
2780 return 1;
2781}
2782
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002783static int __init parse_amd_iommu_options(char *str)
2784{
2785 for (; *str; ++str) {
Joerg Roedel695b5672008-11-17 15:16:43 +01002786 if (strncmp(str, "fullflush", 9) == 0)
FUJITA Tomonoriafa9fdc2008-09-20 01:23:30 +09002787 amd_iommu_unmap_flush = true;
Joerg Roedela5235722010-05-11 17:12:33 +02002788 if (strncmp(str, "off", 3) == 0)
2789 amd_iommu_disabled = true;
Joerg Roedel5abcdba2011-12-01 15:49:45 +01002790 if (strncmp(str, "force_isolation", 15) == 0)
2791 amd_iommu_force_isolation = true;
Joerg Roedel918ad6c2008-06-26 21:27:52 +02002792 }
2793
2794 return 1;
2795}
2796
Joerg Roedel440e89982013-04-09 16:35:28 +02002797static int __init parse_ivrs_ioapic(char *str)
2798{
2799 unsigned int bus, dev, fn;
2800 int ret, id, i;
2801 u16 devid;
2802
2803 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2804
2805 if (ret != 4) {
2806 pr_err("AMD-Vi: Invalid command line: ivrs_ioapic%s\n", str);
2807 return 1;
2808 }
2809
2810 if (early_ioapic_map_size == EARLY_MAP_SIZE) {
2811 pr_err("AMD-Vi: Early IOAPIC map overflow - ignoring ivrs_ioapic%s\n",
2812 str);
2813 return 1;
2814 }
2815
2816 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2817
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002818 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002819 i = early_ioapic_map_size++;
2820 early_ioapic_map[i].id = id;
2821 early_ioapic_map[i].devid = devid;
2822 early_ioapic_map[i].cmd_line = true;
2823
2824 return 1;
2825}
2826
2827static int __init parse_ivrs_hpet(char *str)
2828{
2829 unsigned int bus, dev, fn;
2830 int ret, id, i;
2831 u16 devid;
2832
2833 ret = sscanf(str, "[%d]=%x:%x.%x", &id, &bus, &dev, &fn);
2834
2835 if (ret != 4) {
2836 pr_err("AMD-Vi: Invalid command line: ivrs_hpet%s\n", str);
2837 return 1;
2838 }
2839
2840 if (early_hpet_map_size == EARLY_MAP_SIZE) {
2841 pr_err("AMD-Vi: Early HPET map overflow - ignoring ivrs_hpet%s\n",
2842 str);
2843 return 1;
2844 }
2845
2846 devid = ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2847
Joerg Roedeldfbb6d42013-04-09 19:06:18 +02002848 cmdline_maps = true;
Joerg Roedel440e89982013-04-09 16:35:28 +02002849 i = early_hpet_map_size++;
2850 early_hpet_map[i].id = id;
2851 early_hpet_map[i].devid = devid;
2852 early_hpet_map[i].cmd_line = true;
2853
2854 return 1;
2855}
2856
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002857static int __init parse_ivrs_acpihid(char *str)
2858{
2859 u32 bus, dev, fn;
2860 char *hid, *uid, *p;
2861 char acpiid[ACPIHID_UID_LEN + ACPIHID_HID_LEN] = {0};
2862 int ret, i;
2863
2864 ret = sscanf(str, "[%x:%x.%x]=%s", &bus, &dev, &fn, acpiid);
2865 if (ret != 4) {
2866 pr_err("AMD-Vi: Invalid command line: ivrs_acpihid(%s)\n", str);
2867 return 1;
2868 }
2869
2870 p = acpiid;
2871 hid = strsep(&p, ":");
2872 uid = p;
2873
2874 if (!hid || !(*hid) || !uid) {
2875 pr_err("AMD-Vi: Invalid command line: hid or uid\n");
2876 return 1;
2877 }
2878
2879 i = early_acpihid_map_size++;
2880 memcpy(early_acpihid_map[i].hid, hid, strlen(hid));
2881 memcpy(early_acpihid_map[i].uid, uid, strlen(uid));
2882 early_acpihid_map[i].devid =
2883 ((bus & 0xff) << 8) | ((dev & 0x1f) << 3) | (fn & 0x7);
2884 early_acpihid_map[i].cmd_line = true;
2885
2886 return 1;
2887}
2888
Joerg Roedel440e89982013-04-09 16:35:28 +02002889__setup("amd_iommu_dump", parse_amd_iommu_dump);
2890__setup("amd_iommu=", parse_amd_iommu_options);
Suravee Suthikulpanit3928aa32016-08-23 13:52:32 -05002891__setup("amd_iommu_intr=", parse_amd_iommu_intr);
Joerg Roedel440e89982013-04-09 16:35:28 +02002892__setup("ivrs_ioapic", parse_ivrs_ioapic);
2893__setup("ivrs_hpet", parse_ivrs_hpet);
Suravee Suthikulpanitca3bf5d2016-04-01 09:06:01 -04002894__setup("ivrs_acpihid", parse_ivrs_acpihid);
Konrad Rzeszutek Wilk22e6daf2010-08-26 13:58:03 -04002895
2896IOMMU_INIT_FINISH(amd_iommu_detect,
2897 gart_iommu_hole_init,
Joerg Roedel98f1ad22012-07-06 13:28:37 +02002898 NULL,
2899 NULL);
Joerg Roedel400a28a2011-11-28 15:11:02 +01002900
2901bool amd_iommu_v2_supported(void)
2902{
2903 return amd_iommu_v2_present;
2904}
2905EXPORT_SYMBOL(amd_iommu_v2_supported);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002906
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002907struct amd_iommu *get_amd_iommu(unsigned int idx)
2908{
2909 unsigned int i = 0;
2910 struct amd_iommu *iommu;
2911
2912 for_each_iommu(iommu)
2913 if (i++ == idx)
2914 return iommu;
2915 return NULL;
2916}
2917EXPORT_SYMBOL(get_amd_iommu);
2918
Steven L Kinney30861dd2013-06-05 16:11:48 -05002919/****************************************************************************
2920 *
2921 * IOMMU EFR Performance Counter support functionality. This code allows
2922 * access to the IOMMU PC functionality.
2923 *
2924 ****************************************************************************/
2925
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002926u8 amd_iommu_pc_get_max_banks(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002927{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002928 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002929
Steven L Kinney30861dd2013-06-05 16:11:48 -05002930 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002931 return iommu->max_banks;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002932
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002933 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002934}
2935EXPORT_SYMBOL(amd_iommu_pc_get_max_banks);
2936
2937bool amd_iommu_pc_supported(void)
2938{
2939 return amd_iommu_pc_present;
2940}
2941EXPORT_SYMBOL(amd_iommu_pc_supported);
2942
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002943u8 amd_iommu_pc_get_max_counters(unsigned int idx)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002944{
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002945 struct amd_iommu *iommu = get_amd_iommu(idx);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002946
Steven L Kinney30861dd2013-06-05 16:11:48 -05002947 if (iommu)
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002948 return iommu->max_counters;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002949
Suravee Suthikulpanitf5863a02017-02-24 02:48:18 -06002950 return 0;
Steven L Kinney30861dd2013-06-05 16:11:48 -05002951}
2952EXPORT_SYMBOL(amd_iommu_pc_get_max_counters);
2953
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002954static int iommu_pc_get_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr,
2955 u8 fxn, u64 *value, bool is_write)
Steven L Kinney30861dd2013-06-05 16:11:48 -05002956{
Steven L Kinney30861dd2013-06-05 16:11:48 -05002957 u32 offset;
2958 u32 max_offset_lim;
2959
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002960 /* Make sure the IOMMU PC resource is available */
2961 if (!amd_iommu_pc_present)
2962 return -ENODEV;
2963
Steven L Kinney30861dd2013-06-05 16:11:48 -05002964 /* Check for valid iommu and pc register indexing */
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002965 if (WARN_ON(!iommu || (fxn > 0x28) || (fxn & 7)))
Steven L Kinney30861dd2013-06-05 16:11:48 -05002966 return -ENODEV;
2967
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002968 offset = (u32)(((0x40 | bank) << 12) | (cntr << 8) | fxn);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002969
2970 /* Limit the offset to the hw defined mmio region aperture */
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002971 max_offset_lim = (u32)(((0x40 | iommu->max_banks) << 12) |
Steven L Kinney30861dd2013-06-05 16:11:48 -05002972 (iommu->max_counters << 8) | 0x28);
2973 if ((offset < MMIO_CNTR_REG_OFFSET) ||
2974 (offset > max_offset_lim))
2975 return -EINVAL;
2976
2977 if (is_write) {
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002978 u64 val = *value & GENMASK_ULL(47, 0);
2979
2980 writel((u32)val, iommu->mmio_base + offset);
2981 writel((val >> 32), iommu->mmio_base + offset + 4);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002982 } else {
2983 *value = readl(iommu->mmio_base + offset + 4);
2984 *value <<= 32;
Suravee Suthikulpanit0a6d80c2017-02-24 02:48:16 -06002985 *value |= readl(iommu->mmio_base + offset);
2986 *value &= GENMASK_ULL(47, 0);
Steven L Kinney30861dd2013-06-05 16:11:48 -05002987 }
2988
2989 return 0;
2990}
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002991
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002992int amd_iommu_pc_get_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002993{
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002994 if (!iommu)
2995 return -EINVAL;
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002996
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002997 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, false);
Suravee Suthikulpanit38e45d02016-02-23 13:03:30 +01002998}
Suravee Suthikulpanit1650dfd2017-02-24 02:48:19 -06002999EXPORT_SYMBOL(amd_iommu_pc_get_reg);
3000
3001int amd_iommu_pc_set_reg(struct amd_iommu *iommu, u8 bank, u8 cntr, u8 fxn, u64 *value)
3002{
3003 if (!iommu)
3004 return -EINVAL;
3005
3006 return iommu_pc_get_set_reg(iommu, bank, cntr, fxn, value, true);
3007}
3008EXPORT_SYMBOL(amd_iommu_pc_set_reg);