xref: /freebsd/sys/x86/iommu/x86_iommu.h (revision d97838b7c2a605932b6edf36f87abe7ccce74314)
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);
51f713ed66SKonstantin Belousov void iommu_pgfree(struct vm_object *obj, vm_pindex_t idx, int flags,
52f713ed66SKonstantin Belousov     struct iommu_map_entry *entry);
5340d951bcSKonstantin Belousov void *iommu_map_pgtbl(struct vm_object *obj, vm_pindex_t idx, int flags,
5440d951bcSKonstantin Belousov     struct sf_buf **sf);
5540d951bcSKonstantin Belousov void iommu_unmap_pgtbl(struct sf_buf *sf);
5640d951bcSKonstantin Belousov 
5740d951bcSKonstantin Belousov extern iommu_haddr_t iommu_high;
5840d951bcSKonstantin Belousov extern int iommu_tbl_pagecnt;
590386b245SKonstantin Belousov extern int iommu_qi_batch_coalesce;
6040d951bcSKonstantin Belousov 
6140d951bcSKonstantin Belousov SYSCTL_DECL(_hw_iommu);
6240d951bcSKonstantin Belousov 
63ad794e6dSKonstantin Belousov struct x86_unit_common;
64ad794e6dSKonstantin Belousov 
6565b133e5SKonstantin Belousov struct x86_iommu {
66ad794e6dSKonstantin Belousov 	struct x86_unit_common *(*get_x86_common)(struct
67ad794e6dSKonstantin Belousov 	    iommu_unit *iommu);
68ba33e74cSKonstantin Belousov 	void (*unit_pre_instantiate_ctx)(struct iommu_unit *iommu);
69ad794e6dSKonstantin Belousov 	void (*qi_ensure)(struct iommu_unit *unit, int descr_count);
70ad794e6dSKonstantin Belousov 	void (*qi_emit_wait_descr)(struct iommu_unit *unit, uint32_t seq,
71ad794e6dSKonstantin Belousov 	    bool, bool, bool);
72ad794e6dSKonstantin Belousov 	void (*qi_advance_tail)(struct iommu_unit *unit);
73ad794e6dSKonstantin Belousov 	void (*qi_invalidate_emit)(struct iommu_domain *idomain,
74ad794e6dSKonstantin Belousov 	    iommu_gaddr_t base, iommu_gaddr_t size, struct iommu_qi_genseq *
75ad794e6dSKonstantin Belousov 	    pseq, bool emit_wait);
7665b133e5SKonstantin Belousov 	void (*domain_unload_entry)(struct iommu_map_entry *entry, bool free,
7765b133e5SKonstantin Belousov 	    bool cansleep);
7865b133e5SKonstantin Belousov 	void (*domain_unload)(struct iommu_domain *iodom,
7965b133e5SKonstantin Belousov 		struct iommu_map_entries_tailq *entries, bool cansleep);
8065b133e5SKonstantin Belousov 	struct iommu_ctx *(*get_ctx)(struct iommu_unit *iommu,
8165b133e5SKonstantin Belousov 	    device_t dev, uint16_t rid, bool id_mapped, bool rmrr_init);
8265b133e5SKonstantin Belousov 	void (*free_ctx_locked)(struct iommu_unit *iommu,
8365b133e5SKonstantin Belousov 	    struct iommu_ctx *context);
8465b133e5SKonstantin Belousov 	struct iommu_unit *(*find)(device_t dev, bool verbose);
8565b133e5SKonstantin Belousov 	int (*alloc_msi_intr)(device_t src, u_int *cookies, u_int count);
8665b133e5SKonstantin Belousov 	int (*map_msi_intr)(device_t src, u_int cpu, u_int vector,
8765b133e5SKonstantin Belousov 	    u_int cookie, uint64_t *addr, uint32_t *data);
8865b133e5SKonstantin Belousov 	int (*unmap_msi_intr)(device_t src, u_int cookie);
8965b133e5SKonstantin Belousov 	int (*map_ioapic_intr)(u_int ioapic_id, u_int cpu, u_int vector,
9065b133e5SKonstantin Belousov 	    bool edge, bool activehi, int irq, u_int *cookie, uint32_t *hi,
9165b133e5SKonstantin Belousov 	    uint32_t *lo);
9265b133e5SKonstantin Belousov 	int (*unmap_ioapic_intr)(u_int ioapic_id, u_int *cookie);
9365b133e5SKonstantin Belousov };
9465b133e5SKonstantin Belousov void set_x86_iommu(struct x86_iommu *);
9565b133e5SKonstantin Belousov struct x86_iommu *get_x86_iommu(void);
9665b133e5SKonstantin Belousov 
975967352aSKonstantin Belousov struct iommu_msi_data {
985967352aSKonstantin Belousov 	int irq;
995967352aSKonstantin Belousov 	int irq_rid;
1005967352aSKonstantin Belousov 	struct resource *irq_res;
1015967352aSKonstantin Belousov 	void *intr_handle;
1025967352aSKonstantin Belousov 	int (*handler)(void *);
1035967352aSKonstantin Belousov 	int msi_data_reg;
1045967352aSKonstantin Belousov 	int msi_addr_reg;
1055967352aSKonstantin Belousov 	int msi_uaddr_reg;
1065967352aSKonstantin Belousov 	uint64_t msi_addr;
1075967352aSKonstantin Belousov 	uint32_t msi_data;
1085967352aSKonstantin Belousov 	void (*enable_intr)(struct iommu_unit *);
1095967352aSKonstantin Belousov 	void (*disable_intr)(struct iommu_unit *);
1105967352aSKonstantin Belousov 	const char *name;
1115967352aSKonstantin Belousov };
1125967352aSKonstantin Belousov 
1135967352aSKonstantin Belousov #define	IOMMU_MAX_MSI	3
1145967352aSKonstantin Belousov 
115ad794e6dSKonstantin Belousov struct x86_unit_common {
116ad794e6dSKonstantin Belousov 	uint32_t qi_buf_maxsz;
117ad794e6dSKonstantin Belousov 	uint32_t qi_cmd_sz;
118ad794e6dSKonstantin Belousov 
119ad794e6dSKonstantin Belousov 	char *inv_queue;
120ad794e6dSKonstantin Belousov 	vm_size_t inv_queue_size;
121ad794e6dSKonstantin Belousov 	uint32_t inv_queue_avail;
122ad794e6dSKonstantin Belousov 	uint32_t inv_queue_tail;
123fc8da73bSKonstantin Belousov 
124fc8da73bSKonstantin Belousov 	/*
125fc8da73bSKonstantin Belousov 	 * Hw writes there on completion of wait descriptor
126fc8da73bSKonstantin Belousov 	 * processing.  Intel writes 4 bytes, while AMD does the
127fc8da73bSKonstantin Belousov 	 * 8-bytes write.  Due to little-endian, and use of 4-byte
128fc8da73bSKonstantin Belousov 	 * sequence numbers, the difference does not matter for us.
129fc8da73bSKonstantin Belousov 	 */
130fc8da73bSKonstantin Belousov 	volatile uint64_t inv_waitd_seq_hw;
131ad794e6dSKonstantin Belousov 
132ad794e6dSKonstantin Belousov 	uint64_t inv_waitd_seq_hw_phys;
133ad794e6dSKonstantin Belousov 	uint32_t inv_waitd_seq; /* next sequence number to use for wait descr */
134ad794e6dSKonstantin Belousov 	u_int inv_waitd_gen;	/* seq number generation AKA seq overflows */
135ad794e6dSKonstantin Belousov 	u_int inv_seq_waiters;	/* count of waiters for seq */
136ad794e6dSKonstantin Belousov 	u_int inv_queue_full;	/* informational counter */
137ad794e6dSKonstantin Belousov 
138ad794e6dSKonstantin Belousov 	/*
139ad794e6dSKonstantin Belousov 	 * Delayed freeing of map entries queue processing:
140ad794e6dSKonstantin Belousov 	 *
141ad794e6dSKonstantin Belousov 	 * tlb_flush_head and tlb_flush_tail are used to implement a FIFO
142ad794e6dSKonstantin Belousov 	 * queue that supports concurrent dequeues and enqueues.  However,
143ad794e6dSKonstantin Belousov 	 * there can only be a single dequeuer (accessing tlb_flush_head) and
144ad794e6dSKonstantin Belousov 	 * a single enqueuer (accessing tlb_flush_tail) at a time.  Since the
145ad794e6dSKonstantin Belousov 	 * unit's qi_task is the only dequeuer, it can access tlb_flush_head
146ad794e6dSKonstantin Belousov 	 * without any locking.  In contrast, there may be multiple enqueuers,
147ad794e6dSKonstantin Belousov 	 * so the enqueuers acquire the iommu unit lock to serialize their
148ad794e6dSKonstantin Belousov 	 * accesses to tlb_flush_tail.
149ad794e6dSKonstantin Belousov 	 *
150ad794e6dSKonstantin Belousov 	 * In this FIFO queue implementation, the key to enabling concurrent
151ad794e6dSKonstantin Belousov 	 * dequeues and enqueues is that the dequeuer never needs to access
152ad794e6dSKonstantin Belousov 	 * tlb_flush_tail and the enqueuer never needs to access
153ad794e6dSKonstantin Belousov 	 * tlb_flush_head.  In particular, tlb_flush_head and tlb_flush_tail
154ad794e6dSKonstantin Belousov 	 * are never NULL, so neither a dequeuer nor an enqueuer ever needs to
155ad794e6dSKonstantin Belousov 	 * update both.  Instead, tlb_flush_head always points to a "zombie"
156ad794e6dSKonstantin Belousov 	 * struct, which previously held the last dequeued item.  Thus, the
157ad794e6dSKonstantin Belousov 	 * zombie's next field actually points to the struct holding the first
158ad794e6dSKonstantin Belousov 	 * item in the queue.  When an item is dequeued, the current zombie is
159ad794e6dSKonstantin Belousov 	 * finally freed, and the struct that held the just dequeued item
160ad794e6dSKonstantin Belousov 	 * becomes the new zombie.  When the queue is empty, tlb_flush_tail
161ad794e6dSKonstantin Belousov 	 * also points to the zombie.
162ad794e6dSKonstantin Belousov 	 */
163ad794e6dSKonstantin Belousov 	struct iommu_map_entry *tlb_flush_head;
164ad794e6dSKonstantin Belousov 	struct iommu_map_entry *tlb_flush_tail;
165ad794e6dSKonstantin Belousov 	struct task qi_task;
166ad794e6dSKonstantin Belousov 	struct taskqueue *qi_taskqueue;
1675967352aSKonstantin Belousov 
1685967352aSKonstantin Belousov 	struct iommu_msi_data intrs[IOMMU_MAX_MSI];
169ad794e6dSKonstantin Belousov };
170ad794e6dSKonstantin Belousov 
171b3042e3aSKonstantin Belousov void iommu_domain_free_entry(struct iommu_map_entry *entry, bool free);
172b3042e3aSKonstantin Belousov 
173ad794e6dSKonstantin Belousov void iommu_qi_emit_wait_seq(struct iommu_unit *unit, struct iommu_qi_genseq *
174ad794e6dSKonstantin Belousov     pseq, bool emit_wait);
175ad794e6dSKonstantin Belousov void iommu_qi_wait_for_seq(struct iommu_unit *unit, const struct
176ad794e6dSKonstantin Belousov     iommu_qi_genseq *gseq, bool nowait);
177ad794e6dSKonstantin Belousov void iommu_qi_drain_tlb_flush(struct iommu_unit *unit);
178ad794e6dSKonstantin Belousov void iommu_qi_invalidate_locked(struct iommu_domain *domain,
179ad794e6dSKonstantin Belousov     struct iommu_map_entry *entry, bool emit_wait);
180ad794e6dSKonstantin Belousov void iommu_qi_invalidate_sync(struct iommu_domain *domain, iommu_gaddr_t base,
181ad794e6dSKonstantin Belousov     iommu_gaddr_t size, bool cansleep);
182ad794e6dSKonstantin Belousov void iommu_qi_common_init(struct iommu_unit *unit, task_fn_t taskfunc);
183ad794e6dSKonstantin Belousov void iommu_qi_common_fini(struct iommu_unit *unit, void (*disable_qi)(
184ad794e6dSKonstantin Belousov     struct iommu_unit *));
185ad794e6dSKonstantin Belousov 
1865967352aSKonstantin Belousov int iommu_alloc_irq(struct iommu_unit *unit, int idx);
1875967352aSKonstantin Belousov void iommu_release_intr(struct iommu_unit *unit, int idx);
1885967352aSKonstantin Belousov 
189ff54674bSKonstantin Belousov void iommu_device_tag_init(struct iommu_ctx *ctx, device_t dev);
190*b08d332dSKonstantin Belousov void iommu_device_set_iommu_prop(device_t dev, device_t iommu);
191ff54674bSKonstantin Belousov 
19229e22704SKonstantin Belousov int pglvl_pgtbl_pte_off(int pglvl, iommu_gaddr_t base, int lvl);
19329e22704SKonstantin Belousov vm_pindex_t pglvl_pgtbl_get_pindex(int pglvl, iommu_gaddr_t base, int lvl);
19429e22704SKonstantin Belousov vm_pindex_t pglvl_max_pages(int pglvl);
19529e22704SKonstantin Belousov iommu_gaddr_t pglvl_page_size(int total_pglvl, int lvl);
19629e22704SKonstantin Belousov 
197e9d948cfSKonstantin Belousov void iommu_db_print_domain_entry(const struct iommu_map_entry *entry);
198e9d948cfSKonstantin Belousov void iommu_db_print_ctx(struct iommu_ctx *ctx);
199c9e22c74SKonstantin Belousov void iommu_db_domain_print_contexts(struct iommu_domain *iodom);
200c9e22c74SKonstantin Belousov void iommu_db_domain_print_mappings(struct iommu_domain *iodom);
201e9d948cfSKonstantin Belousov 
20240d951bcSKonstantin Belousov #endif
203