summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/qxl/qxl_display.c
blob: 5374e857be0d799303028313c58eeffff6dfd880 (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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#include "qxl_drv.h"
#include "drm_crtc_helper.h"

static void qxl_crtc_set_to_mode(struct qxl_device *qdev,
				 struct drm_connector *connector,
				 struct qxl_head *head)
{
	struct drm_device *dev = connector->dev;
	struct drm_display_mode *mode, *t;
	int width = head->width;
	int height = head->height;

	DRM_ERROR("TODO: check against framebuffer size\n");
	if (width < 320 || height < 240) {
		qxl_io_log(qdev, "%s: bad head: %dx%d", width, height);
		width = 1024;
		height = 768;
	}
	if (width * height * 4 > 16*1024*1024) {
		width = 1024;
		height = 768;
	}
	// TODO: go over regular modes and removed preferred?
	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
		drm_mode_remove(connector, mode);
	mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
	mode->type |= DRM_MODE_TYPE_PREFERRED;
	mode->status = MODE_OK;
	drm_mode_probed_add(connector, mode);
	qxl_io_log(qdev, "%s: %d x %d\n", __func__, width, height);
}

void qxl_crtc_set_from_client_monitors_config(struct qxl_device *qdev)
{
	struct drm_connector *connector;
	int i;
	struct drm_device *dev = qdev->ddev;

	i = 0;
	qxl_io_log(qdev, "%s: %d, %d\n", __func__,
		   dev->mode_config.num_connector,
		   qdev->monitors_config->count);
	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
		if (i > qdev->client_monitors_config->count) {
			/* crtc will be reported as disabled */
			continue;
		}
		qxl_crtc_set_to_mode(qdev, connector,
				     &qdev->client_monitors_config->heads[i]);
		++i;
	}
}

void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
{
	if (qdev->client_monitors_config &&
	    count > qdev->client_monitors_config->count) {
		kfree(qdev->client_monitors_config);
	}
	if (!qdev->client_monitors_config) {
		qdev->client_monitors_config = kzalloc(
				sizeof(struct qxl_monitors_config) +
				sizeof(struct qxl_head) * count, GFP_KERNEL);
		if (!qdev->client_monitors_config) {
			qxl_io_log(qdev,
				   "%s: allocation failure for %u heads\n",
				   __func__, count);
			return;
		}
	}
	qdev->client_monitors_config->count = count;
}

static void qxl_display_copy_client_monitors_config(struct qxl_device *qdev)
{
	int i;

	BUG_ON(!qdev->monitors_config);
	qxl_alloc_client_monitors_config(qdev, qdev->monitors_config->count);
	/* we copy max from the client but it isn't used */
	qdev->client_monitors_config->max_allowed = qdev->monitors_config->max_allowed;
	for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
		qdev->client_monitors_config->heads[i] = qdev->monitors_config->heads[i];
	}
}

void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
{
	int num_monitors;

	num_monitors = inl(qdev->io_base + QXL_IO_CLIENT_MONITORS_CONFIG_NUM);
	if (num_monitors > qdev->monitors_config->max_allowed) {
		DRM_INFO("client monitors list will be truncated: %d < %d\n",
			 qdev->monitors_config->max_allowed, num_monitors);
	}
	qxl_io_log(qdev, "client_monitors_config_num = %d\n", num_monitors);
	outb(0, qdev->io_base + QXL_IO_CLIENT_MONITORS_CONFIG_GET);
	qxl_display_copy_client_monitors_config(qdev);
	qxl_crtc_set_from_client_monitors_config(qdev);

        /* fire off a uevent and let userspace tell us what to do */
	qxl_io_log(qdev, "calling drm_sysfs_hotplug_event\n");
	drm_sysfs_hotplug_event(qdev->ddev);
	qxl_io_log(qdev, "calling QXL_IO_CLIENT_MONITORS_CONFIG_DONE\n");
	outb(0, qdev->io_base + QXL_IO_CLIENT_MONITORS_CONFIG_DONE);
}

static int qxl_add_monitors_config_modes(struct drm_connector *connector)
{
	struct drm_device *dev = connector->dev;
	struct qxl_device *qdev = dev->dev_private;
	struct qxl_output *output = drm_connector_to_qxl_output(connector);
	int h = output->index;
	struct drm_display_mode *mode = NULL;
	struct qxl_head *head;

	if (!qdev->monitors_config) {
		return 0;
	}
	head = &qdev->monitors_config->heads[h];

	mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false, false);
	mode->type |= DRM_MODE_TYPE_PREFERRED;
	drm_mode_probed_add(connector, mode);
	return 1;
}

