mdp5_crtc.c 22 KB

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