i915_gem_tiling.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. intel_runtime_pm_get(dev_priv);
  158. mutex_lock(&dev->struct_mutex);
  159. if (obj->pin_display || obj->framebuffer_references) {
  160. ret = -EBUSY;
  161. goto err;
  162. }
  163. if (args->tiling_mode == I915_TILING_NONE) {
  164. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  165. args->stride = 0;
  166. } else {
  167. if (args->tiling_mode == I915_TILING_X)
  168. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  169. else
  170. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  171. /* Hide bit 17 swizzling from the user. This prevents old Mesa
  172. * from aborting the application on sw fallbacks to bit 17,
  173. * and we use the pread/pwrite bit17 paths to swizzle for it.
  174. * If there was a user that was relying on the swizzle
  175. * information for drm_intel_bo_map()ed reads/writes this would
  176. * break it, but we don't have any of those.
  177. */
  178. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  179. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  180. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  181. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  182. /* If we can't handle the swizzling, make it untiled. */
  183. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
  184. args->tiling_mode = I915_TILING_NONE;
  185. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  186. args->stride = 0;
  187. }
  188. }
  189. if (args->tiling_mode != obj->tiling_mode ||
  190. args->stride != obj->stride) {
  191. /* We need to rebind the object if its current allocation
  192. * no longer meets the alignment restrictions for its new
  193. * tiling mode. Otherwise we can just leave it alone, but
  194. * need to ensure that any fence register is updated before
  195. * the next fenced (either through the GTT or by the BLT unit
  196. * on older GPUs) access.
  197. *
  198. * After updating the tiling parameters, we then flag whether
  199. * we need to update an associated fence register. Note this
  200. * has to also include the unfenced register the GPU uses
  201. * whilst executing a fenced command for an untiled object.
  202. */
  203. if (obj->map_and_fenceable &&
  204. !i915_gem_object_fence_ok(obj, args->tiling_mode))
  205. ret = i915_gem_object_ggtt_unbind(obj);
  206. if (ret == 0) {
  207. if (obj->pages &&
  208. obj->madv == I915_MADV_WILLNEED &&
  209. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  210. if (args->tiling_mode == I915_TILING_NONE)
  211. i915_gem_object_unpin_pages(obj);
  212. if (obj->tiling_mode == I915_TILING_NONE)
  213. i915_gem_object_pin_pages(obj);
  214. }
  215. obj->fence_dirty =
  216. obj->last_fenced_req ||
  217. obj->fence_reg != I915_FENCE_REG_NONE;
  218. obj->tiling_mode = args->tiling_mode;
  219. obj->stride = args->stride;
  220. /* Force the fence to be reacquired for GTT access */
  221. i915_gem_release_mmap(obj);
  222. }
  223. }
  224. /* we have to maintain this existing ABI... */
  225. args->stride = obj->stride;
  226. args->tiling_mode = obj->tiling_mode;
  227. /* Try to preallocate memory required to save swizzling on put-pages */
  228. if (i915_gem_object_needs_bit17_swizzle(obj)) {
  229. if (obj->bit_17 == NULL) {
  230. obj->bit_17 = kcalloc(BITS_TO_LONGS(obj->base.size >> PAGE_SHIFT),
  231. sizeof(long), GFP_KERNEL);
  232. }
  233. } else {
  234. kfree(obj->bit_17);
  235. obj->bit_17 = NULL;
  236. }
  237. err:
  238. drm_gem_object_unreference(&obj->base);
  239. mutex_unlock(&dev->struct_mutex);
  240. intel_runtime_pm_put(dev_priv);
  241. return ret;
  242. }
  243. /**
  244. * i915_gem_get_tiling - IOCTL handler to get tiling mode
  245. * @dev: DRM device
  246. * @data: data pointer for the ioctl
  247. * @file: DRM file for the ioctl call
  248. *
  249. * Returns the current tiling mode and required bit 6 swizzling for the object.
  250. *
  251. * Called by the user via ioctl.
  252. *
  253. * Returns:
  254. * Zero on success, negative errno on failure.
  255. */
  256. int
  257. i915_gem_get_tiling(struct drm_device *dev, void *data,
  258. struct drm_file *file)
  259. {
  260. struct drm_i915_gem_get_tiling *args = data;
  261. struct drm_i915_private *dev_priv = dev->dev_private;
  262. struct drm_i915_gem_object *obj;
  263. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  264. if (&obj->base == NULL)
  265. return -ENOENT;
  266. mutex_lock(&dev->struct_mutex);
  267. args->tiling_mode = obj->tiling_mode;
  268. switch (obj->tiling_mode) {
  269. case I915_TILING_X:
  270. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  271. break;
  272. case I915_TILING_Y:
  273. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  274. break;
  275. case I915_TILING_NONE:
  276. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  277. break;
  278. default:
  279. DRM_ERROR("unknown tiling mode\n");
  280. }
  281. /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
  282. if (dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  283. args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN;
  284. else
  285. args->phys_swizzle_mode = args->swizzle_mode;
  286. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  287. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  288. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  289. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  290. drm_gem_object_unreference(&obj->base);
  291. mutex_unlock(&dev->struct_mutex);
  292. return 0;
  293. }