static int qxl_add_common_modes(struct drm_connector *connector)
{
	struct drm_device *dev = connector->dev;
	struct drm_display_mode *mode = NULL;
	int i;
	struct mode_size {
		int w;
		int h;
	} common_modes[] = {
		{ 640,  480},
		{ 720,  480},
		{ 800,  600},
		{ 848,  480},
		{1024,  768},
		{1152,  768},
		{1280,  720},
		{1280,  800},
		{1280,  854},
		{1280,  960},
		{1280, 1024},
		{1440,  900},
		{1400, 1050},
		{1680, 1050},
		{1600, 1200},
		{1920, 1080},
		{1920, 1200}
	};

	for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
		if (common_modes[i].w < 320 || common_modes[i].h < 200)
			continue;

		mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h, 60, false, false, false);
		if (common_modes[i].w == 1024 && common_modes[i].h == 768) {
			mode->type |= DRM_MODE_TYPE_PREFERRED;
		}
		drm_mode_probed_add(connector, mode);
	}
	return i - 1;
}

static void qxl_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
			       u16 *blue, uint32_t start, uint32_t size)
{
}

static void qxl_crtc_destroy(struct drm_crtc *crtc)
{
	struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);

	drm_crtc_cleanup(crtc);
	kfree(qxl_crtc);
}

static const struct drm_crtc_funcs qxl_crtc_funcs = {
//	.cursor_set = qxl_crtc_cursor_set,
//	.cursor_move = qxl_crtc_cursor_move,
	.gamma_set = qxl_crtc_gamma_set,
	.set_config = drm_crtc_helper_set_config,
	.destroy = qxl_crtc_destroy,
};

static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
{
	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);

	if (qxl_fb->obj) {
		drm_gem_object_unreference_unlocked(qxl_fb->obj);
	}
	drm_framebuffer_cleanup(fb);
	kfree(qxl_fb);
}

int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
				  struct drm_file *file_priv,
				  unsigned flags, unsigned color,
				  struct drm_clip_rect *clips,
				  unsigned num_clips)
{
	/* TODO: vmwgfx where this was cribbed from had locking. Why? */
	struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
	struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
	struct drm_clip_rect norect;
	struct qxl_bo *qobj;
	int inc = 1;

	qobj = gem_to_qxl_bo(qxl_fb->obj);
	if (qxl_fb != qdev->active_user_framebuffer) {
		DRM_INFO("%s: qxl_fb 0x%p !=  qdev->active_user_framebuffer 0x%p\n",
			__func__, qxl_fb, qdev->active_user_framebuffer);
	}
	if (!num_clips) {
		num_clips = 1;
		clips = &norect;
		norect.x1 = norect.y1 = 0;
		norect.x2 = fb->width;
		norect.y2 = fb->height;
	} else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
		num_clips /= 2;
		inc = 2; /* skip source rects */
	}

	qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
			  clips, num_clips, inc);
	return 0;
}

static const struct drm_framebuffer_funcs qxl_fb_funcs = {
	.destroy = qxl_user_framebuffer_destroy,
	.dirty = qxl_framebuffer_surface_dirty,
//	.create_handle = qxl_user_framebuffer_create_handle,
};

void
qxl_framebuffer_init(struct drm_device *dev,
                       struct qxl_framebuffer *qfb,
                       struct drm_mode_fb_cmd2 *mode_cmd,
                       struct drm_gem_object *obj)
{
       qfb->obj = obj;
       drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs);
       drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
}

static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
{
}

static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
				  const struct drm_display_mode *mode,
				  struct drm_display_mode *adjusted_mode)
{
	struct drm_device *dev = crtc->dev;
	struct qxl_device *qdev = dev->dev_private;

	qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
		   __func__,
		   mode->hdisplay, mode->vdisplay,
		   adjusted_mode->hdisplay,
		   adjusted_mode->vdisplay);
	return true;
}

void
qxl_send_monitors_config(struct qxl_device *qdev)
{
	int i;

	BUG_ON(!qdev->ram_header->monitors_config);

	if (qdev->monitors_config->count == 0) {
		qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
		return;
	}
	for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
		struct qxl_head *head = &qdev->monitors_config->heads[i];

		if (head->y > 8192 || head->y < head->x ||
		    head->width > 8192 || head->height > 8192) {
			DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
				  i, head->width, head->height,
				  head->x, head->y);
			return;
		}
	}
	qxl_io_monitors_config(qdev);
}

