msm_fbdev.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <[email protected]>
  5. */
  6. #include <drm/drm_aperture.h>
  7. #include <drm/drm_crtc.h>
  8. #include <drm/drm_fb_helper.h>
  9. #include <drm/drm_fourcc.h>
  10. #include <drm/drm_framebuffer.h>
  11. #include <drm/drm_prime.h>
  12. #include "msm_drv.h"
  13. #include "msm_gem.h"
  14. #include "msm_kms.h"
  15. static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma);
  16. /*
  17. * fbdev funcs, to implement legacy fbdev interface on top of drm driver
  18. */
  19. #define to_msm_fbdev(x) container_of(x, struct msm_fbdev, base)
  20. struct msm_fbdev {
  21. struct drm_fb_helper base;
  22. struct drm_framebuffer *fb;
  23. };
  24. static const struct fb_ops msm_fb_ops = {
  25. .owner = THIS_MODULE,
  26. DRM_FB_HELPER_DEFAULT_OPS,
  27. /* Note: to properly handle manual update displays, we wrap the
  28. * basic fbdev ops which write to the framebuffer
  29. */
  30. .fb_read = drm_fb_helper_sys_read,
  31. .fb_write = drm_fb_helper_sys_write,
  32. .fb_fillrect = drm_fb_helper_sys_fillrect,
  33. .fb_copyarea = drm_fb_helper_sys_copyarea,
  34. .fb_imageblit = drm_fb_helper_sys_imageblit,
  35. .fb_mmap = msm_fbdev_mmap,
  36. };
  37. static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
  38. {
  39. struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
  40. struct msm_fbdev *fbdev = to_msm_fbdev(helper);
  41. struct drm_gem_object *bo = msm_framebuffer_bo(fbdev->fb, 0);
  42. return drm_gem_prime_mmap(bo, vma);
  43. }
  44. static int msm_fbdev_create(struct drm_fb_helper *helper,
  45. struct drm_fb_helper_surface_size *sizes)
  46. {
  47. struct msm_fbdev *fbdev = to_msm_fbdev(helper);
  48. struct drm_device *dev = helper->dev;
  49. struct msm_drm_private *priv = dev->dev_private;
  50. struct drm_framebuffer *fb = NULL;
  51. struct drm_gem_object *bo;
  52. struct fb_info *fbi = NULL;
  53. uint64_t paddr;
  54. uint32_t format;
  55. int ret, pitch;
  56. format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
  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. pitch = align_pitch(sizes->surface_width, sizes->surface_bpp);
  61. fb = msm_alloc_stolen_fb(dev, sizes->surface_width,
  62. sizes->surface_height, pitch, format);
  63. if (IS_ERR(fb)) {
  64. DRM_DEV_ERROR(dev->dev, "failed to allocate fb\n");
  65. return PTR_ERR(fb);
  66. }
  67. bo = msm_framebuffer_bo(fb, 0);
  68. /*
  69. * NOTE: if we can be guaranteed to be able to map buffer
  70. * in panic (ie. lock-safe, etc) we could avoid pinning the
  71. * buffer now:
  72. */
  73. ret = msm_gem_get_and_pin_iova(bo, priv->kms->aspace, &paddr);
  74. if (ret) {
  75. DRM_DEV_ERROR(dev->dev, "failed to get buffer obj iova: %d\n", ret);
  76. goto fail;
  77. }
  78. fbi = drm_fb_helper_alloc_fbi(helper);
  79. if (IS_ERR(fbi)) {
  80. DRM_DEV_ERROR(dev->dev, "failed to allocate fb info\n");
  81. ret = PTR_ERR(fbi);
  82. goto fail;
  83. }
  84. DBG("fbi=%p, dev=%p", fbi, dev);
  85. fbdev->fb = fb;
  86. helper->fb = fb;
  87. fbi->fbops = &msm_fb_ops;
  88. drm_fb_helper_fill_info(fbi, helper, sizes);
  89. dev->mode_config.fb_base = paddr;
  90. fbi->screen_base = msm_gem_get_vaddr(bo);
  91. if (IS_ERR(fbi->screen_base)) {
  92. ret = PTR_ERR(fbi->screen_base);
  93. goto fail;
  94. }
  95. fbi->screen_size = bo->size;
  96. fbi->fix.smem_start = paddr;
  97. fbi->fix.smem_len = bo->size;
  98. DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
  99. DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
  100. return 0;
  101. fail:
  102. drm_framebuffer_remove(fb);
  103. return ret;
  104. }
  105. static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
  106. .fb_probe = msm_fbdev_create,
  107. };
  108. /* initialize fbdev helper */
  109. struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
  110. {
  111. struct msm_drm_private *priv = dev->dev_private;
  112. struct msm_fbdev *fbdev = NULL;
  113. struct drm_fb_helper *helper;
  114. int ret;
  115. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  116. if (!fbdev)
  117. goto fail;
  118. helper = &fbdev->base;
  119. drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
  120. ret = drm_fb_helper_init(dev, helper);
  121. if (ret) {
  122. DRM_DEV_ERROR(dev->dev, "could not init fbdev: ret=%d\n", ret);
  123. goto fail;
  124. }
  125. /* the fw fb could be anywhere in memory */
  126. ret = drm_aperture_remove_framebuffers(false, dev->driver);
  127. if (ret)
  128. goto fini;
  129. ret = drm_fb_helper_initial_config(helper, 32);
  130. if (ret)
  131. goto fini;
  132. priv->fbdev = helper;
  133. return helper;
  134. fini:
  135. drm_fb_helper_fini(helper);
  136. fail:
  137. kfree(fbdev);
  138. return NULL;
  139. }
  140. void msm_fbdev_free(struct drm_device *dev)
  141. {
  142. struct msm_drm_private *priv = dev->dev_private;
  143. struct drm_fb_helper *helper = priv->fbdev;
  144. struct msm_fbdev *fbdev;
  145. DBG();
  146. drm_fb_helper_unregister_fbi(helper);
  147. drm_fb_helper_fini(helper);
  148. fbdev = to_msm_fbdev(priv->fbdev);
  149. /* this will free the backing object */
  150. if (fbdev->fb) {
  151. struct drm_gem_object *bo =
  152. msm_framebuffer_bo(fbdev->fb, 0);
  153. msm_gem_put_vaddr(bo);
  154. drm_framebuffer_remove(fbdev->fb);
  155. }
  156. kfree(fbdev);
  157. priv->fbdev = NULL;
  158. }