omap_fbdev.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Texas Instruments Incorporated - https://www.ti.com/
  4. * Author: Rob Clark <[email protected]>
  5. */
  6. #include <drm/drm_crtc.h>
  7. #include <drm/drm_util.h>
  8. #include <drm/drm_fb_helper.h>
  9. #include <drm/drm_file.h>
  10. #include <drm/drm_fourcc.h>
  11. #include <drm/drm_framebuffer.h>
  12. #include "omap_drv.h"
  13. MODULE_PARM_DESC(ywrap, "Enable ywrap scrolling (omap44xx and later, default 'y')");
  14. static bool ywrap_enabled = true;
  15. module_param_named(ywrap, ywrap_enabled, bool, 0644);
  16. /*
  17. * fbdev funcs, to implement legacy fbdev interface on top of drm driver
  18. */
  19. #define to_omap_fbdev(x) container_of(x, struct omap_fbdev, base)
  20. struct omap_fbdev {
  21. struct drm_fb_helper base;
  22. struct drm_framebuffer *fb;
  23. struct drm_gem_object *bo;
  24. bool ywrap_enabled;
  25. /* for deferred dmm roll when getting called in atomic ctx */
  26. struct work_struct work;
  27. };
  28. static struct drm_fb_helper *get_fb(struct fb_info *fbi);
  29. static void pan_worker(struct work_struct *work)
  30. {
  31. struct omap_fbdev *fbdev = container_of(work, struct omap_fbdev, work);
  32. struct fb_info *fbi = fbdev->base.fbdev;
  33. int npages;
  34. /* DMM roll shifts in 4K pages: */
  35. npages = fbi->fix.line_length >> PAGE_SHIFT;
  36. omap_gem_roll(fbdev->bo, fbi->var.yoffset * npages);
  37. }
  38. static int omap_fbdev_pan_display(struct fb_var_screeninfo *var,
  39. struct fb_info *fbi)
  40. {
  41. struct drm_fb_helper *helper = get_fb(fbi);
  42. struct omap_fbdev *fbdev = to_omap_fbdev(helper);
  43. if (!helper)
  44. goto fallback;
  45. if (!fbdev->ywrap_enabled)
  46. goto fallback;
  47. if (drm_can_sleep()) {
  48. pan_worker(&fbdev->work);
  49. } else {
  50. struct omap_drm_private *priv = helper->dev->dev_private;
  51. queue_work(priv->wq, &fbdev->work);
  52. }
  53. return 0;
  54. fallback:
  55. return drm_fb_helper_pan_display(var, fbi);
  56. }
  57. static const struct fb_ops omap_fb_ops = {
  58. .owner = THIS_MODULE,
  59. .fb_check_var = drm_fb_helper_check_var,
  60. .fb_set_par = drm_fb_helper_set_par,
  61. .fb_setcmap = drm_fb_helper_setcmap,
  62. .fb_blank = drm_fb_helper_blank,
  63. .fb_pan_display = omap_fbdev_pan_display,
  64. .fb_ioctl = drm_fb_helper_ioctl,
  65. .fb_read = drm_fb_helper_sys_read,
  66. .fb_write = drm_fb_helper_sys_write,
  67. .fb_fillrect = drm_fb_helper_sys_fillrect,
  68. .fb_copyarea = drm_fb_helper_sys_copyarea,
  69. .fb_imageblit = drm_fb_helper_sys_imageblit,
  70. };
  71. static int omap_fbdev_create(struct drm_fb_helper *helper,
  72. struct drm_fb_helper_surface_size *sizes)
  73. {
  74. struct omap_fbdev *fbdev = to_omap_fbdev(helper);
  75. struct drm_device *dev = helper->dev;
  76. struct omap_drm_private *priv = dev->dev_private;
  77. struct drm_framebuffer *fb = NULL;
  78. union omap_gem_size gsize;
  79. struct fb_info *fbi = NULL;
  80. struct drm_mode_fb_cmd2 mode_cmd = {0};
  81. dma_addr_t dma_addr;
  82. int ret;
  83. sizes->surface_bpp = 32;
  84. sizes->surface_depth = 24;
  85. DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
  86. sizes->surface_height, sizes->surface_bpp,
  87. sizes->fb_width, sizes->fb_height);
  88. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  89. sizes->surface_depth);
  90. mode_cmd.width = sizes->surface_width;
  91. mode_cmd.height = sizes->surface_height;
  92. mode_cmd.pitches[0] =
  93. DIV_ROUND_UP(mode_cmd.width * sizes->surface_bpp, 8);
  94. fbdev->ywrap_enabled = priv->has_dmm && ywrap_enabled;
  95. if (fbdev->ywrap_enabled) {
  96. /* need to align pitch to page size if using DMM scrolling */
  97. mode_cmd.pitches[0] = PAGE_ALIGN(mode_cmd.pitches[0]);
  98. }
  99. /* allocate backing bo */
  100. gsize = (union omap_gem_size){
  101. .bytes = PAGE_ALIGN(mode_cmd.pitches[0] * mode_cmd.height),
  102. };
  103. DBG("allocating %d bytes for fb %d", gsize.bytes, dev->primary->index);
  104. fbdev->bo = omap_gem_new(dev, gsize, OMAP_BO_SCANOUT | OMAP_BO_WC);
  105. if (!fbdev->bo) {
  106. dev_err(dev->dev, "failed to allocate buffer object\n");
  107. ret = -ENOMEM;
  108. goto fail;
  109. }
  110. fb = omap_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
  111. if (IS_ERR(fb)) {
  112. dev_err(dev->dev, "failed to allocate fb\n");
  113. /* note: if fb creation failed, we can't rely on fb destroy
  114. * to unref the bo:
  115. */
  116. drm_gem_object_put(fbdev->bo);
  117. ret = PTR_ERR(fb);
  118. goto fail;
  119. }
  120. /* note: this keeps the bo pinned.. which is perhaps not ideal,
  121. * but is needed as long as we use fb_mmap() to mmap to userspace
  122. * (since this happens using fix.smem_start). Possibly we could
  123. * implement our own mmap using GEM mmap support to avoid this
  124. * (non-tiled buffer doesn't need to be pinned for fbcon to write
  125. * to it). Then we just need to be sure that we are able to re-
  126. * pin it in case of an opps.
  127. */
  128. ret = omap_gem_pin(fbdev->bo, &dma_addr);
  129. if (ret) {
  130. dev_err(dev->dev, "could not pin framebuffer\n");
  131. ret = -ENOMEM;
  132. goto fail;
  133. }
  134. fbi = drm_fb_helper_alloc_fbi(helper);
  135. if (IS_ERR(fbi)) {
  136. dev_err(dev->dev, "failed to allocate fb info\n");
  137. ret = PTR_ERR(fbi);
  138. goto fail;
  139. }
  140. DBG("fbi=%p, dev=%p", fbi, dev);
  141. fbdev->fb = fb;
  142. helper->fb = fb;
  143. fbi->fbops = &omap_fb_ops;
  144. drm_fb_helper_fill_info(fbi, helper, sizes);
  145. dev->mode_config.fb_base = dma_addr;
  146. fbi->screen_buffer = omap_gem_vaddr(fbdev->bo);
  147. fbi->screen_size = fbdev->bo->size;
  148. fbi->fix.smem_start = dma_addr;
  149. fbi->fix.smem_len = fbdev->bo->size;
  150. /* if we have DMM, then we can use it for scrolling by just
  151. * shuffling pages around in DMM rather than doing sw blit.
  152. */
  153. if (fbdev->ywrap_enabled) {
  154. DRM_INFO("Enabling DMM ywrap scrolling\n");
  155. fbi->flags |= FBINFO_HWACCEL_YWRAP | FBINFO_READS_FAST;
  156. fbi->fix.ywrapstep = 1;
  157. }
  158. DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
  159. DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
  160. return 0;
  161. fail:
  162. if (ret) {
  163. if (fb)
  164. drm_framebuffer_remove(fb);
  165. }
  166. return ret;
  167. }
  168. static const struct drm_fb_helper_funcs omap_fb_helper_funcs = {
  169. .fb_probe = omap_fbdev_create,
  170. };
  171. static struct drm_fb_helper *get_fb(struct fb_info *fbi)
  172. {
  173. if (!fbi || strcmp(fbi->fix.id, MODULE_NAME)) {
  174. /* these are not the fb's you're looking for */
  175. return NULL;
  176. }
  177. return fbi->par;
  178. }
  179. /* initialize fbdev helper */
  180. void omap_fbdev_init(struct drm_device *dev)
  181. {
  182. struct omap_drm_private *priv = dev->dev_private;
  183. struct omap_fbdev *fbdev = NULL;
  184. struct drm_fb_helper *helper;
  185. int ret = 0;
  186. if (!priv->num_pipes)
  187. return;
  188. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  189. if (!fbdev)
  190. goto fail;
  191. INIT_WORK(&fbdev->work, pan_worker);
  192. helper = &fbdev->base;
  193. drm_fb_helper_prepare(dev, helper, &omap_fb_helper_funcs);
  194. ret = drm_fb_helper_init(dev, helper);
  195. if (ret)
  196. goto fail;
  197. ret = drm_fb_helper_initial_config(helper, 32);
  198. if (ret)
  199. goto fini;
  200. priv->fbdev = helper;
  201. return;
  202. fini:
  203. drm_fb_helper_fini(helper);
  204. fail:
  205. kfree(fbdev);
  206. dev_warn(dev->dev, "omap_fbdev_init failed\n");
  207. }
  208. void omap_fbdev_fini(struct drm_device *dev)
  209. {
  210. struct omap_drm_private *priv = dev->dev_private;
  211. struct drm_fb_helper *helper = priv->fbdev;
  212. struct omap_fbdev *fbdev;
  213. DBG();
  214. if (!helper)
  215. return;
  216. drm_fb_helper_unregister_fbi(helper);
  217. drm_fb_helper_fini(helper);
  218. fbdev = to_omap_fbdev(helper);
  219. /* unpin the GEM object pinned in omap_fbdev_create() */
  220. if (fbdev->bo)
  221. omap_gem_unpin(fbdev->bo);
  222. /* this will free the backing object */
  223. if (fbdev->fb)
  224. drm_framebuffer_remove(fbdev->fb);
  225. kfree(fbdev);
  226. priv->fbdev = NULL;
  227. }