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
);
145 intel_runtime_pm_get(dev_priv
);
147 guc
= &dev_priv
->guc
;
149 pr_err("No guc object!\n");
154 err
= check_all_doorbells(guc
);
159 * Get rid of clients created during driver load because the test will
162 guc_clients_disable(guc
);
163 guc_clients_destroy(guc
);
164 if (guc
->execbuf_client
|| guc
->preempt_client
) {
165 pr_err("guc_clients_destroy lied!\n");
170 err
= guc_clients_create(guc
);
172 pr_err("Failed to create clients\n");
175 GEM_BUG_ON(!guc
->execbuf_client
);
177 err
= validate_client(guc
->execbuf_client
,
178 GUC_CLIENT_PRIORITY_KMD_NORMAL
, false);
180 pr_err("execbug client validation failed\n");
184 if (guc
->preempt_client
) {
185 err
= validate_client(guc
->preempt_client
,
186 GUC_CLIENT_PRIORITY_KMD_HIGH
, true);
188 pr_err("preempt client validation failed\n");
193 /* each client should now have reserved a doorbell */
194 if (!has_doorbell(guc
->execbuf_client
) ||
195 (guc
->preempt_client
&& !has_doorbell(guc
->preempt_client
))) {
196 pr_err("guc_clients_create didn't reserve doorbells\n");
201 /* Now enable the clients */
202 guc_clients_enable(guc
);
204 /* each client should now have received a doorbell */
205 if (!client_doorbell_in_sync(guc
->execbuf_client
) ||
206 !client_doorbell_in_sync(guc
->preempt_client
)) {
207 pr_err("failed to initialize the doorbells\n");
213 * Basic test - an attempt to reallocate a valid doorbell to the
214 * client it is currently assigned should not cause a failure.
216 err
= create_doorbell(guc
->execbuf_client
);
220 * Leave clean state for other test, plus the driver always destroy the
221 * clients during unload.
223 guc_clients_disable(guc
);
224 guc_clients_destroy(guc
);
225 guc_clients_create(guc
);
226 guc_clients_enable(guc
);
228 intel_runtime_pm_put(dev_priv
);
229 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
234 * Create as many clients as number of doorbells. Note that there's already
235 * client(s)/doorbell(s) created during driver load, but this test creates
236 * its own and do not interact with the existing ones.
238 static int igt_guc_doorbells(void *arg
)
240 struct drm_i915_private
*dev_priv
= arg
;
241 struct intel_guc
*guc
;
245 GEM_BUG_ON(!HAS_GUC(dev_priv
));
246 mutex_lock(&dev_priv
->drm
.struct_mutex
);
247 intel_runtime_pm_get(dev_priv
);
249 guc
= &dev_priv
->guc
;
251 pr_err("No guc object!\n");
256 err
= check_all_doorbells(guc
);
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
);
267 pr_err("[%d] No guc client\n", i
);
272 if (IS_ERR(clients
[i
])) {
273 if (PTR_ERR(clients
[i
]) != -ENOSPC
) {
274 pr_err("[%d] unexpected error\n", i
);
275 err
= PTR_ERR(clients
[i
]);
279 if (available_dbs(guc
, i
% GUC_CLIENT_PRIORITY_NUM
)) {
280 pr_err("[%d] non-db related alloc fail\n", i
);
285 /* expected, ran out of dbs for this client type */
290 * The check below is only valid because we keep a doorbell
291 * assigned during the whole life of the client.
293 if (clients
[i
]->stage_id
>= GUC_NUM_DOORBELLS
) {
294 pr_err("[%d] more clients than doorbells (%d >= %d)\n",
295 i
, clients
[i
]->stage_id
, GUC_NUM_DOORBELLS
);
300 err
= validate_client(clients
[i
],
301 i
% GUC_CLIENT_PRIORITY_NUM
, false);
303 pr_err("[%d] client_alloc sanity check failed!\n", i
);
308 db_id
= clients
[i
]->doorbell_id
;
310 err
= __guc_client_enable(clients
[i
]);
312 pr_err("[%d] Failed to create a doorbell\n", i
);
316 /* doorbell id shouldn't change, we are holding the mutex */
317 if (db_id
!= clients
[i
]->doorbell_id
) {
318 pr_err("[%d] doorbell id changed (%d != %d)\n",
319 i
, db_id
, clients
[i
]->doorbell_id
);
324 err
= check_all_doorbells(guc
);
328 err
= ring_doorbell_nop(clients
[i
]);
334 for (i
= 0; i
< ATTEMPTS
; i
++)
335 if (!IS_ERR_OR_NULL(clients
[i
])) {
336 __guc_client_disable(clients
[i
]);
337 guc_client_free(clients
[i
]);
340 intel_runtime_pm_put(dev_priv
);
341 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
345 int intel_guc_live_selftest(struct drm_i915_private
*dev_priv
)
347 static const struct i915_subtest tests
[] = {
348 SUBTEST(igt_guc_clients
),
349 SUBTEST(igt_guc_doorbells
),
352 if (!USES_GUC_SUBMISSION(dev_priv
))
355 return i915_subtests(tests
, dev_priv
);