msm_fbdev.c 6.2 KB

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