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
;
50 /* 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
;
94 /* TODO: hard coding 32 bpp */
95 int stride
= qfbdev
->qfb
.base
.pitches
[0];
97 spin_lock_irqsave(&qfbdev
->dirty
.lock
, flags
);
99 x1
= qfbdev
->dirty
.x1
;
100 x2
= qfbdev
->dirty
.x2
;
101 y1
= qfbdev
->dirty
.y1
;
102 y2
= qfbdev
->dirty
.y2
;
103 qfbdev
->dirty
.x1
= 0;
104 qfbdev
->dirty
.x2
= 0;
105 qfbdev
->dirty
.y1
= 0;
106 qfbdev
->dirty
.y2
= 0;
108 spin_unlock_irqrestore(&qfbdev
->dirty
.lock
, flags
);
111 * we are using a shadow draw buffer, at qdev->surface0_shadow
113 qxl_io_log(qdev
, "dirty x[%d, %d], y[%d, %d]", x1
, x2
, y1
, y2
);
116 image
->width
= x2
- x1
+ 1;
117 image
->height
= y2
- y1
+ 1;
118 image
->fg_color
= 0xffffffff; /* unused, just to avoid uninitialized
121 image
->depth
= 32; /* TODO: take from somewhere? */
122 image
->cmap
.start
= 0;
124 image
->cmap
.red
= NULL
;
125 image
->cmap
.green
= NULL
;
126 image
->cmap
.blue
= NULL
;
127 image
->cmap
.transp
= NULL
;
128 image
->data
= qfbdev
->shadow
+ (x1
* 4) + (stride
* y1
);
130 qxl_fb_image_init(&qxl_fb_image
, qdev
, info
, NULL
);
131 qxl_draw_opaque_fb(&qxl_fb_image
, stride
);
134 static void qxl_dirty_update(struct qxl_fbdev
*qfbdev
,
135 int x
, int y
, int width
, int height
)
137 struct qxl_device
*qdev
= qfbdev
->qdev
;
144 spin_lock_irqsave(&qfbdev
->dirty
.lock
, flags
);
146 if ((qfbdev
->dirty
.y2
- qfbdev
->dirty
.y1
) &&
147 (qfbdev
->dirty
.x2
- qfbdev
->dirty
.x1
)) {
148 if (qfbdev
->dirty
.y1
< y
)
149 y
= qfbdev
->dirty
.y1
;
150 if (qfbdev
->dirty
.y2
> y2
)
151 y2
= qfbdev
->dirty
.y2
;
152 if (qfbdev
->dirty
.x1
< x
)
153 x
= qfbdev
->dirty
.x1
;
154 if (qfbdev
->dirty
.x2
> x2
)
155 x2
= qfbdev
->dirty
.x2
;
158 qfbdev
->dirty
.x1
= x
;
159 qfbdev
->dirty
.x2
= x2
;
160 qfbdev
->dirty
.y1
= y
;
161 qfbdev
->dirty
.y2
= y2
;
163 spin_unlock_irqrestore(&qfbdev
->dirty
.lock
, flags
);
165 schedule_work(&qdev
->fb_work
);
168 static void qxl_deferred_io(struct fb_info
*info
,
169 struct list_head
*pagelist
)
171 struct qxl_fbdev
*qfbdev
= info
->par
;
172 unsigned long start
, end
, min
, max
;
178 list_for_each_entry(page
, pagelist
, lru
) {
179 start
= page
->index
<< PAGE_SHIFT
;
180 end
= start
+ PAGE_SIZE
- 1;
181 min
= min(min
, start
);
186 y1
= min
/ info
->fix
.line_length
;
187 y2
= (max
/ info
->fix
.line_length
) + 1;
188 qxl_dirty_update(qfbdev
, 0, y1
, info
->var
.xres
, y2
- y1
);
192 static struct fb_deferred_io qxl_defio
= {
193 .delay
= QXL_DIRTY_DELAY
,
194 .deferred_io
= qxl_deferred_io
,
197 static void qxl_fb_fillrect(struct fb_info
*info
,
198 const struct fb_fillrect
*rect
)
200 struct qxl_fbdev
*qfbdev
= info
->par
;
202 drm_fb_helper_sys_fillrect(info
, rect
);
203 qxl_dirty_update(qfbdev
, rect
->dx
, rect
->dy
, rect
->width
,
207 static void qxl_fb_copyarea(struct fb_info
*info
,
208 const struct fb_copyarea
*area
)
210 struct qxl_fbdev
*qfbdev
= info
->par
;
212 drm_fb_helper_sys_copyarea(info
, area
);
213 qxl_dirty_update(qfbdev
, area
->dx
, area
->dy
, area
->width
,
217 static void qxl_fb_imageblit(struct fb_info
*info
,
218 const struct fb_image
*image
)
220 struct qxl_fbdev
*qfbdev
= info
->par
;
222 drm_fb_helper_sys_imageblit(info
, image
);
223 qxl_dirty_update(qfbdev
, image
->dx
, image
->dy
, image
->width
,
227 static void qxl_fb_work(struct work_struct
*work
)
229 struct qxl_device
*qdev
= container_of(work
, struct qxl_device
, fb_work
);
230 struct qxl_fbdev
*qfbdev
= qdev
->mode_info
.qfbdev
;
232 qxl_fb_dirty_flush(qfbdev
->helper
.fbdev
);
235 int qxl_fb_init(struct qxl_device
*qdev
)
237 INIT_WORK(&qdev
->fb_work
, qxl_fb_work
);
241 static struct fb_ops qxlfb_ops
= {
242 .owner
= THIS_MODULE
,
243 .fb_check_var
= drm_fb_helper_check_var
,
244 .fb_set_par
= drm_fb_helper_set_par
, /* TODO: copy vmwgfx */
245 .fb_fillrect
= qxl_fb_fillrect
,
246 .fb_copyarea
= qxl_fb_copyarea
,
247 .fb_imageblit
= qxl_fb_imageblit
,
248 .fb_pan_display
= drm_fb_helper_pan_display
,
249 .fb_blank
= drm_fb_helper_blank
,
250 .fb_setcmap
= drm_fb_helper_setcmap
,
251 .fb_debug_enter
= drm_fb_helper_debug_enter
,
252 .fb_debug_leave
= drm_fb_helper_debug_leave
,
255 static void qxlfb_destroy_pinned_object(struct drm_gem_object
*gobj
)
257 struct qxl_bo
*qbo
= gem_to_qxl_bo(gobj
);
260 ret
= qxl_bo_reserve(qbo
, false);
261 if (likely(ret
== 0)) {
264 qxl_bo_unreserve(qbo
);
266 drm_gem_object_unreference_unlocked(gobj
);
269 int qxl_get_handle_for_primary_fb(struct qxl_device
*qdev
,
270 struct drm_file
*file_priv
,
274 struct drm_gem_object
*gobj
= qdev
->fbdev_qfb
->obj
;
277 /* drm_get_handle_create adds a reference - good */
278 r
= drm_gem_handle_create(file_priv
, gobj
, handle
);
284 static int qxlfb_create_pinned_object(struct qxl_fbdev
*qfbdev
,
285 const struct drm_mode_fb_cmd2
*mode_cmd
,
286 struct drm_gem_object
**gobj_p
)
288 struct qxl_device
*qdev
= qfbdev
->qdev
;
289 struct drm_gem_object
*gobj
= NULL
;
290 struct qxl_bo
*qbo
= NULL
;
292 int aligned_size
, size
;
293 int height
= mode_cmd
->height
;
297 drm_fb_get_bpp_depth(mode_cmd
->pixel_format
, &bpp
, &depth
);
299 size
= mode_cmd
->pitches
[0] * height
;
300 aligned_size
= ALIGN(size
, PAGE_SIZE
);
301 /* TODO: unallocate and reallocate surface0 for real. Hack to just
302 * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
303 ret
= qxl_gem_object_create(qdev
, aligned_size
, 0,
304 QXL_GEM_DOMAIN_SURFACE
,
305 false, /* is discardable */
306 false, /* is kernel (false means device) */
310 pr_err("failed to allocate framebuffer (%d)\n",
314 qbo
= gem_to_qxl_bo(gobj
);
316 qbo
->surf
.width
= mode_cmd
->width
;
317 qbo
->surf
.height
= mode_cmd
->height
;
318 qbo
->surf
.stride
= mode_cmd
->pitches
[0];
319 qbo
->surf
.format
= SPICE_SURFACE_FMT_32_xRGB
;
320 ret
= qxl_bo_reserve(qbo
, false);
321 if (unlikely(ret
!= 0))
323 ret
= qxl_bo_pin(qbo
, QXL_GEM_DOMAIN_SURFACE
, NULL
);
325 qxl_bo_unreserve(qbo
);
328 ret
= qxl_bo_kmap(qbo
, NULL
);
329 qxl_bo_unreserve(qbo
); /* unreserve, will be mmaped */
336 qxlfb_destroy_pinned_object(gobj
);
341 static int qxlfb_create(struct qxl_fbdev
*qfbdev
,
342 struct drm_fb_helper_surface_size
*sizes
)
344 struct qxl_device
*qdev
= qfbdev
->qdev
;
345 struct fb_info
*info
;
346 struct drm_framebuffer
*fb
= NULL
;
347 struct drm_mode_fb_cmd2 mode_cmd
;
348 struct drm_gem_object
*gobj
= NULL
;
349 struct qxl_bo
*qbo
= NULL
;
352 int bpp
= sizes
->surface_bpp
;
353 int depth
= sizes
->surface_depth
;
356 mode_cmd
.width
= sizes
->surface_width
;
357 mode_cmd
.height
= sizes
->surface_height
;
359 mode_cmd
.pitches
[0] = ALIGN(mode_cmd
.width
* ((bpp
+ 1) / 8), 64);
360 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(bpp
, depth
);
362 ret
= qxlfb_create_pinned_object(qfbdev
, &mode_cmd
, &gobj
);
363 qbo
= gem_to_qxl_bo(gobj
);
364 QXL_INFO(qdev
, "%s: %dx%d %d\n", __func__
, mode_cmd
.width
,
365 mode_cmd
.height
, mode_cmd
.pitches
[0]);
367 shadow
= vmalloc(mode_cmd
.pitches
[0] * mode_cmd
.height
);
368 /* TODO: what's the usual response to memory allocation errors? */
371 "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
372 qxl_bo_gpu_offset(qbo
),
373 qxl_bo_mmap_offset(qbo
),
376 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
378 info
= drm_fb_helper_alloc_fbi(&qfbdev
->helper
);
386 qxl_framebuffer_init(qdev
->ddev
, &qfbdev
->qfb
, &mode_cmd
, gobj
);
388 fb
= &qfbdev
->qfb
.base
;
390 /* setup helper with fb data */
391 qfbdev
->helper
.fb
= fb
;
393 qfbdev
->shadow
= shadow
;
394 strcpy(info
->fix
.id
, "qxldrmfb");
396 drm_fb_helper_fill_fix(info
, fb
->pitches
[0], fb
->depth
);
398 info
->flags
= FBINFO_DEFAULT
| FBINFO_HWACCEL_COPYAREA
| FBINFO_HWACCEL_FILLRECT
;
399 info
->fbops
= &qxlfb_ops
;
402 * TODO: using gobj->size in various places in this function. Not sure
403 * what the difference between the different sizes is.
405 info
->fix
.smem_start
= qdev
->vram_base
; /* TODO - correct? */
406 info
->fix
.smem_len
= gobj
->size
;
407 info
->screen_base
= qfbdev
->shadow
;
408 info
->screen_size
= gobj
->size
;
410 drm_fb_helper_fill_var(info
, &qfbdev
->helper
, sizes
->fb_width
,
413 /* setup aperture base/size for vesafb takeover */
414 info
->apertures
->ranges
[0].base
= qdev
->ddev
->mode_config
.fb_base
;
415 info
->apertures
->ranges
[0].size
= qdev
->vram_size
;
417 info
->fix
.mmio_start
= 0;
418 info
->fix
.mmio_len
= 0;
420 if (info
->screen_base
== NULL
) {
422 goto out_destroy_fbi
;
425 info
->fbdefio
= &qxl_defio
;
426 fb_deferred_io_init(info
);
428 qdev
->fbdev_info
= info
;
429 qdev
->fbdev_qfb
= &qfbdev
->qfb
;
430 DRM_INFO("fb mappable at 0x%lX, size %lu\n", info
->fix
.smem_start
, (unsigned long)info
->screen_size
);
431 DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n", fb
->depth
, fb
->pitches
[0], fb
->width
, fb
->height
);
435 drm_fb_helper_release_fbi(&qfbdev
->helper
);
438 ret
= qxl_bo_reserve(qbo
, false);
439 if (likely(ret
== 0)) {
442 qxl_bo_unreserve(qbo
);
446 drm_gem_object_unreference(gobj
);
447 drm_framebuffer_cleanup(fb
);
450 drm_gem_object_unreference(gobj
);
454 static int qxl_fb_find_or_create_single(
455 struct drm_fb_helper
*helper
,
456 struct drm_fb_helper_surface_size
*sizes
)
458 struct qxl_fbdev
*qfbdev
=
459 container_of(helper
, struct qxl_fbdev
, helper
);
464 ret
= qxlfb_create(qfbdev
, sizes
);
472 static int qxl_fbdev_destroy(struct drm_device
*dev
, struct qxl_fbdev
*qfbdev
)
474 struct qxl_framebuffer
*qfb
= &qfbdev
->qfb
;
476 drm_fb_helper_unregister_fbi(&qfbdev
->helper
);
477 drm_fb_helper_release_fbi(&qfbdev
->helper
);
480 qxlfb_destroy_pinned_object(qfb
->obj
);
483 drm_fb_helper_fini(&qfbdev
->helper
);
484 vfree(qfbdev
->shadow
);
485 drm_framebuffer_cleanup(&qfb
->base
);
490 static const struct drm_fb_helper_funcs qxl_fb_helper_funcs
= {
491 .fb_probe
= qxl_fb_find_or_create_single
,
494 int qxl_fbdev_init(struct qxl_device
*qdev
)
496 struct qxl_fbdev
*qfbdev
;
497 int bpp_sel
= 32; /* TODO: parameter from somewhere? */
500 qfbdev
= kzalloc(sizeof(struct qxl_fbdev
), GFP_KERNEL
);
505 qdev
->mode_info
.qfbdev
= qfbdev
;
506 spin_lock_init(&qfbdev
->delayed_ops_lock
);
507 spin_lock_init(&qfbdev
->dirty
.lock
);
508 INIT_LIST_HEAD(&qfbdev
->delayed_ops
);
510 drm_fb_helper_prepare(qdev
->ddev
, &qfbdev
->helper
,
511 &qxl_fb_helper_funcs
);
513 ret
= drm_fb_helper_init(qdev
->ddev
, &qfbdev
->helper
,
514 qxl_num_crtc
/* num_crtc - QXL supports just 1 */,
519 ret
= drm_fb_helper_single_add_all_connectors(&qfbdev
->helper
);
523 ret
= drm_fb_helper_initial_config(&qfbdev
->helper
, bpp_sel
);
530 drm_fb_helper_fini(&qfbdev
->helper
);
536 void qxl_fbdev_fini(struct qxl_device
*qdev
)
538 if (!qdev
->mode_info
.qfbdev
)
541 qxl_fbdev_destroy(qdev
->ddev
, qdev
->mode_info
.qfbdev
);
542 kfree(qdev
->mode_info
.qfbdev
);
543 qdev
->mode_info
.qfbdev
= NULL
;
546 void qxl_fbdev_set_suspend(struct qxl_device
*qdev
, int state
)
548 drm_fb_helper_set_suspend(&qdev
->mode_info
.qfbdev
->helper
, state
);
551 bool qxl_fbdev_qobj_is_fb(struct qxl_device
*qdev
, struct qxl_bo
*qobj
)
553 if (qobj
== gem_to_qxl_bo(qdev
->mode_info
.qfbdev
->qfb
.obj
))