OSDN Git Service

49ff9c4a630213e1c8c9314963fe24c605f54da1
[uclinux-h8/linux.git] / drivers / staging / vboxvideo / vbox_mode.c
1 /*
2  * Copyright (C) 2013-2017 Oracle Corporation
3  * This file is based on ast_mode.c
4  * Copyright 2012 Red Hat Inc.
5  * Parts based on xf86-video-ast
6  * Copyright (c) 2005 ASPEED Technology Inc.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22  * USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * The above copyright notice and this permission notice (including the
25  * next paragraph) shall be included in all copies or substantial portions
26  * of the Software.
27  *
28  */
29 /*
30  * Authors: Dave Airlie <airlied@redhat.com>
31  *          Michael Thayer <michael.thayer@oracle.com,
32  *          Hans de Goede <hdegoede@redhat.com>
33  */
34 #include <linux/export.h>
35 #include <drm/drm_atomic.h>
36 #include <drm/drm_crtc_helper.h>
37 #include <drm/drm_plane_helper.h>
38 #include <drm/drm_atomic_helper.h>
39
40 #include "vbox_drv.h"
41 #include "vboxvideo.h"
42 #include "hgsmi_channels.h"
43
44 /**
45  * Set a graphics mode.  Poke any required values into registers, do an HGSMI
46  * mode set and tell the host we support advanced graphics functions.
47  */
48 static void vbox_do_modeset(struct drm_crtc *crtc)
49 {
50         struct drm_framebuffer *fb = crtc->primary->state->fb;
51         struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
52         struct vbox_private *vbox;
53         int width, height, bpp, pitch;
54         u16 flags;
55         s32 x_offset, y_offset;
56
57         vbox = crtc->dev->dev_private;
58         width = vbox_crtc->width ? vbox_crtc->width : 640;
59         height = vbox_crtc->height ? vbox_crtc->height : 480;
60         bpp = fb ? fb->format->cpp[0] * 8 : 32;
61         pitch = fb ? fb->pitches[0] : width * bpp / 8;
62         x_offset = vbox->single_framebuffer ? vbox_crtc->x : vbox_crtc->x_hint;
63         y_offset = vbox->single_framebuffer ? vbox_crtc->y : vbox_crtc->y_hint;
64
65         /*
66          * This is the old way of setting graphics modes.  It assumed one screen
67          * and a frame-buffer at the start of video RAM.  On older versions of
68          * VirtualBox, certain parts of the code still assume that the first
69          * screen is programmed this way, so try to fake it.
70          */
71         if (vbox_crtc->crtc_id == 0 && fb &&
72             vbox_crtc->fb_offset / pitch < 0xffff - crtc->y &&
73             vbox_crtc->fb_offset % (bpp / 8) == 0) {
74                 vbox_write_ioport(VBE_DISPI_INDEX_XRES, width);
75                 vbox_write_ioport(VBE_DISPI_INDEX_YRES, height);
76                 vbox_write_ioport(VBE_DISPI_INDEX_VIRT_WIDTH, pitch * 8 / bpp);
77                 vbox_write_ioport(VBE_DISPI_INDEX_BPP, bpp);
78                 vbox_write_ioport(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED);
79                 vbox_write_ioport(
80                         VBE_DISPI_INDEX_X_OFFSET,
81                         vbox_crtc->fb_offset % pitch / bpp * 8 + vbox_crtc->x);
82                 vbox_write_ioport(VBE_DISPI_INDEX_Y_OFFSET,
83                                   vbox_crtc->fb_offset / pitch + vbox_crtc->y);
84         }
85
86         flags = VBVA_SCREEN_F_ACTIVE;
87         flags |= (fb && crtc->state->active) ? 0 : VBVA_SCREEN_F_BLANK;
88         flags |= vbox_crtc->disconnected ? VBVA_SCREEN_F_DISABLED : 0;
89         hgsmi_process_display_info(vbox->guest_pool, vbox_crtc->crtc_id,
90                                    x_offset, y_offset,
91                                    vbox_crtc->x * bpp / 8 +
92                                                         vbox_crtc->y * pitch,
93                                    pitch, width, height, bpp, flags);
94 }
95
96 static int vbox_set_view(struct drm_crtc *crtc)
97 {
98         struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
99         struct vbox_private *vbox = crtc->dev->dev_private;
100         struct vbva_infoview *p;
101
102         /*
103          * Tell the host about the view.  This design originally targeted the
104          * Windows XP driver architecture and assumed that each screen would
105          * have a dedicated frame buffer with the command buffer following it,
106          * the whole being a "view".  The host works out which screen a command
107          * buffer belongs to by checking whether it is in the first view, then
108          * whether it is in the second and so on.  The first match wins.  We
109          * cheat around this by making the first view be the managed memory
110          * plus the first command buffer, the second the same plus the second
111          * buffer and so on.
112          */
113         p = hgsmi_buffer_alloc(vbox->guest_pool, sizeof(*p),
114                                HGSMI_CH_VBVA, VBVA_INFO_VIEW);
115         if (!p)
116                 return -ENOMEM;
117
118         p->view_index = vbox_crtc->crtc_id;
119         p->view_offset = vbox_crtc->fb_offset;
120         p->view_size = vbox->available_vram_size - vbox_crtc->fb_offset +
121                        vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE;
122         p->max_screen_size = vbox->available_vram_size - vbox_crtc->fb_offset;
123
124         hgsmi_buffer_submit(vbox->guest_pool, p);
125         hgsmi_buffer_free(vbox->guest_pool, p);
126
127         return 0;
128 }
129
130 /*
131  * Try to map the layout of virtual screens to the range of the input device.
132  * Return true if we need to re-set the crtc modes due to screen offset
133  * changes.
134  */
135 static bool vbox_set_up_input_mapping(struct vbox_private *vbox)
136 {
137         struct drm_crtc *crtci;
138         struct drm_connector *connectori;
139         struct drm_framebuffer *fb, *fb1 = NULL;
140         bool single_framebuffer = true;
141         bool old_single_framebuffer = vbox->single_framebuffer;
142         u16 width = 0, height = 0;
143
144         /*
145          * Are we using an X.Org-style single large frame-buffer for all crtcs?
146          * If so then screen layout can be deduced from the crtc offsets.
147          * Same fall-back if this is the fbdev frame-buffer.
148          */
149         list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
150                 fb = crtci->primary->state->fb;
151                 if (!fb)
152                         continue;
153
154                 if (!fb1) {
155                         fb1 = fb;
156                         if (to_vbox_framebuffer(fb1) == &vbox->fbdev->afb)
157                                 break;
158                 } else if (fb != fb1) {
159                         single_framebuffer = false;
160                 }
161         }
162         if (!fb1)
163                 return false;
164
165         if (single_framebuffer) {
166                 vbox->single_framebuffer = true;
167                 vbox->input_mapping_width = fb1->width;
168                 vbox->input_mapping_height = fb1->height;
169                 return old_single_framebuffer != vbox->single_framebuffer;
170         }
171         /* Otherwise calculate the total span of all screens. */
172         list_for_each_entry(connectori, &vbox->ddev.mode_config.connector_list,
173                             head) {
174                 struct vbox_connector *vbox_connector =
175                     to_vbox_connector(connectori);
176                 struct vbox_crtc *vbox_crtc = vbox_connector->vbox_crtc;
177
178                 width = max_t(u16, width, vbox_crtc->x_hint +
179                                           vbox_connector->mode_hint.width);
180                 height = max_t(u16, height, vbox_crtc->y_hint +
181                                             vbox_connector->mode_hint.height);
182         }
183
184         vbox->single_framebuffer = false;
185         vbox->input_mapping_width = width;
186         vbox->input_mapping_height = height;
187
188         return old_single_framebuffer != vbox->single_framebuffer;
189 }
190
191 static void vbox_crtc_set_base_and_mode(struct drm_crtc *crtc,
192                                         struct drm_framebuffer *fb,
193                                         struct drm_display_mode *mode,
194                                         int x, int y)
195 {
196         struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(fb)->obj);
197         struct vbox_private *vbox = crtc->dev->dev_private;
198         struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
199         bool needs_modeset = drm_atomic_crtc_needs_modeset(crtc->state);
200
201         mutex_lock(&vbox->hw_mutex);
202
203         vbox_crtc->width = mode->hdisplay;
204         vbox_crtc->height = mode->vdisplay;
205         vbox_crtc->x = x;
206         vbox_crtc->y = y;
207         vbox_crtc->fb_offset = vbox_bo_gpu_offset(bo);
208
209         /* vbox_do_modeset() checks vbox->single_framebuffer so update it now */
210         if (needs_modeset && vbox_set_up_input_mapping(vbox)) {
211                 struct drm_crtc *crtci;
212
213                 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list,
214                                     head) {
215                         if (crtci == crtc)
216                                 continue;
217                         vbox_do_modeset(crtci);
218                 }
219         }
220
221         vbox_set_view(crtc);
222         vbox_do_modeset(crtc);
223
224         if (needs_modeset)
225                 hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
226                                            vbox->input_mapping_width,
227                                            vbox->input_mapping_height);
228
229         mutex_unlock(&vbox->hw_mutex);
230 }
231
232 static void vbox_crtc_disable(struct drm_crtc *crtc)
233 {
234 }
235
236 static void vbox_crtc_commit(struct drm_crtc *crtc)
237 {
238 }
239
240 static void vbox_crtc_atomic_flush(struct drm_crtc *crtc,
241                                    struct drm_crtc_state *old_crtc_state)
242 {
243         struct drm_pending_vblank_event *event;
244         unsigned long flags;
245
246         if (crtc->state && crtc->state->event) {
247                 event = crtc->state->event;
248                 crtc->state->event = NULL;
249
250                 spin_lock_irqsave(&crtc->dev->event_lock, flags);
251                 drm_crtc_send_vblank_event(crtc, event);
252                 spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
253         }
254 }
255
256 static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = {
257         .disable = vbox_crtc_disable,
258         .commit = vbox_crtc_commit,
259         .atomic_flush = vbox_crtc_atomic_flush,
260 };
261
262 static void vbox_crtc_destroy(struct drm_crtc *crtc)
263 {
264         drm_crtc_cleanup(crtc);
265         kfree(crtc);
266 }
267
268 static const struct drm_crtc_funcs vbox_crtc_funcs = {
269         .set_config = drm_atomic_helper_set_config,
270         .page_flip = drm_atomic_helper_page_flip,
271         /* .gamma_set = vbox_crtc_gamma_set, */
272         .destroy = vbox_crtc_destroy,
273         .reset = drm_atomic_helper_crtc_reset,
274         .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
275         .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
276 };
277
278 static int vbox_primary_atomic_check(struct drm_plane *plane,
279                                      struct drm_plane_state *new_state)
280 {
281         return 0;
282 }
283
284 static void vbox_primary_atomic_update(struct drm_plane *plane,
285                                        struct drm_plane_state *old_state)
286 {
287         struct drm_crtc *crtc = plane->state->crtc;
288         struct drm_framebuffer *fb = plane->state->fb;
289
290         vbox_crtc_set_base_and_mode(crtc, fb, &crtc->state->mode,
291                                     plane->state->src_x >> 16,
292                                     plane->state->src_y >> 16);
293 }
294
295 void vbox_primary_atomic_disable(struct drm_plane *plane,
296                                  struct drm_plane_state *old_state)
297 {
298         struct drm_crtc *crtc = old_state->crtc;
299
300         /* vbox_do_modeset checks plane->state->fb and will disable if NULL */
301         vbox_crtc_set_base_and_mode(crtc, old_state->fb, &crtc->state->mode,
302                                     old_state->src_x >> 16,
303                                     old_state->src_y >> 16);
304 }
305
306 static int vbox_primary_prepare_fb(struct drm_plane *plane,
307                                    struct drm_plane_state *new_state)
308 {
309         struct vbox_bo *bo;
310         int ret;
311
312         if (!new_state->fb)
313                 return 0;
314
315         bo = gem_to_vbox_bo(to_vbox_framebuffer(new_state->fb)->obj);
316         ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM);
317         if (ret)
318                 DRM_WARN("Error %d pinning new fb, out of video mem?\n", ret);
319
320         return ret;
321 }
322
323 static void vbox_primary_cleanup_fb(struct drm_plane *plane,
324                                     struct drm_plane_state *old_state)
325 {
326         struct vbox_bo *bo;
327
328         if (!old_state->fb)
329                 return;
330
331         bo = gem_to_vbox_bo(to_vbox_framebuffer(old_state->fb)->obj);
332         vbox_bo_unpin(bo);
333 }
334
335 static int vbox_cursor_atomic_check(struct drm_plane *plane,
336                                     struct drm_plane_state *new_state)
337 {
338         u32 width = new_state->crtc_w;
339         u32 height = new_state->crtc_h;
340
341         if (!new_state->fb)
342                 return 0;
343
344         if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT ||
345             width == 0 || height == 0)
346                 return -EINVAL;
347
348         return 0;
349 }
350
351 /**
352  * Copy the ARGB image and generate the mask, which is needed in case the host
353  * does not support ARGB cursors.  The mask is a 1BPP bitmap with the bit set
354  * if the corresponding alpha value in the ARGB image is greater than 0xF0.
355  */
356 static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,
357                               size_t mask_size)
358 {
359         size_t line_size = (width + 7) / 8;
360         u32 i, j;
361
362         memcpy(dst + mask_size, src, width * height * 4);
363         for (i = 0; i < height; ++i)
364                 for (j = 0; j < width; ++j)
365                         if (((u32 *)src)[i * width + j] > 0xf0000000)
366                                 dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
367 }
368
369 static void vbox_cursor_atomic_update(struct drm_plane *plane,
370                                       struct drm_plane_state *old_state)
371 {
372         struct vbox_private *vbox =
373                 container_of(plane->dev, struct vbox_private, ddev);
374         struct vbox_crtc *vbox_crtc = to_vbox_crtc(plane->state->crtc);
375         struct drm_framebuffer *fb = plane->state->fb;
376         struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(fb)->obj);
377         u32 width = plane->state->crtc_w;
378         u32 height = plane->state->crtc_h;
379         size_t data_size, mask_size;
380         u32 flags;
381         u8 *src;
382
383         /*
384          * VirtualBox uses the host windowing system to draw the cursor so
385          * moves are a no-op, we only need to upload new cursor sprites.
386          */
387         if (fb == old_state->fb)
388                 return;
389
390         mutex_lock(&vbox->hw_mutex);
391
392         vbox_crtc->cursor_enabled = true;
393
394         /* pinning is done in prepare/cleanup framebuffer */
395         src = vbox_bo_kmap(bo);
396         if (IS_ERR(src)) {
397                 DRM_WARN("Could not kmap cursor bo, skipping update\n");
398                 return;
399         }
400
401         /*
402          * The mask must be calculated based on the alpha
403          * channel, one bit per ARGB word, and must be 32-bit
404          * padded.
405          */
406         mask_size = ((width + 7) / 8 * height + 3) & ~3;
407         data_size = width * height * 4 + mask_size;
408
409         copy_cursor_image(src, vbox->cursor_data, width, height, mask_size);
410         vbox_bo_kunmap(bo);
411
412         flags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE |
413                 VBOX_MOUSE_POINTER_ALPHA;
414         hgsmi_update_pointer_shape(vbox->guest_pool, flags,
415                                    min_t(u32, max(fb->hot_x, 0), width),
416                                    min_t(u32, max(fb->hot_y, 0), height),
417                                    width, height, vbox->cursor_data, data_size);
418
419         mutex_unlock(&vbox->hw_mutex);
420 }
421
422 void vbox_cursor_atomic_disable(struct drm_plane *plane,
423                                 struct drm_plane_state *old_state)
424 {
425         struct vbox_private *vbox =
426                 container_of(plane->dev, struct vbox_private, ddev);
427         struct vbox_crtc *vbox_crtc = to_vbox_crtc(old_state->crtc);
428         bool cursor_enabled = false;
429         struct drm_crtc *crtci;
430
431         mutex_lock(&vbox->hw_mutex);
432
433         vbox_crtc->cursor_enabled = false;
434
435         list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
436                 if (to_vbox_crtc(crtci)->cursor_enabled)
437                         cursor_enabled = true;
438         }
439
440         if (!cursor_enabled)
441                 hgsmi_update_pointer_shape(vbox->guest_pool, 0, 0, 0,
442                                            0, 0, NULL, 0);
443
444         mutex_unlock(&vbox->hw_mutex);
445 }
446
447 static int vbox_cursor_prepare_fb(struct drm_plane *plane,
448                                   struct drm_plane_state *new_state)
449 {
450         struct vbox_bo *bo;
451
452         if (!new_state->fb)
453                 return 0;
454
455         bo = gem_to_vbox_bo(to_vbox_framebuffer(new_state->fb)->obj);
456         return vbox_bo_pin(bo, TTM_PL_FLAG_SYSTEM);
457 }
458
459 static void vbox_cursor_cleanup_fb(struct drm_plane *plane,
460                                    struct drm_plane_state *old_state)
461 {
462         struct vbox_bo *bo;
463
464         if (!plane->state->fb)
465                 return;
466
467         bo = gem_to_vbox_bo(to_vbox_framebuffer(plane->state->fb)->obj);
468         vbox_bo_unpin(bo);
469 }
470
471 static const uint32_t vbox_cursor_plane_formats[] = {
472         DRM_FORMAT_ARGB8888,
473 };
474
475 static const struct drm_plane_helper_funcs vbox_cursor_helper_funcs = {
476         .atomic_check   = vbox_cursor_atomic_check,
477         .atomic_update  = vbox_cursor_atomic_update,
478         .atomic_disable = vbox_cursor_atomic_disable,
479         .prepare_fb     = vbox_cursor_prepare_fb,
480         .cleanup_fb     = vbox_cursor_cleanup_fb,
481 };
482
483 static const struct drm_plane_funcs vbox_cursor_plane_funcs = {
484         .update_plane   = drm_atomic_helper_update_plane,
485         .disable_plane  = drm_atomic_helper_disable_plane,
486         .destroy        = drm_primary_helper_destroy,
487         .reset          = drm_atomic_helper_plane_reset,
488         .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
489         .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
490 };
491
492 static const uint32_t vbox_primary_plane_formats[] = {
493         DRM_FORMAT_XRGB8888,
494         DRM_FORMAT_ARGB8888,
495 };
496
497 static const struct drm_plane_helper_funcs vbox_primary_helper_funcs = {
498         .atomic_check = vbox_primary_atomic_check,
499         .atomic_update = vbox_primary_atomic_update,
500         .atomic_disable = vbox_primary_atomic_disable,
501         .prepare_fb = vbox_primary_prepare_fb,
502         .cleanup_fb = vbox_primary_cleanup_fb,
503 };
504
505 static const struct drm_plane_funcs vbox_primary_plane_funcs = {
506         .update_plane   = drm_atomic_helper_update_plane,
507         .disable_plane  = drm_atomic_helper_disable_plane,
508         .destroy        = drm_primary_helper_destroy,
509         .reset          = drm_atomic_helper_plane_reset,
510         .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
511         .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
512 };
513
514 static struct drm_plane *vbox_create_plane(struct vbox_private *vbox,
515                                            unsigned int possible_crtcs,
516                                            enum drm_plane_type type)
517 {
518         const struct drm_plane_helper_funcs *helper_funcs = NULL;
519         const struct drm_plane_funcs *funcs;
520         struct drm_plane *plane;
521         const uint32_t *formats;
522         int num_formats;
523         int err;
524
525         if (type == DRM_PLANE_TYPE_PRIMARY) {
526                 funcs = &vbox_primary_plane_funcs;
527                 formats = vbox_primary_plane_formats;
528                 helper_funcs = &vbox_primary_helper_funcs;
529                 num_formats = ARRAY_SIZE(vbox_primary_plane_formats);
530         } else if (type == DRM_PLANE_TYPE_CURSOR) {
531                 funcs = &vbox_cursor_plane_funcs;
532                 formats = vbox_cursor_plane_formats;
533                 helper_funcs = &vbox_cursor_helper_funcs;
534                 num_formats = ARRAY_SIZE(vbox_cursor_plane_formats);
535         } else {
536                 return ERR_PTR(-EINVAL);
537         }
538
539         plane = kzalloc(sizeof(*plane), GFP_KERNEL);
540         if (!plane)
541                 return ERR_PTR(-ENOMEM);
542
543         err = drm_universal_plane_init(&vbox->ddev, plane, possible_crtcs,
544                                        funcs, formats, num_formats,
545                                        NULL, type, NULL);
546         if (err)
547                 goto free_plane;
548
549         drm_plane_helper_add(plane, helper_funcs);
550
551         return plane;
552
553 free_plane:
554         kfree(plane);
555         return ERR_PTR(-EINVAL);
556 }
557
558 static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned int i)
559 {
560         struct vbox_private *vbox =
561                 container_of(dev, struct vbox_private, ddev);
562         struct drm_plane *cursor = NULL;
563         struct vbox_crtc *vbox_crtc;
564         struct drm_plane *primary;
565         u32 caps = 0;
566         int ret;
567
568         ret = hgsmi_query_conf(vbox->guest_pool,
569                                VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
570         if (ret)
571                 return ERR_PTR(ret);
572
573         vbox_crtc = kzalloc(sizeof(*vbox_crtc), GFP_KERNEL);
574         if (!vbox_crtc)
575                 return ERR_PTR(-ENOMEM);
576
577         primary = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_PRIMARY);
578         if (IS_ERR(primary)) {
579                 ret = PTR_ERR(primary);
580                 goto free_mem;
581         }
582
583         if ((caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)) {
584                 cursor = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_CURSOR);
585                 if (IS_ERR(cursor)) {
586                         ret = PTR_ERR(cursor);
587                         goto clean_primary;
588                 }
589         } else {
590                 DRM_WARN("VirtualBox host is too old, no cursor support\n");
591         }
592
593         vbox_crtc->crtc_id = i;
594
595         ret = drm_crtc_init_with_planes(dev, &vbox_crtc->base, primary, cursor,
596                                         &vbox_crtc_funcs, NULL);
597         if (ret)
598                 goto clean_cursor;
599
600         drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256);
601         drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs);
602
603         return vbox_crtc;
604
605 clean_cursor:
606         if (cursor) {
607                 drm_plane_cleanup(cursor);
608                 kfree(cursor);
609         }
610 clean_primary:
611         drm_plane_cleanup(primary);
612         kfree(primary);
613 free_mem:
614         kfree(vbox_crtc);
615         return ERR_PTR(ret);
616 }
617
618 static void vbox_encoder_destroy(struct drm_encoder *encoder)
619 {
620         drm_encoder_cleanup(encoder);
621         kfree(encoder);
622 }
623
624 static struct drm_encoder *vbox_best_single_encoder(struct drm_connector
625                                                     *connector)
626 {
627         int enc_id = connector->encoder_ids[0];
628
629         /* pick the encoder ids */
630         if (enc_id)
631                 return drm_encoder_find(connector->dev, NULL, enc_id);
632
633         return NULL;
634 }
635
636 static const struct drm_encoder_funcs vbox_enc_funcs = {
637         .destroy = vbox_encoder_destroy,
638 };
639
640 static struct drm_encoder *vbox_encoder_init(struct drm_device *dev,
641                                              unsigned int i)
642 {
643         struct vbox_encoder *vbox_encoder;
644
645         vbox_encoder = kzalloc(sizeof(*vbox_encoder), GFP_KERNEL);
646         if (!vbox_encoder)
647                 return NULL;
648
649         drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
650                          DRM_MODE_ENCODER_DAC, NULL);
651
652         vbox_encoder->base.possible_crtcs = 1 << i;
653         return &vbox_encoder->base;
654 }
655
656 /**
657  * Generate EDID data with a mode-unique serial number for the virtual
658  *  monitor to try to persuade Unity that different modes correspond to
659  *  different monitors and it should not try to force the same resolution on
660  *  them.
661  */
662 static void vbox_set_edid(struct drm_connector *connector, int width,
663                           int height)
664 {
665         enum { EDID_SIZE = 128 };
666         unsigned char edid[EDID_SIZE] = {
667                 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
668                 0x58, 0x58,     /* manufacturer (VBX) */
669                 0x00, 0x00,     /* product code */
670                 0x00, 0x00, 0x00, 0x00, /* serial number goes here */
671                 0x01,           /* week of manufacture */
672                 0x00,           /* year of manufacture */
673                 0x01, 0x03,     /* EDID version */
674                 0x80,           /* capabilities - digital */
675                 0x00,           /* horiz. res in cm, zero for projectors */
676                 0x00,           /* vert. res in cm */
677                 0x78,           /* display gamma (120 == 2.2). */
678                 0xEE,           /* features (standby, suspend, off, RGB, std */
679                                 /* colour space, preferred timing mode) */
680                 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
681                 /* chromaticity for standard colour space. */
682                 0x00, 0x00, 0x00,       /* no default timings */
683                 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
684                     0x01, 0x01,
685                 0x01, 0x01, 0x01, 0x01, /* no standard timings */
686                 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
687                     0x02, 0x02,
688                 /* descriptor block 1 goes below */
689                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
690                 /* descriptor block 2, monitor ranges */
691                 0x00, 0x00, 0x00, 0xFD, 0x00,
692                 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
693                     0x20, 0x20,
694                 /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
695                 0x20,
696                 /* descriptor block 3, monitor name */
697                 0x00, 0x00, 0x00, 0xFC, 0x00,
698                 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
699                 '\n',
700                 /* descriptor block 4: dummy data */
701                 0x00, 0x00, 0x00, 0x10, 0x00,
702                 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
703                 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
704                 0x20,
705                 0x00,           /* number of extensions */
706                 0x00            /* checksum goes here */
707         };
708         int clock = (width + 6) * (height + 6) * 60 / 10000;
709         unsigned int i, sum = 0;
710
711         edid[12] = width & 0xff;
712         edid[13] = width >> 8;
713         edid[14] = height & 0xff;
714         edid[15] = height >> 8;
715         edid[54] = clock & 0xff;
716         edid[55] = clock >> 8;
717         edid[56] = width & 0xff;
718         edid[58] = (width >> 4) & 0xf0;
719         edid[59] = height & 0xff;
720         edid[61] = (height >> 4) & 0xf0;
721         for (i = 0; i < EDID_SIZE - 1; ++i)
722                 sum += edid[i];
723         edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF;
724         drm_connector_update_edid_property(connector, (struct edid *)edid);
725 }
726
727 static int vbox_get_modes(struct drm_connector *connector)
728 {
729         struct vbox_connector *vbox_connector = NULL;
730         struct drm_display_mode *mode = NULL;
731         struct vbox_private *vbox = NULL;
732         unsigned int num_modes = 0;
733         int preferred_width, preferred_height;
734
735         vbox_connector = to_vbox_connector(connector);
736         vbox = connector->dev->dev_private;
737         /*
738          * Heuristic: we do not want to tell the host that we support dynamic
739          * resizing unless we feel confident that the user space client using
740          * the video driver can handle hot-plug events.  So the first time modes
741          * are queried after a "master" switch we tell the host that we do not,
742          * and immediately after we send the client a hot-plug notification as
743          * a test to see if they will respond and query again.
744          * That is also the reason why capabilities are reported to the host at
745          * this place in the code rather than elsewhere.
746          * We need to report the flags location before reporting the IRQ
747          * capability.
748          */
749         hgsmi_report_flags_location(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) +
750                                     HOST_FLAGS_OFFSET);
751         if (vbox_connector->vbox_crtc->crtc_id == 0)
752                 vbox_report_caps(vbox);
753         if (!vbox->initial_mode_queried) {
754                 if (vbox_connector->vbox_crtc->crtc_id == 0) {
755                         vbox->initial_mode_queried = true;
756                         vbox_report_hotplug(vbox);
757                 }
758                 return drm_add_modes_noedid(connector, 800, 600);
759         }
760         num_modes = drm_add_modes_noedid(connector, 2560, 1600);
761         preferred_width = vbox_connector->mode_hint.width ?
762                           vbox_connector->mode_hint.width : 1024;
763         preferred_height = vbox_connector->mode_hint.height ?
764                            vbox_connector->mode_hint.height : 768;
765         mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height,
766                             60, false, false, false);
767         if (mode) {
768                 mode->type |= DRM_MODE_TYPE_PREFERRED;
769                 drm_mode_probed_add(connector, mode);
770                 ++num_modes;
771         }
772         vbox_set_edid(connector, preferred_width, preferred_height);
773
774         if (vbox_connector->vbox_crtc->x_hint != -1)
775                 drm_object_property_set_value(&connector->base,
776                         vbox->ddev.mode_config.suggested_x_property,
777                         vbox_connector->vbox_crtc->x_hint);
778         else
779                 drm_object_property_set_value(&connector->base,
780                         vbox->ddev.mode_config.suggested_x_property, 0);
781
782         if (vbox_connector->vbox_crtc->y_hint != -1)
783                 drm_object_property_set_value(&connector->base,
784                         vbox->ddev.mode_config.suggested_y_property,
785                         vbox_connector->vbox_crtc->y_hint);
786         else
787                 drm_object_property_set_value(&connector->base,
788                         vbox->ddev.mode_config.suggested_y_property, 0);
789
790         return num_modes;
791 }
792
793 static enum drm_mode_status vbox_mode_valid(struct drm_connector *connector,
794                            struct drm_display_mode *mode)
795 {
796         return MODE_OK;
797 }
798
799 static void vbox_connector_destroy(struct drm_connector *connector)
800 {
801         drm_connector_unregister(connector);
802         drm_connector_cleanup(connector);
803         kfree(connector);
804 }
805
806 static enum drm_connector_status
807 vbox_connector_detect(struct drm_connector *connector, bool force)
808 {
809         struct vbox_connector *vbox_connector;
810
811         vbox_connector = to_vbox_connector(connector);
812
813         return vbox_connector->mode_hint.disconnected ?
814             connector_status_disconnected : connector_status_connected;
815 }
816
817 static int vbox_fill_modes(struct drm_connector *connector, u32 max_x,
818                            u32 max_y)
819 {
820         struct vbox_connector *vbox_connector;
821         struct drm_device *dev;
822         struct drm_display_mode *mode, *iterator;
823
824         vbox_connector = to_vbox_connector(connector);
825         dev = vbox_connector->base.dev;
826         list_for_each_entry_safe(mode, iterator, &connector->modes, head) {
827                 list_del(&mode->head);
828                 drm_mode_destroy(dev, mode);
829         }
830
831         return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
832 }
833
834 static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
835         .mode_valid = vbox_mode_valid,
836         .get_modes = vbox_get_modes,
837         .best_encoder = vbox_best_single_encoder,
838 };
839
840 static const struct drm_connector_funcs vbox_connector_funcs = {
841         .detect = vbox_connector_detect,
842         .fill_modes = vbox_fill_modes,
843         .destroy = vbox_connector_destroy,
844         .reset = drm_atomic_helper_connector_reset,
845         .atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
846         .atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
847 };
848
849 static int vbox_connector_init(struct drm_device *dev,
850                                struct vbox_crtc *vbox_crtc,
851                                struct drm_encoder *encoder)
852 {
853         struct vbox_connector *vbox_connector;
854         struct drm_connector *connector;
855
856         vbox_connector = kzalloc(sizeof(*vbox_connector), GFP_KERNEL);
857         if (!vbox_connector)
858                 return -ENOMEM;
859
860         connector = &vbox_connector->base;
861         vbox_connector->vbox_crtc = vbox_crtc;
862
863         drm_connector_init(dev, connector, &vbox_connector_funcs,
864                            DRM_MODE_CONNECTOR_VGA);
865         drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
866
867         connector->interlace_allowed = 0;
868         connector->doublescan_allowed = 0;
869
870         drm_mode_create_suggested_offset_properties(dev);
871         drm_object_attach_property(&connector->base,
872                                    dev->mode_config.suggested_x_property, 0);
873         drm_object_attach_property(&connector->base,
874                                    dev->mode_config.suggested_y_property, 0);
875
876         drm_connector_attach_encoder(connector, encoder);
877
878         return 0;
879 }
880
881 static struct drm_framebuffer *vbox_user_framebuffer_create(
882                 struct drm_device *dev,
883                 struct drm_file *filp,
884                 const struct drm_mode_fb_cmd2 *mode_cmd)
885 {
886         struct vbox_private *vbox =
887                 container_of(dev, struct vbox_private, ddev);
888         struct drm_gem_object *obj;
889         struct vbox_framebuffer *vbox_fb;
890         int ret = -ENOMEM;
891
892         obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
893         if (!obj)
894                 return ERR_PTR(-ENOENT);
895
896         vbox_fb = kzalloc(sizeof(*vbox_fb), GFP_KERNEL);
897         if (!vbox_fb)
898                 goto err_unref_obj;
899
900         ret = vbox_framebuffer_init(vbox, vbox_fb, mode_cmd, obj);
901         if (ret)
902                 goto err_free_vbox_fb;
903
904         return &vbox_fb->base;
905
906 err_free_vbox_fb:
907         kfree(vbox_fb);
908 err_unref_obj:
909         drm_gem_object_put_unlocked(obj);
910         return ERR_PTR(ret);
911 }
912
913 static const struct drm_mode_config_funcs vbox_mode_funcs = {
914         .fb_create = vbox_user_framebuffer_create,
915         .atomic_check = drm_atomic_helper_check,
916         .atomic_commit = drm_atomic_helper_commit,
917 };
918
919 int vbox_mode_init(struct vbox_private *vbox)
920 {
921         struct drm_device *dev = &vbox->ddev;
922         struct drm_encoder *encoder;
923         struct vbox_crtc *vbox_crtc;
924         unsigned int i;
925         int ret;
926
927         drm_mode_config_init(dev);
928
929         dev->mode_config.funcs = (void *)&vbox_mode_funcs;
930         dev->mode_config.min_width = 0;
931         dev->mode_config.min_height = 0;
932         dev->mode_config.preferred_depth = 24;
933         dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
934         dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
935
936         for (i = 0; i < vbox->num_crtcs; ++i) {
937                 vbox_crtc = vbox_crtc_init(dev, i);
938                 if (IS_ERR(vbox_crtc)) {
939                         ret = PTR_ERR(vbox_crtc);
940                         goto err_drm_mode_cleanup;
941                 }
942                 encoder = vbox_encoder_init(dev, i);
943                 if (!encoder) {
944                         ret = -ENOMEM;
945                         goto err_drm_mode_cleanup;
946                 }
947                 ret = vbox_connector_init(dev, vbox_crtc, encoder);
948                 if (ret)
949                         goto err_drm_mode_cleanup;
950         }
951
952         drm_mode_config_reset(dev);
953         return 0;
954
955 err_drm_mode_cleanup:
956         drm_mode_config_cleanup(dev);
957         return ret;
958 }
959
960 void vbox_mode_fini(struct vbox_private *vbox)
961 {
962         drm_mode_config_cleanup(&vbox->ddev);
963 }