blob: d5a00b6a07ea5a5ae3a76a5d39139b9e70c29625 [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,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300328 .reset = drm_atomic_helper_crtc_reset,
329 .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
330 .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
Dave Airlief64122c2013-02-25 14:47:55 +1000331};
332
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200333void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
Dave Airlief64122c2013-02-25 14:47:55 +1000334{
335 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
336
Markus Elfringdc3583c2016-07-22 14:14:54 +0200337 drm_gem_object_unreference_unlocked(qxl_fb->obj);
Dave Airlief64122c2013-02-25 14:47:55 +1000338 drm_framebuffer_cleanup(fb);
339 kfree(qxl_fb);
340}
341
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000342static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
343 struct drm_file *file_priv,
344 unsigned flags, unsigned color,
345 struct drm_clip_rect *clips,
346 unsigned num_clips)
Dave Airlief64122c2013-02-25 14:47:55 +1000347{
348 /* TODO: vmwgfx where this was cribbed from had locking. Why? */
349 struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
350 struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
351 struct drm_clip_rect norect;
352 struct qxl_bo *qobj;
353 int inc = 1;
354
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200355 drm_modeset_lock_all(fb->dev);
356
Dave Airlief64122c2013-02-25 14:47:55 +1000357 qobj = gem_to_qxl_bo(qxl_fb->obj);
Dave Airlieb2b44652013-05-13 12:48:40 +1000358 /* if we aren't primary surface ignore this */
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200359 if (!qobj->is_primary) {
360 drm_modeset_unlock_all(fb->dev);
Dave Airlieb2b44652013-05-13 12:48:40 +1000361 return 0;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200362 }
Dave Airlieb2b44652013-05-13 12:48:40 +1000363
Dave Airlief64122c2013-02-25 14:47:55 +1000364 if (!num_clips) {
365 num_clips = 1;
366 clips = &norect;
367 norect.x1 = norect.y1 = 0;
368 norect.x2 = fb->width;
369 norect.y2 = fb->height;
370 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
371 num_clips /= 2;
372 inc = 2; /* skip source rects */
373 }
374
375 qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
376 clips, num_clips, inc);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200377
378 drm_modeset_unlock_all(fb->dev);
379
Dave Airlief64122c2013-02-25 14:47:55 +1000380 return 0;
381}
382
383static const struct drm_framebuffer_funcs qxl_fb_funcs = {
384 .destroy = qxl_user_framebuffer_destroy,
385 .dirty = qxl_framebuffer_surface_dirty,
386/* TODO?
387 * .create_handle = qxl_user_framebuffer_create_handle, */
388};
389
390int
391qxl_framebuffer_init(struct drm_device *dev,
392 struct qxl_framebuffer *qfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200393 const struct drm_mode_fb_cmd2 *mode_cmd,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200394 struct drm_gem_object *obj,
395 const struct drm_framebuffer_funcs *funcs)
Dave Airlief64122c2013-02-25 14:47:55 +1000396{
397 int ret;
398
399 qfb->obj = obj;
Ville Syrjälä53609432016-11-18 21:52:51 +0200400 drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200401 ret = drm_framebuffer_init(dev, &qfb->base, funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000402 if (ret) {
403 qfb->obj = NULL;
404 return ret;
405 }
Dave Airlief64122c2013-02-25 14:47:55 +1000406 return 0;
407}
408
409static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
410{
411}
412
413static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
414 const struct drm_display_mode *mode,
415 struct drm_display_mode *adjusted_mode)
416{
417 struct drm_device *dev = crtc->dev;
418 struct qxl_device *qdev = dev->dev_private;
419
420 qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
421 __func__,
422 mode->hdisplay, mode->vdisplay,
423 adjusted_mode->hdisplay,
424 adjusted_mode->vdisplay);
425 return true;
426}
427
Christophe Fergeaue4a76442016-11-08 10:12:03 +0100428static void
Dave Airlief64122c2013-02-25 14:47:55 +1000429qxl_send_monitors_config(struct qxl_device *qdev)
430{
431 int i;
432
433 BUG_ON(!qdev->ram_header->monitors_config);
434
435 if (qdev->monitors_config->count == 0) {
436 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
437 return;
438 }
439 for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
440 struct qxl_head *head = &qdev->monitors_config->heads[i];
441
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100442 if (head->y > 8192 || head->x > 8192 ||
Dave Airlief64122c2013-02-25 14:47:55 +1000443 head->width > 8192 || head->height > 8192) {
444 DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
445 i, head->width, head->height,
446 head->x, head->y);
447 return;
448 }
449 }
450 qxl_io_monitors_config(qdev);
451}
452
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100453static void qxl_monitors_config_set(struct qxl_device *qdev,
454 int index,
455 unsigned x, unsigned y,
456 unsigned width, unsigned height,
457 unsigned surf_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000458{
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100459 DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
460 qdev->monitors_config->heads[index].x = x;
461 qdev->monitors_config->heads[index].y = y;
462 qdev->monitors_config->heads[index].width = width;
463 qdev->monitors_config->heads[index].height = height;
464 qdev->monitors_config->heads[index].surface_id = surf_id;
465
Dave Airlief64122c2013-02-25 14:47:55 +1000466}
467
Dave Airlief64122c2013-02-25 14:47:55 +1000468static void qxl_crtc_prepare(struct drm_crtc *crtc)
469{
470 DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
471 crtc->mode.hdisplay, crtc->mode.vdisplay,
472 crtc->x, crtc->y, crtc->enabled);
473}
474
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300475void qxl_mode_set_nofb(struct drm_crtc *crtc)
476{
477 struct qxl_device *qdev = crtc->dev->dev_private;
478 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
479 struct drm_display_mode *mode = &crtc->mode;
480
481 DRM_DEBUG("Mode set (%d,%d)\n",
482 mode->hdisplay, mode->vdisplay);
483
484 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
485 mode->hdisplay, mode->vdisplay, 0);
486
487}
488
Dave Airlief64122c2013-02-25 14:47:55 +1000489static void qxl_crtc_commit(struct drm_crtc *crtc)
490{
491 DRM_DEBUG("\n");
492}
493
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100494static void qxl_crtc_disable(struct drm_crtc *crtc)
495{
496 struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
Gabriel Krisman Bertazi37235452017-02-27 17:43:22 -0300497 struct qxl_device *qdev = crtc->dev->dev_private;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100498
499 qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
500
501 qxl_send_monitors_config(qdev);
502}
503
Dave Airlief64122c2013-02-25 14:47:55 +1000504static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
505 .dpms = qxl_crtc_dpms,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100506 .disable = qxl_crtc_disable,
Dave Airlief64122c2013-02-25 14:47:55 +1000507 .mode_fixup = qxl_crtc_mode_fixup,
Gabriel Krisman Bertazi3538e802017-02-27 17:43:21 -0300508 .mode_set = drm_helper_crtc_mode_set,
509 .mode_set_nofb = qxl_mode_set_nofb,
Dave Airlief64122c2013-02-25 14:47:55 +1000510 .prepare = qxl_crtc_prepare,
511 .commit = qxl_crtc_commit,
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300512 .atomic_flush = qxl_crtc_atomic_flush,
Dave Airlief64122c2013-02-25 14:47:55 +1000513};
514
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300515int qxl_primary_atomic_check(struct drm_plane *plane,
516 struct drm_plane_state *state)
517{
518 struct qxl_device *qdev = plane->dev->dev_private;
519 struct qxl_framebuffer *qfb;
520 struct qxl_bo *bo;
521
522 if (!state->crtc || !state->fb)
523 return 0;
524
525 qfb = to_qxl_framebuffer(state->fb);
526 bo = gem_to_qxl_bo(qfb->obj);
527
528 if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
529 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
530 return -EINVAL;
531 }
532
533 return 0;
534}
535
536static void qxl_primary_atomic_update(struct drm_plane *plane,
537 struct drm_plane_state *old_state)
538{
539 struct qxl_device *qdev = plane->dev->dev_private;
540 struct qxl_framebuffer *qfb =
541 to_qxl_framebuffer(plane->state->fb);
542 struct qxl_framebuffer *qfb_old;
543 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
544 struct qxl_bo *bo_old;
545 struct drm_clip_rect norect = {
546 .x1 = 0,
547 .y1 = 0,
548 .x2 = qfb->base.width,
549 .y2 = qfb->base.height
550 };
551
552 if (!old_state->fb) {
553 qxl_io_log(qdev,
554 "create primary fb: %dx%d,%d,%d\n",
555 bo->surf.width, bo->surf.height,
556 bo->surf.stride, bo->surf.format);
557
558 qxl_io_create_primary(qdev, 0, bo);
559 bo->is_primary = true;
560 return;
561
562 } else {
563 qfb_old = to_qxl_framebuffer(old_state->fb);
564 bo_old = gem_to_qxl_bo(qfb_old->obj);
565 bo_old->is_primary = false;
566 }
567
568 bo->is_primary = true;
569 qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
570}
571
572static void qxl_primary_atomic_disable(struct drm_plane *plane,
573 struct drm_plane_state *old_state)
574{
575 struct qxl_device *qdev = plane->dev->dev_private;
576
577 if (old_state->fb)
578 { struct qxl_framebuffer *qfb =
579 to_qxl_framebuffer(old_state->fb);
580 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
581
582 qxl_io_destroy_primary(qdev);
583 bo->is_primary = false;
584 }
585}
586
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300587int qxl_plane_atomic_check(struct drm_plane *plane,
588 struct drm_plane_state *state)
589{
590 return 0;
591}
592
593static void qxl_cursor_atomic_update(struct drm_plane *plane,
594 struct drm_plane_state *old_state)
595{
596 struct drm_device *dev = plane->dev;
597 struct qxl_device *qdev = dev->dev_private;
598 struct drm_framebuffer *fb = plane->state->fb;
599 struct qxl_release *release;
600 struct qxl_cursor_cmd *cmd;
601 struct qxl_cursor *cursor;
602 struct drm_gem_object *obj;
603 struct qxl_bo *cursor_bo, *user_bo = NULL;
604 int ret;
605 void *user_ptr;
606 int size = 64*64*4;
607
608 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
609 QXL_RELEASE_CURSOR_CMD,
610 &release, NULL);
611
612 cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
613
614 if (fb != old_state->fb) {
615 obj = to_qxl_framebuffer(fb)->obj;
616 user_bo = gem_to_qxl_bo(obj);
617
618 /* pinning is done in the prepare/cleanup framevbuffer */
619 ret = qxl_bo_kmap(user_bo, &user_ptr);
620 if (ret)
621 goto out_free_release;
622
623 ret = qxl_alloc_bo_reserved(qdev, release,
624 sizeof(struct qxl_cursor) + size,
625 &cursor_bo);
626 if (ret)
627 goto out_kunmap;
628
629 ret = qxl_release_reserve_list(release, true);
630 if (ret)
631 goto out_free_bo;
632
633 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
634 if (ret)
635 goto out_backoff;
636
637 cursor->header.unique = 0;
638 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
639 cursor->header.width = 64;
640 cursor->header.height = 64;
641 cursor->header.hot_spot_x = fb->hot_x;
642 cursor->header.hot_spot_y = fb->hot_y;
643 cursor->data_size = size;
644 cursor->chunk.next_chunk = 0;
645 cursor->chunk.prev_chunk = 0;
646 cursor->chunk.data_size = size;
647 memcpy(cursor->chunk.data, user_ptr, size);
648 qxl_bo_kunmap(cursor_bo);
649 qxl_bo_kunmap(user_bo);
650
651 cmd->u.set.visible = 1;
652 cmd->u.set.shape = qxl_bo_physical_address(qdev,
653 cursor_bo, 0);
654 cmd->type = QXL_CURSOR_SET;
655 } else {
656
657 ret = qxl_release_reserve_list(release, true);
658 if (ret)
659 goto out_free_release;
660
661 cmd->type = QXL_CURSOR_MOVE;
662 }
663
664 cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
665 cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
666
667 qxl_release_unmap(qdev, release, &cmd->release_info);
668 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
669 qxl_release_fence_buffer_objects(release);
670
671 return;
672
673out_backoff:
674 qxl_release_backoff_reserve_list(release);
675out_free_bo:
676 qxl_bo_unref(&cursor_bo);
677out_kunmap:
678 qxl_bo_kunmap(user_bo);
679out_free_release:
680 qxl_release_free(qdev, release);
681 return;
682
683}
684
685void qxl_cursor_atomic_disable(struct drm_plane *plane,
686 struct drm_plane_state *old_state)
687{
688 struct qxl_device *qdev = plane->dev->dev_private;
689 struct qxl_release *release;
690 struct qxl_cursor_cmd *cmd;
691 int ret;
692
693 ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
694 QXL_RELEASE_CURSOR_CMD,
695 &release, NULL);
696 if (ret)
697 return;
698
699 ret = qxl_release_reserve_list(release, true);
700 if (ret) {
701 qxl_release_free(qdev, release);
702 return;
703 }
704
705 cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
706 cmd->type = QXL_CURSOR_HIDE;
707 qxl_release_unmap(qdev, release, &cmd->release_info);
708
709 qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
710 qxl_release_fence_buffer_objects(release);
711}
712
713int qxl_plane_prepare_fb(struct drm_plane *plane,
714 struct drm_plane_state *new_state)
715{
716 struct drm_gem_object *obj;
717 struct qxl_bo *user_bo;
718 int ret;
719
720 if (!new_state->fb)
721 return 0;
722
723 obj = to_qxl_framebuffer(new_state->fb)->obj;
724 user_bo = gem_to_qxl_bo(obj);
725
726 ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
727 if (ret)
728 return ret;
729
730 return 0;
731}
732
733static void qxl_plane_cleanup_fb(struct drm_plane *plane,
734 struct drm_plane_state *old_state)
735{
736 struct drm_gem_object *obj;
737 struct qxl_bo *user_bo;
738
739 if (!plane->state->fb) {
740 /* we never executed prepare_fb, so there's nothing to
741 * unpin.
742 */
743 return;
744 }
745
746 obj = to_qxl_framebuffer(plane->state->fb)->obj;
747 user_bo = gem_to_qxl_bo(obj);
748 qxl_bo_unpin(user_bo);
749}
750
751static const uint32_t qxl_cursor_plane_formats[] = {
752 DRM_FORMAT_ARGB8888,
753};
754
755static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
756 .atomic_check = qxl_plane_atomic_check,
757 .atomic_update = qxl_cursor_atomic_update,
758 .atomic_disable = qxl_cursor_atomic_disable,
759 .prepare_fb = qxl_plane_prepare_fb,
760 .cleanup_fb = qxl_plane_cleanup_fb,
761};
762
763static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
764 .update_plane = drm_plane_helper_update,
765 .disable_plane = drm_plane_helper_disable,
766 .destroy = drm_primary_helper_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300767 .reset = drm_atomic_helper_plane_reset,
768 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
769 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300770};
771
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300772static const uint32_t qxl_primary_plane_formats[] = {
773 DRM_FORMAT_XRGB8888,
774 DRM_FORMAT_ARGB8888,
775};
776
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300777static const struct drm_plane_helper_funcs primary_helper_funcs = {
778 .atomic_check = qxl_primary_atomic_check,
779 .atomic_update = qxl_primary_atomic_update,
780 .atomic_disable = qxl_primary_atomic_disable,
781 .prepare_fb = qxl_plane_prepare_fb,
782 .cleanup_fb = qxl_plane_cleanup_fb,
783};
784
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300785static const struct drm_plane_funcs qxl_primary_plane_funcs = {
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300786 .update_plane = drm_plane_helper_update,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300787 .disable_plane = drm_primary_helper_disable,
788 .destroy = drm_primary_helper_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -0300789 .reset = drm_atomic_helper_plane_reset,
790 .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
791 .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300792};
793
794static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
795 unsigned int possible_crtcs,
796 enum drm_plane_type type)
797{
798 const struct drm_plane_helper_funcs *helper_funcs = NULL;
799 struct drm_plane *plane;
800 const struct drm_plane_funcs *funcs;
801 const uint32_t *formats;
802 int num_formats;
803 int err;
804
805 if (type == DRM_PLANE_TYPE_PRIMARY) {
806 funcs = &qxl_primary_plane_funcs;
807 formats = qxl_primary_plane_formats;
808 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
Gabriel Krisman Bertazic2ff6632017-02-27 17:43:20 -0300809 helper_funcs = &primary_helper_funcs;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300810 } else if (type == DRM_PLANE_TYPE_CURSOR) {
811 funcs = &qxl_cursor_plane_funcs;
812 formats = qxl_cursor_plane_formats;
813 helper_funcs = &qxl_cursor_helper_funcs;
814 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300815 } else {
816 return ERR_PTR(-EINVAL);
817 }
818
819 plane = kzalloc(sizeof(*plane), GFP_KERNEL);
820 if (!plane)
821 return ERR_PTR(-ENOMEM);
822
823 err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
824 funcs, formats, num_formats,
825 type, NULL);
826 if (err)
827 goto free_plane;
828
829 drm_plane_helper_add(plane, helper_funcs);
830
831 return plane;
832
833free_plane:
834 kfree(plane);
835 return ERR_PTR(-EINVAL);
836}
837
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100838static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
Dave Airlief64122c2013-02-25 14:47:55 +1000839{
840 struct qxl_crtc *qxl_crtc;
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300841 struct drm_plane *primary, *cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300842 struct qxl_device *qdev = dev->dev_private;
843 int r;
Dave Airlief64122c2013-02-25 14:47:55 +1000844
845 qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
846 if (!qxl_crtc)
847 return -ENOMEM;
848
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300849 primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
850 if (IS_ERR(primary)) {
851 r = -ENOMEM;
852 goto free_mem;
853 }
854
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300855 cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
856 if (IS_ERR(cursor)) {
857 r = -ENOMEM;
858 goto clean_primary;
859 }
860
861 r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300862 &qxl_crtc_funcs, NULL);
863 if (r)
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300864 goto clean_cursor;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300865
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100866 qxl_crtc->index = crtc_id;
Dave Airlief64122c2013-02-25 14:47:55 +1000867 drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
868 return 0;
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300869
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -0300870clean_cursor:
871 drm_plane_cleanup(cursor);
872 kfree(cursor);
Gabriel Krisman Bertazid3e7e422017-02-27 17:43:18 -0300873clean_primary:
874 drm_plane_cleanup(primary);
875 kfree(primary);
876free_mem:
877 kfree(qxl_crtc);
878 return r;
Dave Airlief64122c2013-02-25 14:47:55 +1000879}
880
881static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
882{
883 DRM_DEBUG("\n");
884}
885
Dave Airlief64122c2013-02-25 14:47:55 +1000886static void qxl_enc_prepare(struct drm_encoder *encoder)
887{
888 DRM_DEBUG("\n");
889}
890
891static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
892 struct drm_encoder *encoder)
893{
894 int i;
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100895 struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
Dave Airlief64122c2013-02-25 14:47:55 +1000896 struct qxl_head *head;
897 struct drm_display_mode *mode;
898
899 BUG_ON(!encoder);
900 /* TODO: ugly, do better */
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100901 i = output->index;
Dave Airlief64122c2013-02-25 14:47:55 +1000902 if (!qdev->monitors_config ||
903 qdev->monitors_config->max_allowed <= i) {
904 DRM_ERROR(
905 "head number too large or missing monitors config: %p, %d",
906 qdev->monitors_config,
907 qdev->monitors_config ?
908 qdev->monitors_config->max_allowed : -1);
909 return;
910 }
911 if (!encoder->crtc) {
912 DRM_ERROR("missing crtc on encoder %p\n", encoder);
913 return;
914 }
915 if (i != 0)
916 DRM_DEBUG("missing for multiple monitors: no head holes\n");
917 head = &qdev->monitors_config->heads[i];
918 head->id = i;
Dave Airlief64122c2013-02-25 14:47:55 +1000919 if (encoder->crtc->enabled) {
920 mode = &encoder->crtc->mode;
921 head->width = mode->hdisplay;
922 head->height = mode->vdisplay;
923 head->x = encoder->crtc->x;
924 head->y = encoder->crtc->y;
925 if (qdev->monitors_config->count < i + 1)
926 qdev->monitors_config->count = i + 1;
927 } else {
928 head->width = 0;
929 head->height = 0;
930 head->x = 0;
931 head->y = 0;
932 }
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100933 DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
934 i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
Dave Airlief64122c2013-02-25 14:47:55 +1000935 head->flags = 0;
936 /* TODO - somewhere else to call this for multiple monitors
937 * (config_commit?) */
938 qxl_send_monitors_config(qdev);
939}
940
941static void qxl_enc_commit(struct drm_encoder *encoder)
942{
943 struct qxl_device *qdev = encoder->dev->dev_private;
944
945 qxl_write_monitors_config_for_encoder(qdev, encoder);
946 DRM_DEBUG("\n");
947}
948
949static void qxl_enc_mode_set(struct drm_encoder *encoder,
950 struct drm_display_mode *mode,
951 struct drm_display_mode *adjusted_mode)
952{
953 DRM_DEBUG("\n");
954}
955
956static int qxl_conn_get_modes(struct drm_connector *connector)
957{
958 int ret = 0;
959 struct qxl_device *qdev = connector->dev->dev_private;
Marc-André Lureaub0807422013-10-18 16:11:31 +0200960 unsigned pwidth = 1024;
961 unsigned pheight = 768;
Dave Airlief64122c2013-02-25 14:47:55 +1000962
963 DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
964 /* TODO: what should we do here? only show the configured modes for the
965 * device, or allow the full list, or both? */
966 if (qdev->monitors_config && qdev->monitors_config->count) {
Marc-André Lureaub0807422013-10-18 16:11:31 +0200967 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000968 if (ret < 0)
969 return ret;
970 }
Marc-André Lureaub0807422013-10-18 16:11:31 +0200971 ret += qxl_add_common_modes(connector, pwidth, pheight);
Dave Airlief64122c2013-02-25 14:47:55 +1000972 return ret;
973}
974
975static int qxl_conn_mode_valid(struct drm_connector *connector,
976 struct drm_display_mode *mode)
977{
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500978 struct drm_device *ddev = connector->dev;
979 struct qxl_device *qdev = ddev->dev_private;
980 int i;
981
Dave Airlief64122c2013-02-25 14:47:55 +1000982 /* TODO: is this called for user defined modes? (xrandr --add-mode)
983 * TODO: check that the mode fits in the framebuffer */
Jonathon Jongsmabd3e1c72015-08-20 14:04:32 -0500984
985 if(qdev->monitors_config_width == mode->hdisplay &&
986 qdev->monitors_config_height == mode->vdisplay)
987 return MODE_OK;
988
989 for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
990 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
991 return MODE_OK;
992 }
993 return MODE_BAD;
Dave Airlief64122c2013-02-25 14:47:55 +1000994}
995
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000996static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
Dave Airlief64122c2013-02-25 14:47:55 +1000997{
998 struct qxl_output *qxl_output =
999 drm_connector_to_qxl_output(connector);
1000
1001 DRM_DEBUG("\n");
1002 return &qxl_output->enc;
1003}
1004
1005
1006static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1007 .dpms = qxl_enc_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001008 .prepare = qxl_enc_prepare,
1009 .mode_set = qxl_enc_mode_set,
1010 .commit = qxl_enc_commit,
1011};
1012
1013static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1014 .get_modes = qxl_conn_get_modes,
1015 .mode_valid = qxl_conn_mode_valid,
1016 .best_encoder = qxl_best_encoder,
1017};
1018
Dave Airlief64122c2013-02-25 14:47:55 +10001019static enum drm_connector_status qxl_conn_detect(
1020 struct drm_connector *connector,
1021 bool force)
1022{
1023 struct qxl_output *output =
1024 drm_connector_to_qxl_output(connector);
1025 struct drm_device *ddev = connector->dev;
1026 struct qxl_device *qdev = ddev->dev_private;
Dave Airlie69e5d3f2015-09-14 10:28:34 +10001027 bool connected = false;
Dave Airlief64122c2013-02-25 14:47:55 +10001028
1029 /* The first monitor is always connected */
Dave Airlie69e5d3f2015-09-14 10:28:34 +10001030 if (!qdev->client_monitors_config) {
1031 if (output->index == 0)
1032 connected = true;
1033 } else
1034 connected = qdev->client_monitors_config->count > output->index &&
1035 qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
Dave Airlief64122c2013-02-25 14:47:55 +10001036
Marc-André Lureau5cab51c2013-10-18 16:11:33 +02001037 DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1038 if (!connected)
1039 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
1040
Dave Airlief64122c2013-02-25 14:47:55 +10001041 return connected ? connector_status_connected
1042 : connector_status_disconnected;
1043}
1044
1045static int qxl_conn_set_property(struct drm_connector *connector,
1046 struct drm_property *property,
1047 uint64_t value)
1048{
1049 DRM_DEBUG("\n");
1050 return 0;
1051}
1052
1053static void qxl_conn_destroy(struct drm_connector *connector)
1054{
1055 struct qxl_output *qxl_output =
1056 drm_connector_to_qxl_output(connector);
1057
Thomas Wood34ea3d32014-05-29 16:57:41 +01001058 drm_connector_unregister(connector);
Dave Airlief64122c2013-02-25 14:47:55 +10001059 drm_connector_cleanup(connector);
1060 kfree(qxl_output);
1061}
1062
1063static const struct drm_connector_funcs qxl_connector_funcs = {
1064 .dpms = drm_helper_connector_dpms,
Dave Airlief64122c2013-02-25 14:47:55 +10001065 .detect = qxl_conn_detect,
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001066 .fill_modes = drm_helper_probe_single_connector_modes,
Dave Airlief64122c2013-02-25 14:47:55 +10001067 .set_property = qxl_conn_set_property,
1068 .destroy = qxl_conn_destroy,
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -03001069 .reset = drm_atomic_helper_connector_reset,
1070 .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1071 .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
Dave Airlief64122c2013-02-25 14:47:55 +10001072};
1073
1074static void qxl_enc_destroy(struct drm_encoder *encoder)
1075{
1076 drm_encoder_cleanup(encoder);
1077}
1078
1079static const struct drm_encoder_funcs qxl_enc_funcs = {
1080 .destroy = qxl_enc_destroy,
1081};
1082
Dave Airlie4695b032013-10-11 11:05:00 +10001083static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1084{
1085 if (qdev->hotplug_mode_update_property)
1086 return 0;
1087
1088 qdev->hotplug_mode_update_property =
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001089 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlie4695b032013-10-11 11:05:00 +10001090 "hotplug_mode_update", 0, 1);
1091
1092 return 0;
1093}
1094
Dave Airlie6d01f1f2013-04-16 13:24:25 +10001095static int qdev_output_init(struct drm_device *dev, int num_output)
Dave Airlief64122c2013-02-25 14:47:55 +10001096{
Dave Airlie4695b032013-10-11 11:05:00 +10001097 struct qxl_device *qdev = dev->dev_private;
Dave Airlief64122c2013-02-25 14:47:55 +10001098 struct qxl_output *qxl_output;
1099 struct drm_connector *connector;
1100 struct drm_encoder *encoder;
1101
1102 qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1103 if (!qxl_output)
1104 return -ENOMEM;
1105
1106 qxl_output->index = num_output;
1107
1108 connector = &qxl_output->base;
1109 encoder = &qxl_output->enc;
1110 drm_connector_init(dev, &qxl_output->base,
1111 &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1112
1113 drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +02001114 DRM_MODE_ENCODER_VIRTUAL, NULL);
Dave Airlief64122c2013-02-25 14:47:55 +10001115
Dave Airlie5ff91e42013-07-05 10:20:33 +10001116 /* we get HPD via client monitors config */
1117 connector->polled = DRM_CONNECTOR_POLL_HPD;
Dave Airlief64122c2013-02-25 14:47:55 +10001118 encoder->possible_crtcs = 1 << num_output;
1119 drm_mode_connector_attach_encoder(&qxl_output->base,
1120 &qxl_output->enc);
1121 drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1122 drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1123
Dave Airlie4695b032013-10-11 11:05:00 +10001124 drm_object_attach_property(&connector->base,
1125 qdev->hotplug_mode_update_property, 0);
Dave Airlie7dea0942014-10-28 11:28:44 +10001126 drm_object_attach_property(&connector->base,
1127 dev->mode_config.suggested_x_property, 0);
1128 drm_object_attach_property(&connector->base,
1129 dev->mode_config.suggested_y_property, 0);
Dave Airlief64122c2013-02-25 14:47:55 +10001130 return 0;
1131}
1132
1133static struct drm_framebuffer *
1134qxl_user_framebuffer_create(struct drm_device *dev,
1135 struct drm_file *file_priv,
Ville Syrjälä1eb83452015-11-11 19:11:29 +02001136 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief64122c2013-02-25 14:47:55 +10001137{
1138 struct drm_gem_object *obj;
1139 struct qxl_framebuffer *qxl_fb;
Dave Airlief64122c2013-02-25 14:47:55 +10001140 int ret;
1141
Chris Wilsona8ad0bd2016-05-09 11:04:54 +01001142 obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1143 if (!obj)
1144 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +10001145
1146 qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1147 if (qxl_fb == NULL)
1148 return NULL;
1149
Noralf Trønnes6819c3c2016-04-28 17:18:36 +02001150 ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +10001151 if (ret) {
1152 kfree(qxl_fb);
1153 drm_gem_object_unreference_unlocked(obj);
1154 return NULL;
1155 }
1156
Dave Airlief64122c2013-02-25 14:47:55 +10001157 return &qxl_fb->base;
1158}
1159
1160static const struct drm_mode_config_funcs qxl_mode_funcs = {
1161 .fb_create = qxl_user_framebuffer_create,
1162};
1163
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001164int qxl_create_monitors_object(struct qxl_device *qdev)
Dave Airlief64122c2013-02-25 14:47:55 +10001165{
Dave Airlief64122c2013-02-25 14:47:55 +10001166 int ret;
1167 struct drm_gem_object *gobj;
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001168 int max_allowed = qxl_num_crtc;
Dave Airlief64122c2013-02-25 14:47:55 +10001169 int monitors_config_size = sizeof(struct qxl_monitors_config) +
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001170 max_allowed * sizeof(struct qxl_head);
Dave Airlief64122c2013-02-25 14:47:55 +10001171
Dave Airlief64122c2013-02-25 14:47:55 +10001172 ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1173 QXL_GEM_DOMAIN_VRAM,
1174 false, false, NULL, &gobj);
1175 if (ret) {
1176 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1177 return -ENOMEM;
1178 }
1179 qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001180
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001181 ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001182 if (ret)
1183 return ret;
1184
Dave Airlief64122c2013-02-25 14:47:55 +10001185 qxl_bo_kmap(qdev->monitors_config_bo, NULL);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001186
Dave Airlief64122c2013-02-25 14:47:55 +10001187 qdev->monitors_config = qdev->monitors_config_bo->kptr;
1188 qdev->ram_header->monitors_config =
1189 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1190
1191 memset(qdev->monitors_config, 0, monitors_config_size);
1192 qdev->monitors_config->max_allowed = max_allowed;
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001193 return 0;
1194}
1195
1196int qxl_destroy_monitors_object(struct qxl_device *qdev)
1197{
1198 int ret;
1199
1200 qdev->monitors_config = NULL;
1201 qdev->ram_header->monitors_config = 0;
1202
1203 qxl_bo_kunmap(qdev->monitors_config_bo);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -03001204 ret = qxl_bo_unpin(qdev->monitors_config_bo);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001205 if (ret)
1206 return ret;
1207
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001208 qxl_bo_unref(&qdev->monitors_config_bo);
1209 return 0;
1210}
1211
1212int qxl_modeset_init(struct qxl_device *qdev)
1213{
1214 int i;
1215 int ret;
1216
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001217 drm_mode_config_init(&qdev->ddev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001218
1219 ret = qxl_create_monitors_object(qdev);
1220 if (ret)
1221 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +10001222
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001223 qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
Dave Airlief64122c2013-02-25 14:47:55 +10001224
1225 /* modes will be validated against the framebuffer size */
Gabriel Krisman Bertazi1277eed2017-02-27 17:43:19 -03001226 qdev->ddev.mode_config.min_width = 0;
1227 qdev->ddev.mode_config.min_height = 0;
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001228 qdev->ddev.mode_config.max_width = 8192;
1229 qdev->ddev.mode_config.max_height = 8192;
Dave Airlief64122c2013-02-25 14:47:55 +10001230
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001231 qdev->ddev.mode_config.fb_base = qdev->vram_base;
Dave Airlie4695b032013-10-11 11:05:00 +10001232
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001233 drm_mode_create_suggested_offset_properties(&qdev->ddev);
Dave Airlie4695b032013-10-11 11:05:00 +10001234 qxl_mode_create_hotplug_mode_update_property(qdev);
1235
Dave Airlie07f8d9b2013-07-02 06:37:13 +01001236 for (i = 0 ; i < qxl_num_crtc; ++i) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001237 qdev_crtc_init(&qdev->ddev, i);
1238 qdev_output_init(&qdev->ddev, i);
Dave Airlief64122c2013-02-25 14:47:55 +10001239 }
1240
1241 qdev->mode_info.mode_config_initialized = true;
1242
Gabriel Krisman Bertazi9ade8b92017-02-27 17:43:23 -03001243 drm_mode_config_reset(&qdev->ddev);
1244
Dave Airlief64122c2013-02-25 14:47:55 +10001245 /* primary surface must be created by this point, to allow
1246 * issuing command queue commands and having them read by
1247 * spice server. */
1248 qxl_fbdev_init(qdev);
1249 return 0;
1250}
1251
1252void qxl_modeset_fini(struct qxl_device *qdev)
1253{
1254 qxl_fbdev_fini(qdev);
Dave Airlie2bd6ce82013-07-04 14:46:46 +10001255
1256 qxl_destroy_monitors_object(qdev);
Dave Airlief64122c2013-02-25 14:47:55 +10001257 if (qdev->mode_info.mode_config_initialized) {
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -02001258 drm_mode_config_cleanup(&qdev->ddev);
Dave Airlief64122c2013-02-25 14:47:55 +10001259 qdev->mode_info.mode_config_initialized = false;
1260 }
1261}