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