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 
22 #include <drm/ttm/ttm_bo_api.h>
23 #include <drm/ttm/ttm_bo_driver.h>
24 #include <drm/ttm/ttm_placement.h>
25 #include <drm/ttm/ttm_memory.h>
26 #include <drm/ttm/ttm_module.h>
27 
28 #include "vboxvideo_guest.h"
29 #include "vboxvideo_vbe.h"
30 #include "hgsmi_ch_setup.h"
31 
32 #define DRIVER_NAME         "vboxvideo"
33 #define DRIVER_DESC         "Oracle VM VirtualBox Graphics Card"
34 #define DRIVER_DATE         "20130823"
35 
36 #define DRIVER_MAJOR        1
37 #define DRIVER_MINOR        0
38 #define DRIVER_PATCHLEVEL   0
39 
40 #define VBOX_MAX_CURSOR_WIDTH  64
41 #define VBOX_MAX_CURSOR_HEIGHT 64
42 #define CURSOR_PIXEL_COUNT (VBOX_MAX_CURSOR_WIDTH * VBOX_MAX_CURSOR_HEIGHT)
43 #define CURSOR_DATA_SIZE (CURSOR_PIXEL_COUNT * 4 + CURSOR_PIXEL_COUNT / 8)
44 
45 #define VBOX_MAX_SCREENS  32
46 
47 #define GUEST_HEAP_OFFSET(vbox) ((vbox)->full_vram_size - \
48 				 VBVA_ADAPTER_INFORMATION_SIZE)
49 #define GUEST_HEAP_SIZE   VBVA_ADAPTER_INFORMATION_SIZE
50 #define GUEST_HEAP_USABLE_SIZE (VBVA_ADAPTER_INFORMATION_SIZE - \
51 				sizeof(struct hgsmi_host_flags))
52 #define HOST_FLAGS_OFFSET GUEST_HEAP_USABLE_SIZE
53 
54 struct vbox_framebuffer {
55 	struct drm_framebuffer base;
56 	struct drm_gem_object *obj;
57 };
58 
59 struct vbox_private {
60 	/* Must be first; or we must define our own release callback */
61 	struct drm_device ddev;
62 	struct drm_fb_helper fb_helper;
63 	struct vbox_framebuffer afb;
64 
65 	u8 __iomem *guest_heap;
66 	u8 __iomem *vbva_buffers;
67 	struct gen_pool *guest_pool;
68 	struct vbva_buf_ctx *vbva_info;
69 	bool any_pitch;
70 	u32 num_crtcs;
71 	/* Amount of available VRAM, including space used for buffers. */
72 	u32 full_vram_size;
73 	/* Amount of available VRAM, not including space used for buffers. */
74 	u32 available_vram_size;
75 	/* Array of structures for receiving mode hints. */
76 	struct vbva_modehint *last_mode_hints;
77 
78 	int fb_mtrr;
79 
80 	struct {
81 		struct ttm_bo_device bdev;
82 	} ttm;
83 
84 	struct mutex hw_mutex; /* protects modeset and accel/vbva accesses */
85 	struct work_struct hotplug_work;
86 	u32 input_mapping_width;
87 	u32 input_mapping_height;
88 	/*
89 	 * Is user-space using an X.Org-style layout of one large frame-buffer
90 	 * encompassing all screen ones or is the fbdev console active?
91 	 */
92 	bool single_framebuffer;
93 	u8 cursor_data[CURSOR_DATA_SIZE];
94 };
95 
96 #undef CURSOR_PIXEL_COUNT
97 #undef CURSOR_DATA_SIZE
98 
99 struct vbox_gem_object;
100 
101 struct vbox_connector {
102 	struct drm_connector base;
103 	char name[32];
104 	struct vbox_crtc *vbox_crtc;
105 	struct {
106 		u32 width;
107 		u32 height;
108 		bool disconnected;
109 	} mode_hint;
110 };
111 
112 struct vbox_crtc {
113 	struct drm_crtc base;
114 	bool disconnected;
115 	unsigned int crtc_id;
116 	u32 fb_offset;
117 	bool cursor_enabled;
118 	u32 x_hint;
119 	u32 y_hint;
120 	/*
121 	 * When setting a mode we not only pass the mode to the hypervisor,
122 	 * but also information on how to map / translate input coordinates
123 	 * for the emulated USB tablet.  This input-mapping may change when
124 	 * the mode on *another* crtc changes.
125 	 *
126 	 * This means that sometimes we must do a modeset on other crtc-s then
127 	 * the one being changed to update the input-mapping. Including crtc-s
128 	 * which may be disabled inside the guest (shown as a black window
129 	 * on the host unless closed by the user).
130 	 *
131 	 * With atomic modesetting the mode-info of disabled crtcs gets zeroed
132 	 * yet we need it when updating the input-map to avoid resizing the
133 	 * window as a side effect of a mode_set on another crtc. Therefor we
134 	 * cache the info of the last mode below.
135 	 */
136 	u32 width;
137 	u32 height;
138 	u32 x;
139 	u32 y;
140 };
141 
142 struct vbox_encoder {
143 	struct drm_encoder base;
144 };
145 
146 #define to_vbox_crtc(x) container_of(x, struct vbox_crtc, base)
147 #define to_vbox_connector(x) container_of(x, struct vbox_connector, base)
148 #define to_vbox_encoder(x) container_of(x, struct vbox_encoder, base)
149 #define to_vbox_framebuffer(x) container_of(x, struct vbox_framebuffer, base)
150 
151 bool vbox_check_supported(u16 id);
152 int vbox_hw_init(struct vbox_private *vbox);
153 void vbox_hw_fini(struct vbox_private *vbox);
154 
155 int vbox_mode_init(struct vbox_private *vbox);
156 void vbox_mode_fini(struct vbox_private *vbox);
157 
158 void vbox_report_caps(struct vbox_private *vbox);
159 
160 void vbox_framebuffer_dirty_rectangles(struct drm_framebuffer *fb,
161 				       struct drm_clip_rect *rects,
162 				       unsigned int num_rects);
163 
164 int vbox_framebuffer_init(struct vbox_private *vbox,
165 			  struct vbox_framebuffer *vbox_fb,
166 			  const struct drm_mode_fb_cmd2 *mode_cmd,
167 			  struct drm_gem_object *obj);
168 
169 int vboxfb_create(struct drm_fb_helper *helper,
170 		  struct drm_fb_helper_surface_size *sizes);
171 void vbox_fbdev_fini(struct vbox_private *vbox);
172 
173 struct vbox_bo {
174 	struct ttm_buffer_object bo;
175 	struct ttm_placement placement;
176 	struct ttm_bo_kmap_obj kmap;
177 	struct drm_gem_object gem;
178 	struct ttm_place placements[3];
179 	int pin_count;
180 };
181 
182 #define gem_to_vbox_bo(gobj) container_of((gobj), struct vbox_bo, gem)
183 
184 static inline struct vbox_bo *vbox_bo(struct ttm_buffer_object *bo)
185 {
186 	return container_of(bo, struct vbox_bo, bo);
187 }
188 
189 #define to_vbox_obj(x) container_of(x, struct vbox_gem_object, base)
190 
191 static inline u64 vbox_bo_gpu_offset(struct vbox_bo *bo)
192 {
193 	return bo->bo.offset;
194 }
195 
196 int vbox_dumb_create(struct drm_file *file,
197 		     struct drm_device *dev,
198 		     struct drm_mode_create_dumb *args);
199 
200 void vbox_gem_free_object(struct drm_gem_object *obj);
201 int vbox_dumb_mmap_offset(struct drm_file *file,
202 			  struct drm_device *dev,
203 			  u32 handle, u64 *offset);
204 
205 int vbox_mm_init(struct vbox_private *vbox);
206 void vbox_mm_fini(struct vbox_private *vbox);
207 
208 int vbox_bo_create(struct vbox_private *vbox, int size, int align,
209 		   u32 flags, struct vbox_bo **pvboxbo);
210 
211 int vbox_gem_create(struct vbox_private *vbox,
212 		    u32 size, bool iskernel, struct drm_gem_object **obj);
213 
214 int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag);
215 int vbox_bo_unpin(struct vbox_bo *bo);
216 
217 static inline int vbox_bo_reserve(struct vbox_bo *bo, bool no_wait)
218 {
219 	int ret;
220 
221 	ret = ttm_bo_reserve(&bo->bo, true, no_wait, NULL);
222 	if (ret) {
223 		if (ret != -ERESTARTSYS && ret != -EBUSY)
224 			DRM_ERROR("reserve failed %p\n", bo);
225 		return ret;
226 	}
227 	return 0;
228 }
229 
230 static inline void vbox_bo_unreserve(struct vbox_bo *bo)
231 {
232 	ttm_bo_unreserve(&bo->bo);
233 }
234 
235 void vbox_ttm_placement(struct vbox_bo *bo, int domain);
236 int vbox_bo_push_sysram(struct vbox_bo *bo);
237 int vbox_mmap(struct file *filp, struct vm_area_struct *vma);
238 void *vbox_bo_kmap(struct vbox_bo *bo);
239 void vbox_bo_kunmap(struct vbox_bo *bo);
240 
241 /* vbox_prime.c */
242 int vbox_gem_prime_pin(struct drm_gem_object *obj);
243 void vbox_gem_prime_unpin(struct drm_gem_object *obj);
244 struct sg_table *vbox_gem_prime_get_sg_table(struct drm_gem_object *obj);
245 struct drm_gem_object *vbox_gem_prime_import_sg_table(
246 	struct drm_device *dev, struct dma_buf_attachment *attach,
247 	struct sg_table *table);
248 void *vbox_gem_prime_vmap(struct drm_gem_object *obj);
249 void vbox_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
250 int vbox_gem_prime_mmap(struct drm_gem_object *obj,
251 			struct vm_area_struct *area);
252 
253 /* vbox_irq.c */
254 int vbox_irq_init(struct vbox_private *vbox);
255 void vbox_irq_fini(struct vbox_private *vbox);
256 void vbox_report_hotplug(struct vbox_private *vbox);
257 irqreturn_t vbox_irq_handler(int irq, void *arg);
258 
259 /* vbox_hgsmi.c */
260 void *hgsmi_buffer_alloc(struct gen_pool *guest_pool, size_t size,
261 			 u8 channel, u16 channel_info);
262 void hgsmi_buffer_free(struct gen_pool *guest_pool, void *buf);
263 int hgsmi_buffer_submit(struct gen_pool *guest_pool, void *buf);
264 
265 static inline void vbox_write_ioport(u16 index, u16 data)
266 {
267 	outw(index, VBE_DISPI_IOPORT_INDEX);
268 	outw(data, VBE_DISPI_IOPORT_DATA);
269 }
270 
271 #endif
272