xref: /linux/drivers/gpu/drm/vboxvideo/vbox_main.c (revision 1d1997db870f4058676439ef7014390ba9e24eb2)
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/vbox_err.h>
12 #include <drm/drm_fb_helper.h>
13 #include <drm/drm_crtc_helper.h>
14 #include <drm/drm_damage_helper.h>
15 
16 #include "vbox_drv.h"
17 #include "vboxvideo_guest.h"
18 #include "vboxvideo_vbe.h"
19 
20 void vbox_report_caps(struct vbox_private *vbox)
21 {
22 	u32 caps = VBVACAPS_DISABLE_CURSOR_INTEGRATION |
23 		   VBVACAPS_IRQ | VBVACAPS_USE_VBVA_ONLY;
24 
25 	/* The host only accepts VIDEO_MODE_HINTS if it is send separately. */
26 	hgsmi_send_caps_info(vbox->guest_pool, caps);
27 	caps |= VBVACAPS_VIDEO_MODE_HINTS;
28 	hgsmi_send_caps_info(vbox->guest_pool, caps);
29 }
30 
31 static int vbox_accel_init(struct vbox_private *vbox)
32 {
33 	struct vbva_buffer *vbva;
34 	unsigned int i;
35 
36 	vbox->vbva_info = devm_kcalloc(vbox->ddev.dev, vbox->num_crtcs,
37 				       sizeof(*vbox->vbva_info), GFP_KERNEL);
38 	if (!vbox->vbva_info)
39 		return -ENOMEM;
40 
41 	/* Take a command buffer for each screen from the end of usable VRAM. */
42 	vbox->available_vram_size -= vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE;
43 
44 	vbox->vbva_buffers = pci_iomap_range(vbox->ddev.pdev, 0,
45 					     vbox->available_vram_size,
46 					     vbox->num_crtcs *
47 					     VBVA_MIN_BUFFER_SIZE);
48 	if (!vbox->vbva_buffers)
49 		return -ENOMEM;
50 
51 	for (i = 0; i < vbox->num_crtcs; ++i) {
52 		vbva_setup_buffer_context(&vbox->vbva_info[i],
53 					  vbox->available_vram_size +
54 					  i * VBVA_MIN_BUFFER_SIZE,
55 					  VBVA_MIN_BUFFER_SIZE);
56 		vbva = (void __force *)vbox->vbva_buffers +
57 			i * VBVA_MIN_BUFFER_SIZE;
58 		if (!vbva_enable(&vbox->vbva_info[i],
59 				 vbox->guest_pool, vbva, i)) {
60 			/* very old host or driver error. */
61 			DRM_ERROR("vboxvideo: vbva_enable failed\n");
62 		}
63 	}
64 
65 	return 0;
66 }
67 
68 static void vbox_accel_fini(struct vbox_private *vbox)
69 {
70 	unsigned int i;
71 
72 	for (i = 0; i < vbox->num_crtcs; ++i)
73 		vbva_disable(&vbox->vbva_info[i], vbox->guest_pool, i);
74 
75 	pci_iounmap(vbox->ddev.pdev, vbox->vbva_buffers);
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 	int ret = -ENOMEM;
112 
113 	vbox->full_vram_size = inl(VBE_DISPI_IOPORT_DATA);
114 	vbox->any_pitch = vbox_check_supported(VBE_DISPI_ID_ANYX);
115 
116 	DRM_INFO("VRAM %08x\n", vbox->full_vram_size);
117 
118 	/* Map guest-heap at end of vram */
119 	vbox->guest_heap =
120 	    pci_iomap_range(vbox->ddev.pdev, 0, GUEST_HEAP_OFFSET(vbox),
121 			    GUEST_HEAP_SIZE);
122 	if (!vbox->guest_heap)
123 		return -ENOMEM;
124 
125 	/* Create guest-heap mem-pool use 2^4 = 16 byte chunks */
126 	vbox->guest_pool = gen_pool_create(4, -1);
127 	if (!vbox->guest_pool)
128 		goto err_unmap_guest_heap;
129 
130 	ret = gen_pool_add_virt(vbox->guest_pool,
131 				(unsigned long)vbox->guest_heap,
132 				GUEST_HEAP_OFFSET(vbox),
133 				GUEST_HEAP_USABLE_SIZE, -1);
134 	if (ret)
135 		goto err_destroy_guest_pool;
136 
137 	ret = hgsmi_test_query_conf(vbox->guest_pool);
138 	if (ret) {
139 		DRM_ERROR("vboxvideo: hgsmi_test_query_conf failed\n");
140 		goto err_destroy_guest_pool;
141 	}
142 
143 	/* Reduce available VRAM size to reflect the guest heap. */
144 	vbox->available_vram_size = GUEST_HEAP_OFFSET(vbox);
145 	/* Linux drm represents monitors as a 32-bit array. */
146 	hgsmi_query_conf(vbox->guest_pool, VBOX_VBVA_CONF32_MONITOR_COUNT,
147 			 &vbox->num_crtcs);
148 	vbox->num_crtcs = clamp_t(u32, vbox->num_crtcs, 1, VBOX_MAX_SCREENS);
149 
150 	if (!have_hgsmi_mode_hints(vbox)) {
151 		ret = -ENOTSUPP;
152 		goto err_destroy_guest_pool;
153 	}
154 
155 	vbox->last_mode_hints = devm_kcalloc(vbox->ddev.dev, vbox->num_crtcs,
156 					     sizeof(struct vbva_modehint),
157 					     GFP_KERNEL);
158 	if (!vbox->last_mode_hints) {
159 		ret = -ENOMEM;
160 		goto err_destroy_guest_pool;
161 	}
162 
163 	ret = vbox_accel_init(vbox);
164 	if (ret)
165 		goto err_destroy_guest_pool;
166 
167 	return 0;
168 
169 err_destroy_guest_pool:
170 	gen_pool_destroy(vbox->guest_pool);
171 err_unmap_guest_heap:
172 	pci_iounmap(vbox->ddev.pdev, vbox->guest_heap);
173 	return ret;
174 }
175 
176 void vbox_hw_fini(struct vbox_private *vbox)
177 {
178 	vbox_accel_fini(vbox);
179 	gen_pool_destroy(vbox->guest_pool);
180 	pci_iounmap(vbox->ddev.pdev, vbox->guest_heap);
181 }
182