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