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