Home | History | Annotate | Line # | Download | only in vboxvideo
      1  1.1  riastrad /*	$NetBSD: vbox_main.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) 2013-2017 Oracle Corporation
      6  1.1  riastrad  * This file is based on ast_main.c
      7  1.1  riastrad  * Copyright 2012 Red Hat Inc.
      8  1.1  riastrad  * Authors: Dave Airlie <airlied (at) redhat.com>,
      9  1.1  riastrad  *          Michael Thayer <michael.thayer (at) oracle.com,
     10  1.1  riastrad  *          Hans de Goede <hdegoede (at) redhat.com>
     11  1.1  riastrad  */
     12  1.1  riastrad 
     13  1.1  riastrad #include <sys/cdefs.h>
     14  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: vbox_main.c,v 1.2 2021/12/18 23:45:44 riastradh Exp $");
     15  1.1  riastrad 
     16  1.1  riastrad #include <linux/vbox_err.h>
     17  1.1  riastrad #include <drm/drm_fb_helper.h>
     18  1.1  riastrad #include <drm/drm_crtc_helper.h>
     19  1.1  riastrad #include <drm/drm_damage_helper.h>
     20  1.1  riastrad 
     21  1.1  riastrad #include "vbox_drv.h"
     22  1.1  riastrad #include "vboxvideo_guest.h"
     23  1.1  riastrad #include "vboxvideo_vbe.h"
     24  1.1  riastrad 
     25  1.1  riastrad void vbox_report_caps(struct vbox_private *vbox)
     26  1.1  riastrad {
     27  1.1  riastrad 	u32 caps = VBVACAPS_DISABLE_CURSOR_INTEGRATION |
     28  1.1  riastrad 		   VBVACAPS_IRQ | VBVACAPS_USE_VBVA_ONLY;
     29  1.1  riastrad 
     30  1.1  riastrad 	/* The host only accepts VIDEO_MODE_HINTS if it is send separately. */
     31  1.1  riastrad 	hgsmi_send_caps_info(vbox->guest_pool, caps);
     32  1.1  riastrad 	caps |= VBVACAPS_VIDEO_MODE_HINTS;
     33  1.1  riastrad 	hgsmi_send_caps_info(vbox->guest_pool, caps);
     34  1.1  riastrad }
     35  1.1  riastrad 
     36  1.1  riastrad static int vbox_accel_init(struct vbox_private *vbox)
     37  1.1  riastrad {
     38  1.1  riastrad 	struct vbva_buffer *vbva;
     39  1.1  riastrad 	unsigned int i;
     40  1.1  riastrad 
     41  1.1  riastrad 	vbox->vbva_info = devm_kcalloc(vbox->ddev.dev, vbox->num_crtcs,
     42  1.1  riastrad 				       sizeof(*vbox->vbva_info), GFP_KERNEL);
     43  1.1  riastrad 	if (!vbox->vbva_info)
     44  1.1  riastrad 		return -ENOMEM;
     45  1.1  riastrad 
     46  1.1  riastrad 	/* Take a command buffer for each screen from the end of usable VRAM. */
     47  1.1  riastrad 	vbox->available_vram_size -= vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE;
     48  1.1  riastrad 
     49  1.1  riastrad 	vbox->vbva_buffers = pci_iomap_range(vbox->ddev.pdev, 0,
     50  1.1  riastrad 					     vbox->available_vram_size,
     51  1.1  riastrad 					     vbox->num_crtcs *
     52  1.1  riastrad 					     VBVA_MIN_BUFFER_SIZE);
     53  1.1  riastrad 	if (!vbox->vbva_buffers)
     54  1.1  riastrad 		return -ENOMEM;
     55  1.1  riastrad 
     56  1.1  riastrad 	for (i = 0; i < vbox->num_crtcs; ++i) {
     57  1.1  riastrad 		vbva_setup_buffer_context(&vbox->vbva_info[i],
     58  1.1  riastrad 					  vbox->available_vram_size +
     59  1.1  riastrad 					  i * VBVA_MIN_BUFFER_SIZE,
     60  1.1  riastrad 					  VBVA_MIN_BUFFER_SIZE);
     61  1.1  riastrad 		vbva = (void __force *)vbox->vbva_buffers +
     62  1.1  riastrad 			i * VBVA_MIN_BUFFER_SIZE;
     63  1.1  riastrad 		if (!vbva_enable(&vbox->vbva_info[i],
     64  1.1  riastrad 				 vbox->guest_pool, vbva, i)) {
     65  1.1  riastrad 			/* very old host or driver error. */
     66  1.1  riastrad 			DRM_ERROR("vboxvideo: vbva_enable failed\n");
     67  1.1  riastrad 		}
     68  1.1  riastrad 	}
     69  1.1  riastrad 
     70  1.1  riastrad 	return 0;
     71  1.1  riastrad }
     72  1.1  riastrad 
     73  1.1  riastrad static void vbox_accel_fini(struct vbox_private *vbox)
     74  1.1  riastrad {
     75  1.1  riastrad 	unsigned int i;
     76  1.1  riastrad 
     77  1.1  riastrad 	for (i = 0; i < vbox->num_crtcs; ++i)
     78  1.1  riastrad 		vbva_disable(&vbox->vbva_info[i], vbox->guest_pool, i);
     79  1.1  riastrad 
     80  1.1  riastrad 	pci_iounmap(vbox->ddev.pdev, vbox->vbva_buffers);
     81  1.1  riastrad }
     82  1.1  riastrad 
     83  1.1  riastrad /* Do we support the 4.3 plus mode hint reporting interface? */
     84  1.1  riastrad static bool have_hgsmi_mode_hints(struct vbox_private *vbox)
     85  1.1  riastrad {
     86  1.1  riastrad 	u32 have_hints, have_cursor;
     87  1.1  riastrad 	int ret;
     88  1.1  riastrad 
     89  1.1  riastrad 	ret = hgsmi_query_conf(vbox->guest_pool,
     90  1.1  riastrad 			       VBOX_VBVA_CONF32_MODE_HINT_REPORTING,
     91  1.1  riastrad 			       &have_hints);
     92  1.1  riastrad 	if (ret)
     93  1.1  riastrad 		return false;
     94  1.1  riastrad 
     95  1.1  riastrad 	ret = hgsmi_query_conf(vbox->guest_pool,
     96  1.1  riastrad 			       VBOX_VBVA_CONF32_GUEST_CURSOR_REPORTING,
     97  1.1  riastrad 			       &have_cursor);
     98  1.1  riastrad 	if (ret)
     99  1.1  riastrad 		return false;
    100  1.1  riastrad 
    101  1.1  riastrad 	return have_hints == VINF_SUCCESS && have_cursor == VINF_SUCCESS;
    102  1.1  riastrad }
    103  1.1  riastrad 
    104  1.1  riastrad bool vbox_check_supported(u16 id)
    105  1.1  riastrad {
    106  1.1  riastrad 	u16 dispi_id;
    107  1.1  riastrad 
    108  1.1  riastrad 	vbox_write_ioport(VBE_DISPI_INDEX_ID, id);
    109  1.1  riastrad 	dispi_id = inw(VBE_DISPI_IOPORT_DATA);
    110  1.1  riastrad 
    111  1.1  riastrad 	return dispi_id == id;
    112  1.1  riastrad }
    113  1.1  riastrad 
    114  1.1  riastrad int vbox_hw_init(struct vbox_private *vbox)
    115  1.1  riastrad {
    116  1.1  riastrad 	int ret = -ENOMEM;
    117  1.1  riastrad 
    118  1.1  riastrad 	vbox->full_vram_size = inl(VBE_DISPI_IOPORT_DATA);
    119  1.1  riastrad 	vbox->any_pitch = vbox_check_supported(VBE_DISPI_ID_ANYX);
    120  1.1  riastrad 
    121  1.1  riastrad 	DRM_INFO("VRAM %08x\n", vbox->full_vram_size);
    122  1.1  riastrad 
    123  1.1  riastrad 	/* Map guest-heap at end of vram */
    124  1.1  riastrad 	vbox->guest_heap =
    125  1.1  riastrad 	    pci_iomap_range(vbox->ddev.pdev, 0, GUEST_HEAP_OFFSET(vbox),
    126  1.1  riastrad 			    GUEST_HEAP_SIZE);
    127  1.1  riastrad 	if (!vbox->guest_heap)
    128  1.1  riastrad 		return -ENOMEM;
    129  1.1  riastrad 
    130  1.1  riastrad 	/* Create guest-heap mem-pool use 2^4 = 16 byte chunks */
    131  1.1  riastrad 	vbox->guest_pool = gen_pool_create(4, -1);
    132  1.1  riastrad 	if (!vbox->guest_pool)
    133  1.1  riastrad 		goto err_unmap_guest_heap;
    134  1.1  riastrad 
    135  1.1  riastrad 	ret = gen_pool_add_virt(vbox->guest_pool,
    136  1.1  riastrad 				(unsigned long)vbox->guest_heap,
    137  1.1  riastrad 				GUEST_HEAP_OFFSET(vbox),
    138  1.1  riastrad 				GUEST_HEAP_USABLE_SIZE, -1);
    139  1.1  riastrad 	if (ret)
    140  1.1  riastrad 		goto err_destroy_guest_pool;
    141  1.1  riastrad 
    142  1.1  riastrad 	ret = hgsmi_test_query_conf(vbox->guest_pool);
    143  1.1  riastrad 	if (ret) {
    144  1.1  riastrad 		DRM_ERROR("vboxvideo: hgsmi_test_query_conf failed\n");
    145  1.1  riastrad 		goto err_destroy_guest_pool;
    146  1.1  riastrad 	}
    147  1.1  riastrad 
    148  1.1  riastrad 	/* Reduce available VRAM size to reflect the guest heap. */
    149  1.1  riastrad 	vbox->available_vram_size = GUEST_HEAP_OFFSET(vbox);
    150  1.1  riastrad 	/* Linux drm represents monitors as a 32-bit array. */
    151  1.1  riastrad 	hgsmi_query_conf(vbox->guest_pool, VBOX_VBVA_CONF32_MONITOR_COUNT,
    152  1.1  riastrad 			 &vbox->num_crtcs);
    153  1.1  riastrad 	vbox->num_crtcs = clamp_t(u32, vbox->num_crtcs, 1, VBOX_MAX_SCREENS);
    154  1.1  riastrad 
    155  1.1  riastrad 	if (!have_hgsmi_mode_hints(vbox)) {
    156  1.1  riastrad 		ret = -ENOTSUPP;
    157  1.1  riastrad 		goto err_destroy_guest_pool;
    158  1.1  riastrad 	}
    159  1.1  riastrad 
    160  1.1  riastrad 	vbox->last_mode_hints = devm_kcalloc(vbox->ddev.dev, vbox->num_crtcs,
    161  1.1  riastrad 					     sizeof(struct vbva_modehint),
    162  1.1  riastrad 					     GFP_KERNEL);
    163  1.1  riastrad 	if (!vbox->last_mode_hints) {
    164  1.1  riastrad 		ret = -ENOMEM;
    165  1.1  riastrad 		goto err_destroy_guest_pool;
    166  1.1  riastrad 	}
    167  1.1  riastrad 
    168  1.1  riastrad 	ret = vbox_accel_init(vbox);
    169  1.1  riastrad 	if (ret)
    170  1.1  riastrad 		goto err_destroy_guest_pool;
    171  1.1  riastrad 
    172  1.1  riastrad 	return 0;
    173  1.1  riastrad 
    174  1.1  riastrad err_destroy_guest_pool:
    175  1.1  riastrad 	gen_pool_destroy(vbox->guest_pool);
    176  1.1  riastrad err_unmap_guest_heap:
    177  1.1  riastrad 	pci_iounmap(vbox->ddev.pdev, vbox->guest_heap);
    178  1.1  riastrad 	return ret;
    179  1.1  riastrad }
    180  1.1  riastrad 
    181  1.1  riastrad void vbox_hw_fini(struct vbox_private *vbox)
    182  1.1  riastrad {
    183  1.1  riastrad 	vbox_accel_fini(vbox);
    184  1.1  riastrad 	gen_pool_destroy(vbox->guest_pool);
    185  1.1  riastrad 	pci_iounmap(vbox->ddev.pdev, vbox->guest_heap);
    186  1.1  riastrad }
    187