mdp4_crtc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /*
  2. * Copyright (C) 2013 Red Hat
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "mdp4_kms.h"
  18. #include <drm/drm_mode.h>
  19. #include "drm_crtc.h"
  20. #include "drm_crtc_helper.h"
  21. #include "drm_flip_work.h"
  22. struct mdp4_crtc {
  23. struct drm_crtc base;
  24. char name[8];
  25. struct drm_plane *plane;
  26. struct drm_plane *planes[8];
  27. int id;
  28. int ovlp;
  29. enum mdp4_dma dma;
  30. bool enabled;
  31. /* which mixer/encoder we route output to: */
  32. int mixer;
  33. struct {
  34. spinlock_t lock;
  35. bool stale;
  36. uint32_t width, height;
  37. uint32_t x, y;
  38. /* next cursor to scan-out: */
  39. uint32_t next_iova;
  40. struct drm_gem_object *next_bo;
  41. /* current cursor being scanned out: */
  42. struct drm_gem_object *scanout_bo;
  43. } cursor;
  44. /* if there is a pending flip, these will be non-null: */
  45. struct drm_pending_vblank_event *event;
  46. struct msm_fence_cb pageflip_cb;
  47. #define PENDING_CURSOR 0x1
  48. #define PENDING_FLIP 0x2
  49. atomic_t pending;
  50. /* the fb that we logically (from PoV of KMS API) hold a ref
  51. * to. Which we may not yet be scanning out (we may still
  52. * be scanning out previous in case of page_flip while waiting
  53. * for gpu rendering to complete:
  54. */
  55. struct drm_framebuffer *fb;
  56. /* the fb that we currently hold a scanout ref to: */
  57. struct drm_framebuffer *scanout_fb;
  58. /* for unref'ing framebuffers after scanout completes: */
  59. struct drm_flip_work unref_fb_work;
  60. /* for unref'ing cursor bo's after scanout completes: */
  61. struct drm_flip_work unref_cursor_work;
  62. struct mdp_irq vblank;
  63. struct mdp_irq err;
  64. };
  65. #define to_mdp4_crtc(x) container_of(x, struct mdp4_crtc, base)
  66. static struct mdp4_kms *get_kms(struct drm_crtc *crtc)
  67. {
  68. struct msm_drm_private *priv = crtc->dev->dev_private;
  69. return to_mdp4_kms(to_mdp_kms(priv->kms));
  70. }
  71. static void request_pending(struct drm_crtc *crtc, uint32_t pending)
  72. {
  73. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  74. atomic_or(pending, &mdp4_crtc->pending);
  75. mdp_irq_register(&get_kms(crtc)->base, &mdp4_crtc->vblank);
  76. }
  77. static void crtc_flush(struct drm_crtc *crtc)
  78. {
  79. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  80. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  81. uint32_t i, flush = 0;
  82. for (i = 0; i < ARRAY_SIZE(mdp4_crtc->planes); i++) {
  83. struct drm_plane *plane = mdp4_crtc->planes[i];
  84. if (plane) {
  85. enum mdp4_pipe pipe_id = mdp4_plane_pipe(plane);
  86. flush |= pipe2flush(pipe_id);
  87. }
  88. }
  89. flush |= ovlp2flush(mdp4_crtc->ovlp);
  90. DBG("%s: flush=%08x", mdp4_crtc->name, flush);
  91. mdp4_write(mdp4_kms, REG_MDP4_OVERLAY_FLUSH, flush);
  92. }
  93. static void update_fb(struct drm_crtc *crtc, struct drm_framebuffer *new_fb)
  94. {
  95. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  96. struct drm_framebuffer *old_fb = mdp4_crtc->fb;
  97. /* grab reference to incoming scanout fb: */
  98. drm_framebuffer_reference(new_fb);
  99. mdp4_crtc->base.primary->fb = new_fb;
  100. mdp4_crtc->fb = new_fb;
  101. if (old_fb)
  102. drm_flip_work_queue(&mdp4_crtc->unref_fb_work, old_fb);
  103. }
  104. /* unlike update_fb(), take a ref to the new scanout fb *before* updating
  105. * plane, then call this. Needed to ensure we don't unref the buffer that
  106. * is actually still being scanned out.
  107. *
  108. * Note that this whole thing goes away with atomic.. since we can defer
  109. * calling into driver until rendering is done.
  110. */
  111. static void update_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
  112. {
  113. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  114. /* flush updates, to make sure hw is updated to new scanout fb,
  115. * so that we can safely queue unref to current fb (ie. next
  116. * vblank we know hw is done w/ previous scanout_fb).
  117. */
  118. crtc_flush(crtc);
  119. if (mdp4_crtc->scanout_fb)
  120. drm_flip_work_queue(&mdp4_crtc->unref_fb_work,
  121. mdp4_crtc->scanout_fb);
  122. mdp4_crtc->scanout_fb = fb;
  123. /* enable vblank to complete flip: */
  124. request_pending(crtc, PENDING_FLIP);
  125. }
  126. /* if file!=NULL, this is preclose potential cancel-flip path */
  127. static void complete_flip(struct drm_crtc *crtc, struct drm_file *file)
  128. {
  129. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  130. struct drm_device *dev = crtc->dev;
  131. struct drm_pending_vblank_event *event;
  132. unsigned long flags;
  133. spin_lock_irqsave(&dev->event_lock, flags);
  134. event = mdp4_crtc->event;
  135. if (event) {
  136. /* if regular vblank case (!file) or if cancel-flip from
  137. * preclose on file that requested flip, then send the
  138. * event:
  139. */
  140. if (!file || (event->base.file_priv == file)) {
  141. mdp4_crtc->event = NULL;
  142. drm_send_vblank_event(dev, mdp4_crtc->id, event);
  143. }
  144. }
  145. spin_unlock_irqrestore(&dev->event_lock, flags);
  146. }
  147. static void pageflip_cb(struct msm_fence_cb *cb)
  148. {
  149. struct mdp4_crtc *mdp4_crtc =
  150. container_of(cb, struct mdp4_crtc, pageflip_cb);
  151. struct drm_crtc *crtc = &mdp4_crtc->base;
  152. struct drm_framebuffer *fb = crtc->primary->fb;
  153. if (!fb)
  154. return;
  155. drm_framebuffer_reference(fb);
  156. mdp4_plane_set_scanout(mdp4_crtc->plane, fb);
  157. update_scanout(crtc, fb);
  158. }
  159. static void unref_fb_worker(struct drm_flip_work *work, void *val)
  160. {
  161. struct mdp4_crtc *mdp4_crtc =
  162. container_of(work, struct mdp4_crtc, unref_fb_work);
  163. struct drm_device *dev = mdp4_crtc->base.dev;
  164. mutex_lock(&dev->mode_config.mutex);
  165. drm_framebuffer_unreference(val);
  166. mutex_unlock(&dev->mode_config.mutex);
  167. }
  168. static void unref_cursor_worker(struct drm_flip_work *work, void *val)
  169. {
  170. struct mdp4_crtc *mdp4_crtc =
  171. container_of(work, struct mdp4_crtc, unref_cursor_work);
  172. struct mdp4_kms *mdp4_kms = get_kms(&mdp4_crtc->base);
  173. msm_gem_put_iova(val, mdp4_kms->id);
  174. drm_gem_object_unreference_unlocked(val);
  175. }
  176. static void mdp4_crtc_destroy(struct drm_crtc *crtc)
  177. {
  178. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  179. drm_crtc_cleanup(crtc);
  180. drm_flip_work_cleanup(&mdp4_crtc->unref_fb_work);
  181. drm_flip_work_cleanup(&mdp4_crtc->unref_cursor_work);
  182. kfree(mdp4_crtc);
  183. }
  184. static void mdp4_crtc_dpms(struct drm_crtc *crtc, int mode)
  185. {
  186. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  187. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  188. bool enabled = (mode == DRM_MODE_DPMS_ON);
  189. DBG("%s: mode=%d", mdp4_crtc->name, mode);
  190. if (enabled != mdp4_crtc->enabled) {
  191. if (enabled) {
  192. mdp4_enable(mdp4_kms);
  193. mdp_irq_register(&mdp4_kms->base, &mdp4_crtc->err);
  194. } else {
  195. mdp_irq_unregister(&mdp4_kms->base, &mdp4_crtc->err);
  196. mdp4_disable(mdp4_kms);
  197. }
  198. mdp4_crtc->enabled = enabled;
  199. }
  200. }
  201. static bool mdp4_crtc_mode_fixup(struct drm_crtc *crtc,
  202. const struct drm_display_mode *mode,
  203. struct drm_display_mode *adjusted_mode)
  204. {
  205. return true;
  206. }
  207. static void blend_setup(struct drm_crtc *crtc)
  208. {
  209. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  210. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  211. int i, ovlp = mdp4_crtc->ovlp;
  212. uint32_t mixer_cfg = 0;
  213. static const enum mdp_mixer_stage_id stages[] = {
  214. STAGE_BASE, STAGE0, STAGE1, STAGE2, STAGE3,
  215. };
  216. /* statically (for now) map planes to mixer stage (z-order): */
  217. static const int idxs[] = {
  218. [VG1] = 1,
  219. [VG2] = 2,
  220. [RGB1] = 0,
  221. [RGB2] = 0,
  222. [RGB3] = 0,
  223. [VG3] = 3,
  224. [VG4] = 4,
  225. };
  226. bool alpha[4]= { false, false, false, false };
  227. mdp4_write(mdp4_kms, REG_MDP4_OVLP_TRANSP_LOW0(ovlp), 0);
  228. mdp4_write(mdp4_kms, REG_MDP4_OVLP_TRANSP_LOW1(ovlp), 0);
  229. mdp4_write(mdp4_kms, REG_MDP4_OVLP_TRANSP_HIGH0(ovlp), 0);
  230. mdp4_write(mdp4_kms, REG_MDP4_OVLP_TRANSP_HIGH1(ovlp), 0);
  231. /* TODO single register for all CRTCs, so this won't work properly
  232. * when multiple CRTCs are active..
  233. */
  234. for (i = 0; i < ARRAY_SIZE(mdp4_crtc->planes); i++) {
  235. struct drm_plane *plane = mdp4_crtc->planes[i];
  236. if (plane) {
  237. enum mdp4_pipe pipe_id = mdp4_plane_pipe(plane);
  238. int idx = idxs[pipe_id];
  239. if (idx > 0) {
  240. const struct mdp_format *format =
  241. to_mdp_format(msm_framebuffer_format(plane->fb));
  242. alpha[idx-1] = format->alpha_enable;
  243. }
  244. mixer_cfg |= mixercfg(mdp4_crtc->mixer, pipe_id, stages[idx]);
  245. }
  246. }
  247. /* this shouldn't happen.. and seems to cause underflow: */
  248. WARN_ON(!mixer_cfg);
  249. for (i = 0; i < 4; i++) {
  250. uint32_t op;
  251. if (alpha[i]) {
  252. op = MDP4_OVLP_STAGE_OP_FG_ALPHA(FG_PIXEL) |
  253. MDP4_OVLP_STAGE_OP_BG_ALPHA(FG_PIXEL) |
  254. MDP4_OVLP_STAGE_OP_BG_INV_ALPHA;
  255. } else {
  256. op = MDP4_OVLP_STAGE_OP_FG_ALPHA(FG_CONST) |
  257. MDP4_OVLP_STAGE_OP_BG_ALPHA(BG_CONST);
  258. }
  259. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_FG_ALPHA(ovlp, i), 0xff);
  260. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_BG_ALPHA(ovlp, i), 0x00);
  261. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_OP(ovlp, i), op);
  262. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_CO3(ovlp, i), 1);
  263. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_TRANSP_LOW0(ovlp, i), 0);
  264. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_TRANSP_LOW1(ovlp, i), 0);
  265. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_TRANSP_HIGH0(ovlp, i), 0);
  266. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_TRANSP_HIGH1(ovlp, i), 0);
  267. }
  268. mdp4_write(mdp4_kms, REG_MDP4_LAYERMIXER_IN_CFG, mixer_cfg);
  269. }
  270. static int mdp4_crtc_mode_set(struct drm_crtc *crtc,
  271. struct drm_display_mode *mode,
  272. struct drm_display_mode *adjusted_mode,
  273. int x, int y,
  274. struct drm_framebuffer *old_fb)
  275. {
  276. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  277. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  278. enum mdp4_dma dma = mdp4_crtc->dma;
  279. int ret, ovlp = mdp4_crtc->ovlp;
  280. mode = adjusted_mode;
  281. DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  282. mdp4_crtc->name, mode->base.id, mode->name,
  283. mode->vrefresh, mode->clock,
  284. mode->hdisplay, mode->hsync_start,
  285. mode->hsync_end, mode->htotal,
  286. mode->vdisplay, mode->vsync_start,
  287. mode->vsync_end, mode->vtotal,
  288. mode->type, mode->flags);
  289. /* grab extra ref for update_scanout() */
  290. drm_framebuffer_reference(crtc->primary->fb);
  291. ret = mdp4_plane_mode_set(mdp4_crtc->plane, crtc, crtc->primary->fb,
  292. 0, 0, mode->hdisplay, mode->vdisplay,
  293. x << 16, y << 16,
  294. mode->hdisplay << 16, mode->vdisplay << 16);
  295. if (ret) {
  296. drm_framebuffer_unreference(crtc->primary->fb);
  297. dev_err(crtc->dev->dev, "%s: failed to set mode on plane: %d\n",
  298. mdp4_crtc->name, ret);
  299. return ret;
  300. }
  301. mdp4_write(mdp4_kms, REG_MDP4_DMA_SRC_SIZE(dma),
  302. MDP4_DMA_SRC_SIZE_WIDTH(mode->hdisplay) |
  303. MDP4_DMA_SRC_SIZE_HEIGHT(mode->vdisplay));
  304. /* take data from pipe: */
  305. mdp4_write(mdp4_kms, REG_MDP4_DMA_SRC_BASE(dma), 0);
  306. mdp4_write(mdp4_kms, REG_MDP4_DMA_SRC_STRIDE(dma),
  307. crtc->primary->fb->pitches[0]);
  308. mdp4_write(mdp4_kms, REG_MDP4_DMA_DST_SIZE(dma),
  309. MDP4_DMA_DST_SIZE_WIDTH(0) |
  310. MDP4_DMA_DST_SIZE_HEIGHT(0));
  311. mdp4_write(mdp4_kms, REG_MDP4_OVLP_BASE(ovlp), 0);
  312. mdp4_write(mdp4_kms, REG_MDP4_OVLP_SIZE(ovlp),
  313. MDP4_OVLP_SIZE_WIDTH(mode->hdisplay) |
  314. MDP4_OVLP_SIZE_HEIGHT(mode->vdisplay));
  315. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STRIDE(ovlp),
  316. crtc->primary->fb->pitches[0]);
  317. mdp4_write(mdp4_kms, REG_MDP4_OVLP_CFG(ovlp), 1);
  318. if (dma == DMA_E) {
  319. mdp4_write(mdp4_kms, REG_MDP4_DMA_E_QUANT(0), 0x00ff0000);
  320. mdp4_write(mdp4_kms, REG_MDP4_DMA_E_QUANT(1), 0x00ff0000);
  321. mdp4_write(mdp4_kms, REG_MDP4_DMA_E_QUANT(2), 0x00ff0000);
  322. }
  323. update_fb(crtc, crtc->primary->fb);
  324. update_scanout(crtc, crtc->primary->fb);
  325. return 0;
  326. }
  327. static void mdp4_crtc_prepare(struct drm_crtc *crtc)
  328. {
  329. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  330. DBG("%s", mdp4_crtc->name);
  331. /* make sure we hold a ref to mdp clks while setting up mode: */
  332. mdp4_enable(get_kms(crtc));
  333. mdp4_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  334. }
  335. static void mdp4_crtc_commit(struct drm_crtc *crtc)
  336. {
  337. mdp4_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  338. crtc_flush(crtc);
  339. /* drop the ref to mdp clk's that we got in prepare: */
  340. mdp4_disable(get_kms(crtc));
  341. }
  342. static int mdp4_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  343. struct drm_framebuffer *old_fb)
  344. {
  345. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  346. struct drm_plane *plane = mdp4_crtc->plane;
  347. struct drm_display_mode *mode = &crtc->mode;
  348. int ret;
  349. /* grab extra ref for update_scanout() */
  350. drm_framebuffer_reference(crtc->primary->fb);
  351. ret = mdp4_plane_mode_set(plane, crtc, crtc->primary->fb,
  352. 0, 0, mode->hdisplay, mode->vdisplay,
  353. x << 16, y << 16,
  354. mode->hdisplay << 16, mode->vdisplay << 16);
  355. if (ret) {
  356. drm_framebuffer_unreference(crtc->primary->fb);
  357. return ret;
  358. }
  359. update_fb(crtc, crtc->primary->fb);
  360. update_scanout(crtc, crtc->primary->fb);
  361. return 0;
  362. }
  363. static void mdp4_crtc_load_lut(struct drm_crtc *crtc)
  364. {
  365. }
  366. static int mdp4_crtc_page_flip(struct drm_crtc *crtc,
  367. struct drm_framebuffer *new_fb,
  368. struct drm_pending_vblank_event *event,
  369. uint32_t page_flip_flags)
  370. {
  371. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  372. struct drm_device *dev = crtc->dev;
  373. struct drm_gem_object *obj;
  374. unsigned long flags;
  375. if (mdp4_crtc->event) {
  376. dev_err(dev->dev, "already pending flip!\n");
  377. return -EBUSY;
  378. }
  379. obj = msm_framebuffer_bo(new_fb, 0);
  380. spin_lock_irqsave(&dev->event_lock, flags);
  381. mdp4_crtc->event = event;
  382. spin_unlock_irqrestore(&dev->event_lock, flags);
  383. update_fb(crtc, new_fb);
  384. return msm_gem_queue_inactive_cb(obj, &mdp4_crtc->pageflip_cb);
  385. }
  386. static int mdp4_crtc_set_property(struct drm_crtc *crtc,
  387. struct drm_property *property, uint64_t val)
  388. {
  389. // XXX
  390. return -EINVAL;
  391. }
  392. #define CURSOR_WIDTH 64
  393. #define CURSOR_HEIGHT 64
  394. /* called from IRQ to update cursor related registers (if needed). The
  395. * cursor registers, other than x/y position, appear not to be double
  396. * buffered, and changing them other than from vblank seems to trigger
  397. * underflow.
  398. */
  399. static void update_cursor(struct drm_crtc *crtc)
  400. {
  401. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  402. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  403. enum mdp4_dma dma = mdp4_crtc->dma;
  404. unsigned long flags;
  405. spin_lock_irqsave(&mdp4_crtc->cursor.lock, flags);
  406. if (mdp4_crtc->cursor.stale) {
  407. struct drm_gem_object *next_bo = mdp4_crtc->cursor.next_bo;
  408. struct drm_gem_object *prev_bo = mdp4_crtc->cursor.scanout_bo;
  409. uint32_t iova = mdp4_crtc->cursor.next_iova;
  410. if (next_bo) {
  411. /* take a obj ref + iova ref when we start scanning out: */
  412. drm_gem_object_reference(next_bo);
  413. msm_gem_get_iova_locked(next_bo, mdp4_kms->id, &iova);
  414. /* enable cursor: */
  415. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_SIZE(dma),
  416. MDP4_DMA_CURSOR_SIZE_WIDTH(mdp4_crtc->cursor.width) |
  417. MDP4_DMA_CURSOR_SIZE_HEIGHT(mdp4_crtc->cursor.height));
  418. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_BASE(dma), iova);
  419. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_BLEND_CONFIG(dma),
  420. MDP4_DMA_CURSOR_BLEND_CONFIG_FORMAT(CURSOR_ARGB) |
  421. MDP4_DMA_CURSOR_BLEND_CONFIG_CURSOR_EN);
  422. } else {
  423. /* disable cursor: */
  424. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_BASE(dma),
  425. mdp4_kms->blank_cursor_iova);
  426. }
  427. /* and drop the iova ref + obj rev when done scanning out: */
  428. if (prev_bo)
  429. drm_flip_work_queue(&mdp4_crtc->unref_cursor_work, prev_bo);
  430. mdp4_crtc->cursor.scanout_bo = next_bo;
  431. mdp4_crtc->cursor.stale = false;
  432. }
  433. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_POS(dma),
  434. MDP4_DMA_CURSOR_POS_X(mdp4_crtc->cursor.x) |
  435. MDP4_DMA_CURSOR_POS_Y(mdp4_crtc->cursor.y));
  436. spin_unlock_irqrestore(&mdp4_crtc->cursor.lock, flags);
  437. }
  438. static int mdp4_crtc_cursor_set(struct drm_crtc *crtc,
  439. struct drm_file *file_priv, uint32_t handle,
  440. uint32_t width, uint32_t height)
  441. {
  442. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  443. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  444. struct drm_device *dev = crtc->dev;
  445. struct drm_gem_object *cursor_bo, *old_bo;
  446. unsigned long flags;
  447. uint32_t iova;
  448. int ret;
  449. if ((width > CURSOR_WIDTH) || (height > CURSOR_HEIGHT)) {
  450. dev_err(dev->dev, "bad cursor size: %dx%d\n", width, height);
  451. return -EINVAL;
  452. }
  453. if (handle) {
  454. cursor_bo = drm_gem_object_lookup(dev, file_priv, handle);
  455. if (!cursor_bo)
  456. return -ENOENT;
  457. } else {
  458. cursor_bo = NULL;
  459. }
  460. if (cursor_bo) {
  461. ret = msm_gem_get_iova(cursor_bo, mdp4_kms->id, &iova);
  462. if (ret)
  463. goto fail;
  464. } else {
  465. iova = 0;
  466. }
  467. spin_lock_irqsave(&mdp4_crtc->cursor.lock, flags);
  468. old_bo = mdp4_crtc->cursor.next_bo;
  469. mdp4_crtc->cursor.next_bo = cursor_bo;
  470. mdp4_crtc->cursor.next_iova = iova;
  471. mdp4_crtc->cursor.width = width;
  472. mdp4_crtc->cursor.height = height;
  473. mdp4_crtc->cursor.stale = true;
  474. spin_unlock_irqrestore(&mdp4_crtc->cursor.lock, flags);
  475. if (old_bo) {
  476. /* drop our previous reference: */
  477. drm_flip_work_queue(&mdp4_crtc->unref_cursor_work, old_bo);
  478. }
  479. request_pending(crtc, PENDING_CURSOR);
  480. return 0;
  481. fail:
  482. drm_gem_object_unreference_unlocked(cursor_bo);
  483. return ret;
  484. }
  485. static int mdp4_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  486. {
  487. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  488. unsigned long flags;
  489. spin_lock_irqsave(&mdp4_crtc->cursor.lock, flags);
  490. mdp4_crtc->cursor.x = x;
  491. mdp4_crtc->cursor.y = y;
  492. spin_unlock_irqrestore(&mdp4_crtc->cursor.lock, flags);
  493. crtc_flush(crtc);
  494. request_pending(crtc, PENDING_CURSOR);
  495. return 0;
  496. }
  497. static const struct drm_crtc_funcs mdp4_crtc_funcs = {
  498. .set_config = drm_crtc_helper_set_config,
  499. .destroy = mdp4_crtc_destroy,
  500. .page_flip = mdp4_crtc_page_flip,
  501. .set_property = mdp4_crtc_set_property,
  502. .cursor_set = mdp4_crtc_cursor_set,
  503. .cursor_move = mdp4_crtc_cursor_move,
  504. };
  505. static const struct drm_crtc_helper_funcs mdp4_crtc_helper_funcs = {
  506. .dpms = mdp4_crtc_dpms,
  507. .mode_fixup = mdp4_crtc_mode_fixup,
  508. .mode_set = mdp4_crtc_mode_set,
  509. .prepare = mdp4_crtc_prepare,
  510. .commit = mdp4_crtc_commit,
  511. .mode_set_base = mdp4_crtc_mode_set_base,
  512. .load_lut = mdp4_crtc_load_lut,
  513. };
  514. static void mdp4_crtc_vblank_irq(struct mdp_irq *irq, uint32_t irqstatus)
  515. {
  516. struct mdp4_crtc *mdp4_crtc = container_of(irq, struct mdp4_crtc, vblank);
  517. struct drm_crtc *crtc = &mdp4_crtc->base;
  518. struct msm_drm_private *priv = crtc->dev->dev_private;
  519. unsigned pending;
  520. mdp_irq_unregister(&get_kms(crtc)->base, &mdp4_crtc->vblank);
  521. pending = atomic_xchg(&mdp4_crtc->pending, 0);
  522. if (pending & PENDING_FLIP) {
  523. complete_flip(crtc, NULL);
  524. drm_flip_work_commit(&mdp4_crtc->unref_fb_work, priv->wq);
  525. }
  526. if (pending & PENDING_CURSOR) {
  527. update_cursor(crtc);
  528. drm_flip_work_commit(&mdp4_crtc->unref_cursor_work, priv->wq);
  529. }
  530. }
  531. static void mdp4_crtc_err_irq(struct mdp_irq *irq, uint32_t irqstatus)
  532. {
  533. struct mdp4_crtc *mdp4_crtc = container_of(irq, struct mdp4_crtc, err);
  534. struct drm_crtc *crtc = &mdp4_crtc->base;
  535. DBG("%s: error: %08x", mdp4_crtc->name, irqstatus);
  536. crtc_flush(crtc);
  537. }
  538. uint32_t mdp4_crtc_vblank(struct drm_crtc *crtc)
  539. {
  540. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  541. return mdp4_crtc->vblank.irqmask;
  542. }
  543. void mdp4_crtc_cancel_pending_flip(struct drm_crtc *crtc, struct drm_file *file)
  544. {
  545. DBG("cancel: %p", file);
  546. complete_flip(crtc, file);
  547. }
  548. /* set dma config, ie. the format the encoder wants. */
  549. void mdp4_crtc_set_config(struct drm_crtc *crtc, uint32_t config)
  550. {
  551. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  552. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  553. mdp4_write(mdp4_kms, REG_MDP4_DMA_CONFIG(mdp4_crtc->dma), config);
  554. }
  555. /* set interface for routing crtc->encoder: */
  556. void mdp4_crtc_set_intf(struct drm_crtc *crtc, enum mdp4_intf intf)
  557. {
  558. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  559. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  560. uint32_t intf_sel;
  561. intf_sel = mdp4_read(mdp4_kms, REG_MDP4_DISP_INTF_SEL);
  562. switch (mdp4_crtc->dma) {
  563. case DMA_P:
  564. intf_sel &= ~MDP4_DISP_INTF_SEL_PRIM__MASK;
  565. intf_sel |= MDP4_DISP_INTF_SEL_PRIM(intf);
  566. break;
  567. case DMA_S:
  568. intf_sel &= ~MDP4_DISP_INTF_SEL_SEC__MASK;
  569. intf_sel |= MDP4_DISP_INTF_SEL_SEC(intf);
  570. break;
  571. case DMA_E:
  572. intf_sel &= ~MDP4_DISP_INTF_SEL_EXT__MASK;
  573. intf_sel |= MDP4_DISP_INTF_SEL_EXT(intf);
  574. break;
  575. }
  576. if (intf == INTF_DSI_VIDEO) {
  577. intf_sel &= ~MDP4_DISP_INTF_SEL_DSI_CMD;
  578. intf_sel |= MDP4_DISP_INTF_SEL_DSI_VIDEO;
  579. mdp4_crtc->mixer = 0;
  580. } else if (intf == INTF_DSI_CMD) {
  581. intf_sel &= ~MDP4_DISP_INTF_SEL_DSI_VIDEO;
  582. intf_sel |= MDP4_DISP_INTF_SEL_DSI_CMD;
  583. mdp4_crtc->mixer = 0;
  584. } else if (intf == INTF_LCDC_DTV){
  585. mdp4_crtc->mixer = 1;
  586. }
  587. blend_setup(crtc);
  588. DBG("%s: intf_sel=%08x", mdp4_crtc->name, intf_sel);
  589. mdp4_write(mdp4_kms, REG_MDP4_DISP_INTF_SEL, intf_sel);
  590. }
  591. static void set_attach(struct drm_crtc *crtc, enum mdp4_pipe pipe_id,
  592. struct drm_plane *plane)
  593. {
  594. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  595. BUG_ON(pipe_id >= ARRAY_SIZE(mdp4_crtc->planes));
  596. if (mdp4_crtc->planes[pipe_id] == plane)
  597. return;
  598. mdp4_crtc->planes[pipe_id] = plane;
  599. blend_setup(crtc);
  600. if (mdp4_crtc->enabled && (plane != mdp4_crtc->plane))
  601. crtc_flush(crtc);
  602. }
  603. void mdp4_crtc_attach(struct drm_crtc *crtc, struct drm_plane *plane)
  604. {
  605. set_attach(crtc, mdp4_plane_pipe(plane), plane);
  606. }
  607. void mdp4_crtc_detach(struct drm_crtc *crtc, struct drm_plane *plane)
  608. {
  609. /* don't actually detatch our primary plane: */
  610. if (to_mdp4_crtc(crtc)->plane == plane)
  611. return;
  612. set_attach(crtc, mdp4_plane_pipe(plane), NULL);
  613. }
  614. static const char *dma_names[] = {
  615. "DMA_P", "DMA_S", "DMA_E",
  616. };
  617. /* initialize crtc */
  618. struct drm_crtc *mdp4_crtc_init(struct drm_device *dev,
  619. struct drm_plane *plane, int id, int ovlp_id,
  620. enum mdp4_dma dma_id)
  621. {
  622. struct drm_crtc *crtc = NULL;
  623. struct mdp4_crtc *mdp4_crtc;
  624. int ret;
  625. mdp4_crtc = kzalloc(sizeof(*mdp4_crtc), GFP_KERNEL);
  626. if (!mdp4_crtc) {
  627. ret = -ENOMEM;
  628. goto fail;
  629. }
  630. crtc = &mdp4_crtc->base;
  631. mdp4_crtc->plane = plane;
  632. mdp4_crtc->id = id;
  633. mdp4_crtc->ovlp = ovlp_id;
  634. mdp4_crtc->dma = dma_id;
  635. mdp4_crtc->vblank.irqmask = dma2irq(mdp4_crtc->dma);
  636. mdp4_crtc->vblank.irq = mdp4_crtc_vblank_irq;
  637. mdp4_crtc->err.irqmask = dma2err(mdp4_crtc->dma);
  638. mdp4_crtc->err.irq = mdp4_crtc_err_irq;
  639. snprintf(mdp4_crtc->name, sizeof(mdp4_crtc->name), "%s:%d",
  640. dma_names[dma_id], ovlp_id);
  641. spin_lock_init(&mdp4_crtc->cursor.lock);
  642. ret = drm_flip_work_init(&mdp4_crtc->unref_fb_work, 16,
  643. "unref fb", unref_fb_worker);
  644. if (ret)
  645. goto fail;
  646. ret = drm_flip_work_init(&mdp4_crtc->unref_cursor_work, 64,
  647. "unref cursor", unref_cursor_worker);
  648. INIT_FENCE_CB(&mdp4_crtc->pageflip_cb, pageflip_cb);
  649. drm_crtc_init_with_planes(dev, crtc, plane, NULL, &mdp4_crtc_funcs);
  650. drm_crtc_helper_add(crtc, &mdp4_crtc_helper_funcs);
  651. mdp4_plane_install_properties(mdp4_crtc->plane, &crtc->base);
  652. return crtc;
  653. fail:
  654. if (crtc)
  655. mdp4_crtc_destroy(crtc);
  656. return ERR_PTR(ret);
  657. }