exynos_drm_plane.c 9.0 KB

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