blob: c80ddfedbbabac3763add7d4e1b1bcf29edc1f16 [file] [log] [blame]
Dave Airlief64122c2013-02-25 14:47:55 +10001/*
2 * Copyright 2013 Red Hat Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 * Authors: Dave Airlie
23 * Alon Levy
24 */
25
26
27#include "linux/crc32.h"
28
29#include "qxl_drv.h"
30#include "qxl_object.h"
31#include "drm_crtc_helper.h"
32
33static void qxl_crtc_set_to_mode(struct qxl_device *qdev,
34 struct drm_connector *connector,
35 struct qxl_head *head)
36{
37 struct drm_device *dev = connector->dev;
38 struct drm_display_mode *mode, *t;
39 int width = head->width;
40 int height = head->height;
41
42 if (width < 320 || height < 240) {
43 qxl_io_log(qdev, "%s: bad head: %dx%d", width, height);
44 width = 1024;
45 height = 768;
46 }
47 if (width * height * 4 > 16*1024*1024) {
48 width = 1024;
49 height = 768;
50 }
51 /* TODO: go over regular modes and removed preferred? */
52 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
53 drm_mode_remove(connector, mode);
54 mode = drm_cvt_mode(dev, width, height, 60, false, false, false);
55 mode->type |= DRM_MODE_TYPE_PREFERRED;
56 mode->status = MODE_OK;
57 drm_mode_probed_add(connector, mode);
58 qxl_io_log(qdev, "%s: %d x %d\n", __func__, width, height);
59}
60
61void qxl_crtc_set_from_monitors_config(struct qxl_device *qdev)
62{
63 struct drm_connector *connector;
64 int i;
65 struct drm_device *dev = qdev->ddev;
66
67 i = 0;
68 qxl_io_log(qdev, "%s: %d, %d\n", __func__,
69 dev->mode_config.num_connector,
70 qdev->monitors_config->count);
71 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
72 if (i > qdev->monitors_config->count) {
73 /* crtc will be reported as disabled */
74 continue;
75 }
76 qxl_crtc_set_to_mode(qdev, connector,
77 &qdev->monitors_config->heads[i]);
78 ++i;
79 }
80}
81
82void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
83{
84 if (qdev->client_monitors_config &&
85 count > qdev->client_monitors_config->count) {
86 kfree(qdev->client_monitors_config);
87 }
88 if (!qdev->client_monitors_config) {
89 qdev->client_monitors_config = kzalloc(
90 sizeof(struct qxl_monitors_config) +
91 sizeof(struct qxl_head) * count, GFP_KERNEL);
92 if (!qdev->client_monitors_config) {
93 qxl_io_log(qdev,
94 "%s: allocation failure for %u heads\n",
95 __func__, count);
96 return;
97 }
98 }
99 qdev->client_monitors_config->count = count;
100}
101
102static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
103{
104 int i;
105 int num_monitors;
106 uint32_t crc;
107
108 BUG_ON(!qdev->monitors_config);
109 num_monitors = qdev->rom->client_monitors_config.count;
110 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
111 sizeof(qdev->rom->client_monitors_config));
112 if (crc != qdev->rom->client_monitors_config_crc) {
113 qxl_io_log(qdev, "crc mismatch: have %X (%d) != %X\n", crc,
114 sizeof(qdev->rom->client_monitors_config),
115 qdev->rom->client_monitors_config_crc);
116 return 1;
117 }
118 if (num_monitors > qdev->monitors_config->max_allowed) {
119 DRM_INFO("client monitors list will be truncated: %d < %d\n",
120 qdev->monitors_config->max_allowed, num_monitors);
121 num_monitors = qdev->monitors_config->max_allowed;
122 } else {
123 num_monitors = qdev->rom->client_monitors_config.count;
124 }
125 qxl_alloc_client_monitors_config(qdev, num_monitors);
126 /* we copy max from the client but it isn't used */
127 qdev->client_monitors_config->max_allowed =
128 qdev->monitors_config->max_allowed;
129 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
130 struct qxl_urect *c_rect =
131 &qdev->rom->client_monitors_config.heads[i];
132 struct qxl_head *client_head =
133 &qdev->client_monitors_config->heads[i];
134 struct qxl_head *head = &qdev->monitors_config->heads[i];
135 client_head->x = head->x = c_rect->left;
136 client_head->y = head->y = c_rect->top;
137 client_head->width = head->width =
138 c_rect->right - c_rect->left;
139 client_head->height = head->height =
140 c_rect->bottom - c_rect->top;
141 client_head->surface_id = head->surface_id = 0;
142 client_head->id = head->id = i;
143 client_head->flags = head->flags = 0;
144 QXL_DEBUG(qdev, "read %dx%d+%d+%d\n", head->width, head->height,
145 head->x, head->y);
146 }
147 return 0;
148}
149
150void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
151{
152
153 while (qxl_display_copy_rom_client_monitors_config(qdev)) {
154 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
155 " retrying\n");
156 }
157 qxl_crtc_set_from_monitors_config(qdev);
158 /* fire off a uevent and let userspace tell us what to do */
159 qxl_io_log(qdev, "calling drm_sysfs_hotplug_event\n");
160 drm_sysfs_hotplug_event(qdev->ddev);
161}
162
163static int qxl_add_monitors_config_modes(struct drm_connector *connector)
164{
165 struct drm_device *dev = connector->dev;
166 struct qxl_device *qdev = dev->dev_private;
167 struct qxl_output *output = drm_connector_to_qxl_output(connector);
168 int h = output->index;
169 struct drm_display_mode *mode = NULL;
170 struct qxl_head *head;
171
172 if (!qdev->monitors_config)
173 return 0;
174 head = &qdev->monitors_config->heads[h];
175
176 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
177 false);
178 mode->type |= DRM_MODE_TYPE_PREFERRED;
179 drm_mode_probed_add(connector, mode);
180 return 1;
181}
182
183static int qxl_add_common_modes(struct drm_connector *connector)
184{
185 struct drm_device *dev = connector->dev;
186 struct drm_display_mode *mode = NULL;
187 int i;
188 struct mode_size {
189 int w;
190 int h;
191 } common_modes[] = {
192 { 640, 480},
193 { 720, 480},
194 { 800, 600},
195 { 848, 480},
196 {1024, 768},
197 {1152, 768},
198 {1280, 720},
199 {1280, 800},
200 {1280, 854},
201 {1280, 960},
202 {1280, 1024},
203 {1440, 900},
204 {1400, 1050},
205 {1680, 1050},
206 {1600, 1200},
207 {1920, 1080},
208 {1920, 1200}
209 };
210
211 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
212 if (common_modes[i].w < 320 || common_modes[i].h < 200)
213 continue;
214
215 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
216 60, false, false, false);
217 if (common_modes[i].w == 1024 && common_modes[i].h == 768)
218 mode->type |= DRM_MODE_TYPE_PREFERRED;
219 drm_mode_probed_add(connector, mode);
220 }
221 return i - 1;
222}
223
224static void qxl_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
225 u16 *blue, uint32_t start, uint32_t size)
226{
227 /* TODO */
228}
229
230static void qxl_crtc_destroy(struct drm_crtc *crtc)
231{
232 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
233
234 drm_crtc_cleanup(crtc);
235 kfree(qxl_crtc);
236}
237
238static void
239qxl_hide_cursor(struct qxl_device *qdev)
240{
241 struct qxl_release *release;
242 struct qxl_cursor_cmd *cmd;
243 int ret;
244
245 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
246 &release, NULL);
247
248 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
249 cmd->type = QXL_CURSOR_HIDE;
250 qxl_release_unmap(qdev, release, &cmd->release_info);
251
252 qxl_fence_releaseable(qdev, release);
253 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
254 qxl_release_unreserve(qdev, release);
255}
256
257static int qxl_crtc_cursor_set(struct drm_crtc *crtc,
258 struct drm_file *file_priv,
259 uint32_t handle,
260 uint32_t width,
261 uint32_t height)
262{
263 struct drm_device *dev = crtc->dev;
264 struct qxl_device *qdev = dev->dev_private;
265 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
266 struct drm_gem_object *obj;
267 struct qxl_cursor *cursor;
268 struct qxl_cursor_cmd *cmd;
269 struct qxl_bo *cursor_bo, *user_bo;
270 struct qxl_release *release;
271 void *user_ptr;
272
273 int size = 64*64*4;
274 int ret = 0;
275 if (!handle) {
276 qxl_hide_cursor(qdev);
277 return 0;
278 }
279
280 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
281 if (!obj) {
282 DRM_ERROR("cannot find cursor object\n");
283 return -ENOENT;
284 }
285
286 user_bo = gem_to_qxl_bo(obj);
287
288 ret = qxl_bo_reserve(user_bo, false);
289 if (ret)
290 goto out_unref;
291
292 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
293 if (ret)
294 goto out_unreserve;
295
296 ret = qxl_bo_kmap(user_bo, &user_ptr);
297 if (ret)
298 goto out_unpin;
299
300 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
301 QXL_RELEASE_CURSOR_CMD,
302 &release, NULL);
303 if (ret)
304 goto out_kunmap;
305 ret = qxl_alloc_bo_reserved(qdev, sizeof(struct qxl_cursor) + size,
306 &cursor_bo);
307 if (ret)
308 goto out_free_release;
309 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
310 if (ret)
311 goto out_free_bo;
312
313 cursor->header.unique = 0;
314 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
315 cursor->header.width = 64;
316 cursor->header.height = 64;
317 cursor->header.hot_spot_x = 0;
318 cursor->header.hot_spot_y = 0;
319 cursor->data_size = size;
320 cursor->chunk.next_chunk = 0;
321 cursor->chunk.prev_chunk = 0;
322 cursor->chunk.data_size = size;
323
324 memcpy(cursor->chunk.data, user_ptr, size);
325
326 qxl_bo_kunmap(cursor_bo);
327
328 /* finish with the userspace bo */
329 qxl_bo_kunmap(user_bo);
330 qxl_bo_unpin(user_bo);
331 qxl_bo_unreserve(user_bo);
332 drm_gem_object_unreference_unlocked(obj);
333
334 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
335 cmd->type = QXL_CURSOR_SET;
336 cmd->u.set.position.x = qcrtc->cur_x;
337 cmd->u.set.position.y = qcrtc->cur_y;
338
339 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
340 qxl_release_add_res(qdev, release, cursor_bo);
341
342 cmd->u.set.visible = 1;
343 qxl_release_unmap(qdev, release, &cmd->release_info);
344
345 qxl_fence_releaseable(qdev, release);
346 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
347 qxl_release_unreserve(qdev, release);
348
349 qxl_bo_unreserve(cursor_bo);
350 qxl_bo_unref(&cursor_bo);
351
352 return ret;
353out_free_bo:
354 qxl_bo_unref(&cursor_bo);
355out_free_release:
356 qxl_release_unreserve(qdev, release);
357 qxl_release_free(qdev, release);
358out_kunmap:
359 qxl_bo_kunmap(user_bo);
360out_unpin:
361 qxl_bo_unpin(user_bo);
362out_unreserve:
363 qxl_bo_unreserve(user_bo);
364out_unref:
365 drm_gem_object_unreference_unlocked(obj);
366 return ret;
367}
368
369static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
370 int x, int y)
371{
372 struct drm_device *dev = crtc->dev;
373 struct qxl_device *qdev = dev->dev_private;
374 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
375 struct qxl_release *release;
376 struct qxl_cursor_cmd *cmd;
377 int ret;
378
379 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
380 &release, NULL);
381
382 qcrtc->cur_x = x;
383 qcrtc->cur_y = y;
384
385 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
386 cmd->type = QXL_CURSOR_MOVE;
387 cmd->u.position.x = qcrtc->cur_x;
388 cmd->u.position.y = qcrtc->cur_y;
389 qxl_release_unmap(qdev, release, &cmd->release_info);
390
391 qxl_fence_releaseable(qdev, release);
392 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
393 qxl_release_unreserve(qdev, release);
394 return 0;
395}
396
397
398static const struct drm_crtc_funcs qxl_crtc_funcs = {
399 .cursor_set = qxl_crtc_cursor_set,
400 .cursor_move = qxl_crtc_cursor_move,
401 .gamma_set = qxl_crtc_gamma_set,
402 .set_config = drm_crtc_helper_set_config,
403 .destroy = qxl_crtc_destroy,
404};
405
406static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
407{
408 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
409
410 if (qxl_fb->obj)
411 drm_gem_object_unreference_unlocked(qxl_fb->obj);
412 drm_framebuffer_cleanup(fb);
413 kfree(qxl_fb);
414}
415
416int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
417 struct drm_file *file_priv,
418 unsigned flags, unsigned color,
419 struct drm_clip_rect *clips,
420 unsigned num_clips)
421{
422 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
423 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
424 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
425 struct drm_clip_rect norect;
426 struct qxl_bo *qobj;
427 int inc = 1;
428
429 qobj = gem_to_qxl_bo(qxl_fb->obj);
430 if (qxl_fb != qdev->active_user_framebuffer) {
431 DRM_INFO("%s: qxl_fb 0x%p != qdev->active_user_framebuffer 0x%p\n",
432 __func__, qxl_fb, qdev->active_user_framebuffer);
433 }
434 if (!num_clips) {
435 num_clips = 1;
436 clips = &norect;
437 norect.x1 = norect.y1 = 0;
438 norect.x2 = fb->width;
439 norect.y2 = fb->height;
440 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
441 num_clips /= 2;
442 inc = 2; /* skip source rects */
443 }
444
445 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
446 clips, num_clips, inc);
447 return 0;
448}
449
450static const struct drm_framebuffer_funcs qxl_fb_funcs = {
451 .destroy = qxl_user_framebuffer_destroy,
452 .dirty = qxl_framebuffer_surface_dirty,
453/* TODO?
454 * .create_handle = qxl_user_framebuffer_create_handle, */
455};
456
457int
458qxl_framebuffer_init(struct drm_device *dev,
459 struct qxl_framebuffer *qfb,
460 struct drm_mode_fb_cmd2 *mode_cmd,
461 struct drm_gem_object *obj)
462{
463 int ret;
464
465 qfb->obj = obj;
466 ret = drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs);
467 if (ret) {
468 qfb->obj = NULL;
469 return ret;
470 }
471 drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
472 return 0;
473}
474
475static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
476{
477}
478
479static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
480 const struct drm_display_mode *mode,
481 struct drm_display_mode *adjusted_mode)
482{
483 struct drm_device *dev = crtc->dev;
484 struct qxl_device *qdev = dev->dev_private;
485
486 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
487 __func__,
488 mode->hdisplay, mode->vdisplay,
489 adjusted_mode->hdisplay,
490 adjusted_mode->vdisplay);
491 return true;
492}
493
494void
495qxl_send_monitors_config(struct qxl_device *qdev)
496{
497 int i;
498
499 BUG_ON(!qdev->ram_header->monitors_config);
500
501 if (qdev->monitors_config->count == 0) {
502 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
503 return;
504 }
505 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
506 struct qxl_head *head = &qdev->monitors_config->heads[i];
507
508 if (head->y > 8192 || head->y < head->x ||
509 head->width > 8192 || head->height > 8192) {
510 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
511 i, head->width, head->height,
512 head->x, head->y);
513 return;
514 }
515 }
516 qxl_io_monitors_config(qdev);
517}
518
519static void qxl_monitors_config_set_single(struct qxl_device *qdev,
520 unsigned x, unsigned y,
521 unsigned width, unsigned height)
522{
523 DRM_DEBUG("%dx%d+%d+%d\n", width, height, x, y);
524 qdev->monitors_config->count = 1;
525 qdev->monitors_config->heads[0].x = x;
526 qdev->monitors_config->heads[0].y = y;
527 qdev->monitors_config->heads[0].width = width;
528 qdev->monitors_config->heads[0].height = height;
529}
530
531static int qxl_crtc_mode_set(struct drm_crtc *crtc,
532 struct drm_display_mode *mode,
533 struct drm_display_mode *adjusted_mode,
534 int x, int y,
535 struct drm_framebuffer *old_fb)
536{
537 struct drm_device *dev = crtc->dev;
538 struct qxl_device *qdev = dev->dev_private;
539 struct qxl_mode *m = (void *)mode->private;
540 struct qxl_framebuffer *qfb;
541 struct qxl_bo *bo, *old_bo = NULL;
542 uint32_t width, height, base_offset;
543 bool recreate_primary = false;
544 int ret;
545
546 if (!crtc->fb) {
547 DRM_DEBUG_KMS("No FB bound\n");
548 return 0;
549 }
550
551 if (old_fb) {
552 qfb = to_qxl_framebuffer(old_fb);
553 old_bo = gem_to_qxl_bo(qfb->obj);
554 }
555 qfb = to_qxl_framebuffer(crtc->fb);
556 bo = gem_to_qxl_bo(qfb->obj);
557 if (!m)
558 /* and do we care? */
559 DRM_DEBUG("%dx%d: not a native mode\n", x, y);
560 else
561 DRM_DEBUG("%dx%d: qxl id %d\n",
562 mode->hdisplay, mode->vdisplay, m->id);
563 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
564 x, y,
565 mode->hdisplay, mode->vdisplay,
566 adjusted_mode->hdisplay,
567 adjusted_mode->vdisplay);
568
569 recreate_primary = true;
570
571 width = mode->hdisplay;
572 height = mode->vdisplay;
573 base_offset = 0;
574
575 ret = qxl_bo_reserve(bo, false);
576 if (ret != 0)
577 return ret;
578 ret = qxl_bo_pin(bo, bo->type, NULL);
579 if (ret != 0) {
580 qxl_bo_unreserve(bo);
581 return -EINVAL;
582 }
583 qxl_bo_unreserve(bo);
584 if (recreate_primary) {
585 qxl_io_destroy_primary(qdev);
586 qxl_io_log(qdev,
587 "recreate primary: %dx%d (was %dx%d,%d,%d)\n",
588 width, height, bo->surf.width,
589 bo->surf.height, bo->surf.stride, bo->surf.format);
590 qxl_io_create_primary(qdev, width, height, base_offset, bo);
591 bo->is_primary = true;
592 }
593
594 if (old_bo && old_bo != bo) {
595 old_bo->is_primary = false;
596 ret = qxl_bo_reserve(old_bo, false);
597 qxl_bo_unpin(old_bo);
598 qxl_bo_unreserve(old_bo);
599 }
600
601 if (qdev->monitors_config->count == 0) {
602 qxl_monitors_config_set_single(qdev, x, y,
603 mode->hdisplay,
604 mode->vdisplay);
605 }
606 qdev->mode_set = true;
607 return 0;
608}
609
610static void qxl_crtc_prepare(struct drm_crtc *crtc)
611{
612 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
613 crtc->mode.hdisplay, crtc->mode.vdisplay,
614 crtc->x, crtc->y, crtc->enabled);
615}
616
617static void qxl_crtc_commit(struct drm_crtc *crtc)
618{
619 DRM_DEBUG("\n");
620}
621
622void qxl_crtc_load_lut(struct drm_crtc *crtc)
623{
624 DRM_DEBUG("\n");
625}
626
627static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
628 .dpms = qxl_crtc_dpms,
629 .mode_fixup = qxl_crtc_mode_fixup,
630 .mode_set = qxl_crtc_mode_set,
631 .prepare = qxl_crtc_prepare,
632 .commit = qxl_crtc_commit,
633 .load_lut = qxl_crtc_load_lut,
634};
635
636int qdev_crtc_init(struct drm_device *dev, int num_crtc)
637{
638 struct qxl_crtc *qxl_crtc;
639
640 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
641 if (!qxl_crtc)
642 return -ENOMEM;
643
644 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
645
646 drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
647 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
648 return 0;
649}
650
651static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
652{
653 DRM_DEBUG("\n");
654}
655
656static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
657 const struct drm_display_mode *mode,
658 struct drm_display_mode *adjusted_mode)
659{
660 DRM_DEBUG("\n");
661 return true;
662}
663
664static void qxl_enc_prepare(struct drm_encoder *encoder)
665{
666 DRM_DEBUG("\n");
667}
668
669static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
670 struct drm_encoder *encoder)
671{
672 int i;
673 struct qxl_head *head;
674 struct drm_display_mode *mode;
675
676 BUG_ON(!encoder);
677 /* TODO: ugly, do better */
678 for (i = 0 ; (encoder->possible_crtcs != (1 << i)) && i < 32; ++i)
679 ;
680 if (encoder->possible_crtcs != (1 << i)) {
681 DRM_ERROR("encoder has wrong possible_crtcs: %x\n",
682 encoder->possible_crtcs);
683 return;
684 }
685 if (!qdev->monitors_config ||
686 qdev->monitors_config->max_allowed <= i) {
687 DRM_ERROR(
688 "head number too large or missing monitors config: %p, %d",
689 qdev->monitors_config,
690 qdev->monitors_config ?
691 qdev->monitors_config->max_allowed : -1);
692 return;
693 }
694 if (!encoder->crtc) {
695 DRM_ERROR("missing crtc on encoder %p\n", encoder);
696 return;
697 }
698 if (i != 0)
699 DRM_DEBUG("missing for multiple monitors: no head holes\n");
700 head = &qdev->monitors_config->heads[i];
701 head->id = i;
702 head->surface_id = 0;
703 if (encoder->crtc->enabled) {
704 mode = &encoder->crtc->mode;
705 head->width = mode->hdisplay;
706 head->height = mode->vdisplay;
707 head->x = encoder->crtc->x;
708 head->y = encoder->crtc->y;
709 if (qdev->monitors_config->count < i + 1)
710 qdev->monitors_config->count = i + 1;
711 } else {
712 head->width = 0;
713 head->height = 0;
714 head->x = 0;
715 head->y = 0;
716 }
717 DRM_DEBUG("setting head %d to +%d+%d %dx%d\n",
718 i, head->x, head->y, head->width, head->height);
719 head->flags = 0;
720 /* TODO - somewhere else to call this for multiple monitors
721 * (config_commit?) */
722 qxl_send_monitors_config(qdev);
723}
724
725static void qxl_enc_commit(struct drm_encoder *encoder)
726{
727 struct qxl_device *qdev = encoder->dev->dev_private;
728
729 qxl_write_monitors_config_for_encoder(qdev, encoder);
730 DRM_DEBUG("\n");
731}
732
733static void qxl_enc_mode_set(struct drm_encoder *encoder,
734 struct drm_display_mode *mode,
735 struct drm_display_mode *adjusted_mode)
736{
737 DRM_DEBUG("\n");
738}
739
740static int qxl_conn_get_modes(struct drm_connector *connector)
741{
742 int ret = 0;
743 struct qxl_device *qdev = connector->dev->dev_private;
744
745 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
746 /* TODO: what should we do here? only show the configured modes for the
747 * device, or allow the full list, or both? */
748 if (qdev->monitors_config && qdev->monitors_config->count) {
749 ret = qxl_add_monitors_config_modes(connector);
750 if (ret < 0)
751 return ret;
752 }
753 ret += qxl_add_common_modes(connector);
754 return ret;
755}
756
757static int qxl_conn_mode_valid(struct drm_connector *connector,
758 struct drm_display_mode *mode)
759{
760 /* TODO: is this called for user defined modes? (xrandr --add-mode)
761 * TODO: check that the mode fits in the framebuffer */
762 DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay,
763 mode->vdisplay, mode->status);
764 return MODE_OK;
765}
766
767struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
768{
769 struct qxl_output *qxl_output =
770 drm_connector_to_qxl_output(connector);
771
772 DRM_DEBUG("\n");
773 return &qxl_output->enc;
774}
775
776
777static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
778 .dpms = qxl_enc_dpms,
779 .mode_fixup = qxl_enc_mode_fixup,
780 .prepare = qxl_enc_prepare,
781 .mode_set = qxl_enc_mode_set,
782 .commit = qxl_enc_commit,
783};
784
785static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
786 .get_modes = qxl_conn_get_modes,
787 .mode_valid = qxl_conn_mode_valid,
788 .best_encoder = qxl_best_encoder,
789};
790
791static void qxl_conn_save(struct drm_connector *connector)
792{
793 DRM_DEBUG("\n");
794}
795
796static void qxl_conn_restore(struct drm_connector *connector)
797{
798 DRM_DEBUG("\n");
799}
800
801static enum drm_connector_status qxl_conn_detect(
802 struct drm_connector *connector,
803 bool force)
804{
805 struct qxl_output *output =
806 drm_connector_to_qxl_output(connector);
807 struct drm_device *ddev = connector->dev;
808 struct qxl_device *qdev = ddev->dev_private;
809 int connected;
810
811 /* The first monitor is always connected */
812 connected = (output->index == 0) ||
813 (qdev->monitors_config &&
814 qdev->monitors_config->count > output->index);
815
816 DRM_DEBUG("\n");
817 return connected ? connector_status_connected
818 : connector_status_disconnected;
819}
820
821static int qxl_conn_set_property(struct drm_connector *connector,
822 struct drm_property *property,
823 uint64_t value)
824{
825 DRM_DEBUG("\n");
826 return 0;
827}
828
829static void qxl_conn_destroy(struct drm_connector *connector)
830{
831 struct qxl_output *qxl_output =
832 drm_connector_to_qxl_output(connector);
833
834 drm_sysfs_connector_remove(connector);
835 drm_connector_cleanup(connector);
836 kfree(qxl_output);
837}
838
839static const struct drm_connector_funcs qxl_connector_funcs = {
840 .dpms = drm_helper_connector_dpms,
841 .save = qxl_conn_save,
842 .restore = qxl_conn_restore,
843 .detect = qxl_conn_detect,
844 .fill_modes = drm_helper_probe_single_connector_modes,
845 .set_property = qxl_conn_set_property,
846 .destroy = qxl_conn_destroy,
847};
848
849static void qxl_enc_destroy(struct drm_encoder *encoder)
850{
851 drm_encoder_cleanup(encoder);
852}
853
854static const struct drm_encoder_funcs qxl_enc_funcs = {
855 .destroy = qxl_enc_destroy,
856};
857
858int qdev_output_init(struct drm_device *dev, int num_output)
859{
860 struct qxl_output *qxl_output;
861 struct drm_connector *connector;
862 struct drm_encoder *encoder;
863
864 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
865 if (!qxl_output)
866 return -ENOMEM;
867
868 qxl_output->index = num_output;
869
870 connector = &qxl_output->base;
871 encoder = &qxl_output->enc;
872 drm_connector_init(dev, &qxl_output->base,
873 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
874
875 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
876 DRM_MODE_ENCODER_VIRTUAL);
877
878 encoder->possible_crtcs = 1 << num_output;
879 drm_mode_connector_attach_encoder(&qxl_output->base,
880 &qxl_output->enc);
881 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
882 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
883
884 drm_sysfs_connector_add(connector);
885 return 0;
886}
887
888static struct drm_framebuffer *
889qxl_user_framebuffer_create(struct drm_device *dev,
890 struct drm_file *file_priv,
891 struct drm_mode_fb_cmd2 *mode_cmd)
892{
893 struct drm_gem_object *obj;
894 struct qxl_framebuffer *qxl_fb;
895 struct qxl_device *qdev = dev->dev_private;
896 int ret;
897
898 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
899
900 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
901 if (qxl_fb == NULL)
902 return NULL;
903
904 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
905 if (ret) {
906 kfree(qxl_fb);
907 drm_gem_object_unreference_unlocked(obj);
908 return NULL;
909 }
910
911 if (qdev->active_user_framebuffer) {
912 DRM_INFO("%s: active_user_framebuffer %p -> %p\n",
913 __func__,
914 qdev->active_user_framebuffer, qxl_fb);
915 }
916 qdev->active_user_framebuffer = qxl_fb;
917
918 return &qxl_fb->base;
919}
920
921static const struct drm_mode_config_funcs qxl_mode_funcs = {
922 .fb_create = qxl_user_framebuffer_create,
923};
924
925int qxl_modeset_init(struct qxl_device *qdev)
926{
927 int i;
928 int ret;
929 struct drm_gem_object *gobj;
930 int max_allowed = QXL_NUM_OUTPUTS;
931 int monitors_config_size = sizeof(struct qxl_monitors_config) +
932 max_allowed * sizeof(struct qxl_head);
933
934 drm_mode_config_init(qdev->ddev);
935 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
936 QXL_GEM_DOMAIN_VRAM,
937 false, false, NULL, &gobj);
938 if (ret) {
939 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
940 return -ENOMEM;
941 }
942 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
943 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
944 qdev->monitors_config = qdev->monitors_config_bo->kptr;
945 qdev->ram_header->monitors_config =
946 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
947
948 memset(qdev->monitors_config, 0, monitors_config_size);
949 qdev->monitors_config->max_allowed = max_allowed;
950
951 qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
952
953 /* modes will be validated against the framebuffer size */
954 qdev->ddev->mode_config.min_width = 320;
955 qdev->ddev->mode_config.min_height = 200;
956 qdev->ddev->mode_config.max_width = 8192;
957 qdev->ddev->mode_config.max_height = 8192;
958
959 qdev->ddev->mode_config.fb_base = qdev->vram_base;
960 for (i = 0 ; i < QXL_NUM_OUTPUTS; ++i) {
961 qdev_crtc_init(qdev->ddev, i);
962 qdev_output_init(qdev->ddev, i);
963 }
964
965 qdev->mode_info.mode_config_initialized = true;
966
967 /* primary surface must be created by this point, to allow
968 * issuing command queue commands and having them read by
969 * spice server. */
970 qxl_fbdev_init(qdev);
971 return 0;
972}
973
974void qxl_modeset_fini(struct qxl_device *qdev)
975{
976 qxl_fbdev_fini(qdev);
977 if (qdev->mode_info.mode_config_initialized) {
978 drm_mode_config_cleanup(qdev->ddev);
979 qdev->mode_info.mode_config_initialized = false;
980 }
981}