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 list_head fbdev_list;
44 struct qxl_device *qdev;
49 /* dirty memory logging */
60 static void qxl_fb_image_init(struct qxl_fb_image *qxl_fb_image,
61 struct qxl_device *qdev, struct fb_info *info,
62 const struct fb_image *image)
64 qxl_fb_image->qdev = qdev;
66 qxl_fb_image->visual = info->fix.visual;
67 if (qxl_fb_image->visual == FB_VISUAL_TRUECOLOR ||
68 qxl_fb_image->visual == FB_VISUAL_DIRECTCOLOR)
69 memcpy(&qxl_fb_image->pseudo_palette,
71 sizeof(qxl_fb_image->pseudo_palette));
74 if (image->depth == 1)
75 qxl_fb_image->visual = FB_VISUAL_MONO10;
77 qxl_fb_image->visual = FB_VISUAL_DIRECTCOLOR;
80 memcpy(&qxl_fb_image->fb_image, image,
81 sizeof(qxl_fb_image->fb_image));
85 static void qxl_fb_dirty_flush(struct fb_info *info)
87 struct qxl_fbdev *qfbdev = info->par;
88 struct qxl_device *qdev = qfbdev->qdev;
89 struct qxl_fb_image qxl_fb_image;
90 struct fb_image *image = &qxl_fb_image.fb_image;
93 /* TODO: hard coding 32 bpp */
94 int stride = qfbdev->qfb.base.pitches[0] * 4;
96 x1 = qfbdev->dirty.x1;
97 x2 = qfbdev->dirty.x2;
98 y1 = qfbdev->dirty.y1;
99 y2 = qfbdev->dirty.y2;
101 * we are using a shadow draw buffer, at qdev->surface0_shadow
103 qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]", x1, x2, y1, y2);
106 image->width = x2 - x1;
107 image->height = y2 - y1;
108 image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
111 image->depth = 32; /* TODO: take from somewhere? */
112 image->cmap.start = 0;
114 image->cmap.red = NULL;
115 image->cmap.green = NULL;
116 image->cmap.blue = NULL;
117 image->cmap.transp = NULL;
118 image->data = qfbdev->shadow + (x1 * 4) + (stride * y1);
120 qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
121 qxl_draw_opaque_fb(&qxl_fb_image, stride);
122 qfbdev->dirty.x1 = 0;
123 qfbdev->dirty.x2 = 0;
124 qfbdev->dirty.y1 = 0;
125 qfbdev->dirty.y2 = 0;
128 static void qxl_deferred_io(struct fb_info *info,
129 struct list_head *pagelist)
131 struct qxl_fbdev *qfbdev = info->par;
132 unsigned long start, end, min, max;
138 list_for_each_entry(page, pagelist, lru) {
139 start = page->index << PAGE_SHIFT;
140 end = start + PAGE_SIZE - 1;
141 min = min(min, start);
146 y1 = min / info->fix.line_length;
147 y2 = (max / info->fix.line_length) + 1;
149 /* TODO: add spin lock? */
150 /* spin_lock_irqsave(&qfbdev->dirty.lock, flags); */
151 qfbdev->dirty.x1 = 0;
152 qfbdev->dirty.y1 = y1;
153 qfbdev->dirty.x2 = info->var.xres;
154 qfbdev->dirty.y2 = y2;
155 /* spin_unlock_irqrestore(&qfbdev->dirty.lock, flags); */
158 qxl_fb_dirty_flush(info);
162 static struct fb_deferred_io qxl_defio = {
163 .delay = QXL_DIRTY_DELAY,
164 .deferred_io = qxl_deferred_io,
167 static void qxl_fb_fillrect(struct fb_info *info,
168 const struct fb_fillrect *fb_rect)
170 struct qxl_fbdev *qfbdev = info->par;
171 struct qxl_device *qdev = qfbdev->qdev;
172 struct qxl_rect rect;
176 int width = fb_rect->width;
177 int height = fb_rect->height;
179 struct qxl_draw_fill qxl_draw_fill_rec;
181 if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
182 info->fix.visual == FB_VISUAL_DIRECTCOLOR)
183 color = ((u32 *) (info->pseudo_palette))[fb_rect->color];
185 color = fb_rect->color;
187 rect.right = x + width;
189 rect.bottom = y + height;
190 switch (fb_rect->rop) {
192 rop = SPICE_ROPD_OP_XOR;
195 rop = SPICE_ROPD_OP_PUT;
198 pr_err("qxl_fb_fillrect(): unknown rop, "
199 "defaulting to SPICE_ROPD_OP_PUT\n");
200 rop = SPICE_ROPD_OP_PUT;
202 qxl_draw_fill_rec.qdev = qdev;
203 qxl_draw_fill_rec.rect = rect;
204 qxl_draw_fill_rec.color = color;
205 qxl_draw_fill_rec.rop = rop;
206 if (!drm_can_sleep()) {
208 "%s: TODO use RCU, mysterious locks with spin_lock\n",
212 qxl_draw_fill(&qxl_draw_fill_rec);
215 static void qxl_fb_copyarea(struct fb_info *info,
216 const struct fb_copyarea *region)
218 struct qxl_fbdev *qfbdev = info->par;
220 qxl_draw_copyarea(qfbdev->qdev,
221 region->width, region->height,
222 region->sx, region->sy,
223 region->dx, region->dy);
226 static void qxl_fb_imageblit_safe(struct qxl_fb_image *qxl_fb_image)
228 qxl_draw_opaque_fb(qxl_fb_image, 0);
231 static void qxl_fb_imageblit(struct fb_info *info,
232 const struct fb_image *image)
234 struct qxl_fbdev *qfbdev = info->par;
235 struct qxl_device *qdev = qfbdev->qdev;
236 struct qxl_fb_image qxl_fb_image;
238 if (!drm_can_sleep()) {
239 /* we cannot do any ttm_bo allocation since that will fail on
240 * ioremap_wc..__get_vm_area_node, so queue the work item
241 * instead This can happen from printk inside an interrupt
242 * context, i.e.: smp_apic_timer_interrupt..check_cpu_stall */
244 "%s: TODO use RCU, mysterious locks with spin_lock\n",
249 /* ensure proper order of rendering operations - TODO: must do this
251 qxl_fb_image_init(&qxl_fb_image, qfbdev->qdev, info, image);
252 qxl_fb_imageblit_safe(&qxl_fb_image);
255 int qxl_fb_init(struct qxl_device *qdev)
260 static struct fb_ops qxlfb_ops = {
261 .owner = THIS_MODULE,
262 .fb_check_var = drm_fb_helper_check_var,
263 .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
264 .fb_fillrect = qxl_fb_fillrect,
265 .fb_copyarea = qxl_fb_copyarea,
266 .fb_imageblit = qxl_fb_imageblit,
267 .fb_pan_display = drm_fb_helper_pan_display,
268 .fb_blank = drm_fb_helper_blank,
269 .fb_setcmap = drm_fb_helper_setcmap,
270 .fb_debug_enter = drm_fb_helper_debug_enter,
271 .fb_debug_leave = drm_fb_helper_debug_leave,
274 static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
276 struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
279 ret = qxl_bo_reserve(qbo, false);
280 if (likely(ret == 0)) {
283 qxl_bo_unreserve(qbo);
285 drm_gem_object_unreference_unlocked(gobj);
288 int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
289 struct drm_file *file_priv,
293 struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
296 /* drm_get_handle_create adds a reference - good */
297 r = drm_gem_handle_create(file_priv, gobj, handle);
303 static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
304 struct drm_mode_fb_cmd2 *mode_cmd,
305 struct drm_gem_object **gobj_p)
307 struct qxl_device *qdev = qfbdev->qdev;
308 struct drm_gem_object *gobj = NULL;
309 struct qxl_bo *qbo = NULL;
311 int aligned_size, size;
312 int height = mode_cmd->height;
316 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &bpp, &depth);
318 size = mode_cmd->pitches[0] * height;
319 aligned_size = ALIGN(size, PAGE_SIZE);
320 /* TODO: unallocate and reallocate surface0 for real. Hack to just
321 * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
322 ret = qxl_gem_object_create(qdev, aligned_size, 0,
323 QXL_GEM_DOMAIN_SURFACE,
324 false, /* is discardable */
325 false, /* is kernel (false means device) */
329 pr_err("failed to allocate framebuffer (%d)\n",
333 qbo = gem_to_qxl_bo(gobj);
335 qbo->surf.width = mode_cmd->width;
336 qbo->surf.height = mode_cmd->height;
337 qbo->surf.stride = mode_cmd->pitches[0];
338 qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
339 ret = qxl_bo_reserve(qbo, false);
340 if (unlikely(ret != 0))
342 ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
344 qxl_bo_unreserve(qbo);
347 ret = qxl_bo_kmap(qbo, NULL);
348 qxl_bo_unreserve(qbo); /* unreserve, will be mmaped */
355 qxlfb_destroy_pinned_object(gobj);
360 static int qxlfb_create(struct qxl_fbdev *qfbdev,
361 struct drm_fb_helper_surface_size *sizes)
363 struct qxl_device *qdev = qfbdev->qdev;
364 struct fb_info *info;
365 struct drm_framebuffer *fb = NULL;
366 struct drm_mode_fb_cmd2 mode_cmd;
367 struct drm_gem_object *gobj = NULL;
368 struct qxl_bo *qbo = NULL;
369 struct device *device = &qdev->pdev->dev;
372 int bpp = sizes->surface_bpp;
373 int depth = sizes->surface_depth;
376 mode_cmd.width = sizes->surface_width;
377 mode_cmd.height = sizes->surface_height;
379 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
380 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
382 ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
383 qbo = gem_to_qxl_bo(gobj);
384 QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
385 mode_cmd.height, mode_cmd.pitches[0]);
387 shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
388 /* TODO: what's the usual response to memory allocation errors? */
391 "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
392 qxl_bo_gpu_offset(qbo),
393 qxl_bo_mmap_offset(qbo),
396 size = mode_cmd.pitches[0] * mode_cmd.height;
398 info = framebuffer_alloc(0, device);
406 qxl_framebuffer_init(qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj);
408 fb = &qfbdev->qfb.base;
410 /* setup helper with fb data */
411 qfbdev->helper.fb = fb;
412 qfbdev->helper.fbdev = info;
413 qfbdev->shadow = shadow;
414 strcpy(info->fix.id, "qxldrmfb");
416 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
418 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
419 info->fbops = &qxlfb_ops;
422 * TODO: using gobj->size in various places in this function. Not sure
423 * what the difference between the different sizes is.
425 info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
426 info->fix.smem_len = gobj->size;
427 info->screen_base = qfbdev->shadow;
428 info->screen_size = gobj->size;
430 drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
433 /* setup aperture base/size for vesafb takeover */
434 info->apertures = alloc_apertures(1);
435 if (!info->apertures) {
439 info->apertures->ranges[0].base = qdev->ddev->mode_config.fb_base;
440 info->apertures->ranges[0].size = qdev->vram_size;
442 info->fix.mmio_start = 0;
443 info->fix.mmio_len = 0;
445 if (info->screen_base == NULL) {
450 ret = fb_alloc_cmap(&info->cmap, 256, 0);
456 info->fbdefio = &qxl_defio;
457 fb_deferred_io_init(info);
459 qdev->fbdev_info = info;
460 qdev->fbdev_qfb = &qfbdev->qfb;
461 DRM_INFO("fb mappable at 0x%lX, size %lu\n", info->fix.smem_start, (unsigned long)info->screen_size);
462 DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n", fb->depth, fb->pitches[0], fb->width, fb->height);
467 ret = qxl_bo_reserve(qbo, false);
468 if (likely(ret == 0)) {
471 qxl_bo_unreserve(qbo);
475 drm_gem_object_unreference(gobj);
476 drm_framebuffer_cleanup(fb);
479 drm_gem_object_unreference(gobj);
483 static int qxl_fb_find_or_create_single(
484 struct drm_fb_helper *helper,
485 struct drm_fb_helper_surface_size *sizes)
487 struct qxl_fbdev *qfbdev = (struct qxl_fbdev *)helper;
492 ret = qxlfb_create(qfbdev, sizes);
500 static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
502 struct fb_info *info;
503 struct qxl_framebuffer *qfb = &qfbdev->qfb;
505 if (qfbdev->helper.fbdev) {
506 info = qfbdev->helper.fbdev;
508 unregister_framebuffer(info);
509 framebuffer_release(info);
512 qxlfb_destroy_pinned_object(qfb->obj);
515 drm_fb_helper_fini(&qfbdev->helper);
516 vfree(qfbdev->shadow);
517 drm_framebuffer_cleanup(&qfb->base);
522 static struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
524 .gamma_set = qxl_crtc_fb_gamma_set,
525 .gamma_get = qxl_crtc_fb_gamma_get,
527 .fb_probe = qxl_fb_find_or_create_single,
530 int qxl_fbdev_init(struct qxl_device *qdev)
532 struct qxl_fbdev *qfbdev;
533 int bpp_sel = 32; /* TODO: parameter from somewhere? */
536 qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
541 qdev->mode_info.qfbdev = qfbdev;
542 qfbdev->helper.funcs = &qxl_fb_helper_funcs;
544 ret = drm_fb_helper_init(qdev->ddev, &qfbdev->helper,
545 1 /* num_crtc - QXL supports just 1 */,
552 drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
553 drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
557 void qxl_fbdev_fini(struct qxl_device *qdev)
559 if (!qdev->mode_info.qfbdev)
562 qxl_fbdev_destroy(qdev->ddev, qdev->mode_info.qfbdev);
563 kfree(qdev->mode_info.qfbdev);
564 qdev->mode_info.qfbdev = NULL;