i915_gem_fence.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  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. #include <drm/drmP.h>
  24. #include <drm/i915_drm.h>
  25. #include "i915_drv.h"
  26. /**
  27. * DOC: fence register handling
  28. *
  29. * Important to avoid confusions: "fences" in the i915 driver are not execution
  30. * fences used to track command completion but hardware detiler objects which
  31. * wrap a given range of the global GTT. Each platform has only a fairly limited
  32. * set of these objects.
  33. *
  34. * Fences are used to detile GTT memory mappings. They're also connected to the
  35. * hardware frontbuffer render tracking and hence interact with frontbuffer
  36. * compression. Furthermore on older platforms fences are required for tiled
  37. * objects used by the display engine. They can also be used by the render
  38. * engine - they're required for blitter commands and are optional for render
  39. * commands. But on gen4+ both display (with the exception of fbc) and rendering
  40. * have their own tiling state bits and don't need fences.
  41. *
  42. * Also note that fences only support X and Y tiling and hence can't be used for
  43. * the fancier new tiling formats like W, Ys and Yf.
  44. *
  45. * Finally note that because fences are such a restricted resource they're
  46. * dynamically associated with objects. Furthermore fence state is committed to
  47. * the hardware lazily to avoid unnecessary stalls on gen2/3. Therefore code must
  48. * explicitly call i915_gem_object_get_fence() to synchronize fencing status
  49. * for cpu access. Also note that some code wants an unfenced view, for those
  50. * cases the fence can be removed forcefully with i915_gem_object_put_fence().
  51. *
  52. * Internally these functions will synchronize with userspace access by removing
  53. * CPU ptes into GTT mmaps (not the GTT ptes themselves) as needed.
  54. */
  55. #define pipelined 0
  56. static void i965_write_fence_reg(struct drm_i915_fence_reg *fence,
  57. struct i915_vma *vma)
  58. {
  59. i915_reg_t fence_reg_lo, fence_reg_hi;
  60. int fence_pitch_shift;
  61. u64 val;
  62. if (INTEL_INFO(fence->i915)->gen >= 6) {
  63. fence_reg_lo = FENCE_REG_GEN6_LO(fence->id);
  64. fence_reg_hi = FENCE_REG_GEN6_HI(fence->id);
  65. fence_pitch_shift = GEN6_FENCE_PITCH_SHIFT;
  66. } else {
  67. fence_reg_lo = FENCE_REG_965_LO(fence->id);
  68. fence_reg_hi = FENCE_REG_965_HI(fence->id);
  69. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  70. }
  71. val = 0;
  72. if (vma) {
  73. unsigned int tiling = i915_gem_object_get_tiling(vma->obj);
  74. bool is_y_tiled = tiling == I915_TILING_Y;
  75. unsigned int stride = i915_gem_object_get_stride(vma->obj);
  76. u32 row_size = stride * (is_y_tiled ? 32 : 8);
  77. u32 size = rounddown((u32)vma->node.size, row_size);
  78. val = ((vma->node.start + size - 4096) & 0xfffff000) << 32;
  79. val |= vma->node.start & 0xfffff000;
  80. val |= (u64)((stride / 128) - 1) << fence_pitch_shift;
  81. if (is_y_tiled)
  82. val |= BIT(I965_FENCE_TILING_Y_SHIFT);
  83. val |= I965_FENCE_REG_VALID;
  84. }
  85. if (!pipelined) {
  86. struct drm_i915_private *dev_priv = fence->i915;
  87. /* To w/a incoherency with non-atomic 64-bit register updates,
  88. * we split the 64-bit update into two 32-bit writes. In order
  89. * for a partial fence not to be evaluated between writes, we
  90. * precede the update with write to turn off the fence register,
  91. * and only enable the fence as the last step.
  92. *
  93. * For extra levels of paranoia, we make sure each step lands
  94. * before applying the next step.
  95. */
  96. I915_WRITE(fence_reg_lo, 0);
  97. POSTING_READ(fence_reg_lo);
  98. I915_WRITE(fence_reg_hi, upper_32_bits(val));
  99. I915_WRITE(fence_reg_lo, lower_32_bits(val));
  100. POSTING_READ(fence_reg_lo);
  101. }
  102. }
  103. static void i915_write_fence_reg(struct drm_i915_fence_reg *fence,
  104. struct i915_vma *vma)
  105. {
  106. u32 val;
  107. val = 0;
  108. if (vma) {
  109. unsigned int tiling = i915_gem_object_get_tiling(vma->obj);
  110. bool is_y_tiled = tiling == I915_TILING_Y;
  111. unsigned int stride = i915_gem_object_get_stride(vma->obj);
  112. int pitch_val;
  113. int tile_width;
  114. WARN((vma->node.start & ~I915_FENCE_START_MASK) ||
  115. !is_power_of_2(vma->node.size) ||
  116. (vma->node.start & (vma->node.size - 1)),
  117. "object 0x%08llx [fenceable? %d] not 1M or pot-size (0x%08llx) aligned\n",
  118. vma->node.start,
  119. i915_vma_is_map_and_fenceable(vma),
  120. vma->node.size);
  121. if (is_y_tiled && HAS_128_BYTE_Y_TILING(fence->i915))
  122. tile_width = 128;
  123. else
  124. tile_width = 512;
  125. /* Note: pitch better be a power of two tile widths */
  126. pitch_val = stride / tile_width;
  127. pitch_val = ffs(pitch_val) - 1;
  128. val = vma->node.start;
  129. if (is_y_tiled)
  130. val |= BIT(I830_FENCE_TILING_Y_SHIFT);
  131. val |= I915_FENCE_SIZE_BITS(vma->node.size);
  132. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  133. val |= I830_FENCE_REG_VALID;
  134. }
  135. if (!pipelined) {
  136. struct drm_i915_private *dev_priv = fence->i915;
  137. i915_reg_t reg = FENCE_REG(fence->id);
  138. I915_WRITE(reg, val);
  139. POSTING_READ(reg);
  140. }
  141. }
  142. static void i830_write_fence_reg(struct drm_i915_fence_reg *fence,
  143. struct i915_vma *vma)
  144. {
  145. u32 val;
  146. val = 0;
  147. if (vma) {
  148. unsigned int tiling = i915_gem_object_get_tiling(vma->obj);
  149. bool is_y_tiled = tiling == I915_TILING_Y;
  150. unsigned int stride = i915_gem_object_get_stride(vma->obj);
  151. u32 pitch_val;
  152. WARN((vma->node.start & ~I830_FENCE_START_MASK) ||
  153. !is_power_of_2(vma->node.size) ||
  154. (vma->node.start & (vma->node.size - 1)),
  155. "object 0x%08llx not 512K or pot-size 0x%08llx aligned\n",
  156. vma->node.start, vma->node.size);
  157. pitch_val = stride / 128;
  158. pitch_val = ffs(pitch_val) - 1;
  159. val = vma->node.start;
  160. if (is_y_tiled)
  161. val |= BIT(I830_FENCE_TILING_Y_SHIFT);
  162. val |= I830_FENCE_SIZE_BITS(vma->node.size);
  163. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  164. val |= I830_FENCE_REG_VALID;
  165. }
  166. if (!pipelined) {
  167. struct drm_i915_private *dev_priv = fence->i915;
  168. i915_reg_t reg = FENCE_REG(fence->id);
  169. I915_WRITE(reg, val);
  170. POSTING_READ(reg);
  171. }
  172. }
  173. static void fence_write(struct drm_i915_fence_reg *fence,
  174. struct i915_vma *vma)
  175. {
  176. /* Previous access through the fence register is marshalled by
  177. * the mb() inside the fault handlers (i915_gem_release_mmaps)
  178. * and explicitly managed for internal users.
  179. */
  180. if (IS_GEN2(fence->i915))
  181. i830_write_fence_reg(fence, vma);
  182. else if (IS_GEN3(fence->i915))
  183. i915_write_fence_reg(fence, vma);
  184. else
  185. i965_write_fence_reg(fence, vma);
  186. /* Access through the fenced region afterwards is
  187. * ordered by the posting reads whilst writing the registers.
  188. */
  189. fence->dirty = false;
  190. }
  191. static int fence_update(struct drm_i915_fence_reg *fence,
  192. struct i915_vma *vma)
  193. {
  194. int ret;
  195. if (vma) {
  196. if (!i915_vma_is_map_and_fenceable(vma))
  197. return -EINVAL;
  198. if (WARN(!i915_gem_object_get_stride(vma->obj) ||
  199. !i915_gem_object_get_tiling(vma->obj),
  200. "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
  201. i915_gem_object_get_stride(vma->obj),
  202. i915_gem_object_get_tiling(vma->obj)))
  203. return -EINVAL;
  204. ret = i915_gem_active_retire(&vma->last_fence,
  205. &vma->obj->base.dev->struct_mutex);
  206. if (ret)
  207. return ret;
  208. }
  209. if (fence->vma) {
  210. ret = i915_gem_active_retire(&fence->vma->last_fence,
  211. &fence->vma->obj->base.dev->struct_mutex);
  212. if (ret)
  213. return ret;
  214. }
  215. if (fence->vma && fence->vma != vma) {
  216. /* Ensure that all userspace CPU access is completed before
  217. * stealing the fence.
  218. */
  219. i915_gem_release_mmap(fence->vma->obj);
  220. fence->vma->fence = NULL;
  221. fence->vma = NULL;
  222. list_move(&fence->link, &fence->i915->mm.fence_list);
  223. }
  224. fence_write(fence, vma);
  225. if (vma) {
  226. if (fence->vma != vma) {
  227. vma->fence = fence;
  228. fence->vma = vma;
  229. }
  230. list_move_tail(&fence->link, &fence->i915->mm.fence_list);
  231. }
  232. return 0;
  233. }
  234. /**
  235. * i915_vma_put_fence - force-remove fence for a VMA
  236. * @vma: vma to map linearly (not through a fence reg)
  237. *
  238. * This function force-removes any fence from the given object, which is useful
  239. * if the kernel wants to do untiled GTT access.
  240. *
  241. * Returns:
  242. *
  243. * 0 on success, negative error code on failure.
  244. */
  245. int
  246. i915_vma_put_fence(struct i915_vma *vma)
  247. {
  248. struct drm_i915_fence_reg *fence = vma->fence;
  249. assert_rpm_wakelock_held(to_i915(vma->vm->dev));
  250. if (!fence)
  251. return 0;
  252. if (fence->pin_count)
  253. return -EBUSY;
  254. return fence_update(fence, NULL);
  255. }
  256. static struct drm_i915_fence_reg *fence_find(struct drm_i915_private *dev_priv)
  257. {
  258. struct drm_i915_fence_reg *fence;
  259. list_for_each_entry(fence, &dev_priv->mm.fence_list, link) {
  260. if (fence->pin_count)
  261. continue;
  262. return fence;
  263. }
  264. /* Wait for completion of pending flips which consume fences */
  265. if (intel_has_pending_fb_unpin(&dev_priv->drm))
  266. return ERR_PTR(-EAGAIN);
  267. return ERR_PTR(-EDEADLK);
  268. }
  269. /**
  270. * i915_vma_get_fence - set up fencing for a vma
  271. * @vma: vma to map through a fence reg
  272. *
  273. * When mapping objects through the GTT, userspace wants to be able to write
  274. * to them without having to worry about swizzling if the object is tiled.
  275. * This function walks the fence regs looking for a free one for @obj,
  276. * stealing one if it can't find any.
  277. *
  278. * It then sets up the reg based on the object's properties: address, pitch
  279. * and tiling format.
  280. *
  281. * For an untiled surface, this removes any existing fence.
  282. *
  283. * Returns:
  284. *
  285. * 0 on success, negative error code on failure.
  286. */
  287. int
  288. i915_vma_get_fence(struct i915_vma *vma)
  289. {
  290. struct drm_i915_fence_reg *fence;
  291. struct i915_vma *set = i915_gem_object_is_tiled(vma->obj) ? vma : NULL;
  292. assert_rpm_wakelock_held(to_i915(vma->vm->dev));
  293. /* Just update our place in the LRU if our fence is getting reused. */
  294. if (vma->fence) {
  295. fence = vma->fence;
  296. if (!fence->dirty) {
  297. list_move_tail(&fence->link,
  298. &fence->i915->mm.fence_list);
  299. return 0;
  300. }
  301. } else if (set) {
  302. fence = fence_find(to_i915(vma->vm->dev));
  303. if (IS_ERR(fence))
  304. return PTR_ERR(fence);
  305. } else
  306. return 0;
  307. return fence_update(fence, set);
  308. }
  309. /**
  310. * i915_gem_restore_fences - restore fence state
  311. * @dev: DRM device
  312. *
  313. * Restore the hw fence state to match the software tracking again, to be called
  314. * after a gpu reset and on resume.
  315. */
  316. void i915_gem_restore_fences(struct drm_device *dev)
  317. {
  318. struct drm_i915_private *dev_priv = to_i915(dev);
  319. int i;
  320. /* Note that this may be called outside of struct_mutex, by
  321. * runtime suspend/resume. The barrier we require is enforced by
  322. * rpm itself - all access to fences/GTT are only within an rpm
  323. * wakeref, and to acquire that wakeref you must pass through here.
  324. */
  325. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  326. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  327. struct i915_vma *vma = reg->vma;
  328. /*
  329. * Commit delayed tiling changes if we have an object still
  330. * attached to the fence, otherwise just clear the fence.
  331. */
  332. if (vma && !i915_gem_object_is_tiled(vma->obj)) {
  333. GEM_BUG_ON(!reg->dirty);
  334. GEM_BUG_ON(vma->obj->fault_mappable);
  335. list_move(&reg->link, &dev_priv->mm.fence_list);
  336. vma->fence = NULL;
  337. vma = NULL;
  338. }
  339. fence_write(reg, vma);
  340. reg->vma = vma;
  341. }
  342. }
  343. /**
  344. * DOC: tiling swizzling details
  345. *
  346. * The idea behind tiling is to increase cache hit rates by rearranging
  347. * pixel data so that a group of pixel accesses are in the same cacheline.
  348. * Performance improvement from doing this on the back/depth buffer are on
  349. * the order of 30%.
  350. *
  351. * Intel architectures make this somewhat more complicated, though, by
  352. * adjustments made to addressing of data when the memory is in interleaved
  353. * mode (matched pairs of DIMMS) to improve memory bandwidth.
  354. * For interleaved memory, the CPU sends every sequential 64 bytes
  355. * to an alternate memory channel so it can get the bandwidth from both.
  356. *
  357. * The GPU also rearranges its accesses for increased bandwidth to interleaved
  358. * memory, and it matches what the CPU does for non-tiled. However, when tiled
  359. * it does it a little differently, since one walks addresses not just in the
  360. * X direction but also Y. So, along with alternating channels when bit
  361. * 6 of the address flips, it also alternates when other bits flip -- Bits 9
  362. * (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)
  363. * are common to both the 915 and 965-class hardware.
  364. *
  365. * The CPU also sometimes XORs in higher bits as well, to improve
  366. * bandwidth doing strided access like we do so frequently in graphics. This
  367. * is called "Channel XOR Randomization" in the MCH documentation. The result
  368. * is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address
  369. * decode.
  370. *
  371. * All of this bit 6 XORing has an effect on our memory management,
  372. * as we need to make sure that the 3d driver can correctly address object
  373. * contents.
  374. *
  375. * If we don't have interleaved memory, all tiling is safe and no swizzling is
  376. * required.
  377. *
  378. * When bit 17 is XORed in, we simply refuse to tile at all. Bit
  379. * 17 is not just a page offset, so as we page an object out and back in,
  380. * individual pages in it will have different bit 17 addresses, resulting in
  381. * each 64 bytes being swapped with its neighbor!
  382. *
  383. * Otherwise, if interleaved, we have to tell the 3d driver what the address
  384. * swizzling it needs to do is, since it's writing with the CPU to the pages
  385. * (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the
  386. * pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling
  387. * required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order
  388. * to match what the GPU expects.
  389. */
  390. /**
  391. * i915_gem_detect_bit_6_swizzle - detect bit 6 swizzling pattern
  392. * @dev: DRM device
  393. *
  394. * Detects bit 6 swizzling of address lookup between IGD access and CPU
  395. * access through main memory.
  396. */
  397. void
  398. i915_gem_detect_bit_6_swizzle(struct drm_device *dev)
  399. {
  400. struct drm_i915_private *dev_priv = to_i915(dev);
  401. uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
  402. uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
  403. if (INTEL_INFO(dev)->gen >= 8 || IS_VALLEYVIEW(dev)) {
  404. /*
  405. * On BDW+, swizzling is not used. We leave the CPU memory
  406. * controller in charge of optimizing memory accesses without
  407. * the extra address manipulation GPU side.
  408. *
  409. * VLV and CHV don't have GPU swizzling.
  410. */
  411. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  412. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  413. } else if (INTEL_INFO(dev)->gen >= 6) {
  414. if (dev_priv->preserve_bios_swizzle) {
  415. if (I915_READ(DISP_ARB_CTL) &
  416. DISP_TILE_SURFACE_SWIZZLING) {
  417. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  418. swizzle_y = I915_BIT_6_SWIZZLE_9;
  419. } else {
  420. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  421. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  422. }
  423. } else {
  424. uint32_t dimm_c0, dimm_c1;
  425. dimm_c0 = I915_READ(MAD_DIMM_C0);
  426. dimm_c1 = I915_READ(MAD_DIMM_C1);
  427. dimm_c0 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
  428. dimm_c1 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
  429. /* Enable swizzling when the channels are populated
  430. * with identically sized dimms. We don't need to check
  431. * the 3rd channel because no cpu with gpu attached
  432. * ships in that configuration. Also, swizzling only
  433. * makes sense for 2 channels anyway. */
  434. if (dimm_c0 == dimm_c1) {
  435. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  436. swizzle_y = I915_BIT_6_SWIZZLE_9;
  437. } else {
  438. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  439. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  440. }
  441. }
  442. } else if (IS_GEN5(dev)) {
  443. /* On Ironlake whatever DRAM config, GPU always do
  444. * same swizzling setup.
  445. */
  446. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  447. swizzle_y = I915_BIT_6_SWIZZLE_9;
  448. } else if (IS_GEN2(dev)) {
  449. /* As far as we know, the 865 doesn't have these bit 6
  450. * swizzling issues.
  451. */
  452. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  453. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  454. } else if (IS_MOBILE(dev) || (IS_GEN3(dev) && !IS_G33(dev))) {
  455. uint32_t dcc;
  456. /* On 9xx chipsets, channel interleave by the CPU is
  457. * determined by DCC. For single-channel, neither the CPU
  458. * nor the GPU do swizzling. For dual channel interleaved,
  459. * the GPU's interleave is bit 9 and 10 for X tiled, and bit
  460. * 9 for Y tiled. The CPU's interleave is independent, and
  461. * can be based on either bit 11 (haven't seen this yet) or
  462. * bit 17 (common).
  463. */
  464. dcc = I915_READ(DCC);
  465. switch (dcc & DCC_ADDRESSING_MODE_MASK) {
  466. case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
  467. case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
  468. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  469. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  470. break;
  471. case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
  472. if (dcc & DCC_CHANNEL_XOR_DISABLE) {
  473. /* This is the base swizzling by the GPU for
  474. * tiled buffers.
  475. */
  476. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  477. swizzle_y = I915_BIT_6_SWIZZLE_9;
  478. } else if ((dcc & DCC_CHANNEL_XOR_BIT_17) == 0) {
  479. /* Bit 11 swizzling by the CPU in addition. */
  480. swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
  481. swizzle_y = I915_BIT_6_SWIZZLE_9_11;
  482. } else {
  483. /* Bit 17 swizzling by the CPU in addition. */
  484. swizzle_x = I915_BIT_6_SWIZZLE_9_10_17;
  485. swizzle_y = I915_BIT_6_SWIZZLE_9_17;
  486. }
  487. break;
  488. }
  489. /* check for L-shaped memory aka modified enhanced addressing */
  490. if (IS_GEN4(dev) &&
  491. !(I915_READ(DCC2) & DCC2_MODIFIED_ENHANCED_DISABLE)) {
  492. swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
  493. swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
  494. }
  495. if (dcc == 0xffffffff) {
  496. DRM_ERROR("Couldn't read from MCHBAR. "
  497. "Disabling tiling.\n");
  498. swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
  499. swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
  500. }
  501. } else {
  502. /* The 965, G33, and newer, have a very flexible memory
  503. * configuration. It will enable dual-channel mode
  504. * (interleaving) on as much memory as it can, and the GPU
  505. * will additionally sometimes enable different bit 6
  506. * swizzling for tiled objects from the CPU.
  507. *
  508. * Here's what I found on the G965:
  509. * slot fill memory size swizzling
  510. * 0A 0B 1A 1B 1-ch 2-ch
  511. * 512 0 0 0 512 0 O
  512. * 512 0 512 0 16 1008 X
  513. * 512 0 0 512 16 1008 X
  514. * 0 512 0 512 16 1008 X
  515. * 1024 1024 1024 0 2048 1024 O
  516. *
  517. * We could probably detect this based on either the DRB
  518. * matching, which was the case for the swizzling required in
  519. * the table above, or from the 1-ch value being less than
  520. * the minimum size of a rank.
  521. *
  522. * Reports indicate that the swizzling actually
  523. * varies depending upon page placement inside the
  524. * channels, i.e. we see swizzled pages where the
  525. * banks of memory are paired and unswizzled on the
  526. * uneven portion, so leave that as unknown.
  527. */
  528. if (I915_READ16(C0DRB3) == I915_READ16(C1DRB3)) {
  529. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  530. swizzle_y = I915_BIT_6_SWIZZLE_9;
  531. }
  532. }
  533. if (swizzle_x == I915_BIT_6_SWIZZLE_UNKNOWN ||
  534. swizzle_y == I915_BIT_6_SWIZZLE_UNKNOWN) {
  535. /* Userspace likes to explode if it sees unknown swizzling,
  536. * so lie. We will finish the lie when reporting through
  537. * the get-tiling-ioctl by reporting the physical swizzle
  538. * mode as unknown instead.
  539. *
  540. * As we don't strictly know what the swizzling is, it may be
  541. * bit17 dependent, and so we need to also prevent the pages
  542. * from being moved.
  543. */
  544. dev_priv->quirks |= QUIRK_PIN_SWIZZLED_PAGES;
  545. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  546. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  547. }
  548. dev_priv->mm.bit_6_swizzle_x = swizzle_x;
  549. dev_priv->mm.bit_6_swizzle_y = swizzle_y;
  550. }
  551. /*
  552. * Swap every 64 bytes of this page around, to account for it having a new
  553. * bit 17 of its physical address and therefore being interpreted differently
  554. * by the GPU.
  555. */
  556. static void
  557. i915_gem_swizzle_page(struct page *page)
  558. {
  559. char temp[64];
  560. char *vaddr;
  561. int i;
  562. vaddr = kmap(page);
  563. for (i = 0; i < PAGE_SIZE; i += 128) {
  564. memcpy(temp, &vaddr[i], 64);
  565. memcpy(&vaddr[i], &vaddr[i + 64], 64);
  566. memcpy(&vaddr[i + 64], temp, 64);
  567. }
  568. kunmap(page);
  569. }
  570. /**
  571. * i915_gem_object_do_bit_17_swizzle - fixup bit 17 swizzling
  572. * @obj: i915 GEM buffer object
  573. *
  574. * This function fixes up the swizzling in case any page frame number for this
  575. * object has changed in bit 17 since that state has been saved with
  576. * i915_gem_object_save_bit_17_swizzle().
  577. *
  578. * This is called when pinning backing storage again, since the kernel is free
  579. * to move unpinned backing storage around (either by directly moving pages or
  580. * by swapping them out and back in again).
  581. */
  582. void
  583. i915_gem_object_do_bit_17_swizzle(struct drm_i915_gem_object *obj)
  584. {
  585. struct sgt_iter sgt_iter;
  586. struct page *page;
  587. int i;
  588. if (obj->bit_17 == NULL)
  589. return;
  590. i = 0;
  591. for_each_sgt_page(page, sgt_iter, obj->pages) {
  592. char new_bit_17 = page_to_phys(page) >> 17;
  593. if ((new_bit_17 & 0x1) !=
  594. (test_bit(i, obj->bit_17) != 0)) {
  595. i915_gem_swizzle_page(page);
  596. set_page_dirty(page);
  597. }
  598. i++;
  599. }
  600. }
  601. /**
  602. * i915_gem_object_save_bit_17_swizzle - save bit 17 swizzling
  603. * @obj: i915 GEM buffer object
  604. *
  605. * This function saves the bit 17 of each page frame number so that swizzling
  606. * can be fixed up later on with i915_gem_object_do_bit_17_swizzle(). This must
  607. * be called before the backing storage can be unpinned.
  608. */
  609. void
  610. i915_gem_object_save_bit_17_swizzle(struct drm_i915_gem_object *obj)
  611. {
  612. struct sgt_iter sgt_iter;
  613. struct page *page;
  614. int page_count = obj->base.size >> PAGE_SHIFT;
  615. int i;
  616. if (obj->bit_17 == NULL) {
  617. obj->bit_17 = kcalloc(BITS_TO_LONGS(page_count),
  618. sizeof(long), GFP_KERNEL);
  619. if (obj->bit_17 == NULL) {
  620. DRM_ERROR("Failed to allocate memory for bit 17 "
  621. "record\n");
  622. return;
  623. }
  624. }
  625. i = 0;
  626. for_each_sgt_page(page, sgt_iter, obj->pages) {
  627. if (page_to_phys(page) & (1 << 17))
  628. __set_bit(i, obj->bit_17);
  629. else
  630. __clear_bit(i, obj->bit_17);
  631. i++;
  632. }
  633. }