1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2013-2015, 2024 The FreeBSD Foundation 5 * 6 * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #ifndef __X86_IOMMU_X86_IOMMU_H 32 #define __X86_IOMMU_X86_IOMMU_H 33 34 /* Both Intel and AMD are not too crazy to have different sizes. */ 35 typedef struct iommu_pte { 36 uint64_t pte; 37 } iommu_pte_t; 38 39 #define IOMMU_PAGE_SIZE PAGE_SIZE 40 #define IOMMU_PAGE_MASK (IOMMU_PAGE_SIZE - 1) 41 #define IOMMU_PAGE_SHIFT PAGE_SHIFT 42 #define IOMMU_NPTEPG (IOMMU_PAGE_SIZE / sizeof(iommu_pte_t)) 43 #define IOMMU_NPTEPGSHIFT 9 44 #define IOMMU_PTEMASK (IOMMU_NPTEPG - 1) 45 46 struct sf_buf; 47 struct vm_object; 48 49 struct vm_page *iommu_pgalloc(struct vm_object *obj, vm_pindex_t idx, 50 int flags); 51 void iommu_pgfree(struct vm_object *obj, vm_pindex_t idx, int flags, 52 struct iommu_map_entry *entry); 53 void *iommu_map_pgtbl(struct vm_object *obj, vm_pindex_t idx, int flags, 54 struct sf_buf **sf); 55 void iommu_unmap_pgtbl(struct sf_buf *sf); 56 57 extern iommu_haddr_t iommu_high; 58 extern int iommu_tbl_pagecnt; 59 extern int iommu_qi_batch_coalesce; 60 61 SYSCTL_DECL(_hw_iommu); 62 63 struct x86_unit_common; 64 65 struct x86_iommu { 66 struct x86_unit_common *(*get_x86_common)(struct 67 iommu_unit *iommu); 68 void (*unit_pre_instantiate_ctx)(struct iommu_unit *iommu); 69 void (*qi_ensure)(struct iommu_unit *unit, int descr_count); 70 void (*qi_emit_wait_descr)(struct iommu_unit *unit, uint32_t seq, 71 bool, bool, bool); 72 void (*qi_advance_tail)(struct iommu_unit *unit); 73 void (*qi_invalidate_emit)(struct iommu_domain *idomain, 74 iommu_gaddr_t base, iommu_gaddr_t size, struct iommu_qi_genseq * 75 pseq, bool emit_wait); 76 void (*domain_unload_entry)(struct iommu_map_entry *entry, bool free, 77 bool cansleep); 78 void (*domain_unload)(struct iommu_domain *iodom, 79 struct iommu_map_entries_tailq *entries, bool cansleep); 80 struct iommu_ctx *(*get_ctx)(struct iommu_unit *iommu, 81 device_t dev, uint16_t rid, bool id_mapped, bool rmrr_init); 82 void (*free_ctx_locked)(struct iommu_unit *iommu, 83 struct iommu_ctx *context); 84 struct iommu_unit *(*find)(device_t dev, bool verbose); 85 int (*alloc_msi_intr)(device_t src, u_int *cookies, u_int count); 86 int (*map_msi_intr)(device_t src, u_int cpu, u_int vector, 87 u_int cookie, uint64_t *addr, uint32_t *data); 88 int (*unmap_msi_intr)(device_t src, u_int cookie); 89 int (*map_ioapic_intr)(u_int ioapic_id, u_int cpu, u_int vector, 90 bool edge, bool activehi, int irq, u_int *cookie, uint32_t *hi, 91 uint32_t *lo); 92 int (*unmap_ioapic_intr)(u_int ioapic_id, u_int *cookie); 93 }; 94 void set_x86_iommu(struct x86_iommu *); 95 struct x86_iommu *get_x86_iommu(void); 96 97 struct iommu_msi_data { 98 int irq; 99 int irq_rid; 100 struct resource *irq_res; 101 void *intr_handle; 102 int (*handler)(void *); 103 int msi_data_reg; 104 int msi_addr_reg; 105 int msi_uaddr_reg; 106 uint64_t msi_addr; 107 uint32_t msi_data; 108 void (*enable_intr)(struct iommu_unit *); 109 void (*disable_intr)(struct iommu_unit *); 110 const char *name; 111 }; 112 113 #define IOMMU_MAX_MSI 3 114 115 struct x86_unit_common { 116 uint32_t qi_buf_maxsz; 117 uint32_t qi_cmd_sz; 118 119 char *inv_queue; 120 vm_size_t inv_queue_size; 121 uint32_t inv_queue_avail; 122 uint32_t inv_queue_tail; 123 124 /* 125 * Hw writes there on completion of wait descriptor 126 * processing. Intel writes 4 bytes, while AMD does the 127 * 8-bytes write. Due to little-endian, and use of 4-byte 128 * sequence numbers, the difference does not matter for us. 129 */ 130 volatile uint64_t inv_waitd_seq_hw; 131 132 uint64_t inv_waitd_seq_hw_phys; 133 uint32_t inv_waitd_seq; /* next sequence number to use for wait descr */ 134 u_int inv_waitd_gen; /* seq number generation AKA seq overflows */ 135 u_int inv_seq_waiters; /* count of waiters for seq */ 136 u_int inv_queue_full; /* informational counter */ 137 138 /* 139 * Delayed freeing of map entries queue processing: 140 * 141 * tlb_flush_head and tlb_flush_tail are used to implement a FIFO 142 * queue that supports concurrent dequeues and enqueues. However, 143 * there can only be a single dequeuer (accessing tlb_flush_head) and 144 * a single enqueuer (accessing tlb_flush_tail) at a time. Since the 145 * unit's qi_task is the only dequeuer, it can access tlb_flush_head 146 * without any locking. In contrast, there may be multiple enqueuers, 147 * so the enqueuers acquire the iommu unit lock to serialize their 148 * accesses to tlb_flush_tail. 149 * 150 * In this FIFO queue implementation, the key to enabling concurrent 151 * dequeues and enqueues is that the dequeuer never needs to access 152 * tlb_flush_tail and the enqueuer never needs to access 153 * tlb_flush_head. In particular, tlb_flush_head and tlb_flush_tail 154 * are never NULL, so neither a dequeuer nor an enqueuer ever needs to 155 * update both. Instead, tlb_flush_head always points to a "zombie" 156 * struct, which previously held the last dequeued item. Thus, the 157 * zombie's next field actually points to the struct holding the first 158 * item in the queue. When an item is dequeued, the current zombie is 159 * finally freed, and the struct that held the just dequeued item 160 * becomes the new zombie. When the queue is empty, tlb_flush_tail 161 * also points to the zombie. 162 */ 163 struct iommu_map_entry *tlb_flush_head; 164 struct iommu_map_entry *tlb_flush_tail; 165 struct task qi_task; 166 struct taskqueue *qi_taskqueue; 167 168 struct iommu_msi_data intrs[IOMMU_MAX_MSI]; 169 }; 170 171 void iommu_domain_free_entry(struct iommu_map_entry *entry, bool free); 172 173 void iommu_qi_emit_wait_seq(struct iommu_unit *unit, struct iommu_qi_genseq * 174 pseq, bool emit_wait); 175 void iommu_qi_wait_for_seq(struct iommu_unit *unit, const struct 176 iommu_qi_genseq *gseq, bool nowait); 177 void iommu_qi_drain_tlb_flush(struct iommu_unit *unit); 178 void iommu_qi_invalidate_locked(struct iommu_domain *domain, 179 struct iommu_map_entry *entry, bool emit_wait); 180 void iommu_qi_invalidate_sync(struct iommu_domain *domain, iommu_gaddr_t base, 181 iommu_gaddr_t size, bool cansleep); 182 void iommu_qi_common_init(struct iommu_unit *unit, task_fn_t taskfunc); 183 void iommu_qi_common_fini(struct iommu_unit *unit, void (*disable_qi)( 184 struct iommu_unit *)); 185 186 int iommu_alloc_irq(struct iommu_unit *unit, int idx); 187 void iommu_release_intr(struct iommu_unit *unit, int idx); 188 189 void iommu_device_tag_init(struct iommu_ctx *ctx, device_t dev); 190 void iommu_device_set_iommu_prop(device_t dev, device_t iommu); 191 192 int pglvl_pgtbl_pte_off(int pglvl, iommu_gaddr_t base, int lvl); 193 vm_pindex_t pglvl_pgtbl_get_pindex(int pglvl, iommu_gaddr_t base, int lvl); 194 vm_pindex_t pglvl_max_pages(int pglvl); 195 iommu_gaddr_t pglvl_page_size(int total_pglvl, int lvl); 196 197 void iommu_db_print_domain_entry(const struct iommu_map_entry *entry); 198 void iommu_db_print_ctx(struct iommu_ctx *ctx); 199 void iommu_db_domain_print_contexts(struct iommu_domain *iodom); 200 void iommu_db_domain_print_mappings(struct iommu_domain *iodom); 201 202 #endif 203