1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
42
43
44
45
46
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
58 u32 fence_size;
59 u32 fence_alignment;
60
61 unsigned int flags;
62
63
64
65
66
67
68
69
70
71
72#define I915_VMA_PIN_MASK 0xf
73#define I915_VMA_PIN_OVERFLOW BIT(5)
74
75
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];
86 struct i915_gem_active last_fence;
87
88
89
90
91
92
93
94
95 struct i915_ggtt_view ggtt_view;
96
97
98 struct list_head vm_link;
99
100 struct list_head obj_link;
101 struct rb_node obj_node;
102
103
104 struct list_head exec_list;
105
106
107
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_instance(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
184static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
185{
186 return a - b;
187}
188
189static inline long
190i915_vma_compare(struct i915_vma *vma,
191 struct i915_address_space *vm,
192 const struct i915_ggtt_view *view)
193{
194 ptrdiff_t cmp;
195
196 GEM_BUG_ON(view && !i915_is_ggtt(vm));
197
198 cmp = ptrdiff(vma->vm, vm);
199 if (cmp)
200 return cmp;
201
202 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0);
203 cmp = vma->ggtt_view.type;
204 if (!view)
205 return cmp;
206
207 cmp -= view->type;
208 if (cmp)
209 return cmp;
210
211
212
213
214
215
216
217
218
219
220
221 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL);
222 BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED);
223 BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
224 offsetof(typeof(*view), partial));
225 return memcmp(&vma->ggtt_view.partial, &view->partial, view->type);
226}
227
228int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
229 u32 flags);
230bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
231bool
232i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags);
233void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
234int __must_check i915_vma_unbind(struct i915_vma *vma);
235void i915_vma_close(struct i915_vma *vma);
236void i915_vma_destroy(struct i915_vma *vma);
237
238int __i915_vma_do_pin(struct i915_vma *vma,
239 u64 size, u64 alignment, u64 flags);
240static inline int __must_check
241i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
242{
243 BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
244 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
245 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
246
247
248
249
250 if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0)) {
251 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
252 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
253 return 0;
254 }
255
256 return __i915_vma_do_pin(vma, size, alignment, flags);
257}
258
259static inline int i915_vma_pin_count(const struct i915_vma *vma)
260{
261 return vma->flags & I915_VMA_PIN_MASK;
262}
263
264static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
265{
266 return i915_vma_pin_count(vma);
267}
268
269static inline void __i915_vma_pin(struct i915_vma *vma)
270{
271 vma->flags++;
272 GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
273}
274
275static inline void __i915_vma_unpin(struct i915_vma *vma)
276{
277 GEM_BUG_ON(!i915_vma_is_pinned(vma));
278 vma->flags--;
279}
280
281static inline void i915_vma_unpin(struct i915_vma *vma)
282{
283 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
284 __i915_vma_unpin(vma);
285}
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
301#define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
302
303
304
305
306
307
308
309
310
311
312static inline void i915_vma_unpin_iomap(struct i915_vma *vma)
313{
314 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
315 GEM_BUG_ON(vma->iomap == NULL);
316 i915_vma_unpin(vma);
317}
318
319static inline struct page *i915_vma_first_page(struct i915_vma *vma)
320{
321 GEM_BUG_ON(!vma->pages);
322 return sg_page(vma->pages->sgl);
323}
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340static inline bool
341i915_vma_pin_fence(struct i915_vma *vma)
342{
343 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
344 if (vma->fence) {
345 vma->fence->pin_count++;
346 return true;
347 } else
348 return false;
349}
350
351
352
353
354
355
356
357
358
359static inline void
360i915_vma_unpin_fence(struct i915_vma *vma)
361{
362 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
363 if (vma->fence) {
364 GEM_BUG_ON(vma->fence->pin_count <= 0);
365 vma->fence->pin_count--;
366 }
367}
368
369#endif
370
371