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