intel_guc.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*
  2. * Copyright © 2017 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. /* max doorbell number + negative test for each client type */
  26. #define ATTEMPTS (GUC_NUM_DOORBELLS + GUC_CLIENT_PRIORITY_NUM)
  27. static struct intel_guc_client *clients[ATTEMPTS];
  28. static bool available_dbs(struct intel_guc *guc, u32 priority)
  29. {
  30. unsigned long offset;
  31. unsigned long end;
  32. u16 id;
  33. /* first half is used for normal priority, second half for high */
  34. offset = 0;
  35. end = GUC_NUM_DOORBELLS / 2;
  36. if (priority <= GUC_CLIENT_PRIORITY_HIGH) {
  37. offset = end;
  38. end += offset;
  39. }
  40. id = find_next_zero_bit(guc->doorbell_bitmap, end, offset);
  41. if (id < end)
  42. return true;
  43. return false;
  44. }
  45. static int check_all_doorbells(struct intel_guc *guc)
  46. {
  47. u16 db_id;
  48. pr_info_once("Max number of doorbells: %d", GUC_NUM_DOORBELLS);
  49. for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id) {
  50. if (!doorbell_ok(guc, db_id)) {
  51. pr_err("doorbell %d, not ok\n", db_id);
  52. return -EIO;
  53. }
  54. }
  55. return 0;
  56. }
  57. /*
  58. * Basic client sanity check, handy to validate create_clients.
  59. */
  60. static int validate_client(struct intel_guc_client *client,
  61. int client_priority,
  62. bool is_preempt_client)
  63. {
  64. struct drm_i915_private *dev_priv = guc_to_i915(client->guc);
  65. struct i915_gem_context *ctx_owner = is_preempt_client ?
  66. dev_priv->preempt_context : dev_priv->kernel_context;
  67. if (client->owner != ctx_owner ||
  68. client->engines != INTEL_INFO(dev_priv)->ring_mask ||
  69. client->priority != client_priority ||
  70. client->doorbell_id == GUC_DOORBELL_INVALID)
  71. return -EINVAL;
  72. else
  73. return 0;
  74. }
  75. static bool client_doorbell_in_sync(struct intel_guc_client *client)
  76. {
  77. return doorbell_ok(client->guc, client->doorbell_id);
  78. }
  79. /*
  80. * Check that we're able to synchronize guc_clients with their doorbells
  81. *
  82. * We're creating clients and reserving doorbells once, at module load. During
  83. * module lifetime, GuC, doorbell HW, and i915 state may go out of sync due to
  84. * GuC being reset. In other words - GuC clients are still around, but the
  85. * status of their doorbells may be incorrect. This is the reason behind
  86. * validating that the doorbells status expected by the driver matches what the
  87. * GuC/HW have.
  88. */
  89. static int igt_guc_clients(void *args)
  90. {
  91. struct drm_i915_private *dev_priv = args;
  92. struct intel_guc *guc;
  93. int err = 0;
  94. GEM_BUG_ON(!HAS_GUC(dev_priv));
  95. mutex_lock(&dev_priv->drm.struct_mutex);
  96. guc = &dev_priv->guc;
  97. if (!guc) {
  98. pr_err("No guc object!\n");
  99. err = -EINVAL;
  100. goto unlock;
  101. }
  102. err = check_all_doorbells(guc);
  103. if (err)
  104. goto unlock;
  105. /*
  106. * Get rid of clients created during driver load because the test will
  107. * recreate them.
  108. */
  109. guc_clients_destroy(guc);
  110. if (guc->execbuf_client || guc->preempt_client) {
  111. pr_err("guc_clients_destroy lied!\n");
  112. err = -EINVAL;
  113. goto unlock;
  114. }
  115. err = guc_clients_create(guc);
  116. if (err) {
  117. pr_err("Failed to create clients\n");
  118. goto unlock;
  119. }
  120. GEM_BUG_ON(!guc->execbuf_client);
  121. GEM_BUG_ON(!guc->preempt_client);
  122. err = validate_client(guc->execbuf_client,
  123. GUC_CLIENT_PRIORITY_KMD_NORMAL, false);
  124. if (err) {
  125. pr_err("execbug client validation failed\n");
  126. goto out;
  127. }
  128. err = validate_client(guc->preempt_client,
  129. GUC_CLIENT_PRIORITY_KMD_HIGH, true);
  130. if (err) {
  131. pr_err("preempt client validation failed\n");
  132. goto out;
  133. }
  134. /* each client should now have reserved a doorbell */
  135. if (!has_doorbell(guc->execbuf_client) ||
  136. !has_doorbell(guc->preempt_client)) {
  137. pr_err("guc_clients_create didn't reserve doorbells\n");
  138. err = -EINVAL;
  139. goto out;
  140. }
  141. /* Now create the doorbells */
  142. guc_clients_doorbell_init(guc);
  143. /* each client should now have received a doorbell */
  144. if (!client_doorbell_in_sync(guc->execbuf_client) ||
  145. !client_doorbell_in_sync(guc->preempt_client)) {
  146. pr_err("failed to initialize the doorbells\n");
  147. err = -EINVAL;
  148. goto out;
  149. }
  150. /*
  151. * Basic test - an attempt to reallocate a valid doorbell to the
  152. * client it is currently assigned should not cause a failure.
  153. */
  154. err = guc_clients_doorbell_init(guc);
  155. if (err)
  156. goto out;
  157. /*
  158. * Negative test - a client with no doorbell (invalid db id).
  159. * After destroying the doorbell, the db id is changed to
  160. * GUC_DOORBELL_INVALID and the firmware will reject any attempt to
  161. * allocate a doorbell with an invalid id (db has to be reserved before
  162. * allocation).
  163. */
  164. destroy_doorbell(guc->execbuf_client);
  165. if (client_doorbell_in_sync(guc->execbuf_client)) {
  166. pr_err("destroy db did not work\n");
  167. err = -EINVAL;
  168. goto out;
  169. }
  170. unreserve_doorbell(guc->execbuf_client);
  171. err = guc_clients_doorbell_init(guc);
  172. if (err != -EIO) {
  173. pr_err("unexpected (err = %d)", err);
  174. goto out;
  175. }
  176. if (!available_dbs(guc, guc->execbuf_client->priority)) {
  177. pr_err("doorbell not available when it should\n");
  178. err = -EIO;
  179. goto out;
  180. }
  181. /* clean after test */
  182. err = reserve_doorbell(guc->execbuf_client);
  183. if (err) {
  184. pr_err("failed to reserve back the doorbell back\n");
  185. }
  186. err = create_doorbell(guc->execbuf_client);
  187. if (err) {
  188. pr_err("recreate doorbell failed\n");
  189. goto out;
  190. }
  191. out:
  192. /*
  193. * Leave clean state for other test, plus the driver always destroy the
  194. * clients during unload.
  195. */
  196. destroy_doorbell(guc->execbuf_client);
  197. destroy_doorbell(guc->preempt_client);
  198. guc_clients_destroy(guc);
  199. guc_clients_create(guc);
  200. guc_clients_doorbell_init(guc);
  201. unlock:
  202. mutex_unlock(&dev_priv->drm.struct_mutex);
  203. return err;
  204. }
  205. /*
  206. * Create as many clients as number of doorbells. Note that there's already
  207. * client(s)/doorbell(s) created during driver load, but this test creates
  208. * its own and do not interact with the existing ones.
  209. */
  210. static int igt_guc_doorbells(void *arg)
  211. {
  212. struct drm_i915_private *dev_priv = arg;
  213. struct intel_guc *guc;
  214. int i, err = 0;
  215. u16 db_id;
  216. GEM_BUG_ON(!HAS_GUC(dev_priv));
  217. mutex_lock(&dev_priv->drm.struct_mutex);
  218. guc = &dev_priv->guc;
  219. if (!guc) {
  220. pr_err("No guc object!\n");
  221. err = -EINVAL;
  222. goto unlock;
  223. }
  224. err = check_all_doorbells(guc);
  225. if (err)
  226. goto unlock;
  227. for (i = 0; i < ATTEMPTS; i++) {
  228. clients[i] = guc_client_alloc(dev_priv,
  229. INTEL_INFO(dev_priv)->ring_mask,
  230. i % GUC_CLIENT_PRIORITY_NUM,
  231. dev_priv->kernel_context);
  232. if (!clients[i]) {
  233. pr_err("[%d] No guc client\n", i);
  234. err = -EINVAL;
  235. goto out;
  236. }
  237. if (IS_ERR(clients[i])) {
  238. if (PTR_ERR(clients[i]) != -ENOSPC) {
  239. pr_err("[%d] unexpected error\n", i);
  240. err = PTR_ERR(clients[i]);
  241. goto out;
  242. }
  243. if (available_dbs(guc, i % GUC_CLIENT_PRIORITY_NUM)) {
  244. pr_err("[%d] non-db related alloc fail\n", i);
  245. err = -EINVAL;
  246. goto out;
  247. }
  248. /* expected, ran out of dbs for this client type */
  249. continue;
  250. }
  251. /*
  252. * The check below is only valid because we keep a doorbell
  253. * assigned during the whole life of the client.
  254. */
  255. if (clients[i]->stage_id >= GUC_NUM_DOORBELLS) {
  256. pr_err("[%d] more clients than doorbells (%d >= %d)\n",
  257. i, clients[i]->stage_id, GUC_NUM_DOORBELLS);
  258. err = -EINVAL;
  259. goto out;
  260. }
  261. err = validate_client(clients[i],
  262. i % GUC_CLIENT_PRIORITY_NUM, false);
  263. if (err) {
  264. pr_err("[%d] client_alloc sanity check failed!\n", i);
  265. err = -EINVAL;
  266. goto out;
  267. }
  268. db_id = clients[i]->doorbell_id;
  269. err = create_doorbell(clients[i]);
  270. if (err) {
  271. pr_err("[%d] Failed to create a doorbell\n", i);
  272. goto out;
  273. }
  274. /* doorbell id shouldn't change, we are holding the mutex */
  275. if (db_id != clients[i]->doorbell_id) {
  276. pr_err("[%d] doorbell id changed (%d != %d)\n",
  277. i, db_id, clients[i]->doorbell_id);
  278. err = -EINVAL;
  279. goto out;
  280. }
  281. err = check_all_doorbells(guc);
  282. if (err)
  283. goto out;
  284. }
  285. out:
  286. for (i = 0; i < ATTEMPTS; i++)
  287. if (!IS_ERR_OR_NULL(clients[i])) {
  288. destroy_doorbell(clients[i]);
  289. guc_client_free(clients[i]);
  290. }
  291. unlock:
  292. mutex_unlock(&dev_priv->drm.struct_mutex);
  293. return err;
  294. }
  295. int intel_guc_live_selftest(struct drm_i915_private *dev_priv)
  296. {
  297. static const struct i915_subtest tests[] = {
  298. SUBTEST(igt_guc_clients),
  299. SUBTEST(igt_guc_doorbells),
  300. };
  301. if (!USES_GUC_SUBMISSION(dev_priv))
  302. return 0;
  303. return i915_subtests(tests, dev_priv);
  304. }