Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 29 | #include <drm/drmP.h> |
| 30 | #include <drm/i915_drm.h> |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 31 | #include "i915_drv.h" |
| 32 | #include "i915_trace.h" |
| 33 | #include "intel_drv.h" |
Eugeni Dodonov | f45b555 | 2011-12-09 17:16:37 -0800 | [diff] [blame] | 34 | #include <linux/dma_remapping.h> |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 35 | |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 36 | #define __EXEC_OBJECT_HAS_PIN (1<<31) |
| 37 | #define __EXEC_OBJECT_HAS_FENCE (1<<30) |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 38 | #define __EXEC_OBJECT_NEEDS_MAP (1<<29) |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 39 | #define __EXEC_OBJECT_NEEDS_BIAS (1<<28) |
| 40 | |
| 41 | #define BATCH_OFFSET_BIAS (256*1024) |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 42 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 43 | struct eb_vmas { |
| 44 | struct list_head vmas; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 45 | int and; |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 46 | union { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 47 | struct i915_vma *lut[0]; |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 48 | struct hlist_head buckets[0]; |
| 49 | }; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 52 | static struct eb_vmas * |
Ben Widawsky | 17601cbc | 2013-11-25 09:54:38 -0800 | [diff] [blame] | 53 | eb_create(struct drm_i915_gem_execbuffer2 *args) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 54 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 55 | struct eb_vmas *eb = NULL; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 56 | |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 57 | if (args->flags & I915_EXEC_HANDLE_LUT) { |
Daniel Vetter | b205ca5 | 2013-09-19 14:00:11 +0200 | [diff] [blame] | 58 | unsigned size = args->buffer_count; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 59 | size *= sizeof(struct i915_vma *); |
| 60 | size += sizeof(struct eb_vmas); |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 61 | eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); |
| 62 | } |
| 63 | |
| 64 | if (eb == NULL) { |
Daniel Vetter | b205ca5 | 2013-09-19 14:00:11 +0200 | [diff] [blame] | 65 | unsigned size = args->buffer_count; |
| 66 | unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2; |
Lauri Kasanen | 27b7c63 | 2013-03-27 15:04:55 +0200 | [diff] [blame] | 67 | BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head)); |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 68 | while (count > 2*size) |
| 69 | count >>= 1; |
| 70 | eb = kzalloc(count*sizeof(struct hlist_head) + |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 71 | sizeof(struct eb_vmas), |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 72 | GFP_TEMPORARY); |
| 73 | if (eb == NULL) |
| 74 | return eb; |
| 75 | |
| 76 | eb->and = count - 1; |
| 77 | } else |
| 78 | eb->and = -args->buffer_count; |
| 79 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 80 | INIT_LIST_HEAD(&eb->vmas); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 81 | return eb; |
| 82 | } |
| 83 | |
| 84 | static void |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 85 | eb_reset(struct eb_vmas *eb) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 86 | { |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 87 | if (eb->and >= 0) |
| 88 | memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head)); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 91 | static int |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 92 | eb_lookup_vmas(struct eb_vmas *eb, |
| 93 | struct drm_i915_gem_exec_object2 *exec, |
| 94 | const struct drm_i915_gem_execbuffer2 *args, |
| 95 | struct i915_address_space *vm, |
| 96 | struct drm_file *file) |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 97 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 98 | struct drm_i915_gem_object *obj; |
| 99 | struct list_head objects; |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 100 | int i, ret; |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 101 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 102 | INIT_LIST_HEAD(&objects); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 103 | spin_lock(&file->table_lock); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 104 | /* Grab a reference to the object and release the lock so we can lookup |
| 105 | * or create the VMA without using GFP_ATOMIC */ |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 106 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 107 | obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle)); |
| 108 | if (obj == NULL) { |
| 109 | spin_unlock(&file->table_lock); |
| 110 | DRM_DEBUG("Invalid object handle %d at index %d\n", |
| 111 | exec[i].handle, i); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 112 | ret = -ENOENT; |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 113 | goto err; |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 116 | if (!list_empty(&obj->obj_exec_link)) { |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 117 | spin_unlock(&file->table_lock); |
| 118 | DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n", |
| 119 | obj, exec[i].handle, i); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 120 | ret = -EINVAL; |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 121 | goto err; |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | drm_gem_object_reference(&obj->base); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 125 | list_add_tail(&obj->obj_exec_link, &objects); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 126 | } |
| 127 | spin_unlock(&file->table_lock); |
| 128 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 129 | i = 0; |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 130 | while (!list_empty(&objects)) { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 131 | struct i915_vma *vma; |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 132 | |
Daniel Vetter | 2c9f8d5 | 2013-12-18 17:38:53 +0100 | [diff] [blame] | 133 | if (exec[i].flags & EXEC_OBJECT_NEEDS_GTT && |
| 134 | USES_FULL_PPGTT(vm->dev)) { |
| 135 | ret = -EINVAL; |
Rodrigo Vivi | a25eebb | 2014-01-14 16:21:49 -0200 | [diff] [blame] | 136 | goto err; |
Daniel Vetter | 2c9f8d5 | 2013-12-18 17:38:53 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 139 | obj = list_first_entry(&objects, |
| 140 | struct drm_i915_gem_object, |
| 141 | obj_exec_link); |
| 142 | |
Daniel Vetter | e656a6c | 2013-08-14 14:14:04 +0200 | [diff] [blame] | 143 | /* |
| 144 | * NOTE: We can leak any vmas created here when something fails |
| 145 | * later on. But that's no issue since vma_unbind can deal with |
| 146 | * vmas which are not actually bound. And since only |
| 147 | * lookup_or_create exists as an interface to get at the vma |
| 148 | * from the (obj, vm) we don't run the risk of creating |
| 149 | * duplicated vmas for the same vm. |
| 150 | */ |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame^] | 151 | vma = i915_gem_obj_lookup_or_create_vma(obj, vm); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 152 | if (IS_ERR(vma)) { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 153 | DRM_DEBUG("Failed to lookup VMA\n"); |
| 154 | ret = PTR_ERR(vma); |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 155 | goto err; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 156 | } |
| 157 | |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 158 | /* Transfer ownership from the objects list to the vmas list. */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 159 | list_add_tail(&vma->exec_list, &eb->vmas); |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 160 | list_del_init(&obj->obj_exec_link); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 161 | |
| 162 | vma->exec_entry = &exec[i]; |
| 163 | if (eb->and < 0) { |
| 164 | eb->lut[i] = vma; |
| 165 | } else { |
| 166 | uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle; |
| 167 | vma->exec_handle = handle; |
| 168 | hlist_add_head(&vma->exec_node, |
| 169 | &eb->buckets[handle & eb->and]); |
| 170 | } |
| 171 | ++i; |
| 172 | } |
| 173 | |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 174 | return 0; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 175 | |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 176 | |
| 177 | err: |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 178 | while (!list_empty(&objects)) { |
| 179 | obj = list_first_entry(&objects, |
| 180 | struct drm_i915_gem_object, |
| 181 | obj_exec_link); |
| 182 | list_del_init(&obj->obj_exec_link); |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 183 | drm_gem_object_unreference(&obj->base); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 184 | } |
Chris Wilson | 9ae9ab5 | 2013-12-04 09:52:58 +0000 | [diff] [blame] | 185 | /* |
| 186 | * Objects already transfered to the vmas list will be unreferenced by |
| 187 | * eb_destroy. |
| 188 | */ |
| 189 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 190 | return ret; |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 193 | static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 194 | { |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 195 | if (eb->and < 0) { |
| 196 | if (handle >= -eb->and) |
| 197 | return NULL; |
| 198 | return eb->lut[handle]; |
| 199 | } else { |
| 200 | struct hlist_head *head; |
| 201 | struct hlist_node *node; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 202 | |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 203 | head = &eb->buckets[handle & eb->and]; |
| 204 | hlist_for_each(node, head) { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 205 | struct i915_vma *vma; |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 206 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 207 | vma = hlist_entry(node, struct i915_vma, exec_node); |
| 208 | if (vma->exec_handle == handle) |
| 209 | return vma; |
Chris Wilson | eef90cc | 2013-01-08 10:53:17 +0000 | [diff] [blame] | 210 | } |
| 211 | return NULL; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 212 | } |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 215 | static void |
| 216 | i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma) |
| 217 | { |
| 218 | struct drm_i915_gem_exec_object2 *entry; |
| 219 | struct drm_i915_gem_object *obj = vma->obj; |
| 220 | |
| 221 | if (!drm_mm_node_allocated(&vma->node)) |
| 222 | return; |
| 223 | |
| 224 | entry = vma->exec_entry; |
| 225 | |
| 226 | if (entry->flags & __EXEC_OBJECT_HAS_FENCE) |
| 227 | i915_gem_object_unpin_fence(obj); |
| 228 | |
| 229 | if (entry->flags & __EXEC_OBJECT_HAS_PIN) |
Daniel Vetter | 3d7f0f9 | 2013-12-18 16:23:37 +0100 | [diff] [blame] | 230 | vma->pin_count--; |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 231 | |
| 232 | entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN); |
| 233 | } |
| 234 | |
| 235 | static void eb_destroy(struct eb_vmas *eb) |
| 236 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 237 | while (!list_empty(&eb->vmas)) { |
| 238 | struct i915_vma *vma; |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 239 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 240 | vma = list_first_entry(&eb->vmas, |
| 241 | struct i915_vma, |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 242 | exec_list); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 243 | list_del_init(&vma->exec_list); |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 244 | i915_gem_execbuffer_unreserve_vma(vma); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 245 | drm_gem_object_unreference(&vma->obj->base); |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 246 | } |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 247 | kfree(eb); |
| 248 | } |
| 249 | |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 250 | static inline int use_cpu_reloc(struct drm_i915_gem_object *obj) |
| 251 | { |
Chris Wilson | 2cc86b8 | 2013-08-26 19:51:00 -0300 | [diff] [blame] | 252 | return (HAS_LLC(obj->base.dev) || |
| 253 | obj->base.write_domain == I915_GEM_DOMAIN_CPU || |
Chris Wilson | 504c726 | 2012-08-23 13:12:52 +0100 | [diff] [blame] | 254 | !obj->map_and_fenceable || |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 255 | obj->cache_level != I915_CACHE_NONE); |
| 256 | } |
| 257 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 258 | static int |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 259 | relocate_entry_cpu(struct drm_i915_gem_object *obj, |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 260 | struct drm_i915_gem_relocation_entry *reloc, |
| 261 | uint64_t target_offset) |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 262 | { |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 263 | struct drm_device *dev = obj->base.dev; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 264 | uint32_t page_offset = offset_in_page(reloc->offset); |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 265 | uint64_t delta = reloc->delta + target_offset; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 266 | char *vaddr; |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 267 | int ret; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 268 | |
Chris Wilson | 2cc86b8 | 2013-08-26 19:51:00 -0300 | [diff] [blame] | 269 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 270 | if (ret) |
| 271 | return ret; |
| 272 | |
| 273 | vaddr = kmap_atomic(i915_gem_object_get_page(obj, |
| 274 | reloc->offset >> PAGE_SHIFT)); |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 275 | *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 276 | |
| 277 | if (INTEL_INFO(dev)->gen >= 8) { |
| 278 | page_offset = offset_in_page(page_offset + sizeof(uint32_t)); |
| 279 | |
| 280 | if (page_offset == 0) { |
| 281 | kunmap_atomic(vaddr); |
| 282 | vaddr = kmap_atomic(i915_gem_object_get_page(obj, |
| 283 | (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT)); |
| 284 | } |
| 285 | |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 286 | *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 289 | kunmap_atomic(vaddr); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static int |
| 295 | relocate_entry_gtt(struct drm_i915_gem_object *obj, |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 296 | struct drm_i915_gem_relocation_entry *reloc, |
| 297 | uint64_t target_offset) |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 298 | { |
| 299 | struct drm_device *dev = obj->base.dev; |
| 300 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 301 | uint64_t delta = reloc->delta + target_offset; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 302 | uint32_t __iomem *reloc_entry; |
| 303 | void __iomem *reloc_page; |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 304 | int ret; |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 305 | |
| 306 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 307 | if (ret) |
| 308 | return ret; |
| 309 | |
| 310 | ret = i915_gem_object_put_fence(obj); |
| 311 | if (ret) |
| 312 | return ret; |
| 313 | |
| 314 | /* Map the page containing the relocation we're going to perform. */ |
| 315 | reloc->offset += i915_gem_obj_ggtt_offset(obj); |
| 316 | reloc_page = io_mapping_map_atomic_wc(dev_priv->gtt.mappable, |
| 317 | reloc->offset & PAGE_MASK); |
| 318 | reloc_entry = (uint32_t __iomem *) |
| 319 | (reloc_page + offset_in_page(reloc->offset)); |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 320 | iowrite32(lower_32_bits(delta), reloc_entry); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 321 | |
| 322 | if (INTEL_INFO(dev)->gen >= 8) { |
| 323 | reloc_entry += 1; |
| 324 | |
| 325 | if (offset_in_page(reloc->offset + sizeof(uint32_t)) == 0) { |
| 326 | io_mapping_unmap_atomic(reloc_page); |
| 327 | reloc_page = io_mapping_map_atomic_wc( |
| 328 | dev_priv->gtt.mappable, |
| 329 | reloc->offset + sizeof(uint32_t)); |
| 330 | reloc_entry = reloc_page; |
| 331 | } |
| 332 | |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 333 | iowrite32(upper_32_bits(delta), reloc_entry); |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 336 | io_mapping_unmap_atomic(reloc_page); |
| 337 | |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static int |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 342 | i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 343 | struct eb_vmas *eb, |
Ben Widawsky | 3e7a032 | 2013-12-06 14:10:57 -0800 | [diff] [blame] | 344 | struct drm_i915_gem_relocation_entry *reloc) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 345 | { |
| 346 | struct drm_device *dev = obj->base.dev; |
| 347 | struct drm_gem_object *target_obj; |
Daniel Vetter | 149c840 | 2012-02-15 23:50:23 +0100 | [diff] [blame] | 348 | struct drm_i915_gem_object *target_i915_obj; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 349 | struct i915_vma *target_vma; |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 350 | uint64_t target_offset; |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 351 | int ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 352 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 353 | /* we've already hold a reference to all valid objects */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 354 | target_vma = eb_get_vma(eb, reloc->target_handle); |
| 355 | if (unlikely(target_vma == NULL)) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 356 | return -ENOENT; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 357 | target_i915_obj = target_vma->obj; |
| 358 | target_obj = &target_vma->obj->base; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 359 | |
Ben Widawsky | 5ce0972 | 2013-11-25 09:54:40 -0800 | [diff] [blame] | 360 | target_offset = target_vma->node.start; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 361 | |
Eric Anholt | e844b99 | 2012-07-31 15:35:01 -0700 | [diff] [blame] | 362 | /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and |
| 363 | * pipe_control writes because the gpu doesn't properly redirect them |
| 364 | * through the ppgtt for non_secure batchbuffers. */ |
| 365 | if (unlikely(IS_GEN6(dev) && |
| 366 | reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION && |
| 367 | !target_i915_obj->has_global_gtt_mapping)) { |
Ben Widawsky | 3e7a032 | 2013-12-06 14:10:57 -0800 | [diff] [blame] | 368 | struct i915_vma *vma = |
| 369 | list_first_entry(&target_i915_obj->vma_list, |
| 370 | typeof(*vma), vma_link); |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 371 | vma->bind_vma(vma, target_i915_obj->cache_level, GLOBAL_BIND); |
Eric Anholt | e844b99 | 2012-07-31 15:35:01 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 374 | /* Validate that the target is in a valid r/w GPU domain */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 375 | if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 376 | DRM_DEBUG("reloc with multiple write domains: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 377 | "obj %p target %d offset %d " |
| 378 | "read %08x write %08x", |
| 379 | obj, reloc->target_handle, |
| 380 | (int) reloc->offset, |
| 381 | reloc->read_domains, |
| 382 | reloc->write_domain); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 383 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 384 | } |
Daniel Vetter | 4ca4a25 | 2011-12-14 13:57:27 +0100 | [diff] [blame] | 385 | if (unlikely((reloc->write_domain | reloc->read_domains) |
| 386 | & ~I915_GEM_GPU_DOMAINS)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 387 | DRM_DEBUG("reloc with read/write non-GPU domains: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 388 | "obj %p target %d offset %d " |
| 389 | "read %08x write %08x", |
| 390 | obj, reloc->target_handle, |
| 391 | (int) reloc->offset, |
| 392 | reloc->read_domains, |
| 393 | reloc->write_domain); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 394 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 395 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 396 | |
| 397 | target_obj->pending_read_domains |= reloc->read_domains; |
| 398 | target_obj->pending_write_domain |= reloc->write_domain; |
| 399 | |
| 400 | /* If the relocation already has the right value in it, no |
| 401 | * more work needs to be done. |
| 402 | */ |
| 403 | if (target_offset == reloc->presumed_offset) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 404 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 405 | |
| 406 | /* Check that the relocation address is valid... */ |
Ben Widawsky | 3c94cee | 2013-11-02 21:07:11 -0700 | [diff] [blame] | 407 | if (unlikely(reloc->offset > |
| 408 | obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 409 | DRM_DEBUG("Relocation beyond object bounds: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 410 | "obj %p target %d offset %d size %d.\n", |
| 411 | obj, reloc->target_handle, |
| 412 | (int) reloc->offset, |
| 413 | (int) obj->base.size); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 414 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 415 | } |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 416 | if (unlikely(reloc->offset & 3)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 417 | DRM_DEBUG("Relocation not 4-byte aligned: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 418 | "obj %p target %d offset %d.\n", |
| 419 | obj, reloc->target_handle, |
| 420 | (int) reloc->offset); |
Ben Widawsky | 8b78f0e | 2013-12-26 13:39:50 -0800 | [diff] [blame] | 421 | return -EINVAL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 424 | /* We can't wait for rendering with pagefaults disabled */ |
| 425 | if (obj->active && in_atomic()) |
| 426 | return -EFAULT; |
| 427 | |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 428 | if (use_cpu_reloc(obj)) |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 429 | ret = relocate_entry_cpu(obj, reloc, target_offset); |
Rafael Barbalho | 5032d87 | 2013-08-21 17:10:51 +0100 | [diff] [blame] | 430 | else |
Ben Widawsky | d9ceb95 | 2014-04-28 17:18:28 -0700 | [diff] [blame] | 431 | ret = relocate_entry_gtt(obj, reloc, target_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 432 | |
Daniel Vetter | d4d3601 | 2013-09-02 20:56:23 +0200 | [diff] [blame] | 433 | if (ret) |
| 434 | return ret; |
| 435 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 436 | /* and update the user's relocation entry */ |
| 437 | reloc->presumed_offset = target_offset; |
| 438 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 439 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | static int |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 443 | i915_gem_execbuffer_relocate_vma(struct i915_vma *vma, |
| 444 | struct eb_vmas *eb) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 445 | { |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 446 | #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry)) |
| 447 | struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 448 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 449 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 450 | int remain, ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 451 | |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 452 | user_relocs = to_user_ptr(entry->relocs_ptr); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 453 | |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 454 | remain = entry->relocation_count; |
| 455 | while (remain) { |
| 456 | struct drm_i915_gem_relocation_entry *r = stack_reloc; |
| 457 | int count = remain; |
| 458 | if (count > ARRAY_SIZE(stack_reloc)) |
| 459 | count = ARRAY_SIZE(stack_reloc); |
| 460 | remain -= count; |
| 461 | |
| 462 | if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0]))) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 463 | return -EFAULT; |
| 464 | |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 465 | do { |
| 466 | u64 offset = r->presumed_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 467 | |
Ben Widawsky | 3e7a032 | 2013-12-06 14:10:57 -0800 | [diff] [blame] | 468 | ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r); |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 469 | if (ret) |
| 470 | return ret; |
| 471 | |
| 472 | if (r->presumed_offset != offset && |
| 473 | __copy_to_user_inatomic(&user_relocs->presumed_offset, |
| 474 | &r->presumed_offset, |
| 475 | sizeof(r->presumed_offset))) { |
| 476 | return -EFAULT; |
| 477 | } |
| 478 | |
| 479 | user_relocs++; |
| 480 | r++; |
| 481 | } while (--count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | return 0; |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 485 | #undef N_RELOC |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static int |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 489 | i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma, |
| 490 | struct eb_vmas *eb, |
| 491 | struct drm_i915_gem_relocation_entry *relocs) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 492 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 493 | const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 494 | int i, ret; |
| 495 | |
| 496 | for (i = 0; i < entry->relocation_count; i++) { |
Ben Widawsky | 3e7a032 | 2013-12-06 14:10:57 -0800 | [diff] [blame] | 497 | ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 498 | if (ret) |
| 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int |
Ben Widawsky | 17601cbc | 2013-11-25 09:54:38 -0800 | [diff] [blame] | 506 | i915_gem_execbuffer_relocate(struct eb_vmas *eb) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 507 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 508 | struct i915_vma *vma; |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 509 | int ret = 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 510 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 511 | /* This is the fast path and we cannot handle a pagefault whilst |
| 512 | * holding the struct mutex lest the user pass in the relocations |
| 513 | * contained within a mmaped bo. For in such a case we, the page |
| 514 | * fault handler would call i915_gem_fault() and we would try to |
| 515 | * acquire the struct mutex again. Obviously this is bad and so |
| 516 | * lockdep complains vehemently. |
| 517 | */ |
| 518 | pagefault_disable(); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 519 | list_for_each_entry(vma, &eb->vmas, exec_list) { |
| 520 | ret = i915_gem_execbuffer_relocate_vma(vma, eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 521 | if (ret) |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 522 | break; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 523 | } |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 524 | pagefault_enable(); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 525 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 526 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 529 | static int |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 530 | i915_gem_execbuffer_reserve_vma(struct i915_vma *vma, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 531 | struct intel_engine_cs *ring, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 532 | bool *need_reloc) |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 533 | { |
Ben Widawsky | 6f65e29 | 2013-12-06 14:10:56 -0800 | [diff] [blame] | 534 | struct drm_i915_gem_object *obj = vma->obj; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 535 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 536 | uint64_t flags; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 537 | int ret; |
| 538 | |
Daniel Vetter | 1ec9e26 | 2014-02-14 14:01:11 +0100 | [diff] [blame] | 539 | flags = 0; |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 540 | if (entry->flags & __EXEC_OBJECT_NEEDS_MAP) |
Daniel Vetter | 1ec9e26 | 2014-02-14 14:01:11 +0100 | [diff] [blame] | 541 | flags |= PIN_MAPPABLE; |
Daniel Vetter | 1ec9e26 | 2014-02-14 14:01:11 +0100 | [diff] [blame] | 542 | if (entry->flags & EXEC_OBJECT_NEEDS_GTT) |
Daniel Vetter | bf3d149 | 2014-02-14 14:01:12 +0100 | [diff] [blame] | 543 | flags |= PIN_GLOBAL; |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 544 | if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS) |
| 545 | flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS; |
Daniel Vetter | 1ec9e26 | 2014-02-14 14:01:11 +0100 | [diff] [blame] | 546 | |
| 547 | ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags); |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 548 | if (ret) |
| 549 | return ret; |
| 550 | |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 551 | entry->flags |= __EXEC_OBJECT_HAS_PIN; |
| 552 | |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 553 | if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) { |
| 554 | ret = i915_gem_object_get_fence(obj); |
| 555 | if (ret) |
| 556 | return ret; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 557 | |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 558 | if (i915_gem_object_pin_fence(obj)) |
| 559 | entry->flags |= __EXEC_OBJECT_HAS_FENCE; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 560 | } |
| 561 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 562 | if (entry->offset != vma->node.start) { |
| 563 | entry->offset = vma->node.start; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 564 | *need_reloc = true; |
| 565 | } |
| 566 | |
| 567 | if (entry->flags & EXEC_OBJECT_WRITE) { |
| 568 | obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER; |
| 569 | obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER; |
| 570 | } |
| 571 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 572 | return 0; |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 573 | } |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 574 | |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 575 | static bool |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 576 | need_reloc_mappable(struct i915_vma *vma) |
| 577 | { |
| 578 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
| 579 | |
| 580 | if (entry->relocation_count == 0) |
| 581 | return false; |
| 582 | |
| 583 | if (!i915_is_ggtt(vma->vm)) |
| 584 | return false; |
| 585 | |
| 586 | /* See also use_cpu_reloc() */ |
| 587 | if (HAS_LLC(vma->obj->base.dev)) |
| 588 | return false; |
| 589 | |
| 590 | if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU) |
| 591 | return false; |
| 592 | |
| 593 | return true; |
| 594 | } |
| 595 | |
| 596 | static bool |
| 597 | eb_vma_misplaced(struct i915_vma *vma) |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 598 | { |
| 599 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
| 600 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 601 | |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 602 | WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP && |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 603 | !i915_is_ggtt(vma->vm)); |
| 604 | |
| 605 | if (entry->alignment && |
| 606 | vma->node.start & (entry->alignment - 1)) |
| 607 | return true; |
| 608 | |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 609 | if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable) |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 610 | return true; |
| 611 | |
| 612 | if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS && |
| 613 | vma->node.start < BATCH_OFFSET_BIAS) |
| 614 | return true; |
| 615 | |
| 616 | return false; |
| 617 | } |
| 618 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 619 | static int |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 620 | i915_gem_execbuffer_reserve(struct intel_engine_cs *ring, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 621 | struct list_head *vmas, |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 622 | bool *need_relocs) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 623 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 624 | struct drm_i915_gem_object *obj; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 625 | struct i915_vma *vma; |
Ben Widawsky | 68c8c17 | 2013-09-11 14:57:50 -0700 | [diff] [blame] | 626 | struct i915_address_space *vm; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 627 | struct list_head ordered_vmas; |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 628 | bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4; |
| 629 | int retry; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 630 | |
Ben Widawsky | 68c8c17 | 2013-09-11 14:57:50 -0700 | [diff] [blame] | 631 | if (list_empty(vmas)) |
| 632 | return 0; |
| 633 | |
Chris Wilson | 227f782 | 2014-05-15 10:41:42 +0100 | [diff] [blame] | 634 | i915_gem_retire_requests_ring(ring); |
| 635 | |
Ben Widawsky | 68c8c17 | 2013-09-11 14:57:50 -0700 | [diff] [blame] | 636 | vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm; |
| 637 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 638 | INIT_LIST_HEAD(&ordered_vmas); |
| 639 | while (!list_empty(vmas)) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 640 | struct drm_i915_gem_exec_object2 *entry; |
| 641 | bool need_fence, need_mappable; |
| 642 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 643 | vma = list_first_entry(vmas, struct i915_vma, exec_list); |
| 644 | obj = vma->obj; |
| 645 | entry = vma->exec_entry; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 646 | |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 647 | if (!has_fenced_gpu_access) |
| 648 | entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 649 | need_fence = |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 650 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 651 | obj->tiling_mode != I915_TILING_NONE; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 652 | need_mappable = need_fence || need_reloc_mappable(vma); |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 653 | |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 654 | if (need_mappable) { |
| 655 | entry->flags |= __EXEC_OBJECT_NEEDS_MAP; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 656 | list_move(&vma->exec_list, &ordered_vmas); |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 657 | } else |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 658 | list_move_tail(&vma->exec_list, &ordered_vmas); |
Chris Wilson | 595dad7 | 2011-01-13 11:03:48 +0000 | [diff] [blame] | 659 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 660 | obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND; |
Chris Wilson | 595dad7 | 2011-01-13 11:03:48 +0000 | [diff] [blame] | 661 | obj->base.pending_write_domain = 0; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 662 | } |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 663 | list_splice(&ordered_vmas, vmas); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 664 | |
| 665 | /* Attempt to pin all of the buffers into the GTT. |
| 666 | * This is done in 3 phases: |
| 667 | * |
| 668 | * 1a. Unbind all objects that do not match the GTT constraints for |
| 669 | * the execbuffer (fenceable, mappable, alignment etc). |
| 670 | * 1b. Increment pin count for already bound objects. |
| 671 | * 2. Bind new objects. |
| 672 | * 3. Decrement pin count. |
| 673 | * |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 674 | * This avoid unnecessary unbinding of later objects in order to make |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 675 | * room for the earlier objects *unless* we need to defragment. |
| 676 | */ |
| 677 | retry = 0; |
| 678 | do { |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 679 | int ret = 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 680 | |
| 681 | /* Unbind any ill-fitting objects or pin. */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 682 | list_for_each_entry(vma, vmas, exec_list) { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 683 | if (!drm_mm_node_allocated(&vma->node)) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 684 | continue; |
| 685 | |
Chris Wilson | e6a8446 | 2014-08-11 12:00:12 +0200 | [diff] [blame] | 686 | if (eb_vma_misplaced(vma)) |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 687 | ret = i915_vma_unbind(vma); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 688 | else |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 689 | ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 690 | if (ret) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 691 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* Bind fresh objects */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 695 | list_for_each_entry(vma, vmas, exec_list) { |
| 696 | if (drm_mm_node_allocated(&vma->node)) |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 697 | continue; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 698 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 699 | ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs); |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 700 | if (ret) |
| 701 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 702 | } |
| 703 | |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 704 | err: |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 705 | if (ret != -ENOSPC || retry++) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 706 | return ret; |
| 707 | |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 708 | /* Decrement pin count for bound objects */ |
| 709 | list_for_each_entry(vma, vmas, exec_list) |
| 710 | i915_gem_execbuffer_unreserve_vma(vma); |
| 711 | |
Ben Widawsky | 68c8c17 | 2013-09-11 14:57:50 -0700 | [diff] [blame] | 712 | ret = i915_gem_evict_vm(vm, true); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 713 | if (ret) |
| 714 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 715 | } while (1); |
| 716 | } |
| 717 | |
| 718 | static int |
| 719 | i915_gem_execbuffer_relocate_slow(struct drm_device *dev, |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 720 | struct drm_i915_gem_execbuffer2 *args, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 721 | struct drm_file *file, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 722 | struct intel_engine_cs *ring, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 723 | struct eb_vmas *eb, |
| 724 | struct drm_i915_gem_exec_object2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 725 | { |
| 726 | struct drm_i915_gem_relocation_entry *reloc; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 727 | struct i915_address_space *vm; |
| 728 | struct i915_vma *vma; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 729 | bool need_relocs; |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 730 | int *reloc_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 731 | int i, total, ret; |
Daniel Vetter | b205ca5 | 2013-09-19 14:00:11 +0200 | [diff] [blame] | 732 | unsigned count = args->buffer_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 733 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 734 | if (WARN_ON(list_empty(&eb->vmas))) |
| 735 | return 0; |
| 736 | |
| 737 | vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm; |
| 738 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 739 | /* We may process another execbuffer during the unlock... */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 740 | while (!list_empty(&eb->vmas)) { |
| 741 | vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list); |
| 742 | list_del_init(&vma->exec_list); |
Chris Wilson | a415d35 | 2013-11-26 11:23:15 +0000 | [diff] [blame] | 743 | i915_gem_execbuffer_unreserve_vma(vma); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 744 | drm_gem_object_unreference(&vma->obj->base); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 747 | mutex_unlock(&dev->struct_mutex); |
| 748 | |
| 749 | total = 0; |
| 750 | for (i = 0; i < count; i++) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 751 | total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 752 | |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 753 | reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset)); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 754 | reloc = drm_malloc_ab(total, sizeof(*reloc)); |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 755 | if (reloc == NULL || reloc_offset == NULL) { |
| 756 | drm_free_large(reloc); |
| 757 | drm_free_large(reloc_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 758 | mutex_lock(&dev->struct_mutex); |
| 759 | return -ENOMEM; |
| 760 | } |
| 761 | |
| 762 | total = 0; |
| 763 | for (i = 0; i < count; i++) { |
| 764 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
Chris Wilson | 262b6d3 | 2013-01-15 16:17:54 +0000 | [diff] [blame] | 765 | u64 invalid_offset = (u64)-1; |
| 766 | int j; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 767 | |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 768 | user_relocs = to_user_ptr(exec[i].relocs_ptr); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 769 | |
| 770 | if (copy_from_user(reloc+total, user_relocs, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 771 | exec[i].relocation_count * sizeof(*reloc))) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 772 | ret = -EFAULT; |
| 773 | mutex_lock(&dev->struct_mutex); |
| 774 | goto err; |
| 775 | } |
| 776 | |
Chris Wilson | 262b6d3 | 2013-01-15 16:17:54 +0000 | [diff] [blame] | 777 | /* As we do not update the known relocation offsets after |
| 778 | * relocating (due to the complexities in lock handling), |
| 779 | * we need to mark them as invalid now so that we force the |
| 780 | * relocation processing next time. Just in case the target |
| 781 | * object is evicted and then rebound into its old |
| 782 | * presumed_offset before the next execbuffer - if that |
| 783 | * happened we would make the mistake of assuming that the |
| 784 | * relocations were valid. |
| 785 | */ |
| 786 | for (j = 0; j < exec[i].relocation_count; j++) { |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 787 | if (__copy_to_user(&user_relocs[j].presumed_offset, |
| 788 | &invalid_offset, |
| 789 | sizeof(invalid_offset))) { |
Chris Wilson | 262b6d3 | 2013-01-15 16:17:54 +0000 | [diff] [blame] | 790 | ret = -EFAULT; |
| 791 | mutex_lock(&dev->struct_mutex); |
| 792 | goto err; |
| 793 | } |
| 794 | } |
| 795 | |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 796 | reloc_offset[i] = total; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 797 | total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | ret = i915_mutex_lock_interruptible(dev); |
| 801 | if (ret) { |
| 802 | mutex_lock(&dev->struct_mutex); |
| 803 | goto err; |
| 804 | } |
| 805 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 806 | /* reacquire the objects */ |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 807 | eb_reset(eb); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 808 | ret = eb_lookup_vmas(eb, exec, args, vm, file); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 809 | if (ret) |
| 810 | goto err; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 811 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 812 | need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 813 | ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 814 | if (ret) |
| 815 | goto err; |
| 816 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 817 | list_for_each_entry(vma, &eb->vmas, exec_list) { |
| 818 | int offset = vma->exec_entry - exec; |
| 819 | ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb, |
| 820 | reloc + reloc_offset[offset]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 821 | if (ret) |
| 822 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | /* Leave the user relocations as are, this is the painfully slow path, |
| 826 | * and we want to avoid the complication of dropping the lock whilst |
| 827 | * having buffers reserved in the aperture and so causing spurious |
| 828 | * ENOSPC for random operations. |
| 829 | */ |
| 830 | |
| 831 | err: |
| 832 | drm_free_large(reloc); |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 833 | drm_free_large(reloc_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 834 | return ret; |
| 835 | } |
| 836 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 837 | static int |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 838 | i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 839 | struct list_head *vmas) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 840 | { |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 841 | struct i915_vma *vma; |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 842 | uint32_t flush_domains = 0; |
Chris Wilson | 000433b | 2013-08-08 14:41:09 +0100 | [diff] [blame] | 843 | bool flush_chipset = false; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 844 | int ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 845 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 846 | list_for_each_entry(vma, vmas, exec_list) { |
| 847 | struct drm_i915_gem_object *obj = vma->obj; |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame] | 848 | ret = i915_gem_object_sync(obj, ring); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 849 | if (ret) |
| 850 | return ret; |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 851 | |
| 852 | if (obj->base.write_domain & I915_GEM_DOMAIN_CPU) |
Chris Wilson | 000433b | 2013-08-08 14:41:09 +0100 | [diff] [blame] | 853 | flush_chipset |= i915_gem_clflush_object(obj, false); |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 854 | |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 855 | flush_domains |= obj->base.write_domain; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Chris Wilson | 000433b | 2013-08-08 14:41:09 +0100 | [diff] [blame] | 858 | if (flush_chipset) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 859 | i915_gem_chipset_flush(ring->dev); |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 860 | |
| 861 | if (flush_domains & I915_GEM_DOMAIN_GTT) |
| 862 | wmb(); |
| 863 | |
Chris Wilson | 09cf7c9 | 2012-07-13 14:14:08 +0100 | [diff] [blame] | 864 | /* Unconditionally invalidate gpu caches and ensure that we do flush |
| 865 | * any residual writes from the previous batch. |
| 866 | */ |
Chris Wilson | a7b9761 | 2012-07-20 12:41:08 +0100 | [diff] [blame] | 867 | return intel_ring_invalidate_all_caches(ring); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 868 | } |
| 869 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 870 | static bool |
| 871 | i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 872 | { |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 873 | if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS) |
| 874 | return false; |
| 875 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 876 | return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 877 | } |
| 878 | |
| 879 | static int |
| 880 | validate_exec_list(struct drm_i915_gem_exec_object2 *exec, |
| 881 | int count) |
| 882 | { |
| 883 | int i; |
Daniel Vetter | b205ca5 | 2013-09-19 14:00:11 +0200 | [diff] [blame] | 884 | unsigned relocs_total = 0; |
| 885 | unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 886 | |
| 887 | for (i = 0; i < count; i++) { |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 888 | char __user *ptr = to_user_ptr(exec[i].relocs_ptr); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 889 | int length; /* limited by fault_in_pages_readable() */ |
| 890 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 891 | if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS) |
| 892 | return -EINVAL; |
| 893 | |
Kees Cook | 3118a4f | 2013-03-11 17:31:45 -0700 | [diff] [blame] | 894 | /* First check for malicious input causing overflow in |
| 895 | * the worst case where we need to allocate the entire |
| 896 | * relocation tree as a single array. |
| 897 | */ |
| 898 | if (exec[i].relocation_count > relocs_max - relocs_total) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 899 | return -EINVAL; |
Kees Cook | 3118a4f | 2013-03-11 17:31:45 -0700 | [diff] [blame] | 900 | relocs_total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 901 | |
| 902 | length = exec[i].relocation_count * |
| 903 | sizeof(struct drm_i915_gem_relocation_entry); |
Kees Cook | 3058753 | 2013-03-11 14:37:35 -0700 | [diff] [blame] | 904 | /* |
| 905 | * We must check that the entire relocation array is safe |
| 906 | * to read, but since we may need to update the presumed |
| 907 | * offsets during execution, check for full write access. |
| 908 | */ |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 909 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
| 910 | return -EFAULT; |
| 911 | |
Jani Nikula | d330a95 | 2014-01-21 11:24:25 +0200 | [diff] [blame] | 912 | if (likely(!i915.prefault_disable)) { |
Xiong Zhang | 0b74b50 | 2013-07-19 13:51:24 +0800 | [diff] [blame] | 913 | if (fault_in_multipages_readable(ptr, length)) |
| 914 | return -EFAULT; |
| 915 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
Oscar Mateo | 273497e | 2014-05-22 14:13:37 +0100 | [diff] [blame] | 921 | static struct intel_context * |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 922 | i915_gem_validate_context(struct drm_device *dev, struct drm_file *file, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 923 | struct intel_engine_cs *ring, const u32 ctx_id) |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 924 | { |
Oscar Mateo | 273497e | 2014-05-22 14:13:37 +0100 | [diff] [blame] | 925 | struct intel_context *ctx = NULL; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 926 | struct i915_ctx_hang_stats *hs; |
| 927 | |
Oscar Mateo | 821d66d | 2014-07-03 16:28:00 +0100 | [diff] [blame] | 928 | if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE) |
Daniel Vetter | 7c9c4b8 | 2013-12-18 16:37:49 +0100 | [diff] [blame] | 929 | return ERR_PTR(-EINVAL); |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 930 | |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 931 | ctx = i915_gem_context_get(file->driver_priv, ctx_id); |
Ben Widawsky | 72ad5c4 | 2014-01-02 19:50:27 -1000 | [diff] [blame] | 932 | if (IS_ERR(ctx)) |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 933 | return ctx; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 934 | |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 935 | hs = &ctx->hang_stats; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 936 | if (hs->banned) { |
| 937 | DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id); |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 938 | return ERR_PTR(-EIO); |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 939 | } |
| 940 | |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 941 | return ctx; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 942 | } |
| 943 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 944 | static void |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 945 | i915_gem_execbuffer_move_to_active(struct list_head *vmas, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 946 | struct intel_engine_cs *ring) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 947 | { |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 948 | u32 seqno = intel_ring_get_seqno(ring); |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 949 | struct i915_vma *vma; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 950 | |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 951 | list_for_each_entry(vma, vmas, exec_list) { |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 952 | struct drm_i915_gem_exec_object2 *entry = vma->exec_entry; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 953 | struct drm_i915_gem_object *obj = vma->obj; |
Chris Wilson | 69c2fc8 | 2012-07-20 12:41:03 +0100 | [diff] [blame] | 954 | u32 old_read = obj->base.read_domains; |
| 955 | u32 old_write = obj->base.write_domain; |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 956 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 957 | obj->base.write_domain = obj->base.pending_write_domain; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 958 | if (obj->base.write_domain == 0) |
| 959 | obj->base.pending_read_domains |= obj->base.read_domains; |
| 960 | obj->base.read_domains = obj->base.pending_read_domains; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 961 | |
Ben Widawsky | e2d05a8 | 2013-09-24 09:57:58 -0700 | [diff] [blame] | 962 | i915_vma_move_to_active(vma, ring); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 963 | if (obj->base.write_domain) { |
| 964 | obj->dirty = 1; |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 965 | obj->last_write_seqno = seqno; |
Daniel Vetter | f99d706 | 2014-06-19 16:01:59 +0200 | [diff] [blame] | 966 | |
| 967 | intel_fb_obj_invalidate(obj, ring); |
Chris Wilson | c8725f3 | 2014-03-17 12:21:55 +0000 | [diff] [blame] | 968 | |
| 969 | /* update for the implicit flush after a batch */ |
| 970 | obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 971 | } |
Chris Wilson | 82b6b6d | 2014-08-09 17:37:24 +0100 | [diff] [blame] | 972 | if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) { |
| 973 | obj->last_fenced_seqno = seqno; |
| 974 | if (entry->flags & __EXEC_OBJECT_HAS_FENCE) { |
| 975 | struct drm_i915_private *dev_priv = to_i915(ring->dev); |
| 976 | list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list, |
| 977 | &dev_priv->mm.fence_list); |
| 978 | } |
| 979 | } |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 980 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 981 | trace_i915_gem_object_change_domain(obj, old_read, old_write); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
| 984 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 985 | static void |
| 986 | i915_gem_execbuffer_retire_commands(struct drm_device *dev, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 987 | struct drm_file *file, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 988 | struct intel_engine_cs *ring, |
Mika Kuoppala | 7d736f4 | 2013-06-12 15:01:39 +0300 | [diff] [blame] | 989 | struct drm_i915_gem_object *obj) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 990 | { |
Daniel Vetter | cc889e0 | 2012-06-13 20:45:19 +0200 | [diff] [blame] | 991 | /* Unconditionally force add_request to emit a full flush. */ |
| 992 | ring->gpu_caches_dirty = true; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 993 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 994 | /* Add a breadcrumb for the completion of the batch buffer */ |
Mika Kuoppala | 7d736f4 | 2013-06-12 15:01:39 +0300 | [diff] [blame] | 995 | (void)__i915_add_request(ring, file, obj, NULL); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 996 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 997 | |
| 998 | static int |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 999 | i915_reset_gen7_sol_offsets(struct drm_device *dev, |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 1000 | struct intel_engine_cs *ring) |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1001 | { |
Jani Nikula | 50227e1 | 2014-03-31 14:27:21 +0300 | [diff] [blame] | 1002 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1003 | int ret, i; |
| 1004 | |
Daniel Vetter | 9d662da | 2014-04-24 08:09:09 +0200 | [diff] [blame] | 1005 | if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) { |
| 1006 | DRM_DEBUG("sol reset is gen7/rcs only\n"); |
| 1007 | return -EINVAL; |
| 1008 | } |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1009 | |
| 1010 | ret = intel_ring_begin(ring, 4 * 3); |
| 1011 | if (ret) |
| 1012 | return ret; |
| 1013 | |
| 1014 | for (i = 0; i < 4; i++) { |
| 1015 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 1016 | intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i)); |
| 1017 | intel_ring_emit(ring, 0); |
| 1018 | } |
| 1019 | |
| 1020 | intel_ring_advance(ring); |
| 1021 | |
| 1022 | return 0; |
| 1023 | } |
| 1024 | |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1025 | static int |
| 1026 | legacy_ringbuffer_submission(struct drm_device *dev, struct drm_file *file, |
| 1027 | struct intel_engine_cs *ring, |
| 1028 | struct intel_context *ctx, |
| 1029 | struct drm_i915_gem_execbuffer2 *args, |
| 1030 | struct list_head *vmas, |
| 1031 | struct drm_i915_gem_object *batch_obj, |
| 1032 | u64 exec_start, u32 flags) |
| 1033 | { |
| 1034 | struct drm_clip_rect *cliprects = NULL; |
| 1035 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1036 | u64 exec_len; |
| 1037 | int instp_mode; |
| 1038 | u32 instp_mask; |
| 1039 | int i, ret = 0; |
| 1040 | |
| 1041 | if (args->num_cliprects != 0) { |
| 1042 | if (ring != &dev_priv->ring[RCS]) { |
| 1043 | DRM_DEBUG("clip rectangles are only valid with the render ring\n"); |
| 1044 | return -EINVAL; |
| 1045 | } |
| 1046 | |
| 1047 | if (INTEL_INFO(dev)->gen >= 5) { |
| 1048 | DRM_DEBUG("clip rectangles are only valid on pre-gen5\n"); |
| 1049 | return -EINVAL; |
| 1050 | } |
| 1051 | |
| 1052 | if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) { |
| 1053 | DRM_DEBUG("execbuf with %u cliprects\n", |
| 1054 | args->num_cliprects); |
| 1055 | return -EINVAL; |
| 1056 | } |
| 1057 | |
| 1058 | cliprects = kcalloc(args->num_cliprects, |
| 1059 | sizeof(*cliprects), |
| 1060 | GFP_KERNEL); |
| 1061 | if (cliprects == NULL) { |
| 1062 | ret = -ENOMEM; |
| 1063 | goto error; |
| 1064 | } |
| 1065 | |
| 1066 | if (copy_from_user(cliprects, |
| 1067 | to_user_ptr(args->cliprects_ptr), |
| 1068 | sizeof(*cliprects)*args->num_cliprects)) { |
| 1069 | ret = -EFAULT; |
| 1070 | goto error; |
| 1071 | } |
| 1072 | } else { |
| 1073 | if (args->DR4 == 0xffffffff) { |
| 1074 | DRM_DEBUG("UXA submitting garbage DR4, fixing up\n"); |
| 1075 | args->DR4 = 0; |
| 1076 | } |
| 1077 | |
| 1078 | if (args->DR1 || args->DR4 || args->cliprects_ptr) { |
| 1079 | DRM_DEBUG("0 cliprects but dirt in cliprects fields\n"); |
| 1080 | return -EINVAL; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | ret = i915_gem_execbuffer_move_to_gpu(ring, vmas); |
| 1085 | if (ret) |
| 1086 | goto error; |
| 1087 | |
| 1088 | ret = i915_switch_context(ring, ctx); |
| 1089 | if (ret) |
| 1090 | goto error; |
| 1091 | |
| 1092 | instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK; |
| 1093 | instp_mask = I915_EXEC_CONSTANTS_MASK; |
| 1094 | switch (instp_mode) { |
| 1095 | case I915_EXEC_CONSTANTS_REL_GENERAL: |
| 1096 | case I915_EXEC_CONSTANTS_ABSOLUTE: |
| 1097 | case I915_EXEC_CONSTANTS_REL_SURFACE: |
| 1098 | if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) { |
| 1099 | DRM_DEBUG("non-0 rel constants mode on non-RCS\n"); |
| 1100 | ret = -EINVAL; |
| 1101 | goto error; |
| 1102 | } |
| 1103 | |
| 1104 | if (instp_mode != dev_priv->relative_constants_mode) { |
| 1105 | if (INTEL_INFO(dev)->gen < 4) { |
| 1106 | DRM_DEBUG("no rel constants on pre-gen4\n"); |
| 1107 | ret = -EINVAL; |
| 1108 | goto error; |
| 1109 | } |
| 1110 | |
| 1111 | if (INTEL_INFO(dev)->gen > 5 && |
| 1112 | instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) { |
| 1113 | DRM_DEBUG("rel surface constants mode invalid on gen5+\n"); |
| 1114 | ret = -EINVAL; |
| 1115 | goto error; |
| 1116 | } |
| 1117 | |
| 1118 | /* The HW changed the meaning on this bit on gen6 */ |
| 1119 | if (INTEL_INFO(dev)->gen >= 6) |
| 1120 | instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE; |
| 1121 | } |
| 1122 | break; |
| 1123 | default: |
| 1124 | DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode); |
| 1125 | ret = -EINVAL; |
| 1126 | goto error; |
| 1127 | } |
| 1128 | |
| 1129 | if (ring == &dev_priv->ring[RCS] && |
| 1130 | instp_mode != dev_priv->relative_constants_mode) { |
| 1131 | ret = intel_ring_begin(ring, 4); |
| 1132 | if (ret) |
| 1133 | goto error; |
| 1134 | |
| 1135 | intel_ring_emit(ring, MI_NOOP); |
| 1136 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 1137 | intel_ring_emit(ring, INSTPM); |
| 1138 | intel_ring_emit(ring, instp_mask << 16 | instp_mode); |
| 1139 | intel_ring_advance(ring); |
| 1140 | |
| 1141 | dev_priv->relative_constants_mode = instp_mode; |
| 1142 | } |
| 1143 | |
| 1144 | if (args->flags & I915_EXEC_GEN7_SOL_RESET) { |
| 1145 | ret = i915_reset_gen7_sol_offsets(dev, ring); |
| 1146 | if (ret) |
| 1147 | goto error; |
| 1148 | } |
| 1149 | |
| 1150 | exec_len = args->batch_len; |
| 1151 | if (cliprects) { |
| 1152 | for (i = 0; i < args->num_cliprects; i++) { |
| 1153 | ret = i915_emit_box(dev, &cliprects[i], |
| 1154 | args->DR1, args->DR4); |
| 1155 | if (ret) |
| 1156 | goto error; |
| 1157 | |
| 1158 | ret = ring->dispatch_execbuffer(ring, |
| 1159 | exec_start, exec_len, |
| 1160 | flags); |
| 1161 | if (ret) |
| 1162 | goto error; |
| 1163 | } |
| 1164 | } else { |
| 1165 | ret = ring->dispatch_execbuffer(ring, |
| 1166 | exec_start, exec_len, |
| 1167 | flags); |
| 1168 | if (ret) |
| 1169 | return ret; |
| 1170 | } |
| 1171 | |
| 1172 | trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags); |
| 1173 | |
| 1174 | i915_gem_execbuffer_move_to_active(vmas, ring); |
| 1175 | i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj); |
| 1176 | |
| 1177 | error: |
| 1178 | kfree(cliprects); |
| 1179 | return ret; |
| 1180 | } |
| 1181 | |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1182 | /** |
| 1183 | * Find one BSD ring to dispatch the corresponding BSD command. |
| 1184 | * The Ring ID is returned. |
| 1185 | */ |
| 1186 | static int gen8_dispatch_bsd_ring(struct drm_device *dev, |
| 1187 | struct drm_file *file) |
| 1188 | { |
| 1189 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1190 | struct drm_i915_file_private *file_priv = file->driver_priv; |
| 1191 | |
| 1192 | /* Check whether the file_priv is using one ring */ |
| 1193 | if (file_priv->bsd_ring) |
| 1194 | return file_priv->bsd_ring->id; |
| 1195 | else { |
| 1196 | /* If no, use the ping-pong mechanism to select one ring */ |
| 1197 | int ring_id; |
| 1198 | |
| 1199 | mutex_lock(&dev->struct_mutex); |
Daniel Vetter | bdf1e7e | 2014-05-21 17:37:52 +0200 | [diff] [blame] | 1200 | if (dev_priv->mm.bsd_ring_dispatch_index == 0) { |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1201 | ring_id = VCS; |
Daniel Vetter | bdf1e7e | 2014-05-21 17:37:52 +0200 | [diff] [blame] | 1202 | dev_priv->mm.bsd_ring_dispatch_index = 1; |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1203 | } else { |
| 1204 | ring_id = VCS2; |
Daniel Vetter | bdf1e7e | 2014-05-21 17:37:52 +0200 | [diff] [blame] | 1205 | dev_priv->mm.bsd_ring_dispatch_index = 0; |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1206 | } |
| 1207 | file_priv->bsd_ring = &dev_priv->ring[ring_id]; |
| 1208 | mutex_unlock(&dev->struct_mutex); |
| 1209 | return ring_id; |
| 1210 | } |
| 1211 | } |
| 1212 | |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 1213 | static struct drm_i915_gem_object * |
| 1214 | eb_get_batch(struct eb_vmas *eb) |
| 1215 | { |
| 1216 | struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list); |
| 1217 | |
| 1218 | /* |
| 1219 | * SNA is doing fancy tricks with compressing batch buffers, which leads |
| 1220 | * to negative relocation deltas. Usually that works out ok since the |
| 1221 | * relocate address is still positive, except when the batch is placed |
| 1222 | * very low in the GTT. Ensure this doesn't happen. |
| 1223 | * |
| 1224 | * Note that actual hangs have only been observed on gen7, but for |
| 1225 | * paranoia do it everywhere. |
| 1226 | */ |
| 1227 | vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS; |
| 1228 | |
| 1229 | return vma->obj; |
| 1230 | } |
| 1231 | |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 1232 | static int |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1233 | i915_gem_do_execbuffer(struct drm_device *dev, void *data, |
| 1234 | struct drm_file *file, |
| 1235 | struct drm_i915_gem_execbuffer2 *args, |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1236 | struct drm_i915_gem_exec_object2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1237 | { |
Jani Nikula | 50227e1 | 2014-03-31 14:27:21 +0300 | [diff] [blame] | 1238 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1239 | struct eb_vmas *eb; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1240 | struct drm_i915_gem_object *batch_obj; |
Oscar Mateo | a4872ba | 2014-05-22 14:13:33 +0100 | [diff] [blame] | 1241 | struct intel_engine_cs *ring; |
Oscar Mateo | 273497e | 2014-05-22 14:13:37 +0100 | [diff] [blame] | 1242 | struct intel_context *ctx; |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1243 | struct i915_address_space *vm; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 1244 | const u32 ctx_id = i915_execbuffer2_get_context_id(*args); |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1245 | u64 exec_start = args->batch_start_offset; |
| 1246 | u32 flags; |
| 1247 | int ret; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1248 | bool need_relocs; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1249 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1250 | if (!i915_gem_check_execbuffer(args)) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1251 | return -EINVAL; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1252 | |
| 1253 | ret = validate_exec_list(exec, args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1254 | if (ret) |
| 1255 | return ret; |
| 1256 | |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1257 | flags = 0; |
| 1258 | if (args->flags & I915_EXEC_SECURE) { |
| 1259 | if (!file->is_master || !capable(CAP_SYS_ADMIN)) |
| 1260 | return -EPERM; |
| 1261 | |
| 1262 | flags |= I915_DISPATCH_SECURE; |
| 1263 | } |
Daniel Vetter | b45305f | 2012-12-17 16:21:27 +0100 | [diff] [blame] | 1264 | if (args->flags & I915_EXEC_IS_PINNED) |
| 1265 | flags |= I915_DISPATCH_PINNED; |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1266 | |
Zhao Yakui | b1a9330 | 2014-04-17 10:37:36 +0800 | [diff] [blame] | 1267 | if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1268 | DRM_DEBUG("execbuf with unknown ring: %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1269 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 1270 | return -EINVAL; |
| 1271 | } |
Ben Widawsky | ca01b12 | 2013-12-06 14:11:00 -0800 | [diff] [blame] | 1272 | |
| 1273 | if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT) |
| 1274 | ring = &dev_priv->ring[RCS]; |
Zhao Yakui | a8ebba7 | 2014-04-17 10:37:40 +0800 | [diff] [blame] | 1275 | else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) { |
| 1276 | if (HAS_BSD2(dev)) { |
| 1277 | int ring_id; |
| 1278 | ring_id = gen8_dispatch_bsd_ring(dev, file); |
| 1279 | ring = &dev_priv->ring[ring_id]; |
| 1280 | } else |
| 1281 | ring = &dev_priv->ring[VCS]; |
| 1282 | } else |
Ben Widawsky | ca01b12 | 2013-12-06 14:11:00 -0800 | [diff] [blame] | 1283 | ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1]; |
| 1284 | |
Chris Wilson | a15817c | 2012-05-11 14:29:31 +0100 | [diff] [blame] | 1285 | if (!intel_ring_initialized(ring)) { |
| 1286 | DRM_DEBUG("execbuf with invalid ring: %d\n", |
| 1287 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 1288 | return -EINVAL; |
| 1289 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1290 | |
| 1291 | if (args->buffer_count < 1) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1292 | DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1293 | return -EINVAL; |
| 1294 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1295 | |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1296 | intel_runtime_pm_get(dev_priv); |
| 1297 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1298 | ret = i915_mutex_lock_interruptible(dev); |
| 1299 | if (ret) |
| 1300 | goto pre_mutex_err; |
| 1301 | |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 1302 | if (dev_priv->ums.mm_suspended) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1303 | mutex_unlock(&dev->struct_mutex); |
| 1304 | ret = -EBUSY; |
| 1305 | goto pre_mutex_err; |
| 1306 | } |
| 1307 | |
Daniel Vetter | 7c9c4b8 | 2013-12-18 16:37:49 +0100 | [diff] [blame] | 1308 | ctx = i915_gem_validate_context(dev, file, ring, ctx_id); |
Ben Widawsky | 72ad5c4 | 2014-01-02 19:50:27 -1000 | [diff] [blame] | 1309 | if (IS_ERR(ctx)) { |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 1310 | mutex_unlock(&dev->struct_mutex); |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1311 | ret = PTR_ERR(ctx); |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 1312 | goto pre_mutex_err; |
Ben Widawsky | 935f38d | 2014-04-04 22:41:07 -0700 | [diff] [blame] | 1313 | } |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1314 | |
| 1315 | i915_gem_context_reference(ctx); |
| 1316 | |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1317 | vm = ctx->vm; |
| 1318 | if (!USES_FULL_PPGTT(dev)) |
| 1319 | vm = &dev_priv->gtt.base; |
Mika Kuoppala | d299cce | 2013-11-26 16:14:33 +0200 | [diff] [blame] | 1320 | |
Ben Widawsky | 17601cbc | 2013-11-25 09:54:38 -0800 | [diff] [blame] | 1321 | eb = eb_create(args); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1322 | if (eb == NULL) { |
Ben Widawsky | 935f38d | 2014-04-04 22:41:07 -0700 | [diff] [blame] | 1323 | i915_gem_context_unreference(ctx); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1324 | mutex_unlock(&dev->struct_mutex); |
| 1325 | ret = -ENOMEM; |
| 1326 | goto pre_mutex_err; |
| 1327 | } |
| 1328 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1329 | /* Look up object handles */ |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1330 | ret = eb_lookup_vmas(eb, exec, args, vm, file); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 1331 | if (ret) |
| 1332 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1333 | |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1334 | /* take note of the batch buffer before we might reorder the lists */ |
Chris Wilson | d23db88 | 2014-05-23 08:48:08 +0200 | [diff] [blame] | 1335 | batch_obj = eb_get_batch(eb); |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 1336 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1337 | /* Move the objects en-masse into the GTT, evicting if necessary. */ |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1338 | need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0; |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1339 | ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1340 | if (ret) |
| 1341 | goto err; |
| 1342 | |
| 1343 | /* The objects are in their final locations, apply the relocations. */ |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1344 | if (need_relocs) |
Ben Widawsky | 17601cbc | 2013-11-25 09:54:38 -0800 | [diff] [blame] | 1345 | ret = i915_gem_execbuffer_relocate(eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1346 | if (ret) { |
| 1347 | if (ret == -EFAULT) { |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame] | 1348 | ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring, |
Ben Widawsky | 27173f1 | 2013-08-14 11:38:36 +0200 | [diff] [blame] | 1349 | eb, exec); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1350 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 1351 | } |
| 1352 | if (ret) |
| 1353 | goto err; |
| 1354 | } |
| 1355 | |
| 1356 | /* Set the pending read domains for the batch buffer to COMMAND */ |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1357 | if (batch_obj->base.pending_write_domain) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1358 | DRM_DEBUG("Attempting to use self-modifying batch buffer\n"); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1359 | ret = -EINVAL; |
| 1360 | goto err; |
| 1361 | } |
| 1362 | batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND; |
| 1363 | |
Brad Volkin | 351e3db | 2014-02-18 10:15:46 -0800 | [diff] [blame] | 1364 | if (i915_needs_cmd_parser(ring)) { |
| 1365 | ret = i915_parse_cmds(ring, |
| 1366 | batch_obj, |
| 1367 | args->batch_start_offset, |
| 1368 | file->is_master); |
| 1369 | if (ret) |
| 1370 | goto err; |
| 1371 | |
| 1372 | /* |
| 1373 | * XXX: Actually do this when enabling batch copy... |
| 1374 | * |
| 1375 | * Set the DISPATCH_SECURE bit to remove the NON_SECURE bit |
| 1376 | * from MI_BATCH_BUFFER_START commands issued in the |
| 1377 | * dispatch_execbuffer implementations. We specifically don't |
| 1378 | * want that set when the command parser is enabled. |
| 1379 | */ |
| 1380 | } |
| 1381 | |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1382 | /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure |
| 1383 | * batch" bit. Hence we need to pin secure batches into the global gtt. |
Ben Widawsky | 28cf541 | 2013-11-02 21:07:26 -0700 | [diff] [blame] | 1384 | * hsw should have this fixed, but bdw mucks it up again. */ |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame^] | 1385 | if (flags & I915_DISPATCH_SECURE) { |
| 1386 | /* |
| 1387 | * So on first glance it looks freaky that we pin the batch here |
| 1388 | * outside of the reservation loop. But: |
| 1389 | * - The batch is already pinned into the relevant ppgtt, so we |
| 1390 | * already have the backing storage fully allocated. |
| 1391 | * - No other BO uses the global gtt (well contexts, but meh), |
| 1392 | * so we don't really have issues with mutliple objects not |
| 1393 | * fitting due to fragmentation. |
| 1394 | * So this is actually safe. |
| 1395 | */ |
| 1396 | ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0); |
| 1397 | if (ret) |
| 1398 | goto err; |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1399 | |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1400 | exec_start += i915_gem_obj_ggtt_offset(batch_obj); |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame^] | 1401 | } else |
Ben Widawsky | 7e0d96b | 2013-12-06 14:11:26 -0800 | [diff] [blame] | 1402 | exec_start += i915_gem_obj_offset(batch_obj, vm); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1403 | |
Oscar Mateo | 7838259 | 2014-07-03 16:28:05 +0100 | [diff] [blame] | 1404 | ret = legacy_ringbuffer_submission(dev, file, ring, ctx, |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame^] | 1405 | args, &eb->vmas, batch_obj, exec_start, flags); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1406 | |
Daniel Vetter | da51a1e | 2014-08-11 12:08:58 +0200 | [diff] [blame^] | 1407 | /* |
| 1408 | * FIXME: We crucially rely upon the active tracking for the (ppgtt) |
| 1409 | * batch vma for correctness. For less ugly and less fragility this |
| 1410 | * needs to be adjusted to also track the ggtt batch vma properly as |
| 1411 | * active. |
| 1412 | */ |
| 1413 | if (flags & I915_DISPATCH_SECURE) |
| 1414 | i915_gem_object_ggtt_unpin(batch_obj); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1415 | err: |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1416 | /* the request owns the ref now */ |
| 1417 | i915_gem_context_unreference(ctx); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1418 | eb_destroy(eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1419 | |
| 1420 | mutex_unlock(&dev->struct_mutex); |
| 1421 | |
| 1422 | pre_mutex_err: |
Paulo Zanoni | f65c916 | 2013-11-27 18:20:34 -0200 | [diff] [blame] | 1423 | /* intel_gpu_busy should also get a ref, so it will free when the device |
| 1424 | * is really idle. */ |
| 1425 | intel_runtime_pm_put(dev_priv); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | /* |
| 1430 | * Legacy execbuffer just creates an exec2 list from the original exec object |
| 1431 | * list array and passes it to the real function. |
| 1432 | */ |
| 1433 | int |
| 1434 | i915_gem_execbuffer(struct drm_device *dev, void *data, |
| 1435 | struct drm_file *file) |
| 1436 | { |
| 1437 | struct drm_i915_gem_execbuffer *args = data; |
| 1438 | struct drm_i915_gem_execbuffer2 exec2; |
| 1439 | struct drm_i915_gem_exec_object *exec_list = NULL; |
| 1440 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 1441 | int ret, i; |
| 1442 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1443 | if (args->buffer_count < 1) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1444 | DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1445 | return -EINVAL; |
| 1446 | } |
| 1447 | |
| 1448 | /* Copy in the exec list from userland */ |
| 1449 | exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count); |
| 1450 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 1451 | if (exec_list == NULL || exec2_list == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1452 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1453 | args->buffer_count); |
| 1454 | drm_free_large(exec_list); |
| 1455 | drm_free_large(exec2_list); |
| 1456 | return -ENOMEM; |
| 1457 | } |
| 1458 | ret = copy_from_user(exec_list, |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 1459 | to_user_ptr(args->buffers_ptr), |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1460 | sizeof(*exec_list) * args->buffer_count); |
| 1461 | if (ret != 0) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1462 | DRM_DEBUG("copy %d exec entries failed %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1463 | args->buffer_count, ret); |
| 1464 | drm_free_large(exec_list); |
| 1465 | drm_free_large(exec2_list); |
| 1466 | return -EFAULT; |
| 1467 | } |
| 1468 | |
| 1469 | for (i = 0; i < args->buffer_count; i++) { |
| 1470 | exec2_list[i].handle = exec_list[i].handle; |
| 1471 | exec2_list[i].relocation_count = exec_list[i].relocation_count; |
| 1472 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; |
| 1473 | exec2_list[i].alignment = exec_list[i].alignment; |
| 1474 | exec2_list[i].offset = exec_list[i].offset; |
| 1475 | if (INTEL_INFO(dev)->gen < 4) |
| 1476 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; |
| 1477 | else |
| 1478 | exec2_list[i].flags = 0; |
| 1479 | } |
| 1480 | |
| 1481 | exec2.buffers_ptr = args->buffers_ptr; |
| 1482 | exec2.buffer_count = args->buffer_count; |
| 1483 | exec2.batch_start_offset = args->batch_start_offset; |
| 1484 | exec2.batch_len = args->batch_len; |
| 1485 | exec2.DR1 = args->DR1; |
| 1486 | exec2.DR4 = args->DR4; |
| 1487 | exec2.num_cliprects = args->num_cliprects; |
| 1488 | exec2.cliprects_ptr = args->cliprects_ptr; |
| 1489 | exec2.flags = I915_EXEC_RENDER; |
Ben Widawsky | 6e0a69d | 2012-06-04 14:42:55 -0700 | [diff] [blame] | 1490 | i915_execbuffer2_set_context_id(exec2, 0); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1491 | |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1492 | ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1493 | if (!ret) { |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 1494 | struct drm_i915_gem_exec_object __user *user_exec_list = |
| 1495 | to_user_ptr(args->buffers_ptr); |
| 1496 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1497 | /* Copy the new buffer offsets back to the user's exec list. */ |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 1498 | for (i = 0; i < args->buffer_count; i++) { |
| 1499 | ret = __copy_to_user(&user_exec_list[i].offset, |
| 1500 | &exec2_list[i].offset, |
| 1501 | sizeof(user_exec_list[i].offset)); |
| 1502 | if (ret) { |
| 1503 | ret = -EFAULT; |
| 1504 | DRM_DEBUG("failed to copy %d exec entries " |
| 1505 | "back to user (%d)\n", |
| 1506 | args->buffer_count, ret); |
| 1507 | break; |
| 1508 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | drm_free_large(exec_list); |
| 1513 | drm_free_large(exec2_list); |
| 1514 | return ret; |
| 1515 | } |
| 1516 | |
| 1517 | int |
| 1518 | i915_gem_execbuffer2(struct drm_device *dev, void *data, |
| 1519 | struct drm_file *file) |
| 1520 | { |
| 1521 | struct drm_i915_gem_execbuffer2 *args = data; |
| 1522 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 1523 | int ret; |
| 1524 | |
Xi Wang | ed8cd3b | 2012-04-23 04:06:41 -0400 | [diff] [blame] | 1525 | if (args->buffer_count < 1 || |
| 1526 | args->buffer_count > UINT_MAX / sizeof(*exec2_list)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1527 | DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1528 | return -EINVAL; |
| 1529 | } |
| 1530 | |
Daniel Vetter | 9cb3466 | 2014-04-24 08:09:11 +0200 | [diff] [blame] | 1531 | if (args->rsvd2 != 0) { |
| 1532 | DRM_DEBUG("dirty rvsd2 field\n"); |
| 1533 | return -EINVAL; |
| 1534 | } |
| 1535 | |
Chris Wilson | 8408c28 | 2011-02-21 12:54:48 +0000 | [diff] [blame] | 1536 | exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count, |
Chris Wilson | 419fa72 | 2013-01-08 10:53:13 +0000 | [diff] [blame] | 1537 | GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); |
Chris Wilson | 8408c28 | 2011-02-21 12:54:48 +0000 | [diff] [blame] | 1538 | if (exec2_list == NULL) |
| 1539 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), |
| 1540 | args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1541 | if (exec2_list == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1542 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1543 | args->buffer_count); |
| 1544 | return -ENOMEM; |
| 1545 | } |
| 1546 | ret = copy_from_user(exec2_list, |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 1547 | to_user_ptr(args->buffers_ptr), |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1548 | sizeof(*exec2_list) * args->buffer_count); |
| 1549 | if (ret != 0) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1550 | DRM_DEBUG("copy %d exec entries failed %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1551 | args->buffer_count, ret); |
| 1552 | drm_free_large(exec2_list); |
| 1553 | return -EFAULT; |
| 1554 | } |
| 1555 | |
Ben Widawsky | 41bde55 | 2013-12-06 14:11:21 -0800 | [diff] [blame] | 1556 | ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1557 | if (!ret) { |
| 1558 | /* Copy the new buffer offsets back to the user's exec list. */ |
Ville Syrjälä | d593d99 | 2014-06-13 16:42:51 +0300 | [diff] [blame] | 1559 | struct drm_i915_gem_exec_object2 __user *user_exec_list = |
Chris Wilson | 9aab8bf | 2014-05-23 10:45:52 +0100 | [diff] [blame] | 1560 | to_user_ptr(args->buffers_ptr); |
| 1561 | int i; |
| 1562 | |
| 1563 | for (i = 0; i < args->buffer_count; i++) { |
| 1564 | ret = __copy_to_user(&user_exec_list[i].offset, |
| 1565 | &exec2_list[i].offset, |
| 1566 | sizeof(user_exec_list[i].offset)); |
| 1567 | if (ret) { |
| 1568 | ret = -EFAULT; |
| 1569 | DRM_DEBUG("failed to copy %d exec entries " |
| 1570 | "back to user\n", |
| 1571 | args->buffer_count); |
| 1572 | break; |
| 1573 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1574 | } |
| 1575 | } |
| 1576 | |
| 1577 | drm_free_large(exec2_list); |
| 1578 | return ret; |
| 1579 | } |