1131abc56SHans de Goede // SPDX-License-Identifier: MIT
2131abc56SHans de Goede /*
3131abc56SHans de Goede  * Copyright (C) 2013-2017 Oracle Corporation
4131abc56SHans de Goede  * This file is based on ast_main.c
5131abc56SHans de Goede  * Copyright 2012 Red Hat Inc.
6131abc56SHans de Goede  * Authors: Dave Airlie <airlied@redhat.com>,
7131abc56SHans de Goede  *          Michael Thayer <michael.thayer@oracle.com,
8131abc56SHans de Goede  *          Hans de Goede <hdegoede@redhat.com>
9131abc56SHans de Goede  */
10131abc56SHans de Goede 
1156492fe9SThomas Zimmermann #include <linux/pci.h>
12131abc56SHans de Goede #include <linux/vbox_err.h>
1356492fe9SThomas Zimmermann 
141a74ccfaSThomas Zimmermann #include <drm/drm_damage_helper.h>
15131abc56SHans de Goede 
16131abc56SHans de Goede #include "vbox_drv.h"
17131abc56SHans de Goede #include "vboxvideo_guest.h"
18131abc56SHans de Goede #include "vboxvideo_vbe.h"
19131abc56SHans de Goede 
vbox_report_caps(struct vbox_private * vbox)20131abc56SHans de Goede void vbox_report_caps(struct vbox_private *vbox)
21131abc56SHans de Goede {
22131abc56SHans de Goede 	u32 caps = VBVACAPS_DISABLE_CURSOR_INTEGRATION |
23131abc56SHans de Goede 		   VBVACAPS_IRQ | VBVACAPS_USE_VBVA_ONLY;
24131abc56SHans de Goede 
25131abc56SHans de Goede 	/* The host only accepts VIDEO_MODE_HINTS if it is send separately. */
26131abc56SHans de Goede 	hgsmi_send_caps_info(vbox->guest_pool, caps);
27131abc56SHans de Goede 	caps |= VBVACAPS_VIDEO_MODE_HINTS;
28131abc56SHans de Goede 	hgsmi_send_caps_info(vbox->guest_pool, caps);
29131abc56SHans de Goede }
30131abc56SHans de Goede 
vbox_accel_init(struct vbox_private * vbox)31131abc56SHans de Goede static int vbox_accel_init(struct vbox_private *vbox)
32131abc56SHans de Goede {
3356492fe9SThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(vbox->ddev.dev);
34131abc56SHans de Goede 	struct vbva_buffer *vbva;
35131abc56SHans de Goede 	unsigned int i;
36131abc56SHans de Goede 
37131abc56SHans de Goede 	vbox->vbva_info = devm_kcalloc(vbox->ddev.dev, vbox->num_crtcs,
38131abc56SHans de Goede 				       sizeof(*vbox->vbva_info), GFP_KERNEL);
39131abc56SHans de Goede 	if (!vbox->vbva_info)
40131abc56SHans de Goede 		return -ENOMEM;
41131abc56SHans de Goede 
42131abc56SHans de Goede 	/* Take a command buffer for each screen from the end of usable VRAM. */
43131abc56SHans de Goede 	vbox->available_vram_size -= vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE;
44131abc56SHans de Goede 
4556492fe9SThomas Zimmermann 	vbox->vbva_buffers = pci_iomap_range(pdev, 0,
46131abc56SHans de Goede 					     vbox->available_vram_size,
47131abc56SHans de Goede 					     vbox->num_crtcs *
48131abc56SHans de Goede 					     VBVA_MIN_BUFFER_SIZE);
49131abc56SHans de Goede 	if (!vbox->vbva_buffers)
50131abc56SHans de Goede 		return -ENOMEM;
51131abc56SHans de Goede 
52131abc56SHans de Goede 	for (i = 0; i < vbox->num_crtcs; ++i) {
53131abc56SHans de Goede 		vbva_setup_buffer_context(&vbox->vbva_info[i],
54131abc56SHans de Goede 					  vbox->available_vram_size +
55131abc56SHans de Goede 					  i * VBVA_MIN_BUFFER_SIZE,
56131abc56SHans de Goede 					  VBVA_MIN_BUFFER_SIZE);
57131abc56SHans de Goede 		vbva = (void __force *)vbox->vbva_buffers +
58131abc56SHans de Goede 			i * VBVA_MIN_BUFFER_SIZE;
59131abc56SHans de Goede 		if (!vbva_enable(&vbox->vbva_info[i],
60131abc56SHans de Goede 				 vbox->guest_pool, vbva, i)) {
61131abc56SHans de Goede 			/* very old host or driver error. */
62131abc56SHans de Goede 			DRM_ERROR("vboxvideo: vbva_enable failed\n");
63131abc56SHans de Goede 		}
64131abc56SHans de Goede 	}
65131abc56SHans de Goede 
66131abc56SHans de Goede 	return 0;
67131abc56SHans de Goede }
68131abc56SHans de Goede 
vbox_accel_fini(struct vbox_private * vbox)69131abc56SHans de Goede static void vbox_accel_fini(struct vbox_private *vbox)
70131abc56SHans de Goede {
71131abc56SHans de Goede 	unsigned int i;
72131abc56SHans de Goede 
73131abc56SHans de Goede 	for (i = 0; i < vbox->num_crtcs; ++i)
74131abc56SHans de Goede 		vbva_disable(&vbox->vbva_info[i], vbox->guest_pool, i);
75131abc56SHans de Goede }
76131abc56SHans de Goede 
77131abc56SHans de Goede /* Do we support the 4.3 plus mode hint reporting interface? */
have_hgsmi_mode_hints(struct vbox_private * vbox)78131abc56SHans de Goede static bool have_hgsmi_mode_hints(struct vbox_private *vbox)
79131abc56SHans de Goede {
80131abc56SHans de Goede 	u32 have_hints, have_cursor;
81131abc56SHans de Goede 	int ret;
82131abc56SHans de Goede 
83131abc56SHans de Goede 	ret = hgsmi_query_conf(vbox->guest_pool,
84131abc56SHans de Goede 			       VBOX_VBVA_CONF32_MODE_HINT_REPORTING,
85131abc56SHans de Goede 			       &have_hints);
86131abc56SHans de Goede 	if (ret)
87131abc56SHans de Goede 		return false;
88131abc56SHans de Goede 
89131abc56SHans de Goede 	ret = hgsmi_query_conf(vbox->guest_pool,
90131abc56SHans de Goede 			       VBOX_VBVA_CONF32_GUEST_CURSOR_REPORTING,
91131abc56SHans de Goede 			       &have_cursor);
92131abc56SHans de Goede 	if (ret)
93131abc56SHans de Goede 		return false;
94131abc56SHans de Goede 
95131abc56SHans de Goede 	return have_hints == VINF_SUCCESS && have_cursor == VINF_SUCCESS;
96131abc56SHans de Goede }
97131abc56SHans de Goede 
vbox_check_supported(u16 id)98131abc56SHans de Goede bool vbox_check_supported(u16 id)
99131abc56SHans de Goede {
100131abc56SHans de Goede 	u16 dispi_id;
101131abc56SHans de Goede 
102131abc56SHans de Goede 	vbox_write_ioport(VBE_DISPI_INDEX_ID, id);
103131abc56SHans de Goede 	dispi_id = inw(VBE_DISPI_IOPORT_DATA);
104131abc56SHans de Goede 
105131abc56SHans de Goede 	return dispi_id == id;
106131abc56SHans de Goede }
107131abc56SHans de Goede 
vbox_hw_init(struct vbox_private * vbox)108131abc56SHans de Goede int vbox_hw_init(struct vbox_private *vbox)
109131abc56SHans de Goede {
11056492fe9SThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(vbox->ddev.dev);
111131abc56SHans de Goede 	int ret = -ENOMEM;
112131abc56SHans de Goede 
113131abc56SHans de Goede 	vbox->full_vram_size = inl(VBE_DISPI_IOPORT_DATA);
114131abc56SHans de Goede 	vbox->any_pitch = vbox_check_supported(VBE_DISPI_ID_ANYX);
115131abc56SHans de Goede 
116131abc56SHans de Goede 	DRM_INFO("VRAM %08x\n", vbox->full_vram_size);
117131abc56SHans de Goede 
118131abc56SHans de Goede 	/* Map guest-heap at end of vram */
119131abc56SHans de Goede 	vbox->guest_heap =
12056492fe9SThomas Zimmermann 	    pci_iomap_range(pdev, 0, GUEST_HEAP_OFFSET(vbox),
121131abc56SHans de Goede 			    GUEST_HEAP_SIZE);
122131abc56SHans de Goede 	if (!vbox->guest_heap)
123131abc56SHans de Goede 		return -ENOMEM;
124131abc56SHans de Goede 
125131abc56SHans de Goede 	/* Create guest-heap mem-pool use 2^4 = 16 byte chunks */
1264cc9b565SDaniel Vetter 	vbox->guest_pool = devm_gen_pool_create(vbox->ddev.dev, 4, -1,
1274cc9b565SDaniel Vetter 						"vboxvideo-accel");
128*cebbb5c4SDan Carpenter 	if (IS_ERR(vbox->guest_pool))
129*cebbb5c4SDan Carpenter 		return PTR_ERR(vbox->guest_pool);
130131abc56SHans de Goede 
131131abc56SHans de Goede 	ret = gen_pool_add_virt(vbox->guest_pool,
132131abc56SHans de Goede 				(unsigned long)vbox->guest_heap,
133131abc56SHans de Goede 				GUEST_HEAP_OFFSET(vbox),
134131abc56SHans de Goede 				GUEST_HEAP_USABLE_SIZE, -1);
135131abc56SHans de Goede 	if (ret)
1364cc9b565SDaniel Vetter 		return ret;
137131abc56SHans de Goede 
138131abc56SHans de Goede 	ret = hgsmi_test_query_conf(vbox->guest_pool);
139131abc56SHans de Goede 	if (ret) {
140131abc56SHans de Goede 		DRM_ERROR("vboxvideo: hgsmi_test_query_conf failed\n");
1414cc9b565SDaniel Vetter 		return ret;
142131abc56SHans de Goede 	}
143131abc56SHans de Goede 
144131abc56SHans de Goede 	/* Reduce available VRAM size to reflect the guest heap. */
145131abc56SHans de Goede 	vbox->available_vram_size = GUEST_HEAP_OFFSET(vbox);
146131abc56SHans de Goede 	/* Linux drm represents monitors as a 32-bit array. */
147131abc56SHans de Goede 	hgsmi_query_conf(vbox->guest_pool, VBOX_VBVA_CONF32_MONITOR_COUNT,
148131abc56SHans de Goede 			 &vbox->num_crtcs);
149131abc56SHans de Goede 	vbox->num_crtcs = clamp_t(u32, vbox->num_crtcs, 1, VBOX_MAX_SCREENS);
150131abc56SHans de Goede 
151131abc56SHans de Goede 	if (!have_hgsmi_mode_hints(vbox)) {
152131abc56SHans de Goede 		ret = -ENOTSUPP;
1534cc9b565SDaniel Vetter 		return ret;
154131abc56SHans de Goede 	}
155131abc56SHans de Goede 
156131abc56SHans de Goede 	vbox->last_mode_hints = devm_kcalloc(vbox->ddev.dev, vbox->num_crtcs,
157131abc56SHans de Goede 					     sizeof(struct vbva_modehint),
158131abc56SHans de Goede 					     GFP_KERNEL);
1594cc9b565SDaniel Vetter 	if (!vbox->last_mode_hints)
1604cc9b565SDaniel Vetter 		return -ENOMEM;
161131abc56SHans de Goede 
162131abc56SHans de Goede 	ret = vbox_accel_init(vbox);
163131abc56SHans de Goede 	if (ret)
1644cc9b565SDaniel Vetter 		return ret;
165131abc56SHans de Goede 
166131abc56SHans de Goede 	return 0;
167131abc56SHans de Goede }
168131abc56SHans de Goede 
vbox_hw_fini(struct vbox_private * vbox)169131abc56SHans de Goede void vbox_hw_fini(struct vbox_private *vbox)
170131abc56SHans de Goede {
171131abc56SHans de Goede 	vbox_accel_fini(vbox);
172131abc56SHans de Goede }
173