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_damage_helper.h>
15 #include <drm/drm_print.h>
16
17 #include "vbox_drv.h"
18 #include "vboxvideo_guest.h"
19 #include "vboxvideo_vbe.h"
20
vbox_report_caps(struct vbox_private * vbox)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
vbox_accel_init(struct vbox_private * vbox)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 = pcim_iomap_range(
47 pdev, 0, vbox->available_vram_size,
48 vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE);
49 if (IS_ERR(vbox->vbva_buffers))
50 return PTR_ERR(vbox->vbva_buffers);
51
52 for (i = 0; i < vbox->num_crtcs; ++i) {
53 vbva_setup_buffer_context(&vbox->vbva_info[i],
54 vbox->available_vram_size +
55 i * VBVA_MIN_BUFFER_SIZE,
56 VBVA_MIN_BUFFER_SIZE);
57 vbva = (void __force *)vbox->vbva_buffers +
58 i * VBVA_MIN_BUFFER_SIZE;
59 if (!vbva_enable(&vbox->vbva_info[i],
60 vbox->guest_pool, vbva, i)) {
61 /* very old host or driver error. */
62 DRM_ERROR("vboxvideo: vbva_enable failed\n");
63 }
64 }
65
66 return 0;
67 }
68
vbox_accel_fini(struct vbox_private * vbox)69 static void vbox_accel_fini(struct vbox_private *vbox)
70 {
71 unsigned int i;
72
73 for (i = 0; i < vbox->num_crtcs; ++i)
74 vbva_disable(&vbox->vbva_info[i], vbox->guest_pool, i);
75 }
76
77 /* Do we support the 4.3 plus mode hint reporting interface? */
have_hgsmi_mode_hints(struct vbox_private * vbox)78 static bool have_hgsmi_mode_hints(struct vbox_private *vbox)
79 {
80 u32 have_hints, have_cursor;
81 int ret;
82
83 ret = hgsmi_query_conf(vbox->guest_pool,
84 VBOX_VBVA_CONF32_MODE_HINT_REPORTING,
85 &have_hints);
86 if (ret)
87 return false;
88
89 ret = hgsmi_query_conf(vbox->guest_pool,
90 VBOX_VBVA_CONF32_GUEST_CURSOR_REPORTING,
91 &have_cursor);
92 if (ret)
93 return false;
94
95 return have_hints == VINF_SUCCESS && have_cursor == VINF_SUCCESS;
96 }
97
vbox_check_supported(u16 id)98 bool vbox_check_supported(u16 id)
99 {
100 u16 dispi_id;
101
102 vbox_write_ioport(VBE_DISPI_INDEX_ID, id);
103 dispi_id = inw(VBE_DISPI_IOPORT_DATA);
104
105 return dispi_id == id;
106 }
107
vbox_hw_init(struct vbox_private * vbox)108 int vbox_hw_init(struct vbox_private *vbox)
109 {
110 struct pci_dev *pdev = to_pci_dev(vbox->ddev.dev);
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 ret = pcim_request_region(pdev, 0, "vboxvideo");
119 if (ret)
120 return ret;
121
122 /* Map guest-heap at end of vram */
123 vbox->guest_heap = pcim_iomap_range(pdev, 0,
124 GUEST_HEAP_OFFSET(vbox), GUEST_HEAP_SIZE);
125 if (IS_ERR(vbox->guest_heap))
126 return PTR_ERR(vbox->guest_heap);
127
128 /* Create guest-heap mem-pool use 2^4 = 16 byte chunks */
129 vbox->guest_pool = devm_gen_pool_create(vbox->ddev.dev, 4, -1,
130 "vboxvideo-accel");
131 if (IS_ERR(vbox->guest_pool))
132 return PTR_ERR(vbox->guest_pool);
133
134 ret = gen_pool_add_virt(vbox->guest_pool,
135 (unsigned long)vbox->guest_heap,
136 GUEST_HEAP_OFFSET(vbox),
137 GUEST_HEAP_USABLE_SIZE, -1);
138 if (ret)
139 return ret;
140
141 ret = hgsmi_test_query_conf(vbox->guest_pool);
142 if (ret) {
143 DRM_ERROR("vboxvideo: hgsmi_test_query_conf failed\n");
144 return ret;
145 }
146
147 /* Reduce available VRAM size to reflect the guest heap. */
148 vbox->available_vram_size = GUEST_HEAP_OFFSET(vbox);
149 /* Linux drm represents monitors as a 32-bit array. */
150 hgsmi_query_conf(vbox->guest_pool, VBOX_VBVA_CONF32_MONITOR_COUNT,
151 &vbox->num_crtcs);
152 vbox->num_crtcs = clamp_t(u32, vbox->num_crtcs, 1, VBOX_MAX_SCREENS);
153
154 if (!have_hgsmi_mode_hints(vbox)) {
155 ret = -ENOTSUPP;
156 return ret;
157 }
158
159 vbox->last_mode_hints = devm_kcalloc(vbox->ddev.dev, vbox->num_crtcs,
160 sizeof(struct vbva_modehint),
161 GFP_KERNEL);
162 if (!vbox->last_mode_hints)
163 return -ENOMEM;
164
165 ret = vbox_accel_init(vbox);
166 if (ret)
167 return ret;
168
169 return 0;
170 }
171
vbox_hw_fini(struct vbox_private * vbox)172 void vbox_hw_fini(struct vbox_private *vbox)
173 {
174 vbox_accel_fini(vbox);
175 }
176