i915_gem_tiling.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright © 2008 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * Authors:
 *    Eric Anholt <eric@anholt.net>
 *
 */

28 29 30 31
#include <linux/string.h>
#include <linux/bitops.h>
#include <drm/drmP.h>
#include <drm/i915_drm.h>
32 33
#include "i915_drv.h"

34 35
/**
 * DOC: buffer object tiling
36
 *
37 38
 * i915_gem_set_tiling() and i915_gem_get_tiling() is the userspace interface to
 * declare fence register requirements.
39
 *
40 41 42
 * In principle GEM doesn't care at all about the internal data layout of an
 * object, and hence it also doesn't care about tiling or swizzling. There's two
 * exceptions:
43
 *
44 45 46 47 48 49 50 51 52 53
 * - For X and Y tiling the hardware provides detilers for CPU access, so called
 *   fences. Since there's only a limited amount of them the kernel must manage
 *   these, and therefore userspace must tell the kernel the object tiling if it
 *   wants to use fences for detiling.
 * - On gen3 and gen4 platforms have a swizzling pattern for tiled objects which
 *   depends upon the physical page frame number. When swapping such objects the
 *   page frame number might change and the kernel must be able to fix this up
 *   and hence now the tiling. Note that on a subset of platforms with
 *   asymmetric memory channel population the swizzling pattern changes in an
 *   unknown way, and for those the kernel simply forbids swapping completely.
54
 *
55 56 57 58
 * Since neither of this applies for new tiling layouts on modern platforms like
 * W, Ys and Yf tiling GEM only allows object tiling to be set to X or Y tiled.
 * Anything else can be handled in userspace entirely without the kernel's
 * invovlement.
59 60
 */

