mdp4_plane.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.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 version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "mdp4_kms.h"
  18. #define DOWN_SCALE_MAX 8
  19. #define UP_SCALE_MAX 8
  20. struct mdp4_plane {
  21. struct drm_plane base;
  22. const char *name;
  23. enum mdp4_pipe pipe;
  24. uint32_t caps;
  25. uint32_t nformats;
  26. uint32_t formats[32];
  27. bool enabled;
  28. };
  29. #define to_mdp4_plane(x) container_of(x, struct mdp4_plane, base)
  30. /* MDP format helper functions */
  31. static inline
  32. enum mdp4_frame_format mdp4_get_frame_format(struct drm_framebuffer *fb)
  33. {
  34. bool is_tile = false;
  35. if (fb->modifier == DRM_FORMAT_MOD_SAMSUNG_64_32_TILE)
  36. is_tile = true;
  37. if (fb->format->format == DRM_FORMAT_NV12 && is_tile)
  38. return FRAME_TILE_YCBCR_420;
  39. return FRAME_LINEAR;
  40. }
  41. static void mdp4_plane_set_scanout(struct drm_plane *plane,
  42. struct drm_framebuffer *fb);
  43. static int mdp4_plane_mode_set(struct drm_plane *plane,
  44. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  45. int crtc_x, int crtc_y,
  46. unsigned int crtc_w, unsigned int crtc_h,
  47. uint32_t src_x, uint32_t src_y,
  48. uint32_t src_w, uint32_t src_h);
  49. static struct mdp4_kms *get_kms(struct drm_plane *plane)
  50. {
  51. struct msm_drm_private *priv = plane->dev->dev_private;
  52. return to_mdp4_kms(to_mdp_kms(priv->kms));
  53. }
  54. static void mdp4_plane_destroy(struct drm_plane *plane)
  55. {
  56. struct mdp4_plane *mdp4_plane = to_mdp4_plane(plane);
  57. drm_plane_helper_disable(plane);
  58. drm_plane_cleanup(plane);
  59. kfree(mdp4_plane);
  60. }
  61. /* helper to install properties which are common to planes and crtcs */
  62. static void mdp4_plane_install_properties(struct drm_plane *plane,
  63. struct drm_mode_object *obj)
  64. {
  65. // XXX
  66. }
  67. static int mdp4_plane_set_property(struct drm_plane *plane,
  68. struct drm_property *property, uint64_t val)
  69. {
  70. // XXX
  71. return -EINVAL;
  72. }
  73. static const struct drm_plane_funcs mdp4_plane_funcs = {
  74. .update_plane = drm_atomic_helper_update_plane,
  75. .disable_plane = drm_atomic_helper_disable_plane,
  76. .destroy = mdp4_plane_destroy,
  77. .set_property = mdp4_plane_set_property,
  78. .reset = drm_atomic_helper_plane_reset,
  79. .atomic_duplicate_state = drm_atomic_helper_plane_duplicate_state,
  80. .atomic_destroy_state = drm_atomic_helper_plane_destroy_state,
  81. };
  82. static int mdp4_plane_prepare_fb(struct drm_plane *plane,
  83. struct drm_plane_state *new_state)
  84. {
  85. struct mdp4_plane *mdp4_plane = to_mdp4_plane(plane);
  86. struct mdp4_kms *mdp4_kms = get_kms(plane);
  87. struct msm_kms *kms = &mdp4_kms->base.base;
  88. struct drm_framebuffer *fb = new_state->fb;
  89. if (!fb)
  90. return 0;
  91. DBG("%s: prepare: FB[%u]", mdp4_plane->name, fb->base.id);
  92. return msm_framebuffer_prepare(fb, kms->aspace);
  93. }
  94. static void mdp4_plane_cleanup_fb(struct drm_plane *plane,
  95. struct drm_plane_state *old_state)
  96. {
  97. struct mdp4_plane *mdp4_plane = to_mdp4_plane(plane);
  98. struct mdp4_kms *mdp4_kms = get_kms(plane);
  99. struct msm_kms *kms = &mdp4_kms->base.base;
  100. struct drm_framebuffer *fb = old_state->fb;
  101. if (!fb)
  102. return;
  103. DBG("%s: cleanup: FB[%u]", mdp4_plane->name, fb->base.id);
  104. msm_framebuffer_cleanup(fb, kms->aspace);
  105. }
  106. static int mdp4_plane_atomic_check(struct drm_plane *plane,
  107. struct drm_plane_state *state)
  108. {
  109. return 0;
  110. }
  111. static void mdp4_plane_atomic_update(struct drm_plane *plane,
  112. struct drm_plane_state *old_state)
  113. {
  114. struct drm_plane_state *state = plane->state;
  115. int ret;
  116. ret = mdp4_plane_mode_set(plane,
  117. state->crtc, state->fb,
  118. state->crtc_x, state->crtc_y,
  119. state->crtc_w, state->crtc_h,
  120. state->src_x, state->src_y,
  121. state->src_w, state->src_h);
  122. /* atomic_check should have ensured that this doesn't fail */
  123. WARN_ON(ret < 0);
  124. }
  125. static const struct drm_plane_helper_funcs mdp4_plane_helper_funcs = {
  126. .prepare_fb = mdp4_plane_prepare_fb,
  127. .cleanup_fb = mdp4_plane_cleanup_fb,
  128. .atomic_check = mdp4_plane_atomic_check,
  129. .atomic_update = mdp4_plane_atomic_update,
  130. };
  131. static void mdp4_plane_set_scanout(struct drm_plane *plane,
  132. struct drm_framebuffer *fb)
  133. {
  134. struct mdp4_plane *mdp4_plane = to_mdp4_plane(plane);
  135. struct mdp4_kms *mdp4_kms = get_kms(plane);
  136. struct msm_kms *kms = &mdp4_kms->base.base;
  137. enum mdp4_pipe pipe = mdp4_plane->pipe;
  138. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SRC_STRIDE_A(pipe),
  139. MDP4_PIPE_SRC_STRIDE_A_P0(fb->pitches[0]) |
  140. MDP4_PIPE_SRC_STRIDE_A_P1(fb->pitches[1]));
  141. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SRC_STRIDE_B(pipe),
  142. MDP4_PIPE_SRC_STRIDE_B_P2(fb->pitches[2]) |
  143. MDP4_PIPE_SRC_STRIDE_B_P3(fb->pitches[3]));
  144. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SRCP0_BASE(pipe),
  145. msm_framebuffer_iova(fb, kms->aspace, 0));
  146. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SRCP1_BASE(pipe),
  147. msm_framebuffer_iova(fb, kms->aspace, 1));
  148. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SRCP2_BASE(pipe),
  149. msm_framebuffer_iova(fb, kms->aspace, 2));
  150. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SRCP3_BASE(pipe),
  151. msm_framebuffer_iova(fb, kms->aspace, 3));
  152. plane->fb = fb;
  153. }
  154. static void mdp4_write_csc_config(struct mdp4_kms *mdp4_kms,
  155. enum mdp4_pipe pipe, struct csc_cfg *csc)
  156. {
  157. int i;
  158. for (i = 0; i < ARRAY_SIZE(csc->matrix); i++) {
  159. mdp4_write(mdp4_kms, REG_MDP4_PIPE_CSC_MV(pipe, i),
  160. csc->matrix[i]);
  161. }
  162. for (i = 0; i < ARRAY_SIZE(csc->post_bias) ; i++) {
  163. mdp4_write(mdp4_kms, REG_MDP4_PIPE_CSC_PRE_BV(pipe, i),
  164. csc->pre_bias[i]);
  165. mdp4_write(mdp4_kms, REG_MDP4_PIPE_CSC_POST_BV(pipe, i),
  166. csc->post_bias[i]);
  167. }
  168. for (i = 0; i < ARRAY_SIZE(csc->post_clamp) ; i++) {
  169. mdp4_write(mdp4_kms, REG_MDP4_PIPE_CSC_PRE_LV(pipe, i),
  170. csc->pre_clamp[i]);
  171. mdp4_write(mdp4_kms, REG_MDP4_PIPE_CSC_POST_LV(pipe, i),
  172. csc->post_clamp[i]);
  173. }
  174. }
  175. #define MDP4_VG_PHASE_STEP_DEFAULT 0x20000000
  176. static int mdp4_plane_mode_set(struct drm_plane *plane,
  177. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  178. int crtc_x, int crtc_y,
  179. unsigned int crtc_w, unsigned int crtc_h,
  180. uint32_t src_x, uint32_t src_y,
  181. uint32_t src_w, uint32_t src_h)
  182. {
  183. struct drm_device *dev = plane->dev;
  184. struct mdp4_plane *mdp4_plane = to_mdp4_plane(plane);
  185. struct mdp4_kms *mdp4_kms = get_kms(plane);
  186. enum mdp4_pipe pipe = mdp4_plane->pipe;
  187. const struct mdp_format *format;
  188. uint32_t op_mode = 0;
  189. uint32_t phasex_step = MDP4_VG_PHASE_STEP_DEFAULT;
  190. uint32_t phasey_step = MDP4_VG_PHASE_STEP_DEFAULT;
  191. enum mdp4_frame_format frame_type;
  192. if (!(crtc && fb)) {
  193. DBG("%s: disabled!", mdp4_plane->name);
  194. return 0;
  195. }
  196. frame_type = mdp4_get_frame_format(fb);
  197. /* src values are in Q16 fixed point, convert to integer: */
  198. src_x = src_x >> 16;
  199. src_y = src_y >> 16;
  200. src_w = src_w >> 16;
  201. src_h = src_h >> 16;
  202. DBG("%s: FB[%u] %u,%u,%u,%u -> CRTC[%u] %d,%d,%u,%u", mdp4_plane->name,
  203. fb->base.id, src_x, src_y, src_w, src_h,
  204. crtc->base.id, crtc_x, crtc_y, crtc_w, crtc_h);
  205. format = to_mdp_format(msm_framebuffer_format(fb));
  206. if (src_w > (crtc_w * DOWN_SCALE_MAX)) {
  207. dev_err(dev->dev, "Width down scaling exceeds limits!\n");
  208. return -ERANGE;
  209. }
  210. if (src_h > (crtc_h * DOWN_SCALE_MAX)) {
  211. dev_err(dev->dev, "Height down scaling exceeds limits!\n");
  212. return -ERANGE;
  213. }
  214. if (crtc_w > (src_w * UP_SCALE_MAX)) {
  215. dev_err(dev->dev, "Width up scaling exceeds limits!\n");
  216. return -ERANGE;
  217. }
  218. if (crtc_h > (src_h * UP_SCALE_MAX)) {
  219. dev_err(dev->dev, "Height up scaling exceeds limits!\n");
  220. return -ERANGE;
  221. }
  222. if (src_w != crtc_w) {
  223. uint32_t sel_unit = SCALE_FIR;
  224. op_mode |= MDP4_PIPE_OP_MODE_SCALEX_EN;
  225. if (MDP_FORMAT_IS_YUV(format)) {
  226. if (crtc_w > src_w)
  227. sel_unit = SCALE_PIXEL_RPT;
  228. else if (crtc_w <= (src_w / 4))
  229. sel_unit = SCALE_MN_PHASE;
  230. op_mode |= MDP4_PIPE_OP_MODE_SCALEX_UNIT_SEL(sel_unit);
  231. phasex_step = mult_frac(MDP4_VG_PHASE_STEP_DEFAULT,
  232. src_w, crtc_w);
  233. }
  234. }
  235. if (src_h != crtc_h) {
  236. uint32_t sel_unit = SCALE_FIR;
  237. op_mode |= MDP4_PIPE_OP_MODE_SCALEY_EN;
  238. if (MDP_FORMAT_IS_YUV(format)) {
  239. if (crtc_h > src_h)
  240. sel_unit = SCALE_PIXEL_RPT;
  241. else if (crtc_h <= (src_h / 4))
  242. sel_unit = SCALE_MN_PHASE;
  243. op_mode |= MDP4_PIPE_OP_MODE_SCALEY_UNIT_SEL(sel_unit);
  244. phasey_step = mult_frac(MDP4_VG_PHASE_STEP_DEFAULT,
  245. src_h, crtc_h);
  246. }
  247. }
  248. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SRC_SIZE(pipe),
  249. MDP4_PIPE_SRC_SIZE_WIDTH(src_w) |
  250. MDP4_PIPE_SRC_SIZE_HEIGHT(src_h));
  251. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SRC_XY(pipe),
  252. MDP4_PIPE_SRC_XY_X(src_x) |
  253. MDP4_PIPE_SRC_XY_Y(src_y));
  254. mdp4_write(mdp4_kms, REG_MDP4_PIPE_DST_SIZE(pipe),
  255. MDP4_PIPE_DST_SIZE_WIDTH(crtc_w) |
  256. MDP4_PIPE_DST_SIZE_HEIGHT(crtc_h));
  257. mdp4_write(mdp4_kms, REG_MDP4_PIPE_DST_XY(pipe),
  258. MDP4_PIPE_DST_XY_X(crtc_x) |
  259. MDP4_PIPE_DST_XY_Y(crtc_y));
  260. mdp4_plane_set_scanout(plane, fb);
  261. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SRC_FORMAT(pipe),
  262. MDP4_PIPE_SRC_FORMAT_A_BPC(format->bpc_a) |
  263. MDP4_PIPE_SRC_FORMAT_R_BPC(format->bpc_r) |
  264. MDP4_PIPE_SRC_FORMAT_G_BPC(format->bpc_g) |
  265. MDP4_PIPE_SRC_FORMAT_B_BPC(format->bpc_b) |
  266. COND(format->alpha_enable, MDP4_PIPE_SRC_FORMAT_ALPHA_ENABLE) |
  267. MDP4_PIPE_SRC_FORMAT_CPP(format->cpp - 1) |
  268. MDP4_PIPE_SRC_FORMAT_UNPACK_COUNT(format->unpack_count - 1) |
  269. MDP4_PIPE_SRC_FORMAT_FETCH_PLANES(format->fetch_type) |
  270. MDP4_PIPE_SRC_FORMAT_CHROMA_SAMP(format->chroma_sample) |
  271. MDP4_PIPE_SRC_FORMAT_FRAME_FORMAT(frame_type) |
  272. COND(format->unpack_tight, MDP4_PIPE_SRC_FORMAT_UNPACK_TIGHT));
  273. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SRC_UNPACK(pipe),
  274. MDP4_PIPE_SRC_UNPACK_ELEM0(format->unpack[0]) |
  275. MDP4_PIPE_SRC_UNPACK_ELEM1(format->unpack[1]) |
  276. MDP4_PIPE_SRC_UNPACK_ELEM2(format->unpack[2]) |
  277. MDP4_PIPE_SRC_UNPACK_ELEM3(format->unpack[3]));
  278. if (MDP_FORMAT_IS_YUV(format)) {
  279. struct csc_cfg *csc = mdp_get_default_csc_cfg(CSC_YUV2RGB);
  280. op_mode |= MDP4_PIPE_OP_MODE_SRC_YCBCR;
  281. op_mode |= MDP4_PIPE_OP_MODE_CSC_EN;
  282. mdp4_write_csc_config(mdp4_kms, pipe, csc);
  283. }
  284. mdp4_write(mdp4_kms, REG_MDP4_PIPE_OP_MODE(pipe), op_mode);
  285. mdp4_write(mdp4_kms, REG_MDP4_PIPE_PHASEX_STEP(pipe), phasex_step);
  286. mdp4_write(mdp4_kms, REG_MDP4_PIPE_PHASEY_STEP(pipe), phasey_step);
  287. if (frame_type != FRAME_LINEAR)
  288. mdp4_write(mdp4_kms, REG_MDP4_PIPE_SSTILE_FRAME_SIZE(pipe),
  289. MDP4_PIPE_SSTILE_FRAME_SIZE_WIDTH(src_w) |
  290. MDP4_PIPE_SSTILE_FRAME_SIZE_HEIGHT(src_h));
  291. return 0;
  292. }
  293. static const char *pipe_names[] = {
  294. "VG1", "VG2",
  295. "RGB1", "RGB2", "RGB3",
  296. "VG3", "VG4",
  297. };
  298. enum mdp4_pipe mdp4_plane_pipe(struct drm_plane *plane)
  299. {
  300. struct mdp4_plane *mdp4_plane = to_mdp4_plane(plane);
  301. return mdp4_plane->pipe;
  302. }
  303. /* initialize plane */
  304. struct drm_plane *mdp4_plane_init(struct drm_device *dev,
  305. enum mdp4_pipe pipe_id, bool private_plane)
  306. {
  307. struct drm_plane *plane = NULL;
  308. struct mdp4_plane *mdp4_plane;
  309. int ret;
  310. enum drm_plane_type type;
  311. mdp4_plane = kzalloc(sizeof(*mdp4_plane), GFP_KERNEL);
  312. if (!mdp4_plane) {
  313. ret = -ENOMEM;
  314. goto fail;
  315. }
  316. plane = &mdp4_plane->base;
  317. mdp4_plane->pipe = pipe_id;
  318. mdp4_plane->name = pipe_names[pipe_id];
  319. mdp4_plane->caps = mdp4_pipe_caps(pipe_id);
  320. mdp4_plane->nformats = mdp_get_formats(mdp4_plane->formats,
  321. ARRAY_SIZE(mdp4_plane->formats),
  322. !pipe_supports_yuv(mdp4_plane->caps));
  323. type = private_plane ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  324. ret = drm_universal_plane_init(dev, plane, 0xff, &mdp4_plane_funcs,
  325. mdp4_plane->formats, mdp4_plane->nformats,
  326. NULL, type, NULL);
  327. if (ret)
  328. goto fail;
  329. drm_plane_helper_add(plane, &mdp4_plane_helper_funcs);
  330. mdp4_plane_install_properties(plane, &plane->base);
  331. return plane;
  332. fail:
  333. if (plane)
  334. mdp4_plane_destroy(plane);
  335. return ERR_PTR(ret);
  336. }