mdp5_plane.c 30 KB

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