summaryrefslogtreecommitdiff
path: root/src/qxl_bo_ums.c
blob: 7c26f508cd8d5bae8d139d1ee286126bb65a9657 (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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <spice/qxl_dev.h>

#ifndef XSPICE
#ifdef XSERVER_PCIACCESS
#include "pciaccess.h"
#endif
#include "fb.h"
#include "vgaHW.h"
#endif /* XSPICE */

#include <string.h>
#include <stdlib.h>
#include "qxl_bo.h"
#include "qxl_bo_int.h"

#include "qxl_mem.h"

typedef struct
{
    uint8_t	generation;
    uint64_t	start_phys_addr;
    uint64_t	end_phys_addr;
    uint64_t	start_virt_addr;
    uint64_t	end_virt_addr;
    uint64_t	high_bits;
} qxl_memslot_t;

enum {
    QXL_DEVICE_PRIMARY_UNDEFINED,
    QXL_DEVICE_PRIMARY_NONE,
    QXL_DEVICE_PRIMARY_CREATED,
};

struct qxl_ums_bo_manager {
    struct qxl_bo_manager base;
    struct qxl_mem *mem;
    struct qxl_mem *surf_mem;

    void *			ram;	/* Command RAM */
    void *			ram_physical;
    void *			vram;	/* Surface RAM */
    void *			vram_physical;
    struct QXLRom *		rom;    /* Parameter RAM */

    struct qxl_ring *command_ring;
    struct qxl_ring *cursor_ring;
    struct qxl_ring *release_ring;

#ifndef XSPICE
    void *			io_pages;
    void *			io_pages_physical;

#ifdef XSERVER_LIBPCIACCESS
    struct pci_device *		pci;
#else
    pciVideoPtr			pci;
#endif
#endif /* XSPICE */

    qxl_memslot_t *		mem_slots;
    uint8_t			n_mem_slots;

    uint8_t			main_mem_slot;
    uint8_t			slot_id_bits;
    uint8_t			slot_gen_bits;
    uint64_t			va_slot_mask;

    uint8_t			vram_mem_slot;

};

static inline uint64_t
physical_address (struct qxl_ums_bo_manager *qxl,
		void *virtual, uint8_t slot_id)
{
    qxl_memslot_t *p_slot = &(qxl->mem_slots[slot_id]);

    return p_slot->high_bits | ((unsigned long)virtual - p_slot->start_virt_addr);
}

static inline void *
virtual_address (struct qxl_ums_bo_manager *qxl, void *physical, uint8_t slot_id)
{
    qxl_memslot_t *p_slot = &(qxl->mem_slots[slot_id]);
    unsigned long virt;

    virt = ((unsigned long)physical) & qxl->va_slot_mask;
    virt += p_slot->start_virt_addr;

    return (void *)virt;
}

static inline void *
u64_to_pointer (uint64_t u)
{
    return (void *)(unsigned long)u;
}

static inline uint64_t
pointer_to_u64 (void *p)
{
    return (uint64_t)(unsigned long)p;
}

static uint64_t qxl_garbage_collect_ums(struct qxl_bo_manager *bom,
					uint64_t id)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
#define POINTER_MASK ((1 << 2) - 1)
	union QXLReleaseInfo *info = u64_to_pointer (id & ~POINTER_MASK);
	struct QXLCursorCmd *cmd = (struct QXLCursorCmd *)info;
	struct QXLDrawable *drawable = (struct QXLDrawable *)info;
	struct QXLSurfaceCmd *surface_cmd = (struct QXLSurfaceCmd *)info;
	int is_cursor = FALSE;
	int is_surface = FALSE;
	int is_drawable = FALSE;
    
	if ((id & POINTER_MASK) == 1)
		is_cursor = TRUE;
	else if ((id & POINTER_MASK) == 2)
		is_surface = TRUE;
	else
		is_drawable = TRUE;
    
	if (is_cursor && cmd->type == QXL_CURSOR_SET)
	{
		struct QXLCursor *cursor = (void *)virtual_address (
			qxl, u64_to_pointer (cmd->u.set.shape), qxl->main_mem_slot);
	
		qxl_free (qxl->mem, cursor, "cursor image");
    }
    else if (is_drawable && drawable->type == QXL_DRAW_COPY)
    {
	struct QXLImage *image = virtual_address (
	    qxl, u64_to_pointer (drawable->u.copy.src_bitmap), qxl->main_mem_slot);
	
	if (image->descriptor.type == SPICE_IMAGE_TYPE_SURFACE)
	{
	    qxl_surface_unref (qxl->surface_cache, image->surface_image.surface_id);
	    qxl_surface_cache_sanity_check (qxl->surface_cache);
	    qxl_free (qxl->mem, image, "surface image");
	}
	else
	{
	    qxl_image_destroy (qxl, image);
	}
    }
    else if (is_drawable && drawable->type == QXL_DRAW_COMPOSITE)
    {
	struct QXLTransform *src_trans, *mask_trans;
	struct QXLImage *src_img, *mask_img;
	struct QXLComposite *composite = &drawable->u.composite;
	
	/* Source */
	src_img = virtual_address (
	    qxl, u64_to_pointer (drawable->u.composite.src), qxl->main_mem_slot);
	qxl_free (qxl->mem, src_img, "image struct");
	
	if (composite->src_transform)
	{
	    src_trans = virtual_address (
		qxl, u64_to_pointer (composite->src_transform), qxl->main_mem_slot);
	    qxl_free (qxl->mem, src_trans, "transform");
	}
	
	/* Mask */
	if (drawable->u.composite.mask)
	{
	    if (drawable->u.composite.mask_transform)
	    {
		mask_trans = virtual_address (
		    qxl, u64_to_pointer (drawable->u.composite.mask_transform), qxl->main_mem_slot);
		
		qxl_free (qxl->mem, mask_trans, "transform");
	    }
	    
	    mask_img = virtual_address (
		qxl, u64_to_pointer (drawable->u.composite.mask), qxl->main_mem_slot);
	    qxl_free (qxl->mem, mask_img, "image struct");
	}
    }
    else if (is_surface && surface_cmd->type == QXL_SURFACE_CMD_DESTROY)
    {
	qxl_surface_recycle (qxl->surface_cache, surface_cmd->surface_id);
	qxl_surface_cache_sanity_check (qxl->surface_cache);
    }
    
    id = info->next;
    
    qxl_free (qxl->mem, info, "command");
    
    return id;
}
static struct qxl_bo *ums_bo_alloc(struct qxl_bo_manager *_bom,
				   uint32_t size,
				   uint32_t type,
				   const char *name)
{
    struct qxl_ums_bo_manager *bom = (struct qxl_ums_bo_manager *)_bom;
    struct qxl_bo_int *bo = calloc(1, sizeof(struct qxl_bo_int));

    bo->size = size;

    if (type == QXL_BO_SURFACE) {
	bo->ptr = qxl_alloc(bom->surf_mem, size, name);
    } else {
	bo->ptr = qxl_alloc(bom->mem, size, name);
    }

    bo->type = type;
    bo->name = name;
    return (struct qxl_bo *)bo;
}

