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 | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 36 | struct eb_objects { |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 37 | struct list_head objects; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 38 | int and; |
| 39 | struct hlist_head buckets[0]; |
| 40 | }; |
| 41 | |
| 42 | static struct eb_objects * |
| 43 | eb_create(int size) |
| 44 | { |
| 45 | struct eb_objects *eb; |
| 46 | int count = PAGE_SIZE / sizeof(struct hlist_head) / 2; |
Chris Wilson | 41783ee | 2012-09-18 10:04:02 +0100 | [diff] [blame] | 47 | BUILD_BUG_ON(!is_power_of_2(PAGE_SIZE / sizeof(struct hlist_head))); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 48 | while (count > size) |
| 49 | count >>= 1; |
| 50 | eb = kzalloc(count*sizeof(struct hlist_head) + |
| 51 | sizeof(struct eb_objects), |
| 52 | GFP_KERNEL); |
| 53 | if (eb == NULL) |
| 54 | return eb; |
| 55 | |
| 56 | eb->and = count - 1; |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 57 | INIT_LIST_HEAD(&eb->objects); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 58 | return eb; |
| 59 | } |
| 60 | |
| 61 | static void |
| 62 | eb_reset(struct eb_objects *eb) |
| 63 | { |
| 64 | memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head)); |
| 65 | } |
| 66 | |
| 67 | static void |
| 68 | eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj) |
| 69 | { |
| 70 | hlist_add_head(&obj->exec_node, |
| 71 | &eb->buckets[obj->exec_handle & eb->and]); |
| 72 | } |
| 73 | |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 74 | static int |
| 75 | eb_lookup_objects(struct eb_objects *eb, |
| 76 | struct drm_i915_gem_exec_object2 *exec, |
| 77 | int count, |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 78 | struct drm_file *file) |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 79 | { |
| 80 | int i; |
| 81 | |
| 82 | spin_lock(&file->table_lock); |
| 83 | for (i = 0; i < count; i++) { |
| 84 | struct drm_i915_gem_object *obj; |
| 85 | |
| 86 | obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle)); |
| 87 | if (obj == NULL) { |
| 88 | spin_unlock(&file->table_lock); |
| 89 | DRM_DEBUG("Invalid object handle %d at index %d\n", |
| 90 | exec[i].handle, i); |
| 91 | return -ENOENT; |
| 92 | } |
| 93 | |
| 94 | if (!list_empty(&obj->exec_list)) { |
| 95 | spin_unlock(&file->table_lock); |
| 96 | DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n", |
| 97 | obj, exec[i].handle, i); |
| 98 | return -EINVAL; |
| 99 | } |
| 100 | |
| 101 | drm_gem_object_reference(&obj->base); |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 102 | list_add_tail(&obj->exec_list, &eb->objects); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 103 | |
| 104 | obj->exec_handle = exec[i].handle; |
| 105 | obj->exec_entry = &exec[i]; |
| 106 | eb_add_object(eb, obj); |
| 107 | } |
| 108 | spin_unlock(&file->table_lock); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 113 | static struct drm_i915_gem_object * |
| 114 | eb_get_object(struct eb_objects *eb, unsigned long handle) |
| 115 | { |
| 116 | struct hlist_head *head; |
| 117 | struct hlist_node *node; |
| 118 | struct drm_i915_gem_object *obj; |
| 119 | |
| 120 | head = &eb->buckets[handle & eb->and]; |
| 121 | hlist_for_each(node, head) { |
| 122 | obj = hlist_entry(node, struct drm_i915_gem_object, exec_node); |
| 123 | if (obj->exec_handle == handle) |
| 124 | return obj; |
| 125 | } |
| 126 | |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | static void |
| 131 | eb_destroy(struct eb_objects *eb) |
| 132 | { |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 133 | while (!list_empty(&eb->objects)) { |
| 134 | struct drm_i915_gem_object *obj; |
| 135 | |
| 136 | obj = list_first_entry(&eb->objects, |
| 137 | struct drm_i915_gem_object, |
| 138 | exec_list); |
| 139 | list_del_init(&obj->exec_list); |
| 140 | drm_gem_object_unreference(&obj->base); |
| 141 | } |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 142 | kfree(eb); |
| 143 | } |
| 144 | |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 145 | static inline int use_cpu_reloc(struct drm_i915_gem_object *obj) |
| 146 | { |
| 147 | return (obj->base.write_domain == I915_GEM_DOMAIN_CPU || |
Chris Wilson | 504c726 | 2012-08-23 13:12:52 +0100 | [diff] [blame] | 148 | !obj->map_and_fenceable || |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 149 | obj->cache_level != I915_CACHE_NONE); |
| 150 | } |
| 151 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 152 | static int |
| 153 | i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 154 | struct eb_objects *eb, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 155 | struct drm_i915_gem_relocation_entry *reloc) |
| 156 | { |
| 157 | struct drm_device *dev = obj->base.dev; |
| 158 | struct drm_gem_object *target_obj; |
Daniel Vetter | 149c840 | 2012-02-15 23:50:23 +0100 | [diff] [blame] | 159 | struct drm_i915_gem_object *target_i915_obj; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 160 | uint32_t target_offset; |
| 161 | int ret = -EINVAL; |
| 162 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 163 | /* we've already hold a reference to all valid objects */ |
| 164 | target_obj = &eb_get_object(eb, reloc->target_handle)->base; |
| 165 | if (unlikely(target_obj == NULL)) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 166 | return -ENOENT; |
| 167 | |
Daniel Vetter | 149c840 | 2012-02-15 23:50:23 +0100 | [diff] [blame] | 168 | target_i915_obj = to_intel_bo(target_obj); |
| 169 | target_offset = target_i915_obj->gtt_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 170 | |
Eric Anholt | e844b99 | 2012-07-31 15:35:01 -0700 | [diff] [blame] | 171 | /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and |
| 172 | * pipe_control writes because the gpu doesn't properly redirect them |
| 173 | * through the ppgtt for non_secure batchbuffers. */ |
| 174 | if (unlikely(IS_GEN6(dev) && |
| 175 | reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION && |
| 176 | !target_i915_obj->has_global_gtt_mapping)) { |
| 177 | i915_gem_gtt_bind_object(target_i915_obj, |
| 178 | target_i915_obj->cache_level); |
| 179 | } |
| 180 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 181 | /* Validate that the target is in a valid r/w GPU domain */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 182 | if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 183 | DRM_DEBUG("reloc with multiple write domains: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 184 | "obj %p target %d offset %d " |
| 185 | "read %08x write %08x", |
| 186 | obj, reloc->target_handle, |
| 187 | (int) reloc->offset, |
| 188 | reloc->read_domains, |
| 189 | reloc->write_domain); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 190 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 191 | } |
Daniel Vetter | 4ca4a25 | 2011-12-14 13:57:27 +0100 | [diff] [blame] | 192 | if (unlikely((reloc->write_domain | reloc->read_domains) |
| 193 | & ~I915_GEM_GPU_DOMAINS)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 194 | DRM_DEBUG("reloc with read/write non-GPU domains: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 195 | "obj %p target %d offset %d " |
| 196 | "read %08x write %08x", |
| 197 | obj, reloc->target_handle, |
| 198 | (int) reloc->offset, |
| 199 | reloc->read_domains, |
| 200 | reloc->write_domain); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 201 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 202 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 203 | |
| 204 | target_obj->pending_read_domains |= reloc->read_domains; |
| 205 | target_obj->pending_write_domain |= reloc->write_domain; |
| 206 | |
| 207 | /* If the relocation already has the right value in it, no |
| 208 | * more work needs to be done. |
| 209 | */ |
| 210 | if (target_offset == reloc->presumed_offset) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 211 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 212 | |
| 213 | /* Check that the relocation address is valid... */ |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 214 | if (unlikely(reloc->offset > obj->base.size - 4)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 215 | DRM_DEBUG("Relocation beyond object bounds: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 216 | "obj %p target %d offset %d size %d.\n", |
| 217 | obj, reloc->target_handle, |
| 218 | (int) reloc->offset, |
| 219 | (int) obj->base.size); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 220 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 221 | } |
Chris Wilson | b8f7ab1 | 2010-12-08 10:43:06 +0000 | [diff] [blame] | 222 | if (unlikely(reloc->offset & 3)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 223 | DRM_DEBUG("Relocation not 4-byte aligned: " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 224 | "obj %p target %d offset %d.\n", |
| 225 | obj, reloc->target_handle, |
| 226 | (int) reloc->offset); |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 227 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 230 | /* We can't wait for rendering with pagefaults disabled */ |
| 231 | if (obj->active && in_atomic()) |
| 232 | return -EFAULT; |
| 233 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 234 | reloc->delta += target_offset; |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 235 | if (use_cpu_reloc(obj)) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 236 | uint32_t page_offset = reloc->offset & ~PAGE_MASK; |
| 237 | char *vaddr; |
| 238 | |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 239 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
| 240 | if (ret) |
| 241 | return ret; |
| 242 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 243 | vaddr = kmap_atomic(i915_gem_object_get_page(obj, |
| 244 | reloc->offset >> PAGE_SHIFT)); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 245 | *(uint32_t *)(vaddr + page_offset) = reloc->delta; |
| 246 | kunmap_atomic(vaddr); |
| 247 | } else { |
| 248 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 249 | uint32_t __iomem *reloc_entry; |
| 250 | void __iomem *reloc_page; |
| 251 | |
Chris Wilson | 7b09638 | 2012-04-14 09:55:51 +0100 | [diff] [blame] | 252 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 253 | if (ret) |
| 254 | return ret; |
| 255 | |
| 256 | ret = i915_gem_object_put_fence(obj); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 257 | if (ret) |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 258 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 259 | |
| 260 | /* Map the page containing the relocation we're going to perform. */ |
| 261 | reloc->offset += obj->gtt_offset; |
| 262 | reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping, |
| 263 | reloc->offset & PAGE_MASK); |
| 264 | reloc_entry = (uint32_t __iomem *) |
| 265 | (reloc_page + (reloc->offset & ~PAGE_MASK)); |
| 266 | iowrite32(reloc->delta, reloc_entry); |
| 267 | io_mapping_unmap_atomic(reloc_page); |
| 268 | } |
| 269 | |
| 270 | /* and update the user's relocation entry */ |
| 271 | reloc->presumed_offset = target_offset; |
| 272 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 273 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | static int |
| 277 | i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj, |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 278 | struct eb_objects *eb) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 279 | { |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 280 | #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry)) |
| 281 | struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 282 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 283 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 284 | int remain, ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 285 | |
| 286 | user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 287 | |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 288 | remain = entry->relocation_count; |
| 289 | while (remain) { |
| 290 | struct drm_i915_gem_relocation_entry *r = stack_reloc; |
| 291 | int count = remain; |
| 292 | if (count > ARRAY_SIZE(stack_reloc)) |
| 293 | count = ARRAY_SIZE(stack_reloc); |
| 294 | remain -= count; |
| 295 | |
| 296 | if (__copy_from_user_inatomic(r, user_relocs, count*sizeof(r[0]))) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 297 | return -EFAULT; |
| 298 | |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 299 | do { |
| 300 | u64 offset = r->presumed_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 301 | |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 302 | ret = i915_gem_execbuffer_relocate_entry(obj, eb, r); |
| 303 | if (ret) |
| 304 | return ret; |
| 305 | |
| 306 | if (r->presumed_offset != offset && |
| 307 | __copy_to_user_inatomic(&user_relocs->presumed_offset, |
| 308 | &r->presumed_offset, |
| 309 | sizeof(r->presumed_offset))) { |
| 310 | return -EFAULT; |
| 311 | } |
| 312 | |
| 313 | user_relocs++; |
| 314 | r++; |
| 315 | } while (--count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return 0; |
Chris Wilson | 1d83f44 | 2012-03-24 20:12:53 +0000 | [diff] [blame] | 319 | #undef N_RELOC |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static int |
| 323 | i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 324 | struct eb_objects *eb, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 325 | struct drm_i915_gem_relocation_entry *relocs) |
| 326 | { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 327 | const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 328 | int i, ret; |
| 329 | |
| 330 | for (i = 0; i < entry->relocation_count; i++) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 331 | ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 332 | if (ret) |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | static int |
| 340 | i915_gem_execbuffer_relocate(struct drm_device *dev, |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 341 | struct eb_objects *eb) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 342 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 343 | struct drm_i915_gem_object *obj; |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 344 | int ret = 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 345 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 346 | /* This is the fast path and we cannot handle a pagefault whilst |
| 347 | * holding the struct mutex lest the user pass in the relocations |
| 348 | * contained within a mmaped bo. For in such a case we, the page |
| 349 | * fault handler would call i915_gem_fault() and we would try to |
| 350 | * acquire the struct mutex again. Obviously this is bad and so |
| 351 | * lockdep complains vehemently. |
| 352 | */ |
| 353 | pagefault_disable(); |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 354 | list_for_each_entry(obj, &eb->objects, exec_list) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 355 | ret = i915_gem_execbuffer_relocate_object(obj, eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 356 | if (ret) |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 357 | break; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 358 | } |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 359 | pagefault_enable(); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 360 | |
Chris Wilson | d4aeee7 | 2011-03-14 15:11:24 +0000 | [diff] [blame] | 361 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 364 | #define __EXEC_OBJECT_HAS_PIN (1<<31) |
| 365 | #define __EXEC_OBJECT_HAS_FENCE (1<<30) |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 366 | |
| 367 | static int |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 368 | need_reloc_mappable(struct drm_i915_gem_object *obj) |
| 369 | { |
| 370 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
| 371 | return entry->relocation_count && !use_cpu_reloc(obj); |
| 372 | } |
| 373 | |
| 374 | static int |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 375 | i915_gem_execbuffer_reserve_object(struct drm_i915_gem_object *obj, |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 376 | struct intel_ring_buffer *ring, |
| 377 | bool *need_reloc) |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 378 | { |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 379 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 380 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
| 381 | bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4; |
| 382 | bool need_fence, need_mappable; |
| 383 | int ret; |
| 384 | |
| 385 | need_fence = |
| 386 | has_fenced_gpu_access && |
| 387 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 388 | obj->tiling_mode != I915_TILING_NONE; |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 389 | need_mappable = need_fence || need_reloc_mappable(obj); |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 390 | |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 391 | ret = i915_gem_object_pin(obj, entry->alignment, need_mappable, false); |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 392 | if (ret) |
| 393 | return ret; |
| 394 | |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 395 | entry->flags |= __EXEC_OBJECT_HAS_PIN; |
| 396 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 397 | if (has_fenced_gpu_access) { |
| 398 | if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) { |
Chris Wilson | 06d9813 | 2012-04-17 15:31:24 +0100 | [diff] [blame] | 399 | ret = i915_gem_object_get_fence(obj); |
Chris Wilson | 9a5a53b | 2012-03-22 15:10:00 +0000 | [diff] [blame] | 400 | if (ret) |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 401 | return ret; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 402 | |
Chris Wilson | 9a5a53b | 2012-03-22 15:10:00 +0000 | [diff] [blame] | 403 | if (i915_gem_object_pin_fence(obj)) |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 404 | entry->flags |= __EXEC_OBJECT_HAS_FENCE; |
Chris Wilson | 9a5a53b | 2012-03-22 15:10:00 +0000 | [diff] [blame] | 405 | |
Chris Wilson | 7dd4906 | 2012-03-21 10:48:18 +0000 | [diff] [blame] | 406 | obj->pending_fenced_gpu_access = true; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 407 | } |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 410 | /* Ensure ppgtt mapping exists if needed */ |
| 411 | if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) { |
| 412 | i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt, |
| 413 | obj, obj->cache_level); |
| 414 | |
| 415 | obj->has_aliasing_ppgtt_mapping = 1; |
| 416 | } |
| 417 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 418 | if (entry->offset != obj->gtt_offset) { |
| 419 | entry->offset = obj->gtt_offset; |
| 420 | *need_reloc = true; |
| 421 | } |
| 422 | |
| 423 | if (entry->flags & EXEC_OBJECT_WRITE) { |
| 424 | obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER; |
| 425 | obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER; |
| 426 | } |
| 427 | |
| 428 | if (entry->flags & EXEC_OBJECT_NEEDS_GTT && |
| 429 | !obj->has_global_gtt_mapping) |
| 430 | i915_gem_gtt_bind_object(obj, obj->cache_level); |
| 431 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 432 | return 0; |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 433 | } |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 434 | |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 435 | static void |
| 436 | i915_gem_execbuffer_unreserve_object(struct drm_i915_gem_object *obj) |
| 437 | { |
| 438 | struct drm_i915_gem_exec_object2 *entry; |
| 439 | |
| 440 | if (!obj->gtt_space) |
| 441 | return; |
| 442 | |
| 443 | entry = obj->exec_entry; |
| 444 | |
| 445 | if (entry->flags & __EXEC_OBJECT_HAS_FENCE) |
| 446 | i915_gem_object_unpin_fence(obj); |
| 447 | |
| 448 | if (entry->flags & __EXEC_OBJECT_HAS_PIN) |
| 449 | i915_gem_object_unpin(obj); |
| 450 | |
| 451 | entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN); |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 452 | } |
| 453 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 454 | static int |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 455 | i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 456 | struct drm_file *file, |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 457 | struct list_head *objects, |
| 458 | bool *need_relocs) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 459 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 460 | struct drm_i915_gem_object *obj; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 461 | struct list_head ordered_objects; |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 462 | bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4; |
| 463 | int retry; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 464 | |
| 465 | INIT_LIST_HEAD(&ordered_objects); |
| 466 | while (!list_empty(objects)) { |
| 467 | struct drm_i915_gem_exec_object2 *entry; |
| 468 | bool need_fence, need_mappable; |
| 469 | |
| 470 | obj = list_first_entry(objects, |
| 471 | struct drm_i915_gem_object, |
| 472 | exec_list); |
| 473 | entry = obj->exec_entry; |
| 474 | |
| 475 | need_fence = |
| 476 | has_fenced_gpu_access && |
| 477 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 478 | obj->tiling_mode != I915_TILING_NONE; |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 479 | need_mappable = need_fence || need_reloc_mappable(obj); |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 480 | |
| 481 | if (need_mappable) |
| 482 | list_move(&obj->exec_list, &ordered_objects); |
| 483 | else |
| 484 | list_move_tail(&obj->exec_list, &ordered_objects); |
Chris Wilson | 595dad7 | 2011-01-13 11:03:48 +0000 | [diff] [blame] | 485 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 486 | 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] | 487 | obj->base.pending_write_domain = 0; |
Chris Wilson | 016fd0c | 2012-07-20 12:41:07 +0100 | [diff] [blame] | 488 | obj->pending_fenced_gpu_access = false; |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 489 | } |
| 490 | list_splice(&ordered_objects, objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 491 | |
| 492 | /* Attempt to pin all of the buffers into the GTT. |
| 493 | * This is done in 3 phases: |
| 494 | * |
| 495 | * 1a. Unbind all objects that do not match the GTT constraints for |
| 496 | * the execbuffer (fenceable, mappable, alignment etc). |
| 497 | * 1b. Increment pin count for already bound objects. |
| 498 | * 2. Bind new objects. |
| 499 | * 3. Decrement pin count. |
| 500 | * |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 501 | * This avoid unnecessary unbinding of later objects in order to make |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 502 | * room for the earlier objects *unless* we need to defragment. |
| 503 | */ |
| 504 | retry = 0; |
| 505 | do { |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 506 | int ret = 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 507 | |
| 508 | /* Unbind any ill-fitting objects or pin. */ |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 509 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 510 | struct drm_i915_gem_exec_object2 *entry = obj->exec_entry; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 511 | bool need_fence, need_mappable; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 512 | |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 513 | if (!obj->gtt_space) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 514 | continue; |
| 515 | |
| 516 | need_fence = |
Chris Wilson | 9b3826b | 2010-12-05 17:11:54 +0000 | [diff] [blame] | 517 | has_fenced_gpu_access && |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 518 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 519 | obj->tiling_mode != I915_TILING_NONE; |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 520 | need_mappable = need_fence || need_reloc_mappable(obj); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 521 | |
| 522 | if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) || |
| 523 | (need_mappable && !obj->map_and_fenceable)) |
| 524 | ret = i915_gem_object_unbind(obj); |
| 525 | else |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 526 | ret = i915_gem_execbuffer_reserve_object(obj, ring, need_relocs); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 527 | if (ret) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 528 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* Bind fresh objects */ |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 532 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 533 | if (obj->gtt_space) |
| 534 | continue; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 535 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 536 | ret = i915_gem_execbuffer_reserve_object(obj, ring, need_relocs); |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 537 | if (ret) |
| 538 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Chris Wilson | 7788a76 | 2012-08-24 19:18:18 +0100 | [diff] [blame] | 541 | err: /* Decrement pin count for bound objects */ |
| 542 | list_for_each_entry(obj, objects, exec_list) |
| 543 | i915_gem_execbuffer_unreserve_object(obj); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 544 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 545 | if (ret != -ENOSPC || retry++) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 546 | return ret; |
| 547 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 548 | ret = i915_gem_evict_everything(ring->dev); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 549 | if (ret) |
| 550 | return ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 551 | } while (1); |
| 552 | } |
| 553 | |
| 554 | static int |
| 555 | i915_gem_execbuffer_relocate_slow(struct drm_device *dev, |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 556 | struct drm_i915_gem_execbuffer2 *args, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 557 | struct drm_file *file, |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 558 | struct intel_ring_buffer *ring, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 559 | struct eb_objects *eb, |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 560 | struct drm_i915_gem_exec_object2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 561 | { |
| 562 | struct drm_i915_gem_relocation_entry *reloc; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 563 | struct drm_i915_gem_object *obj; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 564 | bool need_relocs; |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 565 | int *reloc_offset; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 566 | int i, total, ret; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 567 | int count = args->buffer_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 568 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 569 | /* We may process another execbuffer during the unlock... */ |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 570 | while (!list_empty(&eb->objects)) { |
| 571 | obj = list_first_entry(&eb->objects, |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 572 | struct drm_i915_gem_object, |
| 573 | exec_list); |
| 574 | list_del_init(&obj->exec_list); |
| 575 | drm_gem_object_unreference(&obj->base); |
| 576 | } |
| 577 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 578 | mutex_unlock(&dev->struct_mutex); |
| 579 | |
| 580 | total = 0; |
| 581 | for (i = 0; i < count; i++) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 582 | total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 583 | |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 584 | reloc_offset = drm_malloc_ab(count, sizeof(*reloc_offset)); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 585 | reloc = drm_malloc_ab(total, sizeof(*reloc)); |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 586 | if (reloc == NULL || reloc_offset == NULL) { |
| 587 | drm_free_large(reloc); |
| 588 | drm_free_large(reloc_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 589 | mutex_lock(&dev->struct_mutex); |
| 590 | return -ENOMEM; |
| 591 | } |
| 592 | |
| 593 | total = 0; |
| 594 | for (i = 0; i < count; i++) { |
| 595 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
| 596 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 597 | user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 598 | |
| 599 | if (copy_from_user(reloc+total, user_relocs, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 600 | exec[i].relocation_count * sizeof(*reloc))) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 601 | ret = -EFAULT; |
| 602 | mutex_lock(&dev->struct_mutex); |
| 603 | goto err; |
| 604 | } |
| 605 | |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 606 | reloc_offset[i] = total; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 607 | total += exec[i].relocation_count; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | ret = i915_mutex_lock_interruptible(dev); |
| 611 | if (ret) { |
| 612 | mutex_lock(&dev->struct_mutex); |
| 613 | goto err; |
| 614 | } |
| 615 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 616 | /* reacquire the objects */ |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 617 | eb_reset(eb); |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 618 | ret = eb_lookup_objects(eb, exec, count, file); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 619 | if (ret) |
| 620 | goto err; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 621 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 622 | need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0; |
| 623 | ret = i915_gem_execbuffer_reserve(ring, file, &eb->objects, &need_relocs); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 624 | if (ret) |
| 625 | goto err; |
| 626 | |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 627 | list_for_each_entry(obj, &eb->objects, exec_list) { |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 628 | int offset = obj->exec_entry - exec; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 629 | ret = i915_gem_execbuffer_relocate_object_slow(obj, eb, |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 630 | reloc + reloc_offset[offset]); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 631 | if (ret) |
| 632 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | /* Leave the user relocations as are, this is the painfully slow path, |
| 636 | * and we want to avoid the complication of dropping the lock whilst |
| 637 | * having buffers reserved in the aperture and so causing spurious |
| 638 | * ENOSPC for random operations. |
| 639 | */ |
| 640 | |
| 641 | err: |
| 642 | drm_free_large(reloc); |
Chris Wilson | dd6864a | 2011-01-12 23:49:13 +0000 | [diff] [blame] | 643 | drm_free_large(reloc_offset); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 644 | return ret; |
| 645 | } |
| 646 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 647 | static int |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 648 | i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring, |
| 649 | struct list_head *objects) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 650 | { |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 651 | struct drm_i915_gem_object *obj; |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 652 | uint32_t flush_domains = 0; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 653 | int ret; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 654 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 655 | list_for_each_entry(obj, objects, exec_list) { |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame] | 656 | ret = i915_gem_object_sync(obj, ring); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 657 | if (ret) |
| 658 | return ret; |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 659 | |
| 660 | if (obj->base.write_domain & I915_GEM_DOMAIN_CPU) |
| 661 | i915_gem_clflush_object(obj); |
| 662 | |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 663 | flush_domains |= obj->base.write_domain; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 666 | if (flush_domains & I915_GEM_DOMAIN_CPU) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 667 | i915_gem_chipset_flush(ring->dev); |
Daniel Vetter | 6ac42f4 | 2012-07-21 12:25:01 +0200 | [diff] [blame] | 668 | |
| 669 | if (flush_domains & I915_GEM_DOMAIN_GTT) |
| 670 | wmb(); |
| 671 | |
Chris Wilson | 09cf7c9 | 2012-07-13 14:14:08 +0100 | [diff] [blame] | 672 | /* Unconditionally invalidate gpu caches and ensure that we do flush |
| 673 | * any residual writes from the previous batch. |
| 674 | */ |
Chris Wilson | a7b9761 | 2012-07-20 12:41:08 +0100 | [diff] [blame] | 675 | return intel_ring_invalidate_all_caches(ring); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 678 | static bool |
| 679 | i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 680 | { |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 681 | if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS) |
| 682 | return false; |
| 683 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 684 | return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static int |
| 688 | validate_exec_list(struct drm_i915_gem_exec_object2 *exec, |
| 689 | int count) |
| 690 | { |
| 691 | int i; |
| 692 | |
| 693 | for (i = 0; i < count; i++) { |
| 694 | char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr; |
| 695 | int length; /* limited by fault_in_pages_readable() */ |
| 696 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 697 | if (exec[i].flags & __EXEC_OBJECT_UNKNOWN_FLAGS) |
| 698 | return -EINVAL; |
| 699 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 700 | /* First check for malicious input causing overflow */ |
| 701 | if (exec[i].relocation_count > |
| 702 | INT_MAX / sizeof(struct drm_i915_gem_relocation_entry)) |
| 703 | return -EINVAL; |
| 704 | |
| 705 | length = exec[i].relocation_count * |
| 706 | sizeof(struct drm_i915_gem_relocation_entry); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 707 | /* we may also need to update the presumed offsets */ |
| 708 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
| 709 | return -EFAULT; |
| 710 | |
Daniel Vetter | f56f821 | 2012-03-25 19:47:41 +0200 | [diff] [blame] | 711 | if (fault_in_multipages_readable(ptr, length)) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 712 | return -EFAULT; |
| 713 | } |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 718 | static void |
| 719 | i915_gem_execbuffer_move_to_active(struct list_head *objects, |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 720 | struct intel_ring_buffer *ring) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 721 | { |
| 722 | struct drm_i915_gem_object *obj; |
| 723 | |
| 724 | list_for_each_entry(obj, objects, exec_list) { |
Chris Wilson | 69c2fc8 | 2012-07-20 12:41:03 +0100 | [diff] [blame] | 725 | u32 old_read = obj->base.read_domains; |
| 726 | u32 old_write = obj->base.write_domain; |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 727 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 728 | obj->base.write_domain = obj->base.pending_write_domain; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 729 | if (obj->base.write_domain == 0) |
| 730 | obj->base.pending_read_domains |= obj->base.read_domains; |
| 731 | obj->base.read_domains = obj->base.pending_read_domains; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 732 | obj->fenced_gpu_access = obj->pending_fenced_gpu_access; |
| 733 | |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 734 | i915_gem_object_move_to_active(obj, ring); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 735 | if (obj->base.write_domain) { |
| 736 | obj->dirty = 1; |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 737 | obj->last_write_seqno = intel_ring_get_seqno(ring); |
Chris Wilson | acb87df | 2012-05-03 15:47:57 +0100 | [diff] [blame] | 738 | if (obj->pin_count) /* check for potential scanout */ |
Chris Wilson | f047e39 | 2012-07-21 12:31:41 +0100 | [diff] [blame] | 739 | intel_mark_fb_busy(obj); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 742 | trace_i915_gem_object_change_domain(obj, old_read, old_write); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 746 | static void |
| 747 | i915_gem_execbuffer_retire_commands(struct drm_device *dev, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 748 | struct drm_file *file, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 749 | struct intel_ring_buffer *ring) |
| 750 | { |
Daniel Vetter | cc889e0 | 2012-06-13 20:45:19 +0200 | [diff] [blame] | 751 | /* Unconditionally force add_request to emit a full flush. */ |
| 752 | ring->gpu_caches_dirty = true; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 753 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 754 | /* Add a breadcrumb for the completion of the batch buffer */ |
Chris Wilson | 3bb73ab | 2012-07-20 12:40:59 +0100 | [diff] [blame] | 755 | (void)i915_add_request(ring, file, NULL); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 756 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 757 | |
| 758 | static int |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 759 | i915_reset_gen7_sol_offsets(struct drm_device *dev, |
| 760 | struct intel_ring_buffer *ring) |
| 761 | { |
| 762 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 763 | int ret, i; |
| 764 | |
| 765 | if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) |
| 766 | return 0; |
| 767 | |
| 768 | ret = intel_ring_begin(ring, 4 * 3); |
| 769 | if (ret) |
| 770 | return ret; |
| 771 | |
| 772 | for (i = 0; i < 4; i++) { |
| 773 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 774 | intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i)); |
| 775 | intel_ring_emit(ring, 0); |
| 776 | } |
| 777 | |
| 778 | intel_ring_advance(ring); |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | static int |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 784 | i915_gem_do_execbuffer(struct drm_device *dev, void *data, |
| 785 | struct drm_file *file, |
| 786 | struct drm_i915_gem_execbuffer2 *args, |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 787 | struct drm_i915_gem_exec_object2 *exec) |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 788 | { |
| 789 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 790 | struct eb_objects *eb; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 791 | struct drm_i915_gem_object *batch_obj; |
| 792 | struct drm_clip_rect *cliprects = NULL; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 793 | struct intel_ring_buffer *ring; |
Ben Widawsky | 6e0a69d | 2012-06-04 14:42:55 -0700 | [diff] [blame] | 794 | u32 ctx_id = i915_execbuffer2_get_context_id(*args); |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 795 | u32 exec_start, exec_len; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 796 | u32 mask, flags; |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 797 | int ret, mode, i; |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 798 | bool need_relocs; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 799 | |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 800 | if (!i915_gem_check_execbuffer(args)) |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 801 | return -EINVAL; |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 802 | |
| 803 | ret = validate_exec_list(exec, args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 804 | if (ret) |
| 805 | return ret; |
| 806 | |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 807 | flags = 0; |
| 808 | if (args->flags & I915_EXEC_SECURE) { |
| 809 | if (!file->is_master || !capable(CAP_SYS_ADMIN)) |
| 810 | return -EPERM; |
| 811 | |
| 812 | flags |= I915_DISPATCH_SECURE; |
| 813 | } |
Daniel Vetter | b45305f | 2012-12-17 16:21:27 +0100 | [diff] [blame] | 814 | if (args->flags & I915_EXEC_IS_PINNED) |
| 815 | flags |= I915_DISPATCH_PINNED; |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 816 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 817 | switch (args->flags & I915_EXEC_RING_MASK) { |
| 818 | case I915_EXEC_DEFAULT: |
| 819 | case I915_EXEC_RENDER: |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 820 | ring = &dev_priv->ring[RCS]; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 821 | break; |
| 822 | case I915_EXEC_BSD: |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 823 | ring = &dev_priv->ring[VCS]; |
Ben Widawsky | 6e0a69d | 2012-06-04 14:42:55 -0700 | [diff] [blame] | 824 | if (ctx_id != 0) { |
| 825 | DRM_DEBUG("Ring %s doesn't support contexts\n", |
| 826 | ring->name); |
| 827 | return -EPERM; |
| 828 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 829 | break; |
| 830 | case I915_EXEC_BLT: |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 831 | ring = &dev_priv->ring[BCS]; |
Ben Widawsky | 6e0a69d | 2012-06-04 14:42:55 -0700 | [diff] [blame] | 832 | if (ctx_id != 0) { |
| 833 | DRM_DEBUG("Ring %s doesn't support contexts\n", |
| 834 | ring->name); |
| 835 | return -EPERM; |
| 836 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 837 | break; |
| 838 | default: |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 839 | DRM_DEBUG("execbuf with unknown ring: %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 840 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 841 | return -EINVAL; |
| 842 | } |
Chris Wilson | a15817c | 2012-05-11 14:29:31 +0100 | [diff] [blame] | 843 | if (!intel_ring_initialized(ring)) { |
| 844 | DRM_DEBUG("execbuf with invalid ring: %d\n", |
| 845 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 846 | return -EINVAL; |
| 847 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 848 | |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 849 | mode = args->flags & I915_EXEC_CONSTANTS_MASK; |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame] | 850 | mask = I915_EXEC_CONSTANTS_MASK; |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 851 | switch (mode) { |
| 852 | case I915_EXEC_CONSTANTS_REL_GENERAL: |
| 853 | case I915_EXEC_CONSTANTS_ABSOLUTE: |
| 854 | case I915_EXEC_CONSTANTS_REL_SURFACE: |
| 855 | if (ring == &dev_priv->ring[RCS] && |
| 856 | mode != dev_priv->relative_constants_mode) { |
| 857 | if (INTEL_INFO(dev)->gen < 4) |
| 858 | return -EINVAL; |
| 859 | |
| 860 | if (INTEL_INFO(dev)->gen > 5 && |
| 861 | mode == I915_EXEC_CONSTANTS_REL_SURFACE) |
| 862 | return -EINVAL; |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame] | 863 | |
| 864 | /* The HW changed the meaning on this bit on gen6 */ |
| 865 | if (INTEL_INFO(dev)->gen >= 6) |
| 866 | mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE; |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 867 | } |
| 868 | break; |
| 869 | default: |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 870 | DRM_DEBUG("execbuf with unknown constants: %d\n", mode); |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 871 | return -EINVAL; |
| 872 | } |
| 873 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 874 | if (args->buffer_count < 1) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 875 | DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 876 | return -EINVAL; |
| 877 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 878 | |
| 879 | if (args->num_cliprects != 0) { |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 880 | if (ring != &dev_priv->ring[RCS]) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 881 | DRM_DEBUG("clip rectangles are only valid with the render ring\n"); |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 882 | return -EINVAL; |
| 883 | } |
| 884 | |
Daniel Vetter | 6ebebc9 | 2012-04-26 23:28:11 +0200 | [diff] [blame] | 885 | if (INTEL_INFO(dev)->gen >= 5) { |
| 886 | DRM_DEBUG("clip rectangles are only valid on pre-gen5\n"); |
| 887 | return -EINVAL; |
| 888 | } |
| 889 | |
Xi Wang | 44afb3a | 2012-04-23 04:06:42 -0400 | [diff] [blame] | 890 | if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) { |
| 891 | DRM_DEBUG("execbuf with %u cliprects\n", |
| 892 | args->num_cliprects); |
| 893 | return -EINVAL; |
| 894 | } |
Daniel Vetter | 5e13a0c | 2012-05-08 13:39:59 +0200 | [diff] [blame] | 895 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 896 | cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects), |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 897 | GFP_KERNEL); |
| 898 | if (cliprects == NULL) { |
| 899 | ret = -ENOMEM; |
| 900 | goto pre_mutex_err; |
| 901 | } |
| 902 | |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 903 | if (copy_from_user(cliprects, |
| 904 | (struct drm_clip_rect __user *)(uintptr_t) |
| 905 | args->cliprects_ptr, |
| 906 | sizeof(*cliprects)*args->num_cliprects)) { |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 907 | ret = -EFAULT; |
| 908 | goto pre_mutex_err; |
| 909 | } |
| 910 | } |
| 911 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 912 | ret = i915_mutex_lock_interruptible(dev); |
| 913 | if (ret) |
| 914 | goto pre_mutex_err; |
| 915 | |
| 916 | if (dev_priv->mm.suspended) { |
| 917 | mutex_unlock(&dev->struct_mutex); |
| 918 | ret = -EBUSY; |
| 919 | goto pre_mutex_err; |
| 920 | } |
| 921 | |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 922 | eb = eb_create(args->buffer_count); |
| 923 | if (eb == NULL) { |
| 924 | mutex_unlock(&dev->struct_mutex); |
| 925 | ret = -ENOMEM; |
| 926 | goto pre_mutex_err; |
| 927 | } |
| 928 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 929 | /* Look up object handles */ |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 930 | ret = eb_lookup_objects(eb, exec, args->buffer_count, file); |
Chris Wilson | 3b96eff | 2013-01-08 10:53:14 +0000 | [diff] [blame] | 931 | if (ret) |
| 932 | goto err; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 933 | |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 934 | /* take note of the batch buffer before we might reorder the lists */ |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 935 | batch_obj = list_entry(eb->objects.prev, |
Chris Wilson | 6fe4f14 | 2011-01-10 17:35:37 +0000 | [diff] [blame] | 936 | struct drm_i915_gem_object, |
| 937 | exec_list); |
| 938 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 939 | /* Move the objects en-masse into the GTT, evicting if necessary. */ |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 940 | need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0; |
| 941 | ret = i915_gem_execbuffer_reserve(ring, file, &eb->objects, &need_relocs); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 942 | if (ret) |
| 943 | goto err; |
| 944 | |
| 945 | /* The objects are in their final locations, apply the relocations. */ |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 946 | if (need_relocs) |
| 947 | ret = i915_gem_execbuffer_relocate(dev, eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 948 | if (ret) { |
| 949 | if (ret == -EFAULT) { |
Daniel Vetter | ed5982e | 2013-01-17 22:23:36 +0100 | [diff] [blame^] | 950 | ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring, |
| 951 | eb, exec); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 952 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 953 | } |
| 954 | if (ret) |
| 955 | goto err; |
| 956 | } |
| 957 | |
| 958 | /* Set the pending read domains for the batch buffer to COMMAND */ |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 959 | if (batch_obj->base.pending_write_domain) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 960 | DRM_DEBUG("Attempting to use self-modifying batch buffer\n"); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 961 | ret = -EINVAL; |
| 962 | goto err; |
| 963 | } |
| 964 | batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND; |
| 965 | |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 966 | /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure |
| 967 | * batch" bit. Hence we need to pin secure batches into the global gtt. |
| 968 | * hsw should have this fixed, but let's be paranoid and do it |
| 969 | * unconditionally for now. */ |
| 970 | if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping) |
| 971 | i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level); |
| 972 | |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 973 | ret = i915_gem_execbuffer_move_to_gpu(ring, &eb->objects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 974 | if (ret) |
| 975 | goto err; |
| 976 | |
Eric Anholt | 0da5cec | 2012-07-23 12:33:55 -0700 | [diff] [blame] | 977 | ret = i915_switch_context(ring, file, ctx_id); |
| 978 | if (ret) |
| 979 | goto err; |
| 980 | |
Ben Widawsky | e2971bd | 2011-12-12 19:21:57 -0800 | [diff] [blame] | 981 | if (ring == &dev_priv->ring[RCS] && |
| 982 | mode != dev_priv->relative_constants_mode) { |
| 983 | ret = intel_ring_begin(ring, 4); |
| 984 | if (ret) |
| 985 | goto err; |
| 986 | |
| 987 | intel_ring_emit(ring, MI_NOOP); |
| 988 | intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); |
| 989 | intel_ring_emit(ring, INSTPM); |
Ben Widawsky | 84f9f93 | 2011-12-12 19:21:58 -0800 | [diff] [blame] | 990 | intel_ring_emit(ring, mask << 16 | mode); |
Ben Widawsky | e2971bd | 2011-12-12 19:21:57 -0800 | [diff] [blame] | 991 | intel_ring_advance(ring); |
| 992 | |
| 993 | dev_priv->relative_constants_mode = mode; |
| 994 | } |
| 995 | |
Eric Anholt | ae662d3 | 2012-01-03 09:23:29 -0800 | [diff] [blame] | 996 | if (args->flags & I915_EXEC_GEN7_SOL_RESET) { |
| 997 | ret = i915_reset_gen7_sol_offsets(dev, ring); |
| 998 | if (ret) |
| 999 | goto err; |
| 1000 | } |
| 1001 | |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 1002 | exec_start = batch_obj->gtt_offset + args->batch_start_offset; |
| 1003 | exec_len = args->batch_len; |
| 1004 | if (cliprects) { |
| 1005 | for (i = 0; i < args->num_cliprects; i++) { |
| 1006 | ret = i915_emit_box(dev, &cliprects[i], |
| 1007 | args->DR1, args->DR4); |
| 1008 | if (ret) |
| 1009 | goto err; |
| 1010 | |
| 1011 | ret = ring->dispatch_execbuffer(ring, |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1012 | exec_start, exec_len, |
| 1013 | flags); |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 1014 | if (ret) |
| 1015 | goto err; |
| 1016 | } |
| 1017 | } else { |
Chris Wilson | d7d4eed | 2012-10-17 12:09:54 +0100 | [diff] [blame] | 1018 | ret = ring->dispatch_execbuffer(ring, |
| 1019 | exec_start, exec_len, |
| 1020 | flags); |
Chris Wilson | c4e7a41 | 2010-11-30 14:10:25 +0000 | [diff] [blame] | 1021 | if (ret) |
| 1022 | goto err; |
| 1023 | } |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1024 | |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1025 | trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags); |
| 1026 | |
Chris Wilson | bcffc3f | 2013-01-08 10:53:15 +0000 | [diff] [blame] | 1027 | i915_gem_execbuffer_move_to_active(&eb->objects, ring); |
Chris Wilson | 432e58e | 2010-11-25 19:32:06 +0000 | [diff] [blame] | 1028 | i915_gem_execbuffer_retire_commands(dev, file, ring); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1029 | |
| 1030 | err: |
Chris Wilson | 67731b8 | 2010-12-08 10:38:14 +0000 | [diff] [blame] | 1031 | eb_destroy(eb); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1032 | |
| 1033 | mutex_unlock(&dev->struct_mutex); |
| 1034 | |
| 1035 | pre_mutex_err: |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1036 | kfree(cliprects); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1037 | return ret; |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * Legacy execbuffer just creates an exec2 list from the original exec object |
| 1042 | * list array and passes it to the real function. |
| 1043 | */ |
| 1044 | int |
| 1045 | i915_gem_execbuffer(struct drm_device *dev, void *data, |
| 1046 | struct drm_file *file) |
| 1047 | { |
| 1048 | struct drm_i915_gem_execbuffer *args = data; |
| 1049 | struct drm_i915_gem_execbuffer2 exec2; |
| 1050 | struct drm_i915_gem_exec_object *exec_list = NULL; |
| 1051 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 1052 | int ret, i; |
| 1053 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1054 | if (args->buffer_count < 1) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1055 | DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1056 | return -EINVAL; |
| 1057 | } |
| 1058 | |
| 1059 | /* Copy in the exec list from userland */ |
| 1060 | exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count); |
| 1061 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 1062 | if (exec_list == NULL || exec2_list == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1063 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1064 | args->buffer_count); |
| 1065 | drm_free_large(exec_list); |
| 1066 | drm_free_large(exec2_list); |
| 1067 | return -ENOMEM; |
| 1068 | } |
| 1069 | ret = copy_from_user(exec_list, |
Chris Wilson | ba7a645 | 2012-09-14 11:46:00 +0100 | [diff] [blame] | 1070 | (void __user *)(uintptr_t)args->buffers_ptr, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1071 | sizeof(*exec_list) * args->buffer_count); |
| 1072 | if (ret != 0) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1073 | DRM_DEBUG("copy %d exec entries failed %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1074 | args->buffer_count, ret); |
| 1075 | drm_free_large(exec_list); |
| 1076 | drm_free_large(exec2_list); |
| 1077 | return -EFAULT; |
| 1078 | } |
| 1079 | |
| 1080 | for (i = 0; i < args->buffer_count; i++) { |
| 1081 | exec2_list[i].handle = exec_list[i].handle; |
| 1082 | exec2_list[i].relocation_count = exec_list[i].relocation_count; |
| 1083 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; |
| 1084 | exec2_list[i].alignment = exec_list[i].alignment; |
| 1085 | exec2_list[i].offset = exec_list[i].offset; |
| 1086 | if (INTEL_INFO(dev)->gen < 4) |
| 1087 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; |
| 1088 | else |
| 1089 | exec2_list[i].flags = 0; |
| 1090 | } |
| 1091 | |
| 1092 | exec2.buffers_ptr = args->buffers_ptr; |
| 1093 | exec2.buffer_count = args->buffer_count; |
| 1094 | exec2.batch_start_offset = args->batch_start_offset; |
| 1095 | exec2.batch_len = args->batch_len; |
| 1096 | exec2.DR1 = args->DR1; |
| 1097 | exec2.DR4 = args->DR4; |
| 1098 | exec2.num_cliprects = args->num_cliprects; |
| 1099 | exec2.cliprects_ptr = args->cliprects_ptr; |
| 1100 | exec2.flags = I915_EXEC_RENDER; |
Ben Widawsky | 6e0a69d | 2012-06-04 14:42:55 -0700 | [diff] [blame] | 1101 | i915_execbuffer2_set_context_id(exec2, 0); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1102 | |
| 1103 | ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list); |
| 1104 | if (!ret) { |
| 1105 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 1106 | for (i = 0; i < args->buffer_count; i++) |
| 1107 | exec_list[i].offset = exec2_list[i].offset; |
| 1108 | /* ... and back out to userspace */ |
Chris Wilson | ba7a645 | 2012-09-14 11:46:00 +0100 | [diff] [blame] | 1109 | ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1110 | exec_list, |
| 1111 | sizeof(*exec_list) * args->buffer_count); |
| 1112 | if (ret) { |
| 1113 | ret = -EFAULT; |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1114 | DRM_DEBUG("failed to copy %d exec entries " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1115 | "back to user (%d)\n", |
| 1116 | args->buffer_count, ret); |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | drm_free_large(exec_list); |
| 1121 | drm_free_large(exec2_list); |
| 1122 | return ret; |
| 1123 | } |
| 1124 | |
| 1125 | int |
| 1126 | i915_gem_execbuffer2(struct drm_device *dev, void *data, |
| 1127 | struct drm_file *file) |
| 1128 | { |
| 1129 | struct drm_i915_gem_execbuffer2 *args = data; |
| 1130 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 1131 | int ret; |
| 1132 | |
Xi Wang | ed8cd3b | 2012-04-23 04:06:41 -0400 | [diff] [blame] | 1133 | if (args->buffer_count < 1 || |
| 1134 | args->buffer_count > UINT_MAX / sizeof(*exec2_list)) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1135 | DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1136 | return -EINVAL; |
| 1137 | } |
| 1138 | |
Chris Wilson | 8408c28 | 2011-02-21 12:54:48 +0000 | [diff] [blame] | 1139 | exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count, |
Chris Wilson | 419fa72 | 2013-01-08 10:53:13 +0000 | [diff] [blame] | 1140 | GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY); |
Chris Wilson | 8408c28 | 2011-02-21 12:54:48 +0000 | [diff] [blame] | 1141 | if (exec2_list == NULL) |
| 1142 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), |
| 1143 | args->buffer_count); |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1144 | if (exec2_list == NULL) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1145 | DRM_DEBUG("Failed to allocate exec list for %d buffers\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1146 | args->buffer_count); |
| 1147 | return -ENOMEM; |
| 1148 | } |
| 1149 | ret = copy_from_user(exec2_list, |
| 1150 | (struct drm_i915_relocation_entry __user *) |
| 1151 | (uintptr_t) args->buffers_ptr, |
| 1152 | sizeof(*exec2_list) * args->buffer_count); |
| 1153 | if (ret != 0) { |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1154 | DRM_DEBUG("copy %d exec entries failed %d\n", |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1155 | args->buffer_count, ret); |
| 1156 | drm_free_large(exec2_list); |
| 1157 | return -EFAULT; |
| 1158 | } |
| 1159 | |
| 1160 | ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list); |
| 1161 | if (!ret) { |
| 1162 | /* Copy the new buffer offsets back to the user's exec list. */ |
Chris Wilson | ba7a645 | 2012-09-14 11:46:00 +0100 | [diff] [blame] | 1163 | ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr, |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1164 | exec2_list, |
| 1165 | sizeof(*exec2_list) * args->buffer_count); |
| 1166 | if (ret) { |
| 1167 | ret = -EFAULT; |
Daniel Vetter | ff24019 | 2012-01-31 21:08:14 +0100 | [diff] [blame] | 1168 | DRM_DEBUG("failed to copy %d exec entries " |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1169 | "back to user (%d)\n", |
| 1170 | args->buffer_count, ret); |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | drm_free_large(exec2_list); |
| 1175 | return ret; |
| 1176 | } |