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