intel_guc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 !client || 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. err = validate_client(guc->execbuf_client,
  122. GUC_CLIENT_PRIORITY_KMD_NORMAL, false);
  123. if (err) {
  124. pr_err("execbug client validation failed\n");
  125. goto out;
  126. }
  127. if (guc->preempt_client) {
  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. }
  135. /* each client should now have reserved a doorbell */
  136. if (!has_doorbell(guc->execbuf_client) ||
  137. (guc->preempt_client && !has_doorbell(guc->preempt_client))) {
  138. pr_err("guc_clients_create didn't reserve doorbells\n");
  139. err = -EINVAL;
  140. goto out;
  141. }
  142. /* Now create the doorbells */
  143. guc_clients_doorbell_init(guc);
  144. /* each client should now have received a doorbell */
  145. if (!client_doorbell_in_sync(guc->execbuf_client) ||
  146. !client_doorbell_in_sync(guc->preempt_client)) {
  147. pr_err("failed to initialize the doorbells\n");
  148. err = -EINVAL;
  149. goto out;
  150. }
  151. /*
  152. * Basic test - an attempt to reallocate a valid doorbell to the
  153. * client it is currently assigned should not cause a failure.
  154. */
  155. err = guc_clients_doorbell_init(guc);
  156. if (err)
  157. goto out;
  158. /*
  159. * Negative test - a client with no doorbell (invalid db id).
  160. * After destroying the doorbell, the db id is changed to
  161. * GUC_DOORBELL_INVALID and the firmware will reject any attempt to
  162. * allocate a doorbell with an invalid id (db has to be reserved before
  163. * allocation).
  164. */
  165. destroy_doorbell(guc->execbuf_client);
  166. if (client_doorbell_in_sync(guc->execbuf_client)) {
  167. pr_err("destroy db did not work\n");
  168. err = -EINVAL;
  169. goto out;
  170. }
  171. unreserve_doorbell(guc->execbuf_client);
  172. err = guc_clients_doorbell_init(guc);
  173. if (err != -EIO) {
  174. pr_err("unexpected (err = %d)", err);
  175. goto out;
  176. }
  177. if (!available_dbs(guc, guc->execbuf_client->priority)) {
  178. pr_err("doorbell not available when it should\n");
  179. err = -EIO;
  180. goto out;
  181. }
  182. /* clean after test */
  183. err = reserve_doorbell(guc->execbuf_client);
  184. if (err) {
  185. pr_err("failed to reserve back the doorbell back\n");
  186. }
  187. err = create_doorbell(guc->execbuf_client);
  188. if (err) {
  189. pr_err("recreate doorbell failed\n");
  190. goto out;
  191. }
  192. out:
  193. /*
  194. * Leave clean state for other test, plus the driver always destroy the
  195. * clients during unload.
  196. */
  197. destroy_doorbell(guc->execbuf_client);
  198. if (guc->preempt_client)
  199. destroy_doorbell(guc->preempt_client);
  200. guc_clients_destroy(guc);
  201. guc_clients_create(guc);
  202. guc_clients_doorbell_init(guc);
  203. unlock:
  204. mutex_unlock(&dev_priv->drm.struct_mutex);
  205. return err;
  206. }
  207. /*
  208. * Create as many clients as number of doorbells. Note that there's already
  209. * client(s)/doorbell(s) created during driver load, but this test creates
  210. * its own and do not interact with the existing ones.
  211. */
  212. static int igt_guc_doorbells(void *arg)
  213. {
  214. struct drm_i915_private *dev_priv = arg;
  215. struct intel_guc *guc;
  216. int i, err = 0;
  217. u16 db_id;
  218. GEM_BUG_ON(!HAS_GUC(dev_priv));
  219. mutex_lock(&dev_priv->drm.struct_mutex);
  220. guc = &dev_priv->guc;
  221. if (!guc) {
  222. pr_err("No guc object!\n");
  223. err = -EINVAL;
  224. goto unlock;
  225. }
  226. err = check_all_doorbells(guc);
  227. if (err)
  228. goto unlock;
  229. for (i = 0; i < ATTEMPTS; i++) {
  230. clients[i] = guc_client_alloc(dev_priv,
  231. INTEL_INFO(dev_priv)->ring_mask,
  232. i % GUC_CLIENT_PRIORITY_NUM,
  233. dev_priv->kernel_context);
  234. if (!clients[i]) {
  235. pr_err("[%d] No guc client\n", i);
  236. err = -EINVAL;
  237. goto out;
  238. }
  239. if (IS_ERR(clients[i])) {
  240. if (PTR_ERR(clients[i]) != -ENOSPC) {
  241. pr_err("[%d] unexpected error\n", i);
  242. err = PTR_ERR(clients[i]);
  243. goto out;
  244. }
  245. if (available_dbs(guc, i % GUC_CLIENT_PRIORITY_NUM)) {
  246. pr_err("[%d] non-db related alloc fail\n", i);
  247. err = -EINVAL;
  248. goto out;
  249. }
  250. /* expected, ran out of dbs for this client type */
  251. continue;
  252. }
  253. /*
  254. * The check below is only valid because we keep a doorbell
  255. * assigned during the whole life of the client.
  256. */
  257. if (clients[i]->stage_id >= GUC_NUM_DOORBELLS) {
  258. pr_err("[%d] more clients than doorbells (%d >= %d)\n",
  259. i, clients[i]->stage_id, GUC_NUM_DOORBELLS);
  260. err = -EINVAL;
  261. goto out;
  262. }
  263. err = validate_client(clients[i],
  264. i % GUC_CLIENT_PRIORITY_NUM, false);
  265. if (err) {
  266. pr_err("[%d] client_alloc sanity check failed!\n", i);
  267. err = -EINVAL;
  268. goto out;
  269. }
  270. db_id = clients[i]->doorbell_id;
  271. err = create_doorbell(clients[i]);
  272. if (err) {
  273. pr_err("[%d] Failed to create a doorbell\n", i);
  274. goto out;
  275. }
  276. /* doorbell id shouldn't change, we are holding the mutex */
  277. if (db_id != clients[i]->doorbell_id) {
  278. pr_err("[%d] doorbell id changed (%d != %d)\n",
  279. i, db_id, clients[i]->doorbell_id);
  280. err = -EINVAL;
  281. goto out;
  282. }
  283. err = check_all_doorbells(guc);
  284. if (err)
  285. goto out;
  286. }
  287. out:
  288. for (i = 0; i < ATTEMPTS; i++)
  289. if (!IS_ERR_OR_NULL(clients[i])) {
  290. destroy_doorbell(clients[i]);
  291. guc_client_free(clients[i]);
  292. }
  293. unlock:
  294. mutex_unlock(&dev_priv->drm.struct_mutex);
  295. return err;
  296. }
  297. int intel_guc_live_selftest(struct drm_i915_private *dev_priv)
  298. {
  299. static const struct i915_subtest tests[] = {
  300. SUBTEST(igt_guc_clients),
  301. SUBTEST(igt_guc_doorbells),
  302. };
  303. if (!USES_GUC_SUBMISSION(dev_priv))
  304. return 0;
  305. return i915_subtests(tests, dev_priv);
  306. }