i915_gem_tiling.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. /*
  2. * Copyright © 2008 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. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <linux/acpi.h>
  28. #include <linux/pnp.h>
  29. #include "linux/string.h"
  30. #include "linux/bitops.h"
  31. #include "drmP.h"
  32. #include "drm.h"
  33. #include "i915_drm.h"
  34. #include "i915_drv.h"
  35. /** @file i915_gem_tiling.c
  36. *
  37. * Support for managing tiling state of buffer objects.
  38. *
  39. * The idea behind tiling is to increase cache hit rates by rearranging
  40. * pixel data so that a group of pixel accesses are in the same cacheline.
  41. * Performance improvement from doing this on the back/depth buffer are on
  42. * the order of 30%.
  43. *
  44. * Intel architectures make this somewhat more complicated, though, by
  45. * adjustments made to addressing of data when the memory is in interleaved
  46. * mode (matched pairs of DIMMS) to improve memory bandwidth.
  47. * For interleaved memory, the CPU sends every sequential 64 bytes
  48. * to an alternate memory channel so it can get the bandwidth from both.
  49. *
  50. * The GPU also rearranges its accesses for increased bandwidth to interleaved
  51. * memory, and it matches what the CPU does for non-tiled. However, when tiled
  52. * it does it a little differently, since one walks addresses not just in the
  53. * X direction but also Y. So, along with alternating channels when bit
  54. * 6 of the address flips, it also alternates when other bits flip -- Bits 9
  55. * (every 512 bytes, an X tile scanline) and 10 (every two X tile scanlines)
  56. * are common to both the 915 and 965-class hardware.
  57. *
  58. * The CPU also sometimes XORs in higher bits as well, to improve
  59. * bandwidth doing strided access like we do so frequently in graphics. This
  60. * is called "Channel XOR Randomization" in the MCH documentation. The result
  61. * is that the CPU is XORing in either bit 11 or bit 17 to bit 6 of its address
  62. * decode.
  63. *
  64. * All of this bit 6 XORing has an effect on our memory management,
  65. * as we need to make sure that the 3d driver can correctly address object
  66. * contents.
  67. *
  68. * If we don't have interleaved memory, all tiling is safe and no swizzling is
  69. * required.
  70. *
  71. * When bit 17 is XORed in, we simply refuse to tile at all. Bit
  72. * 17 is not just a page offset, so as we page an objet out and back in,
  73. * individual pages in it will have different bit 17 addresses, resulting in
  74. * each 64 bytes being swapped with its neighbor!
  75. *
  76. * Otherwise, if interleaved, we have to tell the 3d driver what the address
  77. * swizzling it needs to do is, since it's writing with the CPU to the pages
  78. * (bit 6 and potentially bit 11 XORed in), and the GPU is reading from the
  79. * pages (bit 6, 9, and 10 XORed in), resulting in a cumulative bit swizzling
  80. * required by the CPU of XORing in bit 6, 9, 10, and potentially 11, in order
  81. * to match what the GPU expects.
  82. */
  83. #define MCHBAR_I915 0x44
  84. #define MCHBAR_I965 0x48
  85. #define MCHBAR_SIZE (4*4096)
  86. #define DEVEN_REG 0x54
  87. #define DEVEN_MCHBAR_EN (1 << 28)
  88. /* Allocate space for the MCH regs if needed, return nonzero on error */
  89. static int
  90. intel_alloc_mchbar_resource(struct drm_device *dev)
  91. {
  92. drm_i915_private_t *dev_priv = dev->dev_private;
  93. int reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
  94. u32 temp_lo, temp_hi = 0;
  95. u64 mchbar_addr;
  96. int ret = 0;
  97. if (IS_I965G(dev))
  98. pci_read_config_dword(dev_priv->bridge_dev, reg + 4, &temp_hi);
  99. pci_read_config_dword(dev_priv->bridge_dev, reg, &temp_lo);
  100. mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
  101. /* If ACPI doesn't have it, assume we need to allocate it ourselves */
  102. #ifdef CONFIG_PNP
  103. if (mchbar_addr &&
  104. pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE)) {
  105. ret = 0;
  106. goto out;
  107. }
  108. #endif
  109. /* Get some space for it */
  110. ret = pci_bus_alloc_resource(dev_priv->bridge_dev->bus, &dev_priv->mch_res,
  111. MCHBAR_SIZE, MCHBAR_SIZE,
  112. PCIBIOS_MIN_MEM,
  113. 0, pcibios_align_resource,
  114. dev_priv->bridge_dev);
  115. if (ret) {
  116. DRM_DEBUG_DRIVER("failed bus alloc: %d\n", ret);
  117. dev_priv->mch_res.start = 0;
  118. goto out;
  119. }
  120. if (IS_I965G(dev))
  121. pci_write_config_dword(dev_priv->bridge_dev, reg + 4,
  122. upper_32_bits(dev_priv->mch_res.start));
  123. pci_write_config_dword(dev_priv->bridge_dev, reg,
  124. lower_32_bits(dev_priv->mch_res.start));
  125. out:
  126. return ret;
  127. }
  128. /* Setup MCHBAR if possible, return true if we should disable it again */
  129. static bool
  130. intel_setup_mchbar(struct drm_device *dev)
  131. {
  132. drm_i915_private_t *dev_priv = dev->dev_private;
  133. int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
  134. u32 temp;
  135. bool need_disable = false, enabled;
  136. if (IS_I915G(dev) || IS_I915GM(dev)) {
  137. pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
  138. enabled = !!(temp & DEVEN_MCHBAR_EN);
  139. } else {
  140. pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
  141. enabled = temp & 1;
  142. }
  143. /* If it's already enabled, don't have to do anything */
  144. if (enabled)
  145. goto out;
  146. if (intel_alloc_mchbar_resource(dev))
  147. goto out;
  148. need_disable = true;
  149. /* Space is allocated or reserved, so enable it. */
  150. if (IS_I915G(dev) || IS_I915GM(dev)) {
  151. pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG,
  152. temp | DEVEN_MCHBAR_EN);
  153. } else {
  154. pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
  155. pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp | 1);
  156. }
  157. out:
  158. return need_disable;
  159. }
  160. static void
  161. intel_teardown_mchbar(struct drm_device *dev, bool disable)
  162. {
  163. drm_i915_private_t *dev_priv = dev->dev_private;
  164. int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
  165. u32 temp;
  166. if (disable) {
  167. if (IS_I915G(dev) || IS_I915GM(dev)) {
  168. pci_read_config_dword(dev_priv->bridge_dev, DEVEN_REG, &temp);
  169. temp &= ~DEVEN_MCHBAR_EN;
  170. pci_write_config_dword(dev_priv->bridge_dev, DEVEN_REG, temp);
  171. } else {
  172. pci_read_config_dword(dev_priv->bridge_dev, mchbar_reg, &temp);
  173. temp &= ~1;
  174. pci_write_config_dword(dev_priv->bridge_dev, mchbar_reg, temp);
  175. }
  176. }
  177. if (dev_priv->mch_res.start)
  178. release_resource(&dev_priv->mch_res);
  179. }
  180. /**
  181. * Detects bit 6 swizzling of address lookup between IGD access and CPU
  182. * access through main memory.
  183. */
  184. void
  185. i915_gem_detect_bit_6_swizzle(struct drm_device *dev)
  186. {
  187. drm_i915_private_t *dev_priv = dev->dev_private;
  188. uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
  189. uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
  190. bool need_disable;
  191. if (IS_IRONLAKE(dev)) {
  192. /* On Ironlake whatever DRAM config, GPU always do
  193. * same swizzling setup.
  194. */
  195. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  196. swizzle_y = I915_BIT_6_SWIZZLE_9;
  197. } else if (!IS_I9XX(dev)) {
  198. /* As far as we know, the 865 doesn't have these bit 6
  199. * swizzling issues.
  200. */
  201. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  202. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  203. } else if (IS_MOBILE(dev)) {
  204. uint32_t dcc;
  205. /* Try to make sure MCHBAR is enabled before poking at it */
  206. need_disable = intel_setup_mchbar(dev);
  207. /* On mobile 9xx chipsets, channel interleave by the CPU is
  208. * determined by DCC. For single-channel, neither the CPU
  209. * nor the GPU do swizzling. For dual channel interleaved,
  210. * the GPU's interleave is bit 9 and 10 for X tiled, and bit
  211. * 9 for Y tiled. The CPU's interleave is independent, and
  212. * can be based on either bit 11 (haven't seen this yet) or
  213. * bit 17 (common).
  214. */
  215. dcc = I915_READ(DCC);
  216. switch (dcc & DCC_ADDRESSING_MODE_MASK) {
  217. case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
  218. case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
  219. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  220. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  221. break;
  222. case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
  223. if (dcc & DCC_CHANNEL_XOR_DISABLE) {
  224. /* This is the base swizzling by the GPU for
  225. * tiled buffers.
  226. */
  227. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  228. swizzle_y = I915_BIT_6_SWIZZLE_9;
  229. } else if ((dcc & DCC_CHANNEL_XOR_BIT_17) == 0) {
  230. /* Bit 11 swizzling by the CPU in addition. */
  231. swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
  232. swizzle_y = I915_BIT_6_SWIZZLE_9_11;
  233. } else {
  234. /* Bit 17 swizzling by the CPU in addition. */
  235. swizzle_x = I915_BIT_6_SWIZZLE_9_10_17;
  236. swizzle_y = I915_BIT_6_SWIZZLE_9_17;
  237. }
  238. break;
  239. }
  240. if (dcc == 0xffffffff) {
  241. DRM_ERROR("Couldn't read from MCHBAR. "
  242. "Disabling tiling.\n");
  243. swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
  244. swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
  245. }
  246. intel_teardown_mchbar(dev, need_disable);
  247. } else {
  248. /* The 965, G33, and newer, have a very flexible memory
  249. * configuration. It will enable dual-channel mode
  250. * (interleaving) on as much memory as it can, and the GPU
  251. * will additionally sometimes enable different bit 6
  252. * swizzling for tiled objects from the CPU.
  253. *
  254. * Here's what I found on the G965:
  255. * slot fill memory size swizzling
  256. * 0A 0B 1A 1B 1-ch 2-ch
  257. * 512 0 0 0 512 0 O
  258. * 512 0 512 0 16 1008 X
  259. * 512 0 0 512 16 1008 X
  260. * 0 512 0 512 16 1008 X
  261. * 1024 1024 1024 0 2048 1024 O
  262. *
  263. * We could probably detect this based on either the DRB
  264. * matching, which was the case for the swizzling required in
  265. * the table above, or from the 1-ch value being less than
  266. * the minimum size of a rank.
  267. */
  268. if (I915_READ16(C0DRB3) != I915_READ16(C1DRB3)) {
  269. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  270. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  271. } else {
  272. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  273. swizzle_y = I915_BIT_6_SWIZZLE_9;
  274. }
  275. }
  276. dev_priv->mm.bit_6_swizzle_x = swizzle_x;
  277. dev_priv->mm.bit_6_swizzle_y = swizzle_y;
  278. }
  279. /**
  280. * Returns the size of the fence for a tiled object of the given size.
  281. */
  282. static int
  283. i915_get_fence_size(struct drm_device *dev, int size)
  284. {
  285. int i;
  286. int start;
  287. if (IS_I965G(dev)) {
  288. /* The 965 can have fences at any page boundary. */
  289. return ALIGN(size, 4096);
  290. } else {
  291. /* Align the size to a power of two greater than the smallest
  292. * fence size.
  293. */
  294. if (IS_I9XX(dev))
  295. start = 1024 * 1024;
  296. else
  297. start = 512 * 1024;
  298. for (i = start; i < size; i <<= 1)
  299. ;
  300. return i;
  301. }
  302. }
  303. /* Check pitch constriants for all chips & tiling formats */
  304. static bool
  305. i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
  306. {
  307. int tile_width;
  308. /* Linear is always fine */
  309. if (tiling_mode == I915_TILING_NONE)
  310. return true;
  311. if (!IS_I9XX(dev) ||
  312. (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
  313. tile_width = 128;
  314. else
  315. tile_width = 512;
  316. /* check maximum stride & object size */
  317. if (IS_I965G(dev)) {
  318. /* i965 stores the end address of the gtt mapping in the fence
  319. * reg, so dont bother to check the size */
  320. if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
  321. return false;
  322. } else if (IS_I9XX(dev)) {
  323. uint32_t pitch_val = ffs(stride / tile_width) - 1;
  324. /* XXX: For Y tiling, FENCE_MAX_PITCH_VAL is actually 6 (8KB)
  325. * instead of 4 (2KB) on 945s.
  326. */
  327. if (pitch_val > I915_FENCE_MAX_PITCH_VAL ||
  328. size > (I830_FENCE_MAX_SIZE_VAL << 20))
  329. return false;
  330. } else {
  331. uint32_t pitch_val = ffs(stride / tile_width) - 1;
  332. if (pitch_val > I830_FENCE_MAX_PITCH_VAL ||
  333. size > (I830_FENCE_MAX_SIZE_VAL << 19))
  334. return false;
  335. }
  336. /* 965+ just needs multiples of tile width */
  337. if (IS_I965G(dev)) {
  338. if (stride & (tile_width - 1))
  339. return false;
  340. return true;
  341. }
  342. /* Pre-965 needs power of two tile widths */
  343. if (stride < tile_width)
  344. return false;
  345. if (stride & (stride - 1))
  346. return false;
  347. /* We don't 0handle the aperture area covered by the fence being bigger
  348. * than the object size.
  349. */
  350. if (i915_get_fence_size(dev, size) != size)
  351. return false;
  352. return true;
  353. }
  354. static bool
  355. i915_gem_object_fence_offset_ok(struct drm_gem_object *obj, int tiling_mode)
  356. {
  357. struct drm_device *dev = obj->dev;
  358. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  359. if (obj_priv->gtt_space == NULL)
  360. return true;
  361. if (tiling_mode == I915_TILING_NONE)
  362. return true;
  363. if (!IS_I965G(dev)) {
  364. if (obj_priv->gtt_offset & (obj->size - 1))
  365. return false;
  366. if (IS_I9XX(dev)) {
  367. if (obj_priv->gtt_offset & ~I915_FENCE_START_MASK)
  368. return false;
  369. } else {
  370. if (obj_priv->gtt_offset & ~I830_FENCE_START_MASK)
  371. return false;
  372. }
  373. }
  374. return true;
  375. }
  376. /**
  377. * Sets the tiling mode of an object, returning the required swizzling of
  378. * bit 6 of addresses in the object.
  379. */
  380. int
  381. i915_gem_set_tiling(struct drm_device *dev, void *data,
  382. struct drm_file *file_priv)
  383. {
  384. struct drm_i915_gem_set_tiling *args = data;
  385. drm_i915_private_t *dev_priv = dev->dev_private;
  386. struct drm_gem_object *obj;
  387. struct drm_i915_gem_object *obj_priv;
  388. int ret = 0;
  389. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  390. if (obj == NULL)
  391. return -EINVAL;
  392. obj_priv = obj->driver_private;
  393. if (!i915_tiling_ok(dev, args->stride, obj->size, args->tiling_mode)) {
  394. mutex_lock(&dev->struct_mutex);
  395. drm_gem_object_unreference(obj);
  396. mutex_unlock(&dev->struct_mutex);
  397. return -EINVAL;
  398. }
  399. if (args->tiling_mode == I915_TILING_NONE) {
  400. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  401. args->stride = 0;
  402. } else {
  403. if (args->tiling_mode == I915_TILING_X)
  404. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  405. else
  406. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  407. /* Hide bit 17 swizzling from the user. This prevents old Mesa
  408. * from aborting the application on sw fallbacks to bit 17,
  409. * and we use the pread/pwrite bit17 paths to swizzle for it.
  410. * If there was a user that was relying on the swizzle
  411. * information for drm_intel_bo_map()ed reads/writes this would
  412. * break it, but we don't have any of those.
  413. */
  414. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  415. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  416. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  417. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  418. /* If we can't handle the swizzling, make it untiled. */
  419. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
  420. args->tiling_mode = I915_TILING_NONE;
  421. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  422. args->stride = 0;
  423. }
  424. }
  425. mutex_lock(&dev->struct_mutex);
  426. if (args->tiling_mode != obj_priv->tiling_mode ||
  427. args->stride != obj_priv->stride) {
  428. /* We need to rebind the object if its current allocation
  429. * no longer meets the alignment restrictions for its new
  430. * tiling mode. Otherwise we can just leave it alone, but
  431. * need to ensure that any fence register is cleared.
  432. */
  433. if (!i915_gem_object_fence_offset_ok(obj, args->tiling_mode))
  434. ret = i915_gem_object_unbind(obj);
  435. else
  436. ret = i915_gem_object_put_fence_reg(obj);
  437. if (ret != 0) {
  438. WARN(ret != -ERESTARTSYS,
  439. "failed to reset object for tiling switch");
  440. args->tiling_mode = obj_priv->tiling_mode;
  441. args->stride = obj_priv->stride;
  442. goto err;
  443. }
  444. /* If we've changed tiling, GTT-mappings of the object
  445. * need to re-fault to ensure that the correct fence register
  446. * setup is in place.
  447. */
  448. i915_gem_release_mmap(obj);
  449. obj_priv->tiling_mode = args->tiling_mode;
  450. obj_priv->stride = args->stride;
  451. }
  452. err:
  453. drm_gem_object_unreference(obj);
  454. mutex_unlock(&dev->struct_mutex);
  455. return ret;
  456. }
  457. /**
  458. * Returns the current tiling mode and required bit 6 swizzling for the object.
  459. */
  460. int
  461. i915_gem_get_tiling(struct drm_device *dev, void *data,
  462. struct drm_file *file_priv)
  463. {
  464. struct drm_i915_gem_get_tiling *args = data;
  465. drm_i915_private_t *dev_priv = dev->dev_private;
  466. struct drm_gem_object *obj;
  467. struct drm_i915_gem_object *obj_priv;
  468. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  469. if (obj == NULL)
  470. return -EINVAL;
  471. obj_priv = obj->driver_private;
  472. mutex_lock(&dev->struct_mutex);
  473. args->tiling_mode = obj_priv->tiling_mode;
  474. switch (obj_priv->tiling_mode) {
  475. case I915_TILING_X:
  476. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  477. break;
  478. case I915_TILING_Y:
  479. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  480. break;
  481. case I915_TILING_NONE:
  482. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  483. break;
  484. default:
  485. DRM_ERROR("unknown tiling mode\n");
  486. }
  487. /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
  488. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  489. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  490. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  491. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  492. drm_gem_object_unreference(obj);
  493. mutex_unlock(&dev->struct_mutex);
  494. return 0;
  495. }
  496. /**
  497. * Swap every 64 bytes of this page around, to account for it having a new
  498. * bit 17 of its physical address and therefore being interpreted differently
  499. * by the GPU.
  500. */
  501. static int
  502. i915_gem_swizzle_page(struct page *page)
  503. {
  504. char *vaddr;
  505. int i;
  506. char temp[64];
  507. vaddr = kmap(page);
  508. if (vaddr == NULL)
  509. return -ENOMEM;
  510. for (i = 0; i < PAGE_SIZE; i += 128) {
  511. memcpy(temp, &vaddr[i], 64);
  512. memcpy(&vaddr[i], &vaddr[i + 64], 64);
  513. memcpy(&vaddr[i + 64], temp, 64);
  514. }
  515. kunmap(page);
  516. return 0;
  517. }
  518. void
  519. i915_gem_object_do_bit_17_swizzle(struct drm_gem_object *obj)
  520. {
  521. struct drm_device *dev = obj->dev;
  522. drm_i915_private_t *dev_priv = dev->dev_private;
  523. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  524. int page_count = obj->size >> PAGE_SHIFT;
  525. int i;
  526. if (dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_9_10_17)
  527. return;
  528. if (obj_priv->bit_17 == NULL)
  529. return;
  530. for (i = 0; i < page_count; i++) {
  531. char new_bit_17 = page_to_phys(obj_priv->pages[i]) >> 17;
  532. if ((new_bit_17 & 0x1) !=
  533. (test_bit(i, obj_priv->bit_17) != 0)) {
  534. int ret = i915_gem_swizzle_page(obj_priv->pages[i]);
  535. if (ret != 0) {
  536. DRM_ERROR("Failed to swizzle page\n");
  537. return;
  538. }
  539. set_page_dirty(obj_priv->pages[i]);
  540. }
  541. }
  542. }
  543. void
  544. i915_gem_object_save_bit_17_swizzle(struct drm_gem_object *obj)
  545. {
  546. struct drm_device *dev = obj->dev;
  547. drm_i915_private_t *dev_priv = dev->dev_private;
  548. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  549. int page_count = obj->size >> PAGE_SHIFT;
  550. int i;
  551. if (dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_9_10_17)
  552. return;
  553. if (obj_priv->bit_17 == NULL) {
  554. obj_priv->bit_17 = kmalloc(BITS_TO_LONGS(page_count) *
  555. sizeof(long), GFP_KERNEL);
  556. if (obj_priv->bit_17 == NULL) {
  557. DRM_ERROR("Failed to allocate memory for bit 17 "
  558. "record\n");
  559. return;
  560. }
  561. }
  562. for (i = 0; i < page_count; i++) {
  563. if (page_to_phys(obj_priv->pages[i]) & (1 << 17))
  564. __set_bit(i, obj_priv->bit_17);
  565. else
  566. __clear_bit(i, obj_priv->bit_17);
  567. }
  568. }