static void ums_bo_ref(struct qxl_bo_int *bo)
{
}

static struct qxl_bo_int *ums_bo_unref(struct qxl_bo_int *bo)
{
    struct qxl_ums_bo_manager *bom = (struct qxl_ums_bo_manager *)bo->bom;

    if (bo->cref)
	return bo;

    if (bo->type == QXL_BO_SURFACE)
	qxl_free(bom->surf_mem, bo->ptr, bo->name);
    else
	qxl_free(bom->mem, bo->ptr, bo->name);
    free(bo);
    return NULL;
}

static int ums_bo_map(struct qxl_bo_int *bo, int write)
{
    return 0;
}

static void ums_bo_unmap(struct qxl_bo_int *bo)
{
}
#ifndef XSPICE
static void
qxl_wait_for_io_command (struct qxl_bo_manager *bom)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
    struct QXLRam *ram_header;
    
    ram_header = (void *)((unsigned long)qxl->ram + qxl->rom->ram_header_offset);
    
    while (!(ram_header->int_pending & QXL_INTERRUPT_IO_CMD))
	usleep (1);
    
    ram_header->int_pending &= ~QXL_INTERRUPT_IO_CMD;
}
#endif

void
qxl_io_update_area (struct qxl_bo_manager *bom)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
#ifndef XSPICE
    if (qxl->pci->revision >= 3)
    {
	ioport_write (qxl, QXL_IO_UPDATE_AREA_ASYNC, 0);
	qxl_wait_for_io_command (qxl);
    }
    else
    {
	ioport_write (qxl, QXL_IO_UPDATE_AREA, 0);
    }
