omap_fbdev.c 7.8 KB

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