blob: eaea0d87cc9476d1210e7d1871edd254081178f9 [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
Christophe Fergeaue4a76442016-11-08 10:12:03 +010039static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
Dave Airlief64122c2013-02-25 14:47:55 +100040{
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
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010060enum {
61 MONITORS_CONFIG_MODIFIED,
62 MONITORS_CONFIG_UNCHANGED,
63 MONITORS_CONFIG_BAD_CRC,
64};
65
Dave Airlief64122c2013-02-25 14:47:55 +100066static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
67{
68 int i;
69 int num_monitors;
70 uint32_t crc;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010071 int status = MONITORS_CONFIG_UNCHANGED;
Dave Airlief64122c2013-02-25 14:47:55 +100072
Dave Airlief64122c2013-02-25 14:47:55 +100073 num_monitors = qdev->rom->client_monitors_config.count;
74 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
75 sizeof(qdev->rom->client_monitors_config));
76 if (crc != qdev->rom->client_monitors_config_crc) {
Frediano Ziglio72ec5652015-06-03 12:09:16 +010077 qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
Dave Airlief64122c2013-02-25 14:47:55 +100078 sizeof(qdev->rom->client_monitors_config),
79 qdev->rom->client_monitors_config_crc);
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010080 return MONITORS_CONFIG_BAD_CRC;
Dave Airlief64122c2013-02-25 14:47:55 +100081 }
82 if (num_monitors > qdev->monitors_config->max_allowed) {
Dave Airlie5b8788c2013-07-01 14:14:38 +100083 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
84 qdev->monitors_config->max_allowed, num_monitors);
Dave Airlief64122c2013-02-25 14:47:55 +100085 num_monitors = qdev->monitors_config->max_allowed;
86 } else {
87 num_monitors = qdev->rom->client_monitors_config.count;
88 }
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010089 if (qdev->client_monitors_config
90 && (num_monitors != qdev->client_monitors_config->count)) {
91 status = MONITORS_CONFIG_MODIFIED;
92 }
Dave Airlief64122c2013-02-25 14:47:55 +100093 qxl_alloc_client_monitors_config(qdev, num_monitors);
94 /* we copy max from the client but it isn't used */
95 qdev->client_monitors_config->max_allowed =
96 qdev->monitors_config->max_allowed;
97 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
98 struct qxl_urect *c_rect =
99 &qdev->rom->client_monitors_config.heads[i];
100 struct qxl_head *client_head =
101 &qdev->client_monitors_config->heads[i];
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100102 if (client_head->x != c_rect->left) {
103 client_head->x = c_rect->left;
104 status = MONITORS_CONFIG_MODIFIED;
105 }
106 if (client_head->y != c_rect->top) {
107 client_head->y = c_rect->top;
108 status = MONITORS_CONFIG_MODIFIED;
109 }
110 if (client_head->width != c_rect->right - c_rect->left) {
111 client_head->width = c_rect->right - c_rect->left;
112 status = MONITORS_CONFIG_MODIFIED;
113 }
114 if (client_head->height != c_rect->bottom - c_rect->top) {
115 client_head->height = c_rect->bottom - c_rect->top;
116 status = MONITORS_CONFIG_MODIFIED;
117 }
118 if (client_head->surface_id != 0) {
119 client_head->surface_id = 0;
120 status = MONITORS_CONFIG_MODIFIED;
121 }
122 if (client_head->id != i) {
123 client_head->id = i;
124 status = MONITORS_CONFIG_MODIFIED;
125 }
126 if (client_head->flags != 0) {
127 client_head->flags = 0;
128 status = MONITORS_CONFIG_MODIFIED;
129 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100130 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
131 client_head->x, client_head->y);
Dave Airlief64122c2013-02-25 14:47:55 +1000132 }
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100133
134 return status;
Dave Airlief64122c2013-02-25 14:47:55 +1000135}
136
Dave Airlie7dea0942014-10-28 11:28:44 +1000137static void qxl_update_offset_props(struct qxl_device *qdev)
138{
139 struct drm_device *dev = qdev->ddev;
140 struct drm_connector *connector;
141 struct qxl_output *output;
142 struct qxl_head *head;
143
144 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
145 output = drm_connector_to_qxl_output(connector);
146
147 head = &qdev->client_monitors_config->heads[output->index];
148
149 drm_object_property_set_value(&connector->base,
150 dev->mode_config.suggested_x_property, head->x);
151 drm_object_property_set_value(&connector->base,
152 dev->mode_config.suggested_y_property, head->y);
153 }
154}
155
Dave Airlief64122c2013-02-25 14:47:55 +1000156void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
157{
158
Dave Airlie7dea0942014-10-28 11:28:44 +1000159 struct drm_device *dev = qdev->ddev;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100160 int status;
161
162 status = qxl_display_copy_rom_client_monitors_config(qdev);
163 while (status == MONITORS_CONFIG_BAD_CRC) {
Dave Airlief64122c2013-02-25 14:47:55 +1000164 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
165 " retrying\n");
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100166 status = qxl_display_copy_rom_client_monitors_config(qdev);
167 }
168 if (status == MONITORS_CONFIG_UNCHANGED) {
169 qxl_io_log(qdev, "config unchanged\n");
170 DRM_DEBUG("ignoring unchanged client monitors config");
171 return;
Dave Airlief64122c2013-02-25 14:47:55 +1000172 }
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200173
Dave Airlie7dea0942014-10-28 11:28:44 +1000174 drm_modeset_lock_all(dev);
175 qxl_update_offset_props(qdev);
176 drm_modeset_unlock_all(dev);
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200177 if (!drm_helper_hpd_irq_event(qdev->ddev)) {
178 /* notify that the monitor configuration changed, to
179 adjust at the arbitrary resolution */
180 drm_kms_helper_hotplug_event(qdev->ddev);
181 }
Dave Airlief64122c2013-02-25 14:47:55 +1000182}
183
Marc-André Lureaub0807422013-10-18 16:11:31 +0200184static int qxl_add_monitors_config_modes(struct drm_connector *connector,
185 unsigned *pwidth,
186 unsigned *pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000187{
188 struct drm_device *dev = connector->dev;
189 struct qxl_device *qdev = dev->dev_private;
190 struct qxl_output *output = drm_connector_to_qxl_output(connector);
191 int h = output->index;
192 struct drm_display_mode *mode = NULL;
193 struct qxl_head *head;
194
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100195 if (!qdev->client_monitors_config)
Dave Airlief64122c2013-02-25 14:47:55 +1000196 return 0;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100197 head = &qdev->client_monitors_config->heads[h];
Dave Airlief64122c2013-02-25 14:47:55 +1000198
199 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
200 false);
201 mode->type |= DRM_MODE_TYPE_PREFERRED;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200202 *pwidth = head->width;
203 *pheight = head->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000204 drm_mode_probed_add(connector, mode);
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500205 /* remember the last custom size for mode validation */
206 qdev->monitors_config_width = mode->hdisplay;
207 qdev->monitors_config_height = mode->vdisplay;
Dave Airlief64122c2013-02-25 14:47:55 +1000208 return 1;
209}
210
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500211static struct mode_size {
212 int w;
213 int h;
214} common_modes[] = {
215 { 640, 480},
216 { 720, 480},
217 { 800, 600},
218 { 848, 480},
219 {1024, 768},
220 {1152, 768},
221 {1280, 720},
222 {1280, 800},
223 {1280, 854},
224 {1280, 960},
225 {1280, 1024},
226 {1440, 900},
227 {1400, 1050},
228 {1680, 1050},
229 {1600, 1200},
230 {1920, 1080},
231 {1920, 1200}
232};
233
Marc-André Lureaub0807422013-10-18 16:11:31 +0200234static int qxl_add_common_modes(struct drm_connector *connector,
235 unsigned pwidth,
236 unsigned pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000237{
238 struct drm_device *dev = connector->dev;
239 struct drm_display_mode *mode = NULL;
240 int i;
Dave Airlief64122c2013-02-25 14:47:55 +1000241 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
Dave Airlief64122c2013-02-25 14:47:55 +1000242 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
243 60, false, false, false);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200244 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000245 mode->type |= DRM_MODE_TYPE_PREFERRED;
246 drm_mode_probed_add(connector, mode);
247 }
248 return i - 1;
249}
250
Dave Airlief64122c2013-02-25 14:47:55 +1000251static void qxl_crtc_destroy(struct drm_crtc *crtc)
252{
253 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
254
255 drm_crtc_cleanup(crtc);
Ray Strode4532b242016-09-09 11:09:05 -0400256 qxl_bo_unref(&qxl_crtc->cursor_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000257 kfree(qxl_crtc);
258}
259
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200260static int qxl_crtc_page_flip(struct drm_crtc *crtc,
261 struct drm_framebuffer *fb,
262 struct drm_pending_vblank_event *event,
263 uint32_t page_flip_flags)
264{
265 struct drm_device *dev = crtc->dev;
266 struct qxl_device *qdev = dev->dev_private;
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200267 struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
268 struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
269 struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
270 struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
271 unsigned long flags;
272 struct drm_clip_rect norect = {
273 .x1 = 0,
274 .y1 = 0,
275 .x2 = fb->width,
276 .y2 = fb->height
277 };
278 int inc = 1;
279 int one_clip_rect = 1;
280 int ret = 0;
281
282 crtc->primary->fb = fb;
283 bo_old->is_primary = false;
284 bo->is_primary = true;
285
286 ret = qxl_bo_reserve(bo, false);
287 if (ret)
288 return ret;
Frediano Ziglio7eb99742015-09-24 14:25:14 +0100289 ret = qxl_bo_pin(bo, bo->type, NULL);
290 qxl_bo_unreserve(bo);
291 if (ret)
292 return ret;
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200293
294 qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
295 &norect, one_clip_rect, inc);
296
Gustavo Padovan078ace62016-06-06 11:41:43 -0300297 drm_crtc_vblank_get(crtc);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200298
299 if (event) {
300 spin_lock_irqsave(&dev->event_lock, flags);
Gustavo Padovan8a605222016-06-06 11:41:35 -0300301 drm_crtc_send_vblank_event(crtc, event);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200302 spin_unlock_irqrestore(&dev->event_lock, flags);
303 }
Gustavo Padovan078ace62016-06-06 11:41:43 -0300304 drm_crtc_vblank_put(crtc);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200305
Frediano Ziglio7eb99742015-09-24 14:25:14 +0100306 ret = qxl_bo_reserve(bo, false);
307 if (!ret) {
308 qxl_bo_unpin(bo);
309 qxl_bo_unreserve(bo);
310 }
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200311
312 return 0;
313}
314
Dave Airlie8002db62013-07-23 14:16:42 +1000315static int
Dave Airlief64122c2013-02-25 14:47:55 +1000316qxl_hide_cursor(struct qxl_device *qdev)
317{
318 struct qxl_release *release;
319 struct qxl_cursor_cmd *cmd;
320 int ret;
321
322 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
323 &release, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000324 if (ret)
325 return ret;
326
327 ret = qxl_release_reserve_list(release, true);
328 if (ret) {
329 qxl_release_free(qdev, release);
330 return ret;
331 }
Dave Airlief64122c2013-02-25 14:47:55 +1000332
333 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
334 cmd->type = QXL_CURSOR_HIDE;
335 qxl_release_unmap(qdev, release, &cmd->release_info);
336
Dave Airlief64122c2013-02-25 14:47:55 +1000337 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000338 qxl_release_fence_buffer_objects(release);
339 return 0;
Dave Airlief64122c2013-02-25 14:47:55 +1000340}
341
Ray Strode4532b242016-09-09 11:09:05 -0400342static int qxl_crtc_apply_cursor(struct drm_crtc *crtc)
343{
344 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
345 struct drm_device *dev = crtc->dev;
346 struct qxl_device *qdev = dev->dev_private;
347 struct qxl_cursor_cmd *cmd;
348 struct qxl_release *release;
349 int ret = 0;
350
351 if (!qcrtc->cursor_bo)
352 return 0;
353
354 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
355 QXL_RELEASE_CURSOR_CMD,
356 &release, NULL);
357 if (ret)
358 return ret;
359
360 ret = qxl_release_list_add(release, qcrtc->cursor_bo);
361 if (ret)
362 goto out_free_release;
363
364 ret = qxl_release_reserve_list(release, false);
365 if (ret)
366 goto out_free_release;
367
368 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
369 cmd->type = QXL_CURSOR_SET;
370 cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
371 cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
372
373 cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
374
375 cmd->u.set.visible = 1;
376 qxl_release_unmap(qdev, release, &cmd->release_info);
377
378 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
379 qxl_release_fence_buffer_objects(release);
380
381 return ret;
382
383out_free_release:
384 qxl_release_free(qdev, release);
385 return ret;
386}
387
Dave Airliec0a60802013-06-20 11:48:53 +1000388static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
389 struct drm_file *file_priv,
390 uint32_t handle,
391 uint32_t width,
392 uint32_t height, int32_t hot_x, int32_t hot_y)
Dave Airlief64122c2013-02-25 14:47:55 +1000393{
394 struct drm_device *dev = crtc->dev;
395 struct qxl_device *qdev = dev->dev_private;
396 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
397 struct drm_gem_object *obj;
398 struct qxl_cursor *cursor;
399 struct qxl_cursor_cmd *cmd;
400 struct qxl_bo *cursor_bo, *user_bo;
401 struct qxl_release *release;
402 void *user_ptr;
403
404 int size = 64*64*4;
405 int ret = 0;
Dave Airlie8002db62013-07-23 14:16:42 +1000406 if (!handle)
407 return qxl_hide_cursor(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +1000408
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100409 obj = drm_gem_object_lookup(file_priv, handle);
Dave Airlief64122c2013-02-25 14:47:55 +1000410 if (!obj) {
411 DRM_ERROR("cannot find cursor object\n");
412 return -ENOENT;
413 }
414
415 user_bo = gem_to_qxl_bo(obj);
416
417 ret = qxl_bo_reserve(user_bo, false);
418 if (ret)
419 goto out_unref;
420
421 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000422 qxl_bo_unreserve(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000423 if (ret)
Dave Airlie8002db62013-07-23 14:16:42 +1000424 goto out_unref;
Dave Airlief64122c2013-02-25 14:47:55 +1000425
426 ret = qxl_bo_kmap(user_bo, &user_ptr);
427 if (ret)
428 goto out_unpin;
429
430 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
431 QXL_RELEASE_CURSOR_CMD,
432 &release, NULL);
433 if (ret)
434 goto out_kunmap;
Dave Airlie8002db62013-07-23 14:16:42 +1000435
436 ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size,
437 &cursor_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000438 if (ret)
439 goto out_free_release;
Dave Airlie8002db62013-07-23 14:16:42 +1000440
441 ret = qxl_release_reserve_list(release, false);
Dave Airlief64122c2013-02-25 14:47:55 +1000442 if (ret)
443 goto out_free_bo;
444
Dave Airlie8002db62013-07-23 14:16:42 +1000445 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
446 if (ret)
447 goto out_backoff;
448
Dave Airlief64122c2013-02-25 14:47:55 +1000449 cursor->header.unique = 0;
450 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
451 cursor->header.width = 64;
452 cursor->header.height = 64;
Dave Airliec0a60802013-06-20 11:48:53 +1000453 cursor->header.hot_spot_x = hot_x;
454 cursor->header.hot_spot_y = hot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000455 cursor->data_size = size;
456 cursor->chunk.next_chunk = 0;
457 cursor->chunk.prev_chunk = 0;
458 cursor->chunk.data_size = size;
459
460 memcpy(cursor->chunk.data, user_ptr, size);
461
462 qxl_bo_kunmap(cursor_bo);
463
Dave Airlief64122c2013-02-25 14:47:55 +1000464 qxl_bo_kunmap(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000465
John Keepingd59a1f72015-11-18 11:17:25 +0000466 qcrtc->cur_x += qcrtc->hot_spot_x - hot_x;
467 qcrtc->cur_y += qcrtc->hot_spot_y - hot_y;
468 qcrtc->hot_spot_x = hot_x;
469 qcrtc->hot_spot_y = hot_y;
470
Dave Airlief64122c2013-02-25 14:47:55 +1000471 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
472 cmd->type = QXL_CURSOR_SET;
John Keepingd59a1f72015-11-18 11:17:25 +0000473 cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
474 cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000475
476 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
Dave Airlief64122c2013-02-25 14:47:55 +1000477
478 cmd->u.set.visible = 1;
479 qxl_release_unmap(qdev, release, &cmd->release_info);
480
Dave Airlief64122c2013-02-25 14:47:55 +1000481 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000482 qxl_release_fence_buffer_objects(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000483
Dave Airlie8002db62013-07-23 14:16:42 +1000484 /* finish with the userspace bo */
485 ret = qxl_bo_reserve(user_bo, false);
486 if (!ret) {
487 qxl_bo_unpin(user_bo);
488 qxl_bo_unreserve(user_bo);
489 }
490 drm_gem_object_unreference_unlocked(obj);
491
Ray Strode4532b242016-09-09 11:09:05 -0400492 qxl_bo_unref (&qcrtc->cursor_bo);
493 qcrtc->cursor_bo = cursor_bo;
Dave Airlief64122c2013-02-25 14:47:55 +1000494
495 return ret;
Dave Airlie8002db62013-07-23 14:16:42 +1000496
497out_backoff:
498 qxl_release_backoff_reserve_list(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000499out_free_bo:
500 qxl_bo_unref(&cursor_bo);
501out_free_release:
Dave Airlief64122c2013-02-25 14:47:55 +1000502 qxl_release_free(qdev, release);
503out_kunmap:
504 qxl_bo_kunmap(user_bo);
505out_unpin:
506 qxl_bo_unpin(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000507out_unref:
508 drm_gem_object_unreference_unlocked(obj);
509 return ret;
510}
511
512static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
513 int x, int y)
514{
515 struct drm_device *dev = crtc->dev;
516 struct qxl_device *qdev = dev->dev_private;
517 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
518 struct qxl_release *release;
519 struct qxl_cursor_cmd *cmd;
520 int ret;
521
522 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
523 &release, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000524 if (ret)
525 return ret;
526
527 ret = qxl_release_reserve_list(release, true);
528 if (ret) {
529 qxl_release_free(qdev, release);
530 return ret;
531 }
Dave Airlief64122c2013-02-25 14:47:55 +1000532
533 qcrtc->cur_x = x;
534 qcrtc->cur_y = y;
535
536 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
537 cmd->type = QXL_CURSOR_MOVE;
John Keepingd59a1f72015-11-18 11:17:25 +0000538 cmd->u.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
539 cmd->u.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000540 qxl_release_unmap(qdev, release, &cmd->release_info);
541
Dave Airlief64122c2013-02-25 14:47:55 +1000542 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000543 qxl_release_fence_buffer_objects(release);
544
Dave Airlief64122c2013-02-25 14:47:55 +1000545 return 0;
546}
547
548
549static const struct drm_crtc_funcs qxl_crtc_funcs = {
Dave Airliec0a60802013-06-20 11:48:53 +1000550 .cursor_set2 = qxl_crtc_cursor_set2,
Dave Airlief64122c2013-02-25 14:47:55 +1000551 .cursor_move = qxl_crtc_cursor_move,
Dave Airlief64122c2013-02-25 14:47:55 +1000552 .set_config = drm_crtc_helper_set_config,
553 .destroy = qxl_crtc_destroy,
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200554 .page_flip = qxl_crtc_page_flip,
Dave Airlief64122c2013-02-25 14:47:55 +1000555};
556
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200557void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
Dave Airlief64122c2013-02-25 14:47:55 +1000558{
559 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
560
Markus Elfringdc3583c2016-07-22 14:14:54 +0200561 drm_gem_object_unreference_unlocked(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000562 drm_framebuffer_cleanup(fb);
563 kfree(qxl_fb);
564}
565
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000566static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
567 struct drm_file *file_priv,
568 unsigned flags, unsigned color,
569 struct drm_clip_rect *clips,
570 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000571{
572 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
573 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
574 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
575 struct drm_clip_rect norect;
576 struct qxl_bo *qobj;
577 int inc = 1;
578
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200579 drm_modeset_lock_all(fb->dev);
580
Dave Airlief64122c2013-02-25 14:47:55 +1000581 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000582 /* if we aren't primary surface ignore this */
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200583 if (!qobj->is_primary) {
584 drm_modeset_unlock_all(fb->dev);
Dave Airlieb2b44652013-05-13 12:48:40 +1000585 return 0;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200586 }
Dave Airlieb2b44652013-05-13 12:48:40 +1000587
Dave Airlief64122c2013-02-25 14:47:55 +1000588 if (!num_clips) {
589 num_clips = 1;
590 clips = &norect;
591 norect.x1 = norect.y1 = 0;
592 norect.x2 = fb->width;
593 norect.y2 = fb->height;
594 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
595 num_clips /= 2;
596 inc = 2; /* skip source rects */
597 }
598
599 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
600 clips, num_clips, inc);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200601
602 drm_modeset_unlock_all(fb->dev);
603
Dave Airlief64122c2013-02-25 14:47:55 +1000604 return 0;
605}
606
607static const struct drm_framebuffer_funcs qxl_fb_funcs = {
608 .destroy = qxl_user_framebuffer_destroy,
609 .dirty = qxl_framebuffer_surface_dirty,
610/* TODO?
611 * .create_handle = qxl_user_framebuffer_create_handle, */
612};
613
614int
615qxl_framebuffer_init(struct drm_device *dev,
616 struct qxl_framebuffer *qfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200617 const struct drm_mode_fb_cmd2 *mode_cmd,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200618 struct drm_gem_object *obj,
619 const struct drm_framebuffer_funcs *funcs)
Dave Airlief64122c2013-02-25 14:47:55 +1000620{
621 int ret;
622
623 qfb->obj = obj;
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200624 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000625 if (ret) {
626 qfb->obj = NULL;
627 return ret;
628 }
629 drm_helper_mode_fill_fb_struct(&qfb->base, mode_cmd);
630 return 0;
631}
632
633static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
634{
635}
636
637static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
638 const struct drm_display_mode *mode,
639 struct drm_display_mode *adjusted_mode)
640{
641 struct drm_device *dev = crtc->dev;
642 struct qxl_device *qdev = dev->dev_private;
643
644 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
645 __func__,
646 mode->hdisplay, mode->vdisplay,
647 adjusted_mode->hdisplay,
648 adjusted_mode->vdisplay);
649 return true;
650}
651
Christophe Fergeaue4a76442016-11-08 10:12:03 +0100652static void
Dave Airlief64122c2013-02-25 14:47:55 +1000653qxl_send_monitors_config(struct qxl_device *qdev)
654{
655 int i;
656
657 BUG_ON(!qdev->ram_header->monitors_config);
658
659 if (qdev->monitors_config->count == 0) {
660 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
661 return;
662 }
663 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
664 struct qxl_head *head = &qdev->monitors_config->heads[i];
665
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100666 if (head->y > 8192 || head->x > 8192 ||
Dave Airlief64122c2013-02-25 14:47:55 +1000667 head->width > 8192 || head->height > 8192) {
668 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
669 i, head->width, head->height,
670 head->x, head->y);
671 return;
672 }
673 }
674 qxl_io_monitors_config(qdev);
675}
676
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100677static void qxl_monitors_config_set(struct qxl_device *qdev,
678 int index,
679 unsigned x, unsigned y,
680 unsigned width, unsigned height,
681 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000682{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100683 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
684 qdev->monitors_config->heads[index].x = x;
685 qdev->monitors_config->heads[index].y = y;
686 qdev->monitors_config->heads[index].width = width;
687 qdev->monitors_config->heads[index].height = height;
688 qdev->monitors_config->heads[index].surface_id = surf_id;
689
Dave Airlief64122c2013-02-25 14:47:55 +1000690}
691
692static int qxl_crtc_mode_set(struct drm_crtc *crtc,
693 struct drm_display_mode *mode,
694 struct drm_display_mode *adjusted_mode,
695 int x, int y,
696 struct drm_framebuffer *old_fb)
697{
698 struct drm_device *dev = crtc->dev;
699 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +1000700 struct qxl_framebuffer *qfb;
701 struct qxl_bo *bo, *old_bo = NULL;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100702 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Dave Airlief64122c2013-02-25 14:47:55 +1000703 bool recreate_primary = false;
704 int ret;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100705 int surf_id;
Matt Roperf4510a22014-04-01 15:22:40 -0700706 if (!crtc->primary->fb) {
Dave Airlief64122c2013-02-25 14:47:55 +1000707 DRM_DEBUG_KMS("No FB bound\n");
708 return 0;
709 }
710
711 if (old_fb) {
712 qfb = to_qxl_framebuffer(old_fb);
713 old_bo = gem_to_qxl_bo(qfb->obj);
714 }
Matt Roperf4510a22014-04-01 15:22:40 -0700715 qfb = to_qxl_framebuffer(crtc->primary->fb);
Dave Airlief64122c2013-02-25 14:47:55 +1000716 bo = gem_to_qxl_bo(qfb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000717 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
718 x, y,
719 mode->hdisplay, mode->vdisplay,
720 adjusted_mode->hdisplay,
721 adjusted_mode->vdisplay);
722
Fabiano Fidêncio8d0d9402015-09-24 15:18:34 +0200723 if (bo->is_primary == false)
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100724 recreate_primary = true;
Dave Airlief64122c2013-02-25 14:47:55 +1000725
Marc-André Lureauc572aaf42014-10-16 11:39:44 +0200726 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
727 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
728 return -EINVAL;
729 }
Dave Airlief64122c2013-02-25 14:47:55 +1000730
731 ret = qxl_bo_reserve(bo, false);
732 if (ret != 0)
733 return ret;
734 ret = qxl_bo_pin(bo, bo->type, NULL);
735 if (ret != 0) {
736 qxl_bo_unreserve(bo);
737 return -EINVAL;
738 }
739 qxl_bo_unreserve(bo);
740 if (recreate_primary) {
741 qxl_io_destroy_primary(qdev);
742 qxl_io_log(qdev,
Marc-André Lureauc572aaf42014-10-16 11:39:44 +0200743 "recreate primary: %dx%d,%d,%d\n",
744 bo->surf.width, bo->surf.height,
745 bo->surf.stride, bo->surf.format);
746 qxl_io_create_primary(qdev, 0, bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000747 bo->is_primary = true;
Ray Strode4532b242016-09-09 11:09:05 -0400748
749 ret = qxl_crtc_apply_cursor(crtc);
750 if (ret) {
751 DRM_ERROR("could not set cursor after modeset");
752 ret = 0;
753 }
David Mansfield52571ad2014-06-04 12:12:15 +1000754 }
755
756 if (bo->is_primary) {
757 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 +0100758 surf_id = 0;
759 } else {
760 surf_id = bo->surface_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000761 }
762
763 if (old_bo && old_bo != bo) {
764 old_bo->is_primary = false;
765 ret = qxl_bo_reserve(old_bo, false);
766 qxl_bo_unpin(old_bo);
767 qxl_bo_unreserve(old_bo);
768 }
769
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100770 qxl_monitors_config_set(qdev, qcrtc->index, x, y,
771 mode->hdisplay,
772 mode->vdisplay, surf_id);
Dave Airlief64122c2013-02-25 14:47:55 +1000773 return 0;
774}
775
776static void qxl_crtc_prepare(struct drm_crtc *crtc)
777{
778 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
779 crtc->mode.hdisplay, crtc->mode.vdisplay,
780 crtc->x, crtc->y, crtc->enabled);
781}
782
783static void qxl_crtc_commit(struct drm_crtc *crtc)
784{
785 DRM_DEBUG("\n");
786}
787
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100788static void qxl_crtc_disable(struct drm_crtc *crtc)
789{
790 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
791 struct drm_device *dev = crtc->dev;
792 struct qxl_device *qdev = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700793 if (crtc->primary->fb) {
794 struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100795 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
796 int ret;
797 ret = qxl_bo_reserve(bo, false);
798 qxl_bo_unpin(bo);
799 qxl_bo_unreserve(bo);
Matt Roperf4510a22014-04-01 15:22:40 -0700800 crtc->primary->fb = NULL;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100801 }
802
803 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
804
805 qxl_send_monitors_config(qdev);
806}
807
Dave Airlief64122c2013-02-25 14:47:55 +1000808static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
809 .dpms = qxl_crtc_dpms,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100810 .disable = qxl_crtc_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000811 .mode_fixup = qxl_crtc_mode_fixup,
812 .mode_set = qxl_crtc_mode_set,
813 .prepare = qxl_crtc_prepare,
814 .commit = qxl_crtc_commit,
Dave Airlief64122c2013-02-25 14:47:55 +1000815};
816
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100817static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000818{
819 struct qxl_crtc *qxl_crtc;
820
821 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
822 if (!qxl_crtc)
823 return -ENOMEM;
824
825 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100826 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000827 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
828 return 0;
829}
830
831static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
832{
833 DRM_DEBUG("\n");
834}
835
Dave Airlief64122c2013-02-25 14:47:55 +1000836static void qxl_enc_prepare(struct drm_encoder *encoder)
837{
838 DRM_DEBUG("\n");
839}
840
841static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
842 struct drm_encoder *encoder)
843{
844 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100845 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000846 struct qxl_head *head;
847 struct drm_display_mode *mode;
848
849 BUG_ON(!encoder);
850 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100851 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000852 if (!qdev->monitors_config ||
853 qdev->monitors_config->max_allowed <= i) {
854 DRM_ERROR(
855 "head number too large or missing monitors config: %p, %d",
856 qdev->monitors_config,
857 qdev->monitors_config ?
858 qdev->monitors_config->max_allowed : -1);
859 return;
860 }
861 if (!encoder->crtc) {
862 DRM_ERROR("missing crtc on encoder %p\n", encoder);
863 return;
864 }
865 if (i != 0)
866 DRM_DEBUG("missing for multiple monitors: no head holes\n");
867 head = &qdev->monitors_config->heads[i];
868 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000869 if (encoder->crtc->enabled) {
870 mode = &encoder->crtc->mode;
871 head->width = mode->hdisplay;
872 head->height = mode->vdisplay;
873 head->x = encoder->crtc->x;
874 head->y = encoder->crtc->y;
875 if (qdev->monitors_config->count < i + 1)
876 qdev->monitors_config->count = i + 1;
877 } else {
878 head->width = 0;
879 head->height = 0;
880 head->x = 0;
881 head->y = 0;
882 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100883 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
884 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000885 head->flags = 0;
886 /* TODO - somewhere else to call this for multiple monitors
887 * (config_commit?) */
888 qxl_send_monitors_config(qdev);
889}
890
891static void qxl_enc_commit(struct drm_encoder *encoder)
892{
893 struct qxl_device *qdev = encoder->dev->dev_private;
894
895 qxl_write_monitors_config_for_encoder(qdev, encoder);
896 DRM_DEBUG("\n");
897}
898
899static void qxl_enc_mode_set(struct drm_encoder *encoder,
900 struct drm_display_mode *mode,
901 struct drm_display_mode *adjusted_mode)
902{
903 DRM_DEBUG("\n");
904}
905
906static int qxl_conn_get_modes(struct drm_connector *connector)
907{
908 int ret = 0;
909 struct qxl_device *qdev = connector->dev->dev_private;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200910 unsigned pwidth = 1024;
911 unsigned pheight = 768;
Dave Airlief64122c2013-02-25 14:47:55 +1000912
913 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
914 /* TODO: what should we do here? only show the configured modes for the
915 * device, or allow the full list, or both? */
916 if (qdev->monitors_config && qdev->monitors_config->count) {
Marc-André Lureaub0807422013-10-18 16:11:31 +0200917 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000918 if (ret < 0)
919 return ret;
920 }
Marc-André Lureaub0807422013-10-18 16:11:31 +0200921 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000922 return ret;
923}
924
925static int qxl_conn_mode_valid(struct drm_connector *connector,
926 struct drm_display_mode *mode)
927{
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500928 struct drm_device *ddev = connector->dev;
929 struct qxl_device *qdev = ddev->dev_private;
930 int i;
931
Dave Airlief64122c2013-02-25 14:47:55 +1000932 /* TODO: is this called for user defined modes? (xrandr --add-mode)
933 * TODO: check that the mode fits in the framebuffer */
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500934
935 if(qdev->monitors_config_width == mode->hdisplay &&
936 qdev->monitors_config_height == mode->vdisplay)
937 return MODE_OK;
938
939 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
940 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
941 return MODE_OK;
942 }
943 return MODE_BAD;
Dave Airlief64122c2013-02-25 14:47:55 +1000944}
945
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000946static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +1000947{
948 struct qxl_output *qxl_output =
949 drm_connector_to_qxl_output(connector);
950
951 DRM_DEBUG("\n");
952 return &qxl_output->enc;
953}
954
955
956static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
957 .dpms = qxl_enc_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +1000958 .prepare = qxl_enc_prepare,
959 .mode_set = qxl_enc_mode_set,
960 .commit = qxl_enc_commit,
961};
962
963static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
964 .get_modes = qxl_conn_get_modes,
965 .mode_valid = qxl_conn_mode_valid,
966 .best_encoder = qxl_best_encoder,
967};
968
Dave Airlief64122c2013-02-25 14:47:55 +1000969static enum drm_connector_status qxl_conn_detect(
970 struct drm_connector *connector,
971 bool force)
972{
973 struct qxl_output *output =
974 drm_connector_to_qxl_output(connector);
975 struct drm_device *ddev = connector->dev;
976 struct qxl_device *qdev = ddev->dev_private;
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000977 bool connected = false;
Dave Airlief64122c2013-02-25 14:47:55 +1000978
979 /* The first monitor is always connected */
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000980 if (!qdev->client_monitors_config) {
981 if (output->index == 0)
982 connected = true;
983 } else
984 connected = qdev->client_monitors_config->count > output->index &&
985 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
Dave Airlief64122c2013-02-25 14:47:55 +1000986
Marc-André Lureau5cab51c2013-10-18 16:11:33 +0200987 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
988 if (!connected)
989 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
990
Dave Airlief64122c2013-02-25 14:47:55 +1000991 return connected ? connector_status_connected
992 : connector_status_disconnected;
993}
994
995static int qxl_conn_set_property(struct drm_connector *connector,
996 struct drm_property *property,
997 uint64_t value)
998{
999 DRM_DEBUG("\n");
1000 return 0;
1001}
1002
1003static void qxl_conn_destroy(struct drm_connector *connector)
1004{
1005 struct qxl_output *qxl_output =
1006 drm_connector_to_qxl_output(connector);
1007
Thomas Wood34ea3d32014-05-29 16:57:41 +01001008 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001009 drm_connector_cleanup(connector);
1010 kfree(qxl_output);
1011}
1012
1013static const struct drm_connector_funcs qxl_connector_funcs = {
1014 .dpms = drm_helper_connector_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001015 .detect = qxl_conn_detect,
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001016 .fill_modes = drm_helper_probe_single_connector_modes,
Dave Airlief64122c2013-02-25 14:47:55 +10001017 .set_property = qxl_conn_set_property,
1018 .destroy = qxl_conn_destroy,
1019};
1020
1021static void qxl_enc_destroy(struct drm_encoder *encoder)
1022{
1023 drm_encoder_cleanup(encoder);
1024}
1025
1026static const struct drm_encoder_funcs qxl_enc_funcs = {
1027 .destroy = qxl_enc_destroy,
1028};
1029
Dave Airlie4695b032013-10-11 11:05:00 +10001030static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1031{
1032 if (qdev->hotplug_mode_update_property)
1033 return 0;
1034
1035 qdev->hotplug_mode_update_property =
1036 drm_property_create_range(qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1037 "hotplug_mode_update", 0, 1);
1038
1039 return 0;
1040}
1041
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001042static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +10001043{
Dave Airlie4695b032013-10-11 11:05:00 +10001044 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +10001045 struct qxl_output *qxl_output;
1046 struct drm_connector *connector;
1047 struct drm_encoder *encoder;
1048
1049 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1050 if (!qxl_output)
1051 return -ENOMEM;
1052
1053 qxl_output->index = num_output;
1054
1055 connector = &qxl_output->base;
1056 encoder = &qxl_output->enc;
1057 drm_connector_init(dev, &qxl_output->base,
1058 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1059
1060 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001061 DRM_MODE_ENCODER_VIRTUAL, NULL);
Dave Airlief64122c2013-02-25 14:47:55 +10001062
Dave Airlie5ff91e42013-07-05 10:20:33 +10001063 /* we get HPD via client monitors config */
1064 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +10001065 encoder->possible_crtcs = 1 << num_output;
1066 drm_mode_connector_attach_encoder(&qxl_output->base,
1067 &qxl_output->enc);
1068 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1069 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1070
Dave Airlie4695b032013-10-11 11:05:00 +10001071 drm_object_attach_property(&connector->base,
1072 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +10001073 drm_object_attach_property(&connector->base,
1074 dev->mode_config.suggested_x_property, 0);
1075 drm_object_attach_property(&connector->base,
1076 dev->mode_config.suggested_y_property, 0);
Thomas Wood34ea3d32014-05-29 16:57:41 +01001077 drm_connector_register(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001078 return 0;
1079}
1080
1081static struct drm_framebuffer *
1082qxl_user_framebuffer_create(struct drm_device *dev,
1083 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02001084 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief64122c2013-02-25 14:47:55 +10001085{
1086 struct drm_gem_object *obj;
1087 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +10001088 int ret;
1089
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001090 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1091 if (!obj)
1092 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +10001093
1094 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1095 if (qxl_fb == NULL)
1096 return NULL;
1097
Noralf Trønnes6819c3c2016-04-28 17:18:36 +02001098 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +10001099 if (ret) {
1100 kfree(qxl_fb);
1101 drm_gem_object_unreference_unlocked(obj);
1102 return NULL;
1103 }
1104
Dave Airlief64122c2013-02-25 14:47:55 +10001105 return &qxl_fb->base;
1106}
1107
1108static const struct drm_mode_config_funcs qxl_mode_funcs = {
1109 .fb_create = qxl_user_framebuffer_create,
1110};
1111
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001112int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001113{
Dave Airlief64122c2013-02-25 14:47:55 +10001114 int ret;
1115 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001116 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001117 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001118 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001119
Dave Airlief64122c2013-02-25 14:47:55 +10001120 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1121 QXL_GEM_DOMAIN_VRAM,
1122 false, false, NULL, &gobj);
1123 if (ret) {
1124 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1125 return -ENOMEM;
1126 }
1127 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001128
1129 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1130 if (ret)
1131 return ret;
1132
1133 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
1134 if (ret) {
1135 qxl_bo_unreserve(qdev->monitors_config_bo);
1136 return ret;
1137 }
1138
1139 qxl_bo_unreserve(qdev->monitors_config_bo);
1140
Dave Airlief64122c2013-02-25 14:47:55 +10001141 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001142
Dave Airlief64122c2013-02-25 14:47:55 +10001143 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1144 qdev->ram_header->monitors_config =
1145 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1146
1147 memset(qdev->monitors_config, 0, monitors_config_size);
1148 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001149 return 0;
1150}
1151
1152int qxl_destroy_monitors_object(struct qxl_device *qdev)
1153{
1154 int ret;
1155
1156 qdev->monitors_config = NULL;
1157 qdev->ram_header->monitors_config = 0;
1158
1159 qxl_bo_kunmap(qdev->monitors_config_bo);
1160 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1161 if (ret)
1162 return ret;
1163
1164 qxl_bo_unpin(qdev->monitors_config_bo);
1165 qxl_bo_unreserve(qdev->monitors_config_bo);
1166
1167 qxl_bo_unref(&qdev->monitors_config_bo);
1168 return 0;
1169}
1170
1171int qxl_modeset_init(struct qxl_device *qdev)
1172{
1173 int i;
1174 int ret;
1175
1176 drm_mode_config_init(qdev->ddev);
1177
1178 ret = qxl_create_monitors_object(qdev);
1179 if (ret)
1180 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001181
1182 qdev->ddev->mode_config.funcs = (void *)&qxl_mode_funcs;
1183
1184 /* modes will be validated against the framebuffer size */
1185 qdev->ddev->mode_config.min_width = 320;
1186 qdev->ddev->mode_config.min_height = 200;
1187 qdev->ddev->mode_config.max_width = 8192;
1188 qdev->ddev->mode_config.max_height = 8192;
1189
1190 qdev->ddev->mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001191
Dave Airlie7dea0942014-10-28 11:28:44 +10001192 drm_mode_create_suggested_offset_properties(qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001193 qxl_mode_create_hotplug_mode_update_property(qdev);
1194
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001195 for (i = 0 ; i < qxl_num_crtc; ++i) {
Dave Airlief64122c2013-02-25 14:47:55 +10001196 qdev_crtc_init(qdev->ddev, i);
1197 qdev_output_init(qdev->ddev, i);
1198 }
1199
1200 qdev->mode_info.mode_config_initialized = true;
1201
1202 /* primary surface must be created by this point, to allow
1203 * issuing command queue commands and having them read by
1204 * spice server. */
1205 qxl_fbdev_init(qdev);
1206 return 0;
1207}
1208
1209void qxl_modeset_fini(struct qxl_device *qdev)
1210{
1211 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001212
1213 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001214 if (qdev->mode_info.mode_config_initialized) {
1215 drm_mode_config_cleanup(qdev->ddev);
1216 qdev->mode_info.mode_config_initialized = false;
1217 }
1218}