msm_fbdev.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #include "msm_gem.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. struct drm_gem_object *bo;
  32. };
  33. static struct fb_ops msm_fb_ops = {
  34. .owner = THIS_MODULE,
  35. /* Note: to properly handle manual update displays, we wrap the
  36. * basic fbdev ops which write to the framebuffer
  37. */
  38. .fb_read = fb_sys_read,
  39. .fb_write = fb_sys_write,
  40. .fb_fillrect = sys_fillrect,
  41. .fb_copyarea = sys_copyarea,
  42. .fb_imageblit = sys_imageblit,
  43. .fb_mmap = msm_fbdev_mmap,
  44. .fb_check_var = drm_fb_helper_check_var,
  45. .fb_set_par = drm_fb_helper_set_par,
  46. .fb_pan_display = drm_fb_helper_pan_display,
  47. .fb_blank = drm_fb_helper_blank,
  48. .fb_setcmap = drm_fb_helper_setcmap,
  49. };
  50. static int msm_fbdev_mmap(struct fb_info *info, struct vm_area_struct *vma)
  51. {
  52. struct drm_fb_helper *helper = (struct drm_fb_helper *)info->par;
  53. struct msm_fbdev *fbdev = to_msm_fbdev(helper);
  54. struct drm_gem_object *drm_obj = fbdev->bo;
  55. struct drm_device *dev = helper->dev;
  56. int ret = 0;
  57. if (drm_device_is_unplugged(dev))
  58. return -ENODEV;
  59. mutex_lock(&dev->struct_mutex);
  60. ret = drm_gem_mmap_obj(drm_obj, drm_obj->size, vma);
  61. mutex_unlock(&dev->struct_mutex);
  62. if (ret) {
  63. pr_err("%s:drm_gem_mmap_obj fail\n", __func__);
  64. return ret;
  65. }
  66. return msm_gem_mmap_obj(drm_obj, vma);
  67. }
  68. static int msm_fbdev_create(struct drm_fb_helper *helper,
  69. struct drm_fb_helper_surface_size *sizes)
  70. {
  71. struct msm_fbdev *fbdev = to_msm_fbdev(helper);
  72. struct drm_device *dev = helper->dev;
  73. struct drm_framebuffer *fb = NULL;
  74. struct fb_info *fbi = NULL;
  75. struct drm_mode_fb_cmd2 mode_cmd = {0};
  76. uint32_t paddr;
  77. int ret, size;
  78. sizes->surface_bpp = 32;
  79. sizes->surface_depth = 24;
  80. DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
  81. sizes->surface_height, sizes->surface_bpp,
  82. sizes->fb_width, sizes->fb_height);
  83. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  84. sizes->surface_depth);
  85. mode_cmd.width = sizes->surface_width;
  86. mode_cmd.height = sizes->surface_height;
  87. mode_cmd.pitches[0] = align_pitch(
  88. mode_cmd.width, sizes->surface_bpp);
  89. /* allocate backing bo */
  90. size = mode_cmd.pitches[0] * mode_cmd.height;
  91. DBG("allocating %d bytes for fb %d", size, dev->primary->index);
  92. mutex_lock(&dev->struct_mutex);
  93. fbdev->bo = msm_gem_new(dev, size, MSM_BO_SCANOUT | MSM_BO_WC);
  94. mutex_unlock(&dev->struct_mutex);
  95. if (IS_ERR(fbdev->bo)) {
  96. ret = PTR_ERR(fbdev->bo);
  97. fbdev->bo = NULL;
  98. dev_err(dev->dev, "failed to allocate buffer object: %d\n", ret);
  99. goto fail;
  100. }
  101. fb = msm_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
  102. if (IS_ERR(fb)) {
  103. dev_err(dev->dev, "failed to allocate fb\n");
  104. /* note: if fb creation failed, we can't rely on fb destroy
  105. * to unref the bo:
  106. */
  107. drm_gem_object_unreference(fbdev->bo);
  108. ret = PTR_ERR(fb);
  109. goto fail;
  110. }
  111. mutex_lock(&dev->struct_mutex);
  112. /*
  113. * NOTE: if we can be guaranteed to be able to map buffer
  114. * in panic (ie. lock-safe, etc) we could avoid pinning the
  115. * buffer now:
  116. */
  117. ret = msm_gem_get_iova_locked(fbdev->bo, 0, &paddr);
  118. if (ret) {
  119. dev_err(dev->dev, "failed to get buffer obj iova: %d\n", ret);
  120. goto fail_unlock;
  121. }
  122. fbi = framebuffer_alloc(0, dev->dev);
  123. if (!fbi) {
  124. dev_err(dev->dev, "failed to allocate fb info\n");
  125. ret = -ENOMEM;
  126. goto fail_unlock;
  127. }
  128. DBG("fbi=%p, dev=%p", fbi, dev);
  129. fbdev->fb = fb;
  130. helper->fb = fb;
  131. helper->fbdev = fbi;
  132. fbi->par = helper;
  133. fbi->flags = FBINFO_DEFAULT;
  134. fbi->fbops = &msm_fb_ops;
  135. strcpy(fbi->fix.id, "msm");
  136. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  137. if (ret) {
  138. ret = -ENOMEM;
  139. goto fail_unlock;
  140. }
  141. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  142. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  143. dev->mode_config.fb_base = paddr;
  144. fbi->screen_base = msm_gem_vaddr_locked(fbdev->bo);
  145. fbi->screen_size = fbdev->bo->size;
  146. fbi->fix.smem_start = paddr;
  147. fbi->fix.smem_len = fbdev->bo->size;
  148. DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
  149. DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
  150. mutex_unlock(&dev->struct_mutex);
  151. return 0;
  152. fail_unlock:
  153. mutex_unlock(&dev->struct_mutex);
  154. fail:
  155. if (ret) {
  156. if (fbi)
  157. framebuffer_release(fbi);
  158. if (fb) {
  159. drm_framebuffer_unregister_private(fb);
  160. drm_framebuffer_remove(fb);
  161. }
  162. }
  163. return ret;
  164. }
  165. static void msm_crtc_fb_gamma_set(struct drm_crtc *crtc,
  166. u16 red, u16 green, u16 blue, int regno)
  167. {
  168. DBG("fbdev: set gamma");
  169. }
  170. static void msm_crtc_fb_gamma_get(struct drm_crtc *crtc,
  171. u16 *red, u16 *green, u16 *blue, int regno)
  172. {
  173. DBG("fbdev: get gamma");
  174. }
  175. static const struct drm_fb_helper_funcs msm_fb_helper_funcs = {
  176. .gamma_set = msm_crtc_fb_gamma_set,
  177. .gamma_get = msm_crtc_fb_gamma_get,
  178. .fb_probe = msm_fbdev_create,
  179. };
  180. /* initialize fbdev helper */
  181. struct drm_fb_helper *msm_fbdev_init(struct drm_device *dev)
  182. {
  183. struct msm_drm_private *priv = dev->dev_private;
  184. struct msm_fbdev *fbdev = NULL;
  185. struct drm_fb_helper *helper;
  186. int ret;
  187. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  188. if (!fbdev)
  189. goto fail;
  190. helper = &fbdev->base;
  191. drm_fb_helper_prepare(dev, helper, &msm_fb_helper_funcs);
  192. ret = drm_fb_helper_init(dev, helper,
  193. priv->num_crtcs, priv->num_connectors);
  194. if (ret) {
  195. dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
  196. goto fail;
  197. }
  198. drm_fb_helper_single_add_all_connectors(helper);
  199. /* disable all the possible outputs/crtcs before entering KMS mode */
  200. drm_helper_disable_unused_functions(dev);
  201. drm_fb_helper_initial_config(helper, 32);
  202. priv->fbdev = helper;
  203. return helper;
  204. fail:
  205. kfree(fbdev);
  206. return NULL;
  207. }
  208. void msm_fbdev_free(struct drm_device *dev)
  209. {
  210. struct msm_drm_private *priv = dev->dev_private;
  211. struct drm_fb_helper *helper = priv->fbdev;
  212. struct msm_fbdev *fbdev;
  213. struct fb_info *fbi;
  214. DBG();
  215. fbi = helper->fbdev;
  216. /* only cleanup framebuffer if it is present */
  217. if (fbi) {
  218. unregister_framebuffer(fbi);
  219. framebuffer_release(fbi);
  220. }
  221. drm_fb_helper_fini(helper);
  222. fbdev = to_msm_fbdev(priv->fbdev);
  223. /* this will free the backing object */
  224. if (fbdev->fb) {
  225. drm_framebuffer_unregister_private(fbdev->fb);
  226. drm_framebuffer_remove(fbdev->fb);
  227. }
  228. kfree(fbdev);
  229. priv->fbdev = NULL;
  230. }