mdp5_crtc.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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. #include <linux/sort.h>
  20. #include <drm/drm_mode.h>
  21. #include "drm_crtc.h"
  22. #include "drm_crtc_helper.h"
  23. #include "drm_flip_work.h"
  24. #define CURSOR_WIDTH 64
  25. #define CURSOR_HEIGHT 64
  26. struct mdp5_crtc {
  27. struct drm_crtc base;
  28. int id;
  29. bool enabled;
  30. /* layer mixer used for this CRTC (+ its lock): */
  31. #define GET_LM_ID(crtc_id) ((crtc_id == 3) ? 5 : crtc_id)
  32. int lm;
  33. spinlock_t lm_lock; /* protect REG_MDP5_LM_* registers */
  34. /* CTL used for this CRTC: */
  35. struct mdp5_ctl *ctl;
  36. /* if there is a pending flip, these will be non-null: */
  37. struct drm_pending_vblank_event *event;
  38. /* Bits have been flushed at the last commit,
  39. * used to decide if a vsync has happened since last commit.
  40. */
  41. u32 flushed_mask;
  42. #define PENDING_CURSOR 0x1
  43. #define PENDING_FLIP 0x2
  44. atomic_t pending;
  45. /* for unref'ing cursor bo's after scanout completes: */
  46. struct drm_flip_work unref_cursor_work;
  47. struct mdp_irq vblank;
  48. struct mdp_irq err;
  49. struct mdp_irq pp_done;
  50. struct completion pp_completion;
  51. bool cmd_mode;
  52. struct {
  53. /* protect REG_MDP5_LM_CURSOR* registers and cursor scanout_bo*/
  54. spinlock_t lock;
  55. /* current cursor being scanned out: */
  56. struct drm_gem_object *scanout_bo;
  57. uint32_t width, height;
  58. uint32_t x, y;
  59. } cursor;
  60. };
  61. #define to_mdp5_crtc(x) container_of(x, struct mdp5_crtc, base)
  62. static struct mdp5_kms *get_kms(struct drm_crtc *crtc)
  63. {
  64. struct msm_drm_private *priv = crtc->dev->dev_private;
  65. return to_mdp5_kms(to_mdp_kms(priv->kms));
  66. }
  67. static void request_pending(struct drm_crtc *crtc, uint32_t pending)
  68. {
  69. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  70. atomic_or(pending, &mdp5_crtc->pending);
  71. mdp_irq_register(&get_kms(crtc)->base, &mdp5_crtc->vblank);
  72. }
  73. static void request_pp_done_pending(struct drm_crtc *crtc)
  74. {
  75. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  76. reinit_completion(&mdp5_crtc->pp_completion);
  77. }
  78. static u32 crtc_flush(struct drm_crtc *crtc, u32 flush_mask)
  79. {
  80. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  81. DBG("%s: flush=%08x", crtc->name, flush_mask);
  82. return mdp5_ctl_commit(mdp5_crtc->ctl, flush_mask);
  83. }
  84. /*
  85. * flush updates, to make sure hw is updated to new scanout fb,
  86. * so that we can safely queue unref to current fb (ie. next
  87. * vblank we know hw is done w/ previous scanout_fb).
  88. */
  89. static u32 crtc_flush_all(struct drm_crtc *crtc)
  90. {
  91. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  92. struct drm_plane *plane;
  93. uint32_t flush_mask = 0;
  94. /* this should not happen: */
  95. if (WARN_ON(!mdp5_crtc->ctl))
  96. return 0;
  97. drm_atomic_crtc_for_each_plane(plane, crtc) {
  98. flush_mask |= mdp5_plane_get_flush(plane);
  99. }
  100. flush_mask |= mdp_ctl_flush_mask_lm(mdp5_crtc->lm);
  101. return crtc_flush(crtc, flush_mask);
  102. }
  103. /* if file!=NULL, this is preclose potential cancel-flip path */
  104. static void complete_flip(struct drm_crtc *crtc, struct drm_file *file)
  105. {
  106. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  107. struct drm_device *dev = crtc->dev;
  108. struct drm_pending_vblank_event *event;
  109. unsigned long flags;
  110. spin_lock_irqsave(&dev->event_lock, flags);
  111. event = mdp5_crtc->event;
  112. if (event) {
  113. /* if regular vblank case (!file) or if cancel-flip from
  114. * preclose on file that requested flip, then send the
  115. * event:
  116. */
  117. if (!file || (event->base.file_priv == file)) {
  118. mdp5_crtc->event = NULL;
  119. DBG("%s: send event: %p", crtc->name, event);
  120. drm_crtc_send_vblank_event(crtc, event);
  121. }
  122. }
  123. spin_unlock_irqrestore(&dev->event_lock, flags);
  124. if (mdp5_crtc->ctl && !crtc->state->enable) {
  125. /* set STAGE_UNUSED for all layers */
  126. mdp5_ctl_blend(mdp5_crtc->ctl, NULL, 0, 0);
  127. mdp5_crtc->ctl = NULL;
  128. }
  129. }
  130. static void unref_cursor_worker(struct drm_flip_work *work, void *val)
  131. {
  132. struct mdp5_crtc *mdp5_crtc =
  133. container_of(work, struct mdp5_crtc, unref_cursor_work);
  134. struct mdp5_kms *mdp5_kms = get_kms(&mdp5_crtc->base);
  135. msm_gem_put_iova(val, mdp5_kms->id);
  136. drm_gem_object_unreference_unlocked(val);
  137. }
  138. static void mdp5_crtc_destroy(struct drm_crtc *crtc)
  139. {
  140. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  141. drm_crtc_cleanup(crtc);
  142. drm_flip_work_cleanup(&mdp5_crtc->unref_cursor_work);
  143. kfree(mdp5_crtc);
  144. }
  145. static inline u32 mdp5_lm_use_fg_alpha_mask(enum mdp_mixer_stage_id stage)
  146. {
  147. switch (stage) {
  148. case STAGE0: return MDP5_LM_BLEND_COLOR_OUT_STAGE0_FG_ALPHA;
  149. case STAGE1: return MDP5_LM_BLEND_COLOR_OUT_STAGE1_FG_ALPHA;
  150. case STAGE2: return MDP5_LM_BLEND_COLOR_OUT_STAGE2_FG_ALPHA;
  151. case STAGE3: return MDP5_LM_BLEND_COLOR_OUT_STAGE3_FG_ALPHA;
  152. case STAGE4: return MDP5_LM_BLEND_COLOR_OUT_STAGE4_FG_ALPHA;
  153. case STAGE5: return MDP5_LM_BLEND_COLOR_OUT_STAGE5_FG_ALPHA;
  154. case STAGE6: return MDP5_LM_BLEND_COLOR_OUT_STAGE6_FG_ALPHA;
  155. default:
  156. return 0;
  157. }
  158. }
  159. /*
  160. * blend_setup() - blend all the planes of a CRTC
  161. *
  162. * If no base layer is available, border will be enabled as the base layer.
  163. * Otherwise all layers will be blended based on their stage calculated
  164. * in mdp5_crtc_atomic_check.
  165. */
  166. static void blend_setup(struct drm_crtc *crtc)
  167. {
  168. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  169. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  170. struct drm_plane *plane;
  171. const struct mdp5_cfg_hw *hw_cfg;
  172. struct mdp5_plane_state *pstate, *pstates[STAGE_MAX + 1] = {NULL};
  173. const struct mdp_format *format;
  174. uint32_t lm = mdp5_crtc->lm;
  175. uint32_t blend_op, fg_alpha, bg_alpha, ctl_blend_flags = 0;
  176. unsigned long flags;
  177. enum mdp5_pipe stage[STAGE_MAX + 1] = { SSPP_NONE };
  178. int i, plane_cnt = 0;
  179. bool bg_alpha_enabled = false;
  180. u32 mixer_op_mode = 0;
  181. #define blender(stage) ((stage) - STAGE0)
  182. hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
  183. spin_lock_irqsave(&mdp5_crtc->lm_lock, flags);
  184. /* ctl could be released already when we are shutting down: */
  185. if (!mdp5_crtc->ctl)
  186. goto out;
  187. /* Collect all plane information */
  188. drm_atomic_crtc_for_each_plane(plane, crtc) {
  189. pstate = to_mdp5_plane_state(plane->state);
  190. pstates[pstate->stage] = pstate;
  191. stage[pstate->stage] = mdp5_plane_pipe(plane);
  192. plane_cnt++;
  193. }
  194. if (!pstates[STAGE_BASE]) {
  195. ctl_blend_flags |= MDP5_CTL_BLEND_OP_FLAG_BORDER_OUT;
  196. DBG("Border Color is enabled");
  197. } else if (plane_cnt) {
  198. format = to_mdp_format(msm_framebuffer_format(pstates[STAGE_BASE]->base.fb));
  199. if (format->alpha_enable)
  200. bg_alpha_enabled = true;
  201. }
  202. /* The reset for blending */
  203. for (i = STAGE0; i <= STAGE_MAX; i++) {
  204. if (!pstates[i])
  205. continue;
  206. format = to_mdp_format(
  207. msm_framebuffer_format(pstates[i]->base.fb));
  208. plane = pstates[i]->base.plane;
  209. blend_op = MDP5_LM_BLEND_OP_MODE_FG_ALPHA(FG_CONST) |
  210. MDP5_LM_BLEND_OP_MODE_BG_ALPHA(BG_CONST);
  211. fg_alpha = pstates[i]->alpha;
  212. bg_alpha = 0xFF - pstates[i]->alpha;
  213. if (!format->alpha_enable && bg_alpha_enabled)
  214. mixer_op_mode = 0;
  215. else
  216. mixer_op_mode |= mdp5_lm_use_fg_alpha_mask(i);
  217. DBG("Stage %d fg_alpha %x bg_alpha %x", i, fg_alpha, bg_alpha);
  218. if (format->alpha_enable && pstates[i]->premultiplied) {
  219. blend_op = MDP5_LM_BLEND_OP_MODE_FG_ALPHA(FG_CONST) |
  220. MDP5_LM_BLEND_OP_MODE_BG_ALPHA(FG_PIXEL);
  221. if (fg_alpha != 0xff) {
  222. bg_alpha = fg_alpha;
  223. blend_op |=
  224. MDP5_LM_BLEND_OP_MODE_BG_MOD_ALPHA |
  225. MDP5_LM_BLEND_OP_MODE_BG_INV_MOD_ALPHA;
  226. } else {
  227. blend_op |= MDP5_LM_BLEND_OP_MODE_BG_INV_ALPHA;
  228. }
  229. } else if (format->alpha_enable) {
  230. blend_op = MDP5_LM_BLEND_OP_MODE_FG_ALPHA(FG_PIXEL) |
  231. MDP5_LM_BLEND_OP_MODE_BG_ALPHA(FG_PIXEL);
  232. if (fg_alpha != 0xff) {
  233. bg_alpha = fg_alpha;
  234. blend_op |=
  235. MDP5_LM_BLEND_OP_MODE_FG_MOD_ALPHA |
  236. MDP5_LM_BLEND_OP_MODE_FG_INV_MOD_ALPHA |
  237. MDP5_LM_BLEND_OP_MODE_BG_MOD_ALPHA |
  238. MDP5_LM_BLEND_OP_MODE_BG_INV_MOD_ALPHA;
  239. } else {
  240. blend_op |= MDP5_LM_BLEND_OP_MODE_BG_INV_ALPHA;
  241. }
  242. }
  243. mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_OP_MODE(lm,
  244. blender(i)), blend_op);
  245. mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_FG_ALPHA(lm,
  246. blender(i)), fg_alpha);
  247. mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_BG_ALPHA(lm,
  248. blender(i)), bg_alpha);
  249. }
  250. mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_COLOR_OUT(lm), mixer_op_mode);
  251. mdp5_ctl_blend(mdp5_crtc->ctl, stage, plane_cnt, ctl_blend_flags);
  252. out:
  253. spin_unlock_irqrestore(&mdp5_crtc->lm_lock, flags);
  254. }
  255. static void mdp5_crtc_mode_set_nofb(struct drm_crtc *crtc)
  256. {
  257. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  258. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  259. unsigned long flags;
  260. struct drm_display_mode *mode;
  261. if (WARN_ON(!crtc->state))
  262. return;
  263. mode = &crtc->state->adjusted_mode;
  264. DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  265. crtc->name, mode->base.id, mode->name,
  266. mode->vrefresh, mode->clock,
  267. mode->hdisplay, mode->hsync_start,
  268. mode->hsync_end, mode->htotal,
  269. mode->vdisplay, mode->vsync_start,
  270. mode->vsync_end, mode->vtotal,
  271. mode->type, mode->flags);
  272. spin_lock_irqsave(&mdp5_crtc->lm_lock, flags);
  273. mdp5_write(mdp5_kms, REG_MDP5_LM_OUT_SIZE(mdp5_crtc->lm),
  274. MDP5_LM_OUT_SIZE_WIDTH(mode->hdisplay) |
  275. MDP5_LM_OUT_SIZE_HEIGHT(mode->vdisplay));
  276. spin_unlock_irqrestore(&mdp5_crtc->lm_lock, flags);
  277. }
  278. static void mdp5_crtc_disable(struct drm_crtc *crtc)
  279. {
  280. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  281. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  282. DBG("%s", crtc->name);
  283. if (WARN_ON(!mdp5_crtc->enabled))
  284. return;
  285. if (mdp5_crtc->cmd_mode)
  286. mdp_irq_unregister(&mdp5_kms->base, &mdp5_crtc->pp_done);
  287. mdp_irq_unregister(&mdp5_kms->base, &mdp5_crtc->err);
  288. mdp5_disable(mdp5_kms);
  289. mdp5_crtc->enabled = false;
  290. }
  291. static void mdp5_crtc_enable(struct drm_crtc *crtc)
  292. {
  293. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  294. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  295. DBG("%s", crtc->name);
  296. if (WARN_ON(mdp5_crtc->enabled))
  297. return;
  298. mdp5_enable(mdp5_kms);
  299. mdp_irq_register(&mdp5_kms->base, &mdp5_crtc->err);
  300. if (mdp5_crtc->cmd_mode)
  301. mdp_irq_register(&mdp5_kms->base, &mdp5_crtc->pp_done);
  302. mdp5_crtc->enabled = true;
  303. }
  304. struct plane_state {
  305. struct drm_plane *plane;
  306. struct mdp5_plane_state *state;
  307. };
  308. static int pstate_cmp(const void *a, const void *b)
  309. {
  310. struct plane_state *pa = (struct plane_state *)a;
  311. struct plane_state *pb = (struct plane_state *)b;
  312. return pa->state->zpos - pb->state->zpos;
  313. }
  314. /* is there a helper for this? */
  315. static bool is_fullscreen(struct drm_crtc_state *cstate,
  316. struct drm_plane_state *pstate)
  317. {
  318. return (pstate->crtc_x <= 0) && (pstate->crtc_y <= 0) &&
  319. ((pstate->crtc_x + pstate->crtc_w) >= cstate->mode.hdisplay) &&
  320. ((pstate->crtc_y + pstate->crtc_h) >= cstate->mode.vdisplay);
  321. }
  322. static int mdp5_crtc_atomic_check(struct drm_crtc *crtc,
  323. struct drm_crtc_state *state)
  324. {
  325. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  326. struct drm_plane *plane;
  327. struct drm_device *dev = crtc->dev;
  328. struct plane_state pstates[STAGE_MAX + 1];
  329. const struct mdp5_cfg_hw *hw_cfg;
  330. const struct drm_plane_state *pstate;
  331. bool cursor_plane = false;
  332. int cnt = 0, base = 0, i;
  333. DBG("%s: check", crtc->name);
  334. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
  335. pstates[cnt].plane = plane;
  336. pstates[cnt].state = to_mdp5_plane_state(pstate);
  337. cnt++;
  338. if (plane->type == DRM_PLANE_TYPE_CURSOR)
  339. cursor_plane = true;
  340. }
  341. /* assign a stage based on sorted zpos property */
  342. sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
  343. /* if the bottom-most layer is not fullscreen, we need to use
  344. * it for solid-color:
  345. */
  346. if ((cnt > 0) && !is_fullscreen(state, &pstates[0].state->base))
  347. base++;
  348. /* trigger a warning if cursor isn't the highest zorder */
  349. WARN_ON(cursor_plane &&
  350. (pstates[cnt - 1].plane->type != DRM_PLANE_TYPE_CURSOR));
  351. /* verify that there are not too many planes attached to crtc
  352. * and that we don't have conflicting mixer stages:
  353. */
  354. hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
  355. if ((cnt + base) >= hw_cfg->lm.nb_stages) {
  356. dev_err(dev->dev, "too many planes! cnt=%d, base=%d\n", cnt, base);
  357. return -EINVAL;
  358. }
  359. for (i = 0; i < cnt; i++) {
  360. if (cursor_plane && (i == (cnt - 1)))
  361. pstates[i].state->stage = hw_cfg->lm.nb_stages;
  362. else
  363. pstates[i].state->stage = STAGE_BASE + i + base;
  364. DBG("%s: assign pipe %s on stage=%d", crtc->name,
  365. pstates[i].plane->name,
  366. pstates[i].state->stage);
  367. }
  368. return 0;
  369. }
  370. static void mdp5_crtc_atomic_begin(struct drm_crtc *crtc,
  371. struct drm_crtc_state *old_crtc_state)
  372. {
  373. DBG("%s: begin", crtc->name);
  374. }
  375. static void mdp5_crtc_atomic_flush(struct drm_crtc *crtc,
  376. struct drm_crtc_state *old_crtc_state)
  377. {
  378. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  379. struct drm_device *dev = crtc->dev;
  380. unsigned long flags;
  381. DBG("%s: event: %p", crtc->name, crtc->state->event);
  382. WARN_ON(mdp5_crtc->event);
  383. spin_lock_irqsave(&dev->event_lock, flags);
  384. mdp5_crtc->event = crtc->state->event;
  385. spin_unlock_irqrestore(&dev->event_lock, flags);
  386. /*
  387. * If no CTL has been allocated in mdp5_crtc_atomic_check(),
  388. * it means we are trying to flush a CRTC whose state is disabled:
  389. * nothing else needs to be done.
  390. */
  391. if (unlikely(!mdp5_crtc->ctl))
  392. return;
  393. blend_setup(crtc);
  394. /* PP_DONE irq is only used by command mode for now.
  395. * It is better to request pending before FLUSH and START trigger
  396. * to make sure no pp_done irq missed.
  397. * This is safe because no pp_done will happen before SW trigger
  398. * in command mode.
  399. */
  400. if (mdp5_crtc->cmd_mode)
  401. request_pp_done_pending(crtc);
  402. mdp5_crtc->flushed_mask = crtc_flush_all(crtc);
  403. request_pending(crtc, PENDING_FLIP);
  404. }
  405. static void get_roi(struct drm_crtc *crtc, uint32_t *roi_w, uint32_t *roi_h)
  406. {
  407. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  408. uint32_t xres = crtc->mode.hdisplay;
  409. uint32_t yres = crtc->mode.vdisplay;
  410. /*
  411. * Cursor Region Of Interest (ROI) is a plane read from cursor
  412. * buffer to render. The ROI region is determined by the visibility of
  413. * the cursor point. In the default Cursor image the cursor point will
  414. * be at the top left of the cursor image, unless it is specified
  415. * otherwise using hotspot feature.
  416. *
  417. * If the cursor point reaches the right (xres - x < cursor.width) or
  418. * bottom (yres - y < cursor.height) boundary of the screen, then ROI
  419. * width and ROI height need to be evaluated to crop the cursor image
  420. * accordingly.
  421. * (xres-x) will be new cursor width when x > (xres - cursor.width)
  422. * (yres-y) will be new cursor height when y > (yres - cursor.height)
  423. */
  424. *roi_w = min(mdp5_crtc->cursor.width, xres -
  425. mdp5_crtc->cursor.x);
  426. *roi_h = min(mdp5_crtc->cursor.height, yres -
  427. mdp5_crtc->cursor.y);
  428. }
  429. static int mdp5_crtc_cursor_set(struct drm_crtc *crtc,
  430. struct drm_file *file, uint32_t handle,
  431. uint32_t width, uint32_t height)
  432. {
  433. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  434. struct drm_device *dev = crtc->dev;
  435. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  436. struct drm_gem_object *cursor_bo, *old_bo = NULL;
  437. uint32_t blendcfg, stride;
  438. uint64_t cursor_addr;
  439. int ret, lm;
  440. enum mdp5_cursor_alpha cur_alpha = CURSOR_ALPHA_PER_PIXEL;
  441. uint32_t flush_mask = mdp_ctl_flush_mask_cursor(0);
  442. uint32_t roi_w, roi_h;
  443. bool cursor_enable = true;
  444. unsigned long flags;
  445. if ((width > CURSOR_WIDTH) || (height > CURSOR_HEIGHT)) {
  446. dev_err(dev->dev, "bad cursor size: %dx%d\n", width, height);
  447. return -EINVAL;
  448. }
  449. if (NULL == mdp5_crtc->ctl)
  450. return -EINVAL;
  451. if (!handle) {
  452. DBG("Cursor off");
  453. cursor_enable = false;
  454. goto set_cursor;
  455. }
  456. cursor_bo = drm_gem_object_lookup(file, handle);
  457. if (!cursor_bo)
  458. return -ENOENT;
  459. ret = msm_gem_get_iova(cursor_bo, mdp5_kms->id, &cursor_addr);
  460. if (ret)
  461. return -EINVAL;
  462. lm = mdp5_crtc->lm;
  463. stride = width * drm_format_plane_cpp(DRM_FORMAT_ARGB8888, 0);
  464. spin_lock_irqsave(&mdp5_crtc->cursor.lock, flags);
  465. old_bo = mdp5_crtc->cursor.scanout_bo;
  466. mdp5_crtc->cursor.scanout_bo = cursor_bo;
  467. mdp5_crtc->cursor.width = width;
  468. mdp5_crtc->cursor.height = height;
  469. get_roi(crtc, &roi_w, &roi_h);
  470. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_STRIDE(lm), stride);
  471. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_FORMAT(lm),
  472. MDP5_LM_CURSOR_FORMAT_FORMAT(CURSOR_FMT_ARGB8888));
  473. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_IMG_SIZE(lm),
  474. MDP5_LM_CURSOR_IMG_SIZE_SRC_H(height) |
  475. MDP5_LM_CURSOR_IMG_SIZE_SRC_W(width));
  476. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_SIZE(lm),
  477. MDP5_LM_CURSOR_SIZE_ROI_H(roi_h) |
  478. MDP5_LM_CURSOR_SIZE_ROI_W(roi_w));
  479. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_BASE_ADDR(lm), cursor_addr);
  480. blendcfg = MDP5_LM_CURSOR_BLEND_CONFIG_BLEND_EN;
  481. blendcfg |= MDP5_LM_CURSOR_BLEND_CONFIG_BLEND_ALPHA_SEL(cur_alpha);
  482. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_BLEND_CONFIG(lm), blendcfg);
  483. spin_unlock_irqrestore(&mdp5_crtc->cursor.lock, flags);
  484. set_cursor:
  485. ret = mdp5_ctl_set_cursor(mdp5_crtc->ctl, 0, cursor_enable);
  486. if (ret) {
  487. dev_err(dev->dev, "failed to %sable cursor: %d\n",
  488. cursor_enable ? "en" : "dis", ret);
  489. goto end;
  490. }
  491. crtc_flush(crtc, flush_mask);
  492. end:
  493. if (old_bo) {
  494. drm_flip_work_queue(&mdp5_crtc->unref_cursor_work, old_bo);
  495. /* enable vblank to complete cursor work: */
  496. request_pending(crtc, PENDING_CURSOR);
  497. }
  498. return ret;
  499. }
  500. static int mdp5_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  501. {
  502. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  503. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  504. uint32_t flush_mask = mdp_ctl_flush_mask_cursor(0);
  505. uint32_t roi_w;
  506. uint32_t roi_h;
  507. unsigned long flags;
  508. /* In case the CRTC is disabled, just drop the cursor update */
  509. if (unlikely(!crtc->state->enable))
  510. return 0;
  511. mdp5_crtc->cursor.x = x = max(x, 0);
  512. mdp5_crtc->cursor.y = y = max(y, 0);
  513. get_roi(crtc, &roi_w, &roi_h);
  514. spin_lock_irqsave(&mdp5_crtc->cursor.lock, flags);
  515. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_SIZE(mdp5_crtc->lm),
  516. MDP5_LM_CURSOR_SIZE_ROI_H(roi_h) |
  517. MDP5_LM_CURSOR_SIZE_ROI_W(roi_w));
  518. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_START_XY(mdp5_crtc->lm),
  519. MDP5_LM_CURSOR_START_XY_Y_START(y) |
  520. MDP5_LM_CURSOR_START_XY_X_START(x));
  521. spin_unlock_irqrestore(&mdp5_crtc->cursor.lock, flags);
  522. crtc_flush(crtc, flush_mask);
  523. return 0;
  524. }
  525. static const struct drm_crtc_funcs mdp5_crtc_funcs = {
  526. .set_config = drm_atomic_helper_set_config,
  527. .destroy = mdp5_crtc_destroy,
  528. .page_flip = drm_atomic_helper_page_flip,
  529. .set_property = drm_atomic_helper_crtc_set_property,
  530. .reset = drm_atomic_helper_crtc_reset,
  531. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  532. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  533. .cursor_set = mdp5_crtc_cursor_set,
  534. .cursor_move = mdp5_crtc_cursor_move,
  535. };
  536. static const struct drm_crtc_funcs mdp5_crtc_no_lm_cursor_funcs = {
  537. .set_config = drm_atomic_helper_set_config,
  538. .destroy = mdp5_crtc_destroy,
  539. .page_flip = drm_atomic_helper_page_flip,
  540. .set_property = drm_atomic_helper_crtc_set_property,
  541. .reset = drm_atomic_helper_crtc_reset,
  542. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  543. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  544. };
  545. static const struct drm_crtc_helper_funcs mdp5_crtc_helper_funcs = {
  546. .mode_set_nofb = mdp5_crtc_mode_set_nofb,
  547. .disable = mdp5_crtc_disable,
  548. .enable = mdp5_crtc_enable,
  549. .atomic_check = mdp5_crtc_atomic_check,
  550. .atomic_begin = mdp5_crtc_atomic_begin,
  551. .atomic_flush = mdp5_crtc_atomic_flush,
  552. };
  553. static void mdp5_crtc_vblank_irq(struct mdp_irq *irq, uint32_t irqstatus)
  554. {
  555. struct mdp5_crtc *mdp5_crtc = container_of(irq, struct mdp5_crtc, vblank);
  556. struct drm_crtc *crtc = &mdp5_crtc->base;
  557. struct msm_drm_private *priv = crtc->dev->dev_private;
  558. unsigned pending;
  559. mdp_irq_unregister(&get_kms(crtc)->base, &mdp5_crtc->vblank);
  560. pending = atomic_xchg(&mdp5_crtc->pending, 0);
  561. if (pending & PENDING_FLIP) {
  562. complete_flip(crtc, NULL);
  563. }
  564. if (pending & PENDING_CURSOR)
  565. drm_flip_work_commit(&mdp5_crtc->unref_cursor_work, priv->wq);
  566. }
  567. static void mdp5_crtc_err_irq(struct mdp_irq *irq, uint32_t irqstatus)
  568. {
  569. struct mdp5_crtc *mdp5_crtc = container_of(irq, struct mdp5_crtc, err);
  570. DBG("%s: error: %08x", mdp5_crtc->base.name, irqstatus);
  571. }
  572. static void mdp5_crtc_pp_done_irq(struct mdp_irq *irq, uint32_t irqstatus)
  573. {
  574. struct mdp5_crtc *mdp5_crtc = container_of(irq, struct mdp5_crtc,
  575. pp_done);
  576. complete(&mdp5_crtc->pp_completion);
  577. }
  578. static void mdp5_crtc_wait_for_pp_done(struct drm_crtc *crtc)
  579. {
  580. struct drm_device *dev = crtc->dev;
  581. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  582. int ret;
  583. ret = wait_for_completion_timeout(&mdp5_crtc->pp_completion,
  584. msecs_to_jiffies(50));
  585. if (ret == 0)
  586. dev_warn(dev->dev, "pp done time out, lm=%d\n", mdp5_crtc->lm);
  587. }
  588. static void mdp5_crtc_wait_for_flush_done(struct drm_crtc *crtc)
  589. {
  590. struct drm_device *dev = crtc->dev;
  591. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  592. int ret;
  593. /* Should not call this function if crtc is disabled. */
  594. if (!mdp5_crtc->ctl)
  595. return;
  596. ret = drm_crtc_vblank_get(crtc);
  597. if (ret)
  598. return;
  599. ret = wait_event_timeout(dev->vblank[drm_crtc_index(crtc)].queue,
  600. ((mdp5_ctl_get_commit_status(mdp5_crtc->ctl) &
  601. mdp5_crtc->flushed_mask) == 0),
  602. msecs_to_jiffies(50));
  603. if (ret <= 0)
  604. dev_warn(dev->dev, "vblank time out, crtc=%d\n", mdp5_crtc->id);
  605. mdp5_crtc->flushed_mask = 0;
  606. drm_crtc_vblank_put(crtc);
  607. }
  608. uint32_t mdp5_crtc_vblank(struct drm_crtc *crtc)
  609. {
  610. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  611. return mdp5_crtc->vblank.irqmask;
  612. }
  613. void mdp5_crtc_set_pipeline(struct drm_crtc *crtc,
  614. struct mdp5_interface *intf, struct mdp5_ctl *ctl)
  615. {
  616. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  617. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  618. int lm = mdp5_crtc_get_lm(crtc);
  619. /* now that we know what irq's we want: */
  620. mdp5_crtc->err.irqmask = intf2err(intf->num);
  621. mdp5_crtc->vblank.irqmask = intf2vblank(lm, intf);
  622. if ((intf->type == INTF_DSI) &&
  623. (intf->mode == MDP5_INTF_DSI_MODE_COMMAND)) {
  624. mdp5_crtc->pp_done.irqmask = lm2ppdone(lm);
  625. mdp5_crtc->pp_done.irq = mdp5_crtc_pp_done_irq;
  626. mdp5_crtc->cmd_mode = true;
  627. } else {
  628. mdp5_crtc->pp_done.irqmask = 0;
  629. mdp5_crtc->pp_done.irq = NULL;
  630. mdp5_crtc->cmd_mode = false;
  631. }
  632. mdp_irq_update(&mdp5_kms->base);
  633. mdp5_crtc->ctl = ctl;
  634. mdp5_ctl_set_pipeline(ctl, intf, lm);
  635. }
  636. struct mdp5_ctl *mdp5_crtc_get_ctl(struct drm_crtc *crtc)
  637. {
  638. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  639. return mdp5_crtc->ctl;
  640. }
  641. int mdp5_crtc_get_lm(struct drm_crtc *crtc)
  642. {
  643. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  644. return WARN_ON(!crtc) ? -EINVAL : mdp5_crtc->lm;
  645. }
  646. void mdp5_crtc_wait_for_commit_done(struct drm_crtc *crtc)
  647. {
  648. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  649. if (mdp5_crtc->cmd_mode)
  650. mdp5_crtc_wait_for_pp_done(crtc);
  651. else
  652. mdp5_crtc_wait_for_flush_done(crtc);
  653. }
  654. /* initialize crtc */
  655. struct drm_crtc *mdp5_crtc_init(struct drm_device *dev,
  656. struct drm_plane *plane,
  657. struct drm_plane *cursor_plane, int id)
  658. {
  659. struct drm_crtc *crtc = NULL;
  660. struct mdp5_crtc *mdp5_crtc;
  661. mdp5_crtc = kzalloc(sizeof(*mdp5_crtc), GFP_KERNEL);
  662. if (!mdp5_crtc)
  663. return ERR_PTR(-ENOMEM);
  664. crtc = &mdp5_crtc->base;
  665. mdp5_crtc->id = id;
  666. mdp5_crtc->lm = GET_LM_ID(id);
  667. spin_lock_init(&mdp5_crtc->lm_lock);
  668. spin_lock_init(&mdp5_crtc->cursor.lock);
  669. init_completion(&mdp5_crtc->pp_completion);
  670. mdp5_crtc->vblank.irq = mdp5_crtc_vblank_irq;
  671. mdp5_crtc->err.irq = mdp5_crtc_err_irq;
  672. if (cursor_plane)
  673. drm_crtc_init_with_planes(dev, crtc, plane, cursor_plane,
  674. &mdp5_crtc_no_lm_cursor_funcs, NULL);
  675. else
  676. drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  677. &mdp5_crtc_funcs, NULL);
  678. drm_flip_work_init(&mdp5_crtc->unref_cursor_work,
  679. "unref cursor", unref_cursor_worker);
  680. drm_crtc_helper_add(crtc, &mdp5_crtc_helper_funcs);
  681. plane->crtc = crtc;
  682. return crtc;
  683. }