#else
    ioport_write (qxl, QXL_IO_UPDATE_AREA, 0);
#endif
}

static void
qxl_io_memslot_add (struct qxl_bo_manager *bom, uint8_t id)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
#ifndef XSPICE
    if (qxl->pci->revision >= 3)
    {
	ioport_write (qxl, QXL_IO_MEMSLOT_ADD_ASYNC, id);
	qxl_wait_for_io_command (qxl);
    }
    else
    {
	ioport_write (qxl, QXL_IO_MEMSLOT_ADD, id);
    }
#else
    ioport_write (qxl, QXL_IO_MEMSLOT_ADD, id);
#endif
}

static void
qxl_io_create_primary (struct qxl_bo_manager *bom)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
#ifndef XSPICE
    if (qxl->pci->revision >= 3)
    {
	ioport_write (qxl, QXL_IO_CREATE_PRIMARY_ASYNC, 0);
	qxl_wait_for_io_command (qxl);
    }
    else
    {
	ioport_write (qxl, QXL_IO_CREATE_PRIMARY, 0);
    }
#else
    ioport_write (qxl, QXL_IO_CREATE_PRIMARY, 0);
#endif
}

static void ums_update_area(struct qxl_bo_manager *bom, uint32_t surface_id,
			    int x1, int y1, int x2, int y2)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
	struct QXLRam *ram_header = get_ram_header (qxl);

	ram_header->update_area.top = y1;
	ram_header->update_area.bottom = y2;
	ram_header->update_area.left = x1;
	ram_header->update_area.right = x2;

	ram_header->update_surface = surface_id;

	qxl_io_update_area(qxl);
}

struct qxl_bo_funcs qxl_bo_ums_funcs = {
    ums_bo_alloc,
    ums_bo_ref,
    ums_bo_unref,
    ums_bo_map,
    ums_bo_unmap,

    ums_update_area,
};


#ifdef XSPICE
static void
unmap_memory_helper (struct qxl_bo_manager *bom)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
    free (qxl->ram);
    free (qxl->vram);
    free (qxl->rom);
    qxl->ram = qxl->ram_physical = qxl->vram = qxl->rom = NULL;
}

static void
map_memory_helper (struct qxl_bo_manager *bom)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
    qxl->ram = malloc (RAM_SIZE);
    qxl->ram_size = RAM_SIZE;
    qxl->ram_physical = qxl->ram;
    qxl->vram = malloc (VRAM_SIZE);
    qxl->vram_size = VRAM_SIZE;
    qxl->vram_physical = qxl->vram;
    qxl->rom = malloc (ROM_SIZE);
    
    init_qxl_rom (qxl, ROM_SIZE);
}

#else /* Default */

