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