exynos_drm_fbdev.c 7.9 KB

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