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 */
29 #include "qxl_object.h"
31 static int qxl_reap_surface_id(struct qxl_device
*qdev
, int max_to_reap
);
34 struct qxl_ring_header header
;
43 wait_queue_head_t
*push_event
;
47 void qxl_ring_free(struct qxl_ring
*ring
)
53 qxl_ring_create(struct qxl_ring_header
*header
,
58 wait_queue_head_t
*push_event
)
60 struct qxl_ring
*ring
;
62 ring
= kmalloc(sizeof(*ring
), GFP_KERNEL
);
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
;
72 header
->notify_on_prod
= ring
->n_elements
;
73 spin_lock_init(&ring
->lock
);
77 static int qxl_check_header(struct qxl_ring
*ring
)
80 struct qxl_ring_header
*header
= &(ring
->ring
->header
);
82 spin_lock_irqsave(&ring
->lock
, flags
);
83 ret
= header
->prod
- header
->cons
< header
->num_items
;
85 header
->notify_on_cons
= header
->cons
+ 1;
86 spin_unlock_irqrestore(&ring
->lock
, flags
);
90 static int qxl_check_idle(struct qxl_ring
*ring
)
93 struct qxl_ring_header
*header
= &(ring
->ring
->header
);
95 spin_lock_irqsave(&ring
->lock
, flags
);
96 ret
= header
->prod
== header
->cons
;
97 spin_unlock_irqrestore(&ring
->lock
, flags
);
101 int qxl_ring_push(struct qxl_ring
*ring
,
102 const void *new_elt
, bool interruptible
)
104 struct qxl_ring_header
*header
= &(ring
->ring
->header
);
108 spin_lock_irqsave(&ring
->lock
, flags
);
109 if (header
->prod
- header
->cons
== header
->num_items
) {
110 header
->notify_on_cons
= header
->cons
+ 1;
112 spin_unlock_irqrestore(&ring
->lock
, flags
);
113 if (!drm_can_sleep()) {
114 while (!qxl_check_header(ring
))
118 ret
= wait_event_interruptible(*ring
->push_event
,
119 qxl_check_header(ring
));
123 wait_event(*ring
->push_event
,
124 qxl_check_header(ring
));
128 spin_lock_irqsave(&ring
->lock
, flags
);
131 idx
= header
->prod
& (ring
->n_elements
- 1);
132 elt
= ring
->ring
->elements
+ idx
* ring
->element_size
;
134 memcpy((void *)elt
, new_elt
, ring
->element_size
);
140 if (header
->prod
== header
->notify_on_prod
)
141 outb(0, ring
->prod_notify
);
143 spin_unlock_irqrestore(&ring
->lock
, flags
);
147 bool qxl_ring_pop(struct qxl_ring
*ring
,
150 volatile struct qxl_ring_header
*header
= &(ring
->ring
->header
);
151 volatile uint8_t *ring_elt
;
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
);
161 idx
= header
->cons
& (ring
->n_elements
- 1);
162 ring_elt
= ring
->ring
->elements
+ idx
* ring
->element_size
;
164 memcpy(element
, (void *)ring_elt
, ring
->element_size
);
168 spin_unlock_irqrestore(&ring
->lock
, flags
);
172 void qxl_ring_wait_idle(struct qxl_ring
*ring
)
174 struct qxl_ring_header
*header
= &(ring
->ring
->header
);
177 spin_lock_irqsave(&ring
->lock
, flags
);
178 if (ring
->ring
->header
.cons
< ring
->ring
->header
.prod
) {
179 header
->notify_on_cons
= header
->prod
;
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
);
186 spin_unlock_irqrestore(&ring
->lock
, flags
);
190 qxl_push_command_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
->bos
[0], release
->release_offset
);
198 return qxl_ring_push(qdev
->command_ring
, &cmd
, interruptible
);
202 qxl_push_cursor_ring_release(struct qxl_device
*qdev
, struct qxl_release
*release
,
203 uint32_t type
, bool interruptible
)
205 struct qxl_command cmd
;
208 cmd
.data
= qxl_bo_physical_address(qdev
, release
->bos
[0], release
->release_offset
);
210 return qxl_ring_push(qdev
->cursor_ring
, &cmd
, interruptible
);
213 bool qxl_queue_garbage_collect(struct qxl_device
*qdev
, bool flush
)
215 if (!qxl_check_idle(qdev
->release_ring
)) {
216 queue_work(qdev
->gc_queue
, &qdev
->gc_work
);
218 flush_work(&qdev
->gc_work
);
224 int qxl_garbage_collect(struct qxl_device
*qdev
)
226 struct qxl_release
*release
;
227 uint64_t id
, next_id
;
230 union qxl_release_info
*info
;
232 while (qxl_ring_pop(qdev
->release_ring
, &id
)) {
233 QXL_INFO(qdev
, "popped %lld\n", id
);
235 release
= qxl_release_from_id_locked(qdev
, id
);
239 ret
= qxl_release_reserve(qdev
, release
, false);
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
);
245 info
= qxl_release_map(qdev
, release
);
246 next_id
= info
->next
;
247 qxl_release_unmap(qdev
, release
, info
);
249 qxl_release_unreserve(qdev
, release
);
250 QXL_INFO(qdev
, "popped %lld, next %lld\n", id
,
253 switch (release
->type
) {
254 case QXL_RELEASE_DRAWABLE
:
255 case QXL_RELEASE_SURFACE_CMD
:
256 case QXL_RELEASE_CURSOR_CMD
:
259 DRM_ERROR("unexpected release type\n");
264 qxl_release_free(qdev
, release
);
269 QXL_INFO(qdev
, "%s: %lld\n", __func__
, i
);
274 int qxl_alloc_bo_reserved(struct qxl_device
*qdev
, unsigned long size
,
280 ret
= qxl_bo_create(qdev
, size
, false /* not kernel - device */,
281 QXL_GEM_DOMAIN_VRAM
, NULL
, &bo
);
283 DRM_ERROR("failed to allocate VRAM BO\n");
286 ret
= qxl_bo_reserve(bo
, false);
287 if (unlikely(ret
!= 0))
297 static int wait_for_io_cmd_user(struct qxl_device
*qdev
, uint8_t val
, long port
)
300 long addr
= qdev
->io_base
+ port
;
303 mutex_lock(&qdev
->async_io_mutex
);
304 irq_num
= atomic_read(&qdev
->irq_received_io_cmd
);
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
);
312 irq_num
= atomic_read(&qdev
->irq_received_io_cmd
);
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
);
319 mutex_unlock(&qdev
->async_io_mutex
);
323 static void wait_for_io_cmd(struct qxl_device
*qdev
, uint8_t val
, long port
)
328 ret
= wait_for_io_cmd_user(qdev
, val
, port
);
329 if (ret
== -ERESTARTSYS
)
333 int qxl_io_update_area(struct qxl_device
*qdev
, struct qxl_bo
*surf
,
334 const struct qxl_rect
*area
)
337 uint32_t surface_width
, surface_height
;
340 if (!surf
->hw_surf_alloc
)
341 DRM_ERROR("got io update area with no hw surface\n");
343 if (surf
->is_primary
)
346 surface_id
= surf
->surface_id
;
347 surface_width
= surf
->surf
.width
;
348 surface_height
= surf
->surf
.height
;
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
);
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
);
365 void qxl_io_notify_oom(struct qxl_device
*qdev
)
367 outb(0, qdev
->io_base
+ QXL_IO_NOTIFY_OOM
);
370 void qxl_io_flush_release(struct qxl_device
*qdev
)
372 outb(0, qdev
->io_base
+ QXL_IO_FLUSH_RELEASE
);
375 void qxl_io_flush_surfaces(struct qxl_device
*qdev
)
377 wait_for_io_cmd(qdev
, 0, QXL_IO_FLUSH_SURFACES_ASYNC
);
381 void qxl_io_destroy_primary(struct qxl_device
*qdev
)
383 wait_for_io_cmd(qdev
, 0, QXL_IO_DESTROY_PRIMARY_ASYNC
);
386 void qxl_io_create_primary(struct qxl_device
*qdev
, unsigned width
,
387 unsigned height
, unsigned offset
, struct qxl_bo
*bo
)
389 struct qxl_surface_create
*create
;
391 QXL_INFO(qdev
, "%s: qdev %p, ram_header %p\n", __func__
, qdev
,
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
);
400 QXL_INFO(qdev
, "%s: mem = %llx, from %p\n", __func__
, create
->mem
,
403 create
->flags
= QXL_SURF_FLAG_KEEP_DATA
;
404 create
->type
= QXL_SURF_TYPE_PRIMARY
;
406 wait_for_io_cmd(qdev
, 0, QXL_IO_CREATE_PRIMARY_ASYNC
);
409 void qxl_io_memslot_add(struct qxl_device
*qdev
, uint8_t id
)
411 QXL_INFO(qdev
, "qxl_memslot_add %d\n", id
);
412 wait_for_io_cmd(qdev
, id
, QXL_IO_MEMSLOT_ADD_ASYNC
);
415 void qxl_io_log(struct qxl_device
*qdev
, const char *fmt
, ...)
420 vsnprintf(qdev
->ram_header
->log_buf
, QXL_LOG_BUF_SIZE
, fmt
, args
);
423 * DO not do a DRM output here - this will call printk, which will
424 * call back into qxl for rendering (qxl_fb)
426 outb(0, qdev
->io_base
+ QXL_IO_LOG
);
429 void qxl_io_reset(struct qxl_device
*qdev
)
431 outb(0, qdev
->io_base
+ QXL_IO_RESET
);
434 void qxl_io_monitors_config(struct qxl_device
*qdev
)
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
449 wait_for_io_cmd(qdev
, 0, QXL_IO_MONITORS_CONFIG_ASYNC
);
452 int qxl_surface_id_alloc(struct qxl_device
*qdev
,
455 uint32_t handle
= -ENOMEM
;
459 if (idr_pre_get(&qdev
->surf_id_idr
, GFP_ATOMIC
) == 0) {
460 DRM_ERROR("Out of memory for surf idr\n");
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
);
469 if (idr_ret
== -EAGAIN
)
472 if (handle
>= qdev
->rom
->n_surfaces
) {
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);
480 surf
->surface_id
= handle
;
482 spin_lock(&qdev
->surf_id_idr_lock
);
483 qdev
->last_alloced_surf_id
= handle
;
484 spin_unlock(&qdev
->surf_id_idr_lock
);
489 void qxl_surface_id_dealloc(struct qxl_device
*qdev
,
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
);
497 int qxl_hw_surface_alloc(struct qxl_device
*qdev
,
499 struct ttm_mem_reg
*new_mem
)
501 struct qxl_surface_cmd
*cmd
;
502 struct qxl_release
*release
;
505 if (surf
->hw_surf_alloc
)
508 ret
= qxl_alloc_surface_release_reserved(qdev
, QXL_SURFACE_CMD_CREATE
,
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
;
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
]);
524 /* TODO - need to hold one of the locks to read tbo.offset */
525 cmd
->u
.surface_create
.data
= slot
->high_bits
;
527 cmd
->u
.surface_create
.data
|= (new_mem
->start
<< PAGE_SHIFT
) + surf
->tbo
.bdev
->man
[new_mem
->mem_type
].gpu_offset
;
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
);
533 surf
->surf_create
= release
;
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
);
540 qxl_push_command_ring_release(qdev
, release
, QXL_CMD_SURFACE
, false);
542 qxl_release_unreserve(qdev
, release
);
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
);
551 int qxl_hw_surface_dealloc(struct qxl_device
*qdev
,
554 struct qxl_surface_cmd
*cmd
;
555 struct qxl_release
*release
;
559 if (!surf
->hw_surf_alloc
)
562 ret
= qxl_alloc_surface_release_reserved(qdev
, QXL_SURFACE_CMD_DESTROY
,
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;
575 id
= surf
->surface_id
;
576 surf
->surface_id
= 0;
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
);
584 qxl_fence_releaseable(qdev
, release
);
586 qxl_push_command_ring_release(qdev
, release
, QXL_CMD_SURFACE
, false);
588 qxl_release_unreserve(qdev
, release
);
594 int qxl_update_surface(struct qxl_device
*qdev
, struct qxl_bo
*surf
)
596 struct qxl_rect rect
;
599 /* if we are evicting, we need to make sure the surface is up
602 rect
.right
= surf
->surf
.width
;
604 rect
.bottom
= surf
->surf
.height
;
606 ret
= qxl_io_update_area(qdev
, surf
, &rect
);
607 if (ret
== -ERESTARTSYS
)
612 void qxl_surface_evict_locked(struct qxl_device
*qdev
, struct qxl_bo
*surf
, bool do_update_area
)
614 /* no need to update area if we are just freeing the surface normally */
616 qxl_update_surface(qdev
, surf
);
618 /* nuke the surface id at the hw */
619 qxl_hw_surface_dealloc(qdev
, surf
);
622 void qxl_surface_evict(struct qxl_device
*qdev
, struct qxl_bo
*surf
, bool do_update_area
)
624 mutex_lock(&qdev
->surf_evict_mutex
);
625 qxl_surface_evict_locked(qdev
, surf
, do_update_area
);
626 mutex_unlock(&qdev
->surf_evict_mutex
);
629 static int qxl_reap_surf(struct qxl_device
*qdev
, struct qxl_bo
*surf
, bool stall
)
633 ret
= qxl_bo_reserve(surf
, false);
637 if (surf
->fence
.num_active_releases
> 0 && stall
== false) {
638 qxl_bo_unreserve(surf
);
643 mutex_unlock(&qdev
->surf_evict_mutex
);
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
);
650 mutex_lock(&qdev
->surf_evict_mutex
);
652 qxl_bo_unreserve(surf
);
656 qxl_surface_evict_locked(qdev
, surf
, true);
657 qxl_bo_unreserve(surf
);
661 static int qxl_reap_surface_id(struct qxl_device
*qdev
, int max_to_reap
)
668 mutex_lock(&qdev
->surf_evict_mutex
);
671 spin_lock(&qdev
->surf_id_idr_lock
);
672 start
= qdev
->last_alloced_surf_id
+ 1;
673 spin_unlock(&qdev
->surf_id_idr_lock
);
675 for (i
= start
; i
< start
+ qdev
->rom
->n_surfaces
; i
++) {
677 int surfid
= i
% qdev
->rom
->n_surfaces
;
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
);
689 ret
= qxl_reap_surf(qdev
, objptr
, stall
);
692 if (num_reaped
>= max_to_reap
)
695 if (num_reaped
== 0 && stall
== false) {
700 mutex_unlock(&qdev
->surf_evict_mutex
);
702 usleep_range(500, 1000);
703 qxl_queue_garbage_collect(qdev
, true);