blob: 0957db17de4daf2b0daa35417962facd170a7fb6 [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{
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200139 struct drm_device *dev = &qdev->ddev;
Dave Airlie7dea0942014-10-28 11:28:44 +1000140 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
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200159 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);
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200177 if (!drm_helper_hpd_irq_event(dev)) {
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200178 /* notify that the monitor configuration changed, to
179 adjust at the arbitrary resolution */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200180 drm_kms_helper_hotplug_event(dev);
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200181 }
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;
Christophe Fergeauff996e72016-11-08 10:12:09 +0100202 mode->hdisplay = head->width;
203 mode->vdisplay = head->height;
204 drm_mode_set_name(mode);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200205 *pwidth = head->width;
206 *pheight = head->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000207 drm_mode_probed_add(connector, mode);
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500208 /* remember the last custom size for mode validation */
209 qdev->monitors_config_width = mode->hdisplay;
210 qdev->monitors_config_height = mode->vdisplay;
Dave Airlief64122c2013-02-25 14:47:55 +1000211 return 1;
212}
213
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500214static struct mode_size {
215 int w;
216 int h;
217} common_modes[] = {
218 { 640, 480},
219 { 720, 480},
220 { 800, 600},
221 { 848, 480},
222 {1024, 768},
223 {1152, 768},
224 {1280, 720},
225 {1280, 800},
226 {1280, 854},
227 {1280, 960},
228 {1280, 1024},
229 {1440, 900},
230 {1400, 1050},
231 {1680, 1050},
232 {1600, 1200},
233 {1920, 1080},
234 {1920, 1200}
235};
236
Marc-André Lureaub0807422013-10-18 16:11:31 +0200237static int qxl_add_common_modes(struct drm_connector *connector,
238 unsigned pwidth,
239 unsigned pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000240{
241 struct drm_device *dev = connector->dev;
242 struct drm_display_mode *mode = NULL;
243 int i;
Dave Airlief64122c2013-02-25 14:47:55 +1000244 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
Dave Airlief64122c2013-02-25 14:47:55 +1000245 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
246 60, false, false, false);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200247 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000248 mode->type |= DRM_MODE_TYPE_PREFERRED;
249 drm_mode_probed_add(connector, mode);
250 }
251 return i - 1;
252}
253
Dave Airlief64122c2013-02-25 14:47:55 +1000254static void qxl_crtc_destroy(struct drm_crtc *crtc)
255{
256 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
257
258 drm_crtc_cleanup(crtc);
Ray Strode4532b242016-09-09 11:09:05 -0400259 qxl_bo_unref(&qxl_crtc->cursor_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000260 kfree(qxl_crtc);
261}
262
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200263static int qxl_crtc_page_flip(struct drm_crtc *crtc,
264 struct drm_framebuffer *fb,
265 struct drm_pending_vblank_event *event,
266 uint32_t page_flip_flags)
267{
268 struct drm_device *dev = crtc->dev;
269 struct qxl_device *qdev = dev->dev_private;
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200270 struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
271 struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
272 struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
273 struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
274 unsigned long flags;
275 struct drm_clip_rect norect = {
276 .x1 = 0,
277 .y1 = 0,
278 .x2 = fb->width,
279 .y2 = fb->height
280 };
281 int inc = 1;
282 int one_clip_rect = 1;
283 int ret = 0;
284
285 crtc->primary->fb = fb;
286 bo_old->is_primary = false;
287 bo->is_primary = true;
288
Frediano Ziglio7eb99742015-09-24 14:25:14 +0100289 ret = qxl_bo_pin(bo, bo->type, NULL);
Frediano Ziglio7eb99742015-09-24 14:25:14 +0100290 if (ret)
291 return ret;
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200292
293 qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
294 &norect, one_clip_rect, inc);
295
Gustavo Padovan078ace62016-06-06 11:41:43 -0300296 drm_crtc_vblank_get(crtc);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200297
298 if (event) {
299 spin_lock_irqsave(&dev->event_lock, flags);
Gustavo Padovan8a605222016-06-06 11:41:35 -0300300 drm_crtc_send_vblank_event(crtc, event);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200301 spin_unlock_irqrestore(&dev->event_lock, flags);
302 }
Gustavo Padovan078ace62016-06-06 11:41:43 -0300303 drm_crtc_vblank_put(crtc);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200304
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300305 qxl_bo_unpin(bo);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200306
307 return 0;
308}
309
Dave Airlie8002db62013-07-23 14:16:42 +1000310static int
Dave Airlief64122c2013-02-25 14:47:55 +1000311qxl_hide_cursor(struct qxl_device *qdev)
312{
313 struct qxl_release *release;
314 struct qxl_cursor_cmd *cmd;
315 int ret;
316
317 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
318 &release, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000319 if (ret)
320 return ret;
321
322 ret = qxl_release_reserve_list(release, true);
323 if (ret) {
324 qxl_release_free(qdev, release);
325 return ret;
326 }
Dave Airlief64122c2013-02-25 14:47:55 +1000327
328 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
329 cmd->type = QXL_CURSOR_HIDE;
330 qxl_release_unmap(qdev, release, &cmd->release_info);
331
Dave Airlief64122c2013-02-25 14:47:55 +1000332 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000333 qxl_release_fence_buffer_objects(release);
334 return 0;
Dave Airlief64122c2013-02-25 14:47:55 +1000335}
336
Ray Strode4532b242016-09-09 11:09:05 -0400337static int qxl_crtc_apply_cursor(struct drm_crtc *crtc)
338{
339 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
340 struct drm_device *dev = crtc->dev;
341 struct qxl_device *qdev = dev->dev_private;
342 struct qxl_cursor_cmd *cmd;
343 struct qxl_release *release;
344 int ret = 0;
345
346 if (!qcrtc->cursor_bo)
347 return 0;
348
349 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
350 QXL_RELEASE_CURSOR_CMD,
351 &release, NULL);
352 if (ret)
353 return ret;
354
355 ret = qxl_release_list_add(release, qcrtc->cursor_bo);
356 if (ret)
357 goto out_free_release;
358
359 ret = qxl_release_reserve_list(release, false);
360 if (ret)
361 goto out_free_release;
362
363 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
364 cmd->type = QXL_CURSOR_SET;
365 cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
366 cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
367
368 cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
369
370 cmd->u.set.visible = 1;
371 qxl_release_unmap(qdev, release, &cmd->release_info);
372
373 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
374 qxl_release_fence_buffer_objects(release);
375
376 return ret;
377
378out_free_release:
379 qxl_release_free(qdev, release);
380 return ret;
381}
382
Dave Airliec0a60802013-06-20 11:48:53 +1000383static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
384 struct drm_file *file_priv,
385 uint32_t handle,
386 uint32_t width,
387 uint32_t height, int32_t hot_x, int32_t hot_y)
Dave Airlief64122c2013-02-25 14:47:55 +1000388{
389 struct drm_device *dev = crtc->dev;
390 struct qxl_device *qdev = dev->dev_private;
391 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
392 struct drm_gem_object *obj;
393 struct qxl_cursor *cursor;
394 struct qxl_cursor_cmd *cmd;
395 struct qxl_bo *cursor_bo, *user_bo;
396 struct qxl_release *release;
397 void *user_ptr;
398
399 int size = 64*64*4;
400 int ret = 0;
Dave Airlie8002db62013-07-23 14:16:42 +1000401 if (!handle)
402 return qxl_hide_cursor(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +1000403
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100404 obj = drm_gem_object_lookup(file_priv, handle);
Dave Airlief64122c2013-02-25 14:47:55 +1000405 if (!obj) {
406 DRM_ERROR("cannot find cursor object\n");
407 return -ENOENT;
408 }
409
410 user_bo = gem_to_qxl_bo(obj);
411
Dave Airlief64122c2013-02-25 14:47:55 +1000412 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
413 if (ret)
Dave Airlie8002db62013-07-23 14:16:42 +1000414 goto out_unref;
Dave Airlief64122c2013-02-25 14:47:55 +1000415
416 ret = qxl_bo_kmap(user_bo, &user_ptr);
417 if (ret)
418 goto out_unpin;
419
420 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
421 QXL_RELEASE_CURSOR_CMD,
422 &release, NULL);
423 if (ret)
424 goto out_kunmap;
Dave Airlie8002db62013-07-23 14:16:42 +1000425
426 ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size,
427 &cursor_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000428 if (ret)
429 goto out_free_release;
Dave Airlie8002db62013-07-23 14:16:42 +1000430
431 ret = qxl_release_reserve_list(release, false);
Dave Airlief64122c2013-02-25 14:47:55 +1000432 if (ret)
433 goto out_free_bo;
434
Dave Airlie8002db62013-07-23 14:16:42 +1000435 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
436 if (ret)
437 goto out_backoff;
438
Dave Airlief64122c2013-02-25 14:47:55 +1000439 cursor->header.unique = 0;
440 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
441 cursor->header.width = 64;
442 cursor->header.height = 64;
Dave Airliec0a60802013-06-20 11:48:53 +1000443 cursor->header.hot_spot_x = hot_x;
444 cursor->header.hot_spot_y = hot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000445 cursor->data_size = size;
446 cursor->chunk.next_chunk = 0;
447 cursor->chunk.prev_chunk = 0;
448 cursor->chunk.data_size = size;
449
450 memcpy(cursor->chunk.data, user_ptr, size);
451
452 qxl_bo_kunmap(cursor_bo);
453
Dave Airlief64122c2013-02-25 14:47:55 +1000454 qxl_bo_kunmap(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000455
John Keepingd59a1f72015-11-18 11:17:25 +0000456 qcrtc->cur_x += qcrtc->hot_spot_x - hot_x;
457 qcrtc->cur_y += qcrtc->hot_spot_y - hot_y;
458 qcrtc->hot_spot_x = hot_x;
459 qcrtc->hot_spot_y = hot_y;
460
Dave Airlief64122c2013-02-25 14:47:55 +1000461 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
462 cmd->type = QXL_CURSOR_SET;
John Keepingd59a1f72015-11-18 11:17:25 +0000463 cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
464 cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000465
466 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
Dave Airlief64122c2013-02-25 14:47:55 +1000467
468 cmd->u.set.visible = 1;
469 qxl_release_unmap(qdev, release, &cmd->release_info);
470
Dave Airlief64122c2013-02-25 14:47:55 +1000471 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000472 qxl_release_fence_buffer_objects(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000473
Dave Airlie8002db62013-07-23 14:16:42 +1000474 /* finish with the userspace bo */
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300475 qxl_bo_unpin(user_bo);
476
Dave Airlie8002db62013-07-23 14:16:42 +1000477 drm_gem_object_unreference_unlocked(obj);
478
Ray Strode4532b242016-09-09 11:09:05 -0400479 qxl_bo_unref (&qcrtc->cursor_bo);
480 qcrtc->cursor_bo = cursor_bo;
Dave Airlief64122c2013-02-25 14:47:55 +1000481
482 return ret;
Dave Airlie8002db62013-07-23 14:16:42 +1000483
484out_backoff:
485 qxl_release_backoff_reserve_list(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000486out_free_bo:
487 qxl_bo_unref(&cursor_bo);
488out_free_release:
Dave Airlief64122c2013-02-25 14:47:55 +1000489 qxl_release_free(qdev, release);
490out_kunmap:
491 qxl_bo_kunmap(user_bo);
492out_unpin:
493 qxl_bo_unpin(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000494out_unref:
495 drm_gem_object_unreference_unlocked(obj);
496 return ret;
497}
498
499static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
500 int x, int y)
501{
502 struct drm_device *dev = crtc->dev;
503 struct qxl_device *qdev = dev->dev_private;
504 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
505 struct qxl_release *release;
506 struct qxl_cursor_cmd *cmd;
507 int ret;
508
509 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
510 &release, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000511 if (ret)
512 return ret;
513
514 ret = qxl_release_reserve_list(release, true);
515 if (ret) {
516 qxl_release_free(qdev, release);
517 return ret;
518 }
Dave Airlief64122c2013-02-25 14:47:55 +1000519
520 qcrtc->cur_x = x;
521 qcrtc->cur_y = y;
522
523 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
524 cmd->type = QXL_CURSOR_MOVE;
John Keepingd59a1f72015-11-18 11:17:25 +0000525 cmd->u.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
526 cmd->u.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000527 qxl_release_unmap(qdev, release, &cmd->release_info);
528
Dave Airlief64122c2013-02-25 14:47:55 +1000529 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000530 qxl_release_fence_buffer_objects(release);
531
Dave Airlief64122c2013-02-25 14:47:55 +1000532 return 0;
533}
534
Shawn Guo54d82e02017-02-07 17:16:27 +0800535static u32 qxl_noop_get_vblank_counter(struct drm_crtc *crtc)
536{
537 return 0;
538}
539
540static int qxl_noop_enable_vblank(struct drm_crtc *crtc)
541{
542 return 0;
543}
544
545static void qxl_noop_disable_vblank(struct drm_crtc *crtc)
546{
547}
Dave Airlief64122c2013-02-25 14:47:55 +1000548
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,
Shawn Guo54d82e02017-02-07 17:16:27 +0800555 .get_vblank_counter = qxl_noop_get_vblank_counter,
556 .enable_vblank = qxl_noop_enable_vblank,
557 .disable_vblank = qxl_noop_disable_vblank,
Dave Airlief64122c2013-02-25 14:47:55 +1000558};
559
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200560void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
Dave Airlief64122c2013-02-25 14:47:55 +1000561{
562 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
563
Markus Elfringdc3583c2016-07-22 14:14:54 +0200564 drm_gem_object_unreference_unlocked(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000565 drm_framebuffer_cleanup(fb);
566 kfree(qxl_fb);
567}
568
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000569static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
570 struct drm_file *file_priv,
571 unsigned flags, unsigned color,
572 struct drm_clip_rect *clips,
573 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000574{
575 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
576 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
577 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
578 struct drm_clip_rect norect;
579 struct qxl_bo *qobj;
580 int inc = 1;
581
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200582 drm_modeset_lock_all(fb->dev);
583
Dave Airlief64122c2013-02-25 14:47:55 +1000584 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000585 /* if we aren't primary surface ignore this */
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200586 if (!qobj->is_primary) {
587 drm_modeset_unlock_all(fb->dev);
Dave Airlieb2b44652013-05-13 12:48:40 +1000588 return 0;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200589 }
Dave Airlieb2b44652013-05-13 12:48:40 +1000590
Dave Airlief64122c2013-02-25 14:47:55 +1000591 if (!num_clips) {
592 num_clips = 1;
593 clips = &norect;
594 norect.x1 = norect.y1 = 0;
595 norect.x2 = fb->width;
596 norect.y2 = fb->height;
597 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
598 num_clips /= 2;
599 inc = 2; /* skip source rects */
600 }
601
602 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
603 clips, num_clips, inc);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200604
605 drm_modeset_unlock_all(fb->dev);
606
Dave Airlief64122c2013-02-25 14:47:55 +1000607 return 0;
608}
609
610static const struct drm_framebuffer_funcs qxl_fb_funcs = {
611 .destroy = qxl_user_framebuffer_destroy,
612 .dirty = qxl_framebuffer_surface_dirty,
613/* TODO?
614 * .create_handle = qxl_user_framebuffer_create_handle, */
615};
616
617int
618qxl_framebuffer_init(struct drm_device *dev,
619 struct qxl_framebuffer *qfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200620 const struct drm_mode_fb_cmd2 *mode_cmd,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200621 struct drm_gem_object *obj,
622 const struct drm_framebuffer_funcs *funcs)
Dave Airlief64122c2013-02-25 14:47:55 +1000623{
624 int ret;
625
626 qfb->obj = obj;
Ville Syrjälä53609432016-11-18 21:52:51 +0200627 drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200628 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000629 if (ret) {
630 qfb->obj = NULL;
631 return ret;
632 }
Dave Airlief64122c2013-02-25 14:47:55 +1000633 return 0;
634}
635
636static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
637{
638}
639
640static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
641 const struct drm_display_mode *mode,
642 struct drm_display_mode *adjusted_mode)
643{
644 struct drm_device *dev = crtc->dev;
645 struct qxl_device *qdev = dev->dev_private;
646
647 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
648 __func__,
649 mode->hdisplay, mode->vdisplay,
650 adjusted_mode->hdisplay,
651 adjusted_mode->vdisplay);
652 return true;
653}
654
Christophe Fergeaue4a76442016-11-08 10:12:03 +0100655static void
Dave Airlief64122c2013-02-25 14:47:55 +1000656qxl_send_monitors_config(struct qxl_device *qdev)
657{
658 int i;
659
660 BUG_ON(!qdev->ram_header->monitors_config);
661
662 if (qdev->monitors_config->count == 0) {
663 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
664 return;
665 }
666 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
667 struct qxl_head *head = &qdev->monitors_config->heads[i];
668
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100669 if (head->y > 8192 || head->x > 8192 ||
Dave Airlief64122c2013-02-25 14:47:55 +1000670 head->width > 8192 || head->height > 8192) {
671 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
672 i, head->width, head->height,
673 head->x, head->y);
674 return;
675 }
676 }
677 qxl_io_monitors_config(qdev);
678}
679
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100680static void qxl_monitors_config_set(struct qxl_device *qdev,
681 int index,
682 unsigned x, unsigned y,
683 unsigned width, unsigned height,
684 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000685{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100686 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
687 qdev->monitors_config->heads[index].x = x;
688 qdev->monitors_config->heads[index].y = y;
689 qdev->monitors_config->heads[index].width = width;
690 qdev->monitors_config->heads[index].height = height;
691 qdev->monitors_config->heads[index].surface_id = surf_id;
692
Dave Airlief64122c2013-02-25 14:47:55 +1000693}
694
695static int qxl_crtc_mode_set(struct drm_crtc *crtc,
696 struct drm_display_mode *mode,
697 struct drm_display_mode *adjusted_mode,
698 int x, int y,
699 struct drm_framebuffer *old_fb)
700{
701 struct drm_device *dev = crtc->dev;
702 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +1000703 struct qxl_framebuffer *qfb;
704 struct qxl_bo *bo, *old_bo = NULL;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100705 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Dave Airlief64122c2013-02-25 14:47:55 +1000706 bool recreate_primary = false;
707 int ret;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100708 int surf_id;
Matt Roperf4510a22014-04-01 15:22:40 -0700709 if (!crtc->primary->fb) {
Dave Airlief64122c2013-02-25 14:47:55 +1000710 DRM_DEBUG_KMS("No FB bound\n");
711 return 0;
712 }
713
714 if (old_fb) {
715 qfb = to_qxl_framebuffer(old_fb);
716 old_bo = gem_to_qxl_bo(qfb->obj);
717 }
Matt Roperf4510a22014-04-01 15:22:40 -0700718 qfb = to_qxl_framebuffer(crtc->primary->fb);
Dave Airlief64122c2013-02-25 14:47:55 +1000719 bo = gem_to_qxl_bo(qfb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000720 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
721 x, y,
722 mode->hdisplay, mode->vdisplay,
723 adjusted_mode->hdisplay,
724 adjusted_mode->vdisplay);
725
Fabiano FidĂªncio8d0d9402015-09-24 15:18:34 +0200726 if (bo->is_primary == false)
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100727 recreate_primary = true;
Dave Airlief64122c2013-02-25 14:47:55 +1000728
Marc-André Lureauc572aaf42014-10-16 11:39:44 +0200729 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
730 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
731 return -EINVAL;
732 }
Dave Airlief64122c2013-02-25 14:47:55 +1000733
Dave Airlief64122c2013-02-25 14:47:55 +1000734 ret = qxl_bo_pin(bo, bo->type, NULL);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300735 if (ret != 0)
Dave Airlief64122c2013-02-25 14:47:55 +1000736 return -EINVAL;
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300737
Dave Airlief64122c2013-02-25 14:47:55 +1000738 if (recreate_primary) {
739 qxl_io_destroy_primary(qdev);
740 qxl_io_log(qdev,
Marc-André Lureauc572aaf42014-10-16 11:39:44 +0200741 "recreate primary: %dx%d,%d,%d\n",
742 bo->surf.width, bo->surf.height,
743 bo->surf.stride, bo->surf.format);
744 qxl_io_create_primary(qdev, 0, bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000745 bo->is_primary = true;
Ray Strode4532b242016-09-09 11:09:05 -0400746
747 ret = qxl_crtc_apply_cursor(crtc);
748 if (ret) {
749 DRM_ERROR("could not set cursor after modeset");
750 ret = 0;
751 }
David Mansfield52571ad2014-06-04 12:12:15 +1000752 }
753
754 if (bo->is_primary) {
755 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 +0100756 surf_id = 0;
757 } else {
758 surf_id = bo->surface_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000759 }
760
761 if (old_bo && old_bo != bo) {
762 old_bo->is_primary = false;
Dave Airlief64122c2013-02-25 14:47:55 +1000763 qxl_bo_unpin(old_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000764 }
765
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100766 qxl_monitors_config_set(qdev, qcrtc->index, x, y,
767 mode->hdisplay,
768 mode->vdisplay, surf_id);
Dave Airlief64122c2013-02-25 14:47:55 +1000769 return 0;
770}
771
772static void qxl_crtc_prepare(struct drm_crtc *crtc)
773{
774 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
775 crtc->mode.hdisplay, crtc->mode.vdisplay,
776 crtc->x, crtc->y, crtc->enabled);
777}
778
779static void qxl_crtc_commit(struct drm_crtc *crtc)
780{
781 DRM_DEBUG("\n");
782}
783
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100784static void qxl_crtc_disable(struct drm_crtc *crtc)
785{
786 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
787 struct drm_device *dev = crtc->dev;
788 struct qxl_device *qdev = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700789 if (crtc->primary->fb) {
790 struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100791 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300792
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100793 qxl_bo_unpin(bo);
Matt Roperf4510a22014-04-01 15:22:40 -0700794 crtc->primary->fb = NULL;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100795 }
796
797 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
798
799 qxl_send_monitors_config(qdev);
800}
801
Dave Airlief64122c2013-02-25 14:47:55 +1000802static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
803 .dpms = qxl_crtc_dpms,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100804 .disable = qxl_crtc_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000805 .mode_fixup = qxl_crtc_mode_fixup,
806 .mode_set = qxl_crtc_mode_set,
807 .prepare = qxl_crtc_prepare,
808 .commit = qxl_crtc_commit,
Dave Airlief64122c2013-02-25 14:47:55 +1000809};
810
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100811static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000812{
813 struct qxl_crtc *qxl_crtc;
814
815 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
816 if (!qxl_crtc)
817 return -ENOMEM;
818
819 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100820 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000821 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
822 return 0;
823}
824
825static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
826{
827 DRM_DEBUG("\n");
828}
829
Dave Airlief64122c2013-02-25 14:47:55 +1000830static void qxl_enc_prepare(struct drm_encoder *encoder)
831{
832 DRM_DEBUG("\n");
833}
834
835static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
836 struct drm_encoder *encoder)
837{
838 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100839 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000840 struct qxl_head *head;
841 struct drm_display_mode *mode;
842
843 BUG_ON(!encoder);
844 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100845 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000846 if (!qdev->monitors_config ||
847 qdev->monitors_config->max_allowed <= i) {
848 DRM_ERROR(
849 "head number too large or missing monitors config: %p, %d",
850 qdev->monitors_config,
851 qdev->monitors_config ?
852 qdev->monitors_config->max_allowed : -1);
853 return;
854 }
855 if (!encoder->crtc) {
856 DRM_ERROR("missing crtc on encoder %p\n", encoder);
857 return;
858 }
859 if (i != 0)
860 DRM_DEBUG("missing for multiple monitors: no head holes\n");
861 head = &qdev->monitors_config->heads[i];
862 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000863 if (encoder->crtc->enabled) {
864 mode = &encoder->crtc->mode;
865 head->width = mode->hdisplay;
866 head->height = mode->vdisplay;
867 head->x = encoder->crtc->x;
868 head->y = encoder->crtc->y;
869 if (qdev->monitors_config->count < i + 1)
870 qdev->monitors_config->count = i + 1;
871 } else {
872 head->width = 0;
873 head->height = 0;
874 head->x = 0;
875 head->y = 0;
876 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100877 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
878 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000879 head->flags = 0;
880 /* TODO - somewhere else to call this for multiple monitors
881 * (config_commit?) */
882 qxl_send_monitors_config(qdev);
883}
884
885static void qxl_enc_commit(struct drm_encoder *encoder)
886{
887 struct qxl_device *qdev = encoder->dev->dev_private;
888
889 qxl_write_monitors_config_for_encoder(qdev, encoder);
890 DRM_DEBUG("\n");
891}
892
893static void qxl_enc_mode_set(struct drm_encoder *encoder,
894 struct drm_display_mode *mode,
895 struct drm_display_mode *adjusted_mode)
896{
897 DRM_DEBUG("\n");
898}
899
900static int qxl_conn_get_modes(struct drm_connector *connector)
901{
902 int ret = 0;
903 struct qxl_device *qdev = connector->dev->dev_private;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200904 unsigned pwidth = 1024;
905 unsigned pheight = 768;
Dave Airlief64122c2013-02-25 14:47:55 +1000906
907 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
908 /* TODO: what should we do here? only show the configured modes for the
909 * device, or allow the full list, or both? */
910 if (qdev->monitors_config && qdev->monitors_config->count) {
Marc-André Lureaub0807422013-10-18 16:11:31 +0200911 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000912 if (ret < 0)
913 return ret;
914 }
Marc-André Lureaub0807422013-10-18 16:11:31 +0200915 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000916 return ret;
917}
918
919static int qxl_conn_mode_valid(struct drm_connector *connector,
920 struct drm_display_mode *mode)
921{
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500922 struct drm_device *ddev = connector->dev;
923 struct qxl_device *qdev = ddev->dev_private;
924 int i;
925
Dave Airlief64122c2013-02-25 14:47:55 +1000926 /* TODO: is this called for user defined modes? (xrandr --add-mode)
927 * TODO: check that the mode fits in the framebuffer */
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500928
929 if(qdev->monitors_config_width == mode->hdisplay &&
930 qdev->monitors_config_height == mode->vdisplay)
931 return MODE_OK;
932
933 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
934 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
935 return MODE_OK;
936 }
937 return MODE_BAD;
Dave Airlief64122c2013-02-25 14:47:55 +1000938}
939
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000940static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +1000941{
942 struct qxl_output *qxl_output =
943 drm_connector_to_qxl_output(connector);
944
945 DRM_DEBUG("\n");
946 return &qxl_output->enc;
947}
948
949
950static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
951 .dpms = qxl_enc_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +1000952 .prepare = qxl_enc_prepare,
953 .mode_set = qxl_enc_mode_set,
954 .commit = qxl_enc_commit,
955};
956
957static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
958 .get_modes = qxl_conn_get_modes,
959 .mode_valid = qxl_conn_mode_valid,
960 .best_encoder = qxl_best_encoder,
961};
962
Dave Airlief64122c2013-02-25 14:47:55 +1000963static enum drm_connector_status qxl_conn_detect(
964 struct drm_connector *connector,
965 bool force)
966{
967 struct qxl_output *output =
968 drm_connector_to_qxl_output(connector);
969 struct drm_device *ddev = connector->dev;
970 struct qxl_device *qdev = ddev->dev_private;
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000971 bool connected = false;
Dave Airlief64122c2013-02-25 14:47:55 +1000972
973 /* The first monitor is always connected */
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000974 if (!qdev->client_monitors_config) {
975 if (output->index == 0)
976 connected = true;
977 } else
978 connected = qdev->client_monitors_config->count > output->index &&
979 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
Dave Airlief64122c2013-02-25 14:47:55 +1000980
Marc-André Lureau5cab51c2013-10-18 16:11:33 +0200981 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
982 if (!connected)
983 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
984
Dave Airlief64122c2013-02-25 14:47:55 +1000985 return connected ? connector_status_connected
986 : connector_status_disconnected;
987}
988
989static int qxl_conn_set_property(struct drm_connector *connector,
990 struct drm_property *property,
991 uint64_t value)
992{
993 DRM_DEBUG("\n");
994 return 0;
995}
996
997static void qxl_conn_destroy(struct drm_connector *connector)
998{
999 struct qxl_output *qxl_output =
1000 drm_connector_to_qxl_output(connector);
1001
Thomas Wood34ea3d32014-05-29 16:57:41 +01001002 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001003 drm_connector_cleanup(connector);
1004 kfree(qxl_output);
1005}
1006
1007static const struct drm_connector_funcs qxl_connector_funcs = {
1008 .dpms = drm_helper_connector_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001009 .detect = qxl_conn_detect,
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001010 .fill_modes = drm_helper_probe_single_connector_modes,
Dave Airlief64122c2013-02-25 14:47:55 +10001011 .set_property = qxl_conn_set_property,
1012 .destroy = qxl_conn_destroy,
1013};
1014
1015static void qxl_enc_destroy(struct drm_encoder *encoder)
1016{
1017 drm_encoder_cleanup(encoder);
1018}
1019
1020static const struct drm_encoder_funcs qxl_enc_funcs = {
1021 .destroy = qxl_enc_destroy,
1022};
1023
Dave Airlie4695b032013-10-11 11:05:00 +10001024static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1025{
1026 if (qdev->hotplug_mode_update_property)
1027 return 0;
1028
1029 qdev->hotplug_mode_update_property =
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001030 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlie4695b032013-10-11 11:05:00 +10001031 "hotplug_mode_update", 0, 1);
1032
1033 return 0;
1034}
1035
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001036static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +10001037{
Dave Airlie4695b032013-10-11 11:05:00 +10001038 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +10001039 struct qxl_output *qxl_output;
1040 struct drm_connector *connector;
1041 struct drm_encoder *encoder;
1042
1043 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1044 if (!qxl_output)
1045 return -ENOMEM;
1046
1047 qxl_output->index = num_output;
1048
1049 connector = &qxl_output->base;
1050 encoder = &qxl_output->enc;
1051 drm_connector_init(dev, &qxl_output->base,
1052 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1053
1054 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001055 DRM_MODE_ENCODER_VIRTUAL, NULL);
Dave Airlief64122c2013-02-25 14:47:55 +10001056
Dave Airlie5ff91e42013-07-05 10:20:33 +10001057 /* we get HPD via client monitors config */
1058 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +10001059 encoder->possible_crtcs = 1 << num_output;
1060 drm_mode_connector_attach_encoder(&qxl_output->base,
1061 &qxl_output->enc);
1062 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1063 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1064
Dave Airlie4695b032013-10-11 11:05:00 +10001065 drm_object_attach_property(&connector->base,
1066 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +10001067 drm_object_attach_property(&connector->base,
1068 dev->mode_config.suggested_x_property, 0);
1069 drm_object_attach_property(&connector->base,
1070 dev->mode_config.suggested_y_property, 0);
Dave Airlief64122c2013-02-25 14:47:55 +10001071 return 0;
1072}
1073
1074static struct drm_framebuffer *
1075qxl_user_framebuffer_create(struct drm_device *dev,
1076 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02001077 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief64122c2013-02-25 14:47:55 +10001078{
1079 struct drm_gem_object *obj;
1080 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +10001081 int ret;
1082
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001083 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1084 if (!obj)
1085 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +10001086
1087 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1088 if (qxl_fb == NULL)
1089 return NULL;
1090
Noralf Trønnes6819c3c2016-04-28 17:18:36 +02001091 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +10001092 if (ret) {
1093 kfree(qxl_fb);
1094 drm_gem_object_unreference_unlocked(obj);
1095 return NULL;
1096 }
1097
Dave Airlief64122c2013-02-25 14:47:55 +10001098 return &qxl_fb->base;
1099}
1100
1101static const struct drm_mode_config_funcs qxl_mode_funcs = {
1102 .fb_create = qxl_user_framebuffer_create,
1103};
1104
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001105int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001106{
Dave Airlief64122c2013-02-25 14:47:55 +10001107 int ret;
1108 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001109 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001110 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001111 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001112
Dave Airlief64122c2013-02-25 14:47:55 +10001113 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1114 QXL_GEM_DOMAIN_VRAM,
1115 false, false, NULL, &gobj);
1116 if (ret) {
1117 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1118 return -ENOMEM;
1119 }
1120 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001121
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001122 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001123 if (ret)
1124 return ret;
1125
Dave Airlief64122c2013-02-25 14:47:55 +10001126 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001127
Dave Airlief64122c2013-02-25 14:47:55 +10001128 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1129 qdev->ram_header->monitors_config =
1130 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1131
1132 memset(qdev->monitors_config, 0, monitors_config_size);
1133 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001134 return 0;
1135}
1136
1137int qxl_destroy_monitors_object(struct qxl_device *qdev)
1138{
1139 int ret;
1140
1141 qdev->monitors_config = NULL;
1142 qdev->ram_header->monitors_config = 0;
1143
1144 qxl_bo_kunmap(qdev->monitors_config_bo);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001145 ret = qxl_bo_unpin(qdev->monitors_config_bo);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001146 if (ret)
1147 return ret;
1148
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001149 qxl_bo_unref(&qdev->monitors_config_bo);
1150 return 0;
1151}
1152
1153int qxl_modeset_init(struct qxl_device *qdev)
1154{
1155 int i;
1156 int ret;
1157
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001158 drm_mode_config_init(&qdev->ddev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001159
1160 ret = qxl_create_monitors_object(qdev);
1161 if (ret)
1162 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001163
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001164 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
Dave Airlief64122c2013-02-25 14:47:55 +10001165
1166 /* modes will be validated against the framebuffer size */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001167 qdev->ddev.mode_config.min_width = 320;
1168 qdev->ddev.mode_config.min_height = 200;
1169 qdev->ddev.mode_config.max_width = 8192;
1170 qdev->ddev.mode_config.max_height = 8192;
Dave Airlief64122c2013-02-25 14:47:55 +10001171
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001172 qdev->ddev.mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001173
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001174 drm_mode_create_suggested_offset_properties(&qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001175 qxl_mode_create_hotplug_mode_update_property(qdev);
1176
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001177 for (i = 0 ; i < qxl_num_crtc; ++i) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001178 qdev_crtc_init(&qdev->ddev, i);
1179 qdev_output_init(&qdev->ddev, i);
Dave Airlief64122c2013-02-25 14:47:55 +10001180 }
1181
1182 qdev->mode_info.mode_config_initialized = true;
1183
1184 /* primary surface must be created by this point, to allow
1185 * issuing command queue commands and having them read by
1186 * spice server. */
1187 qxl_fbdev_init(qdev);
1188 return 0;
1189}
1190
1191void qxl_modeset_fini(struct qxl_device *qdev)
1192{
1193 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001194
1195 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001196 if (qdev->mode_info.mode_config_initialized) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001197 drm_mode_config_cleanup(&qdev->ddev);
Dave Airlief64122c2013-02-25 14:47:55 +10001198 qdev->mode_info.mode_config_initialized = false;
1199 }
1200}