Merge branch 'linux-4.20' of git://github.com/skeggsb/linux into drm-fixes
[sfrench/cifs-2.6.git] / drivers / staging / vboxvideo / vbox_drv.h
1 /*
2  * Copyright (C) 2013-2017 Oracle Corporation
3  * This file is based on ast_drv.h
4  * Copyright 2012 Red Hat Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20  * USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * The above copyright notice and this permission notice (including the
23  * next paragraph) shall be included in all copies or substantial portions
24  * of the Software.
25  *
26  * Authors: Dave Airlie <airlied@redhat.com>
27  *          Michael Thayer <michael.thayer@oracle.com,
28  *          Hans de Goede <hdegoede@redhat.com>
29  */
30 #ifndef __VBOX_DRV_H__
31 #define __VBOX_DRV_H__
32
33 #include <linux/genalloc.h>
34 #include <linux/io.h>
35 #include <linux/string.h>
36 #include <linux/version.h>
37
38 #include <drm/drmP.h>
39 #include <drm/drm_encoder.h>
40 #include <drm/drm_fb_helper.h>
41 #include <drm/drm_gem.h>
42
43 #include <drm/ttm/ttm_bo_api.h>
44 #include <drm/ttm/ttm_bo_driver.h>
45 #include <drm/ttm/ttm_placement.h>
46 #include <drm/ttm/ttm_memory.h>
47 #include <drm/ttm/ttm_module.h>
48
49 #include "vboxvideo_guest.h"
50 #include "vboxvideo_vbe.h"
51 #include "hgsmi_ch_setup.h"
52
53 #define DRIVER_NAME         "vboxvideo"
54 #define DRIVER_DESC         "Oracle VM VirtualBox Graphics Card"
55 #define DRIVER_DATE         "20130823"
56
57 #define DRIVER_MAJOR        1
58 #define DRIVER_MINOR        0
59 #define DRIVER_PATCHLEVEL   0
60
61 #define VBOX_MAX_CURSOR_WIDTH  64
62 #define VBOX_MAX_CURSOR_HEIGHT 64
63 #define CURSOR_PIXEL_COUNT (VBOX_MAX_CURSOR_WIDTH * VBOX_MAX_CURSOR_HEIGHT)
64 #define CURSOR_DATA_SIZE (CURSOR_PIXEL_COUNT * 4 + CURSOR_PIXEL_COUNT / 8)
65
66 #define VBOX_MAX_SCREENS  32
67
68 #define GUEST_HEAP_OFFSET(vbox) ((vbox)->full_vram_size - \
69                                  VBVA_ADAPTER_INFORMATION_SIZE)
70 #define GUEST_HEAP_SIZE   VBVA_ADAPTER_INFORMATION_SIZE
71 #define GUEST_HEAP_USABLE_SIZE (VBVA_ADAPTER_INFORMATION_SIZE - \
72                                 sizeof(struct hgsmi_host_flags))
73 #define HOST_FLAGS_OFFSET GUEST_HEAP_USABLE_SIZE
74
75 struct vbox_framebuffer {
76         struct drm_framebuffer base;
77         struct drm_gem_object *obj;
78 };
79
80 struct vbox_private {
81         /* Must be first; or we must define our own release callback */
82         struct drm_device ddev;
83         struct drm_fb_helper fb_helper;
84         struct vbox_framebuffer afb;
85
86         u8 __iomem *guest_heap;
87         u8 __iomem *vbva_buffers;
88         struct gen_pool *guest_pool;
89         struct vbva_buf_ctx *vbva_info;
90         bool any_pitch;
91         u32 num_crtcs;
92         /** Amount of available VRAM, including space used for buffers. */
93         u32 full_vram_size;
94         /** Amount of available VRAM, not including space used for buffers. */
95         u32 available_vram_size;
96         /** Array of structures for receiving mode hints. */
97         struct vbva_modehint *last_mode_hints;
98
99         int fb_mtrr;
100
101         struct {
102                 struct drm_global_reference mem_global_ref;
103                 struct ttm_bo_global_ref bo_global_ref;
104                 struct ttm_bo_device bdev;
105         } ttm;
106
107         struct mutex hw_mutex; /* protects modeset and accel/vbva accesses */
108         /**
109          * We decide whether or not user-space supports display hot-plug
110          * depending on whether they react to a hot-plug event after the initial
111          * mode query.
112          */
113         bool initial_mode_queried;
114         struct work_struct hotplug_work;
115         u32 input_mapping_width;
116         u32 input_mapping_height;
117         /**
118          * Is user-space using an X.Org-style layout of one large frame-buffer
119          * encompassing all screen ones or is the fbdev console active?
120          */
121         bool single_framebuffer;
122         u8 cursor_data[CURSOR_DATA_SIZE];
123 };
124
125 #undef CURSOR_PIXEL_COUNT
126 #undef CURSOR_DATA_SIZE
127
128 struct vbox_gem_object;
129
130 struct vbox_connector {
131         struct drm_connector base;
132         char name[32];
133         struct vbox_crtc *vbox_crtc;
134         struct {
135                 u32 width;
136                 u32 height;
137                 bool disconnected;
138         } mode_hint;
139 };
140
141 struct vbox_crtc {
142         struct drm_crtc base;
143         bool disconnected;
144         unsigned int crtc_id;
145         u32 fb_offset;
146         bool cursor_enabled;
147         u32 x_hint;
148         u32 y_hint;
149         /*
150          * When setting a mode we not only pass the mode to the hypervisor,
151          * but also information on how to map / translate input coordinates
152          * for the emulated USB tablet.  This input-mapping may change when
153          * the mode on *another* crtc changes.
154          *
155          * This means that sometimes we must do a modeset on other crtc-s then
156          * the one being changed to update the input-mapping. Including crtc-s
157          * which may be disabled inside the guest (shown as a black window
158          * on the host unless closed by the user).
159          *
160          * With atomic modesetting the mode-info of disabled crtcs gets zeroed
161          * yet we need it when updating the input-map to avoid resizing the
162          * window as a side effect of a mode_set on another crtc. Therefor we
163          * cache the info of the last mode below.
164          */
165         u32 width;
166         u32 height;
167         u32 x;
168         u32 y;
169 };
170
171 struct vbox_encoder {
172         struct drm_encoder base;
173 };
174
175 #define to_vbox_crtc(x) container_of(x, struct vbox_crtc, base)
176 #define to_vbox_connector(x) container_of(x, struct vbox_connector, base)
177 #define to_vbox_encoder(x) container_of(x, struct vbox_encoder, base)
178 #define to_vbox_framebuffer(x) container_of(x, struct vbox_framebuffer, base)
179
180 bool vbox_check_supported(u16 id);
181 int vbox_hw_init(struct vbox_private *vbox);
182 void vbox_hw_fini(struct vbox_private *vbox);
183
184 int vbox_mode_init(struct vbox_private *vbox);
185 void vbox_mode_fini(struct vbox_private *vbox);
186
187 #define DRM_MODE_FB_CMD drm_mode_fb_cmd2
188
189 void vbox_enable_accel(struct vbox_private *vbox);
190 void vbox_disable_accel(struct vbox_private *vbox);
191 void vbox_report_caps(struct vbox_private *vbox);
192
193 void vbox_framebuffer_dirty_rectangles(struct drm_framebuffer *fb,
194                                        struct drm_clip_rect *rects,
195                                        unsigned int num_rects);
196
197 int vbox_framebuffer_init(struct vbox_private *vbox,
198                           struct vbox_framebuffer *vbox_fb,
199                           const struct DRM_MODE_FB_CMD *mode_cmd,
200                           struct drm_gem_object *obj);
201
202 int vboxfb_create(struct drm_fb_helper *helper,
203                   struct drm_fb_helper_surface_size *sizes);
204 void vbox_fbdev_fini(struct vbox_private *vbox);
205
206 struct vbox_bo {
207         struct ttm_buffer_object bo;
208         struct ttm_placement placement;
209         struct ttm_bo_kmap_obj kmap;
210         struct drm_gem_object gem;
211         struct ttm_place placements[3];
212         int pin_count;
213 };
214
215 #define gem_to_vbox_bo(gobj) container_of((gobj), struct vbox_bo, gem)
216
217 static inline struct vbox_bo *vbox_bo(struct ttm_buffer_object *bo)
218 {
219         return container_of(bo, struct vbox_bo, bo);
220 }
221
222 #define to_vbox_obj(x) container_of(x, struct vbox_gem_object, base)
223
224 static inline u64 vbox_bo_gpu_offset(struct vbox_bo *bo)
225 {
226         return bo->bo.offset;
227 }
228
229 int vbox_dumb_create(struct drm_file *file,
230                      struct drm_device *dev,
231                      struct drm_mode_create_dumb *args);
232
233 void vbox_gem_free_object(struct drm_gem_object *obj);
234 int vbox_dumb_mmap_offset(struct drm_file *file,
235                           struct drm_device *dev,
236                           u32 handle, u64 *offset);
237
238 #define DRM_FILE_PAGE_OFFSET (0x10000000ULL >> PAGE_SHIFT)
239
240 int vbox_mm_init(struct vbox_private *vbox);
241 void vbox_mm_fini(struct vbox_private *vbox);
242
243 int vbox_bo_create(struct vbox_private *vbox, int size, int align,
244                    u32 flags, struct vbox_bo **pvboxbo);
245
246 int vbox_gem_create(struct vbox_private *vbox,
247                     u32 size, bool iskernel, struct drm_gem_object **obj);
248
249 int vbox_bo_pin(struct vbox_bo *bo, u32 pl_flag);
250 int vbox_bo_unpin(struct vbox_bo *bo);
251
252 static inline int vbox_bo_reserve(struct vbox_bo *bo, bool no_wait)
253 {
254         int ret;
255
256         ret = ttm_bo_reserve(&bo->bo, true, no_wait, NULL);
257         if (ret) {
258                 if (ret != -ERESTARTSYS && ret != -EBUSY)
259                         DRM_ERROR("reserve failed %p\n", bo);
260                 return ret;
261         }
262         return 0;
263 }
264
265 static inline void vbox_bo_unreserve(struct vbox_bo *bo)
266 {
267         ttm_bo_unreserve(&bo->bo);
268 }
269
270 void vbox_ttm_placement(struct vbox_bo *bo, int domain);
271 int vbox_bo_push_sysram(struct vbox_bo *bo);
272 int vbox_mmap(struct file *filp, struct vm_area_struct *vma);
273 void *vbox_bo_kmap(struct vbox_bo *bo);
274 void vbox_bo_kunmap(struct vbox_bo *bo);
275
276 /* vbox_prime.c */
277 int vbox_gem_prime_pin(struct drm_gem_object *obj);
278 void vbox_gem_prime_unpin(struct drm_gem_object *obj);
279 struct sg_table *vbox_gem_prime_get_sg_table(struct drm_gem_object *obj);
280 struct drm_gem_object *vbox_gem_prime_import_sg_table(
281         struct drm_device *dev, struct dma_buf_attachment *attach,
282         struct sg_table *table);
283 void *vbox_gem_prime_vmap(struct drm_gem_object *obj);
284 void vbox_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
285 int vbox_gem_prime_mmap(struct drm_gem_object *obj,
286                         struct vm_area_struct *area);
287
288 /* vbox_irq.c */
289 int vbox_irq_init(struct vbox_private *vbox);
290 void vbox_irq_fini(struct vbox_private *vbox);
291 void vbox_report_hotplug(struct vbox_private *vbox);
292 irqreturn_t vbox_irq_handler(int irq, void *arg);
293
294 /* vbox_hgsmi.c */
295 void *hgsmi_buffer_alloc(struct gen_pool *guest_pool, size_t size,
296                          u8 channel, u16 channel_info);
297 void hgsmi_buffer_free(struct gen_pool *guest_pool, void *buf);
298 int hgsmi_buffer_submit(struct gen_pool *guest_pool, void *buf);
299
300 static inline void vbox_write_ioport(u16 index, u16 data)
301 {
302         outw(index, VBE_DISPI_IOPORT_INDEX);
303         outw(data, VBE_DISPI_IOPORT_DATA);
304 }
305
306 #endif