OSDN Git Service

staging: vboxvideo: Cleanup vbox_set_up_input_mapping()
[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_crtc_helper.h>
36 #include <drm/drm_plane_helper.h>
37
38 #include "vbox_drv.h"
39 #include "vboxvideo.h"
40 #include "hgsmi_channels.h"
41
42 static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
43                             u32 handle, u32 width, u32 height,
44                             s32 hot_x, s32 hot_y);
45 static int vbox_cursor_move(struct drm_crtc *crtc, int x, int y);
46
47 /**
48  * Set a graphics mode.  Poke any required values into registers, do an HGSMI
49  * mode set and tell the host we support advanced graphics functions.
50  */
51 static void vbox_do_modeset(struct drm_crtc *crtc,
52                             const struct drm_display_mode *mode)
53 {
54         struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
55         struct vbox_private *vbox;
56         int width, height, bpp, pitch;
57         u16 flags;
58         s32 x_offset, y_offset;
59
60         vbox = crtc->dev->dev_private;
61         width = mode->hdisplay ? mode->hdisplay : 640;
62         height = mode->vdisplay ? mode->vdisplay : 480;
63         bpp = crtc->enabled ? CRTC_FB(crtc)->format->cpp[0] * 8 : 32;
64         pitch = crtc->enabled ? CRTC_FB(crtc)->pitches[0] : width * bpp / 8;
65         x_offset = vbox->single_framebuffer ? crtc->x : vbox_crtc->x_hint;
66         y_offset = vbox->single_framebuffer ? crtc->y : vbox_crtc->y_hint;
67
68         /*
69          * This is the old way of setting graphics modes.  It assumed one screen
70          * and a frame-buffer at the start of video RAM.  On older versions of
71          * VirtualBox, certain parts of the code still assume that the first
72          * screen is programmed this way, so try to fake it.
73          */
74         if (vbox_crtc->crtc_id == 0 && crtc->enabled &&
75             vbox_crtc->fb_offset / pitch < 0xffff - crtc->y &&
76             vbox_crtc->fb_offset % (bpp / 8) == 0) {
77                 vbox_write_ioport(VBE_DISPI_INDEX_XRES, width);
78                 vbox_write_ioport(VBE_DISPI_INDEX_YRES, height);
79                 vbox_write_ioport(VBE_DISPI_INDEX_VIRT_WIDTH, pitch * 8 / bpp);
80                 vbox_write_ioport(VBE_DISPI_INDEX_BPP,
81                                   CRTC_FB(crtc)->format->cpp[0] * 8);
82                 vbox_write_ioport(VBE_DISPI_INDEX_ENABLE, VBE_DISPI_ENABLED);
83                 vbox_write_ioport(
84                         VBE_DISPI_INDEX_X_OFFSET,
85                         vbox_crtc->fb_offset % pitch / bpp * 8 + crtc->x);
86                 vbox_write_ioport(VBE_DISPI_INDEX_Y_OFFSET,
87                                   vbox_crtc->fb_offset / pitch + crtc->y);
88         }
89
90         flags = VBVA_SCREEN_F_ACTIVE;
91         flags |= (crtc->enabled && !vbox_crtc->blanked) ?
92                  0 : VBVA_SCREEN_F_BLANK;
93         flags |= vbox_crtc->disconnected ? VBVA_SCREEN_F_DISABLED : 0;
94         hgsmi_process_display_info(vbox->guest_pool, vbox_crtc->crtc_id,
95                                    x_offset, y_offset,
96                                    crtc->x * bpp / 8 + crtc->y * pitch,
97                                    pitch, width, height,
98                                    vbox_crtc->blanked ? 0 : bpp, flags);
99 }
100
101 static int vbox_set_view(struct drm_crtc *crtc)
102 {
103         struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
104         struct vbox_private *vbox = crtc->dev->dev_private;
105         struct vbva_infoview *p;
106
107         /*
108          * Tell the host about the view.  This design originally targeted the
109          * Windows XP driver architecture and assumed that each screen would
110          * have a dedicated frame buffer with the command buffer following it,
111          * the whole being a "view".  The host works out which screen a command
112          * buffer belongs to by checking whether it is in the first view, then
113          * whether it is in the second and so on.  The first match wins.  We
114          * cheat around this by making the first view be the managed memory
115          * plus the first command buffer, the second the same plus the second
116          * buffer and so on.
117          */
118         p = hgsmi_buffer_alloc(vbox->guest_pool, sizeof(*p),
119                                HGSMI_CH_VBVA, VBVA_INFO_VIEW);
120         if (!p)
121                 return -ENOMEM;
122
123         p->view_index = vbox_crtc->crtc_id;
124         p->view_offset = vbox_crtc->fb_offset;
125         p->view_size = vbox->available_vram_size - vbox_crtc->fb_offset +
126                        vbox_crtc->crtc_id * VBVA_MIN_BUFFER_SIZE;
127         p->max_screen_size = vbox->available_vram_size - vbox_crtc->fb_offset;
128
129         hgsmi_buffer_submit(vbox->guest_pool, p);
130         hgsmi_buffer_free(vbox->guest_pool, p);
131
132         return 0;
133 }
134
135 static void vbox_crtc_dpms(struct drm_crtc *crtc, int mode)
136 {
137         struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
138         struct vbox_private *vbox = crtc->dev->dev_private;
139
140         switch (mode) {
141         case DRM_MODE_DPMS_ON:
142                 vbox_crtc->blanked = false;
143                 break;
144         case DRM_MODE_DPMS_STANDBY:
145         case DRM_MODE_DPMS_SUSPEND:
146         case DRM_MODE_DPMS_OFF:
147                 vbox_crtc->blanked = true;
148                 break;
149         }
150
151         mutex_lock(&vbox->hw_mutex);
152         vbox_do_modeset(crtc, &crtc->hwmode);
153         mutex_unlock(&vbox->hw_mutex);
154 }
155
156 static bool vbox_crtc_mode_fixup(struct drm_crtc *crtc,
157                                  const struct drm_display_mode *mode,
158                                  struct drm_display_mode *adjusted_mode)
159 {
160         return true;
161 }
162
163 /*
164  * Try to map the layout of virtual screens to the range of the input device.
165  * Return true if we need to re-set the crtc modes due to screen offset
166  * changes.
167  */
168 static bool vbox_set_up_input_mapping(struct vbox_private *vbox)
169 {
170         struct drm_crtc *crtci;
171         struct drm_connector *connectori;
172         struct drm_framebuffer *fb, *fb1 = NULL;
173         bool single_framebuffer = true;
174         bool old_single_framebuffer = vbox->single_framebuffer;
175         u16 width = 0, height = 0;
176
177         /*
178          * Are we using an X.Org-style single large frame-buffer for all crtcs?
179          * If so then screen layout can be deduced from the crtc offsets.
180          * Same fall-back if this is the fbdev frame-buffer.
181          */
182         list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list, head) {
183                 fb = CRTC_FB(crtci);
184                 if (!fb)
185                         continue;
186
187                 if (!fb1) {
188                         fb1 = fb;
189                         if (to_vbox_framebuffer(fb1) == &vbox->fbdev->afb)
190                                 break;
191                 } else if (fb != fb1) {
192                         single_framebuffer = false;
193                 }
194         }
195         if (!fb1)
196                 return false;
197
198         if (single_framebuffer) {
199                 vbox->single_framebuffer = true;
200                 vbox->input_mapping_width = fb1->width;
201                 vbox->input_mapping_height = fb1->height;
202                 return old_single_framebuffer != vbox->single_framebuffer;
203         }
204         /* Otherwise calculate the total span of all screens. */
205         list_for_each_entry(connectori, &vbox->ddev.mode_config.connector_list,
206                             head) {
207                 struct vbox_connector *vbox_connector =
208                     to_vbox_connector(connectori);
209                 struct vbox_crtc *vbox_crtc = vbox_connector->vbox_crtc;
210
211                 width = max_t(u16, width, vbox_crtc->x_hint +
212                                           vbox_connector->mode_hint.width);
213                 height = max_t(u16, height, vbox_crtc->y_hint +
214                                             vbox_connector->mode_hint.height);
215         }
216
217         vbox->single_framebuffer = false;
218         vbox->input_mapping_width = width;
219         vbox->input_mapping_height = height;
220
221         return old_single_framebuffer != vbox->single_framebuffer;
222 }
223
224 static void vbox_crtc_set_base_and_mode(struct drm_crtc *crtc,
225                                         struct drm_framebuffer *fb,
226                                         struct drm_display_mode *mode,
227                                         int x, int y)
228 {
229         struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(fb)->obj);
230         struct vbox_private *vbox = crtc->dev->dev_private;
231         struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
232
233         mutex_lock(&vbox->hw_mutex);
234
235         vbox_crtc->fb_offset = vbox_bo_gpu_offset(bo);
236
237         /* vbox_do_modeset() checks vbox->single_framebuffer so update it now */
238         if (mode && vbox_set_up_input_mapping(vbox)) {
239                 struct drm_crtc *crtci;
240
241                 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list,
242                                     head) {
243                         if (crtci == crtc)
244                                 continue;
245                         vbox_do_modeset(crtci, &crtci->mode);
246                 }
247         }
248
249         vbox_set_view(crtc);
250         vbox_do_modeset(crtc, mode ? mode : &crtc->mode);
251
252         if (mode)
253                 hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
254                                            vbox->input_mapping_width,
255                                            vbox->input_mapping_height);
256
257         mutex_unlock(&vbox->hw_mutex);
258 }
259
260 static int vbox_crtc_mode_set(struct drm_crtc *crtc,
261                               struct drm_display_mode *mode,
262                               struct drm_display_mode *adjusted_mode,
263                               int x, int y, struct drm_framebuffer *old_fb)
264 {
265         struct drm_framebuffer *new_fb = CRTC_FB(crtc);
266         struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(new_fb)->obj);
267         int ret;
268
269         ret = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM);
270         if (ret) {
271                 DRM_WARN("Error %d pinning new fb, out of video mem?\n", ret);
272                 return ret;
273         }
274
275         vbox_crtc_set_base_and_mode(crtc, new_fb, mode, x, y);
276
277         if (old_fb) {
278                 bo = gem_to_vbox_bo(to_vbox_framebuffer(old_fb)->obj);
279                 vbox_bo_unpin(bo);
280         }
281
282         return 0;
283 }
284
285 static int vbox_crtc_page_flip(struct drm_crtc *crtc,
286                                struct drm_framebuffer *fb,
287                                struct drm_pending_vblank_event *event,
288                                uint32_t page_flip_flags,
289                                struct drm_modeset_acquire_ctx *ctx)
290 {
291         struct vbox_bo *bo = gem_to_vbox_bo(to_vbox_framebuffer(fb)->obj);
292         struct drm_framebuffer *old_fb = CRTC_FB(crtc);
293         unsigned long flags;
294         int rc;
295
296         rc = vbox_bo_pin(bo, TTM_PL_FLAG_VRAM);
297         if (rc) {
298                 DRM_WARN("Error %d pinning new fb, out of video mem?\n", rc);
299                 return rc;
300         }
301
302         vbox_crtc_set_base_and_mode(crtc, fb, NULL, 0, 0);
303
304         if (old_fb) {
305                 bo = gem_to_vbox_bo(to_vbox_framebuffer(old_fb)->obj);
306                 vbox_bo_unpin(bo);
307         }
308
309         spin_lock_irqsave(&crtc->dev->event_lock, flags);
310
311         if (event)
312                 drm_crtc_send_vblank_event(crtc, event);
313
314         spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
315
316         return 0;
317 }
318
319 static void vbox_crtc_disable(struct drm_crtc *crtc)
320 {
321 }
322
323 static void vbox_crtc_prepare(struct drm_crtc *crtc)
324 {
325 }
326
327 static void vbox_crtc_commit(struct drm_crtc *crtc)
328 {
329 }
330
331 static const struct drm_crtc_helper_funcs vbox_crtc_helper_funcs = {
332         .dpms = vbox_crtc_dpms,
333         .mode_fixup = vbox_crtc_mode_fixup,
334         .mode_set = vbox_crtc_mode_set,
335         .disable = vbox_crtc_disable,
336         .prepare = vbox_crtc_prepare,
337         .commit = vbox_crtc_commit,
338 };
339
340 static void vbox_crtc_reset(struct drm_crtc *crtc)
341 {
342 }
343
344 static void vbox_crtc_destroy(struct drm_crtc *crtc)
345 {
346         drm_crtc_cleanup(crtc);
347         kfree(crtc);
348 }
349
350 static const struct drm_crtc_funcs vbox_crtc_funcs = {
351         .cursor_move = vbox_cursor_move,
352         .cursor_set2 = vbox_cursor_set2,
353         .reset = vbox_crtc_reset,
354         .set_config = drm_crtc_helper_set_config,
355         /* .gamma_set = vbox_crtc_gamma_set, */
356         .page_flip = vbox_crtc_page_flip,
357         .destroy = vbox_crtc_destroy,
358 };
359
360 static const uint32_t vbox_primary_plane_formats[] = {
361         DRM_FORMAT_XRGB8888,
362         DRM_FORMAT_ARGB8888,
363 };
364
365 static const struct drm_plane_funcs vbox_primary_plane_funcs = {
366         .update_plane   = drm_primary_helper_update,
367         .disable_plane  = drm_primary_helper_disable,
368         .destroy        = drm_primary_helper_destroy,
369 };
370
371 static struct drm_plane *vbox_create_plane(struct vbox_private *vbox,
372                                            unsigned int possible_crtcs,
373                                            enum drm_plane_type type)
374 {
375         const struct drm_plane_helper_funcs *helper_funcs = NULL;
376         const struct drm_plane_funcs *funcs;
377         struct drm_plane *plane;
378         const uint32_t *formats;
379         int num_formats;
380         int err;
381
382         if (type == DRM_PLANE_TYPE_PRIMARY) {
383                 funcs = &vbox_primary_plane_funcs;
384                 formats = vbox_primary_plane_formats;
385                 num_formats = ARRAY_SIZE(vbox_primary_plane_formats);
386         } else {
387                 return ERR_PTR(-EINVAL);
388         }
389
390         plane = kzalloc(sizeof(*plane), GFP_KERNEL);
391         if (!plane)
392                 return ERR_PTR(-ENOMEM);
393
394         err = drm_universal_plane_init(&vbox->ddev, plane, possible_crtcs,
395                                        funcs, formats, num_formats,
396                                        NULL, type, NULL);
397         if (err)
398                 goto free_plane;
399
400         drm_plane_helper_add(plane, helper_funcs);
401
402         return plane;
403
404 free_plane:
405         kfree(plane);
406         return ERR_PTR(-EINVAL);
407 }
408
409 static struct vbox_crtc *vbox_crtc_init(struct drm_device *dev, unsigned int i)
410 {
411         struct vbox_private *vbox =
412                 container_of(dev, struct vbox_private, ddev);
413         struct vbox_crtc *vbox_crtc;
414         struct drm_plane *primary;
415         int ret;
416
417         vbox_crtc = kzalloc(sizeof(*vbox_crtc), GFP_KERNEL);
418         if (!vbox_crtc)
419                 return ERR_PTR(-ENOMEM);
420
421         primary = vbox_create_plane(vbox, 1 << i, DRM_PLANE_TYPE_PRIMARY);
422         if (IS_ERR(primary)) {
423                 ret = PTR_ERR(primary);
424                 goto free_mem;
425         }
426
427         vbox_crtc->crtc_id = i;
428
429         ret = drm_crtc_init_with_planes(dev, &vbox_crtc->base, primary, NULL,
430                                         &vbox_crtc_funcs, NULL);
431         if (ret)
432                 goto clean_primary;
433
434         drm_mode_crtc_set_gamma_size(&vbox_crtc->base, 256);
435         drm_crtc_helper_add(&vbox_crtc->base, &vbox_crtc_helper_funcs);
436
437         return vbox_crtc;
438
439 clean_primary:
440         drm_plane_cleanup(primary);
441         kfree(primary);
442 free_mem:
443         kfree(vbox_crtc);
444         return ERR_PTR(ret);
445 }
446
447 static void vbox_encoder_destroy(struct drm_encoder *encoder)
448 {
449         drm_encoder_cleanup(encoder);
450         kfree(encoder);
451 }
452
453 static struct drm_encoder *vbox_best_single_encoder(struct drm_connector
454                                                     *connector)
455 {
456         int enc_id = connector->encoder_ids[0];
457
458         /* pick the encoder ids */
459         if (enc_id)
460                 return drm_encoder_find(connector->dev, NULL, enc_id);
461
462         return NULL;
463 }
464
465 static const struct drm_encoder_funcs vbox_enc_funcs = {
466         .destroy = vbox_encoder_destroy,
467 };
468
469 static void vbox_encoder_dpms(struct drm_encoder *encoder, int mode)
470 {
471 }
472
473 static bool vbox_mode_fixup(struct drm_encoder *encoder,
474                             const struct drm_display_mode *mode,
475                             struct drm_display_mode *adjusted_mode)
476 {
477         return true;
478 }
479
480 static void vbox_encoder_mode_set(struct drm_encoder *encoder,
481                                   struct drm_display_mode *mode,
482                                   struct drm_display_mode *adjusted_mode)
483 {
484 }
485
486 static void vbox_encoder_prepare(struct drm_encoder *encoder)
487 {
488 }
489
490 static void vbox_encoder_commit(struct drm_encoder *encoder)
491 {
492 }
493
494 static const struct drm_encoder_helper_funcs vbox_enc_helper_funcs = {
495         .dpms = vbox_encoder_dpms,
496         .mode_fixup = vbox_mode_fixup,
497         .prepare = vbox_encoder_prepare,
498         .commit = vbox_encoder_commit,
499         .mode_set = vbox_encoder_mode_set,
500 };
501
502 static struct drm_encoder *vbox_encoder_init(struct drm_device *dev,
503                                              unsigned int i)
504 {
505         struct vbox_encoder *vbox_encoder;
506
507         vbox_encoder = kzalloc(sizeof(*vbox_encoder), GFP_KERNEL);
508         if (!vbox_encoder)
509                 return NULL;
510
511         drm_encoder_init(dev, &vbox_encoder->base, &vbox_enc_funcs,
512                          DRM_MODE_ENCODER_DAC, NULL);
513         drm_encoder_helper_add(&vbox_encoder->base, &vbox_enc_helper_funcs);
514
515         vbox_encoder->base.possible_crtcs = 1 << i;
516         return &vbox_encoder->base;
517 }
518
519 /**
520  * Generate EDID data with a mode-unique serial number for the virtual
521  *  monitor to try to persuade Unity that different modes correspond to
522  *  different monitors and it should not try to force the same resolution on
523  *  them.
524  */
525 static void vbox_set_edid(struct drm_connector *connector, int width,
526                           int height)
527 {
528         enum { EDID_SIZE = 128 };
529         unsigned char edid[EDID_SIZE] = {
530                 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, /* header */
531                 0x58, 0x58,     /* manufacturer (VBX) */
532                 0x00, 0x00,     /* product code */
533                 0x00, 0x00, 0x00, 0x00, /* serial number goes here */
534                 0x01,           /* week of manufacture */
535                 0x00,           /* year of manufacture */
536                 0x01, 0x03,     /* EDID version */
537                 0x80,           /* capabilities - digital */
538                 0x00,           /* horiz. res in cm, zero for projectors */
539                 0x00,           /* vert. res in cm */
540                 0x78,           /* display gamma (120 == 2.2). */
541                 0xEE,           /* features (standby, suspend, off, RGB, std */
542                                 /* colour space, preferred timing mode) */
543                 0xEE, 0x91, 0xA3, 0x54, 0x4C, 0x99, 0x26, 0x0F, 0x50, 0x54,
544                 /* chromaticity for standard colour space. */
545                 0x00, 0x00, 0x00,       /* no default timings */
546                 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
547                     0x01, 0x01,
548                 0x01, 0x01, 0x01, 0x01, /* no standard timings */
549                 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x02, 0x02,
550                     0x02, 0x02,
551                 /* descriptor block 1 goes below */
552                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
553                 /* descriptor block 2, monitor ranges */
554                 0x00, 0x00, 0x00, 0xFD, 0x00,
555                 0x00, 0xC8, 0x00, 0xC8, 0x64, 0x00, 0x0A, 0x20, 0x20, 0x20,
556                     0x20, 0x20,
557                 /* 0-200Hz vertical, 0-200KHz horizontal, 1000MHz pixel clock */
558                 0x20,
559                 /* descriptor block 3, monitor name */
560                 0x00, 0x00, 0x00, 0xFC, 0x00,
561                 'V', 'B', 'O', 'X', ' ', 'm', 'o', 'n', 'i', 't', 'o', 'r',
562                 '\n',
563                 /* descriptor block 4: dummy data */
564                 0x00, 0x00, 0x00, 0x10, 0x00,
565                 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
566                 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
567                 0x20,
568                 0x00,           /* number of extensions */
569                 0x00            /* checksum goes here */
570         };
571         int clock = (width + 6) * (height + 6) * 60 / 10000;
572         unsigned int i, sum = 0;
573
574         edid[12] = width & 0xff;
575         edid[13] = width >> 8;
576         edid[14] = height & 0xff;
577         edid[15] = height >> 8;
578         edid[54] = clock & 0xff;
579         edid[55] = clock >> 8;
580         edid[56] = width & 0xff;
581         edid[58] = (width >> 4) & 0xf0;
582         edid[59] = height & 0xff;
583         edid[61] = (height >> 4) & 0xf0;
584         for (i = 0; i < EDID_SIZE - 1; ++i)
585                 sum += edid[i];
586         edid[EDID_SIZE - 1] = (0x100 - (sum & 0xFF)) & 0xFF;
587         drm_connector_update_edid_property(connector, (struct edid *)edid);
588 }
589
590 static int vbox_get_modes(struct drm_connector *connector)
591 {
592         struct vbox_connector *vbox_connector = NULL;
593         struct drm_display_mode *mode = NULL;
594         struct vbox_private *vbox = NULL;
595         unsigned int num_modes = 0;
596         int preferred_width, preferred_height;
597
598         vbox_connector = to_vbox_connector(connector);
599         vbox = connector->dev->dev_private;
600         /*
601          * Heuristic: we do not want to tell the host that we support dynamic
602          * resizing unless we feel confident that the user space client using
603          * the video driver can handle hot-plug events.  So the first time modes
604          * are queried after a "master" switch we tell the host that we do not,
605          * and immediately after we send the client a hot-plug notification as
606          * a test to see if they will respond and query again.
607          * That is also the reason why capabilities are reported to the host at
608          * this place in the code rather than elsewhere.
609          * We need to report the flags location before reporting the IRQ
610          * capability.
611          */
612         hgsmi_report_flags_location(vbox->guest_pool, GUEST_HEAP_OFFSET(vbox) +
613                                     HOST_FLAGS_OFFSET);
614         if (vbox_connector->vbox_crtc->crtc_id == 0)
615                 vbox_report_caps(vbox);
616         if (!vbox->initial_mode_queried) {
617                 if (vbox_connector->vbox_crtc->crtc_id == 0) {
618                         vbox->initial_mode_queried = true;
619                         vbox_report_hotplug(vbox);
620                 }
621                 return drm_add_modes_noedid(connector, 800, 600);
622         }
623         num_modes = drm_add_modes_noedid(connector, 2560, 1600);
624         preferred_width = vbox_connector->mode_hint.width ?
625                           vbox_connector->mode_hint.width : 1024;
626         preferred_height = vbox_connector->mode_hint.height ?
627                            vbox_connector->mode_hint.height : 768;
628         mode = drm_cvt_mode(connector->dev, preferred_width, preferred_height,
629                             60, false, false, false);
630         if (mode) {
631                 mode->type |= DRM_MODE_TYPE_PREFERRED;
632                 drm_mode_probed_add(connector, mode);
633                 ++num_modes;
634         }
635         vbox_set_edid(connector, preferred_width, preferred_height);
636
637         if (vbox_connector->vbox_crtc->x_hint != -1)
638                 drm_object_property_set_value(&connector->base,
639                         vbox->ddev.mode_config.suggested_x_property,
640                         vbox_connector->vbox_crtc->x_hint);
641         else
642                 drm_object_property_set_value(&connector->base,
643                         vbox->ddev.mode_config.suggested_x_property, 0);
644
645         if (vbox_connector->vbox_crtc->y_hint != -1)
646                 drm_object_property_set_value(&connector->base,
647                         vbox->ddev.mode_config.suggested_y_property,
648                         vbox_connector->vbox_crtc->y_hint);
649         else
650                 drm_object_property_set_value(&connector->base,
651                         vbox->ddev.mode_config.suggested_y_property, 0);
652
653         return num_modes;
654 }
655
656 static enum drm_mode_status vbox_mode_valid(struct drm_connector *connector,
657                            struct drm_display_mode *mode)
658 {
659         return MODE_OK;
660 }
661
662 static void vbox_connector_destroy(struct drm_connector *connector)
663 {
664         drm_connector_unregister(connector);
665         drm_connector_cleanup(connector);
666         kfree(connector);
667 }
668
669 static enum drm_connector_status
670 vbox_connector_detect(struct drm_connector *connector, bool force)
671 {
672         struct vbox_connector *vbox_connector;
673
674         vbox_connector = to_vbox_connector(connector);
675
676         return vbox_connector->mode_hint.disconnected ?
677             connector_status_disconnected : connector_status_connected;
678 }
679
680 static int vbox_fill_modes(struct drm_connector *connector, u32 max_x,
681                            u32 max_y)
682 {
683         struct vbox_connector *vbox_connector;
684         struct drm_device *dev;
685         struct drm_display_mode *mode, *iterator;
686
687         vbox_connector = to_vbox_connector(connector);
688         dev = vbox_connector->base.dev;
689         list_for_each_entry_safe(mode, iterator, &connector->modes, head) {
690                 list_del(&mode->head);
691                 drm_mode_destroy(dev, mode);
692         }
693
694         return drm_helper_probe_single_connector_modes(connector, max_x, max_y);
695 }
696
697 static const struct drm_connector_helper_funcs vbox_connector_helper_funcs = {
698         .mode_valid = vbox_mode_valid,
699         .get_modes = vbox_get_modes,
700         .best_encoder = vbox_best_single_encoder,
701 };
702
703 static const struct drm_connector_funcs vbox_connector_funcs = {
704         .dpms = drm_helper_connector_dpms,
705         .detect = vbox_connector_detect,
706         .fill_modes = vbox_fill_modes,
707         .destroy = vbox_connector_destroy,
708 };
709
710 static int vbox_connector_init(struct drm_device *dev,
711                                struct vbox_crtc *vbox_crtc,
712                                struct drm_encoder *encoder)
713 {
714         struct vbox_connector *vbox_connector;
715         struct drm_connector *connector;
716
717         vbox_connector = kzalloc(sizeof(*vbox_connector), GFP_KERNEL);
718         if (!vbox_connector)
719                 return -ENOMEM;
720
721         connector = &vbox_connector->base;
722         vbox_connector->vbox_crtc = vbox_crtc;
723
724         drm_connector_init(dev, connector, &vbox_connector_funcs,
725                            DRM_MODE_CONNECTOR_VGA);
726         drm_connector_helper_add(connector, &vbox_connector_helper_funcs);
727
728         connector->interlace_allowed = 0;
729         connector->doublescan_allowed = 0;
730
731         drm_mode_create_suggested_offset_properties(dev);
732         drm_object_attach_property(&connector->base,
733                                    dev->mode_config.suggested_x_property, 0);
734         drm_object_attach_property(&connector->base,
735                                    dev->mode_config.suggested_y_property, 0);
736
737         drm_connector_attach_encoder(connector, encoder);
738
739         return 0;
740 }
741
742 static struct drm_framebuffer *vbox_user_framebuffer_create(
743                 struct drm_device *dev,
744                 struct drm_file *filp,
745                 const struct drm_mode_fb_cmd2 *mode_cmd)
746 {
747         struct vbox_private *vbox =
748                 container_of(dev, struct vbox_private, ddev);
749         struct drm_gem_object *obj;
750         struct vbox_framebuffer *vbox_fb;
751         int ret = -ENOMEM;
752
753         obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
754         if (!obj)
755                 return ERR_PTR(-ENOENT);
756
757         vbox_fb = kzalloc(sizeof(*vbox_fb), GFP_KERNEL);
758         if (!vbox_fb)
759                 goto err_unref_obj;
760
761         ret = vbox_framebuffer_init(vbox, vbox_fb, mode_cmd, obj);
762         if (ret)
763                 goto err_free_vbox_fb;
764
765         return &vbox_fb->base;
766
767 err_free_vbox_fb:
768         kfree(vbox_fb);
769 err_unref_obj:
770         drm_gem_object_put_unlocked(obj);
771         return ERR_PTR(ret);
772 }
773
774 static const struct drm_mode_config_funcs vbox_mode_funcs = {
775         .fb_create = vbox_user_framebuffer_create,
776 };
777
778 int vbox_mode_init(struct vbox_private *vbox)
779 {
780         struct drm_device *dev = &vbox->ddev;
781         struct drm_encoder *encoder;
782         struct vbox_crtc *vbox_crtc;
783         unsigned int i;
784         int ret;
785
786         drm_mode_config_init(dev);
787
788         dev->mode_config.funcs = (void *)&vbox_mode_funcs;
789         dev->mode_config.min_width = 64;
790         dev->mode_config.min_height = 64;
791         dev->mode_config.preferred_depth = 24;
792         dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
793         dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
794
795         /* vbox_cursor_init(dev); */
796         for (i = 0; i < vbox->num_crtcs; ++i) {
797                 vbox_crtc = vbox_crtc_init(dev, i);
798                 if (IS_ERR(vbox_crtc)) {
799                         ret = PTR_ERR(vbox_crtc);
800                         goto err_drm_mode_cleanup;
801                 }
802                 encoder = vbox_encoder_init(dev, i);
803                 if (!encoder) {
804                         ret = -ENOMEM;
805                         goto err_drm_mode_cleanup;
806                 }
807                 ret = vbox_connector_init(dev, vbox_crtc, encoder);
808                 if (ret)
809                         goto err_drm_mode_cleanup;
810         }
811
812         return 0;
813
814 err_drm_mode_cleanup:
815         drm_mode_config_cleanup(dev);
816         return ret;
817 }
818
819 void vbox_mode_fini(struct vbox_private *vbox)
820 {
821         drm_mode_config_cleanup(&vbox->ddev);
822         /* vbox_cursor_fini(dev); */
823 }
824
825 /**
826  * Copy the ARGB image and generate the mask, which is needed in case the host
827  * does not support ARGB cursors.  The mask is a 1BPP bitmap with the bit set
828  * if the corresponding alpha value in the ARGB image is greater than 0xF0.
829  */
830 static void copy_cursor_image(u8 *src, u8 *dst, u32 width, u32 height,
831                               size_t mask_size)
832 {
833         size_t line_size = (width + 7) / 8;
834         u32 i, j;
835
836         memcpy(dst + mask_size, src, width * height * 4);
837         for (i = 0; i < height; ++i)
838                 for (j = 0; j < width; ++j)
839                         if (((u32 *)src)[i * width + j] > 0xf0000000)
840                                 dst[i * line_size + j / 8] |= (0x80 >> (j % 8));
841 }
842
843 static int vbox_cursor_set2(struct drm_crtc *crtc, struct drm_file *file_priv,
844                             u32 handle, u32 width, u32 height,
845                             s32 hot_x, s32 hot_y)
846 {
847         struct vbox_private *vbox = crtc->dev->dev_private;
848         struct vbox_crtc *vbox_crtc = to_vbox_crtc(crtc);
849         struct ttm_bo_kmap_obj uobj_map;
850         size_t data_size, mask_size;
851         struct drm_gem_object *obj;
852         u32 flags, caps = 0;
853         struct vbox_bo *bo;
854         bool src_isiomem;
855         u8 *dst = NULL;
856         u8 *src;
857         int ret;
858
859         /*
860          * Re-set this regularly as in 5.0.20 and earlier the information was
861          * lost on save and restore.
862          */
863         hgsmi_update_input_mapping(vbox->guest_pool, 0, 0,
864                                    vbox->input_mapping_width,
865                                    vbox->input_mapping_height);
866         if (!handle) {
867                 bool cursor_enabled = false;
868                 struct drm_crtc *crtci;
869
870                 /* Hide cursor. */
871                 vbox_crtc->cursor_enabled = false;
872                 list_for_each_entry(crtci, &vbox->ddev.mode_config.crtc_list,
873                                     head) {
874                         if (to_vbox_crtc(crtci)->cursor_enabled)
875                                 cursor_enabled = true;
876                 }
877
878                 if (!cursor_enabled)
879                         hgsmi_update_pointer_shape(vbox->guest_pool, 0, 0, 0,
880                                                    0, 0, NULL, 0);
881                 return 0;
882         }
883
884         vbox_crtc->cursor_enabled = true;
885
886         if (width > VBOX_MAX_CURSOR_WIDTH || height > VBOX_MAX_CURSOR_HEIGHT ||
887             width == 0 || height == 0)
888                 return -EINVAL;
889
890         ret = hgsmi_query_conf(vbox->guest_pool,
891                                VBOX_VBVA_CONF32_CURSOR_CAPABILITIES, &caps);
892         if (ret)
893                 return ret;
894
895         if (!(caps & VBOX_VBVA_CURSOR_CAPABILITY_HARDWARE)) {
896                 /*
897                  * -EINVAL means cursor_set2() not supported, -EAGAIN means
898                  * retry at once.
899                  */
900                 return -EBUSY;
901         }
902
903         obj = drm_gem_object_lookup(file_priv, handle);
904         if (!obj) {
905                 DRM_ERROR("Cannot find cursor object %x for crtc\n", handle);
906                 return -ENOENT;
907         }
908
909         bo = gem_to_vbox_bo(obj);
910         ret = vbox_bo_reserve(bo, false);
911         if (ret)
912                 goto out_unref_obj;
913
914         /*
915          * The mask must be calculated based on the alpha
916          * channel, one bit per ARGB word, and must be 32-bit
917          * padded.
918          */
919         mask_size = ((width + 7) / 8 * height + 3) & ~3;
920         data_size = width * height * 4 + mask_size;
921         vbox->cursor_hot_x = min_t(u32, max(hot_x, 0), width);
922         vbox->cursor_hot_y = min_t(u32, max(hot_y, 0), height);
923         vbox->cursor_width = width;
924         vbox->cursor_height = height;
925         vbox->cursor_data_size = data_size;
926         dst = vbox->cursor_data;
927
928         ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &uobj_map);
929         if (ret) {
930                 vbox->cursor_data_size = 0;
931                 goto out_unreserve_bo;
932         }
933
934         src = ttm_kmap_obj_virtual(&uobj_map, &src_isiomem);
935         if (src_isiomem) {
936                 DRM_ERROR("src cursor bo not in main memory\n");
937                 ret = -EIO;
938                 goto out_unmap_bo;
939         }
940
941         copy_cursor_image(src, dst, width, height, mask_size);
942
943         flags = VBOX_MOUSE_POINTER_VISIBLE | VBOX_MOUSE_POINTER_SHAPE |
944                 VBOX_MOUSE_POINTER_ALPHA;
945         ret = hgsmi_update_pointer_shape(vbox->guest_pool, flags,
946                                          vbox->cursor_hot_x, vbox->cursor_hot_y,
947                                          width, height, dst, data_size);
948 out_unmap_bo:
949         ttm_bo_kunmap(&uobj_map);
950 out_unreserve_bo:
951         vbox_bo_unreserve(bo);
952 out_unref_obj:
953         drm_gem_object_put_unlocked(obj);
954
955         return ret;
956 }
957
958 static int vbox_cursor_move(struct drm_crtc *crtc, int x, int y)
959 {
960         struct vbox_private *vbox = crtc->dev->dev_private;
961         u32 flags = VBOX_MOUSE_POINTER_VISIBLE |
962             VBOX_MOUSE_POINTER_SHAPE | VBOX_MOUSE_POINTER_ALPHA;
963         s32 crtc_x =
964             vbox->single_framebuffer ? crtc->x : to_vbox_crtc(crtc)->x_hint;
965         s32 crtc_y =
966             vbox->single_framebuffer ? crtc->y : to_vbox_crtc(crtc)->y_hint;
967         u32 host_x, host_y;
968         u32 hot_x = 0;
969         u32 hot_y = 0;
970         int ret;
971
972         /*
973          * We compare these to unsigned later and don't
974          * need to handle negative.
975          */
976         if (x + crtc_x < 0 || y + crtc_y < 0 || vbox->cursor_data_size == 0)
977                 return 0;
978
979         ret = hgsmi_cursor_position(vbox->guest_pool, true, x + crtc_x,
980                                     y + crtc_y, &host_x, &host_y);
981
982         /*
983          * The only reason we have vbox_cursor_move() is that some older clients
984          * might use DRM_IOCTL_MODE_CURSOR instead of DRM_IOCTL_MODE_CURSOR2 and
985          * use DRM_MODE_CURSOR_MOVE to set the hot-spot.
986          *
987          * However VirtualBox 5.0.20 and earlier has a bug causing it to return
988          * 0,0 as host cursor location after a save and restore.
989          *
990          * To work around this we ignore a 0, 0 return, since missing the odd
991          * time when it legitimately happens is not going to hurt much.
992          */
993         if (ret || (host_x == 0 && host_y == 0))
994                 return ret;
995
996         if (x + crtc_x < host_x)
997                 hot_x = min(host_x - x - crtc_x, vbox->cursor_width);
998         if (y + crtc_y < host_y)
999                 hot_y = min(host_y - y - crtc_y, vbox->cursor_height);
1000
1001         if (hot_x == vbox->cursor_hot_x && hot_y == vbox->cursor_hot_y)
1002                 return 0;
1003
1004         vbox->cursor_hot_x = hot_x;
1005         vbox->cursor_hot_y = hot_y;
1006
1007         return hgsmi_update_pointer_shape(vbox->guest_pool, flags,
1008                         hot_x, hot_y, vbox->cursor_width, vbox->cursor_height,
1009                         vbox->cursor_data, vbox->cursor_data_size);
1010 }