mdp5_plane.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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. const struct drm_plane_state *new_state)
  203. {
  204. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  205. struct mdp5_kms *mdp5_kms = get_kms(plane);
  206. struct drm_framebuffer *fb = new_state->fb;
  207. if (!new_state->fb)
  208. return 0;
  209. DBG("%s: prepare: FB[%u]", mdp5_plane->name, fb->base.id);
  210. return msm_framebuffer_prepare(fb, mdp5_kms->id);
  211. }
  212. static void mdp5_plane_cleanup_fb(struct drm_plane *plane,
  213. const struct drm_plane_state *old_state)
  214. {
  215. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  216. struct mdp5_kms *mdp5_kms = get_kms(plane);
  217. struct drm_framebuffer *fb = old_state->fb;
  218. if (!fb)
  219. return;
  220. DBG("%s: cleanup: FB[%u]", mdp5_plane->name, fb->base.id);
  221. msm_framebuffer_cleanup(fb, mdp5_kms->id);
  222. }
  223. static int mdp5_plane_atomic_check(struct drm_plane *plane,
  224. struct drm_plane_state *state)
  225. {
  226. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  227. struct drm_plane_state *old_state = plane->state;
  228. const struct mdp_format *format;
  229. bool vflip, hflip;
  230. DBG("%s: check (%d -> %d)", mdp5_plane->name,
  231. plane_enabled(old_state), plane_enabled(state));
  232. if (plane_enabled(state)) {
  233. format = to_mdp_format(msm_framebuffer_format(state->fb));
  234. if (MDP_FORMAT_IS_YUV(format) &&
  235. !pipe_supports_yuv(mdp5_plane->caps)) {
  236. dev_err(plane->dev->dev,
  237. "Pipe doesn't support YUV\n");
  238. return -EINVAL;
  239. }
  240. if (!(mdp5_plane->caps & MDP_PIPE_CAP_SCALE) &&
  241. (((state->src_w >> 16) != state->crtc_w) ||
  242. ((state->src_h >> 16) != state->crtc_h))) {
  243. dev_err(plane->dev->dev,
  244. "Pipe doesn't support scaling (%dx%d -> %dx%d)\n",
  245. state->src_w >> 16, state->src_h >> 16,
  246. state->crtc_w, state->crtc_h);
  247. return -EINVAL;
  248. }
  249. hflip = !!(state->rotation & BIT(DRM_REFLECT_X));
  250. vflip = !!(state->rotation & BIT(DRM_REFLECT_Y));
  251. if ((vflip && !(mdp5_plane->caps & MDP_PIPE_CAP_VFLIP)) ||
  252. (hflip && !(mdp5_plane->caps & MDP_PIPE_CAP_HFLIP))) {
  253. dev_err(plane->dev->dev,
  254. "Pipe doesn't support flip\n");
  255. return -EINVAL;
  256. }
  257. }
  258. if (plane_enabled(state) && plane_enabled(old_state)) {
  259. /* we cannot change SMP block configuration during scanout: */
  260. bool full_modeset = false;
  261. if (state->fb->pixel_format != old_state->fb->pixel_format) {
  262. DBG("%s: pixel_format change!", mdp5_plane->name);
  263. full_modeset = true;
  264. }
  265. if (state->src_w != old_state->src_w) {
  266. DBG("%s: src_w change!", mdp5_plane->name);
  267. full_modeset = true;
  268. }
  269. if (to_mdp5_plane_state(old_state)->pending) {
  270. DBG("%s: still pending!", mdp5_plane->name);
  271. full_modeset = true;
  272. }
  273. if (full_modeset) {
  274. struct drm_crtc_state *crtc_state =
  275. drm_atomic_get_crtc_state(state->state, state->crtc);
  276. crtc_state->mode_changed = true;
  277. to_mdp5_plane_state(state)->mode_changed = true;
  278. }
  279. } else {
  280. to_mdp5_plane_state(state)->mode_changed = true;
  281. }
  282. return 0;
  283. }
  284. static void mdp5_plane_atomic_update(struct drm_plane *plane,
  285. struct drm_plane_state *old_state)
  286. {
  287. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  288. struct drm_plane_state *state = plane->state;
  289. DBG("%s: update", mdp5_plane->name);
  290. if (!plane_enabled(state)) {
  291. to_mdp5_plane_state(state)->pending = true;
  292. } else if (to_mdp5_plane_state(state)->mode_changed) {
  293. int ret;
  294. to_mdp5_plane_state(state)->pending = true;
  295. ret = mdp5_plane_mode_set(plane,
  296. state->crtc, state->fb,
  297. state->crtc_x, state->crtc_y,
  298. state->crtc_w, state->crtc_h,
  299. state->src_x, state->src_y,
  300. state->src_w, state->src_h);
  301. /* atomic_check should have ensured that this doesn't fail */
  302. WARN_ON(ret < 0);
  303. } else {
  304. unsigned long flags;
  305. spin_lock_irqsave(&mdp5_plane->pipe_lock, flags);
  306. set_scanout_locked(plane, state->fb);
  307. spin_unlock_irqrestore(&mdp5_plane->pipe_lock, flags);
  308. }
  309. }
  310. static const struct drm_plane_helper_funcs mdp5_plane_helper_funcs = {
  311. .prepare_fb = mdp5_plane_prepare_fb,
  312. .cleanup_fb = mdp5_plane_cleanup_fb,
  313. .atomic_check = mdp5_plane_atomic_check,
  314. .atomic_update = mdp5_plane_atomic_update,
  315. };
  316. static void set_scanout_locked(struct drm_plane *plane,
  317. struct drm_framebuffer *fb)
  318. {
  319. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  320. struct mdp5_kms *mdp5_kms = get_kms(plane);
  321. enum mdp5_pipe pipe = mdp5_plane->pipe;
  322. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_A(pipe),
  323. MDP5_PIPE_SRC_STRIDE_A_P0(fb->pitches[0]) |
  324. MDP5_PIPE_SRC_STRIDE_A_P1(fb->pitches[1]));
  325. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_B(pipe),
  326. MDP5_PIPE_SRC_STRIDE_B_P2(fb->pitches[2]) |
  327. MDP5_PIPE_SRC_STRIDE_B_P3(fb->pitches[3]));
  328. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC0_ADDR(pipe),
  329. msm_framebuffer_iova(fb, mdp5_kms->id, 0));
  330. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC1_ADDR(pipe),
  331. msm_framebuffer_iova(fb, mdp5_kms->id, 1));
  332. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC2_ADDR(pipe),
  333. msm_framebuffer_iova(fb, mdp5_kms->id, 2));
  334. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC3_ADDR(pipe),
  335. msm_framebuffer_iova(fb, mdp5_kms->id, 3));
  336. plane->fb = fb;
  337. }
  338. /* Note: mdp5_plane->pipe_lock must be locked */
  339. static void csc_disable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe)
  340. {
  341. uint32_t value = mdp5_read(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe)) &
  342. ~MDP5_PIPE_OP_MODE_CSC_1_EN;
  343. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), value);
  344. }
  345. /* Note: mdp5_plane->pipe_lock must be locked */
  346. static void csc_enable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe,
  347. struct csc_cfg *csc)
  348. {
  349. uint32_t i, mode = 0; /* RGB, no CSC */
  350. uint32_t *matrix;
  351. if (unlikely(!csc))
  352. return;
  353. if ((csc->type == CSC_YUV2RGB) || (CSC_YUV2YUV == csc->type))
  354. mode |= MDP5_PIPE_OP_MODE_CSC_SRC_DATA_FORMAT(DATA_FORMAT_YUV);
  355. if ((csc->type == CSC_RGB2YUV) || (CSC_YUV2YUV == csc->type))
  356. mode |= MDP5_PIPE_OP_MODE_CSC_DST_DATA_FORMAT(DATA_FORMAT_YUV);
  357. mode |= MDP5_PIPE_OP_MODE_CSC_1_EN;
  358. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), mode);
  359. matrix = csc->matrix;
  360. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_0(pipe),
  361. MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_11(matrix[0]) |
  362. MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_12(matrix[1]));
  363. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_1(pipe),
  364. MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_13(matrix[2]) |
  365. MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_21(matrix[3]));
  366. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_2(pipe),
  367. MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_22(matrix[4]) |
  368. MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_23(matrix[5]));
  369. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_3(pipe),
  370. MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_31(matrix[6]) |
  371. MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_32(matrix[7]));
  372. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_4(pipe),
  373. MDP5_PIPE_CSC_1_MATRIX_COEFF_4_COEFF_33(matrix[8]));
  374. for (i = 0; i < ARRAY_SIZE(csc->pre_bias); i++) {
  375. uint32_t *pre_clamp = csc->pre_clamp;
  376. uint32_t *post_clamp = csc->post_clamp;
  377. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_CLAMP(pipe, i),
  378. MDP5_PIPE_CSC_1_PRE_CLAMP_REG_HIGH(pre_clamp[2*i+1]) |
  379. MDP5_PIPE_CSC_1_PRE_CLAMP_REG_LOW(pre_clamp[2*i]));
  380. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_CLAMP(pipe, i),
  381. MDP5_PIPE_CSC_1_POST_CLAMP_REG_HIGH(post_clamp[2*i+1]) |
  382. MDP5_PIPE_CSC_1_POST_CLAMP_REG_LOW(post_clamp[2*i]));
  383. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_BIAS(pipe, i),
  384. MDP5_PIPE_CSC_1_PRE_BIAS_REG_VALUE(csc->pre_bias[i]));
  385. mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_BIAS(pipe, i),
  386. MDP5_PIPE_CSC_1_POST_BIAS_REG_VALUE(csc->post_bias[i]));
  387. }
  388. }
  389. #define PHASE_STEP_SHIFT 21
  390. #define DOWN_SCALE_RATIO_MAX 32 /* 2^(26-21) */
  391. static int calc_phase_step(uint32_t src, uint32_t dst, uint32_t *out_phase)
  392. {
  393. uint32_t unit;
  394. if (src == 0 || dst == 0)
  395. return -EINVAL;
  396. /*
  397. * PHASE_STEP_X/Y is coded on 26 bits (25:0),
  398. * where 2^21 represents the unity "1" in fixed-point hardware design.
  399. * This leaves 5 bits for the integer part (downscale case):
  400. * -> maximum downscale ratio = 0b1_1111 = 31
  401. */
  402. if (src > (dst * DOWN_SCALE_RATIO_MAX))
  403. return -EOVERFLOW;
  404. unit = 1 << PHASE_STEP_SHIFT;
  405. *out_phase = mult_frac(unit, src, dst);
  406. return 0;
  407. }
  408. static int calc_scalex_steps(struct drm_plane *plane,
  409. uint32_t pixel_format, uint32_t src, uint32_t dest,
  410. uint32_t phasex_steps[COMP_MAX])
  411. {
  412. struct mdp5_kms *mdp5_kms = get_kms(plane);
  413. struct device *dev = mdp5_kms->dev->dev;
  414. uint32_t phasex_step;
  415. unsigned int hsub;
  416. int ret;
  417. ret = calc_phase_step(src, dest, &phasex_step);
  418. if (ret) {
  419. dev_err(dev, "X scaling (%d->%d) failed: %d\n", src, dest, ret);
  420. return ret;
  421. }
  422. hsub = drm_format_horz_chroma_subsampling(pixel_format);
  423. phasex_steps[COMP_0] = phasex_step;
  424. phasex_steps[COMP_3] = phasex_step;
  425. phasex_steps[COMP_1_2] = phasex_step / hsub;
  426. return 0;
  427. }
  428. static int calc_scaley_steps(struct drm_plane *plane,
  429. uint32_t pixel_format, uint32_t src, uint32_t dest,
  430. uint32_t phasey_steps[COMP_MAX])
  431. {
  432. struct mdp5_kms *mdp5_kms = get_kms(plane);
  433. struct device *dev = mdp5_kms->dev->dev;
  434. uint32_t phasey_step;
  435. unsigned int vsub;
  436. int ret;
  437. ret = calc_phase_step(src, dest, &phasey_step);
  438. if (ret) {
  439. dev_err(dev, "Y scaling (%d->%d) failed: %d\n", src, dest, ret);
  440. return ret;
  441. }
  442. vsub = drm_format_vert_chroma_subsampling(pixel_format);
  443. phasey_steps[COMP_0] = phasey_step;
  444. phasey_steps[COMP_3] = phasey_step;
  445. phasey_steps[COMP_1_2] = phasey_step / vsub;
  446. return 0;
  447. }
  448. static uint32_t get_scale_config(const struct mdp_format *format,
  449. uint32_t src, uint32_t dst, bool horz)
  450. {
  451. bool scaling = format->is_yuv ? true : (src != dst);
  452. uint32_t sub, pix_fmt = format->base.pixel_format;
  453. uint32_t ya_filter, uv_filter;
  454. bool yuv = format->is_yuv;
  455. if (!scaling)
  456. return 0;
  457. if (yuv) {
  458. sub = horz ? drm_format_horz_chroma_subsampling(pix_fmt) :
  459. drm_format_vert_chroma_subsampling(pix_fmt);
  460. uv_filter = ((src / sub) <= dst) ?
  461. SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
  462. }
  463. ya_filter = (src <= dst) ? SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
  464. if (horz)
  465. return MDP5_PIPE_SCALE_CONFIG_SCALEX_EN |
  466. MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_0(ya_filter) |
  467. MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_3(ya_filter) |
  468. COND(yuv, MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_1_2(uv_filter));
  469. else
  470. return MDP5_PIPE_SCALE_CONFIG_SCALEY_EN |
  471. MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_0(ya_filter) |
  472. MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_3(ya_filter) |
  473. COND(yuv, MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_1_2(uv_filter));
  474. }
  475. static void calc_pixel_ext(const struct mdp_format *format,
  476. uint32_t src, uint32_t dst, uint32_t phase_step[2],
  477. int pix_ext_edge1[COMP_MAX], int pix_ext_edge2[COMP_MAX],
  478. bool horz)
  479. {
  480. bool scaling = format->is_yuv ? true : (src != dst);
  481. int i;
  482. /*
  483. * Note:
  484. * We assume here that:
  485. * 1. PCMN filter is used for downscale
  486. * 2. bilinear filter is used for upscale
  487. * 3. we are in a single pipe configuration
  488. */
  489. for (i = 0; i < COMP_MAX; i++) {
  490. pix_ext_edge1[i] = 0;
  491. pix_ext_edge2[i] = scaling ? 1 : 0;
  492. }
  493. }
  494. static void mdp5_write_pixel_ext(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe,
  495. const struct mdp_format *format,
  496. uint32_t src_w, int pe_left[COMP_MAX], int pe_right[COMP_MAX],
  497. uint32_t src_h, int pe_top[COMP_MAX], int pe_bottom[COMP_MAX])
  498. {
  499. uint32_t pix_fmt = format->base.pixel_format;
  500. uint32_t lr, tb, req;
  501. int i;
  502. for (i = 0; i < COMP_MAX; i++) {
  503. uint32_t roi_w = src_w;
  504. uint32_t roi_h = src_h;
  505. if (format->is_yuv && i == COMP_1_2) {
  506. roi_w /= drm_format_horz_chroma_subsampling(pix_fmt);
  507. roi_h /= drm_format_vert_chroma_subsampling(pix_fmt);
  508. }
  509. lr = (pe_left[i] >= 0) ?
  510. MDP5_PIPE_SW_PIX_EXT_LR_LEFT_RPT(pe_left[i]) :
  511. MDP5_PIPE_SW_PIX_EXT_LR_LEFT_OVF(pe_left[i]);
  512. lr |= (pe_right[i] >= 0) ?
  513. MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_RPT(pe_right[i]) :
  514. MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_OVF(pe_right[i]);
  515. tb = (pe_top[i] >= 0) ?
  516. MDP5_PIPE_SW_PIX_EXT_TB_TOP_RPT(pe_top[i]) :
  517. MDP5_PIPE_SW_PIX_EXT_TB_TOP_OVF(pe_top[i]);
  518. tb |= (pe_bottom[i] >= 0) ?
  519. MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_RPT(pe_bottom[i]) :
  520. MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_OVF(pe_bottom[i]);
  521. req = MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_LEFT_RIGHT(roi_w +
  522. pe_left[i] + pe_right[i]);
  523. req |= MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_TOP_BOTTOM(roi_h +
  524. pe_top[i] + pe_bottom[i]);
  525. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_LR(pipe, i), lr);
  526. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_TB(pipe, i), tb);
  527. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS(pipe, i), req);
  528. DBG("comp-%d (L/R): rpt=%d/%d, ovf=%d/%d, req=%d", i,
  529. FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_LEFT_RPT),
  530. FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_RPT),
  531. FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_LEFT_OVF),
  532. FIELD(lr, MDP5_PIPE_SW_PIX_EXT_LR_RIGHT_OVF),
  533. FIELD(req, MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_LEFT_RIGHT));
  534. DBG("comp-%d (T/B): rpt=%d/%d, ovf=%d/%d, req=%d", i,
  535. FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_TOP_RPT),
  536. FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_RPT),
  537. FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_TOP_OVF),
  538. FIELD(tb, MDP5_PIPE_SW_PIX_EXT_TB_BOTTOM_OVF),
  539. FIELD(req, MDP5_PIPE_SW_PIX_EXT_REQ_PIXELS_TOP_BOTTOM));
  540. }
  541. }
  542. static int mdp5_plane_mode_set(struct drm_plane *plane,
  543. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  544. int crtc_x, int crtc_y,
  545. unsigned int crtc_w, unsigned int crtc_h,
  546. uint32_t src_x, uint32_t src_y,
  547. uint32_t src_w, uint32_t src_h)
  548. {
  549. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  550. struct drm_plane_state *pstate = plane->state;
  551. struct mdp5_kms *mdp5_kms = get_kms(plane);
  552. enum mdp5_pipe pipe = mdp5_plane->pipe;
  553. const struct mdp_format *format;
  554. uint32_t nplanes, config = 0;
  555. uint32_t phasex_step[COMP_MAX] = {0,}, phasey_step[COMP_MAX] = {0,};
  556. bool pe = mdp5_plane->caps & MDP_PIPE_CAP_SW_PIX_EXT;
  557. int pe_left[COMP_MAX], pe_right[COMP_MAX];
  558. int pe_top[COMP_MAX], pe_bottom[COMP_MAX];
  559. uint32_t hdecm = 0, vdecm = 0;
  560. uint32_t pix_format;
  561. bool vflip, hflip;
  562. unsigned long flags;
  563. int ret;
  564. nplanes = drm_format_num_planes(fb->pixel_format);
  565. /* bad formats should already be rejected: */
  566. if (WARN_ON(nplanes > pipe2nclients(pipe)))
  567. return -EINVAL;
  568. format = to_mdp_format(msm_framebuffer_format(fb));
  569. pix_format = format->base.pixel_format;
  570. /* src values are in Q16 fixed point, convert to integer: */
  571. src_x = src_x >> 16;
  572. src_y = src_y >> 16;
  573. src_w = src_w >> 16;
  574. src_h = src_h >> 16;
  575. DBG("%s: FB[%u] %u,%u,%u,%u -> CRTC[%u] %d,%d,%u,%u", mdp5_plane->name,
  576. fb->base.id, src_x, src_y, src_w, src_h,
  577. crtc->base.id, crtc_x, crtc_y, crtc_w, crtc_h);
  578. /* Request some memory from the SMP: */
  579. if (mdp5_kms->smp) {
  580. ret = mdp5_smp_request(mdp5_kms->smp,
  581. mdp5_plane->pipe, format, src_w, false);
  582. if (ret)
  583. return ret;
  584. }
  585. /*
  586. * Currently we update the hw for allocations/requests immediately,
  587. * but once atomic modeset/pageflip is in place, the allocation
  588. * would move into atomic->check_plane_state(), while updating the
  589. * hw would remain here:
  590. */
  591. if (mdp5_kms->smp)
  592. mdp5_smp_configure(mdp5_kms->smp, pipe);
  593. ret = calc_scalex_steps(plane, pix_format, src_w, crtc_w, phasex_step);
  594. if (ret)
  595. return ret;
  596. ret = calc_scaley_steps(plane, pix_format, src_h, crtc_h, phasey_step);
  597. if (ret)
  598. return ret;
  599. if (mdp5_plane->caps & MDP_PIPE_CAP_SW_PIX_EXT) {
  600. calc_pixel_ext(format, src_w, crtc_w, phasex_step,
  601. pe_left, pe_right, true);
  602. calc_pixel_ext(format, src_h, crtc_h, phasey_step,
  603. pe_top, pe_bottom, false);
  604. }
  605. /* TODO calc hdecm, vdecm */
  606. /* SCALE is used to both scale and up-sample chroma components */
  607. config |= get_scale_config(format, src_w, crtc_w, true);
  608. config |= get_scale_config(format, src_h, crtc_h, false);
  609. DBG("scale config = %x", config);
  610. hflip = !!(pstate->rotation & BIT(DRM_REFLECT_X));
  611. vflip = !!(pstate->rotation & BIT(DRM_REFLECT_Y));
  612. spin_lock_irqsave(&mdp5_plane->pipe_lock, flags);
  613. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_IMG_SIZE(pipe),
  614. MDP5_PIPE_SRC_IMG_SIZE_WIDTH(fb->width) |
  615. MDP5_PIPE_SRC_IMG_SIZE_HEIGHT(fb->height));
  616. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_SIZE(pipe),
  617. MDP5_PIPE_SRC_SIZE_WIDTH(src_w) |
  618. MDP5_PIPE_SRC_SIZE_HEIGHT(src_h));
  619. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_XY(pipe),
  620. MDP5_PIPE_SRC_XY_X(src_x) |
  621. MDP5_PIPE_SRC_XY_Y(src_y));
  622. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_SIZE(pipe),
  623. MDP5_PIPE_OUT_SIZE_WIDTH(crtc_w) |
  624. MDP5_PIPE_OUT_SIZE_HEIGHT(crtc_h));
  625. mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_XY(pipe),
  626. MDP5_PIPE_OUT_XY_X(crtc_x) |
  627. MDP5_PIPE_OUT_XY_Y(crtc_y));
  628. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_FORMAT(pipe),
  629. MDP5_PIPE_SRC_FORMAT_A_BPC(format->bpc_a) |
  630. MDP5_PIPE_SRC_FORMAT_R_BPC(format->bpc_r) |
  631. MDP5_PIPE_SRC_FORMAT_G_BPC(format->bpc_g) |
  632. MDP5_PIPE_SRC_FORMAT_B_BPC(format->bpc_b) |
  633. COND(format->alpha_enable, MDP5_PIPE_SRC_FORMAT_ALPHA_ENABLE) |
  634. MDP5_PIPE_SRC_FORMAT_CPP(format->cpp - 1) |
  635. MDP5_PIPE_SRC_FORMAT_UNPACK_COUNT(format->unpack_count - 1) |
  636. COND(format->unpack_tight, MDP5_PIPE_SRC_FORMAT_UNPACK_TIGHT) |
  637. MDP5_PIPE_SRC_FORMAT_FETCH_TYPE(format->fetch_type) |
  638. MDP5_PIPE_SRC_FORMAT_CHROMA_SAMP(format->chroma_sample));
  639. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_UNPACK(pipe),
  640. MDP5_PIPE_SRC_UNPACK_ELEM0(format->unpack[0]) |
  641. MDP5_PIPE_SRC_UNPACK_ELEM1(format->unpack[1]) |
  642. MDP5_PIPE_SRC_UNPACK_ELEM2(format->unpack[2]) |
  643. MDP5_PIPE_SRC_UNPACK_ELEM3(format->unpack[3]));
  644. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_OP_MODE(pipe),
  645. (hflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_LR : 0) |
  646. (vflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_UD : 0) |
  647. COND(pe, MDP5_PIPE_SRC_OP_MODE_SW_PIX_EXT_OVERRIDE) |
  648. MDP5_PIPE_SRC_OP_MODE_BWC(BWC_LOSSLESS));
  649. /* not using secure mode: */
  650. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_ADDR_SW_STATUS(pipe), 0);
  651. if (mdp5_plane->caps & MDP_PIPE_CAP_SW_PIX_EXT)
  652. mdp5_write_pixel_ext(mdp5_kms, pipe, format,
  653. src_w, pe_left, pe_right,
  654. src_h, pe_top, pe_bottom);
  655. if (mdp5_plane->caps & MDP_PIPE_CAP_SCALE) {
  656. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_X(pipe),
  657. phasex_step[COMP_0]);
  658. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_Y(pipe),
  659. phasey_step[COMP_0]);
  660. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_X(pipe),
  661. phasex_step[COMP_1_2]);
  662. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_Y(pipe),
  663. phasey_step[COMP_1_2]);
  664. mdp5_write(mdp5_kms, REG_MDP5_PIPE_DECIMATION(pipe),
  665. MDP5_PIPE_DECIMATION_VERT(vdecm) |
  666. MDP5_PIPE_DECIMATION_HORZ(hdecm));
  667. mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CONFIG(pipe), config);
  668. }
  669. if (mdp5_plane->caps & MDP_PIPE_CAP_CSC) {
  670. if (MDP_FORMAT_IS_YUV(format))
  671. csc_enable(mdp5_kms, pipe,
  672. mdp_get_default_csc_cfg(CSC_YUV2RGB));
  673. else
  674. csc_disable(mdp5_kms, pipe);
  675. }
  676. set_scanout_locked(plane, fb);
  677. spin_unlock_irqrestore(&mdp5_plane->pipe_lock, flags);
  678. return ret;
  679. }
  680. void mdp5_plane_complete_flip(struct drm_plane *plane)
  681. {
  682. struct mdp5_kms *mdp5_kms = get_kms(plane);
  683. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  684. enum mdp5_pipe pipe = mdp5_plane->pipe;
  685. DBG("%s: complete flip", mdp5_plane->name);
  686. if (mdp5_kms->smp)
  687. mdp5_smp_commit(mdp5_kms->smp, pipe);
  688. to_mdp5_plane_state(plane->state)->pending = false;
  689. }
  690. enum mdp5_pipe mdp5_plane_pipe(struct drm_plane *plane)
  691. {
  692. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  693. return mdp5_plane->pipe;
  694. }
  695. uint32_t mdp5_plane_get_flush(struct drm_plane *plane)
  696. {
  697. struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
  698. return mdp5_plane->flush_mask;
  699. }
  700. /* called after vsync in thread context */
  701. void mdp5_plane_complete_commit(struct drm_plane *plane,
  702. struct drm_plane_state *state)
  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. if (!plane_enabled(plane->state) && mdp5_kms->smp) {
  708. DBG("%s: free SMP", mdp5_plane->name);
  709. mdp5_smp_release(mdp5_kms->smp, pipe);
  710. }
  711. }
  712. /* initialize plane */
  713. struct drm_plane *mdp5_plane_init(struct drm_device *dev,
  714. enum mdp5_pipe pipe, bool private_plane, uint32_t reg_offset,
  715. uint32_t caps)
  716. {
  717. struct drm_plane *plane = NULL;
  718. struct mdp5_plane *mdp5_plane;
  719. int ret;
  720. enum drm_plane_type type;
  721. mdp5_plane = kzalloc(sizeof(*mdp5_plane), GFP_KERNEL);
  722. if (!mdp5_plane) {
  723. ret = -ENOMEM;
  724. goto fail;
  725. }
  726. plane = &mdp5_plane->base;
  727. mdp5_plane->pipe = pipe;
  728. mdp5_plane->name = pipe2name(pipe);
  729. mdp5_plane->caps = caps;
  730. mdp5_plane->nformats = mdp_get_formats(mdp5_plane->formats,
  731. ARRAY_SIZE(mdp5_plane->formats),
  732. !pipe_supports_yuv(mdp5_plane->caps));
  733. mdp5_plane->flush_mask = mdp_ctl_flush_mask_pipe(pipe);
  734. mdp5_plane->reg_offset = reg_offset;
  735. spin_lock_init(&mdp5_plane->pipe_lock);
  736. type = private_plane ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  737. ret = drm_universal_plane_init(dev, plane, 0xff, &mdp5_plane_funcs,
  738. mdp5_plane->formats, mdp5_plane->nformats,
  739. type);
  740. if (ret)
  741. goto fail;
  742. drm_plane_helper_add(plane, &mdp5_plane_helper_funcs);
  743. mdp5_plane_install_properties(plane, &plane->base);
  744. return plane;
  745. fail:
  746. if (plane)
  747. mdp5_plane_destroy(plane);
  748. return ERR_PTR(ret);
  749. }