xref: /linux/drivers/gpu/drm/vboxvideo/vbox_irq.c (revision 815e260a18a3af4dab59025ee99a7156c0e8b5e0)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright (C) 2016-2017 Oracle Corporation
4  * This file is based on qxl_irq.c
5  * Copyright 2013 Red Hat Inc.
6  * Authors: Dave Airlie
7  *          Alon Levy
8  *          Michael Thayer <michael.thayer@oracle.com,
9  *          Hans de Goede <hdegoede@redhat.com>
10  */
11 
12 #include <linux/pci.h>
13 
14 #include <drm/drm_drv.h>
15 #include <drm/drm_print.h>
16 #include <drm/drm_probe_helper.h>
17 
18 #include "vbox_drv.h"
19 #include "vboxvideo.h"
20 
21 static void vbox_clear_irq(void)
22 {
23 	outl((u32)~0, VGA_PORT_HGSMI_HOST);
24 }
25 
26 static u32 vbox_get_flags(struct vbox_private *vbox)
27 {
28 	return readl(vbox->guest_heap + HOST_FLAGS_OFFSET);
29 }
30 
31 void vbox_report_hotplug(struct vbox_private *vbox)
32 {
33 	schedule_work(&vbox->hotplug_work);
34 }
35 
36 static irqreturn_t vbox_irq_handler(int irq, void *arg)
37 {
38 	struct drm_device *dev = (struct drm_device *)arg;
39 	struct vbox_private *vbox = to_vbox_dev(dev);
40 	u32 host_flags = vbox_get_flags(vbox);
41 
42 	if (!(host_flags & HGSMIHOSTFLAGS_IRQ))
43 		return IRQ_NONE;
44 
45 	/*
46 	 * Due to a bug in the initial host implementation of hot-plug irqs,
47 	 * the hot-plug and cursor capability flags were never cleared.
48 	 * Fortunately we can tell when they would have been set by checking
49 	 * that the VSYNC flag is not set.
50 	 */
51 	if (host_flags &
52 	    (HGSMIHOSTFLAGS_HOTPLUG | HGSMIHOSTFLAGS_CURSOR_CAPABILITIES) &&
53 	    !(host_flags & HGSMIHOSTFLAGS_VSYNC))
54 		vbox_report_hotplug(vbox);
55 
56 	vbox_clear_irq();
57 
58 	return IRQ_HANDLED;
59 }
60 
61 /*
62  * Check that the position hints provided by the host are suitable for GNOME
63  * shell (i.e. all screens disjoint and hints for all enabled screens) and if
64  * not replace them with default ones.  Providing valid hints improves the
65  * chances that we will get a known screen layout for pointer mapping.
66  */
67 static void validate_or_set_position_hints(struct vbox_private *vbox)
68 {
69 	struct vbva_modehint *hintsi, *hintsj;
70 	bool valid = true;
71 	u16 currentx = 0;
72 	int i, j;
73 
74 	for (i = 0; i < vbox->num_crtcs; ++i) {
75 		for (j = 0; j < i; ++j) {
76 			hintsi = &vbox->last_mode_hints[i];
77 			hintsj = &vbox->last_mode_hints[j];
78 
79 			if (hintsi->enabled && hintsj->enabled) {
80 				if (hintsi->dx >= 0xffff ||
81 				    hintsi->dy >= 0xffff ||
82 				    hintsj->dx >= 0xffff ||
83 				    hintsj->dy >= 0xffff ||
84 				    (hintsi->dx <
85 					hintsj->dx + (hintsj->cx & 0x8fff) &&
86 				     hintsi->dx + (hintsi->cx & 0x8fff) >
87 					hintsj->dx) ||
88 				    (hintsi->dy <
89 					hintsj->dy + (hintsj->cy & 0x8fff) &&
90 				     hintsi->dy + (hintsi->cy & 0x8fff) >
91 					hintsj->dy))
92 					valid = false;
93 			}
94 		}
95 	}
96 	if (!valid)
97 		for (i = 0; i < vbox->num_crtcs; ++i) {
98 			if (vbox->last_mode_hints[i].enabled) {
99 				vbox->last_mode_hints[i].dx = currentx;
100 				vbox->last_mode_hints[i].dy = 0;
101 				currentx +=
102 				    vbox->last_mode_hints[i].cx & 0x8fff;
103 			}
104 		}
105 }
106 
107 /* Query the host for the most recent video mode hints. */
108 static void vbox_update_mode_hints(struct vbox_private *vbox)
109 {
110 	struct drm_connector_list_iter conn_iter;
111 	struct drm_device *dev = &vbox->ddev;
112 	struct drm_connector *connector;
113 	struct vbox_connector *vbox_conn;
114 	struct vbva_modehint *hints;
115 	u16 flags;
116 	bool disconnected;
117 	unsigned int crtc_id;
118 	int ret;
119 
120 	ret = hgsmi_get_mode_hints(vbox->guest_pool, vbox->num_crtcs,
121 				   vbox->last_mode_hints);
122 	if (ret) {
123 		DRM_ERROR("vboxvideo: hgsmi_get_mode_hints failed: %d\n", ret);
124 		return;
125 	}
126 
127 	validate_or_set_position_hints(vbox);
128 
129 	drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
130 	drm_connector_list_iter_begin(dev, &conn_iter);
131 	drm_for_each_connector_iter(connector, &conn_iter) {
132 		vbox_conn = to_vbox_connector(connector);
133 
134 		hints = &vbox->last_mode_hints[vbox_conn->vbox_crtc->crtc_id];
135 		if (hints->magic != VBVAMODEHINT_MAGIC)
136 			continue;
137 
138 		disconnected = !(hints->enabled);
139 		crtc_id = vbox_conn->vbox_crtc->crtc_id;
140 		vbox_conn->mode_hint.width = hints->cx;
141 		vbox_conn->mode_hint.height = hints->cy;
142 		vbox_conn->vbox_crtc->x_hint = hints->dx;
143 		vbox_conn->vbox_crtc->y_hint = hints->dy;
144 		vbox_conn->mode_hint.disconnected = disconnected;
145 
146 		if (vbox_conn->vbox_crtc->disconnected == disconnected)
147 			continue;
148 
149 		if (disconnected)
150 			flags = VBVA_SCREEN_F_ACTIVE | VBVA_SCREEN_F_DISABLED;
151 		else
152 			flags = VBVA_SCREEN_F_ACTIVE | VBVA_SCREEN_F_BLANK;
153 
154 		hgsmi_process_display_info(vbox->guest_pool, crtc_id, 0, 0, 0,
155 					   hints->cx * 4, hints->cx,
156 					   hints->cy, 0, flags);
157 
158 		vbox_conn->vbox_crtc->disconnected = disconnected;
159 	}
160 	drm_connector_list_iter_end(&conn_iter);
161 	drm_modeset_unlock(&dev->mode_config.connection_mutex);
162 }
163 
164 static void vbox_hotplug_worker(struct work_struct *work)
165 {
166 	struct vbox_private *vbox = container_of(work, struct vbox_private,
167 						 hotplug_work);
168 
169 	vbox_update_mode_hints(vbox);
170 	drm_kms_helper_hotplug_event(&vbox->ddev);
171 }
172 
173 int vbox_irq_init(struct vbox_private *vbox)
174 {
175 	struct drm_device *dev = &vbox->ddev;
176 	struct pci_dev *pdev = to_pci_dev(dev->dev);
177 
178 	INIT_WORK(&vbox->hotplug_work, vbox_hotplug_worker);
179 	vbox_update_mode_hints(vbox);
180 
181 	/* PCI devices require shared interrupts. */
182 	return request_irq(pdev->irq, vbox_irq_handler, IRQF_SHARED, dev->driver->name, dev);
183 }
184 
185 void vbox_irq_fini(struct vbox_private *vbox)
186 {
187 	struct drm_device *dev = &vbox->ddev;
188 	struct pci_dev *pdev = to_pci_dev(dev->dev);
189 
190 	free_irq(pdev->irq, dev);
191 	flush_work(&vbox->hotplug_work);
192 }
193