blob: 7d08a26c3a8bff0a4184012a653d88a53b5612e7 [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);
Gerd Hoffmann735581a2018-04-20 09:19:01 +020051 if (!qdev->client_monitors_config)
Dave Airlief64122c2013-02-25 14:47:55 +100052 return;
Dave Airlief64122c2013-02-25 14:47:55 +100053 }
54 qdev->client_monitors_config->count = count;
55}
56
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010057enum {
58 MONITORS_CONFIG_MODIFIED,
59 MONITORS_CONFIG_UNCHANGED,
60 MONITORS_CONFIG_BAD_CRC,
61};
62
Dave Airlief64122c2013-02-25 14:47:55 +100063static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
64{
65 int i;
66 int num_monitors;
67 uint32_t crc;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010068 int status = MONITORS_CONFIG_UNCHANGED;
Dave Airlief64122c2013-02-25 14:47:55 +100069
Dave Airlief64122c2013-02-25 14:47:55 +100070 num_monitors = qdev->rom->client_monitors_config.count;
71 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
72 sizeof(qdev->rom->client_monitors_config));
Gerd Hoffmann735581a2018-04-20 09:19:01 +020073 if (crc != qdev->rom->client_monitors_config_crc)
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010074 return MONITORS_CONFIG_BAD_CRC;
Gerd Hoffmannc50fad82017-03-01 11:12:33 +010075 if (!num_monitors) {
76 DRM_DEBUG_KMS("no client monitors configured\n");
77 return status;
78 }
Dave Airlief64122c2013-02-25 14:47:55 +100079 if (num_monitors > qdev->monitors_config->max_allowed) {
Dave Airlie5b8788c2013-07-01 14:14:38 +100080 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
81 qdev->monitors_config->max_allowed, num_monitors);
Dave Airlief64122c2013-02-25 14:47:55 +100082 num_monitors = qdev->monitors_config->max_allowed;
83 } else {
84 num_monitors = qdev->rom->client_monitors_config.count;
85 }
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010086 if (qdev->client_monitors_config
87 && (num_monitors != qdev->client_monitors_config->count)) {
88 status = MONITORS_CONFIG_MODIFIED;
89 }
Dave Airlief64122c2013-02-25 14:47:55 +100090 qxl_alloc_client_monitors_config(qdev, num_monitors);
91 /* we copy max from the client but it isn't used */
92 qdev->client_monitors_config->max_allowed =
93 qdev->monitors_config->max_allowed;
94 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
95 struct qxl_urect *c_rect =
96 &qdev->rom->client_monitors_config.heads[i];
97 struct qxl_head *client_head =
98 &qdev->client_monitors_config->heads[i];
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010099 if (client_head->x != c_rect->left) {
100 client_head->x = c_rect->left;
101 status = MONITORS_CONFIG_MODIFIED;
102 }
103 if (client_head->y != c_rect->top) {
104 client_head->y = c_rect->top;
105 status = MONITORS_CONFIG_MODIFIED;
106 }
107 if (client_head->width != c_rect->right - c_rect->left) {
108 client_head->width = c_rect->right - c_rect->left;
109 status = MONITORS_CONFIG_MODIFIED;
110 }
111 if (client_head->height != c_rect->bottom - c_rect->top) {
112 client_head->height = c_rect->bottom - c_rect->top;
113 status = MONITORS_CONFIG_MODIFIED;
114 }
115 if (client_head->surface_id != 0) {
116 client_head->surface_id = 0;
117 status = MONITORS_CONFIG_MODIFIED;
118 }
119 if (client_head->id != i) {
120 client_head->id = i;
121 status = MONITORS_CONFIG_MODIFIED;
122 }
123 if (client_head->flags != 0) {
124 client_head->flags = 0;
125 status = MONITORS_CONFIG_MODIFIED;
126 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100127 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
128 client_head->x, client_head->y);
Dave Airlief64122c2013-02-25 14:47:55 +1000129 }
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100130
131 return status;
Dave Airlief64122c2013-02-25 14:47:55 +1000132}
133
Dave Airlie7dea0942014-10-28 11:28:44 +1000134static void qxl_update_offset_props(struct qxl_device *qdev)
135{
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200136 struct drm_device *dev = &qdev->ddev;
Dave Airlie7dea0942014-10-28 11:28:44 +1000137 struct drm_connector *connector;
138 struct qxl_output *output;
139 struct qxl_head *head;
140
141 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
142 output = drm_connector_to_qxl_output(connector);
143
144 head = &qdev->client_monitors_config->heads[output->index];
145
146 drm_object_property_set_value(&connector->base,
147 dev->mode_config.suggested_x_property, head->x);
148 drm_object_property_set_value(&connector->base,
149 dev->mode_config.suggested_y_property, head->y);
150 }
151}
152
Dave Airlief64122c2013-02-25 14:47:55 +1000153void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
154{
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200155 struct drm_device *dev = &qdev->ddev;
Gerd Hoffmann90621552017-03-01 11:12:32 +0100156 int status, retries;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100157
Gerd Hoffmann90621552017-03-01 11:12:32 +0100158 for (retries = 0; retries < 10; retries++) {
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100159 status = qxl_display_copy_rom_client_monitors_config(qdev);
Gerd Hoffmann90621552017-03-01 11:12:32 +0100160 if (status != MONITORS_CONFIG_BAD_CRC)
161 break;
162 udelay(5);
163 }
164 if (status == MONITORS_CONFIG_BAD_CRC) {
Gerd Hoffmann90621552017-03-01 11:12:32 +0100165 DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
166 return;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100167 }
168 if (status == MONITORS_CONFIG_UNCHANGED) {
Gerd Hoffmann90621552017-03-01 11:12:32 +0100169 DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100170 return;
Dave Airlief64122c2013-02-25 14:47:55 +1000171 }
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200172
Dave Airlie7dea0942014-10-28 11:28:44 +1000173 drm_modeset_lock_all(dev);
174 qxl_update_offset_props(qdev);
175 drm_modeset_unlock_all(dev);
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200176 if (!drm_helper_hpd_irq_event(dev)) {
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200177 /* notify that the monitor configuration changed, to
178 adjust at the arbitrary resolution */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200179 drm_kms_helper_hotplug_event(dev);
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200180 }
Dave Airlief64122c2013-02-25 14:47:55 +1000181}
182
Marc-André Lureaub0807422013-10-18 16:11:31 +0200183static int qxl_add_monitors_config_modes(struct drm_connector *connector,
184 unsigned *pwidth,
185 unsigned *pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000186{
187 struct drm_device *dev = connector->dev;
188 struct qxl_device *qdev = dev->dev_private;
189 struct qxl_output *output = drm_connector_to_qxl_output(connector);
190 int h = output->index;
191 struct drm_display_mode *mode = NULL;
192 struct qxl_head *head;
193
Gerd Hoffmann2d856f942017-03-01 11:12:34 +0100194 if (!qdev->monitors_config)
195 return 0;
196 if (h >= qdev->monitors_config->max_allowed)
197 return 0;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100198 if (!qdev->client_monitors_config)
Dave Airlief64122c2013-02-25 14:47:55 +1000199 return 0;
Gerd Hoffmann2d856f942017-03-01 11:12:34 +0100200 if (h >= qdev->client_monitors_config->count)
201 return 0;
202
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100203 head = &qdev->client_monitors_config->heads[h];
Gerd Hoffmann2d856f942017-03-01 11:12:34 +0100204 DRM_DEBUG_KMS("head %d is %dx%d\n", h, head->width, head->height);
Dave Airlief64122c2013-02-25 14:47:55 +1000205
206 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
207 false);
208 mode->type |= DRM_MODE_TYPE_PREFERRED;
Christophe Fergeauff996e72016-11-08 10:12:09 +0100209 mode->hdisplay = head->width;
210 mode->vdisplay = head->height;
211 drm_mode_set_name(mode);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200212 *pwidth = head->width;
213 *pheight = head->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000214 drm_mode_probed_add(connector, mode);
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500215 /* remember the last custom size for mode validation */
216 qdev->monitors_config_width = mode->hdisplay;
217 qdev->monitors_config_height = mode->vdisplay;
Dave Airlief64122c2013-02-25 14:47:55 +1000218 return 1;
219}
220
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500221static struct mode_size {
222 int w;
223 int h;
224} common_modes[] = {
225 { 640, 480},
226 { 720, 480},
227 { 800, 600},
228 { 848, 480},
229 {1024, 768},
230 {1152, 768},
231 {1280, 720},
232 {1280, 800},
233 {1280, 854},
234 {1280, 960},
235 {1280, 1024},
236 {1440, 900},
237 {1400, 1050},
238 {1680, 1050},
239 {1600, 1200},
240 {1920, 1080},
241 {1920, 1200}
242};
243
Marc-André Lureaub0807422013-10-18 16:11:31 +0200244static int qxl_add_common_modes(struct drm_connector *connector,
245 unsigned pwidth,
246 unsigned pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000247{
248 struct drm_device *dev = connector->dev;
249 struct drm_display_mode *mode = NULL;
250 int i;
Dave Airlief64122c2013-02-25 14:47:55 +1000251 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
Dave Airlief64122c2013-02-25 14:47:55 +1000252 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
253 60, false, false, false);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200254 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000255 mode->type |= DRM_MODE_TYPE_PREFERRED;
256 drm_mode_probed_add(connector, mode);
257 }
258 return i - 1;
259}
260
Gerd Hoffmann998010b2018-04-20 09:19:02 +0200261static void qxl_send_monitors_config(struct qxl_device *qdev)
262{
263 int i;
264
265 BUG_ON(!qdev->ram_header->monitors_config);
266
267 if (qdev->monitors_config->count == 0)
268 return;
269
270 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
271 struct qxl_head *head = &qdev->monitors_config->heads[i];
272
273 if (head->y > 8192 || head->x > 8192 ||
274 head->width > 8192 || head->height > 8192) {
275 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
276 i, head->width, head->height,
277 head->x, head->y);
278 return;
279 }
280 }
281 qxl_io_monitors_config(qdev);
282}
283
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300284static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
285 struct drm_crtc_state *old_crtc_state)
286{
287 struct drm_device *dev = crtc->dev;
288 struct drm_pending_vblank_event *event;
289 unsigned long flags;
290
291 if (crtc->state && crtc->state->event) {
292 event = crtc->state->event;
293 crtc->state->event = NULL;
294
295 spin_lock_irqsave(&dev->event_lock, flags);
296 drm_crtc_send_vblank_event(crtc, event);
297 spin_unlock_irqrestore(&dev->event_lock, flags);
298 }
299}
300
Dave Airlief64122c2013-02-25 14:47:55 +1000301static void qxl_crtc_destroy(struct drm_crtc *crtc)
302{
303 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
304
Ray Strode94280882017-11-27 16:50:10 -0500305 qxl_bo_unref(&qxl_crtc->cursor_bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000306 drm_crtc_cleanup(crtc);
307 kfree(qxl_crtc);
308}
309
Dave Airlief64122c2013-02-25 14:47:55 +1000310static const struct drm_crtc_funcs qxl_crtc_funcs = {
Gabriel Krisman Bertazibc8a00d2017-02-27 17:43:26 -0300311 .set_config = drm_atomic_helper_set_config,
Dave Airlief64122c2013-02-25 14:47:55 +1000312 .destroy = qxl_crtc_destroy,
Gabriel Krisman Bertazi9973c872017-02-27 17:43:27 -0300313 .page_flip = drm_atomic_helper_page_flip,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300314 .reset = drm_atomic_helper_crtc_reset,
315 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
316 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Dave Airlief64122c2013-02-25 14:47:55 +1000317};
318
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200319void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
Dave Airlief64122c2013-02-25 14:47:55 +1000320{
321 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200322 struct qxl_bo *bo = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000323
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200324 WARN_ON(bo->shadow);
Santha Meena Ramamoorthy2793c1d2018-03-20 11:29:27 -0700325 drm_gem_object_put_unlocked(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000326 drm_framebuffer_cleanup(fb);
327 kfree(qxl_fb);
328}
329
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000330static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
331 struct drm_file *file_priv,
332 unsigned flags, unsigned color,
333 struct drm_clip_rect *clips,
334 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000335{
336 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
337 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
338 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
339 struct drm_clip_rect norect;
340 struct qxl_bo *qobj;
341 int inc = 1;
342
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200343 drm_modeset_lock_all(fb->dev);
344
Dave Airlief64122c2013-02-25 14:47:55 +1000345 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000346 /* if we aren't primary surface ignore this */
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200347 if (!qobj->is_primary) {
348 drm_modeset_unlock_all(fb->dev);
Dave Airlieb2b44652013-05-13 12:48:40 +1000349 return 0;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200350 }
Dave Airlieb2b44652013-05-13 12:48:40 +1000351
Dave Airlief64122c2013-02-25 14:47:55 +1000352 if (!num_clips) {
353 num_clips = 1;
354 clips = &norect;
355 norect.x1 = norect.y1 = 0;
356 norect.x2 = fb->width;
357 norect.y2 = fb->height;
358 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
359 num_clips /= 2;
360 inc = 2; /* skip source rects */
361 }
362
363 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
364 clips, num_clips, inc);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200365
366 drm_modeset_unlock_all(fb->dev);
367
Dave Airlief64122c2013-02-25 14:47:55 +1000368 return 0;
369}
370
371static const struct drm_framebuffer_funcs qxl_fb_funcs = {
372 .destroy = qxl_user_framebuffer_destroy,
373 .dirty = qxl_framebuffer_surface_dirty,
374/* TODO?
375 * .create_handle = qxl_user_framebuffer_create_handle, */
376};
377
378int
379qxl_framebuffer_init(struct drm_device *dev,
380 struct qxl_framebuffer *qfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200381 const struct drm_mode_fb_cmd2 *mode_cmd,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200382 struct drm_gem_object *obj,
383 const struct drm_framebuffer_funcs *funcs)
Dave Airlief64122c2013-02-25 14:47:55 +1000384{
385 int ret;
386
387 qfb->obj = obj;
Ville Syrjälä53609432016-11-18 21:52:51 +0200388 drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200389 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000390 if (ret) {
391 qfb->obj = NULL;
392 return ret;
393 }
Dave Airlief64122c2013-02-25 14:47:55 +1000394 return 0;
395}
396
Dave Airlief64122c2013-02-25 14:47:55 +1000397static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
398 const struct drm_display_mode *mode,
399 struct drm_display_mode *adjusted_mode)
400{
Dave Airlief64122c2013-02-25 14:47:55 +1000401 return true;
402}
403
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100404static void qxl_monitors_config_set(struct qxl_device *qdev,
405 int index,
406 unsigned x, unsigned y,
407 unsigned width, unsigned height,
408 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000409{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100410 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
411 qdev->monitors_config->heads[index].x = x;
412 qdev->monitors_config->heads[index].y = y;
413 qdev->monitors_config->heads[index].width = width;
414 qdev->monitors_config->heads[index].height = height;
415 qdev->monitors_config->heads[index].surface_id = surf_id;
416
Dave Airlief64122c2013-02-25 14:47:55 +1000417}
418
Gerd Hoffmann45dfe572017-06-20 13:39:15 +0200419static void qxl_mode_set_nofb(struct drm_crtc *crtc)
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300420{
421 struct qxl_device *qdev = crtc->dev->dev_private;
422 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
423 struct drm_display_mode *mode = &crtc->mode;
424
425 DRM_DEBUG("Mode set (%d,%d)\n",
426 mode->hdisplay, mode->vdisplay);
427
428 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
429 mode->hdisplay, mode->vdisplay, 0);
430
431}
432
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300433static void qxl_crtc_atomic_enable(struct drm_crtc *crtc,
434 struct drm_crtc_state *old_state)
Dave Airlief64122c2013-02-25 14:47:55 +1000435{
436 DRM_DEBUG("\n");
437}
438
Laurent Pinchart64581712017-06-30 12:36:45 +0300439static void qxl_crtc_atomic_disable(struct drm_crtc *crtc,
440 struct drm_crtc_state *old_state)
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100441{
442 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Gabriel Krisman Bertazi37235452017-02-27 17:43:22 -0300443 struct qxl_device *qdev = crtc->dev->dev_private;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100444
445 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
446
447 qxl_send_monitors_config(qdev);
448}
449
Dave Airlief64122c2013-02-25 14:47:55 +1000450static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
Dave Airlief64122c2013-02-25 14:47:55 +1000451 .mode_fixup = qxl_crtc_mode_fixup,
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300452 .mode_set_nofb = qxl_mode_set_nofb,
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300453 .atomic_flush = qxl_crtc_atomic_flush,
Laurent Pinchart0b20a0f2017-06-30 12:36:44 +0300454 .atomic_enable = qxl_crtc_atomic_enable,
Laurent Pinchart64581712017-06-30 12:36:45 +0300455 .atomic_disable = qxl_crtc_atomic_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000456};
457
Gerd Hoffmann45dfe572017-06-20 13:39:15 +0200458static int qxl_primary_atomic_check(struct drm_plane *plane,
459 struct drm_plane_state *state)
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300460{
461 struct qxl_device *qdev = plane->dev->dev_private;
462 struct qxl_framebuffer *qfb;
463 struct qxl_bo *bo;
464
465 if (!state->crtc || !state->fb)
466 return 0;
467
468 qfb = to_qxl_framebuffer(state->fb);
469 bo = gem_to_qxl_bo(qfb->obj);
470
471 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
472 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
473 return -EINVAL;
474 }
475
476 return 0;
477}
478
Ray Strode94280882017-11-27 16:50:10 -0500479static int qxl_primary_apply_cursor(struct drm_plane *plane)
480{
481 struct drm_device *dev = plane->dev;
482 struct qxl_device *qdev = dev->dev_private;
483 struct drm_framebuffer *fb = plane->state->fb;
484 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
485 struct qxl_cursor_cmd *cmd;
486 struct qxl_release *release;
487 int ret = 0;
488
489 if (!qcrtc->cursor_bo)
490 return 0;
491
492 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
493 QXL_RELEASE_CURSOR_CMD,
494 &release, NULL);
495 if (ret)
496 return ret;
497
498 ret = qxl_release_list_add(release, qcrtc->cursor_bo);
499 if (ret)
500 goto out_free_release;
501
502 ret = qxl_release_reserve_list(release, false);
503 if (ret)
504 goto out_free_release;
505
506 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
507 cmd->type = QXL_CURSOR_SET;
508 cmd->u.set.position.x = plane->state->crtc_x + fb->hot_x;
509 cmd->u.set.position.y = plane->state->crtc_y + fb->hot_y;
510
511 cmd->u.set.shape = qxl_bo_physical_address(qdev, qcrtc->cursor_bo, 0);
512
513 cmd->u.set.visible = 1;
514 qxl_release_unmap(qdev, release, &cmd->release_info);
515
516 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
517 qxl_release_fence_buffer_objects(release);
518
519 return ret;
520
521out_free_release:
522 qxl_release_free(qdev, release);
523 return ret;
524}
525
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300526static void qxl_primary_atomic_update(struct drm_plane *plane,
527 struct drm_plane_state *old_state)
528{
529 struct qxl_device *qdev = plane->dev->dev_private;
530 struct qxl_framebuffer *qfb =
531 to_qxl_framebuffer(plane->state->fb);
532 struct qxl_framebuffer *qfb_old;
533 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
534 struct qxl_bo *bo_old;
535 struct drm_clip_rect norect = {
536 .x1 = 0,
537 .y1 = 0,
538 .x2 = qfb->base.width,
539 .y2 = qfb->base.height
540 };
Ray Strode94280882017-11-27 16:50:10 -0500541 int ret;
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200542 bool same_shadow = false;
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300543
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200544 if (old_state->fb) {
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300545 qfb_old = to_qxl_framebuffer(old_state->fb);
546 bo_old = gem_to_qxl_bo(qfb_old->obj);
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200547 } else {
548 bo_old = NULL;
549 }
550
551 if (bo == bo_old)
552 return;
553
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200554 if (bo_old && bo_old->shadow && bo->shadow &&
555 bo_old->shadow == bo->shadow) {
556 same_shadow = true;
557 }
558
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200559 if (bo_old && bo_old->is_primary) {
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200560 if (!same_shadow)
561 qxl_io_destroy_primary(qdev);
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300562 bo_old->is_primary = false;
Ray Strode94280882017-11-27 16:50:10 -0500563
564 ret = qxl_primary_apply_cursor(plane);
565 if (ret)
566 DRM_ERROR(
567 "could not set cursor after creating primary");
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300568 }
569
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200570 if (!bo->is_primary) {
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200571 if (!same_shadow)
572 qxl_io_create_primary(qdev, 0, bo);
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200573 bo->is_primary = true;
574 }
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200575
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300576 qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
577}
578
579static void qxl_primary_atomic_disable(struct drm_plane *plane,
580 struct drm_plane_state *old_state)
581{
582 struct qxl_device *qdev = plane->dev->dev_private;
583
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200584 if (old_state->fb) {
585 struct qxl_framebuffer *qfb =
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300586 to_qxl_framebuffer(old_state->fb);
587 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
588
Gerd Hoffmannb0e07da2017-09-11 11:39:50 +0200589 if (bo->is_primary) {
590 qxl_io_destroy_primary(qdev);
591 bo->is_primary = false;
592 }
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300593 }
594}
595
Gerd Hoffmann45dfe572017-06-20 13:39:15 +0200596static int qxl_plane_atomic_check(struct drm_plane *plane,
597 struct drm_plane_state *state)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300598{
599 return 0;
600}
601
602static void qxl_cursor_atomic_update(struct drm_plane *plane,
603 struct drm_plane_state *old_state)
604{
605 struct drm_device *dev = plane->dev;
606 struct qxl_device *qdev = dev->dev_private;
607 struct drm_framebuffer *fb = plane->state->fb;
Ray Strode94280882017-11-27 16:50:10 -0500608 struct qxl_crtc *qcrtc = to_qxl_crtc(plane->state->crtc);
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300609 struct qxl_release *release;
610 struct qxl_cursor_cmd *cmd;
611 struct qxl_cursor *cursor;
612 struct drm_gem_object *obj;
Ray Strode16c6db32017-11-27 16:50:09 -0500613 struct qxl_bo *cursor_bo = NULL, *user_bo = NULL;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300614 int ret;
615 void *user_ptr;
616 int size = 64*64*4;
617
618 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
619 QXL_RELEASE_CURSOR_CMD,
620 &release, NULL);
Dan Carpenteree5cb7c2017-03-14 10:54:10 +0300621 if (ret)
622 return;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300623
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300624 if (fb != old_state->fb) {
625 obj = to_qxl_framebuffer(fb)->obj;
626 user_bo = gem_to_qxl_bo(obj);
627
628 /* pinning is done in the prepare/cleanup framevbuffer */
629 ret = qxl_bo_kmap(user_bo, &user_ptr);
630 if (ret)
631 goto out_free_release;
632
633 ret = qxl_alloc_bo_reserved(qdev, release,
634 sizeof(struct qxl_cursor) + size,
635 &cursor_bo);
636 if (ret)
637 goto out_kunmap;
638
639 ret = qxl_release_reserve_list(release, true);
640 if (ret)
641 goto out_free_bo;
642
643 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
644 if (ret)
645 goto out_backoff;
646
647 cursor->header.unique = 0;
648 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
649 cursor->header.width = 64;
650 cursor->header.height = 64;
651 cursor->header.hot_spot_x = fb->hot_x;
652 cursor->header.hot_spot_y = fb->hot_y;
653 cursor->data_size = size;
654 cursor->chunk.next_chunk = 0;
655 cursor->chunk.prev_chunk = 0;
656 cursor->chunk.data_size = size;
657 memcpy(cursor->chunk.data, user_ptr, size);
658 qxl_bo_kunmap(cursor_bo);
659 qxl_bo_kunmap(user_bo);
660
Gabriel Krisman Bertazi429030b2017-05-19 14:58:19 -0300661 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300662 cmd->u.set.visible = 1;
663 cmd->u.set.shape = qxl_bo_physical_address(qdev,
664 cursor_bo, 0);
665 cmd->type = QXL_CURSOR_SET;
Ray Strode94280882017-11-27 16:50:10 -0500666
667 qxl_bo_unref(&qcrtc->cursor_bo);
668 qcrtc->cursor_bo = cursor_bo;
669 cursor_bo = NULL;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300670 } else {
671
672 ret = qxl_release_reserve_list(release, true);
673 if (ret)
674 goto out_free_release;
675
Gabriel Krisman Bertazi429030b2017-05-19 14:58:19 -0300676 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300677 cmd->type = QXL_CURSOR_MOVE;
678 }
679
680 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
681 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
682
683 qxl_release_unmap(qdev, release, &cmd->release_info);
684 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
685 qxl_release_fence_buffer_objects(release);
686
Ray Strode16c6db32017-11-27 16:50:09 -0500687 qxl_bo_unref(&cursor_bo);
688
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300689 return;
690
691out_backoff:
692 qxl_release_backoff_reserve_list(release);
693out_free_bo:
694 qxl_bo_unref(&cursor_bo);
695out_kunmap:
696 qxl_bo_kunmap(user_bo);
697out_free_release:
698 qxl_release_free(qdev, release);
699 return;
700
701}
702
Gerd Hoffmann45dfe572017-06-20 13:39:15 +0200703static void qxl_cursor_atomic_disable(struct drm_plane *plane,
704 struct drm_plane_state *old_state)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300705{
706 struct qxl_device *qdev = plane->dev->dev_private;
707 struct qxl_release *release;
708 struct qxl_cursor_cmd *cmd;
709 int ret;
710
711 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
712 QXL_RELEASE_CURSOR_CMD,
713 &release, NULL);
714 if (ret)
715 return;
716
717 ret = qxl_release_reserve_list(release, true);
718 if (ret) {
719 qxl_release_free(qdev, release);
720 return;
721 }
722
723 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
724 cmd->type = QXL_CURSOR_HIDE;
725 qxl_release_unmap(qdev, release, &cmd->release_info);
726
727 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
728 qxl_release_fence_buffer_objects(release);
729}
730
Gerd Hoffmann45dfe572017-06-20 13:39:15 +0200731static int qxl_plane_prepare_fb(struct drm_plane *plane,
732 struct drm_plane_state *new_state)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300733{
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200734 struct qxl_device *qdev = plane->dev->dev_private;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300735 struct drm_gem_object *obj;
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200736 struct qxl_bo *user_bo, *old_bo = NULL;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300737 int ret;
738
739 if (!new_state->fb)
740 return 0;
741
742 obj = to_qxl_framebuffer(new_state->fb)->obj;
743 user_bo = gem_to_qxl_bo(obj);
744
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200745 if (plane->type == DRM_PLANE_TYPE_PRIMARY &&
746 user_bo->is_dumb && !user_bo->shadow) {
747 if (plane->state->fb) {
748 obj = to_qxl_framebuffer(plane->state->fb)->obj;
749 old_bo = gem_to_qxl_bo(obj);
750 }
751 if (old_bo && old_bo->shadow &&
752 user_bo->gem_base.size == old_bo->gem_base.size &&
753 plane->state->crtc == new_state->crtc &&
754 plane->state->crtc_w == new_state->crtc_w &&
755 plane->state->crtc_h == new_state->crtc_h &&
756 plane->state->src_x == new_state->src_x &&
757 plane->state->src_y == new_state->src_y &&
758 plane->state->src_w == new_state->src_w &&
759 plane->state->src_h == new_state->src_h &&
760 plane->state->rotation == new_state->rotation &&
761 plane->state->zpos == new_state->zpos) {
762 drm_gem_object_get(&old_bo->shadow->gem_base);
763 user_bo->shadow = old_bo->shadow;
764 } else {
765 qxl_bo_create(qdev, user_bo->gem_base.size,
766 true, true, QXL_GEM_DOMAIN_VRAM, NULL,
767 &user_bo->shadow);
768 }
769 }
770
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300771 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
772 if (ret)
773 return ret;
774
775 return 0;
776}
777
778static void qxl_plane_cleanup_fb(struct drm_plane *plane,
779 struct drm_plane_state *old_state)
780{
781 struct drm_gem_object *obj;
782 struct qxl_bo *user_bo;
783
Gerd Hoffmann5f3d8622017-09-18 09:41:45 +0200784 if (!old_state->fb) {
785 /*
786 * we never executed prepare_fb, so there's nothing to
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300787 * unpin.
788 */
789 return;
790 }
791
Gerd Hoffmann5f3d8622017-09-18 09:41:45 +0200792 obj = to_qxl_framebuffer(old_state->fb)->obj;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300793 user_bo = gem_to_qxl_bo(obj);
794 qxl_bo_unpin(user_bo);
Gerd Hoffmann62676d12017-10-19 08:21:50 +0200795
796 if (user_bo->shadow && !user_bo->is_primary) {
797 drm_gem_object_put_unlocked(&user_bo->shadow->gem_base);
798 user_bo->shadow = NULL;
799 }
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300800}
801
802static const uint32_t qxl_cursor_plane_formats[] = {
803 DRM_FORMAT_ARGB8888,
804};
805
806static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
807 .atomic_check = qxl_plane_atomic_check,
808 .atomic_update = qxl_cursor_atomic_update,
809 .atomic_disable = qxl_cursor_atomic_disable,
810 .prepare_fb = qxl_plane_prepare_fb,
811 .cleanup_fb = qxl_plane_cleanup_fb,
812};
813
814static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -0300815 .update_plane = drm_atomic_helper_update_plane,
816 .disable_plane = drm_atomic_helper_disable_plane,
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300817 .destroy = drm_primary_helper_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300818 .reset = drm_atomic_helper_plane_reset,
819 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
820 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300821};
822
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300823static const uint32_t qxl_primary_plane_formats[] = {
824 DRM_FORMAT_XRGB8888,
825 DRM_FORMAT_ARGB8888,
826};
827
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300828static const struct drm_plane_helper_funcs primary_helper_funcs = {
829 .atomic_check = qxl_primary_atomic_check,
830 .atomic_update = qxl_primary_atomic_update,
831 .atomic_disable = qxl_primary_atomic_disable,
832 .prepare_fb = qxl_plane_prepare_fb,
833 .cleanup_fb = qxl_plane_cleanup_fb,
834};
835
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300836static const struct drm_plane_funcs qxl_primary_plane_funcs = {
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -0300837 .update_plane = drm_atomic_helper_update_plane,
838 .disable_plane = drm_atomic_helper_disable_plane,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300839 .destroy = drm_primary_helper_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300840 .reset = drm_atomic_helper_plane_reset,
841 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
842 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300843};
844
845static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
846 unsigned int possible_crtcs,
847 enum drm_plane_type type)
848{
849 const struct drm_plane_helper_funcs *helper_funcs = NULL;
850 struct drm_plane *plane;
851 const struct drm_plane_funcs *funcs;
852 const uint32_t *formats;
853 int num_formats;
854 int err;
855
856 if (type == DRM_PLANE_TYPE_PRIMARY) {
857 funcs = &qxl_primary_plane_funcs;
858 formats = qxl_primary_plane_formats;
859 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300860 helper_funcs = &primary_helper_funcs;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300861 } else if (type == DRM_PLANE_TYPE_CURSOR) {
862 funcs = &qxl_cursor_plane_funcs;
863 formats = qxl_cursor_plane_formats;
864 helper_funcs = &qxl_cursor_helper_funcs;
865 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300866 } else {
867 return ERR_PTR(-EINVAL);
868 }
869
870 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
871 if (!plane)
872 return ERR_PTR(-ENOMEM);
873
874 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
875 funcs, formats, num_formats,
Ben Widawskye6fc3b62017-07-23 20:46:38 -0700876 NULL, type, NULL);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300877 if (err)
878 goto free_plane;
879
880 drm_plane_helper_add(plane, helper_funcs);
881
882 return plane;
883
884free_plane:
885 kfree(plane);
886 return ERR_PTR(-EINVAL);
887}
888
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100889static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000890{
891 struct qxl_crtc *qxl_crtc;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300892 struct drm_plane *primary, *cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300893 struct qxl_device *qdev = dev->dev_private;
894 int r;
Dave Airlief64122c2013-02-25 14:47:55 +1000895
896 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
897 if (!qxl_crtc)
898 return -ENOMEM;
899
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300900 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
901 if (IS_ERR(primary)) {
902 r = -ENOMEM;
903 goto free_mem;
904 }
905
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300906 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
907 if (IS_ERR(cursor)) {
908 r = -ENOMEM;
909 goto clean_primary;
910 }
911
912 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300913 &qxl_crtc_funcs, NULL);
914 if (r)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300915 goto clean_cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300916
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100917 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000918 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
919 return 0;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300920
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300921clean_cursor:
922 drm_plane_cleanup(cursor);
923 kfree(cursor);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300924clean_primary:
925 drm_plane_cleanup(primary);
926 kfree(primary);
927free_mem:
928 kfree(qxl_crtc);
929 return r;
Dave Airlief64122c2013-02-25 14:47:55 +1000930}
931
932static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
933{
934 DRM_DEBUG("\n");
935}
936
Dave Airlief64122c2013-02-25 14:47:55 +1000937static void qxl_enc_prepare(struct drm_encoder *encoder)
938{
939 DRM_DEBUG("\n");
940}
941
942static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
943 struct drm_encoder *encoder)
944{
945 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100946 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000947 struct qxl_head *head;
948 struct drm_display_mode *mode;
949
950 BUG_ON(!encoder);
951 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100952 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000953 if (!qdev->monitors_config ||
954 qdev->monitors_config->max_allowed <= i) {
955 DRM_ERROR(
956 "head number too large or missing monitors config: %p, %d",
957 qdev->monitors_config,
958 qdev->monitors_config ?
959 qdev->monitors_config->max_allowed : -1);
960 return;
961 }
962 if (!encoder->crtc) {
963 DRM_ERROR("missing crtc on encoder %p\n", encoder);
964 return;
965 }
966 if (i != 0)
967 DRM_DEBUG("missing for multiple monitors: no head holes\n");
968 head = &qdev->monitors_config->heads[i];
969 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000970 if (encoder->crtc->enabled) {
971 mode = &encoder->crtc->mode;
972 head->width = mode->hdisplay;
973 head->height = mode->vdisplay;
974 head->x = encoder->crtc->x;
975 head->y = encoder->crtc->y;
976 if (qdev->monitors_config->count < i + 1)
977 qdev->monitors_config->count = i + 1;
978 } else {
979 head->width = 0;
980 head->height = 0;
981 head->x = 0;
982 head->y = 0;
983 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100984 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
985 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000986 head->flags = 0;
987 /* TODO - somewhere else to call this for multiple monitors
988 * (config_commit?) */
989 qxl_send_monitors_config(qdev);
990}
991
992static void qxl_enc_commit(struct drm_encoder *encoder)
993{
994 struct qxl_device *qdev = encoder->dev->dev_private;
995
996 qxl_write_monitors_config_for_encoder(qdev, encoder);
997 DRM_DEBUG("\n");
998}
999
1000static void qxl_enc_mode_set(struct drm_encoder *encoder,
1001 struct drm_display_mode *mode,
1002 struct drm_display_mode *adjusted_mode)
1003{
1004 DRM_DEBUG("\n");
1005}
1006
1007static int qxl_conn_get_modes(struct drm_connector *connector)
1008{
Marc-André Lureaub0807422013-10-18 16:11:31 +02001009 unsigned pwidth = 1024;
1010 unsigned pheight = 768;
Gerd Hoffmann2d856f942017-03-01 11:12:34 +01001011 int ret = 0;
Dave Airlief64122c2013-02-25 14:47:55 +10001012
Gerd Hoffmann2d856f942017-03-01 11:12:34 +01001013 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
1014 if (ret < 0)
1015 return ret;
Marc-André Lureaub0807422013-10-18 16:11:31 +02001016 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +10001017 return ret;
1018}
1019
Luc Van Oostenrycke0d92e12018-04-24 15:15:15 +02001020static enum drm_mode_status qxl_conn_mode_valid(struct drm_connector *connector,
Dave Airlief64122c2013-02-25 14:47:55 +10001021 struct drm_display_mode *mode)
1022{
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -05001023 struct drm_device *ddev = connector->dev;
1024 struct qxl_device *qdev = ddev->dev_private;
1025 int i;
1026
Dave Airlief64122c2013-02-25 14:47:55 +10001027 /* TODO: is this called for user defined modes? (xrandr --add-mode)
1028 * TODO: check that the mode fits in the framebuffer */
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -05001029
1030 if(qdev->monitors_config_width == mode->hdisplay &&
1031 qdev->monitors_config_height == mode->vdisplay)
1032 return MODE_OK;
1033
1034 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
1035 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
1036 return MODE_OK;
1037 }
1038 return MODE_BAD;
Dave Airlief64122c2013-02-25 14:47:55 +10001039}
1040
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001041static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +10001042{
1043 struct qxl_output *qxl_output =
1044 drm_connector_to_qxl_output(connector);
1045
1046 DRM_DEBUG("\n");
1047 return &qxl_output->enc;
1048}
1049
1050
1051static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1052 .dpms = qxl_enc_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001053 .prepare = qxl_enc_prepare,
1054 .mode_set = qxl_enc_mode_set,
1055 .commit = qxl_enc_commit,
1056};
1057
1058static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1059 .get_modes = qxl_conn_get_modes,
1060 .mode_valid = qxl_conn_mode_valid,
1061 .best_encoder = qxl_best_encoder,
1062};
1063
Dave Airlief64122c2013-02-25 14:47:55 +10001064static enum drm_connector_status qxl_conn_detect(
1065 struct drm_connector *connector,
1066 bool force)
1067{
1068 struct qxl_output *output =
1069 drm_connector_to_qxl_output(connector);
1070 struct drm_device *ddev = connector->dev;
1071 struct qxl_device *qdev = ddev->dev_private;
Dave Airlie69e5d3f2015-09-14 10:28:34 +10001072 bool connected = false;
Dave Airlief64122c2013-02-25 14:47:55 +10001073
1074 /* The first monitor is always connected */
Dave Airlie69e5d3f2015-09-14 10:28:34 +10001075 if (!qdev->client_monitors_config) {
1076 if (output->index == 0)
1077 connected = true;
1078 } else
1079 connected = qdev->client_monitors_config->count > output->index &&
1080 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
Dave Airlief64122c2013-02-25 14:47:55 +10001081
Marc-André Lureau5cab51c2013-10-18 16:11:33 +02001082 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1083 if (!connected)
1084 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
1085
Dave Airlief64122c2013-02-25 14:47:55 +10001086 return connected ? connector_status_connected
1087 : connector_status_disconnected;
1088}
1089
1090static int qxl_conn_set_property(struct drm_connector *connector,
1091 struct drm_property *property,
1092 uint64_t value)
1093{
1094 DRM_DEBUG("\n");
1095 return 0;
1096}
1097
1098static void qxl_conn_destroy(struct drm_connector *connector)
1099{
1100 struct qxl_output *qxl_output =
1101 drm_connector_to_qxl_output(connector);
1102
Thomas Wood34ea3d32014-05-29 16:57:41 +01001103 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001104 drm_connector_cleanup(connector);
1105 kfree(qxl_output);
1106}
1107
1108static const struct drm_connector_funcs qxl_connector_funcs = {
1109 .dpms = drm_helper_connector_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001110 .detect = qxl_conn_detect,
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001111 .fill_modes = drm_helper_probe_single_connector_modes,
Dave Airlief64122c2013-02-25 14:47:55 +10001112 .set_property = qxl_conn_set_property,
1113 .destroy = qxl_conn_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -03001114 .reset = drm_atomic_helper_connector_reset,
1115 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1116 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Dave Airlief64122c2013-02-25 14:47:55 +10001117};
1118
1119static void qxl_enc_destroy(struct drm_encoder *encoder)
1120{
1121 drm_encoder_cleanup(encoder);
1122}
1123
1124static const struct drm_encoder_funcs qxl_enc_funcs = {
1125 .destroy = qxl_enc_destroy,
1126};
1127
Dave Airlie4695b032013-10-11 11:05:00 +10001128static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1129{
1130 if (qdev->hotplug_mode_update_property)
1131 return 0;
1132
1133 qdev->hotplug_mode_update_property =
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001134 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlie4695b032013-10-11 11:05:00 +10001135 "hotplug_mode_update", 0, 1);
1136
1137 return 0;
1138}
1139
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001140static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +10001141{
Dave Airlie4695b032013-10-11 11:05:00 +10001142 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +10001143 struct qxl_output *qxl_output;
1144 struct drm_connector *connector;
1145 struct drm_encoder *encoder;
1146
1147 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1148 if (!qxl_output)
1149 return -ENOMEM;
1150
1151 qxl_output->index = num_output;
1152
1153 connector = &qxl_output->base;
1154 encoder = &qxl_output->enc;
1155 drm_connector_init(dev, &qxl_output->base,
1156 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1157
1158 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001159 DRM_MODE_ENCODER_VIRTUAL, NULL);
Dave Airlief64122c2013-02-25 14:47:55 +10001160
Dave Airlie5ff91e42013-07-05 10:20:33 +10001161 /* we get HPD via client monitors config */
1162 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +10001163 encoder->possible_crtcs = 1 << num_output;
1164 drm_mode_connector_attach_encoder(&qxl_output->base,
1165 &qxl_output->enc);
1166 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1167 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1168
Dave Airlie4695b032013-10-11 11:05:00 +10001169 drm_object_attach_property(&connector->base,
1170 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +10001171 drm_object_attach_property(&connector->base,
1172 dev->mode_config.suggested_x_property, 0);
1173 drm_object_attach_property(&connector->base,
1174 dev->mode_config.suggested_y_property, 0);
Dave Airlief64122c2013-02-25 14:47:55 +10001175 return 0;
1176}
1177
1178static struct drm_framebuffer *
1179qxl_user_framebuffer_create(struct drm_device *dev,
1180 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02001181 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief64122c2013-02-25 14:47:55 +10001182{
1183 struct drm_gem_object *obj;
1184 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +10001185 int ret;
1186
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001187 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1188 if (!obj)
1189 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +10001190
1191 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1192 if (qxl_fb == NULL)
1193 return NULL;
1194
Noralf Trønnes6819c3c2016-04-28 17:18:36 +02001195 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +10001196 if (ret) {
1197 kfree(qxl_fb);
Santha Meena Ramamoorthy2793c1d2018-03-20 11:29:27 -07001198 drm_gem_object_put_unlocked(obj);
Dave Airlief64122c2013-02-25 14:47:55 +10001199 return NULL;
1200 }
1201
Dave Airlief64122c2013-02-25 14:47:55 +10001202 return &qxl_fb->base;
1203}
1204
1205static const struct drm_mode_config_funcs qxl_mode_funcs = {
1206 .fb_create = qxl_user_framebuffer_create,
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -03001207 .atomic_check = drm_atomic_helper_check,
1208 .atomic_commit = drm_atomic_helper_commit,
Dave Airlief64122c2013-02-25 14:47:55 +10001209};
1210
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001211int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001212{
Dave Airlief64122c2013-02-25 14:47:55 +10001213 int ret;
1214 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001215 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001216 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001217 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001218
Dave Airlief64122c2013-02-25 14:47:55 +10001219 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1220 QXL_GEM_DOMAIN_VRAM,
1221 false, false, NULL, &gobj);
1222 if (ret) {
1223 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1224 return -ENOMEM;
1225 }
1226 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001227
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001228 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001229 if (ret)
1230 return ret;
1231
Dave Airlief64122c2013-02-25 14:47:55 +10001232 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001233
Dave Airlief64122c2013-02-25 14:47:55 +10001234 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1235 qdev->ram_header->monitors_config =
1236 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1237
1238 memset(qdev->monitors_config, 0, monitors_config_size);
1239 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001240 return 0;
1241}
1242
1243int qxl_destroy_monitors_object(struct qxl_device *qdev)
1244{
1245 int ret;
1246
1247 qdev->monitors_config = NULL;
1248 qdev->ram_header->monitors_config = 0;
1249
1250 qxl_bo_kunmap(qdev->monitors_config_bo);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001251 ret = qxl_bo_unpin(qdev->monitors_config_bo);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001252 if (ret)
1253 return ret;
1254
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001255 qxl_bo_unref(&qdev->monitors_config_bo);
1256 return 0;
1257}
1258
1259int qxl_modeset_init(struct qxl_device *qdev)
1260{
1261 int i;
1262 int ret;
1263
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001264 drm_mode_config_init(&qdev->ddev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001265
1266 ret = qxl_create_monitors_object(qdev);
1267 if (ret)
1268 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001269
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001270 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
Dave Airlief64122c2013-02-25 14:47:55 +10001271
1272 /* modes will be validated against the framebuffer size */
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -03001273 qdev->ddev.mode_config.min_width = 0;
1274 qdev->ddev.mode_config.min_height = 0;
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001275 qdev->ddev.mode_config.max_width = 8192;
1276 qdev->ddev.mode_config.max_height = 8192;
Dave Airlief64122c2013-02-25 14:47:55 +10001277
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001278 qdev->ddev.mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001279
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001280 drm_mode_create_suggested_offset_properties(&qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001281 qxl_mode_create_hotplug_mode_update_property(qdev);
1282
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001283 for (i = 0 ; i < qxl_num_crtc; ++i) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001284 qdev_crtc_init(&qdev->ddev, i);
1285 qdev_output_init(&qdev->ddev, i);
Dave Airlief64122c2013-02-25 14:47:55 +10001286 }
1287
Gerd Hoffmannc50fad82017-03-01 11:12:33 +01001288 qxl_display_read_client_monitors_config(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001289 qdev->mode_info.mode_config_initialized = true;
1290
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -03001291 drm_mode_config_reset(&qdev->ddev);
1292
Dave Airlief64122c2013-02-25 14:47:55 +10001293 /* primary surface must be created by this point, to allow
1294 * issuing command queue commands and having them read by
1295 * spice server. */
1296 qxl_fbdev_init(qdev);
1297 return 0;
1298}
1299
1300void qxl_modeset_fini(struct qxl_device *qdev)
1301{
1302 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001303
1304 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001305 if (qdev->mode_info.mode_config_initialized) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001306 drm_mode_config_cleanup(&qdev->ddev);
Dave Airlief64122c2013-02-25 14:47:55 +10001307 qdev->mode_info.mode_config_initialized = false;
1308 }
1309}