blob: daecf1ad76a454845775a802c914384d53fa4c9e [file] [log] [blame]
Dave Airlief9aa76a2012-04-17 14:12:29 +01001/*
2 * Copyright 2012 Red Hat
3 *
4 * This file is subject to the terms and conditions of the GNU General
5 * Public License version 2. See the file COPYING in the main
6 * directory of this archive for more details.
7 *
8 * Authors: Matthew Garrett
9 * Dave Airlie
10 */
11#include <linux/module.h>
David Howells760285e2012-10-02 18:01:07 +010012#include <drm/drmP.h>
13#include <drm/drm_fb_helper.h>
Daniel Vetter76a39db2013-01-20 23:12:54 +010014#include <drm/drm_crtc_helper.h>
Dave Airlief9aa76a2012-04-17 14:12:29 +010015
Dave Airlief9aa76a2012-04-17 14:12:29 +010016#include "cirrus_drv.h"
17
18static void cirrus_dirty_update(struct cirrus_fbdev *afbdev,
19 int x, int y, int width, int height)
20{
21 int i;
22 struct drm_gem_object *obj;
23 struct cirrus_bo *bo;
24 int src_offset, dst_offset;
25 int bpp = (afbdev->gfb.base.bits_per_pixel + 7)/8;
Maarten Lankhorst19d4b722013-06-27 13:38:24 +020026 int ret = -EBUSY;
Dave Airlief9aa76a2012-04-17 14:12:29 +010027 bool unmap = false;
Dave Airlief3b2bbd2013-05-02 02:45:02 -040028 bool store_for_later = false;
29 int x2, y2;
30 unsigned long flags;
Dave Airlief9aa76a2012-04-17 14:12:29 +010031
32 obj = afbdev->gfb.obj;
33 bo = gem_to_cirrus_bo(obj);
34
Dave Airlief3b2bbd2013-05-02 02:45:02 -040035 /*
36 * try and reserve the BO, if we fail with busy
37 * then the BO is being moved and we should
38 * store up the damage until later.
39 */
Dave Airlie8b7ad1b2014-02-05 14:47:45 +100040 if (drm_can_sleep())
Maarten Lankhorst19d4b722013-06-27 13:38:24 +020041 ret = cirrus_bo_reserve(bo, true);
Dave Airlief9aa76a2012-04-17 14:12:29 +010042 if (ret) {
Dave Airlief3b2bbd2013-05-02 02:45:02 -040043 if (ret != -EBUSY)
44 return;
45 store_for_later = true;
46 }
47
48 x2 = x + width - 1;
49 y2 = y + height - 1;
50 spin_lock_irqsave(&afbdev->dirty_lock, flags);
51
52 if (afbdev->y1 < y)
53 y = afbdev->y1;
54 if (afbdev->y2 > y2)
55 y2 = afbdev->y2;
56 if (afbdev->x1 < x)
57 x = afbdev->x1;
58 if (afbdev->x2 > x2)
59 x2 = afbdev->x2;
60
61 if (store_for_later) {
62 afbdev->x1 = x;
63 afbdev->x2 = x2;
64 afbdev->y1 = y;
65 afbdev->y2 = y2;
66 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
Dave Airlief9aa76a2012-04-17 14:12:29 +010067 return;
68 }
69
Dave Airlief3b2bbd2013-05-02 02:45:02 -040070 afbdev->x1 = afbdev->y1 = INT_MAX;
71 afbdev->x2 = afbdev->y2 = 0;
72 spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
73
Dave Airlief9aa76a2012-04-17 14:12:29 +010074 if (!bo->kmap.virtual) {
75 ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
76 if (ret) {
77 DRM_ERROR("failed to kmap fb updates\n");
78 cirrus_bo_unreserve(bo);
79 return;
80 }
81 unmap = true;
82 }
83 for (i = y; i < y + height; i++) {
84 /* assume equal stride for now */
85 src_offset = dst_offset = i * afbdev->gfb.base.pitches[0] + (x * bpp);
86 memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
87
88 }
89 if (unmap)
90 ttm_bo_kunmap(&bo->kmap);
91
92 cirrus_bo_unreserve(bo);
93}
94
95static void cirrus_fillrect(struct fb_info *info,
96 const struct fb_fillrect *rect)
97{
98 struct cirrus_fbdev *afbdev = info->par;
Archit Taneja2b9e6e32015-07-31 16:21:44 +053099 drm_fb_helper_sys_fillrect(info, rect);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100100 cirrus_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
101 rect->height);
102}
103
104static void cirrus_copyarea(struct fb_info *info,
105 const struct fb_copyarea *area)
106{
107 struct cirrus_fbdev *afbdev = info->par;
Archit Taneja2b9e6e32015-07-31 16:21:44 +0530108 drm_fb_helper_sys_copyarea(info, area);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100109 cirrus_dirty_update(afbdev, area->dx, area->dy, area->width,
110 area->height);
111}
112
113static void cirrus_imageblit(struct fb_info *info,
114 const struct fb_image *image)
115{
116 struct cirrus_fbdev *afbdev = info->par;
Archit Taneja2b9e6e32015-07-31 16:21:44 +0530117 drm_fb_helper_sys_imageblit(info, image);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100118 cirrus_dirty_update(afbdev, image->dx, image->dy, image->width,
119 image->height);
120}
121
122
123static struct fb_ops cirrusfb_ops = {
124 .owner = THIS_MODULE,
125 .fb_check_var = drm_fb_helper_check_var,
126 .fb_set_par = drm_fb_helper_set_par,
127 .fb_fillrect = cirrus_fillrect,
128 .fb_copyarea = cirrus_copyarea,
129 .fb_imageblit = cirrus_imageblit,
130 .fb_pan_display = drm_fb_helper_pan_display,
131 .fb_blank = drm_fb_helper_blank,
132 .fb_setcmap = drm_fb_helper_setcmap,
133};
134
135static int cirrusfb_create_object(struct cirrus_fbdev *afbdev,
Ville Syrjälä1eb83452015-11-11 19:11:29 +0200136 const struct drm_mode_fb_cmd2 *mode_cmd,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100137 struct drm_gem_object **gobj_p)
138{
139 struct drm_device *dev = afbdev->helper.dev;
Zach Reizner89756262014-10-29 11:04:24 -0700140 struct cirrus_device *cdev = dev->dev_private;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100141 u32 bpp, depth;
142 u32 size;
143 struct drm_gem_object *gobj;
144
145 int ret = 0;
146 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
147
Zach Reizner89756262014-10-29 11:04:24 -0700148 if (!cirrus_check_framebuffer(cdev, mode_cmd->width, mode_cmd->height,
149 bpp, mode_cmd->pitches[0]))
Dave Airlief9aa76a2012-04-17 14:12:29 +0100150 return -EINVAL;
Zach Reizner89756262014-10-29 11:04:24 -0700151
Dave Airlief9aa76a2012-04-17 14:12:29 +0100152 size = mode_cmd->pitches[0] * mode_cmd->height;
153 ret = cirrus_gem_create(dev, size, true, &gobj);
154 if (ret)
155 return ret;
156
157 *gobj_p = gobj;
158 return ret;
159}
160
Daniel Vettercd5428a2013-01-21 23:42:49 +0100161static int cirrusfb_create(struct drm_fb_helper *helper,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100162 struct drm_fb_helper_surface_size *sizes)
163{
Fabian Frederickea0622c2014-09-14 18:40:14 +0200164 struct cirrus_fbdev *gfbdev =
165 container_of(helper, struct cirrus_fbdev, helper);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100166 struct cirrus_device *cdev = gfbdev->helper.dev->dev_private;
167 struct fb_info *info;
168 struct drm_framebuffer *fb;
169 struct drm_mode_fb_cmd2 mode_cmd;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100170 void *sysram;
171 struct drm_gem_object *gobj = NULL;
172 struct cirrus_bo *bo = NULL;
173 int size, ret;
174
175 mode_cmd.width = sizes->surface_width;
176 mode_cmd.height = sizes->surface_height;
177 mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
178 mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
179 sizes->surface_depth);
180 size = mode_cmd.pitches[0] * mode_cmd.height;
181
182 ret = cirrusfb_create_object(gfbdev, &mode_cmd, &gobj);
183 if (ret) {
184 DRM_ERROR("failed to create fbcon backing object %d\n", ret);
185 return ret;
186 }
187
188 bo = gem_to_cirrus_bo(gobj);
189
190 sysram = vmalloc(size);
191 if (!sysram)
192 return -ENOMEM;
193
Archit Taneja2b9e6e32015-07-31 16:21:44 +0530194 info = drm_fb_helper_alloc_fbi(helper);
195 if (IS_ERR(info))
196 return PTR_ERR(info);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100197
198 info->par = gfbdev;
199
200 ret = cirrus_framebuffer_init(cdev->dev, &gfbdev->gfb, &mode_cmd, gobj);
201 if (ret)
202 return ret;
203
204 gfbdev->sysram = sysram;
205 gfbdev->size = size;
206
207 fb = &gfbdev->gfb.base;
208 if (!fb) {
209 DRM_INFO("fb is NULL\n");
210 return -EINVAL;
211 }
212
213 /* setup helper */
214 gfbdev->helper.fb = fb;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100215
216 strcpy(info->fix.id, "cirrusdrmfb");
217
Dave Airlief9aa76a2012-04-17 14:12:29 +0100218 info->flags = FBINFO_DEFAULT;
219 info->fbops = &cirrusfb_ops;
220
221 drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
222 drm_fb_helper_fill_var(info, &gfbdev->helper, sizes->fb_width,
223 sizes->fb_height);
224
225 /* setup aperture base/size for vesafb takeover */
Dave Airlief9aa76a2012-04-17 14:12:29 +0100226 info->apertures->ranges[0].base = cdev->dev->mode_config.fb_base;
227 info->apertures->ranges[0].size = cdev->mc.vram_size;
228
Martin Koegler99d4a8a2014-01-09 10:05:07 +0100229 info->fix.smem_start = cdev->dev->mode_config.fb_base;
230 info->fix.smem_len = cdev->mc.vram_size;
231
Dave Airlief9aa76a2012-04-17 14:12:29 +0100232 info->screen_base = sysram;
233 info->screen_size = size;
234
235 info->fix.mmio_start = 0;
236 info->fix.mmio_len = 0;
237
Dave Airlief9aa76a2012-04-17 14:12:29 +0100238 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
239 DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info->fix.smem_start);
240 DRM_INFO("size %lu\n", (unsigned long)info->fix.smem_len);
241 DRM_INFO("fb depth is %d\n", fb->depth);
242 DRM_INFO(" pitch is %d\n", fb->pitches[0]);
243
244 return 0;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100245}
246
Dave Airlief9aa76a2012-04-17 14:12:29 +0100247static int cirrus_fbdev_destroy(struct drm_device *dev,
248 struct cirrus_fbdev *gfbdev)
249{
Dave Airlief9aa76a2012-04-17 14:12:29 +0100250 struct cirrus_framebuffer *gfb = &gfbdev->gfb;
251
Archit Taneja2b9e6e32015-07-31 16:21:44 +0530252 drm_fb_helper_unregister_fbi(&gfbdev->helper);
253 drm_fb_helper_release_fbi(&gfbdev->helper);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100254
255 if (gfb->obj) {
256 drm_gem_object_unreference_unlocked(gfb->obj);
257 gfb->obj = NULL;
258 }
259
260 vfree(gfbdev->sysram);
261 drm_fb_helper_fini(&gfbdev->helper);
Daniel Vetter36206362012-12-10 20:42:17 +0100262 drm_framebuffer_unregister_private(&gfb->base);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100263 drm_framebuffer_cleanup(&gfb->base);
264
265 return 0;
266}
267
Thierry Reding3a493872014-06-27 17:19:23 +0200268static const struct drm_fb_helper_funcs cirrus_fb_helper_funcs = {
Dave Airlief9aa76a2012-04-17 14:12:29 +0100269 .gamma_set = cirrus_crtc_fb_gamma_set,
270 .gamma_get = cirrus_crtc_fb_gamma_get,
Daniel Vettercd5428a2013-01-21 23:42:49 +0100271 .fb_probe = cirrusfb_create,
Dave Airlief9aa76a2012-04-17 14:12:29 +0100272};
273
274int cirrus_fbdev_init(struct cirrus_device *cdev)
275{
276 struct cirrus_fbdev *gfbdev;
277 int ret;
278 int bpp_sel = 24;
279
280 /*bpp_sel = 8;*/
281 gfbdev = kzalloc(sizeof(struct cirrus_fbdev), GFP_KERNEL);
282 if (!gfbdev)
283 return -ENOMEM;
284
285 cdev->mode_info.gfbdev = gfbdev;
Dave Airlief3b2bbd2013-05-02 02:45:02 -0400286 spin_lock_init(&gfbdev->dirty_lock);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100287
Thierry Reding10a23102014-06-27 17:19:24 +0200288 drm_fb_helper_prepare(cdev->dev, &gfbdev->helper,
289 &cirrus_fb_helper_funcs);
290
Dave Airlief9aa76a2012-04-17 14:12:29 +0100291 ret = drm_fb_helper_init(cdev->dev, &gfbdev->helper,
292 cdev->num_crtc, CIRRUSFB_CONN_LIMIT);
Thierry Reding01934c22014-12-19 11:21:32 +0100293 if (ret)
Dave Airlief9aa76a2012-04-17 14:12:29 +0100294 return ret;
Thierry Reding01934c22014-12-19 11:21:32 +0100295
296 ret = drm_fb_helper_single_add_all_connectors(&gfbdev->helper);
297 if (ret)
298 return ret;
Daniel Vetter76a39db2013-01-20 23:12:54 +0100299
300 /* disable all the possible outputs/crtcs before entering KMS mode */
301 drm_helper_disable_unused_functions(cdev->dev);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100302
Thierry Reding01934c22014-12-19 11:21:32 +0100303 return drm_fb_helper_initial_config(&gfbdev->helper, bpp_sel);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100304}
305
306void cirrus_fbdev_fini(struct cirrus_device *cdev)
307{
308 if (!cdev->mode_info.gfbdev)
309 return;
310
311 cirrus_fbdev_destroy(cdev->dev, cdev->mode_info.gfbdev);
312 kfree(cdev->mode_info.gfbdev);
313 cdev->mode_info.gfbdev = NULL;
314}