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