summaryrefslogtreecommitdiff
path: root/src/spiceqxl_display.c
blob: 7da10f038a09a11134febac4d507d717634d1aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Copyright 2011 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, and/or sell copies of the Software, and to permit persons to whom
 * the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <spice.h>

#include "qxl.h"
#include "spiceqxl_display.h"

#ifndef container_of
#define container_of(ptr, type, member) ({                      \
        const typeof(((type *) 0)->member) *__mptr = (ptr);     \
        (type *) ((char *) __mptr - offsetof(type, member));})
#endif

/* TODO: these is copied from qemu/hw/qxl.c . It shouldn't be there
 * either, these ugly undef just remove the definitions from spice-protocol/spice/ipc_ring.h
 * What should happen is using one definition, or a rename, and both in spice-protocol (because
 * all the others are there).
 * Practically speaking the only difference between the two is extra checking in this version,
 * and usage (this one takes an extra parameter, the previous is meant to be used by assignment) */
#undef SPICE_RING_PROD_ITEM
#define SPICE_RING_PROD_ITEM(r, ret) {                                  \
        typeof(r) start = r;                                            \
        typeof(r) end = r + 1;                                          \
        uint32_t prod = (r)->prod & SPICE_RING_INDEX_MASK(r);           \
        typeof(&(r)->items[prod]) m_item = &(r)->items[prod];           \
        if (!((uint8_t*)m_item >= (uint8_t*)(start) && (uint8_t*)(m_item + 1) <= (uint8_t*)(end))) { \
            abort();                                                    \
        }                                                               \
        ret = &m_item->el;                                              \
    }

#undef SPICE_RING_CONS_ITEM
#define SPICE_RING_CONS_ITEM(r, ret) {                                  \
        typeof(r) start = r;                                            \
        typeof(r) end = r + 1;                                          \
        uint32_t cons = (r)->cons & SPICE_RING_INDEX_MASK(r);           \
        typeof(&(r)->items[cons]) m_item = &(r)->items[cons];           \
        if (!((uint8_t*)m_item >= (uint8_t*)(start) && (uint8_t*)(m_item + 1) <= (uint8_t*)(end))) { \
            abort();                                                    \
        }                                                               \
        ret = &m_item->el;                                              \
    }



/* XSpice:
 * We only need a single static identity slot.
 * We actually need no slots, but less changes if we use one.
 * We currently add it during attache_worker - should not be called more
 * then once during lifetime (but we don't check)
 */
static QXLDevMemSlot slot = {
.slot_group_id = MEMSLOT_GROUP,
.slot_id = 0,
.generation = 0,
.virt_start = 0,
.virt_end = ~0,
.addr_delta = 0,
.qxl_ram_size = ~0,
};

// TODO - reall dprint, this is just to get it compiling
#define dprint(qxl, lvl, fmt, ...) printf(fmt, __VA_ARGS__)

static void interface_attach_worker(QXLInstance *sin, QXLWorker *qxl_worker)
{
    static int count = 0;
    qxl_screen_t *qxl = container_of(sin, qxl_screen_t, display_sin);

    if (++count > 1) {
        dprint(qxl, 0, "%s ignored\n", __FUNCTION__);
        return;
    }
    dprint(qxl, 1, "%s:\n", __FUNCTION__);
    spice_qxl_add_memslot(sin, &slot);
    qxl->worker = qxl_worker;
}

static void interface_set_compression_level(QXLInstance *sin, int level)
{
    qxl_screen_t *qxl = container_of(sin, qxl_screen_t, display_sin);

    dprint(qxl, 1, "%s: %d\n", __FUNCTION__, level);
    qxl->shadow_rom.compression_level = level;
    qxl->rom->compression_level = level;
}

static void interface_set_mm_time(QXLInstance *sin, uint32_t mm_time)
{
    qxl_screen_t *qxl = container_of(sin, qxl_screen_t, display_sin);

    qxl->shadow_rom.mm_clock = mm_time;
    qxl->rom->mm_clock = mm_time;
}

