msm_fbdev.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "msm_drv.h"
  18. #include "drm_crtc.h"
  19. #include "drm_fb_helper.h"
  20. /*
  21. * fbdev funcs, to implement legacy fbdev interface on top of drm driver
  22. */
  23. #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
  24. struct msm_fbdev {
  25. struct drm_fb_helper base;
  26. struct drm_framebuffer *fb;
  27. struct drm_gem_object *bo;
  28. };
  29. static struct fb_ops msm_fb_ops = {
  30. .owner = THIS_MODULE,
  31. /* Note: to properly handle manual update displays, we wrap the
  32. * basic fbdev ops which write to the framebuffer
  33. */
  34. .fb_read = fb_sys_read,
  35. .fb_write = fb_sys_write,
  36. .fb_fillrect = sys_fillrect,
  37. .fb_copyarea = sys_copyarea,
  38. .fb_imageblit = sys_imageblit,
  39. .fb_check_var = drm_fb_helper_check_var,
  40. .fb_set_par = drm_fb_helper_set_par,
  41. .fb_pan_display = drm_fb_helper_pan_display,
  42. .fb_blank = drm_fb_helper_blank,
  43. .fb_setcmap = drm_fb_helper_setcmap,
  44. };
  45. static int msm_fbdev_create(struct drm_fb_helper *helper,
  46. struct drm_fb_helper_surface_size *sizes)
  47. {
  48. struct msm_fbdev *fbdev = to_msm_fbdev(helper);
  49. struct drm_device *dev = helper->dev;
  50. struct drm_framebuffer *fb = NULL;
  51. struct fb_info *fbi = NULL;
  52. struct drm_mode_fb_cmd2 mode_cmd = {0};
  53. dma_addr_t paddr;
  54. int ret, size;
  55. /* only doing ARGB32 since this is what is needed to alpha-blend
  56. * with video overlays:
  57. */
  58. sizes->surface_bpp = 32;
  59. sizes->surface_depth = 32;
  60. DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
  61. sizes->surface_height, sizes->surface_bpp,
  62. sizes->fb_width, sizes->fb_height);
  63. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  64. sizes->surface_depth);
  65. mode_cmd.width = sizes->surface_width;
  66. mode_cmd.height = sizes->surface_height;
  67. mode_cmd.pitches[0] = align_pitch(
  68. mode_cmd.width, sizes->surface_bpp);
  69. /* allocate backing bo */
  70. size = mode_cmd.pitches[0] * mode_cmd.height;
  71. DBG("allocating %d bytes for fb %d", size, dev->primary->index);
  72. mutex_lock(&dev->struct_mutex);
  73. fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
  74. mutex_unlock(&dev->struct_mutex);
  75. if (IS_ERR(fbdev->bo)) {
  76. ret = PTR_ERR(fbdev->bo);
  77. fbdev->bo = NULL;
  78. dev_err(dev->dev, "failed to allocate buffer object: %d\n", ret);
  79. goto fail;
  80. }
  81. fb = msm_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
  82. if (IS_ERR(fb)) {
  83. dev_err(dev->dev, "failed to allocate fb\n");
  84. /* note: if fb creation failed, we can't rely on fb destroy
  85. * to unref the bo:
  86. */
  87. drm_gem_object_unreference(fbdev->bo);
  88. ret = PTR_ERR(fb);
  89. goto fail;
  90. }
  91. mutex_lock(&dev->struct_mutex);
  92. /* TODO implement our own fb_mmap so we don't need this: */
  93. msm_gem_get_iova_locked(fbdev->bo, 0, &paddr);
  94. fbi = framebuffer_alloc(0, dev->dev);
  95. if (!fbi) {
  96. dev_err(dev->dev, "failed to allocate fb info\n");
  97. ret = -ENOMEM;
  98. goto fail_unlock;
  99. }
  100. DBG("fbi=%p, dev=%p", fbi, dev);
  101. fbdev->fb = fb;
  102. helper->fb = fb;
  103. helper->fbdev = fbi;
  104. fbi->par = helper;
  105. fbi->flags = FBINFO_DEFAULT;
  106. fbi->fbops = &msm_fb_ops;
  107. strcpy(fbi->fix.id, "msm");
  108. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  109. if (ret) {
  110. ret = -ENOMEM;
  111. goto fail_unlock;
  112. }
  113. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  114. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  115. dev->mode_config.fb_base = paddr;
  116. fbi->screen_base = msm_gem_vaddr_locked(fbdev->bo);
  117. fbi->screen_size = fbdev->bo->size;
  118. fbi->fix.smem_start = paddr;
  119. fbi->fix.smem_len = fbdev->bo->size;
  120. DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
  121. DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
  122. mutex_unlock(&dev->struct_mutex);
  123. return 0;
  124. fail_unlock:
  125. mutex_unlock(&dev->struct_mutex);
  126. fail:
  127. if (ret) {
  128. if (fbi)
  129. framebuffer_release(fbi);
  130. if (fb) {
  131. drm_framebuffer_unregister_private(fb);
  132. drm_framebuffer_remove(fb);
  133. }
  134. }
  135. return ret;
  136. }
  137. static void msm_crtc_fb_gamma_set(struct drm_crtc *crtc,
  138. u16 red, u16 green, u16 blue, int regno)
  139. {
  140. DBG("fbdev: set gamma");
  141. }
  142. static void msm_crtc_fb_gamma_get(struct drm_crtc *crtc,
  143. u16 *red, u16 *green, u16 *blue, int regno)
  144. {
  145. DBG("fbdev: get gamma");
  146. }
  147. static struct drm_fb_helper_funcs msm_fb_helper_funcs = {
  148. .gamma_set = msm_crtc_fb_gamma_set,
  149. .gamma_get = msm_crtc_fb_gamma_get,
  150. .fb_probe = msm_fbdev_create,
  151. };
  152. /* initialize fbdev helper */
  153. struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
  154. {
  155. struct msm_drm_private *priv = dev->dev_private;
  156. struct msm_fbdev *fbdev = NULL;
  157. struct drm_fb_helper *helper;
  158. int ret = 0;
  159. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  160. if (!fbdev)
  161. goto fail;
  162. helper = &fbdev->base;
  163. helper->funcs = &msm_fb_helper_funcs;
  164. ret = drm_fb_helper_init(dev, helper,
  165. priv->num_crtcs, priv->num_connectors);
  166. if (ret) {
  167. dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
  168. goto fail;
  169. }
  170. drm_fb_helper_single_add_all_connectors(helper);
  171. /* disable all the possible outputs/crtcs before entering KMS mode */
  172. drm_helper_disable_unused_functions(dev);
  173. drm_fb_helper_initial_config(helper, 32);
  174. priv->fbdev = helper;
  175. return helper;
  176. fail:
  177. kfree(fbdev);
  178. return NULL;
  179. }
  180. void msm_fbdev_free(struct drm_device *dev)
  181. {
  182. struct msm_drm_private *priv = dev->dev_private;
  183. struct drm_fb_helper *helper = priv->fbdev;
  184. struct msm_fbdev *fbdev;
  185. struct fb_info *fbi;
  186. DBG();
  187. fbi = helper->fbdev;
  188. /* only cleanup framebuffer if it is present */
  189. if (fbi) {
  190. unregister_framebuffer(fbi);
  191. framebuffer_release(fbi);
  192. }
  193. drm_fb_helper_fini(helper);
  194. fbdev = to_msm_fbdev(priv->fbdev);
  195. /* this will free the backing object */
  196. if (fbdev->fb) {
  197. drm_framebuffer_unregister_private(fbdev->fb);
  198. drm_framebuffer_remove(fbdev->fb);
  199. }
  200. kfree(fbdev);
  201. priv->fbdev = NULL;
  202. }