st-dma-resv.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. * Copyright © 2021 Advanced Micro Devices, Inc.
  5. */
  6. #include <linux/slab.h>
  7. #include <linux/spinlock.h>
  8. #include <linux/dma-resv.h>
  9. #include "selftest.h"
  10. static struct spinlock fence_lock;
  11. static const char *fence_name(struct dma_fence *f)
  12. {
  13. return "selftest";
  14. }
  15. static const struct dma_fence_ops fence_ops = {
  16. .get_driver_name = fence_name,
  17. .get_timeline_name = fence_name,
  18. };
  19. static struct dma_fence *alloc_fence(void)
  20. {
  21. struct dma_fence *f;
  22. f = kmalloc(sizeof(*f), GFP_KERNEL);
  23. if (!f)
  24. return NULL;
  25. dma_fence_init(f, &fence_ops, &fence_lock, 0, 0);
  26. return f;
  27. }
  28. static int sanitycheck(void *arg)
  29. {
  30. struct dma_resv resv;
  31. struct dma_fence *f;
  32. int r;
  33. f = alloc_fence();
  34. if (!f)
  35. return -ENOMEM;
  36. dma_fence_enable_sw_signaling(f);
  37. dma_fence_signal(f);
  38. dma_fence_put(f);
  39. dma_resv_init(&resv);
  40. r = dma_resv_lock(&resv, NULL);
  41. if (r)
  42. pr_err("Resv locking failed\n");
  43. else
  44. dma_resv_unlock(&resv);
  45. dma_resv_fini(&resv);
  46. return r;
  47. }
  48. static int test_signaling(void *arg)
  49. {
  50. enum dma_resv_usage usage = (unsigned long)arg;
  51. struct dma_resv resv;
  52. struct dma_fence *f;
  53. int r;
  54. f = alloc_fence();
  55. if (!f)
  56. return -ENOMEM;
  57. dma_fence_enable_sw_signaling(f);
  58. dma_resv_init(&resv);
  59. r = dma_resv_lock(&resv, NULL);
  60. if (r) {
  61. pr_err("Resv locking failed\n");
  62. goto err_free;
  63. }
  64. r = dma_resv_reserve_fences(&resv, 1);
  65. if (r) {
  66. pr_err("Resv shared slot allocation failed\n");
  67. goto err_unlock;
  68. }
  69. dma_resv_add_fence(&resv, f, usage);
  70. if (dma_resv_test_signaled(&resv, usage)) {
  71. pr_err("Resv unexpectedly signaled\n");
  72. r = -EINVAL;
  73. goto err_unlock;
  74. }
  75. dma_fence_signal(f);
  76. if (!dma_resv_test_signaled(&resv, usage)) {
  77. pr_err("Resv not reporting signaled\n");
  78. r = -EINVAL;
  79. goto err_unlock;
  80. }
  81. err_unlock:
  82. dma_resv_unlock(&resv);
  83. err_free:
  84. dma_resv_fini(&resv);
  85. dma_fence_put(f);
  86. return r;
  87. }
  88. static int test_for_each(void *arg)
  89. {
  90. enum dma_resv_usage usage = (unsigned long)arg;
  91. struct dma_resv_iter cursor;
  92. struct dma_fence *f, *fence;
  93. struct dma_resv resv;
  94. int r;
  95. f = alloc_fence();
  96. if (!f)
  97. return -ENOMEM;
  98. dma_fence_enable_sw_signaling(f);
  99. dma_resv_init(&resv);
  100. r = dma_resv_lock(&resv, NULL);
  101. if (r) {
  102. pr_err("Resv locking failed\n");
  103. goto err_free;
  104. }
  105. r = dma_resv_reserve_fences(&resv, 1);
  106. if (r) {
  107. pr_err("Resv shared slot allocation failed\n");
  108. goto err_unlock;
  109. }
  110. dma_resv_add_fence(&resv, f, usage);
  111. r = -ENOENT;
  112. dma_resv_for_each_fence(&cursor, &resv, usage, fence) {
  113. if (!r) {
  114. pr_err("More than one fence found\n");
  115. r = -EINVAL;
  116. goto err_unlock;
  117. }
  118. if (f != fence) {
  119. pr_err("Unexpected fence\n");
  120. r = -EINVAL;
  121. goto err_unlock;
  122. }
  123. if (dma_resv_iter_usage(&cursor) != usage) {
  124. pr_err("Unexpected fence usage\n");
  125. r = -EINVAL;
  126. goto err_unlock;
  127. }
  128. r = 0;
  129. }
  130. if (r) {
  131. pr_err("No fence found\n");
  132. goto err_unlock;
  133. }
  134. dma_fence_signal(f);
  135. err_unlock:
  136. dma_resv_unlock(&resv);
  137. err_free:
  138. dma_resv_fini(&resv);
  139. dma_fence_put(f);
  140. return r;
  141. }
  142. static int test_for_each_unlocked(void *arg)
  143. {
  144. enum dma_resv_usage usage = (unsigned long)arg;
  145. struct dma_resv_iter cursor;
  146. struct dma_fence *f, *fence;
  147. struct dma_resv resv;
  148. int r;
  149. f = alloc_fence();
  150. if (!f)
  151. return -ENOMEM;
  152. dma_fence_enable_sw_signaling(f);
  153. dma_resv_init(&resv);
  154. r = dma_resv_lock(&resv, NULL);
  155. if (r) {
  156. pr_err("Resv locking failed\n");
  157. goto err_free;
  158. }
  159. r = dma_resv_reserve_fences(&resv, 1);
  160. if (r) {
  161. pr_err("Resv shared slot allocation failed\n");
  162. dma_resv_unlock(&resv);
  163. goto err_free;
  164. }
  165. dma_resv_add_fence(&resv, f, usage);
  166. dma_resv_unlock(&resv);
  167. r = -ENOENT;
  168. dma_resv_iter_begin(&cursor, &resv, usage);
  169. dma_resv_for_each_fence_unlocked(&cursor, fence) {
  170. if (!r) {
  171. pr_err("More than one fence found\n");
  172. r = -EINVAL;
  173. goto err_iter_end;
  174. }
  175. if (!dma_resv_iter_is_restarted(&cursor)) {
  176. pr_err("No restart flag\n");
  177. goto err_iter_end;
  178. }
  179. if (f != fence) {
  180. pr_err("Unexpected fence\n");
  181. r = -EINVAL;
  182. goto err_iter_end;
  183. }
  184. if (dma_resv_iter_usage(&cursor) != usage) {
  185. pr_err("Unexpected fence usage\n");
  186. r = -EINVAL;
  187. goto err_iter_end;
  188. }
  189. /* We use r as state here */
  190. if (r == -ENOENT) {
  191. r = -EINVAL;
  192. /* That should trigger an restart */
  193. cursor.fences = (void*)~0;
  194. } else if (r == -EINVAL) {
  195. r = 0;
  196. }
  197. }
  198. if (r)
  199. pr_err("No fence found\n");
  200. err_iter_end:
  201. dma_resv_iter_end(&cursor);
  202. dma_fence_signal(f);
  203. err_free:
  204. dma_resv_fini(&resv);
  205. dma_fence_put(f);
  206. return r;
  207. }
  208. static int test_get_fences(void *arg)
  209. {
  210. enum dma_resv_usage usage = (unsigned long)arg;
  211. struct dma_fence *f, **fences = NULL;
  212. struct dma_resv resv;
  213. int r, i;
  214. f = alloc_fence();
  215. if (!f)
  216. return -ENOMEM;
  217. dma_fence_enable_sw_signaling(f);
  218. dma_resv_init(&resv);
  219. r = dma_resv_lock(&resv, NULL);
  220. if (r) {
  221. pr_err("Resv locking failed\n");
  222. goto err_resv;
  223. }
  224. r = dma_resv_reserve_fences(&resv, 1);
  225. if (r) {
  226. pr_err("Resv shared slot allocation failed\n");
  227. dma_resv_unlock(&resv);
  228. goto err_resv;
  229. }
  230. dma_resv_add_fence(&resv, f, usage);
  231. dma_resv_unlock(&resv);
  232. r = dma_resv_get_fences(&resv, usage, &i, &fences);
  233. if (r) {
  234. pr_err("get_fences failed\n");
  235. goto err_free;
  236. }
  237. if (i != 1 || fences[0] != f) {
  238. pr_err("get_fences returned unexpected fence\n");
  239. goto err_free;
  240. }
  241. dma_fence_signal(f);
  242. err_free:
  243. while (i--)
  244. dma_fence_put(fences[i]);
  245. kfree(fences);
  246. err_resv:
  247. dma_resv_fini(&resv);
  248. dma_fence_put(f);
  249. return r;
  250. }
  251. int dma_resv(void)
  252. {
  253. static const struct subtest tests[] = {
  254. SUBTEST(sanitycheck),
  255. SUBTEST(test_signaling),
  256. SUBTEST(test_for_each),
  257. SUBTEST(test_for_each_unlocked),
  258. SUBTEST(test_get_fences),
  259. };
  260. enum dma_resv_usage usage;
  261. int r;
  262. spin_lock_init(&fence_lock);
  263. for (usage = DMA_RESV_USAGE_KERNEL; usage <= DMA_RESV_USAGE_BOOKKEEP;
  264. ++usage) {
  265. r = subtests(tests, (void *)(unsigned long)usage);
  266. if (r)
  267. return r;
  268. }
  269. return 0;
  270. }