2 * Copyright 2013 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Dave Airlie
26 /* QXL cmd/ring handling */
28 #include <linux/delay.h>
30 #include <drm/drm_util.h>
33 #include "qxl_object.h"
35 static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap);
38 struct qxl_ring_header header;
47 wait_queue_head_t *push_event;
51 void qxl_ring_free(struct qxl_ring *ring)
57 qxl_ring_create(struct qxl_ring_header *header,
61 wait_queue_head_t *push_event)
63 struct qxl_ring *ring;
65 ring = kmalloc(sizeof(*ring), GFP_KERNEL);
69 ring->ring = (struct ring *)header;
70 ring->element_size = element_size;
71 ring->n_elements = n_elements;
72 ring->prod_notify = prod_notify;
73 ring->push_event = push_event;
74 spin_lock_init(&ring->lock);
78 static int qxl_check_header(struct qxl_ring *ring)
81 struct qxl_ring_header *header = &(ring->ring->header);
84 spin_lock_irqsave(&ring->lock, flags);
85 ret = header->prod - header->cons < header->num_items;
87 header->notify_on_cons = header->cons + 1;
88 spin_unlock_irqrestore(&ring->lock, flags);
92 int qxl_check_idle(struct qxl_ring *ring)
95 struct qxl_ring_header *header = &(ring->ring->header);
98 spin_lock_irqsave(&ring->lock, flags);
99 ret = header->prod == header->cons;
100 spin_unlock_irqrestore(&ring->lock, flags);
104 int qxl_ring_push(struct qxl_ring *ring,
105 const void *new_elt, bool interruptible)
107 struct qxl_ring_header *header = &(ring->ring->header);
112 spin_lock_irqsave(&ring->lock, flags);
113 if (header->prod - header->cons == header->num_items) {
114 header->notify_on_cons = header->cons + 1;
116 spin_unlock_irqrestore(&ring->lock, flags);
117 if (!drm_can_sleep()) {
118 while (!qxl_check_header(ring))
122 ret = wait_event_interruptible(*ring->push_event,
123 qxl_check_header(ring));
127 wait_event(*ring->push_event,
128 qxl_check_header(ring));
132 spin_lock_irqsave(&ring->lock, flags);
135 idx = header->prod & (ring->n_elements - 1);
136 elt = ring->ring->elements + idx * ring->element_size;
138 memcpy((void *)elt, new_elt, ring->element_size);
144 if (header->prod == header->notify_on_prod)
145 outb(0, ring->prod_notify);
147 spin_unlock_irqrestore(&ring->lock, flags);
151 static bool qxl_ring_pop(struct qxl_ring *ring,
154 volatile struct qxl_ring_header *header = &(ring->ring->header);
155 volatile uint8_t *ring_elt;
159 spin_lock_irqsave(&ring->lock, flags);
160 if (header->cons == header->prod) {
161 header->notify_on_prod = header->cons + 1;
162 spin_unlock_irqrestore(&ring->lock, flags);
166 idx = header->cons & (ring->n_elements - 1);
167 ring_elt = ring->ring->elements + idx * ring->element_size;
169 memcpy(element, (void *)ring_elt, ring->element_size);
173 spin_unlock_irqrestore(&ring->lock, flags);
178 qxl_push_command_ring_release(struct qxl_device *qdev, struct qxl_release *release,
179 uint32_t type, bool interruptible)
181 struct qxl_command cmd;
184 cmd.data = qxl_bo_physical_address(qdev, release->release_bo, release->release_offset);
186 return qxl_ring_push(qdev->command_ring, &cmd, interruptible);
190 qxl_push_cursor_ring_release(struct qxl_device *qdev, struct qxl_release *release,
191 uint32_t type, bool interruptible)
193 struct qxl_command cmd;
196 cmd.data = qxl_bo_physical_address(qdev, release->release_bo, release->release_offset);
198 return qxl_ring_push(qdev->cursor_ring, &cmd, interruptible);
201 bool qxl_queue_garbage_collect(struct qxl_device *qdev, bool flush)
203 if (!qxl_check_idle(qdev->release_ring)) {
204 schedule_work(&qdev->gc_work);
206 flush_work(&qdev->gc_work);
212 int qxl_garbage_collect(struct qxl_device *qdev)
214 struct qxl_release *release;
215 uint64_t id, next_id;
217 union qxl_release_info *info;
219 while (qxl_ring_pop(qdev->release_ring, &id)) {
220 DRM_DEBUG_DRIVER("popped %lld\n", id);
222 release = qxl_release_from_id_locked(qdev, id);
226 info = qxl_release_map(qdev, release);
227 next_id = info->next;
228 qxl_release_unmap(qdev, release, info);
230 DRM_DEBUG_DRIVER("popped %lld, next %lld\n", id,
233 switch (release->type) {
234 case QXL_RELEASE_DRAWABLE:
235 case QXL_RELEASE_SURFACE_CMD:
236 case QXL_RELEASE_CURSOR_CMD:
239 DRM_ERROR("unexpected release type\n");
244 qxl_release_free(qdev, release);
249 wake_up_all(&qdev->release_event);
250 DRM_DEBUG_DRIVER("%d\n", i);
255 int qxl_alloc_bo_reserved(struct qxl_device *qdev,
256 struct qxl_release *release,
263 ret = qxl_bo_create(qdev, size, false /* not kernel - device */,
264 false, QXL_GEM_DOMAIN_VRAM, 0, NULL, &bo);
266 DRM_ERROR("failed to allocate VRAM BO\n");
269 ret = qxl_release_list_add(release, bo);
280 static int wait_for_io_cmd_user(struct qxl_device *qdev, uint8_t val, long port, bool intr)
283 long addr = qdev->io_base + port;
286 mutex_lock(&qdev->async_io_mutex);
287 irq_num = atomic_read(&qdev->irq_received_io_cmd);
288 if (qdev->last_sent_io_cmd > irq_num) {
290 ret = wait_event_interruptible_timeout(qdev->io_cmd_event,
291 atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
293 ret = wait_event_timeout(qdev->io_cmd_event,
294 atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
295 /* 0 is timeout, just bail the "hw" has gone away */
298 irq_num = atomic_read(&qdev->irq_received_io_cmd);
301 qdev->last_sent_io_cmd = irq_num + 1;
303 ret = wait_event_interruptible_timeout(qdev->io_cmd_event,
304 atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
306 ret = wait_event_timeout(qdev->io_cmd_event,
307 atomic_read(&qdev->irq_received_io_cmd) > irq_num, 5*HZ);
311 mutex_unlock(&qdev->async_io_mutex);
315 static void wait_for_io_cmd(struct qxl_device *qdev, uint8_t val, long port)
320 ret = wait_for_io_cmd_user(qdev, val, port, false);
321 if (ret == -ERESTARTSYS)
325 int qxl_io_update_area(struct qxl_device *qdev, struct qxl_bo *surf,
326 const struct qxl_rect *area)
329 uint32_t surface_width, surface_height;
332 if (!surf->hw_surf_alloc)
333 DRM_ERROR("got io update area with no hw surface\n");
335 if (surf->is_primary)
338 surface_id = surf->surface_id;
339 surface_width = surf->surf.width;
340 surface_height = surf->surf.height;
342 if (area->left < 0 || area->top < 0 ||
343 area->right > surface_width || area->bottom > surface_height)
346 mutex_lock(&qdev->update_area_mutex);
347 qdev->ram_header->update_area = *area;
348 qdev->ram_header->update_surface = surface_id;
349 ret = wait_for_io_cmd_user(qdev, 0, QXL_IO_UPDATE_AREA_ASYNC, true);
350 mutex_unlock(&qdev->update_area_mutex);
354 void qxl_io_notify_oom(struct qxl_device *qdev)
356 outb(0, qdev->io_base + QXL_IO_NOTIFY_OOM);
359 void qxl_io_flush_release(struct qxl_device *qdev)
361 outb(0, qdev->io_base + QXL_IO_FLUSH_RELEASE);
364 void qxl_io_flush_surfaces(struct qxl_device *qdev)
366 wait_for_io_cmd(qdev, 0, QXL_IO_FLUSH_SURFACES_ASYNC);
369 void qxl_io_destroy_primary(struct qxl_device *qdev)
371 wait_for_io_cmd(qdev, 0, QXL_IO_DESTROY_PRIMARY_ASYNC);
372 qdev->primary_bo->is_primary = false;
373 drm_gem_object_put(&qdev->primary_bo->tbo.base);
374 qdev->primary_bo = NULL;
377 void qxl_io_create_primary(struct qxl_device *qdev, struct qxl_bo *bo)
379 struct qxl_surface_create *create;
381 if (WARN_ON(qdev->primary_bo))
384 DRM_DEBUG_DRIVER("qdev %p, ram_header %p\n", qdev, qdev->ram_header);
385 create = &qdev->ram_header->create_surface;
386 create->format = bo->surf.format;
387 create->width = bo->surf.width;
388 create->height = bo->surf.height;
389 create->stride = bo->surf.stride;
390 create->mem = qxl_bo_physical_address(qdev, bo, 0);
392 DRM_DEBUG_DRIVER("mem = %llx, from %p\n", create->mem, bo->kptr);
394 create->flags = QXL_SURF_FLAG_KEEP_DATA;
395 create->type = QXL_SURF_TYPE_PRIMARY;
397 wait_for_io_cmd(qdev, 0, QXL_IO_CREATE_PRIMARY_ASYNC);
398 qdev->primary_bo = bo;
399 qdev->primary_bo->is_primary = true;
400 drm_gem_object_get(&qdev->primary_bo->tbo.base);
403 void qxl_io_memslot_add(struct qxl_device *qdev, uint8_t id)
405 DRM_DEBUG_DRIVER("qxl_memslot_add %d\n", id);
406 wait_for_io_cmd(qdev, id, QXL_IO_MEMSLOT_ADD_ASYNC);
409 void qxl_io_reset(struct qxl_device *qdev)
411 outb(0, qdev->io_base + QXL_IO_RESET);
414 void qxl_io_monitors_config(struct qxl_device *qdev)
416 wait_for_io_cmd(qdev, 0, QXL_IO_MONITORS_CONFIG_ASYNC);
419 int qxl_surface_id_alloc(struct qxl_device *qdev,
426 idr_preload(GFP_ATOMIC);
427 spin_lock(&qdev->surf_id_idr_lock);
428 idr_ret = idr_alloc(&qdev->surf_id_idr, NULL, 1, 0, GFP_NOWAIT);
429 spin_unlock(&qdev->surf_id_idr_lock);
435 if (handle >= qdev->rom->n_surfaces) {
437 spin_lock(&qdev->surf_id_idr_lock);
438 idr_remove(&qdev->surf_id_idr, handle);
439 spin_unlock(&qdev->surf_id_idr_lock);
440 qxl_reap_surface_id(qdev, 2);
443 surf->surface_id = handle;
445 spin_lock(&qdev->surf_id_idr_lock);
446 qdev->last_alloced_surf_id = handle;
447 spin_unlock(&qdev->surf_id_idr_lock);
451 void qxl_surface_id_dealloc(struct qxl_device *qdev,
454 spin_lock(&qdev->surf_id_idr_lock);
455 idr_remove(&qdev->surf_id_idr, surface_id);
456 spin_unlock(&qdev->surf_id_idr_lock);
459 int qxl_hw_surface_alloc(struct qxl_device *qdev,
462 struct qxl_surface_cmd *cmd;
463 struct qxl_release *release;
466 if (surf->hw_surf_alloc)
469 ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_CREATE,
475 ret = qxl_release_reserve_list(release, true);
477 qxl_release_free(qdev, release);
480 cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
481 cmd->type = QXL_SURFACE_CMD_CREATE;
482 cmd->flags = QXL_SURF_FLAG_KEEP_DATA;
483 cmd->u.surface_create.format = surf->surf.format;
484 cmd->u.surface_create.width = surf->surf.width;
485 cmd->u.surface_create.height = surf->surf.height;
486 cmd->u.surface_create.stride = surf->surf.stride;
487 cmd->u.surface_create.data = qxl_bo_physical_address(qdev, surf, 0);
488 cmd->surface_id = surf->surface_id;
489 qxl_release_unmap(qdev, release, &cmd->release_info);
491 surf->surf_create = release;
493 /* no need to add a release to the fence for this surface bo,
494 since it is only released when we ask to destroy the surface
495 and it would never signal otherwise */
496 qxl_release_fence_buffer_objects(release);
497 qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
499 surf->hw_surf_alloc = true;
500 spin_lock(&qdev->surf_id_idr_lock);
501 idr_replace(&qdev->surf_id_idr, surf, surf->surface_id);
502 spin_unlock(&qdev->surf_id_idr_lock);
506 int qxl_hw_surface_dealloc(struct qxl_device *qdev,
509 struct qxl_surface_cmd *cmd;
510 struct qxl_release *release;
514 if (!surf->hw_surf_alloc)
517 ret = qxl_alloc_surface_release_reserved(qdev, QXL_SURFACE_CMD_DESTROY,
523 surf->surf_create = NULL;
524 /* remove the surface from the idr, but not the surface id yet */
525 spin_lock(&qdev->surf_id_idr_lock);
526 idr_replace(&qdev->surf_id_idr, NULL, surf->surface_id);
527 spin_unlock(&qdev->surf_id_idr_lock);
528 surf->hw_surf_alloc = false;
530 id = surf->surface_id;
531 surf->surface_id = 0;
533 release->surface_release_id = id;
534 cmd = (struct qxl_surface_cmd *)qxl_release_map(qdev, release);
535 cmd->type = QXL_SURFACE_CMD_DESTROY;
536 cmd->surface_id = id;
537 qxl_release_unmap(qdev, release, &cmd->release_info);
539 qxl_release_fence_buffer_objects(release);
540 qxl_push_command_ring_release(qdev, release, QXL_CMD_SURFACE, false);
545 static int qxl_update_surface(struct qxl_device *qdev, struct qxl_bo *surf)
547 struct qxl_rect rect;
550 /* if we are evicting, we need to make sure the surface is up
553 rect.right = surf->surf.width;
555 rect.bottom = surf->surf.height;
557 ret = qxl_io_update_area(qdev, surf, &rect);
558 if (ret == -ERESTARTSYS)
563 static void qxl_surface_evict_locked(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
565 /* no need to update area if we are just freeing the surface normally */
567 qxl_update_surface(qdev, surf);
569 /* nuke the surface id at the hw */
570 qxl_hw_surface_dealloc(qdev, surf);
573 void qxl_surface_evict(struct qxl_device *qdev, struct qxl_bo *surf, bool do_update_area)
575 mutex_lock(&qdev->surf_evict_mutex);
576 qxl_surface_evict_locked(qdev, surf, do_update_area);
577 mutex_unlock(&qdev->surf_evict_mutex);
580 static int qxl_reap_surf(struct qxl_device *qdev, struct qxl_bo *surf, bool stall)
584 ret = qxl_bo_reserve(surf);
589 mutex_unlock(&qdev->surf_evict_mutex);
592 ret = dma_resv_wait_timeout(surf->tbo.base.resv,
593 DMA_RESV_USAGE_BOOKKEEP, true,
600 ret = dma_resv_test_signaled(surf->tbo.base.resv,
601 DMA_RESV_USAGE_BOOKKEEP);
602 ret = ret ? -EBUSY : 0;
606 mutex_lock(&qdev->surf_evict_mutex);
608 qxl_bo_unreserve(surf);
612 qxl_surface_evict_locked(qdev, surf, true);
613 qxl_bo_unreserve(surf);
617 static int qxl_reap_surface_id(struct qxl_device *qdev, int max_to_reap)
624 mutex_lock(&qdev->surf_evict_mutex);
627 spin_lock(&qdev->surf_id_idr_lock);
628 start = qdev->last_alloced_surf_id + 1;
629 spin_unlock(&qdev->surf_id_idr_lock);
631 for (i = start; i < start + qdev->rom->n_surfaces; i++) {
633 int surfid = i % qdev->rom->n_surfaces;
635 /* this avoids the case where the objects is in the
636 idr but has been evicted half way - its makes
637 the idr lookup atomic with the eviction */
638 spin_lock(&qdev->surf_id_idr_lock);
639 objptr = idr_find(&qdev->surf_id_idr, surfid);
640 spin_unlock(&qdev->surf_id_idr_lock);
645 ret = qxl_reap_surf(qdev, objptr, stall);
648 if (num_reaped >= max_to_reap)
651 if (num_reaped == 0 && stall == false) {
656 mutex_unlock(&qdev->surf_evict_mutex);
658 usleep_range(500, 1000);
659 qxl_queue_garbage_collect(qdev, true);