1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2013 The FreeBSD Foundation 5 * All rights reserved. 6 * 7 * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 8 * under sponsorship from the FreeBSD Foundation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * $FreeBSD$ 32 */ 33 34 #ifndef _DEV_IOMMU_IOMMU_H_ 35 #define _DEV_IOMMU_IOMMU_H_ 36 37 #include <dev/iommu/iommu_types.h> 38 39 struct bus_dma_tag_common; 40 struct iommu_map_entry; 41 TAILQ_HEAD(iommu_map_entries_tailq, iommu_map_entry); 42 43 RB_HEAD(iommu_gas_entries_tree, iommu_map_entry); 44 RB_PROTOTYPE(iommu_gas_entries_tree, iommu_map_entry, rb_entry, 45 iommu_gas_cmp_entries); 46 47 struct iommu_qi_genseq { 48 u_int gen; 49 uint32_t seq; 50 }; 51 52 struct iommu_map_entry { 53 iommu_gaddr_t start; 54 iommu_gaddr_t end; 55 iommu_gaddr_t first; /* Least start in subtree */ 56 iommu_gaddr_t last; /* Greatest end in subtree */ 57 iommu_gaddr_t free_down; /* Max free space below the 58 current R/B tree node */ 59 u_int flags; 60 TAILQ_ENTRY(iommu_map_entry) dmamap_link; /* Link for dmamap entries */ 61 RB_ENTRY(iommu_map_entry) rb_entry; /* Links for domain entries */ 62 TAILQ_ENTRY(iommu_map_entry) unroll_link; /* Link for unroll after 63 dmamap_load failure */ 64 struct iommu_domain *domain; 65 struct iommu_qi_genseq gseq; 66 }; 67 68 struct iommu_unit { 69 struct mtx lock; 70 int unit; 71 72 int dma_enabled; 73 74 /* Busdma delayed map load */ 75 struct task dmamap_load_task; 76 TAILQ_HEAD(, bus_dmamap_iommu) delayed_maps; 77 struct taskqueue *delayed_taskqueue; 78 79 /* 80 * Bitmap of buses for which context must ignore slot:func, 81 * duplicating the page table pointer into all context table 82 * entries. This is a client-controlled quirk to support some 83 * NTBs. 84 */ 85 uint32_t buswide_ctxs[(PCI_BUSMAX + 1) / NBBY / sizeof(uint32_t)]; 86 }; 87 88 struct iommu_domain_map_ops { 89 int (*map)(struct iommu_domain *domain, iommu_gaddr_t base, 90 iommu_gaddr_t size, vm_page_t *ma, uint64_t pflags, int flags); 91 int (*unmap)(struct iommu_domain *domain, iommu_gaddr_t base, 92 iommu_gaddr_t size, int flags); 93 }; 94 95 /* 96 * Locking annotations: 97 * (u) - Protected by iommu unit lock 98 * (d) - Protected by domain lock 99 * (c) - Immutable after initialization 100 */ 101 102 struct iommu_domain { 103 struct iommu_unit *iommu; /* (c) */ 104 const struct iommu_domain_map_ops *ops; 105 struct mtx lock; /* (c) */ 106 struct task unload_task; /* (c) */ 107 u_int entries_cnt; /* (d) */ 108 struct iommu_map_entries_tailq unload_entries; /* (d) Entries to 109 unload */ 110 struct iommu_gas_entries_tree rb_root; /* (d) */ 111 iommu_gaddr_t end; /* (c) Highest address + 1 in 112 the guest AS */ 113 struct iommu_map_entry *first_place, *last_place; /* (d) */ 114 u_int flags; /* (u) */ 115 }; 116 117 struct iommu_ctx { 118 struct iommu_domain *domain; /* (c) */ 119 struct bus_dma_tag_iommu *tag; /* (c) Root tag */ 120 u_long loads; /* atomic updates, for stat only */ 121 u_long unloads; /* same */ 122 u_int flags; /* (u) */ 123 uint16_t rid; /* (c) pci RID */ 124 }; 125 126 /* struct iommu_ctx flags */ 127 #define IOMMU_CTX_FAULTED 0x0001 /* Fault was reported, 128 last_fault_rec is valid */ 129 #define IOMMU_CTX_DISABLED 0x0002 /* Device is disabled, the 130 ephemeral reference is kept 131 to prevent context destruction */ 132 133 #define IOMMU_DOMAIN_GAS_INITED 0x0001 134 #define IOMMU_DOMAIN_PGTBL_INITED 0x0002 135 #define IOMMU_DOMAIN_IDMAP 0x0010 /* Domain uses identity 136 page table */ 137 #define IOMMU_DOMAIN_RMRR 0x0020 /* Domain contains RMRR entry, 138 cannot be turned off */ 139 140 #define IOMMU_LOCK(unit) mtx_lock(&(unit)->lock) 141 #define IOMMU_UNLOCK(unit) mtx_unlock(&(unit)->lock) 142 #define IOMMU_ASSERT_LOCKED(unit) mtx_assert(&(unit)->lock, MA_OWNED) 143 144 #define IOMMU_DOMAIN_LOCK(dom) mtx_lock(&(dom)->lock) 145 #define IOMMU_DOMAIN_UNLOCK(dom) mtx_unlock(&(dom)->lock) 146 #define IOMMU_DOMAIN_ASSERT_LOCKED(dom) mtx_assert(&(dom)->lock, MA_OWNED) 147 148 static inline bool 149 iommu_test_boundary(iommu_gaddr_t start, iommu_gaddr_t size, 150 iommu_gaddr_t boundary) 151 { 152 153 if (boundary == 0) 154 return (true); 155 return (start + size <= ((start + boundary) & ~(boundary - 1))); 156 } 157 158 void iommu_free_ctx(struct iommu_ctx *ctx); 159 void iommu_free_ctx_locked(struct iommu_unit *iommu, struct iommu_ctx *ctx); 160 struct iommu_ctx *iommu_get_ctx(struct iommu_unit *, device_t dev, 161 uint16_t rid, bool id_mapped, bool rmrr_init); 162 struct iommu_unit *iommu_find(device_t dev, bool verbose); 163 void iommu_domain_unload_entry(struct iommu_map_entry *entry, bool free); 164 void iommu_domain_unload(struct iommu_domain *domain, 165 struct iommu_map_entries_tailq *entries, bool cansleep); 166 167 struct iommu_ctx *iommu_instantiate_ctx(struct iommu_unit *iommu, 168 device_t dev, bool rmrr); 169 device_t iommu_get_requester(device_t dev, uint16_t *rid); 170 int iommu_init_busdma(struct iommu_unit *unit); 171 void iommu_fini_busdma(struct iommu_unit *unit); 172 struct iommu_map_entry *iommu_map_alloc_entry(struct iommu_domain *iodom, 173 u_int flags); 174 void iommu_map_free_entry(struct iommu_domain *, struct iommu_map_entry *); 175 int iommu_map(struct iommu_domain *iodom, 176 const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset, 177 u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res); 178 int iommu_map_region(struct iommu_domain *domain, 179 struct iommu_map_entry *entry, u_int eflags, u_int flags, vm_page_t *ma); 180 181 void iommu_gas_init_domain(struct iommu_domain *domain); 182 void iommu_gas_fini_domain(struct iommu_domain *domain); 183 struct iommu_map_entry *iommu_gas_alloc_entry(struct iommu_domain *domain, 184 u_int flags); 185 void iommu_gas_free_entry(struct iommu_domain *domain, 186 struct iommu_map_entry *entry); 187 void iommu_gas_free_space(struct iommu_domain *domain, 188 struct iommu_map_entry *entry); 189 int iommu_gas_map(struct iommu_domain *domain, 190 const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset, 191 u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res); 192 void iommu_gas_free_region(struct iommu_domain *domain, 193 struct iommu_map_entry *entry); 194 int iommu_gas_map_region(struct iommu_domain *domain, 195 struct iommu_map_entry *entry, u_int eflags, u_int flags, vm_page_t *ma); 196 int iommu_gas_reserve_region(struct iommu_domain *domain, iommu_gaddr_t start, 197 iommu_gaddr_t end); 198 199 void iommu_set_buswide_ctx(struct iommu_unit *unit, u_int busno); 200 bool iommu_is_buswide_ctx(struct iommu_unit *unit, u_int busno); 201 void iommu_domain_init(struct iommu_unit *unit, struct iommu_domain *domain, 202 const struct iommu_domain_map_ops *ops); 203 void iommu_domain_fini(struct iommu_domain *domain); 204 205 bool bus_dma_iommu_set_buswide(device_t dev); 206 int bus_dma_iommu_load_ident(bus_dma_tag_t dmat, bus_dmamap_t map, 207 vm_paddr_t start, vm_size_t length, int flags); 208 209 bus_dma_tag_t iommu_get_dma_tag(device_t dev, device_t child); 210 struct iommu_ctx *iommu_get_dev_ctx(device_t dev); 211 struct iommu_domain *iommu_get_ctx_domain(struct iommu_ctx *ctx); 212 213 SYSCTL_DECL(_hw_iommu); 214 215 #endif /* !_DEV_IOMMU_IOMMU_H_ */ 216