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