Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Matt Turner. |
| 3 | * Copyright 2012 Red Hat |
| 4 | * |
| 5 | * This file is subject to the terms and conditions of the GNU General |
| 6 | * Public License version 2. See the file COPYING in the main |
| 7 | * directory of this archive for more details. |
| 8 | * |
| 9 | * Authors: Matthew Garrett |
| 10 | * Matt Turner |
| 11 | * Dave Airlie |
| 12 | */ |
| 13 | #include <linux/module.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 14 | #include <drm/drmP.h> |
| 15 | #include <drm/drm_fb_helper.h> |
Daniel Vetter | 76a39db | 2013-01-20 23:12:54 +0100 | [diff] [blame] | 16 | #include <drm/drm_crtc_helper.h> |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 17 | |
| 18 | #include <linux/fb.h> |
| 19 | |
| 20 | #include "mgag200_drv.h" |
| 21 | |
| 22 | static void mga_dirty_update(struct mga_fbdev *mfbdev, |
| 23 | int x, int y, int width, int height) |
| 24 | { |
| 25 | int i; |
| 26 | struct drm_gem_object *obj; |
| 27 | struct mgag200_bo *bo; |
| 28 | int src_offset, dst_offset; |
| 29 | int bpp = (mfbdev->mfb.base.bits_per_pixel + 7)/8; |
| 30 | int ret; |
| 31 | bool unmap = false; |
| 32 | |
| 33 | obj = mfbdev->mfb.obj; |
| 34 | bo = gem_to_mga_bo(obj); |
| 35 | |
| 36 | ret = mgag200_bo_reserve(bo, true); |
| 37 | if (ret) { |
| 38 | DRM_ERROR("failed to reserve fb bo\n"); |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | if (!bo->kmap.virtual) { |
| 43 | ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap); |
| 44 | if (ret) { |
| 45 | DRM_ERROR("failed to kmap fb updates\n"); |
| 46 | mgag200_bo_unreserve(bo); |
| 47 | return; |
| 48 | } |
| 49 | unmap = true; |
| 50 | } |
| 51 | for (i = y; i < y + height; i++) { |
| 52 | /* assume equal stride for now */ |
| 53 | src_offset = dst_offset = i * mfbdev->mfb.base.pitches[0] + (x * bpp); |
| 54 | memcpy_toio(bo->kmap.virtual + src_offset, mfbdev->sysram + src_offset, width * bpp); |
| 55 | |
| 56 | } |
| 57 | if (unmap) |
| 58 | ttm_bo_kunmap(&bo->kmap); |
| 59 | |
| 60 | mgag200_bo_unreserve(bo); |
| 61 | } |
| 62 | |
| 63 | static void mga_fillrect(struct fb_info *info, |
| 64 | const struct fb_fillrect *rect) |
| 65 | { |
| 66 | struct mga_fbdev *mfbdev = info->par; |
| 67 | sys_fillrect(info, rect); |
| 68 | mga_dirty_update(mfbdev, rect->dx, rect->dy, rect->width, |
| 69 | rect->height); |
| 70 | } |
| 71 | |
| 72 | static void mga_copyarea(struct fb_info *info, |
| 73 | const struct fb_copyarea *area) |
| 74 | { |
| 75 | struct mga_fbdev *mfbdev = info->par; |
| 76 | sys_copyarea(info, area); |
| 77 | mga_dirty_update(mfbdev, area->dx, area->dy, area->width, |
| 78 | area->height); |
| 79 | } |
| 80 | |
| 81 | static void mga_imageblit(struct fb_info *info, |
| 82 | const struct fb_image *image) |
| 83 | { |
| 84 | struct mga_fbdev *mfbdev = info->par; |
| 85 | sys_imageblit(info, image); |
| 86 | mga_dirty_update(mfbdev, image->dx, image->dy, image->width, |
| 87 | image->height); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static struct fb_ops mgag200fb_ops = { |
| 92 | .owner = THIS_MODULE, |
| 93 | .fb_check_var = drm_fb_helper_check_var, |
| 94 | .fb_set_par = drm_fb_helper_set_par, |
| 95 | .fb_fillrect = mga_fillrect, |
| 96 | .fb_copyarea = mga_copyarea, |
| 97 | .fb_imageblit = mga_imageblit, |
| 98 | .fb_pan_display = drm_fb_helper_pan_display, |
| 99 | .fb_blank = drm_fb_helper_blank, |
| 100 | .fb_setcmap = drm_fb_helper_setcmap, |
| 101 | }; |
| 102 | |
| 103 | static int mgag200fb_create_object(struct mga_fbdev *afbdev, |
| 104 | struct drm_mode_fb_cmd2 *mode_cmd, |
| 105 | struct drm_gem_object **gobj_p) |
| 106 | { |
| 107 | struct drm_device *dev = afbdev->helper.dev; |
| 108 | u32 bpp, depth; |
| 109 | u32 size; |
| 110 | struct drm_gem_object *gobj; |
| 111 | |
| 112 | int ret = 0; |
| 113 | drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp); |
| 114 | |
| 115 | size = mode_cmd->pitches[0] * mode_cmd->height; |
| 116 | ret = mgag200_gem_create(dev, size, true, &gobj); |
| 117 | if (ret) |
| 118 | return ret; |
| 119 | |
| 120 | *gobj_p = gobj; |
| 121 | return ret; |
| 122 | } |
| 123 | |
Daniel Vetter | cd5428a | 2013-01-21 23:42:49 +0100 | [diff] [blame] | 124 | static int mgag200fb_create(struct drm_fb_helper *helper, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 125 | struct drm_fb_helper_surface_size *sizes) |
| 126 | { |
Daniel Vetter | cd5428a | 2013-01-21 23:42:49 +0100 | [diff] [blame] | 127 | struct mga_fbdev *mfbdev = (struct mga_fbdev *)helper; |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 128 | struct drm_device *dev = mfbdev->helper.dev; |
| 129 | struct drm_mode_fb_cmd2 mode_cmd; |
| 130 | struct mga_device *mdev = dev->dev_private; |
| 131 | struct fb_info *info; |
| 132 | struct drm_framebuffer *fb; |
| 133 | struct drm_gem_object *gobj = NULL; |
| 134 | struct device *device = &dev->pdev->dev; |
| 135 | struct mgag200_bo *bo; |
| 136 | int ret; |
| 137 | void *sysram; |
| 138 | int size; |
| 139 | |
| 140 | mode_cmd.width = sizes->surface_width; |
| 141 | mode_cmd.height = sizes->surface_height; |
| 142 | mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8); |
| 143 | |
| 144 | mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp, |
| 145 | sizes->surface_depth); |
| 146 | size = mode_cmd.pitches[0] * mode_cmd.height; |
| 147 | |
| 148 | ret = mgag200fb_create_object(mfbdev, &mode_cmd, &gobj); |
| 149 | if (ret) { |
| 150 | DRM_ERROR("failed to create fbcon backing object %d\n", ret); |
| 151 | return ret; |
| 152 | } |
| 153 | bo = gem_to_mga_bo(gobj); |
| 154 | |
| 155 | sysram = vmalloc(size); |
| 156 | if (!sysram) |
| 157 | return -ENOMEM; |
| 158 | |
| 159 | info = framebuffer_alloc(0, device); |
| 160 | if (info == NULL) |
| 161 | return -ENOMEM; |
| 162 | |
| 163 | info->par = mfbdev; |
| 164 | |
| 165 | ret = mgag200_framebuffer_init(dev, &mfbdev->mfb, &mode_cmd, gobj); |
| 166 | if (ret) |
| 167 | return ret; |
| 168 | |
| 169 | mfbdev->sysram = sysram; |
| 170 | mfbdev->size = size; |
| 171 | |
| 172 | fb = &mfbdev->mfb.base; |
| 173 | |
| 174 | /* setup helper */ |
| 175 | mfbdev->helper.fb = fb; |
| 176 | mfbdev->helper.fbdev = info; |
| 177 | |
| 178 | ret = fb_alloc_cmap(&info->cmap, 256, 0); |
| 179 | if (ret) { |
| 180 | DRM_ERROR("%s: can't allocate color map\n", info->fix.id); |
| 181 | ret = -ENOMEM; |
| 182 | goto out; |
| 183 | } |
| 184 | |
| 185 | strcpy(info->fix.id, "mgadrmfb"); |
| 186 | |
| 187 | info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT; |
| 188 | info->fbops = &mgag200fb_ops; |
| 189 | |
| 190 | /* setup aperture base/size for vesafb takeover */ |
| 191 | info->apertures = alloc_apertures(1); |
| 192 | if (!info->apertures) { |
| 193 | ret = -ENOMEM; |
| 194 | goto out; |
| 195 | } |
| 196 | info->apertures->ranges[0].base = mdev->dev->mode_config.fb_base; |
| 197 | info->apertures->ranges[0].size = mdev->mc.vram_size; |
| 198 | |
| 199 | drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth); |
| 200 | drm_fb_helper_fill_var(info, &mfbdev->helper, sizes->fb_width, |
| 201 | sizes->fb_height); |
| 202 | |
| 203 | info->screen_base = sysram; |
| 204 | info->screen_size = size; |
| 205 | info->pixmap.flags = FB_PIXMAP_SYSTEM; |
| 206 | |
| 207 | DRM_DEBUG_KMS("allocated %dx%d\n", |
| 208 | fb->width, fb->height); |
| 209 | return 0; |
| 210 | out: |
| 211 | return ret; |
| 212 | } |
| 213 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 214 | static int mga_fbdev_destroy(struct drm_device *dev, |
| 215 | struct mga_fbdev *mfbdev) |
| 216 | { |
| 217 | struct fb_info *info; |
| 218 | struct mga_framebuffer *mfb = &mfbdev->mfb; |
| 219 | |
| 220 | if (mfbdev->helper.fbdev) { |
| 221 | info = mfbdev->helper.fbdev; |
| 222 | |
| 223 | unregister_framebuffer(info); |
| 224 | if (info->cmap.len) |
| 225 | fb_dealloc_cmap(&info->cmap); |
| 226 | framebuffer_release(info); |
| 227 | } |
| 228 | |
| 229 | if (mfb->obj) { |
| 230 | drm_gem_object_unreference_unlocked(mfb->obj); |
| 231 | mfb->obj = NULL; |
| 232 | } |
| 233 | drm_fb_helper_fini(&mfbdev->helper); |
| 234 | vfree(mfbdev->sysram); |
Daniel Vetter | 3620636 | 2012-12-10 20:42:17 +0100 | [diff] [blame] | 235 | drm_framebuffer_unregister_private(&mfb->base); |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 236 | drm_framebuffer_cleanup(&mfb->base); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static struct drm_fb_helper_funcs mga_fb_helper_funcs = { |
| 242 | .gamma_set = mga_crtc_fb_gamma_set, |
| 243 | .gamma_get = mga_crtc_fb_gamma_get, |
Daniel Vetter | cd5428a | 2013-01-21 23:42:49 +0100 | [diff] [blame] | 244 | .fb_probe = mgag200fb_create, |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | int mgag200_fbdev_init(struct mga_device *mdev) |
| 248 | { |
| 249 | struct mga_fbdev *mfbdev; |
| 250 | int ret; |
| 251 | |
| 252 | mfbdev = kzalloc(sizeof(struct mga_fbdev), GFP_KERNEL); |
| 253 | if (!mfbdev) |
| 254 | return -ENOMEM; |
| 255 | |
| 256 | mdev->mfbdev = mfbdev; |
| 257 | mfbdev->helper.funcs = &mga_fb_helper_funcs; |
| 258 | |
| 259 | ret = drm_fb_helper_init(mdev->dev, &mfbdev->helper, |
| 260 | mdev->num_crtc, MGAG200FB_CONN_LIMIT); |
| 261 | if (ret) { |
| 262 | kfree(mfbdev); |
| 263 | return ret; |
| 264 | } |
| 265 | drm_fb_helper_single_add_all_connectors(&mfbdev->helper); |
Daniel Vetter | 76a39db | 2013-01-20 23:12:54 +0100 | [diff] [blame] | 266 | |
| 267 | /* disable all the possible outputs/crtcs before entering KMS mode */ |
| 268 | drm_helper_disable_unused_functions(mdev->dev); |
| 269 | |
Dave Airlie | 414c453 | 2012-04-17 15:01:25 +0100 | [diff] [blame] | 270 | drm_fb_helper_initial_config(&mfbdev->helper, 32); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | void mgag200_fbdev_fini(struct mga_device *mdev) |
| 276 | { |
| 277 | if (!mdev->mfbdev) |
| 278 | return; |
| 279 | |
| 280 | mga_fbdev_destroy(mdev->dev, mdev->mfbdev); |
| 281 | kfree(mdev->mfbdev); |
| 282 | mdev->mfbdev = NULL; |
| 283 | } |