i915_gem_fence.c 25 KB

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