qemu/hw/i386/intel_iommu.c
<<
>>
Prefs
   1/*
   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
  22#include "qemu/osdep.h"
  23#include "qemu/error-report.h"
  24#include "qapi/error.h"
  25#include "hw/sysbus.h"
  26#include "exec/address-spaces.h"
  27#include "intel_iommu_internal.h"
  28#include "hw/pci/pci.h"
  29#include "hw/pci/pci_bus.h"
  30#include "hw/i386/pc.h"
  31#include "hw/i386/apic-msidef.h"
  32#include "hw/boards.h"
  33#include "hw/i386/x86-iommu.h"
  34#include "hw/pci-host/q35.h"
  35#include "sysemu/kvm.h"
  36#include "hw/i386/apic_internal.h"
  37#include "kvm_i386.h"
  38#include "trace.h"
  39
  40static 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
 131/* GHashTable functions */
 132static gboolean vtd_uint64_equal(gconstpointer v1, gconstpointer v2)
 133{
 134    return *((const uint64_t *)v1) == *((const uint64_t *)v2);
 135}
 136
 137static guint vtd_uint64_hash(gconstpointer v)
 138{
 139    return (guint)*(const uint64_t *)v;
 140}
 141
 142static gboolean vtd_hash_remove_by_domain(gpointer key, gpointer value,
 143                                          gpointer user_data)
 144{
 145    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
 146    uint16_t domain_id = *(uint16_t *)user_data;
 147    return entry->domain_id == domain_id;
 148}
 149
 150/* The shift of an addr for a certain level of paging structure */
 151static inline uint32_t vtd_slpt_level_shift(uint32_t level)
 152{
 153    assert(level != 0);
 154    return VTD_PAGE_SHIFT_4K + (level - 1) * VTD_SL_LEVEL_BITS;
 155}
 156
 157static inline uint64_t vtd_slpt_level_page_mask(uint32_t level)
 158{
 159    return ~((1ULL << vtd_slpt_level_shift(level)) - 1);
 160}
 161
 162static gboolean vtd_hash_remove_by_page(gpointer key, gpointer value,
 163                                        gpointer user_data)
 164{
 165    VTDIOTLBEntry *entry = (VTDIOTLBEntry *)value;
 166    VTDIOTLBPageInvInfo *info = (VTDIOTLBPageInvInfo *)user_data;
 167    uint64_t gfn = (info->addr >> VTD_PAGE_SHIFT_4K) & info->mask;
 168    uint64_t gfn_tlb = (info->addr & entry->mask) >> VTD_PAGE_SHIFT_4K;
 169    return (entry->domain_id == info->domain_id) &&
 170            (((entry->gfn & info->mask) == gfn) ||
 171             (entry->gfn == gfn_tlb));
 172}
 173
 174/* Reset all the gen of VTDAddressSpace to zero and set the gen of
 175 * IntelIOMMUState to 1.
 176 */
 177static void vtd_reset_context_cache(IntelIOMMUState *s)
 178{
 179    VTDAddressSpace *vtd_as;
 180    VTDBus *vtd_bus;
 181    GHashTableIter bus_it;
 182    uint32_t devfn_it;
 183
 184    trace_vtd_context_cache_reset();
 185
 186    g_hash_table_iter_init(&bus_it, s->vtd_as_by_busptr);
 187
 188    while (g_hash_table_iter_next (&bus_it, NULL, (void**)&vtd_bus)) {
 189        for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) {
 190            vtd_as = vtd_bus->dev_as[devfn_it];
 191            if (!vtd_as) {
 192                continue;
 193            }
 194            vtd_as->context_cache_entry.context_cache_gen = 0;
 195        }
 196    }
 197    s->context_cache_gen = 1;
 198}
 199
 200static void vtd_reset_iotlb(IntelIOMMUState *s)
 201{
 202    assert(s->iotlb);
 203    g_hash_table_remove_all(s->iotlb);
 204}
 205
 206static uint64_t vtd_get_iotlb_key(uint64_t gfn, uint16_t source_id,
 207                                  uint32_t level)
 208{
 209    return gfn | ((uint64_t)(source_id) << VTD_IOTLB_SID_SHIFT) |
 210           ((uint64_t)(level) << VTD_IOTLB_LVL_SHIFT);
 211}
 212
 213static uint64_t vtd_get_iotlb_gfn(hwaddr addr, uint32_t level)
 214{
 215    return (addr & vtd_slpt_level_page_mask(level)) >> VTD_PAGE_SHIFT_4K;
 216}
 217
 218static VTDIOTLBEntry *vtd_lookup_iotlb(IntelIOMMUState *s, uint16_t source_id,
 219                                       hwaddr addr)
 220{
 221    VTDIOTLBEntry *entry;
 222    uint64_t key;
 223    int level;
 224
 225    for (level = VTD_SL_PT_LEVEL; level < VTD_SL_PML4_LEVEL; level++) {
 226        key = vtd_get_iotlb_key(vtd_get_iotlb_gfn(addr, level),
 227                                source_id, level);
 228        entry = g_hash_table_lookup(s->iotlb, &key);
 229        if (entry) {
 230            goto out;
 231        }
 232    }
 233
 234out:
 235    return entry;
 236}
 237
 238static void vtd_update_iotlb(IntelIOMMUState *s, uint16_t source_id,
 239                             uint16_t domain_id, hwaddr addr, uint64_t slpte,
 240                             uint8_t access_flags, uint32_t level)
 241{
 242    VTDIOTLBEntry *entry = g_malloc(sizeof(*entry));
 243    uint64_t *key = g_malloc(sizeof(*key));
 244    uint64_t gfn = vtd_get_iotlb_gfn(addr, level);
 245
 246    trace_vtd_iotlb_page_update(source_id, addr, slpte, domain_id);
 247    if (g_hash_table_size(s->iotlb) >= VTD_IOTLB_MAX_SIZE) {
 248        trace_vtd_iotlb_reset("iotlb exceeds size limit");
 249        vtd_reset_iotlb(s);
 250    }
 251
 252    entry->gfn = gfn;
 253    entry->domain_id = domain_id;
 254    entry->slpte = slpte;
 255    entry->access_flags = access_flags;
 256    entry->mask = vtd_slpt_level_page_mask(level);
 257    *key = vtd_get_iotlb_key(gfn, source_id, level);
 258    g_hash_table_replace(s->iotlb, key, entry);
 259}
 260
 261/* Given the reg addr of both the message data and address, generate an
 262 * interrupt via MSI.
 263 */
 264static void vtd_generate_interrupt(IntelIOMMUState *s, hwaddr mesg_addr_reg,
 265                                   hwaddr mesg_data_reg)
 266{
 267    MSIMessage msi;
 268
 269    assert(mesg_data_reg < DMAR_REG_SIZE);
 270    assert(mesg_addr_reg < DMAR_REG_SIZE);
 271
 272    msi.address = vtd_get_long_raw(s, mesg_addr_reg);
 273    msi.data = vtd_get_long_raw(s, mesg_data_reg);
 274
 275    trace_vtd_irq_generate(msi.address, msi.data);
 276
 277    apic_get_class()->send_msi(&msi);
 278}
 279
 280/* Generate a fault event to software via MSI if conditions are met.
 281 * Notice that the value of FSTS_REG being passed to it should be the one
 282 * before any update.
 283 */
 284static void vtd_generate_fault_event(IntelIOMMUState *s, uint32_t pre_fsts)
 285{
 286    if (pre_fsts & VTD_FSTS_PPF || pre_fsts & VTD_FSTS_PFO ||
 287        pre_fsts & VTD_FSTS_IQE) {
 288        trace_vtd_err("There are previous interrupt conditions "
 289                      "to be serviced by software, fault event "
 290                      "is not generated.");
 291        return;
 292    }
 293    vtd_set_clear_mask_long(s, DMAR_FECTL_REG, 0, VTD_FECTL_IP);
 294    if (vtd_get_long_raw(s, DMAR_FECTL_REG) & VTD_FECTL_IM) {
 295        trace_vtd_err("Interrupt Mask set, irq is not generated.");
 296    } else {
 297        vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
 298        vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
 299    }
 300}
 301
 302/* Check if the Fault (F) field of the Fault Recording Register referenced by
 303 * @index is Set.
 304 */
 305static bool vtd_is_frcd_set(IntelIOMMUState *s, uint16_t index)
 306{
 307    /* Each reg is 128-bit */
 308    hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
 309    addr += 8; /* Access the high 64-bit half */
 310
 311    assert(index < DMAR_FRCD_REG_NR);
 312
 313    return vtd_get_quad_raw(s, addr) & VTD_FRCD_F;
 314}
 315
 316/* Update the PPF field of Fault Status Register.
 317 * Should be called whenever change the F field of any fault recording
 318 * registers.
 319 */
 320static void vtd_update_fsts_ppf(IntelIOMMUState *s)
 321{
 322    uint32_t i;
 323    uint32_t ppf_mask = 0;
 324
 325    for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
 326        if (vtd_is_frcd_set(s, i)) {
 327            ppf_mask = VTD_FSTS_PPF;
 328            break;
 329        }
 330    }
 331    vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_PPF, ppf_mask);
 332    trace_vtd_fsts_ppf(!!ppf_mask);
 333}
 334
 335static void vtd_set_frcd_and_update_ppf(IntelIOMMUState *s, uint16_t index)
 336{
 337    /* Each reg is 128-bit */
 338    hwaddr addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
 339    addr += 8; /* Access the high 64-bit half */
 340
 341    assert(index < DMAR_FRCD_REG_NR);
 342
 343    vtd_set_clear_mask_quad(s, addr, 0, VTD_FRCD_F);
 344    vtd_update_fsts_ppf(s);
 345}
 346
 347/* Must not update F field now, should be done later */
 348static void vtd_record_frcd(IntelIOMMUState *s, uint16_t index,
 349                            uint16_t source_id, hwaddr addr,
 350                            VTDFaultReason fault, bool is_write)
 351{
 352    uint64_t hi = 0, lo;
 353    hwaddr frcd_reg_addr = DMAR_FRCD_REG_OFFSET + (((uint64_t)index) << 4);
 354
 355    assert(index < DMAR_FRCD_REG_NR);
 356
 357    lo = VTD_FRCD_FI(addr);
 358    hi = VTD_FRCD_SID(source_id) | VTD_FRCD_FR(fault);
 359    if (!is_write) {
 360        hi |= VTD_FRCD_T;
 361    }
 362    vtd_set_quad_raw(s, frcd_reg_addr, lo);
 363    vtd_set_quad_raw(s, frcd_reg_addr + 8, hi);
 364
 365    trace_vtd_frr_new(index, hi, lo);
 366}
 367
 368/* Try to collapse multiple pending faults from the same requester */
 369static bool vtd_try_collapse_fault(IntelIOMMUState *s, uint16_t source_id)
 370{
 371    uint32_t i;
 372    uint64_t frcd_reg;
 373    hwaddr addr = DMAR_FRCD_REG_OFFSET + 8; /* The high 64-bit half */
 374
 375    for (i = 0; i < DMAR_FRCD_REG_NR; i++) {
 376        frcd_reg = vtd_get_quad_raw(s, addr);
 377        if ((frcd_reg & VTD_FRCD_F) &&
 378            ((frcd_reg & VTD_FRCD_SID_MASK) == source_id)) {
 379            return true;
 380        }
 381        addr += 16; /* 128-bit for each */
 382    }
 383    return false;
 384}
 385
 386/* Log and report an DMAR (address translation) fault to software */
 387static void vtd_report_dmar_fault(IntelIOMMUState *s, uint16_t source_id,
 388                                  hwaddr addr, VTDFaultReason fault,
 389                                  bool is_write)
 390{
 391    uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
 392
 393    assert(fault < VTD_FR_MAX);
 394
 395    if (fault == VTD_FR_RESERVED_ERR) {
 396        /* This is not a normal fault reason case. Drop it. */
 397        return;
 398    }
 399
 400    trace_vtd_dmar_fault(source_id, fault, addr, is_write);
 401
 402    if (fsts_reg & VTD_FSTS_PFO) {
 403        trace_vtd_err("New fault is not recorded due to "
 404                      "Primary Fault Overflow.");
 405        return;
 406    }
 407
 408    if (vtd_try_collapse_fault(s, source_id)) {
 409        trace_vtd_err("New fault is not recorded due to "
 410                      "compression of faults.");
 411        return;
 412    }
 413
 414    if (vtd_is_frcd_set(s, s->next_frcd_reg)) {
 415        trace_vtd_err("Next Fault Recording Reg is used, "
 416                      "new fault is not recorded, set PFO field.");
 417        vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_PFO);
 418        return;
 419    }
 420
 421    vtd_record_frcd(s, s->next_frcd_reg, source_id, addr, fault, is_write);
 422
 423    if (fsts_reg & VTD_FSTS_PPF) {
 424        trace_vtd_err("There are pending faults already, "
 425                      "fault event is not generated.");
 426        vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg);
 427        s->next_frcd_reg++;
 428        if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
 429            s->next_frcd_reg = 0;
 430        }
 431    } else {
 432        vtd_set_clear_mask_long(s, DMAR_FSTS_REG, VTD_FSTS_FRI_MASK,
 433                                VTD_FSTS_FRI(s->next_frcd_reg));
 434        vtd_set_frcd_and_update_ppf(s, s->next_frcd_reg); /* Will set PPF */
 435        s->next_frcd_reg++;
 436        if (s->next_frcd_reg == DMAR_FRCD_REG_NR) {
 437            s->next_frcd_reg = 0;
 438        }
 439        /* This case actually cause the PPF to be Set.
 440         * So generate fault event (interrupt).
 441         */
 442         vtd_generate_fault_event(s, fsts_reg);
 443    }
 444}
 445
 446/* Handle Invalidation Queue Errors of queued invalidation interface error
 447 * conditions.
 448 */
 449static void vtd_handle_inv_queue_error(IntelIOMMUState *s)
 450{
 451    uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
 452
 453    vtd_set_clear_mask_long(s, DMAR_FSTS_REG, 0, VTD_FSTS_IQE);
 454    vtd_generate_fault_event(s, fsts_reg);
 455}
 456
 457/* Set the IWC field and try to generate an invalidation completion interrupt */
 458static void vtd_generate_completion_event(IntelIOMMUState *s)
 459{
 460    if (vtd_get_long_raw(s, DMAR_ICS_REG) & VTD_ICS_IWC) {
 461        trace_vtd_inv_desc_wait_irq("One pending, skip current");
 462        return;
 463    }
 464    vtd_set_clear_mask_long(s, DMAR_ICS_REG, 0, VTD_ICS_IWC);
 465    vtd_set_clear_mask_long(s, DMAR_IECTL_REG, 0, VTD_IECTL_IP);
 466    if (vtd_get_long_raw(s, DMAR_IECTL_REG) & VTD_IECTL_IM) {
 467        trace_vtd_inv_desc_wait_irq("IM in IECTL_REG is set, "
 468                                    "new event not generated");
 469        return;
 470    } else {
 471        /* Generate the interrupt event */
 472        trace_vtd_inv_desc_wait_irq("Generating complete event");
 473        vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
 474        vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
 475    }
 476}
 477
 478static inline bool vtd_root_entry_present(VTDRootEntry *root)
 479{
 480    return root->val & VTD_ROOT_ENTRY_P;
 481}
 482
 483static int vtd_get_root_entry(IntelIOMMUState *s, uint8_t index,
 484                              VTDRootEntry *re)
 485{
 486    dma_addr_t addr;
 487
 488    addr = s->root + index * sizeof(*re);
 489    if (dma_memory_read(&address_space_memory, addr, re, sizeof(*re))) {
 490        trace_vtd_re_invalid(re->rsvd, re->val);
 491        re->val = 0;
 492        return -VTD_FR_ROOT_TABLE_INV;
 493    }
 494    re->val = le64_to_cpu(re->val);
 495    return 0;
 496}
 497
 498static inline bool vtd_ce_present(VTDContextEntry *context)
 499{
 500    return context->lo & VTD_CONTEXT_ENTRY_P;
 501}
 502
 503static int vtd_get_context_entry_from_root(VTDRootEntry *root, uint8_t index,
 504                                           VTDContextEntry *ce)
 505{
 506    dma_addr_t addr;
 507
 508    /* we have checked that root entry is present */
 509    addr = (root->val & VTD_ROOT_ENTRY_CTP) + index * sizeof(*ce);
 510    if (dma_memory_read(&address_space_memory, addr, ce, sizeof(*ce))) {
 511        trace_vtd_re_invalid(root->rsvd, root->val);
 512        return -VTD_FR_CONTEXT_TABLE_INV;
 513    }
 514    ce->lo = le64_to_cpu(ce->lo);
 515    ce->hi = le64_to_cpu(ce->hi);
 516    return 0;
 517}
 518
 519static inline dma_addr_t vtd_ce_get_slpt_base(VTDContextEntry *ce)
 520{
 521    return ce->lo & VTD_CONTEXT_ENTRY_SLPTPTR;
 522}
 523
 524static inline uint64_t vtd_get_slpte_addr(uint64_t slpte, uint8_t aw)
 525{
 526    return slpte & VTD_SL_PT_BASE_ADDR_MASK(aw);
 527}
 528
 529/* Whether the pte indicates the address of the page frame */
 530static inline bool vtd_is_last_slpte(uint64_t slpte, uint32_t level)
 531{
 532    return level == VTD_SL_PT_LEVEL || (slpte & VTD_SL_PT_PAGE_SIZE_MASK);
 533}
 534
 535/* Get the content of a spte located in @base_addr[@index] */
 536static uint64_t vtd_get_slpte(dma_addr_t base_addr, uint32_t index)
 537{
 538    uint64_t slpte;
 539
 540    assert(index < VTD_SL_PT_ENTRY_NR);
 541
 542    if (dma_memory_read(&address_space_memory,
 543                        base_addr + index * sizeof(slpte), &slpte,
 544                        sizeof(slpte))) {
 545        slpte = (uint64_t)-1;
 546        return slpte;
 547    }
 548    slpte = le64_to_cpu(slpte);
 549    return slpte;
 550}
 551
 552/* Given an iova and the level of paging structure, return the offset
 553 * of current level.
 554 */
 555static inline uint32_t vtd_iova_level_offset(uint64_t iova, uint32_t level)
 556{
 557    return (iova >> vtd_slpt_level_shift(level)) &
 558            ((1ULL << VTD_SL_LEVEL_BITS) - 1);
 559}
 560
 561/* Check Capability Register to see if the @level of page-table is supported */
 562static inline bool vtd_is_level_supported(IntelIOMMUState *s, uint32_t level)
 563{
 564    return VTD_CAP_SAGAW_MASK & s->cap &
 565           (1ULL << (level - 2 + VTD_CAP_SAGAW_SHIFT));
 566}
 567
 568/* Get the page-table level that hardware should use for the second-level
 569 * page-table walk from the Address Width field of context-entry.
 570 */
 571static inline uint32_t vtd_ce_get_level(VTDContextEntry *ce)
 572{
 573    return 2 + (ce->hi & VTD_CONTEXT_ENTRY_AW);
 574}
 575
 576static inline uint32_t vtd_ce_get_agaw(VTDContextEntry *ce)
 577{
 578    return 30 + (ce->hi & VTD_CONTEXT_ENTRY_AW) * 9;
 579}
 580
 581static inline uint32_t vtd_ce_get_type(VTDContextEntry *ce)
 582{
 583    return ce->lo & VTD_CONTEXT_ENTRY_TT;
 584}
 585
 586/* Return true if check passed, otherwise false */
 587static inline bool vtd_ce_type_check(X86IOMMUState *x86_iommu,
 588                                     VTDContextEntry *ce)
 589{
 590    switch (vtd_ce_get_type(ce)) {
 591    case VTD_CONTEXT_TT_MULTI_LEVEL:
 592        /* Always supported */
 593        break;
 594    case VTD_CONTEXT_TT_DEV_IOTLB:
 595        if (!x86_iommu->dt_supported) {
 596            return false;
 597        }
 598        break;
 599    case VTD_CONTEXT_TT_PASS_THROUGH:
 600        if (!x86_iommu->pt_supported) {
 601            return false;
 602        }
 603        break;
 604    default:
 605        /* Unknwon type */
 606        return false;
 607    }
 608    return true;
 609}
 610
 611static inline uint64_t vtd_iova_limit(VTDContextEntry *ce, uint8_t aw)
 612{
 613    uint32_t ce_agaw = vtd_ce_get_agaw(ce);
 614    return 1ULL << MIN(ce_agaw, aw);
 615}
 616
 617/* Return true if IOVA passes range check, otherwise false. */
 618static inline bool vtd_iova_range_check(uint64_t iova, VTDContextEntry *ce,
 619                                        uint8_t aw)
 620{
 621    /*
 622     * Check if @iova is above 2^X-1, where X is the minimum of MGAW
 623     * in CAP_REG and AW in context-entry.
 624     */
 625    return !(iova & ~(vtd_iova_limit(ce, aw) - 1));
 626}
 627
 628/*
 629 * Rsvd field masks for spte:
 630 *     Index [1] to [4] 4k pages
 631 *     Index [5] to [8] large pages
 632 */
 633static uint64_t vtd_paging_entry_rsvd_field[9];
 634
 635static bool vtd_slpte_nonzero_rsvd(uint64_t slpte, uint32_t level)
 636{
 637    if (slpte & VTD_SL_PT_PAGE_SIZE_MASK) {
 638        /* Maybe large page */
 639        return slpte & vtd_paging_entry_rsvd_field[level + 4];
 640    } else {
 641        return slpte & vtd_paging_entry_rsvd_field[level];
 642    }
 643}
 644
 645/* Find the VTD address space associated with a given bus number */
 646static VTDBus *vtd_find_as_from_bus_num(IntelIOMMUState *s, uint8_t bus_num)
 647{
 648    VTDBus *vtd_bus = s->vtd_as_by_bus_num[bus_num];
 649    if (!vtd_bus) {
 650        /*
 651         * Iterate over the registered buses to find the one which
 652         * currently hold this bus number, and update the bus_num
 653         * lookup table:
 654         */
 655        GHashTableIter iter;
 656
 657        g_hash_table_iter_init(&iter, s->vtd_as_by_busptr);
 658        while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) {
 659            if (pci_bus_num(vtd_bus->bus) == bus_num) {
 660                s->vtd_as_by_bus_num[bus_num] = vtd_bus;
 661                return vtd_bus;
 662            }
 663        }
 664    }
 665    return vtd_bus;
 666}
 667
 668/* Given the @iova, get relevant @slptep. @slpte_level will be the last level
 669 * of the translation, can be used for deciding the size of large page.
 670 */
 671static int vtd_iova_to_slpte(VTDContextEntry *ce, uint64_t iova, bool is_write,
 672                             uint64_t *slptep, uint32_t *slpte_level,
 673                             bool *reads, bool *writes, uint8_t aw_bits)
 674{
 675    dma_addr_t addr = vtd_ce_get_slpt_base(ce);
 676    uint32_t level = vtd_ce_get_level(ce);
 677    uint32_t offset;
 678    uint64_t slpte;
 679    uint64_t access_right_check;
 680
 681    if (!vtd_iova_range_check(iova, ce, aw_bits)) {
 682        trace_vtd_err_dmar_iova_overflow(iova);
 683        return -VTD_FR_ADDR_BEYOND_MGAW;
 684    }
 685
 686    /* FIXME: what is the Atomics request here? */
 687    access_right_check = is_write ? VTD_SL_W : VTD_SL_R;
 688
 689    while (true) {
 690        offset = vtd_iova_level_offset(iova, level);
 691        slpte = vtd_get_slpte(addr, offset);
 692
 693        if (slpte == (uint64_t)-1) {
 694            trace_vtd_err_dmar_slpte_read_error(iova, level);
 695            if (level == vtd_ce_get_level(ce)) {
 696                /* Invalid programming of context-entry */
 697                return -VTD_FR_CONTEXT_ENTRY_INV;
 698            } else {
 699                return -VTD_FR_PAGING_ENTRY_INV;
 700            }
 701        }
 702        *reads = (*reads) && (slpte & VTD_SL_R);
 703        *writes = (*writes) && (slpte & VTD_SL_W);
 704        if (!(slpte & access_right_check)) {
 705            trace_vtd_err_dmar_slpte_perm_error(iova, level, slpte, is_write);
 706            return is_write ? -VTD_FR_WRITE : -VTD_FR_READ;
 707        }
 708        if (vtd_slpte_nonzero_rsvd(slpte, level)) {
 709            trace_vtd_err_dmar_slpte_resv_error(iova, level, slpte);
 710            return -VTD_FR_PAGING_ENTRY_RSVD;
 711        }
 712
 713        if (vtd_is_last_slpte(slpte, level)) {
 714            *slptep = slpte;
 715            *slpte_level = level;
 716            return 0;
 717        }
 718        addr = vtd_get_slpte_addr(slpte, aw_bits);
 719        level--;
 720    }
 721}
 722
 723typedef int (*vtd_page_walk_hook)(IOMMUTLBEntry *entry, void *private);
 724
 725/**
 726 * vtd_page_walk_level - walk over specific level for IOVA range
 727 *
 728 * @addr: base GPA addr to start the walk
 729 * @start: IOVA range start address
 730 * @end: IOVA range end address (start <= addr < end)
 731 * @hook_fn: hook func to be called when detected page
 732 * @private: private data to be passed into hook func
 733 * @read: whether parent level has read permission
 734 * @write: whether parent level has write permission
 735 * @notify_unmap: whether we should notify invalid entries
 736 * @aw: maximum address width
 737 */
 738static int vtd_page_walk_level(dma_addr_t addr, uint64_t start,
 739                               uint64_t end, vtd_page_walk_hook hook_fn,
 740                               void *private, uint32_t level, bool read,
 741                               bool write, bool notify_unmap, uint8_t aw)
 742{
 743    bool read_cur, write_cur, entry_valid;
 744    uint32_t offset;
 745    uint64_t slpte;
 746    uint64_t subpage_size, subpage_mask;
 747    IOMMUTLBEntry entry;
 748    uint64_t iova = start;
 749    uint64_t iova_next;
 750    int ret = 0;
 751
 752    trace_vtd_page_walk_level(addr, level, start, end);
 753
 754    subpage_size = 1ULL << vtd_slpt_level_shift(level);
 755    subpage_mask = vtd_slpt_level_page_mask(level);
 756
 757    while (iova < end) {
 758        iova_next = (iova & subpage_mask) + subpage_size;
 759
 760        offset = vtd_iova_level_offset(iova, level);
 761        slpte = vtd_get_slpte(addr, offset);
 762
 763        if (slpte == (uint64_t)-1) {
 764            trace_vtd_page_walk_skip_read(iova, iova_next);
 765            goto next;
 766        }
 767
 768        if (vtd_slpte_nonzero_rsvd(slpte, level)) {
 769            trace_vtd_page_walk_skip_reserve(iova, iova_next);
 770            goto next;
 771        }
 772
 773        /* Permissions are stacked with parents' */
 774        read_cur = read && (slpte & VTD_SL_R);
 775        write_cur = write && (slpte & VTD_SL_W);
 776
 777        /*
 778         * As long as we have either read/write permission, this is a
 779         * valid entry. The rule works for both page entries and page
 780         * table entries.
 781         */
 782        entry_valid = read_cur | write_cur;
 783
 784        if (vtd_is_last_slpte(slpte, level)) {
 785            entry.target_as = &address_space_memory;
 786            entry.iova = iova & subpage_mask;
 787            /* NOTE: this is only meaningful if entry_valid == true */
 788            entry.translated_addr = vtd_get_slpte_addr(slpte, aw);
 789            entry.addr_mask = ~subpage_mask;
 790            entry.perm = IOMMU_ACCESS_FLAG(read_cur, write_cur);
 791            if (!entry_valid && !notify_unmap) {
 792                trace_vtd_page_walk_skip_perm(iova, iova_next);
 793                goto next;
 794            }
 795            trace_vtd_page_walk_one(level, entry.iova, entry.translated_addr,
 796                                    entry.addr_mask, entry.perm);
 797            if (hook_fn) {
 798                ret = hook_fn(&entry, private);
 799                if (ret < 0) {
 800                    return ret;
 801                }
 802            }
 803        } else {
 804            if (!entry_valid) {
 805                trace_vtd_page_walk_skip_perm(iova, iova_next);
 806                goto next;
 807            }
 808            ret = vtd_page_walk_level(vtd_get_slpte_addr(slpte, aw), iova,
 809                                      MIN(iova_next, end), hook_fn, private,
 810                                      level - 1, read_cur, write_cur,
 811                                      notify_unmap, aw);
 812            if (ret < 0) {
 813                return ret;
 814            }
 815        }
 816
 817next:
 818        iova = iova_next;
 819    }
 820
 821    return 0;
 822}
 823
 824/**
 825 * vtd_page_walk - walk specific IOVA range, and call the hook
 826 *
 827 * @ce: context entry to walk upon
 828 * @start: IOVA address to start the walk
 829 * @end: IOVA range end address (start <= addr < end)
 830 * @hook_fn: the hook that to be called for each detected area
 831 * @private: private data for the hook function
 832 * @aw: maximum address width
 833 */
 834static int vtd_page_walk(VTDContextEntry *ce, uint64_t start, uint64_t end,
 835                         vtd_page_walk_hook hook_fn, void *private,
 836                         bool notify_unmap, uint8_t aw)
 837{
 838    dma_addr_t addr = vtd_ce_get_slpt_base(ce);
 839    uint32_t level = vtd_ce_get_level(ce);
 840
 841    if (!vtd_iova_range_check(start, ce, aw)) {
 842        return -VTD_FR_ADDR_BEYOND_MGAW;
 843    }
 844
 845    if (!vtd_iova_range_check(end, ce, aw)) {
 846        /* Fix end so that it reaches the maximum */
 847        end = vtd_iova_limit(ce, aw);
 848    }
 849
 850    return vtd_page_walk_level(addr, start, end, hook_fn, private,
 851                               level, true, true, notify_unmap, aw);
 852}
 853
 854/* Map a device to its corresponding domain (context-entry) */
 855static int vtd_dev_to_context_entry(IntelIOMMUState *s, uint8_t bus_num,
 856                                    uint8_t devfn, VTDContextEntry *ce)
 857{
 858    VTDRootEntry re;
 859    int ret_fr;
 860    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 861
 862    ret_fr = vtd_get_root_entry(s, bus_num, &re);
 863    if (ret_fr) {
 864        return ret_fr;
 865    }
 866
 867    if (!vtd_root_entry_present(&re)) {
 868        /* Not error - it's okay we don't have root entry. */
 869        trace_vtd_re_not_present(bus_num);
 870        return -VTD_FR_ROOT_ENTRY_P;
 871    }
 872
 873    if (re.rsvd || (re.val & VTD_ROOT_ENTRY_RSVD(s->aw_bits))) {
 874        trace_vtd_re_invalid(re.rsvd, re.val);
 875        return -VTD_FR_ROOT_ENTRY_RSVD;
 876    }
 877
 878    ret_fr = vtd_get_context_entry_from_root(&re, devfn, ce);
 879    if (ret_fr) {
 880        return ret_fr;
 881    }
 882
 883    if (!vtd_ce_present(ce)) {
 884        /* Not error - it's okay we don't have context entry. */
 885        trace_vtd_ce_not_present(bus_num, devfn);
 886        return -VTD_FR_CONTEXT_ENTRY_P;
 887    }
 888
 889    if ((ce->hi & VTD_CONTEXT_ENTRY_RSVD_HI) ||
 890               (ce->lo & VTD_CONTEXT_ENTRY_RSVD_LO(s->aw_bits))) {
 891        trace_vtd_ce_invalid(ce->hi, ce->lo);
 892        return -VTD_FR_CONTEXT_ENTRY_RSVD;
 893    }
 894
 895    /* Check if the programming of context-entry is valid */
 896    if (!vtd_is_level_supported(s, vtd_ce_get_level(ce))) {
 897        trace_vtd_ce_invalid(ce->hi, ce->lo);
 898        return -VTD_FR_CONTEXT_ENTRY_INV;
 899    }
 900
 901    /* Do translation type check */
 902    if (!vtd_ce_type_check(x86_iommu, ce)) {
 903        trace_vtd_ce_invalid(ce->hi, ce->lo);
 904        return -VTD_FR_CONTEXT_ENTRY_INV;
 905    }
 906
 907    return 0;
 908}
 909
 910/*
 911 * Fetch translation type for specific device. Returns <0 if error
 912 * happens, otherwise return the shifted type to check against
 913 * VTD_CONTEXT_TT_*.
 914 */
 915static int vtd_dev_get_trans_type(VTDAddressSpace *as)
 916{
 917    IntelIOMMUState *s;
 918    VTDContextEntry ce;
 919    int ret;
 920
 921    s = as->iommu_state;
 922
 923    ret = vtd_dev_to_context_entry(s, pci_bus_num(as->bus),
 924                                   as->devfn, &ce);
 925    if (ret) {
 926        return ret;
 927    }
 928
 929    return vtd_ce_get_type(&ce);
 930}
 931
 932static bool vtd_dev_pt_enabled(VTDAddressSpace *as)
 933{
 934    int ret;
 935
 936    assert(as);
 937
 938    ret = vtd_dev_get_trans_type(as);
 939    if (ret < 0) {
 940        /*
 941         * Possibly failed to parse the context entry for some reason
 942         * (e.g., during init, or any guest configuration errors on
 943         * context entries). We should assume PT not enabled for
 944         * safety.
 945         */
 946        return false;
 947    }
 948
 949    return ret == VTD_CONTEXT_TT_PASS_THROUGH;
 950}
 951
 952/* Return whether the device is using IOMMU translation. */
 953static bool vtd_switch_address_space(VTDAddressSpace *as)
 954{
 955    bool use_iommu;
 956    /* Whether we need to take the BQL on our own */
 957    bool take_bql = !qemu_mutex_iothread_locked();
 958
 959    assert(as);
 960
 961    use_iommu = as->iommu_state->dmar_enabled & !vtd_dev_pt_enabled(as);
 962
 963    trace_vtd_switch_address_space(pci_bus_num(as->bus),
 964                                   VTD_PCI_SLOT(as->devfn),
 965                                   VTD_PCI_FUNC(as->devfn),
 966                                   use_iommu);
 967
 968    /*
 969     * It's possible that we reach here without BQL, e.g., when called
 970     * from vtd_pt_enable_fast_path(). However the memory APIs need
 971     * it. We'd better make sure we have had it already, or, take it.
 972     */
 973    if (take_bql) {
 974        qemu_mutex_lock_iothread();
 975    }
 976
 977    /* Turn off first then on the other */
 978    if (use_iommu) {
 979        memory_region_set_enabled(&as->sys_alias, false);
 980        memory_region_set_enabled(MEMORY_REGION(&as->iommu), true);
 981    } else {
 982        memory_region_set_enabled(MEMORY_REGION(&as->iommu), false);
 983        memory_region_set_enabled(&as->sys_alias, true);
 984    }
 985
 986    if (take_bql) {
 987        qemu_mutex_unlock_iothread();
 988    }
 989
 990    return use_iommu;
 991}
 992
 993static void vtd_switch_address_space_all(IntelIOMMUState *s)
 994{
 995    GHashTableIter iter;
 996    VTDBus *vtd_bus;
 997    int i;
 998
 999    g_hash_table_iter_init(&iter, s->vtd_as_by_busptr);
1000    while (g_hash_table_iter_next(&iter, NULL, (void **)&vtd_bus)) {
1001        for (i = 0; i < PCI_DEVFN_MAX; i++) {
1002            if (!vtd_bus->dev_as[i]) {
1003                continue;
1004            }
1005            vtd_switch_address_space(vtd_bus->dev_as[i]);
1006        }
1007    }
1008}
1009
1010static inline uint16_t vtd_make_source_id(uint8_t bus_num, uint8_t devfn)
1011{
1012    return ((bus_num & 0xffUL) << 8) | (devfn & 0xffUL);
1013}
1014
1015static const bool vtd_qualified_faults[] = {
1016    [VTD_FR_RESERVED] = false,
1017    [VTD_FR_ROOT_ENTRY_P] = false,
1018    [VTD_FR_CONTEXT_ENTRY_P] = true,
1019    [VTD_FR_CONTEXT_ENTRY_INV] = true,
1020    [VTD_FR_ADDR_BEYOND_MGAW] = true,
1021    [VTD_FR_WRITE] = true,
1022    [VTD_FR_READ] = true,
1023    [VTD_FR_PAGING_ENTRY_INV] = true,
1024    [VTD_FR_ROOT_TABLE_INV] = false,
1025    [VTD_FR_CONTEXT_TABLE_INV] = false,
1026    [VTD_FR_ROOT_ENTRY_RSVD] = false,
1027    [VTD_FR_PAGING_ENTRY_RSVD] = true,
1028    [VTD_FR_CONTEXT_ENTRY_TT] = true,
1029    [VTD_FR_RESERVED_ERR] = false,
1030    [VTD_FR_MAX] = false,
1031};
1032
1033/* To see if a fault condition is "qualified", which is reported to software
1034 * only if the FPD field in the context-entry used to process the faulting
1035 * request is 0.
1036 */
1037static inline bool vtd_is_qualified_fault(VTDFaultReason fault)
1038{
1039    return vtd_qualified_faults[fault];
1040}
1041
1042static inline bool vtd_is_interrupt_addr(hwaddr addr)
1043{
1044    return VTD_INTERRUPT_ADDR_FIRST <= addr && addr <= VTD_INTERRUPT_ADDR_LAST;
1045}
1046
1047static void vtd_pt_enable_fast_path(IntelIOMMUState *s, uint16_t source_id)
1048{
1049    VTDBus *vtd_bus;
1050    VTDAddressSpace *vtd_as;
1051    bool success = false;
1052
1053    vtd_bus = vtd_find_as_from_bus_num(s, VTD_SID_TO_BUS(source_id));
1054    if (!vtd_bus) {
1055        goto out;
1056    }
1057
1058    vtd_as = vtd_bus->dev_as[VTD_SID_TO_DEVFN(source_id)];
1059    if (!vtd_as) {
1060        goto out;
1061    }
1062
1063    if (vtd_switch_address_space(vtd_as) == false) {
1064        /* We switched off IOMMU region successfully. */
1065        success = true;
1066    }
1067
1068out:
1069    trace_vtd_pt_enable_fast_path(source_id, success);
1070}
1071
1072/* Map dev to context-entry then do a paging-structures walk to do a iommu
1073 * translation.
1074 *
1075 * Called from RCU critical section.
1076 *
1077 * @bus_num: The bus number
1078 * @devfn: The devfn, which is the  combined of device and function number
1079 * @is_write: The access is a write operation
1080 * @entry: IOMMUTLBEntry that contain the addr to be translated and result
1081 *
1082 * Returns true if translation is successful, otherwise false.
1083 */
1084static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
1085                                   uint8_t devfn, hwaddr addr, bool is_write,
1086                                   IOMMUTLBEntry *entry)
1087{
1088    IntelIOMMUState *s = vtd_as->iommu_state;
1089    VTDContextEntry ce;
1090    uint8_t bus_num = pci_bus_num(bus);
1091    VTDContextCacheEntry *cc_entry = &vtd_as->context_cache_entry;
1092    uint64_t slpte, page_mask;
1093    uint32_t level;
1094    uint16_t source_id = vtd_make_source_id(bus_num, devfn);
1095    int ret_fr;
1096    bool is_fpd_set = false;
1097    bool reads = true;
1098    bool writes = true;
1099    uint8_t access_flags;
1100    VTDIOTLBEntry *iotlb_entry;
1101
1102    /*
1103     * We have standalone memory region for interrupt addresses, we
1104     * should never receive translation requests in this region.
1105     */
1106    assert(!vtd_is_interrupt_addr(addr));
1107
1108    /* Try to fetch slpte form IOTLB */
1109    iotlb_entry = vtd_lookup_iotlb(s, source_id, addr);
1110    if (iotlb_entry) {
1111        trace_vtd_iotlb_page_hit(source_id, addr, iotlb_entry->slpte,
1112                                 iotlb_entry->domain_id);
1113        slpte = iotlb_entry->slpte;
1114        access_flags = iotlb_entry->access_flags;
1115        page_mask = iotlb_entry->mask;
1116        goto out;
1117    }
1118
1119    /* Try to fetch context-entry from cache first */
1120    if (cc_entry->context_cache_gen == s->context_cache_gen) {
1121        trace_vtd_iotlb_cc_hit(bus_num, devfn, cc_entry->context_entry.hi,
1122                               cc_entry->context_entry.lo,
1123                               cc_entry->context_cache_gen);
1124        ce = cc_entry->context_entry;
1125        is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
1126    } else {
1127        ret_fr = vtd_dev_to_context_entry(s, bus_num, devfn, &ce);
1128        is_fpd_set = ce.lo & VTD_CONTEXT_ENTRY_FPD;
1129        if (ret_fr) {
1130            ret_fr = -ret_fr;
1131            if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
1132                trace_vtd_fault_disabled();
1133            } else {
1134                vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
1135            }
1136            goto error;
1137        }
1138        /* Update context-cache */
1139        trace_vtd_iotlb_cc_update(bus_num, devfn, ce.hi, ce.lo,
1140                                  cc_entry->context_cache_gen,
1141                                  s->context_cache_gen);
1142        cc_entry->context_entry = ce;
1143        cc_entry->context_cache_gen = s->context_cache_gen;
1144    }
1145
1146    /*
1147     * We don't need to translate for pass-through context entries.
1148     * Also, let's ignore IOTLB caching as well for PT devices.
1149     */
1150    if (vtd_ce_get_type(&ce) == VTD_CONTEXT_TT_PASS_THROUGH) {
1151        entry->iova = addr & VTD_PAGE_MASK_4K;
1152        entry->translated_addr = entry->iova;
1153        entry->addr_mask = ~VTD_PAGE_MASK_4K;
1154        entry->perm = IOMMU_RW;
1155        trace_vtd_translate_pt(source_id, entry->iova);
1156
1157        /*
1158         * When this happens, it means firstly caching-mode is not
1159         * enabled, and this is the first passthrough translation for
1160         * the device. Let's enable the fast path for passthrough.
1161         *
1162         * When passthrough is disabled again for the device, we can
1163         * capture it via the context entry invalidation, then the
1164         * IOMMU region can be swapped back.
1165         */
1166        vtd_pt_enable_fast_path(s, source_id);
1167
1168        return true;
1169    }
1170
1171    ret_fr = vtd_iova_to_slpte(&ce, addr, is_write, &slpte, &level,
1172                               &reads, &writes, s->aw_bits);
1173    if (ret_fr) {
1174        ret_fr = -ret_fr;
1175        if (is_fpd_set && vtd_is_qualified_fault(ret_fr)) {
1176            trace_vtd_fault_disabled();
1177        } else {
1178            vtd_report_dmar_fault(s, source_id, addr, ret_fr, is_write);
1179        }
1180        goto error;
1181    }
1182
1183    page_mask = vtd_slpt_level_page_mask(level);
1184    access_flags = IOMMU_ACCESS_FLAG(reads, writes);
1185    vtd_update_iotlb(s, source_id, VTD_CONTEXT_ENTRY_DID(ce.hi), addr, slpte,
1186                     access_flags, level);
1187out:
1188    entry->iova = addr & page_mask;
1189    entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
1190    entry->addr_mask = ~page_mask;
1191    entry->perm = access_flags;
1192    return true;
1193
1194error:
1195    entry->iova = 0;
1196    entry->translated_addr = 0;
1197    entry->addr_mask = 0;
1198    entry->perm = IOMMU_NONE;
1199    return false;
1200}
1201
1202static void vtd_root_table_setup(IntelIOMMUState *s)
1203{
1204    s->root = vtd_get_quad_raw(s, DMAR_RTADDR_REG);
1205    s->root_extended = s->root & VTD_RTADDR_RTT;
1206    s->root &= VTD_RTADDR_ADDR_MASK(s->aw_bits);
1207
1208    trace_vtd_reg_dmar_root(s->root, s->root_extended);
1209}
1210
1211static void vtd_iec_notify_all(IntelIOMMUState *s, bool global,
1212                               uint32_t index, uint32_t mask)
1213{
1214    x86_iommu_iec_notify_all(X86_IOMMU_DEVICE(s), global, index, mask);
1215}
1216
1217static void vtd_interrupt_remap_table_setup(IntelIOMMUState *s)
1218{
1219    uint64_t value = 0;
1220    value = vtd_get_quad_raw(s, DMAR_IRTA_REG);
1221    s->intr_size = 1UL << ((value & VTD_IRTA_SIZE_MASK) + 1);
1222    s->intr_root = value & VTD_IRTA_ADDR_MASK(s->aw_bits);
1223    s->intr_eime = value & VTD_IRTA_EIME;
1224
1225    /* Notify global invalidation */
1226    vtd_iec_notify_all(s, true, 0, 0);
1227
1228    trace_vtd_reg_ir_root(s->intr_root, s->intr_size);
1229}
1230
1231static void vtd_iommu_replay_all(IntelIOMMUState *s)
1232{
1233    IntelIOMMUNotifierNode *node;
1234
1235    QLIST_FOREACH(node, &s->notifiers_list, next) {
1236        memory_region_iommu_replay_all(&node->vtd_as->iommu);
1237    }
1238}
1239
1240static void vtd_context_global_invalidate(IntelIOMMUState *s)
1241{
1242    trace_vtd_inv_desc_cc_global();
1243    s->context_cache_gen++;
1244    if (s->context_cache_gen == VTD_CONTEXT_CACHE_GEN_MAX) {
1245        vtd_reset_context_cache(s);
1246    }
1247    vtd_switch_address_space_all(s);
1248    /*
1249     * From VT-d spec 6.5.2.1, a global context entry invalidation
1250     * should be followed by a IOTLB global invalidation, so we should
1251     * be safe even without this. Hoewever, let's replay the region as
1252     * well to be safer, and go back here when we need finer tunes for
1253     * VT-d emulation codes.
1254     */
1255    vtd_iommu_replay_all(s);
1256}
1257
1258/* Do a context-cache device-selective invalidation.
1259 * @func_mask: FM field after shifting
1260 */
1261static void vtd_context_device_invalidate(IntelIOMMUState *s,
1262                                          uint16_t source_id,
1263                                          uint16_t func_mask)
1264{
1265    uint16_t mask;
1266    VTDBus *vtd_bus;
1267    VTDAddressSpace *vtd_as;
1268    uint8_t bus_n, devfn;
1269    uint16_t devfn_it;
1270
1271    trace_vtd_inv_desc_cc_devices(source_id, func_mask);
1272
1273    switch (func_mask & 3) {
1274    case 0:
1275        mask = 0;   /* No bits in the SID field masked */
1276        break;
1277    case 1:
1278        mask = 4;   /* Mask bit 2 in the SID field */
1279        break;
1280    case 2:
1281        mask = 6;   /* Mask bit 2:1 in the SID field */
1282        break;
1283    case 3:
1284        mask = 7;   /* Mask bit 2:0 in the SID field */
1285        break;
1286    }
1287    mask = ~mask;
1288
1289    bus_n = VTD_SID_TO_BUS(source_id);
1290    vtd_bus = vtd_find_as_from_bus_num(s, bus_n);
1291    if (vtd_bus) {
1292        devfn = VTD_SID_TO_DEVFN(source_id);
1293        for (devfn_it = 0; devfn_it < PCI_DEVFN_MAX; ++devfn_it) {
1294            vtd_as = vtd_bus->dev_as[devfn_it];
1295            if (vtd_as && ((devfn_it & mask) == (devfn & mask))) {
1296                trace_vtd_inv_desc_cc_device(bus_n, VTD_PCI_SLOT(devfn_it),
1297                                             VTD_PCI_FUNC(devfn_it));
1298                vtd_as->context_cache_entry.context_cache_gen = 0;
1299                /*
1300                 * Do switch address space when needed, in case if the
1301                 * device passthrough bit is switched.
1302                 */
1303                vtd_switch_address_space(vtd_as);
1304                /*
1305                 * So a device is moving out of (or moving into) a
1306                 * domain, a replay() suites here to notify all the
1307                 * IOMMU_NOTIFIER_MAP registers about this change.
1308                 * This won't bring bad even if we have no such
1309                 * notifier registered - the IOMMU notification
1310                 * framework will skip MAP notifications if that
1311                 * happened.
1312                 */
1313                memory_region_iommu_replay_all(&vtd_as->iommu);
1314            }
1315        }
1316    }
1317}
1318
1319/* Context-cache invalidation
1320 * Returns the Context Actual Invalidation Granularity.
1321 * @val: the content of the CCMD_REG
1322 */
1323static uint64_t vtd_context_cache_invalidate(IntelIOMMUState *s, uint64_t val)
1324{
1325    uint64_t caig;
1326    uint64_t type = val & VTD_CCMD_CIRG_MASK;
1327
1328    switch (type) {
1329    case VTD_CCMD_DOMAIN_INVL:
1330        /* Fall through */
1331    case VTD_CCMD_GLOBAL_INVL:
1332        caig = VTD_CCMD_GLOBAL_INVL_A;
1333        vtd_context_global_invalidate(s);
1334        break;
1335
1336    case VTD_CCMD_DEVICE_INVL:
1337        caig = VTD_CCMD_DEVICE_INVL_A;
1338        vtd_context_device_invalidate(s, VTD_CCMD_SID(val), VTD_CCMD_FM(val));
1339        break;
1340
1341    default:
1342        trace_vtd_err("Context cache invalidate type error.");
1343        caig = 0;
1344    }
1345    return caig;
1346}
1347
1348static void vtd_iotlb_global_invalidate(IntelIOMMUState *s)
1349{
1350    trace_vtd_inv_desc_iotlb_global();
1351    vtd_reset_iotlb(s);
1352    vtd_iommu_replay_all(s);
1353}
1354
1355static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
1356{
1357    IntelIOMMUNotifierNode *node;
1358    VTDContextEntry ce;
1359    VTDAddressSpace *vtd_as;
1360
1361    trace_vtd_inv_desc_iotlb_domain(domain_id);
1362
1363    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_domain,
1364                                &domain_id);
1365
1366    QLIST_FOREACH(node, &s->notifiers_list, next) {
1367        vtd_as = node->vtd_as;
1368        if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
1369                                      vtd_as->devfn, &ce) &&
1370            domain_id == VTD_CONTEXT_ENTRY_DID(ce.hi)) {
1371            memory_region_iommu_replay_all(&vtd_as->iommu);
1372        }
1373    }
1374}
1375
1376static int vtd_page_invalidate_notify_hook(IOMMUTLBEntry *entry,
1377                                           void *private)
1378{
1379    memory_region_notify_iommu((IOMMUMemoryRegion *)private, *entry);
1380    return 0;
1381}
1382
1383static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
1384                                           uint16_t domain_id, hwaddr addr,
1385                                           uint8_t am)
1386{
1387    IntelIOMMUNotifierNode *node;
1388    VTDContextEntry ce;
1389    int ret;
1390
1391    QLIST_FOREACH(node, &(s->notifiers_list), next) {
1392        VTDAddressSpace *vtd_as = node->vtd_as;
1393        ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
1394                                       vtd_as->devfn, &ce);
1395        if (!ret && domain_id == VTD_CONTEXT_ENTRY_DID(ce.hi)) {
1396            vtd_page_walk(&ce, addr, addr + (1 << am) * VTD_PAGE_SIZE,
1397                          vtd_page_invalidate_notify_hook,
1398                          (void *)&vtd_as->iommu, true, s->aw_bits);
1399        }
1400    }
1401}
1402
1403static void vtd_iotlb_page_invalidate(IntelIOMMUState *s, uint16_t domain_id,
1404                                      hwaddr addr, uint8_t am)
1405{
1406    VTDIOTLBPageInvInfo info;
1407
1408    trace_vtd_inv_desc_iotlb_pages(domain_id, addr, am);
1409
1410    assert(am <= VTD_MAMV);
1411    info.domain_id = domain_id;
1412    info.addr = addr;
1413    info.mask = ~((1 << am) - 1);
1414    g_hash_table_foreach_remove(s->iotlb, vtd_hash_remove_by_page, &info);
1415    vtd_iotlb_page_invalidate_notify(s, domain_id, addr, am);
1416}
1417
1418/* Flush IOTLB
1419 * Returns the IOTLB Actual Invalidation Granularity.
1420 * @val: the content of the IOTLB_REG
1421 */
1422static uint64_t vtd_iotlb_flush(IntelIOMMUState *s, uint64_t val)
1423{
1424    uint64_t iaig;
1425    uint64_t type = val & VTD_TLB_FLUSH_GRANU_MASK;
1426    uint16_t domain_id;
1427    hwaddr addr;
1428    uint8_t am;
1429
1430    switch (type) {
1431    case VTD_TLB_GLOBAL_FLUSH:
1432        iaig = VTD_TLB_GLOBAL_FLUSH_A;
1433        vtd_iotlb_global_invalidate(s);
1434        break;
1435
1436    case VTD_TLB_DSI_FLUSH:
1437        domain_id = VTD_TLB_DID(val);
1438        iaig = VTD_TLB_DSI_FLUSH_A;
1439        vtd_iotlb_domain_invalidate(s, domain_id);
1440        break;
1441
1442    case VTD_TLB_PSI_FLUSH:
1443        domain_id = VTD_TLB_DID(val);
1444        addr = vtd_get_quad_raw(s, DMAR_IVA_REG);
1445        am = VTD_IVA_AM(addr);
1446        addr = VTD_IVA_ADDR(addr);
1447        if (am > VTD_MAMV) {
1448            trace_vtd_err("IOTLB PSI flush: address mask overflow.");
1449            iaig = 0;
1450            break;
1451        }
1452        iaig = VTD_TLB_PSI_FLUSH_A;
1453        vtd_iotlb_page_invalidate(s, domain_id, addr, am);
1454        break;
1455
1456    default:
1457        trace_vtd_err("IOTLB flush: invalid granularity.");
1458        iaig = 0;
1459    }
1460    return iaig;
1461}
1462
1463static void vtd_fetch_inv_desc(IntelIOMMUState *s);
1464
1465static inline bool vtd_queued_inv_disable_check(IntelIOMMUState *s)
1466{
1467    return s->qi_enabled && (s->iq_tail == s->iq_head) &&
1468           (s->iq_last_desc_type == VTD_INV_DESC_WAIT);
1469}
1470
1471static void vtd_handle_gcmd_qie(IntelIOMMUState *s, bool en)
1472{
1473    uint64_t iqa_val = vtd_get_quad_raw(s, DMAR_IQA_REG);
1474
1475    trace_vtd_inv_qi_enable(en);
1476
1477    if (en) {
1478        s->iq = iqa_val & VTD_IQA_IQA_MASK(s->aw_bits);
1479        /* 2^(x+8) entries */
1480        s->iq_size = 1UL << ((iqa_val & VTD_IQA_QS) + 8);
1481        s->qi_enabled = true;
1482        trace_vtd_inv_qi_setup(s->iq, s->iq_size);
1483        /* Ok - report back to driver */
1484        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_QIES);
1485
1486        if (s->iq_tail != 0) {
1487            /*
1488             * This is a spec violation but Windows guests are known to set up
1489             * Queued Invalidation this way so we allow the write and process
1490             * Invalidation Descriptors right away.
1491             */
1492            trace_vtd_warn_invalid_qi_tail(s->iq_tail);
1493            if (!(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
1494                vtd_fetch_inv_desc(s);
1495            }
1496        }
1497    } else {
1498        if (vtd_queued_inv_disable_check(s)) {
1499            /* disable Queued Invalidation */
1500            vtd_set_quad_raw(s, DMAR_IQH_REG, 0);
1501            s->iq_head = 0;
1502            s->qi_enabled = false;
1503            /* Ok - report back to driver */
1504            vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_QIES, 0);
1505        } else {
1506            trace_vtd_err_qi_disable(s->iq_head, s->iq_tail, s->iq_last_desc_type);
1507        }
1508    }
1509}
1510
1511/* Set Root Table Pointer */
1512static void vtd_handle_gcmd_srtp(IntelIOMMUState *s)
1513{
1514    vtd_root_table_setup(s);
1515    /* Ok - report back to driver */
1516    vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_RTPS);
1517}
1518
1519/* Set Interrupt Remap Table Pointer */
1520static void vtd_handle_gcmd_sirtp(IntelIOMMUState *s)
1521{
1522    vtd_interrupt_remap_table_setup(s);
1523    /* Ok - report back to driver */
1524    vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRTPS);
1525}
1526
1527/* Handle Translation Enable/Disable */
1528static void vtd_handle_gcmd_te(IntelIOMMUState *s, bool en)
1529{
1530    if (s->dmar_enabled == en) {
1531        return;
1532    }
1533
1534    trace_vtd_dmar_enable(en);
1535
1536    if (en) {
1537        s->dmar_enabled = true;
1538        /* Ok - report back to driver */
1539        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_TES);
1540    } else {
1541        s->dmar_enabled = false;
1542
1543        /* Clear the index of Fault Recording Register */
1544        s->next_frcd_reg = 0;
1545        /* Ok - report back to driver */
1546        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_TES, 0);
1547    }
1548
1549    vtd_switch_address_space_all(s);
1550}
1551
1552/* Handle Interrupt Remap Enable/Disable */
1553static void vtd_handle_gcmd_ire(IntelIOMMUState *s, bool en)
1554{
1555    trace_vtd_ir_enable(en);
1556
1557    if (en) {
1558        s->intr_enabled = true;
1559        /* Ok - report back to driver */
1560        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, 0, VTD_GSTS_IRES);
1561    } else {
1562        s->intr_enabled = false;
1563        /* Ok - report back to driver */
1564        vtd_set_clear_mask_long(s, DMAR_GSTS_REG, VTD_GSTS_IRES, 0);
1565    }
1566}
1567
1568/* Handle write to Global Command Register */
1569static void vtd_handle_gcmd_write(IntelIOMMUState *s)
1570{
1571    uint32_t status = vtd_get_long_raw(s, DMAR_GSTS_REG);
1572    uint32_t val = vtd_get_long_raw(s, DMAR_GCMD_REG);
1573    uint32_t changed = status ^ val;
1574
1575    trace_vtd_reg_write_gcmd(status, val);
1576    if (changed & VTD_GCMD_TE) {
1577        /* Translation enable/disable */
1578        vtd_handle_gcmd_te(s, val & VTD_GCMD_TE);
1579    }
1580    if (val & VTD_GCMD_SRTP) {
1581        /* Set/update the root-table pointer */
1582        vtd_handle_gcmd_srtp(s);
1583    }
1584    if (changed & VTD_GCMD_QIE) {
1585        /* Queued Invalidation Enable */
1586        vtd_handle_gcmd_qie(s, val & VTD_GCMD_QIE);
1587    }
1588    if (val & VTD_GCMD_SIRTP) {
1589        /* Set/update the interrupt remapping root-table pointer */
1590        vtd_handle_gcmd_sirtp(s);
1591    }
1592    if (changed & VTD_GCMD_IRE) {
1593        /* Interrupt remap enable/disable */
1594        vtd_handle_gcmd_ire(s, val & VTD_GCMD_IRE);
1595    }
1596}
1597
1598/* Handle write to Context Command Register */
1599static void vtd_handle_ccmd_write(IntelIOMMUState *s)
1600{
1601    uint64_t ret;
1602    uint64_t val = vtd_get_quad_raw(s, DMAR_CCMD_REG);
1603
1604    /* Context-cache invalidation request */
1605    if (val & VTD_CCMD_ICC) {
1606        if (s->qi_enabled) {
1607            trace_vtd_err("Queued Invalidation enabled, "
1608                          "should not use register-based invalidation");
1609            return;
1610        }
1611        ret = vtd_context_cache_invalidate(s, val);
1612        /* Invalidation completed. Change something to show */
1613        vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_ICC, 0ULL);
1614        ret = vtd_set_clear_mask_quad(s, DMAR_CCMD_REG, VTD_CCMD_CAIG_MASK,
1615                                      ret);
1616    }
1617}
1618
1619/* Handle write to IOTLB Invalidation Register */
1620static void vtd_handle_iotlb_write(IntelIOMMUState *s)
1621{
1622    uint64_t ret;
1623    uint64_t val = vtd_get_quad_raw(s, DMAR_IOTLB_REG);
1624
1625    /* IOTLB invalidation request */
1626    if (val & VTD_TLB_IVT) {
1627        if (s->qi_enabled) {
1628            trace_vtd_err("Queued Invalidation enabled, "
1629                          "should not use register-based invalidation.");
1630            return;
1631        }
1632        ret = vtd_iotlb_flush(s, val);
1633        /* Invalidation completed. Change something to show */
1634        vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG, VTD_TLB_IVT, 0ULL);
1635        ret = vtd_set_clear_mask_quad(s, DMAR_IOTLB_REG,
1636                                      VTD_TLB_FLUSH_GRANU_MASK_A, ret);
1637    }
1638}
1639
1640/* Fetch an Invalidation Descriptor from the Invalidation Queue */
1641static bool vtd_get_inv_desc(dma_addr_t base_addr, uint32_t offset,
1642                             VTDInvDesc *inv_desc)
1643{
1644    dma_addr_t addr = base_addr + offset * sizeof(*inv_desc);
1645    if (dma_memory_read(&address_space_memory, addr, inv_desc,
1646        sizeof(*inv_desc))) {
1647        trace_vtd_err("Read INV DESC failed.");
1648        inv_desc->lo = 0;
1649        inv_desc->hi = 0;
1650        return false;
1651    }
1652    inv_desc->lo = le64_to_cpu(inv_desc->lo);
1653    inv_desc->hi = le64_to_cpu(inv_desc->hi);
1654    return true;
1655}
1656
1657static bool vtd_process_wait_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
1658{
1659    if ((inv_desc->hi & VTD_INV_DESC_WAIT_RSVD_HI) ||
1660        (inv_desc->lo & VTD_INV_DESC_WAIT_RSVD_LO)) {
1661        trace_vtd_inv_desc_wait_invalid(inv_desc->hi, inv_desc->lo);
1662        return false;
1663    }
1664    if (inv_desc->lo & VTD_INV_DESC_WAIT_SW) {
1665        /* Status Write */
1666        uint32_t status_data = (uint32_t)(inv_desc->lo >>
1667                               VTD_INV_DESC_WAIT_DATA_SHIFT);
1668
1669        assert(!(inv_desc->lo & VTD_INV_DESC_WAIT_IF));
1670
1671        /* FIXME: need to be masked with HAW? */
1672        dma_addr_t status_addr = inv_desc->hi;
1673        trace_vtd_inv_desc_wait_sw(status_addr, status_data);
1674        status_data = cpu_to_le32(status_data);
1675        if (dma_memory_write(&address_space_memory, status_addr, &status_data,
1676                             sizeof(status_data))) {
1677            trace_vtd_inv_desc_wait_write_fail(inv_desc->hi, inv_desc->lo);
1678            return false;
1679        }
1680    } else if (inv_desc->lo & VTD_INV_DESC_WAIT_IF) {
1681        /* Interrupt flag */
1682        vtd_generate_completion_event(s);
1683    } else {
1684        trace_vtd_inv_desc_wait_invalid(inv_desc->hi, inv_desc->lo);
1685        return false;
1686    }
1687    return true;
1688}
1689
1690static bool vtd_process_context_cache_desc(IntelIOMMUState *s,
1691                                           VTDInvDesc *inv_desc)
1692{
1693    uint16_t sid, fmask;
1694
1695    if ((inv_desc->lo & VTD_INV_DESC_CC_RSVD) || inv_desc->hi) {
1696        trace_vtd_inv_desc_cc_invalid(inv_desc->hi, inv_desc->lo);
1697        return false;
1698    }
1699    switch (inv_desc->lo & VTD_INV_DESC_CC_G) {
1700    case VTD_INV_DESC_CC_DOMAIN:
1701        trace_vtd_inv_desc_cc_domain(
1702            (uint16_t)VTD_INV_DESC_CC_DID(inv_desc->lo));
1703        /* Fall through */
1704    case VTD_INV_DESC_CC_GLOBAL:
1705        vtd_context_global_invalidate(s);
1706        break;
1707
1708    case VTD_INV_DESC_CC_DEVICE:
1709        sid = VTD_INV_DESC_CC_SID(inv_desc->lo);
1710        fmask = VTD_INV_DESC_CC_FM(inv_desc->lo);
1711        vtd_context_device_invalidate(s, sid, fmask);
1712        break;
1713
1714    default:
1715        trace_vtd_inv_desc_cc_invalid(inv_desc->hi, inv_desc->lo);
1716        return false;
1717    }
1718    return true;
1719}
1720
1721static bool vtd_process_iotlb_desc(IntelIOMMUState *s, VTDInvDesc *inv_desc)
1722{
1723    uint16_t domain_id;
1724    uint8_t am;
1725    hwaddr addr;
1726
1727    if ((inv_desc->lo & VTD_INV_DESC_IOTLB_RSVD_LO) ||
1728        (inv_desc->hi & VTD_INV_DESC_IOTLB_RSVD_HI)) {
1729        trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
1730        return false;
1731    }
1732
1733    switch (inv_desc->lo & VTD_INV_DESC_IOTLB_G) {
1734    case VTD_INV_DESC_IOTLB_GLOBAL:
1735        vtd_iotlb_global_invalidate(s);
1736        break;
1737
1738    case VTD_INV_DESC_IOTLB_DOMAIN:
1739        domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
1740        vtd_iotlb_domain_invalidate(s, domain_id);
1741        break;
1742
1743    case VTD_INV_DESC_IOTLB_PAGE:
1744        domain_id = VTD_INV_DESC_IOTLB_DID(inv_desc->lo);
1745        addr = VTD_INV_DESC_IOTLB_ADDR(inv_desc->hi);
1746        am = VTD_INV_DESC_IOTLB_AM(inv_desc->hi);
1747        if (am > VTD_MAMV) {
1748            trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
1749            return false;
1750        }
1751        vtd_iotlb_page_invalidate(s, domain_id, addr, am);
1752        break;
1753
1754    default:
1755        trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
1756        return false;
1757    }
1758    return true;
1759}
1760
1761static bool vtd_process_inv_iec_desc(IntelIOMMUState *s,
1762                                     VTDInvDesc *inv_desc)
1763{
1764    trace_vtd_inv_desc_iec(inv_desc->iec.granularity,
1765                           inv_desc->iec.index,
1766                           inv_desc->iec.index_mask);
1767
1768    vtd_iec_notify_all(s, !inv_desc->iec.granularity,
1769                       inv_desc->iec.index,
1770                       inv_desc->iec.index_mask);
1771    return true;
1772}
1773
1774static bool vtd_process_device_iotlb_desc(IntelIOMMUState *s,
1775                                          VTDInvDesc *inv_desc)
1776{
1777    VTDAddressSpace *vtd_dev_as;
1778    IOMMUTLBEntry entry;
1779    struct VTDBus *vtd_bus;
1780    hwaddr addr;
1781    uint64_t sz;
1782    uint16_t sid;
1783    uint8_t devfn;
1784    bool size;
1785    uint8_t bus_num;
1786
1787    addr = VTD_INV_DESC_DEVICE_IOTLB_ADDR(inv_desc->hi);
1788    sid = VTD_INV_DESC_DEVICE_IOTLB_SID(inv_desc->lo);
1789    devfn = sid & 0xff;
1790    bus_num = sid >> 8;
1791    size = VTD_INV_DESC_DEVICE_IOTLB_SIZE(inv_desc->hi);
1792
1793    if ((inv_desc->lo & VTD_INV_DESC_DEVICE_IOTLB_RSVD_LO) ||
1794        (inv_desc->hi & VTD_INV_DESC_DEVICE_IOTLB_RSVD_HI)) {
1795        trace_vtd_inv_desc_iotlb_invalid(inv_desc->hi, inv_desc->lo);
1796        return false;
1797    }
1798
1799    vtd_bus = vtd_find_as_from_bus_num(s, bus_num);
1800    if (!vtd_bus) {
1801        goto done;
1802    }
1803
1804    vtd_dev_as = vtd_bus->dev_as[devfn];
1805    if (!vtd_dev_as) {
1806        goto done;
1807    }
1808
1809    /* According to ATS spec table 2.4:
1810     * S = 0, bits 15:12 = xxxx     range size: 4K
1811     * S = 1, bits 15:12 = xxx0     range size: 8K
1812     * S = 1, bits 15:12 = xx01     range size: 16K
1813     * S = 1, bits 15:12 = x011     range size: 32K
1814     * S = 1, bits 15:12 = 0111     range size: 64K
1815     * ...
1816     */
1817    if (size) {
1818        sz = (VTD_PAGE_SIZE * 2) << cto64(addr >> VTD_PAGE_SHIFT);
1819        addr &= ~(sz - 1);
1820    } else {
1821        sz = VTD_PAGE_SIZE;
1822    }
1823
1824    entry.target_as = &vtd_dev_as->as;
1825    entry.addr_mask = sz - 1;
1826    entry.iova = addr;
1827    entry.perm = IOMMU_NONE;
1828    entry.translated_addr = 0;
1829    memory_region_notify_iommu(&vtd_dev_as->iommu, entry);
1830
1831done:
1832    return true;
1833}
1834
1835static bool vtd_process_inv_desc(IntelIOMMUState *s)
1836{
1837    VTDInvDesc inv_desc;
1838    uint8_t desc_type;
1839
1840    trace_vtd_inv_qi_head(s->iq_head);
1841    if (!vtd_get_inv_desc(s->iq, s->iq_head, &inv_desc)) {
1842        s->iq_last_desc_type = VTD_INV_DESC_NONE;
1843        return false;
1844    }
1845    desc_type = inv_desc.lo & VTD_INV_DESC_TYPE;
1846    /* FIXME: should update at first or at last? */
1847    s->iq_last_desc_type = desc_type;
1848
1849    switch (desc_type) {
1850    case VTD_INV_DESC_CC:
1851        trace_vtd_inv_desc("context-cache", inv_desc.hi, inv_desc.lo);
1852        if (!vtd_process_context_cache_desc(s, &inv_desc)) {
1853            return false;
1854        }
1855        break;
1856
1857    case VTD_INV_DESC_IOTLB:
1858        trace_vtd_inv_desc("iotlb", inv_desc.hi, inv_desc.lo);
1859        if (!vtd_process_iotlb_desc(s, &inv_desc)) {
1860            return false;
1861        }
1862        break;
1863
1864    case VTD_INV_DESC_WAIT:
1865        trace_vtd_inv_desc("wait", inv_desc.hi, inv_desc.lo);
1866        if (!vtd_process_wait_desc(s, &inv_desc)) {
1867            return false;
1868        }
1869        break;
1870
1871    case VTD_INV_DESC_IEC:
1872        trace_vtd_inv_desc("iec", inv_desc.hi, inv_desc.lo);
1873        if (!vtd_process_inv_iec_desc(s, &inv_desc)) {
1874            return false;
1875        }
1876        break;
1877
1878    case VTD_INV_DESC_DEVICE:
1879        trace_vtd_inv_desc("device", inv_desc.hi, inv_desc.lo);
1880        if (!vtd_process_device_iotlb_desc(s, &inv_desc)) {
1881            return false;
1882        }
1883        break;
1884
1885    default:
1886        trace_vtd_inv_desc_invalid(inv_desc.hi, inv_desc.lo);
1887        return false;
1888    }
1889    s->iq_head++;
1890    if (s->iq_head == s->iq_size) {
1891        s->iq_head = 0;
1892    }
1893    return true;
1894}
1895
1896/* Try to fetch and process more Invalidation Descriptors */
1897static void vtd_fetch_inv_desc(IntelIOMMUState *s)
1898{
1899    trace_vtd_inv_qi_fetch();
1900
1901    if (s->iq_tail >= s->iq_size) {
1902        /* Detects an invalid Tail pointer */
1903        trace_vtd_err_qi_tail(s->iq_tail, s->iq_size);
1904        vtd_handle_inv_queue_error(s);
1905        return;
1906    }
1907    while (s->iq_head != s->iq_tail) {
1908        if (!vtd_process_inv_desc(s)) {
1909            /* Invalidation Queue Errors */
1910            vtd_handle_inv_queue_error(s);
1911            break;
1912        }
1913        /* Must update the IQH_REG in time */
1914        vtd_set_quad_raw(s, DMAR_IQH_REG,
1915                         (((uint64_t)(s->iq_head)) << VTD_IQH_QH_SHIFT) &
1916                         VTD_IQH_QH_MASK);
1917    }
1918}
1919
1920/* Handle write to Invalidation Queue Tail Register */
1921static void vtd_handle_iqt_write(IntelIOMMUState *s)
1922{
1923    uint64_t val = vtd_get_quad_raw(s, DMAR_IQT_REG);
1924
1925    s->iq_tail = VTD_IQT_QT(val);
1926    trace_vtd_inv_qi_tail(s->iq_tail);
1927
1928    if (s->qi_enabled && !(vtd_get_long_raw(s, DMAR_FSTS_REG) & VTD_FSTS_IQE)) {
1929        /* Process Invalidation Queue here */
1930        vtd_fetch_inv_desc(s);
1931    }
1932}
1933
1934static void vtd_handle_fsts_write(IntelIOMMUState *s)
1935{
1936    uint32_t fsts_reg = vtd_get_long_raw(s, DMAR_FSTS_REG);
1937    uint32_t fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
1938    uint32_t status_fields = VTD_FSTS_PFO | VTD_FSTS_PPF | VTD_FSTS_IQE;
1939
1940    if ((fectl_reg & VTD_FECTL_IP) && !(fsts_reg & status_fields)) {
1941        vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
1942        trace_vtd_fsts_clear_ip();
1943    }
1944    /* FIXME: when IQE is Clear, should we try to fetch some Invalidation
1945     * Descriptors if there are any when Queued Invalidation is enabled?
1946     */
1947}
1948
1949static void vtd_handle_fectl_write(IntelIOMMUState *s)
1950{
1951    uint32_t fectl_reg;
1952    /* FIXME: when software clears the IM field, check the IP field. But do we
1953     * need to compare the old value and the new value to conclude that
1954     * software clears the IM field? Or just check if the IM field is zero?
1955     */
1956    fectl_reg = vtd_get_long_raw(s, DMAR_FECTL_REG);
1957
1958    trace_vtd_reg_write_fectl(fectl_reg);
1959
1960    if ((fectl_reg & VTD_FECTL_IP) && !(fectl_reg & VTD_FECTL_IM)) {
1961        vtd_generate_interrupt(s, DMAR_FEADDR_REG, DMAR_FEDATA_REG);
1962        vtd_set_clear_mask_long(s, DMAR_FECTL_REG, VTD_FECTL_IP, 0);
1963    }
1964}
1965
1966static void vtd_handle_ics_write(IntelIOMMUState *s)
1967{
1968    uint32_t ics_reg = vtd_get_long_raw(s, DMAR_ICS_REG);
1969    uint32_t iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
1970
1971    if ((iectl_reg & VTD_IECTL_IP) && !(ics_reg & VTD_ICS_IWC)) {
1972        trace_vtd_reg_ics_clear_ip();
1973        vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
1974    }
1975}
1976
1977static void vtd_handle_iectl_write(IntelIOMMUState *s)
1978{
1979    uint32_t iectl_reg;
1980    /* FIXME: when software clears the IM field, check the IP field. But do we
1981     * need to compare the old value and the new value to conclude that
1982     * software clears the IM field? Or just check if the IM field is zero?
1983     */
1984    iectl_reg = vtd_get_long_raw(s, DMAR_IECTL_REG);
1985
1986    trace_vtd_reg_write_iectl(iectl_reg);
1987
1988    if ((iectl_reg & VTD_IECTL_IP) && !(iectl_reg & VTD_IECTL_IM)) {
1989        vtd_generate_interrupt(s, DMAR_IEADDR_REG, DMAR_IEDATA_REG);
1990        vtd_set_clear_mask_long(s, DMAR_IECTL_REG, VTD_IECTL_IP, 0);
1991    }
1992}
1993
1994static uint64_t vtd_mem_read(void *opaque, hwaddr addr, unsigned size)
1995{
1996    IntelIOMMUState *s = opaque;
1997    uint64_t val;
1998
1999    trace_vtd_reg_read(addr, size);
2000
2001    if (addr + size > DMAR_REG_SIZE) {
2002        trace_vtd_err("Read MMIO over range.");
2003        return (uint64_t)-1;
2004    }
2005
2006    switch (addr) {
2007    /* Root Table Address Register, 64-bit */
2008    case DMAR_RTADDR_REG:
2009        if (size == 4) {
2010            val = s->root & ((1ULL << 32) - 1);
2011        } else {
2012            val = s->root;
2013        }
2014        break;
2015
2016    case DMAR_RTADDR_REG_HI:
2017        assert(size == 4);
2018        val = s->root >> 32;
2019        break;
2020
2021    /* Invalidation Queue Address Register, 64-bit */
2022    case DMAR_IQA_REG:
2023        val = s->iq | (vtd_get_quad(s, DMAR_IQA_REG) & VTD_IQA_QS);
2024        if (size == 4) {
2025            val = val & ((1ULL << 32) - 1);
2026        }
2027        break;
2028
2029    case DMAR_IQA_REG_HI:
2030        assert(size == 4);
2031        val = s->iq >> 32;
2032        break;
2033
2034    default:
2035        if (size == 4) {
2036            val = vtd_get_long(s, addr);
2037        } else {
2038            val = vtd_get_quad(s, addr);
2039        }
2040    }
2041
2042    return val;
2043}
2044
2045static void vtd_mem_write(void *opaque, hwaddr addr,
2046                          uint64_t val, unsigned size)
2047{
2048    IntelIOMMUState *s = opaque;
2049
2050    trace_vtd_reg_write(addr, size, val);
2051
2052    if (addr + size > DMAR_REG_SIZE) {
2053        trace_vtd_err("Write MMIO over range.");
2054        return;
2055    }
2056
2057    switch (addr) {
2058    /* Global Command Register, 32-bit */
2059    case DMAR_GCMD_REG:
2060        vtd_set_long(s, addr, val);
2061        vtd_handle_gcmd_write(s);
2062        break;
2063
2064    /* Context Command Register, 64-bit */
2065    case DMAR_CCMD_REG:
2066        if (size == 4) {
2067            vtd_set_long(s, addr, val);
2068        } else {
2069            vtd_set_quad(s, addr, val);
2070            vtd_handle_ccmd_write(s);
2071        }
2072        break;
2073
2074    case DMAR_CCMD_REG_HI:
2075        assert(size == 4);
2076        vtd_set_long(s, addr, val);
2077        vtd_handle_ccmd_write(s);
2078        break;
2079
2080    /* IOTLB Invalidation Register, 64-bit */
2081    case DMAR_IOTLB_REG:
2082        if (size == 4) {
2083            vtd_set_long(s, addr, val);
2084        } else {
2085            vtd_set_quad(s, addr, val);
2086            vtd_handle_iotlb_write(s);
2087        }
2088        break;
2089
2090    case DMAR_IOTLB_REG_HI:
2091        assert(size == 4);
2092        vtd_set_long(s, addr, val);
2093        vtd_handle_iotlb_write(s);
2094        break;
2095
2096    /* Invalidate Address Register, 64-bit */
2097    case DMAR_IVA_REG:
2098        if (size == 4) {
2099            vtd_set_long(s, addr, val);
2100        } else {
2101            vtd_set_quad(s, addr, val);
2102        }
2103        break;
2104
2105    case DMAR_IVA_REG_HI:
2106        assert(size == 4);
2107        vtd_set_long(s, addr, val);
2108        break;
2109
2110    /* Fault Status Register, 32-bit */
2111    case DMAR_FSTS_REG:
2112        assert(size == 4);
2113        vtd_set_long(s, addr, val);
2114        vtd_handle_fsts_write(s);
2115        break;
2116
2117    /* Fault Event Control Register, 32-bit */
2118    case DMAR_FECTL_REG:
2119        assert(size == 4);
2120        vtd_set_long(s, addr, val);
2121        vtd_handle_fectl_write(s);
2122        break;
2123
2124    /* Fault Event Data Register, 32-bit */
2125    case DMAR_FEDATA_REG:
2126        assert(size == 4);
2127        vtd_set_long(s, addr, val);
2128        break;
2129
2130    /* Fault Event Address Register, 32-bit */
2131    case DMAR_FEADDR_REG:
2132        if (size == 4) {
2133            vtd_set_long(s, addr, val);
2134        } else {
2135            /*
2136             * While the register is 32-bit only, some guests (Xen...) write to
2137             * it with 64-bit.
2138             */
2139            vtd_set_quad(s, addr, val);
2140        }
2141        break;
2142
2143    /* Fault Event Upper Address Register, 32-bit */
2144    case DMAR_FEUADDR_REG:
2145        assert(size == 4);
2146        vtd_set_long(s, addr, val);
2147        break;
2148
2149    /* Protected Memory Enable Register, 32-bit */
2150    case DMAR_PMEN_REG:
2151        assert(size == 4);
2152        vtd_set_long(s, addr, val);
2153        break;
2154
2155    /* Root Table Address Register, 64-bit */
2156    case DMAR_RTADDR_REG:
2157        if (size == 4) {
2158            vtd_set_long(s, addr, val);
2159        } else {
2160            vtd_set_quad(s, addr, val);
2161        }
2162        break;
2163
2164    case DMAR_RTADDR_REG_HI:
2165        assert(size == 4);
2166        vtd_set_long(s, addr, val);
2167        break;
2168
2169    /* Invalidation Queue Tail Register, 64-bit */
2170    case DMAR_IQT_REG:
2171        if (size == 4) {
2172            vtd_set_long(s, addr, val);
2173        } else {
2174            vtd_set_quad(s, addr, val);
2175        }
2176        vtd_handle_iqt_write(s);
2177        break;
2178
2179    case DMAR_IQT_REG_HI:
2180        assert(size == 4);
2181        vtd_set_long(s, addr, val);
2182        /* 19:63 of IQT_REG is RsvdZ, do nothing here */
2183        break;
2184
2185    /* Invalidation Queue Address Register, 64-bit */
2186    case DMAR_IQA_REG:
2187        if (size == 4) {
2188            vtd_set_long(s, addr, val);
2189        } else {
2190            vtd_set_quad(s, addr, val);
2191        }
2192        break;
2193
2194    case DMAR_IQA_REG_HI:
2195        assert(size == 4);
2196        vtd_set_long(s, addr, val);
2197        break;
2198
2199    /* Invalidation Completion Status Register, 32-bit */
2200    case DMAR_ICS_REG:
2201        assert(size == 4);
2202        vtd_set_long(s, addr, val);
2203        vtd_handle_ics_write(s);
2204        break;
2205
2206    /* Invalidation Event Control Register, 32-bit */
2207    case DMAR_IECTL_REG:
2208        assert(size == 4);
2209        vtd_set_long(s, addr, val);
2210        vtd_handle_iectl_write(s);
2211        break;
2212
2213    /* Invalidation Event Data Register, 32-bit */
2214    case DMAR_IEDATA_REG:
2215        assert(size == 4);
2216        vtd_set_long(s, addr, val);
2217        break;
2218
2219    /* Invalidation Event Address Register, 32-bit */
2220    case DMAR_IEADDR_REG:
2221        assert(size == 4);
2222        vtd_set_long(s, addr, val);
2223        break;
2224
2225    /* Invalidation Event Upper Address Register, 32-bit */
2226    case DMAR_IEUADDR_REG:
2227        assert(size == 4);
2228        vtd_set_long(s, addr, val);
2229        break;
2230
2231    /* Fault Recording Registers, 128-bit */
2232    case DMAR_FRCD_REG_0_0:
2233        if (size == 4) {
2234            vtd_set_long(s, addr, val);
2235        } else {
2236            vtd_set_quad(s, addr, val);
2237        }
2238        break;
2239
2240    case DMAR_FRCD_REG_0_1:
2241        assert(size == 4);
2242        vtd_set_long(s, addr, val);
2243        break;
2244
2245    case DMAR_FRCD_REG_0_2:
2246        if (size == 4) {
2247            vtd_set_long(s, addr, val);
2248        } else {
2249            vtd_set_quad(s, addr, val);
2250            /* May clear bit 127 (Fault), update PPF */
2251            vtd_update_fsts_ppf(s);
2252        }
2253        break;
2254
2255    case DMAR_FRCD_REG_0_3:
2256        assert(size == 4);
2257        vtd_set_long(s, addr, val);
2258        /* May clear bit 127 (Fault), update PPF */
2259        vtd_update_fsts_ppf(s);
2260        break;
2261
2262    case DMAR_IRTA_REG:
2263        if (size == 4) {
2264            vtd_set_long(s, addr, val);
2265        } else {
2266            vtd_set_quad(s, addr, val);
2267        }
2268        break;
2269
2270    case DMAR_IRTA_REG_HI:
2271        assert(size == 4);
2272        vtd_set_long(s, addr, val);
2273        break;
2274
2275    default:
2276        if (size == 4) {
2277            vtd_set_long(s, addr, val);
2278        } else {
2279            vtd_set_quad(s, addr, val);
2280        }
2281    }
2282}
2283
2284static IOMMUTLBEntry vtd_iommu_translate(IOMMUMemoryRegion *iommu, hwaddr addr,
2285                                         IOMMUAccessFlags flag)
2286{
2287    VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
2288    IntelIOMMUState *s = vtd_as->iommu_state;
2289    IOMMUTLBEntry iotlb = {
2290        /* We'll fill in the rest later. */
2291        .target_as = &address_space_memory,
2292    };
2293    bool success;
2294
2295    if (likely(s->dmar_enabled)) {
2296        success = vtd_do_iommu_translate(vtd_as, vtd_as->bus, vtd_as->devfn,
2297                                         addr, flag & IOMMU_WO, &iotlb);
2298    } else {
2299        /* DMAR disabled, passthrough, use 4k-page*/
2300        iotlb.iova = addr & VTD_PAGE_MASK_4K;
2301        iotlb.translated_addr = addr & VTD_PAGE_MASK_4K;
2302        iotlb.addr_mask = ~VTD_PAGE_MASK_4K;
2303        iotlb.perm = IOMMU_RW;
2304        success = true;
2305    }
2306
2307    if (likely(success)) {
2308        trace_vtd_dmar_translate(pci_bus_num(vtd_as->bus),
2309                                 VTD_PCI_SLOT(vtd_as->devfn),
2310                                 VTD_PCI_FUNC(vtd_as->devfn),
2311                                 iotlb.iova, iotlb.translated_addr,
2312                                 iotlb.addr_mask);
2313    } else {
2314        trace_vtd_err_dmar_translate(pci_bus_num(vtd_as->bus),
2315                                     VTD_PCI_SLOT(vtd_as->devfn),
2316                                     VTD_PCI_FUNC(vtd_as->devfn),
2317                                     iotlb.iova);
2318    }
2319
2320    return iotlb;
2321}
2322
2323static void vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
2324                                          IOMMUNotifierFlag old,
2325                                          IOMMUNotifierFlag new)
2326{
2327    VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
2328    IntelIOMMUState *s = vtd_as->iommu_state;
2329    IntelIOMMUNotifierNode *node = NULL;
2330    IntelIOMMUNotifierNode *next_node = NULL;
2331
2332    if (!s->caching_mode && new & IOMMU_NOTIFIER_MAP) {
2333        error_report("We need to set caching-mode=1 for intel-iommu to enable "
2334                     "device assignment with IOMMU protection.");
2335        exit(1);
2336    }
2337
2338    if (old == IOMMU_NOTIFIER_NONE) {
2339        node = g_malloc0(sizeof(*node));
2340        node->vtd_as = vtd_as;
2341        QLIST_INSERT_HEAD(&s->notifiers_list, node, next);
2342        return;
2343    }
2344
2345    /* update notifier node with new flags */
2346    QLIST_FOREACH_SAFE(node, &s->notifiers_list, next, next_node) {
2347        if (node->vtd_as == vtd_as) {
2348            if (new == IOMMU_NOTIFIER_NONE) {
2349                QLIST_REMOVE(node, next);
2350                g_free(node);
2351            }
2352            return;
2353        }
2354    }
2355}
2356
2357static int vtd_post_load(void *opaque, int version_id)
2358{
2359    IntelIOMMUState *iommu = opaque;
2360
2361    /*
2362     * Memory regions are dynamically turned on/off depending on
2363     * context entry configurations from the guest. After migration,
2364     * we need to make sure the memory regions are still correct.
2365     */
2366    vtd_switch_address_space_all(iommu);
2367
2368    return 0;
2369}
2370
2371static const VMStateDescription vtd_vmstate = {
2372    .name = "iommu-intel",
2373    .version_id = 1,
2374    .minimum_version_id = 1,
2375    .priority = MIG_PRI_IOMMU,
2376    .post_load = vtd_post_load,
2377    .fields = (VMStateField[]) {
2378        VMSTATE_UINT64(root, IntelIOMMUState),
2379        VMSTATE_UINT64(intr_root, IntelIOMMUState),
2380        VMSTATE_UINT64(iq, IntelIOMMUState),
2381        VMSTATE_UINT32(intr_size, IntelIOMMUState),
2382        VMSTATE_UINT16(iq_head, IntelIOMMUState),
2383        VMSTATE_UINT16(iq_tail, IntelIOMMUState),
2384        VMSTATE_UINT16(iq_size, IntelIOMMUState),
2385        VMSTATE_UINT16(next_frcd_reg, IntelIOMMUState),
2386        VMSTATE_UINT8_ARRAY(csr, IntelIOMMUState, DMAR_REG_SIZE),
2387        VMSTATE_UINT8(iq_last_desc_type, IntelIOMMUState),
2388        VMSTATE_BOOL(root_extended, IntelIOMMUState),
2389        VMSTATE_BOOL(dmar_enabled, IntelIOMMUState),
2390        VMSTATE_BOOL(qi_enabled, IntelIOMMUState),
2391        VMSTATE_BOOL(intr_enabled, IntelIOMMUState),
2392        VMSTATE_BOOL(intr_eime, IntelIOMMUState),
2393        VMSTATE_END_OF_LIST()
2394    }
2395};
2396
2397static const MemoryRegionOps vtd_mem_ops = {
2398    .read = vtd_mem_read,
2399    .write = vtd_mem_write,
2400    .endianness = DEVICE_LITTLE_ENDIAN,
2401    .impl = {
2402        .min_access_size = 4,
2403        .max_access_size = 8,
2404    },
2405    .valid = {
2406        .min_access_size = 4,
2407        .max_access_size = 8,
2408    },
2409};
2410
2411static Property vtd_properties[] = {
2412    DEFINE_PROP_UINT32("version", IntelIOMMUState, version, 0),
2413    DEFINE_PROP_ON_OFF_AUTO("eim", IntelIOMMUState, intr_eim,
2414                            ON_OFF_AUTO_AUTO),
2415    DEFINE_PROP_BOOL("x-buggy-eim", IntelIOMMUState, buggy_eim, false),
2416    DEFINE_PROP_UINT8("x-aw-bits", IntelIOMMUState, aw_bits,
2417                      VTD_HOST_ADDRESS_WIDTH),
2418    DEFINE_PROP_BOOL("caching-mode", IntelIOMMUState, caching_mode, FALSE),
2419    DEFINE_PROP_END_OF_LIST(),
2420};
2421
2422/* Read IRTE entry with specific index */
2423static int vtd_irte_get(IntelIOMMUState *iommu, uint16_t index,
2424                        VTD_IR_TableEntry *entry, uint16_t sid)
2425{
2426    static const uint16_t vtd_svt_mask[VTD_SQ_MAX] = \
2427        {0xffff, 0xfffb, 0xfff9, 0xfff8};
2428    dma_addr_t addr = 0x00;
2429    uint16_t mask, source_id;
2430    uint8_t bus, bus_max, bus_min;
2431
2432    addr = iommu->intr_root + index * sizeof(*entry);
2433    if (dma_memory_read(&address_space_memory, addr, entry,
2434                        sizeof(*entry))) {
2435        trace_vtd_err("Memory read failed for IRTE.");
2436        return -VTD_FR_IR_ROOT_INVAL;
2437    }
2438
2439    trace_vtd_ir_irte_get(index, le64_to_cpu(entry->data[1]),
2440                          le64_to_cpu(entry->data[0]));
2441
2442    if (!entry->irte.present) {
2443        trace_vtd_err_irte(index, le64_to_cpu(entry->data[1]),
2444                           le64_to_cpu(entry->data[0]));
2445        return -VTD_FR_IR_ENTRY_P;
2446    }
2447
2448    if (entry->irte.__reserved_0 || entry->irte.__reserved_1 ||
2449        entry->irte.__reserved_2) {
2450        trace_vtd_err_irte(index, le64_to_cpu(entry->data[1]),
2451                           le64_to_cpu(entry->data[0]));
2452        return -VTD_FR_IR_IRTE_RSVD;
2453    }
2454
2455    if (sid != X86_IOMMU_SID_INVALID) {
2456        /* Validate IRTE SID */
2457        source_id = le32_to_cpu(entry->irte.source_id);
2458        switch (entry->irte.sid_vtype) {
2459        case VTD_SVT_NONE:
2460            break;
2461
2462        case VTD_SVT_ALL:
2463            mask = vtd_svt_mask[entry->irte.sid_q];
2464            if ((source_id & mask) != (sid & mask)) {
2465                trace_vtd_err_irte_sid(index, sid, source_id);
2466                return -VTD_FR_IR_SID_ERR;
2467            }
2468            break;
2469
2470        case VTD_SVT_BUS:
2471            bus_max = source_id >> 8;
2472            bus_min = source_id & 0xff;
2473            bus = sid >> 8;
2474            if (bus > bus_max || bus < bus_min) {
2475                trace_vtd_err_irte_sid_bus(index, bus, bus_min, bus_max);
2476                return -VTD_FR_IR_SID_ERR;
2477            }
2478            break;
2479
2480        default:
2481            trace_vtd_err_irte_svt(index, entry->irte.sid_vtype);
2482            /* Take this as verification failure. */
2483            return -VTD_FR_IR_SID_ERR;
2484            break;
2485        }
2486    }
2487
2488    return 0;
2489}
2490
2491/* Fetch IRQ information of specific IR index */
2492static int vtd_remap_irq_get(IntelIOMMUState *iommu, uint16_t index,
2493                             VTDIrq *irq, uint16_t sid)
2494{
2495    VTD_IR_TableEntry irte = {};
2496    int ret = 0;
2497
2498    ret = vtd_irte_get(iommu, index, &irte, sid);
2499    if (ret) {
2500        return ret;
2501    }
2502
2503    irq->trigger_mode = irte.irte.trigger_mode;
2504    irq->vector = irte.irte.vector;
2505    irq->delivery_mode = irte.irte.delivery_mode;
2506    irq->dest = le32_to_cpu(irte.irte.dest_id);
2507    if (!iommu->intr_eime) {
2508#define  VTD_IR_APIC_DEST_MASK         (0xff00ULL)
2509#define  VTD_IR_APIC_DEST_SHIFT        (8)
2510        irq->dest = (irq->dest & VTD_IR_APIC_DEST_MASK) >>
2511            VTD_IR_APIC_DEST_SHIFT;
2512    }
2513    irq->dest_mode = irte.irte.dest_mode;
2514    irq->redir_hint = irte.irte.redir_hint;
2515
2516    trace_vtd_ir_remap(index, irq->trigger_mode, irq->vector,
2517                       irq->delivery_mode, irq->dest, irq->dest_mode);
2518
2519    return 0;
2520}
2521
2522/* Generate one MSI message from VTDIrq info */
2523static void vtd_generate_msi_message(VTDIrq *irq, MSIMessage *msg_out)
2524{
2525    VTD_MSIMessage msg = {};
2526
2527    /* Generate address bits */
2528    msg.dest_mode = irq->dest_mode;
2529    msg.redir_hint = irq->redir_hint;
2530    msg.dest = irq->dest;
2531    msg.__addr_hi = irq->dest & 0xffffff00;
2532    msg.__addr_head = cpu_to_le32(0xfee);
2533    /* Keep this from original MSI address bits */
2534    msg.__not_used = irq->msi_addr_last_bits;
2535
2536    /* Generate data bits */
2537    msg.vector = irq->vector;
2538    msg.delivery_mode = irq->delivery_mode;
2539    msg.level = 1;
2540    msg.trigger_mode = irq->trigger_mode;
2541
2542    msg_out->address = msg.msi_addr;
2543    msg_out->data = msg.msi_data;
2544}
2545
2546/* Interrupt remapping for MSI/MSI-X entry */
2547static int vtd_interrupt_remap_msi(IntelIOMMUState *iommu,
2548                                   MSIMessage *origin,
2549                                   MSIMessage *translated,
2550                                   uint16_t sid)
2551{
2552    int ret = 0;
2553    VTD_IR_MSIAddress addr;
2554    uint16_t index;
2555    VTDIrq irq = {};
2556
2557    assert(origin && translated);
2558
2559    trace_vtd_ir_remap_msi_req(origin->address, origin->data);
2560
2561    if (!iommu || !iommu->intr_enabled) {
2562        memcpy(translated, origin, sizeof(*origin));
2563        goto out;
2564    }
2565
2566    if (origin->address & VTD_MSI_ADDR_HI_MASK) {
2567        trace_vtd_err("MSI address high 32 bits non-zero when "
2568                      "Interrupt Remapping enabled.");
2569        return -VTD_FR_IR_REQ_RSVD;
2570    }
2571
2572    addr.data = origin->address & VTD_MSI_ADDR_LO_MASK;
2573    if (addr.addr.__head != 0xfee) {
2574        trace_vtd_err("MSI addr low 32 bit invalid.");
2575        return -VTD_FR_IR_REQ_RSVD;
2576    }
2577
2578    /* This is compatible mode. */
2579    if (addr.addr.int_mode != VTD_IR_INT_FORMAT_REMAP) {
2580        memcpy(translated, origin, sizeof(*origin));
2581        goto out;
2582    }
2583
2584    index = addr.addr.index_h << 15 | le16_to_cpu(addr.addr.index_l);
2585
2586#define  VTD_IR_MSI_DATA_SUBHANDLE       (0x0000ffff)
2587#define  VTD_IR_MSI_DATA_RESERVED        (0xffff0000)
2588
2589    if (addr.addr.sub_valid) {
2590        /* See VT-d spec 5.1.2.2 and 5.1.3 on subhandle */
2591        index += origin->data & VTD_IR_MSI_DATA_SUBHANDLE;
2592    }
2593
2594    ret = vtd_remap_irq_get(iommu, index, &irq, sid);
2595    if (ret) {
2596        return ret;
2597    }
2598
2599    if (addr.addr.sub_valid) {
2600        trace_vtd_ir_remap_type("MSI");
2601        if (origin->data & VTD_IR_MSI_DATA_RESERVED) {
2602            trace_vtd_err_ir_msi_invalid(sid, origin->address, origin->data);
2603            return -VTD_FR_IR_REQ_RSVD;
2604        }
2605    } else {
2606        uint8_t vector = origin->data & 0xff;
2607        uint8_t trigger_mode = (origin->data >> MSI_DATA_TRIGGER_SHIFT) & 0x1;
2608
2609        trace_vtd_ir_remap_type("IOAPIC");
2610        /* IOAPIC entry vector should be aligned with IRTE vector
2611         * (see vt-d spec 5.1.5.1). */
2612        if (vector != irq.vector) {
2613            trace_vtd_warn_ir_vector(sid, index, vector, irq.vector);
2614        }
2615
2616        /* The Trigger Mode field must match the Trigger Mode in the IRTE.
2617         * (see vt-d spec 5.1.5.1). */
2618        if (trigger_mode != irq.trigger_mode) {
2619            trace_vtd_warn_ir_trigger(sid, index, trigger_mode,
2620                                      irq.trigger_mode);
2621        }
2622    }
2623
2624    /*
2625     * We'd better keep the last two bits, assuming that guest OS
2626     * might modify it. Keep it does not hurt after all.
2627     */
2628    irq.msi_addr_last_bits = addr.addr.__not_care;
2629
2630    /* Translate VTDIrq to MSI message */
2631    vtd_generate_msi_message(&irq, translated);
2632
2633out:
2634    trace_vtd_ir_remap_msi(origin->address, origin->data,
2635                           translated->address, translated->data);
2636    return 0;
2637}
2638
2639static int vtd_int_remap(X86IOMMUState *iommu, MSIMessage *src,
2640                         MSIMessage *dst, uint16_t sid)
2641{
2642    return vtd_interrupt_remap_msi(INTEL_IOMMU_DEVICE(iommu),
2643                                   src, dst, sid);
2644}
2645
2646static MemTxResult vtd_mem_ir_read(void *opaque, hwaddr addr,
2647                                   uint64_t *data, unsigned size,
2648                                   MemTxAttrs attrs)
2649{
2650    return MEMTX_OK;
2651}
2652
2653static MemTxResult vtd_mem_ir_write(void *opaque, hwaddr addr,
2654                                    uint64_t value, unsigned size,
2655                                    MemTxAttrs attrs)
2656{
2657    int ret = 0;
2658    MSIMessage from = {}, to = {};
2659    uint16_t sid = X86_IOMMU_SID_INVALID;
2660
2661    from.address = (uint64_t) addr + VTD_INTERRUPT_ADDR_FIRST;
2662    from.data = (uint32_t) value;
2663
2664    if (!attrs.unspecified) {
2665        /* We have explicit Source ID */
2666        sid = attrs.requester_id;
2667    }
2668
2669    ret = vtd_interrupt_remap_msi(opaque, &from, &to, sid);
2670    if (ret) {
2671        /* TODO: report error */
2672        /* Drop this interrupt */
2673        return MEMTX_ERROR;
2674    }
2675
2676    apic_get_class()->send_msi(&to);
2677
2678    return MEMTX_OK;
2679}
2680
2681static const MemoryRegionOps vtd_mem_ir_ops = {
2682    .read_with_attrs = vtd_mem_ir_read,
2683    .write_with_attrs = vtd_mem_ir_write,
2684    .endianness = DEVICE_LITTLE_ENDIAN,
2685    .impl = {
2686        .min_access_size = 4,
2687        .max_access_size = 4,
2688    },
2689    .valid = {
2690        .min_access_size = 4,
2691        .max_access_size = 4,
2692    },
2693};
2694
2695VTDAddressSpace *vtd_find_add_as(IntelIOMMUState *s, PCIBus *bus, int devfn)
2696{
2697    uintptr_t key = (uintptr_t)bus;
2698    VTDBus *vtd_bus = g_hash_table_lookup(s->vtd_as_by_busptr, &key);
2699    VTDAddressSpace *vtd_dev_as;
2700    char name[128];
2701
2702    if (!vtd_bus) {
2703        uintptr_t *new_key = g_malloc(sizeof(*new_key));
2704        *new_key = (uintptr_t)bus;
2705        /* No corresponding free() */
2706        vtd_bus = g_malloc0(sizeof(VTDBus) + sizeof(VTDAddressSpace *) * \
2707                            PCI_DEVFN_MAX);
2708        vtd_bus->bus = bus;
2709        g_hash_table_insert(s->vtd_as_by_busptr, new_key, vtd_bus);
2710    }
2711
2712    vtd_dev_as = vtd_bus->dev_as[devfn];
2713
2714    if (!vtd_dev_as) {
2715        snprintf(name, sizeof(name), "intel_iommu_devfn_%d", devfn);
2716        vtd_bus->dev_as[devfn] = vtd_dev_as = g_malloc0(sizeof(VTDAddressSpace));
2717
2718        vtd_dev_as->bus = bus;
2719        vtd_dev_as->devfn = (uint8_t)devfn;
2720        vtd_dev_as->iommu_state = s;
2721        vtd_dev_as->context_cache_entry.context_cache_gen = 0;
2722
2723        /*
2724         * Memory region relationships looks like (Address range shows
2725         * only lower 32 bits to make it short in length...):
2726         *
2727         * |-----------------+-------------------+----------|
2728         * | Name            | Address range     | Priority |
2729         * |-----------------+-------------------+----------+
2730         * | vtd_root        | 00000000-ffffffff |        0 |
2731         * |  intel_iommu    | 00000000-ffffffff |        1 |
2732         * |  vtd_sys_alias  | 00000000-ffffffff |        1 |
2733         * |  intel_iommu_ir | fee00000-feefffff |       64 |
2734         * |-----------------+-------------------+----------|
2735         *
2736         * We enable/disable DMAR by switching enablement for
2737         * vtd_sys_alias and intel_iommu regions. IR region is always
2738         * enabled.
2739         */
2740        memory_region_init_iommu(&vtd_dev_as->iommu, sizeof(vtd_dev_as->iommu),
2741                                 TYPE_INTEL_IOMMU_MEMORY_REGION, OBJECT(s),
2742                                 "intel_iommu_dmar",
2743                                 UINT64_MAX);
2744        memory_region_init_alias(&vtd_dev_as->sys_alias, OBJECT(s),
2745                                 "vtd_sys_alias", get_system_memory(),
2746                                 0, memory_region_size(get_system_memory()));
2747        memory_region_init_io(&vtd_dev_as->iommu_ir, OBJECT(s),
2748                              &vtd_mem_ir_ops, s, "intel_iommu_ir",
2749                              VTD_INTERRUPT_ADDR_SIZE);
2750        memory_region_init(&vtd_dev_as->root, OBJECT(s),
2751                           "vtd_root", UINT64_MAX);
2752        memory_region_add_subregion_overlap(&vtd_dev_as->root,
2753                                            VTD_INTERRUPT_ADDR_FIRST,
2754                                            &vtd_dev_as->iommu_ir, 64);
2755        address_space_init(&vtd_dev_as->as, &vtd_dev_as->root, name);
2756        memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
2757                                            &vtd_dev_as->sys_alias, 1);
2758        memory_region_add_subregion_overlap(&vtd_dev_as->root, 0,
2759                                            MEMORY_REGION(&vtd_dev_as->iommu),
2760                                            1);
2761        vtd_switch_address_space(vtd_dev_as);
2762    }
2763    return vtd_dev_as;
2764}
2765
2766/* Unmap the whole range in the notifier's scope. */
2767static void vtd_address_space_unmap(VTDAddressSpace *as, IOMMUNotifier *n)
2768{
2769    IOMMUTLBEntry entry;
2770    hwaddr size;
2771    hwaddr start = n->start;
2772    hwaddr end = n->end;
2773    IntelIOMMUState *s = as->iommu_state;
2774
2775    /*
2776     * Note: all the codes in this function has a assumption that IOVA
2777     * bits are no more than VTD_MGAW bits (which is restricted by
2778     * VT-d spec), otherwise we need to consider overflow of 64 bits.
2779     */
2780
2781    if (end > VTD_ADDRESS_SIZE(s->aw_bits)) {
2782        /*
2783         * Don't need to unmap regions that is bigger than the whole
2784         * VT-d supported address space size
2785         */
2786        end = VTD_ADDRESS_SIZE(s->aw_bits);
2787    }
2788
2789    assert(start <= end);
2790    size = end - start;
2791
2792    if (ctpop64(size) != 1) {
2793        /*
2794         * This size cannot format a correct mask. Let's enlarge it to
2795         * suite the minimum available mask.
2796         */
2797        int n = 64 - clz64(size);
2798        if (n > s->aw_bits) {
2799            /* should not happen, but in case it happens, limit it */
2800            n = s->aw_bits;
2801        }
2802        size = 1ULL << n;
2803    }
2804
2805    entry.target_as = &address_space_memory;
2806    /* Adjust iova for the size */
2807    entry.iova = n->start & ~(size - 1);
2808    /* This field is meaningless for unmap */
2809    entry.translated_addr = 0;
2810    entry.perm = IOMMU_NONE;
2811    entry.addr_mask = size - 1;
2812
2813    trace_vtd_as_unmap_whole(pci_bus_num(as->bus),
2814                             VTD_PCI_SLOT(as->devfn),
2815                             VTD_PCI_FUNC(as->devfn),
2816                             entry.iova, size);
2817
2818    memory_region_notify_one(n, &entry);
2819}
2820
2821static void vtd_address_space_unmap_all(IntelIOMMUState *s)
2822{
2823    IntelIOMMUNotifierNode *node;
2824    VTDAddressSpace *vtd_as;
2825    IOMMUNotifier *n;
2826
2827    QLIST_FOREACH(node, &s->notifiers_list, next) {
2828        vtd_as = node->vtd_as;
2829        IOMMU_NOTIFIER_FOREACH(n, &vtd_as->iommu) {
2830            vtd_address_space_unmap(vtd_as, n);
2831        }
2832    }
2833}
2834
2835static int vtd_replay_hook(IOMMUTLBEntry *entry, void *private)
2836{
2837    memory_region_notify_one((IOMMUNotifier *)private, entry);
2838    return 0;
2839}
2840
2841static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
2842{
2843    VTDAddressSpace *vtd_as = container_of(iommu_mr, VTDAddressSpace, iommu);
2844    IntelIOMMUState *s = vtd_as->iommu_state;
2845    uint8_t bus_n = pci_bus_num(vtd_as->bus);
2846    VTDContextEntry ce;
2847
2848    /*
2849     * The replay can be triggered by either a invalidation or a newly
2850     * created entry. No matter what, we release existing mappings
2851     * (it means flushing caches for UNMAP-only registers).
2852     */
2853    vtd_address_space_unmap(vtd_as, n);
2854
2855    if (vtd_dev_to_context_entry(s, bus_n, vtd_as->devfn, &ce) == 0) {
2856        trace_vtd_replay_ce_valid(bus_n, PCI_SLOT(vtd_as->devfn),
2857                                  PCI_FUNC(vtd_as->devfn),
2858                                  VTD_CONTEXT_ENTRY_DID(ce.hi),
2859                                  ce.hi, ce.lo);
2860        vtd_page_walk(&ce, 0, ~0ULL, vtd_replay_hook, (void *)n, false,
2861                      s->aw_bits);
2862    } else {
2863        trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
2864                                    PCI_FUNC(vtd_as->devfn));
2865    }
2866
2867    return;
2868}
2869
2870/* Do the initialization. It will also be called when reset, so pay
2871 * attention when adding new initialization stuff.
2872 */
2873static void vtd_init(IntelIOMMUState *s)
2874{
2875    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
2876
2877    memset(s->csr, 0, DMAR_REG_SIZE);
2878    memset(s->wmask, 0, DMAR_REG_SIZE);
2879    memset(s->w1cmask, 0, DMAR_REG_SIZE);
2880    memset(s->womask, 0, DMAR_REG_SIZE);
2881
2882    s->root = 0;
2883    s->root_extended = false;
2884    s->dmar_enabled = false;
2885    s->iq_head = 0;
2886    s->iq_tail = 0;
2887    s->iq = 0;
2888    s->iq_size = 0;
2889    s->qi_enabled = false;
2890    s->iq_last_desc_type = VTD_INV_DESC_NONE;
2891    s->next_frcd_reg = 0;
2892    s->cap = VTD_CAP_FRO | VTD_CAP_NFR | VTD_CAP_ND |
2893             VTD_CAP_MAMV | VTD_CAP_PSI | VTD_CAP_SLLPS |
2894             VTD_CAP_SAGAW_39bit | VTD_CAP_MGAW(s->aw_bits);
2895    if (s->aw_bits == VTD_HOST_AW_48BIT) {
2896        s->cap |= VTD_CAP_SAGAW_48bit;
2897    }
2898    s->ecap = VTD_ECAP_QI | VTD_ECAP_IRO;
2899
2900    /*
2901     * Rsvd field masks for spte
2902     */
2903    vtd_paging_entry_rsvd_field[0] = ~0ULL;
2904    vtd_paging_entry_rsvd_field[1] = VTD_SPTE_PAGE_L1_RSVD_MASK(s->aw_bits);
2905    vtd_paging_entry_rsvd_field[2] = VTD_SPTE_PAGE_L2_RSVD_MASK(s->aw_bits);
2906    vtd_paging_entry_rsvd_field[3] = VTD_SPTE_PAGE_L3_RSVD_MASK(s->aw_bits);
2907    vtd_paging_entry_rsvd_field[4] = VTD_SPTE_PAGE_L4_RSVD_MASK(s->aw_bits);
2908    vtd_paging_entry_rsvd_field[5] = VTD_SPTE_LPAGE_L1_RSVD_MASK(s->aw_bits);
2909    vtd_paging_entry_rsvd_field[6] = VTD_SPTE_LPAGE_L2_RSVD_MASK(s->aw_bits);
2910    vtd_paging_entry_rsvd_field[7] = VTD_SPTE_LPAGE_L3_RSVD_MASK(s->aw_bits);
2911    vtd_paging_entry_rsvd_field[8] = VTD_SPTE_LPAGE_L4_RSVD_MASK(s->aw_bits);
2912
2913    if (x86_iommu->intr_supported) {
2914        s->ecap |= VTD_ECAP_IR | VTD_ECAP_MHMV;
2915        if (s->intr_eim == ON_OFF_AUTO_ON) {
2916            s->ecap |= VTD_ECAP_EIM;
2917        }
2918        assert(s->intr_eim != ON_OFF_AUTO_AUTO);
2919    }
2920
2921    if (x86_iommu->dt_supported) {
2922        s->ecap |= VTD_ECAP_DT;
2923    }
2924
2925    if (x86_iommu->pt_supported) {
2926        s->ecap |= VTD_ECAP_PT;
2927    }
2928
2929    if (s->caching_mode) {
2930        s->cap |= VTD_CAP_CM;
2931    }
2932
2933    vtd_reset_context_cache(s);
2934    vtd_reset_iotlb(s);
2935
2936    /* Define registers with default values and bit semantics */
2937    vtd_define_long(s, DMAR_VER_REG, 0x10UL, 0, 0);
2938    vtd_define_quad(s, DMAR_CAP_REG, s->cap, 0, 0);
2939    vtd_define_quad(s, DMAR_ECAP_REG, s->ecap, 0, 0);
2940    vtd_define_long(s, DMAR_GCMD_REG, 0, 0xff800000UL, 0);
2941    vtd_define_long_wo(s, DMAR_GCMD_REG, 0xff800000UL);
2942    vtd_define_long(s, DMAR_GSTS_REG, 0, 0, 0);
2943    vtd_define_quad(s, DMAR_RTADDR_REG, 0, 0xfffffffffffff000ULL, 0);
2944    vtd_define_quad(s, DMAR_CCMD_REG, 0, 0xe0000003ffffffffULL, 0);
2945    vtd_define_quad_wo(s, DMAR_CCMD_REG, 0x3ffff0000ULL);
2946
2947    /* Advanced Fault Logging not supported */
2948    vtd_define_long(s, DMAR_FSTS_REG, 0, 0, 0x11UL);
2949    vtd_define_long(s, DMAR_FECTL_REG, 0x80000000UL, 0x80000000UL, 0);
2950    vtd_define_long(s, DMAR_FEDATA_REG, 0, 0x0000ffffUL, 0);
2951    vtd_define_long(s, DMAR_FEADDR_REG, 0, 0xfffffffcUL, 0);
2952
2953    /* Treated as RsvdZ when EIM in ECAP_REG is not supported
2954     * vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0xffffffffUL, 0);
2955     */
2956    vtd_define_long(s, DMAR_FEUADDR_REG, 0, 0, 0);
2957
2958    /* Treated as RO for implementations that PLMR and PHMR fields reported
2959     * as Clear in the CAP_REG.
2960     * vtd_define_long(s, DMAR_PMEN_REG, 0, 0x80000000UL, 0);
2961     */
2962    vtd_define_long(s, DMAR_PMEN_REG, 0, 0, 0);
2963
2964    vtd_define_quad(s, DMAR_IQH_REG, 0, 0, 0);
2965    vtd_define_quad(s, DMAR_IQT_REG, 0, 0x7fff0ULL, 0);
2966    vtd_define_quad(s, DMAR_IQA_REG, 0, 0xfffffffffffff007ULL, 0);
2967    vtd_define_long(s, DMAR_ICS_REG, 0, 0, 0x1UL);
2968    vtd_define_long(s, DMAR_IECTL_REG, 0x80000000UL, 0x80000000UL, 0);
2969    vtd_define_long(s, DMAR_IEDATA_REG, 0, 0xffffffffUL, 0);
2970    vtd_define_long(s, DMAR_IEADDR_REG, 0, 0xfffffffcUL, 0);
2971    /* Treadted as RsvdZ when EIM in ECAP_REG is not supported */
2972    vtd_define_long(s, DMAR_IEUADDR_REG, 0, 0, 0);
2973
2974    /* IOTLB registers */
2975    vtd_define_quad(s, DMAR_IOTLB_REG, 0, 0Xb003ffff00000000ULL, 0);
2976    vtd_define_quad(s, DMAR_IVA_REG, 0, 0xfffffffffffff07fULL, 0);
2977    vtd_define_quad_wo(s, DMAR_IVA_REG, 0xfffffffffffff07fULL);
2978
2979    /* Fault Recording Registers, 128-bit */
2980    vtd_define_quad(s, DMAR_FRCD_REG_0_0, 0, 0, 0);
2981    vtd_define_quad(s, DMAR_FRCD_REG_0_2, 0, 0, 0x8000000000000000ULL);
2982
2983    /*
2984     * Interrupt remapping registers.
2985     */
2986    vtd_define_quad(s, DMAR_IRTA_REG, 0, 0xfffffffffffff80fULL, 0);
2987}
2988
2989/* Should not reset address_spaces when reset because devices will still use
2990 * the address space they got at first (won't ask the bus again).
2991 */
2992static void vtd_reset(DeviceState *dev)
2993{
2994    IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
2995
2996    vtd_init(s);
2997
2998    /*
2999     * When device reset, throw away all mappings and external caches
3000     */
3001    vtd_address_space_unmap_all(s);
3002}
3003
3004static AddressSpace *vtd_host_dma_iommu(PCIBus *bus, void *opaque, int devfn)
3005{
3006    IntelIOMMUState *s = opaque;
3007    VTDAddressSpace *vtd_as;
3008
3009    assert(0 <= devfn && devfn < PCI_DEVFN_MAX);
3010
3011    vtd_as = vtd_find_add_as(s, bus, devfn);
3012    return &vtd_as->as;
3013}
3014
3015static bool vtd_decide_config(IntelIOMMUState *s, Error **errp)
3016{
3017    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
3018
3019    /* Currently Intel IOMMU IR only support "kernel-irqchip={off|split}" */
3020    if (x86_iommu->intr_supported && kvm_irqchip_in_kernel() &&
3021        !kvm_irqchip_is_split()) {
3022        error_setg(errp, "Intel Interrupt Remapping cannot work with "
3023                         "kernel-irqchip=on, please use 'split|off'.");
3024        return false;
3025    }
3026    if (s->intr_eim == ON_OFF_AUTO_ON && !x86_iommu->intr_supported) {
3027        error_setg(errp, "eim=on cannot be selected without intremap=on");
3028        return false;
3029    }
3030
3031    if (s->intr_eim == ON_OFF_AUTO_AUTO) {
3032        s->intr_eim = (kvm_irqchip_in_kernel() || s->buggy_eim)
3033                      && x86_iommu->intr_supported ?
3034                                              ON_OFF_AUTO_ON : ON_OFF_AUTO_OFF;
3035    }
3036    if (s->intr_eim == ON_OFF_AUTO_ON && !s->buggy_eim) {
3037        if (!kvm_irqchip_in_kernel()) {
3038            error_setg(errp, "eim=on requires accel=kvm,kernel-irqchip=split");
3039            return false;
3040        }
3041        if (!kvm_enable_x2apic()) {
3042            error_setg(errp, "eim=on requires support on the KVM side"
3043                             "(X2APIC_API, first shipped in v4.7)");
3044            return false;
3045        }
3046    }
3047
3048    /* Currently only address widths supported are 39 and 48 bits */
3049    if ((s->aw_bits != VTD_HOST_AW_39BIT) &&
3050        (s->aw_bits != VTD_HOST_AW_48BIT)) {
3051        error_setg(errp, "Supported values for x-aw-bits are: %d, %d",
3052                   VTD_HOST_AW_39BIT, VTD_HOST_AW_48BIT);
3053        return false;
3054    }
3055
3056    return true;
3057}
3058
3059static void vtd_realize(DeviceState *dev, Error **errp)
3060{
3061    MachineState *ms = MACHINE(qdev_get_machine());
3062    PCMachineState *pcms = PC_MACHINE(ms);
3063    PCIBus *bus = pcms->bus;
3064    IntelIOMMUState *s = INTEL_IOMMU_DEVICE(dev);
3065    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(dev);
3066
3067    x86_iommu->type = TYPE_INTEL;
3068
3069    if (!vtd_decide_config(s, errp)) {
3070        return;
3071    }
3072
3073    QLIST_INIT(&s->notifiers_list);
3074    memset(s->vtd_as_by_bus_num, 0, sizeof(s->vtd_as_by_bus_num));
3075    memory_region_init_io(&s->csrmem, OBJECT(s), &vtd_mem_ops, s,
3076                          "intel_iommu", DMAR_REG_SIZE);
3077    sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->csrmem);
3078    /* No corresponding destroy */
3079    s->iotlb = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
3080                                     g_free, g_free);
3081    s->vtd_as_by_busptr = g_hash_table_new_full(vtd_uint64_hash, vtd_uint64_equal,
3082                                              g_free, g_free);
3083    vtd_init(s);
3084    sysbus_mmio_map(SYS_BUS_DEVICE(s), 0, Q35_HOST_BRIDGE_IOMMU_ADDR);
3085    pci_setup_iommu(bus, vtd_host_dma_iommu, dev);
3086    /* Pseudo address space under root PCI bus. */
3087    pcms->ioapic_as = vtd_host_dma_iommu(bus, s, Q35_PSEUDO_DEVFN_IOAPIC);
3088}
3089
3090static void vtd_class_init(ObjectClass *klass, void *data)
3091{
3092    DeviceClass *dc = DEVICE_CLASS(klass);
3093    X86IOMMUClass *x86_class = X86_IOMMU_CLASS(klass);
3094
3095    dc->reset = vtd_reset;
3096    dc->vmsd = &vtd_vmstate;
3097    dc->props = vtd_properties;
3098    dc->hotpluggable = false;
3099    x86_class->realize = vtd_realize;
3100    x86_class->int_remap = vtd_int_remap;
3101    /* Supported by the pc-q35-* machine types */
3102    dc->user_creatable = true;
3103}
3104
3105static const TypeInfo vtd_info = {
3106    .name          = TYPE_INTEL_IOMMU_DEVICE,
3107    .parent        = TYPE_X86_IOMMU_DEVICE,
3108    .instance_size = sizeof(IntelIOMMUState),
3109    .class_init    = vtd_class_init,
3110};
3111
3112static void vtd_iommu_memory_region_class_init(ObjectClass *klass,
3113                                                     void *data)
3114{
3115    IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_CLASS(klass);
3116
3117    imrc->translate = vtd_iommu_translate;
3118    imrc->notify_flag_changed = vtd_iommu_notify_flag_changed;
3119    imrc->replay = vtd_iommu_replay;
3120}
3121
3122static const TypeInfo vtd_iommu_memory_region_info = {
3123    .parent = TYPE_IOMMU_MEMORY_REGION,
3124    .name = TYPE_INTEL_IOMMU_MEMORY_REGION,
3125    .class_init = vtd_iommu_memory_region_class_init,
3126};
3127
3128static void vtd_register_types(void)
3129{
3130    type_register_static(&vtd_info);
3131    type_register_static(&vtd_iommu_memory_region_info);
3132}
3133
3134type_init(vtd_register_types)
3135