xref: /dflybsd-src/sys/dev/drm/i915/i915_gem_fence_reg.c (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
14be47400SFrançois Tigeot /*
24be47400SFrançois Tigeot  * Copyright © 2008-2015 Intel Corporation
34be47400SFrançois Tigeot  *
44be47400SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
54be47400SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
64be47400SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
74be47400SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84be47400SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
94be47400SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
104be47400SFrançois Tigeot  *
114be47400SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
124be47400SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
134be47400SFrançois Tigeot  * Software.
144be47400SFrançois Tigeot  *
154be47400SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
164be47400SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174be47400SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
184be47400SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
194be47400SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
204be47400SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
214be47400SFrançois Tigeot  * IN THE SOFTWARE.
224be47400SFrançois Tigeot  */
234be47400SFrançois Tigeot 
244be47400SFrançois Tigeot #include <drm/drmP.h>
254be47400SFrançois Tigeot #include <drm/i915_drm.h>
264be47400SFrançois Tigeot #include "i915_drv.h"
274be47400SFrançois Tigeot 
284be47400SFrançois Tigeot /**
294be47400SFrançois Tigeot  * DOC: fence register handling
304be47400SFrançois Tigeot  *
314be47400SFrançois Tigeot  * Important to avoid confusions: "fences" in the i915 driver are not execution
324be47400SFrançois Tigeot  * fences used to track command completion but hardware detiler objects which
334be47400SFrançois Tigeot  * wrap a given range of the global GTT. Each platform has only a fairly limited
344be47400SFrançois Tigeot  * set of these objects.
354be47400SFrançois Tigeot  *
364be47400SFrançois Tigeot  * Fences are used to detile GTT memory mappings. They're also connected to the
374be47400SFrançois Tigeot  * hardware frontbuffer render tracking and hence interact with frontbuffer
384be47400SFrançois Tigeot  * compression. Furthermore on older platforms fences are required for tiled
394be47400SFrançois Tigeot  * objects used by the display engine. They can also be used by the render
404be47400SFrançois Tigeot  * engine - they're required for blitter commands and are optional for render
414be47400SFrançois Tigeot  * commands. But on gen4+ both display (with the exception of fbc) and rendering
424be47400SFrançois Tigeot  * have their own tiling state bits and don't need fences.
434be47400SFrançois Tigeot  *
444be47400SFrançois Tigeot  * Also note that fences only support X and Y tiling and hence can't be used for
454be47400SFrançois Tigeot  * the fancier new tiling formats like W, Ys and Yf.
464be47400SFrançois Tigeot  *
474be47400SFrançois Tigeot  * Finally note that because fences are such a restricted resource they're
484be47400SFrançois Tigeot  * dynamically associated with objects. Furthermore fence state is committed to
494be47400SFrançois Tigeot  * the hardware lazily to avoid unnecessary stalls on gen2/3. Therefore code must
504be47400SFrançois Tigeot  * explicitly call i915_gem_object_get_fence() to synchronize fencing status
514be47400SFrançois Tigeot  * for cpu access. Also note that some code wants an unfenced view, for those
524be47400SFrançois Tigeot  * cases the fence can be removed forcefully with i915_gem_object_put_fence().
534be47400SFrançois Tigeot  *
544be47400SFrançois Tigeot  * Internally these functions will synchronize with userspace access by removing
554be47400SFrançois Tigeot  * CPU ptes into GTT mmaps (not the GTT ptes themselves) as needed.
564be47400SFrançois Tigeot  */
574be47400SFrançois Tigeot 
584be47400SFrançois Tigeot #define pipelined 0
594be47400SFrançois Tigeot 
i965_write_fence_reg(struct drm_i915_fence_reg * fence,struct i915_vma * vma)604be47400SFrançois Tigeot static void i965_write_fence_reg(struct drm_i915_fence_reg *fence,
614be47400SFrançois Tigeot 				 struct i915_vma *vma)
624be47400SFrançois Tigeot {
634be47400SFrançois Tigeot 	i915_reg_t fence_reg_lo, fence_reg_hi;
644be47400SFrançois Tigeot 	int fence_pitch_shift;
654be47400SFrançois Tigeot 	u64 val;
664be47400SFrançois Tigeot 
674be47400SFrançois Tigeot 	if (INTEL_INFO(fence->i915)->gen >= 6) {
684be47400SFrançois Tigeot 		fence_reg_lo = FENCE_REG_GEN6_LO(fence->id);
694be47400SFrançois Tigeot 		fence_reg_hi = FENCE_REG_GEN6_HI(fence->id);
704be47400SFrançois Tigeot 		fence_pitch_shift = GEN6_FENCE_PITCH_SHIFT;
714be47400SFrançois Tigeot 
724be47400SFrançois Tigeot 	} else {
734be47400SFrançois Tigeot 		fence_reg_lo = FENCE_REG_965_LO(fence->id);
744be47400SFrançois Tigeot 		fence_reg_hi = FENCE_REG_965_HI(fence->id);
754be47400SFrançois Tigeot 		fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
764be47400SFrançois Tigeot 	}
774be47400SFrançois Tigeot 
784be47400SFrançois Tigeot 	val = 0;
794be47400SFrançois Tigeot 	if (vma) {
804be47400SFrançois Tigeot 		unsigned int stride = i915_gem_object_get_stride(vma->obj);
814be47400SFrançois Tigeot 
82a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
83a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!IS_ALIGNED(vma->node.start, I965_FENCE_PAGE));
84a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!IS_ALIGNED(vma->fence_size, I965_FENCE_PAGE));
85a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!IS_ALIGNED(stride, 128));
86a85cb24fSFrançois Tigeot 
87a85cb24fSFrançois Tigeot 		val = (vma->node.start + vma->fence_size - I965_FENCE_PAGE) << 32;
88a85cb24fSFrançois Tigeot 		val |= vma->node.start;
894be47400SFrançois Tigeot 		val |= (u64)((stride / 128) - 1) << fence_pitch_shift;
90a85cb24fSFrançois Tigeot 		if (i915_gem_object_get_tiling(vma->obj) == I915_TILING_Y)
914be47400SFrançois Tigeot 			val |= BIT(I965_FENCE_TILING_Y_SHIFT);
924be47400SFrançois Tigeot 		val |= I965_FENCE_REG_VALID;
934be47400SFrançois Tigeot 	}
944be47400SFrançois Tigeot 
954be47400SFrançois Tigeot 	if (!pipelined) {
964be47400SFrançois Tigeot 		struct drm_i915_private *dev_priv = fence->i915;
974be47400SFrançois Tigeot 
984be47400SFrançois Tigeot 		/* To w/a incoherency with non-atomic 64-bit register updates,
994be47400SFrançois Tigeot 		 * we split the 64-bit update into two 32-bit writes. In order
1004be47400SFrançois Tigeot 		 * for a partial fence not to be evaluated between writes, we
1014be47400SFrançois Tigeot 		 * precede the update with write to turn off the fence register,
1024be47400SFrançois Tigeot 		 * and only enable the fence as the last step.
1034be47400SFrançois Tigeot 		 *
1044be47400SFrançois Tigeot 		 * For extra levels of paranoia, we make sure each step lands
1054be47400SFrançois Tigeot 		 * before applying the next step.
1064be47400SFrançois Tigeot 		 */
1074be47400SFrançois Tigeot 		I915_WRITE(fence_reg_lo, 0);
1084be47400SFrançois Tigeot 		POSTING_READ(fence_reg_lo);
1094be47400SFrançois Tigeot 
1104be47400SFrançois Tigeot 		I915_WRITE(fence_reg_hi, upper_32_bits(val));
1114be47400SFrançois Tigeot 		I915_WRITE(fence_reg_lo, lower_32_bits(val));
1124be47400SFrançois Tigeot 		POSTING_READ(fence_reg_lo);
1134be47400SFrançois Tigeot 	}
1144be47400SFrançois Tigeot }
1154be47400SFrançois Tigeot 
i915_write_fence_reg(struct drm_i915_fence_reg * fence,struct i915_vma * vma)1164be47400SFrançois Tigeot static void i915_write_fence_reg(struct drm_i915_fence_reg *fence,
1174be47400SFrançois Tigeot 				 struct i915_vma *vma)
1184be47400SFrançois Tigeot {
1194be47400SFrançois Tigeot 	u32 val;
1204be47400SFrançois Tigeot 
1214be47400SFrançois Tigeot 	val = 0;
1224be47400SFrançois Tigeot 	if (vma) {
1234be47400SFrançois Tigeot 		unsigned int tiling = i915_gem_object_get_tiling(vma->obj);
1244be47400SFrançois Tigeot 		bool is_y_tiled = tiling == I915_TILING_Y;
1254be47400SFrançois Tigeot 		unsigned int stride = i915_gem_object_get_stride(vma->obj);
1264be47400SFrançois Tigeot 
127a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
128a85cb24fSFrançois Tigeot 		GEM_BUG_ON(vma->node.start & ~I915_FENCE_START_MASK);
129a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!is_power_of_2(vma->fence_size));
130a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!IS_ALIGNED(vma->node.start, vma->fence_size));
1314be47400SFrançois Tigeot 
1324be47400SFrançois Tigeot 		if (is_y_tiled && HAS_128_BYTE_Y_TILING(fence->i915))
133a85cb24fSFrançois Tigeot 			stride /= 128;
1344be47400SFrançois Tigeot 		else
135a85cb24fSFrançois Tigeot 			stride /= 512;
136a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!is_power_of_2(stride));
1374be47400SFrançois Tigeot 
1384be47400SFrançois Tigeot 		val = vma->node.start;
1394be47400SFrançois Tigeot 		if (is_y_tiled)
1404be47400SFrançois Tigeot 			val |= BIT(I830_FENCE_TILING_Y_SHIFT);
141a85cb24fSFrançois Tigeot 		val |= I915_FENCE_SIZE_BITS(vma->fence_size);
142a85cb24fSFrançois Tigeot 		val |= ilog2(stride) << I830_FENCE_PITCH_SHIFT;
143a85cb24fSFrançois Tigeot 
1444be47400SFrançois Tigeot 		val |= I830_FENCE_REG_VALID;
1454be47400SFrançois Tigeot 	}
1464be47400SFrançois Tigeot 
1474be47400SFrançois Tigeot 	if (!pipelined) {
1484be47400SFrançois Tigeot 		struct drm_i915_private *dev_priv = fence->i915;
1494be47400SFrançois Tigeot 		i915_reg_t reg = FENCE_REG(fence->id);
1504be47400SFrançois Tigeot 
1514be47400SFrançois Tigeot 		I915_WRITE(reg, val);
1524be47400SFrançois Tigeot 		POSTING_READ(reg);
1534be47400SFrançois Tigeot 	}
1544be47400SFrançois Tigeot }
1554be47400SFrançois Tigeot 
i830_write_fence_reg(struct drm_i915_fence_reg * fence,struct i915_vma * vma)1564be47400SFrançois Tigeot static void i830_write_fence_reg(struct drm_i915_fence_reg *fence,
1574be47400SFrançois Tigeot 				 struct i915_vma *vma)
1584be47400SFrançois Tigeot {
1594be47400SFrançois Tigeot 	u32 val;
1604be47400SFrançois Tigeot 
1614be47400SFrançois Tigeot 	val = 0;
1624be47400SFrançois Tigeot 	if (vma) {
1634be47400SFrançois Tigeot 		unsigned int stride = i915_gem_object_get_stride(vma->obj);
1644be47400SFrançois Tigeot 
165a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
166a85cb24fSFrançois Tigeot 		GEM_BUG_ON(vma->node.start & ~I830_FENCE_START_MASK);
167a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!is_power_of_2(vma->fence_size));
168a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!is_power_of_2(stride / 128));
169a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!IS_ALIGNED(vma->node.start, vma->fence_size));
1704be47400SFrançois Tigeot 
1714be47400SFrançois Tigeot 		val = vma->node.start;
172a85cb24fSFrançois Tigeot 		if (i915_gem_object_get_tiling(vma->obj) == I915_TILING_Y)
1734be47400SFrançois Tigeot 			val |= BIT(I830_FENCE_TILING_Y_SHIFT);
174a85cb24fSFrançois Tigeot 		val |= I830_FENCE_SIZE_BITS(vma->fence_size);
175a85cb24fSFrançois Tigeot 		val |= ilog2(stride / 128) << I830_FENCE_PITCH_SHIFT;
1764be47400SFrançois Tigeot 		val |= I830_FENCE_REG_VALID;
1774be47400SFrançois Tigeot 	}
1784be47400SFrançois Tigeot 
1794be47400SFrançois Tigeot 	if (!pipelined) {
1804be47400SFrançois Tigeot 		struct drm_i915_private *dev_priv = fence->i915;
1814be47400SFrançois Tigeot 		i915_reg_t reg = FENCE_REG(fence->id);
1824be47400SFrançois Tigeot 
1834be47400SFrançois Tigeot 		I915_WRITE(reg, val);
1844be47400SFrançois Tigeot 		POSTING_READ(reg);
1854be47400SFrançois Tigeot 	}
1864be47400SFrançois Tigeot }
1874be47400SFrançois Tigeot 
fence_write(struct drm_i915_fence_reg * fence,struct i915_vma * vma)1884be47400SFrançois Tigeot static void fence_write(struct drm_i915_fence_reg *fence,
1894be47400SFrançois Tigeot 			struct i915_vma *vma)
1904be47400SFrançois Tigeot {
1914be47400SFrançois Tigeot 	/* Previous access through the fence register is marshalled by
1924be47400SFrançois Tigeot 	 * the mb() inside the fault handlers (i915_gem_release_mmaps)
1934be47400SFrançois Tigeot 	 * and explicitly managed for internal users.
1944be47400SFrançois Tigeot 	 */
1954be47400SFrançois Tigeot 
1964be47400SFrançois Tigeot 	if (IS_GEN2(fence->i915))
1974be47400SFrançois Tigeot 		i830_write_fence_reg(fence, vma);
1984be47400SFrançois Tigeot 	else if (IS_GEN3(fence->i915))
1994be47400SFrançois Tigeot 		i915_write_fence_reg(fence, vma);
2004be47400SFrançois Tigeot 	else
2014be47400SFrançois Tigeot 		i965_write_fence_reg(fence, vma);
2024be47400SFrançois Tigeot 
2034be47400SFrançois Tigeot 	/* Access through the fenced region afterwards is
2044be47400SFrançois Tigeot 	 * ordered by the posting reads whilst writing the registers.
2054be47400SFrançois Tigeot 	 */
2064be47400SFrançois Tigeot 
2074be47400SFrançois Tigeot 	fence->dirty = false;
2084be47400SFrançois Tigeot }
2094be47400SFrançois Tigeot 
fence_update(struct drm_i915_fence_reg * fence,struct i915_vma * vma)2104be47400SFrançois Tigeot static int fence_update(struct drm_i915_fence_reg *fence,
2114be47400SFrançois Tigeot 			struct i915_vma *vma)
2124be47400SFrançois Tigeot {
2134be47400SFrançois Tigeot 	int ret;
2144be47400SFrançois Tigeot 
2154be47400SFrançois Tigeot 	if (vma) {
2164be47400SFrançois Tigeot 		if (!i915_vma_is_map_and_fenceable(vma))
2174be47400SFrançois Tigeot 			return -EINVAL;
2184be47400SFrançois Tigeot 
2194be47400SFrançois Tigeot 		if (WARN(!i915_gem_object_get_stride(vma->obj) ||
2204be47400SFrançois Tigeot 			 !i915_gem_object_get_tiling(vma->obj),
2214be47400SFrançois Tigeot 			 "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
2224be47400SFrançois Tigeot 			 i915_gem_object_get_stride(vma->obj),
2234be47400SFrançois Tigeot 			 i915_gem_object_get_tiling(vma->obj)))
2244be47400SFrançois Tigeot 			return -EINVAL;
2254be47400SFrançois Tigeot 
2264be47400SFrançois Tigeot 		ret = i915_gem_active_retire(&vma->last_fence,
2274be47400SFrançois Tigeot 					     &vma->obj->base.dev->struct_mutex);
2284be47400SFrançois Tigeot 		if (ret)
2294be47400SFrançois Tigeot 			return ret;
2304be47400SFrançois Tigeot 	}
2314be47400SFrançois Tigeot 
2324be47400SFrançois Tigeot 	if (fence->vma) {
2334be47400SFrançois Tigeot 		ret = i915_gem_active_retire(&fence->vma->last_fence,
2344be47400SFrançois Tigeot 				      &fence->vma->obj->base.dev->struct_mutex);
2354be47400SFrançois Tigeot 		if (ret)
2364be47400SFrançois Tigeot 			return ret;
2374be47400SFrançois Tigeot 	}
2384be47400SFrançois Tigeot 
2394be47400SFrançois Tigeot 	if (fence->vma && fence->vma != vma) {
2404be47400SFrançois Tigeot 		/* Ensure that all userspace CPU access is completed before
2414be47400SFrançois Tigeot 		 * stealing the fence.
2424be47400SFrançois Tigeot 		 */
243*3f2dd94aSFrançois Tigeot 		GEM_BUG_ON(fence->vma->fence != fence);
244*3f2dd94aSFrançois Tigeot 		i915_vma_revoke_mmap(fence->vma);
2454be47400SFrançois Tigeot 
2464be47400SFrançois Tigeot 		fence->vma->fence = NULL;
2474be47400SFrançois Tigeot 		fence->vma = NULL;
2484be47400SFrançois Tigeot 
2494be47400SFrançois Tigeot 		list_move(&fence->link, &fence->i915->mm.fence_list);
2504be47400SFrançois Tigeot 	}
2514be47400SFrançois Tigeot 
252a85cb24fSFrançois Tigeot 	/* We only need to update the register itself if the device is awake.
253a85cb24fSFrançois Tigeot 	 * If the device is currently powered down, we will defer the write
254a85cb24fSFrançois Tigeot 	 * to the runtime resume, see i915_gem_restore_fences().
255a85cb24fSFrançois Tigeot 	 */
256a85cb24fSFrançois Tigeot 	if (intel_runtime_pm_get_if_in_use(fence->i915)) {
2574be47400SFrançois Tigeot 		fence_write(fence, vma);
258a85cb24fSFrançois Tigeot 		intel_runtime_pm_put(fence->i915);
259a85cb24fSFrançois Tigeot 	}
2604be47400SFrançois Tigeot 
2614be47400SFrançois Tigeot 	if (vma) {
2624be47400SFrançois Tigeot 		if (fence->vma != vma) {
2634be47400SFrançois Tigeot 			vma->fence = fence;
2644be47400SFrançois Tigeot 			fence->vma = vma;
2654be47400SFrançois Tigeot 		}
2664be47400SFrançois Tigeot 
2674be47400SFrançois Tigeot 		list_move_tail(&fence->link, &fence->i915->mm.fence_list);
2684be47400SFrançois Tigeot 	}
2694be47400SFrançois Tigeot 
2704be47400SFrançois Tigeot 	return 0;
2714be47400SFrançois Tigeot }
2724be47400SFrançois Tigeot 
2734be47400SFrançois Tigeot /**
2744be47400SFrançois Tigeot  * i915_vma_put_fence - force-remove fence for a VMA
2754be47400SFrançois Tigeot  * @vma: vma to map linearly (not through a fence reg)
2764be47400SFrançois Tigeot  *
2774be47400SFrançois Tigeot  * This function force-removes any fence from the given object, which is useful
2784be47400SFrançois Tigeot  * if the kernel wants to do untiled GTT access.
2794be47400SFrançois Tigeot  *
2804be47400SFrançois Tigeot  * Returns:
2814be47400SFrançois Tigeot  *
2824be47400SFrançois Tigeot  * 0 on success, negative error code on failure.
2834be47400SFrançois Tigeot  */
i915_vma_put_fence(struct i915_vma * vma)284*3f2dd94aSFrançois Tigeot int i915_vma_put_fence(struct i915_vma *vma)
2854be47400SFrançois Tigeot {
2864be47400SFrançois Tigeot 	struct drm_i915_fence_reg *fence = vma->fence;
2874be47400SFrançois Tigeot 
2884be47400SFrançois Tigeot 	if (!fence)
2894be47400SFrançois Tigeot 		return 0;
2904be47400SFrançois Tigeot 
2914be47400SFrançois Tigeot 	if (fence->pin_count)
2924be47400SFrançois Tigeot 		return -EBUSY;
2934be47400SFrançois Tigeot 
2944be47400SFrançois Tigeot 	return fence_update(fence, NULL);
2954be47400SFrançois Tigeot }
2964be47400SFrançois Tigeot 
fence_find(struct drm_i915_private * dev_priv)2974be47400SFrançois Tigeot static struct drm_i915_fence_reg *fence_find(struct drm_i915_private *dev_priv)
2984be47400SFrançois Tigeot {
2994be47400SFrançois Tigeot 	struct drm_i915_fence_reg *fence;
3004be47400SFrançois Tigeot 
3014be47400SFrançois Tigeot 	list_for_each_entry(fence, &dev_priv->mm.fence_list, link) {
302*3f2dd94aSFrançois Tigeot 		GEM_BUG_ON(fence->vma && fence->vma->fence != fence);
303*3f2dd94aSFrançois Tigeot 
3044be47400SFrançois Tigeot 		if (fence->pin_count)
3054be47400SFrançois Tigeot 			continue;
3064be47400SFrançois Tigeot 
3074be47400SFrançois Tigeot 		return fence;
3084be47400SFrançois Tigeot 	}
3094be47400SFrançois Tigeot 
3104be47400SFrançois Tigeot 	/* Wait for completion of pending flips which consume fences */
311a85cb24fSFrançois Tigeot 	if (intel_has_pending_fb_unpin(dev_priv))
3124be47400SFrançois Tigeot 		return ERR_PTR(-EAGAIN);
3134be47400SFrançois Tigeot 
3144be47400SFrançois Tigeot 	return ERR_PTR(-EDEADLK);
3154be47400SFrançois Tigeot }
3164be47400SFrançois Tigeot 
3174be47400SFrançois Tigeot /**
318*3f2dd94aSFrançois Tigeot  * i915_vma_pin_fence - set up fencing for a vma
3194be47400SFrançois Tigeot  * @vma: vma to map through a fence reg
3204be47400SFrançois Tigeot  *
3214be47400SFrançois Tigeot  * When mapping objects through the GTT, userspace wants to be able to write
3224be47400SFrançois Tigeot  * to them without having to worry about swizzling if the object is tiled.
3234be47400SFrançois Tigeot  * This function walks the fence regs looking for a free one for @obj,
3244be47400SFrançois Tigeot  * stealing one if it can't find any.
3254be47400SFrançois Tigeot  *
3264be47400SFrançois Tigeot  * It then sets up the reg based on the object's properties: address, pitch
3274be47400SFrançois Tigeot  * and tiling format.
3284be47400SFrançois Tigeot  *
3294be47400SFrançois Tigeot  * For an untiled surface, this removes any existing fence.
3304be47400SFrançois Tigeot  *
3314be47400SFrançois Tigeot  * Returns:
3324be47400SFrançois Tigeot  *
3334be47400SFrançois Tigeot  * 0 on success, negative error code on failure.
3344be47400SFrançois Tigeot  */
3354be47400SFrançois Tigeot int
i915_vma_pin_fence(struct i915_vma * vma)336*3f2dd94aSFrançois Tigeot i915_vma_pin_fence(struct i915_vma *vma)
3374be47400SFrançois Tigeot {
3384be47400SFrançois Tigeot 	struct drm_i915_fence_reg *fence;
3394be47400SFrançois Tigeot 	struct i915_vma *set = i915_gem_object_is_tiled(vma->obj) ? vma : NULL;
340*3f2dd94aSFrançois Tigeot 	int err;
3414be47400SFrançois Tigeot 
3424be47400SFrançois Tigeot 	/* Note that we revoke fences on runtime suspend. Therefore the user
3434be47400SFrançois Tigeot 	 * must keep the device awake whilst using the fence.
3444be47400SFrançois Tigeot 	 */
345a85cb24fSFrançois Tigeot 	assert_rpm_wakelock_held(vma->vm->i915);
3464be47400SFrançois Tigeot 
3474be47400SFrançois Tigeot 	/* Just update our place in the LRU if our fence is getting reused. */
3484be47400SFrançois Tigeot 	if (vma->fence) {
3494be47400SFrançois Tigeot 		fence = vma->fence;
350*3f2dd94aSFrançois Tigeot 		GEM_BUG_ON(fence->vma != vma);
351*3f2dd94aSFrançois Tigeot 		fence->pin_count++;
3524be47400SFrançois Tigeot 		if (!fence->dirty) {
3534be47400SFrançois Tigeot 			list_move_tail(&fence->link,
3544be47400SFrançois Tigeot 				       &fence->i915->mm.fence_list);
3554be47400SFrançois Tigeot 			return 0;
3564be47400SFrançois Tigeot 		}
3574be47400SFrançois Tigeot 	} else if (set) {
358a85cb24fSFrançois Tigeot 		fence = fence_find(vma->vm->i915);
3594be47400SFrançois Tigeot 		if (IS_ERR(fence))
3604be47400SFrançois Tigeot 			return PTR_ERR(fence);
361*3f2dd94aSFrançois Tigeot 
362*3f2dd94aSFrançois Tigeot 		GEM_BUG_ON(fence->pin_count);
363*3f2dd94aSFrançois Tigeot 		fence->pin_count++;
3644be47400SFrançois Tigeot 	} else
3654be47400SFrançois Tigeot 		return 0;
3664be47400SFrançois Tigeot 
367*3f2dd94aSFrançois Tigeot 	err = fence_update(fence, set);
368*3f2dd94aSFrançois Tigeot 	if (err)
369*3f2dd94aSFrançois Tigeot 		goto out_unpin;
370*3f2dd94aSFrançois Tigeot 
371*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(fence->vma != set);
372*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(vma->fence != (set ? fence : NULL));
373*3f2dd94aSFrançois Tigeot 
374*3f2dd94aSFrançois Tigeot 	if (set)
375*3f2dd94aSFrançois Tigeot 		return 0;
376*3f2dd94aSFrançois Tigeot 
377*3f2dd94aSFrançois Tigeot out_unpin:
378*3f2dd94aSFrançois Tigeot 	fence->pin_count--;
379*3f2dd94aSFrançois Tigeot 	return err;
380*3f2dd94aSFrançois Tigeot }
381*3f2dd94aSFrançois Tigeot 
382*3f2dd94aSFrançois Tigeot /**
383*3f2dd94aSFrançois Tigeot  * i915_reserve_fence - Reserve a fence for vGPU
384*3f2dd94aSFrançois Tigeot  * @dev_priv: i915 device private
385*3f2dd94aSFrançois Tigeot  *
386*3f2dd94aSFrançois Tigeot  * This function walks the fence regs looking for a free one and remove
387*3f2dd94aSFrançois Tigeot  * it from the fence_list. It is used to reserve fence for vGPU to use.
388*3f2dd94aSFrançois Tigeot  */
389*3f2dd94aSFrançois Tigeot struct drm_i915_fence_reg *
i915_reserve_fence(struct drm_i915_private * dev_priv)390*3f2dd94aSFrançois Tigeot i915_reserve_fence(struct drm_i915_private *dev_priv)
391*3f2dd94aSFrançois Tigeot {
392*3f2dd94aSFrançois Tigeot 	struct drm_i915_fence_reg *fence;
393*3f2dd94aSFrançois Tigeot 	int count;
394*3f2dd94aSFrançois Tigeot 	int ret;
395*3f2dd94aSFrançois Tigeot 
396*3f2dd94aSFrançois Tigeot 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
397*3f2dd94aSFrançois Tigeot 
398*3f2dd94aSFrançois Tigeot 	/* Keep at least one fence available for the display engine. */
399*3f2dd94aSFrançois Tigeot 	count = 0;
400*3f2dd94aSFrançois Tigeot 	list_for_each_entry(fence, &dev_priv->mm.fence_list, link)
401*3f2dd94aSFrançois Tigeot 		count += !fence->pin_count;
402*3f2dd94aSFrançois Tigeot 	if (count <= 1)
403*3f2dd94aSFrançois Tigeot 		return ERR_PTR(-ENOSPC);
404*3f2dd94aSFrançois Tigeot 
405*3f2dd94aSFrançois Tigeot 	fence = fence_find(dev_priv);
406*3f2dd94aSFrançois Tigeot 	if (IS_ERR(fence))
407*3f2dd94aSFrançois Tigeot 		return fence;
408*3f2dd94aSFrançois Tigeot 
409*3f2dd94aSFrançois Tigeot 	if (fence->vma) {
410*3f2dd94aSFrançois Tigeot 		/* Force-remove fence from VMA */
411*3f2dd94aSFrançois Tigeot 		ret = fence_update(fence, NULL);
412*3f2dd94aSFrançois Tigeot 		if (ret)
413*3f2dd94aSFrançois Tigeot 			return ERR_PTR(ret);
414*3f2dd94aSFrançois Tigeot 	}
415*3f2dd94aSFrançois Tigeot 
416*3f2dd94aSFrançois Tigeot 	list_del(&fence->link);
417*3f2dd94aSFrançois Tigeot 	return fence;
418*3f2dd94aSFrançois Tigeot }
419*3f2dd94aSFrançois Tigeot 
420*3f2dd94aSFrançois Tigeot /**
421*3f2dd94aSFrançois Tigeot  * i915_unreserve_fence - Reclaim a reserved fence
422*3f2dd94aSFrançois Tigeot  * @fence: the fence reg
423*3f2dd94aSFrançois Tigeot  *
424*3f2dd94aSFrançois Tigeot  * This function add a reserved fence register from vGPU to the fence_list.
425*3f2dd94aSFrançois Tigeot  */
i915_unreserve_fence(struct drm_i915_fence_reg * fence)426*3f2dd94aSFrançois Tigeot void i915_unreserve_fence(struct drm_i915_fence_reg *fence)
427*3f2dd94aSFrançois Tigeot {
428*3f2dd94aSFrançois Tigeot 	lockdep_assert_held(&fence->i915->drm.struct_mutex);
429*3f2dd94aSFrançois Tigeot 
430*3f2dd94aSFrançois Tigeot 	list_add(&fence->link, &fence->i915->mm.fence_list);
4314be47400SFrançois Tigeot }
4324be47400SFrançois Tigeot 
4334be47400SFrançois Tigeot /**
434a85cb24fSFrançois Tigeot  * i915_gem_revoke_fences - revoke fence state
435a85cb24fSFrançois Tigeot  * @dev_priv: i915 device private
436a85cb24fSFrançois Tigeot  *
437a85cb24fSFrançois Tigeot  * Removes all GTT mmappings via the fence registers. This forces any user
438a85cb24fSFrançois Tigeot  * of the fence to reacquire that fence before continuing with their access.
439a85cb24fSFrançois Tigeot  * One use is during GPU reset where the fence register is lost and we need to
440a85cb24fSFrançois Tigeot  * revoke concurrent userspace access via GTT mmaps until the hardware has been
441a85cb24fSFrançois Tigeot  * reset and the fence registers have been restored.
442a85cb24fSFrançois Tigeot  */
i915_gem_revoke_fences(struct drm_i915_private * dev_priv)443a85cb24fSFrançois Tigeot void i915_gem_revoke_fences(struct drm_i915_private *dev_priv)
444a85cb24fSFrançois Tigeot {
445a85cb24fSFrançois Tigeot 	int i;
446a85cb24fSFrançois Tigeot 
447a85cb24fSFrançois Tigeot 	lockdep_assert_held(&dev_priv->drm.struct_mutex);
448a85cb24fSFrançois Tigeot 
449a85cb24fSFrançois Tigeot 	for (i = 0; i < dev_priv->num_fence_regs; i++) {
450a85cb24fSFrançois Tigeot 		struct drm_i915_fence_reg *fence = &dev_priv->fence_regs[i];
451a85cb24fSFrançois Tigeot 
452*3f2dd94aSFrançois Tigeot 		GEM_BUG_ON(fence->vma && fence->vma->fence != fence);
453*3f2dd94aSFrançois Tigeot 
454a85cb24fSFrançois Tigeot 		if (fence->vma)
455*3f2dd94aSFrançois Tigeot 			i915_vma_revoke_mmap(fence->vma);
456a85cb24fSFrançois Tigeot 	}
457a85cb24fSFrançois Tigeot }
458a85cb24fSFrançois Tigeot 
459a85cb24fSFrançois Tigeot /**
4604be47400SFrançois Tigeot  * i915_gem_restore_fences - restore fence state
4614be47400SFrançois Tigeot  * @dev_priv: i915 device private
4624be47400SFrançois Tigeot  *
4634be47400SFrançois Tigeot  * Restore the hw fence state to match the software tracking again, to be called
4644be47400SFrançois Tigeot  * after a gpu reset and on resume. Note that on runtime suspend we only cancel
4654be47400SFrançois Tigeot  * the fences, to be reacquired by the user later.
4664be47400SFrançois Tigeot  */
i915_gem_restore_fences(struct drm_i915_private * dev_priv)4674be47400SFrançois Tigeot void i915_gem_restore_fences(struct drm_i915_private *dev_priv)
4684be47400SFrançois Tigeot {
4694be47400SFrançois Tigeot 	int i;
4704be47400SFrançois Tigeot 
4714be47400SFrançois Tigeot 	for (i = 0; i < dev_priv->num_fence_regs; i++) {
4724be47400SFrançois Tigeot 		struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
4734be47400SFrançois Tigeot 		struct i915_vma *vma = reg->vma;
4744be47400SFrançois Tigeot 
475*3f2dd94aSFrançois Tigeot 		GEM_BUG_ON(vma && vma->fence != reg);
476*3f2dd94aSFrançois Tigeot 
4774be47400SFrançois Tigeot 		/*
4784be47400SFrançois Tigeot 		 * Commit delayed tiling changes if we have an object still
4794be47400SFrançois Tigeot 		 * attached to the fence, otherwise just clear the fence.
4804be47400SFrançois Tigeot 		 */
4814be47400SFrançois Tigeot 		if (vma && !i915_gem_object_is_tiled(vma->obj)) {
4824be47400SFrançois Tigeot 			GEM_BUG_ON(!reg->dirty);
483*3f2dd94aSFrançois Tigeot 			GEM_BUG_ON(i915_vma_has_userfault(vma));
4844be47400SFrançois Tigeot 
4854be47400SFrançois Tigeot 			list_move(&reg->link, &dev_priv->mm.fence_list);
4864be47400SFrançois Tigeot 			vma->fence = NULL;
4874be47400SFrançois Tigeot 			vma = NULL;
4884be47400SFrançois Tigeot 		}
4894be47400SFrançois Tigeot 
4904be47400SFrançois Tigeot 		fence_write(reg, vma);
4914be47400SFrançois Tigeot 		reg->vma = vma;
4924be47400SFrançois Tigeot 	}
4934be47400SFrançois Tigeot }
4944be47400SFrançois Tigeot 
4954be47400SFrançois Tigeot /**
4964be47400SFrançois Tigeot  * DOC: tiling swizzling details
4974be47400SFrançois Tigeot  *
4984be47400SFrançois Tigeot  * The idea behind tiling is to increase cache hit rates by rearranging
4994be47400SFrançois Tigeot  * pixel data so that a group of pixel accesses are in the same cacheline.
5004be47400SFrançois Tigeot  * Performance improvement from doing this on the back/depth buffer are on
5014be47400SFrançois Tigeot  * the order of 30%.
5024be47400SFrançois Tigeot  *
5034be47400SFrançois Tigeot  * Intel architectures make this somewhat more complicated, though, by
5044be47400SFrançois Tigeot  * adjustments made to addressing of data when the memory is in interleaved
5054be47400SFrançois Tigeot  * mode (matched pairs of DIMMS) to improve memory bandwidth.
5064be47400SFrançois Tigeot  * For interleaved memory, the CPU sends every sequential 64 bytes
5074be47400SFrançois Tigeot  * to an alternate memory channel so it can get the bandwidth from both.
5084be47400SFrançois Tigeot  *
5094be47400SFrançois Tigeot  * The GPU also rearranges its accesses for increased bandwidth to interleaved
5104be47400SFrançois Tigeot  * memory, and it matches what the CPU does for non-tiled.  However, when tiled
5114be47400SFrançois Tigeot  * it does it a little differently, since one walks addresses not just in the
5124be47400SFrançois Tigeot  * X direction but also Y.  So, along with alternating channels when bit
5134be47400SFrançois Tigeot  * 6 of the address flips, it also alternates when other bits flip --  Bits 9
5144be47400SFrançois Tigeot  * (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)
5154be47400SFrançois Tigeot  * are common to both the 915 and 965-class hardware.
5164be47400SFrançois Tigeot  *
5174be47400SFrançois Tigeot  * The CPU also sometimes XORs in higher bits as well, to improve
5184be47400SFrançois Tigeot  * bandwidth doing strided access like we do so frequently in graphics.  This
5194be47400SFrançois Tigeot  * is called "Channel XOR Randomization" in the MCH documentation.  The result
5204be47400SFrançois Tigeot  * is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address
5214be47400SFrançois Tigeot  * decode.
5224be47400SFrançois Tigeot  *
5234be47400SFrançois Tigeot  * All of this bit 6 XORing has an effect on our memory management,
5244be47400SFrançois Tigeot  * as we need to make sure that the 3d driver can correctly address object
5254be47400SFrançois Tigeot  * contents.
5264be47400SFrançois Tigeot  *
5274be47400SFrançois Tigeot  * If we don't have interleaved memory, all tiling is safe and no swizzling is
5284be47400SFrançois Tigeot  * required.
5294be47400SFrançois Tigeot  *
5304be47400SFrançois Tigeot  * When bit 17 is XORed in, we simply refuse to tile at all.  Bit
5314be47400SFrançois Tigeot  * 17 is not just a page offset, so as we page an object out and back in,
5324be47400SFrançois Tigeot  * individual pages in it will have different bit 17 addresses, resulting in
5334be47400SFrançois Tigeot  * each 64 bytes being swapped with its neighbor!
5344be47400SFrançois Tigeot  *
5354be47400SFrançois Tigeot  * Otherwise, if interleaved, we have to tell the 3d driver what the address
5364be47400SFrançois Tigeot  * swizzling it needs to do is, since it's writing with the CPU to the pages
5374be47400SFrançois Tigeot  * (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the
5384be47400SFrançois Tigeot  * pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling
5394be47400SFrançois Tigeot  * required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order
5404be47400SFrançois Tigeot  * to match what the GPU expects.
5414be47400SFrançois Tigeot  */
5424be47400SFrançois Tigeot 
5434be47400SFrançois Tigeot /**
5444be47400SFrançois Tigeot  * i915_gem_detect_bit_6_swizzle - detect bit 6 swizzling pattern
5454be47400SFrançois Tigeot  * @dev_priv: i915 device private
5464be47400SFrançois Tigeot  *
5474be47400SFrançois Tigeot  * Detects bit 6 swizzling of address lookup between IGD access and CPU
5484be47400SFrançois Tigeot  * access through main memory.
5494be47400SFrançois Tigeot  */
5504be47400SFrançois Tigeot void
i915_gem_detect_bit_6_swizzle(struct drm_i915_private * dev_priv)5514be47400SFrançois Tigeot i915_gem_detect_bit_6_swizzle(struct drm_i915_private *dev_priv)
5524be47400SFrançois Tigeot {
5534be47400SFrançois Tigeot 	uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
5544be47400SFrançois Tigeot 	uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
5554be47400SFrançois Tigeot 
5564be47400SFrançois Tigeot 	if (INTEL_GEN(dev_priv) >= 8 || IS_VALLEYVIEW(dev_priv)) {
5574be47400SFrançois Tigeot 		/*
5584be47400SFrançois Tigeot 		 * On BDW+, swizzling is not used. We leave the CPU memory
5594be47400SFrançois Tigeot 		 * controller in charge of optimizing memory accesses without
5604be47400SFrançois Tigeot 		 * the extra address manipulation GPU side.
5614be47400SFrançois Tigeot 		 *
5624be47400SFrançois Tigeot 		 * VLV and CHV don't have GPU swizzling.
5634be47400SFrançois Tigeot 		 */
5644be47400SFrançois Tigeot 		swizzle_x = I915_BIT_6_SWIZZLE_NONE;
5654be47400SFrançois Tigeot 		swizzle_y = I915_BIT_6_SWIZZLE_NONE;
5664be47400SFrançois Tigeot 	} else if (INTEL_GEN(dev_priv) >= 6) {
5674be47400SFrançois Tigeot 		if (dev_priv->preserve_bios_swizzle) {
5684be47400SFrançois Tigeot 			if (I915_READ(DISP_ARB_CTL) &
5694be47400SFrançois Tigeot 			    DISP_TILE_SURFACE_SWIZZLING) {
5704be47400SFrançois Tigeot 				swizzle_x = I915_BIT_6_SWIZZLE_9_10;
5714be47400SFrançois Tigeot 				swizzle_y = I915_BIT_6_SWIZZLE_9;
5724be47400SFrançois Tigeot 			} else {
5734be47400SFrançois Tigeot 				swizzle_x = I915_BIT_6_SWIZZLE_NONE;
5744be47400SFrançois Tigeot 				swizzle_y = I915_BIT_6_SWIZZLE_NONE;
5754be47400SFrançois Tigeot 			}
5764be47400SFrançois Tigeot 		} else {
5774be47400SFrançois Tigeot 			uint32_t dimm_c0, dimm_c1;
5784be47400SFrançois Tigeot 			dimm_c0 = I915_READ(MAD_DIMM_C0);
5794be47400SFrançois Tigeot 			dimm_c1 = I915_READ(MAD_DIMM_C1);
5804be47400SFrançois Tigeot 			dimm_c0 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
5814be47400SFrançois Tigeot 			dimm_c1 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
5824be47400SFrançois Tigeot 			/* Enable swizzling when the channels are populated
5834be47400SFrançois Tigeot 			 * with identically sized dimms. We don't need to check
5844be47400SFrançois Tigeot 			 * the 3rd channel because no cpu with gpu attached
5854be47400SFrançois Tigeot 			 * ships in that configuration. Also, swizzling only
5864be47400SFrançois Tigeot 			 * makes sense for 2 channels anyway. */
5874be47400SFrançois Tigeot 			if (dimm_c0 == dimm_c1) {
5884be47400SFrançois Tigeot 				swizzle_x = I915_BIT_6_SWIZZLE_9_10;
5894be47400SFrançois Tigeot 				swizzle_y = I915_BIT_6_SWIZZLE_9;
5904be47400SFrançois Tigeot 			} else {
5914be47400SFrançois Tigeot 				swizzle_x = I915_BIT_6_SWIZZLE_NONE;
5924be47400SFrançois Tigeot 				swizzle_y = I915_BIT_6_SWIZZLE_NONE;
5934be47400SFrançois Tigeot 			}
5944be47400SFrançois Tigeot 		}
5954be47400SFrançois Tigeot 	} else if (IS_GEN5(dev_priv)) {
5964be47400SFrançois Tigeot 		/* On Ironlake whatever DRAM config, GPU always do
5974be47400SFrançois Tigeot 		 * same swizzling setup.
5984be47400SFrançois Tigeot 		 */
5994be47400SFrançois Tigeot 		swizzle_x = I915_BIT_6_SWIZZLE_9_10;
6004be47400SFrançois Tigeot 		swizzle_y = I915_BIT_6_SWIZZLE_9;
6014be47400SFrançois Tigeot 	} else if (IS_GEN2(dev_priv)) {
6024be47400SFrançois Tigeot 		/* As far as we know, the 865 doesn't have these bit 6
6034be47400SFrançois Tigeot 		 * swizzling issues.
6044be47400SFrançois Tigeot 		 */
6054be47400SFrançois Tigeot 		swizzle_x = I915_BIT_6_SWIZZLE_NONE;
6064be47400SFrançois Tigeot 		swizzle_y = I915_BIT_6_SWIZZLE_NONE;
607a85cb24fSFrançois Tigeot 	} else if (IS_MOBILE(dev_priv) ||
608a85cb24fSFrançois Tigeot 		   IS_I915G(dev_priv) || IS_I945G(dev_priv)) {
6094be47400SFrançois Tigeot 		uint32_t dcc;
6104be47400SFrançois Tigeot 
6114be47400SFrançois Tigeot 		/* On 9xx chipsets, channel interleave by the CPU is
6124be47400SFrançois Tigeot 		 * determined by DCC.  For single-channel, neither the CPU
6134be47400SFrançois Tigeot 		 * nor the GPU do swizzling.  For dual channel interleaved,
6144be47400SFrançois Tigeot 		 * the GPU's interleave is bit 9 and 10 for X tiled, and bit
6154be47400SFrançois Tigeot 		 * 9 for Y tiled.  The CPU's interleave is independent, and
6164be47400SFrançois Tigeot 		 * can be based on either bit 11 (haven't seen this yet) or
6174be47400SFrançois Tigeot 		 * bit 17 (common).
6184be47400SFrançois Tigeot 		 */
6194be47400SFrançois Tigeot 		dcc = I915_READ(DCC);
6204be47400SFrançois Tigeot 		switch (dcc & DCC_ADDRESSING_MODE_MASK) {
6214be47400SFrançois Tigeot 		case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
6224be47400SFrançois Tigeot 		case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
6234be47400SFrançois Tigeot 			swizzle_x = I915_BIT_6_SWIZZLE_NONE;
6244be47400SFrançois Tigeot 			swizzle_y = I915_BIT_6_SWIZZLE_NONE;
6254be47400SFrançois Tigeot 			break;
6264be47400SFrançois Tigeot 		case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
6274be47400SFrançois Tigeot 			if (dcc & DCC_CHANNEL_XOR_DISABLE) {
6284be47400SFrançois Tigeot 				/* This is the base swizzling by the GPU for
6294be47400SFrançois Tigeot 				 * tiled buffers.
6304be47400SFrançois Tigeot 				 */
6314be47400SFrançois Tigeot 				swizzle_x = I915_BIT_6_SWIZZLE_9_10;
6324be47400SFrançois Tigeot 				swizzle_y = I915_BIT_6_SWIZZLE_9;
6334be47400SFrançois Tigeot 			} else if ((dcc & DCC_CHANNEL_XOR_BIT_17) == 0) {
6344be47400SFrançois Tigeot 				/* Bit 11 swizzling by the CPU in addition. */
6354be47400SFrançois Tigeot 				swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
6364be47400SFrançois Tigeot 				swizzle_y = I915_BIT_6_SWIZZLE_9_11;
6374be47400SFrançois Tigeot 			} else {
6384be47400SFrançois Tigeot 				/* Bit 17 swizzling by the CPU in addition. */
6394be47400SFrançois Tigeot 				swizzle_x = I915_BIT_6_SWIZZLE_9_10_17;
6404be47400SFrançois Tigeot 				swizzle_y = I915_BIT_6_SWIZZLE_9_17;
6414be47400SFrançois Tigeot 			}
6424be47400SFrançois Tigeot 			break;
6434be47400SFrançois Tigeot 		}
6444be47400SFrançois Tigeot 
6454be47400SFrançois Tigeot 		/* check for L-shaped memory aka modified enhanced addressing */
6464be47400SFrançois Tigeot 		if (IS_GEN4(dev_priv) &&
6474be47400SFrançois Tigeot 		    !(I915_READ(DCC2) & DCC2_MODIFIED_ENHANCED_DISABLE)) {
6484be47400SFrançois Tigeot 			swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
6494be47400SFrançois Tigeot 			swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
6504be47400SFrançois Tigeot 		}
6514be47400SFrançois Tigeot 
6524be47400SFrançois Tigeot 		if (dcc == 0xffffffff) {
6534be47400SFrançois Tigeot 			DRM_ERROR("Couldn't read from MCHBAR.  "
6544be47400SFrançois Tigeot 				  "Disabling tiling.\n");
6554be47400SFrançois Tigeot 			swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
6564be47400SFrançois Tigeot 			swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
6574be47400SFrançois Tigeot 		}
6584be47400SFrançois Tigeot 	} else {
6594be47400SFrançois Tigeot 		/* The 965, G33, and newer, have a very flexible memory
6604be47400SFrançois Tigeot 		 * configuration.  It will enable dual-channel mode
6614be47400SFrançois Tigeot 		 * (interleaving) on as much memory as it can, and the GPU
6624be47400SFrançois Tigeot 		 * will additionally sometimes enable different bit 6
6634be47400SFrançois Tigeot 		 * swizzling for tiled objects from the CPU.
6644be47400SFrançois Tigeot 		 *
6654be47400SFrançois Tigeot 		 * Here's what I found on the G965:
6664be47400SFrançois Tigeot 		 *    slot fill         memory size  swizzling
6674be47400SFrançois Tigeot 		 * 0A   0B   1A   1B    1-ch   2-ch
6684be47400SFrançois Tigeot 		 * 512  0    0    0     512    0     O
6694be47400SFrançois Tigeot 		 * 512  0    512  0     16     1008  X
6704be47400SFrançois Tigeot 		 * 512  0    0    512   16     1008  X
6714be47400SFrançois Tigeot 		 * 0    512  0    512   16     1008  X
6724be47400SFrançois Tigeot 		 * 1024 1024 1024 0     2048   1024  O
6734be47400SFrançois Tigeot 		 *
6744be47400SFrançois Tigeot 		 * We could probably detect this based on either the DRB
6754be47400SFrançois Tigeot 		 * matching, which was the case for the swizzling required in
6764be47400SFrançois Tigeot 		 * the table above, or from the 1-ch value being less than
6774be47400SFrançois Tigeot 		 * the minimum size of a rank.
6784be47400SFrançois Tigeot 		 *
6794be47400SFrançois Tigeot 		 * Reports indicate that the swizzling actually
6804be47400SFrançois Tigeot 		 * varies depending upon page placement inside the
6814be47400SFrançois Tigeot 		 * channels, i.e. we see swizzled pages where the
6824be47400SFrançois Tigeot 		 * banks of memory are paired and unswizzled on the
6834be47400SFrançois Tigeot 		 * uneven portion, so leave that as unknown.
6844be47400SFrançois Tigeot 		 */
6854be47400SFrançois Tigeot 		if (I915_READ16(C0DRB3) == I915_READ16(C1DRB3)) {
6864be47400SFrançois Tigeot 			swizzle_x = I915_BIT_6_SWIZZLE_9_10;
6874be47400SFrançois Tigeot 			swizzle_y = I915_BIT_6_SWIZZLE_9;
6884be47400SFrançois Tigeot 		}
6894be47400SFrançois Tigeot 	}
6904be47400SFrançois Tigeot 
6914be47400SFrançois Tigeot 	if (swizzle_x == I915_BIT_6_SWIZZLE_UNKNOWN ||
6924be47400SFrançois Tigeot 	    swizzle_y == I915_BIT_6_SWIZZLE_UNKNOWN) {
6934be47400SFrançois Tigeot 		/* Userspace likes to explode if it sees unknown swizzling,
6944be47400SFrançois Tigeot 		 * so lie. We will finish the lie when reporting through
6954be47400SFrançois Tigeot 		 * the get-tiling-ioctl by reporting the physical swizzle
6964be47400SFrançois Tigeot 		 * mode as unknown instead.
6974be47400SFrançois Tigeot 		 *
6984be47400SFrançois Tigeot 		 * As we don't strictly know what the swizzling is, it may be
6994be47400SFrançois Tigeot 		 * bit17 dependent, and so we need to also prevent the pages
7004be47400SFrançois Tigeot 		 * from being moved.
7014be47400SFrançois Tigeot 		 */
7024be47400SFrançois Tigeot 		dev_priv->quirks |= QUIRK_PIN_SWIZZLED_PAGES;
7034be47400SFrançois Tigeot 		swizzle_x = I915_BIT_6_SWIZZLE_NONE;
7044be47400SFrançois Tigeot 		swizzle_y = I915_BIT_6_SWIZZLE_NONE;
7054be47400SFrançois Tigeot 	}
7064be47400SFrançois Tigeot 
7074be47400SFrançois Tigeot 	dev_priv->mm.bit_6_swizzle_x = swizzle_x;
7084be47400SFrançois Tigeot 	dev_priv->mm.bit_6_swizzle_y = swizzle_y;
7094be47400SFrançois Tigeot }
7104be47400SFrançois Tigeot 
7114be47400SFrançois Tigeot /*
7124be47400SFrançois Tigeot  * Swap every 64 bytes of this page around, to account for it having a new
7134be47400SFrançois Tigeot  * bit 17 of its physical address and therefore being interpreted differently
7144be47400SFrançois Tigeot  * by the GPU.
7154be47400SFrançois Tigeot  */
7164be47400SFrançois Tigeot static void
i915_gem_swizzle_page(struct page * page)7174be47400SFrançois Tigeot i915_gem_swizzle_page(struct page *page)
7184be47400SFrançois Tigeot {
7194be47400SFrançois Tigeot 	char temp[64];
7204be47400SFrançois Tigeot 	char *vaddr;
7214be47400SFrançois Tigeot 	int i;
7224be47400SFrançois Tigeot 
7234be47400SFrançois Tigeot 	vaddr = kmap(page);
7244be47400SFrançois Tigeot 
7254be47400SFrançois Tigeot 	for (i = 0; i < PAGE_SIZE; i += 128) {
7264be47400SFrançois Tigeot 		memcpy(temp, &vaddr[i], 64);
7274be47400SFrançois Tigeot 		memcpy(&vaddr[i], &vaddr[i + 64], 64);
7284be47400SFrançois Tigeot 		memcpy(&vaddr[i + 64], temp, 64);
7294be47400SFrançois Tigeot 	}
7304be47400SFrançois Tigeot 
7314be47400SFrançois Tigeot 	kunmap(page);
7324be47400SFrançois Tigeot }
7334be47400SFrançois Tigeot 
7344be47400SFrançois Tigeot /**
7354be47400SFrançois Tigeot  * i915_gem_object_do_bit_17_swizzle - fixup bit 17 swizzling
7364be47400SFrançois Tigeot  * @obj: i915 GEM buffer object
7374be47400SFrançois Tigeot  * @pages: the scattergather list of physical pages
7384be47400SFrançois Tigeot  *
7394be47400SFrançois Tigeot  * This function fixes up the swizzling in case any page frame number for this
7404be47400SFrançois Tigeot  * object has changed in bit 17 since that state has been saved with
7414be47400SFrançois Tigeot  * i915_gem_object_save_bit_17_swizzle().
7424be47400SFrançois Tigeot  *
7434be47400SFrançois Tigeot  * This is called when pinning backing storage again, since the kernel is free
7444be47400SFrançois Tigeot  * to move unpinned backing storage around (either by directly moving pages or
7454be47400SFrançois Tigeot  * by swapping them out and back in again).
7464be47400SFrançois Tigeot  */
7474be47400SFrançois Tigeot void
i915_gem_object_do_bit_17_swizzle(struct drm_i915_gem_object * obj,struct sg_table * pages)7484be47400SFrançois Tigeot i915_gem_object_do_bit_17_swizzle(struct drm_i915_gem_object *obj,
7494be47400SFrançois Tigeot 				  struct sg_table *pages)
7504be47400SFrançois Tigeot {
7514be47400SFrançois Tigeot 	struct sgt_iter sgt_iter;
7524be47400SFrançois Tigeot 	struct page *page;
7534be47400SFrançois Tigeot 	int i;
7544be47400SFrançois Tigeot 
7554be47400SFrançois Tigeot 	if (obj->bit_17 == NULL)
7564be47400SFrançois Tigeot 		return;
7574be47400SFrançois Tigeot 
7584be47400SFrançois Tigeot 	i = 0;
7594be47400SFrançois Tigeot 	for_each_sgt_page(page, sgt_iter, pages) {
7604be47400SFrançois Tigeot 		char new_bit_17 = page_to_phys(page) >> 17;
7614be47400SFrançois Tigeot 		if ((new_bit_17 & 0x1) != (test_bit(i, obj->bit_17) != 0)) {
7624be47400SFrançois Tigeot 			i915_gem_swizzle_page(page);
7634be47400SFrançois Tigeot 			set_page_dirty(page);
7644be47400SFrançois Tigeot 		}
7654be47400SFrançois Tigeot 		i++;
7664be47400SFrançois Tigeot 	}
7674be47400SFrançois Tigeot }
7684be47400SFrançois Tigeot 
7694be47400SFrançois Tigeot /**
7704be47400SFrançois Tigeot  * i915_gem_object_save_bit_17_swizzle - save bit 17 swizzling
7714be47400SFrançois Tigeot  * @obj: i915 GEM buffer object
7724be47400SFrançois Tigeot  * @pages: the scattergather list of physical pages
7734be47400SFrançois Tigeot  *
7744be47400SFrançois Tigeot  * This function saves the bit 17 of each page frame number so that swizzling
7754be47400SFrançois Tigeot  * can be fixed up later on with i915_gem_object_do_bit_17_swizzle(). This must
7764be47400SFrançois Tigeot  * be called before the backing storage can be unpinned.
7774be47400SFrançois Tigeot  */
7784be47400SFrançois Tigeot void
i915_gem_object_save_bit_17_swizzle(struct drm_i915_gem_object * obj,struct sg_table * pages)7794be47400SFrançois Tigeot i915_gem_object_save_bit_17_swizzle(struct drm_i915_gem_object *obj,
7804be47400SFrançois Tigeot 				    struct sg_table *pages)
7814be47400SFrançois Tigeot {
7824be47400SFrançois Tigeot 	const unsigned int page_count = obj->base.size >> PAGE_SHIFT;
7834be47400SFrançois Tigeot 	struct sgt_iter sgt_iter;
7844be47400SFrançois Tigeot 	struct page *page;
7854be47400SFrançois Tigeot 	int i;
7864be47400SFrançois Tigeot 
7874be47400SFrançois Tigeot 	if (obj->bit_17 == NULL) {
7884be47400SFrançois Tigeot 		obj->bit_17 = kcalloc(BITS_TO_LONGS(page_count),
7894be47400SFrançois Tigeot 				      sizeof(long), GFP_KERNEL);
7904be47400SFrançois Tigeot 		if (obj->bit_17 == NULL) {
7914be47400SFrançois Tigeot 			DRM_ERROR("Failed to allocate memory for bit 17 "
7924be47400SFrançois Tigeot 				  "record\n");
7934be47400SFrançois Tigeot 			return;
7944be47400SFrançois Tigeot 		}
7954be47400SFrançois Tigeot 	}
7964be47400SFrançois Tigeot 
7974be47400SFrançois Tigeot 	i = 0;
7984be47400SFrançois Tigeot 
7994be47400SFrançois Tigeot 	for_each_sgt_page(page, sgt_iter, pages) {
8004be47400SFrançois Tigeot 		if (page_to_phys(page) & (1 << 17))
8014be47400SFrançois Tigeot 			__set_bit(i, obj->bit_17);
8024be47400SFrançois Tigeot 		else
8034be47400SFrançois Tigeot 			__clear_bit(i, obj->bit_17);
8044be47400SFrançois Tigeot 		i++;
8054be47400SFrançois Tigeot 	}
8064be47400SFrançois Tigeot }
807