blob: 35124737666ef284863a9fbd4ccd06761b906f55 [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
Daniel Vetter0c756132016-08-10 18:52:38 +020075#ifdef CONFIG_DRM_FBDEV_EMULATION
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};
Daniel Vetter0c756132016-08-10 18:52:38 +020080#endif
Dave Airlief64122c2013-02-25 14:47:55 +100081
Dave Airlief64122c2013-02-25 14:47:55 +100082static struct fb_ops qxlfb_ops = {
83 .owner = THIS_MODULE,
Stefan Christ1102af12016-11-14 00:03:19 +010084 DRM_FB_HELPER_DEFAULT_OPS,
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};
89
90static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
91{
92 struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
Dave Airlief64122c2013-02-25 14:47:55 +100093
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -030094 qxl_bo_kunmap(qbo);
95 qxl_bo_unpin(qbo);
96
Dave Airlief64122c2013-02-25 14:47:55 +100097 drm_gem_object_unreference_unlocked(gobj);
98}
99
100int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
101 struct drm_file *file_priv,
102 uint32_t *handle)
103{
104 int r;
105 struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
106
107 BUG_ON(!gobj);
108 /* drm_get_handle_create adds a reference - good */
109 r = drm_gem_handle_create(file_priv, gobj, handle);
110 if (r)
111 return r;
112 return 0;
113}
114
115static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200116 const struct drm_mode_fb_cmd2 *mode_cmd,
Dave Airlief64122c2013-02-25 14:47:55 +1000117 struct drm_gem_object **gobj_p)
118{
119 struct qxl_device *qdev = qfbdev->qdev;
120 struct drm_gem_object *gobj = NULL;
121 struct qxl_bo *qbo = NULL;
122 int ret;
123 int aligned_size, size;
124 int height = mode_cmd->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000125
126 size = mode_cmd->pitches[0] * height;
127 aligned_size = ALIGN(size, PAGE_SIZE);
128 /* TODO: unallocate and reallocate surface0 for real. Hack to just
129 * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
130 ret = qxl_gem_object_create(qdev, aligned_size, 0,
131 QXL_GEM_DOMAIN_SURFACE,
132 false, /* is discardable */
133 false, /* is kernel (false means device) */
134 NULL,
135 &gobj);
136 if (ret) {
137 pr_err("failed to allocate framebuffer (%d)\n",
138 aligned_size);
139 return -ENOMEM;
140 }
141 qbo = gem_to_qxl_bo(gobj);
142
143 qbo->surf.width = mode_cmd->width;
144 qbo->surf.height = mode_cmd->height;
145 qbo->surf.stride = mode_cmd->pitches[0];
146 qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300147
Dave Airlief64122c2013-02-25 14:47:55 +1000148 ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
149 if (ret) {
Dave Airlief64122c2013-02-25 14:47:55 +1000150 goto out_unref;
151 }
152 ret = qxl_bo_kmap(qbo, NULL);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300153
Dave Airlief64122c2013-02-25 14:47:55 +1000154 if (ret)
155 goto out_unref;
156
157 *gobj_p = gobj;
158 return 0;
159out_unref:
160 qxlfb_destroy_pinned_object(gobj);
161 *gobj_p = NULL;
162 return ret;
163}
164
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200165/*
166 * FIXME
167 * It should not be necessary to have a special dirty() callback for fbdev.
168 */
169static int qxlfb_framebuffer_dirty(struct drm_framebuffer *fb,
170 struct drm_file *file_priv,
171 unsigned flags, unsigned color,
172 struct drm_clip_rect *clips,
173 unsigned num_clips)
174{
175 struct qxl_device *qdev = fb->dev->dev_private;
176 struct fb_info *info = qdev->fbdev_info;
177 struct qxl_fbdev *qfbdev = info->par;
178 struct qxl_fb_image qxl_fb_image;
179 struct fb_image *image = &qxl_fb_image.fb_image;
180
181 /* TODO: hard coding 32 bpp */
182 int stride = qfbdev->qfb.base.pitches[0];
183
184 /*
185 * we are using a shadow draw buffer, at qdev->surface0_shadow
186 */
Christophe Fergeau00d7d642016-11-08 10:12:05 +0100187 qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]\n", clips->x1, clips->x2,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200188 clips->y1, clips->y2);
189 image->dx = clips->x1;
190 image->dy = clips->y1;
191 image->width = clips->x2 - clips->x1;
192 image->height = clips->y2 - clips->y1;
193 image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
194 warnings */
195 image->bg_color = 0;
196 image->depth = 32; /* TODO: take from somewhere? */
197 image->cmap.start = 0;
198 image->cmap.len = 0;
199 image->cmap.red = NULL;
200 image->cmap.green = NULL;
201 image->cmap.blue = NULL;
202 image->cmap.transp = NULL;
203 image->data = qfbdev->shadow + (clips->x1 * 4) + (stride * clips->y1);
204
205 qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
206 qxl_draw_opaque_fb(&qxl_fb_image, stride);
207
208 return 0;
209}
210
211static const struct drm_framebuffer_funcs qxlfb_fb_funcs = {
212 .destroy = qxl_user_framebuffer_destroy,
213 .dirty = qxlfb_framebuffer_dirty,
214};
215
Dave Airlief64122c2013-02-25 14:47:55 +1000216static int qxlfb_create(struct qxl_fbdev *qfbdev,
217 struct drm_fb_helper_surface_size *sizes)
218{
219 struct qxl_device *qdev = qfbdev->qdev;
220 struct fb_info *info;
221 struct drm_framebuffer *fb = NULL;
222 struct drm_mode_fb_cmd2 mode_cmd;
223 struct drm_gem_object *gobj = NULL;
224 struct qxl_bo *qbo = NULL;
Dave Airlief64122c2013-02-25 14:47:55 +1000225 int ret;
226 int size;
227 int bpp = sizes->surface_bpp;
228 int depth = sizes->surface_depth;
229 void *shadow;
230
231 mode_cmd.width = sizes->surface_width;
232 mode_cmd.height = sizes->surface_height;
233
234 mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
235 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
236
237 ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
Gerd Hoffmann2d6b1d42016-05-12 19:06:56 +0200238 if (ret < 0)
239 return ret;
240
Dave Airlief64122c2013-02-25 14:47:55 +1000241 qbo = gem_to_qxl_bo(gobj);
242 QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
243 mode_cmd.height, mode_cmd.pitches[0]);
244
245 shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
246 /* TODO: what's the usual response to memory allocation errors? */
247 BUG_ON(!shadow);
248 QXL_INFO(qdev,
249 "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
250 qxl_bo_gpu_offset(qbo),
251 qxl_bo_mmap_offset(qbo),
252 qbo->kptr,
253 shadow);
254 size = mode_cmd.pitches[0] * mode_cmd.height;
255
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530256 info = drm_fb_helper_alloc_fbi(&qfbdev->helper);
257 if (IS_ERR(info)) {
258 ret = PTR_ERR(info);
Dave Airlief64122c2013-02-25 14:47:55 +1000259 goto out_unref;
260 }
261
262 info->par = qfbdev;
263
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200264 qxl_framebuffer_init(&qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200265 &qxlfb_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000266
267 fb = &qfbdev->qfb.base;
268
269 /* setup helper with fb data */
270 qfbdev->helper.fb = fb;
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530271
Dave Airlief64122c2013-02-25 14:47:55 +1000272 qfbdev->shadow = shadow;
273 strcpy(info->fix.id, "qxldrmfb");
274
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200275 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
Dave Airlief64122c2013-02-25 14:47:55 +1000276
277 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
278 info->fbops = &qxlfb_ops;
279
280 /*
281 * TODO: using gobj->size in various places in this function. Not sure
282 * what the difference between the different sizes is.
283 */
284 info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
285 info->fix.smem_len = gobj->size;
286 info->screen_base = qfbdev->shadow;
287 info->screen_size = gobj->size;
288
289 drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
290 sizes->fb_height);
291
292 /* setup aperture base/size for vesafb takeover */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200293 info->apertures->ranges[0].base = qdev->ddev.mode_config.fb_base;
Dave Airlief64122c2013-02-25 14:47:55 +1000294 info->apertures->ranges[0].size = qdev->vram_size;
295
296 info->fix.mmio_start = 0;
297 info->fix.mmio_len = 0;
298
299 if (info->screen_base == NULL) {
300 ret = -ENOSPC;
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100301 goto out_unref;
Dave Airlief64122c2013-02-25 14:47:55 +1000302 }
303
Daniel Vetter0c756132016-08-10 18:52:38 +0200304#ifdef CONFIG_DRM_FBDEV_EMULATION
Dave Airlief64122c2013-02-25 14:47:55 +1000305 info->fbdefio = &qxl_defio;
306 fb_deferred_io_init(info);
Daniel Vetter0c756132016-08-10 18:52:38 +0200307#endif
Dave Airlief64122c2013-02-25 14:47:55 +1000308
309 qdev->fbdev_info = info;
310 qdev->fbdev_qfb = &qfbdev->qfb;
311 DRM_INFO("fb mappable at 0x%lX, size %lu\n", info->fix.smem_start, (unsigned long)info->screen_size);
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200312 DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n",
313 fb->format->depth, fb->pitches[0], fb->width, fb->height);
Dave Airlief64122c2013-02-25 14:47:55 +1000314 return 0;
315
316out_unref:
317 if (qbo) {
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300318 qxl_bo_kunmap(qbo);
319 qxl_bo_unpin(qbo);
Dave Airlief64122c2013-02-25 14:47:55 +1000320 }
321 if (fb && ret) {
Daniel Vetter068149a2016-03-30 11:40:42 +0200322 drm_gem_object_unreference_unlocked(gobj);
Dave Airlief64122c2013-02-25 14:47:55 +1000323 drm_framebuffer_cleanup(fb);
324 kfree(fb);
325 }
Daniel Vetter068149a2016-03-30 11:40:42 +0200326 drm_gem_object_unreference_unlocked(gobj);
Dave Airlief64122c2013-02-25 14:47:55 +1000327 return ret;
328}
329
330static int qxl_fb_find_or_create_single(
331 struct drm_fb_helper *helper,
332 struct drm_fb_helper_surface_size *sizes)
333{
Fabian Frederickf38e34a2014-09-14 18:40:19 +0200334 struct qxl_fbdev *qfbdev =
335 container_of(helper, struct qxl_fbdev, helper);
Dave Airlief64122c2013-02-25 14:47:55 +1000336 int new_fb = 0;
337 int ret;
338
339 if (!helper->fb) {
340 ret = qxlfb_create(qfbdev, sizes);
341 if (ret)
342 return ret;
343 new_fb = 1;
344 }
345 return new_fb;
346}
347
348static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
349{
Dave Airlief64122c2013-02-25 14:47:55 +1000350 struct qxl_framebuffer *qfb = &qfbdev->qfb;
351
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530352 drm_fb_helper_unregister_fbi(&qfbdev->helper);
Dave Airlief64122c2013-02-25 14:47:55 +1000353
Dave Airlief64122c2013-02-25 14:47:55 +1000354 if (qfb->obj) {
355 qxlfb_destroy_pinned_object(qfb->obj);
356 qfb->obj = NULL;
357 }
358 drm_fb_helper_fini(&qfbdev->helper);
359 vfree(qfbdev->shadow);
360 drm_framebuffer_cleanup(&qfb->base);
361
362 return 0;
363}
364
Thierry Reding3a493872014-06-27 17:19:23 +0200365static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
Dave Airlief64122c2013-02-25 14:47:55 +1000366 .fb_probe = qxl_fb_find_or_create_single,
367};
368
369int qxl_fbdev_init(struct qxl_device *qdev)
370{
371 struct qxl_fbdev *qfbdev;
372 int bpp_sel = 32; /* TODO: parameter from somewhere? */
373 int ret;
374
375 qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
376 if (!qfbdev)
377 return -ENOMEM;
378
379 qfbdev->qdev = qdev;
380 qdev->mode_info.qfbdev = qfbdev;
Dave Airlie0665f9f2013-07-22 14:37:39 +1000381 spin_lock_init(&qfbdev->delayed_ops_lock);
382 INIT_LIST_HEAD(&qfbdev->delayed_ops);
Thierry Reding10a23102014-06-27 17:19:24 +0200383
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200384 drm_fb_helper_prepare(&qdev->ddev, &qfbdev->helper,
Thierry Reding10a23102014-06-27 17:19:24 +0200385 &qxl_fb_helper_funcs);
386
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200387 ret = drm_fb_helper_init(&qdev->ddev, &qfbdev->helper,
Dave Airlief64122c2013-02-25 14:47:55 +1000388 QXLFB_CONN_LIMIT);
Thierry Reding01934c22014-12-19 11:21:32 +0100389 if (ret)
390 goto free;
Dave Airlief64122c2013-02-25 14:47:55 +1000391
Thierry Reding01934c22014-12-19 11:21:32 +0100392 ret = drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
393 if (ret)
394 goto fini;
395
396 ret = drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
397 if (ret)
398 goto fini;
399
Dave Airlief64122c2013-02-25 14:47:55 +1000400 return 0;
Thierry Reding01934c22014-12-19 11:21:32 +0100401
402fini:
403 drm_fb_helper_fini(&qfbdev->helper);
404free:
405 kfree(qfbdev);
406 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +1000407}
408
409void qxl_fbdev_fini(struct qxl_device *qdev)
410{
411 if (!qdev->mode_info.qfbdev)
412 return;
413
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200414 qxl_fbdev_destroy(&qdev->ddev, qdev->mode_info.qfbdev);
Dave Airlief64122c2013-02-25 14:47:55 +1000415 kfree(qdev->mode_info.qfbdev);
416 qdev->mode_info.qfbdev = NULL;
417}
418
Dave Airlieb86487a2013-07-04 14:59:34 +1000419void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
420{
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530421 drm_fb_helper_set_suspend(&qdev->mode_info.qfbdev->helper, state);
Dave Airlieb86487a2013-07-04 14:59:34 +1000422}
Dave Airlief64122c2013-02-25 14:47:55 +1000423
Dave Airlieb86487a2013-07-04 14:59:34 +1000424bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
425{
426 if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))
427 return true;
428 return false;
429}