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_drv.h
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 #ifndef __VBOX_DRV_H__
11131abc56SHans de Goede #define __VBOX_DRV_H__
12131abc56SHans de Goede 
13131abc56SHans de Goede #include <linux/genalloc.h>
14131abc56SHans de Goede #include <linux/io.h>
15131abc56SHans de Goede #include <linux/irqreturn.h>
16131abc56SHans de Goede #include <linux/string.h>
17131abc56SHans de Goede 
18131abc56SHans de Goede #include <drm/drm_encoder.h>
19131abc56SHans de Goede #include <drm/drm_gem.h>
205e6b9255SThomas Zimmermann #include <drm/drm_gem_vram_helper.h>
21131abc56SHans de Goede 
22131abc56SHans de Goede #include "vboxvideo_guest.h"
23131abc56SHans de Goede #include "vboxvideo_vbe.h"
24131abc56SHans de Goede #include "hgsmi_ch_setup.h"
25131abc56SHans de Goede 
26131abc56SHans de Goede #define DRIVER_NAME         "vboxvideo"
27131abc56SHans de Goede #define DRIVER_DESC         "Oracle VM VirtualBox Graphics Card"
28131abc56SHans de Goede #define DRIVER_DATE         "20130823"
29131abc56SHans de Goede 
30131abc56SHans de Goede #define DRIVER_MAJOR        1
31131abc56SHans de Goede #define DRIVER_MINOR        0
32131abc56SHans de Goede #define DRIVER_PATCHLEVEL   0
33131abc56SHans de Goede 
34131abc56SHans de Goede #define VBOX_MAX_CURSOR_WIDTH  64
35131abc56SHans de Goede #define VBOX_MAX_CURSOR_HEIGHT 64
36131abc56SHans de Goede #define CURSOR_PIXEL_COUNT (VBOX_MAX_CURSOR_WIDTH * VBOX_MAX_CURSOR_HEIGHT)
37131abc56SHans de Goede #define CURSOR_DATA_SIZE (CURSOR_PIXEL_COUNT * 4 + CURSOR_PIXEL_COUNT / 8)
38131abc56SHans de Goede 
39131abc56SHans de Goede #define VBOX_MAX_SCREENS  32
40131abc56SHans de Goede 
41131abc56SHans de Goede #define GUEST_HEAP_OFFSET(vbox) ((vbox)->full_vram_size - \
42131abc56SHans de Goede 				 VBVA_ADAPTER_INFORMATION_SIZE)
43131abc56SHans de Goede #define GUEST_HEAP_SIZE   VBVA_ADAPTER_INFORMATION_SIZE
44131abc56SHans de Goede #define GUEST_HEAP_USABLE_SIZE (VBVA_ADAPTER_INFORMATION_SIZE - \
45131abc56SHans de Goede 				sizeof(struct hgsmi_host_flags))
46131abc56SHans de Goede #define HOST_FLAGS_OFFSET GUEST_HEAP_USABLE_SIZE
47131abc56SHans de Goede 
48131abc56SHans de Goede struct vbox_private {
49131abc56SHans de Goede 	/* Must be first; or we must define our own release callback */
50131abc56SHans de Goede 	struct drm_device ddev;
51131abc56SHans de Goede 
52131abc56SHans de Goede 	u8 __iomem *guest_heap;
53131abc56SHans de Goede 	u8 __iomem *vbva_buffers;
54131abc56SHans de Goede 	struct gen_pool *guest_pool;
55131abc56SHans de Goede 	struct vbva_buf_ctx *vbva_info;
56131abc56SHans de Goede 	bool any_pitch;
57131abc56SHans de Goede 	u32 num_crtcs;
58131abc56SHans de Goede 	/* Amount of available VRAM, including space used for buffers. */
59131abc56SHans de Goede 	u32 full_vram_size;
60131abc56SHans de Goede 	/* Amount of available VRAM, not including space used for buffers. */
61131abc56SHans de Goede 	u32 available_vram_size;
62131abc56SHans de Goede 	/* Array of structures for receiving mode hints. */
63131abc56SHans de Goede 	struct vbva_modehint *last_mode_hints;
64131abc56SHans de Goede 
65131abc56SHans de Goede 	int fb_mtrr;
66131abc56SHans de Goede 
67131abc56SHans de Goede 	struct mutex hw_mutex; /* protects modeset and accel/vbva accesses */
68131abc56SHans de Goede 	struct work_struct hotplug_work;
69131abc56SHans de Goede 	u32 input_mapping_width;
70131abc56SHans de Goede 	u32 input_mapping_height;
71131abc56SHans de Goede 	/*
72131abc56SHans de Goede 	 * Is user-space using an X.Org-style layout of one large frame-buffer
73131abc56SHans de Goede 	 * encompassing all screen ones or is the fbdev console active?
74131abc56SHans de Goede 	 */
75131abc56SHans de Goede 	bool single_framebuffer;
76131abc56SHans de Goede 	u8 cursor_data[CURSOR_DATA_SIZE];
77131abc56SHans de Goede };
78131abc56SHans de Goede 
79131abc56SHans de Goede #undef CURSOR_PIXEL_COUNT
80131abc56SHans de Goede #undef CURSOR_DATA_SIZE
81131abc56SHans de Goede 
82131abc56SHans de Goede struct vbox_connector {
83131abc56SHans de Goede 	struct drm_connector base;
84131abc56SHans de Goede 	char name[32];
85131abc56SHans de Goede 	struct vbox_crtc *vbox_crtc;
86131abc56SHans de Goede 	struct {
87131abc56SHans de Goede 		u32 width;
88131abc56SHans de Goede 		u32 height;
89131abc56SHans de Goede 		bool disconnected;
90131abc56SHans de Goede 	} mode_hint;
91131abc56SHans de Goede };
92131abc56SHans de Goede 
93131abc56SHans de Goede struct vbox_crtc {
94131abc56SHans de Goede 	struct drm_crtc base;
95131abc56SHans de Goede 	bool disconnected;
96131abc56SHans de Goede 	unsigned int crtc_id;
97131abc56SHans de Goede 	u32 fb_offset;
98131abc56SHans de Goede 	bool cursor_enabled;
99131abc56SHans de Goede 	u32 x_hint;
100131abc56SHans de Goede 	u32 y_hint;
101131abc56SHans de Goede 	/*
102131abc56SHans de Goede 	 * When setting a mode we not only pass the mode to the hypervisor,
103131abc56SHans de Goede 	 * but also information on how to map / translate input coordinates
104131abc56SHans de Goede 	 * for the emulated USB tablet.  This input-mapping may change when
105131abc56SHans de Goede 	 * the mode on *another* crtc changes.
106131abc56SHans de Goede 	 *
107131abc56SHans de Goede 	 * This means that sometimes we must do a modeset on other crtc-s then
108131abc56SHans de Goede 	 * the one being changed to update the input-mapping. Including crtc-s
109131abc56SHans de Goede 	 * which may be disabled inside the guest (shown as a black window
110131abc56SHans de Goede 	 * on the host unless closed by the user).
111131abc56SHans de Goede 	 *
112131abc56SHans de Goede 	 * With atomic modesetting the mode-info of disabled crtcs gets zeroed
113131abc56SHans de Goede 	 * yet we need it when updating the input-map to avoid resizing the
114131abc56SHans de Goede 	 * window as a side effect of a mode_set on another crtc. Therefor we
115131abc56SHans de Goede 	 * cache the info of the last mode below.
116131abc56SHans de Goede 	 */
117131abc56SHans de Goede 	u32 width;
118131abc56SHans de Goede 	u32 height;
119131abc56SHans de Goede 	u32 x;
120131abc56SHans de Goede 	u32 y;
121131abc56SHans de Goede };
122131abc56SHans de Goede 
123131abc56SHans de Goede struct vbox_encoder {
124131abc56SHans de Goede 	struct drm_encoder base;
125131abc56SHans de Goede };
126131abc56SHans de Goede 
127131abc56SHans de Goede #define to_vbox_crtc(x) container_of(x, struct vbox_crtc, base)
128131abc56SHans de Goede #define to_vbox_connector(x) container_of(x, struct vbox_connector, base)
129131abc56SHans de Goede #define to_vbox_encoder(x) container_of(x, struct vbox_encoder, base)
130dd9a6893SDaniel Vetter #define to_vbox_dev(x) container_of(x, struct vbox_private, ddev)
131131abc56SHans de Goede 
132131abc56SHans de Goede bool vbox_check_supported(u16 id);
133131abc56SHans de Goede int vbox_hw_init(struct vbox_private *vbox);
134131abc56SHans de Goede void vbox_hw_fini(struct vbox_private *vbox);
135131abc56SHans de Goede 
136131abc56SHans de Goede int vbox_mode_init(struct vbox_private *vbox);
137131abc56SHans de Goede void vbox_mode_fini(struct vbox_private *vbox);
138131abc56SHans de Goede 
139131abc56SHans de Goede void vbox_report_caps(struct vbox_private *vbox);
140131abc56SHans de Goede 
141131abc56SHans de Goede int vbox_mm_init(struct vbox_private *vbox);
142131abc56SHans de Goede 
143131abc56SHans de Goede /* vbox_irq.c */
144131abc56SHans de Goede int vbox_irq_init(struct vbox_private *vbox);
145131abc56SHans de Goede void vbox_irq_fini(struct vbox_private *vbox);
146131abc56SHans de Goede void vbox_report_hotplug(struct vbox_private *vbox);
147131abc56SHans de Goede 
148131abc56SHans de Goede /* vbox_hgsmi.c */
149131abc56SHans de Goede void *hgsmi_buffer_alloc(struct gen_pool *guest_pool, size_t size,
150131abc56SHans de Goede 			 u8 channel, u16 channel_info);
151131abc56SHans de Goede void hgsmi_buffer_free(struct gen_pool *guest_pool, void *buf);
152131abc56SHans de Goede int hgsmi_buffer_submit(struct gen_pool *guest_pool, void *buf);
153131abc56SHans de Goede 
vbox_write_ioport(u16 index,u16 data)154131abc56SHans de Goede static inline void vbox_write_ioport(u16 index, u16 data)
155131abc56SHans de Goede {
156131abc56SHans de Goede 	outw(index, VBE_DISPI_IOPORT_INDEX);
157131abc56SHans de Goede 	outw(data, VBE_DISPI_IOPORT_DATA);
158131abc56SHans de Goede }
159131abc56SHans de Goede 
160131abc56SHans de Goede #endif
161