1 // SPDX-License-Identifier: MIT
3 * Copyright (C) 2016-2017 Oracle Corporation
4 * This file is based on qxl_irq.c
5 * Copyright 2013 Red Hat Inc.
12 #include <linux/pci.h>
14 #include <drm/drm_drv.h>
15 #include <drm/drm_probe_helper.h>
18 #include "vboxvideo.h"
20 static void vbox_clear_irq(void)
22 outl((u32)~0, VGA_PORT_HGSMI_HOST);
25 static u32 vbox_get_flags(struct vbox_private *vbox)
27 return readl(vbox->guest_heap + HOST_FLAGS_OFFSET);
30 void vbox_report_hotplug(struct vbox_private *vbox)
32 schedule_work(&vbox->hotplug_work);
35 static irqreturn_t vbox_irq_handler(int irq, void *arg)
37 struct drm_device *dev = (struct drm_device *)arg;
38 struct vbox_private *vbox = to_vbox_dev(dev);
39 u32 host_flags = vbox_get_flags(vbox);
41 if (!(host_flags & HGSMIHOSTFLAGS_IRQ))
45 * Due to a bug in the initial host implementation of hot-plug irqs,
46 * the hot-plug and cursor capability flags were never cleared.
47 * Fortunately we can tell when they would have been set by checking
48 * that the VSYNC flag is not set.
51 (HGSMIHOSTFLAGS_HOTPLUG | HGSMIHOSTFLAGS_CURSOR_CAPABILITIES) &&
52 !(host_flags & HGSMIHOSTFLAGS_VSYNC))
53 vbox_report_hotplug(vbox);
61 * Check that the position hints provided by the host are suitable for GNOME
62 * shell (i.e. all screens disjoint and hints for all enabled screens) and if
63 * not replace them with default ones. Providing valid hints improves the
64 * chances that we will get a known screen layout for pointer mapping.
66 static void validate_or_set_position_hints(struct vbox_private *vbox)
68 struct vbva_modehint *hintsi, *hintsj;
73 for (i = 0; i < vbox->num_crtcs; ++i) {
74 for (j = 0; j < i; ++j) {
75 hintsi = &vbox->last_mode_hints[i];
76 hintsj = &vbox->last_mode_hints[j];
78 if (hintsi->enabled && hintsj->enabled) {
79 if (hintsi->dx >= 0xffff ||
80 hintsi->dy >= 0xffff ||
81 hintsj->dx >= 0xffff ||
82 hintsj->dy >= 0xffff ||
84 hintsj->dx + (hintsj->cx & 0x8fff) &&
85 hintsi->dx + (hintsi->cx & 0x8fff) >
88 hintsj->dy + (hintsj->cy & 0x8fff) &&
89 hintsi->dy + (hintsi->cy & 0x8fff) >
96 for (i = 0; i < vbox->num_crtcs; ++i) {
97 if (vbox->last_mode_hints[i].enabled) {
98 vbox->last_mode_hints[i].dx = currentx;
99 vbox->last_mode_hints[i].dy = 0;
101 vbox->last_mode_hints[i].cx & 0x8fff;
106 /* Query the host for the most recent video mode hints. */
107 static void vbox_update_mode_hints(struct vbox_private *vbox)
109 struct drm_connector_list_iter conn_iter;
110 struct drm_device *dev = &vbox->ddev;
111 struct drm_connector *connector;
112 struct vbox_connector *vbox_conn;
113 struct vbva_modehint *hints;
116 unsigned int crtc_id;
119 ret = hgsmi_get_mode_hints(vbox->guest_pool, vbox->num_crtcs,
120 vbox->last_mode_hints);
122 DRM_ERROR("vboxvideo: hgsmi_get_mode_hints failed: %d\n", ret);
126 validate_or_set_position_hints(vbox);
128 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
129 drm_connector_list_iter_begin(dev, &conn_iter);
130 drm_for_each_connector_iter(connector, &conn_iter) {
131 vbox_conn = to_vbox_connector(connector);
133 hints = &vbox->last_mode_hints[vbox_conn->vbox_crtc->crtc_id];
134 if (hints->magic != VBVAMODEHINT_MAGIC)
137 disconnected = !(hints->enabled);
138 crtc_id = vbox_conn->vbox_crtc->crtc_id;
139 vbox_conn->mode_hint.width = hints->cx;
140 vbox_conn->mode_hint.height = hints->cy;
141 vbox_conn->vbox_crtc->x_hint = hints->dx;
142 vbox_conn->vbox_crtc->y_hint = hints->dy;
143 vbox_conn->mode_hint.disconnected = disconnected;
145 if (vbox_conn->vbox_crtc->disconnected == disconnected)
149 flags = VBVA_SCREEN_F_ACTIVE | VBVA_SCREEN_F_DISABLED;
151 flags = VBVA_SCREEN_F_ACTIVE | VBVA_SCREEN_F_BLANK;
153 hgsmi_process_display_info(vbox->guest_pool, crtc_id, 0, 0, 0,
154 hints->cx * 4, hints->cx,
155 hints->cy, 0, flags);
157 vbox_conn->vbox_crtc->disconnected = disconnected;
159 drm_connector_list_iter_end(&conn_iter);
160 drm_modeset_unlock(&dev->mode_config.connection_mutex);
163 static void vbox_hotplug_worker(struct work_struct *work)
165 struct vbox_private *vbox = container_of(work, struct vbox_private,
168 vbox_update_mode_hints(vbox);
169 drm_kms_helper_hotplug_event(&vbox->ddev);
172 int vbox_irq_init(struct vbox_private *vbox)
174 struct drm_device *dev = &vbox->ddev;
175 struct pci_dev *pdev = to_pci_dev(dev->dev);
177 INIT_WORK(&vbox->hotplug_work, vbox_hotplug_worker);
178 vbox_update_mode_hints(vbox);
180 /* PCI devices require shared interrupts. */
181 return request_irq(pdev->irq, vbox_irq_handler, IRQF_SHARED, dev->driver->name, dev);
184 void vbox_irq_fini(struct vbox_private *vbox)
186 struct drm_device *dev = &vbox->ddev;
187 struct pci_dev *pdev = to_pci_dev(dev->dev);
189 free_irq(pdev->irq, dev);
190 flush_work(&vbox->hotplug_work);