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);
69 * Basic client sanity check, handy to validate create_clients.
71 static int validate_client(struct intel_guc_client *client,
73 bool is_preempt_client)
75 struct drm_i915_private *dev_priv = guc_to_i915(client->guc);
76 struct i915_gem_context *ctx_owner = is_preempt_client ?
77 dev_priv->preempt_context : dev_priv->kernel_context;
79 if (client->owner != ctx_owner ||
80 client->engines != INTEL_INFO(dev_priv)->ring_mask ||
81 client->priority != client_priority ||
82 client->doorbell_id == GUC_DOORBELL_INVALID)
88 static bool client_doorbell_in_sync(struct intel_guc_client *client)
90 return !client || doorbell_ok(client->guc, client->doorbell_id);
94 * Check that we're able to synchronize guc_clients with their doorbells
96 * We're creating clients and reserving doorbells once, at module load. During
97 * module lifetime, GuC, doorbell HW, and i915 state may go out of sync due to
98 * GuC being reset. In other words - GuC clients are still around, but the
99 * status of their doorbells may be incorrect. This is the reason behind
100 * validating that the doorbells status expected by the driver matches what the
103 static int igt_guc_clients(void *args)
105 struct drm_i915_private *dev_priv = args;
106 struct intel_guc *guc;
109 GEM_BUG_ON(!HAS_GUC(dev_priv));
110 mutex_lock(&dev_priv->drm.struct_mutex);
112 guc = &dev_priv->guc;
114 pr_err("No guc object!\n");
119 err = check_all_doorbells(guc);
124 * Get rid of clients created during driver load because the test will
127 guc_clients_destroy(guc);
128 if (guc->execbuf_client || guc->preempt_client) {
129 pr_err("guc_clients_destroy lied!\n");
134 err = guc_clients_create(guc);
136 pr_err("Failed to create clients\n");
139 GEM_BUG_ON(!guc->execbuf_client);
141 err = validate_client(guc->execbuf_client,
142 GUC_CLIENT_PRIORITY_KMD_NORMAL, false);
144 pr_err("execbug client validation failed\n");
148 if (guc->preempt_client) {
149 err = validate_client(guc->preempt_client,
150 GUC_CLIENT_PRIORITY_KMD_HIGH, true);
152 pr_err("preempt client validation failed\n");
157 /* each client should now have reserved a doorbell */
158 if (!has_doorbell(guc->execbuf_client) ||
159 (guc->preempt_client && !has_doorbell(guc->preempt_client))) {
160 pr_err("guc_clients_create didn't reserve doorbells\n");
165 /* Now create the doorbells */
166 guc_clients_doorbell_init(guc);
168 /* each client should now have received a doorbell */
169 if (!client_doorbell_in_sync(guc->execbuf_client) ||
170 !client_doorbell_in_sync(guc->preempt_client)) {
171 pr_err("failed to initialize the doorbells\n");
177 * Basic test - an attempt to reallocate a valid doorbell to the
178 * client it is currently assigned should not cause a failure.
180 err = guc_clients_doorbell_init(guc);
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
191 destroy_doorbell(guc->execbuf_client);
192 if (client_doorbell_in_sync(guc->execbuf_client)) {
193 pr_err("destroy db did not work\n");
198 unreserve_doorbell(guc->execbuf_client);
200 __create_doorbell(guc->execbuf_client);
201 err = __guc_allocate_doorbell(guc, guc->execbuf_client->stage_id);
203 pr_err("unexpected (err = %d)", err);
207 if (!available_dbs(guc, guc->execbuf_client->priority)) {
208 pr_err("doorbell not available when it should\n");
214 /* clean after test */
215 __destroy_doorbell(guc->execbuf_client);
216 err = reserve_doorbell(guc->execbuf_client);
218 pr_err("failed to reserve back the doorbell back\n");
220 err = create_doorbell(guc->execbuf_client);
222 pr_err("recreate doorbell failed\n");
228 * Leave clean state for other test, plus the driver always destroy the
229 * clients during unload.
231 destroy_doorbell(guc->execbuf_client);
232 if (guc->preempt_client)
233 destroy_doorbell(guc->preempt_client);
234 guc_clients_destroy(guc);
235 guc_clients_create(guc);
236 guc_clients_doorbell_init(guc);
238 mutex_unlock(&dev_priv->drm.struct_mutex);
243 * Create as many clients as number of doorbells. Note that there's already
244 * client(s)/doorbell(s) created during driver load, but this test creates
245 * its own and do not interact with the existing ones.
247 static int igt_guc_doorbells(void *arg)
249 struct drm_i915_private *dev_priv = arg;
250 struct intel_guc *guc;
254 GEM_BUG_ON(!HAS_GUC(dev_priv));
255 mutex_lock(&dev_priv->drm.struct_mutex);
257 guc = &dev_priv->guc;
259 pr_err("No guc object!\n");
264 err = check_all_doorbells(guc);
268 for (i = 0; i < ATTEMPTS; i++) {
269 clients[i] = guc_client_alloc(dev_priv,
270 INTEL_INFO(dev_priv)->ring_mask,
271 i % GUC_CLIENT_PRIORITY_NUM,
272 dev_priv->kernel_context);
275 pr_err("[%d] No guc client\n", i);
280 if (IS_ERR(clients[i])) {
281 if (PTR_ERR(clients[i]) != -ENOSPC) {
282 pr_err("[%d] unexpected error\n", i);
283 err = PTR_ERR(clients[i]);
287 if (available_dbs(guc, i % GUC_CLIENT_PRIORITY_NUM)) {
288 pr_err("[%d] non-db related alloc fail\n", i);
293 /* expected, ran out of dbs for this client type */
298 * The check below is only valid because we keep a doorbell
299 * assigned during the whole life of the client.
301 if (clients[i]->stage_id >= GUC_NUM_DOORBELLS) {
302 pr_err("[%d] more clients than doorbells (%d >= %d)\n",
303 i, clients[i]->stage_id, GUC_NUM_DOORBELLS);
308 err = validate_client(clients[i],
309 i % GUC_CLIENT_PRIORITY_NUM, false);
311 pr_err("[%d] client_alloc sanity check failed!\n", i);
316 db_id = clients[i]->doorbell_id;
318 err = create_doorbell(clients[i]);
320 pr_err("[%d] Failed to create a doorbell\n", i);
324 /* doorbell id shouldn't change, we are holding the mutex */
325 if (db_id != clients[i]->doorbell_id) {
326 pr_err("[%d] doorbell id changed (%d != %d)\n",
327 i, db_id, clients[i]->doorbell_id);
332 err = check_all_doorbells(guc);
338 for (i = 0; i < ATTEMPTS; i++)
339 if (!IS_ERR_OR_NULL(clients[i])) {
340 destroy_doorbell(clients[i]);
341 guc_client_free(clients[i]);
344 mutex_unlock(&dev_priv->drm.struct_mutex);
348 int intel_guc_live_selftest(struct drm_i915_private *dev_priv)
350 static const struct i915_subtest tests[] = {
351 SUBTEST(igt_guc_clients),
352 SUBTEST(igt_guc_doorbells),
355 if (!USES_GUC_SUBMISSION(dev_priv))
358 return i915_subtests(tests, dev_priv);