2 * Copyright (C) 2015 Red Hat, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 #include <drm/drm_fb_helper.h>
28 #include "virtgpu_drv.h"
30 #define VIRTIO_GPU_FBCON_POLL_PERIOD (HZ / 60)
32 static int virtio_gpu_dirty_update(struct virtio_gpu_framebuffer *fb,
33 bool store, int x, int y,
34 int width, int height)
36 struct drm_device *dev = fb->base.dev;
37 struct virtio_gpu_device *vgdev = dev->dev_private;
38 bool store_for_later = false;
39 int bpp = fb->base.format->cpp[0];
42 struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(fb->base.obj[0]);
45 (x + width > fb->base.width) ||
46 (y + height > fb->base.height)) {
47 DRM_DEBUG("values out of range %dx%d+%d+%d, fb %dx%d\n",
49 fb->base.width, fb->base.height);
54 * Can be called with pretty much any context (console output
55 * path). If we are in atomic just store the dirty rect info
56 * to send out the update later.
58 * Can't test inside spin lock.
60 if (in_atomic() || store)
61 store_for_later = true;
66 spin_lock_irqsave(&fb->dirty_lock, flags);
77 if (store_for_later) {
82 spin_unlock_irqrestore(&fb->dirty_lock, flags);
86 fb->x1 = fb->y1 = INT_MAX;
89 spin_unlock_irqrestore(&fb->dirty_lock, flags);
93 uint32_t w = x2 - x + 1;
94 uint32_t h = y2 - y + 1;
96 offset = (y * fb->base.pitches[0]) + x * bpp;
98 virtio_gpu_cmd_transfer_to_host_2d(vgdev, obj,
107 virtio_gpu_cmd_resource_flush(vgdev, obj->hw_res_handle,
108 x, y, x2 - x + 1, y2 - y + 1);
112 int virtio_gpu_surface_dirty(struct virtio_gpu_framebuffer *vgfb,
113 struct drm_clip_rect *clips,
114 unsigned int num_clips)
116 struct virtio_gpu_device *vgdev = vgfb->base.dev->dev_private;
117 struct virtio_gpu_object *obj = gem_to_virtio_gpu_obj(vgfb->base.obj[0]);
118 struct drm_clip_rect norect;
119 struct drm_clip_rect *clips_ptr;
120 int left, right, top, bottom;
127 norect.x1 = norect.y1 = 0;
128 norect.x2 = vgfb->base.width;
129 norect.y2 = vgfb->base.height;
136 /* skip the first clip rect */
137 for (i = 1, clips_ptr = clips + inc;
138 i < num_clips; i++, clips_ptr += inc) {
139 left = min_t(int, left, (int)clips_ptr->x1);
140 right = max_t(int, right, (int)clips_ptr->x2);
141 top = min_t(int, top, (int)clips_ptr->y1);
142 bottom = max_t(int, bottom, (int)clips_ptr->y2);
146 return virtio_gpu_dirty_update(vgfb, false, left, top,
147 right - left, bottom - top);
149 virtio_gpu_cmd_resource_flush(vgdev, obj->hw_res_handle,
150 left, top, right - left, bottom - top);
154 static void virtio_gpu_fb_dirty_work(struct work_struct *work)
156 struct delayed_work *delayed_work = to_delayed_work(work);
157 struct virtio_gpu_fbdev *vfbdev =
158 container_of(delayed_work, struct virtio_gpu_fbdev, work);
159 struct virtio_gpu_framebuffer *vgfb = &vfbdev->vgfb;
161 virtio_gpu_dirty_update(&vfbdev->vgfb, false, vgfb->x1, vgfb->y1,
162 vgfb->x2 - vgfb->x1, vgfb->y2 - vgfb->y1);
165 static void virtio_gpu_3d_fillrect(struct fb_info *info,
166 const struct fb_fillrect *rect)
168 struct virtio_gpu_fbdev *vfbdev = info->par;
170 drm_fb_helper_sys_fillrect(info, rect);
171 virtio_gpu_dirty_update(&vfbdev->vgfb, true, rect->dx, rect->dy,
172 rect->width, rect->height);
173 schedule_delayed_work(&vfbdev->work, VIRTIO_GPU_FBCON_POLL_PERIOD);
176 static void virtio_gpu_3d_copyarea(struct fb_info *info,
177 const struct fb_copyarea *area)
179 struct virtio_gpu_fbdev *vfbdev = info->par;
181 drm_fb_helper_sys_copyarea(info, area);
182 virtio_gpu_dirty_update(&vfbdev->vgfb, true, area->dx, area->dy,
183 area->width, area->height);
184 schedule_delayed_work(&vfbdev->work, VIRTIO_GPU_FBCON_POLL_PERIOD);
187 static void virtio_gpu_3d_imageblit(struct fb_info *info,
188 const struct fb_image *image)
190 struct virtio_gpu_fbdev *vfbdev = info->par;
192 drm_fb_helper_sys_imageblit(info, image);
193 virtio_gpu_dirty_update(&vfbdev->vgfb, true, image->dx, image->dy,
194 image->width, image->height);
195 schedule_delayed_work(&vfbdev->work, VIRTIO_GPU_FBCON_POLL_PERIOD);
198 static struct fb_ops virtio_gpufb_ops = {
199 .owner = THIS_MODULE,
200 DRM_FB_HELPER_DEFAULT_OPS,
201 .fb_fillrect = virtio_gpu_3d_fillrect,
202 .fb_copyarea = virtio_gpu_3d_copyarea,
203 .fb_imageblit = virtio_gpu_3d_imageblit,
206 static int virtio_gpufb_create(struct drm_fb_helper *helper,
207 struct drm_fb_helper_surface_size *sizes)
209 struct virtio_gpu_fbdev *vfbdev =
210 container_of(helper, struct virtio_gpu_fbdev, helper);
211 struct drm_device *dev = helper->dev;
212 struct virtio_gpu_device *vgdev = dev->dev_private;
213 struct fb_info *info;
214 struct drm_framebuffer *fb;
215 struct drm_mode_fb_cmd2 mode_cmd = {};
216 struct virtio_gpu_object *obj;
217 uint32_t format, size;
220 mode_cmd.width = sizes->surface_width;
221 mode_cmd.height = sizes->surface_height;
222 mode_cmd.pitches[0] = mode_cmd.width * 4;
223 mode_cmd.pixel_format = DRM_FORMAT_HOST_XRGB8888;
225 format = virtio_gpu_translate_format(mode_cmd.pixel_format);
229 size = mode_cmd.pitches[0] * mode_cmd.height;
230 obj = virtio_gpu_alloc_object(dev, size, false, true);
234 virtio_gpu_cmd_create_resource(vgdev, obj, format,
235 mode_cmd.width, mode_cmd.height);
237 ret = virtio_gpu_object_kmap(obj);
239 DRM_ERROR("failed to kmap fb %d\n", ret);
243 /* attach the object to the resource */
244 ret = virtio_gpu_object_attach(vgdev, obj, NULL);
248 info = drm_fb_helper_alloc_fbi(helper);
256 ret = virtio_gpu_framebuffer_init(dev, &vfbdev->vgfb,
257 &mode_cmd, &obj->gem_base);
261 fb = &vfbdev->vgfb.base;
263 vfbdev->helper.fb = fb;
265 strcpy(info->fix.id, "virtiodrmfb");
266 info->fbops = &virtio_gpufb_ops;
267 info->pixmap.flags = FB_PIXMAP_SYSTEM;
269 info->screen_buffer = obj->vmap;
270 info->screen_size = obj->gem_base.size;
271 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
272 drm_fb_helper_fill_var(info, &vfbdev->helper,
273 sizes->fb_width, sizes->fb_height);
275 info->fix.mmio_start = 0;
276 info->fix.mmio_len = 0;
280 virtio_gpu_object_detach(vgdev, obj);
283 virtio_gpu_gem_free_object(&obj->gem_base);
287 static int virtio_gpu_fbdev_destroy(struct drm_device *dev,
288 struct virtio_gpu_fbdev *vgfbdev)
290 struct virtio_gpu_framebuffer *vgfb = &vgfbdev->vgfb;
292 drm_fb_helper_unregister_fbi(&vgfbdev->helper);
294 if (vgfb->base.obj[0])
295 vgfb->base.obj[0] = NULL;
296 drm_fb_helper_fini(&vgfbdev->helper);
297 drm_framebuffer_cleanup(&vgfb->base);
301 static const struct drm_fb_helper_funcs virtio_gpu_fb_helper_funcs = {
302 .fb_probe = virtio_gpufb_create,
305 int virtio_gpu_fbdev_init(struct virtio_gpu_device *vgdev)
307 struct virtio_gpu_fbdev *vgfbdev;
308 int bpp_sel = 32; /* TODO: parameter from somewhere? */
311 vgfbdev = kzalloc(sizeof(struct virtio_gpu_fbdev), GFP_KERNEL);
315 vgfbdev->vgdev = vgdev;
316 vgdev->vgfbdev = vgfbdev;
317 INIT_DELAYED_WORK(&vgfbdev->work, virtio_gpu_fb_dirty_work);
319 drm_fb_helper_prepare(vgdev->ddev, &vgfbdev->helper,
320 &virtio_gpu_fb_helper_funcs);
321 ret = drm_fb_helper_init(vgdev->ddev, &vgfbdev->helper,
322 VIRTIO_GPUFB_CONN_LIMIT);
328 drm_fb_helper_single_add_all_connectors(&vgfbdev->helper);
329 drm_fb_helper_initial_config(&vgfbdev->helper, bpp_sel);
333 void virtio_gpu_fbdev_fini(struct virtio_gpu_device *vgdev)
338 virtio_gpu_fbdev_destroy(vgdev->ddev, vgdev->vgfbdev);
339 kfree(vgdev->vgfbdev);
340 vgdev->vgfbdev = NULL;