blob: f8cf99bddfec791c4c322fdbd8ff7de3824f7e6f [file] [log] [blame]
Le Tan1da12ec2014-08-16 13:55:38 +08001/*
2 * QEMU emulation of an Intel IOMMU (VT-d)
3 * (DMA Remapping device)
4 *
5 * Copyright (C) 2013 Knut Omang, Oracle <knut.omang@oracle.com>
6 * Copyright (C) 2014 Le Tan, <tamlokveer@gmail.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, see <http://www.gnu.org/licenses/>.
20 *
21 * Lots of defines copied from kernel/include/linux/intel-iommu.h:
22 * Copyright (C) 2006-2008 Intel Corporation
23 * Author: Ashok Raj <ashok.raj@intel.com>
24 * Author: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
25 *
26 */
27
28#ifndef HW_I386_INTEL_IOMMU_INTERNAL_H
29#define HW_I386_INTEL_IOMMU_INTERNAL_H
30#include "hw/i386/intel_iommu.h"
31
32/*
33 * Intel IOMMU register specification
34 */
35#define DMAR_VER_REG 0x0 /* Arch version supported by this IOMMU */
36#define DMAR_CAP_REG 0x8 /* Hardware supported capabilities */
37#define DMAR_CAP_REG_HI 0xc /* High 32-bit of DMAR_CAP_REG */
38#define DMAR_ECAP_REG 0x10 /* Extended capabilities supported */
39#define DMAR_ECAP_REG_HI 0X14
40#define DMAR_GCMD_REG 0x18 /* Global command */
41#define DMAR_GSTS_REG 0x1c /* Global status */
42#define DMAR_RTADDR_REG 0x20 /* Root entry table */
43#define DMAR_RTADDR_REG_HI 0X24
44#define DMAR_CCMD_REG 0x28 /* Context command */
45#define DMAR_CCMD_REG_HI 0x2c
46#define DMAR_FSTS_REG 0x34 /* Fault status */
47#define DMAR_FECTL_REG 0x38 /* Fault control */
48#define DMAR_FEDATA_REG 0x3c /* Fault event interrupt data */
49#define DMAR_FEADDR_REG 0x40 /* Fault event interrupt addr */
50#define DMAR_FEUADDR_REG 0x44 /* Upper address */
51#define DMAR_AFLOG_REG 0x58 /* Advanced fault control */
52#define DMAR_AFLOG_REG_HI 0X5c
53#define DMAR_PMEN_REG 0x64 /* Enable protected memory region */
54#define DMAR_PLMBASE_REG 0x68 /* PMRR low addr */
55#define DMAR_PLMLIMIT_REG 0x6c /* PMRR low limit */
56#define DMAR_PHMBASE_REG 0x70 /* PMRR high base addr */
57#define DMAR_PHMBASE_REG_HI 0X74
58#define DMAR_PHMLIMIT_REG 0x78 /* PMRR high limit */
59#define DMAR_PHMLIMIT_REG_HI 0x7c
60#define DMAR_IQH_REG 0x80 /* Invalidation queue head */
61#define DMAR_IQH_REG_HI 0X84
62#define DMAR_IQT_REG 0x88 /* Invalidation queue tail */
63#define DMAR_IQT_REG_HI 0X8c
64#define DMAR_IQA_REG 0x90 /* Invalidation queue addr */
65#define DMAR_IQA_REG_HI 0x94
66#define DMAR_ICS_REG 0x9c /* Invalidation complete status */
67#define DMAR_IRTA_REG 0xb8 /* Interrupt remapping table addr */
68#define DMAR_IRTA_REG_HI 0xbc
69#define DMAR_IECTL_REG 0xa0 /* Invalidation event control */
70#define DMAR_IEDATA_REG 0xa4 /* Invalidation event data */
71#define DMAR_IEADDR_REG 0xa8 /* Invalidation event address */
72#define DMAR_IEUADDR_REG 0xac /* Invalidation event address */
73#define DMAR_PQH_REG 0xc0 /* Page request queue head */
74#define DMAR_PQH_REG_HI 0xc4
75#define DMAR_PQT_REG 0xc8 /* Page request queue tail*/
76#define DMAR_PQT_REG_HI 0xcc
77#define DMAR_PQA_REG 0xd0 /* Page request queue address */
78#define DMAR_PQA_REG_HI 0xd4
79#define DMAR_PRS_REG 0xdc /* Page request status */
80#define DMAR_PECTL_REG 0xe0 /* Page request event control */
81#define DMAR_PEDATA_REG 0xe4 /* Page request event data */
82#define DMAR_PEADDR_REG 0xe8 /* Page request event address */
83#define DMAR_PEUADDR_REG 0xec /* Page event upper address */
84#define DMAR_MTRRCAP_REG 0x100 /* MTRR capability */
85#define DMAR_MTRRCAP_REG_HI 0x104
86#define DMAR_MTRRDEF_REG 0x108 /* MTRR default type */
87#define DMAR_MTRRDEF_REG_HI 0x10c
88
89/* IOTLB registers */
90#define DMAR_IOTLB_REG_OFFSET 0xf0 /* Offset to the IOTLB registers */
91#define DMAR_IVA_REG DMAR_IOTLB_REG_OFFSET /* Invalidate address */
92#define DMAR_IVA_REG_HI (DMAR_IVA_REG + 4)
93/* IOTLB invalidate register */
94#define DMAR_IOTLB_REG (DMAR_IOTLB_REG_OFFSET + 0x8)
95#define DMAR_IOTLB_REG_HI (DMAR_IOTLB_REG + 4)
96
97/* FRCD */
98#define DMAR_FRCD_REG_OFFSET 0x220 /* Offset to the fault recording regs */
99/* NOTICE: If you change the DMAR_FRCD_REG_NR, please remember to change the
100 * DMAR_REG_SIZE in include/hw/i386/intel_iommu.h.
101 * #define DMAR_REG_SIZE (DMAR_FRCD_REG_OFFSET + 16 * DMAR_FRCD_REG_NR)
102 */
103#define DMAR_FRCD_REG_NR 1ULL /* Num of fault recording regs */
104
105#define DMAR_FRCD_REG_0_0 0x220 /* The 0th fault recording regs */
106#define DMAR_FRCD_REG_0_1 0x224
107#define DMAR_FRCD_REG_0_2 0x228
108#define DMAR_FRCD_REG_0_3 0x22c
109
110/* Interrupt Address Range */
111#define VTD_INTERRUPT_ADDR_FIRST 0xfee00000ULL
112#define VTD_INTERRUPT_ADDR_LAST 0xfeefffffULL
Peter Xu651e4ce2016-07-14 13:56:22 +0800113#define VTD_INTERRUPT_ADDR_SIZE (VTD_INTERRUPT_ADDR_LAST - \
114 VTD_INTERRUPT_ADDR_FIRST + 1)
Le Tan1da12ec2014-08-16 13:55:38 +0800115
Le Tanb5a280c2014-08-16 13:55:44 +0800116/* The shift of source_id in the key of IOTLB hash table */
Jason Wangec1a78c2023-04-12 15:35:10 +0800117#define VTD_IOTLB_SID_SHIFT 26
118#define VTD_IOTLB_LVL_SHIFT 42
119#define VTD_IOTLB_PASID_SHIFT 44
Le Tanb5a280c2014-08-16 13:55:44 +0800120#define VTD_IOTLB_MAX_SIZE 1024 /* Max size of the hash table */
121
Le Tan1da12ec2014-08-16 13:55:38 +0800122/* IOTLB_REG */
123#define VTD_TLB_GLOBAL_FLUSH (1ULL << 60) /* Global invalidation */
124#define VTD_TLB_DSI_FLUSH (2ULL << 60) /* Domain-selective */
125#define VTD_TLB_PSI_FLUSH (3ULL << 60) /* Page-selective */
126#define VTD_TLB_FLUSH_GRANU_MASK (3ULL << 60)
127#define VTD_TLB_GLOBAL_FLUSH_A (1ULL << 57)
128#define VTD_TLB_DSI_FLUSH_A (2ULL << 57)
129#define VTD_TLB_PSI_FLUSH_A (3ULL << 57)
130#define VTD_TLB_FLUSH_GRANU_MASK_A (3ULL << 57)
131#define VTD_TLB_IVT (1ULL << 63)
Le Tanb5a280c2014-08-16 13:55:44 +0800132#define VTD_TLB_DID(val) (((val) >> 32) & VTD_DOMAIN_ID_MASK)
133
134/* IVA_REG */
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500135#define VTD_IVA_ADDR(val) ((val) & ~0xfffULL)
Le Tanb5a280c2014-08-16 13:55:44 +0800136#define VTD_IVA_AM(val) ((val) & 0x3fULL)
Le Tan1da12ec2014-08-16 13:55:38 +0800137
138/* GCMD_REG */
139#define VTD_GCMD_TE (1UL << 31)
140#define VTD_GCMD_SRTP (1UL << 30)
141#define VTD_GCMD_SFL (1UL << 29)
142#define VTD_GCMD_EAFL (1UL << 28)
143#define VTD_GCMD_WBF (1UL << 27)
144#define VTD_GCMD_QIE (1UL << 26)
145#define VTD_GCMD_IRE (1UL << 25)
146#define VTD_GCMD_SIRTP (1UL << 24)
147#define VTD_GCMD_CFI (1UL << 23)
148
149/* GSTS_REG */
150#define VTD_GSTS_TES (1UL << 31)
151#define VTD_GSTS_RTPS (1UL << 30)
152#define VTD_GSTS_FLS (1UL << 29)
153#define VTD_GSTS_AFLS (1UL << 28)
154#define VTD_GSTS_WBFS (1UL << 27)
155#define VTD_GSTS_QIES (1UL << 26)
156#define VTD_GSTS_IRES (1UL << 25)
157#define VTD_GSTS_IRTPS (1UL << 24)
158#define VTD_GSTS_CFIS (1UL << 23)
159
160/* CCMD_REG */
161#define VTD_CCMD_ICC (1ULL << 63)
162#define VTD_CCMD_GLOBAL_INVL (1ULL << 61)
163#define VTD_CCMD_DOMAIN_INVL (2ULL << 61)
164#define VTD_CCMD_DEVICE_INVL (3ULL << 61)
165#define VTD_CCMD_CIRG_MASK (3ULL << 61)
166#define VTD_CCMD_GLOBAL_INVL_A (1ULL << 59)
167#define VTD_CCMD_DOMAIN_INVL_A (2ULL << 59)
168#define VTD_CCMD_DEVICE_INVL_A (3ULL << 59)
169#define VTD_CCMD_CAIG_MASK (3ULL << 59)
Le Tand92fa2d2014-08-16 13:55:43 +0800170#define VTD_CCMD_DID(val) ((val) & VTD_DOMAIN_ID_MASK)
171#define VTD_CCMD_SID(val) (((val) >> 16) & 0xffffULL)
172#define VTD_CCMD_FM(val) (((val) >> 32) & 3ULL)
Le Tan1da12ec2014-08-16 13:55:38 +0800173
174/* RTADDR_REG */
Liu, Yi Lfb43cf72019-03-05 10:34:53 +0800175#define VTD_RTADDR_SMT (1ULL << 10)
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500176#define VTD_RTADDR_ADDR_MASK(aw) (VTD_HAW_MASK(aw) ^ 0xfffULL)
Le Tan1da12ec2014-08-16 13:55:38 +0800177
Peter Xua5861432016-07-14 13:56:18 +0800178/* IRTA_REG */
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500179#define VTD_IRTA_ADDR_MASK(aw) (VTD_HAW_MASK(aw) ^ 0xfffULL)
Jan Kiszka28589312016-07-14 13:56:28 +0800180#define VTD_IRTA_EIME (1ULL << 11)
Peter Xua5861432016-07-14 13:56:18 +0800181#define VTD_IRTA_SIZE_MASK (0xfULL)
182
Le Tan1da12ec2014-08-16 13:55:38 +0800183/* ECAP_REG */
184/* (offset >> 4) << 8 */
185#define VTD_ECAP_IRO (DMAR_IOTLB_REG_OFFSET << 4)
186#define VTD_ECAP_QI (1ULL << 1)
Jason Wang554f5e12016-12-30 18:09:14 +0800187#define VTD_ECAP_DT (1ULL << 2)
Peter Xud54bd7f2016-07-14 13:56:16 +0800188/* Interrupt Remapping support */
189#define VTD_ECAP_IR (1ULL << 3)
Jan Kiszka28589312016-07-14 13:56:28 +0800190#define VTD_ECAP_EIM (1ULL << 4)
Peter Xudbaabb22017-05-19 11:19:47 +0800191#define VTD_ECAP_PT (1ULL << 6)
Jason Wangb8ffd7d2022-02-14 14:03:46 +0800192#define VTD_ECAP_SC (1ULL << 7)
Radim Krčmářa3f409c2016-07-14 13:56:34 +0800193#define VTD_ECAP_MHMV (15ULL << 20)
Yi Sun4a4f2192019-03-05 10:34:55 +0800194#define VTD_ECAP_SRS (1ULL << 31)
Jason Wang1b2b1232022-10-28 14:14:36 +0800195#define VTD_ECAP_PASID (1ULL << 40)
Liu, Yi Lc0c1d352019-03-05 10:34:54 +0800196#define VTD_ECAP_SMTS (1ULL << 43)
Yi Sun4a4f2192019-03-05 10:34:55 +0800197#define VTD_ECAP_SLTS (1ULL << 46)
Le Tan1da12ec2014-08-16 13:55:38 +0800198
199/* CAP_REG */
200/* (offset >> 4) << 24 */
201#define VTD_CAP_FRO (DMAR_FRCD_REG_OFFSET << 20)
202#define VTD_CAP_NFR ((DMAR_FRCD_REG_NR - 1) << 40)
203#define VTD_DOMAIN_ID_SHIFT 16 /* 16-bit domain id for 64K domains */
Le Tand92fa2d2014-08-16 13:55:43 +0800204#define VTD_DOMAIN_ID_MASK ((1UL << VTD_DOMAIN_ID_SHIFT) - 1)
Le Tan1da12ec2014-08-16 13:55:38 +0800205#define VTD_CAP_ND (((VTD_DOMAIN_ID_SHIFT - 4) / 2) & 7ULL)
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500206#define VTD_ADDRESS_SIZE(aw) (1ULL << (aw))
207#define VTD_CAP_MGAW(aw) ((((aw) - 1) & 0x3fULL) << 16)
Jason Wangd66b9692016-01-14 00:47:24 -0500208#define VTD_MAMV 18ULL
Le Tanb5a280c2014-08-16 13:55:44 +0800209#define VTD_CAP_MAMV (VTD_MAMV << 48)
210#define VTD_CAP_PSI (1ULL << 39)
Jason Wangd66b9692016-01-14 00:47:24 -0500211#define VTD_CAP_SLLPS ((1ULL << 34) | (1ULL << 35))
Peter Xuccc23bb2018-12-17 15:31:12 +0800212#define VTD_CAP_DRAIN_WRITE (1ULL << 54)
213#define VTD_CAP_DRAIN_READ (1ULL << 55)
214#define VTD_CAP_DRAIN (VTD_CAP_DRAIN_READ | VTD_CAP_DRAIN_WRITE)
Aviv Ben-David3b40f0e2017-02-07 16:28:06 +0800215#define VTD_CAP_CM (1ULL << 7)
Jason Wang1b2b1232022-10-28 14:14:36 +0800216#define VTD_PASID_ID_SHIFT 20
217#define VTD_PASID_ID_MASK ((1ULL << VTD_PASID_ID_SHIFT) - 1)
Le Tan1da12ec2014-08-16 13:55:38 +0800218
219/* Supported Adjusted Guest Address Widths */
220#define VTD_CAP_SAGAW_SHIFT 8
221#define VTD_CAP_SAGAW_MASK (0x1fULL << VTD_CAP_SAGAW_SHIFT)
222 /* 39-bit AGAW, 3-level page-table */
223#define VTD_CAP_SAGAW_39bit (0x2ULL << VTD_CAP_SAGAW_SHIFT)
224 /* 48-bit AGAW, 4-level page-table */
225#define VTD_CAP_SAGAW_48bit (0x4ULL << VTD_CAP_SAGAW_SHIFT)
Le Tan1da12ec2014-08-16 13:55:38 +0800226
227/* IQT_REG */
Liu, Yi Lc0c1d352019-03-05 10:34:54 +0800228#define VTD_IQT_QT(dw_bit, val) (dw_bit ? (((val) >> 5) & 0x3fffULL) : \
229 (((val) >> 4) & 0x7fffULL))
230#define VTD_IQT_QT_256_RSV_BIT 0x10
Le Tan1da12ec2014-08-16 13:55:38 +0800231
232/* IQA_REG */
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500233#define VTD_IQA_IQA_MASK(aw) (VTD_HAW_MASK(aw) ^ 0xfffULL)
Le Tan1da12ec2014-08-16 13:55:38 +0800234#define VTD_IQA_QS 0x7ULL
Liu, Yi Lc0c1d352019-03-05 10:34:54 +0800235#define VTD_IQA_DW_MASK 0x800
Le Tan1da12ec2014-08-16 13:55:38 +0800236
237/* IQH_REG */
Liu Yi La4544c42020-07-04 01:07:15 -0700238#define VTD_IQH_QH_SHIFT_4 4
239#define VTD_IQH_QH_SHIFT_5 5
Le Tan1da12ec2014-08-16 13:55:38 +0800240#define VTD_IQH_QH_MASK 0x7fff0ULL
241
242/* ICS_REG */
243#define VTD_ICS_IWC 1UL
244
245/* IECTL_REG */
246#define VTD_IECTL_IM (1UL << 31)
247#define VTD_IECTL_IP (1UL << 30)
248
249/* FSTS_REG */
250#define VTD_FSTS_FRI_MASK 0xff00UL
251#define VTD_FSTS_FRI(val) ((((uint32_t)(val)) << 8) & VTD_FSTS_FRI_MASK)
252#define VTD_FSTS_IQE (1UL << 4)
253#define VTD_FSTS_PPF (1UL << 1)
254#define VTD_FSTS_PFO 1UL
255
256/* FECTL_REG */
257#define VTD_FECTL_IM (1UL << 31)
258#define VTD_FECTL_IP (1UL << 30)
259
260/* Fault Recording Register */
261/* For the high 64-bit of 128-bit */
262#define VTD_FRCD_F (1ULL << 63)
263#define VTD_FRCD_T (1ULL << 62)
264#define VTD_FRCD_FR(val) (((val) & 0xffULL) << 32)
265#define VTD_FRCD_SID_MASK 0xffffULL
266#define VTD_FRCD_SID(val) ((val) & VTD_FRCD_SID_MASK)
267/* For the low 64-bit of 128-bit */
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500268#define VTD_FRCD_FI(val) ((val) & ~0xfffULL)
Jason Wang1b2b1232022-10-28 14:14:36 +0800269#define VTD_FRCD_PV(val) (((val) & 0xffffULL) << 40)
270#define VTD_FRCD_PP(val) (((val) & 0x1) << 31)
David Woodhousec7016bf2023-08-23 13:23:25 +0100271#define VTD_FRCD_IR_IDX(val) (((val) & 0xffffULL) << 48)
Le Tan1da12ec2014-08-16 13:55:38 +0800272
273/* DMA Remapping Fault Conditions */
274typedef enum VTDFaultReason {
275 VTD_FR_RESERVED = 0, /* Reserved for Advanced Fault logging */
276 VTD_FR_ROOT_ENTRY_P = 1, /* The Present(P) field of root-entry is 0 */
277 VTD_FR_CONTEXT_ENTRY_P, /* The Present(P) field of context-entry is 0 */
278 VTD_FR_CONTEXT_ENTRY_INV, /* Invalid programming of a context-entry */
279 VTD_FR_ADDR_BEYOND_MGAW, /* Input-address above (2^x-1) */
280 VTD_FR_WRITE, /* No write permission */
281 VTD_FR_READ, /* No read permission */
282 /* Fail to access a second-level paging entry (not SL_PML4E) */
283 VTD_FR_PAGING_ENTRY_INV,
284 VTD_FR_ROOT_TABLE_INV, /* Fail to access a root-entry */
285 VTD_FR_CONTEXT_TABLE_INV, /* Fail to access a context-entry */
286 /* Non-zero reserved field in a present root-entry */
287 VTD_FR_ROOT_ENTRY_RSVD,
288 /* Non-zero reserved field in a present context-entry */
289 VTD_FR_CONTEXT_ENTRY_RSVD,
290 /* Non-zero reserved field in a second-level paging entry with at lease one
291 * Read(R) and Write(W) or Execute(E) field is Set.
292 */
293 VTD_FR_PAGING_ENTRY_RSVD,
294 /* Translation request or translated request explicitly blocked dut to the
295 * programming of the Translation Type (T) field in the present
296 * context-entry.
297 */
298 VTD_FR_CONTEXT_ENTRY_TT,
Jason Wangea97a1b2022-02-10 17:28:15 +0800299 /* Output address in the interrupt address range */
300 VTD_FR_INTERRUPT_ADDR = 0xE,
Peter Xua4ca2972016-07-14 13:56:21 +0800301
302 /* Interrupt remapping transition faults */
303 VTD_FR_IR_REQ_RSVD = 0x20, /* One or more IR request reserved
304 * fields set */
305 VTD_FR_IR_INDEX_OVER = 0x21, /* Index value greater than max */
306 VTD_FR_IR_ENTRY_P = 0x22, /* Present (P) not set in IRTE */
307 VTD_FR_IR_ROOT_INVAL = 0x23, /* IR Root table invalid */
308 VTD_FR_IR_IRTE_RSVD = 0x24, /* IRTE Rsvd field non-zero with
309 * Present flag set */
310 VTD_FR_IR_REQ_COMPAT = 0x25, /* Encountered compatible IR
311 * request while disabled */
312 VTD_FR_IR_SID_ERR = 0x26, /* Invalid Source-ID */
313
Liu, Yi Lfb43cf72019-03-05 10:34:53 +0800314 VTD_FR_PASID_TABLE_INV = 0x58, /*Invalid PASID table entry */
315
Jason Wangea97a1b2022-02-10 17:28:15 +0800316 /* Output address in the interrupt address range for scalable mode */
317 VTD_FR_SM_INTERRUPT_ADDR = 0x87,
Le Tan1da12ec2014-08-16 13:55:38 +0800318 VTD_FR_MAX, /* Guard */
319} VTDFaultReason;
320
Le Tand92fa2d2014-08-16 13:55:43 +0800321#define VTD_CONTEXT_CACHE_GEN_MAX 0xffffffffUL
322
Peter Xu02a2cbc2016-07-14 13:56:26 +0800323/* Interrupt Entry Cache Invalidation Descriptor: VT-d 6.5.2.7. */
324struct VTDInvDescIEC {
Thomas Huth4572b222023-08-02 15:57:20 +0200325#if HOST_BIG_ENDIAN
326 uint64_t reserved_2:16;
327 uint64_t index:16; /* Start index to invalidate */
328 uint64_t index_mask:5; /* 2^N for continuous int invalidation */
329 uint64_t resved_1:22;
330 uint64_t granularity:1; /* If set, it's global IR invalidation */
331 uint64_t type:4; /* Should always be 0x4 */
332#else
Peter Xu02a2cbc2016-07-14 13:56:26 +0800333 uint32_t type:4; /* Should always be 0x4 */
334 uint32_t granularity:1; /* If set, it's global IR invalidation */
335 uint32_t resved_1:22;
336 uint32_t index_mask:5; /* 2^N for continuous int invalidation */
337 uint32_t index:16; /* Start index to invalidate */
338 uint32_t reserved_2:16;
Thomas Huth4572b222023-08-02 15:57:20 +0200339#endif
Le Taned7b8fb2014-08-16 13:55:42 +0800340};
Peter Xu02a2cbc2016-07-14 13:56:26 +0800341typedef struct VTDInvDescIEC VTDInvDescIEC;
342
343/* Queued Invalidation Descriptor */
344union VTDInvDesc {
345 struct {
346 uint64_t lo;
347 uint64_t hi;
348 };
Liu, Yi Lc0c1d352019-03-05 10:34:54 +0800349 struct {
350 uint64_t val[4];
351 };
Peter Xu02a2cbc2016-07-14 13:56:26 +0800352 union {
353 VTDInvDescIEC iec;
354 };
355};
356typedef union VTDInvDesc VTDInvDesc;
Le Taned7b8fb2014-08-16 13:55:42 +0800357
358/* Masks for struct VTDInvDesc */
359#define VTD_INV_DESC_TYPE 0xf
360#define VTD_INV_DESC_CC 0x1 /* Context-cache Invalidate Desc */
361#define VTD_INV_DESC_IOTLB 0x2
Jason Wang554f5e12016-12-30 18:09:14 +0800362#define VTD_INV_DESC_DEVICE 0x3
Peter Xub7910472016-07-14 13:56:15 +0800363#define VTD_INV_DESC_IEC 0x4 /* Interrupt Entry Cache
364 Invalidate Descriptor */
Le Taned7b8fb2014-08-16 13:55:42 +0800365#define VTD_INV_DESC_WAIT 0x5 /* Invalidation Wait Descriptor */
Yi Sun4a4f2192019-03-05 10:34:55 +0800366#define VTD_INV_DESC_PIOTLB 0x6 /* PASID-IOTLB Invalidate Desc */
367#define VTD_INV_DESC_PC 0x7 /* PASID-cache Invalidate Desc */
Le Taned7b8fb2014-08-16 13:55:42 +0800368#define VTD_INV_DESC_NONE 0 /* Not an Invalidate Descriptor */
369
370/* Masks for Invalidation Wait Descriptor*/
371#define VTD_INV_DESC_WAIT_SW (1ULL << 5)
372#define VTD_INV_DESC_WAIT_IF (1ULL << 4)
373#define VTD_INV_DESC_WAIT_FN (1ULL << 6)
374#define VTD_INV_DESC_WAIT_DATA_SHIFT 32
375#define VTD_INV_DESC_WAIT_RSVD_LO 0Xffffff80ULL
376#define VTD_INV_DESC_WAIT_RSVD_HI 3ULL
Le Tan1da12ec2014-08-16 13:55:38 +0800377
Le Tand92fa2d2014-08-16 13:55:43 +0800378/* Masks for Context-cache Invalidation Descriptor */
379#define VTD_INV_DESC_CC_G (3ULL << 4)
380#define VTD_INV_DESC_CC_GLOBAL (1ULL << 4)
381#define VTD_INV_DESC_CC_DOMAIN (2ULL << 4)
382#define VTD_INV_DESC_CC_DEVICE (3ULL << 4)
383#define VTD_INV_DESC_CC_DID(val) (((val) >> 16) & VTD_DOMAIN_ID_MASK)
384#define VTD_INV_DESC_CC_SID(val) (((val) >> 32) & 0xffffUL)
385#define VTD_INV_DESC_CC_FM(val) (((val) >> 48) & 3UL)
386#define VTD_INV_DESC_CC_RSVD 0xfffc00000000ffc0ULL
387
Le Tanb5a280c2014-08-16 13:55:44 +0800388/* Masks for IOTLB Invalidate Descriptor */
389#define VTD_INV_DESC_IOTLB_G (3ULL << 4)
390#define VTD_INV_DESC_IOTLB_GLOBAL (1ULL << 4)
391#define VTD_INV_DESC_IOTLB_DOMAIN (2ULL << 4)
392#define VTD_INV_DESC_IOTLB_PAGE (3ULL << 4)
393#define VTD_INV_DESC_IOTLB_DID(val) (((val) >> 16) & VTD_DOMAIN_ID_MASK)
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500394#define VTD_INV_DESC_IOTLB_ADDR(val) ((val) & ~0xfffULL)
Le Tanb5a280c2014-08-16 13:55:44 +0800395#define VTD_INV_DESC_IOTLB_AM(val) ((val) & 0x3fULL)
396#define VTD_INV_DESC_IOTLB_RSVD_LO 0xffffffff0000ff00ULL
397#define VTD_INV_DESC_IOTLB_RSVD_HI 0xf80ULL
Jason Wang1b2b1232022-10-28 14:14:36 +0800398#define VTD_INV_DESC_IOTLB_PASID_PASID (2ULL << 4)
399#define VTD_INV_DESC_IOTLB_PASID_PAGE (3ULL << 4)
400#define VTD_INV_DESC_IOTLB_PASID(val) (((val) >> 32) & VTD_PASID_ID_MASK)
401#define VTD_INV_DESC_IOTLB_PASID_RSVD_LO 0xfff00000000001c0ULL
402#define VTD_INV_DESC_IOTLB_PASID_RSVD_HI 0xf80ULL
Le Tanb5a280c2014-08-16 13:55:44 +0800403
Jason Wang554f5e12016-12-30 18:09:14 +0800404/* Mask for Device IOTLB Invalidate Descriptor */
405#define VTD_INV_DESC_DEVICE_IOTLB_ADDR(val) ((val) & 0xfffffffffffff000ULL)
406#define VTD_INV_DESC_DEVICE_IOTLB_SIZE(val) ((val) & 0x1)
407#define VTD_INV_DESC_DEVICE_IOTLB_SID(val) (((val) >> 32) & 0xFFFFULL)
408#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI 0xffeULL
409#define VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO 0xffff0000ffe0fff8
410
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500411/* Rsvd field masks for spte */
Jason Wang0192d662021-11-29 11:36:18 +0800412#define VTD_SPTE_SNP 0x800ULL
413
Qi, Yadonge48929c2019-11-25 08:33:21 +0800414#define VTD_SPTE_PAGE_L1_RSVD_MASK(aw, dt_supported) \
415 dt_supported ? \
416 (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500417 (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
418#define VTD_SPTE_PAGE_L2_RSVD_MASK(aw) \
419 (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
420#define VTD_SPTE_PAGE_L3_RSVD_MASK(aw) \
421 (0x800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
422#define VTD_SPTE_PAGE_L4_RSVD_MASK(aw) \
423 (0x880ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
Qi, Yadongce586f32019-11-25 08:33:20 +0800424
Qi, Yadonge48929c2019-11-25 08:33:21 +0800425#define VTD_SPTE_LPAGE_L2_RSVD_MASK(aw, dt_supported) \
426 dt_supported ? \
427 (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500428 (0x1ff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
Qi, Yadonge48929c2019-11-25 08:33:21 +0800429#define VTD_SPTE_LPAGE_L3_RSVD_MASK(aw, dt_supported) \
430 dt_supported ? \
431 (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM | VTD_SL_TM)) : \
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500432 (0x3ffff800ULL | ~(VTD_HAW_MASK(aw) | VTD_SL_IGN_COM))
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500433
Le Tanb5a280c2014-08-16 13:55:44 +0800434/* Information about page-selective IOTLB invalidate */
435struct VTDIOTLBPageInvInfo {
436 uint16_t domain_id;
Jason Wang1b2b1232022-10-28 14:14:36 +0800437 uint32_t pasid;
Jason Wangd66b9692016-01-14 00:47:24 -0500438 uint64_t addr;
Le Tanb5a280c2014-08-16 13:55:44 +0800439 uint8_t mask;
440};
441typedef struct VTDIOTLBPageInvInfo VTDIOTLBPageInvInfo;
442
Le Tan1da12ec2014-08-16 13:55:38 +0800443/* Pagesize of VTD paging structures, including root and context tables */
444#define VTD_PAGE_SHIFT 12
445#define VTD_PAGE_SIZE (1ULL << VTD_PAGE_SHIFT)
446
447#define VTD_PAGE_SHIFT_4K 12
448#define VTD_PAGE_MASK_4K (~((1ULL << VTD_PAGE_SHIFT_4K) - 1))
449#define VTD_PAGE_SHIFT_2M 21
450#define VTD_PAGE_MASK_2M (~((1ULL << VTD_PAGE_SHIFT_2M) - 1))
451#define VTD_PAGE_SHIFT_1G 30
452#define VTD_PAGE_MASK_1G (~((1ULL << VTD_PAGE_SHIFT_1G) - 1))
453
454struct VTDRootEntry {
Liu, Yi Lfb43cf72019-03-05 10:34:53 +0800455 uint64_t lo;
456 uint64_t hi;
Le Tan1da12ec2014-08-16 13:55:38 +0800457};
458typedef struct VTDRootEntry VTDRootEntry;
459
460/* Masks for struct VTDRootEntry */
461#define VTD_ROOT_ENTRY_P 1ULL
462#define VTD_ROOT_ENTRY_CTP (~0xfffULL)
463
464#define VTD_ROOT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(VTDRootEntry))
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500465#define VTD_ROOT_ENTRY_RSVD(aw) (0xffeULL | ~VTD_HAW_MASK(aw))
Le Tan1da12ec2014-08-16 13:55:38 +0800466
Liu, Yi Lfb43cf72019-03-05 10:34:53 +0800467#define VTD_DEVFN_CHECK_MASK 0x80
468
Le Tan1da12ec2014-08-16 13:55:38 +0800469/* Masks for struct VTDContextEntry */
470/* lo */
471#define VTD_CONTEXT_ENTRY_P (1ULL << 0)
472#define VTD_CONTEXT_ENTRY_FPD (1ULL << 1) /* Fault Processing Disable */
473#define VTD_CONTEXT_ENTRY_TT (3ULL << 2) /* Translation Type */
474#define VTD_CONTEXT_TT_MULTI_LEVEL 0
Jason Wang554f5e12016-12-30 18:09:14 +0800475#define VTD_CONTEXT_TT_DEV_IOTLB (1ULL << 2)
476#define VTD_CONTEXT_TT_PASS_THROUGH (2ULL << 2)
Le Tan1da12ec2014-08-16 13:55:38 +0800477/* Second Level Page Translation Pointer*/
478#define VTD_CONTEXT_ENTRY_SLPTPTR (~0xfffULL)
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500479#define VTD_CONTEXT_ENTRY_RSVD_LO(aw) (0xff0ULL | ~VTD_HAW_MASK(aw))
Le Tan1da12ec2014-08-16 13:55:38 +0800480/* hi */
481#define VTD_CONTEXT_ENTRY_AW 7ULL /* Adjusted guest-address-width */
Le Tanb5a280c2014-08-16 13:55:44 +0800482#define VTD_CONTEXT_ENTRY_DID(val) (((val) >> 8) & VTD_DOMAIN_ID_MASK)
Le Tan1da12ec2014-08-16 13:55:38 +0800483#define VTD_CONTEXT_ENTRY_RSVD_HI 0xffffffffff000080ULL
484
485#define VTD_CONTEXT_ENTRY_NR (VTD_PAGE_SIZE / sizeof(VTDContextEntry))
486
Liu, Yi Lfb43cf72019-03-05 10:34:53 +0800487#define VTD_CTX_ENTRY_LEGACY_SIZE 16
488#define VTD_CTX_ENTRY_SCALABLE_SIZE 32
489
490#define VTD_SM_CONTEXT_ENTRY_RID2PASID_MASK 0xfffff
491#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL0(aw) (0x1e0ULL | ~VTD_HAW_MASK(aw))
492#define VTD_SM_CONTEXT_ENTRY_RSVD_VAL1 0xffffffffffe00000ULL
493
494/* PASID Table Related Definitions */
495#define VTD_PASID_DIR_BASE_ADDR_MASK (~0xfffULL)
496#define VTD_PASID_TABLE_BASE_ADDR_MASK (~0xfffULL)
497#define VTD_PASID_DIR_ENTRY_SIZE 8
498#define VTD_PASID_ENTRY_SIZE 64
499#define VTD_PASID_DIR_BITS_MASK (0x3fffULL)
500#define VTD_PASID_DIR_INDEX(pasid) (((pasid) >> 6) & VTD_PASID_DIR_BITS_MASK)
501#define VTD_PASID_DIR_FPD (1ULL << 1) /* Fault Processing Disable */
502#define VTD_PASID_TABLE_BITS_MASK (0x3fULL)
503#define VTD_PASID_TABLE_INDEX(pasid) ((pasid) & VTD_PASID_TABLE_BITS_MASK)
504#define VTD_PASID_ENTRY_FPD (1ULL << 1) /* Fault Processing Disable */
505
506/* PASID Granular Translation Type Mask */
Liu Yi L56fc1e62020-01-03 21:28:06 +0800507#define VTD_PASID_ENTRY_P 1ULL
Liu, Yi Lfb43cf72019-03-05 10:34:53 +0800508#define VTD_SM_PASID_ENTRY_PGTT (7ULL << 6)
509#define VTD_SM_PASID_ENTRY_FLT (1ULL << 6)
510#define VTD_SM_PASID_ENTRY_SLT (2ULL << 6)
511#define VTD_SM_PASID_ENTRY_NESTED (3ULL << 6)
512#define VTD_SM_PASID_ENTRY_PT (4ULL << 6)
513
514#define VTD_SM_PASID_ENTRY_AW 7ULL /* Adjusted guest-address-width */
515#define VTD_SM_PASID_ENTRY_DID(val) ((val) & VTD_DOMAIN_ID_MASK)
516
517/* Second Level Page Translation Pointer*/
518#define VTD_SM_PASID_ENTRY_SLPTPTR (~0xfffULL)
519
Le Tan1da12ec2014-08-16 13:55:38 +0800520/* Paging Structure common */
521#define VTD_SL_PT_PAGE_SIZE_MASK (1ULL << 7)
522/* Bits to decide the offset for each level */
523#define VTD_SL_LEVEL_BITS 9
524
525/* Second Level Paging Structure */
526#define VTD_SL_PML4_LEVEL 4
527#define VTD_SL_PDP_LEVEL 3
528#define VTD_SL_PD_LEVEL 2
529#define VTD_SL_PT_LEVEL 1
530#define VTD_SL_PT_ENTRY_NR 512
531
532/* Masks for Second Level Paging Entry */
533#define VTD_SL_RW_MASK 3ULL
534#define VTD_SL_R 1ULL
535#define VTD_SL_W (1ULL << 1)
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500536#define VTD_SL_PT_BASE_ADDR_MASK(aw) (~(VTD_PAGE_SIZE - 1) & VTD_HAW_MASK(aw))
Le Tan1da12ec2014-08-16 13:55:38 +0800537#define VTD_SL_IGN_COM 0xbff0000000000000ULL
Qi, Yadonge48929c2019-11-25 08:33:21 +0800538#define VTD_SL_TM (1ULL << 62)
Le Tan1da12ec2014-08-16 13:55:38 +0800539
540#endif