mdp5_plane.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. * Copyright (C) 2014-2015 The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2013 Red Hat
  4. * Author: Rob Clark <robdclark@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published by
  8. * the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "mdp5_kms.h"
  19. struct mdp5_plane {
  20. struct drm_plane base;
  21. const char *name;
  22. enum mdp5_pipe pipe;
  23. spinlock_t pipe_lock; /* protect REG_MDP5_PIPE_* registers */
  24. uint32_t reg_offset;
  25. uint32_t caps;
  26. uint32_t flush_mask; /* used to commit pipe registers */
  27. uint32_t nformats;
  28. uint32_t formats[32];
  29. };
  30. #define to_mdp5_plane(x) container_of(x, struct mdp5_plane, base)
  31. static int mdp5_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 void set_scanout_locked(struct drm_plane *plane,
  38. struct drm_framebuffer *fb);
  39. static struct mdp5_kms *get_kms(struct drm_plane *plane)
  40. {
  41. struct msm_drm_private *priv = plane->dev->dev_private;
  42. return to_mdp5_kms(to_mdp_kms(priv->kms));
  43. }
  44. static bool plane_enabled(struct drm_plane_state *state)
  45. {
  46. return state->fb && state->crtc;
  47. }
  48. static void mdp5_plane_destroy(struct drm_plane *plane)
  49. {
  50. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  51. drm_plane_helper_disable(plane);
  52. drm_plane_cleanup(plane);
  53. kfree(mdp5_plane);
  54. }
  55. static void mdp5_plane_install_rotation_property(struct drm_device *dev,
  56. struct drm_plane *plane)
  57. {
  58. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  59. if (!(mdp5_plane->caps & MDP_PIPE_CAP_HFLIP) &&
  60. !(mdp5_plane->caps & MDP_PIPE_CAP_VFLIP))
  61. return;
  62. if (!dev->mode_config.rotation_property)
  63. dev->mode_config.rotation_property =
  64. drm_mode_create_rotation_property(dev,
  65. BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y));
  66. if (dev->mode_config.rotation_property)
  67. drm_object_attach_property(&plane->base,
  68. dev->mode_config.rotation_property,
  69. 0);
  70. }
  71. /* helper to install properties which are common to planes and crtcs */
  72. static void mdp5_plane_install_properties(struct drm_plane *plane,
  73. struct drm_mode_object *obj)
  74. {
  75. struct drm_device *dev = plane->dev;
  76. struct msm_drm_private *dev_priv = dev->dev_private;
  77. struct drm_property *prop;
  78. #define INSTALL_PROPERTY(name, NAME, init_val, fnc, ...) do { \
  79. prop = dev_priv->plane_property[PLANE_PROP_##NAME]; \
  80. if (!prop) { \
  81. prop = drm_property_##fnc(dev, 0, #name, \
  82. ##__VA_ARGS__); \
  83. if (!prop) { \
  84. dev_warn(dev->dev, \
  85. "Create property %s failed\n", \
  86. #name); \
  87. return; \
  88. } \
  89. dev_priv->plane_property[PLANE_PROP_##NAME] = prop; \
  90. } \
  91. drm_object_attach_property(&plane->base, prop, init_val); \
  92. } while (0)
  93. #define INSTALL_RANGE_PROPERTY(name, NAME, min, max, init_val) \
  94. INSTALL_PROPERTY(name, NAME, init_val, \
  95. create_range, min, max)
  96. #define INSTALL_ENUM_PROPERTY(name, NAME, init_val) \
  97. INSTALL_PROPERTY(name, NAME, init_val, \
  98. create_enum, name##_prop_enum_list, \
  99. ARRAY_SIZE(name##_prop_enum_list))
  100. INSTALL_RANGE_PROPERTY(zpos, ZPOS, 1, 255, 1);
  101. mdp5_plane_install_rotation_property(dev, plane);
  102. #undef INSTALL_RANGE_PROPERTY
  103. #undef INSTALL_ENUM_PROPERTY
  104. #undef INSTALL_PROPERTY
  105. }
  106. static int mdp5_plane_atomic_set_property(struct drm_plane *plane,
  107. struct drm_plane_state *state, struct drm_property *property,
  108. uint64_t val)
  109. {
  110. struct drm_device *dev = plane->dev;
  111. struct mdp5_plane_state *pstate;
  112. struct msm_drm_private *dev_priv = dev->dev_private;
  113. int ret = 0;
  114. pstate = to_mdp5_plane_state(state);
  115. #define SET_PROPERTY(name, NAME, type) do { \
  116. if (dev_priv->plane_property[PLANE_PROP_##NAME] == property) { \
  117. pstate->name = (type)val; \
  118. DBG("Set property %s %d", #name, (type)val); \
  119. goto done; \
  120. } \
  121. } while (0)
  122. SET_PROPERTY(zpos, ZPOS, uint8_t);
  123. dev_err(dev->dev, "Invalid property\n");
  124. ret = -EINVAL;
  125. done:
  126. return ret;
  127. #undef SET_PROPERTY
  128. }
  129. static int mdp5_plane_atomic_get_property(struct drm_plane *plane,
  130. const struct drm_plane_state *state,
  131. struct drm_property *property, uint64_t *val)
  132. {
  133. struct drm_device *dev = plane->dev;
  134. struct mdp5_plane_state *pstate;
  135. struct msm_drm_private *dev_priv = dev->dev_private;
  136. int ret = 0;
  137. pstate = to_mdp5_plane_state(state);
  138. #define GET_PROPERTY(name, NAME, type) do { \
  139. if (dev_priv->plane_property[PLANE_PROP_##NAME] == property) { \
  140. *val = pstate->name; \
  141. DBG("Get property %s %lld", #name, *val); \
  142. goto done; \
  143. } \
  144. } while (0)
  145. GET_PROPERTY(zpos, ZPOS, uint8_t);
  146. dev_err(dev->dev, "Invalid property\n");
  147. ret = -EINVAL;
  148. done:
  149. return ret;
  150. #undef SET_PROPERTY
  151. }
  152. static void mdp5_plane_reset(struct drm_plane *plane)
  153. {
  154. struct mdp5_plane_state *mdp5_state;
  155. if (plane->state && plane->state->fb)
  156. drm_framebuffer_unreference(plane->state->fb);
  157. kfree(to_mdp5_plane_state(plane->state));
  158. mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
  159. /* assign default blend parameters */
  160. mdp5_state->alpha = 255;
  161. mdp5_state->premultiplied = 0;
  162. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  163. mdp5_state->zpos = STAGE_BASE;
  164. else
  165. mdp5_state->zpos = STAGE0 + drm_plane_index(plane);
  166. mdp5_state->base.plane = plane;
  167. plane->state = &mdp5_state->base;
  168. }
  169. static struct drm_plane_state *
  170. mdp5_plane_duplicate_state(struct drm_plane *plane)
  171. {
  172. struct mdp5_plane_state *mdp5_state;
  173. if (WARN_ON(!plane->state))
  174. return NULL;
  175. mdp5_state = kmemdup(to_mdp5_plane_state(plane->state),
  176. sizeof(*mdp5_state), GFP_KERNEL);
  177. if (mdp5_state && mdp5_state->base.fb)
  178. drm_framebuffer_reference(mdp5_state->base.fb);
  179. mdp5_state->mode_changed = false;
  180. mdp5_state->pending = false;
  181. return &mdp5_state->base;
  182. }
  183. static void mdp5_plane_destroy_state(struct drm_plane *plane,
  184. struct drm_plane_state *state)
  185. {
  186. if (state->fb)
  187. drm_framebuffer_unreference(state->fb);
  188. kfree(to_mdp5_plane_state(state));
  189. }
  190. static const struct drm_plane_funcs mdp5_plane_funcs = {
  191. .update_plane = drm_atomic_helper_update_plane,
  192. .disable_plane = drm_atomic_helper_disable_plane,
  193. .destroy = mdp5_plane_destroy,
  194. .set_property = drm_atomic_helper_plane_set_property,
  195. .atomic_set_property = mdp5_plane_atomic_set_property,
  196. .atomic_get_property = mdp5_plane_atomic_get_property,
  197. .reset = mdp5_plane_reset,
  198. .atomic_duplicate_state = mdp5_plane_duplicate_state,
  199. .atomic_destroy_state = mdp5_plane_destroy_state,
  200. };
  201. static int mdp5_plane_prepare_fb(struct drm_plane *plane,
  202. struct drm_framebuffer *fb,
  203. const struct drm_plane_state *new_state)
  204. {
  205. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  206. struct mdp5_kms *mdp5_kms = get_kms(plane);
  207. DBG("%s: prepare: FB[%u]", mdp5_plane->name, fb->base.id);
  208. return msm_framebuffer_prepare(fb, mdp5_kms->id);
  209. }
  210. static void mdp5_plane_cleanup_fb(struct drm_plane *plane,
  211. struct drm_framebuffer *fb,
  212. const struct drm_plane_state *old_state)
  213. {
  214. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  215. struct mdp5_kms *mdp5_kms = get_kms(plane);
  216. DBG("%s: cleanup: FB[%u]", mdp5_plane->name, fb->base.id);
  217. msm_framebuffer_cleanup(fb, mdp5_kms->id);
  218. }
  219. static int mdp5_plane_atomic_check(struct drm_plane *plane,
  220. struct drm_plane_state *state)
  221. {
  222. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  223. struct drm_plane_state *old_state = plane->state;
  224. const struct mdp_format *format;
  225. bool vflip, hflip;
  226. DBG("%s: check (%d -> %d)", mdp5_plane->name,
  227. plane_enabled(old_state), plane_enabled(state));
  228. if (plane_enabled(state)) {
  229. format = to_mdp_format(msm_framebuffer_format(state->fb));
  230. if (MDP_FORMAT_IS_YUV(format) &&
  231. !pipe_supports_yuv(mdp5_plane->caps)) {
  232. dev_err(plane->dev->dev,
  233. "Pipe doesn't support YUV\n");
  234. return -EINVAL;
  235. }
  236. if (!(mdp5_plane->caps & MDP_PIPE_CAP_SCALE) &&
  237. (((state->src_w >> 16) != state->crtc_w) ||
  238. ((state->src_h >> 16) != state->crtc_h))) {
  239. dev_err(plane->dev->dev,
  240. "Pipe doesn't support scaling (%dx%d -> %dx%d)\n",
  241. state->src_w >> 16, state->src_h >> 16,
  242. state->crtc_w, state->crtc_h);
  243. return -EINVAL;
  244. }
  245. hflip = !!(state->rotation & BIT(DRM_REFLECT_X));
  246. vflip = !!(state->rotation & BIT(DRM_REFLECT_Y));
  247. if ((vflip && !(mdp5_plane->caps & MDP_PIPE_CAP_VFLIP)) ||
  248. (hflip && !(mdp5_plane->caps & MDP_PIPE_CAP_HFLIP))) {
  249. dev_err(plane->dev->dev,
  250. "Pipe doesn't support flip\n");
  251. return -EINVAL;
  252. }
  253. }
  254. if (plane_enabled(state) && plane_enabled(old_state)) {
  255. /* we cannot change SMP block configuration during scanout: */
  256. bool full_modeset = false;
  257. if (state->fb->pixel_format != old_state->fb->pixel_format) {
  258. DBG("%s: pixel_format change!", mdp5_plane->name);
  259. full_modeset = true;
  260. }
  261. if (state->src_w != old_state->src_w) {
  262. DBG("%s: src_w change!", mdp5_plane->name);
  263. full_modeset = true;
  264. }
  265. if (to_mdp5_plane_state(old_state)->pending) {
  266. DBG("%s: still pending!", mdp5_plane->name);
  267. full_modeset = true;
  268. }
  269. if (full_modeset) {
  270. struct drm_crtc_state *crtc_state =
  271. drm_atomic_get_crtc_state(state->state, state->crtc);
  272. crtc_state->mode_changed = true;
  273. to_mdp5_plane_state(state)->mode_changed = true;
  274. }
  275. } else {
  276. to_mdp5_plane_state(state)->mode_changed = true;
  277. }
  278. return 0;
  279. }
  280. static void mdp5_plane_atomic_update(struct drm_plane *plane,
  281. struct drm_plane_state *old_state)
  282. {
  283. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  284. struct drm_plane_state *state = plane->state;
  285. DBG("%s: update", mdp5_plane->name);
  286. if (!plane_enabled(state)) {
  287. to_mdp5_plane_state(state)->pending = true;
  288. } else if (to_mdp5_plane_state(state)->mode_changed) {
  289. int ret;
  290. to_mdp5_plane_state(state)->pending = true;
  291. ret = mdp5_plane_mode_set(plane,
  292. state->crtc, state->fb,
  293. state->crtc_x, state->crtc_y,
  294. state->crtc_w, state->crtc_h,
  295. state->src_x, state->src_y,
  296. state->src_w, state->src_h);
  297. /* atomic_check should have ensured that this doesn't fail */
  298. WARN_ON(ret < 0);
  299. } else {
  300. unsigned long flags;
  301. spin_lock_irqsave(&mdp5_plane->pipe_lock, flags);
  302. set_scanout_locked(plane, state->fb);
  303. spin_unlock_irqrestore(&mdp5_plane->pipe_lock, flags);
  304. }
  305. }
  306. static const struct drm_plane_helper_funcs mdp5_plane_helper_funcs = {
  307. .prepare_fb = mdp5_plane_prepare_fb,
  308. .cleanup_fb = mdp5_plane_cleanup_fb,
  309. .atomic_check = mdp5_plane_atomic_check,
  310. .atomic_update = mdp5_plane_atomic_update,
  311. };
  312. static void set_scanout_locked(struct drm_plane *plane,
  313. struct drm_framebuffer *fb)
  314. {
  315. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  316. struct mdp5_kms *mdp5_kms = get_kms(plane);
  317. enum mdp5_pipe pipe = mdp5_plane->pipe;
  318. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_A(pipe),
  319. MDP5_PIPE_SRC_STRIDE_A_P0(fb->pitches[0]) |
  320. MDP5_PIPE_SRC_STRIDE_A_P1(fb->pitches[1]));
  321. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_B(pipe),
  322. MDP5_PIPE_SRC_STRIDE_B_P2(fb->pitches[2]) |
  323. MDP5_PIPE_SRC_STRIDE_B_P3(fb->pitches[3]));
  324. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC0_ADDR(pipe),
  325. msm_framebuffer_iova(fb, mdp5_kms->id, 0));
  326. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC1_ADDR(pipe),
  327. msm_framebuffer_iova(fb, mdp5_kms->id, 1));
  328. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC2_ADDR(pipe),
  329. msm_framebuffer_iova(fb, mdp5_kms->id, 2));
  330. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC3_ADDR(pipe),
  331. msm_framebuffer_iova(fb, mdp5_kms->id, 3));
  332. plane->fb = fb;
  333. }
  334. /* Note: mdp5_plane->pipe_lock must be locked */
  335. static void csc_disable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe)
  336. {
  337. uint32_t value = mdp5_read(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe)) &
  338. ~MDP5_PIPE_OP_MODE_CSC_1_EN;
  339. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), value);
  340. }
  341. /* Note: mdp5_plane->pipe_lock must be locked */
  342. static void csc_enable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe,
  343. struct csc_cfg *csc)
  344. {
  345. uint32_t i, mode = 0; /* RGB, no CSC */
  346. uint32_t *matrix;
  347. if (unlikely(!csc))
  348. return;
  349. if ((csc->type == CSC_YUV2RGB) || (CSC_YUV2YUV == csc->type))
  350. mode |= MDP5_PIPE_OP_MODE_CSC_SRC_DATA_FORMAT(DATA_FORMAT_YUV);
  351. if ((csc->type == CSC_RGB2YUV) || (CSC_YUV2YUV == csc->type))
  352. mode |= MDP5_PIPE_OP_MODE_CSC_DST_DATA_FORMAT(DATA_FORMAT_YUV);
  353. mode |= MDP5_PIPE_OP_MODE_CSC_1_EN;
  354. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), mode);
  355. matrix = csc->matrix;
  356. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_0(pipe),
  357. MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_11(matrix[0]) |
  358. MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_12(matrix[1]));
  359. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_1(pipe),
  360. MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_13(matrix[2]) |
  361. MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_21(matrix[3]));
  362. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_2(pipe),
  363. MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_22(matrix[4]) |
  364. MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_23(matrix[5]));
  365. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_3(pipe),
  366. MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_31(matrix[6]) |
  367. MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_32(matrix[7]));
  368. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_4(pipe),
  369. MDP5_PIPE_CSC_1_MATRIX_COEFF_4_COEFF_33(matrix[8]));
  370. for (i = 0; i < ARRAY_SIZE(csc->pre_bias); i++) {
  371. uint32_t *pre_clamp = csc->pre_clamp;
  372. uint32_t *post_clamp = csc->post_clamp;
  373. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_CLAMP(pipe, i),
  374. MDP5_PIPE_CSC_1_PRE_CLAMP_REG_HIGH(pre_clamp[2*i+1]) |
  375. MDP5_PIPE_CSC_1_PRE_CLAMP_REG_LOW(pre_clamp[2*i]));
  376. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_CLAMP(pipe, i),
  377. MDP5_PIPE_CSC_1_POST_CLAMP_REG_HIGH(post_clamp[2*i+1]) |
  378. MDP5_PIPE_CSC_1_POST_CLAMP_REG_LOW(post_clamp[2*i]));
  379. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_BIAS(pipe, i),
  380. MDP5_PIPE_CSC_1_PRE_BIAS_REG_VALUE(csc->pre_bias[i]));
  381. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_BIAS(pipe, i),
  382. MDP5_PIPE_CSC_1_POST_BIAS_REG_VALUE(csc->post_bias[i]));
  383. }
  384. }
  385. #define PHASE_STEP_SHIFT 21
  386. #define DOWN_SCALE_RATIO_MAX 32 /* 2^(26-21) */
  387. static int calc_phase_step(uint32_t src, uint32_t dst, uint32_t *out_phase)
  388. {
  389. uint32_t unit;
  390. if (src == 0 || dst == 0)
  391. return -EINVAL;
  392. /*
  393. * PHASE_STEP_X/Y is coded on 26 bits (25:0),
  394. * where 2^21 represents the unity "1" in fixed-point hardware design.
  395. * This leaves 5 bits for the integer part (downscale case):
  396. * -> maximum downscale ratio = 0b1_1111 = 31
  397. */
  398. if (src > (dst * DOWN_SCALE_RATIO_MAX))
  399. return -EOVERFLOW;
  400. unit = 1 << PHASE_STEP_SHIFT;
  401. *out_phase = mult_frac(unit, src, dst);
  402. return 0;
  403. }
  404. static int calc_scalex_steps(struct drm_plane *plane,
  405. uint32_t pixel_format, uint32_t src, uint32_t dest,
  406. uint32_t phasex_steps[2])
  407. {
  408. struct mdp5_kms *mdp5_kms = get_kms(plane);
  409. struct device *dev = mdp5_kms->dev->dev;
  410. uint32_t phasex_step;
  411. unsigned int hsub;
  412. int ret;
  413. ret = calc_phase_step(src, dest, &phasex_step);
  414. if (ret) {
  415. dev_err(dev, "X scaling (%d->%d) failed: %d\n", src, dest, ret);
  416. return ret;
  417. }
  418. hsub = drm_format_horz_chroma_subsampling(pixel_format);
  419. phasex_steps[0] = phasex_step;
  420. phasex_steps[1] = phasex_step / hsub;
  421. return 0;
  422. }
  423. static int calc_scaley_steps(struct drm_plane *plane,
  424. uint32_t pixel_format, uint32_t src, uint32_t dest,
  425. uint32_t phasey_steps[2])
  426. {
  427. struct mdp5_kms *mdp5_kms = get_kms(plane);
  428. struct device *dev = mdp5_kms->dev->dev;
  429. uint32_t phasey_step;
  430. unsigned int vsub;
  431. int ret;
  432. ret = calc_phase_step(src, dest, &phasey_step);
  433. if (ret) {
  434. dev_err(dev, "Y scaling (%d->%d) failed: %d\n", src, dest, ret);
  435. return ret;
  436. }
  437. vsub = drm_format_vert_chroma_subsampling(pixel_format);
  438. phasey_steps[0] = phasey_step;
  439. phasey_steps[1] = phasey_step / vsub;
  440. return 0;
  441. }
  442. static uint32_t get_scale_config(enum mdp_chroma_samp_type chroma_sample,
  443. uint32_t src, uint32_t dest, bool hor)
  444. {
  445. uint32_t y_filter = (src <= dest) ? SCALE_FILTER_CA : SCALE_FILTER_PCMN;
  446. uint32_t y_a_filter = (src <= dest) ? SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
  447. uint32_t uv_filter = ((src / 2) <= dest) ? /* 2x upsample */
  448. SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
  449. uint32_t value = 0;
  450. if (chroma_sample == CHROMA_420 || chroma_sample == CHROMA_H2V1) {
  451. if (hor)
  452. value = MDP5_PIPE_SCALE_CONFIG_SCALEX_EN |
  453. MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_0(y_filter) |
  454. MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_3(y_a_filter) |
  455. MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_1_2(uv_filter);
  456. else
  457. value = MDP5_PIPE_SCALE_CONFIG_SCALEY_EN |
  458. MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_0(y_filter) |
  459. MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_3(y_a_filter) |
  460. MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_1_2(uv_filter);
  461. } else if (src != dest) {
  462. if (hor)
  463. value = MDP5_PIPE_SCALE_CONFIG_SCALEX_EN |
  464. MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_0(y_a_filter) |
  465. MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_3(y_a_filter);
  466. else
  467. value = MDP5_PIPE_SCALE_CONFIG_SCALEY_EN |
  468. MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_0(y_a_filter) |
  469. MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_3(y_a_filter);
  470. }
  471. return value;
  472. }
  473. static int mdp5_plane_mode_set(struct drm_plane *plane,
  474. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  475. int crtc_x, int crtc_y,
  476. unsigned int crtc_w, unsigned int crtc_h,
  477. uint32_t src_x, uint32_t src_y,
  478. uint32_t src_w, uint32_t src_h)
  479. {
  480. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  481. struct drm_plane_state *pstate = plane->state;
  482. struct mdp5_kms *mdp5_kms = get_kms(plane);
  483. enum mdp5_pipe pipe = mdp5_plane->pipe;
  484. const struct mdp_format *format;
  485. uint32_t nplanes, config = 0;
  486. /* below array -> index 0: comp 0/3 ; index 1: comp 1/2 */
  487. uint32_t phasex_step[2] = {0,}, phasey_step[2] = {0,};
  488. uint32_t hdecm = 0, vdecm = 0;
  489. uint32_t pix_format;
  490. bool vflip, hflip;
  491. unsigned long flags;
  492. int ret;
  493. nplanes = drm_format_num_planes(fb->pixel_format);
  494. /* bad formats should already be rejected: */
  495. if (WARN_ON(nplanes > pipe2nclients(pipe)))
  496. return -EINVAL;
  497. format = to_mdp_format(msm_framebuffer_format(fb));
  498. pix_format = format->base.pixel_format;
  499. /* src values are in Q16 fixed point, convert to integer: */
  500. src_x = src_x >> 16;
  501. src_y = src_y >> 16;
  502. src_w = src_w >> 16;
  503. src_h = src_h >> 16;
  504. DBG("%s: FB[%u] %u,%u,%u,%u -> CRTC[%u] %d,%d,%u,%u", mdp5_plane->name,
  505. fb->base.id, src_x, src_y, src_w, src_h,
  506. crtc->base.id, crtc_x, crtc_y, crtc_w, crtc_h);
  507. /* Request some memory from the SMP: */
  508. ret = mdp5_smp_request(mdp5_kms->smp,
  509. mdp5_plane->pipe, format, src_w, false);
  510. if (ret)
  511. return ret;
  512. /*
  513. * Currently we update the hw for allocations/requests immediately,
  514. * but once atomic modeset/pageflip is in place, the allocation
  515. * would move into atomic->check_plane_state(), while updating the
  516. * hw would remain here:
  517. */
  518. mdp5_smp_configure(mdp5_kms->smp, pipe);
  519. ret = calc_scalex_steps(plane, pix_format, src_w, crtc_w, phasex_step);
  520. if (ret)
  521. return ret;
  522. ret = calc_scaley_steps(plane, pix_format, src_h, crtc_h, phasey_step);
  523. if (ret)
  524. return ret;
  525. /* TODO calc hdecm, vdecm */
  526. /* SCALE is used to both scale and up-sample chroma components */
  527. config |= get_scale_config(format->chroma_sample, src_w, crtc_w, true);
  528. config |= get_scale_config(format->chroma_sample, src_h, crtc_h, false);
  529. DBG("scale config = %x", config);
  530. hflip = !!(pstate->rotation & BIT(DRM_REFLECT_X));
  531. vflip = !!(pstate->rotation & BIT(DRM_REFLECT_Y));
  532. spin_lock_irqsave(&mdp5_plane->pipe_lock, flags);
  533. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_IMG_SIZE(pipe),
  534. MDP5_PIPE_SRC_IMG_SIZE_WIDTH(fb->width) |
  535. MDP5_PIPE_SRC_IMG_SIZE_HEIGHT(fb->height));
  536. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_SIZE(pipe),
  537. MDP5_PIPE_SRC_SIZE_WIDTH(src_w) |
  538. MDP5_PIPE_SRC_SIZE_HEIGHT(src_h));
  539. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_XY(pipe),
  540. MDP5_PIPE_SRC_XY_X(src_x) |
  541. MDP5_PIPE_SRC_XY_Y(src_y));
  542. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_SIZE(pipe),
  543. MDP5_PIPE_OUT_SIZE_WIDTH(crtc_w) |
  544. MDP5_PIPE_OUT_SIZE_HEIGHT(crtc_h));
  545. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_XY(pipe),
  546. MDP5_PIPE_OUT_XY_X(crtc_x) |
  547. MDP5_PIPE_OUT_XY_Y(crtc_y));
  548. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_FORMAT(pipe),
  549. MDP5_PIPE_SRC_FORMAT_A_BPC(format->bpc_a) |
  550. MDP5_PIPE_SRC_FORMAT_R_BPC(format->bpc_r) |
  551. MDP5_PIPE_SRC_FORMAT_G_BPC(format->bpc_g) |
  552. MDP5_PIPE_SRC_FORMAT_B_BPC(format->bpc_b) |
  553. COND(format->alpha_enable, MDP5_PIPE_SRC_FORMAT_ALPHA_ENABLE) |
  554. MDP5_PIPE_SRC_FORMAT_CPP(format->cpp - 1) |
  555. MDP5_PIPE_SRC_FORMAT_UNPACK_COUNT(format->unpack_count - 1) |
  556. COND(format->unpack_tight, MDP5_PIPE_SRC_FORMAT_UNPACK_TIGHT) |
  557. MDP5_PIPE_SRC_FORMAT_FETCH_TYPE(format->fetch_type) |
  558. MDP5_PIPE_SRC_FORMAT_CHROMA_SAMP(format->chroma_sample));
  559. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_UNPACK(pipe),
  560. MDP5_PIPE_SRC_UNPACK_ELEM0(format->unpack[0]) |
  561. MDP5_PIPE_SRC_UNPACK_ELEM1(format->unpack[1]) |
  562. MDP5_PIPE_SRC_UNPACK_ELEM2(format->unpack[2]) |
  563. MDP5_PIPE_SRC_UNPACK_ELEM3(format->unpack[3]));
  564. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_OP_MODE(pipe),
  565. (hflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_LR : 0) |
  566. (vflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_UD : 0) |
  567. MDP5_PIPE_SRC_OP_MODE_BWC(BWC_LOSSLESS));
  568. /* not using secure mode: */
  569. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_ADDR_SW_STATUS(pipe), 0);
  570. if (mdp5_plane->caps & MDP_PIPE_CAP_SCALE) {
  571. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_X(pipe),
  572. phasex_step[0]);
  573. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_Y(pipe),
  574. phasey_step[0]);
  575. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_X(pipe),
  576. phasex_step[1]);
  577. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_Y(pipe),
  578. phasey_step[1]);
  579. mdp5_write(mdp5_kms, REG_MDP5_PIPE_DECIMATION(pipe),
  580. MDP5_PIPE_DECIMATION_VERT(vdecm) |
  581. MDP5_PIPE_DECIMATION_HORZ(hdecm));
  582. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CONFIG(pipe), config);
  583. }
  584. if (mdp5_plane->caps & MDP_PIPE_CAP_CSC) {
  585. if (MDP_FORMAT_IS_YUV(format))
  586. csc_enable(mdp5_kms, pipe,
  587. mdp_get_default_csc_cfg(CSC_YUV2RGB));
  588. else
  589. csc_disable(mdp5_kms, pipe);
  590. }
  591. set_scanout_locked(plane, fb);
  592. spin_unlock_irqrestore(&mdp5_plane->pipe_lock, flags);
  593. return ret;
  594. }
  595. void mdp5_plane_complete_flip(struct drm_plane *plane)
  596. {
  597. struct mdp5_kms *mdp5_kms = get_kms(plane);
  598. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  599. enum mdp5_pipe pipe = mdp5_plane->pipe;
  600. DBG("%s: complete flip", mdp5_plane->name);
  601. mdp5_smp_commit(mdp5_kms->smp, pipe);
  602. to_mdp5_plane_state(plane->state)->pending = false;
  603. }
  604. enum mdp5_pipe mdp5_plane_pipe(struct drm_plane *plane)
  605. {
  606. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  607. return mdp5_plane->pipe;
  608. }
  609. uint32_t mdp5_plane_get_flush(struct drm_plane *plane)
  610. {
  611. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  612. return mdp5_plane->flush_mask;
  613. }
  614. /* called after vsync in thread context */
  615. void mdp5_plane_complete_commit(struct drm_plane *plane,
  616. struct drm_plane_state *state)
  617. {
  618. struct mdp5_kms *mdp5_kms = get_kms(plane);
  619. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  620. enum mdp5_pipe pipe = mdp5_plane->pipe;
  621. if (!plane_enabled(plane->state)) {
  622. DBG("%s: free SMP", mdp5_plane->name);
  623. mdp5_smp_release(mdp5_kms->smp, pipe);
  624. }
  625. }
  626. /* initialize plane */
  627. struct drm_plane *mdp5_plane_init(struct drm_device *dev,
  628. enum mdp5_pipe pipe, bool private_plane, uint32_t reg_offset,
  629. uint32_t caps)
  630. {
  631. struct drm_plane *plane = NULL;
  632. struct mdp5_plane *mdp5_plane;
  633. int ret;
  634. enum drm_plane_type type;
  635. mdp5_plane = kzalloc(sizeof(*mdp5_plane), GFP_KERNEL);
  636. if (!mdp5_plane) {
  637. ret = -ENOMEM;
  638. goto fail;
  639. }
  640. plane = &mdp5_plane->base;
  641. mdp5_plane->pipe = pipe;
  642. mdp5_plane->name = pipe2name(pipe);
  643. mdp5_plane->caps = caps;
  644. mdp5_plane->nformats = mdp_get_formats(mdp5_plane->formats,
  645. ARRAY_SIZE(mdp5_plane->formats),
  646. !pipe_supports_yuv(mdp5_plane->caps));
  647. mdp5_plane->flush_mask = mdp_ctl_flush_mask_pipe(pipe);
  648. mdp5_plane->reg_offset = reg_offset;
  649. spin_lock_init(&mdp5_plane->pipe_lock);
  650. type = private_plane ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  651. ret = drm_universal_plane_init(dev, plane, 0xff, &mdp5_plane_funcs,
  652. mdp5_plane->formats, mdp5_plane->nformats,
  653. type);
  654. if (ret)
  655. goto fail;
  656. drm_plane_helper_add(plane, &mdp5_plane_helper_funcs);
  657. mdp5_plane_install_properties(plane, &plane->base);
  658. return plane;
  659. fail:
  660. if (plane)
  661. mdp5_plane_destroy(plane);
  662. return ERR_PTR(ret);
  663. }