blob: c2cc9b52d330b92a0c098c608f12115c1bfb933c [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
Dave Airlie07f8d9b2013-07-02 06:37:13 +010033static bool qxl_head_enabled(struct qxl_head *head)
34{
35 return head->width && head->height;
36}
37
Dave Airlief64122c2013-02-25 14:47:55 +100038void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
39{
40 if (qdev->client_monitors_config &&
41 count > qdev->client_monitors_config->count) {
42 kfree(qdev->client_monitors_config);
Dave Airlie62c8ba72013-04-16 13:36:00 +100043 qdev->client_monitors_config = NULL;
Dave Airlief64122c2013-02-25 14:47:55 +100044 }
45 if (!qdev->client_monitors_config) {
46 qdev->client_monitors_config = kzalloc(
47 sizeof(struct qxl_monitors_config) +
48 sizeof(struct qxl_head) * count, GFP_KERNEL);
49 if (!qdev->client_monitors_config) {
50 qxl_io_log(qdev,
51 "%s: allocation failure for %u heads\n",
52 __func__, count);
53 return;
54 }
55 }
56 qdev->client_monitors_config->count = count;
57}
58
59static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
60{
61 int i;
62 int num_monitors;
63 uint32_t crc;
64
Dave Airlief64122c2013-02-25 14:47:55 +100065 num_monitors = qdev->rom->client_monitors_config.count;
66 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
67 sizeof(qdev->rom->client_monitors_config));
68 if (crc != qdev->rom->client_monitors_config_crc) {
69 qxl_io_log(qdev, "crc mismatch: have %X (%d) != %X\n", crc,
70 sizeof(qdev->rom->client_monitors_config),
71 qdev->rom->client_monitors_config_crc);
72 return 1;
73 }
74 if (num_monitors > qdev->monitors_config->max_allowed) {
Dave Airlie5b8788c2013-07-01 14:14:38 +100075 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
76 qdev->monitors_config->max_allowed, num_monitors);
Dave Airlief64122c2013-02-25 14:47:55 +100077 num_monitors = qdev->monitors_config->max_allowed;
78 } else {
79 num_monitors = qdev->rom->client_monitors_config.count;
80 }
81 qxl_alloc_client_monitors_config(qdev, num_monitors);
82 /* we copy max from the client but it isn't used */
83 qdev->client_monitors_config->max_allowed =
84 qdev->monitors_config->max_allowed;
85 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
86 struct qxl_urect *c_rect =
87 &qdev->rom->client_monitors_config.heads[i];
88 struct qxl_head *client_head =
89 &qdev->client_monitors_config->heads[i];
Dave Airlie07f8d9b2013-07-02 06:37:13 +010090 client_head->x = c_rect->left;
91 client_head->y = c_rect->top;
92 client_head->width = c_rect->right - c_rect->left;
93 client_head->height = c_rect->bottom - c_rect->top;
94 client_head->surface_id = 0;
95 client_head->id = i;
96 client_head->flags = 0;
97 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
98 client_head->x, client_head->y);
Dave Airlief64122c2013-02-25 14:47:55 +100099 }
100 return 0;
101}
102
103void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
104{
105
106 while (qxl_display_copy_rom_client_monitors_config(qdev)) {
107 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
108 " retrying\n");
109 }
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200110
111 if (!drm_helper_hpd_irq_event(qdev->ddev)) {
112 /* notify that the monitor configuration changed, to
113 adjust at the arbitrary resolution */
114 drm_kms_helper_hotplug_event(qdev->ddev);
115 }
Dave Airlief64122c2013-02-25 14:47:55 +1000116}
117
118static int qxl_add_monitors_config_modes(struct drm_connector *connector)
119{
120 struct drm_device *dev = connector->dev;
121 struct qxl_device *qdev = dev->dev_private;
122 struct qxl_output *output = drm_connector_to_qxl_output(connector);
123 int h = output->index;
124 struct drm_display_mode *mode = NULL;
125 struct qxl_head *head;
126
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100127 if (!qdev->client_monitors_config)
Dave Airlief64122c2013-02-25 14:47:55 +1000128 return 0;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100129 head = &qdev->client_monitors_config->heads[h];
Dave Airlief64122c2013-02-25 14:47:55 +1000130
131 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
132 false);
133 mode->type |= DRM_MODE_TYPE_PREFERRED;
134 drm_mode_probed_add(connector, mode);
135 return 1;
136}
137
138static int qxl_add_common_modes(struct drm_connector *connector)
139{
140 struct drm_device *dev = connector->dev;
141 struct drm_display_mode *mode = NULL;
142 int i;
143 struct mode_size {
144 int w;
145 int h;
146 } common_modes[] = {
147 { 640, 480},
148 { 720, 480},
149 { 800, 600},
150 { 848, 480},
151 {1024, 768},
152 {1152, 768},
153 {1280, 720},
154 {1280, 800},
155 {1280, 854},
156 {1280, 960},
157 {1280, 1024},
158 {1440, 900},
159 {1400, 1050},
160 {1680, 1050},
161 {1600, 1200},
162 {1920, 1080},
163 {1920, 1200}
164 };
165
166 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
167 if (common_modes[i].w < 320 || common_modes[i].h < 200)
168 continue;
169
170 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
171 60, false, false, false);
172 if (common_modes[i].w == 1024 && common_modes[i].h == 768)
173 mode->type |= DRM_MODE_TYPE_PREFERRED;
174 drm_mode_probed_add(connector, mode);
175 }
176 return i - 1;
177}
178
Dave Airlief64122c2013-02-25 14:47:55 +1000179static void qxl_crtc_destroy(struct drm_crtc *crtc)
180{
181 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
182
183 drm_crtc_cleanup(crtc);
184 kfree(qxl_crtc);
185}
186
Dave Airlie8002db62013-07-23 14:16:42 +1000187static int
Dave Airlief64122c2013-02-25 14:47:55 +1000188qxl_hide_cursor(struct qxl_device *qdev)
189{
190 struct qxl_release *release;
191 struct qxl_cursor_cmd *cmd;
192 int ret;
193
194 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
195 &release, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000196 if (ret)
197 return ret;
198
199 ret = qxl_release_reserve_list(release, true);
200 if (ret) {
201 qxl_release_free(qdev, release);
202 return ret;
203 }
Dave Airlief64122c2013-02-25 14:47:55 +1000204
205 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
206 cmd->type = QXL_CURSOR_HIDE;
207 qxl_release_unmap(qdev, release, &cmd->release_info);
208
Dave Airlief64122c2013-02-25 14:47:55 +1000209 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000210 qxl_release_fence_buffer_objects(release);
211 return 0;
Dave Airlief64122c2013-02-25 14:47:55 +1000212}
213
Dave Airliec0a60802013-06-20 11:48:53 +1000214static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
215 struct drm_file *file_priv,
216 uint32_t handle,
217 uint32_t width,
218 uint32_t height, int32_t hot_x, int32_t hot_y)
Dave Airlief64122c2013-02-25 14:47:55 +1000219{
220 struct drm_device *dev = crtc->dev;
221 struct qxl_device *qdev = dev->dev_private;
222 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
223 struct drm_gem_object *obj;
224 struct qxl_cursor *cursor;
225 struct qxl_cursor_cmd *cmd;
226 struct qxl_bo *cursor_bo, *user_bo;
227 struct qxl_release *release;
228 void *user_ptr;
229
230 int size = 64*64*4;
231 int ret = 0;
Dave Airlie8002db62013-07-23 14:16:42 +1000232 if (!handle)
233 return qxl_hide_cursor(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +1000234
235 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
236 if (!obj) {
237 DRM_ERROR("cannot find cursor object\n");
238 return -ENOENT;
239 }
240
241 user_bo = gem_to_qxl_bo(obj);
242
243 ret = qxl_bo_reserve(user_bo, false);
244 if (ret)
245 goto out_unref;
246
247 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000248 qxl_bo_unreserve(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000249 if (ret)
Dave Airlie8002db62013-07-23 14:16:42 +1000250 goto out_unref;
Dave Airlief64122c2013-02-25 14:47:55 +1000251
252 ret = qxl_bo_kmap(user_bo, &user_ptr);
253 if (ret)
254 goto out_unpin;
255
256 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
257 QXL_RELEASE_CURSOR_CMD,
258 &release, NULL);
259 if (ret)
260 goto out_kunmap;
Dave Airlie8002db62013-07-23 14:16:42 +1000261
262 ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size,
263 &cursor_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000264 if (ret)
265 goto out_free_release;
Dave Airlie8002db62013-07-23 14:16:42 +1000266
267 ret = qxl_release_reserve_list(release, false);
Dave Airlief64122c2013-02-25 14:47:55 +1000268 if (ret)
269 goto out_free_bo;
270
Dave Airlie8002db62013-07-23 14:16:42 +1000271 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
272 if (ret)
273 goto out_backoff;
274
Dave Airlief64122c2013-02-25 14:47:55 +1000275 cursor->header.unique = 0;
276 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
277 cursor->header.width = 64;
278 cursor->header.height = 64;
Dave Airliec0a60802013-06-20 11:48:53 +1000279 cursor->header.hot_spot_x = hot_x;
280 cursor->header.hot_spot_y = hot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000281 cursor->data_size = size;
282 cursor->chunk.next_chunk = 0;
283 cursor->chunk.prev_chunk = 0;
284 cursor->chunk.data_size = size;
285
286 memcpy(cursor->chunk.data, user_ptr, size);
287
288 qxl_bo_kunmap(cursor_bo);
289
Dave Airlief64122c2013-02-25 14:47:55 +1000290 qxl_bo_kunmap(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000291
292 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
293 cmd->type = QXL_CURSOR_SET;
294 cmd->u.set.position.x = qcrtc->cur_x;
295 cmd->u.set.position.y = qcrtc->cur_y;
296
297 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
Dave Airlief64122c2013-02-25 14:47:55 +1000298
299 cmd->u.set.visible = 1;
300 qxl_release_unmap(qdev, release, &cmd->release_info);
301
Dave Airlief64122c2013-02-25 14:47:55 +1000302 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000303 qxl_release_fence_buffer_objects(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000304
Dave Airlie8002db62013-07-23 14:16:42 +1000305 /* finish with the userspace bo */
306 ret = qxl_bo_reserve(user_bo, false);
307 if (!ret) {
308 qxl_bo_unpin(user_bo);
309 qxl_bo_unreserve(user_bo);
310 }
311 drm_gem_object_unreference_unlocked(obj);
312
Dave Airlief64122c2013-02-25 14:47:55 +1000313 qxl_bo_unref(&cursor_bo);
314
315 return ret;
Dave Airlie8002db62013-07-23 14:16:42 +1000316
317out_backoff:
318 qxl_release_backoff_reserve_list(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000319out_free_bo:
320 qxl_bo_unref(&cursor_bo);
321out_free_release:
Dave Airlief64122c2013-02-25 14:47:55 +1000322 qxl_release_free(qdev, release);
323out_kunmap:
324 qxl_bo_kunmap(user_bo);
325out_unpin:
326 qxl_bo_unpin(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000327out_unref:
328 drm_gem_object_unreference_unlocked(obj);
329 return ret;
330}
331
332static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
333 int x, int y)
334{
335 struct drm_device *dev = crtc->dev;
336 struct qxl_device *qdev = dev->dev_private;
337 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
338 struct qxl_release *release;
339 struct qxl_cursor_cmd *cmd;
340 int ret;
341
342 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
343 &release, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000344 if (ret)
345 return ret;
346
347 ret = qxl_release_reserve_list(release, true);
348 if (ret) {
349 qxl_release_free(qdev, release);
350 return ret;
351 }
Dave Airlief64122c2013-02-25 14:47:55 +1000352
353 qcrtc->cur_x = x;
354 qcrtc->cur_y = y;
355
356 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
357 cmd->type = QXL_CURSOR_MOVE;
358 cmd->u.position.x = qcrtc->cur_x;
359 cmd->u.position.y = qcrtc->cur_y;
360 qxl_release_unmap(qdev, release, &cmd->release_info);
361
Dave Airlief64122c2013-02-25 14:47:55 +1000362 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000363 qxl_release_fence_buffer_objects(release);
364
Dave Airlief64122c2013-02-25 14:47:55 +1000365 return 0;
366}
367
368
369static const struct drm_crtc_funcs qxl_crtc_funcs = {
Dave Airliec0a60802013-06-20 11:48:53 +1000370 .cursor_set2 = qxl_crtc_cursor_set2,
Dave Airlief64122c2013-02-25 14:47:55 +1000371 .cursor_move = qxl_crtc_cursor_move,
Dave Airlief64122c2013-02-25 14:47:55 +1000372 .set_config = drm_crtc_helper_set_config,
373 .destroy = qxl_crtc_destroy,
374};
375
376static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
377{
378 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
379
380 if (qxl_fb->obj)
381 drm_gem_object_unreference_unlocked(qxl_fb->obj);
382 drm_framebuffer_cleanup(fb);
383 kfree(qxl_fb);
384}
385
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000386static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
387 struct drm_file *file_priv,
388 unsigned flags, unsigned color,
389 struct drm_clip_rect *clips,
390 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000391{
392 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
393 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
394 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
395 struct drm_clip_rect norect;
396 struct qxl_bo *qobj;
397 int inc = 1;
398
399 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000400 /* if we aren't primary surface ignore this */
401 if (!qobj->is_primary)
402 return 0;
403
Dave Airlief64122c2013-02-25 14:47:55 +1000404 if (!num_clips) {
405 num_clips = 1;
406 clips = &norect;
407 norect.x1 = norect.y1 = 0;
408 norect.x2 = fb->width;
409 norect.y2 = fb->height;
410 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
411 num_clips /= 2;
412 inc = 2; /* skip source rects */
413 }
414
415 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
416 clips, num_clips, inc);
417 return 0;
418}
419
420static const struct drm_framebuffer_funcs qxl_fb_funcs = {
421 .destroy = qxl_user_framebuffer_destroy,
422 .dirty = qxl_framebuffer_surface_dirty,
423/* TODO?
424 * .create_handle = qxl_user_framebuffer_create_handle, */
425};
426
427int
428qxl_framebuffer_init(struct drm_device *dev,
429 struct qxl_framebuffer *qfb,
430 struct drm_mode_fb_cmd2 *mode_cmd,
431 struct drm_gem_object *obj)
432{
433 int ret;
434
435 qfb->obj = obj;
436 ret = drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs);
437 if (ret) {
438 qfb->obj = NULL;
439 return ret;
440 }
441 drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
442 return 0;
443}
444
445static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
446{
447}
448
449static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
450 const struct drm_display_mode *mode,
451 struct drm_display_mode *adjusted_mode)
452{
453 struct drm_device *dev = crtc->dev;
454 struct qxl_device *qdev = dev->dev_private;
455
456 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
457 __func__,
458 mode->hdisplay, mode->vdisplay,
459 adjusted_mode->hdisplay,
460 adjusted_mode->vdisplay);
461 return true;
462}
463
464void
465qxl_send_monitors_config(struct qxl_device *qdev)
466{
467 int i;
468
469 BUG_ON(!qdev->ram_header->monitors_config);
470
471 if (qdev->monitors_config->count == 0) {
472 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
473 return;
474 }
475 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
476 struct qxl_head *head = &qdev->monitors_config->heads[i];
477
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100478 if (head->y > 8192 || head->x > 8192 ||
Dave Airlief64122c2013-02-25 14:47:55 +1000479 head->width > 8192 || head->height > 8192) {
480 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
481 i, head->width, head->height,
482 head->x, head->y);
483 return;
484 }
485 }
486 qxl_io_monitors_config(qdev);
487}
488
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100489static void qxl_monitors_config_set(struct qxl_device *qdev,
490 int index,
491 unsigned x, unsigned y,
492 unsigned width, unsigned height,
493 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000494{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100495 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
496 qdev->monitors_config->heads[index].x = x;
497 qdev->monitors_config->heads[index].y = y;
498 qdev->monitors_config->heads[index].width = width;
499 qdev->monitors_config->heads[index].height = height;
500 qdev->monitors_config->heads[index].surface_id = surf_id;
501
Dave Airlief64122c2013-02-25 14:47:55 +1000502}
503
504static int qxl_crtc_mode_set(struct drm_crtc *crtc,
505 struct drm_display_mode *mode,
506 struct drm_display_mode *adjusted_mode,
507 int x, int y,
508 struct drm_framebuffer *old_fb)
509{
510 struct drm_device *dev = crtc->dev;
511 struct qxl_device *qdev = dev->dev_private;
512 struct qxl_mode *m = (void *)mode->private;
513 struct qxl_framebuffer *qfb;
514 struct qxl_bo *bo, *old_bo = NULL;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100515 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Dave Airlief64122c2013-02-25 14:47:55 +1000516 uint32_t width, height, base_offset;
517 bool recreate_primary = false;
518 int ret;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100519 int surf_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000520 if (!crtc->fb) {
521 DRM_DEBUG_KMS("No FB bound\n");
522 return 0;
523 }
524
525 if (old_fb) {
526 qfb = to_qxl_framebuffer(old_fb);
527 old_bo = gem_to_qxl_bo(qfb->obj);
528 }
529 qfb = to_qxl_framebuffer(crtc->fb);
530 bo = gem_to_qxl_bo(qfb->obj);
531 if (!m)
532 /* and do we care? */
533 DRM_DEBUG("%dx%d: not a native mode\n", x, y);
534 else
535 DRM_DEBUG("%dx%d: qxl id %d\n",
536 mode->hdisplay, mode->vdisplay, m->id);
537 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
538 x, y,
539 mode->hdisplay, mode->vdisplay,
540 adjusted_mode->hdisplay,
541 adjusted_mode->vdisplay);
542
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100543 if (qcrtc->index == 0)
544 recreate_primary = true;
Dave Airlief64122c2013-02-25 14:47:55 +1000545
546 width = mode->hdisplay;
547 height = mode->vdisplay;
548 base_offset = 0;
549
550 ret = qxl_bo_reserve(bo, false);
551 if (ret != 0)
552 return ret;
553 ret = qxl_bo_pin(bo, bo->type, NULL);
554 if (ret != 0) {
555 qxl_bo_unreserve(bo);
556 return -EINVAL;
557 }
558 qxl_bo_unreserve(bo);
559 if (recreate_primary) {
560 qxl_io_destroy_primary(qdev);
561 qxl_io_log(qdev,
562 "recreate primary: %dx%d (was %dx%d,%d,%d)\n",
563 width, height, bo->surf.width,
564 bo->surf.height, bo->surf.stride, bo->surf.format);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100565 qxl_io_create_primary(qdev, base_offset, bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000566 bo->is_primary = true;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100567 surf_id = 0;
568 } else {
569 surf_id = bo->surface_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000570 }
571
572 if (old_bo && old_bo != bo) {
573 old_bo->is_primary = false;
574 ret = qxl_bo_reserve(old_bo, false);
575 qxl_bo_unpin(old_bo);
576 qxl_bo_unreserve(old_bo);
577 }
578
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100579 qxl_monitors_config_set(qdev, qcrtc->index, x, y,
580 mode->hdisplay,
581 mode->vdisplay, surf_id);
Dave Airlief64122c2013-02-25 14:47:55 +1000582 return 0;
583}
584
585static void qxl_crtc_prepare(struct drm_crtc *crtc)
586{
587 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
588 crtc->mode.hdisplay, crtc->mode.vdisplay,
589 crtc->x, crtc->y, crtc->enabled);
590}
591
592static void qxl_crtc_commit(struct drm_crtc *crtc)
593{
594 DRM_DEBUG("\n");
595}
596
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100597static void qxl_crtc_disable(struct drm_crtc *crtc)
598{
599 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
600 struct drm_device *dev = crtc->dev;
601 struct qxl_device *qdev = dev->dev_private;
602 if (crtc->fb) {
603 struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->fb);
604 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
605 int ret;
606 ret = qxl_bo_reserve(bo, false);
607 qxl_bo_unpin(bo);
608 qxl_bo_unreserve(bo);
609 crtc->fb = NULL;
610 }
611
612 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
613
614 qxl_send_monitors_config(qdev);
615}
616
Dave Airlief64122c2013-02-25 14:47:55 +1000617static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
618 .dpms = qxl_crtc_dpms,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100619 .disable = qxl_crtc_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000620 .mode_fixup = qxl_crtc_mode_fixup,
621 .mode_set = qxl_crtc_mode_set,
622 .prepare = qxl_crtc_prepare,
623 .commit = qxl_crtc_commit,
Dave Airlief64122c2013-02-25 14:47:55 +1000624};
625
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100626static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000627{
628 struct qxl_crtc *qxl_crtc;
629
630 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
631 if (!qxl_crtc)
632 return -ENOMEM;
633
634 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100635 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000636 drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
637 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
638 return 0;
639}
640
641static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
642{
643 DRM_DEBUG("\n");
644}
645
646static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
647 const struct drm_display_mode *mode,
648 struct drm_display_mode *adjusted_mode)
649{
650 DRM_DEBUG("\n");
651 return true;
652}
653
654static void qxl_enc_prepare(struct drm_encoder *encoder)
655{
656 DRM_DEBUG("\n");
657}
658
659static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
660 struct drm_encoder *encoder)
661{
662 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100663 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000664 struct qxl_head *head;
665 struct drm_display_mode *mode;
666
667 BUG_ON(!encoder);
668 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100669 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000670 if (!qdev->monitors_config ||
671 qdev->monitors_config->max_allowed <= i) {
672 DRM_ERROR(
673 "head number too large or missing monitors config: %p, %d",
674 qdev->monitors_config,
675 qdev->monitors_config ?
676 qdev->monitors_config->max_allowed : -1);
677 return;
678 }
679 if (!encoder->crtc) {
680 DRM_ERROR("missing crtc on encoder %p\n", encoder);
681 return;
682 }
683 if (i != 0)
684 DRM_DEBUG("missing for multiple monitors: no head holes\n");
685 head = &qdev->monitors_config->heads[i];
686 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000687 if (encoder->crtc->enabled) {
688 mode = &encoder->crtc->mode;
689 head->width = mode->hdisplay;
690 head->height = mode->vdisplay;
691 head->x = encoder->crtc->x;
692 head->y = encoder->crtc->y;
693 if (qdev->monitors_config->count < i + 1)
694 qdev->monitors_config->count = i + 1;
695 } else {
696 head->width = 0;
697 head->height = 0;
698 head->x = 0;
699 head->y = 0;
700 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100701 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
702 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000703 head->flags = 0;
704 /* TODO - somewhere else to call this for multiple monitors
705 * (config_commit?) */
706 qxl_send_monitors_config(qdev);
707}
708
709static void qxl_enc_commit(struct drm_encoder *encoder)
710{
711 struct qxl_device *qdev = encoder->dev->dev_private;
712
713 qxl_write_monitors_config_for_encoder(qdev, encoder);
714 DRM_DEBUG("\n");
715}
716
717static void qxl_enc_mode_set(struct drm_encoder *encoder,
718 struct drm_display_mode *mode,
719 struct drm_display_mode *adjusted_mode)
720{
721 DRM_DEBUG("\n");
722}
723
724static int qxl_conn_get_modes(struct drm_connector *connector)
725{
726 int ret = 0;
727 struct qxl_device *qdev = connector->dev->dev_private;
728
729 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
730 /* TODO: what should we do here? only show the configured modes for the
731 * device, or allow the full list, or both? */
732 if (qdev->monitors_config && qdev->monitors_config->count) {
733 ret = qxl_add_monitors_config_modes(connector);
734 if (ret < 0)
735 return ret;
736 }
737 ret += qxl_add_common_modes(connector);
738 return ret;
739}
740
741static int qxl_conn_mode_valid(struct drm_connector *connector,
742 struct drm_display_mode *mode)
743{
744 /* TODO: is this called for user defined modes? (xrandr --add-mode)
745 * TODO: check that the mode fits in the framebuffer */
746 DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay,
747 mode->vdisplay, mode->status);
748 return MODE_OK;
749}
750
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000751static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +1000752{
753 struct qxl_output *qxl_output =
754 drm_connector_to_qxl_output(connector);
755
756 DRM_DEBUG("\n");
757 return &qxl_output->enc;
758}
759
760
761static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
762 .dpms = qxl_enc_dpms,
763 .mode_fixup = qxl_enc_mode_fixup,
764 .prepare = qxl_enc_prepare,
765 .mode_set = qxl_enc_mode_set,
766 .commit = qxl_enc_commit,
767};
768
769static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
770 .get_modes = qxl_conn_get_modes,
771 .mode_valid = qxl_conn_mode_valid,
772 .best_encoder = qxl_best_encoder,
773};
774
775static void qxl_conn_save(struct drm_connector *connector)
776{
777 DRM_DEBUG("\n");
778}
779
780static void qxl_conn_restore(struct drm_connector *connector)
781{
782 DRM_DEBUG("\n");
783}
784
785static enum drm_connector_status qxl_conn_detect(
786 struct drm_connector *connector,
787 bool force)
788{
789 struct qxl_output *output =
790 drm_connector_to_qxl_output(connector);
791 struct drm_device *ddev = connector->dev;
792 struct qxl_device *qdev = ddev->dev_private;
793 int connected;
794
795 /* The first monitor is always connected */
796 connected = (output->index == 0) ||
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100797 (qdev->client_monitors_config &&
798 qdev->client_monitors_config->count > output->index &&
799 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]));
Dave Airlief64122c2013-02-25 14:47:55 +1000800
801 DRM_DEBUG("\n");
802 return connected ? connector_status_connected
803 : connector_status_disconnected;
804}
805
806static int qxl_conn_set_property(struct drm_connector *connector,
807 struct drm_property *property,
808 uint64_t value)
809{
810 DRM_DEBUG("\n");
811 return 0;
812}
813
814static void qxl_conn_destroy(struct drm_connector *connector)
815{
816 struct qxl_output *qxl_output =
817 drm_connector_to_qxl_output(connector);
818
819 drm_sysfs_connector_remove(connector);
820 drm_connector_cleanup(connector);
821 kfree(qxl_output);
822}
823
824static const struct drm_connector_funcs qxl_connector_funcs = {
825 .dpms = drm_helper_connector_dpms,
826 .save = qxl_conn_save,
827 .restore = qxl_conn_restore,
828 .detect = qxl_conn_detect,
829 .fill_modes = drm_helper_probe_single_connector_modes,
830 .set_property = qxl_conn_set_property,
831 .destroy = qxl_conn_destroy,
832};
833
834static void qxl_enc_destroy(struct drm_encoder *encoder)
835{
836 drm_encoder_cleanup(encoder);
837}
838
839static const struct drm_encoder_funcs qxl_enc_funcs = {
840 .destroy = qxl_enc_destroy,
841};
842
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000843static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +1000844{
845 struct qxl_output *qxl_output;
846 struct drm_connector *connector;
847 struct drm_encoder *encoder;
848
849 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
850 if (!qxl_output)
851 return -ENOMEM;
852
853 qxl_output->index = num_output;
854
855 connector = &qxl_output->base;
856 encoder = &qxl_output->enc;
857 drm_connector_init(dev, &qxl_output->base,
858 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
859
860 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
861 DRM_MODE_ENCODER_VIRTUAL);
862
Dave Airlie5ff91e42013-07-05 10:20:33 +1000863 /* we get HPD via client monitors config */
864 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +1000865 encoder->possible_crtcs = 1 << num_output;
866 drm_mode_connector_attach_encoder(&qxl_output->base,
867 &qxl_output->enc);
868 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
869 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
870
871 drm_sysfs_connector_add(connector);
872 return 0;
873}
874
875static struct drm_framebuffer *
876qxl_user_framebuffer_create(struct drm_device *dev,
877 struct drm_file *file_priv,
878 struct drm_mode_fb_cmd2 *mode_cmd)
879{
880 struct drm_gem_object *obj;
881 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +1000882 int ret;
883
884 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
885
886 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
887 if (qxl_fb == NULL)
888 return NULL;
889
890 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
891 if (ret) {
892 kfree(qxl_fb);
893 drm_gem_object_unreference_unlocked(obj);
894 return NULL;
895 }
896
Dave Airlief64122c2013-02-25 14:47:55 +1000897 return &qxl_fb->base;
898}
899
900static const struct drm_mode_config_funcs qxl_mode_funcs = {
901 .fb_create = qxl_user_framebuffer_create,
902};
903
Dave Airlie2bd6ce82013-07-04 14:46:46 +1000904int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +1000905{
Dave Airlief64122c2013-02-25 14:47:55 +1000906 int ret;
907 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100908 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +1000909 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +1000910 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +1000911
Dave Airlief64122c2013-02-25 14:47:55 +1000912 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
913 QXL_GEM_DOMAIN_VRAM,
914 false, false, NULL, &gobj);
915 if (ret) {
916 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
917 return -ENOMEM;
918 }
919 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +1000920
921 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
922 if (ret)
923 return ret;
924
925 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
926 if (ret) {
927 qxl_bo_unreserve(qdev->monitors_config_bo);
928 return ret;
929 }
930
931 qxl_bo_unreserve(qdev->monitors_config_bo);
932
Dave Airlief64122c2013-02-25 14:47:55 +1000933 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +1000934
Dave Airlief64122c2013-02-25 14:47:55 +1000935 qdev->monitors_config = qdev->monitors_config_bo->kptr;
936 qdev->ram_header->monitors_config =
937 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
938
939 memset(qdev->monitors_config, 0, monitors_config_size);
940 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +1000941 return 0;
942}
943
944int qxl_destroy_monitors_object(struct qxl_device *qdev)
945{
946 int ret;
947
948 qdev->monitors_config = NULL;
949 qdev->ram_header->monitors_config = 0;
950
951 qxl_bo_kunmap(qdev->monitors_config_bo);
952 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
953 if (ret)
954 return ret;
955
956 qxl_bo_unpin(qdev->monitors_config_bo);
957 qxl_bo_unreserve(qdev->monitors_config_bo);
958
959 qxl_bo_unref(&qdev->monitors_config_bo);
960 return 0;
961}
962
963int qxl_modeset_init(struct qxl_device *qdev)
964{
965 int i;
966 int ret;
967
968 drm_mode_config_init(qdev->ddev);
969
970 ret = qxl_create_monitors_object(qdev);
971 if (ret)
972 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +1000973
974 qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
975
976 /* modes will be validated against the framebuffer size */
977 qdev->ddev->mode_config.min_width = 320;
978 qdev->ddev->mode_config.min_height = 200;
979 qdev->ddev->mode_config.max_width = 8192;
980 qdev->ddev->mode_config.max_height = 8192;
981
982 qdev->ddev->mode_config.fb_base = qdev->vram_base;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100983 for (i = 0 ; i < qxl_num_crtc; ++i) {
Dave Airlief64122c2013-02-25 14:47:55 +1000984 qdev_crtc_init(qdev->ddev, i);
985 qdev_output_init(qdev->ddev, i);
986 }
987
988 qdev->mode_info.mode_config_initialized = true;
989
990 /* primary surface must be created by this point, to allow
991 * issuing command queue commands and having them read by
992 * spice server. */
993 qxl_fbdev_init(qdev);
994 return 0;
995}
996
997void qxl_modeset_fini(struct qxl_device *qdev)
998{
999 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001000
1001 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001002 if (qdev->mode_info.mode_config_initialized) {
1003 drm_mode_config_cleanup(qdev->ddev);
1004 qdev->mode_info.mode_config_initialized = false;
1005 }
1006}