static void
unmap_memory_helper (struct qxl_bo_manager *bom)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
#ifdef XSERVER_LIBPCIACCESS
    if (qxl->ram)
	pci_device_unmap_range (qxl->pci, qxl->ram, qxl->pci->regions[0].size);
    if (qxl->vram)
	pci_device_unmap_range (qxl->pci, qxl->vram, qxl->pci->regions[1].size);
    if (qxl->rom)
	pci_device_unmap_range (qxl->pci, qxl->rom, qxl->pci->regions[2].size);
#else
    if (qxl->ram)
	xf86UnMapVidMem (scrnIndex, qxl->ram, (1 << qxl->pci->size[0]));
    if (qxl->vram)
	xf86UnMapVidMem (scrnIndex, qxl->vram, (1 << qxl->pci->size[1]));
    if (qxl->rom)
	xf86UnMapVidMem (scrnIndex, qxl->rom, (1 << qxl->pci->size[2]));
#endif
    qxl->ram = qxl->ram_physical = qxl->vram = qxl->rom = NULL;
}

static void
map_memory_helper (struct qxl_bo_manager *bom)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
#ifdef XSERVER_LIBPCIACCESS
    pci_device_map_range (qxl->pci, qxl->pci->regions[0].base_addr,
                          qxl->pci->regions[0].size,
                          PCI_DEV_MAP_FLAG_WRITABLE | PCI_DEV_MAP_FLAG_WRITE_COMBINE,
                          &qxl->ram);
    qxl->ram_physical = u64_to_pointer (qxl->pci->regions[0].base_addr);
    qxl->ram_size = qxl->pci->regions[0].size;
    
    pci_device_map_range (qxl->pci, qxl->pci->regions[1].base_addr,
                          qxl->pci->regions[1].size,
                          PCI_DEV_MAP_FLAG_WRITABLE,
                          &qxl->vram);
    qxl->vram_physical = u64_to_pointer (qxl->pci->regions[1].base_addr);
    qxl->vram_size = qxl->pci->regions[1].size;
    
    pci_device_map_range (qxl->pci, qxl->pci->regions[2].base_addr,
                          qxl->pci->regions[2].size, 0,
                          (void **)&qxl->rom);
    
    qxl->io_base = qxl->pci->regions[3].base_addr;
#else
    qxl->ram = xf86MapPciMem (scrnIndex, VIDMEM_FRAMEBUFFER,
                              qxl->pci_tag, qxl->pci->memBase[0],
                              (1 << qxl->pci->size[0]));
    qxl->ram_physical = (void *)qxl->pci->memBase[0];
    
    qxl->vram = xf86MapPciMem (scrnIndex, VIDMEM_MMIO | VIDMEM_MMIO_32BIT,
                               qxl->pci_tag, qxl->pci->memBase[1],
                               (1 << qxl->pci->size[1]));
    qxl->vram_physical = (void *)qxl->pci->memBase[1];
    qxl->vram_size = (1 << qxl->pci->size[1]);
    
    qxl->rom = xf86MapPciMem (scrnIndex, VIDMEM_MMIO | VIDMEM_MMIO_32BIT,
                              qxl->pci_tag, qxl->pci->memBase[2],
                              (1 << qxl->pci->size[2]));
    
    qxl->io_base = qxl->pci->ioBase[3];
#endif
}

#endif /* XSPICE */

struct qxl_bo_manager *qxl_bo_ums_manager_create(void *pci)
{
    struct qxl_ums_bo_manager *bom = calloc(1, sizeof(struct qxl_ums_bo_manager));
    struct QXLRam *ram_header;

    if (!bom)
	return NULL;

    bom->base.funcs = &qxl_bo_ums_funcs;
    bom->pci = pci;

