1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright (C) 2013-2017 Oracle Corporation
4  * This file is based on ast_drv.h
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 #ifndef __VBOX_DRV_H__
11 #define __VBOX_DRV_H__
12 
13 #include <linux/genalloc.h>
14 #include <linux/io.h>
15 #include <linux/irqreturn.h>
16 #include <linux/string.h>
17 
18 #include <drm/drm_encoder.h>
19 #include <drm/drm_fb_helper.h>
20 #include <drm/drm_gem.h>
21 #include <drm/drm_gem_vram_helper.h>
22 
23 #include "vboxvideo_guest.h"
24 #include "vboxvideo_vbe.h"
25 #include "hgsmi_ch_setup.h"
26 
27 #define DRIVER_NAME         "vboxvideo"
28 #define DRIVER_DESC         "Oracle VM VirtualBox Graphics Card"
29 #define DRIVER_DATE         "20130823"
30 
31 #define DRIVER_MAJOR        1
32 #define DRIVER_MINOR        0
33 #define DRIVER_PATCHLEVEL   0
34 
35 #define VBOX_MAX_CURSOR_WIDTH  64
36 #define VBOX_MAX_CURSOR_HEIGHT 64
37 #define CURSOR_PIXEL_COUNT (VBOX_MAX_CURSOR_WIDTH * VBOX_MAX_CURSOR_HEIGHT)
38 #define CURSOR_DATA_SIZE (CURSOR_PIXEL_COUNT * 4 + CURSOR_PIXEL_COUNT / 8)
39 
40 #define VBOX_MAX_SCREENS  32
41 
42 #define GUEST_HEAP_OFFSET(vbox) ((vbox)->full_vram_size - \
43 				 VBVA_ADAPTER_INFORMATION_SIZE)
44 #define GUEST_HEAP_SIZE   VBVA_ADAPTER_INFORMATION_SIZE
45 #define GUEST_HEAP_USABLE_SIZE (VBVA_ADAPTER_INFORMATION_SIZE - \
46 				sizeof(struct hgsmi_host_flags))
47 #define HOST_FLAGS_OFFSET GUEST_HEAP_USABLE_SIZE
48 
49 struct vbox_framebuffer {
50 	struct drm_framebuffer base;
51 	struct drm_gem_object *obj;
52 };
53 
54 struct vbox_private {
55 	/* Must be first; or we must define our own release callback */
56 	struct drm_device ddev;
57 	struct drm_fb_helper fb_helper;
58 	struct vbox_framebuffer afb;
59 
60 	u8 __iomem *guest_heap;
61 	u8 __iomem *vbva_buffers;
62 	struct gen_pool *guest_pool;
63 	struct vbva_buf_ctx *vbva_info;
64 	bool any_pitch;
65 	u32 num_crtcs;
66 	/* Amount of available VRAM, including space used for buffers. */
67 	u32 full_vram_size;
68 	/* Amount of available VRAM, not including space used for buffers. */
69 	u32 available_vram_size;
70 	/* Array of structures for receiving mode hints. */
71 	struct vbva_modehint *last_mode_hints;
72 
73 	int fb_mtrr;
74 
75 	struct mutex hw_mutex; /* protects modeset and accel/vbva accesses */
76 	struct work_struct hotplug_work;
77 	u32 input_mapping_width;
78 	u32 input_mapping_height;
79 	/*
80 	 * Is user-space using an X.Org-style layout of one large frame-buffer
81 	 * encompassing all screen ones or is the fbdev console active?
82 	 */
83 	bool single_framebuffer;
84 	u8 cursor_data[CURSOR_DATA_SIZE];
85 };
86 
87 #undef CURSOR_PIXEL_COUNT
88 #undef CURSOR_DATA_SIZE
89 
90 struct vbox_connector {
91 	struct drm_connector base;
92 	char name[32];
93 	struct vbox_crtc *vbox_crtc;
94 	struct {
95 		u32 width;
96 		u32 height;
97 		bool disconnected;
98 	} mode_hint;
99 };
100 
101 struct vbox_crtc {
102 	struct drm_crtc base;
103 	bool disconnected;
104 	unsigned int crtc_id;
105 	u32 fb_offset;
106 	bool cursor_enabled;
107 	u32 x_hint;
108 	u32 y_hint;
109 	/*
110 	 * When setting a mode we not only pass the mode to the hypervisor,
111 	 * but also information on how to map / translate input coordinates
112 	 * for the emulated USB tablet.  This input-mapping may change when
113 	 * the mode on *another* crtc changes.
114 	 *
115 	 * This means that sometimes we must do a modeset on other crtc-s then
116 	 * the one being changed to update the input-mapping. Including crtc-s
117 	 * which may be disabled inside the guest (shown as a black window
118 	 * on the host unless closed by the user).
119 	 *
120 	 * With atomic modesetting the mode-info of disabled crtcs gets zeroed
121 	 * yet we need it when updating the input-map to avoid resizing the
122 	 * window as a side effect of a mode_set on another crtc. Therefor we
123 	 * cache the info of the last mode below.
124 	 */
125 	u32 width;
126 	u32 height;
127 	u32 x;
128 	u32 y;
129 };
130 
131 struct vbox_encoder {
132 	struct drm_encoder base;
133 };
134 
135 #define to_vbox_crtc(x) container_of(x, struct vbox_crtc, base)
136 #define to_vbox_connector(x) container_of(x, struct vbox_connector, base)
137 #define to_vbox_encoder(x) container_of(x, struct vbox_encoder, base)
138 #define to_vbox_framebuffer(x) container_of(x, struct vbox_framebuffer, base)
139 
140 bool vbox_check_supported(u16 id);
141 int vbox_hw_init(struct vbox_private *vbox);
142 void vbox_hw_fini(struct vbox_private *vbox);
143 
144 int vbox_mode_init(struct vbox_private *vbox);
145 void vbox_mode_fini(struct vbox_private *vbox);
146 
147 void vbox_report_caps(struct vbox_private *vbox);
148 
149 void vbox_framebuffer_dirty_rectangles(struct drm_framebuffer *fb,
150 				       struct drm_clip_rect *rects,
151 				       unsigned int num_rects);
152 
153 int vbox_framebuffer_init(struct vbox_private *vbox,
154 			  struct vbox_framebuffer *vbox_fb,
155 			  const struct drm_mode_fb_cmd2 *mode_cmd,
156 			  struct drm_gem_object *obj);
157 
158 int vboxfb_create(struct drm_fb_helper *helper,
159 		  struct drm_fb_helper_surface_size *sizes);
160 void vbox_fbdev_fini(struct vbox_private *vbox);
161 
162 int vbox_mm_init(struct vbox_private *vbox);
163 void vbox_mm_fini(struct vbox_private *vbox);
164 
165 int vbox_gem_create(struct vbox_private *vbox,
166 		    u32 size, bool iskernel, struct drm_gem_object **obj);
167 
168 /* vbox_irq.c */
169 int vbox_irq_init(struct vbox_private *vbox);
170 void vbox_irq_fini(struct vbox_private *vbox);
171 void vbox_report_hotplug(struct vbox_private *vbox);
172 irqreturn_t vbox_irq_handler(int irq, void *arg);
173 
174 /* vbox_hgsmi.c */
175 void *hgsmi_buffer_alloc(struct gen_pool *guest_pool, size_t size,
176 			 u8 channel, u16 channel_info);
177 void hgsmi_buffer_free(struct gen_pool *guest_pool, void *buf);
178 int hgsmi_buffer_submit(struct gen_pool *guest_pool, void *buf);
179 
180 static inline void vbox_write_ioport(u16 index, u16 data)
181 {
182 	outw(index, VBE_DISPI_IOPORT_INDEX);
183 	outw(data, VBE_DISPI_IOPORT_DATA);
184 }
185 
186 #endif
187