]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - drivers/gpu/drm/i915/selftests/intel_guc.c
drm/amd/display: Check hpd_gpio for NULL before accessing it
[thirdparty/kernel/stable.git] / drivers / gpu / drm / i915 / selftests / intel_guc.c
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
25 #include "../i915_selftest.h"
26
27 /* max doorbell number + negative test for each client type */
28 #define ATTEMPTS (GUC_NUM_DOORBELLS + GUC_CLIENT_PRIORITY_NUM)
29
30 static struct intel_guc_client *clients[ATTEMPTS];
31
32 static bool available_dbs(struct intel_guc *guc, u32 priority)
33 {
34 unsigned long offset;
35 unsigned long end;
36 u16 id;
37
38 /* first half is used for normal priority, second half for high */
39 offset = 0;
40 end = GUC_NUM_DOORBELLS / 2;
41 if (priority <= GUC_CLIENT_PRIORITY_HIGH) {
42 offset = end;
43 end += offset;
44 }
45
46 id = find_next_zero_bit(guc->doorbell_bitmap, end, offset);
47 if (id < end)
48 return true;
49
50 return false;
51 }
52
53 static int check_all_doorbells(struct intel_guc *guc)
54 {
55 u16 db_id;
56
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);
61 return -EIO;
62 }
63 }
64
65 return 0;
66 }
67
68 static int ring_doorbell_nop(struct intel_guc_client *client)
69 {
70 struct guc_process_desc *desc = __get_process_desc(client);
71 int err;
72
73 client->use_nop_wqi = true;
74
75 spin_lock_irq(&client->wq_lock);
76
77 guc_wq_item_append(client, 0, 0, 0, 0);
78 guc_ring_doorbell(client);
79
80 spin_unlock_irq(&client->wq_lock);
81
82 client->use_nop_wqi = false;
83
84 /* if there are no issues GuC will update the WQ head and keep the
85 * WQ in active status
86 */
87 err = wait_for(READ_ONCE(desc->head) == READ_ONCE(desc->tail), 10);
88 if (err) {
89 pr_err("doorbell %u ring failed!\n", client->doorbell_id);
90 return -EIO;
91 }
92
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);
96 return -EIO;
97 }
98
99 return 0;
100 }
101
102 /*
103 * Basic client sanity check, handy to validate create_clients.
104 */
105 static int validate_client(struct intel_guc_client *client,
106 int client_priority,
107 bool is_preempt_client)
108 {
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;
112
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)
117 return -EINVAL;
118 else
119 return 0;
120 }
121
122 static bool client_doorbell_in_sync(struct intel_guc_client *client)
123 {
124 return !client || doorbell_ok(client->guc, client->doorbell_id);
125 }
126
127 /*
128 * Check that we're able to synchronize guc_clients with their doorbells
129 *
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
135 * GuC/HW have.
136 */
137 static int igt_guc_clients(void *args)
138 {
139 struct drm_i915_private *dev_priv = args;
140 struct intel_guc *guc;
141 int err = 0;
142
143 GEM_BUG_ON(!HAS_GUC(dev_priv));
144 mutex_lock(&dev_priv->drm.struct_mutex);
145 intel_runtime_pm_get(dev_priv);
146
147 guc = &dev_priv->guc;
148 if (!guc) {
149 pr_err("No guc object!\n");
150 err = -EINVAL;
151 goto unlock;
152 }
153
154 err = check_all_doorbells(guc);
155 if (err)
156 goto unlock;
157
158 /*
159 * Get rid of clients created during driver load because the test will
160 * recreate them.
161 */
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");
166 err = -EINVAL;
167 goto unlock;
168 }
169
170 err = guc_clients_create(guc);
171 if (err) {
172 pr_err("Failed to create clients\n");
173 goto unlock;
174 }
175 GEM_BUG_ON(!guc->execbuf_client);
176
177 err = validate_client(guc->execbuf_client,
178 GUC_CLIENT_PRIORITY_KMD_NORMAL, false);
179 if (err) {
180 pr_err("execbug client validation failed\n");
181 goto out;
182 }
183
184 if (guc->preempt_client) {
185 err = validate_client(guc->preempt_client,
186 GUC_CLIENT_PRIORITY_KMD_HIGH, true);
187 if (err) {
188 pr_err("preempt client validation failed\n");
189 goto out;
190 }
191 }
192
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");
197 err = -EINVAL;
198 goto out;
199 }
200
201 /* Now enable the clients */
202 guc_clients_enable(guc);
203
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");
208 err = -EINVAL;
209 goto out;
210 }
211
212 /*
213 * Basic test - an attempt to reallocate a valid doorbell to the
214 * client it is currently assigned should not cause a failure.
215 */
216 err = create_doorbell(guc->execbuf_client);
217
218 out:
219 /*
220 * Leave clean state for other test, plus the driver always destroy the
221 * clients during unload.
222 */
223 guc_clients_disable(guc);
224 guc_clients_destroy(guc);
225 guc_clients_create(guc);
226 guc_clients_enable(guc);
227 unlock:
228 intel_runtime_pm_put(dev_priv);
229 mutex_unlock(&dev_priv->drm.struct_mutex);
230 return err;
231 }
232
233 /*
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.
237 */
238 static int igt_guc_doorbells(void *arg)
239 {
240 struct drm_i915_private *dev_priv = arg;
241 struct intel_guc *guc;
242 int i, err = 0;
243 u16 db_id;
244
245 GEM_BUG_ON(!HAS_GUC(dev_priv));
246 mutex_lock(&dev_priv->drm.struct_mutex);
247 intel_runtime_pm_get(dev_priv);
248
249 guc = &dev_priv->guc;
250 if (!guc) {
251 pr_err("No guc object!\n");
252 err = -EINVAL;
253 goto unlock;
254 }
255
256 err = check_all_doorbells(guc);
257 if (err)
258 goto unlock;
259
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
266 if (!clients[i]) {
267 pr_err("[%d] No guc client\n", i);
268 err = -EINVAL;
269 goto out;
270 }
271
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]);
276 goto out;
277 }
278
279 if (available_dbs(guc, i % GUC_CLIENT_PRIORITY_NUM)) {
280 pr_err("[%d] non-db related alloc fail\n", i);
281 err = -EINVAL;
282 goto out;
283 }
284
285 /* expected, ran out of dbs for this client type */
286 continue;
287 }
288
289 /*
290 * The check below is only valid because we keep a doorbell
291 * assigned during the whole life of the client.
292 */
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);
296 err = -EINVAL;
297 goto out;
298 }
299
300 err = validate_client(clients[i],
301 i % GUC_CLIENT_PRIORITY_NUM, false);
302 if (err) {
303 pr_err("[%d] client_alloc sanity check failed!\n", i);
304 err = -EINVAL;
305 goto out;
306 }
307
308 db_id = clients[i]->doorbell_id;
309
310 err = __guc_client_enable(clients[i]);
311 if (err) {
312 pr_err("[%d] Failed to create a doorbell\n", i);
313 goto out;
314 }
315
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);
320 err = -EINVAL;
321 goto out;
322 }
323
324 err = check_all_doorbells(guc);
325 if (err)
326 goto out;
327
328 err = ring_doorbell_nop(clients[i]);
329 if (err)
330 goto out;
331 }
332
333 out:
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]);
338 }
339 unlock:
340 intel_runtime_pm_put(dev_priv);
341 mutex_unlock(&dev_priv->drm.struct_mutex);
342 return err;
343 }
344
345 int intel_guc_live_selftest(struct drm_i915_private *dev_priv)
346 {
347 static const struct i915_subtest tests[] = {
348 SUBTEST(igt_guc_clients),
349 SUBTEST(igt_guc_doorbells),
350 };
351
352 if (!USES_GUC_SUBMISSION(dev_priv))
353 return 0;
354
355 return i915_subtests(tests, dev_priv);
356 }