i915_gem_fence.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787
  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 interract with frontbuffer
  36. * conmpression. 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 unecessary stalls on gen2/3. Therefore code must
  48. * explictly 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. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  56. struct drm_i915_gem_object *obj)
  57. {
  58. struct drm_i915_private *dev_priv = dev->dev_private;
  59. int fence_reg;
  60. int fence_pitch_shift;
  61. if (INTEL_INFO(dev)->gen >= 6) {
  62. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  63. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  64. } else {
  65. fence_reg = FENCE_REG_965_0;
  66. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  67. }
  68. fence_reg += reg * 8;
  69. /* To w/a incoherency with non-atomic 64-bit register updates,
  70. * we split the 64-bit update into two 32-bit writes. In order
  71. * for a partial fence not to be evaluated between writes, we
  72. * precede the update with write to turn off the fence register,
  73. * and only enable the fence as the last step.
  74. *
  75. * For extra levels of paranoia, we make sure each step lands
  76. * before applying the next step.
  77. */
  78. I915_WRITE(fence_reg, 0);
  79. POSTING_READ(fence_reg);
  80. if (obj) {
  81. u32 size = i915_gem_obj_ggtt_size(obj);
  82. uint64_t val;
  83. /* Adjust fence size to match tiled area */
  84. if (obj->tiling_mode != I915_TILING_NONE) {
  85. uint32_t row_size = obj->stride *
  86. (obj->tiling_mode == I915_TILING_Y ? 32 : 8);
  87. size = (size / row_size) * row_size;
  88. }
  89. val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
  90. 0xfffff000) << 32;
  91. val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
  92. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  93. if (obj->tiling_mode == I915_TILING_Y)
  94. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  95. val |= I965_FENCE_REG_VALID;
  96. I915_WRITE(fence_reg + 4, val >> 32);
  97. POSTING_READ(fence_reg + 4);
  98. I915_WRITE(fence_reg + 0, val);
  99. POSTING_READ(fence_reg);
  100. } else {
  101. I915_WRITE(fence_reg + 4, 0);
  102. POSTING_READ(fence_reg + 4);
  103. }
  104. }
  105. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  106. struct drm_i915_gem_object *obj)
  107. {
  108. struct drm_i915_private *dev_priv = dev->dev_private;
  109. u32 val;
  110. if (obj) {
  111. u32 size = i915_gem_obj_ggtt_size(obj);
  112. int pitch_val;
  113. int tile_width;
  114. WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
  115. (size & -size) != size ||
  116. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  117. "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  118. i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
  119. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  120. tile_width = 128;
  121. else
  122. tile_width = 512;
  123. /* Note: pitch better be a power of two tile widths */
  124. pitch_val = obj->stride / tile_width;
  125. pitch_val = ffs(pitch_val) - 1;
  126. val = i915_gem_obj_ggtt_offset(obj);
  127. if (obj->tiling_mode == I915_TILING_Y)
  128. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  129. val |= I915_FENCE_SIZE_BITS(size);
  130. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  131. val |= I830_FENCE_REG_VALID;
  132. } else
  133. val = 0;
  134. if (reg < 8)
  135. reg = FENCE_REG_830_0 + reg * 4;
  136. else
  137. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  138. I915_WRITE(reg, val);
  139. POSTING_READ(reg);
  140. }
  141. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  142. struct drm_i915_gem_object *obj)
  143. {
  144. struct drm_i915_private *dev_priv = dev->dev_private;
  145. uint32_t val;
  146. if (obj) {
  147. u32 size = i915_gem_obj_ggtt_size(obj);
  148. uint32_t pitch_val;
  149. WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
  150. (size & -size) != size ||
  151. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  152. "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
  153. i915_gem_obj_ggtt_offset(obj), size);
  154. pitch_val = obj->stride / 128;
  155. pitch_val = ffs(pitch_val) - 1;
  156. val = i915_gem_obj_ggtt_offset(obj);
  157. if (obj->tiling_mode == I915_TILING_Y)
  158. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  159. val |= I830_FENCE_SIZE_BITS(size);
  160. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  161. val |= I830_FENCE_REG_VALID;
  162. } else
  163. val = 0;
  164. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  165. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  166. }
  167. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  168. {
  169. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  170. }
  171. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  172. struct drm_i915_gem_object *obj)
  173. {
  174. struct drm_i915_private *dev_priv = dev->dev_private;
  175. /* Ensure that all CPU reads are completed before installing a fence
  176. * and all writes before removing the fence.
  177. */
  178. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  179. mb();
  180. WARN(obj && (!obj->stride || !obj->tiling_mode),
  181. "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
  182. obj->stride, obj->tiling_mode);
  183. if (IS_GEN2(dev))
  184. i830_write_fence_reg(dev, reg, obj);
  185. else if (IS_GEN3(dev))
  186. i915_write_fence_reg(dev, reg, obj);
  187. else if (INTEL_INFO(dev)->gen >= 4)
  188. i965_write_fence_reg(dev, reg, obj);
  189. /* And similarly be paranoid that no direct access to this region
  190. * is reordered to before the fence is installed.
  191. */
  192. if (i915_gem_object_needs_mb(obj))
  193. mb();
  194. }
  195. static inline int fence_number(struct drm_i915_private *dev_priv,
  196. struct drm_i915_fence_reg *fence)
  197. {
  198. return fence - dev_priv->fence_regs;
  199. }
  200. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  201. struct drm_i915_fence_reg *fence,
  202. bool enable)
  203. {
  204. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  205. int reg = fence_number(dev_priv, fence);
  206. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  207. if (enable) {
  208. obj->fence_reg = reg;
  209. fence->obj = obj;
  210. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  211. } else {
  212. obj->fence_reg = I915_FENCE_REG_NONE;
  213. fence->obj = NULL;
  214. list_del_init(&fence->lru_list);
  215. }
  216. obj->fence_dirty = false;
  217. }
  218. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  219. {
  220. if (obj->tiling_mode)
  221. i915_gem_release_mmap(obj);
  222. /* As we do not have an associated fence register, we will force
  223. * a tiling change if we ever need to acquire one.
  224. */
  225. obj->fence_dirty = false;
  226. obj->fence_reg = I915_FENCE_REG_NONE;
  227. }
  228. static int
  229. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  230. {
  231. if (obj->last_fenced_req) {
  232. int ret = i915_wait_request(obj->last_fenced_req);
  233. if (ret)
  234. return ret;
  235. i915_gem_request_assign(&obj->last_fenced_req, NULL);
  236. }
  237. return 0;
  238. }
  239. /**
  240. * i915_gem_object_put_fence - force-remove fence for an object
  241. * @obj: object to map through a fence reg
  242. *
  243. * This function force-removes any fence from the given object, which is useful
  244. * if the kernel wants to do untiled GTT access.
  245. *
  246. * Returns:
  247. *
  248. * 0 on success, negative error code on failure.
  249. */
  250. int
  251. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  252. {
  253. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  254. struct drm_i915_fence_reg *fence;
  255. int ret;
  256. ret = i915_gem_object_wait_fence(obj);
  257. if (ret)
  258. return ret;
  259. if (obj->fence_reg == I915_FENCE_REG_NONE)
  260. return 0;
  261. fence = &dev_priv->fence_regs[obj->fence_reg];
  262. if (WARN_ON(fence->pin_count))
  263. return -EBUSY;
  264. i915_gem_object_fence_lost(obj);
  265. i915_gem_object_update_fence(obj, fence, false);
  266. return 0;
  267. }
  268. static struct drm_i915_fence_reg *
  269. i915_find_fence_reg(struct drm_device *dev)
  270. {
  271. struct drm_i915_private *dev_priv = dev->dev_private;
  272. struct drm_i915_fence_reg *reg, *avail;
  273. int i;
  274. /* First try to find a free reg */
  275. avail = NULL;
  276. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  277. reg = &dev_priv->fence_regs[i];
  278. if (!reg->obj)
  279. return reg;
  280. if (!reg->pin_count)
  281. avail = reg;
  282. }
  283. if (avail == NULL)
  284. goto deadlock;
  285. /* None available, try to steal one or wait for a user to finish */
  286. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  287. if (reg->pin_count)
  288. continue;
  289. return reg;
  290. }
  291. deadlock:
  292. /* Wait for completion of pending flips which consume fences */
  293. if (intel_has_pending_fb_unpin(dev))
  294. return ERR_PTR(-EAGAIN);
  295. return ERR_PTR(-EDEADLK);
  296. }
  297. /**
  298. * i915_gem_object_get_fence - set up fencing for an object
  299. * @obj: object to map through a fence reg
  300. *
  301. * When mapping objects through the GTT, userspace wants to be able to write
  302. * to them without having to worry about swizzling if the object is tiled.
  303. * This function walks the fence regs looking for a free one for @obj,
  304. * stealing one if it can't find any.
  305. *
  306. * It then sets up the reg based on the object's properties: address, pitch
  307. * and tiling format.
  308. *
  309. * For an untiled surface, this removes any existing fence.
  310. *
  311. * Returns:
  312. *
  313. * 0 on success, negative error code on failure.
  314. */
  315. int
  316. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  317. {
  318. struct drm_device *dev = obj->base.dev;
  319. struct drm_i915_private *dev_priv = dev->dev_private;
  320. bool enable = obj->tiling_mode != I915_TILING_NONE;
  321. struct drm_i915_fence_reg *reg;
  322. int ret;
  323. /* Have we updated the tiling parameters upon the object and so
  324. * will need to serialise the write to the associated fence register?
  325. */
  326. if (obj->fence_dirty) {
  327. ret = i915_gem_object_wait_fence(obj);
  328. if (ret)
  329. return ret;
  330. }
  331. /* Just update our place in the LRU if our fence is getting reused. */
  332. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  333. reg = &dev_priv->fence_regs[obj->fence_reg];
  334. if (!obj->fence_dirty) {
  335. list_move_tail(&reg->lru_list,
  336. &dev_priv->mm.fence_list);
  337. return 0;
  338. }
  339. } else if (enable) {
  340. if (WARN_ON(!obj->map_and_fenceable))
  341. return -EINVAL;
  342. reg = i915_find_fence_reg(dev);
  343. if (IS_ERR(reg))
  344. return PTR_ERR(reg);
  345. if (reg->obj) {
  346. struct drm_i915_gem_object *old = reg->obj;
  347. ret = i915_gem_object_wait_fence(old);
  348. if (ret)
  349. return ret;
  350. i915_gem_object_fence_lost(old);
  351. }
  352. } else
  353. return 0;
  354. i915_gem_object_update_fence(obj, reg, enable);
  355. return 0;
  356. }
  357. /**
  358. * i915_gem_object_pin_fence - pin fencing state
  359. * @obj: object to pin fencing for
  360. *
  361. * This pins the fencing state (whether tiled or untiled) to make sure the
  362. * object is ready to be used as a scanout target. Fencing status must be
  363. * synchronize first by calling i915_gem_object_get_fence():
  364. *
  365. * The resulting fence pin reference must be released again with
  366. * i915_gem_object_unpin_fence().
  367. *
  368. * Returns:
  369. *
  370. * True if the object has a fence, false otherwise.
  371. */
  372. bool
  373. i915_gem_object_pin_fence(struct drm_i915_gem_object *obj)
  374. {
  375. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  376. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  377. struct i915_vma *ggtt_vma = i915_gem_obj_to_ggtt(obj);
  378. WARN_ON(!ggtt_vma ||
  379. dev_priv->fence_regs[obj->fence_reg].pin_count >
  380. ggtt_vma->pin_count);
  381. dev_priv->fence_regs[obj->fence_reg].pin_count++;
  382. return true;
  383. } else
  384. return false;
  385. }
  386. /**
  387. * i915_gem_object_unpin_fence - unpin fencing state
  388. * @obj: object to unpin fencing for
  389. *
  390. * This releases the fence pin reference acquired through
  391. * i915_gem_object_pin_fence. It will handle both objects with and without an
  392. * attached fence correctly, callers do not need to distinguish this.
  393. */
  394. void
  395. i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
  396. {
  397. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  398. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  399. WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
  400. dev_priv->fence_regs[obj->fence_reg].pin_count--;
  401. }
  402. }
  403. /**
  404. * i915_gem_restore_fences - restore fence state
  405. * @dev: DRM device
  406. *
  407. * Restore the hw fence state to match the software tracking again, to be called
  408. * after a gpu reset and on resume.
  409. */
  410. void i915_gem_restore_fences(struct drm_device *dev)
  411. {
  412. struct drm_i915_private *dev_priv = dev->dev_private;
  413. int i;
  414. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  415. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  416. /*
  417. * Commit delayed tiling changes if we have an object still
  418. * attached to the fence, otherwise just clear the fence.
  419. */
  420. if (reg->obj) {
  421. i915_gem_object_update_fence(reg->obj, reg,
  422. reg->obj->tiling_mode);
  423. } else {
  424. i915_gem_write_fence(dev, i, NULL);
  425. }
  426. }
  427. }
  428. /**
  429. * DOC: tiling swizzling details
  430. *
  431. * The idea behind tiling is to increase cache hit rates by rearranging
  432. * pixel data so that a group of pixel accesses are in the same cacheline.
  433. * Performance improvement from doing this on the back/depth buffer are on
  434. * the order of 30%.
  435. *
  436. * Intel architectures make this somewhat more complicated, though, by
  437. * adjustments made to addressing of data when the memory is in interleaved
  438. * mode (matched pairs of DIMMS) to improve memory bandwidth.
  439. * For interleaved memory, the CPU sends every sequential 64 bytes
  440. * to an alternate memory channel so it can get the bandwidth from both.
  441. *
  442. * The GPU also rearranges its accesses for increased bandwidth to interleaved
  443. * memory, and it matches what the CPU does for non-tiled. However, when tiled
  444. * it does it a little differently, since one walks addresses not just in the
  445. * X direction but also Y. So, along with alternating channels when bit
  446. * 6 of the address flips, it also alternates when other bits flip -- Bits 9
  447. * (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)
  448. * are common to both the 915 and 965-class hardware.
  449. *
  450. * The CPU also sometimes XORs in higher bits as well, to improve
  451. * bandwidth doing strided access like we do so frequently in graphics. This
  452. * is called "Channel XOR Randomization" in the MCH documentation. The result
  453. * is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address
  454. * decode.
  455. *
  456. * All of this bit 6 XORing has an effect on our memory management,
  457. * as we need to make sure that the 3d driver can correctly address object
  458. * contents.
  459. *
  460. * If we don't have interleaved memory, all tiling is safe and no swizzling is
  461. * required.
  462. *
  463. * When bit 17 is XORed in, we simply refuse to tile at all. Bit
  464. * 17 is not just a page offset, so as we page an objet out and back in,
  465. * individual pages in it will have different bit 17 addresses, resulting in
  466. * each 64 bytes being swapped with its neighbor!
  467. *
  468. * Otherwise, if interleaved, we have to tell the 3d driver what the address
  469. * swizzling it needs to do is, since it's writing with the CPU to the pages
  470. * (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the
  471. * pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling
  472. * required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order
  473. * to match what the GPU expects.
  474. */
  475. /**
  476. * i915_gem_detect_bit_6_swizzle - detect bit 6 swizzling pattern
  477. * @dev: DRM device
  478. *
  479. * Detects bit 6 swizzling of address lookup between IGD access and CPU
  480. * access through main memory.
  481. */
  482. void
  483. i915_gem_detect_bit_6_swizzle(struct drm_device *dev)
  484. {
  485. struct drm_i915_private *dev_priv = dev->dev_private;
  486. uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
  487. uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
  488. if (INTEL_INFO(dev)->gen >= 8 || IS_VALLEYVIEW(dev)) {
  489. /*
  490. * On BDW+, swizzling is not used. We leave the CPU memory
  491. * controller in charge of optimizing memory accesses without
  492. * the extra address manipulation GPU side.
  493. *
  494. * VLV and CHV don't have GPU swizzling.
  495. */
  496. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  497. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  498. } else if (INTEL_INFO(dev)->gen >= 6) {
  499. if (dev_priv->preserve_bios_swizzle) {
  500. if (I915_READ(DISP_ARB_CTL) &
  501. DISP_TILE_SURFACE_SWIZZLING) {
  502. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  503. swizzle_y = I915_BIT_6_SWIZZLE_9;
  504. } else {
  505. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  506. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  507. }
  508. } else {
  509. uint32_t dimm_c0, dimm_c1;
  510. dimm_c0 = I915_READ(MAD_DIMM_C0);
  511. dimm_c1 = I915_READ(MAD_DIMM_C1);
  512. dimm_c0 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
  513. dimm_c1 &= MAD_DIMM_A_SIZE_MASK | MAD_DIMM_B_SIZE_MASK;
  514. /* Enable swizzling when the channels are populated
  515. * with identically sized dimms. We don't need to check
  516. * the 3rd channel because no cpu with gpu attached
  517. * ships in that configuration. Also, swizzling only
  518. * makes sense for 2 channels anyway. */
  519. if (dimm_c0 == dimm_c1) {
  520. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  521. swizzle_y = I915_BIT_6_SWIZZLE_9;
  522. } else {
  523. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  524. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  525. }
  526. }
  527. } else if (IS_GEN5(dev)) {
  528. /* On Ironlake whatever DRAM config, GPU always do
  529. * same swizzling setup.
  530. */
  531. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  532. swizzle_y = I915_BIT_6_SWIZZLE_9;
  533. } else if (IS_GEN2(dev)) {
  534. /* As far as we know, the 865 doesn't have these bit 6
  535. * swizzling issues.
  536. */
  537. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  538. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  539. } else if (IS_MOBILE(dev) || (IS_GEN3(dev) && !IS_G33(dev))) {
  540. uint32_t dcc;
  541. /* On 9xx chipsets, channel interleave by the CPU is
  542. * determined by DCC. For single-channel, neither the CPU
  543. * nor the GPU do swizzling. For dual channel interleaved,
  544. * the GPU's interleave is bit 9 and 10 for X tiled, and bit
  545. * 9 for Y tiled. The CPU's interleave is independent, and
  546. * can be based on either bit 11 (haven't seen this yet) or
  547. * bit 17 (common).
  548. */
  549. dcc = I915_READ(DCC);
  550. switch (dcc & DCC_ADDRESSING_MODE_MASK) {
  551. case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
  552. case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
  553. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  554. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  555. break;
  556. case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
  557. if (dcc & DCC_CHANNEL_XOR_DISABLE) {
  558. /* This is the base swizzling by the GPU for
  559. * tiled buffers.
  560. */
  561. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  562. swizzle_y = I915_BIT_6_SWIZZLE_9;
  563. } else if ((dcc & DCC_CHANNEL_XOR_BIT_17) == 0) {
  564. /* Bit 11 swizzling by the CPU in addition. */
  565. swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
  566. swizzle_y = I915_BIT_6_SWIZZLE_9_11;
  567. } else {
  568. /* Bit 17 swizzling by the CPU in addition. */
  569. swizzle_x = I915_BIT_6_SWIZZLE_9_10_17;
  570. swizzle_y = I915_BIT_6_SWIZZLE_9_17;
  571. }
  572. break;
  573. }
  574. /* check for L-shaped memory aka modified enhanced addressing */
  575. if (IS_GEN4(dev)) {
  576. uint32_t ddc2 = I915_READ(DCC2);
  577. if (!(ddc2 & DCC2_MODIFIED_ENHANCED_DISABLE))
  578. dev_priv->quirks |= QUIRK_PIN_SWIZZLED_PAGES;
  579. }
  580. if (dcc == 0xffffffff) {
  581. DRM_ERROR("Couldn't read from MCHBAR. "
  582. "Disabling tiling.\n");
  583. swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
  584. swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
  585. }
  586. } else {
  587. /* The 965, G33, and newer, have a very flexible memory
  588. * configuration. It will enable dual-channel mode
  589. * (interleaving) on as much memory as it can, and the GPU
  590. * will additionally sometimes enable different bit 6
  591. * swizzling for tiled objects from the CPU.
  592. *
  593. * Here's what I found on the G965:
  594. * slot fill memory size swizzling
  595. * 0A 0B 1A 1B 1-ch 2-ch
  596. * 512 0 0 0 512 0 O
  597. * 512 0 512 0 16 1008 X
  598. * 512 0 0 512 16 1008 X
  599. * 0 512 0 512 16 1008 X
  600. * 1024 1024 1024 0 2048 1024 O
  601. *
  602. * We could probably detect this based on either the DRB
  603. * matching, which was the case for the swizzling required in
  604. * the table above, or from the 1-ch value being less than
  605. * the minimum size of a rank.
  606. */
  607. if (I915_READ16(C0DRB3) != I915_READ16(C1DRB3)) {
  608. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  609. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  610. } else {
  611. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  612. swizzle_y = I915_BIT_6_SWIZZLE_9;
  613. }
  614. }
  615. dev_priv->mm.bit_6_swizzle_x = swizzle_x;
  616. dev_priv->mm.bit_6_swizzle_y = swizzle_y;
  617. }
  618. /*
  619. * Swap every 64 bytes of this page around, to account for it having a new
  620. * bit 17 of its physical address and therefore being interpreted differently
  621. * by the GPU.
  622. */
  623. static void
  624. i915_gem_swizzle_page(struct page *page)
  625. {
  626. char temp[64];
  627. char *vaddr;
  628. int i;
  629. vaddr = kmap(page);
  630. for (i = 0; i < PAGE_SIZE; i += 128) {
  631. memcpy(temp, &vaddr[i], 64);
  632. memcpy(&vaddr[i], &vaddr[i + 64], 64);
  633. memcpy(&vaddr[i + 64], temp, 64);
  634. }
  635. kunmap(page);
  636. }
  637. /**
  638. * i915_gem_object_do_bit_17_swizzle - fixup bit 17 swizzling
  639. * @obj: i915 GEM buffer object
  640. *
  641. * This function fixes up the swizzling in case any page frame number for this
  642. * object has changed in bit 17 since that state has been saved with
  643. * i915_gem_object_save_bit_17_swizzle().
  644. *
  645. * This is called when pinning backing storage again, since the kernel is free
  646. * to move unpinned backing storage around (either by directly moving pages or
  647. * by swapping them out and back in again).
  648. */
  649. void
  650. i915_gem_object_do_bit_17_swizzle(struct drm_i915_gem_object *obj)
  651. {
  652. struct sg_page_iter sg_iter;
  653. int i;
  654. if (obj->bit_17 == NULL)
  655. return;
  656. i = 0;
  657. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  658. struct page *page = sg_page_iter_page(&sg_iter);
  659. char new_bit_17 = page_to_phys(page) >> 17;
  660. if ((new_bit_17 & 0x1) !=
  661. (test_bit(i, obj->bit_17) != 0)) {
  662. i915_gem_swizzle_page(page);
  663. set_page_dirty(page);
  664. }
  665. i++;
  666. }
  667. }
  668. /**
  669. * i915_gem_object_save_bit_17_swizzle - save bit 17 swizzling
  670. * @obj: i915 GEM buffer object
  671. *
  672. * This function saves the bit 17 of each page frame number so that swizzling
  673. * can be fixed up later on with i915_gem_object_do_bit_17_swizzle(). This must
  674. * be called before the backing storage can be unpinned.
  675. */
  676. void
  677. i915_gem_object_save_bit_17_swizzle(struct drm_i915_gem_object *obj)
  678. {
  679. struct sg_page_iter sg_iter;
  680. int page_count = obj->base.size >> PAGE_SHIFT;
  681. int i;
  682. if (obj->bit_17 == NULL) {
  683. obj->bit_17 = kcalloc(BITS_TO_LONGS(page_count),
  684. sizeof(long), GFP_KERNEL);
  685. if (obj->bit_17 == NULL) {
  686. DRM_ERROR("Failed to allocate memory for bit 17 "
  687. "record\n");
  688. return;
  689. }
  690. }
  691. i = 0;
  692. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  693. if (page_to_phys(sg_page_iter_page(&sg_iter)) & (1 << 17))
  694. __set_bit(i, obj->bit_17);
  695. else
  696. __clear_bit(i, obj->bit_17);
  697. i++;
  698. }
  699. }