msm_fbdev.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <drm/drm_crtc.h>
  18. #include <drm/drm_fb_helper.h>
  19. #include "msm_drv.h"
  20. #include "msm_kms.h"
  21. extern int msm_gem_mmap_obj(struct drm_gem_object *obj,
  22. struct vm_area_struct *vma);
  23. static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
  24. /*
  25. * fbdev funcs, to implement legacy fbdev interface on top of drm driver
  26. */
  27. #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
  28. struct msm_fbdev {
  29. struct drm_fb_helper base;
  30. struct drm_framebuffer *fb;
  31. };
  32. static struct fb_ops msm_fb_ops = {
  33. .owner = THIS_MODULE,
  34. DRM_FB_HELPER_DEFAULT_OPS,
  35. /* Note: to properly handle manual update displays, we wrap the
  36. * basic fbdev ops which write to the framebuffer
  37. */
  38. .fb_read = drm_fb_helper_sys_read,
  39. .fb_write = drm_fb_helper_sys_write,
  40. .fb_fillrect = drm_fb_helper_sys_fillrect,
  41. .fb_copyarea = drm_fb_helper_sys_copyarea,
  42. .fb_imageblit = drm_fb_helper_sys_imageblit,
  43. .fb_mmap = msm_fbdev_mmap,
  44. };
  45. static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
  46. {
  47. struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
  48. struct msm_fbdev *fbdev = to_msm_fbdev(helper);
  49. struct drm_gem_object *bo = msm_framebuffer_bo(fbdev->fb, 0);
  50. int ret = 0;
  51. ret = drm_gem_mmap_obj(bo, bo->size, vma);
  52. if (ret) {
  53. pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
  54. return ret;
  55. }
  56. return msm_gem_mmap_obj(bo, vma);
  57. }
  58. static int msm_fbdev_create(struct drm_fb_helper *helper,
  59. struct drm_fb_helper_surface_size *sizes)
  60. {
  61. struct msm_fbdev *fbdev = to_msm_fbdev(helper);
  62. struct drm_device *dev = helper->dev;
  63. struct msm_drm_private *priv = dev->dev_private;
  64. struct drm_framebuffer *fb = NULL;
  65. struct drm_gem_object *bo;
  66. struct fb_info *fbi = NULL;
  67. uint64_t paddr;
  68. uint32_t format;
  69. int ret, pitch;
  70. format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
  71. DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
  72. sizes->surface_height, sizes->surface_bpp,
  73. sizes->fb_width, sizes->fb_height);
  74. pitch = align_pitch(sizes->surface_width, sizes->surface_bpp);
  75. fb = msm_alloc_stolen_fb(dev, sizes->surface_width,
  76. sizes->surface_height, pitch, format);
  77. if (IS_ERR(fb)) {
  78. dev_err(dev->dev, "failed to allocate fb\n");
  79. return PTR_ERR(fb);
  80. }
  81. bo = msm_framebuffer_bo(fb, 0);
  82. mutex_lock(&dev->struct_mutex);
  83. /*
  84. * NOTE: if we can be guaranteed to be able to map buffer
  85. * in panic (ie. lock-safe, etc) we could avoid pinning the
  86. * buffer now:
  87. */
  88. ret = msm_gem_get_iova(bo, priv->kms->aspace, &paddr);
  89. if (ret) {
  90. dev_err(dev->dev, "failed to get buffer obj iova: %d\n", ret);
  91. goto fail_unlock;
  92. }
  93. fbi = drm_fb_helper_alloc_fbi(helper);
  94. if (IS_ERR(fbi)) {
  95. dev_err(dev->dev, "failed to allocate fb info\n");
  96. ret = PTR_ERR(fbi);
  97. goto fail_unlock;
  98. }
  99. DBG("fbi=%p, dev=%p", fbi, dev);
  100. fbdev->fb = fb;
  101. helper->fb = fb;
  102. fbi->par = helper;
  103. fbi->fbops = &msm_fb_ops;
  104. strcpy(fbi->fix.id, "msm");
  105. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
  106. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  107. dev->mode_config.fb_base = paddr;
  108. fbi->screen_base = msm_gem_get_vaddr(bo);
  109. if (IS_ERR(fbi->screen_base)) {
  110. ret = PTR_ERR(fbi->screen_base);
  111. goto fail_unlock;
  112. }
  113. fbi->screen_size = bo->size;
  114. fbi->fix.smem_start = paddr;
  115. fbi->fix.smem_len = bo->size;
  116. DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
  117. DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
  118. mutex_unlock(&dev->struct_mutex);
  119. return 0;
  120. fail_unlock:
  121. mutex_unlock(&dev->struct_mutex);
  122. drm_framebuffer_remove(fb);
  123. return ret;
  124. }
  125. static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
  126. .fb_probe = msm_fbdev_create,
  127. };
  128. /* initialize fbdev helper */
  129. struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
  130. {
  131. struct msm_drm_private *priv = dev->dev_private;
  132. struct msm_fbdev *fbdev = NULL;
  133. struct drm_fb_helper *helper;
  134. int ret;
  135. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  136. if (!fbdev)
  137. goto fail;
  138. helper = &fbdev->base;
  139. drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
  140. ret = drm_fb_helper_init(dev, helper, priv->num_connectors);
  141. if (ret) {
  142. dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
  143. goto fail;
  144. }
  145. ret = drm_fb_helper_single_add_all_connectors(helper);
  146. if (ret)
  147. goto fini;
  148. ret = drm_fb_helper_initial_config(helper, 32);
  149. if (ret)
  150. goto fini;
  151. priv->fbdev = helper;
  152. return helper;
  153. fini:
  154. drm_fb_helper_fini(helper);
  155. fail:
  156. kfree(fbdev);
  157. return NULL;
  158. }
  159. void msm_fbdev_free(struct drm_device *dev)
  160. {
  161. struct msm_drm_private *priv = dev->dev_private;
  162. struct drm_fb_helper *helper = priv->fbdev;
  163. struct msm_fbdev *fbdev;
  164. DBG();
  165. drm_fb_helper_unregister_fbi(helper);
  166. drm_fb_helper_fini(helper);
  167. fbdev = to_msm_fbdev(priv->fbdev);
  168. /* this will free the backing object */
  169. if (fbdev->fb) {
  170. struct drm_gem_object *bo =
  171. msm_framebuffer_bo(fbdev->fb, 0);
  172. msm_gem_put_vaddr(bo);
  173. drm_framebuffer_remove(fbdev->fb);
  174. }
  175. kfree(fbdev);
  176. priv->fbdev = NULL;
  177. }