i915_gem_tiling.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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_i915_private *dev_priv,
  61. int stride, int size, int tiling_mode)
  62. {
  63. int tile_width;
  64. /* Linear is always fine */
  65. if (tiling_mode == I915_TILING_NONE)
  66. return true;
  67. if (tiling_mode > I915_TILING_LAST)
  68. return false;
  69. if (IS_GEN2(dev_priv) ||
  70. (tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev_priv)))
  71. tile_width = 128;
  72. else
  73. tile_width = 512;
  74. /* check maximum stride & object size */
  75. /* i965+ stores the end address of the gtt mapping in the fence
  76. * reg, so dont bother to check the size */
  77. if (INTEL_GEN(dev_priv) >= 7) {
  78. if (stride / 128 > GEN7_FENCE_MAX_PITCH_VAL)
  79. return false;
  80. } else if (INTEL_GEN(dev_priv) >= 4) {
  81. if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
  82. return false;
  83. } else {
  84. if (stride > 8192)
  85. return false;
  86. if (IS_GEN3(dev_priv)) {
  87. if (size > I830_FENCE_MAX_SIZE_VAL << 20)
  88. return false;
  89. } else {
  90. if (size > I830_FENCE_MAX_SIZE_VAL << 19)
  91. return false;
  92. }
  93. }
  94. if (stride < tile_width)
  95. return false;
  96. /* 965+ just needs multiples of tile width */
  97. if (INTEL_GEN(dev_priv) >= 4) {
  98. if (stride & (tile_width - 1))
  99. return false;
  100. return true;
  101. }
  102. /* Pre-965 needs power of two tile widths */
  103. if (stride & (stride - 1))
  104. return false;
  105. return true;
  106. }
  107. static bool i915_vma_fence_prepare(struct i915_vma *vma, int tiling_mode)
  108. {
  109. struct drm_i915_private *dev_priv = vma->vm->i915;
  110. u32 size;
  111. if (!i915_vma_is_map_and_fenceable(vma))
  112. return true;
  113. if (INTEL_GEN(dev_priv) == 3) {
  114. if (vma->node.start & ~I915_FENCE_START_MASK)
  115. return false;
  116. } else {
  117. if (vma->node.start & ~I830_FENCE_START_MASK)
  118. return false;
  119. }
  120. size = i915_gem_get_ggtt_size(dev_priv, vma->size, tiling_mode);
  121. if (vma->node.size < size)
  122. return false;
  123. if (vma->node.start & (size - 1))
  124. return false;
  125. return true;
  126. }
  127. /* Make the current GTT allocation valid for the change in tiling. */
  128. static int
  129. i915_gem_object_fence_prepare(struct drm_i915_gem_object *obj, int tiling_mode)
  130. {
  131. struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
  132. struct i915_vma *vma;
  133. int ret;
  134. if (tiling_mode == I915_TILING_NONE)
  135. return 0;
  136. if (INTEL_GEN(dev_priv) >= 4)
  137. return 0;
  138. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  139. if (i915_vma_fence_prepare(vma, tiling_mode))
  140. continue;
  141. ret = i915_vma_unbind(vma);
  142. if (ret)
  143. return ret;
  144. }
  145. return 0;
  146. }
  147. /**
  148. * i915_gem_set_tiling - IOCTL handler to set tiling mode
  149. * @dev: DRM device
  150. * @data: data pointer for the ioctl
  151. * @file: DRM file for the ioctl call
  152. *
  153. * Sets the tiling mode of an object, returning the required swizzling of
  154. * bit 6 of addresses in the object.
  155. *
  156. * Called by the user via ioctl.
  157. *
  158. * Returns:
  159. * Zero on success, negative errno on failure.
  160. */
  161. int
  162. i915_gem_set_tiling(struct drm_device *dev, void *data,
  163. struct drm_file *file)
  164. {
  165. struct drm_i915_gem_set_tiling *args = data;
  166. struct drm_i915_private *dev_priv = to_i915(dev);
  167. struct drm_i915_gem_object *obj;
  168. int err = 0;
  169. /* Make sure we don't cross-contaminate obj->tiling_and_stride */
  170. BUILD_BUG_ON(I915_TILING_LAST & STRIDE_MASK);
  171. obj = i915_gem_object_lookup(file, args->handle);
  172. if (!obj)
  173. return -ENOENT;
  174. if (!i915_tiling_ok(dev_priv,
  175. args->stride, obj->base.size, args->tiling_mode)) {
  176. i915_gem_object_put(obj);
  177. return -EINVAL;
  178. }
  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. mutex_lock(&obj->mm.lock);
  228. if (obj->mm.pages &&
  229. obj->mm.madv == I915_MADV_WILLNEED &&
  230. dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  231. if (args->tiling_mode == I915_TILING_NONE) {
  232. GEM_BUG_ON(!obj->mm.quirked);
  233. __i915_gem_object_unpin_pages(obj);
  234. obj->mm.quirked = false;
  235. }
  236. if (!i915_gem_object_is_tiled(obj)) {
  237. GEM_BUG_ON(!obj->mm.quirked);
  238. __i915_gem_object_pin_pages(obj);
  239. obj->mm.quirked = true;
  240. }
  241. }
  242. mutex_unlock(&obj->mm.lock);
  243. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  244. if (!vma->fence)
  245. continue;
  246. vma->fence->dirty = true;
  247. }
  248. obj->tiling_and_stride =
  249. args->stride | args->tiling_mode;
  250. /* Force the fence to be reacquired for GTT access */
  251. i915_gem_release_mmap(obj);
  252. }
  253. }
  254. /* we have to maintain this existing ABI... */
  255. args->stride = i915_gem_object_get_stride(obj);
  256. args->tiling_mode = i915_gem_object_get_tiling(obj);
  257. /* Try to preallocate memory required to save swizzling on put-pages */
  258. if (i915_gem_object_needs_bit17_swizzle(obj)) {
  259. if (obj->bit_17 == NULL) {
  260. obj->bit_17 = kcalloc(BITS_TO_LONGS(obj->base.size >> PAGE_SHIFT),
  261. sizeof(long), GFP_KERNEL);
  262. }
  263. } else {
  264. kfree(obj->bit_17);
  265. obj->bit_17 = NULL;
  266. }
  267. err:
  268. i915_gem_object_put(obj);
  269. mutex_unlock(&dev->struct_mutex);
  270. return err;
  271. }
  272. /**
  273. * i915_gem_get_tiling - IOCTL handler to get tiling mode
  274. * @dev: DRM device
  275. * @data: data pointer for the ioctl
  276. * @file: DRM file for the ioctl call
  277. *
  278. * Returns the current tiling mode and required bit 6 swizzling for the object.
  279. *
  280. * Called by the user via ioctl.
  281. *
  282. * Returns:
  283. * Zero on success, negative errno on failure.
  284. */
  285. int
  286. i915_gem_get_tiling(struct drm_device *dev, void *data,
  287. struct drm_file *file)
  288. {
  289. struct drm_i915_gem_get_tiling *args = data;
  290. struct drm_i915_private *dev_priv = to_i915(dev);
  291. struct drm_i915_gem_object *obj;
  292. int err = -ENOENT;
  293. rcu_read_lock();
  294. obj = i915_gem_object_lookup_rcu(file, args->handle);
  295. if (obj) {
  296. args->tiling_mode =
  297. READ_ONCE(obj->tiling_and_stride) & TILING_MASK;
  298. err = 0;
  299. }
  300. rcu_read_unlock();
  301. if (unlikely(err))
  302. return err;
  303. switch (args->tiling_mode) {
  304. case I915_TILING_X:
  305. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  306. break;
  307. case I915_TILING_Y:
  308. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  309. break;
  310. default:
  311. case I915_TILING_NONE:
  312. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  313. break;
  314. }
  315. /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
  316. if (dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  317. args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN;
  318. else
  319. args->phys_swizzle_mode = args->swizzle_mode;
  320. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  321. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  322. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  323. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  324. return 0;
  325. }