blob: 59c010d8f47be5a9b516c832febcb00eb86a5cdd [file] [log] [blame]
Eric Anholt673a3942008-07-30 12:06:12 -07001/*
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 Howells760285e2012-10-02 18:01:07 +010028#include <linux/string.h>
29#include <linux/bitops.h>
30#include <drm/drmP.h>
31#include <drm/i915_drm.h>
Eric Anholt673a3942008-07-30 12:06:12 -070032#include "i915_drv.h"
33
Daniel Vetter3271dca2015-07-24 17:40:15 +020034/**
35 * DOC: buffer object tiling
Eric Anholt673a3942008-07-30 12:06:12 -070036 *
Chris Wilson111dbca2017-01-10 12:10:44 +000037 * i915_gem_set_tiling_ioctl() and i915_gem_get_tiling_ioctl() is the userspace
38 * interface to declare fence register requirements.
Eric Anholt673a3942008-07-30 12:06:12 -070039 *
Daniel Vetter3271dca2015-07-24 17:40:15 +020040 * 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 Anholt673a3942008-07-30 12:06:12 -070043 *
Daniel Vetter3271dca2015-07-24 17:40:15 +020044 * - 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 Anholt673a3942008-07-30 12:06:12 -070054 *
Daniel Vetter3271dca2015-07-24 17:40:15 +020055 * 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 Anholt673a3942008-07-30 12:06:12 -070059 */
60
Chris Wilson91d4e0aa2017-01-09 16:16:13 +000061/**
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 */
71u32 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);
85 GEM_BUG_ON(stride & 4095);
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 */
111u32 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 */
120 if (INTEL_GEN(i915) >= 4 || tiling == I915_TILING_NONE)
121 return 4096;
122
123 /*
124 * Previous chips need to be aligned to the size of the smallest
125 * fence register that can contain the object.
126 */
127 return i915_gem_fence_size(i915, size, tiling, stride);
128}
129
Jesse Barnes0f973f22009-01-26 17:10:45 -0800130/* Check pitch constriants for all chips & tiling formats */
Chris Wilsona00b10c2010-09-24 21:15:47 +0100131static bool
Tvrtko Ursulin118bb9f2016-11-16 08:55:36 +0000132i915_tiling_ok(struct drm_i915_private *dev_priv,
133 int stride, int size, int tiling_mode)
Jesse Barnes0f973f22009-01-26 17:10:45 -0800134{
Chris Wilson0ee537a2011-03-06 09:03:16 +0000135 int tile_width;
Jesse Barnes0f973f22009-01-26 17:10:45 -0800136
137 /* Linear is always fine */
138 if (tiling_mode == I915_TILING_NONE)
139 return true;
140
Chris Wilsondeeb1512016-08-05 10:14:22 +0100141 if (tiling_mode > I915_TILING_LAST)
142 return false;
143
Tvrtko Ursulin5db94012016-10-13 11:03:10 +0100144 if (IS_GEN2(dev_priv) ||
Tvrtko Ursulin50a0bc92016-10-13 11:02:58 +0100145 (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev_priv)))
Jesse Barnes0f973f22009-01-26 17:10:45 -0800146 tile_width = 128;
147 else
148 tile_width = 512;
149
Daniel Vetter8d7773a2009-03-29 14:09:41 +0200150 /* check maximum stride & object size */
Ville Syrjälä3a062472013-04-09 11:45:05 +0300151 /* i965+ stores the end address of the gtt mapping in the fence
152 * reg, so dont bother to check the size */
Tvrtko Ursulin118bb9f2016-11-16 08:55:36 +0000153 if (INTEL_GEN(dev_priv) >= 7) {
Ville Syrjälä3a062472013-04-09 11:45:05 +0300154 if (stride / 128 > GEN7_FENCE_MAX_PITCH_VAL)
155 return false;
Tvrtko Ursulin118bb9f2016-11-16 08:55:36 +0000156 } else if (INTEL_GEN(dev_priv) >= 4) {
Daniel Vetter8d7773a2009-03-29 14:09:41 +0200157 if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
158 return false;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100159 } else {
Daniel Vetterc36a2a62010-04-17 15:12:03 +0200160 if (stride > 8192)
Daniel Vetter8d7773a2009-03-29 14:09:41 +0200161 return false;
Eric Anholte76a16d2009-05-26 17:44:56 -0700162
Tvrtko Ursulin5db94012016-10-13 11:03:10 +0100163 if (IS_GEN3(dev_priv)) {
Daniel Vetterc36a2a62010-04-17 15:12:03 +0200164 if (size > I830_FENCE_MAX_SIZE_VAL << 20)
165 return false;
166 } else {
167 if (size > I830_FENCE_MAX_SIZE_VAL << 19)
168 return false;
169 }
Daniel Vetter8d7773a2009-03-29 14:09:41 +0200170 }
171
Ville Syrjäläfe48d8d2013-04-09 20:09:13 +0300172 if (stride < tile_width)
173 return false;
174
Jesse Barnes0f973f22009-01-26 17:10:45 -0800175 /* 965+ just needs multiples of tile width */
Tvrtko Ursulin118bb9f2016-11-16 08:55:36 +0000176 if (INTEL_GEN(dev_priv) >= 4) {
Jesse Barnes0f973f22009-01-26 17:10:45 -0800177 if (stride & (tile_width - 1))
178 return false;
179 return true;
180 }
181
182 /* Pre-965 needs power of two tile widths */
Jesse Barnes0f973f22009-01-26 17:10:45 -0800183 if (stride & (stride - 1))
184 return false;
185
Jesse Barnes0f973f22009-01-26 17:10:45 -0800186 return true;
187}
188
Chris Wilson5b306942017-01-09 16:16:09 +0000189static bool i915_vma_fence_prepare(struct i915_vma *vma,
190 int tiling_mode, unsigned int stride)
Chris Wilson49ef5292016-08-18 17:17:00 +0100191{
Chris Wilson944397f2017-01-09 16:16:11 +0000192 struct drm_i915_private *i915 = vma->vm->i915;
193 u32 size, alignment;
Chris Wilson49ef5292016-08-18 17:17:00 +0100194
195 if (!i915_vma_is_map_and_fenceable(vma))
196 return true;
197
Chris Wilson91d4e0aa2017-01-09 16:16:13 +0000198 size = i915_gem_fence_size(i915, vma->size, tiling_mode, stride);
Chris Wilson49ef5292016-08-18 17:17:00 +0100199 if (vma->node.size < size)
200 return false;
201
Chris Wilson91d4e0aa2017-01-09 16:16:13 +0000202 alignment = i915_gem_fence_alignment(i915, vma->size, tiling_mode, stride);
Chris Wilson944397f2017-01-09 16:16:11 +0000203 if (vma->node.start & (alignment - 1))
Chris Wilson49ef5292016-08-18 17:17:00 +0100204 return false;
205
206 return true;
207}
208
Chris Wilsonf23eda82016-08-15 10:48:53 +0100209/* Make the current GTT allocation valid for the change in tiling. */
210static int
Chris Wilson5b306942017-01-09 16:16:09 +0000211i915_gem_object_fence_prepare(struct drm_i915_gem_object *obj,
212 int tiling_mode, unsigned int stride)
Chris Wilson52dc7d32009-06-06 09:46:01 +0100213{
Chris Wilsonf23eda82016-08-15 10:48:53 +0100214 struct i915_vma *vma;
Chris Wilson49ef5292016-08-18 17:17:00 +0100215 int ret;
Chris Wilson52dc7d32009-06-06 09:46:01 +0100216
217 if (tiling_mode == I915_TILING_NONE)
Chris Wilsonf23eda82016-08-15 10:48:53 +0100218 return 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +0100219
Chris Wilson49ef5292016-08-18 17:17:00 +0100220 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilson944397f2017-01-09 16:16:11 +0000221 if (!i915_vma_is_ggtt(vma))
222 break;
223
Chris Wilson5b306942017-01-09 16:16:09 +0000224 if (i915_vma_fence_prepare(vma, tiling_mode, stride))
Chris Wilson49ef5292016-08-18 17:17:00 +0100225 continue;
Chris Wilsonf23eda82016-08-15 10:48:53 +0100226
Chris Wilson49ef5292016-08-18 17:17:00 +0100227 ret = i915_vma_unbind(vma);
228 if (ret)
229 return ret;
Chris Wilsondf153152010-11-15 05:25:58 +0000230 }
231
Chris Wilsonf23eda82016-08-15 10:48:53 +0100232 return 0;
Chris Wilson52dc7d32009-06-06 09:46:01 +0100233}
234
Eric Anholt673a3942008-07-30 12:06:12 -0700235/**
Chris Wilson111dbca2017-01-10 12:10:44 +0000236 * i915_gem_set_tiling_ioctl - IOCTL handler to set tiling mode
Daniel Vetter3271dca2015-07-24 17:40:15 +0200237 * @dev: DRM device
238 * @data: data pointer for the ioctl
239 * @file: DRM file for the ioctl call
240 *
Eric Anholt673a3942008-07-30 12:06:12 -0700241 * Sets the tiling mode of an object, returning the required swizzling of
242 * bit 6 of addresses in the object.
Daniel Vetter3271dca2015-07-24 17:40:15 +0200243 *
244 * Called by the user via ioctl.
245 *
246 * Returns:
247 * Zero on success, negative errno on failure.
Eric Anholt673a3942008-07-30 12:06:12 -0700248 */
249int
Chris Wilson111dbca2017-01-10 12:10:44 +0000250i915_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
251 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700252{
253 struct drm_i915_gem_set_tiling *args = data;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100254 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson05394f32010-11-08 19:18:58 +0000255 struct drm_i915_gem_object *obj;
Chris Wilsonf23eda82016-08-15 10:48:53 +0100256 int err = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700257
Chris Wilson3e510a82016-08-05 10:14:23 +0100258 /* Make sure we don't cross-contaminate obj->tiling_and_stride */
259 BUILD_BUG_ON(I915_TILING_LAST & STRIDE_MASK);
260
Chris Wilson03ac0642016-07-20 13:31:51 +0100261 obj = i915_gem_object_lookup(file, args->handle);
262 if (!obj)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100263 return -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700264
Tvrtko Ursulin118bb9f2016-11-16 08:55:36 +0000265 if (!i915_tiling_ok(dev_priv,
Chris Wilson05394f32010-11-08 19:18:58 +0000266 args->stride, obj->base.size, args->tiling_mode)) {
Chris Wilsonf0cd5182016-10-28 13:58:43 +0100267 i915_gem_object_put(obj);
Jesse Barnes0f973f22009-01-26 17:10:45 -0800268 return -EINVAL;
Chris Wilson72daad42009-01-30 21:10:22 +0000269 }
Jesse Barnes0f973f22009-01-26 17:10:45 -0800270
Chris Wilson6c31a612015-02-12 07:53:18 +0000271 mutex_lock(&dev->struct_mutex);
Chris Wilson1f30a612015-04-15 16:39:59 +0100272 if (obj->pin_display || obj->framebuffer_references) {
Chris Wilsonf23eda82016-08-15 10:48:53 +0100273 err = -EBUSY;
Chris Wilson6c31a612015-02-12 07:53:18 +0000274 goto err;
Daniel Vetter31770bd2010-04-23 23:01:01 +0200275 }
276
Eric Anholt673a3942008-07-30 12:06:12 -0700277 if (args->tiling_mode == I915_TILING_NONE) {
Eric Anholt673a3942008-07-30 12:06:12 -0700278 args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
Chris Wilson52dc7d32009-06-06 09:46:01 +0100279 args->stride = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700280 } else {
281 if (args->tiling_mode == I915_TILING_X)
282 args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
283 else
284 args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
Eric Anholt280b7132009-03-12 16:56:27 -0700285
286 /* Hide bit 17 swizzling from the user. This prevents old Mesa
287 * from aborting the application on sw fallbacks to bit 17,
288 * and we use the pread/pwrite bit17 paths to swizzle for it.
289 * If there was a user that was relying on the swizzle
290 * information for drm_intel_bo_map()ed reads/writes this would
291 * break it, but we don't have any of those.
292 */
293 if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
294 args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
295 if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
296 args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
297
Eric Anholt673a3942008-07-30 12:06:12 -0700298 /* If we can't handle the swizzling, make it untiled. */
299 if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
300 args->tiling_mode = I915_TILING_NONE;
301 args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
Chris Wilson52dc7d32009-06-06 09:46:01 +0100302 args->stride = 0;
Eric Anholt673a3942008-07-30 12:06:12 -0700303 }
304 }
Jesse Barnes0f973f22009-01-26 17:10:45 -0800305
Chris Wilson3e510a82016-08-05 10:14:23 +0100306 if (args->tiling_mode != i915_gem_object_get_tiling(obj) ||
307 args->stride != i915_gem_object_get_stride(obj)) {
Chris Wilson52dc7d32009-06-06 09:46:01 +0100308 /* We need to rebind the object if its current allocation
309 * no longer meets the alignment restrictions for its new
310 * tiling mode. Otherwise we can just leave it alone, but
Chris Wilson1869b622012-04-21 16:23:24 +0100311 * need to ensure that any fence register is updated before
312 * the next fenced (either through the GTT or by the BLT unit
313 * on older GPUs) access.
Chris Wilson5d82e3e2012-04-21 16:23:23 +0100314 *
315 * After updating the tiling parameters, we then flag whether
316 * we need to update an associated fence register. Note this
317 * has to also include the unfenced register the GPU uses
318 * whilst executing a fenced command for an untiled object.
Jesse Barnes0f973f22009-01-26 17:10:45 -0800319 */
Chris Wilson467cffb2011-03-07 10:42:03 +0000320
Chris Wilson5b306942017-01-09 16:16:09 +0000321 err = i915_gem_object_fence_prepare(obj,
322 args->tiling_mode,
323 args->stride);
Chris Wilsonf23eda82016-08-15 10:48:53 +0100324 if (!err) {
Chris Wilson49ef5292016-08-18 17:17:00 +0100325 struct i915_vma *vma;
326
Chris Wilson1233e2d2016-10-28 13:58:37 +0100327 mutex_lock(&obj->mm.lock);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100328 if (obj->mm.pages &&
329 obj->mm.madv == I915_MADV_WILLNEED &&
Daniel Vetter656bfa32014-11-20 09:26:30 +0100330 dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
Chris Wilsonbc0629a2016-11-01 10:03:17 +0000331 if (args->tiling_mode == I915_TILING_NONE) {
332 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100333 __i915_gem_object_unpin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +0000334 obj->mm.quirked = false;
335 }
336 if (!i915_gem_object_is_tiled(obj)) {
Chris Wilson2c3a3f42016-11-04 10:30:01 +0000337 GEM_BUG_ON(!obj->mm.quirked);
Chris Wilsona4f5ea62016-10-28 13:58:35 +0100338 __i915_gem_object_pin_pages(obj);
Chris Wilsonbc0629a2016-11-01 10:03:17 +0000339 obj->mm.quirked = true;
340 }
Daniel Vetter656bfa32014-11-20 09:26:30 +0100341 }
Chris Wilson1233e2d2016-10-28 13:58:37 +0100342 mutex_unlock(&obj->mm.lock);
Daniel Vetter656bfa32014-11-20 09:26:30 +0100343
Chris Wilson49ef5292016-08-18 17:17:00 +0100344 list_for_each_entry(vma, &obj->vma_list, obj_link) {
Chris Wilson944397f2017-01-09 16:16:11 +0000345 if (!i915_vma_is_ggtt(vma))
346 break;
Chris Wilson5d82e3e2012-04-21 16:23:23 +0100347
Chris Wilson91d4e0aa2017-01-09 16:16:13 +0000348 vma->fence_size = i915_gem_fence_size(dev_priv, vma->size,
349 args->tiling_mode,
350 args->stride);
351 vma->fence_alignment = i915_gem_fence_alignment(dev_priv, vma->size,
352 args->tiling_mode,
353 args->stride);
Chris Wilson944397f2017-01-09 16:16:11 +0000354
355 if (vma->fence)
356 vma->fence->dirty = true;
Chris Wilson49ef5292016-08-18 17:17:00 +0100357 }
Chris Wilson3e510a82016-08-05 10:14:23 +0100358 obj->tiling_and_stride =
359 args->stride | args->tiling_mode;
Chris Wilson1869b622012-04-21 16:23:24 +0100360
361 /* Force the fence to be reacquired for GTT access */
362 i915_gem_release_mmap(obj);
Chris Wilson467cffb2011-03-07 10:42:03 +0000363 }
Chris Wilson52dc7d32009-06-06 09:46:01 +0100364 }
Chris Wilson467cffb2011-03-07 10:42:03 +0000365 /* we have to maintain this existing ABI... */
Chris Wilson3e510a82016-08-05 10:14:23 +0100366 args->stride = i915_gem_object_get_stride(obj);
367 args->tiling_mode = i915_gem_object_get_tiling(obj);
Chris Wilsone9b73c62012-12-03 21:03:14 +0000368
369 /* Try to preallocate memory required to save swizzling on put-pages */
370 if (i915_gem_object_needs_bit17_swizzle(obj)) {
371 if (obj->bit_17 == NULL) {
Daniel Vettera1e22652013-09-21 00:35:38 +0200372 obj->bit_17 = kcalloc(BITS_TO_LONGS(obj->base.size >> PAGE_SHIFT),
Chris Wilsone9b73c62012-12-03 21:03:14 +0000373 sizeof(long), GFP_KERNEL);
374 }
375 } else {
376 kfree(obj->bit_17);
377 obj->bit_17 = NULL;
378 }
379
Chris Wilson6c31a612015-02-12 07:53:18 +0000380err:
Chris Wilsonf8c417c2016-07-20 13:31:53 +0100381 i915_gem_object_put(obj);
Chris Wilsond6873102009-02-08 19:07:51 +0000382 mutex_unlock(&dev->struct_mutex);
Eric Anholt673a3942008-07-30 12:06:12 -0700383
Chris Wilsonf23eda82016-08-15 10:48:53 +0100384 return err;
Eric Anholt673a3942008-07-30 12:06:12 -0700385}
386
387/**
Chris Wilson111dbca2017-01-10 12:10:44 +0000388 * i915_gem_get_tiling_ioctl - IOCTL handler to get tiling mode
Daniel Vetter3271dca2015-07-24 17:40:15 +0200389 * @dev: DRM device
390 * @data: data pointer for the ioctl
391 * @file: DRM file for the ioctl call
392 *
Eric Anholt673a3942008-07-30 12:06:12 -0700393 * Returns the current tiling mode and required bit 6 swizzling for the object.
Daniel Vetter3271dca2015-07-24 17:40:15 +0200394 *
395 * Called by the user via ioctl.
396 *
397 * Returns:
398 * Zero on success, negative errno on failure.
Eric Anholt673a3942008-07-30 12:06:12 -0700399 */
400int
Chris Wilson111dbca2017-01-10 12:10:44 +0000401i915_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
402 struct drm_file *file)
Eric Anholt673a3942008-07-30 12:06:12 -0700403{
404 struct drm_i915_gem_get_tiling *args = data;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100405 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson05394f32010-11-08 19:18:58 +0000406 struct drm_i915_gem_object *obj;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100407 int err = -ENOENT;
Eric Anholt673a3942008-07-30 12:06:12 -0700408
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100409 rcu_read_lock();
410 obj = i915_gem_object_lookup_rcu(file, args->handle);
411 if (obj) {
412 args->tiling_mode =
413 READ_ONCE(obj->tiling_and_stride) & TILING_MASK;
414 err = 0;
415 }
416 rcu_read_unlock();
417 if (unlikely(err))
418 return err;
Eric Anholt673a3942008-07-30 12:06:12 -0700419
Chris Wilson9ad36762016-08-05 10:14:21 +0100420 switch (args->tiling_mode) {
Eric Anholt673a3942008-07-30 12:06:12 -0700421 case I915_TILING_X:
422 args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
423 break;
424 case I915_TILING_Y:
425 args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
426 break;
Chris Wilsonfbbd37b2016-10-28 13:58:42 +0100427 default:
Eric Anholt673a3942008-07-30 12:06:12 -0700428 case I915_TILING_NONE:
429 args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
430 break;
Eric Anholt673a3942008-07-30 12:06:12 -0700431 }
432
Eric Anholt280b7132009-03-12 16:56:27 -0700433 /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
Chris Wilson5eb3e5a2015-06-28 09:19:26 +0100434 if (dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
435 args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN;
436 else
437 args->phys_swizzle_mode = args->swizzle_mode;
Eric Anholt280b7132009-03-12 16:56:27 -0700438 if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
439 args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
440 if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
441 args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
442
Eric Anholt673a3942008-07-30 12:06:12 -0700443 return 0;
444}