i915_gem_tiling.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 whether an object is currently fenceable. If not, it may need
  281. * to be unbound and have its pitch adjusted.
  282. */
  283. bool
  284. i915_obj_fenceable(struct drm_device *dev, struct drm_gem_object *obj)
  285. {
  286. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  287. if (IS_I965G(dev)) {
  288. /* The 965 can have fences at any page boundary. */
  289. if (obj->size & 4095)
  290. return false;
  291. return true;
  292. } else if (IS_I9XX(dev)) {
  293. if (obj_priv->gtt_offset & ~I915_FENCE_START_MASK)
  294. return false;
  295. } else {
  296. if (obj_priv->gtt_offset & ~I830_FENCE_START_MASK)
  297. return false;
  298. }
  299. /* Power of two sized... */
  300. if (obj->size & (obj->size - 1))
  301. return false;
  302. /* Objects must be size aligned as well */
  303. if (obj_priv->gtt_offset & (obj->size - 1))
  304. return false;
  305. return true;
  306. }
  307. /* Check pitch constriants for all chips & tiling formats */
  308. bool
  309. i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
  310. {
  311. int tile_width;
  312. /* Linear is always fine */
  313. if (tiling_mode == I915_TILING_NONE)
  314. return true;
  315. if (!IS_I9XX(dev) ||
  316. (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
  317. tile_width = 128;
  318. else
  319. tile_width = 512;
  320. /* check maximum stride & object size */
  321. if (IS_I965G(dev)) {
  322. /* i965 stores the end address of the gtt mapping in the fence
  323. * reg, so dont bother to check the size */
  324. if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
  325. return false;
  326. } else if (IS_I9XX(dev)) {
  327. uint32_t pitch_val = ffs(stride / tile_width) - 1;
  328. /* XXX: For Y tiling, FENCE_MAX_PITCH_VAL is actually 6 (8KB)
  329. * instead of 4 (2KB) on 945s.
  330. */
  331. if (pitch_val > I915_FENCE_MAX_PITCH_VAL ||
  332. size > (I830_FENCE_MAX_SIZE_VAL << 20))
  333. return false;
  334. } else {
  335. uint32_t pitch_val = ffs(stride / tile_width) - 1;
  336. if (pitch_val > I830_FENCE_MAX_PITCH_VAL ||
  337. size > (I830_FENCE_MAX_SIZE_VAL << 19))
  338. return false;
  339. }
  340. /* 965+ just needs multiples of tile width */
  341. if (IS_I965G(dev)) {
  342. if (stride & (tile_width - 1))
  343. return false;
  344. return true;
  345. }
  346. /* Pre-965 needs power of two tile widths */
  347. if (stride < tile_width)
  348. return false;
  349. if (stride & (stride - 1))
  350. return false;
  351. return true;
  352. }
  353. static bool
  354. i915_gem_object_fence_offset_ok(struct drm_gem_object *obj, int tiling_mode)
  355. {
  356. struct drm_device *dev = obj->dev;
  357. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  358. if (obj_priv->gtt_space == NULL)
  359. return true;
  360. if (tiling_mode == I915_TILING_NONE)
  361. return true;
  362. if (!IS_I965G(dev)) {
  363. if (obj_priv->gtt_offset & (obj->size - 1))
  364. return false;
  365. if (IS_I9XX(dev)) {
  366. if (obj_priv->gtt_offset & ~I915_FENCE_START_MASK)
  367. return false;
  368. } else {
  369. if (obj_priv->gtt_offset & ~I830_FENCE_START_MASK)
  370. return false;
  371. }
  372. }
  373. return true;
  374. }
  375. /**
  376. * Sets the tiling mode of an object, returning the required swizzling of
  377. * bit 6 of addresses in the object.
  378. */
  379. int
  380. i915_gem_set_tiling(struct drm_device *dev, void *data,
  381. struct drm_file *file_priv)
  382. {
  383. struct drm_i915_gem_set_tiling *args = data;
  384. drm_i915_private_t *dev_priv = dev->dev_private;
  385. struct drm_gem_object *obj;
  386. struct drm_i915_gem_object *obj_priv;
  387. int ret = 0;
  388. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  389. if (obj == NULL)
  390. return -EINVAL;
  391. obj_priv = obj->driver_private;
  392. if (!i915_tiling_ok(dev, args->stride, obj->size, args->tiling_mode)) {
  393. mutex_lock(&dev->struct_mutex);
  394. drm_gem_object_unreference(obj);
  395. mutex_unlock(&dev->struct_mutex);
  396. return -EINVAL;
  397. }
  398. if (args->tiling_mode == I915_TILING_NONE) {
  399. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  400. args->stride = 0;
  401. } else {
  402. if (args->tiling_mode == I915_TILING_X)
  403. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  404. else
  405. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  406. /* Hide bit 17 swizzling from the user. This prevents old Mesa
  407. * from aborting the application on sw fallbacks to bit 17,
  408. * and we use the pread/pwrite bit17 paths to swizzle for it.
  409. * If there was a user that was relying on the swizzle
  410. * information for drm_intel_bo_map()ed reads/writes this would
  411. * break it, but we don't have any of those.
  412. */
  413. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  414. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  415. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  416. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  417. /* If we can't handle the swizzling, make it untiled. */
  418. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
  419. args->tiling_mode = I915_TILING_NONE;
  420. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  421. args->stride = 0;
  422. }
  423. }
  424. mutex_lock(&dev->struct_mutex);
  425. if (args->tiling_mode != obj_priv->tiling_mode ||
  426. args->stride != obj_priv->stride) {
  427. /* We need to rebind the object if its current allocation
  428. * no longer meets the alignment restrictions for its new
  429. * tiling mode. Otherwise we can just leave it alone, but
  430. * need to ensure that any fence register is cleared.
  431. */
  432. if (!i915_gem_object_fence_offset_ok(obj, args->tiling_mode))
  433. ret = i915_gem_object_unbind(obj);
  434. else
  435. ret = i915_gem_object_put_fence_reg(obj);
  436. if (ret != 0) {
  437. WARN(ret != -ERESTARTSYS,
  438. "failed to reset object for tiling switch");
  439. args->tiling_mode = obj_priv->tiling_mode;
  440. args->stride = obj_priv->stride;
  441. goto err;
  442. }
  443. /* If we've changed tiling, GTT-mappings of the object
  444. * need to re-fault to ensure that the correct fence register
  445. * setup is in place.
  446. */
  447. i915_gem_release_mmap(obj);
  448. obj_priv->tiling_mode = args->tiling_mode;
  449. obj_priv->stride = args->stride;
  450. }
  451. err:
  452. drm_gem_object_unreference(obj);
  453. mutex_unlock(&dev->struct_mutex);
  454. return ret;
  455. }
  456. /**
  457. * Returns the current tiling mode and required bit 6 swizzling for the object.
  458. */
  459. int
  460. i915_gem_get_tiling(struct drm_device *dev, void *data,
  461. struct drm_file *file_priv)
  462. {
  463. struct drm_i915_gem_get_tiling *args = data;
  464. drm_i915_private_t *dev_priv = dev->dev_private;
  465. struct drm_gem_object *obj;
  466. struct drm_i915_gem_object *obj_priv;
  467. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  468. if (obj == NULL)
  469. return -EINVAL;
  470. obj_priv = obj->driver_private;
  471. mutex_lock(&dev->struct_mutex);
  472. args->tiling_mode = obj_priv->tiling_mode;
  473. switch (obj_priv->tiling_mode) {
  474. case I915_TILING_X:
  475. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  476. break;
  477. case I915_TILING_Y:
  478. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  479. break;
  480. case I915_TILING_NONE:
  481. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  482. break;
  483. default:
  484. DRM_ERROR("unknown tiling mode\n");
  485. }
  486. /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
  487. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  488. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  489. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  490. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  491. drm_gem_object_unreference(obj);
  492. mutex_unlock(&dev->struct_mutex);
  493. return 0;
  494. }
  495. /**
  496. * Swap every 64 bytes of this page around, to account for it having a new
  497. * bit 17 of its physical address and therefore being interpreted differently
  498. * by the GPU.
  499. */
  500. static int
  501. i915_gem_swizzle_page(struct page *page)
  502. {
  503. char *vaddr;
  504. int i;
  505. char temp[64];
  506. vaddr = kmap(page);
  507. if (vaddr == NULL)
  508. return -ENOMEM;
  509. for (i = 0; i < PAGE_SIZE; i += 128) {
  510. memcpy(temp, &vaddr[i], 64);
  511. memcpy(&vaddr[i], &vaddr[i + 64], 64);
  512. memcpy(&vaddr[i + 64], temp, 64);
  513. }
  514. kunmap(page);
  515. return 0;
  516. }
  517. void
  518. i915_gem_object_do_bit_17_swizzle(struct drm_gem_object *obj)
  519. {
  520. struct drm_device *dev = obj->dev;
  521. drm_i915_private_t *dev_priv = dev->dev_private;
  522. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  523. int page_count = obj->size >> PAGE_SHIFT;
  524. int i;
  525. if (dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_9_10_17)
  526. return;
  527. if (obj_priv->bit_17 == NULL)
  528. return;
  529. for (i = 0; i < page_count; i++) {
  530. char new_bit_17 = page_to_phys(obj_priv->pages[i]) >> 17;
  531. if ((new_bit_17 & 0x1) !=
  532. (test_bit(i, obj_priv->bit_17) != 0)) {
  533. int ret = i915_gem_swizzle_page(obj_priv->pages[i]);
  534. if (ret != 0) {
  535. DRM_ERROR("Failed to swizzle page\n");
  536. return;
  537. }
  538. set_page_dirty(obj_priv->pages[i]);
  539. }
  540. }
  541. }
  542. void
  543. i915_gem_object_save_bit_17_swizzle(struct drm_gem_object *obj)
  544. {
  545. struct drm_device *dev = obj->dev;
  546. drm_i915_private_t *dev_priv = dev->dev_private;
  547. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  548. int page_count = obj->size >> PAGE_SHIFT;
  549. int i;
  550. if (dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_9_10_17)
  551. return;
  552. if (obj_priv->bit_17 == NULL) {
  553. obj_priv->bit_17 = kmalloc(BITS_TO_LONGS(page_count) *
  554. sizeof(long), GFP_KERNEL);
  555. if (obj_priv->bit_17 == NULL) {
  556. DRM_ERROR("Failed to allocate memory for bit 17 "
  557. "record\n");
  558. return;
  559. }
  560. }
  561. for (i = 0; i < page_count; i++) {
  562. if (page_to_phys(obj_priv->pages[i]) & (1 << 17))
  563. __set_bit(i, obj_priv->bit_17);
  564. else
  565. __clear_bit(i, obj_priv->bit_17);
  566. }
  567. }