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