1 /* $NetBSD: i915_vma_types.h,v 1.4 2021/12/19 11:33:30 riastradh Exp $ */
2
3 /* SPDX-License-Identifier: MIT */
4 /*
5 * Copyright © 2016 Intel Corporation
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * IN THE SOFTWARE.
25 *
26 */
27
28 #ifndef __I915_VMA_TYPES_H__
29 #define __I915_VMA_TYPES_H__
30
31 #include <linux/rbtree.h>
32
33 #include <drm/drm_mm.h>
34
35 #include "gem/i915_gem_object_types.h"
36
37 enum i915_cache_level;
38
39 /**
40 * DOC: Global GTT views
41 *
42 * Background and previous state
43 *
44 * Historically objects could exists (be bound) in global GTT space only as
45 * singular instances with a view representing all of the object's backing pages
46 * in a linear fashion. This view will be called a normal view.
47 *
48 * To support multiple views of the same object, where the number of mapped
49 * pages is not equal to the backing store, or where the layout of the pages
50 * is not linear, concept of a GGTT view was added.
51 *
52 * One example of an alternative view is a stereo display driven by a single
53 * image. In this case we would have a framebuffer looking like this
54 * (2x2 pages):
55 *
56 * 12
57 * 34
58 *
59 * Above would represent a normal GGTT view as normally mapped for GPU or CPU
60 * rendering. In contrast, fed to the display engine would be an alternative
61 * view which could look something like this:
62 *
63 * 1212
64 * 3434
65 *
66 * In this example both the size and layout of pages in the alternative view is
67 * different from the normal view.
68 *
69 * Implementation and usage
70 *
71 * GGTT views are implemented using VMAs and are distinguished via enum
72 * i915_ggtt_view_type and struct i915_ggtt_view.
73 *
74 * A new flavour of core GEM functions which work with GGTT bound objects were
75 * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
76 * renaming in large amounts of code. They take the struct i915_ggtt_view
77 * parameter encapsulating all metadata required to implement a view.
78 *
79 * As a helper for callers which are only interested in the normal view,
80 * globally const i915_ggtt_view_normal singleton instance exists. All old core
81 * GEM API functions, the ones not taking the view parameter, are operating on,
82 * or with the normal GGTT view.
83 *
84 * Code wanting to add or use a new GGTT view needs to:
85 *
86 * 1. Add a new enum with a suitable name.
87 * 2. Extend the metadata in the i915_ggtt_view structure if required.
88 * 3. Add support to i915_get_vma_pages().
89 *
90 * New views are required to build a scatter-gather table from within the
91 * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
92 * exists for the lifetime of an VMA.
93 *
94 * Core API is designed to have copy semantics which means that passed in
95 * struct i915_ggtt_view does not need to be persistent (left around after
96 * calling the core API functions).
97 *
98 */
99
100 struct intel_remapped_plane_info {
101 /* in gtt pages */
102 unsigned int width, height, stride, offset;
103 } __packed;
104
105 struct intel_remapped_info {
106 struct intel_remapped_plane_info plane[2];
107 unsigned int unused_mbz;
108 } __packed;
109
110 struct intel_rotation_info {
111 struct intel_remapped_plane_info plane[2];
112 } __packed;
113
114 struct intel_partial_info {
115 u64 offset;
116 unsigned int size;
117 } __packed;
118
119 enum i915_ggtt_view_type {
120 I915_GGTT_VIEW_NORMAL = 0,
121 I915_GGTT_VIEW_ROTATED = sizeof(struct intel_rotation_info),
122 I915_GGTT_VIEW_PARTIAL = sizeof(struct intel_partial_info),
123 I915_GGTT_VIEW_REMAPPED = sizeof(struct intel_remapped_info),
124 };
125
assert_i915_gem_gtt_types(void)126 static inline void assert_i915_gem_gtt_types(void)
127 {
128 BUILD_BUG_ON(sizeof(struct intel_rotation_info) != 8*sizeof(unsigned int));
129 BUILD_BUG_ON(sizeof(struct intel_partial_info) != sizeof(u64) + sizeof(unsigned int));
130 BUILD_BUG_ON(sizeof(struct intel_remapped_info) != 9*sizeof(unsigned int));
131
132 /* Check that rotation/remapped shares offsets for simplicity */
133 BUILD_BUG_ON(offsetof(struct intel_remapped_info, plane[0]) !=
134 offsetof(struct intel_rotation_info, plane[0]));
135 BUILD_BUG_ON(offsetofend(struct intel_remapped_info, plane[1]) !=
136 offsetofend(struct intel_rotation_info, plane[1]));
137
138 /* As we encode the size of each branch inside the union into its type,
139 * we have to be careful that each branch has a unique size.
140 */
141 switch ((enum i915_ggtt_view_type)0) {
142 case I915_GGTT_VIEW_NORMAL:
143 case I915_GGTT_VIEW_PARTIAL:
144 case I915_GGTT_VIEW_ROTATED:
145 case I915_GGTT_VIEW_REMAPPED:
146 /* gcc complains if these are identical cases */
147 break;
148 }
149 }
150
151 struct i915_ggtt_view {
152 enum i915_ggtt_view_type type;
153 union {
154 /* Members need to contain no holes/padding */
155 struct intel_partial_info partial;
156 struct intel_rotation_info rotated;
157 struct intel_remapped_info remapped;
158 };
159 };
160
161
162 #ifdef __NetBSD__
163 # define __i915_vma_iomem /* write-combining */
164 # define __iomem __i915_vma_iomem
165 #endif
166
167 /**
168 * DOC: Virtual Memory Address
169 *
170 * A VMA represents a GEM BO that is bound into an address space. Therefore, a
171 * VMA's presence cannot be guaranteed before binding, or after unbinding the
172 * object into/from the address space.
173 *
174 * To make things as simple as possible (ie. no refcounting), a VMA's lifetime
175 * will always be <= an objects lifetime. So object refcounting should cover us.
176 */
177 struct i915_vma {
178 struct drm_mm_node node;
179
180 struct i915_address_space *vm;
181 const struct i915_vma_ops *ops;
182
183 struct drm_i915_gem_object *obj;
184 struct dma_resv *resv; /** Alias of obj->resv */
185
186 struct sg_table *pages;
187 void __iomem *iomap;
188 void *private; /* owned by creator */
189
190 struct i915_fence_reg *fence;
191
192 u64 size;
193 u64 display_alignment;
194 struct i915_page_sizes page_sizes;
195
196 /* mmap-offset associated with fencing for this vma */
197 struct i915_mmap_offset *mmo;
198
199 u32 fence_size;
200 u32 fence_alignment;
201
202 /**
203 * Count of the number of times this vma has been opened by different
204 * handles (but same file) for execbuf, i.e. the number of aliases
205 * that exist in the ctx->handle_vmas LUT for this vma.
206 */
207 struct kref ref;
208 atomic_t open_count;
209 atomic_t flags;
210 /**
211 * How many users have pinned this object in GTT space.
212 *
213 * This is a tightly bound, fairly small number of users, so we
214 * stuff inside the flags field so that we can both check for overflow
215 * and detect a no-op i915_vma_pin() in a single check, while also
216 * pinning the vma.
217 *
218 * The worst case display setup would have the same vma pinned for
219 * use on each plane on each crtc, while also building the next atomic
220 * state and holding a pin for the length of the cleanup queue. In the
221 * future, the flip queue may be increased from 1.
222 * Estimated worst case: 3 [qlen] * 4 [max crtcs] * 7 [max planes] = 84
223 *
224 * For GEM, the number of concurrent users for pwrite/pread is
225 * unbounded. For execbuffer, it is currently one but will in future
226 * be extended to allow multiple clients to pin vma concurrently.
227 *
228 * We also use suballocated pages, with each suballocation claiming
229 * its own pin on the shared vma. At present, this is limited to
230 * exclusive cachelines of a single page, so a maximum of 64 possible
231 * users.
232 */
233 #define I915_VMA_PIN_MASK 0x3ff
234 #define I915_VMA_OVERFLOW 0x200
235
236 /** Flags and address space this VMA is bound to */
237 #define I915_VMA_GLOBAL_BIND_BIT 10
238 #define I915_VMA_LOCAL_BIND_BIT 11
239
240 #define I915_VMA_GLOBAL_BIND ((int)BIT(I915_VMA_GLOBAL_BIND_BIT))
241 #define I915_VMA_LOCAL_BIND ((int)BIT(I915_VMA_LOCAL_BIND_BIT))
242
243 #define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND)
244
245 #define I915_VMA_ALLOC_BIT 12
246 #define I915_VMA_ALLOC ((int)BIT(I915_VMA_ALLOC_BIT))
247
248 #define I915_VMA_ERROR_BIT 13
249 #define I915_VMA_ERROR ((int)BIT(I915_VMA_ERROR_BIT))
250
251 #define I915_VMA_GGTT_BIT 14
252 #define I915_VMA_CAN_FENCE_BIT 15
253 #define I915_VMA_USERFAULT_BIT 16
254 #define I915_VMA_GGTT_WRITE_BIT 17
255
256 #define I915_VMA_GGTT ((int)BIT(I915_VMA_GGTT_BIT))
257 #define I915_VMA_CAN_FENCE ((int)BIT(I915_VMA_CAN_FENCE_BIT))
258 #define I915_VMA_USERFAULT ((int)BIT(I915_VMA_USERFAULT_BIT))
259 #define I915_VMA_GGTT_WRITE ((int)BIT(I915_VMA_GGTT_WRITE_BIT))
260
261 struct i915_active active;
262
263 #define I915_VMA_PAGES_BIAS 24
264 #define I915_VMA_PAGES_ACTIVE (BIT(24) | 1)
265 atomic_t pages_count; /* number of active binds to the pages */
266 struct mutex pages_mutex; /* protect acquire/release of backing pages */
267
268 /**
269 * Support different GGTT views into the same object.
270 * This means there can be multiple VMA mappings per object and per VM.
271 * i915_ggtt_view_type is used to distinguish between those entries.
272 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
273 * assumed in GEM functions which take no ggtt view parameter.
274 */
275 struct i915_ggtt_view ggtt_view;
276
277 /** This object's place on the active/inactive lists */
278 struct list_head vm_link;
279
280 struct list_head obj_link; /* Link in the object's VMA list */
281 struct rb_node obj_node;
282 struct hlist_node obj_hash;
283
284 /** This vma's place in the execbuf reservation list */
285 struct list_head exec_link;
286 struct list_head reloc_link;
287
288 /** This vma's place in the eviction list */
289 struct list_head evict_link;
290
291 struct list_head closed_link;
292
293 /**
294 * Used for performing relocations during execbuffer insertion.
295 */
296 unsigned int *exec_flags;
297 struct hlist_node exec_node;
298 u32 exec_handle;
299 };
300
301 #ifdef __NetBSD__
302 # undef __iomem
303 #endif
304
305 #endif
306
307