exynos_drm_fbdev.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* exynos_drm_fbdev.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * Authors:
  5. * Inki Dae <inki.dae@samsung.com>
  6. * Joonyoung Shim <jy0922.shim@samsung.com>
  7. * Seung-Woo Kim <sw0312.kim@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_fb_helper.h>
  17. #include <drm/drm_crtc_helper.h>
  18. #include <drm/exynos_drm.h>
  19. #include <linux/console.h>
  20. #include "exynos_drm_drv.h"
  21. #include "exynos_drm_fb.h"
  22. #include "exynos_drm_fbdev.h"
  23. #include "exynos_drm_iommu.h"
  24. #define MAX_CONNECTOR 4
  25. #define PREFERRED_BPP 32
  26. #define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\
  27. drm_fb_helper)
  28. struct exynos_drm_fbdev {
  29. struct drm_fb_helper drm_fb_helper;
  30. struct exynos_drm_gem *exynos_gem;
  31. };
  32. static int exynos_drm_fb_mmap(struct fb_info *info,
  33. struct vm_area_struct *vma)
  34. {
  35. struct drm_fb_helper *helper = info->par;
  36. struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
  37. struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
  38. unsigned long vm_size;
  39. int ret;
  40. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  41. vm_size = vma->vm_end - vma->vm_start;
  42. if (vm_size > exynos_gem->size)
  43. return -EINVAL;
  44. ret = dma_mmap_attrs(to_dma_dev(helper->dev), vma, exynos_gem->cookie,
  45. exynos_gem->dma_addr, exynos_gem->size,
  46. exynos_gem->dma_attrs);
  47. if (ret < 0) {
  48. DRM_ERROR("failed to mmap.\n");
  49. return ret;
  50. }
  51. return 0;
  52. }
  53. static struct fb_ops exynos_drm_fb_ops = {
  54. .owner = THIS_MODULE,
  55. DRM_FB_HELPER_DEFAULT_OPS,
  56. .fb_mmap = exynos_drm_fb_mmap,
  57. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  58. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  59. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  60. };
  61. static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
  62. struct drm_fb_helper_surface_size *sizes,
  63. struct exynos_drm_gem *exynos_gem)
  64. {
  65. struct fb_info *fbi;
  66. struct drm_framebuffer *fb = helper->fb;
  67. unsigned int size = fb->width * fb->height * fb->format->cpp[0];
  68. unsigned int nr_pages;
  69. unsigned long offset;
  70. fbi = drm_fb_helper_alloc_fbi(helper);
  71. if (IS_ERR(fbi)) {
  72. DRM_ERROR("failed to allocate fb info.\n");
  73. return PTR_ERR(fbi);
  74. }
  75. fbi->par = helper;
  76. fbi->flags = FBINFO_FLAG_DEFAULT;
  77. fbi->fbops = &exynos_drm_fb_ops;
  78. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->format->depth);
  79. drm_fb_helper_fill_var(fbi, helper, sizes->fb_width, sizes->fb_height);
  80. nr_pages = exynos_gem->size >> PAGE_SHIFT;
  81. exynos_gem->kvaddr = (void __iomem *) vmap(exynos_gem->pages, nr_pages,
  82. VM_MAP, pgprot_writecombine(PAGE_KERNEL));
  83. if (!exynos_gem->kvaddr) {
  84. DRM_ERROR("failed to map pages to kernel space.\n");
  85. return -EIO;
  86. }
  87. offset = fbi->var.xoffset * fb->format->cpp[0];
  88. offset += fbi->var.yoffset * fb->pitches[0];
  89. fbi->screen_base = exynos_gem->kvaddr + offset;
  90. fbi->screen_size = size;
  91. fbi->fix.smem_len = size;
  92. return 0;
  93. }
  94. static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
  95. struct drm_fb_helper_surface_size *sizes)
  96. {
  97. struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
  98. struct exynos_drm_gem *exynos_gem;
  99. struct drm_device *dev = helper->dev;
  100. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  101. unsigned long size;
  102. int ret;
  103. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
  104. sizes->surface_width, sizes->surface_height,
  105. sizes->surface_bpp);
  106. mode_cmd.width = sizes->surface_width;
  107. mode_cmd.height = sizes->surface_height;
  108. mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
  109. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  110. sizes->surface_depth);
  111. size = mode_cmd.pitches[0] * mode_cmd.height;
  112. exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
  113. /*
  114. * If physically contiguous memory allocation fails and if IOMMU is
  115. * supported then try to get buffer from non physically contiguous
  116. * memory area.
  117. */
  118. if (IS_ERR(exynos_gem) && is_drm_iommu_supported(dev)) {
  119. dev_warn(dev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
  120. exynos_gem = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
  121. size);
  122. }
  123. if (IS_ERR(exynos_gem))
  124. return PTR_ERR(exynos_gem);
  125. exynos_fbdev->exynos_gem = exynos_gem;
  126. helper->fb =
  127. exynos_drm_framebuffer_init(dev, &mode_cmd, &exynos_gem, 1);
  128. if (IS_ERR(helper->fb)) {
  129. DRM_ERROR("failed to create drm framebuffer.\n");
  130. ret = PTR_ERR(helper->fb);
  131. goto err_destroy_gem;
  132. }
  133. ret = exynos_drm_fbdev_update(helper, sizes, exynos_gem);
  134. if (ret < 0)
  135. goto err_destroy_framebuffer;
  136. return ret;
  137. err_destroy_framebuffer:
  138. drm_framebuffer_cleanup(helper->fb);
  139. err_destroy_gem:
  140. exynos_drm_gem_destroy(exynos_gem);
  141. /*
  142. * if failed, all resources allocated above would be released by
  143. * drm_mode_config_cleanup() when drm_load() had been called prior
  144. * to any specific driver such as fimd or hdmi driver.
  145. */
  146. return ret;
  147. }
  148. static const struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
  149. .fb_probe = exynos_drm_fbdev_create,
  150. };
  151. int exynos_drm_fbdev_init(struct drm_device *dev)
  152. {
  153. struct exynos_drm_fbdev *fbdev;
  154. struct exynos_drm_private *private = dev->dev_private;
  155. struct drm_fb_helper *helper;
  156. int ret;
  157. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  158. return 0;
  159. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  160. if (!fbdev)
  161. return -ENOMEM;
  162. private->fb_helper = helper = &fbdev->drm_fb_helper;
  163. drm_fb_helper_prepare(dev, helper, &exynos_drm_fb_helper_funcs);
  164. ret = drm_fb_helper_init(dev, helper, MAX_CONNECTOR);
  165. if (ret < 0) {
  166. DRM_ERROR("failed to initialize drm fb helper.\n");
  167. goto err_init;
  168. }
  169. ret = drm_fb_helper_single_add_all_connectors(helper);
  170. if (ret < 0) {
  171. DRM_ERROR("failed to register drm_fb_helper_connector.\n");
  172. goto err_setup;
  173. }
  174. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  175. if (ret < 0) {
  176. DRM_ERROR("failed to set up hw configuration.\n");
  177. goto err_setup;
  178. }
  179. return 0;
  180. err_setup:
  181. drm_fb_helper_fini(helper);
  182. err_init:
  183. private->fb_helper = NULL;
  184. kfree(fbdev);
  185. return ret;
  186. }
  187. static void exynos_drm_fbdev_destroy(struct drm_device *dev,
  188. struct drm_fb_helper *fb_helper)
  189. {
  190. struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
  191. struct exynos_drm_gem *exynos_gem = exynos_fbd->exynos_gem;
  192. struct drm_framebuffer *fb;
  193. vunmap(exynos_gem->kvaddr);
  194. /* release drm framebuffer and real buffer */
  195. if (fb_helper->fb && fb_helper->fb->funcs) {
  196. fb = fb_helper->fb;
  197. if (fb)
  198. drm_framebuffer_remove(fb);
  199. }
  200. drm_fb_helper_unregister_fbi(fb_helper);
  201. drm_fb_helper_fini(fb_helper);
  202. }
  203. void exynos_drm_fbdev_fini(struct drm_device *dev)
  204. {
  205. struct exynos_drm_private *private = dev->dev_private;
  206. struct exynos_drm_fbdev *fbdev;
  207. if (!private || !private->fb_helper)
  208. return;
  209. fbdev = to_exynos_fbdev(private->fb_helper);
  210. exynos_drm_fbdev_destroy(dev, private->fb_helper);
  211. kfree(fbdev);
  212. private->fb_helper = NULL;
  213. }
  214. void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
  215. {
  216. struct exynos_drm_private *private = dev->dev_private;
  217. if (!private || !private->fb_helper)
  218. return;
  219. drm_fb_helper_restore_fbdev_mode_unlocked(private->fb_helper);
  220. }
  221. void exynos_drm_output_poll_changed(struct drm_device *dev)
  222. {
  223. struct exynos_drm_private *private = dev->dev_private;
  224. struct drm_fb_helper *fb_helper = private->fb_helper;
  225. drm_fb_helper_hotplug_event(fb_helper);
  226. }
  227. void exynos_drm_fbdev_suspend(struct drm_device *dev)
  228. {
  229. struct exynos_drm_private *private = dev->dev_private;
  230. console_lock();
  231. drm_fb_helper_set_suspend(private->fb_helper, 1);
  232. console_unlock();
  233. }
  234. void exynos_drm_fbdev_resume(struct drm_device *dev)
  235. {
  236. struct exynos_drm_private *private = dev->dev_private;
  237. console_lock();
  238. drm_fb_helper_set_suspend(private->fb_helper, 0);
  239. console_unlock();
  240. }