omap_fbdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 "omap_drv.h"
  20. #include "drm_crtc.h"
  21. #include "drm_fb_helper.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 void omap_fbdev_flush(struct fb_info *fbi, int x, int y, int w, int h);
  38. static struct drm_fb_helper *get_fb(struct fb_info *fbi);
  39. static ssize_t omap_fbdev_write(struct fb_info *fbi, const char __user *buf,
  40. size_t count, loff_t *ppos)
  41. {
  42. ssize_t res;
  43. res = fb_sys_write(fbi, buf, count, ppos);
  44. omap_fbdev_flush(fbi, 0, 0, fbi->var.xres, fbi->var.yres);
  45. return res;
  46. }
  47. static void omap_fbdev_fillrect(struct fb_info *fbi,
  48. const struct fb_fillrect *rect)
  49. {
  50. sys_fillrect(fbi, rect);
  51. omap_fbdev_flush(fbi, rect->dx, rect->dy, rect->width, rect->height);
  52. }
  53. static void omap_fbdev_copyarea(struct fb_info *fbi,
  54. const struct fb_copyarea *area)
  55. {
  56. sys_copyarea(fbi, area);
  57. omap_fbdev_flush(fbi, area->dx, area->dy, area->width, area->height);
  58. }
  59. static void omap_fbdev_imageblit(struct fb_info *fbi,
  60. const struct fb_image *image)
  61. {
  62. sys_imageblit(fbi, image);
  63. omap_fbdev_flush(fbi, image->dx, image->dy,
  64. image->width, image->height);
  65. }
  66. static void pan_worker(struct work_struct *work)
  67. {
  68. struct omap_fbdev *fbdev = container_of(work, struct omap_fbdev, work);
  69. struct fb_info *fbi = fbdev->base.fbdev;
  70. int npages;
  71. /* DMM roll shifts in 4K pages: */
  72. npages = fbi->fix.line_length >> PAGE_SHIFT;
  73. omap_gem_roll(fbdev->bo, fbi->var.yoffset * npages);
  74. }
  75. static int omap_fbdev_pan_display(struct fb_var_screeninfo *var,
  76. struct fb_info *fbi)
  77. {
  78. struct drm_fb_helper *helper = get_fb(fbi);
  79. struct omap_fbdev *fbdev = to_omap_fbdev(helper);
  80. if (!helper)
  81. goto fallback;
  82. if (!fbdev->ywrap_enabled)
  83. goto fallback;
  84. if (drm_can_sleep()) {
  85. pan_worker(&fbdev->work);
  86. } else {
  87. struct omap_drm_private *priv = helper->dev->dev_private;
  88. queue_work(priv->wq, &fbdev->work);
  89. }
  90. return 0;
  91. fallback:
  92. return drm_fb_helper_pan_display(var, fbi);
  93. }
  94. static struct fb_ops omap_fb_ops = {
  95. .owner = THIS_MODULE,
  96. /* Note: to properly handle manual update displays, we wrap the
  97. * basic fbdev ops which write to the framebuffer
  98. */
  99. .fb_read = fb_sys_read,
  100. .fb_write = omap_fbdev_write,
  101. .fb_fillrect = omap_fbdev_fillrect,
  102. .fb_copyarea = omap_fbdev_copyarea,
  103. .fb_imageblit = omap_fbdev_imageblit,
  104. .fb_check_var = drm_fb_helper_check_var,
  105. .fb_set_par = drm_fb_helper_set_par,
  106. .fb_pan_display = omap_fbdev_pan_display,
  107. .fb_blank = drm_fb_helper_blank,
  108. .fb_setcmap = drm_fb_helper_setcmap,
  109. .fb_debug_enter = drm_fb_helper_debug_enter,
  110. .fb_debug_leave = drm_fb_helper_debug_leave,
  111. };
  112. static int omap_fbdev_create(struct drm_fb_helper *helper,
  113. struct drm_fb_helper_surface_size *sizes)
  114. {
  115. struct omap_fbdev *fbdev = to_omap_fbdev(helper);
  116. struct drm_device *dev = helper->dev;
  117. struct omap_drm_private *priv = dev->dev_private;
  118. struct drm_framebuffer *fb = NULL;
  119. union omap_gem_size gsize;
  120. struct fb_info *fbi = NULL;
  121. struct drm_mode_fb_cmd2 mode_cmd = {0};
  122. dma_addr_t paddr;
  123. int ret;
  124. /* only doing ARGB32 since this is what is needed to alpha-blend
  125. * with video overlays:
  126. */
  127. sizes->surface_bpp = 32;
  128. sizes->surface_depth = 32;
  129. DBG("create fbdev: %dx%d@%d (%dx%d)", sizes->surface_width,
  130. sizes->surface_height, sizes->surface_bpp,
  131. sizes->fb_width, sizes->fb_height);
  132. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  133. sizes->surface_depth);
  134. mode_cmd.width = sizes->surface_width;
  135. mode_cmd.height = sizes->surface_height;
  136. mode_cmd.pitches[0] = align_pitch(
  137. mode_cmd.width * ((sizes->surface_bpp + 7) / 8),
  138. mode_cmd.width, sizes->surface_bpp);
  139. fbdev->ywrap_enabled = priv->has_dmm && ywrap_enabled;
  140. if (fbdev->ywrap_enabled) {
  141. /* need to align pitch to page size if using DMM scrolling */
  142. mode_cmd.pitches[0] = ALIGN(mode_cmd.pitches[0], PAGE_SIZE);
  143. }
  144. /* allocate backing bo */
  145. gsize = (union omap_gem_size){
  146. .bytes = PAGE_ALIGN(mode_cmd.pitches[0] * mode_cmd.height),
  147. };
  148. DBG("allocating %d bytes for fb %d", gsize.bytes, dev->primary->index);
  149. fbdev->bo = omap_gem_new(dev, gsize, OMAP_BO_SCANOUT | OMAP_BO_WC);
  150. if (!fbdev->bo) {
  151. dev_err(dev->dev, "failed to allocate buffer object\n");
  152. ret = -ENOMEM;
  153. goto fail;
  154. }
  155. fb = omap_framebuffer_init(dev, &mode_cmd, &fbdev->bo);
  156. if (IS_ERR(fb)) {
  157. dev_err(dev->dev, "failed to allocate fb\n");
  158. /* note: if fb creation failed, we can't rely on fb destroy
  159. * to unref the bo:
  160. */
  161. drm_gem_object_unreference(fbdev->bo);
  162. ret = PTR_ERR(fb);
  163. goto fail;
  164. }
  165. /* note: this keeps the bo pinned.. which is perhaps not ideal,
  166. * but is needed as long as we use fb_mmap() to mmap to userspace
  167. * (since this happens using fix.smem_start). Possibly we could
  168. * implement our own mmap using GEM mmap support to avoid this
  169. * (non-tiled buffer doesn't need to be pinned for fbcon to write
  170. * to it). Then we just need to be sure that we are able to re-
  171. * pin it in case of an opps.
  172. */
  173. ret = omap_gem_get_paddr(fbdev->bo, &paddr, true);
  174. if (ret) {
  175. dev_err(dev->dev,
  176. "could not map (paddr)! Skipping framebuffer alloc\n");
  177. ret = -ENOMEM;
  178. goto fail;
  179. }
  180. mutex_lock(&dev->struct_mutex);
  181. fbi = framebuffer_alloc(0, dev->dev);
  182. if (!fbi) {
  183. dev_err(dev->dev, "failed to allocate fb info\n");
  184. ret = -ENOMEM;
  185. goto fail_unlock;
  186. }
  187. DBG("fbi=%p, dev=%p", fbi, dev);
  188. fbdev->fb = fb;
  189. helper->fb = fb;
  190. helper->fbdev = fbi;
  191. fbi->par = helper;
  192. fbi->flags = FBINFO_DEFAULT;
  193. fbi->fbops = &omap_fb_ops;
  194. strcpy(fbi->fix.id, MODULE_NAME);
  195. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  196. if (ret) {
  197. ret = -ENOMEM;
  198. goto fail_unlock;
  199. }
  200. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  201. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  202. dev->mode_config.fb_base = paddr;
  203. fbi->screen_base = omap_gem_vaddr(fbdev->bo);
  204. fbi->screen_size = fbdev->bo->size;
  205. fbi->fix.smem_start = paddr;
  206. fbi->fix.smem_len = fbdev->bo->size;
  207. /* if we have DMM, then we can use it for scrolling by just
  208. * shuffling pages around in DMM rather than doing sw blit.
  209. */
  210. if (fbdev->ywrap_enabled) {
  211. DRM_INFO("Enabling DMM ywrap scrolling\n");
  212. fbi->flags |= FBINFO_HWACCEL_YWRAP | FBINFO_READS_FAST;
  213. fbi->fix.ywrapstep = 1;
  214. }
  215. DBG("par=%p, %dx%d", fbi->par, fbi->var.xres, fbi->var.yres);
  216. DBG("allocated %dx%d fb", fbdev->fb->width, fbdev->fb->height);
  217. mutex_unlock(&dev->struct_mutex);
  218. return 0;
  219. fail_unlock:
  220. mutex_unlock(&dev->struct_mutex);
  221. fail:
  222. if (ret) {
  223. if (fbi)
  224. framebuffer_release(fbi);
  225. if (fb) {
  226. drm_framebuffer_unregister_private(fb);
  227. drm_framebuffer_remove(fb);
  228. }
  229. }
  230. return ret;
  231. }
  232. static void omap_crtc_fb_gamma_set(struct drm_crtc *crtc,
  233. u16 red, u16 green, u16 blue, int regno)
  234. {
  235. DBG("fbdev: set gamma");
  236. }
  237. static void omap_crtc_fb_gamma_get(struct drm_crtc *crtc,
  238. u16 *red, u16 *green, u16 *blue, int regno)
  239. {
  240. DBG("fbdev: get gamma");
  241. }
  242. static struct drm_fb_helper_funcs omap_fb_helper_funcs = {
  243. .gamma_set = omap_crtc_fb_gamma_set,
  244. .gamma_get = omap_crtc_fb_gamma_get,
  245. .fb_probe = omap_fbdev_create,
  246. };
  247. static struct drm_fb_helper *get_fb(struct fb_info *fbi)
  248. {
  249. if (!fbi || strcmp(fbi->fix.id, MODULE_NAME)) {
  250. /* these are not the fb's you're looking for */
  251. return NULL;
  252. }
  253. return fbi->par;
  254. }
  255. /* flush an area of the framebuffer (in case of manual update display that
  256. * is not automatically flushed)
  257. */
  258. static void omap_fbdev_flush(struct fb_info *fbi, int x, int y, int w, int h)
  259. {
  260. struct drm_fb_helper *helper = get_fb(fbi);
  261. if (!helper)
  262. return;
  263. VERB("flush fbdev: %d,%d %dx%d, fbi=%p", x, y, w, h, fbi);
  264. omap_framebuffer_flush(helper->fb, x, y, w, h);
  265. }
  266. /* initialize fbdev helper */
  267. struct drm_fb_helper *omap_fbdev_init(struct drm_device *dev)
  268. {
  269. struct omap_drm_private *priv = dev->dev_private;
  270. struct omap_fbdev *fbdev = NULL;
  271. struct drm_fb_helper *helper;
  272. int ret = 0;
  273. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  274. if (!fbdev) {
  275. dev_err(dev->dev, "could not allocate fbdev\n");
  276. goto fail;
  277. }
  278. INIT_WORK(&fbdev->work, pan_worker);
  279. helper = &fbdev->base;
  280. helper->funcs = &omap_fb_helper_funcs;
  281. ret = drm_fb_helper_init(dev, helper,
  282. priv->num_crtcs, priv->num_connectors);
  283. if (ret) {
  284. dev_err(dev->dev, "could not init fbdev: ret=%d\n", ret);
  285. goto fail;
  286. }
  287. drm_fb_helper_single_add_all_connectors(helper);
  288. /* disable all the possible outputs/crtcs before entering KMS mode */
  289. drm_helper_disable_unused_functions(dev);
  290. drm_fb_helper_initial_config(helper, 32);
  291. priv->fbdev = helper;
  292. return helper;
  293. fail:
  294. kfree(fbdev);
  295. return NULL;
  296. }
  297. void omap_fbdev_free(struct drm_device *dev)
  298. {
  299. struct omap_drm_private *priv = dev->dev_private;
  300. struct drm_fb_helper *helper = priv->fbdev;
  301. struct omap_fbdev *fbdev;
  302. struct fb_info *fbi;
  303. DBG();
  304. fbi = helper->fbdev;
  305. /* only cleanup framebuffer if it is present */
  306. if (fbi) {
  307. unregister_framebuffer(fbi);
  308. framebuffer_release(fbi);
  309. }
  310. drm_fb_helper_fini(helper);
  311. fbdev = to_omap_fbdev(priv->fbdev);
  312. /* this will free the backing object */
  313. if (fbdev->fb) {
  314. drm_framebuffer_unregister_private(fbdev->fb);
  315. drm_framebuffer_remove(fbdev->fb);
  316. }
  317. kfree(fbdev);
  318. priv->fbdev = NULL;
  319. }