]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iommu/iommu.h
o Make the _hw_iommu sysctl node non-static;
[FreeBSD/FreeBSD.git] / sys / dev / iommu / iommu.h
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 _SYS_IOMMU_H_
35 #define _SYS_IOMMU_H_
36
37 #include <sys/queue.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
40 #include <sys/tree.h>
41 #include <sys/types.h>
42
43 /* Host or physical memory address, after translation. */
44 typedef uint64_t iommu_haddr_t;
45 /* Guest or bus address, before translation. */
46 typedef uint64_t iommu_gaddr_t;
47
48 struct bus_dma_tag_common;
49 struct iommu_map_entry;
50 TAILQ_HEAD(iommu_map_entries_tailq, iommu_map_entry);
51
52 RB_HEAD(iommu_gas_entries_tree, iommu_map_entry);
53 RB_PROTOTYPE(iommu_gas_entries_tree, iommu_map_entry, rb_entry,
54     iommu_gas_cmp_entries);
55
56 struct iommu_qi_genseq {
57         u_int gen;
58         uint32_t seq;
59 };
60
61 struct iommu_map_entry {
62         iommu_gaddr_t start;
63         iommu_gaddr_t end;
64         iommu_gaddr_t first;            /* Least start in subtree */
65         iommu_gaddr_t last;             /* Greatest end in subtree */
66         iommu_gaddr_t free_down;        /* Max free space below the
67                                            current R/B tree node */
68         u_int flags;
69         TAILQ_ENTRY(iommu_map_entry) dmamap_link; /* Link for dmamap entries */
70         RB_ENTRY(iommu_map_entry) rb_entry;      /* Links for domain entries */
71         TAILQ_ENTRY(iommu_map_entry) unroll_link; /* Link for unroll after
72                                                     dmamap_load failure */
73         struct iommu_domain *domain;
74         struct iommu_qi_genseq gseq;
75 };
76
77 #define IOMMU_MAP_ENTRY_PLACE   0x0001  /* Fake entry */
78 #define IOMMU_MAP_ENTRY_RMRR    0x0002  /* Permanent, not linked by
79                                            dmamap_link */
80 #define IOMMU_MAP_ENTRY_MAP     0x0004  /* Busdma created, linked by
81                                            dmamap_link */
82 #define IOMMU_MAP_ENTRY_UNMAPPED        0x0010  /* No backing pages */
83 #define IOMMU_MAP_ENTRY_QI_NF   0x0020  /* qi task, do not free entry */
84 #define IOMMU_MAP_ENTRY_READ    0x1000  /* Read permitted */
85 #define IOMMU_MAP_ENTRY_WRITE   0x2000  /* Write permitted */
86 #define IOMMU_MAP_ENTRY_SNOOP   0x4000  /* Snoop */
87 #define IOMMU_MAP_ENTRY_TM      0x8000  /* Transient */
88
89 struct iommu_unit {
90         struct mtx lock;
91         int unit;
92
93         int dma_enabled;
94
95         /* Busdma delayed map load */
96         struct task dmamap_load_task;
97         TAILQ_HEAD(, bus_dmamap_iommu) delayed_maps;
98         struct taskqueue *delayed_taskqueue;
99 };
100
101 /*
102  * Locking annotations:
103  * (u) - Protected by iommu unit lock
104  * (d) - Protected by domain lock
105  * (c) - Immutable after initialization
106  */
107
108 struct iommu_domain {
109         struct iommu_unit *iommu;       /* (c) */
110         struct mtx lock;                /* (c) */
111         struct task unload_task;        /* (c) */
112         u_int entries_cnt;              /* (d) */
113         struct iommu_map_entries_tailq unload_entries; /* (d) Entries to
114                                                          unload */
115         struct iommu_gas_entries_tree rb_root; /* (d) */
116         iommu_gaddr_t end;              /* (c) Highest address + 1 in
117                                            the guest AS */
118         struct iommu_map_entry *first_place, *last_place; /* (d) */
119         u_int flags;                    /* (u) */
120 };
121
122 struct iommu_ctx {
123         struct iommu_domain *domain;    /* (c) */
124         struct bus_dma_tag_iommu *tag;  /* (c) Root tag */
125         u_long loads;                   /* atomic updates, for stat only */
126         u_long unloads;                 /* same */
127         u_int flags;                    /* (u) */
128 };
129
130 /* struct iommu_ctx flags */
131 #define IOMMU_CTX_FAULTED       0x0001  /* Fault was reported,
132                                            last_fault_rec is valid */
133 #define IOMMU_CTX_DISABLED      0x0002  /* Device is disabled, the
134                                            ephemeral reference is kept
135                                            to prevent context destruction */
136
137 #define DMAR_DOMAIN_GAS_INITED          0x0001
138 #define DMAR_DOMAIN_PGTBL_INITED        0x0002
139 #define DMAR_DOMAIN_IDMAP               0x0010  /* Domain uses identity
140                                                    page table */
141 #define DMAR_DOMAIN_RMRR                0x0020  /* Domain contains RMRR entry,
142                                                    cannot be turned off */
143
144 /* Map flags */
145 #define IOMMU_MF_CANWAIT        0x0001
146 #define IOMMU_MF_CANSPLIT       0x0002
147 #define IOMMU_MF_RMRR           0x0004
148
149 #define DMAR_PGF_WAITOK         0x0001
150 #define DMAR_PGF_ZERO           0x0002
151 #define DMAR_PGF_ALLOC          0x0004
152 #define DMAR_PGF_NOALLOC        0x0008
153 #define DMAR_PGF_OBJL           0x0010
154
155 #define IOMMU_LOCK(unit)                mtx_lock(&(unit)->lock)
156 #define IOMMU_UNLOCK(unit)              mtx_unlock(&(unit)->lock)
157 #define IOMMU_ASSERT_LOCKED(unit)       mtx_assert(&(unit)->lock, MA_OWNED)
158
159 #define IOMMU_DOMAIN_LOCK(dom)          mtx_lock(&(dom)->lock)
160 #define IOMMU_DOMAIN_UNLOCK(dom)        mtx_unlock(&(dom)->lock)
161 #define IOMMU_DOMAIN_ASSERT_LOCKED(dom) mtx_assert(&(dom)->lock, MA_OWNED)
162
163 static inline bool
164 iommu_test_boundary(iommu_gaddr_t start, iommu_gaddr_t size,
165     iommu_gaddr_t boundary)
166 {
167
168         if (boundary == 0)
169                 return (true);
170         return (start + size <= ((start + boundary) & ~(boundary - 1)));
171 }
172
173 void iommu_free_ctx(struct iommu_ctx *ctx);
174 void iommu_free_ctx_locked(struct iommu_unit *iommu, struct iommu_ctx *ctx);
175 struct iommu_ctx *iommu_get_ctx(struct iommu_unit *, device_t dev,
176     uint16_t rid, bool id_mapped, bool rmrr_init);
177 struct iommu_unit *iommu_find(device_t dev, bool verbose);
178 void iommu_domain_unload_entry(struct iommu_map_entry *entry, bool free);
179 void iommu_domain_unload(struct iommu_domain *domain,
180     struct iommu_map_entries_tailq *entries, bool cansleep);
181
182 struct iommu_ctx *iommu_instantiate_ctx(struct iommu_unit *iommu,
183     device_t dev, bool rmrr);
184 device_t iommu_get_requester(device_t dev, uint16_t *rid);
185 int iommu_init_busdma(struct iommu_unit *unit);
186 void iommu_fini_busdma(struct iommu_unit *unit);
187 struct iommu_map_entry *iommu_map_alloc_entry(struct iommu_domain *iodom,
188     u_int flags);
189 void iommu_map_free_entry(struct iommu_domain *, struct iommu_map_entry *);
190 int iommu_map(struct iommu_domain *iodom,
191     const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset,
192     u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res);
193 int iommu_map_region(struct iommu_domain *domain,
194     struct iommu_map_entry *entry, u_int eflags, u_int flags, vm_page_t *ma);
195
196 void iommu_gas_init_domain(struct iommu_domain *domain);
197 void iommu_gas_fini_domain(struct iommu_domain *domain);
198 struct iommu_map_entry *iommu_gas_alloc_entry(struct iommu_domain *domain,
199     u_int flags);
200 void iommu_gas_free_entry(struct iommu_domain *domain,
201     struct iommu_map_entry *entry);
202 void iommu_gas_free_space(struct iommu_domain *domain,
203     struct iommu_map_entry *entry);
204 int iommu_gas_map(struct iommu_domain *domain,
205     const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset,
206     u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res);
207 void iommu_gas_free_region(struct iommu_domain *domain,
208     struct iommu_map_entry *entry);
209 int iommu_gas_map_region(struct iommu_domain *domain,
210     struct iommu_map_entry *entry, u_int eflags, u_int flags, vm_page_t *ma);
211 int iommu_gas_reserve_region(struct iommu_domain *domain, iommu_gaddr_t start,
212     iommu_gaddr_t end);
213
214 SYSCTL_DECL(_hw_iommu);
215
216 #endif /* !_SYS_IOMMU_H_ */