i915_gem_tiling.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. struct pci_dev *bridge_dev;
  93. drm_i915_private_t *dev_priv = dev->dev_private;
  94. int reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
  95. u32 temp_lo, temp_hi = 0;
  96. u64 mchbar_addr;
  97. int ret = 0;
  98. bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
  99. if (!bridge_dev) {
  100. DRM_DEBUG("no bridge dev?!\n");
  101. ret = -ENODEV;
  102. goto out;
  103. }
  104. if (IS_I965G(dev))
  105. pci_read_config_dword(bridge_dev, reg + 4, &temp_hi);
  106. pci_read_config_dword(bridge_dev, reg, &temp_lo);
  107. mchbar_addr = ((u64)temp_hi << 32) | temp_lo;
  108. /* If ACPI doesn't have it, assume we need to allocate it ourselves */
  109. #ifdef CONFIG_PNP
  110. if (mchbar_addr &&
  111. pnp_range_reserved(mchbar_addr, mchbar_addr + MCHBAR_SIZE)) {
  112. ret = 0;
  113. goto out_put;
  114. }
  115. #endif
  116. /* Get some space for it */
  117. ret = pci_bus_alloc_resource(bridge_dev->bus, &dev_priv->mch_res,
  118. MCHBAR_SIZE, MCHBAR_SIZE,
  119. PCIBIOS_MIN_MEM,
  120. 0, pcibios_align_resource,
  121. bridge_dev);
  122. if (ret) {
  123. DRM_DEBUG("failed bus alloc: %d\n", ret);
  124. dev_priv->mch_res.start = 0;
  125. goto out_put;
  126. }
  127. if (IS_I965G(dev))
  128. pci_write_config_dword(bridge_dev, reg + 4,
  129. upper_32_bits(dev_priv->mch_res.start));
  130. pci_write_config_dword(bridge_dev, reg,
  131. lower_32_bits(dev_priv->mch_res.start));
  132. out_put:
  133. pci_dev_put(bridge_dev);
  134. out:
  135. return ret;
  136. }
  137. /* Setup MCHBAR if possible, return true if we should disable it again */
  138. static bool
  139. intel_setup_mchbar(struct drm_device *dev)
  140. {
  141. struct pci_dev *bridge_dev;
  142. int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
  143. u32 temp;
  144. bool need_disable = false, enabled;
  145. bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
  146. if (!bridge_dev) {
  147. DRM_DEBUG("no bridge dev?!\n");
  148. goto out;
  149. }
  150. if (IS_I915G(dev) || IS_I915GM(dev)) {
  151. pci_read_config_dword(bridge_dev, DEVEN_REG, &temp);
  152. enabled = !!(temp & DEVEN_MCHBAR_EN);
  153. } else {
  154. pci_read_config_dword(bridge_dev, mchbar_reg, &temp);
  155. enabled = temp & 1;
  156. }
  157. /* If it's already enabled, don't have to do anything */
  158. if (enabled)
  159. goto out_put;
  160. if (intel_alloc_mchbar_resource(dev))
  161. goto out_put;
  162. need_disable = true;
  163. /* Space is allocated or reserved, so enable it. */
  164. if (IS_I915G(dev) || IS_I915GM(dev)) {
  165. pci_write_config_dword(bridge_dev, DEVEN_REG,
  166. temp | DEVEN_MCHBAR_EN);
  167. } else {
  168. pci_read_config_dword(bridge_dev, mchbar_reg, &temp);
  169. pci_write_config_dword(bridge_dev, mchbar_reg, temp | 1);
  170. }
  171. out_put:
  172. pci_dev_put(bridge_dev);
  173. out:
  174. return need_disable;
  175. }
  176. static void
  177. intel_teardown_mchbar(struct drm_device *dev, bool disable)
  178. {
  179. drm_i915_private_t *dev_priv = dev->dev_private;
  180. struct pci_dev *bridge_dev;
  181. int mchbar_reg = IS_I965G(dev) ? MCHBAR_I965 : MCHBAR_I915;
  182. u32 temp;
  183. bridge_dev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0));
  184. if (!bridge_dev) {
  185. DRM_DEBUG("no bridge dev?!\n");
  186. return;
  187. }
  188. if (disable) {
  189. if (IS_I915G(dev) || IS_I915GM(dev)) {
  190. pci_read_config_dword(bridge_dev, DEVEN_REG, &temp);
  191. temp &= ~DEVEN_MCHBAR_EN;
  192. pci_write_config_dword(bridge_dev, DEVEN_REG, temp);
  193. } else {
  194. pci_read_config_dword(bridge_dev, mchbar_reg, &temp);
  195. temp &= ~1;
  196. pci_write_config_dword(bridge_dev, mchbar_reg, temp);
  197. }
  198. }
  199. if (dev_priv->mch_res.start)
  200. release_resource(&dev_priv->mch_res);
  201. }
  202. /**
  203. * Detects bit 6 swizzling of address lookup between IGD access and CPU
  204. * access through main memory.
  205. */
  206. void
  207. i915_gem_detect_bit_6_swizzle(struct drm_device *dev)
  208. {
  209. drm_i915_private_t *dev_priv = dev->dev_private;
  210. uint32_t swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
  211. uint32_t swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
  212. bool need_disable;
  213. if (!IS_I9XX(dev)) {
  214. /* As far as we know, the 865 doesn't have these bit 6
  215. * swizzling issues.
  216. */
  217. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  218. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  219. } else if (IS_MOBILE(dev)) {
  220. uint32_t dcc;
  221. /* Try to make sure MCHBAR is enabled before poking at it */
  222. need_disable = intel_setup_mchbar(dev);
  223. /* On mobile 9xx chipsets, channel interleave by the CPU is
  224. * determined by DCC. For single-channel, neither the CPU
  225. * nor the GPU do swizzling. For dual channel interleaved,
  226. * the GPU's interleave is bit 9 and 10 for X tiled, and bit
  227. * 9 for Y tiled. The CPU's interleave is independent, and
  228. * can be based on either bit 11 (haven't seen this yet) or
  229. * bit 17 (common).
  230. */
  231. dcc = I915_READ(DCC);
  232. switch (dcc & DCC_ADDRESSING_MODE_MASK) {
  233. case DCC_ADDRESSING_MODE_SINGLE_CHANNEL:
  234. case DCC_ADDRESSING_MODE_DUAL_CHANNEL_ASYMMETRIC:
  235. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  236. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  237. break;
  238. case DCC_ADDRESSING_MODE_DUAL_CHANNEL_INTERLEAVED:
  239. if (dcc & DCC_CHANNEL_XOR_DISABLE) {
  240. /* This is the base swizzling by the GPU for
  241. * tiled buffers.
  242. */
  243. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  244. swizzle_y = I915_BIT_6_SWIZZLE_9;
  245. } else if ((dcc & DCC_CHANNEL_XOR_BIT_17) == 0) {
  246. /* Bit 11 swizzling by the CPU in addition. */
  247. swizzle_x = I915_BIT_6_SWIZZLE_9_10_11;
  248. swizzle_y = I915_BIT_6_SWIZZLE_9_11;
  249. } else {
  250. /* Bit 17 swizzling by the CPU in addition. */
  251. swizzle_x = I915_BIT_6_SWIZZLE_9_10_17;
  252. swizzle_y = I915_BIT_6_SWIZZLE_9_17;
  253. }
  254. break;
  255. }
  256. if (dcc == 0xffffffff) {
  257. DRM_ERROR("Couldn't read from MCHBAR. "
  258. "Disabling tiling.\n");
  259. swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
  260. swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
  261. }
  262. intel_teardown_mchbar(dev, need_disable);
  263. } else {
  264. /* The 965, G33, and newer, have a very flexible memory
  265. * configuration. It will enable dual-channel mode
  266. * (interleaving) on as much memory as it can, and the GPU
  267. * will additionally sometimes enable different bit 6
  268. * swizzling for tiled objects from the CPU.
  269. *
  270. * Here's what I found on the G965:
  271. * slot fill memory size swizzling
  272. * 0A 0B 1A 1B 1-ch 2-ch
  273. * 512 0 0 0 512 0 O
  274. * 512 0 512 0 16 1008 X
  275. * 512 0 0 512 16 1008 X
  276. * 0 512 0 512 16 1008 X
  277. * 1024 1024 1024 0 2048 1024 O
  278. *
  279. * We could probably detect this based on either the DRB
  280. * matching, which was the case for the swizzling required in
  281. * the table above, or from the 1-ch value being less than
  282. * the minimum size of a rank.
  283. */
  284. if (I915_READ16(C0DRB3) != I915_READ16(C1DRB3)) {
  285. swizzle_x = I915_BIT_6_SWIZZLE_NONE;
  286. swizzle_y = I915_BIT_6_SWIZZLE_NONE;
  287. } else {
  288. swizzle_x = I915_BIT_6_SWIZZLE_9_10;
  289. swizzle_y = I915_BIT_6_SWIZZLE_9;
  290. }
  291. }
  292. /* FIXME: check with memory config on IGDNG */
  293. if (IS_IGDNG(dev)) {
  294. DRM_ERROR("disable tiling on IGDNG...\n");
  295. swizzle_x = I915_BIT_6_SWIZZLE_UNKNOWN;
  296. swizzle_y = I915_BIT_6_SWIZZLE_UNKNOWN;
  297. }
  298. dev_priv->mm.bit_6_swizzle_x = swizzle_x;
  299. dev_priv->mm.bit_6_swizzle_y = swizzle_y;
  300. }
  301. /**
  302. * Returns the size of the fence for a tiled object of the given size.
  303. */
  304. static int
  305. i915_get_fence_size(struct drm_device *dev, int size)
  306. {
  307. int i;
  308. int start;
  309. if (IS_I965G(dev)) {
  310. /* The 965 can have fences at any page boundary. */
  311. return ALIGN(size, 4096);
  312. } else {
  313. /* Align the size to a power of two greater than the smallest
  314. * fence size.
  315. */
  316. if (IS_I9XX(dev))
  317. start = 1024 * 1024;
  318. else
  319. start = 512 * 1024;
  320. for (i = start; i < size; i <<= 1)
  321. ;
  322. return i;
  323. }
  324. }
  325. /* Check pitch constriants for all chips & tiling formats */
  326. static bool
  327. i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
  328. {
  329. int tile_width;
  330. /* Linear is always fine */
  331. if (tiling_mode == I915_TILING_NONE)
  332. return true;
  333. if (!IS_I9XX(dev) ||
  334. (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
  335. tile_width = 128;
  336. else
  337. tile_width = 512;
  338. /* check maximum stride & object size */
  339. if (IS_I965G(dev)) {
  340. /* i965 stores the end address of the gtt mapping in the fence
  341. * reg, so dont bother to check the size */
  342. if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
  343. return false;
  344. } else if (IS_I9XX(dev)) {
  345. uint32_t pitch_val = ffs(stride / tile_width) - 1;
  346. /* XXX: For Y tiling, FENCE_MAX_PITCH_VAL is actually 6 (8KB)
  347. * instead of 4 (2KB) on 945s.
  348. */
  349. if (pitch_val > I915_FENCE_MAX_PITCH_VAL ||
  350. size > (I830_FENCE_MAX_SIZE_VAL << 20))
  351. return false;
  352. } else {
  353. uint32_t pitch_val = ffs(stride / tile_width) - 1;
  354. if (pitch_val > I830_FENCE_MAX_PITCH_VAL ||
  355. size > (I830_FENCE_MAX_SIZE_VAL << 19))
  356. return false;
  357. }
  358. /* 965+ just needs multiples of tile width */
  359. if (IS_I965G(dev)) {
  360. if (stride & (tile_width - 1))
  361. return false;
  362. return true;
  363. }
  364. /* Pre-965 needs power of two tile widths */
  365. if (stride < tile_width)
  366. return false;
  367. if (stride & (stride - 1))
  368. return false;
  369. /* We don't 0handle the aperture area covered by the fence being bigger
  370. * than the object size.
  371. */
  372. if (i915_get_fence_size(dev, size) != size)
  373. return false;
  374. return true;
  375. }
  376. static bool
  377. i915_gem_object_fence_offset_ok(struct drm_gem_object *obj, int tiling_mode)
  378. {
  379. struct drm_device *dev = obj->dev;
  380. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  381. if (obj_priv->gtt_space == NULL)
  382. return true;
  383. if (tiling_mode == I915_TILING_NONE)
  384. return true;
  385. if (!IS_I965G(dev)) {
  386. if (obj_priv->gtt_offset & (obj->size - 1))
  387. return false;
  388. if (IS_I9XX(dev)) {
  389. if (obj_priv->gtt_offset & ~I915_FENCE_START_MASK)
  390. return false;
  391. } else {
  392. if (obj_priv->gtt_offset & ~I830_FENCE_START_MASK)
  393. return false;
  394. }
  395. }
  396. return true;
  397. }
  398. /**
  399. * Sets the tiling mode of an object, returning the required swizzling of
  400. * bit 6 of addresses in the object.
  401. */
  402. int
  403. i915_gem_set_tiling(struct drm_device *dev, void *data,
  404. struct drm_file *file_priv)
  405. {
  406. struct drm_i915_gem_set_tiling *args = data;
  407. drm_i915_private_t *dev_priv = dev->dev_private;
  408. struct drm_gem_object *obj;
  409. struct drm_i915_gem_object *obj_priv;
  410. int ret = 0;
  411. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  412. if (obj == NULL)
  413. return -EINVAL;
  414. obj_priv = obj->driver_private;
  415. if (!i915_tiling_ok(dev, args->stride, obj->size, args->tiling_mode)) {
  416. mutex_lock(&dev->struct_mutex);
  417. drm_gem_object_unreference(obj);
  418. mutex_unlock(&dev->struct_mutex);
  419. return -EINVAL;
  420. }
  421. if (args->tiling_mode == I915_TILING_NONE) {
  422. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  423. args->stride = 0;
  424. } else {
  425. if (args->tiling_mode == I915_TILING_X)
  426. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  427. else
  428. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  429. /* Hide bit 17 swizzling from the user. This prevents old Mesa
  430. * from aborting the application on sw fallbacks to bit 17,
  431. * and we use the pread/pwrite bit17 paths to swizzle for it.
  432. * If there was a user that was relying on the swizzle
  433. * information for drm_intel_bo_map()ed reads/writes this would
  434. * break it, but we don't have any of those.
  435. */
  436. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  437. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  438. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  439. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  440. /* If we can't handle the swizzling, make it untiled. */
  441. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
  442. args->tiling_mode = I915_TILING_NONE;
  443. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  444. args->stride = 0;
  445. }
  446. }
  447. mutex_lock(&dev->struct_mutex);
  448. if (args->tiling_mode != obj_priv->tiling_mode ||
  449. args->stride != obj_priv->stride) {
  450. /* We need to rebind the object if its current allocation
  451. * no longer meets the alignment restrictions for its new
  452. * tiling mode. Otherwise we can just leave it alone, but
  453. * need to ensure that any fence register is cleared.
  454. */
  455. if (!i915_gem_object_fence_offset_ok(obj, args->tiling_mode))
  456. ret = i915_gem_object_unbind(obj);
  457. else
  458. ret = i915_gem_object_put_fence_reg(obj);
  459. if (ret != 0) {
  460. WARN(ret != -ERESTARTSYS,
  461. "failed to reset object for tiling switch");
  462. args->tiling_mode = obj_priv->tiling_mode;
  463. args->stride = obj_priv->stride;
  464. goto err;
  465. }
  466. /* If we've changed tiling, GTT-mappings of the object
  467. * need to re-fault to ensure that the correct fence register
  468. * setup is in place.
  469. */
  470. i915_gem_release_mmap(obj);
  471. obj_priv->tiling_mode = args->tiling_mode;
  472. obj_priv->stride = args->stride;
  473. }
  474. err:
  475. drm_gem_object_unreference(obj);
  476. mutex_unlock(&dev->struct_mutex);
  477. return ret;
  478. }
  479. /**
  480. * Returns the current tiling mode and required bit 6 swizzling for the object.
  481. */
  482. int
  483. i915_gem_get_tiling(struct drm_device *dev, void *data,
  484. struct drm_file *file_priv)
  485. {
  486. struct drm_i915_gem_get_tiling *args = data;
  487. drm_i915_private_t *dev_priv = dev->dev_private;
  488. struct drm_gem_object *obj;
  489. struct drm_i915_gem_object *obj_priv;
  490. obj = drm_gem_object_lookup(dev, file_priv, args->handle);
  491. if (obj == NULL)
  492. return -EINVAL;
  493. obj_priv = obj->driver_private;
  494. mutex_lock(&dev->struct_mutex);
  495. args->tiling_mode = obj_priv->tiling_mode;
  496. switch (obj_priv->tiling_mode) {
  497. case I915_TILING_X:
  498. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  499. break;
  500. case I915_TILING_Y:
  501. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  502. break;
  503. case I915_TILING_NONE:
  504. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  505. break;
  506. default:
  507. DRM_ERROR("unknown tiling mode\n");
  508. }
  509. /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
  510. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  511. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  512. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  513. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  514. drm_gem_object_unreference(obj);
  515. mutex_unlock(&dev->struct_mutex);
  516. return 0;
  517. }
  518. /**
  519. * Swap every 64 bytes of this page around, to account for it having a new
  520. * bit 17 of its physical address and therefore being interpreted differently
  521. * by the GPU.
  522. */
  523. static int
  524. i915_gem_swizzle_page(struct page *page)
  525. {
  526. char *vaddr;
  527. int i;
  528. char temp[64];
  529. vaddr = kmap(page);
  530. if (vaddr == NULL)
  531. return -ENOMEM;
  532. for (i = 0; i < PAGE_SIZE; i += 128) {
  533. memcpy(temp, &vaddr[i], 64);
  534. memcpy(&vaddr[i], &vaddr[i + 64], 64);
  535. memcpy(&vaddr[i + 64], temp, 64);
  536. }
  537. kunmap(page);
  538. return 0;
  539. }
  540. void
  541. i915_gem_object_do_bit_17_swizzle(struct drm_gem_object *obj)
  542. {
  543. struct drm_device *dev = obj->dev;
  544. drm_i915_private_t *dev_priv = dev->dev_private;
  545. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  546. int page_count = obj->size >> PAGE_SHIFT;
  547. int i;
  548. if (dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_9_10_17)
  549. return;
  550. if (obj_priv->bit_17 == NULL)
  551. return;
  552. for (i = 0; i < page_count; i++) {
  553. char new_bit_17 = page_to_phys(obj_priv->pages[i]) >> 17;
  554. if ((new_bit_17 & 0x1) !=
  555. (test_bit(i, obj_priv->bit_17) != 0)) {
  556. int ret = i915_gem_swizzle_page(obj_priv->pages[i]);
  557. if (ret != 0) {
  558. DRM_ERROR("Failed to swizzle page\n");
  559. return;
  560. }
  561. set_page_dirty(obj_priv->pages[i]);
  562. }
  563. }
  564. }
  565. void
  566. i915_gem_object_save_bit_17_swizzle(struct drm_gem_object *obj)
  567. {
  568. struct drm_device *dev = obj->dev;
  569. drm_i915_private_t *dev_priv = dev->dev_private;
  570. struct drm_i915_gem_object *obj_priv = obj->driver_private;
  571. int page_count = obj->size >> PAGE_SHIFT;
  572. int i;
  573. if (dev_priv->mm.bit_6_swizzle_x != I915_BIT_6_SWIZZLE_9_10_17)
  574. return;
  575. if (obj_priv->bit_17 == NULL) {
  576. obj_priv->bit_17 = kmalloc(BITS_TO_LONGS(page_count) *
  577. sizeof(long), GFP_KERNEL);
  578. if (obj_priv->bit_17 == NULL) {
  579. DRM_ERROR("Failed to allocate memory for bit 17 "
  580. "record\n");
  581. return;
  582. }
  583. }
  584. for (i = 0; i < page_count; i++) {
  585. if (page_to_phys(obj_priv->pages[i]) & (1 << 17))
  586. __set_bit(i, obj_priv->bit_17);
  587. else
  588. __clear_bit(i, obj_priv->bit_17);
  589. }
  590. }