Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2008 Intel Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | * |
| 23 | * Authors: |
| 24 | * Eric Anholt <eric@anholt.net> |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #include "drmP.h" |
| 29 | #include "drm.h" |
| 30 | #include "i915_drm.h" |
| 31 | #include "i915_drv.h" |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 32 | #include "i915_trace.h" |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 33 | #include "intel_drv.h" |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 35 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 36 | #include <linux/pci.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 37 | |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 38 | struct change_domains { |
| 39 | uint32_t invalidate_domains; |
| 40 | uint32_t flush_domains; |
| 41 | uint32_t flush_rings; |
| 42 | }; |
| 43 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 44 | static int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj, |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 45 | struct intel_ring_buffer *pipelined); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 46 | static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj); |
| 47 | static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj); |
| 48 | static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 49 | bool write); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 50 | static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj, |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 51 | uint64_t offset, |
| 52 | uint64_t size); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 53 | static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj); |
| 54 | static int i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj, |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 55 | bool interruptible); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 56 | static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 57 | unsigned alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 58 | bool map_and_fenceable); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 59 | static void i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj); |
| 60 | static int i915_gem_phys_pwrite(struct drm_device *dev, |
| 61 | struct drm_i915_gem_object *obj, |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 62 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 63 | struct drm_file *file); |
| 64 | static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 65 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 66 | static int i915_gem_inactive_shrink(struct shrinker *shrinker, |
| 67 | int nr_to_scan, |
| 68 | gfp_t gfp_mask); |
| 69 | |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 70 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 71 | /* some bookkeeping */ |
| 72 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, |
| 73 | size_t size) |
| 74 | { |
| 75 | dev_priv->mm.object_count++; |
| 76 | dev_priv->mm.object_memory += size; |
| 77 | } |
| 78 | |
| 79 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, |
| 80 | size_t size) |
| 81 | { |
| 82 | dev_priv->mm.object_count--; |
| 83 | dev_priv->mm.object_memory -= size; |
| 84 | } |
| 85 | |
| 86 | 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] | 87 | struct drm_i915_gem_object *obj) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 88 | { |
| 89 | dev_priv->mm.gtt_count++; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 90 | dev_priv->mm.gtt_memory += obj->gtt_space->size; |
| 91 | if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 92 | dev_priv->mm.mappable_gtt_used += |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 93 | min_t(size_t, obj->gtt_space->size, |
| 94 | dev_priv->mm.gtt_mappable_end - obj->gtt_offset); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 95 | } |
Daniel Vetter | 93a37f2 | 2010-11-05 20:24:53 +0100 | [diff] [blame] | 96 | list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | 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] | 100 | struct drm_i915_gem_object *obj) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 101 | { |
| 102 | dev_priv->mm.gtt_count--; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 103 | dev_priv->mm.gtt_memory -= obj->gtt_space->size; |
| 104 | if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 105 | dev_priv->mm.mappable_gtt_used -= |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 106 | min_t(size_t, obj->gtt_space->size, |
| 107 | dev_priv->mm.gtt_mappable_end - obj->gtt_offset); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 108 | } |
Daniel Vetter | 93a37f2 | 2010-11-05 20:24:53 +0100 | [diff] [blame] | 109 | list_del_init(&obj->gtt_list); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Update the mappable working set counters. Call _only_ when there is a change |
| 114 | * in one of (pin|fault)_mappable and update *_mappable _before_ calling. |
| 115 | * @mappable: new state the changed mappable flag (either pin_ or fault_). |
| 116 | */ |
| 117 | static void |
| 118 | i915_gem_info_update_mappable(struct drm_i915_private *dev_priv, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 119 | struct drm_i915_gem_object *obj, |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 120 | bool mappable) |
| 121 | { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 122 | if (mappable) { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 123 | if (obj->pin_mappable && obj->fault_mappable) |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 124 | /* Combined state was already mappable. */ |
| 125 | return; |
| 126 | dev_priv->mm.gtt_mappable_count++; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 127 | dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 128 | } else { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 129 | if (obj->pin_mappable || obj->fault_mappable) |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 130 | /* Combined state still mappable. */ |
| 131 | return; |
| 132 | dev_priv->mm.gtt_mappable_count--; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 133 | dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 134 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 138 | struct drm_i915_gem_object *obj, |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 139 | bool mappable) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 140 | { |
| 141 | dev_priv->mm.pin_count++; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 142 | dev_priv->mm.pin_memory += obj->gtt_space->size; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 143 | if (mappable) { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 144 | obj->pin_mappable = true; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 145 | i915_gem_info_update_mappable(dev_priv, obj, true); |
| 146 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 150 | struct drm_i915_gem_object *obj) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 151 | { |
| 152 | dev_priv->mm.pin_count--; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 153 | dev_priv->mm.pin_memory -= obj->gtt_space->size; |
| 154 | if (obj->pin_mappable) { |
| 155 | obj->pin_mappable = false; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 156 | i915_gem_info_update_mappable(dev_priv, obj, false); |
| 157 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 160 | int |
| 161 | i915_gem_check_is_wedged(struct drm_device *dev) |
| 162 | { |
| 163 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 164 | struct completion *x = &dev_priv->error_completion; |
| 165 | unsigned long flags; |
| 166 | int ret; |
| 167 | |
| 168 | if (!atomic_read(&dev_priv->mm.wedged)) |
| 169 | return 0; |
| 170 | |
| 171 | ret = wait_for_completion_interruptible(x); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
| 175 | /* Success, we reset the GPU! */ |
| 176 | if (!atomic_read(&dev_priv->mm.wedged)) |
| 177 | return 0; |
| 178 | |
| 179 | /* GPU is hung, bump the completion count to account for |
| 180 | * the token we just consumed so that we never hit zero and |
| 181 | * end up waiting upon a subsequent completion event that |
| 182 | * will never happen. |
| 183 | */ |
| 184 | spin_lock_irqsave(&x->wait.lock, flags); |
| 185 | x->done++; |
| 186 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 187 | return -EIO; |
| 188 | } |
| 189 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 190 | static int i915_mutex_lock_interruptible(struct drm_device *dev) |
| 191 | { |
| 192 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 193 | int ret; |
| 194 | |
| 195 | ret = i915_gem_check_is_wedged(dev); |
| 196 | if (ret) |
| 197 | return ret; |
| 198 | |
| 199 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 200 | if (ret) |
| 201 | return ret; |
| 202 | |
| 203 | if (atomic_read(&dev_priv->mm.wedged)) { |
| 204 | mutex_unlock(&dev->struct_mutex); |
| 205 | return -EAGAIN; |
| 206 | } |
| 207 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 208 | WARN_ON(i915_verify_lists(dev)); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 209 | return 0; |
| 210 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 211 | |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 212 | static inline bool |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 213 | i915_gem_object_is_inactive(struct drm_i915_gem_object *obj) |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 214 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 215 | return obj->gtt_space && !obj->active && obj->pin_count == 0; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 216 | } |
| 217 | |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame^] | 218 | void i915_gem_do_init(struct drm_device *dev, |
| 219 | unsigned long start, |
| 220 | unsigned long mappable_end, |
| 221 | unsigned long end) |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 222 | { |
| 223 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 224 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 225 | drm_mm_init(&dev_priv->mm.gtt_space, start, |
| 226 | end - start); |
| 227 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 228 | dev_priv->mm.gtt_total = end - start; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 229 | dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start; |
Daniel Vetter | 5398463 | 2010-09-22 23:44:24 +0200 | [diff] [blame] | 230 | dev_priv->mm.gtt_mappable_end = mappable_end; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 231 | } |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 232 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 233 | int |
| 234 | i915_gem_init_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 235 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 236 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 237 | struct drm_i915_gem_init *args = data; |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame^] | 238 | |
| 239 | if (args->gtt_start >= args->gtt_end || |
| 240 | (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1)) |
| 241 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 242 | |
| 243 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame^] | 244 | 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] | 245 | mutex_unlock(&dev->struct_mutex); |
| 246 | |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame^] | 247 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 250 | int |
| 251 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 252 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 253 | { |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 254 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 255 | struct drm_i915_gem_get_aperture *args = data; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 256 | |
| 257 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 258 | return -ENODEV; |
| 259 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 260 | mutex_lock(&dev->struct_mutex); |
| 261 | args->aper_size = dev_priv->mm.gtt_total; |
| 262 | args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory; |
| 263 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 268 | |
| 269 | /** |
| 270 | * Creates a new mm object and returns a handle to it. |
| 271 | */ |
| 272 | int |
| 273 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 274 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 275 | { |
| 276 | struct drm_i915_gem_create *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 277 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 278 | int ret; |
| 279 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 280 | |
| 281 | args->size = roundup(args->size, PAGE_SIZE); |
| 282 | |
| 283 | /* Allocate the new object */ |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 284 | obj = i915_gem_alloc_object(dev, args->size); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 285 | if (obj == NULL) |
| 286 | return -ENOMEM; |
| 287 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 288 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 1dfd975 | 2010-09-06 14:44:14 +0100 | [diff] [blame] | 289 | if (ret) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 290 | drm_gem_object_release(&obj->base); |
| 291 | i915_gem_info_remove_obj(dev->dev_private, obj->base.size); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 292 | kfree(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 293 | return ret; |
Chris Wilson | 1dfd975 | 2010-09-06 14:44:14 +0100 | [diff] [blame] | 294 | } |
| 295 | |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 296 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 297 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 298 | trace_i915_gem_object_create(obj); |
| 299 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 300 | args->handle = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 301 | return 0; |
| 302 | } |
| 303 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 304 | static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj) |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 305 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 306 | drm_i915_private_t *dev_priv = obj->base.dev->dev_private; |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 307 | |
| 308 | return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 && |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 309 | obj->tiling_mode != I915_TILING_NONE; |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 312 | static inline void |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 313 | slow_shmem_copy(struct page *dst_page, |
| 314 | int dst_offset, |
| 315 | struct page *src_page, |
| 316 | int src_offset, |
| 317 | int length) |
| 318 | { |
| 319 | char *dst_vaddr, *src_vaddr; |
| 320 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 321 | dst_vaddr = kmap(dst_page); |
| 322 | src_vaddr = kmap(src_page); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 323 | |
| 324 | memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length); |
| 325 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 326 | kunmap(src_page); |
| 327 | kunmap(dst_page); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 330 | static inline void |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 331 | slow_shmem_bit17_copy(struct page *gpu_page, |
| 332 | int gpu_offset, |
| 333 | struct page *cpu_page, |
| 334 | int cpu_offset, |
| 335 | int length, |
| 336 | int is_read) |
| 337 | { |
| 338 | char *gpu_vaddr, *cpu_vaddr; |
| 339 | |
| 340 | /* Use the unswizzled path if this page isn't affected. */ |
| 341 | if ((page_to_phys(gpu_page) & (1 << 17)) == 0) { |
| 342 | if (is_read) |
| 343 | return slow_shmem_copy(cpu_page, cpu_offset, |
| 344 | gpu_page, gpu_offset, length); |
| 345 | else |
| 346 | return slow_shmem_copy(gpu_page, gpu_offset, |
| 347 | cpu_page, cpu_offset, length); |
| 348 | } |
| 349 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 350 | gpu_vaddr = kmap(gpu_page); |
| 351 | cpu_vaddr = kmap(cpu_page); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 352 | |
| 353 | /* Copy the data, XORing A6 with A17 (1). The user already knows he's |
| 354 | * XORing with the other bits (A9 for Y, A9 and A10 for X) |
| 355 | */ |
| 356 | while (length > 0) { |
| 357 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 358 | int this_length = min(cacheline_end - gpu_offset, length); |
| 359 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 360 | |
| 361 | if (is_read) { |
| 362 | memcpy(cpu_vaddr + cpu_offset, |
| 363 | gpu_vaddr + swizzled_gpu_offset, |
| 364 | this_length); |
| 365 | } else { |
| 366 | memcpy(gpu_vaddr + swizzled_gpu_offset, |
| 367 | cpu_vaddr + cpu_offset, |
| 368 | this_length); |
| 369 | } |
| 370 | cpu_offset += this_length; |
| 371 | gpu_offset += this_length; |
| 372 | length -= this_length; |
| 373 | } |
| 374 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 375 | kunmap(cpu_page); |
| 376 | kunmap(gpu_page); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 379 | /** |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 380 | * This is the fast shmem pread path, which attempts to copy_from_user directly |
| 381 | * from the backing pages of the object to the user's address space. On a |
| 382 | * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow(). |
| 383 | */ |
| 384 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 385 | i915_gem_shmem_pread_fast(struct drm_device *dev, |
| 386 | struct drm_i915_gem_object *obj, |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 387 | struct drm_i915_gem_pread *args, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 388 | struct drm_file *file) |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 389 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 390 | struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 391 | ssize_t remain; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 392 | loff_t offset; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 393 | char __user *user_data; |
| 394 | int page_offset, page_length; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 395 | |
| 396 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 397 | remain = args->size; |
| 398 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 399 | offset = args->offset; |
| 400 | |
| 401 | while (remain > 0) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 402 | struct page *page; |
| 403 | char *vaddr; |
| 404 | int ret; |
| 405 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 406 | /* Operation in this page |
| 407 | * |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 408 | * page_offset = offset within page |
| 409 | * page_length = bytes to copy for this page |
| 410 | */ |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 411 | page_offset = offset & (PAGE_SIZE-1); |
| 412 | page_length = remain; |
| 413 | if ((page_offset + remain) > PAGE_SIZE) |
| 414 | page_length = PAGE_SIZE - page_offset; |
| 415 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 416 | page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT, |
| 417 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 418 | if (IS_ERR(page)) |
| 419 | return PTR_ERR(page); |
| 420 | |
| 421 | vaddr = kmap_atomic(page); |
| 422 | ret = __copy_to_user_inatomic(user_data, |
| 423 | vaddr + page_offset, |
| 424 | page_length); |
| 425 | kunmap_atomic(vaddr); |
| 426 | |
| 427 | mark_page_accessed(page); |
| 428 | page_cache_release(page); |
| 429 | if (ret) |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 430 | return -EFAULT; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 431 | |
| 432 | remain -= page_length; |
| 433 | user_data += page_length; |
| 434 | offset += page_length; |
| 435 | } |
| 436 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 437 | return 0; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | /** |
| 441 | * This is the fallback shmem pread path, which allocates temporary storage |
| 442 | * in kernel space to copy_to_user into outside of the struct_mutex, so we |
| 443 | * can copy out of the object's backing pages while holding the struct mutex |
| 444 | * and not take page faults. |
| 445 | */ |
| 446 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 447 | i915_gem_shmem_pread_slow(struct drm_device *dev, |
| 448 | struct drm_i915_gem_object *obj, |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 449 | struct drm_i915_gem_pread *args, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 450 | struct drm_file *file) |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 451 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 452 | struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 453 | struct mm_struct *mm = current->mm; |
| 454 | struct page **user_pages; |
| 455 | ssize_t remain; |
| 456 | loff_t offset, pinned_pages, i; |
| 457 | loff_t first_data_page, last_data_page, num_pages; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 458 | int shmem_page_offset; |
| 459 | int data_page_index, data_page_offset; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 460 | int page_length; |
| 461 | int ret; |
| 462 | uint64_t data_ptr = args->data_ptr; |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 463 | int do_bit17_swizzling; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 464 | |
| 465 | remain = args->size; |
| 466 | |
| 467 | /* Pin the user pages containing the data. We can't fault while |
| 468 | * holding the struct mutex, yet we want to hold it while |
| 469 | * dereferencing the user data. |
| 470 | */ |
| 471 | first_data_page = data_ptr / PAGE_SIZE; |
| 472 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 473 | num_pages = last_data_page - first_data_page + 1; |
| 474 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 475 | user_pages = drm_malloc_ab(num_pages, sizeof(struct page *)); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 476 | if (user_pages == NULL) |
| 477 | return -ENOMEM; |
| 478 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 479 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 480 | down_read(&mm->mmap_sem); |
| 481 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
Eric Anholt | e5e9ecd | 2009-04-07 16:01:22 -0700 | [diff] [blame] | 482 | num_pages, 1, 0, user_pages, NULL); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 483 | up_read(&mm->mmap_sem); |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 484 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 485 | if (pinned_pages < num_pages) { |
| 486 | ret = -EFAULT; |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 487 | goto out; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 490 | ret = i915_gem_object_set_cpu_read_domain_range(obj, |
| 491 | args->offset, |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 492 | args->size); |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 493 | if (ret) |
| 494 | goto out; |
| 495 | |
| 496 | do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 497 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 498 | offset = args->offset; |
| 499 | |
| 500 | while (remain > 0) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 501 | struct page *page; |
| 502 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 503 | /* Operation in this page |
| 504 | * |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 505 | * shmem_page_offset = offset within page in shmem file |
| 506 | * data_page_index = page number in get_user_pages return |
| 507 | * data_page_offset = offset with data_page_index page. |
| 508 | * page_length = bytes to copy for this page |
| 509 | */ |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 510 | shmem_page_offset = offset & ~PAGE_MASK; |
| 511 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 512 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 513 | |
| 514 | page_length = remain; |
| 515 | if ((shmem_page_offset + page_length) > PAGE_SIZE) |
| 516 | page_length = PAGE_SIZE - shmem_page_offset; |
| 517 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 518 | page_length = PAGE_SIZE - data_page_offset; |
| 519 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 520 | page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT, |
| 521 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 522 | if (IS_ERR(page)) |
| 523 | return PTR_ERR(page); |
| 524 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 525 | if (do_bit17_swizzling) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 526 | slow_shmem_bit17_copy(page, |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 527 | shmem_page_offset, |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 528 | user_pages[data_page_index], |
| 529 | data_page_offset, |
| 530 | page_length, |
| 531 | 1); |
| 532 | } else { |
| 533 | slow_shmem_copy(user_pages[data_page_index], |
| 534 | data_page_offset, |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 535 | page, |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 536 | shmem_page_offset, |
| 537 | page_length); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 538 | } |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 539 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 540 | mark_page_accessed(page); |
| 541 | page_cache_release(page); |
| 542 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 543 | remain -= page_length; |
| 544 | data_ptr += page_length; |
| 545 | offset += page_length; |
| 546 | } |
| 547 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 548 | out: |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 549 | for (i = 0; i < pinned_pages; i++) { |
| 550 | SetPageDirty(user_pages[i]); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 551 | mark_page_accessed(user_pages[i]); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 552 | page_cache_release(user_pages[i]); |
| 553 | } |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 554 | drm_free_large(user_pages); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 555 | |
| 556 | return ret; |
| 557 | } |
| 558 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 559 | /** |
| 560 | * Reads data from the object referenced by handle. |
| 561 | * |
| 562 | * On error, the contents of *data are undefined. |
| 563 | */ |
| 564 | int |
| 565 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 566 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 567 | { |
| 568 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 569 | struct drm_i915_gem_object *obj; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 570 | int ret = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 571 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 572 | if (args->size == 0) |
| 573 | return 0; |
| 574 | |
| 575 | if (!access_ok(VERIFY_WRITE, |
| 576 | (char __user *)(uintptr_t)args->data_ptr, |
| 577 | args->size)) |
| 578 | return -EFAULT; |
| 579 | |
| 580 | ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr, |
| 581 | args->size); |
| 582 | if (ret) |
| 583 | return -EFAULT; |
| 584 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 585 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 586 | if (ret) |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 587 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 588 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 589 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 590 | if (obj == NULL) { |
| 591 | ret = -ENOENT; |
| 592 | goto unlock; |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 593 | } |
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. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 596 | if (args->offset > obj->base.size || |
| 597 | args->size > obj->base.size - args->offset) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 598 | ret = -EINVAL; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 599 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 600 | } |
| 601 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 602 | ret = i915_gem_object_set_cpu_read_domain_range(obj, |
| 603 | args->offset, |
| 604 | args->size); |
| 605 | if (ret) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 606 | goto out; |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 607 | |
| 608 | ret = -EFAULT; |
| 609 | if (!i915_gem_object_needs_bit17_swizzle(obj)) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 610 | ret = i915_gem_shmem_pread_fast(dev, obj, args, file); |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 611 | if (ret == -EFAULT) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 612 | ret = i915_gem_shmem_pread_slow(dev, obj, args, file); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 613 | |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 614 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 615 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 616 | unlock: |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 617 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 618 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 621 | /* This is the fast write path which cannot handle |
| 622 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 623 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 624 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 625 | static inline int |
| 626 | fast_user_write(struct io_mapping *mapping, |
| 627 | loff_t page_base, int page_offset, |
| 628 | char __user *user_data, |
| 629 | int length) |
| 630 | { |
| 631 | char *vaddr_atomic; |
| 632 | unsigned long unwritten; |
| 633 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 634 | vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base); |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 635 | unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset, |
| 636 | user_data, length); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 637 | io_mapping_unmap_atomic(vaddr_atomic); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 638 | return unwritten; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | /* Here's the write path which can sleep for |
| 642 | * page faults |
| 643 | */ |
| 644 | |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 645 | static inline void |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 646 | slow_kernel_write(struct io_mapping *mapping, |
| 647 | loff_t gtt_base, int gtt_offset, |
| 648 | struct page *user_page, int user_offset, |
| 649 | int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 650 | { |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 651 | char __iomem *dst_vaddr; |
| 652 | char *src_vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 653 | |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 654 | dst_vaddr = io_mapping_map_wc(mapping, gtt_base); |
| 655 | src_vaddr = kmap(user_page); |
| 656 | |
| 657 | memcpy_toio(dst_vaddr + gtt_offset, |
| 658 | src_vaddr + user_offset, |
| 659 | length); |
| 660 | |
| 661 | kunmap(user_page); |
| 662 | io_mapping_unmap(dst_vaddr); |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 665 | /** |
| 666 | * This is the fast pwrite path, where we copy the data directly from the |
| 667 | * user into the GTT, uncached. |
| 668 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 669 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 670 | i915_gem_gtt_pwrite_fast(struct drm_device *dev, |
| 671 | struct drm_i915_gem_object *obj, |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 672 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 673 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 674 | { |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 675 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 676 | ssize_t remain; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 677 | loff_t offset, page_base; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 678 | char __user *user_data; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 679 | int page_offset, page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 680 | |
| 681 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 682 | remain = args->size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 683 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 684 | offset = obj->gtt_offset + args->offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 685 | |
| 686 | while (remain > 0) { |
| 687 | /* Operation in this page |
| 688 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 689 | * page_base = page offset within aperture |
| 690 | * page_offset = offset within page |
| 691 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 692 | */ |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 693 | page_base = (offset & ~(PAGE_SIZE-1)); |
| 694 | page_offset = offset & (PAGE_SIZE-1); |
| 695 | page_length = remain; |
| 696 | if ((page_offset + remain) > PAGE_SIZE) |
| 697 | page_length = PAGE_SIZE - page_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 698 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 699 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 700 | * source page isn't available. Return the error and we'll |
| 701 | * retry in the slow path. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 702 | */ |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 703 | if (fast_user_write(dev_priv->mm.gtt_mapping, page_base, |
| 704 | page_offset, user_data, page_length)) |
| 705 | |
| 706 | return -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 707 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 708 | remain -= page_length; |
| 709 | user_data += page_length; |
| 710 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 711 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 712 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 713 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 714 | } |
| 715 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 716 | /** |
| 717 | * This is the fallback GTT pwrite path, which uses get_user_pages to pin |
| 718 | * the memory and maps it using kmap_atomic for copying. |
| 719 | * |
| 720 | * This code resulted in x11perf -rgb10text consuming about 10% more CPU |
| 721 | * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit). |
| 722 | */ |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 723 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 724 | i915_gem_gtt_pwrite_slow(struct drm_device *dev, |
| 725 | struct drm_i915_gem_object *obj, |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 726 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 727 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 728 | { |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 729 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 730 | ssize_t remain; |
| 731 | loff_t gtt_page_base, offset; |
| 732 | loff_t first_data_page, last_data_page, num_pages; |
| 733 | loff_t pinned_pages, i; |
| 734 | struct page **user_pages; |
| 735 | struct mm_struct *mm = current->mm; |
| 736 | int gtt_page_offset, data_page_offset, data_page_index, page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 737 | int ret; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 738 | uint64_t data_ptr = args->data_ptr; |
| 739 | |
| 740 | remain = args->size; |
| 741 | |
| 742 | /* Pin the user pages containing the data. We can't fault while |
| 743 | * holding the struct mutex, and all of the pwrite implementations |
| 744 | * want to hold it while dereferencing the user data. |
| 745 | */ |
| 746 | first_data_page = data_ptr / PAGE_SIZE; |
| 747 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 748 | num_pages = last_data_page - first_data_page + 1; |
| 749 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 750 | user_pages = drm_malloc_ab(num_pages, sizeof(struct page *)); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 751 | if (user_pages == NULL) |
| 752 | return -ENOMEM; |
| 753 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 754 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 755 | down_read(&mm->mmap_sem); |
| 756 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
| 757 | num_pages, 0, 0, user_pages, NULL); |
| 758 | up_read(&mm->mmap_sem); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 759 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 760 | if (pinned_pages < num_pages) { |
| 761 | ret = -EFAULT; |
| 762 | goto out_unpin_pages; |
| 763 | } |
| 764 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 765 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
| 766 | if (ret) |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 767 | goto out_unpin_pages; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 768 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 769 | offset = obj->gtt_offset + args->offset; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 770 | |
| 771 | while (remain > 0) { |
| 772 | /* Operation in this page |
| 773 | * |
| 774 | * gtt_page_base = page offset within aperture |
| 775 | * gtt_page_offset = offset within page in aperture |
| 776 | * data_page_index = page number in get_user_pages return |
| 777 | * data_page_offset = offset with data_page_index page. |
| 778 | * page_length = bytes to copy for this page |
| 779 | */ |
| 780 | gtt_page_base = offset & PAGE_MASK; |
| 781 | gtt_page_offset = offset & ~PAGE_MASK; |
| 782 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 783 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 784 | |
| 785 | page_length = remain; |
| 786 | if ((gtt_page_offset + page_length) > PAGE_SIZE) |
| 787 | page_length = PAGE_SIZE - gtt_page_offset; |
| 788 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 789 | page_length = PAGE_SIZE - data_page_offset; |
| 790 | |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 791 | slow_kernel_write(dev_priv->mm.gtt_mapping, |
| 792 | gtt_page_base, gtt_page_offset, |
| 793 | user_pages[data_page_index], |
| 794 | data_page_offset, |
| 795 | page_length); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 796 | |
| 797 | remain -= page_length; |
| 798 | offset += page_length; |
| 799 | data_ptr += page_length; |
| 800 | } |
| 801 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 802 | out_unpin_pages: |
| 803 | for (i = 0; i < pinned_pages; i++) |
| 804 | page_cache_release(user_pages[i]); |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 805 | drm_free_large(user_pages); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 806 | |
| 807 | return ret; |
| 808 | } |
| 809 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 810 | /** |
| 811 | * This is the fast shmem pwrite path, which attempts to directly |
| 812 | * copy_from_user into the kmapped pages backing the object. |
| 813 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 814 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 815 | i915_gem_shmem_pwrite_fast(struct drm_device *dev, |
| 816 | struct drm_i915_gem_object *obj, |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 817 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 818 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 819 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 820 | struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 821 | ssize_t remain; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 822 | loff_t offset; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 823 | char __user *user_data; |
| 824 | int page_offset, page_length; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 825 | |
| 826 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 827 | remain = args->size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 828 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 829 | offset = args->offset; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 830 | obj->dirty = 1; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 831 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 832 | while (remain > 0) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 833 | struct page *page; |
| 834 | char *vaddr; |
| 835 | int ret; |
| 836 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 837 | /* Operation in this page |
| 838 | * |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 839 | * page_offset = offset within page |
| 840 | * page_length = bytes to copy for this page |
| 841 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 842 | page_offset = offset & (PAGE_SIZE-1); |
| 843 | page_length = remain; |
| 844 | if ((page_offset + remain) > PAGE_SIZE) |
| 845 | page_length = PAGE_SIZE - page_offset; |
| 846 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 847 | page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT, |
| 848 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 849 | if (IS_ERR(page)) |
| 850 | return PTR_ERR(page); |
| 851 | |
| 852 | vaddr = kmap_atomic(page, KM_USER0); |
| 853 | ret = __copy_from_user_inatomic(vaddr + page_offset, |
| 854 | user_data, |
| 855 | page_length); |
| 856 | kunmap_atomic(vaddr, KM_USER0); |
| 857 | |
| 858 | set_page_dirty(page); |
| 859 | mark_page_accessed(page); |
| 860 | page_cache_release(page); |
| 861 | |
| 862 | /* If we get a fault while copying data, then (presumably) our |
| 863 | * source page isn't available. Return the error and we'll |
| 864 | * retry in the slow path. |
| 865 | */ |
| 866 | if (ret) |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 867 | return -EFAULT; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 868 | |
| 869 | remain -= page_length; |
| 870 | user_data += page_length; |
| 871 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 874 | return 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /** |
| 878 | * This is the fallback shmem pwrite path, which uses get_user_pages to pin |
| 879 | * the memory and maps it using kmap_atomic for copying. |
| 880 | * |
| 881 | * This avoids taking mmap_sem for faulting on the user's address while the |
| 882 | * struct_mutex is held. |
| 883 | */ |
| 884 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 885 | i915_gem_shmem_pwrite_slow(struct drm_device *dev, |
| 886 | struct drm_i915_gem_object *obj, |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 887 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 888 | struct drm_file *file) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 889 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 890 | struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 891 | struct mm_struct *mm = current->mm; |
| 892 | struct page **user_pages; |
| 893 | ssize_t remain; |
| 894 | loff_t offset, pinned_pages, i; |
| 895 | loff_t first_data_page, last_data_page, num_pages; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 896 | int shmem_page_offset; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 897 | int data_page_index, data_page_offset; |
| 898 | int page_length; |
| 899 | int ret; |
| 900 | uint64_t data_ptr = args->data_ptr; |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 901 | int do_bit17_swizzling; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 902 | |
| 903 | remain = args->size; |
| 904 | |
| 905 | /* Pin the user pages containing the data. We can't fault while |
| 906 | * holding the struct mutex, and all of the pwrite implementations |
| 907 | * want to hold it while dereferencing the user data. |
| 908 | */ |
| 909 | first_data_page = data_ptr / PAGE_SIZE; |
| 910 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 911 | num_pages = last_data_page - first_data_page + 1; |
| 912 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 913 | user_pages = drm_malloc_ab(num_pages, sizeof(struct page *)); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 914 | if (user_pages == NULL) |
| 915 | return -ENOMEM; |
| 916 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 917 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 918 | down_read(&mm->mmap_sem); |
| 919 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
| 920 | num_pages, 0, 0, user_pages, NULL); |
| 921 | up_read(&mm->mmap_sem); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 922 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 923 | if (pinned_pages < num_pages) { |
| 924 | ret = -EFAULT; |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 925 | goto out; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 926 | } |
| 927 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 928 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 929 | if (ret) |
| 930 | goto out; |
| 931 | |
| 932 | do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 933 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 934 | offset = args->offset; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 935 | obj->dirty = 1; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 936 | |
| 937 | while (remain > 0) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 938 | struct page *page; |
| 939 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 940 | /* Operation in this page |
| 941 | * |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 942 | * shmem_page_offset = offset within page in shmem file |
| 943 | * data_page_index = page number in get_user_pages return |
| 944 | * data_page_offset = offset with data_page_index page. |
| 945 | * page_length = bytes to copy for this page |
| 946 | */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 947 | shmem_page_offset = offset & ~PAGE_MASK; |
| 948 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 949 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 950 | |
| 951 | page_length = remain; |
| 952 | if ((shmem_page_offset + page_length) > PAGE_SIZE) |
| 953 | page_length = PAGE_SIZE - shmem_page_offset; |
| 954 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 955 | page_length = PAGE_SIZE - data_page_offset; |
| 956 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 957 | page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT, |
| 958 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 959 | if (IS_ERR(page)) { |
| 960 | ret = PTR_ERR(page); |
| 961 | goto out; |
| 962 | } |
| 963 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 964 | if (do_bit17_swizzling) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 965 | slow_shmem_bit17_copy(page, |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 966 | shmem_page_offset, |
| 967 | user_pages[data_page_index], |
| 968 | data_page_offset, |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 969 | page_length, |
| 970 | 0); |
| 971 | } else { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 972 | slow_shmem_copy(page, |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 973 | shmem_page_offset, |
| 974 | user_pages[data_page_index], |
| 975 | data_page_offset, |
| 976 | page_length); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 977 | } |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 978 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 979 | set_page_dirty(page); |
| 980 | mark_page_accessed(page); |
| 981 | page_cache_release(page); |
| 982 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 983 | remain -= page_length; |
| 984 | data_ptr += page_length; |
| 985 | offset += page_length; |
| 986 | } |
| 987 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 988 | out: |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 989 | for (i = 0; i < pinned_pages; i++) |
| 990 | page_cache_release(user_pages[i]); |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 991 | drm_free_large(user_pages); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 992 | |
| 993 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /** |
| 997 | * Writes data to the object referenced by handle. |
| 998 | * |
| 999 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1000 | */ |
| 1001 | int |
| 1002 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1003 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1004 | { |
| 1005 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1006 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 1007 | int ret; |
| 1008 | |
| 1009 | if (args->size == 0) |
| 1010 | return 0; |
| 1011 | |
| 1012 | if (!access_ok(VERIFY_READ, |
| 1013 | (char __user *)(uintptr_t)args->data_ptr, |
| 1014 | args->size)) |
| 1015 | return -EFAULT; |
| 1016 | |
| 1017 | ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr, |
| 1018 | args->size); |
| 1019 | if (ret) |
| 1020 | return -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1021 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1022 | ret = i915_mutex_lock_interruptible(dev); |
| 1023 | if (ret) |
| 1024 | return ret; |
| 1025 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1026 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1027 | if (obj == NULL) { |
| 1028 | ret = -ENOENT; |
| 1029 | goto unlock; |
| 1030 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1031 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1032 | /* Bounds check destination. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1033 | if (args->offset > obj->base.size || |
| 1034 | args->size > obj->base.size - args->offset) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1035 | ret = -EINVAL; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 1036 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1037 | } |
| 1038 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1039 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1040 | * it would end up going through the fenced access, and we'll get |
| 1041 | * different detiling behavior between reading and writing. |
| 1042 | * pread/pwrite currently are reading and writing from the CPU |
| 1043 | * perspective, requiring manual detiling by the client. |
| 1044 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1045 | if (obj->phys_obj) |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1046 | ret = i915_gem_phys_pwrite(dev, obj, args, file); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1047 | else if (obj->tiling_mode == I915_TILING_NONE && |
| 1048 | obj->gtt_space && |
| 1049 | obj->base.write_domain != I915_GEM_DOMAIN_CPU) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 1050 | ret = i915_gem_object_pin(obj, 0, true); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1051 | if (ret) |
| 1052 | goto out; |
| 1053 | |
| 1054 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
| 1055 | if (ret) |
| 1056 | goto out_unpin; |
| 1057 | |
| 1058 | ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file); |
| 1059 | if (ret == -EFAULT) |
| 1060 | ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file); |
| 1061 | |
| 1062 | out_unpin: |
| 1063 | i915_gem_object_unpin(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1064 | } else { |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1065 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
| 1066 | if (ret) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1067 | goto out; |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1068 | |
| 1069 | ret = -EFAULT; |
| 1070 | if (!i915_gem_object_needs_bit17_swizzle(obj)) |
| 1071 | ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file); |
| 1072 | if (ret == -EFAULT) |
| 1073 | ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1074 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1075 | |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 1076 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1077 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1078 | unlock: |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1079 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1080 | return ret; |
| 1081 | } |
| 1082 | |
| 1083 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1084 | * Called when user space prepares to use an object with the CPU, either |
| 1085 | * through the mmap ioctl's mapping or a GTT mapping. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1086 | */ |
| 1087 | int |
| 1088 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1089 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1090 | { |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1091 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1092 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1093 | struct drm_i915_gem_object *obj; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1094 | uint32_t read_domains = args->read_domains; |
| 1095 | uint32_t write_domain = args->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1096 | int ret; |
| 1097 | |
| 1098 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1099 | return -ENODEV; |
| 1100 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1101 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | 21d509e | 2009-06-06 09:46:02 +0100 | [diff] [blame] | 1102 | if (write_domain & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1103 | return -EINVAL; |
| 1104 | |
Chris Wilson | 21d509e | 2009-06-06 09:46:02 +0100 | [diff] [blame] | 1105 | if (read_domains & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1106 | return -EINVAL; |
| 1107 | |
| 1108 | /* Having something in the write domain implies it's in the read |
| 1109 | * domain, and only that read domain. Enforce that in the request. |
| 1110 | */ |
| 1111 | if (write_domain != 0 && read_domains != write_domain) |
| 1112 | return -EINVAL; |
| 1113 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1114 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1115 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1116 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1117 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1118 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1119 | if (obj == NULL) { |
| 1120 | ret = -ENOENT; |
| 1121 | goto unlock; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1122 | } |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1123 | |
| 1124 | intel_mark_busy(dev, obj); |
| 1125 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1126 | if (read_domains & I915_GEM_DOMAIN_GTT) { |
| 1127 | ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0); |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 1128 | |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1129 | /* Update the LRU on the fence for the CPU access that's |
| 1130 | * about to occur. |
| 1131 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1132 | if (obj->fence_reg != I915_FENCE_REG_NONE) { |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 1133 | struct drm_i915_fence_reg *reg = |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1134 | &dev_priv->fence_regs[obj->fence_reg]; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 1135 | list_move_tail(®->lru_list, |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1136 | &dev_priv->mm.fence_list); |
| 1137 | } |
| 1138 | |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 1139 | /* Silently promote "you're not bound, there was nothing to do" |
| 1140 | * to success, since the client was just asking us to |
| 1141 | * make sure everything was done. |
| 1142 | */ |
| 1143 | if (ret == -EINVAL) |
| 1144 | ret = 0; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1145 | } else { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1146 | ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1147 | } |
| 1148 | |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1149 | /* Maintain LRU order of "inactive" objects */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1150 | if (ret == 0 && i915_gem_object_is_inactive(obj)) |
| 1151 | list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list); |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1152 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1153 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1154 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1155 | mutex_unlock(&dev->struct_mutex); |
| 1156 | return ret; |
| 1157 | } |
| 1158 | |
| 1159 | /** |
| 1160 | * Called when user space has done writes to this buffer |
| 1161 | */ |
| 1162 | int |
| 1163 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1164 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1165 | { |
| 1166 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1167 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1168 | int ret = 0; |
| 1169 | |
| 1170 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1171 | return -ENODEV; |
| 1172 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1173 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1174 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1175 | return ret; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1176 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1177 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1178 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1179 | ret = -ENOENT; |
| 1180 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1183 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1184 | if (obj->pin_count) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1185 | i915_gem_object_flush_cpu_write_domain(obj); |
| 1186 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1187 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1188 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1189 | mutex_unlock(&dev->struct_mutex); |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | /** |
| 1194 | * Maps the contents of an object, returning the address it is mapped |
| 1195 | * into. |
| 1196 | * |
| 1197 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1198 | * imply a ref on the object itself. |
| 1199 | */ |
| 1200 | int |
| 1201 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1202 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1203 | { |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1204 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1205 | struct drm_i915_gem_mmap *args = data; |
| 1206 | struct drm_gem_object *obj; |
| 1207 | loff_t offset; |
| 1208 | unsigned long addr; |
| 1209 | |
| 1210 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1211 | return -ENODEV; |
| 1212 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1213 | obj = drm_gem_object_lookup(dev, file, args->handle); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1214 | if (obj == NULL) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1215 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1216 | |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1217 | if (obj->size > dev_priv->mm.gtt_mappable_end) { |
| 1218 | drm_gem_object_unreference_unlocked(obj); |
| 1219 | return -E2BIG; |
| 1220 | } |
| 1221 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1222 | offset = args->offset; |
| 1223 | |
| 1224 | down_write(¤t->mm->mmap_sem); |
| 1225 | addr = do_mmap(obj->filp, 0, args->size, |
| 1226 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1227 | args->offset); |
| 1228 | up_write(¤t->mm->mmap_sem); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 1229 | drm_gem_object_unreference_unlocked(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1230 | if (IS_ERR((void *)addr)) |
| 1231 | return addr; |
| 1232 | |
| 1233 | args->addr_ptr = (uint64_t) addr; |
| 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1238 | /** |
| 1239 | * i915_gem_fault - fault a page into the GTT |
| 1240 | * vma: VMA in question |
| 1241 | * vmf: fault info |
| 1242 | * |
| 1243 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1244 | * from userspace. The fault handler takes care of binding the object to |
| 1245 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1246 | * only if needed based on whether the old reg is still valid or the object |
| 1247 | * is tiled) and inserting a new PTE into the faulting process. |
| 1248 | * |
| 1249 | * Note that the faulting process may involve evicting existing objects |
| 1250 | * from the GTT and/or fence registers to make room. So performance may |
| 1251 | * suffer if the GTT working set is large or there are few fence registers |
| 1252 | * left. |
| 1253 | */ |
| 1254 | int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1255 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1256 | struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data); |
| 1257 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1258 | drm_i915_private_t *dev_priv = dev->dev_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1259 | pgoff_t page_offset; |
| 1260 | unsigned long pfn; |
| 1261 | int ret = 0; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1262 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1263 | |
| 1264 | /* We don't use vmf->pgoff since that has the fake offset */ |
| 1265 | page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> |
| 1266 | PAGE_SHIFT; |
| 1267 | |
| 1268 | /* Now bind it into the GTT if needed */ |
| 1269 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1270 | BUG_ON(obj->pin_count && !obj->pin_mappable); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1271 | |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 1272 | if (!obj->map_and_fenceable) { |
| 1273 | ret = i915_gem_object_unbind(obj); |
| 1274 | if (ret) |
| 1275 | goto unlock; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1276 | } |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 1277 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1278 | if (!obj->gtt_space) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 1279 | ret = i915_gem_object_bind_to_gtt(obj, 0, true); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1280 | if (ret) |
| 1281 | goto unlock; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1282 | } |
| 1283 | |
Chris Wilson | 4a684a4 | 2010-10-28 14:44:08 +0100 | [diff] [blame] | 1284 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1285 | if (ret) |
| 1286 | goto unlock; |
| 1287 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1288 | if (!obj->fault_mappable) { |
| 1289 | obj->fault_mappable = true; |
| 1290 | i915_gem_info_update_mappable(dev_priv, obj, true); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1291 | } |
| 1292 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1293 | /* Need a new fence register? */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1294 | if (obj->tiling_mode != I915_TILING_NONE) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 1295 | ret = i915_gem_object_get_fence_reg(obj, true); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1296 | if (ret) |
| 1297 | goto unlock; |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 1298 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1299 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1300 | if (i915_gem_object_is_inactive(obj)) |
| 1301 | list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list); |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1302 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1303 | pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) + |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1304 | page_offset; |
| 1305 | |
| 1306 | /* Finally, remap it using the new GTT offset */ |
| 1307 | ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1308 | unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1309 | mutex_unlock(&dev->struct_mutex); |
| 1310 | |
| 1311 | switch (ret) { |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1312 | case -EAGAIN: |
| 1313 | set_need_resched(); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1314 | case 0: |
| 1315 | case -ERESTARTSYS: |
| 1316 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1317 | case -ENOMEM: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1318 | return VM_FAULT_OOM; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1319 | default: |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1320 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | /** |
| 1325 | * i915_gem_create_mmap_offset - create a fake mmap offset for an object |
| 1326 | * @obj: obj in question |
| 1327 | * |
| 1328 | * GEM memory mapping works by handing back to userspace a fake mmap offset |
| 1329 | * it can use in a subsequent mmap(2) call. The DRM core code then looks |
| 1330 | * up the object based on the offset and sets up the various memory mapping |
| 1331 | * structures. |
| 1332 | * |
| 1333 | * This routine allocates and attaches a fake offset for @obj. |
| 1334 | */ |
| 1335 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1336 | i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1337 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1338 | struct drm_device *dev = obj->base.dev; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1339 | struct drm_gem_mm *mm = dev->mm_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1340 | struct drm_map_list *list; |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 1341 | struct drm_local_map *map; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1342 | int ret = 0; |
| 1343 | |
| 1344 | /* Set the object up for mmap'ing */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1345 | list = &obj->base.map_list; |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1346 | list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1347 | if (!list->map) |
| 1348 | return -ENOMEM; |
| 1349 | |
| 1350 | map = list->map; |
| 1351 | map->type = _DRM_GEM; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1352 | map->size = obj->base.size; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1353 | map->handle = obj; |
| 1354 | |
| 1355 | /* Get a DRM GEM mmap offset allocated... */ |
| 1356 | list->file_offset_node = drm_mm_search_free(&mm->offset_manager, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1357 | obj->base.size / PAGE_SIZE, |
| 1358 | 0, 0); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1359 | if (!list->file_offset_node) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1360 | DRM_ERROR("failed to allocate offset for bo %d\n", |
| 1361 | obj->base.name); |
Chris Wilson | 9e0ae53 | 2010-09-21 15:05:24 +0100 | [diff] [blame] | 1362 | ret = -ENOSPC; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1363 | goto out_free_list; |
| 1364 | } |
| 1365 | |
| 1366 | list->file_offset_node = drm_mm_get_block(list->file_offset_node, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1367 | obj->base.size / PAGE_SIZE, |
| 1368 | 0); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1369 | if (!list->file_offset_node) { |
| 1370 | ret = -ENOMEM; |
| 1371 | goto out_free_list; |
| 1372 | } |
| 1373 | |
| 1374 | list->hash.key = list->file_offset_node->start; |
Chris Wilson | 9e0ae53 | 2010-09-21 15:05:24 +0100 | [diff] [blame] | 1375 | ret = drm_ht_insert_item(&mm->offset_hash, &list->hash); |
| 1376 | if (ret) { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1377 | DRM_ERROR("failed to add to map hash\n"); |
| 1378 | goto out_free_mm; |
| 1379 | } |
| 1380 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1381 | return 0; |
| 1382 | |
| 1383 | out_free_mm: |
| 1384 | drm_mm_put_block(list->file_offset_node); |
| 1385 | out_free_list: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1386 | kfree(list->map); |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame] | 1387 | list->map = NULL; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1388 | |
| 1389 | return ret; |
| 1390 | } |
| 1391 | |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1392 | /** |
| 1393 | * i915_gem_release_mmap - remove physical page mappings |
| 1394 | * @obj: obj in question |
| 1395 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1396 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1397 | * relinquish ownership of the pages back to the system. |
| 1398 | * |
| 1399 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 1400 | * object through the GTT and then lose the fence register due to |
| 1401 | * resource pressure. Similarly if the object has been moved out of the |
| 1402 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 1403 | * mapping will then trigger a page fault on the next user access, allowing |
| 1404 | * fixup by i915_gem_fault(). |
| 1405 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 1406 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1407 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1408 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1409 | struct drm_device *dev = obj->base.dev; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1410 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1411 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1412 | if (unlikely(obj->base.map_list.map && dev->dev_mapping)) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1413 | unmap_mapping_range(dev->dev_mapping, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1414 | (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT, |
| 1415 | obj->base.size, 1); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1416 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1417 | if (obj->fault_mappable) { |
| 1418 | obj->fault_mappable = false; |
| 1419 | i915_gem_info_update_mappable(dev_priv, obj, false); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1420 | } |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1421 | } |
| 1422 | |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1423 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1424 | i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj) |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1425 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1426 | struct drm_device *dev = obj->base.dev; |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1427 | struct drm_gem_mm *mm = dev->mm_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1428 | struct drm_map_list *list = &obj->base.map_list; |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1429 | |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1430 | drm_ht_remove_item(&mm->offset_hash, &list->hash); |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame] | 1431 | drm_mm_put_block(list->file_offset_node); |
| 1432 | kfree(list->map); |
| 1433 | list->map = NULL; |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
Chris Wilson | 92b88ae | 2010-11-09 11:47:32 +0000 | [diff] [blame] | 1436 | static uint32_t |
| 1437 | i915_gem_get_gtt_size(struct drm_i915_gem_object *obj) |
| 1438 | { |
| 1439 | struct drm_device *dev = obj->base.dev; |
| 1440 | uint32_t size; |
| 1441 | |
| 1442 | if (INTEL_INFO(dev)->gen >= 4 || |
| 1443 | obj->tiling_mode == I915_TILING_NONE) |
| 1444 | return obj->base.size; |
| 1445 | |
| 1446 | /* Previous chips need a power-of-two fence region when tiling */ |
| 1447 | if (INTEL_INFO(dev)->gen == 3) |
| 1448 | size = 1024*1024; |
| 1449 | else |
| 1450 | size = 512*1024; |
| 1451 | |
| 1452 | while (size < obj->base.size) |
| 1453 | size <<= 1; |
| 1454 | |
| 1455 | return size; |
| 1456 | } |
| 1457 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1458 | /** |
| 1459 | * i915_gem_get_gtt_alignment - return required GTT alignment for an object |
| 1460 | * @obj: object to check |
| 1461 | * |
| 1462 | * Return the required GTT alignment for an object, taking into account |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 1463 | * potential fence register mapping. |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1464 | */ |
| 1465 | static uint32_t |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1466 | i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1467 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1468 | struct drm_device *dev = obj->base.dev; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1469 | |
| 1470 | /* |
| 1471 | * Minimum alignment is 4k (GTT page size), but might be greater |
| 1472 | * if a fence register is needed for the object. |
| 1473 | */ |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1474 | if (INTEL_INFO(dev)->gen >= 4 || |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1475 | obj->tiling_mode == I915_TILING_NONE) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1476 | return 4096; |
| 1477 | |
| 1478 | /* |
| 1479 | * Previous chips need to be aligned to the size of the smallest |
| 1480 | * fence register that can contain the object. |
| 1481 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1482 | return i915_gem_get_gtt_size(obj); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1483 | } |
| 1484 | |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 1485 | /** |
| 1486 | * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an |
| 1487 | * unfenced object |
| 1488 | * @obj: object to check |
| 1489 | * |
| 1490 | * Return the required GTT alignment for an object, only taking into account |
| 1491 | * unfenced tiled surface requirements. |
| 1492 | */ |
| 1493 | static uint32_t |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1494 | i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj) |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 1495 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1496 | struct drm_device *dev = obj->base.dev; |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 1497 | int tile_height; |
| 1498 | |
| 1499 | /* |
| 1500 | * Minimum alignment is 4k (GTT page size) for sane hw. |
| 1501 | */ |
| 1502 | if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) || |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1503 | obj->tiling_mode == I915_TILING_NONE) |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 1504 | return 4096; |
| 1505 | |
| 1506 | /* |
| 1507 | * Older chips need unfenced tiled buffers to be aligned to the left |
| 1508 | * edge of an even tile row (where tile rows are counted as if the bo is |
| 1509 | * placed in a fenced gtt region). |
| 1510 | */ |
| 1511 | if (IS_GEN2(dev) || |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1512 | (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))) |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 1513 | tile_height = 32; |
| 1514 | else |
| 1515 | tile_height = 8; |
| 1516 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1517 | return tile_height * obj->stride * 2; |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1520 | /** |
| 1521 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 1522 | * @dev: DRM device |
| 1523 | * @data: GTT mapping ioctl data |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1524 | * @file: GEM object info |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1525 | * |
| 1526 | * Simply returns the fake offset to userspace so it can mmap it. |
| 1527 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 1528 | * up so we can get faults in the handler above. |
| 1529 | * |
| 1530 | * The fault handler will take care of binding the object into the GTT |
| 1531 | * (since it may have been evicted to make room for something), allocating |
| 1532 | * a fence register, and mapping the appropriate aperture address into |
| 1533 | * userspace. |
| 1534 | */ |
| 1535 | int |
| 1536 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1537 | struct drm_file *file) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1538 | { |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1539 | struct drm_i915_private *dev_priv = dev->dev_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1540 | struct drm_i915_gem_mmap_gtt *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1541 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1542 | int ret; |
| 1543 | |
| 1544 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1545 | return -ENODEV; |
| 1546 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1547 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1548 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1549 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1550 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1551 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1552 | if (obj == NULL) { |
| 1553 | ret = -ENOENT; |
| 1554 | goto unlock; |
| 1555 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1556 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1557 | if (obj->base.size > dev_priv->mm.gtt_mappable_end) { |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1558 | ret = -E2BIG; |
| 1559 | goto unlock; |
| 1560 | } |
| 1561 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1562 | if (obj->madv != I915_MADV_WILLNEED) { |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 1563 | DRM_ERROR("Attempting to mmap a purgeable buffer\n"); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1564 | ret = -EINVAL; |
| 1565 | goto out; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 1566 | } |
| 1567 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1568 | if (!obj->base.map_list.map) { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1569 | ret = i915_gem_create_mmap_offset(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1570 | if (ret) |
| 1571 | goto out; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1572 | } |
| 1573 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1574 | args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1575 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1576 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1577 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1578 | unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1579 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1580 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1581 | } |
| 1582 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1583 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1584 | i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj, |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1585 | gfp_t gfpmask) |
| 1586 | { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1587 | int page_count, i; |
| 1588 | struct address_space *mapping; |
| 1589 | struct inode *inode; |
| 1590 | struct page *page; |
| 1591 | |
| 1592 | /* Get the list of pages out of our struct file. They'll be pinned |
| 1593 | * at this point until we release them. |
| 1594 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1595 | page_count = obj->base.size / PAGE_SIZE; |
| 1596 | BUG_ON(obj->pages != NULL); |
| 1597 | obj->pages = drm_malloc_ab(page_count, sizeof(struct page *)); |
| 1598 | if (obj->pages == NULL) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1599 | return -ENOMEM; |
| 1600 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1601 | inode = obj->base.filp->f_path.dentry->d_inode; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1602 | mapping = inode->i_mapping; |
| 1603 | for (i = 0; i < page_count; i++) { |
| 1604 | page = read_cache_page_gfp(mapping, i, |
| 1605 | GFP_HIGHUSER | |
| 1606 | __GFP_COLD | |
| 1607 | __GFP_RECLAIMABLE | |
| 1608 | gfpmask); |
| 1609 | if (IS_ERR(page)) |
| 1610 | goto err_pages; |
| 1611 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1612 | obj->pages[i] = page; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1613 | } |
| 1614 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1615 | if (obj->tiling_mode != I915_TILING_NONE) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1616 | i915_gem_object_do_bit_17_swizzle(obj); |
| 1617 | |
| 1618 | return 0; |
| 1619 | |
| 1620 | err_pages: |
| 1621 | while (i--) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1622 | page_cache_release(obj->pages[i]); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1623 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1624 | drm_free_large(obj->pages); |
| 1625 | obj->pages = NULL; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1626 | return PTR_ERR(page); |
| 1627 | } |
| 1628 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 1629 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1630 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1631 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1632 | int page_count = obj->base.size / PAGE_SIZE; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1633 | int i; |
| 1634 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1635 | BUG_ON(obj->madv == __I915_MADV_PURGED); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1636 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1637 | if (obj->tiling_mode != I915_TILING_NONE) |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 1638 | i915_gem_object_save_bit_17_swizzle(obj); |
| 1639 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1640 | if (obj->madv == I915_MADV_DONTNEED) |
| 1641 | obj->dirty = 0; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1642 | |
| 1643 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1644 | if (obj->dirty) |
| 1645 | set_page_dirty(obj->pages[i]); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1646 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1647 | if (obj->madv == I915_MADV_WILLNEED) |
| 1648 | mark_page_accessed(obj->pages[i]); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1649 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1650 | page_cache_release(obj->pages[i]); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1651 | } |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1652 | obj->dirty = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1653 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1654 | drm_free_large(obj->pages); |
| 1655 | obj->pages = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1658 | static uint32_t |
| 1659 | i915_gem_next_request_seqno(struct drm_device *dev, |
| 1660 | struct intel_ring_buffer *ring) |
| 1661 | { |
| 1662 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 5d97eb6 | 2010-11-10 20:40:02 +0000 | [diff] [blame] | 1663 | return ring->outstanding_lazy_request = dev_priv->next_seqno; |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1664 | } |
| 1665 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1666 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1667 | i915_gem_object_move_to_active(struct drm_i915_gem_object *obj, |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1668 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1669 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1670 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1671 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1672 | uint32_t seqno = i915_gem_next_request_seqno(dev, ring); |
Daniel Vetter | 617dbe2 | 2010-02-11 22:16:02 +0100 | [diff] [blame] | 1673 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1674 | BUG_ON(ring == NULL); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1675 | obj->ring = ring; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1676 | |
| 1677 | /* Add a reference if we're newly entering the active list. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1678 | if (!obj->active) { |
| 1679 | drm_gem_object_reference(&obj->base); |
| 1680 | obj->active = 1; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1681 | } |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 1682 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1683 | /* Move from whatever list we were on to the tail of execution. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1684 | list_move_tail(&obj->mm_list, &dev_priv->mm.active_list); |
| 1685 | list_move_tail(&obj->ring_list, &ring->active_list); |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1686 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1687 | obj->last_rendering_seqno = seqno; |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1688 | if (obj->fenced_gpu_access) { |
| 1689 | struct drm_i915_fence_reg *reg; |
| 1690 | |
| 1691 | BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE); |
| 1692 | |
| 1693 | obj->last_fenced_seqno = seqno; |
| 1694 | obj->last_fenced_ring = ring; |
| 1695 | |
| 1696 | reg = &dev_priv->fence_regs[obj->fence_reg]; |
| 1697 | list_move_tail(®->lru_list, &dev_priv->mm.fence_list); |
| 1698 | } |
| 1699 | } |
| 1700 | |
| 1701 | static void |
| 1702 | i915_gem_object_move_off_active(struct drm_i915_gem_object *obj) |
| 1703 | { |
| 1704 | list_del_init(&obj->ring_list); |
| 1705 | obj->last_rendering_seqno = 0; |
| 1706 | obj->last_fenced_seqno = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1707 | } |
| 1708 | |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1709 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1710 | i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj) |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1711 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1712 | struct drm_device *dev = obj->base.dev; |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1713 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1714 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1715 | BUG_ON(!obj->active); |
| 1716 | list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list); |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1717 | |
| 1718 | i915_gem_object_move_off_active(obj); |
| 1719 | } |
| 1720 | |
| 1721 | static void |
| 1722 | i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj) |
| 1723 | { |
| 1724 | struct drm_device *dev = obj->base.dev; |
| 1725 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1726 | |
| 1727 | if (obj->pin_count != 0) |
| 1728 | list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list); |
| 1729 | else |
| 1730 | list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list); |
| 1731 | |
| 1732 | BUG_ON(!list_empty(&obj->gpu_write_list)); |
| 1733 | BUG_ON(!obj->active); |
| 1734 | obj->ring = NULL; |
| 1735 | |
| 1736 | i915_gem_object_move_off_active(obj); |
| 1737 | obj->fenced_gpu_access = false; |
| 1738 | obj->last_fenced_ring = NULL; |
| 1739 | |
| 1740 | obj->active = 0; |
| 1741 | drm_gem_object_unreference(&obj->base); |
| 1742 | |
| 1743 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1744 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1745 | |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1746 | /* Immediately discard the backing storage */ |
| 1747 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1748 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1749 | { |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1750 | struct inode *inode; |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1751 | |
Chris Wilson | ae9fed6 | 2010-08-07 11:01:30 +0100 | [diff] [blame] | 1752 | /* Our goal here is to return as much of the memory as |
| 1753 | * is possible back to the system as we are called from OOM. |
| 1754 | * To do this we must instruct the shmfs to drop all of its |
| 1755 | * backing pages, *now*. Here we mirror the actions taken |
| 1756 | * when by shmem_delete_inode() to release the backing store. |
| 1757 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1758 | inode = obj->base.filp->f_path.dentry->d_inode; |
Chris Wilson | ae9fed6 | 2010-08-07 11:01:30 +0100 | [diff] [blame] | 1759 | truncate_inode_pages(inode->i_mapping, 0); |
| 1760 | if (inode->i_op->truncate_range) |
| 1761 | inode->i_op->truncate_range(inode, 0, (loff_t)-1); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1762 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1763 | obj->madv = __I915_MADV_PURGED; |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | static inline int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1767 | i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj) |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1768 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1769 | return obj->madv == I915_MADV_DONTNEED; |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1770 | } |
| 1771 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1772 | static void |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1773 | i915_gem_process_flushing_list(struct drm_device *dev, |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1774 | uint32_t flush_domains, |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1775 | struct intel_ring_buffer *ring) |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1776 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1777 | struct drm_i915_gem_object *obj, *next; |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1778 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1779 | list_for_each_entry_safe(obj, next, |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 1780 | &ring->gpu_write_list, |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1781 | gpu_write_list) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1782 | if (obj->base.write_domain & flush_domains) { |
| 1783 | uint32_t old_write_domain = obj->base.write_domain; |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1784 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1785 | obj->base.write_domain = 0; |
| 1786 | list_del_init(&obj->gpu_write_list); |
Daniel Vetter | 617dbe2 | 2010-02-11 22:16:02 +0100 | [diff] [blame] | 1787 | i915_gem_object_move_to_active(obj, ring); |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1788 | |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1789 | trace_i915_gem_object_change_domain(obj, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1790 | obj->base.read_domains, |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1791 | old_write_domain); |
| 1792 | } |
| 1793 | } |
| 1794 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 1795 | |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1796 | int |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1797 | i915_add_request(struct drm_device *dev, |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1798 | struct drm_file *file, |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 1799 | struct drm_i915_gem_request *request, |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1800 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1801 | { |
| 1802 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1803 | struct drm_i915_file_private *file_priv = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1804 | uint32_t seqno; |
| 1805 | int was_empty; |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1806 | int ret; |
| 1807 | |
| 1808 | BUG_ON(request == NULL); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1809 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1810 | if (file != NULL) |
| 1811 | file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1812 | |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1813 | ret = ring->add_request(ring, &seqno); |
| 1814 | if (ret) |
| 1815 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1816 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1817 | ring->outstanding_lazy_request = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1818 | |
| 1819 | request->seqno = seqno; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1820 | request->ring = ring; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1821 | request->emitted_jiffies = jiffies; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1822 | was_empty = list_empty(&ring->request_list); |
| 1823 | list_add_tail(&request->list, &ring->request_list); |
| 1824 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1825 | if (file_priv) { |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1826 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1827 | request->file_priv = file_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1828 | list_add_tail(&request->client_list, |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1829 | &file_priv->mm.request_list); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1830 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1831 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1832 | |
Ben Gamari | f65d942 | 2009-09-14 17:48:44 -0400 | [diff] [blame] | 1833 | if (!dev_priv->mm.suspended) { |
Chris Wilson | b3b079d | 2010-09-13 23:44:34 +0100 | [diff] [blame] | 1834 | mod_timer(&dev_priv->hangcheck_timer, |
| 1835 | jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD)); |
Ben Gamari | f65d942 | 2009-09-14 17:48:44 -0400 | [diff] [blame] | 1836 | if (was_empty) |
Chris Wilson | b3b079d | 2010-09-13 23:44:34 +0100 | [diff] [blame] | 1837 | queue_delayed_work(dev_priv->wq, |
| 1838 | &dev_priv->mm.retire_work, HZ); |
Ben Gamari | f65d942 | 2009-09-14 17:48:44 -0400 | [diff] [blame] | 1839 | } |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1840 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * Command execution barrier |
| 1845 | * |
| 1846 | * Ensures that all commands in the ring are finished |
| 1847 | * before signalling the CPU |
| 1848 | */ |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1849 | static void |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1850 | i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1851 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1852 | uint32_t flush_domains = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1853 | |
| 1854 | /* The sampler always gets flushed on i965 (sigh) */ |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 1855 | if (INTEL_INFO(dev)->gen >= 4) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1856 | flush_domains |= I915_GEM_DOMAIN_SAMPLER; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1857 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 1858 | ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1861 | static inline void |
| 1862 | i915_gem_request_remove_from_client(struct drm_i915_gem_request *request) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1863 | { |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1864 | struct drm_i915_file_private *file_priv = request->file_priv; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1865 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1866 | if (!file_priv) |
| 1867 | return; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 1868 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1869 | spin_lock(&file_priv->mm.lock); |
| 1870 | list_del(&request->client_list); |
| 1871 | request->file_priv = NULL; |
| 1872 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1875 | static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv, |
| 1876 | struct intel_ring_buffer *ring) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1877 | { |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1878 | while (!list_empty(&ring->request_list)) { |
| 1879 | struct drm_i915_gem_request *request; |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1880 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1881 | request = list_first_entry(&ring->request_list, |
| 1882 | struct drm_i915_gem_request, |
| 1883 | list); |
| 1884 | |
| 1885 | list_del(&request->list); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1886 | i915_gem_request_remove_from_client(request); |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1887 | kfree(request); |
| 1888 | } |
| 1889 | |
| 1890 | while (!list_empty(&ring->active_list)) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1891 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1892 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1893 | obj = list_first_entry(&ring->active_list, |
| 1894 | struct drm_i915_gem_object, |
| 1895 | ring_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1896 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1897 | obj->base.write_domain = 0; |
| 1898 | list_del_init(&obj->gpu_write_list); |
| 1899 | i915_gem_object_move_to_inactive(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1900 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
Chris Wilson | 312817a | 2010-11-22 11:50:11 +0000 | [diff] [blame] | 1903 | static void i915_gem_reset_fences(struct drm_device *dev) |
| 1904 | { |
| 1905 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1906 | int i; |
| 1907 | |
| 1908 | for (i = 0; i < 16; i++) { |
| 1909 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
| 1910 | if (reg->obj) |
| 1911 | i915_gem_clear_fence_reg(reg->obj); |
| 1912 | } |
| 1913 | } |
| 1914 | |
Chris Wilson | 069efc1 | 2010-09-30 16:53:18 +0100 | [diff] [blame] | 1915 | void i915_gem_reset(struct drm_device *dev) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1916 | { |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1917 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1918 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1919 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1920 | i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 1921 | i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 1922 | i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring); |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1923 | |
| 1924 | /* Remove anything from the flushing lists. The GPU cache is likely |
| 1925 | * to be lost on reset along with the data, so simply move the |
| 1926 | * lost bo to the inactive list. |
| 1927 | */ |
| 1928 | while (!list_empty(&dev_priv->mm.flushing_list)) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1929 | obj= list_first_entry(&dev_priv->mm.flushing_list, |
| 1930 | struct drm_i915_gem_object, |
| 1931 | mm_list); |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1932 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1933 | obj->base.write_domain = 0; |
| 1934 | list_del_init(&obj->gpu_write_list); |
| 1935 | i915_gem_object_move_to_inactive(obj); |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1936 | } |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1937 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1938 | /* Move everything out of the GPU domains to ensure we do any |
| 1939 | * necessary invalidation upon reuse. |
| 1940 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1941 | list_for_each_entry(obj, |
Chris Wilson | 77f0123 | 2010-09-19 12:31:36 +0100 | [diff] [blame] | 1942 | &dev_priv->mm.inactive_list, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1943 | mm_list) |
Chris Wilson | 77f0123 | 2010-09-19 12:31:36 +0100 | [diff] [blame] | 1944 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1945 | obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS; |
Chris Wilson | 77f0123 | 2010-09-19 12:31:36 +0100 | [diff] [blame] | 1946 | } |
Chris Wilson | 069efc1 | 2010-09-30 16:53:18 +0100 | [diff] [blame] | 1947 | |
| 1948 | /* The fence registers are invalidated so clear them out */ |
Chris Wilson | 312817a | 2010-11-22 11:50:11 +0000 | [diff] [blame] | 1949 | i915_gem_reset_fences(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1950 | } |
| 1951 | |
| 1952 | /** |
| 1953 | * This function clears the request list as sequence numbers are passed. |
| 1954 | */ |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 1955 | static void |
| 1956 | i915_gem_retire_requests_ring(struct drm_device *dev, |
| 1957 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1958 | { |
| 1959 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1960 | uint32_t seqno; |
| 1961 | |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1962 | if (!ring->status_page.page_addr || |
| 1963 | list_empty(&ring->request_list)) |
Karsten Wiese | 6c0594a | 2009-02-23 15:07:57 +0100 | [diff] [blame] | 1964 | return; |
| 1965 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 1966 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1967 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 1968 | seqno = ring->get_seqno(ring); |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1969 | while (!list_empty(&ring->request_list)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1970 | struct drm_i915_gem_request *request; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1971 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1972 | request = list_first_entry(&ring->request_list, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1973 | struct drm_i915_gem_request, |
| 1974 | list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1975 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1976 | if (!i915_seqno_passed(seqno, request->seqno)) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1977 | break; |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1978 | |
| 1979 | trace_i915_gem_request_retire(dev, request->seqno); |
| 1980 | |
| 1981 | list_del(&request->list); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1982 | i915_gem_request_remove_from_client(request); |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1983 | kfree(request); |
| 1984 | } |
| 1985 | |
| 1986 | /* Move any buffers on the active list that are no longer referenced |
| 1987 | * by the ringbuffer to the flushing/inactive lists as appropriate. |
| 1988 | */ |
| 1989 | while (!list_empty(&ring->active_list)) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1990 | struct drm_i915_gem_object *obj; |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1991 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1992 | obj= list_first_entry(&ring->active_list, |
| 1993 | struct drm_i915_gem_object, |
| 1994 | ring_list); |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1995 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1996 | if (!i915_seqno_passed(seqno, obj->last_rendering_seqno)) |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1997 | break; |
| 1998 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1999 | if (obj->base.write_domain != 0) |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2000 | i915_gem_object_move_to_flushing(obj); |
| 2001 | else |
| 2002 | i915_gem_object_move_to_inactive(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2003 | } |
Chris Wilson | 9d34e5d | 2009-09-24 05:26:06 +0100 | [diff] [blame] | 2004 | |
| 2005 | if (unlikely (dev_priv->trace_irq_seqno && |
| 2006 | i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) { |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2007 | ring->user_irq_put(ring); |
Chris Wilson | 9d34e5d | 2009-09-24 05:26:06 +0100 | [diff] [blame] | 2008 | dev_priv->trace_irq_seqno = 0; |
| 2009 | } |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 2010 | |
| 2011 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | void |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2015 | i915_gem_retire_requests(struct drm_device *dev) |
| 2016 | { |
| 2017 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 2018 | |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 2019 | if (!list_empty(&dev_priv->mm.deferred_free_list)) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2020 | struct drm_i915_gem_object *obj, *next; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 2021 | |
| 2022 | /* We must be careful that during unbind() we do not |
| 2023 | * accidentally infinitely recurse into retire requests. |
| 2024 | * Currently: |
| 2025 | * retire -> free -> unbind -> wait -> retire_ring |
| 2026 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2027 | list_for_each_entry_safe(obj, next, |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 2028 | &dev_priv->mm.deferred_free_list, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 2029 | mm_list) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2030 | i915_gem_free_object_tail(obj); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 2031 | } |
| 2032 | |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2033 | i915_gem_retire_requests_ring(dev, &dev_priv->render_ring); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 2034 | i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2035 | i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring); |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2036 | } |
| 2037 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 2038 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2039 | i915_gem_retire_work_handler(struct work_struct *work) |
| 2040 | { |
| 2041 | drm_i915_private_t *dev_priv; |
| 2042 | struct drm_device *dev; |
| 2043 | |
| 2044 | dev_priv = container_of(work, drm_i915_private_t, |
| 2045 | mm.retire_work.work); |
| 2046 | dev = dev_priv->dev; |
| 2047 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2048 | /* Come back later if the device is busy... */ |
| 2049 | if (!mutex_trylock(&dev->struct_mutex)) { |
| 2050 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ); |
| 2051 | return; |
| 2052 | } |
| 2053 | |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2054 | i915_gem_retire_requests(dev); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2055 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 2056 | if (!dev_priv->mm.suspended && |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2057 | (!list_empty(&dev_priv->render_ring.request_list) || |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2058 | !list_empty(&dev_priv->bsd_ring.request_list) || |
| 2059 | !list_empty(&dev_priv->blt_ring.request_list))) |
Eric Anholt | 9c9fe1f | 2009-08-03 16:09:16 -0700 | [diff] [blame] | 2060 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2061 | mutex_unlock(&dev->struct_mutex); |
| 2062 | } |
| 2063 | |
Daniel Vetter | 5a5a0c6 | 2009-09-15 22:57:36 +0200 | [diff] [blame] | 2064 | int |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2065 | i915_do_wait_request(struct drm_device *dev, uint32_t seqno, |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 2066 | bool interruptible, struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2067 | { |
| 2068 | drm_i915_private_t *dev_priv = dev->dev_private; |
Jesse Barnes | 802c7eb | 2009-05-05 16:03:48 -0700 | [diff] [blame] | 2069 | u32 ier; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2070 | int ret = 0; |
| 2071 | |
| 2072 | BUG_ON(seqno == 0); |
| 2073 | |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 2074 | if (atomic_read(&dev_priv->mm.wedged)) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 2075 | return -EAGAIN; |
Ben Gamari | ffed1d0 | 2009-09-14 17:48:41 -0400 | [diff] [blame] | 2076 | |
Chris Wilson | 5d97eb6 | 2010-11-10 20:40:02 +0000 | [diff] [blame] | 2077 | if (seqno == ring->outstanding_lazy_request) { |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 2078 | struct drm_i915_gem_request *request; |
| 2079 | |
| 2080 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
| 2081 | if (request == NULL) |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 2082 | return -ENOMEM; |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 2083 | |
| 2084 | ret = i915_add_request(dev, NULL, request, ring); |
| 2085 | if (ret) { |
| 2086 | kfree(request); |
| 2087 | return ret; |
| 2088 | } |
| 2089 | |
| 2090 | seqno = request->seqno; |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 2091 | } |
| 2092 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2093 | if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) { |
Eric Anholt | bad720f | 2009-10-22 16:11:14 -0700 | [diff] [blame] | 2094 | if (HAS_PCH_SPLIT(dev)) |
Zhenyu Wang | 036a4a7 | 2009-06-08 14:40:19 +0800 | [diff] [blame] | 2095 | ier = I915_READ(DEIER) | I915_READ(GTIER); |
| 2096 | else |
| 2097 | ier = I915_READ(IER); |
Jesse Barnes | 802c7eb | 2009-05-05 16:03:48 -0700 | [diff] [blame] | 2098 | if (!ier) { |
| 2099 | DRM_ERROR("something (likely vbetool) disabled " |
| 2100 | "interrupts, re-enabling\n"); |
| 2101 | i915_driver_irq_preinstall(dev); |
| 2102 | i915_driver_irq_postinstall(dev); |
| 2103 | } |
| 2104 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2105 | trace_i915_gem_request_wait_begin(dev, seqno); |
| 2106 | |
Chris Wilson | b222349 | 2010-10-27 15:27:33 +0100 | [diff] [blame] | 2107 | ring->waiting_seqno = seqno; |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2108 | ring->user_irq_get(ring); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2109 | if (interruptible) |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2110 | ret = wait_event_interruptible(ring->irq_queue, |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2111 | i915_seqno_passed(ring->get_seqno(ring), seqno) |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2112 | || atomic_read(&dev_priv->mm.wedged)); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2113 | else |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2114 | wait_event(ring->irq_queue, |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2115 | i915_seqno_passed(ring->get_seqno(ring), seqno) |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2116 | || atomic_read(&dev_priv->mm.wedged)); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2117 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2118 | ring->user_irq_put(ring); |
Chris Wilson | b222349 | 2010-10-27 15:27:33 +0100 | [diff] [blame] | 2119 | ring->waiting_seqno = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2120 | |
| 2121 | trace_i915_gem_request_wait_end(dev, seqno); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2122 | } |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 2123 | if (atomic_read(&dev_priv->mm.wedged)) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 2124 | ret = -EAGAIN; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2125 | |
| 2126 | if (ret && ret != -ERESTARTSYS) |
Daniel Vetter | 8bff917 | 2010-02-11 22:19:40 +0100 | [diff] [blame] | 2127 | DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n", |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2128 | __func__, ret, seqno, ring->get_seqno(ring), |
Daniel Vetter | 8bff917 | 2010-02-11 22:19:40 +0100 | [diff] [blame] | 2129 | dev_priv->next_seqno); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2130 | |
| 2131 | /* Directly dispatch request retiring. While we have the work queue |
| 2132 | * to handle this, the waiter on a request often wants an associated |
| 2133 | * buffer to have made it to the inactive list, and we would need |
| 2134 | * a separate wait queue to handle that. |
| 2135 | */ |
| 2136 | if (ret == 0) |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2137 | i915_gem_retire_requests_ring(dev, ring); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2138 | |
| 2139 | return ret; |
| 2140 | } |
| 2141 | |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2142 | /** |
| 2143 | * Waits for a sequence number to be signaled, and cleans up the |
| 2144 | * request and object lists appropriately for that event. |
| 2145 | */ |
| 2146 | static int |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2147 | i915_wait_request(struct drm_device *dev, uint32_t seqno, |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2148 | struct intel_ring_buffer *ring) |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2149 | { |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2150 | return i915_do_wait_request(dev, seqno, 1, ring); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2151 | } |
| 2152 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2153 | static void |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2154 | i915_gem_flush_ring(struct drm_device *dev, |
| 2155 | struct intel_ring_buffer *ring, |
| 2156 | uint32_t invalidate_domains, |
| 2157 | uint32_t flush_domains) |
| 2158 | { |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2159 | ring->flush(ring, invalidate_domains, flush_domains); |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2160 | i915_gem_process_flushing_list(dev, flush_domains, ring); |
| 2161 | } |
| 2162 | |
| 2163 | static void |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2164 | i915_gem_flush(struct drm_device *dev, |
| 2165 | uint32_t invalidate_domains, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2166 | uint32_t flush_domains, |
| 2167 | uint32_t flush_rings) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2168 | { |
| 2169 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 8bff917 | 2010-02-11 22:19:40 +0100 | [diff] [blame] | 2170 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2171 | if (flush_domains & I915_GEM_DOMAIN_CPU) |
Daniel Vetter | 40ce657 | 2010-11-05 18:12:18 +0100 | [diff] [blame] | 2172 | intel_gtt_chipset_flush(); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2173 | |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2174 | if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) { |
| 2175 | if (flush_rings & RING_RENDER) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2176 | i915_gem_flush_ring(dev, &dev_priv->render_ring, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2177 | invalidate_domains, flush_domains); |
| 2178 | if (flush_rings & RING_BSD) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2179 | i915_gem_flush_ring(dev, &dev_priv->bsd_ring, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2180 | invalidate_domains, flush_domains); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2181 | if (flush_rings & RING_BLT) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2182 | i915_gem_flush_ring(dev, &dev_priv->blt_ring, |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2183 | invalidate_domains, flush_domains); |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2184 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2185 | } |
| 2186 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2187 | /** |
| 2188 | * Ensures that all rendering to the object has completed and the object is |
| 2189 | * safe to unbind from the GTT or access from the CPU. |
| 2190 | */ |
| 2191 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2192 | i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj, |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2193 | bool interruptible) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2194 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2195 | struct drm_device *dev = obj->base.dev; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2196 | int ret; |
| 2197 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2198 | /* This function only exists to support waiting for existing rendering, |
| 2199 | * not for emitting required flushes. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2200 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2201 | BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2202 | |
| 2203 | /* If there is rendering queued on the buffer being evicted, wait for |
| 2204 | * it. |
| 2205 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2206 | if (obj->active) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2207 | ret = i915_do_wait_request(dev, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2208 | obj->last_rendering_seqno, |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2209 | interruptible, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2210 | obj->ring); |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2211 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2212 | return ret; |
| 2213 | } |
| 2214 | |
| 2215 | return 0; |
| 2216 | } |
| 2217 | |
| 2218 | /** |
| 2219 | * Unbinds an object from the GTT aperture. |
| 2220 | */ |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2221 | int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2222 | i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2223 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2224 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 2225 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2226 | int ret = 0; |
| 2227 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2228 | if (obj->gtt_space == NULL) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2229 | return 0; |
| 2230 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2231 | if (obj->pin_count != 0) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2232 | DRM_ERROR("Attempting to unbind pinned buffer\n"); |
| 2233 | return -EINVAL; |
| 2234 | } |
| 2235 | |
Eric Anholt | 5323fd0 | 2009-09-09 11:50:45 -0700 | [diff] [blame] | 2236 | /* blow away mappings if mapped through GTT */ |
| 2237 | i915_gem_release_mmap(obj); |
| 2238 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2239 | /* Move the object to the CPU domain to ensure that |
| 2240 | * any possible CPU writes while it's not in the GTT |
| 2241 | * are flushed when we go to remap it. This will |
| 2242 | * also ensure that all pending GPU writes are finished |
| 2243 | * before we unbind. |
| 2244 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2245 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
Chris Wilson | 8dc1775 | 2010-07-23 23:18:51 +0100 | [diff] [blame] | 2246 | if (ret == -ERESTARTSYS) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2247 | return ret; |
Chris Wilson | 8dc1775 | 2010-07-23 23:18:51 +0100 | [diff] [blame] | 2248 | /* Continue on if we fail due to EIO, the GPU is hung so we |
| 2249 | * should be safe and we need to cleanup or else we might |
| 2250 | * cause memory corruption through use-after-free. |
| 2251 | */ |
Chris Wilson | 812ed49 | 2010-09-30 15:08:57 +0100 | [diff] [blame] | 2252 | if (ret) { |
| 2253 | i915_gem_clflush_object(obj); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2254 | obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
Chris Wilson | 812ed49 | 2010-09-30 15:08:57 +0100 | [diff] [blame] | 2255 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2256 | |
Daniel Vetter | 96b47b6 | 2009-12-15 17:50:00 +0100 | [diff] [blame] | 2257 | /* release the fence reg _after_ flushing */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2258 | if (obj->fence_reg != I915_FENCE_REG_NONE) |
Daniel Vetter | 96b47b6 | 2009-12-15 17:50:00 +0100 | [diff] [blame] | 2259 | i915_gem_clear_fence_reg(obj); |
| 2260 | |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 2261 | i915_gem_gtt_unbind_object(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2262 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2263 | i915_gem_object_put_pages_gtt(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2264 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2265 | i915_gem_info_remove_gtt(dev_priv, obj); |
| 2266 | list_del_init(&obj->mm_list); |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2267 | /* Avoid an unnecessary call to unbind on rebind. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2268 | obj->map_and_fenceable = true; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2269 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2270 | drm_mm_put_block(obj->gtt_space); |
| 2271 | obj->gtt_space = NULL; |
| 2272 | obj->gtt_offset = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2273 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2274 | if (i915_gem_object_is_purgeable(obj)) |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 2275 | i915_gem_object_truncate(obj); |
| 2276 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2277 | trace_i915_gem_object_unbind(obj); |
| 2278 | |
Chris Wilson | 8dc1775 | 2010-07-23 23:18:51 +0100 | [diff] [blame] | 2279 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2282 | static int i915_ring_idle(struct drm_device *dev, |
| 2283 | struct intel_ring_buffer *ring) |
| 2284 | { |
Chris Wilson | 395b70b | 2010-10-28 21:28:46 +0100 | [diff] [blame] | 2285 | if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list)) |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 2286 | return 0; |
| 2287 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2288 | i915_gem_flush_ring(dev, ring, |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2289 | I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); |
| 2290 | return i915_wait_request(dev, |
| 2291 | i915_gem_next_request_seqno(dev, ring), |
| 2292 | ring); |
| 2293 | } |
| 2294 | |
Chris Wilson | b47eb4a | 2010-08-07 11:01:23 +0100 | [diff] [blame] | 2295 | int |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2296 | i915_gpu_idle(struct drm_device *dev) |
| 2297 | { |
| 2298 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 2299 | bool lists_empty; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2300 | int ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2301 | |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2302 | lists_empty = (list_empty(&dev_priv->mm.flushing_list) && |
Chris Wilson | 395b70b | 2010-10-28 21:28:46 +0100 | [diff] [blame] | 2303 | list_empty(&dev_priv->mm.active_list)); |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2304 | if (lists_empty) |
| 2305 | return 0; |
| 2306 | |
| 2307 | /* Flush everything onto the inactive list. */ |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2308 | ret = i915_ring_idle(dev, &dev_priv->render_ring); |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 2309 | if (ret) |
| 2310 | return ret; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2311 | |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 2312 | ret = i915_ring_idle(dev, &dev_priv->bsd_ring); |
| 2313 | if (ret) |
| 2314 | return ret; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2315 | |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2316 | ret = i915_ring_idle(dev, &dev_priv->blt_ring); |
| 2317 | if (ret) |
| 2318 | return ret; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2319 | |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 2320 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2321 | } |
| 2322 | |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2323 | static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj, |
| 2324 | struct intel_ring_buffer *pipelined) |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2325 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2326 | struct drm_device *dev = obj->base.dev; |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2327 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2328 | u32 size = obj->gtt_space->size; |
| 2329 | int regnum = obj->fence_reg; |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2330 | uint64_t val; |
| 2331 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2332 | val = (uint64_t)((obj->gtt_offset + size - 4096) & |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2333 | 0xfffff000) << 32; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2334 | val |= obj->gtt_offset & 0xfffff000; |
| 2335 | val |= (uint64_t)((obj->stride / 128) - 1) << |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2336 | SANDYBRIDGE_FENCE_PITCH_SHIFT; |
| 2337 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2338 | if (obj->tiling_mode == I915_TILING_Y) |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2339 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; |
| 2340 | val |= I965_FENCE_REG_VALID; |
| 2341 | |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2342 | if (pipelined) { |
| 2343 | int ret = intel_ring_begin(pipelined, 6); |
| 2344 | if (ret) |
| 2345 | return ret; |
| 2346 | |
| 2347 | intel_ring_emit(pipelined, MI_NOOP); |
| 2348 | intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2)); |
| 2349 | intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8); |
| 2350 | intel_ring_emit(pipelined, (u32)val); |
| 2351 | intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4); |
| 2352 | intel_ring_emit(pipelined, (u32)(val >> 32)); |
| 2353 | intel_ring_advance(pipelined); |
| 2354 | } else |
| 2355 | I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val); |
| 2356 | |
| 2357 | return 0; |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2360 | static int i965_write_fence_reg(struct drm_i915_gem_object *obj, |
| 2361 | struct intel_ring_buffer *pipelined) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2362 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2363 | struct drm_device *dev = obj->base.dev; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2364 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2365 | u32 size = obj->gtt_space->size; |
| 2366 | int regnum = obj->fence_reg; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2367 | uint64_t val; |
| 2368 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2369 | val = (uint64_t)((obj->gtt_offset + size - 4096) & |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2370 | 0xfffff000) << 32; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2371 | val |= obj->gtt_offset & 0xfffff000; |
| 2372 | val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT; |
| 2373 | if (obj->tiling_mode == I915_TILING_Y) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2374 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; |
| 2375 | val |= I965_FENCE_REG_VALID; |
| 2376 | |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2377 | if (pipelined) { |
| 2378 | int ret = intel_ring_begin(pipelined, 6); |
| 2379 | if (ret) |
| 2380 | return ret; |
| 2381 | |
| 2382 | intel_ring_emit(pipelined, MI_NOOP); |
| 2383 | intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2)); |
| 2384 | intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8); |
| 2385 | intel_ring_emit(pipelined, (u32)val); |
| 2386 | intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4); |
| 2387 | intel_ring_emit(pipelined, (u32)(val >> 32)); |
| 2388 | intel_ring_advance(pipelined); |
| 2389 | } else |
| 2390 | I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val); |
| 2391 | |
| 2392 | return 0; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2393 | } |
| 2394 | |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2395 | static int i915_write_fence_reg(struct drm_i915_gem_object *obj, |
| 2396 | struct intel_ring_buffer *pipelined) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2397 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2398 | struct drm_device *dev = obj->base.dev; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2399 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2400 | u32 size = obj->gtt_space->size; |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2401 | u32 fence_reg, val, pitch_val; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2402 | int tile_width; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2403 | |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2404 | if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) || |
| 2405 | (size & -size) != size || |
| 2406 | (obj->gtt_offset & (size - 1)), |
| 2407 | "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n", |
| 2408 | obj->gtt_offset, obj->map_and_fenceable, size)) |
| 2409 | return -EINVAL; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2410 | |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2411 | if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)) |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2412 | tile_width = 128; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2413 | else |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2414 | tile_width = 512; |
| 2415 | |
| 2416 | /* Note: pitch better be a power of two tile widths */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2417 | pitch_val = obj->stride / tile_width; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2418 | pitch_val = ffs(pitch_val) - 1; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2419 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2420 | val = obj->gtt_offset; |
| 2421 | if (obj->tiling_mode == I915_TILING_Y) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2422 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2423 | val |= I915_FENCE_SIZE_BITS(size); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2424 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; |
| 2425 | val |= I830_FENCE_REG_VALID; |
| 2426 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2427 | fence_reg = obj->fence_reg; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2428 | if (fence_reg < 8) |
| 2429 | fence_reg = FENCE_REG_830_0 + fence_reg * 4; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2430 | else |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2431 | fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4; |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2432 | |
| 2433 | if (pipelined) { |
| 2434 | int ret = intel_ring_begin(pipelined, 4); |
| 2435 | if (ret) |
| 2436 | return ret; |
| 2437 | |
| 2438 | intel_ring_emit(pipelined, MI_NOOP); |
| 2439 | intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1)); |
| 2440 | intel_ring_emit(pipelined, fence_reg); |
| 2441 | intel_ring_emit(pipelined, val); |
| 2442 | intel_ring_advance(pipelined); |
| 2443 | } else |
| 2444 | I915_WRITE(fence_reg, val); |
| 2445 | |
| 2446 | return 0; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2447 | } |
| 2448 | |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2449 | static int i830_write_fence_reg(struct drm_i915_gem_object *obj, |
| 2450 | struct intel_ring_buffer *pipelined) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2451 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2452 | struct drm_device *dev = obj->base.dev; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2453 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2454 | u32 size = obj->gtt_space->size; |
| 2455 | int regnum = obj->fence_reg; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2456 | uint32_t val; |
| 2457 | uint32_t pitch_val; |
| 2458 | |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2459 | if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) || |
| 2460 | (size & -size) != size || |
| 2461 | (obj->gtt_offset & (size - 1)), |
| 2462 | "object 0x%08x not 512K or pot-size 0x%08x aligned\n", |
| 2463 | obj->gtt_offset, size)) |
| 2464 | return -EINVAL; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2465 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2466 | pitch_val = obj->stride / 128; |
Eric Anholt | e76a16d | 2009-05-26 17:44:56 -0700 | [diff] [blame] | 2467 | pitch_val = ffs(pitch_val) - 1; |
Eric Anholt | e76a16d | 2009-05-26 17:44:56 -0700 | [diff] [blame] | 2468 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2469 | val = obj->gtt_offset; |
| 2470 | if (obj->tiling_mode == I915_TILING_Y) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2471 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2472 | val |= I830_FENCE_SIZE_BITS(size); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2473 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; |
| 2474 | val |= I830_FENCE_REG_VALID; |
| 2475 | |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2476 | if (pipelined) { |
| 2477 | int ret = intel_ring_begin(pipelined, 4); |
| 2478 | if (ret) |
| 2479 | return ret; |
| 2480 | |
| 2481 | intel_ring_emit(pipelined, MI_NOOP); |
| 2482 | intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1)); |
| 2483 | intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4); |
| 2484 | intel_ring_emit(pipelined, val); |
| 2485 | intel_ring_advance(pipelined); |
| 2486 | } else |
| 2487 | I915_WRITE(FENCE_REG_830_0 + regnum * 4, val); |
| 2488 | |
| 2489 | return 0; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2490 | } |
| 2491 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2492 | static int i915_find_fence_reg(struct drm_device *dev, |
| 2493 | bool interruptible) |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2494 | { |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2495 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2496 | struct drm_i915_fence_reg *reg; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2497 | struct drm_i915_gem_object *obj = NULL; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2498 | int i, avail, ret; |
| 2499 | |
| 2500 | /* First try to find a free reg */ |
| 2501 | avail = 0; |
| 2502 | for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) { |
| 2503 | reg = &dev_priv->fence_regs[i]; |
| 2504 | if (!reg->obj) |
| 2505 | return i; |
| 2506 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2507 | if (!reg->obj->pin_count) |
| 2508 | avail++; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2509 | } |
| 2510 | |
| 2511 | if (avail == 0) |
| 2512 | return -ENOSPC; |
| 2513 | |
| 2514 | /* 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] | 2515 | avail = I915_FENCE_REG_NONE; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2516 | list_for_each_entry(reg, &dev_priv->mm.fence_list, |
| 2517 | lru_list) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2518 | obj = reg->obj; |
| 2519 | if (obj->pin_count) |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2520 | continue; |
| 2521 | |
| 2522 | /* found one! */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2523 | avail = obj->fence_reg; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2524 | break; |
| 2525 | } |
| 2526 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2527 | BUG_ON(avail == I915_FENCE_REG_NONE); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2528 | |
| 2529 | /* We only have a reference on obj from the active list. put_fence_reg |
| 2530 | * might drop that one, causing a use-after-free in it. So hold a |
| 2531 | * private reference to obj like the other callers of put_fence_reg |
| 2532 | * (set_tiling ioctl) do. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2533 | drm_gem_object_reference(&obj->base); |
| 2534 | ret = i915_gem_object_put_fence_reg(obj, interruptible); |
| 2535 | drm_gem_object_unreference(&obj->base); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2536 | if (ret != 0) |
| 2537 | return ret; |
| 2538 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2539 | return avail; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2540 | } |
| 2541 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2542 | /** |
| 2543 | * i915_gem_object_get_fence_reg - set up a fence reg for an object |
| 2544 | * @obj: object to map through a fence reg |
| 2545 | * |
| 2546 | * When mapping objects through the GTT, userspace wants to be able to write |
| 2547 | * to them without having to worry about swizzling if the object is tiled. |
| 2548 | * |
| 2549 | * This function walks the fence regs looking for a free one for @obj, |
| 2550 | * stealing one if it can't find any. |
| 2551 | * |
| 2552 | * It then sets up the reg based on the object's properties: address, pitch |
| 2553 | * and tiling format. |
| 2554 | */ |
Chris Wilson | 8c4b8c3 | 2009-06-17 22:08:52 +0100 | [diff] [blame] | 2555 | int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2556 | i915_gem_object_get_fence_reg(struct drm_i915_gem_object *obj, |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2557 | bool interruptible) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2558 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2559 | struct drm_device *dev = obj->base.dev; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2560 | struct drm_i915_private *dev_priv = dev->dev_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2561 | struct drm_i915_fence_reg *reg = NULL; |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2562 | struct intel_ring_buffer *pipelined = NULL; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2563 | int ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2564 | |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 2565 | /* Just update our place in the LRU if our fence is getting used. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2566 | if (obj->fence_reg != I915_FENCE_REG_NONE) { |
| 2567 | reg = &dev_priv->fence_regs[obj->fence_reg]; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2568 | list_move_tail(®->lru_list, &dev_priv->mm.fence_list); |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 2569 | return 0; |
| 2570 | } |
| 2571 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2572 | switch (obj->tiling_mode) { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2573 | case I915_TILING_NONE: |
| 2574 | WARN(1, "allocating a fence for non-tiled object?\n"); |
| 2575 | break; |
| 2576 | case I915_TILING_X: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2577 | if (!obj->stride) |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2578 | return -EINVAL; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2579 | WARN((obj->stride & (512 - 1)), |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2580 | "object 0x%08x is X tiled but has non-512B pitch\n", |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2581 | obj->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2582 | break; |
| 2583 | case I915_TILING_Y: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2584 | if (!obj->stride) |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2585 | return -EINVAL; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2586 | WARN((obj->stride & (128 - 1)), |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2587 | "object 0x%08x is Y tiled but has non-128B pitch\n", |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2588 | obj->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2589 | break; |
| 2590 | } |
| 2591 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2592 | ret = i915_find_fence_reg(dev, interruptible); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2593 | if (ret < 0) |
| 2594 | return ret; |
Chris Wilson | fc7170b | 2009-02-11 14:26:46 +0000 | [diff] [blame] | 2595 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2596 | obj->fence_reg = ret; |
| 2597 | reg = &dev_priv->fence_regs[obj->fence_reg]; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2598 | list_add_tail(®->lru_list, &dev_priv->mm.fence_list); |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 2599 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2600 | reg->obj = obj; |
| 2601 | |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2602 | switch (INTEL_INFO(dev)->gen) { |
| 2603 | case 6: |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2604 | ret = sandybridge_write_fence_reg(obj, pipelined); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2605 | break; |
| 2606 | case 5: |
| 2607 | case 4: |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2608 | ret = i965_write_fence_reg(obj, pipelined); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2609 | break; |
| 2610 | case 3: |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2611 | ret = i915_write_fence_reg(obj, pipelined); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2612 | break; |
| 2613 | case 2: |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2614 | ret = i830_write_fence_reg(obj, pipelined); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2615 | break; |
| 2616 | } |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 2617 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2618 | trace_i915_gem_object_get_fence(obj, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2619 | obj->fence_reg, |
| 2620 | obj->tiling_mode); |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2621 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | /** |
| 2625 | * i915_gem_clear_fence_reg - clear out fence register info |
| 2626 | * @obj: object to clear |
| 2627 | * |
| 2628 | * Zeroes out the fence register itself and clears out the associated |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2629 | * data structures in dev_priv and obj. |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2630 | */ |
| 2631 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2632 | i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2633 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2634 | struct drm_device *dev = obj->base.dev; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2635 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2636 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[obj->fence_reg]; |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2637 | uint32_t fence_reg; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2638 | |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2639 | switch (INTEL_INFO(dev)->gen) { |
| 2640 | case 6: |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2641 | I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2642 | (obj->fence_reg * 8), 0); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2643 | break; |
| 2644 | case 5: |
| 2645 | case 4: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2646 | I915_WRITE64(FENCE_REG_965_0 + (obj->fence_reg * 8), 0); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2647 | break; |
| 2648 | case 3: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2649 | if (obj->fence_reg >= 8) |
| 2650 | fence_reg = FENCE_REG_945_8 + (obj->fence_reg - 8) * 4; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2651 | else |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2652 | case 2: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2653 | fence_reg = FENCE_REG_830_0 + obj->fence_reg * 4; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2654 | |
| 2655 | I915_WRITE(fence_reg, 0); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2656 | break; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2657 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2658 | |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2659 | reg->obj = NULL; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2660 | obj->fence_reg = I915_FENCE_REG_NONE; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2661 | list_del_init(®->lru_list); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2662 | } |
| 2663 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2664 | /** |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2665 | * i915_gem_object_put_fence_reg - waits on outstanding fenced access |
| 2666 | * to the buffer to finish, and then resets the fence register. |
| 2667 | * @obj: tiled object holding a fence register. |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2668 | * @bool: whether the wait upon the fence is interruptible |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2669 | * |
| 2670 | * Zeroes out the fence register itself and clears out the associated |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2671 | * data structures in dev_priv and obj. |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2672 | */ |
| 2673 | int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2674 | i915_gem_object_put_fence_reg(struct drm_i915_gem_object *obj, |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2675 | bool interruptible) |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2676 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2677 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 2678 | int ret; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2679 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2680 | if (obj->fence_reg == I915_FENCE_REG_NONE) |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 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 | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 2693 | if (obj->fenced_gpu_access) { |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 2694 | ret = i915_gem_object_flush_gpu_write_domain(obj, NULL); |
Chris Wilson | 0bc23aa | 2010-09-14 10:22:23 +0100 | [diff] [blame] | 2695 | if (ret) |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2696 | return ret; |
| 2697 | |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 2698 | obj->fenced_gpu_access = false; |
| 2699 | } |
| 2700 | |
| 2701 | if (obj->last_fenced_seqno) { |
| 2702 | ret = i915_do_wait_request(dev, |
| 2703 | obj->last_fenced_seqno, |
| 2704 | interruptible, |
| 2705 | obj->last_fenced_ring); |
Chris Wilson | 0bc23aa | 2010-09-14 10:22:23 +0100 | [diff] [blame] | 2706 | if (ret) |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2707 | return ret; |
Chris Wilson | 53640e1 | 2010-09-20 11:40:50 +0100 | [diff] [blame] | 2708 | |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 2709 | obj->last_fenced_seqno = false; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2710 | } |
| 2711 | |
Daniel Vetter | 4a72661 | 2010-02-01 13:59:16 +0100 | [diff] [blame] | 2712 | i915_gem_object_flush_gtt_write_domain(obj); |
Chris Wilson | 0bc23aa | 2010-09-14 10:22:23 +0100 | [diff] [blame] | 2713 | i915_gem_clear_fence_reg(obj); |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2714 | |
| 2715 | return 0; |
| 2716 | } |
| 2717 | |
| 2718 | /** |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2719 | * Finds free space in the GTT aperture and binds the object there. |
| 2720 | */ |
| 2721 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2722 | i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj, |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2723 | unsigned alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2724 | bool map_and_fenceable) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2725 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2726 | struct drm_device *dev = obj->base.dev; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2727 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2728 | struct drm_mm_node *free_space; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2729 | gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN; |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 2730 | u32 size, fence_size, fence_alignment, unfenced_alignment; |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2731 | bool mappable, fenceable; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2732 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2733 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2734 | if (obj->madv != I915_MADV_WILLNEED) { |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2735 | DRM_ERROR("Attempting to bind a purgeable object\n"); |
| 2736 | return -EINVAL; |
| 2737 | } |
| 2738 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2739 | fence_size = i915_gem_get_gtt_size(obj); |
| 2740 | fence_alignment = i915_gem_get_gtt_alignment(obj); |
| 2741 | unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2742 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2743 | if (alignment == 0) |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 2744 | alignment = map_and_fenceable ? fence_alignment : |
| 2745 | unfenced_alignment; |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2746 | if (map_and_fenceable && alignment & (fence_alignment - 1)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2747 | DRM_ERROR("Invalid object alignment requested %u\n", alignment); |
| 2748 | return -EINVAL; |
| 2749 | } |
| 2750 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2751 | size = map_and_fenceable ? fence_size : obj->base.size; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2752 | |
Chris Wilson | 654fc60 | 2010-05-27 13:18:21 +0100 | [diff] [blame] | 2753 | /* If the object is bigger than the entire aperture, reject it early |
| 2754 | * before evicting everything in a vain attempt to find space. |
| 2755 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2756 | if (obj->base.size > |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2757 | (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] | 2758 | DRM_ERROR("Attempting to bind an object larger than the aperture\n"); |
| 2759 | return -E2BIG; |
| 2760 | } |
| 2761 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2762 | search_free: |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2763 | if (map_and_fenceable) |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2764 | free_space = |
| 2765 | drm_mm_search_free_in_range(&dev_priv->mm.gtt_space, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2766 | size, alignment, 0, |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2767 | dev_priv->mm.gtt_mappable_end, |
| 2768 | 0); |
| 2769 | else |
| 2770 | free_space = drm_mm_search_free(&dev_priv->mm.gtt_space, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2771 | size, alignment, 0); |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2772 | |
| 2773 | if (free_space != NULL) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2774 | if (map_and_fenceable) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2775 | obj->gtt_space = |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2776 | drm_mm_get_block_range_generic(free_space, |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2777 | size, alignment, 0, |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2778 | dev_priv->mm.gtt_mappable_end, |
| 2779 | 0); |
| 2780 | else |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2781 | obj->gtt_space = |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2782 | drm_mm_get_block(free_space, size, alignment); |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2783 | } |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2784 | if (obj->gtt_space == NULL) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2785 | /* If the gtt is empty and we're still having trouble |
| 2786 | * fitting our object in, we're out of memory. |
| 2787 | */ |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2788 | ret = i915_gem_evict_something(dev, size, alignment, |
| 2789 | map_and_fenceable); |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 2790 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2791 | return ret; |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 2792 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2793 | goto search_free; |
| 2794 | } |
| 2795 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2796 | ret = i915_gem_object_get_pages_gtt(obj, gfpmask); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2797 | if (ret) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2798 | drm_mm_put_block(obj->gtt_space); |
| 2799 | obj->gtt_space = NULL; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2800 | |
| 2801 | if (ret == -ENOMEM) { |
| 2802 | /* first try to clear up some space from the GTT */ |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2803 | ret = i915_gem_evict_something(dev, size, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2804 | alignment, |
| 2805 | map_and_fenceable); |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2806 | if (ret) { |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2807 | /* now try to shrink everyone else */ |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 2808 | if (gfpmask) { |
| 2809 | gfpmask = 0; |
| 2810 | goto search_free; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2811 | } |
| 2812 | |
| 2813 | return ret; |
| 2814 | } |
| 2815 | |
| 2816 | goto search_free; |
| 2817 | } |
| 2818 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2819 | return ret; |
| 2820 | } |
| 2821 | |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 2822 | ret = i915_gem_gtt_bind_object(obj); |
| 2823 | if (ret) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 2824 | i915_gem_object_put_pages_gtt(obj); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2825 | drm_mm_put_block(obj->gtt_space); |
| 2826 | obj->gtt_space = NULL; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2827 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2828 | ret = i915_gem_evict_something(dev, size, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2829 | alignment, map_and_fenceable); |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 2830 | if (ret) |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2831 | return ret; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2832 | |
| 2833 | goto search_free; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2834 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2835 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2836 | obj->gtt_offset = obj->gtt_space->start; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 2837 | |
Chris Wilson | bf1a109 | 2010-08-07 11:01:20 +0100 | [diff] [blame] | 2838 | /* keep track of bounds object by adding it to the inactive list */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2839 | list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list); |
| 2840 | i915_gem_info_add_gtt(dev_priv, obj); |
Chris Wilson | bf1a109 | 2010-08-07 11:01:20 +0100 | [diff] [blame] | 2841 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2842 | /* Assert that the object is not currently in any GPU domain. As it |
| 2843 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2844 | * a GPU cache |
| 2845 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2846 | BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS); |
| 2847 | BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2848 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2849 | trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2850 | |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2851 | fenceable = |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2852 | obj->gtt_space->size == fence_size && |
| 2853 | (obj->gtt_space->start & (fence_alignment -1)) == 0; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2854 | |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2855 | mappable = |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2856 | obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 2857 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2858 | obj->map_and_fenceable = mappable && fenceable; |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2859 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2860 | return 0; |
| 2861 | } |
| 2862 | |
| 2863 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2864 | i915_gem_clflush_object(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2865 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2866 | /* If we don't have a page list set up, then we're not pinned |
| 2867 | * to GPU, and we can ignore the cache flush because it'll happen |
| 2868 | * again at bind time. |
| 2869 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2870 | if (obj->pages == NULL) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2871 | return; |
| 2872 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2873 | trace_i915_gem_object_clflush(obj); |
Eric Anholt | cfa16a0 | 2009-05-26 18:46:16 -0700 | [diff] [blame] | 2874 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2875 | drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2878 | /** Flushes any GPU write domain for the object if it's dirty. */ |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2879 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2880 | i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj, |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 2881 | struct intel_ring_buffer *pipelined) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2882 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2883 | struct drm_device *dev = obj->base.dev; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2884 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2885 | if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0) |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2886 | return 0; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2887 | |
| 2888 | /* Queue the GPU write cache flushing we need. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2889 | i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain); |
| 2890 | BUG_ON(obj->base.write_domain); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2891 | |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 2892 | if (pipelined && pipelined == obj->ring) |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2893 | return 0; |
| 2894 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2895 | return i915_gem_object_wait_rendering(obj, true); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2896 | } |
| 2897 | |
| 2898 | /** Flushes the GTT write domain for the object if it's dirty. */ |
| 2899 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2900 | i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2901 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2902 | uint32_t old_write_domain; |
| 2903 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2904 | if (obj->base.write_domain != I915_GEM_DOMAIN_GTT) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2905 | return; |
| 2906 | |
| 2907 | /* No actual flushing is required for the GTT write domain. Writes |
| 2908 | * to it immediately go to main memory as far as we know, so there's |
| 2909 | * no chipset flush. It also doesn't land in render cache. |
| 2910 | */ |
Chris Wilson | 4a684a4 | 2010-10-28 14:44:08 +0100 | [diff] [blame] | 2911 | i915_gem_release_mmap(obj); |
| 2912 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2913 | old_write_domain = obj->base.write_domain; |
| 2914 | obj->base.write_domain = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2915 | |
| 2916 | trace_i915_gem_object_change_domain(obj, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2917 | obj->base.read_domains, |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2918 | old_write_domain); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2919 | } |
| 2920 | |
| 2921 | /** Flushes the CPU write domain for the object if it's dirty. */ |
| 2922 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2923 | i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2924 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2925 | uint32_t old_write_domain; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2926 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2927 | if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2928 | return; |
| 2929 | |
| 2930 | i915_gem_clflush_object(obj); |
Daniel Vetter | 40ce657 | 2010-11-05 18:12:18 +0100 | [diff] [blame] | 2931 | intel_gtt_chipset_flush(); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2932 | old_write_domain = obj->base.write_domain; |
| 2933 | obj->base.write_domain = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2934 | |
| 2935 | trace_i915_gem_object_change_domain(obj, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2936 | obj->base.read_domains, |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2937 | old_write_domain); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2938 | } |
| 2939 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2940 | /** |
| 2941 | * Moves a single object to the GTT read, and possibly write domain. |
| 2942 | * |
| 2943 | * This function returns when the move is complete, including waiting on |
| 2944 | * flushes to occur. |
| 2945 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2946 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame^] | 2947 | i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2948 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2949 | uint32_t old_write_domain, old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2950 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2951 | |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 2952 | /* Not valid to be called on unbound objects. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2953 | if (obj->gtt_space == NULL) |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 2954 | return -EINVAL; |
| 2955 | |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 2956 | ret = i915_gem_object_flush_gpu_write_domain(obj, NULL); |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2957 | if (ret != 0) |
| 2958 | return ret; |
| 2959 | |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 2960 | i915_gem_object_flush_cpu_write_domain(obj); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2961 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2962 | if (write) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2963 | ret = i915_gem_object_wait_rendering(obj, true); |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2964 | if (ret) |
| 2965 | return ret; |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2966 | } |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2967 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2968 | old_write_domain = obj->base.write_domain; |
| 2969 | old_read_domains = obj->base.read_domains; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2970 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2971 | /* It should now be out of any other write domains, and we can update |
| 2972 | * the domain values for our changes. |
| 2973 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2974 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 2975 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2976 | if (write) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2977 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
| 2978 | obj->base.write_domain = I915_GEM_DOMAIN_GTT; |
| 2979 | obj->dirty = 1; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2980 | } |
| 2981 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2982 | trace_i915_gem_object_change_domain(obj, |
| 2983 | old_read_domains, |
| 2984 | old_write_domain); |
| 2985 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2986 | return 0; |
| 2987 | } |
| 2988 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2989 | /* |
| 2990 | * Prepare buffer for display plane. Use uninterruptible for possible flush |
| 2991 | * wait, as in modesetting process we're not supposed to be interrupted. |
| 2992 | */ |
| 2993 | int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2994 | i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj, |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 2995 | struct intel_ring_buffer *pipelined) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2996 | { |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2997 | uint32_t old_read_domains; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2998 | int ret; |
| 2999 | |
| 3000 | /* Not valid to be called on unbound objects. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3001 | if (obj->gtt_space == NULL) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3002 | return -EINVAL; |
| 3003 | |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3004 | ret = i915_gem_object_flush_gpu_write_domain(obj, pipelined); |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 3005 | if (ret) |
| 3006 | return ret; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3007 | |
Chris Wilson | ced270f | 2010-09-26 22:47:46 +0100 | [diff] [blame] | 3008 | /* Currently, we are always called from an non-interruptible context. */ |
| 3009 | if (!pipelined) { |
| 3010 | ret = i915_gem_object_wait_rendering(obj, false); |
| 3011 | if (ret) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3012 | return ret; |
| 3013 | } |
| 3014 | |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3015 | i915_gem_object_flush_cpu_write_domain(obj); |
| 3016 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3017 | old_read_domains = obj->base.read_domains; |
| 3018 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3019 | |
| 3020 | trace_i915_gem_object_change_domain(obj, |
| 3021 | old_read_domains, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3022 | obj->base.write_domain); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3023 | |
| 3024 | return 0; |
| 3025 | } |
| 3026 | |
Chris Wilson | 8534551 | 2010-11-13 09:49:11 +0000 | [diff] [blame] | 3027 | int |
| 3028 | i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj, |
| 3029 | bool interruptible) |
| 3030 | { |
| 3031 | if (!obj->active) |
| 3032 | return 0; |
| 3033 | |
| 3034 | if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3035 | i915_gem_flush_ring(obj->base.dev, obj->ring, |
Chris Wilson | 8534551 | 2010-11-13 09:49:11 +0000 | [diff] [blame] | 3036 | 0, obj->base.write_domain); |
| 3037 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3038 | return i915_gem_object_wait_rendering(obj, interruptible); |
Chris Wilson | 8534551 | 2010-11-13 09:49:11 +0000 | [diff] [blame] | 3039 | } |
| 3040 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3041 | /** |
| 3042 | * Moves a single object to the CPU read, and possibly write domain. |
| 3043 | * |
| 3044 | * This function returns when the move is complete, including waiting on |
| 3045 | * flushes to occur. |
| 3046 | */ |
| 3047 | static int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3048 | i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3049 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3050 | uint32_t old_write_domain, old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3051 | int ret; |
| 3052 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 3053 | ret = i915_gem_object_flush_gpu_write_domain(obj, false); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3054 | if (ret != 0) |
| 3055 | return ret; |
| 3056 | |
| 3057 | i915_gem_object_flush_gtt_write_domain(obj); |
| 3058 | |
| 3059 | /* If we have a partially-valid cache of the object in the CPU, |
| 3060 | * finish invalidating it and free the per-page flags. |
| 3061 | */ |
| 3062 | i915_gem_object_set_to_full_cpu_read_domain(obj); |
| 3063 | |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 3064 | if (write) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 3065 | ret = i915_gem_object_wait_rendering(obj, true); |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 3066 | if (ret) |
| 3067 | return ret; |
| 3068 | } |
| 3069 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3070 | old_write_domain = obj->base.write_domain; |
| 3071 | old_read_domains = obj->base.read_domains; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3072 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3073 | /* Flush the CPU cache if it's still invalid. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3074 | if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3075 | i915_gem_clflush_object(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3076 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3077 | obj->base.read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | /* It should now be out of any other write domains, and we can update |
| 3081 | * the domain values for our changes. |
| 3082 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3083 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3084 | |
| 3085 | /* If we're writing through the CPU, then the GPU read domains will |
| 3086 | * need to be invalidated at next use. |
| 3087 | */ |
| 3088 | if (write) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3089 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 3090 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3091 | } |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3092 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3093 | trace_i915_gem_object_change_domain(obj, |
| 3094 | old_read_domains, |
| 3095 | old_write_domain); |
| 3096 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3097 | return 0; |
| 3098 | } |
| 3099 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3100 | /* |
| 3101 | * Set the next domain for the specified object. This |
| 3102 | * may not actually perform the necessary flushing/invaliding though, |
| 3103 | * as that may want to be batched with other set_domain operations |
| 3104 | * |
| 3105 | * This is (we hope) the only really tricky part of gem. The goal |
| 3106 | * is fairly simple -- track which caches hold bits of the object |
| 3107 | * and make sure they remain coherent. A few concrete examples may |
| 3108 | * help to explain how it works. For shorthand, we use the notation |
| 3109 | * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the |
| 3110 | * a pair of read and write domain masks. |
| 3111 | * |
| 3112 | * Case 1: the batch buffer |
| 3113 | * |
| 3114 | * 1. Allocated |
| 3115 | * 2. Written by CPU |
| 3116 | * 3. Mapped to GTT |
| 3117 | * 4. Read by GPU |
| 3118 | * 5. Unmapped from GTT |
| 3119 | * 6. Freed |
| 3120 | * |
| 3121 | * Let's take these a step at a time |
| 3122 | * |
| 3123 | * 1. Allocated |
| 3124 | * Pages allocated from the kernel may still have |
| 3125 | * cache contents, so we set them to (CPU, CPU) always. |
| 3126 | * 2. Written by CPU (using pwrite) |
| 3127 | * The pwrite function calls set_domain (CPU, CPU) and |
| 3128 | * this function does nothing (as nothing changes) |
| 3129 | * 3. Mapped by GTT |
| 3130 | * This function asserts that the object is not |
| 3131 | * currently in any GPU-based read or write domains |
| 3132 | * 4. Read by GPU |
| 3133 | * i915_gem_execbuffer calls set_domain (COMMAND, 0). |
| 3134 | * As write_domain is zero, this function adds in the |
| 3135 | * current read domains (CPU+COMMAND, 0). |
| 3136 | * flush_domains is set to CPU. |
| 3137 | * invalidate_domains is set to COMMAND |
| 3138 | * clflush is run to get data out of the CPU caches |
| 3139 | * then i915_dev_set_domain calls i915_gem_flush to |
| 3140 | * emit an MI_FLUSH and drm_agp_chipset_flush |
| 3141 | * 5. Unmapped from GTT |
| 3142 | * i915_gem_object_unbind calls set_domain (CPU, CPU) |
| 3143 | * flush_domains and invalidate_domains end up both zero |
| 3144 | * so no flushing/invalidating happens |
| 3145 | * 6. Freed |
| 3146 | * yay, done |
| 3147 | * |
| 3148 | * Case 2: The shared render buffer |
| 3149 | * |
| 3150 | * 1. Allocated |
| 3151 | * 2. Mapped to GTT |
| 3152 | * 3. Read/written by GPU |
| 3153 | * 4. set_domain to (CPU,CPU) |
| 3154 | * 5. Read/written by CPU |
| 3155 | * 6. Read/written by GPU |
| 3156 | * |
| 3157 | * 1. Allocated |
| 3158 | * Same as last example, (CPU, CPU) |
| 3159 | * 2. Mapped to GTT |
| 3160 | * Nothing changes (assertions find that it is not in the GPU) |
| 3161 | * 3. Read/written by GPU |
| 3162 | * execbuffer calls set_domain (RENDER, RENDER) |
| 3163 | * flush_domains gets CPU |
| 3164 | * invalidate_domains gets GPU |
| 3165 | * clflush (obj) |
| 3166 | * MI_FLUSH and drm_agp_chipset_flush |
| 3167 | * 4. set_domain (CPU, CPU) |
| 3168 | * flush_domains gets GPU |
| 3169 | * invalidate_domains gets CPU |
| 3170 | * wait_rendering (obj) to make sure all drawing is complete. |
| 3171 | * This will include an MI_FLUSH to get the data from GPU |
| 3172 | * to memory |
| 3173 | * clflush (obj) to invalidate the CPU cache |
| 3174 | * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?) |
| 3175 | * 5. Read/written by CPU |
| 3176 | * cache lines are loaded and dirtied |
| 3177 | * 6. Read written by GPU |
| 3178 | * Same as last GPU access |
| 3179 | * |
| 3180 | * Case 3: The constant buffer |
| 3181 | * |
| 3182 | * 1. Allocated |
| 3183 | * 2. Written by CPU |
| 3184 | * 3. Read by GPU |
| 3185 | * 4. Updated (written) by CPU again |
| 3186 | * 5. Read by GPU |
| 3187 | * |
| 3188 | * 1. Allocated |
| 3189 | * (CPU, CPU) |
| 3190 | * 2. Written by CPU |
| 3191 | * (CPU, CPU) |
| 3192 | * 3. Read by GPU |
| 3193 | * (CPU+RENDER, 0) |
| 3194 | * flush_domains = CPU |
| 3195 | * invalidate_domains = RENDER |
| 3196 | * clflush (obj) |
| 3197 | * MI_FLUSH |
| 3198 | * drm_agp_chipset_flush |
| 3199 | * 4. Updated (written) by CPU again |
| 3200 | * (CPU, CPU) |
| 3201 | * flush_domains = 0 (no previous write domain) |
| 3202 | * invalidate_domains = 0 (no new read domains) |
| 3203 | * 5. Read by GPU |
| 3204 | * (CPU+RENDER, 0) |
| 3205 | * flush_domains = CPU |
| 3206 | * invalidate_domains = RENDER |
| 3207 | * clflush (obj) |
| 3208 | * MI_FLUSH |
| 3209 | * drm_agp_chipset_flush |
| 3210 | */ |
Keith Packard | c0d9082 | 2008-11-20 23:11:08 -0800 | [diff] [blame] | 3211 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3212 | i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj, |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3213 | struct intel_ring_buffer *ring, |
| 3214 | struct change_domains *cd) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3215 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3216 | uint32_t invalidate_domains = 0, flush_domains = 0; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 3217 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3218 | /* |
| 3219 | * If the object isn't moving to a new write domain, |
| 3220 | * let the object stay in multiple read domains |
| 3221 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3222 | if (obj->base.pending_write_domain == 0) |
| 3223 | obj->base.pending_read_domains |= obj->base.read_domains; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3224 | |
| 3225 | /* |
| 3226 | * Flush the current write domain if |
| 3227 | * the new read domains don't match. Invalidate |
| 3228 | * any read domains which differ from the old |
| 3229 | * write domain |
| 3230 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3231 | if (obj->base.write_domain && |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 3232 | (((obj->base.write_domain != obj->base.pending_read_domains || |
| 3233 | obj->ring != ring)) || |
| 3234 | (obj->fenced_gpu_access && !obj->pending_fenced_gpu_access))) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3235 | flush_domains |= obj->base.write_domain; |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 3236 | invalidate_domains |= |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3237 | obj->base.pending_read_domains & ~obj->base.write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3238 | } |
| 3239 | /* |
| 3240 | * Invalidate any read caches which may have |
| 3241 | * stale data. That is, any new read domains. |
| 3242 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3243 | invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains; |
Chris Wilson | 3d2a812 | 2010-09-29 11:39:53 +0100 | [diff] [blame] | 3244 | if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3245 | i915_gem_clflush_object(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3246 | |
Chris Wilson | 4a684a4 | 2010-10-28 14:44:08 +0100 | [diff] [blame] | 3247 | /* blow away mappings if mapped through GTT */ |
| 3248 | if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT) |
| 3249 | i915_gem_release_mmap(obj); |
| 3250 | |
Eric Anholt | efbeed9 | 2009-02-19 14:54:51 -0800 | [diff] [blame] | 3251 | /* The actual obj->write_domain will be updated with |
| 3252 | * pending_write_domain after we emit the accumulated flush for all |
| 3253 | * of our domain changes in execbuffers (which clears objects' |
| 3254 | * write_domains). So if we have a current write domain that we |
| 3255 | * aren't changing, set pending_write_domain to that. |
| 3256 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3257 | if (flush_domains == 0 && obj->base.pending_write_domain == 0) |
| 3258 | obj->base.pending_write_domain = obj->base.write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3259 | |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3260 | cd->invalidate_domains |= invalidate_domains; |
| 3261 | cd->flush_domains |= flush_domains; |
Chris Wilson | b665145 | 2010-10-23 10:15:06 +0100 | [diff] [blame] | 3262 | if (flush_domains & I915_GEM_GPU_DOMAINS) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3263 | cd->flush_rings |= obj->ring->id; |
Chris Wilson | b665145 | 2010-10-23 10:15:06 +0100 | [diff] [blame] | 3264 | if (invalidate_domains & I915_GEM_GPU_DOMAINS) |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3265 | cd->flush_rings |= ring->id; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3266 | } |
| 3267 | |
| 3268 | /** |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3269 | * Moves the object from a partially CPU read to a full one. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3270 | * |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3271 | * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(), |
| 3272 | * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU). |
| 3273 | */ |
| 3274 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3275 | i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3276 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3277 | if (!obj->page_cpu_valid) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3278 | return; |
| 3279 | |
| 3280 | /* If we're partially in the CPU read domain, finish moving it in. |
| 3281 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3282 | if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3283 | int i; |
| 3284 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3285 | for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) { |
| 3286 | if (obj->page_cpu_valid[i]) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3287 | continue; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3288 | drm_clflush_pages(obj->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 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3295 | kfree(obj->page_cpu_valid); |
| 3296 | obj->page_cpu_valid = NULL; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 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 |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3312 | i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj, |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3313 | uint64_t offset, uint64_t size) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3314 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3315 | uint32_t old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3316 | int i, ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3317 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3318 | if (offset == 0 && size == obj->base.size) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3319 | return i915_gem_object_set_to_cpu_domain(obj, 0); |
| 3320 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 3321 | ret = i915_gem_object_flush_gpu_write_domain(obj, false); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3322 | if (ret != 0) |
| 3323 | return ret; |
| 3324 | i915_gem_object_flush_gtt_write_domain(obj); |
| 3325 | |
| 3326 | /* If we're already fully in the CPU read domain, we're done. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3327 | if (obj->page_cpu_valid == NULL && |
| 3328 | (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3329 | return 0; |
| 3330 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3331 | /* Otherwise, create/clear the per-page CPU read domain flag if we're |
| 3332 | * newly adding I915_GEM_DOMAIN_CPU |
| 3333 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3334 | if (obj->page_cpu_valid == NULL) { |
| 3335 | obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE, |
| 3336 | GFP_KERNEL); |
| 3337 | if (obj->page_cpu_valid == NULL) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3338 | return -ENOMEM; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3339 | } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) |
| 3340 | memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3341 | |
| 3342 | /* Flush the cache on any pages that are still invalid from the CPU's |
| 3343 | * perspective. |
| 3344 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3345 | for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE; |
| 3346 | i++) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3347 | if (obj->page_cpu_valid[i]) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3348 | continue; |
| 3349 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3350 | drm_clflush_pages(obj->pages + i, 1); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3351 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3352 | obj->page_cpu_valid[i] = 1; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3353 | } |
| 3354 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3355 | /* It should now be out of any other write domains, and we can update |
| 3356 | * the domain values for our changes. |
| 3357 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3358 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3359 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3360 | old_read_domains = obj->base.read_domains; |
| 3361 | obj->base.read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3362 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3363 | trace_i915_gem_object_change_domain(obj, |
| 3364 | old_read_domains, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3365 | obj->base.write_domain); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3366 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3367 | return 0; |
| 3368 | } |
| 3369 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3370 | static int |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3371 | i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj, |
| 3372 | struct drm_file *file_priv, |
| 3373 | struct drm_i915_gem_exec_object2 *entry, |
| 3374 | struct drm_i915_gem_relocation_entry *reloc) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3375 | { |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3376 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3377 | struct drm_gem_object *target_obj; |
| 3378 | uint32_t target_offset; |
| 3379 | int ret = -EINVAL; |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3380 | |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3381 | target_obj = drm_gem_object_lookup(dev, file_priv, |
| 3382 | reloc->target_handle); |
| 3383 | if (target_obj == NULL) |
| 3384 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3385 | |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3386 | target_offset = to_intel_bo(target_obj)->gtt_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3387 | |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3388 | #if WATCH_RELOC |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3389 | DRM_INFO("%s: obj %p offset %08x target %d " |
| 3390 | "read %08x write %08x gtt %08x " |
| 3391 | "presumed %08x delta %08x\n", |
| 3392 | __func__, |
| 3393 | obj, |
| 3394 | (int) reloc->offset, |
| 3395 | (int) reloc->target_handle, |
| 3396 | (int) reloc->read_domains, |
| 3397 | (int) reloc->write_domain, |
| 3398 | (int) target_offset, |
| 3399 | (int) reloc->presumed_offset, |
| 3400 | reloc->delta); |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3401 | #endif |
| 3402 | |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3403 | /* The target buffer should have appeared before us in the |
| 3404 | * exec_object list, so it should have a GTT space bound by now. |
| 3405 | */ |
| 3406 | if (target_offset == 0) { |
| 3407 | DRM_ERROR("No GTT space found for object %d\n", |
| 3408 | reloc->target_handle); |
| 3409 | goto err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3410 | } |
| 3411 | |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3412 | /* Validate that the target is in a valid r/w GPU domain */ |
| 3413 | if (reloc->write_domain & (reloc->write_domain - 1)) { |
| 3414 | DRM_ERROR("reloc with multiple write domains: " |
| 3415 | "obj %p target %d offset %d " |
| 3416 | "read %08x write %08x", |
| 3417 | obj, reloc->target_handle, |
| 3418 | (int) reloc->offset, |
| 3419 | reloc->read_domains, |
| 3420 | reloc->write_domain); |
| 3421 | goto err; |
| 3422 | } |
| 3423 | if (reloc->write_domain & I915_GEM_DOMAIN_CPU || |
| 3424 | reloc->read_domains & I915_GEM_DOMAIN_CPU) { |
| 3425 | DRM_ERROR("reloc with read/write CPU domains: " |
| 3426 | "obj %p target %d offset %d " |
| 3427 | "read %08x write %08x", |
| 3428 | obj, reloc->target_handle, |
| 3429 | (int) reloc->offset, |
| 3430 | reloc->read_domains, |
| 3431 | reloc->write_domain); |
| 3432 | goto err; |
| 3433 | } |
| 3434 | if (reloc->write_domain && target_obj->pending_write_domain && |
| 3435 | reloc->write_domain != target_obj->pending_write_domain) { |
| 3436 | DRM_ERROR("Write domain conflict: " |
| 3437 | "obj %p target %d offset %d " |
| 3438 | "new %08x old %08x\n", |
| 3439 | obj, reloc->target_handle, |
| 3440 | (int) reloc->offset, |
| 3441 | reloc->write_domain, |
| 3442 | target_obj->pending_write_domain); |
| 3443 | goto err; |
| 3444 | } |
| 3445 | |
| 3446 | target_obj->pending_read_domains |= reloc->read_domains; |
| 3447 | target_obj->pending_write_domain |= reloc->write_domain; |
| 3448 | |
| 3449 | /* If the relocation already has the right value in it, no |
| 3450 | * more work needs to be done. |
| 3451 | */ |
| 3452 | if (target_offset == reloc->presumed_offset) |
| 3453 | goto out; |
| 3454 | |
| 3455 | /* Check that the relocation address is valid... */ |
| 3456 | if (reloc->offset > obj->base.size - 4) { |
| 3457 | DRM_ERROR("Relocation beyond object bounds: " |
| 3458 | "obj %p target %d offset %d size %d.\n", |
| 3459 | obj, reloc->target_handle, |
| 3460 | (int) reloc->offset, |
| 3461 | (int) obj->base.size); |
| 3462 | goto err; |
| 3463 | } |
| 3464 | if (reloc->offset & 3) { |
| 3465 | DRM_ERROR("Relocation not 4-byte aligned: " |
| 3466 | "obj %p target %d offset %d.\n", |
| 3467 | obj, reloc->target_handle, |
| 3468 | (int) reloc->offset); |
| 3469 | goto err; |
| 3470 | } |
| 3471 | |
| 3472 | /* and points to somewhere within the target object. */ |
| 3473 | if (reloc->delta >= target_obj->size) { |
| 3474 | DRM_ERROR("Relocation beyond target object bounds: " |
| 3475 | "obj %p target %d delta %d size %d.\n", |
| 3476 | obj, reloc->target_handle, |
| 3477 | (int) reloc->delta, |
| 3478 | (int) target_obj->size); |
| 3479 | goto err; |
| 3480 | } |
| 3481 | |
| 3482 | reloc->delta += target_offset; |
| 3483 | if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) { |
| 3484 | uint32_t page_offset = reloc->offset & ~PAGE_MASK; |
| 3485 | char *vaddr; |
| 3486 | |
| 3487 | vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]); |
| 3488 | *(uint32_t *)(vaddr + page_offset) = reloc->delta; |
| 3489 | kunmap_atomic(vaddr); |
| 3490 | } else { |
| 3491 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 3492 | uint32_t __iomem *reloc_entry; |
| 3493 | void __iomem *reloc_page; |
| 3494 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3495 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3496 | if (ret) |
| 3497 | goto err; |
| 3498 | |
| 3499 | /* Map the page containing the relocation we're going to perform. */ |
| 3500 | reloc->offset += obj->gtt_offset; |
| 3501 | reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping, |
| 3502 | reloc->offset & PAGE_MASK); |
| 3503 | reloc_entry = (uint32_t __iomem *) |
| 3504 | (reloc_page + (reloc->offset & ~PAGE_MASK)); |
| 3505 | iowrite32(reloc->delta, reloc_entry); |
| 3506 | io_mapping_unmap_atomic(reloc_page); |
| 3507 | } |
| 3508 | |
| 3509 | /* and update the user's relocation entry */ |
| 3510 | reloc->presumed_offset = target_offset; |
| 3511 | |
| 3512 | out: |
| 3513 | ret = 0; |
| 3514 | err: |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3515 | drm_gem_object_unreference(target_obj); |
| 3516 | return ret; |
| 3517 | } |
| 3518 | |
| 3519 | static int |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3520 | i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj, |
| 3521 | struct drm_file *file_priv, |
| 3522 | struct drm_i915_gem_exec_object2 *entry) |
| 3523 | { |
| 3524 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
| 3525 | int i, ret; |
| 3526 | |
| 3527 | user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr; |
| 3528 | for (i = 0; i < entry->relocation_count; i++) { |
| 3529 | struct drm_i915_gem_relocation_entry reloc; |
| 3530 | |
| 3531 | if (__copy_from_user_inatomic(&reloc, |
| 3532 | user_relocs+i, |
| 3533 | sizeof(reloc))) |
| 3534 | return -EFAULT; |
| 3535 | |
| 3536 | ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc); |
| 3537 | if (ret) |
| 3538 | return ret; |
| 3539 | |
| 3540 | if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset, |
| 3541 | &reloc.presumed_offset, |
| 3542 | sizeof(reloc.presumed_offset))) |
| 3543 | return -EFAULT; |
| 3544 | } |
| 3545 | |
| 3546 | return 0; |
| 3547 | } |
| 3548 | |
| 3549 | static int |
| 3550 | i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj, |
| 3551 | struct drm_file *file_priv, |
| 3552 | struct drm_i915_gem_exec_object2 *entry, |
| 3553 | struct drm_i915_gem_relocation_entry *relocs) |
| 3554 | { |
| 3555 | int i, ret; |
| 3556 | |
| 3557 | for (i = 0; i < entry->relocation_count; i++) { |
| 3558 | ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]); |
| 3559 | if (ret) |
| 3560 | return ret; |
| 3561 | } |
| 3562 | |
| 3563 | return 0; |
| 3564 | } |
| 3565 | |
| 3566 | static int |
| 3567 | i915_gem_execbuffer_relocate(struct drm_device *dev, |
| 3568 | struct drm_file *file, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3569 | struct drm_i915_gem_object **object_list, |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3570 | struct drm_i915_gem_exec_object2 *exec_list, |
| 3571 | int count) |
| 3572 | { |
| 3573 | int i, ret; |
| 3574 | |
| 3575 | for (i = 0; i < count; i++) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3576 | struct drm_i915_gem_object *obj = object_list[i]; |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3577 | obj->base.pending_read_domains = 0; |
| 3578 | obj->base.pending_write_domain = 0; |
| 3579 | ret = i915_gem_execbuffer_relocate_object(obj, file, |
| 3580 | &exec_list[i]); |
| 3581 | if (ret) |
| 3582 | return ret; |
| 3583 | } |
| 3584 | |
| 3585 | return 0; |
| 3586 | } |
| 3587 | |
| 3588 | static int |
| 3589 | i915_gem_execbuffer_reserve(struct drm_device *dev, |
| 3590 | struct drm_file *file, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3591 | struct drm_i915_gem_object **object_list, |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3592 | struct drm_i915_gem_exec_object2 *exec_list, |
| 3593 | int count) |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3594 | { |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3595 | int ret, i, retry; |
| 3596 | |
Chris Wilson | a7a09ae | 2010-11-12 13:49:09 +0000 | [diff] [blame] | 3597 | /* Attempt to pin all of the buffers into the GTT. |
| 3598 | * This is done in 3 phases: |
| 3599 | * |
| 3600 | * 1a. Unbind all objects that do not match the GTT constraints for |
| 3601 | * the execbuffer (fenceable, mappable, alignment etc). |
| 3602 | * 1b. Increment pin count for already bound objects. |
| 3603 | * 2. Bind new objects. |
| 3604 | * 3. Decrement pin count. |
| 3605 | * |
| 3606 | * This avoid unnecessary unbinding of later objects in order to makr |
| 3607 | * room for the earlier objects *unless* we need to defragment. |
| 3608 | */ |
Chris Wilson | 5eac3ab | 2010-10-31 08:49:47 +0000 | [diff] [blame] | 3609 | retry = 0; |
| 3610 | do { |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3611 | ret = 0; |
Chris Wilson | a7a09ae | 2010-11-12 13:49:09 +0000 | [diff] [blame] | 3612 | |
| 3613 | /* Unbind any ill-fitting objects or pin. */ |
| 3614 | for (i = 0; i < count; i++) { |
| 3615 | struct drm_i915_gem_object *obj = object_list[i]; |
| 3616 | struct drm_i915_gem_exec_object2 *entry = &exec_list[i]; |
| 3617 | bool need_fence, need_mappable; |
| 3618 | |
| 3619 | if (!obj->gtt_space) |
| 3620 | continue; |
| 3621 | |
| 3622 | need_fence = |
| 3623 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 3624 | obj->tiling_mode != I915_TILING_NONE; |
| 3625 | need_mappable = |
| 3626 | entry->relocation_count ? true : need_fence; |
| 3627 | |
| 3628 | if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) || |
| 3629 | (need_mappable && !obj->map_and_fenceable)) |
| 3630 | ret = i915_gem_object_unbind(obj); |
| 3631 | else |
| 3632 | ret = i915_gem_object_pin(obj, |
| 3633 | entry->alignment, |
| 3634 | need_mappable); |
| 3635 | if (ret) { |
| 3636 | count = i; |
| 3637 | goto err; |
| 3638 | } |
| 3639 | } |
| 3640 | |
| 3641 | /* Bind fresh objects */ |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3642 | for (i = 0; i < count; i++) { |
| 3643 | struct drm_i915_gem_exec_object2 *entry = &exec_list[i]; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3644 | struct drm_i915_gem_object *obj = object_list[i]; |
Chris Wilson | a7a09ae | 2010-11-12 13:49:09 +0000 | [diff] [blame] | 3645 | bool need_fence; |
| 3646 | |
| 3647 | need_fence = |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3648 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 3649 | obj->tiling_mode != I915_TILING_NONE; |
| 3650 | |
Chris Wilson | a7a09ae | 2010-11-12 13:49:09 +0000 | [diff] [blame] | 3651 | if (!obj->gtt_space) { |
| 3652 | bool need_mappable = |
| 3653 | entry->relocation_count ? true : need_fence; |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 3654 | |
Chris Wilson | a7a09ae | 2010-11-12 13:49:09 +0000 | [diff] [blame] | 3655 | ret = i915_gem_object_pin(obj, |
| 3656 | entry->alignment, |
| 3657 | need_mappable); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3658 | if (ret) |
| 3659 | break; |
| 3660 | } |
| 3661 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3662 | if (need_fence) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3663 | ret = i915_gem_object_get_fence_reg(obj, true); |
Chris Wilson | a7a09ae | 2010-11-12 13:49:09 +0000 | [diff] [blame] | 3664 | if (ret) |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3665 | break; |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3666 | |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 3667 | obj->pending_fenced_gpu_access = true; |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | entry->offset = obj->gtt_offset; |
| 3671 | } |
| 3672 | |
Chris Wilson | a7a09ae | 2010-11-12 13:49:09 +0000 | [diff] [blame] | 3673 | err: /* Decrement pin count for bound objects */ |
| 3674 | for (i = 0; i < count; i++) { |
| 3675 | struct drm_i915_gem_object *obj = object_list[i]; |
| 3676 | if (obj->gtt_space) |
| 3677 | i915_gem_object_unpin(obj); |
| 3678 | } |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3679 | |
Chris Wilson | 5eac3ab | 2010-10-31 08:49:47 +0000 | [diff] [blame] | 3680 | if (ret != -ENOSPC || retry > 1) |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3681 | return ret; |
| 3682 | |
Chris Wilson | 5eac3ab | 2010-10-31 08:49:47 +0000 | [diff] [blame] | 3683 | /* First attempt, just clear anything that is purgeable. |
| 3684 | * Second attempt, clear the entire GTT. |
| 3685 | */ |
| 3686 | ret = i915_gem_evict_everything(dev, retry == 0); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3687 | if (ret) |
| 3688 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3689 | |
Chris Wilson | 5eac3ab | 2010-10-31 08:49:47 +0000 | [diff] [blame] | 3690 | retry++; |
| 3691 | } while (1); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3692 | } |
| 3693 | |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3694 | static int |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3695 | i915_gem_execbuffer_relocate_slow(struct drm_device *dev, |
| 3696 | struct drm_file *file, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3697 | struct drm_i915_gem_object **object_list, |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3698 | struct drm_i915_gem_exec_object2 *exec_list, |
| 3699 | int count) |
| 3700 | { |
| 3701 | struct drm_i915_gem_relocation_entry *reloc; |
| 3702 | int i, total, ret; |
| 3703 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3704 | for (i = 0; i < count; i++) |
| 3705 | object_list[i]->in_execbuffer = false; |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3706 | |
| 3707 | mutex_unlock(&dev->struct_mutex); |
| 3708 | |
| 3709 | total = 0; |
| 3710 | for (i = 0; i < count; i++) |
| 3711 | total += exec_list[i].relocation_count; |
| 3712 | |
| 3713 | reloc = drm_malloc_ab(total, sizeof(*reloc)); |
| 3714 | if (reloc == NULL) { |
| 3715 | mutex_lock(&dev->struct_mutex); |
| 3716 | return -ENOMEM; |
| 3717 | } |
| 3718 | |
| 3719 | total = 0; |
| 3720 | for (i = 0; i < count; i++) { |
| 3721 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
| 3722 | |
| 3723 | user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr; |
| 3724 | |
| 3725 | if (copy_from_user(reloc+total, user_relocs, |
| 3726 | exec_list[i].relocation_count * |
| 3727 | sizeof(*reloc))) { |
| 3728 | ret = -EFAULT; |
| 3729 | mutex_lock(&dev->struct_mutex); |
| 3730 | goto err; |
| 3731 | } |
| 3732 | |
| 3733 | total += exec_list[i].relocation_count; |
| 3734 | } |
| 3735 | |
| 3736 | ret = i915_mutex_lock_interruptible(dev); |
| 3737 | if (ret) { |
| 3738 | mutex_lock(&dev->struct_mutex); |
| 3739 | goto err; |
| 3740 | } |
| 3741 | |
| 3742 | ret = i915_gem_execbuffer_reserve(dev, file, |
| 3743 | object_list, exec_list, |
| 3744 | count); |
| 3745 | if (ret) |
| 3746 | goto err; |
| 3747 | |
| 3748 | total = 0; |
| 3749 | for (i = 0; i < count; i++) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3750 | struct drm_i915_gem_object *obj = object_list[i]; |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 3751 | obj->base.pending_read_domains = 0; |
| 3752 | obj->base.pending_write_domain = 0; |
| 3753 | ret = i915_gem_execbuffer_relocate_object_slow(obj, file, |
| 3754 | &exec_list[i], |
| 3755 | reloc + total); |
| 3756 | if (ret) |
| 3757 | goto err; |
| 3758 | |
| 3759 | total += exec_list[i].relocation_count; |
| 3760 | } |
| 3761 | |
| 3762 | /* Leave the user relocations as are, this is the painfully slow path, |
| 3763 | * and we want to avoid the complication of dropping the lock whilst |
| 3764 | * having buffers reserved in the aperture and so causing spurious |
| 3765 | * ENOSPC for random operations. |
| 3766 | */ |
| 3767 | |
| 3768 | err: |
| 3769 | drm_free_large(reloc); |
| 3770 | return ret; |
| 3771 | } |
| 3772 | |
| 3773 | static int |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3774 | i915_gem_execbuffer_move_to_gpu(struct drm_device *dev, |
| 3775 | struct drm_file *file, |
| 3776 | struct intel_ring_buffer *ring, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3777 | struct drm_i915_gem_object **objects, |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3778 | int count) |
| 3779 | { |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3780 | struct change_domains cd; |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3781 | int ret, i; |
| 3782 | |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3783 | cd.invalidate_domains = 0; |
| 3784 | cd.flush_domains = 0; |
| 3785 | cd.flush_rings = 0; |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3786 | for (i = 0; i < count; i++) |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3787 | i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd); |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3788 | |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3789 | if (cd.invalidate_domains | cd.flush_domains) { |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3790 | #if WATCH_EXEC |
| 3791 | DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n", |
| 3792 | __func__, |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3793 | cd.invalidate_domains, |
| 3794 | cd.flush_domains); |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3795 | #endif |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3796 | i915_gem_flush(dev, |
Chris Wilson | 0f8c6d7 | 2010-11-01 12:38:44 +0000 | [diff] [blame] | 3797 | cd.invalidate_domains, |
| 3798 | cd.flush_domains, |
| 3799 | cd.flush_rings); |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
| 3802 | for (i = 0; i < count; i++) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3803 | struct drm_i915_gem_object *obj = objects[i]; |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3804 | /* XXX replace with semaphores */ |
| 3805 | if (obj->ring && ring != obj->ring) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3806 | ret = i915_gem_object_wait_rendering(obj, true); |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 3807 | if (ret) |
| 3808 | return ret; |
| 3809 | } |
| 3810 | } |
| 3811 | |
| 3812 | return 0; |
| 3813 | } |
| 3814 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3815 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3816 | * emitted over 20 msec ago. |
| 3817 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3818 | * Note that if we were to use the current jiffies each time around the loop, |
| 3819 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3820 | * render a frame was over 20ms. |
| 3821 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3822 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3823 | * relatively low latency when blocking on a particular request to finish. |
| 3824 | */ |
| 3825 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3826 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3827 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3828 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 3829 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3830 | unsigned long recent_enough = jiffies - msecs_to_jiffies(20); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3831 | struct drm_i915_gem_request *request; |
| 3832 | struct intel_ring_buffer *ring = NULL; |
| 3833 | u32 seqno = 0; |
| 3834 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3835 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3836 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3837 | list_for_each_entry(request, &file_priv->mm.request_list, client_list) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3838 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3839 | break; |
| 3840 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3841 | ring = request->ring; |
| 3842 | seqno = request->seqno; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3843 | } |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3844 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3845 | |
| 3846 | if (seqno == 0) |
| 3847 | return 0; |
| 3848 | |
| 3849 | ret = 0; |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3850 | if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3851 | /* And wait for the seqno passing without holding any locks and |
| 3852 | * causing extra latency for others. This is safe as the irq |
| 3853 | * generation is designed to be run atomically and so is |
| 3854 | * lockless. |
| 3855 | */ |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3856 | ring->user_irq_get(ring); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3857 | ret = wait_event_interruptible(ring->irq_queue, |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3858 | i915_seqno_passed(ring->get_seqno(ring), seqno) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3859 | || atomic_read(&dev_priv->mm.wedged)); |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3860 | ring->user_irq_put(ring); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3861 | |
| 3862 | if (ret == 0 && atomic_read(&dev_priv->mm.wedged)) |
| 3863 | ret = -EIO; |
| 3864 | } |
| 3865 | |
| 3866 | if (ret == 0) |
| 3867 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3868 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3869 | return ret; |
| 3870 | } |
| 3871 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3872 | static int |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3873 | i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec, |
| 3874 | uint64_t exec_offset) |
Chris Wilson | 83d6079 | 2009-06-06 09:45:57 +0100 | [diff] [blame] | 3875 | { |
| 3876 | uint32_t exec_start, exec_len; |
| 3877 | |
| 3878 | exec_start = (uint32_t) exec_offset + exec->batch_start_offset; |
| 3879 | exec_len = (uint32_t) exec->batch_len; |
| 3880 | |
| 3881 | if ((exec_start | exec_len) & 0x7) |
| 3882 | return -EINVAL; |
| 3883 | |
| 3884 | if (!exec_start) |
| 3885 | return -EINVAL; |
| 3886 | |
| 3887 | return 0; |
| 3888 | } |
| 3889 | |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3890 | static int |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3891 | validate_exec_list(struct drm_i915_gem_exec_object2 *exec, |
| 3892 | int count) |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3893 | { |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3894 | int i; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3895 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3896 | for (i = 0; i < count; i++) { |
| 3897 | char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr; |
Chris Wilson | d1d7883 | 2010-11-21 09:23:48 +0000 | [diff] [blame] | 3898 | int length; /* limited by fault_in_pages_readable() */ |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3899 | |
Chris Wilson | d1d7883 | 2010-11-21 09:23:48 +0000 | [diff] [blame] | 3900 | /* First check for malicious input causing overflow */ |
| 3901 | if (exec[i].relocation_count > |
| 3902 | INT_MAX / sizeof(struct drm_i915_gem_relocation_entry)) |
| 3903 | return -EINVAL; |
| 3904 | |
| 3905 | length = exec[i].relocation_count * |
| 3906 | sizeof(struct drm_i915_gem_relocation_entry); |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3907 | if (!access_ok(VERIFY_READ, ptr, length)) |
| 3908 | return -EFAULT; |
| 3909 | |
Chris Wilson | b5dc608 | 2010-10-20 20:59:57 +0100 | [diff] [blame] | 3910 | /* we may also need to update the presumed offsets */ |
| 3911 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
| 3912 | return -EFAULT; |
| 3913 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3914 | if (fault_in_pages_readable(ptr, length)) |
| 3915 | return -EFAULT; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3916 | } |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3917 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3918 | return 0; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3919 | } |
| 3920 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3921 | static int |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3922 | i915_gem_do_execbuffer(struct drm_device *dev, void *data, |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3923 | struct drm_file *file, |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3924 | struct drm_i915_gem_execbuffer2 *args, |
| 3925 | struct drm_i915_gem_exec_object2 *exec_list) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3926 | { |
| 3927 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3928 | struct drm_i915_gem_object **object_list = NULL; |
| 3929 | struct drm_i915_gem_object *batch_obj; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3930 | struct drm_clip_rect *cliprects = NULL; |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 3931 | struct drm_i915_gem_request *request = NULL; |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3932 | int ret, i, flips; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3933 | uint64_t exec_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3934 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 3935 | struct intel_ring_buffer *ring = NULL; |
| 3936 | |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 3937 | ret = i915_gem_check_is_wedged(dev); |
| 3938 | if (ret) |
| 3939 | return ret; |
| 3940 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3941 | ret = validate_exec_list(exec_list, args->buffer_count); |
| 3942 | if (ret) |
| 3943 | return ret; |
| 3944 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3945 | #if WATCH_EXEC |
| 3946 | DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", |
| 3947 | (int) args->buffers_ptr, args->buffer_count, args->batch_len); |
| 3948 | #endif |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 3949 | switch (args->flags & I915_EXEC_RING_MASK) { |
| 3950 | case I915_EXEC_DEFAULT: |
| 3951 | case I915_EXEC_RENDER: |
| 3952 | ring = &dev_priv->render_ring; |
| 3953 | break; |
| 3954 | case I915_EXEC_BSD: |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3955 | if (!HAS_BSD(dev)) { |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 3956 | DRM_ERROR("execbuf with invalid ring (BSD)\n"); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3957 | return -EINVAL; |
| 3958 | } |
| 3959 | ring = &dev_priv->bsd_ring; |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 3960 | break; |
| 3961 | case I915_EXEC_BLT: |
| 3962 | if (!HAS_BLT(dev)) { |
| 3963 | DRM_ERROR("execbuf with invalid ring (BLT)\n"); |
| 3964 | return -EINVAL; |
| 3965 | } |
| 3966 | ring = &dev_priv->blt_ring; |
| 3967 | break; |
| 3968 | default: |
| 3969 | DRM_ERROR("execbuf with unknown ring: %d\n", |
| 3970 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 3971 | return -EINVAL; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3972 | } |
| 3973 | |
Eric Anholt | 4f481ed | 2008-09-10 14:22:49 -0700 | [diff] [blame] | 3974 | if (args->buffer_count < 1) { |
| 3975 | DRM_ERROR("execbuf with %d buffers\n", args->buffer_count); |
| 3976 | return -EINVAL; |
| 3977 | } |
Eric Anholt | c8e0f93 | 2009-11-22 03:49:37 +0100 | [diff] [blame] | 3978 | object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count); |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3979 | if (object_list == NULL) { |
| 3980 | DRM_ERROR("Failed to allocate object list for %d buffers\n", |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3981 | args->buffer_count); |
| 3982 | ret = -ENOMEM; |
| 3983 | goto pre_mutex_err; |
| 3984 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3985 | |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3986 | if (args->num_cliprects != 0) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 3987 | cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects), |
| 3988 | GFP_KERNEL); |
Owain Ainsworth | a40e8d3 | 2010-02-09 14:25:55 +0000 | [diff] [blame] | 3989 | if (cliprects == NULL) { |
| 3990 | ret = -ENOMEM; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3991 | goto pre_mutex_err; |
Owain Ainsworth | a40e8d3 | 2010-02-09 14:25:55 +0000 | [diff] [blame] | 3992 | } |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3993 | |
| 3994 | ret = copy_from_user(cliprects, |
| 3995 | (struct drm_clip_rect __user *) |
| 3996 | (uintptr_t) args->cliprects_ptr, |
| 3997 | sizeof(*cliprects) * args->num_cliprects); |
| 3998 | if (ret != 0) { |
| 3999 | DRM_ERROR("copy %d cliprects failed: %d\n", |
| 4000 | args->num_cliprects, ret); |
Dan Carpenter | c877cdc | 2010-06-23 19:03:01 +0200 | [diff] [blame] | 4001 | ret = -EFAULT; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 4002 | goto pre_mutex_err; |
| 4003 | } |
| 4004 | } |
| 4005 | |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 4006 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
| 4007 | if (request == NULL) { |
| 4008 | ret = -ENOMEM; |
Chris Wilson | a198bc8 | 2009-02-06 16:55:20 +0000 | [diff] [blame] | 4009 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4010 | } |
| 4011 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 4012 | ret = i915_mutex_lock_interruptible(dev); |
| 4013 | if (ret) |
| 4014 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4015 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4016 | if (dev_priv->mm.suspended) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4017 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | a198bc8 | 2009-02-06 16:55:20 +0000 | [diff] [blame] | 4018 | ret = -EBUSY; |
| 4019 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4020 | } |
| 4021 | |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 4022 | /* Look up object handles */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4023 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4024 | struct drm_i915_gem_object *obj; |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 4025 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4026 | obj = to_intel_bo (drm_gem_object_lookup(dev, file, |
| 4027 | exec_list[i].handle)); |
| 4028 | if (obj == NULL) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4029 | DRM_ERROR("Invalid object handle %d at index %d\n", |
| 4030 | exec_list[i].handle, i); |
Chris Wilson | 0ce907f | 2010-01-23 20:26:35 +0000 | [diff] [blame] | 4031 | /* prevent error path from reading uninitialized data */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4032 | args->buffer_count = i; |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 4033 | ret = -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4034 | goto err; |
| 4035 | } |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4036 | object_list[i] = obj; |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4037 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4038 | if (obj->in_execbuffer) { |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4039 | DRM_ERROR("Object %p appears more than once in object list\n", |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4040 | obj); |
Chris Wilson | 0ce907f | 2010-01-23 20:26:35 +0000 | [diff] [blame] | 4041 | /* prevent error path from reading uninitialized data */ |
| 4042 | args->buffer_count = i + 1; |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 4043 | ret = -EINVAL; |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4044 | goto err; |
| 4045 | } |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4046 | obj->in_execbuffer = true; |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 4047 | obj->pending_fenced_gpu_access = false; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 4048 | } |
| 4049 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 4050 | /* Move the objects en-masse into the GTT, evicting if necessary. */ |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 4051 | ret = i915_gem_execbuffer_reserve(dev, file, |
| 4052 | object_list, exec_list, |
| 4053 | args->buffer_count); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 4054 | if (ret) |
| 4055 | goto err; |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 4056 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 4057 | /* The objects are in their final locations, apply the relocations. */ |
Chris Wilson | bcf50e2 | 2010-11-21 22:07:12 +0000 | [diff] [blame] | 4058 | ret = i915_gem_execbuffer_relocate(dev, file, |
| 4059 | object_list, exec_list, |
| 4060 | args->buffer_count); |
| 4061 | if (ret) { |
| 4062 | if (ret == -EFAULT) { |
| 4063 | ret = i915_gem_execbuffer_relocate_slow(dev, file, |
| 4064 | object_list, |
| 4065 | exec_list, |
| 4066 | args->buffer_count); |
| 4067 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 4068 | } |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4069 | if (ret) |
| 4070 | goto err; |
| 4071 | } |
| 4072 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4073 | /* Set the pending read domains for the batch buffer to COMMAND */ |
| 4074 | batch_obj = object_list[args->buffer_count-1]; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4075 | if (batch_obj->base.pending_write_domain) { |
Chris Wilson | 5f26a2c | 2009-06-06 09:45:58 +0100 | [diff] [blame] | 4076 | DRM_ERROR("Attempting to use self-modifying batch buffer\n"); |
| 4077 | ret = -EINVAL; |
| 4078 | goto err; |
| 4079 | } |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4080 | batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4081 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 4082 | /* Sanity check the batch buffer */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4083 | exec_offset = batch_obj->gtt_offset; |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 4084 | ret = i915_gem_check_execbuffer(args, exec_offset); |
Chris Wilson | 83d6079 | 2009-06-06 09:45:57 +0100 | [diff] [blame] | 4085 | if (ret != 0) { |
| 4086 | DRM_ERROR("execbuf with invalid offset/length\n"); |
| 4087 | goto err; |
| 4088 | } |
| 4089 | |
Chris Wilson | 13b2928 | 2010-11-01 12:22:48 +0000 | [diff] [blame] | 4090 | ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring, |
| 4091 | object_list, args->buffer_count); |
| 4092 | if (ret) |
| 4093 | goto err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4094 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4095 | #if WATCH_COHERENCY |
| 4096 | for (i = 0; i < args->buffer_count; i++) { |
| 4097 | i915_gem_object_check_coherency(object_list[i], |
| 4098 | exec_list[i].handle); |
| 4099 | } |
| 4100 | #endif |
| 4101 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4102 | #if WATCH_EXEC |
Ben Gamari | 6911a9b | 2009-04-02 11:24:54 -0700 | [diff] [blame] | 4103 | i915_gem_dump_object(batch_obj, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4104 | args->batch_len, |
| 4105 | __func__, |
| 4106 | ~0); |
| 4107 | #endif |
| 4108 | |
Chris Wilson | e59f2ba | 2010-10-07 17:28:15 +0100 | [diff] [blame] | 4109 | /* Check for any pending flips. As we only maintain a flip queue depth |
| 4110 | * of 1, we can simply insert a WAIT for the next display flip prior |
| 4111 | * to executing the batch and avoid stalling the CPU. |
| 4112 | */ |
| 4113 | flips = 0; |
| 4114 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4115 | if (object_list[i]->base.write_domain) |
| 4116 | flips |= atomic_read(&object_list[i]->pending_flip); |
Chris Wilson | e59f2ba | 2010-10-07 17:28:15 +0100 | [diff] [blame] | 4117 | } |
| 4118 | if (flips) { |
| 4119 | int plane, flip_mask; |
| 4120 | |
| 4121 | for (plane = 0; flips >> plane; plane++) { |
| 4122 | if (((flips >> plane) & 1) == 0) |
| 4123 | continue; |
| 4124 | |
| 4125 | if (plane) |
| 4126 | flip_mask = MI_WAIT_FOR_PLANE_B_FLIP; |
| 4127 | else |
| 4128 | flip_mask = MI_WAIT_FOR_PLANE_A_FLIP; |
| 4129 | |
Chris Wilson | e1f99ce | 2010-10-27 12:45:26 +0100 | [diff] [blame] | 4130 | ret = intel_ring_begin(ring, 2); |
| 4131 | if (ret) |
| 4132 | goto err; |
| 4133 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4134 | intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask); |
| 4135 | intel_ring_emit(ring, MI_NOOP); |
| 4136 | intel_ring_advance(ring); |
Chris Wilson | e59f2ba | 2010-10-07 17:28:15 +0100 | [diff] [blame] | 4137 | } |
| 4138 | } |
| 4139 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4140 | /* Exec the batchbuffer */ |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4141 | ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4142 | if (ret) { |
| 4143 | DRM_ERROR("dispatch failed %d\n", ret); |
| 4144 | goto err; |
| 4145 | } |
| 4146 | |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 4147 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4148 | struct drm_i915_gem_object *obj = object_list[i]; |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 4149 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4150 | obj->base.read_domains = obj->base.pending_read_domains; |
| 4151 | obj->base.write_domain = obj->base.pending_write_domain; |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 4152 | obj->fenced_gpu_access = obj->pending_fenced_gpu_access; |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 4153 | |
| 4154 | i915_gem_object_move_to_active(obj, ring); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4155 | if (obj->base.write_domain) { |
| 4156 | obj->dirty = 1; |
| 4157 | list_move_tail(&obj->gpu_write_list, |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 4158 | &ring->gpu_write_list); |
| 4159 | intel_mark_busy(dev, obj); |
| 4160 | } |
| 4161 | |
| 4162 | trace_i915_gem_object_change_domain(obj, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4163 | obj->base.read_domains, |
| 4164 | obj->base.write_domain); |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 4165 | } |
| 4166 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4167 | /* |
| 4168 | * Ensure that the commands in the batch buffer are |
| 4169 | * finished before the interrupt fires |
| 4170 | */ |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 4171 | i915_retire_commands(dev, ring); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4172 | |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 4173 | if (i915_add_request(dev, file, request, ring)) |
Chris Wilson | 5d97eb6 | 2010-11-10 20:40:02 +0000 | [diff] [blame] | 4174 | i915_gem_next_request_seqno(dev, ring); |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 4175 | else |
| 4176 | request = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4177 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4178 | err: |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4179 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4180 | object_list[i]->in_execbuffer = false; |
| 4181 | drm_gem_object_unreference(&object_list[i]->base); |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 4182 | } |
Julia Lawall | aad87df | 2008-12-21 16:28:47 +0100 | [diff] [blame] | 4183 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4184 | mutex_unlock(&dev->struct_mutex); |
| 4185 | |
Chris Wilson | 93533c2 | 2010-01-31 10:40:48 +0000 | [diff] [blame] | 4186 | pre_mutex_err: |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 4187 | drm_free_large(object_list); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 4188 | kfree(cliprects); |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 4189 | kfree(request); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4190 | |
| 4191 | return ret; |
| 4192 | } |
| 4193 | |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4194 | /* |
| 4195 | * Legacy execbuffer just creates an exec2 list from the original exec object |
| 4196 | * list array and passes it to the real function. |
| 4197 | */ |
| 4198 | int |
| 4199 | i915_gem_execbuffer(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4200 | struct drm_file *file) |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4201 | { |
| 4202 | struct drm_i915_gem_execbuffer *args = data; |
| 4203 | struct drm_i915_gem_execbuffer2 exec2; |
| 4204 | struct drm_i915_gem_exec_object *exec_list = NULL; |
| 4205 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 4206 | int ret, i; |
| 4207 | |
| 4208 | #if WATCH_EXEC |
| 4209 | DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", |
| 4210 | (int) args->buffers_ptr, args->buffer_count, args->batch_len); |
| 4211 | #endif |
| 4212 | |
| 4213 | if (args->buffer_count < 1) { |
| 4214 | DRM_ERROR("execbuf with %d buffers\n", args->buffer_count); |
| 4215 | return -EINVAL; |
| 4216 | } |
| 4217 | |
| 4218 | /* Copy in the exec list from userland */ |
| 4219 | exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count); |
| 4220 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 4221 | if (exec_list == NULL || exec2_list == NULL) { |
| 4222 | DRM_ERROR("Failed to allocate exec list for %d buffers\n", |
| 4223 | args->buffer_count); |
| 4224 | drm_free_large(exec_list); |
| 4225 | drm_free_large(exec2_list); |
| 4226 | return -ENOMEM; |
| 4227 | } |
| 4228 | ret = copy_from_user(exec_list, |
| 4229 | (struct drm_i915_relocation_entry __user *) |
| 4230 | (uintptr_t) args->buffers_ptr, |
| 4231 | sizeof(*exec_list) * args->buffer_count); |
| 4232 | if (ret != 0) { |
| 4233 | DRM_ERROR("copy %d exec entries failed %d\n", |
| 4234 | args->buffer_count, ret); |
| 4235 | drm_free_large(exec_list); |
| 4236 | drm_free_large(exec2_list); |
| 4237 | return -EFAULT; |
| 4238 | } |
| 4239 | |
| 4240 | for (i = 0; i < args->buffer_count; i++) { |
| 4241 | exec2_list[i].handle = exec_list[i].handle; |
| 4242 | exec2_list[i].relocation_count = exec_list[i].relocation_count; |
| 4243 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; |
| 4244 | exec2_list[i].alignment = exec_list[i].alignment; |
| 4245 | exec2_list[i].offset = exec_list[i].offset; |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4246 | if (INTEL_INFO(dev)->gen < 4) |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4247 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; |
| 4248 | else |
| 4249 | exec2_list[i].flags = 0; |
| 4250 | } |
| 4251 | |
| 4252 | exec2.buffers_ptr = args->buffers_ptr; |
| 4253 | exec2.buffer_count = args->buffer_count; |
| 4254 | exec2.batch_start_offset = args->batch_start_offset; |
| 4255 | exec2.batch_len = args->batch_len; |
| 4256 | exec2.DR1 = args->DR1; |
| 4257 | exec2.DR4 = args->DR4; |
| 4258 | exec2.num_cliprects = args->num_cliprects; |
| 4259 | exec2.cliprects_ptr = args->cliprects_ptr; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 4260 | exec2.flags = I915_EXEC_RENDER; |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4261 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4262 | ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list); |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4263 | if (!ret) { |
| 4264 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 4265 | for (i = 0; i < args->buffer_count; i++) |
| 4266 | exec_list[i].offset = exec2_list[i].offset; |
| 4267 | /* ... and back out to userspace */ |
| 4268 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 4269 | (uintptr_t) args->buffers_ptr, |
| 4270 | exec_list, |
| 4271 | sizeof(*exec_list) * args->buffer_count); |
| 4272 | if (ret) { |
| 4273 | ret = -EFAULT; |
| 4274 | DRM_ERROR("failed to copy %d exec entries " |
| 4275 | "back to user (%d)\n", |
| 4276 | args->buffer_count, ret); |
| 4277 | } |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4278 | } |
| 4279 | |
| 4280 | drm_free_large(exec_list); |
| 4281 | drm_free_large(exec2_list); |
| 4282 | return ret; |
| 4283 | } |
| 4284 | |
| 4285 | int |
| 4286 | i915_gem_execbuffer2(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4287 | struct drm_file *file) |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4288 | { |
| 4289 | struct drm_i915_gem_execbuffer2 *args = data; |
| 4290 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 4291 | int ret; |
| 4292 | |
| 4293 | #if WATCH_EXEC |
| 4294 | DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", |
| 4295 | (int) args->buffers_ptr, args->buffer_count, args->batch_len); |
| 4296 | #endif |
| 4297 | |
| 4298 | if (args->buffer_count < 1) { |
| 4299 | DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count); |
| 4300 | return -EINVAL; |
| 4301 | } |
| 4302 | |
| 4303 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 4304 | if (exec2_list == NULL) { |
| 4305 | DRM_ERROR("Failed to allocate exec list for %d buffers\n", |
| 4306 | args->buffer_count); |
| 4307 | return -ENOMEM; |
| 4308 | } |
| 4309 | ret = copy_from_user(exec2_list, |
| 4310 | (struct drm_i915_relocation_entry __user *) |
| 4311 | (uintptr_t) args->buffers_ptr, |
| 4312 | sizeof(*exec2_list) * args->buffer_count); |
| 4313 | if (ret != 0) { |
| 4314 | DRM_ERROR("copy %d exec entries failed %d\n", |
| 4315 | args->buffer_count, ret); |
| 4316 | drm_free_large(exec2_list); |
| 4317 | return -EFAULT; |
| 4318 | } |
| 4319 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4320 | ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list); |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4321 | if (!ret) { |
| 4322 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 4323 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 4324 | (uintptr_t) args->buffers_ptr, |
| 4325 | exec2_list, |
| 4326 | sizeof(*exec2_list) * args->buffer_count); |
| 4327 | if (ret) { |
| 4328 | ret = -EFAULT; |
| 4329 | DRM_ERROR("failed to copy %d exec entries " |
| 4330 | "back to user (%d)\n", |
| 4331 | args->buffer_count, ret); |
| 4332 | } |
| 4333 | } |
| 4334 | |
| 4335 | drm_free_large(exec2_list); |
| 4336 | return ret; |
| 4337 | } |
| 4338 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4339 | int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4340 | i915_gem_object_pin(struct drm_i915_gem_object *obj, |
| 4341 | uint32_t alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4342 | bool map_and_fenceable) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4343 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4344 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4345 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4346 | int ret; |
| 4347 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4348 | BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT); |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4349 | WARN_ON(i915_verify_lists(dev)); |
Chris Wilson | ac0c6b5 | 2010-05-27 13:18:18 +0100 | [diff] [blame] | 4350 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4351 | if (obj->gtt_space != NULL) { |
| 4352 | if ((alignment && obj->gtt_offset & (alignment - 1)) || |
| 4353 | (map_and_fenceable && !obj->map_and_fenceable)) { |
| 4354 | WARN(obj->pin_count, |
Chris Wilson | ae7d49d | 2010-08-04 12:37:41 +0100 | [diff] [blame] | 4355 | "bo is already pinned with incorrect alignment:" |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4356 | " offset=%x, req.alignment=%x, req.map_and_fenceable=%d," |
| 4357 | " obj->map_and_fenceable=%d\n", |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4358 | obj->gtt_offset, alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4359 | map_and_fenceable, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4360 | obj->map_and_fenceable); |
Chris Wilson | ac0c6b5 | 2010-05-27 13:18:18 +0100 | [diff] [blame] | 4361 | ret = i915_gem_object_unbind(obj); |
| 4362 | if (ret) |
| 4363 | return ret; |
| 4364 | } |
| 4365 | } |
| 4366 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4367 | if (obj->gtt_space == NULL) { |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 4368 | ret = i915_gem_object_bind_to_gtt(obj, alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4369 | map_and_fenceable); |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 4370 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4371 | return ret; |
Chris Wilson | 22c344e | 2009-02-11 14:26:45 +0000 | [diff] [blame] | 4372 | } |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4373 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4374 | if (obj->pin_count++ == 0) { |
| 4375 | i915_gem_info_add_pin(dev_priv, obj, map_and_fenceable); |
| 4376 | if (!obj->active) |
| 4377 | list_move_tail(&obj->mm_list, |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4378 | &dev_priv->mm.pinned_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4379 | } |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4380 | BUG_ON(!obj->pin_mappable && map_and_fenceable); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4381 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4382 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4383 | return 0; |
| 4384 | } |
| 4385 | |
| 4386 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4387 | i915_gem_object_unpin(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4388 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4389 | struct drm_device *dev = obj->base.dev; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4390 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4391 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4392 | WARN_ON(i915_verify_lists(dev)); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4393 | BUG_ON(obj->pin_count == 0); |
| 4394 | BUG_ON(obj->gtt_space == NULL); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4395 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4396 | if (--obj->pin_count == 0) { |
| 4397 | if (!obj->active) |
| 4398 | list_move_tail(&obj->mm_list, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4399 | &dev_priv->mm.inactive_list); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4400 | i915_gem_info_remove_pin(dev_priv, obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4401 | } |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4402 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4403 | } |
| 4404 | |
| 4405 | int |
| 4406 | i915_gem_pin_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4407 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4408 | { |
| 4409 | struct drm_i915_gem_pin *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4410 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4411 | int ret; |
| 4412 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4413 | ret = i915_mutex_lock_interruptible(dev); |
| 4414 | if (ret) |
| 4415 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4416 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4417 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4418 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4419 | ret = -ENOENT; |
| 4420 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4421 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4422 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4423 | if (obj->madv != I915_MADV_WILLNEED) { |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4424 | DRM_ERROR("Attempting to pin a purgeable buffer\n"); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4425 | ret = -EINVAL; |
| 4426 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4427 | } |
| 4428 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4429 | if (obj->pin_filp != NULL && obj->pin_filp != file) { |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4430 | DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n", |
| 4431 | args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4432 | ret = -EINVAL; |
| 4433 | goto out; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4434 | } |
| 4435 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4436 | obj->user_pin_count++; |
| 4437 | obj->pin_filp = file; |
| 4438 | if (obj->user_pin_count == 1) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4439 | ret = i915_gem_object_pin(obj, args->alignment, true); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4440 | if (ret) |
| 4441 | goto out; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | /* XXX - flush the CPU caches for pinned objects |
| 4445 | * as the X server doesn't manage domains yet |
| 4446 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4447 | i915_gem_object_flush_cpu_write_domain(obj); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4448 | args->offset = obj->gtt_offset; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4449 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4450 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4451 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4452 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4453 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | int |
| 4457 | i915_gem_unpin_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4458 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4459 | { |
| 4460 | struct drm_i915_gem_pin *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4461 | struct drm_i915_gem_object *obj; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 4462 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4463 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4464 | ret = i915_mutex_lock_interruptible(dev); |
| 4465 | if (ret) |
| 4466 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4467 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4468 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4469 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4470 | ret = -ENOENT; |
| 4471 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4472 | } |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 4473 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4474 | if (obj->pin_filp != file) { |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4475 | DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n", |
| 4476 | args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4477 | ret = -EINVAL; |
| 4478 | goto out; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4479 | } |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4480 | obj->user_pin_count--; |
| 4481 | if (obj->user_pin_count == 0) { |
| 4482 | obj->pin_filp = NULL; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4483 | i915_gem_object_unpin(obj); |
| 4484 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4485 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4486 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4487 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4488 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4489 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4490 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4491 | } |
| 4492 | |
| 4493 | int |
| 4494 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4495 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4496 | { |
| 4497 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4498 | struct drm_i915_gem_object *obj; |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 4499 | int ret; |
| 4500 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4501 | ret = i915_mutex_lock_interruptible(dev); |
| 4502 | if (ret) |
| 4503 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4504 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4505 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4506 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4507 | ret = -ENOENT; |
| 4508 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4509 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4510 | |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4511 | /* Count all active objects as busy, even if they are currently not used |
| 4512 | * by the gpu. Users of this interface expect objects to eventually |
| 4513 | * become non-busy without any further actions, therefore emit any |
| 4514 | * necessary flushes here. |
Eric Anholt | c4de0a5 | 2008-12-14 19:05:04 -0800 | [diff] [blame] | 4515 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4516 | args->busy = obj->active; |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4517 | if (args->busy) { |
| 4518 | /* Unconditionally flush objects, even when the gpu still uses this |
| 4519 | * object. Userspace calling this function indicates that it wants to |
| 4520 | * use this buffer rather sooner than later, so issuing the required |
| 4521 | * flush earlier is beneficial. |
| 4522 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4523 | if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) |
| 4524 | i915_gem_flush_ring(dev, obj->ring, |
| 4525 | 0, obj->base.write_domain); |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4526 | |
| 4527 | /* Update the active list for the hardware's current position. |
| 4528 | * Otherwise this only updates on a delayed timer or when irqs |
| 4529 | * are actually unmasked, and our working set ends up being |
| 4530 | * larger than required. |
| 4531 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4532 | i915_gem_retire_requests_ring(dev, obj->ring); |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4533 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4534 | args->busy = obj->active; |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4535 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4536 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4537 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4538 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4539 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4540 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4541 | } |
| 4542 | |
| 4543 | int |
| 4544 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4545 | struct drm_file *file_priv) |
| 4546 | { |
| 4547 | return i915_gem_ring_throttle(dev, file_priv); |
| 4548 | } |
| 4549 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4550 | int |
| 4551 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4552 | struct drm_file *file_priv) |
| 4553 | { |
| 4554 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4555 | struct drm_i915_gem_object *obj; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 4556 | int ret; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4557 | |
| 4558 | switch (args->madv) { |
| 4559 | case I915_MADV_DONTNEED: |
| 4560 | case I915_MADV_WILLNEED: |
| 4561 | break; |
| 4562 | default: |
| 4563 | return -EINVAL; |
| 4564 | } |
| 4565 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4566 | ret = i915_mutex_lock_interruptible(dev); |
| 4567 | if (ret) |
| 4568 | return ret; |
| 4569 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4570 | obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle)); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4571 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4572 | ret = -ENOENT; |
| 4573 | goto unlock; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4574 | } |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4575 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4576 | if (obj->pin_count) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4577 | ret = -EINVAL; |
| 4578 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4579 | } |
| 4580 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4581 | if (obj->madv != __I915_MADV_PURGED) |
| 4582 | obj->madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4583 | |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4584 | /* if the object is no longer bound, discard its backing storage */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4585 | if (i915_gem_object_is_purgeable(obj) && |
| 4586 | obj->gtt_space == NULL) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4587 | i915_gem_object_truncate(obj); |
| 4588 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4589 | args->retained = obj->madv != __I915_MADV_PURGED; |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4590 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4591 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4592 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4593 | unlock: |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4594 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4595 | return ret; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4596 | } |
| 4597 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4598 | struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev, |
| 4599 | size_t size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4600 | { |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4601 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4602 | struct drm_i915_gem_object *obj; |
| 4603 | |
| 4604 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
| 4605 | if (obj == NULL) |
| 4606 | return NULL; |
| 4607 | |
| 4608 | if (drm_gem_object_init(dev, &obj->base, size) != 0) { |
| 4609 | kfree(obj); |
| 4610 | return NULL; |
| 4611 | } |
| 4612 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4613 | i915_gem_info_add_obj(dev_priv, size); |
| 4614 | |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4615 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
| 4616 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 4617 | |
| 4618 | obj->agp_type = AGP_USER_MEMORY; |
Daniel Vetter | 62b8b21 | 2010-04-09 19:05:08 +0000 | [diff] [blame] | 4619 | obj->base.driver_private = NULL; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4620 | obj->fence_reg = I915_FENCE_REG_NONE; |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4621 | INIT_LIST_HEAD(&obj->mm_list); |
Daniel Vetter | 93a37f2 | 2010-11-05 20:24:53 +0100 | [diff] [blame] | 4622 | INIT_LIST_HEAD(&obj->gtt_list); |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4623 | INIT_LIST_HEAD(&obj->ring_list); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4624 | INIT_LIST_HEAD(&obj->gpu_write_list); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4625 | obj->madv = I915_MADV_WILLNEED; |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 4626 | /* Avoid an unnecessary call to unbind on the first bind. */ |
| 4627 | obj->map_and_fenceable = true; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4628 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4629 | return obj; |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4630 | } |
| 4631 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4632 | int i915_gem_init_object(struct drm_gem_object *obj) |
| 4633 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4634 | BUG(); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4635 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4636 | return 0; |
| 4637 | } |
| 4638 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4639 | static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4640 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4641 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4642 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4643 | int ret; |
| 4644 | |
| 4645 | ret = i915_gem_object_unbind(obj); |
| 4646 | if (ret == -ERESTARTSYS) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4647 | list_move(&obj->mm_list, |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4648 | &dev_priv->mm.deferred_free_list); |
| 4649 | return; |
| 4650 | } |
| 4651 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4652 | if (obj->base.map_list.map) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4653 | i915_gem_free_mmap_offset(obj); |
| 4654 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4655 | drm_gem_object_release(&obj->base); |
| 4656 | i915_gem_info_remove_obj(dev_priv, obj->base.size); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4657 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4658 | kfree(obj->page_cpu_valid); |
| 4659 | kfree(obj->bit_17); |
| 4660 | kfree(obj); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4661 | } |
| 4662 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4663 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4664 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4665 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
| 4666 | struct drm_device *dev = obj->base.dev; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4667 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 4668 | trace_i915_gem_object_destroy(obj); |
| 4669 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4670 | while (obj->pin_count > 0) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4671 | i915_gem_object_unpin(obj); |
| 4672 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4673 | if (obj->phys_obj) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4674 | i915_gem_detach_phys_object(dev, obj); |
| 4675 | |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4676 | i915_gem_free_object_tail(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4677 | } |
| 4678 | |
Jesse Barnes | 5669fca | 2009-02-17 15:13:31 -0800 | [diff] [blame] | 4679 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4680 | i915_gem_idle(struct drm_device *dev) |
| 4681 | { |
| 4682 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4683 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4684 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4685 | mutex_lock(&dev->struct_mutex); |
| 4686 | |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 4687 | if (dev_priv->mm.suspended) { |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4688 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4689 | return 0; |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4690 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4691 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4692 | ret = i915_gpu_idle(dev); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4693 | if (ret) { |
| 4694 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4695 | return ret; |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4696 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4697 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4698 | /* Under UMS, be paranoid and evict. */ |
| 4699 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) { |
Chris Wilson | 5eac3ab | 2010-10-31 08:49:47 +0000 | [diff] [blame] | 4700 | ret = i915_gem_evict_inactive(dev, false); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4701 | if (ret) { |
| 4702 | mutex_unlock(&dev->struct_mutex); |
| 4703 | return ret; |
| 4704 | } |
| 4705 | } |
| 4706 | |
Chris Wilson | 312817a | 2010-11-22 11:50:11 +0000 | [diff] [blame] | 4707 | i915_gem_reset_fences(dev); |
| 4708 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4709 | /* Hack! Don't let anybody do execbuf while we don't control the chip. |
| 4710 | * We need to replace this with a semaphore, or something. |
| 4711 | * And not confound mm.suspended! |
| 4712 | */ |
| 4713 | dev_priv->mm.suspended = 1; |
Daniel Vetter | bc0c7f1 | 2010-08-20 18:18:48 +0200 | [diff] [blame] | 4714 | del_timer_sync(&dev_priv->hangcheck_timer); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4715 | |
| 4716 | i915_kernel_lost_context(dev); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4717 | i915_gem_cleanup_ringbuffer(dev); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4718 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4719 | mutex_unlock(&dev->struct_mutex); |
| 4720 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4721 | /* Cancel the retire work handler, which should be idle now. */ |
| 4722 | cancel_delayed_work_sync(&dev_priv->mm.retire_work); |
| 4723 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4724 | return 0; |
| 4725 | } |
| 4726 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4727 | int |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4728 | i915_gem_init_ringbuffer(struct drm_device *dev) |
| 4729 | { |
| 4730 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4731 | int ret; |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4732 | |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 4733 | ret = intel_init_render_ring_buffer(dev); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4734 | if (ret) |
Chris Wilson | b6913e4 | 2010-11-12 10:46:37 +0000 | [diff] [blame] | 4735 | return ret; |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4736 | |
| 4737 | if (HAS_BSD(dev)) { |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 4738 | ret = intel_init_bsd_ring_buffer(dev); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4739 | if (ret) |
| 4740 | goto cleanup_render_ring; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4741 | } |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4742 | |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4743 | if (HAS_BLT(dev)) { |
| 4744 | ret = intel_init_blt_ring_buffer(dev); |
| 4745 | if (ret) |
| 4746 | goto cleanup_bsd_ring; |
| 4747 | } |
| 4748 | |
Chris Wilson | 6f392d5 | 2010-08-07 11:01:22 +0100 | [diff] [blame] | 4749 | dev_priv->next_seqno = 1; |
| 4750 | |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4751 | return 0; |
| 4752 | |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4753 | cleanup_bsd_ring: |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4754 | intel_cleanup_ring_buffer(&dev_priv->bsd_ring); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4755 | cleanup_render_ring: |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4756 | intel_cleanup_ring_buffer(&dev_priv->render_ring); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4757 | return ret; |
| 4758 | } |
| 4759 | |
| 4760 | void |
| 4761 | i915_gem_cleanup_ringbuffer(struct drm_device *dev) |
| 4762 | { |
| 4763 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4764 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4765 | intel_cleanup_ring_buffer(&dev_priv->render_ring); |
| 4766 | intel_cleanup_ring_buffer(&dev_priv->bsd_ring); |
| 4767 | intel_cleanup_ring_buffer(&dev_priv->blt_ring); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4771 | i915_gem_entervt_ioctl(struct drm_device *dev, void *data, |
| 4772 | struct drm_file *file_priv) |
| 4773 | { |
| 4774 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4775 | int ret; |
| 4776 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4777 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4778 | return 0; |
| 4779 | |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 4780 | if (atomic_read(&dev_priv->mm.wedged)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4781 | DRM_ERROR("Reenabling wedged hardware, good luck\n"); |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 4782 | atomic_set(&dev_priv->mm.wedged, 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4783 | } |
| 4784 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4785 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4786 | dev_priv->mm.suspended = 0; |
| 4787 | |
| 4788 | ret = i915_gem_init_ringbuffer(dev); |
Wu Fengguang | d816f6a | 2009-04-18 10:43:32 +0800 | [diff] [blame] | 4789 | if (ret != 0) { |
| 4790 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4791 | return ret; |
Wu Fengguang | d816f6a | 2009-04-18 10:43:32 +0800 | [diff] [blame] | 4792 | } |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4793 | |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4794 | BUG_ON(!list_empty(&dev_priv->mm.active_list)); |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 4795 | BUG_ON(!list_empty(&dev_priv->render_ring.active_list)); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 4796 | BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list)); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4797 | BUG_ON(!list_empty(&dev_priv->blt_ring.active_list)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4798 | BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); |
| 4799 | BUG_ON(!list_empty(&dev_priv->mm.inactive_list)); |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 4800 | BUG_ON(!list_empty(&dev_priv->render_ring.request_list)); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 4801 | BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list)); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4802 | BUG_ON(!list_empty(&dev_priv->blt_ring.request_list)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4803 | mutex_unlock(&dev->struct_mutex); |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4804 | |
Chris Wilson | 5f35308 | 2010-06-07 14:03:03 +0100 | [diff] [blame] | 4805 | ret = drm_irq_install(dev); |
| 4806 | if (ret) |
| 4807 | goto cleanup_ringbuffer; |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4808 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4809 | return 0; |
Chris Wilson | 5f35308 | 2010-06-07 14:03:03 +0100 | [diff] [blame] | 4810 | |
| 4811 | cleanup_ringbuffer: |
| 4812 | mutex_lock(&dev->struct_mutex); |
| 4813 | i915_gem_cleanup_ringbuffer(dev); |
| 4814 | dev_priv->mm.suspended = 1; |
| 4815 | mutex_unlock(&dev->struct_mutex); |
| 4816 | |
| 4817 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | int |
| 4821 | i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, |
| 4822 | struct drm_file *file_priv) |
| 4823 | { |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4824 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4825 | return 0; |
| 4826 | |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4827 | drm_irq_uninstall(dev); |
Linus Torvalds | e6890f6 | 2009-09-08 17:09:24 -0700 | [diff] [blame] | 4828 | return i915_gem_idle(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | void |
| 4832 | i915_gem_lastclose(struct drm_device *dev) |
| 4833 | { |
| 4834 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4835 | |
Eric Anholt | e806b49 | 2009-01-22 09:56:58 -0800 | [diff] [blame] | 4836 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4837 | return; |
| 4838 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4839 | ret = i915_gem_idle(dev); |
| 4840 | if (ret) |
| 4841 | DRM_ERROR("failed to idle hardware: %d\n", ret); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4842 | } |
| 4843 | |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 4844 | static void |
| 4845 | init_ring_lists(struct intel_ring_buffer *ring) |
| 4846 | { |
| 4847 | INIT_LIST_HEAD(&ring->active_list); |
| 4848 | INIT_LIST_HEAD(&ring->request_list); |
| 4849 | INIT_LIST_HEAD(&ring->gpu_write_list); |
| 4850 | } |
| 4851 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4852 | void |
| 4853 | i915_gem_load(struct drm_device *dev) |
| 4854 | { |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4855 | int i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4856 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4857 | |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4858 | INIT_LIST_HEAD(&dev_priv->mm.active_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4859 | INIT_LIST_HEAD(&dev_priv->mm.flushing_list); |
| 4860 | INIT_LIST_HEAD(&dev_priv->mm.inactive_list); |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4861 | INIT_LIST_HEAD(&dev_priv->mm.pinned_list); |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 4862 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4863 | INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list); |
Daniel Vetter | 93a37f2 | 2010-11-05 20:24:53 +0100 | [diff] [blame] | 4864 | INIT_LIST_HEAD(&dev_priv->mm.gtt_list); |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 4865 | init_ring_lists(&dev_priv->render_ring); |
| 4866 | init_ring_lists(&dev_priv->bsd_ring); |
| 4867 | init_ring_lists(&dev_priv->blt_ring); |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 4868 | for (i = 0; i < 16; i++) |
| 4869 | INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4870 | INIT_DELAYED_WORK(&dev_priv->mm.retire_work, |
| 4871 | i915_gem_retire_work_handler); |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 4872 | init_completion(&dev_priv->error_completion); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4873 | |
Dave Airlie | 9440012 | 2010-07-20 13:15:31 +1000 | [diff] [blame] | 4874 | /* On GEN3 we really need to make sure the ARB C3 LP bit is set */ |
| 4875 | if (IS_GEN3(dev)) { |
| 4876 | u32 tmp = I915_READ(MI_ARB_STATE); |
| 4877 | if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) { |
| 4878 | /* arb state is a masked write, so set bit + bit in mask */ |
| 4879 | tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT); |
| 4880 | I915_WRITE(MI_ARB_STATE, tmp); |
| 4881 | } |
| 4882 | } |
| 4883 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4884 | /* Old X drivers will take 0-2 for front, back, depth buffers */ |
Eric Anholt | b397c83 | 2010-01-26 09:43:10 -0800 | [diff] [blame] | 4885 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4886 | dev_priv->fence_reg_start = 3; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4887 | |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4888 | 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] | 4889 | dev_priv->num_fence_regs = 16; |
| 4890 | else |
| 4891 | dev_priv->num_fence_regs = 8; |
| 4892 | |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4893 | /* Initialize fence registers to zero */ |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4894 | switch (INTEL_INFO(dev)->gen) { |
| 4895 | case 6: |
| 4896 | for (i = 0; i < 16; i++) |
| 4897 | I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0); |
| 4898 | break; |
| 4899 | case 5: |
| 4900 | case 4: |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4901 | for (i = 0; i < 16; i++) |
| 4902 | I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0); |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4903 | break; |
| 4904 | case 3: |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4905 | if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) |
| 4906 | for (i = 0; i < 8; i++) |
| 4907 | I915_WRITE(FENCE_REG_945_8 + (i * 4), 0); |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4908 | case 2: |
| 4909 | for (i = 0; i < 8; i++) |
| 4910 | I915_WRITE(FENCE_REG_830_0 + (i * 4), 0); |
| 4911 | break; |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4912 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4913 | i915_gem_detect_bit_6_swizzle(dev); |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 4914 | init_waitqueue_head(&dev_priv->pending_flip_queue); |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 4915 | |
| 4916 | dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink; |
| 4917 | dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS; |
| 4918 | register_shrinker(&dev_priv->mm.inactive_shrinker); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4919 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4920 | |
| 4921 | /* |
| 4922 | * Create a physically contiguous memory object for this object |
| 4923 | * e.g. for cursor + overlay regs |
| 4924 | */ |
Chris Wilson | 995b676 | 2010-08-20 13:23:26 +0100 | [diff] [blame] | 4925 | static int i915_gem_init_phys_object(struct drm_device *dev, |
| 4926 | int id, int size, int align) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4927 | { |
| 4928 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4929 | struct drm_i915_gem_phys_object *phys_obj; |
| 4930 | int ret; |
| 4931 | |
| 4932 | if (dev_priv->mm.phys_objs[id - 1] || !size) |
| 4933 | return 0; |
| 4934 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 4935 | phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4936 | if (!phys_obj) |
| 4937 | return -ENOMEM; |
| 4938 | |
| 4939 | phys_obj->id = id; |
| 4940 | |
Chris Wilson | 6eeefaf | 2010-08-07 11:01:39 +0100 | [diff] [blame] | 4941 | phys_obj->handle = drm_pci_alloc(dev, size, align); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4942 | if (!phys_obj->handle) { |
| 4943 | ret = -ENOMEM; |
| 4944 | goto kfree_obj; |
| 4945 | } |
| 4946 | #ifdef CONFIG_X86 |
| 4947 | set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); |
| 4948 | #endif |
| 4949 | |
| 4950 | dev_priv->mm.phys_objs[id - 1] = phys_obj; |
| 4951 | |
| 4952 | return 0; |
| 4953 | kfree_obj: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 4954 | kfree(phys_obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4955 | return ret; |
| 4956 | } |
| 4957 | |
Chris Wilson | 995b676 | 2010-08-20 13:23:26 +0100 | [diff] [blame] | 4958 | 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] | 4959 | { |
| 4960 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4961 | struct drm_i915_gem_phys_object *phys_obj; |
| 4962 | |
| 4963 | if (!dev_priv->mm.phys_objs[id - 1]) |
| 4964 | return; |
| 4965 | |
| 4966 | phys_obj = dev_priv->mm.phys_objs[id - 1]; |
| 4967 | if (phys_obj->cur_obj) { |
| 4968 | i915_gem_detach_phys_object(dev, phys_obj->cur_obj); |
| 4969 | } |
| 4970 | |
| 4971 | #ifdef CONFIG_X86 |
| 4972 | set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); |
| 4973 | #endif |
| 4974 | drm_pci_free(dev, phys_obj->handle); |
| 4975 | kfree(phys_obj); |
| 4976 | dev_priv->mm.phys_objs[id - 1] = NULL; |
| 4977 | } |
| 4978 | |
| 4979 | void i915_gem_free_all_phys_object(struct drm_device *dev) |
| 4980 | { |
| 4981 | int i; |
| 4982 | |
Dave Airlie | 260883c | 2009-01-22 17:58:49 +1000 | [diff] [blame] | 4983 | 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] | 4984 | i915_gem_free_phys_object(dev, i); |
| 4985 | } |
| 4986 | |
| 4987 | void i915_gem_detach_phys_object(struct drm_device *dev, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4988 | struct drm_i915_gem_object *obj) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4989 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4990 | struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 4991 | char *vaddr; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4992 | int i; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4993 | int page_count; |
| 4994 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4995 | if (!obj->phys_obj) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4996 | return; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4997 | vaddr = obj->phys_obj->handle->vaddr; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4998 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4999 | page_count = obj->base.size / PAGE_SIZE; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5000 | for (i = 0; i < page_count; i++) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5001 | struct page *page = read_cache_page_gfp(mapping, i, |
| 5002 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 5003 | if (!IS_ERR(page)) { |
| 5004 | char *dst = kmap_atomic(page); |
| 5005 | memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE); |
| 5006 | kunmap_atomic(dst); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5007 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5008 | drm_clflush_pages(&page, 1); |
| 5009 | |
| 5010 | set_page_dirty(page); |
| 5011 | mark_page_accessed(page); |
| 5012 | page_cache_release(page); |
| 5013 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5014 | } |
Daniel Vetter | 40ce657 | 2010-11-05 18:12:18 +0100 | [diff] [blame] | 5015 | intel_gtt_chipset_flush(); |
Chris Wilson | d78b47b | 2009-06-17 21:52:49 +0100 | [diff] [blame] | 5016 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5017 | obj->phys_obj->cur_obj = NULL; |
| 5018 | obj->phys_obj = NULL; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5019 | } |
| 5020 | |
| 5021 | int |
| 5022 | i915_gem_attach_phys_object(struct drm_device *dev, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5023 | struct drm_i915_gem_object *obj, |
Chris Wilson | 6eeefaf | 2010-08-07 11:01:39 +0100 | [diff] [blame] | 5024 | int id, |
| 5025 | int align) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5026 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5027 | struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5028 | drm_i915_private_t *dev_priv = dev->dev_private; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5029 | int ret = 0; |
| 5030 | int page_count; |
| 5031 | int i; |
| 5032 | |
| 5033 | if (id > I915_MAX_PHYS_OBJECT) |
| 5034 | return -EINVAL; |
| 5035 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5036 | if (obj->phys_obj) { |
| 5037 | if (obj->phys_obj->id == id) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5038 | return 0; |
| 5039 | i915_gem_detach_phys_object(dev, obj); |
| 5040 | } |
| 5041 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5042 | /* create a new object */ |
| 5043 | if (!dev_priv->mm.phys_objs[id - 1]) { |
| 5044 | ret = i915_gem_init_phys_object(dev, id, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5045 | obj->base.size, align); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5046 | if (ret) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5047 | DRM_ERROR("failed to init phys object %d size: %zu\n", |
| 5048 | id, obj->base.size); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5049 | return ret; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5050 | } |
| 5051 | } |
| 5052 | |
| 5053 | /* bind to the object */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5054 | obj->phys_obj = dev_priv->mm.phys_objs[id - 1]; |
| 5055 | obj->phys_obj->cur_obj = obj; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5056 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5057 | page_count = obj->base.size / PAGE_SIZE; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5058 | |
| 5059 | for (i = 0; i < page_count; i++) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5060 | struct page *page; |
| 5061 | char *dst, *src; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5062 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5063 | page = read_cache_page_gfp(mapping, i, |
| 5064 | GFP_HIGHUSER | __GFP_RECLAIMABLE); |
| 5065 | if (IS_ERR(page)) |
| 5066 | return PTR_ERR(page); |
| 5067 | |
Chris Wilson | ff75b9b | 2010-10-30 22:52:31 +0100 | [diff] [blame] | 5068 | src = kmap_atomic(page); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5069 | dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5070 | memcpy(dst, src, PAGE_SIZE); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 5071 | kunmap_atomic(src); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 5072 | |
| 5073 | mark_page_accessed(page); |
| 5074 | page_cache_release(page); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5075 | } |
| 5076 | |
| 5077 | return 0; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5078 | } |
| 5079 | |
| 5080 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5081 | i915_gem_phys_pwrite(struct drm_device *dev, |
| 5082 | struct drm_i915_gem_object *obj, |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5083 | struct drm_i915_gem_pwrite *args, |
| 5084 | struct drm_file *file_priv) |
| 5085 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 5086 | void *vaddr = obj->phys_obj->handle->vaddr + args->offset; |
Chris Wilson | b47b30c | 2010-11-08 01:12:29 +0000 | [diff] [blame] | 5087 | char __user *user_data = (char __user *) (uintptr_t) args->data_ptr; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5088 | |
Chris Wilson | b47b30c | 2010-11-08 01:12:29 +0000 | [diff] [blame] | 5089 | if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) { |
| 5090 | unsigned long unwritten; |
| 5091 | |
| 5092 | /* The physical object once assigned is fixed for the lifetime |
| 5093 | * of the obj, so we can safely drop the lock and continue |
| 5094 | * to access vaddr. |
| 5095 | */ |
| 5096 | mutex_unlock(&dev->struct_mutex); |
| 5097 | unwritten = copy_from_user(vaddr, user_data, args->size); |
| 5098 | mutex_lock(&dev->struct_mutex); |
| 5099 | if (unwritten) |
| 5100 | return -EFAULT; |
| 5101 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5102 | |
Daniel Vetter | 40ce657 | 2010-11-05 18:12:18 +0100 | [diff] [blame] | 5103 | intel_gtt_chipset_flush(); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 5104 | return 0; |
| 5105 | } |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5106 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5107 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5108 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5109 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5110 | |
| 5111 | /* Clean up our request list when the client is going away, so that |
| 5112 | * later retire_requests won't dereference our soon-to-be-gone |
| 5113 | * file_priv. |
| 5114 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5115 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 5116 | while (!list_empty(&file_priv->mm.request_list)) { |
| 5117 | struct drm_i915_gem_request *request; |
| 5118 | |
| 5119 | request = list_first_entry(&file_priv->mm.request_list, |
| 5120 | struct drm_i915_gem_request, |
| 5121 | client_list); |
| 5122 | list_del(&request->client_list); |
| 5123 | request->file_priv = NULL; |
| 5124 | } |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 5125 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 5126 | } |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5127 | |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5128 | static int |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5129 | i915_gpu_is_active(struct drm_device *dev) |
| 5130 | { |
| 5131 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 5132 | int lists_empty; |
| 5133 | |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5134 | lists_empty = list_empty(&dev_priv->mm.flushing_list) && |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5135 | list_empty(&dev_priv->mm.active_list); |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5136 | |
| 5137 | return !lists_empty; |
| 5138 | } |
| 5139 | |
| 5140 | static int |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5141 | i915_gem_inactive_shrink(struct shrinker *shrinker, |
| 5142 | int nr_to_scan, |
| 5143 | gfp_t gfp_mask) |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5144 | { |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5145 | struct drm_i915_private *dev_priv = |
| 5146 | container_of(shrinker, |
| 5147 | struct drm_i915_private, |
| 5148 | mm.inactive_shrinker); |
| 5149 | struct drm_device *dev = dev_priv->dev; |
| 5150 | struct drm_i915_gem_object *obj, *next; |
| 5151 | int cnt; |
| 5152 | |
| 5153 | if (!mutex_trylock(&dev->struct_mutex)) |
Chris Wilson | bbe2e11 | 2010-10-28 22:35:07 +0100 | [diff] [blame] | 5154 | return 0; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5155 | |
| 5156 | /* "fast-path" to count number of available objects */ |
| 5157 | if (nr_to_scan == 0) { |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5158 | cnt = 0; |
| 5159 | list_for_each_entry(obj, |
| 5160 | &dev_priv->mm.inactive_list, |
| 5161 | mm_list) |
| 5162 | cnt++; |
| 5163 | mutex_unlock(&dev->struct_mutex); |
| 5164 | return cnt / 100 * sysctl_vfs_cache_pressure; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5165 | } |
| 5166 | |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5167 | rescan: |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5168 | /* first scan for clean buffers */ |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5169 | i915_gem_retire_requests(dev); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5170 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5171 | list_for_each_entry_safe(obj, next, |
| 5172 | &dev_priv->mm.inactive_list, |
| 5173 | mm_list) { |
| 5174 | if (i915_gem_object_is_purgeable(obj)) { |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame^] | 5175 | if (i915_gem_object_unbind(obj) == 0 && |
| 5176 | --nr_to_scan == 0) |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5177 | break; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5178 | } |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | /* second pass, evict/count anything still on the inactive list */ |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5182 | cnt = 0; |
| 5183 | list_for_each_entry_safe(obj, next, |
| 5184 | &dev_priv->mm.inactive_list, |
| 5185 | mm_list) { |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame^] | 5186 | if (nr_to_scan && |
| 5187 | i915_gem_object_unbind(obj) == 0) |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5188 | nr_to_scan--; |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame^] | 5189 | else |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5190 | cnt++; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5191 | } |
| 5192 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5193 | if (nr_to_scan && i915_gpu_is_active(dev)) { |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5194 | /* |
| 5195 | * We are desperate for pages, so as a last resort, wait |
| 5196 | * for the GPU to finish and discard whatever we can. |
| 5197 | * This has a dramatic impact to reduce the number of |
| 5198 | * OOM-killer events whilst running the GPU aggressively. |
| 5199 | */ |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5200 | if (i915_gpu_idle(dev) == 0) |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5201 | goto rescan; |
| 5202 | } |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5203 | mutex_unlock(&dev->struct_mutex); |
| 5204 | return cnt / 100 * sysctl_vfs_cache_pressure; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5205 | } |