mdp5_crtc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * Copyright (c) 2014 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. #define PENDING_CURSOR 0x1
  41. #define PENDING_FLIP 0x2
  42. atomic_t pending;
  43. /* for unref'ing cursor bo's after scanout completes: */
  44. struct drm_flip_work unref_cursor_work;
  45. struct mdp_irq vblank;
  46. struct mdp_irq err;
  47. struct {
  48. /* protect REG_MDP5_LM_CURSOR* registers and cursor scanout_bo*/
  49. spinlock_t lock;
  50. /* current cursor being scanned out: */
  51. struct drm_gem_object *scanout_bo;
  52. uint32_t width, height;
  53. uint32_t x, y;
  54. } cursor;
  55. };
  56. #define to_mdp5_crtc(x) container_of(x, struct mdp5_crtc, base)
  57. static struct mdp5_kms *get_kms(struct drm_crtc *crtc)
  58. {
  59. struct msm_drm_private *priv = crtc->dev->dev_private;
  60. return to_mdp5_kms(to_mdp_kms(priv->kms));
  61. }
  62. static void request_pending(struct drm_crtc *crtc, uint32_t pending)
  63. {
  64. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  65. atomic_or(pending, &mdp5_crtc->pending);
  66. mdp_irq_register(&get_kms(crtc)->base, &mdp5_crtc->vblank);
  67. }
  68. static void crtc_flush(struct drm_crtc *crtc, u32 flush_mask)
  69. {
  70. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  71. DBG("%s: flush=%08x", mdp5_crtc->name, flush_mask);
  72. mdp5_ctl_commit(mdp5_crtc->ctl, flush_mask);
  73. }
  74. /*
  75. * flush updates, to make sure hw is updated to new scanout fb,
  76. * so that we can safely queue unref to current fb (ie. next
  77. * vblank we know hw is done w/ previous scanout_fb).
  78. */
  79. static void crtc_flush_all(struct drm_crtc *crtc)
  80. {
  81. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  82. struct drm_plane *plane;
  83. uint32_t flush_mask = 0;
  84. /* this should not happen: */
  85. if (WARN_ON(!mdp5_crtc->ctl))
  86. return;
  87. drm_atomic_crtc_for_each_plane(plane, crtc) {
  88. flush_mask |= mdp5_plane_get_flush(plane);
  89. }
  90. flush_mask |= mdp_ctl_flush_mask_lm(mdp5_crtc->lm);
  91. crtc_flush(crtc, flush_mask);
  92. }
  93. /* if file!=NULL, this is preclose potential cancel-flip path */
  94. static void complete_flip(struct drm_crtc *crtc, struct drm_file *file)
  95. {
  96. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  97. struct drm_device *dev = crtc->dev;
  98. struct drm_pending_vblank_event *event;
  99. struct drm_plane *plane;
  100. unsigned long flags;
  101. spin_lock_irqsave(&dev->event_lock, flags);
  102. event = mdp5_crtc->event;
  103. if (event) {
  104. /* if regular vblank case (!file) or if cancel-flip from
  105. * preclose on file that requested flip, then send the
  106. * event:
  107. */
  108. if (!file || (event->base.file_priv == file)) {
  109. mdp5_crtc->event = NULL;
  110. DBG("%s: send event: %p", mdp5_crtc->name, event);
  111. drm_send_vblank_event(dev, mdp5_crtc->id, event);
  112. }
  113. }
  114. spin_unlock_irqrestore(&dev->event_lock, flags);
  115. drm_atomic_crtc_for_each_plane(plane, crtc) {
  116. mdp5_plane_complete_flip(plane);
  117. }
  118. if (mdp5_crtc->ctl && !crtc->state->enable) {
  119. mdp5_ctl_release(mdp5_crtc->ctl);
  120. mdp5_crtc->ctl = NULL;
  121. }
  122. }
  123. static void unref_cursor_worker(struct drm_flip_work *work, void *val)
  124. {
  125. struct mdp5_crtc *mdp5_crtc =
  126. container_of(work, struct mdp5_crtc, unref_cursor_work);
  127. struct mdp5_kms *mdp5_kms = get_kms(&mdp5_crtc->base);
  128. msm_gem_put_iova(val, mdp5_kms->id);
  129. drm_gem_object_unreference_unlocked(val);
  130. }
  131. static void mdp5_crtc_destroy(struct drm_crtc *crtc)
  132. {
  133. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  134. drm_crtc_cleanup(crtc);
  135. drm_flip_work_cleanup(&mdp5_crtc->unref_cursor_work);
  136. kfree(mdp5_crtc);
  137. }
  138. static bool mdp5_crtc_mode_fixup(struct drm_crtc *crtc,
  139. const struct drm_display_mode *mode,
  140. struct drm_display_mode *adjusted_mode)
  141. {
  142. return true;
  143. }
  144. /*
  145. * blend_setup() - blend all the planes of a CRTC
  146. *
  147. * When border is enabled, the border color will ALWAYS be the base layer.
  148. * Therefore, the first plane (private RGB pipe) will start at STAGE0.
  149. * If disabled, the first plane starts at STAGE_BASE.
  150. *
  151. * Note:
  152. * Border is not enabled here because the private plane is exactly
  153. * the CRTC resolution.
  154. */
  155. static void blend_setup(struct drm_crtc *crtc)
  156. {
  157. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  158. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  159. struct drm_plane *plane;
  160. const struct mdp5_cfg_hw *hw_cfg;
  161. uint32_t lm = mdp5_crtc->lm, blend_cfg = 0;
  162. unsigned long flags;
  163. #define blender(stage) ((stage) - STAGE_BASE)
  164. hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
  165. spin_lock_irqsave(&mdp5_crtc->lm_lock, flags);
  166. /* ctl could be released already when we are shutting down: */
  167. if (!mdp5_crtc->ctl)
  168. goto out;
  169. drm_atomic_crtc_for_each_plane(plane, crtc) {
  170. enum mdp_mixer_stage_id stage =
  171. to_mdp5_plane_state(plane->state)->stage;
  172. /*
  173. * Note: This cannot happen with current implementation but
  174. * we need to check this condition once z property is added
  175. */
  176. BUG_ON(stage > hw_cfg->lm.nb_stages);
  177. /* LM */
  178. mdp5_write(mdp5_kms,
  179. REG_MDP5_LM_BLEND_OP_MODE(lm, blender(stage)),
  180. MDP5_LM_BLEND_OP_MODE_FG_ALPHA(FG_CONST) |
  181. MDP5_LM_BLEND_OP_MODE_BG_ALPHA(BG_CONST));
  182. mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_FG_ALPHA(lm,
  183. blender(stage)), 0xff);
  184. mdp5_write(mdp5_kms, REG_MDP5_LM_BLEND_BG_ALPHA(lm,
  185. blender(stage)), 0x00);
  186. /* CTL */
  187. blend_cfg |= mdp_ctl_blend_mask(mdp5_plane_pipe(plane), stage);
  188. DBG("%s: blending pipe %s on stage=%d", mdp5_crtc->name,
  189. pipe2name(mdp5_plane_pipe(plane)), stage);
  190. }
  191. DBG("%s: lm%d: blend config = 0x%08x", mdp5_crtc->name, lm, blend_cfg);
  192. mdp5_ctl_blend(mdp5_crtc->ctl, lm, blend_cfg);
  193. out:
  194. spin_unlock_irqrestore(&mdp5_crtc->lm_lock, flags);
  195. }
  196. static void mdp5_crtc_mode_set_nofb(struct drm_crtc *crtc)
  197. {
  198. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  199. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  200. unsigned long flags;
  201. struct drm_display_mode *mode;
  202. if (WARN_ON(!crtc->state))
  203. return;
  204. mode = &crtc->state->adjusted_mode;
  205. DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  206. mdp5_crtc->name, mode->base.id, mode->name,
  207. mode->vrefresh, mode->clock,
  208. mode->hdisplay, mode->hsync_start,
  209. mode->hsync_end, mode->htotal,
  210. mode->vdisplay, mode->vsync_start,
  211. mode->vsync_end, mode->vtotal,
  212. mode->type, mode->flags);
  213. spin_lock_irqsave(&mdp5_crtc->lm_lock, flags);
  214. mdp5_write(mdp5_kms, REG_MDP5_LM_OUT_SIZE(mdp5_crtc->lm),
  215. MDP5_LM_OUT_SIZE_WIDTH(mode->hdisplay) |
  216. MDP5_LM_OUT_SIZE_HEIGHT(mode->vdisplay));
  217. spin_unlock_irqrestore(&mdp5_crtc->lm_lock, flags);
  218. }
  219. static void mdp5_crtc_disable(struct drm_crtc *crtc)
  220. {
  221. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  222. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  223. DBG("%s", mdp5_crtc->name);
  224. if (WARN_ON(!mdp5_crtc->enabled))
  225. return;
  226. /* set STAGE_UNUSED for all layers */
  227. mdp5_ctl_blend(mdp5_crtc->ctl, mdp5_crtc->lm, 0x00000000);
  228. mdp_irq_unregister(&mdp5_kms->base, &mdp5_crtc->err);
  229. mdp5_disable(mdp5_kms);
  230. mdp5_crtc->enabled = false;
  231. }
  232. static void mdp5_crtc_enable(struct drm_crtc *crtc)
  233. {
  234. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  235. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  236. DBG("%s", mdp5_crtc->name);
  237. if (WARN_ON(mdp5_crtc->enabled))
  238. return;
  239. mdp5_enable(mdp5_kms);
  240. mdp_irq_register(&mdp5_kms->base, &mdp5_crtc->err);
  241. mdp5_crtc->enabled = true;
  242. }
  243. struct plane_state {
  244. struct drm_plane *plane;
  245. struct mdp5_plane_state *state;
  246. };
  247. static int pstate_cmp(const void *a, const void *b)
  248. {
  249. struct plane_state *pa = (struct plane_state *)a;
  250. struct plane_state *pb = (struct plane_state *)b;
  251. return pa->state->zpos - pb->state->zpos;
  252. }
  253. static int mdp5_crtc_atomic_check(struct drm_crtc *crtc,
  254. struct drm_crtc_state *state)
  255. {
  256. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  257. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  258. struct drm_plane *plane;
  259. struct drm_device *dev = crtc->dev;
  260. struct plane_state pstates[STAGE3 + 1];
  261. int cnt = 0, i;
  262. DBG("%s: check", mdp5_crtc->name);
  263. /* request a free CTL, if none is already allocated for this CRTC */
  264. if (state->enable && !mdp5_crtc->ctl) {
  265. mdp5_crtc->ctl = mdp5_ctlm_request(mdp5_kms->ctlm, crtc);
  266. if (WARN_ON(!mdp5_crtc->ctl))
  267. return -EINVAL;
  268. }
  269. /* verify that there are not too many planes attached to crtc
  270. * and that we don't have conflicting mixer stages:
  271. */
  272. drm_atomic_crtc_state_for_each_plane(plane, state) {
  273. struct drm_plane_state *pstate;
  274. if (cnt >= ARRAY_SIZE(pstates)) {
  275. dev_err(dev->dev, "too many planes!\n");
  276. return -EINVAL;
  277. }
  278. pstate = state->state->plane_states[drm_plane_index(plane)];
  279. /* plane might not have changed, in which case take
  280. * current state:
  281. */
  282. if (!pstate)
  283. pstate = plane->state;
  284. pstates[cnt].plane = plane;
  285. pstates[cnt].state = to_mdp5_plane_state(pstate);
  286. cnt++;
  287. }
  288. sort(pstates, cnt, sizeof(pstates[0]), pstate_cmp, NULL);
  289. for (i = 0; i < cnt; i++) {
  290. pstates[i].state->stage = STAGE_BASE + i;
  291. DBG("%s: assign pipe %s on stage=%d", mdp5_crtc->name,
  292. pipe2name(mdp5_plane_pipe(pstates[i].plane)),
  293. pstates[i].state->stage);
  294. }
  295. return 0;
  296. }
  297. static void mdp5_crtc_atomic_begin(struct drm_crtc *crtc)
  298. {
  299. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  300. DBG("%s: begin", mdp5_crtc->name);
  301. }
  302. static void mdp5_crtc_atomic_flush(struct drm_crtc *crtc)
  303. {
  304. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  305. struct drm_device *dev = crtc->dev;
  306. unsigned long flags;
  307. DBG("%s: event: %p", mdp5_crtc->name, crtc->state->event);
  308. WARN_ON(mdp5_crtc->event);
  309. spin_lock_irqsave(&dev->event_lock, flags);
  310. mdp5_crtc->event = crtc->state->event;
  311. spin_unlock_irqrestore(&dev->event_lock, flags);
  312. /*
  313. * If no CTL has been allocated in mdp5_crtc_atomic_check(),
  314. * it means we are trying to flush a CRTC whose state is disabled:
  315. * nothing else needs to be done.
  316. */
  317. if (unlikely(!mdp5_crtc->ctl))
  318. return;
  319. blend_setup(crtc);
  320. crtc_flush_all(crtc);
  321. request_pending(crtc, PENDING_FLIP);
  322. }
  323. static int mdp5_crtc_set_property(struct drm_crtc *crtc,
  324. struct drm_property *property, uint64_t val)
  325. {
  326. // XXX
  327. return -EINVAL;
  328. }
  329. static void get_roi(struct drm_crtc *crtc, uint32_t *roi_w, uint32_t *roi_h)
  330. {
  331. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  332. uint32_t xres = crtc->mode.hdisplay;
  333. uint32_t yres = crtc->mode.vdisplay;
  334. /*
  335. * Cursor Region Of Interest (ROI) is a plane read from cursor
  336. * buffer to render. The ROI region is determined by the visibility of
  337. * the cursor point. In the default Cursor image the cursor point will
  338. * be at the top left of the cursor image, unless it is specified
  339. * otherwise using hotspot feature.
  340. *
  341. * If the cursor point reaches the right (xres - x < cursor.width) or
  342. * bottom (yres - y < cursor.height) boundary of the screen, then ROI
  343. * width and ROI height need to be evaluated to crop the cursor image
  344. * accordingly.
  345. * (xres-x) will be new cursor width when x > (xres - cursor.width)
  346. * (yres-y) will be new cursor height when y > (yres - cursor.height)
  347. */
  348. *roi_w = min(mdp5_crtc->cursor.width, xres -
  349. mdp5_crtc->cursor.x);
  350. *roi_h = min(mdp5_crtc->cursor.height, yres -
  351. mdp5_crtc->cursor.y);
  352. }
  353. static int mdp5_crtc_cursor_set(struct drm_crtc *crtc,
  354. struct drm_file *file, uint32_t handle,
  355. uint32_t width, uint32_t height)
  356. {
  357. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  358. struct drm_device *dev = crtc->dev;
  359. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  360. struct drm_gem_object *cursor_bo, *old_bo = NULL;
  361. uint32_t blendcfg, cursor_addr, stride;
  362. int ret, bpp, lm;
  363. unsigned int depth;
  364. enum mdp5_cursor_alpha cur_alpha = CURSOR_ALPHA_PER_PIXEL;
  365. uint32_t flush_mask = mdp_ctl_flush_mask_cursor(0);
  366. uint32_t roi_w, roi_h;
  367. bool cursor_enable = true;
  368. unsigned long flags;
  369. if ((width > CURSOR_WIDTH) || (height > CURSOR_HEIGHT)) {
  370. dev_err(dev->dev, "bad cursor size: %dx%d\n", width, height);
  371. return -EINVAL;
  372. }
  373. if (NULL == mdp5_crtc->ctl)
  374. return -EINVAL;
  375. if (!handle) {
  376. DBG("Cursor off");
  377. cursor_enable = false;
  378. goto set_cursor;
  379. }
  380. cursor_bo = drm_gem_object_lookup(dev, file, handle);
  381. if (!cursor_bo)
  382. return -ENOENT;
  383. ret = msm_gem_get_iova(cursor_bo, mdp5_kms->id, &cursor_addr);
  384. if (ret)
  385. return -EINVAL;
  386. lm = mdp5_crtc->lm;
  387. drm_fb_get_bpp_depth(DRM_FORMAT_ARGB8888, &depth, &bpp);
  388. stride = width * (bpp >> 3);
  389. spin_lock_irqsave(&mdp5_crtc->cursor.lock, flags);
  390. old_bo = mdp5_crtc->cursor.scanout_bo;
  391. mdp5_crtc->cursor.scanout_bo = cursor_bo;
  392. mdp5_crtc->cursor.width = width;
  393. mdp5_crtc->cursor.height = height;
  394. get_roi(crtc, &roi_w, &roi_h);
  395. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_STRIDE(lm), stride);
  396. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_FORMAT(lm),
  397. MDP5_LM_CURSOR_FORMAT_FORMAT(CURSOR_FMT_ARGB8888));
  398. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_IMG_SIZE(lm),
  399. MDP5_LM_CURSOR_IMG_SIZE_SRC_H(height) |
  400. MDP5_LM_CURSOR_IMG_SIZE_SRC_W(width));
  401. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_SIZE(lm),
  402. MDP5_LM_CURSOR_SIZE_ROI_H(roi_h) |
  403. MDP5_LM_CURSOR_SIZE_ROI_W(roi_w));
  404. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_BASE_ADDR(lm), cursor_addr);
  405. blendcfg = MDP5_LM_CURSOR_BLEND_CONFIG_BLEND_EN;
  406. blendcfg |= MDP5_LM_CURSOR_BLEND_CONFIG_BLEND_ALPHA_SEL(cur_alpha);
  407. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_BLEND_CONFIG(lm), blendcfg);
  408. spin_unlock_irqrestore(&mdp5_crtc->cursor.lock, flags);
  409. set_cursor:
  410. ret = mdp5_ctl_set_cursor(mdp5_crtc->ctl, 0, cursor_enable);
  411. if (ret) {
  412. dev_err(dev->dev, "failed to %sable cursor: %d\n",
  413. cursor_enable ? "en" : "dis", ret);
  414. goto end;
  415. }
  416. crtc_flush(crtc, flush_mask);
  417. end:
  418. if (old_bo) {
  419. drm_flip_work_queue(&mdp5_crtc->unref_cursor_work, old_bo);
  420. /* enable vblank to complete cursor work: */
  421. request_pending(crtc, PENDING_CURSOR);
  422. }
  423. return ret;
  424. }
  425. static int mdp5_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  426. {
  427. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  428. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  429. uint32_t flush_mask = mdp_ctl_flush_mask_cursor(0);
  430. uint32_t roi_w;
  431. uint32_t roi_h;
  432. unsigned long flags;
  433. /* In case the CRTC is disabled, just drop the cursor update */
  434. if (unlikely(!crtc->state->enable))
  435. return 0;
  436. mdp5_crtc->cursor.x = x = max(x, 0);
  437. mdp5_crtc->cursor.y = y = max(y, 0);
  438. get_roi(crtc, &roi_w, &roi_h);
  439. spin_lock_irqsave(&mdp5_crtc->cursor.lock, flags);
  440. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_SIZE(mdp5_crtc->lm),
  441. MDP5_LM_CURSOR_SIZE_ROI_H(roi_h) |
  442. MDP5_LM_CURSOR_SIZE_ROI_W(roi_w));
  443. mdp5_write(mdp5_kms, REG_MDP5_LM_CURSOR_START_XY(mdp5_crtc->lm),
  444. MDP5_LM_CURSOR_START_XY_Y_START(y) |
  445. MDP5_LM_CURSOR_START_XY_X_START(x));
  446. spin_unlock_irqrestore(&mdp5_crtc->cursor.lock, flags);
  447. crtc_flush(crtc, flush_mask);
  448. return 0;
  449. }
  450. static const struct drm_crtc_funcs mdp5_crtc_funcs = {
  451. .set_config = drm_atomic_helper_set_config,
  452. .destroy = mdp5_crtc_destroy,
  453. .page_flip = drm_atomic_helper_page_flip,
  454. .set_property = mdp5_crtc_set_property,
  455. .reset = drm_atomic_helper_crtc_reset,
  456. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  457. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  458. .cursor_set = mdp5_crtc_cursor_set,
  459. .cursor_move = mdp5_crtc_cursor_move,
  460. };
  461. static const struct drm_crtc_helper_funcs mdp5_crtc_helper_funcs = {
  462. .mode_fixup = mdp5_crtc_mode_fixup,
  463. .mode_set_nofb = mdp5_crtc_mode_set_nofb,
  464. .disable = mdp5_crtc_disable,
  465. .enable = mdp5_crtc_enable,
  466. .atomic_check = mdp5_crtc_atomic_check,
  467. .atomic_begin = mdp5_crtc_atomic_begin,
  468. .atomic_flush = mdp5_crtc_atomic_flush,
  469. };
  470. static void mdp5_crtc_vblank_irq(struct mdp_irq *irq, uint32_t irqstatus)
  471. {
  472. struct mdp5_crtc *mdp5_crtc = container_of(irq, struct mdp5_crtc, vblank);
  473. struct drm_crtc *crtc = &mdp5_crtc->base;
  474. struct msm_drm_private *priv = crtc->dev->dev_private;
  475. unsigned pending;
  476. mdp_irq_unregister(&get_kms(crtc)->base, &mdp5_crtc->vblank);
  477. pending = atomic_xchg(&mdp5_crtc->pending, 0);
  478. if (pending & PENDING_FLIP) {
  479. complete_flip(crtc, NULL);
  480. }
  481. if (pending & PENDING_CURSOR)
  482. drm_flip_work_commit(&mdp5_crtc->unref_cursor_work, priv->wq);
  483. }
  484. static void mdp5_crtc_err_irq(struct mdp_irq *irq, uint32_t irqstatus)
  485. {
  486. struct mdp5_crtc *mdp5_crtc = container_of(irq, struct mdp5_crtc, err);
  487. DBG("%s: error: %08x", mdp5_crtc->name, irqstatus);
  488. }
  489. uint32_t mdp5_crtc_vblank(struct drm_crtc *crtc)
  490. {
  491. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  492. return mdp5_crtc->vblank.irqmask;
  493. }
  494. void mdp5_crtc_cancel_pending_flip(struct drm_crtc *crtc, struct drm_file *file)
  495. {
  496. DBG("cancel: %p", file);
  497. complete_flip(crtc, file);
  498. }
  499. /* set interface for routing crtc->encoder: */
  500. void mdp5_crtc_set_intf(struct drm_crtc *crtc, struct mdp5_interface *intf)
  501. {
  502. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  503. struct mdp5_kms *mdp5_kms = get_kms(crtc);
  504. int lm = mdp5_crtc_get_lm(crtc);
  505. /* now that we know what irq's we want: */
  506. mdp5_crtc->err.irqmask = intf2err(intf->num);
  507. /* Register command mode Pingpong done as vblank for now,
  508. * so that atomic commit should wait for it to finish.
  509. * Ideally, in the future, we should take rd_ptr done as vblank,
  510. * and let atomic commit wait for pingpong done for commond mode.
  511. */
  512. if (intf->mode == MDP5_INTF_DSI_MODE_COMMAND)
  513. mdp5_crtc->vblank.irqmask = lm2ppdone(lm);
  514. else
  515. mdp5_crtc->vblank.irqmask = intf2vblank(lm, intf);
  516. mdp_irq_update(&mdp5_kms->base);
  517. mdp5_ctl_set_intf(mdp5_crtc->ctl, intf);
  518. }
  519. int mdp5_crtc_get_lm(struct drm_crtc *crtc)
  520. {
  521. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  522. return WARN_ON(!crtc) ? -EINVAL : mdp5_crtc->lm;
  523. }
  524. struct mdp5_ctl *mdp5_crtc_get_ctl(struct drm_crtc *crtc)
  525. {
  526. struct mdp5_crtc *mdp5_crtc = to_mdp5_crtc(crtc);
  527. return WARN_ON(!crtc) ? NULL : mdp5_crtc->ctl;
  528. }
  529. /* initialize crtc */
  530. struct drm_crtc *mdp5_crtc_init(struct drm_device *dev,
  531. struct drm_plane *plane, int id)
  532. {
  533. struct drm_crtc *crtc = NULL;
  534. struct mdp5_crtc *mdp5_crtc;
  535. mdp5_crtc = kzalloc(sizeof(*mdp5_crtc), GFP_KERNEL);
  536. if (!mdp5_crtc)
  537. return ERR_PTR(-ENOMEM);
  538. crtc = &mdp5_crtc->base;
  539. mdp5_crtc->id = id;
  540. mdp5_crtc->lm = GET_LM_ID(id);
  541. spin_lock_init(&mdp5_crtc->lm_lock);
  542. spin_lock_init(&mdp5_crtc->cursor.lock);
  543. mdp5_crtc->vblank.irq = mdp5_crtc_vblank_irq;
  544. mdp5_crtc->err.irq = mdp5_crtc_err_irq;
  545. snprintf(mdp5_crtc->name, sizeof(mdp5_crtc->name), "%s:%d",
  546. pipe2name(mdp5_plane_pipe(plane)), id);
  547. drm_crtc_init_with_planes(dev, crtc, plane, NULL, &mdp5_crtc_funcs);
  548. drm_flip_work_init(&mdp5_crtc->unref_cursor_work,
  549. "unref cursor", unref_cursor_worker);
  550. drm_crtc_helper_add(crtc, &mdp5_crtc_helper_funcs);
  551. plane->crtc = crtc;
  552. mdp5_plane_install_properties(plane, &crtc->base);
  553. return crtc;
  554. }