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 static 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
);
173 qxl_push_command_ring_release(struct qxl_device
*qdev
, struct qxl_release
*release
,
174 uint32_t type
, bool interruptible
)
176 struct qxl_command cmd
;
179 cmd
.data
= qxl_bo_physical_address(qdev
, release
->bos
[0], release
->release_offset
);
181 return qxl_ring_push(qdev
->command_ring
, &cmd
, interruptible
);
185 qxl_push_cursor_ring_release(struct qxl_device
*qdev
, struct qxl_release
*release
,
186 uint32_t type
, bool interruptible
)
188 struct qxl_command cmd
;
191 cmd
.data
= qxl_bo_physical_address(qdev
, release
->bos
[0], release
->release_offset
);
193 return qxl_ring_push(qdev
->cursor_ring
, &cmd
, interruptible
);
196 bool qxl_queue_garbage_collect(struct qxl_device
*qdev
, bool flush
)
198 if (!qxl_check_idle(qdev
->release_ring
)) {
199 queue_work(qdev
->gc_queue
, &qdev
->gc_work
);
201 flush_work(&qdev
->gc_work
);
207 int qxl_garbage_collect(struct qxl_device
*qdev
)
209 struct qxl_release
*release
;
210 uint64_t id
, next_id
;
213 union qxl_release_info
*info
;
215 while (qxl_ring_pop(qdev
->release_ring
, &id
)) {
216 QXL_INFO(qdev
, "popped %lld\n", id
);
218 release
= qxl_release_from_id_locked(qdev
, id
);
222 ret
= qxl_release_reserve(qdev
, release
, false);
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
);
228 info
= qxl_release_map(qdev
, release
);
229 next_id
= info
->next
;
230 qxl_release_unmap(qdev
, release
, info
);
232 qxl_release_unreserve(qdev
, release
);
233 QXL_INFO(qdev
, "popped %lld, next %lld\n", id
,
236 switch (release
->type
) {
237 case QXL_RELEASE_DRAWABLE
:
238 case QXL_RELEASE_SURFACE_CMD
:
239 case QXL_RELEASE_CURSOR_CMD
:
242 DRM_ERROR("unexpected release type\n");
247 qxl_release_free(qdev
, release
);
252 QXL_INFO(qdev
, "%s: %lld\n", __func__
, i
);
257 int qxl_alloc_bo_reserved(struct qxl_device
*qdev
, unsigned long size
,
263 ret
= qxl_bo_create(qdev
, size
, false /* not kernel - device */,
264 QXL_GEM_DOMAIN_VRAM
, NULL
, &bo
);
266 DRM_ERROR("failed to allocate VRAM BO\n");
269 ret
= qxl_bo_reserve(bo
, false);
270 if (unlikely(ret
!= 0))
280 static int wait_for_io_cmd_user(struct qxl_device
*qdev
, uint8_t val
, long port
)
283 long addr
= qdev
->io_base
+ port
;
286 mutex_lock(&qdev
->async_io_mutex
);
287 irq_num
= atomic_read(&qdev
->irq_received_io_cmd
);
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
);
295 irq_num
= atomic_read(&qdev
->irq_received_io_cmd
);
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
);
302 mutex_unlock(&qdev
->async_io_mutex
);
306 static void wait_for_io_cmd(struct qxl_device
*qdev
, uint8_t val
, long port
)
311 ret
= wait_for_io_cmd_user(qdev
, val
, port
);
312 if (ret
== -ERESTARTSYS
)
316 int qxl_io_update_area(struct qxl_device
*qdev
, struct qxl_bo
*surf
,
317 const struct qxl_rect
*area
)
320 uint32_t surface_width
, surface_height
;
323 if (!surf
->hw_surf_alloc
)
324 DRM_ERROR("got io update area with no hw surface\n");
326 if (surf
->is_primary
)
329 surface_id
= surf
->surface_id
;
330 surface_width
= surf
->surf
.width
;
331 surface_height
= surf
->surf
.height
;
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
);
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
);
348 void qxl_io_notify_oom(struct qxl_device
*qdev
)
350 outb(0, qdev
->io_base
+ QXL_IO_NOTIFY_OOM
);
353 void qxl_io_flush_release(struct qxl_device
*qdev
)
355 outb(0, qdev
->io_base
+ QXL_IO_FLUSH_RELEASE
);
358 void qxl_io_flush_surfaces(struct qxl_device
*qdev
)
360 wait_for_io_cmd(qdev
, 0, QXL_IO_FLUSH_SURFACES_ASYNC
);
364 void qxl_io_destroy_primary(struct qxl_device
*qdev
)
366 wait_for_io_cmd(qdev
, 0, QXL_IO_DESTROY_PRIMARY_ASYNC
);
369 void qxl_io_create_primary(struct qxl_device
*qdev
, unsigned width
,
370 unsigned height
, unsigned offset
, struct qxl_bo
*bo
)
372 struct qxl_surface_create
*create
;
374 QXL_INFO(qdev
, "%s: qdev %p, ram_header %p\n", __func__
, qdev
,
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
);
383 QXL_INFO(qdev
, "%s: mem = %llx, from %p\n", __func__
, create
->mem
,
386 create
->flags
= QXL_SURF_FLAG_KEEP_DATA
;
387 create
->type
= QXL_SURF_TYPE_PRIMARY
;
389 wait_for_io_cmd(qdev
, 0, QXL_IO_CREATE_PRIMARY_ASYNC
);
392 void qxl_io_memslot_add(struct qxl_device
*qdev
, uint8_t id
)
394 QXL_INFO(qdev
, "qxl_memslot_add %d\n", id
);
395 wait_for_io_cmd(qdev
, id
, QXL_IO_MEMSLOT_ADD_ASYNC
);
398 void qxl_io_log(struct qxl_device
*qdev
, const char *fmt
, ...)
403 vsnprintf(qdev
->ram_header
->log_buf
, QXL_LOG_BUF_SIZE
, fmt
, args
);
406 * DO not do a DRM output here - this will call printk, which will
407 * call back into qxl for rendering (qxl_fb)
409 outb(0, qdev
->io_base
+ QXL_IO_LOG
);
412 void qxl_io_reset(struct qxl_device
*qdev
)
414 outb(0, qdev
->io_base
+ QXL_IO_RESET
);
417 void qxl_io_monitors_config(struct qxl_device
*qdev
)
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
432 wait_for_io_cmd(qdev
, 0, QXL_IO_MONITORS_CONFIG_ASYNC
);
435 int qxl_surface_id_alloc(struct qxl_device
*qdev
,
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
);
451 if (handle
>= qdev
->rom
->n_surfaces
) {
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);
459 surf
->surface_id
= handle
;
461 spin_lock(&qdev
->surf_id_idr_lock
);
462 qdev
->last_alloced_surf_id
= handle
;
463 spin_unlock(&qdev
->surf_id_idr_lock
);
467 void qxl_surface_id_dealloc(struct qxl_device
*qdev
,
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
);
475 int qxl_hw_surface_alloc(struct qxl_device
*qdev
,
477 struct ttm_mem_reg
*new_mem
)
479 struct qxl_surface_cmd
*cmd
;
480 struct qxl_release
*release
;
483 if (surf
->hw_surf_alloc
)
486 ret
= qxl_alloc_surface_release_reserved(qdev
, QXL_SURFACE_CMD_CREATE
,
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
;
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
]);
502 /* TODO - need to hold one of the locks to read tbo.offset */
503 cmd
->u
.surface_create
.data
= slot
->high_bits
;
505 cmd
->u
.surface_create
.data
|= (new_mem
->start
<< PAGE_SHIFT
) + surf
->tbo
.bdev
->man
[new_mem
->mem_type
].gpu_offset
;
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
);
511 surf
->surf_create
= release
;
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
);
518 qxl_push_command_ring_release(qdev
, release
, QXL_CMD_SURFACE
, false);
520 qxl_release_unreserve(qdev
, release
);
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
);
529 int qxl_hw_surface_dealloc(struct qxl_device
*qdev
,
532 struct qxl_surface_cmd
*cmd
;
533 struct qxl_release
*release
;
537 if (!surf
->hw_surf_alloc
)
540 ret
= qxl_alloc_surface_release_reserved(qdev
, QXL_SURFACE_CMD_DESTROY
,
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;
553 id
= surf
->surface_id
;
554 surf
->surface_id
= 0;
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
);
562 qxl_fence_releaseable(qdev
, release
);
564 qxl_push_command_ring_release(qdev
, release
, QXL_CMD_SURFACE
, false);
566 qxl_release_unreserve(qdev
, release
);
572 int qxl_update_surface(struct qxl_device
*qdev
, struct qxl_bo
*surf
)
574 struct qxl_rect rect
;
577 /* if we are evicting, we need to make sure the surface is up
580 rect
.right
= surf
->surf
.width
;
582 rect
.bottom
= surf
->surf
.height
;
584 ret
= qxl_io_update_area(qdev
, surf
, &rect
);
585 if (ret
== -ERESTARTSYS
)
590 static void qxl_surface_evict_locked(struct qxl_device
*qdev
, struct qxl_bo
*surf
, bool do_update_area
)
592 /* no need to update area if we are just freeing the surface normally */
594 qxl_update_surface(qdev
, surf
);
596 /* nuke the surface id at the hw */
597 qxl_hw_surface_dealloc(qdev
, surf
);
600 void qxl_surface_evict(struct qxl_device
*qdev
, struct qxl_bo
*surf
, bool do_update_area
)
602 mutex_lock(&qdev
->surf_evict_mutex
);
603 qxl_surface_evict_locked(qdev
, surf
, do_update_area
);
604 mutex_unlock(&qdev
->surf_evict_mutex
);
607 static int qxl_reap_surf(struct qxl_device
*qdev
, struct qxl_bo
*surf
, bool stall
)
611 ret
= qxl_bo_reserve(surf
, false);
615 if (surf
->fence
.num_active_releases
> 0 && stall
== false) {
616 qxl_bo_unreserve(surf
);
621 mutex_unlock(&qdev
->surf_evict_mutex
);
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
);
628 mutex_lock(&qdev
->surf_evict_mutex
);
630 qxl_bo_unreserve(surf
);
634 qxl_surface_evict_locked(qdev
, surf
, true);
635 qxl_bo_unreserve(surf
);
639 static int qxl_reap_surface_id(struct qxl_device
*qdev
, int max_to_reap
)
646 mutex_lock(&qdev
->surf_evict_mutex
);
649 spin_lock(&qdev
->surf_id_idr_lock
);
650 start
= qdev
->last_alloced_surf_id
+ 1;
651 spin_unlock(&qdev
->surf_id_idr_lock
);
653 for (i
= start
; i
< start
+ qdev
->rom
->n_surfaces
; i
++) {
655 int surfid
= i
% qdev
->rom
->n_surfaces
;
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
);
667 ret
= qxl_reap_surf(qdev
, objptr
, stall
);
670 if (num_reaped
>= max_to_reap
)
673 if (num_reaped
== 0 && stall
== false) {
678 mutex_unlock(&qdev
->surf_evict_mutex
);
680 usleep_range(500, 1000);
681 qxl_queue_garbage_collect(qdev
, true);