exynos_drm_fbdev.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. #define MAX_CONNECTOR 4
  36. #define PREFERRED_BPP 32
  37. #define to_exynos_fbdev(x) container_of(x, struct exynos_drm_fbdev,\
  38. drm_fb_helper)
  39. struct exynos_drm_fbdev {
  40. struct drm_fb_helper drm_fb_helper;
  41. struct exynos_drm_gem_obj *exynos_gem_obj;
  42. };
  43. static int exynos_drm_fb_mmap(struct fb_info *info,
  44. struct vm_area_struct *vma)
  45. {
  46. struct drm_fb_helper *helper = info->par;
  47. struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(helper);
  48. struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
  49. struct exynos_drm_gem_buf *buffer = exynos_gem_obj->buffer;
  50. unsigned long vm_size;
  51. int ret;
  52. DRM_DEBUG_KMS("%s\n", __func__);
  53. vma->vm_flags |= VM_IO | VM_DONTEXPAND | VM_DONTDUMP;
  54. vm_size = vma->vm_end - vma->vm_start;
  55. if (vm_size > buffer->size)
  56. return -EINVAL;
  57. ret = dma_mmap_attrs(helper->dev->dev, vma, buffer->pages,
  58. buffer->dma_addr, buffer->size, &buffer->dma_attrs);
  59. if (ret < 0) {
  60. DRM_ERROR("failed to mmap.\n");
  61. return ret;
  62. }
  63. return 0;
  64. }
  65. static struct fb_ops exynos_drm_fb_ops = {
  66. .owner = THIS_MODULE,
  67. .fb_mmap = exynos_drm_fb_mmap,
  68. .fb_fillrect = cfb_fillrect,
  69. .fb_copyarea = cfb_copyarea,
  70. .fb_imageblit = cfb_imageblit,
  71. .fb_check_var = drm_fb_helper_check_var,
  72. .fb_set_par = drm_fb_helper_set_par,
  73. .fb_blank = drm_fb_helper_blank,
  74. .fb_pan_display = drm_fb_helper_pan_display,
  75. .fb_setcmap = drm_fb_helper_setcmap,
  76. };
  77. static int exynos_drm_fbdev_update(struct drm_fb_helper *helper,
  78. struct drm_framebuffer *fb)
  79. {
  80. struct fb_info *fbi = helper->fbdev;
  81. struct drm_device *dev = helper->dev;
  82. struct exynos_drm_gem_buf *buffer;
  83. unsigned int size = fb->width * fb->height * (fb->bits_per_pixel >> 3);
  84. unsigned long offset;
  85. DRM_DEBUG_KMS("%s\n", __FILE__);
  86. drm_fb_helper_fill_fix(fbi, fb->pitches[0], fb->depth);
  87. drm_fb_helper_fill_var(fbi, helper, fb->width, fb->height);
  88. /* RGB formats use only one buffer */
  89. buffer = exynos_drm_fb_buffer(fb, 0);
  90. if (!buffer) {
  91. DRM_LOG_KMS("buffer is null.\n");
  92. return -EFAULT;
  93. }
  94. /* map pages with kernel virtual space. */
  95. if (!buffer->kvaddr) {
  96. unsigned int nr_pages = buffer->size >> PAGE_SHIFT;
  97. buffer->kvaddr = vmap(buffer->pages, nr_pages, VM_MAP,
  98. pgprot_writecombine(PAGE_KERNEL));
  99. if (!buffer->kvaddr) {
  100. DRM_ERROR("failed to map pages to kernel space.\n");
  101. return -EIO;
  102. }
  103. }
  104. /* buffer count to framebuffer always is 1 at booting time. */
  105. exynos_drm_fb_set_buf_cnt(fb, 1);
  106. offset = fbi->var.xoffset * (fb->bits_per_pixel >> 3);
  107. offset += fbi->var.yoffset * fb->pitches[0];
  108. dev->mode_config.fb_base = (resource_size_t)buffer->dma_addr;
  109. fbi->screen_base = buffer->kvaddr + offset;
  110. fbi->fix.smem_start = (unsigned long)
  111. (page_to_phys(sg_page(buffer->sgt->sgl)) + offset);
  112. fbi->screen_size = size;
  113. fbi->fix.smem_len = size;
  114. return 0;
  115. }
  116. static int exynos_drm_fbdev_create(struct drm_fb_helper *helper,
  117. struct drm_fb_helper_surface_size *sizes)
  118. {
  119. struct exynos_drm_fbdev *exynos_fbdev = to_exynos_fbdev(helper);
  120. struct exynos_drm_gem_obj *exynos_gem_obj;
  121. struct drm_device *dev = helper->dev;
  122. struct fb_info *fbi;
  123. struct drm_mode_fb_cmd2 mode_cmd = { 0 };
  124. struct platform_device *pdev = dev->platformdev;
  125. unsigned long size;
  126. int ret;
  127. DRM_DEBUG_KMS("%s\n", __FILE__);
  128. DRM_DEBUG_KMS("surface width(%d), height(%d) and bpp(%d\n",
  129. sizes->surface_width, sizes->surface_height,
  130. sizes->surface_bpp);
  131. mode_cmd.width = sizes->surface_width;
  132. mode_cmd.height = sizes->surface_height;
  133. mode_cmd.pitches[0] = sizes->surface_width * (sizes->surface_bpp >> 3);
  134. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  135. sizes->surface_depth);
  136. mutex_lock(&dev->struct_mutex);
  137. fbi = framebuffer_alloc(0, &pdev->dev);
  138. if (!fbi) {
  139. DRM_ERROR("failed to allocate fb info.\n");
  140. ret = -ENOMEM;
  141. goto out;
  142. }
  143. size = mode_cmd.pitches[0] * mode_cmd.height;
  144. /* 0 means to allocate physically continuous memory */
  145. exynos_gem_obj = exynos_drm_gem_create(dev, 0, size);
  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_OR_NULL(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 int exynos_drm_fbdev_probe(struct drm_fb_helper *helper,
  190. struct drm_fb_helper_surface_size *sizes)
  191. {
  192. int ret = 0;
  193. DRM_DEBUG_KMS("%s\n", __FILE__);
  194. /*
  195. * with !helper->fb, it means that this funcion is called first time
  196. * and after that, the helper->fb would be used as clone mode.
  197. */
  198. if (!helper->fb) {
  199. ret = exynos_drm_fbdev_create(helper, sizes);
  200. if (ret < 0) {
  201. DRM_ERROR("failed to create fbdev.\n");
  202. return ret;
  203. }
  204. /*
  205. * fb_helper expects a value more than 1 if succeed
  206. * because register_framebuffer() should be called.
  207. */
  208. ret = 1;
  209. }
  210. return ret;
  211. }
  212. static struct drm_fb_helper_funcs exynos_drm_fb_helper_funcs = {
  213. .fb_probe = exynos_drm_fbdev_probe,
  214. };
  215. int exynos_drm_fbdev_init(struct drm_device *dev)
  216. {
  217. struct exynos_drm_fbdev *fbdev;
  218. struct exynos_drm_private *private = dev->dev_private;
  219. struct drm_fb_helper *helper;
  220. unsigned int num_crtc;
  221. int ret;
  222. DRM_DEBUG_KMS("%s\n", __FILE__);
  223. if (!dev->mode_config.num_crtc || !dev->mode_config.num_connector)
  224. return 0;
  225. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  226. if (!fbdev) {
  227. DRM_ERROR("failed to allocate drm fbdev.\n");
  228. return -ENOMEM;
  229. }
  230. private->fb_helper = helper = &fbdev->drm_fb_helper;
  231. helper->funcs = &exynos_drm_fb_helper_funcs;
  232. num_crtc = dev->mode_config.num_crtc;
  233. ret = drm_fb_helper_init(dev, helper, num_crtc, MAX_CONNECTOR);
  234. if (ret < 0) {
  235. DRM_ERROR("failed to initialize drm fb helper.\n");
  236. goto err_init;
  237. }
  238. ret = drm_fb_helper_single_add_all_connectors(helper);
  239. if (ret < 0) {
  240. DRM_ERROR("failed to register drm_fb_helper_connector.\n");
  241. goto err_setup;
  242. }
  243. ret = drm_fb_helper_initial_config(helper, PREFERRED_BPP);
  244. if (ret < 0) {
  245. DRM_ERROR("failed to set up hw configuration.\n");
  246. goto err_setup;
  247. }
  248. return 0;
  249. err_setup:
  250. drm_fb_helper_fini(helper);
  251. err_init:
  252. private->fb_helper = NULL;
  253. kfree(fbdev);
  254. return ret;
  255. }
  256. static void exynos_drm_fbdev_destroy(struct drm_device *dev,
  257. struct drm_fb_helper *fb_helper)
  258. {
  259. struct exynos_drm_fbdev *exynos_fbd = to_exynos_fbdev(fb_helper);
  260. struct exynos_drm_gem_obj *exynos_gem_obj = exynos_fbd->exynos_gem_obj;
  261. struct drm_framebuffer *fb;
  262. if (exynos_gem_obj->buffer->kvaddr)
  263. vunmap(exynos_gem_obj->buffer->kvaddr);
  264. /* release drm framebuffer and real buffer */
  265. if (fb_helper->fb && fb_helper->fb->funcs) {
  266. fb = fb_helper->fb;
  267. if (fb)
  268. drm_framebuffer_remove(fb);
  269. }
  270. /* release linux framebuffer */
  271. if (fb_helper->fbdev) {
  272. struct fb_info *info;
  273. int ret;
  274. info = fb_helper->fbdev;
  275. ret = unregister_framebuffer(info);
  276. if (ret < 0)
  277. DRM_DEBUG_KMS("failed unregister_framebuffer()\n");
  278. if (info->cmap.len)
  279. fb_dealloc_cmap(&info->cmap);
  280. framebuffer_release(info);
  281. }
  282. drm_fb_helper_fini(fb_helper);
  283. }
  284. void exynos_drm_fbdev_fini(struct drm_device *dev)
  285. {
  286. struct exynos_drm_private *private = dev->dev_private;
  287. struct exynos_drm_fbdev *fbdev;
  288. if (!private || !private->fb_helper)
  289. return;
  290. fbdev = to_exynos_fbdev(private->fb_helper);
  291. if (fbdev->exynos_gem_obj)
  292. exynos_drm_gem_destroy(fbdev->exynos_gem_obj);
  293. exynos_drm_fbdev_destroy(dev, private->fb_helper);
  294. kfree(fbdev);
  295. private->fb_helper = NULL;
  296. }
  297. void exynos_drm_fbdev_restore_mode(struct drm_device *dev)
  298. {
  299. struct exynos_drm_private *private = dev->dev_private;
  300. if (!private || !private->fb_helper)
  301. return;
  302. drm_fb_helper_restore_fbdev_mode(private->fb_helper);
  303. }