omap_fbdev.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_fbdev.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Author: Rob Clark <rob@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <drm/drm_crtc.h>
  20. #include <drm/drm_fb_helper.h>
  21. #include "omap_drv.h"
  22. MODULE_PARM_DESC(ywrap, "Enable ywrap scrolling (omap44xx and later, default 'y')");
  23. static bool ywrap_enabled = true;
  24. module_param_named(ywrap, ywrap_enabled, bool, 0644);
  25. /*
  26. * fbdev funcs, to implement legacy fbdev interface on top of drm driver
  27. */
  28. #define to_omap_fbdev(x) container_of(x, struct omap_fbdev, base)
  29. struct omap_fbdev {
  30. struct drm_fb_helper base;
  31. struct drm_framebuffer *fb;
  32. struct drm_gem_object *bo;
  33. bool ywrap_enabled;
  34. /* for deferred dmm roll when getting called in atomic ctx */
  35. struct work_struct work;
  36. };
  37. static struct drm_fb_helper *get_fb(struct fb_info *fbi);
  38. static void pan_worker(struct work_struct *work)
  39. {
  40. struct omap_fbdev *fbdev = container_of(work, struct omap_fbdev, work);
  41. struct fb_info *fbi = fbdev->base.fbdev;
  42. int npages;
  43. /* DMM roll shifts in 4K pages: */
  44. npages = fbi->fix.line_length >> PAGE_SHIFT;
  45. omap_gem_roll(fbdev->bo, fbi->var.yoffset * npages);
  46. }
  47. static int omap_fbdev_pan_display(struct fb_var_screeninfo *var,
  48. struct fb_info *fbi)
  49. {
  50. struct drm_fb_helper *helper = get_fb(fbi);
  51. struct omap_fbdev *fbdev = to_omap_fbdev(helper);
  52. if (!helper)
  53. goto fallback;
  54. if (!fbdev->ywrap_enabled)
  55. goto fallback;
  56. if (drm_can_sleep()) {
  57. pan_worker(&fbdev->work);
  58. } else {
  59. struct omap_drm_private *priv = helper->dev->dev_private;
  60. queue_work(priv->wq, &fbdev->work);
  61. }
  62. return 0;
  63. fallback:
  64. return drm_fb_helper_pan_display(var, fbi);
  65. }
  66. static struct fb_ops omap_fb_ops = {
  67. .owner = THIS_MODULE,
  68. /* Note: to properly handle manual update displays, we wrap the
  69. * basic fbdev ops which write to the framebuffer
  70. */
  71. .fb_read = fb_sys_read,
  72. .fb_write = fb_sys_write,
  73. .fb_fillrect = sys_fillrect,
  74. .fb_copyarea = sys_copyarea,
  75. .fb_imageblit = sys_imageblit,
  76. .fb_check_var = drm_fb_helper_check_var,
  77. .fb_set_par = drm_fb_helper_set_par,
  78. .fb_pan_display = omap_fbdev_pan_display,
  79. .fb_blank = drm_fb_helper_blank,
  80. .fb_setcmap = drm_fb_helper_setcmap,
  81. };
  82. static int omap_fbdev_create(struct drm_fb_helper *helper,
  83. struct drm_fb_helper_surface_size *sizes)
  84. {
  85. struct omap_fbdev *fbdev = to_omap_fbdev(helper);
  86. struct drm_device *dev = helper->dev;
  87. struct omap_drm_private *priv = dev->dev_private;
  88. struct drm_framebuffer *fb = NULL;
  89. union omap_gem_size gsize;
  90. struct fb_info *fbi = NULL;
  91. struct drm_mode_fb_cmd2 mode_cmd = {0};
  92. dma_addr_t paddr;
  93. int ret;
  94. /* only doing ARGB32 since this is what is needed to alpha-blend
  95. * with video overlays:
  96. */
  97. sizes->surface_bpp = 32;
  98. sizes->surface_depth = 32;
  99. DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
  100. sizes->surface_height, sizes->surface_bpp,
  101. sizes->fb_width, sizes->fb_height);
  102. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  103. sizes->surface_depth);
  104. mode_cmd.width = sizes->surface_width;
  105. mode_cmd.height = sizes->surface_height;
  106. mode_cmd.pitches[0] = align_pitch(
  107. mode_cmd.width * ((sizes->surface_bpp + 7) / 8),
  108. mode_cmd.width, sizes->surface_bpp);
  109. fbdev->ywrap_enabled = priv->has_dmm && ywrap_enabled;
  110. if (fbdev->ywrap_enabled) {
  111. /* need to align pitch to page size if using DMM scrolling */
  112. mode_cmd.pitches[0] = ALIGN(mode_cmd.pitches[0], PAGE_SIZE);
  113. }
  114. /* allocate backing bo */
  115. gsize = (union omap_gem_size){
  116. .bytes = PAGE_ALIGN(mode_cmd.pitches[0] * mode_cmd.height),
  117. };
  118. DBG("allocating %d bytes for fb %d", gsize.bytes, dev->primary->index);
  119. fbdev->bo = omap_gem_new(dev, gsize, OMAP_BO_SCANOUT | OMAP_BO_WC);
  120. if (!fbdev->bo) {
  121. dev_err(dev->dev, "failed to allocate buffer object\n");
  122. ret = -ENOMEM;
  123. goto fail;
  124. }
  125. fb = omap_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
  126. if (IS_ERR(fb)) {
  127. dev_err(dev->dev, "failed to allocate fb\n");
  128. /* note: if fb creation failed, we can't rely on fb destroy
  129. * to unref the bo:
  130. */
  131. drm_gem_object_unreference(fbdev->bo);
  132. ret = PTR_ERR(fb);
  133. goto fail;
  134. }
  135. /* note: this keeps the bo pinned.. which is perhaps not ideal,
  136. * but is needed as long as we use fb_mmap() to mmap to userspace
  137. * (since this happens using fix.smem_start). Possibly we could
  138. * implement our own mmap using GEM mmap support to avoid this
  139. * (non-tiled buffer doesn't need to be pinned for fbcon to write
  140. * to it). Then we just need to be sure that we are able to re-
  141. * pin it in case of an opps.
  142. */
  143. ret = omap_gem_get_paddr(fbdev->bo, &paddr, true);
  144. if (ret) {
  145. dev_err(dev->dev,
  146. "could not map (paddr)! Skipping framebuffer alloc\n");
  147. ret = -ENOMEM;
  148. goto fail;
  149. }
  150. mutex_lock(&dev->struct_mutex);
  151. fbi = framebuffer_alloc(0, dev->dev);
  152. if (!fbi) {
  153. dev_err(dev->dev, "failed to allocate fb info\n");
  154. ret = -ENOMEM;
  155. goto fail_unlock;
  156. }
  157. DBG("fbi=%p, dev=%p", fbi, dev);
  158. fbdev->fb = fb;
  159. helper->fb = fb;
  160. helper->fbdev = fbi;
  161. fbi->par = helper;
  162. fbi->flags = FBINFO_DEFAULT;
  163. fbi->fbops = &omap_fb_ops;
  164. strcpy(fbi->fix.id, MODULE_NAME);
  165. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  166. if (ret) {
  167. ret = -ENOMEM;
  168. goto fail_unlock;
  169. }
  170. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  171. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  172. dev->mode_config.fb_base = paddr;
  173. fbi->screen_base = omap_gem_vaddr(fbdev->bo);
  174. fbi->screen_size = fbdev->bo->size;
  175. fbi->fix.smem_start = paddr;
  176. fbi->fix.smem_len = fbdev->bo->size;
  177. /* if we have DMM, then we can use it for scrolling by just
  178. * shuffling pages around in DMM rather than doing sw blit.
  179. */
  180. if (fbdev->ywrap_enabled) {
  181. DRM_INFO("Enabling DMM ywrap scrolling\n");
  182. fbi->flags |= FBINFO_HWACCEL_YWRAP | FBINFO_READS_FAST;
  183. fbi->fix.ywrapstep = 1;
  184. }
  185. DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
  186. DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
  187. mutex_unlock(&dev->struct_mutex);
  188. return 0;
  189. fail_unlock:
  190. mutex_unlock(&dev->struct_mutex);
  191. fail:
  192. if (ret) {
  193. if (fbi)
  194. framebuffer_release(fbi);
  195. if (fb) {
  196. drm_framebuffer_unregister_private(fb);
  197. drm_framebuffer_remove(fb);
  198. }
  199. }
  200. return ret;
  201. }
  202. static const struct drm_fb_helper_funcs omap_fb_helper_funcs = {
  203. .fb_probe = omap_fbdev_create,
  204. };
  205. static struct drm_fb_helper *get_fb(struct fb_info *fbi)
  206. {
  207. if (!fbi || strcmp(fbi->fix.id, MODULE_NAME)) {
  208. /* these are not the fb's you're looking for */
  209. return NULL;
  210. }
  211. return fbi->par;
  212. }
  213. /* initialize fbdev helper */
  214. struct drm_fb_helper *omap_fbdev_init(struct drm_device *dev)
  215. {
  216. struct omap_drm_private *priv = dev->dev_private;
  217. struct omap_fbdev *fbdev = NULL;
  218. struct drm_fb_helper *helper;
  219. int ret = 0;
  220. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  221. if (!fbdev)
  222. goto fail;
  223. INIT_WORK(&fbdev->work, pan_worker);
  224. helper = &fbdev->base;
  225. drm_fb_helper_prepare(dev, helper, &omap_fb_helper_funcs);
  226. ret = drm_fb_helper_init(dev, helper,
  227. priv->num_crtcs, priv->num_connectors);
  228. if (ret) {
  229. dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
  230. goto fail;
  231. }
  232. ret = drm_fb_helper_single_add_all_connectors(helper);
  233. if (ret)
  234. goto fini;
  235. /* disable all the possible outputs/crtcs before entering KMS mode */
  236. drm_helper_disable_unused_functions(dev);
  237. ret = drm_fb_helper_initial_config(helper, 32);
  238. if (ret)
  239. goto fini;
  240. priv->fbdev = helper;
  241. return helper;
  242. fini:
  243. drm_fb_helper_fini(helper);
  244. fail:
  245. kfree(fbdev);
  246. return NULL;
  247. }
  248. void omap_fbdev_free(struct drm_device *dev)
  249. {
  250. struct omap_drm_private *priv = dev->dev_private;
  251. struct drm_fb_helper *helper = priv->fbdev;
  252. struct omap_fbdev *fbdev;
  253. struct fb_info *fbi;
  254. DBG();
  255. fbi = helper->fbdev;
  256. /* only cleanup framebuffer if it is present */
  257. if (fbi) {
  258. unregister_framebuffer(fbi);
  259. framebuffer_release(fbi);
  260. }
  261. drm_fb_helper_fini(helper);
  262. fbdev = to_omap_fbdev(priv->fbdev);
  263. /* release the ref taken in omap_fbdev_create() */
  264. omap_gem_put_paddr(fbdev->bo);
  265. /* this will free the backing object */
  266. if (fbdev->fb) {
  267. drm_framebuffer_unregister_private(fbdev->fb);
  268. drm_framebuffer_remove(fbdev->fb);
  269. }
  270. kfree(fbdev);
  271. priv->fbdev = NULL;
  272. }