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 <linux/string.h> |
| 29 | #include <linux/bitops.h> |
| 30 | #include <drm/drmP.h> |
| 31 | #include <drm/i915_drm.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 32 | #include "i915_drv.h" |
| 33 | |
Daniel Vetter | 3271dca | 2015-07-24 17:40:15 +0200 | [diff] [blame] | 34 | /** |
| 35 | * DOC: buffer object tiling |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 36 | * |
Chris Wilson | 111dbca | 2017-01-10 12:10:44 +0000 | [diff] [blame] | 37 | * i915_gem_set_tiling_ioctl() and i915_gem_get_tiling_ioctl() is the userspace |
| 38 | * interface to declare fence register requirements. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 39 | * |
Daniel Vetter | 3271dca | 2015-07-24 17:40:15 +0200 | [diff] [blame] | 40 | * In principle GEM doesn't care at all about the internal data layout of an |
| 41 | * object, and hence it also doesn't care about tiling or swizzling. There's two |
| 42 | * exceptions: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 43 | * |
Daniel Vetter | 3271dca | 2015-07-24 17:40:15 +0200 | [diff] [blame] | 44 | * - For X and Y tiling the hardware provides detilers for CPU access, so called |
| 45 | * fences. Since there's only a limited amount of them the kernel must manage |
| 46 | * these, and therefore userspace must tell the kernel the object tiling if it |
| 47 | * wants to use fences for detiling. |
| 48 | * - On gen3 and gen4 platforms have a swizzling pattern for tiled objects which |
| 49 | * depends upon the physical page frame number. When swapping such objects the |
| 50 | * page frame number might change and the kernel must be able to fix this up |
| 51 | * and hence now the tiling. Note that on a subset of platforms with |
| 52 | * asymmetric memory channel population the swizzling pattern changes in an |
| 53 | * unknown way, and for those the kernel simply forbids swapping completely. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 54 | * |
Daniel Vetter | 3271dca | 2015-07-24 17:40:15 +0200 | [diff] [blame] | 55 | * Since neither of this applies for new tiling layouts on modern platforms like |
| 56 | * W, Ys and Yf tiling GEM only allows object tiling to be set to X or Y tiled. |
| 57 | * Anything else can be handled in userspace entirely without the kernel's |
| 58 | * invovlement. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 59 | */ |
| 60 | |
Chris Wilson | 91d4e0aa | 2017-01-09 16:16:13 +0000 | [diff] [blame] | 61 | /** |
| 62 | * i915_gem_fence_size - required global GTT size for a fence |
| 63 | * @i915: i915 device |
| 64 | * @size: object size |
| 65 | * @tiling: tiling mode |
| 66 | * @stride: tiling stride |
| 67 | * |
| 68 | * Return the required global GTT size for a fence (view of a tiled object), |
| 69 | * taking into account potential fence register mapping. |
| 70 | */ |
| 71 | u32 i915_gem_fence_size(struct drm_i915_private *i915, |
| 72 | u32 size, unsigned int tiling, unsigned int stride) |
| 73 | { |
| 74 | u32 ggtt_size; |
| 75 | |
| 76 | GEM_BUG_ON(!size); |
| 77 | |
| 78 | if (tiling == I915_TILING_NONE) |
| 79 | return size; |
| 80 | |
| 81 | GEM_BUG_ON(!stride); |
| 82 | |
| 83 | if (INTEL_GEN(i915) >= 4) { |
| 84 | stride *= i915_gem_tile_height(tiling); |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame^] | 85 | GEM_BUG_ON(!IS_ALIGNED(stride, I965_FENCE_PAGE)); |
Chris Wilson | 91d4e0aa | 2017-01-09 16:16:13 +0000 | [diff] [blame] | 86 | return roundup(size, stride); |
| 87 | } |
| 88 | |
| 89 | /* Previous chips need a power-of-two fence region when tiling */ |
| 90 | if (IS_GEN3(i915)) |
| 91 | ggtt_size = 1024*1024; |
| 92 | else |
| 93 | ggtt_size = 512*1024; |
| 94 | |
| 95 | while (ggtt_size < size) |
| 96 | ggtt_size <<= 1; |
| 97 | |
| 98 | return ggtt_size; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * i915_gem_fence_alignment - required global GTT alignment for a fence |
| 103 | * @i915: i915 device |
| 104 | * @size: object size |
| 105 | * @tiling: tiling mode |
| 106 | * @stride: tiling stride |
| 107 | * |
| 108 | * Return the required global GTT alignment for a fence (a view of a tiled |
| 109 | * object), taking into account potential fence register mapping. |
| 110 | */ |
| 111 | u32 i915_gem_fence_alignment(struct drm_i915_private *i915, u32 size, |
| 112 | unsigned int tiling, unsigned int stride) |
| 113 | { |
| 114 | GEM_BUG_ON(!size); |
| 115 | |
| 116 | /* |
| 117 | * Minimum alignment is 4k (GTT page size), but might be greater |
| 118 | * if a fence register is needed for the object. |
| 119 | */ |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame^] | 120 | if (tiling == I915_TILING_NONE) |
| 121 | return I915_GTT_MIN_ALIGNMENT; |
| 122 | |
| 123 | if (INTEL_GEN(i915) >= 4) |
| 124 | return I965_FENCE_PAGE; |
Chris Wilson | 91d4e0aa | 2017-01-09 16:16:13 +0000 | [diff] [blame] | 125 | |
| 126 | /* |
| 127 | * Previous chips need to be aligned to the size of the smallest |
| 128 | * fence register that can contain the object. |
| 129 | */ |
| 130 | return i915_gem_fence_size(i915, size, tiling, stride); |
| 131 | } |
| 132 | |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 133 | /* Check pitch constriants for all chips & tiling formats */ |
Chris Wilson | a00b10c | 2010-09-24 21:15:47 +0100 | [diff] [blame] | 134 | static bool |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 135 | i915_tiling_ok(struct drm_i915_gem_object *obj, |
| 136 | unsigned int tiling, unsigned int stride) |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 137 | { |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 138 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 139 | unsigned int tile_width; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 140 | |
| 141 | /* Linear is always fine */ |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 142 | if (tiling == I915_TILING_NONE) |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 143 | return true; |
| 144 | |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 145 | if (tiling > I915_TILING_LAST) |
Chris Wilson | deeb151 | 2016-08-05 10:14:22 +0100 | [diff] [blame] | 146 | return false; |
| 147 | |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 148 | /* check maximum stride & object size */ |
Ville Syrjälä | 3a06247 | 2013-04-09 11:45:05 +0300 | [diff] [blame] | 149 | /* i965+ stores the end address of the gtt mapping in the fence |
| 150 | * reg, so dont bother to check the size */ |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 151 | if (INTEL_GEN(i915) >= 7) { |
Ville Syrjälä | 3a06247 | 2013-04-09 11:45:05 +0300 | [diff] [blame] | 152 | if (stride / 128 > GEN7_FENCE_MAX_PITCH_VAL) |
| 153 | return false; |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 154 | } else if (INTEL_GEN(i915) >= 4) { |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 155 | if (stride / 128 > I965_FENCE_MAX_PITCH_VAL) |
| 156 | return false; |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 157 | } else { |
Daniel Vetter | c36a2a6 | 2010-04-17 15:12:03 +0200 | [diff] [blame] | 158 | if (stride > 8192) |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 159 | return false; |
Eric Anholt | e76a16d | 2009-05-26 17:44:56 -0700 | [diff] [blame] | 160 | |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 161 | if (IS_GEN3(i915)) { |
| 162 | if (obj->base.size > I830_FENCE_MAX_SIZE_VAL << 20) |
Daniel Vetter | c36a2a6 | 2010-04-17 15:12:03 +0200 | [diff] [blame] | 163 | return false; |
| 164 | } else { |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 165 | if (obj->base.size > I830_FENCE_MAX_SIZE_VAL << 19) |
Daniel Vetter | c36a2a6 | 2010-04-17 15:12:03 +0200 | [diff] [blame] | 166 | return false; |
| 167 | } |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 170 | if (IS_GEN2(i915) || |
| 171 | (tiling == I915_TILING_Y && HAS_128_BYTE_Y_TILING(i915))) |
| 172 | tile_width = 128; |
| 173 | else |
| 174 | tile_width = 512; |
| 175 | |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame^] | 176 | if (!IS_ALIGNED(stride, tile_width)) |
Ville Syrjälä | fe48d8d | 2013-04-09 20:09:13 +0300 | [diff] [blame] | 177 | return false; |
| 178 | |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 179 | /* 965+ just needs multiples of tile width */ |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 180 | if (INTEL_GEN(i915) >= 4) |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 181 | return true; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 182 | |
| 183 | /* Pre-965 needs power of two tile widths */ |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 184 | return is_power_of_2(stride); |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Chris Wilson | 5b30694 | 2017-01-09 16:16:09 +0000 | [diff] [blame] | 187 | static bool i915_vma_fence_prepare(struct i915_vma *vma, |
| 188 | int tiling_mode, unsigned int stride) |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 189 | { |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 190 | struct drm_i915_private *i915 = vma->vm->i915; |
| 191 | u32 size, alignment; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 192 | |
| 193 | if (!i915_vma_is_map_and_fenceable(vma)) |
| 194 | return true; |
| 195 | |
Chris Wilson | 91d4e0aa | 2017-01-09 16:16:13 +0000 | [diff] [blame] | 196 | size = i915_gem_fence_size(i915, vma->size, tiling_mode, stride); |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 197 | if (vma->node.size < size) |
| 198 | return false; |
| 199 | |
Chris Wilson | 91d4e0aa | 2017-01-09 16:16:13 +0000 | [diff] [blame] | 200 | alignment = i915_gem_fence_alignment(i915, vma->size, tiling_mode, stride); |
Chris Wilson | f51455d | 2017-01-10 14:47:34 +0000 | [diff] [blame^] | 201 | if (!IS_ALIGNED(vma->node.start, alignment)) |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 202 | return false; |
| 203 | |
| 204 | return true; |
| 205 | } |
| 206 | |
Chris Wilson | f23eda8 | 2016-08-15 10:48:53 +0100 | [diff] [blame] | 207 | /* Make the current GTT allocation valid for the change in tiling. */ |
| 208 | static int |
Chris Wilson | 5b30694 | 2017-01-09 16:16:09 +0000 | [diff] [blame] | 209 | i915_gem_object_fence_prepare(struct drm_i915_gem_object *obj, |
| 210 | int tiling_mode, unsigned int stride) |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 211 | { |
Chris Wilson | f23eda8 | 2016-08-15 10:48:53 +0100 | [diff] [blame] | 212 | struct i915_vma *vma; |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 213 | int ret; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 214 | |
| 215 | if (tiling_mode == I915_TILING_NONE) |
Chris Wilson | f23eda8 | 2016-08-15 10:48:53 +0100 | [diff] [blame] | 216 | return 0; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 217 | |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 218 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
Chris Wilson | 944397f | 2017-01-09 16:16:11 +0000 | [diff] [blame] | 219 | if (!i915_vma_is_ggtt(vma)) |
| 220 | break; |
| 221 | |
Chris Wilson | 5b30694 | 2017-01-09 16:16:09 +0000 | [diff] [blame] | 222 | if (i915_vma_fence_prepare(vma, tiling_mode, stride)) |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 223 | continue; |
Chris Wilson | f23eda8 | 2016-08-15 10:48:53 +0100 | [diff] [blame] | 224 | |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 225 | ret = i915_vma_unbind(vma); |
| 226 | if (ret) |
| 227 | return ret; |
Chris Wilson | df15315 | 2010-11-15 05:25:58 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Chris Wilson | f23eda8 | 2016-08-15 10:48:53 +0100 | [diff] [blame] | 230 | return 0; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 231 | } |
| 232 | |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 233 | int |
| 234 | i915_gem_object_set_tiling(struct drm_i915_gem_object *obj, |
| 235 | unsigned int tiling, unsigned int stride) |
| 236 | { |
| 237 | struct drm_i915_private *i915 = to_i915(obj->base.dev); |
| 238 | struct i915_vma *vma; |
| 239 | int err; |
| 240 | |
| 241 | /* Make sure we don't cross-contaminate obj->tiling_and_stride */ |
| 242 | BUILD_BUG_ON(I915_TILING_LAST & STRIDE_MASK); |
| 243 | |
| 244 | GEM_BUG_ON(!i915_tiling_ok(obj, tiling, stride)); |
| 245 | GEM_BUG_ON(!stride ^ (tiling == I915_TILING_NONE)); |
| 246 | lockdep_assert_held(&i915->drm.struct_mutex); |
| 247 | |
| 248 | if ((tiling | stride) == obj->tiling_and_stride) |
| 249 | return 0; |
| 250 | |
| 251 | if (obj->framebuffer_references) |
| 252 | return -EBUSY; |
| 253 | |
| 254 | /* We need to rebind the object if its current allocation |
| 255 | * no longer meets the alignment restrictions for its new |
| 256 | * tiling mode. Otherwise we can just leave it alone, but |
| 257 | * need to ensure that any fence register is updated before |
| 258 | * the next fenced (either through the GTT or by the BLT unit |
| 259 | * on older GPUs) access. |
| 260 | * |
| 261 | * After updating the tiling parameters, we then flag whether |
| 262 | * we need to update an associated fence register. Note this |
| 263 | * has to also include the unfenced register the GPU uses |
| 264 | * whilst executing a fenced command for an untiled object. |
| 265 | */ |
| 266 | |
| 267 | err = i915_gem_object_fence_prepare(obj, tiling, stride); |
| 268 | if (err) |
| 269 | return err; |
| 270 | |
| 271 | /* If the memory has unknown (i.e. varying) swizzling, we pin the |
| 272 | * pages to prevent them being swapped out and causing corruption |
| 273 | * due to the change in swizzling. |
| 274 | */ |
| 275 | mutex_lock(&obj->mm.lock); |
| 276 | if (obj->mm.pages && |
| 277 | obj->mm.madv == I915_MADV_WILLNEED && |
| 278 | i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) { |
| 279 | if (tiling == I915_TILING_NONE) { |
| 280 | GEM_BUG_ON(!obj->mm.quirked); |
| 281 | __i915_gem_object_unpin_pages(obj); |
| 282 | obj->mm.quirked = false; |
| 283 | } |
| 284 | if (!i915_gem_object_is_tiled(obj)) { |
| 285 | GEM_BUG_ON(!obj->mm.quirked); |
| 286 | __i915_gem_object_pin_pages(obj); |
| 287 | obj->mm.quirked = true; |
| 288 | } |
| 289 | } |
| 290 | mutex_unlock(&obj->mm.lock); |
| 291 | |
| 292 | list_for_each_entry(vma, &obj->vma_list, obj_link) { |
| 293 | if (!i915_vma_is_ggtt(vma)) |
| 294 | break; |
| 295 | |
| 296 | vma->fence_size = |
| 297 | i915_gem_fence_size(i915, vma->size, tiling, stride); |
| 298 | vma->fence_alignment = |
| 299 | i915_gem_fence_alignment(i915, |
| 300 | vma->size, tiling, stride); |
| 301 | |
| 302 | if (vma->fence) |
| 303 | vma->fence->dirty = true; |
| 304 | } |
| 305 | |
| 306 | obj->tiling_and_stride = tiling | stride; |
| 307 | |
| 308 | /* Force the fence to be reacquired for GTT access */ |
| 309 | i915_gem_release_mmap(obj); |
| 310 | |
| 311 | /* Try to preallocate memory required to save swizzling on put-pages */ |
| 312 | if (i915_gem_object_needs_bit17_swizzle(obj)) { |
| 313 | if (!obj->bit_17) { |
| 314 | obj->bit_17 = kcalloc(BITS_TO_LONGS(obj->base.size >> PAGE_SHIFT), |
| 315 | sizeof(long), GFP_KERNEL); |
| 316 | } |
| 317 | } else { |
| 318 | kfree(obj->bit_17); |
| 319 | obj->bit_17 = NULL; |
| 320 | } |
| 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 325 | /** |
Chris Wilson | 111dbca | 2017-01-10 12:10:44 +0000 | [diff] [blame] | 326 | * i915_gem_set_tiling_ioctl - IOCTL handler to set tiling mode |
Daniel Vetter | 3271dca | 2015-07-24 17:40:15 +0200 | [diff] [blame] | 327 | * @dev: DRM device |
| 328 | * @data: data pointer for the ioctl |
| 329 | * @file: DRM file for the ioctl call |
| 330 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 331 | * Sets the tiling mode of an object, returning the required swizzling of |
| 332 | * bit 6 of addresses in the object. |
Daniel Vetter | 3271dca | 2015-07-24 17:40:15 +0200 | [diff] [blame] | 333 | * |
| 334 | * Called by the user via ioctl. |
| 335 | * |
| 336 | * Returns: |
| 337 | * Zero on success, negative errno on failure. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 338 | */ |
| 339 | int |
Chris Wilson | 111dbca | 2017-01-10 12:10:44 +0000 | [diff] [blame] | 340 | i915_gem_set_tiling_ioctl(struct drm_device *dev, void *data, |
| 341 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 342 | { |
| 343 | struct drm_i915_gem_set_tiling *args = data; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 344 | struct drm_i915_gem_object *obj; |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 345 | int err; |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 346 | |
Chris Wilson | 03ac064 | 2016-07-20 13:31:51 +0100 | [diff] [blame] | 347 | obj = i915_gem_object_lookup(file, args->handle); |
| 348 | if (!obj) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 349 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 350 | |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 351 | if (!i915_tiling_ok(obj, args->tiling_mode, args->stride)) { |
| 352 | err = -EINVAL; |
Chris Wilson | 6c31a61 | 2015-02-12 07:53:18 +0000 | [diff] [blame] | 353 | goto err; |
Daniel Vetter | 31770bd | 2010-04-23 23:01:01 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 356 | if (args->tiling_mode == I915_TILING_NONE) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 357 | args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 358 | args->stride = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 359 | } else { |
| 360 | if (args->tiling_mode == I915_TILING_X) |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 361 | args->swizzle_mode = to_i915(dev)->mm.bit_6_swizzle_x; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 362 | else |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 363 | args->swizzle_mode = to_i915(dev)->mm.bit_6_swizzle_y; |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 364 | |
| 365 | /* Hide bit 17 swizzling from the user. This prevents old Mesa |
| 366 | * from aborting the application on sw fallbacks to bit 17, |
| 367 | * and we use the pread/pwrite bit17 paths to swizzle for it. |
| 368 | * If there was a user that was relying on the swizzle |
| 369 | * information for drm_intel_bo_map()ed reads/writes this would |
| 370 | * break it, but we don't have any of those. |
| 371 | */ |
| 372 | if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17) |
| 373 | args->swizzle_mode = I915_BIT_6_SWIZZLE_9; |
| 374 | if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17) |
| 375 | args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10; |
| 376 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 377 | /* If we can't handle the swizzling, make it untiled. */ |
| 378 | if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) { |
| 379 | args->tiling_mode = I915_TILING_NONE; |
| 380 | args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 381 | args->stride = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 384 | |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 385 | err = mutex_lock_interruptible(&dev->struct_mutex); |
| 386 | if (err) |
| 387 | goto err; |
Chris Wilson | 467cffb | 2011-03-07 10:42:03 +0000 | [diff] [blame] | 388 | |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 389 | err = i915_gem_object_set_tiling(obj, args->tiling_mode, args->stride); |
| 390 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 49ef529 | 2016-08-18 17:17:00 +0100 | [diff] [blame] | 391 | |
Chris Wilson | 957870f | 2017-01-10 12:10:45 +0000 | [diff] [blame] | 392 | /* We have to maintain this existing ABI... */ |
Chris Wilson | 3e510a8 | 2016-08-05 10:14:23 +0100 | [diff] [blame] | 393 | args->stride = i915_gem_object_get_stride(obj); |
| 394 | args->tiling_mode = i915_gem_object_get_tiling(obj); |
Chris Wilson | e9b73c6 | 2012-12-03 21:03:14 +0000 | [diff] [blame] | 395 | |
Chris Wilson | 6c31a61 | 2015-02-12 07:53:18 +0000 | [diff] [blame] | 396 | err: |
Chris Wilson | f8c417c | 2016-07-20 13:31:53 +0100 | [diff] [blame] | 397 | i915_gem_object_put(obj); |
Chris Wilson | f23eda8 | 2016-08-15 10:48:53 +0100 | [diff] [blame] | 398 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /** |
Chris Wilson | 111dbca | 2017-01-10 12:10:44 +0000 | [diff] [blame] | 402 | * i915_gem_get_tiling_ioctl - IOCTL handler to get tiling mode |
Daniel Vetter | 3271dca | 2015-07-24 17:40:15 +0200 | [diff] [blame] | 403 | * @dev: DRM device |
| 404 | * @data: data pointer for the ioctl |
| 405 | * @file: DRM file for the ioctl call |
| 406 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 407 | * Returns the current tiling mode and required bit 6 swizzling for the object. |
Daniel Vetter | 3271dca | 2015-07-24 17:40:15 +0200 | [diff] [blame] | 408 | * |
| 409 | * Called by the user via ioctl. |
| 410 | * |
| 411 | * Returns: |
| 412 | * Zero on success, negative errno on failure. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 413 | */ |
| 414 | int |
Chris Wilson | 111dbca | 2017-01-10 12:10:44 +0000 | [diff] [blame] | 415 | i915_gem_get_tiling_ioctl(struct drm_device *dev, void *data, |
| 416 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 417 | { |
| 418 | struct drm_i915_gem_get_tiling *args = data; |
Chris Wilson | fac5e23 | 2016-07-04 11:34:36 +0100 | [diff] [blame] | 419 | struct drm_i915_private *dev_priv = to_i915(dev); |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 420 | struct drm_i915_gem_object *obj; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 421 | int err = -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 422 | |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 423 | rcu_read_lock(); |
| 424 | obj = i915_gem_object_lookup_rcu(file, args->handle); |
| 425 | if (obj) { |
| 426 | args->tiling_mode = |
| 427 | READ_ONCE(obj->tiling_and_stride) & TILING_MASK; |
| 428 | err = 0; |
| 429 | } |
| 430 | rcu_read_unlock(); |
| 431 | if (unlikely(err)) |
| 432 | return err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 433 | |
Chris Wilson | 9ad3676 | 2016-08-05 10:14:21 +0100 | [diff] [blame] | 434 | switch (args->tiling_mode) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 435 | case I915_TILING_X: |
| 436 | args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x; |
| 437 | break; |
| 438 | case I915_TILING_Y: |
| 439 | args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y; |
| 440 | break; |
Chris Wilson | fbbd37b | 2016-10-28 13:58:42 +0100 | [diff] [blame] | 441 | default: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 442 | case I915_TILING_NONE: |
| 443 | args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE; |
| 444 | break; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 445 | } |
| 446 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 447 | /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */ |
Chris Wilson | 5eb3e5a | 2015-06-28 09:19:26 +0100 | [diff] [blame] | 448 | if (dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) |
| 449 | args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN; |
| 450 | else |
| 451 | args->phys_swizzle_mode = args->swizzle_mode; |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 452 | if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17) |
| 453 | args->swizzle_mode = I915_BIT_6_SWIZZLE_9; |
| 454 | if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17) |
| 455 | args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10; |
| 456 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 457 | return 0; |
| 458 | } |