exynos_drm_plane.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. __drm_atomic_helper_plane_destroy_state(plane->state);
  117. kfree(exynos_state);
  118. plane->state = NULL;
  119. }
  120. exynos_state = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
  121. if (exynos_state) {
  122. __drm_atomic_helper_plane_reset(plane, &exynos_state->base);
  123. plane->state->zpos = exynos_plane->config->zpos;
  124. }
  125. }
  126. static struct drm_plane_state *
  127. exynos_drm_plane_duplicate_state(struct drm_plane *plane)
  128. {
  129. struct exynos_drm_plane_state *exynos_state;
  130. struct exynos_drm_plane_state *copy;
  131. exynos_state = to_exynos_plane_state(plane->state);
  132. copy = kzalloc(sizeof(*exynos_state), GFP_KERNEL);
  133. if (!copy)
  134. return NULL;
  135. __drm_atomic_helper_plane_duplicate_state(plane, &copy->base);
  136. return &copy->base;
  137. }
  138. static void exynos_drm_plane_destroy_state(struct drm_plane *plane,
  139. struct drm_plane_state *old_state)
  140. {
  141. struct exynos_drm_plane_state *old_exynos_state =
  142. to_exynos_plane_state(old_state);
  143. __drm_atomic_helper_plane_destroy_state(old_state);
  144. kfree(old_exynos_state);
  145. }
  146. static struct drm_plane_funcs exynos_plane_funcs = {
  147. .update_plane = drm_atomic_helper_update_plane,
  148. .disable_plane = drm_atomic_helper_disable_plane,
  149. .destroy = drm_plane_cleanup,
  150. .reset = exynos_drm_plane_reset,
  151. .atomic_duplicate_state = exynos_drm_plane_duplicate_state,
  152. .atomic_destroy_state = exynos_drm_plane_destroy_state,
  153. };
  154. static int
  155. exynos_drm_plane_check_format(const struct exynos_drm_plane_config *config,
  156. struct exynos_drm_plane_state *state)
  157. {
  158. struct drm_framebuffer *fb = state->base.fb;
  159. switch (fb->modifier) {
  160. case DRM_FORMAT_MOD_SAMSUNG_64_32_TILE:
  161. if (!(config->capabilities & EXYNOS_DRM_PLANE_CAP_TILE))
  162. return -ENOTSUPP;
  163. break;
  164. case DRM_FORMAT_MOD_LINEAR:
  165. break;
  166. default:
  167. DRM_ERROR("unsupported pixel format modifier");
  168. return -ENOTSUPP;
  169. }
  170. return 0;
  171. }
  172. static int
  173. exynos_drm_plane_check_size(const struct exynos_drm_plane_config *config,
  174. struct exynos_drm_plane_state *state)
  175. {
  176. bool width_ok = false, height_ok = false;
  177. if (config->capabilities & EXYNOS_DRM_PLANE_CAP_SCALE)
  178. return 0;
  179. if (state->src.w == state->crtc.w)
  180. width_ok = true;
  181. if (state->src.h == state->crtc.h)
  182. height_ok = true;
  183. if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
  184. state->h_ratio == (1 << 15))
  185. width_ok = true;
  186. if ((config->capabilities & EXYNOS_DRM_PLANE_CAP_DOUBLE) &&
  187. state->v_ratio == (1 << 15))
  188. height_ok = true;
  189. if (width_ok && height_ok)
  190. return 0;
  191. DRM_DEBUG_KMS("scaling mode is not supported");
  192. return -ENOTSUPP;
  193. }
  194. static int exynos_plane_atomic_check(struct drm_plane *plane,
  195. struct drm_plane_state *state)
  196. {
  197. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  198. struct exynos_drm_plane_state *exynos_state =
  199. to_exynos_plane_state(state);
  200. int ret = 0;
  201. if (!state->crtc || !state->fb)
  202. return 0;
  203. /* translate state into exynos_state */
  204. exynos_plane_mode_set(exynos_state);
  205. ret = exynos_drm_plane_check_format(exynos_plane->config, exynos_state);
  206. if (ret)
  207. return ret;
  208. ret = exynos_drm_plane_check_size(exynos_plane->config, exynos_state);
  209. return ret;
  210. }
  211. static void exynos_plane_atomic_update(struct drm_plane *plane,
  212. struct drm_plane_state *old_state)
  213. {
  214. struct drm_plane_state *state = plane->state;
  215. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(state->crtc);
  216. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  217. if (!state->crtc)
  218. return;
  219. if (exynos_crtc->ops->update_plane)
  220. exynos_crtc->ops->update_plane(exynos_crtc, exynos_plane);
  221. }
  222. static void exynos_plane_atomic_disable(struct drm_plane *plane,
  223. struct drm_plane_state *old_state)
  224. {
  225. struct exynos_drm_plane *exynos_plane = to_exynos_plane(plane);
  226. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(old_state->crtc);
  227. if (!old_state->crtc)
  228. return;
  229. if (exynos_crtc->ops->disable_plane)
  230. exynos_crtc->ops->disable_plane(exynos_crtc, exynos_plane);
  231. }
  232. static const struct drm_plane_helper_funcs plane_helper_funcs = {
  233. .atomic_check = exynos_plane_atomic_check,
  234. .atomic_update = exynos_plane_atomic_update,
  235. .atomic_disable = exynos_plane_atomic_disable,
  236. };
  237. static void exynos_plane_attach_zpos_property(struct drm_plane *plane,
  238. int zpos, bool immutable)
  239. {
  240. if (immutable)
  241. drm_plane_create_zpos_immutable_property(plane, zpos);
  242. else
  243. drm_plane_create_zpos_property(plane, zpos, 0, MAX_PLANE - 1);
  244. }
  245. int exynos_plane_init(struct drm_device *dev,
  246. struct exynos_drm_plane *exynos_plane, unsigned int index,
  247. const struct exynos_drm_plane_config *config)
  248. {
  249. int err;
  250. unsigned int supported_modes = BIT(DRM_MODE_BLEND_PIXEL_NONE) |
  251. BIT(DRM_MODE_BLEND_PREMULTI) |
  252. BIT(DRM_MODE_BLEND_COVERAGE);
  253. struct drm_plane *plane = &exynos_plane->base;
  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, config->zpos,
  268. !(config->capabilities & EXYNOS_DRM_PLANE_CAP_ZPOS));
  269. if (config->capabilities & EXYNOS_DRM_PLANE_CAP_PIX_BLEND)
  270. drm_plane_create_blend_mode_property(plane, supported_modes);
  271. if (config->capabilities & EXYNOS_DRM_PLANE_CAP_WIN_BLEND)
  272. drm_plane_create_alpha_property(plane);
  273. return 0;
  274. }