exynos_drm_fbdev.c 9.3 KB

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