blob: 739a08cddf6dccb457f1042749f5a8156ff34d94 [file] [log] [blame]
Dave Airlief64122c2013-02-25 14:47:55 +10001/*
2 * Copyright © 2013 Red Hat
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 *
23 * Authors:
24 * David Airlie
25 */
26#include <linux/module.h>
27#include <linux/fb.h>
28
29#include "drmP.h"
30#include "drm/drm.h"
31#include "drm/drm_crtc.h"
32#include "drm/drm_crtc_helper.h"
33#include "qxl_drv.h"
34
35#include "qxl_object.h"
36#include "drm_fb_helper.h"
37
38#define QXL_DIRTY_DELAY (HZ / 30)
39
40struct qxl_fbdev {
41 struct drm_fb_helper helper;
42 struct qxl_framebuffer qfb;
Dave Airlief64122c2013-02-25 14:47:55 +100043 struct qxl_device *qdev;
44
Dave Airlie0665f9f2013-07-22 14:37:39 +100045 spinlock_t delayed_ops_lock;
46 struct list_head delayed_ops;
Dave Airlief64122c2013-02-25 14:47:55 +100047 void *shadow;
48 int size;
Dave Airlief64122c2013-02-25 14:47:55 +100049};
50
51static 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)
54{
55 qxl_fb_image->qdev = qdev;
56 if (info) {
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,
61 info->pseudo_palette,
62 sizeof(qxl_fb_image->pseudo_palette));
63 } else {
64 /* fallback */
65 if (image->depth == 1)
66 qxl_fb_image->visual = FB_VISUAL_MONO10;
67 else
68 qxl_fb_image->visual = FB_VISUAL_DIRECTCOLOR;
69 }
70 if (image) {
71 memcpy(&qxl_fb_image->fb_image, image,
72 sizeof(qxl_fb_image->fb_image));
73 }
74}
75
Dave Airlie6d01f1f2013-04-16 13:24:25 +100076static struct fb_deferred_io qxl_defio = {
Dave Airlief64122c2013-02-25 14:47:55 +100077 .delay = QXL_DIRTY_DELAY,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +020078 .deferred_io = drm_fb_helper_deferred_io,
Dave Airlief64122c2013-02-25 14:47:55 +100079};
80
Dave Airlief64122c2013-02-25 14:47:55 +100081static struct fb_ops qxlfb_ops = {
82 .owner = THIS_MODULE,
83 .fb_check_var = drm_fb_helper_check_var,
84 .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
Noralf Trønnes6819c3c2016-04-28 17:18:36 +020085 .fb_fillrect = drm_fb_helper_sys_fillrect,
86 .fb_copyarea = drm_fb_helper_sys_copyarea,
87 .fb_imageblit = drm_fb_helper_sys_imageblit,
Dave Airlief64122c2013-02-25 14:47:55 +100088 .fb_pan_display = drm_fb_helper_pan_display,
89 .fb_blank = drm_fb_helper_blank,
90 .fb_setcmap = drm_fb_helper_setcmap,
91 .fb_debug_enter = drm_fb_helper_debug_enter,
92 .fb_debug_leave = drm_fb_helper_debug_leave,
93};
94
95static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
96{
97 struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
98 int ret;
99
100 ret = qxl_bo_reserve(qbo, false);
101 if (likely(ret == 0)) {
102 qxl_bo_kunmap(qbo);
103 qxl_bo_unpin(qbo);
104 qxl_bo_unreserve(qbo);
105 }
106 drm_gem_object_unreference_unlocked(gobj);
107}
108
109int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
110 struct drm_file *file_priv,
111 uint32_t *handle)
112{
113 int r;
114 struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
115
116 BUG_ON(!gobj);
117 /* drm_get_handle_create adds a reference - good */
118 r = drm_gem_handle_create(file_priv, gobj, handle);
119 if (r)
120 return r;
121 return 0;
122}
123
124static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200125 const struct drm_mode_fb_cmd2 *mode_cmd,
Dave Airlief64122c2013-02-25 14:47:55 +1000126 struct drm_gem_object **gobj_p)
127{
128 struct qxl_device *qdev = qfbdev->qdev;
129 struct drm_gem_object *gobj = NULL;
130 struct qxl_bo *qbo = NULL;
131 int ret;
132 int aligned_size, size;
133 int height = mode_cmd->height;
134 int bpp;
135 int depth;
136
137 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &bpp, &depth);
138
139 size = mode_cmd->pitches[0] * height;
140 aligned_size = ALIGN(size, PAGE_SIZE);
141 /* TODO: unallocate and reallocate surface0 for real. Hack to just
142 * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
143 ret = qxl_gem_object_create(qdev, aligned_size, 0,
144 QXL_GEM_DOMAIN_SURFACE,
145 false, /* is discardable */
146 false, /* is kernel (false means device) */
147 NULL,
148 &gobj);
149 if (ret) {
150 pr_err("failed to allocate framebuffer (%d)\n",
151 aligned_size);
152 return -ENOMEM;
153 }
154 qbo = gem_to_qxl_bo(gobj);
155
156 qbo->surf.width = mode_cmd->width;
157 qbo->surf.height = mode_cmd->height;
158 qbo->surf.stride = mode_cmd->pitches[0];
159 qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
160 ret = qxl_bo_reserve(qbo, false);
161 if (unlikely(ret != 0))
162 goto out_unref;
163 ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
164 if (ret) {
165 qxl_bo_unreserve(qbo);
166 goto out_unref;
167 }
168 ret = qxl_bo_kmap(qbo, NULL);
169 qxl_bo_unreserve(qbo); /* unreserve, will be mmaped */
170 if (ret)
171 goto out_unref;
172
173 *gobj_p = gobj;
174 return 0;
175out_unref:
176 qxlfb_destroy_pinned_object(gobj);
177 *gobj_p = NULL;
178 return ret;
179}
180
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200181/*
182 * FIXME
183 * It should not be necessary to have a special dirty() callback for fbdev.
184 */
185static int qxlfb_framebuffer_dirty(struct drm_framebuffer *fb,
186 struct drm_file *file_priv,
187 unsigned flags, unsigned color,
188 struct drm_clip_rect *clips,
189 unsigned num_clips)
190{
191 struct qxl_device *qdev = fb->dev->dev_private;
192 struct fb_info *info = qdev->fbdev_info;
193 struct qxl_fbdev *qfbdev = info->par;
194 struct qxl_fb_image qxl_fb_image;
195 struct fb_image *image = &qxl_fb_image.fb_image;
196
197 /* TODO: hard coding 32 bpp */
198 int stride = qfbdev->qfb.base.pitches[0];
199
200 /*
201 * we are using a shadow draw buffer, at qdev->surface0_shadow
202 */
203 qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]", clips->x1, clips->x2,
204 clips->y1, clips->y2);
205 image->dx = clips->x1;
206 image->dy = clips->y1;
207 image->width = clips->x2 - clips->x1;
208 image->height = clips->y2 - clips->y1;
209 image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
210 warnings */
211 image->bg_color = 0;
212 image->depth = 32; /* TODO: take from somewhere? */
213 image->cmap.start = 0;
214 image->cmap.len = 0;
215 image->cmap.red = NULL;
216 image->cmap.green = NULL;
217 image->cmap.blue = NULL;
218 image->cmap.transp = NULL;
219 image->data = qfbdev->shadow + (clips->x1 * 4) + (stride * clips->y1);
220
221 qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
222 qxl_draw_opaque_fb(&qxl_fb_image, stride);
223
224 return 0;
225}
226
227static const struct drm_framebuffer_funcs qxlfb_fb_funcs = {
228 .destroy = qxl_user_framebuffer_destroy,
229 .dirty = qxlfb_framebuffer_dirty,
230};
231
Dave Airlief64122c2013-02-25 14:47:55 +1000232static int qxlfb_create(struct qxl_fbdev *qfbdev,
233 struct drm_fb_helper_surface_size *sizes)
234{
235 struct qxl_device *qdev = qfbdev->qdev;
236 struct fb_info *info;
237 struct drm_framebuffer *fb = NULL;
238 struct drm_mode_fb_cmd2 mode_cmd;
239 struct drm_gem_object *gobj = NULL;
240 struct qxl_bo *qbo = NULL;
Dave Airlief64122c2013-02-25 14:47:55 +1000241 int ret;
242 int size;
243 int bpp = sizes->surface_bpp;
244 int depth = sizes->surface_depth;
245 void *shadow;
246
247 mode_cmd.width = sizes->surface_width;
248 mode_cmd.height = sizes->surface_height;
249
250 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
251 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
252
253 ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
254 qbo = gem_to_qxl_bo(gobj);
255 QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
256 mode_cmd.height, mode_cmd.pitches[0]);
257
258 shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
259 /* TODO: what's the usual response to memory allocation errors? */
260 BUG_ON(!shadow);
261 QXL_INFO(qdev,
262 "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
263 qxl_bo_gpu_offset(qbo),
264 qxl_bo_mmap_offset(qbo),
265 qbo->kptr,
266 shadow);
267 size = mode_cmd.pitches[0] * mode_cmd.height;
268
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530269 info = drm_fb_helper_alloc_fbi(&qfbdev->helper);
270 if (IS_ERR(info)) {
271 ret = PTR_ERR(info);
Dave Airlief64122c2013-02-25 14:47:55 +1000272 goto out_unref;
273 }
274
275 info->par = qfbdev;
276
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200277 qxl_framebuffer_init(qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj,
278 &qxlfb_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000279
280 fb = &qfbdev->qfb.base;
281
282 /* setup helper with fb data */
283 qfbdev->helper.fb = fb;
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530284
Dave Airlief64122c2013-02-25 14:47:55 +1000285 qfbdev->shadow = shadow;
286 strcpy(info->fix.id, "qxldrmfb");
287
288 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
289
290 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
291 info->fbops = &qxlfb_ops;
292
293 /*
294 * TODO: using gobj->size in various places in this function. Not sure
295 * what the difference between the different sizes is.
296 */
297 info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
298 info->fix.smem_len = gobj->size;
299 info->screen_base = qfbdev->shadow;
300 info->screen_size = gobj->size;
301
302 drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
303 sizes->fb_height);
304
305 /* setup aperture base/size for vesafb takeover */
Dave Airlief64122c2013-02-25 14:47:55 +1000306 info->apertures->ranges[0].base = qdev->ddev->mode_config.fb_base;
307 info->apertures->ranges[0].size = qdev->vram_size;
308
309 info->fix.mmio_start = 0;
310 info->fix.mmio_len = 0;
311
312 if (info->screen_base == NULL) {
313 ret = -ENOSPC;
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530314 goto out_destroy_fbi;
Dave Airlief64122c2013-02-25 14:47:55 +1000315 }
316
317 info->fbdefio = &qxl_defio;
318 fb_deferred_io_init(info);
319
320 qdev->fbdev_info = info;
321 qdev->fbdev_qfb = &qfbdev->qfb;
322 DRM_INFO("fb mappable at 0x%lX, size %lu\n", info->fix.smem_start, (unsigned long)info->screen_size);
323 DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n", fb->depth, fb->pitches[0], fb->width, fb->height);
324 return 0;
325
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530326out_destroy_fbi:
327 drm_fb_helper_release_fbi(&qfbdev->helper);
Dave Airlief64122c2013-02-25 14:47:55 +1000328out_unref:
329 if (qbo) {
330 ret = qxl_bo_reserve(qbo, false);
331 if (likely(ret == 0)) {
332 qxl_bo_kunmap(qbo);
333 qxl_bo_unpin(qbo);
334 qxl_bo_unreserve(qbo);
335 }
336 }
337 if (fb && ret) {
Daniel Vetter068149a2016-03-30 11:40:42 +0200338 drm_gem_object_unreference_unlocked(gobj);
Dave Airlief64122c2013-02-25 14:47:55 +1000339 drm_framebuffer_cleanup(fb);
340 kfree(fb);
341 }
Daniel Vetter068149a2016-03-30 11:40:42 +0200342 drm_gem_object_unreference_unlocked(gobj);
Dave Airlief64122c2013-02-25 14:47:55 +1000343 return ret;
344}
345
346static int qxl_fb_find_or_create_single(
347 struct drm_fb_helper *helper,
348 struct drm_fb_helper_surface_size *sizes)
349{
Fabian Frederickf38e34a2014-09-14 18:40:19 +0200350 struct qxl_fbdev *qfbdev =
351 container_of(helper, struct qxl_fbdev, helper);
Dave Airlief64122c2013-02-25 14:47:55 +1000352 int new_fb = 0;
353 int ret;
354
355 if (!helper->fb) {
356 ret = qxlfb_create(qfbdev, sizes);
357 if (ret)
358 return ret;
359 new_fb = 1;
360 }
361 return new_fb;
362}
363
364static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
365{
Dave Airlief64122c2013-02-25 14:47:55 +1000366 struct qxl_framebuffer *qfb = &qfbdev->qfb;
367
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530368 drm_fb_helper_unregister_fbi(&qfbdev->helper);
369 drm_fb_helper_release_fbi(&qfbdev->helper);
Dave Airlief64122c2013-02-25 14:47:55 +1000370
Dave Airlief64122c2013-02-25 14:47:55 +1000371 if (qfb->obj) {
372 qxlfb_destroy_pinned_object(qfb->obj);
373 qfb->obj = NULL;
374 }
375 drm_fb_helper_fini(&qfbdev->helper);
376 vfree(qfbdev->shadow);
377 drm_framebuffer_cleanup(&qfb->base);
378
379 return 0;
380}
381
Thierry Reding3a493872014-06-27 17:19:23 +0200382static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
Dave Airlief64122c2013-02-25 14:47:55 +1000383 .fb_probe = qxl_fb_find_or_create_single,
384};
385
386int qxl_fbdev_init(struct qxl_device *qdev)
387{
388 struct qxl_fbdev *qfbdev;
389 int bpp_sel = 32; /* TODO: parameter from somewhere? */
390 int ret;
391
392 qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
393 if (!qfbdev)
394 return -ENOMEM;
395
396 qfbdev->qdev = qdev;
397 qdev->mode_info.qfbdev = qfbdev;
Dave Airlie0665f9f2013-07-22 14:37:39 +1000398 spin_lock_init(&qfbdev->delayed_ops_lock);
399 INIT_LIST_HEAD(&qfbdev->delayed_ops);
Thierry Reding10a23102014-06-27 17:19:24 +0200400
401 drm_fb_helper_prepare(qdev->ddev, &qfbdev->helper,
402 &qxl_fb_helper_funcs);
403
Dave Airlief64122c2013-02-25 14:47:55 +1000404 ret = drm_fb_helper_init(qdev->ddev, &qfbdev->helper,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100405 qxl_num_crtc /* num_crtc - QXL supports just 1 */,
Dave Airlief64122c2013-02-25 14:47:55 +1000406 QXLFB_CONN_LIMIT);
Thierry Reding01934c22014-12-19 11:21:32 +0100407 if (ret)
408 goto free;
Dave Airlief64122c2013-02-25 14:47:55 +1000409
Thierry Reding01934c22014-12-19 11:21:32 +0100410 ret = drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
411 if (ret)
412 goto fini;
413
414 ret = drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
415 if (ret)
416 goto fini;
417
Dave Airlief64122c2013-02-25 14:47:55 +1000418 return 0;
Thierry Reding01934c22014-12-19 11:21:32 +0100419
420fini:
421 drm_fb_helper_fini(&qfbdev->helper);
422free:
423 kfree(qfbdev);
424 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +1000425}
426
427void qxl_fbdev_fini(struct qxl_device *qdev)
428{
429 if (!qdev->mode_info.qfbdev)
430 return;
431
432 qxl_fbdev_destroy(qdev->ddev, qdev->mode_info.qfbdev);
433 kfree(qdev->mode_info.qfbdev);
434 qdev->mode_info.qfbdev = NULL;
435}
436
Dave Airlieb86487a2013-07-04 14:59:34 +1000437void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
438{
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530439 drm_fb_helper_set_suspend(&qdev->mode_info.qfbdev->helper, state);
Dave Airlieb86487a2013-07-04 14:59:34 +1000440}
Dave Airlief64122c2013-02-25 14:47:55 +1000441
Dave Airlieb86487a2013-07-04 14:59:34 +1000442bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
443{
444 if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))
445 return true;
446 return false;
447}