mdp5_crtc.c 20 KB

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