cirrus_fbdev.c 7.2 KB

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