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>
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/drm_fb_helper.h>
36 #include "qxl_object.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 #ifdef CONFIG_DRM_FBDEV_EMULATION
77 static struct fb_deferred_io qxl_defio
= {
78 .delay
= QXL_DIRTY_DELAY
,
79 .deferred_io
= drm_fb_helper_deferred_io
,
83 static struct fb_ops qxlfb_ops
= {
85 DRM_FB_HELPER_DEFAULT_OPS
,
86 .fb_fillrect
= drm_fb_helper_sys_fillrect
,
87 .fb_copyarea
= drm_fb_helper_sys_copyarea
,
88 .fb_imageblit
= drm_fb_helper_sys_imageblit
,
91 static void qxlfb_destroy_pinned_object(struct drm_gem_object
*gobj
)
93 struct qxl_bo
*qbo
= gem_to_qxl_bo(gobj
);
98 drm_gem_object_unreference_unlocked(gobj
);
101 int qxl_get_handle_for_primary_fb(struct qxl_device
*qdev
,
102 struct drm_file
*file_priv
,
106 struct drm_gem_object
*gobj
= qdev
->fbdev_qfb
->obj
;
109 /* drm_get_handle_create adds a reference - good */
110 r
= drm_gem_handle_create(file_priv
, gobj
, handle
);
116 static int qxlfb_create_pinned_object(struct qxl_fbdev
*qfbdev
,
117 const struct drm_mode_fb_cmd2
*mode_cmd
,
118 struct drm_gem_object
**gobj_p
)
120 struct qxl_device
*qdev
= qfbdev
->qdev
;
121 struct drm_gem_object
*gobj
= NULL
;
122 struct qxl_bo
*qbo
= NULL
;
124 int aligned_size
, size
;
125 int height
= mode_cmd
->height
;
127 size
= mode_cmd
->pitches
[0] * height
;
128 aligned_size
= ALIGN(size
, PAGE_SIZE
);
129 /* TODO: unallocate and reallocate surface0 for real. Hack to just
130 * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
131 ret
= qxl_gem_object_create(qdev
, aligned_size
, 0,
132 QXL_GEM_DOMAIN_SURFACE
,
133 false, /* is discardable */
134 false, /* is kernel (false means device) */
138 pr_err("failed to allocate framebuffer (%d)\n",
142 qbo
= gem_to_qxl_bo(gobj
);
144 qbo
->surf
.width
= mode_cmd
->width
;
145 qbo
->surf
.height
= mode_cmd
->height
;
146 qbo
->surf
.stride
= mode_cmd
->pitches
[0];
147 qbo
->surf
.format
= SPICE_SURFACE_FMT_32_xRGB
;
149 ret
= qxl_bo_pin(qbo
, QXL_GEM_DOMAIN_SURFACE
, NULL
);
153 ret
= qxl_bo_kmap(qbo
, NULL
);
161 qxlfb_destroy_pinned_object(gobj
);
168 * It should not be necessary to have a special dirty() callback for fbdev.
170 static int qxlfb_framebuffer_dirty(struct drm_framebuffer
*fb
,
171 struct drm_file
*file_priv
,
172 unsigned flags
, unsigned color
,
173 struct drm_clip_rect
*clips
,
176 struct qxl_device
*qdev
= fb
->dev
->dev_private
;
177 struct fb_info
*info
= qdev
->fbdev_info
;
178 struct qxl_fbdev
*qfbdev
= info
->par
;
179 struct qxl_fb_image qxl_fb_image
;
180 struct fb_image
*image
= &qxl_fb_image
.fb_image
;
182 /* TODO: hard coding 32 bpp */
183 int stride
= qfbdev
->qfb
.base
.pitches
[0];
186 * we are using a shadow draw buffer, at qdev->surface0_shadow
188 qxl_io_log(qdev
, "dirty x[%d, %d], y[%d, %d]\n", clips
->x1
, clips
->x2
,
189 clips
->y1
, clips
->y2
);
190 image
->dx
= clips
->x1
;
191 image
->dy
= clips
->y1
;
192 image
->width
= clips
->x2
- clips
->x1
;
193 image
->height
= clips
->y2
- clips
->y1
;
194 image
->fg_color
= 0xffffffff; /* unused, just to avoid uninitialized
197 image
->depth
= 32; /* TODO: take from somewhere? */
198 image
->cmap
.start
= 0;
200 image
->cmap
.red
= NULL
;
201 image
->cmap
.green
= NULL
;
202 image
->cmap
.blue
= NULL
;
203 image
->cmap
.transp
= NULL
;
204 image
->data
= qfbdev
->shadow
+ (clips
->x1
* 4) + (stride
* clips
->y1
);
206 qxl_fb_image_init(&qxl_fb_image
, qdev
, info
, NULL
);
207 qxl_draw_opaque_fb(&qxl_fb_image
, stride
);
212 static const struct drm_framebuffer_funcs qxlfb_fb_funcs
= {
213 .destroy
= qxl_user_framebuffer_destroy
,
214 .dirty
= qxlfb_framebuffer_dirty
,
217 static int qxlfb_create(struct qxl_fbdev
*qfbdev
,
218 struct drm_fb_helper_surface_size
*sizes
)
220 struct qxl_device
*qdev
= qfbdev
->qdev
;
221 struct fb_info
*info
;
222 struct drm_framebuffer
*fb
= NULL
;
223 struct drm_mode_fb_cmd2 mode_cmd
;
224 struct drm_gem_object
*gobj
= NULL
;
225 struct qxl_bo
*qbo
= NULL
;
228 int bpp
= sizes
->surface_bpp
;
229 int depth
= sizes
->surface_depth
;
232 mode_cmd
.width
= sizes
->surface_width
;
233 mode_cmd
.height
= sizes
->surface_height
;
235 mode_cmd
.pitches
[0] = ALIGN(mode_cmd
.width
* ((bpp
+ 1) / 8), 64);
236 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(bpp
, depth
);
238 ret
= qxlfb_create_pinned_object(qfbdev
, &mode_cmd
, &gobj
);
242 qbo
= gem_to_qxl_bo(gobj
);
243 DRM_DEBUG_DRIVER("%dx%d %d\n", mode_cmd
.width
,
244 mode_cmd
.height
, mode_cmd
.pitches
[0]);
246 shadow
= vmalloc(mode_cmd
.pitches
[0] * mode_cmd
.height
);
247 /* TODO: what's the usual response to memory allocation errors? */
249 DRM_DEBUG_DRIVER("surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
250 qxl_bo_gpu_offset(qbo
), qxl_bo_mmap_offset(qbo
),
252 size
= mode_cmd
.pitches
[0] * mode_cmd
.height
;
254 info
= drm_fb_helper_alloc_fbi(&qfbdev
->helper
);
262 qxl_framebuffer_init(&qdev
->ddev
, &qfbdev
->qfb
, &mode_cmd
, gobj
,
265 fb
= &qfbdev
->qfb
.base
;
267 /* setup helper with fb data */
268 qfbdev
->helper
.fb
= fb
;
270 qfbdev
->shadow
= shadow
;
271 strcpy(info
->fix
.id
, "qxldrmfb");
273 drm_fb_helper_fill_fix(info
, fb
->pitches
[0], fb
->format
->depth
);
275 info
->fbops
= &qxlfb_ops
;
278 * TODO: using gobj->size in various places in this function. Not sure
279 * what the difference between the different sizes is.
281 info
->fix
.smem_start
= qdev
->vram_base
; /* TODO - correct? */
282 info
->fix
.smem_len
= gobj
->size
;
283 info
->screen_base
= qfbdev
->shadow
;
284 info
->screen_size
= gobj
->size
;
286 drm_fb_helper_fill_var(info
, &qfbdev
->helper
, sizes
->fb_width
,
289 /* setup aperture base/size for vesafb takeover */
290 info
->apertures
->ranges
[0].base
= qdev
->ddev
.mode_config
.fb_base
;
291 info
->apertures
->ranges
[0].size
= qdev
->vram_size
;
293 info
->fix
.mmio_start
= 0;
294 info
->fix
.mmio_len
= 0;
296 if (info
->screen_base
== NULL
) {
301 #ifdef CONFIG_DRM_FBDEV_EMULATION
302 info
->fbdefio
= &qxl_defio
;
303 fb_deferred_io_init(info
);
306 qdev
->fbdev_info
= info
;
307 qdev
->fbdev_qfb
= &qfbdev
->qfb
;
308 DRM_INFO("fb mappable at 0x%lX, size %lu\n", info
->fix
.smem_start
, (unsigned long)info
->screen_size
);
309 DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n",
310 fb
->format
->depth
, fb
->pitches
[0], fb
->width
, fb
->height
);
319 drm_gem_object_unreference_unlocked(gobj
);
320 drm_framebuffer_cleanup(fb
);
323 drm_gem_object_unreference_unlocked(gobj
);
327 static int qxl_fb_find_or_create_single(
328 struct drm_fb_helper
*helper
,
329 struct drm_fb_helper_surface_size
*sizes
)
331 struct qxl_fbdev
*qfbdev
=
332 container_of(helper
, struct qxl_fbdev
, helper
);
337 ret
= qxlfb_create(qfbdev
, sizes
);
345 static int qxl_fbdev_destroy(struct drm_device
*dev
, struct qxl_fbdev
*qfbdev
)
347 struct qxl_framebuffer
*qfb
= &qfbdev
->qfb
;
349 drm_fb_helper_unregister_fbi(&qfbdev
->helper
);
352 qxlfb_destroy_pinned_object(qfb
->obj
);
355 drm_fb_helper_fini(&qfbdev
->helper
);
356 vfree(qfbdev
->shadow
);
357 drm_framebuffer_cleanup(&qfb
->base
);
362 static const struct drm_fb_helper_funcs qxl_fb_helper_funcs
= {
363 .fb_probe
= qxl_fb_find_or_create_single
,
366 int qxl_fbdev_init(struct qxl_device
*qdev
)
370 #ifdef CONFIG_DRM_FBDEV_EMULATION
371 struct qxl_fbdev
*qfbdev
;
372 int bpp_sel
= 32; /* TODO: parameter from somewhere? */
374 qfbdev
= kzalloc(sizeof(struct qxl_fbdev
), GFP_KERNEL
);
379 qdev
->mode_info
.qfbdev
= qfbdev
;
380 spin_lock_init(&qfbdev
->delayed_ops_lock
);
381 INIT_LIST_HEAD(&qfbdev
->delayed_ops
);
383 drm_fb_helper_prepare(&qdev
->ddev
, &qfbdev
->helper
,
384 &qxl_fb_helper_funcs
);
386 ret
= drm_fb_helper_init(&qdev
->ddev
, &qfbdev
->helper
,
391 ret
= drm_fb_helper_single_add_all_connectors(&qfbdev
->helper
);
395 ret
= drm_fb_helper_initial_config(&qfbdev
->helper
, bpp_sel
);
402 drm_fb_helper_fini(&qfbdev
->helper
);
410 void qxl_fbdev_fini(struct qxl_device
*qdev
)
412 if (!qdev
->mode_info
.qfbdev
)
415 qxl_fbdev_destroy(&qdev
->ddev
, qdev
->mode_info
.qfbdev
);
416 kfree(qdev
->mode_info
.qfbdev
);
417 qdev
->mode_info
.qfbdev
= NULL
;
420 void qxl_fbdev_set_suspend(struct qxl_device
*qdev
, int state
)
422 if (!qdev
->mode_info
.qfbdev
)
425 drm_fb_helper_set_suspend(&qdev
->mode_info
.qfbdev
->helper
, state
);
428 bool qxl_fbdev_qobj_is_fb(struct qxl_device
*qdev
, struct qxl_bo
*qobj
)
430 if (qobj
== gem_to_qxl_bo(qdev
->mode_info
.qfbdev
->qfb
.obj
))