blob: 9fdbd66128a68bdcd4b786664734d3b128e0d8b9 [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) {
88 u32 size = i915_gem_obj_ggtt_size(obj);
89 uint64_t val;
90
91 /* Adjust fence size to match tiled area */
92 if (obj->tiling_mode != I915_TILING_NONE) {
93 uint32_t row_size = obj->stride *
94 (obj->tiling_mode == I915_TILING_Y ? 32 : 8);
95 size = (size / row_size) * row_size;
96 }
97
98 val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
99 0xfffff000) << 32;
100 val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
101 val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
102 if (obj->tiling_mode == I915_TILING_Y)
103 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
104 val |= I965_FENCE_REG_VALID;
105
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300106 I915_WRITE(fence_reg_hi, val >> 32);
107 POSTING_READ(fence_reg_hi);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200108
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300109 I915_WRITE(fence_reg_lo, val);
110 POSTING_READ(fence_reg_lo);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200111 } else {
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300112 I915_WRITE(fence_reg_hi, 0);
113 POSTING_READ(fence_reg_hi);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200114 }
115}
116
117static void i915_write_fence_reg(struct drm_device *dev, int reg,
118 struct drm_i915_gem_object *obj)
119{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100120 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200121 u32 val;
122
123 if (obj) {
124 u32 size = i915_gem_obj_ggtt_size(obj);
125 int pitch_val;
126 int tile_width;
127
128 WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
129 (size & -size) != size ||
130 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
Michel Thierry088e0df2015-08-07 17:40:17 +0100131 "object 0x%08llx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
Daniel Vetter41a36b72015-07-24 13:55:11 +0200132 i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
133
134 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
135 tile_width = 128;
136 else
137 tile_width = 512;
138
139 /* Note: pitch better be a power of two tile widths */
140 pitch_val = obj->stride / tile_width;
141 pitch_val = ffs(pitch_val) - 1;
142
143 val = i915_gem_obj_ggtt_offset(obj);
144 if (obj->tiling_mode == I915_TILING_Y)
145 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
146 val |= I915_FENCE_SIZE_BITS(size);
147 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
148 val |= I830_FENCE_REG_VALID;
149 } else
150 val = 0;
151
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300152 I915_WRITE(FENCE_REG(reg), val);
153 POSTING_READ(FENCE_REG(reg));
Daniel Vetter41a36b72015-07-24 13:55:11 +0200154}
155
156static void i830_write_fence_reg(struct drm_device *dev, int reg,
157 struct drm_i915_gem_object *obj)
158{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100159 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200160 uint32_t val;
161
162 if (obj) {
163 u32 size = i915_gem_obj_ggtt_size(obj);
164 uint32_t pitch_val;
165
166 WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
167 (size & -size) != size ||
168 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
Michel Thierry088e0df2015-08-07 17:40:17 +0100169 "object 0x%08llx not 512K or pot-size 0x%08x aligned\n",
Daniel Vetter41a36b72015-07-24 13:55:11 +0200170 i915_gem_obj_ggtt_offset(obj), size);
171
172 pitch_val = obj->stride / 128;
173 pitch_val = ffs(pitch_val) - 1;
174
175 val = i915_gem_obj_ggtt_offset(obj);
176 if (obj->tiling_mode == I915_TILING_Y)
177 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
178 val |= I830_FENCE_SIZE_BITS(size);
179 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
180 val |= I830_FENCE_REG_VALID;
181 } else
182 val = 0;
183
Ville Syrjäläeecf6132015-09-21 18:05:14 +0300184 I915_WRITE(FENCE_REG(reg), val);
185 POSTING_READ(FENCE_REG(reg));
Daniel Vetter41a36b72015-07-24 13:55:11 +0200186}
187
188inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
189{
190 return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
191}
192
193static void i915_gem_write_fence(struct drm_device *dev, int reg,
194 struct drm_i915_gem_object *obj)
195{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100196 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200197
198 /* Ensure that all CPU reads are completed before installing a fence
199 * and all writes before removing the fence.
200 */
201 if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
202 mb();
203
204 WARN(obj && (!obj->stride || !obj->tiling_mode),
205 "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
206 obj->stride, obj->tiling_mode);
207
208 if (IS_GEN2(dev))
209 i830_write_fence_reg(dev, reg, obj);
210 else if (IS_GEN3(dev))
211 i915_write_fence_reg(dev, reg, obj);
212 else if (INTEL_INFO(dev)->gen >= 4)
213 i965_write_fence_reg(dev, reg, obj);
214
215 /* And similarly be paranoid that no direct access to this region
216 * is reordered to before the fence is installed.
217 */
218 if (i915_gem_object_needs_mb(obj))
219 mb();
220}
221
222static inline int fence_number(struct drm_i915_private *dev_priv,
223 struct drm_i915_fence_reg *fence)
224{
225 return fence - dev_priv->fence_regs;
226}
227
228static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
229 struct drm_i915_fence_reg *fence,
230 bool enable)
231{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100232 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200233 int reg = fence_number(dev_priv, fence);
234
235 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
236
237 if (enable) {
238 obj->fence_reg = reg;
239 fence->obj = obj;
240 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
241 } else {
242 obj->fence_reg = I915_FENCE_REG_NONE;
243 fence->obj = NULL;
244 list_del_init(&fence->lru_list);
245 }
246 obj->fence_dirty = false;
247}
248
249static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
250{
251 if (obj->tiling_mode)
252 i915_gem_release_mmap(obj);
253
254 /* As we do not have an associated fence register, we will force
255 * a tiling change if we ever need to acquire one.
256 */
257 obj->fence_dirty = false;
258 obj->fence_reg = I915_FENCE_REG_NONE;
259}
260
261static int
262i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
263{
Chris Wilson27c01aa2016-08-04 07:52:30 +0100264 int ret;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200265
Chris Wilson27c01aa2016-08-04 07:52:30 +0100266 ret = i915_gem_active_wait(&obj->last_fence);
267 if (ret)
268 return ret;
Daniel Vetter41a36b72015-07-24 13:55:11 +0200269
Chris Wilson27c01aa2016-08-04 07:52:30 +0100270 i915_gem_active_set(&obj->last_fence, NULL);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200271 return 0;
272}
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
301 if (WARN_ON(fence->pin_count))
302 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);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200370 bool enable = obj->tiling_mode != I915_TILING_NONE;
371 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 Wilsonfac5e232016-07-04 11:34:36 +0100435 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200436 struct i915_vma *ggtt_vma = i915_gem_obj_to_ggtt(obj);
437
438 WARN_ON(!ggtt_vma ||
439 dev_priv->fence_regs[obj->fence_reg].pin_count >
440 ggtt_vma->pin_count);
441 dev_priv->fence_regs[obj->fence_reg].pin_count++;
442 return true;
443 } else
444 return false;
445}
446
Daniel Vettera794f622015-07-24 17:40:12 +0200447/**
448 * i915_gem_object_unpin_fence - unpin fencing state
449 * @obj: object to unpin fencing for
450 *
451 * This releases the fence pin reference acquired through
452 * i915_gem_object_pin_fence. It will handle both objects with and without an
453 * attached fence correctly, callers do not need to distinguish this.
454 */
Daniel Vetter41a36b72015-07-24 13:55:11 +0200455void
456i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
457{
458 if (obj->fence_reg != I915_FENCE_REG_NONE) {
Chris Wilsonfac5e232016-07-04 11:34:36 +0100459 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200460 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
461 dev_priv->fence_regs[obj->fence_reg].pin_count--;
462 }
463}
464
Daniel Vettera794f622015-07-24 17:40:12 +0200465/**
466 * i915_gem_restore_fences - restore fence state
467 * @dev: DRM device
468 *
469 * Restore the hw fence state to match the software tracking again, to be called
470 * after a gpu reset and on resume.
471 */
Daniel Vetter41a36b72015-07-24 13:55:11 +0200472void i915_gem_restore_fences(struct drm_device *dev)
473{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100474 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter41a36b72015-07-24 13:55:11 +0200475 int i;
476
477 for (i = 0; i < dev_priv->num_fence_regs; i++) {
478 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
479
480 /*
481 * Commit delayed tiling changes if we have an object still
482 * attached to the fence, otherwise just clear the fence.
483 */
484 if (reg->obj) {
485 i915_gem_object_update_fence(reg->obj, reg,
486 reg->obj->tiling_mode);
487 } else {
488 i915_gem_write_fence(dev, i, NULL);
489 }
490 }
491}
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200492
493/**
Daniel Vetter3271dca2015-07-24 17:40:15 +0200494 * DOC: tiling swizzling details
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200495 *
496 * The idea behind tiling is to increase cache hit rates by rearranging
497 * pixel data so that a group of pixel accesses are in the same cacheline.
498 * Performance improvement from doing this on the back/depth buffer are on
499 * the order of 30%.
500 *
501 * Intel architectures make this somewhat more complicated, though, by
502 * adjustments made to addressing of data when the memory is in interleaved
503 * mode (matched pairs of DIMMS) to improve memory bandwidth.
504 * For interleaved memory, the CPU sends every sequential 64 bytes
505 * to an alternate memory channel so it can get the bandwidth from both.
506 *
507 * The GPU also rearranges its accesses for increased bandwidth to interleaved
508 * memory, and it matches what the CPU does for non-tiled. However, when tiled
509 * it does it a little differently, since one walks addresses not just in the
510 * X direction but also Y. So, along with alternating channels when bit
511 * 6 of the address flips, it also alternates when other bits flip -- Bits 9
512 * (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)
513 * are common to both the 915 and 965-class hardware.
514 *
515 * The CPU also sometimes XORs in higher bits as well, to improve
516 * bandwidth doing strided access like we do so frequently in graphics. This
517 * is called "Channel XOR Randomization" in the MCH documentation. The result
518 * is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address
519 * decode.
520 *
521 * All of this bit 6 XORing has an effect on our memory management,
522 * as we need to make sure that the 3d driver can correctly address object
523 * contents.
524 *
525 * If we don't have interleaved memory, all tiling is safe and no swizzling is
526 * required.
527 *
528 * When bit 17 is XORed in, we simply refuse to tile at all. Bit
Masanari Iida34fd3e12016-01-05 12:29:17 +0900529 * 17 is not just a page offset, so as we page an object out and back in,
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200530 * individual pages in it will have different bit 17 addresses, resulting in
531 * each 64 bytes being swapped with its neighbor!
532 *
533 * Otherwise, if interleaved, we have to tell the 3d driver what the address
534 * swizzling it needs to do is, since it's writing with the CPU to the pages
535 * (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the
536 * pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling
537 * required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order
538 * to match what the GPU expects.
539 */
540
541/**
Daniel Vetter3271dca2015-07-24 17:40:15 +0200542 * i915_gem_detect_bit_6_swizzle - detect bit 6 swizzling pattern
543 * @dev: DRM device
544 *
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200545 * Detects bit 6 swizzling of address lookup between IGD access and CPU
546 * access through main memory.
547 */
548void
549i915_gem_detect_bit_6_swizzle(struct drm_device *dev)
550{
Chris Wilsonfac5e232016-07-04 11:34:36 +0100551 struct drm_i915_private *dev_priv = to_i915(dev);
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200552 uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
553 uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
554
555 if (INTEL_INFO(dev)->gen >= 8 || IS_VALLEYVIEW(dev)) {
556 /*
557 * On BDW+, swizzling is not used. We leave the CPU memory
558 * controller in charge of optimizing memory accesses without
559 * the extra address manipulation GPU side.
560 *
561 * VLV and CHV don't have GPU swizzling.
562 */
563 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
564 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
565 } else if (INTEL_INFO(dev)->gen >= 6) {
566 if (dev_priv->preserve_bios_swizzle) {
567 if (I915_READ(DISP_ARB_CTL) &
568 DISP_TILE_SURFACE_SWIZZLING) {
569 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
570 swizzle_y = I915_BIT_6_SWIZZLE_9;
571 } else {
572 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
573 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
574 }
575 } else {
576 uint32_t dimm_c0, dimm_c1;
577 dimm_c0 = I915_READ(MAD_DIMM_C0);
578 dimm_c1 = I915_READ(MAD_DIMM_C1);
579 dimm_c0 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
580 dimm_c1 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
581 /* Enable swizzling when the channels are populated
582 * with identically sized dimms. We don't need to check
583 * the 3rd channel because no cpu with gpu attached
584 * ships in that configuration. Also, swizzling only
585 * makes sense for 2 channels anyway. */
586 if (dimm_c0 == dimm_c1) {
587 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
588 swizzle_y = I915_BIT_6_SWIZZLE_9;
589 } else {
590 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
591 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
592 }
593 }
594 } else if (IS_GEN5(dev)) {
595 /* On Ironlake whatever DRAM config, GPU always do
596 * same swizzling setup.
597 */
598 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
599 swizzle_y = I915_BIT_6_SWIZZLE_9;
600 } else if (IS_GEN2(dev)) {
601 /* As far as we know, the 865 doesn't have these bit 6
602 * swizzling issues.
603 */
604 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
605 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
606 } else if (IS_MOBILE(dev) || (IS_GEN3(dev) && !IS_G33(dev))) {
607 uint32_t dcc;
608
609 /* On 9xx chipsets, channel interleave by the CPU is
610 * determined by DCC. For single-channel, neither the CPU
611 * nor the GPU do swizzling. For dual channel interleaved,
612 * the GPU's interleave is bit 9 and 10 for X tiled, and bit
613 * 9 for Y tiled. The CPU's interleave is independent, and
614 * can be based on either bit 11 (haven't seen this yet) or
615 * bit 17 (common).
616 */
617 dcc = I915_READ(DCC);
618 switch (dcc & DCC_ADDRESSING_MODE_MASK) {
619 case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
620 case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
621 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
622 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
623 break;
624 case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
625 if (dcc & DCC_CHANNEL_XOR_DISABLE) {
626 /* This is the base swizzling by the GPU for
627 * tiled buffers.
628 */
629 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
630 swizzle_y = I915_BIT_6_SWIZZLE_9;
631 } else if ((dcc & DCC_CHANNEL_XOR_BIT_17) == 0) {
632 /* Bit 11 swizzling by the CPU in addition. */
633 swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
634 swizzle_y = I915_BIT_6_SWIZZLE_9_11;
635 } else {
636 /* Bit 17 swizzling by the CPU in addition. */
637 swizzle_x = I915_BIT_6_SWIZZLE_9_10_17;
638 swizzle_y = I915_BIT_6_SWIZZLE_9_17;
639 }
640 break;
641 }
642
643 /* check for L-shaped memory aka modified enhanced addressing */
Chris Wilson0b466dc22015-11-19 09:58:05 +0000644 if (IS_GEN4(dev) &&
645 !(I915_READ(DCC2) & DCC2_MODIFIED_ENHANCED_DISABLE)) {
646 swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
647 swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200648 }
649
650 if (dcc == 0xffffffff) {
651 DRM_ERROR("Couldn't read from MCHBAR. "
652 "Disabling tiling.\n");
653 swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
654 swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
655 }
656 } else {
657 /* The 965, G33, and newer, have a very flexible memory
658 * configuration. It will enable dual-channel mode
659 * (interleaving) on as much memory as it can, and the GPU
660 * will additionally sometimes enable different bit 6
661 * swizzling for tiled objects from the CPU.
662 *
663 * Here's what I found on the G965:
664 * slot fill memory size swizzling
665 * 0A 0B 1A 1B 1-ch 2-ch
666 * 512 0 0 0 512 0 O
667 * 512 0 512 0 16 1008 X
668 * 512 0 0 512 16 1008 X
669 * 0 512 0 512 16 1008 X
670 * 1024 1024 1024 0 2048 1024 O
671 *
672 * We could probably detect this based on either the DRB
673 * matching, which was the case for the swizzling required in
674 * the table above, or from the 1-ch value being less than
675 * the minimum size of a rank.
Chris Wilson0b466dc22015-11-19 09:58:05 +0000676 *
677 * Reports indicate that the swizzling actually
678 * varies depending upon page placement inside the
679 * channels, i.e. we see swizzled pages where the
680 * banks of memory are paired and unswizzled on the
681 * uneven portion, so leave that as unknown.
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200682 */
Chris Wilson0b466dc22015-11-19 09:58:05 +0000683 if (I915_READ16(C0DRB3) == I915_READ16(C1DRB3)) {
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200684 swizzle_x = I915_BIT_6_SWIZZLE_9_10;
685 swizzle_y = I915_BIT_6_SWIZZLE_9;
686 }
687 }
688
Chris Wilson0b466dc22015-11-19 09:58:05 +0000689 if (swizzle_x == I915_BIT_6_SWIZZLE_UNKNOWN ||
690 swizzle_y == I915_BIT_6_SWIZZLE_UNKNOWN) {
691 /* Userspace likes to explode if it sees unknown swizzling,
692 * so lie. We will finish the lie when reporting through
693 * the get-tiling-ioctl by reporting the physical swizzle
694 * mode as unknown instead.
695 *
696 * As we don't strictly know what the swizzling is, it may be
697 * bit17 dependent, and so we need to also prevent the pages
698 * from being moved.
699 */
700 dev_priv->quirks |= QUIRK_PIN_SWIZZLED_PAGES;
701 swizzle_x = I915_BIT_6_SWIZZLE_NONE;
702 swizzle_y = I915_BIT_6_SWIZZLE_NONE;
703 }
704
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200705 dev_priv->mm.bit_6_swizzle_x = swizzle_x;
706 dev_priv->mm.bit_6_swizzle_y = swizzle_y;
707}
708
Daniel Vetter3271dca2015-07-24 17:40:15 +0200709/*
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200710 * Swap every 64 bytes of this page around, to account for it having a new
711 * bit 17 of its physical address and therefore being interpreted differently
712 * by the GPU.
713 */
714static void
715i915_gem_swizzle_page(struct page *page)
716{
717 char temp[64];
718 char *vaddr;
719 int i;
720
721 vaddr = kmap(page);
722
723 for (i = 0; i < PAGE_SIZE; i += 128) {
724 memcpy(temp, &vaddr[i], 64);
725 memcpy(&vaddr[i], &vaddr[i + 64], 64);
726 memcpy(&vaddr[i + 64], temp, 64);
727 }
728
729 kunmap(page);
730}
731
Daniel Vetter3271dca2015-07-24 17:40:15 +0200732/**
733 * i915_gem_object_do_bit_17_swizzle - fixup bit 17 swizzling
734 * @obj: i915 GEM buffer object
735 *
736 * This function fixes up the swizzling in case any page frame number for this
737 * object has changed in bit 17 since that state has been saved with
738 * i915_gem_object_save_bit_17_swizzle().
739 *
740 * This is called when pinning backing storage again, since the kernel is free
741 * to move unpinned backing storage around (either by directly moving pages or
742 * by swapping them out and back in again).
743 */
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200744void
745i915_gem_object_do_bit_17_swizzle(struct drm_i915_gem_object *obj)
746{
Dave Gordon85d12252016-05-20 11:54:06 +0100747 struct sgt_iter sgt_iter;
748 struct page *page;
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200749 int i;
750
751 if (obj->bit_17 == NULL)
752 return;
753
754 i = 0;
Dave Gordon85d12252016-05-20 11:54:06 +0100755 for_each_sgt_page(page, sgt_iter, obj->pages) {
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200756 char new_bit_17 = page_to_phys(page) >> 17;
757 if ((new_bit_17 & 0x1) !=
758 (test_bit(i, obj->bit_17) != 0)) {
759 i915_gem_swizzle_page(page);
760 set_page_dirty(page);
761 }
762 i++;
763 }
764}
765
Daniel Vetter3271dca2015-07-24 17:40:15 +0200766/**
767 * i915_gem_object_save_bit_17_swizzle - save bit 17 swizzling
768 * @obj: i915 GEM buffer object
769 *
770 * This function saves the bit 17 of each page frame number so that swizzling
771 * can be fixed up later on with i915_gem_object_do_bit_17_swizzle(). This must
772 * be called before the backing storage can be unpinned.
773 */
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200774void
775i915_gem_object_save_bit_17_swizzle(struct drm_i915_gem_object *obj)
776{
Dave Gordon85d12252016-05-20 11:54:06 +0100777 struct sgt_iter sgt_iter;
778 struct page *page;
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200779 int page_count = obj->base.size >> PAGE_SHIFT;
780 int i;
781
782 if (obj->bit_17 == NULL) {
783 obj->bit_17 = kcalloc(BITS_TO_LONGS(page_count),
784 sizeof(long), GFP_KERNEL);
785 if (obj->bit_17 == NULL) {
786 DRM_ERROR("Failed to allocate memory for bit 17 "
787 "record\n");
788 return;
789 }
790 }
791
792 i = 0;
Dave Gordon85d12252016-05-20 11:54:06 +0100793
794 for_each_sgt_page(page, sgt_iter, obj->pages) {
795 if (page_to_phys(page) & (1 << 17))
Daniel Vetter7f96eca2015-07-24 17:40:14 +0200796 __set_bit(i, obj->bit_17);
797 else
798 __clear_bit(i, obj->bit_17);
799 i++;
800 }
801}