exynos_drm_fbdev.c 8.9 KB

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