Merge tag 'wireless-drivers-next-for-davem-2016-09-15' of git://git.kernel.org/pub...
[cascardo/linux.git] / drivers / gpu / drm / qxl / qxl_fb.c
1 /*
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
40 struct qxl_fbdev {
41         struct drm_fb_helper helper;
42         struct qxl_framebuffer  qfb;
43         struct qxl_device       *qdev;
44
45         spinlock_t delayed_ops_lock;
46         struct list_head delayed_ops;
47         void *shadow;
48         int size;
49 };
50
51 static 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
76 #ifdef CONFIG_DRM_FBDEV_EMULATION
77 static struct fb_deferred_io qxl_defio = {
78         .delay          = QXL_DIRTY_DELAY,
79         .deferred_io    = drm_fb_helper_deferred_io,
80 };
81 #endif
82
83 static struct fb_ops qxlfb_ops = {
84         .owner = THIS_MODULE,
85         .fb_check_var = drm_fb_helper_check_var,
86         .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
87         .fb_fillrect = drm_fb_helper_sys_fillrect,
88         .fb_copyarea = drm_fb_helper_sys_copyarea,
89         .fb_imageblit = drm_fb_helper_sys_imageblit,
90         .fb_pan_display = drm_fb_helper_pan_display,
91         .fb_blank = drm_fb_helper_blank,
92         .fb_setcmap = drm_fb_helper_setcmap,
93         .fb_debug_enter = drm_fb_helper_debug_enter,
94         .fb_debug_leave = drm_fb_helper_debug_leave,
95 };
96
97 static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
98 {
99         struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
100         int ret;
101
102         ret = qxl_bo_reserve(qbo, false);
103         if (likely(ret == 0)) {
104                 qxl_bo_kunmap(qbo);
105                 qxl_bo_unpin(qbo);
106                 qxl_bo_unreserve(qbo);
107         }
108         drm_gem_object_unreference_unlocked(gobj);
109 }
110
111 int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
112                                   struct drm_file *file_priv,
113                                   uint32_t *handle)
114 {
115         int r;
116         struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
117
118         BUG_ON(!gobj);
119         /* drm_get_handle_create adds a reference - good */
120         r = drm_gem_handle_create(file_priv, gobj, handle);
121         if (r)
122                 return r;
123         return 0;
124 }
125
126 static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
127                                       const struct drm_mode_fb_cmd2 *mode_cmd,
128                                       struct drm_gem_object **gobj_p)
129 {
130         struct qxl_device *qdev = qfbdev->qdev;
131         struct drm_gem_object *gobj = NULL;
132         struct qxl_bo *qbo = NULL;
133         int ret;
134         int aligned_size, size;
135         int height = mode_cmd->height;
136
137         size = mode_cmd->pitches[0] * height;
138         aligned_size = ALIGN(size, PAGE_SIZE);
139         /* TODO: unallocate and reallocate surface0 for real. Hack to just
140          * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
141         ret = qxl_gem_object_create(qdev, aligned_size, 0,
142                                     QXL_GEM_DOMAIN_SURFACE,
143                                     false, /* is discardable */
144                                     false, /* is kernel (false means device) */
145                                     NULL,
146                                     &gobj);
147         if (ret) {
148                 pr_err("failed to allocate framebuffer (%d)\n",
149                        aligned_size);
150                 return -ENOMEM;
151         }
152         qbo = gem_to_qxl_bo(gobj);
153
154         qbo->surf.width = mode_cmd->width;
155         qbo->surf.height = mode_cmd->height;
156         qbo->surf.stride = mode_cmd->pitches[0];
157         qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
158         ret = qxl_bo_reserve(qbo, false);
159         if (unlikely(ret != 0))
160                 goto out_unref;
161         ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
162         if (ret) {
163                 qxl_bo_unreserve(qbo);
164                 goto out_unref;
165         }
166         ret = qxl_bo_kmap(qbo, NULL);
167         qxl_bo_unreserve(qbo); /* unreserve, will be mmaped */
168         if (ret)
169                 goto out_unref;
170
171         *gobj_p = gobj;
172         return 0;
173 out_unref:
174         qxlfb_destroy_pinned_object(gobj);
175         *gobj_p = NULL;
176         return ret;
177 }
178
179 /*
180  * FIXME
181  * It should not be necessary to have a special dirty() callback for fbdev.
182  */
183 static int qxlfb_framebuffer_dirty(struct drm_framebuffer *fb,
184                                    struct drm_file *file_priv,
185                                    unsigned flags, unsigned color,
186                                    struct drm_clip_rect *clips,
187                                    unsigned num_clips)
188 {
189         struct qxl_device *qdev = fb->dev->dev_private;
190         struct fb_info *info = qdev->fbdev_info;
191         struct qxl_fbdev *qfbdev = info->par;
192         struct qxl_fb_image qxl_fb_image;
193         struct fb_image *image = &qxl_fb_image.fb_image;
194
195         /* TODO: hard coding 32 bpp */
196         int stride = qfbdev->qfb.base.pitches[0];
197
198         /*
199          * we are using a shadow draw buffer, at qdev->surface0_shadow
200          */
201         qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]", clips->x1, clips->x2,
202                    clips->y1, clips->y2);
203         image->dx = clips->x1;
204         image->dy = clips->y1;
205         image->width = clips->x2 - clips->x1;
206         image->height = clips->y2 - clips->y1;
207         image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
208                                          warnings */
209         image->bg_color = 0;
210         image->depth = 32;           /* TODO: take from somewhere? */
211         image->cmap.start = 0;
212         image->cmap.len = 0;
213         image->cmap.red = NULL;
214         image->cmap.green = NULL;
215         image->cmap.blue = NULL;
216         image->cmap.transp = NULL;
217         image->data = qfbdev->shadow + (clips->x1 * 4) + (stride * clips->y1);
218
219         qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
220         qxl_draw_opaque_fb(&qxl_fb_image, stride);
221
222         return 0;
223 }
224
225 static const struct drm_framebuffer_funcs qxlfb_fb_funcs = {
226         .destroy = qxl_user_framebuffer_destroy,
227         .dirty = qxlfb_framebuffer_dirty,
228 };
229
230 static int qxlfb_create(struct qxl_fbdev *qfbdev,
231                         struct drm_fb_helper_surface_size *sizes)
232 {
233         struct qxl_device *qdev = qfbdev->qdev;
234         struct fb_info *info;
235         struct drm_framebuffer *fb = NULL;
236         struct drm_mode_fb_cmd2 mode_cmd;
237         struct drm_gem_object *gobj = NULL;
238         struct qxl_bo *qbo = NULL;
239         int ret;
240         int size;
241         int bpp = sizes->surface_bpp;
242         int depth = sizes->surface_depth;
243         void *shadow;
244
245         mode_cmd.width = sizes->surface_width;
246         mode_cmd.height = sizes->surface_height;
247
248         mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
249         mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
250
251         ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
252         if (ret < 0)
253                 return ret;
254
255         qbo = gem_to_qxl_bo(gobj);
256         QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
257                  mode_cmd.height, mode_cmd.pitches[0]);
258
259         shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
260         /* TODO: what's the usual response to memory allocation errors? */
261         BUG_ON(!shadow);
262         QXL_INFO(qdev,
263         "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
264                  qxl_bo_gpu_offset(qbo),
265                  qxl_bo_mmap_offset(qbo),
266                  qbo->kptr,
267                  shadow);
268         size = mode_cmd.pitches[0] * mode_cmd.height;
269
270         info = drm_fb_helper_alloc_fbi(&qfbdev->helper);
271         if (IS_ERR(info)) {
272                 ret = PTR_ERR(info);
273                 goto out_unref;
274         }
275
276         info->par = qfbdev;
277
278         qxl_framebuffer_init(qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj,
279                              &qxlfb_fb_funcs);
280
281         fb = &qfbdev->qfb.base;
282
283         /* setup helper with fb data */
284         qfbdev->helper.fb = fb;
285
286         qfbdev->shadow = shadow;
287         strcpy(info->fix.id, "qxldrmfb");
288
289         drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
290
291         info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
292         info->fbops = &qxlfb_ops;
293
294         /*
295          * TODO: using gobj->size in various places in this function. Not sure
296          * what the difference between the different sizes is.
297          */
298         info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
299         info->fix.smem_len = gobj->size;
300         info->screen_base = qfbdev->shadow;
301         info->screen_size = gobj->size;
302
303         drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
304                                sizes->fb_height);
305
306         /* setup aperture base/size for vesafb takeover */
307         info->apertures->ranges[0].base = qdev->ddev->mode_config.fb_base;
308         info->apertures->ranges[0].size = qdev->vram_size;
309
310         info->fix.mmio_start = 0;
311         info->fix.mmio_len = 0;
312
313         if (info->screen_base == NULL) {
314                 ret = -ENOSPC;
315                 goto out_destroy_fbi;
316         }
317
318 #ifdef CONFIG_DRM_FBDEV_EMULATION
319         info->fbdefio = &qxl_defio;
320         fb_deferred_io_init(info);
321 #endif
322
323         qdev->fbdev_info = info;
324         qdev->fbdev_qfb = &qfbdev->qfb;
325         DRM_INFO("fb mappable at 0x%lX, size %lu\n",  info->fix.smem_start, (unsigned long)info->screen_size);
326         DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n", fb->depth, fb->pitches[0], fb->width, fb->height);
327         return 0;
328
329 out_destroy_fbi:
330         drm_fb_helper_release_fbi(&qfbdev->helper);
331 out_unref:
332         if (qbo) {
333                 ret = qxl_bo_reserve(qbo, false);
334                 if (likely(ret == 0)) {
335                         qxl_bo_kunmap(qbo);
336                         qxl_bo_unpin(qbo);
337                         qxl_bo_unreserve(qbo);
338                 }
339         }
340         if (fb && ret) {
341                 drm_gem_object_unreference_unlocked(gobj);
342                 drm_framebuffer_cleanup(fb);
343                 kfree(fb);
344         }
345         drm_gem_object_unreference_unlocked(gobj);
346         return ret;
347 }
348
349 static int qxl_fb_find_or_create_single(
350                 struct drm_fb_helper *helper,
351                 struct drm_fb_helper_surface_size *sizes)
352 {
353         struct qxl_fbdev *qfbdev =
354                 container_of(helper, struct qxl_fbdev, helper);
355         int new_fb = 0;
356         int ret;
357
358         if (!helper->fb) {
359                 ret = qxlfb_create(qfbdev, sizes);
360                 if (ret)
361                         return ret;
362                 new_fb = 1;
363         }
364         return new_fb;
365 }
366
367 static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
368 {
369         struct qxl_framebuffer *qfb = &qfbdev->qfb;
370
371         drm_fb_helper_unregister_fbi(&qfbdev->helper);
372         drm_fb_helper_release_fbi(&qfbdev->helper);
373
374         if (qfb->obj) {
375                 qxlfb_destroy_pinned_object(qfb->obj);
376                 qfb->obj = NULL;
377         }
378         drm_fb_helper_fini(&qfbdev->helper);
379         vfree(qfbdev->shadow);
380         drm_framebuffer_cleanup(&qfb->base);
381
382         return 0;
383 }
384
385 static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
386         .fb_probe = qxl_fb_find_or_create_single,
387 };
388
389 int qxl_fbdev_init(struct qxl_device *qdev)
390 {
391         struct qxl_fbdev *qfbdev;
392         int bpp_sel = 32; /* TODO: parameter from somewhere? */
393         int ret;
394
395         qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
396         if (!qfbdev)
397                 return -ENOMEM;
398
399         qfbdev->qdev = qdev;
400         qdev->mode_info.qfbdev = qfbdev;
401         spin_lock_init(&qfbdev->delayed_ops_lock);
402         INIT_LIST_HEAD(&qfbdev->delayed_ops);
403
404         drm_fb_helper_prepare(qdev->ddev, &qfbdev->helper,
405                               &qxl_fb_helper_funcs);
406
407         ret = drm_fb_helper_init(qdev->ddev, &qfbdev->helper,
408                                  qxl_num_crtc /* num_crtc - QXL supports just 1 */,
409                                  QXLFB_CONN_LIMIT);
410         if (ret)
411                 goto free;
412
413         ret = drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
414         if (ret)
415                 goto fini;
416
417         ret = drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
418         if (ret)
419                 goto fini;
420
421         return 0;
422
423 fini:
424         drm_fb_helper_fini(&qfbdev->helper);
425 free:
426         kfree(qfbdev);
427         return ret;
428 }
429
430 void qxl_fbdev_fini(struct qxl_device *qdev)
431 {
432         if (!qdev->mode_info.qfbdev)
433                 return;
434
435         qxl_fbdev_destroy(qdev->ddev, qdev->mode_info.qfbdev);
436         kfree(qdev->mode_info.qfbdev);
437         qdev->mode_info.qfbdev = NULL;
438 }
439
440 void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
441 {
442         drm_fb_helper_set_suspend(&qdev->mode_info.qfbdev->helper, state);
443 }
444
445 bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
446 {
447         if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))
448                 return true;
449         return false;
450 }