mdp4_plane.c 11 KB

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