static void interface_get_init_info(QXLInstance *sin, QXLDevInitInfo *info)
{
    qxl_screen_t *qxl = container_of(sin, qxl_screen_t, display_sin);

    dprint(qxl, 1, "%s:\n", __FUNCTION__);
    info->memslot_gen_bits = MEMSLOT_GENERATION_BITS;
    info->memslot_id_bits = MEMSLOT_SLOT_BITS;
    info->num_memslots = NUM_MEMSLOTS;
    info->num_memslots_groups = NUM_MEMSLOTS_GROUPS;
    info->internal_groupslot_id = 0;
    info->qxl_ram_size = qxl->shadow_rom.num_pages << TARGET_PAGE_BITS;
    info->n_surfaces = NUM_SURFACES;
}

void qxl_send_events(qxl_screen_t *qxl, int events)
{
#if 0
    ErrorF("qxl_send_events %d\n", events);
    qxl_garbage_collect(qxl);
#endif
    /* we should trigger a garbage collection, but via a pipe. TODO */
}

/* called from spice server thread context only */
static int interface_get_command(QXLInstance *sin, struct QXLCommandExt *ext)
{
    qxl_screen_t *qxl = container_of(sin, qxl_screen_t, display_sin);
    QXLRam *ram = get_ram_header(qxl);
    QXLCommandRing *ring;
    QXLCommand *cmd;
    int notify;

    dprint(qxl, 2, "%s: %s\n", __FUNCTION__,
           qxl->cmdflags ? "compat" : "native");
    ring = &ram->cmd_ring;
    if (SPICE_RING_IS_EMPTY(ring)) {
        return FALSE;
    }
    SPICE_RING_CONS_ITEM(ring, cmd);
    ext->cmd      = *cmd;
    ext->group_id = MEMSLOT_GROUP;
    ext->flags    = qxl->cmdflags;
    SPICE_RING_POP(ring, notify);
    if (notify) {
        qxl_send_events(qxl, QXL_INTERRUPT_DISPLAY);
    }
    qxl->guest_primary.commands++;
    // TODO: re-enable, useful
    //qxl_track_command(qxl, ext);
    //qxl_log_command(qxl, "cmd", ext);
    return TRUE;
}

/* called from spice server thread context only */
static int interface_req_cmd_notification(QXLInstance *sin)
{
    qxl_screen_t *qxl = container_of(sin, qxl_screen_t, display_sin);
    QXLRam *header = get_ram_header(qxl);
    int wait = 1;

    SPICE_RING_CONS_WAIT(&header->cmd_ring, wait);
    return wait;
}

/* called from spice server thread context only */
static inline void qxl_push_free_res(qxl_screen_t *qxl, int flush)
{
    QXLRam *header = get_ram_header(qxl);
    QXLReleaseRing *ring = &header->release_ring;
    uint64_t *item;
    int notify;

#define QXL_FREE_BUNCH_SIZE 32

    if (ring->prod - ring->cons + 1 == ring->num_items) {
        /* ring full -- can't push */
        return;
    }
    if (!flush && qxl->oom_running) {
        /* collect everything from oom handler before pushing */
        return;
    }
    if (!flush && qxl->num_free_res < QXL_FREE_BUNCH_SIZE) {
        /* collect a bit more before pushing */
        return;
    }

    SPICE_RING_PUSH(ring, notify);
    dprint(qxl, 2, "free: push %d items, notify %s, ring %d/%d [%d,%d]\n",
           qxl->num_free_res, notify ? "yes" : "no",
           ring->prod - ring->cons, ring->num_items,
           ring->prod, ring->cons);
    if (notify) {
        qxl_send_events(qxl, QXL_INTERRUPT_DISPLAY);
    }
    SPICE_RING_PROD_ITEM(ring, item);
    *item = 0;
    qxl->num_free_res = 0;
    qxl->last_release = NULL;
}

/* called from spice server thread context only */
static void interface_release_resource(QXLInstance *sin,
                                       struct QXLReleaseInfoExt ext)
{
    qxl_screen_t *qxl = container_of(sin, qxl_screen_t, display_sin);
    QXLRam *ram = get_ram_header(qxl);
    QXLReleaseRing *ring;
    uint64_t *item, id;

    /*
     * ext->info points into guest-visible memory
     * pci bar 0, $command.release_info
     */
    ring = &ram->release_ring;
    SPICE_RING_PROD_ITEM(ring, item);
    if (*item == 0) {
        /* stick head into the ring */
        id = ext.info->id;
        ext.info->next = 0;
        *item = id;
    } else {
        /* append item to the list */
        qxl->last_release->next = ext.info->id;
        ext.info->next = 0;
    }
    qxl->last_release = ext.info;
    qxl->num_free_res++;
    dprint(qxl, 3, "%4d\r", qxl->num_free_res);
    qxl_push_free_res(qxl, 0);
}

