blob: 2b99496d881ae1d032f3cb69f0349caf0ee8fcd6 [file] [log] [blame]
Dave Airlief64122c2013-02-25 14:47:55 +10001/*
2 * Copyright 2013 Red Hat Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Dave Airlie
23 * Alon Levy
24 */
25
26
Randy Dunlapc5416d662013-12-20 10:58:15 -080027#include <linux/crc32.h>
Dave Airlief64122c2013-02-25 14:47:55 +100028
29#include "qxl_drv.h"
30#include "qxl_object.h"
31#include "drm_crtc_helper.h"
Daniel Vetter3cb9ae42014-10-29 10:03:57 +010032#include <drm/drm_plane_helper.h>
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -030033#include <drm/drm_atomic_helper.h>
Gabriel Krisman Bertazi10a0bd82017-02-27 17:43:24 -030034#include <drm/drm_atomic.h>
Dave Airlief64122c2013-02-25 14:47:55 +100035
Dave Airlie07f8d9b2013-07-02 06:37:13 +010036static bool qxl_head_enabled(struct qxl_head *head)
37{
38 return head->width && head->height;
39}
40
Christophe Fergeaue4a76442016-11-08 10:12:03 +010041static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
Dave Airlief64122c2013-02-25 14:47:55 +100042{
43 if (qdev->client_monitors_config &&
44 count > qdev->client_monitors_config->count) {
45 kfree(qdev->client_monitors_config);
Dave Airlie62c8ba72013-04-16 13:36:00 +100046 qdev->client_monitors_config = NULL;
Dave Airlief64122c2013-02-25 14:47:55 +100047 }
48 if (!qdev->client_monitors_config) {
49 qdev->client_monitors_config = kzalloc(
50 sizeof(struct qxl_monitors_config) +
51 sizeof(struct qxl_head) * count, GFP_KERNEL);
52 if (!qdev->client_monitors_config) {
53 qxl_io_log(qdev,
54 "%s: allocation failure for %u heads\n",
55 __func__, count);
56 return;
57 }
58 }
59 qdev->client_monitors_config->count = count;
60}
61
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010062enum {
63 MONITORS_CONFIG_MODIFIED,
64 MONITORS_CONFIG_UNCHANGED,
65 MONITORS_CONFIG_BAD_CRC,
66};
67
Dave Airlief64122c2013-02-25 14:47:55 +100068static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
69{
70 int i;
71 int num_monitors;
72 uint32_t crc;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010073 int status = MONITORS_CONFIG_UNCHANGED;
Dave Airlief64122c2013-02-25 14:47:55 +100074
Dave Airlief64122c2013-02-25 14:47:55 +100075 num_monitors = qdev->rom->client_monitors_config.count;
76 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
77 sizeof(qdev->rom->client_monitors_config));
78 if (crc != qdev->rom->client_monitors_config_crc) {
Frediano Ziglio72ec5652015-06-03 12:09:16 +010079 qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
Dave Airlief64122c2013-02-25 14:47:55 +100080 sizeof(qdev->rom->client_monitors_config),
81 qdev->rom->client_monitors_config_crc);
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010082 return MONITORS_CONFIG_BAD_CRC;
Dave Airlief64122c2013-02-25 14:47:55 +100083 }
84 if (num_monitors > qdev->monitors_config->max_allowed) {
Dave Airlie5b8788c2013-07-01 14:14:38 +100085 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
86 qdev->monitors_config->max_allowed, num_monitors);
Dave Airlief64122c2013-02-25 14:47:55 +100087 num_monitors = qdev->monitors_config->max_allowed;
88 } else {
89 num_monitors = qdev->rom->client_monitors_config.count;
90 }
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010091 if (qdev->client_monitors_config
92 && (num_monitors != qdev->client_monitors_config->count)) {
93 status = MONITORS_CONFIG_MODIFIED;
94 }
Dave Airlief64122c2013-02-25 14:47:55 +100095 qxl_alloc_client_monitors_config(qdev, num_monitors);
96 /* we copy max from the client but it isn't used */
97 qdev->client_monitors_config->max_allowed =
98 qdev->monitors_config->max_allowed;
99 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
100 struct qxl_urect *c_rect =
101 &qdev->rom->client_monitors_config.heads[i];
102 struct qxl_head *client_head =
103 &qdev->client_monitors_config->heads[i];
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100104 if (client_head->x != c_rect->left) {
105 client_head->x = c_rect->left;
106 status = MONITORS_CONFIG_MODIFIED;
107 }
108 if (client_head->y != c_rect->top) {
109 client_head->y = c_rect->top;
110 status = MONITORS_CONFIG_MODIFIED;
111 }
112 if (client_head->width != c_rect->right - c_rect->left) {
113 client_head->width = c_rect->right - c_rect->left;
114 status = MONITORS_CONFIG_MODIFIED;
115 }
116 if (client_head->height != c_rect->bottom - c_rect->top) {
117 client_head->height = c_rect->bottom - c_rect->top;
118 status = MONITORS_CONFIG_MODIFIED;
119 }
120 if (client_head->surface_id != 0) {
121 client_head->surface_id = 0;
122 status = MONITORS_CONFIG_MODIFIED;
123 }
124 if (client_head->id != i) {
125 client_head->id = i;
126 status = MONITORS_CONFIG_MODIFIED;
127 }
128 if (client_head->flags != 0) {
129 client_head->flags = 0;
130 status = MONITORS_CONFIG_MODIFIED;
131 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100132 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
133 client_head->x, client_head->y);
Dave Airlief64122c2013-02-25 14:47:55 +1000134 }
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100135
136 return status;
Dave Airlief64122c2013-02-25 14:47:55 +1000137}
138
Dave Airlie7dea0942014-10-28 11:28:44 +1000139static void qxl_update_offset_props(struct qxl_device *qdev)
140{
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200141 struct drm_device *dev = &qdev->ddev;
Dave Airlie7dea0942014-10-28 11:28:44 +1000142 struct drm_connector *connector;
143 struct qxl_output *output;
144 struct qxl_head *head;
145
146 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
147 output = drm_connector_to_qxl_output(connector);
148
149 head = &qdev->client_monitors_config->heads[output->index];
150
151 drm_object_property_set_value(&connector->base,
152 dev->mode_config.suggested_x_property, head->x);
153 drm_object_property_set_value(&connector->base,
154 dev->mode_config.suggested_y_property, head->y);
155 }
156}
157
Dave Airlief64122c2013-02-25 14:47:55 +1000158void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
159{
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200160 struct drm_device *dev = &qdev->ddev;
Gerd Hoffmann90621552017-03-01 11:12:32 +0100161 int status, retries;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100162
Gerd Hoffmann90621552017-03-01 11:12:32 +0100163 for (retries = 0; retries < 10; retries++) {
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100164 status = qxl_display_copy_rom_client_monitors_config(qdev);
Gerd Hoffmann90621552017-03-01 11:12:32 +0100165 if (status != MONITORS_CONFIG_BAD_CRC)
166 break;
167 udelay(5);
168 }
169 if (status == MONITORS_CONFIG_BAD_CRC) {
170 qxl_io_log(qdev, "config: bad crc\n");
171 DRM_DEBUG_KMS("ignoring client monitors config: bad crc");
172 return;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100173 }
174 if (status == MONITORS_CONFIG_UNCHANGED) {
Gerd Hoffmann90621552017-03-01 11:12:32 +0100175 qxl_io_log(qdev, "config: unchanged\n");
176 DRM_DEBUG_KMS("ignoring client monitors config: unchanged");
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100177 return;
Dave Airlief64122c2013-02-25 14:47:55 +1000178 }
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200179
Dave Airlie7dea0942014-10-28 11:28:44 +1000180 drm_modeset_lock_all(dev);
181 qxl_update_offset_props(qdev);
182 drm_modeset_unlock_all(dev);
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200183 if (!drm_helper_hpd_irq_event(dev)) {
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200184 /* notify that the monitor configuration changed, to
185 adjust at the arbitrary resolution */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200186 drm_kms_helper_hotplug_event(dev);
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200187 }
Dave Airlief64122c2013-02-25 14:47:55 +1000188}
189
Marc-André Lureaub0807422013-10-18 16:11:31 +0200190static int qxl_add_monitors_config_modes(struct drm_connector *connector,
191 unsigned *pwidth,
192 unsigned *pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000193{
194 struct drm_device *dev = connector->dev;
195 struct qxl_device *qdev = dev->dev_private;
196 struct qxl_output *output = drm_connector_to_qxl_output(connector);
197 int h = output->index;
198 struct drm_display_mode *mode = NULL;
199 struct qxl_head *head;
200
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100201 if (!qdev->client_monitors_config)
Dave Airlief64122c2013-02-25 14:47:55 +1000202 return 0;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100203 head = &qdev->client_monitors_config->heads[h];
Dave Airlief64122c2013-02-25 14:47:55 +1000204
205 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
206 false);
207 mode->type |= DRM_MODE_TYPE_PREFERRED;
Christophe Fergeauff996e72016-11-08 10:12:09 +0100208 mode->hdisplay = head->width;
209 mode->vdisplay = head->height;
210 drm_mode_set_name(mode);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200211 *pwidth = head->width;
212 *pheight = head->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000213 drm_mode_probed_add(connector, mode);
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500214 /* remember the last custom size for mode validation */
215 qdev->monitors_config_width = mode->hdisplay;
216 qdev->monitors_config_height = mode->vdisplay;
Dave Airlief64122c2013-02-25 14:47:55 +1000217 return 1;
218}
219
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500220static struct mode_size {
221 int w;
222 int h;
223} common_modes[] = {
224 { 640, 480},
225 { 720, 480},
226 { 800, 600},
227 { 848, 480},
228 {1024, 768},
229 {1152, 768},
230 {1280, 720},
231 {1280, 800},
232 {1280, 854},
233 {1280, 960},
234 {1280, 1024},
235 {1440, 900},
236 {1400, 1050},
237 {1680, 1050},
238 {1600, 1200},
239 {1920, 1080},
240 {1920, 1200}
241};
242
Marc-André Lureaub0807422013-10-18 16:11:31 +0200243static int qxl_add_common_modes(struct drm_connector *connector,
244 unsigned pwidth,
245 unsigned pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000246{
247 struct drm_device *dev = connector->dev;
248 struct drm_display_mode *mode = NULL;
249 int i;
Dave Airlief64122c2013-02-25 14:47:55 +1000250 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
Dave Airlief64122c2013-02-25 14:47:55 +1000251 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
252 60, false, false, false);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200253 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000254 mode->type |= DRM_MODE_TYPE_PREFERRED;
255 drm_mode_probed_add(connector, mode);
256 }
257 return i - 1;
258}
259
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300260static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
261 struct drm_crtc_state *old_crtc_state)
262{
263 struct drm_device *dev = crtc->dev;
264 struct drm_pending_vblank_event *event;
265 unsigned long flags;
266
267 if (crtc->state && crtc->state->event) {
268 event = crtc->state->event;
269 crtc->state->event = NULL;
270
271 spin_lock_irqsave(&dev->event_lock, flags);
272 drm_crtc_send_vblank_event(crtc, event);
273 spin_unlock_irqrestore(&dev->event_lock, flags);
274 }
275}
276
Dave Airlief64122c2013-02-25 14:47:55 +1000277static void qxl_crtc_destroy(struct drm_crtc *crtc)
278{
279 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
280
281 drm_crtc_cleanup(crtc);
282 kfree(qxl_crtc);
283}
284
Dave Airlief64122c2013-02-25 14:47:55 +1000285static const struct drm_crtc_funcs qxl_crtc_funcs = {
Gabriel Krisman Bertazibc8a00d2017-02-27 17:43:26 -0300286 .set_config = drm_atomic_helper_set_config,
Dave Airlief64122c2013-02-25 14:47:55 +1000287 .destroy = qxl_crtc_destroy,
Gabriel Krisman Bertazi9973c872017-02-27 17:43:27 -0300288 .page_flip = drm_atomic_helper_page_flip,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300289 .reset = drm_atomic_helper_crtc_reset,
290 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
291 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Dave Airlief64122c2013-02-25 14:47:55 +1000292};
293
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200294void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
Dave Airlief64122c2013-02-25 14:47:55 +1000295{
296 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
297
Markus Elfringdc3583c2016-07-22 14:14:54 +0200298 drm_gem_object_unreference_unlocked(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000299 drm_framebuffer_cleanup(fb);
300 kfree(qxl_fb);
301}
302
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000303static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
304 struct drm_file *file_priv,
305 unsigned flags, unsigned color,
306 struct drm_clip_rect *clips,
307 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000308{
309 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
310 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
311 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
312 struct drm_clip_rect norect;
313 struct qxl_bo *qobj;
314 int inc = 1;
315
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200316 drm_modeset_lock_all(fb->dev);
317
Dave Airlief64122c2013-02-25 14:47:55 +1000318 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000319 /* if we aren't primary surface ignore this */
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200320 if (!qobj->is_primary) {
321 drm_modeset_unlock_all(fb->dev);
Dave Airlieb2b44652013-05-13 12:48:40 +1000322 return 0;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200323 }
Dave Airlieb2b44652013-05-13 12:48:40 +1000324
Dave Airlief64122c2013-02-25 14:47:55 +1000325 if (!num_clips) {
326 num_clips = 1;
327 clips = &norect;
328 norect.x1 = norect.y1 = 0;
329 norect.x2 = fb->width;
330 norect.y2 = fb->height;
331 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
332 num_clips /= 2;
333 inc = 2; /* skip source rects */
334 }
335
336 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
337 clips, num_clips, inc);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200338
339 drm_modeset_unlock_all(fb->dev);
340
Dave Airlief64122c2013-02-25 14:47:55 +1000341 return 0;
342}
343
344static const struct drm_framebuffer_funcs qxl_fb_funcs = {
345 .destroy = qxl_user_framebuffer_destroy,
346 .dirty = qxl_framebuffer_surface_dirty,
347/* TODO?
348 * .create_handle = qxl_user_framebuffer_create_handle, */
349};
350
351int
352qxl_framebuffer_init(struct drm_device *dev,
353 struct qxl_framebuffer *qfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200354 const struct drm_mode_fb_cmd2 *mode_cmd,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200355 struct drm_gem_object *obj,
356 const struct drm_framebuffer_funcs *funcs)
Dave Airlief64122c2013-02-25 14:47:55 +1000357{
358 int ret;
359
360 qfb->obj = obj;
Ville Syrjälä53609432016-11-18 21:52:51 +0200361 drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200362 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000363 if (ret) {
364 qfb->obj = NULL;
365 return ret;
366 }
Dave Airlief64122c2013-02-25 14:47:55 +1000367 return 0;
368}
369
370static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
371{
372}
373
374static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
375 const struct drm_display_mode *mode,
376 struct drm_display_mode *adjusted_mode)
377{
378 struct drm_device *dev = crtc->dev;
379 struct qxl_device *qdev = dev->dev_private;
380
381 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
382 __func__,
383 mode->hdisplay, mode->vdisplay,
384 adjusted_mode->hdisplay,
385 adjusted_mode->vdisplay);
386 return true;
387}
388
Christophe Fergeaue4a76442016-11-08 10:12:03 +0100389static void
Dave Airlief64122c2013-02-25 14:47:55 +1000390qxl_send_monitors_config(struct qxl_device *qdev)
391{
392 int i;
393
394 BUG_ON(!qdev->ram_header->monitors_config);
395
396 if (qdev->monitors_config->count == 0) {
397 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
398 return;
399 }
400 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
401 struct qxl_head *head = &qdev->monitors_config->heads[i];
402
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100403 if (head->y > 8192 || head->x > 8192 ||
Dave Airlief64122c2013-02-25 14:47:55 +1000404 head->width > 8192 || head->height > 8192) {
405 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
406 i, head->width, head->height,
407 head->x, head->y);
408 return;
409 }
410 }
411 qxl_io_monitors_config(qdev);
412}
413
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100414static void qxl_monitors_config_set(struct qxl_device *qdev,
415 int index,
416 unsigned x, unsigned y,
417 unsigned width, unsigned height,
418 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000419{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100420 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
421 qdev->monitors_config->heads[index].x = x;
422 qdev->monitors_config->heads[index].y = y;
423 qdev->monitors_config->heads[index].width = width;
424 qdev->monitors_config->heads[index].height = height;
425 qdev->monitors_config->heads[index].surface_id = surf_id;
426
Dave Airlief64122c2013-02-25 14:47:55 +1000427}
428
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300429void qxl_mode_set_nofb(struct drm_crtc *crtc)
430{
431 struct qxl_device *qdev = crtc->dev->dev_private;
432 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
433 struct drm_display_mode *mode = &crtc->mode;
434
435 DRM_DEBUG("Mode set (%d,%d)\n",
436 mode->hdisplay, mode->vdisplay);
437
438 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
439 mode->hdisplay, mode->vdisplay, 0);
440
441}
442
Dave Airlief64122c2013-02-25 14:47:55 +1000443static void qxl_crtc_commit(struct drm_crtc *crtc)
444{
445 DRM_DEBUG("\n");
446}
447
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100448static void qxl_crtc_disable(struct drm_crtc *crtc)
449{
450 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Gabriel Krisman Bertazi37235452017-02-27 17:43:22 -0300451 struct qxl_device *qdev = crtc->dev->dev_private;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100452
453 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
454
455 qxl_send_monitors_config(qdev);
456}
457
Dave Airlief64122c2013-02-25 14:47:55 +1000458static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
459 .dpms = qxl_crtc_dpms,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100460 .disable = qxl_crtc_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000461 .mode_fixup = qxl_crtc_mode_fixup,
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300462 .mode_set_nofb = qxl_mode_set_nofb,
Dave Airlief64122c2013-02-25 14:47:55 +1000463 .commit = qxl_crtc_commit,
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300464 .atomic_flush = qxl_crtc_atomic_flush,
Dave Airlief64122c2013-02-25 14:47:55 +1000465};
466
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300467int qxl_primary_atomic_check(struct drm_plane *plane,
468 struct drm_plane_state *state)
469{
470 struct qxl_device *qdev = plane->dev->dev_private;
471 struct qxl_framebuffer *qfb;
472 struct qxl_bo *bo;
473
474 if (!state->crtc || !state->fb)
475 return 0;
476
477 qfb = to_qxl_framebuffer(state->fb);
478 bo = gem_to_qxl_bo(qfb->obj);
479
480 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
481 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
482 return -EINVAL;
483 }
484
485 return 0;
486}
487
488static void qxl_primary_atomic_update(struct drm_plane *plane,
489 struct drm_plane_state *old_state)
490{
491 struct qxl_device *qdev = plane->dev->dev_private;
492 struct qxl_framebuffer *qfb =
493 to_qxl_framebuffer(plane->state->fb);
494 struct qxl_framebuffer *qfb_old;
495 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
496 struct qxl_bo *bo_old;
497 struct drm_clip_rect norect = {
498 .x1 = 0,
499 .y1 = 0,
500 .x2 = qfb->base.width,
501 .y2 = qfb->base.height
502 };
503
504 if (!old_state->fb) {
505 qxl_io_log(qdev,
506 "create primary fb: %dx%d,%d,%d\n",
507 bo->surf.width, bo->surf.height,
508 bo->surf.stride, bo->surf.format);
509
510 qxl_io_create_primary(qdev, 0, bo);
511 bo->is_primary = true;
512 return;
513
514 } else {
515 qfb_old = to_qxl_framebuffer(old_state->fb);
516 bo_old = gem_to_qxl_bo(qfb_old->obj);
517 bo_old->is_primary = false;
518 }
519
520 bo->is_primary = true;
521 qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
522}
523
524static void qxl_primary_atomic_disable(struct drm_plane *plane,
525 struct drm_plane_state *old_state)
526{
527 struct qxl_device *qdev = plane->dev->dev_private;
528
529 if (old_state->fb)
530 { struct qxl_framebuffer *qfb =
531 to_qxl_framebuffer(old_state->fb);
532 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
533
534 qxl_io_destroy_primary(qdev);
535 bo->is_primary = false;
536 }
537}
538
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300539int qxl_plane_atomic_check(struct drm_plane *plane,
540 struct drm_plane_state *state)
541{
542 return 0;
543}
544
545static void qxl_cursor_atomic_update(struct drm_plane *plane,
546 struct drm_plane_state *old_state)
547{
548 struct drm_device *dev = plane->dev;
549 struct qxl_device *qdev = dev->dev_private;
550 struct drm_framebuffer *fb = plane->state->fb;
551 struct qxl_release *release;
552 struct qxl_cursor_cmd *cmd;
553 struct qxl_cursor *cursor;
554 struct drm_gem_object *obj;
555 struct qxl_bo *cursor_bo, *user_bo = NULL;
556 int ret;
557 void *user_ptr;
558 int size = 64*64*4;
559
560 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
561 QXL_RELEASE_CURSOR_CMD,
562 &release, NULL);
563
564 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
565
566 if (fb != old_state->fb) {
567 obj = to_qxl_framebuffer(fb)->obj;
568 user_bo = gem_to_qxl_bo(obj);
569
570 /* pinning is done in the prepare/cleanup framevbuffer */
571 ret = qxl_bo_kmap(user_bo, &user_ptr);
572 if (ret)
573 goto out_free_release;
574
575 ret = qxl_alloc_bo_reserved(qdev, release,
576 sizeof(struct qxl_cursor) + size,
577 &cursor_bo);
578 if (ret)
579 goto out_kunmap;
580
581 ret = qxl_release_reserve_list(release, true);
582 if (ret)
583 goto out_free_bo;
584
585 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
586 if (ret)
587 goto out_backoff;
588
589 cursor->header.unique = 0;
590 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
591 cursor->header.width = 64;
592 cursor->header.height = 64;
593 cursor->header.hot_spot_x = fb->hot_x;
594 cursor->header.hot_spot_y = fb->hot_y;
595 cursor->data_size = size;
596 cursor->chunk.next_chunk = 0;
597 cursor->chunk.prev_chunk = 0;
598 cursor->chunk.data_size = size;
599 memcpy(cursor->chunk.data, user_ptr, size);
600 qxl_bo_kunmap(cursor_bo);
601 qxl_bo_kunmap(user_bo);
602
603 cmd->u.set.visible = 1;
604 cmd->u.set.shape = qxl_bo_physical_address(qdev,
605 cursor_bo, 0);
606 cmd->type = QXL_CURSOR_SET;
607 } else {
608
609 ret = qxl_release_reserve_list(release, true);
610 if (ret)
611 goto out_free_release;
612
613 cmd->type = QXL_CURSOR_MOVE;
614 }
615
616 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
617 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
618
619 qxl_release_unmap(qdev, release, &cmd->release_info);
620 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
621 qxl_release_fence_buffer_objects(release);
622
623 return;
624
625out_backoff:
626 qxl_release_backoff_reserve_list(release);
627out_free_bo:
628 qxl_bo_unref(&cursor_bo);
629out_kunmap:
630 qxl_bo_kunmap(user_bo);
631out_free_release:
632 qxl_release_free(qdev, release);
633 return;
634
635}
636
637void qxl_cursor_atomic_disable(struct drm_plane *plane,
638 struct drm_plane_state *old_state)
639{
640 struct qxl_device *qdev = plane->dev->dev_private;
641 struct qxl_release *release;
642 struct qxl_cursor_cmd *cmd;
643 int ret;
644
645 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
646 QXL_RELEASE_CURSOR_CMD,
647 &release, NULL);
648 if (ret)
649 return;
650
651 ret = qxl_release_reserve_list(release, true);
652 if (ret) {
653 qxl_release_free(qdev, release);
654 return;
655 }
656
657 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
658 cmd->type = QXL_CURSOR_HIDE;
659 qxl_release_unmap(qdev, release, &cmd->release_info);
660
661 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
662 qxl_release_fence_buffer_objects(release);
663}
664
665int qxl_plane_prepare_fb(struct drm_plane *plane,
666 struct drm_plane_state *new_state)
667{
668 struct drm_gem_object *obj;
669 struct qxl_bo *user_bo;
670 int ret;
671
672 if (!new_state->fb)
673 return 0;
674
675 obj = to_qxl_framebuffer(new_state->fb)->obj;
676 user_bo = gem_to_qxl_bo(obj);
677
678 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
679 if (ret)
680 return ret;
681
682 return 0;
683}
684
685static void qxl_plane_cleanup_fb(struct drm_plane *plane,
686 struct drm_plane_state *old_state)
687{
688 struct drm_gem_object *obj;
689 struct qxl_bo *user_bo;
690
691 if (!plane->state->fb) {
692 /* we never executed prepare_fb, so there's nothing to
693 * unpin.
694 */
695 return;
696 }
697
698 obj = to_qxl_framebuffer(plane->state->fb)->obj;
699 user_bo = gem_to_qxl_bo(obj);
700 qxl_bo_unpin(user_bo);
701}
702
703static const uint32_t qxl_cursor_plane_formats[] = {
704 DRM_FORMAT_ARGB8888,
705};
706
707static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
708 .atomic_check = qxl_plane_atomic_check,
709 .atomic_update = qxl_cursor_atomic_update,
710 .atomic_disable = qxl_cursor_atomic_disable,
711 .prepare_fb = qxl_plane_prepare_fb,
712 .cleanup_fb = qxl_plane_cleanup_fb,
713};
714
715static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -0300716 .update_plane = drm_atomic_helper_update_plane,
717 .disable_plane = drm_atomic_helper_disable_plane,
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300718 .destroy = drm_primary_helper_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300719 .reset = drm_atomic_helper_plane_reset,
720 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
721 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300722};
723
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300724static const uint32_t qxl_primary_plane_formats[] = {
725 DRM_FORMAT_XRGB8888,
726 DRM_FORMAT_ARGB8888,
727};
728
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300729static const struct drm_plane_helper_funcs primary_helper_funcs = {
730 .atomic_check = qxl_primary_atomic_check,
731 .atomic_update = qxl_primary_atomic_update,
732 .atomic_disable = qxl_primary_atomic_disable,
733 .prepare_fb = qxl_plane_prepare_fb,
734 .cleanup_fb = qxl_plane_cleanup_fb,
735};
736
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300737static const struct drm_plane_funcs qxl_primary_plane_funcs = {
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -0300738 .update_plane = drm_atomic_helper_update_plane,
739 .disable_plane = drm_atomic_helper_disable_plane,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300740 .destroy = drm_primary_helper_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300741 .reset = drm_atomic_helper_plane_reset,
742 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
743 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300744};
745
746static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
747 unsigned int possible_crtcs,
748 enum drm_plane_type type)
749{
750 const struct drm_plane_helper_funcs *helper_funcs = NULL;
751 struct drm_plane *plane;
752 const struct drm_plane_funcs *funcs;
753 const uint32_t *formats;
754 int num_formats;
755 int err;
756
757 if (type == DRM_PLANE_TYPE_PRIMARY) {
758 funcs = &qxl_primary_plane_funcs;
759 formats = qxl_primary_plane_formats;
760 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300761 helper_funcs = &primary_helper_funcs;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300762 } else if (type == DRM_PLANE_TYPE_CURSOR) {
763 funcs = &qxl_cursor_plane_funcs;
764 formats = qxl_cursor_plane_formats;
765 helper_funcs = &qxl_cursor_helper_funcs;
766 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300767 } else {
768 return ERR_PTR(-EINVAL);
769 }
770
771 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
772 if (!plane)
773 return ERR_PTR(-ENOMEM);
774
775 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
776 funcs, formats, num_formats,
777 type, NULL);
778 if (err)
779 goto free_plane;
780
781 drm_plane_helper_add(plane, helper_funcs);
782
783 return plane;
784
785free_plane:
786 kfree(plane);
787 return ERR_PTR(-EINVAL);
788}
789
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100790static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000791{
792 struct qxl_crtc *qxl_crtc;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300793 struct drm_plane *primary, *cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300794 struct qxl_device *qdev = dev->dev_private;
795 int r;
Dave Airlief64122c2013-02-25 14:47:55 +1000796
797 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
798 if (!qxl_crtc)
799 return -ENOMEM;
800
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300801 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
802 if (IS_ERR(primary)) {
803 r = -ENOMEM;
804 goto free_mem;
805 }
806
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300807 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
808 if (IS_ERR(cursor)) {
809 r = -ENOMEM;
810 goto clean_primary;
811 }
812
813 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300814 &qxl_crtc_funcs, NULL);
815 if (r)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300816 goto clean_cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300817
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100818 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000819 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
820 return 0;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300821
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300822clean_cursor:
823 drm_plane_cleanup(cursor);
824 kfree(cursor);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300825clean_primary:
826 drm_plane_cleanup(primary);
827 kfree(primary);
828free_mem:
829 kfree(qxl_crtc);
830 return r;
Dave Airlief64122c2013-02-25 14:47:55 +1000831}
832
833static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
834{
835 DRM_DEBUG("\n");
836}
837
Dave Airlief64122c2013-02-25 14:47:55 +1000838static void qxl_enc_prepare(struct drm_encoder *encoder)
839{
840 DRM_DEBUG("\n");
841}
842
843static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
844 struct drm_encoder *encoder)
845{
846 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100847 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000848 struct qxl_head *head;
849 struct drm_display_mode *mode;
850
851 BUG_ON(!encoder);
852 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100853 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000854 if (!qdev->monitors_config ||
855 qdev->monitors_config->max_allowed <= i) {
856 DRM_ERROR(
857 "head number too large or missing monitors config: %p, %d",
858 qdev->monitors_config,
859 qdev->monitors_config ?
860 qdev->monitors_config->max_allowed : -1);
861 return;
862 }
863 if (!encoder->crtc) {
864 DRM_ERROR("missing crtc on encoder %p\n", encoder);
865 return;
866 }
867 if (i != 0)
868 DRM_DEBUG("missing for multiple monitors: no head holes\n");
869 head = &qdev->monitors_config->heads[i];
870 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000871 if (encoder->crtc->enabled) {
872 mode = &encoder->crtc->mode;
873 head->width = mode->hdisplay;
874 head->height = mode->vdisplay;
875 head->x = encoder->crtc->x;
876 head->y = encoder->crtc->y;
877 if (qdev->monitors_config->count < i + 1)
878 qdev->monitors_config->count = i + 1;
879 } else {
880 head->width = 0;
881 head->height = 0;
882 head->x = 0;
883 head->y = 0;
884 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100885 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
886 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000887 head->flags = 0;
888 /* TODO - somewhere else to call this for multiple monitors
889 * (config_commit?) */
890 qxl_send_monitors_config(qdev);
891}
892
893static void qxl_enc_commit(struct drm_encoder *encoder)
894{
895 struct qxl_device *qdev = encoder->dev->dev_private;
896
897 qxl_write_monitors_config_for_encoder(qdev, encoder);
898 DRM_DEBUG("\n");
899}
900
901static void qxl_enc_mode_set(struct drm_encoder *encoder,
902 struct drm_display_mode *mode,
903 struct drm_display_mode *adjusted_mode)
904{
905 DRM_DEBUG("\n");
906}
907
908static int qxl_conn_get_modes(struct drm_connector *connector)
909{
910 int ret = 0;
911 struct qxl_device *qdev = connector->dev->dev_private;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200912 unsigned pwidth = 1024;
913 unsigned pheight = 768;
Dave Airlief64122c2013-02-25 14:47:55 +1000914
915 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
916 /* TODO: what should we do here? only show the configured modes for the
917 * device, or allow the full list, or both? */
918 if (qdev->monitors_config && qdev->monitors_config->count) {
Marc-André Lureaub0807422013-10-18 16:11:31 +0200919 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000920 if (ret < 0)
921 return ret;
922 }
Marc-André Lureaub0807422013-10-18 16:11:31 +0200923 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000924 return ret;
925}
926
927static int qxl_conn_mode_valid(struct drm_connector *connector,
928 struct drm_display_mode *mode)
929{
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500930 struct drm_device *ddev = connector->dev;
931 struct qxl_device *qdev = ddev->dev_private;
932 int i;
933
Dave Airlief64122c2013-02-25 14:47:55 +1000934 /* TODO: is this called for user defined modes? (xrandr --add-mode)
935 * TODO: check that the mode fits in the framebuffer */
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500936
937 if(qdev->monitors_config_width == mode->hdisplay &&
938 qdev->monitors_config_height == mode->vdisplay)
939 return MODE_OK;
940
941 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
942 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
943 return MODE_OK;
944 }
945 return MODE_BAD;
Dave Airlief64122c2013-02-25 14:47:55 +1000946}
947
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000948static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +1000949{
950 struct qxl_output *qxl_output =
951 drm_connector_to_qxl_output(connector);
952
953 DRM_DEBUG("\n");
954 return &qxl_output->enc;
955}
956
957
958static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
959 .dpms = qxl_enc_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +1000960 .prepare = qxl_enc_prepare,
961 .mode_set = qxl_enc_mode_set,
962 .commit = qxl_enc_commit,
963};
964
965static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
966 .get_modes = qxl_conn_get_modes,
967 .mode_valid = qxl_conn_mode_valid,
968 .best_encoder = qxl_best_encoder,
969};
970
Dave Airlief64122c2013-02-25 14:47:55 +1000971static enum drm_connector_status qxl_conn_detect(
972 struct drm_connector *connector,
973 bool force)
974{
975 struct qxl_output *output =
976 drm_connector_to_qxl_output(connector);
977 struct drm_device *ddev = connector->dev;
978 struct qxl_device *qdev = ddev->dev_private;
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000979 bool connected = false;
Dave Airlief64122c2013-02-25 14:47:55 +1000980
981 /* The first monitor is always connected */
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000982 if (!qdev->client_monitors_config) {
983 if (output->index == 0)
984 connected = true;
985 } else
986 connected = qdev->client_monitors_config->count > output->index &&
987 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
Dave Airlief64122c2013-02-25 14:47:55 +1000988
Marc-André Lureau5cab51c2013-10-18 16:11:33 +0200989 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
990 if (!connected)
991 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
992
Dave Airlief64122c2013-02-25 14:47:55 +1000993 return connected ? connector_status_connected
994 : connector_status_disconnected;
995}
996
997static int qxl_conn_set_property(struct drm_connector *connector,
998 struct drm_property *property,
999 uint64_t value)
1000{
1001 DRM_DEBUG("\n");
1002 return 0;
1003}
1004
1005static void qxl_conn_destroy(struct drm_connector *connector)
1006{
1007 struct qxl_output *qxl_output =
1008 drm_connector_to_qxl_output(connector);
1009
Thomas Wood34ea3d32014-05-29 16:57:41 +01001010 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001011 drm_connector_cleanup(connector);
1012 kfree(qxl_output);
1013}
1014
1015static const struct drm_connector_funcs qxl_connector_funcs = {
1016 .dpms = drm_helper_connector_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001017 .detect = qxl_conn_detect,
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001018 .fill_modes = drm_helper_probe_single_connector_modes,
Dave Airlief64122c2013-02-25 14:47:55 +10001019 .set_property = qxl_conn_set_property,
1020 .destroy = qxl_conn_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -03001021 .reset = drm_atomic_helper_connector_reset,
1022 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1023 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Dave Airlief64122c2013-02-25 14:47:55 +10001024};
1025
1026static void qxl_enc_destroy(struct drm_encoder *encoder)
1027{
1028 drm_encoder_cleanup(encoder);
1029}
1030
1031static const struct drm_encoder_funcs qxl_enc_funcs = {
1032 .destroy = qxl_enc_destroy,
1033};
1034
Dave Airlie4695b032013-10-11 11:05:00 +10001035static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1036{
1037 if (qdev->hotplug_mode_update_property)
1038 return 0;
1039
1040 qdev->hotplug_mode_update_property =
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001041 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlie4695b032013-10-11 11:05:00 +10001042 "hotplug_mode_update", 0, 1);
1043
1044 return 0;
1045}
1046
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001047static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +10001048{
Dave Airlie4695b032013-10-11 11:05:00 +10001049 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +10001050 struct qxl_output *qxl_output;
1051 struct drm_connector *connector;
1052 struct drm_encoder *encoder;
1053
1054 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1055 if (!qxl_output)
1056 return -ENOMEM;
1057
1058 qxl_output->index = num_output;
1059
1060 connector = &qxl_output->base;
1061 encoder = &qxl_output->enc;
1062 drm_connector_init(dev, &qxl_output->base,
1063 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1064
1065 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001066 DRM_MODE_ENCODER_VIRTUAL, NULL);
Dave Airlief64122c2013-02-25 14:47:55 +10001067
Dave Airlie5ff91e42013-07-05 10:20:33 +10001068 /* we get HPD via client monitors config */
1069 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +10001070 encoder->possible_crtcs = 1 << num_output;
1071 drm_mode_connector_attach_encoder(&qxl_output->base,
1072 &qxl_output->enc);
1073 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1074 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1075
Dave Airlie4695b032013-10-11 11:05:00 +10001076 drm_object_attach_property(&connector->base,
1077 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +10001078 drm_object_attach_property(&connector->base,
1079 dev->mode_config.suggested_x_property, 0);
1080 drm_object_attach_property(&connector->base,
1081 dev->mode_config.suggested_y_property, 0);
Dave Airlief64122c2013-02-25 14:47:55 +10001082 return 0;
1083}
1084
1085static struct drm_framebuffer *
1086qxl_user_framebuffer_create(struct drm_device *dev,
1087 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02001088 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief64122c2013-02-25 14:47:55 +10001089{
1090 struct drm_gem_object *obj;
1091 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +10001092 int ret;
1093
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001094 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1095 if (!obj)
1096 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +10001097
1098 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1099 if (qxl_fb == NULL)
1100 return NULL;
1101
Noralf Trønnes6819c3c2016-04-28 17:18:36 +02001102 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +10001103 if (ret) {
1104 kfree(qxl_fb);
1105 drm_gem_object_unreference_unlocked(obj);
1106 return NULL;
1107 }
1108
Dave Airlief64122c2013-02-25 14:47:55 +10001109 return &qxl_fb->base;
1110}
1111
1112static const struct drm_mode_config_funcs qxl_mode_funcs = {
1113 .fb_create = qxl_user_framebuffer_create,
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -03001114 .atomic_check = drm_atomic_helper_check,
1115 .atomic_commit = drm_atomic_helper_commit,
Dave Airlief64122c2013-02-25 14:47:55 +10001116};
1117
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001118int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001119{
Dave Airlief64122c2013-02-25 14:47:55 +10001120 int ret;
1121 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001122 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001123 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001124 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001125
Dave Airlief64122c2013-02-25 14:47:55 +10001126 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1127 QXL_GEM_DOMAIN_VRAM,
1128 false, false, NULL, &gobj);
1129 if (ret) {
1130 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1131 return -ENOMEM;
1132 }
1133 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001134
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001135 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001136 if (ret)
1137 return ret;
1138
Dave Airlief64122c2013-02-25 14:47:55 +10001139 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001140
Dave Airlief64122c2013-02-25 14:47:55 +10001141 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1142 qdev->ram_header->monitors_config =
1143 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1144
1145 memset(qdev->monitors_config, 0, monitors_config_size);
1146 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001147 return 0;
1148}
1149
1150int qxl_destroy_monitors_object(struct qxl_device *qdev)
1151{
1152 int ret;
1153
1154 qdev->monitors_config = NULL;
1155 qdev->ram_header->monitors_config = 0;
1156
1157 qxl_bo_kunmap(qdev->monitors_config_bo);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001158 ret = qxl_bo_unpin(qdev->monitors_config_bo);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001159 if (ret)
1160 return ret;
1161
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001162 qxl_bo_unref(&qdev->monitors_config_bo);
1163 return 0;
1164}
1165
1166int qxl_modeset_init(struct qxl_device *qdev)
1167{
1168 int i;
1169 int ret;
1170
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001171 drm_mode_config_init(&qdev->ddev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001172
1173 ret = qxl_create_monitors_object(qdev);
1174 if (ret)
1175 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001176
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001177 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
Dave Airlief64122c2013-02-25 14:47:55 +10001178
1179 /* modes will be validated against the framebuffer size */
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -03001180 qdev->ddev.mode_config.min_width = 0;
1181 qdev->ddev.mode_config.min_height = 0;
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001182 qdev->ddev.mode_config.max_width = 8192;
1183 qdev->ddev.mode_config.max_height = 8192;
Dave Airlief64122c2013-02-25 14:47:55 +10001184
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001185 qdev->ddev.mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001186
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001187 drm_mode_create_suggested_offset_properties(&qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001188 qxl_mode_create_hotplug_mode_update_property(qdev);
1189
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001190 for (i = 0 ; i < qxl_num_crtc; ++i) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001191 qdev_crtc_init(&qdev->ddev, i);
1192 qdev_output_init(&qdev->ddev, i);
Dave Airlief64122c2013-02-25 14:47:55 +10001193 }
1194
1195 qdev->mode_info.mode_config_initialized = true;
1196
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -03001197 drm_mode_config_reset(&qdev->ddev);
1198
Dave Airlief64122c2013-02-25 14:47:55 +10001199 /* primary surface must be created by this point, to allow
1200 * issuing command queue commands and having them read by
1201 * spice server. */
1202 qxl_fbdev_init(qdev);
1203 return 0;
1204}
1205
1206void qxl_modeset_fini(struct qxl_device *qdev)
1207{
1208 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001209
1210 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001211 if (qdev->mode_info.mode_config_initialized) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001212 drm_mode_config_cleanup(&qdev->ddev);
Dave Airlief64122c2013-02-25 14:47:55 +10001213 qdev->mode_info.mode_config_initialized = false;
1214 }
1215}