static void qxl_monitors_config_set_single(struct qxl_device *qdev,
					   unsigned x, unsigned y,
					   unsigned width, unsigned height)
{
	DRM_DEBUG("%dx%d+%d+%d\n", width, height, x, y);
	qdev->monitors_config->count = 1;
	qdev->monitors_config->heads[0].x = x;
	qdev->monitors_config->heads[0].y = y;
	qdev->monitors_config->heads[0].width = width;
	qdev->monitors_config->heads[0].height = height;
}

static int qxl_crtc_mode_set(struct drm_crtc *crtc,
			       struct drm_display_mode *mode,
			       struct drm_display_mode *adjusted_mode,
			       int x, int y,
			       struct drm_framebuffer *old_fb)
{
	struct drm_device *dev = crtc->dev;
	struct qxl_device *qdev = dev->dev_private;
	struct qxl_mode *m = (void *)mode->private;

	if (!m) {
		// and do we care?
		DRM_DEBUG("%dx%d: not a native mode\n", x, y);
	} else {
		DRM_DEBUG("%dx%d: qxl id %d\n",
			  mode->hdisplay, mode->vdisplay, m->id);
	}
	DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
		  x, y,
		  mode->hdisplay, mode->vdisplay,
		  adjusted_mode->hdisplay,
		  adjusted_mode->vdisplay);

	if (!qdev->primary_created) {
		qxl_io_create_primary(qdev, mode->hdisplay + x, mode->vdisplay + y);
	} else {
		int width = x + mode->hdisplay;
		int height = y + mode->vdisplay;
		if (width > qdev->primary_width || height ) {
			if (width < qdev->primary_width) {
				width = qdev->primary_width;
			}
			if (height < qdev->primary_height) {
				height = qdev->primary_height;
			}
			qxl_io_destroy_primary(qdev);
			qxl_io_create_primary(qdev, width, height);
		}
	}
	if (qdev->monitors_config->count == 0) {
		qxl_monitors_config_set_single(qdev, x, y,
					       mode->hdisplay,
					       mode->vdisplay);
	}
	qdev->mode_set = true;
	return 0;
}

static int
qxl_crtc_set_base(struct drm_crtc *crtc, int x, int y,
		  struct drm_framebuffer *old_fb)
{
	DRM_DEBUG("\n");
	return 0;
}

static void qxl_crtc_prepare (struct drm_crtc *crtc)
{
	DRM_DEBUG("current: %dx%d+%d+%d (%d). \n",
		  crtc->mode.hdisplay, crtc->mode.vdisplay,
		  crtc->x, crtc->y, crtc->enabled);
}

static void qxl_crtc_commit (struct drm_crtc *crtc)
{
	DRM_DEBUG("\n");
}

void qxl_crtc_load_lut(struct drm_crtc *crtc)
{
	DRM_DEBUG("\n");
}

static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
	.dpms = qxl_crtc_dpms,
	.mode_fixup = qxl_crtc_mode_fixup,
	.mode_set = qxl_crtc_mode_set,
	.mode_set_base = qxl_crtc_set_base,
	.prepare = qxl_crtc_prepare,
	.commit = qxl_crtc_commit,
	.load_lut = qxl_crtc_load_lut,
};

int qdev_crtc_init(struct drm_device *dev, int num_crtc)
{
	struct qxl_crtc *qxl_crtc;

	qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
	if (!qxl_crtc)
		return -ENOMEM;

	drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);

	drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
	drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
	return 0;
}

static void qxl_enc_dpms(struct drm_encoder* encoder, int mode)
{
	DRM_DEBUG("\n");
}

static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
			       const struct drm_display_mode *mode,
			       struct drm_display_mode *adjusted_mode)
{
	DRM_DEBUG("\n");
	return true;
}

static void qxl_enc_prepare(struct drm_encoder *encoder)
{
	DRM_DEBUG("\n");
}

static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
		struct drm_encoder *encoder)
{
	int i;
	struct qxl_head *head;
	struct drm_display_mode *mode;

