drm: add new QXL driver. (v1.4)
[linux-2.6-microblaze.git] / drivers / gpu / drm / qxl / qxl_cmd.c
1 /*
2  * Copyright 2013 Red Hat Inc.
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Dave Airlie
23  *          Alon Levy
24  */
25
26 /* QXL cmd/ring handling */
27
28 #include "qxl_drv.h"
29 #include "qxl_object.h"
30
31 static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap);
32
33 struct ring {
34         struct qxl_ring_header      header;
35         uint8_t                     elements[0];
36 };
37
38 struct qxl_ring {
39         struct ring            *ring;
40         int                     element_size;
41         int                     n_elements;
42         int                     prod_notify;
43         wait_queue_head_t      *push_event;
44         spinlock_t             lock;
45 };
46
47 void qxl_ring_free(struct qxl_ring *ring)
48 {
49         kfree(ring);
50 }
51
52 struct qxl_ring *
53 qxl_ring_create(struct qxl_ring_header *header,
54                 int element_size,
55                 int n_elements,
56                 int prod_notify,
57                 bool set_prod_notify,
58                 wait_queue_head_t *push_event)
59 {
60         struct qxl_ring *ring;
61
62         ring = kmalloc(sizeof(*ring), GFP_KERNEL);
63         if (!ring)
64                 return NULL;
65
66         ring->ring = (struct ring *)header;
67         ring->element_size = element_size;
68         ring->n_elements = n_elements;
69         ring->prod_notify = prod_notify;
70         ring->push_event = push_event;
71         if (set_prod_notify)
72                 header->notify_on_prod = ring->n_elements;
73         spin_lock_init(&ring->lock);
74         return ring;
75 }
76
77 static int qxl_check_header(struct qxl_ring *ring)
78 {
79         int ret;
80         struct qxl_ring_header *header = &(ring->ring->header);
81         unsigned long flags;
82         spin_lock_irqsave(&ring->lock, flags);
83         ret = header->prod - header->cons < header->num_items;
84         if (ret == 0)
85                 header->notify_on_cons = header->cons + 1;
86         spin_unlock_irqrestore(&ring->lock, flags);
87         return ret;
88 }
89
90 static int qxl_check_idle(struct qxl_ring *ring)
91 {
92         int ret;
93         struct qxl_ring_header *header = &(ring->ring->header);
94         unsigned long flags;
95         spin_lock_irqsave(&ring->lock, flags);
96         ret = header->prod == header->cons;
97         spin_unlock_irqrestore(&ring->lock, flags);
98         return ret;
99 }
100
101 int qxl_ring_push(struct qxl_ring *ring,
102                   const void *new_elt, bool interruptible)
103 {
104         struct qxl_ring_header *header = &(ring->ring->header);
105         uint8_t *elt;
106         int idx, ret;
107         unsigned long flags;
108         spin_lock_irqsave(&ring->lock, flags);
109         if (header->prod - header->cons == header->num_items) {
110                 header->notify_on_cons = header->cons + 1;
111                 mb();
112                 spin_unlock_irqrestore(&ring->lock, flags);
113                 if (!drm_can_sleep()) {
114                         while (!qxl_check_header(ring))
115                                 udelay(1);
116                 } else {
117                         if (interruptible) {
118                                 ret = wait_event_interruptible(*ring->push_event,
119                                                                qxl_check_header(ring));
120                                 if (ret)
121                                         return ret;
122                         } else {
123                                 wait_event(*ring->push_event,
124                                            qxl_check_header(ring));
125                         }
126
127                 }
128                 spin_lock_irqsave(&ring->lock, flags);
129         }
130
131         idx = header->prod & (ring->n_elements - 1);
132         elt = ring->ring->elements + idx * ring->element_size;
133
134         memcpy((void *)elt, new_elt, ring->element_size);
135
136         header->prod++;
137
138         mb();
139
140         if (header->prod == header->notify_on_prod)
141                 outb(0, ring->prod_notify);
142
143         spin_unlock_irqrestore(&ring->lock, flags);
144         return 0;
145 }
146
147 bool qxl_ring_pop(struct qxl_ring *ring,
148                   void *element)
149 {
150         volatile struct qxl_ring_header *header = &(ring->ring->header);
151         volatile uint8_t *ring_elt;
152         int idx;
153         unsigned long flags;
154         spin_lock_irqsave(&ring->lock, flags);
155         if (header->cons == header->prod) {
156                 header->notify_on_prod = header->cons + 1;
157                 spin_unlock_irqrestore(&ring->lock, flags);
158                 return false;
159         }
160
161         idx = header->cons & (ring->n_elements - 1);
162         ring_elt = ring->ring->elements + idx * ring->element_size;
163
164         memcpy(element, (void *)ring_elt, ring->element_size);
165
166         header->cons++;
167
168         spin_unlock_irqrestore(&ring->lock, flags);
169         return true;
170 }
171
172 void qxl_ring_wait_idle(struct qxl_ring *ring)
173 {
174         struct qxl_ring_header *header = &(ring->ring->header);
175         unsigned long flags;
176
177         spin_lock_irqsave(&ring->lock, flags);
178         if (ring->ring->header.cons < ring->ring->header.prod) {
179                 header->notify_on_cons = header->prod;
180                 mb();
181                 spin_unlock_irqrestore(&ring->lock, flags);
182                 wait_event_interruptible(*ring->push_event,
183                                          qxl_check_idle(ring));
184                 spin_lock_irqsave(&ring->lock, flags);
185         }
186         spin_unlock_irqrestore(&ring->lock, flags);
187 }
188
189 int
190 qxl_push_command_ring_release(struct qxl_device *qdev, struct qxl_release *release,
191                               uint32_t type, bool interruptible)
192 {
193         struct qxl_command cmd;
194
195         cmd.type = type;
196         cmd.data = qxl_bo_physical_address(qdev, release->bos[0], release->release_offset);
197
198         return qxl_ring_push(qdev->command_ring, &cmd, interruptible);
199 }
200
201 int
202 qxl_push_cursor_ring_release(struct qxl_device *qdev, struct qxl_release *release,
203                              uint32_t type, bool interruptible)
204 {
205         struct qxl_command cmd;
206
207         cmd.type = type;
208         cmd.data = qxl_bo_physical_address(qdev, release->bos[0], release->release_offset);
209
210         return qxl_ring_push(qdev->cursor_ring, &cmd, interruptible);
211 }
212
213 bool qxl_queue_garbage_collect(struct qxl_device *qdev, bool flush)
214 {
215         if (!qxl_check_idle(qdev->release_ring)) {
216                 queue_work(qdev->gc_queue, &qdev->gc_work);
217                 if (flush)
218                         flush_work(&qdev->gc_work);
219                 return true;
220         }
221         return false;
222 }
223
224 int qxl_garbage_collect(struct qxl_device *qdev)
225 {
226         struct qxl_release *release;
227         uint64_t id, next_id;
228         int i = 0;
229         int ret;
230         union qxl_release_info *info;
231
232         while (qxl_ring_pop(qdev->release_ring, &id)) {
233                 QXL_INFO(qdev, "popped %lld\n", id);
234                 while (id) {
235                         release = qxl_release_from_id_locked(qdev, id);
236                         if (release == NULL)
237                                 break;
238
239                         ret = qxl_release_reserve(qdev, release, false);
240                         if (ret) {
241                                 qxl_io_log(qdev, "failed to reserve release on garbage collect %lld\n", id);
242                                 DRM_ERROR("failed to reserve release %lld\n", id);
243                         }
244
245                         info = qxl_release_map(qdev, release);
246                         next_id = info->next;
247                         qxl_release_unmap(qdev, release, info);
248
249                         qxl_release_unreserve(qdev, release);
250                         QXL_INFO(qdev, "popped %lld, next %lld\n", id,
251                                 next_id);
252
253                         switch (release->type) {
254                         case QXL_RELEASE_DRAWABLE:
255                         case QXL_RELEASE_SURFACE_CMD:
256                         case QXL_RELEASE_CURSOR_CMD:
257                                 break;
258                         default:
259                                 DRM_ERROR("unexpected release type\n");
260                                 break;
261                         }
262                         id = next_id;
263
264                         qxl_release_free(qdev, release);
265                         ++i;
266                 }
267         }
268
269         QXL_INFO(qdev, "%s: %lld\n", __func__, i);
270
271         return i;
272 }
273
274 int qxl_alloc_bo_reserved(struct qxl_device *qdev, unsigned long size,
275                           struct qxl_bo **_bo)
276 {
277         struct qxl_bo *bo;
278         int ret;
279
280         ret = qxl_bo_create(qdev, size, false /* not kernel - device */,
281                             QXL_GEM_DOMAIN_VRAM, NULL, &bo);
282         if (ret) {
283                 DRM_ERROR("failed to allocate VRAM BO\n");
284                 return ret;
285         }
286         ret = qxl_bo_reserve(bo, false);
287         if (unlikely(ret != 0))
288                 goto out_unref;
289
290         *_bo = bo;
291         return 0;
292 out_unref:
293         qxl_bo_unref(&bo);
294         return 0;
295 }
296
297 static int wait_for_io_cmd_user(struct qxl_device *qdev, uint8_t val, long port)
298 {
299         int irq_num;
300         long addr = qdev->io_base + port;
301         int ret;
302
303         mutex_lock(&qdev->async_io_mutex);
304         irq_num = atomic_read(&qdev->irq_received_io_cmd);
305
306
307         if (qdev->last_sent_io_cmd > irq_num) {
308                 ret = wait_event_interruptible(qdev->io_cmd_event,
309                                                atomic_read(&qdev->irq_received_io_cmd) > irq_num);
310                 if (ret)
311                         goto out;
312                 irq_num = atomic_read(&qdev->irq_received_io_cmd);
313         }
314         outb(val, addr);
315         qdev->last_sent_io_cmd = irq_num + 1;
316         ret = wait_event_interruptible(qdev->io_cmd_event,
317                                        atomic_read(&qdev->irq_received_io_cmd) > irq_num);
318 out:
319         mutex_unlock(&qdev->async_io_mutex);
320         return ret;
321 }
322
323 static void wait_for_io_cmd(struct qxl_device *qdev, uint8_t val, long port)
324 {
325         int ret;
326
327 restart:
328         ret = wait_for_io_cmd_user(qdev, val, port);
329         if (ret == -ERESTARTSYS)
330                 goto restart;
331 }
332
333 int qxl_io_update_area(struct qxl_device *qdev, struct qxl_bo *surf,
334                         const struct qxl_rect *area)
335 {
336         int surface_id;
337         uint32_t surface_width, surface_height;
338         int ret;
339
340         if (!surf->hw_surf_alloc)
341                 DRM_ERROR("got io update area with no hw surface\n");
342
343         if (surf->is_primary)
344                 surface_id = 0;
345         else
346                 surface_id = surf->surface_id;
347         surface_width = surf->surf.width;
348         surface_height = surf->surf.height;
349
350         if (area->left < 0 || area->top < 0 ||
351             area->right > surface_width || area->bottom > surface_height) {
352                 qxl_io_log(qdev, "%s: not doing area update for "
353                            "%d, (%d,%d,%d,%d) (%d,%d)\n", __func__, surface_id, area->left,
354                            area->top, area->right, area->bottom, surface_width, surface_height);
355                 return -EINVAL;
356         }
357         mutex_lock(&qdev->update_area_mutex);
358         qdev->ram_header->update_area = *area;
359         qdev->ram_header->update_surface = surface_id;
360         ret = wait_for_io_cmd_user(qdev, 0, QXL_IO_UPDATE_AREA_ASYNC);
361         mutex_unlock(&qdev->update_area_mutex);
362         return ret;
363 }
364
365 void qxl_io_notify_oom(struct qxl_device *qdev)
366 {
367         outb(0, qdev->io_base + QXL_IO_NOTIFY_OOM);
368 }
369
370 void qxl_io_flush_release(struct qxl_device *qdev)
371 {
372         outb(0, qdev->io_base + QXL_IO_FLUSH_RELEASE);
373 }
374
375 void qxl_io_flush_surfaces(struct qxl_device *qdev)
376 {
377         wait_for_io_cmd(qdev, 0, QXL_IO_FLUSH_SURFACES_ASYNC);
378 }
379
380
381 void qxl_io_destroy_primary(struct qxl_device *qdev)
382 {
383         wait_for_io_cmd(qdev, 0, QXL_IO_DESTROY_PRIMARY_ASYNC);
384 }
385
386 void qxl_io_create_primary(struct qxl_device *qdev, unsigned width,
387                            unsigned height, unsigned offset, struct qxl_bo *bo)
388 {
389         struct qxl_surface_create *create;
390
391         QXL_INFO(qdev, "%s: qdev %p, ram_header %p\n", __func__, qdev,
392                  qdev->ram_header);
393         create = &qdev->ram_header->create_surface;
394         create->format = bo->surf.format;
395         create->width = width;
396         create->height = height;
397         create->stride = bo->surf.stride;
398         create->mem = qxl_bo_physical_address(qdev, bo, offset);
399
400         QXL_INFO(qdev, "%s: mem = %llx, from %p\n", __func__, create->mem,
401                  bo->kptr);
402
403         create->flags = QXL_SURF_FLAG_KEEP_DATA;
404         create->type = QXL_SURF_TYPE_PRIMARY;
405
406         wait_for_io_cmd(qdev, 0, QXL_IO_CREATE_PRIMARY_ASYNC);
407 }
408
409 void qxl_io_memslot_add(struct qxl_device *qdev, uint8_t id)
410 {
411         QXL_INFO(qdev, "qxl_memslot_add %d\n", id);
412         wait_for_io_cmd(qdev, id, QXL_IO_MEMSLOT_ADD_ASYNC);
413 }
414
415 void qxl_io_log(struct qxl_device *qdev, const char *fmt, ...)
416 {
417         va_list args;
418
419         va_start(args, fmt);
420         vsnprintf(qdev->ram_header->log_buf, QXL_LOG_BUF_SIZE, fmt, args);
421         va_end(args);
422         /*
423          * DO not do a DRM output here - this will call printk, which will
424          * call back into qxl for rendering (qxl_fb)
425          */
426         outb(0, qdev->io_base + QXL_IO_LOG);
427 }
428
429 void qxl_io_reset(struct qxl_device *qdev)
430 {
431         outb(0, qdev->io_base + QXL_IO_RESET);
432 }
433
434 void qxl_io_monitors_config(struct qxl_device *qdev)
435 {
436         qxl_io_log(qdev, "%s: %d [%dx%d+%d+%d]\n", __func__,
437                    qdev->monitors_config ?
438                    qdev->monitors_config->count : -1,
439                    qdev->monitors_config && qdev->monitors_config->count ?
440                    qdev->monitors_config->heads[0].width : -1,
441                    qdev->monitors_config && qdev->monitors_config->count ?
442                    qdev->monitors_config->heads[0].height : -1,
443                    qdev->monitors_config && qdev->monitors_config->count ?
444                    qdev->monitors_config->heads[0].x : -1,
445                    qdev->monitors_config && qdev->monitors_config->count ?
446                    qdev->monitors_config->heads[0].y : -1
447                    );
448
449         wait_for_io_cmd(qdev, 0, QXL_IO_MONITORS_CONFIG_ASYNC);
450 }
451
452 int qxl_surface_id_alloc(struct qxl_device *qdev,
453                       struct qxl_bo *surf)
454 {
455         uint32_t handle = -ENOMEM;
456         int idr_ret;
457         int count = 0;
458 again:
459         if (idr_pre_get(&qdev->surf_id_idr, GFP_ATOMIC) == 0) {
460                 DRM_ERROR("Out of memory for surf idr\n");
461                 kfree(surf);
462                 goto alloc_fail;
463         }
464
465         spin_lock(&qdev->surf_id_idr_lock);
466         idr_ret = idr_get_new_above(&qdev->surf_id_idr, NULL, 1, &handle);
467         spin_unlock(&qdev->surf_id_idr_lock);
468
469         if (idr_ret == -EAGAIN)
470                 goto again;
471
472         if (handle >= qdev->rom->n_surfaces) {
473                 count++;
474                 spin_lock(&qdev->surf_id_idr_lock);
475                 idr_remove(&qdev->surf_id_idr, handle);
476                 spin_unlock(&qdev->surf_id_idr_lock);
477                 qxl_reap_surface_id(qdev, 2);
478                 goto again;
479         }
480         surf->surface_id = handle;
481
482         spin_lock(&qdev->surf_id_idr_lock);
483         qdev->last_alloced_surf_id = handle;
484         spin_unlock(&qdev->surf_id_idr_lock);
485  alloc_fail:
486         return 0;
487 }
488
489 void qxl_surface_id_dealloc(struct qxl_device *qdev,
490                             uint32_t surface_id)
491 {
492         spin_lock(&qdev->surf_id_idr_lock);
493         idr_remove(&qdev->surf_id_idr, surface_id);
494         spin_unlock(&qdev->surf_id_idr_lock);
495 }
496
497 int qxl_hw_surface_alloc(struct qxl_device *qdev,
498                          struct qxl_bo *surf,
499                          struct ttm_mem_reg *new_mem)
500 {
501         struct qxl_surface_cmd *cmd;
502         struct qxl_release *release;
503         int ret;
504
505         if (surf->hw_surf_alloc)
506                 return 0;
507
508         ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_CREATE,
509                                                  NULL,
510                                                  &release);
511         if (ret)
512                 return ret;
513
514         cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
515         cmd->type = QXL_SURFACE_CMD_CREATE;
516         cmd->u.surface_create.format = surf->surf.format;
517         cmd->u.surface_create.width = surf->surf.width;
518         cmd->u.surface_create.height = surf->surf.height;
519         cmd->u.surface_create.stride = surf->surf.stride;
520         if (new_mem) {
521                 int slot_id = surf->type == QXL_GEM_DOMAIN_VRAM ? qdev->main_mem_slot : qdev->surfaces_mem_slot;
522                 struct qxl_memslot *slot = &(qdev->mem_slots[slot_id]);
523
524                 /* TODO - need to hold one of the locks to read tbo.offset */
525                 cmd->u.surface_create.data = slot->high_bits;
526
527                 cmd->u.surface_create.data |= (new_mem->start << PAGE_SHIFT) + surf->tbo.bdev->man[new_mem->mem_type].gpu_offset;
528         } else
529                 cmd->u.surface_create.data = qxl_bo_physical_address(qdev, surf, 0);
530         cmd->surface_id = surf->surface_id;
531         qxl_release_unmap(qdev, release, &cmd->release_info);
532
533         surf->surf_create = release;
534
535         /* no need to add a release to the fence for this bo,
536            since it is only released when we ask to destroy the surface
537            and it would never signal otherwise */
538         qxl_fence_releaseable(qdev, release);
539
540         qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
541
542         qxl_release_unreserve(qdev, release);
543
544         surf->hw_surf_alloc = true;
545         spin_lock(&qdev->surf_id_idr_lock);
546         idr_replace(&qdev->surf_id_idr, surf, surf->surface_id);
547         spin_unlock(&qdev->surf_id_idr_lock);
548         return 0;
549 }
550
551 int qxl_hw_surface_dealloc(struct qxl_device *qdev,
552                            struct qxl_bo *surf)
553 {
554         struct qxl_surface_cmd *cmd;
555         struct qxl_release *release;
556         int ret;
557         int id;
558
559         if (!surf->hw_surf_alloc)
560                 return 0;
561
562         ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_DESTROY,
563                                                  surf->surf_create,
564                                                  &release);
565         if (ret)
566                 return ret;
567
568         surf->surf_create = NULL;
569         /* remove the surface from the idr, but not the surface id yet */
570         spin_lock(&qdev->surf_id_idr_lock);
571         idr_replace(&qdev->surf_id_idr, NULL, surf->surface_id);
572         spin_unlock(&qdev->surf_id_idr_lock);
573         surf->hw_surf_alloc = false;
574
575         id = surf->surface_id;
576         surf->surface_id = 0;
577
578         release->surface_release_id = id;
579         cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
580         cmd->type = QXL_SURFACE_CMD_DESTROY;
581         cmd->surface_id = id;
582         qxl_release_unmap(qdev, release, &cmd->release_info);
583
584         qxl_fence_releaseable(qdev, release);
585
586         qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
587
588         qxl_release_unreserve(qdev, release);
589
590
591         return 0;
592 }
593
594 int qxl_update_surface(struct qxl_device *qdev, struct qxl_bo *surf)
595 {
596         struct qxl_rect rect;
597         int ret;
598
599         /* if we are evicting, we need to make sure the surface is up
600            to date */
601         rect.left = 0;
602         rect.right = surf->surf.width;
603         rect.top = 0;
604         rect.bottom = surf->surf.height;
605 retry:
606         ret = qxl_io_update_area(qdev, surf, &rect);
607         if (ret == -ERESTARTSYS)
608                 goto retry;
609         return ret;
610 }
611
612 void qxl_surface_evict_locked(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
613 {
614         /* no need to update area if we are just freeing the surface normally */
615         if (do_update_area)
616                 qxl_update_surface(qdev, surf);
617
618         /* nuke the surface id at the hw */
619         qxl_hw_surface_dealloc(qdev, surf);
620 }
621
622 void qxl_surface_evict(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
623 {
624         mutex_lock(&qdev->surf_evict_mutex);
625         qxl_surface_evict_locked(qdev, surf, do_update_area);
626         mutex_unlock(&qdev->surf_evict_mutex);
627 }
628
629 static int qxl_reap_surf(struct qxl_device *qdev, struct qxl_bo *surf, bool stall)
630 {
631         int ret;
632
633         ret = qxl_bo_reserve(surf, false);
634         if (ret == -EBUSY)
635                 return -EBUSY;
636
637         if (surf->fence.num_active_releases > 0 && stall == false) {
638                 qxl_bo_unreserve(surf);
639                 return -EBUSY;
640         }
641
642         if (stall)
643                 mutex_unlock(&qdev->surf_evict_mutex);
644
645         spin_lock(&surf->tbo.bdev->fence_lock);
646         ret = ttm_bo_wait(&surf->tbo, true, true, !stall);
647         spin_unlock(&surf->tbo.bdev->fence_lock);
648
649         if (stall)
650                 mutex_lock(&qdev->surf_evict_mutex);
651         if (ret == -EBUSY) {
652                 qxl_bo_unreserve(surf);
653                 return -EBUSY;
654         }
655
656         qxl_surface_evict_locked(qdev, surf, true);
657         qxl_bo_unreserve(surf);
658         return 0;
659 }
660
661 static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap)
662 {
663         int num_reaped = 0;
664         int i, ret;
665         bool stall = false;
666         int start = 0;
667
668         mutex_lock(&qdev->surf_evict_mutex);
669 again:
670
671         spin_lock(&qdev->surf_id_idr_lock);
672         start = qdev->last_alloced_surf_id + 1;
673         spin_unlock(&qdev->surf_id_idr_lock);
674
675         for (i = start; i < start + qdev->rom->n_surfaces; i++) {
676                 void *objptr;
677                 int surfid = i % qdev->rom->n_surfaces;
678
679                 /* this avoids the case where the objects is in the
680                    idr but has been evicted half way - its makes
681                    the idr lookup atomic with the eviction */
682                 spin_lock(&qdev->surf_id_idr_lock);
683                 objptr = idr_find(&qdev->surf_id_idr, surfid);
684                 spin_unlock(&qdev->surf_id_idr_lock);
685
686                 if (!objptr)
687                         continue;
688
689                 ret = qxl_reap_surf(qdev, objptr, stall);
690                 if (ret == 0)
691                         num_reaped++;
692                 if (num_reaped >= max_to_reap)
693                         break;
694         }
695         if (num_reaped == 0 && stall == false) {
696                 stall = true;
697                 goto again;
698         }
699
700         mutex_unlock(&qdev->surf_evict_mutex);
701         if (num_reaped) {
702                 usleep_range(500, 1000);
703                 qxl_queue_garbage_collect(qdev, true);
704         }
705
706         return 0;
707 }