i915_gem_tiling.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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/string.h>
  28. #include <linux/bitops.h>
  29. #include <drm/drmP.h>
  30. #include <drm/i915_drm.h>
  31. #include "i915_drv.h"
  32. /**
  33. * DOC: buffer object tiling
  34. *
  35. * i915_gem_set_tiling() and i915_gem_get_tiling() is the userspace interface to
  36. * declare fence register requirements.
  37. *
  38. * In principle GEM doesn't care at all about the internal data layout of an
  39. * object, and hence it also doesn't care about tiling or swizzling. There's two
  40. * exceptions:
  41. *
  42. * - For X and Y tiling the hardware provides detilers for CPU access, so called
  43. * fences. Since there's only a limited amount of them the kernel must manage
  44. * these, and therefore userspace must tell the kernel the object tiling if it
  45. * wants to use fences for detiling.
  46. * - On gen3 and gen4 platforms have a swizzling pattern for tiled objects which
  47. * depends upon the physical page frame number. When swapping such objects the
  48. * page frame number might change and the kernel must be able to fix this up
  49. * and hence now the tiling. Note that on a subset of platforms with
  50. * asymmetric memory channel population the swizzling pattern changes in an
  51. * unknown way, and for those the kernel simply forbids swapping completely.
  52. *
  53. * Since neither of this applies for new tiling layouts on modern platforms like
  54. * W, Ys and Yf tiling GEM only allows object tiling to be set to X or Y tiled.
  55. * Anything else can be handled in userspace entirely without the kernel's
  56. * invovlement.
  57. */
  58. /* Check pitch constriants for all chips & tiling formats */
  59. static bool
  60. i915_tiling_ok(struct drm_device *dev, int stride, int size, int tiling_mode)
  61. {
  62. int tile_width;
  63. /* Linear is always fine */
  64. if (tiling_mode == I915_TILING_NONE)
  65. return true;
  66. if (IS_GEN2(dev) ||
  67. (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
  68. tile_width = 128;
  69. else
  70. tile_width = 512;
  71. /* check maximum stride & object size */
  72. /* i965+ stores the end address of the gtt mapping in the fence
  73. * reg, so dont bother to check the size */
  74. if (INTEL_INFO(dev)->gen >= 7) {
  75. if (stride / 128 > GEN7_FENCE_MAX_PITCH_VAL)
  76. return false;
  77. } else if (INTEL_INFO(dev)->gen >= 4) {
  78. if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
  79. return false;
  80. } else {
  81. if (stride > 8192)
  82. return false;
  83. if (IS_GEN3(dev)) {
  84. if (size > I830_FENCE_MAX_SIZE_VAL << 20)
  85. return false;
  86. } else {
  87. if (size > I830_FENCE_MAX_SIZE_VAL << 19)
  88. return false;
  89. }
  90. }
  91. if (stride < tile_width)
  92. return false;
  93. /* 965+ just needs multiples of tile width */
  94. if (INTEL_INFO(dev)->gen >= 4) {
  95. if (stride & (tile_width - 1))
  96. return false;
  97. return true;
  98. }
  99. /* Pre-965 needs power of two tile widths */
  100. if (stride & (stride - 1))
  101. return false;
  102. return true;
  103. }
  104. /* Is the current GTT allocation valid for the change in tiling? */
  105. static bool
  106. i915_gem_object_fence_ok(struct drm_i915_gem_object *obj, int tiling_mode)
  107. {
  108. u32 size;
  109. if (tiling_mode == I915_TILING_NONE)
  110. return true;
  111. if (INTEL_INFO(obj->base.dev)->gen >= 4)
  112. return true;
  113. if (INTEL_INFO(obj->base.dev)->gen == 3) {
  114. if (i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK)
  115. return false;
  116. } else {
  117. if (i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK)
  118. return false;
  119. }
  120. size = i915_gem_get_gtt_size(obj->base.dev, obj->base.size, tiling_mode);
  121. if (i915_gem_obj_ggtt_size(obj) != size)
  122. return false;
  123. if (i915_gem_obj_ggtt_offset(obj) & (size - 1))
  124. return false;
  125. return true;
  126. }
  127. /**
  128. * i915_gem_set_tiling - IOCTL handler to set tiling mode
  129. * @dev: DRM device
  130. * @data: data pointer for the ioctl
  131. * @file: DRM file for the ioctl call
  132. *
  133. * Sets the tiling mode of an object, returning the required swizzling of
  134. * bit 6 of addresses in the object.
  135. *
  136. * Called by the user via ioctl.
  137. *
  138. * Returns:
  139. * Zero on success, negative errno on failure.
  140. */
  141. int
  142. i915_gem_set_tiling(struct drm_device *dev, void *data,
  143. struct drm_file *file)
  144. {
  145. struct drm_i915_gem_set_tiling *args = data;
  146. struct drm_i915_private *dev_priv = dev->dev_private;
  147. struct drm_i915_gem_object *obj;
  148. int ret = 0;
  149. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  150. if (&obj->base == NULL)
  151. return -ENOENT;
  152. if (!i915_tiling_ok(dev,
  153. args->stride, obj->base.size, args->tiling_mode)) {
  154. drm_gem_object_unreference_unlocked(&obj->base);
  155. return -EINVAL;
  156. }
  157. mutex_lock(&dev->struct_mutex);
  158. if (obj->pin_display || obj->framebuffer_references) {
  159. ret = -EBUSY;
  160. goto err;
  161. }
  162. if (args->tiling_mode == I915_TILING_NONE) {
  163. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  164. args->stride = 0;
  165. } else {
  166. if (args->tiling_mode == I915_TILING_X)
  167. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  168. else
  169. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  170. /* Hide bit 17 swizzling from the user. This prevents old Mesa
  171. * from aborting the application on sw fallbacks to bit 17,
  172. * and we use the pread/pwrite bit17 paths to swizzle for it.
  173. * If there was a user that was relying on the swizzle
  174. * information for drm_intel_bo_map()ed reads/writes this would
  175. * break it, but we don't have any of those.
  176. */
  177. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  178. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  179. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  180. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  181. /* If we can't handle the swizzling, make it untiled. */
  182. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
  183. args->tiling_mode = I915_TILING_NONE;
  184. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  185. args->stride = 0;
  186. }
  187. }
  188. if (args->tiling_mode != obj->tiling_mode ||
  189. args->stride != obj->stride) {
  190. /* We need to rebind the object if its current allocation
  191. * no longer meets the alignment restrictions for its new
  192. * tiling mode. Otherwise we can just leave it alone, but
  193. * need to ensure that any fence register is updated before
  194. * the next fenced (either through the GTT or by the BLT unit
  195. * on older GPUs) access.
  196. *
  197. * After updating the tiling parameters, we then flag whether
  198. * we need to update an associated fence register. Note this
  199. * has to also include the unfenced register the GPU uses
  200. * whilst executing a fenced command for an untiled object.
  201. */
  202. if (obj->map_and_fenceable &&
  203. !i915_gem_object_fence_ok(obj, args->tiling_mode))
  204. ret = i915_gem_object_ggtt_unbind(obj);
  205. if (ret == 0) {
  206. if (obj->pages &&
  207. obj->madv == I915_MADV_WILLNEED &&
  208. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  209. if (args->tiling_mode == I915_TILING_NONE)
  210. i915_gem_object_unpin_pages(obj);
  211. if (obj->tiling_mode == I915_TILING_NONE)
  212. i915_gem_object_pin_pages(obj);
  213. }
  214. obj->fence_dirty =
  215. obj->last_fenced_req ||
  216. obj->fence_reg != I915_FENCE_REG_NONE;
  217. obj->tiling_mode = args->tiling_mode;
  218. obj->stride = args->stride;
  219. /* Force the fence to be reacquired for GTT access */
  220. i915_gem_release_mmap(obj);
  221. }
  222. }
  223. /* we have to maintain this existing ABI... */
  224. args->stride = obj->stride;
  225. args->tiling_mode = obj->tiling_mode;
  226. /* Try to preallocate memory required to save swizzling on put-pages */
  227. if (i915_gem_object_needs_bit17_swizzle(obj)) {
  228. if (obj->bit_17 == NULL) {
  229. obj->bit_17 = kcalloc(BITS_TO_LONGS(obj->base.size >> PAGE_SHIFT),
  230. sizeof(long), GFP_KERNEL);
  231. }
  232. } else {
  233. kfree(obj->bit_17);
  234. obj->bit_17 = NULL;
  235. }
  236. err:
  237. drm_gem_object_unreference(&obj->base);
  238. mutex_unlock(&dev->struct_mutex);
  239. return ret;
  240. }
  241. /**
  242. * i915_gem_get_tiling - IOCTL handler to get tiling mode
  243. * @dev: DRM device
  244. * @data: data pointer for the ioctl
  245. * @file: DRM file for the ioctl call
  246. *
  247. * Returns the current tiling mode and required bit 6 swizzling for the object.
  248. *
  249. * Called by the user via ioctl.
  250. *
  251. * Returns:
  252. * Zero on success, negative errno on failure.
  253. */
  254. int
  255. i915_gem_get_tiling(struct drm_device *dev, void *data,
  256. struct drm_file *file)
  257. {
  258. struct drm_i915_gem_get_tiling *args = data;
  259. struct drm_i915_private *dev_priv = dev->dev_private;
  260. struct drm_i915_gem_object *obj;
  261. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  262. if (&obj->base == NULL)
  263. return -ENOENT;
  264. mutex_lock(&dev->struct_mutex);
  265. args->tiling_mode = obj->tiling_mode;
  266. switch (obj->tiling_mode) {
  267. case I915_TILING_X:
  268. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  269. break;
  270. case I915_TILING_Y:
  271. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  272. break;
  273. case I915_TILING_NONE:
  274. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  275. break;
  276. default:
  277. DRM_ERROR("unknown tiling mode\n");
  278. }
  279. /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
  280. if (dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  281. args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN;
  282. else
  283. args->phys_swizzle_mode = args->swizzle_mode;
  284. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  285. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  286. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  287. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  288. drm_gem_object_unreference(&obj->base);
  289. mutex_unlock(&dev->struct_mutex);
  290. return 0;
  291. }