	BUG_ON(!encoder);
	// TODO: ugly, do better
	for (i = 0 ; (encoder->possible_crtcs != (1 << i)) && i < 32; ++i);
	if (encoder->possible_crtcs != (1 << i)) {
		DRM_ERROR("encoder has wrong possible_crtcs: %x\n",
			  encoder->possible_crtcs);
		return;
	}
	if (!qdev->monitors_config ||
	    qdev->monitors_config->max_allowed <= i) {
		DRM_ERROR(
		"head number too large or missing monitors config: %p, %d",
		qdev->monitors_config,
		qdev->monitors_config ?
			qdev->monitors_config->max_allowed : -1);
		return;
	}
	if (!encoder->crtc) {
		DRM_ERROR("missing crtc on encoder %p\n", encoder);
		return;
	}
	if (i != 0) {
		DRM_DEBUG("missing for multiple monitors: no head holes\n");
	}
	head = &qdev->monitors_config->heads[i];
	head->id = i;
	head->surface_id = 0;
	if (encoder->crtc->enabled) {
		mode = &encoder->crtc->mode;
		head->width = mode->hdisplay;
		head->height = mode->vdisplay;
		head->x = encoder->crtc->x;
		head->y = encoder->crtc->y;
		if (qdev->monitors_config->count < i + 1)
			qdev->monitors_config->count = i + 1;
	} else {
		head->width = 0;
		head->height = 0;
		head->x = 0;
		head->y = 0;
	}
	DRM_DEBUG("setting head %d to +%d+%d %dx%d\n",
		  i, head->x, head->y, head->width, head->height);
	head->flags = 0;
	// TODO - somewhere else to call this for multiple monitors (config_commit?)
	qxl_send_monitors_config(qdev);
}

static void qxl_enc_commit(struct drm_encoder *encoder)
{
	struct qxl_device *qdev = encoder->dev->dev_private;

	qxl_write_monitors_config_for_encoder(qdev, encoder);
	DRM_DEBUG("\n");
}

static void qxl_enc_mode_set(struct drm_encoder *encoder,
				struct drm_display_mode *mode,
				struct drm_display_mode *adjusted_mode)
{
	DRM_DEBUG("\n");
}

#if 0
static struct qxl_mode *
qxl_find_native_mode(struct qxl_device *qdev, struct drm_display_mode *mode,
		     int bpp)
{
	int i;

	for (i = 0; i < qdev->mode_info.num_modes; i++) {
		struct qxl_mode *m = qdev->mode_info.modes + i;
		if (m->x_res == mode->hdisplay &&
		    m->y_res == mode->vdisplay &&
		    m->bits == bpp)
			return m;
	}
	return NULL;
}
#endif

static int qxl_conn_get_modes(struct drm_connector *connector)
{
	int ret;
	struct qxl_device *qdev = connector->dev->dev_private;

	DRM_INFO("monitors_config=%p\n", qdev->monitors_config);
	// TODO: what should we do here? only show the configured modes for the device,
	// or allow the full list, or both?
	if (qdev->monitors_config && qdev->monitors_config->count) {
		ret = qxl_add_monitors_config_modes(connector);
	} else {
		ret = qxl_add_common_modes(connector);
	}
	return ret;
}

static int qxl_conn_mode_valid(struct drm_connector *connector,
			       struct drm_display_mode *mode)
{
	/* TODO: is this called for user defined modes? (xrandr --add-mode)
	 * TODO: check that the mode fits in the framebuffer */
	DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay, mode->vdisplay, mode->status);
	return MODE_OK;
}

struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
{
	struct qxl_output *qxl_output =
		drm_connector_to_qxl_output(connector);

	DRM_DEBUG("\n");
	return &qxl_output->enc;
}


static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
	.dpms = qxl_enc_dpms,
	.mode_fixup = qxl_enc_mode_fixup,
	.prepare = qxl_enc_prepare,
	.mode_set = qxl_enc_mode_set,
	.commit = qxl_enc_commit,	
};

static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
	.get_modes = qxl_conn_get_modes,
	.mode_valid = qxl_conn_mode_valid,
	.best_encoder = qxl_best_encoder,
};

static void qxl_conn_save(struct drm_connector *connector)
{
	DRM_DEBUG("\n");
}

static void qxl_conn_restore(struct drm_connector *connector)
{
	DRM_DEBUG("\n");
}

static enum drm_connector_status qxl_conn_detect(struct drm_connector *connector,
						 bool force)
{
	struct qxl_output *output =
		drm_connector_to_qxl_output(connector);
	struct drm_device *ddev = connector->dev;
	struct qxl_device *qdev = ddev->dev_private;
	int connected;

	// first monitor is always connected
	connected = (output->index == 0) ||
		    (qdev->monitors_config &&
		     qdev->monitors_config->count > output->index);

	DRM_DEBUG("\n");
	return connected ? connector_status_connected : connector_status_disconnected;
}

static int qxl_conn_set_property(struct drm_connector *connector,
				   struct drm_property *property,
				   uint64_t value)
{
	DRM_DEBUG("\n");
	return 0;
}

