i915_gem_evict.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright © 2016 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. */
  24. #include "../i915_selftest.h"
  25. #include "mock_gem_device.h"
  26. static int populate_ggtt(struct drm_i915_private *i915)
  27. {
  28. struct drm_i915_gem_object *obj;
  29. u64 size;
  30. for (size = 0;
  31. size + I915_GTT_PAGE_SIZE <= i915->ggtt.base.total;
  32. size += I915_GTT_PAGE_SIZE) {
  33. struct i915_vma *vma;
  34. obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
  35. if (IS_ERR(obj))
  36. return PTR_ERR(obj);
  37. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
  38. if (IS_ERR(vma))
  39. return PTR_ERR(vma);
  40. }
  41. if (!list_empty(&i915->mm.unbound_list)) {
  42. size = 0;
  43. list_for_each_entry(obj, &i915->mm.unbound_list, global_link)
  44. size++;
  45. pr_err("Found %lld objects unbound!\n", size);
  46. return -EINVAL;
  47. }
  48. if (list_empty(&i915->ggtt.base.inactive_list)) {
  49. pr_err("No objects on the GGTT inactive list!\n");
  50. return -EINVAL;
  51. }
  52. return 0;
  53. }
  54. static void unpin_ggtt(struct drm_i915_private *i915)
  55. {
  56. struct i915_vma *vma;
  57. list_for_each_entry(vma, &i915->ggtt.base.inactive_list, vm_link)
  58. i915_vma_unpin(vma);
  59. }
  60. static void cleanup_objects(struct drm_i915_private *i915)
  61. {
  62. struct drm_i915_gem_object *obj, *on;
  63. list_for_each_entry_safe(obj, on, &i915->mm.unbound_list, global_link)
  64. i915_gem_object_put(obj);
  65. list_for_each_entry_safe(obj, on, &i915->mm.bound_list, global_link)
  66. i915_gem_object_put(obj);
  67. mutex_unlock(&i915->drm.struct_mutex);
  68. i915_gem_drain_freed_objects(i915);
  69. mutex_lock(&i915->drm.struct_mutex);
  70. }
  71. static int igt_evict_something(void *arg)
  72. {
  73. struct drm_i915_private *i915 = arg;
  74. struct i915_ggtt *ggtt = &i915->ggtt;
  75. int err;
  76. /* Fill the GGTT with pinned objects and try to evict one. */
  77. err = populate_ggtt(i915);
  78. if (err)
  79. goto cleanup;
  80. /* Everything is pinned, nothing should happen */
  81. err = i915_gem_evict_something(&ggtt->base,
  82. I915_GTT_PAGE_SIZE, 0, 0,
  83. 0, U64_MAX,
  84. 0);
  85. if (err != -ENOSPC) {
  86. pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
  87. err);
  88. goto cleanup;
  89. }
  90. unpin_ggtt(i915);
  91. /* Everything is unpinned, we should be able to evict something */
  92. err = i915_gem_evict_something(&ggtt->base,
  93. I915_GTT_PAGE_SIZE, 0, 0,
  94. 0, U64_MAX,
  95. 0);
  96. if (err) {
  97. pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
  98. err);
  99. goto cleanup;
  100. }
  101. cleanup:
  102. cleanup_objects(i915);
  103. return err;
  104. }
  105. static int igt_overcommit(void *arg)
  106. {
  107. struct drm_i915_private *i915 = arg;
  108. struct drm_i915_gem_object *obj;
  109. struct i915_vma *vma;
  110. int err;
  111. /* Fill the GGTT with pinned objects and then try to pin one more.
  112. * We expect it to fail.
  113. */
  114. err = populate_ggtt(i915);
  115. if (err)
  116. goto cleanup;
  117. obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
  118. if (IS_ERR(obj)) {
  119. err = PTR_ERR(obj);
  120. goto cleanup;
  121. }
  122. list_move(&obj->global_link, &i915->mm.unbound_list);
  123. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
  124. if (!IS_ERR(vma) || PTR_ERR(vma) != -ENOSPC) {
  125. pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR(vma));
  126. err = -EINVAL;
  127. goto cleanup;
  128. }
  129. cleanup:
  130. cleanup_objects(i915);
  131. return err;
  132. }
  133. static int igt_evict_for_vma(void *arg)
  134. {
  135. struct drm_i915_private *i915 = arg;
  136. struct i915_ggtt *ggtt = &i915->ggtt;
  137. struct drm_mm_node target = {
  138. .start = 0,
  139. .size = 4096,
  140. };
  141. int err;
  142. /* Fill the GGTT with pinned objects and try to evict a range. */
  143. err = populate_ggtt(i915);
  144. if (err)
  145. goto cleanup;
  146. /* Everything is pinned, nothing should happen */
  147. err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
  148. if (err != -ENOSPC) {
  149. pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
  150. err);
  151. goto cleanup;
  152. }
  153. unpin_ggtt(i915);
  154. /* Everything is unpinned, we should be able to evict the node */
  155. err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
  156. if (err) {
  157. pr_err("i915_gem_evict_for_node returned err=%d\n",
  158. err);
  159. goto cleanup;
  160. }
  161. cleanup:
  162. cleanup_objects(i915);
  163. return err;
  164. }
  165. static void mock_color_adjust(const struct drm_mm_node *node,
  166. unsigned long color,
  167. u64 *start,
  168. u64 *end)
  169. {
  170. }
  171. static int igt_evict_for_cache_color(void *arg)
  172. {
  173. struct drm_i915_private *i915 = arg;
  174. struct i915_ggtt *ggtt = &i915->ggtt;
  175. const unsigned long flags = PIN_OFFSET_FIXED;
  176. struct drm_mm_node target = {
  177. .start = I915_GTT_PAGE_SIZE * 2,
  178. .size = I915_GTT_PAGE_SIZE,
  179. .color = I915_CACHE_LLC,
  180. };
  181. struct drm_i915_gem_object *obj;
  182. struct i915_vma *vma;
  183. int err;
  184. /* Currently the use of color_adjust is limited to cache domains within
  185. * the ggtt, and so the presence of mm.color_adjust is assumed to be
  186. * i915_gtt_color_adjust throughout our driver, so using a mock color
  187. * adjust will work just fine for our purposes.
  188. */
  189. ggtt->base.mm.color_adjust = mock_color_adjust;
  190. obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
  191. if (IS_ERR(obj)) {
  192. err = PTR_ERR(obj);
  193. goto cleanup;
  194. }
  195. i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  196. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  197. I915_GTT_PAGE_SIZE | flags);
  198. if (IS_ERR(vma)) {
  199. pr_err("[0]i915_gem_object_ggtt_pin failed\n");
  200. err = PTR_ERR(vma);
  201. goto cleanup;
  202. }
  203. obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
  204. if (IS_ERR(obj)) {
  205. err = PTR_ERR(obj);
  206. goto cleanup;
  207. }
  208. i915_gem_object_set_cache_level(obj, I915_CACHE_LLC);
  209. /* Neighbouring; same colour - should fit */
  210. vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
  211. (I915_GTT_PAGE_SIZE * 2) | flags);
  212. if (IS_ERR(vma)) {
  213. pr_err("[1]i915_gem_object_ggtt_pin failed\n");
  214. err = PTR_ERR(vma);
  215. goto cleanup;
  216. }
  217. i915_vma_unpin(vma);
  218. /* Remove just the second vma */
  219. err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
  220. if (err) {
  221. pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
  222. goto cleanup;
  223. }
  224. /* Attempt to remove the first *pinned* vma, by removing the (empty)
  225. * neighbour -- this should fail.
  226. */
  227. target.color = I915_CACHE_L3_LLC;
  228. err = i915_gem_evict_for_node(&ggtt->base, &target, 0);
  229. if (!err) {
  230. pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
  231. err = -EINVAL;
  232. goto cleanup;
  233. }
  234. err = 0;
  235. cleanup:
  236. unpin_ggtt(i915);
  237. cleanup_objects(i915);
  238. ggtt->base.mm.color_adjust = NULL;
  239. return err;
  240. }
  241. static int igt_evict_vm(void *arg)
  242. {
  243. struct drm_i915_private *i915 = arg;
  244. struct i915_ggtt *ggtt = &i915->ggtt;
  245. int err;
  246. /* Fill the GGTT with pinned objects and try to evict everything. */
  247. err = populate_ggtt(i915);
  248. if (err)
  249. goto cleanup;
  250. /* Everything is pinned, nothing should happen */
  251. err = i915_gem_evict_vm(&ggtt->base, false);
  252. if (err) {
  253. pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
  254. err);
  255. goto cleanup;
  256. }
  257. unpin_ggtt(i915);
  258. err = i915_gem_evict_vm(&ggtt->base, false);
  259. if (err) {
  260. pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
  261. err);
  262. goto cleanup;
  263. }
  264. cleanup:
  265. cleanup_objects(i915);
  266. return err;
  267. }
  268. int i915_gem_evict_mock_selftests(void)
  269. {
  270. static const struct i915_subtest tests[] = {
  271. SUBTEST(igt_evict_something),
  272. SUBTEST(igt_evict_for_vma),
  273. SUBTEST(igt_evict_for_cache_color),
  274. SUBTEST(igt_evict_vm),
  275. SUBTEST(igt_overcommit),
  276. };
  277. struct drm_i915_private *i915;
  278. int err;
  279. i915 = mock_gem_device();
  280. if (!i915)
  281. return -ENOMEM;
  282. mutex_lock(&i915->drm.struct_mutex);
  283. err = i915_subtests(tests, i915);
  284. mutex_unlock(&i915->drm.struct_mutex);
  285. drm_dev_unref(&i915->drm);
  286. return err;
  287. }