exynos_drm_fbdev.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <drm/drmP.h>
  29. #include <drm/drm_crtc.h>
  30. #include <drm/drm_fb_helper.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include "exynos_drm_drv.h"
  33. #include "exynos_drm_fb.h"
  34. #include "exynos_drm_gem.h"
  35. #include "exynos_drm_iommu.h"
  36. #define MAX_CONNECTOR 4
  37. #define PREFERRED_BPP 32
  38. #define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\
  39. drm_fb_helper)
  40. struct exynos_drm_fbdev {
  41. struct drm_fb_helper drm_fb_helper;
  42. struct exynos_drm_gem_obj *exynos_gem_obj;
  43. };
  44. static int exynos_drm_fb_mmap(struct fb_info *info,
  45. struct vm_area_struct *vma)
  46. {
  47. struct drm_fb_helper *helper = info->par;
  48. struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
  49. struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
  50. struct exynos_drm_gem_buf *buffer = exynos_gem_obj->buffer;
  51. unsigned long vm_size;
  52. int ret;
  53. DRM_DEBUG_KMS("%s\n", __func__);
  54. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  55. vm_size = vma->vm_end - vma->vm_start;
  56. if (vm_size > buffer->size)
  57. return -EINVAL;
  58. ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages,
  59. buffer->dma_addr, buffer->size, &buffer->dma_attrs);
  60. if (ret < 0) {
  61. DRM_ERROR("failed to mmap.\n");
  62. return ret;
  63. }
  64. return 0;
  65. }
  66. static struct fb_ops exynos_drm_fb_ops = {
  67. .owner = THIS_MODULE,
  68. .fb_mmap = exynos_drm_fb_mmap,
  69. .fb_fillrect = cfb_fillrect,
  70. .fb_copyarea = cfb_copyarea,
  71. .fb_imageblit = cfb_imageblit,
  72. .fb_check_var = drm_fb_helper_check_var,
  73. .fb_set_par = drm_fb_helper_set_par,
  74. .fb_blank = drm_fb_helper_blank,
  75. .fb_pan_display = drm_fb_helper_pan_display,
  76. .fb_setcmap = drm_fb_helper_setcmap,
  77. };
  78. static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
  79. struct drm_framebuffer *fb)
  80. {
  81. struct fb_info *fbi = helper->fbdev;
  82. struct drm_device *dev = helper->dev;
  83. struct exynos_drm_gem_buf *buffer;
  84. unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
  85. unsigned long offset;
  86. DRM_DEBUG_KMS("%s\n", __FILE__);
  87. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  88. drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
  89. /* RGB formats use only one buffer */
  90. buffer = exynos_drm_fb_buffer(fb, 0);
  91. if (!buffer) {
  92. DRM_LOG_KMS("buffer is null.\n");
  93. return -EFAULT;
  94. }
  95. /* map pages with kernel virtual space. */
  96. if (!buffer->kvaddr) {
  97. if (is_drm_iommu_supported(dev)) {
  98. unsigned int nr_pages = buffer->size >> PAGE_SHIFT;
  99. buffer->kvaddr = vmap(buffer->pages, nr_pages, VM_MAP,
  100. pgprot_writecombine(PAGE_KERNEL));
  101. } else {
  102. phys_addr_t dma_addr = buffer->dma_addr;
  103. if (dma_addr)
  104. buffer->kvaddr = phys_to_virt(dma_addr);
  105. else
  106. buffer->kvaddr = (void __iomem *)NULL;
  107. }
  108. if (!buffer->kvaddr) {
  109. DRM_ERROR("failed to map pages to kernel space.\n");
  110. return -EIO;
  111. }
  112. }
  113. /* buffer count to framebuffer always is 1 at booting time. */
  114. exynos_drm_fb_set_buf_cnt(fb, 1);
  115. offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
  116. offset += fbi->var.yoffset * fb->pitches[0];
  117. dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr;
  118. fbi->screen_base = buffer->kvaddr + offset;
  119. if (is_drm_iommu_supported(dev))
  120. fbi->fix.smem_start = (unsigned long)
  121. (page_to_phys(sg_page(buffer->sgt->sgl)) + offset);
  122. else
  123. fbi->fix.smem_start = (unsigned long)buffer->dma_addr;
  124. fbi->screen_size = size;
  125. fbi->fix.smem_len = size;
  126. return 0;
  127. }
  128. static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
  129. struct drm_fb_helper_surface_size *sizes)
  130. {
  131. struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
  132. struct exynos_drm_gem_obj *exynos_gem_obj;
  133. struct drm_device *dev = helper->dev;
  134. struct fb_info *fbi;
  135. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  136. struct platform_device *pdev = dev->platformdev;
  137. unsigned long size;
  138. int ret;
  139. DRM_DEBUG_KMS("%s\n", __FILE__);
  140. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
  141. sizes->surface_width, sizes->surface_height,
  142. sizes->surface_bpp);
  143. mode_cmd.width = sizes->surface_width;
  144. mode_cmd.height = sizes->surface_height;
  145. mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
  146. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  147. sizes->surface_depth);
  148. mutex_lock(&dev->struct_mutex);
  149. fbi = framebuffer_alloc(0, &pdev->dev);
  150. if (!fbi) {
  151. DRM_ERROR("failed to allocate fb info.\n");
  152. ret = -ENOMEM;
  153. goto out;
  154. }
  155. size = mode_cmd.pitches[0] * mode_cmd.height;
  156. /* 0 means to allocate physically continuous memory */
  157. exynos_gem_obj = exynos_drm_gem_create(dev, 0, size);
  158. if (IS_ERR(exynos_gem_obj)) {
  159. ret = PTR_ERR(exynos_gem_obj);
  160. goto err_release_framebuffer;
  161. }
  162. exynos_fbdev->exynos_gem_obj = exynos_gem_obj;
  163. helper->fb = exynos_drm_framebuffer_init(dev, &mode_cmd,
  164. &exynos_gem_obj->base);
  165. if (IS_ERR_OR_NULL(helper->fb)) {
  166. DRM_ERROR("failed to create drm framebuffer.\n");
  167. ret = PTR_ERR(helper->fb);
  168. goto err_destroy_gem;
  169. }
  170. helper->fbdev = fbi;
  171. fbi->par = helper;
  172. fbi->flags = FBINFO_FLAG_DEFAULT;
  173. fbi->fbops = &exynos_drm_fb_ops;
  174. ret = fb_alloc_cmap(&fbi->cmap, 256, 0);
  175. if (ret) {
  176. DRM_ERROR("failed to allocate cmap.\n");
  177. goto err_destroy_framebuffer;
  178. }
  179. ret = exynos_drm_fbdev_update(helper, helper->fb);
  180. if (ret < 0)
  181. goto err_dealloc_cmap;
  182. mutex_unlock(&dev->struct_mutex);
  183. return ret;
  184. err_dealloc_cmap:
  185. fb_dealloc_cmap(&fbi->cmap);
  186. err_destroy_framebuffer:
  187. drm_framebuffer_cleanup(helper->fb);
  188. err_destroy_gem:
  189. exynos_drm_gem_destroy(exynos_gem_obj);
  190. err_release_framebuffer:
  191. framebuffer_release(fbi);
  192. /*
  193. * if failed, all resources allocated above would be released by
  194. * drm_mode_config_cleanup() when drm_load() had been called prior
  195. * to any specific driver such as fimd or hdmi driver.
  196. */
  197. out:
  198. mutex_unlock(&dev->struct_mutex);
  199. return ret;
  200. }
  201. static int exynos_drm_fbdev_probe(struct drm_fb_helper *helper,
  202. struct drm_fb_helper_surface_size *sizes)
  203. {
  204. int ret = 0;
  205. DRM_DEBUG_KMS("%s\n", __FILE__);
  206. /*
  207. * with !helper->fb, it means that this funcion is called first time
  208. * and after that, the helper->fb would be used as clone mode.
  209. */
  210. if (!helper->fb) {
  211. ret = exynos_drm_fbdev_create(helper, sizes);
  212. if (ret < 0) {
  213. DRM_ERROR("failed to create fbdev.\n");
  214. return ret;
  215. }
  216. /*
  217. * fb_helper expects a value more than 1 if succeed
  218. * because register_framebuffer() should be called.
  219. */
  220. ret = 1;
  221. }
  222. return ret;
  223. }
  224. static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
  225. .fb_probe = exynos_drm_fbdev_probe,
  226. };
  227. int exynos_drm_fbdev_init(struct drm_device *dev)
  228. {
  229. struct exynos_drm_fbdev *fbdev;
  230. struct exynos_drm_private *private = dev->dev_private;
  231. struct drm_fb_helper *helper;
  232. unsigned int num_crtc;
  233. int ret;
  234. DRM_DEBUG_KMS("%s\n", __FILE__);
  235. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  236. return 0;
  237. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  238. if (!fbdev) {
  239. DRM_ERROR("failed to allocate drm fbdev.\n");
  240. return -ENOMEM;
  241. }
  242. private->fb_helper = helper = &fbdev->drm_fb_helper;
  243. helper->funcs = &exynos_drm_fb_helper_funcs;
  244. num_crtc = dev->mode_config.num_crtc;
  245. ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
  246. if (ret < 0) {
  247. DRM_ERROR("failed to initialize drm fb helper.\n");
  248. goto err_init;
  249. }
  250. ret = drm_fb_helper_single_add_all_connectors(helper);
  251. if (ret < 0) {
  252. DRM_ERROR("failed to register drm_fb_helper_connector.\n");
  253. goto err_setup;
  254. }
  255. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  256. if (ret < 0) {
  257. DRM_ERROR("failed to set up hw configuration.\n");
  258. goto err_setup;
  259. }
  260. return 0;
  261. err_setup:
  262. drm_fb_helper_fini(helper);
  263. err_init:
  264. private->fb_helper = NULL;
  265. kfree(fbdev);
  266. return ret;
  267. }
  268. static void exynos_drm_fbdev_destroy(struct drm_device *dev,
  269. struct drm_fb_helper *fb_helper)
  270. {
  271. struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
  272. struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
  273. struct drm_framebuffer *fb;
  274. if (is_drm_iommu_supported(dev) && exynos_gem_obj->buffer->kvaddr)
  275. vunmap(exynos_gem_obj->buffer->kvaddr);
  276. /* release drm framebuffer and real buffer */
  277. if (fb_helper->fb && fb_helper->fb->funcs) {
  278. fb = fb_helper->fb;
  279. if (fb)
  280. drm_framebuffer_remove(fb);
  281. }
  282. /* release linux framebuffer */
  283. if (fb_helper->fbdev) {
  284. struct fb_info *info;
  285. int ret;
  286. info = fb_helper->fbdev;
  287. ret = unregister_framebuffer(info);
  288. if (ret < 0)
  289. DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
  290. if (info->cmap.len)
  291. fb_dealloc_cmap(&info->cmap);
  292. framebuffer_release(info);
  293. }
  294. drm_fb_helper_fini(fb_helper);
  295. }
  296. void exynos_drm_fbdev_fini(struct drm_device *dev)
  297. {
  298. struct exynos_drm_private *private = dev->dev_private;
  299. struct exynos_drm_fbdev *fbdev;
  300. if (!private || !private->fb_helper)
  301. return;
  302. fbdev = to_exynos_fbdev(private->fb_helper);
  303. if (fbdev->exynos_gem_obj)
  304. exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
  305. exynos_drm_fbdev_destroy(dev, private->fb_helper);
  306. kfree(fbdev);
  307. private->fb_helper = NULL;
  308. }
  309. void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
  310. {
  311. struct exynos_drm_private *private = dev->dev_private;
  312. if (!private || !private->fb_helper)
  313. return;
  314. drm_fb_helper_restore_fbdev_mode(private->fb_helper);
  315. }