mdp5_plane.c 28 KB

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