exynos_drm_fb.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* exynos_drm_fb.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_crtc_helper.h>
  31. #include <drm/drm_fb_helper.h>
  32. #include <uapi/drm/exynos_drm.h>
  33. #include "exynos_drm_drv.h"
  34. #include "exynos_drm_fb.h"
  35. #include "exynos_drm_gem.h"
  36. #include "exynos_drm_iommu.h"
  37. #include "exynos_drm_encoder.h"
  38. #define to_exynos_fb(x) container_of(x, struct exynos_drm_fb, fb)
  39. /*
  40. * exynos specific framebuffer structure.
  41. *
  42. * @fb: drm framebuffer obejct.
  43. * @buf_cnt: a buffer count to drm framebuffer.
  44. * @exynos_gem_obj: array of exynos specific gem object containing a gem object.
  45. */
  46. struct exynos_drm_fb {
  47. struct drm_framebuffer fb;
  48. unsigned int buf_cnt;
  49. struct exynos_drm_gem_obj *exynos_gem_obj[MAX_FB_BUFFER];
  50. };
  51. static int check_fb_gem_memory_type(struct drm_device *drm_dev,
  52. struct exynos_drm_gem_obj *exynos_gem_obj)
  53. {
  54. unsigned int flags;
  55. /*
  56. * if exynos drm driver supports iommu then framebuffer can use
  57. * all the buffer types.
  58. */
  59. if (is_drm_iommu_supported(drm_dev))
  60. return 0;
  61. flags = exynos_gem_obj->flags;
  62. /*
  63. * without iommu support, not support physically non-continuous memory
  64. * for framebuffer.
  65. */
  66. if (IS_NONCONTIG_BUFFER(flags)) {
  67. DRM_ERROR("cannot use this gem memory type for fb.\n");
  68. return -EINVAL;
  69. }
  70. return 0;
  71. }
  72. static void exynos_drm_fb_destroy(struct drm_framebuffer *fb)
  73. {
  74. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  75. unsigned int i;
  76. DRM_DEBUG_KMS("%s\n", __FILE__);
  77. /* make sure that overlay data are updated before relesing fb. */
  78. exynos_drm_encoder_complete_scanout(fb);
  79. drm_framebuffer_cleanup(fb);
  80. for (i = 0; i < ARRAY_SIZE(exynos_fb->exynos_gem_obj); i++) {
  81. struct drm_gem_object *obj;
  82. if (exynos_fb->exynos_gem_obj[i] == NULL)
  83. continue;
  84. obj = &exynos_fb->exynos_gem_obj[i]->base;
  85. drm_gem_object_unreference_unlocked(obj);
  86. }
  87. kfree(exynos_fb);
  88. exynos_fb = NULL;
  89. }
  90. static int exynos_drm_fb_create_handle(struct drm_framebuffer *fb,
  91. struct drm_file *file_priv,
  92. unsigned int *handle)
  93. {
  94. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  95. DRM_DEBUG_KMS("%s\n", __FILE__);
  96. return drm_gem_handle_create(file_priv,
  97. &exynos_fb->exynos_gem_obj[0]->base, handle);
  98. }
  99. static int exynos_drm_fb_dirty(struct drm_framebuffer *fb,
  100. struct drm_file *file_priv, unsigned flags,
  101. unsigned color, struct drm_clip_rect *clips,
  102. unsigned num_clips)
  103. {
  104. DRM_DEBUG_KMS("%s\n", __FILE__);
  105. /* TODO */
  106. return 0;
  107. }
  108. static struct drm_framebuffer_funcs exynos_drm_fb_funcs = {
  109. .destroy = exynos_drm_fb_destroy,
  110. .create_handle = exynos_drm_fb_create_handle,
  111. .dirty = exynos_drm_fb_dirty,
  112. };
  113. void exynos_drm_fb_set_buf_cnt(struct drm_framebuffer *fb,
  114. unsigned int cnt)
  115. {
  116. struct exynos_drm_fb *exynos_fb;
  117. exynos_fb = to_exynos_fb(fb);
  118. exynos_fb->buf_cnt = cnt;
  119. }
  120. unsigned int exynos_drm_fb_get_buf_cnt(struct drm_framebuffer *fb)
  121. {
  122. struct exynos_drm_fb *exynos_fb;
  123. exynos_fb = to_exynos_fb(fb);
  124. return exynos_fb->buf_cnt;
  125. }
  126. struct drm_framebuffer *
  127. exynos_drm_framebuffer_init(struct drm_device *dev,
  128. struct drm_mode_fb_cmd2 *mode_cmd,
  129. struct drm_gem_object *obj)
  130. {
  131. struct exynos_drm_fb *exynos_fb;
  132. struct exynos_drm_gem_obj *exynos_gem_obj;
  133. int ret;
  134. exynos_gem_obj = to_exynos_gem_obj(obj);
  135. ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
  136. if (ret < 0) {
  137. DRM_ERROR("cannot use this gem memory type for fb.\n");
  138. return ERR_PTR(-EINVAL);
  139. }
  140. exynos_fb = kzalloc(sizeof(*exynos_fb), GFP_KERNEL);
  141. if (!exynos_fb) {
  142. DRM_ERROR("failed to allocate exynos drm framebuffer\n");
  143. return ERR_PTR(-ENOMEM);
  144. }
  145. exynos_fb->exynos_gem_obj[0] = exynos_gem_obj;
  146. ret = drm_framebuffer_init(dev, &exynos_fb->fb, &exynos_drm_fb_funcs);
  147. if (ret) {
  148. DRM_ERROR("failed to initialize framebuffer\n");
  149. return ERR_PTR(ret);
  150. }
  151. drm_helper_mode_fill_fb_struct(&exynos_fb->fb, mode_cmd);
  152. return &exynos_fb->fb;
  153. }
  154. static u32 exynos_drm_format_num_buffers(struct drm_mode_fb_cmd2 *mode_cmd)
  155. {
  156. unsigned int cnt = 0;
  157. if (mode_cmd->pixel_format != DRM_FORMAT_NV12)
  158. return drm_format_num_planes(mode_cmd->pixel_format);
  159. while (cnt != MAX_FB_BUFFER) {
  160. if (!mode_cmd->handles[cnt])
  161. break;
  162. cnt++;
  163. }
  164. /*
  165. * check if NV12 or NV12M.
  166. *
  167. * NV12
  168. * handles[0] = base1, offsets[0] = 0
  169. * handles[1] = base1, offsets[1] = Y_size
  170. *
  171. * NV12M
  172. * handles[0] = base1, offsets[0] = 0
  173. * handles[1] = base2, offsets[1] = 0
  174. */
  175. if (cnt == 2) {
  176. /*
  177. * in case of NV12 format, offsets[1] is not 0 and
  178. * handles[0] is same as handles[1].
  179. */
  180. if (mode_cmd->offsets[1] &&
  181. mode_cmd->handles[0] == mode_cmd->handles[1])
  182. cnt = 1;
  183. }
  184. return cnt;
  185. }
  186. static struct drm_framebuffer *
  187. exynos_user_fb_create(struct drm_device *dev, struct drm_file *file_priv,
  188. struct drm_mode_fb_cmd2 *mode_cmd)
  189. {
  190. struct drm_gem_object *obj;
  191. struct drm_framebuffer *fb;
  192. struct exynos_drm_fb *exynos_fb;
  193. int i;
  194. DRM_DEBUG_KMS("%s\n", __FILE__);
  195. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  196. if (!obj) {
  197. DRM_ERROR("failed to lookup gem object\n");
  198. return ERR_PTR(-ENOENT);
  199. }
  200. fb = exynos_drm_framebuffer_init(dev, mode_cmd, obj);
  201. if (IS_ERR(fb)) {
  202. drm_gem_object_unreference_unlocked(obj);
  203. return fb;
  204. }
  205. exynos_fb = to_exynos_fb(fb);
  206. exynos_fb->buf_cnt = exynos_drm_format_num_buffers(mode_cmd);
  207. DRM_DEBUG_KMS("buf_cnt = %d\n", exynos_fb->buf_cnt);
  208. for (i = 1; i < exynos_fb->buf_cnt; i++) {
  209. struct exynos_drm_gem_obj *exynos_gem_obj;
  210. int ret;
  211. obj = drm_gem_object_lookup(dev, file_priv,
  212. mode_cmd->handles[i]);
  213. if (!obj) {
  214. DRM_ERROR("failed to lookup gem object\n");
  215. exynos_drm_fb_destroy(fb);
  216. return ERR_PTR(-ENOENT);
  217. }
  218. exynos_gem_obj = to_exynos_gem_obj(obj);
  219. ret = check_fb_gem_memory_type(dev, exynos_gem_obj);
  220. if (ret < 0) {
  221. DRM_ERROR("cannot use this gem memory type for fb.\n");
  222. exynos_drm_fb_destroy(fb);
  223. return ERR_PTR(ret);
  224. }
  225. exynos_fb->exynos_gem_obj[i] = to_exynos_gem_obj(obj);
  226. }
  227. return fb;
  228. }
  229. struct exynos_drm_gem_buf *exynos_drm_fb_buffer(struct drm_framebuffer *fb,
  230. int index)
  231. {
  232. struct exynos_drm_fb *exynos_fb = to_exynos_fb(fb);
  233. struct exynos_drm_gem_buf *buffer;
  234. DRM_DEBUG_KMS("%s\n", __FILE__);
  235. if (index >= MAX_FB_BUFFER)
  236. return NULL;
  237. buffer = exynos_fb->exynos_gem_obj[index]->buffer;
  238. if (!buffer)
  239. return NULL;
  240. DRM_DEBUG_KMS("vaddr = 0x%lx, dma_addr = 0x%lx\n",
  241. (unsigned long)buffer->kvaddr,
  242. (unsigned long)buffer->dma_addr);
  243. return buffer;
  244. }
  245. static void exynos_drm_output_poll_changed(struct drm_device *dev)
  246. {
  247. struct exynos_drm_private *private = dev->dev_private;
  248. struct drm_fb_helper *fb_helper = private->fb_helper;
  249. if (fb_helper)
  250. drm_fb_helper_hotplug_event(fb_helper);
  251. }
  252. static const struct drm_mode_config_funcs exynos_drm_mode_config_funcs = {
  253. .fb_create = exynos_user_fb_create,
  254. .output_poll_changed = exynos_drm_output_poll_changed,
  255. };
  256. void exynos_drm_mode_config_init(struct drm_device *dev)
  257. {
  258. dev->mode_config.min_width = 0;
  259. dev->mode_config.min_height = 0;
  260. /*
  261. * set max width and height as default value(4096x4096).
  262. * this value would be used to check framebuffer size limitation
  263. * at drm_mode_addfb().
  264. */
  265. dev->mode_config.max_width = 4096;
  266. dev->mode_config.max_height = 4096;
  267. dev->mode_config.funcs = &exynos_drm_mode_config_funcs;
  268. }