/* called from spice server thread context only */
static int interface_get_cursor_command(QXLInstance *sin, struct QXLCommandExt *ext)
{
    qxl_screen_t *qxl = container_of(sin, qxl_screen_t, display_sin);
    QXLCursorRing *ring;
    QXLCommand *cmd;
    QXLRam *ram = get_ram_header(qxl);
    int notify;

    ring = &ram->cursor_ring;
    if (SPICE_RING_IS_EMPTY(ring)) {
        return FALSE;
    }
    SPICE_RING_CONS_ITEM(ring, cmd);
    ext->cmd      = *cmd;
    ext->group_id = MEMSLOT_GROUP;
    ext->flags    = qxl->cmdflags;
    SPICE_RING_POP(ring, notify);
    if (notify) {
        qxl_send_events(qxl, QXL_INTERRUPT_CURSOR);
    }
    qxl->guest_primary.commands++;
    //qxl_track_command(qxl, ext); // TODO - copy me
    //qxl_log_command(qxl, "csr", ext); // TODO - copy me
    return TRUE;
}

/* called from spice server thread context only */
static int interface_req_cursor_notification(QXLInstance *sin)
{
    qxl_screen_t *qxl = container_of(sin, qxl_screen_t, display_sin);
    QXLRam *ram = get_ram_header(qxl);
    int wait = 1;

    SPICE_RING_CONS_WAIT(&ram->cursor_ring, wait);
    return wait;
}

/* called from spice server thread context */
static void __attribute__ ((__noreturn__))
    interface_notify_update(QXLInstance *sin, uint32_t update_id)
{
    fprintf(stderr, "%s: abort()\n", __FUNCTION__);
    abort();
}

/* called from spice server thread context only */
static int interface_flush_resources(QXLInstance *sin)
{
    qxl_screen_t *qxl = container_of(sin, qxl_screen_t, display_sin);
    int ret;

    dprint(qxl, 1, "free: guest flush (have %d)\n", qxl->num_free_res);
    ret = qxl->num_free_res;
    if (ret) {
        qxl_push_free_res(qxl, 1);
    }
    return ret;
}

static void interface_async_complete(QXLInstance *sin, uint64_t cookie_token)
{
}

static const QXLInterface qxl_interface = {
    .base.type               = SPICE_INTERFACE_QXL,
    .base.description        = "qxl gpu",
    .base.major_version      = SPICE_INTERFACE_QXL_MAJOR,
    .base.minor_version      = SPICE_INTERFACE_QXL_MINOR,

    .attache_worker          = interface_attach_worker,
    .set_compression_level   = interface_set_compression_level,
    .set_mm_time             = interface_set_mm_time,
    .get_init_info           = interface_get_init_info,

    /* the callbacks below are called from spice server thread context */
    .get_command             = interface_get_command,
    .req_cmd_notification    = interface_req_cmd_notification,
    .release_resource        = interface_release_resource,
    .get_cursor_command      = interface_get_cursor_command,
    .req_cursor_notification = interface_req_cursor_notification,
    .notify_update           = interface_notify_update,
    .flush_resources         = interface_flush_resources,
    .async_complete          = interface_async_complete,
};

void qxl_add_spice_display_interface(qxl_screen_t *qxl)
{
    /* use this function to initialize the parts of qxl_screen_t
     * that were added directly from qemu/hw/qxl.c */
    qxl->cmdflags = 0;
    qxl->oom_running = 0;
    qxl->num_free_res = 0;

    qxl->display_sin.base.sif = &qxl_interface.base;
    qxl->display_sin.id = 0;
    qxl->display_sin.st = (struct QXLState*)qxl;
    spice_server_add_interface(qxl->spice_server, &qxl->display_sin.base);
}

void spiceqxl_display_monitors_config(qxl_screen_t *qxl)
{
    spice_qxl_monitors_config_async(&qxl->display_sin, physical_address(qxl, qxl->monitors_config, 0),
                                    MEMSLOT_GROUP, 0);
}