xref: /openbmc/linux/drivers/gpu/drm/qxl/qxl_cmd.c (revision f35e839a)
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 static 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 int
173 qxl_push_command_ring_release(struct qxl_device *qdev, struct qxl_release *release,
174 			      uint32_t type, bool interruptible)
175 {
176 	struct qxl_command cmd;
177 
178 	cmd.type = type;
179 	cmd.data = qxl_bo_physical_address(qdev, release->bos[0], release->release_offset);
180 
181 	return qxl_ring_push(qdev->command_ring, &cmd, interruptible);
182 }
183 
184 int
185 qxl_push_cursor_ring_release(struct qxl_device *qdev, struct qxl_release *release,
186 			     uint32_t type, bool interruptible)
187 {
188 	struct qxl_command cmd;
189 
190 	cmd.type = type;
191 	cmd.data = qxl_bo_physical_address(qdev, release->bos[0], release->release_offset);
192 
193 	return qxl_ring_push(qdev->cursor_ring, &cmd, interruptible);
194 }
195 
196 bool qxl_queue_garbage_collect(struct qxl_device *qdev, bool flush)
197 {
198 	if (!qxl_check_idle(qdev->release_ring)) {
199 		queue_work(qdev->gc_queue, &qdev->gc_work);
200 		if (flush)
201 			flush_work(&qdev->gc_work);
202 		return true;
203 	}
204 	return false;
205 }
206 
207 int qxl_garbage_collect(struct qxl_device *qdev)
208 {
209 	struct qxl_release *release;
210 	uint64_t id, next_id;
211 	int i = 0;
212 	int ret;
213 	union qxl_release_info *info;
214 
215 	while (qxl_ring_pop(qdev->release_ring, &id)) {
216 		QXL_INFO(qdev, "popped %lld\n", id);
217 		while (id) {
218 			release = qxl_release_from_id_locked(qdev, id);
219 			if (release == NULL)
220 				break;
221 
222 			ret = qxl_release_reserve(qdev, release, false);
223 			if (ret) {
224 				qxl_io_log(qdev, "failed to reserve release on garbage collect %lld\n", id);
225 				DRM_ERROR("failed to reserve release %lld\n", id);
226 			}
227 
228 			info = qxl_release_map(qdev, release);
229 			next_id = info->next;
230 			qxl_release_unmap(qdev, release, info);
231 
232 			qxl_release_unreserve(qdev, release);
233 			QXL_INFO(qdev, "popped %lld, next %lld\n", id,
234 				next_id);
235 
236 			switch (release->type) {
237 			case QXL_RELEASE_DRAWABLE:
238 			case QXL_RELEASE_SURFACE_CMD:
239 			case QXL_RELEASE_CURSOR_CMD:
240 				break;
241 			default:
242 				DRM_ERROR("unexpected release type\n");
243 				break;
244 			}
245 			id = next_id;
246 
247 			qxl_release_free(qdev, release);
248 			++i;
249 		}
250 	}
251 
252 	QXL_INFO(qdev, "%s: %lld\n", __func__, i);
253 
254 	return i;
255 }
256 
257 int qxl_alloc_bo_reserved(struct qxl_device *qdev, unsigned long size,
258 			  struct qxl_bo **_bo)
259 {
260 	struct qxl_bo *bo;
261 	int ret;
262 
263 	ret = qxl_bo_create(qdev, size, false /* not kernel - device */,
264 			    QXL_GEM_DOMAIN_VRAM, NULL, &bo);
265 	if (ret) {
266 		DRM_ERROR("failed to allocate VRAM BO\n");
267 		return ret;
268 	}
269 	ret = qxl_bo_reserve(bo, false);
270 	if (unlikely(ret != 0))
271 		goto out_unref;
272 
273 	*_bo = bo;
274 	return 0;
275 out_unref:
276 	qxl_bo_unref(&bo);
277 	return 0;
278 }
279 
280 static int wait_for_io_cmd_user(struct qxl_device *qdev, uint8_t val, long port)
281 {
282 	int irq_num;
283 	long addr = qdev->io_base + port;
284 	int ret;
285 
286 	mutex_lock(&qdev->async_io_mutex);
287 	irq_num = atomic_read(&qdev->irq_received_io_cmd);
288 
289 
290 	if (qdev->last_sent_io_cmd > irq_num) {
291 		ret = wait_event_interruptible(qdev->io_cmd_event,
292 					       atomic_read(&qdev->irq_received_io_cmd) > irq_num);
293 		if (ret)
294 			goto out;
295 		irq_num = atomic_read(&qdev->irq_received_io_cmd);
296 	}
297 	outb(val, addr);
298 	qdev->last_sent_io_cmd = irq_num + 1;
299 	ret = wait_event_interruptible(qdev->io_cmd_event,
300 				       atomic_read(&qdev->irq_received_io_cmd) > irq_num);
301 out:
302 	mutex_unlock(&qdev->async_io_mutex);
303 	return ret;
304 }
305 
306 static void wait_for_io_cmd(struct qxl_device *qdev, uint8_t val, long port)
307 {
308 	int ret;
309 
310 restart:
311 	ret = wait_for_io_cmd_user(qdev, val, port);
312 	if (ret == -ERESTARTSYS)
313 		goto restart;
314 }
315 
316 int qxl_io_update_area(struct qxl_device *qdev, struct qxl_bo *surf,
317 			const struct qxl_rect *area)
318 {
319 	int surface_id;
320 	uint32_t surface_width, surface_height;
321 	int ret;
322 
323 	if (!surf->hw_surf_alloc)
324 		DRM_ERROR("got io update area with no hw surface\n");
325 
326 	if (surf->is_primary)
327 		surface_id = 0;
328 	else
329 		surface_id = surf->surface_id;
330 	surface_width = surf->surf.width;
331 	surface_height = surf->surf.height;
332 
333 	if (area->left < 0 || area->top < 0 ||
334 	    area->right > surface_width || area->bottom > surface_height) {
335 		qxl_io_log(qdev, "%s: not doing area update for "
336 			   "%d, (%d,%d,%d,%d) (%d,%d)\n", __func__, surface_id, area->left,
337 			   area->top, area->right, area->bottom, surface_width, surface_height);
338 		return -EINVAL;
339 	}
340 	mutex_lock(&qdev->update_area_mutex);
341 	qdev->ram_header->update_area = *area;
342 	qdev->ram_header->update_surface = surface_id;
343 	ret = wait_for_io_cmd_user(qdev, 0, QXL_IO_UPDATE_AREA_ASYNC);
344 	mutex_unlock(&qdev->update_area_mutex);
345 	return ret;
346 }
347 
348 void qxl_io_notify_oom(struct qxl_device *qdev)
349 {
350 	outb(0, qdev->io_base + QXL_IO_NOTIFY_OOM);
351 }
352 
353 void qxl_io_flush_release(struct qxl_device *qdev)
354 {
355 	outb(0, qdev->io_base + QXL_IO_FLUSH_RELEASE);
356 }
357 
358 void qxl_io_flush_surfaces(struct qxl_device *qdev)
359 {
360 	wait_for_io_cmd(qdev, 0, QXL_IO_FLUSH_SURFACES_ASYNC);
361 }
362 
363 
364 void qxl_io_destroy_primary(struct qxl_device *qdev)
365 {
366 	wait_for_io_cmd(qdev, 0, QXL_IO_DESTROY_PRIMARY_ASYNC);
367 }
368 
369 void qxl_io_create_primary(struct qxl_device *qdev, unsigned width,
370 			   unsigned height, unsigned offset, struct qxl_bo *bo)
371 {
372 	struct qxl_surface_create *create;
373 
374 	QXL_INFO(qdev, "%s: qdev %p, ram_header %p\n", __func__, qdev,
375 		 qdev->ram_header);
376 	create = &qdev->ram_header->create_surface;
377 	create->format = bo->surf.format;
378 	create->width = width;
379 	create->height = height;
380 	create->stride = bo->surf.stride;
381 	create->mem = qxl_bo_physical_address(qdev, bo, offset);
382 
383 	QXL_INFO(qdev, "%s: mem = %llx, from %p\n", __func__, create->mem,
384 		 bo->kptr);
385 
386 	create->flags = QXL_SURF_FLAG_KEEP_DATA;
387 	create->type = QXL_SURF_TYPE_PRIMARY;
388 
389 	wait_for_io_cmd(qdev, 0, QXL_IO_CREATE_PRIMARY_ASYNC);
390 }
391 
392 void qxl_io_memslot_add(struct qxl_device *qdev, uint8_t id)
393 {
394 	QXL_INFO(qdev, "qxl_memslot_add %d\n", id);
395 	wait_for_io_cmd(qdev, id, QXL_IO_MEMSLOT_ADD_ASYNC);
396 }
397 
398 void qxl_io_log(struct qxl_device *qdev, const char *fmt, ...)
399 {
400 	va_list args;
401 
402 	va_start(args, fmt);
403 	vsnprintf(qdev->ram_header->log_buf, QXL_LOG_BUF_SIZE, fmt, args);
404 	va_end(args);
405 	/*
406 	 * DO not do a DRM output here - this will call printk, which will
407 	 * call back into qxl for rendering (qxl_fb)
408 	 */
409 	outb(0, qdev->io_base + QXL_IO_LOG);
410 }
411 
412 void qxl_io_reset(struct qxl_device *qdev)
413 {
414 	outb(0, qdev->io_base + QXL_IO_RESET);
415 }
416 
417 void qxl_io_monitors_config(struct qxl_device *qdev)
418 {
419 	qxl_io_log(qdev, "%s: %d [%dx%d+%d+%d]\n", __func__,
420 		   qdev->monitors_config ?
421 		   qdev->monitors_config->count : -1,
422 		   qdev->monitors_config && qdev->monitors_config->count ?
423 		   qdev->monitors_config->heads[0].width : -1,
424 		   qdev->monitors_config && qdev->monitors_config->count ?
425 		   qdev->monitors_config->heads[0].height : -1,
426 		   qdev->monitors_config && qdev->monitors_config->count ?
427 		   qdev->monitors_config->heads[0].x : -1,
428 		   qdev->monitors_config && qdev->monitors_config->count ?
429 		   qdev->monitors_config->heads[0].y : -1
430 		   );
431 
432 	wait_for_io_cmd(qdev, 0, QXL_IO_MONITORS_CONFIG_ASYNC);
433 }
434 
435 int qxl_surface_id_alloc(struct qxl_device *qdev,
436 		      struct qxl_bo *surf)
437 {
438 	uint32_t handle;
439 	int idr_ret;
440 	int count = 0;
441 again:
442 	idr_preload(GFP_ATOMIC);
443 	spin_lock(&qdev->surf_id_idr_lock);
444 	idr_ret = idr_alloc(&qdev->surf_id_idr, NULL, 1, 0, GFP_NOWAIT);
445 	spin_unlock(&qdev->surf_id_idr_lock);
446 	idr_preload_end();
447 	if (idr_ret < 0)
448 		return idr_ret;
449 	handle = idr_ret;
450 
451 	if (handle >= qdev->rom->n_surfaces) {
452 		count++;
453 		spin_lock(&qdev->surf_id_idr_lock);
454 		idr_remove(&qdev->surf_id_idr, handle);
455 		spin_unlock(&qdev->surf_id_idr_lock);
456 		qxl_reap_surface_id(qdev, 2);
457 		goto again;
458 	}
459 	surf->surface_id = handle;
460 
461 	spin_lock(&qdev->surf_id_idr_lock);
462 	qdev->last_alloced_surf_id = handle;
463 	spin_unlock(&qdev->surf_id_idr_lock);
464 	return 0;
465 }
466 
467 void qxl_surface_id_dealloc(struct qxl_device *qdev,
468 			    uint32_t surface_id)
469 {
470 	spin_lock(&qdev->surf_id_idr_lock);
471 	idr_remove(&qdev->surf_id_idr, surface_id);
472 	spin_unlock(&qdev->surf_id_idr_lock);
473 }
474 
475 int qxl_hw_surface_alloc(struct qxl_device *qdev,
476 			 struct qxl_bo *surf,
477 			 struct ttm_mem_reg *new_mem)
478 {
479 	struct qxl_surface_cmd *cmd;
480 	struct qxl_release *release;
481 	int ret;
482 
483 	if (surf->hw_surf_alloc)
484 		return 0;
485 
486 	ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_CREATE,
487 						 NULL,
488 						 &release);
489 	if (ret)
490 		return ret;
491 
492 	cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
493 	cmd->type = QXL_SURFACE_CMD_CREATE;
494 	cmd->u.surface_create.format = surf->surf.format;
495 	cmd->u.surface_create.width = surf->surf.width;
496 	cmd->u.surface_create.height = surf->surf.height;
497 	cmd->u.surface_create.stride = surf->surf.stride;
498 	if (new_mem) {
499 		int slot_id = surf->type == QXL_GEM_DOMAIN_VRAM ? qdev->main_mem_slot : qdev->surfaces_mem_slot;
500 		struct qxl_memslot *slot = &(qdev->mem_slots[slot_id]);
501 
502 		/* TODO - need to hold one of the locks to read tbo.offset */
503 		cmd->u.surface_create.data = slot->high_bits;
504 
505 		cmd->u.surface_create.data |= (new_mem->start << PAGE_SHIFT) + surf->tbo.bdev->man[new_mem->mem_type].gpu_offset;
506 	} else
507 		cmd->u.surface_create.data = qxl_bo_physical_address(qdev, surf, 0);
508 	cmd->surface_id = surf->surface_id;
509 	qxl_release_unmap(qdev, release, &cmd->release_info);
510 
511 	surf->surf_create = release;
512 
513 	/* no need to add a release to the fence for this bo,
514 	   since it is only released when we ask to destroy the surface
515 	   and it would never signal otherwise */
516 	qxl_fence_releaseable(qdev, release);
517 
518 	qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
519 
520 	qxl_release_unreserve(qdev, release);
521 
522 	surf->hw_surf_alloc = true;
523 	spin_lock(&qdev->surf_id_idr_lock);
524 	idr_replace(&qdev->surf_id_idr, surf, surf->surface_id);
525 	spin_unlock(&qdev->surf_id_idr_lock);
526 	return 0;
527 }
528 
529 int qxl_hw_surface_dealloc(struct qxl_device *qdev,
530 			   struct qxl_bo *surf)
531 {
532 	struct qxl_surface_cmd *cmd;
533 	struct qxl_release *release;
534 	int ret;
535 	int id;
536 
537 	if (!surf->hw_surf_alloc)
538 		return 0;
539 
540 	ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_DESTROY,
541 						 surf->surf_create,
542 						 &release);
543 	if (ret)
544 		return ret;
545 
546 	surf->surf_create = NULL;
547 	/* remove the surface from the idr, but not the surface id yet */
548 	spin_lock(&qdev->surf_id_idr_lock);
549 	idr_replace(&qdev->surf_id_idr, NULL, surf->surface_id);
550 	spin_unlock(&qdev->surf_id_idr_lock);
551 	surf->hw_surf_alloc = false;
552 
553 	id = surf->surface_id;
554 	surf->surface_id = 0;
555 
556 	release->surface_release_id = id;
557 	cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
558 	cmd->type = QXL_SURFACE_CMD_DESTROY;
559 	cmd->surface_id = id;
560 	qxl_release_unmap(qdev, release, &cmd->release_info);
561 
562 	qxl_fence_releaseable(qdev, release);
563 
564 	qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
565 
566 	qxl_release_unreserve(qdev, release);
567 
568 
569 	return 0;
570 }
571 
572 int qxl_update_surface(struct qxl_device *qdev, struct qxl_bo *surf)
573 {
574 	struct qxl_rect rect;
575 	int ret;
576 
577 	/* if we are evicting, we need to make sure the surface is up
578 	   to date */
579 	rect.left = 0;
580 	rect.right = surf->surf.width;
581 	rect.top = 0;
582 	rect.bottom = surf->surf.height;
583 retry:
584 	ret = qxl_io_update_area(qdev, surf, &rect);
585 	if (ret == -ERESTARTSYS)
586 		goto retry;
587 	return ret;
588 }
589 
590 static void qxl_surface_evict_locked(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
591 {
592 	/* no need to update area if we are just freeing the surface normally */
593 	if (do_update_area)
594 		qxl_update_surface(qdev, surf);
595 
596 	/* nuke the surface id at the hw */
597 	qxl_hw_surface_dealloc(qdev, surf);
598 }
599 
600 void qxl_surface_evict(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
601 {
602 	mutex_lock(&qdev->surf_evict_mutex);
603 	qxl_surface_evict_locked(qdev, surf, do_update_area);
604 	mutex_unlock(&qdev->surf_evict_mutex);
605 }
606 
607 static int qxl_reap_surf(struct qxl_device *qdev, struct qxl_bo *surf, bool stall)
608 {
609 	int ret;
610 
611 	ret = qxl_bo_reserve(surf, false);
612 	if (ret == -EBUSY)
613 		return -EBUSY;
614 
615 	if (surf->fence.num_active_releases > 0 && stall == false) {
616 		qxl_bo_unreserve(surf);
617 		return -EBUSY;
618 	}
619 
620 	if (stall)
621 		mutex_unlock(&qdev->surf_evict_mutex);
622 
623 	spin_lock(&surf->tbo.bdev->fence_lock);
624 	ret = ttm_bo_wait(&surf->tbo, true, true, !stall);
625 	spin_unlock(&surf->tbo.bdev->fence_lock);
626 
627 	if (stall)
628 		mutex_lock(&qdev->surf_evict_mutex);
629 	if (ret == -EBUSY) {
630 		qxl_bo_unreserve(surf);
631 		return -EBUSY;
632 	}
633 
634 	qxl_surface_evict_locked(qdev, surf, true);
635 	qxl_bo_unreserve(surf);
636 	return 0;
637 }
638 
639 static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap)
640 {
641 	int num_reaped = 0;
642 	int i, ret;
643 	bool stall = false;
644 	int start = 0;
645 
646 	mutex_lock(&qdev->surf_evict_mutex);
647 again:
648 
649 	spin_lock(&qdev->surf_id_idr_lock);
650 	start = qdev->last_alloced_surf_id + 1;
651 	spin_unlock(&qdev->surf_id_idr_lock);
652 
653 	for (i = start; i < start + qdev->rom->n_surfaces; i++) {
654 		void *objptr;
655 		int surfid = i % qdev->rom->n_surfaces;
656 
657 		/* this avoids the case where the objects is in the
658 		   idr but has been evicted half way - its makes
659 		   the idr lookup atomic with the eviction */
660 		spin_lock(&qdev->surf_id_idr_lock);
661 		objptr = idr_find(&qdev->surf_id_idr, surfid);
662 		spin_unlock(&qdev->surf_id_idr_lock);
663 
664 		if (!objptr)
665 			continue;
666 
667 		ret = qxl_reap_surf(qdev, objptr, stall);
668 		if (ret == 0)
669 			num_reaped++;
670 		if (num_reaped >= max_to_reap)
671 			break;
672 	}
673 	if (num_reaped == 0 && stall == false) {
674 		stall = true;
675 		goto again;
676 	}
677 
678 	mutex_unlock(&qdev->surf_evict_mutex);
679 	if (num_reaped) {
680 		usleep_range(500, 1000);
681 		qxl_queue_garbage_collect(qdev, true);
682 	}
683 
684 	return 0;
685 }
686