blob: 09c076f5a79293b7e4db67d3385f4cf252f4fe81 [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>
Dave Airlief64122c2013-02-25 14:47:55 +100034
Dave Airlie07f8d9b2013-07-02 06:37:13 +010035static bool qxl_head_enabled(struct qxl_head *head)
36{
37 return head->width && head->height;
38}
39
Christophe Fergeaue4a76442016-11-08 10:12:03 +010040static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
Dave Airlief64122c2013-02-25 14:47:55 +100041{
42 if (qdev->client_monitors_config &&
43 count > qdev->client_monitors_config->count) {
44 kfree(qdev->client_monitors_config);
Dave Airlie62c8ba72013-04-16 13:36:00 +100045 qdev->client_monitors_config = NULL;
Dave Airlief64122c2013-02-25 14:47:55 +100046 }
47 if (!qdev->client_monitors_config) {
48 qdev->client_monitors_config = kzalloc(
49 sizeof(struct qxl_monitors_config) +
50 sizeof(struct qxl_head) * count, GFP_KERNEL);
51 if (!qdev->client_monitors_config) {
52 qxl_io_log(qdev,
53 "%s: allocation failure for %u heads\n",
54 __func__, count);
55 return;
56 }
57 }
58 qdev->client_monitors_config->count = count;
59}
60
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010061enum {
62 MONITORS_CONFIG_MODIFIED,
63 MONITORS_CONFIG_UNCHANGED,
64 MONITORS_CONFIG_BAD_CRC,
65};
66
Dave Airlief64122c2013-02-25 14:47:55 +100067static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
68{
69 int i;
70 int num_monitors;
71 uint32_t crc;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010072 int status = MONITORS_CONFIG_UNCHANGED;
Dave Airlief64122c2013-02-25 14:47:55 +100073
Dave Airlief64122c2013-02-25 14:47:55 +100074 num_monitors = qdev->rom->client_monitors_config.count;
75 crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
76 sizeof(qdev->rom->client_monitors_config));
77 if (crc != qdev->rom->client_monitors_config_crc) {
Frediano Ziglio72ec5652015-06-03 12:09:16 +010078 qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
Dave Airlief64122c2013-02-25 14:47:55 +100079 sizeof(qdev->rom->client_monitors_config),
80 qdev->rom->client_monitors_config_crc);
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010081 return MONITORS_CONFIG_BAD_CRC;
Dave Airlief64122c2013-02-25 14:47:55 +100082 }
83 if (num_monitors > qdev->monitors_config->max_allowed) {
Dave Airlie5b8788c2013-07-01 14:14:38 +100084 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
85 qdev->monitors_config->max_allowed, num_monitors);
Dave Airlief64122c2013-02-25 14:47:55 +100086 num_monitors = qdev->monitors_config->max_allowed;
87 } else {
88 num_monitors = qdev->rom->client_monitors_config.count;
89 }
Christophe Fergeau9e3b3172016-11-08 10:12:08 +010090 if (qdev->client_monitors_config
91 && (num_monitors != qdev->client_monitors_config->count)) {
92 status = MONITORS_CONFIG_MODIFIED;
93 }
Dave Airlief64122c2013-02-25 14:47:55 +100094 qxl_alloc_client_monitors_config(qdev, num_monitors);
95 /* we copy max from the client but it isn't used */
96 qdev->client_monitors_config->max_allowed =
97 qdev->monitors_config->max_allowed;
98 for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
99 struct qxl_urect *c_rect =
100 &qdev->rom->client_monitors_config.heads[i];
101 struct qxl_head *client_head =
102 &qdev->client_monitors_config->heads[i];
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100103 if (client_head->x != c_rect->left) {
104 client_head->x = c_rect->left;
105 status = MONITORS_CONFIG_MODIFIED;
106 }
107 if (client_head->y != c_rect->top) {
108 client_head->y = c_rect->top;
109 status = MONITORS_CONFIG_MODIFIED;
110 }
111 if (client_head->width != c_rect->right - c_rect->left) {
112 client_head->width = c_rect->right - c_rect->left;
113 status = MONITORS_CONFIG_MODIFIED;
114 }
115 if (client_head->height != c_rect->bottom - c_rect->top) {
116 client_head->height = c_rect->bottom - c_rect->top;
117 status = MONITORS_CONFIG_MODIFIED;
118 }
119 if (client_head->surface_id != 0) {
120 client_head->surface_id = 0;
121 status = MONITORS_CONFIG_MODIFIED;
122 }
123 if (client_head->id != i) {
124 client_head->id = i;
125 status = MONITORS_CONFIG_MODIFIED;
126 }
127 if (client_head->flags != 0) {
128 client_head->flags = 0;
129 status = MONITORS_CONFIG_MODIFIED;
130 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100131 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
132 client_head->x, client_head->y);
Dave Airlief64122c2013-02-25 14:47:55 +1000133 }
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100134
135 return status;
Dave Airlief64122c2013-02-25 14:47:55 +1000136}
137
Dave Airlie7dea0942014-10-28 11:28:44 +1000138static void qxl_update_offset_props(struct qxl_device *qdev)
139{
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200140 struct drm_device *dev = &qdev->ddev;
Dave Airlie7dea0942014-10-28 11:28:44 +1000141 struct drm_connector *connector;
142 struct qxl_output *output;
143 struct qxl_head *head;
144
145 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
146 output = drm_connector_to_qxl_output(connector);
147
148 head = &qdev->client_monitors_config->heads[output->index];
149
150 drm_object_property_set_value(&connector->base,
151 dev->mode_config.suggested_x_property, head->x);
152 drm_object_property_set_value(&connector->base,
153 dev->mode_config.suggested_y_property, head->y);
154 }
155}
156
Dave Airlief64122c2013-02-25 14:47:55 +1000157void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
158{
159
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200160 struct drm_device *dev = &qdev->ddev;
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100161 int status;
162
163 status = qxl_display_copy_rom_client_monitors_config(qdev);
164 while (status == MONITORS_CONFIG_BAD_CRC) {
Dave Airlief64122c2013-02-25 14:47:55 +1000165 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
166 " retrying\n");
Christophe Fergeau9e3b3172016-11-08 10:12:08 +0100167 status = qxl_display_copy_rom_client_monitors_config(qdev);
168 }
169 if (status == MONITORS_CONFIG_UNCHANGED) {
170 qxl_io_log(qdev, "config unchanged\n");
171 DRM_DEBUG("ignoring unchanged client monitors config");
172 return;
Dave Airlief64122c2013-02-25 14:47:55 +1000173 }
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200174
Dave Airlie7dea0942014-10-28 11:28:44 +1000175 drm_modeset_lock_all(dev);
176 qxl_update_offset_props(qdev);
177 drm_modeset_unlock_all(dev);
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200178 if (!drm_helper_hpd_irq_event(dev)) {
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200179 /* notify that the monitor configuration changed, to
180 adjust at the arbitrary resolution */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200181 drm_kms_helper_hotplug_event(dev);
Marc-André Lureau4fdb0862013-10-18 16:11:29 +0200182 }
Dave Airlief64122c2013-02-25 14:47:55 +1000183}
184
Marc-André Lureaub0807422013-10-18 16:11:31 +0200185static int qxl_add_monitors_config_modes(struct drm_connector *connector,
186 unsigned *pwidth,
187 unsigned *pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000188{
189 struct drm_device *dev = connector->dev;
190 struct qxl_device *qdev = dev->dev_private;
191 struct qxl_output *output = drm_connector_to_qxl_output(connector);
192 int h = output->index;
193 struct drm_display_mode *mode = NULL;
194 struct qxl_head *head;
195
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100196 if (!qdev->client_monitors_config)
Dave Airlief64122c2013-02-25 14:47:55 +1000197 return 0;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100198 head = &qdev->client_monitors_config->heads[h];
Dave Airlief64122c2013-02-25 14:47:55 +1000199
200 mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
201 false);
202 mode->type |= DRM_MODE_TYPE_PREFERRED;
Christophe Fergeauff996e72016-11-08 10:12:09 +0100203 mode->hdisplay = head->width;
204 mode->vdisplay = head->height;
205 drm_mode_set_name(mode);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200206 *pwidth = head->width;
207 *pheight = head->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000208 drm_mode_probed_add(connector, mode);
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500209 /* remember the last custom size for mode validation */
210 qdev->monitors_config_width = mode->hdisplay;
211 qdev->monitors_config_height = mode->vdisplay;
Dave Airlief64122c2013-02-25 14:47:55 +1000212 return 1;
213}
214
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500215static struct mode_size {
216 int w;
217 int h;
218} common_modes[] = {
219 { 640, 480},
220 { 720, 480},
221 { 800, 600},
222 { 848, 480},
223 {1024, 768},
224 {1152, 768},
225 {1280, 720},
226 {1280, 800},
227 {1280, 854},
228 {1280, 960},
229 {1280, 1024},
230 {1440, 900},
231 {1400, 1050},
232 {1680, 1050},
233 {1600, 1200},
234 {1920, 1080},
235 {1920, 1200}
236};
237
Marc-André Lureaub0807422013-10-18 16:11:31 +0200238static int qxl_add_common_modes(struct drm_connector *connector,
239 unsigned pwidth,
240 unsigned pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000241{
242 struct drm_device *dev = connector->dev;
243 struct drm_display_mode *mode = NULL;
244 int i;
Dave Airlief64122c2013-02-25 14:47:55 +1000245 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
Dave Airlief64122c2013-02-25 14:47:55 +1000246 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
247 60, false, false, false);
Marc-André Lureaub0807422013-10-18 16:11:31 +0200248 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
Dave Airlief64122c2013-02-25 14:47:55 +1000249 mode->type |= DRM_MODE_TYPE_PREFERRED;
250 drm_mode_probed_add(connector, mode);
251 }
252 return i - 1;
253}
254
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300255static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
256 struct drm_crtc_state *old_crtc_state)
257{
258 struct drm_device *dev = crtc->dev;
259 struct drm_pending_vblank_event *event;
260 unsigned long flags;
261
262 if (crtc->state && crtc->state->event) {
263 event = crtc->state->event;
264 crtc->state->event = NULL;
265
266 spin_lock_irqsave(&dev->event_lock, flags);
267 drm_crtc_send_vblank_event(crtc, event);
268 spin_unlock_irqrestore(&dev->event_lock, flags);
269 }
270}
271
Dave Airlief64122c2013-02-25 14:47:55 +1000272static void qxl_crtc_destroy(struct drm_crtc *crtc)
273{
274 struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
275
276 drm_crtc_cleanup(crtc);
277 kfree(qxl_crtc);
278}
279
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200280static int qxl_crtc_page_flip(struct drm_crtc *crtc,
281 struct drm_framebuffer *fb,
282 struct drm_pending_vblank_event *event,
283 uint32_t page_flip_flags)
284{
285 struct drm_device *dev = crtc->dev;
286 struct qxl_device *qdev = dev->dev_private;
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200287 struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
288 struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
289 struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
290 struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
291 unsigned long flags;
292 struct drm_clip_rect norect = {
293 .x1 = 0,
294 .y1 = 0,
295 .x2 = fb->width,
296 .y2 = fb->height
297 };
298 int inc = 1;
299 int one_clip_rect = 1;
300 int ret = 0;
301
302 crtc->primary->fb = fb;
303 bo_old->is_primary = false;
304 bo->is_primary = true;
305
Frediano Ziglio7eb99742015-09-24 14:25:14 +0100306 ret = qxl_bo_pin(bo, bo->type, NULL);
Frediano Ziglio7eb99742015-09-24 14:25:14 +0100307 if (ret)
308 return ret;
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200309
310 qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
311 &norect, one_clip_rect, inc);
312
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200313 if (event) {
314 spin_lock_irqsave(&dev->event_lock, flags);
Gustavo Padovan8a605222016-06-06 11:41:35 -0300315 drm_crtc_send_vblank_event(crtc, event);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200316 spin_unlock_irqrestore(&dev->event_lock, flags);
317 }
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200318
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300319 qxl_bo_unpin(bo);
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200320
321 return 0;
322}
323
Dave Airlief64122c2013-02-25 14:47:55 +1000324static const struct drm_crtc_funcs qxl_crtc_funcs = {
Dave Airlief64122c2013-02-25 14:47:55 +1000325 .set_config = drm_crtc_helper_set_config,
326 .destroy = qxl_crtc_destroy,
Andreas Pokorny058e9f52014-08-08 10:40:55 +0200327 .page_flip = qxl_crtc_page_flip,
Dave Airlief64122c2013-02-25 14:47:55 +1000328};
329
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200330void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
Dave Airlief64122c2013-02-25 14:47:55 +1000331{
332 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
333
Markus Elfringdc3583c2016-07-22 14:14:54 +0200334 drm_gem_object_unreference_unlocked(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000335 drm_framebuffer_cleanup(fb);
336 kfree(qxl_fb);
337}
338
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000339static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
340 struct drm_file *file_priv,
341 unsigned flags, unsigned color,
342 struct drm_clip_rect *clips,
343 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000344{
345 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
346 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
347 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
348 struct drm_clip_rect norect;
349 struct qxl_bo *qobj;
350 int inc = 1;
351
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200352 drm_modeset_lock_all(fb->dev);
353
Dave Airlief64122c2013-02-25 14:47:55 +1000354 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000355 /* if we aren't primary surface ignore this */
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200356 if (!qobj->is_primary) {
357 drm_modeset_unlock_all(fb->dev);
Dave Airlieb2b44652013-05-13 12:48:40 +1000358 return 0;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200359 }
Dave Airlieb2b44652013-05-13 12:48:40 +1000360
Dave Airlief64122c2013-02-25 14:47:55 +1000361 if (!num_clips) {
362 num_clips = 1;
363 clips = &norect;
364 norect.x1 = norect.y1 = 0;
365 norect.x2 = fb->width;
366 norect.y2 = fb->height;
367 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
368 num_clips /= 2;
369 inc = 2; /* skip source rects */
370 }
371
372 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
373 clips, num_clips, inc);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200374
375 drm_modeset_unlock_all(fb->dev);
376
Dave Airlief64122c2013-02-25 14:47:55 +1000377 return 0;
378}
379
380static const struct drm_framebuffer_funcs qxl_fb_funcs = {
381 .destroy = qxl_user_framebuffer_destroy,
382 .dirty = qxl_framebuffer_surface_dirty,
383/* TODO?
384 * .create_handle = qxl_user_framebuffer_create_handle, */
385};
386
387int
388qxl_framebuffer_init(struct drm_device *dev,
389 struct qxl_framebuffer *qfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200390 const struct drm_mode_fb_cmd2 *mode_cmd,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200391 struct drm_gem_object *obj,
392 const struct drm_framebuffer_funcs *funcs)
Dave Airlief64122c2013-02-25 14:47:55 +1000393{
394 int ret;
395
396 qfb->obj = obj;
Ville Syrjälä53609432016-11-18 21:52:51 +0200397 drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200398 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000399 if (ret) {
400 qfb->obj = NULL;
401 return ret;
402 }
Dave Airlief64122c2013-02-25 14:47:55 +1000403 return 0;
404}
405
406static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
407{
408}
409
410static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
411 const struct drm_display_mode *mode,
412 struct drm_display_mode *adjusted_mode)
413{
414 struct drm_device *dev = crtc->dev;
415 struct qxl_device *qdev = dev->dev_private;
416
417 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
418 __func__,
419 mode->hdisplay, mode->vdisplay,
420 adjusted_mode->hdisplay,
421 adjusted_mode->vdisplay);
422 return true;
423}
424
Christophe Fergeaue4a76442016-11-08 10:12:03 +0100425static void
Dave Airlief64122c2013-02-25 14:47:55 +1000426qxl_send_monitors_config(struct qxl_device *qdev)
427{
428 int i;
429
430 BUG_ON(!qdev->ram_header->monitors_config);
431
432 if (qdev->monitors_config->count == 0) {
433 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
434 return;
435 }
436 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
437 struct qxl_head *head = &qdev->monitors_config->heads[i];
438
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100439 if (head->y > 8192 || head->x > 8192 ||
Dave Airlief64122c2013-02-25 14:47:55 +1000440 head->width > 8192 || head->height > 8192) {
441 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
442 i, head->width, head->height,
443 head->x, head->y);
444 return;
445 }
446 }
447 qxl_io_monitors_config(qdev);
448}
449
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100450static void qxl_monitors_config_set(struct qxl_device *qdev,
451 int index,
452 unsigned x, unsigned y,
453 unsigned width, unsigned height,
454 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000455{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100456 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
457 qdev->monitors_config->heads[index].x = x;
458 qdev->monitors_config->heads[index].y = y;
459 qdev->monitors_config->heads[index].width = width;
460 qdev->monitors_config->heads[index].height = height;
461 qdev->monitors_config->heads[index].surface_id = surf_id;
462
Dave Airlief64122c2013-02-25 14:47:55 +1000463}
464
Dave Airlief64122c2013-02-25 14:47:55 +1000465static void qxl_crtc_prepare(struct drm_crtc *crtc)
466{
467 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
468 crtc->mode.hdisplay, crtc->mode.vdisplay,
469 crtc->x, crtc->y, crtc->enabled);
470}
471
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300472void qxl_mode_set_nofb(struct drm_crtc *crtc)
473{
474 struct qxl_device *qdev = crtc->dev->dev_private;
475 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
476 struct drm_display_mode *mode = &crtc->mode;
477
478 DRM_DEBUG("Mode set (%d,%d)\n",
479 mode->hdisplay, mode->vdisplay);
480
481 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
482 mode->hdisplay, mode->vdisplay, 0);
483
484}
485
Dave Airlief64122c2013-02-25 14:47:55 +1000486static void qxl_crtc_commit(struct drm_crtc *crtc)
487{
488 DRM_DEBUG("\n");
489}
490
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100491static void qxl_crtc_disable(struct drm_crtc *crtc)
492{
493 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Gabriel Krisman Bertazi37235452017-02-27 17:43:22 -0300494 struct qxl_device *qdev = crtc->dev->dev_private;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100495
496 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
497
498 qxl_send_monitors_config(qdev);
499}
500
Dave Airlief64122c2013-02-25 14:47:55 +1000501static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
502 .dpms = qxl_crtc_dpms,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100503 .disable = qxl_crtc_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000504 .mode_fixup = qxl_crtc_mode_fixup,
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300505 .mode_set = drm_helper_crtc_mode_set,
506 .mode_set_nofb = qxl_mode_set_nofb,
Dave Airlief64122c2013-02-25 14:47:55 +1000507 .prepare = qxl_crtc_prepare,
508 .commit = qxl_crtc_commit,
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300509 .atomic_flush = qxl_crtc_atomic_flush,
Dave Airlief64122c2013-02-25 14:47:55 +1000510};
511
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300512int qxl_primary_atomic_check(struct drm_plane *plane,
513 struct drm_plane_state *state)
514{
515 struct qxl_device *qdev = plane->dev->dev_private;
516 struct qxl_framebuffer *qfb;
517 struct qxl_bo *bo;
518
519 if (!state->crtc || !state->fb)
520 return 0;
521
522 qfb = to_qxl_framebuffer(state->fb);
523 bo = gem_to_qxl_bo(qfb->obj);
524
525 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
526 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
527 return -EINVAL;
528 }
529
530 return 0;
531}
532
533static void qxl_primary_atomic_update(struct drm_plane *plane,
534 struct drm_plane_state *old_state)
535{
536 struct qxl_device *qdev = plane->dev->dev_private;
537 struct qxl_framebuffer *qfb =
538 to_qxl_framebuffer(plane->state->fb);
539 struct qxl_framebuffer *qfb_old;
540 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
541 struct qxl_bo *bo_old;
542 struct drm_clip_rect norect = {
543 .x1 = 0,
544 .y1 = 0,
545 .x2 = qfb->base.width,
546 .y2 = qfb->base.height
547 };
548
549 if (!old_state->fb) {
550 qxl_io_log(qdev,
551 "create primary fb: %dx%d,%d,%d\n",
552 bo->surf.width, bo->surf.height,
553 bo->surf.stride, bo->surf.format);
554
555 qxl_io_create_primary(qdev, 0, bo);
556 bo->is_primary = true;
557 return;
558
559 } else {
560 qfb_old = to_qxl_framebuffer(old_state->fb);
561 bo_old = gem_to_qxl_bo(qfb_old->obj);
562 bo_old->is_primary = false;
563 }
564
565 bo->is_primary = true;
566 qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
567}
568
569static void qxl_primary_atomic_disable(struct drm_plane *plane,
570 struct drm_plane_state *old_state)
571{
572 struct qxl_device *qdev = plane->dev->dev_private;
573
574 if (old_state->fb)
575 { struct qxl_framebuffer *qfb =
576 to_qxl_framebuffer(old_state->fb);
577 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
578
579 qxl_io_destroy_primary(qdev);
580 bo->is_primary = false;
581 }
582}
583
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300584int qxl_plane_atomic_check(struct drm_plane *plane,
585 struct drm_plane_state *state)
586{
587 return 0;
588}
589
590static void qxl_cursor_atomic_update(struct drm_plane *plane,
591 struct drm_plane_state *old_state)
592{
593 struct drm_device *dev = plane->dev;
594 struct qxl_device *qdev = dev->dev_private;
595 struct drm_framebuffer *fb = plane->state->fb;
596 struct qxl_release *release;
597 struct qxl_cursor_cmd *cmd;
598 struct qxl_cursor *cursor;
599 struct drm_gem_object *obj;
600 struct qxl_bo *cursor_bo, *user_bo = NULL;
601 int ret;
602 void *user_ptr;
603 int size = 64*64*4;
604
605 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
606 QXL_RELEASE_CURSOR_CMD,
607 &release, NULL);
608
609 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
610
611 if (fb != old_state->fb) {
612 obj = to_qxl_framebuffer(fb)->obj;
613 user_bo = gem_to_qxl_bo(obj);
614
615 /* pinning is done in the prepare/cleanup framevbuffer */
616 ret = qxl_bo_kmap(user_bo, &user_ptr);
617 if (ret)
618 goto out_free_release;
619
620 ret = qxl_alloc_bo_reserved(qdev, release,
621 sizeof(struct qxl_cursor) + size,
622 &cursor_bo);
623 if (ret)
624 goto out_kunmap;
625
626 ret = qxl_release_reserve_list(release, true);
627 if (ret)
628 goto out_free_bo;
629
630 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
631 if (ret)
632 goto out_backoff;
633
634 cursor->header.unique = 0;
635 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
636 cursor->header.width = 64;
637 cursor->header.height = 64;
638 cursor->header.hot_spot_x = fb->hot_x;
639 cursor->header.hot_spot_y = fb->hot_y;
640 cursor->data_size = size;
641 cursor->chunk.next_chunk = 0;
642 cursor->chunk.prev_chunk = 0;
643 cursor->chunk.data_size = size;
644 memcpy(cursor->chunk.data, user_ptr, size);
645 qxl_bo_kunmap(cursor_bo);
646 qxl_bo_kunmap(user_bo);
647
648 cmd->u.set.visible = 1;
649 cmd->u.set.shape = qxl_bo_physical_address(qdev,
650 cursor_bo, 0);
651 cmd->type = QXL_CURSOR_SET;
652 } else {
653
654 ret = qxl_release_reserve_list(release, true);
655 if (ret)
656 goto out_free_release;
657
658 cmd->type = QXL_CURSOR_MOVE;
659 }
660
661 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
662 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
663
664 qxl_release_unmap(qdev, release, &cmd->release_info);
665 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
666 qxl_release_fence_buffer_objects(release);
667
668 return;
669
670out_backoff:
671 qxl_release_backoff_reserve_list(release);
672out_free_bo:
673 qxl_bo_unref(&cursor_bo);
674out_kunmap:
675 qxl_bo_kunmap(user_bo);
676out_free_release:
677 qxl_release_free(qdev, release);
678 return;
679
680}
681
682void qxl_cursor_atomic_disable(struct drm_plane *plane,
683 struct drm_plane_state *old_state)
684{
685 struct qxl_device *qdev = plane->dev->dev_private;
686 struct qxl_release *release;
687 struct qxl_cursor_cmd *cmd;
688 int ret;
689
690 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
691 QXL_RELEASE_CURSOR_CMD,
692 &release, NULL);
693 if (ret)
694 return;
695
696 ret = qxl_release_reserve_list(release, true);
697 if (ret) {
698 qxl_release_free(qdev, release);
699 return;
700 }
701
702 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
703 cmd->type = QXL_CURSOR_HIDE;
704 qxl_release_unmap(qdev, release, &cmd->release_info);
705
706 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
707 qxl_release_fence_buffer_objects(release);
708}
709
710int qxl_plane_prepare_fb(struct drm_plane *plane,
711 struct drm_plane_state *new_state)
712{
713 struct drm_gem_object *obj;
714 struct qxl_bo *user_bo;
715 int ret;
716
717 if (!new_state->fb)
718 return 0;
719
720 obj = to_qxl_framebuffer(new_state->fb)->obj;
721 user_bo = gem_to_qxl_bo(obj);
722
723 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
724 if (ret)
725 return ret;
726
727 return 0;
728}
729
730static void qxl_plane_cleanup_fb(struct drm_plane *plane,
731 struct drm_plane_state *old_state)
732{
733 struct drm_gem_object *obj;
734 struct qxl_bo *user_bo;
735
736 if (!plane->state->fb) {
737 /* we never executed prepare_fb, so there's nothing to
738 * unpin.
739 */
740 return;
741 }
742
743 obj = to_qxl_framebuffer(plane->state->fb)->obj;
744 user_bo = gem_to_qxl_bo(obj);
745 qxl_bo_unpin(user_bo);
746}
747
748static const uint32_t qxl_cursor_plane_formats[] = {
749 DRM_FORMAT_ARGB8888,
750};
751
752static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
753 .atomic_check = qxl_plane_atomic_check,
754 .atomic_update = qxl_cursor_atomic_update,
755 .atomic_disable = qxl_cursor_atomic_disable,
756 .prepare_fb = qxl_plane_prepare_fb,
757 .cleanup_fb = qxl_plane_cleanup_fb,
758};
759
760static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
761 .update_plane = drm_plane_helper_update,
762 .disable_plane = drm_plane_helper_disable,
763 .destroy = drm_primary_helper_destroy,
764};
765
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300766static const uint32_t qxl_primary_plane_formats[] = {
767 DRM_FORMAT_XRGB8888,
768 DRM_FORMAT_ARGB8888,
769};
770
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300771static const struct drm_plane_helper_funcs primary_helper_funcs = {
772 .atomic_check = qxl_primary_atomic_check,
773 .atomic_update = qxl_primary_atomic_update,
774 .atomic_disable = qxl_primary_atomic_disable,
775 .prepare_fb = qxl_plane_prepare_fb,
776 .cleanup_fb = qxl_plane_cleanup_fb,
777};
778
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300779static const struct drm_plane_funcs qxl_primary_plane_funcs = {
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300780 .update_plane = drm_plane_helper_update,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300781 .disable_plane = drm_primary_helper_disable,
782 .destroy = drm_primary_helper_destroy,
783};
784
785static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
786 unsigned int possible_crtcs,
787 enum drm_plane_type type)
788{
789 const struct drm_plane_helper_funcs *helper_funcs = NULL;
790 struct drm_plane *plane;
791 const struct drm_plane_funcs *funcs;
792 const uint32_t *formats;
793 int num_formats;
794 int err;
795
796 if (type == DRM_PLANE_TYPE_PRIMARY) {
797 funcs = &qxl_primary_plane_funcs;
798 formats = qxl_primary_plane_formats;
799 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300800 helper_funcs = &primary_helper_funcs;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300801 } else if (type == DRM_PLANE_TYPE_CURSOR) {
802 funcs = &qxl_cursor_plane_funcs;
803 formats = qxl_cursor_plane_formats;
804 helper_funcs = &qxl_cursor_helper_funcs;
805 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300806 } else {
807 return ERR_PTR(-EINVAL);
808 }
809
810 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
811 if (!plane)
812 return ERR_PTR(-ENOMEM);
813
814 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
815 funcs, formats, num_formats,
816 type, NULL);
817 if (err)
818 goto free_plane;
819
820 drm_plane_helper_add(plane, helper_funcs);
821
822 return plane;
823
824free_plane:
825 kfree(plane);
826 return ERR_PTR(-EINVAL);
827}
828
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100829static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000830{
831 struct qxl_crtc *qxl_crtc;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300832 struct drm_plane *primary, *cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300833 struct qxl_device *qdev = dev->dev_private;
834 int r;
Dave Airlief64122c2013-02-25 14:47:55 +1000835
836 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
837 if (!qxl_crtc)
838 return -ENOMEM;
839
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300840 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
841 if (IS_ERR(primary)) {
842 r = -ENOMEM;
843 goto free_mem;
844 }
845
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300846 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
847 if (IS_ERR(cursor)) {
848 r = -ENOMEM;
849 goto clean_primary;
850 }
851
852 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300853 &qxl_crtc_funcs, NULL);
854 if (r)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300855 goto clean_cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300856
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100857 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000858 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
859 return 0;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300860
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300861clean_cursor:
862 drm_plane_cleanup(cursor);
863 kfree(cursor);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300864clean_primary:
865 drm_plane_cleanup(primary);
866 kfree(primary);
867free_mem:
868 kfree(qxl_crtc);
869 return r;
Dave Airlief64122c2013-02-25 14:47:55 +1000870}
871
872static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
873{
874 DRM_DEBUG("\n");
875}
876
Dave Airlief64122c2013-02-25 14:47:55 +1000877static void qxl_enc_prepare(struct drm_encoder *encoder)
878{
879 DRM_DEBUG("\n");
880}
881
882static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
883 struct drm_encoder *encoder)
884{
885 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100886 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000887 struct qxl_head *head;
888 struct drm_display_mode *mode;
889
890 BUG_ON(!encoder);
891 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100892 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000893 if (!qdev->monitors_config ||
894 qdev->monitors_config->max_allowed <= i) {
895 DRM_ERROR(
896 "head number too large or missing monitors config: %p, %d",
897 qdev->monitors_config,
898 qdev->monitors_config ?
899 qdev->monitors_config->max_allowed : -1);
900 return;
901 }
902 if (!encoder->crtc) {
903 DRM_ERROR("missing crtc on encoder %p\n", encoder);
904 return;
905 }
906 if (i != 0)
907 DRM_DEBUG("missing for multiple monitors: no head holes\n");
908 head = &qdev->monitors_config->heads[i];
909 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000910 if (encoder->crtc->enabled) {
911 mode = &encoder->crtc->mode;
912 head->width = mode->hdisplay;
913 head->height = mode->vdisplay;
914 head->x = encoder->crtc->x;
915 head->y = encoder->crtc->y;
916 if (qdev->monitors_config->count < i + 1)
917 qdev->monitors_config->count = i + 1;
918 } else {
919 head->width = 0;
920 head->height = 0;
921 head->x = 0;
922 head->y = 0;
923 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100924 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
925 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000926 head->flags = 0;
927 /* TODO - somewhere else to call this for multiple monitors
928 * (config_commit?) */
929 qxl_send_monitors_config(qdev);
930}
931
932static void qxl_enc_commit(struct drm_encoder *encoder)
933{
934 struct qxl_device *qdev = encoder->dev->dev_private;
935
936 qxl_write_monitors_config_for_encoder(qdev, encoder);
937 DRM_DEBUG("\n");
938}
939
940static void qxl_enc_mode_set(struct drm_encoder *encoder,
941 struct drm_display_mode *mode,
942 struct drm_display_mode *adjusted_mode)
943{
944 DRM_DEBUG("\n");
945}
946
947static int qxl_conn_get_modes(struct drm_connector *connector)
948{
949 int ret = 0;
950 struct qxl_device *qdev = connector->dev->dev_private;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200951 unsigned pwidth = 1024;
952 unsigned pheight = 768;
Dave Airlief64122c2013-02-25 14:47:55 +1000953
954 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
955 /* TODO: what should we do here? only show the configured modes for the
956 * device, or allow the full list, or both? */
957 if (qdev->monitors_config && qdev->monitors_config->count) {
Marc-André Lureaub0807422013-10-18 16:11:31 +0200958 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000959 if (ret < 0)
960 return ret;
961 }
Marc-André Lureaub0807422013-10-18 16:11:31 +0200962 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000963 return ret;
964}
965
966static int qxl_conn_mode_valid(struct drm_connector *connector,
967 struct drm_display_mode *mode)
968{
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500969 struct drm_device *ddev = connector->dev;
970 struct qxl_device *qdev = ddev->dev_private;
971 int i;
972
Dave Airlief64122c2013-02-25 14:47:55 +1000973 /* TODO: is this called for user defined modes? (xrandr --add-mode)
974 * TODO: check that the mode fits in the framebuffer */
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500975
976 if(qdev->monitors_config_width == mode->hdisplay &&
977 qdev->monitors_config_height == mode->vdisplay)
978 return MODE_OK;
979
980 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
981 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
982 return MODE_OK;
983 }
984 return MODE_BAD;
Dave Airlief64122c2013-02-25 14:47:55 +1000985}
986
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000987static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +1000988{
989 struct qxl_output *qxl_output =
990 drm_connector_to_qxl_output(connector);
991
992 DRM_DEBUG("\n");
993 return &qxl_output->enc;
994}
995
996
997static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
998 .dpms = qxl_enc_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +1000999 .prepare = qxl_enc_prepare,
1000 .mode_set = qxl_enc_mode_set,
1001 .commit = qxl_enc_commit,
1002};
1003
1004static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1005 .get_modes = qxl_conn_get_modes,
1006 .mode_valid = qxl_conn_mode_valid,
1007 .best_encoder = qxl_best_encoder,
1008};
1009
Dave Airlief64122c2013-02-25 14:47:55 +10001010static enum drm_connector_status qxl_conn_detect(
1011 struct drm_connector *connector,
1012 bool force)
1013{
1014 struct qxl_output *output =
1015 drm_connector_to_qxl_output(connector);
1016 struct drm_device *ddev = connector->dev;
1017 struct qxl_device *qdev = ddev->dev_private;
Dave Airlie69e5d3f2015-09-14 10:28:34 +10001018 bool connected = false;
Dave Airlief64122c2013-02-25 14:47:55 +10001019
1020 /* The first monitor is always connected */
Dave Airlie69e5d3f2015-09-14 10:28:34 +10001021 if (!qdev->client_monitors_config) {
1022 if (output->index == 0)
1023 connected = true;
1024 } else
1025 connected = qdev->client_monitors_config->count > output->index &&
1026 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
Dave Airlief64122c2013-02-25 14:47:55 +10001027
Marc-André Lureau5cab51c2013-10-18 16:11:33 +02001028 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1029 if (!connected)
1030 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
1031
Dave Airlief64122c2013-02-25 14:47:55 +10001032 return connected ? connector_status_connected
1033 : connector_status_disconnected;
1034}
1035
1036static int qxl_conn_set_property(struct drm_connector *connector,
1037 struct drm_property *property,
1038 uint64_t value)
1039{
1040 DRM_DEBUG("\n");
1041 return 0;
1042}
1043
1044static void qxl_conn_destroy(struct drm_connector *connector)
1045{
1046 struct qxl_output *qxl_output =
1047 drm_connector_to_qxl_output(connector);
1048
Thomas Wood34ea3d32014-05-29 16:57:41 +01001049 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001050 drm_connector_cleanup(connector);
1051 kfree(qxl_output);
1052}
1053
1054static const struct drm_connector_funcs qxl_connector_funcs = {
1055 .dpms = drm_helper_connector_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001056 .detect = qxl_conn_detect,
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001057 .fill_modes = drm_helper_probe_single_connector_modes,
Dave Airlief64122c2013-02-25 14:47:55 +10001058 .set_property = qxl_conn_set_property,
1059 .destroy = qxl_conn_destroy,
1060};
1061
1062static void qxl_enc_destroy(struct drm_encoder *encoder)
1063{
1064 drm_encoder_cleanup(encoder);
1065}
1066
1067static const struct drm_encoder_funcs qxl_enc_funcs = {
1068 .destroy = qxl_enc_destroy,
1069};
1070
Dave Airlie4695b032013-10-11 11:05:00 +10001071static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1072{
1073 if (qdev->hotplug_mode_update_property)
1074 return 0;
1075
1076 qdev->hotplug_mode_update_property =
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001077 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlie4695b032013-10-11 11:05:00 +10001078 "hotplug_mode_update", 0, 1);
1079
1080 return 0;
1081}
1082
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001083static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +10001084{
Dave Airlie4695b032013-10-11 11:05:00 +10001085 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +10001086 struct qxl_output *qxl_output;
1087 struct drm_connector *connector;
1088 struct drm_encoder *encoder;
1089
1090 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1091 if (!qxl_output)
1092 return -ENOMEM;
1093
1094 qxl_output->index = num_output;
1095
1096 connector = &qxl_output->base;
1097 encoder = &qxl_output->enc;
1098 drm_connector_init(dev, &qxl_output->base,
1099 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1100
1101 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001102 DRM_MODE_ENCODER_VIRTUAL, NULL);
Dave Airlief64122c2013-02-25 14:47:55 +10001103
Dave Airlie5ff91e42013-07-05 10:20:33 +10001104 /* we get HPD via client monitors config */
1105 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +10001106 encoder->possible_crtcs = 1 << num_output;
1107 drm_mode_connector_attach_encoder(&qxl_output->base,
1108 &qxl_output->enc);
1109 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1110 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1111
Dave Airlie4695b032013-10-11 11:05:00 +10001112 drm_object_attach_property(&connector->base,
1113 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +10001114 drm_object_attach_property(&connector->base,
1115 dev->mode_config.suggested_x_property, 0);
1116 drm_object_attach_property(&connector->base,
1117 dev->mode_config.suggested_y_property, 0);
Dave Airlief64122c2013-02-25 14:47:55 +10001118 return 0;
1119}
1120
1121static struct drm_framebuffer *
1122qxl_user_framebuffer_create(struct drm_device *dev,
1123 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02001124 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief64122c2013-02-25 14:47:55 +10001125{
1126 struct drm_gem_object *obj;
1127 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +10001128 int ret;
1129
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001130 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1131 if (!obj)
1132 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +10001133
1134 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1135 if (qxl_fb == NULL)
1136 return NULL;
1137
Noralf Trønnes6819c3c2016-04-28 17:18:36 +02001138 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +10001139 if (ret) {
1140 kfree(qxl_fb);
1141 drm_gem_object_unreference_unlocked(obj);
1142 return NULL;
1143 }
1144
Dave Airlief64122c2013-02-25 14:47:55 +10001145 return &qxl_fb->base;
1146}
1147
1148static const struct drm_mode_config_funcs qxl_mode_funcs = {
1149 .fb_create = qxl_user_framebuffer_create,
1150};
1151
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001152int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001153{
Dave Airlief64122c2013-02-25 14:47:55 +10001154 int ret;
1155 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001156 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001157 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001158 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001159
Dave Airlief64122c2013-02-25 14:47:55 +10001160 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1161 QXL_GEM_DOMAIN_VRAM,
1162 false, false, NULL, &gobj);
1163 if (ret) {
1164 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1165 return -ENOMEM;
1166 }
1167 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001168
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001169 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001170 if (ret)
1171 return ret;
1172
Dave Airlief64122c2013-02-25 14:47:55 +10001173 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001174
Dave Airlief64122c2013-02-25 14:47:55 +10001175 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1176 qdev->ram_header->monitors_config =
1177 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1178
1179 memset(qdev->monitors_config, 0, monitors_config_size);
1180 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001181 return 0;
1182}
1183
1184int qxl_destroy_monitors_object(struct qxl_device *qdev)
1185{
1186 int ret;
1187
1188 qdev->monitors_config = NULL;
1189 qdev->ram_header->monitors_config = 0;
1190
1191 qxl_bo_kunmap(qdev->monitors_config_bo);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001192 ret = qxl_bo_unpin(qdev->monitors_config_bo);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001193 if (ret)
1194 return ret;
1195
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001196 qxl_bo_unref(&qdev->monitors_config_bo);
1197 return 0;
1198}
1199
1200int qxl_modeset_init(struct qxl_device *qdev)
1201{
1202 int i;
1203 int ret;
1204
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001205 drm_mode_config_init(&qdev->ddev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001206
1207 ret = qxl_create_monitors_object(qdev);
1208 if (ret)
1209 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001210
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001211 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
Dave Airlief64122c2013-02-25 14:47:55 +10001212
1213 /* modes will be validated against the framebuffer size */
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -03001214 qdev->ddev.mode_config.min_width = 0;
1215 qdev->ddev.mode_config.min_height = 0;
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001216 qdev->ddev.mode_config.max_width = 8192;
1217 qdev->ddev.mode_config.max_height = 8192;
Dave Airlief64122c2013-02-25 14:47:55 +10001218
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001219 qdev->ddev.mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001220
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001221 drm_mode_create_suggested_offset_properties(&qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001222 qxl_mode_create_hotplug_mode_update_property(qdev);
1223
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001224 for (i = 0 ; i < qxl_num_crtc; ++i) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001225 qdev_crtc_init(&qdev->ddev, i);
1226 qdev_output_init(&qdev->ddev, i);
Dave Airlief64122c2013-02-25 14:47:55 +10001227 }
1228
1229 qdev->mode_info.mode_config_initialized = true;
1230
1231 /* primary surface must be created by this point, to allow
1232 * issuing command queue commands and having them read by
1233 * spice server. */
1234 qxl_fbdev_init(qdev);
1235 return 0;
1236}
1237
1238void qxl_modeset_fini(struct qxl_device *qdev)
1239{
1240 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001241
1242 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001243 if (qdev->mode_info.mode_config_initialized) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001244 drm_mode_config_cleanup(&qdev->ddev);
Dave Airlief64122c2013-02-25 14:47:55 +10001245 qdev->mode_info.mode_config_initialized = false;
1246 }
1247}