Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 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 | * |
| 26 | */ |
| 27 | |
| 28 | #include "drmP.h" |
| 29 | #include "drm.h" |
| 30 | #include "i915_drm.h" |
| 31 | #include "i915_drv.h" |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 32 | #include "i915_trace.h" |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 33 | #include "intel_drv.h" |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 35 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 36 | #include <linux/pci.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 37 | |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 38 | struct change_domains { |
| 39 | uint32_t invalidate_domains; |
| 40 | uint32_t flush_domains; |
| 41 | uint32_t flush_rings; |
| 42 | }; |
| 43 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 44 | static uint32_t i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv); |
| 45 | static uint32_t i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv); |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 46 | |
| 47 | static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj, |
| 48 | bool pipelined); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 49 | static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj); |
| 50 | static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 51 | static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, |
| 52 | int write); |
| 53 | static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj, |
| 54 | uint64_t offset, |
| 55 | uint64_t size); |
| 56 | static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj); |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 57 | static int i915_gem_object_wait_rendering(struct drm_gem_object *obj, |
| 58 | bool interruptible); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 59 | static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 60 | unsigned alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 61 | bool map_and_fenceable); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 62 | static void i915_gem_clear_fence_reg(struct drm_gem_object *obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 63 | static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj, |
| 64 | struct drm_i915_gem_pwrite *args, |
| 65 | struct drm_file *file_priv); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 66 | static void i915_gem_free_object_tail(struct drm_gem_object *obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 67 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 68 | static int i915_gem_inactive_shrink(struct shrinker *shrinker, |
| 69 | int nr_to_scan, |
| 70 | gfp_t gfp_mask); |
| 71 | |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 72 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 73 | /* some bookkeeping */ |
| 74 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, |
| 75 | size_t size) |
| 76 | { |
| 77 | dev_priv->mm.object_count++; |
| 78 | dev_priv->mm.object_memory += size; |
| 79 | } |
| 80 | |
| 81 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, |
| 82 | size_t size) |
| 83 | { |
| 84 | dev_priv->mm.object_count--; |
| 85 | dev_priv->mm.object_memory -= size; |
| 86 | } |
| 87 | |
| 88 | static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 89 | struct drm_i915_gem_object *obj) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 90 | { |
| 91 | dev_priv->mm.gtt_count++; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 92 | dev_priv->mm.gtt_memory += obj->gtt_space->size; |
| 93 | if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 94 | dev_priv->mm.mappable_gtt_used += |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 95 | min_t(size_t, obj->gtt_space->size, |
| 96 | dev_priv->mm.gtt_mappable_end - obj->gtt_offset); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 97 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 101 | struct drm_i915_gem_object *obj) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 102 | { |
| 103 | dev_priv->mm.gtt_count--; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 104 | dev_priv->mm.gtt_memory -= obj->gtt_space->size; |
| 105 | if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 106 | dev_priv->mm.mappable_gtt_used -= |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 107 | min_t(size_t, obj->gtt_space->size, |
| 108 | dev_priv->mm.gtt_mappable_end - obj->gtt_offset); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Update the mappable working set counters. Call _only_ when there is a change |
| 114 | * in one of (pin|fault)_mappable and update *_mappable _before_ calling. |
| 115 | * @mappable: new state the changed mappable flag (either pin_ or fault_). |
| 116 | */ |
| 117 | static void |
| 118 | i915_gem_info_update_mappable(struct drm_i915_private *dev_priv, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 119 | struct drm_i915_gem_object *obj, |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 120 | bool mappable) |
| 121 | { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 122 | if (mappable) { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 123 | if (obj->pin_mappable && obj->fault_mappable) |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 124 | /* Combined state was already mappable. */ |
| 125 | return; |
| 126 | dev_priv->mm.gtt_mappable_count++; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 127 | dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 128 | } else { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 129 | if (obj->pin_mappable || obj->fault_mappable) |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 130 | /* Combined state still mappable. */ |
| 131 | return; |
| 132 | dev_priv->mm.gtt_mappable_count--; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 133 | dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 134 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 138 | struct drm_i915_gem_object *obj, |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 139 | bool mappable) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 140 | { |
| 141 | dev_priv->mm.pin_count++; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 142 | dev_priv->mm.pin_memory += obj->gtt_space->size; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 143 | if (mappable) { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 144 | obj->pin_mappable = true; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 145 | i915_gem_info_update_mappable(dev_priv, obj, true); |
| 146 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 150 | struct drm_i915_gem_object *obj) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 151 | { |
| 152 | dev_priv->mm.pin_count--; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 153 | dev_priv->mm.pin_memory -= obj->gtt_space->size; |
| 154 | if (obj->pin_mappable) { |
| 155 | obj->pin_mappable = false; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 156 | i915_gem_info_update_mappable(dev_priv, obj, false); |
| 157 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 160 | int |
| 161 | i915_gem_check_is_wedged(struct drm_device *dev) |
| 162 | { |
| 163 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 164 | struct completion *x = &dev_priv->error_completion; |
| 165 | unsigned long flags; |
| 166 | int ret; |
| 167 | |
| 168 | if (!atomic_read(&dev_priv->mm.wedged)) |
| 169 | return 0; |
| 170 | |
| 171 | ret = wait_for_completion_interruptible(x); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
| 175 | /* Success, we reset the GPU! */ |
| 176 | if (!atomic_read(&dev_priv->mm.wedged)) |
| 177 | return 0; |
| 178 | |
| 179 | /* GPU is hung, bump the completion count to account for |
| 180 | * the token we just consumed so that we never hit zero and |
| 181 | * end up waiting upon a subsequent completion event that |
| 182 | * will never happen. |
| 183 | */ |
| 184 | spin_lock_irqsave(&x->wait.lock, flags); |
| 185 | x->done++; |
| 186 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 187 | return -EIO; |
| 188 | } |
| 189 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 190 | static int i915_mutex_lock_interruptible(struct drm_device *dev) |
| 191 | { |
| 192 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 193 | int ret; |
| 194 | |
| 195 | ret = i915_gem_check_is_wedged(dev); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | |
| 199 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 200 | if (ret) |
| 201 | return ret; |
| 202 | |
| 203 | if (atomic_read(&dev_priv->mm.wedged)) { |
| 204 | mutex_unlock(&dev->struct_mutex); |
| 205 | return -EAGAIN; |
| 206 | } |
| 207 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 208 | WARN_ON(i915_verify_lists(dev)); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 209 | return 0; |
| 210 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 211 | |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 212 | static inline bool |
| 213 | i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv) |
| 214 | { |
| 215 | return obj_priv->gtt_space && |
| 216 | !obj_priv->active && |
| 217 | obj_priv->pin_count == 0; |
| 218 | } |
| 219 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 220 | int i915_gem_do_init(struct drm_device *dev, |
| 221 | unsigned long start, |
Daniel Vetter | 5398463 | 2010-09-22 23:44:24 +0200 | [diff] [blame] | 222 | unsigned long mappable_end, |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 223 | unsigned long end) |
| 224 | { |
| 225 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 226 | |
| 227 | if (start >= end || |
| 228 | (start & (PAGE_SIZE - 1)) != 0 || |
| 229 | (end & (PAGE_SIZE - 1)) != 0) { |
| 230 | return -EINVAL; |
| 231 | } |
| 232 | |
| 233 | drm_mm_init(&dev_priv->mm.gtt_space, start, |
| 234 | end - start); |
| 235 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 236 | dev_priv->mm.gtt_total = end - start; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 237 | dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start; |
Daniel Vetter | 5398463 | 2010-09-22 23:44:24 +0200 | [diff] [blame] | 238 | dev_priv->mm.gtt_mappable_end = mappable_end; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 239 | |
| 240 | return 0; |
| 241 | } |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 242 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 243 | int |
| 244 | i915_gem_init_ioctl(struct drm_device *dev, void *data, |
| 245 | struct drm_file *file_priv) |
| 246 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 247 | struct drm_i915_gem_init *args = data; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 248 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 249 | |
| 250 | mutex_lock(&dev->struct_mutex); |
Daniel Vetter | 5398463 | 2010-09-22 23:44:24 +0200 | [diff] [blame] | 251 | ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 252 | mutex_unlock(&dev->struct_mutex); |
| 253 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 254 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 257 | int |
| 258 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
| 259 | struct drm_file *file_priv) |
| 260 | { |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 261 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 262 | struct drm_i915_gem_get_aperture *args = data; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 263 | |
| 264 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 265 | return -ENODEV; |
| 266 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 267 | mutex_lock(&dev->struct_mutex); |
| 268 | args->aper_size = dev_priv->mm.gtt_total; |
| 269 | args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory; |
| 270 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * Creates a new mm object and returns a handle to it. |
| 278 | */ |
| 279 | int |
| 280 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 281 | struct drm_file *file_priv) |
| 282 | { |
| 283 | struct drm_i915_gem_create *args = data; |
| 284 | struct drm_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 285 | int ret; |
| 286 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 287 | |
| 288 | args->size = roundup(args->size, PAGE_SIZE); |
| 289 | |
| 290 | /* Allocate the new object */ |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 291 | obj = i915_gem_alloc_object(dev, args->size); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 292 | if (obj == NULL) |
| 293 | return -ENOMEM; |
| 294 | |
| 295 | ret = drm_gem_handle_create(file_priv, obj, &handle); |
Chris Wilson | 1dfd975 | 2010-09-06 14:44:14 +0100 | [diff] [blame] | 296 | if (ret) { |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 297 | drm_gem_object_release(obj); |
| 298 | i915_gem_info_remove_obj(dev->dev_private, obj->size); |
| 299 | kfree(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 300 | return ret; |
Chris Wilson | 1dfd975 | 2010-09-06 14:44:14 +0100 | [diff] [blame] | 301 | } |
| 302 | |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 303 | /* drop reference from allocate - handle holds it now */ |
| 304 | drm_gem_object_unreference(obj); |
| 305 | trace_i915_gem_object_create(obj); |
| 306 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 307 | args->handle = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 311 | static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj) |
| 312 | { |
| 313 | drm_i915_private_t *dev_priv = obj->dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 314 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 315 | |
| 316 | return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 && |
| 317 | obj_priv->tiling_mode != I915_TILING_NONE; |
| 318 | } |
| 319 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 320 | static inline void |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 321 | slow_shmem_copy(struct page *dst_page, |
| 322 | int dst_offset, |
| 323 | struct page *src_page, |
| 324 | int src_offset, |
| 325 | int length) |
| 326 | { |
| 327 | char *dst_vaddr, *src_vaddr; |
| 328 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 329 | dst_vaddr = kmap(dst_page); |
| 330 | src_vaddr = kmap(src_page); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 331 | |
| 332 | memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length); |
| 333 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 334 | kunmap(src_page); |
| 335 | kunmap(dst_page); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 336 | } |
| 337 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 338 | static inline void |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 339 | slow_shmem_bit17_copy(struct page *gpu_page, |
| 340 | int gpu_offset, |
| 341 | struct page *cpu_page, |
| 342 | int cpu_offset, |
| 343 | int length, |
| 344 | int is_read) |
| 345 | { |
| 346 | char *gpu_vaddr, *cpu_vaddr; |
| 347 | |
| 348 | /* Use the unswizzled path if this page isn't affected. */ |
| 349 | if ((page_to_phys(gpu_page) & (1 << 17)) == 0) { |
| 350 | if (is_read) |
| 351 | return slow_shmem_copy(cpu_page, cpu_offset, |
| 352 | gpu_page, gpu_offset, length); |
| 353 | else |
| 354 | return slow_shmem_copy(gpu_page, gpu_offset, |
| 355 | cpu_page, cpu_offset, length); |
| 356 | } |
| 357 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 358 | gpu_vaddr = kmap(gpu_page); |
| 359 | cpu_vaddr = kmap(cpu_page); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 360 | |
| 361 | /* Copy the data, XORing A6 with A17 (1). The user already knows he's |
| 362 | * XORing with the other bits (A9 for Y, A9 and A10 for X) |
| 363 | */ |
| 364 | while (length > 0) { |
| 365 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 366 | int this_length = min(cacheline_end - gpu_offset, length); |
| 367 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 368 | |
| 369 | if (is_read) { |
| 370 | memcpy(cpu_vaddr + cpu_offset, |
| 371 | gpu_vaddr + swizzled_gpu_offset, |
| 372 | this_length); |
| 373 | } else { |
| 374 | memcpy(gpu_vaddr + swizzled_gpu_offset, |
| 375 | cpu_vaddr + cpu_offset, |
| 376 | this_length); |
| 377 | } |
| 378 | cpu_offset += this_length; |
| 379 | gpu_offset += this_length; |
| 380 | length -= this_length; |
| 381 | } |
| 382 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 383 | kunmap(cpu_page); |
| 384 | kunmap(gpu_page); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 385 | } |
| 386 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 387 | /** |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 388 | * This is the fast shmem pread path, which attempts to copy_from_user directly |
| 389 | * from the backing pages of the object to the user's address space. On a |
| 390 | * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow(). |
| 391 | */ |
| 392 | static int |
| 393 | i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj, |
| 394 | struct drm_i915_gem_pread *args, |
| 395 | struct drm_file *file_priv) |
| 396 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 397 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 398 | struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 399 | ssize_t remain; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 400 | loff_t offset; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 401 | char __user *user_data; |
| 402 | int page_offset, page_length; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 403 | |
| 404 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 405 | remain = args->size; |
| 406 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 407 | obj_priv = to_intel_bo(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 408 | offset = args->offset; |
| 409 | |
| 410 | while (remain > 0) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 411 | struct page *page; |
| 412 | char *vaddr; |
| 413 | int ret; |
| 414 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 415 | /* Operation in this page |
| 416 | * |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 417 | * page_offset = offset within page |
| 418 | * page_length = bytes to copy for this page |
| 419 | */ |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 420 | page_offset = offset & (PAGE_SIZE-1); |
| 421 | page_length = remain; |
| 422 | if ((page_offset + remain) > PAGE_SIZE) |
| 423 | page_length = PAGE_SIZE - page_offset; |
| 424 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 425 | page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT, |
| 426 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 427 | if (IS_ERR(page)) |
| 428 | return PTR_ERR(page); |
| 429 | |
| 430 | vaddr = kmap_atomic(page); |
| 431 | ret = __copy_to_user_inatomic(user_data, |
| 432 | vaddr + page_offset, |
| 433 | page_length); |
| 434 | kunmap_atomic(vaddr); |
| 435 | |
| 436 | mark_page_accessed(page); |
| 437 | page_cache_release(page); |
| 438 | if (ret) |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 439 | return -EFAULT; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 440 | |
| 441 | remain -= page_length; |
| 442 | user_data += page_length; |
| 443 | offset += page_length; |
| 444 | } |
| 445 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 446 | return 0; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /** |
| 450 | * This is the fallback shmem pread path, which allocates temporary storage |
| 451 | * in kernel space to copy_to_user into outside of the struct_mutex, so we |
| 452 | * can copy out of the object's backing pages while holding the struct mutex |
| 453 | * and not take page faults. |
| 454 | */ |
| 455 | static int |
| 456 | i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj, |
| 457 | struct drm_i915_gem_pread *args, |
| 458 | struct drm_file *file_priv) |
| 459 | { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 460 | struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 461 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 462 | struct mm_struct *mm = current->mm; |
| 463 | struct page **user_pages; |
| 464 | ssize_t remain; |
| 465 | loff_t offset, pinned_pages, i; |
| 466 | loff_t first_data_page, last_data_page, num_pages; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 467 | int shmem_page_offset; |
| 468 | int data_page_index, data_page_offset; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 469 | int page_length; |
| 470 | int ret; |
| 471 | uint64_t data_ptr = args->data_ptr; |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 472 | int do_bit17_swizzling; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 473 | |
| 474 | remain = args->size; |
| 475 | |
| 476 | /* Pin the user pages containing the data. We can't fault while |
| 477 | * holding the struct mutex, yet we want to hold it while |
| 478 | * dereferencing the user data. |
| 479 | */ |
| 480 | first_data_page = data_ptr / PAGE_SIZE; |
| 481 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 482 | num_pages = last_data_page - first_data_page + 1; |
| 483 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 484 | user_pages = drm_malloc_ab(num_pages, sizeof(struct page *)); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 485 | if (user_pages == NULL) |
| 486 | return -ENOMEM; |
| 487 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 488 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 489 | down_read(&mm->mmap_sem); |
| 490 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
Eric Anholt | e5e9ecd | 2009-04-07 16:01:22 -0700 | [diff] [blame] | 491 | num_pages, 1, 0, user_pages, NULL); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 492 | up_read(&mm->mmap_sem); |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 493 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 494 | if (pinned_pages < num_pages) { |
| 495 | ret = -EFAULT; |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 496 | goto out; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 497 | } |
| 498 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 499 | ret = i915_gem_object_set_cpu_read_domain_range(obj, |
| 500 | args->offset, |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 501 | args->size); |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 502 | if (ret) |
| 503 | goto out; |
| 504 | |
| 505 | do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 506 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 507 | obj_priv = to_intel_bo(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 508 | offset = args->offset; |
| 509 | |
| 510 | while (remain > 0) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 511 | struct page *page; |
| 512 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 513 | /* Operation in this page |
| 514 | * |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 515 | * shmem_page_offset = offset within page in shmem file |
| 516 | * data_page_index = page number in get_user_pages return |
| 517 | * data_page_offset = offset with data_page_index page. |
| 518 | * page_length = bytes to copy for this page |
| 519 | */ |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 520 | shmem_page_offset = offset & ~PAGE_MASK; |
| 521 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 522 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 523 | |
| 524 | page_length = remain; |
| 525 | if ((shmem_page_offset + page_length) > PAGE_SIZE) |
| 526 | page_length = PAGE_SIZE - shmem_page_offset; |
| 527 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 528 | page_length = PAGE_SIZE - data_page_offset; |
| 529 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 530 | page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT, |
| 531 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 532 | if (IS_ERR(page)) |
| 533 | return PTR_ERR(page); |
| 534 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 535 | if (do_bit17_swizzling) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 536 | slow_shmem_bit17_copy(page, |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 537 | shmem_page_offset, |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 538 | user_pages[data_page_index], |
| 539 | data_page_offset, |
| 540 | page_length, |
| 541 | 1); |
| 542 | } else { |
| 543 | slow_shmem_copy(user_pages[data_page_index], |
| 544 | data_page_offset, |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 545 | page, |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 546 | shmem_page_offset, |
| 547 | page_length); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 548 | } |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 549 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 550 | mark_page_accessed(page); |
| 551 | page_cache_release(page); |
| 552 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 553 | remain -= page_length; |
| 554 | data_ptr += page_length; |
| 555 | offset += page_length; |
| 556 | } |
| 557 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 558 | out: |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 559 | for (i = 0; i < pinned_pages; i++) { |
| 560 | SetPageDirty(user_pages[i]); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 561 | mark_page_accessed(user_pages[i]); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 562 | page_cache_release(user_pages[i]); |
| 563 | } |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 564 | drm_free_large(user_pages); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 565 | |
| 566 | return ret; |
| 567 | } |
| 568 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 569 | /** |
| 570 | * Reads data from the object referenced by handle. |
| 571 | * |
| 572 | * On error, the contents of *data are undefined. |
| 573 | */ |
| 574 | int |
| 575 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
| 576 | struct drm_file *file_priv) |
| 577 | { |
| 578 | struct drm_i915_gem_pread *args = data; |
| 579 | struct drm_gem_object *obj; |
| 580 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 581 | int ret = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 582 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 583 | if (args->size == 0) |
| 584 | return 0; |
| 585 | |
| 586 | if (!access_ok(VERIFY_WRITE, |
| 587 | (char __user *)(uintptr_t)args->data_ptr, |
| 588 | args->size)) |
| 589 | return -EFAULT; |
| 590 | |
| 591 | ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr, |
| 592 | args->size); |
| 593 | if (ret) |
| 594 | return -EFAULT; |
| 595 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 596 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 597 | if (ret) |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 598 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 599 | |
| 600 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 601 | if (obj == NULL) { |
| 602 | ret = -ENOENT; |
| 603 | goto unlock; |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 604 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 605 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 606 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 607 | /* Bounds check source. */ |
| 608 | if (args->offset > obj->size || args->size > obj->size - args->offset) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 609 | ret = -EINVAL; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 610 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 611 | } |
| 612 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 613 | ret = i915_gem_object_set_cpu_read_domain_range(obj, |
| 614 | args->offset, |
| 615 | args->size); |
| 616 | if (ret) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 617 | goto out; |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 618 | |
| 619 | ret = -EFAULT; |
| 620 | if (!i915_gem_object_needs_bit17_swizzle(obj)) |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 621 | ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv); |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 622 | if (ret == -EFAULT) |
| 623 | ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 624 | |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 625 | out: |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 626 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 627 | unlock: |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 628 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 629 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 630 | } |
| 631 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 632 | /* This is the fast write path which cannot handle |
| 633 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 634 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 635 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 636 | static inline int |
| 637 | fast_user_write(struct io_mapping *mapping, |
| 638 | loff_t page_base, int page_offset, |
| 639 | char __user *user_data, |
| 640 | int length) |
| 641 | { |
| 642 | char *vaddr_atomic; |
| 643 | unsigned long unwritten; |
| 644 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 645 | vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base); |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 646 | unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset, |
| 647 | user_data, length); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 648 | io_mapping_unmap_atomic(vaddr_atomic); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 649 | return unwritten; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | /* Here's the write path which can sleep for |
| 653 | * page faults |
| 654 | */ |
| 655 | |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 656 | static inline void |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 657 | slow_kernel_write(struct io_mapping *mapping, |
| 658 | loff_t gtt_base, int gtt_offset, |
| 659 | struct page *user_page, int user_offset, |
| 660 | int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 661 | { |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 662 | char __iomem *dst_vaddr; |
| 663 | char *src_vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 664 | |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 665 | dst_vaddr = io_mapping_map_wc(mapping, gtt_base); |
| 666 | src_vaddr = kmap(user_page); |
| 667 | |
| 668 | memcpy_toio(dst_vaddr + gtt_offset, |
| 669 | src_vaddr + user_offset, |
| 670 | length); |
| 671 | |
| 672 | kunmap(user_page); |
| 673 | io_mapping_unmap(dst_vaddr); |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 676 | /** |
| 677 | * This is the fast pwrite path, where we copy the data directly from the |
| 678 | * user into the GTT, uncached. |
| 679 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 680 | static int |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 681 | i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj, |
| 682 | struct drm_i915_gem_pwrite *args, |
| 683 | struct drm_file *file_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 684 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 685 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 686 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 687 | ssize_t remain; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 688 | loff_t offset, page_base; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 689 | char __user *user_data; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 690 | int page_offset, page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 691 | |
| 692 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 693 | remain = args->size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 694 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 695 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 696 | offset = obj_priv->gtt_offset + args->offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 697 | |
| 698 | while (remain > 0) { |
| 699 | /* Operation in this page |
| 700 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 701 | * page_base = page offset within aperture |
| 702 | * page_offset = offset within page |
| 703 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 704 | */ |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 705 | page_base = (offset & ~(PAGE_SIZE-1)); |
| 706 | page_offset = offset & (PAGE_SIZE-1); |
| 707 | page_length = remain; |
| 708 | if ((page_offset + remain) > PAGE_SIZE) |
| 709 | page_length = PAGE_SIZE - page_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 710 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 711 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 712 | * source page isn't available. Return the error and we'll |
| 713 | * retry in the slow path. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 714 | */ |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 715 | if (fast_user_write(dev_priv->mm.gtt_mapping, page_base, |
| 716 | page_offset, user_data, page_length)) |
| 717 | |
| 718 | return -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 719 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 720 | remain -= page_length; |
| 721 | user_data += page_length; |
| 722 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 723 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 724 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 725 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 726 | } |
| 727 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 728 | /** |
| 729 | * This is the fallback GTT pwrite path, which uses get_user_pages to pin |
| 730 | * the memory and maps it using kmap_atomic for copying. |
| 731 | * |
| 732 | * This code resulted in x11perf -rgb10text consuming about 10% more CPU |
| 733 | * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit). |
| 734 | */ |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 735 | static int |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 736 | i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj, |
| 737 | struct drm_i915_gem_pwrite *args, |
| 738 | struct drm_file *file_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 739 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 740 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 741 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 742 | ssize_t remain; |
| 743 | loff_t gtt_page_base, offset; |
| 744 | loff_t first_data_page, last_data_page, num_pages; |
| 745 | loff_t pinned_pages, i; |
| 746 | struct page **user_pages; |
| 747 | struct mm_struct *mm = current->mm; |
| 748 | int gtt_page_offset, data_page_offset, data_page_index, page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 749 | int ret; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 750 | uint64_t data_ptr = args->data_ptr; |
| 751 | |
| 752 | remain = args->size; |
| 753 | |
| 754 | /* Pin the user pages containing the data. We can't fault while |
| 755 | * holding the struct mutex, and all of the pwrite implementations |
| 756 | * want to hold it while dereferencing the user data. |
| 757 | */ |
| 758 | first_data_page = data_ptr / PAGE_SIZE; |
| 759 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 760 | num_pages = last_data_page - first_data_page + 1; |
| 761 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 762 | user_pages = drm_malloc_ab(num_pages, sizeof(struct page *)); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 763 | if (user_pages == NULL) |
| 764 | return -ENOMEM; |
| 765 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 766 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 767 | down_read(&mm->mmap_sem); |
| 768 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
| 769 | num_pages, 0, 0, user_pages, NULL); |
| 770 | up_read(&mm->mmap_sem); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 771 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 772 | if (pinned_pages < num_pages) { |
| 773 | ret = -EFAULT; |
| 774 | goto out_unpin_pages; |
| 775 | } |
| 776 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 777 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
| 778 | if (ret) |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 779 | goto out_unpin_pages; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 780 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 781 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 782 | offset = obj_priv->gtt_offset + args->offset; |
| 783 | |
| 784 | while (remain > 0) { |
| 785 | /* Operation in this page |
| 786 | * |
| 787 | * gtt_page_base = page offset within aperture |
| 788 | * gtt_page_offset = offset within page in aperture |
| 789 | * data_page_index = page number in get_user_pages return |
| 790 | * data_page_offset = offset with data_page_index page. |
| 791 | * page_length = bytes to copy for this page |
| 792 | */ |
| 793 | gtt_page_base = offset & PAGE_MASK; |
| 794 | gtt_page_offset = offset & ~PAGE_MASK; |
| 795 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 796 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 797 | |
| 798 | page_length = remain; |
| 799 | if ((gtt_page_offset + page_length) > PAGE_SIZE) |
| 800 | page_length = PAGE_SIZE - gtt_page_offset; |
| 801 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 802 | page_length = PAGE_SIZE - data_page_offset; |
| 803 | |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 804 | slow_kernel_write(dev_priv->mm.gtt_mapping, |
| 805 | gtt_page_base, gtt_page_offset, |
| 806 | user_pages[data_page_index], |
| 807 | data_page_offset, |
| 808 | page_length); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 809 | |
| 810 | remain -= page_length; |
| 811 | offset += page_length; |
| 812 | data_ptr += page_length; |
| 813 | } |
| 814 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 815 | out_unpin_pages: |
| 816 | for (i = 0; i < pinned_pages; i++) |
| 817 | page_cache_release(user_pages[i]); |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 818 | drm_free_large(user_pages); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 819 | |
| 820 | return ret; |
| 821 | } |
| 822 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 823 | /** |
| 824 | * This is the fast shmem pwrite path, which attempts to directly |
| 825 | * copy_from_user into the kmapped pages backing the object. |
| 826 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 827 | static int |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 828 | i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj, |
| 829 | struct drm_i915_gem_pwrite *args, |
| 830 | struct drm_file *file_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 831 | { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 832 | struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 833 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 834 | ssize_t remain; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 835 | loff_t offset; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 836 | char __user *user_data; |
| 837 | int page_offset, page_length; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 838 | |
| 839 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 840 | remain = args->size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 841 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 842 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 843 | offset = args->offset; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 844 | obj_priv->dirty = 1; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 845 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 846 | while (remain > 0) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 847 | struct page *page; |
| 848 | char *vaddr; |
| 849 | int ret; |
| 850 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 851 | /* Operation in this page |
| 852 | * |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 853 | * page_offset = offset within page |
| 854 | * page_length = bytes to copy for this page |
| 855 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 856 | page_offset = offset & (PAGE_SIZE-1); |
| 857 | page_length = remain; |
| 858 | if ((page_offset + remain) > PAGE_SIZE) |
| 859 | page_length = PAGE_SIZE - page_offset; |
| 860 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 861 | page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT, |
| 862 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 863 | if (IS_ERR(page)) |
| 864 | return PTR_ERR(page); |
| 865 | |
| 866 | vaddr = kmap_atomic(page, KM_USER0); |
| 867 | ret = __copy_from_user_inatomic(vaddr + page_offset, |
| 868 | user_data, |
| 869 | page_length); |
| 870 | kunmap_atomic(vaddr, KM_USER0); |
| 871 | |
| 872 | set_page_dirty(page); |
| 873 | mark_page_accessed(page); |
| 874 | page_cache_release(page); |
| 875 | |
| 876 | /* If we get a fault while copying data, then (presumably) our |
| 877 | * source page isn't available. Return the error and we'll |
| 878 | * retry in the slow path. |
| 879 | */ |
| 880 | if (ret) |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 881 | return -EFAULT; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 882 | |
| 883 | remain -= page_length; |
| 884 | user_data += page_length; |
| 885 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 886 | } |
| 887 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 888 | return 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | /** |
| 892 | * This is the fallback shmem pwrite path, which uses get_user_pages to pin |
| 893 | * the memory and maps it using kmap_atomic for copying. |
| 894 | * |
| 895 | * This avoids taking mmap_sem for faulting on the user's address while the |
| 896 | * struct_mutex is held. |
| 897 | */ |
| 898 | static int |
| 899 | i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj, |
| 900 | struct drm_i915_gem_pwrite *args, |
| 901 | struct drm_file *file_priv) |
| 902 | { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 903 | struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 904 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 905 | struct mm_struct *mm = current->mm; |
| 906 | struct page **user_pages; |
| 907 | ssize_t remain; |
| 908 | loff_t offset, pinned_pages, i; |
| 909 | loff_t first_data_page, last_data_page, num_pages; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 910 | int shmem_page_offset; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 911 | int data_page_index, data_page_offset; |
| 912 | int page_length; |
| 913 | int ret; |
| 914 | uint64_t data_ptr = args->data_ptr; |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 915 | int do_bit17_swizzling; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 916 | |
| 917 | remain = args->size; |
| 918 | |
| 919 | /* Pin the user pages containing the data. We can't fault while |
| 920 | * holding the struct mutex, and all of the pwrite implementations |
| 921 | * want to hold it while dereferencing the user data. |
| 922 | */ |
| 923 | first_data_page = data_ptr / PAGE_SIZE; |
| 924 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 925 | num_pages = last_data_page - first_data_page + 1; |
| 926 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 927 | user_pages = drm_malloc_ab(num_pages, sizeof(struct page *)); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 928 | if (user_pages == NULL) |
| 929 | return -ENOMEM; |
| 930 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 931 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 932 | down_read(&mm->mmap_sem); |
| 933 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
| 934 | num_pages, 0, 0, user_pages, NULL); |
| 935 | up_read(&mm->mmap_sem); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 936 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 937 | if (pinned_pages < num_pages) { |
| 938 | ret = -EFAULT; |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 939 | goto out; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 940 | } |
| 941 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 942 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 943 | if (ret) |
| 944 | goto out; |
| 945 | |
| 946 | do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 947 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 948 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 949 | offset = args->offset; |
| 950 | obj_priv->dirty = 1; |
| 951 | |
| 952 | while (remain > 0) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 953 | struct page *page; |
| 954 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 955 | /* Operation in this page |
| 956 | * |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 957 | * shmem_page_offset = offset within page in shmem file |
| 958 | * data_page_index = page number in get_user_pages return |
| 959 | * data_page_offset = offset with data_page_index page. |
| 960 | * page_length = bytes to copy for this page |
| 961 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 962 | shmem_page_offset = offset & ~PAGE_MASK; |
| 963 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 964 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 965 | |
| 966 | page_length = remain; |
| 967 | if ((shmem_page_offset + page_length) > PAGE_SIZE) |
| 968 | page_length = PAGE_SIZE - shmem_page_offset; |
| 969 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 970 | page_length = PAGE_SIZE - data_page_offset; |
| 971 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 972 | page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT, |
| 973 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 974 | if (IS_ERR(page)) { |
| 975 | ret = PTR_ERR(page); |
| 976 | goto out; |
| 977 | } |
| 978 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 979 | if (do_bit17_swizzling) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 980 | slow_shmem_bit17_copy(page, |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 981 | shmem_page_offset, |
| 982 | user_pages[data_page_index], |
| 983 | data_page_offset, |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 984 | page_length, |
| 985 | 0); |
| 986 | } else { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 987 | slow_shmem_copy(page, |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 988 | shmem_page_offset, |
| 989 | user_pages[data_page_index], |
| 990 | data_page_offset, |
| 991 | page_length); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 992 | } |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 993 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 994 | set_page_dirty(page); |
| 995 | mark_page_accessed(page); |
| 996 | page_cache_release(page); |
| 997 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 998 | remain -= page_length; |
| 999 | data_ptr += page_length; |
| 1000 | offset += page_length; |
| 1001 | } |
| 1002 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1003 | out: |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1004 | for (i = 0; i < pinned_pages; i++) |
| 1005 | page_cache_release(user_pages[i]); |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 1006 | drm_free_large(user_pages); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1007 | |
| 1008 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | /** |
| 1012 | * Writes data to the object referenced by handle. |
| 1013 | * |
| 1014 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1015 | */ |
| 1016 | int |
| 1017 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1018 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1019 | { |
| 1020 | struct drm_i915_gem_pwrite *args = data; |
| 1021 | struct drm_gem_object *obj; |
| 1022 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1023 | int ret; |
| 1024 | |
| 1025 | if (args->size == 0) |
| 1026 | return 0; |
| 1027 | |
| 1028 | if (!access_ok(VERIFY_READ, |
| 1029 | (char __user *)(uintptr_t)args->data_ptr, |
| 1030 | args->size)) |
| 1031 | return -EFAULT; |
| 1032 | |
| 1033 | ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr, |
| 1034 | args->size); |
| 1035 | if (ret) |
| 1036 | return -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1037 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1038 | ret = i915_mutex_lock_interruptible(dev); |
| 1039 | if (ret) |
| 1040 | return ret; |
| 1041 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1042 | obj = drm_gem_object_lookup(dev, file, args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1043 | if (obj == NULL) { |
| 1044 | ret = -ENOENT; |
| 1045 | goto unlock; |
| 1046 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1047 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1048 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1049 | /* Bounds check destination. */ |
| 1050 | if (args->offset > obj->size || args->size > obj->size - args->offset) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1051 | ret = -EINVAL; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 1052 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1053 | } |
| 1054 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1055 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1056 | * it would end up going through the fenced access, and we'll get |
| 1057 | * different detiling behavior between reading and writing. |
| 1058 | * pread/pwrite currently are reading and writing from the CPU |
| 1059 | * perspective, requiring manual detiling by the client. |
| 1060 | */ |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 1061 | if (obj_priv->phys_obj) |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1062 | ret = i915_gem_phys_pwrite(dev, obj, args, file); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 1063 | else if (obj_priv->tiling_mode == I915_TILING_NONE && |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 1064 | obj_priv->gtt_space && |
Chris Wilson | 9b8c4a0 | 2010-05-27 14:21:01 +0100 | [diff] [blame] | 1065 | obj->write_domain != I915_GEM_DOMAIN_CPU) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 1066 | ret = i915_gem_object_pin(obj, 0, true); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1067 | if (ret) |
| 1068 | goto out; |
| 1069 | |
| 1070 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
| 1071 | if (ret) |
| 1072 | goto out_unpin; |
| 1073 | |
| 1074 | ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file); |
| 1075 | if (ret == -EFAULT) |
| 1076 | ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file); |
| 1077 | |
| 1078 | out_unpin: |
| 1079 | i915_gem_object_unpin(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1080 | } else { |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1081 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
| 1082 | if (ret) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1083 | goto out; |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1084 | |
| 1085 | ret = -EFAULT; |
| 1086 | if (!i915_gem_object_needs_bit17_swizzle(obj)) |
| 1087 | ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file); |
| 1088 | if (ret == -EFAULT) |
| 1089 | ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1090 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1091 | |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 1092 | out: |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1093 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1094 | unlock: |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1095 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1096 | return ret; |
| 1097 | } |
| 1098 | |
| 1099 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1100 | * Called when user space prepares to use an object with the CPU, either |
| 1101 | * through the mmap ioctl's mapping or a GTT mapping. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1102 | */ |
| 1103 | int |
| 1104 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
| 1105 | struct drm_file *file_priv) |
| 1106 | { |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1107 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1108 | struct drm_i915_gem_set_domain *args = data; |
| 1109 | struct drm_gem_object *obj; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1110 | struct drm_i915_gem_object *obj_priv; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1111 | uint32_t read_domains = args->read_domains; |
| 1112 | uint32_t write_domain = args->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1113 | int ret; |
| 1114 | |
| 1115 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1116 | return -ENODEV; |
| 1117 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1118 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | 21d509e | 2009-06-06 09:46:02 +0100 | [diff] [blame] | 1119 | if (write_domain & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1120 | return -EINVAL; |
| 1121 | |
Chris Wilson | 21d509e | 2009-06-06 09:46:02 +0100 | [diff] [blame] | 1122 | if (read_domains & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1123 | return -EINVAL; |
| 1124 | |
| 1125 | /* Having something in the write domain implies it's in the read |
| 1126 | * domain, and only that read domain. Enforce that in the request. |
| 1127 | */ |
| 1128 | if (write_domain != 0 && read_domains != write_domain) |
| 1129 | return -EINVAL; |
| 1130 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1131 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1132 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1133 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1134 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1135 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 1136 | if (obj == NULL) { |
| 1137 | ret = -ENOENT; |
| 1138 | goto unlock; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1139 | } |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1140 | obj_priv = to_intel_bo(obj); |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1141 | |
| 1142 | intel_mark_busy(dev, obj); |
| 1143 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1144 | if (read_domains & I915_GEM_DOMAIN_GTT) { |
| 1145 | ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0); |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 1146 | |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1147 | /* Update the LRU on the fence for the CPU access that's |
| 1148 | * about to occur. |
| 1149 | */ |
| 1150 | if (obj_priv->fence_reg != I915_FENCE_REG_NONE) { |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 1151 | struct drm_i915_fence_reg *reg = |
| 1152 | &dev_priv->fence_regs[obj_priv->fence_reg]; |
| 1153 | list_move_tail(®->lru_list, |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1154 | &dev_priv->mm.fence_list); |
| 1155 | } |
| 1156 | |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 1157 | /* Silently promote "you're not bound, there was nothing to do" |
| 1158 | * to success, since the client was just asking us to |
| 1159 | * make sure everything was done. |
| 1160 | */ |
| 1161 | if (ret == -EINVAL) |
| 1162 | ret = 0; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1163 | } else { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1164 | ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1167 | /* Maintain LRU order of "inactive" objects */ |
| 1168 | if (ret == 0 && i915_gem_object_is_inactive(obj_priv)) |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1169 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list); |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1170 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1171 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1172 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1173 | mutex_unlock(&dev->struct_mutex); |
| 1174 | return ret; |
| 1175 | } |
| 1176 | |
| 1177 | /** |
| 1178 | * Called when user space has done writes to this buffer |
| 1179 | */ |
| 1180 | int |
| 1181 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
| 1182 | struct drm_file *file_priv) |
| 1183 | { |
| 1184 | struct drm_i915_gem_sw_finish *args = data; |
| 1185 | struct drm_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1186 | int ret = 0; |
| 1187 | |
| 1188 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1189 | return -ENODEV; |
| 1190 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1191 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1192 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1193 | return ret; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1194 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1195 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 1196 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1197 | ret = -ENOENT; |
| 1198 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1201 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 3d2a812 | 2010-09-29 11:39:53 +0100 | [diff] [blame] | 1202 | if (to_intel_bo(obj)->pin_count) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1203 | i915_gem_object_flush_cpu_write_domain(obj); |
| 1204 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1205 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1206 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1207 | mutex_unlock(&dev->struct_mutex); |
| 1208 | return ret; |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * Maps the contents of an object, returning the address it is mapped |
| 1213 | * into. |
| 1214 | * |
| 1215 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1216 | * imply a ref on the object itself. |
| 1217 | */ |
| 1218 | int |
| 1219 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 1220 | struct drm_file *file_priv) |
| 1221 | { |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1222 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1223 | struct drm_i915_gem_mmap *args = data; |
| 1224 | struct drm_gem_object *obj; |
| 1225 | loff_t offset; |
| 1226 | unsigned long addr; |
| 1227 | |
| 1228 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1229 | return -ENODEV; |
| 1230 | |
| 1231 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 1232 | if (obj == NULL) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1233 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1234 | |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1235 | if (obj->size > dev_priv->mm.gtt_mappable_end) { |
| 1236 | drm_gem_object_unreference_unlocked(obj); |
| 1237 | return -E2BIG; |
| 1238 | } |
| 1239 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1240 | offset = args->offset; |
| 1241 | |
| 1242 | down_write(¤t->mm->mmap_sem); |
| 1243 | addr = do_mmap(obj->filp, 0, args->size, |
| 1244 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1245 | args->offset); |
| 1246 | up_write(¤t->mm->mmap_sem); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 1247 | drm_gem_object_unreference_unlocked(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1248 | if (IS_ERR((void *)addr)) |
| 1249 | return addr; |
| 1250 | |
| 1251 | args->addr_ptr = (uint64_t) addr; |
| 1252 | |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1256 | /** |
| 1257 | * i915_gem_fault - fault a page into the GTT |
| 1258 | * vma: VMA in question |
| 1259 | * vmf: fault info |
| 1260 | * |
| 1261 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1262 | * from userspace. The fault handler takes care of binding the object to |
| 1263 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1264 | * only if needed based on whether the old reg is still valid or the object |
| 1265 | * is tiled) and inserting a new PTE into the faulting process. |
| 1266 | * |
| 1267 | * Note that the faulting process may involve evicting existing objects |
| 1268 | * from the GTT and/or fence registers to make room. So performance may |
| 1269 | * suffer if the GTT working set is large or there are few fence registers |
| 1270 | * left. |
| 1271 | */ |
| 1272 | int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1273 | { |
| 1274 | struct drm_gem_object *obj = vma->vm_private_data; |
| 1275 | struct drm_device *dev = obj->dev; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1276 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1277 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1278 | pgoff_t page_offset; |
| 1279 | unsigned long pfn; |
| 1280 | int ret = 0; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1281 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1282 | |
| 1283 | /* We don't use vmf->pgoff since that has the fake offset */ |
| 1284 | page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> |
| 1285 | PAGE_SHIFT; |
| 1286 | |
| 1287 | /* Now bind it into the GTT if needed */ |
| 1288 | mutex_lock(&dev->struct_mutex); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1289 | BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1290 | |
| 1291 | if (obj_priv->gtt_space) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 1292 | if (!obj_priv->map_and_fenceable) { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1293 | ret = i915_gem_object_unbind(obj); |
| 1294 | if (ret) |
| 1295 | goto unlock; |
| 1296 | } |
| 1297 | } |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 1298 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1299 | if (!obj_priv->gtt_space) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 1300 | ret = i915_gem_object_bind_to_gtt(obj, 0, true); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1301 | if (ret) |
| 1302 | goto unlock; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1303 | } |
| 1304 | |
Chris Wilson | 4a684a4 | 2010-10-28 14:44:08 +0100 | [diff] [blame] | 1305 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1306 | if (ret) |
| 1307 | goto unlock; |
| 1308 | |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1309 | if (!obj_priv->fault_mappable) { |
| 1310 | obj_priv->fault_mappable = true; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1311 | i915_gem_info_update_mappable(dev_priv, obj_priv, true); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1312 | } |
| 1313 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1314 | /* Need a new fence register? */ |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1315 | if (obj_priv->tiling_mode != I915_TILING_NONE) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 1316 | ret = i915_gem_object_get_fence_reg(obj, true); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1317 | if (ret) |
| 1318 | goto unlock; |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 1319 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1320 | |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1321 | if (i915_gem_object_is_inactive(obj_priv)) |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1322 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list); |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1323 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1324 | pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) + |
| 1325 | page_offset; |
| 1326 | |
| 1327 | /* Finally, remap it using the new GTT offset */ |
| 1328 | ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1329 | unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1330 | mutex_unlock(&dev->struct_mutex); |
| 1331 | |
| 1332 | switch (ret) { |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1333 | case -EAGAIN: |
| 1334 | set_need_resched(); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1335 | case 0: |
| 1336 | case -ERESTARTSYS: |
| 1337 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1338 | case -ENOMEM: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1339 | return VM_FAULT_OOM; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1340 | default: |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1341 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | /** |
| 1346 | * i915_gem_create_mmap_offset - create a fake mmap offset for an object |
| 1347 | * @obj: obj in question |
| 1348 | * |
| 1349 | * GEM memory mapping works by handing back to userspace a fake mmap offset |
| 1350 | * it can use in a subsequent mmap(2) call. The DRM core code then looks |
| 1351 | * up the object based on the offset and sets up the various memory mapping |
| 1352 | * structures. |
| 1353 | * |
| 1354 | * This routine allocates and attaches a fake offset for @obj. |
| 1355 | */ |
| 1356 | static int |
| 1357 | i915_gem_create_mmap_offset(struct drm_gem_object *obj) |
| 1358 | { |
| 1359 | struct drm_device *dev = obj->dev; |
| 1360 | struct drm_gem_mm *mm = dev->mm_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1361 | struct drm_map_list *list; |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 1362 | struct drm_local_map *map; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1363 | int ret = 0; |
| 1364 | |
| 1365 | /* Set the object up for mmap'ing */ |
| 1366 | list = &obj->map_list; |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1367 | list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1368 | if (!list->map) |
| 1369 | return -ENOMEM; |
| 1370 | |
| 1371 | map = list->map; |
| 1372 | map->type = _DRM_GEM; |
| 1373 | map->size = obj->size; |
| 1374 | map->handle = obj; |
| 1375 | |
| 1376 | /* Get a DRM GEM mmap offset allocated... */ |
| 1377 | list->file_offset_node = drm_mm_search_free(&mm->offset_manager, |
| 1378 | obj->size / PAGE_SIZE, 0, 0); |
| 1379 | if (!list->file_offset_node) { |
| 1380 | DRM_ERROR("failed to allocate offset for bo %d\n", obj->name); |
Chris Wilson | 9e0ae534 | 2010-09-21 15:05:24 +0100 | [diff] [blame] | 1381 | ret = -ENOSPC; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1382 | goto out_free_list; |
| 1383 | } |
| 1384 | |
| 1385 | list->file_offset_node = drm_mm_get_block(list->file_offset_node, |
| 1386 | obj->size / PAGE_SIZE, 0); |
| 1387 | if (!list->file_offset_node) { |
| 1388 | ret = -ENOMEM; |
| 1389 | goto out_free_list; |
| 1390 | } |
| 1391 | |
| 1392 | list->hash.key = list->file_offset_node->start; |
Chris Wilson | 9e0ae534 | 2010-09-21 15:05:24 +0100 | [diff] [blame] | 1393 | ret = drm_ht_insert_item(&mm->offset_hash, &list->hash); |
| 1394 | if (ret) { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1395 | DRM_ERROR("failed to add to map hash\n"); |
| 1396 | goto out_free_mm; |
| 1397 | } |
| 1398 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1399 | return 0; |
| 1400 | |
| 1401 | out_free_mm: |
| 1402 | drm_mm_put_block(list->file_offset_node); |
| 1403 | out_free_list: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1404 | kfree(list->map); |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame] | 1405 | list->map = NULL; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1406 | |
| 1407 | return ret; |
| 1408 | } |
| 1409 | |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1410 | /** |
| 1411 | * i915_gem_release_mmap - remove physical page mappings |
| 1412 | * @obj: obj in question |
| 1413 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1414 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1415 | * relinquish ownership of the pages back to the system. |
| 1416 | * |
| 1417 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 1418 | * object through the GTT and then lose the fence register due to |
| 1419 | * resource pressure. Similarly if the object has been moved out of the |
| 1420 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 1421 | * mapping will then trigger a page fault on the next user access, allowing |
| 1422 | * fixup by i915_gem_fault(). |
| 1423 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 1424 | void |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1425 | i915_gem_release_mmap(struct drm_gem_object *obj) |
| 1426 | { |
| 1427 | struct drm_device *dev = obj->dev; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1428 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1429 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1430 | |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame] | 1431 | if (unlikely(obj->map_list.map && dev->dev_mapping)) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1432 | unmap_mapping_range(dev->dev_mapping, |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame] | 1433 | (loff_t)obj->map_list.hash.key<<PAGE_SHIFT, |
| 1434 | obj->size, 1); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1435 | |
| 1436 | if (obj_priv->fault_mappable) { |
| 1437 | obj_priv->fault_mappable = false; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1438 | i915_gem_info_update_mappable(dev_priv, obj_priv, false); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1439 | } |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1440 | } |
| 1441 | |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1442 | static void |
| 1443 | i915_gem_free_mmap_offset(struct drm_gem_object *obj) |
| 1444 | { |
| 1445 | struct drm_device *dev = obj->dev; |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1446 | struct drm_gem_mm *mm = dev->mm_private; |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame] | 1447 | struct drm_map_list *list = &obj->map_list; |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1448 | |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1449 | drm_ht_remove_item(&mm->offset_hash, &list->hash); |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame] | 1450 | drm_mm_put_block(list->file_offset_node); |
| 1451 | kfree(list->map); |
| 1452 | list->map = NULL; |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1453 | } |
| 1454 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1455 | /** |
| 1456 | * i915_gem_get_gtt_alignment - return required GTT alignment for an object |
| 1457 | * @obj: object to check |
| 1458 | * |
| 1459 | * Return the required GTT alignment for an object, taking into account |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 1460 | * potential fence register mapping. |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1461 | */ |
| 1462 | static uint32_t |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1463 | i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1464 | { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1465 | struct drm_device *dev = obj_priv->base.dev; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1466 | |
| 1467 | /* |
| 1468 | * Minimum alignment is 4k (GTT page size), but might be greater |
| 1469 | * if a fence register is needed for the object. |
| 1470 | */ |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1471 | if (INTEL_INFO(dev)->gen >= 4 || |
| 1472 | obj_priv->tiling_mode == I915_TILING_NONE) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1473 | return 4096; |
| 1474 | |
| 1475 | /* |
| 1476 | * Previous chips need to be aligned to the size of the smallest |
| 1477 | * fence register that can contain the object. |
| 1478 | */ |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1479 | return i915_gem_get_gtt_size(obj_priv); |
| 1480 | } |
| 1481 | |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 1482 | /** |
| 1483 | * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an |
| 1484 | * unfenced object |
| 1485 | * @obj: object to check |
| 1486 | * |
| 1487 | * Return the required GTT alignment for an object, only taking into account |
| 1488 | * unfenced tiled surface requirements. |
| 1489 | */ |
| 1490 | static uint32_t |
| 1491 | i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj_priv) |
| 1492 | { |
| 1493 | struct drm_device *dev = obj_priv->base.dev; |
| 1494 | int tile_height; |
| 1495 | |
| 1496 | /* |
| 1497 | * Minimum alignment is 4k (GTT page size) for sane hw. |
| 1498 | */ |
| 1499 | if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) || |
| 1500 | obj_priv->tiling_mode == I915_TILING_NONE) |
| 1501 | return 4096; |
| 1502 | |
| 1503 | /* |
| 1504 | * Older chips need unfenced tiled buffers to be aligned to the left |
| 1505 | * edge of an even tile row (where tile rows are counted as if the bo is |
| 1506 | * placed in a fenced gtt region). |
| 1507 | */ |
| 1508 | if (IS_GEN2(dev) || |
| 1509 | (obj_priv->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))) |
| 1510 | tile_height = 32; |
| 1511 | else |
| 1512 | tile_height = 8; |
| 1513 | |
| 1514 | return tile_height * obj_priv->stride * 2; |
| 1515 | } |
| 1516 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1517 | static uint32_t |
| 1518 | i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv) |
| 1519 | { |
| 1520 | struct drm_device *dev = obj_priv->base.dev; |
| 1521 | uint32_t size; |
| 1522 | |
| 1523 | /* |
| 1524 | * Minimum alignment is 4k (GTT page size), but might be greater |
| 1525 | * if a fence register is needed for the object. |
| 1526 | */ |
| 1527 | if (INTEL_INFO(dev)->gen >= 4) |
| 1528 | return obj_priv->base.size; |
| 1529 | |
| 1530 | /* |
| 1531 | * Previous chips need to be aligned to the size of the smallest |
| 1532 | * fence register that can contain the object. |
| 1533 | */ |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 1534 | if (INTEL_INFO(dev)->gen == 3) |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1535 | size = 1024*1024; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1536 | else |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1537 | size = 512*1024; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1538 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1539 | while (size < obj_priv->base.size) |
| 1540 | size <<= 1; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1541 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1542 | return size; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | /** |
| 1546 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 1547 | * @dev: DRM device |
| 1548 | * @data: GTT mapping ioctl data |
| 1549 | * @file_priv: GEM object info |
| 1550 | * |
| 1551 | * Simply returns the fake offset to userspace so it can mmap it. |
| 1552 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 1553 | * up so we can get faults in the handler above. |
| 1554 | * |
| 1555 | * The fault handler will take care of binding the object into the GTT |
| 1556 | * (since it may have been evicted to make room for something), allocating |
| 1557 | * a fence register, and mapping the appropriate aperture address into |
| 1558 | * userspace. |
| 1559 | */ |
| 1560 | int |
| 1561 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 1562 | struct drm_file *file_priv) |
| 1563 | { |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1564 | struct drm_i915_private *dev_priv = dev->dev_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1565 | struct drm_i915_gem_mmap_gtt *args = data; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1566 | struct drm_gem_object *obj; |
| 1567 | struct drm_i915_gem_object *obj_priv; |
| 1568 | int ret; |
| 1569 | |
| 1570 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1571 | return -ENODEV; |
| 1572 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1573 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1574 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1575 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1576 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1577 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1578 | if (obj == NULL) { |
| 1579 | ret = -ENOENT; |
| 1580 | goto unlock; |
| 1581 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1582 | obj_priv = to_intel_bo(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1583 | |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1584 | if (obj->size > dev_priv->mm.gtt_mappable_end) { |
| 1585 | ret = -E2BIG; |
| 1586 | goto unlock; |
| 1587 | } |
| 1588 | |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 1589 | if (obj_priv->madv != I915_MADV_WILLNEED) { |
| 1590 | DRM_ERROR("Attempting to mmap a purgeable buffer\n"); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1591 | ret = -EINVAL; |
| 1592 | goto out; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 1593 | } |
| 1594 | |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame] | 1595 | if (!obj->map_list.map) { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1596 | ret = i915_gem_create_mmap_offset(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1597 | if (ret) |
| 1598 | goto out; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1599 | } |
| 1600 | |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame] | 1601 | args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1602 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1603 | out: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1604 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1605 | unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1606 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1607 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1608 | } |
| 1609 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1610 | static int |
| 1611 | i915_gem_object_get_pages_gtt(struct drm_gem_object *obj, |
| 1612 | gfp_t gfpmask) |
| 1613 | { |
| 1614 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
| 1615 | int page_count, i; |
| 1616 | struct address_space *mapping; |
| 1617 | struct inode *inode; |
| 1618 | struct page *page; |
| 1619 | |
| 1620 | /* Get the list of pages out of our struct file. They'll be pinned |
| 1621 | * at this point until we release them. |
| 1622 | */ |
| 1623 | page_count = obj->size / PAGE_SIZE; |
| 1624 | BUG_ON(obj_priv->pages != NULL); |
| 1625 | obj_priv->pages = drm_malloc_ab(page_count, sizeof(struct page *)); |
| 1626 | if (obj_priv->pages == NULL) |
| 1627 | return -ENOMEM; |
| 1628 | |
| 1629 | inode = obj->filp->f_path.dentry->d_inode; |
| 1630 | mapping = inode->i_mapping; |
| 1631 | for (i = 0; i < page_count; i++) { |
| 1632 | page = read_cache_page_gfp(mapping, i, |
| 1633 | GFP_HIGHUSER | |
| 1634 | __GFP_COLD | |
| 1635 | __GFP_RECLAIMABLE | |
| 1636 | gfpmask); |
| 1637 | if (IS_ERR(page)) |
| 1638 | goto err_pages; |
| 1639 | |
| 1640 | obj_priv->pages[i] = page; |
| 1641 | } |
| 1642 | |
| 1643 | if (obj_priv->tiling_mode != I915_TILING_NONE) |
| 1644 | i915_gem_object_do_bit_17_swizzle(obj); |
| 1645 | |
| 1646 | return 0; |
| 1647 | |
| 1648 | err_pages: |
| 1649 | while (i--) |
| 1650 | page_cache_release(obj_priv->pages[i]); |
| 1651 | |
| 1652 | drm_free_large(obj_priv->pages); |
| 1653 | obj_priv->pages = NULL; |
| 1654 | return PTR_ERR(page); |
| 1655 | } |
| 1656 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 1657 | static void |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1658 | i915_gem_object_put_pages_gtt(struct drm_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1659 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1660 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1661 | int page_count = obj->size / PAGE_SIZE; |
| 1662 | int i; |
| 1663 | |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1664 | BUG_ON(obj_priv->madv == __I915_MADV_PURGED); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1665 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 1666 | if (obj_priv->tiling_mode != I915_TILING_NONE) |
| 1667 | i915_gem_object_save_bit_17_swizzle(obj); |
| 1668 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1669 | if (obj_priv->madv == I915_MADV_DONTNEED) |
Chris Wilson | 13a05fd | 2009-09-20 23:03:19 +0100 | [diff] [blame] | 1670 | obj_priv->dirty = 0; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1671 | |
| 1672 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1673 | if (obj_priv->dirty) |
| 1674 | set_page_dirty(obj_priv->pages[i]); |
| 1675 | |
| 1676 | if (obj_priv->madv == I915_MADV_WILLNEED) |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1677 | mark_page_accessed(obj_priv->pages[i]); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1678 | |
| 1679 | page_cache_release(obj_priv->pages[i]); |
| 1680 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1681 | obj_priv->dirty = 0; |
| 1682 | |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 1683 | drm_free_large(obj_priv->pages); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1684 | obj_priv->pages = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1687 | static uint32_t |
| 1688 | i915_gem_next_request_seqno(struct drm_device *dev, |
| 1689 | struct intel_ring_buffer *ring) |
| 1690 | { |
| 1691 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 5d97eb6 | 2010-11-10 20:40:02 +0000 | [diff] [blame] | 1692 | return ring->outstanding_lazy_request = dev_priv->next_seqno; |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1693 | } |
| 1694 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1695 | static void |
Daniel Vetter | 617dbe2 | 2010-02-11 22:16:02 +0100 | [diff] [blame] | 1696 | i915_gem_object_move_to_active(struct drm_gem_object *obj, |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1697 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1698 | { |
| 1699 | struct drm_device *dev = obj->dev; |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1700 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1701 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1702 | uint32_t seqno = i915_gem_next_request_seqno(dev, ring); |
Daniel Vetter | 617dbe2 | 2010-02-11 22:16:02 +0100 | [diff] [blame] | 1703 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1704 | BUG_ON(ring == NULL); |
| 1705 | obj_priv->ring = ring; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1706 | |
| 1707 | /* Add a reference if we're newly entering the active list. */ |
| 1708 | if (!obj_priv->active) { |
| 1709 | drm_gem_object_reference(obj); |
| 1710 | obj_priv->active = 1; |
| 1711 | } |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 1712 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1713 | /* Move from whatever list we were on to the tail of execution. */ |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1714 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list); |
| 1715 | list_move_tail(&obj_priv->ring_list, &ring->active_list); |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1716 | obj_priv->last_rendering_seqno = seqno; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1719 | static void |
| 1720 | i915_gem_object_move_to_flushing(struct drm_gem_object *obj) |
| 1721 | { |
| 1722 | struct drm_device *dev = obj->dev; |
| 1723 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1724 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1725 | |
| 1726 | BUG_ON(!obj_priv->active); |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1727 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list); |
| 1728 | list_del_init(&obj_priv->ring_list); |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1729 | obj_priv->last_rendering_seqno = 0; |
| 1730 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1731 | |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1732 | /* Immediately discard the backing storage */ |
| 1733 | static void |
| 1734 | i915_gem_object_truncate(struct drm_gem_object *obj) |
| 1735 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1736 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1737 | struct inode *inode; |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1738 | |
Chris Wilson | ae9fed6 | 2010-08-07 11:01:30 +0100 | [diff] [blame] | 1739 | /* Our goal here is to return as much of the memory as |
| 1740 | * is possible back to the system as we are called from OOM. |
| 1741 | * To do this we must instruct the shmfs to drop all of its |
| 1742 | * backing pages, *now*. Here we mirror the actions taken |
| 1743 | * when by shmem_delete_inode() to release the backing store. |
| 1744 | */ |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1745 | inode = obj->filp->f_path.dentry->d_inode; |
Chris Wilson | ae9fed6 | 2010-08-07 11:01:30 +0100 | [diff] [blame] | 1746 | truncate_inode_pages(inode->i_mapping, 0); |
| 1747 | if (inode->i_op->truncate_range) |
| 1748 | inode->i_op->truncate_range(inode, 0, (loff_t)-1); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1749 | |
| 1750 | obj_priv->madv = __I915_MADV_PURGED; |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | static inline int |
| 1754 | i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv) |
| 1755 | { |
| 1756 | return obj_priv->madv == I915_MADV_DONTNEED; |
| 1757 | } |
| 1758 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1759 | static void |
| 1760 | i915_gem_object_move_to_inactive(struct drm_gem_object *obj) |
| 1761 | { |
| 1762 | struct drm_device *dev = obj->dev; |
| 1763 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1764 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1765 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1766 | if (obj_priv->pin_count != 0) |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1767 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1768 | else |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1769 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list); |
| 1770 | list_del_init(&obj_priv->ring_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1771 | |
Daniel Vetter | 99fcb76 | 2010-02-07 16:20:18 +0100 | [diff] [blame] | 1772 | BUG_ON(!list_empty(&obj_priv->gpu_write_list)); |
| 1773 | |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1774 | obj_priv->last_rendering_seqno = 0; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1775 | obj_priv->ring = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1776 | if (obj_priv->active) { |
| 1777 | obj_priv->active = 0; |
| 1778 | drm_gem_object_unreference(obj); |
| 1779 | } |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 1780 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1783 | static void |
| 1784 | i915_gem_process_flushing_list(struct drm_device *dev, |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1785 | uint32_t flush_domains, |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1786 | struct intel_ring_buffer *ring) |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1787 | { |
| 1788 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1789 | struct drm_i915_gem_object *obj_priv, *next; |
| 1790 | |
| 1791 | list_for_each_entry_safe(obj_priv, next, |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 1792 | &ring->gpu_write_list, |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1793 | gpu_write_list) { |
Daniel Vetter | a8089e8 | 2010-04-09 19:05:09 +0000 | [diff] [blame] | 1794 | struct drm_gem_object *obj = &obj_priv->base; |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1795 | |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 1796 | if (obj->write_domain & flush_domains) { |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1797 | uint32_t old_write_domain = obj->write_domain; |
| 1798 | |
| 1799 | obj->write_domain = 0; |
| 1800 | list_del_init(&obj_priv->gpu_write_list); |
Daniel Vetter | 617dbe2 | 2010-02-11 22:16:02 +0100 | [diff] [blame] | 1801 | i915_gem_object_move_to_active(obj, ring); |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1802 | |
| 1803 | /* update the fence lru list */ |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 1804 | if (obj_priv->fence_reg != I915_FENCE_REG_NONE) { |
| 1805 | struct drm_i915_fence_reg *reg = |
| 1806 | &dev_priv->fence_regs[obj_priv->fence_reg]; |
| 1807 | list_move_tail(®->lru_list, |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1808 | &dev_priv->mm.fence_list); |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 1809 | } |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1810 | |
| 1811 | trace_i915_gem_object_change_domain(obj, |
| 1812 | obj->read_domains, |
| 1813 | old_write_domain); |
| 1814 | } |
| 1815 | } |
| 1816 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 1817 | |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1818 | int |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1819 | i915_add_request(struct drm_device *dev, |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1820 | struct drm_file *file, |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 1821 | struct drm_i915_gem_request *request, |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1822 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1823 | { |
| 1824 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1825 | struct drm_i915_file_private *file_priv = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1826 | uint32_t seqno; |
| 1827 | int was_empty; |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1828 | int ret; |
| 1829 | |
| 1830 | BUG_ON(request == NULL); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1831 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1832 | if (file != NULL) |
| 1833 | file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1834 | |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1835 | ret = ring->add_request(ring, &seqno); |
| 1836 | if (ret) |
| 1837 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1838 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1839 | ring->outstanding_lazy_request = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1840 | |
| 1841 | request->seqno = seqno; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1842 | request->ring = ring; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1843 | request->emitted_jiffies = jiffies; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1844 | was_empty = list_empty(&ring->request_list); |
| 1845 | list_add_tail(&request->list, &ring->request_list); |
| 1846 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1847 | if (file_priv) { |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1848 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1849 | request->file_priv = file_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1850 | list_add_tail(&request->client_list, |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1851 | &file_priv->mm.request_list); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1852 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1853 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1854 | |
Ben Gamari | f65d942 | 2009-09-14 17:48:44 -0400 | [diff] [blame] | 1855 | if (!dev_priv->mm.suspended) { |
Chris Wilson | b3b079d | 2010-09-13 23:44:34 +0100 | [diff] [blame] | 1856 | mod_timer(&dev_priv->hangcheck_timer, |
| 1857 | jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD)); |
Ben Gamari | f65d942 | 2009-09-14 17:48:44 -0400 | [diff] [blame] | 1858 | if (was_empty) |
Chris Wilson | b3b079d | 2010-09-13 23:44:34 +0100 | [diff] [blame] | 1859 | queue_delayed_work(dev_priv->wq, |
| 1860 | &dev_priv->mm.retire_work, HZ); |
Ben Gamari | f65d942 | 2009-09-14 17:48:44 -0400 | [diff] [blame] | 1861 | } |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1862 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1863 | } |
| 1864 | |
| 1865 | /** |
| 1866 | * Command execution barrier |
| 1867 | * |
| 1868 | * Ensures that all commands in the ring are finished |
| 1869 | * before signalling the CPU |
| 1870 | */ |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1871 | static void |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1872 | i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1873 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1874 | uint32_t flush_domains = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1875 | |
| 1876 | /* The sampler always gets flushed on i965 (sigh) */ |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 1877 | if (INTEL_INFO(dev)->gen >= 4) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1878 | flush_domains |= I915_GEM_DOMAIN_SAMPLER; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1879 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 1880 | ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1881 | } |
| 1882 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1883 | static inline void |
| 1884 | i915_gem_request_remove_from_client(struct drm_i915_gem_request *request) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1885 | { |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1886 | struct drm_i915_file_private *file_priv = request->file_priv; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1887 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1888 | if (!file_priv) |
| 1889 | return; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 1890 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1891 | spin_lock(&file_priv->mm.lock); |
| 1892 | list_del(&request->client_list); |
| 1893 | request->file_priv = NULL; |
| 1894 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1897 | static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv, |
| 1898 | struct intel_ring_buffer *ring) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1899 | { |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1900 | while (!list_empty(&ring->request_list)) { |
| 1901 | struct drm_i915_gem_request *request; |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1902 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1903 | request = list_first_entry(&ring->request_list, |
| 1904 | struct drm_i915_gem_request, |
| 1905 | list); |
| 1906 | |
| 1907 | list_del(&request->list); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1908 | i915_gem_request_remove_from_client(request); |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1909 | kfree(request); |
| 1910 | } |
| 1911 | |
| 1912 | while (!list_empty(&ring->active_list)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1913 | struct drm_i915_gem_object *obj_priv; |
| 1914 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1915 | obj_priv = list_first_entry(&ring->active_list, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1916 | struct drm_i915_gem_object, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1917 | ring_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1918 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1919 | obj_priv->base.write_domain = 0; |
| 1920 | list_del_init(&obj_priv->gpu_write_list); |
| 1921 | i915_gem_object_move_to_inactive(&obj_priv->base); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1922 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1923 | } |
| 1924 | |
Chris Wilson | 069efc1 | 2010-09-30 16:53:18 +0100 | [diff] [blame] | 1925 | void i915_gem_reset(struct drm_device *dev) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1926 | { |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1927 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1928 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 069efc1 | 2010-09-30 16:53:18 +0100 | [diff] [blame] | 1929 | int i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1930 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1931 | i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 1932 | i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 1933 | i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring); |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1934 | |
| 1935 | /* Remove anything from the flushing lists. The GPU cache is likely |
| 1936 | * to be lost on reset along with the data, so simply move the |
| 1937 | * lost bo to the inactive list. |
| 1938 | */ |
| 1939 | while (!list_empty(&dev_priv->mm.flushing_list)) { |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1940 | obj_priv = list_first_entry(&dev_priv->mm.flushing_list, |
| 1941 | struct drm_i915_gem_object, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1942 | mm_list); |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1943 | |
| 1944 | obj_priv->base.write_domain = 0; |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1945 | list_del_init(&obj_priv->gpu_write_list); |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1946 | i915_gem_object_move_to_inactive(&obj_priv->base); |
| 1947 | } |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1948 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1949 | /* Move everything out of the GPU domains to ensure we do any |
| 1950 | * necessary invalidation upon reuse. |
| 1951 | */ |
Chris Wilson | 77f0123 | 2010-09-19 12:31:36 +0100 | [diff] [blame] | 1952 | list_for_each_entry(obj_priv, |
| 1953 | &dev_priv->mm.inactive_list, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1954 | mm_list) |
Chris Wilson | 77f0123 | 2010-09-19 12:31:36 +0100 | [diff] [blame] | 1955 | { |
| 1956 | obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS; |
| 1957 | } |
Chris Wilson | 069efc1 | 2010-09-30 16:53:18 +0100 | [diff] [blame] | 1958 | |
| 1959 | /* The fence registers are invalidated so clear them out */ |
| 1960 | for (i = 0; i < 16; i++) { |
| 1961 | struct drm_i915_fence_reg *reg; |
| 1962 | |
| 1963 | reg = &dev_priv->fence_regs[i]; |
| 1964 | if (!reg->obj) |
| 1965 | continue; |
| 1966 | |
| 1967 | i915_gem_clear_fence_reg(reg->obj); |
| 1968 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | /** |
| 1972 | * This function clears the request list as sequence numbers are passed. |
| 1973 | */ |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 1974 | static void |
| 1975 | i915_gem_retire_requests_ring(struct drm_device *dev, |
| 1976 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1977 | { |
| 1978 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1979 | uint32_t seqno; |
| 1980 | |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1981 | if (!ring->status_page.page_addr || |
| 1982 | list_empty(&ring->request_list)) |
Karsten Wiese | 6c0594a | 2009-02-23 15:07:57 +0100 | [diff] [blame] | 1983 | return; |
| 1984 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 1985 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1986 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 1987 | seqno = ring->get_seqno(ring); |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1988 | while (!list_empty(&ring->request_list)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1989 | struct drm_i915_gem_request *request; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1990 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1991 | request = list_first_entry(&ring->request_list, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1992 | struct drm_i915_gem_request, |
| 1993 | list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1994 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1995 | if (!i915_seqno_passed(seqno, request->seqno)) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1996 | break; |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1997 | |
| 1998 | trace_i915_gem_request_retire(dev, request->seqno); |
| 1999 | |
| 2000 | list_del(&request->list); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 2001 | i915_gem_request_remove_from_client(request); |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2002 | kfree(request); |
| 2003 | } |
| 2004 | |
| 2005 | /* Move any buffers on the active list that are no longer referenced |
| 2006 | * by the ringbuffer to the flushing/inactive lists as appropriate. |
| 2007 | */ |
| 2008 | while (!list_empty(&ring->active_list)) { |
| 2009 | struct drm_gem_object *obj; |
| 2010 | struct drm_i915_gem_object *obj_priv; |
| 2011 | |
| 2012 | obj_priv = list_first_entry(&ring->active_list, |
| 2013 | struct drm_i915_gem_object, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 2014 | ring_list); |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2015 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 2016 | if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno)) |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2017 | break; |
| 2018 | |
| 2019 | obj = &obj_priv->base; |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2020 | if (obj->write_domain != 0) |
| 2021 | i915_gem_object_move_to_flushing(obj); |
| 2022 | else |
| 2023 | i915_gem_object_move_to_inactive(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2024 | } |
Chris Wilson | 9d34e5d | 2009-09-24 05:26:06 +0100 | [diff] [blame] | 2025 | |
| 2026 | if (unlikely (dev_priv->trace_irq_seqno && |
| 2027 | i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) { |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2028 | ring->user_irq_put(ring); |
Chris Wilson | 9d34e5d | 2009-09-24 05:26:06 +0100 | [diff] [blame] | 2029 | dev_priv->trace_irq_seqno = 0; |
| 2030 | } |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 2031 | |
| 2032 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | void |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2036 | i915_gem_retire_requests(struct drm_device *dev) |
| 2037 | { |
| 2038 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 2039 | |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 2040 | if (!list_empty(&dev_priv->mm.deferred_free_list)) { |
| 2041 | struct drm_i915_gem_object *obj_priv, *tmp; |
| 2042 | |
| 2043 | /* We must be careful that during unbind() we do not |
| 2044 | * accidentally infinitely recurse into retire requests. |
| 2045 | * Currently: |
| 2046 | * retire -> free -> unbind -> wait -> retire_ring |
| 2047 | */ |
| 2048 | list_for_each_entry_safe(obj_priv, tmp, |
| 2049 | &dev_priv->mm.deferred_free_list, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 2050 | mm_list) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 2051 | i915_gem_free_object_tail(&obj_priv->base); |
| 2052 | } |
| 2053 | |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2054 | i915_gem_retire_requests_ring(dev, &dev_priv->render_ring); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 2055 | i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2056 | i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring); |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 2059 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2060 | i915_gem_retire_work_handler(struct work_struct *work) |
| 2061 | { |
| 2062 | drm_i915_private_t *dev_priv; |
| 2063 | struct drm_device *dev; |
| 2064 | |
| 2065 | dev_priv = container_of(work, drm_i915_private_t, |
| 2066 | mm.retire_work.work); |
| 2067 | dev = dev_priv->dev; |
| 2068 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2069 | /* Come back later if the device is busy... */ |
| 2070 | if (!mutex_trylock(&dev->struct_mutex)) { |
| 2071 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ); |
| 2072 | return; |
| 2073 | } |
| 2074 | |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2075 | i915_gem_retire_requests(dev); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2076 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 2077 | if (!dev_priv->mm.suspended && |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2078 | (!list_empty(&dev_priv->render_ring.request_list) || |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2079 | !list_empty(&dev_priv->bsd_ring.request_list) || |
| 2080 | !list_empty(&dev_priv->blt_ring.request_list))) |
Eric Anholt | 9c9fe1f | 2009-08-03 16:09:16 -0700 | [diff] [blame] | 2081 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2082 | mutex_unlock(&dev->struct_mutex); |
| 2083 | } |
| 2084 | |
Daniel Vetter | 5a5a0c6 | 2009-09-15 22:57:36 +0200 | [diff] [blame] | 2085 | int |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2086 | i915_do_wait_request(struct drm_device *dev, uint32_t seqno, |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 2087 | bool interruptible, struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2088 | { |
| 2089 | drm_i915_private_t *dev_priv = dev->dev_private; |
Jesse Barnes | 802c7eb | 2009-05-05 16:03:48 -0700 | [diff] [blame] | 2090 | u32 ier; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2091 | int ret = 0; |
| 2092 | |
| 2093 | BUG_ON(seqno == 0); |
| 2094 | |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 2095 | if (atomic_read(&dev_priv->mm.wedged)) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 2096 | return -EAGAIN; |
Ben Gamari | ffed1d0 | 2009-09-14 17:48:41 -0400 | [diff] [blame] | 2097 | |
Chris Wilson | 5d97eb6 | 2010-11-10 20:40:02 +0000 | [diff] [blame] | 2098 | if (seqno == ring->outstanding_lazy_request) { |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 2099 | struct drm_i915_gem_request *request; |
| 2100 | |
| 2101 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
| 2102 | if (request == NULL) |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 2103 | return -ENOMEM; |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 2104 | |
| 2105 | ret = i915_add_request(dev, NULL, request, ring); |
| 2106 | if (ret) { |
| 2107 | kfree(request); |
| 2108 | return ret; |
| 2109 | } |
| 2110 | |
| 2111 | seqno = request->seqno; |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 2112 | } |
| 2113 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2114 | if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) { |
Eric Anholt | bad720f | 2009-10-22 16:11:14 -0700 | [diff] [blame] | 2115 | if (HAS_PCH_SPLIT(dev)) |
Zhenyu Wang | 036a4a7 | 2009-06-08 14:40:19 +0800 | [diff] [blame] | 2116 | ier = I915_READ(DEIER) | I915_READ(GTIER); |
| 2117 | else |
| 2118 | ier = I915_READ(IER); |
Jesse Barnes | 802c7eb | 2009-05-05 16:03:48 -0700 | [diff] [blame] | 2119 | if (!ier) { |
| 2120 | DRM_ERROR("something (likely vbetool) disabled " |
| 2121 | "interrupts, re-enabling\n"); |
| 2122 | i915_driver_irq_preinstall(dev); |
| 2123 | i915_driver_irq_postinstall(dev); |
| 2124 | } |
| 2125 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2126 | trace_i915_gem_request_wait_begin(dev, seqno); |
| 2127 | |
Chris Wilson | b222349 | 2010-10-27 15:27:33 +0100 | [diff] [blame] | 2128 | ring->waiting_seqno = seqno; |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2129 | ring->user_irq_get(ring); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2130 | if (interruptible) |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2131 | ret = wait_event_interruptible(ring->irq_queue, |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2132 | i915_seqno_passed(ring->get_seqno(ring), seqno) |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2133 | || atomic_read(&dev_priv->mm.wedged)); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2134 | else |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2135 | wait_event(ring->irq_queue, |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2136 | i915_seqno_passed(ring->get_seqno(ring), seqno) |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2137 | || atomic_read(&dev_priv->mm.wedged)); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2138 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2139 | ring->user_irq_put(ring); |
Chris Wilson | b222349 | 2010-10-27 15:27:33 +0100 | [diff] [blame] | 2140 | ring->waiting_seqno = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2141 | |
| 2142 | trace_i915_gem_request_wait_end(dev, seqno); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2143 | } |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 2144 | if (atomic_read(&dev_priv->mm.wedged)) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 2145 | ret = -EAGAIN; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2146 | |
| 2147 | if (ret && ret != -ERESTARTSYS) |
Daniel Vetter | 8bff917 | 2010-02-11 22:19:40 +0100 | [diff] [blame] | 2148 | DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n", |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2149 | __func__, ret, seqno, ring->get_seqno(ring), |
Daniel Vetter | 8bff917 | 2010-02-11 22:19:40 +0100 | [diff] [blame] | 2150 | dev_priv->next_seqno); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2151 | |
| 2152 | /* Directly dispatch request retiring. While we have the work queue |
| 2153 | * to handle this, the waiter on a request often wants an associated |
| 2154 | * buffer to have made it to the inactive list, and we would need |
| 2155 | * a separate wait queue to handle that. |
| 2156 | */ |
| 2157 | if (ret == 0) |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2158 | i915_gem_retire_requests_ring(dev, ring); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2159 | |
| 2160 | return ret; |
| 2161 | } |
| 2162 | |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2163 | /** |
| 2164 | * Waits for a sequence number to be signaled, and cleans up the |
| 2165 | * request and object lists appropriately for that event. |
| 2166 | */ |
| 2167 | static int |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2168 | i915_wait_request(struct drm_device *dev, uint32_t seqno, |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2169 | struct intel_ring_buffer *ring) |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2170 | { |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2171 | return i915_do_wait_request(dev, seqno, 1, ring); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2172 | } |
| 2173 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2174 | static void |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2175 | i915_gem_flush_ring(struct drm_device *dev, |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 2176 | struct drm_file *file_priv, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2177 | struct intel_ring_buffer *ring, |
| 2178 | uint32_t invalidate_domains, |
| 2179 | uint32_t flush_domains) |
| 2180 | { |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2181 | ring->flush(ring, invalidate_domains, flush_domains); |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2182 | i915_gem_process_flushing_list(dev, flush_domains, ring); |
| 2183 | } |
| 2184 | |
| 2185 | static void |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2186 | i915_gem_flush(struct drm_device *dev, |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 2187 | struct drm_file *file_priv, |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2188 | uint32_t invalidate_domains, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2189 | uint32_t flush_domains, |
| 2190 | uint32_t flush_rings) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2191 | { |
| 2192 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 8bff917 | 2010-02-11 22:19:40 +0100 | [diff] [blame] | 2193 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2194 | if (flush_domains & I915_GEM_DOMAIN_CPU) |
Daniel Vetter | 40ce657 | 2010-11-05 18:12:18 +0100 | [diff] [blame^] | 2195 | intel_gtt_chipset_flush(); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2196 | |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2197 | if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) { |
| 2198 | if (flush_rings & RING_RENDER) |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 2199 | i915_gem_flush_ring(dev, file_priv, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2200 | &dev_priv->render_ring, |
| 2201 | invalidate_domains, flush_domains); |
| 2202 | if (flush_rings & RING_BSD) |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 2203 | i915_gem_flush_ring(dev, file_priv, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2204 | &dev_priv->bsd_ring, |
| 2205 | invalidate_domains, flush_domains); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2206 | if (flush_rings & RING_BLT) |
| 2207 | i915_gem_flush_ring(dev, file_priv, |
| 2208 | &dev_priv->blt_ring, |
| 2209 | invalidate_domains, flush_domains); |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2210 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2211 | } |
| 2212 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2213 | /** |
| 2214 | * Ensures that all rendering to the object has completed and the object is |
| 2215 | * safe to unbind from the GTT or access from the CPU. |
| 2216 | */ |
| 2217 | static int |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2218 | i915_gem_object_wait_rendering(struct drm_gem_object *obj, |
| 2219 | bool interruptible) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2220 | { |
| 2221 | struct drm_device *dev = obj->dev; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2222 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2223 | int ret; |
| 2224 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2225 | /* This function only exists to support waiting for existing rendering, |
| 2226 | * not for emitting required flushes. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2227 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2228 | BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2229 | |
| 2230 | /* If there is rendering queued on the buffer being evicted, wait for |
| 2231 | * it. |
| 2232 | */ |
| 2233 | if (obj_priv->active) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2234 | ret = i915_do_wait_request(dev, |
| 2235 | obj_priv->last_rendering_seqno, |
| 2236 | interruptible, |
| 2237 | obj_priv->ring); |
| 2238 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2239 | return ret; |
| 2240 | } |
| 2241 | |
| 2242 | return 0; |
| 2243 | } |
| 2244 | |
| 2245 | /** |
| 2246 | * Unbinds an object from the GTT aperture. |
| 2247 | */ |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2248 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2249 | i915_gem_object_unbind(struct drm_gem_object *obj) |
| 2250 | { |
| 2251 | struct drm_device *dev = obj->dev; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 2252 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2253 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2254 | int ret = 0; |
| 2255 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2256 | if (obj_priv->gtt_space == NULL) |
| 2257 | return 0; |
| 2258 | |
| 2259 | if (obj_priv->pin_count != 0) { |
| 2260 | DRM_ERROR("Attempting to unbind pinned buffer\n"); |
| 2261 | return -EINVAL; |
| 2262 | } |
| 2263 | |
Eric Anholt | 5323fd0 | 2009-09-09 11:50:45 -0700 | [diff] [blame] | 2264 | /* blow away mappings if mapped through GTT */ |
| 2265 | i915_gem_release_mmap(obj); |
| 2266 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2267 | /* Move the object to the CPU domain to ensure that |
| 2268 | * any possible CPU writes while it's not in the GTT |
| 2269 | * are flushed when we go to remap it. This will |
| 2270 | * also ensure that all pending GPU writes are finished |
| 2271 | * before we unbind. |
| 2272 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2273 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
Chris Wilson | 8dc1775 | 2010-07-23 23:18:51 +0100 | [diff] [blame] | 2274 | if (ret == -ERESTARTSYS) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2275 | return ret; |
Chris Wilson | 8dc1775 | 2010-07-23 23:18:51 +0100 | [diff] [blame] | 2276 | /* Continue on if we fail due to EIO, the GPU is hung so we |
| 2277 | * should be safe and we need to cleanup or else we might |
| 2278 | * cause memory corruption through use-after-free. |
| 2279 | */ |
Chris Wilson | 812ed492 | 2010-09-30 15:08:57 +0100 | [diff] [blame] | 2280 | if (ret) { |
| 2281 | i915_gem_clflush_object(obj); |
| 2282 | obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 2283 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2284 | |
Daniel Vetter | 96b47b6 | 2009-12-15 17:50:00 +0100 | [diff] [blame] | 2285 | /* release the fence reg _after_ flushing */ |
| 2286 | if (obj_priv->fence_reg != I915_FENCE_REG_NONE) |
| 2287 | i915_gem_clear_fence_reg(obj); |
| 2288 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 2289 | drm_unbind_agp(obj_priv->agp_mem); |
| 2290 | drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2291 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2292 | i915_gem_object_put_pages_gtt(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2293 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2294 | i915_gem_info_remove_gtt(dev_priv, obj_priv); |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 2295 | list_del_init(&obj_priv->mm_list); |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2296 | /* Avoid an unnecessary call to unbind on rebind. */ |
| 2297 | obj_priv->map_and_fenceable = true; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2298 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 2299 | drm_mm_put_block(obj_priv->gtt_space); |
| 2300 | obj_priv->gtt_space = NULL; |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 2301 | obj_priv->gtt_offset = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2302 | |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 2303 | if (i915_gem_object_is_purgeable(obj_priv)) |
| 2304 | i915_gem_object_truncate(obj); |
| 2305 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2306 | trace_i915_gem_object_unbind(obj); |
| 2307 | |
Chris Wilson | 8dc1775 | 2010-07-23 23:18:51 +0100 | [diff] [blame] | 2308 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2309 | } |
| 2310 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2311 | static int i915_ring_idle(struct drm_device *dev, |
| 2312 | struct intel_ring_buffer *ring) |
| 2313 | { |
Chris Wilson | 395b70b | 2010-10-28 21:28:46 +0100 | [diff] [blame] | 2314 | if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list)) |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 2315 | return 0; |
| 2316 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2317 | i915_gem_flush_ring(dev, NULL, ring, |
| 2318 | I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); |
| 2319 | return i915_wait_request(dev, |
| 2320 | i915_gem_next_request_seqno(dev, ring), |
| 2321 | ring); |
| 2322 | } |
| 2323 | |
Chris Wilson | b47eb4a | 2010-08-07 11:01:23 +0100 | [diff] [blame] | 2324 | int |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2325 | i915_gpu_idle(struct drm_device *dev) |
| 2326 | { |
| 2327 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 2328 | bool lists_empty; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2329 | int ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2330 | |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2331 | lists_empty = (list_empty(&dev_priv->mm.flushing_list) && |
Chris Wilson | 395b70b | 2010-10-28 21:28:46 +0100 | [diff] [blame] | 2332 | list_empty(&dev_priv->mm.active_list)); |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2333 | if (lists_empty) |
| 2334 | return 0; |
| 2335 | |
| 2336 | /* Flush everything onto the inactive list. */ |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2337 | ret = i915_ring_idle(dev, &dev_priv->render_ring); |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 2338 | if (ret) |
| 2339 | return ret; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2340 | |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 2341 | ret = i915_ring_idle(dev, &dev_priv->bsd_ring); |
| 2342 | if (ret) |
| 2343 | return ret; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2344 | |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2345 | ret = i915_ring_idle(dev, &dev_priv->blt_ring); |
| 2346 | if (ret) |
| 2347 | return ret; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2348 | |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 2349 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2350 | } |
| 2351 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2352 | static void sandybridge_write_fence_reg(struct drm_gem_object *obj) |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2353 | { |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2354 | struct drm_device *dev = obj->dev; |
| 2355 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2356 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2357 | u32 size = i915_gem_get_gtt_size(obj_priv); |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2358 | int regnum = obj_priv->fence_reg; |
| 2359 | uint64_t val; |
| 2360 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2361 | val = (uint64_t)((obj_priv->gtt_offset + size - 4096) & |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2362 | 0xfffff000) << 32; |
| 2363 | val |= obj_priv->gtt_offset & 0xfffff000; |
| 2364 | val |= (uint64_t)((obj_priv->stride / 128) - 1) << |
| 2365 | SANDYBRIDGE_FENCE_PITCH_SHIFT; |
| 2366 | |
| 2367 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 2368 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; |
| 2369 | val |= I965_FENCE_REG_VALID; |
| 2370 | |
| 2371 | I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val); |
| 2372 | } |
| 2373 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2374 | static void i965_write_fence_reg(struct drm_gem_object *obj) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2375 | { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2376 | struct drm_device *dev = obj->dev; |
| 2377 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2378 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2379 | u32 size = i915_gem_get_gtt_size(obj_priv); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2380 | int regnum = obj_priv->fence_reg; |
| 2381 | uint64_t val; |
| 2382 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2383 | val = (uint64_t)((obj_priv->gtt_offset + size - 4096) & |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2384 | 0xfffff000) << 32; |
| 2385 | val |= obj_priv->gtt_offset & 0xfffff000; |
| 2386 | val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT; |
| 2387 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 2388 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; |
| 2389 | val |= I965_FENCE_REG_VALID; |
| 2390 | |
| 2391 | I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val); |
| 2392 | } |
| 2393 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2394 | static void i915_write_fence_reg(struct drm_gem_object *obj) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2395 | { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2396 | struct drm_device *dev = obj->dev; |
| 2397 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2398 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2399 | u32 size = i915_gem_get_gtt_size(obj_priv); |
| 2400 | uint32_t fence_reg, val, pitch_val; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2401 | int tile_width; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2402 | |
| 2403 | if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) || |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2404 | (obj_priv->gtt_offset & (size - 1))) { |
| 2405 | WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n", |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2406 | __func__, obj_priv->gtt_offset, obj_priv->map_and_fenceable, size, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2407 | obj_priv->gtt_space->start, obj_priv->gtt_space->size); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2408 | return; |
| 2409 | } |
| 2410 | |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2411 | if (obj_priv->tiling_mode == I915_TILING_Y && |
| 2412 | HAS_128_BYTE_Y_TILING(dev)) |
| 2413 | tile_width = 128; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2414 | else |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2415 | tile_width = 512; |
| 2416 | |
| 2417 | /* Note: pitch better be a power of two tile widths */ |
| 2418 | pitch_val = obj_priv->stride / tile_width; |
| 2419 | pitch_val = ffs(pitch_val) - 1; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2420 | |
Daniel Vetter | c36a2a6 | 2010-04-17 15:12:03 +0200 | [diff] [blame] | 2421 | if (obj_priv->tiling_mode == I915_TILING_Y && |
| 2422 | HAS_128_BYTE_Y_TILING(dev)) |
| 2423 | WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL); |
| 2424 | else |
| 2425 | WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL); |
| 2426 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2427 | val = obj_priv->gtt_offset; |
| 2428 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 2429 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2430 | val |= I915_FENCE_SIZE_BITS(size); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2431 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; |
| 2432 | val |= I830_FENCE_REG_VALID; |
| 2433 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2434 | fence_reg = obj_priv->fence_reg; |
| 2435 | if (fence_reg < 8) |
| 2436 | fence_reg = FENCE_REG_830_0 + fence_reg * 4; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2437 | else |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2438 | fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2439 | I915_WRITE(fence_reg, val); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2440 | } |
| 2441 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2442 | static void i830_write_fence_reg(struct drm_gem_object *obj) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2443 | { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2444 | struct drm_device *dev = obj->dev; |
| 2445 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2446 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2447 | u32 size = i915_gem_get_gtt_size(obj_priv); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2448 | int regnum = obj_priv->fence_reg; |
| 2449 | uint32_t val; |
| 2450 | uint32_t pitch_val; |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 2451 | uint32_t fence_size_bits; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2452 | |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 2453 | if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) || |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2454 | (obj_priv->gtt_offset & (obj->size - 1))) { |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 2455 | WARN(1, "%s: object 0x%08x not 512K or size aligned\n", |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2456 | __func__, obj_priv->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2457 | return; |
| 2458 | } |
| 2459 | |
Eric Anholt | e76a16d | 2009-05-26 17:44:56 -0700 | [diff] [blame] | 2460 | pitch_val = obj_priv->stride / 128; |
| 2461 | pitch_val = ffs(pitch_val) - 1; |
| 2462 | WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL); |
| 2463 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2464 | val = obj_priv->gtt_offset; |
| 2465 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 2466 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2467 | fence_size_bits = I830_FENCE_SIZE_BITS(size); |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 2468 | WARN_ON(fence_size_bits & ~0x00000f00); |
| 2469 | val |= fence_size_bits; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2470 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; |
| 2471 | val |= I830_FENCE_REG_VALID; |
| 2472 | |
| 2473 | I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2474 | } |
| 2475 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2476 | static int i915_find_fence_reg(struct drm_device *dev, |
| 2477 | bool interruptible) |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2478 | { |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2479 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2480 | struct drm_i915_fence_reg *reg; |
| 2481 | struct drm_i915_gem_object *obj_priv = NULL; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2482 | int i, avail, ret; |
| 2483 | |
| 2484 | /* First try to find a free reg */ |
| 2485 | avail = 0; |
| 2486 | for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) { |
| 2487 | reg = &dev_priv->fence_regs[i]; |
| 2488 | if (!reg->obj) |
| 2489 | return i; |
| 2490 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2491 | obj_priv = to_intel_bo(reg->obj); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2492 | if (!obj_priv->pin_count) |
| 2493 | avail++; |
| 2494 | } |
| 2495 | |
| 2496 | if (avail == 0) |
| 2497 | return -ENOSPC; |
| 2498 | |
| 2499 | /* None available, try to steal one or wait for a user to finish */ |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2500 | avail = I915_FENCE_REG_NONE; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2501 | list_for_each_entry(reg, &dev_priv->mm.fence_list, |
| 2502 | lru_list) { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2503 | obj_priv = to_intel_bo(reg->obj); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2504 | if (obj_priv->pin_count) |
| 2505 | continue; |
| 2506 | |
| 2507 | /* found one! */ |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2508 | avail = obj_priv->fence_reg; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2509 | break; |
| 2510 | } |
| 2511 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2512 | BUG_ON(avail == I915_FENCE_REG_NONE); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2513 | |
| 2514 | /* We only have a reference on obj from the active list. put_fence_reg |
| 2515 | * might drop that one, causing a use-after-free in it. So hold a |
| 2516 | * private reference to obj like the other callers of put_fence_reg |
| 2517 | * (set_tiling ioctl) do. */ |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2518 | drm_gem_object_reference(&obj_priv->base); |
| 2519 | ret = i915_gem_object_put_fence_reg(&obj_priv->base, interruptible); |
| 2520 | drm_gem_object_unreference(&obj_priv->base); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2521 | if (ret != 0) |
| 2522 | return ret; |
| 2523 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2524 | return avail; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2525 | } |
| 2526 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2527 | /** |
| 2528 | * i915_gem_object_get_fence_reg - set up a fence reg for an object |
| 2529 | * @obj: object to map through a fence reg |
| 2530 | * |
| 2531 | * When mapping objects through the GTT, userspace wants to be able to write |
| 2532 | * to them without having to worry about swizzling if the object is tiled. |
| 2533 | * |
| 2534 | * This function walks the fence regs looking for a free one for @obj, |
| 2535 | * stealing one if it can't find any. |
| 2536 | * |
| 2537 | * It then sets up the reg based on the object's properties: address, pitch |
| 2538 | * and tiling format. |
| 2539 | */ |
Chris Wilson | 8c4b8c3 | 2009-06-17 22:08:52 +0100 | [diff] [blame] | 2540 | int |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2541 | i915_gem_object_get_fence_reg(struct drm_gem_object *obj, |
| 2542 | bool interruptible) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2543 | { |
| 2544 | struct drm_device *dev = obj->dev; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2545 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2546 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2547 | struct drm_i915_fence_reg *reg = NULL; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2548 | int ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2549 | |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 2550 | /* Just update our place in the LRU if our fence is getting used. */ |
| 2551 | if (obj_priv->fence_reg != I915_FENCE_REG_NONE) { |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2552 | reg = &dev_priv->fence_regs[obj_priv->fence_reg]; |
| 2553 | list_move_tail(®->lru_list, &dev_priv->mm.fence_list); |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 2554 | return 0; |
| 2555 | } |
| 2556 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2557 | switch (obj_priv->tiling_mode) { |
| 2558 | case I915_TILING_NONE: |
| 2559 | WARN(1, "allocating a fence for non-tiled object?\n"); |
| 2560 | break; |
| 2561 | case I915_TILING_X: |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2562 | if (!obj_priv->stride) |
| 2563 | return -EINVAL; |
| 2564 | WARN((obj_priv->stride & (512 - 1)), |
| 2565 | "object 0x%08x is X tiled but has non-512B pitch\n", |
| 2566 | obj_priv->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2567 | break; |
| 2568 | case I915_TILING_Y: |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2569 | if (!obj_priv->stride) |
| 2570 | return -EINVAL; |
| 2571 | WARN((obj_priv->stride & (128 - 1)), |
| 2572 | "object 0x%08x is Y tiled but has non-128B pitch\n", |
| 2573 | obj_priv->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2574 | break; |
| 2575 | } |
| 2576 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2577 | ret = i915_find_fence_reg(dev, interruptible); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2578 | if (ret < 0) |
| 2579 | return ret; |
Chris Wilson | fc7170b | 2009-02-11 14:26:46 +0000 | [diff] [blame] | 2580 | |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2581 | obj_priv->fence_reg = ret; |
| 2582 | reg = &dev_priv->fence_regs[obj_priv->fence_reg]; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2583 | list_add_tail(®->lru_list, &dev_priv->mm.fence_list); |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 2584 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2585 | reg->obj = obj; |
| 2586 | |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2587 | switch (INTEL_INFO(dev)->gen) { |
| 2588 | case 6: |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2589 | sandybridge_write_fence_reg(obj); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2590 | break; |
| 2591 | case 5: |
| 2592 | case 4: |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2593 | i965_write_fence_reg(obj); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2594 | break; |
| 2595 | case 3: |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2596 | i915_write_fence_reg(obj); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2597 | break; |
| 2598 | case 2: |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2599 | i830_write_fence_reg(obj); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2600 | break; |
| 2601 | } |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 2602 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2603 | trace_i915_gem_object_get_fence(obj, |
| 2604 | obj_priv->fence_reg, |
| 2605 | obj_priv->tiling_mode); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2606 | |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 2607 | return 0; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | /** |
| 2611 | * i915_gem_clear_fence_reg - clear out fence register info |
| 2612 | * @obj: object to clear |
| 2613 | * |
| 2614 | * Zeroes out the fence register itself and clears out the associated |
| 2615 | * data structures in dev_priv and obj_priv. |
| 2616 | */ |
| 2617 | static void |
| 2618 | i915_gem_clear_fence_reg(struct drm_gem_object *obj) |
| 2619 | { |
| 2620 | struct drm_device *dev = obj->dev; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2621 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2622 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2623 | struct drm_i915_fence_reg *reg = |
| 2624 | &dev_priv->fence_regs[obj_priv->fence_reg]; |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2625 | uint32_t fence_reg; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2626 | |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2627 | switch (INTEL_INFO(dev)->gen) { |
| 2628 | case 6: |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2629 | I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + |
| 2630 | (obj_priv->fence_reg * 8), 0); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2631 | break; |
| 2632 | case 5: |
| 2633 | case 4: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2634 | I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2635 | break; |
| 2636 | case 3: |
Chris Wilson | 9b74f73 | 2010-09-22 19:10:44 +0100 | [diff] [blame] | 2637 | if (obj_priv->fence_reg >= 8) |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2638 | fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2639 | else |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2640 | case 2: |
| 2641 | fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2642 | |
| 2643 | I915_WRITE(fence_reg, 0); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2644 | break; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2645 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2646 | |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2647 | reg->obj = NULL; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2648 | obj_priv->fence_reg = I915_FENCE_REG_NONE; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2649 | list_del_init(®->lru_list); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2650 | } |
| 2651 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2652 | /** |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2653 | * i915_gem_object_put_fence_reg - waits on outstanding fenced access |
| 2654 | * to the buffer to finish, and then resets the fence register. |
| 2655 | * @obj: tiled object holding a fence register. |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2656 | * @bool: whether the wait upon the fence is interruptible |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2657 | * |
| 2658 | * Zeroes out the fence register itself and clears out the associated |
| 2659 | * data structures in dev_priv and obj_priv. |
| 2660 | */ |
| 2661 | int |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2662 | i915_gem_object_put_fence_reg(struct drm_gem_object *obj, |
| 2663 | bool interruptible) |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2664 | { |
| 2665 | struct drm_device *dev = obj->dev; |
Chris Wilson | 53640e1 | 2010-09-20 11:40:50 +0100 | [diff] [blame] | 2666 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2667 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 53640e1 | 2010-09-20 11:40:50 +0100 | [diff] [blame] | 2668 | struct drm_i915_fence_reg *reg; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2669 | |
| 2670 | if (obj_priv->fence_reg == I915_FENCE_REG_NONE) |
| 2671 | return 0; |
| 2672 | |
Daniel Vetter | 10ae9bd | 2010-02-01 13:59:17 +0100 | [diff] [blame] | 2673 | /* If we've changed tiling, GTT-mappings of the object |
| 2674 | * need to re-fault to ensure that the correct fence register |
| 2675 | * setup is in place. |
| 2676 | */ |
| 2677 | i915_gem_release_mmap(obj); |
| 2678 | |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2679 | /* On the i915, GPU access to tiled buffers is via a fence, |
| 2680 | * therefore we must wait for any outstanding access to complete |
| 2681 | * before clearing the fence. |
| 2682 | */ |
Chris Wilson | 53640e1 | 2010-09-20 11:40:50 +0100 | [diff] [blame] | 2683 | reg = &dev_priv->fence_regs[obj_priv->fence_reg]; |
| 2684 | if (reg->gpu) { |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2685 | int ret; |
| 2686 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2687 | ret = i915_gem_object_flush_gpu_write_domain(obj, true); |
Chris Wilson | 0bc23aa | 2010-09-14 10:22:23 +0100 | [diff] [blame] | 2688 | if (ret) |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2689 | return ret; |
| 2690 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2691 | ret = i915_gem_object_wait_rendering(obj, interruptible); |
Chris Wilson | 0bc23aa | 2010-09-14 10:22:23 +0100 | [diff] [blame] | 2692 | if (ret) |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2693 | return ret; |
Chris Wilson | 53640e1 | 2010-09-20 11:40:50 +0100 | [diff] [blame] | 2694 | |
| 2695 | reg->gpu = false; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2696 | } |
| 2697 | |
Daniel Vetter | 4a72661 | 2010-02-01 13:59:16 +0100 | [diff] [blame] | 2698 | i915_gem_object_flush_gtt_write_domain(obj); |
Chris Wilson | 0bc23aa | 2010-09-14 10:22:23 +0100 | [diff] [blame] | 2699 | i915_gem_clear_fence_reg(obj); |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2700 | |
| 2701 | return 0; |
| 2702 | } |
| 2703 | |
| 2704 | /** |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2705 | * Finds free space in the GTT aperture and binds the object there. |
| 2706 | */ |
| 2707 | static int |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2708 | i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, |
| 2709 | unsigned alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2710 | bool map_and_fenceable) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2711 | { |
| 2712 | struct drm_device *dev = obj->dev; |
| 2713 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2714 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2715 | struct drm_mm_node *free_space; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2716 | gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN; |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 2717 | u32 size, fence_size, fence_alignment, unfenced_alignment; |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2718 | bool mappable, fenceable; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2719 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2720 | |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 2721 | if (obj_priv->madv != I915_MADV_WILLNEED) { |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2722 | DRM_ERROR("Attempting to bind a purgeable object\n"); |
| 2723 | return -EINVAL; |
| 2724 | } |
| 2725 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2726 | fence_size = i915_gem_get_gtt_size(obj_priv); |
| 2727 | fence_alignment = i915_gem_get_gtt_alignment(obj_priv); |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 2728 | unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj_priv); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2729 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2730 | if (alignment == 0) |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 2731 | alignment = map_and_fenceable ? fence_alignment : |
| 2732 | unfenced_alignment; |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2733 | if (map_and_fenceable && alignment & (fence_alignment - 1)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2734 | DRM_ERROR("Invalid object alignment requested %u\n", alignment); |
| 2735 | return -EINVAL; |
| 2736 | } |
| 2737 | |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2738 | size = map_and_fenceable ? fence_size : obj->size; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2739 | |
Chris Wilson | 654fc60 | 2010-05-27 13:18:21 +0100 | [diff] [blame] | 2740 | /* If the object is bigger than the entire aperture, reject it early |
| 2741 | * before evicting everything in a vain attempt to find space. |
| 2742 | */ |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2743 | if (obj->size > |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2744 | (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) { |
Chris Wilson | 654fc60 | 2010-05-27 13:18:21 +0100 | [diff] [blame] | 2745 | DRM_ERROR("Attempting to bind an object larger than the aperture\n"); |
| 2746 | return -E2BIG; |
| 2747 | } |
| 2748 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2749 | search_free: |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2750 | if (map_and_fenceable) |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2751 | free_space = |
| 2752 | drm_mm_search_free_in_range(&dev_priv->mm.gtt_space, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2753 | size, alignment, 0, |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2754 | dev_priv->mm.gtt_mappable_end, |
| 2755 | 0); |
| 2756 | else |
| 2757 | free_space = drm_mm_search_free(&dev_priv->mm.gtt_space, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2758 | size, alignment, 0); |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2759 | |
| 2760 | if (free_space != NULL) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2761 | if (map_and_fenceable) |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2762 | obj_priv->gtt_space = |
| 2763 | drm_mm_get_block_range_generic(free_space, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2764 | size, alignment, 0, |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2765 | dev_priv->mm.gtt_mappable_end, |
| 2766 | 0); |
| 2767 | else |
| 2768 | obj_priv->gtt_space = |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2769 | drm_mm_get_block(free_space, size, alignment); |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2770 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2771 | if (obj_priv->gtt_space == NULL) { |
| 2772 | /* If the gtt is empty and we're still having trouble |
| 2773 | * fitting our object in, we're out of memory. |
| 2774 | */ |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2775 | ret = i915_gem_evict_something(dev, size, alignment, |
| 2776 | map_and_fenceable); |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 2777 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2778 | return ret; |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 2779 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2780 | goto search_free; |
| 2781 | } |
| 2782 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2783 | ret = i915_gem_object_get_pages_gtt(obj, gfpmask); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2784 | if (ret) { |
| 2785 | drm_mm_put_block(obj_priv->gtt_space); |
| 2786 | obj_priv->gtt_space = NULL; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2787 | |
| 2788 | if (ret == -ENOMEM) { |
| 2789 | /* first try to clear up some space from the GTT */ |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2790 | ret = i915_gem_evict_something(dev, size, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2791 | alignment, |
| 2792 | map_and_fenceable); |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2793 | if (ret) { |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2794 | /* now try to shrink everyone else */ |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 2795 | if (gfpmask) { |
| 2796 | gfpmask = 0; |
| 2797 | goto search_free; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2798 | } |
| 2799 | |
| 2800 | return ret; |
| 2801 | } |
| 2802 | |
| 2803 | goto search_free; |
| 2804 | } |
| 2805 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2806 | return ret; |
| 2807 | } |
| 2808 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2809 | /* Create an AGP memory structure pointing at our pages, and bind it |
| 2810 | * into the GTT. |
| 2811 | */ |
| 2812 | obj_priv->agp_mem = drm_agp_bind_pages(dev, |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2813 | obj_priv->pages, |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2814 | obj->size >> PAGE_SHIFT, |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 2815 | obj_priv->gtt_space->start, |
Keith Packard | ba1eb1d | 2008-10-14 19:55:10 -0700 | [diff] [blame] | 2816 | obj_priv->agp_type); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2817 | if (obj_priv->agp_mem == NULL) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2818 | i915_gem_object_put_pages_gtt(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2819 | drm_mm_put_block(obj_priv->gtt_space); |
| 2820 | obj_priv->gtt_space = NULL; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2821 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2822 | ret = i915_gem_evict_something(dev, size, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2823 | alignment, map_and_fenceable); |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 2824 | if (ret) |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2825 | return ret; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2826 | |
| 2827 | goto search_free; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2828 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2829 | |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 2830 | obj_priv->gtt_offset = obj_priv->gtt_space->start; |
| 2831 | |
Chris Wilson | bf1a109 | 2010-08-07 11:01:20 +0100 | [diff] [blame] | 2832 | /* keep track of bounds object by adding it to the inactive list */ |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 2833 | list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2834 | i915_gem_info_add_gtt(dev_priv, obj_priv); |
Chris Wilson | bf1a109 | 2010-08-07 11:01:20 +0100 | [diff] [blame] | 2835 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2836 | /* Assert that the object is not currently in any GPU domain. As it |
| 2837 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2838 | * a GPU cache |
| 2839 | */ |
Chris Wilson | 21d509e | 2009-06-06 09:46:02 +0100 | [diff] [blame] | 2840 | BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2841 | BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2842 | |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2843 | trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, map_and_fenceable); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2844 | |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2845 | fenceable = |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2846 | obj_priv->gtt_space->size == fence_size && |
| 2847 | (obj_priv->gtt_space->start & (fence_alignment -1)) == 0; |
| 2848 | |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2849 | mappable = |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2850 | obj_priv->gtt_offset + obj->size <= dev_priv->mm.gtt_mappable_end; |
| 2851 | |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2852 | obj_priv->map_and_fenceable = mappable && fenceable; |
| 2853 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2854 | return 0; |
| 2855 | } |
| 2856 | |
| 2857 | void |
| 2858 | i915_gem_clflush_object(struct drm_gem_object *obj) |
| 2859 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2860 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2861 | |
| 2862 | /* If we don't have a page list set up, then we're not pinned |
| 2863 | * to GPU, and we can ignore the cache flush because it'll happen |
| 2864 | * again at bind time. |
| 2865 | */ |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2866 | if (obj_priv->pages == NULL) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2867 | return; |
| 2868 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2869 | trace_i915_gem_object_clflush(obj); |
Eric Anholt | cfa16a0 | 2009-05-26 18:46:16 -0700 | [diff] [blame] | 2870 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2871 | drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2872 | } |
| 2873 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2874 | /** Flushes any GPU write domain for the object if it's dirty. */ |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2875 | static int |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2876 | i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj, |
| 2877 | bool pipelined) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2878 | { |
| 2879 | struct drm_device *dev = obj->dev; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2880 | |
| 2881 | if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0) |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2882 | return 0; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2883 | |
| 2884 | /* Queue the GPU write cache flushing we need. */ |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 2885 | i915_gem_flush_ring(dev, NULL, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2886 | to_intel_bo(obj)->ring, |
| 2887 | 0, obj->write_domain); |
Chris Wilson | 48b956c | 2010-09-14 12:50:34 +0100 | [diff] [blame] | 2888 | BUG_ON(obj->write_domain); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2889 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2890 | if (pipelined) |
| 2891 | return 0; |
| 2892 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2893 | return i915_gem_object_wait_rendering(obj, true); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | /** Flushes the GTT write domain for the object if it's dirty. */ |
| 2897 | static void |
| 2898 | i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj) |
| 2899 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2900 | uint32_t old_write_domain; |
| 2901 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2902 | if (obj->write_domain != I915_GEM_DOMAIN_GTT) |
| 2903 | return; |
| 2904 | |
| 2905 | /* No actual flushing is required for the GTT write domain. Writes |
| 2906 | * to it immediately go to main memory as far as we know, so there's |
| 2907 | * no chipset flush. It also doesn't land in render cache. |
| 2908 | */ |
Chris Wilson | 4a684a4 | 2010-10-28 14:44:08 +0100 | [diff] [blame] | 2909 | i915_gem_release_mmap(obj); |
| 2910 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2911 | old_write_domain = obj->write_domain; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2912 | obj->write_domain = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2913 | |
| 2914 | trace_i915_gem_object_change_domain(obj, |
| 2915 | obj->read_domains, |
| 2916 | old_write_domain); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | /** Flushes the CPU write domain for the object if it's dirty. */ |
| 2920 | static void |
| 2921 | i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj) |
| 2922 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2923 | uint32_t old_write_domain; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2924 | |
| 2925 | if (obj->write_domain != I915_GEM_DOMAIN_CPU) |
| 2926 | return; |
| 2927 | |
| 2928 | i915_gem_clflush_object(obj); |
Daniel Vetter | 40ce657 | 2010-11-05 18:12:18 +0100 | [diff] [blame^] | 2929 | intel_gtt_chipset_flush(); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2930 | old_write_domain = obj->write_domain; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2931 | obj->write_domain = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2932 | |
| 2933 | trace_i915_gem_object_change_domain(obj, |
| 2934 | obj->read_domains, |
| 2935 | old_write_domain); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2936 | } |
| 2937 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2938 | /** |
| 2939 | * Moves a single object to the GTT read, and possibly write domain. |
| 2940 | * |
| 2941 | * This function returns when the move is complete, including waiting on |
| 2942 | * flushes to occur. |
| 2943 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2944 | int |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2945 | i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write) |
| 2946 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2947 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2948 | uint32_t old_write_domain, old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2949 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2950 | |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 2951 | /* Not valid to be called on unbound objects. */ |
| 2952 | if (obj_priv->gtt_space == NULL) |
| 2953 | return -EINVAL; |
| 2954 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2955 | ret = i915_gem_object_flush_gpu_write_domain(obj, false); |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2956 | if (ret != 0) |
| 2957 | return ret; |
| 2958 | |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 2959 | i915_gem_object_flush_cpu_write_domain(obj); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2960 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2961 | if (write) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2962 | ret = i915_gem_object_wait_rendering(obj, true); |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2963 | if (ret) |
| 2964 | return ret; |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2965 | } |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2966 | |
| 2967 | old_write_domain = obj->write_domain; |
| 2968 | old_read_domains = obj->read_domains; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2969 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2970 | /* It should now be out of any other write domains, and we can update |
| 2971 | * the domain values for our changes. |
| 2972 | */ |
| 2973 | BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 2974 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2975 | if (write) { |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 2976 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2977 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2978 | obj_priv->dirty = 1; |
| 2979 | } |
| 2980 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2981 | trace_i915_gem_object_change_domain(obj, |
| 2982 | old_read_domains, |
| 2983 | old_write_domain); |
| 2984 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2985 | return 0; |
| 2986 | } |
| 2987 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2988 | /* |
| 2989 | * Prepare buffer for display plane. Use uninterruptible for possible flush |
| 2990 | * wait, as in modesetting process we're not supposed to be interrupted. |
| 2991 | */ |
| 2992 | int |
Chris Wilson | 48b956c | 2010-09-14 12:50:34 +0100 | [diff] [blame] | 2993 | i915_gem_object_set_to_display_plane(struct drm_gem_object *obj, |
| 2994 | bool pipelined) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2995 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2996 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2997 | uint32_t old_read_domains; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2998 | int ret; |
| 2999 | |
| 3000 | /* Not valid to be called on unbound objects. */ |
| 3001 | if (obj_priv->gtt_space == NULL) |
| 3002 | return -EINVAL; |
| 3003 | |
Chris Wilson | ced270f | 2010-09-26 22:47:46 +0100 | [diff] [blame] | 3004 | ret = i915_gem_object_flush_gpu_write_domain(obj, true); |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 3005 | if (ret) |
| 3006 | return ret; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3007 | |
Chris Wilson | ced270f | 2010-09-26 22:47:46 +0100 | [diff] [blame] | 3008 | /* Currently, we are always called from an non-interruptible context. */ |
| 3009 | if (!pipelined) { |
| 3010 | ret = i915_gem_object_wait_rendering(obj, false); |
| 3011 | if (ret) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3012 | return ret; |
| 3013 | } |
| 3014 | |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3015 | i915_gem_object_flush_cpu_write_domain(obj); |
| 3016 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3017 | old_read_domains = obj->read_domains; |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 3018 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3019 | |
| 3020 | trace_i915_gem_object_change_domain(obj, |
| 3021 | old_read_domains, |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 3022 | obj->write_domain); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3023 | |
| 3024 | return 0; |
| 3025 | } |
| 3026 | |
Chris Wilson | 8534551 | 2010-11-13 09:49:11 +0000 | [diff] [blame] | 3027 | int |
| 3028 | i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj, |
| 3029 | bool interruptible) |
| 3030 | { |
| 3031 | if (!obj->active) |
| 3032 | return 0; |
| 3033 | |
| 3034 | if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) |
| 3035 | i915_gem_flush_ring(obj->base.dev, NULL, obj->ring, |
| 3036 | 0, obj->base.write_domain); |
| 3037 | |
| 3038 | return i915_gem_object_wait_rendering(&obj->base, interruptible); |
| 3039 | } |
| 3040 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3041 | /** |
| 3042 | * Moves a single object to the CPU read, and possibly write domain. |
| 3043 | * |
| 3044 | * This function returns when the move is complete, including waiting on |
| 3045 | * flushes to occur. |
| 3046 | */ |
| 3047 | static int |
| 3048 | i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write) |
| 3049 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3050 | uint32_t old_write_domain, old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3051 | int ret; |
| 3052 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 3053 | ret = i915_gem_object_flush_gpu_write_domain(obj, false); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3054 | if (ret != 0) |
| 3055 | return ret; |
| 3056 | |
| 3057 | i915_gem_object_flush_gtt_write_domain(obj); |
| 3058 | |
| 3059 | /* If we have a partially-valid cache of the object in the CPU, |
| 3060 | * finish invalidating it and free the per-page flags. |
| 3061 | */ |
| 3062 | i915_gem_object_set_to_full_cpu_read_domain(obj); |
| 3063 | |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 3064 | if (write) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 3065 | ret = i915_gem_object_wait_rendering(obj, true); |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 3066 | if (ret) |
| 3067 | return ret; |
| 3068 | } |
| 3069 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3070 | old_write_domain = obj->write_domain; |
| 3071 | old_read_domains = obj->read_domains; |
| 3072 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3073 | /* Flush the CPU cache if it's still invalid. */ |
| 3074 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
| 3075 | i915_gem_clflush_object(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3076 | |
| 3077 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
| 3078 | } |
| 3079 | |
| 3080 | /* It should now be out of any other write domains, and we can update |
| 3081 | * the domain values for our changes. |
| 3082 | */ |
| 3083 | BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
| 3084 | |
| 3085 | /* If we're writing through the CPU, then the GPU read domains will |
| 3086 | * need to be invalidated at next use. |
| 3087 | */ |
| 3088 | if (write) { |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 3089 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3090 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 3091 | } |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3092 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3093 | trace_i915_gem_object_change_domain(obj, |
| 3094 | old_read_domains, |
| 3095 | old_write_domain); |
| 3096 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3097 | return 0; |
| 3098 | } |
| 3099 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3100 | /* |
| 3101 | * Set the next domain for the specified object. This |
| 3102 | * may not actually perform the necessary flushing/invaliding though, |
| 3103 | * as that may want to be batched with other set_domain operations |
| 3104 | * |
| 3105 | * This is (we hope) the only really tricky part of gem. The goal |
| 3106 | * is fairly simple -- track which caches hold bits of the object |
| 3107 | * and make sure they remain coherent. A few concrete examples may |
| 3108 | * help to explain how it works. For shorthand, we use the notation |
| 3109 | * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the |
| 3110 | * a pair of read and write domain masks. |
| 3111 | * |
| 3112 | * Case 1: the batch buffer |
| 3113 | * |
| 3114 | * 1. Allocated |
| 3115 | * 2. Written by CPU |
| 3116 | * 3. Mapped to GTT |
| 3117 | * 4. Read by GPU |
| 3118 | * 5. Unmapped from GTT |
| 3119 | * 6. Freed |
| 3120 | * |
| 3121 | * Let's take these a step at a time |
| 3122 | * |
| 3123 | * 1. Allocated |
| 3124 | * Pages allocated from the kernel may still have |
| 3125 | * cache contents, so we set them to (CPU, CPU) always. |
| 3126 | * 2. Written by CPU (using pwrite) |
| 3127 | * The pwrite function calls set_domain (CPU, CPU) and |
| 3128 | * this function does nothing (as nothing changes) |
| 3129 | * 3. Mapped by GTT |
| 3130 | * This function asserts that the object is not |
| 3131 | * currently in any GPU-based read or write domains |
| 3132 | * 4. Read by GPU |
| 3133 | * i915_gem_execbuffer calls set_domain (COMMAND, 0). |
| 3134 | * As write_domain is zero, this function adds in the |
| 3135 | * current read domains (CPU+COMMAND, 0). |
| 3136 | * flush_domains is set to CPU. |
| 3137 | * invalidate_domains is set to COMMAND |
| 3138 | * clflush is run to get data out of the CPU caches |
| 3139 | * then i915_dev_set_domain calls i915_gem_flush to |
| 3140 | * emit an MI_FLUSH and drm_agp_chipset_flush |
| 3141 | * 5. Unmapped from GTT |
| 3142 | * i915_gem_object_unbind calls set_domain (CPU, CPU) |
| 3143 | * flush_domains and invalidate_domains end up both zero |
| 3144 | * so no flushing/invalidating happens |
| 3145 | * 6. Freed |
| 3146 | * yay, done |
| 3147 | * |
| 3148 | * Case 2: The shared render buffer |
| 3149 | * |
| 3150 | * 1. Allocated |
| 3151 | * 2. Mapped to GTT |
| 3152 | * 3. Read/written by GPU |
| 3153 | * 4. set_domain to (CPU,CPU) |
| 3154 | * 5. Read/written by CPU |
| 3155 | * 6. Read/written by GPU |
| 3156 | * |
| 3157 | * 1. Allocated |
| 3158 | * Same as last example, (CPU, CPU) |
| 3159 | * 2. Mapped to GTT |
| 3160 | * Nothing changes (assertions find that it is not in the GPU) |
| 3161 | * 3. Read/written by GPU |
| 3162 | * execbuffer calls set_domain (RENDER, RENDER) |
| 3163 | * flush_domains gets CPU |
| 3164 | * invalidate_domains gets GPU |
| 3165 | * clflush (obj) |
| 3166 | * MI_FLUSH and drm_agp_chipset_flush |
| 3167 | * 4. set_domain (CPU, CPU) |
| 3168 | * flush_domains gets GPU |
| 3169 | * invalidate_domains gets CPU |
| 3170 | * wait_rendering (obj) to make sure all drawing is complete. |
| 3171 | * This will include an MI_FLUSH to get the data from GPU |
| 3172 | * to memory |
| 3173 | * clflush (obj) to invalidate the CPU cache |
| 3174 | * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?) |
| 3175 | * 5. Read/written by CPU |
| 3176 | * cache lines are loaded and dirtied |
| 3177 | * 6. Read written by GPU |
| 3178 | * Same as last GPU access |
| 3179 | * |
| 3180 | * Case 3: The constant buffer |
| 3181 | * |
| 3182 | * 1. Allocated |
| 3183 | * 2. Written by CPU |
| 3184 | * 3. Read by GPU |
| 3185 | * 4. Updated (written) by CPU again |
| 3186 | * 5. Read by GPU |
| 3187 | * |
| 3188 | * 1. Allocated |
| 3189 | * (CPU, CPU) |
| 3190 | * 2. Written by CPU |
| 3191 | * (CPU, CPU) |
| 3192 | * 3. Read by GPU |
| 3193 | * (CPU+RENDER, 0) |
| 3194 | * flush_domains = CPU |
| 3195 | * invalidate_domains = RENDER |
| 3196 | * clflush (obj) |
| 3197 | * MI_FLUSH |
| 3198 | * drm_agp_chipset_flush |
| 3199 | * 4. Updated (written) by CPU again |
| 3200 | * (CPU, CPU) |
| 3201 | * flush_domains = 0 (no previous write domain) |
| 3202 | * invalidate_domains = 0 (no new read domains) |
| 3203 | * 5. Read by GPU |
| 3204 | * (CPU+RENDER, 0) |
| 3205 | * flush_domains = CPU |
| 3206 | * invalidate_domains = RENDER |
| 3207 | * clflush (obj) |
| 3208 | * MI_FLUSH |
| 3209 | * drm_agp_chipset_flush |
| 3210 | */ |
Keith Packard | c0d9082 | 2008-11-20 23:11:08 -0800 | [diff] [blame] | 3211 | static void |
Chris Wilson | b665145 | 2010-10-23 10:15:06 +0100 | [diff] [blame] | 3212 | i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj, |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3213 | struct intel_ring_buffer *ring, |
| 3214 | struct change_domains *cd) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3215 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 3216 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3217 | uint32_t invalidate_domains = 0; |
| 3218 | uint32_t flush_domains = 0; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 3219 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3220 | /* |
| 3221 | * If the object isn't moving to a new write domain, |
| 3222 | * let the object stay in multiple read domains |
| 3223 | */ |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 3224 | if (obj->pending_write_domain == 0) |
| 3225 | obj->pending_read_domains |= obj->read_domains; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3226 | |
| 3227 | /* |
| 3228 | * Flush the current write domain if |
| 3229 | * the new read domains don't match. Invalidate |
| 3230 | * any read domains which differ from the old |
| 3231 | * write domain |
| 3232 | */ |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 3233 | if (obj->write_domain && |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3234 | (obj->write_domain != obj->pending_read_domains || |
| 3235 | obj_priv->ring != ring)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3236 | flush_domains |= obj->write_domain; |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 3237 | invalidate_domains |= |
| 3238 | obj->pending_read_domains & ~obj->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3239 | } |
| 3240 | /* |
| 3241 | * Invalidate any read caches which may have |
| 3242 | * stale data. That is, any new read domains. |
| 3243 | */ |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 3244 | invalidate_domains |= obj->pending_read_domains & ~obj->read_domains; |
Chris Wilson | 3d2a812 | 2010-09-29 11:39:53 +0100 | [diff] [blame] | 3245 | if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3246 | i915_gem_clflush_object(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3247 | |
Chris Wilson | 4a684a4 | 2010-10-28 14:44:08 +0100 | [diff] [blame] | 3248 | /* blow away mappings if mapped through GTT */ |
| 3249 | if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT) |
| 3250 | i915_gem_release_mmap(obj); |
| 3251 | |
Eric Anholt | efbeed9 | 2009-02-19 14:54:51 -0800 | [diff] [blame] | 3252 | /* The actual obj->write_domain will be updated with |
| 3253 | * pending_write_domain after we emit the accumulated flush for all |
| 3254 | * of our domain changes in execbuffers (which clears objects' |
| 3255 | * write_domains). So if we have a current write domain that we |
| 3256 | * aren't changing, set pending_write_domain to that. |
| 3257 | */ |
| 3258 | if (flush_domains == 0 && obj->pending_write_domain == 0) |
| 3259 | obj->pending_write_domain = obj->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3260 | |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3261 | cd->invalidate_domains |= invalidate_domains; |
| 3262 | cd->flush_domains |= flush_domains; |
Chris Wilson | b665145 | 2010-10-23 10:15:06 +0100 | [diff] [blame] | 3263 | if (flush_domains & I915_GEM_GPU_DOMAINS) |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3264 | cd->flush_rings |= obj_priv->ring->id; |
Chris Wilson | b665145 | 2010-10-23 10:15:06 +0100 | [diff] [blame] | 3265 | if (invalidate_domains & I915_GEM_GPU_DOMAINS) |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3266 | cd->flush_rings |= ring->id; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3267 | } |
| 3268 | |
| 3269 | /** |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3270 | * Moves the object from a partially CPU read to a full one. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3271 | * |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3272 | * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(), |
| 3273 | * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU). |
| 3274 | */ |
| 3275 | static void |
| 3276 | i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj) |
| 3277 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 3278 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3279 | |
| 3280 | if (!obj_priv->page_cpu_valid) |
| 3281 | return; |
| 3282 | |
| 3283 | /* If we're partially in the CPU read domain, finish moving it in. |
| 3284 | */ |
| 3285 | if (obj->read_domains & I915_GEM_DOMAIN_CPU) { |
| 3286 | int i; |
| 3287 | |
| 3288 | for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) { |
| 3289 | if (obj_priv->page_cpu_valid[i]) |
| 3290 | continue; |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 3291 | drm_clflush_pages(obj_priv->pages + i, 1); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3292 | } |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | /* Free the page_cpu_valid mappings which are now stale, whether |
| 3296 | * or not we've got I915_GEM_DOMAIN_CPU. |
| 3297 | */ |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 3298 | kfree(obj_priv->page_cpu_valid); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3299 | obj_priv->page_cpu_valid = NULL; |
| 3300 | } |
| 3301 | |
| 3302 | /** |
| 3303 | * Set the CPU read domain on a range of the object. |
| 3304 | * |
| 3305 | * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's |
| 3306 | * not entirely valid. The page_cpu_valid member of the object flags which |
| 3307 | * pages have been flushed, and will be respected by |
| 3308 | * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping |
| 3309 | * of the whole object. |
| 3310 | * |
| 3311 | * This function returns when the move is complete, including waiting on |
| 3312 | * flushes to occur. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3313 | */ |
| 3314 | static int |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3315 | i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj, |
| 3316 | uint64_t offset, uint64_t size) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3317 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 3318 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3319 | uint32_t old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3320 | int i, ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3321 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3322 | if (offset == 0 && size == obj->size) |
| 3323 | return i915_gem_object_set_to_cpu_domain(obj, 0); |
| 3324 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 3325 | ret = i915_gem_object_flush_gpu_write_domain(obj, false); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3326 | if (ret != 0) |
| 3327 | return ret; |
| 3328 | i915_gem_object_flush_gtt_write_domain(obj); |
| 3329 | |
| 3330 | /* If we're already fully in the CPU read domain, we're done. */ |
| 3331 | if (obj_priv->page_cpu_valid == NULL && |
| 3332 | (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3333 | return 0; |
| 3334 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3335 | /* Otherwise, create/clear the per-page CPU read domain flag if we're |
| 3336 | * newly adding I915_GEM_DOMAIN_CPU |
| 3337 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3338 | if (obj_priv->page_cpu_valid == NULL) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 3339 | obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE, |
| 3340 | GFP_KERNEL); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3341 | if (obj_priv->page_cpu_valid == NULL) |
| 3342 | return -ENOMEM; |
| 3343 | } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) |
| 3344 | memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3345 | |
| 3346 | /* Flush the cache on any pages that are still invalid from the CPU's |
| 3347 | * perspective. |
| 3348 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3349 | for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE; |
| 3350 | i++) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3351 | if (obj_priv->page_cpu_valid[i]) |
| 3352 | continue; |
| 3353 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 3354 | drm_clflush_pages(obj_priv->pages + i, 1); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3355 | |
| 3356 | obj_priv->page_cpu_valid[i] = 1; |
| 3357 | } |
| 3358 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3359 | /* It should now be out of any other write domains, and we can update |
| 3360 | * the domain values for our changes. |
| 3361 | */ |
| 3362 | BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
| 3363 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3364 | old_read_domains = obj->read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3365 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
| 3366 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3367 | trace_i915_gem_object_change_domain(obj, |
| 3368 | old_read_domains, |
| 3369 | obj->write_domain); |
| 3370 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3371 | return 0; |
| 3372 | } |
| 3373 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3374 | static int |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3375 | i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj, |
| 3376 | struct drm_file *file_priv, |
| 3377 | struct drm_i915_gem_exec_object2 *entry, |
| 3378 | struct drm_i915_gem_relocation_entry *reloc) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3379 | { |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3380 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3381 | struct drm_gem_object *target_obj; |
| 3382 | uint32_t target_offset; |
| 3383 | int ret = -EINVAL; |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3384 | |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3385 | target_obj = drm_gem_object_lookup(dev, file_priv, |
| 3386 | reloc->target_handle); |
| 3387 | if (target_obj == NULL) |
| 3388 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3389 | |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3390 | target_offset = to_intel_bo(target_obj)->gtt_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3391 | |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3392 | #if WATCH_RELOC |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3393 | DRM_INFO("%s: obj %p offset %08x target %d " |
| 3394 | "read %08x write %08x gtt %08x " |
| 3395 | "presumed %08x delta %08x\n", |
| 3396 | __func__, |
| 3397 | obj, |
| 3398 | (int) reloc->offset, |
| 3399 | (int) reloc->target_handle, |
| 3400 | (int) reloc->read_domains, |
| 3401 | (int) reloc->write_domain, |
| 3402 | (int) target_offset, |
| 3403 | (int) reloc->presumed_offset, |
| 3404 | reloc->delta); |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3405 | #endif |
| 3406 | |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3407 | /* The target buffer should have appeared before us in the |
| 3408 | * exec_object list, so it should have a GTT space bound by now. |
| 3409 | */ |
| 3410 | if (target_offset == 0) { |
| 3411 | DRM_ERROR("No GTT space found for object %d\n", |
| 3412 | reloc->target_handle); |
| 3413 | goto err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3414 | } |
| 3415 | |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3416 | /* Validate that the target is in a valid r/w GPU domain */ |
| 3417 | if (reloc->write_domain & (reloc->write_domain - 1)) { |
| 3418 | DRM_ERROR("reloc with multiple write domains: " |
| 3419 | "obj %p target %d offset %d " |
| 3420 | "read %08x write %08x", |
| 3421 | obj, reloc->target_handle, |
| 3422 | (int) reloc->offset, |
| 3423 | reloc->read_domains, |
| 3424 | reloc->write_domain); |
| 3425 | goto err; |
| 3426 | } |
| 3427 | if (reloc->write_domain & I915_GEM_DOMAIN_CPU || |
| 3428 | reloc->read_domains & I915_GEM_DOMAIN_CPU) { |
| 3429 | DRM_ERROR("reloc with read/write CPU domains: " |
| 3430 | "obj %p target %d offset %d " |
| 3431 | "read %08x write %08x", |
| 3432 | obj, reloc->target_handle, |
| 3433 | (int) reloc->offset, |
| 3434 | reloc->read_domains, |
| 3435 | reloc->write_domain); |
| 3436 | goto err; |
| 3437 | } |
| 3438 | if (reloc->write_domain && target_obj->pending_write_domain && |
| 3439 | reloc->write_domain != target_obj->pending_write_domain) { |
| 3440 | DRM_ERROR("Write domain conflict: " |
| 3441 | "obj %p target %d offset %d " |
| 3442 | "new %08x old %08x\n", |
| 3443 | obj, reloc->target_handle, |
| 3444 | (int) reloc->offset, |
| 3445 | reloc->write_domain, |
| 3446 | target_obj->pending_write_domain); |
| 3447 | goto err; |
| 3448 | } |
| 3449 | |
| 3450 | target_obj->pending_read_domains |= reloc->read_domains; |
| 3451 | target_obj->pending_write_domain |= reloc->write_domain; |
| 3452 | |
| 3453 | /* If the relocation already has the right value in it, no |
| 3454 | * more work needs to be done. |
| 3455 | */ |
| 3456 | if (target_offset == reloc->presumed_offset) |
| 3457 | goto out; |
| 3458 | |
| 3459 | /* Check that the relocation address is valid... */ |
| 3460 | if (reloc->offset > obj->base.size - 4) { |
| 3461 | DRM_ERROR("Relocation beyond object bounds: " |
| 3462 | "obj %p target %d offset %d size %d.\n", |
| 3463 | obj, reloc->target_handle, |
| 3464 | (int) reloc->offset, |
| 3465 | (int) obj->base.size); |
| 3466 | goto err; |
| 3467 | } |
| 3468 | if (reloc->offset & 3) { |
| 3469 | DRM_ERROR("Relocation not 4-byte aligned: " |
| 3470 | "obj %p target %d offset %d.\n", |
| 3471 | obj, reloc->target_handle, |
| 3472 | (int) reloc->offset); |
| 3473 | goto err; |
| 3474 | } |
| 3475 | |
| 3476 | /* and points to somewhere within the target object. */ |
| 3477 | if (reloc->delta >= target_obj->size) { |
| 3478 | DRM_ERROR("Relocation beyond target object bounds: " |
| 3479 | "obj %p target %d delta %d size %d.\n", |
| 3480 | obj, reloc->target_handle, |
| 3481 | (int) reloc->delta, |
| 3482 | (int) target_obj->size); |
| 3483 | goto err; |
| 3484 | } |
| 3485 | |
| 3486 | reloc->delta += target_offset; |
| 3487 | if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) { |
| 3488 | uint32_t page_offset = reloc->offset & ~PAGE_MASK; |
| 3489 | char *vaddr; |
| 3490 | |
| 3491 | vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]); |
| 3492 | *(uint32_t *)(vaddr + page_offset) = reloc->delta; |
| 3493 | kunmap_atomic(vaddr); |
| 3494 | } else { |
| 3495 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 3496 | uint32_t __iomem *reloc_entry; |
| 3497 | void __iomem *reloc_page; |
| 3498 | |
| 3499 | ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1); |
| 3500 | if (ret) |
| 3501 | goto err; |
| 3502 | |
| 3503 | /* Map the page containing the relocation we're going to perform. */ |
| 3504 | reloc->offset += obj->gtt_offset; |
| 3505 | reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping, |
| 3506 | reloc->offset & PAGE_MASK); |
| 3507 | reloc_entry = (uint32_t __iomem *) |
| 3508 | (reloc_page + (reloc->offset & ~PAGE_MASK)); |
| 3509 | iowrite32(reloc->delta, reloc_entry); |
| 3510 | io_mapping_unmap_atomic(reloc_page); |
| 3511 | } |
| 3512 | |
| 3513 | /* and update the user's relocation entry */ |
| 3514 | reloc->presumed_offset = target_offset; |
| 3515 | |
| 3516 | out: |
| 3517 | ret = 0; |
| 3518 | err: |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3519 | drm_gem_object_unreference(target_obj); |
| 3520 | return ret; |
| 3521 | } |
| 3522 | |
| 3523 | static int |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3524 | i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj, |
| 3525 | struct drm_file *file_priv, |
| 3526 | struct drm_i915_gem_exec_object2 *entry) |
| 3527 | { |
| 3528 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
| 3529 | int i, ret; |
| 3530 | |
| 3531 | user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr; |
| 3532 | for (i = 0; i < entry->relocation_count; i++) { |
| 3533 | struct drm_i915_gem_relocation_entry reloc; |
| 3534 | |
| 3535 | if (__copy_from_user_inatomic(&reloc, |
| 3536 | user_relocs+i, |
| 3537 | sizeof(reloc))) |
| 3538 | return -EFAULT; |
| 3539 | |
| 3540 | ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc); |
| 3541 | if (ret) |
| 3542 | return ret; |
| 3543 | |
| 3544 | if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset, |
| 3545 | &reloc.presumed_offset, |
| 3546 | sizeof(reloc.presumed_offset))) |
| 3547 | return -EFAULT; |
| 3548 | } |
| 3549 | |
| 3550 | return 0; |
| 3551 | } |
| 3552 | |
| 3553 | static int |
| 3554 | i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj, |
| 3555 | struct drm_file *file_priv, |
| 3556 | struct drm_i915_gem_exec_object2 *entry, |
| 3557 | struct drm_i915_gem_relocation_entry *relocs) |
| 3558 | { |
| 3559 | int i, ret; |
| 3560 | |
| 3561 | for (i = 0; i < entry->relocation_count; i++) { |
| 3562 | ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]); |
| 3563 | if (ret) |
| 3564 | return ret; |
| 3565 | } |
| 3566 | |
| 3567 | return 0; |
| 3568 | } |
| 3569 | |
| 3570 | static int |
| 3571 | i915_gem_execbuffer_relocate(struct drm_device *dev, |
| 3572 | struct drm_file *file, |
| 3573 | struct drm_gem_object **object_list, |
| 3574 | struct drm_i915_gem_exec_object2 *exec_list, |
| 3575 | int count) |
| 3576 | { |
| 3577 | int i, ret; |
| 3578 | |
| 3579 | for (i = 0; i < count; i++) { |
| 3580 | struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]); |
| 3581 | obj->base.pending_read_domains = 0; |
| 3582 | obj->base.pending_write_domain = 0; |
| 3583 | ret = i915_gem_execbuffer_relocate_object(obj, file, |
| 3584 | &exec_list[i]); |
| 3585 | if (ret) |
| 3586 | return ret; |
| 3587 | } |
| 3588 | |
| 3589 | return 0; |
| 3590 | } |
| 3591 | |
| 3592 | static int |
| 3593 | i915_gem_execbuffer_reserve(struct drm_device *dev, |
| 3594 | struct drm_file *file, |
| 3595 | struct drm_gem_object **object_list, |
| 3596 | struct drm_i915_gem_exec_object2 *exec_list, |
| 3597 | int count) |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3598 | { |
| 3599 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 3600 | int ret, i, retry; |
| 3601 | |
| 3602 | /* attempt to pin all of the buffers into the GTT */ |
Chris Wilson | 5eac3ab | 2010-10-31 08:49:47 +0000 | [diff] [blame] | 3603 | retry = 0; |
| 3604 | do { |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3605 | ret = 0; |
| 3606 | for (i = 0; i < count; i++) { |
| 3607 | struct drm_i915_gem_exec_object2 *entry = &exec_list[i]; |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 3608 | struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3609 | bool need_fence = |
| 3610 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 3611 | obj->tiling_mode != I915_TILING_NONE; |
| 3612 | |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 3613 | /* g33/pnv can't fence buffers in the unmappable part */ |
| 3614 | bool need_mappable = |
| 3615 | entry->relocation_count ? true : need_fence; |
| 3616 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3617 | /* Check fence reg constraints and rebind if necessary */ |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 3618 | if (need_mappable && !obj->map_and_fenceable) { |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3619 | ret = i915_gem_object_unbind(&obj->base); |
| 3620 | if (ret) |
| 3621 | break; |
| 3622 | } |
| 3623 | |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 3624 | ret = i915_gem_object_pin(&obj->base, |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 3625 | entry->alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 3626 | need_mappable); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3627 | if (ret) |
| 3628 | break; |
| 3629 | |
| 3630 | /* |
| 3631 | * Pre-965 chips need a fence register set up in order |
| 3632 | * to properly handle blits to/from tiled surfaces. |
| 3633 | */ |
| 3634 | if (need_fence) { |
| 3635 | ret = i915_gem_object_get_fence_reg(&obj->base, true); |
| 3636 | if (ret) { |
| 3637 | i915_gem_object_unpin(&obj->base); |
| 3638 | break; |
| 3639 | } |
| 3640 | |
| 3641 | dev_priv->fence_regs[obj->fence_reg].gpu = true; |
| 3642 | } |
| 3643 | |
| 3644 | entry->offset = obj->gtt_offset; |
| 3645 | } |
| 3646 | |
| 3647 | while (i--) |
| 3648 | i915_gem_object_unpin(object_list[i]); |
| 3649 | |
Chris Wilson | 5eac3ab | 2010-10-31 08:49:47 +0000 | [diff] [blame] | 3650 | if (ret != -ENOSPC || retry > 1) |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3651 | return ret; |
| 3652 | |
Chris Wilson | 5eac3ab | 2010-10-31 08:49:47 +0000 | [diff] [blame] | 3653 | /* First attempt, just clear anything that is purgeable. |
| 3654 | * Second attempt, clear the entire GTT. |
| 3655 | */ |
| 3656 | ret = i915_gem_evict_everything(dev, retry == 0); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3657 | if (ret) |
| 3658 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3659 | |
Chris Wilson | 5eac3ab | 2010-10-31 08:49:47 +0000 | [diff] [blame] | 3660 | retry++; |
| 3661 | } while (1); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3662 | } |
| 3663 | |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3664 | static int |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3665 | i915_gem_execbuffer_relocate_slow(struct drm_device *dev, |
| 3666 | struct drm_file *file, |
| 3667 | struct drm_gem_object **object_list, |
| 3668 | struct drm_i915_gem_exec_object2 *exec_list, |
| 3669 | int count) |
| 3670 | { |
| 3671 | struct drm_i915_gem_relocation_entry *reloc; |
| 3672 | int i, total, ret; |
| 3673 | |
| 3674 | for (i = 0; i < count; i++) { |
| 3675 | struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]); |
| 3676 | obj->in_execbuffer = false; |
| 3677 | } |
| 3678 | |
| 3679 | mutex_unlock(&dev->struct_mutex); |
| 3680 | |
| 3681 | total = 0; |
| 3682 | for (i = 0; i < count; i++) |
| 3683 | total += exec_list[i].relocation_count; |
| 3684 | |
| 3685 | reloc = drm_malloc_ab(total, sizeof(*reloc)); |
| 3686 | if (reloc == NULL) { |
| 3687 | mutex_lock(&dev->struct_mutex); |
| 3688 | return -ENOMEM; |
| 3689 | } |
| 3690 | |
| 3691 | total = 0; |
| 3692 | for (i = 0; i < count; i++) { |
| 3693 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
| 3694 | |
| 3695 | user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr; |
| 3696 | |
| 3697 | if (copy_from_user(reloc+total, user_relocs, |
| 3698 | exec_list[i].relocation_count * |
| 3699 | sizeof(*reloc))) { |
| 3700 | ret = -EFAULT; |
| 3701 | mutex_lock(&dev->struct_mutex); |
| 3702 | goto err; |
| 3703 | } |
| 3704 | |
| 3705 | total += exec_list[i].relocation_count; |
| 3706 | } |
| 3707 | |
| 3708 | ret = i915_mutex_lock_interruptible(dev); |
| 3709 | if (ret) { |
| 3710 | mutex_lock(&dev->struct_mutex); |
| 3711 | goto err; |
| 3712 | } |
| 3713 | |
| 3714 | ret = i915_gem_execbuffer_reserve(dev, file, |
| 3715 | object_list, exec_list, |
| 3716 | count); |
| 3717 | if (ret) |
| 3718 | goto err; |
| 3719 | |
| 3720 | total = 0; |
| 3721 | for (i = 0; i < count; i++) { |
| 3722 | struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]); |
| 3723 | obj->base.pending_read_domains = 0; |
| 3724 | obj->base.pending_write_domain = 0; |
| 3725 | ret = i915_gem_execbuffer_relocate_object_slow(obj, file, |
| 3726 | &exec_list[i], |
| 3727 | reloc + total); |
| 3728 | if (ret) |
| 3729 | goto err; |
| 3730 | |
| 3731 | total += exec_list[i].relocation_count; |
| 3732 | } |
| 3733 | |
| 3734 | /* Leave the user relocations as are, this is the painfully slow path, |
| 3735 | * and we want to avoid the complication of dropping the lock whilst |
| 3736 | * having buffers reserved in the aperture and so causing spurious |
| 3737 | * ENOSPC for random operations. |
| 3738 | */ |
| 3739 | |
| 3740 | err: |
| 3741 | drm_free_large(reloc); |
| 3742 | return ret; |
| 3743 | } |
| 3744 | |
| 3745 | static int |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3746 | i915_gem_execbuffer_move_to_gpu(struct drm_device *dev, |
| 3747 | struct drm_file *file, |
| 3748 | struct intel_ring_buffer *ring, |
| 3749 | struct drm_gem_object **objects, |
| 3750 | int count) |
| 3751 | { |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3752 | struct change_domains cd; |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3753 | int ret, i; |
| 3754 | |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3755 | cd.invalidate_domains = 0; |
| 3756 | cd.flush_domains = 0; |
| 3757 | cd.flush_rings = 0; |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3758 | for (i = 0; i < count; i++) |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3759 | i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd); |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3760 | |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3761 | if (cd.invalidate_domains | cd.flush_domains) { |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3762 | #if WATCH_EXEC |
| 3763 | DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n", |
| 3764 | __func__, |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3765 | cd.invalidate_domains, |
| 3766 | cd.flush_domains); |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3767 | #endif |
| 3768 | i915_gem_flush(dev, file, |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3769 | cd.invalidate_domains, |
| 3770 | cd.flush_domains, |
| 3771 | cd.flush_rings); |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | for (i = 0; i < count; i++) { |
| 3775 | struct drm_i915_gem_object *obj = to_intel_bo(objects[i]); |
| 3776 | /* XXX replace with semaphores */ |
| 3777 | if (obj->ring && ring != obj->ring) { |
| 3778 | ret = i915_gem_object_wait_rendering(&obj->base, true); |
| 3779 | if (ret) |
| 3780 | return ret; |
| 3781 | } |
| 3782 | } |
| 3783 | |
| 3784 | return 0; |
| 3785 | } |
| 3786 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3787 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3788 | * emitted over 20 msec ago. |
| 3789 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3790 | * Note that if we were to use the current jiffies each time around the loop, |
| 3791 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3792 | * render a frame was over 20ms. |
| 3793 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3794 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3795 | * relatively low latency when blocking on a particular request to finish. |
| 3796 | */ |
| 3797 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3798 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3799 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3800 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 3801 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3802 | unsigned long recent_enough = jiffies - msecs_to_jiffies(20); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3803 | struct drm_i915_gem_request *request; |
| 3804 | struct intel_ring_buffer *ring = NULL; |
| 3805 | u32 seqno = 0; |
| 3806 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3807 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3808 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3809 | list_for_each_entry(request, &file_priv->mm.request_list, client_list) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3810 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3811 | break; |
| 3812 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3813 | ring = request->ring; |
| 3814 | seqno = request->seqno; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3815 | } |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3816 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3817 | |
| 3818 | if (seqno == 0) |
| 3819 | return 0; |
| 3820 | |
| 3821 | ret = 0; |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3822 | if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3823 | /* And wait for the seqno passing without holding any locks and |
| 3824 | * causing extra latency for others. This is safe as the irq |
| 3825 | * generation is designed to be run atomically and so is |
| 3826 | * lockless. |
| 3827 | */ |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3828 | ring->user_irq_get(ring); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3829 | ret = wait_event_interruptible(ring->irq_queue, |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3830 | i915_seqno_passed(ring->get_seqno(ring), seqno) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3831 | || atomic_read(&dev_priv->mm.wedged)); |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3832 | ring->user_irq_put(ring); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3833 | |
| 3834 | if (ret == 0 && atomic_read(&dev_priv->mm.wedged)) |
| 3835 | ret = -EIO; |
| 3836 | } |
| 3837 | |
| 3838 | if (ret == 0) |
| 3839 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3840 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3841 | return ret; |
| 3842 | } |
| 3843 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3844 | static int |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3845 | i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec, |
| 3846 | uint64_t exec_offset) |
Chris Wilson | 83d6079 | 2009-06-06 09:45:57 +0100 | [diff] [blame] | 3847 | { |
| 3848 | uint32_t exec_start, exec_len; |
| 3849 | |
| 3850 | exec_start = (uint32_t) exec_offset + exec->batch_start_offset; |
| 3851 | exec_len = (uint32_t) exec->batch_len; |
| 3852 | |
| 3853 | if ((exec_start | exec_len) & 0x7) |
| 3854 | return -EINVAL; |
| 3855 | |
| 3856 | if (!exec_start) |
| 3857 | return -EINVAL; |
| 3858 | |
| 3859 | return 0; |
| 3860 | } |
| 3861 | |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3862 | static int |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3863 | validate_exec_list(struct drm_i915_gem_exec_object2 *exec, |
| 3864 | int count) |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3865 | { |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3866 | int i; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3867 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3868 | for (i = 0; i < count; i++) { |
| 3869 | char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr; |
Chris Wilson | d1d7883 | 2010-11-21 09:23:48 +0000 | [diff] [blame] | 3870 | int length; /* limited by fault_in_pages_readable() */ |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3871 | |
Chris Wilson | d1d7883 | 2010-11-21 09:23:48 +0000 | [diff] [blame] | 3872 | /* First check for malicious input causing overflow */ |
| 3873 | if (exec[i].relocation_count > |
| 3874 | INT_MAX / sizeof(struct drm_i915_gem_relocation_entry)) |
| 3875 | return -EINVAL; |
| 3876 | |
| 3877 | length = exec[i].relocation_count * |
| 3878 | sizeof(struct drm_i915_gem_relocation_entry); |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3879 | if (!access_ok(VERIFY_READ, ptr, length)) |
| 3880 | return -EFAULT; |
| 3881 | |
Chris Wilson | b5dc608 | 2010-10-20 20:59:57 +0100 | [diff] [blame] | 3882 | /* we may also need to update the presumed offsets */ |
| 3883 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
| 3884 | return -EFAULT; |
| 3885 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3886 | if (fault_in_pages_readable(ptr, length)) |
| 3887 | return -EFAULT; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3888 | } |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3889 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3890 | return 0; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3891 | } |
| 3892 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3893 | static int |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3894 | i915_gem_do_execbuffer(struct drm_device *dev, void *data, |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3895 | struct drm_file *file, |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3896 | struct drm_i915_gem_execbuffer2 *args, |
| 3897 | struct drm_i915_gem_exec_object2 *exec_list) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3898 | { |
| 3899 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3900 | struct drm_gem_object **object_list = NULL; |
| 3901 | struct drm_gem_object *batch_obj; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3902 | struct drm_clip_rect *cliprects = NULL; |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 3903 | struct drm_i915_gem_request *request = NULL; |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3904 | int ret, i, flips; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3905 | uint64_t exec_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3906 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 3907 | struct intel_ring_buffer *ring = NULL; |
| 3908 | |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 3909 | ret = i915_gem_check_is_wedged(dev); |
| 3910 | if (ret) |
| 3911 | return ret; |
| 3912 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3913 | ret = validate_exec_list(exec_list, args->buffer_count); |
| 3914 | if (ret) |
| 3915 | return ret; |
| 3916 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3917 | #if WATCH_EXEC |
| 3918 | DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", |
| 3919 | (int) args->buffers_ptr, args->buffer_count, args->batch_len); |
| 3920 | #endif |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 3921 | switch (args->flags & I915_EXEC_RING_MASK) { |
| 3922 | case I915_EXEC_DEFAULT: |
| 3923 | case I915_EXEC_RENDER: |
| 3924 | ring = &dev_priv->render_ring; |
| 3925 | break; |
| 3926 | case I915_EXEC_BSD: |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3927 | if (!HAS_BSD(dev)) { |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 3928 | DRM_ERROR("execbuf with invalid ring (BSD)\n"); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3929 | return -EINVAL; |
| 3930 | } |
| 3931 | ring = &dev_priv->bsd_ring; |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 3932 | break; |
| 3933 | case I915_EXEC_BLT: |
| 3934 | if (!HAS_BLT(dev)) { |
| 3935 | DRM_ERROR("execbuf with invalid ring (BLT)\n"); |
| 3936 | return -EINVAL; |
| 3937 | } |
| 3938 | ring = &dev_priv->blt_ring; |
| 3939 | break; |
| 3940 | default: |
| 3941 | DRM_ERROR("execbuf with unknown ring: %d\n", |
| 3942 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 3943 | return -EINVAL; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3944 | } |
| 3945 | |
Eric Anholt | 4f481ed | 2008-09-10 14:22:49 -0700 | [diff] [blame] | 3946 | if (args->buffer_count < 1) { |
| 3947 | DRM_ERROR("execbuf with %d buffers\n", args->buffer_count); |
| 3948 | return -EINVAL; |
| 3949 | } |
Eric Anholt | c8e0f93 | 2009-11-22 03:49:37 +0100 | [diff] [blame] | 3950 | object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count); |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3951 | if (object_list == NULL) { |
| 3952 | DRM_ERROR("Failed to allocate object list for %d buffers\n", |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3953 | args->buffer_count); |
| 3954 | ret = -ENOMEM; |
| 3955 | goto pre_mutex_err; |
| 3956 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3957 | |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3958 | if (args->num_cliprects != 0) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 3959 | cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects), |
| 3960 | GFP_KERNEL); |
Owain Ainsworth | a40e8d3 | 2010-02-09 14:25:55 +0000 | [diff] [blame] | 3961 | if (cliprects == NULL) { |
| 3962 | ret = -ENOMEM; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3963 | goto pre_mutex_err; |
Owain Ainsworth | a40e8d3 | 2010-02-09 14:25:55 +0000 | [diff] [blame] | 3964 | } |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3965 | |
| 3966 | ret = copy_from_user(cliprects, |
| 3967 | (struct drm_clip_rect __user *) |
| 3968 | (uintptr_t) args->cliprects_ptr, |
| 3969 | sizeof(*cliprects) * args->num_cliprects); |
| 3970 | if (ret != 0) { |
| 3971 | DRM_ERROR("copy %d cliprects failed: %d\n", |
| 3972 | args->num_cliprects, ret); |
Dan Carpenter | c877cdc | 2010-06-23 19:03:01 +0200 | [diff] [blame] | 3973 | ret = -EFAULT; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3974 | goto pre_mutex_err; |
| 3975 | } |
| 3976 | } |
| 3977 | |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 3978 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
| 3979 | if (request == NULL) { |
| 3980 | ret = -ENOMEM; |
Chris Wilson | a198bc8 | 2009-02-06 16:55:20 +0000 | [diff] [blame] | 3981 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3982 | } |
| 3983 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 3984 | ret = i915_mutex_lock_interruptible(dev); |
| 3985 | if (ret) |
| 3986 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3987 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3988 | if (dev_priv->mm.suspended) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3989 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | a198bc8 | 2009-02-06 16:55:20 +0000 | [diff] [blame] | 3990 | ret = -EBUSY; |
| 3991 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3992 | } |
| 3993 | |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3994 | /* Look up object handles */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3995 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 3996 | struct drm_i915_gem_object *obj_priv; |
| 3997 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3998 | object_list[i] = drm_gem_object_lookup(dev, file, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3999 | exec_list[i].handle); |
| 4000 | if (object_list[i] == NULL) { |
| 4001 | DRM_ERROR("Invalid object handle %d at index %d\n", |
| 4002 | exec_list[i].handle, i); |
Chris Wilson | 0ce907f | 2010-01-23 20:26:35 +0000 | [diff] [blame] | 4003 | /* prevent error path from reading uninitialized data */ |
| 4004 | args->buffer_count = i + 1; |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 4005 | ret = -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4006 | goto err; |
| 4007 | } |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4008 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4009 | obj_priv = to_intel_bo(object_list[i]); |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4010 | if (obj_priv->in_execbuffer) { |
| 4011 | DRM_ERROR("Object %p appears more than once in object list\n", |
| 4012 | object_list[i]); |
Chris Wilson | 0ce907f | 2010-01-23 20:26:35 +0000 | [diff] [blame] | 4013 | /* prevent error path from reading uninitialized data */ |
| 4014 | args->buffer_count = i + 1; |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 4015 | ret = -EINVAL; |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4016 | goto err; |
| 4017 | } |
| 4018 | obj_priv->in_execbuffer = true; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 4019 | } |
| 4020 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 4021 | /* Move the objects en-masse into the GTT, evicting if necessary. */ |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 4022 | ret = i915_gem_execbuffer_reserve(dev, file, |
| 4023 | object_list, exec_list, |
| 4024 | args->buffer_count); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 4025 | if (ret) |
| 4026 | goto err; |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 4027 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 4028 | /* The objects are in their final locations, apply the relocations. */ |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 4029 | ret = i915_gem_execbuffer_relocate(dev, file, |
| 4030 | object_list, exec_list, |
| 4031 | args->buffer_count); |
| 4032 | if (ret) { |
| 4033 | if (ret == -EFAULT) { |
| 4034 | ret = i915_gem_execbuffer_relocate_slow(dev, file, |
| 4035 | object_list, |
| 4036 | exec_list, |
| 4037 | args->buffer_count); |
| 4038 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 4039 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4040 | if (ret) |
| 4041 | goto err; |
| 4042 | } |
| 4043 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4044 | /* Set the pending read domains for the batch buffer to COMMAND */ |
| 4045 | batch_obj = object_list[args->buffer_count-1]; |
Chris Wilson | 5f26a2c | 2009-06-06 09:45:58 +0100 | [diff] [blame] | 4046 | if (batch_obj->pending_write_domain) { |
| 4047 | DRM_ERROR("Attempting to use self-modifying batch buffer\n"); |
| 4048 | ret = -EINVAL; |
| 4049 | goto err; |
| 4050 | } |
| 4051 | batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4052 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 4053 | /* Sanity check the batch buffer */ |
| 4054 | exec_offset = to_intel_bo(batch_obj)->gtt_offset; |
| 4055 | ret = i915_gem_check_execbuffer(args, exec_offset); |
Chris Wilson | 83d6079 | 2009-06-06 09:45:57 +0100 | [diff] [blame] | 4056 | if (ret != 0) { |
| 4057 | DRM_ERROR("execbuf with invalid offset/length\n"); |
| 4058 | goto err; |
| 4059 | } |
| 4060 | |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 4061 | ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring, |
| 4062 | object_list, args->buffer_count); |
| 4063 | if (ret) |
| 4064 | goto err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4065 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4066 | #if WATCH_COHERENCY |
| 4067 | for (i = 0; i < args->buffer_count; i++) { |
| 4068 | i915_gem_object_check_coherency(object_list[i], |
| 4069 | exec_list[i].handle); |
| 4070 | } |
| 4071 | #endif |
| 4072 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4073 | #if WATCH_EXEC |
Ben Gamari | 6911a9b | 2009-04-02 11:24:54 -0700 | [diff] [blame] | 4074 | i915_gem_dump_object(batch_obj, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4075 | args->batch_len, |
| 4076 | __func__, |
| 4077 | ~0); |
| 4078 | #endif |
| 4079 | |
Chris Wilson | e59f2ba | 2010-10-07 17:28:15 +0100 | [diff] [blame] | 4080 | /* Check for any pending flips. As we only maintain a flip queue depth |
| 4081 | * of 1, we can simply insert a WAIT for the next display flip prior |
| 4082 | * to executing the batch and avoid stalling the CPU. |
| 4083 | */ |
| 4084 | flips = 0; |
| 4085 | for (i = 0; i < args->buffer_count; i++) { |
| 4086 | if (object_list[i]->write_domain) |
| 4087 | flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip); |
| 4088 | } |
| 4089 | if (flips) { |
| 4090 | int plane, flip_mask; |
| 4091 | |
| 4092 | for (plane = 0; flips >> plane; plane++) { |
| 4093 | if (((flips >> plane) & 1) == 0) |
| 4094 | continue; |
| 4095 | |
| 4096 | if (plane) |
| 4097 | flip_mask = MI_WAIT_FOR_PLANE_B_FLIP; |
| 4098 | else |
| 4099 | flip_mask = MI_WAIT_FOR_PLANE_A_FLIP; |
| 4100 | |
Chris Wilson | e1f99ce | 2010-10-27 12:45:26 +0100 | [diff] [blame] | 4101 | ret = intel_ring_begin(ring, 2); |
| 4102 | if (ret) |
| 4103 | goto err; |
| 4104 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4105 | intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask); |
| 4106 | intel_ring_emit(ring, MI_NOOP); |
| 4107 | intel_ring_advance(ring); |
Chris Wilson | e59f2ba | 2010-10-07 17:28:15 +0100 | [diff] [blame] | 4108 | } |
| 4109 | } |
| 4110 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4111 | /* Exec the batchbuffer */ |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4112 | ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4113 | if (ret) { |
| 4114 | DRM_ERROR("dispatch failed %d\n", ret); |
| 4115 | goto err; |
| 4116 | } |
| 4117 | |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 4118 | for (i = 0; i < args->buffer_count; i++) { |
| 4119 | struct drm_gem_object *obj = object_list[i]; |
| 4120 | |
| 4121 | obj->read_domains = obj->pending_read_domains; |
| 4122 | obj->write_domain = obj->pending_write_domain; |
| 4123 | |
| 4124 | i915_gem_object_move_to_active(obj, ring); |
| 4125 | if (obj->write_domain) { |
| 4126 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
| 4127 | obj_priv->dirty = 1; |
| 4128 | list_move_tail(&obj_priv->gpu_write_list, |
| 4129 | &ring->gpu_write_list); |
| 4130 | intel_mark_busy(dev, obj); |
| 4131 | } |
| 4132 | |
| 4133 | trace_i915_gem_object_change_domain(obj, |
| 4134 | obj->read_domains, |
| 4135 | obj->write_domain); |
| 4136 | } |
| 4137 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4138 | /* |
| 4139 | * Ensure that the commands in the batch buffer are |
| 4140 | * finished before the interrupt fires |
| 4141 | */ |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 4142 | i915_retire_commands(dev, ring); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4143 | |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 4144 | if (i915_add_request(dev, file, request, ring)) |
Chris Wilson | 5d97eb6 | 2010-11-10 20:40:02 +0000 | [diff] [blame] | 4145 | i915_gem_next_request_seqno(dev, ring); |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 4146 | else |
| 4147 | request = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4148 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4149 | err: |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4150 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 4151 | if (object_list[i] == NULL) |
| 4152 | break; |
| 4153 | |
| 4154 | to_intel_bo(object_list[i])->in_execbuffer = false; |
Julia Lawall | aad87df | 2008-12-21 16:28:47 +0100 | [diff] [blame] | 4155 | drm_gem_object_unreference(object_list[i]); |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4156 | } |
Julia Lawall | aad87df | 2008-12-21 16:28:47 +0100 | [diff] [blame] | 4157 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4158 | mutex_unlock(&dev->struct_mutex); |
| 4159 | |
Chris Wilson | 93533c2 | 2010-01-31 10:40:48 +0000 | [diff] [blame] | 4160 | pre_mutex_err: |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 4161 | drm_free_large(object_list); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 4162 | kfree(cliprects); |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 4163 | kfree(request); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4164 | |
| 4165 | return ret; |
| 4166 | } |
| 4167 | |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4168 | /* |
| 4169 | * Legacy execbuffer just creates an exec2 list from the original exec object |
| 4170 | * list array and passes it to the real function. |
| 4171 | */ |
| 4172 | int |
| 4173 | i915_gem_execbuffer(struct drm_device *dev, void *data, |
| 4174 | struct drm_file *file_priv) |
| 4175 | { |
| 4176 | struct drm_i915_gem_execbuffer *args = data; |
| 4177 | struct drm_i915_gem_execbuffer2 exec2; |
| 4178 | struct drm_i915_gem_exec_object *exec_list = NULL; |
| 4179 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 4180 | int ret, i; |
| 4181 | |
| 4182 | #if WATCH_EXEC |
| 4183 | DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", |
| 4184 | (int) args->buffers_ptr, args->buffer_count, args->batch_len); |
| 4185 | #endif |
| 4186 | |
| 4187 | if (args->buffer_count < 1) { |
| 4188 | DRM_ERROR("execbuf with %d buffers\n", args->buffer_count); |
| 4189 | return -EINVAL; |
| 4190 | } |
| 4191 | |
| 4192 | /* Copy in the exec list from userland */ |
| 4193 | exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count); |
| 4194 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 4195 | if (exec_list == NULL || exec2_list == NULL) { |
| 4196 | DRM_ERROR("Failed to allocate exec list for %d buffers\n", |
| 4197 | args->buffer_count); |
| 4198 | drm_free_large(exec_list); |
| 4199 | drm_free_large(exec2_list); |
| 4200 | return -ENOMEM; |
| 4201 | } |
| 4202 | ret = copy_from_user(exec_list, |
| 4203 | (struct drm_i915_relocation_entry __user *) |
| 4204 | (uintptr_t) args->buffers_ptr, |
| 4205 | sizeof(*exec_list) * args->buffer_count); |
| 4206 | if (ret != 0) { |
| 4207 | DRM_ERROR("copy %d exec entries failed %d\n", |
| 4208 | args->buffer_count, ret); |
| 4209 | drm_free_large(exec_list); |
| 4210 | drm_free_large(exec2_list); |
| 4211 | return -EFAULT; |
| 4212 | } |
| 4213 | |
| 4214 | for (i = 0; i < args->buffer_count; i++) { |
| 4215 | exec2_list[i].handle = exec_list[i].handle; |
| 4216 | exec2_list[i].relocation_count = exec_list[i].relocation_count; |
| 4217 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; |
| 4218 | exec2_list[i].alignment = exec_list[i].alignment; |
| 4219 | exec2_list[i].offset = exec_list[i].offset; |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4220 | if (INTEL_INFO(dev)->gen < 4) |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4221 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; |
| 4222 | else |
| 4223 | exec2_list[i].flags = 0; |
| 4224 | } |
| 4225 | |
| 4226 | exec2.buffers_ptr = args->buffers_ptr; |
| 4227 | exec2.buffer_count = args->buffer_count; |
| 4228 | exec2.batch_start_offset = args->batch_start_offset; |
| 4229 | exec2.batch_len = args->batch_len; |
| 4230 | exec2.DR1 = args->DR1; |
| 4231 | exec2.DR4 = args->DR4; |
| 4232 | exec2.num_cliprects = args->num_cliprects; |
| 4233 | exec2.cliprects_ptr = args->cliprects_ptr; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 4234 | exec2.flags = I915_EXEC_RENDER; |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4235 | |
| 4236 | ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list); |
| 4237 | if (!ret) { |
| 4238 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 4239 | for (i = 0; i < args->buffer_count; i++) |
| 4240 | exec_list[i].offset = exec2_list[i].offset; |
| 4241 | /* ... and back out to userspace */ |
| 4242 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 4243 | (uintptr_t) args->buffers_ptr, |
| 4244 | exec_list, |
| 4245 | sizeof(*exec_list) * args->buffer_count); |
| 4246 | if (ret) { |
| 4247 | ret = -EFAULT; |
| 4248 | DRM_ERROR("failed to copy %d exec entries " |
| 4249 | "back to user (%d)\n", |
| 4250 | args->buffer_count, ret); |
| 4251 | } |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4252 | } |
| 4253 | |
| 4254 | drm_free_large(exec_list); |
| 4255 | drm_free_large(exec2_list); |
| 4256 | return ret; |
| 4257 | } |
| 4258 | |
| 4259 | int |
| 4260 | i915_gem_execbuffer2(struct drm_device *dev, void *data, |
| 4261 | struct drm_file *file_priv) |
| 4262 | { |
| 4263 | struct drm_i915_gem_execbuffer2 *args = data; |
| 4264 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 4265 | int ret; |
| 4266 | |
| 4267 | #if WATCH_EXEC |
| 4268 | DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", |
| 4269 | (int) args->buffers_ptr, args->buffer_count, args->batch_len); |
| 4270 | #endif |
| 4271 | |
| 4272 | if (args->buffer_count < 1) { |
| 4273 | DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count); |
| 4274 | return -EINVAL; |
| 4275 | } |
| 4276 | |
| 4277 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 4278 | if (exec2_list == NULL) { |
| 4279 | DRM_ERROR("Failed to allocate exec list for %d buffers\n", |
| 4280 | args->buffer_count); |
| 4281 | return -ENOMEM; |
| 4282 | } |
| 4283 | ret = copy_from_user(exec2_list, |
| 4284 | (struct drm_i915_relocation_entry __user *) |
| 4285 | (uintptr_t) args->buffers_ptr, |
| 4286 | sizeof(*exec2_list) * args->buffer_count); |
| 4287 | if (ret != 0) { |
| 4288 | DRM_ERROR("copy %d exec entries failed %d\n", |
| 4289 | args->buffer_count, ret); |
| 4290 | drm_free_large(exec2_list); |
| 4291 | return -EFAULT; |
| 4292 | } |
| 4293 | |
| 4294 | ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list); |
| 4295 | if (!ret) { |
| 4296 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 4297 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 4298 | (uintptr_t) args->buffers_ptr, |
| 4299 | exec2_list, |
| 4300 | sizeof(*exec2_list) * args->buffer_count); |
| 4301 | if (ret) { |
| 4302 | ret = -EFAULT; |
| 4303 | DRM_ERROR("failed to copy %d exec entries " |
| 4304 | "back to user (%d)\n", |
| 4305 | args->buffer_count, ret); |
| 4306 | } |
| 4307 | } |
| 4308 | |
| 4309 | drm_free_large(exec2_list); |
| 4310 | return ret; |
| 4311 | } |
| 4312 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4313 | int |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 4314 | i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4315 | bool map_and_fenceable) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4316 | { |
| 4317 | struct drm_device *dev = obj->dev; |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4318 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4319 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4320 | int ret; |
| 4321 | |
Daniel Vetter | 778c354 | 2010-05-13 11:49:44 +0200 | [diff] [blame] | 4322 | BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT); |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4323 | BUG_ON(map_and_fenceable && !map_and_fenceable); |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4324 | WARN_ON(i915_verify_lists(dev)); |
Chris Wilson | ac0c6b5 | 2010-05-27 13:18:18 +0100 | [diff] [blame] | 4325 | |
| 4326 | if (obj_priv->gtt_space != NULL) { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 4327 | if ((alignment && obj_priv->gtt_offset & (alignment - 1)) || |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4328 | (map_and_fenceable && !obj_priv->map_and_fenceable)) { |
Chris Wilson | ae7d49d | 2010-08-04 12:37:41 +0100 | [diff] [blame] | 4329 | WARN(obj_priv->pin_count, |
| 4330 | "bo is already pinned with incorrect alignment:" |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4331 | " offset=%x, req.alignment=%x, req.map_and_fenceable=%d," |
| 4332 | " obj->map_and_fenceable=%d\n", |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 4333 | obj_priv->gtt_offset, alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4334 | map_and_fenceable, |
| 4335 | obj_priv->map_and_fenceable); |
Chris Wilson | ac0c6b5 | 2010-05-27 13:18:18 +0100 | [diff] [blame] | 4336 | ret = i915_gem_object_unbind(obj); |
| 4337 | if (ret) |
| 4338 | return ret; |
| 4339 | } |
| 4340 | } |
| 4341 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4342 | if (obj_priv->gtt_space == NULL) { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 4343 | ret = i915_gem_object_bind_to_gtt(obj, alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4344 | map_and_fenceable); |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 4345 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4346 | return ret; |
Chris Wilson | 22c344e | 2009-02-11 14:26:45 +0000 | [diff] [blame] | 4347 | } |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4348 | |
Chris Wilson | 7465378 | 2010-10-29 10:41:23 +0100 | [diff] [blame] | 4349 | if (obj_priv->pin_count++ == 0) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4350 | i915_gem_info_add_pin(dev_priv, obj_priv, map_and_fenceable); |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4351 | if (!obj_priv->active) |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4352 | list_move_tail(&obj_priv->mm_list, |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4353 | &dev_priv->mm.pinned_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4354 | } |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4355 | BUG_ON(!obj_priv->pin_mappable && map_and_fenceable); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4356 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4357 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4358 | return 0; |
| 4359 | } |
| 4360 | |
| 4361 | void |
| 4362 | i915_gem_object_unpin(struct drm_gem_object *obj) |
| 4363 | { |
| 4364 | struct drm_device *dev = obj->dev; |
| 4365 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4366 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4367 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4368 | WARN_ON(i915_verify_lists(dev)); |
Chris Wilson | 7465378 | 2010-10-29 10:41:23 +0100 | [diff] [blame] | 4369 | BUG_ON(obj_priv->pin_count == 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4370 | BUG_ON(obj_priv->gtt_space == NULL); |
| 4371 | |
Chris Wilson | 7465378 | 2010-10-29 10:41:23 +0100 | [diff] [blame] | 4372 | if (--obj_priv->pin_count == 0) { |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4373 | if (!obj_priv->active) |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4374 | list_move_tail(&obj_priv->mm_list, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4375 | &dev_priv->mm.inactive_list); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 4376 | i915_gem_info_remove_pin(dev_priv, obj_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4377 | } |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4378 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4379 | } |
| 4380 | |
| 4381 | int |
| 4382 | i915_gem_pin_ioctl(struct drm_device *dev, void *data, |
| 4383 | struct drm_file *file_priv) |
| 4384 | { |
| 4385 | struct drm_i915_gem_pin *args = data; |
| 4386 | struct drm_gem_object *obj; |
| 4387 | struct drm_i915_gem_object *obj_priv; |
| 4388 | int ret; |
| 4389 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4390 | ret = i915_mutex_lock_interruptible(dev); |
| 4391 | if (ret) |
| 4392 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4393 | |
| 4394 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 4395 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4396 | ret = -ENOENT; |
| 4397 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4398 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4399 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4400 | |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4401 | if (obj_priv->madv != I915_MADV_WILLNEED) { |
| 4402 | DRM_ERROR("Attempting to pin a purgeable buffer\n"); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4403 | ret = -EINVAL; |
| 4404 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4405 | } |
| 4406 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4407 | if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) { |
| 4408 | DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n", |
| 4409 | args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4410 | ret = -EINVAL; |
| 4411 | goto out; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | obj_priv->user_pin_count++; |
| 4415 | obj_priv->pin_filp = file_priv; |
| 4416 | if (obj_priv->user_pin_count == 1) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4417 | ret = i915_gem_object_pin(obj, args->alignment, true); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4418 | if (ret) |
| 4419 | goto out; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | /* XXX - flush the CPU caches for pinned objects |
| 4423 | * as the X server doesn't manage domains yet |
| 4424 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4425 | i915_gem_object_flush_cpu_write_domain(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4426 | args->offset = obj_priv->gtt_offset; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4427 | out: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4428 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4429 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4430 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4431 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | int |
| 4435 | i915_gem_unpin_ioctl(struct drm_device *dev, void *data, |
| 4436 | struct drm_file *file_priv) |
| 4437 | { |
| 4438 | struct drm_i915_gem_pin *args = data; |
| 4439 | struct drm_gem_object *obj; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4440 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 4441 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4442 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4443 | ret = i915_mutex_lock_interruptible(dev); |
| 4444 | if (ret) |
| 4445 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4446 | |
| 4447 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 4448 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4449 | ret = -ENOENT; |
| 4450 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4451 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4452 | obj_priv = to_intel_bo(obj); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 4453 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4454 | if (obj_priv->pin_filp != file_priv) { |
| 4455 | DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n", |
| 4456 | args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4457 | ret = -EINVAL; |
| 4458 | goto out; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4459 | } |
| 4460 | obj_priv->user_pin_count--; |
| 4461 | if (obj_priv->user_pin_count == 0) { |
| 4462 | obj_priv->pin_filp = NULL; |
| 4463 | i915_gem_object_unpin(obj); |
| 4464 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4465 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4466 | out: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4467 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4468 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4469 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4470 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | int |
| 4474 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
| 4475 | struct drm_file *file_priv) |
| 4476 | { |
| 4477 | struct drm_i915_gem_busy *args = data; |
| 4478 | struct drm_gem_object *obj; |
| 4479 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 4480 | int ret; |
| 4481 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4482 | ret = i915_mutex_lock_interruptible(dev); |
| 4483 | if (ret) |
| 4484 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4485 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4486 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 4487 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4488 | ret = -ENOENT; |
| 4489 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4490 | } |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4491 | obj_priv = to_intel_bo(obj); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4492 | |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4493 | /* Count all active objects as busy, even if they are currently not used |
| 4494 | * by the gpu. Users of this interface expect objects to eventually |
| 4495 | * become non-busy without any further actions, therefore emit any |
| 4496 | * necessary flushes here. |
Eric Anholt | c4de0a5 | 2008-12-14 19:05:04 -0800 | [diff] [blame] | 4497 | */ |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4498 | args->busy = obj_priv->active; |
| 4499 | if (args->busy) { |
| 4500 | /* Unconditionally flush objects, even when the gpu still uses this |
| 4501 | * object. Userspace calling this function indicates that it wants to |
| 4502 | * use this buffer rather sooner than later, so issuing the required |
| 4503 | * flush earlier is beneficial. |
| 4504 | */ |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 4505 | if (obj->write_domain & I915_GEM_GPU_DOMAINS) |
| 4506 | i915_gem_flush_ring(dev, file_priv, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 4507 | obj_priv->ring, |
| 4508 | 0, obj->write_domain); |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4509 | |
| 4510 | /* Update the active list for the hardware's current position. |
| 4511 | * Otherwise this only updates on a delayed timer or when irqs |
| 4512 | * are actually unmasked, and our working set ends up being |
| 4513 | * larger than required. |
| 4514 | */ |
| 4515 | i915_gem_retire_requests_ring(dev, obj_priv->ring); |
| 4516 | |
| 4517 | args->busy = obj_priv->active; |
| 4518 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4519 | |
| 4520 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4521 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4522 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4523 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4524 | } |
| 4525 | |
| 4526 | int |
| 4527 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4528 | struct drm_file *file_priv) |
| 4529 | { |
| 4530 | return i915_gem_ring_throttle(dev, file_priv); |
| 4531 | } |
| 4532 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4533 | int |
| 4534 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4535 | struct drm_file *file_priv) |
| 4536 | { |
| 4537 | struct drm_i915_gem_madvise *args = data; |
| 4538 | struct drm_gem_object *obj; |
| 4539 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 4540 | int ret; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4541 | |
| 4542 | switch (args->madv) { |
| 4543 | case I915_MADV_DONTNEED: |
| 4544 | case I915_MADV_WILLNEED: |
| 4545 | break; |
| 4546 | default: |
| 4547 | return -EINVAL; |
| 4548 | } |
| 4549 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4550 | ret = i915_mutex_lock_interruptible(dev); |
| 4551 | if (ret) |
| 4552 | return ret; |
| 4553 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4554 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 4555 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4556 | ret = -ENOENT; |
| 4557 | goto unlock; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4558 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4559 | obj_priv = to_intel_bo(obj); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4560 | |
| 4561 | if (obj_priv->pin_count) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4562 | ret = -EINVAL; |
| 4563 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4564 | } |
| 4565 | |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4566 | if (obj_priv->madv != __I915_MADV_PURGED) |
| 4567 | obj_priv->madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4568 | |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4569 | /* if the object is no longer bound, discard its backing storage */ |
| 4570 | if (i915_gem_object_is_purgeable(obj_priv) && |
| 4571 | obj_priv->gtt_space == NULL) |
| 4572 | i915_gem_object_truncate(obj); |
| 4573 | |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4574 | args->retained = obj_priv->madv != __I915_MADV_PURGED; |
| 4575 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4576 | out: |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4577 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4578 | unlock: |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4579 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4580 | return ret; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4581 | } |
| 4582 | |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4583 | struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev, |
| 4584 | size_t size) |
| 4585 | { |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4586 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4587 | struct drm_i915_gem_object *obj; |
| 4588 | |
| 4589 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
| 4590 | if (obj == NULL) |
| 4591 | return NULL; |
| 4592 | |
| 4593 | if (drm_gem_object_init(dev, &obj->base, size) != 0) { |
| 4594 | kfree(obj); |
| 4595 | return NULL; |
| 4596 | } |
| 4597 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4598 | i915_gem_info_add_obj(dev_priv, size); |
| 4599 | |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4600 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
| 4601 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 4602 | |
| 4603 | obj->agp_type = AGP_USER_MEMORY; |
Daniel Vetter | 62b8b21 | 2010-04-09 19:05:08 +0000 | [diff] [blame] | 4604 | obj->base.driver_private = NULL; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4605 | obj->fence_reg = I915_FENCE_REG_NONE; |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4606 | INIT_LIST_HEAD(&obj->mm_list); |
| 4607 | INIT_LIST_HEAD(&obj->ring_list); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4608 | INIT_LIST_HEAD(&obj->gpu_write_list); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4609 | obj->madv = I915_MADV_WILLNEED; |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4610 | /* Avoid an unnecessary call to unbind on the first bind. */ |
| 4611 | obj->map_and_fenceable = true; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4612 | |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4613 | return &obj->base; |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4614 | } |
| 4615 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4616 | int i915_gem_init_object(struct drm_gem_object *obj) |
| 4617 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4618 | BUG(); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4619 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4620 | return 0; |
| 4621 | } |
| 4622 | |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4623 | static void i915_gem_free_object_tail(struct drm_gem_object *obj) |
| 4624 | { |
| 4625 | struct drm_device *dev = obj->dev; |
| 4626 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4627 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
| 4628 | int ret; |
| 4629 | |
| 4630 | ret = i915_gem_object_unbind(obj); |
| 4631 | if (ret == -ERESTARTSYS) { |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4632 | list_move(&obj_priv->mm_list, |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4633 | &dev_priv->mm.deferred_free_list); |
| 4634 | return; |
| 4635 | } |
| 4636 | |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame] | 4637 | if (obj->map_list.map) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4638 | i915_gem_free_mmap_offset(obj); |
| 4639 | |
| 4640 | drm_gem_object_release(obj); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4641 | i915_gem_info_remove_obj(dev_priv, obj->size); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4642 | |
| 4643 | kfree(obj_priv->page_cpu_valid); |
| 4644 | kfree(obj_priv->bit_17); |
| 4645 | kfree(obj_priv); |
| 4646 | } |
| 4647 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4648 | void i915_gem_free_object(struct drm_gem_object *obj) |
| 4649 | { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4650 | struct drm_device *dev = obj->dev; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4651 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4652 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 4653 | trace_i915_gem_object_destroy(obj); |
| 4654 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4655 | while (obj_priv->pin_count > 0) |
| 4656 | i915_gem_object_unpin(obj); |
| 4657 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4658 | if (obj_priv->phys_obj) |
| 4659 | i915_gem_detach_phys_object(dev, obj); |
| 4660 | |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4661 | i915_gem_free_object_tail(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4662 | } |
| 4663 | |
Jesse Barnes | 5669fca | 2009-02-17 15:13:31 -0800 | [diff] [blame] | 4664 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4665 | i915_gem_idle(struct drm_device *dev) |
| 4666 | { |
| 4667 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4668 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4669 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4670 | mutex_lock(&dev->struct_mutex); |
| 4671 | |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 4672 | if (dev_priv->mm.suspended) { |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4673 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4674 | return 0; |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4675 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4676 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4677 | ret = i915_gpu_idle(dev); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4678 | if (ret) { |
| 4679 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4680 | return ret; |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4681 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4682 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4683 | /* Under UMS, be paranoid and evict. */ |
| 4684 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) { |
Chris Wilson | 5eac3ab | 2010-10-31 08:49:47 +0000 | [diff] [blame] | 4685 | ret = i915_gem_evict_inactive(dev, false); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4686 | if (ret) { |
| 4687 | mutex_unlock(&dev->struct_mutex); |
| 4688 | return ret; |
| 4689 | } |
| 4690 | } |
| 4691 | |
| 4692 | /* Hack! Don't let anybody do execbuf while we don't control the chip. |
| 4693 | * We need to replace this with a semaphore, or something. |
| 4694 | * And not confound mm.suspended! |
| 4695 | */ |
| 4696 | dev_priv->mm.suspended = 1; |
Daniel Vetter | bc0c7f1 | 2010-08-20 18:18:48 +0200 | [diff] [blame] | 4697 | del_timer_sync(&dev_priv->hangcheck_timer); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4698 | |
| 4699 | i915_kernel_lost_context(dev); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4700 | i915_gem_cleanup_ringbuffer(dev); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4701 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4702 | mutex_unlock(&dev->struct_mutex); |
| 4703 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4704 | /* Cancel the retire work handler, which should be idle now. */ |
| 4705 | cancel_delayed_work_sync(&dev_priv->mm.retire_work); |
| 4706 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4707 | return 0; |
| 4708 | } |
| 4709 | |
Jesse Barnes | e552eb7 | 2010-04-21 11:39:23 -0700 | [diff] [blame] | 4710 | /* |
| 4711 | * 965+ support PIPE_CONTROL commands, which provide finer grained control |
| 4712 | * over cache flushing. |
| 4713 | */ |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4714 | static int |
Jesse Barnes | e552eb7 | 2010-04-21 11:39:23 -0700 | [diff] [blame] | 4715 | i915_gem_init_pipe_control(struct drm_device *dev) |
| 4716 | { |
| 4717 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4718 | struct drm_gem_object *obj; |
| 4719 | struct drm_i915_gem_object *obj_priv; |
| 4720 | int ret; |
| 4721 | |
Eric Anholt | 34dc4d4 | 2010-05-07 14:30:03 -0700 | [diff] [blame] | 4722 | obj = i915_gem_alloc_object(dev, 4096); |
Jesse Barnes | e552eb7 | 2010-04-21 11:39:23 -0700 | [diff] [blame] | 4723 | if (obj == NULL) { |
| 4724 | DRM_ERROR("Failed to allocate seqno page\n"); |
| 4725 | ret = -ENOMEM; |
| 4726 | goto err; |
| 4727 | } |
| 4728 | obj_priv = to_intel_bo(obj); |
| 4729 | obj_priv->agp_type = AGP_USER_CACHED_MEMORY; |
| 4730 | |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4731 | ret = i915_gem_object_pin(obj, 4096, true); |
Jesse Barnes | e552eb7 | 2010-04-21 11:39:23 -0700 | [diff] [blame] | 4732 | if (ret) |
| 4733 | goto err_unref; |
| 4734 | |
| 4735 | dev_priv->seqno_gfx_addr = obj_priv->gtt_offset; |
| 4736 | dev_priv->seqno_page = kmap(obj_priv->pages[0]); |
| 4737 | if (dev_priv->seqno_page == NULL) |
| 4738 | goto err_unpin; |
| 4739 | |
| 4740 | dev_priv->seqno_obj = obj; |
| 4741 | memset(dev_priv->seqno_page, 0, PAGE_SIZE); |
| 4742 | |
| 4743 | return 0; |
| 4744 | |
| 4745 | err_unpin: |
| 4746 | i915_gem_object_unpin(obj); |
| 4747 | err_unref: |
| 4748 | drm_gem_object_unreference(obj); |
| 4749 | err: |
| 4750 | return ret; |
| 4751 | } |
| 4752 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4753 | |
| 4754 | static void |
Jesse Barnes | e552eb7 | 2010-04-21 11:39:23 -0700 | [diff] [blame] | 4755 | i915_gem_cleanup_pipe_control(struct drm_device *dev) |
| 4756 | { |
| 4757 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4758 | struct drm_gem_object *obj; |
| 4759 | struct drm_i915_gem_object *obj_priv; |
| 4760 | |
| 4761 | obj = dev_priv->seqno_obj; |
| 4762 | obj_priv = to_intel_bo(obj); |
| 4763 | kunmap(obj_priv->pages[0]); |
| 4764 | i915_gem_object_unpin(obj); |
| 4765 | drm_gem_object_unreference(obj); |
| 4766 | dev_priv->seqno_obj = NULL; |
| 4767 | |
| 4768 | dev_priv->seqno_page = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4769 | } |
| 4770 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4771 | int |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4772 | i915_gem_init_ringbuffer(struct drm_device *dev) |
| 4773 | { |
| 4774 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4775 | int ret; |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4776 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4777 | if (HAS_PIPE_CONTROL(dev)) { |
| 4778 | ret = i915_gem_init_pipe_control(dev); |
| 4779 | if (ret) |
| 4780 | return ret; |
| 4781 | } |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4782 | |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 4783 | ret = intel_init_render_ring_buffer(dev); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4784 | if (ret) |
| 4785 | goto cleanup_pipe_control; |
| 4786 | |
| 4787 | if (HAS_BSD(dev)) { |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 4788 | ret = intel_init_bsd_ring_buffer(dev); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4789 | if (ret) |
| 4790 | goto cleanup_render_ring; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4791 | } |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4792 | |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4793 | if (HAS_BLT(dev)) { |
| 4794 | ret = intel_init_blt_ring_buffer(dev); |
| 4795 | if (ret) |
| 4796 | goto cleanup_bsd_ring; |
| 4797 | } |
| 4798 | |
Chris Wilson | 6f392d5 | 2010-08-07 11:01:22 +0100 | [diff] [blame] | 4799 | dev_priv->next_seqno = 1; |
| 4800 | |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4801 | return 0; |
| 4802 | |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4803 | cleanup_bsd_ring: |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4804 | intel_cleanup_ring_buffer(&dev_priv->bsd_ring); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4805 | cleanup_render_ring: |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4806 | intel_cleanup_ring_buffer(&dev_priv->render_ring); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4807 | cleanup_pipe_control: |
| 4808 | if (HAS_PIPE_CONTROL(dev)) |
| 4809 | i915_gem_cleanup_pipe_control(dev); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4810 | return ret; |
| 4811 | } |
| 4812 | |
| 4813 | void |
| 4814 | i915_gem_cleanup_ringbuffer(struct drm_device *dev) |
| 4815 | { |
| 4816 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4817 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4818 | intel_cleanup_ring_buffer(&dev_priv->render_ring); |
| 4819 | intel_cleanup_ring_buffer(&dev_priv->bsd_ring); |
| 4820 | intel_cleanup_ring_buffer(&dev_priv->blt_ring); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4821 | if (HAS_PIPE_CONTROL(dev)) |
| 4822 | i915_gem_cleanup_pipe_control(dev); |
| 4823 | } |
| 4824 | |
| 4825 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4826 | i915_gem_entervt_ioctl(struct drm_device *dev, void *data, |
| 4827 | struct drm_file *file_priv) |
| 4828 | { |
| 4829 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4830 | int ret; |
| 4831 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4832 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4833 | return 0; |
| 4834 | |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 4835 | if (atomic_read(&dev_priv->mm.wedged)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4836 | DRM_ERROR("Reenabling wedged hardware, good luck\n"); |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 4837 | atomic_set(&dev_priv->mm.wedged, 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4838 | } |
| 4839 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4840 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4841 | dev_priv->mm.suspended = 0; |
| 4842 | |
| 4843 | ret = i915_gem_init_ringbuffer(dev); |
Wu Fengguang | d816f6ac | 2009-04-18 10:43:32 +0800 | [diff] [blame] | 4844 | if (ret != 0) { |
| 4845 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4846 | return ret; |
Wu Fengguang | d816f6ac | 2009-04-18 10:43:32 +0800 | [diff] [blame] | 4847 | } |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4848 | |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4849 | BUG_ON(!list_empty(&dev_priv->mm.active_list)); |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 4850 | BUG_ON(!list_empty(&dev_priv->render_ring.active_list)); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 4851 | BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list)); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4852 | BUG_ON(!list_empty(&dev_priv->blt_ring.active_list)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4853 | BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); |
| 4854 | BUG_ON(!list_empty(&dev_priv->mm.inactive_list)); |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 4855 | BUG_ON(!list_empty(&dev_priv->render_ring.request_list)); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 4856 | BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list)); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4857 | BUG_ON(!list_empty(&dev_priv->blt_ring.request_list)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4858 | mutex_unlock(&dev->struct_mutex); |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4859 | |
Chris Wilson | 5f35308 | 2010-06-07 14:03:03 +0100 | [diff] [blame] | 4860 | ret = drm_irq_install(dev); |
| 4861 | if (ret) |
| 4862 | goto cleanup_ringbuffer; |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4863 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4864 | return 0; |
Chris Wilson | 5f35308 | 2010-06-07 14:03:03 +0100 | [diff] [blame] | 4865 | |
| 4866 | cleanup_ringbuffer: |
| 4867 | mutex_lock(&dev->struct_mutex); |
| 4868 | i915_gem_cleanup_ringbuffer(dev); |
| 4869 | dev_priv->mm.suspended = 1; |
| 4870 | mutex_unlock(&dev->struct_mutex); |
| 4871 | |
| 4872 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | int |
| 4876 | i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, |
| 4877 | struct drm_file *file_priv) |
| 4878 | { |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4879 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4880 | return 0; |
| 4881 | |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4882 | drm_irq_uninstall(dev); |
Linus Torvalds | e6890f6 | 2009-09-08 17:09:24 -0700 | [diff] [blame] | 4883 | return i915_gem_idle(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | void |
| 4887 | i915_gem_lastclose(struct drm_device *dev) |
| 4888 | { |
| 4889 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4890 | |
Eric Anholt | e806b49 | 2009-01-22 09:56:58 -0800 | [diff] [blame] | 4891 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4892 | return; |
| 4893 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4894 | ret = i915_gem_idle(dev); |
| 4895 | if (ret) |
| 4896 | DRM_ERROR("failed to idle hardware: %d\n", ret); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4897 | } |
| 4898 | |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 4899 | static void |
| 4900 | init_ring_lists(struct intel_ring_buffer *ring) |
| 4901 | { |
| 4902 | INIT_LIST_HEAD(&ring->active_list); |
| 4903 | INIT_LIST_HEAD(&ring->request_list); |
| 4904 | INIT_LIST_HEAD(&ring->gpu_write_list); |
| 4905 | } |
| 4906 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4907 | void |
| 4908 | i915_gem_load(struct drm_device *dev) |
| 4909 | { |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4910 | int i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4911 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4912 | |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4913 | INIT_LIST_HEAD(&dev_priv->mm.active_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4914 | INIT_LIST_HEAD(&dev_priv->mm.flushing_list); |
| 4915 | INIT_LIST_HEAD(&dev_priv->mm.inactive_list); |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4916 | INIT_LIST_HEAD(&dev_priv->mm.pinned_list); |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 4917 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4918 | INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list); |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 4919 | init_ring_lists(&dev_priv->render_ring); |
| 4920 | init_ring_lists(&dev_priv->bsd_ring); |
| 4921 | init_ring_lists(&dev_priv->blt_ring); |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 4922 | for (i = 0; i < 16; i++) |
| 4923 | INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4924 | INIT_DELAYED_WORK(&dev_priv->mm.retire_work, |
| 4925 | i915_gem_retire_work_handler); |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 4926 | init_completion(&dev_priv->error_completion); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4927 | |
Dave Airlie | 9440012 | 2010-07-20 13:15:31 +1000 | [diff] [blame] | 4928 | /* On GEN3 we really need to make sure the ARB C3 LP bit is set */ |
| 4929 | if (IS_GEN3(dev)) { |
| 4930 | u32 tmp = I915_READ(MI_ARB_STATE); |
| 4931 | if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) { |
| 4932 | /* arb state is a masked write, so set bit + bit in mask */ |
| 4933 | tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT); |
| 4934 | I915_WRITE(MI_ARB_STATE, tmp); |
| 4935 | } |
| 4936 | } |
| 4937 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4938 | /* Old X drivers will take 0-2 for front, back, depth buffers */ |
Eric Anholt | b397c83 | 2010-01-26 09:43:10 -0800 | [diff] [blame] | 4939 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4940 | dev_priv->fence_reg_start = 3; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4941 | |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4942 | if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4943 | dev_priv->num_fence_regs = 16; |
| 4944 | else |
| 4945 | dev_priv->num_fence_regs = 8; |
| 4946 | |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4947 | /* Initialize fence registers to zero */ |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4948 | switch (INTEL_INFO(dev)->gen) { |
| 4949 | case 6: |
| 4950 | for (i = 0; i < 16; i++) |
| 4951 | I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0); |
| 4952 | break; |
| 4953 | case 5: |
| 4954 | case 4: |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4955 | for (i = 0; i < 16; i++) |
| 4956 | I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0); |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4957 | break; |
| 4958 | case 3: |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4959 | if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) |
| 4960 | for (i = 0; i < 8; i++) |
| 4961 | I915_WRITE(FENCE_REG_945_8 + (i * 4), 0); |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4962 | case 2: |
| 4963 | for (i = 0; i < 8; i++) |
| 4964 | I915_WRITE(FENCE_REG_830_0 + (i * 4), 0); |
| 4965 | break; |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4966 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4967 | i915_gem_detect_bit_6_swizzle(dev); |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 4968 | init_waitqueue_head(&dev_priv->pending_flip_queue); |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 4969 | |
| 4970 | dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink; |
| 4971 | dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS; |
| 4972 | register_shrinker(&dev_priv->mm.inactive_shrinker); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4973 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4974 | |
| 4975 | /* |
| 4976 | * Create a physically contiguous memory object for this object |
| 4977 | * e.g. for cursor + overlay regs |
| 4978 | */ |
Chris Wilson | 995b6762 | 2010-08-20 13:23:26 +0100 | [diff] [blame] | 4979 | static int i915_gem_init_phys_object(struct drm_device *dev, |
| 4980 | int id, int size, int align) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4981 | { |
| 4982 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4983 | struct drm_i915_gem_phys_object *phys_obj; |
| 4984 | int ret; |
| 4985 | |
| 4986 | if (dev_priv->mm.phys_objs[id - 1] || !size) |
| 4987 | return 0; |
| 4988 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 4989 | phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4990 | if (!phys_obj) |
| 4991 | return -ENOMEM; |
| 4992 | |
| 4993 | phys_obj->id = id; |
| 4994 | |
Chris Wilson | 6eeefaf | 2010-08-07 11:01:39 +0100 | [diff] [blame] | 4995 | phys_obj->handle = drm_pci_alloc(dev, size, align); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4996 | if (!phys_obj->handle) { |
| 4997 | ret = -ENOMEM; |
| 4998 | goto kfree_obj; |
| 4999 | } |
| 5000 | #ifdef CONFIG_X86 |
| 5001 | set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); |
| 5002 | #endif |
| 5003 | |
| 5004 | dev_priv->mm.phys_objs[id - 1] = phys_obj; |
| 5005 | |
| 5006 | return 0; |
| 5007 | kfree_obj: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 5008 | kfree(phys_obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5009 | return ret; |
| 5010 | } |
| 5011 | |
Chris Wilson | 995b6762 | 2010-08-20 13:23:26 +0100 | [diff] [blame] | 5012 | static void i915_gem_free_phys_object(struct drm_device *dev, int id) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5013 | { |
| 5014 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 5015 | struct drm_i915_gem_phys_object *phys_obj; |
| 5016 | |
| 5017 | if (!dev_priv->mm.phys_objs[id - 1]) |
| 5018 | return; |
| 5019 | |
| 5020 | phys_obj = dev_priv->mm.phys_objs[id - 1]; |
| 5021 | if (phys_obj->cur_obj) { |
| 5022 | i915_gem_detach_phys_object(dev, phys_obj->cur_obj); |
| 5023 | } |
| 5024 | |
| 5025 | #ifdef CONFIG_X86 |
| 5026 | set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); |
| 5027 | #endif |
| 5028 | drm_pci_free(dev, phys_obj->handle); |
| 5029 | kfree(phys_obj); |
| 5030 | dev_priv->mm.phys_objs[id - 1] = NULL; |
| 5031 | } |
| 5032 | |
| 5033 | void i915_gem_free_all_phys_object(struct drm_device *dev) |
| 5034 | { |
| 5035 | int i; |
| 5036 | |
Dave Airlie | 260883c | 2009-01-22 17:58:49 +1000 | [diff] [blame] | 5037 | for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5038 | i915_gem_free_phys_object(dev, i); |
| 5039 | } |
| 5040 | |
| 5041 | void i915_gem_detach_phys_object(struct drm_device *dev, |
| 5042 | struct drm_gem_object *obj) |
| 5043 | { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5044 | struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping; |
| 5045 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
| 5046 | char *vaddr; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5047 | int i; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5048 | int page_count; |
| 5049 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5050 | if (!obj_priv->phys_obj) |
| 5051 | return; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5052 | vaddr = obj_priv->phys_obj->handle->vaddr; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5053 | |
| 5054 | page_count = obj->size / PAGE_SIZE; |
| 5055 | |
| 5056 | for (i = 0; i < page_count; i++) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5057 | struct page *page = read_cache_page_gfp(mapping, i, |
| 5058 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 5059 | if (!IS_ERR(page)) { |
| 5060 | char *dst = kmap_atomic(page); |
| 5061 | memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE); |
| 5062 | kunmap_atomic(dst); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5063 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5064 | drm_clflush_pages(&page, 1); |
| 5065 | |
| 5066 | set_page_dirty(page); |
| 5067 | mark_page_accessed(page); |
| 5068 | page_cache_release(page); |
| 5069 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5070 | } |
Daniel Vetter | 40ce657 | 2010-11-05 18:12:18 +0100 | [diff] [blame^] | 5071 | intel_gtt_chipset_flush(); |
Chris Wilson | d78b47b | 2009-06-17 21:52:49 +0100 | [diff] [blame] | 5072 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5073 | obj_priv->phys_obj->cur_obj = NULL; |
| 5074 | obj_priv->phys_obj = NULL; |
| 5075 | } |
| 5076 | |
| 5077 | int |
| 5078 | i915_gem_attach_phys_object(struct drm_device *dev, |
Chris Wilson | 6eeefaf | 2010-08-07 11:01:39 +0100 | [diff] [blame] | 5079 | struct drm_gem_object *obj, |
| 5080 | int id, |
| 5081 | int align) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5082 | { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5083 | struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5084 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 5085 | struct drm_i915_gem_object *obj_priv; |
| 5086 | int ret = 0; |
| 5087 | int page_count; |
| 5088 | int i; |
| 5089 | |
| 5090 | if (id > I915_MAX_PHYS_OBJECT) |
| 5091 | return -EINVAL; |
| 5092 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 5093 | obj_priv = to_intel_bo(obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5094 | |
| 5095 | if (obj_priv->phys_obj) { |
| 5096 | if (obj_priv->phys_obj->id == id) |
| 5097 | return 0; |
| 5098 | i915_gem_detach_phys_object(dev, obj); |
| 5099 | } |
| 5100 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5101 | /* create a new object */ |
| 5102 | if (!dev_priv->mm.phys_objs[id - 1]) { |
| 5103 | ret = i915_gem_init_phys_object(dev, id, |
Chris Wilson | 6eeefaf | 2010-08-07 11:01:39 +0100 | [diff] [blame] | 5104 | obj->size, align); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5105 | if (ret) { |
Linus Torvalds | aeb565d | 2009-01-26 10:01:53 -0800 | [diff] [blame] | 5106 | DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5107 | return ret; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5108 | } |
| 5109 | } |
| 5110 | |
| 5111 | /* bind to the object */ |
| 5112 | obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1]; |
| 5113 | obj_priv->phys_obj->cur_obj = obj; |
| 5114 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5115 | page_count = obj->size / PAGE_SIZE; |
| 5116 | |
| 5117 | for (i = 0; i < page_count; i++) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5118 | struct page *page; |
| 5119 | char *dst, *src; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5120 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5121 | page = read_cache_page_gfp(mapping, i, |
| 5122 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 5123 | if (IS_ERR(page)) |
| 5124 | return PTR_ERR(page); |
| 5125 | |
Chris Wilson | ff75b9b | 2010-10-30 22:52:31 +0100 | [diff] [blame] | 5126 | src = kmap_atomic(page); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5127 | dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5128 | memcpy(dst, src, PAGE_SIZE); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 5129 | kunmap_atomic(src); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5130 | |
| 5131 | mark_page_accessed(page); |
| 5132 | page_cache_release(page); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5133 | } |
| 5134 | |
| 5135 | return 0; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5136 | } |
| 5137 | |
| 5138 | static int |
| 5139 | i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj, |
| 5140 | struct drm_i915_gem_pwrite *args, |
| 5141 | struct drm_file *file_priv) |
| 5142 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 5143 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | b47b30c | 2010-11-08 01:12:29 +0000 | [diff] [blame] | 5144 | void *vaddr = obj_priv->phys_obj->handle->vaddr + args->offset; |
| 5145 | char __user *user_data = (char __user *) (uintptr_t) args->data_ptr; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5146 | |
Chris Wilson | b47b30c | 2010-11-08 01:12:29 +0000 | [diff] [blame] | 5147 | DRM_DEBUG_DRIVER("vaddr %p, %lld\n", vaddr, args->size); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5148 | |
Chris Wilson | b47b30c | 2010-11-08 01:12:29 +0000 | [diff] [blame] | 5149 | if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) { |
| 5150 | unsigned long unwritten; |
| 5151 | |
| 5152 | /* The physical object once assigned is fixed for the lifetime |
| 5153 | * of the obj, so we can safely drop the lock and continue |
| 5154 | * to access vaddr. |
| 5155 | */ |
| 5156 | mutex_unlock(&dev->struct_mutex); |
| 5157 | unwritten = copy_from_user(vaddr, user_data, args->size); |
| 5158 | mutex_lock(&dev->struct_mutex); |
| 5159 | if (unwritten) |
| 5160 | return -EFAULT; |
| 5161 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5162 | |
Daniel Vetter | 40ce657 | 2010-11-05 18:12:18 +0100 | [diff] [blame^] | 5163 | intel_gtt_chipset_flush(); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5164 | return 0; |
| 5165 | } |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5166 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5167 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5168 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5169 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5170 | |
| 5171 | /* Clean up our request list when the client is going away, so that |
| 5172 | * later retire_requests won't dereference our soon-to-be-gone |
| 5173 | * file_priv. |
| 5174 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5175 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5176 | while (!list_empty(&file_priv->mm.request_list)) { |
| 5177 | struct drm_i915_gem_request *request; |
| 5178 | |
| 5179 | request = list_first_entry(&file_priv->mm.request_list, |
| 5180 | struct drm_i915_gem_request, |
| 5181 | client_list); |
| 5182 | list_del(&request->client_list); |
| 5183 | request->file_priv = NULL; |
| 5184 | } |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5185 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5186 | } |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5187 | |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5188 | static int |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5189 | i915_gpu_is_active(struct drm_device *dev) |
| 5190 | { |
| 5191 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 5192 | int lists_empty; |
| 5193 | |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5194 | lists_empty = list_empty(&dev_priv->mm.flushing_list) && |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5195 | list_empty(&dev_priv->mm.active_list); |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5196 | |
| 5197 | return !lists_empty; |
| 5198 | } |
| 5199 | |
| 5200 | static int |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5201 | i915_gem_inactive_shrink(struct shrinker *shrinker, |
| 5202 | int nr_to_scan, |
| 5203 | gfp_t gfp_mask) |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5204 | { |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5205 | struct drm_i915_private *dev_priv = |
| 5206 | container_of(shrinker, |
| 5207 | struct drm_i915_private, |
| 5208 | mm.inactive_shrinker); |
| 5209 | struct drm_device *dev = dev_priv->dev; |
| 5210 | struct drm_i915_gem_object *obj, *next; |
| 5211 | int cnt; |
| 5212 | |
| 5213 | if (!mutex_trylock(&dev->struct_mutex)) |
Chris Wilson | bbe2e11 | 2010-10-28 22:35:07 +0100 | [diff] [blame] | 5214 | return 0; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5215 | |
| 5216 | /* "fast-path" to count number of available objects */ |
| 5217 | if (nr_to_scan == 0) { |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5218 | cnt = 0; |
| 5219 | list_for_each_entry(obj, |
| 5220 | &dev_priv->mm.inactive_list, |
| 5221 | mm_list) |
| 5222 | cnt++; |
| 5223 | mutex_unlock(&dev->struct_mutex); |
| 5224 | return cnt / 100 * sysctl_vfs_cache_pressure; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5225 | } |
| 5226 | |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5227 | rescan: |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5228 | /* first scan for clean buffers */ |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5229 | i915_gem_retire_requests(dev); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5230 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5231 | list_for_each_entry_safe(obj, next, |
| 5232 | &dev_priv->mm.inactive_list, |
| 5233 | mm_list) { |
| 5234 | if (i915_gem_object_is_purgeable(obj)) { |
| 5235 | i915_gem_object_unbind(&obj->base); |
| 5236 | if (--nr_to_scan == 0) |
| 5237 | break; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5238 | } |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5239 | } |
| 5240 | |
| 5241 | /* second pass, evict/count anything still on the inactive list */ |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5242 | cnt = 0; |
| 5243 | list_for_each_entry_safe(obj, next, |
| 5244 | &dev_priv->mm.inactive_list, |
| 5245 | mm_list) { |
| 5246 | if (nr_to_scan) { |
| 5247 | i915_gem_object_unbind(&obj->base); |
| 5248 | nr_to_scan--; |
| 5249 | } else |
| 5250 | cnt++; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5251 | } |
| 5252 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5253 | if (nr_to_scan && i915_gpu_is_active(dev)) { |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5254 | /* |
| 5255 | * We are desperate for pages, so as a last resort, wait |
| 5256 | * for the GPU to finish and discard whatever we can. |
| 5257 | * This has a dramatic impact to reduce the number of |
| 5258 | * OOM-killer events whilst running the GPU aggressively. |
| 5259 | */ |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5260 | if (i915_gpu_idle(dev) == 0) |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5261 | goto rescan; |
| 5262 | } |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5263 | mutex_unlock(&dev->struct_mutex); |
| 5264 | return cnt / 100 * sysctl_vfs_cache_pressure; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5265 | } |