i915_gem_tiling.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 (tiling_mode > I915_TILING_LAST)
  67. return false;
  68. if (IS_GEN2(dev) ||
  69. (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
  70. tile_width = 128;
  71. else
  72. tile_width = 512;
  73. /* check maximum stride & object size */
  74. /* i965+ stores the end address of the gtt mapping in the fence
  75. * reg, so dont bother to check the size */
  76. if (INTEL_INFO(dev)->gen >= 7) {
  77. if (stride / 128 > GEN7_FENCE_MAX_PITCH_VAL)
  78. return false;
  79. } else if (INTEL_INFO(dev)->gen >= 4) {
  80. if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
  81. return false;
  82. } else {
  83. if (stride > 8192)
  84. return false;
  85. if (IS_GEN3(dev)) {
  86. if (size > I830_FENCE_MAX_SIZE_VAL << 20)
  87. return false;
  88. } else {
  89. if (size > I830_FENCE_MAX_SIZE_VAL << 19)
  90. return false;
  91. }
  92. }
  93. if (stride < tile_width)
  94. return false;
  95. /* 965+ just needs multiples of tile width */
  96. if (INTEL_INFO(dev)->gen >= 4) {
  97. if (stride & (tile_width - 1))
  98. return false;
  99. return true;
  100. }
  101. /* Pre-965 needs power of two tile widths */
  102. if (stride & (stride - 1))
  103. return false;
  104. return true;
  105. }
  106. static bool i915_vma_fence_prepare(struct i915_vma *vma, int tiling_mode)
  107. {
  108. struct drm_i915_private *dev_priv = to_i915(vma->vm->dev);
  109. u32 size;
  110. if (!i915_vma_is_map_and_fenceable(vma))
  111. return true;
  112. if (INTEL_GEN(dev_priv) == 3) {
  113. if (vma->node.start & ~I915_FENCE_START_MASK)
  114. return false;
  115. } else {
  116. if (vma->node.start & ~I830_FENCE_START_MASK)
  117. return false;
  118. }
  119. size = i915_gem_get_ggtt_size(dev_priv, vma->size, tiling_mode);
  120. if (vma->node.size < size)
  121. return false;
  122. if (vma->node.start & (size - 1))
  123. return false;
  124. return true;
  125. }
  126. /* Make the current GTT allocation valid for the change in tiling. */
  127. static int
  128. i915_gem_object_fence_prepare(struct drm_i915_gem_object *obj, int tiling_mode)
  129. {
  130. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  131. struct i915_vma *vma;
  132. int ret;
  133. if (tiling_mode == I915_TILING_NONE)
  134. return 0;
  135. if (INTEL_GEN(dev_priv) >= 4)
  136. return 0;
  137. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  138. if (i915_vma_fence_prepare(vma, tiling_mode))
  139. continue;
  140. ret = i915_vma_unbind(vma);
  141. if (ret)
  142. return ret;
  143. }
  144. return 0;
  145. }
  146. /**
  147. * i915_gem_set_tiling - IOCTL handler to set tiling mode
  148. * @dev: DRM device
  149. * @data: data pointer for the ioctl
  150. * @file: DRM file for the ioctl call
  151. *
  152. * Sets the tiling mode of an object, returning the required swizzling of
  153. * bit 6 of addresses in the object.
  154. *
  155. * Called by the user via ioctl.
  156. *
  157. * Returns:
  158. * Zero on success, negative errno on failure.
  159. */
  160. int
  161. i915_gem_set_tiling(struct drm_device *dev, void *data,
  162. struct drm_file *file)
  163. {
  164. struct drm_i915_gem_set_tiling *args = data;
  165. struct drm_i915_private *dev_priv = to_i915(dev);
  166. struct drm_i915_gem_object *obj;
  167. int err = 0;
  168. /* Make sure we don't cross-contaminate obj->tiling_and_stride */
  169. BUILD_BUG_ON(I915_TILING_LAST & STRIDE_MASK);
  170. obj = i915_gem_object_lookup(file, args->handle);
  171. if (!obj)
  172. return -ENOENT;
  173. if (!i915_tiling_ok(dev,
  174. args->stride, obj->base.size, args->tiling_mode)) {
  175. i915_gem_object_put_unlocked(obj);
  176. return -EINVAL;
  177. }
  178. intel_runtime_pm_get(dev_priv);
  179. mutex_lock(&dev->struct_mutex);
  180. if (obj->pin_display || obj->framebuffer_references) {
  181. err = -EBUSY;
  182. goto err;
  183. }
  184. if (args->tiling_mode == I915_TILING_NONE) {
  185. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  186. args->stride = 0;
  187. } else {
  188. if (args->tiling_mode == I915_TILING_X)
  189. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  190. else
  191. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  192. /* Hide bit 17 swizzling from the user. This prevents old Mesa
  193. * from aborting the application on sw fallbacks to bit 17,
  194. * and we use the pread/pwrite bit17 paths to swizzle for it.
  195. * If there was a user that was relying on the swizzle
  196. * information for drm_intel_bo_map()ed reads/writes this would
  197. * break it, but we don't have any of those.
  198. */
  199. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  200. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  201. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  202. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  203. /* If we can't handle the swizzling, make it untiled. */
  204. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
  205. args->tiling_mode = I915_TILING_NONE;
  206. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  207. args->stride = 0;
  208. }
  209. }
  210. if (args->tiling_mode != i915_gem_object_get_tiling(obj) ||
  211. args->stride != i915_gem_object_get_stride(obj)) {
  212. /* We need to rebind the object if its current allocation
  213. * no longer meets the alignment restrictions for its new
  214. * tiling mode. Otherwise we can just leave it alone, but
  215. * need to ensure that any fence register is updated before
  216. * the next fenced (either through the GTT or by the BLT unit
  217. * on older GPUs) access.
  218. *
  219. * After updating the tiling parameters, we then flag whether
  220. * we need to update an associated fence register. Note this
  221. * has to also include the unfenced register the GPU uses
  222. * whilst executing a fenced command for an untiled object.
  223. */
  224. err = i915_gem_object_fence_prepare(obj, args->tiling_mode);
  225. if (!err) {
  226. struct i915_vma *vma;
  227. if (obj->pages &&
  228. obj->madv == I915_MADV_WILLNEED &&
  229. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  230. if (args->tiling_mode == I915_TILING_NONE)
  231. i915_gem_object_unpin_pages(obj);
  232. if (!i915_gem_object_is_tiled(obj))
  233. i915_gem_object_pin_pages(obj);
  234. }
  235. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  236. if (!vma->fence)
  237. continue;
  238. vma->fence->dirty = true;
  239. }
  240. obj->tiling_and_stride =
  241. args->stride | args->tiling_mode;
  242. /* Force the fence to be reacquired for GTT access */
  243. i915_gem_release_mmap(obj);
  244. }
  245. }
  246. /* we have to maintain this existing ABI... */
  247. args->stride = i915_gem_object_get_stride(obj);
  248. args->tiling_mode = i915_gem_object_get_tiling(obj);
  249. /* Try to preallocate memory required to save swizzling on put-pages */
  250. if (i915_gem_object_needs_bit17_swizzle(obj)) {
  251. if (obj->bit_17 == NULL) {
  252. obj->bit_17 = kcalloc(BITS_TO_LONGS(obj->base.size >> PAGE_SHIFT),
  253. sizeof(long), GFP_KERNEL);
  254. }
  255. } else {
  256. kfree(obj->bit_17);
  257. obj->bit_17 = NULL;
  258. }
  259. err:
  260. i915_gem_object_put(obj);
  261. mutex_unlock(&dev->struct_mutex);
  262. intel_runtime_pm_put(dev_priv);
  263. return err;
  264. }
  265. /**
  266. * i915_gem_get_tiling - IOCTL handler to get tiling mode
  267. * @dev: DRM device
  268. * @data: data pointer for the ioctl
  269. * @file: DRM file for the ioctl call
  270. *
  271. * Returns the current tiling mode and required bit 6 swizzling for the object.
  272. *
  273. * Called by the user via ioctl.
  274. *
  275. * Returns:
  276. * Zero on success, negative errno on failure.
  277. */
  278. int
  279. i915_gem_get_tiling(struct drm_device *dev, void *data,
  280. struct drm_file *file)
  281. {
  282. struct drm_i915_gem_get_tiling *args = data;
  283. struct drm_i915_private *dev_priv = to_i915(dev);
  284. struct drm_i915_gem_object *obj;
  285. obj = i915_gem_object_lookup(file, args->handle);
  286. if (!obj)
  287. return -ENOENT;
  288. args->tiling_mode = READ_ONCE(obj->tiling_and_stride) & TILING_MASK;
  289. switch (args->tiling_mode) {
  290. case I915_TILING_X:
  291. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  292. break;
  293. case I915_TILING_Y:
  294. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  295. break;
  296. case I915_TILING_NONE:
  297. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  298. break;
  299. default:
  300. DRM_ERROR("unknown tiling mode\n");
  301. }
  302. /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
  303. if (dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  304. args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN;
  305. else
  306. args->phys_swizzle_mode = args->swizzle_mode;
  307. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  308. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  309. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  310. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  311. i915_gem_object_put_unlocked(obj);
  312. return 0;
  313. }