blob: e15365be404566e70c254dd69d89968fc9f7b0b3 [file] [log] [blame]
Daniel Vetter41a36b72015-07-24 13:55:11 +02001/*
2 * Copyright © 2008-2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include <drm/drmP.h>
25#include <drm/i915_drm.h>
26#include "i915_drv.h"
27
Daniel Vettera794f622015-07-24 17:40:12 +020028/**
29 * DOC: fence register handling
30 *
31 * Important to avoid confusions: "fences" in the i915 driver are not execution
32 * fences used to track command completion but hardware detiler objects which
33 * wrap a given range of the global GTT. Each platform has only a fairly limited
34 * set of these objects.
35 *
36 * Fences are used to detile GTT memory mappings. They're also connected to the
Masanari Iida34fd3e12016-01-05 12:29:17 +090037 * hardware frontbuffer render tracking and hence interact with frontbuffer
38 * compression. Furthermore on older platforms fences are required for tiled
Daniel Vettera794f622015-07-24 17:40:12 +020039 * objects used by the display engine. They can also be used by the render
40 * engine - they're required for blitter commands and are optional for render
41 * commands. But on gen4+ both display (with the exception of fbc) and rendering
42 * have their own tiling state bits and don't need fences.
43 *
44 * Also note that fences only support X and Y tiling and hence can't be used for
45 * the fancier new tiling formats like W, Ys and Yf.
46 *
47 * Finally note that because fences are such a restricted resource they're
48 * dynamically associated with objects. Furthermore fence state is committed to
Masanari Iida34fd3e12016-01-05 12:29:17 +090049 * the hardware lazily to avoid unnecessary stalls on gen2/3. Therefore code must
50 * explicitly call i915_gem_object_get_fence() to synchronize fencing status
Daniel Vettera794f622015-07-24 17:40:12 +020051 * for cpu access. Also note that some code wants an unfenced view, for those
52 * cases the fence can be removed forcefully with i915_gem_object_put_fence().
53 *
54 * Internally these functions will synchronize with userspace access by removing
55 * CPU ptes into GTT mmaps (not the GTT ptes themselves) as needed.
56 */
57
Daniel Vetter41a36b72015-07-24 13:55:11 +020058static void i965_write_fence_reg(struct drm_device *dev, int reg,
59 struct drm_i915_gem_object *obj)
60{
Chris Wilsonfac5e232016-07-04 11:34:36 +010061 struct drm_i915_private *dev_priv = to_i915(dev);
Ville Syrjäläf0f59a02015-11-18 15:33:26 +020062 i915_reg_t fence_reg_lo, fence_reg_hi;
Daniel Vetter41a36b72015-07-24 13:55:11 +020063 int fence_pitch_shift;
64
65 if (INTEL_INFO(dev)->gen >= 6) {
Ville Syrjäläeecf6132015-09-21 18:05:14 +030066 fence_reg_lo = FENCE_REG_GEN6_LO(reg);
67 fence_reg_hi = FENCE_REG_GEN6_HI(reg);
68 fence_pitch_shift = GEN6_FENCE_PITCH_SHIFT;
Daniel Vetter41a36b72015-07-24 13:55:11 +020069 } else {
Ville Syrjäläeecf6132015-09-21 18:05:14 +030070 fence_reg_lo = FENCE_REG_965_LO(reg);
71 fence_reg_hi = FENCE_REG_965_HI(reg);
Daniel Vetter41a36b72015-07-24 13:55:11 +020072 fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
73 }
74
Daniel Vetter41a36b72015-07-24 13:55:11 +020075 /* To w/a incoherency with non-atomic 64-bit register updates,
76 * we split the 64-bit update into two 32-bit writes. In order
77 * for a partial fence not to be evaluated between writes, we
78 * precede the update with write to turn off the fence register,
79 * and only enable the fence as the last step.
80 *
81 * For extra levels of paranoia, we make sure each step lands
82 * before applying the next step.
83 */
Ville Syrjäläeecf6132015-09-21 18:05:14 +030084 I915_WRITE(fence_reg_lo, 0);
85 POSTING_READ(fence_reg_lo);
Daniel Vetter41a36b72015-07-24 13:55:11 +020086
87 if (obj) {
Chris Wilson058d88c2016-08-15 10:49:06 +010088 struct i915_vma *vma = i915_gem_object_to_ggtt(obj, NULL);
Chris Wilson3e510a82016-08-05 10:14:23 +010089 unsigned int tiling = i915_gem_object_get_tiling(obj);
90 unsigned int stride = i915_gem_object_get_stride(obj);
Chris Wilsona83718b2016-08-15 10:48:52 +010091 u32 size = vma->node.size;
92 u32 row_size = stride * (tiling == I915_TILING_Y ? 32 : 8);
93 u64 val;
Daniel Vetter41a36b72015-07-24 13:55:11 +020094
95 /* Adjust fence size to match tiled area */
Chris Wilsona83718b2016-08-15 10:48:52 +010096 size = rounddown(size, row_size);
Daniel Vetter41a36b72015-07-24 13:55:11 +020097
Chris Wilsona83718b2016-08-15 10:48:52 +010098 val = ((vma->node.start + size - 4096) & 0xfffff000) << 32;
99 val |= vma->node.start & 0xfffff000;
100 val |= (u64)((stride / 128) - 1) << fence_pitch_shift;
Chris Wilson3e510a82016-08-05 10:14:23 +0100101 if (tiling == I915_TILING_Y)
Daniel Vetter41a36b72015-07-24 13:55:11 +0200102 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
103 val |= I965_FENCE_REG_VALID;
104
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300105 I915_WRITE(fence_reg_hi, val >> 32);
106 POSTING_READ(fence_reg_hi);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200107
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300108 I915_WRITE(fence_reg_lo, val);
109 POSTING_READ(fence_reg_lo);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200110 } else {
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300111 I915_WRITE(fence_reg_hi, 0);
112 POSTING_READ(fence_reg_hi);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200113 }
114}
115
116static void i915_write_fence_reg(struct drm_device *dev, int reg,
117 struct drm_i915_gem_object *obj)
118{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100119 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200120 u32 val;
121
122 if (obj) {
Chris Wilson058d88c2016-08-15 10:49:06 +0100123 struct i915_vma *vma = i915_gem_object_to_ggtt(obj, NULL);
Chris Wilson3e510a82016-08-05 10:14:23 +0100124 unsigned int tiling = i915_gem_object_get_tiling(obj);
125 unsigned int stride = i915_gem_object_get_stride(obj);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200126 int pitch_val;
127 int tile_width;
128
Chris Wilsona83718b2016-08-15 10:48:52 +0100129 WARN((vma->node.start & ~I915_FENCE_START_MASK) ||
130 !is_power_of_2(vma->node.size) ||
131 (vma->node.start & (vma->node.size - 1)),
132 "object 0x%08llx [fenceable? %d] not 1M or pot-size (0x%08llx) aligned\n",
Chris Wilson05a20d02016-08-18 17:16:55 +0100133 vma->node.start,
134 i915_vma_is_map_and_fenceable(vma),
135 vma->node.size);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200136
Chris Wilson3e510a82016-08-05 10:14:23 +0100137 if (tiling == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Daniel Vetter41a36b72015-07-24 13:55:11 +0200138 tile_width = 128;
139 else
140 tile_width = 512;
141
142 /* Note: pitch better be a power of two tile widths */
Chris Wilson3e510a82016-08-05 10:14:23 +0100143 pitch_val = stride / tile_width;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200144 pitch_val = ffs(pitch_val) - 1;
145
Chris Wilsona83718b2016-08-15 10:48:52 +0100146 val = vma->node.start;
Chris Wilson3e510a82016-08-05 10:14:23 +0100147 if (tiling == I915_TILING_Y)
Daniel Vetter41a36b72015-07-24 13:55:11 +0200148 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona83718b2016-08-15 10:48:52 +0100149 val |= I915_FENCE_SIZE_BITS(vma->node.size);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200150 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
151 val |= I830_FENCE_REG_VALID;
152 } else
153 val = 0;
154
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300155 I915_WRITE(FENCE_REG(reg), val);
156 POSTING_READ(FENCE_REG(reg));
Daniel Vetter41a36b72015-07-24 13:55:11 +0200157}
158
159static void i830_write_fence_reg(struct drm_device *dev, int reg,
160 struct drm_i915_gem_object *obj)
161{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100162 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsona83718b2016-08-15 10:48:52 +0100163 u32 val;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200164
165 if (obj) {
Chris Wilson058d88c2016-08-15 10:49:06 +0100166 struct i915_vma *vma = i915_gem_object_to_ggtt(obj, NULL);
Chris Wilson3e510a82016-08-05 10:14:23 +0100167 unsigned int tiling = i915_gem_object_get_tiling(obj);
168 unsigned int stride = i915_gem_object_get_stride(obj);
Chris Wilsona83718b2016-08-15 10:48:52 +0100169 u32 pitch_val;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200170
Chris Wilsona83718b2016-08-15 10:48:52 +0100171 WARN((vma->node.start & ~I830_FENCE_START_MASK) ||
172 !is_power_of_2(vma->node.size) ||
173 (vma->node.start & (vma->node.size - 1)),
174 "object 0x%08llx not 512K or pot-size 0x%08llx aligned\n",
175 vma->node.start, vma->node.size);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200176
Chris Wilson3e510a82016-08-05 10:14:23 +0100177 pitch_val = stride / 128;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200178 pitch_val = ffs(pitch_val) - 1;
179
Chris Wilsona83718b2016-08-15 10:48:52 +0100180 val = vma->node.start;
Chris Wilson3e510a82016-08-05 10:14:23 +0100181 if (tiling == I915_TILING_Y)
Daniel Vetter41a36b72015-07-24 13:55:11 +0200182 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona83718b2016-08-15 10:48:52 +0100183 val |= I830_FENCE_SIZE_BITS(vma->node.size);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200184 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
185 val |= I830_FENCE_REG_VALID;
186 } else
187 val = 0;
188
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300189 I915_WRITE(FENCE_REG(reg), val);
190 POSTING_READ(FENCE_REG(reg));
Daniel Vetter41a36b72015-07-24 13:55:11 +0200191}
192
193inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
194{
195 return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
196}
197
198static void i915_gem_write_fence(struct drm_device *dev, int reg,
199 struct drm_i915_gem_object *obj)
200{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100201 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200202
203 /* Ensure that all CPU reads are completed before installing a fence
204 * and all writes before removing the fence.
205 */
206 if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
207 mb();
208
Chris Wilson3e510a82016-08-05 10:14:23 +0100209 WARN(obj &&
210 (!i915_gem_object_get_stride(obj) ||
211 !i915_gem_object_get_tiling(obj)),
Daniel Vetter41a36b72015-07-24 13:55:11 +0200212 "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
Chris Wilson3e510a82016-08-05 10:14:23 +0100213 i915_gem_object_get_stride(obj),
214 i915_gem_object_get_tiling(obj));
Daniel Vetter41a36b72015-07-24 13:55:11 +0200215
216 if (IS_GEN2(dev))
217 i830_write_fence_reg(dev, reg, obj);
218 else if (IS_GEN3(dev))
219 i915_write_fence_reg(dev, reg, obj);
220 else if (INTEL_INFO(dev)->gen >= 4)
221 i965_write_fence_reg(dev, reg, obj);
222
223 /* And similarly be paranoid that no direct access to this region
224 * is reordered to before the fence is installed.
225 */
226 if (i915_gem_object_needs_mb(obj))
227 mb();
228}
229
230static inline int fence_number(struct drm_i915_private *dev_priv,
231 struct drm_i915_fence_reg *fence)
232{
233 return fence - dev_priv->fence_regs;
234}
235
236static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
237 struct drm_i915_fence_reg *fence,
238 bool enable)
239{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100240 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200241 int reg = fence_number(dev_priv, fence);
242
243 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
244
245 if (enable) {
246 obj->fence_reg = reg;
247 fence->obj = obj;
248 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
249 } else {
250 obj->fence_reg = I915_FENCE_REG_NONE;
251 fence->obj = NULL;
252 list_del_init(&fence->lru_list);
253 }
254 obj->fence_dirty = false;
255}
256
257static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
258{
Chris Wilson3e510a82016-08-05 10:14:23 +0100259 if (i915_gem_object_is_tiled(obj))
Daniel Vetter41a36b72015-07-24 13:55:11 +0200260 i915_gem_release_mmap(obj);
261
262 /* As we do not have an associated fence register, we will force
263 * a tiling change if we ever need to acquire one.
264 */
265 obj->fence_dirty = false;
266 obj->fence_reg = I915_FENCE_REG_NONE;
267}
268
269static int
270i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
271{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100272 return i915_gem_active_retire(&obj->last_fence,
273 &obj->base.dev->struct_mutex);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200274}
275
Daniel Vettera794f622015-07-24 17:40:12 +0200276/**
277 * i915_gem_object_put_fence - force-remove fence for an object
278 * @obj: object to map through a fence reg
279 *
280 * This function force-removes any fence from the given object, which is useful
281 * if the kernel wants to do untiled GTT access.
282 *
283 * Returns:
284 *
285 * 0 on success, negative error code on failure.
286 */
Daniel Vetter41a36b72015-07-24 13:55:11 +0200287int
288i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
289{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100290 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200291 struct drm_i915_fence_reg *fence;
292 int ret;
293
294 ret = i915_gem_object_wait_fence(obj);
295 if (ret)
296 return ret;
297
298 if (obj->fence_reg == I915_FENCE_REG_NONE)
299 return 0;
300
301 fence = &dev_priv->fence_regs[obj->fence_reg];
302
Chris Wilson18034582016-08-18 17:16:45 +0100303 if (fence->pin_count)
Daniel Vetter41a36b72015-07-24 13:55:11 +0200304 return -EBUSY;
305
306 i915_gem_object_fence_lost(obj);
307 i915_gem_object_update_fence(obj, fence, false);
308
309 return 0;
310}
311
312static struct drm_i915_fence_reg *
313i915_find_fence_reg(struct drm_device *dev)
314{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100315 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200316 struct drm_i915_fence_reg *reg, *avail;
317 int i;
318
319 /* First try to find a free reg */
320 avail = NULL;
Daniel Vetterc668cde2015-09-30 10:46:59 +0200321 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Daniel Vetter41a36b72015-07-24 13:55:11 +0200322 reg = &dev_priv->fence_regs[i];
323 if (!reg->obj)
324 return reg;
325
326 if (!reg->pin_count)
327 avail = reg;
328 }
329
330 if (avail == NULL)
331 goto deadlock;
332
333 /* None available, try to steal one or wait for a user to finish */
334 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
335 if (reg->pin_count)
336 continue;
337
338 return reg;
339 }
340
341deadlock:
342 /* Wait for completion of pending flips which consume fences */
343 if (intel_has_pending_fb_unpin(dev))
344 return ERR_PTR(-EAGAIN);
345
346 return ERR_PTR(-EDEADLK);
347}
348
349/**
350 * i915_gem_object_get_fence - set up fencing for an object
351 * @obj: object to map through a fence reg
352 *
353 * When mapping objects through the GTT, userspace wants to be able to write
354 * to them without having to worry about swizzling if the object is tiled.
355 * This function walks the fence regs looking for a free one for @obj,
356 * stealing one if it can't find any.
357 *
358 * It then sets up the reg based on the object's properties: address, pitch
359 * and tiling format.
360 *
361 * For an untiled surface, this removes any existing fence.
Daniel Vettera794f622015-07-24 17:40:12 +0200362 *
363 * Returns:
364 *
365 * 0 on success, negative error code on failure.
Daniel Vetter41a36b72015-07-24 13:55:11 +0200366 */
367int
368i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
369{
370 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100371 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3e510a82016-08-05 10:14:23 +0100372 bool enable = i915_gem_object_is_tiled(obj);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200373 struct drm_i915_fence_reg *reg;
374 int ret;
375
376 /* Have we updated the tiling parameters upon the object and so
377 * will need to serialise the write to the associated fence register?
378 */
379 if (obj->fence_dirty) {
380 ret = i915_gem_object_wait_fence(obj);
381 if (ret)
382 return ret;
383 }
384
385 /* Just update our place in the LRU if our fence is getting reused. */
386 if (obj->fence_reg != I915_FENCE_REG_NONE) {
387 reg = &dev_priv->fence_regs[obj->fence_reg];
388 if (!obj->fence_dirty) {
389 list_move_tail(&reg->lru_list,
390 &dev_priv->mm.fence_list);
391 return 0;
392 }
393 } else if (enable) {
Daniel Vetter41a36b72015-07-24 13:55:11 +0200394 reg = i915_find_fence_reg(dev);
395 if (IS_ERR(reg))
396 return PTR_ERR(reg);
397
398 if (reg->obj) {
399 struct drm_i915_gem_object *old = reg->obj;
400
401 ret = i915_gem_object_wait_fence(old);
402 if (ret)
403 return ret;
404
405 i915_gem_object_fence_lost(old);
406 }
407 } else
408 return 0;
409
410 i915_gem_object_update_fence(obj, reg, enable);
411
412 return 0;
413}
414
Daniel Vettera794f622015-07-24 17:40:12 +0200415/**
416 * i915_gem_object_pin_fence - pin fencing state
417 * @obj: object to pin fencing for
418 *
419 * This pins the fencing state (whether tiled or untiled) to make sure the
420 * object is ready to be used as a scanout target. Fencing status must be
421 * synchronize first by calling i915_gem_object_get_fence():
422 *
423 * The resulting fence pin reference must be released again with
424 * i915_gem_object_unpin_fence().
425 *
426 * Returns:
427 *
428 * True if the object has a fence, false otherwise.
429 */
Daniel Vetter41a36b72015-07-24 13:55:11 +0200430bool
431i915_gem_object_pin_fence(struct drm_i915_gem_object *obj)
432{
433 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Chris Wilson058d88c2016-08-15 10:49:06 +0100434 to_i915(obj->base.dev)->fence_regs[obj->fence_reg].pin_count++;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200435 return true;
436 } else
437 return false;
438}
439
Daniel Vettera794f622015-07-24 17:40:12 +0200440/**
441 * i915_gem_object_unpin_fence - unpin fencing state
442 * @obj: object to unpin fencing for
443 *
444 * This releases the fence pin reference acquired through
445 * i915_gem_object_pin_fence. It will handle both objects with and without an
446 * attached fence correctly, callers do not need to distinguish this.
447 */
Daniel Vetter41a36b72015-07-24 13:55:11 +0200448void
449i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
450{
451 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Chris Wilsonfac5e232016-07-04 11:34:36 +0100452 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200453 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
454 dev_priv->fence_regs[obj->fence_reg].pin_count--;
455 }
456}
457
Daniel Vettera794f622015-07-24 17:40:12 +0200458/**
459 * i915_gem_restore_fences - restore fence state
460 * @dev: DRM device
461 *
462 * Restore the hw fence state to match the software tracking again, to be called
463 * after a gpu reset and on resume.
464 */
Daniel Vetter41a36b72015-07-24 13:55:11 +0200465void i915_gem_restore_fences(struct drm_device *dev)
466{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100467 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200468 int i;
469
470 for (i = 0; i < dev_priv->num_fence_regs; i++) {
471 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
472
473 /*
474 * Commit delayed tiling changes if we have an object still
475 * attached to the fence, otherwise just clear the fence.
476 */
477 if (reg->obj) {
478 i915_gem_object_update_fence(reg->obj, reg,
Chris Wilson3e510a82016-08-05 10:14:23 +0100479 i915_gem_object_get_tiling(reg->obj));
Daniel Vetter41a36b72015-07-24 13:55:11 +0200480 } else {
481 i915_gem_write_fence(dev, i, NULL);
482 }
483 }
484}
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200485
486/**
Daniel Vetter3271dca2015-07-24 17:40:15 +0200487 * DOC: tiling swizzling details
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200488 *
489 * The idea behind tiling is to increase cache hit rates by rearranging
490 * pixel data so that a group of pixel accesses are in the same cacheline.
491 * Performance improvement from doing this on the back/depth buffer are on
492 * the order of 30%.
493 *
494 * Intel architectures make this somewhat more complicated, though, by
495 * adjustments made to addressing of data when the memory is in interleaved
496 * mode (matched pairs of DIMMS) to improve memory bandwidth.
497 * For interleaved memory, the CPU sends every sequential 64 bytes
498 * to an alternate memory channel so it can get the bandwidth from both.
499 *
500 * The GPU also rearranges its accesses for increased bandwidth to interleaved
501 * memory, and it matches what the CPU does for non-tiled. However, when tiled
502 * it does it a little differently, since one walks addresses not just in the
503 * X direction but also Y. So, along with alternating channels when bit
504 * 6 of the address flips, it also alternates when other bits flip -- Bits 9
505 * (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)
506 * are common to both the 915 and 965-class hardware.
507 *
508 * The CPU also sometimes XORs in higher bits as well, to improve
509 * bandwidth doing strided access like we do so frequently in graphics. This
510 * is called "Channel XOR Randomization" in the MCH documentation. The result
511 * is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address
512 * decode.
513 *
514 * All of this bit 6 XORing has an effect on our memory management,
515 * as we need to make sure that the 3d driver can correctly address object
516 * contents.
517 *
518 * If we don't have interleaved memory, all tiling is safe and no swizzling is
519 * required.
520 *
521 * When bit 17 is XORed in, we simply refuse to tile at all. Bit
Masanari Iida34fd3e12016-01-05 12:29:17 +0900522 * 17 is not just a page offset, so as we page an object out and back in,
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200523 * individual pages in it will have different bit 17 addresses, resulting in
524 * each 64 bytes being swapped with its neighbor!
525 *
526 * Otherwise, if interleaved, we have to tell the 3d driver what the address
527 * swizzling it needs to do is, since it's writing with the CPU to the pages
528 * (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the
529 * pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling
530 * required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order
531 * to match what the GPU expects.
532 */
533
534/**
Daniel Vetter3271dca2015-07-24 17:40:15 +0200535 * i915_gem_detect_bit_6_swizzle - detect bit 6 swizzling pattern
536 * @dev: DRM device
537 *
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200538 * Detects bit 6 swizzling of address lookup between IGD access and CPU
539 * access through main memory.
540 */
541void
542i915_gem_detect_bit_6_swizzle(struct drm_device *dev)
543{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100544 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200545 uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
546 uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
547
548 if (INTEL_INFO(dev)->gen >= 8 || IS_VALLEYVIEW(dev)) {
549 /*
550 * On BDW+, swizzling is not used. We leave the CPU memory
551 * controller in charge of optimizing memory accesses without
552 * the extra address manipulation GPU side.
553 *
554 * VLV and CHV don't have GPU swizzling.
555 */
556 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
557 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
558 } else if (INTEL_INFO(dev)->gen >= 6) {
559 if (dev_priv->preserve_bios_swizzle) {
560 if (I915_READ(DISP_ARB_CTL) &
561 DISP_TILE_SURFACE_SWIZZLING) {
562 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
563 swizzle_y = I915_BIT_6_SWIZZLE_9;
564 } else {
565 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
566 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
567 }
568 } else {
569 uint32_t dimm_c0, dimm_c1;
570 dimm_c0 = I915_READ(MAD_DIMM_C0);
571 dimm_c1 = I915_READ(MAD_DIMM_C1);
572 dimm_c0 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
573 dimm_c1 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
574 /* Enable swizzling when the channels are populated
575 * with identically sized dimms. We don't need to check
576 * the 3rd channel because no cpu with gpu attached
577 * ships in that configuration. Also, swizzling only
578 * makes sense for 2 channels anyway. */
579 if (dimm_c0 == dimm_c1) {
580 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
581 swizzle_y = I915_BIT_6_SWIZZLE_9;
582 } else {
583 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
584 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
585 }
586 }
587 } else if (IS_GEN5(dev)) {
588 /* On Ironlake whatever DRAM config, GPU always do
589 * same swizzling setup.
590 */
591 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
592 swizzle_y = I915_BIT_6_SWIZZLE_9;
593 } else if (IS_GEN2(dev)) {
594 /* As far as we know, the 865 doesn't have these bit 6
595 * swizzling issues.
596 */
597 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
598 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
599 } else if (IS_MOBILE(dev) || (IS_GEN3(dev) && !IS_G33(dev))) {
600 uint32_t dcc;
601
602 /* On 9xx chipsets, channel interleave by the CPU is
603 * determined by DCC. For single-channel, neither the CPU
604 * nor the GPU do swizzling. For dual channel interleaved,
605 * the GPU's interleave is bit 9 and 10 for X tiled, and bit
606 * 9 for Y tiled. The CPU's interleave is independent, and
607 * can be based on either bit 11 (haven't seen this yet) or
608 * bit 17 (common).
609 */
610 dcc = I915_READ(DCC);
611 switch (dcc & DCC_ADDRESSING_MODE_MASK) {
612 case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
613 case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
614 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
615 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
616 break;
617 case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
618 if (dcc & DCC_CHANNEL_XOR_DISABLE) {
619 /* This is the base swizzling by the GPU for
620 * tiled buffers.
621 */
622 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
623 swizzle_y = I915_BIT_6_SWIZZLE_9;
624 } else if ((dcc & DCC_CHANNEL_XOR_BIT_17) == 0) {
625 /* Bit 11 swizzling by the CPU in addition. */
626 swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
627 swizzle_y = I915_BIT_6_SWIZZLE_9_11;
628 } else {
629 /* Bit 17 swizzling by the CPU in addition. */
630 swizzle_x = I915_BIT_6_SWIZZLE_9_10_17;
631 swizzle_y = I915_BIT_6_SWIZZLE_9_17;
632 }
633 break;
634 }
635
636 /* check for L-shaped memory aka modified enhanced addressing */
Chris Wilson0b466dc22015-11-19 09:58:05 +0000637 if (IS_GEN4(dev) &&
638 !(I915_READ(DCC2) & DCC2_MODIFIED_ENHANCED_DISABLE)) {
639 swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
640 swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200641 }
642
643 if (dcc == 0xffffffff) {
644 DRM_ERROR("Couldn't read from MCHBAR. "
645 "Disabling tiling.\n");
646 swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
647 swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
648 }
649 } else {
650 /* The 965, G33, and newer, have a very flexible memory
651 * configuration. It will enable dual-channel mode
652 * (interleaving) on as much memory as it can, and the GPU
653 * will additionally sometimes enable different bit 6
654 * swizzling for tiled objects from the CPU.
655 *
656 * Here's what I found on the G965:
657 * slot fill memory size swizzling
658 * 0A 0B 1A 1B 1-ch 2-ch
659 * 512 0 0 0 512 0 O
660 * 512 0 512 0 16 1008 X
661 * 512 0 0 512 16 1008 X
662 * 0 512 0 512 16 1008 X
663 * 1024 1024 1024 0 2048 1024 O
664 *
665 * We could probably detect this based on either the DRB
666 * matching, which was the case for the swizzling required in
667 * the table above, or from the 1-ch value being less than
668 * the minimum size of a rank.
Chris Wilson0b466dc22015-11-19 09:58:05 +0000669 *
670 * Reports indicate that the swizzling actually
671 * varies depending upon page placement inside the
672 * channels, i.e. we see swizzled pages where the
673 * banks of memory are paired and unswizzled on the
674 * uneven portion, so leave that as unknown.
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200675 */
Chris Wilson0b466dc22015-11-19 09:58:05 +0000676 if (I915_READ16(C0DRB3) == I915_READ16(C1DRB3)) {
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200677 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
678 swizzle_y = I915_BIT_6_SWIZZLE_9;
679 }
680 }
681
Chris Wilson0b466dc22015-11-19 09:58:05 +0000682 if (swizzle_x == I915_BIT_6_SWIZZLE_UNKNOWN ||
683 swizzle_y == I915_BIT_6_SWIZZLE_UNKNOWN) {
684 /* Userspace likes to explode if it sees unknown swizzling,
685 * so lie. We will finish the lie when reporting through
686 * the get-tiling-ioctl by reporting the physical swizzle
687 * mode as unknown instead.
688 *
689 * As we don't strictly know what the swizzling is, it may be
690 * bit17 dependent, and so we need to also prevent the pages
691 * from being moved.
692 */
693 dev_priv->quirks |= QUIRK_PIN_SWIZZLED_PAGES;
694 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
695 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
696 }
697
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200698 dev_priv->mm.bit_6_swizzle_x = swizzle_x;
699 dev_priv->mm.bit_6_swizzle_y = swizzle_y;
700}
701
Daniel Vetter3271dca2015-07-24 17:40:15 +0200702/*
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200703 * Swap every 64 bytes of this page around, to account for it having a new
704 * bit 17 of its physical address and therefore being interpreted differently
705 * by the GPU.
706 */
707static void
708i915_gem_swizzle_page(struct page *page)
709{
710 char temp[64];
711 char *vaddr;
712 int i;
713
714 vaddr = kmap(page);
715
716 for (i = 0; i < PAGE_SIZE; i += 128) {
717 memcpy(temp, &vaddr[i], 64);
718 memcpy(&vaddr[i], &vaddr[i + 64], 64);
719 memcpy(&vaddr[i + 64], temp, 64);
720 }
721
722 kunmap(page);
723}
724
Daniel Vetter3271dca2015-07-24 17:40:15 +0200725/**
726 * i915_gem_object_do_bit_17_swizzle - fixup bit 17 swizzling
727 * @obj: i915 GEM buffer object
728 *
729 * This function fixes up the swizzling in case any page frame number for this
730 * object has changed in bit 17 since that state has been saved with
731 * i915_gem_object_save_bit_17_swizzle().
732 *
733 * This is called when pinning backing storage again, since the kernel is free
734 * to move unpinned backing storage around (either by directly moving pages or
735 * by swapping them out and back in again).
736 */
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200737void
738i915_gem_object_do_bit_17_swizzle(struct drm_i915_gem_object *obj)
739{
Dave Gordon85d12252016-05-20 11:54:06 +0100740 struct sgt_iter sgt_iter;
741 struct page *page;
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200742 int i;
743
744 if (obj->bit_17 == NULL)
745 return;
746
747 i = 0;
Dave Gordon85d12252016-05-20 11:54:06 +0100748 for_each_sgt_page(page, sgt_iter, obj->pages) {
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200749 char new_bit_17 = page_to_phys(page) >> 17;
750 if ((new_bit_17 & 0x1) !=
751 (test_bit(i, obj->bit_17) != 0)) {
752 i915_gem_swizzle_page(page);
753 set_page_dirty(page);
754 }
755 i++;
756 }
757}
758
Daniel Vetter3271dca2015-07-24 17:40:15 +0200759/**
760 * i915_gem_object_save_bit_17_swizzle - save bit 17 swizzling
761 * @obj: i915 GEM buffer object
762 *
763 * This function saves the bit 17 of each page frame number so that swizzling
764 * can be fixed up later on with i915_gem_object_do_bit_17_swizzle(). This must
765 * be called before the backing storage can be unpinned.
766 */
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200767void
768i915_gem_object_save_bit_17_swizzle(struct drm_i915_gem_object *obj)
769{
Dave Gordon85d12252016-05-20 11:54:06 +0100770 struct sgt_iter sgt_iter;
771 struct page *page;
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200772 int page_count = obj->base.size >> PAGE_SHIFT;
773 int i;
774
775 if (obj->bit_17 == NULL) {
776 obj->bit_17 = kcalloc(BITS_TO_LONGS(page_count),
777 sizeof(long), GFP_KERNEL);
778 if (obj->bit_17 == NULL) {
779 DRM_ERROR("Failed to allocate memory for bit 17 "
780 "record\n");
781 return;
782 }
783 }
784
785 i = 0;
Dave Gordon85d12252016-05-20 11:54:06 +0100786
787 for_each_sgt_page(page, sgt_iter, obj->pages) {
788 if (page_to_phys(page) & (1 << 17))
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200789 __set_bit(i, obj->bit_17);
790 else
791 __clear_bit(i, obj->bit_17);
792 i++;
793 }
794}