static void qxl_conn_destroy(struct drm_connector *connector)
{
	struct qxl_output *qxl_output =
		drm_connector_to_qxl_output(connector);

	drm_sysfs_connector_remove(connector);
	drm_connector_cleanup(connector);
	kfree(qxl_output);
}

static const struct drm_connector_funcs qxl_connector_funcs = {
	.dpms = drm_helper_connector_dpms,
	.save = qxl_conn_save,
	.restore = qxl_conn_restore,
	.detect = qxl_conn_detect,
	.fill_modes = drm_helper_probe_single_connector_modes,
	.set_property = qxl_conn_set_property,
	.destroy = qxl_conn_destroy,
};

static void qxl_enc_destroy(struct drm_encoder *encoder)
{
	drm_encoder_cleanup(encoder);
}

static const struct drm_encoder_funcs qxl_enc_funcs = {
	.destroy = qxl_enc_destroy,
};

int qdev_output_init(struct drm_device *dev, int num_output)
{
	struct qxl_output *qxl_output;
	struct drm_connector *connector;
	struct drm_encoder *encoder;

	qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
	if (!qxl_output)
		return -ENOMEM;

	qxl_output->index = num_output;

	connector = &qxl_output->base;
	encoder = &qxl_output->enc;
	drm_connector_init(dev, &qxl_output->base,
			   &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);

	drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
			 DRM_MODE_ENCODER_VIRTUAL);

	encoder->possible_crtcs = 1 << num_output;
	drm_mode_connector_attach_encoder(&qxl_output->base,
					  &qxl_output->enc);
	drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
	drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
       
	drm_sysfs_connector_add(connector);
	return 0;
}

static struct drm_framebuffer *
qxl_user_framebuffer_create(struct drm_device *dev,
			    struct drm_file *file_priv,
			    struct drm_mode_fb_cmd2 *mode_cmd)
{
	struct drm_gem_object *obj;
	struct qxl_framebuffer *qxl_fb;
	struct qxl_device *qdev = dev->dev_private;

	obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);

	DRM_INFO("%s: ENTRY\n", __func__);

	qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
	if (qxl_fb == NULL) {
		return NULL;
	}

	qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
	if (qdev->active_user_framebuffer) {
		DRM_INFO("%s: active_user_framebuffer %p -> %p\n",
			 __func__,
			 qdev->active_user_framebuffer, qxl_fb);
	}
	qdev->active_user_framebuffer = qxl_fb;

	return &qxl_fb->base;
}

static const struct drm_mode_config_funcs qxl_mode_funcs = {
	.fb_create = qxl_user_framebuffer_create,
};

int qxl_modeset_init(struct qxl_device *qdev)
{
	int i;
	int ret;
	struct drm_gem_object *gobj;
	int max_allowed = QXL_NUM_OUTPUTS;
	int monitors_config_size = sizeof(struct qxl_monitors_config) +
				   max_allowed * sizeof(struct qxl_head);

	drm_mode_config_init(qdev->ddev);
	ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
				    QXL_GEM_DOMAIN_VRAM,
				    false, false, &gobj);
	if (ret) {
		DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
		return -ENOMEM;
	}
	qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
	qxl_bo_kmap(qdev->monitors_config_bo, NULL);
	qdev->monitors_config = qdev->monitors_config_bo->kptr;
	qdev->ram_header->monitors_config =
		qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0,
					qdev->main_mem_slot);
	memset(qdev->monitors_config, 0, monitors_config_size);
	qdev->monitors_config->max_allowed = max_allowed;

	qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;

	/* modes will be validated against the framebuffer size */
	qdev->ddev->mode_config.max_width = 8192;
	qdev->ddev->mode_config.max_height = 8192;

	qdev->ddev->mode_config.fb_base = qdev->vram_base;
	for (i = 0 ; i < QXL_NUM_OUTPUTS; ++i) {
		qdev_crtc_init(qdev->ddev, i);
		qdev_output_init(qdev->ddev, i);
	}

	qdev->mode_info.mode_config_initialized = true;

	/* primary surface must be created by this point, to allow
	 * issuing command queue commands and having them read by
	 * spice server. */
	qxl_fbdev_init(qdev);
	return 0;
}

void qxl_modeset_fini(struct qxl_device *qdev)
{
	qxl_fbdev_fini(qdev);
	if (qdev->mode_info.mode_config_initialized) {
		drm_mode_config_cleanup(qdev->ddev);
		qdev->mode_info.mode_config_initialized = false;
	}
	
}