blob: 2ce805a7ce5e179bf21f2f2390136fd368175df6 [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
289 ret = qxl_bo_reserve(bo, false);
290 if (ret)
291 return ret;
Frediano Ziglio7eb99742015-09-24 14:25:14 +0100292 ret = qxl_bo_pin(bo, bo->type, NULL);
293 qxl_bo_unreserve(bo);
294 if (ret)
295 return ret;
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200296
297 qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
298 &norect, one_clip_rect, inc);
299
Gustavo Padovan078ace62016-06-06 11:41:43 -0300300 drm_crtc_vblank_get(crtc);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200301
302 if (event) {
303 spin_lock_irqsave(&dev->event_lock, flags);
Gustavo Padovan8a605222016-06-06 11:41:35 -0300304 drm_crtc_send_vblank_event(crtc, event);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200305 spin_unlock_irqrestore(&dev->event_lock, flags);
306 }
Gustavo Padovan078ace62016-06-06 11:41:43 -0300307 drm_crtc_vblank_put(crtc);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200308
Frediano Ziglio7eb99742015-09-24 14:25:14 +0100309 ret = qxl_bo_reserve(bo, false);
310 if (!ret) {
311 qxl_bo_unpin(bo);
312 qxl_bo_unreserve(bo);
313 }
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200314
315 return 0;
316}
317
Dave Airlie8002db62013-07-23 14:16:42 +1000318static int
Dave Airlief64122c2013-02-25 14:47:55 +1000319qxl_hide_cursor(struct qxl_device *qdev)
320{
321 struct qxl_release *release;
322 struct qxl_cursor_cmd *cmd;
323 int ret;
324
325 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
326 &release, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000327 if (ret)
328 return ret;
329
330 ret = qxl_release_reserve_list(release, true);
331 if (ret) {
332 qxl_release_free(qdev, release);
333 return ret;
334 }
Dave Airlief64122c2013-02-25 14:47:55 +1000335
336 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
337 cmd->type = QXL_CURSOR_HIDE;
338 qxl_release_unmap(qdev, release, &cmd->release_info);
339
Dave Airlief64122c2013-02-25 14:47:55 +1000340 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000341 qxl_release_fence_buffer_objects(release);
342 return 0;
Dave Airlief64122c2013-02-25 14:47:55 +1000343}
344
Ray Strode4532b242016-09-09 11:09:05 -0400345static int qxl_crtc_apply_cursor(struct drm_crtc *crtc)
346{
347 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
348 struct drm_device *dev = crtc->dev;
349 struct qxl_device *qdev = dev->dev_private;
350 struct qxl_cursor_cmd *cmd;
351 struct qxl_release *release;
352 int ret = 0;
353
354 if (!qcrtc->cursor_bo)
355 return 0;
356
357 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
358 QXL_RELEASE_CURSOR_CMD,
359 &release, NULL);
360 if (ret)
361 return ret;
362
363 ret = qxl_release_list_add(release, qcrtc->cursor_bo);
364 if (ret)
365 goto out_free_release;
366
367 ret = qxl_release_reserve_list(release, false);
368 if (ret)
369 goto out_free_release;
370
371 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
372 cmd->type = QXL_CURSOR_SET;
373 cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
374 cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
375
376 cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
377
378 cmd->u.set.visible = 1;
379 qxl_release_unmap(qdev, release, &cmd->release_info);
380
381 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
382 qxl_release_fence_buffer_objects(release);
383
384 return ret;
385
386out_free_release:
387 qxl_release_free(qdev, release);
388 return ret;
389}
390
Dave Airliec0a60802013-06-20 11:48:53 +1000391static int qxl_crtc_cursor_set2(struct drm_crtc *crtc,
392 struct drm_file *file_priv,
393 uint32_t handle,
394 uint32_t width,
395 uint32_t height, int32_t hot_x, int32_t hot_y)
Dave Airlief64122c2013-02-25 14:47:55 +1000396{
397 struct drm_device *dev = crtc->dev;
398 struct qxl_device *qdev = dev->dev_private;
399 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
400 struct drm_gem_object *obj;
401 struct qxl_cursor *cursor;
402 struct qxl_cursor_cmd *cmd;
403 struct qxl_bo *cursor_bo, *user_bo;
404 struct qxl_release *release;
405 void *user_ptr;
406
407 int size = 64*64*4;
408 int ret = 0;
Dave Airlie8002db62013-07-23 14:16:42 +1000409 if (!handle)
410 return qxl_hide_cursor(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +1000411
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100412 obj = drm_gem_object_lookup(file_priv, handle);
Dave Airlief64122c2013-02-25 14:47:55 +1000413 if (!obj) {
414 DRM_ERROR("cannot find cursor object\n");
415 return -ENOENT;
416 }
417
418 user_bo = gem_to_qxl_bo(obj);
419
420 ret = qxl_bo_reserve(user_bo, false);
421 if (ret)
422 goto out_unref;
423
424 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000425 qxl_bo_unreserve(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000426 if (ret)
Dave Airlie8002db62013-07-23 14:16:42 +1000427 goto out_unref;
Dave Airlief64122c2013-02-25 14:47:55 +1000428
429 ret = qxl_bo_kmap(user_bo, &user_ptr);
430 if (ret)
431 goto out_unpin;
432
433 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
434 QXL_RELEASE_CURSOR_CMD,
435 &release, NULL);
436 if (ret)
437 goto out_kunmap;
Dave Airlie8002db62013-07-23 14:16:42 +1000438
439 ret = qxl_alloc_bo_reserved(qdev, release, sizeof(struct qxl_cursor) + size,
440 &cursor_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000441 if (ret)
442 goto out_free_release;
Dave Airlie8002db62013-07-23 14:16:42 +1000443
444 ret = qxl_release_reserve_list(release, false);
Dave Airlief64122c2013-02-25 14:47:55 +1000445 if (ret)
446 goto out_free_bo;
447
Dave Airlie8002db62013-07-23 14:16:42 +1000448 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
449 if (ret)
450 goto out_backoff;
451
Dave Airlief64122c2013-02-25 14:47:55 +1000452 cursor->header.unique = 0;
453 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
454 cursor->header.width = 64;
455 cursor->header.height = 64;
Dave Airliec0a60802013-06-20 11:48:53 +1000456 cursor->header.hot_spot_x = hot_x;
457 cursor->header.hot_spot_y = hot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000458 cursor->data_size = size;
459 cursor->chunk.next_chunk = 0;
460 cursor->chunk.prev_chunk = 0;
461 cursor->chunk.data_size = size;
462
463 memcpy(cursor->chunk.data, user_ptr, size);
464
465 qxl_bo_kunmap(cursor_bo);
466
Dave Airlief64122c2013-02-25 14:47:55 +1000467 qxl_bo_kunmap(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000468
John Keepingd59a1f72015-11-18 11:17:25 +0000469 qcrtc->cur_x += qcrtc->hot_spot_x - hot_x;
470 qcrtc->cur_y += qcrtc->hot_spot_y - hot_y;
471 qcrtc->hot_spot_x = hot_x;
472 qcrtc->hot_spot_y = hot_y;
473
Dave Airlief64122c2013-02-25 14:47:55 +1000474 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
475 cmd->type = QXL_CURSOR_SET;
John Keepingd59a1f72015-11-18 11:17:25 +0000476 cmd->u.set.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
477 cmd->u.set.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000478
479 cmd->u.set.shape = qxl_bo_physical_address(qdev, cursor_bo, 0);
Dave Airlief64122c2013-02-25 14:47:55 +1000480
481 cmd->u.set.visible = 1;
482 qxl_release_unmap(qdev, release, &cmd->release_info);
483
Dave Airlief64122c2013-02-25 14:47:55 +1000484 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000485 qxl_release_fence_buffer_objects(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000486
Dave Airlie8002db62013-07-23 14:16:42 +1000487 /* finish with the userspace bo */
488 ret = qxl_bo_reserve(user_bo, false);
489 if (!ret) {
490 qxl_bo_unpin(user_bo);
491 qxl_bo_unreserve(user_bo);
492 }
493 drm_gem_object_unreference_unlocked(obj);
494
Ray Strode4532b242016-09-09 11:09:05 -0400495 qxl_bo_unref (&qcrtc->cursor_bo);
496 qcrtc->cursor_bo = cursor_bo;
Dave Airlief64122c2013-02-25 14:47:55 +1000497
498 return ret;
Dave Airlie8002db62013-07-23 14:16:42 +1000499
500out_backoff:
501 qxl_release_backoff_reserve_list(release);
Dave Airlief64122c2013-02-25 14:47:55 +1000502out_free_bo:
503 qxl_bo_unref(&cursor_bo);
504out_free_release:
Dave Airlief64122c2013-02-25 14:47:55 +1000505 qxl_release_free(qdev, release);
506out_kunmap:
507 qxl_bo_kunmap(user_bo);
508out_unpin:
509 qxl_bo_unpin(user_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000510out_unref:
511 drm_gem_object_unreference_unlocked(obj);
512 return ret;
513}
514
515static int qxl_crtc_cursor_move(struct drm_crtc *crtc,
516 int x, int y)
517{
518 struct drm_device *dev = crtc->dev;
519 struct qxl_device *qdev = dev->dev_private;
520 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
521 struct qxl_release *release;
522 struct qxl_cursor_cmd *cmd;
523 int ret;
524
525 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd), QXL_RELEASE_CURSOR_CMD,
526 &release, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000527 if (ret)
528 return ret;
529
530 ret = qxl_release_reserve_list(release, true);
531 if (ret) {
532 qxl_release_free(qdev, release);
533 return ret;
534 }
Dave Airlief64122c2013-02-25 14:47:55 +1000535
536 qcrtc->cur_x = x;
537 qcrtc->cur_y = y;
538
539 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
540 cmd->type = QXL_CURSOR_MOVE;
John Keepingd59a1f72015-11-18 11:17:25 +0000541 cmd->u.position.x = qcrtc->cur_x + qcrtc->hot_spot_x;
542 cmd->u.position.y = qcrtc->cur_y + qcrtc->hot_spot_y;
Dave Airlief64122c2013-02-25 14:47:55 +1000543 qxl_release_unmap(qdev, release, &cmd->release_info);
544
Dave Airlief64122c2013-02-25 14:47:55 +1000545 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
Dave Airlie8002db62013-07-23 14:16:42 +1000546 qxl_release_fence_buffer_objects(release);
547
Dave Airlief64122c2013-02-25 14:47:55 +1000548 return 0;
549}
550
Shawn Guo54d82e02017-02-07 17:16:27 +0800551static u32 qxl_noop_get_vblank_counter(struct drm_crtc *crtc)
552{
553 return 0;
554}
555
556static int qxl_noop_enable_vblank(struct drm_crtc *crtc)
557{
558 return 0;
559}
560
561static void qxl_noop_disable_vblank(struct drm_crtc *crtc)
562{
563}
Dave Airlief64122c2013-02-25 14:47:55 +1000564
565static const struct drm_crtc_funcs qxl_crtc_funcs = {
Dave Airliec0a60802013-06-20 11:48:53 +1000566 .cursor_set2 = qxl_crtc_cursor_set2,
Dave Airlief64122c2013-02-25 14:47:55 +1000567 .cursor_move = qxl_crtc_cursor_move,
Dave Airlief64122c2013-02-25 14:47:55 +1000568 .set_config = drm_crtc_helper_set_config,
569 .destroy = qxl_crtc_destroy,
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200570 .page_flip = qxl_crtc_page_flip,
Shawn Guo54d82e02017-02-07 17:16:27 +0800571 .get_vblank_counter = qxl_noop_get_vblank_counter,
572 .enable_vblank = qxl_noop_enable_vblank,
573 .disable_vblank = qxl_noop_disable_vblank,
Dave Airlief64122c2013-02-25 14:47:55 +1000574};
575
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200576void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
Dave Airlief64122c2013-02-25 14:47:55 +1000577{
578 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
579
Markus Elfringdc3583c2016-07-22 14:14:54 +0200580 drm_gem_object_unreference_unlocked(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000581 drm_framebuffer_cleanup(fb);
582 kfree(qxl_fb);
583}
584
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000585static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
586 struct drm_file *file_priv,
587 unsigned flags, unsigned color,
588 struct drm_clip_rect *clips,
589 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000590{
591 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
592 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
593 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
594 struct drm_clip_rect norect;
595 struct qxl_bo *qobj;
596 int inc = 1;
597
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200598 drm_modeset_lock_all(fb->dev);
599
Dave Airlief64122c2013-02-25 14:47:55 +1000600 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000601 /* if we aren't primary surface ignore this */
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200602 if (!qobj->is_primary) {
603 drm_modeset_unlock_all(fb->dev);
Dave Airlieb2b44652013-05-13 12:48:40 +1000604 return 0;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200605 }
Dave Airlieb2b44652013-05-13 12:48:40 +1000606
Dave Airlief64122c2013-02-25 14:47:55 +1000607 if (!num_clips) {
608 num_clips = 1;
609 clips = &norect;
610 norect.x1 = norect.y1 = 0;
611 norect.x2 = fb->width;
612 norect.y2 = fb->height;
613 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
614 num_clips /= 2;
615 inc = 2; /* skip source rects */
616 }
617
618 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
619 clips, num_clips, inc);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200620
621 drm_modeset_unlock_all(fb->dev);
622
Dave Airlief64122c2013-02-25 14:47:55 +1000623 return 0;
624}
625
626static const struct drm_framebuffer_funcs qxl_fb_funcs = {
627 .destroy = qxl_user_framebuffer_destroy,
628 .dirty = qxl_framebuffer_surface_dirty,
629/* TODO?
630 * .create_handle = qxl_user_framebuffer_create_handle, */
631};
632
633int
634qxl_framebuffer_init(struct drm_device *dev,
635 struct qxl_framebuffer *qfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200636 const struct drm_mode_fb_cmd2 *mode_cmd,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200637 struct drm_gem_object *obj,
638 const struct drm_framebuffer_funcs *funcs)
Dave Airlief64122c2013-02-25 14:47:55 +1000639{
640 int ret;
641
642 qfb->obj = obj;
Ville Syrjälä53609432016-11-18 21:52:51 +0200643 drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200644 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000645 if (ret) {
646 qfb->obj = NULL;
647 return ret;
648 }
Dave Airlief64122c2013-02-25 14:47:55 +1000649 return 0;
650}
651
652static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
653{
654}
655
656static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
657 const struct drm_display_mode *mode,
658 struct drm_display_mode *adjusted_mode)
659{
660 struct drm_device *dev = crtc->dev;
661 struct qxl_device *qdev = dev->dev_private;
662
663 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
664 __func__,
665 mode->hdisplay, mode->vdisplay,
666 adjusted_mode->hdisplay,
667 adjusted_mode->vdisplay);
668 return true;
669}
670
Christophe Fergeaue4a76442016-11-08 10:12:03 +0100671static void
Dave Airlief64122c2013-02-25 14:47:55 +1000672qxl_send_monitors_config(struct qxl_device *qdev)
673{
674 int i;
675
676 BUG_ON(!qdev->ram_header->monitors_config);
677
678 if (qdev->monitors_config->count == 0) {
679 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
680 return;
681 }
682 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
683 struct qxl_head *head = &qdev->monitors_config->heads[i];
684
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100685 if (head->y > 8192 || head->x > 8192 ||
Dave Airlief64122c2013-02-25 14:47:55 +1000686 head->width > 8192 || head->height > 8192) {
687 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
688 i, head->width, head->height,
689 head->x, head->y);
690 return;
691 }
692 }
693 qxl_io_monitors_config(qdev);
694}
695
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100696static void qxl_monitors_config_set(struct qxl_device *qdev,
697 int index,
698 unsigned x, unsigned y,
699 unsigned width, unsigned height,
700 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000701{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100702 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
703 qdev->monitors_config->heads[index].x = x;
704 qdev->monitors_config->heads[index].y = y;
705 qdev->monitors_config->heads[index].width = width;
706 qdev->monitors_config->heads[index].height = height;
707 qdev->monitors_config->heads[index].surface_id = surf_id;
708
Dave Airlief64122c2013-02-25 14:47:55 +1000709}
710
711static int qxl_crtc_mode_set(struct drm_crtc *crtc,
712 struct drm_display_mode *mode,
713 struct drm_display_mode *adjusted_mode,
714 int x, int y,
715 struct drm_framebuffer *old_fb)
716{
717 struct drm_device *dev = crtc->dev;
718 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +1000719 struct qxl_framebuffer *qfb;
720 struct qxl_bo *bo, *old_bo = NULL;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100721 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Dave Airlief64122c2013-02-25 14:47:55 +1000722 bool recreate_primary = false;
723 int ret;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100724 int surf_id;
Matt Roperf4510a22014-04-01 15:22:40 -0700725 if (!crtc->primary->fb) {
Dave Airlief64122c2013-02-25 14:47:55 +1000726 DRM_DEBUG_KMS("No FB bound\n");
727 return 0;
728 }
729
730 if (old_fb) {
731 qfb = to_qxl_framebuffer(old_fb);
732 old_bo = gem_to_qxl_bo(qfb->obj);
733 }
Matt Roperf4510a22014-04-01 15:22:40 -0700734 qfb = to_qxl_framebuffer(crtc->primary->fb);
Dave Airlief64122c2013-02-25 14:47:55 +1000735 bo = gem_to_qxl_bo(qfb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000736 DRM_DEBUG("+%d+%d (%d,%d) => (%d,%d)\n",
737 x, y,
738 mode->hdisplay, mode->vdisplay,
739 adjusted_mode->hdisplay,
740 adjusted_mode->vdisplay);
741
Fabiano FidĂªncio8d0d9402015-09-24 15:18:34 +0200742 if (bo->is_primary == false)
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100743 recreate_primary = true;
Dave Airlief64122c2013-02-25 14:47:55 +1000744
Marc-André Lureauc572aaf42014-10-16 11:39:44 +0200745 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
746 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
747 return -EINVAL;
748 }
Dave Airlief64122c2013-02-25 14:47:55 +1000749
750 ret = qxl_bo_reserve(bo, false);
751 if (ret != 0)
752 return ret;
753 ret = qxl_bo_pin(bo, bo->type, NULL);
754 if (ret != 0) {
755 qxl_bo_unreserve(bo);
756 return -EINVAL;
757 }
758 qxl_bo_unreserve(bo);
759 if (recreate_primary) {
760 qxl_io_destroy_primary(qdev);
761 qxl_io_log(qdev,
Marc-André Lureauc572aaf42014-10-16 11:39:44 +0200762 "recreate primary: %dx%d,%d,%d\n",
763 bo->surf.width, bo->surf.height,
764 bo->surf.stride, bo->surf.format);
765 qxl_io_create_primary(qdev, 0, bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000766 bo->is_primary = true;
Ray Strode4532b242016-09-09 11:09:05 -0400767
768 ret = qxl_crtc_apply_cursor(crtc);
769 if (ret) {
770 DRM_ERROR("could not set cursor after modeset");
771 ret = 0;
772 }
David Mansfield52571ad2014-06-04 12:12:15 +1000773 }
774
775 if (bo->is_primary) {
776 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 +0100777 surf_id = 0;
778 } else {
779 surf_id = bo->surface_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000780 }
781
782 if (old_bo && old_bo != bo) {
783 old_bo->is_primary = false;
784 ret = qxl_bo_reserve(old_bo, false);
785 qxl_bo_unpin(old_bo);
786 qxl_bo_unreserve(old_bo);
787 }
788
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100789 qxl_monitors_config_set(qdev, qcrtc->index, x, y,
790 mode->hdisplay,
791 mode->vdisplay, surf_id);
Dave Airlief64122c2013-02-25 14:47:55 +1000792 return 0;
793}
794
795static void qxl_crtc_prepare(struct drm_crtc *crtc)
796{
797 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
798 crtc->mode.hdisplay, crtc->mode.vdisplay,
799 crtc->x, crtc->y, crtc->enabled);
800}
801
802static void qxl_crtc_commit(struct drm_crtc *crtc)
803{
804 DRM_DEBUG("\n");
805}
806
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100807static void qxl_crtc_disable(struct drm_crtc *crtc)
808{
809 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
810 struct drm_device *dev = crtc->dev;
811 struct qxl_device *qdev = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700812 if (crtc->primary->fb) {
813 struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100814 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
815 int ret;
816 ret = qxl_bo_reserve(bo, false);
817 qxl_bo_unpin(bo);
818 qxl_bo_unreserve(bo);
Matt Roperf4510a22014-04-01 15:22:40 -0700819 crtc->primary->fb = NULL;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100820 }
821
822 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
823
824 qxl_send_monitors_config(qdev);
825}
826
Dave Airlief64122c2013-02-25 14:47:55 +1000827static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
828 .dpms = qxl_crtc_dpms,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100829 .disable = qxl_crtc_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000830 .mode_fixup = qxl_crtc_mode_fixup,
831 .mode_set = qxl_crtc_mode_set,
832 .prepare = qxl_crtc_prepare,
833 .commit = qxl_crtc_commit,
Dave Airlief64122c2013-02-25 14:47:55 +1000834};
835
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100836static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000837{
838 struct qxl_crtc *qxl_crtc;
839
840 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
841 if (!qxl_crtc)
842 return -ENOMEM;
843
844 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100845 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000846 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
847 return 0;
848}
849
850static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
851{
852 DRM_DEBUG("\n");
853}
854
Dave Airlief64122c2013-02-25 14:47:55 +1000855static void qxl_enc_prepare(struct drm_encoder *encoder)
856{
857 DRM_DEBUG("\n");
858}
859
860static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
861 struct drm_encoder *encoder)
862{
863 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100864 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000865 struct qxl_head *head;
866 struct drm_display_mode *mode;
867
868 BUG_ON(!encoder);
869 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100870 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000871 if (!qdev->monitors_config ||
872 qdev->monitors_config->max_allowed <= i) {
873 DRM_ERROR(
874 "head number too large or missing monitors config: %p, %d",
875 qdev->monitors_config,
876 qdev->monitors_config ?
877 qdev->monitors_config->max_allowed : -1);
878 return;
879 }
880 if (!encoder->crtc) {
881 DRM_ERROR("missing crtc on encoder %p\n", encoder);
882 return;
883 }
884 if (i != 0)
885 DRM_DEBUG("missing for multiple monitors: no head holes\n");
886 head = &qdev->monitors_config->heads[i];
887 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000888 if (encoder->crtc->enabled) {
889 mode = &encoder->crtc->mode;
890 head->width = mode->hdisplay;
891 head->height = mode->vdisplay;
892 head->x = encoder->crtc->x;
893 head->y = encoder->crtc->y;
894 if (qdev->monitors_config->count < i + 1)
895 qdev->monitors_config->count = i + 1;
896 } else {
897 head->width = 0;
898 head->height = 0;
899 head->x = 0;
900 head->y = 0;
901 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100902 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
903 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000904 head->flags = 0;
905 /* TODO - somewhere else to call this for multiple monitors
906 * (config_commit?) */
907 qxl_send_monitors_config(qdev);
908}
909
910static void qxl_enc_commit(struct drm_encoder *encoder)
911{
912 struct qxl_device *qdev = encoder->dev->dev_private;
913
914 qxl_write_monitors_config_for_encoder(qdev, encoder);
915 DRM_DEBUG("\n");
916}
917
918static void qxl_enc_mode_set(struct drm_encoder *encoder,
919 struct drm_display_mode *mode,
920 struct drm_display_mode *adjusted_mode)
921{
922 DRM_DEBUG("\n");
923}
924
925static int qxl_conn_get_modes(struct drm_connector *connector)
926{
927 int ret = 0;
928 struct qxl_device *qdev = connector->dev->dev_private;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200929 unsigned pwidth = 1024;
930 unsigned pheight = 768;
Dave Airlief64122c2013-02-25 14:47:55 +1000931
932 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
933 /* TODO: what should we do here? only show the configured modes for the
934 * device, or allow the full list, or both? */
935 if (qdev->monitors_config && qdev->monitors_config->count) {
Marc-André Lureaub0807422013-10-18 16:11:31 +0200936 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000937 if (ret < 0)
938 return ret;
939 }
Marc-André Lureaub0807422013-10-18 16:11:31 +0200940 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000941 return ret;
942}
943
944static int qxl_conn_mode_valid(struct drm_connector *connector,
945 struct drm_display_mode *mode)
946{
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500947 struct drm_device *ddev = connector->dev;
948 struct qxl_device *qdev = ddev->dev_private;
949 int i;
950
Dave Airlief64122c2013-02-25 14:47:55 +1000951 /* TODO: is this called for user defined modes? (xrandr --add-mode)
952 * TODO: check that the mode fits in the framebuffer */
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500953
954 if(qdev->monitors_config_width == mode->hdisplay &&
955 qdev->monitors_config_height == mode->vdisplay)
956 return MODE_OK;
957
958 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
959 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
960 return MODE_OK;
961 }
962 return MODE_BAD;
Dave Airlief64122c2013-02-25 14:47:55 +1000963}
964
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000965static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +1000966{
967 struct qxl_output *qxl_output =
968 drm_connector_to_qxl_output(connector);
969
970 DRM_DEBUG("\n");
971 return &qxl_output->enc;
972}
973
974
975static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
976 .dpms = qxl_enc_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +1000977 .prepare = qxl_enc_prepare,
978 .mode_set = qxl_enc_mode_set,
979 .commit = qxl_enc_commit,
980};
981
982static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
983 .get_modes = qxl_conn_get_modes,
984 .mode_valid = qxl_conn_mode_valid,
985 .best_encoder = qxl_best_encoder,
986};
987
Dave Airlief64122c2013-02-25 14:47:55 +1000988static enum drm_connector_status qxl_conn_detect(
989 struct drm_connector *connector,
990 bool force)
991{
992 struct qxl_output *output =
993 drm_connector_to_qxl_output(connector);
994 struct drm_device *ddev = connector->dev;
995 struct qxl_device *qdev = ddev->dev_private;
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000996 bool connected = false;
Dave Airlief64122c2013-02-25 14:47:55 +1000997
998 /* The first monitor is always connected */
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000999 if (!qdev->client_monitors_config) {
1000 if (output->index == 0)
1001 connected = true;
1002 } else
1003 connected = qdev->client_monitors_config->count > output->index &&
1004 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
Dave Airlief64122c2013-02-25 14:47:55 +10001005
Marc-André Lureau5cab51c2013-10-18 16:11:33 +02001006 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1007 if (!connected)
1008 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
1009
Dave Airlief64122c2013-02-25 14:47:55 +10001010 return connected ? connector_status_connected
1011 : connector_status_disconnected;
1012}
1013
1014static int qxl_conn_set_property(struct drm_connector *connector,
1015 struct drm_property *property,
1016 uint64_t value)
1017{
1018 DRM_DEBUG("\n");
1019 return 0;
1020}
1021
1022static void qxl_conn_destroy(struct drm_connector *connector)
1023{
1024 struct qxl_output *qxl_output =
1025 drm_connector_to_qxl_output(connector);
1026
Thomas Wood34ea3d32014-05-29 16:57:41 +01001027 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001028 drm_connector_cleanup(connector);
1029 kfree(qxl_output);
1030}
1031
1032static const struct drm_connector_funcs qxl_connector_funcs = {
1033 .dpms = drm_helper_connector_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001034 .detect = qxl_conn_detect,
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001035 .fill_modes = drm_helper_probe_single_connector_modes,
Dave Airlief64122c2013-02-25 14:47:55 +10001036 .set_property = qxl_conn_set_property,
1037 .destroy = qxl_conn_destroy,
1038};
1039
1040static void qxl_enc_destroy(struct drm_encoder *encoder)
1041{
1042 drm_encoder_cleanup(encoder);
1043}
1044
1045static const struct drm_encoder_funcs qxl_enc_funcs = {
1046 .destroy = qxl_enc_destroy,
1047};
1048
Dave Airlie4695b032013-10-11 11:05:00 +10001049static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1050{
1051 if (qdev->hotplug_mode_update_property)
1052 return 0;
1053
1054 qdev->hotplug_mode_update_property =
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001055 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlie4695b032013-10-11 11:05:00 +10001056 "hotplug_mode_update", 0, 1);
1057
1058 return 0;
1059}
1060
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001061static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +10001062{
Dave Airlie4695b032013-10-11 11:05:00 +10001063 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +10001064 struct qxl_output *qxl_output;
1065 struct drm_connector *connector;
1066 struct drm_encoder *encoder;
1067
1068 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1069 if (!qxl_output)
1070 return -ENOMEM;
1071
1072 qxl_output->index = num_output;
1073
1074 connector = &qxl_output->base;
1075 encoder = &qxl_output->enc;
1076 drm_connector_init(dev, &qxl_output->base,
1077 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1078
1079 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001080 DRM_MODE_ENCODER_VIRTUAL, NULL);
Dave Airlief64122c2013-02-25 14:47:55 +10001081
Dave Airlie5ff91e42013-07-05 10:20:33 +10001082 /* we get HPD via client monitors config */
1083 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +10001084 encoder->possible_crtcs = 1 << num_output;
1085 drm_mode_connector_attach_encoder(&qxl_output->base,
1086 &qxl_output->enc);
1087 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1088 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1089
Dave Airlie4695b032013-10-11 11:05:00 +10001090 drm_object_attach_property(&connector->base,
1091 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +10001092 drm_object_attach_property(&connector->base,
1093 dev->mode_config.suggested_x_property, 0);
1094 drm_object_attach_property(&connector->base,
1095 dev->mode_config.suggested_y_property, 0);
Dave Airlief64122c2013-02-25 14:47:55 +10001096 return 0;
1097}
1098
1099static struct drm_framebuffer *
1100qxl_user_framebuffer_create(struct drm_device *dev,
1101 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02001102 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief64122c2013-02-25 14:47:55 +10001103{
1104 struct drm_gem_object *obj;
1105 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +10001106 int ret;
1107
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001108 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1109 if (!obj)
1110 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +10001111
1112 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1113 if (qxl_fb == NULL)
1114 return NULL;
1115
Noralf Trønnes6819c3c2016-04-28 17:18:36 +02001116 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +10001117 if (ret) {
1118 kfree(qxl_fb);
1119 drm_gem_object_unreference_unlocked(obj);
1120 return NULL;
1121 }
1122
Dave Airlief64122c2013-02-25 14:47:55 +10001123 return &qxl_fb->base;
1124}
1125
1126static const struct drm_mode_config_funcs qxl_mode_funcs = {
1127 .fb_create = qxl_user_framebuffer_create,
1128};
1129
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001130int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001131{
Dave Airlief64122c2013-02-25 14:47:55 +10001132 int ret;
1133 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001134 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001135 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001136 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001137
Dave Airlief64122c2013-02-25 14:47:55 +10001138 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1139 QXL_GEM_DOMAIN_VRAM,
1140 false, false, NULL, &gobj);
1141 if (ret) {
1142 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1143 return -ENOMEM;
1144 }
1145 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001146
1147 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1148 if (ret)
1149 return ret;
1150
1151 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
1152 if (ret) {
1153 qxl_bo_unreserve(qdev->monitors_config_bo);
1154 return ret;
1155 }
1156
1157 qxl_bo_unreserve(qdev->monitors_config_bo);
1158
Dave Airlief64122c2013-02-25 14:47:55 +10001159 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001160
Dave Airlief64122c2013-02-25 14:47:55 +10001161 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1162 qdev->ram_header->monitors_config =
1163 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1164
1165 memset(qdev->monitors_config, 0, monitors_config_size);
1166 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001167 return 0;
1168}
1169
1170int qxl_destroy_monitors_object(struct qxl_device *qdev)
1171{
1172 int ret;
1173
1174 qdev->monitors_config = NULL;
1175 qdev->ram_header->monitors_config = 0;
1176
1177 qxl_bo_kunmap(qdev->monitors_config_bo);
1178 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1179 if (ret)
1180 return ret;
1181
1182 qxl_bo_unpin(qdev->monitors_config_bo);
1183 qxl_bo_unreserve(qdev->monitors_config_bo);
1184
1185 qxl_bo_unref(&qdev->monitors_config_bo);
1186 return 0;
1187}
1188
1189int qxl_modeset_init(struct qxl_device *qdev)
1190{
1191 int i;
1192 int ret;
1193
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001194 drm_mode_config_init(&qdev->ddev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001195
1196 ret = qxl_create_monitors_object(qdev);
1197 if (ret)
1198 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001199
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001200 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
Dave Airlief64122c2013-02-25 14:47:55 +10001201
1202 /* modes will be validated against the framebuffer size */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001203 qdev->ddev.mode_config.min_width = 320;
1204 qdev->ddev.mode_config.min_height = 200;
1205 qdev->ddev.mode_config.max_width = 8192;
1206 qdev->ddev.mode_config.max_height = 8192;
Dave Airlief64122c2013-02-25 14:47:55 +10001207
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001208 qdev->ddev.mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001209
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001210 drm_mode_create_suggested_offset_properties(&qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001211 qxl_mode_create_hotplug_mode_update_property(qdev);
1212
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001213 for (i = 0 ; i < qxl_num_crtc; ++i) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001214 qdev_crtc_init(&qdev->ddev, i);
1215 qdev_output_init(&qdev->ddev, i);
Dave Airlief64122c2013-02-25 14:47:55 +10001216 }
1217
1218 qdev->mode_info.mode_config_initialized = true;
1219
1220 /* primary surface must be created by this point, to allow
1221 * issuing command queue commands and having them read by
1222 * spice server. */
1223 qxl_fbdev_init(qdev);
1224 return 0;
1225}
1226
1227void qxl_modeset_fini(struct qxl_device *qdev)
1228{
1229 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001230
1231 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001232 if (qdev->mode_info.mode_config_initialized) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001233 drm_mode_config_cleanup(&qdev->ddev);
Dave Airlief64122c2013-02-25 14:47:55 +10001234 qdev->mode_info.mode_config_initialized = false;
1235 }
1236}