msm_fbdev.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. ret = PTR_ERR(fb);
  80. goto fail;
  81. }
  82. bo = msm_framebuffer_bo(fb, 0);
  83. mutex_lock(&dev->struct_mutex);
  84. /*
  85. * NOTE: if we can be guaranteed to be able to map buffer
  86. * in panic (ie. lock-safe, etc) we could avoid pinning the
  87. * buffer now:
  88. */
  89. ret = msm_gem_get_iova(bo, priv->kms->aspace, &paddr);
  90. if (ret) {
  91. dev_err(dev->dev, "failed to get buffer obj iova: %d\n", ret);
  92. goto fail_unlock;
  93. }
  94. fbi = drm_fb_helper_alloc_fbi(helper);
  95. if (IS_ERR(fbi)) {
  96. dev_err(dev->dev, "failed to allocate fb info\n");
  97. ret = PTR_ERR(fbi);
  98. goto fail_unlock;
  99. }
  100. DBG("fbi=%p, dev=%p", fbi, dev);
  101. fbdev->fb = fb;
  102. helper->fb = fb;
  103. fbi->par = helper;
  104. fbi->fbops = &msm_fb_ops;
  105. strcpy(fbi->fix.id, "msm");
  106. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
  107. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  108. dev->mode_config.fb_base = paddr;
  109. fbi->screen_base = msm_gem_get_vaddr(bo);
  110. if (IS_ERR(fbi->screen_base)) {
  111. ret = PTR_ERR(fbi->screen_base);
  112. goto fail_unlock;
  113. }
  114. fbi->screen_size = bo->size;
  115. fbi->fix.smem_start = paddr;
  116. fbi->fix.smem_len = 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 (fb)
  126. drm_framebuffer_remove(fb);
  127. }
  128. return ret;
  129. }
  130. static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
  131. .fb_probe = msm_fbdev_create,
  132. };
  133. /* initialize fbdev helper */
  134. struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
  135. {
  136. struct msm_drm_private *priv = dev->dev_private;
  137. struct msm_fbdev *fbdev = NULL;
  138. struct drm_fb_helper *helper;
  139. int ret;
  140. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  141. if (!fbdev)
  142. goto fail;
  143. helper = &fbdev->base;
  144. drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
  145. ret = drm_fb_helper_init(dev, helper, priv->num_connectors);
  146. if (ret) {
  147. dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
  148. goto fail;
  149. }
  150. ret = drm_fb_helper_single_add_all_connectors(helper);
  151. if (ret)
  152. goto fini;
  153. ret = drm_fb_helper_initial_config(helper, 32);
  154. if (ret)
  155. goto fini;
  156. priv->fbdev = helper;
  157. return helper;
  158. fini:
  159. drm_fb_helper_fini(helper);
  160. fail:
  161. kfree(fbdev);
  162. return NULL;
  163. }
  164. void msm_fbdev_free(struct drm_device *dev)
  165. {
  166. struct msm_drm_private *priv = dev->dev_private;
  167. struct drm_fb_helper *helper = priv->fbdev;
  168. struct msm_fbdev *fbdev;
  169. DBG();
  170. drm_fb_helper_unregister_fbi(helper);
  171. drm_fb_helper_fini(helper);
  172. fbdev = to_msm_fbdev(priv->fbdev);
  173. /* this will free the backing object */
  174. if (fbdev->fb) {
  175. struct drm_gem_object *bo =
  176. msm_framebuffer_bo(fbdev->fb, 0);
  177. msm_gem_put_vaddr(bo);
  178. drm_framebuffer_remove(fbdev->fb);
  179. }
  180. kfree(fbdev);
  181. priv->fbdev = NULL;
  182. }