i915_gem_tiling.c 13 KB

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