blob: 1094cd33eb06e9d78009c4fc91c0423da6df1182 [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
551
552static const struct drm_crtc_funcs qxl_crtc_funcs = {
Dave Airliec0a60802013-06-20 11:48:53 +1000553 .cursor_set2 = qxl_crtc_cursor_set2,
Dave Airlief64122c2013-02-25 14:47:55 +1000554 .cursor_move = qxl_crtc_cursor_move,
Dave Airlief64122c2013-02-25 14:47:55 +1000555 .set_config = drm_crtc_helper_set_config,
556 .destroy = qxl_crtc_destroy,
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200557 .page_flip = qxl_crtc_page_flip,
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
734 ret = qxl_bo_reserve(bo, false);
735 if (ret != 0)
736 return ret;
737 ret = qxl_bo_pin(bo, bo->type, NULL);
738 if (ret != 0) {
739 qxl_bo_unreserve(bo);
740 return -EINVAL;
741 }
742 qxl_bo_unreserve(bo);
743 if (recreate_primary) {
744 qxl_io_destroy_primary(qdev);
745 qxl_io_log(qdev,
Marc-André Lureauc572aaf42014-10-16 11:39:44 +0200746 "recreate primary: %dx%d,%d,%d\n",
747 bo->surf.width, bo->surf.height,
748 bo->surf.stride, bo->surf.format);
749 qxl_io_create_primary(qdev, 0, bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000750 bo->is_primary = true;
Ray Strode4532b242016-09-09 11:09:05 -0400751
752 ret = qxl_crtc_apply_cursor(crtc);
753 if (ret) {
754 DRM_ERROR("could not set cursor after modeset");
755 ret = 0;
756 }
David Mansfield52571ad2014-06-04 12:12:15 +1000757 }
758
759 if (bo->is_primary) {
760 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 +0100761 surf_id = 0;
762 } else {
763 surf_id = bo->surface_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000764 }
765
766 if (old_bo && old_bo != bo) {
767 old_bo->is_primary = false;
768 ret = qxl_bo_reserve(old_bo, false);
769 qxl_bo_unpin(old_bo);
770 qxl_bo_unreserve(old_bo);
771 }
772
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100773 qxl_monitors_config_set(qdev, qcrtc->index, x, y,
774 mode->hdisplay,
775 mode->vdisplay, surf_id);
Dave Airlief64122c2013-02-25 14:47:55 +1000776 return 0;
777}
778
779static void qxl_crtc_prepare(struct drm_crtc *crtc)
780{
781 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
782 crtc->mode.hdisplay, crtc->mode.vdisplay,
783 crtc->x, crtc->y, crtc->enabled);
784}
785
786static void qxl_crtc_commit(struct drm_crtc *crtc)
787{
788 DRM_DEBUG("\n");
789}
790
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100791static void qxl_crtc_disable(struct drm_crtc *crtc)
792{
793 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
794 struct drm_device *dev = crtc->dev;
795 struct qxl_device *qdev = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -0700796 if (crtc->primary->fb) {
797 struct qxl_framebuffer *qfb = to_qxl_framebuffer(crtc->primary->fb);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100798 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
799 int ret;
800 ret = qxl_bo_reserve(bo, false);
801 qxl_bo_unpin(bo);
802 qxl_bo_unreserve(bo);
Matt Roperf4510a22014-04-01 15:22:40 -0700803 crtc->primary->fb = NULL;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100804 }
805
806 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
807
808 qxl_send_monitors_config(qdev);
809}
810
Dave Airlief64122c2013-02-25 14:47:55 +1000811static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
812 .dpms = qxl_crtc_dpms,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100813 .disable = qxl_crtc_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000814 .mode_fixup = qxl_crtc_mode_fixup,
815 .mode_set = qxl_crtc_mode_set,
816 .prepare = qxl_crtc_prepare,
817 .commit = qxl_crtc_commit,
Dave Airlief64122c2013-02-25 14:47:55 +1000818};
819
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100820static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000821{
822 struct qxl_crtc *qxl_crtc;
823
824 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
825 if (!qxl_crtc)
826 return -ENOMEM;
827
828 drm_crtc_init(dev, &qxl_crtc->base, &qxl_crtc_funcs);
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100829 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000830 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
831 return 0;
832}
833
834static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
835{
836 DRM_DEBUG("\n");
837}
838
Dave Airlief64122c2013-02-25 14:47:55 +1000839static void qxl_enc_prepare(struct drm_encoder *encoder)
840{
841 DRM_DEBUG("\n");
842}
843
844static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
845 struct drm_encoder *encoder)
846{
847 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100848 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000849 struct qxl_head *head;
850 struct drm_display_mode *mode;
851
852 BUG_ON(!encoder);
853 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100854 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000855 if (!qdev->monitors_config ||
856 qdev->monitors_config->max_allowed <= i) {
857 DRM_ERROR(
858 "head number too large or missing monitors config: %p, %d",
859 qdev->monitors_config,
860 qdev->monitors_config ?
861 qdev->monitors_config->max_allowed : -1);
862 return;
863 }
864 if (!encoder->crtc) {
865 DRM_ERROR("missing crtc on encoder %p\n", encoder);
866 return;
867 }
868 if (i != 0)
869 DRM_DEBUG("missing for multiple monitors: no head holes\n");
870 head = &qdev->monitors_config->heads[i];
871 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000872 if (encoder->crtc->enabled) {
873 mode = &encoder->crtc->mode;
874 head->width = mode->hdisplay;
875 head->height = mode->vdisplay;
876 head->x = encoder->crtc->x;
877 head->y = encoder->crtc->y;
878 if (qdev->monitors_config->count < i + 1)
879 qdev->monitors_config->count = i + 1;
880 } else {
881 head->width = 0;
882 head->height = 0;
883 head->x = 0;
884 head->y = 0;
885 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100886 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
887 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000888 head->flags = 0;
889 /* TODO - somewhere else to call this for multiple monitors
890 * (config_commit?) */
891 qxl_send_monitors_config(qdev);
892}
893
894static void qxl_enc_commit(struct drm_encoder *encoder)
895{
896 struct qxl_device *qdev = encoder->dev->dev_private;
897
898 qxl_write_monitors_config_for_encoder(qdev, encoder);
899 DRM_DEBUG("\n");
900}
901
902static void qxl_enc_mode_set(struct drm_encoder *encoder,
903 struct drm_display_mode *mode,
904 struct drm_display_mode *adjusted_mode)
905{
906 DRM_DEBUG("\n");
907}
908
909static int qxl_conn_get_modes(struct drm_connector *connector)
910{
911 int ret = 0;
912 struct qxl_device *qdev = connector->dev->dev_private;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200913 unsigned pwidth = 1024;
914 unsigned pheight = 768;
Dave Airlief64122c2013-02-25 14:47:55 +1000915
916 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
917 /* TODO: what should we do here? only show the configured modes for the
918 * device, or allow the full list, or both? */
919 if (qdev->monitors_config && qdev->monitors_config->count) {
Marc-André Lureaub0807422013-10-18 16:11:31 +0200920 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000921 if (ret < 0)
922 return ret;
923 }
Marc-André Lureaub0807422013-10-18 16:11:31 +0200924 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000925 return ret;
926}
927
928static int qxl_conn_mode_valid(struct drm_connector *connector,
929 struct drm_display_mode *mode)
930{
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500931 struct drm_device *ddev = connector->dev;
932 struct qxl_device *qdev = ddev->dev_private;
933 int i;
934
Dave Airlief64122c2013-02-25 14:47:55 +1000935 /* TODO: is this called for user defined modes? (xrandr --add-mode)
936 * TODO: check that the mode fits in the framebuffer */
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500937
938 if(qdev->monitors_config_width == mode->hdisplay &&
939 qdev->monitors_config_height == mode->vdisplay)
940 return MODE_OK;
941
942 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
943 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
944 return MODE_OK;
945 }
946 return MODE_BAD;
Dave Airlief64122c2013-02-25 14:47:55 +1000947}
948
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000949static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +1000950{
951 struct qxl_output *qxl_output =
952 drm_connector_to_qxl_output(connector);
953
954 DRM_DEBUG("\n");
955 return &qxl_output->enc;
956}
957
958
959static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
960 .dpms = qxl_enc_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +1000961 .prepare = qxl_enc_prepare,
962 .mode_set = qxl_enc_mode_set,
963 .commit = qxl_enc_commit,
964};
965
966static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
967 .get_modes = qxl_conn_get_modes,
968 .mode_valid = qxl_conn_mode_valid,
969 .best_encoder = qxl_best_encoder,
970};
971
Dave Airlief64122c2013-02-25 14:47:55 +1000972static enum drm_connector_status qxl_conn_detect(
973 struct drm_connector *connector,
974 bool force)
975{
976 struct qxl_output *output =
977 drm_connector_to_qxl_output(connector);
978 struct drm_device *ddev = connector->dev;
979 struct qxl_device *qdev = ddev->dev_private;
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000980 bool connected = false;
Dave Airlief64122c2013-02-25 14:47:55 +1000981
982 /* The first monitor is always connected */
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000983 if (!qdev->client_monitors_config) {
984 if (output->index == 0)
985 connected = true;
986 } else
987 connected = qdev->client_monitors_config->count > output->index &&
988 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
Dave Airlief64122c2013-02-25 14:47:55 +1000989
Marc-André Lureau5cab51c2013-10-18 16:11:33 +0200990 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
991 if (!connected)
992 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
993
Dave Airlief64122c2013-02-25 14:47:55 +1000994 return connected ? connector_status_connected
995 : connector_status_disconnected;
996}
997
998static int qxl_conn_set_property(struct drm_connector *connector,
999 struct drm_property *property,
1000 uint64_t value)
1001{
1002 DRM_DEBUG("\n");
1003 return 0;
1004}
1005
1006static void qxl_conn_destroy(struct drm_connector *connector)
1007{
1008 struct qxl_output *qxl_output =
1009 drm_connector_to_qxl_output(connector);
1010
Thomas Wood34ea3d32014-05-29 16:57:41 +01001011 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001012 drm_connector_cleanup(connector);
1013 kfree(qxl_output);
1014}
1015
1016static const struct drm_connector_funcs qxl_connector_funcs = {
1017 .dpms = drm_helper_connector_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001018 .detect = qxl_conn_detect,
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001019 .fill_modes = drm_helper_probe_single_connector_modes,
Dave Airlief64122c2013-02-25 14:47:55 +10001020 .set_property = qxl_conn_set_property,
1021 .destroy = qxl_conn_destroy,
1022};
1023
1024static void qxl_enc_destroy(struct drm_encoder *encoder)
1025{
1026 drm_encoder_cleanup(encoder);
1027}
1028
1029static const struct drm_encoder_funcs qxl_enc_funcs = {
1030 .destroy = qxl_enc_destroy,
1031};
1032
Dave Airlie4695b032013-10-11 11:05:00 +10001033static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1034{
1035 if (qdev->hotplug_mode_update_property)
1036 return 0;
1037
1038 qdev->hotplug_mode_update_property =
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001039 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlie4695b032013-10-11 11:05:00 +10001040 "hotplug_mode_update", 0, 1);
1041
1042 return 0;
1043}
1044
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001045static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +10001046{
Dave Airlie4695b032013-10-11 11:05:00 +10001047 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +10001048 struct qxl_output *qxl_output;
1049 struct drm_connector *connector;
1050 struct drm_encoder *encoder;
1051
1052 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1053 if (!qxl_output)
1054 return -ENOMEM;
1055
1056 qxl_output->index = num_output;
1057
1058 connector = &qxl_output->base;
1059 encoder = &qxl_output->enc;
1060 drm_connector_init(dev, &qxl_output->base,
1061 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1062
1063 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001064 DRM_MODE_ENCODER_VIRTUAL, NULL);
Dave Airlief64122c2013-02-25 14:47:55 +10001065
Dave Airlie5ff91e42013-07-05 10:20:33 +10001066 /* we get HPD via client monitors config */
1067 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +10001068 encoder->possible_crtcs = 1 << num_output;
1069 drm_mode_connector_attach_encoder(&qxl_output->base,
1070 &qxl_output->enc);
1071 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1072 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1073
Dave Airlie4695b032013-10-11 11:05:00 +10001074 drm_object_attach_property(&connector->base,
1075 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +10001076 drm_object_attach_property(&connector->base,
1077 dev->mode_config.suggested_x_property, 0);
1078 drm_object_attach_property(&connector->base,
1079 dev->mode_config.suggested_y_property, 0);
Dave Airlief64122c2013-02-25 14:47:55 +10001080 return 0;
1081}
1082
1083static struct drm_framebuffer *
1084qxl_user_framebuffer_create(struct drm_device *dev,
1085 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02001086 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief64122c2013-02-25 14:47:55 +10001087{
1088 struct drm_gem_object *obj;
1089 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +10001090 int ret;
1091
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001092 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1093 if (!obj)
1094 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +10001095
1096 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1097 if (qxl_fb == NULL)
1098 return NULL;
1099
Noralf Trønnes6819c3c2016-04-28 17:18:36 +02001100 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +10001101 if (ret) {
1102 kfree(qxl_fb);
1103 drm_gem_object_unreference_unlocked(obj);
1104 return NULL;
1105 }
1106
Dave Airlief64122c2013-02-25 14:47:55 +10001107 return &qxl_fb->base;
1108}
1109
1110static const struct drm_mode_config_funcs qxl_mode_funcs = {
1111 .fb_create = qxl_user_framebuffer_create,
1112};
1113
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001114int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001115{
Dave Airlief64122c2013-02-25 14:47:55 +10001116 int ret;
1117 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001118 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001119 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001120 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001121
Dave Airlief64122c2013-02-25 14:47:55 +10001122 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1123 QXL_GEM_DOMAIN_VRAM,
1124 false, false, NULL, &gobj);
1125 if (ret) {
1126 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1127 return -ENOMEM;
1128 }
1129 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001130
1131 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1132 if (ret)
1133 return ret;
1134
1135 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
1136 if (ret) {
1137 qxl_bo_unreserve(qdev->monitors_config_bo);
1138 return ret;
1139 }
1140
1141 qxl_bo_unreserve(qdev->monitors_config_bo);
1142
Dave Airlief64122c2013-02-25 14:47:55 +10001143 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001144
Dave Airlief64122c2013-02-25 14:47:55 +10001145 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1146 qdev->ram_header->monitors_config =
1147 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1148
1149 memset(qdev->monitors_config, 0, monitors_config_size);
1150 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001151 return 0;
1152}
1153
1154int qxl_destroy_monitors_object(struct qxl_device *qdev)
1155{
1156 int ret;
1157
1158 qdev->monitors_config = NULL;
1159 qdev->ram_header->monitors_config = 0;
1160
1161 qxl_bo_kunmap(qdev->monitors_config_bo);
1162 ret = qxl_bo_reserve(qdev->monitors_config_bo, false);
1163 if (ret)
1164 return ret;
1165
1166 qxl_bo_unpin(qdev->monitors_config_bo);
1167 qxl_bo_unreserve(qdev->monitors_config_bo);
1168
1169 qxl_bo_unref(&qdev->monitors_config_bo);
1170 return 0;
1171}
1172
1173int qxl_modeset_init(struct qxl_device *qdev)
1174{
1175 int i;
1176 int ret;
1177
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001178 drm_mode_config_init(&qdev->ddev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001179
1180 ret = qxl_create_monitors_object(qdev);
1181 if (ret)
1182 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001183
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001184 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
Dave Airlief64122c2013-02-25 14:47:55 +10001185
1186 /* modes will be validated against the framebuffer size */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001187 qdev->ddev.mode_config.min_width = 320;
1188 qdev->ddev.mode_config.min_height = 200;
1189 qdev->ddev.mode_config.max_width = 8192;
1190 qdev->ddev.mode_config.max_height = 8192;
Dave Airlief64122c2013-02-25 14:47:55 +10001191
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001192 qdev->ddev.mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001193
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001194 drm_mode_create_suggested_offset_properties(&qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001195 qxl_mode_create_hotplug_mode_update_property(qdev);
1196
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001197 for (i = 0 ; i < qxl_num_crtc; ++i) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001198 qdev_crtc_init(&qdev->ddev, i);
1199 qdev_output_init(&qdev->ddev, i);
Dave Airlief64122c2013-02-25 14:47:55 +10001200 }
1201
1202 qdev->mode_info.mode_config_initialized = true;
1203
1204 /* primary surface must be created by this point, to allow
1205 * issuing command queue commands and having them read by
1206 * spice server. */
1207 qxl_fbdev_init(qdev);
1208 return 0;
1209}
1210
1211void qxl_modeset_fini(struct qxl_device *qdev)
1212{
1213 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001214
1215 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001216 if (qdev->mode_info.mode_config_initialized) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001217 drm_mode_config_cleanup(&qdev->ddev);
Dave Airlief64122c2013-02-25 14:47:55 +10001218 qdev->mode_info.mode_config_initialized = false;
1219 }
1220}