blob: b8cf556b431c11d31d86074d00f698d90433d166 [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
Randy Dunlapc5416d662013-12-20 10:58:15 -080027#include <linux/crc32.h>
Dave Airlief64122c2013-02-25 14:47:55 +100028
29#include "qxl_drv.h"
30#include "qxl_object.h"
31#include "drm_crtc_helper.h"
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010032#include <drm/drm_plane_helper.h>
Dave Airlief64122c2013-02-25 14:47:55 +100033
Dave Airlie07f8d9b2013-07-02 06:37:13 +010034static bool qxl_head_enabled(struct qxl_head *head)
35{
36 return head->width && head->height;
37}
38
Dave Airlief64122c2013-02-25 14:47:55 +100039void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
40{
41 if (qdev->client_monitors_config &&
42 count > qdev->client_monitors_config->count) {
43 kfree(qdev->client_monitors_config);
Dave Airlie62c8ba72013-04-16 13:36:00 +100044 qdev->client_monitors_config = NULL;
Dave Airlief64122c2013-02-25 14:47:55 +100045 }
46 if (!qdev->client_monitors_config) {
47 qdev->client_monitors_config = kzalloc(
48 sizeof(struct qxl_monitors_config) +
49 sizeof(struct qxl_head) * count, GFP_KERNEL);
50 if (!qdev->client_monitors_config) {
51 qxl_io_log(qdev,
52 "%s: allocation failure for %u heads\n",
53 __func__, count);
54 return;
55 }
56 }
57 qdev->client_monitors_config->count = count;
58}
59
60static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
61{
62 int i;
63 int num_monitors;
64 uint32_t crc;
65
Dave Airlief64122c2013-02-25 14:47:55 +100066 num_monitors = qdev->rom->client_monitors_config.count;
67 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
68 sizeof(qdev->rom->client_monitors_config));
69 if (crc != qdev->rom->client_monitors_config_crc) {
70 qxl_io_log(qdev, "crc mismatch: have %X (%d) != %X\n", crc,
71 sizeof(qdev->rom->client_monitors_config),
72 qdev->rom->client_monitors_config_crc);
73 return 1;
74 }
75 if (num_monitors > qdev->monitors_config->max_allowed) {
Dave Airlie5b8788c2013-07-01 14:14:38 +100076 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
77 qdev->monitors_config->max_allowed, num_monitors);
Dave Airlief64122c2013-02-25 14:47:55 +100078 num_monitors = qdev->monitors_config->max_allowed;
79 } else {
80 num_monitors = qdev->rom->client_monitors_config.count;
81 }
82 qxl_alloc_client_monitors_config(qdev, num_monitors);
83 /* we copy max from the client but it isn't used */
84 qdev->client_monitors_config->max_allowed =
85 qdev->monitors_config->max_allowed;
86 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
87 struct qxl_urect *c_rect =
88 &qdev->rom->client_monitors_config.heads[i];
89 struct qxl_head *client_head =
90 &qdev->client_monitors_config->heads[i];
Dave Airlie07f8d9b2013-07-02 06:37:13 +010091 client_head->x = c_rect->left;
92 client_head->y = c_rect->top;
93 client_head->width = c_rect->right - c_rect->left;
94 client_head->height = c_rect->bottom - c_rect->top;
95 client_head->surface_id = 0;
96 client_head->id = i;
97 client_head->flags = 0;
98 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
99 client_head->x, client_head->y);
Dave Airlief64122c2013-02-25 14:47:55 +1000100 }
101 return 0;
102}
103
Dave Airlie7dea0942014-10-28 11:28:44 +1000104static void qxl_update_offset_props(struct qxl_device *qdev)
105{
106 struct drm_device *dev = qdev->ddev;
107 struct drm_connector *connector;
108 struct qxl_output *output;
109 struct qxl_head *head;
110
111 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
112 output = drm_connector_to_qxl_output(connector);
113
114 head = &qdev->client_monitors_config->heads[output->index];
115
116 drm_object_property_set_value(&connector->base,
117 dev->mode_config.suggested_x_property, head->x);
118 drm_object_property_set_value(&connector->base,
119 dev->mode_config.suggested_y_property, head->y);
120 }
121}
122
Dave Airlief64122c2013-02-25 14:47:55 +1000123void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
124{
125
Dave Airlie7dea0942014-10-28 11:28:44 +1000126 struct drm_device *dev = qdev->ddev;
Dave Airlief64122c2013-02-25 14:47:55 +1000127 while (qxl_display_copy_rom_client_monitors_config(qdev)) {
128 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
129 " retrying\n");
130 }
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200131
Dave Airlie7dea0942014-10-28 11:28:44 +1000132 drm_modeset_lock_all(dev);
133 qxl_update_offset_props(qdev);
134 drm_modeset_unlock_all(dev);
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200135 if (!drm_helper_hpd_irq_event(qdev->ddev)) {
136 /* notify that the monitor configuration changed, to
137 adjust at the arbitrary resolution */
138 drm_kms_helper_hotplug_event(qdev->ddev);
139 }
Dave Airlief64122c2013-02-25 14:47:55 +1000140}
141
Marc-André Lureaub0807422013-10-18 16:11:31 +0200142static int qxl_add_monitors_config_modes(struct drm_connector *connector,
143 unsigned *pwidth,
144 unsigned *pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000145{
146 struct drm_device *dev = connector->dev;
147 struct qxl_device *qdev = dev->dev_private;
148 struct qxl_output *output = drm_connector_to_qxl_output(connector);
149 int h = output->index;
150 struct drm_display_mode *mode = NULL;
151 struct qxl_head *head;
152
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100153 if (!qdev->client_monitors_config)
Dave Airlief64122c2013-02-25 14:47:55 +1000154 return 0;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100155 head = &qdev->client_monitors_config->heads[h];
Dave Airlief64122c2013-02-25 14:47:55 +1000156
157 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
158 false);
159 mode->type |= DRM_MODE_TYPE_PREFERRED;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200160 *pwidth = head->width;
161 *pheight = head->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000162 drm_mode_probed_add(connector, mode);
163 return 1;
164}
165
Marc-André Lureaub0807422013-10-18 16:11:31 +0200166static int qxl_add_common_modes(struct drm_connector *connector,
167 unsigned pwidth,
168 unsigned pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000169{
170 struct drm_device *dev = connector->dev;
171 struct drm_display_mode *mode = NULL;
172 int i;
173 struct mode_size {
174 int w;
175 int h;
176 } common_modes[] = {
177 { 640, 480},
178 { 720, 480},
179 { 800, 600},
180 { 848, 480},
181 {1024, 768},
182 {1152, 768},
183 {1280, 720},
184 {1280, 800},
185 {1280, 854},
186 {1280, 960},
187 {1280, 1024},
188 {1440, 900},
189 {1400, 1050},
190 {1680, 1050},
191 {1600, 1200},
192 {1920, 1080},
193 {1920, 1200}
194 };
195
196 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
Dave Airlief64122c2013-02-25 14:47:55 +1000197 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
198 60, false, false, false);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200199 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000200 mode->type |= DRM_MODE_TYPE_PREFERRED;
201 drm_mode_probed_add(connector, mode);
202 }
203 return i - 1;
204}
205
Dave Airlief64122c2013-02-25 14:47:55 +1000206static void qxl_crtc_destroy(struct drm_crtc *crtc)
207{
208 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
209
210 drm_crtc_cleanup(crtc);
211 kfree(qxl_crtc);
212}
213
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200214static int qxl_crtc_page_flip(struct drm_crtc *crtc,
215 struct drm_framebuffer *fb,
216 struct drm_pending_vblank_event *event,
217 uint32_t page_flip_flags)
218{
219 struct drm_device *dev = crtc->dev;
220 struct qxl_device *qdev = dev->dev_private;
221 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
222 struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
223 struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
224 struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
225 struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
226 unsigned long flags;
227 struct drm_clip_rect norect = {
228 .x1 = 0,
229 .y1 = 0,
230 .x2 = fb->width,
231 .y2 = fb->height
232 };
233 int inc = 1;
234 int one_clip_rect = 1;
235 int ret = 0;
236
237 crtc->primary->fb = fb;
238 bo_old->is_primary = false;
239 bo->is_primary = true;
240
241 ret = qxl_bo_reserve(bo, false);
242 if (ret)
243 return ret;
244
245 qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
246 &norect, one_clip_rect, inc);
247
248 drm_vblank_get(dev, qcrtc->index);
249
250 if (event) {
251 spin_lock_irqsave(&dev->event_lock, flags);
252 drm_send_vblank_event(dev, qcrtc->index, event);
253 spin_unlock_irqrestore(&dev->event_lock, flags);
254 }
255 drm_vblank_put(dev, qcrtc->index);
256
257 qxl_bo_unreserve(bo);
258
259 return 0;
260}
261
Dave Airlie8002db62013-07-23 14:16:42 +1000262static int
Dave Airlief64122c2013-02-25 14:47:55 +1000263qxl_hide_cursor(struct qxl_device *qdev)
264{
265 struct qxl_release *release;
266 struct qxl_cursor_cmd *cmd;
267 int ret;
268
269 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
270 &release, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000271 if (ret)
272 return ret;
273
274 ret = qxl_release_reserve_list(release, true);
275 if (ret) {
276 qxl_release_free(qdev, release);
277 return ret;
278 }
Dave Airlief64122c2013-02-25 14:47:55 +1000279
280 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
281 cmd->type = QXL_CURSOR_HIDE;
282 qxl_release_unmap(qdev, release, &cmd->release_info);
283
Dave Airlief64122c2013-02-25 14:47:55 +1000284 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000285 qxl_release_fence_buffer_objects(release);
286 return 0;
Dave Airlief64122c2013-02-25 14:47:55 +1000287}
288
Dave Airliec0a60802013-06-20 11:48:53 +1000289static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
290 struct drm_file *file_priv,
291 uint32_t handle,
292 uint32_t width,
293 uint32_t height, int32_t hot_x, int32_t hot_y)
Dave Airlief64122c2013-02-25 14:47:55 +1000294{
295 struct drm_device *dev = crtc->dev;
296 struct qxl_device *qdev = dev->dev_private;
297 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
298 struct drm_gem_object *obj;
299 struct qxl_cursor *cursor;
300 struct qxl_cursor_cmd *cmd;
301 struct qxl_bo *cursor_bo, *user_bo;
302 struct qxl_release *release;
303 void *user_ptr;
304
305 int size = 64*64*4;
306 int ret = 0;
Dave Airlie8002db62013-07-23 14:16:42 +1000307 if (!handle)
308 return qxl_hide_cursor(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +1000309
310 obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
311 if (!obj) {
312 DRM_ERROR("cannot find cursor object\n");
313 return -ENOENT;
314 }
315
316 user_bo = gem_to_qxl_bo(obj);
317
318 ret = qxl_bo_reserve(user_bo, false);
319 if (ret)
320 goto out_unref;
321
322 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000323 qxl_bo_unreserve(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000324 if (ret)
Dave Airlie8002db62013-07-23 14:16:42 +1000325 goto out_unref;
Dave Airlief64122c2013-02-25 14:47:55 +1000326
327 ret = qxl_bo_kmap(user_bo, &user_ptr);
328 if (ret)
329 goto out_unpin;
330
331 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
332 QXL_RELEASE_CURSOR_CMD,
333 &release, NULL);
334 if (ret)
335 goto out_kunmap;
Dave Airlie8002db62013-07-23 14:16:42 +1000336
337 ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size,
338 &cursor_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000339 if (ret)
340 goto out_free_release;
Dave Airlie8002db62013-07-23 14:16:42 +1000341
342 ret = qxl_release_reserve_list(release, false);
Dave Airlief64122c2013-02-25 14:47:55 +1000343 if (ret)
344 goto out_free_bo;
345
Dave Airlie8002db62013-07-23 14:16:42 +1000346 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
347 if (ret)
348 goto out_backoff;
349
Dave Airlief64122c2013-02-25 14:47:55 +1000350 cursor->header.unique = 0;
351 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
352 cursor->header.width = 64;
353 cursor->header.height = 64;
Dave Airliec0a60802013-06-20 11:48:53 +1000354 cursor->header.hot_spot_x = hot_x;
355 cursor->header.hot_spot_y = hot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000356 cursor->data_size = size;
357 cursor->chunk.next_chunk = 0;
358 cursor->chunk.prev_chunk = 0;
359 cursor->chunk.data_size = size;
360
361 memcpy(cursor->chunk.data, user_ptr, size);
362
363 qxl_bo_kunmap(cursor_bo);
364
Dave Airlief64122c2013-02-25 14:47:55 +1000365 qxl_bo_kunmap(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000366
367 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
368 cmd->type = QXL_CURSOR_SET;
369 cmd->u.set.position.x = qcrtc->cur_x;
370 cmd->u.set.position.y = qcrtc->cur_y;
371
372 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
Dave Airlief64122c2013-02-25 14:47:55 +1000373
374 cmd->u.set.visible = 1;
375 qxl_release_unmap(qdev, release, &cmd->release_info);
376
Dave Airlief64122c2013-02-25 14:47:55 +1000377 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000378 qxl_release_fence_buffer_objects(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000379
Dave Airlie8002db62013-07-23 14:16:42 +1000380 /* finish with the userspace bo */
381 ret = qxl_bo_reserve(user_bo, false);
382 if (!ret) {
383 qxl_bo_unpin(user_bo);
384 qxl_bo_unreserve(user_bo);
385 }
386 drm_gem_object_unreference_unlocked(obj);
387
Dave Airlief64122c2013-02-25 14:47:55 +1000388 qxl_bo_unref(&cursor_bo);
389
390 return ret;
Dave Airlie8002db62013-07-23 14:16:42 +1000391
392out_backoff:
393 qxl_release_backoff_reserve_list(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000394out_free_bo:
395 qxl_bo_unref(&cursor_bo);
396out_free_release:
Dave Airlief64122c2013-02-25 14:47:55 +1000397 qxl_release_free(qdev, release);
398out_kunmap:
399 qxl_bo_kunmap(user_bo);
400out_unpin:
401 qxl_bo_unpin(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000402out_unref:
403 drm_gem_object_unreference_unlocked(obj);
404 return ret;
405}
406
407static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
408 int x, int y)
409{
410 struct drm_device *dev = crtc->dev;
411 struct qxl_device *qdev = dev->dev_private;
412 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
413 struct qxl_release *release;
414 struct qxl_cursor_cmd *cmd;
415 int ret;
416
417 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
418 &release, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000419 if (ret)
420 return ret;
421
422 ret = qxl_release_reserve_list(release, true);
423 if (ret) {
424 qxl_release_free(qdev, release);
425 return ret;
426 }
Dave Airlief64122c2013-02-25 14:47:55 +1000427
428 qcrtc->cur_x = x;
429 qcrtc->cur_y = y;
430
431 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
432 cmd->type = QXL_CURSOR_MOVE;
433 cmd->u.position.x = qcrtc->cur_x;
434 cmd->u.position.y = qcrtc->cur_y;
435 qxl_release_unmap(qdev, release, &cmd->release_info);
436
Dave Airlief64122c2013-02-25 14:47:55 +1000437 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000438 qxl_release_fence_buffer_objects(release);
439
Dave Airlief64122c2013-02-25 14:47:55 +1000440 return 0;
441}
442
443
444static const struct drm_crtc_funcs qxl_crtc_funcs = {
Dave Airliec0a60802013-06-20 11:48:53 +1000445 .cursor_set2 = qxl_crtc_cursor_set2,
Dave Airlief64122c2013-02-25 14:47:55 +1000446 .cursor_move = qxl_crtc_cursor_move,
Dave Airlief64122c2013-02-25 14:47:55 +1000447 .set_config = drm_crtc_helper_set_config,
448 .destroy = qxl_crtc_destroy,
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200449 .page_flip = qxl_crtc_page_flip,
Dave Airlief64122c2013-02-25 14:47:55 +1000450};
451
452static void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
453{
454 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
455
456 if (qxl_fb->obj)
457 drm_gem_object_unreference_unlocked(qxl_fb->obj);
458 drm_framebuffer_cleanup(fb);
459 kfree(qxl_fb);
460}
461
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000462static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
463 struct drm_file *file_priv,
464 unsigned flags, unsigned color,
465 struct drm_clip_rect *clips,
466 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000467{
468 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
469 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
470 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
471 struct drm_clip_rect norect;
472 struct qxl_bo *qobj;
473 int inc = 1;
474
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200475 drm_modeset_lock_all(fb->dev);
476
Dave Airlief64122c2013-02-25 14:47:55 +1000477 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000478 /* if we aren't primary surface ignore this */
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200479 if (!qobj->is_primary) {
480 drm_modeset_unlock_all(fb->dev);
Dave Airlieb2b44652013-05-13 12:48:40 +1000481 return 0;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200482 }
Dave Airlieb2b44652013-05-13 12:48:40 +1000483
Dave Airlief64122c2013-02-25 14:47:55 +1000484 if (!num_clips) {
485 num_clips = 1;
486 clips = &norect;
487 norect.x1 = norect.y1 = 0;
488 norect.x2 = fb->width;
489 norect.y2 = fb->height;
490 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
491 num_clips /= 2;
492 inc = 2; /* skip source rects */
493 }
494
495 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
496 clips, num_clips, inc);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200497
498 drm_modeset_unlock_all(fb->dev);
499
Dave Airlief64122c2013-02-25 14:47:55 +1000500 return 0;
501}
502
503static const struct drm_framebuffer_funcs qxl_fb_funcs = {
504 .destroy = qxl_user_framebuffer_destroy,
505 .dirty = qxl_framebuffer_surface_dirty,
506/* TODO?
507 * .create_handle = qxl_user_framebuffer_create_handle, */
508};
509
510int
511qxl_framebuffer_init(struct drm_device *dev,
512 struct qxl_framebuffer *qfb,
513 struct drm_mode_fb_cmd2 *mode_cmd,
514 struct drm_gem_object *obj)
515{
516 int ret;
517
518 qfb->obj = obj;
519 ret = drm_framebuffer_init(dev, &qfb->base, &qxl_fb_funcs);
520 if (ret) {
521 qfb->obj = NULL;
522 return ret;
523 }
524 drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
525 return 0;
526}
527
528static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
529{
530}
531
532static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
533 const struct drm_display_mode *mode,
534 struct drm_display_mode *adjusted_mode)
535{
536 struct drm_device *dev = crtc->dev;
537 struct qxl_device *qdev = dev->dev_private;
538
539 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
540 __func__,
541 mode->hdisplay, mode->vdisplay,
542 adjusted_mode->hdisplay,
543 adjusted_mode->vdisplay);
544 return true;
545}
546
547void
548qxl_send_monitors_config(struct qxl_device *qdev)
549{
550 int i;
551
552 BUG_ON(!qdev->ram_header->monitors_config);
553
554 if (qdev->monitors_config->count == 0) {
555 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
556 return;
557 }
558 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
559 struct qxl_head *head = &qdev->monitors_config->heads[i];
560
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100561 if (head->y > 8192 || head->x > 8192 ||
Dave Airlief64122c2013-02-25 14:47:55 +1000562 head->width > 8192 || head->height > 8192) {
563 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
564 i, head->width, head->height,
565 head->x, head->y);
566 return;
567 }
568 }
569 qxl_io_monitors_config(qdev);
570}
571
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100572static void qxl_monitors_config_set(struct qxl_device *qdev,
573 int index,
574 unsigned x, unsigned y,
575 unsigned width, unsigned height,
576 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000577{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100578 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
579 qdev->monitors_config->heads[index].x = x;
580 qdev->monitors_config->heads[index].y = y;
581 qdev->monitors_config->heads[index].width = width;
582 qdev->monitors_config->heads[index].height = height;
583 qdev->monitors_config->heads[index].surface_id = surf_id;
584
Dave Airlief64122c2013-02-25 14:47:55 +1000585}
586
587static int qxl_crtc_mode_set(struct drm_crtc *crtc,
588 struct drm_display_mode *mode,
589 struct drm_display_mode *adjusted_mode,
590 int x, int y,
591 struct drm_framebuffer *old_fb)
592{
593 struct drm_device *dev = crtc->dev;
594 struct qxl_device *qdev = dev->dev_private;
595 struct qxl_mode *m = (void *)mode->private;
596 struct qxl_framebuffer *qfb;
597 struct qxl_bo *bo, *old_bo = NULL;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100598 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Dave Airlief64122c2013-02-25 14:47:55 +1000599 bool recreate_primary = false;
600 int ret;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100601 int surf_id;
Matt Roperf4510a22014-04-01 15:22:40 -0700602 if (!crtc->primary->fb) {
Dave Airlief64122c2013-02-25 14:47:55 +1000603 DRM_DEBUG_KMS("No FB bound\n");
604 return 0;
605 }
606
607 if (old_fb) {
608 qfb = to_qxl_framebuffer(old_fb);
609 old_bo = gem_to_qxl_bo(qfb->obj);
610 }
Matt Roperf4510a22014-04-01 15:22:40 -0700611 qfb = to_qxl_framebuffer(crtc->primary->fb);
Dave Airlief64122c2013-02-25 14:47:55 +1000612 bo = gem_to_qxl_bo(qfb->obj);
613 if (!m)
614 /* and do we care? */
615 DRM_DEBUG("%dx%d: not a native mode\n", x, y);
616 else
617 DRM_DEBUG("%dx%d: qxl id %d\n",
618 mode->hdisplay, mode->vdisplay, m->id);
619 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
620 x, y,
621 mode->hdisplay, mode->vdisplay,
622 adjusted_mode->hdisplay,
623 adjusted_mode->vdisplay);
624
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100625 if (qcrtc->index == 0)
626 recreate_primary = true;
Dave Airlief64122c2013-02-25 14:47:55 +1000627
Marc-André Lureauc572aaf2014-10-16 11:39:44 +0200628 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
629 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
630 return -EINVAL;
631 }
Dave Airlief64122c2013-02-25 14:47:55 +1000632
633 ret = qxl_bo_reserve(bo, false);
634 if (ret != 0)
635 return ret;
636 ret = qxl_bo_pin(bo, bo->type, NULL);
637 if (ret != 0) {
638 qxl_bo_unreserve(bo);
639 return -EINVAL;
640 }
641 qxl_bo_unreserve(bo);
642 if (recreate_primary) {
643 qxl_io_destroy_primary(qdev);
644 qxl_io_log(qdev,
Marc-André Lureauc572aaf2014-10-16 11:39:44 +0200645 "recreate primary: %dx%d,%d,%d\n",
646 bo->surf.width, bo->surf.height,
647 bo->surf.stride, bo->surf.format);
648 qxl_io_create_primary(qdev, 0, bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000649 bo->is_primary = true;
David Mansfield52571ad2014-06-04 12:12:15 +1000650 }
651
652 if (bo->is_primary) {
653 DRM_DEBUG_KMS("setting surface_id to 0 for primary surface %d on crtc %d\n", bo->surface_id, qcrtc->index);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100654 surf_id = 0;
655 } else {
656 surf_id = bo->surface_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000657 }
658
659 if (old_bo && old_bo != bo) {
660 old_bo->is_primary = false;
661 ret = qxl_bo_reserve(old_bo, false);
662 qxl_bo_unpin(old_bo);
663 qxl_bo_unreserve(old_bo);
664 }
665
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100666 qxl_monitors_config_set(qdev, qcrtc->index, x, y,
667 mode->hdisplay,
668 mode->vdisplay, surf_id);
Dave Airlief64122c2013-02-25 14:47:55 +1000669 return 0;
670}
671
672static void qxl_crtc_prepare(struct drm_crtc *crtc)
673{
674 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
675 crtc->mode.hdisplay, crtc->mode.vdisplay,
676 crtc->x, crtc->y, crtc->enabled);
677}
678
679static void qxl_crtc_commit(struct drm_crtc *crtc)
680{
681 DRM_DEBUG("\n");
682}
683
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100684static void qxl_crtc_disable(struct drm_crtc *crtc)
685{
686 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
687 struct drm_device *dev = crtc->dev;
688 struct qxl_device *qdev = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700689 if (crtc->primary->fb) {
690 struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100691 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
692 int ret;
693 ret = qxl_bo_reserve(bo, false);
694 qxl_bo_unpin(bo);
695 qxl_bo_unreserve(bo);
Matt Roperf4510a22014-04-01 15:22:40 -0700696 crtc->primary->fb = NULL;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100697 }
698
699 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
700
701 qxl_send_monitors_config(qdev);
702}
703
Dave Airlief64122c2013-02-25 14:47:55 +1000704static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
705 .dpms = qxl_crtc_dpms,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100706 .disable = qxl_crtc_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000707 .mode_fixup = qxl_crtc_mode_fixup,
708 .mode_set = qxl_crtc_mode_set,
709 .prepare = qxl_crtc_prepare,
710 .commit = qxl_crtc_commit,
Dave Airlief64122c2013-02-25 14:47:55 +1000711};
712
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100713static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000714{
715 struct qxl_crtc *qxl_crtc;
716
717 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
718 if (!qxl_crtc)
719 return -ENOMEM;
720
721 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100722 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000723 drm_mode_crtc_set_gamma_size(&qxl_crtc->base, 256);
724 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
725 return 0;
726}
727
728static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
729{
730 DRM_DEBUG("\n");
731}
732
733static bool qxl_enc_mode_fixup(struct drm_encoder *encoder,
734 const struct drm_display_mode *mode,
735 struct drm_display_mode *adjusted_mode)
736{
737 DRM_DEBUG("\n");
738 return true;
739}
740
741static void qxl_enc_prepare(struct drm_encoder *encoder)
742{
743 DRM_DEBUG("\n");
744}
745
746static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
747 struct drm_encoder *encoder)
748{
749 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100750 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000751 struct qxl_head *head;
752 struct drm_display_mode *mode;
753
754 BUG_ON(!encoder);
755 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100756 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000757 if (!qdev->monitors_config ||
758 qdev->monitors_config->max_allowed <= i) {
759 DRM_ERROR(
760 "head number too large or missing monitors config: %p, %d",
761 qdev->monitors_config,
762 qdev->monitors_config ?
763 qdev->monitors_config->max_allowed : -1);
764 return;
765 }
766 if (!encoder->crtc) {
767 DRM_ERROR("missing crtc on encoder %p\n", encoder);
768 return;
769 }
770 if (i != 0)
771 DRM_DEBUG("missing for multiple monitors: no head holes\n");
772 head = &qdev->monitors_config->heads[i];
773 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000774 if (encoder->crtc->enabled) {
775 mode = &encoder->crtc->mode;
776 head->width = mode->hdisplay;
777 head->height = mode->vdisplay;
778 head->x = encoder->crtc->x;
779 head->y = encoder->crtc->y;
780 if (qdev->monitors_config->count < i + 1)
781 qdev->monitors_config->count = i + 1;
782 } else {
783 head->width = 0;
784 head->height = 0;
785 head->x = 0;
786 head->y = 0;
787 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100788 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
789 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000790 head->flags = 0;
791 /* TODO - somewhere else to call this for multiple monitors
792 * (config_commit?) */
793 qxl_send_monitors_config(qdev);
794}
795
796static void qxl_enc_commit(struct drm_encoder *encoder)
797{
798 struct qxl_device *qdev = encoder->dev->dev_private;
799
800 qxl_write_monitors_config_for_encoder(qdev, encoder);
801 DRM_DEBUG("\n");
802}
803
804static void qxl_enc_mode_set(struct drm_encoder *encoder,
805 struct drm_display_mode *mode,
806 struct drm_display_mode *adjusted_mode)
807{
808 DRM_DEBUG("\n");
809}
810
811static int qxl_conn_get_modes(struct drm_connector *connector)
812{
813 int ret = 0;
814 struct qxl_device *qdev = connector->dev->dev_private;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200815 unsigned pwidth = 1024;
816 unsigned pheight = 768;
Dave Airlief64122c2013-02-25 14:47:55 +1000817
818 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
819 /* TODO: what should we do here? only show the configured modes for the
820 * device, or allow the full list, or both? */
821 if (qdev->monitors_config && qdev->monitors_config->count) {
Marc-André Lureaub0807422013-10-18 16:11:31 +0200822 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000823 if (ret < 0)
824 return ret;
825 }
Marc-André Lureaub0807422013-10-18 16:11:31 +0200826 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000827 return ret;
828}
829
830static int qxl_conn_mode_valid(struct drm_connector *connector,
831 struct drm_display_mode *mode)
832{
833 /* TODO: is this called for user defined modes? (xrandr --add-mode)
834 * TODO: check that the mode fits in the framebuffer */
835 DRM_DEBUG("%s: %dx%d status=%d\n", mode->name, mode->hdisplay,
836 mode->vdisplay, mode->status);
837 return MODE_OK;
838}
839
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000840static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +1000841{
842 struct qxl_output *qxl_output =
843 drm_connector_to_qxl_output(connector);
844
845 DRM_DEBUG("\n");
846 return &qxl_output->enc;
847}
848
849
850static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
851 .dpms = qxl_enc_dpms,
852 .mode_fixup = qxl_enc_mode_fixup,
853 .prepare = qxl_enc_prepare,
854 .mode_set = qxl_enc_mode_set,
855 .commit = qxl_enc_commit,
856};
857
858static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
859 .get_modes = qxl_conn_get_modes,
860 .mode_valid = qxl_conn_mode_valid,
861 .best_encoder = qxl_best_encoder,
862};
863
864static void qxl_conn_save(struct drm_connector *connector)
865{
866 DRM_DEBUG("\n");
867}
868
869static void qxl_conn_restore(struct drm_connector *connector)
870{
871 DRM_DEBUG("\n");
872}
873
874static enum drm_connector_status qxl_conn_detect(
875 struct drm_connector *connector,
876 bool force)
877{
878 struct qxl_output *output =
879 drm_connector_to_qxl_output(connector);
880 struct drm_device *ddev = connector->dev;
881 struct qxl_device *qdev = ddev->dev_private;
882 int connected;
883
884 /* The first monitor is always connected */
885 connected = (output->index == 0) ||
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100886 (qdev->client_monitors_config &&
887 qdev->client_monitors_config->count > output->index &&
888 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]));
Dave Airlief64122c2013-02-25 14:47:55 +1000889
Marc-André Lureau5cab51c2013-10-18 16:11:33 +0200890 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
891 if (!connected)
892 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
893
Dave Airlief64122c2013-02-25 14:47:55 +1000894 return connected ? connector_status_connected
895 : connector_status_disconnected;
896}
897
898static int qxl_conn_set_property(struct drm_connector *connector,
899 struct drm_property *property,
900 uint64_t value)
901{
902 DRM_DEBUG("\n");
903 return 0;
904}
905
906static void qxl_conn_destroy(struct drm_connector *connector)
907{
908 struct qxl_output *qxl_output =
909 drm_connector_to_qxl_output(connector);
910
Thomas Wood34ea3d32014-05-29 16:57:41 +0100911 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +1000912 drm_connector_cleanup(connector);
913 kfree(qxl_output);
914}
915
916static const struct drm_connector_funcs qxl_connector_funcs = {
917 .dpms = drm_helper_connector_dpms,
918 .save = qxl_conn_save,
919 .restore = qxl_conn_restore,
920 .detect = qxl_conn_detect,
Dave Airlieb87577b2014-05-01 09:26:53 +1000921 .fill_modes = drm_helper_probe_single_connector_modes_nomerge,
Dave Airlief64122c2013-02-25 14:47:55 +1000922 .set_property = qxl_conn_set_property,
923 .destroy = qxl_conn_destroy,
924};
925
926static void qxl_enc_destroy(struct drm_encoder *encoder)
927{
928 drm_encoder_cleanup(encoder);
929}
930
931static const struct drm_encoder_funcs qxl_enc_funcs = {
932 .destroy = qxl_enc_destroy,
933};
934
Dave Airlie4695b032013-10-11 11:05:00 +1000935static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
936{
937 if (qdev->hotplug_mode_update_property)
938 return 0;
939
940 qdev->hotplug_mode_update_property =
941 drm_property_create_range(qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
942 "hotplug_mode_update", 0, 1);
943
944 return 0;
945}
946
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000947static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +1000948{
Dave Airlie4695b032013-10-11 11:05:00 +1000949 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +1000950 struct qxl_output *qxl_output;
951 struct drm_connector *connector;
952 struct drm_encoder *encoder;
953
954 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
955 if (!qxl_output)
956 return -ENOMEM;
957
958 qxl_output->index = num_output;
959
960 connector = &qxl_output->base;
961 encoder = &qxl_output->enc;
962 drm_connector_init(dev, &qxl_output->base,
963 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
964
965 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
966 DRM_MODE_ENCODER_VIRTUAL);
967
Dave Airlie5ff91e42013-07-05 10:20:33 +1000968 /* we get HPD via client monitors config */
969 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +1000970 encoder->possible_crtcs = 1 << num_output;
971 drm_mode_connector_attach_encoder(&qxl_output->base,
972 &qxl_output->enc);
973 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
974 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
975
Dave Airlie4695b032013-10-11 11:05:00 +1000976 drm_object_attach_property(&connector->base,
977 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +1000978 drm_object_attach_property(&connector->base,
979 dev->mode_config.suggested_x_property, 0);
980 drm_object_attach_property(&connector->base,
981 dev->mode_config.suggested_y_property, 0);
Thomas Wood34ea3d32014-05-29 16:57:41 +0100982 drm_connector_register(connector);
Dave Airlief64122c2013-02-25 14:47:55 +1000983 return 0;
984}
985
986static struct drm_framebuffer *
987qxl_user_framebuffer_create(struct drm_device *dev,
988 struct drm_file *file_priv,
989 struct drm_mode_fb_cmd2 *mode_cmd)
990{
991 struct drm_gem_object *obj;
992 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +1000993 int ret;
994
995 obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
996
997 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
998 if (qxl_fb == NULL)
999 return NULL;
1000
1001 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj);
1002 if (ret) {
1003 kfree(qxl_fb);
1004 drm_gem_object_unreference_unlocked(obj);
1005 return NULL;
1006 }
1007
Dave Airlief64122c2013-02-25 14:47:55 +10001008 return &qxl_fb->base;
1009}
1010
1011static const struct drm_mode_config_funcs qxl_mode_funcs = {
1012 .fb_create = qxl_user_framebuffer_create,
1013};
1014
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001015int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001016{
Dave Airlief64122c2013-02-25 14:47:55 +10001017 int ret;
1018 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001019 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001020 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001021 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001022
Dave Airlief64122c2013-02-25 14:47:55 +10001023 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1024 QXL_GEM_DOMAIN_VRAM,
1025 false, false, NULL, &gobj);
1026 if (ret) {
1027 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1028 return -ENOMEM;
1029 }
1030 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001031
1032 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1033 if (ret)
1034 return ret;
1035
1036 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
1037 if (ret) {
1038 qxl_bo_unreserve(qdev->monitors_config_bo);
1039 return ret;
1040 }
1041
1042 qxl_bo_unreserve(qdev->monitors_config_bo);
1043
Dave Airlief64122c2013-02-25 14:47:55 +10001044 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001045
Dave Airlief64122c2013-02-25 14:47:55 +10001046 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1047 qdev->ram_header->monitors_config =
1048 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1049
1050 memset(qdev->monitors_config, 0, monitors_config_size);
1051 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001052 return 0;
1053}
1054
1055int qxl_destroy_monitors_object(struct qxl_device *qdev)
1056{
1057 int ret;
1058
1059 qdev->monitors_config = NULL;
1060 qdev->ram_header->monitors_config = 0;
1061
1062 qxl_bo_kunmap(qdev->monitors_config_bo);
1063 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1064 if (ret)
1065 return ret;
1066
1067 qxl_bo_unpin(qdev->monitors_config_bo);
1068 qxl_bo_unreserve(qdev->monitors_config_bo);
1069
1070 qxl_bo_unref(&qdev->monitors_config_bo);
1071 return 0;
1072}
1073
1074int qxl_modeset_init(struct qxl_device *qdev)
1075{
1076 int i;
1077 int ret;
1078
1079 drm_mode_config_init(qdev->ddev);
1080
1081 ret = qxl_create_monitors_object(qdev);
1082 if (ret)
1083 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001084
1085 qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
1086
1087 /* modes will be validated against the framebuffer size */
1088 qdev->ddev->mode_config.min_width = 320;
1089 qdev->ddev->mode_config.min_height = 200;
1090 qdev->ddev->mode_config.max_width = 8192;
1091 qdev->ddev->mode_config.max_height = 8192;
1092
1093 qdev->ddev->mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001094
Dave Airlie7dea0942014-10-28 11:28:44 +10001095 drm_mode_create_suggested_offset_properties(qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001096 qxl_mode_create_hotplug_mode_update_property(qdev);
1097
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001098 for (i = 0 ; i < qxl_num_crtc; ++i) {
Dave Airlief64122c2013-02-25 14:47:55 +10001099 qdev_crtc_init(qdev->ddev, i);
1100 qdev_output_init(qdev->ddev, i);
1101 }
1102
1103 qdev->mode_info.mode_config_initialized = true;
1104
1105 /* primary surface must be created by this point, to allow
1106 * issuing command queue commands and having them read by
1107 * spice server. */
1108 qxl_fbdev_init(qdev);
1109 return 0;
1110}
1111
1112void qxl_modeset_fini(struct qxl_device *qdev)
1113{
1114 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001115
1116 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001117 if (qdev->mode_info.mode_config_initialized) {
1118 drm_mode_config_cleanup(qdev->ddev);
1119 qdev->mode_info.mode_config_initialized = false;
1120 }
1121}