]>
Commit | Line | Data |
---|---|---|
54cf91dc CW |
1 | /* |
2 | * Copyright © 2008,2010 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 | * Authors: | |
24 | * Eric Anholt <eric@anholt.net> | |
25 | * Chris Wilson <chris@chris-wilson.co.uk> | |
26 | * | |
27 | */ | |
28 | ||
760285e7 DH |
29 | #include <drm/drmP.h> |
30 | #include <drm/i915_drm.h> | |
54cf91dc CW |
31 | #include "i915_drv.h" |
32 | #include "i915_trace.h" | |
33 | #include "intel_drv.h" | |
f45b5557 | 34 | #include <linux/dma_remapping.h> |
32d82067 | 35 | #include <linux/uaccess.h> |
54cf91dc | 36 | |
a415d355 CW |
37 | #define __EXEC_OBJECT_HAS_PIN (1<<31) |
38 | #define __EXEC_OBJECT_HAS_FENCE (1<<30) | |
e6a84468 | 39 | #define __EXEC_OBJECT_NEEDS_MAP (1<<29) |
d23db88c CW |
40 | #define __EXEC_OBJECT_NEEDS_BIAS (1<<28) |
41 | ||
42 | #define BATCH_OFFSET_BIAS (256*1024) | |
a415d355 | 43 | |
27173f1f BW |
44 | struct eb_vmas { |
45 | struct list_head vmas; | |
67731b87 | 46 | int and; |
eef90ccb | 47 | union { |
27173f1f | 48 | struct i915_vma *lut[0]; |
eef90ccb CW |
49 | struct hlist_head buckets[0]; |
50 | }; | |
67731b87 CW |
51 | }; |
52 | ||
27173f1f | 53 | static struct eb_vmas * |
17601cbc | 54 | eb_create(struct drm_i915_gem_execbuffer2 *args) |
67731b87 | 55 | { |
27173f1f | 56 | struct eb_vmas *eb = NULL; |
eef90ccb CW |
57 | |
58 | if (args->flags & I915_EXEC_HANDLE_LUT) { | |
b205ca57 | 59 | unsigned size = args->buffer_count; |
27173f1f BW |
60 | size *= sizeof(struct i915_vma *); |
61 | size += sizeof(struct eb_vmas); | |
eef90ccb CW |
62 | eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); |
63 | } | |
64 | ||
65 | if (eb == NULL) { | |
b205ca57 DV |
66 | unsigned size = args->buffer_count; |
67 | unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2; | |
27b7c63a | 68 | BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head)); |
eef90ccb CW |
69 | while (count > 2*size) |
70 | count >>= 1; | |
71 | eb = kzalloc(count*sizeof(struct hlist_head) + | |
27173f1f | 72 | sizeof(struct eb_vmas), |
eef90ccb CW |
73 | GFP_TEMPORARY); |
74 | if (eb == NULL) | |
75 | return eb; | |
76 | ||
77 | eb->and = count - 1; | |
78 | } else | |
79 | eb->and = -args->buffer_count; | |
80 | ||
27173f1f | 81 | INIT_LIST_HEAD(&eb->vmas); |
67731b87 CW |
82 | return eb; |
83 | } | |
84 | ||
85 | static void | |
27173f1f | 86 | eb_reset(struct eb_vmas *eb) |
67731b87 | 87 | { |
eef90ccb CW |
88 | if (eb->and >= 0) |
89 | memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head)); | |
67731b87 CW |
90 | } |
91 | ||
3b96eff4 | 92 | static int |
27173f1f BW |
93 | eb_lookup_vmas(struct eb_vmas *eb, |
94 | struct drm_i915_gem_exec_object2 *exec, | |
95 | const struct drm_i915_gem_execbuffer2 *args, | |
96 | struct i915_address_space *vm, | |
97 | struct drm_file *file) | |
3b96eff4 | 98 | { |
27173f1f BW |
99 | struct drm_i915_gem_object *obj; |
100 | struct list_head objects; | |
9ae9ab52 | 101 | int i, ret; |
3b96eff4 | 102 | |
27173f1f | 103 | INIT_LIST_HEAD(&objects); |
3b96eff4 | 104 | spin_lock(&file->table_lock); |
27173f1f BW |
105 | /* Grab a reference to the object and release the lock so we can lookup |
106 | * or create the VMA without using GFP_ATOMIC */ | |
eef90ccb | 107 | for (i = 0; i < args->buffer_count; i++) { |
3b96eff4 CW |
108 | obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle)); |
109 | if (obj == NULL) { | |
110 | spin_unlock(&file->table_lock); | |
111 | DRM_DEBUG("Invalid object handle %d at index %d\n", | |
112 | exec[i].handle, i); | |
27173f1f | 113 | ret = -ENOENT; |
9ae9ab52 | 114 | goto err; |
3b96eff4 CW |
115 | } |
116 | ||
27173f1f | 117 | if (!list_empty(&obj->obj_exec_link)) { |
3b96eff4 CW |
118 | spin_unlock(&file->table_lock); |
119 | DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n", | |
120 | obj, exec[i].handle, i); | |
27173f1f | 121 | ret = -EINVAL; |
9ae9ab52 | 122 | goto err; |
3b96eff4 CW |
123 | } |
124 | ||
125 | drm_gem_object_reference(&obj->base); | |
27173f1f BW |
126 | list_add_tail(&obj->obj_exec_link, &objects); |
127 | } | |
128 | spin_unlock(&file->table_lock); | |
3b96eff4 | 129 | |
27173f1f | 130 | i = 0; |
9ae9ab52 | 131 | while (!list_empty(&objects)) { |
27173f1f | 132 | struct i915_vma *vma; |
6f65e29a | 133 | |
9ae9ab52 CW |
134 | obj = list_first_entry(&objects, |
135 | struct drm_i915_gem_object, | |
136 | obj_exec_link); | |
137 | ||
e656a6cb DV |
138 | /* |
139 | * NOTE: We can leak any vmas created here when something fails | |
140 | * later on. But that's no issue since vma_unbind can deal with | |
141 | * vmas which are not actually bound. And since only | |
142 | * lookup_or_create exists as an interface to get at the vma | |
143 | * from the (obj, vm) we don't run the risk of creating | |
144 | * duplicated vmas for the same vm. | |
145 | */ | |
da51a1e7 | 146 | vma = i915_gem_obj_lookup_or_create_vma(obj, vm); |
27173f1f | 147 | if (IS_ERR(vma)) { |
27173f1f BW |
148 | DRM_DEBUG("Failed to lookup VMA\n"); |
149 | ret = PTR_ERR(vma); | |
9ae9ab52 | 150 | goto err; |
27173f1f BW |
151 | } |
152 | ||
9ae9ab52 | 153 | /* Transfer ownership from the objects list to the vmas list. */ |
27173f1f | 154 | list_add_tail(&vma->exec_list, &eb->vmas); |
9ae9ab52 | 155 | list_del_init(&obj->obj_exec_link); |
27173f1f BW |
156 | |
157 | vma->exec_entry = &exec[i]; | |
eef90ccb | 158 | if (eb->and < 0) { |
27173f1f | 159 | eb->lut[i] = vma; |
eef90ccb CW |
160 | } else { |
161 | uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle; | |
27173f1f BW |
162 | vma->exec_handle = handle; |
163 | hlist_add_head(&vma->exec_node, | |
eef90ccb CW |
164 | &eb->buckets[handle & eb->and]); |
165 | } | |
27173f1f | 166 | ++i; |
3b96eff4 | 167 | } |
3b96eff4 | 168 | |
9ae9ab52 | 169 | return 0; |
27173f1f | 170 | |
27173f1f | 171 | |
9ae9ab52 | 172 | err: |
27173f1f BW |
173 | while (!list_empty(&objects)) { |
174 | obj = list_first_entry(&objects, | |
175 | struct drm_i915_gem_object, | |
176 | obj_exec_link); | |
177 | list_del_init(&obj->obj_exec_link); | |
9ae9ab52 | 178 | drm_gem_object_unreference(&obj->base); |
27173f1f | 179 | } |
9ae9ab52 CW |
180 | /* |
181 | * Objects already transfered to the vmas list will be unreferenced by | |
182 | * eb_destroy. | |
183 | */ | |
184 | ||
27173f1f | 185 | return ret; |
3b96eff4 CW |
186 | } |
187 | ||
27173f1f | 188 | static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle) |
67731b87 | 189 | { |
eef90ccb CW |
190 | if (eb->and < 0) { |
191 | if (handle >= -eb->and) | |
192 | return NULL; | |
193 | return eb->lut[handle]; | |
194 | } else { | |
195 | struct hlist_head *head; | |
196 | struct hlist_node *node; | |
67731b87 | 197 | |
eef90ccb CW |
198 | head = &eb->buckets[handle & eb->and]; |
199 | hlist_for_each(node, head) { | |
27173f1f | 200 | struct i915_vma *vma; |
67731b87 | 201 | |
27173f1f BW |
202 | vma = hlist_entry(node, struct i915_vma, exec_node); |
203 | if (vma->exec_handle == handle) | |
204 | return vma; | |
eef90ccb CW |
205 | } |
206 | return NULL; | |
207 | } | |
67731b87 CW |
208 | } |
209 | ||
a415d355 CW |
210 | static void |
211 | i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma) | |
212 | { | |
213 | struct drm_i915_gem_exec_object2 *entry; | |
214 | struct drm_i915_gem_object *obj = vma->obj; | |
215 | ||
216 | if (!drm_mm_node_allocated(&vma->node)) | |
217 | return; | |
218 | ||
219 | entry = vma->exec_entry; | |
220 | ||
221 | if (entry->flags & __EXEC_OBJECT_HAS_FENCE) | |
222 | i915_gem_object_unpin_fence(obj); | |
223 | ||
224 | if (entry->flags & __EXEC_OBJECT_HAS_PIN) | |
3d7f0f9d | 225 | vma->pin_count--; |
a415d355 | 226 | |
de4e783a | 227 | entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN); |
a415d355 CW |
228 | } |
229 | ||
230 | static void eb_destroy(struct eb_vmas *eb) | |
231 | { | |
27173f1f BW |
232 | while (!list_empty(&eb->vmas)) { |
233 | struct i915_vma *vma; | |
bcffc3fa | 234 | |
27173f1f BW |
235 | vma = list_first_entry(&eb->vmas, |
236 | struct i915_vma, | |
bcffc3fa | 237 | exec_list); |
27173f1f | 238 | list_del_init(&vma->exec_list); |
a415d355 | 239 | i915_gem_execbuffer_unreserve_vma(vma); |
27173f1f | 240 | drm_gem_object_unreference(&vma->obj->base); |
bcffc3fa | 241 | } |
67731b87 CW |
242 | kfree(eb); |
243 | } | |
244 | ||
dabdfe02 CW |
245 | static inline int use_cpu_reloc(struct drm_i915_gem_object *obj) |
246 | { | |
2cc86b82 CW |
247 | return (HAS_LLC(obj->base.dev) || |
248 | obj->base.write_domain == I915_GEM_DOMAIN_CPU || | |
dabdfe02 CW |
249 | obj->cache_level != I915_CACHE_NONE); |
250 | } | |
251 | ||
5032d871 RB |
252 | static int |
253 | relocate_entry_cpu(struct drm_i915_gem_object *obj, | |
d9ceb957 BW |
254 | struct drm_i915_gem_relocation_entry *reloc, |
255 | uint64_t target_offset) | |
5032d871 | 256 | { |
3c94ceee | 257 | struct drm_device *dev = obj->base.dev; |
5032d871 | 258 | uint32_t page_offset = offset_in_page(reloc->offset); |
d9ceb957 | 259 | uint64_t delta = reloc->delta + target_offset; |
5032d871 | 260 | char *vaddr; |
8b78f0e5 | 261 | int ret; |
5032d871 | 262 | |
2cc86b82 | 263 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
5032d871 RB |
264 | if (ret) |
265 | return ret; | |
266 | ||
267 | vaddr = kmap_atomic(i915_gem_object_get_page(obj, | |
268 | reloc->offset >> PAGE_SHIFT)); | |
d9ceb957 | 269 | *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta); |
3c94ceee BW |
270 | |
271 | if (INTEL_INFO(dev)->gen >= 8) { | |
272 | page_offset = offset_in_page(page_offset + sizeof(uint32_t)); | |
273 | ||
274 | if (page_offset == 0) { | |
275 | kunmap_atomic(vaddr); | |
276 | vaddr = kmap_atomic(i915_gem_object_get_page(obj, | |
277 | (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT)); | |
278 | } | |
279 | ||
d9ceb957 | 280 | *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta); |
3c94ceee BW |
281 | } |
282 | ||
5032d871 RB |
283 | kunmap_atomic(vaddr); |
284 | ||
285 | return 0; | |
286 | } | |
287 | ||
288 | static int | |
289 | relocate_entry_gtt(struct drm_i915_gem_object *obj, | |
d9ceb957 BW |
290 | struct drm_i915_gem_relocation_entry *reloc, |
291 | uint64_t target_offset) | |
5032d871 RB |
292 | { |
293 | struct drm_device *dev = obj->base.dev; | |
294 | struct drm_i915_private *dev_priv = dev->dev_private; | |
d9ceb957 | 295 | uint64_t delta = reloc->delta + target_offset; |
906843c3 | 296 | uint64_t offset; |
5032d871 | 297 | void __iomem *reloc_page; |
8b78f0e5 | 298 | int ret; |
5032d871 RB |
299 | |
300 | ret = i915_gem_object_set_to_gtt_domain(obj, true); | |
301 | if (ret) | |
302 | return ret; | |
303 | ||
304 | ret = i915_gem_object_put_fence(obj); | |
305 | if (ret) | |
306 | return ret; | |
307 | ||
308 | /* Map the page containing the relocation we're going to perform. */ | |
906843c3 CW |
309 | offset = i915_gem_obj_ggtt_offset(obj); |
310 | offset += reloc->offset; | |
5032d871 | 311 | reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable, |
906843c3 CW |
312 | offset & PAGE_MASK); |
313 | iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset)); | |
3c94ceee BW |
314 | |
315 | if (INTEL_INFO(dev)->gen >= 8) { | |
906843c3 | 316 | offset += sizeof(uint32_t); |
3c94ceee | 317 | |
906843c3 | 318 | if (offset_in_page(offset) == 0) { |
3c94ceee | 319 | io_mapping_unmap_atomic(reloc_page); |
906843c3 CW |
320 | reloc_page = |
321 | io_mapping_map_atomic_wc(dev_priv->gtt.mappable, | |
322 | offset); | |
3c94ceee BW |
323 | } |
324 | ||
906843c3 CW |
325 | iowrite32(upper_32_bits(delta), |
326 | reloc_page + offset_in_page(offset)); | |
3c94ceee BW |
327 | } |
328 | ||
5032d871 RB |
329 | io_mapping_unmap_atomic(reloc_page); |
330 | ||
331 | return 0; | |
332 | } | |
333 | ||
edf4427b CW |
334 | static void |
335 | clflush_write32(void *addr, uint32_t value) | |
336 | { | |
337 | /* This is not a fast path, so KISS. */ | |
338 | drm_clflush_virt_range(addr, sizeof(uint32_t)); | |
339 | *(uint32_t *)addr = value; | |
340 | drm_clflush_virt_range(addr, sizeof(uint32_t)); | |
341 | } | |
342 | ||
343 | static int | |
344 | relocate_entry_clflush(struct drm_i915_gem_object *obj, | |
345 | struct drm_i915_gem_relocation_entry *reloc, | |
346 | uint64_t target_offset) | |
347 | { | |
348 | struct drm_device *dev = obj->base.dev; | |
349 | uint32_t page_offset = offset_in_page(reloc->offset); | |
350 | uint64_t delta = (int)reloc->delta + target_offset; | |
351 | char *vaddr; | |
352 | int ret; | |
353 | ||
354 | ret = i915_gem_object_set_to_gtt_domain(obj, true); | |
355 | if (ret) | |
356 | return ret; | |
357 | ||
358 | vaddr = kmap_atomic(i915_gem_object_get_page(obj, | |
359 | reloc->offset >> PAGE_SHIFT)); | |
360 | clflush_write32(vaddr + page_offset, lower_32_bits(delta)); | |
361 | ||
362 | if (INTEL_INFO(dev)->gen >= 8) { | |
363 | page_offset = offset_in_page(page_offset + sizeof(uint32_t)); | |
364 | ||
365 | if (page_offset == 0) { | |
366 | kunmap_atomic(vaddr); | |
367 | vaddr = kmap_atomic(i915_gem_object_get_page(obj, | |
368 | (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT)); | |
369 | } | |
370 | ||
371 | clflush_write32(vaddr + page_offset, upper_32_bits(delta)); | |
372 | } | |
373 | ||
374 | kunmap_atomic(vaddr); | |
375 | ||
376 | return 0; | |
377 | } | |
378 | ||
54cf91dc CW |
379 | static int |
380 | i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj, | |
27173f1f | 381 | struct eb_vmas *eb, |
3e7a0322 | 382 | struct drm_i915_gem_relocation_entry *reloc) |
54cf91dc CW |
383 | { |
384 | struct drm_device *dev = obj->base.dev; | |
385 | struct drm_gem_object *target_obj; | |
149c8407 | 386 | struct drm_i915_gem_object *target_i915_obj; |
27173f1f | 387 | struct i915_vma *target_vma; |
d9ceb957 | 388 | uint64_t target_offset; |
8b78f0e5 | 389 | int ret; |
54cf91dc | 390 | |
67731b87 | 391 | /* we've already hold a reference to all valid objects */ |
27173f1f BW |
392 | target_vma = eb_get_vma(eb, reloc->target_handle); |
393 | if (unlikely(target_vma == NULL)) | |
54cf91dc | 394 | return -ENOENT; |
27173f1f BW |
395 | target_i915_obj = target_vma->obj; |
396 | target_obj = &target_vma->obj->base; | |
54cf91dc | 397 | |
5ce09725 | 398 | target_offset = target_vma->node.start; |
54cf91dc | 399 | |
e844b990 EA |
400 | /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and |
401 | * pipe_control writes because the gpu doesn't properly redirect them | |
402 | * through the ppgtt for non_secure batchbuffers. */ | |
403 | if (unlikely(IS_GEN6(dev) && | |
0875546c | 404 | reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) { |
fe14d5f4 | 405 | ret = i915_vma_bind(target_vma, target_i915_obj->cache_level, |
0875546c | 406 | PIN_GLOBAL); |
fe14d5f4 TU |
407 | if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!")) |
408 | return ret; | |
409 | } | |
e844b990 | 410 | |
54cf91dc | 411 | /* Validate that the target is in a valid r/w GPU domain */ |
b8f7ab17 | 412 | if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { |
ff240199 | 413 | DRM_DEBUG("reloc with multiple write domains: " |
54cf91dc CW |
414 | "obj %p target %d offset %d " |
415 | "read %08x write %08x", | |
416 | obj, reloc->target_handle, | |
417 | (int) reloc->offset, | |
418 | reloc->read_domains, | |
419 | reloc->write_domain); | |
8b78f0e5 | 420 | return -EINVAL; |
54cf91dc | 421 | } |
4ca4a250 DV |
422 | if (unlikely((reloc->write_domain | reloc->read_domains) |
423 | & ~I915_GEM_GPU_DOMAINS)) { | |
ff240199 | 424 | DRM_DEBUG("reloc with read/write non-GPU domains: " |
54cf91dc CW |
425 | "obj %p target %d offset %d " |
426 | "read %08x write %08x", | |
427 | obj, reloc->target_handle, | |
428 | (int) reloc->offset, | |
429 | reloc->read_domains, | |
430 | reloc->write_domain); | |
8b78f0e5 | 431 | return -EINVAL; |
54cf91dc | 432 | } |
54cf91dc CW |
433 | |
434 | target_obj->pending_read_domains |= reloc->read_domains; | |
435 | target_obj->pending_write_domain |= reloc->write_domain; | |
436 | ||
437 | /* If the relocation already has the right value in it, no | |
438 | * more work needs to be done. | |
439 | */ | |
440 | if (target_offset == reloc->presumed_offset) | |
67731b87 | 441 | return 0; |
54cf91dc CW |
442 | |
443 | /* Check that the relocation address is valid... */ | |
3c94ceee BW |
444 | if (unlikely(reloc->offset > |
445 | obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) { | |
ff240199 | 446 | DRM_DEBUG("Relocation beyond object bounds: " |
54cf91dc CW |
447 | "obj %p target %d offset %d size %d.\n", |
448 | obj, reloc->target_handle, | |
449 | (int) reloc->offset, | |
450 | (int) obj->base.size); | |
8b78f0e5 | 451 | return -EINVAL; |
54cf91dc | 452 | } |
b8f7ab17 | 453 | if (unlikely(reloc->offset & 3)) { |
ff240199 | 454 | DRM_DEBUG("Relocation not 4-byte aligned: " |
54cf91dc CW |
455 | "obj %p target %d offset %d.\n", |
456 | obj, reloc->target_handle, | |
457 | (int) reloc->offset); | |
8b78f0e5 | 458 | return -EINVAL; |
54cf91dc CW |
459 | } |
460 | ||
dabdfe02 | 461 | /* We can't wait for rendering with pagefaults disabled */ |
32d82067 | 462 | if (obj->active && pagefault_disabled()) |
dabdfe02 CW |
463 | return -EFAULT; |
464 | ||
5032d871 | 465 | if (use_cpu_reloc(obj)) |
d9ceb957 | 466 | ret = relocate_entry_cpu(obj, reloc, target_offset); |
edf4427b | 467 | else if (obj->map_and_fenceable) |
d9ceb957 | 468 | ret = relocate_entry_gtt(obj, reloc, target_offset); |
edf4427b CW |
469 | else if (cpu_has_clflush) |
470 | ret = relocate_entry_clflush(obj, reloc, target_offset); | |
471 | else { | |
472 | WARN_ONCE(1, "Impossible case in relocation handling\n"); | |
473 | ret = -ENODEV; | |
474 | } | |
54cf91dc | 475 | |
d4d36014 DV |
476 | if (ret) |
477 | return ret; | |
478 | ||
54cf91dc CW |
479 | /* and update the user's relocation entry */ |
480 | reloc->presumed_offset = target_offset; | |
481 | ||
67731b87 | 482 | return 0; |
54cf91dc CW |
483 | } |
484 | ||
485 | static int | |
27173f1f BW |
486 | i915_gem_execbuffer_relocate_vma(struct i915_vma *vma, |
487 | struct eb_vmas *eb) | |
54cf91dc | 488 | { |
1d83f442 CW |
489 | #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry)) |
490 | struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)]; | |
54cf91dc | 491 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
27173f1f | 492 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
1d83f442 | 493 | int remain, ret; |
54cf91dc | 494 | |
2bb4629a | 495 | user_relocs = to_user_ptr(entry->relocs_ptr); |
54cf91dc | 496 | |
1d83f442 CW |
497 | remain = entry->relocation_count; |
498 | while (remain) { | |
499 | struct drm_i915_gem_relocation_entry *r = stack_reloc; | |
500 | int count = remain; | |
501 | if (count > ARRAY_SIZE(stack_reloc)) | |
502 | count = ARRAY_SIZE(stack_reloc); | |
503 | remain -= count; | |
504 | ||
505 | if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0]))) | |
54cf91dc CW |
506 | return -EFAULT; |
507 | ||
1d83f442 CW |
508 | do { |
509 | u64 offset = r->presumed_offset; | |
54cf91dc | 510 | |
3e7a0322 | 511 | ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r); |
1d83f442 CW |
512 | if (ret) |
513 | return ret; | |
514 | ||
515 | if (r->presumed_offset != offset && | |
516 | __copy_to_user_inatomic(&user_relocs->presumed_offset, | |
517 | &r->presumed_offset, | |
518 | sizeof(r->presumed_offset))) { | |
519 | return -EFAULT; | |
520 | } | |
521 | ||
522 | user_relocs++; | |
523 | r++; | |
524 | } while (--count); | |
54cf91dc CW |
525 | } |
526 | ||
527 | return 0; | |
1d83f442 | 528 | #undef N_RELOC |
54cf91dc CW |
529 | } |
530 | ||
531 | static int | |
27173f1f BW |
532 | i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma, |
533 | struct eb_vmas *eb, | |
534 | struct drm_i915_gem_relocation_entry *relocs) | |
54cf91dc | 535 | { |
27173f1f | 536 | const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
54cf91dc CW |
537 | int i, ret; |
538 | ||
539 | for (i = 0; i < entry->relocation_count; i++) { | |
3e7a0322 | 540 | ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]); |
54cf91dc CW |
541 | if (ret) |
542 | return ret; | |
543 | } | |
544 | ||
545 | return 0; | |
546 | } | |
547 | ||
548 | static int | |
17601cbc | 549 | i915_gem_execbuffer_relocate(struct eb_vmas *eb) |
54cf91dc | 550 | { |
27173f1f | 551 | struct i915_vma *vma; |
d4aeee77 CW |
552 | int ret = 0; |
553 | ||
554 | /* This is the fast path and we cannot handle a pagefault whilst | |
555 | * holding the struct mutex lest the user pass in the relocations | |
556 | * contained within a mmaped bo. For in such a case we, the page | |
557 | * fault handler would call i915_gem_fault() and we would try to | |
558 | * acquire the struct mutex again. Obviously this is bad and so | |
559 | * lockdep complains vehemently. | |
560 | */ | |
561 | pagefault_disable(); | |
27173f1f BW |
562 | list_for_each_entry(vma, &eb->vmas, exec_list) { |
563 | ret = i915_gem_execbuffer_relocate_vma(vma, eb); | |
54cf91dc | 564 | if (ret) |
d4aeee77 | 565 | break; |
54cf91dc | 566 | } |
d4aeee77 | 567 | pagefault_enable(); |
54cf91dc | 568 | |
d4aeee77 | 569 | return ret; |
54cf91dc CW |
570 | } |
571 | ||
edf4427b CW |
572 | static bool only_mappable_for_reloc(unsigned int flags) |
573 | { | |
574 | return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) == | |
575 | __EXEC_OBJECT_NEEDS_MAP; | |
576 | } | |
577 | ||
1690e1eb | 578 | static int |
27173f1f | 579 | i915_gem_execbuffer_reserve_vma(struct i915_vma *vma, |
a4872ba6 | 580 | struct intel_engine_cs *ring, |
27173f1f | 581 | bool *need_reloc) |
1690e1eb | 582 | { |
6f65e29a | 583 | struct drm_i915_gem_object *obj = vma->obj; |
27173f1f | 584 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
d23db88c | 585 | uint64_t flags; |
1690e1eb CW |
586 | int ret; |
587 | ||
0875546c | 588 | flags = PIN_USER; |
0229da32 DV |
589 | if (entry->flags & EXEC_OBJECT_NEEDS_GTT) |
590 | flags |= PIN_GLOBAL; | |
591 | ||
edf4427b | 592 | if (!drm_mm_node_allocated(&vma->node)) { |
101b506a MT |
593 | /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset, |
594 | * limit address to the first 4GBs for unflagged objects. | |
595 | */ | |
596 | if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0) | |
597 | flags |= PIN_ZONE_4G; | |
edf4427b CW |
598 | if (entry->flags & __EXEC_OBJECT_NEEDS_MAP) |
599 | flags |= PIN_GLOBAL | PIN_MAPPABLE; | |
edf4427b CW |
600 | if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS) |
601 | flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS; | |
101b506a MT |
602 | if ((flags & PIN_MAPPABLE) == 0) |
603 | flags |= PIN_HIGH; | |
edf4427b | 604 | } |
1ec9e26d DV |
605 | |
606 | ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags); | |
edf4427b CW |
607 | if ((ret == -ENOSPC || ret == -E2BIG) && |
608 | only_mappable_for_reloc(entry->flags)) | |
609 | ret = i915_gem_object_pin(obj, vma->vm, | |
610 | entry->alignment, | |
0229da32 | 611 | flags & ~PIN_MAPPABLE); |
1690e1eb CW |
612 | if (ret) |
613 | return ret; | |
614 | ||
7788a765 CW |
615 | entry->flags |= __EXEC_OBJECT_HAS_PIN; |
616 | ||
82b6b6d7 CW |
617 | if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) { |
618 | ret = i915_gem_object_get_fence(obj); | |
619 | if (ret) | |
620 | return ret; | |
9a5a53b3 | 621 | |
82b6b6d7 CW |
622 | if (i915_gem_object_pin_fence(obj)) |
623 | entry->flags |= __EXEC_OBJECT_HAS_FENCE; | |
1690e1eb CW |
624 | } |
625 | ||
27173f1f BW |
626 | if (entry->offset != vma->node.start) { |
627 | entry->offset = vma->node.start; | |
ed5982e6 DV |
628 | *need_reloc = true; |
629 | } | |
630 | ||
631 | if (entry->flags & EXEC_OBJECT_WRITE) { | |
632 | obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER; | |
633 | obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER; | |
634 | } | |
635 | ||
1690e1eb | 636 | return 0; |
7788a765 | 637 | } |
1690e1eb | 638 | |
d23db88c | 639 | static bool |
e6a84468 | 640 | need_reloc_mappable(struct i915_vma *vma) |
d23db88c CW |
641 | { |
642 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; | |
d23db88c | 643 | |
e6a84468 CW |
644 | if (entry->relocation_count == 0) |
645 | return false; | |
646 | ||
647 | if (!i915_is_ggtt(vma->vm)) | |
648 | return false; | |
649 | ||
650 | /* See also use_cpu_reloc() */ | |
651 | if (HAS_LLC(vma->obj->base.dev)) | |
652 | return false; | |
653 | ||
654 | if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU) | |
655 | return false; | |
656 | ||
657 | return true; | |
658 | } | |
659 | ||
660 | static bool | |
661 | eb_vma_misplaced(struct i915_vma *vma) | |
662 | { | |
663 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; | |
664 | struct drm_i915_gem_object *obj = vma->obj; | |
d23db88c | 665 | |
e6a84468 | 666 | WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP && |
d23db88c CW |
667 | !i915_is_ggtt(vma->vm)); |
668 | ||
669 | if (entry->alignment && | |
670 | vma->node.start & (entry->alignment - 1)) | |
671 | return true; | |
672 | ||
d23db88c CW |
673 | if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS && |
674 | vma->node.start < BATCH_OFFSET_BIAS) | |
675 | return true; | |
676 | ||
edf4427b CW |
677 | /* avoid costly ping-pong once a batch bo ended up non-mappable */ |
678 | if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable) | |
679 | return !only_mappable_for_reloc(entry->flags); | |
680 | ||
101b506a MT |
681 | if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 && |
682 | (vma->node.start + vma->node.size - 1) >> 32) | |
683 | return true; | |
684 | ||
d23db88c CW |
685 | return false; |
686 | } | |
687 | ||
54cf91dc | 688 | static int |
a4872ba6 | 689 | i915_gem_execbuffer_reserve(struct intel_engine_cs *ring, |
27173f1f | 690 | struct list_head *vmas, |
b1b38278 | 691 | struct intel_context *ctx, |
ed5982e6 | 692 | bool *need_relocs) |
54cf91dc | 693 | { |
432e58ed | 694 | struct drm_i915_gem_object *obj; |
27173f1f | 695 | struct i915_vma *vma; |
68c8c17f | 696 | struct i915_address_space *vm; |
27173f1f | 697 | struct list_head ordered_vmas; |
7788a765 CW |
698 | bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4; |
699 | int retry; | |
6fe4f140 | 700 | |
227f782e CW |
701 | i915_gem_retire_requests_ring(ring); |
702 | ||
68c8c17f BW |
703 | vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm; |
704 | ||
27173f1f BW |
705 | INIT_LIST_HEAD(&ordered_vmas); |
706 | while (!list_empty(vmas)) { | |
6fe4f140 CW |
707 | struct drm_i915_gem_exec_object2 *entry; |
708 | bool need_fence, need_mappable; | |
709 | ||
27173f1f BW |
710 | vma = list_first_entry(vmas, struct i915_vma, exec_list); |
711 | obj = vma->obj; | |
712 | entry = vma->exec_entry; | |
6fe4f140 | 713 | |
b1b38278 DW |
714 | if (ctx->flags & CONTEXT_NO_ZEROMAP) |
715 | entry->flags |= __EXEC_OBJECT_NEEDS_BIAS; | |
716 | ||
82b6b6d7 CW |
717 | if (!has_fenced_gpu_access) |
718 | entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE; | |
6fe4f140 | 719 | need_fence = |
6fe4f140 CW |
720 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
721 | obj->tiling_mode != I915_TILING_NONE; | |
27173f1f | 722 | need_mappable = need_fence || need_reloc_mappable(vma); |
6fe4f140 | 723 | |
e6a84468 CW |
724 | if (need_mappable) { |
725 | entry->flags |= __EXEC_OBJECT_NEEDS_MAP; | |
27173f1f | 726 | list_move(&vma->exec_list, &ordered_vmas); |
e6a84468 | 727 | } else |
27173f1f | 728 | list_move_tail(&vma->exec_list, &ordered_vmas); |
595dad76 | 729 | |
ed5982e6 | 730 | obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND; |
595dad76 | 731 | obj->base.pending_write_domain = 0; |
6fe4f140 | 732 | } |
27173f1f | 733 | list_splice(&ordered_vmas, vmas); |
54cf91dc CW |
734 | |
735 | /* Attempt to pin all of the buffers into the GTT. | |
736 | * This is done in 3 phases: | |
737 | * | |
738 | * 1a. Unbind all objects that do not match the GTT constraints for | |
739 | * the execbuffer (fenceable, mappable, alignment etc). | |
740 | * 1b. Increment pin count for already bound objects. | |
741 | * 2. Bind new objects. | |
742 | * 3. Decrement pin count. | |
743 | * | |
7788a765 | 744 | * This avoid unnecessary unbinding of later objects in order to make |
54cf91dc CW |
745 | * room for the earlier objects *unless* we need to defragment. |
746 | */ | |
747 | retry = 0; | |
748 | do { | |
7788a765 | 749 | int ret = 0; |
54cf91dc CW |
750 | |
751 | /* Unbind any ill-fitting objects or pin. */ | |
27173f1f | 752 | list_for_each_entry(vma, vmas, exec_list) { |
27173f1f | 753 | if (!drm_mm_node_allocated(&vma->node)) |
54cf91dc CW |
754 | continue; |
755 | ||
e6a84468 | 756 | if (eb_vma_misplaced(vma)) |
27173f1f | 757 | ret = i915_vma_unbind(vma); |
54cf91dc | 758 | else |
27173f1f | 759 | ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs); |
432e58ed | 760 | if (ret) |
54cf91dc | 761 | goto err; |
54cf91dc CW |
762 | } |
763 | ||
764 | /* Bind fresh objects */ | |
27173f1f BW |
765 | list_for_each_entry(vma, vmas, exec_list) { |
766 | if (drm_mm_node_allocated(&vma->node)) | |
1690e1eb | 767 | continue; |
54cf91dc | 768 | |
27173f1f | 769 | ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs); |
7788a765 CW |
770 | if (ret) |
771 | goto err; | |
54cf91dc CW |
772 | } |
773 | ||
a415d355 | 774 | err: |
6c085a72 | 775 | if (ret != -ENOSPC || retry++) |
54cf91dc CW |
776 | return ret; |
777 | ||
a415d355 CW |
778 | /* Decrement pin count for bound objects */ |
779 | list_for_each_entry(vma, vmas, exec_list) | |
780 | i915_gem_execbuffer_unreserve_vma(vma); | |
781 | ||
68c8c17f | 782 | ret = i915_gem_evict_vm(vm, true); |
54cf91dc CW |
783 | if (ret) |
784 | return ret; | |
54cf91dc CW |
785 | } while (1); |
786 | } | |
787 | ||
788 | static int | |
789 | i915_gem_execbuffer_relocate_slow(struct drm_device *dev, | |
ed5982e6 | 790 | struct drm_i915_gem_execbuffer2 *args, |
54cf91dc | 791 | struct drm_file *file, |
a4872ba6 | 792 | struct intel_engine_cs *ring, |
27173f1f | 793 | struct eb_vmas *eb, |
b1b38278 DW |
794 | struct drm_i915_gem_exec_object2 *exec, |
795 | struct intel_context *ctx) | |
54cf91dc CW |
796 | { |
797 | struct drm_i915_gem_relocation_entry *reloc; | |
27173f1f BW |
798 | struct i915_address_space *vm; |
799 | struct i915_vma *vma; | |
ed5982e6 | 800 | bool need_relocs; |
dd6864a4 | 801 | int *reloc_offset; |
54cf91dc | 802 | int i, total, ret; |
b205ca57 | 803 | unsigned count = args->buffer_count; |
54cf91dc | 804 | |
27173f1f BW |
805 | vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm; |
806 | ||
67731b87 | 807 | /* We may process another execbuffer during the unlock... */ |
27173f1f BW |
808 | while (!list_empty(&eb->vmas)) { |
809 | vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list); | |
810 | list_del_init(&vma->exec_list); | |
a415d355 | 811 | i915_gem_execbuffer_unreserve_vma(vma); |
27173f1f | 812 | drm_gem_object_unreference(&vma->obj->base); |
67731b87 CW |
813 | } |
814 | ||
54cf91dc CW |
815 | mutex_unlock(&dev->struct_mutex); |
816 | ||
817 | total = 0; | |
818 | for (i = 0; i < count; i++) | |
432e58ed | 819 | total += exec[i].relocation_count; |
54cf91dc | 820 | |
dd6864a4 | 821 | reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset)); |
54cf91dc | 822 | reloc = drm_malloc_ab(total, sizeof(*reloc)); |
dd6864a4 CW |
823 | if (reloc == NULL || reloc_offset == NULL) { |
824 | drm_free_large(reloc); | |
825 | drm_free_large(reloc_offset); | |
54cf91dc CW |
826 | mutex_lock(&dev->struct_mutex); |
827 | return -ENOMEM; | |
828 | } | |
829 | ||
830 | total = 0; | |
831 | for (i = 0; i < count; i++) { | |
832 | struct drm_i915_gem_relocation_entry __user *user_relocs; | |
262b6d36 CW |
833 | u64 invalid_offset = (u64)-1; |
834 | int j; | |
54cf91dc | 835 | |
2bb4629a | 836 | user_relocs = to_user_ptr(exec[i].relocs_ptr); |
54cf91dc CW |
837 | |
838 | if (copy_from_user(reloc+total, user_relocs, | |
432e58ed | 839 | exec[i].relocation_count * sizeof(*reloc))) { |
54cf91dc CW |
840 | ret = -EFAULT; |
841 | mutex_lock(&dev->struct_mutex); | |
842 | goto err; | |
843 | } | |
844 | ||
262b6d36 CW |
845 | /* As we do not update the known relocation offsets after |
846 | * relocating (due to the complexities in lock handling), | |
847 | * we need to mark them as invalid now so that we force the | |
848 | * relocation processing next time. Just in case the target | |
849 | * object is evicted and then rebound into its old | |
850 | * presumed_offset before the next execbuffer - if that | |
851 | * happened we would make the mistake of assuming that the | |
852 | * relocations were valid. | |
853 | */ | |
854 | for (j = 0; j < exec[i].relocation_count; j++) { | |
9aab8bff CW |
855 | if (__copy_to_user(&user_relocs[j].presumed_offset, |
856 | &invalid_offset, | |
857 | sizeof(invalid_offset))) { | |
262b6d36 CW |
858 | ret = -EFAULT; |
859 | mutex_lock(&dev->struct_mutex); | |
860 | goto err; | |
861 | } | |
862 | } | |
863 | ||
dd6864a4 | 864 | reloc_offset[i] = total; |
432e58ed | 865 | total += exec[i].relocation_count; |
54cf91dc CW |
866 | } |
867 | ||
868 | ret = i915_mutex_lock_interruptible(dev); | |
869 | if (ret) { | |
870 | mutex_lock(&dev->struct_mutex); | |
871 | goto err; | |
872 | } | |
873 | ||
67731b87 | 874 | /* reacquire the objects */ |
67731b87 | 875 | eb_reset(eb); |
27173f1f | 876 | ret = eb_lookup_vmas(eb, exec, args, vm, file); |
3b96eff4 CW |
877 | if (ret) |
878 | goto err; | |
67731b87 | 879 | |
ed5982e6 | 880 | need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0; |
b1b38278 | 881 | ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs); |
54cf91dc CW |
882 | if (ret) |
883 | goto err; | |
884 | ||
27173f1f BW |
885 | list_for_each_entry(vma, &eb->vmas, exec_list) { |
886 | int offset = vma->exec_entry - exec; | |
887 | ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb, | |
888 | reloc + reloc_offset[offset]); | |
54cf91dc CW |
889 | if (ret) |
890 | goto err; | |
54cf91dc CW |
891 | } |
892 | ||
893 | /* Leave the user relocations as are, this is the painfully slow path, | |
894 | * and we want to avoid the complication of dropping the lock whilst | |
895 | * having buffers reserved in the aperture and so causing spurious | |
896 | * ENOSPC for random operations. | |
897 | */ | |
898 | ||
899 | err: | |
900 | drm_free_large(reloc); | |
dd6864a4 | 901 | drm_free_large(reloc_offset); |
54cf91dc CW |
902 | return ret; |
903 | } | |
904 | ||
54cf91dc | 905 | static int |
535fbe82 | 906 | i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req, |
27173f1f | 907 | struct list_head *vmas) |
54cf91dc | 908 | { |
535fbe82 | 909 | const unsigned other_rings = ~intel_ring_flag(req->ring); |
27173f1f | 910 | struct i915_vma *vma; |
6ac42f41 | 911 | uint32_t flush_domains = 0; |
000433b6 | 912 | bool flush_chipset = false; |
432e58ed | 913 | int ret; |
54cf91dc | 914 | |
27173f1f BW |
915 | list_for_each_entry(vma, vmas, exec_list) { |
916 | struct drm_i915_gem_object *obj = vma->obj; | |
03ade511 CW |
917 | |
918 | if (obj->active & other_rings) { | |
91af127f | 919 | ret = i915_gem_object_sync(obj, req->ring, &req); |
03ade511 CW |
920 | if (ret) |
921 | return ret; | |
922 | } | |
6ac42f41 DV |
923 | |
924 | if (obj->base.write_domain & I915_GEM_DOMAIN_CPU) | |
000433b6 | 925 | flush_chipset |= i915_gem_clflush_object(obj, false); |
6ac42f41 | 926 | |
6ac42f41 | 927 | flush_domains |= obj->base.write_domain; |
c59a333f CW |
928 | } |
929 | ||
000433b6 | 930 | if (flush_chipset) |
535fbe82 | 931 | i915_gem_chipset_flush(req->ring->dev); |
6ac42f41 DV |
932 | |
933 | if (flush_domains & I915_GEM_DOMAIN_GTT) | |
934 | wmb(); | |
935 | ||
09cf7c9a CW |
936 | /* Unconditionally invalidate gpu caches and ensure that we do flush |
937 | * any residual writes from the previous batch. | |
938 | */ | |
2f20055d | 939 | return intel_ring_invalidate_all_caches(req); |
54cf91dc CW |
940 | } |
941 | ||
432e58ed CW |
942 | static bool |
943 | i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) | |
54cf91dc | 944 | { |
ed5982e6 DV |
945 | if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS) |
946 | return false; | |
947 | ||
432e58ed | 948 | return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0; |
54cf91dc CW |
949 | } |
950 | ||
951 | static int | |
ad19f10b CW |
952 | validate_exec_list(struct drm_device *dev, |
953 | struct drm_i915_gem_exec_object2 *exec, | |
54cf91dc CW |
954 | int count) |
955 | { | |
b205ca57 DV |
956 | unsigned relocs_total = 0; |
957 | unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry); | |
ad19f10b CW |
958 | unsigned invalid_flags; |
959 | int i; | |
960 | ||
961 | invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS; | |
962 | if (USES_FULL_PPGTT(dev)) | |
963 | invalid_flags |= EXEC_OBJECT_NEEDS_GTT; | |
54cf91dc CW |
964 | |
965 | for (i = 0; i < count; i++) { | |
2bb4629a | 966 | char __user *ptr = to_user_ptr(exec[i].relocs_ptr); |
54cf91dc CW |
967 | int length; /* limited by fault_in_pages_readable() */ |
968 | ||
ad19f10b | 969 | if (exec[i].flags & invalid_flags) |
ed5982e6 DV |
970 | return -EINVAL; |
971 | ||
55a9785d CW |
972 | if (exec[i].alignment && !is_power_of_2(exec[i].alignment)) |
973 | return -EINVAL; | |
974 | ||
3118a4f6 KC |
975 | /* First check for malicious input causing overflow in |
976 | * the worst case where we need to allocate the entire | |
977 | * relocation tree as a single array. | |
978 | */ | |
979 | if (exec[i].relocation_count > relocs_max - relocs_total) | |
54cf91dc | 980 | return -EINVAL; |
3118a4f6 | 981 | relocs_total += exec[i].relocation_count; |
54cf91dc CW |
982 | |
983 | length = exec[i].relocation_count * | |
984 | sizeof(struct drm_i915_gem_relocation_entry); | |
30587535 KC |
985 | /* |
986 | * We must check that the entire relocation array is safe | |
987 | * to read, but since we may need to update the presumed | |
988 | * offsets during execution, check for full write access. | |
989 | */ | |
54cf91dc CW |
990 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
991 | return -EFAULT; | |
992 | ||
d330a953 | 993 | if (likely(!i915.prefault_disable)) { |
0b74b508 XZ |
994 | if (fault_in_multipages_readable(ptr, length)) |
995 | return -EFAULT; | |
996 | } | |
54cf91dc CW |
997 | } |
998 | ||
999 | return 0; | |
1000 | } | |
1001 | ||
273497e5 | 1002 | static struct intel_context * |
d299cce7 | 1003 | i915_gem_validate_context(struct drm_device *dev, struct drm_file *file, |
a4872ba6 | 1004 | struct intel_engine_cs *ring, const u32 ctx_id) |
d299cce7 | 1005 | { |
273497e5 | 1006 | struct intel_context *ctx = NULL; |
d299cce7 MK |
1007 | struct i915_ctx_hang_stats *hs; |
1008 | ||
821d66dd | 1009 | if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE) |
7c9c4b8f DV |
1010 | return ERR_PTR(-EINVAL); |
1011 | ||
41bde553 | 1012 | ctx = i915_gem_context_get(file->driver_priv, ctx_id); |
72ad5c45 | 1013 | if (IS_ERR(ctx)) |
41bde553 | 1014 | return ctx; |
d299cce7 | 1015 | |
41bde553 | 1016 | hs = &ctx->hang_stats; |
d299cce7 MK |
1017 | if (hs->banned) { |
1018 | DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id); | |
41bde553 | 1019 | return ERR_PTR(-EIO); |
d299cce7 MK |
1020 | } |
1021 | ||
ec3e9963 | 1022 | if (i915.enable_execlists && !ctx->engine[ring->id].state) { |
e84fe803 | 1023 | int ret = intel_lr_context_deferred_alloc(ctx, ring); |
ec3e9963 OM |
1024 | if (ret) { |
1025 | DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret); | |
1026 | return ERR_PTR(ret); | |
1027 | } | |
1028 | } | |
1029 | ||
41bde553 | 1030 | return ctx; |
d299cce7 MK |
1031 | } |
1032 | ||
ba8b7ccb | 1033 | void |
27173f1f | 1034 | i915_gem_execbuffer_move_to_active(struct list_head *vmas, |
8a8edb59 | 1035 | struct drm_i915_gem_request *req) |
432e58ed | 1036 | { |
8a8edb59 | 1037 | struct intel_engine_cs *ring = i915_gem_request_get_ring(req); |
27173f1f | 1038 | struct i915_vma *vma; |
432e58ed | 1039 | |
27173f1f | 1040 | list_for_each_entry(vma, vmas, exec_list) { |
82b6b6d7 | 1041 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
27173f1f | 1042 | struct drm_i915_gem_object *obj = vma->obj; |
69c2fc89 CW |
1043 | u32 old_read = obj->base.read_domains; |
1044 | u32 old_write = obj->base.write_domain; | |
db53a302 | 1045 | |
51bc1404 | 1046 | obj->dirty = 1; /* be paranoid */ |
432e58ed | 1047 | obj->base.write_domain = obj->base.pending_write_domain; |
ed5982e6 DV |
1048 | if (obj->base.write_domain == 0) |
1049 | obj->base.pending_read_domains |= obj->base.read_domains; | |
1050 | obj->base.read_domains = obj->base.pending_read_domains; | |
432e58ed | 1051 | |
b2af0376 | 1052 | i915_vma_move_to_active(vma, req); |
432e58ed | 1053 | if (obj->base.write_domain) { |
97b2a6a1 | 1054 | i915_gem_request_assign(&obj->last_write_req, req); |
f99d7069 | 1055 | |
77a0d1ca | 1056 | intel_fb_obj_invalidate(obj, ORIGIN_CS); |
c8725f3d CW |
1057 | |
1058 | /* update for the implicit flush after a batch */ | |
1059 | obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS; | |
432e58ed | 1060 | } |
82b6b6d7 | 1061 | if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) { |
97b2a6a1 | 1062 | i915_gem_request_assign(&obj->last_fenced_req, req); |
82b6b6d7 CW |
1063 | if (entry->flags & __EXEC_OBJECT_HAS_FENCE) { |
1064 | struct drm_i915_private *dev_priv = to_i915(ring->dev); | |
1065 | list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list, | |
1066 | &dev_priv->mm.fence_list); | |
1067 | } | |
1068 | } | |
432e58ed | 1069 | |
db53a302 | 1070 | trace_i915_gem_object_change_domain(obj, old_read, old_write); |
432e58ed CW |
1071 | } |
1072 | } | |
1073 | ||
ba8b7ccb | 1074 | void |
adeca76d | 1075 | i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params) |
54cf91dc | 1076 | { |
cc889e0f | 1077 | /* Unconditionally force add_request to emit a full flush. */ |
adeca76d | 1078 | params->ring->gpu_caches_dirty = true; |
54cf91dc | 1079 | |
432e58ed | 1080 | /* Add a breadcrumb for the completion of the batch buffer */ |
fcfa423c | 1081 | __i915_add_request(params->request, params->batch_obj, true); |
432e58ed | 1082 | } |
54cf91dc | 1083 | |
ae662d31 EA |
1084 | static int |
1085 | i915_reset_gen7_sol_offsets(struct drm_device *dev, | |
2f20055d | 1086 | struct drm_i915_gem_request *req) |
ae662d31 | 1087 | { |
2f20055d | 1088 | struct intel_engine_cs *ring = req->ring; |
50227e1c | 1089 | struct drm_i915_private *dev_priv = dev->dev_private; |
ae662d31 EA |
1090 | int ret, i; |
1091 | ||
9d662da8 DV |
1092 | if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) { |
1093 | DRM_DEBUG("sol reset is gen7/rcs only\n"); | |
1094 | return -EINVAL; | |
1095 | } | |
ae662d31 | 1096 | |
5fb9de1a | 1097 | ret = intel_ring_begin(req, 4 * 3); |
ae662d31 EA |
1098 | if (ret) |
1099 | return ret; | |
1100 | ||
1101 | for (i = 0; i < 4; i++) { | |
1102 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); | |
1103 | intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i)); | |
1104 | intel_ring_emit(ring, 0); | |
1105 | } | |
1106 | ||
1107 | intel_ring_advance(ring); | |
1108 | ||
1109 | return 0; | |
1110 | } | |
1111 | ||
5c6c6003 | 1112 | static int |
2f20055d | 1113 | i915_emit_box(struct drm_i915_gem_request *req, |
5c6c6003 CW |
1114 | struct drm_clip_rect *box, |
1115 | int DR1, int DR4) | |
1116 | { | |
2f20055d | 1117 | struct intel_engine_cs *ring = req->ring; |
5c6c6003 CW |
1118 | int ret; |
1119 | ||
1120 | if (box->y2 <= box->y1 || box->x2 <= box->x1 || | |
1121 | box->y2 <= 0 || box->x2 <= 0) { | |
1122 | DRM_ERROR("Bad box %d,%d..%d,%d\n", | |
1123 | box->x1, box->y1, box->x2, box->y2); | |
1124 | return -EINVAL; | |
1125 | } | |
1126 | ||
1127 | if (INTEL_INFO(ring->dev)->gen >= 4) { | |
5fb9de1a | 1128 | ret = intel_ring_begin(req, 4); |
5c6c6003 CW |
1129 | if (ret) |
1130 | return ret; | |
1131 | ||
1132 | intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO_I965); | |
1133 | intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16); | |
1134 | intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16); | |
1135 | intel_ring_emit(ring, DR4); | |
1136 | } else { | |
5fb9de1a | 1137 | ret = intel_ring_begin(req, 6); |
5c6c6003 CW |
1138 | if (ret) |
1139 | return ret; | |
1140 | ||
1141 | intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO); | |
1142 | intel_ring_emit(ring, DR1); | |
1143 | intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16); | |
1144 | intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16); | |
1145 | intel_ring_emit(ring, DR4); | |
1146 | intel_ring_emit(ring, 0); | |
1147 | } | |
1148 | intel_ring_advance(ring); | |
1149 | ||
1150 | return 0; | |
1151 | } | |
1152 | ||
71745376 BV |
1153 | static struct drm_i915_gem_object* |
1154 | i915_gem_execbuffer_parse(struct intel_engine_cs *ring, | |
1155 | struct drm_i915_gem_exec_object2 *shadow_exec_entry, | |
1156 | struct eb_vmas *eb, | |
1157 | struct drm_i915_gem_object *batch_obj, | |
1158 | u32 batch_start_offset, | |
1159 | u32 batch_len, | |
17cabf57 | 1160 | bool is_master) |
71745376 | 1161 | { |
71745376 | 1162 | struct drm_i915_gem_object *shadow_batch_obj; |
17cabf57 | 1163 | struct i915_vma *vma; |
71745376 BV |
1164 | int ret; |
1165 | ||
06fbca71 | 1166 | shadow_batch_obj = i915_gem_batch_pool_get(&ring->batch_pool, |
17cabf57 | 1167 | PAGE_ALIGN(batch_len)); |
71745376 BV |
1168 | if (IS_ERR(shadow_batch_obj)) |
1169 | return shadow_batch_obj; | |
1170 | ||
1171 | ret = i915_parse_cmds(ring, | |
1172 | batch_obj, | |
1173 | shadow_batch_obj, | |
1174 | batch_start_offset, | |
1175 | batch_len, | |
1176 | is_master); | |
17cabf57 CW |
1177 | if (ret) |
1178 | goto err; | |
71745376 | 1179 | |
17cabf57 CW |
1180 | ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0); |
1181 | if (ret) | |
1182 | goto err; | |
71745376 | 1183 | |
de4e783a CW |
1184 | i915_gem_object_unpin_pages(shadow_batch_obj); |
1185 | ||
17cabf57 | 1186 | memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry)); |
71745376 | 1187 | |
17cabf57 CW |
1188 | vma = i915_gem_obj_to_ggtt(shadow_batch_obj); |
1189 | vma->exec_entry = shadow_exec_entry; | |
de4e783a | 1190 | vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN; |
17cabf57 CW |
1191 | drm_gem_object_reference(&shadow_batch_obj->base); |
1192 | list_add_tail(&vma->exec_list, &eb->vmas); | |
71745376 | 1193 | |
17cabf57 CW |
1194 | shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND; |
1195 | ||
1196 | return shadow_batch_obj; | |
71745376 | 1197 | |
17cabf57 | 1198 | err: |
de4e783a | 1199 | i915_gem_object_unpin_pages(shadow_batch_obj); |
17cabf57 CW |
1200 | if (ret == -EACCES) /* unhandled chained batch */ |
1201 | return batch_obj; | |
1202 | else | |
1203 | return ERR_PTR(ret); | |
71745376 | 1204 | } |
5c6c6003 | 1205 | |
a83014d3 | 1206 | int |
5f19e2bf | 1207 | i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params, |
a83014d3 | 1208 | struct drm_i915_gem_execbuffer2 *args, |
5f19e2bf | 1209 | struct list_head *vmas) |
78382593 OM |
1210 | { |
1211 | struct drm_clip_rect *cliprects = NULL; | |
5f19e2bf JH |
1212 | struct drm_device *dev = params->dev; |
1213 | struct intel_engine_cs *ring = params->ring; | |
78382593 | 1214 | struct drm_i915_private *dev_priv = dev->dev_private; |
5f19e2bf | 1215 | u64 exec_start, exec_len; |
78382593 OM |
1216 | int instp_mode; |
1217 | u32 instp_mask; | |
1218 | int i, ret = 0; | |
1219 | ||
1220 | if (args->num_cliprects != 0) { | |
1221 | if (ring != &dev_priv->ring[RCS]) { | |
1222 | DRM_DEBUG("clip rectangles are only valid with the render ring\n"); | |
1223 | return -EINVAL; | |
1224 | } | |
1225 | ||
1226 | if (INTEL_INFO(dev)->gen >= 5) { | |
1227 | DRM_DEBUG("clip rectangles are only valid on pre-gen5\n"); | |
1228 | return -EINVAL; | |
1229 | } | |
1230 | ||
1231 | if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) { | |
1232 | DRM_DEBUG("execbuf with %u cliprects\n", | |
1233 | args->num_cliprects); | |
1234 | return -EINVAL; | |
1235 | } | |
1236 | ||
1237 | cliprects = kcalloc(args->num_cliprects, | |
1238 | sizeof(*cliprects), | |
1239 | GFP_KERNEL); | |
1240 | if (cliprects == NULL) { | |
1241 | ret = -ENOMEM; | |
1242 | goto error; | |
1243 | } | |
1244 | ||
1245 | if (copy_from_user(cliprects, | |
1246 | to_user_ptr(args->cliprects_ptr), | |
1247 | sizeof(*cliprects)*args->num_cliprects)) { | |
1248 | ret = -EFAULT; | |
1249 | goto error; | |
1250 | } | |
1251 | } else { | |
1252 | if (args->DR4 == 0xffffffff) { | |
1253 | DRM_DEBUG("UXA submitting garbage DR4, fixing up\n"); | |
1254 | args->DR4 = 0; | |
1255 | } | |
1256 | ||
1257 | if (args->DR1 || args->DR4 || args->cliprects_ptr) { | |
1258 | DRM_DEBUG("0 cliprects but dirt in cliprects fields\n"); | |
1259 | return -EINVAL; | |
1260 | } | |
1261 | } | |
1262 | ||
535fbe82 | 1263 | ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas); |
78382593 OM |
1264 | if (ret) |
1265 | goto error; | |
1266 | ||
ba01cc93 | 1267 | ret = i915_switch_context(params->request); |
78382593 OM |
1268 | if (ret) |
1269 | goto error; | |
1270 | ||
5f19e2bf | 1271 | WARN(params->ctx->ppgtt && params->ctx->ppgtt->pd_dirty_rings & (1<<ring->id), |
9258811c | 1272 | "%s didn't clear reload\n", ring->name); |
563222a7 | 1273 | |
78382593 OM |
1274 | instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK; |
1275 | instp_mask = I915_EXEC_CONSTANTS_MASK; | |
1276 | switch (instp_mode) { | |
1277 | case I915_EXEC_CONSTANTS_REL_GENERAL: | |
1278 | case I915_EXEC_CONSTANTS_ABSOLUTE: | |
1279 | case I915_EXEC_CONSTANTS_REL_SURFACE: | |
1280 | if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) { | |
1281 | DRM_DEBUG("non-0 rel constants mode on non-RCS\n"); | |
1282 | ret = -EINVAL; | |
1283 | goto error; | |
1284 | } | |
1285 | ||
1286 | if (instp_mode != dev_priv->relative_constants_mode) { | |
1287 | if (INTEL_INFO(dev)->gen < 4) { | |
1288 | DRM_DEBUG("no rel constants on pre-gen4\n"); | |
1289 | ret = -EINVAL; | |
1290 | goto error; | |
1291 | } | |
1292 | ||
1293 | if (INTEL_INFO(dev)->gen > 5 && | |
1294 | instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) { | |
1295 | DRM_DEBUG("rel surface constants mode invalid on gen5+\n"); | |
1296 | ret = -EINVAL; | |
1297 | goto error; | |
1298 | } | |
1299 | ||
1300 | /* The HW changed the meaning on this bit on gen6 */ | |
1301 | if (INTEL_INFO(dev)->gen >= 6) | |
1302 | instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE; | |
1303 | } | |
1304 | break; | |
1305 | default: | |
1306 | DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode); | |
1307 | ret = -EINVAL; | |
1308 | goto error; | |
1309 | } | |
1310 | ||
1311 | if (ring == &dev_priv->ring[RCS] && | |
1312 | instp_mode != dev_priv->relative_constants_mode) { | |
5fb9de1a | 1313 | ret = intel_ring_begin(params->request, 4); |
78382593 OM |
1314 | if (ret) |
1315 | goto error; | |
1316 | ||
1317 | intel_ring_emit(ring, MI_NOOP); | |
1318 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); | |
1319 | intel_ring_emit(ring, INSTPM); | |
1320 | intel_ring_emit(ring, instp_mask << 16 | instp_mode); | |
1321 | intel_ring_advance(ring); | |
1322 | ||
1323 | dev_priv->relative_constants_mode = instp_mode; | |
1324 | } | |
1325 | ||
1326 | if (args->flags & I915_EXEC_GEN7_SOL_RESET) { | |
2f20055d | 1327 | ret = i915_reset_gen7_sol_offsets(dev, params->request); |
78382593 OM |
1328 | if (ret) |
1329 | goto error; | |
1330 | } | |
1331 | ||
5f19e2bf JH |
1332 | exec_len = args->batch_len; |
1333 | exec_start = params->batch_obj_vm_offset + | |
1334 | params->args_batch_start_offset; | |
1335 | ||
78382593 OM |
1336 | if (cliprects) { |
1337 | for (i = 0; i < args->num_cliprects; i++) { | |
2f20055d | 1338 | ret = i915_emit_box(params->request, &cliprects[i], |
78382593 OM |
1339 | args->DR1, args->DR4); |
1340 | if (ret) | |
1341 | goto error; | |
1342 | ||
53fddaf7 | 1343 | ret = ring->dispatch_execbuffer(params->request, |
78382593 | 1344 | exec_start, exec_len, |
5f19e2bf | 1345 | params->dispatch_flags); |
78382593 OM |
1346 | if (ret) |
1347 | goto error; | |
1348 | } | |
1349 | } else { | |
53fddaf7 | 1350 | ret = ring->dispatch_execbuffer(params->request, |
78382593 | 1351 | exec_start, exec_len, |
5f19e2bf | 1352 | params->dispatch_flags); |
78382593 OM |
1353 | if (ret) |
1354 | return ret; | |
1355 | } | |
1356 | ||
95c24161 | 1357 | trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags); |
78382593 | 1358 | |
8a8edb59 | 1359 | i915_gem_execbuffer_move_to_active(vmas, params->request); |
adeca76d | 1360 | i915_gem_execbuffer_retire_commands(params); |
78382593 OM |
1361 | |
1362 | error: | |
1363 | kfree(cliprects); | |
1364 | return ret; | |
1365 | } | |
1366 | ||
a8ebba75 ZY |
1367 | /** |
1368 | * Find one BSD ring to dispatch the corresponding BSD command. | |
1369 | * The Ring ID is returned. | |
1370 | */ | |
1371 | static int gen8_dispatch_bsd_ring(struct drm_device *dev, | |
1372 | struct drm_file *file) | |
1373 | { | |
1374 | struct drm_i915_private *dev_priv = dev->dev_private; | |
1375 | struct drm_i915_file_private *file_priv = file->driver_priv; | |
1376 | ||
1377 | /* Check whether the file_priv is using one ring */ | |
1378 | if (file_priv->bsd_ring) | |
1379 | return file_priv->bsd_ring->id; | |
1380 | else { | |
1381 | /* If no, use the ping-pong mechanism to select one ring */ | |
1382 | int ring_id; | |
1383 | ||
1384 | mutex_lock(&dev->struct_mutex); | |
bdf1e7e3 | 1385 | if (dev_priv->mm.bsd_ring_dispatch_index == 0) { |
a8ebba75 | 1386 | ring_id = VCS; |
bdf1e7e3 | 1387 | dev_priv->mm.bsd_ring_dispatch_index = 1; |
a8ebba75 ZY |
1388 | } else { |
1389 | ring_id = VCS2; | |
bdf1e7e3 | 1390 | dev_priv->mm.bsd_ring_dispatch_index = 0; |
a8ebba75 ZY |
1391 | } |
1392 | file_priv->bsd_ring = &dev_priv->ring[ring_id]; | |
1393 | mutex_unlock(&dev->struct_mutex); | |
1394 | return ring_id; | |
1395 | } | |
1396 | } | |
1397 | ||
d23db88c CW |
1398 | static struct drm_i915_gem_object * |
1399 | eb_get_batch(struct eb_vmas *eb) | |
1400 | { | |
1401 | struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list); | |
1402 | ||
1403 | /* | |
1404 | * SNA is doing fancy tricks with compressing batch buffers, which leads | |
1405 | * to negative relocation deltas. Usually that works out ok since the | |
1406 | * relocate address is still positive, except when the batch is placed | |
1407 | * very low in the GTT. Ensure this doesn't happen. | |
1408 | * | |
1409 | * Note that actual hangs have only been observed on gen7, but for | |
1410 | * paranoia do it everywhere. | |
1411 | */ | |
1412 | vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS; | |
1413 | ||
1414 | return vma->obj; | |
1415 | } | |
1416 | ||
54cf91dc CW |
1417 | static int |
1418 | i915_gem_do_execbuffer(struct drm_device *dev, void *data, | |
1419 | struct drm_file *file, | |
1420 | struct drm_i915_gem_execbuffer2 *args, | |
41bde553 | 1421 | struct drm_i915_gem_exec_object2 *exec) |
54cf91dc | 1422 | { |
50227e1c | 1423 | struct drm_i915_private *dev_priv = dev->dev_private; |
27173f1f | 1424 | struct eb_vmas *eb; |
54cf91dc | 1425 | struct drm_i915_gem_object *batch_obj; |
78a42377 | 1426 | struct drm_i915_gem_exec_object2 shadow_exec_entry; |
a4872ba6 | 1427 | struct intel_engine_cs *ring; |
273497e5 | 1428 | struct intel_context *ctx; |
41bde553 | 1429 | struct i915_address_space *vm; |
5f19e2bf JH |
1430 | struct i915_execbuffer_params params_master; /* XXX: will be removed later */ |
1431 | struct i915_execbuffer_params *params = ¶ms_master; | |
d299cce7 | 1432 | const u32 ctx_id = i915_execbuffer2_get_context_id(*args); |
8e004efc | 1433 | u32 dispatch_flags; |
78382593 | 1434 | int ret; |
ed5982e6 | 1435 | bool need_relocs; |
54cf91dc | 1436 | |
ed5982e6 | 1437 | if (!i915_gem_check_execbuffer(args)) |
432e58ed | 1438 | return -EINVAL; |
432e58ed | 1439 | |
ad19f10b | 1440 | ret = validate_exec_list(dev, exec, args->buffer_count); |
54cf91dc CW |
1441 | if (ret) |
1442 | return ret; | |
1443 | ||
8e004efc | 1444 | dispatch_flags = 0; |
d7d4eedd CW |
1445 | if (args->flags & I915_EXEC_SECURE) { |
1446 | if (!file->is_master || !capable(CAP_SYS_ADMIN)) | |
1447 | return -EPERM; | |
1448 | ||
8e004efc | 1449 | dispatch_flags |= I915_DISPATCH_SECURE; |
d7d4eedd | 1450 | } |
b45305fc | 1451 | if (args->flags & I915_EXEC_IS_PINNED) |
8e004efc | 1452 | dispatch_flags |= I915_DISPATCH_PINNED; |
d7d4eedd | 1453 | |
b1a93306 | 1454 | if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) { |
ff240199 | 1455 | DRM_DEBUG("execbuf with unknown ring: %d\n", |
54cf91dc CW |
1456 | (int)(args->flags & I915_EXEC_RING_MASK)); |
1457 | return -EINVAL; | |
1458 | } | |
ca01b12b | 1459 | |
8d360dff ZG |
1460 | if (((args->flags & I915_EXEC_RING_MASK) != I915_EXEC_BSD) && |
1461 | ((args->flags & I915_EXEC_BSD_MASK) != 0)) { | |
1462 | DRM_DEBUG("execbuf with non bsd ring but with invalid " | |
1463 | "bsd dispatch flags: %d\n", (int)(args->flags)); | |
1464 | return -EINVAL; | |
1465 | } | |
1466 | ||
ca01b12b BW |
1467 | if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT) |
1468 | ring = &dev_priv->ring[RCS]; | |
a8ebba75 ZY |
1469 | else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) { |
1470 | if (HAS_BSD2(dev)) { | |
1471 | int ring_id; | |
8d360dff ZG |
1472 | |
1473 | switch (args->flags & I915_EXEC_BSD_MASK) { | |
1474 | case I915_EXEC_BSD_DEFAULT: | |
1475 | ring_id = gen8_dispatch_bsd_ring(dev, file); | |
1476 | ring = &dev_priv->ring[ring_id]; | |
1477 | break; | |
1478 | case I915_EXEC_BSD_RING1: | |
1479 | ring = &dev_priv->ring[VCS]; | |
1480 | break; | |
1481 | case I915_EXEC_BSD_RING2: | |
1482 | ring = &dev_priv->ring[VCS2]; | |
1483 | break; | |
1484 | default: | |
1485 | DRM_DEBUG("execbuf with unknown bsd ring: %d\n", | |
1486 | (int)(args->flags & I915_EXEC_BSD_MASK)); | |
1487 | return -EINVAL; | |
1488 | } | |
a8ebba75 ZY |
1489 | } else |
1490 | ring = &dev_priv->ring[VCS]; | |
1491 | } else | |
ca01b12b BW |
1492 | ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1]; |
1493 | ||
a15817cf CW |
1494 | if (!intel_ring_initialized(ring)) { |
1495 | DRM_DEBUG("execbuf with invalid ring: %d\n", | |
1496 | (int)(args->flags & I915_EXEC_RING_MASK)); | |
1497 | return -EINVAL; | |
1498 | } | |
54cf91dc CW |
1499 | |
1500 | if (args->buffer_count < 1) { | |
ff240199 | 1501 | DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count); |
54cf91dc CW |
1502 | return -EINVAL; |
1503 | } | |
54cf91dc | 1504 | |
a9ed33ca AJ |
1505 | if (args->flags & I915_EXEC_RESOURCE_STREAMER) { |
1506 | if (!HAS_RESOURCE_STREAMER(dev)) { | |
1507 | DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n"); | |
1508 | return -EINVAL; | |
1509 | } | |
1510 | if (ring->id != RCS) { | |
1511 | DRM_DEBUG("RS is not available on %s\n", | |
1512 | ring->name); | |
1513 | return -EINVAL; | |
1514 | } | |
1515 | ||
1516 | dispatch_flags |= I915_DISPATCH_RS; | |
1517 | } | |
1518 | ||
f65c9168 PZ |
1519 | intel_runtime_pm_get(dev_priv); |
1520 | ||
54cf91dc CW |
1521 | ret = i915_mutex_lock_interruptible(dev); |
1522 | if (ret) | |
1523 | goto pre_mutex_err; | |
1524 | ||
7c9c4b8f | 1525 | ctx = i915_gem_validate_context(dev, file, ring, ctx_id); |
72ad5c45 | 1526 | if (IS_ERR(ctx)) { |
d299cce7 | 1527 | mutex_unlock(&dev->struct_mutex); |
41bde553 | 1528 | ret = PTR_ERR(ctx); |
d299cce7 | 1529 | goto pre_mutex_err; |
935f38d6 | 1530 | } |
41bde553 BW |
1531 | |
1532 | i915_gem_context_reference(ctx); | |
1533 | ||
ae6c4806 DV |
1534 | if (ctx->ppgtt) |
1535 | vm = &ctx->ppgtt->base; | |
1536 | else | |
7e0d96bc | 1537 | vm = &dev_priv->gtt.base; |
d299cce7 | 1538 | |
5f19e2bf JH |
1539 | memset(¶ms_master, 0x00, sizeof(params_master)); |
1540 | ||
17601cbc | 1541 | eb = eb_create(args); |
67731b87 | 1542 | if (eb == NULL) { |
935f38d6 | 1543 | i915_gem_context_unreference(ctx); |
67731b87 CW |
1544 | mutex_unlock(&dev->struct_mutex); |
1545 | ret = -ENOMEM; | |
1546 | goto pre_mutex_err; | |
1547 | } | |
1548 | ||
54cf91dc | 1549 | /* Look up object handles */ |
27173f1f | 1550 | ret = eb_lookup_vmas(eb, exec, args, vm, file); |
3b96eff4 CW |
1551 | if (ret) |
1552 | goto err; | |
54cf91dc | 1553 | |
6fe4f140 | 1554 | /* take note of the batch buffer before we might reorder the lists */ |
d23db88c | 1555 | batch_obj = eb_get_batch(eb); |
6fe4f140 | 1556 | |
54cf91dc | 1557 | /* Move the objects en-masse into the GTT, evicting if necessary. */ |
ed5982e6 | 1558 | need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0; |
b1b38278 | 1559 | ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs); |
54cf91dc CW |
1560 | if (ret) |
1561 | goto err; | |
1562 | ||
1563 | /* The objects are in their final locations, apply the relocations. */ | |
ed5982e6 | 1564 | if (need_relocs) |
17601cbc | 1565 | ret = i915_gem_execbuffer_relocate(eb); |
54cf91dc CW |
1566 | if (ret) { |
1567 | if (ret == -EFAULT) { | |
ed5982e6 | 1568 | ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring, |
b1b38278 | 1569 | eb, exec, ctx); |
54cf91dc CW |
1570 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
1571 | } | |
1572 | if (ret) | |
1573 | goto err; | |
1574 | } | |
1575 | ||
1576 | /* Set the pending read domains for the batch buffer to COMMAND */ | |
54cf91dc | 1577 | if (batch_obj->base.pending_write_domain) { |
ff240199 | 1578 | DRM_DEBUG("Attempting to use self-modifying batch buffer\n"); |
54cf91dc CW |
1579 | ret = -EINVAL; |
1580 | goto err; | |
1581 | } | |
54cf91dc | 1582 | |
5f19e2bf | 1583 | params->args_batch_start_offset = args->batch_start_offset; |
743e78c1 | 1584 | if (i915_needs_cmd_parser(ring) && args->batch_len) { |
c7c7372e RP |
1585 | struct drm_i915_gem_object *parsed_batch_obj; |
1586 | ||
1587 | parsed_batch_obj = i915_gem_execbuffer_parse(ring, | |
71745376 BV |
1588 | &shadow_exec_entry, |
1589 | eb, | |
1590 | batch_obj, | |
1591 | args->batch_start_offset, | |
1592 | args->batch_len, | |
17cabf57 | 1593 | file->is_master); |
c7c7372e RP |
1594 | if (IS_ERR(parsed_batch_obj)) { |
1595 | ret = PTR_ERR(parsed_batch_obj); | |
78a42377 BV |
1596 | goto err; |
1597 | } | |
17cabf57 CW |
1598 | |
1599 | /* | |
c7c7372e RP |
1600 | * parsed_batch_obj == batch_obj means batch not fully parsed: |
1601 | * Accept, but don't promote to secure. | |
17cabf57 | 1602 | */ |
17cabf57 | 1603 | |
c7c7372e RP |
1604 | if (parsed_batch_obj != batch_obj) { |
1605 | /* | |
1606 | * Batch parsed and accepted: | |
1607 | * | |
1608 | * Set the DISPATCH_SECURE bit to remove the NON_SECURE | |
1609 | * bit from MI_BATCH_BUFFER_START commands issued in | |
1610 | * the dispatch_execbuffer implementations. We | |
1611 | * specifically don't want that set on batches the | |
1612 | * command parser has accepted. | |
1613 | */ | |
1614 | dispatch_flags |= I915_DISPATCH_SECURE; | |
5f19e2bf | 1615 | params->args_batch_start_offset = 0; |
c7c7372e RP |
1616 | batch_obj = parsed_batch_obj; |
1617 | } | |
351e3db2 BV |
1618 | } |
1619 | ||
78a42377 BV |
1620 | batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND; |
1621 | ||
d7d4eedd CW |
1622 | /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure |
1623 | * batch" bit. Hence we need to pin secure batches into the global gtt. | |
28cf5415 | 1624 | * hsw should have this fixed, but bdw mucks it up again. */ |
8e004efc | 1625 | if (dispatch_flags & I915_DISPATCH_SECURE) { |
da51a1e7 DV |
1626 | /* |
1627 | * So on first glance it looks freaky that we pin the batch here | |
1628 | * outside of the reservation loop. But: | |
1629 | * - The batch is already pinned into the relevant ppgtt, so we | |
1630 | * already have the backing storage fully allocated. | |
1631 | * - No other BO uses the global gtt (well contexts, but meh), | |
fd0753cf | 1632 | * so we don't really have issues with multiple objects not |
da51a1e7 DV |
1633 | * fitting due to fragmentation. |
1634 | * So this is actually safe. | |
1635 | */ | |
1636 | ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0); | |
1637 | if (ret) | |
1638 | goto err; | |
d7d4eedd | 1639 | |
5f19e2bf | 1640 | params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj); |
da51a1e7 | 1641 | } else |
5f19e2bf | 1642 | params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm); |
d7d4eedd | 1643 | |
0c8dac88 | 1644 | /* Allocate a request for this batch buffer nice and early. */ |
6a6ae79a | 1645 | ret = i915_gem_request_alloc(ring, ctx, ¶ms->request); |
0c8dac88 JH |
1646 | if (ret) |
1647 | goto err_batch_unpin; | |
1648 | ||
fcfa423c JH |
1649 | ret = i915_gem_request_add_to_client(params->request, file); |
1650 | if (ret) | |
1651 | goto err_batch_unpin; | |
1652 | ||
5f19e2bf JH |
1653 | /* |
1654 | * Save assorted stuff away to pass through to *_submission(). | |
1655 | * NB: This data should be 'persistent' and not local as it will | |
1656 | * kept around beyond the duration of the IOCTL once the GPU | |
1657 | * scheduler arrives. | |
1658 | */ | |
1659 | params->dev = dev; | |
1660 | params->file = file; | |
1661 | params->ring = ring; | |
1662 | params->dispatch_flags = dispatch_flags; | |
1663 | params->batch_obj = batch_obj; | |
1664 | params->ctx = ctx; | |
1665 | ||
1666 | ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas); | |
54cf91dc | 1667 | |
0c8dac88 | 1668 | err_batch_unpin: |
da51a1e7 DV |
1669 | /* |
1670 | * FIXME: We crucially rely upon the active tracking for the (ppgtt) | |
1671 | * batch vma for correctness. For less ugly and less fragility this | |
1672 | * needs to be adjusted to also track the ggtt batch vma properly as | |
1673 | * active. | |
1674 | */ | |
8e004efc | 1675 | if (dispatch_flags & I915_DISPATCH_SECURE) |
da51a1e7 | 1676 | i915_gem_object_ggtt_unpin(batch_obj); |
0c8dac88 | 1677 | |
54cf91dc | 1678 | err: |
41bde553 BW |
1679 | /* the request owns the ref now */ |
1680 | i915_gem_context_unreference(ctx); | |
67731b87 | 1681 | eb_destroy(eb); |
54cf91dc | 1682 | |
6a6ae79a JH |
1683 | /* |
1684 | * If the request was created but not successfully submitted then it | |
1685 | * must be freed again. If it was submitted then it is being tracked | |
1686 | * on the active request list and no clean up is required here. | |
1687 | */ | |
bccca494 | 1688 | if (ret && params->request) |
6a6ae79a | 1689 | i915_gem_request_cancel(params->request); |
6a6ae79a | 1690 | |
54cf91dc CW |
1691 | mutex_unlock(&dev->struct_mutex); |
1692 | ||
1693 | pre_mutex_err: | |
f65c9168 PZ |
1694 | /* intel_gpu_busy should also get a ref, so it will free when the device |
1695 | * is really idle. */ | |
1696 | intel_runtime_pm_put(dev_priv); | |
54cf91dc CW |
1697 | return ret; |
1698 | } | |
1699 | ||
1700 | /* | |
1701 | * Legacy execbuffer just creates an exec2 list from the original exec object | |
1702 | * list array and passes it to the real function. | |
1703 | */ | |
1704 | int | |
1705 | i915_gem_execbuffer(struct drm_device *dev, void *data, | |
1706 | struct drm_file *file) | |
1707 | { | |
1708 | struct drm_i915_gem_execbuffer *args = data; | |
1709 | struct drm_i915_gem_execbuffer2 exec2; | |
1710 | struct drm_i915_gem_exec_object *exec_list = NULL; | |
1711 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; | |
1712 | int ret, i; | |
1713 | ||
54cf91dc | 1714 | if (args->buffer_count < 1) { |
ff240199 | 1715 | DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count); |
54cf91dc CW |
1716 | return -EINVAL; |
1717 | } | |
1718 | ||
1719 | /* Copy in the exec list from userland */ | |
1720 | exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count); | |
1721 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); | |
1722 | if (exec_list == NULL || exec2_list == NULL) { | |
ff240199 | 1723 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
54cf91dc CW |
1724 | args->buffer_count); |
1725 | drm_free_large(exec_list); | |
1726 | drm_free_large(exec2_list); | |
1727 | return -ENOMEM; | |
1728 | } | |
1729 | ret = copy_from_user(exec_list, | |
2bb4629a | 1730 | to_user_ptr(args->buffers_ptr), |
54cf91dc CW |
1731 | sizeof(*exec_list) * args->buffer_count); |
1732 | if (ret != 0) { | |
ff240199 | 1733 | DRM_DEBUG("copy %d exec entries failed %d\n", |
54cf91dc CW |
1734 | args->buffer_count, ret); |
1735 | drm_free_large(exec_list); | |
1736 | drm_free_large(exec2_list); | |
1737 | return -EFAULT; | |
1738 | } | |
1739 | ||
1740 | for (i = 0; i < args->buffer_count; i++) { | |
1741 | exec2_list[i].handle = exec_list[i].handle; | |
1742 | exec2_list[i].relocation_count = exec_list[i].relocation_count; | |
1743 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; | |
1744 | exec2_list[i].alignment = exec_list[i].alignment; | |
1745 | exec2_list[i].offset = exec_list[i].offset; | |
1746 | if (INTEL_INFO(dev)->gen < 4) | |
1747 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; | |
1748 | else | |
1749 | exec2_list[i].flags = 0; | |
1750 | } | |
1751 | ||
1752 | exec2.buffers_ptr = args->buffers_ptr; | |
1753 | exec2.buffer_count = args->buffer_count; | |
1754 | exec2.batch_start_offset = args->batch_start_offset; | |
1755 | exec2.batch_len = args->batch_len; | |
1756 | exec2.DR1 = args->DR1; | |
1757 | exec2.DR4 = args->DR4; | |
1758 | exec2.num_cliprects = args->num_cliprects; | |
1759 | exec2.cliprects_ptr = args->cliprects_ptr; | |
1760 | exec2.flags = I915_EXEC_RENDER; | |
6e0a69db | 1761 | i915_execbuffer2_set_context_id(exec2, 0); |
54cf91dc | 1762 | |
41bde553 | 1763 | ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list); |
54cf91dc | 1764 | if (!ret) { |
9aab8bff CW |
1765 | struct drm_i915_gem_exec_object __user *user_exec_list = |
1766 | to_user_ptr(args->buffers_ptr); | |
1767 | ||
54cf91dc | 1768 | /* Copy the new buffer offsets back to the user's exec list. */ |
9aab8bff CW |
1769 | for (i = 0; i < args->buffer_count; i++) { |
1770 | ret = __copy_to_user(&user_exec_list[i].offset, | |
1771 | &exec2_list[i].offset, | |
1772 | sizeof(user_exec_list[i].offset)); | |
1773 | if (ret) { | |
1774 | ret = -EFAULT; | |
1775 | DRM_DEBUG("failed to copy %d exec entries " | |
1776 | "back to user (%d)\n", | |
1777 | args->buffer_count, ret); | |
1778 | break; | |
1779 | } | |
54cf91dc CW |
1780 | } |
1781 | } | |
1782 | ||
1783 | drm_free_large(exec_list); | |
1784 | drm_free_large(exec2_list); | |
1785 | return ret; | |
1786 | } | |
1787 | ||
1788 | int | |
1789 | i915_gem_execbuffer2(struct drm_device *dev, void *data, | |
1790 | struct drm_file *file) | |
1791 | { | |
1792 | struct drm_i915_gem_execbuffer2 *args = data; | |
1793 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; | |
1794 | int ret; | |
1795 | ||
ed8cd3b2 XW |
1796 | if (args->buffer_count < 1 || |
1797 | args->buffer_count > UINT_MAX / sizeof(*exec2_list)) { | |
ff240199 | 1798 | DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count); |
54cf91dc CW |
1799 | return -EINVAL; |
1800 | } | |
1801 | ||
9cb34664 DV |
1802 | if (args->rsvd2 != 0) { |
1803 | DRM_DEBUG("dirty rvsd2 field\n"); | |
1804 | return -EINVAL; | |
1805 | } | |
1806 | ||
8408c282 | 1807 | exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count, |
419fa72a | 1808 | GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); |
8408c282 CW |
1809 | if (exec2_list == NULL) |
1810 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), | |
1811 | args->buffer_count); | |
54cf91dc | 1812 | if (exec2_list == NULL) { |
ff240199 | 1813 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
54cf91dc CW |
1814 | args->buffer_count); |
1815 | return -ENOMEM; | |
1816 | } | |
1817 | ret = copy_from_user(exec2_list, | |
2bb4629a | 1818 | to_user_ptr(args->buffers_ptr), |
54cf91dc CW |
1819 | sizeof(*exec2_list) * args->buffer_count); |
1820 | if (ret != 0) { | |
ff240199 | 1821 | DRM_DEBUG("copy %d exec entries failed %d\n", |
54cf91dc CW |
1822 | args->buffer_count, ret); |
1823 | drm_free_large(exec2_list); | |
1824 | return -EFAULT; | |
1825 | } | |
1826 | ||
41bde553 | 1827 | ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list); |
54cf91dc CW |
1828 | if (!ret) { |
1829 | /* Copy the new buffer offsets back to the user's exec list. */ | |
d593d992 | 1830 | struct drm_i915_gem_exec_object2 __user *user_exec_list = |
9aab8bff CW |
1831 | to_user_ptr(args->buffers_ptr); |
1832 | int i; | |
1833 | ||
1834 | for (i = 0; i < args->buffer_count; i++) { | |
1835 | ret = __copy_to_user(&user_exec_list[i].offset, | |
1836 | &exec2_list[i].offset, | |
1837 | sizeof(user_exec_list[i].offset)); | |
1838 | if (ret) { | |
1839 | ret = -EFAULT; | |
1840 | DRM_DEBUG("failed to copy %d exec entries " | |
1841 | "back to user\n", | |
1842 | args->buffer_count); | |
1843 | break; | |
1844 | } | |
54cf91dc CW |
1845 | } |
1846 | } | |
1847 | ||
1848 | drm_free_large(exec2_list); | |
1849 | return ret; | |
1850 | } |