2 * Copyright © 2013 Red Hat
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 #include <linux/module.h>
31 #include "drm/drm_crtc.h"
32 #include "drm/drm_crtc_helper.h"
35 #include "qxl_object.h"
36 #include "drm_fb_helper.h"
38 #define QXL_DIRTY_DELAY (HZ / 30)
41 struct drm_fb_helper helper;
42 struct qxl_framebuffer qfb;
43 struct qxl_device *qdev;
45 spinlock_t delayed_ops_lock;
46 struct list_head delayed_ops;
51 static void qxl_fb_image_init(struct qxl_fb_image *qxl_fb_image,
52 struct qxl_device *qdev, struct fb_info *info,
53 const struct fb_image *image)
55 qxl_fb_image->qdev = qdev;
57 qxl_fb_image->visual = info->fix.visual;
58 if (qxl_fb_image->visual == FB_VISUAL_TRUECOLOR ||
59 qxl_fb_image->visual == FB_VISUAL_DIRECTCOLOR)
60 memcpy(&qxl_fb_image->pseudo_palette,
62 sizeof(qxl_fb_image->pseudo_palette));
65 if (image->depth == 1)
66 qxl_fb_image->visual = FB_VISUAL_MONO10;
68 qxl_fb_image->visual = FB_VISUAL_DIRECTCOLOR;
71 memcpy(&qxl_fb_image->fb_image, image,
72 sizeof(qxl_fb_image->fb_image));
76 static struct fb_deferred_io qxl_defio = {
77 .delay = QXL_DIRTY_DELAY,
78 .deferred_io = drm_fb_helper_deferred_io,
81 static struct fb_ops qxlfb_ops = {
83 .fb_check_var = drm_fb_helper_check_var,
84 .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
85 .fb_fillrect = drm_fb_helper_sys_fillrect,
86 .fb_copyarea = drm_fb_helper_sys_copyarea,
87 .fb_imageblit = drm_fb_helper_sys_imageblit,
88 .fb_pan_display = drm_fb_helper_pan_display,
89 .fb_blank = drm_fb_helper_blank,
90 .fb_setcmap = drm_fb_helper_setcmap,
91 .fb_debug_enter = drm_fb_helper_debug_enter,
92 .fb_debug_leave = drm_fb_helper_debug_leave,
95 static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
97 struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
100 ret = qxl_bo_reserve(qbo, false);
101 if (likely(ret == 0)) {
104 qxl_bo_unreserve(qbo);
106 drm_gem_object_unreference_unlocked(gobj);
109 int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
110 struct drm_file *file_priv,
114 struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
117 /* drm_get_handle_create adds a reference - good */
118 r = drm_gem_handle_create(file_priv, gobj, handle);
124 static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
125 const struct drm_mode_fb_cmd2 *mode_cmd,
126 struct drm_gem_object **gobj_p)
128 struct qxl_device *qdev = qfbdev->qdev;
129 struct drm_gem_object *gobj = NULL;
130 struct qxl_bo *qbo = NULL;
132 int aligned_size, size;
133 int height = mode_cmd->height;
135 size = mode_cmd->pitches[0] * height;
136 aligned_size = ALIGN(size, PAGE_SIZE);
137 /* TODO: unallocate and reallocate surface0 for real. Hack to just
138 * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
139 ret = qxl_gem_object_create(qdev, aligned_size, 0,
140 QXL_GEM_DOMAIN_SURFACE,
141 false, /* is discardable */
142 false, /* is kernel (false means device) */
146 pr_err("failed to allocate framebuffer (%d)\n",
150 qbo = gem_to_qxl_bo(gobj);
152 qbo->surf.width = mode_cmd->width;
153 qbo->surf.height = mode_cmd->height;
154 qbo->surf.stride = mode_cmd->pitches[0];
155 qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
156 ret = qxl_bo_reserve(qbo, false);
157 if (unlikely(ret != 0))
159 ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
161 qxl_bo_unreserve(qbo);
164 ret = qxl_bo_kmap(qbo, NULL);
165 qxl_bo_unreserve(qbo); /* unreserve, will be mmaped */
172 qxlfb_destroy_pinned_object(gobj);
179 * It should not be necessary to have a special dirty() callback for fbdev.
181 static int qxlfb_framebuffer_dirty(struct drm_framebuffer *fb,
182 struct drm_file *file_priv,
183 unsigned flags, unsigned color,
184 struct drm_clip_rect *clips,
187 struct qxl_device *qdev = fb->dev->dev_private;
188 struct fb_info *info = qdev->fbdev_info;
189 struct qxl_fbdev *qfbdev = info->par;
190 struct qxl_fb_image qxl_fb_image;
191 struct fb_image *image = &qxl_fb_image.fb_image;
193 /* TODO: hard coding 32 bpp */
194 int stride = qfbdev->qfb.base.pitches[0];
197 * we are using a shadow draw buffer, at qdev->surface0_shadow
199 qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]", clips->x1, clips->x2,
200 clips->y1, clips->y2);
201 image->dx = clips->x1;
202 image->dy = clips->y1;
203 image->width = clips->x2 - clips->x1;
204 image->height = clips->y2 - clips->y1;
205 image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
208 image->depth = 32; /* TODO: take from somewhere? */
209 image->cmap.start = 0;
211 image->cmap.red = NULL;
212 image->cmap.green = NULL;
213 image->cmap.blue = NULL;
214 image->cmap.transp = NULL;
215 image->data = qfbdev->shadow + (clips->x1 * 4) + (stride * clips->y1);
217 qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
218 qxl_draw_opaque_fb(&qxl_fb_image, stride);
223 static const struct drm_framebuffer_funcs qxlfb_fb_funcs = {
224 .destroy = qxl_user_framebuffer_destroy,
225 .dirty = qxlfb_framebuffer_dirty,
228 static int qxlfb_create(struct qxl_fbdev *qfbdev,
229 struct drm_fb_helper_surface_size *sizes)
231 struct qxl_device *qdev = qfbdev->qdev;
232 struct fb_info *info;
233 struct drm_framebuffer *fb = NULL;
234 struct drm_mode_fb_cmd2 mode_cmd;
235 struct drm_gem_object *gobj = NULL;
236 struct qxl_bo *qbo = NULL;
239 int bpp = sizes->surface_bpp;
240 int depth = sizes->surface_depth;
243 mode_cmd.width = sizes->surface_width;
244 mode_cmd.height = sizes->surface_height;
246 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
247 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
249 ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
253 qbo = gem_to_qxl_bo(gobj);
254 QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
255 mode_cmd.height, mode_cmd.pitches[0]);
257 shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
258 /* TODO: what's the usual response to memory allocation errors? */
261 "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
262 qxl_bo_gpu_offset(qbo),
263 qxl_bo_mmap_offset(qbo),
266 size = mode_cmd.pitches[0] * mode_cmd.height;
268 info = drm_fb_helper_alloc_fbi(&qfbdev->helper);
276 qxl_framebuffer_init(qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj,
279 fb = &qfbdev->qfb.base;
281 /* setup helper with fb data */
282 qfbdev->helper.fb = fb;
284 qfbdev->shadow = shadow;
285 strcpy(info->fix.id, "qxldrmfb");
287 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
289 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
290 info->fbops = &qxlfb_ops;
293 * TODO: using gobj->size in various places in this function. Not sure
294 * what the difference between the different sizes is.
296 info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
297 info->fix.smem_len = gobj->size;
298 info->screen_base = qfbdev->shadow;
299 info->screen_size = gobj->size;
301 drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
304 /* setup aperture base/size for vesafb takeover */
305 info->apertures->ranges[0].base = qdev->ddev->mode_config.fb_base;
306 info->apertures->ranges[0].size = qdev->vram_size;
308 info->fix.mmio_start = 0;
309 info->fix.mmio_len = 0;
311 if (info->screen_base == NULL) {
313 goto out_destroy_fbi;
316 info->fbdefio = &qxl_defio;
317 fb_deferred_io_init(info);
319 qdev->fbdev_info = info;
320 qdev->fbdev_qfb = &qfbdev->qfb;
321 DRM_INFO("fb mappable at 0x%lX, size %lu\n", info->fix.smem_start, (unsigned long)info->screen_size);
322 DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n", fb->depth, fb->pitches[0], fb->width, fb->height);
326 drm_fb_helper_release_fbi(&qfbdev->helper);
329 ret = qxl_bo_reserve(qbo, false);
330 if (likely(ret == 0)) {
333 qxl_bo_unreserve(qbo);
337 drm_gem_object_unreference_unlocked(gobj);
338 drm_framebuffer_cleanup(fb);
341 drm_gem_object_unreference_unlocked(gobj);
345 static int qxl_fb_find_or_create_single(
346 struct drm_fb_helper *helper,
347 struct drm_fb_helper_surface_size *sizes)
349 struct qxl_fbdev *qfbdev =
350 container_of(helper, struct qxl_fbdev, helper);
355 ret = qxlfb_create(qfbdev, sizes);
363 static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
365 struct qxl_framebuffer *qfb = &qfbdev->qfb;
367 drm_fb_helper_unregister_fbi(&qfbdev->helper);
368 drm_fb_helper_release_fbi(&qfbdev->helper);
371 qxlfb_destroy_pinned_object(qfb->obj);
374 drm_fb_helper_fini(&qfbdev->helper);
375 vfree(qfbdev->shadow);
376 drm_framebuffer_cleanup(&qfb->base);
381 static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
382 .fb_probe = qxl_fb_find_or_create_single,
385 int qxl_fbdev_init(struct qxl_device *qdev)
387 struct qxl_fbdev *qfbdev;
388 int bpp_sel = 32; /* TODO: parameter from somewhere? */
391 qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
396 qdev->mode_info.qfbdev = qfbdev;
397 spin_lock_init(&qfbdev->delayed_ops_lock);
398 INIT_LIST_HEAD(&qfbdev->delayed_ops);
400 drm_fb_helper_prepare(qdev->ddev, &qfbdev->helper,
401 &qxl_fb_helper_funcs);
403 ret = drm_fb_helper_init(qdev->ddev, &qfbdev->helper,
404 qxl_num_crtc /* num_crtc - QXL supports just 1 */,
409 ret = drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
413 ret = drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
420 drm_fb_helper_fini(&qfbdev->helper);
426 void qxl_fbdev_fini(struct qxl_device *qdev)
428 if (!qdev->mode_info.qfbdev)
431 qxl_fbdev_destroy(qdev->ddev, qdev->mode_info.qfbdev);
432 kfree(qdev->mode_info.qfbdev);
433 qdev->mode_info.qfbdev = NULL;
436 void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
438 drm_fb_helper_set_suspend(&qdev->mode_info.qfbdev->helper, state);
441 bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
443 if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))