]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/iommu.h
Merge sendmail 8.16.1 to HEAD: See contrib/sendmail/RELEASE_NOTES for details
[FreeBSD/FreeBSD.git] / sys / sys / 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/tree.h>
39 #include <sys/types.h>
40
41 /* Host or physical memory address, after translation. */
42 typedef uint64_t iommu_haddr_t;
43 /* Guest or bus address, before translation. */
44 typedef uint64_t iommu_gaddr_t;
45
46 struct iommu_map_entry;
47 TAILQ_HEAD(iommu_map_entries_tailq, iommu_map_entry);
48
49 struct iommu_qi_genseq {
50         u_int gen;
51         uint32_t seq;
52 };
53
54 struct iommu_map_entry {
55         iommu_gaddr_t start;
56         iommu_gaddr_t end;
57         iommu_gaddr_t first;            /* Least start in subtree */
58         iommu_gaddr_t last;             /* Greatest end in subtree */
59         iommu_gaddr_t free_down;        /* Max free space below the
60                                            current R/B tree node */
61         u_int flags;
62         TAILQ_ENTRY(iommu_map_entry) dmamap_link; /* Link for dmamap entries */
63         RB_ENTRY(iommu_map_entry) rb_entry;      /* Links for domain entries */
64         TAILQ_ENTRY(iommu_map_entry) unroll_link; /* Link for unroll after
65                                                     dmamap_load failure */
66         struct iommu_domain *domain;
67         struct iommu_qi_genseq gseq;
68 };
69
70 #define IOMMU_MAP_ENTRY_PLACE   0x0001  /* Fake entry */
71 #define IOMMU_MAP_ENTRY_RMRR    0x0002  /* Permanent, not linked by
72                                            dmamap_link */
73 #define IOMMU_MAP_ENTRY_MAP     0x0004  /* Busdma created, linked by
74                                            dmamap_link */
75 #define IOMMU_MAP_ENTRY_UNMAPPED        0x0010  /* No backing pages */
76 #define IOMMU_MAP_ENTRY_QI_NF   0x0020  /* qi task, do not free entry */
77 #define IOMMU_MAP_ENTRY_READ    0x1000  /* Read permitted */
78 #define IOMMU_MAP_ENTRY_WRITE   0x2000  /* Write permitted */
79 #define IOMMU_MAP_ENTRY_SNOOP   0x4000  /* Snoop */
80 #define IOMMU_MAP_ENTRY_TM      0x8000  /* Transient */
81
82 struct iommu_unit {
83         struct mtx lock;
84         int unit;
85
86         int dma_enabled;
87
88         /* Busdma delayed map load */
89         struct task dmamap_load_task;
90         TAILQ_HEAD(, bus_dmamap_iommu) delayed_maps;
91         struct taskqueue *delayed_taskqueue;
92 };
93
94 /*
95  * Locking annotations:
96  * (u) - Protected by iommu unit lock
97  * (d) - Protected by domain lock
98  * (c) - Immutable after initialization
99  */
100
101 struct iommu_domain {
102         struct iommu_unit *iommu;       /* (c) */
103         struct mtx lock;                /* (c) */
104         struct task unload_task;        /* (c) */
105         struct iommu_map_entries_tailq unload_entries; /* (d) Entries to
106                                                          unload */
107 };
108
109 struct iommu_ctx {
110         struct iommu_domain *domain;    /* (c) */
111         struct bus_dma_tag_iommu *tag;  /* (c) Root tag */
112         u_long loads;                   /* atomic updates, for stat only */
113         u_long unloads;                 /* same */
114         u_int flags;                    /* (u) */
115 };
116
117 /* struct iommu_ctx flags */
118 #define IOMMU_CTX_FAULTED       0x0001  /* Fault was reported,
119                                            last_fault_rec is valid */
120 #define IOMMU_CTX_DISABLED      0x0002  /* Device is disabled, the
121                                            ephemeral reference is kept
122                                            to prevent context destruction */
123
124 #define IOMMU_LOCK(unit)                mtx_lock(&(unit)->lock)
125 #define IOMMU_UNLOCK(unit)              mtx_unlock(&(unit)->lock)
126 #define IOMMU_ASSERT_LOCKED(unit)       mtx_assert(&(unit)->lock, MA_OWNED)
127
128 #define IOMMU_DOMAIN_LOCK(dom)          mtx_lock(&(dom)->lock)
129 #define IOMMU_DOMAIN_UNLOCK(dom)        mtx_unlock(&(dom)->lock)
130 #define IOMMU_DOMAIN_ASSERT_LOCKED(dom) mtx_assert(&(dom)->lock, MA_OWNED)
131
132 void iommu_free_ctx(struct iommu_ctx *ctx);
133 void iommu_free_ctx_locked(struct iommu_unit *iommu, struct iommu_ctx *ctx);
134 struct iommu_ctx *iommu_get_ctx(struct iommu_unit *, device_t dev,
135     uint16_t rid, bool id_mapped, bool rmrr_init);
136 struct iommu_unit *iommu_find(device_t dev, bool verbose);
137 void iommu_domain_unload_entry(struct iommu_map_entry *entry, bool free);
138 void iommu_domain_unload(struct iommu_domain *domain,
139     struct iommu_map_entries_tailq *entries, bool cansleep);
140
141 struct iommu_ctx *iommu_instantiate_ctx(struct iommu_unit *iommu,
142     device_t dev, bool rmrr);
143 device_t iommu_get_requester(device_t dev, uint16_t *rid);
144 int iommu_init_busdma(struct iommu_unit *unit);
145 void iommu_fini_busdma(struct iommu_unit *unit);
146 struct iommu_map_entry *iommu_map_alloc_entry(struct iommu_domain *iodom,
147     u_int flags);
148 void iommu_map_free_entry(struct iommu_domain *, struct iommu_map_entry *);
149 int iommu_map(struct iommu_domain *iodom,
150     const struct bus_dma_tag_common *common, iommu_gaddr_t size, int offset,
151     u_int eflags, u_int flags, vm_page_t *ma, struct iommu_map_entry **res);
152 int iommu_map_region(struct iommu_domain *domain,
153     struct iommu_map_entry *entry, u_int eflags, u_int flags, vm_page_t *ma);
154
155 #endif /* !_SYS_IOMMU_H_ */