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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 28 | #include <drm/drmP.h> |
| 29 | #include <drm/i915_drm.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 30 | #include "i915_drv.h" |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 31 | #include "i915_trace.h" |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 32 | #include "intel_drv.h" |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 33 | #include <linux/shmem_fs.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> |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 37 | #include <linux/dma-buf.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 38 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 39 | static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj); |
| 40 | static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 41 | static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj, |
| 42 | unsigned alignment, |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 43 | bool map_and_fenceable, |
| 44 | bool nonblocking); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 45 | static int i915_gem_phys_pwrite(struct drm_device *dev, |
| 46 | struct drm_i915_gem_object *obj, |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 47 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 48 | struct drm_file *file); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 49 | |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 50 | static void i915_gem_write_fence(struct drm_device *dev, int reg, |
| 51 | struct drm_i915_gem_object *obj); |
| 52 | static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj, |
| 53 | struct drm_i915_fence_reg *fence, |
| 54 | bool enable); |
| 55 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 56 | static int i915_gem_inactive_shrink(struct shrinker *shrinker, |
Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 57 | struct shrink_control *sc); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 58 | static long i915_gem_purge(struct drm_i915_private *dev_priv, long target); |
| 59 | static void i915_gem_shrink_all(struct drm_i915_private *dev_priv); |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 60 | static void i915_gem_object_truncate(struct drm_i915_gem_object *obj); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 61 | |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 62 | static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj) |
| 63 | { |
| 64 | if (obj->tiling_mode) |
| 65 | i915_gem_release_mmap(obj); |
| 66 | |
| 67 | /* As we do not have an associated fence register, we will force |
| 68 | * a tiling change if we ever need to acquire one. |
| 69 | */ |
Chris Wilson | 5d82e3e | 2012-04-21 16:23:23 +0100 | [diff] [blame] | 70 | obj->fence_dirty = false; |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 71 | obj->fence_reg = I915_FENCE_REG_NONE; |
| 72 | } |
| 73 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 74 | /* some bookkeeping */ |
| 75 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, |
| 76 | size_t size) |
| 77 | { |
| 78 | dev_priv->mm.object_count++; |
| 79 | dev_priv->mm.object_memory += size; |
| 80 | } |
| 81 | |
| 82 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, |
| 83 | size_t size) |
| 84 | { |
| 85 | dev_priv->mm.object_count--; |
| 86 | dev_priv->mm.object_memory -= size; |
| 87 | } |
| 88 | |
Chris Wilson | 21dd373 | 2011-01-26 15:55:56 +0000 | [diff] [blame] | 89 | static int |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 90 | i915_gem_wait_for_error(struct i915_gpu_error *error) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 91 | { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 92 | int ret; |
| 93 | |
Daniel Vetter | 7abb690 | 2013-05-24 21:29:32 +0200 | [diff] [blame] | 94 | #define EXIT_COND (!i915_reset_in_progress(error) || \ |
| 95 | i915_terminally_wedged(error)) |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 96 | if (EXIT_COND) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 97 | return 0; |
| 98 | |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 99 | /* |
| 100 | * Only wait 10 seconds for the gpu reset to complete to avoid hanging |
| 101 | * userspace. If it takes that long something really bad is going on and |
| 102 | * we should simply try to bail out and fail as gracefully as possible. |
| 103 | */ |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 104 | ret = wait_event_interruptible_timeout(error->reset_queue, |
| 105 | EXIT_COND, |
| 106 | 10*HZ); |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 107 | if (ret == 0) { |
| 108 | DRM_ERROR("Timed out waiting for the gpu reset to complete\n"); |
| 109 | return -EIO; |
| 110 | } else if (ret < 0) { |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 111 | return ret; |
Daniel Vetter | 0a6759c | 2012-07-04 22:18:41 +0200 | [diff] [blame] | 112 | } |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 113 | #undef EXIT_COND |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 114 | |
Chris Wilson | 21dd373 | 2011-01-26 15:55:56 +0000 | [diff] [blame] | 115 | return 0; |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 116 | } |
| 117 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 118 | int i915_mutex_lock_interruptible(struct drm_device *dev) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 119 | { |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 120 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 121 | int ret; |
| 122 | |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 123 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 124 | if (ret) |
| 125 | return ret; |
| 126 | |
| 127 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 128 | if (ret) |
| 129 | return ret; |
| 130 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 131 | WARN_ON(i915_verify_lists(dev)); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 132 | return 0; |
| 133 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 134 | |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 135 | static inline bool |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 136 | i915_gem_object_is_inactive(struct drm_i915_gem_object *obj) |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 137 | { |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 138 | return i915_gem_obj_ggtt_bound(obj) && !obj->active; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 139 | } |
| 140 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 141 | int |
| 142 | i915_gem_init_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 143 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 144 | { |
Ben Widawsky | 93d1879 | 2013-01-17 12:45:17 -0800 | [diff] [blame] | 145 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 146 | struct drm_i915_gem_init *args = data; |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 147 | |
Daniel Vetter | 7bb6fb8 | 2012-04-24 08:22:52 +0200 | [diff] [blame] | 148 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 149 | return -ENODEV; |
| 150 | |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 151 | if (args->gtt_start >= args->gtt_end || |
| 152 | (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1)) |
| 153 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 154 | |
Daniel Vetter | f534bc0 | 2012-03-26 22:37:04 +0200 | [diff] [blame] | 155 | /* GEM with user mode setting was never supported on ilk and later. */ |
| 156 | if (INTEL_INFO(dev)->gen >= 5) |
| 157 | return -ENODEV; |
| 158 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 159 | mutex_lock(&dev->struct_mutex); |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 160 | i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end, |
| 161 | args->gtt_end); |
Ben Widawsky | 93d1879 | 2013-01-17 12:45:17 -0800 | [diff] [blame] | 162 | dev_priv->gtt.mappable_end = args->gtt_end; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 163 | mutex_unlock(&dev->struct_mutex); |
| 164 | |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 165 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 168 | int |
| 169 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 170 | struct drm_file *file) |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 171 | { |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 172 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 173 | struct drm_i915_gem_get_aperture *args = data; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 174 | struct drm_i915_gem_object *obj; |
| 175 | size_t pinned; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 176 | |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 177 | pinned = 0; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 178 | mutex_lock(&dev->struct_mutex); |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 179 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) |
Chris Wilson | 1b50247 | 2012-04-24 15:47:30 +0100 | [diff] [blame] | 180 | if (obj->pin_count) |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 181 | pinned += i915_gem_obj_ggtt_size(obj); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 182 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 183 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 184 | args->aper_size = dev_priv->gtt.base.total; |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 185 | args->aper_available_size = args->aper_size - pinned; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 186 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 190 | void *i915_gem_object_alloc(struct drm_device *dev) |
| 191 | { |
| 192 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 193 | return kmem_cache_alloc(dev_priv->slab, GFP_KERNEL | __GFP_ZERO); |
| 194 | } |
| 195 | |
| 196 | void i915_gem_object_free(struct drm_i915_gem_object *obj) |
| 197 | { |
| 198 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
| 199 | kmem_cache_free(dev_priv->slab, obj); |
| 200 | } |
| 201 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 202 | static int |
| 203 | i915_gem_create(struct drm_file *file, |
| 204 | struct drm_device *dev, |
| 205 | uint64_t size, |
| 206 | uint32_t *handle_p) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 207 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 208 | struct drm_i915_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 209 | int ret; |
| 210 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 211 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 212 | size = roundup(size, PAGE_SIZE); |
Chris Wilson | 8ffc024 | 2011-09-14 14:14:28 +0200 | [diff] [blame] | 213 | if (size == 0) |
| 214 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 215 | |
| 216 | /* Allocate the new object */ |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 217 | obj = i915_gem_alloc_object(dev, size); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 218 | if (obj == NULL) |
| 219 | return -ENOMEM; |
| 220 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 221 | ret = drm_gem_handle_create(file, &obj->base, &handle); |
Chris Wilson | 1dfd975 | 2010-09-06 14:44:14 +0100 | [diff] [blame] | 222 | if (ret) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 223 | drm_gem_object_release(&obj->base); |
| 224 | i915_gem_info_remove_obj(dev->dev_private, obj->base.size); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 225 | i915_gem_object_free(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 226 | return ret; |
Chris Wilson | 1dfd975 | 2010-09-06 14:44:14 +0100 | [diff] [blame] | 227 | } |
| 228 | |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 229 | /* drop reference from allocate - handle holds it now */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 230 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 231 | trace_i915_gem_object_create(obj); |
| 232 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 233 | *handle_p = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 237 | int |
| 238 | i915_gem_dumb_create(struct drm_file *file, |
| 239 | struct drm_device *dev, |
| 240 | struct drm_mode_create_dumb *args) |
| 241 | { |
| 242 | /* have to work out size/pitch and return them */ |
Chris Wilson | ed0291f | 2011-03-19 08:21:45 +0000 | [diff] [blame] | 243 | args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 244 | args->size = args->pitch * args->height; |
| 245 | return i915_gem_create(file, dev, |
| 246 | args->size, &args->handle); |
| 247 | } |
| 248 | |
| 249 | int i915_gem_dumb_destroy(struct drm_file *file, |
| 250 | struct drm_device *dev, |
| 251 | uint32_t handle) |
| 252 | { |
| 253 | return drm_gem_handle_delete(file, handle); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Creates a new mm object and returns a handle to it. |
| 258 | */ |
| 259 | int |
| 260 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 261 | struct drm_file *file) |
| 262 | { |
| 263 | struct drm_i915_gem_create *args = data; |
Daniel Vetter | 63ed2cb | 2012-04-23 16:50:50 +0200 | [diff] [blame] | 264 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 265 | return i915_gem_create(file, dev, |
| 266 | args->size, &args->handle); |
| 267 | } |
| 268 | |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 269 | static inline int |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 270 | __copy_to_user_swizzled(char __user *cpu_vaddr, |
| 271 | const char *gpu_vaddr, int gpu_offset, |
| 272 | int length) |
| 273 | { |
| 274 | int ret, cpu_offset = 0; |
| 275 | |
| 276 | while (length > 0) { |
| 277 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 278 | int this_length = min(cacheline_end - gpu_offset, length); |
| 279 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 280 | |
| 281 | ret = __copy_to_user(cpu_vaddr + cpu_offset, |
| 282 | gpu_vaddr + swizzled_gpu_offset, |
| 283 | this_length); |
| 284 | if (ret) |
| 285 | return ret + length; |
| 286 | |
| 287 | cpu_offset += this_length; |
| 288 | gpu_offset += this_length; |
| 289 | length -= this_length; |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static inline int |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 296 | __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset, |
| 297 | const char __user *cpu_vaddr, |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 298 | int length) |
| 299 | { |
| 300 | int ret, cpu_offset = 0; |
| 301 | |
| 302 | while (length > 0) { |
| 303 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 304 | int this_length = min(cacheline_end - gpu_offset, length); |
| 305 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 306 | |
| 307 | ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset, |
| 308 | cpu_vaddr + cpu_offset, |
| 309 | this_length); |
| 310 | if (ret) |
| 311 | return ret + length; |
| 312 | |
| 313 | cpu_offset += this_length; |
| 314 | gpu_offset += this_length; |
| 315 | length -= this_length; |
| 316 | } |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 321 | /* Per-page copy function for the shmem pread fastpath. |
| 322 | * Flushes invalid cachelines before reading the target if |
| 323 | * needs_clflush is set. */ |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 324 | static int |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 325 | shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length, |
| 326 | char __user *user_data, |
| 327 | bool page_do_bit17_swizzling, bool needs_clflush) |
| 328 | { |
| 329 | char *vaddr; |
| 330 | int ret; |
| 331 | |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 332 | if (unlikely(page_do_bit17_swizzling)) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 333 | return -EINVAL; |
| 334 | |
| 335 | vaddr = kmap_atomic(page); |
| 336 | if (needs_clflush) |
| 337 | drm_clflush_virt_range(vaddr + shmem_page_offset, |
| 338 | page_length); |
| 339 | ret = __copy_to_user_inatomic(user_data, |
| 340 | vaddr + shmem_page_offset, |
| 341 | page_length); |
| 342 | kunmap_atomic(vaddr); |
| 343 | |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 344 | return ret ? -EFAULT : 0; |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 345 | } |
| 346 | |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 347 | static void |
| 348 | shmem_clflush_swizzled_range(char *addr, unsigned long length, |
| 349 | bool swizzled) |
| 350 | { |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 351 | if (unlikely(swizzled)) { |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 352 | unsigned long start = (unsigned long) addr; |
| 353 | unsigned long end = (unsigned long) addr + length; |
| 354 | |
| 355 | /* For swizzling simply ensure that we always flush both |
| 356 | * channels. Lame, but simple and it works. Swizzled |
| 357 | * pwrite/pread is far from a hotpath - current userspace |
| 358 | * doesn't use it at all. */ |
| 359 | start = round_down(start, 128); |
| 360 | end = round_up(end, 128); |
| 361 | |
| 362 | drm_clflush_virt_range((void *)start, end - start); |
| 363 | } else { |
| 364 | drm_clflush_virt_range(addr, length); |
| 365 | } |
| 366 | |
| 367 | } |
| 368 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 369 | /* Only difference to the fast-path function is that this can handle bit17 |
| 370 | * and uses non-atomic copy and kmap functions. */ |
| 371 | static int |
| 372 | shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length, |
| 373 | char __user *user_data, |
| 374 | bool page_do_bit17_swizzling, bool needs_clflush) |
| 375 | { |
| 376 | char *vaddr; |
| 377 | int ret; |
| 378 | |
| 379 | vaddr = kmap(page); |
| 380 | if (needs_clflush) |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 381 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
| 382 | page_length, |
| 383 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 384 | |
| 385 | if (page_do_bit17_swizzling) |
| 386 | ret = __copy_to_user_swizzled(user_data, |
| 387 | vaddr, shmem_page_offset, |
| 388 | page_length); |
| 389 | else |
| 390 | ret = __copy_to_user(user_data, |
| 391 | vaddr + shmem_page_offset, |
| 392 | page_length); |
| 393 | kunmap(page); |
| 394 | |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 395 | return ret ? - EFAULT : 0; |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 396 | } |
| 397 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 398 | static int |
Daniel Vetter | dbf7bff | 2012-03-25 19:47:29 +0200 | [diff] [blame] | 399 | i915_gem_shmem_pread(struct drm_device *dev, |
| 400 | struct drm_i915_gem_object *obj, |
| 401 | struct drm_i915_gem_pread *args, |
| 402 | struct drm_file *file) |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 403 | { |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 404 | char __user *user_data; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 405 | ssize_t remain; |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 406 | loff_t offset; |
Ben Widawsky | eb2c0c8 | 2012-02-15 14:42:43 +0100 | [diff] [blame] | 407 | int shmem_page_offset, page_length, ret = 0; |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 408 | int obj_do_bit17_swizzling, page_do_bit17_swizzling; |
Daniel Vetter | 96d79b5 | 2012-03-25 19:47:36 +0200 | [diff] [blame] | 409 | int prefaulted = 0; |
Daniel Vetter | 8489731 | 2012-03-25 19:47:31 +0200 | [diff] [blame] | 410 | int needs_clflush = 0; |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 411 | struct sg_page_iter sg_iter; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 412 | |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 413 | user_data = to_user_ptr(args->data_ptr); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 414 | remain = args->size; |
| 415 | |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 416 | obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 417 | |
Daniel Vetter | 8489731 | 2012-03-25 19:47:31 +0200 | [diff] [blame] | 418 | if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) { |
| 419 | /* If we're not in the cpu read domain, set ourself into the gtt |
| 420 | * read domain and manually flush cachelines (if required). This |
| 421 | * optimizes for the case when the gpu will dirty the data |
| 422 | * anyway again before the next pread happens. */ |
| 423 | if (obj->cache_level == I915_CACHE_NONE) |
| 424 | needs_clflush = 1; |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 425 | if (i915_gem_obj_ggtt_bound(obj)) { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 426 | ret = i915_gem_object_set_to_gtt_domain(obj, false); |
| 427 | if (ret) |
| 428 | return ret; |
| 429 | } |
Daniel Vetter | 8489731 | 2012-03-25 19:47:31 +0200 | [diff] [blame] | 430 | } |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 431 | |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 432 | ret = i915_gem_object_get_pages(obj); |
| 433 | if (ret) |
| 434 | return ret; |
| 435 | |
| 436 | i915_gem_object_pin_pages(obj); |
| 437 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 438 | offset = args->offset; |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 439 | |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 440 | for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, |
| 441 | offset >> PAGE_SHIFT) { |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 442 | struct page *page = sg_page_iter_page(&sg_iter); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 443 | |
| 444 | if (remain <= 0) |
| 445 | break; |
| 446 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 447 | /* Operation in this page |
| 448 | * |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 449 | * shmem_page_offset = offset within page in shmem file |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 450 | * page_length = bytes to copy for this page |
| 451 | */ |
Chris Wilson | c8cbbb8 | 2011-05-12 22:17:11 +0100 | [diff] [blame] | 452 | shmem_page_offset = offset_in_page(offset); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 453 | page_length = remain; |
| 454 | if ((shmem_page_offset + page_length) > PAGE_SIZE) |
| 455 | page_length = PAGE_SIZE - shmem_page_offset; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 456 | |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 457 | page_do_bit17_swizzling = obj_do_bit17_swizzling && |
| 458 | (page_to_phys(page) & (1 << 17)) != 0; |
| 459 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 460 | ret = shmem_pread_fast(page, shmem_page_offset, page_length, |
| 461 | user_data, page_do_bit17_swizzling, |
| 462 | needs_clflush); |
| 463 | if (ret == 0) |
| 464 | goto next_page; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 465 | |
Daniel Vetter | dbf7bff | 2012-03-25 19:47:29 +0200 | [diff] [blame] | 466 | mutex_unlock(&dev->struct_mutex); |
| 467 | |
Xiong Zhang | 0b74b50 | 2013-07-19 13:51:24 +0800 | [diff] [blame^] | 468 | if (likely(!i915_prefault_disable) && !prefaulted) { |
Daniel Vetter | f56f821 | 2012-03-25 19:47:41 +0200 | [diff] [blame] | 469 | ret = fault_in_multipages_writeable(user_data, remain); |
Daniel Vetter | 96d79b5 | 2012-03-25 19:47:36 +0200 | [diff] [blame] | 470 | /* Userspace is tricking us, but we've already clobbered |
| 471 | * its pages with the prefault and promised to write the |
| 472 | * data up to the first fault. Hence ignore any errors |
| 473 | * and just continue. */ |
| 474 | (void)ret; |
| 475 | prefaulted = 1; |
| 476 | } |
| 477 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 478 | ret = shmem_pread_slow(page, shmem_page_offset, page_length, |
| 479 | user_data, page_do_bit17_swizzling, |
| 480 | needs_clflush); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 481 | |
Daniel Vetter | dbf7bff | 2012-03-25 19:47:29 +0200 | [diff] [blame] | 482 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 483 | |
Daniel Vetter | dbf7bff | 2012-03-25 19:47:29 +0200 | [diff] [blame] | 484 | next_page: |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 485 | mark_page_accessed(page); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 486 | |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 487 | if (ret) |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 488 | goto out; |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 489 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 490 | remain -= page_length; |
Daniel Vetter | 8461d22 | 2011-12-14 13:57:32 +0100 | [diff] [blame] | 491 | user_data += page_length; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 492 | offset += page_length; |
| 493 | } |
| 494 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 495 | out: |
Chris Wilson | f60d7f0 | 2012-09-04 21:02:56 +0100 | [diff] [blame] | 496 | i915_gem_object_unpin_pages(obj); |
| 497 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 498 | return ret; |
| 499 | } |
| 500 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 501 | /** |
| 502 | * Reads data from the object referenced by handle. |
| 503 | * |
| 504 | * On error, the contents of *data are undefined. |
| 505 | */ |
| 506 | int |
| 507 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 508 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 509 | { |
| 510 | struct drm_i915_gem_pread *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 511 | struct drm_i915_gem_object *obj; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 512 | int ret = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 513 | |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 514 | if (args->size == 0) |
| 515 | return 0; |
| 516 | |
| 517 | if (!access_ok(VERIFY_WRITE, |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 518 | to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 519 | args->size)) |
| 520 | return -EFAULT; |
| 521 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 522 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 523 | if (ret) |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 524 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 525 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 526 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 527 | if (&obj->base == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 528 | ret = -ENOENT; |
| 529 | goto unlock; |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 530 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 531 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 532 | /* Bounds check source. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 533 | if (args->offset > obj->base.size || |
| 534 | args->size > obj->base.size - args->offset) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 535 | ret = -EINVAL; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 536 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 539 | /* prime objects have no backing filp to GEM pread/pwrite |
| 540 | * pages from. |
| 541 | */ |
| 542 | if (!obj->base.filp) { |
| 543 | ret = -EINVAL; |
| 544 | goto out; |
| 545 | } |
| 546 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 547 | trace_i915_gem_object_pread(obj, args->offset, args->size); |
| 548 | |
Daniel Vetter | dbf7bff | 2012-03-25 19:47:29 +0200 | [diff] [blame] | 549 | ret = i915_gem_shmem_pread(dev, obj, args, file); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 550 | |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 551 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 552 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 553 | unlock: |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 554 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 555 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 558 | /* This is the fast write path which cannot handle |
| 559 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 560 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 561 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 562 | static inline int |
| 563 | fast_user_write(struct io_mapping *mapping, |
| 564 | loff_t page_base, int page_offset, |
| 565 | char __user *user_data, |
| 566 | int length) |
| 567 | { |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 568 | void __iomem *vaddr_atomic; |
| 569 | void *vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 570 | unsigned long unwritten; |
| 571 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 572 | vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base); |
Ben Widawsky | 4f0c7cf | 2012-04-16 14:07:47 -0700 | [diff] [blame] | 573 | /* We can use the cpu mem copy function because this is X86. */ |
| 574 | vaddr = (void __force*)vaddr_atomic + page_offset; |
| 575 | unwritten = __copy_from_user_inatomic_nocache(vaddr, |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 576 | user_data, length); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 577 | io_mapping_unmap_atomic(vaddr_atomic); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 578 | return unwritten; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 581 | /** |
| 582 | * This is the fast pwrite path, where we copy the data directly from the |
| 583 | * user into the GTT, uncached. |
| 584 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 585 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 586 | i915_gem_gtt_pwrite_fast(struct drm_device *dev, |
| 587 | struct drm_i915_gem_object *obj, |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 588 | struct drm_i915_gem_pwrite *args, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 589 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 590 | { |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 591 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 592 | ssize_t remain; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 593 | loff_t offset, page_base; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 594 | char __user *user_data; |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 595 | int page_offset, page_length, ret; |
| 596 | |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 597 | ret = i915_gem_object_pin(obj, 0, true, true); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 598 | if (ret) |
| 599 | goto out; |
| 600 | |
| 601 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 602 | if (ret) |
| 603 | goto out_unpin; |
| 604 | |
| 605 | ret = i915_gem_object_put_fence(obj); |
| 606 | if (ret) |
| 607 | goto out_unpin; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 608 | |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 609 | user_data = to_user_ptr(args->data_ptr); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 610 | remain = args->size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 611 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 612 | offset = i915_gem_obj_ggtt_offset(obj) + args->offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 613 | |
| 614 | while (remain > 0) { |
| 615 | /* Operation in this page |
| 616 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 617 | * page_base = page offset within aperture |
| 618 | * page_offset = offset within page |
| 619 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 620 | */ |
Chris Wilson | c8cbbb8 | 2011-05-12 22:17:11 +0100 | [diff] [blame] | 621 | page_base = offset & PAGE_MASK; |
| 622 | page_offset = offset_in_page(offset); |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 623 | page_length = remain; |
| 624 | if ((page_offset + remain) > PAGE_SIZE) |
| 625 | page_length = PAGE_SIZE - page_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 626 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 627 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 628 | * source page isn't available. Return the error and we'll |
| 629 | * retry in the slow path. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 630 | */ |
Ben Widawsky | 5d4545a | 2013-01-17 12:45:15 -0800 | [diff] [blame] | 631 | if (fast_user_write(dev_priv->gtt.mappable, page_base, |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 632 | page_offset, user_data, page_length)) { |
| 633 | ret = -EFAULT; |
| 634 | goto out_unpin; |
| 635 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 636 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 637 | remain -= page_length; |
| 638 | user_data += page_length; |
| 639 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 640 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 641 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 642 | out_unpin: |
| 643 | i915_gem_object_unpin(obj); |
| 644 | out: |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 645 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 648 | /* Per-page copy function for the shmem pwrite fastpath. |
| 649 | * Flushes invalid cachelines before writing to the target if |
| 650 | * needs_clflush_before is set and flushes out any written cachelines after |
| 651 | * writing if needs_clflush is set. */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 652 | static int |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 653 | shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length, |
| 654 | char __user *user_data, |
| 655 | bool page_do_bit17_swizzling, |
| 656 | bool needs_clflush_before, |
| 657 | bool needs_clflush_after) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 658 | { |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 659 | char *vaddr; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 660 | int ret; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 661 | |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 662 | if (unlikely(page_do_bit17_swizzling)) |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 663 | return -EINVAL; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 664 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 665 | vaddr = kmap_atomic(page); |
| 666 | if (needs_clflush_before) |
| 667 | drm_clflush_virt_range(vaddr + shmem_page_offset, |
| 668 | page_length); |
| 669 | ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset, |
| 670 | user_data, |
| 671 | page_length); |
| 672 | if (needs_clflush_after) |
| 673 | drm_clflush_virt_range(vaddr + shmem_page_offset, |
| 674 | page_length); |
| 675 | kunmap_atomic(vaddr); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 676 | |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 677 | return ret ? -EFAULT : 0; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 680 | /* Only difference to the fast-path function is that this can handle bit17 |
| 681 | * and uses non-atomic copy and kmap functions. */ |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 682 | static int |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 683 | shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length, |
| 684 | char __user *user_data, |
| 685 | bool page_do_bit17_swizzling, |
| 686 | bool needs_clflush_before, |
| 687 | bool needs_clflush_after) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 688 | { |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 689 | char *vaddr; |
| 690 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 691 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 692 | vaddr = kmap(page); |
Daniel Vetter | e7e58eb | 2012-03-25 19:47:43 +0200 | [diff] [blame] | 693 | if (unlikely(needs_clflush_before || page_do_bit17_swizzling)) |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 694 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
| 695 | page_length, |
| 696 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 697 | if (page_do_bit17_swizzling) |
| 698 | ret = __copy_from_user_swizzled(vaddr, shmem_page_offset, |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 699 | user_data, |
| 700 | page_length); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 701 | else |
| 702 | ret = __copy_from_user(vaddr + shmem_page_offset, |
| 703 | user_data, |
| 704 | page_length); |
| 705 | if (needs_clflush_after) |
Daniel Vetter | 23c18c7 | 2012-03-25 19:47:42 +0200 | [diff] [blame] | 706 | shmem_clflush_swizzled_range(vaddr + shmem_page_offset, |
| 707 | page_length, |
| 708 | page_do_bit17_swizzling); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 709 | kunmap(page); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 710 | |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 711 | return ret ? -EFAULT : 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 714 | static int |
Daniel Vetter | e244a44 | 2012-03-25 19:47:28 +0200 | [diff] [blame] | 715 | i915_gem_shmem_pwrite(struct drm_device *dev, |
| 716 | struct drm_i915_gem_object *obj, |
| 717 | struct drm_i915_gem_pwrite *args, |
| 718 | struct drm_file *file) |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 719 | { |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 720 | ssize_t remain; |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 721 | loff_t offset; |
| 722 | char __user *user_data; |
Ben Widawsky | eb2c0c8 | 2012-02-15 14:42:43 +0100 | [diff] [blame] | 723 | int shmem_page_offset, page_length, ret = 0; |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 724 | int obj_do_bit17_swizzling, page_do_bit17_swizzling; |
Daniel Vetter | e244a44 | 2012-03-25 19:47:28 +0200 | [diff] [blame] | 725 | int hit_slowpath = 0; |
Daniel Vetter | 5864288 | 2012-03-25 19:47:37 +0200 | [diff] [blame] | 726 | int needs_clflush_after = 0; |
| 727 | int needs_clflush_before = 0; |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 728 | struct sg_page_iter sg_iter; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 729 | |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 730 | user_data = to_user_ptr(args->data_ptr); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 731 | remain = args->size; |
| 732 | |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 733 | obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 734 | |
Daniel Vetter | 5864288 | 2012-03-25 19:47:37 +0200 | [diff] [blame] | 735 | if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) { |
| 736 | /* If we're not in the cpu write domain, set ourself into the gtt |
| 737 | * write domain and manually flush cachelines (if required). This |
| 738 | * optimizes for the case when the gpu will use the data |
| 739 | * right away and we therefore have to clflush anyway. */ |
| 740 | if (obj->cache_level == I915_CACHE_NONE) |
| 741 | needs_clflush_after = 1; |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 742 | if (i915_gem_obj_ggtt_bound(obj)) { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 743 | ret = i915_gem_object_set_to_gtt_domain(obj, true); |
| 744 | if (ret) |
| 745 | return ret; |
| 746 | } |
Daniel Vetter | 5864288 | 2012-03-25 19:47:37 +0200 | [diff] [blame] | 747 | } |
| 748 | /* Same trick applies for invalidate partially written cachelines before |
| 749 | * writing. */ |
| 750 | if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU) |
| 751 | && obj->cache_level == I915_CACHE_NONE) |
| 752 | needs_clflush_before = 1; |
| 753 | |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 754 | ret = i915_gem_object_get_pages(obj); |
| 755 | if (ret) |
| 756 | return ret; |
| 757 | |
| 758 | i915_gem_object_pin_pages(obj); |
| 759 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 760 | offset = args->offset; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 761 | obj->dirty = 1; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 762 | |
Imre Deak | 67d5a50 | 2013-02-18 19:28:02 +0200 | [diff] [blame] | 763 | for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, |
| 764 | offset >> PAGE_SHIFT) { |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 765 | struct page *page = sg_page_iter_page(&sg_iter); |
Daniel Vetter | 5864288 | 2012-03-25 19:47:37 +0200 | [diff] [blame] | 766 | int partial_cacheline_write; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 767 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 768 | if (remain <= 0) |
| 769 | break; |
| 770 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 771 | /* Operation in this page |
| 772 | * |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 773 | * shmem_page_offset = offset within page in shmem file |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 774 | * page_length = bytes to copy for this page |
| 775 | */ |
Chris Wilson | c8cbbb8 | 2011-05-12 22:17:11 +0100 | [diff] [blame] | 776 | shmem_page_offset = offset_in_page(offset); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 777 | |
| 778 | page_length = remain; |
| 779 | if ((shmem_page_offset + page_length) > PAGE_SIZE) |
| 780 | page_length = PAGE_SIZE - shmem_page_offset; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 781 | |
Daniel Vetter | 5864288 | 2012-03-25 19:47:37 +0200 | [diff] [blame] | 782 | /* If we don't overwrite a cacheline completely we need to be |
| 783 | * careful to have up-to-date data by first clflushing. Don't |
| 784 | * overcomplicate things and flush the entire patch. */ |
| 785 | partial_cacheline_write = needs_clflush_before && |
| 786 | ((shmem_page_offset | page_length) |
| 787 | & (boot_cpu_data.x86_clflush_size - 1)); |
| 788 | |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 789 | page_do_bit17_swizzling = obj_do_bit17_swizzling && |
| 790 | (page_to_phys(page) & (1 << 17)) != 0; |
| 791 | |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 792 | ret = shmem_pwrite_fast(page, shmem_page_offset, page_length, |
| 793 | user_data, page_do_bit17_swizzling, |
| 794 | partial_cacheline_write, |
| 795 | needs_clflush_after); |
| 796 | if (ret == 0) |
| 797 | goto next_page; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 798 | |
Daniel Vetter | e244a44 | 2012-03-25 19:47:28 +0200 | [diff] [blame] | 799 | hit_slowpath = 1; |
Daniel Vetter | e244a44 | 2012-03-25 19:47:28 +0200 | [diff] [blame] | 800 | mutex_unlock(&dev->struct_mutex); |
Daniel Vetter | d174bd6 | 2012-03-25 19:47:40 +0200 | [diff] [blame] | 801 | ret = shmem_pwrite_slow(page, shmem_page_offset, page_length, |
| 802 | user_data, page_do_bit17_swizzling, |
| 803 | partial_cacheline_write, |
| 804 | needs_clflush_after); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 805 | |
Daniel Vetter | e244a44 | 2012-03-25 19:47:28 +0200 | [diff] [blame] | 806 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 807 | |
Daniel Vetter | e244a44 | 2012-03-25 19:47:28 +0200 | [diff] [blame] | 808 | next_page: |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 809 | set_page_dirty(page); |
| 810 | mark_page_accessed(page); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 811 | |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 812 | if (ret) |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 813 | goto out; |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 814 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 815 | remain -= page_length; |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 816 | user_data += page_length; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 817 | offset += page_length; |
| 818 | } |
| 819 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 820 | out: |
Chris Wilson | 755d221 | 2012-09-04 21:02:55 +0100 | [diff] [blame] | 821 | i915_gem_object_unpin_pages(obj); |
| 822 | |
Daniel Vetter | e244a44 | 2012-03-25 19:47:28 +0200 | [diff] [blame] | 823 | if (hit_slowpath) { |
Daniel Vetter | 8dcf015 | 2012-11-15 16:53:58 +0100 | [diff] [blame] | 824 | /* |
| 825 | * Fixup: Flush cpu caches in case we didn't flush the dirty |
| 826 | * cachelines in-line while writing and the object moved |
| 827 | * out of the cpu write domain while we've dropped the lock. |
| 828 | */ |
| 829 | if (!needs_clflush_after && |
| 830 | obj->base.write_domain != I915_GEM_DOMAIN_CPU) { |
Daniel Vetter | e244a44 | 2012-03-25 19:47:28 +0200 | [diff] [blame] | 831 | i915_gem_clflush_object(obj); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 832 | i915_gem_chipset_flush(dev); |
Daniel Vetter | e244a44 | 2012-03-25 19:47:28 +0200 | [diff] [blame] | 833 | } |
Daniel Vetter | 8c59967 | 2011-12-14 13:57:31 +0100 | [diff] [blame] | 834 | } |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 835 | |
Daniel Vetter | 5864288 | 2012-03-25 19:47:37 +0200 | [diff] [blame] | 836 | if (needs_clflush_after) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 837 | i915_gem_chipset_flush(dev); |
Daniel Vetter | 5864288 | 2012-03-25 19:47:37 +0200 | [diff] [blame] | 838 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 839 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /** |
| 843 | * Writes data to the object referenced by handle. |
| 844 | * |
| 845 | * On error, the contents of the buffer that were to be modified are undefined. |
| 846 | */ |
| 847 | int |
| 848 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 849 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 850 | { |
| 851 | struct drm_i915_gem_pwrite *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 852 | struct drm_i915_gem_object *obj; |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 853 | int ret; |
| 854 | |
| 855 | if (args->size == 0) |
| 856 | return 0; |
| 857 | |
| 858 | if (!access_ok(VERIFY_READ, |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 859 | to_user_ptr(args->data_ptr), |
Chris Wilson | 51311d0 | 2010-11-17 09:10:42 +0000 | [diff] [blame] | 860 | args->size)) |
| 861 | return -EFAULT; |
| 862 | |
Xiong Zhang | 0b74b50 | 2013-07-19 13:51:24 +0800 | [diff] [blame^] | 863 | if (likely(!i915_prefault_disable)) { |
| 864 | ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr), |
| 865 | args->size); |
| 866 | if (ret) |
| 867 | return -EFAULT; |
| 868 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 869 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 870 | ret = i915_mutex_lock_interruptible(dev); |
| 871 | if (ret) |
| 872 | return ret; |
| 873 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 874 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 875 | if (&obj->base == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 876 | ret = -ENOENT; |
| 877 | goto unlock; |
| 878 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 879 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 880 | /* Bounds check destination. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 881 | if (args->offset > obj->base.size || |
| 882 | args->size > obj->base.size - args->offset) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 883 | ret = -EINVAL; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 884 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 885 | } |
| 886 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 887 | /* prime objects have no backing filp to GEM pread/pwrite |
| 888 | * pages from. |
| 889 | */ |
| 890 | if (!obj->base.filp) { |
| 891 | ret = -EINVAL; |
| 892 | goto out; |
| 893 | } |
| 894 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 895 | trace_i915_gem_object_pwrite(obj, args->offset, args->size); |
| 896 | |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 897 | ret = -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 898 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 899 | * it would end up going through the fenced access, and we'll get |
| 900 | * different detiling behavior between reading and writing. |
| 901 | * pread/pwrite currently are reading and writing from the CPU |
| 902 | * perspective, requiring manual detiling by the client. |
| 903 | */ |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 904 | if (obj->phys_obj) { |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 905 | ret = i915_gem_phys_pwrite(dev, obj, args, file); |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 906 | goto out; |
| 907 | } |
| 908 | |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 909 | if (obj->cache_level == I915_CACHE_NONE && |
Daniel Vetter | c07496f | 2012-04-13 15:51:51 +0200 | [diff] [blame] | 910 | obj->tiling_mode == I915_TILING_NONE && |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 911 | obj->base.write_domain != I915_GEM_DOMAIN_CPU) { |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 912 | ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file); |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 913 | /* Note that the gtt paths might fail with non-page-backed user |
| 914 | * pointers (e.g. gtt mappings when moving data between |
| 915 | * textures). Fallback to the shmem path in that case. */ |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 916 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 917 | |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 918 | if (ret == -EFAULT || ret == -ENOSPC) |
Daniel Vetter | 935aaa6 | 2012-03-25 19:47:35 +0200 | [diff] [blame] | 919 | ret = i915_gem_shmem_pwrite(dev, obj, args, file); |
Daniel Vetter | 5c0480f | 2011-12-14 13:57:30 +0100 | [diff] [blame] | 920 | |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 921 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 922 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 923 | unlock: |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 924 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 925 | return ret; |
| 926 | } |
| 927 | |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 928 | int |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 929 | i915_gem_check_wedge(struct i915_gpu_error *error, |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 930 | bool interruptible) |
| 931 | { |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 932 | if (i915_reset_in_progress(error)) { |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 933 | /* Non-interruptible callers can't handle -EAGAIN, hence return |
| 934 | * -EIO unconditionally for these. */ |
| 935 | if (!interruptible) |
| 936 | return -EIO; |
| 937 | |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 938 | /* Recovery complete, but the reset failed ... */ |
| 939 | if (i915_terminally_wedged(error)) |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 940 | return -EIO; |
| 941 | |
| 942 | return -EAGAIN; |
| 943 | } |
| 944 | |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * Compare seqno against outstanding lazy request. Emit a request if they are |
| 950 | * equal. |
| 951 | */ |
| 952 | static int |
| 953 | i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno) |
| 954 | { |
| 955 | int ret; |
| 956 | |
| 957 | BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex)); |
| 958 | |
| 959 | ret = 0; |
| 960 | if (seqno == ring->outstanding_lazy_request) |
Mika Kuoppala | 0025c07 | 2013-06-12 12:35:30 +0300 | [diff] [blame] | 961 | ret = i915_add_request(ring, NULL); |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 962 | |
| 963 | return ret; |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * __wait_seqno - wait until execution of seqno has finished |
| 968 | * @ring: the ring expected to report seqno |
| 969 | * @seqno: duh! |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 970 | * @reset_counter: reset sequence associated with the given seqno |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 971 | * @interruptible: do an interruptible wait (normally yes) |
| 972 | * @timeout: in - how long to wait (NULL forever); out - how much time remaining |
| 973 | * |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 974 | * Note: It is of utmost importance that the passed in seqno and reset_counter |
| 975 | * values have been read by the caller in an smp safe manner. Where read-side |
| 976 | * locks are involved, it is sufficient to read the reset_counter before |
| 977 | * unlocking the lock that protects the seqno. For lockless tricks, the |
| 978 | * reset_counter _must_ be read before, and an appropriate smp_rmb must be |
| 979 | * inserted. |
| 980 | * |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 981 | * Returns 0 if the seqno was found within the alloted time. Else returns the |
| 982 | * errno with remaining time filled in timeout argument. |
| 983 | */ |
| 984 | static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno, |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 985 | unsigned reset_counter, |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 986 | bool interruptible, struct timespec *timeout) |
| 987 | { |
| 988 | drm_i915_private_t *dev_priv = ring->dev->dev_private; |
| 989 | struct timespec before, now, wait_time={1,0}; |
| 990 | unsigned long timeout_jiffies; |
| 991 | long end; |
| 992 | bool wait_forever = true; |
| 993 | int ret; |
| 994 | |
| 995 | if (i915_seqno_passed(ring->get_seqno(ring, true), seqno)) |
| 996 | return 0; |
| 997 | |
| 998 | trace_i915_gem_request_wait_begin(ring, seqno); |
| 999 | |
| 1000 | if (timeout != NULL) { |
| 1001 | wait_time = *timeout; |
| 1002 | wait_forever = false; |
| 1003 | } |
| 1004 | |
Imre Deak | e054cc3 | 2013-05-21 20:03:19 +0300 | [diff] [blame] | 1005 | timeout_jiffies = timespec_to_jiffies_timeout(&wait_time); |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 1006 | |
| 1007 | if (WARN_ON(!ring->irq_get(ring))) |
| 1008 | return -ENODEV; |
| 1009 | |
| 1010 | /* Record current time in case interrupted by signal, or wedged * */ |
| 1011 | getrawmonotonic(&before); |
| 1012 | |
| 1013 | #define EXIT_COND \ |
| 1014 | (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \ |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 1015 | i915_reset_in_progress(&dev_priv->gpu_error) || \ |
| 1016 | reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 1017 | do { |
| 1018 | if (interruptible) |
| 1019 | end = wait_event_interruptible_timeout(ring->irq_queue, |
| 1020 | EXIT_COND, |
| 1021 | timeout_jiffies); |
| 1022 | else |
| 1023 | end = wait_event_timeout(ring->irq_queue, EXIT_COND, |
| 1024 | timeout_jiffies); |
| 1025 | |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 1026 | /* We need to check whether any gpu reset happened in between |
| 1027 | * the caller grabbing the seqno and now ... */ |
| 1028 | if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) |
| 1029 | end = -EAGAIN; |
| 1030 | |
| 1031 | /* ... but upgrade the -EGAIN to an -EIO if the gpu is truely |
| 1032 | * gone. */ |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 1033 | ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible); |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 1034 | if (ret) |
| 1035 | end = ret; |
| 1036 | } while (end == 0 && wait_forever); |
| 1037 | |
| 1038 | getrawmonotonic(&now); |
| 1039 | |
| 1040 | ring->irq_put(ring); |
| 1041 | trace_i915_gem_request_wait_end(ring, seqno); |
| 1042 | #undef EXIT_COND |
| 1043 | |
| 1044 | if (timeout) { |
| 1045 | struct timespec sleep_time = timespec_sub(now, before); |
| 1046 | *timeout = timespec_sub(*timeout, sleep_time); |
Chris Wilson | 4f42f4e | 2013-04-26 16:22:46 +0300 | [diff] [blame] | 1047 | if (!timespec_valid(timeout)) /* i.e. negative time remains */ |
| 1048 | set_normalized_timespec(timeout, 0, 0); |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 1049 | } |
| 1050 | |
| 1051 | switch (end) { |
| 1052 | case -EIO: |
| 1053 | case -EAGAIN: /* Wedged */ |
| 1054 | case -ERESTARTSYS: /* Signal */ |
| 1055 | return (int)end; |
| 1056 | case 0: /* Timeout */ |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 1057 | return -ETIME; |
| 1058 | default: /* Completed */ |
| 1059 | WARN_ON(end < 0); /* We're not aware of other errors */ |
| 1060 | return 0; |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | /** |
| 1065 | * Waits for a sequence number to be signaled, and cleans up the |
| 1066 | * request and object lists appropriately for that event. |
| 1067 | */ |
| 1068 | int |
| 1069 | i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno) |
| 1070 | { |
| 1071 | struct drm_device *dev = ring->dev; |
| 1072 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1073 | bool interruptible = dev_priv->mm.interruptible; |
| 1074 | int ret; |
| 1075 | |
| 1076 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 1077 | BUG_ON(seqno == 0); |
| 1078 | |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 1079 | ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible); |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 1080 | if (ret) |
| 1081 | return ret; |
| 1082 | |
| 1083 | ret = i915_gem_check_olr(ring, seqno); |
| 1084 | if (ret) |
| 1085 | return ret; |
| 1086 | |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 1087 | return __wait_seqno(ring, seqno, |
| 1088 | atomic_read(&dev_priv->gpu_error.reset_counter), |
| 1089 | interruptible, NULL); |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 1090 | } |
| 1091 | |
Chris Wilson | d26e3af | 2013-06-29 22:05:26 +0100 | [diff] [blame] | 1092 | static int |
| 1093 | i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj, |
| 1094 | struct intel_ring_buffer *ring) |
| 1095 | { |
| 1096 | i915_gem_retire_requests_ring(ring); |
| 1097 | |
| 1098 | /* Manually manage the write flush as we may have not yet |
| 1099 | * retired the buffer. |
| 1100 | * |
| 1101 | * Note that the last_write_seqno is always the earlier of |
| 1102 | * the two (read/write) seqno, so if we haved successfully waited, |
| 1103 | * we know we have passed the last write. |
| 1104 | */ |
| 1105 | obj->last_write_seqno = 0; |
| 1106 | obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS; |
| 1107 | |
| 1108 | return 0; |
| 1109 | } |
| 1110 | |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 1111 | /** |
| 1112 | * Ensures that all rendering to the object has completed and the object is |
| 1113 | * safe to unbind from the GTT or access from the CPU. |
| 1114 | */ |
| 1115 | static __must_check int |
| 1116 | i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj, |
| 1117 | bool readonly) |
| 1118 | { |
| 1119 | struct intel_ring_buffer *ring = obj->ring; |
| 1120 | u32 seqno; |
| 1121 | int ret; |
| 1122 | |
| 1123 | seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno; |
| 1124 | if (seqno == 0) |
| 1125 | return 0; |
| 1126 | |
| 1127 | ret = i915_wait_seqno(ring, seqno); |
| 1128 | if (ret) |
| 1129 | return ret; |
| 1130 | |
Chris Wilson | d26e3af | 2013-06-29 22:05:26 +0100 | [diff] [blame] | 1131 | return i915_gem_object_wait_rendering__tail(obj, ring); |
Chris Wilson | b361237 | 2012-08-24 09:35:08 +0100 | [diff] [blame] | 1132 | } |
| 1133 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1134 | /* A nonblocking variant of the above wait. This is a highly dangerous routine |
| 1135 | * as the object state may change during this call. |
| 1136 | */ |
| 1137 | static __must_check int |
| 1138 | i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj, |
| 1139 | bool readonly) |
| 1140 | { |
| 1141 | struct drm_device *dev = obj->base.dev; |
| 1142 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1143 | struct intel_ring_buffer *ring = obj->ring; |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 1144 | unsigned reset_counter; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1145 | u32 seqno; |
| 1146 | int ret; |
| 1147 | |
| 1148 | BUG_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 1149 | BUG_ON(!dev_priv->mm.interruptible); |
| 1150 | |
| 1151 | seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno; |
| 1152 | if (seqno == 0) |
| 1153 | return 0; |
| 1154 | |
Daniel Vetter | 33196de | 2012-11-14 17:14:05 +0100 | [diff] [blame] | 1155 | ret = i915_gem_check_wedge(&dev_priv->gpu_error, true); |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1156 | if (ret) |
| 1157 | return ret; |
| 1158 | |
| 1159 | ret = i915_gem_check_olr(ring, seqno); |
| 1160 | if (ret) |
| 1161 | return ret; |
| 1162 | |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 1163 | reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter); |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1164 | mutex_unlock(&dev->struct_mutex); |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 1165 | ret = __wait_seqno(ring, seqno, reset_counter, true, NULL); |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1166 | mutex_lock(&dev->struct_mutex); |
Chris Wilson | d26e3af | 2013-06-29 22:05:26 +0100 | [diff] [blame] | 1167 | if (ret) |
| 1168 | return ret; |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1169 | |
Chris Wilson | d26e3af | 2013-06-29 22:05:26 +0100 | [diff] [blame] | 1170 | return i915_gem_object_wait_rendering__tail(obj, ring); |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1173 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1174 | * Called when user space prepares to use an object with the CPU, either |
| 1175 | * through the mmap ioctl's mapping or a GTT mapping. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1176 | */ |
| 1177 | int |
| 1178 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1179 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1180 | { |
| 1181 | struct drm_i915_gem_set_domain *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1182 | struct drm_i915_gem_object *obj; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1183 | uint32_t read_domains = args->read_domains; |
| 1184 | uint32_t write_domain = args->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1185 | int ret; |
| 1186 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1187 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | 21d509e | 2009-06-06 09:46:02 +0100 | [diff] [blame] | 1188 | if (write_domain & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1189 | return -EINVAL; |
| 1190 | |
Chris Wilson | 21d509e | 2009-06-06 09:46:02 +0100 | [diff] [blame] | 1191 | if (read_domains & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1192 | return -EINVAL; |
| 1193 | |
| 1194 | /* Having something in the write domain implies it's in the read |
| 1195 | * domain, and only that read domain. Enforce that in the request. |
| 1196 | */ |
| 1197 | if (write_domain != 0 && read_domains != write_domain) |
| 1198 | return -EINVAL; |
| 1199 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1200 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1201 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1202 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1203 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1204 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 1205 | if (&obj->base == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1206 | ret = -ENOENT; |
| 1207 | goto unlock; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1208 | } |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1209 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1210 | /* Try to flush the object off the GPU without holding the lock. |
| 1211 | * We will repeat the flush holding the lock in the normal manner |
| 1212 | * to catch cases where we are gazumped. |
| 1213 | */ |
| 1214 | ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain); |
| 1215 | if (ret) |
| 1216 | goto unref; |
| 1217 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1218 | if (read_domains & I915_GEM_DOMAIN_GTT) { |
| 1219 | ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0); |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 1220 | |
| 1221 | /* Silently promote "you're not bound, there was nothing to do" |
| 1222 | * to success, since the client was just asking us to |
| 1223 | * make sure everything was done. |
| 1224 | */ |
| 1225 | if (ret == -EINVAL) |
| 1226 | ret = 0; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1227 | } else { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1228 | ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1229 | } |
| 1230 | |
Chris Wilson | 3236f57 | 2012-08-24 09:35:09 +0100 | [diff] [blame] | 1231 | unref: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1232 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1233 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1234 | mutex_unlock(&dev->struct_mutex); |
| 1235 | return ret; |
| 1236 | } |
| 1237 | |
| 1238 | /** |
| 1239 | * Called when user space has done writes to this buffer |
| 1240 | */ |
| 1241 | int |
| 1242 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1243 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1244 | { |
| 1245 | struct drm_i915_gem_sw_finish *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1246 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1247 | int ret = 0; |
| 1248 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1249 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1250 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1251 | return ret; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1252 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1253 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 1254 | if (&obj->base == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1255 | ret = -ENOENT; |
| 1256 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1257 | } |
| 1258 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1259 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1260 | if (obj->pin_count) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1261 | i915_gem_object_flush_cpu_write_domain(obj); |
| 1262 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1263 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1264 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1265 | mutex_unlock(&dev->struct_mutex); |
| 1266 | return ret; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
| 1270 | * Maps the contents of an object, returning the address it is mapped |
| 1271 | * into. |
| 1272 | * |
| 1273 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1274 | * imply a ref on the object itself. |
| 1275 | */ |
| 1276 | int |
| 1277 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1278 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1279 | { |
| 1280 | struct drm_i915_gem_mmap *args = data; |
| 1281 | struct drm_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1282 | unsigned long addr; |
| 1283 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1284 | obj = drm_gem_object_lookup(dev, file, args->handle); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1285 | if (obj == NULL) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1286 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1287 | |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1288 | /* prime objects have no backing filp to GEM mmap |
| 1289 | * pages from. |
| 1290 | */ |
| 1291 | if (!obj->filp) { |
| 1292 | drm_gem_object_unreference_unlocked(obj); |
| 1293 | return -EINVAL; |
| 1294 | } |
| 1295 | |
Linus Torvalds | 6be5ceb | 2012-04-20 17:13:58 -0700 | [diff] [blame] | 1296 | addr = vm_mmap(obj->filp, 0, args->size, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1297 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1298 | args->offset); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 1299 | drm_gem_object_unreference_unlocked(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1300 | if (IS_ERR((void *)addr)) |
| 1301 | return addr; |
| 1302 | |
| 1303 | args->addr_ptr = (uint64_t) addr; |
| 1304 | |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1308 | /** |
| 1309 | * i915_gem_fault - fault a page into the GTT |
| 1310 | * vma: VMA in question |
| 1311 | * vmf: fault info |
| 1312 | * |
| 1313 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1314 | * from userspace. The fault handler takes care of binding the object to |
| 1315 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1316 | * only if needed based on whether the old reg is still valid or the object |
| 1317 | * is tiled) and inserting a new PTE into the faulting process. |
| 1318 | * |
| 1319 | * Note that the faulting process may involve evicting existing objects |
| 1320 | * from the GTT and/or fence registers to make room. So performance may |
| 1321 | * suffer if the GTT working set is large or there are few fence registers |
| 1322 | * left. |
| 1323 | */ |
| 1324 | int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1325 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1326 | struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data); |
| 1327 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1328 | drm_i915_private_t *dev_priv = dev->dev_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1329 | pgoff_t page_offset; |
| 1330 | unsigned long pfn; |
| 1331 | int ret = 0; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1332 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1333 | |
| 1334 | /* We don't use vmf->pgoff since that has the fake offset */ |
| 1335 | page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> |
| 1336 | PAGE_SHIFT; |
| 1337 | |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1338 | ret = i915_mutex_lock_interruptible(dev); |
| 1339 | if (ret) |
| 1340 | goto out; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1341 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 1342 | trace_i915_gem_object_fault(obj, page_offset, true, write); |
| 1343 | |
Chris Wilson | eb119bd | 2012-12-16 12:43:36 +0000 | [diff] [blame] | 1344 | /* Access to snoopable pages through the GTT is incoherent. */ |
| 1345 | if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) { |
| 1346 | ret = -EINVAL; |
| 1347 | goto unlock; |
| 1348 | } |
| 1349 | |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1350 | /* Now bind it into the GTT if needed */ |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1351 | ret = i915_gem_object_pin(obj, 0, true, false); |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 1352 | if (ret) |
| 1353 | goto unlock; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1354 | |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1355 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
| 1356 | if (ret) |
| 1357 | goto unpin; |
| 1358 | |
| 1359 | ret = i915_gem_object_get_fence(obj); |
| 1360 | if (ret) |
| 1361 | goto unpin; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1362 | |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 1363 | obj->fault_mappable = true; |
| 1364 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 1365 | pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj); |
| 1366 | pfn >>= PAGE_SHIFT; |
| 1367 | pfn += page_offset; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1368 | |
| 1369 | /* Finally, remap it using the new GTT offset */ |
| 1370 | ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); |
Chris Wilson | c983930 | 2012-11-20 10:45:17 +0000 | [diff] [blame] | 1371 | unpin: |
| 1372 | i915_gem_object_unpin(obj); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1373 | unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1374 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1375 | out: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1376 | switch (ret) { |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1377 | case -EIO: |
Daniel Vetter | a9340cc | 2012-07-04 22:18:42 +0200 | [diff] [blame] | 1378 | /* If this -EIO is due to a gpu hang, give the reset code a |
| 1379 | * chance to clean up the mess. Otherwise return the proper |
| 1380 | * SIGBUS. */ |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 1381 | if (i915_terminally_wedged(&dev_priv->gpu_error)) |
Daniel Vetter | a9340cc | 2012-07-04 22:18:42 +0200 | [diff] [blame] | 1382 | return VM_FAULT_SIGBUS; |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1383 | case -EAGAIN: |
Chris Wilson | d9bc7e9 | 2011-02-07 13:09:31 +0000 | [diff] [blame] | 1384 | /* Give the error handler a chance to run and move the |
| 1385 | * objects off the GPU active list. Next time we service the |
| 1386 | * fault, we should be able to transition the page into the |
| 1387 | * GTT without touching the GPU (and so avoid further |
| 1388 | * EIO/EGAIN). If the GPU is wedged, then there is no issue |
| 1389 | * with coherency, just lost writes. |
| 1390 | */ |
Chris Wilson | 045e769 | 2010-11-07 09:18:22 +0000 | [diff] [blame] | 1391 | set_need_resched(); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1392 | case 0: |
| 1393 | case -ERESTARTSYS: |
Chris Wilson | bed636a | 2011-02-11 20:31:19 +0000 | [diff] [blame] | 1394 | case -EINTR: |
Dmitry Rogozhkin | e79e0fe | 2012-10-03 17:15:26 +0300 | [diff] [blame] | 1395 | case -EBUSY: |
| 1396 | /* |
| 1397 | * EBUSY is ok: this just means that another thread |
| 1398 | * already did the job. |
| 1399 | */ |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1400 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1401 | case -ENOMEM: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1402 | return VM_FAULT_OOM; |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1403 | case -ENOSPC: |
| 1404 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1405 | default: |
Daniel Vetter | a7c2e1a | 2012-10-17 11:17:16 +0200 | [diff] [blame] | 1406 | WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1407 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | /** |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1412 | * i915_gem_release_mmap - remove physical page mappings |
| 1413 | * @obj: obj in question |
| 1414 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1415 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1416 | * relinquish ownership of the pages back to the system. |
| 1417 | * |
| 1418 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 1419 | * object through the GTT and then lose the fence register due to |
| 1420 | * resource pressure. Similarly if the object has been moved out of the |
| 1421 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 1422 | * mapping will then trigger a page fault on the next user access, allowing |
| 1423 | * fixup by i915_gem_fault(). |
| 1424 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 1425 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1426 | i915_gem_release_mmap(struct drm_i915_gem_object *obj) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1427 | { |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 1428 | if (!obj->fault_mappable) |
| 1429 | return; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1430 | |
Chris Wilson | f6e4788 | 2011-03-20 21:09:12 +0000 | [diff] [blame] | 1431 | if (obj->base.dev->dev_mapping) |
| 1432 | unmap_mapping_range(obj->base.dev->dev_mapping, |
| 1433 | (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT, |
| 1434 | obj->base.size, 1); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1435 | |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 1436 | obj->fault_mappable = false; |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1437 | } |
| 1438 | |
Imre Deak | 0fa8779 | 2013-01-07 21:47:35 +0200 | [diff] [blame] | 1439 | uint32_t |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 1440 | i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode) |
Chris Wilson | 92b88ae | 2010-11-09 11:47:32 +0000 | [diff] [blame] | 1441 | { |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 1442 | uint32_t gtt_size; |
Chris Wilson | 92b88ae | 2010-11-09 11:47:32 +0000 | [diff] [blame] | 1443 | |
| 1444 | if (INTEL_INFO(dev)->gen >= 4 || |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 1445 | tiling_mode == I915_TILING_NONE) |
| 1446 | return size; |
Chris Wilson | 92b88ae | 2010-11-09 11:47:32 +0000 | [diff] [blame] | 1447 | |
| 1448 | /* Previous chips need a power-of-two fence region when tiling */ |
| 1449 | if (INTEL_INFO(dev)->gen == 3) |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 1450 | gtt_size = 1024*1024; |
Chris Wilson | 92b88ae | 2010-11-09 11:47:32 +0000 | [diff] [blame] | 1451 | else |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 1452 | gtt_size = 512*1024; |
Chris Wilson | 92b88ae | 2010-11-09 11:47:32 +0000 | [diff] [blame] | 1453 | |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 1454 | while (gtt_size < size) |
| 1455 | gtt_size <<= 1; |
Chris Wilson | 92b88ae | 2010-11-09 11:47:32 +0000 | [diff] [blame] | 1456 | |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 1457 | return gtt_size; |
Chris Wilson | 92b88ae | 2010-11-09 11:47:32 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1460 | /** |
| 1461 | * i915_gem_get_gtt_alignment - return required GTT alignment for an object |
| 1462 | * @obj: object to check |
| 1463 | * |
| 1464 | * Return the required GTT alignment for an object, taking into account |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 1465 | * potential fence register mapping. |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1466 | */ |
Imre Deak | d865110c | 2013-01-07 21:47:33 +0200 | [diff] [blame] | 1467 | uint32_t |
| 1468 | i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size, |
| 1469 | int tiling_mode, bool fenced) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1470 | { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1471 | /* |
| 1472 | * Minimum alignment is 4k (GTT page size), but might be greater |
| 1473 | * if a fence register is needed for the object. |
| 1474 | */ |
Imre Deak | d865110c | 2013-01-07 21:47:33 +0200 | [diff] [blame] | 1475 | if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) || |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 1476 | tiling_mode == I915_TILING_NONE) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1477 | return 4096; |
| 1478 | |
| 1479 | /* |
| 1480 | * Previous chips need to be aligned to the size of the smallest |
| 1481 | * fence register that can contain the object. |
| 1482 | */ |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 1483 | return i915_gem_get_gtt_size(dev, size, tiling_mode); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 1484 | } |
| 1485 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1486 | static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj) |
| 1487 | { |
| 1488 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
| 1489 | int ret; |
| 1490 | |
| 1491 | if (obj->base.map_list.map) |
| 1492 | return 0; |
| 1493 | |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 1494 | dev_priv->mm.shrinker_no_lock_stealing = true; |
| 1495 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1496 | ret = drm_gem_create_mmap_offset(&obj->base); |
| 1497 | if (ret != -ENOSPC) |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 1498 | goto out; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1499 | |
| 1500 | /* Badly fragmented mmap space? The only way we can recover |
| 1501 | * space is by destroying unwanted objects. We can't randomly release |
| 1502 | * mmap_offsets as userspace expects them to be persistent for the |
| 1503 | * lifetime of the objects. The closest we can is to release the |
| 1504 | * offsets on purgeable objects by truncating it and marking it purged, |
| 1505 | * which prevents userspace from ever using that object again. |
| 1506 | */ |
| 1507 | i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT); |
| 1508 | ret = drm_gem_create_mmap_offset(&obj->base); |
| 1509 | if (ret != -ENOSPC) |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 1510 | goto out; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1511 | |
| 1512 | i915_gem_shrink_all(dev_priv); |
Daniel Vetter | da494d7 | 2012-12-20 15:11:16 +0100 | [diff] [blame] | 1513 | ret = drm_gem_create_mmap_offset(&obj->base); |
| 1514 | out: |
| 1515 | dev_priv->mm.shrinker_no_lock_stealing = false; |
| 1516 | |
| 1517 | return ret; |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj) |
| 1521 | { |
| 1522 | if (!obj->base.map_list.map) |
| 1523 | return; |
| 1524 | |
| 1525 | drm_gem_free_mmap_offset(&obj->base); |
| 1526 | } |
| 1527 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1528 | int |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1529 | i915_gem_mmap_gtt(struct drm_file *file, |
| 1530 | struct drm_device *dev, |
| 1531 | uint32_t handle, |
| 1532 | uint64_t *offset) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1533 | { |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1534 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1535 | struct drm_i915_gem_object *obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1536 | int ret; |
| 1537 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1538 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1539 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1540 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1541 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1542 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 1543 | if (&obj->base == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1544 | ret = -ENOENT; |
| 1545 | goto unlock; |
| 1546 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1547 | |
Ben Widawsky | 5d4545a | 2013-01-17 12:45:15 -0800 | [diff] [blame] | 1548 | if (obj->base.size > dev_priv->gtt.mappable_end) { |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1549 | ret = -E2BIG; |
Eric Anholt | ff56b0b | 2011-10-31 23:16:21 -0700 | [diff] [blame] | 1550 | goto out; |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1551 | } |
| 1552 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1553 | if (obj->madv != I915_MADV_WILLNEED) { |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 1554 | DRM_ERROR("Attempting to mmap a purgeable buffer\n"); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1555 | ret = -EINVAL; |
| 1556 | goto out; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 1557 | } |
| 1558 | |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 1559 | ret = i915_gem_object_create_mmap_offset(obj); |
| 1560 | if (ret) |
| 1561 | goto out; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1562 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1563 | *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1564 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1565 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1566 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1567 | unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1568 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1569 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1570 | } |
| 1571 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1572 | /** |
| 1573 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 1574 | * @dev: DRM device |
| 1575 | * @data: GTT mapping ioctl data |
| 1576 | * @file: GEM object info |
| 1577 | * |
| 1578 | * Simply returns the fake offset to userspace so it can mmap it. |
| 1579 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 1580 | * up so we can get faults in the handler above. |
| 1581 | * |
| 1582 | * The fault handler will take care of binding the object into the GTT |
| 1583 | * (since it may have been evicted to make room for something), allocating |
| 1584 | * a fence register, and mapping the appropriate aperture address into |
| 1585 | * userspace. |
| 1586 | */ |
| 1587 | int |
| 1588 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 1589 | struct drm_file *file) |
| 1590 | { |
| 1591 | struct drm_i915_gem_mmap_gtt *args = data; |
| 1592 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 1593 | return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset); |
| 1594 | } |
| 1595 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 1596 | /* Immediately discard the backing storage */ |
| 1597 | static void |
| 1598 | i915_gem_object_truncate(struct drm_i915_gem_object *obj) |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1599 | { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1600 | struct inode *inode; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1601 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 1602 | i915_gem_object_free_mmap_offset(obj); |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1603 | |
Chris Wilson | 4d6294bf | 2012-08-11 15:41:05 +0100 | [diff] [blame] | 1604 | if (obj->base.filp == NULL) |
| 1605 | return; |
| 1606 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 1607 | /* Our goal here is to return as much of the memory as |
| 1608 | * is possible back to the system as we are called from OOM. |
| 1609 | * To do this we must instruct the shmfs to drop all of its |
| 1610 | * backing pages, *now*. |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1611 | */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1612 | inode = file_inode(obj->base.filp); |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 1613 | shmem_truncate_range(inode, 0, (loff_t)-1); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 1614 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 1615 | obj->madv = __I915_MADV_PURGED; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1616 | } |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1617 | |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 1618 | static inline int |
| 1619 | i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj) |
| 1620 | { |
| 1621 | return obj->madv == I915_MADV_DONTNEED; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 1622 | } |
| 1623 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 1624 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1625 | i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1626 | { |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 1627 | struct sg_page_iter sg_iter; |
| 1628 | int ret; |
Daniel Vetter | 1286ff7 | 2012-05-10 15:25:09 +0200 | [diff] [blame] | 1629 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1630 | BUG_ON(obj->madv == __I915_MADV_PURGED); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1631 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1632 | ret = i915_gem_object_set_to_cpu_domain(obj, true); |
| 1633 | if (ret) { |
| 1634 | /* In the event of a disaster, abandon all caches and |
| 1635 | * hope for the best. |
| 1636 | */ |
| 1637 | WARN_ON(ret != -EIO); |
| 1638 | i915_gem_clflush_object(obj); |
| 1639 | obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
| 1640 | } |
| 1641 | |
Daniel Vetter | 6dacfd2 | 2011-09-12 21:30:02 +0200 | [diff] [blame] | 1642 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 1643 | i915_gem_object_save_bit_17_swizzle(obj); |
| 1644 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1645 | if (obj->madv == I915_MADV_DONTNEED) |
| 1646 | obj->dirty = 0; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1647 | |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 1648 | for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) { |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 1649 | struct page *page = sg_page_iter_page(&sg_iter); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1650 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1651 | if (obj->dirty) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1652 | set_page_dirty(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1653 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1654 | if (obj->madv == I915_MADV_WILLNEED) |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1655 | mark_page_accessed(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1656 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1657 | page_cache_release(page); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1658 | } |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1659 | obj->dirty = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1660 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1661 | sg_free_table(obj->pages); |
| 1662 | kfree(obj->pages); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
Chris Wilson | dd624af | 2013-01-15 12:39:35 +0000 | [diff] [blame] | 1665 | int |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1666 | i915_gem_object_put_pages(struct drm_i915_gem_object *obj) |
| 1667 | { |
| 1668 | const struct drm_i915_gem_object_ops *ops = obj->ops; |
| 1669 | |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 1670 | if (obj->pages == NULL) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1671 | return 0; |
| 1672 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 1673 | BUG_ON(i915_gem_obj_ggtt_bound(obj)); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1674 | |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 1675 | if (obj->pages_pin_count) |
| 1676 | return -EBUSY; |
| 1677 | |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 1678 | /* ->put_pages might need to allocate memory for the bit17 swizzle |
| 1679 | * array, hence protect them from being reaped by removing them from gtt |
| 1680 | * lists early. */ |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 1681 | list_del(&obj->global_list); |
Chris Wilson | a2165e3 | 2012-12-03 11:49:00 +0000 | [diff] [blame] | 1682 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1683 | ops->put_pages(obj); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1684 | obj->pages = NULL; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1685 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1686 | if (i915_gem_object_is_purgeable(obj)) |
| 1687 | i915_gem_object_truncate(obj); |
| 1688 | |
| 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | static long |
Daniel Vetter | 93927ca | 2013-01-10 18:03:00 +0100 | [diff] [blame] | 1693 | __i915_gem_shrink(struct drm_i915_private *dev_priv, long target, |
| 1694 | bool purgeable_only) |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1695 | { |
| 1696 | struct drm_i915_gem_object *obj, *next; |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 1697 | struct i915_address_space *vm = &dev_priv->gtt.base; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1698 | long count = 0; |
| 1699 | |
| 1700 | list_for_each_entry_safe(obj, next, |
| 1701 | &dev_priv->mm.unbound_list, |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 1702 | global_list) { |
Daniel Vetter | 93927ca | 2013-01-10 18:03:00 +0100 | [diff] [blame] | 1703 | if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) && |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1704 | i915_gem_object_put_pages(obj) == 0) { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1705 | count += obj->base.size >> PAGE_SHIFT; |
| 1706 | if (count >= target) |
| 1707 | return count; |
| 1708 | } |
| 1709 | } |
| 1710 | |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 1711 | list_for_each_entry_safe(obj, next, &vm->inactive_list, mm_list) { |
Daniel Vetter | 93927ca | 2013-01-10 18:03:00 +0100 | [diff] [blame] | 1712 | if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) && |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1713 | i915_gem_object_unbind(obj) == 0 && |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1714 | i915_gem_object_put_pages(obj) == 0) { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1715 | count += obj->base.size >> PAGE_SHIFT; |
| 1716 | if (count >= target) |
| 1717 | return count; |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | return count; |
| 1722 | } |
| 1723 | |
Daniel Vetter | 93927ca | 2013-01-10 18:03:00 +0100 | [diff] [blame] | 1724 | static long |
| 1725 | i915_gem_purge(struct drm_i915_private *dev_priv, long target) |
| 1726 | { |
| 1727 | return __i915_gem_shrink(dev_priv, target, true); |
| 1728 | } |
| 1729 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1730 | static void |
| 1731 | i915_gem_shrink_all(struct drm_i915_private *dev_priv) |
| 1732 | { |
| 1733 | struct drm_i915_gem_object *obj, *next; |
| 1734 | |
| 1735 | i915_gem_evict_everything(dev_priv->dev); |
| 1736 | |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 1737 | list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list, |
| 1738 | global_list) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1739 | i915_gem_object_put_pages(obj); |
Daniel Vetter | 225067e | 2012-08-20 10:23:20 +0200 | [diff] [blame] | 1740 | } |
| 1741 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1742 | static int |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1743 | i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1744 | { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1745 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1746 | int page_count, i; |
| 1747 | struct address_space *mapping; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1748 | struct sg_table *st; |
| 1749 | struct scatterlist *sg; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 1750 | struct sg_page_iter sg_iter; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1751 | struct page *page; |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 1752 | unsigned long last_pfn = 0; /* suppress gcc warning */ |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1753 | gfp_t gfp; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1754 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1755 | /* Assert that the object is not currently in any GPU domain. As it |
| 1756 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 1757 | * a GPU cache |
| 1758 | */ |
| 1759 | BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS); |
| 1760 | BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS); |
| 1761 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1762 | st = kmalloc(sizeof(*st), GFP_KERNEL); |
| 1763 | if (st == NULL) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1764 | return -ENOMEM; |
| 1765 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1766 | page_count = obj->base.size / PAGE_SIZE; |
| 1767 | if (sg_alloc_table(st, page_count, GFP_KERNEL)) { |
| 1768 | sg_free_table(st); |
| 1769 | kfree(st); |
| 1770 | return -ENOMEM; |
| 1771 | } |
| 1772 | |
| 1773 | /* Get the list of pages out of our struct file. They'll be pinned |
| 1774 | * at this point until we release them. |
| 1775 | * |
| 1776 | * Fail silently without starting the shrinker |
| 1777 | */ |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1778 | mapping = file_inode(obj->base.filp)->i_mapping; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1779 | gfp = mapping_gfp_mask(mapping); |
Linus Torvalds | caf4919 | 2012-12-10 10:51:16 -0800 | [diff] [blame] | 1780 | gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1781 | gfp &= ~(__GFP_IO | __GFP_WAIT); |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 1782 | sg = st->sgl; |
| 1783 | st->nents = 0; |
| 1784 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1785 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
| 1786 | if (IS_ERR(page)) { |
| 1787 | i915_gem_purge(dev_priv, page_count); |
| 1788 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
| 1789 | } |
| 1790 | if (IS_ERR(page)) { |
| 1791 | /* We've tried hard to allocate the memory by reaping |
| 1792 | * our own buffer, now let the real VM do its job and |
| 1793 | * go down in flames if truly OOM. |
| 1794 | */ |
Linus Torvalds | caf4919 | 2012-12-10 10:51:16 -0800 | [diff] [blame] | 1795 | gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1796 | gfp |= __GFP_IO | __GFP_WAIT; |
| 1797 | |
| 1798 | i915_gem_shrink_all(dev_priv); |
| 1799 | page = shmem_read_mapping_page_gfp(mapping, i, gfp); |
| 1800 | if (IS_ERR(page)) |
| 1801 | goto err_pages; |
| 1802 | |
Linus Torvalds | caf4919 | 2012-12-10 10:51:16 -0800 | [diff] [blame] | 1803 | gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 1804 | gfp &= ~(__GFP_IO | __GFP_WAIT); |
| 1805 | } |
Konrad Rzeszutek Wilk | 1625e7e | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 1806 | #ifdef CONFIG_SWIOTLB |
| 1807 | if (swiotlb_nr_tbl()) { |
| 1808 | st->nents++; |
| 1809 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 1810 | sg = sg_next(sg); |
| 1811 | continue; |
| 1812 | } |
| 1813 | #endif |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 1814 | if (!i || page_to_pfn(page) != last_pfn + 1) { |
| 1815 | if (i) |
| 1816 | sg = sg_next(sg); |
| 1817 | st->nents++; |
| 1818 | sg_set_page(sg, page, PAGE_SIZE, 0); |
| 1819 | } else { |
| 1820 | sg->length += PAGE_SIZE; |
| 1821 | } |
| 1822 | last_pfn = page_to_pfn(page); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1823 | } |
Konrad Rzeszutek Wilk | 1625e7e | 2013-06-24 11:47:48 -0400 | [diff] [blame] | 1824 | #ifdef CONFIG_SWIOTLB |
| 1825 | if (!swiotlb_nr_tbl()) |
| 1826 | #endif |
| 1827 | sg_mark_end(sg); |
Chris Wilson | 74ce6b6 | 2012-10-19 15:51:06 +0100 | [diff] [blame] | 1828 | obj->pages = st; |
| 1829 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1830 | if (i915_gem_object_needs_bit17_swizzle(obj)) |
| 1831 | i915_gem_object_do_bit_17_swizzle(obj); |
| 1832 | |
| 1833 | return 0; |
| 1834 | |
| 1835 | err_pages: |
Imre Deak | 90797e6 | 2013-02-18 19:28:03 +0200 | [diff] [blame] | 1836 | sg_mark_end(sg); |
| 1837 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 1838 | page_cache_release(sg_page_iter_page(&sg_iter)); |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 1839 | sg_free_table(st); |
| 1840 | kfree(st); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1841 | return PTR_ERR(page); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1842 | } |
| 1843 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1844 | /* Ensure that the associated pages are gathered from the backing storage |
| 1845 | * and pinned into our object. i915_gem_object_get_pages() may be called |
| 1846 | * multiple times before they are released by a single call to |
| 1847 | * i915_gem_object_put_pages() - once the pages are no longer referenced |
| 1848 | * either as a result of memory pressure (reaping pages under the shrinker) |
| 1849 | * or as the object is itself released. |
| 1850 | */ |
| 1851 | int |
| 1852 | i915_gem_object_get_pages(struct drm_i915_gem_object *obj) |
| 1853 | { |
| 1854 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
| 1855 | const struct drm_i915_gem_object_ops *ops = obj->ops; |
| 1856 | int ret; |
| 1857 | |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 1858 | if (obj->pages) |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1859 | return 0; |
| 1860 | |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 1861 | if (obj->madv != I915_MADV_WILLNEED) { |
| 1862 | DRM_ERROR("Attempting to obtain a purgeable object\n"); |
| 1863 | return -EINVAL; |
| 1864 | } |
| 1865 | |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 1866 | BUG_ON(obj->pages_pin_count); |
| 1867 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1868 | ret = ops->get_pages(obj); |
| 1869 | if (ret) |
| 1870 | return ret; |
| 1871 | |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 1872 | list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list); |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 1873 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1874 | } |
| 1875 | |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 1876 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1877 | i915_gem_object_move_to_active(struct drm_i915_gem_object *obj, |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1878 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1879 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1880 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1881 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 1882 | struct i915_address_space *vm = &dev_priv->gtt.base; |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1883 | u32 seqno = intel_ring_get_seqno(ring); |
Daniel Vetter | 617dbe2 | 2010-02-11 22:16:02 +0100 | [diff] [blame] | 1884 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1885 | BUG_ON(ring == NULL); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1886 | obj->ring = ring; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1887 | |
| 1888 | /* Add a reference if we're newly entering the active list. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1889 | if (!obj->active) { |
| 1890 | drm_gem_object_reference(&obj->base); |
| 1891 | obj->active = 1; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1892 | } |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 1893 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1894 | /* Move from whatever list we were on to the tail of execution. */ |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 1895 | list_move_tail(&obj->mm_list, &vm->active_list); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 1896 | list_move_tail(&obj->ring_list, &ring->active_list); |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1897 | |
Chris Wilson | 0201f1e | 2012-07-20 12:41:01 +0100 | [diff] [blame] | 1898 | obj->last_read_seqno = seqno; |
Chris Wilson | 7dd4906 | 2012-03-21 10:48:18 +0000 | [diff] [blame] | 1899 | |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1900 | if (obj->fenced_gpu_access) { |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1901 | obj->last_fenced_seqno = seqno; |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1902 | |
Chris Wilson | 7dd4906 | 2012-03-21 10:48:18 +0000 | [diff] [blame] | 1903 | /* Bump MRU to take account of the delayed flush */ |
| 1904 | if (obj->fence_reg != I915_FENCE_REG_NONE) { |
| 1905 | struct drm_i915_fence_reg *reg; |
| 1906 | |
| 1907 | reg = &dev_priv->fence_regs[obj->fence_reg]; |
| 1908 | list_move_tail(®->lru_list, |
| 1909 | &dev_priv->mm.fence_list); |
| 1910 | } |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | static void |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1915 | i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj) |
| 1916 | { |
| 1917 | struct drm_device *dev = obj->base.dev; |
| 1918 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 1919 | struct i915_address_space *vm = &dev_priv->gtt.base; |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1920 | |
Chris Wilson | 65ce302 | 2012-07-20 12:41:02 +0100 | [diff] [blame] | 1921 | BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS); |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1922 | BUG_ON(!obj->active); |
Chris Wilson | 65ce302 | 2012-07-20 12:41:02 +0100 | [diff] [blame] | 1923 | |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 1924 | list_move_tail(&obj->mm_list, &vm->inactive_list); |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1925 | |
Chris Wilson | 65ce302 | 2012-07-20 12:41:02 +0100 | [diff] [blame] | 1926 | list_del_init(&obj->ring_list); |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1927 | obj->ring = NULL; |
| 1928 | |
Chris Wilson | 65ce302 | 2012-07-20 12:41:02 +0100 | [diff] [blame] | 1929 | obj->last_read_seqno = 0; |
| 1930 | obj->last_write_seqno = 0; |
| 1931 | obj->base.write_domain = 0; |
| 1932 | |
| 1933 | obj->last_fenced_seqno = 0; |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1934 | obj->fenced_gpu_access = false; |
Chris Wilson | caea747 | 2010-11-12 13:53:37 +0000 | [diff] [blame] | 1935 | |
| 1936 | obj->active = 0; |
| 1937 | drm_gem_object_unreference(&obj->base); |
| 1938 | |
| 1939 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1940 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1941 | |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1942 | static int |
Mika Kuoppala | fca26bb | 2012-12-19 11:13:08 +0200 | [diff] [blame] | 1943 | i915_gem_init_seqno(struct drm_device *dev, u32 seqno) |
Daniel Vetter | 53d227f | 2012-01-25 16:32:49 +0100 | [diff] [blame] | 1944 | { |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1945 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1946 | struct intel_ring_buffer *ring; |
| 1947 | int ret, i, j; |
Daniel Vetter | 53d227f | 2012-01-25 16:32:49 +0100 | [diff] [blame] | 1948 | |
Chris Wilson | 107f27a5 | 2012-12-10 13:56:17 +0200 | [diff] [blame] | 1949 | /* Carefully retire all requests without writing to the rings */ |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1950 | for_each_ring(ring, dev_priv, i) { |
Chris Wilson | 107f27a5 | 2012-12-10 13:56:17 +0200 | [diff] [blame] | 1951 | ret = intel_ring_idle(ring); |
| 1952 | if (ret) |
| 1953 | return ret; |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1954 | } |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1955 | i915_gem_retire_requests(dev); |
Chris Wilson | 107f27a5 | 2012-12-10 13:56:17 +0200 | [diff] [blame] | 1956 | |
| 1957 | /* Finally reset hw state */ |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1958 | for_each_ring(ring, dev_priv, i) { |
Mika Kuoppala | fca26bb | 2012-12-19 11:13:08 +0200 | [diff] [blame] | 1959 | intel_ring_init_seqno(ring, seqno); |
Mika Kuoppala | 498d2ac | 2012-12-04 15:12:04 +0200 | [diff] [blame] | 1960 | |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1961 | for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++) |
| 1962 | ring->sync_seqno[j] = 0; |
| 1963 | } |
| 1964 | |
| 1965 | return 0; |
Daniel Vetter | 53d227f | 2012-01-25 16:32:49 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
Mika Kuoppala | fca26bb | 2012-12-19 11:13:08 +0200 | [diff] [blame] | 1968 | int i915_gem_set_seqno(struct drm_device *dev, u32 seqno) |
| 1969 | { |
| 1970 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1971 | int ret; |
| 1972 | |
| 1973 | if (seqno == 0) |
| 1974 | return -EINVAL; |
| 1975 | |
| 1976 | /* HWS page needs to be set less than what we |
| 1977 | * will inject to ring |
| 1978 | */ |
| 1979 | ret = i915_gem_init_seqno(dev, seqno - 1); |
| 1980 | if (ret) |
| 1981 | return ret; |
| 1982 | |
| 1983 | /* Carefully set the last_seqno value so that wrap |
| 1984 | * detection still works |
| 1985 | */ |
| 1986 | dev_priv->next_seqno = seqno; |
| 1987 | dev_priv->last_seqno = seqno - 1; |
| 1988 | if (dev_priv->last_seqno == 0) |
| 1989 | dev_priv->last_seqno--; |
| 1990 | |
| 1991 | return 0; |
| 1992 | } |
| 1993 | |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1994 | int |
| 1995 | i915_gem_get_seqno(struct drm_device *dev, u32 *seqno) |
Daniel Vetter | 53d227f | 2012-01-25 16:32:49 +0100 | [diff] [blame] | 1996 | { |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1997 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 53d227f | 2012-01-25 16:32:49 +0100 | [diff] [blame] | 1998 | |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 1999 | /* reserve 0 for non-seqno */ |
| 2000 | if (dev_priv->next_seqno == 0) { |
Mika Kuoppala | fca26bb | 2012-12-19 11:13:08 +0200 | [diff] [blame] | 2001 | int ret = i915_gem_init_seqno(dev, 0); |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 2002 | if (ret) |
| 2003 | return ret; |
| 2004 | |
| 2005 | dev_priv->next_seqno = 1; |
| 2006 | } |
| 2007 | |
Mika Kuoppala | f72b343 | 2012-12-10 15:41:48 +0200 | [diff] [blame] | 2008 | *seqno = dev_priv->last_seqno = dev_priv->next_seqno++; |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 2009 | return 0; |
Daniel Vetter | 53d227f | 2012-01-25 16:32:49 +0100 | [diff] [blame] | 2010 | } |
| 2011 | |
Mika Kuoppala | 0025c07 | 2013-06-12 12:35:30 +0300 | [diff] [blame] | 2012 | int __i915_add_request(struct intel_ring_buffer *ring, |
| 2013 | struct drm_file *file, |
Mika Kuoppala | 7d736f4 | 2013-06-12 15:01:39 +0300 | [diff] [blame] | 2014 | struct drm_i915_gem_object *obj, |
Mika Kuoppala | 0025c07 | 2013-06-12 12:35:30 +0300 | [diff] [blame] | 2015 | u32 *out_seqno) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2016 | { |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2017 | drm_i915_private_t *dev_priv = ring->dev->dev_private; |
Chris Wilson | acb868d | 2012-09-26 13:47:30 +0100 | [diff] [blame] | 2018 | struct drm_i915_gem_request *request; |
Mika Kuoppala | 7d736f4 | 2013-06-12 15:01:39 +0300 | [diff] [blame] | 2019 | u32 request_ring_position, request_start; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2020 | int was_empty; |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 2021 | int ret; |
| 2022 | |
Mika Kuoppala | 7d736f4 | 2013-06-12 15:01:39 +0300 | [diff] [blame] | 2023 | request_start = intel_ring_get_tail(ring); |
Daniel Vetter | cc889e0 | 2012-06-13 20:45:19 +0200 | [diff] [blame] | 2024 | /* |
| 2025 | * Emit any outstanding flushes - execbuf can fail to emit the flush |
| 2026 | * after having emitted the batchbuffer command. Hence we need to fix |
| 2027 | * things up similar to emitting the lazy request. The difference here |
| 2028 | * is that the flush _must_ happen before the next request, no matter |
| 2029 | * what. |
| 2030 | */ |
Chris Wilson | a7b9761 | 2012-07-20 12:41:08 +0100 | [diff] [blame] | 2031 | ret = intel_ring_flush_all_caches(ring); |
| 2032 | if (ret) |
| 2033 | return ret; |
Daniel Vetter | cc889e0 | 2012-06-13 20:45:19 +0200 | [diff] [blame] | 2034 | |
Chris Wilson | acb868d | 2012-09-26 13:47:30 +0100 | [diff] [blame] | 2035 | request = kmalloc(sizeof(*request), GFP_KERNEL); |
| 2036 | if (request == NULL) |
| 2037 | return -ENOMEM; |
Daniel Vetter | cc889e0 | 2012-06-13 20:45:19 +0200 | [diff] [blame] | 2038 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2039 | |
Chris Wilson | a71d8d9 | 2012-02-15 11:25:36 +0000 | [diff] [blame] | 2040 | /* Record the position of the start of the request so that |
| 2041 | * should we detect the updated seqno part-way through the |
| 2042 | * GPU processing the request, we never over-estimate the |
| 2043 | * position of the head. |
| 2044 | */ |
| 2045 | request_ring_position = intel_ring_get_tail(ring); |
| 2046 | |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 2047 | ret = ring->add_request(ring); |
Chris Wilson | 3bb73ab | 2012-07-20 12:40:59 +0100 | [diff] [blame] | 2048 | if (ret) { |
| 2049 | kfree(request); |
| 2050 | return ret; |
| 2051 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2052 | |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 2053 | request->seqno = intel_ring_get_seqno(ring); |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2054 | request->ring = ring; |
Mika Kuoppala | 7d736f4 | 2013-06-12 15:01:39 +0300 | [diff] [blame] | 2055 | request->head = request_start; |
Chris Wilson | a71d8d9 | 2012-02-15 11:25:36 +0000 | [diff] [blame] | 2056 | request->tail = request_ring_position; |
Mika Kuoppala | 0e50e96 | 2013-05-02 16:48:08 +0300 | [diff] [blame] | 2057 | request->ctx = ring->last_context; |
Mika Kuoppala | 7d736f4 | 2013-06-12 15:01:39 +0300 | [diff] [blame] | 2058 | request->batch_obj = obj; |
| 2059 | |
| 2060 | /* Whilst this request exists, batch_obj will be on the |
| 2061 | * active_list, and so will hold the active reference. Only when this |
| 2062 | * request is retired will the the batch_obj be moved onto the |
| 2063 | * inactive_list and lose its active reference. Hence we do not need |
| 2064 | * to explicitly hold another reference here. |
| 2065 | */ |
Mika Kuoppala | 0e50e96 | 2013-05-02 16:48:08 +0300 | [diff] [blame] | 2066 | |
| 2067 | if (request->ctx) |
| 2068 | i915_gem_context_reference(request->ctx); |
| 2069 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2070 | request->emitted_jiffies = jiffies; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2071 | was_empty = list_empty(&ring->request_list); |
| 2072 | list_add_tail(&request->list, &ring->request_list); |
Chris Wilson | 3bb73ab | 2012-07-20 12:40:59 +0100 | [diff] [blame] | 2073 | request->file_priv = NULL; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2074 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2075 | if (file) { |
| 2076 | struct drm_i915_file_private *file_priv = file->driver_priv; |
| 2077 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 2078 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 2079 | request->file_priv = file_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 2080 | list_add_tail(&request->client_list, |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 2081 | &file_priv->mm.request_list); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 2082 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 2083 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2084 | |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 2085 | trace_i915_gem_request_add(ring, request->seqno); |
Daniel Vetter | 5391d0c | 2012-01-25 14:03:57 +0100 | [diff] [blame] | 2086 | ring->outstanding_lazy_request = 0; |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2087 | |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 2088 | if (!dev_priv->ums.mm_suspended) { |
Mika Kuoppala | 10cd45b | 2013-07-03 17:22:08 +0300 | [diff] [blame] | 2089 | i915_queue_hangcheck(ring->dev); |
| 2090 | |
Chris Wilson | f047e39 | 2012-07-21 12:31:41 +0100 | [diff] [blame] | 2091 | if (was_empty) { |
Chris Wilson | b3b079d | 2010-09-13 23:44:34 +0100 | [diff] [blame] | 2092 | queue_delayed_work(dev_priv->wq, |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 2093 | &dev_priv->mm.retire_work, |
| 2094 | round_jiffies_up_relative(HZ)); |
Chris Wilson | f047e39 | 2012-07-21 12:31:41 +0100 | [diff] [blame] | 2095 | intel_mark_busy(dev_priv->dev); |
| 2096 | } |
Ben Gamari | f65d942 | 2009-09-14 17:48:44 -0400 | [diff] [blame] | 2097 | } |
Daniel Vetter | cc889e0 | 2012-06-13 20:45:19 +0200 | [diff] [blame] | 2098 | |
Chris Wilson | acb868d | 2012-09-26 13:47:30 +0100 | [diff] [blame] | 2099 | if (out_seqno) |
Chris Wilson | 9d773091 | 2012-11-27 16:22:52 +0000 | [diff] [blame] | 2100 | *out_seqno = request->seqno; |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 2101 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2102 | } |
| 2103 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 2104 | static inline void |
| 2105 | i915_gem_request_remove_from_client(struct drm_i915_gem_request *request) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2106 | { |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 2107 | struct drm_i915_file_private *file_priv = request->file_priv; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2108 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 2109 | if (!file_priv) |
| 2110 | return; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2111 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 2112 | spin_lock(&file_priv->mm.lock); |
Herton Ronaldo Krzesinski | 09bfa51 | 2011-03-17 13:45:12 +0000 | [diff] [blame] | 2113 | if (request->file_priv) { |
| 2114 | list_del(&request->client_list); |
| 2115 | request->file_priv = NULL; |
| 2116 | } |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 2117 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2118 | } |
| 2119 | |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2120 | static bool i915_head_inside_object(u32 acthd, struct drm_i915_gem_object *obj) |
| 2121 | { |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2122 | if (acthd >= i915_gem_obj_ggtt_offset(obj) && |
| 2123 | acthd < i915_gem_obj_ggtt_offset(obj) + obj->base.size) |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2124 | return true; |
| 2125 | |
| 2126 | return false; |
| 2127 | } |
| 2128 | |
| 2129 | static bool i915_head_inside_request(const u32 acthd_unmasked, |
| 2130 | const u32 request_start, |
| 2131 | const u32 request_end) |
| 2132 | { |
| 2133 | const u32 acthd = acthd_unmasked & HEAD_ADDR; |
| 2134 | |
| 2135 | if (request_start < request_end) { |
| 2136 | if (acthd >= request_start && acthd < request_end) |
| 2137 | return true; |
| 2138 | } else if (request_start > request_end) { |
| 2139 | if (acthd >= request_start || acthd < request_end) |
| 2140 | return true; |
| 2141 | } |
| 2142 | |
| 2143 | return false; |
| 2144 | } |
| 2145 | |
| 2146 | static bool i915_request_guilty(struct drm_i915_gem_request *request, |
| 2147 | const u32 acthd, bool *inside) |
| 2148 | { |
| 2149 | /* There is a possibility that unmasked head address |
| 2150 | * pointing inside the ring, matches the batch_obj address range. |
| 2151 | * However this is extremely unlikely. |
| 2152 | */ |
| 2153 | |
| 2154 | if (request->batch_obj) { |
| 2155 | if (i915_head_inside_object(acthd, request->batch_obj)) { |
| 2156 | *inside = true; |
| 2157 | return true; |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | if (i915_head_inside_request(acthd, request->head, request->tail)) { |
| 2162 | *inside = false; |
| 2163 | return true; |
| 2164 | } |
| 2165 | |
| 2166 | return false; |
| 2167 | } |
| 2168 | |
| 2169 | static void i915_set_reset_status(struct intel_ring_buffer *ring, |
| 2170 | struct drm_i915_gem_request *request, |
| 2171 | u32 acthd) |
| 2172 | { |
| 2173 | struct i915_ctx_hang_stats *hs = NULL; |
| 2174 | bool inside, guilty; |
| 2175 | |
| 2176 | /* Innocent until proven guilty */ |
| 2177 | guilty = false; |
| 2178 | |
| 2179 | if (ring->hangcheck.action != wait && |
| 2180 | i915_request_guilty(request, acthd, &inside)) { |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2181 | DRM_ERROR("%s hung %s bo (0x%lx ctx %d) at 0x%x\n", |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2182 | ring->name, |
| 2183 | inside ? "inside" : "flushing", |
| 2184 | request->batch_obj ? |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2185 | i915_gem_obj_ggtt_offset(request->batch_obj) : 0, |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2186 | request->ctx ? request->ctx->id : 0, |
| 2187 | acthd); |
| 2188 | |
| 2189 | guilty = true; |
| 2190 | } |
| 2191 | |
| 2192 | /* If contexts are disabled or this is the default context, use |
| 2193 | * file_priv->reset_state |
| 2194 | */ |
| 2195 | if (request->ctx && request->ctx->id != DEFAULT_CONTEXT_ID) |
| 2196 | hs = &request->ctx->hang_stats; |
| 2197 | else if (request->file_priv) |
| 2198 | hs = &request->file_priv->hang_stats; |
| 2199 | |
| 2200 | if (hs) { |
| 2201 | if (guilty) |
| 2202 | hs->batch_active++; |
| 2203 | else |
| 2204 | hs->batch_pending++; |
| 2205 | } |
| 2206 | } |
| 2207 | |
Mika Kuoppala | 0e50e96 | 2013-05-02 16:48:08 +0300 | [diff] [blame] | 2208 | static void i915_gem_free_request(struct drm_i915_gem_request *request) |
| 2209 | { |
| 2210 | list_del(&request->list); |
| 2211 | i915_gem_request_remove_from_client(request); |
| 2212 | |
| 2213 | if (request->ctx) |
| 2214 | i915_gem_context_unreference(request->ctx); |
| 2215 | |
| 2216 | kfree(request); |
| 2217 | } |
| 2218 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 2219 | static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv, |
| 2220 | struct intel_ring_buffer *ring) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2221 | { |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2222 | u32 completed_seqno; |
| 2223 | u32 acthd; |
| 2224 | |
| 2225 | acthd = intel_ring_get_active_head(ring); |
| 2226 | completed_seqno = ring->get_seqno(ring, false); |
| 2227 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 2228 | while (!list_empty(&ring->request_list)) { |
| 2229 | struct drm_i915_gem_request *request; |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 2230 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 2231 | request = list_first_entry(&ring->request_list, |
| 2232 | struct drm_i915_gem_request, |
| 2233 | list); |
| 2234 | |
Mika Kuoppala | aa60c66 | 2013-06-12 15:13:20 +0300 | [diff] [blame] | 2235 | if (request->seqno > completed_seqno) |
| 2236 | i915_set_reset_status(ring, request, acthd); |
| 2237 | |
Mika Kuoppala | 0e50e96 | 2013-05-02 16:48:08 +0300 | [diff] [blame] | 2238 | i915_gem_free_request(request); |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | while (!list_empty(&ring->active_list)) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2242 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2243 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2244 | obj = list_first_entry(&ring->active_list, |
| 2245 | struct drm_i915_gem_object, |
| 2246 | ring_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2247 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2248 | i915_gem_object_move_to_inactive(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2249 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2250 | } |
| 2251 | |
Chris Wilson | 312817a | 2010-11-22 11:50:11 +0000 | [diff] [blame] | 2252 | static void i915_gem_reset_fences(struct drm_device *dev) |
| 2253 | { |
| 2254 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2255 | int i; |
| 2256 | |
Daniel Vetter | 4b9de73 | 2011-10-09 21:52:02 +0200 | [diff] [blame] | 2257 | for (i = 0; i < dev_priv->num_fence_regs; i++) { |
Chris Wilson | 312817a | 2010-11-22 11:50:11 +0000 | [diff] [blame] | 2258 | struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i]; |
Chris Wilson | 7d2cb39 | 2010-11-27 17:38:29 +0000 | [diff] [blame] | 2259 | |
Chris Wilson | ada726c | 2012-04-17 15:31:32 +0100 | [diff] [blame] | 2260 | if (reg->obj) |
| 2261 | i915_gem_object_fence_lost(reg->obj); |
Chris Wilson | 7d2cb39 | 2010-11-27 17:38:29 +0000 | [diff] [blame] | 2262 | |
Chris Wilson | f9c513e | 2013-03-26 11:29:27 +0000 | [diff] [blame] | 2263 | i915_gem_write_fence(dev, i, NULL); |
| 2264 | |
Chris Wilson | ada726c | 2012-04-17 15:31:32 +0100 | [diff] [blame] | 2265 | reg->pin_count = 0; |
| 2266 | reg->obj = NULL; |
| 2267 | INIT_LIST_HEAD(®->lru_list); |
Chris Wilson | 312817a | 2010-11-22 11:50:11 +0000 | [diff] [blame] | 2268 | } |
Chris Wilson | ada726c | 2012-04-17 15:31:32 +0100 | [diff] [blame] | 2269 | |
| 2270 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
Chris Wilson | 312817a | 2010-11-22 11:50:11 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Chris Wilson | 069efc1 | 2010-09-30 16:53:18 +0100 | [diff] [blame] | 2273 | void i915_gem_reset(struct drm_device *dev) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2274 | { |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 2275 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 2276 | struct i915_address_space *vm = &dev_priv->gtt.base; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2277 | struct drm_i915_gem_object *obj; |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 2278 | struct intel_ring_buffer *ring; |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 2279 | int i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2280 | |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 2281 | for_each_ring(ring, dev_priv, i) |
| 2282 | i915_gem_reset_ring_lists(dev_priv, ring); |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 2283 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 2284 | /* Move everything out of the GPU domains to ensure we do any |
| 2285 | * necessary invalidation upon reuse. |
| 2286 | */ |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 2287 | list_for_each_entry(obj, &vm->inactive_list, mm_list) |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2288 | obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS; |
Chris Wilson | 069efc1 | 2010-09-30 16:53:18 +0100 | [diff] [blame] | 2289 | |
| 2290 | /* The fence registers are invalidated so clear them out */ |
Chris Wilson | 312817a | 2010-11-22 11:50:11 +0000 | [diff] [blame] | 2291 | i915_gem_reset_fences(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | /** |
| 2295 | * This function clears the request list as sequence numbers are passed. |
| 2296 | */ |
Chris Wilson | a71d8d9 | 2012-02-15 11:25:36 +0000 | [diff] [blame] | 2297 | void |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2298 | i915_gem_retire_requests_ring(struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2299 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2300 | uint32_t seqno; |
| 2301 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2302 | if (list_empty(&ring->request_list)) |
Karsten Wiese | 6c0594a | 2009-02-23 15:07:57 +0100 | [diff] [blame] | 2303 | return; |
| 2304 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2305 | WARN_ON(i915_verify_lists(ring->dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2306 | |
Chris Wilson | b2eadbc | 2012-08-09 10:58:30 +0100 | [diff] [blame] | 2307 | seqno = ring->get_seqno(ring, true); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 2308 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2309 | while (!list_empty(&ring->request_list)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2310 | struct drm_i915_gem_request *request; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2311 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2312 | request = list_first_entry(&ring->request_list, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2313 | struct drm_i915_gem_request, |
| 2314 | list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2315 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 2316 | if (!i915_seqno_passed(seqno, request->seqno)) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2317 | break; |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2318 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2319 | trace_i915_gem_request_retire(ring, request->seqno); |
Chris Wilson | a71d8d9 | 2012-02-15 11:25:36 +0000 | [diff] [blame] | 2320 | /* We know the GPU must have read the request to have |
| 2321 | * sent us the seqno + interrupt, so use the position |
| 2322 | * of tail of the request to update the last known position |
| 2323 | * of the GPU head. |
| 2324 | */ |
| 2325 | ring->last_retired_head = request->tail; |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2326 | |
Mika Kuoppala | 0e50e96 | 2013-05-02 16:48:08 +0300 | [diff] [blame] | 2327 | i915_gem_free_request(request); |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | /* Move any buffers on the active list that are no longer referenced |
| 2331 | * by the ringbuffer to the flushing/inactive lists as appropriate. |
| 2332 | */ |
| 2333 | while (!list_empty(&ring->active_list)) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2334 | struct drm_i915_gem_object *obj; |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2335 | |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 2336 | obj = list_first_entry(&ring->active_list, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2337 | struct drm_i915_gem_object, |
| 2338 | ring_list); |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2339 | |
Chris Wilson | 0201f1e | 2012-07-20 12:41:01 +0100 | [diff] [blame] | 2340 | if (!i915_seqno_passed(seqno, obj->last_read_seqno)) |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 2341 | break; |
| 2342 | |
Chris Wilson | 65ce302 | 2012-07-20 12:41:02 +0100 | [diff] [blame] | 2343 | i915_gem_object_move_to_inactive(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2344 | } |
Chris Wilson | 9d34e5d | 2009-09-24 05:26:06 +0100 | [diff] [blame] | 2345 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2346 | if (unlikely(ring->trace_irq_seqno && |
| 2347 | i915_seqno_passed(seqno, ring->trace_irq_seqno))) { |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 2348 | ring->irq_put(ring); |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2349 | ring->trace_irq_seqno = 0; |
Chris Wilson | 9d34e5d | 2009-09-24 05:26:06 +0100 | [diff] [blame] | 2350 | } |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 2351 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2352 | WARN_ON(i915_verify_lists(ring->dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | void |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2356 | i915_gem_retire_requests(struct drm_device *dev) |
| 2357 | { |
| 2358 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 2359 | struct intel_ring_buffer *ring; |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 2360 | int i; |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2361 | |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 2362 | for_each_ring(ring, dev_priv, i) |
| 2363 | i915_gem_retire_requests_ring(ring); |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2364 | } |
| 2365 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 2366 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2367 | i915_gem_retire_work_handler(struct work_struct *work) |
| 2368 | { |
| 2369 | drm_i915_private_t *dev_priv; |
| 2370 | struct drm_device *dev; |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 2371 | struct intel_ring_buffer *ring; |
Chris Wilson | 0a58705 | 2011-01-09 21:05:44 +0000 | [diff] [blame] | 2372 | bool idle; |
| 2373 | int i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2374 | |
| 2375 | dev_priv = container_of(work, drm_i915_private_t, |
| 2376 | mm.retire_work.work); |
| 2377 | dev = dev_priv->dev; |
| 2378 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2379 | /* Come back later if the device is busy... */ |
| 2380 | if (!mutex_trylock(&dev->struct_mutex)) { |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 2381 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, |
| 2382 | round_jiffies_up_relative(HZ)); |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2383 | return; |
| 2384 | } |
| 2385 | |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2386 | i915_gem_retire_requests(dev); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2387 | |
Chris Wilson | 0a58705 | 2011-01-09 21:05:44 +0000 | [diff] [blame] | 2388 | /* Send a periodic flush down the ring so we don't hold onto GEM |
| 2389 | * objects indefinitely. |
| 2390 | */ |
| 2391 | idle = true; |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 2392 | for_each_ring(ring, dev_priv, i) { |
Chris Wilson | 3bb73ab | 2012-07-20 12:40:59 +0100 | [diff] [blame] | 2393 | if (ring->gpu_caches_dirty) |
Mika Kuoppala | 0025c07 | 2013-06-12 12:35:30 +0300 | [diff] [blame] | 2394 | i915_add_request(ring, NULL); |
Chris Wilson | 0a58705 | 2011-01-09 21:05:44 +0000 | [diff] [blame] | 2395 | |
| 2396 | idle &= list_empty(&ring->request_list); |
| 2397 | } |
| 2398 | |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 2399 | if (!dev_priv->ums.mm_suspended && !idle) |
Chris Wilson | bcb4508 | 2012-10-05 17:02:57 +0100 | [diff] [blame] | 2400 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, |
| 2401 | round_jiffies_up_relative(HZ)); |
Chris Wilson | f047e39 | 2012-07-21 12:31:41 +0100 | [diff] [blame] | 2402 | if (idle) |
| 2403 | intel_mark_idle(dev); |
Chris Wilson | 0a58705 | 2011-01-09 21:05:44 +0000 | [diff] [blame] | 2404 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2405 | mutex_unlock(&dev->struct_mutex); |
| 2406 | } |
| 2407 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 2408 | /** |
Daniel Vetter | 30dfebf | 2012-06-01 15:21:23 +0200 | [diff] [blame] | 2409 | * Ensures that an object will eventually get non-busy by flushing any required |
| 2410 | * write domains, emitting any outstanding lazy request and retiring and |
| 2411 | * completed requests. |
| 2412 | */ |
| 2413 | static int |
| 2414 | i915_gem_object_flush_active(struct drm_i915_gem_object *obj) |
| 2415 | { |
| 2416 | int ret; |
| 2417 | |
| 2418 | if (obj->active) { |
Chris Wilson | 0201f1e | 2012-07-20 12:41:01 +0100 | [diff] [blame] | 2419 | ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno); |
Daniel Vetter | 30dfebf | 2012-06-01 15:21:23 +0200 | [diff] [blame] | 2420 | if (ret) |
| 2421 | return ret; |
| 2422 | |
Daniel Vetter | 30dfebf | 2012-06-01 15:21:23 +0200 | [diff] [blame] | 2423 | i915_gem_retire_requests_ring(obj->ring); |
| 2424 | } |
| 2425 | |
| 2426 | return 0; |
| 2427 | } |
| 2428 | |
| 2429 | /** |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2430 | * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT |
| 2431 | * @DRM_IOCTL_ARGS: standard ioctl arguments |
| 2432 | * |
| 2433 | * Returns 0 if successful, else an error is returned with the remaining time in |
| 2434 | * the timeout parameter. |
| 2435 | * -ETIME: object is still busy after timeout |
| 2436 | * -ERESTARTSYS: signal interrupted the wait |
| 2437 | * -ENONENT: object doesn't exist |
| 2438 | * Also possible, but rare: |
| 2439 | * -EAGAIN: GPU wedged |
| 2440 | * -ENOMEM: damn |
| 2441 | * -ENODEV: Internal IRQ fail |
| 2442 | * -E?: The add request failed |
| 2443 | * |
| 2444 | * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any |
| 2445 | * non-zero timeout parameter the wait ioctl will wait for the given number of |
| 2446 | * nanoseconds on an object becoming unbusy. Since the wait itself does so |
| 2447 | * without holding struct_mutex the object may become re-busied before this |
| 2448 | * function completes. A similar but shorter * race condition exists in the busy |
| 2449 | * ioctl |
| 2450 | */ |
| 2451 | int |
| 2452 | i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file) |
| 2453 | { |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 2454 | drm_i915_private_t *dev_priv = dev->dev_private; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2455 | struct drm_i915_gem_wait *args = data; |
| 2456 | struct drm_i915_gem_object *obj; |
| 2457 | struct intel_ring_buffer *ring = NULL; |
Ben Widawsky | eac1f14 | 2012-06-05 15:24:24 -0700 | [diff] [blame] | 2458 | struct timespec timeout_stack, *timeout = NULL; |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 2459 | unsigned reset_counter; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2460 | u32 seqno = 0; |
| 2461 | int ret = 0; |
| 2462 | |
Ben Widawsky | eac1f14 | 2012-06-05 15:24:24 -0700 | [diff] [blame] | 2463 | if (args->timeout_ns >= 0) { |
| 2464 | timeout_stack = ns_to_timespec(args->timeout_ns); |
| 2465 | timeout = &timeout_stack; |
| 2466 | } |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2467 | |
| 2468 | ret = i915_mutex_lock_interruptible(dev); |
| 2469 | if (ret) |
| 2470 | return ret; |
| 2471 | |
| 2472 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle)); |
| 2473 | if (&obj->base == NULL) { |
| 2474 | mutex_unlock(&dev->struct_mutex); |
| 2475 | return -ENOENT; |
| 2476 | } |
| 2477 | |
Daniel Vetter | 30dfebf | 2012-06-01 15:21:23 +0200 | [diff] [blame] | 2478 | /* Need to make sure the object gets inactive eventually. */ |
| 2479 | ret = i915_gem_object_flush_active(obj); |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2480 | if (ret) |
| 2481 | goto out; |
| 2482 | |
| 2483 | if (obj->active) { |
Chris Wilson | 0201f1e | 2012-07-20 12:41:01 +0100 | [diff] [blame] | 2484 | seqno = obj->last_read_seqno; |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2485 | ring = obj->ring; |
| 2486 | } |
| 2487 | |
| 2488 | if (seqno == 0) |
| 2489 | goto out; |
| 2490 | |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2491 | /* Do this after OLR check to make sure we make forward progress polling |
| 2492 | * on this IOCTL with a 0 timeout (like busy ioctl) |
| 2493 | */ |
| 2494 | if (!args->timeout_ns) { |
| 2495 | ret = -ETIME; |
| 2496 | goto out; |
| 2497 | } |
| 2498 | |
| 2499 | drm_gem_object_unreference(&obj->base); |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 2500 | reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter); |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2501 | mutex_unlock(&dev->struct_mutex); |
| 2502 | |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 2503 | ret = __wait_seqno(ring, seqno, reset_counter, true, timeout); |
Chris Wilson | 4f42f4e | 2013-04-26 16:22:46 +0300 | [diff] [blame] | 2504 | if (timeout) |
Ben Widawsky | eac1f14 | 2012-06-05 15:24:24 -0700 | [diff] [blame] | 2505 | args->timeout_ns = timespec_to_ns(timeout); |
Ben Widawsky | 23ba4fd | 2012-05-24 15:03:10 -0700 | [diff] [blame] | 2506 | return ret; |
| 2507 | |
| 2508 | out: |
| 2509 | drm_gem_object_unreference(&obj->base); |
| 2510 | mutex_unlock(&dev->struct_mutex); |
| 2511 | return ret; |
| 2512 | } |
| 2513 | |
| 2514 | /** |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 2515 | * i915_gem_object_sync - sync an object to a ring. |
| 2516 | * |
| 2517 | * @obj: object which may be in use on another ring. |
| 2518 | * @to: ring we wish to use the object on. May be NULL. |
| 2519 | * |
| 2520 | * This code is meant to abstract object synchronization with the GPU. |
| 2521 | * Calling with NULL implies synchronizing the object with the CPU |
| 2522 | * rather than a particular GPU ring. |
| 2523 | * |
| 2524 | * Returns 0 if successful, else propagates up the lower layer error. |
| 2525 | */ |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame] | 2526 | int |
| 2527 | i915_gem_object_sync(struct drm_i915_gem_object *obj, |
| 2528 | struct intel_ring_buffer *to) |
| 2529 | { |
| 2530 | struct intel_ring_buffer *from = obj->ring; |
| 2531 | u32 seqno; |
| 2532 | int ret, idx; |
| 2533 | |
| 2534 | if (from == NULL || to == from) |
| 2535 | return 0; |
| 2536 | |
Ben Widawsky | 5816d64 | 2012-04-11 11:18:19 -0700 | [diff] [blame] | 2537 | if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev)) |
Chris Wilson | 0201f1e | 2012-07-20 12:41:01 +0100 | [diff] [blame] | 2538 | return i915_gem_object_wait_rendering(obj, false); |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame] | 2539 | |
| 2540 | idx = intel_ring_sync_index(from, to); |
| 2541 | |
Chris Wilson | 0201f1e | 2012-07-20 12:41:01 +0100 | [diff] [blame] | 2542 | seqno = obj->last_read_seqno; |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame] | 2543 | if (seqno <= from->sync_seqno[idx]) |
| 2544 | return 0; |
| 2545 | |
Ben Widawsky | b4aca01 | 2012-04-25 20:50:12 -0700 | [diff] [blame] | 2546 | ret = i915_gem_check_olr(obj->ring, seqno); |
| 2547 | if (ret) |
| 2548 | return ret; |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame] | 2549 | |
Ben Widawsky | 1500f7e | 2012-04-11 11:18:21 -0700 | [diff] [blame] | 2550 | ret = to->sync_to(to, from, seqno); |
Ben Widawsky | e3a5a22 | 2012-04-11 11:18:20 -0700 | [diff] [blame] | 2551 | if (!ret) |
Mika Kuoppala | 7b01e26 | 2012-11-28 17:18:45 +0200 | [diff] [blame] | 2552 | /* We use last_read_seqno because sync_to() |
| 2553 | * might have just caused seqno wrap under |
| 2554 | * the radar. |
| 2555 | */ |
| 2556 | from->sync_seqno[idx] = obj->last_read_seqno; |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame] | 2557 | |
Ben Widawsky | e3a5a22 | 2012-04-11 11:18:20 -0700 | [diff] [blame] | 2558 | return ret; |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame] | 2559 | } |
| 2560 | |
Chris Wilson | b5ffc9b | 2011-04-13 22:06:03 +0100 | [diff] [blame] | 2561 | static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj) |
| 2562 | { |
| 2563 | u32 old_write_domain, old_read_domains; |
| 2564 | |
Chris Wilson | b5ffc9b | 2011-04-13 22:06:03 +0100 | [diff] [blame] | 2565 | /* Force a pagefault for domain tracking on next user access */ |
| 2566 | i915_gem_release_mmap(obj); |
| 2567 | |
Keith Packard | b97c3d9 | 2011-06-24 21:02:59 -0700 | [diff] [blame] | 2568 | if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) |
| 2569 | return; |
| 2570 | |
Chris Wilson | 97c809fd | 2012-10-09 19:24:38 +0100 | [diff] [blame] | 2571 | /* Wait for any direct GTT access to complete */ |
| 2572 | mb(); |
| 2573 | |
Chris Wilson | b5ffc9b | 2011-04-13 22:06:03 +0100 | [diff] [blame] | 2574 | old_read_domains = obj->base.read_domains; |
| 2575 | old_write_domain = obj->base.write_domain; |
| 2576 | |
| 2577 | obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT; |
| 2578 | obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT; |
| 2579 | |
| 2580 | trace_i915_gem_object_change_domain(obj, |
| 2581 | old_read_domains, |
| 2582 | old_write_domain); |
| 2583 | } |
| 2584 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2585 | /** |
| 2586 | * Unbinds an object from the GTT aperture. |
| 2587 | */ |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2588 | int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2589 | i915_gem_object_unbind(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2590 | { |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 2591 | drm_i915_private_t *dev_priv = obj->base.dev->dev_private; |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 2592 | struct i915_vma *vma; |
Chris Wilson | 43e28f0 | 2013-01-08 10:53:09 +0000 | [diff] [blame] | 2593 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2594 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2595 | if (!i915_gem_obj_ggtt_bound(obj)) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2596 | return 0; |
| 2597 | |
Chris Wilson | 31d8d65 | 2012-05-24 19:11:20 +0100 | [diff] [blame] | 2598 | if (obj->pin_count) |
| 2599 | return -EBUSY; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2600 | |
Chris Wilson | c4670ad | 2012-08-20 10:23:27 +0100 | [diff] [blame] | 2601 | BUG_ON(obj->pages == NULL); |
| 2602 | |
Chris Wilson | a8198ee | 2011-04-13 22:04:09 +0100 | [diff] [blame] | 2603 | ret = i915_gem_object_finish_gpu(obj); |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 2604 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2605 | return ret; |
Chris Wilson | 8dc1775 | 2010-07-23 23:18:51 +0100 | [diff] [blame] | 2606 | /* Continue on if we fail due to EIO, the GPU is hung so we |
| 2607 | * should be safe and we need to cleanup or else we might |
| 2608 | * cause memory corruption through use-after-free. |
| 2609 | */ |
Chris Wilson | a8198ee | 2011-04-13 22:04:09 +0100 | [diff] [blame] | 2610 | |
Chris Wilson | b5ffc9b | 2011-04-13 22:06:03 +0100 | [diff] [blame] | 2611 | i915_gem_object_finish_gtt(obj); |
Chris Wilson | a8198ee | 2011-04-13 22:04:09 +0100 | [diff] [blame] | 2612 | |
Daniel Vetter | 96b47b6 | 2009-12-15 17:50:00 +0100 | [diff] [blame] | 2613 | /* release the fence reg _after_ flushing */ |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2614 | ret = i915_gem_object_put_fence(obj); |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 2615 | if (ret) |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2616 | return ret; |
Daniel Vetter | 96b47b6 | 2009-12-15 17:50:00 +0100 | [diff] [blame] | 2617 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 2618 | trace_i915_gem_object_unbind(obj); |
| 2619 | |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 2620 | if (obj->has_global_gtt_mapping) |
| 2621 | i915_gem_gtt_unbind_object(obj); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 2622 | if (obj->has_aliasing_ppgtt_mapping) { |
| 2623 | i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj); |
| 2624 | obj->has_aliasing_ppgtt_mapping = 0; |
| 2625 | } |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 2626 | i915_gem_gtt_finish_object(obj); |
Ben Widawsky | 401c29f | 2013-05-31 11:28:47 -0700 | [diff] [blame] | 2627 | i915_gem_object_unpin_pages(obj); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 2628 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 2629 | list_del(&obj->mm_list); |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 2630 | /* Avoid an unnecessary call to unbind on rebind. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2631 | obj->map_and_fenceable = true; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2632 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 2633 | vma = __i915_gem_obj_to_vma(obj); |
| 2634 | list_del(&vma->vma_link); |
| 2635 | drm_mm_remove_node(&vma->node); |
| 2636 | i915_gem_vma_destroy(vma); |
| 2637 | |
| 2638 | /* Since the unbound list is global, only move to that list if |
| 2639 | * no more VMAs exist. |
| 2640 | * NB: Until we have real VMAs there will only ever be one */ |
| 2641 | WARN_ON(!list_empty(&obj->vma_list)); |
| 2642 | if (list_empty(&obj->vma_list)) |
| 2643 | list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2644 | |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 2645 | return 0; |
Chris Wilson | 54cf91d | 2010-11-25 18:00:26 +0000 | [diff] [blame] | 2646 | } |
| 2647 | |
Ben Widawsky | b2da9fe | 2012-04-26 16:02:58 -0700 | [diff] [blame] | 2648 | int i915_gpu_idle(struct drm_device *dev) |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2649 | { |
| 2650 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 2651 | struct intel_ring_buffer *ring; |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 2652 | int ret, i; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2653 | |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2654 | /* Flush everything onto the inactive list. */ |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 2655 | for_each_ring(ring, dev_priv, i) { |
Ben Widawsky | b6c7488 | 2012-08-14 14:35:14 -0700 | [diff] [blame] | 2656 | ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID); |
| 2657 | if (ret) |
| 2658 | return ret; |
| 2659 | |
Chris Wilson | 3e96050 | 2012-11-27 16:22:54 +0000 | [diff] [blame] | 2660 | ret = intel_ring_idle(ring); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 2661 | if (ret) |
| 2662 | return ret; |
| 2663 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2664 | |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 2665 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2666 | } |
| 2667 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2668 | static void i965_write_fence_reg(struct drm_device *dev, int reg, |
| 2669 | struct drm_i915_gem_object *obj) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2670 | { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2671 | drm_i915_private_t *dev_priv = dev->dev_private; |
Imre Deak | 56c844e | 2013-01-07 21:47:34 +0200 | [diff] [blame] | 2672 | int fence_reg; |
| 2673 | int fence_pitch_shift; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2674 | uint64_t val; |
| 2675 | |
Imre Deak | 56c844e | 2013-01-07 21:47:34 +0200 | [diff] [blame] | 2676 | if (INTEL_INFO(dev)->gen >= 6) { |
| 2677 | fence_reg = FENCE_REG_SANDYBRIDGE_0; |
| 2678 | fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT; |
| 2679 | } else { |
| 2680 | fence_reg = FENCE_REG_965_0; |
| 2681 | fence_pitch_shift = I965_FENCE_PITCH_SHIFT; |
| 2682 | } |
| 2683 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2684 | if (obj) { |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2685 | u32 size = i915_gem_obj_ggtt_size(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2686 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2687 | val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) & |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2688 | 0xfffff000) << 32; |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2689 | val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000; |
Imre Deak | 56c844e | 2013-01-07 21:47:34 +0200 | [diff] [blame] | 2690 | val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift; |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2691 | if (obj->tiling_mode == I915_TILING_Y) |
| 2692 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; |
| 2693 | val |= I965_FENCE_REG_VALID; |
| 2694 | } else |
| 2695 | val = 0; |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2696 | |
Imre Deak | 56c844e | 2013-01-07 21:47:34 +0200 | [diff] [blame] | 2697 | fence_reg += reg * 8; |
| 2698 | I915_WRITE64(fence_reg, val); |
| 2699 | POSTING_READ(fence_reg); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2700 | } |
| 2701 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2702 | static void i915_write_fence_reg(struct drm_device *dev, int reg, |
| 2703 | struct drm_i915_gem_object *obj) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2704 | { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2705 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2706 | u32 val; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2707 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2708 | if (obj) { |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2709 | u32 size = i915_gem_obj_ggtt_size(obj); |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2710 | int pitch_val; |
| 2711 | int tile_width; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2712 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2713 | WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) || |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2714 | (size & -size) != size || |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2715 | (i915_gem_obj_ggtt_offset(obj) & (size - 1)), |
| 2716 | "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n", |
| 2717 | i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size); |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2718 | |
| 2719 | if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)) |
| 2720 | tile_width = 128; |
| 2721 | else |
| 2722 | tile_width = 512; |
| 2723 | |
| 2724 | /* Note: pitch better be a power of two tile widths */ |
| 2725 | pitch_val = obj->stride / tile_width; |
| 2726 | pitch_val = ffs(pitch_val) - 1; |
| 2727 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2728 | val = i915_gem_obj_ggtt_offset(obj); |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2729 | if (obj->tiling_mode == I915_TILING_Y) |
| 2730 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; |
| 2731 | val |= I915_FENCE_SIZE_BITS(size); |
| 2732 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; |
| 2733 | val |= I830_FENCE_REG_VALID; |
| 2734 | } else |
| 2735 | val = 0; |
| 2736 | |
| 2737 | if (reg < 8) |
| 2738 | reg = FENCE_REG_830_0 + reg * 4; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2739 | else |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2740 | reg = FENCE_REG_945_8 + (reg - 8) * 4; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2741 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2742 | I915_WRITE(reg, val); |
| 2743 | POSTING_READ(reg); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2744 | } |
| 2745 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2746 | static void i830_write_fence_reg(struct drm_device *dev, int reg, |
| 2747 | struct drm_i915_gem_object *obj) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2748 | { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2749 | drm_i915_private_t *dev_priv = dev->dev_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2750 | uint32_t val; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2751 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2752 | if (obj) { |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2753 | u32 size = i915_gem_obj_ggtt_size(obj); |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2754 | uint32_t pitch_val; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2755 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2756 | WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) || |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2757 | (size & -size) != size || |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2758 | (i915_gem_obj_ggtt_offset(obj) & (size - 1)), |
| 2759 | "object 0x%08lx not 512K or pot-size 0x%08x aligned\n", |
| 2760 | i915_gem_obj_ggtt_offset(obj), size); |
Eric Anholt | e76a16d | 2009-05-26 17:44:56 -0700 | [diff] [blame] | 2761 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2762 | pitch_val = obj->stride / 128; |
| 2763 | pitch_val = ffs(pitch_val) - 1; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2764 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 2765 | val = i915_gem_obj_ggtt_offset(obj); |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2766 | if (obj->tiling_mode == I915_TILING_Y) |
| 2767 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; |
| 2768 | val |= I830_FENCE_SIZE_BITS(size); |
| 2769 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; |
| 2770 | val |= I830_FENCE_REG_VALID; |
| 2771 | } else |
| 2772 | val = 0; |
Daniel Vetter | c664278 | 2010-11-12 13:46:18 +0000 | [diff] [blame] | 2773 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2774 | I915_WRITE(FENCE_REG_830_0 + reg * 4, val); |
| 2775 | POSTING_READ(FENCE_REG_830_0 + reg * 4); |
| 2776 | } |
| 2777 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 2778 | inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj) |
| 2779 | { |
| 2780 | return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT; |
| 2781 | } |
| 2782 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2783 | static void i915_gem_write_fence(struct drm_device *dev, int reg, |
| 2784 | struct drm_i915_gem_object *obj) |
| 2785 | { |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 2786 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2787 | |
| 2788 | /* Ensure that all CPU reads are completed before installing a fence |
| 2789 | * and all writes before removing the fence. |
| 2790 | */ |
| 2791 | if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj)) |
| 2792 | mb(); |
| 2793 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2794 | switch (INTEL_INFO(dev)->gen) { |
| 2795 | case 7: |
Imre Deak | 56c844e | 2013-01-07 21:47:34 +0200 | [diff] [blame] | 2796 | case 6: |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2797 | case 5: |
| 2798 | case 4: i965_write_fence_reg(dev, reg, obj); break; |
| 2799 | case 3: i915_write_fence_reg(dev, reg, obj); break; |
| 2800 | case 2: i830_write_fence_reg(dev, reg, obj); break; |
Ben Widawsky | 7dbf9d6 | 2012-12-18 10:31:22 -0800 | [diff] [blame] | 2801 | default: BUG(); |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 2802 | } |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 2803 | |
| 2804 | /* And similarly be paranoid that no direct access to this region |
| 2805 | * is reordered to before the fence is installed. |
| 2806 | */ |
| 2807 | if (i915_gem_object_needs_mb(obj)) |
| 2808 | mb(); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2809 | } |
| 2810 | |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 2811 | static inline int fence_number(struct drm_i915_private *dev_priv, |
| 2812 | struct drm_i915_fence_reg *fence) |
| 2813 | { |
| 2814 | return fence - dev_priv->fence_regs; |
| 2815 | } |
| 2816 | |
Chris Wilson | 2dc8aae | 2013-05-22 17:08:06 +0100 | [diff] [blame] | 2817 | struct write_fence { |
| 2818 | struct drm_device *dev; |
| 2819 | struct drm_i915_gem_object *obj; |
| 2820 | int fence; |
| 2821 | }; |
| 2822 | |
Chris Wilson | 25ff119 | 2013-04-04 21:31:03 +0100 | [diff] [blame] | 2823 | static void i915_gem_write_fence__ipi(void *data) |
| 2824 | { |
Chris Wilson | 2dc8aae | 2013-05-22 17:08:06 +0100 | [diff] [blame] | 2825 | struct write_fence *args = data; |
| 2826 | |
| 2827 | /* Required for SNB+ with LLC */ |
Chris Wilson | 25ff119 | 2013-04-04 21:31:03 +0100 | [diff] [blame] | 2828 | wbinvd(); |
Chris Wilson | 2dc8aae | 2013-05-22 17:08:06 +0100 | [diff] [blame] | 2829 | |
| 2830 | /* Required for VLV */ |
| 2831 | i915_gem_write_fence(args->dev, args->fence, args->obj); |
Chris Wilson | 25ff119 | 2013-04-04 21:31:03 +0100 | [diff] [blame] | 2832 | } |
| 2833 | |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 2834 | static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj, |
| 2835 | struct drm_i915_fence_reg *fence, |
| 2836 | bool enable) |
| 2837 | { |
Chris Wilson | 2dc8aae | 2013-05-22 17:08:06 +0100 | [diff] [blame] | 2838 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
| 2839 | struct write_fence args = { |
| 2840 | .dev = obj->base.dev, |
| 2841 | .fence = fence_number(dev_priv, fence), |
| 2842 | .obj = enable ? obj : NULL, |
| 2843 | }; |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 2844 | |
Chris Wilson | 25ff119 | 2013-04-04 21:31:03 +0100 | [diff] [blame] | 2845 | /* In order to fully serialize access to the fenced region and |
| 2846 | * the update to the fence register we need to take extreme |
| 2847 | * measures on SNB+. In theory, the write to the fence register |
| 2848 | * flushes all memory transactions before, and coupled with the |
| 2849 | * mb() placed around the register write we serialise all memory |
| 2850 | * operations with respect to the changes in the tiler. Yet, on |
| 2851 | * SNB+ we need to take a step further and emit an explicit wbinvd() |
| 2852 | * on each processor in order to manually flush all memory |
| 2853 | * transactions before updating the fence register. |
Chris Wilson | 2dc8aae | 2013-05-22 17:08:06 +0100 | [diff] [blame] | 2854 | * |
| 2855 | * However, Valleyview complicates matter. There the wbinvd is |
| 2856 | * insufficient and unlike SNB/IVB requires the serialising |
| 2857 | * register write. (Note that that register write by itself is |
| 2858 | * conversely not sufficient for SNB+.) To compromise, we do both. |
Chris Wilson | 25ff119 | 2013-04-04 21:31:03 +0100 | [diff] [blame] | 2859 | */ |
Chris Wilson | 2dc8aae | 2013-05-22 17:08:06 +0100 | [diff] [blame] | 2860 | if (INTEL_INFO(args.dev)->gen >= 6) |
| 2861 | on_each_cpu(i915_gem_write_fence__ipi, &args, 1); |
| 2862 | else |
| 2863 | i915_gem_write_fence(args.dev, args.fence, args.obj); |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 2864 | |
| 2865 | if (enable) { |
Chris Wilson | 2dc8aae | 2013-05-22 17:08:06 +0100 | [diff] [blame] | 2866 | obj->fence_reg = args.fence; |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 2867 | fence->obj = obj; |
| 2868 | list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list); |
| 2869 | } else { |
| 2870 | obj->fence_reg = I915_FENCE_REG_NONE; |
| 2871 | fence->obj = NULL; |
| 2872 | list_del_init(&fence->lru_list); |
| 2873 | } |
| 2874 | } |
| 2875 | |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2876 | static int |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 2877 | i915_gem_object_wait_fence(struct drm_i915_gem_object *obj) |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2878 | { |
Chris Wilson | 1c293ea | 2012-04-17 15:31:27 +0100 | [diff] [blame] | 2879 | if (obj->last_fenced_seqno) { |
Chris Wilson | 86d5bc3 | 2012-07-20 12:41:04 +0100 | [diff] [blame] | 2880 | int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno); |
Chris Wilson | 1899184 | 2012-04-17 15:31:29 +0100 | [diff] [blame] | 2881 | if (ret) |
| 2882 | return ret; |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2883 | |
| 2884 | obj->last_fenced_seqno = 0; |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2885 | } |
| 2886 | |
Chris Wilson | 86d5bc3 | 2012-07-20 12:41:04 +0100 | [diff] [blame] | 2887 | obj->fenced_gpu_access = false; |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2888 | return 0; |
| 2889 | } |
| 2890 | |
| 2891 | int |
| 2892 | i915_gem_object_put_fence(struct drm_i915_gem_object *obj) |
| 2893 | { |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 2894 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
Chris Wilson | f9c513e | 2013-03-26 11:29:27 +0000 | [diff] [blame] | 2895 | struct drm_i915_fence_reg *fence; |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2896 | int ret; |
| 2897 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 2898 | ret = i915_gem_object_wait_fence(obj); |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2899 | if (ret) |
| 2900 | return ret; |
| 2901 | |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 2902 | if (obj->fence_reg == I915_FENCE_REG_NONE) |
| 2903 | return 0; |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 2904 | |
Chris Wilson | f9c513e | 2013-03-26 11:29:27 +0000 | [diff] [blame] | 2905 | fence = &dev_priv->fence_regs[obj->fence_reg]; |
| 2906 | |
Chris Wilson | 6105080 | 2012-04-17 15:31:31 +0100 | [diff] [blame] | 2907 | i915_gem_object_fence_lost(obj); |
Chris Wilson | f9c513e | 2013-03-26 11:29:27 +0000 | [diff] [blame] | 2908 | i915_gem_object_update_fence(obj, fence, false); |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2909 | |
| 2910 | return 0; |
| 2911 | } |
| 2912 | |
| 2913 | static struct drm_i915_fence_reg * |
Chris Wilson | a360bb1 | 2012-04-17 15:31:25 +0100 | [diff] [blame] | 2914 | i915_find_fence_reg(struct drm_device *dev) |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2915 | { |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2916 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 8fe301a | 2012-04-17 15:31:28 +0100 | [diff] [blame] | 2917 | struct drm_i915_fence_reg *reg, *avail; |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2918 | int i; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2919 | |
| 2920 | /* First try to find a free reg */ |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2921 | avail = NULL; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2922 | for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) { |
| 2923 | reg = &dev_priv->fence_regs[i]; |
| 2924 | if (!reg->obj) |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2925 | return reg; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2926 | |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 2927 | if (!reg->pin_count) |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2928 | avail = reg; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2929 | } |
| 2930 | |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2931 | if (avail == NULL) |
| 2932 | return NULL; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2933 | |
| 2934 | /* None available, try to steal one or wait for a user to finish */ |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2935 | list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) { |
Chris Wilson | 1690e1e | 2011-12-14 13:57:08 +0100 | [diff] [blame] | 2936 | if (reg->pin_count) |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2937 | continue; |
| 2938 | |
Chris Wilson | 8fe301a | 2012-04-17 15:31:28 +0100 | [diff] [blame] | 2939 | return reg; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2940 | } |
| 2941 | |
Chris Wilson | 8fe301a | 2012-04-17 15:31:28 +0100 | [diff] [blame] | 2942 | return NULL; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2943 | } |
| 2944 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2945 | /** |
Chris Wilson | 9a5a53b | 2012-03-22 15:10:00 +0000 | [diff] [blame] | 2946 | * i915_gem_object_get_fence - set up fencing for an object |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2947 | * @obj: object to map through a fence reg |
| 2948 | * |
| 2949 | * When mapping objects through the GTT, userspace wants to be able to write |
| 2950 | * to them without having to worry about swizzling if the object is tiled. |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2951 | * This function walks the fence regs looking for a free one for @obj, |
| 2952 | * stealing one if it can't find any. |
| 2953 | * |
| 2954 | * It then sets up the reg based on the object's properties: address, pitch |
| 2955 | * and tiling format. |
Chris Wilson | 9a5a53b | 2012-03-22 15:10:00 +0000 | [diff] [blame] | 2956 | * |
| 2957 | * For an untiled surface, this removes any existing fence. |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2958 | */ |
Chris Wilson | 8c4b8c3 | 2009-06-17 22:08:52 +0100 | [diff] [blame] | 2959 | int |
Chris Wilson | 06d9813 | 2012-04-17 15:31:24 +0100 | [diff] [blame] | 2960 | i915_gem_object_get_fence(struct drm_i915_gem_object *obj) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2961 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2962 | struct drm_device *dev = obj->base.dev; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2963 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 1441574 | 2012-04-17 15:31:33 +0100 | [diff] [blame] | 2964 | bool enable = obj->tiling_mode != I915_TILING_NONE; |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2965 | struct drm_i915_fence_reg *reg; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2966 | int ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2967 | |
Chris Wilson | 1441574 | 2012-04-17 15:31:33 +0100 | [diff] [blame] | 2968 | /* Have we updated the tiling parameters upon the object and so |
| 2969 | * will need to serialise the write to the associated fence register? |
| 2970 | */ |
Chris Wilson | 5d82e3e | 2012-04-21 16:23:23 +0100 | [diff] [blame] | 2971 | if (obj->fence_dirty) { |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 2972 | ret = i915_gem_object_wait_fence(obj); |
Chris Wilson | 1441574 | 2012-04-17 15:31:33 +0100 | [diff] [blame] | 2973 | if (ret) |
| 2974 | return ret; |
| 2975 | } |
Chris Wilson | 9a5a53b | 2012-03-22 15:10:00 +0000 | [diff] [blame] | 2976 | |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2977 | /* Just update our place in the LRU if our fence is getting reused. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 2978 | if (obj->fence_reg != I915_FENCE_REG_NONE) { |
| 2979 | reg = &dev_priv->fence_regs[obj->fence_reg]; |
Chris Wilson | 5d82e3e | 2012-04-21 16:23:23 +0100 | [diff] [blame] | 2980 | if (!obj->fence_dirty) { |
Chris Wilson | 1441574 | 2012-04-17 15:31:33 +0100 | [diff] [blame] | 2981 | list_move_tail(®->lru_list, |
| 2982 | &dev_priv->mm.fence_list); |
| 2983 | return 0; |
| 2984 | } |
| 2985 | } else if (enable) { |
| 2986 | reg = i915_find_fence_reg(dev); |
| 2987 | if (reg == NULL) |
| 2988 | return -EDEADLK; |
Chris Wilson | d9e86c0 | 2010-11-10 16:40:20 +0000 | [diff] [blame] | 2989 | |
Chris Wilson | 1441574 | 2012-04-17 15:31:33 +0100 | [diff] [blame] | 2990 | if (reg->obj) { |
| 2991 | struct drm_i915_gem_object *old = reg->obj; |
| 2992 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 2993 | ret = i915_gem_object_wait_fence(old); |
Chris Wilson | 29c5a58 | 2011-03-17 15:23:22 +0000 | [diff] [blame] | 2994 | if (ret) |
| 2995 | return ret; |
| 2996 | |
Chris Wilson | 1441574 | 2012-04-17 15:31:33 +0100 | [diff] [blame] | 2997 | i915_gem_object_fence_lost(old); |
Chris Wilson | 29c5a58 | 2011-03-17 15:23:22 +0000 | [diff] [blame] | 2998 | } |
Chris Wilson | 1441574 | 2012-04-17 15:31:33 +0100 | [diff] [blame] | 2999 | } else |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 3000 | return 0; |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 3001 | |
Chris Wilson | 1441574 | 2012-04-17 15:31:33 +0100 | [diff] [blame] | 3002 | i915_gem_object_update_fence(obj, reg, enable); |
Chris Wilson | 5d82e3e | 2012-04-21 16:23:23 +0100 | [diff] [blame] | 3003 | obj->fence_dirty = false; |
Chris Wilson | 1441574 | 2012-04-17 15:31:33 +0100 | [diff] [blame] | 3004 | |
Chris Wilson | 9ce079e | 2012-04-17 15:31:30 +0100 | [diff] [blame] | 3005 | return 0; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 3006 | } |
| 3007 | |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3008 | static bool i915_gem_valid_gtt_space(struct drm_device *dev, |
| 3009 | struct drm_mm_node *gtt_space, |
| 3010 | unsigned long cache_level) |
| 3011 | { |
| 3012 | struct drm_mm_node *other; |
| 3013 | |
| 3014 | /* On non-LLC machines we have to be careful when putting differing |
| 3015 | * types of snoopable memory together to avoid the prefetcher |
Damien Lespiau | 4239ca7 | 2012-12-03 16:26:16 +0000 | [diff] [blame] | 3016 | * crossing memory domains and dying. |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3017 | */ |
| 3018 | if (HAS_LLC(dev)) |
| 3019 | return true; |
| 3020 | |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 3021 | if (!drm_mm_node_allocated(gtt_space)) |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3022 | return true; |
| 3023 | |
| 3024 | if (list_empty(>t_space->node_list)) |
| 3025 | return true; |
| 3026 | |
| 3027 | other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list); |
| 3028 | if (other->allocated && !other->hole_follows && other->color != cache_level) |
| 3029 | return false; |
| 3030 | |
| 3031 | other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list); |
| 3032 | if (other->allocated && !gtt_space->hole_follows && other->color != cache_level) |
| 3033 | return false; |
| 3034 | |
| 3035 | return true; |
| 3036 | } |
| 3037 | |
| 3038 | static void i915_gem_verify_gtt(struct drm_device *dev) |
| 3039 | { |
| 3040 | #if WATCH_GTT |
| 3041 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 3042 | struct drm_i915_gem_object *obj; |
| 3043 | int err = 0; |
| 3044 | |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 3045 | list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) { |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3046 | if (obj->gtt_space == NULL) { |
| 3047 | printk(KERN_ERR "object found on GTT list with no space reserved\n"); |
| 3048 | err++; |
| 3049 | continue; |
| 3050 | } |
| 3051 | |
| 3052 | if (obj->cache_level != obj->gtt_space->color) { |
| 3053 | printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n", |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3054 | i915_gem_obj_ggtt_offset(obj), |
| 3055 | i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj), |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3056 | obj->cache_level, |
| 3057 | obj->gtt_space->color); |
| 3058 | err++; |
| 3059 | continue; |
| 3060 | } |
| 3061 | |
| 3062 | if (!i915_gem_valid_gtt_space(dev, |
| 3063 | obj->gtt_space, |
| 3064 | obj->cache_level)) { |
| 3065 | printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n", |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3066 | i915_gem_obj_ggtt_offset(obj), |
| 3067 | i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj), |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3068 | obj->cache_level); |
| 3069 | err++; |
| 3070 | continue; |
| 3071 | } |
| 3072 | } |
| 3073 | |
| 3074 | WARN_ON(err); |
| 3075 | #endif |
| 3076 | } |
| 3077 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 3078 | /** |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3079 | * Finds free space in the GTT aperture and binds the object there. |
| 3080 | */ |
| 3081 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3082 | i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj, |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 3083 | unsigned alignment, |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 3084 | bool map_and_fenceable, |
| 3085 | bool nonblocking) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3086 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3087 | struct drm_device *dev = obj->base.dev; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3088 | drm_i915_private_t *dev_priv = dev->dev_private; |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 3089 | struct i915_address_space *vm = &dev_priv->gtt.base; |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 3090 | u32 size, fence_size, fence_alignment, unfenced_alignment; |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 3091 | bool mappable, fenceable; |
Ben Widawsky | 0a9ae0d | 2013-05-25 12:26:35 -0700 | [diff] [blame] | 3092 | size_t gtt_max = map_and_fenceable ? |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 3093 | dev_priv->gtt.mappable_end : dev_priv->gtt.base.total; |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3094 | struct i915_vma *vma; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 3095 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3096 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3097 | if (WARN_ON(!list_empty(&obj->vma_list))) |
| 3098 | return -EBUSY; |
| 3099 | |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 3100 | fence_size = i915_gem_get_gtt_size(dev, |
| 3101 | obj->base.size, |
| 3102 | obj->tiling_mode); |
| 3103 | fence_alignment = i915_gem_get_gtt_alignment(dev, |
| 3104 | obj->base.size, |
Imre Deak | d865110c | 2013-01-07 21:47:33 +0200 | [diff] [blame] | 3105 | obj->tiling_mode, true); |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 3106 | unfenced_alignment = |
Imre Deak | d865110c | 2013-01-07 21:47:33 +0200 | [diff] [blame] | 3107 | i915_gem_get_gtt_alignment(dev, |
Chris Wilson | e28f871 | 2011-07-18 13:11:49 -0700 | [diff] [blame] | 3108 | obj->base.size, |
Imre Deak | d865110c | 2013-01-07 21:47:33 +0200 | [diff] [blame] | 3109 | obj->tiling_mode, false); |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 3110 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3111 | if (alignment == 0) |
Daniel Vetter | 5e78330 | 2010-11-14 22:32:36 +0100 | [diff] [blame] | 3112 | alignment = map_and_fenceable ? fence_alignment : |
| 3113 | unfenced_alignment; |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 3114 | if (map_and_fenceable && alignment & (fence_alignment - 1)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3115 | DRM_ERROR("Invalid object alignment requested %u\n", alignment); |
| 3116 | return -EINVAL; |
| 3117 | } |
| 3118 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3119 | size = map_and_fenceable ? fence_size : obj->base.size; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 3120 | |
Chris Wilson | 654fc60 | 2010-05-27 13:18:21 +0100 | [diff] [blame] | 3121 | /* If the object is bigger than the entire aperture, reject it early |
| 3122 | * before evicting everything in a vain attempt to find space. |
| 3123 | */ |
Ben Widawsky | 0a9ae0d | 2013-05-25 12:26:35 -0700 | [diff] [blame] | 3124 | if (obj->base.size > gtt_max) { |
Jani Nikula | 3765f30 | 2013-06-07 16:03:50 +0300 | [diff] [blame] | 3125 | DRM_ERROR("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n", |
Chris Wilson | a36689c | 2013-05-21 16:58:49 +0100 | [diff] [blame] | 3126 | obj->base.size, |
| 3127 | map_and_fenceable ? "mappable" : "total", |
Ben Widawsky | 0a9ae0d | 2013-05-25 12:26:35 -0700 | [diff] [blame] | 3128 | gtt_max); |
Chris Wilson | 654fc60 | 2010-05-27 13:18:21 +0100 | [diff] [blame] | 3129 | return -E2BIG; |
| 3130 | } |
| 3131 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3132 | ret = i915_gem_object_get_pages(obj); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 3133 | if (ret) |
| 3134 | return ret; |
| 3135 | |
Chris Wilson | fbdda6f | 2012-11-20 10:45:16 +0000 | [diff] [blame] | 3136 | i915_gem_object_pin_pages(obj); |
| 3137 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3138 | vma = i915_gem_vma_create(obj, &dev_priv->gtt.base); |
Dan Carpenter | db473b3 | 2013-07-19 08:45:46 +0300 | [diff] [blame] | 3139 | if (IS_ERR(vma)) { |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3140 | i915_gem_object_unpin_pages(obj); |
Dan Carpenter | db473b3 | 2013-07-19 08:45:46 +0300 | [diff] [blame] | 3141 | return PTR_ERR(vma); |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3142 | } |
| 3143 | |
Ben Widawsky | 0a9ae0d | 2013-05-25 12:26:35 -0700 | [diff] [blame] | 3144 | search_free: |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 3145 | ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm, |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3146 | &vma->node, |
Ben Widawsky | 0a9ae0d | 2013-05-25 12:26:35 -0700 | [diff] [blame] | 3147 | size, alignment, |
| 3148 | obj->cache_level, 0, gtt_max); |
Chris Wilson | dc9dd7a | 2012-12-07 20:37:07 +0000 | [diff] [blame] | 3149 | if (ret) { |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 3150 | ret = i915_gem_evict_something(dev, size, alignment, |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3151 | obj->cache_level, |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 3152 | map_and_fenceable, |
| 3153 | nonblocking); |
Chris Wilson | dc9dd7a | 2012-12-07 20:37:07 +0000 | [diff] [blame] | 3154 | if (ret == 0) |
| 3155 | goto search_free; |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 3156 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3157 | goto err_out; |
Chris Wilson | dc9dd7a | 2012-12-07 20:37:07 +0000 | [diff] [blame] | 3158 | } |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3159 | if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node, |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 3160 | obj->cache_level))) { |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3161 | ret = -EINVAL; |
| 3162 | goto err_out; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3163 | } |
| 3164 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 3165 | ret = i915_gem_gtt_prepare_object(obj); |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3166 | if (ret) |
| 3167 | goto err_out; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3168 | |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 3169 | list_move_tail(&obj->global_list, &dev_priv->mm.bound_list); |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 3170 | list_add_tail(&obj->mm_list, &vm->inactive_list); |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3171 | list_add(&vma->vma_link, &obj->vma_list); |
Chris Wilson | bf1a109 | 2010-08-07 11:01:20 +0100 | [diff] [blame] | 3172 | |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 3173 | fenceable = |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 3174 | i915_gem_obj_ggtt_size(obj) == fence_size && |
| 3175 | (i915_gem_obj_ggtt_offset(obj) & (fence_alignment - 1)) == 0; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 3176 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3177 | mappable = i915_gem_obj_ggtt_offset(obj) + obj->base.size <= |
| 3178 | dev_priv->gtt.mappable_end; |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 3179 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3180 | obj->map_and_fenceable = mappable && fenceable; |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 3181 | |
Chris Wilson | db53a30 | 2011-02-03 11:57:46 +0000 | [diff] [blame] | 3182 | trace_i915_gem_object_bind(obj, map_and_fenceable); |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3183 | i915_gem_verify_gtt(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3184 | return 0; |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3185 | |
| 3186 | err_out: |
Dan Carpenter | 6286ef9 | 2013-07-19 08:46:27 +0300 | [diff] [blame] | 3187 | drm_mm_remove_node(&vma->node); |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3188 | i915_gem_vma_destroy(vma); |
| 3189 | i915_gem_object_unpin_pages(obj); |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3190 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3194 | i915_gem_clflush_object(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3195 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3196 | /* If we don't have a page list set up, then we're not pinned |
| 3197 | * to GPU, and we can ignore the cache flush because it'll happen |
| 3198 | * again at bind time. |
| 3199 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3200 | if (obj->pages == NULL) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3201 | return; |
| 3202 | |
Imre Deak | 769ce46 | 2013-02-13 21:56:05 +0200 | [diff] [blame] | 3203 | /* |
| 3204 | * Stolen memory is always coherent with the GPU as it is explicitly |
| 3205 | * marked as wc by the system, or the system is cache-coherent. |
| 3206 | */ |
| 3207 | if (obj->stolen) |
| 3208 | return; |
| 3209 | |
Chris Wilson | 9c23f7f | 2011-03-29 16:59:52 -0700 | [diff] [blame] | 3210 | /* If the GPU is snooping the contents of the CPU cache, |
| 3211 | * we do not need to manually clear the CPU cache lines. However, |
| 3212 | * the caches are only snooped when the render cache is |
| 3213 | * flushed/invalidated. As we always have to emit invalidations |
| 3214 | * and flushes when moving into and out of the RENDER domain, correct |
| 3215 | * snooping behaviour occurs naturally as the result of our domain |
| 3216 | * tracking. |
| 3217 | */ |
| 3218 | if (obj->cache_level != I915_CACHE_NONE) |
| 3219 | return; |
| 3220 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3221 | trace_i915_gem_object_clflush(obj); |
Eric Anholt | cfa16a0 | 2009-05-26 18:46:16 -0700 | [diff] [blame] | 3222 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 3223 | drm_clflush_sg(obj->pages); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | /** Flushes the GTT write domain for the object if it's dirty. */ |
| 3227 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3228 | 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] | 3229 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3230 | uint32_t old_write_domain; |
| 3231 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3232 | if (obj->base.write_domain != I915_GEM_DOMAIN_GTT) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3233 | return; |
| 3234 | |
Chris Wilson | 63256ec | 2011-01-04 18:42:07 +0000 | [diff] [blame] | 3235 | /* No actual flushing is required for the GTT write domain. Writes |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3236 | * to it immediately go to main memory as far as we know, so there's |
| 3237 | * no chipset flush. It also doesn't land in render cache. |
Chris Wilson | 63256ec | 2011-01-04 18:42:07 +0000 | [diff] [blame] | 3238 | * |
| 3239 | * However, we do have to enforce the order so that all writes through |
| 3240 | * the GTT land before any writes to the device, such as updates to |
| 3241 | * the GATT itself. |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3242 | */ |
Chris Wilson | 63256ec | 2011-01-04 18:42:07 +0000 | [diff] [blame] | 3243 | wmb(); |
| 3244 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3245 | old_write_domain = obj->base.write_domain; |
| 3246 | obj->base.write_domain = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3247 | |
| 3248 | trace_i915_gem_object_change_domain(obj, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3249 | obj->base.read_domains, |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3250 | old_write_domain); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | /** Flushes the CPU write domain for the object if it's dirty. */ |
| 3254 | static void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3255 | 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] | 3256 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3257 | uint32_t old_write_domain; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3258 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3259 | if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3260 | return; |
| 3261 | |
| 3262 | i915_gem_clflush_object(obj); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 3263 | i915_gem_chipset_flush(obj->base.dev); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3264 | old_write_domain = obj->base.write_domain; |
| 3265 | obj->base.write_domain = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3266 | |
| 3267 | trace_i915_gem_object_change_domain(obj, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3268 | obj->base.read_domains, |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3269 | old_write_domain); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3270 | } |
| 3271 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3272 | /** |
| 3273 | * Moves a single object to the GTT read, and possibly write domain. |
| 3274 | * |
| 3275 | * This function returns when the move is complete, including waiting on |
| 3276 | * flushes to occur. |
| 3277 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3278 | int |
Chris Wilson | 2021746 | 2010-11-23 15:26:33 +0000 | [diff] [blame] | 3279 | 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] | 3280 | { |
Chris Wilson | 8325a09 | 2012-04-24 15:52:35 +0100 | [diff] [blame] | 3281 | drm_i915_private_t *dev_priv = obj->base.dev->dev_private; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3282 | uint32_t old_write_domain, old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3283 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3284 | |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 3285 | /* Not valid to be called on unbound objects. */ |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3286 | if (!i915_gem_obj_ggtt_bound(obj)) |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 3287 | return -EINVAL; |
| 3288 | |
Chris Wilson | 8d7e3de | 2011-02-07 15:23:02 +0000 | [diff] [blame] | 3289 | if (obj->base.write_domain == I915_GEM_DOMAIN_GTT) |
| 3290 | return 0; |
| 3291 | |
Chris Wilson | 0201f1e | 2012-07-20 12:41:01 +0100 | [diff] [blame] | 3292 | ret = i915_gem_object_wait_rendering(obj, !write); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3293 | if (ret) |
| 3294 | return ret; |
| 3295 | |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 3296 | i915_gem_object_flush_cpu_write_domain(obj); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3297 | |
Chris Wilson | d0a5778 | 2012-10-09 19:24:37 +0100 | [diff] [blame] | 3298 | /* Serialise direct access to this object with the barriers for |
| 3299 | * coherent writes from the GPU, by effectively invalidating the |
| 3300 | * GTT domain upon first access. |
| 3301 | */ |
| 3302 | if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0) |
| 3303 | mb(); |
| 3304 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3305 | old_write_domain = obj->base.write_domain; |
| 3306 | old_read_domains = obj->base.read_domains; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3307 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3308 | /* It should now be out of any other write domains, and we can update |
| 3309 | * the domain values for our changes. |
| 3310 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3311 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 3312 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3313 | if (write) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3314 | obj->base.read_domains = I915_GEM_DOMAIN_GTT; |
| 3315 | obj->base.write_domain = I915_GEM_DOMAIN_GTT; |
| 3316 | obj->dirty = 1; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3317 | } |
| 3318 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3319 | trace_i915_gem_object_change_domain(obj, |
| 3320 | old_read_domains, |
| 3321 | old_write_domain); |
| 3322 | |
Chris Wilson | 8325a09 | 2012-04-24 15:52:35 +0100 | [diff] [blame] | 3323 | /* And bump the LRU for this access */ |
| 3324 | if (i915_gem_object_is_inactive(obj)) |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 3325 | list_move_tail(&obj->mm_list, |
| 3326 | &dev_priv->gtt.base.inactive_list); |
Chris Wilson | 8325a09 | 2012-04-24 15:52:35 +0100 | [diff] [blame] | 3327 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3328 | return 0; |
| 3329 | } |
| 3330 | |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3331 | int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj, |
| 3332 | enum i915_cache_level cache_level) |
| 3333 | { |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 3334 | struct drm_device *dev = obj->base.dev; |
| 3335 | drm_i915_private_t *dev_priv = dev->dev_private; |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3336 | struct i915_vma *vma = __i915_gem_obj_to_vma(obj); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3337 | int ret; |
| 3338 | |
| 3339 | if (obj->cache_level == cache_level) |
| 3340 | return 0; |
| 3341 | |
| 3342 | if (obj->pin_count) { |
| 3343 | DRM_DEBUG("can not change the cache level of pinned objects\n"); |
| 3344 | return -EBUSY; |
| 3345 | } |
| 3346 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3347 | if (vma && !i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) { |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3348 | ret = i915_gem_object_unbind(obj); |
| 3349 | if (ret) |
| 3350 | return ret; |
| 3351 | } |
| 3352 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3353 | if (i915_gem_obj_ggtt_bound(obj)) { |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3354 | ret = i915_gem_object_finish_gpu(obj); |
| 3355 | if (ret) |
| 3356 | return ret; |
| 3357 | |
| 3358 | i915_gem_object_finish_gtt(obj); |
| 3359 | |
| 3360 | /* Before SandyBridge, you could not use tiling or fence |
| 3361 | * registers with snooped memory, so relinquish any fences |
| 3362 | * currently pointing to our region in the aperture. |
| 3363 | */ |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3364 | if (INTEL_INFO(dev)->gen < 6) { |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3365 | ret = i915_gem_object_put_fence(obj); |
| 3366 | if (ret) |
| 3367 | return ret; |
| 3368 | } |
| 3369 | |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 3370 | if (obj->has_global_gtt_mapping) |
| 3371 | i915_gem_gtt_bind_object(obj, cache_level); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 3372 | if (obj->has_aliasing_ppgtt_mapping) |
| 3373 | i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt, |
| 3374 | obj, cache_level); |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3375 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3376 | i915_gem_obj_ggtt_set_color(obj, cache_level); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3377 | } |
| 3378 | |
| 3379 | if (cache_level == I915_CACHE_NONE) { |
| 3380 | u32 old_read_domains, old_write_domain; |
| 3381 | |
| 3382 | /* If we're coming from LLC cached, then we haven't |
| 3383 | * actually been tracking whether the data is in the |
| 3384 | * CPU cache or not, since we only allow one bit set |
| 3385 | * in obj->write_domain and have been skipping the clflushes. |
| 3386 | * Just set it to the CPU cache for now. |
| 3387 | */ |
| 3388 | WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU); |
| 3389 | WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU); |
| 3390 | |
| 3391 | old_read_domains = obj->base.read_domains; |
| 3392 | old_write_domain = obj->base.write_domain; |
| 3393 | |
| 3394 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 3395 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
| 3396 | |
| 3397 | trace_i915_gem_object_change_domain(obj, |
| 3398 | old_read_domains, |
| 3399 | old_write_domain); |
| 3400 | } |
| 3401 | |
| 3402 | obj->cache_level = cache_level; |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 3403 | i915_gem_verify_gtt(dev); |
Chris Wilson | e4ffd17 | 2011-04-04 09:44:39 +0100 | [diff] [blame] | 3404 | return 0; |
| 3405 | } |
| 3406 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3407 | int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data, |
| 3408 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3409 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3410 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3411 | struct drm_i915_gem_object *obj; |
| 3412 | int ret; |
| 3413 | |
| 3414 | ret = i915_mutex_lock_interruptible(dev); |
| 3415 | if (ret) |
| 3416 | return ret; |
| 3417 | |
| 3418 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
| 3419 | if (&obj->base == NULL) { |
| 3420 | ret = -ENOENT; |
| 3421 | goto unlock; |
| 3422 | } |
| 3423 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3424 | args->caching = obj->cache_level != I915_CACHE_NONE; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3425 | |
| 3426 | drm_gem_object_unreference(&obj->base); |
| 3427 | unlock: |
| 3428 | mutex_unlock(&dev->struct_mutex); |
| 3429 | return ret; |
| 3430 | } |
| 3431 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3432 | int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data, |
| 3433 | struct drm_file *file) |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3434 | { |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3435 | struct drm_i915_gem_caching *args = data; |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3436 | struct drm_i915_gem_object *obj; |
| 3437 | enum i915_cache_level level; |
| 3438 | int ret; |
| 3439 | |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3440 | switch (args->caching) { |
| 3441 | case I915_CACHING_NONE: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3442 | level = I915_CACHE_NONE; |
| 3443 | break; |
Ben Widawsky | 199adf4 | 2012-09-21 17:01:20 -0700 | [diff] [blame] | 3444 | case I915_CACHING_CACHED: |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3445 | level = I915_CACHE_LLC; |
| 3446 | break; |
| 3447 | default: |
| 3448 | return -EINVAL; |
| 3449 | } |
| 3450 | |
Ben Widawsky | 3bc2913 | 2012-09-26 16:15:20 -0700 | [diff] [blame] | 3451 | ret = i915_mutex_lock_interruptible(dev); |
| 3452 | if (ret) |
| 3453 | return ret; |
| 3454 | |
Chris Wilson | e6994ae | 2012-07-10 10:27:08 +0100 | [diff] [blame] | 3455 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
| 3456 | if (&obj->base == NULL) { |
| 3457 | ret = -ENOENT; |
| 3458 | goto unlock; |
| 3459 | } |
| 3460 | |
| 3461 | ret = i915_gem_object_set_cache_level(obj, level); |
| 3462 | |
| 3463 | drm_gem_object_unreference(&obj->base); |
| 3464 | unlock: |
| 3465 | mutex_unlock(&dev->struct_mutex); |
| 3466 | return ret; |
| 3467 | } |
| 3468 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3469 | /* |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3470 | * Prepare buffer for display plane (scanout, cursors, etc). |
| 3471 | * Can be called from an uninterruptible phase (modesetting) and allows |
| 3472 | * any flushes to be pipelined (for pageflips). |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3473 | */ |
| 3474 | int |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3475 | i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj, |
| 3476 | u32 alignment, |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3477 | struct intel_ring_buffer *pipelined) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3478 | { |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3479 | u32 old_read_domains, old_write_domain; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3480 | int ret; |
| 3481 | |
Chris Wilson | 0be7328 | 2010-12-06 14:36:27 +0000 | [diff] [blame] | 3482 | if (pipelined != obj->ring) { |
Ben Widawsky | 2911a35 | 2012-04-05 14:47:36 -0700 | [diff] [blame] | 3483 | ret = i915_gem_object_sync(obj, pipelined); |
| 3484 | if (ret) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3485 | return ret; |
| 3486 | } |
| 3487 | |
Eric Anholt | a7ef064 | 2011-03-29 16:59:54 -0700 | [diff] [blame] | 3488 | /* The display engine is not coherent with the LLC cache on gen6. As |
| 3489 | * a result, we make sure that the pinning that is about to occur is |
| 3490 | * done with uncached PTEs. This is lowest common denominator for all |
| 3491 | * chipsets. |
| 3492 | * |
| 3493 | * However for gen6+, we could do better by using the GFDT bit instead |
| 3494 | * of uncaching, which would allow us to flush all the LLC-cached data |
| 3495 | * with that bit in the PTE to main memory with just one PIPE_CONTROL. |
| 3496 | */ |
| 3497 | ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE); |
| 3498 | if (ret) |
| 3499 | return ret; |
| 3500 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3501 | /* As the user may map the buffer once pinned in the display plane |
| 3502 | * (e.g. libkms for the bootup splash), we have to ensure that we |
| 3503 | * always use map_and_fenceable for all scanout buffers. |
| 3504 | */ |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 3505 | ret = i915_gem_object_pin(obj, alignment, true, false); |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3506 | if (ret) |
| 3507 | return ret; |
| 3508 | |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3509 | i915_gem_object_flush_cpu_write_domain(obj); |
| 3510 | |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3511 | old_write_domain = obj->base.write_domain; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3512 | old_read_domains = obj->base.read_domains; |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3513 | |
| 3514 | /* It should now be out of any other write domains, and we can update |
| 3515 | * the domain values for our changes. |
| 3516 | */ |
Chris Wilson | e5f1d96 | 2012-07-20 12:41:00 +0100 | [diff] [blame] | 3517 | obj->base.write_domain = 0; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3518 | obj->base.read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3519 | |
| 3520 | trace_i915_gem_object_change_domain(obj, |
| 3521 | old_read_domains, |
Chris Wilson | 2da3b9b | 2011-04-14 09:41:17 +0100 | [diff] [blame] | 3522 | old_write_domain); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3523 | |
| 3524 | return 0; |
| 3525 | } |
| 3526 | |
Chris Wilson | 8534551 | 2010-11-13 09:49:11 +0000 | [diff] [blame] | 3527 | int |
Chris Wilson | a8198ee | 2011-04-13 22:04:09 +0100 | [diff] [blame] | 3528 | i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj) |
Chris Wilson | 8534551 | 2010-11-13 09:49:11 +0000 | [diff] [blame] | 3529 | { |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3530 | int ret; |
| 3531 | |
Chris Wilson | a8198ee | 2011-04-13 22:04:09 +0100 | [diff] [blame] | 3532 | if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0) |
Chris Wilson | 8534551 | 2010-11-13 09:49:11 +0000 | [diff] [blame] | 3533 | return 0; |
| 3534 | |
Chris Wilson | 0201f1e | 2012-07-20 12:41:01 +0100 | [diff] [blame] | 3535 | ret = i915_gem_object_wait_rendering(obj, false); |
Chris Wilson | c501ae7 | 2011-12-14 13:57:23 +0100 | [diff] [blame] | 3536 | if (ret) |
| 3537 | return ret; |
| 3538 | |
Chris Wilson | a8198ee | 2011-04-13 22:04:09 +0100 | [diff] [blame] | 3539 | /* Ensure that we invalidate the GPU's caches and TLBs. */ |
| 3540 | obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS; |
Chris Wilson | c501ae7 | 2011-12-14 13:57:23 +0100 | [diff] [blame] | 3541 | return 0; |
Chris Wilson | 8534551 | 2010-11-13 09:49:11 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3544 | /** |
| 3545 | * Moves a single object to the CPU read, and possibly write domain. |
| 3546 | * |
| 3547 | * This function returns when the move is complete, including waiting on |
| 3548 | * flushes to occur. |
| 3549 | */ |
Chris Wilson | dabdfe0 | 2012-03-26 10:10:27 +0200 | [diff] [blame] | 3550 | int |
Chris Wilson | 919926a | 2010-11-12 13:42:53 +0000 | [diff] [blame] | 3551 | 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] | 3552 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3553 | uint32_t old_write_domain, old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3554 | int ret; |
| 3555 | |
Chris Wilson | 8d7e3de | 2011-02-07 15:23:02 +0000 | [diff] [blame] | 3556 | if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) |
| 3557 | return 0; |
| 3558 | |
Chris Wilson | 0201f1e | 2012-07-20 12:41:01 +0100 | [diff] [blame] | 3559 | ret = i915_gem_object_wait_rendering(obj, !write); |
Chris Wilson | 8824178 | 2011-01-07 17:09:48 +0000 | [diff] [blame] | 3560 | if (ret) |
| 3561 | return ret; |
| 3562 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3563 | i915_gem_object_flush_gtt_write_domain(obj); |
| 3564 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3565 | old_write_domain = obj->base.write_domain; |
| 3566 | old_read_domains = obj->base.read_domains; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3567 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3568 | /* Flush the CPU cache if it's still invalid. */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3569 | if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3570 | i915_gem_clflush_object(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3571 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3572 | obj->base.read_domains |= I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3573 | } |
| 3574 | |
| 3575 | /* It should now be out of any other write domains, and we can update |
| 3576 | * the domain values for our changes. |
| 3577 | */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3578 | BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3579 | |
| 3580 | /* If we're writing through the CPU, then the GPU read domains will |
| 3581 | * need to be invalidated at next use. |
| 3582 | */ |
| 3583 | if (write) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3584 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 3585 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3586 | } |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3587 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3588 | trace_i915_gem_object_change_domain(obj, |
| 3589 | old_read_domains, |
| 3590 | old_write_domain); |
| 3591 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3592 | return 0; |
| 3593 | } |
| 3594 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3595 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3596 | * emitted over 20 msec ago. |
| 3597 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3598 | * Note that if we were to use the current jiffies each time around the loop, |
| 3599 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3600 | * render a frame was over 20ms. |
| 3601 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3602 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3603 | * relatively low latency when blocking on a particular request to finish. |
| 3604 | */ |
| 3605 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3606 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3607 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3608 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 3609 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3610 | unsigned long recent_enough = jiffies - msecs_to_jiffies(20); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3611 | struct drm_i915_gem_request *request; |
| 3612 | struct intel_ring_buffer *ring = NULL; |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 3613 | unsigned reset_counter; |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3614 | u32 seqno = 0; |
| 3615 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3616 | |
Daniel Vetter | 308887a | 2012-11-14 17:14:06 +0100 | [diff] [blame] | 3617 | ret = i915_gem_wait_for_error(&dev_priv->gpu_error); |
| 3618 | if (ret) |
| 3619 | return ret; |
| 3620 | |
| 3621 | ret = i915_gem_check_wedge(&dev_priv->gpu_error, false); |
| 3622 | if (ret) |
| 3623 | return ret; |
Chris Wilson | e110e8d | 2011-01-26 15:39:14 +0000 | [diff] [blame] | 3624 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3625 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3626 | list_for_each_entry(request, &file_priv->mm.request_list, client_list) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3627 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3628 | break; |
| 3629 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3630 | ring = request->ring; |
| 3631 | seqno = request->seqno; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3632 | } |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 3633 | reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3634 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3635 | |
| 3636 | if (seqno == 0) |
| 3637 | return 0; |
| 3638 | |
Daniel Vetter | f69061b | 2012-12-06 09:01:42 +0100 | [diff] [blame] | 3639 | ret = __wait_seqno(ring, seqno, reset_counter, true, NULL); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3640 | if (ret == 0) |
| 3641 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3642 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3643 | return ret; |
| 3644 | } |
| 3645 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3646 | int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3647 | i915_gem_object_pin(struct drm_i915_gem_object *obj, |
| 3648 | uint32_t alignment, |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 3649 | bool map_and_fenceable, |
| 3650 | bool nonblocking) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3651 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3652 | int ret; |
| 3653 | |
Chris Wilson | 7e81a42 | 2012-09-15 09:41:57 +0100 | [diff] [blame] | 3654 | if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT)) |
| 3655 | return -EBUSY; |
Chris Wilson | ac0c6b5 | 2010-05-27 13:18:18 +0100 | [diff] [blame] | 3656 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3657 | if (i915_gem_obj_ggtt_bound(obj)) { |
| 3658 | if ((alignment && i915_gem_obj_ggtt_offset(obj) & (alignment - 1)) || |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3659 | (map_and_fenceable && !obj->map_and_fenceable)) { |
| 3660 | WARN(obj->pin_count, |
Chris Wilson | ae7d49d | 2010-08-04 12:37:41 +0100 | [diff] [blame] | 3661 | "bo is already pinned with incorrect alignment:" |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3662 | " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d," |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 3663 | " obj->map_and_fenceable=%d\n", |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3664 | i915_gem_obj_ggtt_offset(obj), alignment, |
Daniel Vetter | 75e9e91 | 2010-11-04 17:11:09 +0100 | [diff] [blame] | 3665 | map_and_fenceable, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3666 | obj->map_and_fenceable); |
Chris Wilson | ac0c6b5 | 2010-05-27 13:18:18 +0100 | [diff] [blame] | 3667 | ret = i915_gem_object_unbind(obj); |
| 3668 | if (ret) |
| 3669 | return ret; |
| 3670 | } |
| 3671 | } |
| 3672 | |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3673 | if (!i915_gem_obj_ggtt_bound(obj)) { |
Chris Wilson | 8742267 | 2012-11-21 13:04:03 +0000 | [diff] [blame] | 3674 | struct drm_i915_private *dev_priv = obj->base.dev->dev_private; |
| 3675 | |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 3676 | ret = i915_gem_object_bind_to_gtt(obj, alignment, |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 3677 | map_and_fenceable, |
| 3678 | nonblocking); |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 3679 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3680 | return ret; |
Chris Wilson | 8742267 | 2012-11-21 13:04:03 +0000 | [diff] [blame] | 3681 | |
| 3682 | if (!dev_priv->mm.aliasing_ppgtt) |
| 3683 | i915_gem_gtt_bind_object(obj, obj->cache_level); |
Chris Wilson | 22c344e | 2009-02-11 14:26:45 +0000 | [diff] [blame] | 3684 | } |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3685 | |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 3686 | if (!obj->has_global_gtt_mapping && map_and_fenceable) |
| 3687 | i915_gem_gtt_bind_object(obj, obj->cache_level); |
| 3688 | |
Chris Wilson | 1b50247 | 2012-04-24 15:47:30 +0100 | [diff] [blame] | 3689 | obj->pin_count++; |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 3690 | obj->pin_mappable |= map_and_fenceable; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3691 | |
| 3692 | return 0; |
| 3693 | } |
| 3694 | |
| 3695 | void |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3696 | i915_gem_object_unpin(struct drm_i915_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3697 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3698 | BUG_ON(obj->pin_count == 0); |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3699 | BUG_ON(!i915_gem_obj_ggtt_bound(obj)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3700 | |
Chris Wilson | 1b50247 | 2012-04-24 15:47:30 +0100 | [diff] [blame] | 3701 | if (--obj->pin_count == 0) |
Chris Wilson | 6299f99 | 2010-11-24 12:23:44 +0000 | [diff] [blame] | 3702 | obj->pin_mappable = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3703 | } |
| 3704 | |
| 3705 | int |
| 3706 | i915_gem_pin_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3707 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3708 | { |
| 3709 | struct drm_i915_gem_pin *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3710 | struct drm_i915_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3711 | int ret; |
| 3712 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3713 | ret = i915_mutex_lock_interruptible(dev); |
| 3714 | if (ret) |
| 3715 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3716 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3717 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 3718 | if (&obj->base == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3719 | ret = -ENOENT; |
| 3720 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3721 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3722 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3723 | if (obj->madv != I915_MADV_WILLNEED) { |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 3724 | DRM_ERROR("Attempting to pin a purgeable buffer\n"); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3725 | ret = -EINVAL; |
| 3726 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3727 | } |
| 3728 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3729 | if (obj->pin_filp != NULL && obj->pin_filp != file) { |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3730 | DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n", |
| 3731 | args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3732 | ret = -EINVAL; |
| 3733 | goto out; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3734 | } |
| 3735 | |
Chris Wilson | 93be878 | 2013-01-02 10:31:22 +0000 | [diff] [blame] | 3736 | if (obj->user_pin_count == 0) { |
Chris Wilson | 86a1ee2 | 2012-08-11 15:41:04 +0100 | [diff] [blame] | 3737 | ret = i915_gem_object_pin(obj, args->alignment, true, false); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3738 | if (ret) |
| 3739 | goto out; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3740 | } |
| 3741 | |
Chris Wilson | 93be878 | 2013-01-02 10:31:22 +0000 | [diff] [blame] | 3742 | obj->user_pin_count++; |
| 3743 | obj->pin_filp = file; |
| 3744 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3745 | /* XXX - flush the CPU caches for pinned objects |
| 3746 | * as the X server doesn't manage domains yet |
| 3747 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3748 | i915_gem_object_flush_cpu_write_domain(obj); |
Ben Widawsky | f343c5f | 2013-07-05 14:41:04 -0700 | [diff] [blame] | 3749 | args->offset = i915_gem_obj_ggtt_offset(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3750 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3751 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3752 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3753 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3754 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | int |
| 3758 | i915_gem_unpin_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3759 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3760 | { |
| 3761 | struct drm_i915_gem_pin *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3762 | struct drm_i915_gem_object *obj; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 3763 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3764 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3765 | ret = i915_mutex_lock_interruptible(dev); |
| 3766 | if (ret) |
| 3767 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3768 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3769 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 3770 | if (&obj->base == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3771 | ret = -ENOENT; |
| 3772 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3773 | } |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 3774 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3775 | if (obj->pin_filp != file) { |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3776 | DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n", |
| 3777 | args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3778 | ret = -EINVAL; |
| 3779 | goto out; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3780 | } |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3781 | obj->user_pin_count--; |
| 3782 | if (obj->user_pin_count == 0) { |
| 3783 | obj->pin_filp = NULL; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3784 | i915_gem_object_unpin(obj); |
| 3785 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3786 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3787 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3788 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3789 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3790 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3791 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | int |
| 3795 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3796 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3797 | { |
| 3798 | struct drm_i915_gem_busy *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3799 | struct drm_i915_gem_object *obj; |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 3800 | int ret; |
| 3801 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3802 | ret = i915_mutex_lock_interruptible(dev); |
| 3803 | if (ret) |
| 3804 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3805 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3806 | obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 3807 | if (&obj->base == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3808 | ret = -ENOENT; |
| 3809 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3810 | } |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3811 | |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 3812 | /* Count all active objects as busy, even if they are currently not used |
| 3813 | * by the gpu. Users of this interface expect objects to eventually |
| 3814 | * become non-busy without any further actions, therefore emit any |
| 3815 | * necessary flushes here. |
Eric Anholt | c4de0a5 | 2008-12-14 19:05:04 -0800 | [diff] [blame] | 3816 | */ |
Daniel Vetter | 30dfebf | 2012-06-01 15:21:23 +0200 | [diff] [blame] | 3817 | ret = i915_gem_object_flush_active(obj); |
| 3818 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3819 | args->busy = obj->active; |
Chris Wilson | e9808ed | 2012-07-04 12:25:08 +0100 | [diff] [blame] | 3820 | if (obj->ring) { |
| 3821 | BUILD_BUG_ON(I915_NUM_RINGS > 16); |
| 3822 | args->busy |= intel_ring_flag(obj->ring) << 16; |
| 3823 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3824 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3825 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3826 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3827 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3828 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | int |
| 3832 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 3833 | struct drm_file *file_priv) |
| 3834 | { |
Akshay Joshi | 0206e35 | 2011-08-16 15:34:10 -0400 | [diff] [blame] | 3835 | return i915_gem_ring_throttle(dev, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3836 | } |
| 3837 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3838 | int |
| 3839 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 3840 | struct drm_file *file_priv) |
| 3841 | { |
| 3842 | struct drm_i915_gem_madvise *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3843 | struct drm_i915_gem_object *obj; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 3844 | int ret; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3845 | |
| 3846 | switch (args->madv) { |
| 3847 | case I915_MADV_DONTNEED: |
| 3848 | case I915_MADV_WILLNEED: |
| 3849 | break; |
| 3850 | default: |
| 3851 | return -EINVAL; |
| 3852 | } |
| 3853 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3854 | ret = i915_mutex_lock_interruptible(dev); |
| 3855 | if (ret) |
| 3856 | return ret; |
| 3857 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3858 | obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle)); |
Chris Wilson | c872522 | 2011-02-19 11:31:06 +0000 | [diff] [blame] | 3859 | if (&obj->base == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3860 | ret = -ENOENT; |
| 3861 | goto unlock; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3862 | } |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3863 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3864 | if (obj->pin_count) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3865 | ret = -EINVAL; |
| 3866 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3867 | } |
| 3868 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3869 | if (obj->madv != __I915_MADV_PURGED) |
| 3870 | obj->madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3871 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 3872 | /* if the object is no longer attached, discard its backing storage */ |
| 3873 | if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL) |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 3874 | i915_gem_object_truncate(obj); |
| 3875 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3876 | args->retained = obj->madv != __I915_MADV_PURGED; |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 3877 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3878 | out: |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3879 | drm_gem_object_unreference(&obj->base); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3880 | unlock: |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3881 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 3882 | return ret; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 3883 | } |
| 3884 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3885 | void i915_gem_object_init(struct drm_i915_gem_object *obj, |
| 3886 | const struct drm_i915_gem_object_ops *ops) |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3887 | { |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3888 | INIT_LIST_HEAD(&obj->mm_list); |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 3889 | INIT_LIST_HEAD(&obj->global_list); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3890 | INIT_LIST_HEAD(&obj->ring_list); |
| 3891 | INIT_LIST_HEAD(&obj->exec_list); |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 3892 | INIT_LIST_HEAD(&obj->vma_list); |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3893 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3894 | obj->ops = ops; |
| 3895 | |
Chris Wilson | 0327d6b | 2012-08-11 15:41:06 +0100 | [diff] [blame] | 3896 | obj->fence_reg = I915_FENCE_REG_NONE; |
| 3897 | obj->madv = I915_MADV_WILLNEED; |
| 3898 | /* Avoid an unnecessary call to unbind on the first bind. */ |
| 3899 | obj->map_and_fenceable = true; |
| 3900 | |
| 3901 | i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size); |
| 3902 | } |
| 3903 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3904 | static const struct drm_i915_gem_object_ops i915_gem_object_ops = { |
| 3905 | .get_pages = i915_gem_object_get_pages_gtt, |
| 3906 | .put_pages = i915_gem_object_put_pages_gtt, |
| 3907 | }; |
| 3908 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3909 | struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev, |
| 3910 | size_t size) |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 3911 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3912 | struct drm_i915_gem_object *obj; |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 3913 | struct address_space *mapping; |
Daniel Vetter | 1a240d4 | 2012-11-29 22:18:51 +0100 | [diff] [blame] | 3914 | gfp_t mask; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3915 | |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 3916 | obj = i915_gem_object_alloc(dev); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3917 | if (obj == NULL) |
| 3918 | return NULL; |
| 3919 | |
| 3920 | if (drm_gem_object_init(dev, &obj->base, size) != 0) { |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 3921 | i915_gem_object_free(obj); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3922 | return NULL; |
| 3923 | } |
| 3924 | |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 3925 | mask = GFP_HIGHUSER | __GFP_RECLAIMABLE; |
| 3926 | if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) { |
| 3927 | /* 965gm cannot relocate objects above 4GiB. */ |
| 3928 | mask &= ~__GFP_HIGHMEM; |
| 3929 | mask |= __GFP_DMA32; |
| 3930 | } |
| 3931 | |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 3932 | mapping = file_inode(obj->base.filp)->i_mapping; |
Chris Wilson | bed1ea9 | 2012-05-24 20:48:12 +0100 | [diff] [blame] | 3933 | mapping_set_gfp_mask(mapping, mask); |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 3934 | |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3935 | i915_gem_object_init(obj, &i915_gem_object_ops); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 3936 | |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3937 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
| 3938 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 3939 | |
Eugeni Dodonov | 3d29b84 | 2012-01-17 14:43:53 -0200 | [diff] [blame] | 3940 | if (HAS_LLC(dev)) { |
| 3941 | /* On some devices, we can have the GPU use the LLC (the CPU |
Eric Anholt | a187111 | 2011-03-29 16:59:55 -0700 | [diff] [blame] | 3942 | * cache) for about a 10% performance improvement |
| 3943 | * compared to uncached. Graphics requests other than |
| 3944 | * display scanout are coherent with the CPU in |
| 3945 | * accessing this cache. This means in this mode we |
| 3946 | * don't need to clflush on the CPU side, and on the |
| 3947 | * GPU side we only need to flush internal caches to |
| 3948 | * get data visible to the CPU. |
| 3949 | * |
| 3950 | * However, we maintain the display planes as UC, and so |
| 3951 | * need to rebind when first used as such. |
| 3952 | */ |
| 3953 | obj->cache_level = I915_CACHE_LLC; |
| 3954 | } else |
| 3955 | obj->cache_level = I915_CACHE_NONE; |
| 3956 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3957 | return obj; |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3960 | int i915_gem_init_object(struct drm_gem_object *obj) |
| 3961 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 3962 | BUG(); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 3963 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3964 | return 0; |
| 3965 | } |
| 3966 | |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 3967 | void i915_gem_free_object(struct drm_gem_object *gem_obj) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 3968 | { |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 3969 | struct drm_i915_gem_object *obj = to_intel_bo(gem_obj); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 3970 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 3971 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 3972 | |
Chris Wilson | 26e12f8 | 2011-03-20 11:20:19 +0000 | [diff] [blame] | 3973 | trace_i915_gem_object_destroy(obj); |
| 3974 | |
Chris Wilson | 1488fc0 | 2012-04-24 15:47:31 +0100 | [diff] [blame] | 3975 | if (obj->phys_obj) |
| 3976 | i915_gem_detach_phys_object(dev, obj); |
| 3977 | |
| 3978 | obj->pin_count = 0; |
| 3979 | if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) { |
| 3980 | bool was_interruptible; |
| 3981 | |
| 3982 | was_interruptible = dev_priv->mm.interruptible; |
| 3983 | dev_priv->mm.interruptible = false; |
| 3984 | |
| 3985 | WARN_ON(i915_gem_object_unbind(obj)); |
| 3986 | |
| 3987 | dev_priv->mm.interruptible = was_interruptible; |
| 3988 | } |
| 3989 | |
Ben Widawsky | 1d64ae7 | 2013-05-31 14:46:20 -0700 | [diff] [blame] | 3990 | /* Stolen objects don't hold a ref, but do hold pin count. Fix that up |
| 3991 | * before progressing. */ |
| 3992 | if (obj->stolen) |
| 3993 | i915_gem_object_unpin_pages(obj); |
| 3994 | |
Ben Widawsky | 401c29f | 2013-05-31 11:28:47 -0700 | [diff] [blame] | 3995 | if (WARN_ON(obj->pages_pin_count)) |
| 3996 | obj->pages_pin_count = 0; |
Chris Wilson | 37e680a | 2012-06-07 15:38:42 +0100 | [diff] [blame] | 3997 | i915_gem_object_put_pages(obj); |
Chris Wilson | d8cb508 | 2012-08-11 15:41:03 +0100 | [diff] [blame] | 3998 | i915_gem_object_free_mmap_offset(obj); |
Chris Wilson | 0104fdb | 2012-11-15 11:32:26 +0000 | [diff] [blame] | 3999 | i915_gem_object_release_stolen(obj); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4000 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 4001 | BUG_ON(obj->pages); |
| 4002 | |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 4003 | if (obj->base.import_attach) |
| 4004 | drm_prime_gem_destroy(&obj->base, NULL); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4005 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4006 | drm_gem_object_release(&obj->base); |
| 4007 | i915_gem_info_remove_obj(dev_priv, obj->base.size); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4008 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4009 | kfree(obj->bit_17); |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 4010 | i915_gem_object_free(obj); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4011 | } |
| 4012 | |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 4013 | struct i915_vma *i915_gem_vma_create(struct drm_i915_gem_object *obj, |
| 4014 | struct i915_address_space *vm) |
| 4015 | { |
| 4016 | struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL); |
| 4017 | if (vma == NULL) |
| 4018 | return ERR_PTR(-ENOMEM); |
| 4019 | |
| 4020 | INIT_LIST_HEAD(&vma->vma_link); |
| 4021 | vma->vm = vm; |
| 4022 | vma->obj = obj; |
| 4023 | |
| 4024 | return vma; |
| 4025 | } |
| 4026 | |
| 4027 | void i915_gem_vma_destroy(struct i915_vma *vma) |
| 4028 | { |
| 4029 | WARN_ON(vma->node.allocated); |
| 4030 | kfree(vma); |
| 4031 | } |
| 4032 | |
Jesse Barnes | 5669fca | 2009-02-17 15:13:31 -0800 | [diff] [blame] | 4033 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4034 | i915_gem_idle(struct drm_device *dev) |
| 4035 | { |
| 4036 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4037 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4038 | |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 4039 | if (dev_priv->ums.mm_suspended) { |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4040 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4041 | return 0; |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4042 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4043 | |
Ben Widawsky | b2da9fe | 2012-04-26 16:02:58 -0700 | [diff] [blame] | 4044 | ret = i915_gpu_idle(dev); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4045 | if (ret) { |
| 4046 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4047 | return ret; |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4048 | } |
Ben Widawsky | b2da9fe | 2012-04-26 16:02:58 -0700 | [diff] [blame] | 4049 | i915_gem_retire_requests(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4050 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4051 | /* Under UMS, be paranoid and evict. */ |
Chris Wilson | a39d7ef | 2012-04-24 18:22:52 +0100 | [diff] [blame] | 4052 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4053 | i915_gem_evict_everything(dev); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4054 | |
Chris Wilson | 312817a | 2010-11-22 11:50:11 +0000 | [diff] [blame] | 4055 | i915_gem_reset_fences(dev); |
| 4056 | |
Daniel Vetter | 99584db | 2012-11-14 17:14:04 +0100 | [diff] [blame] | 4057 | del_timer_sync(&dev_priv->gpu_error.hangcheck_timer); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4058 | |
| 4059 | i915_kernel_lost_context(dev); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4060 | i915_gem_cleanup_ringbuffer(dev); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4061 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4062 | /* Cancel the retire work handler, which should be idle now. */ |
| 4063 | cancel_delayed_work_sync(&dev_priv->mm.retire_work); |
| 4064 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4065 | return 0; |
| 4066 | } |
| 4067 | |
Ben Widawsky | b9524a1 | 2012-05-25 16:56:24 -0700 | [diff] [blame] | 4068 | void i915_gem_l3_remap(struct drm_device *dev) |
| 4069 | { |
| 4070 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4071 | u32 misccpctl; |
| 4072 | int i; |
| 4073 | |
Daniel Vetter | eb32e45 | 2013-02-14 19:46:07 +0100 | [diff] [blame] | 4074 | if (!HAS_L3_GPU_CACHE(dev)) |
Ben Widawsky | b9524a1 | 2012-05-25 16:56:24 -0700 | [diff] [blame] | 4075 | return; |
| 4076 | |
Daniel Vetter | a4da4fa | 2012-11-02 19:55:07 +0100 | [diff] [blame] | 4077 | if (!dev_priv->l3_parity.remap_info) |
Ben Widawsky | b9524a1 | 2012-05-25 16:56:24 -0700 | [diff] [blame] | 4078 | return; |
| 4079 | |
| 4080 | misccpctl = I915_READ(GEN7_MISCCPCTL); |
| 4081 | I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE); |
| 4082 | POSTING_READ(GEN7_MISCCPCTL); |
| 4083 | |
| 4084 | for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) { |
| 4085 | u32 remap = I915_READ(GEN7_L3LOG_BASE + i); |
Daniel Vetter | a4da4fa | 2012-11-02 19:55:07 +0100 | [diff] [blame] | 4086 | if (remap && remap != dev_priv->l3_parity.remap_info[i/4]) |
Ben Widawsky | b9524a1 | 2012-05-25 16:56:24 -0700 | [diff] [blame] | 4087 | DRM_DEBUG("0x%x was already programmed to %x\n", |
| 4088 | GEN7_L3LOG_BASE + i, remap); |
Daniel Vetter | a4da4fa | 2012-11-02 19:55:07 +0100 | [diff] [blame] | 4089 | if (remap && !dev_priv->l3_parity.remap_info[i/4]) |
Ben Widawsky | b9524a1 | 2012-05-25 16:56:24 -0700 | [diff] [blame] | 4090 | DRM_DEBUG_DRIVER("Clearing remapped register\n"); |
Daniel Vetter | a4da4fa | 2012-11-02 19:55:07 +0100 | [diff] [blame] | 4091 | I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]); |
Ben Widawsky | b9524a1 | 2012-05-25 16:56:24 -0700 | [diff] [blame] | 4092 | } |
| 4093 | |
| 4094 | /* Make sure all the writes land before disabling dop clock gating */ |
| 4095 | POSTING_READ(GEN7_L3LOG_BASE); |
| 4096 | |
| 4097 | I915_WRITE(GEN7_MISCCPCTL, misccpctl); |
| 4098 | } |
| 4099 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4100 | void i915_gem_init_swizzling(struct drm_device *dev) |
| 4101 | { |
| 4102 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4103 | |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4104 | if (INTEL_INFO(dev)->gen < 5 || |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4105 | dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE) |
| 4106 | return; |
| 4107 | |
| 4108 | I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) | |
| 4109 | DISP_TILE_SURFACE_SWIZZLING); |
| 4110 | |
Daniel Vetter | 11782b0 | 2012-01-31 16:47:55 +0100 | [diff] [blame] | 4111 | if (IS_GEN5(dev)) |
| 4112 | return; |
| 4113 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4114 | I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL); |
| 4115 | if (IS_GEN6(dev)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4116 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4117 | else if (IS_GEN7(dev)) |
Daniel Vetter | 6b26c86 | 2012-04-24 14:04:12 +0200 | [diff] [blame] | 4118 | I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB)); |
Ben Widawsky | 8782e26 | 2012-12-18 10:31:23 -0800 | [diff] [blame] | 4119 | else |
| 4120 | BUG(); |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4121 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4122 | |
Chris Wilson | 67b1b57 | 2012-07-05 23:49:40 +0100 | [diff] [blame] | 4123 | static bool |
| 4124 | intel_enable_blt(struct drm_device *dev) |
| 4125 | { |
| 4126 | if (!HAS_BLT(dev)) |
| 4127 | return false; |
| 4128 | |
| 4129 | /* The blitter was dysfunctional on early prototypes */ |
| 4130 | if (IS_GEN6(dev) && dev->pdev->revision < 8) { |
| 4131 | DRM_INFO("BLT not supported on this pre-production hardware;" |
| 4132 | " graphics performance will be degraded.\n"); |
| 4133 | return false; |
| 4134 | } |
| 4135 | |
| 4136 | return true; |
| 4137 | } |
| 4138 | |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4139 | static int i915_gem_init_rings(struct drm_device *dev) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4140 | { |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4141 | struct drm_i915_private *dev_priv = dev->dev_private; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4142 | int ret; |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4143 | |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 4144 | ret = intel_init_render_ring_buffer(dev); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4145 | if (ret) |
Chris Wilson | b6913e4 | 2010-11-12 10:46:37 +0000 | [diff] [blame] | 4146 | return ret; |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4147 | |
| 4148 | if (HAS_BSD(dev)) { |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 4149 | ret = intel_init_bsd_ring_buffer(dev); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4150 | if (ret) |
| 4151 | goto cleanup_render_ring; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4152 | } |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4153 | |
Chris Wilson | 67b1b57 | 2012-07-05 23:49:40 +0100 | [diff] [blame] | 4154 | if (intel_enable_blt(dev)) { |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4155 | ret = intel_init_blt_ring_buffer(dev); |
| 4156 | if (ret) |
| 4157 | goto cleanup_bsd_ring; |
| 4158 | } |
| 4159 | |
Ben Widawsky | 9a8a221 | 2013-05-28 19:22:23 -0700 | [diff] [blame] | 4160 | if (HAS_VEBOX(dev)) { |
| 4161 | ret = intel_init_vebox_ring_buffer(dev); |
| 4162 | if (ret) |
| 4163 | goto cleanup_blt_ring; |
| 4164 | } |
| 4165 | |
| 4166 | |
Mika Kuoppala | 9943393 | 2013-01-22 14:12:17 +0200 | [diff] [blame] | 4167 | ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000)); |
| 4168 | if (ret) |
Ben Widawsky | 9a8a221 | 2013-05-28 19:22:23 -0700 | [diff] [blame] | 4169 | goto cleanup_vebox_ring; |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4170 | |
| 4171 | return 0; |
| 4172 | |
Ben Widawsky | 9a8a221 | 2013-05-28 19:22:23 -0700 | [diff] [blame] | 4173 | cleanup_vebox_ring: |
| 4174 | intel_cleanup_ring_buffer(&dev_priv->ring[VECS]); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4175 | cleanup_blt_ring: |
| 4176 | intel_cleanup_ring_buffer(&dev_priv->ring[BCS]); |
| 4177 | cleanup_bsd_ring: |
| 4178 | intel_cleanup_ring_buffer(&dev_priv->ring[VCS]); |
| 4179 | cleanup_render_ring: |
| 4180 | intel_cleanup_ring_buffer(&dev_priv->ring[RCS]); |
| 4181 | |
| 4182 | return ret; |
| 4183 | } |
| 4184 | |
| 4185 | int |
| 4186 | i915_gem_init_hw(struct drm_device *dev) |
| 4187 | { |
| 4188 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4189 | int ret; |
| 4190 | |
| 4191 | if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt()) |
| 4192 | return -EIO; |
| 4193 | |
Ben Widawsky | 5912450 | 2013-07-04 11:02:05 -0700 | [diff] [blame] | 4194 | if (dev_priv->ellc_size) |
Ben Widawsky | 05e21cc | 2013-07-04 11:02:04 -0700 | [diff] [blame] | 4195 | I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf)); |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4196 | |
Ben Widawsky | 88a2b2a | 2013-04-05 13:12:43 -0700 | [diff] [blame] | 4197 | if (HAS_PCH_NOP(dev)) { |
| 4198 | u32 temp = I915_READ(GEN7_MSG_CTL); |
| 4199 | temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK); |
| 4200 | I915_WRITE(GEN7_MSG_CTL, temp); |
| 4201 | } |
| 4202 | |
Ben Widawsky | 4fc7c97 | 2013-02-08 11:49:24 -0800 | [diff] [blame] | 4203 | i915_gem_l3_remap(dev); |
| 4204 | |
| 4205 | i915_gem_init_swizzling(dev); |
| 4206 | |
| 4207 | ret = i915_gem_init_rings(dev); |
| 4208 | if (ret) |
Mika Kuoppala | 9943393 | 2013-01-22 14:12:17 +0200 | [diff] [blame] | 4209 | return ret; |
| 4210 | |
Ben Widawsky | 254f965 | 2012-06-04 14:42:42 -0700 | [diff] [blame] | 4211 | /* |
| 4212 | * XXX: There was some w/a described somewhere suggesting loading |
| 4213 | * contexts before PPGTT. |
| 4214 | */ |
| 4215 | i915_gem_context_init(dev); |
Ben Widawsky | b7c36d2 | 2013-04-08 18:43:56 -0700 | [diff] [blame] | 4216 | if (dev_priv->mm.aliasing_ppgtt) { |
| 4217 | ret = dev_priv->mm.aliasing_ppgtt->enable(dev); |
| 4218 | if (ret) { |
| 4219 | i915_gem_cleanup_aliasing_ppgtt(dev); |
| 4220 | DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n"); |
| 4221 | } |
| 4222 | } |
Daniel Vetter | e21af88 | 2012-02-09 20:53:27 +0100 | [diff] [blame] | 4223 | |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4224 | return 0; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4225 | } |
| 4226 | |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4227 | int i915_gem_init(struct drm_device *dev) |
| 4228 | { |
| 4229 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4230 | int ret; |
| 4231 | |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4232 | mutex_lock(&dev->struct_mutex); |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 4233 | |
| 4234 | if (IS_VALLEYVIEW(dev)) { |
| 4235 | /* VLVA0 (potential hack), BIOS isn't actually waking us */ |
| 4236 | I915_WRITE(VLV_GTLC_WAKE_CTRL, 1); |
| 4237 | if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10)) |
| 4238 | DRM_DEBUG_DRIVER("allow wake ack timed out\n"); |
| 4239 | } |
| 4240 | |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 4241 | i915_gem_init_global_gtt(dev); |
Jesse Barnes | d62b489 | 2013-03-08 10:45:53 -0800 | [diff] [blame] | 4242 | |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4243 | ret = i915_gem_init_hw(dev); |
| 4244 | mutex_unlock(&dev->struct_mutex); |
| 4245 | if (ret) { |
| 4246 | i915_gem_cleanup_aliasing_ppgtt(dev); |
| 4247 | return ret; |
| 4248 | } |
| 4249 | |
Daniel Vetter | 53ca26c | 2012-04-26 23:28:03 +0200 | [diff] [blame] | 4250 | /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */ |
| 4251 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4252 | dev_priv->dri1.allow_batchbuffer = 1; |
Chris Wilson | 1070a42 | 2012-04-24 15:47:41 +0100 | [diff] [blame] | 4253 | return 0; |
| 4254 | } |
| 4255 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4256 | void |
| 4257 | i915_gem_cleanup_ringbuffer(struct drm_device *dev) |
| 4258 | { |
| 4259 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 4260 | struct intel_ring_buffer *ring; |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 4261 | int i; |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4262 | |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 4263 | for_each_ring(ring, dev_priv, i) |
| 4264 | intel_cleanup_ring_buffer(ring); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4265 | } |
| 4266 | |
| 4267 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4268 | i915_gem_entervt_ioctl(struct drm_device *dev, void *data, |
| 4269 | struct drm_file *file_priv) |
| 4270 | { |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 4271 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | b451951 | 2012-05-11 14:29:30 +0100 | [diff] [blame] | 4272 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4273 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4274 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4275 | return 0; |
| 4276 | |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 4277 | if (i915_reset_in_progress(&dev_priv->gpu_error)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4278 | DRM_ERROR("Reenabling wedged hardware, good luck\n"); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 4279 | atomic_set(&dev_priv->gpu_error.reset_counter, 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4280 | } |
| 4281 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4282 | mutex_lock(&dev->struct_mutex); |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 4283 | dev_priv->ums.mm_suspended = 0; |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4284 | |
Daniel Vetter | f691e2f | 2012-02-02 09:58:12 +0100 | [diff] [blame] | 4285 | ret = i915_gem_init_hw(dev); |
Wu Fengguang | d816f6ac | 2009-04-18 10:43:32 +0800 | [diff] [blame] | 4286 | if (ret != 0) { |
| 4287 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4288 | return ret; |
Wu Fengguang | d816f6ac | 2009-04-18 10:43:32 +0800 | [diff] [blame] | 4289 | } |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4290 | |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 4291 | BUG_ON(!list_empty(&dev_priv->gtt.base.active_list)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4292 | mutex_unlock(&dev->struct_mutex); |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4293 | |
Chris Wilson | 5f35308 | 2010-06-07 14:03:03 +0100 | [diff] [blame] | 4294 | ret = drm_irq_install(dev); |
| 4295 | if (ret) |
| 4296 | goto cleanup_ringbuffer; |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4297 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4298 | return 0; |
Chris Wilson | 5f35308 | 2010-06-07 14:03:03 +0100 | [diff] [blame] | 4299 | |
| 4300 | cleanup_ringbuffer: |
| 4301 | mutex_lock(&dev->struct_mutex); |
| 4302 | i915_gem_cleanup_ringbuffer(dev); |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 4303 | dev_priv->ums.mm_suspended = 1; |
Chris Wilson | 5f35308 | 2010-06-07 14:03:03 +0100 | [diff] [blame] | 4304 | mutex_unlock(&dev->struct_mutex); |
| 4305 | |
| 4306 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | int |
| 4310 | i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, |
| 4311 | struct drm_file *file_priv) |
| 4312 | { |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 4313 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 4314 | int ret; |
| 4315 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4316 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4317 | return 0; |
| 4318 | |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4319 | drm_irq_uninstall(dev); |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 4320 | |
| 4321 | mutex_lock(&dev->struct_mutex); |
| 4322 | ret = i915_gem_idle(dev); |
| 4323 | |
| 4324 | /* Hack! Don't let anybody do execbuf while we don't control the chip. |
| 4325 | * We need to replace this with a semaphore, or something. |
| 4326 | * And not confound ums.mm_suspended! |
| 4327 | */ |
| 4328 | if (ret != 0) |
| 4329 | dev_priv->ums.mm_suspended = 1; |
| 4330 | mutex_unlock(&dev->struct_mutex); |
| 4331 | |
| 4332 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4333 | } |
| 4334 | |
| 4335 | void |
| 4336 | i915_gem_lastclose(struct drm_device *dev) |
| 4337 | { |
| 4338 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4339 | |
Eric Anholt | e806b49 | 2009-01-22 09:56:58 -0800 | [diff] [blame] | 4340 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4341 | return; |
| 4342 | |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 4343 | mutex_lock(&dev->struct_mutex); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4344 | ret = i915_gem_idle(dev); |
| 4345 | if (ret) |
| 4346 | DRM_ERROR("failed to idle hardware: %d\n", ret); |
Daniel Vetter | db1b76c | 2013-07-09 16:51:37 +0200 | [diff] [blame] | 4347 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4348 | } |
| 4349 | |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 4350 | static void |
| 4351 | init_ring_lists(struct intel_ring_buffer *ring) |
| 4352 | { |
| 4353 | INIT_LIST_HEAD(&ring->active_list); |
| 4354 | INIT_LIST_HEAD(&ring->request_list); |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 4355 | } |
| 4356 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4357 | void |
| 4358 | i915_gem_load(struct drm_device *dev) |
| 4359 | { |
| 4360 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 42dcedd | 2012-11-15 11:32:30 +0000 | [diff] [blame] | 4361 | int i; |
| 4362 | |
| 4363 | dev_priv->slab = |
| 4364 | kmem_cache_create("i915_gem_object", |
| 4365 | sizeof(struct drm_i915_gem_object), 0, |
| 4366 | SLAB_HWCACHE_ALIGN, |
| 4367 | NULL); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4368 | |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 4369 | INIT_LIST_HEAD(&dev_priv->gtt.base.active_list); |
| 4370 | INIT_LIST_HEAD(&dev_priv->gtt.base.inactive_list); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4371 | INIT_LIST_HEAD(&dev_priv->mm.unbound_list); |
| 4372 | INIT_LIST_HEAD(&dev_priv->mm.bound_list); |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 4373 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
Chris Wilson | 1ec14ad | 2010-12-04 11:30:53 +0000 | [diff] [blame] | 4374 | for (i = 0; i < I915_NUM_RINGS; i++) |
| 4375 | init_ring_lists(&dev_priv->ring[i]); |
Daniel Vetter | 4b9de73 | 2011-10-09 21:52:02 +0200 | [diff] [blame] | 4376 | for (i = 0; i < I915_MAX_NUM_FENCES; i++) |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 4377 | INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4378 | INIT_DELAYED_WORK(&dev_priv->mm.retire_work, |
| 4379 | i915_gem_retire_work_handler); |
Daniel Vetter | 1f83fee | 2012-11-15 17:17:22 +0100 | [diff] [blame] | 4380 | init_waitqueue_head(&dev_priv->gpu_error.reset_queue); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4381 | |
Dave Airlie | 9440012 | 2010-07-20 13:15:31 +1000 | [diff] [blame] | 4382 | /* On GEN3 we really need to make sure the ARB C3 LP bit is set */ |
| 4383 | if (IS_GEN3(dev)) { |
Daniel Vetter | 5074329 | 2012-04-26 22:02:54 +0200 | [diff] [blame] | 4384 | I915_WRITE(MI_ARB_STATE, |
| 4385 | _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE)); |
Dave Airlie | 9440012 | 2010-07-20 13:15:31 +1000 | [diff] [blame] | 4386 | } |
| 4387 | |
Chris Wilson | 72bfa19 | 2010-12-19 11:42:05 +0000 | [diff] [blame] | 4388 | dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL; |
| 4389 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4390 | /* Old X drivers will take 0-2 for front, back, depth buffers */ |
Eric Anholt | b397c83 | 2010-01-26 09:43:10 -0800 | [diff] [blame] | 4391 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4392 | dev_priv->fence_reg_start = 3; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4393 | |
Ville Syrjälä | 42b5aea | 2013-04-09 13:02:47 +0300 | [diff] [blame] | 4394 | if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev)) |
| 4395 | dev_priv->num_fence_regs = 32; |
| 4396 | else 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] | 4397 | dev_priv->num_fence_regs = 16; |
| 4398 | else |
| 4399 | dev_priv->num_fence_regs = 8; |
| 4400 | |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4401 | /* Initialize fence registers to zero */ |
Chris Wilson | ada726c | 2012-04-17 15:31:32 +0100 | [diff] [blame] | 4402 | i915_gem_reset_fences(dev); |
Eric Anholt | 10ed13e | 2011-05-06 13:53:49 -0700 | [diff] [blame] | 4403 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4404 | i915_gem_detect_bit_6_swizzle(dev); |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 4405 | init_waitqueue_head(&dev_priv->pending_flip_queue); |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 4406 | |
Chris Wilson | ce453d8 | 2011-02-21 14:43:56 +0000 | [diff] [blame] | 4407 | dev_priv->mm.interruptible = true; |
| 4408 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 4409 | dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink; |
| 4410 | dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS; |
| 4411 | register_shrinker(&dev_priv->mm.inactive_shrinker); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4412 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4413 | |
| 4414 | /* |
| 4415 | * Create a physically contiguous memory object for this object |
| 4416 | * e.g. for cursor + overlay regs |
| 4417 | */ |
Chris Wilson | 995b6762 | 2010-08-20 13:23:26 +0100 | [diff] [blame] | 4418 | static int i915_gem_init_phys_object(struct drm_device *dev, |
| 4419 | int id, int size, int align) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4420 | { |
| 4421 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4422 | struct drm_i915_gem_phys_object *phys_obj; |
| 4423 | int ret; |
| 4424 | |
| 4425 | if (dev_priv->mm.phys_objs[id - 1] || !size) |
| 4426 | return 0; |
| 4427 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 4428 | phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4429 | if (!phys_obj) |
| 4430 | return -ENOMEM; |
| 4431 | |
| 4432 | phys_obj->id = id; |
| 4433 | |
Chris Wilson | 6eeefaf | 2010-08-07 11:01:39 +0100 | [diff] [blame] | 4434 | phys_obj->handle = drm_pci_alloc(dev, size, align); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4435 | if (!phys_obj->handle) { |
| 4436 | ret = -ENOMEM; |
| 4437 | goto kfree_obj; |
| 4438 | } |
| 4439 | #ifdef CONFIG_X86 |
| 4440 | set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); |
| 4441 | #endif |
| 4442 | |
| 4443 | dev_priv->mm.phys_objs[id - 1] = phys_obj; |
| 4444 | |
| 4445 | return 0; |
| 4446 | kfree_obj: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 4447 | kfree(phys_obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4448 | return ret; |
| 4449 | } |
| 4450 | |
Chris Wilson | 995b6762 | 2010-08-20 13:23:26 +0100 | [diff] [blame] | 4451 | 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] | 4452 | { |
| 4453 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4454 | struct drm_i915_gem_phys_object *phys_obj; |
| 4455 | |
| 4456 | if (!dev_priv->mm.phys_objs[id - 1]) |
| 4457 | return; |
| 4458 | |
| 4459 | phys_obj = dev_priv->mm.phys_objs[id - 1]; |
| 4460 | if (phys_obj->cur_obj) { |
| 4461 | i915_gem_detach_phys_object(dev, phys_obj->cur_obj); |
| 4462 | } |
| 4463 | |
| 4464 | #ifdef CONFIG_X86 |
| 4465 | set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); |
| 4466 | #endif |
| 4467 | drm_pci_free(dev, phys_obj->handle); |
| 4468 | kfree(phys_obj); |
| 4469 | dev_priv->mm.phys_objs[id - 1] = NULL; |
| 4470 | } |
| 4471 | |
| 4472 | void i915_gem_free_all_phys_object(struct drm_device *dev) |
| 4473 | { |
| 4474 | int i; |
| 4475 | |
Dave Airlie | 260883c | 2009-01-22 17:58:49 +1000 | [diff] [blame] | 4476 | 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] | 4477 | i915_gem_free_phys_object(dev, i); |
| 4478 | } |
| 4479 | |
| 4480 | void i915_gem_detach_phys_object(struct drm_device *dev, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4481 | struct drm_i915_gem_object *obj) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4482 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4483 | struct address_space *mapping = file_inode(obj->base.filp)->i_mapping; |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 4484 | char *vaddr; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4485 | int i; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4486 | int page_count; |
| 4487 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4488 | if (!obj->phys_obj) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4489 | return; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4490 | vaddr = obj->phys_obj->handle->vaddr; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4491 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4492 | page_count = obj->base.size / PAGE_SIZE; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4493 | for (i = 0; i < page_count; i++) { |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4494 | struct page *page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 4495 | if (!IS_ERR(page)) { |
| 4496 | char *dst = kmap_atomic(page); |
| 4497 | memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE); |
| 4498 | kunmap_atomic(dst); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4499 | |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 4500 | drm_clflush_pages(&page, 1); |
| 4501 | |
| 4502 | set_page_dirty(page); |
| 4503 | mark_page_accessed(page); |
| 4504 | page_cache_release(page); |
| 4505 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4506 | } |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 4507 | i915_gem_chipset_flush(dev); |
Chris Wilson | d78b47b | 2009-06-17 21:52:49 +0100 | [diff] [blame] | 4508 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4509 | obj->phys_obj->cur_obj = NULL; |
| 4510 | obj->phys_obj = NULL; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4511 | } |
| 4512 | |
| 4513 | int |
| 4514 | i915_gem_attach_phys_object(struct drm_device *dev, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4515 | struct drm_i915_gem_object *obj, |
Chris Wilson | 6eeefaf | 2010-08-07 11:01:39 +0100 | [diff] [blame] | 4516 | int id, |
| 4517 | int align) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4518 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 4519 | struct address_space *mapping = file_inode(obj->base.filp)->i_mapping; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4520 | drm_i915_private_t *dev_priv = dev->dev_private; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4521 | int ret = 0; |
| 4522 | int page_count; |
| 4523 | int i; |
| 4524 | |
| 4525 | if (id > I915_MAX_PHYS_OBJECT) |
| 4526 | return -EINVAL; |
| 4527 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4528 | if (obj->phys_obj) { |
| 4529 | if (obj->phys_obj->id == id) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4530 | return 0; |
| 4531 | i915_gem_detach_phys_object(dev, obj); |
| 4532 | } |
| 4533 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4534 | /* create a new object */ |
| 4535 | if (!dev_priv->mm.phys_objs[id - 1]) { |
| 4536 | ret = i915_gem_init_phys_object(dev, id, |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4537 | obj->base.size, align); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4538 | if (ret) { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4539 | DRM_ERROR("failed to init phys object %d size: %zu\n", |
| 4540 | id, obj->base.size); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 4541 | return ret; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4542 | } |
| 4543 | } |
| 4544 | |
| 4545 | /* bind to the object */ |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4546 | obj->phys_obj = dev_priv->mm.phys_objs[id - 1]; |
| 4547 | obj->phys_obj->cur_obj = obj; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4548 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4549 | page_count = obj->base.size / PAGE_SIZE; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4550 | |
| 4551 | for (i = 0; i < page_count; i++) { |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 4552 | struct page *page; |
| 4553 | char *dst, *src; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4554 | |
Hugh Dickins | 5949eac | 2011-06-27 16:18:18 -0700 | [diff] [blame] | 4555 | page = shmem_read_mapping_page(mapping, i); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 4556 | if (IS_ERR(page)) |
| 4557 | return PTR_ERR(page); |
| 4558 | |
Chris Wilson | ff75b9b | 2010-10-30 22:52:31 +0100 | [diff] [blame] | 4559 | src = kmap_atomic(page); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4560 | dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4561 | memcpy(dst, src, PAGE_SIZE); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 4562 | kunmap_atomic(src); |
Chris Wilson | e5281cc | 2010-10-28 13:45:36 +0100 | [diff] [blame] | 4563 | |
| 4564 | mark_page_accessed(page); |
| 4565 | page_cache_release(page); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | return 0; |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4569 | } |
| 4570 | |
| 4571 | static int |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4572 | i915_gem_phys_pwrite(struct drm_device *dev, |
| 4573 | struct drm_i915_gem_object *obj, |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4574 | struct drm_i915_gem_pwrite *args, |
| 4575 | struct drm_file *file_priv) |
| 4576 | { |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 4577 | void *vaddr = obj->phys_obj->handle->vaddr + args->offset; |
Ville Syrjälä | 2bb4629 | 2013-02-22 16:12:51 +0200 | [diff] [blame] | 4578 | char __user *user_data = to_user_ptr(args->data_ptr); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4579 | |
Chris Wilson | b47b30c | 2010-11-08 01:12:29 +0000 | [diff] [blame] | 4580 | if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) { |
| 4581 | unsigned long unwritten; |
| 4582 | |
| 4583 | /* The physical object once assigned is fixed for the lifetime |
| 4584 | * of the obj, so we can safely drop the lock and continue |
| 4585 | * to access vaddr. |
| 4586 | */ |
| 4587 | mutex_unlock(&dev->struct_mutex); |
| 4588 | unwritten = copy_from_user(vaddr, user_data, args->size); |
| 4589 | mutex_lock(&dev->struct_mutex); |
| 4590 | if (unwritten) |
| 4591 | return -EFAULT; |
| 4592 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4593 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 4594 | i915_gem_chipset_flush(dev); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4595 | return 0; |
| 4596 | } |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4597 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4598 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4599 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4600 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4601 | |
| 4602 | /* Clean up our request list when the client is going away, so that |
| 4603 | * later retire_requests won't dereference our soon-to-be-gone |
| 4604 | * file_priv. |
| 4605 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4606 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4607 | while (!list_empty(&file_priv->mm.request_list)) { |
| 4608 | struct drm_i915_gem_request *request; |
| 4609 | |
| 4610 | request = list_first_entry(&file_priv->mm.request_list, |
| 4611 | struct drm_i915_gem_request, |
| 4612 | client_list); |
| 4613 | list_del(&request->client_list); |
| 4614 | request->file_priv = NULL; |
| 4615 | } |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4616 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4617 | } |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4618 | |
Chris Wilson | 5774506 | 2012-11-21 13:04:04 +0000 | [diff] [blame] | 4619 | static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task) |
| 4620 | { |
| 4621 | if (!mutex_is_locked(mutex)) |
| 4622 | return false; |
| 4623 | |
| 4624 | #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES) |
| 4625 | return mutex->owner == task; |
| 4626 | #else |
| 4627 | /* Since UP may be pre-empted, we cannot assume that we own the lock */ |
| 4628 | return false; |
| 4629 | #endif |
| 4630 | } |
| 4631 | |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4632 | static int |
Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 4633 | i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc) |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4634 | { |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 4635 | struct drm_i915_private *dev_priv = |
| 4636 | container_of(shrinker, |
| 4637 | struct drm_i915_private, |
| 4638 | mm.inactive_shrinker); |
| 4639 | struct drm_device *dev = dev_priv->dev; |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 4640 | struct i915_address_space *vm = &dev_priv->gtt.base; |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4641 | struct drm_i915_gem_object *obj; |
Ying Han | 1495f23 | 2011-05-24 17:12:27 -0700 | [diff] [blame] | 4642 | int nr_to_scan = sc->nr_to_scan; |
Chris Wilson | 5774506 | 2012-11-21 13:04:04 +0000 | [diff] [blame] | 4643 | bool unlock = true; |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 4644 | int cnt; |
| 4645 | |
Chris Wilson | 5774506 | 2012-11-21 13:04:04 +0000 | [diff] [blame] | 4646 | if (!mutex_trylock(&dev->struct_mutex)) { |
| 4647 | if (!mutex_is_locked_by(&dev->struct_mutex, current)) |
| 4648 | return 0; |
| 4649 | |
Daniel Vetter | 677feac | 2012-12-19 14:33:45 +0100 | [diff] [blame] | 4650 | if (dev_priv->mm.shrinker_no_lock_stealing) |
| 4651 | return 0; |
| 4652 | |
Chris Wilson | 5774506 | 2012-11-21 13:04:04 +0000 | [diff] [blame] | 4653 | unlock = false; |
| 4654 | } |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4655 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4656 | if (nr_to_scan) { |
| 4657 | nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan); |
| 4658 | if (nr_to_scan > 0) |
Daniel Vetter | 93927ca | 2013-01-10 18:03:00 +0100 | [diff] [blame] | 4659 | nr_to_scan -= __i915_gem_shrink(dev_priv, nr_to_scan, |
| 4660 | false); |
| 4661 | if (nr_to_scan > 0) |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4662 | i915_gem_shrink_all(dev_priv); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4663 | } |
| 4664 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 4665 | cnt = 0; |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 4666 | list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 4667 | if (obj->pages_pin_count == 0) |
| 4668 | cnt += obj->base.size >> PAGE_SHIFT; |
Ben Widawsky | 5cef07e | 2013-07-16 16:50:08 -0700 | [diff] [blame] | 4669 | list_for_each_entry(obj, &vm->inactive_list, global_list) |
Chris Wilson | a557017 | 2012-09-04 21:02:54 +0100 | [diff] [blame] | 4670 | if (obj->pin_count == 0 && obj->pages_pin_count == 0) |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4671 | cnt += obj->base.size >> PAGE_SHIFT; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4672 | |
Chris Wilson | 5774506 | 2012-11-21 13:04:04 +0000 | [diff] [blame] | 4673 | if (unlock) |
| 4674 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 4675 | return cnt; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4676 | } |