Loading...
Note: File does not exist in v4.6.
1/*
2 * Copyright © 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25#ifndef __I915_VMA_H__
26#define __I915_VMA_H__
27
28#include <linux/io-mapping.h>
29
30#include <drm/drm_mm.h>
31
32#include "i915_gem_gtt.h"
33#include "i915_gem_fence_reg.h"
34#include "i915_gem_object.h"
35
36#include "i915_request.h"
37
38enum i915_cache_level;
39
40/**
41 * A VMA represents a GEM BO that is bound into an address space. Therefore, a
42 * VMA's presence cannot be guaranteed before binding, or after unbinding the
43 * object into/from the address space.
44 *
45 * To make things as simple as possible (ie. no refcounting), a VMA's lifetime
46 * will always be <= an objects lifetime. So object refcounting should cover us.
47 */
48struct i915_vma {
49 struct drm_mm_node node;
50 struct drm_i915_gem_object *obj;
51 struct i915_address_space *vm;
52 struct drm_i915_fence_reg *fence;
53 struct reservation_object *resv; /** Alias of obj->resv */
54 struct sg_table *pages;
55 void __iomem *iomap;
56 u64 size;
57 u64 display_alignment;
58 struct i915_page_sizes page_sizes;
59
60 u32 fence_size;
61 u32 fence_alignment;
62
63 /**
64 * Count of the number of times this vma has been opened by different
65 * handles (but same file) for execbuf, i.e. the number of aliases
66 * that exist in the ctx->handle_vmas LUT for this vma.
67 */
68 unsigned int open_count;
69 unsigned long flags;
70 /**
71 * How many users have pinned this object in GTT space. The following
72 * users can each hold at most one reference: pwrite/pread, execbuffer
73 * (objects are not allowed multiple times for the same batchbuffer),
74 * and the framebuffer code. When switching/pageflipping, the
75 * framebuffer code has at most two buffers pinned per crtc.
76 *
77 * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
78 * bits with absolutely no headroom. So use 4 bits.
79 */
80#define I915_VMA_PIN_MASK 0xf
81#define I915_VMA_PIN_OVERFLOW BIT(5)
82
83 /** Flags and address space this VMA is bound to */
84#define I915_VMA_GLOBAL_BIND BIT(6)
85#define I915_VMA_LOCAL_BIND BIT(7)
86#define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND | I915_VMA_PIN_OVERFLOW)
87
88#define I915_VMA_GGTT BIT(8)
89#define I915_VMA_CAN_FENCE BIT(9)
90#define I915_VMA_CLOSED BIT(10)
91#define I915_VMA_USERFAULT_BIT 11
92#define I915_VMA_USERFAULT BIT(I915_VMA_USERFAULT_BIT)
93#define I915_VMA_GGTT_WRITE BIT(12)
94
95 unsigned int active;
96 struct i915_gem_active last_read[I915_NUM_ENGINES];
97 struct i915_gem_active last_fence;
98
99 /**
100 * Support different GGTT views into the same object.
101 * This means there can be multiple VMA mappings per object and per VM.
102 * i915_ggtt_view_type is used to distinguish between those entries.
103 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
104 * assumed in GEM functions which take no ggtt view parameter.
105 */
106 struct i915_ggtt_view ggtt_view;
107
108 /** This object's place on the active/inactive lists */
109 struct list_head vm_link;
110
111 struct list_head obj_link; /* Link in the object's VMA list */
112 struct rb_node obj_node;
113 struct hlist_node obj_hash;
114
115 /** This vma's place in the execbuf reservation list */
116 struct list_head exec_link;
117 struct list_head reloc_link;
118
119 /** This vma's place in the eviction list */
120 struct list_head evict_link;
121
122 /**
123 * Used for performing relocations during execbuffer insertion.
124 */
125 unsigned int *exec_flags;
126 struct hlist_node exec_node;
127 u32 exec_handle;
128};
129
130struct i915_vma *
131i915_vma_instance(struct drm_i915_gem_object *obj,
132 struct i915_address_space *vm,
133 const struct i915_ggtt_view *view);
134
135void i915_vma_unpin_and_release(struct i915_vma **p_vma);
136
137static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
138{
139 return vma->flags & I915_VMA_GGTT;
140}
141
142static inline bool i915_vma_has_ggtt_write(const struct i915_vma *vma)
143{
144 return vma->flags & I915_VMA_GGTT_WRITE;
145}
146
147static inline void i915_vma_set_ggtt_write(struct i915_vma *vma)
148{
149 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
150 vma->flags |= I915_VMA_GGTT_WRITE;
151}
152
153static inline void i915_vma_unset_ggtt_write(struct i915_vma *vma)
154{
155 vma->flags &= ~I915_VMA_GGTT_WRITE;
156}
157
158void i915_vma_flush_writes(struct i915_vma *vma);
159
160static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
161{
162 return vma->flags & I915_VMA_CAN_FENCE;
163}
164
165static inline bool i915_vma_is_closed(const struct i915_vma *vma)
166{
167 return vma->flags & I915_VMA_CLOSED;
168}
169
170static inline bool i915_vma_set_userfault(struct i915_vma *vma)
171{
172 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
173 return __test_and_set_bit(I915_VMA_USERFAULT_BIT, &vma->flags);
174}
175
176static inline void i915_vma_unset_userfault(struct i915_vma *vma)
177{
178 return __clear_bit(I915_VMA_USERFAULT_BIT, &vma->flags);
179}
180
181static inline bool i915_vma_has_userfault(const struct i915_vma *vma)
182{
183 return test_bit(I915_VMA_USERFAULT_BIT, &vma->flags);
184}
185
186static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
187{
188 return vma->active;
189}
190
191static inline bool i915_vma_is_active(const struct i915_vma *vma)
192{
193 return i915_vma_get_active(vma);
194}
195
196static inline void i915_vma_set_active(struct i915_vma *vma,
197 unsigned int engine)
198{
199 vma->active |= BIT(engine);
200}
201
202static inline void i915_vma_clear_active(struct i915_vma *vma,
203 unsigned int engine)
204{
205 vma->active &= ~BIT(engine);
206}
207
208static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
209 unsigned int engine)
210{
211 return vma->active & BIT(engine);
212}
213
214static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
215{
216 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
217 GEM_BUG_ON(!vma->node.allocated);
218 GEM_BUG_ON(upper_32_bits(vma->node.start));
219 GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
220 return lower_32_bits(vma->node.start);
221}
222
223static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
224{
225 i915_gem_object_get(vma->obj);
226 return vma;
227}
228
229static inline void i915_vma_put(struct i915_vma *vma)
230{
231 i915_gem_object_put(vma->obj);
232}
233
234static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
235{
236 return a - b;
237}
238
239static inline long
240i915_vma_compare(struct i915_vma *vma,
241 struct i915_address_space *vm,
242 const struct i915_ggtt_view *view)
243{
244 ptrdiff_t cmp;
245
246 GEM_BUG_ON(view && !i915_is_ggtt(vm));
247
248 cmp = ptrdiff(vma->vm, vm);
249 if (cmp)
250 return cmp;
251
252 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0);
253 cmp = vma->ggtt_view.type;
254 if (!view)
255 return cmp;
256
257 cmp -= view->type;
258 if (cmp)
259 return cmp;
260
261 /* ggtt_view.type also encodes its size so that we both distinguish
262 * different views using it as a "type" and also use a compact (no
263 * accessing of uninitialised padding bytes) memcmp without storing
264 * an extra parameter or adding more code.
265 *
266 * To ensure that the memcmp is valid for all branches of the union,
267 * even though the code looks like it is just comparing one branch,
268 * we assert above that all branches have the same address, and that
269 * each branch has a unique type/size.
270 */
271 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL);
272 BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED);
273 BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
274 offsetof(typeof(*view), partial));
275 return memcmp(&vma->ggtt_view.partial, &view->partial, view->type);
276}
277
278int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
279 u32 flags);
280bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
281bool i915_vma_misplaced(const struct i915_vma *vma,
282 u64 size, u64 alignment, u64 flags);
283void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
284void i915_vma_revoke_mmap(struct i915_vma *vma);
285int __must_check i915_vma_unbind(struct i915_vma *vma);
286void i915_vma_unlink_ctx(struct i915_vma *vma);
287void i915_vma_close(struct i915_vma *vma);
288
289int __i915_vma_do_pin(struct i915_vma *vma,
290 u64 size, u64 alignment, u64 flags);
291static inline int __must_check
292i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
293{
294 BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
295 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
296 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
297
298 /* Pin early to prevent the shrinker/eviction logic from destroying
299 * our vma as we insert and bind.
300 */
301 if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0)) {
302 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
303 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
304 return 0;
305 }
306
307 return __i915_vma_do_pin(vma, size, alignment, flags);
308}
309
310static inline int i915_vma_pin_count(const struct i915_vma *vma)
311{
312 return vma->flags & I915_VMA_PIN_MASK;
313}
314
315static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
316{
317 return i915_vma_pin_count(vma);
318}
319
320static inline void __i915_vma_pin(struct i915_vma *vma)
321{
322 vma->flags++;
323 GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
324}
325
326static inline void __i915_vma_unpin(struct i915_vma *vma)
327{
328 vma->flags--;
329}
330
331static inline void i915_vma_unpin(struct i915_vma *vma)
332{
333 GEM_BUG_ON(!i915_vma_is_pinned(vma));
334 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
335 __i915_vma_unpin(vma);
336}
337
338/**
339 * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
340 * @vma: VMA to iomap
341 *
342 * The passed in VMA has to be pinned in the global GTT mappable region.
343 * An extra pinning of the VMA is acquired for the return iomapping,
344 * the caller must call i915_vma_unpin_iomap to relinquish the pinning
345 * after the iomapping is no longer required.
346 *
347 * Callers must hold the struct_mutex.
348 *
349 * Returns a valid iomapped pointer or ERR_PTR.
350 */
351void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
352#define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
353
354/**
355 * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
356 * @vma: VMA to unpin
357 *
358 * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
359 *
360 * Callers must hold the struct_mutex. This function is only valid to be
361 * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
362 */
363void i915_vma_unpin_iomap(struct i915_vma *vma);
364
365static inline struct page *i915_vma_first_page(struct i915_vma *vma)
366{
367 GEM_BUG_ON(!vma->pages);
368 return sg_page(vma->pages->sgl);
369}
370
371/**
372 * i915_vma_pin_fence - pin fencing state
373 * @vma: vma to pin fencing for
374 *
375 * This pins the fencing state (whether tiled or untiled) to make sure the
376 * vma (and its object) is ready to be used as a scanout target. Fencing
377 * status must be synchronize first by calling i915_vma_get_fence():
378 *
379 * The resulting fence pin reference must be released again with
380 * i915_vma_unpin_fence().
381 *
382 * Returns:
383 *
384 * True if the vma has a fence, false otherwise.
385 */
386int i915_vma_pin_fence(struct i915_vma *vma);
387int __must_check i915_vma_put_fence(struct i915_vma *vma);
388
389static inline void __i915_vma_unpin_fence(struct i915_vma *vma)
390{
391 GEM_BUG_ON(vma->fence->pin_count <= 0);
392 vma->fence->pin_count--;
393}
394
395/**
396 * i915_vma_unpin_fence - unpin fencing state
397 * @vma: vma to unpin fencing for
398 *
399 * This releases the fence pin reference acquired through
400 * i915_vma_pin_fence. It will handle both objects with and without an
401 * attached fence correctly, callers do not need to distinguish this.
402 */
403static inline void
404i915_vma_unpin_fence(struct i915_vma *vma)
405{
406 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
407 if (vma->fence)
408 __i915_vma_unpin_fence(vma);
409}
410
411#define for_each_until(cond) if (cond) break; else
412
413/**
414 * for_each_ggtt_vma - Iterate over the GGTT VMA belonging to an object.
415 * @V: the #i915_vma iterator
416 * @OBJ: the #drm_i915_gem_object
417 *
418 * GGTT VMA are placed at the being of the object's vma_list, see
419 * vma_create(), so we can stop our walk as soon as we see a ppgtt VMA,
420 * or the list is empty ofc.
421 */
422#define for_each_ggtt_vma(V, OBJ) \
423 list_for_each_entry(V, &(OBJ)->vma_list, obj_link) \
424 for_each_until(!i915_vma_is_ggtt(V))
425
426#endif