blob: 2cd14bebc49ca060fe35457d5bf3d591ae8bfad5 [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{
160
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200161 struct drm_device *dev = &qdev->ddev;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100162 int status;
163
164 status = qxl_display_copy_rom_client_monitors_config(qdev);
165 while (status == MONITORS_CONFIG_BAD_CRC) {
Dave Airlief64122c2013-02-25 14:47:55 +1000166 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
167 " retrying\n");
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100168 status = qxl_display_copy_rom_client_monitors_config(qdev);
169 }
170 if (status == MONITORS_CONFIG_UNCHANGED) {
171 qxl_io_log(qdev, "config unchanged\n");
172 DRM_DEBUG("ignoring unchanged client monitors config");
173 return;
Dave Airlief64122c2013-02-25 14:47:55 +1000174 }
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200175
Dave Airlie7dea0942014-10-28 11:28:44 +1000176 drm_modeset_lock_all(dev);
177 qxl_update_offset_props(qdev);
178 drm_modeset_unlock_all(dev);
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200179 if (!drm_helper_hpd_irq_event(dev)) {
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200180 /* notify that the monitor configuration changed, to
181 adjust at the arbitrary resolution */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200182 drm_kms_helper_hotplug_event(dev);
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200183 }
Dave Airlief64122c2013-02-25 14:47:55 +1000184}
185
Marc-André Lureaub0807422013-10-18 16:11:31 +0200186static int qxl_add_monitors_config_modes(struct drm_connector *connector,
187 unsigned *pwidth,
188 unsigned *pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000189{
190 struct drm_device *dev = connector->dev;
191 struct qxl_device *qdev = dev->dev_private;
192 struct qxl_output *output = drm_connector_to_qxl_output(connector);
193 int h = output->index;
194 struct drm_display_mode *mode = NULL;
195 struct qxl_head *head;
196
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100197 if (!qdev->client_monitors_config)
Dave Airlief64122c2013-02-25 14:47:55 +1000198 return 0;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100199 head = &qdev->client_monitors_config->heads[h];
Dave Airlief64122c2013-02-25 14:47:55 +1000200
201 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
202 false);
203 mode->type |= DRM_MODE_TYPE_PREFERRED;
Christophe Fergeauff996e72016-11-08 10:12:09 +0100204 mode->hdisplay = head->width;
205 mode->vdisplay = head->height;
206 drm_mode_set_name(mode);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200207 *pwidth = head->width;
208 *pheight = head->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000209 drm_mode_probed_add(connector, mode);
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500210 /* remember the last custom size for mode validation */
211 qdev->monitors_config_width = mode->hdisplay;
212 qdev->monitors_config_height = mode->vdisplay;
Dave Airlief64122c2013-02-25 14:47:55 +1000213 return 1;
214}
215
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500216static struct mode_size {
217 int w;
218 int h;
219} common_modes[] = {
220 { 640, 480},
221 { 720, 480},
222 { 800, 600},
223 { 848, 480},
224 {1024, 768},
225 {1152, 768},
226 {1280, 720},
227 {1280, 800},
228 {1280, 854},
229 {1280, 960},
230 {1280, 1024},
231 {1440, 900},
232 {1400, 1050},
233 {1680, 1050},
234 {1600, 1200},
235 {1920, 1080},
236 {1920, 1200}
237};
238
Marc-André Lureaub0807422013-10-18 16:11:31 +0200239static int qxl_add_common_modes(struct drm_connector *connector,
240 unsigned pwidth,
241 unsigned pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000242{
243 struct drm_device *dev = connector->dev;
244 struct drm_display_mode *mode = NULL;
245 int i;
Dave Airlief64122c2013-02-25 14:47:55 +1000246 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
Dave Airlief64122c2013-02-25 14:47:55 +1000247 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
248 60, false, false, false);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200249 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000250 mode->type |= DRM_MODE_TYPE_PREFERRED;
251 drm_mode_probed_add(connector, mode);
252 }
253 return i - 1;
254}
255
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300256static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
257 struct drm_crtc_state *old_crtc_state)
258{
259 struct drm_device *dev = crtc->dev;
260 struct drm_pending_vblank_event *event;
261 unsigned long flags;
262
263 if (crtc->state && crtc->state->event) {
264 event = crtc->state->event;
265 crtc->state->event = NULL;
266
267 spin_lock_irqsave(&dev->event_lock, flags);
268 drm_crtc_send_vblank_event(crtc, event);
269 spin_unlock_irqrestore(&dev->event_lock, flags);
270 }
271}
272
Dave Airlief64122c2013-02-25 14:47:55 +1000273static void qxl_crtc_destroy(struct drm_crtc *crtc)
274{
275 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
276
277 drm_crtc_cleanup(crtc);
278 kfree(qxl_crtc);
279}
280
Dave Airlief64122c2013-02-25 14:47:55 +1000281static const struct drm_crtc_funcs qxl_crtc_funcs = {
Gabriel Krisman Bertazibc8a00d2017-02-27 17:43:26 -0300282 .set_config = drm_atomic_helper_set_config,
Dave Airlief64122c2013-02-25 14:47:55 +1000283 .destroy = qxl_crtc_destroy,
Gabriel Krisman Bertazi9973c872017-02-27 17:43:27 -0300284 .page_flip = drm_atomic_helper_page_flip,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300285 .reset = drm_atomic_helper_crtc_reset,
286 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
287 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Dave Airlief64122c2013-02-25 14:47:55 +1000288};
289
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200290void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
Dave Airlief64122c2013-02-25 14:47:55 +1000291{
292 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
293
Markus Elfringdc3583c2016-07-22 14:14:54 +0200294 drm_gem_object_unreference_unlocked(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000295 drm_framebuffer_cleanup(fb);
296 kfree(qxl_fb);
297}
298
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000299static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
300 struct drm_file *file_priv,
301 unsigned flags, unsigned color,
302 struct drm_clip_rect *clips,
303 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000304{
305 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
306 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
307 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
308 struct drm_clip_rect norect;
309 struct qxl_bo *qobj;
310 int inc = 1;
311
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200312 drm_modeset_lock_all(fb->dev);
313
Dave Airlief64122c2013-02-25 14:47:55 +1000314 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000315 /* if we aren't primary surface ignore this */
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200316 if (!qobj->is_primary) {
317 drm_modeset_unlock_all(fb->dev);
Dave Airlieb2b44652013-05-13 12:48:40 +1000318 return 0;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200319 }
Dave Airlieb2b44652013-05-13 12:48:40 +1000320
Dave Airlief64122c2013-02-25 14:47:55 +1000321 if (!num_clips) {
322 num_clips = 1;
323 clips = &norect;
324 norect.x1 = norect.y1 = 0;
325 norect.x2 = fb->width;
326 norect.y2 = fb->height;
327 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
328 num_clips /= 2;
329 inc = 2; /* skip source rects */
330 }
331
332 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
333 clips, num_clips, inc);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200334
335 drm_modeset_unlock_all(fb->dev);
336
Dave Airlief64122c2013-02-25 14:47:55 +1000337 return 0;
338}
339
340static const struct drm_framebuffer_funcs qxl_fb_funcs = {
341 .destroy = qxl_user_framebuffer_destroy,
342 .dirty = qxl_framebuffer_surface_dirty,
343/* TODO?
344 * .create_handle = qxl_user_framebuffer_create_handle, */
345};
346
347int
348qxl_framebuffer_init(struct drm_device *dev,
349 struct qxl_framebuffer *qfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200350 const struct drm_mode_fb_cmd2 *mode_cmd,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200351 struct drm_gem_object *obj,
352 const struct drm_framebuffer_funcs *funcs)
Dave Airlief64122c2013-02-25 14:47:55 +1000353{
354 int ret;
355
356 qfb->obj = obj;
Ville Syrjälä53609432016-11-18 21:52:51 +0200357 drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200358 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000359 if (ret) {
360 qfb->obj = NULL;
361 return ret;
362 }
Dave Airlief64122c2013-02-25 14:47:55 +1000363 return 0;
364}
365
366static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
367{
368}
369
370static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
371 const struct drm_display_mode *mode,
372 struct drm_display_mode *adjusted_mode)
373{
374 struct drm_device *dev = crtc->dev;
375 struct qxl_device *qdev = dev->dev_private;
376
377 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
378 __func__,
379 mode->hdisplay, mode->vdisplay,
380 adjusted_mode->hdisplay,
381 adjusted_mode->vdisplay);
382 return true;
383}
384
Christophe Fergeaue4a76442016-11-08 10:12:03 +0100385static void
Dave Airlief64122c2013-02-25 14:47:55 +1000386qxl_send_monitors_config(struct qxl_device *qdev)
387{
388 int i;
389
390 BUG_ON(!qdev->ram_header->monitors_config);
391
392 if (qdev->monitors_config->count == 0) {
393 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
394 return;
395 }
396 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
397 struct qxl_head *head = &qdev->monitors_config->heads[i];
398
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100399 if (head->y > 8192 || head->x > 8192 ||
Dave Airlief64122c2013-02-25 14:47:55 +1000400 head->width > 8192 || head->height > 8192) {
401 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
402 i, head->width, head->height,
403 head->x, head->y);
404 return;
405 }
406 }
407 qxl_io_monitors_config(qdev);
408}
409
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100410static void qxl_monitors_config_set(struct qxl_device *qdev,
411 int index,
412 unsigned x, unsigned y,
413 unsigned width, unsigned height,
414 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000415{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100416 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
417 qdev->monitors_config->heads[index].x = x;
418 qdev->monitors_config->heads[index].y = y;
419 qdev->monitors_config->heads[index].width = width;
420 qdev->monitors_config->heads[index].height = height;
421 qdev->monitors_config->heads[index].surface_id = surf_id;
422
Dave Airlief64122c2013-02-25 14:47:55 +1000423}
424
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300425void qxl_mode_set_nofb(struct drm_crtc *crtc)
426{
427 struct qxl_device *qdev = crtc->dev->dev_private;
428 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
429 struct drm_display_mode *mode = &crtc->mode;
430
431 DRM_DEBUG("Mode set (%d,%d)\n",
432 mode->hdisplay, mode->vdisplay);
433
434 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
435 mode->hdisplay, mode->vdisplay, 0);
436
437}
438
Dave Airlief64122c2013-02-25 14:47:55 +1000439static void qxl_crtc_commit(struct drm_crtc *crtc)
440{
441 DRM_DEBUG("\n");
442}
443
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100444static void qxl_crtc_disable(struct drm_crtc *crtc)
445{
446 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Gabriel Krisman Bertazi37235452017-02-27 17:43:22 -0300447 struct qxl_device *qdev = crtc->dev->dev_private;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100448
449 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
450
451 qxl_send_monitors_config(qdev);
452}
453
Dave Airlief64122c2013-02-25 14:47:55 +1000454static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
455 .dpms = qxl_crtc_dpms,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100456 .disable = qxl_crtc_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000457 .mode_fixup = qxl_crtc_mode_fixup,
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300458 .mode_set_nofb = qxl_mode_set_nofb,
Dave Airlief64122c2013-02-25 14:47:55 +1000459 .commit = qxl_crtc_commit,
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300460 .atomic_flush = qxl_crtc_atomic_flush,
Dave Airlief64122c2013-02-25 14:47:55 +1000461};
462
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300463int qxl_primary_atomic_check(struct drm_plane *plane,
464 struct drm_plane_state *state)
465{
466 struct qxl_device *qdev = plane->dev->dev_private;
467 struct qxl_framebuffer *qfb;
468 struct qxl_bo *bo;
469
470 if (!state->crtc || !state->fb)
471 return 0;
472
473 qfb = to_qxl_framebuffer(state->fb);
474 bo = gem_to_qxl_bo(qfb->obj);
475
476 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
477 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
478 return -EINVAL;
479 }
480
481 return 0;
482}
483
484static void qxl_primary_atomic_update(struct drm_plane *plane,
485 struct drm_plane_state *old_state)
486{
487 struct qxl_device *qdev = plane->dev->dev_private;
488 struct qxl_framebuffer *qfb =
489 to_qxl_framebuffer(plane->state->fb);
490 struct qxl_framebuffer *qfb_old;
491 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
492 struct qxl_bo *bo_old;
493 struct drm_clip_rect norect = {
494 .x1 = 0,
495 .y1 = 0,
496 .x2 = qfb->base.width,
497 .y2 = qfb->base.height
498 };
499
500 if (!old_state->fb) {
501 qxl_io_log(qdev,
502 "create primary fb: %dx%d,%d,%d\n",
503 bo->surf.width, bo->surf.height,
504 bo->surf.stride, bo->surf.format);
505
506 qxl_io_create_primary(qdev, 0, bo);
507 bo->is_primary = true;
508 return;
509
510 } else {
511 qfb_old = to_qxl_framebuffer(old_state->fb);
512 bo_old = gem_to_qxl_bo(qfb_old->obj);
513 bo_old->is_primary = false;
514 }
515
516 bo->is_primary = true;
517 qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
518}
519
520static void qxl_primary_atomic_disable(struct drm_plane *plane,
521 struct drm_plane_state *old_state)
522{
523 struct qxl_device *qdev = plane->dev->dev_private;
524
525 if (old_state->fb)
526 { struct qxl_framebuffer *qfb =
527 to_qxl_framebuffer(old_state->fb);
528 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
529
530 qxl_io_destroy_primary(qdev);
531 bo->is_primary = false;
532 }
533}
534
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300535int qxl_plane_atomic_check(struct drm_plane *plane,
536 struct drm_plane_state *state)
537{
538 return 0;
539}
540
541static void qxl_cursor_atomic_update(struct drm_plane *plane,
542 struct drm_plane_state *old_state)
543{
544 struct drm_device *dev = plane->dev;
545 struct qxl_device *qdev = dev->dev_private;
546 struct drm_framebuffer *fb = plane->state->fb;
547 struct qxl_release *release;
548 struct qxl_cursor_cmd *cmd;
549 struct qxl_cursor *cursor;
550 struct drm_gem_object *obj;
551 struct qxl_bo *cursor_bo, *user_bo = NULL;
552 int ret;
553 void *user_ptr;
554 int size = 64*64*4;
555
556 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
557 QXL_RELEASE_CURSOR_CMD,
558 &release, NULL);
559
560 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
561
562 if (fb != old_state->fb) {
563 obj = to_qxl_framebuffer(fb)->obj;
564 user_bo = gem_to_qxl_bo(obj);
565
566 /* pinning is done in the prepare/cleanup framevbuffer */
567 ret = qxl_bo_kmap(user_bo, &user_ptr);
568 if (ret)
569 goto out_free_release;
570
571 ret = qxl_alloc_bo_reserved(qdev, release,
572 sizeof(struct qxl_cursor) + size,
573 &cursor_bo);
574 if (ret)
575 goto out_kunmap;
576
577 ret = qxl_release_reserve_list(release, true);
578 if (ret)
579 goto out_free_bo;
580
581 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
582 if (ret)
583 goto out_backoff;
584
585 cursor->header.unique = 0;
586 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
587 cursor->header.width = 64;
588 cursor->header.height = 64;
589 cursor->header.hot_spot_x = fb->hot_x;
590 cursor->header.hot_spot_y = fb->hot_y;
591 cursor->data_size = size;
592 cursor->chunk.next_chunk = 0;
593 cursor->chunk.prev_chunk = 0;
594 cursor->chunk.data_size = size;
595 memcpy(cursor->chunk.data, user_ptr, size);
596 qxl_bo_kunmap(cursor_bo);
597 qxl_bo_kunmap(user_bo);
598
599 cmd->u.set.visible = 1;
600 cmd->u.set.shape = qxl_bo_physical_address(qdev,
601 cursor_bo, 0);
602 cmd->type = QXL_CURSOR_SET;
603 } else {
604
605 ret = qxl_release_reserve_list(release, true);
606 if (ret)
607 goto out_free_release;
608
609 cmd->type = QXL_CURSOR_MOVE;
610 }
611
612 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
613 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
614
615 qxl_release_unmap(qdev, release, &cmd->release_info);
616 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
617 qxl_release_fence_buffer_objects(release);
618
619 return;
620
621out_backoff:
622 qxl_release_backoff_reserve_list(release);
623out_free_bo:
624 qxl_bo_unref(&cursor_bo);
625out_kunmap:
626 qxl_bo_kunmap(user_bo);
627out_free_release:
628 qxl_release_free(qdev, release);
629 return;
630
631}
632
633void qxl_cursor_atomic_disable(struct drm_plane *plane,
634 struct drm_plane_state *old_state)
635{
636 struct qxl_device *qdev = plane->dev->dev_private;
637 struct qxl_release *release;
638 struct qxl_cursor_cmd *cmd;
639 int ret;
640
641 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
642 QXL_RELEASE_CURSOR_CMD,
643 &release, NULL);
644 if (ret)
645 return;
646
647 ret = qxl_release_reserve_list(release, true);
648 if (ret) {
649 qxl_release_free(qdev, release);
650 return;
651 }
652
653 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
654 cmd->type = QXL_CURSOR_HIDE;
655 qxl_release_unmap(qdev, release, &cmd->release_info);
656
657 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
658 qxl_release_fence_buffer_objects(release);
659}
660
661int qxl_plane_prepare_fb(struct drm_plane *plane,
662 struct drm_plane_state *new_state)
663{
664 struct drm_gem_object *obj;
665 struct qxl_bo *user_bo;
666 int ret;
667
668 if (!new_state->fb)
669 return 0;
670
671 obj = to_qxl_framebuffer(new_state->fb)->obj;
672 user_bo = gem_to_qxl_bo(obj);
673
674 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
675 if (ret)
676 return ret;
677
678 return 0;
679}
680
681static void qxl_plane_cleanup_fb(struct drm_plane *plane,
682 struct drm_plane_state *old_state)
683{
684 struct drm_gem_object *obj;
685 struct qxl_bo *user_bo;
686
687 if (!plane->state->fb) {
688 /* we never executed prepare_fb, so there's nothing to
689 * unpin.
690 */
691 return;
692 }
693
694 obj = to_qxl_framebuffer(plane->state->fb)->obj;
695 user_bo = gem_to_qxl_bo(obj);
696 qxl_bo_unpin(user_bo);
697}
698
699static const uint32_t qxl_cursor_plane_formats[] = {
700 DRM_FORMAT_ARGB8888,
701};
702
703static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
704 .atomic_check = qxl_plane_atomic_check,
705 .atomic_update = qxl_cursor_atomic_update,
706 .atomic_disable = qxl_cursor_atomic_disable,
707 .prepare_fb = qxl_plane_prepare_fb,
708 .cleanup_fb = qxl_plane_cleanup_fb,
709};
710
711static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -0300712 .update_plane = drm_atomic_helper_update_plane,
713 .disable_plane = drm_atomic_helper_disable_plane,
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300714 .destroy = drm_primary_helper_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300715 .reset = drm_atomic_helper_plane_reset,
716 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
717 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300718};
719
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300720static const uint32_t qxl_primary_plane_formats[] = {
721 DRM_FORMAT_XRGB8888,
722 DRM_FORMAT_ARGB8888,
723};
724
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300725static const struct drm_plane_helper_funcs primary_helper_funcs = {
726 .atomic_check = qxl_primary_atomic_check,
727 .atomic_update = qxl_primary_atomic_update,
728 .atomic_disable = qxl_primary_atomic_disable,
729 .prepare_fb = qxl_plane_prepare_fb,
730 .cleanup_fb = qxl_plane_cleanup_fb,
731};
732
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300733static const struct drm_plane_funcs qxl_primary_plane_funcs = {
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -0300734 .update_plane = drm_atomic_helper_update_plane,
735 .disable_plane = drm_atomic_helper_disable_plane,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300736 .destroy = drm_primary_helper_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300737 .reset = drm_atomic_helper_plane_reset,
738 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
739 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300740};
741
742static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
743 unsigned int possible_crtcs,
744 enum drm_plane_type type)
745{
746 const struct drm_plane_helper_funcs *helper_funcs = NULL;
747 struct drm_plane *plane;
748 const struct drm_plane_funcs *funcs;
749 const uint32_t *formats;
750 int num_formats;
751 int err;
752
753 if (type == DRM_PLANE_TYPE_PRIMARY) {
754 funcs = &qxl_primary_plane_funcs;
755 formats = qxl_primary_plane_formats;
756 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300757 helper_funcs = &primary_helper_funcs;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300758 } else if (type == DRM_PLANE_TYPE_CURSOR) {
759 funcs = &qxl_cursor_plane_funcs;
760 formats = qxl_cursor_plane_formats;
761 helper_funcs = &qxl_cursor_helper_funcs;
762 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300763 } else {
764 return ERR_PTR(-EINVAL);
765 }
766
767 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
768 if (!plane)
769 return ERR_PTR(-ENOMEM);
770
771 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
772 funcs, formats, num_formats,
773 type, NULL);
774 if (err)
775 goto free_plane;
776
777 drm_plane_helper_add(plane, helper_funcs);
778
779 return plane;
780
781free_plane:
782 kfree(plane);
783 return ERR_PTR(-EINVAL);
784}
785
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100786static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000787{
788 struct qxl_crtc *qxl_crtc;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300789 struct drm_plane *primary, *cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300790 struct qxl_device *qdev = dev->dev_private;
791 int r;
Dave Airlief64122c2013-02-25 14:47:55 +1000792
793 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
794 if (!qxl_crtc)
795 return -ENOMEM;
796
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300797 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
798 if (IS_ERR(primary)) {
799 r = -ENOMEM;
800 goto free_mem;
801 }
802
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300803 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
804 if (IS_ERR(cursor)) {
805 r = -ENOMEM;
806 goto clean_primary;
807 }
808
809 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300810 &qxl_crtc_funcs, NULL);
811 if (r)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300812 goto clean_cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300813
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100814 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000815 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
816 return 0;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300817
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300818clean_cursor:
819 drm_plane_cleanup(cursor);
820 kfree(cursor);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300821clean_primary:
822 drm_plane_cleanup(primary);
823 kfree(primary);
824free_mem:
825 kfree(qxl_crtc);
826 return r;
Dave Airlief64122c2013-02-25 14:47:55 +1000827}
828
829static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
830{
831 DRM_DEBUG("\n");
832}
833
Dave Airlief64122c2013-02-25 14:47:55 +1000834static void qxl_enc_prepare(struct drm_encoder *encoder)
835{
836 DRM_DEBUG("\n");
837}
838
839static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
840 struct drm_encoder *encoder)
841{
842 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100843 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000844 struct qxl_head *head;
845 struct drm_display_mode *mode;
846
847 BUG_ON(!encoder);
848 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100849 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000850 if (!qdev->monitors_config ||
851 qdev->monitors_config->max_allowed <= i) {
852 DRM_ERROR(
853 "head number too large or missing monitors config: %p, %d",
854 qdev->monitors_config,
855 qdev->monitors_config ?
856 qdev->monitors_config->max_allowed : -1);
857 return;
858 }
859 if (!encoder->crtc) {
860 DRM_ERROR("missing crtc on encoder %p\n", encoder);
861 return;
862 }
863 if (i != 0)
864 DRM_DEBUG("missing for multiple monitors: no head holes\n");
865 head = &qdev->monitors_config->heads[i];
866 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000867 if (encoder->crtc->enabled) {
868 mode = &encoder->crtc->mode;
869 head->width = mode->hdisplay;
870 head->height = mode->vdisplay;
871 head->x = encoder->crtc->x;
872 head->y = encoder->crtc->y;
873 if (qdev->monitors_config->count < i + 1)
874 qdev->monitors_config->count = i + 1;
875 } else {
876 head->width = 0;
877 head->height = 0;
878 head->x = 0;
879 head->y = 0;
880 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100881 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
882 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000883 head->flags = 0;
884 /* TODO - somewhere else to call this for multiple monitors
885 * (config_commit?) */
886 qxl_send_monitors_config(qdev);
887}
888
889static void qxl_enc_commit(struct drm_encoder *encoder)
890{
891 struct qxl_device *qdev = encoder->dev->dev_private;
892
893 qxl_write_monitors_config_for_encoder(qdev, encoder);
894 DRM_DEBUG("\n");
895}
896
897static void qxl_enc_mode_set(struct drm_encoder *encoder,
898 struct drm_display_mode *mode,
899 struct drm_display_mode *adjusted_mode)
900{
901 DRM_DEBUG("\n");
902}
903
904static int qxl_conn_get_modes(struct drm_connector *connector)
905{
906 int ret = 0;
907 struct qxl_device *qdev = connector->dev->dev_private;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200908 unsigned pwidth = 1024;
909 unsigned pheight = 768;
Dave Airlief64122c2013-02-25 14:47:55 +1000910
911 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
912 /* TODO: what should we do here? only show the configured modes for the
913 * device, or allow the full list, or both? */
914 if (qdev->monitors_config && qdev->monitors_config->count) {
Marc-André Lureaub0807422013-10-18 16:11:31 +0200915 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000916 if (ret < 0)
917 return ret;
918 }
Marc-André Lureaub0807422013-10-18 16:11:31 +0200919 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000920 return ret;
921}
922
923static int qxl_conn_mode_valid(struct drm_connector *connector,
924 struct drm_display_mode *mode)
925{
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500926 struct drm_device *ddev = connector->dev;
927 struct qxl_device *qdev = ddev->dev_private;
928 int i;
929
Dave Airlief64122c2013-02-25 14:47:55 +1000930 /* TODO: is this called for user defined modes? (xrandr --add-mode)
931 * TODO: check that the mode fits in the framebuffer */
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500932
933 if(qdev->monitors_config_width == mode->hdisplay &&
934 qdev->monitors_config_height == mode->vdisplay)
935 return MODE_OK;
936
937 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
938 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
939 return MODE_OK;
940 }
941 return MODE_BAD;
Dave Airlief64122c2013-02-25 14:47:55 +1000942}
943
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000944static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +1000945{
946 struct qxl_output *qxl_output =
947 drm_connector_to_qxl_output(connector);
948
949 DRM_DEBUG("\n");
950 return &qxl_output->enc;
951}
952
953
954static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
955 .dpms = qxl_enc_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +1000956 .prepare = qxl_enc_prepare,
957 .mode_set = qxl_enc_mode_set,
958 .commit = qxl_enc_commit,
959};
960
961static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
962 .get_modes = qxl_conn_get_modes,
963 .mode_valid = qxl_conn_mode_valid,
964 .best_encoder = qxl_best_encoder,
965};
966
Dave Airlief64122c2013-02-25 14:47:55 +1000967static enum drm_connector_status qxl_conn_detect(
968 struct drm_connector *connector,
969 bool force)
970{
971 struct qxl_output *output =
972 drm_connector_to_qxl_output(connector);
973 struct drm_device *ddev = connector->dev;
974 struct qxl_device *qdev = ddev->dev_private;
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000975 bool connected = false;
Dave Airlief64122c2013-02-25 14:47:55 +1000976
977 /* The first monitor is always connected */
Dave Airlie69e5d3f2015-09-14 10:28:34 +1000978 if (!qdev->client_monitors_config) {
979 if (output->index == 0)
980 connected = true;
981 } else
982 connected = qdev->client_monitors_config->count > output->index &&
983 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
Dave Airlief64122c2013-02-25 14:47:55 +1000984
Marc-André Lureau5cab51c2013-10-18 16:11:33 +0200985 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
986 if (!connected)
987 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
988
Dave Airlief64122c2013-02-25 14:47:55 +1000989 return connected ? connector_status_connected
990 : connector_status_disconnected;
991}
992
993static int qxl_conn_set_property(struct drm_connector *connector,
994 struct drm_property *property,
995 uint64_t value)
996{
997 DRM_DEBUG("\n");
998 return 0;
999}
1000
1001static void qxl_conn_destroy(struct drm_connector *connector)
1002{
1003 struct qxl_output *qxl_output =
1004 drm_connector_to_qxl_output(connector);
1005
Thomas Wood34ea3d32014-05-29 16:57:41 +01001006 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001007 drm_connector_cleanup(connector);
1008 kfree(qxl_output);
1009}
1010
1011static const struct drm_connector_funcs qxl_connector_funcs = {
1012 .dpms = drm_helper_connector_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001013 .detect = qxl_conn_detect,
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001014 .fill_modes = drm_helper_probe_single_connector_modes,
Dave Airlief64122c2013-02-25 14:47:55 +10001015 .set_property = qxl_conn_set_property,
1016 .destroy = qxl_conn_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -03001017 .reset = drm_atomic_helper_connector_reset,
1018 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1019 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Dave Airlief64122c2013-02-25 14:47:55 +10001020};
1021
1022static void qxl_enc_destroy(struct drm_encoder *encoder)
1023{
1024 drm_encoder_cleanup(encoder);
1025}
1026
1027static const struct drm_encoder_funcs qxl_enc_funcs = {
1028 .destroy = qxl_enc_destroy,
1029};
1030
Dave Airlie4695b032013-10-11 11:05:00 +10001031static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1032{
1033 if (qdev->hotplug_mode_update_property)
1034 return 0;
1035
1036 qdev->hotplug_mode_update_property =
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001037 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlie4695b032013-10-11 11:05:00 +10001038 "hotplug_mode_update", 0, 1);
1039
1040 return 0;
1041}
1042
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001043static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +10001044{
Dave Airlie4695b032013-10-11 11:05:00 +10001045 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +10001046 struct qxl_output *qxl_output;
1047 struct drm_connector *connector;
1048 struct drm_encoder *encoder;
1049
1050 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1051 if (!qxl_output)
1052 return -ENOMEM;
1053
1054 qxl_output->index = num_output;
1055
1056 connector = &qxl_output->base;
1057 encoder = &qxl_output->enc;
1058 drm_connector_init(dev, &qxl_output->base,
1059 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1060
1061 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001062 DRM_MODE_ENCODER_VIRTUAL, NULL);
Dave Airlief64122c2013-02-25 14:47:55 +10001063
Dave Airlie5ff91e42013-07-05 10:20:33 +10001064 /* we get HPD via client monitors config */
1065 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +10001066 encoder->possible_crtcs = 1 << num_output;
1067 drm_mode_connector_attach_encoder(&qxl_output->base,
1068 &qxl_output->enc);
1069 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1070 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1071
Dave Airlie4695b032013-10-11 11:05:00 +10001072 drm_object_attach_property(&connector->base,
1073 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +10001074 drm_object_attach_property(&connector->base,
1075 dev->mode_config.suggested_x_property, 0);
1076 drm_object_attach_property(&connector->base,
1077 dev->mode_config.suggested_y_property, 0);
Dave Airlief64122c2013-02-25 14:47:55 +10001078 return 0;
1079}
1080
1081static struct drm_framebuffer *
1082qxl_user_framebuffer_create(struct drm_device *dev,
1083 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02001084 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief64122c2013-02-25 14:47:55 +10001085{
1086 struct drm_gem_object *obj;
1087 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +10001088 int ret;
1089
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001090 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1091 if (!obj)
1092 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +10001093
1094 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1095 if (qxl_fb == NULL)
1096 return NULL;
1097
Noralf Trønnes6819c3c2016-04-28 17:18:36 +02001098 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +10001099 if (ret) {
1100 kfree(qxl_fb);
1101 drm_gem_object_unreference_unlocked(obj);
1102 return NULL;
1103 }
1104
Dave Airlief64122c2013-02-25 14:47:55 +10001105 return &qxl_fb->base;
1106}
1107
1108static const struct drm_mode_config_funcs qxl_mode_funcs = {
1109 .fb_create = qxl_user_framebuffer_create,
Gabriel Krisman Bertazi472e6d42017-02-27 17:43:25 -03001110 .atomic_check = drm_atomic_helper_check,
1111 .atomic_commit = drm_atomic_helper_commit,
Dave Airlief64122c2013-02-25 14:47:55 +10001112};
1113
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001114int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001115{
Dave Airlief64122c2013-02-25 14:47:55 +10001116 int ret;
1117 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001118 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001119 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001120 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001121
Dave Airlief64122c2013-02-25 14:47:55 +10001122 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1123 QXL_GEM_DOMAIN_VRAM,
1124 false, false, NULL, &gobj);
1125 if (ret) {
1126 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1127 return -ENOMEM;
1128 }
1129 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001130
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001131 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001132 if (ret)
1133 return ret;
1134
Dave Airlief64122c2013-02-25 14:47:55 +10001135 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001136
Dave Airlief64122c2013-02-25 14:47:55 +10001137 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1138 qdev->ram_header->monitors_config =
1139 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1140
1141 memset(qdev->monitors_config, 0, monitors_config_size);
1142 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001143 return 0;
1144}
1145
1146int qxl_destroy_monitors_object(struct qxl_device *qdev)
1147{
1148 int ret;
1149
1150 qdev->monitors_config = NULL;
1151 qdev->ram_header->monitors_config = 0;
1152
1153 qxl_bo_kunmap(qdev->monitors_config_bo);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001154 ret = qxl_bo_unpin(qdev->monitors_config_bo);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001155 if (ret)
1156 return ret;
1157
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001158 qxl_bo_unref(&qdev->monitors_config_bo);
1159 return 0;
1160}
1161
1162int qxl_modeset_init(struct qxl_device *qdev)
1163{
1164 int i;
1165 int ret;
1166
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001167 drm_mode_config_init(&qdev->ddev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001168
1169 ret = qxl_create_monitors_object(qdev);
1170 if (ret)
1171 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001172
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001173 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
Dave Airlief64122c2013-02-25 14:47:55 +10001174
1175 /* modes will be validated against the framebuffer size */
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -03001176 qdev->ddev.mode_config.min_width = 0;
1177 qdev->ddev.mode_config.min_height = 0;
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001178 qdev->ddev.mode_config.max_width = 8192;
1179 qdev->ddev.mode_config.max_height = 8192;
Dave Airlief64122c2013-02-25 14:47:55 +10001180
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001181 qdev->ddev.mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001182
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001183 drm_mode_create_suggested_offset_properties(&qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001184 qxl_mode_create_hotplug_mode_update_property(qdev);
1185
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001186 for (i = 0 ; i < qxl_num_crtc; ++i) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001187 qdev_crtc_init(&qdev->ddev, i);
1188 qdev_output_init(&qdev->ddev, i);
Dave Airlief64122c2013-02-25 14:47:55 +10001189 }
1190
1191 qdev->mode_info.mode_config_initialized = true;
1192
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -03001193 drm_mode_config_reset(&qdev->ddev);
1194
Dave Airlief64122c2013-02-25 14:47:55 +10001195 /* primary surface must be created by this point, to allow
1196 * issuing command queue commands and having them read by
1197 * spice server. */
1198 qxl_fbdev_init(qdev);
1199 return 0;
1200}
1201
1202void qxl_modeset_fini(struct qxl_device *qdev)
1203{
1204 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001205
1206 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001207 if (qdev->mode_info.mode_config_initialized) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001208 drm_mode_config_cleanup(&qdev->ddev);
Dave Airlief64122c2013-02-25 14:47:55 +10001209 qdev->mode_info.mode_config_initialized = false;
1210 }
1211}