xref: /openbmc/linux/drivers/gpu/drm/i915/gem/i915_gem_tiling.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
110be98a7SChris Wilson /*
210be98a7SChris Wilson  * SPDX-License-Identifier: MIT
310be98a7SChris Wilson  *
410be98a7SChris Wilson  * Copyright © 2008 Intel Corporation
510be98a7SChris Wilson  */
610be98a7SChris Wilson 
710be98a7SChris Wilson #include <linux/string.h>
810be98a7SChris Wilson #include <linux/bitops.h>
910be98a7SChris Wilson 
1010be98a7SChris Wilson #include "i915_drv.h"
1110be98a7SChris Wilson #include "i915_gem.h"
1210be98a7SChris Wilson #include "i915_gem_ioctls.h"
13cc662126SAbdiel Janulgue #include "i915_gem_mman.h"
1410be98a7SChris Wilson #include "i915_gem_object.h"
15386e75a4SJani Nikula #include "i915_gem_tiling.h"
16ce2fce25SMatt Roper #include "i915_reg.h"
1710be98a7SChris Wilson 
1810be98a7SChris Wilson /**
1910be98a7SChris Wilson  * DOC: buffer object tiling
2010be98a7SChris Wilson  *
2110be98a7SChris Wilson  * i915_gem_set_tiling_ioctl() and i915_gem_get_tiling_ioctl() is the userspace
2210be98a7SChris Wilson  * interface to declare fence register requirements.
2310be98a7SChris Wilson  *
2410be98a7SChris Wilson  * In principle GEM doesn't care at all about the internal data layout of an
2510be98a7SChris Wilson  * object, and hence it also doesn't care about tiling or swizzling. There's two
2610be98a7SChris Wilson  * exceptions:
2710be98a7SChris Wilson  *
2810be98a7SChris Wilson  * - For X and Y tiling the hardware provides detilers for CPU access, so called
2910be98a7SChris Wilson  *   fences. Since there's only a limited amount of them the kernel must manage
3010be98a7SChris Wilson  *   these, and therefore userspace must tell the kernel the object tiling if it
3110be98a7SChris Wilson  *   wants to use fences for detiling.
3210be98a7SChris Wilson  * - On gen3 and gen4 platforms have a swizzling pattern for tiled objects which
3310be98a7SChris Wilson  *   depends upon the physical page frame number. When swapping such objects the
3410be98a7SChris Wilson  *   page frame number might change and the kernel must be able to fix this up
3510be98a7SChris Wilson  *   and hence now the tiling. Note that on a subset of platforms with
3610be98a7SChris Wilson  *   asymmetric memory channel population the swizzling pattern changes in an
3710be98a7SChris Wilson  *   unknown way, and for those the kernel simply forbids swapping completely.
3810be98a7SChris Wilson  *
3910be98a7SChris Wilson  * Since neither of this applies for new tiling layouts on modern platforms like
4010be98a7SChris Wilson  * W, Ys and Yf tiling GEM only allows object tiling to be set to X or Y tiled.
4110be98a7SChris Wilson  * Anything else can be handled in userspace entirely without the kernel's
4210be98a7SChris Wilson  * invovlement.
4310be98a7SChris Wilson  */
4410be98a7SChris Wilson 
4510be98a7SChris Wilson /**
4610be98a7SChris Wilson  * i915_gem_fence_size - required global GTT size for a fence
4710be98a7SChris Wilson  * @i915: i915 device
4810be98a7SChris Wilson  * @size: object size
4910be98a7SChris Wilson  * @tiling: tiling mode
5010be98a7SChris Wilson  * @stride: tiling stride
5110be98a7SChris Wilson  *
5210be98a7SChris Wilson  * Return the required global GTT size for a fence (view of a tiled object),
5310be98a7SChris Wilson  * taking into account potential fence register mapping.
5410be98a7SChris Wilson  */
i915_gem_fence_size(struct drm_i915_private * i915,u32 size,unsigned int tiling,unsigned int stride)5510be98a7SChris Wilson u32 i915_gem_fence_size(struct drm_i915_private *i915,
5610be98a7SChris Wilson 			u32 size, unsigned int tiling, unsigned int stride)
5710be98a7SChris Wilson {
5810be98a7SChris Wilson 	u32 ggtt_size;
5910be98a7SChris Wilson 
6010be98a7SChris Wilson 	GEM_BUG_ON(!size);
6110be98a7SChris Wilson 
6210be98a7SChris Wilson 	if (tiling == I915_TILING_NONE)
6310be98a7SChris Wilson 		return size;
6410be98a7SChris Wilson 
6510be98a7SChris Wilson 	GEM_BUG_ON(!stride);
6610be98a7SChris Wilson 
6740e1956eSLucas De Marchi 	if (GRAPHICS_VER(i915) >= 4) {
6810be98a7SChris Wilson 		stride *= i915_gem_tile_height(tiling);
6910be98a7SChris Wilson 		GEM_BUG_ON(!IS_ALIGNED(stride, I965_FENCE_PAGE));
7010be98a7SChris Wilson 		return roundup(size, stride);
7110be98a7SChris Wilson 	}
7210be98a7SChris Wilson 
7310be98a7SChris Wilson 	/* Previous chips need a power-of-two fence region when tiling */
7440e1956eSLucas De Marchi 	if (GRAPHICS_VER(i915) == 3)
7510be98a7SChris Wilson 		ggtt_size = 1024*1024;
7610be98a7SChris Wilson 	else
7710be98a7SChris Wilson 		ggtt_size = 512*1024;
7810be98a7SChris Wilson 
7910be98a7SChris Wilson 	while (ggtt_size < size)
8010be98a7SChris Wilson 		ggtt_size <<= 1;
8110be98a7SChris Wilson 
8210be98a7SChris Wilson 	return ggtt_size;
8310be98a7SChris Wilson }
8410be98a7SChris Wilson 
8510be98a7SChris Wilson /**
8610be98a7SChris Wilson  * i915_gem_fence_alignment - required global GTT alignment for a fence
8710be98a7SChris Wilson  * @i915: i915 device
8810be98a7SChris Wilson  * @size: object size
8910be98a7SChris Wilson  * @tiling: tiling mode
9010be98a7SChris Wilson  * @stride: tiling stride
9110be98a7SChris Wilson  *
9210be98a7SChris Wilson  * Return the required global GTT alignment for a fence (a view of a tiled
9310be98a7SChris Wilson  * object), taking into account potential fence register mapping.
9410be98a7SChris Wilson  */
i915_gem_fence_alignment(struct drm_i915_private * i915,u32 size,unsigned int tiling,unsigned int stride)9510be98a7SChris Wilson u32 i915_gem_fence_alignment(struct drm_i915_private *i915, u32 size,
9610be98a7SChris Wilson 			     unsigned int tiling, unsigned int stride)
9710be98a7SChris Wilson {
9810be98a7SChris Wilson 	GEM_BUG_ON(!size);
9910be98a7SChris Wilson 
10010be98a7SChris Wilson 	/*
10110be98a7SChris Wilson 	 * Minimum alignment is 4k (GTT page size), but might be greater
10210be98a7SChris Wilson 	 * if a fence register is needed for the object.
10310be98a7SChris Wilson 	 */
10410be98a7SChris Wilson 	if (tiling == I915_TILING_NONE)
10510be98a7SChris Wilson 		return I915_GTT_MIN_ALIGNMENT;
10610be98a7SChris Wilson 
10740e1956eSLucas De Marchi 	if (GRAPHICS_VER(i915) >= 4)
10810be98a7SChris Wilson 		return I965_FENCE_PAGE;
10910be98a7SChris Wilson 
11010be98a7SChris Wilson 	/*
11110be98a7SChris Wilson 	 * Previous chips need to be aligned to the size of the smallest
11210be98a7SChris Wilson 	 * fence register that can contain the object.
11310be98a7SChris Wilson 	 */
11410be98a7SChris Wilson 	return i915_gem_fence_size(i915, size, tiling, stride);
11510be98a7SChris Wilson }
11610be98a7SChris Wilson 
11714d6a086Spengfuyuan /* Check pitch constraints for all chips & tiling formats */
11810be98a7SChris Wilson static bool
i915_tiling_ok(struct drm_i915_gem_object * obj,unsigned int tiling,unsigned int stride)11910be98a7SChris Wilson i915_tiling_ok(struct drm_i915_gem_object *obj,
12010be98a7SChris Wilson 	       unsigned int tiling, unsigned int stride)
12110be98a7SChris Wilson {
12210be98a7SChris Wilson 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
12310be98a7SChris Wilson 	unsigned int tile_width;
12410be98a7SChris Wilson 
12510be98a7SChris Wilson 	/* Linear is always fine */
12610be98a7SChris Wilson 	if (tiling == I915_TILING_NONE)
12710be98a7SChris Wilson 		return true;
12810be98a7SChris Wilson 
12910be98a7SChris Wilson 	if (tiling > I915_TILING_LAST)
13010be98a7SChris Wilson 		return false;
13110be98a7SChris Wilson 
13210be98a7SChris Wilson 	/* check maximum stride & object size */
13310be98a7SChris Wilson 	/* i965+ stores the end address of the gtt mapping in the fence
13410be98a7SChris Wilson 	 * reg, so dont bother to check the size */
13540e1956eSLucas De Marchi 	if (GRAPHICS_VER(i915) >= 7) {
13610be98a7SChris Wilson 		if (stride / 128 > GEN7_FENCE_MAX_PITCH_VAL)
13710be98a7SChris Wilson 			return false;
13840e1956eSLucas De Marchi 	} else if (GRAPHICS_VER(i915) >= 4) {
13910be98a7SChris Wilson 		if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
14010be98a7SChris Wilson 			return false;
14110be98a7SChris Wilson 	} else {
14210be98a7SChris Wilson 		if (stride > 8192)
14310be98a7SChris Wilson 			return false;
14410be98a7SChris Wilson 
14510be98a7SChris Wilson 		if (!is_power_of_2(stride))
14610be98a7SChris Wilson 			return false;
14710be98a7SChris Wilson 	}
14810be98a7SChris Wilson 
14940e1956eSLucas De Marchi 	if (GRAPHICS_VER(i915) == 2 ||
15010be98a7SChris Wilson 	    (tiling == I915_TILING_Y && HAS_128_BYTE_Y_TILING(i915)))
15110be98a7SChris Wilson 		tile_width = 128;
15210be98a7SChris Wilson 	else
15310be98a7SChris Wilson 		tile_width = 512;
15410be98a7SChris Wilson 
15510be98a7SChris Wilson 	if (!stride || !IS_ALIGNED(stride, tile_width))
15610be98a7SChris Wilson 		return false;
15710be98a7SChris Wilson 
15810be98a7SChris Wilson 	return true;
15910be98a7SChris Wilson }
16010be98a7SChris Wilson 
i915_vma_fence_prepare(struct i915_vma * vma,int tiling_mode,unsigned int stride)16110be98a7SChris Wilson static bool i915_vma_fence_prepare(struct i915_vma *vma,
16210be98a7SChris Wilson 				   int tiling_mode, unsigned int stride)
16310be98a7SChris Wilson {
16410be98a7SChris Wilson 	struct drm_i915_private *i915 = vma->vm->i915;
16510be98a7SChris Wilson 	u32 size, alignment;
16610be98a7SChris Wilson 
16710be98a7SChris Wilson 	if (!i915_vma_is_map_and_fenceable(vma))
16810be98a7SChris Wilson 		return true;
16910be98a7SChris Wilson 
17010be98a7SChris Wilson 	size = i915_gem_fence_size(i915, vma->size, tiling_mode, stride);
1718e4ee5e8SChris Wilson 	if (i915_vma_size(vma) < size)
17210be98a7SChris Wilson 		return false;
17310be98a7SChris Wilson 
17410be98a7SChris Wilson 	alignment = i915_gem_fence_alignment(i915, vma->size, tiling_mode, stride);
1758e4ee5e8SChris Wilson 	if (!IS_ALIGNED(i915_ggtt_offset(vma), alignment))
17610be98a7SChris Wilson 		return false;
17710be98a7SChris Wilson 
17810be98a7SChris Wilson 	return true;
17910be98a7SChris Wilson }
18010be98a7SChris Wilson 
18110be98a7SChris Wilson /* Make the current GTT allocation valid for the change in tiling. */
18210be98a7SChris Wilson static int
i915_gem_object_fence_prepare(struct drm_i915_gem_object * obj,int tiling_mode,unsigned int stride)18310be98a7SChris Wilson i915_gem_object_fence_prepare(struct drm_i915_gem_object *obj,
18410be98a7SChris Wilson 			      int tiling_mode, unsigned int stride)
18510be98a7SChris Wilson {
1865c24c9d2SMichał Winiarski 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
1875c24c9d2SMichał Winiarski 	struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
188cb593e5dSChris Wilson 	struct i915_vma *vma, *vn;
189cb593e5dSChris Wilson 	LIST_HEAD(unbind);
1902850748eSChris Wilson 	int ret = 0;
19110be98a7SChris Wilson 
19210be98a7SChris Wilson 	if (tiling_mode == I915_TILING_NONE)
19310be98a7SChris Wilson 		return 0;
19410be98a7SChris Wilson 
1952850748eSChris Wilson 	mutex_lock(&ggtt->vm.mutex);
196cb593e5dSChris Wilson 
197cb593e5dSChris Wilson 	spin_lock(&obj->vma.lock);
19810be98a7SChris Wilson 	for_each_ggtt_vma(vma, obj) {
199cb593e5dSChris Wilson 		GEM_BUG_ON(vma->vm != &ggtt->vm);
200cb593e5dSChris Wilson 
20110be98a7SChris Wilson 		if (i915_vma_fence_prepare(vma, tiling_mode, stride))
20210be98a7SChris Wilson 			continue;
20310be98a7SChris Wilson 
204cb593e5dSChris Wilson 		list_move(&vma->vm_link, &unbind);
205cb593e5dSChris Wilson 	}
206cb593e5dSChris Wilson 	spin_unlock(&obj->vma.lock);
207cb593e5dSChris Wilson 
208cb593e5dSChris Wilson 	list_for_each_entry_safe(vma, vn, &unbind, vm_link) {
2092850748eSChris Wilson 		ret = __i915_vma_unbind(vma);
210cb593e5dSChris Wilson 		if (ret) {
211cb593e5dSChris Wilson 			/* Restore the remaining vma on an error */
212cb593e5dSChris Wilson 			list_splice(&unbind, &ggtt->vm.bound_list);
2132850748eSChris Wilson 			break;
21410be98a7SChris Wilson 		}
215cb593e5dSChris Wilson 	}
216cb593e5dSChris Wilson 
2172850748eSChris Wilson 	mutex_unlock(&ggtt->vm.mutex);
21810be98a7SChris Wilson 
2192850748eSChris Wilson 	return ret;
22010be98a7SChris Wilson }
22110be98a7SChris Wilson 
i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object * obj)2220438fd1aSJani Nikula bool i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
2230438fd1aSJani Nikula {
2240438fd1aSJani Nikula 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
2250438fd1aSJani Nikula 
2260438fd1aSJani Nikula 	return to_gt(i915)->ggtt->bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
2270438fd1aSJani Nikula 		i915_gem_object_is_tiled(obj);
2280438fd1aSJani Nikula }
2290438fd1aSJani Nikula 
23010be98a7SChris Wilson int
i915_gem_object_set_tiling(struct drm_i915_gem_object * obj,unsigned int tiling,unsigned int stride)23110be98a7SChris Wilson i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
23210be98a7SChris Wilson 			   unsigned int tiling, unsigned int stride)
23310be98a7SChris Wilson {
23410be98a7SChris Wilson 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
23510be98a7SChris Wilson 	struct i915_vma *vma;
23610be98a7SChris Wilson 	int err;
23710be98a7SChris Wilson 
23810be98a7SChris Wilson 	/* Make sure we don't cross-contaminate obj->tiling_and_stride */
23910be98a7SChris Wilson 	BUILD_BUG_ON(I915_TILING_LAST & STRIDE_MASK);
24010be98a7SChris Wilson 
24110be98a7SChris Wilson 	GEM_BUG_ON(!i915_tiling_ok(obj, tiling, stride));
24210be98a7SChris Wilson 	GEM_BUG_ON(!stride ^ (tiling == I915_TILING_NONE));
24310be98a7SChris Wilson 
24410be98a7SChris Wilson 	if ((tiling | stride) == obj->tiling_and_stride)
24510be98a7SChris Wilson 		return 0;
24610be98a7SChris Wilson 
24710be98a7SChris Wilson 	if (i915_gem_object_is_framebuffer(obj))
24810be98a7SChris Wilson 		return -EBUSY;
24910be98a7SChris Wilson 
25010be98a7SChris Wilson 	/* We need to rebind the object if its current allocation
25110be98a7SChris Wilson 	 * no longer meets the alignment restrictions for its new
25210be98a7SChris Wilson 	 * tiling mode. Otherwise we can just leave it alone, but
25310be98a7SChris Wilson 	 * need to ensure that any fence register is updated before
25410be98a7SChris Wilson 	 * the next fenced (either through the GTT or by the BLT unit
25510be98a7SChris Wilson 	 * on older GPUs) access.
25610be98a7SChris Wilson 	 *
25710be98a7SChris Wilson 	 * After updating the tiling parameters, we then flag whether
25810be98a7SChris Wilson 	 * we need to update an associated fence register. Note this
25910be98a7SChris Wilson 	 * has to also include the unfenced register the GPU uses
26010be98a7SChris Wilson 	 * whilst executing a fenced command for an untiled object.
26110be98a7SChris Wilson 	 */
26210be98a7SChris Wilson 
26380f0b679SMaarten Lankhorst 	i915_gem_object_lock(obj, NULL);
26410be98a7SChris Wilson 	if (i915_gem_object_is_framebuffer(obj)) {
26510be98a7SChris Wilson 		i915_gem_object_unlock(obj);
26610be98a7SChris Wilson 		return -EBUSY;
26710be98a7SChris Wilson 	}
26810be98a7SChris Wilson 
2692850748eSChris Wilson 	err = i915_gem_object_fence_prepare(obj, tiling, stride);
2702850748eSChris Wilson 	if (err) {
2712850748eSChris Wilson 		i915_gem_object_unlock(obj);
2722850748eSChris Wilson 		return err;
2732850748eSChris Wilson 	}
2742850748eSChris Wilson 
27510be98a7SChris Wilson 	/* If the memory has unknown (i.e. varying) swizzling, we pin the
27610be98a7SChris Wilson 	 * pages to prevent them being swapped out and causing corruption
27710be98a7SChris Wilson 	 * due to the change in swizzling.
27810be98a7SChris Wilson 	 */
27910be98a7SChris Wilson 	if (i915_gem_object_has_pages(obj) &&
28010be98a7SChris Wilson 	    obj->mm.madv == I915_MADV_WILLNEED &&
28195086cb9SJani Nikula 	    i915->gem_quirks & GEM_QUIRK_PIN_SWIZZLED_PAGES) {
28210be98a7SChris Wilson 		if (tiling == I915_TILING_NONE) {
2830175969eSChris Wilson 			GEM_BUG_ON(!i915_gem_object_has_tiling_quirk(obj));
2840175969eSChris Wilson 			i915_gem_object_clear_tiling_quirk(obj);
2850175969eSChris Wilson 			i915_gem_object_make_shrinkable(obj);
28610be98a7SChris Wilson 		}
28710be98a7SChris Wilson 		if (!i915_gem_object_is_tiled(obj)) {
2880175969eSChris Wilson 			GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
2890175969eSChris Wilson 			i915_gem_object_make_unshrinkable(obj);
2900175969eSChris Wilson 			i915_gem_object_set_tiling_quirk(obj);
29110be98a7SChris Wilson 		}
29210be98a7SChris Wilson 	}
29310be98a7SChris Wilson 
294cb593e5dSChris Wilson 	spin_lock(&obj->vma.lock);
29510be98a7SChris Wilson 	for_each_ggtt_vma(vma, obj) {
29610be98a7SChris Wilson 		vma->fence_size =
29710be98a7SChris Wilson 			i915_gem_fence_size(i915, vma->size, tiling, stride);
29810be98a7SChris Wilson 		vma->fence_alignment =
29910be98a7SChris Wilson 			i915_gem_fence_alignment(i915,
30010be98a7SChris Wilson 						 vma->size, tiling, stride);
30110be98a7SChris Wilson 
30210be98a7SChris Wilson 		if (vma->fence)
30310be98a7SChris Wilson 			vma->fence->dirty = true;
30410be98a7SChris Wilson 	}
305cb593e5dSChris Wilson 	spin_unlock(&obj->vma.lock);
30610be98a7SChris Wilson 
30710be98a7SChris Wilson 	obj->tiling_and_stride = tiling | stride;
30810be98a7SChris Wilson 
30910be98a7SChris Wilson 	/* Try to preallocate memory required to save swizzling on put-pages */
31010be98a7SChris Wilson 	if (i915_gem_object_needs_bit17_swizzle(obj)) {
31110be98a7SChris Wilson 		if (!obj->bit_17) {
31210be98a7SChris Wilson 			obj->bit_17 = bitmap_zalloc(obj->base.size >> PAGE_SHIFT,
31310be98a7SChris Wilson 						    GFP_KERNEL);
31410be98a7SChris Wilson 		}
31510be98a7SChris Wilson 	} else {
31610be98a7SChris Wilson 		bitmap_free(obj->bit_17);
31710be98a7SChris Wilson 		obj->bit_17 = NULL;
31810be98a7SChris Wilson 	}
31910be98a7SChris Wilson 
320*10e0cbaaSRob Clark 	i915_gem_object_unlock(obj);
321*10e0cbaaSRob Clark 
322*10e0cbaaSRob Clark 	/* Force the fence to be reacquired for GTT access */
323*10e0cbaaSRob Clark 	i915_gem_object_release_mmap_gtt(obj);
324*10e0cbaaSRob Clark 
32510be98a7SChris Wilson 	return 0;
32610be98a7SChris Wilson }
32710be98a7SChris Wilson 
32810be98a7SChris Wilson /**
32910be98a7SChris Wilson  * i915_gem_set_tiling_ioctl - IOCTL handler to set tiling mode
33010be98a7SChris Wilson  * @dev: DRM device
33110be98a7SChris Wilson  * @data: data pointer for the ioctl
33210be98a7SChris Wilson  * @file: DRM file for the ioctl call
33310be98a7SChris Wilson  *
33410be98a7SChris Wilson  * Sets the tiling mode of an object, returning the required swizzling of
33510be98a7SChris Wilson  * bit 6 of addresses in the object.
33610be98a7SChris Wilson  *
33710be98a7SChris Wilson  * Called by the user via ioctl.
33810be98a7SChris Wilson  *
33910be98a7SChris Wilson  * Returns:
34010be98a7SChris Wilson  * Zero on success, negative errno on failure.
34110be98a7SChris Wilson  */
34210be98a7SChris Wilson int
i915_gem_set_tiling_ioctl(struct drm_device * dev,void * data,struct drm_file * file)34310be98a7SChris Wilson i915_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
34410be98a7SChris Wilson 			  struct drm_file *file)
34510be98a7SChris Wilson {
346ab016914SDaniel Vetter 	struct drm_i915_private *dev_priv = to_i915(dev);
34710be98a7SChris Wilson 	struct drm_i915_gem_set_tiling *args = data;
34810be98a7SChris Wilson 	struct drm_i915_gem_object *obj;
34910be98a7SChris Wilson 	int err;
35010be98a7SChris Wilson 
3515c24c9d2SMichał Winiarski 	if (!to_gt(dev_priv)->ggtt->num_fences)
352ab016914SDaniel Vetter 		return -EOPNOTSUPP;
353ab016914SDaniel Vetter 
35410be98a7SChris Wilson 	obj = i915_gem_object_lookup(file, args->handle);
35510be98a7SChris Wilson 	if (!obj)
35610be98a7SChris Wilson 		return -ENOENT;
35710be98a7SChris Wilson 
35810be98a7SChris Wilson 	/*
35910be98a7SChris Wilson 	 * The tiling mode of proxy objects is handled by its generator, and
36010be98a7SChris Wilson 	 * not allowed to be changed by userspace.
36110be98a7SChris Wilson 	 */
36210be98a7SChris Wilson 	if (i915_gem_object_is_proxy(obj)) {
36310be98a7SChris Wilson 		err = -ENXIO;
36410be98a7SChris Wilson 		goto err;
36510be98a7SChris Wilson 	}
36610be98a7SChris Wilson 
36710be98a7SChris Wilson 	if (!i915_tiling_ok(obj, args->tiling_mode, args->stride)) {
36810be98a7SChris Wilson 		err = -EINVAL;
36910be98a7SChris Wilson 		goto err;
37010be98a7SChris Wilson 	}
37110be98a7SChris Wilson 
37210be98a7SChris Wilson 	if (args->tiling_mode == I915_TILING_NONE) {
37310be98a7SChris Wilson 		args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
37410be98a7SChris Wilson 		args->stride = 0;
37510be98a7SChris Wilson 	} else {
37610be98a7SChris Wilson 		if (args->tiling_mode == I915_TILING_X)
3775c24c9d2SMichał Winiarski 			args->swizzle_mode = to_gt(dev_priv)->ggtt->bit_6_swizzle_x;
37810be98a7SChris Wilson 		else
3795c24c9d2SMichał Winiarski 			args->swizzle_mode = to_gt(dev_priv)->ggtt->bit_6_swizzle_y;
38010be98a7SChris Wilson 
38110be98a7SChris Wilson 		/* Hide bit 17 swizzling from the user.  This prevents old Mesa
38210be98a7SChris Wilson 		 * from aborting the application on sw fallbacks to bit 17,
38310be98a7SChris Wilson 		 * and we use the pread/pwrite bit17 paths to swizzle for it.
38410be98a7SChris Wilson 		 * If there was a user that was relying on the swizzle
38510be98a7SChris Wilson 		 * information for drm_intel_bo_map()ed reads/writes this would
38610be98a7SChris Wilson 		 * break it, but we don't have any of those.
38710be98a7SChris Wilson 		 */
38810be98a7SChris Wilson 		if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
38910be98a7SChris Wilson 			args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
39010be98a7SChris Wilson 		if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
39110be98a7SChris Wilson 			args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
39210be98a7SChris Wilson 
39310be98a7SChris Wilson 		/* If we can't handle the swizzling, make it untiled. */
39410be98a7SChris Wilson 		if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
39510be98a7SChris Wilson 			args->tiling_mode = I915_TILING_NONE;
39610be98a7SChris Wilson 			args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
39710be98a7SChris Wilson 			args->stride = 0;
39810be98a7SChris Wilson 		}
39910be98a7SChris Wilson 	}
40010be98a7SChris Wilson 
40110be98a7SChris Wilson 	err = i915_gem_object_set_tiling(obj, args->tiling_mode, args->stride);
40210be98a7SChris Wilson 
40310be98a7SChris Wilson 	/* We have to maintain this existing ABI... */
40410be98a7SChris Wilson 	args->stride = i915_gem_object_get_stride(obj);
40510be98a7SChris Wilson 	args->tiling_mode = i915_gem_object_get_tiling(obj);
40610be98a7SChris Wilson 
40710be98a7SChris Wilson err:
40810be98a7SChris Wilson 	i915_gem_object_put(obj);
40910be98a7SChris Wilson 	return err;
41010be98a7SChris Wilson }
41110be98a7SChris Wilson 
41210be98a7SChris Wilson /**
41310be98a7SChris Wilson  * i915_gem_get_tiling_ioctl - IOCTL handler to get tiling mode
41410be98a7SChris Wilson  * @dev: DRM device
41510be98a7SChris Wilson  * @data: data pointer for the ioctl
41610be98a7SChris Wilson  * @file: DRM file for the ioctl call
41710be98a7SChris Wilson  *
41810be98a7SChris Wilson  * Returns the current tiling mode and required bit 6 swizzling for the object.
41910be98a7SChris Wilson  *
42010be98a7SChris Wilson  * Called by the user via ioctl.
42110be98a7SChris Wilson  *
42210be98a7SChris Wilson  * Returns:
42310be98a7SChris Wilson  * Zero on success, negative errno on failure.
42410be98a7SChris Wilson  */
42510be98a7SChris Wilson int
i915_gem_get_tiling_ioctl(struct drm_device * dev,void * data,struct drm_file * file)42610be98a7SChris Wilson i915_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
42710be98a7SChris Wilson 			  struct drm_file *file)
42810be98a7SChris Wilson {
42910be98a7SChris Wilson 	struct drm_i915_gem_get_tiling *args = data;
43010be98a7SChris Wilson 	struct drm_i915_private *dev_priv = to_i915(dev);
43110be98a7SChris Wilson 	struct drm_i915_gem_object *obj;
43210be98a7SChris Wilson 	int err = -ENOENT;
43310be98a7SChris Wilson 
4345c24c9d2SMichał Winiarski 	if (!to_gt(dev_priv)->ggtt->num_fences)
435ab016914SDaniel Vetter 		return -EOPNOTSUPP;
436ab016914SDaniel Vetter 
43710be98a7SChris Wilson 	rcu_read_lock();
43810be98a7SChris Wilson 	obj = i915_gem_object_lookup_rcu(file, args->handle);
43910be98a7SChris Wilson 	if (obj) {
44010be98a7SChris Wilson 		args->tiling_mode =
44110be98a7SChris Wilson 			READ_ONCE(obj->tiling_and_stride) & TILING_MASK;
44210be98a7SChris Wilson 		err = 0;
44310be98a7SChris Wilson 	}
44410be98a7SChris Wilson 	rcu_read_unlock();
44510be98a7SChris Wilson 	if (unlikely(err))
44610be98a7SChris Wilson 		return err;
44710be98a7SChris Wilson 
44810be98a7SChris Wilson 	switch (args->tiling_mode) {
44910be98a7SChris Wilson 	case I915_TILING_X:
4505c24c9d2SMichał Winiarski 		args->swizzle_mode = to_gt(dev_priv)->ggtt->bit_6_swizzle_x;
45110be98a7SChris Wilson 		break;
45210be98a7SChris Wilson 	case I915_TILING_Y:
4535c24c9d2SMichał Winiarski 		args->swizzle_mode = to_gt(dev_priv)->ggtt->bit_6_swizzle_y;
45410be98a7SChris Wilson 		break;
45510be98a7SChris Wilson 	default:
45610be98a7SChris Wilson 	case I915_TILING_NONE:
45710be98a7SChris Wilson 		args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
45810be98a7SChris Wilson 		break;
45910be98a7SChris Wilson 	}
46010be98a7SChris Wilson 
46110be98a7SChris Wilson 	/* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
46295086cb9SJani Nikula 	if (dev_priv->gem_quirks & GEM_QUIRK_PIN_SWIZZLED_PAGES)
46310be98a7SChris Wilson 		args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN;
46410be98a7SChris Wilson 	else
46510be98a7SChris Wilson 		args->phys_swizzle_mode = args->swizzle_mode;
46610be98a7SChris Wilson 	if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
46710be98a7SChris Wilson 		args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
46810be98a7SChris Wilson 	if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
46910be98a7SChris Wilson 		args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
47010be98a7SChris Wilson 
47110be98a7SChris Wilson 	return 0;
47210be98a7SChris Wilson }
473