exynos_drm_fbdev.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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_gem.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_obj *exynos_gem_obj;
  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_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
  38. struct exynos_drm_gem_buf *buffer = exynos_gem_obj->buffer;
  39. unsigned long vm_size;
  40. int ret;
  41. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  42. vm_size = vma->vm_end - vma->vm_start;
  43. if (vm_size > buffer->size)
  44. return -EINVAL;
  45. ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages,
  46. buffer->dma_addr, buffer->size, &buffer->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. .fb_mmap = exynos_drm_fb_mmap,
  56. .fb_fillrect = cfb_fillrect,
  57. .fb_copyarea = cfb_copyarea,
  58. .fb_imageblit = cfb_imageblit,
  59. .fb_check_var = drm_fb_helper_check_var,
  60. .fb_set_par = drm_fb_helper_set_par,
  61. .fb_blank = drm_fb_helper_blank,
  62. .fb_pan_display = drm_fb_helper_pan_display,
  63. .fb_setcmap = drm_fb_helper_setcmap,
  64. };
  65. static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
  66. struct drm_framebuffer *fb)
  67. {
  68. struct fb_info *fbi = helper->fbdev;
  69. struct drm_device *dev = helper->dev;
  70. struct exynos_drm_gem_buf *buffer;
  71. unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
  72. unsigned long offset;
  73. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  74. drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
  75. /* RGB formats use only one buffer */
  76. buffer = exynos_drm_fb_buffer(fb, 0);
  77. if (!buffer) {
  78. DRM_DEBUG_KMS("buffer is null.\n");
  79. return -EFAULT;
  80. }
  81. /* map pages with kernel virtual space. */
  82. if (!buffer->kvaddr) {
  83. if (is_drm_iommu_supported(dev)) {
  84. unsigned int nr_pages = buffer->size >> PAGE_SHIFT;
  85. buffer->kvaddr = (void __iomem *) vmap(buffer->pages,
  86. nr_pages, VM_MAP,
  87. pgprot_writecombine(PAGE_KERNEL));
  88. } else {
  89. phys_addr_t dma_addr = buffer->dma_addr;
  90. if (dma_addr)
  91. buffer->kvaddr = (void __iomem *)phys_to_virt(dma_addr);
  92. else
  93. buffer->kvaddr = (void __iomem *)NULL;
  94. }
  95. if (!buffer->kvaddr) {
  96. DRM_ERROR("failed to map pages to kernel space.\n");
  97. return -EIO;
  98. }
  99. }
  100. /* buffer count to framebuffer always is 1 at booting time. */
  101. exynos_drm_fb_set_buf_cnt(fb, 1);
  102. offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
  103. offset += fbi->var.yoffset * fb->pitches[0];
  104. fbi->screen_base = buffer->kvaddr + offset;
  105. fbi->screen_size = size;
  106. return 0;
  107. }
  108. static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
  109. struct drm_fb_helper_surface_size *sizes)
  110. {
  111. struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
  112. struct exynos_drm_gem_obj *exynos_gem_obj;
  113. struct drm_device *dev = helper->dev;
  114. struct fb_info *fbi;
  115. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  116. struct platform_device *pdev = dev->platformdev;
  117. unsigned long size;
  118. int ret;
  119. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
  120. sizes->surface_width, sizes->surface_height,
  121. sizes->surface_bpp);
  122. mode_cmd.width = sizes->surface_width;
  123. mode_cmd.height = sizes->surface_height;
  124. mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
  125. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  126. sizes->surface_depth);
  127. mutex_lock(&dev->struct_mutex);
  128. fbi = framebuffer_alloc(0, &pdev->dev);
  129. if (!fbi) {
  130. DRM_ERROR("failed to allocate fb info.\n");
  131. ret = -ENOMEM;
  132. goto out;
  133. }
  134. size = mode_cmd.pitches[0] * mode_cmd.height;
  135. exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_CONTIG, size);
  136. /*
  137. * If physically contiguous memory allocation fails and if IOMMU is
  138. * supported then try to get buffer from non physically contiguous
  139. * memory area.
  140. */
  141. if (IS_ERR(exynos_gem_obj) && is_drm_iommu_supported(dev)) {
  142. dev_warn(&pdev->dev, "contiguous FB allocation failed, falling back to non-contiguous\n");
  143. exynos_gem_obj = exynos_drm_gem_create(dev, EXYNOS_BO_NONCONTIG,
  144. size);
  145. }
  146. if (IS_ERR(exynos_gem_obj)) {
  147. ret = PTR_ERR(exynos_gem_obj);
  148. goto err_release_framebuffer;
  149. }
  150. exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
  151. helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
  152. &exynos_gem_obj->base);
  153. if (IS_ERR(helper->fb)) {
  154. DRM_ERROR("failed to create drm framebuffer.\n");
  155. ret = PTR_ERR(helper->fb);
  156. goto err_destroy_gem;
  157. }
  158. helper->fbdev = fbi;
  159. fbi->par = helper;
  160. fbi->flags = FBINFO_FLAG_DEFAULT;
  161. fbi->fbops = &exynos_drm_fb_ops;
  162. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  163. if (ret) {
  164. DRM_ERROR("failed to allocate cmap.\n");
  165. goto err_destroy_framebuffer;
  166. }
  167. ret = exynos_drm_fbdev_update(helper, helper->fb);
  168. if (ret < 0)
  169. goto err_dealloc_cmap;
  170. mutex_unlock(&dev->struct_mutex);
  171. return ret;
  172. err_dealloc_cmap:
  173. fb_dealloc_cmap(&fbi->cmap);
  174. err_destroy_framebuffer:
  175. drm_framebuffer_cleanup(helper->fb);
  176. err_destroy_gem:
  177. exynos_drm_gem_destroy(exynos_gem_obj);
  178. err_release_framebuffer:
  179. framebuffer_release(fbi);
  180. /*
  181. * if failed, all resources allocated above would be released by
  182. * drm_mode_config_cleanup() when drm_load() had been called prior
  183. * to any specific driver such as fimd or hdmi driver.
  184. */
  185. out:
  186. mutex_unlock(&dev->struct_mutex);
  187. return ret;
  188. }
  189. static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
  190. .fb_probe = exynos_drm_fbdev_create,
  191. };
  192. static bool exynos_drm_fbdev_is_anything_connected(struct drm_device *dev)
  193. {
  194. struct drm_connector *connector;
  195. bool ret = false;
  196. mutex_lock(&dev->mode_config.mutex);
  197. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  198. if (connector->status != connector_status_connected)
  199. continue;
  200. ret = true;
  201. break;
  202. }
  203. mutex_unlock(&dev->mode_config.mutex);
  204. return ret;
  205. }
  206. int exynos_drm_fbdev_init(struct drm_device *dev)
  207. {
  208. struct exynos_drm_fbdev *fbdev;
  209. struct exynos_drm_private *private = dev->dev_private;
  210. struct drm_fb_helper *helper;
  211. unsigned int num_crtc;
  212. int ret;
  213. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  214. return 0;
  215. if (!exynos_drm_fbdev_is_anything_connected(dev))
  216. return 0;
  217. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  218. if (!fbdev)
  219. return -ENOMEM;
  220. private->fb_helper = helper = &fbdev->drm_fb_helper;
  221. helper->funcs = &exynos_drm_fb_helper_funcs;
  222. num_crtc = dev->mode_config.num_crtc;
  223. ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
  224. if (ret < 0) {
  225. DRM_ERROR("failed to initialize drm fb helper.\n");
  226. goto err_init;
  227. }
  228. ret = drm_fb_helper_single_add_all_connectors(helper);
  229. if (ret < 0) {
  230. DRM_ERROR("failed to register drm_fb_helper_connector.\n");
  231. goto err_setup;
  232. }
  233. /* disable all the possible outputs/crtcs before entering KMS mode */
  234. drm_helper_disable_unused_functions(dev);
  235. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  236. if (ret < 0) {
  237. DRM_ERROR("failed to set up hw configuration.\n");
  238. goto err_setup;
  239. }
  240. return 0;
  241. err_setup:
  242. drm_fb_helper_fini(helper);
  243. err_init:
  244. private->fb_helper = NULL;
  245. kfree(fbdev);
  246. return ret;
  247. }
  248. static void exynos_drm_fbdev_destroy(struct drm_device *dev,
  249. struct drm_fb_helper *fb_helper)
  250. {
  251. struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
  252. struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
  253. struct drm_framebuffer *fb;
  254. if (is_drm_iommu_supported(dev) && exynos_gem_obj->buffer->kvaddr)
  255. vunmap(exynos_gem_obj->buffer->kvaddr);
  256. /* release drm framebuffer and real buffer */
  257. if (fb_helper->fb && fb_helper->fb->funcs) {
  258. fb = fb_helper->fb;
  259. if (fb) {
  260. drm_framebuffer_unregister_private(fb);
  261. drm_framebuffer_remove(fb);
  262. }
  263. }
  264. /* release linux framebuffer */
  265. if (fb_helper->fbdev) {
  266. struct fb_info *info;
  267. int ret;
  268. info = fb_helper->fbdev;
  269. ret = unregister_framebuffer(info);
  270. if (ret < 0)
  271. DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
  272. if (info->cmap.len)
  273. fb_dealloc_cmap(&info->cmap);
  274. framebuffer_release(info);
  275. }
  276. drm_fb_helper_fini(fb_helper);
  277. }
  278. void exynos_drm_fbdev_fini(struct drm_device *dev)
  279. {
  280. struct exynos_drm_private *private = dev->dev_private;
  281. struct exynos_drm_fbdev *fbdev;
  282. if (!private || !private->fb_helper)
  283. return;
  284. fbdev = to_exynos_fbdev(private->fb_helper);
  285. if (fbdev->exynos_gem_obj)
  286. exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
  287. exynos_drm_fbdev_destroy(dev, private->fb_helper);
  288. kfree(fbdev);
  289. private->fb_helper = NULL;
  290. }
  291. void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
  292. {
  293. struct exynos_drm_private *private = dev->dev_private;
  294. if (!private || !private->fb_helper)
  295. return;
  296. drm_fb_helper_restore_fbdev_mode_unlocked(private->fb_helper);
  297. }