exynos_drm_fbdev.c 8.0 KB

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