    ram_header = (void *)((unsigned long)qxl->ram +
			  (unsigned long)qxl->rom->ram_header_offset);
    bom->command_ring = qxl_ring_create ((struct qxl_ring_header *)&(ram_header->cmd_ring),
					 sizeof (struct QXLCommand),
					 QXL_COMMAND_RING_SIZE, QXL_IO_NOTIFY_CMD, qxl);
    bom->cursor_ring = qxl_ring_create ((struct qxl_ring_header *)&(ram_header->cursor_ring),
					sizeof (struct QXLCommand),
					QXL_CURSOR_RING_SIZE, QXL_IO_NOTIFY_CURSOR, qxl);
    bom->release_ring = qxl_ring_create ((struct qxl_ring_header *)&(ram_header->release_ring),
					 sizeof (uint64_t),
					 QXL_RELEASE_RING_SIZE, 0, qxl);
    }
    return &bom->base;
}

void qxl_bo_ums_create_mem(struct qxl_bo_manager *manager, uint32_t type, void *start, uint32_t size)
{
    struct qxl_ums_bo_manager *bom = (struct qxl_ums_bo_manager *)manager;

    if (type == QXL_BO_SURFACE) {
	bom->surf_mem = qxl_mem_create(start, size);
    } else {
	bom->mem = qxl_mem_create(start, size);
    }
}

 
static void
qxl_ums_reset_and_create_mem_slots (struct qxl_bo_manager *bom)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
    ioport_write (qxl, QXL_IO_RESET, 0);
    qxl->device_primary = QXL_DEVICE_PRIMARY_NONE;
    /* Mem slots */
    ErrorF ("slots start: %d, slots end: %d\n",
            qxl->rom->slots_start,
            qxl->rom->slots_end);
    
    /* Main slot */
    qxl->n_mem_slots = qxl->rom->slots_end;
    qxl->slot_gen_bits = qxl->rom->slot_gen_bits;
    qxl->slot_id_bits = qxl->rom->slot_id_bits;
    qxl->va_slot_mask = (~(uint64_t)0) >> (qxl->slot_id_bits + qxl->slot_gen_bits);
    
    qxl->mem_slots = xnfalloc (qxl->n_mem_slots * sizeof (qxl_memslot_t));
    
#ifdef XSPICE
    qxl->main_mem_slot = qxl->vram_mem_slot = setup_slot (qxl, 0, 0, ~0, 0, ~0);
#else /* QXL */
    qxl->main_mem_slot = setup_slot (qxl, 0,
                                     (unsigned long)qxl->ram_physical,
                                     (unsigned long)qxl->ram_physical + qxl->surface0_size +
                                     (unsigned long)qxl->rom->num_pages * getpagesize (),
                                     (uint64_t)(uintptr_t)qxl->ram,
                                     (uint64_t)(uintptr_t)qxl->ram + qxl->surface0_size +
                                     (unsigned long)qxl->rom->num_pages * getpagesize ()
	);
    qxl->vram_mem_slot = setup_slot (qxl, 1,
                                     (unsigned long)qxl->vram_physical,
                                     (unsigned long)qxl->vram_physical + (unsigned long)qxl->vram_size,
                                     (uint64_t)(uintptr_t)qxl->vram,
                                     (uint64_t)(uintptr_t)qxl->vram + (uint64_t)qxl->vram_size);
#endif
}

static void
qxl_mark_mem_unverifiable (struct qxl_bo_manager *bom)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
    qxl_mem_unverifiable (qxl->mem);
    qxl_mem_unverifiable (qxl->surf_mem);
}

void
qxl_io_destroy_all_surfaces (struct qxl_bo_manager *bom)
{
    struct qxl_ums_bo_manager *qxl = (struct qxl_ums_bo_manager *)bom;
#ifndef XSPICE
    if (qxl->pci->revision >= 3)
    {
	ioport_write (qxl, QXL_IO_DESTROY_ALL_SURFACES_ASYNC, 0);
	qxl_wait_for_io_command (qxl);
    }
    else
    {
	ioport_write (qxl, QXL_IO_DESTROY_ALL_SURFACES, 0);
    }
#else
    ErrorF ("Xspice: error: UNIMPLEMENTED qxl_io_destroy_all_surfaces\n");
#endif
    qxl->device_primary = QXL_DEVICE_PRIMARY_NONE;
}