140d951bcSKonstantin Belousov /*- 240d951bcSKonstantin Belousov * SPDX-License-Identifier: BSD-2-Clause 340d951bcSKonstantin Belousov * 465b133e5SKonstantin Belousov * Copyright (c) 2013-2015, 2024 The FreeBSD Foundation 540d951bcSKonstantin Belousov * 640d951bcSKonstantin Belousov * This software was developed by Konstantin Belousov <kib@FreeBSD.org> 740d951bcSKonstantin Belousov * under sponsorship from the FreeBSD Foundation. 840d951bcSKonstantin Belousov * 940d951bcSKonstantin Belousov * Redistribution and use in source and binary forms, with or without 1040d951bcSKonstantin Belousov * modification, are permitted provided that the following conditions 1140d951bcSKonstantin Belousov * are met: 1240d951bcSKonstantin Belousov * 1. Redistributions of source code must retain the above copyright 1340d951bcSKonstantin Belousov * notice, this list of conditions and the following disclaimer. 1440d951bcSKonstantin Belousov * 2. Redistributions in binary form must reproduce the above copyright 1540d951bcSKonstantin Belousov * notice, this list of conditions and the following disclaimer in the 1640d951bcSKonstantin Belousov * documentation and/or other materials provided with the distribution. 1740d951bcSKonstantin Belousov * 1840d951bcSKonstantin Belousov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1940d951bcSKonstantin Belousov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2040d951bcSKonstantin Belousov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2140d951bcSKonstantin Belousov * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2240d951bcSKonstantin Belousov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2340d951bcSKonstantin Belousov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2440d951bcSKonstantin Belousov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2540d951bcSKonstantin Belousov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2640d951bcSKonstantin Belousov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2740d951bcSKonstantin Belousov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2840d951bcSKonstantin Belousov * SUCH DAMAGE. 2940d951bcSKonstantin Belousov */ 3040d951bcSKonstantin Belousov 3140d951bcSKonstantin Belousov #ifndef __X86_IOMMU_X86_IOMMU_H 3240d951bcSKonstantin Belousov #define __X86_IOMMU_X86_IOMMU_H 3340d951bcSKonstantin Belousov 3440d951bcSKonstantin Belousov /* Both Intel and AMD are not too crazy to have different sizes. */ 3540d951bcSKonstantin Belousov typedef struct iommu_pte { 3640d951bcSKonstantin Belousov uint64_t pte; 3740d951bcSKonstantin Belousov } iommu_pte_t; 3840d951bcSKonstantin Belousov 3940d951bcSKonstantin Belousov #define IOMMU_PAGE_SIZE PAGE_SIZE 4040d951bcSKonstantin Belousov #define IOMMU_PAGE_MASK (IOMMU_PAGE_SIZE - 1) 4140d951bcSKonstantin Belousov #define IOMMU_PAGE_SHIFT PAGE_SHIFT 4240d951bcSKonstantin Belousov #define IOMMU_NPTEPG (IOMMU_PAGE_SIZE / sizeof(iommu_pte_t)) 4340d951bcSKonstantin Belousov #define IOMMU_NPTEPGSHIFT 9 4440d951bcSKonstantin Belousov #define IOMMU_PTEMASK (IOMMU_NPTEPG - 1) 4540d951bcSKonstantin Belousov 4640d951bcSKonstantin Belousov struct sf_buf; 4740d951bcSKonstantin Belousov struct vm_object; 4840d951bcSKonstantin Belousov 4940d951bcSKonstantin Belousov struct vm_page *iommu_pgalloc(struct vm_object *obj, vm_pindex_t idx, 5040d951bcSKonstantin Belousov int flags); 5140d951bcSKonstantin Belousov void iommu_pgfree(struct vm_object *obj, vm_pindex_t idx, int flags); 5240d951bcSKonstantin Belousov void *iommu_map_pgtbl(struct vm_object *obj, vm_pindex_t idx, int flags, 5340d951bcSKonstantin Belousov struct sf_buf **sf); 5440d951bcSKonstantin Belousov void iommu_unmap_pgtbl(struct sf_buf *sf); 5540d951bcSKonstantin Belousov 5640d951bcSKonstantin Belousov extern iommu_haddr_t iommu_high; 5740d951bcSKonstantin Belousov extern int iommu_tbl_pagecnt; 5840d951bcSKonstantin Belousov 5940d951bcSKonstantin Belousov SYSCTL_DECL(_hw_iommu); 6040d951bcSKonstantin Belousov SYSCTL_DECL(_hw_iommu_dmar); 6140d951bcSKonstantin Belousov 62ad794e6dSKonstantin Belousov struct x86_unit_common; 63ad794e6dSKonstantin Belousov 6465b133e5SKonstantin Belousov struct x86_iommu { 65ad794e6dSKonstantin Belousov struct x86_unit_common *(*get_x86_common)(struct 66ad794e6dSKonstantin Belousov iommu_unit *iommu); 67ad794e6dSKonstantin Belousov void (*qi_ensure)(struct iommu_unit *unit, int descr_count); 68ad794e6dSKonstantin Belousov void (*qi_emit_wait_descr)(struct iommu_unit *unit, uint32_t seq, 69ad794e6dSKonstantin Belousov bool, bool, bool); 70ad794e6dSKonstantin Belousov void (*qi_advance_tail)(struct iommu_unit *unit); 71ad794e6dSKonstantin Belousov void (*qi_invalidate_emit)(struct iommu_domain *idomain, 72ad794e6dSKonstantin Belousov iommu_gaddr_t base, iommu_gaddr_t size, struct iommu_qi_genseq * 73ad794e6dSKonstantin Belousov pseq, bool emit_wait); 7465b133e5SKonstantin Belousov void (*domain_unload_entry)(struct iommu_map_entry *entry, bool free, 7565b133e5SKonstantin Belousov bool cansleep); 7665b133e5SKonstantin Belousov void (*domain_unload)(struct iommu_domain *iodom, 7765b133e5SKonstantin Belousov struct iommu_map_entries_tailq *entries, bool cansleep); 7865b133e5SKonstantin Belousov struct iommu_ctx *(*get_ctx)(struct iommu_unit *iommu, 7965b133e5SKonstantin Belousov device_t dev, uint16_t rid, bool id_mapped, bool rmrr_init); 8065b133e5SKonstantin Belousov void (*free_ctx_locked)(struct iommu_unit *iommu, 8165b133e5SKonstantin Belousov struct iommu_ctx *context); 8265b133e5SKonstantin Belousov void (*free_ctx)(struct iommu_ctx *context); 8365b133e5SKonstantin Belousov struct iommu_unit *(*find)(device_t dev, bool verbose); 8465b133e5SKonstantin Belousov int (*alloc_msi_intr)(device_t src, u_int *cookies, u_int count); 8565b133e5SKonstantin Belousov int (*map_msi_intr)(device_t src, u_int cpu, u_int vector, 8665b133e5SKonstantin Belousov u_int cookie, uint64_t *addr, uint32_t *data); 8765b133e5SKonstantin Belousov int (*unmap_msi_intr)(device_t src, u_int cookie); 8865b133e5SKonstantin Belousov int (*map_ioapic_intr)(u_int ioapic_id, u_int cpu, u_int vector, 8965b133e5SKonstantin Belousov bool edge, bool activehi, int irq, u_int *cookie, uint32_t *hi, 9065b133e5SKonstantin Belousov uint32_t *lo); 9165b133e5SKonstantin Belousov int (*unmap_ioapic_intr)(u_int ioapic_id, u_int *cookie); 9265b133e5SKonstantin Belousov }; 9365b133e5SKonstantin Belousov void set_x86_iommu(struct x86_iommu *); 9465b133e5SKonstantin Belousov struct x86_iommu *get_x86_iommu(void); 9565b133e5SKonstantin Belousov 96ad794e6dSKonstantin Belousov struct x86_unit_common { 97ad794e6dSKonstantin Belousov uint32_t qi_buf_maxsz; 98ad794e6dSKonstantin Belousov uint32_t qi_cmd_sz; 99ad794e6dSKonstantin Belousov 100ad794e6dSKonstantin Belousov char *inv_queue; 101ad794e6dSKonstantin Belousov vm_size_t inv_queue_size; 102ad794e6dSKonstantin Belousov uint32_t inv_queue_avail; 103ad794e6dSKonstantin Belousov uint32_t inv_queue_tail; 104*fc8da73bSKonstantin Belousov 105*fc8da73bSKonstantin Belousov /* 106*fc8da73bSKonstantin Belousov * Hw writes there on completion of wait descriptor 107*fc8da73bSKonstantin Belousov * processing. Intel writes 4 bytes, while AMD does the 108*fc8da73bSKonstantin Belousov * 8-bytes write. Due to little-endian, and use of 4-byte 109*fc8da73bSKonstantin Belousov * sequence numbers, the difference does not matter for us. 110*fc8da73bSKonstantin Belousov */ 111*fc8da73bSKonstantin Belousov volatile uint64_t inv_waitd_seq_hw; 112ad794e6dSKonstantin Belousov 113ad794e6dSKonstantin Belousov uint64_t inv_waitd_seq_hw_phys; 114ad794e6dSKonstantin Belousov uint32_t inv_waitd_seq; /* next sequence number to use for wait descr */ 115ad794e6dSKonstantin Belousov u_int inv_waitd_gen; /* seq number generation AKA seq overflows */ 116ad794e6dSKonstantin Belousov u_int inv_seq_waiters; /* count of waiters for seq */ 117ad794e6dSKonstantin Belousov u_int inv_queue_full; /* informational counter */ 118ad794e6dSKonstantin Belousov 119ad794e6dSKonstantin Belousov /* 120ad794e6dSKonstantin Belousov * Delayed freeing of map entries queue processing: 121ad794e6dSKonstantin Belousov * 122ad794e6dSKonstantin Belousov * tlb_flush_head and tlb_flush_tail are used to implement a FIFO 123ad794e6dSKonstantin Belousov * queue that supports concurrent dequeues and enqueues. However, 124ad794e6dSKonstantin Belousov * there can only be a single dequeuer (accessing tlb_flush_head) and 125ad794e6dSKonstantin Belousov * a single enqueuer (accessing tlb_flush_tail) at a time. Since the 126ad794e6dSKonstantin Belousov * unit's qi_task is the only dequeuer, it can access tlb_flush_head 127ad794e6dSKonstantin Belousov * without any locking. In contrast, there may be multiple enqueuers, 128ad794e6dSKonstantin Belousov * so the enqueuers acquire the iommu unit lock to serialize their 129ad794e6dSKonstantin Belousov * accesses to tlb_flush_tail. 130ad794e6dSKonstantin Belousov * 131ad794e6dSKonstantin Belousov * In this FIFO queue implementation, the key to enabling concurrent 132ad794e6dSKonstantin Belousov * dequeues and enqueues is that the dequeuer never needs to access 133ad794e6dSKonstantin Belousov * tlb_flush_tail and the enqueuer never needs to access 134ad794e6dSKonstantin Belousov * tlb_flush_head. In particular, tlb_flush_head and tlb_flush_tail 135ad794e6dSKonstantin Belousov * are never NULL, so neither a dequeuer nor an enqueuer ever needs to 136ad794e6dSKonstantin Belousov * update both. Instead, tlb_flush_head always points to a "zombie" 137ad794e6dSKonstantin Belousov * struct, which previously held the last dequeued item. Thus, the 138ad794e6dSKonstantin Belousov * zombie's next field actually points to the struct holding the first 139ad794e6dSKonstantin Belousov * item in the queue. When an item is dequeued, the current zombie is 140ad794e6dSKonstantin Belousov * finally freed, and the struct that held the just dequeued item 141ad794e6dSKonstantin Belousov * becomes the new zombie. When the queue is empty, tlb_flush_tail 142ad794e6dSKonstantin Belousov * also points to the zombie. 143ad794e6dSKonstantin Belousov */ 144ad794e6dSKonstantin Belousov struct iommu_map_entry *tlb_flush_head; 145ad794e6dSKonstantin Belousov struct iommu_map_entry *tlb_flush_tail; 146ad794e6dSKonstantin Belousov struct task qi_task; 147ad794e6dSKonstantin Belousov struct taskqueue *qi_taskqueue; 148ad794e6dSKonstantin Belousov }; 149ad794e6dSKonstantin Belousov 150ad794e6dSKonstantin Belousov void iommu_qi_emit_wait_seq(struct iommu_unit *unit, struct iommu_qi_genseq * 151ad794e6dSKonstantin Belousov pseq, bool emit_wait); 152ad794e6dSKonstantin Belousov void iommu_qi_wait_for_seq(struct iommu_unit *unit, const struct 153ad794e6dSKonstantin Belousov iommu_qi_genseq *gseq, bool nowait); 154ad794e6dSKonstantin Belousov void iommu_qi_drain_tlb_flush(struct iommu_unit *unit); 155ad794e6dSKonstantin Belousov void iommu_qi_invalidate_locked(struct iommu_domain *domain, 156ad794e6dSKonstantin Belousov struct iommu_map_entry *entry, bool emit_wait); 157ad794e6dSKonstantin Belousov void iommu_qi_invalidate_sync(struct iommu_domain *domain, iommu_gaddr_t base, 158ad794e6dSKonstantin Belousov iommu_gaddr_t size, bool cansleep); 159ad794e6dSKonstantin Belousov void iommu_qi_common_init(struct iommu_unit *unit, task_fn_t taskfunc); 160ad794e6dSKonstantin Belousov void iommu_qi_common_fini(struct iommu_unit *unit, void (*disable_qi)( 161ad794e6dSKonstantin Belousov struct iommu_unit *)); 162ad794e6dSKonstantin Belousov 16340d951bcSKonstantin Belousov #endif 164