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#include <linux/crc32.h>
27#include <drm/drm_atomic.h>
28#include <drm/drm_atomic_helper.h>
29#include <drm/drm_gem_framebuffer_helper.h>
30#include <drm/drm_plane_helper.h>
31#include <drm/drm_probe_helper.h>
32
33#include "qxl_drv.h"
34#include "qxl_object.h"
35
36static bool qxl_head_enabled(struct qxl_head *head)
37{
38 return head->width && head->height;
39}
40
41static int qxl_alloc_client_monitors_config(struct qxl_device *qdev,
42 unsigned int count)
43{
44 if (qdev->client_monitors_config &&
45 count > qdev->client_monitors_config->count) {
46 kfree(qdev->client_monitors_config);
47 qdev->client_monitors_config = NULL;
48 }
49 if (!qdev->client_monitors_config) {
50 qdev->client_monitors_config = kzalloc(
51 struct_size(qdev->client_monitors_config,
52 heads, count), GFP_KERNEL);
53 if (!qdev->client_monitors_config)
54 return -ENOMEM;
55 }
56 qdev->client_monitors_config->count = count;
57 return 0;
58}
59
60enum {
61 MONITORS_CONFIG_MODIFIED,
62 MONITORS_CONFIG_UNCHANGED,
63 MONITORS_CONFIG_BAD_CRC,
64 MONITORS_CONFIG_ERROR,
65};
66
67static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
68{
69 int i;
70 int num_monitors;
71 uint32_t crc;
72 int status = MONITORS_CONFIG_UNCHANGED;
73
74 num_monitors = qdev->rom->client_monitors_config.count;
75 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
76 sizeof(qdev->rom->client_monitors_config));
77 if (crc != qdev->rom->client_monitors_config_crc)
78 return MONITORS_CONFIG_BAD_CRC;
79 if (!num_monitors) {
80 DRM_DEBUG_KMS("no client monitors configured\n");
81 return status;
82 }
83 if (num_monitors > qxl_num_crtc) {
84 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
85 qxl_num_crtc, num_monitors);
86 num_monitors = qxl_num_crtc;
87 } else {
88 num_monitors = qdev->rom->client_monitors_config.count;
89 }
90 if (qdev->client_monitors_config
91 && (num_monitors != qdev->client_monitors_config->count)) {
92 status = MONITORS_CONFIG_MODIFIED;
93 }
94 if (qxl_alloc_client_monitors_config(qdev, num_monitors)) {
95 status = MONITORS_CONFIG_ERROR;
96 return status;
97 }
98
99 qdev->client_monitors_config->max_allowed = qxl_num_crtc;
100 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
101 struct qxl_urect *c_rect =
102 &qdev->rom->client_monitors_config.heads[i];
103 struct qxl_head *client_head =
104 &qdev->client_monitors_config->heads[i];
105 if (client_head->x != c_rect->left) {
106 client_head->x = c_rect->left;
107 status = MONITORS_CONFIG_MODIFIED;
108 }
109 if (client_head->y != c_rect->top) {
110 client_head->y = c_rect->top;
111 status = MONITORS_CONFIG_MODIFIED;
112 }
113 if (client_head->width != c_rect->right - c_rect->left) {
114 client_head->width = c_rect->right - c_rect->left;
115 status = MONITORS_CONFIG_MODIFIED;
116 }
117 if (client_head->height != c_rect->bottom - c_rect->top) {
118 client_head->height = c_rect->bottom - c_rect->top;
119 status = MONITORS_CONFIG_MODIFIED;
120 }
121 if (client_head->surface_id != 0) {
122 client_head->surface_id = 0;
123 status = MONITORS_CONFIG_MODIFIED;
124 }
125 if (client_head->id != i) {
126 client_head->id = i;
127 status = MONITORS_CONFIG_MODIFIED;
128 }
129 if (client_head->flags != 0) {
130 client_head->flags = 0;
131 status = MONITORS_CONFIG_MODIFIED;
132 }
133 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
134 client_head->x, client_head->y);
135 }
136
137 return status;
138}
139
140static void qxl_update_offset_props(struct qxl_device *qdev)
141{
142 struct drm_device *dev = &qdev->ddev;
143 struct drm_connector *connector;
144 struct qxl_output *output;
145 struct qxl_head *head;
146
147 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
148 output = drm_connector_to_qxl_output(connector);
149
150 head = &qdev->client_monitors_config->heads[output->index];
151
152 drm_object_property_set_value(&connector->base,
153 dev->mode_config.suggested_x_property, head->x);
154 drm_object_property_set_value(&connector->base,
155 dev->mode_config.suggested_y_property, head->y);
156 }
157}
158
159void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
160{
161 struct drm_device *dev = &qdev->ddev;
162 int status, retries;
163
164 for (retries = 0; retries < 10; retries++) {
165 status = qxl_display_copy_rom_client_monitors_config(qdev);
166 if (status != MONITORS_CONFIG_BAD_CRC)
167 break;
168 udelay(5);
169 }
170 if (status == MONITORS_CONFIG_ERROR) {
171 DRM_DEBUG_KMS("ignoring client monitors config: error");
172 return;
173 }
174 if (status == MONITORS_CONFIG_BAD_CRC) {
175 DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
176 return;
177 }
178 if (status == MONITORS_CONFIG_UNCHANGED) {
179 DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
180 return;
181 }
182
183 drm_modeset_lock_all(dev);
184 qxl_update_offset_props(qdev);
185 drm_modeset_unlock_all(dev);
186 if (!drm_helper_hpd_irq_event(dev)) {
187
188
189 drm_kms_helper_hotplug_event(dev);
190 }
191}
192
193static int qxl_check_mode(struct qxl_device *qdev,
194 unsigned int width,
195 unsigned int height)
196{
197 unsigned int stride;
198 unsigned int size;
199
200 if (check_mul_overflow(width, 4u, &stride))
201 return -EINVAL;
202 if (check_mul_overflow(stride, height, &size))
203 return -EINVAL;
204 if (size > qdev->vram_size)
205 return -ENOMEM;
206 return 0;
207}
208
209static int qxl_check_framebuffer(struct qxl_device *qdev,
210 struct qxl_bo *bo)
211{
212 return qxl_check_mode(qdev, bo->surf.width, bo->surf.height);
213}
214
215static int qxl_add_mode(struct drm_connector *connector,
216 unsigned int width,
217 unsigned int height,
218 bool preferred)
219{
220 struct drm_device *dev = connector->dev;
221 struct qxl_device *qdev = dev->dev_private;
222 struct drm_display_mode *mode = NULL;
223 int rc;
224
225 rc = qxl_check_mode(qdev, width, height);
226 if (rc != 0)
227 return 0;
228
229 mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
230 if (preferred)
231 mode->type |= DRM_MODE_TYPE_PREFERRED;
232 mode->hdisplay = width;
233 mode->vdisplay = height;
234 drm_mode_set_name(mode);
235 drm_mode_probed_add(connector, mode);
236 return 1;
237}
238
239static int qxl_add_monitors_config_modes(struct drm_connector *connector)
240{
241 struct drm_device *dev = connector->dev;
242 struct qxl_device *qdev = dev->dev_private;
243 struct qxl_output *output = drm_connector_to_qxl_output(connector);
244 int h = output->index;
245 struct qxl_head *head;
246
247 if (!qdev->monitors_config)
248 return 0;
249 if (h >= qxl_num_crtc)
250 return 0;
251 if (!qdev->client_monitors_config)
252 return 0;
253 if (h >= qdev->client_monitors_config->count)
254 return 0;
255
256 head = &qdev->client_monitors_config->heads[h];
257 DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
258
259 return qxl_add_mode(connector, head->width, head->height, true);
260}
261
262static struct mode_size {
263 int w;
264 int h;
265} extra_modes[] = {
266 { 720, 480},
267 {1152, 768},
268 {1280, 854},
269};
270
271static int qxl_add_extra_modes(struct drm_connector *connector)
272{
273 int i, ret = 0;
274
275 for (i = 0; i < ARRAY_SIZE(extra_modes); i++)
276 ret += qxl_add_mode(connector,
277 extra_modes[i].w,
278 extra_modes[i].h,
279 false);
280 return ret;
281}
282
283static void qxl_send_monitors_config(struct qxl_device *qdev)
284{
285 int i;
286
287 BUG_ON(!qdev->ram_header->monitors_config);
288
289 if (qdev->monitors_config->count == 0)
290 return;
291
292 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
293 struct qxl_head *head = &qdev->monitors_config->heads[i];
294
295 if (head->y > 8192 || head->x > 8192 ||
296 head->width > 8192 || head->height > 8192) {
297 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
298 i, head->width, head->height,
299 head->x, head->y);
300 return;
301 }
302 }
303 qxl_io_monitors_config(qdev);
304}
305
306static void qxl_crtc_update_monitors_config(struct drm_crtc *crtc,
307 const char *reason)
308{
309 struct drm_device *dev = crtc->dev;
310 struct qxl_device *qdev = dev->dev_private;
311 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
312 struct qxl_head head;
313 int oldcount, i = qcrtc->index;
314
315 if (!qdev->primary_bo) {
316 DRM_DEBUG_KMS("no primary surface, skip (%s)\n", reason);
317 return;
318 }
319
320 if (!qdev->monitors_config || qxl_num_crtc <= i)
321 return;
322
323 head.id = i;
324 head.flags = 0;
325 oldcount = qdev->monitors_config->count;
326 if (crtc->state->active) {
327 struct drm_display_mode *mode = &crtc->mode;
328
329 head.width = mode->hdisplay;
330 head.height = mode->vdisplay;
331 head.x = crtc->x;
332 head.y = crtc->y;
333 if (qdev->monitors_config->count < i + 1)
334 qdev->monitors_config->count = i + 1;
335 if (qdev->primary_bo == qdev->dumb_shadow_bo)
336 head.x += qdev->dumb_heads[i].x;
337 } else if (i > 0) {
338 head.width = 0;
339 head.height = 0;
340 head.x = 0;
341 head.y = 0;
342 if (qdev->monitors_config->count == i + 1)
343 qdev->monitors_config->count = i;
344 } else {
345 DRM_DEBUG_KMS("inactive head 0, skip (%s)\n", reason);
346 return;
347 }
348
349 if (head.width == qdev->monitors_config->heads[i].width &&
350 head.height == qdev->monitors_config->heads[i].height &&
351 head.x == qdev->monitors_config->heads[i].x &&
352 head.y == qdev->monitors_config->heads[i].y &&
353 oldcount == qdev->monitors_config->count)
354 return;
355
356 DRM_DEBUG_KMS("head %d, %dx%d, at +%d+%d, %s (%s)\n",
357 i, head.width, head.height, head.x, head.y,
358 crtc->state->active ? "on" : "off", reason);
359 if (oldcount != qdev->monitors_config->count)
360 DRM_DEBUG_KMS("active heads %d -> %d (%d total)\n",
361 oldcount, qdev->monitors_config->count,
362 qxl_num_crtc);
363
364 qdev->monitors_config->heads[i] = head;
365 qdev->monitors_config->max_allowed = qxl_num_crtc;
366 qxl_send_monitors_config(qdev);
367}
368
369static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
370 struct drm_crtc_state *old_crtc_state)
371{
372 struct drm_device *dev = crtc->dev;
373 struct drm_pending_vblank_event *event;
374 unsigned long flags;
375
376 if (crtc->state && crtc->state->event) {
377 event = crtc->state->event;
378 crtc->state->event = NULL;
379
380 spin_lock_irqsave(&dev->event_lock, flags);
381 drm_crtc_send_vblank_event(crtc, event);
382 spin_unlock_irqrestore(&dev->event_lock, flags);
383 }
384
385 qxl_crtc_update_monitors_config(crtc, "flush");
386}
387
388static void qxl_crtc_destroy(struct drm_crtc *crtc)
389{
390 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
391
392 qxl_bo_unref(&qxl_crtc->cursor_bo);
393 drm_crtc_cleanup(crtc);
394 kfree(qxl_crtc);
395}
396
397static const struct drm_crtc_funcs qxl_crtc_funcs = {
398 .set_config = drm_atomic_helper_set_config,
399 .destroy = qxl_crtc_destroy,
400 .page_flip = drm_atomic_helper_page_flip,
401 .reset = drm_atomic_helper_crtc_reset,
402 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
403 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
404};
405
406static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
407 struct drm_file *file_priv,
408 unsigned int flags, unsigned int color,
409 struct drm_clip_rect *clips,
410 unsigned int num_clips)
411{
412
413 struct qxl_device *qdev = fb->dev->dev_private;
414 struct drm_clip_rect norect;
415 struct qxl_bo *qobj;
416 bool is_primary;
417 int inc = 1;
418
419 drm_modeset_lock_all(fb->dev);
420
421 qobj = gem_to_qxl_bo(fb->obj[0]);
422
423 is_primary = qobj->shadow ? qobj->shadow->is_primary : qobj->is_primary;
424 if (!is_primary) {
425 drm_modeset_unlock_all(fb->dev);
426 return 0;
427 }
428
429 if (!num_clips) {
430 num_clips = 1;
431 clips = &norect;
432 norect.x1 = norect.y1 = 0;
433 norect.x2 = fb->width;
434 norect.y2 = fb->height;
435 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
436 num_clips /= 2;
437 inc = 2;
438 }
439
440 qxl_draw_dirty_fb(qdev, fb, qobj, flags, color,
441 clips, num_clips, inc, 0);
442
443 drm_modeset_unlock_all(fb->dev);
444
445 return 0;
446}
447
448static const struct drm_framebuffer_funcs qxl_fb_funcs = {
449 .destroy = drm_gem_fb_destroy,
450 .dirty = qxl_framebuffer_surface_dirty,
451 .create_handle = drm_gem_fb_create_handle,
452};
453
454static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
455 struct drm_crtc_state *old_state)
456{
457 qxl_crtc_update_monitors_config(crtc, "enable");
458}
459
460static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
461 struct drm_crtc_state *old_state)
462{
463 qxl_crtc_update_monitors_config(crtc, "disable");
464}
465
466static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
467 .atomic_flush = qxl_crtc_atomic_flush,
468 .atomic_enable = qxl_crtc_atomic_enable,
469 .atomic_disable = qxl_crtc_atomic_disable,
470};
471
472static int qxl_primary_atomic_check(struct drm_plane *plane,
473 struct drm_plane_state *state)
474{
475 struct qxl_device *qdev = plane->dev->dev_private;
476 struct qxl_bo *bo;
477
478 if (!state->crtc || !state->fb)
479 return 0;
480
481 bo = gem_to_qxl_bo(state->fb->obj[0]);
482
483 return qxl_check_framebuffer(qdev, bo);
484}
485
486static int qxl_primary_apply_cursor(struct drm_plane *plane)
487{
488 struct drm_device *dev = plane->dev;
489 struct qxl_device *qdev = dev->dev_private;
490 struct drm_framebuffer *fb = plane->state->fb;
491 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
492 struct qxl_cursor_cmd *cmd;
493 struct qxl_release *release;
494 int ret = 0;
495
496 if (!qcrtc->cursor_bo)
497 return 0;
498
499 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
500 QXL_RELEASE_CURSOR_CMD,
501 &release, NULL);
502 if (ret)
503 return ret;
504
505 ret = qxl_release_list_add(release, qcrtc->cursor_bo);
506 if (ret)
507 goto out_free_release;
508
509 ret = qxl_release_reserve_list(release, false);
510 if (ret)
511 goto out_free_release;
512
513 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
514 cmd->type = QXL_CURSOR_SET;
515 cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
516 cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
517
518 cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
519
520 cmd->u.set.visible = 1;
521 qxl_release_unmap(qdev, release, &cmd->release_info);
522
523 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
524 qxl_release_fence_buffer_objects(release);
525
526 return ret;
527
528out_free_release:
529 qxl_release_free(qdev, release);
530 return ret;
531}
532
533static void qxl_primary_atomic_update(struct drm_plane *plane,
534 struct drm_plane_state *old_state)
535{
536 struct qxl_device *qdev = plane->dev->dev_private;
537 struct qxl_bo *bo = gem_to_qxl_bo(plane->state->fb->obj[0]);
538 struct qxl_bo *primary;
539 struct drm_clip_rect norect = {
540 .x1 = 0,
541 .y1 = 0,
542 .x2 = plane->state->fb->width,
543 .y2 = plane->state->fb->height
544 };
545 uint32_t dumb_shadow_offset = 0;
546
547 primary = bo->shadow ? bo->shadow : bo;
548
549 if (!primary->is_primary) {
550 if (qdev->primary_bo)
551 qxl_io_destroy_primary(qdev);
552 qxl_io_create_primary(qdev, primary);
553 qxl_primary_apply_cursor(plane);
554 }
555
556 if (bo->is_dumb)
557 dumb_shadow_offset =
558 qdev->dumb_heads[plane->state->crtc->index].x;
559
560 qxl_draw_dirty_fb(qdev, plane->state->fb, bo, 0, 0, &norect, 1, 1,
561 dumb_shadow_offset);
562}
563
564static void qxl_primary_atomic_disable(struct drm_plane *plane,
565 struct drm_plane_state *old_state)
566{
567 struct qxl_device *qdev = plane->dev->dev_private;
568
569 if (old_state->fb) {
570 struct qxl_bo *bo = gem_to_qxl_bo(old_state->fb->obj[0]);
571
572 if (bo->is_primary) {
573 qxl_io_destroy_primary(qdev);
574 bo->is_primary = false;
575 }
576 }
577}
578
579static void qxl_cursor_atomic_update(struct drm_plane *plane,
580 struct drm_plane_state *old_state)
581{
582 struct drm_device *dev = plane->dev;
583 struct qxl_device *qdev = dev->dev_private;
584 struct drm_framebuffer *fb = plane->state->fb;
585 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
586 struct qxl_release *release;
587 struct qxl_cursor_cmd *cmd;
588 struct qxl_cursor *cursor;
589 struct drm_gem_object *obj;
590 struct qxl_bo *cursor_bo = NULL, *user_bo = NULL, *old_cursor_bo = NULL;
591 int ret;
592 void *user_ptr;
593 int size = 64*64*4;
594
595 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
596 QXL_RELEASE_CURSOR_CMD,
597 &release, NULL);
598 if (ret)
599 return;
600
601 if (fb != old_state->fb) {
602 obj = fb->obj[0];
603 user_bo = gem_to_qxl_bo(obj);
604
605
606 ret = qxl_bo_kmap(user_bo, &user_ptr);
607 if (ret)
608 goto out_free_release;
609
610 ret = qxl_alloc_bo_reserved(qdev, release,
611 sizeof(struct qxl_cursor) + size,
612 &cursor_bo);
613 if (ret)
614 goto out_kunmap;
615
616 ret = qxl_bo_pin(cursor_bo);
617 if (ret)
618 goto out_free_bo;
619
620 ret = qxl_release_reserve_list(release, true);
621 if (ret)
622 goto out_unpin;
623
624 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
625 if (ret)
626 goto out_backoff;
627
628 cursor->header.unique = 0;
629 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
630 cursor->header.width = 64;
631 cursor->header.height = 64;
632 cursor->header.hot_spot_x = fb->hot_x;
633 cursor->header.hot_spot_y = fb->hot_y;
634 cursor->data_size = size;
635 cursor->chunk.next_chunk = 0;
636 cursor->chunk.prev_chunk = 0;
637 cursor->chunk.data_size = size;
638 memcpy(cursor->chunk.data, user_ptr, size);
639 qxl_bo_kunmap(cursor_bo);
640 qxl_bo_kunmap(user_bo);
641
642 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
643 cmd->u.set.visible = 1;
644 cmd->u.set.shape = qxl_bo_physical_address(qdev,
645 cursor_bo, 0);
646 cmd->type = QXL_CURSOR_SET;
647
648 old_cursor_bo = qcrtc->cursor_bo;
649 qcrtc->cursor_bo = cursor_bo;
650 cursor_bo = NULL;
651 } else {
652
653 ret = qxl_release_reserve_list(release, true);
654 if (ret)
655 goto out_free_release;
656
657 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
658 cmd->type = QXL_CURSOR_MOVE;
659 }
660
661 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
662 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
663
664 qxl_release_unmap(qdev, release, &cmd->release_info);
665 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
666 qxl_release_fence_buffer_objects(release);
667
668 if (old_cursor_bo != NULL)
669 qxl_bo_unpin(old_cursor_bo);
670 qxl_bo_unref(&old_cursor_bo);
671 qxl_bo_unref(&cursor_bo);
672
673 return;
674
675out_backoff:
676 qxl_release_backoff_reserve_list(release);
677out_unpin:
678 qxl_bo_unpin(cursor_bo);
679out_free_bo:
680 qxl_bo_unref(&cursor_bo);
681out_kunmap:
682 qxl_bo_kunmap(user_bo);
683out_free_release:
684 qxl_release_free(qdev, release);
685 return;
686
687}
688
689static void qxl_cursor_atomic_disable(struct drm_plane *plane,
690 struct drm_plane_state *old_state)
691{
692 struct qxl_device *qdev = plane->dev->dev_private;
693 struct qxl_release *release;
694 struct qxl_cursor_cmd *cmd;
695 int ret;
696
697 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
698 QXL_RELEASE_CURSOR_CMD,
699 &release, NULL);
700 if (ret)
701 return;
702
703 ret = qxl_release_reserve_list(release, true);
704 if (ret) {
705 qxl_release_free(qdev, release);
706 return;
707 }
708
709 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
710 cmd->type = QXL_CURSOR_HIDE;
711 qxl_release_unmap(qdev, release, &cmd->release_info);
712
713 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
714 qxl_release_fence_buffer_objects(release);
715}
716
717static void qxl_update_dumb_head(struct qxl_device *qdev,
718 int index, struct qxl_bo *bo)
719{
720 uint32_t width, height;
721
722 if (index >= qdev->monitors_config->max_allowed)
723 return;
724
725 if (bo && bo->is_dumb) {
726 width = bo->surf.width;
727 height = bo->surf.height;
728 } else {
729 width = 0;
730 height = 0;
731 }
732
733 if (qdev->dumb_heads[index].width == width &&
734 qdev->dumb_heads[index].height == height)
735 return;
736
737 DRM_DEBUG("#%d: %dx%d -> %dx%d\n", index,
738 qdev->dumb_heads[index].width,
739 qdev->dumb_heads[index].height,
740 width, height);
741 qdev->dumb_heads[index].width = width;
742 qdev->dumb_heads[index].height = height;
743}
744
745static void qxl_calc_dumb_shadow(struct qxl_device *qdev,
746 struct qxl_surface *surf)
747{
748 struct qxl_head *head;
749 int i;
750
751 memset(surf, 0, sizeof(*surf));
752 for (i = 0; i < qdev->monitors_config->max_allowed; i++) {
753 head = qdev->dumb_heads + i;
754 head->x = surf->width;
755 surf->width += head->width;
756 if (surf->height < head->height)
757 surf->height = head->height;
758 }
759 if (surf->width < 64)
760 surf->width = 64;
761 if (surf->height < 64)
762 surf->height = 64;
763 surf->format = SPICE_SURFACE_FMT_32_xRGB;
764 surf->stride = surf->width * 4;
765
766 if (!qdev->dumb_shadow_bo ||
767 qdev->dumb_shadow_bo->surf.width != surf->width ||
768 qdev->dumb_shadow_bo->surf.height != surf->height)
769 DRM_DEBUG("%dx%d\n", surf->width, surf->height);
770}
771
772static int qxl_plane_prepare_fb(struct drm_plane *plane,
773 struct drm_plane_state *new_state)
774{
775 struct qxl_device *qdev = plane->dev->dev_private;
776 struct drm_gem_object *obj;
777 struct qxl_bo *user_bo;
778 struct qxl_surface surf;
779 int ret;
780
781 if (!new_state->fb)
782 return 0;
783
784 obj = new_state->fb->obj[0];
785 user_bo = gem_to_qxl_bo(obj);
786
787 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
788 user_bo->is_dumb) {
789 qxl_update_dumb_head(qdev, new_state->crtc->index,
790 user_bo);
791 qxl_calc_dumb_shadow(qdev, &surf);
792 if (!qdev->dumb_shadow_bo ||
793 qdev->dumb_shadow_bo->surf.width != surf.width ||
794 qdev->dumb_shadow_bo->surf.height != surf.height) {
795 if (qdev->dumb_shadow_bo) {
796 drm_gem_object_put_unlocked
797 (&qdev->dumb_shadow_bo->gem_base);
798 qdev->dumb_shadow_bo = NULL;
799 }
800 qxl_bo_create(qdev, surf.height * surf.stride,
801 true, true, QXL_GEM_DOMAIN_SURFACE, &surf,
802 &qdev->dumb_shadow_bo);
803 }
804 if (user_bo->shadow != qdev->dumb_shadow_bo) {
805 if (user_bo->shadow) {
806 drm_gem_object_put_unlocked
807 (&user_bo->shadow->gem_base);
808 user_bo->shadow = NULL;
809 }
810 drm_gem_object_get(&qdev->dumb_shadow_bo->gem_base);
811 user_bo->shadow = qdev->dumb_shadow_bo;
812 }
813 }
814
815 ret = qxl_bo_pin(user_bo);
816 if (ret)
817 return ret;
818
819 return 0;
820}
821
822static void qxl_plane_cleanup_fb(struct drm_plane *plane,
823 struct drm_plane_state *old_state)
824{
825 struct drm_gem_object *obj;
826 struct qxl_bo *user_bo;
827
828 if (!old_state->fb) {
829
830
831
832
833 return;
834 }
835
836 obj = old_state->fb->obj[0];
837 user_bo = gem_to_qxl_bo(obj);
838 qxl_bo_unpin(user_bo);
839
840 if (old_state->fb != plane->state->fb && user_bo->shadow) {
841 drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
842 user_bo->shadow = NULL;
843 }
844}
845
846static const uint32_t qxl_cursor_plane_formats[] = {
847 DRM_FORMAT_ARGB8888,
848};
849
850static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
851 .atomic_update = qxl_cursor_atomic_update,
852 .atomic_disable = qxl_cursor_atomic_disable,
853 .prepare_fb = qxl_plane_prepare_fb,
854 .cleanup_fb = qxl_plane_cleanup_fb,
855};
856
857static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
858 .update_plane = drm_atomic_helper_update_plane,
859 .disable_plane = drm_atomic_helper_disable_plane,
860 .destroy = drm_primary_helper_destroy,
861 .reset = drm_atomic_helper_plane_reset,
862 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
863 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
864};
865
866static const uint32_t qxl_primary_plane_formats[] = {
867 DRM_FORMAT_XRGB8888,
868 DRM_FORMAT_ARGB8888,
869};
870
871static const struct drm_plane_helper_funcs primary_helper_funcs = {
872 .atomic_check = qxl_primary_atomic_check,
873 .atomic_update = qxl_primary_atomic_update,
874 .atomic_disable = qxl_primary_atomic_disable,
875 .prepare_fb = qxl_plane_prepare_fb,
876 .cleanup_fb = qxl_plane_cleanup_fb,
877};
878
879static const struct drm_plane_funcs qxl_primary_plane_funcs = {
880 .update_plane = drm_atomic_helper_update_plane,
881 .disable_plane = drm_atomic_helper_disable_plane,
882 .destroy = drm_primary_helper_destroy,
883 .reset = drm_atomic_helper_plane_reset,
884 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
885 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
886};
887
888static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
889 unsigned int possible_crtcs,
890 enum drm_plane_type type)
891{
892 const struct drm_plane_helper_funcs *helper_funcs = NULL;
893 struct drm_plane *plane;
894 const struct drm_plane_funcs *funcs;
895 const uint32_t *formats;
896 int num_formats;
897 int err;
898
899 if (type == DRM_PLANE_TYPE_PRIMARY) {
900 funcs = &qxl_primary_plane_funcs;
901 formats = qxl_primary_plane_formats;
902 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
903 helper_funcs = &primary_helper_funcs;
904 } else if (type == DRM_PLANE_TYPE_CURSOR) {
905 funcs = &qxl_cursor_plane_funcs;
906 formats = qxl_cursor_plane_formats;
907 helper_funcs = &qxl_cursor_helper_funcs;
908 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
909 } else {
910 return ERR_PTR(-EINVAL);
911 }
912
913 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
914 if (!plane)
915 return ERR_PTR(-ENOMEM);
916
917 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
918 funcs, formats, num_formats,
919 NULL, type, NULL);
920 if (err)
921 goto free_plane;
922
923 drm_plane_helper_add(plane, helper_funcs);
924
925 return plane;
926
927free_plane:
928 kfree(plane);
929 return ERR_PTR(-EINVAL);
930}
931
932static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
933{
934 struct qxl_crtc *qxl_crtc;
935 struct drm_plane *primary, *cursor;
936 struct qxl_device *qdev = dev->dev_private;
937 int r;
938
939 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
940 if (!qxl_crtc)
941 return -ENOMEM;
942
943 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
944 if (IS_ERR(primary)) {
945 r = -ENOMEM;
946 goto free_mem;
947 }
948
949 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
950 if (IS_ERR(cursor)) {
951 r = -ENOMEM;
952 goto clean_primary;
953 }
954
955 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
956 &qxl_crtc_funcs, NULL);
957 if (r)
958 goto clean_cursor;
959
960 qxl_crtc->index = crtc_id;
961 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
962 return 0;
963
964clean_cursor:
965 drm_plane_cleanup(cursor);
966 kfree(cursor);
967clean_primary:
968 drm_plane_cleanup(primary);
969 kfree(primary);
970free_mem:
971 kfree(qxl_crtc);
972 return r;
973}
974
975static int qxl_conn_get_modes(struct drm_connector *connector)
976{
977 struct drm_device *dev = connector->dev;
978 struct qxl_device *qdev = dev->dev_private;
979 struct qxl_output *output = drm_connector_to_qxl_output(connector);
980 unsigned int pwidth = 1024;
981 unsigned int pheight = 768;
982 int ret = 0;
983
984 if (qdev->client_monitors_config) {
985 struct qxl_head *head;
986 head = &qdev->client_monitors_config->heads[output->index];
987 if (head->width)
988 pwidth = head->width;
989 if (head->height)
990 pheight = head->height;
991 }
992
993 ret += drm_add_modes_noedid(connector, 8192, 8192);
994 ret += qxl_add_extra_modes(connector);
995 ret += qxl_add_monitors_config_modes(connector);
996 drm_set_preferred_mode(connector, pwidth, pheight);
997 return ret;
998}
999
1000static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
1001 struct drm_display_mode *mode)
1002{
1003 struct drm_device *ddev = connector->dev;
1004 struct qxl_device *qdev = ddev->dev_private;
1005
1006 if (qxl_check_mode(qdev, mode->hdisplay, mode->vdisplay) != 0)
1007 return MODE_BAD;
1008
1009 return MODE_OK;
1010}
1011
1012static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
1013{
1014 struct qxl_output *qxl_output =
1015 drm_connector_to_qxl_output(connector);
1016
1017 DRM_DEBUG("\n");
1018 return &qxl_output->enc;
1019}
1020
1021static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1022};
1023
1024static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1025 .get_modes = qxl_conn_get_modes,
1026 .mode_valid = qxl_conn_mode_valid,
1027 .best_encoder = qxl_best_encoder,
1028};
1029
1030static enum drm_connector_status qxl_conn_detect(
1031 struct drm_connector *connector,
1032 bool force)
1033{
1034 struct qxl_output *output =
1035 drm_connector_to_qxl_output(connector);
1036 struct drm_device *ddev = connector->dev;
1037 struct qxl_device *qdev = ddev->dev_private;
1038 bool connected = false;
1039
1040
1041 if (!qdev->client_monitors_config) {
1042 if (output->index == 0)
1043 connected = true;
1044 } else
1045 connected = qdev->client_monitors_config->count > output->index &&
1046 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1047
1048 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1049
1050 return connected ? connector_status_connected
1051 : connector_status_disconnected;
1052}
1053
1054static void qxl_conn_destroy(struct drm_connector *connector)
1055{
1056 struct qxl_output *qxl_output =
1057 drm_connector_to_qxl_output(connector);
1058
1059 drm_connector_unregister(connector);
1060 drm_connector_cleanup(connector);
1061 kfree(qxl_output);
1062}
1063
1064static const struct drm_connector_funcs qxl_connector_funcs = {
1065 .detect = qxl_conn_detect,
1066 .fill_modes = drm_helper_probe_single_connector_modes,
1067 .destroy = qxl_conn_destroy,
1068 .reset = drm_atomic_helper_connector_reset,
1069 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1070 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1071};
1072
1073static void qxl_enc_destroy(struct drm_encoder *encoder)
1074{
1075 drm_encoder_cleanup(encoder);
1076}
1077
1078static const struct drm_encoder_funcs qxl_enc_funcs = {
1079 .destroy = qxl_enc_destroy,
1080};
1081
1082static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1083{
1084 if (qdev->hotplug_mode_update_property)
1085 return 0;
1086
1087 qdev->hotplug_mode_update_property =
1088 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1089 "hotplug_mode_update", 0, 1);
1090
1091 return 0;
1092}
1093
1094static int qdev_output_init(struct drm_device *dev, int num_output)
1095{
1096 struct qxl_device *qdev = dev->dev_private;
1097 struct qxl_output *qxl_output;
1098 struct drm_connector *connector;
1099 struct drm_encoder *encoder;
1100
1101 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1102 if (!qxl_output)
1103 return -ENOMEM;
1104
1105 qxl_output->index = num_output;
1106
1107 connector = &qxl_output->base;
1108 encoder = &qxl_output->enc;
1109 drm_connector_init(dev, &qxl_output->base,
1110 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1111
1112 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
1113 DRM_MODE_ENCODER_VIRTUAL, NULL);
1114
1115
1116 connector->polled = DRM_CONNECTOR_POLL_HPD;
1117 encoder->possible_crtcs = 1 << num_output;
1118 drm_connector_attach_encoder(&qxl_output->base,
1119 &qxl_output->enc);
1120 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1121 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1122
1123 drm_object_attach_property(&connector->base,
1124 qdev->hotplug_mode_update_property, 0);
1125 drm_object_attach_property(&connector->base,
1126 dev->mode_config.suggested_x_property, 0);
1127 drm_object_attach_property(&connector->base,
1128 dev->mode_config.suggested_y_property, 0);
1129 return 0;
1130}
1131
1132static struct drm_framebuffer *
1133qxl_user_framebuffer_create(struct drm_device *dev,
1134 struct drm_file *file_priv,
1135 const struct drm_mode_fb_cmd2 *mode_cmd)
1136{
1137 return drm_gem_fb_create_with_funcs(dev, file_priv, mode_cmd,
1138 &qxl_fb_funcs);
1139}
1140
1141static const struct drm_mode_config_funcs qxl_mode_funcs = {
1142 .fb_create = qxl_user_framebuffer_create,
1143 .atomic_check = drm_atomic_helper_check,
1144 .atomic_commit = drm_atomic_helper_commit,
1145};
1146
1147int qxl_create_monitors_object(struct qxl_device *qdev)
1148{
1149 int ret;
1150 struct drm_gem_object *gobj;
1151 int monitors_config_size = sizeof(struct qxl_monitors_config) +
1152 qxl_num_crtc * sizeof(struct qxl_head);
1153
1154 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1155 QXL_GEM_DOMAIN_VRAM,
1156 false, false, NULL, &gobj);
1157 if (ret) {
1158 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1159 return -ENOMEM;
1160 }
1161 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1162
1163 ret = qxl_bo_pin(qdev->monitors_config_bo);
1164 if (ret)
1165 return ret;
1166
1167 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
1168
1169 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1170 qdev->ram_header->monitors_config =
1171 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1172
1173 memset(qdev->monitors_config, 0, monitors_config_size);
1174 qdev->dumb_heads = kcalloc(qxl_num_crtc, sizeof(qdev->dumb_heads[0]),
1175 GFP_KERNEL);
1176 if (!qdev->dumb_heads) {
1177 qxl_destroy_monitors_object(qdev);
1178 return -ENOMEM;
1179 }
1180 return 0;
1181}
1182
1183int qxl_destroy_monitors_object(struct qxl_device *qdev)
1184{
1185 int ret;
1186
1187 qdev->monitors_config = NULL;
1188 qdev->ram_header->monitors_config = 0;
1189
1190 qxl_bo_kunmap(qdev->monitors_config_bo);
1191 ret = qxl_bo_unpin(qdev->monitors_config_bo);
1192 if (ret)
1193 return ret;
1194
1195 qxl_bo_unref(&qdev->monitors_config_bo);
1196 return 0;
1197}
1198
1199int qxl_modeset_init(struct qxl_device *qdev)
1200{
1201 int i;
1202 int ret;
1203
1204 drm_mode_config_init(&qdev->ddev);
1205
1206 ret = qxl_create_monitors_object(qdev);
1207 if (ret)
1208 return ret;
1209
1210 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1211
1212
1213 qdev->ddev.mode_config.min_width = 0;
1214 qdev->ddev.mode_config.min_height = 0;
1215 qdev->ddev.mode_config.max_width = 8192;
1216 qdev->ddev.mode_config.max_height = 8192;
1217
1218 qdev->ddev.mode_config.fb_base = qdev->vram_base;
1219
1220 drm_mode_create_suggested_offset_properties(&qdev->ddev);
1221 qxl_mode_create_hotplug_mode_update_property(qdev);
1222
1223 for (i = 0 ; i < qxl_num_crtc; ++i) {
1224 qdev_crtc_init(&qdev->ddev, i);
1225 qdev_output_init(&qdev->ddev, i);
1226 }
1227
1228 qxl_display_read_client_monitors_config(qdev);
1229
1230 drm_mode_config_reset(&qdev->ddev);
1231 return 0;
1232}
1233
1234void qxl_modeset_fini(struct qxl_device *qdev)
1235{
1236 qxl_destroy_monitors_object(qdev);
1237 drm_mode_config_cleanup(&qdev->ddev);
1238}
1239