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 unsigned int flags;
59
60
61
62
63
64
65
66
67
68
69#define I915_VMA_PIN_MASK 0xf
70#define I915_VMA_PIN_OVERFLOW BIT(5)
71
72
73#define I915_VMA_GLOBAL_BIND BIT(6)
74#define I915_VMA_LOCAL_BIND BIT(7)
75#define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND | I915_VMA_PIN_OVERFLOW)
76
77#define I915_VMA_GGTT BIT(8)
78#define I915_VMA_CAN_FENCE BIT(9)
79#define I915_VMA_CLOSED BIT(10)
80
81 unsigned int active;
82 struct i915_gem_active last_read[I915_NUM_ENGINES];
83 struct i915_gem_active last_fence;
84
85
86
87
88
89
90
91
92 struct i915_ggtt_view ggtt_view;
93
94
95 struct list_head vm_link;
96
97 struct list_head obj_link;
98 struct rb_node obj_node;
99
100
101 struct list_head exec_list;
102
103
104
105
106 struct hlist_node exec_node;
107 unsigned long exec_handle;
108 struct drm_i915_gem_exec_object2 *exec_entry;
109};
110
111struct i915_vma *
112i915_vma_create(struct drm_i915_gem_object *obj,
113 struct i915_address_space *vm,
114 const struct i915_ggtt_view *view);
115
116void i915_vma_unpin_and_release(struct i915_vma **p_vma);
117
118static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
119{
120 return vma->flags & I915_VMA_GGTT;
121}
122
123static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
124{
125 return vma->flags & I915_VMA_CAN_FENCE;
126}
127
128static inline bool i915_vma_is_closed(const struct i915_vma *vma)
129{
130 return vma->flags & I915_VMA_CLOSED;
131}
132
133static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
134{
135 return vma->active;
136}
137
138static inline bool i915_vma_is_active(const struct i915_vma *vma)
139{
140 return i915_vma_get_active(vma);
141}
142
143static inline void i915_vma_set_active(struct i915_vma *vma,
144 unsigned int engine)
145{
146 vma->active |= BIT(engine);
147}
148
149static inline void i915_vma_clear_active(struct i915_vma *vma,
150 unsigned int engine)
151{
152 vma->active &= ~BIT(engine);
153}
154
155static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
156 unsigned int engine)
157{
158 return vma->active & BIT(engine);
159}
160
161static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
162{
163 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
164 GEM_BUG_ON(!vma->node.allocated);
165 GEM_BUG_ON(upper_32_bits(vma->node.start));
166 GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
167 return lower_32_bits(vma->node.start);
168}
169
170static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
171{
172 i915_gem_object_get(vma->obj);
173 return vma;
174}
175
176static inline void i915_vma_put(struct i915_vma *vma)
177{
178 i915_gem_object_put(vma->obj);
179}
180
181static inline long
182i915_vma_compare(struct i915_vma *vma,
183 struct i915_address_space *vm,
184 const struct i915_ggtt_view *view)
185{
186 GEM_BUG_ON(view && !i915_is_ggtt(vm));
187
188 if (vma->vm != vm)
189 return vma->vm - vm;
190
191 if (!view)
192 return vma->ggtt_view.type;
193
194 if (vma->ggtt_view.type != view->type)
195 return vma->ggtt_view.type - view->type;
196
197 return memcmp(&vma->ggtt_view.params,
198 &view->params,
199 sizeof(view->params));
200}
201
202int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
203 u32 flags);
204bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
205bool
206i915_vma_misplaced(struct i915_vma *vma, u64 size, u64 alignment, u64 flags);
207void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
208int __must_check i915_vma_unbind(struct i915_vma *vma);
209void i915_vma_close(struct i915_vma *vma);
210void i915_vma_destroy(struct i915_vma *vma);
211
212int __i915_vma_do_pin(struct i915_vma *vma,
213 u64 size, u64 alignment, u64 flags);
214static inline int __must_check
215i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
216{
217 BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
218 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
219 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
220
221
222
223
224 if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0))
225 return 0;
226
227 return __i915_vma_do_pin(vma, size, alignment, flags);
228}
229
230static inline int i915_vma_pin_count(const struct i915_vma *vma)
231{
232 return vma->flags & I915_VMA_PIN_MASK;
233}
234
235static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
236{
237 return i915_vma_pin_count(vma);
238}
239
240static inline void __i915_vma_pin(struct i915_vma *vma)
241{
242 vma->flags++;
243 GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
244}
245
246static inline void __i915_vma_unpin(struct i915_vma *vma)
247{
248 GEM_BUG_ON(!i915_vma_is_pinned(vma));
249 vma->flags--;
250}
251
252static inline void i915_vma_unpin(struct i915_vma *vma)
253{
254 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
255 __i915_vma_unpin(vma);
256}
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
272#define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
273
274
275
276
277
278
279
280
281
282
283static inline void i915_vma_unpin_iomap(struct i915_vma *vma)
284{
285 lockdep_assert_held(&vma->vm->dev->struct_mutex);
286 GEM_BUG_ON(vma->iomap == NULL);
287 i915_vma_unpin(vma);
288}
289
290static inline struct page *i915_vma_first_page(struct i915_vma *vma)
291{
292 GEM_BUG_ON(!vma->pages);
293 return sg_page(vma->pages->sgl);
294}
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311static inline bool
312i915_vma_pin_fence(struct i915_vma *vma)
313{
314 lockdep_assert_held(&vma->vm->dev->struct_mutex);
315 if (vma->fence) {
316 vma->fence->pin_count++;
317 return true;
318 } else
319 return false;
320}
321
322
323
324
325
326
327
328
329
330static inline void
331i915_vma_unpin_fence(struct i915_vma *vma)
332{
333 lockdep_assert_held(&vma->vm->dev->struct_mutex);
334 if (vma->fence) {
335 GEM_BUG_ON(vma->fence->pin_count <= 0);
336 vma->fence->pin_count--;
337 }
338}
339
340#endif
341
342