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_power_of_2(stride))
  143. return false;
  144. }
  145. if (IS_GEN2(i915) ||
  146. (tiling == I915_TILING_Y && HAS_128_BYTE_Y_TILING(i915)))
  147. tile_width = 128;
  148. else
  149. tile_width = 512;
  150. if (!stride || !IS_ALIGNED(stride, tile_width))
  151. return false;
  152. return true;
  153. }
  154. static bool i915_vma_fence_prepare(struct i915_vma *vma,
  155. int tiling_mode, unsigned int stride)
  156. {
  157. struct drm_i915_private *i915 = vma->vm->i915;
  158. u32 size, alignment;
  159. if (!i915_vma_is_map_and_fenceable(vma))
  160. return true;
  161. size = i915_gem_fence_size(i915, vma->size, tiling_mode, stride);
  162. if (vma->node.size < size)
  163. return false;
  164. alignment = i915_gem_fence_alignment(i915, vma->size, tiling_mode, stride);
  165. if (!IS_ALIGNED(vma->node.start, alignment))
  166. return false;
  167. return true;
  168. }
  169. /* Make the current GTT allocation valid for the change in tiling. */
  170. static int
  171. i915_gem_object_fence_prepare(struct drm_i915_gem_object *obj,
  172. int tiling_mode, unsigned int stride)
  173. {
  174. struct i915_vma *vma;
  175. int ret;
  176. if (tiling_mode == I915_TILING_NONE)
  177. return 0;
  178. for_each_ggtt_vma(vma, obj) {
  179. if (i915_vma_fence_prepare(vma, tiling_mode, stride))
  180. continue;
  181. ret = i915_vma_unbind(vma);
  182. if (ret)
  183. return ret;
  184. }
  185. return 0;
  186. }
  187. int
  188. i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
  189. unsigned int tiling, unsigned int stride)
  190. {
  191. struct drm_i915_private *i915 = to_i915(obj->base.dev);
  192. struct i915_vma *vma;
  193. int err;
  194. /* Make sure we don't cross-contaminate obj->tiling_and_stride */
  195. BUILD_BUG_ON(I915_TILING_LAST & STRIDE_MASK);
  196. GEM_BUG_ON(!i915_tiling_ok(obj, tiling, stride));
  197. GEM_BUG_ON(!stride ^ (tiling == I915_TILING_NONE));
  198. lockdep_assert_held(&i915->drm.struct_mutex);
  199. if ((tiling | stride) == obj->tiling_and_stride)
  200. return 0;
  201. if (i915_gem_object_is_framebuffer(obj))
  202. return -EBUSY;
  203. /* We need to rebind the object if its current allocation
  204. * no longer meets the alignment restrictions for its new
  205. * tiling mode. Otherwise we can just leave it alone, but
  206. * need to ensure that any fence register is updated before
  207. * the next fenced (either through the GTT or by the BLT unit
  208. * on older GPUs) access.
  209. *
  210. * After updating the tiling parameters, we then flag whether
  211. * we need to update an associated fence register. Note this
  212. * has to also include the unfenced register the GPU uses
  213. * whilst executing a fenced command for an untiled object.
  214. */
  215. err = i915_gem_object_fence_prepare(obj, tiling, stride);
  216. if (err)
  217. return err;
  218. i915_gem_object_lock(obj);
  219. if (i915_gem_object_is_framebuffer(obj)) {
  220. i915_gem_object_unlock(obj);
  221. return -EBUSY;
  222. }
  223. /* If the memory has unknown (i.e. varying) swizzling, we pin the
  224. * pages to prevent them being swapped out and causing corruption
  225. * due to the change in swizzling.
  226. */
  227. mutex_lock(&obj->mm.lock);
  228. if (i915_gem_object_has_pages(obj) &&
  229. obj->mm.madv == I915_MADV_WILLNEED &&
  230. i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  231. if (tiling == 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. for_each_ggtt_vma(vma, obj) {
  244. vma->fence_size =
  245. i915_gem_fence_size(i915, vma->size, tiling, stride);
  246. vma->fence_alignment =
  247. i915_gem_fence_alignment(i915,
  248. vma->size, tiling, stride);
  249. if (vma->fence)
  250. vma->fence->dirty = true;
  251. }
  252. obj->tiling_and_stride = tiling | stride;
  253. i915_gem_object_unlock(obj);
  254. /* Force the fence to be reacquired for GTT access */
  255. i915_gem_release_mmap(obj);
  256. /* Try to preallocate memory required to save swizzling on put-pages */
  257. if (i915_gem_object_needs_bit17_swizzle(obj)) {
  258. if (!obj->bit_17) {
  259. obj->bit_17 = kcalloc(BITS_TO_LONGS(obj->base.size >> PAGE_SHIFT),
  260. sizeof(long), GFP_KERNEL);
  261. }
  262. } else {
  263. kfree(obj->bit_17);
  264. obj->bit_17 = NULL;
  265. }
  266. return 0;
  267. }
  268. /**
  269. * i915_gem_set_tiling_ioctl - IOCTL handler to set tiling mode
  270. * @dev: DRM device
  271. * @data: data pointer for the ioctl
  272. * @file: DRM file for the ioctl call
  273. *
  274. * Sets the tiling mode of an object, returning the required swizzling of
  275. * bit 6 of addresses in the object.
  276. *
  277. * Called by the user via ioctl.
  278. *
  279. * Returns:
  280. * Zero on success, negative errno on failure.
  281. */
  282. int
  283. i915_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
  284. struct drm_file *file)
  285. {
  286. struct drm_i915_gem_set_tiling *args = data;
  287. struct drm_i915_gem_object *obj;
  288. int err;
  289. obj = i915_gem_object_lookup(file, args->handle);
  290. if (!obj)
  291. return -ENOENT;
  292. /*
  293. * The tiling mode of proxy objects is handled by its generator, and
  294. * not allowed to be changed by userspace.
  295. */
  296. if (i915_gem_object_is_proxy(obj)) {
  297. err = -ENXIO;
  298. goto err;
  299. }
  300. if (!i915_tiling_ok(obj, args->tiling_mode, args->stride)) {
  301. err = -EINVAL;
  302. goto err;
  303. }
  304. if (args->tiling_mode == I915_TILING_NONE) {
  305. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  306. args->stride = 0;
  307. } else {
  308. if (args->tiling_mode == I915_TILING_X)
  309. args->swizzle_mode = to_i915(dev)->mm.bit_6_swizzle_x;
  310. else
  311. args->swizzle_mode = to_i915(dev)->mm.bit_6_swizzle_y;
  312. /* Hide bit 17 swizzling from the user. This prevents old Mesa
  313. * from aborting the application on sw fallbacks to bit 17,
  314. * and we use the pread/pwrite bit17 paths to swizzle for it.
  315. * If there was a user that was relying on the swizzle
  316. * information for drm_intel_bo_map()ed reads/writes this would
  317. * break it, but we don't have any of those.
  318. */
  319. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  320. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  321. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  322. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  323. /* If we can't handle the swizzling, make it untiled. */
  324. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_UNKNOWN) {
  325. args->tiling_mode = I915_TILING_NONE;
  326. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  327. args->stride = 0;
  328. }
  329. }
  330. err = mutex_lock_interruptible(&dev->struct_mutex);
  331. if (err)
  332. goto err;
  333. err = i915_gem_object_set_tiling(obj, args->tiling_mode, args->stride);
  334. mutex_unlock(&dev->struct_mutex);
  335. /* We have to maintain this existing ABI... */
  336. args->stride = i915_gem_object_get_stride(obj);
  337. args->tiling_mode = i915_gem_object_get_tiling(obj);
  338. err:
  339. i915_gem_object_put(obj);
  340. return err;
  341. }
  342. /**
  343. * i915_gem_get_tiling_ioctl - IOCTL handler to get tiling mode
  344. * @dev: DRM device
  345. * @data: data pointer for the ioctl
  346. * @file: DRM file for the ioctl call
  347. *
  348. * Returns the current tiling mode and required bit 6 swizzling for the object.
  349. *
  350. * Called by the user via ioctl.
  351. *
  352. * Returns:
  353. * Zero on success, negative errno on failure.
  354. */
  355. int
  356. i915_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
  357. struct drm_file *file)
  358. {
  359. struct drm_i915_gem_get_tiling *args = data;
  360. struct drm_i915_private *dev_priv = to_i915(dev);
  361. struct drm_i915_gem_object *obj;
  362. int err = -ENOENT;
  363. rcu_read_lock();
  364. obj = i915_gem_object_lookup_rcu(file, args->handle);
  365. if (obj) {
  366. args->tiling_mode =
  367. READ_ONCE(obj->tiling_and_stride) & TILING_MASK;
  368. err = 0;
  369. }
  370. rcu_read_unlock();
  371. if (unlikely(err))
  372. return err;
  373. switch (args->tiling_mode) {
  374. case I915_TILING_X:
  375. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_x;
  376. break;
  377. case I915_TILING_Y:
  378. args->swizzle_mode = dev_priv->mm.bit_6_swizzle_y;
  379. break;
  380. default:
  381. case I915_TILING_NONE:
  382. args->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  383. break;
  384. }
  385. /* Hide bit 17 from the user -- see comment in i915_gem_set_tiling */
  386. if (dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  387. args->phys_swizzle_mode = I915_BIT_6_SWIZZLE_UNKNOWN;
  388. else
  389. args->phys_swizzle_mode = args->swizzle_mode;
  390. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_17)
  391. args->swizzle_mode = I915_BIT_6_SWIZZLE_9;
  392. if (args->swizzle_mode == I915_BIT_6_SWIZZLE_9_10_17)
  393. args->swizzle_mode = I915_BIT_6_SWIZZLE_9_10;
  394. return 0;
  395. }