1 | /* $Id: vbox_drv.h 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Additions Linux kernel video driver
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2019 Oracle Corporation
|
---|
8 | * This file is based on ast_drv.h
|
---|
9 | * Copyright 2012 Red Hat Inc.
|
---|
10 | *
|
---|
11 | * Permission is hereby granted, free of charge, to any person obtaining a
|
---|
12 | * copy of this software and associated documentation files (the
|
---|
13 | * "Software"), to deal in the Software without restriction, including
|
---|
14 | * without limitation the rights to use, copy, modify, merge, publish,
|
---|
15 | * distribute, sub license, and/or sell copies of the Software, and to
|
---|
16 | * permit persons to whom the Software is furnished to do so, subject to
|
---|
17 | * the following conditions:
|
---|
18 | *
|
---|
19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
---|
20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
---|
21 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
---|
22 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
---|
23 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
---|
24 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
---|
25 | * USE OR OTHER DEALINGS IN THE SOFTWARE.
|
---|
26 | *
|
---|
27 | * The above copyright notice and this permission notice (including the
|
---|
28 | * next paragraph) shall be included in all copies or substantial portions
|
---|
29 | * of the Software.
|
---|
30 | *
|
---|
31 | * Authors: Dave Airlie <[email protected]>
|
---|
32 | * Michael Thayer <[email protected],
|
---|
33 | * Hans de Goede <[email protected]>
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef __VBOX_DRV_H__
|
---|
37 | #define __VBOX_DRV_H__
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <linux/version.h>
|
---|
43 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 5, 0)
|
---|
44 | # include <linux/types.h>
|
---|
45 | # include <linux/spinlock_types.h>
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #include <linux/genalloc.h>
|
---|
49 | #include <linux/io.h>
|
---|
50 | #include <linux/string.h>
|
---|
51 |
|
---|
52 | #if defined(RHEL_MAJOR) && defined(RHEL_MINOR)
|
---|
53 | # if RHEL_MAJOR == 7 && RHEL_MINOR >= 6
|
---|
54 | # define RHEL_76
|
---|
55 | # endif
|
---|
56 | # if RHEL_MAJOR == 7 && RHEL_MINOR >= 5
|
---|
57 | # define RHEL_75
|
---|
58 | # endif
|
---|
59 | # if RHEL_MAJOR == 7 && RHEL_MINOR >= 4
|
---|
60 | # define RHEL_74
|
---|
61 | # endif
|
---|
62 | # if RHEL_MAJOR == 7 && RHEL_MINOR >= 3
|
---|
63 | # define RHEL_73
|
---|
64 | # endif
|
---|
65 | # if RHEL_MAJOR == 7 && RHEL_MINOR >= 2
|
---|
66 | # define RHEL_72
|
---|
67 | # endif
|
---|
68 | # if RHEL_MAJOR == 7 && RHEL_MINOR >= 1
|
---|
69 | # define RHEL_71
|
---|
70 | # endif
|
---|
71 | # if RHEL_MAJOR == 7 && RHEL_MINOR >= 0
|
---|
72 | # define RHEL_70
|
---|
73 | # endif
|
---|
74 | #endif
|
---|
75 |
|
---|
76 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 14, 0) || defined(RHEL_71)
|
---|
77 | #define U8_MAX ((u8)~0U)
|
---|
78 | #define S8_MAX ((s8)(U8_MAX>>1))
|
---|
79 | #define S8_MIN ((s8)(-S8_MAX - 1))
|
---|
80 | #define U16_MAX ((u16)~0U)
|
---|
81 | #define S16_MAX ((s16)(U16_MAX>>1))
|
---|
82 | #define S16_MIN ((s16)(-S16_MAX - 1))
|
---|
83 | #define U32_MAX ((u32)~0U)
|
---|
84 | #define S32_MAX ((s32)(U32_MAX>>1))
|
---|
85 | #define S32_MIN ((s32)(-S32_MAX - 1))
|
---|
86 | #define U64_MAX ((u64)~0ULL)
|
---|
87 | #define S64_MAX ((s64)(U64_MAX>>1))
|
---|
88 | #define S64_MIN ((s64)(-S64_MAX - 1))
|
---|
89 | #endif
|
---|
90 |
|
---|
91 | #include <drm/drmP.h>
|
---|
92 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) || defined(RHEL_75)
|
---|
93 | #include <drm/drm_encoder.h>
|
---|
94 | #endif
|
---|
95 | #include <drm/drm_fb_helper.h>
|
---|
96 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 18, 0) || defined(RHEL_72)
|
---|
97 | #include <drm/drm_gem.h>
|
---|
98 | #endif
|
---|
99 |
|
---|
100 | #include <drm/ttm/ttm_bo_api.h>
|
---|
101 | #include <drm/ttm/ttm_bo_driver.h>
|
---|
102 | #include <drm/ttm/ttm_placement.h>
|
---|
103 | #include <drm/ttm/ttm_memory.h>
|
---|
104 | #include <drm/ttm/ttm_module.h>
|
---|
105 |
|
---|
106 | #include "vboxvideo_guest.h"
|
---|
107 | #include "vboxvideo_vbe.h"
|
---|
108 | #include "hgsmi_ch_setup.h"
|
---|
109 |
|
---|
110 | #include "product-generated.h"
|
---|
111 |
|
---|
112 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0) && !defined(RHEL_75)
|
---|
113 | static inline void drm_gem_object_put_unlocked(struct drm_gem_object *obj)
|
---|
114 | {
|
---|
115 | drm_gem_object_unreference_unlocked(obj);
|
---|
116 | }
|
---|
117 | #endif
|
---|
118 |
|
---|
119 | #define DRIVER_AUTHOR VBOX_VENDOR
|
---|
120 |
|
---|
121 | #define DRIVER_NAME "vboxvideo"
|
---|
122 | #define DRIVER_DESC VBOX_PRODUCT " Graphics Card"
|
---|
123 | #define DRIVER_DATE "20130823"
|
---|
124 |
|
---|
125 | #define DRIVER_MAJOR 1
|
---|
126 | #define DRIVER_MINOR 0
|
---|
127 | #define DRIVER_PATCHLEVEL 0
|
---|
128 |
|
---|
129 | #define VBOX_MAX_CURSOR_WIDTH 64
|
---|
130 | #define VBOX_MAX_CURSOR_HEIGHT 64
|
---|
131 | #define CURSOR_PIXEL_COUNT (VBOX_MAX_CURSOR_WIDTH * VBOX_MAX_CURSOR_HEIGHT)
|
---|
132 | #define CURSOR_DATA_SIZE (CURSOR_PIXEL_COUNT * 4 + CURSOR_PIXEL_COUNT / 8)
|
---|
133 |
|
---|
134 | #define VBOX_MAX_SCREENS 32
|
---|
135 |
|
---|
136 | #define GUEST_HEAP_OFFSET(vbox) ((vbox)->full_vram_size - \
|
---|
137 | VBVA_ADAPTER_INFORMATION_SIZE)
|
---|
138 | #define GUEST_HEAP_SIZE VBVA_ADAPTER_INFORMATION_SIZE
|
---|
139 | #define GUEST_HEAP_USABLE_SIZE (VBVA_ADAPTER_INFORMATION_SIZE - \
|
---|
140 | sizeof(HGSMIHOSTFLAGS))
|
---|
141 | #define HOST_FLAGS_OFFSET GUEST_HEAP_USABLE_SIZE
|
---|
142 |
|
---|
143 | /** How frequently we refresh if the guest is not providing dirty rectangles. */
|
---|
144 | #define VBOX_REFRESH_PERIOD (HZ / 2)
|
---|
145 |
|
---|
146 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 13, 0) && !defined(RHEL_72)
|
---|
147 | static inline void *devm_kcalloc(struct device *dev, size_t n, size_t size,
|
---|
148 | gfp_t flags)
|
---|
149 | {
|
---|
150 | return devm_kzalloc(dev, n * size, flags);
|
---|
151 | }
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | struct vbox_fbdev;
|
---|
155 |
|
---|
156 | struct vbox_private {
|
---|
157 | struct drm_device *dev;
|
---|
158 |
|
---|
159 | u8 __iomem *guest_heap;
|
---|
160 | u8 __iomem *vbva_buffers;
|
---|
161 | struct gen_pool *guest_pool;
|
---|
162 | struct VBVABUFFERCONTEXT *vbva_info;
|
---|
163 | bool any_pitch;
|
---|
164 | u32 num_crtcs;
|
---|
165 | /** Amount of available VRAM, including space used for buffers. */
|
---|
166 | u32 full_vram_size;
|
---|
167 | /** Amount of available VRAM, not including space used for buffers. */
|
---|
168 | u32 available_vram_size;
|
---|
169 | /** Array of structures for receiving mode hints. */
|
---|
170 | VBVAMODEHINT *last_mode_hints;
|
---|
171 |
|
---|
172 | struct vbox_fbdev *fbdev;
|
---|
173 |
|
---|
174 | int fb_mtrr;
|
---|
175 |
|
---|
176 | struct {
|
---|
177 | struct drm_global_reference mem_global_ref;
|
---|
178 | struct ttm_bo_global_ref bo_global_ref;
|
---|
179 | struct ttm_bo_device bdev;
|
---|
180 | bool mm_initialised;
|
---|
181 | } ttm;
|
---|
182 |
|
---|
183 | struct mutex hw_mutex; /* protects modeset and accel/vbva accesses */
|
---|
184 | /**
|
---|
185 | * We decide whether or not user-space supports display hot-plug
|
---|
186 | * depending on whether they react to a hot-plug event after the initial
|
---|
187 | * mode query.
|
---|
188 | */
|
---|
189 | bool initial_mode_queried;
|
---|
190 | /**
|
---|
191 | * Do we know that the current user can send us dirty rectangle information?
|
---|
192 | * If not, do periodic refreshes until we do know.
|
---|
193 | */
|
---|
194 | bool need_refresh_timer;
|
---|
195 | /**
|
---|
196 | * As long as the user is not sending us dirty rectangle information,
|
---|
197 | * refresh the whole screen at regular intervals.
|
---|
198 | */
|
---|
199 | struct delayed_work refresh_work;
|
---|
200 | struct work_struct hotplug_work;
|
---|
201 | u32 input_mapping_width;
|
---|
202 | u32 input_mapping_height;
|
---|
203 | /**
|
---|
204 | * Is user-space using an X.Org-style layout of one large frame-buffer
|
---|
205 | * encompassing all screen ones or is the fbdev console active?
|
---|
206 | */
|
---|
207 | bool single_framebuffer;
|
---|
208 | u32 cursor_width;
|
---|
209 | u32 cursor_height;
|
---|
210 | u32 cursor_hot_x;
|
---|
211 | u32 cursor_hot_y;
|
---|
212 | size_t cursor_data_size;
|
---|
213 | u8 cursor_data[CURSOR_DATA_SIZE];
|
---|
214 | };
|
---|
215 |
|
---|
216 | #undef CURSOR_PIXEL_COUNT
|
---|
217 | #undef CURSOR_DATA_SIZE
|
---|
218 |
|
---|
219 | int vbox_driver_load(struct drm_device *dev, unsigned long flags);
|
---|
220 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) || defined(RHEL_75)
|
---|
221 | void vbox_driver_unload(struct drm_device *dev);
|
---|
222 | #else
|
---|
223 | int vbox_driver_unload(struct drm_device *dev);
|
---|
224 | #endif
|
---|
225 | void vbox_driver_lastclose(struct drm_device *dev);
|
---|
226 |
|
---|
227 | struct vbox_gem_object;
|
---|
228 |
|
---|
229 | #ifndef VGA_PORT_HGSMI_HOST
|
---|
230 | #define VGA_PORT_HGSMI_HOST 0x3b0
|
---|
231 | #define VGA_PORT_HGSMI_GUEST 0x3d0
|
---|
232 | #endif
|
---|
233 |
|
---|
234 | struct vbox_connector {
|
---|
235 | struct drm_connector base;
|
---|
236 | char name[32];
|
---|
237 | struct vbox_crtc *vbox_crtc;
|
---|
238 | struct {
|
---|
239 | u16 width;
|
---|
240 | u16 height;
|
---|
241 | bool disconnected;
|
---|
242 | } mode_hint;
|
---|
243 | };
|
---|
244 |
|
---|
245 | struct vbox_crtc {
|
---|
246 | struct drm_crtc base;
|
---|
247 | bool blanked;
|
---|
248 | bool disconnected;
|
---|
249 | unsigned int crtc_id;
|
---|
250 | u32 fb_offset;
|
---|
251 | bool cursor_enabled;
|
---|
252 | u32 x_hint;
|
---|
253 | u32 y_hint;
|
---|
254 | };
|
---|
255 |
|
---|
256 | struct vbox_encoder {
|
---|
257 | struct drm_encoder base;
|
---|
258 | };
|
---|
259 |
|
---|
260 | struct vbox_framebuffer {
|
---|
261 | struct drm_framebuffer base;
|
---|
262 | struct drm_gem_object *obj;
|
---|
263 | };
|
---|
264 |
|
---|
265 | struct vbox_fbdev {
|
---|
266 | struct drm_fb_helper helper;
|
---|
267 | struct vbox_framebuffer afb;
|
---|
268 | int size;
|
---|
269 | struct ttm_bo_kmap_obj mapping;
|
---|
270 | int x1, y1, x2, y2; /* dirty rect */
|
---|
271 | spinlock_t dirty_lock;
|
---|
272 | };
|
---|
273 |
|
---|
274 | #define to_vbox_crtc(x) container_of(x, struct vbox_crtc, base)
|
---|
275 | #define to_vbox_connector(x) container_of(x, struct vbox_connector, base)
|
---|
276 | #define to_vbox_encoder(x) container_of(x, struct vbox_encoder, base)
|
---|
277 | #define to_vbox_framebuffer(x) container_of(x, struct vbox_framebuffer, base)
|
---|
278 |
|
---|
279 | int vbox_mode_init(struct drm_device *dev);
|
---|
280 | void vbox_mode_fini(struct drm_device *dev);
|
---|
281 |
|
---|
282 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 3, 0)
|
---|
283 | #define DRM_MODE_FB_CMD drm_mode_fb_cmd
|
---|
284 | #else
|
---|
285 | #define DRM_MODE_FB_CMD drm_mode_fb_cmd2
|
---|
286 | #endif
|
---|
287 |
|
---|
288 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0) && !defined(RHEL_71)
|
---|
289 | #define CRTC_FB(crtc) ((crtc)->fb)
|
---|
290 | #else
|
---|
291 | #define CRTC_FB(crtc) ((crtc)->primary->fb)
|
---|
292 | #endif
|
---|
293 |
|
---|
294 | void vbox_enable_accel(struct vbox_private *vbox);
|
---|
295 | void vbox_disable_accel(struct vbox_private *vbox);
|
---|
296 | void vbox_report_caps(struct vbox_private *vbox);
|
---|
297 |
|
---|
298 | void vbox_framebuffer_dirty_rectangles(struct drm_framebuffer *fb,
|
---|
299 | struct drm_clip_rect *rects,
|
---|
300 | unsigned int num_rects);
|
---|
301 |
|
---|
302 | int vbox_framebuffer_init(struct drm_device *dev,
|
---|
303 | struct vbox_framebuffer *vbox_fb,
|
---|
304 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 5, 0) || defined(RHEL_73)
|
---|
305 | const struct DRM_MODE_FB_CMD *mode_cmd,
|
---|
306 | #else
|
---|
307 | struct DRM_MODE_FB_CMD *mode_cmd,
|
---|
308 | #endif
|
---|
309 | struct drm_gem_object *obj);
|
---|
310 |
|
---|
311 | int vbox_fbdev_init(struct drm_device *dev);
|
---|
312 | void vbox_fbdev_fini(struct drm_device *dev);
|
---|
313 | void vbox_fbdev_set_base(struct vbox_private *vbox, unsigned long gpu_addr);
|
---|
314 |
|
---|
315 | struct vbox_bo {
|
---|
316 | struct ttm_buffer_object bo;
|
---|
317 | struct ttm_placement placement;
|
---|
318 | struct ttm_bo_kmap_obj kmap;
|
---|
319 | struct drm_gem_object gem;
|
---|
320 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0) && !defined(RHEL_72)
|
---|
321 | u32 placements[3];
|
---|
322 | #else
|
---|
323 | struct ttm_place placements[3];
|
---|
324 | #endif
|
---|
325 | int pin_count;
|
---|
326 | };
|
---|
327 |
|
---|
328 | #define gem_to_vbox_bo(gobj) container_of((gobj), struct vbox_bo, gem)
|
---|
329 |
|
---|
330 | static inline struct vbox_bo *vbox_bo(struct ttm_buffer_object *bo)
|
---|
331 | {
|
---|
332 | return container_of(bo, struct vbox_bo, bo);
|
---|
333 | }
|
---|
334 |
|
---|
335 | #define to_vbox_obj(x) container_of(x, struct vbox_gem_object, base)
|
---|
336 |
|
---|
337 | int vbox_dumb_create(struct drm_file *file,
|
---|
338 | struct drm_device *dev,
|
---|
339 | struct drm_mode_create_dumb *args);
|
---|
340 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0) && !defined(RHEL_73)
|
---|
341 | int vbox_dumb_destroy(struct drm_file *file,
|
---|
342 | struct drm_device *dev, u32 handle);
|
---|
343 | #endif
|
---|
344 |
|
---|
345 | void vbox_gem_free_object(struct drm_gem_object *obj);
|
---|
346 | int vbox_dumb_mmap_offset(struct drm_file *file,
|
---|
347 | struct drm_device *dev,
|
---|
348 | u32 handle, u64 *offset);
|
---|
349 |
|
---|
350 | #define DRM_FILE_PAGE_OFFSET (0x10000000ULL >> PAGE_SHIFT)
|
---|
351 |
|
---|
352 | int vbox_mm_init(struct vbox_private *vbox);
|
---|
353 | void vbox_mm_fini(struct vbox_private *vbox);
|
---|
354 |
|
---|
355 | int vbox_bo_create(struct drm_device *dev, int size, int align,
|
---|
356 | u32 flags, struct vbox_bo **pvboxbo);
|
---|
357 |
|
---|
358 | int vbox_gem_create(struct drm_device *dev,
|
---|
359 | u32 size, bool iskernel, struct drm_gem_object **obj);
|
---|
360 |
|
---|
361 | int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag, u64 *gpu_addr);
|
---|
362 | int vbox_bo_unpin(struct vbox_bo *bo);
|
---|
363 |
|
---|
364 | static inline int vbox_bo_reserve(struct vbox_bo *bo, bool no_wait)
|
---|
365 | {
|
---|
366 | int ret;
|
---|
367 |
|
---|
368 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0) || defined(RHEL_74)
|
---|
369 | ret = ttm_bo_reserve(&bo->bo, true, no_wait, NULL);
|
---|
370 | #else
|
---|
371 | ret = ttm_bo_reserve(&bo->bo, true, no_wait, false, 0);
|
---|
372 | #endif
|
---|
373 | if (ret) {
|
---|
374 | if (ret != -ERESTARTSYS && ret != -EBUSY)
|
---|
375 | DRM_ERROR("reserve failed %p\n", bo);
|
---|
376 | return ret;
|
---|
377 | }
|
---|
378 | return 0;
|
---|
379 | }
|
---|
380 |
|
---|
381 | static inline void vbox_bo_unreserve(struct vbox_bo *bo)
|
---|
382 | {
|
---|
383 | ttm_bo_unreserve(&bo->bo);
|
---|
384 | }
|
---|
385 |
|
---|
386 | void vbox_ttm_placement(struct vbox_bo *bo, int domain);
|
---|
387 | int vbox_bo_push_sysram(struct vbox_bo *bo);
|
---|
388 | int vbox_mmap(struct file *filp, struct vm_area_struct *vma);
|
---|
389 |
|
---|
390 | /* vbox_prime.c */
|
---|
391 | int vbox_gem_prime_pin(struct drm_gem_object *obj);
|
---|
392 | void vbox_gem_prime_unpin(struct drm_gem_object *obj);
|
---|
393 | struct sg_table *vbox_gem_prime_get_sg_table(struct drm_gem_object *obj);
|
---|
394 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 18, 0) && !defined(RHEL_72)
|
---|
395 | struct drm_gem_object *vbox_gem_prime_import_sg_table(
|
---|
396 | struct drm_device *dev, size_t size, struct sg_table *table);
|
---|
397 | #else
|
---|
398 | struct drm_gem_object *vbox_gem_prime_import_sg_table(
|
---|
399 | struct drm_device *dev, struct dma_buf_attachment *attach,
|
---|
400 | struct sg_table *table);
|
---|
401 | #endif
|
---|
402 | void *vbox_gem_prime_vmap(struct drm_gem_object *obj);
|
---|
403 | void vbox_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
|
---|
404 | int vbox_gem_prime_mmap(struct drm_gem_object *obj,
|
---|
405 | struct vm_area_struct *area);
|
---|
406 |
|
---|
407 | /* vbox_irq.c */
|
---|
408 | int vbox_irq_init(struct vbox_private *vbox);
|
---|
409 | void vbox_irq_fini(struct vbox_private *vbox);
|
---|
410 | void vbox_report_hotplug(struct vbox_private *vbox);
|
---|
411 | irqreturn_t vbox_irq_handler(int irq, void *arg);
|
---|
412 |
|
---|
413 | /* vbox_hgsmi.c */
|
---|
414 | void *hgsmi_buffer_alloc(struct gen_pool *guest_pool, size_t size,
|
---|
415 | u8 channel, u16 channel_info);
|
---|
416 | void hgsmi_buffer_free(struct gen_pool *guest_pool, void *buf);
|
---|
417 | int hgsmi_buffer_submit(struct gen_pool *guest_pool, void *buf);
|
---|
418 |
|
---|
419 | static inline void vbox_write_ioport(u16 index, u16 data)
|
---|
420 | {
|
---|
421 | outw(index, VBE_DISPI_IOPORT_INDEX);
|
---|
422 | outw(data, VBE_DISPI_IOPORT_DATA);
|
---|
423 | }
|
---|
424 |
|
---|
425 | #endif
|
---|