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