intel_dmar.h revision 257512
1166124Srafan/*- 2166124Srafan * Copyright (c) 2013 The FreeBSD Foundation 3262685Sdelphij * All rights reserved. 4166124Srafan * 5166124Srafan * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 6166124Srafan * under sponsorship from the FreeBSD Foundation. 7166124Srafan * 8166124Srafan * Redistribution and use in source and binary forms, with or without 9166124Srafan * modification, are permitted provided that the following conditions 10166124Srafan * are met: 11166124Srafan * 1. Redistributions of source code must retain the above copyright 12166124Srafan * notice, this list of conditions and the following disclaimer. 13166124Srafan * 2. Redistributions in binary form must reproduce the above copyright 14166124Srafan * notice, this list of conditions and the following disclaimer in the 15166124Srafan * documentation and/or other materials provided with the distribution. 16166124Srafan * 17166124Srafan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18166124Srafan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19166124Srafan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20166124Srafan * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21166124Srafan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22166124Srafan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23166124Srafan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24166124Srafan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25166124Srafan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26166124Srafan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27166124Srafan * SUCH DAMAGE. 28166124Srafan * 29166124Srafan * $FreeBSD: head/sys/x86/iommu/intel_dmar.h 257512 2013-11-01 17:38:52Z kib $ 30166124Srafan */ 31166124Srafan 32262685Sdelphij#ifndef __X86_IOMMU_INTEL_DMAR_H 33166124Srafan#define __X86_IOMMU_INTEL_DMAR_H 34166124Srafan 35166124Srafan/* Host or physical memory address, after translation. */ 36166124Srafantypedef uint64_t dmar_haddr_t; 37166124Srafan/* Guest or bus address, before translation. */ 38166124Srafantypedef uint64_t dmar_gaddr_t; 39166124Srafan 40262685Sdelphijstruct dmar_qi_genseq { 41166124Srafan u_int gen; 42262685Sdelphij uint32_t seq; 43262685Sdelphij}; 44262685Sdelphij 45262685Sdelphijstruct dmar_map_entry { 46262685Sdelphij dmar_gaddr_t start; 47166124Srafan dmar_gaddr_t end; 48166124Srafan dmar_gaddr_t free_after; /* Free space after the entry */ 49262685Sdelphij dmar_gaddr_t free_down; /* Max free space below the 50166124Srafan current R/B tree node */ 51166124Srafan u_int flags; 52166124Srafan TAILQ_ENTRY(dmar_map_entry) dmamap_link; /* Link for dmamap entries */ 53166124Srafan RB_ENTRY(dmar_map_entry) rb_entry; /* Links for ctx entries */ 54166124Srafan TAILQ_ENTRY(dmar_map_entry) unroll_link; /* Link for unroll after 55166124Srafan dmamap_load failure */ 56262685Sdelphij struct dmar_ctx *ctx; 57262685Sdelphij struct dmar_qi_genseq gseq; 58166124Srafan}; 59166124Srafan 60166124SrafanRB_HEAD(dmar_gas_entries_tree, dmar_map_entry); 61166124SrafanRB_PROTOTYPE(dmar_gas_entries_tree, dmar_map_entry, rb_entry, 62166124Srafan dmar_gas_cmp_entries); 63166124Srafan 64262685Sdelphij#define DMAR_MAP_ENTRY_PLACE 0x0001 /* Fake entry */ 65262685Sdelphij#define DMAR_MAP_ENTRY_RMRR 0x0002 /* Permanent, not linked by 66262685Sdelphij dmamap_link */ 67262685Sdelphij#define DMAR_MAP_ENTRY_MAP 0x0004 /* Busdma created, linked by 68262685Sdelphij dmamap_link */ 69262685Sdelphij#define DMAR_MAP_ENTRY_UNMAPPED 0x0010 /* No backing pages */ 70262685Sdelphij#define DMAR_MAP_ENTRY_QI_NF 0x0020 /* qi task, do not free entry */ 71262685Sdelphij#define DMAR_MAP_ENTRY_READ 0x1000 /* Read permitted */ 72262685Sdelphij#define DMAR_MAP_ENTRY_WRITE 0x2000 /* Write permitted */ 73262685Sdelphij#define DMAR_MAP_ENTRY_SNOOP 0x4000 /* Snoop */ 74262685Sdelphij#define DMAR_MAP_ENTRY_TM 0x8000 /* Transient */ 75262685Sdelphij 76262685Sdelphijstruct dmar_ctx { 77262685Sdelphij int bus; /* pci bus/slot/func */ 78262685Sdelphij int slot; 79262685Sdelphij int func; 80262685Sdelphij int domain; /* DID */ 81262685Sdelphij int mgaw; /* Real max address width */ 82262685Sdelphij int agaw; /* Adjusted guest address width */ 83262685Sdelphij int pglvl; /* The pagelevel */ 84262685Sdelphij int awlvl; /* The pagelevel as the bitmask, to set in 85262685Sdelphij context entry */ 86262685Sdelphij dmar_gaddr_t end;/* Highest address + 1 in the guest AS */ 87262685Sdelphij u_int refs; /* References to the context, from tags */ 88262685Sdelphij struct dmar_unit *dmar; 89262685Sdelphij struct bus_dma_tag_dmar ctx_tag; /* Root tag */ 90262685Sdelphij struct mtx lock; 91262685Sdelphij LIST_ENTRY(dmar_ctx) link; /* Member in the dmar list */ 92262685Sdelphij vm_object_t pgtbl_obj; /* Page table pages */ 93262685Sdelphij u_int flags; /* Protected by dmar lock */ 94262685Sdelphij uint64_t last_fault_rec[2]; /* Last fault reported */ 95262685Sdelphij u_int entries_cnt; 96262685Sdelphij u_long loads; 97262685Sdelphij u_long unloads; 98262685Sdelphij struct dmar_gas_entries_tree rb_root; 99262685Sdelphij struct dmar_map_entries_tailq unload_entries; /* Entries to unload */ 100262685Sdelphij struct dmar_map_entry *first_place, *last_place; 101262685Sdelphij struct task unload_task; 102262685Sdelphij}; 103262685Sdelphij 104262685Sdelphij/* struct dmar_ctx flags */ 105262685Sdelphij#define DMAR_CTX_FAULTED 0x0001 /* Fault was reported, 106262685Sdelphij last_fault_rec is valid */ 107262685Sdelphij#define DMAR_CTX_IDMAP 0x0002 /* Context uses identity page table */ 108262685Sdelphij#define DMAR_CTX_RMRR 0x0004 /* Context contains RMRR entry, 109262685Sdelphij cannot be turned off */ 110262685Sdelphij#define DMAR_CTX_DISABLED 0x0008 /* Device is disabled, the 111262685Sdelphij ephemeral reference is kept 112262685Sdelphij to prevent context destruction */ 113262685Sdelphij 114262685Sdelphij#define DMAR_CTX_PGLOCK(ctx) VM_OBJECT_WLOCK((ctx)->pgtbl_obj) 115262685Sdelphij#define DMAR_CTX_PGTRYLOCK(ctx) VM_OBJECT_TRYWLOCK((ctx)->pgtbl_obj) 116262685Sdelphij#define DMAR_CTX_PGUNLOCK(ctx) VM_OBJECT_WUNLOCK((ctx)->pgtbl_obj) 117262685Sdelphij#define DMAR_CTX_ASSERT_PGLOCKED(ctx) \ 118262685Sdelphij VM_OBJECT_ASSERT_WLOCKED((ctx)->pgtbl_obj) 119 120#define DMAR_CTX_LOCK(ctx) mtx_lock(&(ctx)->lock) 121#define DMAR_CTX_UNLOCK(ctx) mtx_unlock(&(ctx)->lock) 122#define DMAR_CTX_ASSERT_LOCKED(ctx) mtx_assert(&(ctx)->lock, MA_OWNED) 123 124struct dmar_msi_data { 125 int irq; 126 int irq_rid; 127 struct resource *irq_res; 128 void *intr_handle; 129 int (*handler)(void *); 130 int msi_data_reg; 131 int msi_addr_reg; 132 int msi_uaddr_reg; 133 void (*enable_intr)(struct dmar_unit *); 134 void (*disable_intr)(struct dmar_unit *); 135 const char *name; 136}; 137 138#define DMAR_INTR_FAULT 0 139#define DMAR_INTR_QI 1 140#define DMAR_INTR_TOTAL 2 141 142struct dmar_unit { 143 device_t dev; 144 int unit; 145 uint16_t segment; 146 uint64_t base; 147 148 /* Resources */ 149 int reg_rid; 150 struct resource *regs; 151 152 struct dmar_msi_data intrs[DMAR_INTR_TOTAL]; 153 154 /* Hardware registers cache */ 155 uint32_t hw_ver; 156 uint64_t hw_cap; 157 uint64_t hw_ecap; 158 uint32_t hw_gcmd; 159 160 /* Data for being a dmar */ 161 struct mtx lock; 162 LIST_HEAD(, dmar_ctx) contexts; 163 struct unrhdr *domids; 164 vm_object_t ctx_obj; 165 u_int barrier_flags; 166 167 /* Fault handler data */ 168 struct mtx fault_lock; 169 uint64_t *fault_log; 170 int fault_log_head; 171 int fault_log_tail; 172 int fault_log_size; 173 struct task fault_task; 174 struct taskqueue *fault_taskqueue; 175 176 /* QI */ 177 int qi_enabled; 178 vm_offset_t inv_queue; 179 vm_size_t inv_queue_size; 180 uint32_t inv_queue_avail; 181 uint32_t inv_queue_tail; 182 volatile uint32_t inv_waitd_seq_hw; /* hw writes there on wait 183 descr completion */ 184 uint64_t inv_waitd_seq_hw_phys; 185 uint32_t inv_waitd_seq; /* next sequence number to use for wait descr */ 186 u_int inv_waitd_gen; /* seq number generation AKA seq overflows */ 187 u_int inv_seq_waiters; /* count of waiters for seq */ 188 u_int inv_queue_full; /* informational counter */ 189 190 /* Delayed freeing of map entries queue processing */ 191 struct dmar_map_entries_tailq tlb_flush_entries; 192 struct task qi_task; 193 struct taskqueue *qi_taskqueue; 194 195 /* Busdma delayed map load */ 196 struct task dmamap_load_task; 197 TAILQ_HEAD(, bus_dmamap_dmar) delayed_maps; 198 struct taskqueue *delayed_taskqueue; 199}; 200 201#define DMAR_LOCK(dmar) mtx_lock(&(dmar)->lock) 202#define DMAR_UNLOCK(dmar) mtx_unlock(&(dmar)->lock) 203#define DMAR_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->lock, MA_OWNED) 204 205#define DMAR_FAULT_LOCK(dmar) mtx_lock_spin(&(dmar)->fault_lock) 206#define DMAR_FAULT_UNLOCK(dmar) mtx_unlock_spin(&(dmar)->fault_lock) 207#define DMAR_FAULT_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->fault_lock, MA_OWNED) 208 209#define DMAR_IS_COHERENT(dmar) (((dmar)->hw_ecap & DMAR_ECAP_C) != 0) 210#define DMAR_HAS_QI(dmar) (((dmar)->hw_ecap & DMAR_ECAP_QI) != 0) 211 212/* Barrier ids */ 213#define DMAR_BARRIER_RMRR 0 214#define DMAR_BARRIER_USEQ 1 215 216struct dmar_unit *dmar_find(device_t dev); 217 218u_int dmar_nd2mask(u_int nd); 219bool dmar_pglvl_supported(struct dmar_unit *unit, int pglvl); 220int ctx_set_agaw(struct dmar_ctx *ctx, int mgaw); 221int dmar_maxaddr2mgaw(struct dmar_unit* unit, dmar_gaddr_t maxaddr, 222 bool allow_less); 223vm_pindex_t pglvl_max_pages(int pglvl); 224int ctx_is_sp_lvl(struct dmar_ctx *ctx, int lvl); 225dmar_gaddr_t pglvl_page_size(int total_pglvl, int lvl); 226dmar_gaddr_t ctx_page_size(struct dmar_ctx *ctx, int lvl); 227int calc_am(struct dmar_unit *unit, dmar_gaddr_t base, dmar_gaddr_t size, 228 dmar_gaddr_t *isizep); 229struct vm_page *dmar_pgalloc(vm_object_t obj, vm_pindex_t idx, int flags); 230void dmar_pgfree(vm_object_t obj, vm_pindex_t idx, int flags); 231void *dmar_map_pgtbl(vm_object_t obj, vm_pindex_t idx, int flags, 232 struct sf_buf **sf); 233void dmar_unmap_pgtbl(struct sf_buf *sf, bool coherent); 234int dmar_load_root_entry_ptr(struct dmar_unit *unit); 235int dmar_inv_ctx_glob(struct dmar_unit *unit); 236int dmar_inv_iotlb_glob(struct dmar_unit *unit); 237int dmar_flush_write_bufs(struct dmar_unit *unit); 238int dmar_enable_translation(struct dmar_unit *unit); 239int dmar_disable_translation(struct dmar_unit *unit); 240bool dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id); 241void dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id); 242 243int dmar_fault_intr(void *arg); 244void dmar_enable_fault_intr(struct dmar_unit *unit); 245void dmar_disable_fault_intr(struct dmar_unit *unit); 246int dmar_init_fault_log(struct dmar_unit *unit); 247void dmar_fini_fault_log(struct dmar_unit *unit); 248 249int dmar_qi_intr(void *arg); 250void dmar_enable_qi_intr(struct dmar_unit *unit); 251void dmar_disable_qi_intr(struct dmar_unit *unit); 252int dmar_init_qi(struct dmar_unit *unit); 253void dmar_fini_qi(struct dmar_unit *unit); 254void dmar_qi_invalidate_locked(struct dmar_ctx *ctx, dmar_gaddr_t start, 255 dmar_gaddr_t size, struct dmar_qi_genseq *pseq); 256void dmar_qi_invalidate_ctx_glob_locked(struct dmar_unit *unit); 257void dmar_qi_invalidate_iotlb_glob_locked(struct dmar_unit *unit); 258 259vm_object_t ctx_get_idmap_pgtbl(struct dmar_ctx *ctx, dmar_gaddr_t maxaddr); 260void put_idmap_pgtbl(vm_object_t obj); 261int ctx_map_buf(struct dmar_ctx *ctx, dmar_gaddr_t base, dmar_gaddr_t size, 262 vm_page_t *ma, uint64_t pflags, int flags); 263int ctx_unmap_buf(struct dmar_ctx *ctx, dmar_gaddr_t base, dmar_gaddr_t size, 264 int flags); 265void ctx_flush_iotlb_sync(struct dmar_ctx *ctx, dmar_gaddr_t base, 266 dmar_gaddr_t size); 267int ctx_alloc_pgtbl(struct dmar_ctx *ctx); 268void ctx_free_pgtbl(struct dmar_ctx *ctx); 269 270struct dmar_ctx *dmar_instantiate_ctx(struct dmar_unit *dmar, device_t dev, 271 bool rmrr); 272struct dmar_ctx *dmar_get_ctx(struct dmar_unit *dmar, device_t dev, 273 bool id_mapped, bool rmrr_init); 274void dmar_free_ctx_locked(struct dmar_unit *dmar, struct dmar_ctx *ctx); 275void dmar_free_ctx(struct dmar_ctx *ctx); 276struct dmar_ctx *dmar_find_ctx_locked(struct dmar_unit *dmar, int bus, 277 int slot, int func); 278void dmar_ctx_unload_entry(struct dmar_map_entry *entry, bool free); 279void dmar_ctx_unload(struct dmar_ctx *ctx, 280 struct dmar_map_entries_tailq *entries, bool cansleep); 281void dmar_ctx_free_entry(struct dmar_map_entry *entry, bool free); 282 283int dmar_init_busdma(struct dmar_unit *unit); 284void dmar_fini_busdma(struct dmar_unit *unit); 285 286void dmar_gas_init_ctx(struct dmar_ctx *ctx); 287void dmar_gas_fini_ctx(struct dmar_ctx *ctx); 288struct dmar_map_entry *dmar_gas_alloc_entry(struct dmar_ctx *ctx, u_int flags); 289void dmar_gas_free_entry(struct dmar_ctx *ctx, struct dmar_map_entry *entry); 290void dmar_gas_free_space(struct dmar_ctx *ctx, struct dmar_map_entry *entry); 291int dmar_gas_map(struct dmar_ctx *ctx, const struct bus_dma_tag_common *common, 292 dmar_gaddr_t size, u_int eflags, u_int flags, vm_page_t *ma, 293 struct dmar_map_entry **res); 294void dmar_gas_free_region(struct dmar_ctx *ctx, struct dmar_map_entry *entry); 295int dmar_gas_map_region(struct dmar_ctx *ctx, struct dmar_map_entry *entry, 296 u_int eflags, u_int flags, vm_page_t *ma); 297int dmar_gas_reserve_region(struct dmar_ctx *ctx, dmar_gaddr_t start, 298 dmar_gaddr_t end); 299 300void dmar_ctx_parse_rmrr(struct dmar_ctx *ctx, device_t dev, 301 struct dmar_map_entries_tailq *rmrr_entries); 302int dmar_instantiate_rmrr_ctxs(struct dmar_unit *dmar); 303 304void dmar_quirks_post_ident(struct dmar_unit *dmar); 305void dmar_quirks_pre_use(struct dmar_unit *dmar); 306 307#define DMAR_GM_CANWAIT 0x0001 308#define DMAR_GM_CANSPLIT 0x0002 309 310#define DMAR_PGF_WAITOK 0x0001 311#define DMAR_PGF_ZERO 0x0002 312#define DMAR_PGF_ALLOC 0x0004 313#define DMAR_PGF_NOALLOC 0x0008 314#define DMAR_PGF_OBJL 0x0010 315 316extern dmar_haddr_t dmar_high; 317extern int haw; 318extern int dmar_tbl_pagecnt; 319extern int dmar_match_verbose; 320extern int dmar_check_free; 321 322static inline uint32_t 323dmar_read4(const struct dmar_unit *unit, int reg) 324{ 325 326 return (bus_read_4(unit->regs, reg)); 327} 328 329static inline uint64_t 330dmar_read8(const struct dmar_unit *unit, int reg) 331{ 332#ifdef __i386__ 333 uint32_t high, low; 334 335 low = bus_read_4(unit->regs, reg); 336 high = bus_read_4(unit->regs, reg + 4); 337 return (low | ((uint64_t)high << 32)); 338#else 339 return (bus_read_8(unit->regs, reg)); 340#endif 341} 342 343static inline void 344dmar_write4(const struct dmar_unit *unit, int reg, uint32_t val) 345{ 346 347 KASSERT(reg != DMAR_GCMD_REG || (val & DMAR_GCMD_TE) == 348 (unit->hw_gcmd & DMAR_GCMD_TE), 349 ("dmar%d clearing TE 0x%08x 0x%08x", unit->unit, 350 unit->hw_gcmd, val)); 351 bus_write_4(unit->regs, reg, val); 352} 353 354static inline void 355dmar_write8(const struct dmar_unit *unit, int reg, uint64_t val) 356{ 357 358 KASSERT(reg != DMAR_GCMD_REG, ("8byte GCMD write")); 359#ifdef __i386__ 360 uint32_t high, low; 361 362 low = val; 363 high = val >> 32; 364 bus_write_4(unit->regs, reg, low); 365 bus_write_4(unit->regs, reg + 4, high); 366#else 367 bus_write_8(unit->regs, reg, val); 368#endif 369} 370 371/* 372 * dmar_pte_store and dmar_pte_clear ensure that on i386, 32bit writes 373 * are issued in the correct order. For store, the lower word, 374 * containing the P or R and W bits, is set only after the high word 375 * is written. For clear, the P bit is cleared first, then the high 376 * word is cleared. 377 */ 378static inline void 379dmar_pte_store(volatile uint64_t *dst, uint64_t val) 380{ 381 382 KASSERT(*dst == 0, ("used pte %p oldval %jx newval %jx", 383 dst, (uintmax_t)*dst, (uintmax_t)val)); 384#ifdef __i386__ 385 volatile uint32_t *p; 386 uint32_t hi, lo; 387 388 hi = val >> 32; 389 lo = val; 390 p = (volatile uint32_t *)dst; 391 *(p + 1) = hi; 392 *p = lo; 393#else 394 *dst = val; 395#endif 396} 397 398static inline void 399dmar_pte_clear(volatile uint64_t *dst) 400{ 401#ifdef __i386__ 402 volatile uint32_t *p; 403 404 p = (volatile uint32_t *)dst; 405 *p = 0; 406 *(p + 1) = 0; 407#else 408 *dst = 0; 409#endif 410} 411 412static inline bool 413dmar_test_boundary(dmar_gaddr_t start, dmar_gaddr_t size, 414 dmar_gaddr_t boundary) 415{ 416 417 if (boundary == 0) 418 return (true); 419 return (start + size <= ((start + boundary) & ~(boundary - 1))); 420} 421 422#ifdef INVARIANTS 423#define TD_PREP_PINNED_ASSERT \ 424 int old_td_pinned; \ 425 old_td_pinned = curthread->td_pinned 426#define TD_PINNED_ASSERT \ 427 KASSERT(curthread->td_pinned == old_td_pinned, \ 428 ("pin count leak: %d %d %s:%d", curthread->td_pinned, \ 429 old_td_pinned, __FILE__, __LINE__)) 430#else 431#define TD_PREP_PINNED_ASSERT 432#define TD_PINNED_ASSERT 433#endif 434 435#endif 436