intel_guc.c 10 KB

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