61
/* Check pitch constriants for all chips & tiling formats */
62
static bool
63 64
i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
{
65
	int tile_width;
66 67 68 69 70

	/* Linear is always fine */
	if (tiling_mode == I915_TILING_NONE)
		return true;

71 72 73
	if (tiling_mode > I915_TILING_LAST)
		return false;

74
	if (IS_GEN2(dev) ||
75
	    (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
76 77 78 79
		tile_width = 128;
	else
		tile_width = 512;

80
	/* check maximum stride & object size */
81 82 83 84 85 86
	/* i965+ stores the end address of the gtt mapping in the fence
	 * reg, so dont bother to check the size */
	if (INTEL_INFO(dev)->gen >= 7) {
		if (stride / 128 > GEN7_FENCE_MAX_PITCH_VAL)
			return false;
	} else if (INTEL_INFO(dev)->gen >= 4) {
87 88
		if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
			return false;
89
	} else {
90
		if (stride > 8192)
91
			return false;
92

93 94 95 96 97 98 99
		if (IS_GEN3(dev)) {
			if (size > I830_FENCE_MAX_SIZE_VAL << 20)
				return false;
		} else {
			if (size > I830_FENCE_MAX_SIZE_VAL << 19)
				return false;
		}
100 101
	}

102 103 104
	if (stride < tile_width)
		return false;

105
	/* 965+ just needs multiples of tile width */
106
	if (INTEL_INFO(dev)->gen >= 4) {
107 108 109 110 111 112 113 114 115 116 117 118
		if (stride & (tile_width - 1))
			return false;
		return true;
	}

	/* Pre-965 needs power of two tile widths */
	if (stride & (stride - 1))
		return false;

	return true;
}

119 120
/* Is the current GTT allocation valid for the change in tiling? */
static bool
121
i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
122
{
123
	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
124
	u32 size;
125 126 127 128

	if (tiling_mode == I915_TILING_NONE)
		return true;

129
	if (INTEL_GEN(dev_priv) >= 4)
130 131
		return true;

132
	if (IS_GEN3(dev_priv)) {
133
		if (i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK)
134 135
			return false;
	} else {
136
		if (i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK)
137 138 139
			return false;
	}

140
	size = i915_gem_get_ggtt_size(dev_priv, obj->base.size, tiling_mode);
141
	if (i915_gem_obj_ggtt_size(obj) != size)
142 143
		return false;

144
	if (i915_gem_obj_ggtt_offset(obj) & (size - 1))
145
		return false;
146 147 148 149

	return true;
}

150
/**
151 152 153 154 155
 * i915_gem_set_tiling - IOCTL handler to set tiling mode
 * @dev: DRM device
 * @data: data pointer for the ioctl
 * @file: DRM file for the ioctl call
 *
156 157
 * Sets the tiling mode of an object, returning the required swizzling of
 * bit 6 of addresses in the object.
158 159 160 161 162
 *
 * Called by the user via ioctl.
 *
 * Returns:
 * Zero on success, negative errno on failure.
163 164 165
 */
int
i915_gem_set_tiling(struct drm_device *dev, void *data,
166
		   struct drm_file *file)
167 168
{
	struct drm_i915_gem_set_tiling *args = data;
169
	struct drm_i915_private *dev_priv = to_i915(dev);
170
	struct drm_i915_gem_object *obj;
171
	int ret = 0;
172

173 174 175
	/* Make sure we don't cross-contaminate obj->tiling_and_stride */
	BUILD_BUG_ON(I915_TILING_LAST & STRIDE_MASK);

176 177
	obj = i915_gem_object_lookup(file, args->handle);
	if (!obj)
178
		return -ENOENT;
179

180 181
	if (!i915_tiling_ok(dev,
			    args->stride, obj->base.size, args->tiling_mode)) {
182
		i915_gem_object_put_unlocked(obj);
183
		return -EINVAL;
184
	}
185

186 187
	intel_runtime_pm_get(dev_priv);

188
	mutex_lock(&dev->struct_mutex);
189
	if (obj->pin_display || obj->framebuffer_references) {
190 191
		ret = -EBUSY;
		goto err;
192 193
	}

194 195
	if (args->tiling_mode == I915_TILING_NONE) {
		args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
196
		args->stride = 0;
197 198 199 200 201
	} else {
		if (args->tiling_mode == I915_TILING_X)
			args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
		else
			args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
202 203 204 205 206 207 208 209 210 211 212 213 214

		/* Hide bit 17 swizzling from the user.  This prevents old Mesa
		 * from aborting the application on sw fallbacks to bit 17,
		 * and we use the pread/pwrite bit17 paths to swizzle for it.
		 * If there was a user that was relying on the swizzle
		 * information for drm_intel_bo_map()ed reads/writes this would
		 * break it, but we don't have any of those.
		 */
		if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
			args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
		if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
			args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;

215 216 217 218
		/* If we can't handle the swizzling, make it untiled. */
		if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
			args->tiling_mode = I915_TILING_NONE;
			args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
219
			args->stride = 0;
220 221
		}
	}
222

223 224
	if (args->tiling_mode != i915_gem_object_get_tiling(obj) ||
	    args->stride != i915_gem_object_get_stride(obj)) {
225 226 227
		/* We need to rebind the object if its current allocation
		 * no longer meets the alignment restrictions for its new
		 * tiling mode. Otherwise we can just leave it alone, but
228 229 230
		 * need to ensure that any fence register is updated before
		 * the next fenced (either through the GTT or by the BLT unit
		 * on older GPUs) access.
231 232 233 234 235
		 *
		 * After updating the tiling parameters, we then flag whether
		 * we need to update an associated fence register. Note this
		 * has to also include the unfenced register the GPU uses
		 * whilst executing a fenced command for an untiled object.
236
		 */
237 238
		if (obj->map_and_fenceable &&
		    !i915_gem_object_fence_ok(obj, args->tiling_mode))
239
			ret = i915_vma_unbind(i915_gem_obj_to_ggtt(obj));
240 241

		if (ret == 0) {
242 243 244 245 246
			if (obj->pages &&
			    obj->madv == I915_MADV_WILLNEED &&
			    dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
				if (args->tiling_mode == I915_TILING_NONE)
					i915_gem_object_unpin_pages(obj);
247
				if (!i915_gem_object_is_tiled(obj))
248 249 250
					i915_gem_object_pin_pages(obj);
			}

251
			obj->fence_dirty =
252 253
				!i915_gem_active_is_idle(&obj->last_fence,
							 &dev->struct_mutex) ||
254 255
				obj->fence_reg != I915_FENCE_REG_NONE;

256 257
			obj->tiling_and_stride =
				args->stride | args->tiling_mode;
258 259 260

			/* Force the fence to be reacquired for GTT access */
			i915_gem_release_mmap(obj);
261
		}
262
	}
263
	/* we have to maintain this existing ABI... */
264 265
	args->stride = i915_gem_object_get_stride(obj);
	args->tiling_mode = i915_gem_object_get_tiling(obj);
266 267 268 269

	/* Try to preallocate memory required to save swizzling on put-pages */
	if (i915_gem_object_needs_bit17_swizzle(obj)) {
		if (obj->bit_17 == NULL) {
D
Daniel Vetter 已提交
270
			obj->bit_17 = kcalloc(BITS_TO_LONGS(obj->base.size >> PAGE_SHIFT),
271 272 273 274 275 276 277
					      sizeof(long), GFP_KERNEL);
		}
	} else {
		kfree(obj->bit_17);
		obj->bit_17 = NULL;
	}

278
err:
279
	i915_gem_object_put(obj);
280
	mutex_unlock(&dev->struct_mutex);
281

282 283
	intel_runtime_pm_put(dev_priv);

284
	return ret;
285 286 287
}

/**
288 289 290 291 292
 * i915_gem_get_tiling - IOCTL handler to get tiling mode
 * @dev: DRM device
 * @data: data pointer for the ioctl
 * @file: DRM file for the ioctl call
 *
293
 * Returns the current tiling mode and required bit 6 swizzling for the object.
294 295 296 297 298
 *
 * Called by the user via ioctl.
 *
 * Returns:
 * Zero on success, negative errno on failure.
299 300 301
 */
int
i915_gem_get_tiling(struct drm_device *dev, void *data,
302
		   struct drm_file *file)
303 304
{
	struct drm_i915_gem_get_tiling *args = data;
305
	struct drm_i915_private *dev_priv = to_i915(dev);
306
	struct drm_i915_gem_object *obj;
307

308 309
	obj = i915_gem_object_lookup(file, args->handle);
	if (!obj)
310
		return -ENOENT;
311

312
	args->tiling_mode = READ_ONCE(obj->tiling_and_stride) & TILING_MASK;
313
	switch (args->tiling_mode) {
314 315 316 317 318 319 320 321 322 323 324 325 326
	case I915_TILING_X:
		args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
		break;
	case I915_TILING_Y:
		args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
		break;
	case I915_TILING_NONE:
		args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
		break;
	default:
		DRM_ERROR("unknown tiling mode\n");
	}

327
	/* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
328 329 330 331
	if (dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
		args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN;
	else
		args->phys_swizzle_mode = args->swizzle_mode;
332 333 334 335 336
	if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
		args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
	if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
		args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;

337
	i915_gem_object_put_unlocked(obj);
338 339
	return 0;
}