cirrus_fbdev.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "drmP.h"
  13. #include "drm.h"
  14. #include "drm_fb_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 cirrus_fbdev *gfbdev,
  107. struct drm_fb_helper_surface_size *sizes)
  108. {
  109. struct drm_device *dev = gfbdev->helper.dev;
  110. struct cirrus_device *cdev = gfbdev->helper.dev->dev_private;
  111. struct fb_info *info;
  112. struct drm_framebuffer *fb;
  113. struct drm_mode_fb_cmd2 mode_cmd;
  114. struct device *device = &dev->pdev->dev;
  115. void *sysram;
  116. struct drm_gem_object *gobj = NULL;
  117. struct cirrus_bo *bo = NULL;
  118. int size, ret;
  119. mode_cmd.width = sizes->surface_width;
  120. mode_cmd.height = sizes->surface_height;
  121. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  122. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  123. sizes->surface_depth);
  124. size = mode_cmd.pitches[0] * mode_cmd.height;
  125. ret = cirrusfb_create_object(gfbdev, &mode_cmd, &gobj);
  126. if (ret) {
  127. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  128. return ret;
  129. }
  130. bo = gem_to_cirrus_bo(gobj);
  131. sysram = vmalloc(size);
  132. if (!sysram)
  133. return -ENOMEM;
  134. info = framebuffer_alloc(0, device);
  135. if (info == NULL)
  136. return -ENOMEM;
  137. info->par = gfbdev;
  138. ret = cirrus_framebuffer_init(cdev->dev, &gfbdev->gfb, &mode_cmd, gobj);
  139. if (ret)
  140. return ret;
  141. gfbdev->sysram = sysram;
  142. gfbdev->size = size;
  143. fb = &gfbdev->gfb.base;
  144. if (!fb) {
  145. DRM_INFO("fb is NULL\n");
  146. return -EINVAL;
  147. }
  148. /* setup helper */
  149. gfbdev->helper.fb = fb;
  150. gfbdev->helper.fbdev = info;
  151. strcpy(info->fix.id, "cirrusdrmfb");
  152. info->flags = FBINFO_DEFAULT;
  153. info->fbops = &cirrusfb_ops;
  154. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  155. drm_fb_helper_fill_var(info, &gfbdev->helper, sizes->fb_width,
  156. sizes->fb_height);
  157. /* setup aperture base/size for vesafb takeover */
  158. info->apertures = alloc_apertures(1);
  159. if (!info->apertures) {
  160. ret = -ENOMEM;
  161. goto out_iounmap;
  162. }
  163. info->apertures->ranges[0].base = cdev->dev->mode_config.fb_base;
  164. info->apertures->ranges[0].size = cdev->mc.vram_size;
  165. info->screen_base = sysram;
  166. info->screen_size = size;
  167. info->fix.mmio_start = 0;
  168. info->fix.mmio_len = 0;
  169. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  170. if (ret) {
  171. DRM_ERROR("%s: can't allocate color map\n", info->fix.id);
  172. ret = -ENOMEM;
  173. goto out_iounmap;
  174. }
  175. DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
  176. DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info->fix.smem_start);
  177. DRM_INFO("size %lu\n", (unsigned long)info->fix.smem_len);
  178. DRM_INFO("fb depth is %d\n", fb->depth);
  179. DRM_INFO(" pitch is %d\n", fb->pitches[0]);
  180. return 0;
  181. out_iounmap:
  182. return ret;
  183. }
  184. static int cirrus_fb_find_or_create_single(struct drm_fb_helper *helper,
  185. struct drm_fb_helper_surface_size
  186. *sizes)
  187. {
  188. struct cirrus_fbdev *gfbdev = (struct cirrus_fbdev *)helper;
  189. int new_fb = 0;
  190. int ret;
  191. if (!helper->fb) {
  192. ret = cirrusfb_create(gfbdev, sizes);
  193. if (ret)
  194. return ret;
  195. new_fb = 1;
  196. }
  197. return new_fb;
  198. }
  199. static int cirrus_fbdev_destroy(struct drm_device *dev,
  200. struct cirrus_fbdev *gfbdev)
  201. {
  202. struct fb_info *info;
  203. struct cirrus_framebuffer *gfb = &gfbdev->gfb;
  204. if (gfbdev->helper.fbdev) {
  205. info = gfbdev->helper.fbdev;
  206. unregister_framebuffer(info);
  207. if (info->cmap.len)
  208. fb_dealloc_cmap(&info->cmap);
  209. framebuffer_release(info);
  210. }
  211. if (gfb->obj) {
  212. drm_gem_object_unreference_unlocked(gfb->obj);
  213. gfb->obj = NULL;
  214. }
  215. vfree(gfbdev->sysram);
  216. drm_fb_helper_fini(&gfbdev->helper);
  217. drm_framebuffer_cleanup(&gfb->base);
  218. return 0;
  219. }
  220. static struct drm_fb_helper_funcs cirrus_fb_helper_funcs = {
  221. .gamma_set = cirrus_crtc_fb_gamma_set,
  222. .gamma_get = cirrus_crtc_fb_gamma_get,
  223. .fb_probe = cirrus_fb_find_or_create_single,
  224. };
  225. int cirrus_fbdev_init(struct cirrus_device *cdev)
  226. {
  227. struct cirrus_fbdev *gfbdev;
  228. int ret;
  229. int bpp_sel = 24;
  230. /*bpp_sel = 8;*/
  231. gfbdev = kzalloc(sizeof(struct cirrus_fbdev), GFP_KERNEL);
  232. if (!gfbdev)
  233. return -ENOMEM;
  234. cdev->mode_info.gfbdev = gfbdev;
  235. gfbdev->helper.funcs = &cirrus_fb_helper_funcs;
  236. ret = drm_fb_helper_init(cdev->dev, &gfbdev->helper,
  237. cdev->num_crtc, CIRRUSFB_CONN_LIMIT);
  238. if (ret) {
  239. kfree(gfbdev);
  240. return ret;
  241. }
  242. drm_fb_helper_single_add_all_connectors(&gfbdev->helper);
  243. drm_fb_helper_initial_config(&gfbdev->helper, bpp_sel);
  244. return 0;
  245. }
  246. void cirrus_fbdev_fini(struct cirrus_device *cdev)
  247. {
  248. if (!cdev->mode_info.gfbdev)
  249. return;
  250. cirrus_fbdev_destroy(cdev->dev, cdev->mode_info.gfbdev);
  251. kfree(cdev->mode_info.gfbdev);
  252. cdev->mode_info.gfbdev = NULL;
  253. }