exynos_drm_plane.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  3. * Authors: Joonyoung Shim <jy0922.shim@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. */
  11. #include <drm/drmP.h>
  12. #include <drm/drm_atomic.h>
  13. #include <drm/drm_atomic_helper.h>
  14. #include <drm/drm_plane_helper.h>
  15. #include <drm/exynos_drm.h>
  16. #include "exynos_drm_drv.h"
  17. #include "exynos_drm_crtc.h"
  18. #include "exynos_drm_fb.h"
  19. #include "exynos_drm_gem.h"
  20. #include "exynos_drm_plane.h"
  21. /*
  22. * This function is to get X or Y size shown via screen. This needs length and
  23. * start position of CRTC.
  24. *
  25. * <--- length --->
  26. * CRTC ----------------
  27. * ^ start ^ end
  28. *
  29. * There are six cases from a to f.
  30. *
  31. * <----- SCREEN ----->
  32. * 0 last
  33. * ----------|------------------|----------
  34. * CRTCs
  35. * a -------
  36. * b -------
  37. * c --------------------------
  38. * d --------
  39. * e -------
  40. * f -------
  41. */
  42. static int exynos_plane_get_size(int start, unsigned length, unsigned last)
  43. {
  44. int end = start + length;
  45. int size = 0;
  46. if (start <= 0) {
  47. if (end > 0)
  48. size = min_t(unsigned, end, last);
  49. } else if (start <= last) {
  50. size = min_t(unsigned, last - start, length);
  51. }
  52. return size;
  53. }
  54. static void exynos_plane_mode_set(struct exynos_drm_plane_state *exynos_state)
  55. {
  56. struct drm_plane_state *state = &exynos_state->base;
  57. struct drm_crtc *crtc = state->crtc;
  58. struct drm_crtc_state *crtc_state =
  59. drm_atomic_get_existing_crtc_state(state->state, crtc);
  60. struct drm_display_mode *mode = &crtc_state->adjusted_mode;
  61. int crtc_x, crtc_y;
  62. unsigned int crtc_w, crtc_h;
  63. unsigned int src_x, src_y;
  64. unsigned int src_w, src_h;
  65. unsigned int actual_w;
  66. unsigned int actual_h;
  67. /*
  68. * The original src/dest coordinates are stored in exynos_state->base,
  69. * but we want to keep another copy internal to our driver that we can
  70. * clip/modify ourselves.
  71. */
  72. crtc_x = state->crtc_x;
  73. crtc_y = state->crtc_y;
  74. crtc_w = state->crtc_w;
  75. crtc_h = state->crtc_h;
  76. src_x = state->src_x >> 16;
  77. src_y = state->src_y >> 16;
  78. src_w = state->src_w >> 16;
  79. src_h = state->src_h >> 16;
  80. /* set ratio */
  81. exynos_state->h_ratio = (src_w << 16) / crtc_w;
  82. exynos_state->v_ratio = (src_h << 16) / crtc_h;
  83. /* clip to visible area */
  84. actual_w = exynos_plane_get_size(crtc_x, crtc_w, mode->hdisplay);
  85. actual_h = exynos_plane_get_size(crtc_y, crtc_h, mode->vdisplay);
  86. if (crtc_x < 0) {
  87. if (actual_w)
  88. src_x += ((-crtc_x) * exynos_state->h_ratio) >> 16;
  89. crtc_x = 0;
  90. }
  91. if (crtc_y < 0) {
  92. if (actual_h)
  93. src_y += ((-crtc_y) * exynos_state->v_ratio) >> 16;
  94. crtc_y = 0;
  95. }
  96. /* set drm framebuffer data. */
  97. exynos_state->src.x = src_x;
  98. exynos_state->src.y = src_y;
  99. exynos_state->src.w = (actual_w * exynos_state->h_ratio) >> 16;
  100. exynos_state->src.h = (actual_h * exynos_state->v_ratio) >> 16;
  101. /* set plane range to be displayed. */
  102. exynos_state->crtc.x = crtc_x;
  103. exynos_state->crtc.y = crtc_y;
  104. exynos_state->crtc.w = actual_w;
  105. exynos_state->crtc.h = actual_h;
  106. DRM_DEBUG_KMS("plane : offset_x/y(%d,%d), width/height(%d,%d)",
  107. exynos_state->crtc.x, exynos_state->crtc.y,
  108. exynos_state->crtc.w, exynos_state->crtc.h);
  109. }
  110. static void exynos_drm_plane_reset(struct drm_plane *plane)
  111. {
  112. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  113. struct exynos_drm_plane_state *exynos_state;
  114. if (plane->state) {
  115. exynos_state = to_exynos_plane_state(plane->state);
  116. if (exynos_state->base.fb)
  117. drm_framebuffer_unreference(exynos_state->base.fb);
  118. kfree(exynos_state);
  119. plane->state = NULL;
  120. }
  121. exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
  122. if (exynos_state) {
  123. plane->state = &exynos_state->base;
  124. plane->state->plane = plane;
  125. plane->state->zpos = exynos_plane->config->zpos;
  126. }
  127. }
  128. static struct drm_plane_state *
  129. exynos_drm_plane_duplicate_state(struct drm_plane *plane)
  130. {
  131. struct exynos_drm_plane_state *exynos_state;
  132. struct exynos_drm_plane_state *copy;
  133. exynos_state = to_exynos_plane_state(plane->state);
  134. copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
  135. if (!copy)
  136. return NULL;
  137. __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
  138. return &copy->base;
  139. }
  140. static void exynos_drm_plane_destroy_state(struct drm_plane *plane,
  141. struct drm_plane_state *old_state)
  142. {
  143. struct exynos_drm_plane_state *old_exynos_state =
  144. to_exynos_plane_state(old_state);
  145. __drm_atomic_helper_plane_destroy_state(old_state);
  146. kfree(old_exynos_state);
  147. }
  148. static struct drm_plane_funcs exynos_plane_funcs = {
  149. .update_plane = drm_atomic_helper_update_plane,
  150. .disable_plane = drm_atomic_helper_disable_plane,
  151. .destroy = drm_plane_cleanup,
  152. .reset = exynos_drm_plane_reset,
  153. .atomic_duplicate_state = exynos_drm_plane_duplicate_state,
  154. .atomic_destroy_state = exynos_drm_plane_destroy_state,
  155. };
  156. static int
  157. exynos_drm_plane_check_format(const struct exynos_drm_plane_config *config,
  158. struct exynos_drm_plane_state *state)
  159. {
  160. struct drm_framebuffer *fb = state->base.fb;
  161. switch (fb->modifier) {
  162. case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
  163. if (!(config->capabilities & EXYNOS_DRM_PLANE_CAP_TILE))
  164. return -ENOTSUPP;
  165. break;
  166. case DRM_FORMAT_MOD_LINEAR:
  167. break;
  168. default:
  169. DRM_ERROR("unsupported pixel format modifier");
  170. return -ENOTSUPP;
  171. }
  172. return 0;
  173. }
  174. static int
  175. exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config,
  176. struct exynos_drm_plane_state *state)
  177. {
  178. bool width_ok = false, height_ok = false;
  179. if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE)
  180. return 0;
  181. if (state->src.w == state->crtc.w)
  182. width_ok = true;
  183. if (state->src.h == state->crtc.h)
  184. height_ok = true;
  185. if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
  186. state->h_ratio == (1 << 15))
  187. width_ok = true;
  188. if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
  189. state->v_ratio == (1 << 15))
  190. height_ok = true;
  191. if (width_ok && height_ok)
  192. return 0;
  193. DRM_DEBUG_KMS("scaling mode is not supported");
  194. return -ENOTSUPP;
  195. }
  196. static int exynos_plane_atomic_check(struct drm_plane *plane,
  197. struct drm_plane_state *state)
  198. {
  199. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  200. struct exynos_drm_plane_state *exynos_state =
  201. to_exynos_plane_state(state);
  202. int ret = 0;
  203. if (!state->crtc || !state->fb)
  204. return 0;
  205. /* translate state into exynos_state */
  206. exynos_plane_mode_set(exynos_state);
  207. ret = exynos_drm_plane_check_format(exynos_plane->config, exynos_state);
  208. if (ret)
  209. return ret;
  210. ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state);
  211. return ret;
  212. }
  213. static void exynos_plane_atomic_update(struct drm_plane *plane,
  214. struct drm_plane_state *old_state)
  215. {
  216. struct drm_plane_state *state = plane->state;
  217. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
  218. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  219. if (!state->crtc)
  220. return;
  221. plane->crtc = state->crtc;
  222. if (exynos_crtc->ops->update_plane)
  223. exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
  224. }
  225. static void exynos_plane_atomic_disable(struct drm_plane *plane,
  226. struct drm_plane_state *old_state)
  227. {
  228. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  229. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
  230. if (!old_state->crtc)
  231. return;
  232. if (exynos_crtc->ops->disable_plane)
  233. exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane);
  234. }
  235. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  236. .atomic_check = exynos_plane_atomic_check,
  237. .atomic_update = exynos_plane_atomic_update,
  238. .atomic_disable = exynos_plane_atomic_disable,
  239. };
  240. static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
  241. bool immutable)
  242. {
  243. /* FIXME */
  244. if (immutable)
  245. drm_plane_create_zpos_immutable_property(plane, 0);
  246. else
  247. drm_plane_create_zpos_property(plane, 0, 0, MAX_PLANE - 1);
  248. }
  249. int exynos_plane_init(struct drm_device *dev,
  250. struct exynos_drm_plane *exynos_plane, unsigned int index,
  251. const struct exynos_drm_plane_config *config)
  252. {
  253. int err;
  254. err = drm_universal_plane_init(dev, &exynos_plane->base,
  255. 1 << dev->mode_config.num_crtc,
  256. &exynos_plane_funcs,
  257. config->pixel_formats,
  258. config->num_pixel_formats,
  259. NULL, config->type, NULL);
  260. if (err) {
  261. DRM_ERROR("failed to initialize plane\n");
  262. return err;
  263. }
  264. drm_plane_helper_add(&exynos_plane->base, &plane_helper_funcs);
  265. exynos_plane->index = index;
  266. exynos_plane->config = config;
  267. exynos_plane_attach_zpos_property(&exynos_plane->base,
  268. !(config->capabilities & EXYNOS_DRM_PLANE_CAP_ZPOS));
  269. return 0;
  270. }