186be9f0dSKonstantin Belousov /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3ebf5747bSPedro F. Giffuni *
40a110d5bSKonstantin Belousov * Copyright (c) 2013-2015 The FreeBSD Foundation
586be9f0dSKonstantin Belousov *
686be9f0dSKonstantin Belousov * This software was developed by Konstantin Belousov <kib@FreeBSD.org>
786be9f0dSKonstantin Belousov * under sponsorship from the FreeBSD Foundation.
886be9f0dSKonstantin Belousov *
986be9f0dSKonstantin Belousov * Redistribution and use in source and binary forms, with or without
1086be9f0dSKonstantin Belousov * modification, are permitted provided that the following conditions
1186be9f0dSKonstantin Belousov * are met:
1286be9f0dSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright
1386be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer.
1486be9f0dSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright
1586be9f0dSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the
1686be9f0dSKonstantin Belousov * documentation and/or other materials provided with the distribution.
1786be9f0dSKonstantin Belousov *
1886be9f0dSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1986be9f0dSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2086be9f0dSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2186be9f0dSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2286be9f0dSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2386be9f0dSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2486be9f0dSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2586be9f0dSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2686be9f0dSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2786be9f0dSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2886be9f0dSKonstantin Belousov * SUCH DAMAGE.
2986be9f0dSKonstantin Belousov */
3086be9f0dSKonstantin Belousov
3186be9f0dSKonstantin Belousov #ifndef __X86_IOMMU_INTEL_DMAR_H
3286be9f0dSKonstantin Belousov #define __X86_IOMMU_INTEL_DMAR_H
3386be9f0dSKonstantin Belousov
341238a28dSRuslan Bukin #include <dev/iommu/iommu.h>
3586be9f0dSKonstantin Belousov
3659e37c8aSRuslan Bukin struct dmar_unit;
3768eeb96aSKonstantin Belousov
381abfd355SKonstantin Belousov /*
391abfd355SKonstantin Belousov * Locking annotations:
4059e37c8aSRuslan Bukin * (u) - Protected by iommu unit lock
411abfd355SKonstantin Belousov * (d) - Protected by domain lock
421abfd355SKonstantin Belousov * (c) - Immutable after initialization
431abfd355SKonstantin Belousov */
441abfd355SKonstantin Belousov
451abfd355SKonstantin Belousov /*
461abfd355SKonstantin Belousov * The domain abstraction. Most non-constant members of the domain
47ad969cb1SKonstantin Belousov * are protected by owning dmar unit lock, not by the domain lock.
48ad969cb1SKonstantin Belousov * Most important, the dmar lock protects the contexts list.
491abfd355SKonstantin Belousov *
501abfd355SKonstantin Belousov * The domain lock protects the address map for the domain, and list
511abfd355SKonstantin Belousov * of unload entries delayed.
521abfd355SKonstantin Belousov *
531abfd355SKonstantin Belousov * Page tables pages and pages content is protected by the vm object
541abfd355SKonstantin Belousov * lock pgtbl_obj, which contains the page tables pages.
551abfd355SKonstantin Belousov */
561abfd355SKonstantin Belousov struct dmar_domain {
5759e37c8aSRuslan Bukin struct iommu_domain iodom;
581abfd355SKonstantin Belousov int domain; /* (c) DID, written in context entry */
591abfd355SKonstantin Belousov int mgaw; /* (c) Real max address width */
601abfd355SKonstantin Belousov int agaw; /* (c) Adjusted guest address width */
611abfd355SKonstantin Belousov int pglvl; /* (c) The pagelevel */
621abfd355SKonstantin Belousov int awlvl; /* (c) The pagelevel as the bitmask,
631abfd355SKonstantin Belousov to set in context entry */
641abfd355SKonstantin Belousov u_int ctx_cnt; /* (u) Number of contexts owned */
651abfd355SKonstantin Belousov u_int refs; /* (u) Refs, including ctx */
661abfd355SKonstantin Belousov struct dmar_unit *dmar; /* (c) */
671abfd355SKonstantin Belousov LIST_ENTRY(dmar_domain) link; /* (u) Member in the dmar list */
681abfd355SKonstantin Belousov vm_object_t pgtbl_obj; /* (c) Page table pages */
69e164cafcSKonstantin Belousov u_int batch_no;
7086be9f0dSKonstantin Belousov };
7186be9f0dSKonstantin Belousov
721abfd355SKonstantin Belousov struct dmar_ctx {
7359e37c8aSRuslan Bukin struct iommu_ctx context;
741abfd355SKonstantin Belousov uint64_t last_fault_rec[2]; /* Last fault reported */
751abfd355SKonstantin Belousov };
761abfd355SKonstantin Belousov
771abfd355SKonstantin Belousov #define DMAR_DOMAIN_PGLOCK(dom) VM_OBJECT_WLOCK((dom)->pgtbl_obj)
781abfd355SKonstantin Belousov #define DMAR_DOMAIN_PGTRYLOCK(dom) VM_OBJECT_TRYWLOCK((dom)->pgtbl_obj)
791abfd355SKonstantin Belousov #define DMAR_DOMAIN_PGUNLOCK(dom) VM_OBJECT_WUNLOCK((dom)->pgtbl_obj)
801abfd355SKonstantin Belousov #define DMAR_DOMAIN_ASSERT_PGLOCKED(dom) \
811abfd355SKonstantin Belousov VM_OBJECT_ASSERT_WLOCKED((dom)->pgtbl_obj)
8286be9f0dSKonstantin Belousov
8359e37c8aSRuslan Bukin #define DMAR_DOMAIN_LOCK(dom) mtx_lock(&(dom)->iodom.lock)
8459e37c8aSRuslan Bukin #define DMAR_DOMAIN_UNLOCK(dom) mtx_unlock(&(dom)->iodom.lock)
8559e37c8aSRuslan Bukin #define DMAR_DOMAIN_ASSERT_LOCKED(dom) mtx_assert(&(dom)->iodom.lock, MA_OWNED)
8686be9f0dSKonstantin Belousov
87c47229aeSKonstantin Belousov #define DMAR2IOMMU(dmar) (&((dmar)->iommu))
8878b51754SRuslan Bukin #define IOMMU2DMAR(dmar) \
8978b51754SRuslan Bukin __containerof((dmar), struct dmar_unit, iommu)
9078b51754SRuslan Bukin
91c47229aeSKonstantin Belousov #define DOM2IODOM(domain) (&((domain)->iodom))
9278b51754SRuslan Bukin #define IODOM2DOM(domain) \
9378b51754SRuslan Bukin __containerof((domain), struct dmar_domain, iodom)
9478b51754SRuslan Bukin
95c47229aeSKonstantin Belousov #define CTX2IOCTX(ctx) (&((ctx)->context))
9678b51754SRuslan Bukin #define IOCTX2CTX(ctx) \
9778b51754SRuslan Bukin __containerof((ctx), struct dmar_ctx, context)
9878b51754SRuslan Bukin
9978b51754SRuslan Bukin #define CTX2DOM(ctx) IODOM2DOM((ctx)->context.domain)
10078b51754SRuslan Bukin #define CTX2DMAR(ctx) (CTX2DOM(ctx)->dmar)
10178b51754SRuslan Bukin #define DOM2DMAR(domain) ((domain)->dmar)
10278b51754SRuslan Bukin
10368eeb96aSKonstantin Belousov #define DMAR_INTR_FAULT 0
10468eeb96aSKonstantin Belousov #define DMAR_INTR_QI 1
10568eeb96aSKonstantin Belousov #define DMAR_INTR_TOTAL 2
10668eeb96aSKonstantin Belousov
10786be9f0dSKonstantin Belousov struct dmar_unit {
10859e37c8aSRuslan Bukin struct iommu_unit iommu;
109ad794e6dSKonstantin Belousov struct x86_unit_common x86c;
11086be9f0dSKonstantin Belousov uint16_t segment;
11186be9f0dSKonstantin Belousov uint64_t base;
112705090cbSKonstantin Belousov int memdomain;
11386be9f0dSKonstantin Belousov
11486be9f0dSKonstantin Belousov /* Resources */
11586be9f0dSKonstantin Belousov int reg_rid;
11686be9f0dSKonstantin Belousov struct resource *regs;
11768eeb96aSKonstantin Belousov
11886be9f0dSKonstantin Belousov /* Hardware registers cache */
11986be9f0dSKonstantin Belousov uint32_t hw_ver;
12086be9f0dSKonstantin Belousov uint64_t hw_cap;
12186be9f0dSKonstantin Belousov uint64_t hw_ecap;
12286be9f0dSKonstantin Belousov uint32_t hw_gcmd;
12386be9f0dSKonstantin Belousov
12486be9f0dSKonstantin Belousov /* Data for being a dmar */
1251abfd355SKonstantin Belousov LIST_HEAD(, dmar_domain) domains;
12686be9f0dSKonstantin Belousov struct unrhdr *domids;
12786be9f0dSKonstantin Belousov vm_object_t ctx_obj;
12886be9f0dSKonstantin Belousov u_int barrier_flags;
12986be9f0dSKonstantin Belousov
13086be9f0dSKonstantin Belousov /* Fault handler data */
13186be9f0dSKonstantin Belousov struct mtx fault_lock;
13286be9f0dSKonstantin Belousov uint64_t *fault_log;
13386be9f0dSKonstantin Belousov int fault_log_head;
13486be9f0dSKonstantin Belousov int fault_log_tail;
13586be9f0dSKonstantin Belousov int fault_log_size;
13686be9f0dSKonstantin Belousov struct task fault_task;
13786be9f0dSKonstantin Belousov struct taskqueue *fault_taskqueue;
13886be9f0dSKonstantin Belousov
13968eeb96aSKonstantin Belousov /* QI */
14068eeb96aSKonstantin Belousov int qi_enabled;
14168eeb96aSKonstantin Belousov
1420a110d5bSKonstantin Belousov /* IR */
1430a110d5bSKonstantin Belousov int ir_enabled;
1440a110d5bSKonstantin Belousov vm_paddr_t irt_phys;
1450a110d5bSKonstantin Belousov dmar_irte_t *irt;
1460a110d5bSKonstantin Belousov u_int irte_cnt;
1470a110d5bSKonstantin Belousov vmem_t *irtids;
14886be9f0dSKonstantin Belousov };
14986be9f0dSKonstantin Belousov
1500fcbf494SKonstantin Belousov #define DMAR_LOCK(dmar) mtx_lock(&DMAR2IOMMU(dmar)->lock)
1510fcbf494SKonstantin Belousov #define DMAR_UNLOCK(dmar) mtx_unlock(&DMAR2IOMMU(dmar)->lock)
1520fcbf494SKonstantin Belousov #define DMAR_ASSERT_LOCKED(dmar) mtx_assert(&DMAR2IOMMU(dmar)->lock, MA_OWNED)
15386be9f0dSKonstantin Belousov
15486be9f0dSKonstantin Belousov #define DMAR_FAULT_LOCK(dmar) mtx_lock_spin(&(dmar)->fault_lock)
15586be9f0dSKonstantin Belousov #define DMAR_FAULT_UNLOCK(dmar) mtx_unlock_spin(&(dmar)->fault_lock)
15686be9f0dSKonstantin Belousov #define DMAR_FAULT_ASSERT_LOCKED(dmar) mtx_assert(&(dmar)->fault_lock, MA_OWNED)
15786be9f0dSKonstantin Belousov
15886be9f0dSKonstantin Belousov #define DMAR_IS_COHERENT(dmar) (((dmar)->hw_ecap & DMAR_ECAP_C) != 0)
15968eeb96aSKonstantin Belousov #define DMAR_HAS_QI(dmar) (((dmar)->hw_ecap & DMAR_ECAP_QI) != 0)
1600a110d5bSKonstantin Belousov #define DMAR_X2APIC(dmar) \
1610a110d5bSKonstantin Belousov (x2apic_mode && ((dmar)->hw_ecap & DMAR_ECAP_EIM) != 0)
16286be9f0dSKonstantin Belousov
16386be9f0dSKonstantin Belousov /* Barrier ids */
16486be9f0dSKonstantin Belousov #define DMAR_BARRIER_RMRR 0
16586be9f0dSKonstantin Belousov #define DMAR_BARRIER_USEQ 1
16686be9f0dSKonstantin Belousov
1670386b245SKonstantin Belousov SYSCTL_DECL(_hw_iommu_dmar);
1680386b245SKonstantin Belousov
169f9feb091SKonstantin Belousov struct dmar_unit *dmar_find(device_t dev, bool verbose);
1700a110d5bSKonstantin Belousov struct dmar_unit *dmar_find_hpet(device_t dev, uint16_t *rid);
1710a110d5bSKonstantin Belousov struct dmar_unit *dmar_find_ioapic(u_int apic_id, uint16_t *rid);
17286be9f0dSKonstantin Belousov
17386be9f0dSKonstantin Belousov u_int dmar_nd2mask(u_int nd);
17486be9f0dSKonstantin Belousov bool dmar_pglvl_supported(struct dmar_unit *unit, int pglvl);
1751abfd355SKonstantin Belousov int domain_set_agaw(struct dmar_domain *domain, int mgaw);
17659e37c8aSRuslan Bukin int dmar_maxaddr2mgaw(struct dmar_unit *unit, iommu_gaddr_t maxaddr,
17786be9f0dSKonstantin Belousov bool allow_less);
1781abfd355SKonstantin Belousov int domain_is_sp_lvl(struct dmar_domain *domain, int lvl);
17959e37c8aSRuslan Bukin iommu_gaddr_t domain_page_size(struct dmar_domain *domain, int lvl);
18059e37c8aSRuslan Bukin int calc_am(struct dmar_unit *unit, iommu_gaddr_t base, iommu_gaddr_t size,
18159e37c8aSRuslan Bukin iommu_gaddr_t *isizep);
18286be9f0dSKonstantin Belousov int dmar_load_root_entry_ptr(struct dmar_unit *unit);
18386be9f0dSKonstantin Belousov int dmar_inv_ctx_glob(struct dmar_unit *unit);
18486be9f0dSKonstantin Belousov int dmar_inv_iotlb_glob(struct dmar_unit *unit);
18586be9f0dSKonstantin Belousov int dmar_flush_write_bufs(struct dmar_unit *unit);
18640d951bcSKonstantin Belousov void dmar_flush_pte_to_ram(struct dmar_unit *unit, iommu_pte_t *dst);
1876b7c46afSKonstantin Belousov void dmar_flush_ctx_to_ram(struct dmar_unit *unit, dmar_ctx_entry_t *dst);
1886b7c46afSKonstantin Belousov void dmar_flush_root_to_ram(struct dmar_unit *unit, dmar_root_entry_t *dst);
18906e6ca6dSKornel Duleba int dmar_disable_protected_regions(struct dmar_unit *unit);
19086be9f0dSKonstantin Belousov int dmar_enable_translation(struct dmar_unit *unit);
19186be9f0dSKonstantin Belousov int dmar_disable_translation(struct dmar_unit *unit);
1920a110d5bSKonstantin Belousov int dmar_load_irt_ptr(struct dmar_unit *unit);
1930a110d5bSKonstantin Belousov int dmar_enable_ir(struct dmar_unit *unit);
1940a110d5bSKonstantin Belousov int dmar_disable_ir(struct dmar_unit *unit);
19586be9f0dSKonstantin Belousov bool dmar_barrier_enter(struct dmar_unit *dmar, u_int barrier_id);
19686be9f0dSKonstantin Belousov void dmar_barrier_exit(struct dmar_unit *dmar, u_int barrier_id);
197476358b3SKonstantin Belousov uint64_t dmar_get_timeout(void);
198476358b3SKonstantin Belousov void dmar_update_timeout(uint64_t newval);
19986be9f0dSKonstantin Belousov
20068eeb96aSKonstantin Belousov int dmar_fault_intr(void *arg);
2015967352aSKonstantin Belousov void dmar_enable_fault_intr(struct iommu_unit *unit);
2025967352aSKonstantin Belousov void dmar_disable_fault_intr(struct iommu_unit *unit);
20386be9f0dSKonstantin Belousov int dmar_init_fault_log(struct dmar_unit *unit);
20486be9f0dSKonstantin Belousov void dmar_fini_fault_log(struct dmar_unit *unit);
20586be9f0dSKonstantin Belousov
20668eeb96aSKonstantin Belousov int dmar_qi_intr(void *arg);
2075967352aSKonstantin Belousov void dmar_enable_qi_intr(struct iommu_unit *unit);
2085967352aSKonstantin Belousov void dmar_disable_qi_intr(struct iommu_unit *unit);
20968eeb96aSKonstantin Belousov int dmar_init_qi(struct dmar_unit *unit);
21068eeb96aSKonstantin Belousov void dmar_fini_qi(struct dmar_unit *unit);
21142736dc4SAlan Cox void dmar_qi_invalidate_locked(struct dmar_domain *domain,
21242736dc4SAlan Cox struct iommu_map_entry *entry, bool emit_wait);
213c2515634SAlan Cox void dmar_qi_invalidate_sync(struct dmar_domain *domain, iommu_gaddr_t start,
214c2515634SAlan Cox iommu_gaddr_t size, bool cansleep);
21568eeb96aSKonstantin Belousov void dmar_qi_invalidate_ctx_glob_locked(struct dmar_unit *unit);
21668eeb96aSKonstantin Belousov void dmar_qi_invalidate_iotlb_glob_locked(struct dmar_unit *unit);
2170a110d5bSKonstantin Belousov void dmar_qi_invalidate_iec_glob(struct dmar_unit *unit);
2180a110d5bSKonstantin Belousov void dmar_qi_invalidate_iec(struct dmar_unit *unit, u_int start, u_int cnt);
21968eeb96aSKonstantin Belousov
22010ebd230SKonstantin Belousov vm_object_t dmar_get_idmap_pgtbl(struct dmar_domain *domain,
22159e37c8aSRuslan Bukin iommu_gaddr_t maxaddr);
22210ebd230SKonstantin Belousov void dmar_put_idmap_pgtbl(vm_object_t obj);
22310ebd230SKonstantin Belousov void dmar_flush_iotlb_sync(struct dmar_domain *domain, iommu_gaddr_t base,
22459e37c8aSRuslan Bukin iommu_gaddr_t size);
22523145534SKonstantin Belousov int dmar_domain_alloc_pgtbl(struct dmar_domain *domain);
22623145534SKonstantin Belousov void dmar_domain_free_pgtbl(struct dmar_domain *domain);
22716696f60SRuslan Bukin extern const struct iommu_domain_map_ops dmar_domain_map_ops;
22886be9f0dSKonstantin Belousov
229f9feb091SKonstantin Belousov int dmar_dev_depth(device_t child);
230f9feb091SKonstantin Belousov void dmar_dev_path(device_t child, int *busno, void *path1, int depth);
231f9feb091SKonstantin Belousov
2321abfd355SKonstantin Belousov struct dmar_ctx *dmar_get_ctx_for_dev(struct dmar_unit *dmar, device_t dev,
23367499354SRyan Stone uint16_t rid, bool id_mapped, bool rmrr_init);
234f9feb091SKonstantin Belousov struct dmar_ctx *dmar_get_ctx_for_devpath(struct dmar_unit *dmar, uint16_t rid,
235f9feb091SKonstantin Belousov int dev_domain, int dev_busno, const void *dev_path, int dev_path_len,
236f9feb091SKonstantin Belousov bool id_mapped, bool rmrr_init);
2371abfd355SKonstantin Belousov int dmar_move_ctx_to_domain(struct dmar_domain *domain, struct dmar_ctx *ctx);
23865b133e5SKonstantin Belousov void dmar_free_ctx_locked_method(struct iommu_unit *dmar,
23965b133e5SKonstantin Belousov struct iommu_ctx *ctx);
24067499354SRyan Stone struct dmar_ctx *dmar_find_ctx_locked(struct dmar_unit *dmar, uint16_t rid);
24165b133e5SKonstantin Belousov struct iommu_ctx *dmar_get_ctx(struct iommu_unit *iommu, device_t dev,
24265b133e5SKonstantin Belousov uint16_t rid, bool id_mapped, bool rmrr_init);
24365b133e5SKonstantin Belousov void dmar_domain_unload_entry(struct iommu_map_entry *entry, bool free,
24465b133e5SKonstantin Belousov bool cansleep);
24565b133e5SKonstantin Belousov void dmar_domain_unload(struct iommu_domain *iodom,
24665b133e5SKonstantin Belousov struct iommu_map_entries_tailq *entries, bool cansleep);
24786be9f0dSKonstantin Belousov
248f9feb091SKonstantin Belousov void dmar_dev_parse_rmrr(struct dmar_domain *domain, int dev_domain,
249f9feb091SKonstantin Belousov int dev_busno, const void *dev_path, int dev_path_len,
25059e37c8aSRuslan Bukin struct iommu_map_entries_tailq *rmrr_entries);
25159e37c8aSRuslan Bukin int dmar_instantiate_rmrr_ctxs(struct iommu_unit *dmar);
25286be9f0dSKonstantin Belousov
25386be9f0dSKonstantin Belousov void dmar_quirks_post_ident(struct dmar_unit *dmar);
25459e37c8aSRuslan Bukin void dmar_quirks_pre_use(struct iommu_unit *dmar);
25586be9f0dSKonstantin Belousov
2560a110d5bSKonstantin Belousov int dmar_init_irt(struct dmar_unit *unit);
2570a110d5bSKonstantin Belousov void dmar_fini_irt(struct dmar_unit *unit);
25865b133e5SKonstantin Belousov int dmar_alloc_msi_intr(device_t src, u_int *cookies, u_int count);
25965b133e5SKonstantin Belousov int dmar_map_msi_intr(device_t src, u_int cpu, u_int vector, u_int cookie,
26065b133e5SKonstantin Belousov uint64_t *addr, uint32_t *data);
26165b133e5SKonstantin Belousov int dmar_unmap_msi_intr(device_t src, u_int cookie);
26265b133e5SKonstantin Belousov int dmar_map_ioapic_intr(u_int ioapic_id, u_int cpu, u_int vector, bool edge,
26365b133e5SKonstantin Belousov bool activehi, int irq, u_int *cookie, uint32_t *hi, uint32_t *lo);
26465b133e5SKonstantin Belousov int dmar_unmap_ioapic_intr(u_int ioapic_id, u_int *cookie);
2650a110d5bSKonstantin Belousov
266*512bb0daSKonstantin Belousov int dmar_is_running(void);
267*512bb0daSKonstantin Belousov
26886be9f0dSKonstantin Belousov extern int haw;
26924e38af6SKonstantin Belousov extern int dmar_rmrr_enable;
27086be9f0dSKonstantin Belousov
27186be9f0dSKonstantin Belousov static inline uint32_t
dmar_read4(const struct dmar_unit * unit,int reg)27286be9f0dSKonstantin Belousov dmar_read4(const struct dmar_unit *unit, int reg)
27386be9f0dSKonstantin Belousov {
27486be9f0dSKonstantin Belousov
27586be9f0dSKonstantin Belousov return (bus_read_4(unit->regs, reg));
27686be9f0dSKonstantin Belousov }
27786be9f0dSKonstantin Belousov
27886be9f0dSKonstantin Belousov static inline uint64_t
dmar_read8(const struct dmar_unit * unit,int reg)27986be9f0dSKonstantin Belousov dmar_read8(const struct dmar_unit *unit, int reg)
28086be9f0dSKonstantin Belousov {
28186be9f0dSKonstantin Belousov #ifdef __i386__
28286be9f0dSKonstantin Belousov uint32_t high, low;
28386be9f0dSKonstantin Belousov
28486be9f0dSKonstantin Belousov low = bus_read_4(unit->regs, reg);
28586be9f0dSKonstantin Belousov high = bus_read_4(unit->regs, reg + 4);
28686be9f0dSKonstantin Belousov return (low | ((uint64_t)high << 32));
28786be9f0dSKonstantin Belousov #else
28886be9f0dSKonstantin Belousov return (bus_read_8(unit->regs, reg));
28986be9f0dSKonstantin Belousov #endif
29086be9f0dSKonstantin Belousov }
29186be9f0dSKonstantin Belousov
29286be9f0dSKonstantin Belousov static inline void
dmar_write4(const struct dmar_unit * unit,int reg,uint32_t val)29386be9f0dSKonstantin Belousov dmar_write4(const struct dmar_unit *unit, int reg, uint32_t val)
29486be9f0dSKonstantin Belousov {
29586be9f0dSKonstantin Belousov
29686be9f0dSKonstantin Belousov KASSERT(reg != DMAR_GCMD_REG || (val & DMAR_GCMD_TE) ==
29786be9f0dSKonstantin Belousov (unit->hw_gcmd & DMAR_GCMD_TE),
29859e37c8aSRuslan Bukin ("dmar%d clearing TE 0x%08x 0x%08x", unit->iommu.unit,
29986be9f0dSKonstantin Belousov unit->hw_gcmd, val));
30086be9f0dSKonstantin Belousov bus_write_4(unit->regs, reg, val);
30186be9f0dSKonstantin Belousov }
30286be9f0dSKonstantin Belousov
30386be9f0dSKonstantin Belousov static inline void
dmar_write8(const struct dmar_unit * unit,int reg,uint64_t val)30486be9f0dSKonstantin Belousov dmar_write8(const struct dmar_unit *unit, int reg, uint64_t val)
30586be9f0dSKonstantin Belousov {
30686be9f0dSKonstantin Belousov
30786be9f0dSKonstantin Belousov KASSERT(reg != DMAR_GCMD_REG, ("8byte GCMD write"));
30886be9f0dSKonstantin Belousov #ifdef __i386__
30986be9f0dSKonstantin Belousov uint32_t high, low;
31086be9f0dSKonstantin Belousov
31186be9f0dSKonstantin Belousov low = val;
31286be9f0dSKonstantin Belousov high = val >> 32;
31386be9f0dSKonstantin Belousov bus_write_4(unit->regs, reg, low);
31486be9f0dSKonstantin Belousov bus_write_4(unit->regs, reg + 4, high);
31586be9f0dSKonstantin Belousov #else
31686be9f0dSKonstantin Belousov bus_write_8(unit->regs, reg, val);
31786be9f0dSKonstantin Belousov #endif
31886be9f0dSKonstantin Belousov }
31986be9f0dSKonstantin Belousov
32086be9f0dSKonstantin Belousov /*
32186be9f0dSKonstantin Belousov * dmar_pte_store and dmar_pte_clear ensure that on i386, 32bit writes
32286be9f0dSKonstantin Belousov * are issued in the correct order. For store, the lower word,
32386be9f0dSKonstantin Belousov * containing the P or R and W bits, is set only after the high word
32486be9f0dSKonstantin Belousov * is written. For clear, the P bit is cleared first, then the high
32586be9f0dSKonstantin Belousov * word is cleared.
3260a110d5bSKonstantin Belousov *
3270a110d5bSKonstantin Belousov * dmar_pte_update updates the pte. For amd64, the update is atomic.
3280a110d5bSKonstantin Belousov * For i386, it first disables the entry by clearing the word
3290a110d5bSKonstantin Belousov * containing the P bit, and then defer to dmar_pte_store. The locked
3300a110d5bSKonstantin Belousov * cmpxchg8b is probably available on any machine having DMAR support,
3310a110d5bSKonstantin Belousov * but interrupt translation table may be mapped uncached.
33286be9f0dSKonstantin Belousov */
33386be9f0dSKonstantin Belousov static inline void
dmar_pte_store1(volatile uint64_t * dst,uint64_t val)3340a110d5bSKonstantin Belousov dmar_pte_store1(volatile uint64_t *dst, uint64_t val)
33586be9f0dSKonstantin Belousov {
33686be9f0dSKonstantin Belousov #ifdef __i386__
33786be9f0dSKonstantin Belousov volatile uint32_t *p;
33886be9f0dSKonstantin Belousov uint32_t hi, lo;
33986be9f0dSKonstantin Belousov
34086be9f0dSKonstantin Belousov hi = val >> 32;
34186be9f0dSKonstantin Belousov lo = val;
34286be9f0dSKonstantin Belousov p = (volatile uint32_t *)dst;
34386be9f0dSKonstantin Belousov *(p + 1) = hi;
34486be9f0dSKonstantin Belousov *p = lo;
34586be9f0dSKonstantin Belousov #else
34686be9f0dSKonstantin Belousov *dst = val;
34786be9f0dSKonstantin Belousov #endif
34886be9f0dSKonstantin Belousov }
34986be9f0dSKonstantin Belousov
35086be9f0dSKonstantin Belousov static inline void
dmar_pte_store(volatile uint64_t * dst,uint64_t val)3510a110d5bSKonstantin Belousov dmar_pte_store(volatile uint64_t *dst, uint64_t val)
3520a110d5bSKonstantin Belousov {
3530a110d5bSKonstantin Belousov
3540a110d5bSKonstantin Belousov KASSERT(*dst == 0, ("used pte %p oldval %jx newval %jx",
3550a110d5bSKonstantin Belousov dst, (uintmax_t)*dst, (uintmax_t)val));
3560a110d5bSKonstantin Belousov dmar_pte_store1(dst, val);
3570a110d5bSKonstantin Belousov }
3580a110d5bSKonstantin Belousov
3590a110d5bSKonstantin Belousov static inline void
dmar_pte_update(volatile uint64_t * dst,uint64_t val)3600a110d5bSKonstantin Belousov dmar_pte_update(volatile uint64_t *dst, uint64_t val)
3610a110d5bSKonstantin Belousov {
3620a110d5bSKonstantin Belousov
3630a110d5bSKonstantin Belousov #ifdef __i386__
3640a110d5bSKonstantin Belousov volatile uint32_t *p;
3650a110d5bSKonstantin Belousov
3660a110d5bSKonstantin Belousov p = (volatile uint32_t *)dst;
3670a110d5bSKonstantin Belousov *p = 0;
3680a110d5bSKonstantin Belousov #endif
3690a110d5bSKonstantin Belousov dmar_pte_store1(dst, val);
3700a110d5bSKonstantin Belousov }
3710a110d5bSKonstantin Belousov
3720a110d5bSKonstantin Belousov static inline void
dmar_pte_clear(volatile uint64_t * dst)37386be9f0dSKonstantin Belousov dmar_pte_clear(volatile uint64_t *dst)
37486be9f0dSKonstantin Belousov {
37586be9f0dSKonstantin Belousov #ifdef __i386__
37686be9f0dSKonstantin Belousov volatile uint32_t *p;
37786be9f0dSKonstantin Belousov
37886be9f0dSKonstantin Belousov p = (volatile uint32_t *)dst;
37986be9f0dSKonstantin Belousov *p = 0;
38086be9f0dSKonstantin Belousov *(p + 1) = 0;
38186be9f0dSKonstantin Belousov #else
38286be9f0dSKonstantin Belousov *dst = 0;
38386be9f0dSKonstantin Belousov #endif
38486be9f0dSKonstantin Belousov }
38586be9f0dSKonstantin Belousov
386476358b3SKonstantin Belousov extern struct timespec dmar_hw_timeout;
387476358b3SKonstantin Belousov
388476358b3SKonstantin Belousov #define DMAR_WAIT_UNTIL(cond) \
389476358b3SKonstantin Belousov { \
390476358b3SKonstantin Belousov struct timespec last, curr; \
391476358b3SKonstantin Belousov bool forever; \
392476358b3SKonstantin Belousov \
393476358b3SKonstantin Belousov if (dmar_hw_timeout.tv_sec == 0 && \
394476358b3SKonstantin Belousov dmar_hw_timeout.tv_nsec == 0) { \
395476358b3SKonstantin Belousov forever = true; \
396476358b3SKonstantin Belousov } else { \
397476358b3SKonstantin Belousov forever = false; \
398476358b3SKonstantin Belousov nanouptime(&curr); \
3996040822cSAlan Somers timespecadd(&curr, &dmar_hw_timeout, &last); \
400476358b3SKonstantin Belousov } \
401476358b3SKonstantin Belousov for (;;) { \
402476358b3SKonstantin Belousov if (cond) { \
403476358b3SKonstantin Belousov error = 0; \
404476358b3SKonstantin Belousov break; \
405476358b3SKonstantin Belousov } \
406476358b3SKonstantin Belousov nanouptime(&curr); \
407476358b3SKonstantin Belousov if (!forever && timespeccmp(&last, &curr, <)) { \
408476358b3SKonstantin Belousov error = ETIMEDOUT; \
409476358b3SKonstantin Belousov break; \
410476358b3SKonstantin Belousov } \
411476358b3SKonstantin Belousov cpu_spinwait(); \
412476358b3SKonstantin Belousov } \
413476358b3SKonstantin Belousov }
414476358b3SKonstantin Belousov
41586be9f0dSKonstantin Belousov #ifdef INVARIANTS
41686be9f0dSKonstantin Belousov #define TD_PREP_PINNED_ASSERT \
41786be9f0dSKonstantin Belousov int old_td_pinned; \
41886be9f0dSKonstantin Belousov old_td_pinned = curthread->td_pinned
41986be9f0dSKonstantin Belousov #define TD_PINNED_ASSERT \
42086be9f0dSKonstantin Belousov KASSERT(curthread->td_pinned == old_td_pinned, \
42186be9f0dSKonstantin Belousov ("pin count leak: %d %d %s:%d", curthread->td_pinned, \
42286be9f0dSKonstantin Belousov old_td_pinned, __FILE__, __LINE__))
42386be9f0dSKonstantin Belousov #else
42486be9f0dSKonstantin Belousov #define TD_PREP_PINNED_ASSERT
42586be9f0dSKonstantin Belousov #define TD_PINNED_ASSERT
42686be9f0dSKonstantin Belousov #endif
42786be9f0dSKonstantin Belousov
42886be9f0dSKonstantin Belousov #endif
429