xref: /dflybsd-src/sys/dev/drm/i915/i915_gem_tiling.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1e3adcf8fSFrançois Tigeot /*
2e3adcf8fSFrançois Tigeot  * Copyright © 2008 Intel Corporation
3e3adcf8fSFrançois Tigeot  *
4e3adcf8fSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5e3adcf8fSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6e3adcf8fSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7e3adcf8fSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8e3adcf8fSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9e3adcf8fSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10e3adcf8fSFrançois Tigeot  *
11e3adcf8fSFrançois Tigeot  * The above copyright notice and this permission notice (including the next
12e3adcf8fSFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
13e3adcf8fSFrançois Tigeot  * Software.
14e3adcf8fSFrançois Tigeot  *
15e3adcf8fSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16e3adcf8fSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17e3adcf8fSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18e3adcf8fSFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19e3adcf8fSFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20e3adcf8fSFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21e3adcf8fSFrançois Tigeot  * IN THE SOFTWARE.
22e3adcf8fSFrançois Tigeot  *
23e3adcf8fSFrançois Tigeot  * Authors:
24e3adcf8fSFrançois Tigeot  *    Eric Anholt <eric@anholt.net>
25e3adcf8fSFrançois Tigeot  *
26e3adcf8fSFrançois Tigeot  */
27e3adcf8fSFrançois Tigeot 
281487f786SFrançois Tigeot #include <linux/string.h>
29e3440f96SFrançois Tigeot #include <linux/bitops.h>
3018e26a6dSFrançois Tigeot #include <drm/drmP.h>
315c6c6f23SFrançois Tigeot #include <drm/i915_drm.h>
32e3adcf8fSFrançois Tigeot #include "i915_drv.h"
33e3adcf8fSFrançois Tigeot 
34e3adcf8fSFrançois Tigeot /**
35a05eeebfSFrançois Tigeot  * DOC: buffer object tiling
361b13d190SFrançois Tigeot  *
37a85cb24fSFrançois Tigeot  * i915_gem_set_tiling_ioctl() and i915_gem_get_tiling_ioctl() is the userspace
38a85cb24fSFrançois Tigeot  * interface to declare fence register requirements.
39e3adcf8fSFrançois Tigeot  *
40a05eeebfSFrançois Tigeot  * In principle GEM doesn't care at all about the internal data layout of an
41a05eeebfSFrançois Tigeot  * object, and hence it also doesn't care about tiling or swizzling. There's two
42a05eeebfSFrançois Tigeot  * exceptions:
43e3adcf8fSFrançois Tigeot  *
44a05eeebfSFrançois Tigeot  * - For X and Y tiling the hardware provides detilers for CPU access, so called
45a05eeebfSFrançois Tigeot  *   fences. Since there's only a limited amount of them the kernel must manage
46a05eeebfSFrançois Tigeot  *   these, and therefore userspace must tell the kernel the object tiling if it
47a05eeebfSFrançois Tigeot  *   wants to use fences for detiling.
48a05eeebfSFrançois Tigeot  * - On gen3 and gen4 platforms have a swizzling pattern for tiled objects which
49a05eeebfSFrançois Tigeot  *   depends upon the physical page frame number. When swapping such objects the
50a05eeebfSFrançois Tigeot  *   page frame number might change and the kernel must be able to fix this up
51a05eeebfSFrançois Tigeot  *   and hence now the tiling. Note that on a subset of platforms with
52a05eeebfSFrançois Tigeot  *   asymmetric memory channel population the swizzling pattern changes in an
53a05eeebfSFrançois Tigeot  *   unknown way, and for those the kernel simply forbids swapping completely.
54a05eeebfSFrançois Tigeot  *
55a05eeebfSFrançois Tigeot  * Since neither of this applies for new tiling layouts on modern platforms like
56a05eeebfSFrançois Tigeot  * W, Ys and Yf tiling GEM only allows object tiling to be set to X or Y tiled.
57a05eeebfSFrançois Tigeot  * Anything else can be handled in userspace entirely without the kernel's
58a05eeebfSFrançois Tigeot  * invovlement.
59e3adcf8fSFrançois Tigeot  */
60e3adcf8fSFrançois Tigeot 
61a85cb24fSFrançois Tigeot /**
62a85cb24fSFrançois Tigeot  * i915_gem_fence_size - required global GTT size for a fence
63a85cb24fSFrançois Tigeot  * @i915: i915 device
64a85cb24fSFrançois Tigeot  * @size: object size
65a85cb24fSFrançois Tigeot  * @tiling: tiling mode
66a85cb24fSFrançois Tigeot  * @stride: tiling stride
67a85cb24fSFrançois Tigeot  *
68a85cb24fSFrançois Tigeot  * Return the required global GTT size for a fence (view of a tiled object),
69a85cb24fSFrançois Tigeot  * taking into account potential fence register mapping.
70a85cb24fSFrançois Tigeot  */
i915_gem_fence_size(struct drm_i915_private * i915,u32 size,unsigned int tiling,unsigned int stride)71a85cb24fSFrançois Tigeot u32 i915_gem_fence_size(struct drm_i915_private *i915,
72a85cb24fSFrançois Tigeot 			u32 size, unsigned int tiling, unsigned int stride)
73a85cb24fSFrançois Tigeot {
74a85cb24fSFrançois Tigeot 	u32 ggtt_size;
75a85cb24fSFrançois Tigeot 
76a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!size);
77a85cb24fSFrançois Tigeot 
78a85cb24fSFrançois Tigeot 	if (tiling == I915_TILING_NONE)
79a85cb24fSFrançois Tigeot 		return size;
80a85cb24fSFrançois Tigeot 
81a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!stride);
82a85cb24fSFrançois Tigeot 
83a85cb24fSFrançois Tigeot 	if (INTEL_GEN(i915) >= 4) {
84a85cb24fSFrançois Tigeot 		stride *= i915_gem_tile_height(tiling);
85a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!IS_ALIGNED(stride, I965_FENCE_PAGE));
86a85cb24fSFrançois Tigeot 		return roundup(size, stride);
87a85cb24fSFrançois Tigeot 	}
88a85cb24fSFrançois Tigeot 
89a85cb24fSFrançois Tigeot 	/* Previous chips need a power-of-two fence region when tiling */
90a85cb24fSFrançois Tigeot 	if (IS_GEN3(i915))
91a85cb24fSFrançois Tigeot 		ggtt_size = 1024*1024;
92a85cb24fSFrançois Tigeot 	else
93a85cb24fSFrançois Tigeot 		ggtt_size = 512*1024;
94a85cb24fSFrançois Tigeot 
95a85cb24fSFrançois Tigeot 	while (ggtt_size < size)
96a85cb24fSFrançois Tigeot 		ggtt_size <<= 1;
97a85cb24fSFrançois Tigeot 
98a85cb24fSFrançois Tigeot 	return ggtt_size;
99a85cb24fSFrançois Tigeot }
100a85cb24fSFrançois Tigeot 
101a85cb24fSFrançois Tigeot /**
102a85cb24fSFrançois Tigeot  * i915_gem_fence_alignment - required global GTT alignment for a fence
103a85cb24fSFrançois Tigeot  * @i915: i915 device
104a85cb24fSFrançois Tigeot  * @size: object size
105a85cb24fSFrançois Tigeot  * @tiling: tiling mode
106a85cb24fSFrançois Tigeot  * @stride: tiling stride
107a85cb24fSFrançois Tigeot  *
108a85cb24fSFrançois Tigeot  * Return the required global GTT alignment for a fence (a view of a tiled
109a85cb24fSFrançois Tigeot  * object), taking into account potential fence register mapping.
110a85cb24fSFrançois Tigeot  */
i915_gem_fence_alignment(struct drm_i915_private * i915,u32 size,unsigned int tiling,unsigned int stride)111a85cb24fSFrançois Tigeot u32 i915_gem_fence_alignment(struct drm_i915_private *i915, u32 size,
112a85cb24fSFrançois Tigeot 			     unsigned int tiling, unsigned int stride)
113a85cb24fSFrançois Tigeot {
114a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!size);
115a85cb24fSFrançois Tigeot 
116a85cb24fSFrançois Tigeot 	/*
117a85cb24fSFrançois Tigeot 	 * Minimum alignment is 4k (GTT page size), but might be greater
118a85cb24fSFrançois Tigeot 	 * if a fence register is needed for the object.
119a85cb24fSFrançois Tigeot 	 */
120a85cb24fSFrançois Tigeot 	if (tiling == I915_TILING_NONE)
121a85cb24fSFrançois Tigeot 		return I915_GTT_MIN_ALIGNMENT;
122a85cb24fSFrançois Tigeot 
123a85cb24fSFrançois Tigeot 	if (INTEL_GEN(i915) >= 4)
124a85cb24fSFrançois Tigeot 		return I965_FENCE_PAGE;
125a85cb24fSFrançois Tigeot 
126a85cb24fSFrançois Tigeot 	/*
127a85cb24fSFrançois Tigeot 	 * Previous chips need to be aligned to the size of the smallest
128a85cb24fSFrançois Tigeot 	 * fence register that can contain the object.
129a85cb24fSFrançois Tigeot 	 */
130a85cb24fSFrançois Tigeot 	return i915_gem_fence_size(i915, size, tiling, stride);
131a85cb24fSFrançois Tigeot }
132a85cb24fSFrançois Tigeot 
133e3adcf8fSFrançois Tigeot /* Check pitch constriants for all chips & tiling formats */
134e3adcf8fSFrançois Tigeot static bool
i915_tiling_ok(struct drm_i915_gem_object * obj,unsigned int tiling,unsigned int stride)135a85cb24fSFrançois Tigeot i915_tiling_ok(struct drm_i915_gem_object *obj,
136a85cb24fSFrançois Tigeot 	       unsigned int tiling, unsigned int stride)
137e3adcf8fSFrançois Tigeot {
138a85cb24fSFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
139a85cb24fSFrançois Tigeot 	unsigned int tile_width;
140e3adcf8fSFrançois Tigeot 
141e3adcf8fSFrançois Tigeot 	/* Linear is always fine */
142a85cb24fSFrançois Tigeot 	if (tiling == I915_TILING_NONE)
143e9243325SFrançois Tigeot 		return true;
144e3adcf8fSFrançois Tigeot 
145a85cb24fSFrançois Tigeot 	if (tiling > I915_TILING_LAST)
14671f41f3eSFrançois Tigeot 		return false;
14771f41f3eSFrançois Tigeot 
148e3adcf8fSFrançois Tigeot 	/* check maximum stride & object size */
1498e26cdf6SFrançois Tigeot 	/* i965+ stores the end address of the gtt mapping in the fence
150e3adcf8fSFrançois Tigeot 	 * reg, so dont bother to check the size */
151a85cb24fSFrançois Tigeot 	if (INTEL_GEN(i915) >= 7) {
1528e26cdf6SFrançois Tigeot 		if (stride / 128 > GEN7_FENCE_MAX_PITCH_VAL)
1538e26cdf6SFrançois Tigeot 			return false;
154a85cb24fSFrançois Tigeot 	} else if (INTEL_GEN(i915) >= 4) {
155e3adcf8fSFrançois Tigeot 		if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
156e9243325SFrançois Tigeot 			return false;
157e3adcf8fSFrançois Tigeot 	} else {
158e3adcf8fSFrançois Tigeot 		if (stride > 8192)
159e9243325SFrançois Tigeot 			return false;
160e3adcf8fSFrançois Tigeot 
161a85cb24fSFrançois Tigeot 		if (!is_power_of_2(stride))
162e9243325SFrançois Tigeot 			return false;
163e3adcf8fSFrançois Tigeot 	}
164e3adcf8fSFrançois Tigeot 
165a85cb24fSFrançois Tigeot 	if (IS_GEN2(i915) ||
166a85cb24fSFrançois Tigeot 	    (tiling == I915_TILING_Y && HAS_128_BYTE_Y_TILING(i915)))
167a85cb24fSFrançois Tigeot 		tile_width = 128;
168a85cb24fSFrançois Tigeot 	else
169a85cb24fSFrançois Tigeot 		tile_width = 512;
1708e26cdf6SFrançois Tigeot 
171a85cb24fSFrançois Tigeot 	if (!stride || !IS_ALIGNED(stride, tile_width))
172e9243325SFrançois Tigeot 		return false;
173e3adcf8fSFrançois Tigeot 
174e9243325SFrançois Tigeot 	return true;
175e3adcf8fSFrançois Tigeot }
176e3adcf8fSFrançois Tigeot 
i915_vma_fence_prepare(struct i915_vma * vma,int tiling_mode,unsigned int stride)177a85cb24fSFrançois Tigeot static bool i915_vma_fence_prepare(struct i915_vma *vma,
178a85cb24fSFrançois Tigeot 				   int tiling_mode, unsigned int stride)
179e3adcf8fSFrançois Tigeot {
180a85cb24fSFrançois Tigeot 	struct drm_i915_private *i915 = vma->vm->i915;
181a85cb24fSFrançois Tigeot 	u32 size, alignment;
182e3adcf8fSFrançois Tigeot 
1831e12ee3bSFrançois Tigeot 	if (!i915_vma_is_map_and_fenceable(vma))
184e9243325SFrançois Tigeot 		return true;
185e3adcf8fSFrançois Tigeot 
186a85cb24fSFrançois Tigeot 	size = i915_gem_fence_size(i915, vma->size, tiling_mode, stride);
1871e12ee3bSFrançois Tigeot 	if (vma->node.size < size)
188e9243325SFrançois Tigeot 		return false;
189e3adcf8fSFrançois Tigeot 
190a85cb24fSFrançois Tigeot 	alignment = i915_gem_fence_alignment(i915, vma->size, tiling_mode, stride);
191a85cb24fSFrançois Tigeot 	if (!IS_ALIGNED(vma->node.start, alignment))
192e9243325SFrançois Tigeot 		return false;
193e3adcf8fSFrançois Tigeot 
194e9243325SFrançois Tigeot 	return true;
195e3adcf8fSFrançois Tigeot }
196e3adcf8fSFrançois Tigeot 
1971e12ee3bSFrançois Tigeot /* Make the current GTT allocation valid for the change in tiling. */
1981e12ee3bSFrançois Tigeot static int
i915_gem_object_fence_prepare(struct drm_i915_gem_object * obj,int tiling_mode,unsigned int stride)199a85cb24fSFrançois Tigeot i915_gem_object_fence_prepare(struct drm_i915_gem_object *obj,
200a85cb24fSFrançois Tigeot 			      int tiling_mode, unsigned int stride)
2011e12ee3bSFrançois Tigeot {
2021e12ee3bSFrançois Tigeot 	struct i915_vma *vma;
2031e12ee3bSFrançois Tigeot 	int ret;
2041e12ee3bSFrançois Tigeot 
2051e12ee3bSFrançois Tigeot 	if (tiling_mode == I915_TILING_NONE)
2061e12ee3bSFrançois Tigeot 		return 0;
2071e12ee3bSFrançois Tigeot 
2081e12ee3bSFrançois Tigeot 	list_for_each_entry(vma, &obj->vma_list, obj_link) {
209a85cb24fSFrançois Tigeot 		if (!i915_vma_is_ggtt(vma))
210a85cb24fSFrançois Tigeot 			break;
211a85cb24fSFrançois Tigeot 
212a85cb24fSFrançois Tigeot 		if (i915_vma_fence_prepare(vma, tiling_mode, stride))
2131e12ee3bSFrançois Tigeot 			continue;
2141e12ee3bSFrançois Tigeot 
2151e12ee3bSFrançois Tigeot 		ret = i915_vma_unbind(vma);
2161e12ee3bSFrançois Tigeot 		if (ret)
2171e12ee3bSFrançois Tigeot 			return ret;
2181e12ee3bSFrançois Tigeot 	}
2191e12ee3bSFrançois Tigeot 
2201e12ee3bSFrançois Tigeot 	return 0;
2211e12ee3bSFrançois Tigeot }
2221e12ee3bSFrançois Tigeot 
223a85cb24fSFrançois Tigeot int
i915_gem_object_set_tiling(struct drm_i915_gem_object * obj,unsigned int tiling,unsigned int stride)224a85cb24fSFrançois Tigeot i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
225a85cb24fSFrançois Tigeot 			   unsigned int tiling, unsigned int stride)
226a85cb24fSFrançois Tigeot {
227a85cb24fSFrançois Tigeot 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
228a85cb24fSFrançois Tigeot 	struct i915_vma *vma;
229a85cb24fSFrançois Tigeot 	int err;
230a85cb24fSFrançois Tigeot 
231a85cb24fSFrançois Tigeot 	/* Make sure we don't cross-contaminate obj->tiling_and_stride */
232a85cb24fSFrançois Tigeot 	BUILD_BUG_ON(I915_TILING_LAST & STRIDE_MASK);
233a85cb24fSFrançois Tigeot 
234a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!i915_tiling_ok(obj, tiling, stride));
235a85cb24fSFrançois Tigeot 	GEM_BUG_ON(!stride ^ (tiling == I915_TILING_NONE));
236a85cb24fSFrançois Tigeot 	lockdep_assert_held(&i915->drm.struct_mutex);
237a85cb24fSFrançois Tigeot 
238a85cb24fSFrançois Tigeot 	if ((tiling | stride) == obj->tiling_and_stride)
239a85cb24fSFrançois Tigeot 		return 0;
240a85cb24fSFrançois Tigeot 
241a85cb24fSFrançois Tigeot 	if (i915_gem_object_is_framebuffer(obj))
242a85cb24fSFrançois Tigeot 		return -EBUSY;
243a85cb24fSFrançois Tigeot 
244a85cb24fSFrançois Tigeot 	/* We need to rebind the object if its current allocation
245a85cb24fSFrançois Tigeot 	 * no longer meets the alignment restrictions for its new
246a85cb24fSFrançois Tigeot 	 * tiling mode. Otherwise we can just leave it alone, but
247a85cb24fSFrançois Tigeot 	 * need to ensure that any fence register is updated before
248a85cb24fSFrançois Tigeot 	 * the next fenced (either through the GTT or by the BLT unit
249a85cb24fSFrançois Tigeot 	 * on older GPUs) access.
250a85cb24fSFrançois Tigeot 	 *
251a85cb24fSFrançois Tigeot 	 * After updating the tiling parameters, we then flag whether
252a85cb24fSFrançois Tigeot 	 * we need to update an associated fence register. Note this
253a85cb24fSFrançois Tigeot 	 * has to also include the unfenced register the GPU uses
254a85cb24fSFrançois Tigeot 	 * whilst executing a fenced command for an untiled object.
255a85cb24fSFrançois Tigeot 	 */
256a85cb24fSFrançois Tigeot 
257a85cb24fSFrançois Tigeot 	err = i915_gem_object_fence_prepare(obj, tiling, stride);
258a85cb24fSFrançois Tigeot 	if (err)
259a85cb24fSFrançois Tigeot 		return err;
260a85cb24fSFrançois Tigeot 
261a85cb24fSFrançois Tigeot 	i915_gem_object_lock(obj);
262a85cb24fSFrançois Tigeot 	if (i915_gem_object_is_framebuffer(obj)) {
263a85cb24fSFrançois Tigeot 		i915_gem_object_unlock(obj);
264a85cb24fSFrançois Tigeot 		return -EBUSY;
265a85cb24fSFrançois Tigeot 	}
266a85cb24fSFrançois Tigeot 
267a85cb24fSFrançois Tigeot 	/* If the memory has unknown (i.e. varying) swizzling, we pin the
268a85cb24fSFrançois Tigeot 	 * pages to prevent them being swapped out and causing corruption
269a85cb24fSFrançois Tigeot 	 * due to the change in swizzling.
270a85cb24fSFrançois Tigeot 	 */
271a85cb24fSFrançois Tigeot 	mutex_lock(&obj->mm.lock);
272*3f2dd94aSFrançois Tigeot 	if (i915_gem_object_has_pages(obj) &&
273a85cb24fSFrançois Tigeot 	    obj->mm.madv == I915_MADV_WILLNEED &&
274a85cb24fSFrançois Tigeot 	    i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
275a85cb24fSFrançois Tigeot 		if (tiling == I915_TILING_NONE) {
276a85cb24fSFrançois Tigeot 			GEM_BUG_ON(!obj->mm.quirked);
277a85cb24fSFrançois Tigeot 			__i915_gem_object_unpin_pages(obj);
278a85cb24fSFrançois Tigeot 			obj->mm.quirked = false;
279a85cb24fSFrançois Tigeot 		}
280a85cb24fSFrançois Tigeot 		if (!i915_gem_object_is_tiled(obj)) {
281a85cb24fSFrançois Tigeot 			GEM_BUG_ON(obj->mm.quirked);
282a85cb24fSFrançois Tigeot 			__i915_gem_object_pin_pages(obj);
283a85cb24fSFrançois Tigeot 			obj->mm.quirked = true;
284a85cb24fSFrançois Tigeot 		}
285a85cb24fSFrançois Tigeot 	}
286a85cb24fSFrançois Tigeot 	mutex_unlock(&obj->mm.lock);
287a85cb24fSFrançois Tigeot 
288a85cb24fSFrançois Tigeot 	list_for_each_entry(vma, &obj->vma_list, obj_link) {
289a85cb24fSFrançois Tigeot 		if (!i915_vma_is_ggtt(vma))
290a85cb24fSFrançois Tigeot 			break;
291a85cb24fSFrançois Tigeot 
292a85cb24fSFrançois Tigeot 		vma->fence_size =
293a85cb24fSFrançois Tigeot 			i915_gem_fence_size(i915, vma->size, tiling, stride);
294a85cb24fSFrançois Tigeot 		vma->fence_alignment =
295a85cb24fSFrançois Tigeot 			i915_gem_fence_alignment(i915,
296a85cb24fSFrançois Tigeot 						 vma->size, tiling, stride);
297a85cb24fSFrançois Tigeot 
298a85cb24fSFrançois Tigeot 		if (vma->fence)
299a85cb24fSFrançois Tigeot 			vma->fence->dirty = true;
300a85cb24fSFrançois Tigeot 	}
301a85cb24fSFrançois Tigeot 
302a85cb24fSFrançois Tigeot 	obj->tiling_and_stride = tiling | stride;
303a85cb24fSFrançois Tigeot 	i915_gem_object_unlock(obj);
304a85cb24fSFrançois Tigeot 
305a85cb24fSFrançois Tigeot 	/* Force the fence to be reacquired for GTT access */
306a85cb24fSFrançois Tigeot 	i915_gem_release_mmap(obj);
307a85cb24fSFrançois Tigeot 
308a85cb24fSFrançois Tigeot 	/* Try to preallocate memory required to save swizzling on put-pages */
309a85cb24fSFrançois Tigeot 	if (i915_gem_object_needs_bit17_swizzle(obj)) {
310a85cb24fSFrançois Tigeot 		if (!obj->bit_17) {
311a85cb24fSFrançois Tigeot 			obj->bit_17 = kcalloc(BITS_TO_LONGS(obj->base.size >> PAGE_SHIFT),
312a85cb24fSFrançois Tigeot 					      sizeof(long), GFP_KERNEL);
313a85cb24fSFrançois Tigeot 		}
314a85cb24fSFrançois Tigeot 	} else {
315a85cb24fSFrançois Tigeot 		kfree(obj->bit_17);
316a85cb24fSFrançois Tigeot 		obj->bit_17 = NULL;
317a85cb24fSFrançois Tigeot 	}
318a85cb24fSFrançois Tigeot 
319a85cb24fSFrançois Tigeot 	return 0;
320a85cb24fSFrançois Tigeot }
321a85cb24fSFrançois Tigeot 
322e3adcf8fSFrançois Tigeot /**
323a85cb24fSFrançois Tigeot  * i915_gem_set_tiling_ioctl - IOCTL handler to set tiling mode
324a05eeebfSFrançois Tigeot  * @dev: DRM device
325a05eeebfSFrançois Tigeot  * @data: data pointer for the ioctl
326a05eeebfSFrançois Tigeot  * @file: DRM file for the ioctl call
327a05eeebfSFrançois Tigeot  *
328e3adcf8fSFrançois Tigeot  * Sets the tiling mode of an object, returning the required swizzling of
329e3adcf8fSFrançois Tigeot  * bit 6 of addresses in the object.
330a05eeebfSFrançois Tigeot  *
331a05eeebfSFrançois Tigeot  * Called by the user via ioctl.
332a05eeebfSFrançois Tigeot  *
333a05eeebfSFrançois Tigeot  * Returns:
334a05eeebfSFrançois Tigeot  * Zero on success, negative errno on failure.
335e3adcf8fSFrançois Tigeot  */
336e3adcf8fSFrançois Tigeot int
i915_gem_set_tiling_ioctl(struct drm_device * dev,void * data,struct drm_file * file)337a85cb24fSFrançois Tigeot i915_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
338e3adcf8fSFrançois Tigeot 			  struct drm_file *file)
339e3adcf8fSFrançois Tigeot {
340e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_set_tiling *args = data;
341e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
342a85cb24fSFrançois Tigeot 	int err;
34371f41f3eSFrançois Tigeot 
34487df8fc6SFrançois Tigeot 	obj = i915_gem_object_lookup(file, args->handle);
34587df8fc6SFrançois Tigeot 	if (!obj)
346e3adcf8fSFrançois Tigeot 		return -ENOENT;
347e3adcf8fSFrançois Tigeot 
348a85cb24fSFrançois Tigeot 	if (!i915_tiling_ok(obj, args->tiling_mode, args->stride)) {
349a85cb24fSFrançois Tigeot 		err = -EINVAL;
3502c9916cdSFrançois Tigeot 		goto err;
351e3adcf8fSFrançois Tigeot 	}
352e3adcf8fSFrançois Tigeot 
353e3adcf8fSFrançois Tigeot 	if (args->tiling_mode == I915_TILING_NONE) {
354e3adcf8fSFrançois Tigeot 		args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
355e3adcf8fSFrançois Tigeot 		args->stride = 0;
356e3adcf8fSFrançois Tigeot 	} else {
357e3adcf8fSFrançois Tigeot 		if (args->tiling_mode == I915_TILING_X)
358a85cb24fSFrançois Tigeot 			args->swizzle_mode = to_i915(dev)->mm.bit_6_swizzle_x;
359e3adcf8fSFrançois Tigeot 		else
360a85cb24fSFrançois Tigeot 			args->swizzle_mode = to_i915(dev)->mm.bit_6_swizzle_y;
361e3adcf8fSFrançois Tigeot 
362e3adcf8fSFrançois Tigeot 		/* Hide bit 17 swizzling from the user.  This prevents old Mesa
363e3adcf8fSFrançois Tigeot 		 * from aborting the application on sw fallbacks to bit 17,
364e3adcf8fSFrançois Tigeot 		 * and we use the pread/pwrite bit17 paths to swizzle for it.
365e3adcf8fSFrançois Tigeot 		 * If there was a user that was relying on the swizzle
366e3adcf8fSFrançois Tigeot 		 * information for drm_intel_bo_map()ed reads/writes this would
367e3adcf8fSFrançois Tigeot 		 * break it, but we don't have any of those.
368e3adcf8fSFrançois Tigeot 		 */
369e3adcf8fSFrançois Tigeot 		if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
370e3adcf8fSFrançois Tigeot 			args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
371e3adcf8fSFrançois Tigeot 		if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
372e3adcf8fSFrançois Tigeot 			args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
373e3adcf8fSFrançois Tigeot 
374e3adcf8fSFrançois Tigeot 		/* If we can't handle the swizzling, make it untiled. */
375e3adcf8fSFrançois Tigeot 		if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
376e3adcf8fSFrançois Tigeot 			args->tiling_mode = I915_TILING_NONE;
377e3adcf8fSFrançois Tigeot 			args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
378e3adcf8fSFrançois Tigeot 			args->stride = 0;
379e3adcf8fSFrançois Tigeot 		}
380e3adcf8fSFrançois Tigeot 	}
381e3adcf8fSFrançois Tigeot 
382a85cb24fSFrançois Tigeot 	err = mutex_lock_interruptible(&dev->struct_mutex);
383a85cb24fSFrançois Tigeot 	if (err)
384a85cb24fSFrançois Tigeot 		goto err;
385f192107fSFrançois Tigeot 
386a85cb24fSFrançois Tigeot 	err = i915_gem_object_set_tiling(obj, args->tiling_mode, args->stride);
387a85cb24fSFrançois Tigeot 	mutex_unlock(&dev->struct_mutex);
3881e12ee3bSFrançois Tigeot 
389a85cb24fSFrançois Tigeot 	/* We have to maintain this existing ABI... */
39071f41f3eSFrançois Tigeot 	args->stride = i915_gem_object_get_stride(obj);
39171f41f3eSFrançois Tigeot 	args->tiling_mode = i915_gem_object_get_tiling(obj);
392a2fdbec6SFrançois Tigeot 
3932c9916cdSFrançois Tigeot err:
39487df8fc6SFrançois Tigeot 	i915_gem_object_put(obj);
3951e12ee3bSFrançois Tigeot 	return err;
396e3adcf8fSFrançois Tigeot }
397e3adcf8fSFrançois Tigeot 
398e3adcf8fSFrançois Tigeot /**
399a85cb24fSFrançois Tigeot  * i915_gem_get_tiling_ioctl - IOCTL handler to get tiling mode
400a05eeebfSFrançois Tigeot  * @dev: DRM device
401a05eeebfSFrançois Tigeot  * @data: data pointer for the ioctl
402a05eeebfSFrançois Tigeot  * @file: DRM file for the ioctl call
403a05eeebfSFrançois Tigeot  *
404e3adcf8fSFrançois Tigeot  * Returns the current tiling mode and required bit 6 swizzling for the object.
405a05eeebfSFrançois Tigeot  *
406a05eeebfSFrançois Tigeot  * Called by the user via ioctl.
407a05eeebfSFrançois Tigeot  *
408a05eeebfSFrançois Tigeot  * Returns:
409a05eeebfSFrançois Tigeot  * Zero on success, negative errno on failure.
410e3adcf8fSFrançois Tigeot  */
411e3adcf8fSFrançois Tigeot int
i915_gem_get_tiling_ioctl(struct drm_device * dev,void * data,struct drm_file * file)412a85cb24fSFrançois Tigeot i915_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
413e3adcf8fSFrançois Tigeot 			  struct drm_file *file)
414e3adcf8fSFrançois Tigeot {
415e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_get_tiling *args = data;
416303bf270SFrançois Tigeot 	struct drm_i915_private *dev_priv = to_i915(dev);
417e3adcf8fSFrançois Tigeot 	struct drm_i915_gem_object *obj;
4184be47400SFrançois Tigeot 	int err = -ENOENT;
419e3adcf8fSFrançois Tigeot 
4204be47400SFrançois Tigeot 	rcu_read_lock();
4214be47400SFrançois Tigeot 	obj = i915_gem_object_lookup_rcu(file, args->handle);
4224be47400SFrançois Tigeot 	if (obj) {
4234be47400SFrançois Tigeot 		args->tiling_mode =
4244be47400SFrançois Tigeot 			READ_ONCE(obj->tiling_and_stride) & TILING_MASK;
4254be47400SFrançois Tigeot 		err = 0;
4264be47400SFrançois Tigeot 	}
4274be47400SFrançois Tigeot 	rcu_read_unlock();
4284be47400SFrançois Tigeot 	if (unlikely(err))
4294be47400SFrançois Tigeot 		return err;
430e3adcf8fSFrançois Tigeot 
43171f41f3eSFrançois Tigeot 	switch (args->tiling_mode) {
432e3adcf8fSFrançois Tigeot 	case I915_TILING_X:
433e3adcf8fSFrançois Tigeot 		args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
434e3adcf8fSFrançois Tigeot 		break;
435e3adcf8fSFrançois Tigeot 	case I915_TILING_Y:
436e3adcf8fSFrançois Tigeot 		args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
437e3adcf8fSFrançois Tigeot 		break;
4384be47400SFrançois Tigeot 	default:
439e3adcf8fSFrançois Tigeot 	case I915_TILING_NONE:
440e3adcf8fSFrançois Tigeot 		args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
441e3adcf8fSFrançois Tigeot 		break;
442e3adcf8fSFrançois Tigeot 	}
443e3adcf8fSFrançois Tigeot 
444e3adcf8fSFrançois Tigeot 	/* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
44519c468b4SFrançois Tigeot 	if (dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
44619c468b4SFrançois Tigeot 		args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN;
44719c468b4SFrançois Tigeot 	else
4482c9916cdSFrançois Tigeot 		args->phys_swizzle_mode = args->swizzle_mode;
449e3adcf8fSFrançois Tigeot 	if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
450e3adcf8fSFrançois Tigeot 		args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
451e3adcf8fSFrançois Tigeot 	if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
452e3adcf8fSFrançois Tigeot 		args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
453e3adcf8fSFrançois Tigeot 
454e3adcf8fSFrançois Tigeot 	return 0;
455e3adcf8fSFrançois Tigeot }
456