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