exynos_drm_plane.c 9.2 KB

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