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