blob: 573e7e9a5f986e6af4487f4696b8eef92ad5206e [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
Masahiro Yamadaedaf4922017-04-24 13:50:30 +090028#include <drm/drmP.h>
29#include <drm/drm.h>
30#include <drm/drm_crtc.h>
31#include <drm/drm_crtc_helper.h>
32#include <drm/drm_fb_helper.h>
33
Dave Airlief64122c2013-02-25 14:47:55 +100034#include "qxl_drv.h"
35
36#include "qxl_object.h"
Dave Airlief64122c2013-02-25 14:47:55 +100037
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
Daniel Vetter0c756132016-08-10 18:52:38 +020076#ifdef CONFIG_DRM_FBDEV_EMULATION
Dave Airlie6d01f1f2013-04-16 13:24:25 +100077static struct fb_deferred_io qxl_defio = {
Dave Airlief64122c2013-02-25 14:47:55 +100078 .delay = QXL_DIRTY_DELAY,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +020079 .deferred_io = drm_fb_helper_deferred_io,
Dave Airlief64122c2013-02-25 14:47:55 +100080};
Daniel Vetter0c756132016-08-10 18:52:38 +020081#endif
Dave Airlief64122c2013-02-25 14:47:55 +100082
Dave Airlief64122c2013-02-25 14:47:55 +100083static struct fb_ops qxlfb_ops = {
84 .owner = THIS_MODULE,
Stefan Christ1102af12016-11-14 00:03:19 +010085 DRM_FB_HELPER_DEFAULT_OPS,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +020086 .fb_fillrect = drm_fb_helper_sys_fillrect,
87 .fb_copyarea = drm_fb_helper_sys_copyarea,
88 .fb_imageblit = drm_fb_helper_sys_imageblit,
Dave Airlief64122c2013-02-25 14:47:55 +100089};
90
91static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
92{
93 struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
Dave Airlief64122c2013-02-25 14:47:55 +100094
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -030095 qxl_bo_kunmap(qbo);
96 qxl_bo_unpin(qbo);
97
Dave Airlief64122c2013-02-25 14:47:55 +100098 drm_gem_object_unreference_unlocked(gobj);
99}
100
101int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
102 struct drm_file *file_priv,
103 uint32_t *handle)
104{
105 int r;
106 struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
107
108 BUG_ON(!gobj);
109 /* drm_get_handle_create adds a reference - good */
110 r = drm_gem_handle_create(file_priv, gobj, handle);
111 if (r)
112 return r;
113 return 0;
114}
115
116static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200117 const struct drm_mode_fb_cmd2 *mode_cmd,
Dave Airlief64122c2013-02-25 14:47:55 +1000118 struct drm_gem_object **gobj_p)
119{
120 struct qxl_device *qdev = qfbdev->qdev;
121 struct drm_gem_object *gobj = NULL;
122 struct qxl_bo *qbo = NULL;
123 int ret;
124 int aligned_size, size;
125 int height = mode_cmd->height;
Dave Airlief64122c2013-02-25 14:47:55 +1000126
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) */
135 NULL,
136 &gobj);
137 if (ret) {
138 pr_err("failed to allocate framebuffer (%d)\n",
139 aligned_size);
140 return -ENOMEM;
141 }
142 qbo = gem_to_qxl_bo(gobj);
143
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;
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300148
Dave Airlief64122c2013-02-25 14:47:55 +1000149 ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
150 if (ret) {
Dave Airlief64122c2013-02-25 14:47:55 +1000151 goto out_unref;
152 }
153 ret = qxl_bo_kmap(qbo, NULL);
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300154
Dave Airlief64122c2013-02-25 14:47:55 +1000155 if (ret)
156 goto out_unref;
157
158 *gobj_p = gobj;
159 return 0;
160out_unref:
161 qxlfb_destroy_pinned_object(gobj);
162 *gobj_p = NULL;
163 return ret;
164}
165
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200166/*
167 * FIXME
168 * It should not be necessary to have a special dirty() callback for fbdev.
169 */
170static 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,
174 unsigned num_clips)
175{
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;
181
182 /* TODO: hard coding 32 bpp */
183 int stride = qfbdev->qfb.base.pitches[0];
184
185 /*
186 * we are using a shadow draw buffer, at qdev->surface0_shadow
187 */
Christophe Fergeau00d7d642016-11-08 10:12:05 +0100188 qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]\n", clips->x1, clips->x2,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200189 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
195 warnings */
196 image->bg_color = 0;
197 image->depth = 32; /* TODO: take from somewhere? */
198 image->cmap.start = 0;
199 image->cmap.len = 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);
205
206 qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
207 qxl_draw_opaque_fb(&qxl_fb_image, stride);
208
209 return 0;
210}
211
212static const struct drm_framebuffer_funcs qxlfb_fb_funcs = {
213 .destroy = qxl_user_framebuffer_destroy,
214 .dirty = qxlfb_framebuffer_dirty,
215};
216
Dave Airlief64122c2013-02-25 14:47:55 +1000217static int qxlfb_create(struct qxl_fbdev *qfbdev,
218 struct drm_fb_helper_surface_size *sizes)
219{
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;
Dave Airlief64122c2013-02-25 14:47:55 +1000226 int ret;
227 int size;
228 int bpp = sizes->surface_bpp;
229 int depth = sizes->surface_depth;
230 void *shadow;
231
232 mode_cmd.width = sizes->surface_width;
233 mode_cmd.height = sizes->surface_height;
234
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);
237
238 ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
Gerd Hoffmann2d6b1d42016-05-12 19:06:56 +0200239 if (ret < 0)
240 return ret;
241
Dave Airlief64122c2013-02-25 14:47:55 +1000242 qbo = gem_to_qxl_bo(gobj);
243 QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
244 mode_cmd.height, mode_cmd.pitches[0]);
245
246 shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
247 /* TODO: what's the usual response to memory allocation errors? */
248 BUG_ON(!shadow);
249 QXL_INFO(qdev,
250 "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
251 qxl_bo_gpu_offset(qbo),
252 qxl_bo_mmap_offset(qbo),
253 qbo->kptr,
254 shadow);
255 size = mode_cmd.pitches[0] * mode_cmd.height;
256
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530257 info = drm_fb_helper_alloc_fbi(&qfbdev->helper);
258 if (IS_ERR(info)) {
259 ret = PTR_ERR(info);
Dave Airlief64122c2013-02-25 14:47:55 +1000260 goto out_unref;
261 }
262
263 info->par = qfbdev;
264
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200265 qxl_framebuffer_init(&qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj,
Noralf Trønnes6819c3c2016-04-28 17:18:36 +0200266 &qxlfb_fb_funcs);
Dave Airlief64122c2013-02-25 14:47:55 +1000267
268 fb = &qfbdev->qfb.base;
269
270 /* setup helper with fb data */
271 qfbdev->helper.fb = fb;
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530272
Dave Airlief64122c2013-02-25 14:47:55 +1000273 qfbdev->shadow = shadow;
274 strcpy(info->fix.id, "qxldrmfb");
275
Ville Syrjäläb00c6002016-12-14 23:31:35 +0200276 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
Dave Airlief64122c2013-02-25 14:47:55 +1000277
278 info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
279 info->fbops = &qxlfb_ops;
280
281 /*
282 * TODO: using gobj->size in various places in this function. Not sure
283 * what the difference between the different sizes is.
284 */
285 info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
286 info->fix.smem_len = gobj->size;
287 info->screen_base = qfbdev->shadow;
288 info->screen_size = gobj->size;
289
290 drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
291 sizes->fb_height);
292
293 /* setup aperture base/size for vesafb takeover */
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200294 info->apertures->ranges[0].base = qdev->ddev.mode_config.fb_base;
Dave Airlief64122c2013-02-25 14:47:55 +1000295 info->apertures->ranges[0].size = qdev->vram_size;
296
297 info->fix.mmio_start = 0;
298 info->fix.mmio_len = 0;
299
300 if (info->screen_base == NULL) {
301 ret = -ENOSPC;
Daniel Vetterda7bdda2017-02-07 17:16:03 +0100302 goto out_unref;
Dave Airlief64122c2013-02-25 14:47:55 +1000303 }
304
Daniel Vetter0c756132016-08-10 18:52:38 +0200305#ifdef CONFIG_DRM_FBDEV_EMULATION
Dave Airlief64122c2013-02-25 14:47:55 +1000306 info->fbdefio = &qxl_defio;
307 fb_deferred_io_init(info);
Daniel Vetter0c756132016-08-10 18:52:38 +0200308#endif
Dave Airlief64122c2013-02-25 14:47:55 +1000309
310 qdev->fbdev_info = info;
311 qdev->fbdev_qfb = &qfbdev->qfb;
312 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 +0200313 DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n",
314 fb->format->depth, fb->pitches[0], fb->width, fb->height);
Dave Airlief64122c2013-02-25 14:47:55 +1000315 return 0;
316
317out_unref:
318 if (qbo) {
Gabriel Krisman Bertazi715a11f2017-02-27 17:43:16 -0300319 qxl_bo_kunmap(qbo);
320 qxl_bo_unpin(qbo);
Dave Airlief64122c2013-02-25 14:47:55 +1000321 }
322 if (fb && ret) {
Daniel Vetter068149a2016-03-30 11:40:42 +0200323 drm_gem_object_unreference_unlocked(gobj);
Dave Airlief64122c2013-02-25 14:47:55 +1000324 drm_framebuffer_cleanup(fb);
325 kfree(fb);
326 }
Daniel Vetter068149a2016-03-30 11:40:42 +0200327 drm_gem_object_unreference_unlocked(gobj);
Dave Airlief64122c2013-02-25 14:47:55 +1000328 return ret;
329}
330
331static int qxl_fb_find_or_create_single(
332 struct drm_fb_helper *helper,
333 struct drm_fb_helper_surface_size *sizes)
334{
Fabian Frederickf38e34a2014-09-14 18:40:19 +0200335 struct qxl_fbdev *qfbdev =
336 container_of(helper, struct qxl_fbdev, helper);
Dave Airlief64122c2013-02-25 14:47:55 +1000337 int new_fb = 0;
338 int ret;
339
340 if (!helper->fb) {
341 ret = qxlfb_create(qfbdev, sizes);
342 if (ret)
343 return ret;
344 new_fb = 1;
345 }
346 return new_fb;
347}
348
349static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
350{
Dave Airlief64122c2013-02-25 14:47:55 +1000351 struct qxl_framebuffer *qfb = &qfbdev->qfb;
352
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530353 drm_fb_helper_unregister_fbi(&qfbdev->helper);
Dave Airlief64122c2013-02-25 14:47:55 +1000354
Dave Airlief64122c2013-02-25 14:47:55 +1000355 if (qfb->obj) {
356 qxlfb_destroy_pinned_object(qfb->obj);
357 qfb->obj = NULL;
358 }
359 drm_fb_helper_fini(&qfbdev->helper);
360 vfree(qfbdev->shadow);
361 drm_framebuffer_cleanup(&qfb->base);
362
363 return 0;
364}
365
Thierry Reding3a493872014-06-27 17:19:23 +0200366static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
Dave Airlief64122c2013-02-25 14:47:55 +1000367 .fb_probe = qxl_fb_find_or_create_single,
368};
369
370int qxl_fbdev_init(struct qxl_device *qdev)
371{
Gabriel Krisman Bertazi86107832017-02-27 17:33:30 -0300372 int ret = 0;
373
374#ifdef CONFIG_DRM_FBDEV_EMULATION
Dave Airlief64122c2013-02-25 14:47:55 +1000375 struct qxl_fbdev *qfbdev;
376 int bpp_sel = 32; /* TODO: parameter from somewhere? */
Dave Airlief64122c2013-02-25 14:47:55 +1000377
378 qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
379 if (!qfbdev)
380 return -ENOMEM;
381
382 qfbdev->qdev = qdev;
383 qdev->mode_info.qfbdev = qfbdev;
Dave Airlie0665f9f2013-07-22 14:37:39 +1000384 spin_lock_init(&qfbdev->delayed_ops_lock);
385 INIT_LIST_HEAD(&qfbdev->delayed_ops);
Thierry Reding10a23102014-06-27 17:19:24 +0200386
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200387 drm_fb_helper_prepare(&qdev->ddev, &qfbdev->helper,
Thierry Reding10a23102014-06-27 17:19:24 +0200388 &qxl_fb_helper_funcs);
389
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200390 ret = drm_fb_helper_init(&qdev->ddev, &qfbdev->helper,
Dave Airlief64122c2013-02-25 14:47:55 +1000391 QXLFB_CONN_LIMIT);
Thierry Reding01934c22014-12-19 11:21:32 +0100392 if (ret)
393 goto free;
Dave Airlief64122c2013-02-25 14:47:55 +1000394
Thierry Reding01934c22014-12-19 11:21:32 +0100395 ret = drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
396 if (ret)
397 goto fini;
398
399 ret = drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
400 if (ret)
401 goto fini;
402
Dave Airlief64122c2013-02-25 14:47:55 +1000403 return 0;
Thierry Reding01934c22014-12-19 11:21:32 +0100404
405fini:
406 drm_fb_helper_fini(&qfbdev->helper);
407free:
408 kfree(qfbdev);
Gabriel Krisman Bertazi86107832017-02-27 17:33:30 -0300409#endif
410
Thierry Reding01934c22014-12-19 11:21:32 +0100411 return ret;
Dave Airlief64122c2013-02-25 14:47:55 +1000412}
413
414void qxl_fbdev_fini(struct qxl_device *qdev)
415{
416 if (!qdev->mode_info.qfbdev)
417 return;
418
Gabriel Krisman Bertazicbdded72017-01-26 23:05:48 -0200419 qxl_fbdev_destroy(&qdev->ddev, qdev->mode_info.qfbdev);
Dave Airlief64122c2013-02-25 14:47:55 +1000420 kfree(qdev->mode_info.qfbdev);
421 qdev->mode_info.qfbdev = NULL;
422}
423
Dave Airlieb86487a2013-07-04 14:59:34 +1000424void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
425{
Gabriel Krisman Bertazi86107832017-02-27 17:33:30 -0300426 if (!qdev->mode_info.qfbdev)
427 return;
428
Archit Tanejae7cd84c2015-07-22 14:58:13 +0530429 drm_fb_helper_set_suspend(&qdev->mode_info.qfbdev->helper, state);
Dave Airlieb86487a2013-07-04 14:59:34 +1000430}
Dave Airlief64122c2013-02-25 14:47:55 +1000431
Dave Airlieb86487a2013-07-04 14:59:34 +1000432bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
433{
434 if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))
435 return true;
436 return false;
437}