]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/i915_vma.h
drm/i915: Move i915_ppgtt_close() into i915_gem_gtt.c
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / i915_vma.h
CommitLineData
b42fe9ca
JL
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#include "i915_gem_request.h"
36
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 sg_table *pages;
54 void __iomem *iomap;
55 u64 size;
56 u64 display_alignment;
57
944397f0
CW
58 u32 fence_size;
59 u32 fence_alignment;
60
b42fe9ca
JL
61 unsigned int flags;
62 /**
63 * How many users have pinned this object in GTT space. The following
64 * users can each hold at most one reference: pwrite/pread, execbuffer
65 * (objects are not allowed multiple times for the same batchbuffer),
66 * and the framebuffer code. When switching/pageflipping, the
67 * framebuffer code has at most two buffers pinned per crtc.
68 *
69 * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
70 * bits with absolutely no headroom. So use 4 bits.
71 */
72#define I915_VMA_PIN_MASK 0xf
73#define I915_VMA_PIN_OVERFLOW BIT(5)
74
75 /** Flags and address space this VMA is bound to */
76#define I915_VMA_GLOBAL_BIND BIT(6)
77#define I915_VMA_LOCAL_BIND BIT(7)
78#define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND | I915_VMA_PIN_OVERFLOW)
79
80#define I915_VMA_GGTT BIT(8)
81#define I915_VMA_CAN_FENCE BIT(9)
82#define I915_VMA_CLOSED BIT(10)
83
84 unsigned int active;
85 struct i915_gem_active last_read[I915_NUM_ENGINES];
b42fe9ca
JL
86 struct i915_gem_active last_fence;
87
88 /**
89 * Support different GGTT views into the same object.
90 * This means there can be multiple VMA mappings per object and per VM.
91 * i915_ggtt_view_type is used to distinguish between those entries.
92 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
93 * assumed in GEM functions which take no ggtt view parameter.
94 */
95 struct i915_ggtt_view ggtt_view;
96
97 /** This object's place on the active/inactive lists */
98 struct list_head vm_link;
99
100 struct list_head obj_link; /* Link in the object's VMA list */
101 struct rb_node obj_node;
102
103 /** This vma's place in the batchbuffer or on the eviction list */
104 struct list_head exec_list;
105
106 /**
107 * Used for performing relocations during execbuffer insertion.
108 */
109 struct hlist_node exec_node;
110 unsigned long exec_handle;
111 struct drm_i915_gem_exec_object2 *exec_entry;
112};
113
114struct i915_vma *
115i915_vma_create(struct drm_i915_gem_object *obj,
116 struct i915_address_space *vm,
117 const struct i915_ggtt_view *view);
118
119void i915_vma_unpin_and_release(struct i915_vma **p_vma);
120
121static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
122{
123 return vma->flags & I915_VMA_GGTT;
124}
125
126static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
127{
128 return vma->flags & I915_VMA_CAN_FENCE;
129}
130
131static inline bool i915_vma_is_closed(const struct i915_vma *vma)
132{
133 return vma->flags & I915_VMA_CLOSED;
134}
135
136static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
137{
138 return vma->active;
139}
140
141static inline bool i915_vma_is_active(const struct i915_vma *vma)
142{
143 return i915_vma_get_active(vma);
144}
145
146static inline void i915_vma_set_active(struct i915_vma *vma,
147 unsigned int engine)
148{
149 vma->active |= BIT(engine);
150}
151
152static inline void i915_vma_clear_active(struct i915_vma *vma,
153 unsigned int engine)
154{
155 vma->active &= ~BIT(engine);
156}
157
158static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
159 unsigned int engine)
160{
161 return vma->active & BIT(engine);
162}
163
164static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
165{
166 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
167 GEM_BUG_ON(!vma->node.allocated);
168 GEM_BUG_ON(upper_32_bits(vma->node.start));
169 GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
170 return lower_32_bits(vma->node.start);
171}
172
173static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
174{
175 i915_gem_object_get(vma->obj);
176 return vma;
177}
178
179static inline void i915_vma_put(struct i915_vma *vma)
180{
181 i915_gem_object_put(vma->obj);
182}
183
2bf0d267
TU
184static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
185{
186 return a - b;
187}
188
b42fe9ca
JL
189static inline long
190i915_vma_compare(struct i915_vma *vma,
191 struct i915_address_space *vm,
192 const struct i915_ggtt_view *view)
193{
2bf0d267
TU
194 ptrdiff_t cmp;
195
4d1368f7 196 GEM_BUG_ON(view && !i915_is_ggtt(vm));
b42fe9ca 197
2bf0d267
TU
198 cmp = ptrdiff(vma->vm, vm);
199 if (cmp)
200 return cmp;
b42fe9ca
JL
201
202 if (!view)
203 return vma->ggtt_view.type;
204
205 if (vma->ggtt_view.type != view->type)
206 return vma->ggtt_view.type - view->type;
207
208 return memcmp(&vma->ggtt_view.params,
209 &view->params,
210 sizeof(view->params));
211}
212
213int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
214 u32 flags);
215bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
216bool
217i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags);
218void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
219int __must_check i915_vma_unbind(struct i915_vma *vma);
220void i915_vma_close(struct i915_vma *vma);
221void i915_vma_destroy(struct i915_vma *vma);
222
223int __i915_vma_do_pin(struct i915_vma *vma,
224 u64 size, u64 alignment, u64 flags);
225static inline int __must_check
226i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
227{
228 BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
229 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
230 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
231
232 /* Pin early to prevent the shrinker/eviction logic from destroying
233 * our vma as we insert and bind.
234 */
235 if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0))
236 return 0;
237
238 return __i915_vma_do_pin(vma, size, alignment, flags);
239}
240
241static inline int i915_vma_pin_count(const struct i915_vma *vma)
242{
243 return vma->flags & I915_VMA_PIN_MASK;
244}
245
246static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
247{
248 return i915_vma_pin_count(vma);
249}
250
251static inline void __i915_vma_pin(struct i915_vma *vma)
252{
253 vma->flags++;
254 GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
255}
256
257static inline void __i915_vma_unpin(struct i915_vma *vma)
258{
259 GEM_BUG_ON(!i915_vma_is_pinned(vma));
260 vma->flags--;
261}
262
263static inline void i915_vma_unpin(struct i915_vma *vma)
264{
265 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
266 __i915_vma_unpin(vma);
267}
268
269/**
270 * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
271 * @vma: VMA to iomap
272 *
273 * The passed in VMA has to be pinned in the global GTT mappable region.
274 * An extra pinning of the VMA is acquired for the return iomapping,
275 * the caller must call i915_vma_unpin_iomap to relinquish the pinning
276 * after the iomapping is no longer required.
277 *
278 * Callers must hold the struct_mutex.
279 *
280 * Returns a valid iomapped pointer or ERR_PTR.
281 */
282void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
283#define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
284
285/**
286 * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
287 * @vma: VMA to unpin
288 *
289 * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
290 *
291 * Callers must hold the struct_mutex. This function is only valid to be
292 * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
293 */
294static inline void i915_vma_unpin_iomap(struct i915_vma *vma)
295{
49d73912 296 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
b42fe9ca
JL
297 GEM_BUG_ON(vma->iomap == NULL);
298 i915_vma_unpin(vma);
299}
300
301static inline struct page *i915_vma_first_page(struct i915_vma *vma)
302{
303 GEM_BUG_ON(!vma->pages);
304 return sg_page(vma->pages->sgl);
305}
306
307/**
308 * i915_vma_pin_fence - pin fencing state
309 * @vma: vma to pin fencing for
310 *
311 * This pins the fencing state (whether tiled or untiled) to make sure the
312 * vma (and its object) is ready to be used as a scanout target. Fencing
313 * status must be synchronize first by calling i915_vma_get_fence():
314 *
315 * The resulting fence pin reference must be released again with
316 * i915_vma_unpin_fence().
317 *
318 * Returns:
319 *
320 * True if the vma has a fence, false otherwise.
321 */
322static inline bool
323i915_vma_pin_fence(struct i915_vma *vma)
324{
49d73912 325 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
b42fe9ca
JL
326 if (vma->fence) {
327 vma->fence->pin_count++;
328 return true;
329 } else
330 return false;
331}
332
333/**
334 * i915_vma_unpin_fence - unpin fencing state
335 * @vma: vma to unpin fencing for
336 *
337 * This releases the fence pin reference acquired through
338 * i915_vma_pin_fence. It will handle both objects with and without an
339 * attached fence correctly, callers do not need to distinguish this.
340 */
341static inline void
342i915_vma_unpin_fence(struct i915_vma *vma)
343{
49d73912 344 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
b42fe9ca
JL
345 if (vma->fence) {
346 GEM_BUG_ON(vma->fence->pin_count <= 0);
347 vma->fence->pin_count--;
348 }
349}
350
351#endif
352