cirrus_fbdev.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  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>
  12. #include <drm/drmP.h>
  13. #include <drm/drm_fb_helper.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include <linux/fb.h>
  16. #include "cirrus_drv.h"
  17. static void cirrus_dirty_update(struct cirrus_fbdev *afbdev,
  18. int x, int y, int width, int height)
  19. {
  20. int i;
  21. struct drm_gem_object *obj;
  22. struct cirrus_bo *bo;
  23. int src_offset, dst_offset;
  24. int bpp = (afbdev->gfb.base.bits_per_pixel + 7)/8;
  25. int ret;
  26. bool unmap = false;
  27. obj = afbdev->gfb.obj;
  28. bo = gem_to_cirrus_bo(obj);
  29. ret = cirrus_bo_reserve(bo, true);
  30. if (ret) {
  31. DRM_ERROR("failed to reserve fb bo\n");
  32. return;
  33. }
  34. if (!bo->kmap.virtual) {
  35. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  36. if (ret) {
  37. DRM_ERROR("failed to kmap fb updates\n");
  38. cirrus_bo_unreserve(bo);
  39. return;
  40. }
  41. unmap = true;
  42. }
  43. for (i = y; i < y + height; i++) {
  44. /* assume equal stride for now */
  45. src_offset = dst_offset = i * afbdev->gfb.base.pitches[0] + (x * bpp);
  46. memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
  47. }
  48. if (unmap)
  49. ttm_bo_kunmap(&bo->kmap);
  50. cirrus_bo_unreserve(bo);
  51. }
  52. static void cirrus_fillrect(struct fb_info *info,
  53. const struct fb_fillrect *rect)
  54. {
  55. struct cirrus_fbdev *afbdev = info->par;
  56. sys_fillrect(info, rect);
  57. cirrus_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
  58. rect->height);
  59. }
  60. static void cirrus_copyarea(struct fb_info *info,
  61. const struct fb_copyarea *area)
  62. {
  63. struct cirrus_fbdev *afbdev = info->par;
  64. sys_copyarea(info, area);
  65. cirrus_dirty_update(afbdev, area->dx, area->dy, area->width,
  66. area->height);
  67. }
  68. static void cirrus_imageblit(struct fb_info *info,
  69. const struct fb_image *image)
  70. {
  71. struct cirrus_fbdev *afbdev = info->par;
  72. sys_imageblit(info, image);
  73. cirrus_dirty_update(afbdev, image->dx, image->dy, image->width,
  74. image->height);
  75. }
  76. static struct fb_ops cirrusfb_ops = {
  77. .owner = THIS_MODULE,
  78. .fb_check_var = drm_fb_helper_check_var,
  79. .fb_set_par = drm_fb_helper_set_par,
  80. .fb_fillrect = cirrus_fillrect,
  81. .fb_copyarea = cirrus_copyarea,
  82. .fb_imageblit = cirrus_imageblit,
  83. .fb_pan_display = drm_fb_helper_pan_display,
  84. .fb_blank = drm_fb_helper_blank,
  85. .fb_setcmap = drm_fb_helper_setcmap,
  86. };
  87. static int cirrusfb_create_object(struct cirrus_fbdev *afbdev,
  88. struct drm_mode_fb_cmd2 *mode_cmd,
  89. struct drm_gem_object **gobj_p)
  90. {
  91. struct drm_device *dev = afbdev->helper.dev;
  92. u32 bpp, depth;
  93. u32 size;
  94. struct drm_gem_object *gobj;
  95. int ret = 0;
  96. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  97. if (bpp > 24)
  98. return -EINVAL;
  99. size = mode_cmd->pitches[0] * mode_cmd->height;
  100. ret = cirrus_gem_create(dev, size, true, &gobj);
  101. if (ret)
  102. return ret;
  103. *gobj_p = gobj;
  104. return ret;
  105. }
  106. static int cirrusfb_create(struct drm_fb_helper *helper,
  107. struct drm_fb_helper_surface_size *sizes)
  108. {
  109. struct cirrus_fbdev *gfbdev = (struct cirrus_fbdev *)helper;
  110. struct drm_device *dev = gfbdev->helper.dev;
  111. struct cirrus_device *cdev = gfbdev->helper.dev->dev_private;
  112. struct fb_info *info;
  113. struct drm_framebuffer *fb;
  114. struct drm_mode_fb_cmd2 mode_cmd;
  115. struct device *device = &dev->pdev->dev;
  116. void *sysram;
  117. struct drm_gem_object *gobj = NULL;
  118. struct cirrus_bo *bo = NULL;
  119. int size, ret;
  120. mode_cmd.width = sizes->surface_width;
  121. mode_cmd.height = sizes->surface_height;
  122. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  123. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  124. sizes->surface_depth);
  125. size = mode_cmd.pitches[0] * mode_cmd.height;
  126. ret = cirrusfb_create_object(gfbdev, &mode_cmd, &gobj);
  127. if (ret) {
  128. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  129. return ret;
  130. }
  131. bo = gem_to_cirrus_bo(gobj);
  132. sysram = vmalloc(size);
  133. if (!sysram)
  134. return -ENOMEM;
  135. info = framebuffer_alloc(0, device);
  136. if (info == NULL)
  137. return -ENOMEM;
  138. info->par = gfbdev;
  139. ret = cirrus_framebuffer_init(cdev->dev, &gfbdev->gfb, &mode_cmd, gobj);
  140. if (ret)
  141. return ret;
  142. gfbdev->sysram = sysram;
  143. gfbdev->size = size;
  144. fb = &gfbdev->gfb.base;
  145. if (!fb) {
  146. DRM_INFO("fb is NULL\n");
  147. return -EINVAL;
  148. }
  149. /* setup helper */
  150. gfbdev->helper.fb = fb;
  151. gfbdev->helper.fbdev = info;
  152. strcpy(info->fix.id, "cirrusdrmfb");
  153. info->flags = FBINFO_DEFAULT;
  154. info->fbops = &cirrusfb_ops;
  155. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  156. drm_fb_helper_fill_var(info, &gfbdev->helper, sizes->fb_width,
  157. sizes->fb_height);
  158. /* setup aperture base/size for vesafb takeover */
  159. info->apertures = alloc_apertures(1);
  160. if (!info->apertures) {
  161. ret = -ENOMEM;
  162. goto out_iounmap;
  163. }
  164. info->apertures->ranges[0].base = cdev->dev->mode_config.fb_base;
  165. info->apertures->ranges[0].size = cdev->mc.vram_size;
  166. info->screen_base = sysram;
  167. info->screen_size = size;
  168. info->fix.mmio_start = 0;
  169. info->fix.mmio_len = 0;
  170. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  171. if (ret) {
  172. DRM_ERROR("%s: can't allocate color map\n", info->fix.id);
  173. ret = -ENOMEM;
  174. goto out_iounmap;
  175. }
  176. DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
  177. DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info->fix.smem_start);
  178. DRM_INFO("size %lu\n", (unsigned long)info->fix.smem_len);
  179. DRM_INFO("fb depth is %d\n", fb->depth);
  180. DRM_INFO(" pitch is %d\n", fb->pitches[0]);
  181. return 0;
  182. out_iounmap:
  183. return ret;
  184. }
  185. static int cirrus_fbdev_destroy(struct drm_device *dev,
  186. struct cirrus_fbdev *gfbdev)
  187. {
  188. struct fb_info *info;
  189. struct cirrus_framebuffer *gfb = &gfbdev->gfb;
  190. if (gfbdev->helper.fbdev) {
  191. info = gfbdev->helper.fbdev;
  192. unregister_framebuffer(info);
  193. if (info->cmap.len)
  194. fb_dealloc_cmap(&info->cmap);
  195. framebuffer_release(info);
  196. }
  197. if (gfb->obj) {
  198. drm_gem_object_unreference_unlocked(gfb->obj);
  199. gfb->obj = NULL;
  200. }
  201. vfree(gfbdev->sysram);
  202. drm_fb_helper_fini(&gfbdev->helper);
  203. drm_framebuffer_unregister_private(&gfb->base);
  204. drm_framebuffer_cleanup(&gfb->base);
  205. return 0;
  206. }
  207. static struct drm_fb_helper_funcs cirrus_fb_helper_funcs = {
  208. .gamma_set = cirrus_crtc_fb_gamma_set,
  209. .gamma_get = cirrus_crtc_fb_gamma_get,
  210. .fb_probe = cirrusfb_create,
  211. };
  212. int cirrus_fbdev_init(struct cirrus_device *cdev)
  213. {
  214. struct cirrus_fbdev *gfbdev;
  215. int ret;
  216. int bpp_sel = 24;
  217. /*bpp_sel = 8;*/
  218. gfbdev = kzalloc(sizeof(struct cirrus_fbdev), GFP_KERNEL);
  219. if (!gfbdev)
  220. return -ENOMEM;
  221. cdev->mode_info.gfbdev = gfbdev;
  222. gfbdev->helper.funcs = &cirrus_fb_helper_funcs;
  223. ret = drm_fb_helper_init(cdev->dev, &gfbdev->helper,
  224. cdev->num_crtc, CIRRUSFB_CONN_LIMIT);
  225. if (ret) {
  226. kfree(gfbdev);
  227. return ret;
  228. }
  229. drm_fb_helper_single_add_all_connectors(&gfbdev->helper);
  230. /* disable all the possible outputs/crtcs before entering KMS mode */
  231. drm_helper_disable_unused_functions(cdev->dev);
  232. drm_fb_helper_initial_config(&gfbdev->helper, bpp_sel);
  233. return 0;
  234. }
  235. void cirrus_fbdev_fini(struct cirrus_device *cdev)
  236. {
  237. if (!cdev->mode_info.gfbdev)
  238. return;
  239. cirrus_fbdev_destroy(cdev->dev, cdev->mode_info.gfbdev);
  240. kfree(cdev->mode_info.gfbdev);
  241. cdev->mode_info.gfbdev = NULL;
  242. }