drm/i915/selftests: Live tests emit requests and so require rpm
[sfrench/cifs-2.6.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_destroy(guc);
163         if (guc->execbuf_client || guc->preempt_client) {
164                 pr_err("guc_clients_destroy lied!\n");
165                 err = -EINVAL;
166                 goto unlock;
167         }
168
169         err = guc_clients_create(guc);
170         if (err) {
171                 pr_err("Failed to create clients\n");
172                 goto unlock;
173         }
174         GEM_BUG_ON(!guc->execbuf_client);
175
176         err = validate_client(guc->execbuf_client,
177                               GUC_CLIENT_PRIORITY_KMD_NORMAL, false);
178         if (err) {
179                 pr_err("execbug client validation failed\n");
180                 goto out;
181         }
182
183         if (guc->preempt_client) {
184                 err = validate_client(guc->preempt_client,
185                                       GUC_CLIENT_PRIORITY_KMD_HIGH, true);
186                 if (err) {
187                         pr_err("preempt client validation failed\n");
188                         goto out;
189                 }
190         }
191
192         /* each client should now have reserved a doorbell */
193         if (!has_doorbell(guc->execbuf_client) ||
194             (guc->preempt_client && !has_doorbell(guc->preempt_client))) {
195                 pr_err("guc_clients_create didn't reserve doorbells\n");
196                 err = -EINVAL;
197                 goto out;
198         }
199
200         /* Now create the doorbells */
201         guc_clients_doorbell_init(guc);
202
203         /* each client should now have received a doorbell */
204         if (!client_doorbell_in_sync(guc->execbuf_client) ||
205             !client_doorbell_in_sync(guc->preempt_client)) {
206                 pr_err("failed to initialize the doorbells\n");
207                 err = -EINVAL;
208                 goto out;
209         }
210
211         /*
212          * Basic test - an attempt to reallocate a valid doorbell to the
213          * client it is currently assigned should not cause a failure.
214          */
215         err = guc_clients_doorbell_init(guc);
216         if (err)
217                 goto out;
218
219         /*
220          * Negative test - a client with no doorbell (invalid db id).
221          * After destroying the doorbell, the db id is changed to
222          * GUC_DOORBELL_INVALID and the firmware will reject any attempt to
223          * allocate a doorbell with an invalid id (db has to be reserved before
224          * allocation).
225          */
226         destroy_doorbell(guc->execbuf_client);
227         if (client_doorbell_in_sync(guc->execbuf_client)) {
228                 pr_err("destroy db did not work\n");
229                 err = -EINVAL;
230                 goto out;
231         }
232
233         unreserve_doorbell(guc->execbuf_client);
234
235         __create_doorbell(guc->execbuf_client);
236         err = __guc_allocate_doorbell(guc, guc->execbuf_client->stage_id);
237         if (err != -EIO) {
238                 pr_err("unexpected (err = %d)", err);
239                 goto out_db;
240         }
241
242         if (!available_dbs(guc, guc->execbuf_client->priority)) {
243                 pr_err("doorbell not available when it should\n");
244                 err = -EIO;
245                 goto out_db;
246         }
247
248 out_db:
249         /* clean after test */
250         __destroy_doorbell(guc->execbuf_client);
251         err = reserve_doorbell(guc->execbuf_client);
252         if (err) {
253                 pr_err("failed to reserve back the doorbell back\n");
254         }
255         err = create_doorbell(guc->execbuf_client);
256         if (err) {
257                 pr_err("recreate doorbell failed\n");
258                 goto out;
259         }
260
261 out:
262         /*
263          * Leave clean state for other test, plus the driver always destroy the
264          * clients during unload.
265          */
266         destroy_doorbell(guc->execbuf_client);
267         if (guc->preempt_client)
268                 destroy_doorbell(guc->preempt_client);
269         guc_clients_destroy(guc);
270         guc_clients_create(guc);
271         guc_clients_doorbell_init(guc);
272 unlock:
273         intel_runtime_pm_put(dev_priv);
274         mutex_unlock(&dev_priv->drm.struct_mutex);
275         return err;
276 }
277
278 /*
279  * Create as many clients as number of doorbells. Note that there's already
280  * client(s)/doorbell(s) created during driver load, but this test creates
281  * its own and do not interact with the existing ones.
282  */
283 static int igt_guc_doorbells(void *arg)
284 {
285         struct drm_i915_private *dev_priv = arg;
286         struct intel_guc *guc;
287         int i, err = 0;
288         u16 db_id;
289
290         GEM_BUG_ON(!HAS_GUC(dev_priv));
291         mutex_lock(&dev_priv->drm.struct_mutex);
292         intel_runtime_pm_get(dev_priv);
293
294         guc = &dev_priv->guc;
295         if (!guc) {
296                 pr_err("No guc object!\n");
297                 err = -EINVAL;
298                 goto unlock;
299         }
300
301         err = check_all_doorbells(guc);
302         if (err)
303                 goto unlock;
304
305         for (i = 0; i < ATTEMPTS; i++) {
306                 clients[i] = guc_client_alloc(dev_priv,
307                                               INTEL_INFO(dev_priv)->ring_mask,
308                                               i % GUC_CLIENT_PRIORITY_NUM,
309                                               dev_priv->kernel_context);
310
311                 if (!clients[i]) {
312                         pr_err("[%d] No guc client\n", i);
313                         err = -EINVAL;
314                         goto out;
315                 }
316
317                 if (IS_ERR(clients[i])) {
318                         if (PTR_ERR(clients[i]) != -ENOSPC) {
319                                 pr_err("[%d] unexpected error\n", i);
320                                 err = PTR_ERR(clients[i]);
321                                 goto out;
322                         }
323
324                         if (available_dbs(guc, i % GUC_CLIENT_PRIORITY_NUM)) {
325                                 pr_err("[%d] non-db related alloc fail\n", i);
326                                 err = -EINVAL;
327                                 goto out;
328                         }
329
330                         /* expected, ran out of dbs for this client type */
331                         continue;
332                 }
333
334                 /*
335                  * The check below is only valid because we keep a doorbell
336                  * assigned during the whole life of the client.
337                  */
338                 if (clients[i]->stage_id >= GUC_NUM_DOORBELLS) {
339                         pr_err("[%d] more clients than doorbells (%d >= %d)\n",
340                                i, clients[i]->stage_id, GUC_NUM_DOORBELLS);
341                         err = -EINVAL;
342                         goto out;
343                 }
344
345                 err = validate_client(clients[i],
346                                       i % GUC_CLIENT_PRIORITY_NUM, false);
347                 if (err) {
348                         pr_err("[%d] client_alloc sanity check failed!\n", i);
349                         err = -EINVAL;
350                         goto out;
351                 }
352
353                 db_id = clients[i]->doorbell_id;
354
355                 err = create_doorbell(clients[i]);
356                 if (err) {
357                         pr_err("[%d] Failed to create a doorbell\n", i);
358                         goto out;
359                 }
360
361                 /* doorbell id shouldn't change, we are holding the mutex */
362                 if (db_id != clients[i]->doorbell_id) {
363                         pr_err("[%d] doorbell id changed (%d != %d)\n",
364                                i, db_id, clients[i]->doorbell_id);
365                         err = -EINVAL;
366                         goto out;
367                 }
368
369                 err = check_all_doorbells(guc);
370                 if (err)
371                         goto out;
372
373                 err = ring_doorbell_nop(clients[i]);
374                 if (err)
375                         goto out;
376         }
377
378 out:
379         for (i = 0; i < ATTEMPTS; i++)
380                 if (!IS_ERR_OR_NULL(clients[i])) {
381                         destroy_doorbell(clients[i]);
382                         guc_client_free(clients[i]);
383                 }
384 unlock:
385         intel_runtime_pm_put(dev_priv);
386         mutex_unlock(&dev_priv->drm.struct_mutex);
387         return err;
388 }
389
390 int intel_guc_live_selftest(struct drm_i915_private *dev_priv)
391 {
392         static const struct i915_subtest tests[] = {
393                 SUBTEST(igt_guc_clients),
394                 SUBTEST(igt_guc_doorbells),
395         };
396
397         if (!USES_GUC_SUBMISSION(dev_priv))
398                 return 0;
399
400         return i915_subtests(tests, dev_priv);
401 }