blob: 0a8cd4e9ccf6c874b56f828bdc892a1ea016bb53 [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
Peter Maydellb6a0aa02016-01-26 18:17:03 +000022#include "qemu/osdep.h"
Peter Xu4684a202016-07-14 13:56:36 +080023#include "qemu/error-report.h"
Radim Krčmář6333e932016-10-10 17:28:45 +020024#include "qapi/error.h"
Le Tan1da12ec2014-08-16 13:55:38 +080025#include "hw/sysbus.h"
26#include "exec/address-spaces.h"
27#include "intel_iommu_internal.h"
Knut Omang7df953b2015-10-04 15:48:50 +020028#include "hw/pci/pci.h"
Alex Williamson3cb3b152016-06-30 13:00:24 -060029#include "hw/pci/pci_bus.h"
Marcel Apfelbaum621d9832016-06-27 18:38:34 +030030#include "hw/i386/pc.h"
Feng Wudea651a2016-09-22 00:12:17 +080031#include "hw/i386/apic-msidef.h"
Peter Xu04af0e12016-07-14 13:56:11 +080032#include "hw/boards.h"
33#include "hw/i386/x86-iommu.h"
Peter Xucb135f52016-07-14 13:56:23 +080034#include "hw/pci-host/q35.h"
Peter Xu4684a202016-07-14 13:56:36 +080035#include "sysemu/kvm.h"
Radim Krčmář32946012016-10-10 17:28:44 +020036#include "hw/i386/apic_internal.h"
Radim Krčmářfb506e72016-10-10 17:28:47 +020037#include "kvm_i386.h"
Peter Xubc535e52017-02-07 16:28:09 +080038#include "trace.h"
Le Tan1da12ec2014-08-16 13:55:38 +080039
Le Tan1da12ec2014-08-16 13:55:38 +080040static void vtd_define_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val,
41 uint64_t wmask, uint64_t w1cmask)
42{
43 stq_le_p(&s->csr[addr], val);
44 stq_le_p(&s->wmask[addr], wmask);
45 stq_le_p(&s->w1cmask[addr], w1cmask);
46}
47
48static void vtd_define_quad_wo(IntelIOMMUState *s, hwaddr addr, uint64_t mask)
49{
50 stq_le_p(&s->womask[addr], mask);
51}
52
53static void vtd_define_long(IntelIOMMUState *s, hwaddr addr, uint32_t val,
54 uint32_t wmask, uint32_t w1cmask)
55{
56 stl_le_p(&s->csr[addr], val);
57 stl_le_p(&s->wmask[addr], wmask);
58 stl_le_p(&s->w1cmask[addr], w1cmask);
59}
60
61static void vtd_define_long_wo(IntelIOMMUState *s, hwaddr addr, uint32_t mask)
62{
63 stl_le_p(&s->womask[addr], mask);
64}
65
66/* "External" get/set operations */
67static void vtd_set_quad(IntelIOMMUState *s, hwaddr addr, uint64_t val)
68{
69 uint64_t oldval = ldq_le_p(&s->csr[addr]);
70 uint64_t wmask = ldq_le_p(&s->wmask[addr]);
71 uint64_t w1cmask = ldq_le_p(&s->w1cmask[addr]);
72 stq_le_p(&s->csr[addr],
73 ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
74}
75
76static void vtd_set_long(IntelIOMMUState *s, hwaddr addr, uint32_t val)
77{
78 uint32_t oldval = ldl_le_p(&s->csr[addr]);
79 uint32_t wmask = ldl_le_p(&s->wmask[addr]);
80 uint32_t w1cmask = ldl_le_p(&s->w1cmask[addr]);
81 stl_le_p(&s->csr[addr],
82 ((oldval & ~wmask) | (val & wmask)) & ~(w1cmask & val));
83}
84
85static uint64_t vtd_get_quad(IntelIOMMUState *s, hwaddr addr)
86{
87 uint64_t val = ldq_le_p(&s->csr[addr]);
88 uint64_t womask = ldq_le_p(&s->womask[addr]);
89 return val & ~womask;
90}
91
92static uint32_t vtd_get_long(IntelIOMMUState *s, hwaddr addr)
93{
94 uint32_t val = ldl_le_p(&s->csr[addr]);
95 uint32_t womask = ldl_le_p(&s->womask[addr]);
96 return val & ~womask;
97}
98
99/* "Internal" get/set operations */
100static uint64_t vtd_get_quad_raw(IntelIOMMUState *s, hwaddr addr)
101{
102 return ldq_le_p(&s->csr[addr]);
103}
104
105static uint32_t vtd_get_long_raw(IntelIOMMUState *s, hwaddr addr)
106{
107 return ldl_le_p(&s->csr[addr]);
108}
109
110static void vtd_set_quad_raw(IntelIOMMUState *s, hwaddr addr, uint64_t val)
111{
112 stq_le_p(&s->csr[addr], val);
113}
114
115static uint32_t vtd_set_clear_mask_long(IntelIOMMUState *s, hwaddr addr,
116 uint32_t clear, uint32_t mask)
117{
118 uint32_t new_val = (ldl_le_p(&s->csr[addr]) & ~clear) | mask;
119 stl_le_p(&s->csr[addr], new_val);
120 return new_val;
121}
122
123static uint64_t vtd_set_clear_mask_quad(IntelIOMMUState *s, hwaddr addr,
124 uint64_t clear, uint64_t mask)
125{
126 uint64_t new_val = (ldq_le_p(&s->csr[addr]) & ~clear) | mask;
127 stq_le_p(&s->csr[addr], new_val);
128 return new_val;
129}
130
Peter Xu1d9efa72018-05-18 15:25:11 +0800131static inline void vtd_iommu_lock(IntelIOMMUState *s)
132{
133 qemu_mutex_lock(&s->iommu_lock);
134}
135
136static inline void vtd_iommu_unlock(IntelIOMMUState *s)
137{
138 qemu_mutex_unlock(&s->iommu_lock);
139}
140
Peter Xu4f8a62a2018-05-18 15:25:12 +0800141/* Whether the address space needs to notify new mappings */
142static inline gboolean vtd_as_has_map_notifier(VTDAddressSpace *as)
143{
144 return as->notifier_flags & IOMMU_NOTIFIER_MAP;
145}
146
Le Tanb5a280c2014-08-16 13:55:44 +0800147/* GHashTable functions */
148static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
149{
150 return *((const uint64_t *)v1) == *((const uint64_t *)v2);
151}
152
153static guint vtd_uint64_hash(gconstpointer v)
154{
155 return (guint)*(const uint64_t *)v;
156}
157
158static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
159 gpointer user_data)
160{
161 VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
162 uint16_t domain_id = *(uint16_t *)user_data;
163 return entry->domain_id == domain_id;
164}
165
Jason Wangd66b9692016-01-14 00:47:24 -0500166/* The shift of an addr for a certain level of paging structure */
167static inline uint32_t vtd_slpt_level_shift(uint32_t level)
168{
Peter Xu7e583262017-02-07 16:28:11 +0800169 assert(level != 0);
Jason Wangd66b9692016-01-14 00:47:24 -0500170 return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_SL_LEVEL_BITS;
171}
172
173static inline uint64_t vtd_slpt_level_page_mask(uint32_t level)
174{
175 return ~((1ULL << vtd_slpt_level_shift(level)) - 1);
176}
177
Le Tanb5a280c2014-08-16 13:55:44 +0800178static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
179 gpointer user_data)
180{
181 VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
182 VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
Jason Wangd66b9692016-01-14 00:47:24 -0500183 uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
184 uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
Le Tanb5a280c2014-08-16 13:55:44 +0800185 return (entry->domain_id == info->domain_id) &&
Jason Wangd66b9692016-01-14 00:47:24 -0500186 (((entry->gfn & info->mask) == gfn) ||
187 (entry->gfn == gfn_tlb));
Le Tanb5a280c2014-08-16 13:55:44 +0800188}
189
Le Tand92fa2d2014-08-16 13:55:43 +0800190/* Reset all the gen of VTDAddressSpace to zero and set the gen of
Peter Xu1d9efa72018-05-18 15:25:11 +0800191 * IntelIOMMUState to 1. Must be called with IOMMU lock held.
Le Tand92fa2d2014-08-16 13:55:43 +0800192 */
Peter Xu1d9efa72018-05-18 15:25:11 +0800193static void vtd_reset_context_cache_locked(IntelIOMMUState *s)
Le Tand92fa2d2014-08-16 13:55:43 +0800194{
Le Tand92fa2d2014-08-16 13:55:43 +0800195 VTDAddressSpace *vtd_as;
Knut Omang7df953b2015-10-04 15:48:50 +0200196 VTDBus *vtd_bus;
197 GHashTableIter bus_it;
Le Tand92fa2d2014-08-16 13:55:43 +0800198 uint32_t devfn_it;
199
Peter Xu7feb51b2017-06-09 21:53:27 +0800200 trace_vtd_context_cache_reset();
201
Knut Omang7df953b2015-10-04 15:48:50 +0200202 g_hash_table_iter_init(&bus_it, s->vtd_as_by_busptr);
203
Knut Omang7df953b2015-10-04 15:48:50 +0200204 while (g_hash_table_iter_next (&bus_it, NULL, (void**)&vtd_bus)) {
Peter Xubf33cc72017-12-08 12:26:53 +0800205 for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) {
Knut Omang7df953b2015-10-04 15:48:50 +0200206 vtd_as = vtd_bus->dev_as[devfn_it];
Le Tand92fa2d2014-08-16 13:55:43 +0800207 if (!vtd_as) {
208 continue;
209 }
210 vtd_as->context_cache_entry.context_cache_gen = 0;
211 }
212 }
213 s->context_cache_gen = 1;
214}
215
Peter Xu1d9efa72018-05-18 15:25:11 +0800216/* Must be called with IOMMU lock held. */
217static void vtd_reset_iotlb_locked(IntelIOMMUState *s)
Le Tanb5a280c2014-08-16 13:55:44 +0800218{
219 assert(s->iotlb);
220 g_hash_table_remove_all(s->iotlb);
221}
222
Peter Xu1d9efa72018-05-18 15:25:11 +0800223static void vtd_reset_iotlb(IntelIOMMUState *s)
224{
225 vtd_iommu_lock(s);
226 vtd_reset_iotlb_locked(s);
227 vtd_iommu_unlock(s);
228}
229
Jason Wangbacabb02016-11-03 09:22:23 +0800230static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id,
Jason Wangd66b9692016-01-14 00:47:24 -0500231 uint32_t level)
232{
233 return gfn | ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT) |
234 ((uint64_t)(level) << VTD_IOTLB_LVL_SHIFT);
235}
236
237static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
238{
239 return (addr & vtd_slpt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K;
240}
241
Peter Xu1d9efa72018-05-18 15:25:11 +0800242/* Must be called with IOMMU lock held */
Le Tanb5a280c2014-08-16 13:55:44 +0800243static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
244 hwaddr addr)
245{
Jason Wangd66b9692016-01-14 00:47:24 -0500246 VTDIOTLBEntry *entry;
Le Tanb5a280c2014-08-16 13:55:44 +0800247 uint64_t key;
Jason Wangd66b9692016-01-14 00:47:24 -0500248 int level;
Le Tanb5a280c2014-08-16 13:55:44 +0800249
Jason Wangd66b9692016-01-14 00:47:24 -0500250 for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) {
251 key = vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level),
252 source_id, level);
253 entry = g_hash_table_lookup(s->iotlb, &key);
254 if (entry) {
255 goto out;
256 }
257 }
Le Tanb5a280c2014-08-16 13:55:44 +0800258
Jason Wangd66b9692016-01-14 00:47:24 -0500259out:
260 return entry;
Le Tanb5a280c2014-08-16 13:55:44 +0800261}
262
Peter Xu1d9efa72018-05-18 15:25:11 +0800263/* Must be with IOMMU lock held */
Le Tanb5a280c2014-08-16 13:55:44 +0800264static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
265 uint16_t domain_id, hwaddr addr, uint64_t slpte,
Peter Xu07f7b732017-07-17 17:02:30 +0800266 uint8_t access_flags, uint32_t level)
Le Tanb5a280c2014-08-16 13:55:44 +0800267{
268 VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
269 uint64_t *key = g_malloc(sizeof(*key));
Jason Wangd66b9692016-01-14 00:47:24 -0500270 uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
Le Tanb5a280c2014-08-16 13:55:44 +0800271
Peter Xu6c441e12017-02-07 16:28:10 +0800272 trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
Le Tanb5a280c2014-08-16 13:55:44 +0800273 if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
Peter Xu6c441e12017-02-07 16:28:10 +0800274 trace_vtd_iotlb_reset("iotlb exceeds size limit");
Peter Xu1d9efa72018-05-18 15:25:11 +0800275 vtd_reset_iotlb_locked(s);
Le Tanb5a280c2014-08-16 13:55:44 +0800276 }
277
278 entry->gfn = gfn;
279 entry->domain_id = domain_id;
280 entry->slpte = slpte;
Peter Xu07f7b732017-07-17 17:02:30 +0800281 entry->access_flags = access_flags;
Jason Wangd66b9692016-01-14 00:47:24 -0500282 entry->mask = vtd_slpt_level_page_mask(level);
283 *key = vtd_get_iotlb_key(gfn, source_id, level);
Le Tanb5a280c2014-08-16 13:55:44 +0800284 g_hash_table_replace(s->iotlb, key, entry);
285}
286
Le Tan1da12ec2014-08-16 13:55:38 +0800287/* Given the reg addr of both the message data and address, generate an
288 * interrupt via MSI.
289 */
290static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg,
291 hwaddr mesg_data_reg)
292{
Radim Krčmář32946012016-10-10 17:28:44 +0200293 MSIMessage msi;
Le Tan1da12ec2014-08-16 13:55:38 +0800294
295 assert(mesg_data_reg < DMAR_REG_SIZE);
296 assert(mesg_addr_reg < DMAR_REG_SIZE);
297
Radim Krčmář32946012016-10-10 17:28:44 +0200298 msi.address = vtd_get_long_raw(s, mesg_addr_reg);
299 msi.data = vtd_get_long_raw(s, mesg_data_reg);
Le Tan1da12ec2014-08-16 13:55:38 +0800300
Peter Xu7feb51b2017-06-09 21:53:27 +0800301 trace_vtd_irq_generate(msi.address, msi.data);
302
Radim Krčmář32946012016-10-10 17:28:44 +0200303 apic_get_class()->send_msi(&msi);
Le Tan1da12ec2014-08-16 13:55:38 +0800304}
305
306/* Generate a fault event to software via MSI if conditions are met.
307 * Notice that the value of FSTS_REG being passed to it should be the one
308 * before any update.
309 */
310static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts)
311{
312 if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO ||
313 pre_fsts & VTD_FSTS_IQE) {
Peter Xu7feb51b2017-06-09 21:53:27 +0800314 trace_vtd_err("There are previous interrupt conditions "
315 "to be serviced by software, fault event "
316 "is not generated.");
Le Tan1da12ec2014-08-16 13:55:38 +0800317 return;
318 }
319 vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP);
320 if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) {
Peter Xu7feb51b2017-06-09 21:53:27 +0800321 trace_vtd_err("Interrupt Mask set, irq is not generated.");
Le Tan1da12ec2014-08-16 13:55:38 +0800322 } else {
323 vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
324 vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
325 }
326}
327
328/* Check if the Fault (F) field of the Fault Recording Register referenced by
329 * @index is Set.
330 */
331static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index)
332{
333 /* Each reg is 128-bit */
334 hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
335 addr += 8; /* Access the high 64-bit half */
336
337 assert(index < DMAR_FRCD_REG_NR);
338
339 return vtd_get_quad_raw(s, addr) & VTD_FRCD_F;
340}
341
342/* Update the PPF field of Fault Status Register.
343 * Should be called whenever change the F field of any fault recording
344 * registers.
345 */
346static void vtd_update_fsts_ppf(IntelIOMMUState *s)
347{
348 uint32_t i;
349 uint32_t ppf_mask = 0;
350
351 for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
352 if (vtd_is_frcd_set(s, i)) {
353 ppf_mask = VTD_FSTS_PPF;
354 break;
355 }
356 }
357 vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask);
Peter Xu7feb51b2017-06-09 21:53:27 +0800358 trace_vtd_fsts_ppf(!!ppf_mask);
Le Tan1da12ec2014-08-16 13:55:38 +0800359}
360
361static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)
362{
363 /* Each reg is 128-bit */
364 hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
365 addr += 8; /* Access the high 64-bit half */
366
367 assert(index < DMAR_FRCD_REG_NR);
368
369 vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F);
370 vtd_update_fsts_ppf(s);
371}
372
373/* Must not update F field now, should be done later */
374static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
375 uint16_t source_id, hwaddr addr,
376 VTDFaultReason fault, bool is_write)
377{
378 uint64_t hi = 0, lo;
379 hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
380
381 assert(index < DMAR_FRCD_REG_NR);
382
383 lo = VTD_FRCD_FI(addr);
384 hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault);
385 if (!is_write) {
386 hi |= VTD_FRCD_T;
387 }
388 vtd_set_quad_raw(s, frcd_reg_addr, lo);
389 vtd_set_quad_raw(s, frcd_reg_addr + 8, hi);
Peter Xu7feb51b2017-06-09 21:53:27 +0800390
391 trace_vtd_frr_new(index, hi, lo);
Le Tan1da12ec2014-08-16 13:55:38 +0800392}
393
394/* Try to collapse multiple pending faults from the same requester */
395static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id)
396{
397 uint32_t i;
398 uint64_t frcd_reg;
399 hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */
400
401 for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
402 frcd_reg = vtd_get_quad_raw(s, addr);
Le Tan1da12ec2014-08-16 13:55:38 +0800403 if ((frcd_reg & VTD_FRCD_F) &&
404 ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) {
405 return true;
406 }
407 addr += 16; /* 128-bit for each */
408 }
409 return false;
410}
411
412/* Log and report an DMAR (address translation) fault to software */
413static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
414 hwaddr addr, VTDFaultReason fault,
415 bool is_write)
416{
417 uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
418
419 assert(fault < VTD_FR_MAX);
420
421 if (fault == VTD_FR_RESERVED_ERR) {
422 /* This is not a normal fault reason case. Drop it. */
423 return;
424 }
Peter Xu7feb51b2017-06-09 21:53:27 +0800425
426 trace_vtd_dmar_fault(source_id, fault, addr, is_write);
427
Le Tan1da12ec2014-08-16 13:55:38 +0800428 if (fsts_reg & VTD_FSTS_PFO) {
Peter Xu7feb51b2017-06-09 21:53:27 +0800429 trace_vtd_err("New fault is not recorded due to "
430 "Primary Fault Overflow.");
Le Tan1da12ec2014-08-16 13:55:38 +0800431 return;
432 }
Peter Xu7feb51b2017-06-09 21:53:27 +0800433
Le Tan1da12ec2014-08-16 13:55:38 +0800434 if (vtd_try_collapse_fault(s, source_id)) {
Peter Xu7feb51b2017-06-09 21:53:27 +0800435 trace_vtd_err("New fault is not recorded due to "
436 "compression of faults.");
Le Tan1da12ec2014-08-16 13:55:38 +0800437 return;
438 }
Peter Xu7feb51b2017-06-09 21:53:27 +0800439
Le Tan1da12ec2014-08-16 13:55:38 +0800440 if (vtd_is_frcd_set(s, s->next_frcd_reg)) {
Peter Xu7feb51b2017-06-09 21:53:27 +0800441 trace_vtd_err("Next Fault Recording Reg is used, "
442 "new fault is not recorded, set PFO field.");
Le Tan1da12ec2014-08-16 13:55:38 +0800443 vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO);
444 return;
445 }
446
447 vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, is_write);
448
449 if (fsts_reg & VTD_FSTS_PPF) {
Peter Xu7feb51b2017-06-09 21:53:27 +0800450 trace_vtd_err("There are pending faults already, "
451 "fault event is not generated.");
Le Tan1da12ec2014-08-16 13:55:38 +0800452 vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg);
453 s->next_frcd_reg++;
454 if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
455 s->next_frcd_reg = 0;
456 }
457 } else {
458 vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK,
459 VTD_FSTS_FRI(s->next_frcd_reg));
460 vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */
461 s->next_frcd_reg++;
462 if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
463 s->next_frcd_reg = 0;
464 }
465 /* This case actually cause the PPF to be Set.
466 * So generate fault event (interrupt).
467 */
468 vtd_generate_fault_event(s, fsts_reg);
469 }
470}
471
Le Taned7b8fb2014-08-16 13:55:42 +0800472/* Handle Invalidation Queue Errors of queued invalidation interface error
473 * conditions.
474 */
475static void vtd_handle_inv_queue_error(IntelIOMMUState *s)
476{
477 uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
478
479 vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE);
480 vtd_generate_fault_event(s, fsts_reg);
481}
482
483/* Set the IWC field and try to generate an invalidation completion interrupt */
484static void vtd_generate_completion_event(IntelIOMMUState *s)
485{
Le Taned7b8fb2014-08-16 13:55:42 +0800486 if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) {
Peter Xubc535e52017-02-07 16:28:09 +0800487 trace_vtd_inv_desc_wait_irq("One pending, skip current");
Le Taned7b8fb2014-08-16 13:55:42 +0800488 return;
489 }
490 vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC);
491 vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP);
492 if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) {
Peter Xubc535e52017-02-07 16:28:09 +0800493 trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, "
494 "new event not generated");
Le Taned7b8fb2014-08-16 13:55:42 +0800495 return;
496 } else {
497 /* Generate the interrupt event */
Peter Xubc535e52017-02-07 16:28:09 +0800498 trace_vtd_inv_desc_wait_irq("Generating complete event");
Le Taned7b8fb2014-08-16 13:55:42 +0800499 vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
500 vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
501 }
502}
503
Le Tan1da12ec2014-08-16 13:55:38 +0800504static inline bool vtd_root_entry_present(VTDRootEntry *root)
505{
506 return root->val & VTD_ROOT_ENTRY_P;
507}
508
509static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index,
510 VTDRootEntry *re)
511{
512 dma_addr_t addr;
513
514 addr = s->root + index * sizeof(*re);
515 if (dma_memory_read(&address_space_memory, addr, re, sizeof(*re))) {
Peter Xu6c441e12017-02-07 16:28:10 +0800516 trace_vtd_re_invalid(re->rsvd, re->val);
Le Tan1da12ec2014-08-16 13:55:38 +0800517 re->val = 0;
518 return -VTD_FR_ROOT_TABLE_INV;
519 }
520 re->val = le64_to_cpu(re->val);
521 return 0;
522}
523
Peter Xu8f7d7162017-05-19 11:19:43 +0800524static inline bool vtd_ce_present(VTDContextEntry *context)
Le Tan1da12ec2014-08-16 13:55:38 +0800525{
526 return context->lo & VTD_CONTEXT_ENTRY_P;
527}
528
529static int vtd_get_context_entry_from_root(VTDRootEntry *root, uint8_t index,
530 VTDContextEntry *ce)
531{
532 dma_addr_t addr;
533
Peter Xu6c441e12017-02-07 16:28:10 +0800534 /* we have checked that root entry is present */
Le Tan1da12ec2014-08-16 13:55:38 +0800535 addr = (root->val & VTD_ROOT_ENTRY_CTP) + index * sizeof(*ce);
536 if (dma_memory_read(&address_space_memory, addr, ce, sizeof(*ce))) {
Peter Xu6c441e12017-02-07 16:28:10 +0800537 trace_vtd_re_invalid(root->rsvd, root->val);
Le Tan1da12ec2014-08-16 13:55:38 +0800538 return -VTD_FR_CONTEXT_TABLE_INV;
539 }
540 ce->lo = le64_to_cpu(ce->lo);
541 ce->hi = le64_to_cpu(ce->hi);
542 return 0;
543}
544
Peter Xu8f7d7162017-05-19 11:19:43 +0800545static inline dma_addr_t vtd_ce_get_slpt_base(VTDContextEntry *ce)
Le Tan1da12ec2014-08-16 13:55:38 +0800546{
547 return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR;
548}
549
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500550static inline uint64_t vtd_get_slpte_addr(uint64_t slpte, uint8_t aw)
Le Tan1da12ec2014-08-16 13:55:38 +0800551{
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500552 return slpte & VTD_SL_PT_BASE_ADDR_MASK(aw);
Le Tan1da12ec2014-08-16 13:55:38 +0800553}
554
555/* Whether the pte indicates the address of the page frame */
556static inline bool vtd_is_last_slpte(uint64_t slpte, uint32_t level)
557{
558 return level == VTD_SL_PT_LEVEL || (slpte & VTD_SL_PT_PAGE_SIZE_MASK);
559}
560
561/* Get the content of a spte located in @base_addr[@index] */
562static uint64_t vtd_get_slpte(dma_addr_t base_addr, uint32_t index)
563{
564 uint64_t slpte;
565
566 assert(index < VTD_SL_PT_ENTRY_NR);
567
568 if (dma_memory_read(&address_space_memory,
569 base_addr + index * sizeof(slpte), &slpte,
570 sizeof(slpte))) {
571 slpte = (uint64_t)-1;
572 return slpte;
573 }
574 slpte = le64_to_cpu(slpte);
575 return slpte;
576}
577
Peter Xu6e905562017-02-07 16:28:08 +0800578/* Given an iova and the level of paging structure, return the offset
579 * of current level.
Le Tan1da12ec2014-08-16 13:55:38 +0800580 */
Peter Xu6e905562017-02-07 16:28:08 +0800581static inline uint32_t vtd_iova_level_offset(uint64_t iova, uint32_t level)
Le Tan1da12ec2014-08-16 13:55:38 +0800582{
Peter Xu6e905562017-02-07 16:28:08 +0800583 return (iova >> vtd_slpt_level_shift(level)) &
Le Tan1da12ec2014-08-16 13:55:38 +0800584 ((1ULL << VTD_SL_LEVEL_BITS) - 1);
585}
586
587/* Check Capability Register to see if the @level of page-table is supported */
588static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)
589{
590 return VTD_CAP_SAGAW_MASK & s->cap &
591 (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT));
592}
593
594/* Get the page-table level that hardware should use for the second-level
595 * page-table walk from the Address Width field of context-entry.
596 */
Peter Xu8f7d7162017-05-19 11:19:43 +0800597static inline uint32_t vtd_ce_get_level(VTDContextEntry *ce)
Le Tan1da12ec2014-08-16 13:55:38 +0800598{
599 return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW);
600}
601
Peter Xu8f7d7162017-05-19 11:19:43 +0800602static inline uint32_t vtd_ce_get_agaw(VTDContextEntry *ce)
Le Tan1da12ec2014-08-16 13:55:38 +0800603{
604 return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9;
605}
606
Peter Xu127ff5c2017-05-19 11:19:44 +0800607static inline uint32_t vtd_ce_get_type(VTDContextEntry *ce)
608{
609 return ce->lo & VTD_CONTEXT_ENTRY_TT;
610}
611
Peter Xuf80c9872017-05-19 11:19:46 +0800612/* Return true if check passed, otherwise false */
613static inline bool vtd_ce_type_check(X86IOMMUState *x86_iommu,
614 VTDContextEntry *ce)
615{
616 switch (vtd_ce_get_type(ce)) {
617 case VTD_CONTEXT_TT_MULTI_LEVEL:
618 /* Always supported */
619 break;
620 case VTD_CONTEXT_TT_DEV_IOTLB:
621 if (!x86_iommu->dt_supported) {
622 return false;
623 }
624 break;
Peter Xudbaabb22017-05-19 11:19:47 +0800625 case VTD_CONTEXT_TT_PASS_THROUGH:
626 if (!x86_iommu->pt_supported) {
627 return false;
628 }
629 break;
Peter Xuf80c9872017-05-19 11:19:46 +0800630 default:
631 /* Unknwon type */
632 return false;
633 }
634 return true;
635}
636
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500637static inline uint64_t vtd_iova_limit(VTDContextEntry *ce, uint8_t aw)
Peter Xuf06a6962017-04-07 18:59:13 +0800638{
Peter Xu8f7d7162017-05-19 11:19:43 +0800639 uint32_t ce_agaw = vtd_ce_get_agaw(ce);
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500640 return 1ULL << MIN(ce_agaw, aw);
Peter Xuf06a6962017-04-07 18:59:13 +0800641}
642
643/* Return true if IOVA passes range check, otherwise false. */
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500644static inline bool vtd_iova_range_check(uint64_t iova, VTDContextEntry *ce,
645 uint8_t aw)
Peter Xuf06a6962017-04-07 18:59:13 +0800646{
647 /*
648 * Check if @iova is above 2^X-1, where X is the minimum of MGAW
649 * in CAP_REG and AW in context-entry.
650 */
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500651 return !(iova & ~(vtd_iova_limit(ce, aw) - 1));
Peter Xuf06a6962017-04-07 18:59:13 +0800652}
653
Prasad Singamsetty92e5d852017-11-14 18:13:49 -0500654/*
655 * Rsvd field masks for spte:
656 * Index [1] to [4] 4k pages
657 * Index [5] to [8] large pages
658 */
659static uint64_t vtd_paging_entry_rsvd_field[9];
Le Tan1da12ec2014-08-16 13:55:38 +0800660
661static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)
662{
663 if (slpte & VTD_SL_PT_PAGE_SIZE_MASK) {
664 /* Maybe large page */
665 return slpte & vtd_paging_entry_rsvd_field[level + 4];
666 } else {
667 return slpte & vtd_paging_entry_rsvd_field[level];
668 }
669}
670
Peter Xudbaabb22017-05-19 11:19:47 +0800671/* Find the VTD address space associated with a given bus number */
672static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)
673{
674 VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num];
675 if (!vtd_bus) {
676 /*
677 * Iterate over the registered buses to find the one which
678 * currently hold this bus number, and update the bus_num
679 * lookup table:
680 */
681 GHashTableIter iter;
682
683 g_hash_table_iter_init(&iter, s->vtd_as_by_busptr);
684 while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) {
685 if (pci_bus_num(vtd_bus->bus) == bus_num) {
686 s->vtd_as_by_bus_num[bus_num] = vtd_bus;
687 return vtd_bus;
688 }
689 }
690 }
691 return vtd_bus;
692}
693
Peter Xu6e905562017-02-07 16:28:08 +0800694/* Given the @iova, get relevant @slptep. @slpte_level will be the last level
Le Tan1da12ec2014-08-16 13:55:38 +0800695 * of the translation, can be used for deciding the size of large page.
696 */
Peter Xu6e905562017-02-07 16:28:08 +0800697static int vtd_iova_to_slpte(VTDContextEntry *ce, uint64_t iova, bool is_write,
698 uint64_t *slptep, uint32_t *slpte_level,
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500699 bool *reads, bool *writes, uint8_t aw_bits)
Le Tan1da12ec2014-08-16 13:55:38 +0800700{
Peter Xu8f7d7162017-05-19 11:19:43 +0800701 dma_addr_t addr = vtd_ce_get_slpt_base(ce);
702 uint32_t level = vtd_ce_get_level(ce);
Le Tan1da12ec2014-08-16 13:55:38 +0800703 uint32_t offset;
704 uint64_t slpte;
Le Tan1da12ec2014-08-16 13:55:38 +0800705 uint64_t access_right_check;
706
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500707 if (!vtd_iova_range_check(iova, ce, aw_bits)) {
Peter Xu7feb51b2017-06-09 21:53:27 +0800708 trace_vtd_err_dmar_iova_overflow(iova);
Le Tan1da12ec2014-08-16 13:55:38 +0800709 return -VTD_FR_ADDR_BEYOND_MGAW;
710 }
711
712 /* FIXME: what is the Atomics request here? */
713 access_right_check = is_write ? VTD_SL_W : VTD_SL_R;
714
715 while (true) {
Peter Xu6e905562017-02-07 16:28:08 +0800716 offset = vtd_iova_level_offset(iova, level);
Le Tan1da12ec2014-08-16 13:55:38 +0800717 slpte = vtd_get_slpte(addr, offset);
718
719 if (slpte == (uint64_t)-1) {
Peter Xu7feb51b2017-06-09 21:53:27 +0800720 trace_vtd_err_dmar_slpte_read_error(iova, level);
Peter Xu8f7d7162017-05-19 11:19:43 +0800721 if (level == vtd_ce_get_level(ce)) {
Le Tan1da12ec2014-08-16 13:55:38 +0800722 /* Invalid programming of context-entry */
723 return -VTD_FR_CONTEXT_ENTRY_INV;
724 } else {
725 return -VTD_FR_PAGING_ENTRY_INV;
726 }
727 }
728 *reads = (*reads) && (slpte & VTD_SL_R);
729 *writes = (*writes) && (slpte & VTD_SL_W);
730 if (!(slpte & access_right_check)) {
Peter Xu7feb51b2017-06-09 21:53:27 +0800731 trace_vtd_err_dmar_slpte_perm_error(iova, level, slpte, is_write);
Le Tan1da12ec2014-08-16 13:55:38 +0800732 return is_write ? -VTD_FR_WRITE : -VTD_FR_READ;
733 }
734 if (vtd_slpte_nonzero_rsvd(slpte, level)) {
Peter Xu7feb51b2017-06-09 21:53:27 +0800735 trace_vtd_err_dmar_slpte_resv_error(iova, level, slpte);
Le Tan1da12ec2014-08-16 13:55:38 +0800736 return -VTD_FR_PAGING_ENTRY_RSVD;
737 }
738
739 if (vtd_is_last_slpte(slpte, level)) {
740 *slptep = slpte;
741 *slpte_level = level;
742 return 0;
743 }
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500744 addr = vtd_get_slpte_addr(slpte, aw_bits);
Le Tan1da12ec2014-08-16 13:55:38 +0800745 level--;
746 }
747}
748
Peter Xuf06a6962017-04-07 18:59:13 +0800749typedef int (*vtd_page_walk_hook)(IOMMUTLBEntry *entry, void *private);
750
Peter Xufe215b02018-05-18 15:25:13 +0800751/**
752 * Constant information used during page walking
753 *
754 * @hook_fn: hook func to be called when detected page
755 * @private: private data to be passed into hook func
756 * @notify_unmap: whether we should notify invalid entries
Peter Xu2f764fa2018-05-18 15:25:14 +0800757 * @as: VT-d address space of the device
Peter Xufe215b02018-05-18 15:25:13 +0800758 * @aw: maximum address width
Peter Xud118c062018-05-18 15:25:15 +0800759 * @domain: domain ID of the page walk
Peter Xufe215b02018-05-18 15:25:13 +0800760 */
761typedef struct {
Peter Xu2f764fa2018-05-18 15:25:14 +0800762 VTDAddressSpace *as;
Peter Xufe215b02018-05-18 15:25:13 +0800763 vtd_page_walk_hook hook_fn;
764 void *private;
765 bool notify_unmap;
766 uint8_t aw;
Peter Xud118c062018-05-18 15:25:15 +0800767 uint16_t domain_id;
Peter Xufe215b02018-05-18 15:25:13 +0800768} vtd_page_walk_info;
769
Peter Xud118c062018-05-18 15:25:15 +0800770static int vtd_page_walk_one(IOMMUTLBEntry *entry, vtd_page_walk_info *info)
Peter Xu36d2d522018-05-18 15:25:09 +0800771{
Peter Xu63b88962018-05-18 15:25:17 +0800772 VTDAddressSpace *as = info->as;
Peter Xufe215b02018-05-18 15:25:13 +0800773 vtd_page_walk_hook hook_fn = info->hook_fn;
774 void *private = info->private;
Peter Xu63b88962018-05-18 15:25:17 +0800775 DMAMap target = {
776 .iova = entry->iova,
777 .size = entry->addr_mask,
778 .translated_addr = entry->translated_addr,
779 .perm = entry->perm,
780 };
781 DMAMap *mapped = iova_tree_find(as->iova_tree, &target);
782
783 if (entry->perm == IOMMU_NONE && !info->notify_unmap) {
784 trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
785 return 0;
786 }
Peter Xufe215b02018-05-18 15:25:13 +0800787
Peter Xu36d2d522018-05-18 15:25:09 +0800788 assert(hook_fn);
Peter Xu63b88962018-05-18 15:25:17 +0800789
790 /* Update local IOVA mapped ranges */
791 if (entry->perm) {
792 if (mapped) {
793 /* If it's exactly the same translation, skip */
794 if (!memcmp(mapped, &target, sizeof(target))) {
795 trace_vtd_page_walk_one_skip_map(entry->iova, entry->addr_mask,
796 entry->translated_addr);
797 return 0;
798 } else {
799 /*
800 * Translation changed. Normally this should not
801 * happen, but it can happen when with buggy guest
802 * OSes. Note that there will be a small window that
803 * we don't have map at all. But that's the best
804 * effort we can do. The ideal way to emulate this is
805 * atomically modify the PTE to follow what has
806 * changed, but we can't. One example is that vfio
807 * driver only has VFIO_IOMMU_[UN]MAP_DMA but no
808 * interface to modify a mapping (meanwhile it seems
809 * meaningless to even provide one). Anyway, let's
810 * mark this as a TODO in case one day we'll have
811 * a better solution.
812 */
813 IOMMUAccessFlags cache_perm = entry->perm;
814 int ret;
815
816 /* Emulate an UNMAP */
817 entry->perm = IOMMU_NONE;
818 trace_vtd_page_walk_one(info->domain_id,
819 entry->iova,
820 entry->translated_addr,
821 entry->addr_mask,
822 entry->perm);
823 ret = hook_fn(entry, private);
824 if (ret) {
825 return ret;
826 }
827 /* Drop any existing mapping */
828 iova_tree_remove(as->iova_tree, &target);
829 /* Recover the correct permission */
830 entry->perm = cache_perm;
831 }
832 }
833 iova_tree_insert(as->iova_tree, &target);
834 } else {
835 if (!mapped) {
836 /* Skip since we didn't map this range at all */
837 trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
838 return 0;
839 }
840 iova_tree_remove(as->iova_tree, &target);
841 }
842
Peter Xud118c062018-05-18 15:25:15 +0800843 trace_vtd_page_walk_one(info->domain_id, entry->iova,
844 entry->translated_addr, entry->addr_mask,
845 entry->perm);
Peter Xu36d2d522018-05-18 15:25:09 +0800846 return hook_fn(entry, private);
847}
848
Peter Xuf06a6962017-04-07 18:59:13 +0800849/**
850 * vtd_page_walk_level - walk over specific level for IOVA range
851 *
852 * @addr: base GPA addr to start the walk
853 * @start: IOVA range start address
854 * @end: IOVA range end address (start <= addr < end)
Peter Xuf06a6962017-04-07 18:59:13 +0800855 * @read: whether parent level has read permission
856 * @write: whether parent level has write permission
Peter Xufe215b02018-05-18 15:25:13 +0800857 * @info: constant information for the page walk
Peter Xuf06a6962017-04-07 18:59:13 +0800858 */
859static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,
Peter Xufe215b02018-05-18 15:25:13 +0800860 uint64_t end, uint32_t level, bool read,
861 bool write, vtd_page_walk_info *info)
Peter Xuf06a6962017-04-07 18:59:13 +0800862{
863 bool read_cur, write_cur, entry_valid;
864 uint32_t offset;
865 uint64_t slpte;
866 uint64_t subpage_size, subpage_mask;
867 IOMMUTLBEntry entry;
868 uint64_t iova = start;
869 uint64_t iova_next;
870 int ret = 0;
871
872 trace_vtd_page_walk_level(addr, level, start, end);
873
874 subpage_size = 1ULL << vtd_slpt_level_shift(level);
875 subpage_mask = vtd_slpt_level_page_mask(level);
876
877 while (iova < end) {
878 iova_next = (iova & subpage_mask) + subpage_size;
879
880 offset = vtd_iova_level_offset(iova, level);
881 slpte = vtd_get_slpte(addr, offset);
882
883 if (slpte == (uint64_t)-1) {
884 trace_vtd_page_walk_skip_read(iova, iova_next);
885 goto next;
886 }
887
888 if (vtd_slpte_nonzero_rsvd(slpte, level)) {
889 trace_vtd_page_walk_skip_reserve(iova, iova_next);
890 goto next;
891 }
892
893 /* Permissions are stacked with parents' */
894 read_cur = read && (slpte & VTD_SL_R);
895 write_cur = write && (slpte & VTD_SL_W);
896
897 /*
898 * As long as we have either read/write permission, this is a
899 * valid entry. The rule works for both page entries and page
900 * table entries.
901 */
902 entry_valid = read_cur | write_cur;
903
Peter Xu63b88962018-05-18 15:25:17 +0800904 if (!vtd_is_last_slpte(slpte, level) && entry_valid) {
905 /*
906 * This is a valid PDE (or even bigger than PDE). We need
907 * to walk one further level.
908 */
Peter Xufe215b02018-05-18 15:25:13 +0800909 ret = vtd_page_walk_level(vtd_get_slpte_addr(slpte, info->aw),
910 iova, MIN(iova_next, end), level - 1,
911 read_cur, write_cur, info);
Peter Xu63b88962018-05-18 15:25:17 +0800912 } else {
913 /*
914 * This means we are either:
915 *
916 * (1) the real page entry (either 4K page, or huge page)
917 * (2) the whole range is invalid
918 *
919 * In either case, we send an IOTLB notification down.
920 */
921 entry.target_as = &address_space_memory;
922 entry.iova = iova & subpage_mask;
923 entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
924 entry.addr_mask = ~subpage_mask;
925 /* NOTE: this is only meaningful if entry_valid == true */
926 entry.translated_addr = vtd_get_slpte_addr(slpte, info->aw);
927 ret = vtd_page_walk_one(&entry, info);
928 }
929
930 if (ret < 0) {
931 return ret;
Peter Xuf06a6962017-04-07 18:59:13 +0800932 }
933
934next:
935 iova = iova_next;
936 }
937
938 return 0;
939}
940
941/**
942 * vtd_page_walk - walk specific IOVA range, and call the hook
943 *
944 * @ce: context entry to walk upon
945 * @start: IOVA address to start the walk
946 * @end: IOVA range end address (start <= addr < end)
Peter Xufe215b02018-05-18 15:25:13 +0800947 * @info: page walking information struct
Peter Xuf06a6962017-04-07 18:59:13 +0800948 */
949static int vtd_page_walk(VTDContextEntry *ce, uint64_t start, uint64_t end,
Peter Xufe215b02018-05-18 15:25:13 +0800950 vtd_page_walk_info *info)
Peter Xuf06a6962017-04-07 18:59:13 +0800951{
Peter Xu8f7d7162017-05-19 11:19:43 +0800952 dma_addr_t addr = vtd_ce_get_slpt_base(ce);
953 uint32_t level = vtd_ce_get_level(ce);
Peter Xuf06a6962017-04-07 18:59:13 +0800954
Peter Xufe215b02018-05-18 15:25:13 +0800955 if (!vtd_iova_range_check(start, ce, info->aw)) {
Peter Xuf06a6962017-04-07 18:59:13 +0800956 return -VTD_FR_ADDR_BEYOND_MGAW;
957 }
958
Peter Xufe215b02018-05-18 15:25:13 +0800959 if (!vtd_iova_range_check(end, ce, info->aw)) {
Peter Xuf06a6962017-04-07 18:59:13 +0800960 /* Fix end so that it reaches the maximum */
Peter Xufe215b02018-05-18 15:25:13 +0800961 end = vtd_iova_limit(ce, info->aw);
Peter Xuf06a6962017-04-07 18:59:13 +0800962 }
963
Peter Xufe215b02018-05-18 15:25:13 +0800964 return vtd_page_walk_level(addr, start, end, level, true, true, info);
Peter Xuf06a6962017-04-07 18:59:13 +0800965}
966
Le Tan1da12ec2014-08-16 13:55:38 +0800967/* Map a device to its corresponding domain (context-entry) */
968static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
969 uint8_t devfn, VTDContextEntry *ce)
970{
971 VTDRootEntry re;
972 int ret_fr;
Peter Xuf80c9872017-05-19 11:19:46 +0800973 X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
Le Tan1da12ec2014-08-16 13:55:38 +0800974
975 ret_fr = vtd_get_root_entry(s, bus_num, &re);
976 if (ret_fr) {
977 return ret_fr;
978 }
979
980 if (!vtd_root_entry_present(&re)) {
Peter Xu6c441e12017-02-07 16:28:10 +0800981 /* Not error - it's okay we don't have root entry. */
982 trace_vtd_re_not_present(bus_num);
Le Tan1da12ec2014-08-16 13:55:38 +0800983 return -VTD_FR_ROOT_ENTRY_P;
Peter Xuf80c9872017-05-19 11:19:46 +0800984 }
985
Prasad Singamsetty37f51382017-11-14 18:13:50 -0500986 if (re.rsvd || (re.val & VTD_ROOT_ENTRY_RSVD(s->aw_bits))) {
Peter Xu6c441e12017-02-07 16:28:10 +0800987 trace_vtd_re_invalid(re.rsvd, re.val);
Le Tan1da12ec2014-08-16 13:55:38 +0800988 return -VTD_FR_ROOT_ENTRY_RSVD;
989 }
990
991 ret_fr = vtd_get_context_entry_from_root(&re, devfn, ce);
992 if (ret_fr) {
993 return ret_fr;
994 }
995
Peter Xu8f7d7162017-05-19 11:19:43 +0800996 if (!vtd_ce_present(ce)) {
Peter Xu6c441e12017-02-07 16:28:10 +0800997 /* Not error - it's okay we don't have context entry. */
998 trace_vtd_ce_not_present(bus_num, devfn);
Le Tan1da12ec2014-08-16 13:55:38 +0800999 return -VTD_FR_CONTEXT_ENTRY_P;
Peter Xuf80c9872017-05-19 11:19:46 +08001000 }
1001
1002 if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) ||
Prasad Singamsetty37f51382017-11-14 18:13:50 -05001003 (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO(s->aw_bits))) {
Peter Xu6c441e12017-02-07 16:28:10 +08001004 trace_vtd_ce_invalid(ce->hi, ce->lo);
Le Tan1da12ec2014-08-16 13:55:38 +08001005 return -VTD_FR_CONTEXT_ENTRY_RSVD;
1006 }
Peter Xuf80c9872017-05-19 11:19:46 +08001007
Le Tan1da12ec2014-08-16 13:55:38 +08001008 /* Check if the programming of context-entry is valid */
Peter Xu8f7d7162017-05-19 11:19:43 +08001009 if (!vtd_is_level_supported(s, vtd_ce_get_level(ce))) {
Peter Xu6c441e12017-02-07 16:28:10 +08001010 trace_vtd_ce_invalid(ce->hi, ce->lo);
Le Tan1da12ec2014-08-16 13:55:38 +08001011 return -VTD_FR_CONTEXT_ENTRY_INV;
Le Tan1da12ec2014-08-16 13:55:38 +08001012 }
Peter Xuf80c9872017-05-19 11:19:46 +08001013
1014 /* Do translation type check */
1015 if (!vtd_ce_type_check(x86_iommu, ce)) {
1016 trace_vtd_ce_invalid(ce->hi, ce->lo);
1017 return -VTD_FR_CONTEXT_ENTRY_INV;
1018 }
1019
Le Tan1da12ec2014-08-16 13:55:38 +08001020 return 0;
1021}
1022
Peter Xu63b88962018-05-18 15:25:17 +08001023static int vtd_sync_shadow_page_hook(IOMMUTLBEntry *entry,
1024 void *private)
1025{
Peter Maydellcb1efcf2018-06-15 14:57:16 +01001026 memory_region_notify_iommu((IOMMUMemoryRegion *)private, 0, *entry);
Peter Xu63b88962018-05-18 15:25:17 +08001027 return 0;
1028}
1029
1030/* If context entry is NULL, we'll try to fetch it on our own. */
1031static int vtd_sync_shadow_page_table_range(VTDAddressSpace *vtd_as,
1032 VTDContextEntry *ce,
1033 hwaddr addr, hwaddr size)
1034{
1035 IntelIOMMUState *s = vtd_as->iommu_state;
1036 vtd_page_walk_info info = {
1037 .hook_fn = vtd_sync_shadow_page_hook,
1038 .private = (void *)&vtd_as->iommu,
1039 .notify_unmap = true,
1040 .aw = s->aw_bits,
1041 .as = vtd_as,
1042 };
1043 VTDContextEntry ce_cache;
1044 int ret;
1045
1046 if (ce) {
1047 /* If the caller provided context entry, use it */
1048 ce_cache = *ce;
1049 } else {
1050 /* If the caller didn't provide ce, try to fetch */
1051 ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
1052 vtd_as->devfn, &ce_cache);
1053 if (ret) {
1054 /*
1055 * This should not really happen, but in case it happens,
1056 * we just skip the sync for this time. After all we even
1057 * don't have the root table pointer!
1058 */
1059 trace_vtd_err("Detected invalid context entry when "
1060 "trying to sync shadow page table");
1061 return 0;
1062 }
1063 }
1064
1065 info.domain_id = VTD_CONTEXT_ENTRY_DID(ce_cache.hi);
1066
1067 return vtd_page_walk(&ce_cache, addr, addr + size, &info);
1068}
1069
1070static int vtd_sync_shadow_page_table(VTDAddressSpace *vtd_as)
1071{
1072 return vtd_sync_shadow_page_table_range(vtd_as, NULL, 0, UINT64_MAX);
1073}
1074
Peter Xudbaabb22017-05-19 11:19:47 +08001075/*
1076 * Fetch translation type for specific device. Returns <0 if error
1077 * happens, otherwise return the shifted type to check against
1078 * VTD_CONTEXT_TT_*.
1079 */
1080static int vtd_dev_get_trans_type(VTDAddressSpace *as)
1081{
1082 IntelIOMMUState *s;
1083 VTDContextEntry ce;
1084 int ret;
1085
1086 s = as->iommu_state;
1087
1088 ret = vtd_dev_to_context_entry(s, pci_bus_num(as->bus),
1089 as->devfn, &ce);
1090 if (ret) {
1091 return ret;
1092 }
1093
1094 return vtd_ce_get_type(&ce);
1095}
1096
1097static bool vtd_dev_pt_enabled(VTDAddressSpace *as)
1098{
1099 int ret;
1100
1101 assert(as);
1102
1103 ret = vtd_dev_get_trans_type(as);
1104 if (ret < 0) {
1105 /*
1106 * Possibly failed to parse the context entry for some reason
1107 * (e.g., during init, or any guest configuration errors on
1108 * context entries). We should assume PT not enabled for
1109 * safety.
1110 */
1111 return false;
1112 }
1113
1114 return ret == VTD_CONTEXT_TT_PASS_THROUGH;
1115}
1116
1117/* Return whether the device is using IOMMU translation. */
1118static bool vtd_switch_address_space(VTDAddressSpace *as)
1119{
1120 bool use_iommu;
Peter Xu66a4a032017-08-17 13:56:14 +08001121 /* Whether we need to take the BQL on our own */
1122 bool take_bql = !qemu_mutex_iothread_locked();
Peter Xudbaabb22017-05-19 11:19:47 +08001123
1124 assert(as);
1125
1126 use_iommu = as->iommu_state->dmar_enabled & !vtd_dev_pt_enabled(as);
1127
1128 trace_vtd_switch_address_space(pci_bus_num(as->bus),
1129 VTD_PCI_SLOT(as->devfn),
1130 VTD_PCI_FUNC(as->devfn),
1131 use_iommu);
1132
Peter Xu66a4a032017-08-17 13:56:14 +08001133 /*
1134 * It's possible that we reach here without BQL, e.g., when called
1135 * from vtd_pt_enable_fast_path(). However the memory APIs need
1136 * it. We'd better make sure we have had it already, or, take it.
1137 */
1138 if (take_bql) {
1139 qemu_mutex_lock_iothread();
1140 }
1141
Peter Xudbaabb22017-05-19 11:19:47 +08001142 /* Turn off first then on the other */
1143 if (use_iommu) {
1144 memory_region_set_enabled(&as->sys_alias, false);
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001145 memory_region_set_enabled(MEMORY_REGION(&as->iommu), true);
Peter Xudbaabb22017-05-19 11:19:47 +08001146 } else {
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10001147 memory_region_set_enabled(MEMORY_REGION(&as->iommu), false);
Peter Xudbaabb22017-05-19 11:19:47 +08001148 memory_region_set_enabled(&as->sys_alias, true);
1149 }
1150
Peter Xu66a4a032017-08-17 13:56:14 +08001151 if (take_bql) {
1152 qemu_mutex_unlock_iothread();
1153 }
1154
Peter Xudbaabb22017-05-19 11:19:47 +08001155 return use_iommu;
1156}
1157
1158static void vtd_switch_address_space_all(IntelIOMMUState *s)
1159{
1160 GHashTableIter iter;
1161 VTDBus *vtd_bus;
1162 int i;
1163
1164 g_hash_table_iter_init(&iter, s->vtd_as_by_busptr);
1165 while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) {
Peter Xubf33cc72017-12-08 12:26:53 +08001166 for (i = 0; i < PCI_DEVFN_MAX; i++) {
Peter Xudbaabb22017-05-19 11:19:47 +08001167 if (!vtd_bus->dev_as[i]) {
1168 continue;
1169 }
1170 vtd_switch_address_space(vtd_bus->dev_as[i]);
1171 }
1172 }
1173}
1174
Le Tan1da12ec2014-08-16 13:55:38 +08001175static inline uint16_t vtd_make_source_id(uint8_t bus_num, uint8_t devfn)
1176{
1177 return ((bus_num & 0xffUL) << 8) | (devfn & 0xffUL);
1178}
1179
1180static const bool vtd_qualified_faults[] = {
1181 [VTD_FR_RESERVED] = false,
1182 [VTD_FR_ROOT_ENTRY_P] = false,
1183 [VTD_FR_CONTEXT_ENTRY_P] = true,
1184 [VTD_FR_CONTEXT_ENTRY_INV] = true,
1185 [VTD_FR_ADDR_BEYOND_MGAW] = true,
1186 [VTD_FR_WRITE] = true,
1187 [VTD_FR_READ] = true,
1188 [VTD_FR_PAGING_ENTRY_INV] = true,
1189 [VTD_FR_ROOT_TABLE_INV] = false,
1190 [VTD_FR_CONTEXT_TABLE_INV] = false,
1191 [VTD_FR_ROOT_ENTRY_RSVD] = false,
1192 [VTD_FR_PAGING_ENTRY_RSVD] = true,
1193 [VTD_FR_CONTEXT_ENTRY_TT] = true,
1194 [VTD_FR_RESERVED_ERR] = false,
1195 [VTD_FR_MAX] = false,
1196};
1197
1198/* To see if a fault condition is "qualified", which is reported to software
1199 * only if the FPD field in the context-entry used to process the faulting
1200 * request is 0.
1201 */
1202static inline bool vtd_is_qualified_fault(VTDFaultReason fault)
1203{
1204 return vtd_qualified_faults[fault];
1205}
1206
1207static inline bool vtd_is_interrupt_addr(hwaddr addr)
1208{
1209 return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST;
1210}
1211
Peter Xudbaabb22017-05-19 11:19:47 +08001212static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
1213{
1214 VTDBus *vtd_bus;
1215 VTDAddressSpace *vtd_as;
1216 bool success = false;
1217
1218 vtd_bus = vtd_find_as_from_bus_num(s, VTD_SID_TO_BUS(source_id));
1219 if (!vtd_bus) {
1220 goto out;
1221 }
1222
1223 vtd_as = vtd_bus->dev_as[VTD_SID_TO_DEVFN(source_id)];
1224 if (!vtd_as) {
1225 goto out;
1226 }
1227
1228 if (vtd_switch_address_space(vtd_as) == false) {
1229 /* We switched off IOMMU region successfully. */
1230 success = true;
1231 }
1232
1233out:
1234 trace_vtd_pt_enable_fast_path(source_id, success);
1235}
1236
Le Tan1da12ec2014-08-16 13:55:38 +08001237/* Map dev to context-entry then do a paging-structures walk to do a iommu
1238 * translation.
Paolo Bonzini79e2b9a2015-01-21 12:09:14 +01001239 *
1240 * Called from RCU critical section.
1241 *
Le Tan1da12ec2014-08-16 13:55:38 +08001242 * @bus_num: The bus number
1243 * @devfn: The devfn, which is the combined of device and function number
1244 * @is_write: The access is a write operation
1245 * @entry: IOMMUTLBEntry that contain the addr to be translated and result
Peter Xub9313022017-06-09 21:53:28 +08001246 *
1247 * Returns true if translation is successful, otherwise false.
Le Tan1da12ec2014-08-16 13:55:38 +08001248 */
Peter Xub9313022017-06-09 21:53:28 +08001249static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
Le Tan1da12ec2014-08-16 13:55:38 +08001250 uint8_t devfn, hwaddr addr, bool is_write,
1251 IOMMUTLBEntry *entry)
1252{
Le Tand92fa2d2014-08-16 13:55:43 +08001253 IntelIOMMUState *s = vtd_as->iommu_state;
Le Tan1da12ec2014-08-16 13:55:38 +08001254 VTDContextEntry ce;
Knut Omang7df953b2015-10-04 15:48:50 +02001255 uint8_t bus_num = pci_bus_num(bus);
Peter Xu1d9efa72018-05-18 15:25:11 +08001256 VTDContextCacheEntry *cc_entry;
Jason Wangd66b9692016-01-14 00:47:24 -05001257 uint64_t slpte, page_mask;
Le Tan1da12ec2014-08-16 13:55:38 +08001258 uint32_t level;
1259 uint16_t source_id = vtd_make_source_id(bus_num, devfn);
1260 int ret_fr;
1261 bool is_fpd_set = false;
1262 bool reads = true;
1263 bool writes = true;
Peter Xu07f7b732017-07-17 17:02:30 +08001264 uint8_t access_flags;
Le Tanb5a280c2014-08-16 13:55:44 +08001265 VTDIOTLBEntry *iotlb_entry;
Le Tan1da12ec2014-08-16 13:55:38 +08001266
Peter Xu046ab7e2017-02-07 16:28:07 +08001267 /*
1268 * We have standalone memory region for interrupt addresses, we
1269 * should never receive translation requests in this region.
1270 */
1271 assert(!vtd_is_interrupt_addr(addr));
1272
Peter Xu1d9efa72018-05-18 15:25:11 +08001273 vtd_iommu_lock(s);
1274
1275 cc_entry = &vtd_as->context_cache_entry;
1276
Le Tanb5a280c2014-08-16 13:55:44 +08001277 /* Try to fetch slpte form IOTLB */
1278 iotlb_entry = vtd_lookup_iotlb(s, source_id, addr);
1279 if (iotlb_entry) {
Peter Xu6c441e12017-02-07 16:28:10 +08001280 trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte,
1281 iotlb_entry->domain_id);
Le Tanb5a280c2014-08-16 13:55:44 +08001282 slpte = iotlb_entry->slpte;
Peter Xu07f7b732017-07-17 17:02:30 +08001283 access_flags = iotlb_entry->access_flags;
Jason Wangd66b9692016-01-14 00:47:24 -05001284 page_mask = iotlb_entry->mask;
Le Tanb5a280c2014-08-16 13:55:44 +08001285 goto out;
1286 }
Peter Xub9313022017-06-09 21:53:28 +08001287
Le Tand92fa2d2014-08-16 13:55:43 +08001288 /* Try to fetch context-entry from cache first */
1289 if (cc_entry->context_cache_gen == s->context_cache_gen) {
Peter Xu6c441e12017-02-07 16:28:10 +08001290 trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi,
1291 cc_entry->context_entry.lo,
1292 cc_entry->context_cache_gen);
Le Tand92fa2d2014-08-16 13:55:43 +08001293 ce = cc_entry->context_entry;
1294 is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
1295 } else {
1296 ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
1297 is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
1298 if (ret_fr) {
1299 ret_fr = -ret_fr;
1300 if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
Peter Xu6c441e12017-02-07 16:28:10 +08001301 trace_vtd_fault_disabled();
Le Tand92fa2d2014-08-16 13:55:43 +08001302 } else {
1303 vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
1304 }
Peter Xub9313022017-06-09 21:53:28 +08001305 goto error;
Le Tan1da12ec2014-08-16 13:55:38 +08001306 }
Le Tand92fa2d2014-08-16 13:55:43 +08001307 /* Update context-cache */
Peter Xu6c441e12017-02-07 16:28:10 +08001308 trace_vtd_iotlb_cc_update(bus_num, devfn, ce.hi, ce.lo,
1309 cc_entry->context_cache_gen,
1310 s->context_cache_gen);
Le Tand92fa2d2014-08-16 13:55:43 +08001311 cc_entry->context_entry = ce;
1312 cc_entry->context_cache_gen = s->context_cache_gen;
Le Tan1da12ec2014-08-16 13:55:38 +08001313 }
1314
Peter Xudbaabb22017-05-19 11:19:47 +08001315 /*
1316 * We don't need to translate for pass-through context entries.
1317 * Also, let's ignore IOTLB caching as well for PT devices.
1318 */
1319 if (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH) {
Peter Xu892721d2017-07-17 17:02:29 +08001320 entry->iova = addr & VTD_PAGE_MASK_4K;
Peter Xudbaabb22017-05-19 11:19:47 +08001321 entry->translated_addr = entry->iova;
Peter Xu892721d2017-07-17 17:02:29 +08001322 entry->addr_mask = ~VTD_PAGE_MASK_4K;
Peter Xudbaabb22017-05-19 11:19:47 +08001323 entry->perm = IOMMU_RW;
1324 trace_vtd_translate_pt(source_id, entry->iova);
1325
1326 /*
1327 * When this happens, it means firstly caching-mode is not
1328 * enabled, and this is the first passthrough translation for
1329 * the device. Let's enable the fast path for passthrough.
1330 *
1331 * When passthrough is disabled again for the device, we can
1332 * capture it via the context entry invalidation, then the
1333 * IOMMU region can be swapped back.
1334 */
1335 vtd_pt_enable_fast_path(s, source_id);
Peter Xu1d9efa72018-05-18 15:25:11 +08001336 vtd_iommu_unlock(s);
Peter Xub9313022017-06-09 21:53:28 +08001337 return true;
Peter Xudbaabb22017-05-19 11:19:47 +08001338 }
1339
Peter Xu6e905562017-02-07 16:28:08 +08001340 ret_fr = vtd_iova_to_slpte(&ce, addr, is_write, &slpte, &level,
Prasad Singamsetty37f51382017-11-14 18:13:50 -05001341 &reads, &writes, s->aw_bits);
Le Tan1da12ec2014-08-16 13:55:38 +08001342 if (ret_fr) {
1343 ret_fr = -ret_fr;
1344 if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
Peter Xu6c441e12017-02-07 16:28:10 +08001345 trace_vtd_fault_disabled();
Le Tan1da12ec2014-08-16 13:55:38 +08001346 } else {
1347 vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
1348 }
Peter Xub9313022017-06-09 21:53:28 +08001349 goto error;
Le Tan1da12ec2014-08-16 13:55:38 +08001350 }
1351
Jason Wangd66b9692016-01-14 00:47:24 -05001352 page_mask = vtd_slpt_level_page_mask(level);
Peter Xu07f7b732017-07-17 17:02:30 +08001353 access_flags = IOMMU_ACCESS_FLAG(reads, writes);
Le Tanb5a280c2014-08-16 13:55:44 +08001354 vtd_update_iotlb(s, source_id, VTD_CONTEXT_ENTRY_DID(ce.hi), addr, slpte,
Peter Xu07f7b732017-07-17 17:02:30 +08001355 access_flags, level);
Le Tanb5a280c2014-08-16 13:55:44 +08001356out:
Peter Xu1d9efa72018-05-18 15:25:11 +08001357 vtd_iommu_unlock(s);
Jason Wangd66b9692016-01-14 00:47:24 -05001358 entry->iova = addr & page_mask;
Prasad Singamsetty37f51382017-11-14 18:13:50 -05001359 entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
Jason Wangd66b9692016-01-14 00:47:24 -05001360 entry->addr_mask = ~page_mask;
Peter Xu07f7b732017-07-17 17:02:30 +08001361 entry->perm = access_flags;
Peter Xub9313022017-06-09 21:53:28 +08001362 return true;
1363
1364error:
Peter Xu1d9efa72018-05-18 15:25:11 +08001365 vtd_iommu_unlock(s);
Peter Xub9313022017-06-09 21:53:28 +08001366 entry->iova = 0;
1367 entry->translated_addr = 0;
1368 entry->addr_mask = 0;
1369 entry->perm = IOMMU_NONE;
1370 return false;
Le Tan1da12ec2014-08-16 13:55:38 +08001371}
1372
1373static void vtd_root_table_setup(IntelIOMMUState *s)
1374{
1375 s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
1376 s->root_extended = s->root & VTD_RTADDR_RTT;
Prasad Singamsetty37f51382017-11-14 18:13:50 -05001377 s->root &= VTD_RTADDR_ADDR_MASK(s->aw_bits);
Le Tan1da12ec2014-08-16 13:55:38 +08001378
Peter Xu7feb51b2017-06-09 21:53:27 +08001379 trace_vtd_reg_dmar_root(s->root, s->root_extended);
Le Tan1da12ec2014-08-16 13:55:38 +08001380}
1381
Peter Xu02a2cbc2016-07-14 13:56:26 +08001382static void vtd_iec_notify_all(IntelIOMMUState *s, bool global,
1383 uint32_t index, uint32_t mask)
1384{
1385 x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s), global, index, mask);
1386}
1387
Peter Xua5861432016-07-14 13:56:18 +08001388static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s)
1389{
1390 uint64_t value = 0;
1391 value = vtd_get_quad_raw(s, DMAR_IRTA_REG);
1392 s->intr_size = 1UL << ((value & VTD_IRTA_SIZE_MASK) + 1);
Prasad Singamsetty37f51382017-11-14 18:13:50 -05001393 s->intr_root = value & VTD_IRTA_ADDR_MASK(s->aw_bits);
Jan Kiszka28589312016-07-14 13:56:28 +08001394 s->intr_eime = value & VTD_IRTA_EIME;
Peter Xua5861432016-07-14 13:56:18 +08001395
Peter Xu02a2cbc2016-07-14 13:56:26 +08001396 /* Notify global invalidation */
1397 vtd_iec_notify_all(s, true, 0, 0);
Peter Xua5861432016-07-14 13:56:18 +08001398
Peter Xu7feb51b2017-06-09 21:53:27 +08001399 trace_vtd_reg_ir_root(s->intr_root, s->intr_size);
Peter Xua5861432016-07-14 13:56:18 +08001400}
1401
Peter Xudd4d6072017-04-07 18:59:15 +08001402static void vtd_iommu_replay_all(IntelIOMMUState *s)
1403{
Peter Xub4a4ba02018-05-18 15:25:10 +08001404 VTDAddressSpace *vtd_as;
Peter Xudd4d6072017-04-07 18:59:15 +08001405
Peter Xub4a4ba02018-05-18 15:25:10 +08001406 QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
Peter Xu63b88962018-05-18 15:25:17 +08001407 vtd_sync_shadow_page_table(vtd_as);
Peter Xudd4d6072017-04-07 18:59:15 +08001408 }
1409}
1410
Le Tand92fa2d2014-08-16 13:55:43 +08001411static void vtd_context_global_invalidate(IntelIOMMUState *s)
1412{
Peter Xubc535e52017-02-07 16:28:09 +08001413 trace_vtd_inv_desc_cc_global();
Peter Xu1d9efa72018-05-18 15:25:11 +08001414 /* Protects context cache */
1415 vtd_iommu_lock(s);
Le Tand92fa2d2014-08-16 13:55:43 +08001416 s->context_cache_gen++;
1417 if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) {
Peter Xu1d9efa72018-05-18 15:25:11 +08001418 vtd_reset_context_cache_locked(s);
Le Tand92fa2d2014-08-16 13:55:43 +08001419 }
Peter Xu1d9efa72018-05-18 15:25:11 +08001420 vtd_iommu_unlock(s);
Peter Xudbaabb22017-05-19 11:19:47 +08001421 vtd_switch_address_space_all(s);
Peter Xudd4d6072017-04-07 18:59:15 +08001422 /*
1423 * From VT-d spec 6.5.2.1, a global context entry invalidation
1424 * should be followed by a IOTLB global invalidation, so we should
1425 * be safe even without this. Hoewever, let's replay the region as
1426 * well to be safer, and go back here when we need finer tunes for
1427 * VT-d emulation codes.
1428 */
1429 vtd_iommu_replay_all(s);
Le Tand92fa2d2014-08-16 13:55:43 +08001430}
1431
1432/* Do a context-cache device-selective invalidation.
1433 * @func_mask: FM field after shifting
1434 */
1435static void vtd_context_device_invalidate(IntelIOMMUState *s,
1436 uint16_t source_id,
1437 uint16_t func_mask)
1438{
1439 uint16_t mask;
Knut Omang7df953b2015-10-04 15:48:50 +02001440 VTDBus *vtd_bus;
Le Tand92fa2d2014-08-16 13:55:43 +08001441 VTDAddressSpace *vtd_as;
Peter Xubc535e52017-02-07 16:28:09 +08001442 uint8_t bus_n, devfn;
Le Tand92fa2d2014-08-16 13:55:43 +08001443 uint16_t devfn_it;
1444
Peter Xubc535e52017-02-07 16:28:09 +08001445 trace_vtd_inv_desc_cc_devices(source_id, func_mask);
1446
Le Tand92fa2d2014-08-16 13:55:43 +08001447 switch (func_mask & 3) {
1448 case 0:
1449 mask = 0; /* No bits in the SID field masked */
1450 break;
1451 case 1:
1452 mask = 4; /* Mask bit 2 in the SID field */
1453 break;
1454 case 2:
1455 mask = 6; /* Mask bit 2:1 in the SID field */
1456 break;
1457 case 3:
1458 mask = 7; /* Mask bit 2:0 in the SID field */
1459 break;
1460 }
Peter Xu6cb99ac2016-11-29 13:43:40 +08001461 mask = ~mask;
Peter Xubc535e52017-02-07 16:28:09 +08001462
1463 bus_n = VTD_SID_TO_BUS(source_id);
1464 vtd_bus = vtd_find_as_from_bus_num(s, bus_n);
Knut Omang7df953b2015-10-04 15:48:50 +02001465 if (vtd_bus) {
Le Tand92fa2d2014-08-16 13:55:43 +08001466 devfn = VTD_SID_TO_DEVFN(source_id);
Peter Xubf33cc72017-12-08 12:26:53 +08001467 for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) {
Knut Omang7df953b2015-10-04 15:48:50 +02001468 vtd_as = vtd_bus->dev_as[devfn_it];
Le Tand92fa2d2014-08-16 13:55:43 +08001469 if (vtd_as && ((devfn_it & mask) == (devfn & mask))) {
Peter Xubc535e52017-02-07 16:28:09 +08001470 trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(devfn_it),
1471 VTD_PCI_FUNC(devfn_it));
Peter Xu1d9efa72018-05-18 15:25:11 +08001472 vtd_iommu_lock(s);
Le Tand92fa2d2014-08-16 13:55:43 +08001473 vtd_as->context_cache_entry.context_cache_gen = 0;
Peter Xu1d9efa72018-05-18 15:25:11 +08001474 vtd_iommu_unlock(s);
Peter Xudd4d6072017-04-07 18:59:15 +08001475 /*
Peter Xudbaabb22017-05-19 11:19:47 +08001476 * Do switch address space when needed, in case if the
1477 * device passthrough bit is switched.
1478 */
1479 vtd_switch_address_space(vtd_as);
1480 /*
Peter Xudd4d6072017-04-07 18:59:15 +08001481 * So a device is moving out of (or moving into) a
Peter Xu63b88962018-05-18 15:25:17 +08001482 * domain, resync the shadow page table.
Peter Xudd4d6072017-04-07 18:59:15 +08001483 * This won't bring bad even if we have no such
1484 * notifier registered - the IOMMU notification
1485 * framework will skip MAP notifications if that
1486 * happened.
1487 */
Peter Xu63b88962018-05-18 15:25:17 +08001488 vtd_sync_shadow_page_table(vtd_as);
Le Tand92fa2d2014-08-16 13:55:43 +08001489 }
1490 }
1491 }
1492}
1493
Le Tan1da12ec2014-08-16 13:55:38 +08001494/* Context-cache invalidation
1495 * Returns the Context Actual Invalidation Granularity.
1496 * @val: the content of the CCMD_REG
1497 */
1498static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val)
1499{
1500 uint64_t caig;
1501 uint64_t type = val & VTD_CCMD_CIRG_MASK;
1502
1503 switch (type) {
Le Tan1da12ec2014-08-16 13:55:38 +08001504 case VTD_CCMD_DOMAIN_INVL:
Le Tand92fa2d2014-08-16 13:55:43 +08001505 /* Fall through */
1506 case VTD_CCMD_GLOBAL_INVL:
Le Tand92fa2d2014-08-16 13:55:43 +08001507 caig = VTD_CCMD_GLOBAL_INVL_A;
1508 vtd_context_global_invalidate(s);
Le Tan1da12ec2014-08-16 13:55:38 +08001509 break;
1510
1511 case VTD_CCMD_DEVICE_INVL:
Le Tan1da12ec2014-08-16 13:55:38 +08001512 caig = VTD_CCMD_DEVICE_INVL_A;
Le Tand92fa2d2014-08-16 13:55:43 +08001513 vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val));
Le Tan1da12ec2014-08-16 13:55:38 +08001514 break;
1515
1516 default:
Peter Xu7feb51b2017-06-09 21:53:27 +08001517 trace_vtd_err("Context cache invalidate type error.");
Le Tan1da12ec2014-08-16 13:55:38 +08001518 caig = 0;
1519 }
1520 return caig;
1521}
1522
Le Tanb5a280c2014-08-16 13:55:44 +08001523static void vtd_iotlb_global_invalidate(IntelIOMMUState *s)
1524{
Peter Xu7feb51b2017-06-09 21:53:27 +08001525 trace_vtd_inv_desc_iotlb_global();
Le Tanb5a280c2014-08-16 13:55:44 +08001526 vtd_reset_iotlb(s);
Peter Xudd4d6072017-04-07 18:59:15 +08001527 vtd_iommu_replay_all(s);
Le Tanb5a280c2014-08-16 13:55:44 +08001528}
1529
1530static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
1531{
Peter Xudd4d6072017-04-07 18:59:15 +08001532 VTDContextEntry ce;
1533 VTDAddressSpace *vtd_as;
1534
Peter Xu7feb51b2017-06-09 21:53:27 +08001535 trace_vtd_inv_desc_iotlb_domain(domain_id);
1536
Peter Xu1d9efa72018-05-18 15:25:11 +08001537 vtd_iommu_lock(s);
Le Tanb5a280c2014-08-16 13:55:44 +08001538 g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
1539 &domain_id);
Peter Xu1d9efa72018-05-18 15:25:11 +08001540 vtd_iommu_unlock(s);
Peter Xudd4d6072017-04-07 18:59:15 +08001541
Peter Xub4a4ba02018-05-18 15:25:10 +08001542 QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
Peter Xudd4d6072017-04-07 18:59:15 +08001543 if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
1544 vtd_as->devfn, &ce) &&
1545 domain_id == VTD_CONTEXT_ENTRY_DID(ce.hi)) {
Peter Xu63b88962018-05-18 15:25:17 +08001546 vtd_sync_shadow_page_table(vtd_as);
Peter Xudd4d6072017-04-07 18:59:15 +08001547 }
1548 }
1549}
1550
Peter Xudd4d6072017-04-07 18:59:15 +08001551static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
1552 uint16_t domain_id, hwaddr addr,
1553 uint8_t am)
1554{
Peter Xub4a4ba02018-05-18 15:25:10 +08001555 VTDAddressSpace *vtd_as;
Peter Xudd4d6072017-04-07 18:59:15 +08001556 VTDContextEntry ce;
1557 int ret;
Peter Xu4f8a62a2018-05-18 15:25:12 +08001558 hwaddr size = (1 << am) * VTD_PAGE_SIZE;
Peter Xudd4d6072017-04-07 18:59:15 +08001559
Peter Xub4a4ba02018-05-18 15:25:10 +08001560 QLIST_FOREACH(vtd_as, &(s->vtd_as_with_notifiers), next) {
Peter Xudd4d6072017-04-07 18:59:15 +08001561 ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
1562 vtd_as->devfn, &ce);
1563 if (!ret && domain_id == VTD_CONTEXT_ENTRY_DID(ce.hi)) {
Peter Xu4f8a62a2018-05-18 15:25:12 +08001564 if (vtd_as_has_map_notifier(vtd_as)) {
1565 /*
1566 * As long as we have MAP notifications registered in
1567 * any of our IOMMU notifiers, we need to sync the
1568 * shadow page table.
1569 */
Peter Xu63b88962018-05-18 15:25:17 +08001570 vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
Peter Xu4f8a62a2018-05-18 15:25:12 +08001571 } else {
1572 /*
1573 * For UNMAP-only notifiers, we don't need to walk the
1574 * page tables. We just deliver the PSI down to
1575 * invalidate caches.
1576 */
1577 IOMMUTLBEntry entry = {
1578 .target_as = &address_space_memory,
1579 .iova = addr,
1580 .translated_addr = 0,
1581 .addr_mask = size - 1,
1582 .perm = IOMMU_NONE,
1583 };
Peter Maydellcb1efcf2018-06-15 14:57:16 +01001584 memory_region_notify_iommu(&vtd_as->iommu, 0, entry);
Peter Xu4f8a62a2018-05-18 15:25:12 +08001585 }
Peter Xudd4d6072017-04-07 18:59:15 +08001586 }
1587 }
Le Tanb5a280c2014-08-16 13:55:44 +08001588}
1589
1590static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
1591 hwaddr addr, uint8_t am)
1592{
1593 VTDIOTLBPageInvInfo info;
1594
Peter Xu7feb51b2017-06-09 21:53:27 +08001595 trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am);
1596
Le Tanb5a280c2014-08-16 13:55:44 +08001597 assert(am <= VTD_MAMV);
1598 info.domain_id = domain_id;
Jason Wangd66b9692016-01-14 00:47:24 -05001599 info.addr = addr;
Le Tanb5a280c2014-08-16 13:55:44 +08001600 info.mask = ~((1 << am) - 1);
Peter Xu1d9efa72018-05-18 15:25:11 +08001601 vtd_iommu_lock(s);
Le Tanb5a280c2014-08-16 13:55:44 +08001602 g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
Peter Xu1d9efa72018-05-18 15:25:11 +08001603 vtd_iommu_unlock(s);
Peter Xudd4d6072017-04-07 18:59:15 +08001604 vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am);
Le Tanb5a280c2014-08-16 13:55:44 +08001605}
1606
Le Tan1da12ec2014-08-16 13:55:38 +08001607/* Flush IOTLB
1608 * Returns the IOTLB Actual Invalidation Granularity.
1609 * @val: the content of the IOTLB_REG
1610 */
1611static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val)
1612{
1613 uint64_t iaig;
1614 uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK;
Le Tanb5a280c2014-08-16 13:55:44 +08001615 uint16_t domain_id;
1616 hwaddr addr;
1617 uint8_t am;
Le Tan1da12ec2014-08-16 13:55:38 +08001618
1619 switch (type) {
1620 case VTD_TLB_GLOBAL_FLUSH:
Le Tan1da12ec2014-08-16 13:55:38 +08001621 iaig = VTD_TLB_GLOBAL_FLUSH_A;
Le Tanb5a280c2014-08-16 13:55:44 +08001622 vtd_iotlb_global_invalidate(s);
Le Tan1da12ec2014-08-16 13:55:38 +08001623 break;
1624
1625 case VTD_TLB_DSI_FLUSH:
Le Tanb5a280c2014-08-16 13:55:44 +08001626 domain_id = VTD_TLB_DID(val);
Le Tan1da12ec2014-08-16 13:55:38 +08001627 iaig = VTD_TLB_DSI_FLUSH_A;
Le Tanb5a280c2014-08-16 13:55:44 +08001628 vtd_iotlb_domain_invalidate(s, domain_id);
Le Tan1da12ec2014-08-16 13:55:38 +08001629 break;
1630
1631 case VTD_TLB_PSI_FLUSH:
Le Tanb5a280c2014-08-16 13:55:44 +08001632 domain_id = VTD_TLB_DID(val);
1633 addr = vtd_get_quad_raw(s, DMAR_IVA_REG);
1634 am = VTD_IVA_AM(addr);
1635 addr = VTD_IVA_ADDR(addr);
Le Tanb5a280c2014-08-16 13:55:44 +08001636 if (am > VTD_MAMV) {
Peter Xu7feb51b2017-06-09 21:53:27 +08001637 trace_vtd_err("IOTLB PSI flush: address mask overflow.");
Le Tanb5a280c2014-08-16 13:55:44 +08001638 iaig = 0;
1639 break;
1640 }
Le Tan1da12ec2014-08-16 13:55:38 +08001641 iaig = VTD_TLB_PSI_FLUSH_A;
Le Tanb5a280c2014-08-16 13:55:44 +08001642 vtd_iotlb_page_invalidate(s, domain_id, addr, am);
Le Tan1da12ec2014-08-16 13:55:38 +08001643 break;
1644
1645 default:
Peter Xu7feb51b2017-06-09 21:53:27 +08001646 trace_vtd_err("IOTLB flush: invalid granularity.");
Le Tan1da12ec2014-08-16 13:55:38 +08001647 iaig = 0;
1648 }
1649 return iaig;
1650}
1651
Ladi Prosek8991c462017-06-19 09:31:16 +02001652static void vtd_fetch_inv_desc(IntelIOMMUState *s);
Le Taned7b8fb2014-08-16 13:55:42 +08001653
1654static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s)
1655{
1656 return s->qi_enabled && (s->iq_tail == s->iq_head) &&
1657 (s->iq_last_desc_type == VTD_INV_DESC_WAIT);
1658}
1659
1660static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en)
1661{
1662 uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG);
1663
Peter Xu7feb51b2017-06-09 21:53:27 +08001664 trace_vtd_inv_qi_enable(en);
1665
Le Taned7b8fb2014-08-16 13:55:42 +08001666 if (en) {
Prasad Singamsetty37f51382017-11-14 18:13:50 -05001667 s->iq = iqa_val & VTD_IQA_IQA_MASK(s->aw_bits);
Ladi Prosek8991c462017-06-19 09:31:16 +02001668 /* 2^(x+8) entries */
1669 s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8);
1670 s->qi_enabled = true;
1671 trace_vtd_inv_qi_setup(s->iq, s->iq_size);
1672 /* Ok - report back to driver */
1673 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES);
1674
1675 if (s->iq_tail != 0) {
1676 /*
1677 * This is a spec violation but Windows guests are known to set up
1678 * Queued Invalidation this way so we allow the write and process
1679 * Invalidation Descriptors right away.
1680 */
1681 trace_vtd_warn_invalid_qi_tail(s->iq_tail);
1682 if (!(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
1683 vtd_fetch_inv_desc(s);
1684 }
Le Taned7b8fb2014-08-16 13:55:42 +08001685 }
1686 } else {
1687 if (vtd_queued_inv_disable_check(s)) {
1688 /* disable Queued Invalidation */
1689 vtd_set_quad_raw(s, DMAR_IQH_REG, 0);
1690 s->iq_head = 0;
1691 s->qi_enabled = false;
1692 /* Ok - report back to driver */
1693 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0);
1694 } else {
Peter Xu7feb51b2017-06-09 21:53:27 +08001695 trace_vtd_err_qi_disable(s->iq_head, s->iq_tail, s->iq_last_desc_type);
Le Taned7b8fb2014-08-16 13:55:42 +08001696 }
1697 }
1698}
1699
Le Tan1da12ec2014-08-16 13:55:38 +08001700/* Set Root Table Pointer */
1701static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
1702{
Le Tan1da12ec2014-08-16 13:55:38 +08001703 vtd_root_table_setup(s);
1704 /* Ok - report back to driver */
1705 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
1706}
1707
Peter Xua5861432016-07-14 13:56:18 +08001708/* Set Interrupt Remap Table Pointer */
1709static void vtd_handle_gcmd_sirtp(IntelIOMMUState *s)
1710{
Peter Xua5861432016-07-14 13:56:18 +08001711 vtd_interrupt_remap_table_setup(s);
1712 /* Ok - report back to driver */
1713 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRTPS);
1714}
1715
Le Tan1da12ec2014-08-16 13:55:38 +08001716/* Handle Translation Enable/Disable */
1717static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
1718{
Peter Xu558e0022017-04-07 18:59:14 +08001719 if (s->dmar_enabled == en) {
1720 return;
1721 }
1722
Peter Xu7feb51b2017-06-09 21:53:27 +08001723 trace_vtd_dmar_enable(en);
Le Tan1da12ec2014-08-16 13:55:38 +08001724
1725 if (en) {
1726 s->dmar_enabled = true;
1727 /* Ok - report back to driver */
1728 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES);
1729 } else {
1730 s->dmar_enabled = false;
1731
1732 /* Clear the index of Fault Recording Register */
1733 s->next_frcd_reg = 0;
1734 /* Ok - report back to driver */
1735 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0);
1736 }
Peter Xu558e0022017-04-07 18:59:14 +08001737
1738 vtd_switch_address_space_all(s);
Le Tan1da12ec2014-08-16 13:55:38 +08001739}
1740
Peter Xu80de52b2016-07-14 13:56:19 +08001741/* Handle Interrupt Remap Enable/Disable */
1742static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
1743{
Peter Xu7feb51b2017-06-09 21:53:27 +08001744 trace_vtd_ir_enable(en);
Peter Xu80de52b2016-07-14 13:56:19 +08001745
1746 if (en) {
1747 s->intr_enabled = true;
1748 /* Ok - report back to driver */
1749 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES);
1750 } else {
1751 s->intr_enabled = false;
1752 /* Ok - report back to driver */
1753 vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0);
1754 }
1755}
1756
Le Tan1da12ec2014-08-16 13:55:38 +08001757/* Handle write to Global Command Register */
1758static void vtd_handle_gcmd_write(IntelIOMMUState *s)
1759{
1760 uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG);
1761 uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG);
1762 uint32_t changed = status ^ val;
1763
Peter Xu7feb51b2017-06-09 21:53:27 +08001764 trace_vtd_reg_write_gcmd(status, val);
Le Tan1da12ec2014-08-16 13:55:38 +08001765 if (changed & VTD_GCMD_TE) {
1766 /* Translation enable/disable */
1767 vtd_handle_gcmd_te(s, val & VTD_GCMD_TE);
1768 }
1769 if (val & VTD_GCMD_SRTP) {
1770 /* Set/update the root-table pointer */
1771 vtd_handle_gcmd_srtp(s);
1772 }
Le Taned7b8fb2014-08-16 13:55:42 +08001773 if (changed & VTD_GCMD_QIE) {
1774 /* Queued Invalidation Enable */
1775 vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE);
1776 }
Peter Xua5861432016-07-14 13:56:18 +08001777 if (val & VTD_GCMD_SIRTP) {
1778 /* Set/update the interrupt remapping root-table pointer */
1779 vtd_handle_gcmd_sirtp(s);
1780 }
Peter Xu80de52b2016-07-14 13:56:19 +08001781 if (changed & VTD_GCMD_IRE) {
1782 /* Interrupt remap enable/disable */
1783 vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE);
1784 }
Le Tan1da12ec2014-08-16 13:55:38 +08001785}
1786
1787/* Handle write to Context Command Register */
1788static void vtd_handle_ccmd_write(IntelIOMMUState *s)
1789{
1790 uint64_t ret;
1791 uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG);
1792
1793 /* Context-cache invalidation request */
1794 if (val & VTD_CCMD_ICC) {
Le Taned7b8fb2014-08-16 13:55:42 +08001795 if (s->qi_enabled) {
Peter Xu7feb51b2017-06-09 21:53:27 +08001796 trace_vtd_err("Queued Invalidation enabled, "
1797 "should not use register-based invalidation");
Le Taned7b8fb2014-08-16 13:55:42 +08001798 return;
1799 }
Le Tan1da12ec2014-08-16 13:55:38 +08001800 ret = vtd_context_cache_invalidate(s, val);
1801 /* Invalidation completed. Change something to show */
1802 vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL);
1803 ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK,
1804 ret);
Le Tan1da12ec2014-08-16 13:55:38 +08001805 }
1806}
1807
1808/* Handle write to IOTLB Invalidation Register */
1809static void vtd_handle_iotlb_write(IntelIOMMUState *s)
1810{
1811 uint64_t ret;
1812 uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG);
1813
1814 /* IOTLB invalidation request */
1815 if (val & VTD_TLB_IVT) {
Le Taned7b8fb2014-08-16 13:55:42 +08001816 if (s->qi_enabled) {
Peter Xu7feb51b2017-06-09 21:53:27 +08001817 trace_vtd_err("Queued Invalidation enabled, "
1818 "should not use register-based invalidation.");
Le Taned7b8fb2014-08-16 13:55:42 +08001819 return;
1820 }
Le Tan1da12ec2014-08-16 13:55:38 +08001821 ret = vtd_iotlb_flush(s, val);
1822 /* Invalidation completed. Change something to show */
1823 vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL);
1824 ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG,
1825 VTD_TLB_FLUSH_GRANU_MASK_A, ret);
Le Tan1da12ec2014-08-16 13:55:38 +08001826 }
1827}
1828
Le Taned7b8fb2014-08-16 13:55:42 +08001829/* Fetch an Invalidation Descriptor from the Invalidation Queue */
1830static bool vtd_get_inv_desc(dma_addr_t base_addr, uint32_t offset,
1831 VTDInvDesc *inv_desc)
1832{
1833 dma_addr_t addr = base_addr + offset * sizeof(*inv_desc);
1834 if (dma_memory_read(&address_space_memory, addr, inv_desc,
1835 sizeof(*inv_desc))) {
Peter Xu7feb51b2017-06-09 21:53:27 +08001836 trace_vtd_err("Read INV DESC failed.");
Le Taned7b8fb2014-08-16 13:55:42 +08001837 inv_desc->lo = 0;
1838 inv_desc->hi = 0;
Le Taned7b8fb2014-08-16 13:55:42 +08001839 return false;
1840 }
1841 inv_desc->lo = le64_to_cpu(inv_desc->lo);
1842 inv_desc->hi = le64_to_cpu(inv_desc->hi);
1843 return true;
1844}
1845
1846static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
1847{
1848 if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) ||
1849 (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) {
Peter Xubc535e52017-02-07 16:28:09 +08001850 trace_vtd_inv_desc_wait_invalid(inv_desc->hi, inv_desc->lo);
Le Taned7b8fb2014-08-16 13:55:42 +08001851 return false;
1852 }
1853 if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) {
1854 /* Status Write */
1855 uint32_t status_data = (uint32_t)(inv_desc->lo >>
1856 VTD_INV_DESC_WAIT_DATA_SHIFT);
1857
1858 assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF));
1859
1860 /* FIXME: need to be masked with HAW? */
1861 dma_addr_t status_addr = inv_desc->hi;
Peter Xubc535e52017-02-07 16:28:09 +08001862 trace_vtd_inv_desc_wait_sw(status_addr, status_data);
Le Taned7b8fb2014-08-16 13:55:42 +08001863 status_data = cpu_to_le32(status_data);
1864 if (dma_memory_write(&address_space_memory, status_addr, &status_data,
1865 sizeof(status_data))) {
Peter Xubc535e52017-02-07 16:28:09 +08001866 trace_vtd_inv_desc_wait_write_fail(inv_desc->hi, inv_desc->lo);
Le Taned7b8fb2014-08-16 13:55:42 +08001867 return false;
1868 }
1869 } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
1870 /* Interrupt flag */
Le Taned7b8fb2014-08-16 13:55:42 +08001871 vtd_generate_completion_event(s);
1872 } else {
Peter Xubc535e52017-02-07 16:28:09 +08001873 trace_vtd_inv_desc_wait_invalid(inv_desc->hi, inv_desc->lo);
Le Taned7b8fb2014-08-16 13:55:42 +08001874 return false;
1875 }
1876 return true;
1877}
1878
Le Tand92fa2d2014-08-16 13:55:43 +08001879static bool vtd_process_context_cache_desc(IntelIOMMUState *s,
1880 VTDInvDesc *inv_desc)
1881{
Peter Xubc535e52017-02-07 16:28:09 +08001882 uint16_t sid, fmask;
1883
Le Tand92fa2d2014-08-16 13:55:43 +08001884 if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) {
Peter Xubc535e52017-02-07 16:28:09 +08001885 trace_vtd_inv_desc_cc_invalid(inv_desc->hi, inv_desc->lo);
Le Tand92fa2d2014-08-16 13:55:43 +08001886 return false;
1887 }
1888 switch (inv_desc->lo & VTD_INV_DESC_CC_G) {
1889 case VTD_INV_DESC_CC_DOMAIN:
Peter Xubc535e52017-02-07 16:28:09 +08001890 trace_vtd_inv_desc_cc_domain(
1891 (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo));
Le Tand92fa2d2014-08-16 13:55:43 +08001892 /* Fall through */
1893 case VTD_INV_DESC_CC_GLOBAL:
Le Tand92fa2d2014-08-16 13:55:43 +08001894 vtd_context_global_invalidate(s);
1895 break;
1896
1897 case VTD_INV_DESC_CC_DEVICE:
Peter Xubc535e52017-02-07 16:28:09 +08001898 sid = VTD_INV_DESC_CC_SID(inv_desc->lo);
1899 fmask = VTD_INV_DESC_CC_FM(inv_desc->lo);
1900 vtd_context_device_invalidate(s, sid, fmask);
Le Tand92fa2d2014-08-16 13:55:43 +08001901 break;
1902
1903 default:
Peter Xubc535e52017-02-07 16:28:09 +08001904 trace_vtd_inv_desc_cc_invalid(inv_desc->hi, inv_desc->lo);
Le Tand92fa2d2014-08-16 13:55:43 +08001905 return false;
1906 }
1907 return true;
1908}
1909
Le Tanb5a280c2014-08-16 13:55:44 +08001910static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
1911{
1912 uint16_t domain_id;
1913 uint8_t am;
1914 hwaddr addr;
1915
1916 if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) ||
1917 (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) {
Peter Xubc535e52017-02-07 16:28:09 +08001918 trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
Le Tanb5a280c2014-08-16 13:55:44 +08001919 return false;
1920 }
1921
1922 switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) {
1923 case VTD_INV_DESC_IOTLB_GLOBAL:
Le Tanb5a280c2014-08-16 13:55:44 +08001924 vtd_iotlb_global_invalidate(s);
1925 break;
1926
1927 case VTD_INV_DESC_IOTLB_DOMAIN:
1928 domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
Le Tanb5a280c2014-08-16 13:55:44 +08001929 vtd_iotlb_domain_invalidate(s, domain_id);
1930 break;
1931
1932 case VTD_INV_DESC_IOTLB_PAGE:
1933 domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
1934 addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi);
1935 am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi);
Le Tanb5a280c2014-08-16 13:55:44 +08001936 if (am > VTD_MAMV) {
Peter Xubc535e52017-02-07 16:28:09 +08001937 trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
Le Tanb5a280c2014-08-16 13:55:44 +08001938 return false;
1939 }
1940 vtd_iotlb_page_invalidate(s, domain_id, addr, am);
1941 break;
1942
1943 default:
Peter Xubc535e52017-02-07 16:28:09 +08001944 trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
Le Tanb5a280c2014-08-16 13:55:44 +08001945 return false;
1946 }
1947 return true;
1948}
1949
Peter Xu02a2cbc2016-07-14 13:56:26 +08001950static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
1951 VTDInvDesc *inv_desc)
1952{
Peter Xu7feb51b2017-06-09 21:53:27 +08001953 trace_vtd_inv_desc_iec(inv_desc->iec.granularity,
1954 inv_desc->iec.index,
1955 inv_desc->iec.index_mask);
Peter Xu02a2cbc2016-07-14 13:56:26 +08001956
1957 vtd_iec_notify_all(s, !inv_desc->iec.granularity,
1958 inv_desc->iec.index,
1959 inv_desc->iec.index_mask);
Jason Wang554f5e12016-12-30 18:09:14 +08001960 return true;
1961}
Peter Xu02a2cbc2016-07-14 13:56:26 +08001962
Jason Wang554f5e12016-12-30 18:09:14 +08001963static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
1964 VTDInvDesc *inv_desc)
1965{
1966 VTDAddressSpace *vtd_dev_as;
1967 IOMMUTLBEntry entry;
1968 struct VTDBus *vtd_bus;
1969 hwaddr addr;
1970 uint64_t sz;
1971 uint16_t sid;
1972 uint8_t devfn;
1973 bool size;
1974 uint8_t bus_num;
1975
1976 addr = VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi);
1977 sid = VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo);
1978 devfn = sid & 0xff;
1979 bus_num = sid >> 8;
1980 size = VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi);
1981
1982 if ((inv_desc->lo & VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO) ||
1983 (inv_desc->hi & VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI)) {
Peter Xu7feb51b2017-06-09 21:53:27 +08001984 trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
Jason Wang554f5e12016-12-30 18:09:14 +08001985 return false;
1986 }
1987
1988 vtd_bus = vtd_find_as_from_bus_num(s, bus_num);
1989 if (!vtd_bus) {
1990 goto done;
1991 }
1992
1993 vtd_dev_as = vtd_bus->dev_as[devfn];
1994 if (!vtd_dev_as) {
1995 goto done;
1996 }
1997
Jason Wang04eb6242017-01-20 14:35:28 +08001998 /* According to ATS spec table 2.4:
1999 * S = 0, bits 15:12 = xxxx range size: 4K
2000 * S = 1, bits 15:12 = xxx0 range size: 8K
2001 * S = 1, bits 15:12 = xx01 range size: 16K
2002 * S = 1, bits 15:12 = x011 range size: 32K
2003 * S = 1, bits 15:12 = 0111 range size: 64K
2004 * ...
2005 */
Jason Wang554f5e12016-12-30 18:09:14 +08002006 if (size) {
Jason Wang04eb6242017-01-20 14:35:28 +08002007 sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
Jason Wang554f5e12016-12-30 18:09:14 +08002008 addr &= ~(sz - 1);
2009 } else {
2010 sz = VTD_PAGE_SIZE;
2011 }
2012
2013 entry.target_as = &vtd_dev_as->as;
2014 entry.addr_mask = sz - 1;
2015 entry.iova = addr;
2016 entry.perm = IOMMU_NONE;
2017 entry.translated_addr = 0;
Peter Maydellcb1efcf2018-06-15 14:57:16 +01002018 memory_region_notify_iommu(&vtd_dev_as->iommu, 0, entry);
Jason Wang554f5e12016-12-30 18:09:14 +08002019
2020done:
Peter Xu02a2cbc2016-07-14 13:56:26 +08002021 return true;
2022}
2023
Le Taned7b8fb2014-08-16 13:55:42 +08002024static bool vtd_process_inv_desc(IntelIOMMUState *s)
2025{
2026 VTDInvDesc inv_desc;
2027 uint8_t desc_type;
2028
Peter Xu7feb51b2017-06-09 21:53:27 +08002029 trace_vtd_inv_qi_head(s->iq_head);
Le Taned7b8fb2014-08-16 13:55:42 +08002030 if (!vtd_get_inv_desc(s->iq, s->iq_head, &inv_desc)) {
2031 s->iq_last_desc_type = VTD_INV_DESC_NONE;
2032 return false;
2033 }
2034 desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
2035 /* FIXME: should update at first or at last? */
2036 s->iq_last_desc_type = desc_type;
2037
2038 switch (desc_type) {
2039 case VTD_INV_DESC_CC:
Peter Xubc535e52017-02-07 16:28:09 +08002040 trace_vtd_inv_desc("context-cache", inv_desc.hi, inv_desc.lo);
Le Tand92fa2d2014-08-16 13:55:43 +08002041 if (!vtd_process_context_cache_desc(s, &inv_desc)) {
2042 return false;
2043 }
Le Taned7b8fb2014-08-16 13:55:42 +08002044 break;
2045
2046 case VTD_INV_DESC_IOTLB:
Peter Xubc535e52017-02-07 16:28:09 +08002047 trace_vtd_inv_desc("iotlb", inv_desc.hi, inv_desc.lo);
Le Tanb5a280c2014-08-16 13:55:44 +08002048 if (!vtd_process_iotlb_desc(s, &inv_desc)) {
2049 return false;
2050 }
Le Taned7b8fb2014-08-16 13:55:42 +08002051 break;
2052
2053 case VTD_INV_DESC_WAIT:
Peter Xubc535e52017-02-07 16:28:09 +08002054 trace_vtd_inv_desc("wait", inv_desc.hi, inv_desc.lo);
Le Taned7b8fb2014-08-16 13:55:42 +08002055 if (!vtd_process_wait_desc(s, &inv_desc)) {
2056 return false;
2057 }
2058 break;
2059
Peter Xub7910472016-07-14 13:56:15 +08002060 case VTD_INV_DESC_IEC:
Peter Xubc535e52017-02-07 16:28:09 +08002061 trace_vtd_inv_desc("iec", inv_desc.hi, inv_desc.lo);
Peter Xu02a2cbc2016-07-14 13:56:26 +08002062 if (!vtd_process_inv_iec_desc(s, &inv_desc)) {
2063 return false;
2064 }
Peter Xub7910472016-07-14 13:56:15 +08002065 break;
2066
Jason Wang554f5e12016-12-30 18:09:14 +08002067 case VTD_INV_DESC_DEVICE:
Peter Xu7feb51b2017-06-09 21:53:27 +08002068 trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo);
Jason Wang554f5e12016-12-30 18:09:14 +08002069 if (!vtd_process_device_iotlb_desc(s, &inv_desc)) {
2070 return false;
2071 }
2072 break;
2073
Le Taned7b8fb2014-08-16 13:55:42 +08002074 default:
Peter Xubc535e52017-02-07 16:28:09 +08002075 trace_vtd_inv_desc_invalid(inv_desc.hi, inv_desc.lo);
Le Taned7b8fb2014-08-16 13:55:42 +08002076 return false;
2077 }
2078 s->iq_head++;
2079 if (s->iq_head == s->iq_size) {
2080 s->iq_head = 0;
2081 }
2082 return true;
2083}
2084
2085/* Try to fetch and process more Invalidation Descriptors */
2086static void vtd_fetch_inv_desc(IntelIOMMUState *s)
2087{
Peter Xu7feb51b2017-06-09 21:53:27 +08002088 trace_vtd_inv_qi_fetch();
2089
Le Taned7b8fb2014-08-16 13:55:42 +08002090 if (s->iq_tail >= s->iq_size) {
2091 /* Detects an invalid Tail pointer */
Peter Xu7feb51b2017-06-09 21:53:27 +08002092 trace_vtd_err_qi_tail(s->iq_tail, s->iq_size);
Le Taned7b8fb2014-08-16 13:55:42 +08002093 vtd_handle_inv_queue_error(s);
2094 return;
2095 }
2096 while (s->iq_head != s->iq_tail) {
2097 if (!vtd_process_inv_desc(s)) {
2098 /* Invalidation Queue Errors */
2099 vtd_handle_inv_queue_error(s);
2100 break;
2101 }
2102 /* Must update the IQH_REG in time */
2103 vtd_set_quad_raw(s, DMAR_IQH_REG,
2104 (((uint64_t)(s->iq_head)) << VTD_IQH_QH_SHIFT) &
2105 VTD_IQH_QH_MASK);
2106 }
2107}
2108
2109/* Handle write to Invalidation Queue Tail Register */
2110static void vtd_handle_iqt_write(IntelIOMMUState *s)
2111{
2112 uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG);
2113
2114 s->iq_tail = VTD_IQT_QT(val);
Peter Xu7feb51b2017-06-09 21:53:27 +08002115 trace_vtd_inv_qi_tail(s->iq_tail);
2116
Le Taned7b8fb2014-08-16 13:55:42 +08002117 if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
2118 /* Process Invalidation Queue here */
2119 vtd_fetch_inv_desc(s);
2120 }
2121}
2122
Le Tan1da12ec2014-08-16 13:55:38 +08002123static void vtd_handle_fsts_write(IntelIOMMUState *s)
2124{
2125 uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
2126 uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
2127 uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE;
2128
2129 if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) {
2130 vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
Peter Xu7feb51b2017-06-09 21:53:27 +08002131 trace_vtd_fsts_clear_ip();
Le Tan1da12ec2014-08-16 13:55:38 +08002132 }
Le Taned7b8fb2014-08-16 13:55:42 +08002133 /* FIXME: when IQE is Clear, should we try to fetch some Invalidation
2134 * Descriptors if there are any when Queued Invalidation is enabled?
2135 */
Le Tan1da12ec2014-08-16 13:55:38 +08002136}
2137
2138static void vtd_handle_fectl_write(IntelIOMMUState *s)
2139{
2140 uint32_t fectl_reg;
2141 /* FIXME: when software clears the IM field, check the IP field. But do we
2142 * need to compare the old value and the new value to conclude that
2143 * software clears the IM field? Or just check if the IM field is zero?
2144 */
2145 fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
Peter Xu7feb51b2017-06-09 21:53:27 +08002146
2147 trace_vtd_reg_write_fectl(fectl_reg);
2148
Le Tan1da12ec2014-08-16 13:55:38 +08002149 if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) {
2150 vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
2151 vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
Le Tan1da12ec2014-08-16 13:55:38 +08002152 }
2153}
2154
Le Taned7b8fb2014-08-16 13:55:42 +08002155static void vtd_handle_ics_write(IntelIOMMUState *s)
2156{
2157 uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG);
2158 uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
2159
2160 if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002161 trace_vtd_reg_ics_clear_ip();
Le Taned7b8fb2014-08-16 13:55:42 +08002162 vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
Le Taned7b8fb2014-08-16 13:55:42 +08002163 }
2164}
2165
2166static void vtd_handle_iectl_write(IntelIOMMUState *s)
2167{
2168 uint32_t iectl_reg;
2169 /* FIXME: when software clears the IM field, check the IP field. But do we
2170 * need to compare the old value and the new value to conclude that
2171 * software clears the IM field? Or just check if the IM field is zero?
2172 */
2173 iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
Peter Xu7feb51b2017-06-09 21:53:27 +08002174
2175 trace_vtd_reg_write_iectl(iectl_reg);
2176
Le Taned7b8fb2014-08-16 13:55:42 +08002177 if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) {
2178 vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
2179 vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
Le Taned7b8fb2014-08-16 13:55:42 +08002180 }
2181}
2182
Le Tan1da12ec2014-08-16 13:55:38 +08002183static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
2184{
2185 IntelIOMMUState *s = opaque;
2186 uint64_t val;
2187
Peter Xu7feb51b2017-06-09 21:53:27 +08002188 trace_vtd_reg_read(addr, size);
2189
Le Tan1da12ec2014-08-16 13:55:38 +08002190 if (addr + size > DMAR_REG_SIZE) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002191 trace_vtd_err("Read MMIO over range.");
Le Tan1da12ec2014-08-16 13:55:38 +08002192 return (uint64_t)-1;
2193 }
2194
2195 switch (addr) {
2196 /* Root Table Address Register, 64-bit */
2197 case DMAR_RTADDR_REG:
2198 if (size == 4) {
2199 val = s->root & ((1ULL << 32) - 1);
2200 } else {
2201 val = s->root;
2202 }
2203 break;
2204
2205 case DMAR_RTADDR_REG_HI:
2206 assert(size == 4);
2207 val = s->root >> 32;
2208 break;
2209
Le Taned7b8fb2014-08-16 13:55:42 +08002210 /* Invalidation Queue Address Register, 64-bit */
2211 case DMAR_IQA_REG:
2212 val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS);
2213 if (size == 4) {
2214 val = val & ((1ULL << 32) - 1);
2215 }
2216 break;
2217
2218 case DMAR_IQA_REG_HI:
2219 assert(size == 4);
2220 val = s->iq >> 32;
2221 break;
2222
Le Tan1da12ec2014-08-16 13:55:38 +08002223 default:
2224 if (size == 4) {
2225 val = vtd_get_long(s, addr);
2226 } else {
2227 val = vtd_get_quad(s, addr);
2228 }
2229 }
Peter Xu7feb51b2017-06-09 21:53:27 +08002230
Le Tan1da12ec2014-08-16 13:55:38 +08002231 return val;
2232}
2233
2234static void vtd_mem_write(void *opaque, hwaddr addr,
2235 uint64_t val, unsigned size)
2236{
2237 IntelIOMMUState *s = opaque;
2238
Peter Xu7feb51b2017-06-09 21:53:27 +08002239 trace_vtd_reg_write(addr, size, val);
2240
Le Tan1da12ec2014-08-16 13:55:38 +08002241 if (addr + size > DMAR_REG_SIZE) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002242 trace_vtd_err("Write MMIO over range.");
Le Tan1da12ec2014-08-16 13:55:38 +08002243 return;
2244 }
2245
2246 switch (addr) {
2247 /* Global Command Register, 32-bit */
2248 case DMAR_GCMD_REG:
Le Tan1da12ec2014-08-16 13:55:38 +08002249 vtd_set_long(s, addr, val);
2250 vtd_handle_gcmd_write(s);
2251 break;
2252
2253 /* Context Command Register, 64-bit */
2254 case DMAR_CCMD_REG:
Le Tan1da12ec2014-08-16 13:55:38 +08002255 if (size == 4) {
2256 vtd_set_long(s, addr, val);
2257 } else {
2258 vtd_set_quad(s, addr, val);
2259 vtd_handle_ccmd_write(s);
2260 }
2261 break;
2262
2263 case DMAR_CCMD_REG_HI:
Le Tan1da12ec2014-08-16 13:55:38 +08002264 assert(size == 4);
2265 vtd_set_long(s, addr, val);
2266 vtd_handle_ccmd_write(s);
2267 break;
2268
2269 /* IOTLB Invalidation Register, 64-bit */
2270 case DMAR_IOTLB_REG:
Le Tan1da12ec2014-08-16 13:55:38 +08002271 if (size == 4) {
2272 vtd_set_long(s, addr, val);
2273 } else {
2274 vtd_set_quad(s, addr, val);
2275 vtd_handle_iotlb_write(s);
2276 }
2277 break;
2278
2279 case DMAR_IOTLB_REG_HI:
Le Tan1da12ec2014-08-16 13:55:38 +08002280 assert(size == 4);
2281 vtd_set_long(s, addr, val);
2282 vtd_handle_iotlb_write(s);
2283 break;
2284
Le Tanb5a280c2014-08-16 13:55:44 +08002285 /* Invalidate Address Register, 64-bit */
2286 case DMAR_IVA_REG:
Le Tanb5a280c2014-08-16 13:55:44 +08002287 if (size == 4) {
2288 vtd_set_long(s, addr, val);
2289 } else {
2290 vtd_set_quad(s, addr, val);
2291 }
2292 break;
2293
2294 case DMAR_IVA_REG_HI:
Le Tanb5a280c2014-08-16 13:55:44 +08002295 assert(size == 4);
2296 vtd_set_long(s, addr, val);
2297 break;
2298
Le Tan1da12ec2014-08-16 13:55:38 +08002299 /* Fault Status Register, 32-bit */
2300 case DMAR_FSTS_REG:
Le Tan1da12ec2014-08-16 13:55:38 +08002301 assert(size == 4);
2302 vtd_set_long(s, addr, val);
2303 vtd_handle_fsts_write(s);
2304 break;
2305
2306 /* Fault Event Control Register, 32-bit */
2307 case DMAR_FECTL_REG:
Le Tan1da12ec2014-08-16 13:55:38 +08002308 assert(size == 4);
2309 vtd_set_long(s, addr, val);
2310 vtd_handle_fectl_write(s);
2311 break;
2312
2313 /* Fault Event Data Register, 32-bit */
2314 case DMAR_FEDATA_REG:
Le Tan1da12ec2014-08-16 13:55:38 +08002315 assert(size == 4);
2316 vtd_set_long(s, addr, val);
2317 break;
2318
2319 /* Fault Event Address Register, 32-bit */
2320 case DMAR_FEADDR_REG:
Jan Kiszkab7a7bb32018-02-24 09:30:12 +01002321 if (size == 4) {
2322 vtd_set_long(s, addr, val);
2323 } else {
2324 /*
2325 * While the register is 32-bit only, some guests (Xen...) write to
2326 * it with 64-bit.
2327 */
2328 vtd_set_quad(s, addr, val);
2329 }
Le Tan1da12ec2014-08-16 13:55:38 +08002330 break;
2331
2332 /* Fault Event Upper Address Register, 32-bit */
2333 case DMAR_FEUADDR_REG:
Le Tan1da12ec2014-08-16 13:55:38 +08002334 assert(size == 4);
2335 vtd_set_long(s, addr, val);
2336 break;
2337
2338 /* Protected Memory Enable Register, 32-bit */
2339 case DMAR_PMEN_REG:
Le Tan1da12ec2014-08-16 13:55:38 +08002340 assert(size == 4);
2341 vtd_set_long(s, addr, val);
2342 break;
2343
2344 /* Root Table Address Register, 64-bit */
2345 case DMAR_RTADDR_REG:
Le Tan1da12ec2014-08-16 13:55:38 +08002346 if (size == 4) {
2347 vtd_set_long(s, addr, val);
2348 } else {
2349 vtd_set_quad(s, addr, val);
2350 }
2351 break;
2352
2353 case DMAR_RTADDR_REG_HI:
Le Tan1da12ec2014-08-16 13:55:38 +08002354 assert(size == 4);
2355 vtd_set_long(s, addr, val);
2356 break;
2357
Le Taned7b8fb2014-08-16 13:55:42 +08002358 /* Invalidation Queue Tail Register, 64-bit */
2359 case DMAR_IQT_REG:
Le Taned7b8fb2014-08-16 13:55:42 +08002360 if (size == 4) {
2361 vtd_set_long(s, addr, val);
2362 } else {
2363 vtd_set_quad(s, addr, val);
2364 }
2365 vtd_handle_iqt_write(s);
2366 break;
2367
2368 case DMAR_IQT_REG_HI:
Le Taned7b8fb2014-08-16 13:55:42 +08002369 assert(size == 4);
2370 vtd_set_long(s, addr, val);
2371 /* 19:63 of IQT_REG is RsvdZ, do nothing here */
2372 break;
2373
2374 /* Invalidation Queue Address Register, 64-bit */
2375 case DMAR_IQA_REG:
Le Taned7b8fb2014-08-16 13:55:42 +08002376 if (size == 4) {
2377 vtd_set_long(s, addr, val);
2378 } else {
2379 vtd_set_quad(s, addr, val);
2380 }
2381 break;
2382
2383 case DMAR_IQA_REG_HI:
Le Taned7b8fb2014-08-16 13:55:42 +08002384 assert(size == 4);
2385 vtd_set_long(s, addr, val);
2386 break;
2387
2388 /* Invalidation Completion Status Register, 32-bit */
2389 case DMAR_ICS_REG:
Le Taned7b8fb2014-08-16 13:55:42 +08002390 assert(size == 4);
2391 vtd_set_long(s, addr, val);
2392 vtd_handle_ics_write(s);
2393 break;
2394
2395 /* Invalidation Event Control Register, 32-bit */
2396 case DMAR_IECTL_REG:
Le Taned7b8fb2014-08-16 13:55:42 +08002397 assert(size == 4);
2398 vtd_set_long(s, addr, val);
2399 vtd_handle_iectl_write(s);
2400 break;
2401
2402 /* Invalidation Event Data Register, 32-bit */
2403 case DMAR_IEDATA_REG:
Le Taned7b8fb2014-08-16 13:55:42 +08002404 assert(size == 4);
2405 vtd_set_long(s, addr, val);
2406 break;
2407
2408 /* Invalidation Event Address Register, 32-bit */
2409 case DMAR_IEADDR_REG:
Le Taned7b8fb2014-08-16 13:55:42 +08002410 assert(size == 4);
2411 vtd_set_long(s, addr, val);
2412 break;
2413
2414 /* Invalidation Event Upper Address Register, 32-bit */
2415 case DMAR_IEUADDR_REG:
Le Taned7b8fb2014-08-16 13:55:42 +08002416 assert(size == 4);
2417 vtd_set_long(s, addr, val);
2418 break;
2419
Le Tan1da12ec2014-08-16 13:55:38 +08002420 /* Fault Recording Registers, 128-bit */
2421 case DMAR_FRCD_REG_0_0:
Le Tan1da12ec2014-08-16 13:55:38 +08002422 if (size == 4) {
2423 vtd_set_long(s, addr, val);
2424 } else {
2425 vtd_set_quad(s, addr, val);
2426 }
2427 break;
2428
2429 case DMAR_FRCD_REG_0_1:
Le Tan1da12ec2014-08-16 13:55:38 +08002430 assert(size == 4);
2431 vtd_set_long(s, addr, val);
2432 break;
2433
2434 case DMAR_FRCD_REG_0_2:
Le Tan1da12ec2014-08-16 13:55:38 +08002435 if (size == 4) {
2436 vtd_set_long(s, addr, val);
2437 } else {
2438 vtd_set_quad(s, addr, val);
2439 /* May clear bit 127 (Fault), update PPF */
2440 vtd_update_fsts_ppf(s);
2441 }
2442 break;
2443
2444 case DMAR_FRCD_REG_0_3:
Le Tan1da12ec2014-08-16 13:55:38 +08002445 assert(size == 4);
2446 vtd_set_long(s, addr, val);
2447 /* May clear bit 127 (Fault), update PPF */
2448 vtd_update_fsts_ppf(s);
2449 break;
2450
Peter Xua5861432016-07-14 13:56:18 +08002451 case DMAR_IRTA_REG:
Peter Xua5861432016-07-14 13:56:18 +08002452 if (size == 4) {
2453 vtd_set_long(s, addr, val);
2454 } else {
2455 vtd_set_quad(s, addr, val);
2456 }
2457 break;
2458
2459 case DMAR_IRTA_REG_HI:
Peter Xua5861432016-07-14 13:56:18 +08002460 assert(size == 4);
2461 vtd_set_long(s, addr, val);
2462 break;
2463
Le Tan1da12ec2014-08-16 13:55:38 +08002464 default:
Le Tan1da12ec2014-08-16 13:55:38 +08002465 if (size == 4) {
2466 vtd_set_long(s, addr, val);
2467 } else {
2468 vtd_set_quad(s, addr, val);
2469 }
2470 }
2471}
2472
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10002473static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
Peter Maydell2c91bcf2018-06-15 14:57:16 +01002474 IOMMUAccessFlags flag, int iommu_idx)
Le Tan1da12ec2014-08-16 13:55:38 +08002475{
2476 VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
2477 IntelIOMMUState *s = vtd_as->iommu_state;
Peter Xub9313022017-06-09 21:53:28 +08002478 IOMMUTLBEntry iotlb = {
2479 /* We'll fill in the rest later. */
Le Tan1da12ec2014-08-16 13:55:38 +08002480 .target_as = &address_space_memory,
Le Tan1da12ec2014-08-16 13:55:38 +08002481 };
Peter Xub9313022017-06-09 21:53:28 +08002482 bool success;
Le Tan1da12ec2014-08-16 13:55:38 +08002483
Peter Xub9313022017-06-09 21:53:28 +08002484 if (likely(s->dmar_enabled)) {
2485 success = vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn,
2486 addr, flag & IOMMU_WO, &iotlb);
2487 } else {
Le Tan1da12ec2014-08-16 13:55:38 +08002488 /* DMAR disabled, passthrough, use 4k-page*/
Peter Xub9313022017-06-09 21:53:28 +08002489 iotlb.iova = addr & VTD_PAGE_MASK_4K;
2490 iotlb.translated_addr = addr & VTD_PAGE_MASK_4K;
2491 iotlb.addr_mask = ~VTD_PAGE_MASK_4K;
2492 iotlb.perm = IOMMU_RW;
2493 success = true;
Le Tan1da12ec2014-08-16 13:55:38 +08002494 }
2495
Peter Xub9313022017-06-09 21:53:28 +08002496 if (likely(success)) {
2497 trace_vtd_dmar_translate(pci_bus_num(vtd_as->bus),
2498 VTD_PCI_SLOT(vtd_as->devfn),
2499 VTD_PCI_FUNC(vtd_as->devfn),
2500 iotlb.iova, iotlb.translated_addr,
2501 iotlb.addr_mask);
2502 } else {
2503 trace_vtd_err_dmar_translate(pci_bus_num(vtd_as->bus),
2504 VTD_PCI_SLOT(vtd_as->devfn),
2505 VTD_PCI_FUNC(vtd_as->devfn),
2506 iotlb.iova);
2507 }
Peter Xu7feb51b2017-06-09 21:53:27 +08002508
Peter Xub9313022017-06-09 21:53:28 +08002509 return iotlb;
Le Tan1da12ec2014-08-16 13:55:38 +08002510}
2511
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10002512static void vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
Peter Xu5bf3d312016-09-23 13:02:27 +08002513 IOMMUNotifierFlag old,
2514 IOMMUNotifierFlag new)
Alex Williamson3cb3b152016-06-30 13:00:24 -06002515{
2516 VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
Peter Xudd4d6072017-04-07 18:59:15 +08002517 IntelIOMMUState *s = vtd_as->iommu_state;
Alex Williamson3cb3b152016-06-30 13:00:24 -06002518
Peter Xudd4d6072017-04-07 18:59:15 +08002519 if (!s->caching_mode && new & IOMMU_NOTIFIER_MAP) {
Peter Xu4c427a42017-12-08 12:26:54 +08002520 error_report("We need to set caching-mode=1 for intel-iommu to enable "
Peter Xudd4d6072017-04-07 18:59:15 +08002521 "device assignment with IOMMU protection.");
Peter Xua3276f72016-09-23 13:02:28 +08002522 exit(1);
2523 }
Peter Xudd4d6072017-04-07 18:59:15 +08002524
Peter Xu4f8a62a2018-05-18 15:25:12 +08002525 /* Update per-address-space notifier flags */
2526 vtd_as->notifier_flags = new;
2527
Peter Xudd4d6072017-04-07 18:59:15 +08002528 if (old == IOMMU_NOTIFIER_NONE) {
Peter Xub4a4ba02018-05-18 15:25:10 +08002529 QLIST_INSERT_HEAD(&s->vtd_as_with_notifiers, vtd_as, next);
2530 } else if (new == IOMMU_NOTIFIER_NONE) {
2531 QLIST_REMOVE(vtd_as, next);
Peter Xudd4d6072017-04-07 18:59:15 +08002532 }
Alex Williamson3cb3b152016-06-30 13:00:24 -06002533}
2534
Peter Xu552a1e02017-06-30 15:24:38 +08002535static int vtd_post_load(void *opaque, int version_id)
2536{
2537 IntelIOMMUState *iommu = opaque;
2538
2539 /*
2540 * Memory regions are dynamically turned on/off depending on
2541 * context entry configurations from the guest. After migration,
2542 * we need to make sure the memory regions are still correct.
2543 */
2544 vtd_switch_address_space_all(iommu);
2545
2546 return 0;
2547}
2548
Le Tan1da12ec2014-08-16 13:55:38 +08002549static const VMStateDescription vtd_vmstate = {
2550 .name = "iommu-intel",
Peter Xu8cdcf3c2017-01-06 12:06:13 +08002551 .version_id = 1,
2552 .minimum_version_id = 1,
2553 .priority = MIG_PRI_IOMMU,
Peter Xu552a1e02017-06-30 15:24:38 +08002554 .post_load = vtd_post_load,
Peter Xu8cdcf3c2017-01-06 12:06:13 +08002555 .fields = (VMStateField[]) {
2556 VMSTATE_UINT64(root, IntelIOMMUState),
2557 VMSTATE_UINT64(intr_root, IntelIOMMUState),
2558 VMSTATE_UINT64(iq, IntelIOMMUState),
2559 VMSTATE_UINT32(intr_size, IntelIOMMUState),
2560 VMSTATE_UINT16(iq_head, IntelIOMMUState),
2561 VMSTATE_UINT16(iq_tail, IntelIOMMUState),
2562 VMSTATE_UINT16(iq_size, IntelIOMMUState),
2563 VMSTATE_UINT16(next_frcd_reg, IntelIOMMUState),
2564 VMSTATE_UINT8_ARRAY(csr, IntelIOMMUState, DMAR_REG_SIZE),
2565 VMSTATE_UINT8(iq_last_desc_type, IntelIOMMUState),
2566 VMSTATE_BOOL(root_extended, IntelIOMMUState),
2567 VMSTATE_BOOL(dmar_enabled, IntelIOMMUState),
2568 VMSTATE_BOOL(qi_enabled, IntelIOMMUState),
2569 VMSTATE_BOOL(intr_enabled, IntelIOMMUState),
2570 VMSTATE_BOOL(intr_eime, IntelIOMMUState),
2571 VMSTATE_END_OF_LIST()
2572 }
Le Tan1da12ec2014-08-16 13:55:38 +08002573};
2574
2575static const MemoryRegionOps vtd_mem_ops = {
2576 .read = vtd_mem_read,
2577 .write = vtd_mem_write,
2578 .endianness = DEVICE_LITTLE_ENDIAN,
2579 .impl = {
2580 .min_access_size = 4,
2581 .max_access_size = 8,
2582 },
2583 .valid = {
2584 .min_access_size = 4,
2585 .max_access_size = 8,
2586 },
2587};
2588
2589static Property vtd_properties[] = {
2590 DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
Radim Krčmáře6b6af02016-10-10 17:28:46 +02002591 DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState, intr_eim,
2592 ON_OFF_AUTO_AUTO),
Radim Krčmářfb506e72016-10-10 17:28:47 +02002593 DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
Prasad Singamsetty37f51382017-11-14 18:13:50 -05002594 DEFINE_PROP_UINT8("x-aw-bits", IntelIOMMUState, aw_bits,
2595 VTD_HOST_ADDRESS_WIDTH),
Aviv Ben-David3b40f0e2017-02-07 16:28:06 +08002596 DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
Le Tan1da12ec2014-08-16 13:55:38 +08002597 DEFINE_PROP_END_OF_LIST(),
2598};
2599
Peter Xu651e4ce2016-07-14 13:56:22 +08002600/* Read IRTE entry with specific index */
2601static int vtd_irte_get(IntelIOMMUState *iommu, uint16_t index,
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002602 VTD_IR_TableEntry *entry, uint16_t sid)
Peter Xu651e4ce2016-07-14 13:56:22 +08002603{
Peter Xuede9c942016-07-14 13:56:29 +08002604 static const uint16_t vtd_svt_mask[VTD_SQ_MAX] = \
2605 {0xffff, 0xfffb, 0xfff9, 0xfff8};
Peter Xu651e4ce2016-07-14 13:56:22 +08002606 dma_addr_t addr = 0x00;
Peter Xuede9c942016-07-14 13:56:29 +08002607 uint16_t mask, source_id;
2608 uint8_t bus, bus_max, bus_min;
Peter Xu651e4ce2016-07-14 13:56:22 +08002609
2610 addr = iommu->intr_root + index * sizeof(*entry);
2611 if (dma_memory_read(&address_space_memory, addr, entry,
2612 sizeof(*entry))) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002613 trace_vtd_err("Memory read failed for IRTE.");
Peter Xu651e4ce2016-07-14 13:56:22 +08002614 return -VTD_FR_IR_ROOT_INVAL;
2615 }
2616
Peter Xu7feb51b2017-06-09 21:53:27 +08002617 trace_vtd_ir_irte_get(index, le64_to_cpu(entry->data[1]),
2618 le64_to_cpu(entry->data[0]));
2619
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002620 if (!entry->irte.present) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002621 trace_vtd_err_irte(index, le64_to_cpu(entry->data[1]),
2622 le64_to_cpu(entry->data[0]));
Peter Xu651e4ce2016-07-14 13:56:22 +08002623 return -VTD_FR_IR_ENTRY_P;
2624 }
2625
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002626 if (entry->irte.__reserved_0 || entry->irte.__reserved_1 ||
2627 entry->irte.__reserved_2) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002628 trace_vtd_err_irte(index, le64_to_cpu(entry->data[1]),
2629 le64_to_cpu(entry->data[0]));
Peter Xu651e4ce2016-07-14 13:56:22 +08002630 return -VTD_FR_IR_IRTE_RSVD;
2631 }
2632
Peter Xuede9c942016-07-14 13:56:29 +08002633 if (sid != X86_IOMMU_SID_INVALID) {
2634 /* Validate IRTE SID */
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002635 source_id = le32_to_cpu(entry->irte.source_id);
2636 switch (entry->irte.sid_vtype) {
Peter Xuede9c942016-07-14 13:56:29 +08002637 case VTD_SVT_NONE:
Peter Xuede9c942016-07-14 13:56:29 +08002638 break;
2639
2640 case VTD_SVT_ALL:
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002641 mask = vtd_svt_mask[entry->irte.sid_q];
Peter Xuede9c942016-07-14 13:56:29 +08002642 if ((source_id & mask) != (sid & mask)) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002643 trace_vtd_err_irte_sid(index, sid, source_id);
Peter Xuede9c942016-07-14 13:56:29 +08002644 return -VTD_FR_IR_SID_ERR;
2645 }
2646 break;
2647
2648 case VTD_SVT_BUS:
2649 bus_max = source_id >> 8;
2650 bus_min = source_id & 0xff;
2651 bus = sid >> 8;
2652 if (bus > bus_max || bus < bus_min) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002653 trace_vtd_err_irte_sid_bus(index, bus, bus_min, bus_max);
Peter Xuede9c942016-07-14 13:56:29 +08002654 return -VTD_FR_IR_SID_ERR;
2655 }
2656 break;
2657
2658 default:
Peter Xu7feb51b2017-06-09 21:53:27 +08002659 trace_vtd_err_irte_svt(index, entry->irte.sid_vtype);
Peter Xuede9c942016-07-14 13:56:29 +08002660 /* Take this as verification failure. */
2661 return -VTD_FR_IR_SID_ERR;
2662 break;
2663 }
2664 }
Peter Xu651e4ce2016-07-14 13:56:22 +08002665
2666 return 0;
2667}
2668
2669/* Fetch IRQ information of specific IR index */
Peter Xuede9c942016-07-14 13:56:29 +08002670static int vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index,
2671 VTDIrq *irq, uint16_t sid)
Peter Xu651e4ce2016-07-14 13:56:22 +08002672{
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002673 VTD_IR_TableEntry irte = {};
Peter Xu651e4ce2016-07-14 13:56:22 +08002674 int ret = 0;
2675
Peter Xuede9c942016-07-14 13:56:29 +08002676 ret = vtd_irte_get(iommu, index, &irte, sid);
Peter Xu651e4ce2016-07-14 13:56:22 +08002677 if (ret) {
2678 return ret;
2679 }
2680
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002681 irq->trigger_mode = irte.irte.trigger_mode;
2682 irq->vector = irte.irte.vector;
2683 irq->delivery_mode = irte.irte.delivery_mode;
2684 irq->dest = le32_to_cpu(irte.irte.dest_id);
Jan Kiszka28589312016-07-14 13:56:28 +08002685 if (!iommu->intr_eime) {
Peter Xu651e4ce2016-07-14 13:56:22 +08002686#define VTD_IR_APIC_DEST_MASK (0xff00ULL)
2687#define VTD_IR_APIC_DEST_SHIFT (8)
Jan Kiszka28589312016-07-14 13:56:28 +08002688 irq->dest = (irq->dest & VTD_IR_APIC_DEST_MASK) >>
2689 VTD_IR_APIC_DEST_SHIFT;
2690 }
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002691 irq->dest_mode = irte.irte.dest_mode;
2692 irq->redir_hint = irte.irte.redir_hint;
Peter Xu651e4ce2016-07-14 13:56:22 +08002693
Peter Xu7feb51b2017-06-09 21:53:27 +08002694 trace_vtd_ir_remap(index, irq->trigger_mode, irq->vector,
2695 irq->delivery_mode, irq->dest, irq->dest_mode);
Peter Xu651e4ce2016-07-14 13:56:22 +08002696
2697 return 0;
2698}
2699
2700/* Generate one MSI message from VTDIrq info */
2701static void vtd_generate_msi_message(VTDIrq *irq, MSIMessage *msg_out)
2702{
2703 VTD_MSIMessage msg = {};
2704
2705 /* Generate address bits */
2706 msg.dest_mode = irq->dest_mode;
2707 msg.redir_hint = irq->redir_hint;
2708 msg.dest = irq->dest;
Radim Krčmář32946012016-10-10 17:28:44 +02002709 msg.__addr_hi = irq->dest & 0xffffff00;
Peter Xu651e4ce2016-07-14 13:56:22 +08002710 msg.__addr_head = cpu_to_le32(0xfee);
2711 /* Keep this from original MSI address bits */
2712 msg.__not_used = irq->msi_addr_last_bits;
2713
2714 /* Generate data bits */
2715 msg.vector = irq->vector;
2716 msg.delivery_mode = irq->delivery_mode;
2717 msg.level = 1;
2718 msg.trigger_mode = irq->trigger_mode;
2719
2720 msg_out->address = msg.msi_addr;
2721 msg_out->data = msg.msi_data;
2722}
2723
2724/* Interrupt remapping for MSI/MSI-X entry */
2725static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu,
2726 MSIMessage *origin,
Peter Xuede9c942016-07-14 13:56:29 +08002727 MSIMessage *translated,
2728 uint16_t sid)
Peter Xu651e4ce2016-07-14 13:56:22 +08002729{
2730 int ret = 0;
2731 VTD_IR_MSIAddress addr;
2732 uint16_t index;
Michael S. Tsirkin09cd0582016-07-21 18:42:42 +03002733 VTDIrq irq = {};
Peter Xu651e4ce2016-07-14 13:56:22 +08002734
2735 assert(origin && translated);
2736
Peter Xu7feb51b2017-06-09 21:53:27 +08002737 trace_vtd_ir_remap_msi_req(origin->address, origin->data);
2738
Peter Xu651e4ce2016-07-14 13:56:22 +08002739 if (!iommu || !iommu->intr_enabled) {
Peter Xue7a3b912017-06-09 21:53:29 +08002740 memcpy(translated, origin, sizeof(*origin));
2741 goto out;
Peter Xu651e4ce2016-07-14 13:56:22 +08002742 }
2743
2744 if (origin->address & VTD_MSI_ADDR_HI_MASK) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002745 trace_vtd_err("MSI address high 32 bits non-zero when "
2746 "Interrupt Remapping enabled.");
Peter Xu651e4ce2016-07-14 13:56:22 +08002747 return -VTD_FR_IR_REQ_RSVD;
2748 }
2749
2750 addr.data = origin->address & VTD_MSI_ADDR_LO_MASK;
Peter Xu1a437132016-10-31 15:34:38 +08002751 if (addr.addr.__head != 0xfee) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002752 trace_vtd_err("MSI addr low 32 bit invalid.");
Peter Xu651e4ce2016-07-14 13:56:22 +08002753 return -VTD_FR_IR_REQ_RSVD;
2754 }
2755
2756 /* This is compatible mode. */
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002757 if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) {
Peter Xue7a3b912017-06-09 21:53:29 +08002758 memcpy(translated, origin, sizeof(*origin));
2759 goto out;
Peter Xu651e4ce2016-07-14 13:56:22 +08002760 }
2761
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002762 index = addr.addr.index_h << 15 | le16_to_cpu(addr.addr.index_l);
Peter Xu651e4ce2016-07-14 13:56:22 +08002763
2764#define VTD_IR_MSI_DATA_SUBHANDLE (0x0000ffff)
2765#define VTD_IR_MSI_DATA_RESERVED (0xffff0000)
2766
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002767 if (addr.addr.sub_valid) {
Peter Xu651e4ce2016-07-14 13:56:22 +08002768 /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */
2769 index += origin->data & VTD_IR_MSI_DATA_SUBHANDLE;
2770 }
2771
Peter Xuede9c942016-07-14 13:56:29 +08002772 ret = vtd_remap_irq_get(iommu, index, &irq, sid);
Peter Xu651e4ce2016-07-14 13:56:22 +08002773 if (ret) {
2774 return ret;
2775 }
2776
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002777 if (addr.addr.sub_valid) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002778 trace_vtd_ir_remap_type("MSI");
Peter Xu651e4ce2016-07-14 13:56:22 +08002779 if (origin->data & VTD_IR_MSI_DATA_RESERVED) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002780 trace_vtd_err_ir_msi_invalid(sid, origin->address, origin->data);
Peter Xu651e4ce2016-07-14 13:56:22 +08002781 return -VTD_FR_IR_REQ_RSVD;
2782 }
2783 } else {
2784 uint8_t vector = origin->data & 0xff;
Feng Wudea651a2016-09-22 00:12:17 +08002785 uint8_t trigger_mode = (origin->data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
2786
Peter Xu7feb51b2017-06-09 21:53:27 +08002787 trace_vtd_ir_remap_type("IOAPIC");
Peter Xu651e4ce2016-07-14 13:56:22 +08002788 /* IOAPIC entry vector should be aligned with IRTE vector
2789 * (see vt-d spec 5.1.5.1). */
2790 if (vector != irq.vector) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002791 trace_vtd_warn_ir_vector(sid, index, vector, irq.vector);
Peter Xu651e4ce2016-07-14 13:56:22 +08002792 }
Feng Wudea651a2016-09-22 00:12:17 +08002793
2794 /* The Trigger Mode field must match the Trigger Mode in the IRTE.
2795 * (see vt-d spec 5.1.5.1). */
2796 if (trigger_mode != irq.trigger_mode) {
Peter Xu7feb51b2017-06-09 21:53:27 +08002797 trace_vtd_warn_ir_trigger(sid, index, trigger_mode,
2798 irq.trigger_mode);
Feng Wudea651a2016-09-22 00:12:17 +08002799 }
Peter Xu651e4ce2016-07-14 13:56:22 +08002800 }
2801
2802 /*
2803 * We'd better keep the last two bits, assuming that guest OS
2804 * might modify it. Keep it does not hurt after all.
2805 */
Michael S. Tsirkinbc38ee12016-07-21 18:54:10 +03002806 irq.msi_addr_last_bits = addr.addr.__not_care;
Peter Xu651e4ce2016-07-14 13:56:22 +08002807
2808 /* Translate VTDIrq to MSI message */
2809 vtd_generate_msi_message(&irq, translated);
2810
Peter Xue7a3b912017-06-09 21:53:29 +08002811out:
Peter Xu7feb51b2017-06-09 21:53:27 +08002812 trace_vtd_ir_remap_msi(origin->address, origin->data,
2813 translated->address, translated->data);
Peter Xu651e4ce2016-07-14 13:56:22 +08002814 return 0;
2815}
2816
Peter Xu8b5ed7d2016-07-14 13:56:25 +08002817static int vtd_int_remap(X86IOMMUState *iommu, MSIMessage *src,
2818 MSIMessage *dst, uint16_t sid)
2819{
Peter Xuede9c942016-07-14 13:56:29 +08002820 return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu),
2821 src, dst, sid);
Peter Xu8b5ed7d2016-07-14 13:56:25 +08002822}
2823
Peter Xu651e4ce2016-07-14 13:56:22 +08002824static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr,
2825 uint64_t *data, unsigned size,
2826 MemTxAttrs attrs)
2827{
2828 return MEMTX_OK;
2829}
2830
2831static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,
2832 uint64_t value, unsigned size,
2833 MemTxAttrs attrs)
2834{
2835 int ret = 0;
Michael S. Tsirkin09cd0582016-07-21 18:42:42 +03002836 MSIMessage from = {}, to = {};
Peter Xuede9c942016-07-14 13:56:29 +08002837 uint16_t sid = X86_IOMMU_SID_INVALID;
Peter Xu651e4ce2016-07-14 13:56:22 +08002838
2839 from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST;
2840 from.data = (uint32_t) value;
2841
Peter Xuede9c942016-07-14 13:56:29 +08002842 if (!attrs.unspecified) {
2843 /* We have explicit Source ID */
2844 sid = attrs.requester_id;
2845 }
2846
2847 ret = vtd_interrupt_remap_msi(opaque, &from, &to, sid);
Peter Xu651e4ce2016-07-14 13:56:22 +08002848 if (ret) {
2849 /* TODO: report error */
Peter Xu651e4ce2016-07-14 13:56:22 +08002850 /* Drop this interrupt */
2851 return MEMTX_ERROR;
2852 }
2853
Radim Krčmář32946012016-10-10 17:28:44 +02002854 apic_get_class()->send_msi(&to);
Peter Xu651e4ce2016-07-14 13:56:22 +08002855
2856 return MEMTX_OK;
2857}
2858
2859static const MemoryRegionOps vtd_mem_ir_ops = {
2860 .read_with_attrs = vtd_mem_ir_read,
2861 .write_with_attrs = vtd_mem_ir_write,
2862 .endianness = DEVICE_LITTLE_ENDIAN,
2863 .impl = {
2864 .min_access_size = 4,
2865 .max_access_size = 4,
2866 },
2867 .valid = {
2868 .min_access_size = 4,
2869 .max_access_size = 4,
2870 },
2871};
Knut Omang7df953b2015-10-04 15:48:50 +02002872
2873VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)
2874{
2875 uintptr_t key = (uintptr_t)bus;
2876 VTDBus *vtd_bus = g_hash_table_lookup(s->vtd_as_by_busptr, &key);
2877 VTDAddressSpace *vtd_dev_as;
Jason Wange0a3c8c2016-12-30 18:09:11 +08002878 char name[128];
Knut Omang7df953b2015-10-04 15:48:50 +02002879
2880 if (!vtd_bus) {
Jason Wang2d3fc582016-12-30 18:09:12 +08002881 uintptr_t *new_key = g_malloc(sizeof(*new_key));
2882 *new_key = (uintptr_t)bus;
Knut Omang7df953b2015-10-04 15:48:50 +02002883 /* No corresponding free() */
Peter Xu04af0e12016-07-14 13:56:11 +08002884 vtd_bus = g_malloc0(sizeof(VTDBus) + sizeof(VTDAddressSpace *) * \
Peter Xubf33cc72017-12-08 12:26:53 +08002885 PCI_DEVFN_MAX);
Knut Omang7df953b2015-10-04 15:48:50 +02002886 vtd_bus->bus = bus;
Jason Wang2d3fc582016-12-30 18:09:12 +08002887 g_hash_table_insert(s->vtd_as_by_busptr, new_key, vtd_bus);
Knut Omang7df953b2015-10-04 15:48:50 +02002888 }
2889
2890 vtd_dev_as = vtd_bus->dev_as[devfn];
2891
2892 if (!vtd_dev_as) {
Jason Wange0a3c8c2016-12-30 18:09:11 +08002893 snprintf(name, sizeof(name), "intel_iommu_devfn_%d", devfn);
Knut Omang7df953b2015-10-04 15:48:50 +02002894 vtd_bus->dev_as[devfn] = vtd_dev_as = g_malloc0(sizeof(VTDAddressSpace));
2895
2896 vtd_dev_as->bus = bus;
2897 vtd_dev_as->devfn = (uint8_t)devfn;
2898 vtd_dev_as->iommu_state = s;
2899 vtd_dev_as->context_cache_entry.context_cache_gen = 0;
Peter Xu63b88962018-05-18 15:25:17 +08002900 vtd_dev_as->iova_tree = iova_tree_new();
Peter Xu558e0022017-04-07 18:59:14 +08002901
2902 /*
2903 * Memory region relationships looks like (Address range shows
2904 * only lower 32 bits to make it short in length...):
2905 *
2906 * |-----------------+-------------------+----------|
2907 * | Name | Address range | Priority |
2908 * |-----------------+-------------------+----------+
2909 * | vtd_root | 00000000-ffffffff | 0 |
2910 * | intel_iommu | 00000000-ffffffff | 1 |
2911 * | vtd_sys_alias | 00000000-ffffffff | 1 |
2912 * | intel_iommu_ir | fee00000-feefffff | 64 |
2913 * |-----------------+-------------------+----------|
2914 *
2915 * We enable/disable DMAR by switching enablement for
2916 * vtd_sys_alias and intel_iommu regions. IR region is always
2917 * enabled.
2918 */
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +10002919 memory_region_init_iommu(&vtd_dev_as->iommu, sizeof(vtd_dev_as->iommu),
2920 TYPE_INTEL_IOMMU_MEMORY_REGION, OBJECT(s),
2921 "intel_iommu_dmar",
Peter Xu558e0022017-04-07 18:59:14 +08002922 UINT64_MAX);
2923 memory_region_init_alias(&vtd_dev_as->sys_alias, OBJECT(s),
2924 "vtd_sys_alias", get_system_memory(),
2925 0, memory_region_size(get_system_memory()));
Peter Xu651e4ce2016-07-14 13:56:22 +08002926 memory_region_init_io(&vtd_dev_as->iommu_ir, OBJECT(s),
2927 &vtd_mem_ir_ops, s, "intel_iommu_ir",
2928 VTD_INTERRUPT_ADDR_SIZE);
Peter Xu558e0022017-04-07 18:59:14 +08002929 memory_region_init(&vtd_dev_as->root, OBJECT(s),
2930 "vtd_root", UINT64_MAX);
2931 memory_region_add_subregion_overlap(&vtd_dev_as->root,
2932 VTD_INTERRUPT_ADDR_FIRST,
2933 &vtd_dev_as->iommu_ir, 64);
2934 address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, name);
2935 memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
2936 &vtd_dev_as->sys_alias, 1);
2937 memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10002938 MEMORY_REGION(&vtd_dev_as->iommu),
2939 1);
Peter Xu558e0022017-04-07 18:59:14 +08002940 vtd_switch_address_space(vtd_dev_as);
Knut Omang7df953b2015-10-04 15:48:50 +02002941 }
2942 return vtd_dev_as;
2943}
2944
Peter Xudd4d6072017-04-07 18:59:15 +08002945/* Unmap the whole range in the notifier's scope. */
2946static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
2947{
2948 IOMMUTLBEntry entry;
2949 hwaddr size;
2950 hwaddr start = n->start;
2951 hwaddr end = n->end;
Prasad Singamsetty37f51382017-11-14 18:13:50 -05002952 IntelIOMMUState *s = as->iommu_state;
Peter Xu63b88962018-05-18 15:25:17 +08002953 DMAMap map;
Peter Xudd4d6072017-04-07 18:59:15 +08002954
2955 /*
2956 * Note: all the codes in this function has a assumption that IOVA
2957 * bits are no more than VTD_MGAW bits (which is restricted by
2958 * VT-d spec), otherwise we need to consider overflow of 64 bits.
2959 */
2960
Prasad Singamsetty37f51382017-11-14 18:13:50 -05002961 if (end > VTD_ADDRESS_SIZE(s->aw_bits)) {
Peter Xudd4d6072017-04-07 18:59:15 +08002962 /*
2963 * Don't need to unmap regions that is bigger than the whole
2964 * VT-d supported address space size
2965 */
Prasad Singamsetty37f51382017-11-14 18:13:50 -05002966 end = VTD_ADDRESS_SIZE(s->aw_bits);
Peter Xudd4d6072017-04-07 18:59:15 +08002967 }
2968
2969 assert(start <= end);
2970 size = end - start;
2971
2972 if (ctpop64(size) != 1) {
2973 /*
2974 * This size cannot format a correct mask. Let's enlarge it to
2975 * suite the minimum available mask.
2976 */
2977 int n = 64 - clz64(size);
Prasad Singamsetty37f51382017-11-14 18:13:50 -05002978 if (n > s->aw_bits) {
Peter Xudd4d6072017-04-07 18:59:15 +08002979 /* should not happen, but in case it happens, limit it */
Prasad Singamsetty37f51382017-11-14 18:13:50 -05002980 n = s->aw_bits;
Peter Xudd4d6072017-04-07 18:59:15 +08002981 }
2982 size = 1ULL << n;
2983 }
2984
2985 entry.target_as = &address_space_memory;
2986 /* Adjust iova for the size */
2987 entry.iova = n->start & ~(size - 1);
2988 /* This field is meaningless for unmap */
2989 entry.translated_addr = 0;
2990 entry.perm = IOMMU_NONE;
2991 entry.addr_mask = size - 1;
2992
2993 trace_vtd_as_unmap_whole(pci_bus_num(as->bus),
2994 VTD_PCI_SLOT(as->devfn),
2995 VTD_PCI_FUNC(as->devfn),
2996 entry.iova, size);
2997
Peter Xu63b88962018-05-18 15:25:17 +08002998 map.iova = entry.iova;
2999 map.size = entry.addr_mask;
3000 iova_tree_remove(as->iova_tree, &map);
3001
Peter Xudd4d6072017-04-07 18:59:15 +08003002 memory_region_notify_one(n, &entry);
3003}
3004
3005static void vtd_address_space_unmap_all(IntelIOMMUState *s)
3006{
Peter Xudd4d6072017-04-07 18:59:15 +08003007 VTDAddressSpace *vtd_as;
3008 IOMMUNotifier *n;
3009
Peter Xub4a4ba02018-05-18 15:25:10 +08003010 QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
Peter Xudd4d6072017-04-07 18:59:15 +08003011 IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
3012 vtd_address_space_unmap(vtd_as, n);
3013 }
3014 }
3015}
3016
Peter Xuf06a6962017-04-07 18:59:13 +08003017static int vtd_replay_hook(IOMMUTLBEntry *entry, void *private)
3018{
3019 memory_region_notify_one((IOMMUNotifier *)private, entry);
3020 return 0;
3021}
3022
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10003023static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
Peter Xuf06a6962017-04-07 18:59:13 +08003024{
Alexey Kardashevskiy3df9d742017-07-11 13:56:19 +10003025 VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu);
Peter Xuf06a6962017-04-07 18:59:13 +08003026 IntelIOMMUState *s = vtd_as->iommu_state;
3027 uint8_t bus_n = pci_bus_num(vtd_as->bus);
3028 VTDContextEntry ce;
3029
Peter Xudd4d6072017-04-07 18:59:15 +08003030 /*
3031 * The replay can be triggered by either a invalidation or a newly
3032 * created entry. No matter what, we release existing mappings
3033 * (it means flushing caches for UNMAP-only registers).
3034 */
3035 vtd_address_space_unmap(vtd_as, n);
3036
Peter Xuf06a6962017-04-07 18:59:13 +08003037 if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
Peter Xuf06a6962017-04-07 18:59:13 +08003038 trace_vtd_replay_ce_valid(bus_n, PCI_SLOT(vtd_as->devfn),
3039 PCI_FUNC(vtd_as->devfn),
3040 VTD_CONTEXT_ENTRY_DID(ce.hi),
3041 ce.hi, ce.lo);
Peter Xu4f8a62a2018-05-18 15:25:12 +08003042 if (vtd_as_has_map_notifier(vtd_as)) {
3043 /* This is required only for MAP typed notifiers */
Peter Xufe215b02018-05-18 15:25:13 +08003044 vtd_page_walk_info info = {
3045 .hook_fn = vtd_replay_hook,
3046 .private = (void *)n,
3047 .notify_unmap = false,
3048 .aw = s->aw_bits,
Peter Xu2f764fa2018-05-18 15:25:14 +08003049 .as = vtd_as,
Peter Xud118c062018-05-18 15:25:15 +08003050 .domain_id = VTD_CONTEXT_ENTRY_DID(ce.hi),
Peter Xufe215b02018-05-18 15:25:13 +08003051 };
3052
3053 vtd_page_walk(&ce, 0, ~0ULL, &info);
Peter Xu4f8a62a2018-05-18 15:25:12 +08003054 }
Peter Xuf06a6962017-04-07 18:59:13 +08003055 } else {
3056 trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
3057 PCI_FUNC(vtd_as->devfn));
3058 }
3059
3060 return;
3061}
3062
Le Tan1da12ec2014-08-16 13:55:38 +08003063/* Do the initialization. It will also be called when reset, so pay
3064 * attention when adding new initialization stuff.
3065 */
3066static void vtd_init(IntelIOMMUState *s)
3067{
Peter Xud54bd7f2016-07-14 13:56:16 +08003068 X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
3069
Le Tan1da12ec2014-08-16 13:55:38 +08003070 memset(s->csr, 0, DMAR_REG_SIZE);
3071 memset(s->wmask, 0, DMAR_REG_SIZE);
3072 memset(s->w1cmask, 0, DMAR_REG_SIZE);
3073 memset(s->womask, 0, DMAR_REG_SIZE);
3074
Le Tan1da12ec2014-08-16 13:55:38 +08003075 s->root = 0;
3076 s->root_extended = false;
3077 s->dmar_enabled = false;
3078 s->iq_head = 0;
3079 s->iq_tail = 0;
3080 s->iq = 0;
3081 s->iq_size = 0;
3082 s->qi_enabled = false;
3083 s->iq_last_desc_type = VTD_INV_DESC_NONE;
3084 s->next_frcd_reg = 0;
Prasad Singamsetty92e5d852017-11-14 18:13:49 -05003085 s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND |
3086 VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS |
Prasad Singamsetty37f51382017-11-14 18:13:50 -05003087 VTD_CAP_SAGAW_39bit | VTD_CAP_MGAW(s->aw_bits);
3088 if (s->aw_bits == VTD_HOST_AW_48BIT) {
3089 s->cap |= VTD_CAP_SAGAW_48bit;
3090 }
Le Taned7b8fb2014-08-16 13:55:42 +08003091 s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
Le Tan1da12ec2014-08-16 13:55:38 +08003092
Prasad Singamsetty92e5d852017-11-14 18:13:49 -05003093 /*
3094 * Rsvd field masks for spte
3095 */
3096 vtd_paging_entry_rsvd_field[0] = ~0ULL;
Prasad Singamsetty37f51382017-11-14 18:13:50 -05003097 vtd_paging_entry_rsvd_field[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits);
3098 vtd_paging_entry_rsvd_field[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
3099 vtd_paging_entry_rsvd_field[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
3100 vtd_paging_entry_rsvd_field[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
3101 vtd_paging_entry_rsvd_field[5] = VTD_SPTE_LPAGE_L1_RSVD_MASK(s->aw_bits);
3102 vtd_paging_entry_rsvd_field[6] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits);
3103 vtd_paging_entry_rsvd_field[7] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits);
3104 vtd_paging_entry_rsvd_field[8] = VTD_SPTE_LPAGE_L4_RSVD_MASK(s->aw_bits);
Prasad Singamsetty92e5d852017-11-14 18:13:49 -05003105
Peter Xud54bd7f2016-07-14 13:56:16 +08003106 if (x86_iommu->intr_supported) {
Radim Krčmáře6b6af02016-10-10 17:28:46 +02003107 s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV;
3108 if (s->intr_eim == ON_OFF_AUTO_ON) {
3109 s->ecap |= VTD_ECAP_EIM;
3110 }
3111 assert(s->intr_eim != ON_OFF_AUTO_AUTO);
Peter Xud54bd7f2016-07-14 13:56:16 +08003112 }
3113
Jason Wang554f5e12016-12-30 18:09:14 +08003114 if (x86_iommu->dt_supported) {
3115 s->ecap |= VTD_ECAP_DT;
3116 }
3117
Peter Xudbaabb22017-05-19 11:19:47 +08003118 if (x86_iommu->pt_supported) {
3119 s->ecap |= VTD_ECAP_PT;
3120 }
3121
Aviv Ben-David3b40f0e2017-02-07 16:28:06 +08003122 if (s->caching_mode) {
3123 s->cap |= VTD_CAP_CM;
3124 }
3125
Peter Xu1d9efa72018-05-18 15:25:11 +08003126 vtd_iommu_lock(s);
3127 vtd_reset_context_cache_locked(s);
3128 vtd_reset_iotlb_locked(s);
3129 vtd_iommu_unlock(s);
Le Tand92fa2d2014-08-16 13:55:43 +08003130
Le Tan1da12ec2014-08-16 13:55:38 +08003131 /* Define registers with default values and bit semantics */
3132 vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
3133 vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
3134 vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
3135 vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
3136 vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
3137 vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
3138 vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffff000ULL, 0);
3139 vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0);
3140 vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL);
3141
3142 /* Advanced Fault Logging not supported */
3143 vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL);
3144 vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0);
3145 vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0);
3146 vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0);
3147
3148 /* Treated as RsvdZ when EIM in ECAP_REG is not supported
3149 * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0);
3150 */
3151 vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0);
3152
3153 /* Treated as RO for implementations that PLMR and PHMR fields reported
3154 * as Clear in the CAP_REG.
3155 * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0);
3156 */
3157 vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0);
3158
Le Taned7b8fb2014-08-16 13:55:42 +08003159 vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0);
3160 vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0);
3161 vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff007ULL, 0);
3162 vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL);
3163 vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0);
3164 vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0);
3165 vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0);
3166 /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */
3167 vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0);
3168
Le Tan1da12ec2014-08-16 13:55:38 +08003169 /* IOTLB registers */
3170 vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0);
3171 vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0);
3172 vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL);
3173
3174 /* Fault Recording Registers, 128-bit */
3175 vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0);
3176 vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL);
Peter Xua5861432016-07-14 13:56:18 +08003177
3178 /*
Jan Kiszka28589312016-07-14 13:56:28 +08003179 * Interrupt remapping registers.
Peter Xua5861432016-07-14 13:56:18 +08003180 */
Jan Kiszka28589312016-07-14 13:56:28 +08003181 vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0);
Le Tan1da12ec2014-08-16 13:55:38 +08003182}
3183
3184/* Should not reset address_spaces when reset because devices will still use
3185 * the address space they got at first (won't ask the bus again).
3186 */
3187static void vtd_reset(DeviceState *dev)
3188{
3189 IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
3190
Le Tan1da12ec2014-08-16 13:55:38 +08003191 vtd_init(s);
Peter Xudd4d6072017-04-07 18:59:15 +08003192
3193 /*
3194 * When device reset, throw away all mappings and external caches
3195 */
3196 vtd_address_space_unmap_all(s);
Le Tan1da12ec2014-08-16 13:55:38 +08003197}
3198
Marcel Apfelbaum621d9832016-06-27 18:38:34 +03003199static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
3200{
3201 IntelIOMMUState *s = opaque;
3202 VTDAddressSpace *vtd_as;
3203
Peter Xubf33cc72017-12-08 12:26:53 +08003204 assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
Marcel Apfelbaum621d9832016-06-27 18:38:34 +03003205
3206 vtd_as = vtd_find_add_as(s, bus, devfn);
3207 return &vtd_as->as;
3208}
3209
Radim Krčmáře6b6af02016-10-10 17:28:46 +02003210static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
Radim Krčmář6333e932016-10-10 17:28:45 +02003211{
Radim Krčmáře6b6af02016-10-10 17:28:46 +02003212 X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
3213
Radim Krčmář6333e932016-10-10 17:28:45 +02003214 /* Currently Intel IOMMU IR only support "kernel-irqchip={off|split}" */
3215 if (x86_iommu->intr_supported && kvm_irqchip_in_kernel() &&
3216 !kvm_irqchip_is_split()) {
3217 error_setg(errp, "Intel Interrupt Remapping cannot work with "
3218 "kernel-irqchip=on, please use 'split|off'.");
3219 return false;
3220 }
Radim Krčmáře6b6af02016-10-10 17:28:46 +02003221 if (s->intr_eim == ON_OFF_AUTO_ON && !x86_iommu->intr_supported) {
3222 error_setg(errp, "eim=on cannot be selected without intremap=on");
3223 return false;
3224 }
3225
3226 if (s->intr_eim == ON_OFF_AUTO_AUTO) {
Radim Krčmářfb506e72016-10-10 17:28:47 +02003227 s->intr_eim = (kvm_irqchip_in_kernel() || s->buggy_eim)
3228 && x86_iommu->intr_supported ?
Radim Krčmáře6b6af02016-10-10 17:28:46 +02003229 ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
3230 }
Radim Krčmářfb506e72016-10-10 17:28:47 +02003231 if (s->intr_eim == ON_OFF_AUTO_ON && !s->buggy_eim) {
3232 if (!kvm_irqchip_in_kernel()) {
3233 error_setg(errp, "eim=on requires accel=kvm,kernel-irqchip=split");
3234 return false;
3235 }
3236 if (!kvm_enable_x2apic()) {
3237 error_setg(errp, "eim=on requires support on the KVM side"
3238 "(X2APIC_API, first shipped in v4.7)");
3239 return false;
3240 }
3241 }
Radim Krčmáře6b6af02016-10-10 17:28:46 +02003242
Prasad Singamsetty37f51382017-11-14 18:13:50 -05003243 /* Currently only address widths supported are 39 and 48 bits */
3244 if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
3245 (s->aw_bits != VTD_HOST_AW_48BIT)) {
3246 error_setg(errp, "Supported values for x-aw-bits are: %d, %d",
3247 VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
3248 return false;
3249 }
3250
Radim Krčmář6333e932016-10-10 17:28:45 +02003251 return true;
3252}
3253
Le Tan1da12ec2014-08-16 13:55:38 +08003254static void vtd_realize(DeviceState *dev, Error **errp)
3255{
Eduardo Habkostef0e8fc2017-05-08 17:08:12 -03003256 MachineState *ms = MACHINE(qdev_get_machine());
Mohammed Gamal29396ed2017-11-29 13:33:12 +01003257 PCMachineState *pcms = PC_MACHINE(ms);
3258 PCIBus *bus = pcms->bus;
Le Tan1da12ec2014-08-16 13:55:38 +08003259 IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
Peter Xu4684a202016-07-14 13:56:36 +08003260 X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
Le Tan1da12ec2014-08-16 13:55:38 +08003261
David Kiariefb9f5922016-09-20 18:42:34 +03003262 x86_iommu->type = TYPE_INTEL;
Radim Krčmář6333e932016-10-10 17:28:45 +02003263
Radim Krčmáře6b6af02016-10-10 17:28:46 +02003264 if (!vtd_decide_config(s, errp)) {
Radim Krčmář6333e932016-10-10 17:28:45 +02003265 return;
3266 }
3267
Peter Xub4a4ba02018-05-18 15:25:10 +08003268 QLIST_INIT(&s->vtd_as_with_notifiers);
Peter Xu1d9efa72018-05-18 15:25:11 +08003269 qemu_mutex_init(&s->iommu_lock);
Knut Omang7df953b2015-10-04 15:48:50 +02003270 memset(s->vtd_as_by_bus_num, 0, sizeof(s->vtd_as_by_bus_num));
Le Tan1da12ec2014-08-16 13:55:38 +08003271 memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
3272 "intel_iommu", DMAR_REG_SIZE);
3273 sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem);
Le Tanb5a280c2014-08-16 13:55:44 +08003274 /* No corresponding destroy */
3275 s->iotlb = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
3276 g_free, g_free);
Knut Omang7df953b2015-10-04 15:48:50 +02003277 s->vtd_as_by_busptr = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
3278 g_free, g_free);
Le Tan1da12ec2014-08-16 13:55:38 +08003279 vtd_init(s);
Marcel Apfelbaum621d9832016-06-27 18:38:34 +03003280 sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR);
3281 pci_setup_iommu(bus, vtd_host_dma_iommu, dev);
Peter Xucb135f52016-07-14 13:56:23 +08003282 /* Pseudo address space under root PCI bus. */
3283 pcms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC);
Le Tan1da12ec2014-08-16 13:55:38 +08003284}
3285
3286static void vtd_class_init(ObjectClass *klass, void *data)
3287{
3288 DeviceClass *dc = DEVICE_CLASS(klass);
Peter Xu1c7955c2016-07-14 13:56:10 +08003289 X86IOMMUClass *x86_class = X86_IOMMU_CLASS(klass);
Le Tan1da12ec2014-08-16 13:55:38 +08003290
3291 dc->reset = vtd_reset;
Le Tan1da12ec2014-08-16 13:55:38 +08003292 dc->vmsd = &vtd_vmstate;
3293 dc->props = vtd_properties;
Marcel Apfelbaum621d9832016-06-27 18:38:34 +03003294 dc->hotpluggable = false;
Peter Xu1c7955c2016-07-14 13:56:10 +08003295 x86_class->realize = vtd_realize;
Peter Xu8b5ed7d2016-07-14 13:56:25 +08003296 x86_class->int_remap = vtd_int_remap;
Eduardo Habkost8ab57002017-05-03 17:35:47 -03003297 /* Supported by the pc-q35-* machine types */
Eduardo Habkoste4f4fb12017-05-03 17:35:45 -03003298 dc->user_creatable = true;
Le Tan1da12ec2014-08-16 13:55:38 +08003299}
3300
3301static const TypeInfo vtd_info = {
3302 .name = TYPE_INTEL_IOMMU_DEVICE,
Peter Xu1c7955c2016-07-14 13:56:10 +08003303 .parent = TYPE_X86_IOMMU_DEVICE,
Le Tan1da12ec2014-08-16 13:55:38 +08003304 .instance_size = sizeof(IntelIOMMUState),
3305 .class_init = vtd_class_init,
3306};
3307
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +10003308static void vtd_iommu_memory_region_class_init(ObjectClass *klass,
3309 void *data)
3310{
3311 IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
3312
3313 imrc->translate = vtd_iommu_translate;
3314 imrc->notify_flag_changed = vtd_iommu_notify_flag_changed;
3315 imrc->replay = vtd_iommu_replay;
3316}
3317
3318static const TypeInfo vtd_iommu_memory_region_info = {
3319 .parent = TYPE_IOMMU_MEMORY_REGION,
3320 .name = TYPE_INTEL_IOMMU_MEMORY_REGION,
3321 .class_init = vtd_iommu_memory_region_class_init,
3322};
3323
Le Tan1da12ec2014-08-16 13:55:38 +08003324static void vtd_register_types(void)
3325{
Le Tan1da12ec2014-08-16 13:55:38 +08003326 type_register_static(&vtd_info);
Alexey Kardashevskiy1221a472017-07-11 13:56:20 +10003327 type_register_static(&vtd_iommu_memory_region_info);
Le Tan1da12ec2014-08-16 13:55:38 +08003328}
3329
3330type_init(vtd_register_types)