2 * Copyright © 2017 Intel Corporation
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:
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
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
25 #include "../i915_selftest.h"
27 /* max doorbell number + negative test for each client type */
28 #define ATTEMPTS (GUC_NUM_DOORBELLS + GUC_CLIENT_PRIORITY_NUM)
30 static struct intel_guc_client *clients[ATTEMPTS];
32 static bool available_dbs(struct intel_guc *guc, u32 priority)
38 /* first half is used for normal priority, second half for high */
40 end = GUC_NUM_DOORBELLS / 2;
41 if (priority <= GUC_CLIENT_PRIORITY_HIGH) {
46 id = find_next_zero_bit(guc->doorbell_bitmap, end, offset);
53 static int check_all_doorbells(struct intel_guc *guc)
57 pr_info_once("Max number of doorbells: %d", GUC_NUM_DOORBELLS);
58 for (db_id = 0; db_id < GUC_NUM_DOORBELLS; ++db_id) {
59 if (!doorbell_ok(guc, db_id)) {
60 pr_err("doorbell %d, not ok\n", db_id);
68 static int ring_doorbell_nop(struct intel_guc_client *client)
70 struct guc_process_desc *desc = __get_process_desc(client);
73 client->use_nop_wqi = true;
75 spin_lock_irq(&client->wq_lock);
77 guc_wq_item_append(client, 0, 0, 0, 0);
78 guc_ring_doorbell(client);
80 spin_unlock_irq(&client->wq_lock);
82 client->use_nop_wqi = false;
84 /* if there are no issues GuC will update the WQ head and keep the
87 err = wait_for(READ_ONCE(desc->head) == READ_ONCE(desc->tail), 10);
89 pr_err("doorbell %u ring failed!\n", client->doorbell_id);
93 if (desc->wq_status != WQ_STATUS_ACTIVE) {
94 pr_err("doorbell %u ring put WQ in bad state (%u)!\n",
95 client->doorbell_id, desc->wq_status);
103 * Basic client sanity check, handy to validate create_clients.
105 static int validate_client(struct intel_guc_client *client,
107 bool is_preempt_client)
109 struct drm_i915_private *dev_priv = guc_to_i915(client->guc);
110 struct i915_gem_context *ctx_owner = is_preempt_client ?
111 dev_priv->preempt_context : dev_priv->kernel_context;
113 if (client->owner != ctx_owner ||
114 client->engines != INTEL_INFO(dev_priv)->ring_mask ||
115 client->priority != client_priority ||
116 client->doorbell_id == GUC_DOORBELL_INVALID)
122 static bool client_doorbell_in_sync(struct intel_guc_client *client)
124 return !client || doorbell_ok(client->guc, client->doorbell_id);
128 * Check that we're able to synchronize guc_clients with their doorbells
130 * We're creating clients and reserving doorbells once, at module load. During
131 * module lifetime, GuC, doorbell HW, and i915 state may go out of sync due to
132 * GuC being reset. In other words - GuC clients are still around, but the
133 * status of their doorbells may be incorrect. This is the reason behind
134 * validating that the doorbells status expected by the driver matches what the
137 static int igt_guc_clients(void *args)
139 struct drm_i915_private *dev_priv = args;
140 struct intel_guc *guc;
143 GEM_BUG_ON(!HAS_GUC(dev_priv));
144 mutex_lock(&dev_priv->drm.struct_mutex);
146 guc = &dev_priv->guc;
148 pr_err("No guc object!\n");
153 err = check_all_doorbells(guc);
158 * Get rid of clients created during driver load because the test will
161 guc_clients_destroy(guc);
162 if (guc->execbuf_client || guc->preempt_client) {
163 pr_err("guc_clients_destroy lied!\n");
168 err = guc_clients_create(guc);
170 pr_err("Failed to create clients\n");
173 GEM_BUG_ON(!guc->execbuf_client);
175 err = validate_client(guc->execbuf_client,
176 GUC_CLIENT_PRIORITY_KMD_NORMAL, false);
178 pr_err("execbug client validation failed\n");
182 if (guc->preempt_client) {
183 err = validate_client(guc->preempt_client,
184 GUC_CLIENT_PRIORITY_KMD_HIGH, true);
186 pr_err("preempt client validation failed\n");
191 /* each client should now have reserved a doorbell */
192 if (!has_doorbell(guc->execbuf_client) ||
193 (guc->preempt_client && !has_doorbell(guc->preempt_client))) {
194 pr_err("guc_clients_create didn't reserve doorbells\n");
199 /* Now create the doorbells */
200 guc_clients_doorbell_init(guc);
202 /* each client should now have received a doorbell */
203 if (!client_doorbell_in_sync(guc->execbuf_client) ||
204 !client_doorbell_in_sync(guc->preempt_client)) {
205 pr_err("failed to initialize the doorbells\n");
211 * Basic test - an attempt to reallocate a valid doorbell to the
212 * client it is currently assigned should not cause a failure.
214 err = guc_clients_doorbell_init(guc);
219 * Negative test - a client with no doorbell (invalid db id).
220 * After destroying the doorbell, the db id is changed to
221 * GUC_DOORBELL_INVALID and the firmware will reject any attempt to
222 * allocate a doorbell with an invalid id (db has to be reserved before
225 destroy_doorbell(guc->execbuf_client);
226 if (client_doorbell_in_sync(guc->execbuf_client)) {
227 pr_err("destroy db did not work\n");
232 unreserve_doorbell(guc->execbuf_client);
234 __create_doorbell(guc->execbuf_client);
235 err = __guc_allocate_doorbell(guc, guc->execbuf_client->stage_id);
237 pr_err("unexpected (err = %d)", err);
241 if (!available_dbs(guc, guc->execbuf_client->priority)) {
242 pr_err("doorbell not available when it should\n");
248 /* clean after test */
249 __destroy_doorbell(guc->execbuf_client);
250 err = reserve_doorbell(guc->execbuf_client);
252 pr_err("failed to reserve back the doorbell back\n");
254 err = create_doorbell(guc->execbuf_client);
256 pr_err("recreate doorbell failed\n");
262 * Leave clean state for other test, plus the driver always destroy the
263 * clients during unload.
265 destroy_doorbell(guc->execbuf_client);
266 if (guc->preempt_client)
267 destroy_doorbell(guc->preempt_client);
268 guc_clients_destroy(guc);
269 guc_clients_create(guc);
270 guc_clients_doorbell_init(guc);
272 mutex_unlock(&dev_priv->drm.struct_mutex);
277 * Create as many clients as number of doorbells. Note that there's already
278 * client(s)/doorbell(s) created during driver load, but this test creates
279 * its own and do not interact with the existing ones.
281 static int igt_guc_doorbells(void *arg)
283 struct drm_i915_private *dev_priv = arg;
284 struct intel_guc *guc;
288 GEM_BUG_ON(!HAS_GUC(dev_priv));
289 mutex_lock(&dev_priv->drm.struct_mutex);
291 guc = &dev_priv->guc;
293 pr_err("No guc object!\n");
298 err = check_all_doorbells(guc);
302 for (i = 0; i < ATTEMPTS; i++) {
303 clients[i] = guc_client_alloc(dev_priv,
304 INTEL_INFO(dev_priv)->ring_mask,
305 i % GUC_CLIENT_PRIORITY_NUM,
306 dev_priv->kernel_context);
309 pr_err("[%d] No guc client\n", i);
314 if (IS_ERR(clients[i])) {
315 if (PTR_ERR(clients[i]) != -ENOSPC) {
316 pr_err("[%d] unexpected error\n", i);
317 err = PTR_ERR(clients[i]);
321 if (available_dbs(guc, i % GUC_CLIENT_PRIORITY_NUM)) {
322 pr_err("[%d] non-db related alloc fail\n", i);
327 /* expected, ran out of dbs for this client type */
332 * The check below is only valid because we keep a doorbell
333 * assigned during the whole life of the client.
335 if (clients[i]->stage_id >= GUC_NUM_DOORBELLS) {
336 pr_err("[%d] more clients than doorbells (%d >= %d)\n",
337 i, clients[i]->stage_id, GUC_NUM_DOORBELLS);
342 err = validate_client(clients[i],
343 i % GUC_CLIENT_PRIORITY_NUM, false);
345 pr_err("[%d] client_alloc sanity check failed!\n", i);
350 db_id = clients[i]->doorbell_id;
352 err = create_doorbell(clients[i]);
354 pr_err("[%d] Failed to create a doorbell\n", i);
358 /* doorbell id shouldn't change, we are holding the mutex */
359 if (db_id != clients[i]->doorbell_id) {
360 pr_err("[%d] doorbell id changed (%d != %d)\n",
361 i, db_id, clients[i]->doorbell_id);
366 err = check_all_doorbells(guc);
370 err = ring_doorbell_nop(clients[i]);
376 for (i = 0; i < ATTEMPTS; i++)
377 if (!IS_ERR_OR_NULL(clients[i])) {
378 destroy_doorbell(clients[i]);
379 guc_client_free(clients[i]);
382 mutex_unlock(&dev_priv->drm.struct_mutex);
386 int intel_guc_live_selftest(struct drm_i915_private *dev_priv)
388 static const struct i915_subtest tests[] = {
389 SUBTEST(igt_guc_clients),
390 SUBTEST(igt_guc_doorbells),
393 if (!USES_GUC_SUBMISSION(dev_priv))
396 return i915_subtests(tests, dev_priv);