i915_gem_tiling.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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_ioctl() and i915_gem_get_tiling_ioctl() is the userspace
  36. * interface to 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. /**
  59. * i915_gem_fence_size - required global GTT size for a fence
  60. * @i915: i915 device
  61. * @size: object size
  62. * @tiling: tiling mode
  63. * @stride: tiling stride
  64. *
  65. * Return the required global GTT size for a fence (view of a tiled object),
  66. * taking into account potential fence register mapping.
  67. */
  68. u32 i915_gem_fence_size(struct drm_i915_private *i915,
  69. u32 size, unsigned int tiling, unsigned int stride)
  70. {
  71. u32 ggtt_size;
  72. GEM_BUG_ON(!size);
  73. if (tiling == I915_TILING_NONE)
  74. return size;
  75. GEM_BUG_ON(!stride);
  76. if (INTEL_GEN(i915) >= 4) {
  77. stride *= i915_gem_tile_height(tiling);
  78. GEM_BUG_ON(!IS_ALIGNED(stride, I965_FENCE_PAGE));
  79. return roundup(size, stride);
  80. }
  81. /* Previous chips need a power-of-two fence region when tiling */
  82. if (IS_GEN3(i915))
  83. ggtt_size = 1024*1024;
  84. else
  85. ggtt_size = 512*1024;
  86. while (ggtt_size < size)
  87. ggtt_size <<= 1;
  88. return ggtt_size;
  89. }
  90. /**
  91. * i915_gem_fence_alignment - required global GTT alignment for a fence
  92. * @i915: i915 device
  93. * @size: object size
  94. * @tiling: tiling mode
  95. * @stride: tiling stride
  96. *
  97. * Return the required global GTT alignment for a fence (a view of a tiled
  98. * object), taking into account potential fence register mapping.
  99. */
  100. u32 i915_gem_fence_alignment(struct drm_i915_private *i915, u32 size,
  101. unsigned int tiling, unsigned int stride)
  102. {
  103. GEM_BUG_ON(!size);
  104. /*
  105. * Minimum alignment is 4k (GTT page size), but might be greater
  106. * if a fence register is needed for the object.
  107. */
  108. if (tiling == I915_TILING_NONE)
  109. return I915_GTT_MIN_ALIGNMENT;
  110. if (INTEL_GEN(i915) >= 4)
  111. return I965_FENCE_PAGE;
  112. /*
  113. * Previous chips need to be aligned to the size of the smallest
  114. * fence register that can contain the object.
  115. */
  116. return i915_gem_fence_size(i915, size, tiling, stride);
  117. }
  118. /* Check pitch constriants for all chips & tiling formats */
  119. static bool
  120. i915_tiling_ok(struct drm_i915_gem_object *obj,
  121. unsigned int tiling, unsigned int stride)
  122. {
  123. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  124. unsigned int tile_width;
  125. /* Linear is always fine */
  126. if (tiling == I915_TILING_NONE)
  127. return true;
  128. if (tiling > I915_TILING_LAST)
  129. return false;
  130. /* check maximum stride & object size */
  131. /* i965+ stores the end address of the gtt mapping in the fence
  132. * reg, so dont bother to check the size */
  133. if (INTEL_GEN(i915) >= 7) {
  134. if (stride / 128 > GEN7_FENCE_MAX_PITCH_VAL)
  135. return false;
  136. } else if (INTEL_GEN(i915) >= 4) {
  137. if (stride / 128 > I965_FENCE_MAX_PITCH_VAL)
  138. return false;
  139. } else {
  140. if (stride > 8192)
  141. return false;
  142. if (IS_GEN3(i915)) {
  143. if (obj->base.size > I830_FENCE_MAX_SIZE_VAL << 20)
  144. return false;
  145. } else {
  146. if (obj->base.size > I830_FENCE_MAX_SIZE_VAL << 19)
  147. return false;
  148. }
  149. }
  150. if (IS_GEN2(i915) ||
  151. (tiling == I915_TILING_Y && HAS_128_BYTE_Y_TILING(i915)))
  152. tile_width = 128;
  153. else
  154. tile_width = 512;
  155. if (!IS_ALIGNED(stride, tile_width))
  156. return false;
  157. /* 965+ just needs multiples of tile width */
  158. if (INTEL_GEN(i915) >= 4)
  159. return true;
  160. /* Pre-965 needs power of two tile widths */
  161. return is_power_of_2(stride);
  162. }
  163. static bool i915_vma_fence_prepare(struct i915_vma *vma,
  164. int tiling_mode, unsigned int stride)
  165. {
  166. struct drm_i915_private *i915 = vma->vm->i915;
  167. u32 size, alignment;
  168. if (!i915_vma_is_map_and_fenceable(vma))
  169. return true;
  170. size = i915_gem_fence_size(i915, vma->size, tiling_mode, stride);
  171. if (vma->node.size < size)
  172. return false;
  173. alignment = i915_gem_fence_alignment(i915, vma->size, tiling_mode, stride);
  174. if (!IS_ALIGNED(vma->node.start, alignment))
  175. return false;
  176. return true;
  177. }
  178. /* Make the current GTT allocation valid for the change in tiling. */
  179. static int
  180. i915_gem_object_fence_prepare(struct drm_i915_gem_object *obj,
  181. int tiling_mode, unsigned int stride)
  182. {
  183. struct i915_vma *vma;
  184. int ret;
  185. if (tiling_mode == I915_TILING_NONE)
  186. return 0;
  187. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  188. if (!i915_vma_is_ggtt(vma))
  189. break;
  190. if (i915_vma_fence_prepare(vma, tiling_mode, stride))
  191. continue;
  192. ret = i915_vma_unbind(vma);
  193. if (ret)
  194. return ret;
  195. }
  196. return 0;
  197. }
  198. int
  199. i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
  200. unsigned int tiling, unsigned int stride)
  201. {
  202. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  203. struct i915_vma *vma;
  204. int err;
  205. /* Make sure we don't cross-contaminate obj->tiling_and_stride */
  206. BUILD_BUG_ON(I915_TILING_LAST & STRIDE_MASK);
  207. GEM_BUG_ON(!i915_tiling_ok(obj, tiling, stride));
  208. GEM_BUG_ON(!stride ^ (tiling == I915_TILING_NONE));
  209. lockdep_assert_held(&i915->drm.struct_mutex);
  210. if ((tiling | stride) == obj->tiling_and_stride)
  211. return 0;
  212. if (obj->framebuffer_references)
  213. return -EBUSY;
  214. /* We need to rebind the object if its current allocation
  215. * no longer meets the alignment restrictions for its new
  216. * tiling mode. Otherwise we can just leave it alone, but
  217. * need to ensure that any fence register is updated before
  218. * the next fenced (either through the GTT or by the BLT unit
  219. * on older GPUs) access.
  220. *
  221. * After updating the tiling parameters, we then flag whether
  222. * we need to update an associated fence register. Note this
  223. * has to also include the unfenced register the GPU uses
  224. * whilst executing a fenced command for an untiled object.
  225. */
  226. err = i915_gem_object_fence_prepare(obj, tiling, stride);
  227. if (err)
  228. return err;
  229. /* If the memory has unknown (i.e. varying) swizzling, we pin the
  230. * pages to prevent them being swapped out and causing corruption
  231. * due to the change in swizzling.
  232. */
  233. mutex_lock(&obj->mm.lock);
  234. if (obj->mm.pages &&
  235. obj->mm.madv == I915_MADV_WILLNEED &&
  236. i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  237. if (tiling == I915_TILING_NONE) {
  238. GEM_BUG_ON(!obj->mm.quirked);
  239. __i915_gem_object_unpin_pages(obj);
  240. obj->mm.quirked = false;
  241. }
  242. if (!i915_gem_object_is_tiled(obj)) {
  243. GEM_BUG_ON(!obj->mm.quirked);
  244. __i915_gem_object_pin_pages(obj);
  245. obj->mm.quirked = true;
  246. }
  247. }
  248. mutex_unlock(&obj->mm.lock);
  249. list_for_each_entry(vma, &obj->vma_list, obj_link) {
  250. if (!i915_vma_is_ggtt(vma))
  251. break;
  252. vma->fence_size =
  253. i915_gem_fence_size(i915, vma->size, tiling, stride);
  254. vma->fence_alignment =
  255. i915_gem_fence_alignment(i915,
  256. vma->size, tiling, stride);
  257. if (vma->fence)
  258. vma->fence->dirty = true;
  259. }
  260. obj->tiling_and_stride = tiling | stride;
  261. /* Force the fence to be reacquired for GTT access */
  262. i915_gem_release_mmap(obj);
  263. /* Try to preallocate memory required to save swizzling on put-pages */
  264. if (i915_gem_object_needs_bit17_swizzle(obj)) {
  265. if (!obj->bit_17) {
  266. obj->bit_17 = kcalloc(BITS_TO_LONGS(obj->base.size >> PAGE_SHIFT),
  267. sizeof(long), GFP_KERNEL);
  268. }
  269. } else {
  270. kfree(obj->bit_17);
  271. obj->bit_17 = NULL;
  272. }
  273. return 0;
  274. }
  275. /**
  276. * i915_gem_set_tiling_ioctl - IOCTL handler to set tiling mode
  277. * @dev: DRM device
  278. * @data: data pointer for the ioctl
  279. * @file: DRM file for the ioctl call
  280. *
  281. * Sets the tiling mode of an object, returning the required swizzling of
  282. * bit 6 of addresses in the object.
  283. *
  284. * Called by the user via ioctl.
  285. *
  286. * Returns:
  287. * Zero on success, negative errno on failure.
  288. */
  289. int
  290. i915_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
  291. struct drm_file *file)
  292. {
  293. struct drm_i915_gem_set_tiling *args = data;
  294. struct drm_i915_gem_object *obj;
  295. int err;
  296. obj = i915_gem_object_lookup(file, args->handle);
  297. if (!obj)
  298. return -ENOENT;
  299. if (!i915_tiling_ok(obj, args->tiling_mode, args->stride)) {
  300. err = -EINVAL;
  301. goto err;
  302. }
  303. if (args->tiling_mode == I915_TILING_NONE) {
  304. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  305. args->stride = 0;
  306. } else {
  307. if (args->tiling_mode == I915_TILING_X)
  308. args->swizzle_mode = to_i915(dev)->mm.bit_6_swizzle_x;
  309. else
  310. args->swizzle_mode = to_i915(dev)->mm.bit_6_swizzle_y;
  311. /* Hide bit 17 swizzling from the user. This prevents old Mesa
  312. * from aborting the application on sw fallbacks to bit 17,
  313. * and we use the pread/pwrite bit17 paths to swizzle for it.
  314. * If there was a user that was relying on the swizzle
  315. * information for drm_intel_bo_map()ed reads/writes this would
  316. * break it, but we don't have any of those.
  317. */
  318. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  319. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  320. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  321. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  322. /* If we can't handle the swizzling, make it untiled. */
  323. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
  324. args->tiling_mode = I915_TILING_NONE;
  325. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  326. args->stride = 0;
  327. }
  328. }
  329. err = mutex_lock_interruptible(&dev->struct_mutex);
  330. if (err)
  331. goto err;
  332. err = i915_gem_object_set_tiling(obj, args->tiling_mode, args->stride);
  333. mutex_unlock(&dev->struct_mutex);
  334. /* We have to maintain this existing ABI... */
  335. args->stride = i915_gem_object_get_stride(obj);
  336. args->tiling_mode = i915_gem_object_get_tiling(obj);
  337. err:
  338. i915_gem_object_put(obj);
  339. return err;
  340. }
  341. /**
  342. * i915_gem_get_tiling_ioctl - IOCTL handler to get tiling mode
  343. * @dev: DRM device
  344. * @data: data pointer for the ioctl
  345. * @file: DRM file for the ioctl call
  346. *
  347. * Returns the current tiling mode and required bit 6 swizzling for the object.
  348. *
  349. * Called by the user via ioctl.
  350. *
  351. * Returns:
  352. * Zero on success, negative errno on failure.
  353. */
  354. int
  355. i915_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
  356. struct drm_file *file)
  357. {
  358. struct drm_i915_gem_get_tiling *args = data;
  359. struct drm_i915_private *dev_priv = to_i915(dev);
  360. struct drm_i915_gem_object *obj;
  361. int err = -ENOENT;
  362. rcu_read_lock();
  363. obj = i915_gem_object_lookup_rcu(file, args->handle);
  364. if (obj) {
  365. args->tiling_mode =
  366. READ_ONCE(obj->tiling_and_stride) & TILING_MASK;
  367. err = 0;
  368. }
  369. rcu_read_unlock();
  370. if (unlikely(err))
  371. return err;
  372. switch (args->tiling_mode) {
  373. case I915_TILING_X:
  374. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  375. break;
  376. case I915_TILING_Y:
  377. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  378. break;
  379. default:
  380. case I915_TILING_NONE:
  381. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  382. break;
  383. }
  384. /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
  385. if (dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  386. args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN;
  387. else
  388. args->phys_swizzle_mode = args->swizzle_mode;
  389. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  390. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  391. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  392. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  393. return 0;
  394. }