2 * Copyright (C) 2013-2017 Oracle Corporation
3 * This file is based on ast_main.c
4 * Copyright 2012 Red Hat Inc.
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:
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.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
30 #include <drm/drm_fb_helper.h>
31 #include <drm/drm_crtc_helper.h>
35 #include "vboxvideo_guest.h"
36 #include "vboxvideo_vbe.h"
38 static void vbox_user_framebuffer_destroy(struct drm_framebuffer *fb)
40 struct vbox_framebuffer *vbox_fb = to_vbox_framebuffer(fb);
43 drm_gem_object_put_unlocked(vbox_fb->obj);
45 drm_framebuffer_cleanup(fb);
49 void vbox_enable_accel(struct vbox_private *vbox)
52 struct vbva_buffer *vbva;
54 if (!vbox->vbva_info || !vbox->vbva_buffers) {
55 /* Should never happen... */
56 DRM_ERROR("vboxvideo: failed to set up VBVA.\n");
60 for (i = 0; i < vbox->num_crtcs; ++i) {
61 if (vbox->vbva_info[i].vbva)
64 vbva = (void __force *)vbox->vbva_buffers +
65 i * VBVA_MIN_BUFFER_SIZE;
66 if (!vbva_enable(&vbox->vbva_info[i],
67 vbox->guest_pool, vbva, i)) {
68 /* very old host or driver error. */
69 DRM_ERROR("vboxvideo: vbva_enable failed\n");
75 void vbox_disable_accel(struct vbox_private *vbox)
79 for (i = 0; i < vbox->num_crtcs; ++i)
80 vbva_disable(&vbox->vbva_info[i], vbox->guest_pool, i);
83 void vbox_report_caps(struct vbox_private *vbox)
85 u32 caps = VBVACAPS_DISABLE_CURSOR_INTEGRATION |
86 VBVACAPS_IRQ | VBVACAPS_USE_VBVA_ONLY;
88 if (vbox->initial_mode_queried)
89 caps |= VBVACAPS_VIDEO_MODE_HINTS;
91 hgsmi_send_caps_info(vbox->guest_pool, caps);
95 * Send information about dirty rectangles to VBVA. If necessary we enable
96 * VBVA first, as this is normally disabled after a change of master in case
97 * the new master does not send dirty rectangle information (is this even
100 void vbox_framebuffer_dirty_rectangles(struct drm_framebuffer *fb,
101 struct drm_clip_rect *rects,
102 unsigned int num_rects)
104 struct vbox_private *vbox = fb->dev->dev_private;
105 struct drm_crtc *crtc;
108 mutex_lock(&vbox->hw_mutex);
109 list_for_each_entry(crtc, &fb->dev->mode_config.crtc_list, head) {
110 if (CRTC_FB(crtc) != fb)
113 vbox_enable_accel(vbox);
115 for (i = 0; i < num_rects; ++i) {
116 struct vbva_cmd_hdr cmd_hdr;
117 unsigned int crtc_id = to_vbox_crtc(crtc)->crtc_id;
119 if ((rects[i].x1 > crtc->x + crtc->hwmode.hdisplay) ||
120 (rects[i].y1 > crtc->y + crtc->hwmode.vdisplay) ||
121 (rects[i].x2 < crtc->x) ||
122 (rects[i].y2 < crtc->y))
125 cmd_hdr.x = (s16)rects[i].x1;
126 cmd_hdr.y = (s16)rects[i].y1;
127 cmd_hdr.w = (u16)rects[i].x2 - rects[i].x1;
128 cmd_hdr.h = (u16)rects[i].y2 - rects[i].y1;
130 if (!vbva_buffer_begin_update(&vbox->vbva_info[crtc_id],
134 vbva_write(&vbox->vbva_info[crtc_id], vbox->guest_pool,
135 &cmd_hdr, sizeof(cmd_hdr));
136 vbva_buffer_end_update(&vbox->vbva_info[crtc_id]);
139 mutex_unlock(&vbox->hw_mutex);
142 static int vbox_user_framebuffer_dirty(struct drm_framebuffer *fb,
143 struct drm_file *file_priv,
144 unsigned int flags, unsigned int color,
145 struct drm_clip_rect *rects,
146 unsigned int num_rects)
148 vbox_framebuffer_dirty_rectangles(fb, rects, num_rects);
153 static const struct drm_framebuffer_funcs vbox_fb_funcs = {
154 .destroy = vbox_user_framebuffer_destroy,
155 .dirty = vbox_user_framebuffer_dirty,
158 int vbox_framebuffer_init(struct drm_device *dev,
159 struct vbox_framebuffer *vbox_fb,
160 const struct DRM_MODE_FB_CMD *mode_cmd,
161 struct drm_gem_object *obj)
165 drm_helper_mode_fill_fb_struct(dev, &vbox_fb->base, mode_cmd);
167 ret = drm_framebuffer_init(dev, &vbox_fb->base, &vbox_fb_funcs);
169 DRM_ERROR("framebuffer init failed %d\n", ret);
176 static struct drm_framebuffer *vbox_user_framebuffer_create(
177 struct drm_device *dev,
178 struct drm_file *filp,
179 const struct drm_mode_fb_cmd2 *mode_cmd)
181 struct drm_gem_object *obj;
182 struct vbox_framebuffer *vbox_fb;
185 obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
187 return ERR_PTR(-ENOENT);
189 vbox_fb = kzalloc(sizeof(*vbox_fb), GFP_KERNEL);
193 ret = vbox_framebuffer_init(dev, vbox_fb, mode_cmd, obj);
195 goto err_free_vbox_fb;
197 return &vbox_fb->base;
202 drm_gem_object_put_unlocked(obj);
206 static const struct drm_mode_config_funcs vbox_mode_funcs = {
207 .fb_create = vbox_user_framebuffer_create,
210 static int vbox_accel_init(struct vbox_private *vbox)
214 vbox->vbva_info = devm_kcalloc(vbox->dev->dev, vbox->num_crtcs,
215 sizeof(*vbox->vbva_info), GFP_KERNEL);
216 if (!vbox->vbva_info)
219 /* Take a command buffer for each screen from the end of usable VRAM. */
220 vbox->available_vram_size -= vbox->num_crtcs * VBVA_MIN_BUFFER_SIZE;
222 vbox->vbva_buffers = pci_iomap_range(vbox->dev->pdev, 0,
223 vbox->available_vram_size,
225 VBVA_MIN_BUFFER_SIZE);
226 if (!vbox->vbva_buffers)
229 for (i = 0; i < vbox->num_crtcs; ++i)
230 vbva_setup_buffer_context(&vbox->vbva_info[i],
231 vbox->available_vram_size +
232 i * VBVA_MIN_BUFFER_SIZE,
233 VBVA_MIN_BUFFER_SIZE);
238 static void vbox_accel_fini(struct vbox_private *vbox)
240 vbox_disable_accel(vbox);
241 pci_iounmap(vbox->dev->pdev, vbox->vbva_buffers);
244 /** Do we support the 4.3 plus mode hint reporting interface? */
245 static bool have_hgsmi_mode_hints(struct vbox_private *vbox)
247 u32 have_hints, have_cursor;
250 ret = hgsmi_query_conf(vbox->guest_pool,
251 VBOX_VBVA_CONF32_MODE_HINT_REPORTING,
256 ret = hgsmi_query_conf(vbox->guest_pool,
257 VBOX_VBVA_CONF32_GUEST_CURSOR_REPORTING,
262 return have_hints == VINF_SUCCESS && have_cursor == VINF_SUCCESS;
265 static bool vbox_check_supported(u16 id)
269 vbox_write_ioport(VBE_DISPI_INDEX_ID, id);
270 dispi_id = inw(VBE_DISPI_IOPORT_DATA);
272 return dispi_id == id;
276 * Set up our heaps and data exchange buffers in VRAM before handing the rest
277 * to the memory manager.
279 static int vbox_hw_init(struct vbox_private *vbox)
283 vbox->full_vram_size = inl(VBE_DISPI_IOPORT_DATA);
284 vbox->any_pitch = vbox_check_supported(VBE_DISPI_ID_ANYX);
286 DRM_INFO("VRAM %08x\n", vbox->full_vram_size);
288 /* Map guest-heap at end of vram */
290 pci_iomap_range(vbox->dev->pdev, 0, GUEST_HEAP_OFFSET(vbox),
292 if (!vbox->guest_heap)
295 /* Create guest-heap mem-pool use 2^4 = 16 byte chunks */
296 vbox->guest_pool = gen_pool_create(4, -1);
297 if (!vbox->guest_pool)
298 goto err_unmap_guest_heap;
300 ret = gen_pool_add_virt(vbox->guest_pool,
301 (unsigned long)vbox->guest_heap,
302 GUEST_HEAP_OFFSET(vbox),
303 GUEST_HEAP_USABLE_SIZE, -1);
305 goto err_destroy_guest_pool;
307 ret = hgsmi_test_query_conf(vbox->guest_pool);
309 DRM_ERROR("vboxvideo: hgsmi_test_query_conf failed\n");
310 goto err_destroy_guest_pool;
313 /* Reduce available VRAM size to reflect the guest heap. */
314 vbox->available_vram_size = GUEST_HEAP_OFFSET(vbox);
315 /* Linux drm represents monitors as a 32-bit array. */
316 hgsmi_query_conf(vbox->guest_pool, VBOX_VBVA_CONF32_MONITOR_COUNT,
318 vbox->num_crtcs = clamp_t(u32, vbox->num_crtcs, 1, VBOX_MAX_SCREENS);
320 if (!have_hgsmi_mode_hints(vbox)) {
322 goto err_destroy_guest_pool;
325 vbox->last_mode_hints = devm_kcalloc(vbox->dev->dev, vbox->num_crtcs,
326 sizeof(struct vbva_modehint),
328 if (!vbox->last_mode_hints) {
330 goto err_destroy_guest_pool;
333 ret = vbox_accel_init(vbox);
335 goto err_destroy_guest_pool;
339 err_destroy_guest_pool:
340 gen_pool_destroy(vbox->guest_pool);
341 err_unmap_guest_heap:
342 pci_iounmap(vbox->dev->pdev, vbox->guest_heap);
346 static void vbox_hw_fini(struct vbox_private *vbox)
348 vbox_accel_fini(vbox);
349 gen_pool_destroy(vbox->guest_pool);
350 pci_iounmap(vbox->dev->pdev, vbox->guest_heap);
353 int vbox_driver_load(struct drm_device *dev, unsigned long flags)
355 struct vbox_private *vbox;
358 if (!vbox_check_supported(VBE_DISPI_ID_HGSMI))
361 vbox = devm_kzalloc(dev->dev, sizeof(*vbox), GFP_KERNEL);
365 dev->dev_private = vbox;
368 mutex_init(&vbox->hw_mutex);
370 ret = vbox_hw_init(vbox);
374 ret = vbox_mm_init(vbox);
378 drm_mode_config_init(dev);
380 dev->mode_config.funcs = (void *)&vbox_mode_funcs;
381 dev->mode_config.min_width = 64;
382 dev->mode_config.min_height = 64;
383 dev->mode_config.preferred_depth = 24;
384 dev->mode_config.max_width = VBE_DISPI_MAX_XRES;
385 dev->mode_config.max_height = VBE_DISPI_MAX_YRES;
387 ret = vbox_mode_init(dev);
389 goto err_drm_mode_cleanup;
391 ret = vbox_irq_init(vbox);
395 ret = vbox_fbdev_init(dev);
405 err_drm_mode_cleanup:
406 drm_mode_config_cleanup(dev);
413 void vbox_driver_unload(struct drm_device *dev)
415 struct vbox_private *vbox = dev->dev_private;
417 vbox_fbdev_fini(dev);
420 drm_mode_config_cleanup(dev);
426 * @note this is described in the DRM framework documentation. AST does not
427 * have it, but we get an oops on driver unload if it is not present.
429 void vbox_driver_lastclose(struct drm_device *dev)
431 struct vbox_private *vbox = dev->dev_private;
434 drm_fb_helper_restore_fbdev_mode_unlocked(&vbox->fbdev->helper);
437 int vbox_gem_create(struct drm_device *dev,
438 u32 size, bool iskernel, struct drm_gem_object **obj)
440 struct vbox_bo *vboxbo;
445 size = roundup(size, PAGE_SIZE);
449 ret = vbox_bo_create(dev, size, 0, 0, &vboxbo);
451 if (ret != -ERESTARTSYS)
452 DRM_ERROR("failed to allocate GEM object\n");
461 int vbox_dumb_create(struct drm_file *file,
462 struct drm_device *dev, struct drm_mode_create_dumb *args)
465 struct drm_gem_object *gobj;
468 args->pitch = args->width * ((args->bpp + 7) / 8);
469 args->size = args->pitch * args->height;
471 ret = vbox_gem_create(dev, args->size, false, &gobj);
475 ret = drm_gem_handle_create(file, gobj, &handle);
476 drm_gem_object_put_unlocked(gobj);
480 args->handle = handle;
485 static void vbox_bo_unref(struct vbox_bo **bo)
487 struct ttm_buffer_object *tbo;
498 void vbox_gem_free_object(struct drm_gem_object *obj)
500 struct vbox_bo *vbox_bo = gem_to_vbox_bo(obj);
502 vbox_bo_unref(&vbox_bo);
505 static inline u64 vbox_bo_mmap_offset(struct vbox_bo *bo)
507 return drm_vma_node_offset_addr(&bo->bo.vma_node);
511 vbox_dumb_mmap_offset(struct drm_file *file,
512 struct drm_device *dev,
513 u32 handle, u64 *offset)
515 struct drm_gem_object *obj;
519 mutex_lock(&dev->struct_mutex);
520 obj = drm_gem_object_lookup(file, handle);
526 bo = gem_to_vbox_bo(obj);
527 *offset = vbox_bo_mmap_offset(bo);
529 drm_gem_object_put(obj);
533 mutex_unlock(&dev->struct_mutex);