blob: a0831c42c0739b7cf2ef4b521db3f774464e55f9 [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>
Dave Airlief64122c2013-02-25 14:47:55 +100027
28#include "drmP.h"
29#include "drm/drm.h"
30#include "drm/drm_crtc.h"
31#include "drm/drm_crtc_helper.h"
32#include "qxl_drv.h"
33
34#include "qxl_object.h"
35#include "drm_fb_helper.h"
36
37#define QXL_DIRTY_DELAY (HZ / 30)
38
39struct qxl_fbdev {
40 struct drm_fb_helper helper;
41 struct qxl_framebuffer qfb;
Dave Airlief64122c2013-02-25 14:47:55 +100042 struct qxl_device *qdev;
43
Dave Airlie0665f9f2013-07-22 14:37:39 +100044 spinlock_t delayed_ops_lock;
45 struct list_head delayed_ops;
Dave Airlief64122c2013-02-25 14:47:55 +100046 void *shadow;
47 int size;
Dave Airlief64122c2013-02-25 14:47:55 +100048};
49
50static void qxl_fb_image_init(struct qxl_fb_image *qxl_fb_image,
51 struct qxl_device *qdev, struct fb_info *info,
52 const struct fb_image *image)
53{
54 qxl_fb_image->qdev = qdev;
55 if (info) {
56 qxl_fb_image->visual = info->fix.visual;
57 if (qxl_fb_image->visual == FB_VISUAL_TRUECOLOR ||
58 qxl_fb_image->visual == FB_VISUAL_DIRECTCOLOR)
59 memcpy(&qxl_fb_image->pseudo_palette,
60 info->pseudo_palette,
61 sizeof(qxl_fb_image->pseudo_palette));
62 } else {
63 /* fallback */
64 if (image->depth == 1)
65 qxl_fb_image->visual = FB_VISUAL_MONO10;
66 else
67 qxl_fb_image->visual = FB_VISUAL_DIRECTCOLOR;
68 }
69 if (image) {
70 memcpy(&qxl_fb_image->fb_image, image,
71 sizeof(qxl_fb_image->fb_image));
72 }
73}
74
Dave Airlie6d01f1f2013-04-16 13:24:25 +100075static struct fb_deferred_io qxl_defio = {
Dave Airlief64122c2013-02-25 14:47:55 +100076 .delay = QXL_DIRTY_DELAY,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +020077 .deferred_io = drm_fb_helper_deferred_io,
Dave Airlief64122c2013-02-25 14:47:55 +100078};
79
Dave Airlief64122c2013-02-25 14:47:55 +100080static struct fb_ops qxlfb_ops = {
81 .owner = THIS_MODULE,
82 .fb_check_var = drm_fb_helper_check_var,
83 .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
Noralf Trønnes6819c3c2016-04-28 17:18:36 +020084 .fb_fillrect = drm_fb_helper_sys_fillrect,
85 .fb_copyarea = drm_fb_helper_sys_copyarea,
86 .fb_imageblit = drm_fb_helper_sys_imageblit,
Dave Airlief64122c2013-02-25 14:47:55 +100087 .fb_pan_display = drm_fb_helper_pan_display,
88 .fb_blank = drm_fb_helper_blank,
89 .fb_setcmap = drm_fb_helper_setcmap,
90 .fb_debug_enter = drm_fb_helper_debug_enter,
91 .fb_debug_leave = drm_fb_helper_debug_leave,
92};
93
94static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
95{
96 struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
97 int ret;
98
99 ret = qxl_bo_reserve(qbo, false);
100 if (likely(ret == 0)) {
101 qxl_bo_kunmap(qbo);
102 qxl_bo_unpin(qbo);
103 qxl_bo_unreserve(qbo);
104 }
105 drm_gem_object_unreference_unlocked(gobj);
106}
107
108int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
109 struct drm_file *file_priv,
110 uint32_t *handle)
111{
112 int r;
113 struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
114
115 BUG_ON(!gobj);
116 /* drm_get_handle_create adds a reference - good */
117 r = drm_gem_handle_create(file_priv, gobj, handle);
118 if (r)
119 return r;
120 return 0;
121}
122
123static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200124 const struct drm_mode_fb_cmd2 *mode_cmd,
Dave Airlief64122c2013-02-25 14:47:55 +1000125 struct drm_gem_object **gobj_p)
126{
127 struct qxl_device *qdev = qfbdev->qdev;
128 struct drm_gem_object *gobj = NULL;
129 struct qxl_bo *qbo = NULL;
130 int ret;
131 int aligned_size, size;
132 int height = mode_cmd->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000133
134 size = mode_cmd->pitches[0] * height;
135 aligned_size = ALIGN(size, PAGE_SIZE);
136 /* TODO: unallocate and reallocate surface0 for real. Hack to just
137 * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
138 ret = qxl_gem_object_create(qdev, aligned_size, 0,
139 QXL_GEM_DOMAIN_SURFACE,
140 false, /* is discardable */
141 false, /* is kernel (false means device) */
142 NULL,
143 &gobj);
144 if (ret) {
145 pr_err("failed to allocate framebuffer (%d)\n",
146 aligned_size);
147 return -ENOMEM;
148 }
149 qbo = gem_to_qxl_bo(gobj);
150
151 qbo->surf.width = mode_cmd->width;
152 qbo->surf.height = mode_cmd->height;
153 qbo->surf.stride = mode_cmd->pitches[0];
154 qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
155 ret = qxl_bo_reserve(qbo, false);
156 if (unlikely(ret != 0))
157 goto out_unref;
158 ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
159 if (ret) {
160 qxl_bo_unreserve(qbo);
161 goto out_unref;
162 }
163 ret = qxl_bo_kmap(qbo, NULL);
164 qxl_bo_unreserve(qbo); /* unreserve, will be mmaped */
165 if (ret)
166 goto out_unref;
167
168 *gobj_p = gobj;
169 return 0;
170out_unref:
171 qxlfb_destroy_pinned_object(gobj);
172 *gobj_p = NULL;
173 return ret;
174}
175
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200176/*
177 * FIXME
178 * It should not be necessary to have a special dirty() callback for fbdev.
179 */
180static int qxlfb_framebuffer_dirty(struct drm_framebuffer *fb,
181 struct drm_file *file_priv,
182 unsigned flags, unsigned color,
183 struct drm_clip_rect *clips,
184 unsigned num_clips)
185{
186 struct qxl_device *qdev = fb->dev->dev_private;
187 struct fb_info *info = qdev->fbdev_info;
188 struct qxl_fbdev *qfbdev = info->par;
189 struct qxl_fb_image qxl_fb_image;
190 struct fb_image *image = &qxl_fb_image.fb_image;
191
192 /* TODO: hard coding 32 bpp */
193 int stride = qfbdev->qfb.base.pitches[0];
194
195 /*
196 * we are using a shadow draw buffer, at qdev->surface0_shadow
197 */
198 qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]", clips->x1, clips->x2,
199 clips->y1, clips->y2);
200 image->dx = clips->x1;
201 image->dy = clips->y1;
202 image->width = clips->x2 - clips->x1;
203 image->height = clips->y2 - clips->y1;
204 image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
205 warnings */
206 image->bg_color = 0;
207 image->depth = 32; /* TODO: take from somewhere? */
208 image->cmap.start = 0;
209 image->cmap.len = 0;
210 image->cmap.red = NULL;
211 image->cmap.green = NULL;
212 image->cmap.blue = NULL;
213 image->cmap.transp = NULL;
214 image->data = qfbdev->shadow + (clips->x1 * 4) + (stride * clips->y1);
215
216 qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
217 qxl_draw_opaque_fb(&qxl_fb_image, stride);
218
219 return 0;
220}
221
222static const struct drm_framebuffer_funcs qxlfb_fb_funcs = {
223 .destroy = qxl_user_framebuffer_destroy,
224 .dirty = qxlfb_framebuffer_dirty,
225};
226
Dave Airlief64122c2013-02-25 14:47:55 +1000227static int qxlfb_create(struct qxl_fbdev *qfbdev,
228 struct drm_fb_helper_surface_size *sizes)
229{
230 struct qxl_device *qdev = qfbdev->qdev;
231 struct fb_info *info;
232 struct drm_framebuffer *fb = NULL;
233 struct drm_mode_fb_cmd2 mode_cmd;
234 struct drm_gem_object *gobj = NULL;
235 struct qxl_bo *qbo = NULL;
Dave Airlief64122c2013-02-25 14:47:55 +1000236 int ret;
237 int size;
238 int bpp = sizes->surface_bpp;
239 int depth = sizes->surface_depth;
240 void *shadow;
241
242 mode_cmd.width = sizes->surface_width;
243 mode_cmd.height = sizes->surface_height;
244
245 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
246 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
247
248 ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
Gerd Hoffmann2d6b1d42016-05-12 19:06:56 +0200249 if (ret < 0)
250 return ret;
251
Dave Airlief64122c2013-02-25 14:47:55 +1000252 qbo = gem_to_qxl_bo(gobj);
253 QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
254 mode_cmd.height, mode_cmd.pitches[0]);
255
256 shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
257 /* TODO: what's the usual response to memory allocation errors? */
258 BUG_ON(!shadow);
259 QXL_INFO(qdev,
260 "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
261 qxl_bo_gpu_offset(qbo),
262 qxl_bo_mmap_offset(qbo),
263 qbo->kptr,
264 shadow);
265 size = mode_cmd.pitches[0] * mode_cmd.height;
266
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530267 info = drm_fb_helper_alloc_fbi(&qfbdev->helper);
268 if (IS_ERR(info)) {
269 ret = PTR_ERR(info);
Dave Airlief64122c2013-02-25 14:47:55 +1000270 goto out_unref;
271 }
272
273 info->par = qfbdev;
274
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200275 qxl_framebuffer_init(qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj,
276 &qxlfb_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000277
278 fb = &qfbdev->qfb.base;
279
280 /* setup helper with fb data */
281 qfbdev->helper.fb = fb;
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530282
Dave Airlief64122c2013-02-25 14:47:55 +1000283 qfbdev->shadow = shadow;
284 strcpy(info->fix.id, "qxldrmfb");
285
286 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
287
288 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
289 info->fbops = &qxlfb_ops;
290
291 /*
292 * TODO: using gobj->size in various places in this function. Not sure
293 * what the difference between the different sizes is.
294 */
295 info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
296 info->fix.smem_len = gobj->size;
297 info->screen_base = qfbdev->shadow;
298 info->screen_size = gobj->size;
299
300 drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
301 sizes->fb_height);
302
303 /* setup aperture base/size for vesafb takeover */
Dave Airlief64122c2013-02-25 14:47:55 +1000304 info->apertures->ranges[0].base = qdev->ddev->mode_config.fb_base;
305 info->apertures->ranges[0].size = qdev->vram_size;
306
307 info->fix.mmio_start = 0;
308 info->fix.mmio_len = 0;
309
310 if (info->screen_base == NULL) {
311 ret = -ENOSPC;
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530312 goto out_destroy_fbi;
Dave Airlief64122c2013-02-25 14:47:55 +1000313 }
314
315 info->fbdefio = &qxl_defio;
316 fb_deferred_io_init(info);
317
318 qdev->fbdev_info = info;
319 qdev->fbdev_qfb = &qfbdev->qfb;
320 DRM_INFO("fb mappable at 0x%lX, size %lu\n", info->fix.smem_start, (unsigned long)info->screen_size);
321 DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n", fb->depth, fb->pitches[0], fb->width, fb->height);
322 return 0;
323
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530324out_destroy_fbi:
325 drm_fb_helper_release_fbi(&qfbdev->helper);
Dave Airlief64122c2013-02-25 14:47:55 +1000326out_unref:
327 if (qbo) {
328 ret = qxl_bo_reserve(qbo, false);
329 if (likely(ret == 0)) {
330 qxl_bo_kunmap(qbo);
331 qxl_bo_unpin(qbo);
332 qxl_bo_unreserve(qbo);
333 }
334 }
335 if (fb && ret) {
Daniel Vetter068149a2016-03-30 11:40:42 +0200336 drm_gem_object_unreference_unlocked(gobj);
Dave Airlief64122c2013-02-25 14:47:55 +1000337 drm_framebuffer_cleanup(fb);
338 kfree(fb);
339 }
Daniel Vetter068149a2016-03-30 11:40:42 +0200340 drm_gem_object_unreference_unlocked(gobj);
Dave Airlief64122c2013-02-25 14:47:55 +1000341 return ret;
342}
343
344static int qxl_fb_find_or_create_single(
345 struct drm_fb_helper *helper,
346 struct drm_fb_helper_surface_size *sizes)
347{
Fabian Frederickf38e34a2014-09-14 18:40:19 +0200348 struct qxl_fbdev *qfbdev =
349 container_of(helper, struct qxl_fbdev, helper);
Dave Airlief64122c2013-02-25 14:47:55 +1000350 int new_fb = 0;
351 int ret;
352
353 if (!helper->fb) {
354 ret = qxlfb_create(qfbdev, sizes);
355 if (ret)
356 return ret;
357 new_fb = 1;
358 }
359 return new_fb;
360}
361
362static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
363{
Dave Airlief64122c2013-02-25 14:47:55 +1000364 struct qxl_framebuffer *qfb = &qfbdev->qfb;
365
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530366 drm_fb_helper_unregister_fbi(&qfbdev->helper);
367 drm_fb_helper_release_fbi(&qfbdev->helper);
Dave Airlief64122c2013-02-25 14:47:55 +1000368
Dave Airlief64122c2013-02-25 14:47:55 +1000369 if (qfb->obj) {
370 qxlfb_destroy_pinned_object(qfb->obj);
371 qfb->obj = NULL;
372 }
373 drm_fb_helper_fini(&qfbdev->helper);
374 vfree(qfbdev->shadow);
375 drm_framebuffer_cleanup(&qfb->base);
376
377 return 0;
378}
379
Thierry Reding3a493872014-06-27 17:19:23 +0200380static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
Dave Airlief64122c2013-02-25 14:47:55 +1000381 .fb_probe = qxl_fb_find_or_create_single,
382};
383
384int qxl_fbdev_init(struct qxl_device *qdev)
385{
386 struct qxl_fbdev *qfbdev;
387 int bpp_sel = 32; /* TODO: parameter from somewhere? */
388 int ret;
389
390 qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
391 if (!qfbdev)
392 return -ENOMEM;
393
394 qfbdev->qdev = qdev;
395 qdev->mode_info.qfbdev = qfbdev;
Dave Airlie0665f9f2013-07-22 14:37:39 +1000396 spin_lock_init(&qfbdev->delayed_ops_lock);
397 INIT_LIST_HEAD(&qfbdev->delayed_ops);
Thierry Reding10a23102014-06-27 17:19:24 +0200398
399 drm_fb_helper_prepare(qdev->ddev, &qfbdev->helper,
400 &qxl_fb_helper_funcs);
401
Dave Airlief64122c2013-02-25 14:47:55 +1000402 ret = drm_fb_helper_init(qdev->ddev, &qfbdev->helper,
Dave Airlie07f8d9b2013-07-02 06:37:13 +0100403 qxl_num_crtc /* num_crtc - QXL supports just 1 */,
Dave Airlief64122c2013-02-25 14:47:55 +1000404 QXLFB_CONN_LIMIT);
Thierry Reding01934c22014-12-19 11:21:32 +0100405 if (ret)
406 goto free;
Dave Airlief64122c2013-02-25 14:47:55 +1000407
Thierry Reding01934c22014-12-19 11:21:32 +0100408 ret = drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
409 if (ret)
410 goto fini;
411
412 ret = drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
413 if (ret)
414 goto fini;
415
Dave Airlief64122c2013-02-25 14:47:55 +1000416 return 0;
Thierry Reding01934c22014-12-19 11:21:32 +0100417
418fini:
419 drm_fb_helper_fini(&qfbdev->helper);
420free:
421 kfree(qfbdev);
422 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +1000423}
424
425void qxl_fbdev_fini(struct qxl_device *qdev)
426{
427 if (!qdev->mode_info.qfbdev)
428 return;
429
430 qxl_fbdev_destroy(qdev->ddev, qdev->mode_info.qfbdev);
431 kfree(qdev->mode_info.qfbdev);
432 qdev->mode_info.qfbdev = NULL;
433}
434
Dave Airlieb86487a2013-07-04 14:59:34 +1000435void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
436{
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530437 drm_fb_helper_set_suspend(&qdev->mode_info.qfbdev->helper, state);
Dave Airlieb86487a2013-07-04 14:59:34 +1000438}
Dave Airlief64122c2013-02-25 14:47:55 +1000439
Dave Airlieb86487a2013-07-04 14:59:34 +1000440bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
441{
442 if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))
443 return true;
444 return false;
445}