]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/i915/selftests/i915_gem_evict.c
8c69198c7e4ec1c52f17a6fb1618a86d9e8e094e
[linux.git] / drivers / gpu / drm / i915 / selftests / i915_gem_evict.c
1 /*
2  * Copyright © 2016 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 "gem/i915_gem_pm.h"
26 #include "gem/selftests/igt_gem_utils.h"
27 #include "gem/selftests/mock_context.h"
28
29 #include "i915_selftest.h"
30
31 #include "lib_sw_fence.h"
32 #include "mock_drm.h"
33 #include "mock_gem_device.h"
34
35 static void quirk_add(struct drm_i915_gem_object *obj,
36                       struct list_head *objects)
37 {
38         /* quirk is only for live tiled objects, use it to declare ownership */
39         GEM_BUG_ON(obj->mm.quirked);
40         obj->mm.quirked = true;
41         list_add(&obj->st_link, objects);
42 }
43
44 static int populate_ggtt(struct drm_i915_private *i915,
45                          struct list_head *objects)
46 {
47         unsigned long unbound, bound, count;
48         struct drm_i915_gem_object *obj;
49         u64 size;
50
51         count = 0;
52         for (size = 0;
53              size + I915_GTT_PAGE_SIZE <= i915->ggtt.vm.total;
54              size += I915_GTT_PAGE_SIZE) {
55                 struct i915_vma *vma;
56
57                 obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
58                 if (IS_ERR(obj))
59                         return PTR_ERR(obj);
60
61                 quirk_add(obj, objects);
62
63                 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
64                 if (IS_ERR(vma))
65                         return PTR_ERR(vma);
66
67                 count++;
68         }
69
70         bound = 0;
71         unbound = 0;
72         list_for_each_entry(obj, objects, st_link) {
73                 GEM_BUG_ON(!obj->mm.quirked);
74
75                 if (atomic_read(&obj->bind_count))
76                         bound++;
77                 else
78                         unbound++;
79         }
80         GEM_BUG_ON(bound + unbound != count);
81
82         if (unbound) {
83                 pr_err("%s: Found %lu objects unbound, expected %u!\n",
84                        __func__, unbound, 0);
85                 return -EINVAL;
86         }
87
88         if (bound != count) {
89                 pr_err("%s: Found %lu objects bound, expected %lu!\n",
90                        __func__, bound, count);
91                 return -EINVAL;
92         }
93
94         if (list_empty(&i915->ggtt.vm.bound_list)) {
95                 pr_err("No objects on the GGTT inactive list!\n");
96                 return -EINVAL;
97         }
98
99         return 0;
100 }
101
102 static void unpin_ggtt(struct drm_i915_private *i915)
103 {
104         struct i915_ggtt *ggtt = &i915->ggtt;
105         struct i915_vma *vma;
106
107         mutex_lock(&ggtt->vm.mutex);
108         list_for_each_entry(vma, &i915->ggtt.vm.bound_list, vm_link)
109                 if (vma->obj->mm.quirked)
110                         i915_vma_unpin(vma);
111         mutex_unlock(&ggtt->vm.mutex);
112 }
113
114 static void cleanup_objects(struct drm_i915_private *i915,
115                             struct list_head *list)
116 {
117         struct drm_i915_gem_object *obj, *on;
118
119         list_for_each_entry_safe(obj, on, list, st_link) {
120                 GEM_BUG_ON(!obj->mm.quirked);
121                 obj->mm.quirked = false;
122                 i915_gem_object_put(obj);
123         }
124
125         mutex_unlock(&i915->drm.struct_mutex);
126
127         i915_gem_drain_freed_objects(i915);
128
129         mutex_lock(&i915->drm.struct_mutex);
130 }
131
132 static int igt_evict_something(void *arg)
133 {
134         struct drm_i915_private *i915 = arg;
135         struct i915_ggtt *ggtt = &i915->ggtt;
136         LIST_HEAD(objects);
137         int err;
138
139         /* Fill the GGTT with pinned objects and try to evict one. */
140
141         err = populate_ggtt(i915, &objects);
142         if (err)
143                 goto cleanup;
144
145         /* Everything is pinned, nothing should happen */
146         err = i915_gem_evict_something(&ggtt->vm,
147                                        I915_GTT_PAGE_SIZE, 0, 0,
148                                        0, U64_MAX,
149                                        0);
150         if (err != -ENOSPC) {
151                 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
152                        err);
153                 goto cleanup;
154         }
155
156         unpin_ggtt(i915);
157
158         /* Everything is unpinned, we should be able to evict something */
159         err = i915_gem_evict_something(&ggtt->vm,
160                                        I915_GTT_PAGE_SIZE, 0, 0,
161                                        0, U64_MAX,
162                                        0);
163         if (err) {
164                 pr_err("i915_gem_evict_something failed on a full GGTT with err=%d\n",
165                        err);
166                 goto cleanup;
167         }
168
169 cleanup:
170         cleanup_objects(i915, &objects);
171         return err;
172 }
173
174 static int igt_overcommit(void *arg)
175 {
176         struct drm_i915_private *i915 = arg;
177         struct drm_i915_gem_object *obj;
178         struct i915_vma *vma;
179         LIST_HEAD(objects);
180         int err;
181
182         /* Fill the GGTT with pinned objects and then try to pin one more.
183          * We expect it to fail.
184          */
185
186         err = populate_ggtt(i915, &objects);
187         if (err)
188                 goto cleanup;
189
190         obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
191         if (IS_ERR(obj)) {
192                 err = PTR_ERR(obj);
193                 goto cleanup;
194         }
195
196         quirk_add(obj, &objects);
197
198         vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
199         if (!IS_ERR(vma) || PTR_ERR(vma) != -ENOSPC) {
200                 pr_err("Failed to evict+insert, i915_gem_object_ggtt_pin returned err=%d\n", (int)PTR_ERR(vma));
201                 err = -EINVAL;
202                 goto cleanup;
203         }
204
205 cleanup:
206         cleanup_objects(i915, &objects);
207         return err;
208 }
209
210 static int igt_evict_for_vma(void *arg)
211 {
212         struct drm_i915_private *i915 = arg;
213         struct i915_ggtt *ggtt = &i915->ggtt;
214         struct drm_mm_node target = {
215                 .start = 0,
216                 .size = 4096,
217         };
218         LIST_HEAD(objects);
219         int err;
220
221         /* Fill the GGTT with pinned objects and try to evict a range. */
222
223         err = populate_ggtt(i915, &objects);
224         if (err)
225                 goto cleanup;
226
227         /* Everything is pinned, nothing should happen */
228         err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
229         if (err != -ENOSPC) {
230                 pr_err("i915_gem_evict_for_node on a full GGTT returned err=%d\n",
231                        err);
232                 goto cleanup;
233         }
234
235         unpin_ggtt(i915);
236
237         /* Everything is unpinned, we should be able to evict the node */
238         err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
239         if (err) {
240                 pr_err("i915_gem_evict_for_node returned err=%d\n",
241                        err);
242                 goto cleanup;
243         }
244
245 cleanup:
246         cleanup_objects(i915, &objects);
247         return err;
248 }
249
250 static void mock_color_adjust(const struct drm_mm_node *node,
251                               unsigned long color,
252                               u64 *start,
253                               u64 *end)
254 {
255 }
256
257 static int igt_evict_for_cache_color(void *arg)
258 {
259         struct drm_i915_private *i915 = arg;
260         struct i915_ggtt *ggtt = &i915->ggtt;
261         const unsigned long flags = PIN_OFFSET_FIXED;
262         struct drm_mm_node target = {
263                 .start = I915_GTT_PAGE_SIZE * 2,
264                 .size = I915_GTT_PAGE_SIZE,
265                 .color = I915_CACHE_LLC,
266         };
267         struct drm_i915_gem_object *obj;
268         struct i915_vma *vma;
269         LIST_HEAD(objects);
270         int err;
271
272         /* Currently the use of color_adjust is limited to cache domains within
273          * the ggtt, and so the presence of mm.color_adjust is assumed to be
274          * i915_gtt_color_adjust throughout our driver, so using a mock color
275          * adjust will work just fine for our purposes.
276          */
277         ggtt->vm.mm.color_adjust = mock_color_adjust;
278
279         obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
280         if (IS_ERR(obj)) {
281                 err = PTR_ERR(obj);
282                 goto cleanup;
283         }
284         i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
285         quirk_add(obj, &objects);
286
287         vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
288                                        I915_GTT_PAGE_SIZE | flags);
289         if (IS_ERR(vma)) {
290                 pr_err("[0]i915_gem_object_ggtt_pin failed\n");
291                 err = PTR_ERR(vma);
292                 goto cleanup;
293         }
294
295         obj = i915_gem_object_create_internal(i915, I915_GTT_PAGE_SIZE);
296         if (IS_ERR(obj)) {
297                 err = PTR_ERR(obj);
298                 goto cleanup;
299         }
300         i915_gem_object_set_cache_coherency(obj, I915_CACHE_LLC);
301         quirk_add(obj, &objects);
302
303         /* Neighbouring; same colour - should fit */
304         vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
305                                        (I915_GTT_PAGE_SIZE * 2) | flags);
306         if (IS_ERR(vma)) {
307                 pr_err("[1]i915_gem_object_ggtt_pin failed\n");
308                 err = PTR_ERR(vma);
309                 goto cleanup;
310         }
311
312         i915_vma_unpin(vma);
313
314         /* Remove just the second vma */
315         err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
316         if (err) {
317                 pr_err("[0]i915_gem_evict_for_node returned err=%d\n", err);
318                 goto cleanup;
319         }
320
321         /* Attempt to remove the first *pinned* vma, by removing the (empty)
322          * neighbour -- this should fail.
323          */
324         target.color = I915_CACHE_L3_LLC;
325
326         err = i915_gem_evict_for_node(&ggtt->vm, &target, 0);
327         if (!err) {
328                 pr_err("[1]i915_gem_evict_for_node returned err=%d\n", err);
329                 err = -EINVAL;
330                 goto cleanup;
331         }
332
333         err = 0;
334
335 cleanup:
336         unpin_ggtt(i915);
337         cleanup_objects(i915, &objects);
338         ggtt->vm.mm.color_adjust = NULL;
339         return err;
340 }
341
342 static int igt_evict_vm(void *arg)
343 {
344         struct drm_i915_private *i915 = arg;
345         struct i915_ggtt *ggtt = &i915->ggtt;
346         LIST_HEAD(objects);
347         int err;
348
349         /* Fill the GGTT with pinned objects and try to evict everything. */
350
351         err = populate_ggtt(i915, &objects);
352         if (err)
353                 goto cleanup;
354
355         /* Everything is pinned, nothing should happen */
356         err = i915_gem_evict_vm(&ggtt->vm);
357         if (err) {
358                 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
359                        err);
360                 goto cleanup;
361         }
362
363         unpin_ggtt(i915);
364
365         err = i915_gem_evict_vm(&ggtt->vm);
366         if (err) {
367                 pr_err("i915_gem_evict_vm on a full GGTT returned err=%d]\n",
368                        err);
369                 goto cleanup;
370         }
371
372 cleanup:
373         cleanup_objects(i915, &objects);
374         return err;
375 }
376
377 static int igt_evict_contexts(void *arg)
378 {
379         const u64 PRETEND_GGTT_SIZE = 16ull << 20;
380         struct drm_i915_private *i915 = arg;
381         struct intel_engine_cs *engine;
382         enum intel_engine_id id;
383         struct reserved {
384                 struct drm_mm_node node;
385                 struct reserved *next;
386         } *reserved = NULL;
387         intel_wakeref_t wakeref;
388         struct drm_mm_node hole;
389         unsigned long count;
390         int err;
391
392         /*
393          * The purpose of this test is to verify that we will trigger an
394          * eviction in the GGTT when constructing a request that requires
395          * additional space in the GGTT for pinning the context. This space
396          * is not directly tied to the request so reclaiming it requires
397          * extra work.
398          *
399          * As such this test is only meaningful for full-ppgtt environments
400          * where the GTT space of the request is separate from the GGTT
401          * allocation required to build the request.
402          */
403         if (!HAS_FULL_PPGTT(i915))
404                 return 0;
405
406         mutex_lock(&i915->drm.struct_mutex);
407         wakeref = intel_runtime_pm_get(&i915->runtime_pm);
408
409         /* Reserve a block so that we know we have enough to fit a few rq */
410         memset(&hole, 0, sizeof(hole));
411         err = i915_gem_gtt_insert(&i915->ggtt.vm, &hole,
412                                   PRETEND_GGTT_SIZE, 0, I915_COLOR_UNEVICTABLE,
413                                   0, i915->ggtt.vm.total,
414                                   PIN_NOEVICT);
415         if (err)
416                 goto out_locked;
417
418         /* Make the GGTT appear small by filling it with unevictable nodes */
419         count = 0;
420         do {
421                 struct reserved *r;
422
423                 r = kcalloc(1, sizeof(*r), GFP_KERNEL);
424                 if (!r) {
425                         err = -ENOMEM;
426                         goto out_locked;
427                 }
428
429                 if (i915_gem_gtt_insert(&i915->ggtt.vm, &r->node,
430                                         1ul << 20, 0, I915_COLOR_UNEVICTABLE,
431                                         0, i915->ggtt.vm.total,
432                                         PIN_NOEVICT)) {
433                         kfree(r);
434                         break;
435                 }
436
437                 r->next = reserved;
438                 reserved = r;
439
440                 count++;
441         } while (1);
442         drm_mm_remove_node(&hole);
443         mutex_unlock(&i915->drm.struct_mutex);
444         pr_info("Filled GGTT with %lu 1MiB nodes\n", count);
445
446         /* Overfill the GGTT with context objects and so try to evict one. */
447         for_each_engine(engine, i915, id) {
448                 struct i915_sw_fence fence;
449                 struct drm_file *file;
450
451                 file = mock_file(i915);
452                 if (IS_ERR(file)) {
453                         err = PTR_ERR(file);
454                         break;
455                 }
456
457                 count = 0;
458                 mutex_lock(&i915->drm.struct_mutex);
459                 onstack_fence_init(&fence);
460                 do {
461                         struct i915_request *rq;
462                         struct i915_gem_context *ctx;
463
464                         ctx = live_context(i915, file);
465                         if (IS_ERR(ctx))
466                                 break;
467
468                         /* We will need some GGTT space for the rq's context */
469                         igt_evict_ctl.fail_if_busy = true;
470                         rq = igt_request_alloc(ctx, engine);
471                         igt_evict_ctl.fail_if_busy = false;
472
473                         if (IS_ERR(rq)) {
474                                 /* When full, fail_if_busy will trigger EBUSY */
475                                 if (PTR_ERR(rq) != -EBUSY) {
476                                         pr_err("Unexpected error from request alloc (ctx hw id %u, on %s): %d\n",
477                                                ctx->hw_id, engine->name,
478                                                (int)PTR_ERR(rq));
479                                         err = PTR_ERR(rq);
480                                 }
481                                 break;
482                         }
483
484                         /* Keep every request/ctx pinned until we are full */
485                         err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
486                                                                &fence,
487                                                                GFP_KERNEL);
488                         if (err < 0)
489                                 break;
490
491                         i915_request_add(rq);
492                         count++;
493                         err = 0;
494                 } while(1);
495                 mutex_unlock(&i915->drm.struct_mutex);
496
497                 onstack_fence_fini(&fence);
498                 pr_info("Submitted %lu contexts/requests on %s\n",
499                         count, engine->name);
500
501                 mock_file_free(i915, file);
502                 if (err)
503                         break;
504         }
505
506         mutex_lock(&i915->drm.struct_mutex);
507 out_locked:
508         while (reserved) {
509                 struct reserved *next = reserved->next;
510
511                 drm_mm_remove_node(&reserved->node);
512                 kfree(reserved);
513
514                 reserved = next;
515         }
516         if (drm_mm_node_allocated(&hole))
517                 drm_mm_remove_node(&hole);
518         intel_runtime_pm_put(&i915->runtime_pm, wakeref);
519         mutex_unlock(&i915->drm.struct_mutex);
520
521         return err;
522 }
523
524 int i915_gem_evict_mock_selftests(void)
525 {
526         static const struct i915_subtest tests[] = {
527                 SUBTEST(igt_evict_something),
528                 SUBTEST(igt_evict_for_vma),
529                 SUBTEST(igt_evict_for_cache_color),
530                 SUBTEST(igt_evict_vm),
531                 SUBTEST(igt_overcommit),
532         };
533         struct drm_i915_private *i915;
534         intel_wakeref_t wakeref;
535         int err = 0;
536
537         i915 = mock_gem_device();
538         if (!i915)
539                 return -ENOMEM;
540
541         mutex_lock(&i915->drm.struct_mutex);
542         with_intel_runtime_pm(&i915->runtime_pm, wakeref)
543                 err = i915_subtests(tests, i915);
544
545         mutex_unlock(&i915->drm.struct_mutex);
546
547         drm_dev_put(&i915->drm);
548         return err;
549 }
550
551 int i915_gem_evict_live_selftests(struct drm_i915_private *i915)
552 {
553         static const struct i915_subtest tests[] = {
554                 SUBTEST(igt_evict_contexts),
555         };
556
557         if (i915_terminally_wedged(i915))
558                 return 0;
559
560         return i915_subtests(tests, i915);
561 }