52c4e643331ab8849d8945e7889ffaf49f70b2fe
[linux-2.6-microblaze.git] / drivers / gpu / drm / qxl / qxl_display.c
1 /*
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
27 #include <linux/crc32.h>
28
29 #include "qxl_drv.h"
30 #include "qxl_object.h"
31 #include "drm_crtc_helper.h"
32 #include <drm/drm_plane_helper.h>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_atomic.h>
35
36 static bool qxl_head_enabled(struct qxl_head *head)
37 {
38         return head->width && head->height;
39 }
40
41 static void qxl_alloc_client_monitors_config(struct qxl_device *qdev, unsigned count)
42 {
43         if (qdev->client_monitors_config &&
44             count > qdev->client_monitors_config->count) {
45                 kfree(qdev->client_monitors_config);
46                 qdev->client_monitors_config = NULL;
47         }
48         if (!qdev->client_monitors_config) {
49                 qdev->client_monitors_config = kzalloc(
50                                 sizeof(struct qxl_monitors_config) +
51                                 sizeof(struct qxl_head) * count, GFP_KERNEL);
52                 if (!qdev->client_monitors_config) {
53                         qxl_io_log(qdev,
54                                    "%s: allocation failure for %u heads\n",
55                                    __func__, count);
56                         return;
57                 }
58         }
59         qdev->client_monitors_config->count = count;
60 }
61
62 enum {
63         MONITORS_CONFIG_MODIFIED,
64         MONITORS_CONFIG_UNCHANGED,
65         MONITORS_CONFIG_BAD_CRC,
66 };
67
68 static int qxl_display_copy_rom_client_monitors_config(struct qxl_device *qdev)
69 {
70         int i;
71         int num_monitors;
72         uint32_t crc;
73         int status = MONITORS_CONFIG_UNCHANGED;
74
75         num_monitors = qdev->rom->client_monitors_config.count;
76         crc = crc32(0, (const uint8_t *)&qdev->rom->client_monitors_config,
77                   sizeof(qdev->rom->client_monitors_config));
78         if (crc != qdev->rom->client_monitors_config_crc) {
79                 qxl_io_log(qdev, "crc mismatch: have %X (%zd) != %X\n", crc,
80                            sizeof(qdev->rom->client_monitors_config),
81                            qdev->rom->client_monitors_config_crc);
82                 return MONITORS_CONFIG_BAD_CRC;
83         }
84         if (num_monitors > qdev->monitors_config->max_allowed) {
85                 DRM_DEBUG_KMS("client monitors list will be truncated: %d < %d\n",
86                               qdev->monitors_config->max_allowed, num_monitors);
87                 num_monitors = qdev->monitors_config->max_allowed;
88         } else {
89                 num_monitors = qdev->rom->client_monitors_config.count;
90         }
91         if (qdev->client_monitors_config
92               && (num_monitors != qdev->client_monitors_config->count)) {
93                 status = MONITORS_CONFIG_MODIFIED;
94         }
95         qxl_alloc_client_monitors_config(qdev, num_monitors);
96         /* we copy max from the client but it isn't used */
97         qdev->client_monitors_config->max_allowed =
98                                 qdev->monitors_config->max_allowed;
99         for (i = 0 ; i < qdev->client_monitors_config->count ; ++i) {
100                 struct qxl_urect *c_rect =
101                         &qdev->rom->client_monitors_config.heads[i];
102                 struct qxl_head *client_head =
103                         &qdev->client_monitors_config->heads[i];
104                 if (client_head->x != c_rect->left) {
105                         client_head->x = c_rect->left;
106                         status = MONITORS_CONFIG_MODIFIED;
107                 }
108                 if (client_head->y != c_rect->top) {
109                         client_head->y = c_rect->top;
110                         status = MONITORS_CONFIG_MODIFIED;
111                 }
112                 if (client_head->width != c_rect->right - c_rect->left) {
113                         client_head->width = c_rect->right - c_rect->left;
114                         status = MONITORS_CONFIG_MODIFIED;
115                 }
116                 if (client_head->height != c_rect->bottom - c_rect->top) {
117                         client_head->height = c_rect->bottom - c_rect->top;
118                         status = MONITORS_CONFIG_MODIFIED;
119                 }
120                 if (client_head->surface_id != 0) {
121                         client_head->surface_id = 0;
122                         status = MONITORS_CONFIG_MODIFIED;
123                 }
124                 if (client_head->id != i) {
125                         client_head->id = i;
126                         status = MONITORS_CONFIG_MODIFIED;
127                 }
128                 if (client_head->flags != 0) {
129                         client_head->flags = 0;
130                         status = MONITORS_CONFIG_MODIFIED;
131                 }
132                 DRM_DEBUG_KMS("read %dx%d+%d+%d\n", client_head->width, client_head->height,
133                           client_head->x, client_head->y);
134         }
135
136         return status;
137 }
138
139 static void qxl_update_offset_props(struct qxl_device *qdev)
140 {
141         struct drm_device *dev = &qdev->ddev;
142         struct drm_connector *connector;
143         struct qxl_output *output;
144         struct qxl_head *head;
145
146         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
147                 output = drm_connector_to_qxl_output(connector);
148
149                 head = &qdev->client_monitors_config->heads[output->index];
150
151                 drm_object_property_set_value(&connector->base,
152                         dev->mode_config.suggested_x_property, head->x);
153                 drm_object_property_set_value(&connector->base,
154                         dev->mode_config.suggested_y_property, head->y);
155         }
156 }
157
158 void qxl_display_read_client_monitors_config(struct qxl_device *qdev)
159 {
160
161         struct drm_device *dev = &qdev->ddev;
162         int status;
163
164         status = qxl_display_copy_rom_client_monitors_config(qdev);
165         while (status == MONITORS_CONFIG_BAD_CRC) {
166                 qxl_io_log(qdev, "failed crc check for client_monitors_config,"
167                                  " retrying\n");
168                 status = qxl_display_copy_rom_client_monitors_config(qdev);
169         }
170         if (status == MONITORS_CONFIG_UNCHANGED) {
171                 qxl_io_log(qdev, "config unchanged\n");
172                 DRM_DEBUG("ignoring unchanged client monitors config");
173                 return;
174         }
175
176         drm_modeset_lock_all(dev);
177         qxl_update_offset_props(qdev);
178         drm_modeset_unlock_all(dev);
179         if (!drm_helper_hpd_irq_event(dev)) {
180                 /* notify that the monitor configuration changed, to
181                    adjust at the arbitrary resolution */
182                 drm_kms_helper_hotplug_event(dev);
183         }
184 }
185
186 static int qxl_add_monitors_config_modes(struct drm_connector *connector,
187                                          unsigned *pwidth,
188                                          unsigned *pheight)
189 {
190         struct drm_device *dev = connector->dev;
191         struct qxl_device *qdev = dev->dev_private;
192         struct qxl_output *output = drm_connector_to_qxl_output(connector);
193         int h = output->index;
194         struct drm_display_mode *mode = NULL;
195         struct qxl_head *head;
196
197         if (!qdev->client_monitors_config)
198                 return 0;
199         head = &qdev->client_monitors_config->heads[h];
200
201         mode = drm_cvt_mode(dev, head->width, head->height, 60, false, false,
202                             false);
203         mode->type |= DRM_MODE_TYPE_PREFERRED;
204         mode->hdisplay = head->width;
205         mode->vdisplay = head->height;
206         drm_mode_set_name(mode);
207         *pwidth = head->width;
208         *pheight = head->height;
209         drm_mode_probed_add(connector, mode);
210         /* remember the last custom size for mode validation */
211         qdev->monitors_config_width = mode->hdisplay;
212         qdev->monitors_config_height = mode->vdisplay;
213         return 1;
214 }
215
216 static struct mode_size {
217         int w;
218         int h;
219 } common_modes[] = {
220         { 640,  480},
221         { 720,  480},
222         { 800,  600},
223         { 848,  480},
224         {1024,  768},
225         {1152,  768},
226         {1280,  720},
227         {1280,  800},
228         {1280,  854},
229         {1280,  960},
230         {1280, 1024},
231         {1440,  900},
232         {1400, 1050},
233         {1680, 1050},
234         {1600, 1200},
235         {1920, 1080},
236         {1920, 1200}
237 };
238
239 static int qxl_add_common_modes(struct drm_connector *connector,
240                                 unsigned pwidth,
241                                 unsigned pheight)
242 {
243         struct drm_device *dev = connector->dev;
244         struct drm_display_mode *mode = NULL;
245         int i;
246         for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
247                 mode = drm_cvt_mode(dev, common_modes[i].w, common_modes[i].h,
248                                     60, false, false, false);
249                 if (common_modes[i].w == pwidth && common_modes[i].h == pheight)
250                         mode->type |= DRM_MODE_TYPE_PREFERRED;
251                 drm_mode_probed_add(connector, mode);
252         }
253         return i - 1;
254 }
255
256 static void qxl_crtc_atomic_flush(struct drm_crtc *crtc,
257                                   struct drm_crtc_state *old_crtc_state)
258 {
259         struct drm_device *dev = crtc->dev;
260         struct drm_pending_vblank_event *event;
261         unsigned long flags;
262
263         if (crtc->state && crtc->state->event) {
264                 event = crtc->state->event;
265                 crtc->state->event = NULL;
266
267                 spin_lock_irqsave(&dev->event_lock, flags);
268                 drm_crtc_send_vblank_event(crtc, event);
269                 spin_unlock_irqrestore(&dev->event_lock, flags);
270         }
271 }
272
273 static void qxl_crtc_destroy(struct drm_crtc *crtc)
274 {
275         struct qxl_crtc *qxl_crtc = to_qxl_crtc(crtc);
276
277         drm_crtc_cleanup(crtc);
278         kfree(qxl_crtc);
279 }
280
281 static int qxl_crtc_page_flip(struct drm_crtc *crtc,
282                               struct drm_framebuffer *fb,
283                               struct drm_pending_vblank_event *event,
284                               uint32_t page_flip_flags)
285 {
286         struct drm_device *dev = crtc->dev;
287         struct qxl_device *qdev = dev->dev_private;
288         struct qxl_framebuffer *qfb_src = to_qxl_framebuffer(fb);
289         struct qxl_framebuffer *qfb_old = to_qxl_framebuffer(crtc->primary->fb);
290         struct qxl_bo *bo_old = gem_to_qxl_bo(qfb_old->obj);
291         struct qxl_bo *bo = gem_to_qxl_bo(qfb_src->obj);
292         unsigned long flags;
293         struct drm_clip_rect norect = {
294             .x1 = 0,
295             .y1 = 0,
296             .x2 = fb->width,
297             .y2 = fb->height
298         };
299         int inc = 1;
300         int one_clip_rect = 1;
301         int ret = 0;
302
303         drm_atomic_set_fb_for_plane(crtc->primary->state, fb);
304
305         bo_old->is_primary = false;
306         bo->is_primary = true;
307
308         ret = qxl_bo_pin(bo, bo->type, NULL);
309         if (ret)
310                 return ret;
311
312         qxl_draw_dirty_fb(qdev, qfb_src, bo, 0, 0,
313                           &norect, one_clip_rect, inc);
314
315         if (event) {
316                 spin_lock_irqsave(&dev->event_lock, flags);
317                 drm_crtc_send_vblank_event(crtc, event);
318                 spin_unlock_irqrestore(&dev->event_lock, flags);
319         }
320
321         qxl_bo_unpin(bo);
322
323         return 0;
324 }
325
326 static const struct drm_crtc_funcs qxl_crtc_funcs = {
327         .set_config = drm_crtc_helper_set_config,
328         .destroy = qxl_crtc_destroy,
329         .page_flip = qxl_crtc_page_flip,
330         .reset = drm_atomic_helper_crtc_reset,
331         .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
332         .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
333 };
334
335 void qxl_user_framebuffer_destroy(struct drm_framebuffer *fb)
336 {
337         struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
338
339         drm_gem_object_unreference_unlocked(qxl_fb->obj);
340         drm_framebuffer_cleanup(fb);
341         kfree(qxl_fb);
342 }
343
344 static int qxl_framebuffer_surface_dirty(struct drm_framebuffer *fb,
345                                          struct drm_file *file_priv,
346                                          unsigned flags, unsigned color,
347                                          struct drm_clip_rect *clips,
348                                          unsigned num_clips)
349 {
350         /* TODO: vmwgfx where this was cribbed from had locking. Why? */
351         struct qxl_framebuffer *qxl_fb = to_qxl_framebuffer(fb);
352         struct qxl_device *qdev = qxl_fb->base.dev->dev_private;
353         struct drm_clip_rect norect;
354         struct qxl_bo *qobj;
355         int inc = 1;
356
357         drm_modeset_lock_all(fb->dev);
358
359         qobj = gem_to_qxl_bo(qxl_fb->obj);
360         /* if we aren't primary surface ignore this */
361         if (!qobj->is_primary) {
362                 drm_modeset_unlock_all(fb->dev);
363                 return 0;
364         }
365
366         if (!num_clips) {
367                 num_clips = 1;
368                 clips = &norect;
369                 norect.x1 = norect.y1 = 0;
370                 norect.x2 = fb->width;
371                 norect.y2 = fb->height;
372         } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
373                 num_clips /= 2;
374                 inc = 2; /* skip source rects */
375         }
376
377         qxl_draw_dirty_fb(qdev, qxl_fb, qobj, flags, color,
378                           clips, num_clips, inc);
379
380         drm_modeset_unlock_all(fb->dev);
381
382         return 0;
383 }
384
385 static const struct drm_framebuffer_funcs qxl_fb_funcs = {
386         .destroy = qxl_user_framebuffer_destroy,
387         .dirty = qxl_framebuffer_surface_dirty,
388 /*      TODO?
389  *      .create_handle = qxl_user_framebuffer_create_handle, */
390 };
391
392 int
393 qxl_framebuffer_init(struct drm_device *dev,
394                      struct qxl_framebuffer *qfb,
395                      const struct drm_mode_fb_cmd2 *mode_cmd,
396                      struct drm_gem_object *obj,
397                      const struct drm_framebuffer_funcs *funcs)
398 {
399         int ret;
400
401         qfb->obj = obj;
402         drm_helper_mode_fill_fb_struct(dev, &qfb->base, mode_cmd);
403         ret = drm_framebuffer_init(dev, &qfb->base, funcs);
404         if (ret) {
405                 qfb->obj = NULL;
406                 return ret;
407         }
408         return 0;
409 }
410
411 static void qxl_crtc_dpms(struct drm_crtc *crtc, int mode)
412 {
413 }
414
415 static bool qxl_crtc_mode_fixup(struct drm_crtc *crtc,
416                                   const struct drm_display_mode *mode,
417                                   struct drm_display_mode *adjusted_mode)
418 {
419         struct drm_device *dev = crtc->dev;
420         struct qxl_device *qdev = dev->dev_private;
421
422         qxl_io_log(qdev, "%s: (%d,%d) => (%d,%d)\n",
423                    __func__,
424                    mode->hdisplay, mode->vdisplay,
425                    adjusted_mode->hdisplay,
426                    adjusted_mode->vdisplay);
427         return true;
428 }
429
430 static void
431 qxl_send_monitors_config(struct qxl_device *qdev)
432 {
433         int i;
434
435         BUG_ON(!qdev->ram_header->monitors_config);
436
437         if (qdev->monitors_config->count == 0) {
438                 qxl_io_log(qdev, "%s: 0 monitors??\n", __func__);
439                 return;
440         }
441         for (i = 0 ; i < qdev->monitors_config->count ; ++i) {
442                 struct qxl_head *head = &qdev->monitors_config->heads[i];
443
444                 if (head->y > 8192 || head->x > 8192 ||
445                     head->width > 8192 || head->height > 8192) {
446                         DRM_ERROR("head %d wrong: %dx%d+%d+%d\n",
447                                   i, head->width, head->height,
448                                   head->x, head->y);
449                         return;
450                 }
451         }
452         qxl_io_monitors_config(qdev);
453 }
454
455 static void qxl_monitors_config_set(struct qxl_device *qdev,
456                                     int index,
457                                     unsigned x, unsigned y,
458                                     unsigned width, unsigned height,
459                                     unsigned surf_id)
460 {
461         DRM_DEBUG_KMS("%d:%dx%d+%d+%d\n", index, width, height, x, y);
462         qdev->monitors_config->heads[index].x = x;
463         qdev->monitors_config->heads[index].y = y;
464         qdev->monitors_config->heads[index].width = width;
465         qdev->monitors_config->heads[index].height = height;
466         qdev->monitors_config->heads[index].surface_id = surf_id;
467
468 }
469
470 static void qxl_crtc_prepare(struct drm_crtc *crtc)
471 {
472         DRM_DEBUG("current: %dx%d+%d+%d (%d).\n",
473                   crtc->mode.hdisplay, crtc->mode.vdisplay,
474                   crtc->x, crtc->y, crtc->enabled);
475 }
476
477 void qxl_mode_set_nofb(struct drm_crtc *crtc)
478 {
479         struct qxl_device *qdev = crtc->dev->dev_private;
480         struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
481         struct drm_display_mode *mode = &crtc->mode;
482
483         DRM_DEBUG("Mode set (%d,%d)\n",
484                   mode->hdisplay, mode->vdisplay);
485
486         qxl_monitors_config_set(qdev, qcrtc->index, 0, 0,
487                                 mode->hdisplay, mode->vdisplay, 0);
488
489 }
490
491 static void qxl_crtc_commit(struct drm_crtc *crtc)
492 {
493         DRM_DEBUG("\n");
494 }
495
496 static void qxl_crtc_disable(struct drm_crtc *crtc)
497 {
498         struct qxl_crtc *qcrtc = to_qxl_crtc(crtc);
499         struct qxl_device *qdev = crtc->dev->dev_private;
500
501         qxl_monitors_config_set(qdev, qcrtc->index, 0, 0, 0, 0, 0);
502
503         qxl_send_monitors_config(qdev);
504 }
505
506 static const struct drm_crtc_helper_funcs qxl_crtc_helper_funcs = {
507         .dpms = qxl_crtc_dpms,
508         .disable = qxl_crtc_disable,
509         .mode_fixup = qxl_crtc_mode_fixup,
510         .mode_set = drm_helper_crtc_mode_set,
511         .mode_set_nofb = qxl_mode_set_nofb,
512         .prepare = qxl_crtc_prepare,
513         .commit = qxl_crtc_commit,
514         .atomic_flush = qxl_crtc_atomic_flush,
515 };
516
517 int qxl_primary_atomic_check(struct drm_plane *plane,
518                              struct drm_plane_state *state)
519 {
520         struct qxl_device *qdev = plane->dev->dev_private;
521         struct qxl_framebuffer *qfb;
522         struct qxl_bo *bo;
523
524         if (!state->crtc || !state->fb)
525                 return 0;
526
527         qfb = to_qxl_framebuffer(state->fb);
528         bo = gem_to_qxl_bo(qfb->obj);
529
530         if (bo->surf.stride * bo->surf.height > qdev->vram_size) {
531                 DRM_ERROR("Mode doesn't fit in vram size (vgamem)");
532                 return -EINVAL;
533         }
534
535         return 0;
536 }
537
538 static void qxl_primary_atomic_update(struct drm_plane *plane,
539                                       struct drm_plane_state *old_state)
540 {
541         struct qxl_device *qdev = plane->dev->dev_private;
542         struct qxl_framebuffer *qfb =
543                 to_qxl_framebuffer(plane->state->fb);
544         struct qxl_framebuffer *qfb_old;
545         struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
546         struct qxl_bo *bo_old;
547         struct drm_clip_rect norect = {
548             .x1 = 0,
549             .y1 = 0,
550             .x2 = qfb->base.width,
551             .y2 = qfb->base.height
552         };
553
554         if (!old_state->fb) {
555                 qxl_io_log(qdev,
556                            "create primary fb: %dx%d,%d,%d\n",
557                            bo->surf.width, bo->surf.height,
558                            bo->surf.stride, bo->surf.format);
559
560                 qxl_io_create_primary(qdev, 0, bo);
561                 bo->is_primary = true;
562                 return;
563
564         } else {
565                 qfb_old = to_qxl_framebuffer(old_state->fb);
566                 bo_old = gem_to_qxl_bo(qfb_old->obj);
567                 bo_old->is_primary = false;
568         }
569
570         bo->is_primary = true;
571         qxl_draw_dirty_fb(qdev, qfb, bo, 0, 0, &norect, 1, 1);
572 }
573
574 static void qxl_primary_atomic_disable(struct drm_plane *plane,
575                                        struct drm_plane_state *old_state)
576 {
577         struct qxl_device *qdev = plane->dev->dev_private;
578
579         if (old_state->fb)
580         {       struct qxl_framebuffer *qfb =
581                         to_qxl_framebuffer(old_state->fb);
582                 struct qxl_bo *bo = gem_to_qxl_bo(qfb->obj);
583
584                 qxl_io_destroy_primary(qdev);
585                 bo->is_primary = false;
586         }
587 }
588
589 int qxl_plane_atomic_check(struct drm_plane *plane,
590                            struct drm_plane_state *state)
591 {
592         return 0;
593 }
594
595 static void qxl_cursor_atomic_update(struct drm_plane *plane,
596                                      struct drm_plane_state *old_state)
597 {
598         struct drm_device *dev = plane->dev;
599         struct qxl_device *qdev = dev->dev_private;
600         struct drm_framebuffer *fb = plane->state->fb;
601         struct qxl_release *release;
602         struct qxl_cursor_cmd *cmd;
603         struct qxl_cursor *cursor;
604         struct drm_gem_object *obj;
605         struct qxl_bo *cursor_bo, *user_bo = NULL;
606         int ret;
607         void *user_ptr;
608         int size = 64*64*4;
609
610         ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
611                                          QXL_RELEASE_CURSOR_CMD,
612                                          &release, NULL);
613
614         cmd = (struct qxl_cursor_cmd *) qxl_release_map(qdev, release);
615
616         if (fb != old_state->fb) {
617                 obj = to_qxl_framebuffer(fb)->obj;
618                 user_bo = gem_to_qxl_bo(obj);
619
620                 /* pinning is done in the prepare/cleanup framevbuffer */
621                 ret = qxl_bo_kmap(user_bo, &user_ptr);
622                 if (ret)
623                         goto out_free_release;
624
625                 ret = qxl_alloc_bo_reserved(qdev, release,
626                                             sizeof(struct qxl_cursor) + size,
627                                             &cursor_bo);
628                 if (ret)
629                         goto out_kunmap;
630
631                 ret = qxl_release_reserve_list(release, true);
632                 if (ret)
633                         goto out_free_bo;
634
635                 ret = qxl_bo_kmap(cursor_bo, (void **)&cursor);
636                 if (ret)
637                         goto out_backoff;
638
639                 cursor->header.unique = 0;
640                 cursor->header.type = SPICE_CURSOR_TYPE_ALPHA;
641                 cursor->header.width = 64;
642                 cursor->header.height = 64;
643                 cursor->header.hot_spot_x = fb->hot_x;
644                 cursor->header.hot_spot_y = fb->hot_y;
645                 cursor->data_size = size;
646                 cursor->chunk.next_chunk = 0;
647                 cursor->chunk.prev_chunk = 0;
648                 cursor->chunk.data_size = size;
649                 memcpy(cursor->chunk.data, user_ptr, size);
650                 qxl_bo_kunmap(cursor_bo);
651                 qxl_bo_kunmap(user_bo);
652
653                 cmd->u.set.visible = 1;
654                 cmd->u.set.shape = qxl_bo_physical_address(qdev,
655                                                            cursor_bo, 0);
656                 cmd->type = QXL_CURSOR_SET;
657         } else {
658
659                 ret = qxl_release_reserve_list(release, true);
660                 if (ret)
661                         goto out_free_release;
662
663                 cmd->type = QXL_CURSOR_MOVE;
664         }
665
666         cmd->u.position.x = plane->state->crtc_x + fb->hot_x;
667         cmd->u.position.y = plane->state->crtc_y + fb->hot_y;
668
669         qxl_release_unmap(qdev, release, &cmd->release_info);
670         qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
671         qxl_release_fence_buffer_objects(release);
672
673         return;
674
675 out_backoff:
676         qxl_release_backoff_reserve_list(release);
677 out_free_bo:
678         qxl_bo_unref(&cursor_bo);
679 out_kunmap:
680         qxl_bo_kunmap(user_bo);
681 out_free_release:
682         qxl_release_free(qdev, release);
683         return;
684
685 }
686
687 void qxl_cursor_atomic_disable(struct drm_plane *plane,
688                                struct drm_plane_state *old_state)
689 {
690         struct qxl_device *qdev = plane->dev->dev_private;
691         struct qxl_release *release;
692         struct qxl_cursor_cmd *cmd;
693         int ret;
694
695         ret = qxl_alloc_release_reserved(qdev, sizeof(*cmd),
696                                          QXL_RELEASE_CURSOR_CMD,
697                                          &release, NULL);
698         if (ret)
699                 return;
700
701         ret = qxl_release_reserve_list(release, true);
702         if (ret) {
703                 qxl_release_free(qdev, release);
704                 return;
705         }
706
707         cmd = (struct qxl_cursor_cmd *)qxl_release_map(qdev, release);
708         cmd->type = QXL_CURSOR_HIDE;
709         qxl_release_unmap(qdev, release, &cmd->release_info);
710
711         qxl_push_cursor_ring_release(qdev, release, QXL_CMD_CURSOR, false);
712         qxl_release_fence_buffer_objects(release);
713 }
714
715 int qxl_plane_prepare_fb(struct drm_plane *plane,
716                          struct drm_plane_state *new_state)
717 {
718         struct drm_gem_object *obj;
719         struct qxl_bo *user_bo;
720         int ret;
721
722         if (!new_state->fb)
723                 return 0;
724
725         obj = to_qxl_framebuffer(new_state->fb)->obj;
726         user_bo = gem_to_qxl_bo(obj);
727
728         ret = qxl_bo_pin(user_bo, QXL_GEM_DOMAIN_CPU, NULL);
729         if (ret)
730                 return ret;
731
732         return 0;
733 }
734
735 static void qxl_plane_cleanup_fb(struct drm_plane *plane,
736                                  struct drm_plane_state *old_state)
737 {
738         struct drm_gem_object *obj;
739         struct qxl_bo *user_bo;
740
741         if (!plane->state->fb) {
742                 /* we never executed prepare_fb, so there's nothing to
743                  * unpin.
744                  */
745                 return;
746         }
747
748         obj = to_qxl_framebuffer(plane->state->fb)->obj;
749         user_bo = gem_to_qxl_bo(obj);
750         qxl_bo_unpin(user_bo);
751 }
752
753 static const uint32_t qxl_cursor_plane_formats[] = {
754         DRM_FORMAT_ARGB8888,
755 };
756
757 static const struct drm_plane_helper_funcs qxl_cursor_helper_funcs = {
758         .atomic_check = qxl_plane_atomic_check,
759         .atomic_update = qxl_cursor_atomic_update,
760         .atomic_disable = qxl_cursor_atomic_disable,
761         .prepare_fb = qxl_plane_prepare_fb,
762         .cleanup_fb = qxl_plane_cleanup_fb,
763 };
764
765 static const struct drm_plane_funcs qxl_cursor_plane_funcs = {
766         .update_plane   = drm_atomic_helper_update_plane,
767         .disable_plane  = drm_atomic_helper_disable_plane,
768         .destroy        = drm_primary_helper_destroy,
769         .reset          = drm_atomic_helper_plane_reset,
770         .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
771         .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
772 };
773
774 static const uint32_t qxl_primary_plane_formats[] = {
775         DRM_FORMAT_XRGB8888,
776         DRM_FORMAT_ARGB8888,
777 };
778
779 static const struct drm_plane_helper_funcs primary_helper_funcs = {
780         .atomic_check = qxl_primary_atomic_check,
781         .atomic_update = qxl_primary_atomic_update,
782         .atomic_disable = qxl_primary_atomic_disable,
783         .prepare_fb = qxl_plane_prepare_fb,
784         .cleanup_fb = qxl_plane_cleanup_fb,
785 };
786
787 static const struct drm_plane_funcs qxl_primary_plane_funcs = {
788         .update_plane   = drm_atomic_helper_update_plane,
789         .disable_plane  = drm_atomic_helper_disable_plane,
790         .destroy        = drm_primary_helper_destroy,
791         .reset          = drm_atomic_helper_plane_reset,
792         .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
793         .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
794 };
795
796 static struct drm_plane *qxl_create_plane(struct qxl_device *qdev,
797                                           unsigned int possible_crtcs,
798                                           enum drm_plane_type type)
799 {
800         const struct drm_plane_helper_funcs *helper_funcs = NULL;
801         struct drm_plane *plane;
802         const struct drm_plane_funcs *funcs;
803         const uint32_t *formats;
804         int num_formats;
805         int err;
806
807         if (type == DRM_PLANE_TYPE_PRIMARY) {
808                 funcs = &qxl_primary_plane_funcs;
809                 formats = qxl_primary_plane_formats;
810                 num_formats = ARRAY_SIZE(qxl_primary_plane_formats);
811                 helper_funcs = &primary_helper_funcs;
812         } else if (type == DRM_PLANE_TYPE_CURSOR) {
813                 funcs = &qxl_cursor_plane_funcs;
814                 formats = qxl_cursor_plane_formats;
815                 helper_funcs = &qxl_cursor_helper_funcs;
816                 num_formats = ARRAY_SIZE(qxl_cursor_plane_formats);
817         } else {
818                 return ERR_PTR(-EINVAL);
819         }
820
821         plane = kzalloc(sizeof(*plane), GFP_KERNEL);
822         if (!plane)
823                 return ERR_PTR(-ENOMEM);
824
825         err = drm_universal_plane_init(&qdev->ddev, plane, possible_crtcs,
826                                        funcs, formats, num_formats,
827                                        type, NULL);
828         if (err)
829                 goto free_plane;
830
831         drm_plane_helper_add(plane, helper_funcs);
832
833         return plane;
834
835 free_plane:
836         kfree(plane);
837         return ERR_PTR(-EINVAL);
838 }
839
840 static int qdev_crtc_init(struct drm_device *dev, int crtc_id)
841 {
842         struct qxl_crtc *qxl_crtc;
843         struct drm_plane *primary, *cursor;
844         struct qxl_device *qdev = dev->dev_private;
845         int r;
846
847         qxl_crtc = kzalloc(sizeof(struct qxl_crtc), GFP_KERNEL);
848         if (!qxl_crtc)
849                 return -ENOMEM;
850
851         primary = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_PRIMARY);
852         if (IS_ERR(primary)) {
853                 r = -ENOMEM;
854                 goto free_mem;
855         }
856
857         cursor = qxl_create_plane(qdev, 1 << crtc_id, DRM_PLANE_TYPE_CURSOR);
858         if (IS_ERR(cursor)) {
859                 r = -ENOMEM;
860                 goto clean_primary;
861         }
862
863         r = drm_crtc_init_with_planes(dev, &qxl_crtc->base, primary, cursor,
864                                       &qxl_crtc_funcs, NULL);
865         if (r)
866                 goto clean_cursor;
867
868         qxl_crtc->index = crtc_id;
869         drm_crtc_helper_add(&qxl_crtc->base, &qxl_crtc_helper_funcs);
870         return 0;
871
872 clean_cursor:
873         drm_plane_cleanup(cursor);
874         kfree(cursor);
875 clean_primary:
876         drm_plane_cleanup(primary);
877         kfree(primary);
878 free_mem:
879         kfree(qxl_crtc);
880         return r;
881 }
882
883 static void qxl_enc_dpms(struct drm_encoder *encoder, int mode)
884 {
885         DRM_DEBUG("\n");
886 }
887
888 static void qxl_enc_prepare(struct drm_encoder *encoder)
889 {
890         DRM_DEBUG("\n");
891 }
892
893 static void qxl_write_monitors_config_for_encoder(struct qxl_device *qdev,
894                 struct drm_encoder *encoder)
895 {
896         int i;
897         struct qxl_output *output = drm_encoder_to_qxl_output(encoder);
898         struct qxl_head *head;
899         struct drm_display_mode *mode;
900
901         BUG_ON(!encoder);
902         /* TODO: ugly, do better */
903         i = output->index;
904         if (!qdev->monitors_config ||
905             qdev->monitors_config->max_allowed <= i) {
906                 DRM_ERROR(
907                 "head number too large or missing monitors config: %p, %d",
908                 qdev->monitors_config,
909                 qdev->monitors_config ?
910                         qdev->monitors_config->max_allowed : -1);
911                 return;
912         }
913         if (!encoder->crtc) {
914                 DRM_ERROR("missing crtc on encoder %p\n", encoder);
915                 return;
916         }
917         if (i != 0)
918                 DRM_DEBUG("missing for multiple monitors: no head holes\n");
919         head = &qdev->monitors_config->heads[i];
920         head->id = i;
921         if (encoder->crtc->enabled) {
922                 mode = &encoder->crtc->mode;
923                 head->width = mode->hdisplay;
924                 head->height = mode->vdisplay;
925                 head->x = encoder->crtc->x;
926                 head->y = encoder->crtc->y;
927                 if (qdev->monitors_config->count < i + 1)
928                         qdev->monitors_config->count = i + 1;
929         } else {
930                 head->width = 0;
931                 head->height = 0;
932                 head->x = 0;
933                 head->y = 0;
934         }
935         DRM_DEBUG_KMS("setting head %d to +%d+%d %dx%d out of %d\n",
936                       i, head->x, head->y, head->width, head->height, qdev->monitors_config->count);
937         head->flags = 0;
938         /* TODO - somewhere else to call this for multiple monitors
939          * (config_commit?) */
940         qxl_send_monitors_config(qdev);
941 }
942
943 static void qxl_enc_commit(struct drm_encoder *encoder)
944 {
945         struct qxl_device *qdev = encoder->dev->dev_private;
946
947         qxl_write_monitors_config_for_encoder(qdev, encoder);
948         DRM_DEBUG("\n");
949 }
950
951 static void qxl_enc_mode_set(struct drm_encoder *encoder,
952                                 struct drm_display_mode *mode,
953                                 struct drm_display_mode *adjusted_mode)
954 {
955         DRM_DEBUG("\n");
956 }
957
958 static int qxl_conn_get_modes(struct drm_connector *connector)
959 {
960         int ret = 0;
961         struct qxl_device *qdev = connector->dev->dev_private;
962         unsigned pwidth = 1024;
963         unsigned pheight = 768;
964
965         DRM_DEBUG_KMS("monitors_config=%p\n", qdev->monitors_config);
966         /* TODO: what should we do here? only show the configured modes for the
967          * device, or allow the full list, or both? */
968         if (qdev->monitors_config && qdev->monitors_config->count) {
969                 ret = qxl_add_monitors_config_modes(connector, &pwidth, &pheight);
970                 if (ret < 0)
971                         return ret;
972         }
973         ret += qxl_add_common_modes(connector, pwidth, pheight);
974         return ret;
975 }
976
977 static int qxl_conn_mode_valid(struct drm_connector *connector,
978                                struct drm_display_mode *mode)
979 {
980         struct drm_device *ddev = connector->dev;
981         struct qxl_device *qdev = ddev->dev_private;
982         int i;
983
984         /* TODO: is this called for user defined modes? (xrandr --add-mode)
985          * TODO: check that the mode fits in the framebuffer */
986
987         if(qdev->monitors_config_width == mode->hdisplay &&
988            qdev->monitors_config_height == mode->vdisplay)
989                 return MODE_OK;
990
991         for (i = 0; i < ARRAY_SIZE(common_modes); i++) {
992                 if (common_modes[i].w == mode->hdisplay && common_modes[i].h == mode->vdisplay)
993                         return MODE_OK;
994         }
995         return MODE_BAD;
996 }
997
998 static struct drm_encoder *qxl_best_encoder(struct drm_connector *connector)
999 {
1000         struct qxl_output *qxl_output =
1001                 drm_connector_to_qxl_output(connector);
1002
1003         DRM_DEBUG("\n");
1004         return &qxl_output->enc;
1005 }
1006
1007
1008 static const struct drm_encoder_helper_funcs qxl_enc_helper_funcs = {
1009         .dpms = qxl_enc_dpms,
1010         .prepare = qxl_enc_prepare,
1011         .mode_set = qxl_enc_mode_set,
1012         .commit = qxl_enc_commit,
1013 };
1014
1015 static const struct drm_connector_helper_funcs qxl_connector_helper_funcs = {
1016         .get_modes = qxl_conn_get_modes,
1017         .mode_valid = qxl_conn_mode_valid,
1018         .best_encoder = qxl_best_encoder,
1019 };
1020
1021 static enum drm_connector_status qxl_conn_detect(
1022                         struct drm_connector *connector,
1023                         bool force)
1024 {
1025         struct qxl_output *output =
1026                 drm_connector_to_qxl_output(connector);
1027         struct drm_device *ddev = connector->dev;
1028         struct qxl_device *qdev = ddev->dev_private;
1029         bool connected = false;
1030
1031         /* The first monitor is always connected */
1032         if (!qdev->client_monitors_config) {
1033                 if (output->index == 0)
1034                         connected = true;
1035         } else
1036                 connected = qdev->client_monitors_config->count > output->index &&
1037                      qxl_head_enabled(&qdev->client_monitors_config->heads[output->index]);
1038
1039         DRM_DEBUG("#%d connected: %d\n", output->index, connected);
1040         if (!connected)
1041                 qxl_monitors_config_set(qdev, output->index, 0, 0, 0, 0, 0);
1042
1043         return connected ? connector_status_connected
1044                          : connector_status_disconnected;
1045 }
1046
1047 static int qxl_conn_set_property(struct drm_connector *connector,
1048                                    struct drm_property *property,
1049                                    uint64_t value)
1050 {
1051         DRM_DEBUG("\n");
1052         return 0;
1053 }
1054
1055 static void qxl_conn_destroy(struct drm_connector *connector)
1056 {
1057         struct qxl_output *qxl_output =
1058                 drm_connector_to_qxl_output(connector);
1059
1060         drm_connector_unregister(connector);
1061         drm_connector_cleanup(connector);
1062         kfree(qxl_output);
1063 }
1064
1065 static const struct drm_connector_funcs qxl_connector_funcs = {
1066         .dpms = drm_helper_connector_dpms,
1067         .detect = qxl_conn_detect,
1068         .fill_modes = drm_helper_probe_single_connector_modes,
1069         .set_property = qxl_conn_set_property,
1070         .destroy = qxl_conn_destroy,
1071         .reset = drm_atomic_helper_connector_reset,
1072         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
1073         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
1074 };
1075
1076 static void qxl_enc_destroy(struct drm_encoder *encoder)
1077 {
1078         drm_encoder_cleanup(encoder);
1079 }
1080
1081 static const struct drm_encoder_funcs qxl_enc_funcs = {
1082         .destroy = qxl_enc_destroy,
1083 };
1084
1085 static int qxl_mode_create_hotplug_mode_update_property(struct qxl_device *qdev)
1086 {
1087         if (qdev->hotplug_mode_update_property)
1088                 return 0;
1089
1090         qdev->hotplug_mode_update_property =
1091                 drm_property_create_range(&qdev->ddev, DRM_MODE_PROP_IMMUTABLE,
1092                                           "hotplug_mode_update", 0, 1);
1093
1094         return 0;
1095 }
1096
1097 static int qdev_output_init(struct drm_device *dev, int num_output)
1098 {
1099         struct qxl_device *qdev = dev->dev_private;
1100         struct qxl_output *qxl_output;
1101         struct drm_connector *connector;
1102         struct drm_encoder *encoder;
1103
1104         qxl_output = kzalloc(sizeof(struct qxl_output), GFP_KERNEL);
1105         if (!qxl_output)
1106                 return -ENOMEM;
1107
1108         qxl_output->index = num_output;
1109
1110         connector = &qxl_output->base;
1111         encoder = &qxl_output->enc;
1112         drm_connector_init(dev, &qxl_output->base,
1113                            &qxl_connector_funcs, DRM_MODE_CONNECTOR_VIRTUAL);
1114
1115         drm_encoder_init(dev, &qxl_output->enc, &qxl_enc_funcs,
1116                          DRM_MODE_ENCODER_VIRTUAL, NULL);
1117
1118         /* we get HPD via client monitors config */
1119         connector->polled = DRM_CONNECTOR_POLL_HPD;
1120         encoder->possible_crtcs = 1 << num_output;
1121         drm_mode_connector_attach_encoder(&qxl_output->base,
1122                                           &qxl_output->enc);
1123         drm_encoder_helper_add(encoder, &qxl_enc_helper_funcs);
1124         drm_connector_helper_add(connector, &qxl_connector_helper_funcs);
1125
1126         drm_object_attach_property(&connector->base,
1127                                    qdev->hotplug_mode_update_property, 0);
1128         drm_object_attach_property(&connector->base,
1129                                    dev->mode_config.suggested_x_property, 0);
1130         drm_object_attach_property(&connector->base,
1131                                    dev->mode_config.suggested_y_property, 0);
1132         return 0;
1133 }
1134
1135 static struct drm_framebuffer *
1136 qxl_user_framebuffer_create(struct drm_device *dev,
1137                             struct drm_file *file_priv,
1138                             const struct drm_mode_fb_cmd2 *mode_cmd)
1139 {
1140         struct drm_gem_object *obj;
1141         struct qxl_framebuffer *qxl_fb;
1142         int ret;
1143
1144         obj = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
1145         if (!obj)
1146                 return NULL;
1147
1148         qxl_fb = kzalloc(sizeof(*qxl_fb), GFP_KERNEL);
1149         if (qxl_fb == NULL)
1150                 return NULL;
1151
1152         ret = qxl_framebuffer_init(dev, qxl_fb, mode_cmd, obj, &qxl_fb_funcs);
1153         if (ret) {
1154                 kfree(qxl_fb);
1155                 drm_gem_object_unreference_unlocked(obj);
1156                 return NULL;
1157         }
1158
1159         return &qxl_fb->base;
1160 }
1161
1162 static const struct drm_mode_config_funcs qxl_mode_funcs = {
1163         .fb_create = qxl_user_framebuffer_create,
1164         .atomic_check = drm_atomic_helper_check,
1165         .atomic_commit = drm_atomic_helper_commit,
1166 };
1167
1168 int qxl_create_monitors_object(struct qxl_device *qdev)
1169 {
1170         int ret;
1171         struct drm_gem_object *gobj;
1172         int max_allowed = qxl_num_crtc;
1173         int monitors_config_size = sizeof(struct qxl_monitors_config) +
1174                 max_allowed * sizeof(struct qxl_head);
1175
1176         ret = qxl_gem_object_create(qdev, monitors_config_size, 0,
1177                                     QXL_GEM_DOMAIN_VRAM,
1178                                     false, false, NULL, &gobj);
1179         if (ret) {
1180                 DRM_ERROR("%s: failed to create gem ret=%d\n", __func__, ret);
1181                 return -ENOMEM;
1182         }
1183         qdev->monitors_config_bo = gem_to_qxl_bo(gobj);
1184
1185         ret = qxl_bo_pin(qdev->monitors_config_bo, QXL_GEM_DOMAIN_VRAM, NULL);
1186         if (ret)
1187                 return ret;
1188
1189         qxl_bo_kmap(qdev->monitors_config_bo, NULL);
1190
1191         qdev->monitors_config = qdev->monitors_config_bo->kptr;
1192         qdev->ram_header->monitors_config =
1193                 qxl_bo_physical_address(qdev, qdev->monitors_config_bo, 0);
1194
1195         memset(qdev->monitors_config, 0, monitors_config_size);
1196         qdev->monitors_config->max_allowed = max_allowed;
1197         return 0;
1198 }
1199
1200 int qxl_destroy_monitors_object(struct qxl_device *qdev)
1201 {
1202         int ret;
1203
1204         qdev->monitors_config = NULL;
1205         qdev->ram_header->monitors_config = 0;
1206
1207         qxl_bo_kunmap(qdev->monitors_config_bo);
1208         ret = qxl_bo_unpin(qdev->monitors_config_bo);
1209         if (ret)
1210                 return ret;
1211
1212         qxl_bo_unref(&qdev->monitors_config_bo);
1213         return 0;
1214 }
1215
1216 int qxl_modeset_init(struct qxl_device *qdev)
1217 {
1218         int i;
1219         int ret;
1220
1221         drm_mode_config_init(&qdev->ddev);
1222
1223         ret = qxl_create_monitors_object(qdev);
1224         if (ret)
1225                 return ret;
1226
1227         qdev->ddev.mode_config.funcs = (void *)&qxl_mode_funcs;
1228
1229         /* modes will be validated against the framebuffer size */
1230         qdev->ddev.mode_config.min_width = 0;
1231         qdev->ddev.mode_config.min_height = 0;
1232         qdev->ddev.mode_config.max_width = 8192;
1233         qdev->ddev.mode_config.max_height = 8192;
1234
1235         qdev->ddev.mode_config.fb_base = qdev->vram_base;
1236
1237         drm_mode_create_suggested_offset_properties(&qdev->ddev);
1238         qxl_mode_create_hotplug_mode_update_property(qdev);
1239
1240         for (i = 0 ; i < qxl_num_crtc; ++i) {
1241                 qdev_crtc_init(&qdev->ddev, i);
1242                 qdev_output_init(&qdev->ddev, i);
1243         }
1244
1245         qdev->mode_info.mode_config_initialized = true;
1246
1247         drm_mode_config_reset(&qdev->ddev);
1248
1249         /* primary surface must be created by this point, to allow
1250          * issuing command queue commands and having them read by
1251          * spice server. */
1252         qxl_fbdev_init(qdev);
1253         return 0;
1254 }
1255
1256 void qxl_modeset_fini(struct qxl_device *qdev)
1257 {
1258         qxl_fbdev_fini(qdev);
1259
1260         qxl_destroy_monitors_object(qdev);
1261         if (qdev->mode_info.mode_config_initialized) {
1262                 drm_mode_config_cleanup(&qdev->ddev);
1263                 qdev->mode_info.mode_config_initialized = false;
1264         }
1265 }