blob: b0c6c2777725cae82d2cde3637d16c8345477ae0 [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",
133 vma->node.start, obj->map_and_fenceable, vma->node.size);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200134
Chris Wilson3e510a82016-08-05 10:14:23 +0100135 if (tiling == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
Daniel Vetter41a36b72015-07-24 13:55:11 +0200136 tile_width = 128;
137 else
138 tile_width = 512;
139
140 /* Note: pitch better be a power of two tile widths */
Chris Wilson3e510a82016-08-05 10:14:23 +0100141 pitch_val = stride / tile_width;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200142 pitch_val = ffs(pitch_val) - 1;
143
Chris Wilsona83718b2016-08-15 10:48:52 +0100144 val = vma->node.start;
Chris Wilson3e510a82016-08-05 10:14:23 +0100145 if (tiling == I915_TILING_Y)
Daniel Vetter41a36b72015-07-24 13:55:11 +0200146 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona83718b2016-08-15 10:48:52 +0100147 val |= I915_FENCE_SIZE_BITS(vma->node.size);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200148 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
149 val |= I830_FENCE_REG_VALID;
150 } else
151 val = 0;
152
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300153 I915_WRITE(FENCE_REG(reg), val);
154 POSTING_READ(FENCE_REG(reg));
Daniel Vetter41a36b72015-07-24 13:55:11 +0200155}
156
157static void i830_write_fence_reg(struct drm_device *dev, int reg,
158 struct drm_i915_gem_object *obj)
159{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100160 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilsona83718b2016-08-15 10:48:52 +0100161 u32 val;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200162
163 if (obj) {
Chris Wilson058d88c2016-08-15 10:49:06 +0100164 struct i915_vma *vma = i915_gem_object_to_ggtt(obj, NULL);
Chris Wilson3e510a82016-08-05 10:14:23 +0100165 unsigned int tiling = i915_gem_object_get_tiling(obj);
166 unsigned int stride = i915_gem_object_get_stride(obj);
Chris Wilsona83718b2016-08-15 10:48:52 +0100167 u32 pitch_val;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200168
Chris Wilsona83718b2016-08-15 10:48:52 +0100169 WARN((vma->node.start & ~I830_FENCE_START_MASK) ||
170 !is_power_of_2(vma->node.size) ||
171 (vma->node.start & (vma->node.size - 1)),
172 "object 0x%08llx not 512K or pot-size 0x%08llx aligned\n",
173 vma->node.start, vma->node.size);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200174
Chris Wilson3e510a82016-08-05 10:14:23 +0100175 pitch_val = stride / 128;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200176 pitch_val = ffs(pitch_val) - 1;
177
Chris Wilsona83718b2016-08-15 10:48:52 +0100178 val = vma->node.start;
Chris Wilson3e510a82016-08-05 10:14:23 +0100179 if (tiling == I915_TILING_Y)
Daniel Vetter41a36b72015-07-24 13:55:11 +0200180 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
Chris Wilsona83718b2016-08-15 10:48:52 +0100181 val |= I830_FENCE_SIZE_BITS(vma->node.size);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200182 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
183 val |= I830_FENCE_REG_VALID;
184 } else
185 val = 0;
186
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300187 I915_WRITE(FENCE_REG(reg), val);
188 POSTING_READ(FENCE_REG(reg));
Daniel Vetter41a36b72015-07-24 13:55:11 +0200189}
190
191inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
192{
193 return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
194}
195
196static void i915_gem_write_fence(struct drm_device *dev, int reg,
197 struct drm_i915_gem_object *obj)
198{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100199 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200200
201 /* Ensure that all CPU reads are completed before installing a fence
202 * and all writes before removing the fence.
203 */
204 if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
205 mb();
206
Chris Wilson3e510a82016-08-05 10:14:23 +0100207 WARN(obj &&
208 (!i915_gem_object_get_stride(obj) ||
209 !i915_gem_object_get_tiling(obj)),
Daniel Vetter41a36b72015-07-24 13:55:11 +0200210 "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
Chris Wilson3e510a82016-08-05 10:14:23 +0100211 i915_gem_object_get_stride(obj),
212 i915_gem_object_get_tiling(obj));
Daniel Vetter41a36b72015-07-24 13:55:11 +0200213
214 if (IS_GEN2(dev))
215 i830_write_fence_reg(dev, reg, obj);
216 else if (IS_GEN3(dev))
217 i915_write_fence_reg(dev, reg, obj);
218 else if (INTEL_INFO(dev)->gen >= 4)
219 i965_write_fence_reg(dev, reg, obj);
220
221 /* And similarly be paranoid that no direct access to this region
222 * is reordered to before the fence is installed.
223 */
224 if (i915_gem_object_needs_mb(obj))
225 mb();
226}
227
228static inline int fence_number(struct drm_i915_private *dev_priv,
229 struct drm_i915_fence_reg *fence)
230{
231 return fence - dev_priv->fence_regs;
232}
233
234static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
235 struct drm_i915_fence_reg *fence,
236 bool enable)
237{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100238 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200239 int reg = fence_number(dev_priv, fence);
240
241 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
242
243 if (enable) {
244 obj->fence_reg = reg;
245 fence->obj = obj;
246 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
247 } else {
248 obj->fence_reg = I915_FENCE_REG_NONE;
249 fence->obj = NULL;
250 list_del_init(&fence->lru_list);
251 }
252 obj->fence_dirty = false;
253}
254
255static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
256{
Chris Wilson3e510a82016-08-05 10:14:23 +0100257 if (i915_gem_object_is_tiled(obj))
Daniel Vetter41a36b72015-07-24 13:55:11 +0200258 i915_gem_release_mmap(obj);
259
260 /* As we do not have an associated fence register, we will force
261 * a tiling change if we ever need to acquire one.
262 */
263 obj->fence_dirty = false;
264 obj->fence_reg = I915_FENCE_REG_NONE;
265}
266
267static int
268i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
269{
Chris Wilsonfa545cb2016-08-04 07:52:35 +0100270 return i915_gem_active_retire(&obj->last_fence,
271 &obj->base.dev->struct_mutex);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200272}
273
Daniel Vettera794f622015-07-24 17:40:12 +0200274/**
275 * i915_gem_object_put_fence - force-remove fence for an object
276 * @obj: object to map through a fence reg
277 *
278 * This function force-removes any fence from the given object, which is useful
279 * if the kernel wants to do untiled GTT access.
280 *
281 * Returns:
282 *
283 * 0 on success, negative error code on failure.
284 */
Daniel Vetter41a36b72015-07-24 13:55:11 +0200285int
286i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
287{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100288 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200289 struct drm_i915_fence_reg *fence;
290 int ret;
291
292 ret = i915_gem_object_wait_fence(obj);
293 if (ret)
294 return ret;
295
296 if (obj->fence_reg == I915_FENCE_REG_NONE)
297 return 0;
298
299 fence = &dev_priv->fence_regs[obj->fence_reg];
300
Chris Wilson18034582016-08-18 17:16:45 +0100301 if (fence->pin_count)
Daniel Vetter41a36b72015-07-24 13:55:11 +0200302 return -EBUSY;
303
304 i915_gem_object_fence_lost(obj);
305 i915_gem_object_update_fence(obj, fence, false);
306
307 return 0;
308}
309
310static struct drm_i915_fence_reg *
311i915_find_fence_reg(struct drm_device *dev)
312{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100313 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200314 struct drm_i915_fence_reg *reg, *avail;
315 int i;
316
317 /* First try to find a free reg */
318 avail = NULL;
Daniel Vetterc668cde2015-09-30 10:46:59 +0200319 for (i = 0; i < dev_priv->num_fence_regs; i++) {
Daniel Vetter41a36b72015-07-24 13:55:11 +0200320 reg = &dev_priv->fence_regs[i];
321 if (!reg->obj)
322 return reg;
323
324 if (!reg->pin_count)
325 avail = reg;
326 }
327
328 if (avail == NULL)
329 goto deadlock;
330
331 /* None available, try to steal one or wait for a user to finish */
332 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
333 if (reg->pin_count)
334 continue;
335
336 return reg;
337 }
338
339deadlock:
340 /* Wait for completion of pending flips which consume fences */
341 if (intel_has_pending_fb_unpin(dev))
342 return ERR_PTR(-EAGAIN);
343
344 return ERR_PTR(-EDEADLK);
345}
346
347/**
348 * i915_gem_object_get_fence - set up fencing for an object
349 * @obj: object to map through a fence reg
350 *
351 * When mapping objects through the GTT, userspace wants to be able to write
352 * to them without having to worry about swizzling if the object is tiled.
353 * This function walks the fence regs looking for a free one for @obj,
354 * stealing one if it can't find any.
355 *
356 * It then sets up the reg based on the object's properties: address, pitch
357 * and tiling format.
358 *
359 * For an untiled surface, this removes any existing fence.
Daniel Vettera794f622015-07-24 17:40:12 +0200360 *
361 * Returns:
362 *
363 * 0 on success, negative error code on failure.
Daniel Vetter41a36b72015-07-24 13:55:11 +0200364 */
365int
366i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
367{
368 struct drm_device *dev = obj->base.dev;
Chris Wilsonfac5e232016-07-04 11:34:36 +0100369 struct drm_i915_private *dev_priv = to_i915(dev);
Chris Wilson3e510a82016-08-05 10:14:23 +0100370 bool enable = i915_gem_object_is_tiled(obj);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200371 struct drm_i915_fence_reg *reg;
372 int ret;
373
374 /* Have we updated the tiling parameters upon the object and so
375 * will need to serialise the write to the associated fence register?
376 */
377 if (obj->fence_dirty) {
378 ret = i915_gem_object_wait_fence(obj);
379 if (ret)
380 return ret;
381 }
382
383 /* Just update our place in the LRU if our fence is getting reused. */
384 if (obj->fence_reg != I915_FENCE_REG_NONE) {
385 reg = &dev_priv->fence_regs[obj->fence_reg];
386 if (!obj->fence_dirty) {
387 list_move_tail(&reg->lru_list,
388 &dev_priv->mm.fence_list);
389 return 0;
390 }
391 } else if (enable) {
392 if (WARN_ON(!obj->map_and_fenceable))
393 return -EINVAL;
394
395 reg = i915_find_fence_reg(dev);
396 if (IS_ERR(reg))
397 return PTR_ERR(reg);
398
399 if (reg->obj) {
400 struct drm_i915_gem_object *old = reg->obj;
401
402 ret = i915_gem_object_wait_fence(old);
403 if (ret)
404 return ret;
405
406 i915_gem_object_fence_lost(old);
407 }
408 } else
409 return 0;
410
411 i915_gem_object_update_fence(obj, reg, enable);
412
413 return 0;
414}
415
Daniel Vettera794f622015-07-24 17:40:12 +0200416/**
417 * i915_gem_object_pin_fence - pin fencing state
418 * @obj: object to pin fencing for
419 *
420 * This pins the fencing state (whether tiled or untiled) to make sure the
421 * object is ready to be used as a scanout target. Fencing status must be
422 * synchronize first by calling i915_gem_object_get_fence():
423 *
424 * The resulting fence pin reference must be released again with
425 * i915_gem_object_unpin_fence().
426 *
427 * Returns:
428 *
429 * True if the object has a fence, false otherwise.
430 */
Daniel Vetter41a36b72015-07-24 13:55:11 +0200431bool
432i915_gem_object_pin_fence(struct drm_i915_gem_object *obj)
433{
434 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Chris Wilson058d88c2016-08-15 10:49:06 +0100435 to_i915(obj->base.dev)->fence_regs[obj->fence_reg].pin_count++;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200436 return true;
437 } else
438 return false;
439}
440
Daniel Vettera794f622015-07-24 17:40:12 +0200441/**
442 * i915_gem_object_unpin_fence - unpin fencing state
443 * @obj: object to unpin fencing for
444 *
445 * This releases the fence pin reference acquired through
446 * i915_gem_object_pin_fence. It will handle both objects with and without an
447 * attached fence correctly, callers do not need to distinguish this.
448 */
Daniel Vetter41a36b72015-07-24 13:55:11 +0200449void
450i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
451{
452 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Chris Wilsonfac5e232016-07-04 11:34:36 +0100453 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200454 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
455 dev_priv->fence_regs[obj->fence_reg].pin_count--;
456 }
457}
458
Daniel Vettera794f622015-07-24 17:40:12 +0200459/**
460 * i915_gem_restore_fences - restore fence state
461 * @dev: DRM device
462 *
463 * Restore the hw fence state to match the software tracking again, to be called
464 * after a gpu reset and on resume.
465 */
Daniel Vetter41a36b72015-07-24 13:55:11 +0200466void i915_gem_restore_fences(struct drm_device *dev)
467{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100468 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200469 int i;
470
471 for (i = 0; i < dev_priv->num_fence_regs; i++) {
472 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
473
474 /*
475 * Commit delayed tiling changes if we have an object still
476 * attached to the fence, otherwise just clear the fence.
477 */
478 if (reg->obj) {
479 i915_gem_object_update_fence(reg->obj, reg,
Chris Wilson3e510a82016-08-05 10:14:23 +0100480 i915_gem_object_get_tiling(reg->obj));
Daniel Vetter41a36b72015-07-24 13:55:11 +0200481 } else {
482 i915_gem_write_fence(dev, i, NULL);
483 }
484 }
485}
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200486
487/**
Daniel Vetter3271dca2015-07-24 17:40:15 +0200488 * DOC: tiling swizzling details
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200489 *
490 * The idea behind tiling is to increase cache hit rates by rearranging
491 * pixel data so that a group of pixel accesses are in the same cacheline.
492 * Performance improvement from doing this on the back/depth buffer are on
493 * the order of 30%.
494 *
495 * Intel architectures make this somewhat more complicated, though, by
496 * adjustments made to addressing of data when the memory is in interleaved
497 * mode (matched pairs of DIMMS) to improve memory bandwidth.
498 * For interleaved memory, the CPU sends every sequential 64 bytes
499 * to an alternate memory channel so it can get the bandwidth from both.
500 *
501 * The GPU also rearranges its accesses for increased bandwidth to interleaved
502 * memory, and it matches what the CPU does for non-tiled. However, when tiled
503 * it does it a little differently, since one walks addresses not just in the
504 * X direction but also Y. So, along with alternating channels when bit
505 * 6 of the address flips, it also alternates when other bits flip -- Bits 9
506 * (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)
507 * are common to both the 915 and 965-class hardware.
508 *
509 * The CPU also sometimes XORs in higher bits as well, to improve
510 * bandwidth doing strided access like we do so frequently in graphics. This
511 * is called "Channel XOR Randomization" in the MCH documentation. The result
512 * is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address
513 * decode.
514 *
515 * All of this bit 6 XORing has an effect on our memory management,
516 * as we need to make sure that the 3d driver can correctly address object
517 * contents.
518 *
519 * If we don't have interleaved memory, all tiling is safe and no swizzling is
520 * required.
521 *
522 * When bit 17 is XORed in, we simply refuse to tile at all. Bit
Masanari Iida34fd3e12016-01-05 12:29:17 +0900523 * 17 is not just a page offset, so as we page an object out and back in,
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200524 * individual pages in it will have different bit 17 addresses, resulting in
525 * each 64 bytes being swapped with its neighbor!
526 *
527 * Otherwise, if interleaved, we have to tell the 3d driver what the address
528 * swizzling it needs to do is, since it's writing with the CPU to the pages
529 * (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the
530 * pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling
531 * required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order
532 * to match what the GPU expects.
533 */
534
535/**
Daniel Vetter3271dca2015-07-24 17:40:15 +0200536 * i915_gem_detect_bit_6_swizzle - detect bit 6 swizzling pattern
537 * @dev: DRM device
538 *
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200539 * Detects bit 6 swizzling of address lookup between IGD access and CPU
540 * access through main memory.
541 */
542void
543i915_gem_detect_bit_6_swizzle(struct drm_device *dev)
544{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100545 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200546 uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
547 uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
548
549 if (INTEL_INFO(dev)->gen >= 8 || IS_VALLEYVIEW(dev)) {
550 /*
551 * On BDW+, swizzling is not used. We leave the CPU memory
552 * controller in charge of optimizing memory accesses without
553 * the extra address manipulation GPU side.
554 *
555 * VLV and CHV don't have GPU swizzling.
556 */
557 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
558 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
559 } else if (INTEL_INFO(dev)->gen >= 6) {
560 if (dev_priv->preserve_bios_swizzle) {
561 if (I915_READ(DISP_ARB_CTL) &
562 DISP_TILE_SURFACE_SWIZZLING) {
563 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
564 swizzle_y = I915_BIT_6_SWIZZLE_9;
565 } else {
566 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
567 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
568 }
569 } else {
570 uint32_t dimm_c0, dimm_c1;
571 dimm_c0 = I915_READ(MAD_DIMM_C0);
572 dimm_c1 = I915_READ(MAD_DIMM_C1);
573 dimm_c0 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
574 dimm_c1 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
575 /* Enable swizzling when the channels are populated
576 * with identically sized dimms. We don't need to check
577 * the 3rd channel because no cpu with gpu attached
578 * ships in that configuration. Also, swizzling only
579 * makes sense for 2 channels anyway. */
580 if (dimm_c0 == dimm_c1) {
581 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
582 swizzle_y = I915_BIT_6_SWIZZLE_9;
583 } else {
584 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
585 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
586 }
587 }
588 } else if (IS_GEN5(dev)) {
589 /* On Ironlake whatever DRAM config, GPU always do
590 * same swizzling setup.
591 */
592 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
593 swizzle_y = I915_BIT_6_SWIZZLE_9;
594 } else if (IS_GEN2(dev)) {
595 /* As far as we know, the 865 doesn't have these bit 6
596 * swizzling issues.
597 */
598 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
599 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
600 } else if (IS_MOBILE(dev) || (IS_GEN3(dev) && !IS_G33(dev))) {
601 uint32_t dcc;
602
603 /* On 9xx chipsets, channel interleave by the CPU is
604 * determined by DCC. For single-channel, neither the CPU
605 * nor the GPU do swizzling. For dual channel interleaved,
606 * the GPU's interleave is bit 9 and 10 for X tiled, and bit
607 * 9 for Y tiled. The CPU's interleave is independent, and
608 * can be based on either bit 11 (haven't seen this yet) or
609 * bit 17 (common).
610 */
611 dcc = I915_READ(DCC);
612 switch (dcc & DCC_ADDRESSING_MODE_MASK) {
613 case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
614 case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
615 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
616 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
617 break;
618 case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
619 if (dcc & DCC_CHANNEL_XOR_DISABLE) {
620 /* This is the base swizzling by the GPU for
621 * tiled buffers.
622 */
623 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
624 swizzle_y = I915_BIT_6_SWIZZLE_9;
625 } else if ((dcc & DCC_CHANNEL_XOR_BIT_17) == 0) {
626 /* Bit 11 swizzling by the CPU in addition. */
627 swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
628 swizzle_y = I915_BIT_6_SWIZZLE_9_11;
629 } else {
630 /* Bit 17 swizzling by the CPU in addition. */
631 swizzle_x = I915_BIT_6_SWIZZLE_9_10_17;
632 swizzle_y = I915_BIT_6_SWIZZLE_9_17;
633 }
634 break;
635 }
636
637 /* check for L-shaped memory aka modified enhanced addressing */
Chris Wilson0b466dc22015-11-19 09:58:05 +0000638 if (IS_GEN4(dev) &&
639 !(I915_READ(DCC2) & DCC2_MODIFIED_ENHANCED_DISABLE)) {
640 swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
641 swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200642 }
643
644 if (dcc == 0xffffffff) {
645 DRM_ERROR("Couldn't read from MCHBAR. "
646 "Disabling tiling.\n");
647 swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
648 swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
649 }
650 } else {
651 /* The 965, G33, and newer, have a very flexible memory
652 * configuration. It will enable dual-channel mode
653 * (interleaving) on as much memory as it can, and the GPU
654 * will additionally sometimes enable different bit 6
655 * swizzling for tiled objects from the CPU.
656 *
657 * Here's what I found on the G965:
658 * slot fill memory size swizzling
659 * 0A 0B 1A 1B 1-ch 2-ch
660 * 512 0 0 0 512 0 O
661 * 512 0 512 0 16 1008 X
662 * 512 0 0 512 16 1008 X
663 * 0 512 0 512 16 1008 X
664 * 1024 1024 1024 0 2048 1024 O
665 *
666 * We could probably detect this based on either the DRB
667 * matching, which was the case for the swizzling required in
668 * the table above, or from the 1-ch value being less than
669 * the minimum size of a rank.
Chris Wilson0b466dc22015-11-19 09:58:05 +0000670 *
671 * Reports indicate that the swizzling actually
672 * varies depending upon page placement inside the
673 * channels, i.e. we see swizzled pages where the
674 * banks of memory are paired and unswizzled on the
675 * uneven portion, so leave that as unknown.
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200676 */
Chris Wilson0b466dc22015-11-19 09:58:05 +0000677 if (I915_READ16(C0DRB3) == I915_READ16(C1DRB3)) {
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200678 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
679 swizzle_y = I915_BIT_6_SWIZZLE_9;
680 }
681 }
682
Chris Wilson0b466dc22015-11-19 09:58:05 +0000683 if (swizzle_x == I915_BIT_6_SWIZZLE_UNKNOWN ||
684 swizzle_y == I915_BIT_6_SWIZZLE_UNKNOWN) {
685 /* Userspace likes to explode if it sees unknown swizzling,
686 * so lie. We will finish the lie when reporting through
687 * the get-tiling-ioctl by reporting the physical swizzle
688 * mode as unknown instead.
689 *
690 * As we don't strictly know what the swizzling is, it may be
691 * bit17 dependent, and so we need to also prevent the pages
692 * from being moved.
693 */
694 dev_priv->quirks |= QUIRK_PIN_SWIZZLED_PAGES;
695 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
696 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
697 }
698
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200699 dev_priv->mm.bit_6_swizzle_x = swizzle_x;
700 dev_priv->mm.bit_6_swizzle_y = swizzle_y;
701}
702
Daniel Vetter3271dca2015-07-24 17:40:15 +0200703/*
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200704 * Swap every 64 bytes of this page around, to account for it having a new
705 * bit 17 of its physical address and therefore being interpreted differently
706 * by the GPU.
707 */
708static void
709i915_gem_swizzle_page(struct page *page)
710{
711 char temp[64];
712 char *vaddr;
713 int i;
714
715 vaddr = kmap(page);
716
717 for (i = 0; i < PAGE_SIZE; i += 128) {
718 memcpy(temp, &vaddr[i], 64);
719 memcpy(&vaddr[i], &vaddr[i + 64], 64);
720 memcpy(&vaddr[i + 64], temp, 64);
721 }
722
723 kunmap(page);
724}
725
Daniel Vetter3271dca2015-07-24 17:40:15 +0200726/**
727 * i915_gem_object_do_bit_17_swizzle - fixup bit 17 swizzling
728 * @obj: i915 GEM buffer object
729 *
730 * This function fixes up the swizzling in case any page frame number for this
731 * object has changed in bit 17 since that state has been saved with
732 * i915_gem_object_save_bit_17_swizzle().
733 *
734 * This is called when pinning backing storage again, since the kernel is free
735 * to move unpinned backing storage around (either by directly moving pages or
736 * by swapping them out and back in again).
737 */
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200738void
739i915_gem_object_do_bit_17_swizzle(struct drm_i915_gem_object *obj)
740{
Dave Gordon85d12252016-05-20 11:54:06 +0100741 struct sgt_iter sgt_iter;
742 struct page *page;
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200743 int i;
744
745 if (obj->bit_17 == NULL)
746 return;
747
748 i = 0;
Dave Gordon85d12252016-05-20 11:54:06 +0100749 for_each_sgt_page(page, sgt_iter, obj->pages) {
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200750 char new_bit_17 = page_to_phys(page) >> 17;
751 if ((new_bit_17 & 0x1) !=
752 (test_bit(i, obj->bit_17) != 0)) {
753 i915_gem_swizzle_page(page);
754 set_page_dirty(page);
755 }
756 i++;
757 }
758}
759
Daniel Vetter3271dca2015-07-24 17:40:15 +0200760/**
761 * i915_gem_object_save_bit_17_swizzle - save bit 17 swizzling
762 * @obj: i915 GEM buffer object
763 *
764 * This function saves the bit 17 of each page frame number so that swizzling
765 * can be fixed up later on with i915_gem_object_do_bit_17_swizzle(). This must
766 * be called before the backing storage can be unpinned.
767 */
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200768void
769i915_gem_object_save_bit_17_swizzle(struct drm_i915_gem_object *obj)
770{
Dave Gordon85d12252016-05-20 11:54:06 +0100771 struct sgt_iter sgt_iter;
772 struct page *page;
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200773 int page_count = obj->base.size >> PAGE_SHIFT;
774 int i;
775
776 if (obj->bit_17 == NULL) {
777 obj->bit_17 = kcalloc(BITS_TO_LONGS(page_count),
778 sizeof(long), GFP_KERNEL);
779 if (obj->bit_17 == NULL) {
780 DRM_ERROR("Failed to allocate memory for bit 17 "
781 "record\n");
782 return;
783 }
784 }
785
786 i = 0;
Dave Gordon85d12252016-05-20 11:54:06 +0100787
788 for_each_sgt_page(page, sgt_iter, obj->pages) {
789 if (page_to_phys(page) & (1 << 17))
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200790 __set_bit(i, obj->bit_17);
791 else
792 __clear_bit(i, obj->bit_17);
793 i++;
794 }
795}