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