msm_fbdev.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. uint32_t paddr;
  54. int ret, size;
  55. sizes->surface_bpp = 32;
  56. sizes->surface_depth = 24;
  57. DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
  58. sizes->surface_height, sizes->surface_bpp,
  59. sizes->fb_width, sizes->fb_height);
  60. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  61. sizes->surface_depth);
  62. mode_cmd.width = sizes->surface_width;
  63. mode_cmd.height = sizes->surface_height;
  64. mode_cmd.pitches[0] = align_pitch(
  65. mode_cmd.width, sizes->surface_bpp);
  66. /* allocate backing bo */
  67. size = mode_cmd.pitches[0] * mode_cmd.height;
  68. DBG("allocating %d bytes for fb %d", size, dev->primary->index);
  69. mutex_lock(&dev->struct_mutex);
  70. fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
  71. mutex_unlock(&dev->struct_mutex);
  72. if (IS_ERR(fbdev->bo)) {
  73. ret = PTR_ERR(fbdev->bo);
  74. fbdev->bo = NULL;
  75. dev_err(dev->dev, "failed to allocate buffer object: %d\n", ret);
  76. goto fail;
  77. }
  78. fb = msm_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
  79. if (IS_ERR(fb)) {
  80. dev_err(dev->dev, "failed to allocate fb\n");
  81. /* note: if fb creation failed, we can't rely on fb destroy
  82. * to unref the bo:
  83. */
  84. drm_gem_object_unreference(fbdev->bo);
  85. ret = PTR_ERR(fb);
  86. goto fail;
  87. }
  88. mutex_lock(&dev->struct_mutex);
  89. /* TODO implement our own fb_mmap so we don't need this: */
  90. msm_gem_get_iova_locked(fbdev->bo, 0, &paddr);
  91. fbi = framebuffer_alloc(0, dev->dev);
  92. if (!fbi) {
  93. dev_err(dev->dev, "failed to allocate fb info\n");
  94. ret = -ENOMEM;
  95. goto fail_unlock;
  96. }
  97. DBG("fbi=%p, dev=%p", fbi, dev);
  98. fbdev->fb = fb;
  99. helper->fb = fb;
  100. helper->fbdev = fbi;
  101. fbi->par = helper;
  102. fbi->flags = FBINFO_DEFAULT;
  103. fbi->fbops = &msm_fb_ops;
  104. strcpy(fbi->fix.id, "msm");
  105. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  106. if (ret) {
  107. ret = -ENOMEM;
  108. goto fail_unlock;
  109. }
  110. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  111. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  112. dev->mode_config.fb_base = paddr;
  113. fbi->screen_base = msm_gem_vaddr_locked(fbdev->bo);
  114. fbi->screen_size = fbdev->bo->size;
  115. fbi->fix.smem_start = paddr;
  116. fbi->fix.smem_len = fbdev->bo->size;
  117. DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
  118. DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
  119. mutex_unlock(&dev->struct_mutex);
  120. return 0;
  121. fail_unlock:
  122. mutex_unlock(&dev->struct_mutex);
  123. fail:
  124. if (ret) {
  125. if (fbi)
  126. framebuffer_release(fbi);
  127. if (fb) {
  128. drm_framebuffer_unregister_private(fb);
  129. drm_framebuffer_remove(fb);
  130. }
  131. }
  132. return ret;
  133. }
  134. static void msm_crtc_fb_gamma_set(struct drm_crtc *crtc,
  135. u16 red, u16 green, u16 blue, int regno)
  136. {
  137. DBG("fbdev: set gamma");
  138. }
  139. static void msm_crtc_fb_gamma_get(struct drm_crtc *crtc,
  140. u16 *red, u16 *green, u16 *blue, int regno)
  141. {
  142. DBG("fbdev: get gamma");
  143. }
  144. static struct drm_fb_helper_funcs msm_fb_helper_funcs = {
  145. .gamma_set = msm_crtc_fb_gamma_set,
  146. .gamma_get = msm_crtc_fb_gamma_get,
  147. .fb_probe = msm_fbdev_create,
  148. };
  149. /* initialize fbdev helper */
  150. struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
  151. {
  152. struct msm_drm_private *priv = dev->dev_private;
  153. struct msm_fbdev *fbdev = NULL;
  154. struct drm_fb_helper *helper;
  155. int ret = 0;
  156. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  157. if (!fbdev)
  158. goto fail;
  159. helper = &fbdev->base;
  160. helper->funcs = &msm_fb_helper_funcs;
  161. ret = drm_fb_helper_init(dev, helper,
  162. priv->num_crtcs, priv->num_connectors);
  163. if (ret) {
  164. dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
  165. goto fail;
  166. }
  167. drm_fb_helper_single_add_all_connectors(helper);
  168. /* disable all the possible outputs/crtcs before entering KMS mode */
  169. drm_helper_disable_unused_functions(dev);
  170. drm_fb_helper_initial_config(helper, 32);
  171. priv->fbdev = helper;
  172. return helper;
  173. fail:
  174. kfree(fbdev);
  175. return NULL;
  176. }
  177. void msm_fbdev_free(struct drm_device *dev)
  178. {
  179. struct msm_drm_private *priv = dev->dev_private;
  180. struct drm_fb_helper *helper = priv->fbdev;
  181. struct msm_fbdev *fbdev;
  182. struct fb_info *fbi;
  183. DBG();
  184. fbi = helper->fbdev;
  185. /* only cleanup framebuffer if it is present */
  186. if (fbi) {
  187. unregister_framebuffer(fbi);
  188. framebuffer_release(fbi);
  189. }
  190. drm_fb_helper_fini(helper);
  191. fbdev = to_msm_fbdev(priv->fbdev);
  192. /* this will free the backing object */
  193. if (fbdev->fb) {
  194. drm_framebuffer_unregister_private(fbdev->fb);
  195. drm_framebuffer_remove(fbdev->fb);
  196. }
  197. kfree(fbdev);
  198. priv->fbdev = NULL;
  199. }