blob: 7335d99244d5b1e6e98ca16e873bfe5d09691a4d [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
Randy Dunlapc5416d662013-12-20 10:58:15 -080026#include <linux/crc32.h>
Masahiro Yamadaedaf4922017-04-24 13:50:30 +090027#include <drm/drm_crtc_helper.h>
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010028#include <drm/drm_plane_helper.h>
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -030029#include <drm/drm_atomic_helper.h>
Gabriel Krisman Bertazi10a0bd82017-02-27 17:43:24 -030030#include <drm/drm_atomic.h>
Dave Airlief64122c2013-02-25 14:47:55 +100031
Masahiro Yamadaedaf4922017-04-24 13:50:30 +090032#include "qxl_drv.h"
33#include "qxl_object.h"
34
Dave Airlie07f8d9b2013-07-02 06:37:13 +010035static bool qxl_head_enabled(struct qxl_head *head)
36{
37 return head->width && head->height;
38}
39
Christophe Fergeaue4a76442016-11-08 10:12:03 +010040static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
Dave Airlief64122c2013-02-25 14:47:55 +100041{
42 if (qdev->client_monitors_config &&
43 count > qdev->client_monitors_config->count) {
44 kfree(qdev->client_monitors_config);
Dave Airlie62c8ba72013-04-16 13:36:00 +100045 qdev->client_monitors_config = NULL;
Dave Airlief64122c2013-02-25 14:47:55 +100046 }
47 if (!qdev->client_monitors_config) {
48 qdev->client_monitors_config = kzalloc(
49 sizeof(struct qxl_monitors_config) +
50 sizeof(struct qxl_head) * count, GFP_KERNEL);
51 if (!qdev->client_monitors_config) {
52 qxl_io_log(qdev,
53 "%s: allocation failure for %u heads\n",
54 __func__, count);
55 return;
56 }
57 }
58 qdev->client_monitors_config->count = count;
59}
60
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010061enum {
62 MONITORS_CONFIG_MODIFIED,
63 MONITORS_CONFIG_UNCHANGED,
64 MONITORS_CONFIG_BAD_CRC,
65};
66
Dave Airlief64122c2013-02-25 14:47:55 +100067static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
68{
69 int i;
70 int num_monitors;
71 uint32_t crc;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010072 int status = MONITORS_CONFIG_UNCHANGED;
Dave Airlief64122c2013-02-25 14:47:55 +100073
Dave Airlief64122c2013-02-25 14:47:55 +100074 num_monitors = qdev->rom->client_monitors_config.count;
75 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
76 sizeof(qdev->rom->client_monitors_config));
77 if (crc != qdev->rom->client_monitors_config_crc) {
Frediano Ziglio72ec5652015-06-03 12:09:16 +010078 qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
Dave Airlief64122c2013-02-25 14:47:55 +100079 sizeof(qdev->rom->client_monitors_config),
80 qdev->rom->client_monitors_config_crc);
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010081 return MONITORS_CONFIG_BAD_CRC;
Dave Airlief64122c2013-02-25 14:47:55 +100082 }
Gerd Hoffmannc50fad82017-03-01 11:12:33 +010083 if (!num_monitors) {
84 DRM_DEBUG_KMS("no client monitors configured\n");
85 return status;
86 }
Dave Airlief64122c2013-02-25 14:47:55 +100087 if (num_monitors > qdev->monitors_config->max_allowed) {
Dave Airlie5b8788c2013-07-01 14:14:38 +100088 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
89 qdev->monitors_config->max_allowed, num_monitors);
Dave Airlief64122c2013-02-25 14:47:55 +100090 num_monitors = qdev->monitors_config->max_allowed;
91 } else {
92 num_monitors = qdev->rom->client_monitors_config.count;
93 }
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010094 if (qdev->client_monitors_config
95 && (num_monitors != qdev->client_monitors_config->count)) {
96 status = MONITORS_CONFIG_MODIFIED;
97 }
Dave Airlief64122c2013-02-25 14:47:55 +100098 qxl_alloc_client_monitors_config(qdev, num_monitors);
99 /* we copy max from the client but it isn't used */
100 qdev->client_monitors_config->max_allowed =
101 qdev->monitors_config->max_allowed;
102 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
103 struct qxl_urect *c_rect =
104 &qdev->rom->client_monitors_config.heads[i];
105 struct qxl_head *client_head =
106 &qdev->client_monitors_config->heads[i];
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100107 if (client_head->x != c_rect->left) {
108 client_head->x = c_rect->left;
109 status = MONITORS_CONFIG_MODIFIED;
110 }
111 if (client_head->y != c_rect->top) {
112 client_head->y = c_rect->top;
113 status = MONITORS_CONFIG_MODIFIED;
114 }
115 if (client_head->width != c_rect->right - c_rect->left) {
116 client_head->width = c_rect->right - c_rect->left;
117 status = MONITORS_CONFIG_MODIFIED;
118 }
119 if (client_head->height != c_rect->bottom - c_rect->top) {
120 client_head->height = c_rect->bottom - c_rect->top;
121 status = MONITORS_CONFIG_MODIFIED;
122 }
123 if (client_head->surface_id != 0) {
124 client_head->surface_id = 0;
125 status = MONITORS_CONFIG_MODIFIED;
126 }
127 if (client_head->id != i) {
128 client_head->id = i;
129 status = MONITORS_CONFIG_MODIFIED;
130 }
131 if (client_head->flags != 0) {
132 client_head->flags = 0;
133 status = MONITORS_CONFIG_MODIFIED;
134 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100135 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
136 client_head->x, client_head->y);
Dave Airlief64122c2013-02-25 14:47:55 +1000137 }
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100138
139 return status;
Dave Airlief64122c2013-02-25 14:47:55 +1000140}
141
Dave Airlie7dea0942014-10-28 11:28:44 +1000142static void qxl_update_offset_props(struct qxl_device *qdev)
143{
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200144 struct drm_device *dev = &qdev->ddev;
Dave Airlie7dea0942014-10-28 11:28:44 +1000145 struct drm_connector *connector;
146 struct qxl_output *output;
147 struct qxl_head *head;
148
149 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
150 output = drm_connector_to_qxl_output(connector);
151
152 head = &qdev->client_monitors_config->heads[output->index];
153
154 drm_object_property_set_value(&connector->base,
155 dev->mode_config.suggested_x_property, head->x);
156 drm_object_property_set_value(&connector->base,
157 dev->mode_config.suggested_y_property, head->y);
158 }
159}
160
Dave Airlief64122c2013-02-25 14:47:55 +1000161void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
162{
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200163 struct drm_device *dev = &qdev->ddev;
Gerd Hoffmann90621552017-03-01 11:12:32 +0100164 int status, retries;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100165
Gerd Hoffmann90621552017-03-01 11:12:32 +0100166 for (retries = 0; retries < 10; retries++) {
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100167 status = qxl_display_copy_rom_client_monitors_config(qdev);
Gerd Hoffmann90621552017-03-01 11:12:32 +0100168 if (status != MONITORS_CONFIG_BAD_CRC)
169 break;
170 udelay(5);
171 }
172 if (status == MONITORS_CONFIG_BAD_CRC) {
173 qxl_io_log(qdev, "config: bad crc\n");
174 DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
175 return;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100176 }
177 if (status == MONITORS_CONFIG_UNCHANGED) {
Gerd Hoffmann90621552017-03-01 11:12:32 +0100178 qxl_io_log(qdev, "config: unchanged\n");
179 DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100180 return;
Dave Airlief64122c2013-02-25 14:47:55 +1000181 }
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200182
Dave Airlie7dea0942014-10-28 11:28:44 +1000183 drm_modeset_lock_all(dev);
184 qxl_update_offset_props(qdev);
185 drm_modeset_unlock_all(dev);
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200186 if (!drm_helper_hpd_irq_event(dev)) {
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200187 /* notify that the monitor configuration changed, to
188 adjust at the arbitrary resolution */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200189 drm_kms_helper_hotplug_event(dev);
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200190 }
Dave Airlief64122c2013-02-25 14:47:55 +1000191}
192
Marc-André Lureaub0807422013-10-18 16:11:31 +0200193static int qxl_add_monitors_config_modes(struct drm_connector *connector,
194 unsigned *pwidth,
195 unsigned *pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000196{
197 struct drm_device *dev = connector->dev;
198 struct qxl_device *qdev = dev->dev_private;
199 struct qxl_output *output = drm_connector_to_qxl_output(connector);
200 int h = output->index;
201 struct drm_display_mode *mode = NULL;
202 struct qxl_head *head;
203
Gerd Hoffmann2d856f942017-03-01 11:12:34 +0100204 if (!qdev->monitors_config)
205 return 0;
206 if (h >= qdev->monitors_config->max_allowed)
207 return 0;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100208 if (!qdev->client_monitors_config)
Dave Airlief64122c2013-02-25 14:47:55 +1000209 return 0;
Gerd Hoffmann2d856f942017-03-01 11:12:34 +0100210 if (h >= qdev->client_monitors_config->count)
211 return 0;
212
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100213 head = &qdev->client_monitors_config->heads[h];
Gerd Hoffmann2d856f942017-03-01 11:12:34 +0100214 DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
Dave Airlief64122c2013-02-25 14:47:55 +1000215
216 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
217 false);
218 mode->type |= DRM_MODE_TYPE_PREFERRED;
Christophe Fergeauff996e72016-11-08 10:12:09 +0100219 mode->hdisplay = head->width;
220 mode->vdisplay = head->height;
221 drm_mode_set_name(mode);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200222 *pwidth = head->width;
223 *pheight = head->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000224 drm_mode_probed_add(connector, mode);
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500225 /* remember the last custom size for mode validation */
226 qdev->monitors_config_width = mode->hdisplay;
227 qdev->monitors_config_height = mode->vdisplay;
Dave Airlief64122c2013-02-25 14:47:55 +1000228 return 1;
229}
230
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500231static struct mode_size {
232 int w;
233 int h;
234} common_modes[] = {
235 { 640, 480},
236 { 720, 480},
237 { 800, 600},
238 { 848, 480},
239 {1024, 768},
240 {1152, 768},
241 {1280, 720},
242 {1280, 800},
243 {1280, 854},
244 {1280, 960},
245 {1280, 1024},
246 {1440, 900},
247 {1400, 1050},
248 {1680, 1050},
249 {1600, 1200},
250 {1920, 1080},
251 {1920, 1200}
252};
253
Marc-André Lureaub0807422013-10-18 16:11:31 +0200254static int qxl_add_common_modes(struct drm_connector *connector,
255 unsigned pwidth,
256 unsigned pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000257{
258 struct drm_device *dev = connector->dev;
259 struct drm_display_mode *mode = NULL;
260 int i;
Dave Airlief64122c2013-02-25 14:47:55 +1000261 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
Dave Airlief64122c2013-02-25 14:47:55 +1000262 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
263 60, false, false, false);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200264 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000265 mode->type |= DRM_MODE_TYPE_PREFERRED;
266 drm_mode_probed_add(connector, mode);
267 }
268 return i - 1;
269}
270
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300271static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
272 struct drm_crtc_state *old_crtc_state)
273{
274 struct drm_device *dev = crtc->dev;
275 struct drm_pending_vblank_event *event;
276 unsigned long flags;
277
278 if (crtc->state && crtc->state->event) {
279 event = crtc->state->event;
280 crtc->state->event = NULL;
281
282 spin_lock_irqsave(&dev->event_lock, flags);
283 drm_crtc_send_vblank_event(crtc, event);
284 spin_unlock_irqrestore(&dev->event_lock, flags);
285 }
286}
287
Dave Airlief64122c2013-02-25 14:47:55 +1000288static void qxl_crtc_destroy(struct drm_crtc *crtc)
289{
290 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
291
292 drm_crtc_cleanup(crtc);
293 kfree(qxl_crtc);
294}
295
Dave Airlief64122c2013-02-25 14:47:55 +1000296static const struct drm_crtc_funcs qxl_crtc_funcs = {
Gabriel Krisman Bertazibc8a00d2017-02-27 17:43:26 -0300297 .set_config = drm_atomic_helper_set_config,
Dave Airlief64122c2013-02-25 14:47:55 +1000298 .destroy = qxl_crtc_destroy,
Gabriel Krisman Bertazi9973c872017-02-27 17:43:27 -0300299 .page_flip = drm_atomic_helper_page_flip,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300300 .reset = drm_atomic_helper_crtc_reset,
301 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
302 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Dave Airlief64122c2013-02-25 14:47:55 +1000303};
304
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200305void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
Dave Airlief64122c2013-02-25 14:47:55 +1000306{
307 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200308 struct qxl_bo *bo = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000309
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200310 WARN_ON(bo->shadow);
Markus Elfringdc3583c2016-07-22 14:14:54 +0200311 drm_gem_object_unreference_unlocked(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000312 drm_framebuffer_cleanup(fb);
313 kfree(qxl_fb);
314}
315
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000316static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
317 struct drm_file *file_priv,
318 unsigned flags, unsigned color,
319 struct drm_clip_rect *clips,
320 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000321{
322 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
323 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
324 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
325 struct drm_clip_rect norect;
326 struct qxl_bo *qobj;
327 int inc = 1;
328
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200329 drm_modeset_lock_all(fb->dev);
330
Dave Airlief64122c2013-02-25 14:47:55 +1000331 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000332 /* if we aren't primary surface ignore this */
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200333 if (!qobj->is_primary) {
334 drm_modeset_unlock_all(fb->dev);
Dave Airlieb2b44652013-05-13 12:48:40 +1000335 return 0;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200336 }
Dave Airlieb2b44652013-05-13 12:48:40 +1000337
Dave Airlief64122c2013-02-25 14:47:55 +1000338 if (!num_clips) {
339 num_clips = 1;
340 clips = &norect;
341 norect.x1 = norect.y1 = 0;
342 norect.x2 = fb->width;
343 norect.y2 = fb->height;
344 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
345 num_clips /= 2;
346 inc = 2; /* skip source rects */
347 }
348
349 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
350 clips, num_clips, inc);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200351
352 drm_modeset_unlock_all(fb->dev);
353
Dave Airlief64122c2013-02-25 14:47:55 +1000354 return 0;
355}
356
357static const struct drm_framebuffer_funcs qxl_fb_funcs = {
358 .destroy = qxl_user_framebuffer_destroy,
359 .dirty = qxl_framebuffer_surface_dirty,
360/* TODO?
361 * .create_handle = qxl_user_framebuffer_create_handle, */
362};
363
364int
365qxl_framebuffer_init(struct drm_device *dev,
366 struct qxl_framebuffer *qfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200367 const struct drm_mode_fb_cmd2 *mode_cmd,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200368 struct drm_gem_object *obj,
369 const struct drm_framebuffer_funcs *funcs)
Dave Airlief64122c2013-02-25 14:47:55 +1000370{
371 int ret;
372
373 qfb->obj = obj;
Ville Syrjälä53609432016-11-18 21:52:51 +0200374 drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200375 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000376 if (ret) {
377 qfb->obj = NULL;
378 return ret;
379 }
Dave Airlief64122c2013-02-25 14:47:55 +1000380 return 0;
381}
382
Dave Airlief64122c2013-02-25 14:47:55 +1000383static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
384 const struct drm_display_mode *mode,
385 struct drm_display_mode *adjusted_mode)
386{
387 struct drm_device *dev = crtc->dev;
388 struct qxl_device *qdev = dev->dev_private;
389
390 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
391 __func__,
392 mode->hdisplay, mode->vdisplay,
393 adjusted_mode->hdisplay,
394 adjusted_mode->vdisplay);
395 return true;
396}
397
Christophe Fergeaue4a76442016-11-08 10:12:03 +0100398static void
Dave Airlief64122c2013-02-25 14:47:55 +1000399qxl_send_monitors_config(struct qxl_device *qdev)
400{
401 int i;
402
403 BUG_ON(!qdev->ram_header->monitors_config);
404
405 if (qdev->monitors_config->count == 0) {
406 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
407 return;
408 }
409 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
410 struct qxl_head *head = &qdev->monitors_config->heads[i];
411
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100412 if (head->y > 8192 || head->x > 8192 ||
Dave Airlief64122c2013-02-25 14:47:55 +1000413 head->width > 8192 || head->height > 8192) {
414 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
415 i, head->width, head->height,
416 head->x, head->y);
417 return;
418 }
419 }
420 qxl_io_monitors_config(qdev);
421}
422
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100423static void qxl_monitors_config_set(struct qxl_device *qdev,
424 int index,
425 unsigned x, unsigned y,
426 unsigned width, unsigned height,
427 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000428{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100429 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
430 qdev->monitors_config->heads[index].x = x;
431 qdev->monitors_config->heads[index].y = y;
432 qdev->monitors_config->heads[index].width = width;
433 qdev->monitors_config->heads[index].height = height;
434 qdev->monitors_config->heads[index].surface_id = surf_id;
435
Dave Airlief64122c2013-02-25 14:47:55 +1000436}
437
Gerd Hoffmann45dfe572017-06-20 13:39:15 +0200438static void qxl_mode_set_nofb(struct drm_crtc *crtc)
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300439{
440 struct qxl_device *qdev = crtc->dev->dev_private;
441 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
442 struct drm_display_mode *mode = &crtc->mode;
443
444 DRM_DEBUG("Mode set (%d,%d)\n",
445 mode->hdisplay, mode->vdisplay);
446
447 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
448 mode->hdisplay, mode->vdisplay, 0);
449
450}
451
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300452static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
453 struct drm_crtc_state *old_state)
Dave Airlief64122c2013-02-25 14:47:55 +1000454{
455 DRM_DEBUG("\n");
456}
457
Laurent Pinchart64581712017-06-30 12:36:45 +0300458static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
459 struct drm_crtc_state *old_state)
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100460{
461 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Gabriel Krisman Bertazi37235452017-02-27 17:43:22 -0300462 struct qxl_device *qdev = crtc->dev->dev_private;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100463
464 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
465
466 qxl_send_monitors_config(qdev);
467}
468
Dave Airlief64122c2013-02-25 14:47:55 +1000469static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
Dave Airlief64122c2013-02-25 14:47:55 +1000470 .mode_fixup = qxl_crtc_mode_fixup,
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300471 .mode_set_nofb = qxl_mode_set_nofb,
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300472 .atomic_flush = qxl_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300473 .atomic_enable = qxl_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +0300474 .atomic_disable = qxl_crtc_atomic_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000475};
476
Gerd Hoffmann45dfe572017-06-20 13:39:15 +0200477static int qxl_primary_atomic_check(struct drm_plane *plane,
478 struct drm_plane_state *state)
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300479{
480 struct qxl_device *qdev = plane->dev->dev_private;
481 struct qxl_framebuffer *qfb;
482 struct qxl_bo *bo;
483
484 if (!state->crtc || !state->fb)
485 return 0;
486
487 qfb = to_qxl_framebuffer(state->fb);
488 bo = gem_to_qxl_bo(qfb->obj);
489
490 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
491 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
492 return -EINVAL;
493 }
494
495 return 0;
496}
497
498static void qxl_primary_atomic_update(struct drm_plane *plane,
499 struct drm_plane_state *old_state)
500{
501 struct qxl_device *qdev = plane->dev->dev_private;
502 struct qxl_framebuffer *qfb =
503 to_qxl_framebuffer(plane->state->fb);
504 struct qxl_framebuffer *qfb_old;
505 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
506 struct qxl_bo *bo_old;
507 struct drm_clip_rect norect = {
508 .x1 = 0,
509 .y1 = 0,
510 .x2 = qfb->base.width,
511 .y2 = qfb->base.height
512 };
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200513 bool same_shadow = false;
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300514
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200515 if (old_state->fb) {
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300516 qfb_old = to_qxl_framebuffer(old_state->fb);
517 bo_old = gem_to_qxl_bo(qfb_old->obj);
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200518 } else {
519 bo_old = NULL;
520 }
521
522 if (bo == bo_old)
523 return;
524
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200525 if (bo_old && bo_old->shadow && bo->shadow &&
526 bo_old->shadow == bo->shadow) {
527 same_shadow = true;
528 }
529
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200530 if (bo_old && bo_old->is_primary) {
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200531 if (!same_shadow)
532 qxl_io_destroy_primary(qdev);
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300533 bo_old->is_primary = false;
534 }
535
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200536 if (!bo->is_primary) {
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200537 if (!same_shadow)
538 qxl_io_create_primary(qdev, 0, bo);
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200539 bo->is_primary = true;
540 }
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200541
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300542 qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
543}
544
545static void qxl_primary_atomic_disable(struct drm_plane *plane,
546 struct drm_plane_state *old_state)
547{
548 struct qxl_device *qdev = plane->dev->dev_private;
549
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200550 if (old_state->fb) {
551 struct qxl_framebuffer *qfb =
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300552 to_qxl_framebuffer(old_state->fb);
553 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
554
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200555 if (bo->is_primary) {
556 qxl_io_destroy_primary(qdev);
557 bo->is_primary = false;
558 }
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300559 }
560}
561
Gerd Hoffmann45dfe572017-06-20 13:39:15 +0200562static int qxl_plane_atomic_check(struct drm_plane *plane,
563 struct drm_plane_state *state)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300564{
565 return 0;
566}
567
568static void qxl_cursor_atomic_update(struct drm_plane *plane,
569 struct drm_plane_state *old_state)
570{
571 struct drm_device *dev = plane->dev;
572 struct qxl_device *qdev = dev->dev_private;
573 struct drm_framebuffer *fb = plane->state->fb;
574 struct qxl_release *release;
575 struct qxl_cursor_cmd *cmd;
576 struct qxl_cursor *cursor;
577 struct drm_gem_object *obj;
Ray Strode16c6db32017-11-27 16:50:09 -0500578 struct qxl_bo *cursor_bo = NULL, *user_bo = NULL;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300579 int ret;
580 void *user_ptr;
581 int size = 64*64*4;
582
583 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
584 QXL_RELEASE_CURSOR_CMD,
585 &release, NULL);
Dan Carpenteree5cb7c2017-03-14 10:54:10 +0300586 if (ret)
587 return;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300588
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300589 if (fb != old_state->fb) {
590 obj = to_qxl_framebuffer(fb)->obj;
591 user_bo = gem_to_qxl_bo(obj);
592
593 /* pinning is done in the prepare/cleanup framevbuffer */
594 ret = qxl_bo_kmap(user_bo, &user_ptr);
595 if (ret)
596 goto out_free_release;
597
598 ret = qxl_alloc_bo_reserved(qdev, release,
599 sizeof(struct qxl_cursor) + size,
600 &cursor_bo);
601 if (ret)
602 goto out_kunmap;
603
604 ret = qxl_release_reserve_list(release, true);
605 if (ret)
606 goto out_free_bo;
607
608 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
609 if (ret)
610 goto out_backoff;
611
612 cursor->header.unique = 0;
613 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
614 cursor->header.width = 64;
615 cursor->header.height = 64;
616 cursor->header.hot_spot_x = fb->hot_x;
617 cursor->header.hot_spot_y = fb->hot_y;
618 cursor->data_size = size;
619 cursor->chunk.next_chunk = 0;
620 cursor->chunk.prev_chunk = 0;
621 cursor->chunk.data_size = size;
622 memcpy(cursor->chunk.data, user_ptr, size);
623 qxl_bo_kunmap(cursor_bo);
624 qxl_bo_kunmap(user_bo);
625
Gabriel Krisman Bertazi429030b2017-05-19 14:58:19 -0300626 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300627 cmd->u.set.visible = 1;
628 cmd->u.set.shape = qxl_bo_physical_address(qdev,
629 cursor_bo, 0);
630 cmd->type = QXL_CURSOR_SET;
631 } else {
632
633 ret = qxl_release_reserve_list(release, true);
634 if (ret)
635 goto out_free_release;
636
Gabriel Krisman Bertazi429030b2017-05-19 14:58:19 -0300637 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300638 cmd->type = QXL_CURSOR_MOVE;
639 }
640
641 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
642 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
643
644 qxl_release_unmap(qdev, release, &cmd->release_info);
645 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
646 qxl_release_fence_buffer_objects(release);
647
Ray Strode16c6db32017-11-27 16:50:09 -0500648 qxl_bo_unref(&cursor_bo);
649
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300650 return;
651
652out_backoff:
653 qxl_release_backoff_reserve_list(release);
654out_free_bo:
655 qxl_bo_unref(&cursor_bo);
656out_kunmap:
657 qxl_bo_kunmap(user_bo);
658out_free_release:
659 qxl_release_free(qdev, release);
660 return;
661
662}
663
Gerd Hoffmann45dfe572017-06-20 13:39:15 +0200664static void qxl_cursor_atomic_disable(struct drm_plane *plane,
665 struct drm_plane_state *old_state)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300666{
667 struct qxl_device *qdev = plane->dev->dev_private;
668 struct qxl_release *release;
669 struct qxl_cursor_cmd *cmd;
670 int ret;
671
672 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
673 QXL_RELEASE_CURSOR_CMD,
674 &release, NULL);
675 if (ret)
676 return;
677
678 ret = qxl_release_reserve_list(release, true);
679 if (ret) {
680 qxl_release_free(qdev, release);
681 return;
682 }
683
684 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
685 cmd->type = QXL_CURSOR_HIDE;
686 qxl_release_unmap(qdev, release, &cmd->release_info);
687
688 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
689 qxl_release_fence_buffer_objects(release);
690}
691
Gerd Hoffmann45dfe572017-06-20 13:39:15 +0200692static int qxl_plane_prepare_fb(struct drm_plane *plane,
693 struct drm_plane_state *new_state)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300694{
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200695 struct qxl_device *qdev = plane->dev->dev_private;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300696 struct drm_gem_object *obj;
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200697 struct qxl_bo *user_bo, *old_bo = NULL;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300698 int ret;
699
700 if (!new_state->fb)
701 return 0;
702
703 obj = to_qxl_framebuffer(new_state->fb)->obj;
704 user_bo = gem_to_qxl_bo(obj);
705
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200706 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
707 user_bo->is_dumb && !user_bo->shadow) {
708 if (plane->state->fb) {
709 obj = to_qxl_framebuffer(plane->state->fb)->obj;
710 old_bo = gem_to_qxl_bo(obj);
711 }
712 if (old_bo && old_bo->shadow &&
713 user_bo->gem_base.size == old_bo->gem_base.size &&
714 plane->state->crtc == new_state->crtc &&
715 plane->state->crtc_w == new_state->crtc_w &&
716 plane->state->crtc_h == new_state->crtc_h &&
717 plane->state->src_x == new_state->src_x &&
718 plane->state->src_y == new_state->src_y &&
719 plane->state->src_w == new_state->src_w &&
720 plane->state->src_h == new_state->src_h &&
721 plane->state->rotation == new_state->rotation &&
722 plane->state->zpos == new_state->zpos) {
723 drm_gem_object_get(&old_bo->shadow->gem_base);
724 user_bo->shadow = old_bo->shadow;
725 } else {
726 qxl_bo_create(qdev, user_bo->gem_base.size,
727 true, true, QXL_GEM_DOMAIN_VRAM, NULL,
728 &user_bo->shadow);
729 }
730 }
731
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300732 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
733 if (ret)
734 return ret;
735
736 return 0;
737}
738
739static void qxl_plane_cleanup_fb(struct drm_plane *plane,
740 struct drm_plane_state *old_state)
741{
742 struct drm_gem_object *obj;
743 struct qxl_bo *user_bo;
744
Gerd Hoffmann5f3d8622017-09-18 09:41:45 +0200745 if (!old_state->fb) {
746 /*
747 * we never executed prepare_fb, so there's nothing to
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300748 * unpin.
749 */
750 return;
751 }
752
Gerd Hoffmann5f3d8622017-09-18 09:41:45 +0200753 obj = to_qxl_framebuffer(old_state->fb)->obj;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300754 user_bo = gem_to_qxl_bo(obj);
755 qxl_bo_unpin(user_bo);
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200756
757 if (user_bo->shadow && !user_bo->is_primary) {
758 drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
759 user_bo->shadow = NULL;
760 }
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300761}
762
763static const uint32_t qxl_cursor_plane_formats[] = {
764 DRM_FORMAT_ARGB8888,
765};
766
767static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
768 .atomic_check = qxl_plane_atomic_check,
769 .atomic_update = qxl_cursor_atomic_update,
770 .atomic_disable = qxl_cursor_atomic_disable,
771 .prepare_fb = qxl_plane_prepare_fb,
772 .cleanup_fb = qxl_plane_cleanup_fb,
773};
774
775static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -0300776 .update_plane = drm_atomic_helper_update_plane,
777 .disable_plane = drm_atomic_helper_disable_plane,
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300778 .destroy = drm_primary_helper_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300779 .reset = drm_atomic_helper_plane_reset,
780 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
781 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300782};
783
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300784static const uint32_t qxl_primary_plane_formats[] = {
785 DRM_FORMAT_XRGB8888,
786 DRM_FORMAT_ARGB8888,
787};
788
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300789static const struct drm_plane_helper_funcs primary_helper_funcs = {
790 .atomic_check = qxl_primary_atomic_check,
791 .atomic_update = qxl_primary_atomic_update,
792 .atomic_disable = qxl_primary_atomic_disable,
793 .prepare_fb = qxl_plane_prepare_fb,
794 .cleanup_fb = qxl_plane_cleanup_fb,
795};
796
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300797static const struct drm_plane_funcs qxl_primary_plane_funcs = {
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -0300798 .update_plane = drm_atomic_helper_update_plane,
799 .disable_plane = drm_atomic_helper_disable_plane,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300800 .destroy = drm_primary_helper_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300801 .reset = drm_atomic_helper_plane_reset,
802 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
803 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300804};
805
806static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
807 unsigned int possible_crtcs,
808 enum drm_plane_type type)
809{
810 const struct drm_plane_helper_funcs *helper_funcs = NULL;
811 struct drm_plane *plane;
812 const struct drm_plane_funcs *funcs;
813 const uint32_t *formats;
814 int num_formats;
815 int err;
816
817 if (type == DRM_PLANE_TYPE_PRIMARY) {
818 funcs = &qxl_primary_plane_funcs;
819 formats = qxl_primary_plane_formats;
820 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300821 helper_funcs = &primary_helper_funcs;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300822 } else if (type == DRM_PLANE_TYPE_CURSOR) {
823 funcs = &qxl_cursor_plane_funcs;
824 formats = qxl_cursor_plane_formats;
825 helper_funcs = &qxl_cursor_helper_funcs;
826 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300827 } else {
828 return ERR_PTR(-EINVAL);
829 }
830
831 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
832 if (!plane)
833 return ERR_PTR(-ENOMEM);
834
835 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
836 funcs, formats, num_formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700837 NULL, type, NULL);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300838 if (err)
839 goto free_plane;
840
841 drm_plane_helper_add(plane, helper_funcs);
842
843 return plane;
844
845free_plane:
846 kfree(plane);
847 return ERR_PTR(-EINVAL);
848}
849
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100850static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000851{
852 struct qxl_crtc *qxl_crtc;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300853 struct drm_plane *primary, *cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300854 struct qxl_device *qdev = dev->dev_private;
855 int r;
Dave Airlief64122c2013-02-25 14:47:55 +1000856
857 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
858 if (!qxl_crtc)
859 return -ENOMEM;
860
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300861 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
862 if (IS_ERR(primary)) {
863 r = -ENOMEM;
864 goto free_mem;
865 }
866
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300867 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
868 if (IS_ERR(cursor)) {
869 r = -ENOMEM;
870 goto clean_primary;
871 }
872
873 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300874 &qxl_crtc_funcs, NULL);
875 if (r)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300876 goto clean_cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300877
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100878 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000879 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
880 return 0;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300881
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300882clean_cursor:
883 drm_plane_cleanup(cursor);
884 kfree(cursor);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300885clean_primary:
886 drm_plane_cleanup(primary);
887 kfree(primary);
888free_mem:
889 kfree(qxl_crtc);
890 return r;
Dave Airlief64122c2013-02-25 14:47:55 +1000891}
892
893static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
894{
895 DRM_DEBUG("\n");
896}
897
Dave Airlief64122c2013-02-25 14:47:55 +1000898static void qxl_enc_prepare(struct drm_encoder *encoder)
899{
900 DRM_DEBUG("\n");
901}
902
903static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
904 struct drm_encoder *encoder)
905{
906 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100907 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000908 struct qxl_head *head;
909 struct drm_display_mode *mode;
910
911 BUG_ON(!encoder);
912 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100913 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000914 if (!qdev->monitors_config ||
915 qdev->monitors_config->max_allowed <= i) {
916 DRM_ERROR(
917 "head number too large or missing monitors config: %p, %d",
918 qdev->monitors_config,
919 qdev->monitors_config ?
920 qdev->monitors_config->max_allowed : -1);
921 return;
922 }
923 if (!encoder->crtc) {
924 DRM_ERROR("missing crtc on encoder %p\n", encoder);
925 return;
926 }
927 if (i != 0)
928 DRM_DEBUG("missing for multiple monitors: no head holes\n");
929 head = &qdev->monitors_config->heads[i];
930 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000931 if (encoder->crtc->enabled) {
932 mode = &encoder->crtc->mode;
933 head->width = mode->hdisplay;
934 head->height = mode->vdisplay;
935 head->x = encoder->crtc->x;
936 head->y = encoder->crtc->y;
937 if (qdev->monitors_config->count < i + 1)
938 qdev->monitors_config->count = i + 1;
939 } else {
940 head->width = 0;
941 head->height = 0;
942 head->x = 0;
943 head->y = 0;
944 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100945 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
946 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000947 head->flags = 0;
948 /* TODO - somewhere else to call this for multiple monitors
949 * (config_commit?) */
950 qxl_send_monitors_config(qdev);
951}
952
953static void qxl_enc_commit(struct drm_encoder *encoder)
954{
955 struct qxl_device *qdev = encoder->dev->dev_private;
956
957 qxl_write_monitors_config_for_encoder(qdev, encoder);
958 DRM_DEBUG("\n");
959}
960
961static void qxl_enc_mode_set(struct drm_encoder *encoder,
962 struct drm_display_mode *mode,
963 struct drm_display_mode *adjusted_mode)
964{
965 DRM_DEBUG("\n");
966}
967
968static int qxl_conn_get_modes(struct drm_connector *connector)
969{
Marc-André Lureaub0807422013-10-18 16:11:31 +0200970 unsigned pwidth = 1024;
971 unsigned pheight = 768;
Gerd Hoffmann2d856f942017-03-01 11:12:34 +0100972 int ret = 0;
Dave Airlief64122c2013-02-25 14:47:55 +1000973
Gerd Hoffmann2d856f942017-03-01 11:12:34 +0100974 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
975 if (ret < 0)
976 return ret;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200977 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000978 return ret;
979}
980
981static int qxl_conn_mode_valid(struct drm_connector *connector,
982 struct drm_display_mode *mode)
983{
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500984 struct drm_device *ddev = connector->dev;
985 struct qxl_device *qdev = ddev->dev_private;
986 int i;
987
Dave Airlief64122c2013-02-25 14:47:55 +1000988 /* TODO: is this called for user defined modes? (xrandr --add-mode)
989 * TODO: check that the mode fits in the framebuffer */
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500990
991 if(qdev->monitors_config_width == mode->hdisplay &&
992 qdev->monitors_config_height == mode->vdisplay)
993 return MODE_OK;
994
995 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
996 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
997 return MODE_OK;
998 }
999 return MODE_BAD;
Dave Airlief64122c2013-02-25 14:47:55 +10001000}
1001
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001002static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +10001003{
1004 struct qxl_output *qxl_output =
1005 drm_connector_to_qxl_output(connector);
1006
1007 DRM_DEBUG("\n");
1008 return &qxl_output->enc;
1009}
1010
1011
1012static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1013 .dpms = qxl_enc_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001014 .prepare = qxl_enc_prepare,
1015 .mode_set = qxl_enc_mode_set,
1016 .commit = qxl_enc_commit,
1017};
1018
1019static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1020 .get_modes = qxl_conn_get_modes,
1021 .mode_valid = qxl_conn_mode_valid,
1022 .best_encoder = qxl_best_encoder,
1023};
1024
Dave Airlief64122c2013-02-25 14:47:55 +10001025static enum drm_connector_status qxl_conn_detect(
1026 struct drm_connector *connector,
1027 bool force)
1028{
1029 struct qxl_output *output =
1030 drm_connector_to_qxl_output(connector);
1031 struct drm_device *ddev = connector->dev;
1032 struct qxl_device *qdev = ddev->dev_private;
Dave Airlie69e5d3f2015-09-14 10:28:34 +10001033 bool connected = false;
Dave Airlief64122c2013-02-25 14:47:55 +10001034
1035 /* The first monitor is always connected */
Dave Airlie69e5d3f2015-09-14 10:28:34 +10001036 if (!qdev->client_monitors_config) {
1037 if (output->index == 0)
1038 connected = true;
1039 } else
1040 connected = qdev->client_monitors_config->count > output->index &&
1041 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
Dave Airlief64122c2013-02-25 14:47:55 +10001042
Marc-André Lureau5cab51c2013-10-18 16:11:33 +02001043 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1044 if (!connected)
1045 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
1046
Dave Airlief64122c2013-02-25 14:47:55 +10001047 return connected ? connector_status_connected
1048 : connector_status_disconnected;
1049}
1050
1051static int qxl_conn_set_property(struct drm_connector *connector,
1052 struct drm_property *property,
1053 uint64_t value)
1054{
1055 DRM_DEBUG("\n");
1056 return 0;
1057}
1058
1059static void qxl_conn_destroy(struct drm_connector *connector)
1060{
1061 struct qxl_output *qxl_output =
1062 drm_connector_to_qxl_output(connector);
1063
Thomas Wood34ea3d32014-05-29 16:57:41 +01001064 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001065 drm_connector_cleanup(connector);
1066 kfree(qxl_output);
1067}
1068
1069static const struct drm_connector_funcs qxl_connector_funcs = {
1070 .dpms = drm_helper_connector_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001071 .detect = qxl_conn_detect,
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001072 .fill_modes = drm_helper_probe_single_connector_modes,
Dave Airlief64122c2013-02-25 14:47:55 +10001073 .set_property = qxl_conn_set_property,
1074 .destroy = qxl_conn_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -03001075 .reset = drm_atomic_helper_connector_reset,
1076 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1077 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Dave Airlief64122c2013-02-25 14:47:55 +10001078};
1079
1080static void qxl_enc_destroy(struct drm_encoder *encoder)
1081{
1082 drm_encoder_cleanup(encoder);
1083}
1084
1085static const struct drm_encoder_funcs qxl_enc_funcs = {
1086 .destroy = qxl_enc_destroy,
1087};
1088
Dave Airlie4695b032013-10-11 11:05:00 +10001089static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1090{
1091 if (qdev->hotplug_mode_update_property)
1092 return 0;
1093
1094 qdev->hotplug_mode_update_property =
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001095 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlie4695b032013-10-11 11:05:00 +10001096 "hotplug_mode_update", 0, 1);
1097
1098 return 0;
1099}
1100
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001101static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +10001102{
Dave Airlie4695b032013-10-11 11:05:00 +10001103 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +10001104 struct qxl_output *qxl_output;
1105 struct drm_connector *connector;
1106 struct drm_encoder *encoder;
1107
1108 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1109 if (!qxl_output)
1110 return -ENOMEM;
1111
1112 qxl_output->index = num_output;
1113
1114 connector = &qxl_output->base;
1115 encoder = &qxl_output->enc;
1116 drm_connector_init(dev, &qxl_output->base,
1117 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1118
1119 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001120 DRM_MODE_ENCODER_VIRTUAL, NULL);
Dave Airlief64122c2013-02-25 14:47:55 +10001121
Dave Airlie5ff91e42013-07-05 10:20:33 +10001122 /* we get HPD via client monitors config */
1123 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +10001124 encoder->possible_crtcs = 1 << num_output;
1125 drm_mode_connector_attach_encoder(&qxl_output->base,
1126 &qxl_output->enc);
1127 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1128 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1129
Dave Airlie4695b032013-10-11 11:05:00 +10001130 drm_object_attach_property(&connector->base,
1131 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +10001132 drm_object_attach_property(&connector->base,
1133 dev->mode_config.suggested_x_property, 0);
1134 drm_object_attach_property(&connector->base,
1135 dev->mode_config.suggested_y_property, 0);
Dave Airlief64122c2013-02-25 14:47:55 +10001136 return 0;
1137}
1138
1139static struct drm_framebuffer *
1140qxl_user_framebuffer_create(struct drm_device *dev,
1141 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02001142 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief64122c2013-02-25 14:47:55 +10001143{
1144 struct drm_gem_object *obj;
1145 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +10001146 int ret;
1147
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001148 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1149 if (!obj)
1150 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +10001151
1152 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1153 if (qxl_fb == NULL)
1154 return NULL;
1155
Noralf Trønnes6819c3c2016-04-28 17:18:36 +02001156 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +10001157 if (ret) {
1158 kfree(qxl_fb);
1159 drm_gem_object_unreference_unlocked(obj);
1160 return NULL;
1161 }
1162
Dave Airlief64122c2013-02-25 14:47:55 +10001163 return &qxl_fb->base;
1164}
1165
1166static const struct drm_mode_config_funcs qxl_mode_funcs = {
1167 .fb_create = qxl_user_framebuffer_create,
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -03001168 .atomic_check = drm_atomic_helper_check,
1169 .atomic_commit = drm_atomic_helper_commit,
Dave Airlief64122c2013-02-25 14:47:55 +10001170};
1171
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001172int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001173{
Dave Airlief64122c2013-02-25 14:47:55 +10001174 int ret;
1175 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001176 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001177 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001178 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001179
Dave Airlief64122c2013-02-25 14:47:55 +10001180 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1181 QXL_GEM_DOMAIN_VRAM,
1182 false, false, NULL, &gobj);
1183 if (ret) {
1184 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1185 return -ENOMEM;
1186 }
1187 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001188
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001189 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001190 if (ret)
1191 return ret;
1192
Dave Airlief64122c2013-02-25 14:47:55 +10001193 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001194
Dave Airlief64122c2013-02-25 14:47:55 +10001195 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1196 qdev->ram_header->monitors_config =
1197 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1198
1199 memset(qdev->monitors_config, 0, monitors_config_size);
1200 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001201 return 0;
1202}
1203
1204int qxl_destroy_monitors_object(struct qxl_device *qdev)
1205{
1206 int ret;
1207
1208 qdev->monitors_config = NULL;
1209 qdev->ram_header->monitors_config = 0;
1210
1211 qxl_bo_kunmap(qdev->monitors_config_bo);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001212 ret = qxl_bo_unpin(qdev->monitors_config_bo);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001213 if (ret)
1214 return ret;
1215
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001216 qxl_bo_unref(&qdev->monitors_config_bo);
1217 return 0;
1218}
1219
1220int qxl_modeset_init(struct qxl_device *qdev)
1221{
1222 int i;
1223 int ret;
1224
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001225 drm_mode_config_init(&qdev->ddev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001226
1227 ret = qxl_create_monitors_object(qdev);
1228 if (ret)
1229 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001230
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001231 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
Dave Airlief64122c2013-02-25 14:47:55 +10001232
1233 /* modes will be validated against the framebuffer size */
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -03001234 qdev->ddev.mode_config.min_width = 0;
1235 qdev->ddev.mode_config.min_height = 0;
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001236 qdev->ddev.mode_config.max_width = 8192;
1237 qdev->ddev.mode_config.max_height = 8192;
Dave Airlief64122c2013-02-25 14:47:55 +10001238
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001239 qdev->ddev.mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001240
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001241 drm_mode_create_suggested_offset_properties(&qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001242 qxl_mode_create_hotplug_mode_update_property(qdev);
1243
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001244 for (i = 0 ; i < qxl_num_crtc; ++i) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001245 qdev_crtc_init(&qdev->ddev, i);
1246 qdev_output_init(&qdev->ddev, i);
Dave Airlief64122c2013-02-25 14:47:55 +10001247 }
1248
Gerd Hoffmannc50fad82017-03-01 11:12:33 +01001249 qxl_display_read_client_monitors_config(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001250 qdev->mode_info.mode_config_initialized = true;
1251
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -03001252 drm_mode_config_reset(&qdev->ddev);
1253
Dave Airlief64122c2013-02-25 14:47:55 +10001254 /* primary surface must be created by this point, to allow
1255 * issuing command queue commands and having them read by
1256 * spice server. */
1257 qxl_fbdev_init(qdev);
1258 return 0;
1259}
1260
1261void qxl_modeset_fini(struct qxl_device *qdev)
1262{
1263 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001264
1265 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001266 if (qdev->mode_info.mode_config_initialized) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001267 drm_mode_config_cleanup(&qdev->ddev);
Dave Airlief64122c2013-02-25 14:47:55 +10001268 qdev->mode_info.mode_config_initialized = false;
1269 }
1270}