1 | /* $Id: vbox_irq.c 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Additions Linux kernel video driver
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2016-2019 Oracle Corporation
|
---|
8 | * This file is based on qxl_irq.c
|
---|
9 | * Copyright 2013 Red Hat Inc.
|
---|
10 | *
|
---|
11 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
12 | * copy of this software and associated documentation files (the "Software"),
|
---|
13 | * to deal in the Software without restriction, including without limitation
|
---|
14 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
---|
15 | * and/or sell copies of the Software, and to permit persons to whom the
|
---|
16 | * Software is furnished to do so, subject to the following conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be included in
|
---|
19 | * all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
---|
24 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
---|
25 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
---|
26 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
27 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
28 | *
|
---|
29 | * Authors: Dave Airlie
|
---|
30 | * Alon Levy
|
---|
31 | * Michael Thayer <[email protected],
|
---|
32 | * Hans de Goede <[email protected]>
|
---|
33 | */
|
---|
34 | #include "vbox_drv.h"
|
---|
35 |
|
---|
36 | #include <drm/drm_crtc_helper.h>
|
---|
37 | #include <VBoxVideo.h>
|
---|
38 |
|
---|
39 | static void vbox_clear_irq(void)
|
---|
40 | {
|
---|
41 | outl((u32)~0, VGA_PORT_HGSMI_HOST);
|
---|
42 | }
|
---|
43 |
|
---|
44 | static u32 vbox_get_flags(struct vbox_private *vbox)
|
---|
45 | {
|
---|
46 | return readl(vbox->guest_heap + HOST_FLAGS_OFFSET);
|
---|
47 | }
|
---|
48 |
|
---|
49 | void vbox_report_hotplug(struct vbox_private *vbox)
|
---|
50 | {
|
---|
51 | schedule_work(&vbox->hotplug_work);
|
---|
52 | }
|
---|
53 |
|
---|
54 | irqreturn_t vbox_irq_handler(int irq, void *arg)
|
---|
55 | {
|
---|
56 | struct drm_device *dev = (struct drm_device *)arg;
|
---|
57 | struct vbox_private *vbox = (struct vbox_private *)dev->dev_private;
|
---|
58 | u32 host_flags = vbox_get_flags(vbox);
|
---|
59 |
|
---|
60 | if (!(host_flags & HGSMIHOSTFLAGS_IRQ))
|
---|
61 | return IRQ_NONE;
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * Due to a bug in the initial host implementation of hot-plug irqs,
|
---|
65 | * the hot-plug and cursor capability flags were never cleared.
|
---|
66 | * Fortunately we can tell when they would have been set by checking
|
---|
67 | * that the VSYNC flag is not set.
|
---|
68 | */
|
---|
69 | if (host_flags &
|
---|
70 | (HGSMIHOSTFLAGS_HOTPLUG | HGSMIHOSTFLAGS_CURSOR_CAPABILITIES) &&
|
---|
71 | !(host_flags & HGSMIHOSTFLAGS_VSYNC))
|
---|
72 | vbox_report_hotplug(vbox);
|
---|
73 |
|
---|
74 | vbox_clear_irq();
|
---|
75 |
|
---|
76 | return IRQ_HANDLED;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Check that the position hints provided by the host are suitable for GNOME
|
---|
81 | * shell (i.e. all screens disjoint and hints for all enabled screens) and if
|
---|
82 | * not replace them with default ones. Providing valid hints improves the
|
---|
83 | * chances that we will get a known screen layout for pointer mapping.
|
---|
84 | */
|
---|
85 | static void validate_or_set_position_hints(struct vbox_private *vbox)
|
---|
86 | {
|
---|
87 | struct VBVAMODEHINT *hintsi, *hintsj;
|
---|
88 | bool valid = true;
|
---|
89 | u16 currentx = 0;
|
---|
90 | int i, j;
|
---|
91 |
|
---|
92 | for (i = 0; i < vbox->num_crtcs; ++i) {
|
---|
93 | for (j = 0; j < i; ++j) {
|
---|
94 | hintsi = &vbox->last_mode_hints[i];
|
---|
95 | hintsj = &vbox->last_mode_hints[j];
|
---|
96 |
|
---|
97 | if (hintsi->fEnabled && hintsj->fEnabled) {
|
---|
98 | if (hintsi->dx >= 0xffff ||
|
---|
99 | hintsi->dy >= 0xffff ||
|
---|
100 | hintsj->dx >= 0xffff ||
|
---|
101 | hintsj->dy >= 0xffff ||
|
---|
102 | (hintsi->dx <
|
---|
103 | hintsj->dx + (hintsj->cx & 0x8fff) &&
|
---|
104 | hintsi->dx + (hintsi->cx & 0x8fff) >
|
---|
105 | hintsj->dx) ||
|
---|
106 | (hintsi->dy <
|
---|
107 | hintsj->dy + (hintsj->cy & 0x8fff) &&
|
---|
108 | hintsi->dy + (hintsi->cy & 0x8fff) >
|
---|
109 | hintsj->dy))
|
---|
110 | valid = false;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | if (!valid)
|
---|
115 | for (i = 0; i < vbox->num_crtcs; ++i) {
|
---|
116 | if (vbox->last_mode_hints[i].fEnabled) {
|
---|
117 | vbox->last_mode_hints[i].dx = currentx;
|
---|
118 | vbox->last_mode_hints[i].dy = 0;
|
---|
119 | currentx +=
|
---|
120 | vbox->last_mode_hints[i].cx & 0x8fff;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Query the host for the most recent video mode hints.
|
---|
127 | */
|
---|
128 | static void vbox_update_mode_hints(struct vbox_private *vbox)
|
---|
129 | {
|
---|
130 | struct drm_device *dev = vbox->dev;
|
---|
131 | struct drm_connector *connector;
|
---|
132 | struct vbox_connector *vbox_conn;
|
---|
133 | struct VBVAMODEHINT *hints;
|
---|
134 | u16 flags;
|
---|
135 | bool disconnected;
|
---|
136 | unsigned int crtc_id;
|
---|
137 | int ret;
|
---|
138 |
|
---|
139 | ret = VBoxHGSMIGetModeHints(vbox->guest_pool, vbox->num_crtcs,
|
---|
140 | vbox->last_mode_hints);
|
---|
141 | if (ret) {
|
---|
142 | DRM_ERROR("vboxvideo: hgsmi_get_mode_hints failed: %d\n", ret);
|
---|
143 | return;
|
---|
144 | }
|
---|
145 |
|
---|
146 | validate_or_set_position_hints(vbox);
|
---|
147 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
|
---|
148 | drm_modeset_lock_all(dev);
|
---|
149 | #else
|
---|
150 | mutex_lock(&dev->mode_config.mutex);
|
---|
151 | #endif
|
---|
152 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
|
---|
153 | vbox_conn = to_vbox_connector(connector);
|
---|
154 |
|
---|
155 | hints = &vbox->last_mode_hints[vbox_conn->vbox_crtc->crtc_id];
|
---|
156 | if (hints->magic != VBVAMODEHINT_MAGIC)
|
---|
157 | continue;
|
---|
158 |
|
---|
159 | disconnected = !(hints->fEnabled);
|
---|
160 | crtc_id = vbox_conn->vbox_crtc->crtc_id;
|
---|
161 | vbox_conn->mode_hint.width = hints->cx & 0x8fff;
|
---|
162 | vbox_conn->mode_hint.height = hints->cy & 0x8fff;
|
---|
163 | vbox_conn->vbox_crtc->x_hint = hints->dx;
|
---|
164 | vbox_conn->vbox_crtc->y_hint = hints->dy;
|
---|
165 | vbox_conn->mode_hint.disconnected = disconnected;
|
---|
166 |
|
---|
167 | if (vbox_conn->vbox_crtc->disconnected == disconnected)
|
---|
168 | continue;
|
---|
169 |
|
---|
170 | if (disconnected)
|
---|
171 | flags = VBVA_SCREEN_F_ACTIVE | VBVA_SCREEN_F_DISABLED;
|
---|
172 | else
|
---|
173 | flags = VBVA_SCREEN_F_ACTIVE | VBVA_SCREEN_F_BLANK;
|
---|
174 |
|
---|
175 | VBoxHGSMIProcessDisplayInfo(vbox->guest_pool, crtc_id, 0, 0, 0,
|
---|
176 | hints->cx * 4, hints->cx,
|
---|
177 | hints->cy, 0, flags);
|
---|
178 |
|
---|
179 | vbox_conn->vbox_crtc->disconnected = disconnected;
|
---|
180 | }
|
---|
181 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
|
---|
182 | drm_modeset_unlock_all(dev);
|
---|
183 | #else
|
---|
184 | mutex_unlock(&dev->mode_config.mutex);
|
---|
185 | #endif
|
---|
186 | }
|
---|
187 |
|
---|
188 | static void vbox_hotplug_worker(struct work_struct *work)
|
---|
189 | {
|
---|
190 | struct vbox_private *vbox = container_of(work, struct vbox_private,
|
---|
191 | hotplug_work);
|
---|
192 |
|
---|
193 | vbox_update_mode_hints(vbox);
|
---|
194 | drm_kms_helper_hotplug_event(vbox->dev);
|
---|
195 | }
|
---|
196 |
|
---|
197 | int vbox_irq_init(struct vbox_private *vbox)
|
---|
198 | {
|
---|
199 | INIT_WORK(&vbox->hotplug_work, vbox_hotplug_worker);
|
---|
200 | vbox_update_mode_hints(vbox);
|
---|
201 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 16, 0) || defined(RHEL_71)
|
---|
202 | return drm_irq_install(vbox->dev, vbox->dev->pdev->irq);
|
---|
203 | #else
|
---|
204 | return drm_irq_install(vbox->dev);
|
---|
205 | #endif
|
---|
206 | }
|
---|
207 |
|
---|
208 | void vbox_irq_fini(struct vbox_private *vbox)
|
---|
209 | {
|
---|
210 | drm_irq_uninstall(vbox->dev);
|
---|
211 | flush_work(&vbox->hotplug_work);
|
---|
212 | }
|
---|