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)
40 #define QXL_FB_OP_FILLRECT 1
41 #define QXL_FB_OP_COPYAREA 2
42 #define QXL_FB_OP_IMAGEBLIT 3
45 struct list_head head;
48 struct fb_fillrect fr;
49 struct fb_copyarea ca;
56 struct drm_fb_helper helper;
57 struct qxl_framebuffer qfb;
58 struct list_head fbdev_list;
59 struct qxl_device *qdev;
61 spinlock_t delayed_ops_lock;
62 struct list_head delayed_ops;
66 /* dirty memory logging */
77 static void qxl_fb_image_init(struct qxl_fb_image *qxl_fb_image,
78 struct qxl_device *qdev, struct fb_info *info,
79 const struct fb_image *image)
81 qxl_fb_image->qdev = qdev;
83 qxl_fb_image->visual = info->fix.visual;
84 if (qxl_fb_image->visual == FB_VISUAL_TRUECOLOR ||
85 qxl_fb_image->visual == FB_VISUAL_DIRECTCOLOR)
86 memcpy(&qxl_fb_image->pseudo_palette,
88 sizeof(qxl_fb_image->pseudo_palette));
91 if (image->depth == 1)
92 qxl_fb_image->visual = FB_VISUAL_MONO10;
94 qxl_fb_image->visual = FB_VISUAL_DIRECTCOLOR;
97 memcpy(&qxl_fb_image->fb_image, image,
98 sizeof(qxl_fb_image->fb_image));
102 static void qxl_fb_dirty_flush(struct fb_info *info)
104 struct qxl_fbdev *qfbdev = info->par;
105 struct qxl_device *qdev = qfbdev->qdev;
106 struct qxl_fb_image qxl_fb_image;
107 struct fb_image *image = &qxl_fb_image.fb_image;
110 /* TODO: hard coding 32 bpp */
111 int stride = qfbdev->qfb.base.pitches[0];
113 x1 = qfbdev->dirty.x1;
114 x2 = qfbdev->dirty.x2;
115 y1 = qfbdev->dirty.y1;
116 y2 = qfbdev->dirty.y2;
118 * we are using a shadow draw buffer, at qdev->surface0_shadow
120 qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]", x1, x2, y1, y2);
123 image->width = x2 - x1;
124 image->height = y2 - y1;
125 image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
128 image->depth = 32; /* TODO: take from somewhere? */
129 image->cmap.start = 0;
131 image->cmap.red = NULL;
132 image->cmap.green = NULL;
133 image->cmap.blue = NULL;
134 image->cmap.transp = NULL;
135 image->data = qfbdev->shadow + (x1 * 4) + (stride * y1);
137 qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
138 qxl_draw_opaque_fb(&qxl_fb_image, stride);
139 qfbdev->dirty.x1 = 0;
140 qfbdev->dirty.x2 = 0;
141 qfbdev->dirty.y1 = 0;
142 qfbdev->dirty.y2 = 0;
145 static void qxl_deferred_io(struct fb_info *info,
146 struct list_head *pagelist)
148 struct qxl_fbdev *qfbdev = info->par;
149 unsigned long start, end, min, max;
155 list_for_each_entry(page, pagelist, lru) {
156 start = page->index << PAGE_SHIFT;
157 end = start + PAGE_SIZE - 1;
158 min = min(min, start);
163 y1 = min / info->fix.line_length;
164 y2 = (max / info->fix.line_length) + 1;
166 /* TODO: add spin lock? */
167 /* spin_lock_irqsave(&qfbdev->dirty.lock, flags); */
168 qfbdev->dirty.x1 = 0;
169 qfbdev->dirty.y1 = y1;
170 qfbdev->dirty.x2 = info->var.xres;
171 qfbdev->dirty.y2 = y2;
172 /* spin_unlock_irqrestore(&qfbdev->dirty.lock, flags); */
175 qxl_fb_dirty_flush(info);
179 static struct fb_deferred_io qxl_defio = {
180 .delay = QXL_DIRTY_DELAY,
181 .deferred_io = qxl_deferred_io,
184 static void qxl_fb_delayed_fillrect(struct qxl_fbdev *qfbdev,
185 const struct fb_fillrect *fb_rect)
187 struct qxl_fb_op *op;
190 op = kmalloc(sizeof(struct qxl_fb_op), GFP_ATOMIC | __GFP_NOWARN);
194 op->op.fr = *fb_rect;
196 op->op_type = QXL_FB_OP_FILLRECT;
198 spin_lock_irqsave(&qfbdev->delayed_ops_lock, flags);
199 list_add_tail(&op->head, &qfbdev->delayed_ops);
200 spin_unlock_irqrestore(&qfbdev->delayed_ops_lock, flags);
203 static void qxl_fb_delayed_copyarea(struct qxl_fbdev *qfbdev,
204 const struct fb_copyarea *fb_copy)
206 struct qxl_fb_op *op;
209 op = kmalloc(sizeof(struct qxl_fb_op), GFP_ATOMIC | __GFP_NOWARN);
213 op->op.ca = *fb_copy;
215 op->op_type = QXL_FB_OP_COPYAREA;
217 spin_lock_irqsave(&qfbdev->delayed_ops_lock, flags);
218 list_add_tail(&op->head, &qfbdev->delayed_ops);
219 spin_unlock_irqrestore(&qfbdev->delayed_ops_lock, flags);
222 static void qxl_fb_delayed_imageblit(struct qxl_fbdev *qfbdev,
223 const struct fb_image *fb_image)
225 struct qxl_fb_op *op;
227 uint32_t size = fb_image->width * fb_image->height * (fb_image->depth >= 8 ? fb_image->depth / 8 : 1);
229 op = kmalloc(sizeof(struct qxl_fb_op) + size, GFP_ATOMIC | __GFP_NOWARN);
233 op->op.ib = *fb_image;
234 op->img_data = (void *)(op + 1);
235 op->op_type = QXL_FB_OP_IMAGEBLIT;
237 memcpy(op->img_data, fb_image->data, size);
239 op->op.ib.data = op->img_data;
240 spin_lock_irqsave(&qfbdev->delayed_ops_lock, flags);
241 list_add_tail(&op->head, &qfbdev->delayed_ops);
242 spin_unlock_irqrestore(&qfbdev->delayed_ops_lock, flags);
245 static void qxl_fb_fillrect_internal(struct fb_info *info,
246 const struct fb_fillrect *fb_rect)
248 struct qxl_fbdev *qfbdev = info->par;
249 struct qxl_device *qdev = qfbdev->qdev;
250 struct qxl_rect rect;
254 int width = fb_rect->width;
255 int height = fb_rect->height;
257 struct qxl_draw_fill qxl_draw_fill_rec;
259 if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
260 info->fix.visual == FB_VISUAL_DIRECTCOLOR)
261 color = ((u32 *) (info->pseudo_palette))[fb_rect->color];
263 color = fb_rect->color;
265 rect.right = x + width;
267 rect.bottom = y + height;
268 switch (fb_rect->rop) {
270 rop = SPICE_ROPD_OP_XOR;
273 rop = SPICE_ROPD_OP_PUT;
276 pr_err("qxl_fb_fillrect(): unknown rop, "
277 "defaulting to SPICE_ROPD_OP_PUT\n");
278 rop = SPICE_ROPD_OP_PUT;
280 qxl_draw_fill_rec.qdev = qdev;
281 qxl_draw_fill_rec.rect = rect;
282 qxl_draw_fill_rec.color = color;
283 qxl_draw_fill_rec.rop = rop;
285 qxl_draw_fill(&qxl_draw_fill_rec);
288 static void qxl_fb_fillrect(struct fb_info *info,
289 const struct fb_fillrect *fb_rect)
291 struct qxl_fbdev *qfbdev = info->par;
292 struct qxl_device *qdev = qfbdev->qdev;
294 if (!drm_can_sleep()) {
295 qxl_fb_delayed_fillrect(qfbdev, fb_rect);
296 schedule_work(&qdev->fb_work);
299 /* make sure any previous work is done */
300 flush_work(&qdev->fb_work);
301 qxl_fb_fillrect_internal(info, fb_rect);
304 static void qxl_fb_copyarea_internal(struct fb_info *info,
305 const struct fb_copyarea *region)
307 struct qxl_fbdev *qfbdev = info->par;
309 qxl_draw_copyarea(qfbdev->qdev,
310 region->width, region->height,
311 region->sx, region->sy,
312 region->dx, region->dy);
315 static void qxl_fb_copyarea(struct fb_info *info,
316 const struct fb_copyarea *region)
318 struct qxl_fbdev *qfbdev = info->par;
319 struct qxl_device *qdev = qfbdev->qdev;
321 if (!drm_can_sleep()) {
322 qxl_fb_delayed_copyarea(qfbdev, region);
323 schedule_work(&qdev->fb_work);
326 /* make sure any previous work is done */
327 flush_work(&qdev->fb_work);
328 qxl_fb_copyarea_internal(info, region);
331 static void qxl_fb_imageblit_safe(struct qxl_fb_image *qxl_fb_image)
333 qxl_draw_opaque_fb(qxl_fb_image, 0);
336 static void qxl_fb_imageblit_internal(struct fb_info *info,
337 const struct fb_image *image)
339 struct qxl_fbdev *qfbdev = info->par;
340 struct qxl_fb_image qxl_fb_image;
342 /* ensure proper order rendering operations - TODO: must do this
344 qxl_fb_image_init(&qxl_fb_image, qfbdev->qdev, info, image);
345 qxl_fb_imageblit_safe(&qxl_fb_image);
348 static void qxl_fb_imageblit(struct fb_info *info,
349 const struct fb_image *image)
351 struct qxl_fbdev *qfbdev = info->par;
352 struct qxl_device *qdev = qfbdev->qdev;
354 if (!drm_can_sleep()) {
355 qxl_fb_delayed_imageblit(qfbdev, image);
356 schedule_work(&qdev->fb_work);
359 /* make sure any previous work is done */
360 flush_work(&qdev->fb_work);
361 qxl_fb_imageblit_internal(info, image);
364 static void qxl_fb_work(struct work_struct *work)
366 struct qxl_device *qdev = container_of(work, struct qxl_device, fb_work);
368 struct qxl_fb_op *entry, *tmp;
369 struct qxl_fbdev *qfbdev = qdev->mode_info.qfbdev;
371 /* since the irq context just adds entries to the end of the
372 list dropping the lock should be fine, as entry isn't modified
373 in the operation code */
374 spin_lock_irqsave(&qfbdev->delayed_ops_lock, flags);
375 list_for_each_entry_safe(entry, tmp, &qfbdev->delayed_ops, head) {
376 spin_unlock_irqrestore(&qfbdev->delayed_ops_lock, flags);
377 switch (entry->op_type) {
378 case QXL_FB_OP_FILLRECT:
379 qxl_fb_fillrect_internal(qfbdev->helper.fbdev, &entry->op.fr);
381 case QXL_FB_OP_COPYAREA:
382 qxl_fb_copyarea_internal(qfbdev->helper.fbdev, &entry->op.ca);
384 case QXL_FB_OP_IMAGEBLIT:
385 qxl_fb_imageblit_internal(qfbdev->helper.fbdev, &entry->op.ib);
388 spin_lock_irqsave(&qfbdev->delayed_ops_lock, flags);
389 list_del(&entry->head);
392 spin_unlock_irqrestore(&qfbdev->delayed_ops_lock, flags);
395 int qxl_fb_init(struct qxl_device *qdev)
397 INIT_WORK(&qdev->fb_work, qxl_fb_work);
401 static struct fb_ops qxlfb_ops = {
402 .owner = THIS_MODULE,
403 .fb_check_var = drm_fb_helper_check_var,
404 .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
405 .fb_fillrect = qxl_fb_fillrect,
406 .fb_copyarea = qxl_fb_copyarea,
407 .fb_imageblit = qxl_fb_imageblit,
408 .fb_pan_display = drm_fb_helper_pan_display,
409 .fb_blank = drm_fb_helper_blank,
410 .fb_setcmap = drm_fb_helper_setcmap,
411 .fb_debug_enter = drm_fb_helper_debug_enter,
412 .fb_debug_leave = drm_fb_helper_debug_leave,
415 static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
417 struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
420 ret = qxl_bo_reserve(qbo, false);
421 if (likely(ret == 0)) {
424 qxl_bo_unreserve(qbo);
426 drm_gem_object_unreference_unlocked(gobj);
429 int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
430 struct drm_file *file_priv,
434 struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
437 /* drm_get_handle_create adds a reference - good */
438 r = drm_gem_handle_create(file_priv, gobj, handle);
444 static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
445 struct drm_mode_fb_cmd2 *mode_cmd,
446 struct drm_gem_object **gobj_p)
448 struct qxl_device *qdev = qfbdev->qdev;
449 struct drm_gem_object *gobj = NULL;
450 struct qxl_bo *qbo = NULL;
452 int aligned_size, size;
453 int height = mode_cmd->height;
457 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &bpp, &depth);
459 size = mode_cmd->pitches[0] * height;
460 aligned_size = ALIGN(size, PAGE_SIZE);
461 /* TODO: unallocate and reallocate surface0 for real. Hack to just
462 * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
463 ret = qxl_gem_object_create(qdev, aligned_size, 0,
464 QXL_GEM_DOMAIN_SURFACE,
465 false, /* is discardable */
466 false, /* is kernel (false means device) */
470 pr_err("failed to allocate framebuffer (%d)\n",
474 qbo = gem_to_qxl_bo(gobj);
476 qbo->surf.width = mode_cmd->width;
477 qbo->surf.height = mode_cmd->height;
478 qbo->surf.stride = mode_cmd->pitches[0];
479 qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
480 ret = qxl_bo_reserve(qbo, false);
481 if (unlikely(ret != 0))
483 ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
485 qxl_bo_unreserve(qbo);
488 ret = qxl_bo_kmap(qbo, NULL);
489 qxl_bo_unreserve(qbo); /* unreserve, will be mmaped */
496 qxlfb_destroy_pinned_object(gobj);
501 static int qxlfb_create(struct qxl_fbdev *qfbdev,
502 struct drm_fb_helper_surface_size *sizes)
504 struct qxl_device *qdev = qfbdev->qdev;
505 struct fb_info *info;
506 struct drm_framebuffer *fb = NULL;
507 struct drm_mode_fb_cmd2 mode_cmd;
508 struct drm_gem_object *gobj = NULL;
509 struct qxl_bo *qbo = NULL;
510 struct device *device = &qdev->pdev->dev;
513 int bpp = sizes->surface_bpp;
514 int depth = sizes->surface_depth;
517 mode_cmd.width = sizes->surface_width;
518 mode_cmd.height = sizes->surface_height;
520 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
521 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
523 ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
524 qbo = gem_to_qxl_bo(gobj);
525 QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
526 mode_cmd.height, mode_cmd.pitches[0]);
528 shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
529 /* TODO: what's the usual response to memory allocation errors? */
532 "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
533 qxl_bo_gpu_offset(qbo),
534 qxl_bo_mmap_offset(qbo),
537 size = mode_cmd.pitches[0] * mode_cmd.height;
539 info = framebuffer_alloc(0, device);
547 qxl_framebuffer_init(qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj);
549 fb = &qfbdev->qfb.base;
551 /* setup helper with fb data */
552 qfbdev->helper.fb = fb;
553 qfbdev->helper.fbdev = info;
554 qfbdev->shadow = shadow;
555 strcpy(info->fix.id, "qxldrmfb");
557 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
559 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
560 info->fbops = &qxlfb_ops;
563 * TODO: using gobj->size in various places in this function. Not sure
564 * what the difference between the different sizes is.
566 info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
567 info->fix.smem_len = gobj->size;
568 info->screen_base = qfbdev->shadow;
569 info->screen_size = gobj->size;
571 drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
574 /* setup aperture base/size for vesafb takeover */
575 info->apertures = alloc_apertures(1);
576 if (!info->apertures) {
580 info->apertures->ranges[0].base = qdev->ddev->mode_config.fb_base;
581 info->apertures->ranges[0].size = qdev->vram_size;
583 info->fix.mmio_start = 0;
584 info->fix.mmio_len = 0;
586 if (info->screen_base == NULL) {
591 ret = fb_alloc_cmap(&info->cmap, 256, 0);
597 info->fbdefio = &qxl_defio;
598 fb_deferred_io_init(info);
600 qdev->fbdev_info = info;
601 qdev->fbdev_qfb = &qfbdev->qfb;
602 DRM_INFO("fb mappable at 0x%lX, size %lu\n", info->fix.smem_start, (unsigned long)info->screen_size);
603 DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n", fb->depth, fb->pitches[0], fb->width, fb->height);
608 ret = qxl_bo_reserve(qbo, false);
609 if (likely(ret == 0)) {
612 qxl_bo_unreserve(qbo);
616 drm_gem_object_unreference(gobj);
617 drm_framebuffer_cleanup(fb);
620 drm_gem_object_unreference(gobj);
624 static int qxl_fb_find_or_create_single(
625 struct drm_fb_helper *helper,
626 struct drm_fb_helper_surface_size *sizes)
628 struct qxl_fbdev *qfbdev =
629 container_of(helper, struct qxl_fbdev, helper);
634 ret = qxlfb_create(qfbdev, sizes);
642 static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
644 struct fb_info *info;
645 struct qxl_framebuffer *qfb = &qfbdev->qfb;
647 if (qfbdev->helper.fbdev) {
648 info = qfbdev->helper.fbdev;
650 unregister_framebuffer(info);
651 framebuffer_release(info);
654 qxlfb_destroy_pinned_object(qfb->obj);
657 drm_fb_helper_fini(&qfbdev->helper);
658 vfree(qfbdev->shadow);
659 drm_framebuffer_cleanup(&qfb->base);
664 static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
665 .fb_probe = qxl_fb_find_or_create_single,
668 int qxl_fbdev_init(struct qxl_device *qdev)
670 struct qxl_fbdev *qfbdev;
671 int bpp_sel = 32; /* TODO: parameter from somewhere? */
674 qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
679 qdev->mode_info.qfbdev = qfbdev;
680 spin_lock_init(&qfbdev->delayed_ops_lock);
681 INIT_LIST_HEAD(&qfbdev->delayed_ops);
683 drm_fb_helper_prepare(qdev->ddev, &qfbdev->helper,
684 &qxl_fb_helper_funcs);
686 ret = drm_fb_helper_init(qdev->ddev, &qfbdev->helper,
687 qxl_num_crtc /* num_crtc - QXL supports just 1 */,
694 drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
695 drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
699 void qxl_fbdev_fini(struct qxl_device *qdev)
701 if (!qdev->mode_info.qfbdev)
704 qxl_fbdev_destroy(qdev->ddev, qdev->mode_info.qfbdev);
705 kfree(qdev->mode_info.qfbdev);
706 qdev->mode_info.qfbdev = NULL;
709 void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
711 fb_set_suspend(qdev->mode_info.qfbdev->helper.fbdev, state);
714 bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
716 if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))