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