mdp4_crtc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. /* next cursor to scan-out: */
  38. uint32_t next_iova;
  39. struct drm_gem_object *next_bo;
  40. /* current cursor being scanned out: */
  41. struct drm_gem_object *scanout_bo;
  42. } cursor;
  43. /* if there is a pending flip, these will be non-null: */
  44. struct drm_pending_vblank_event *event;
  45. struct msm_fence_cb pageflip_cb;
  46. /* the fb that we currently hold a scanout ref to: */
  47. struct drm_framebuffer *fb;
  48. /* for unref'ing framebuffers after scanout completes: */
  49. struct drm_flip_work unref_fb_work;
  50. /* for unref'ing cursor bo's after scanout completes: */
  51. struct drm_flip_work unref_cursor_work;
  52. struct mdp4_irq vblank;
  53. struct mdp4_irq err;
  54. };
  55. #define to_mdp4_crtc(x) container_of(x, struct mdp4_crtc, base)
  56. static struct mdp4_kms *get_kms(struct drm_crtc *crtc)
  57. {
  58. struct msm_drm_private *priv = crtc->dev->dev_private;
  59. return to_mdp4_kms(priv->kms);
  60. }
  61. static void update_fb(struct drm_crtc *crtc, bool async,
  62. struct drm_framebuffer *new_fb)
  63. {
  64. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  65. struct drm_framebuffer *old_fb = mdp4_crtc->fb;
  66. if (old_fb)
  67. drm_flip_work_queue(&mdp4_crtc->unref_fb_work, old_fb);
  68. /* grab reference to incoming scanout fb: */
  69. drm_framebuffer_reference(new_fb);
  70. mdp4_crtc->base.fb = new_fb;
  71. mdp4_crtc->fb = new_fb;
  72. if (!async) {
  73. /* enable vblank to pick up the old_fb */
  74. mdp4_irq_register(get_kms(crtc), &mdp4_crtc->vblank);
  75. }
  76. }
  77. static void complete_flip(struct drm_crtc *crtc, bool canceled)
  78. {
  79. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  80. struct drm_device *dev = crtc->dev;
  81. struct drm_pending_vblank_event *event;
  82. unsigned long flags;
  83. spin_lock_irqsave(&dev->event_lock, flags);
  84. event = mdp4_crtc->event;
  85. if (event) {
  86. mdp4_crtc->event = NULL;
  87. if (canceled)
  88. event->base.destroy(&event->base);
  89. else
  90. drm_send_vblank_event(dev, mdp4_crtc->id, event);
  91. }
  92. spin_unlock_irqrestore(&dev->event_lock, flags);
  93. }
  94. static void crtc_flush(struct drm_crtc *crtc)
  95. {
  96. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  97. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  98. uint32_t i, flush = 0;
  99. for (i = 0; i < ARRAY_SIZE(mdp4_crtc->planes); i++) {
  100. struct drm_plane *plane = mdp4_crtc->planes[i];
  101. if (plane) {
  102. enum mdp4_pipe pipe_id = mdp4_plane_pipe(plane);
  103. flush |= pipe2flush(pipe_id);
  104. }
  105. }
  106. flush |= ovlp2flush(mdp4_crtc->ovlp);
  107. DBG("%s: flush=%08x", mdp4_crtc->name, flush);
  108. mdp4_write(mdp4_kms, REG_MDP4_OVERLAY_FLUSH, flush);
  109. }
  110. static void pageflip_cb(struct msm_fence_cb *cb)
  111. {
  112. struct mdp4_crtc *mdp4_crtc =
  113. container_of(cb, struct mdp4_crtc, pageflip_cb);
  114. struct drm_crtc *crtc = &mdp4_crtc->base;
  115. mdp4_plane_set_scanout(mdp4_crtc->plane, crtc->fb);
  116. crtc_flush(crtc);
  117. /* enable vblank to complete flip: */
  118. mdp4_irq_register(get_kms(crtc), &mdp4_crtc->vblank);
  119. }
  120. static void unref_fb_worker(struct drm_flip_work *work, void *val)
  121. {
  122. struct mdp4_crtc *mdp4_crtc =
  123. container_of(work, struct mdp4_crtc, unref_fb_work);
  124. struct drm_device *dev = mdp4_crtc->base.dev;
  125. mutex_lock(&dev->mode_config.mutex);
  126. drm_framebuffer_unreference(val);
  127. mutex_unlock(&dev->mode_config.mutex);
  128. }
  129. static void unref_cursor_worker(struct drm_flip_work *work, void *val)
  130. {
  131. struct mdp4_crtc *mdp4_crtc =
  132. container_of(work, struct mdp4_crtc, unref_cursor_work);
  133. struct mdp4_kms *mdp4_kms = get_kms(&mdp4_crtc->base);
  134. msm_gem_put_iova(val, mdp4_kms->id);
  135. drm_gem_object_unreference_unlocked(val);
  136. }
  137. static void mdp4_crtc_destroy(struct drm_crtc *crtc)
  138. {
  139. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  140. mdp4_crtc->plane->funcs->destroy(mdp4_crtc->plane);
  141. drm_crtc_cleanup(crtc);
  142. drm_flip_work_cleanup(&mdp4_crtc->unref_fb_work);
  143. drm_flip_work_cleanup(&mdp4_crtc->unref_cursor_work);
  144. kfree(mdp4_crtc);
  145. }
  146. static void mdp4_crtc_dpms(struct drm_crtc *crtc, int mode)
  147. {
  148. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  149. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  150. bool enabled = (mode == DRM_MODE_DPMS_ON);
  151. DBG("%s: mode=%d", mdp4_crtc->name, mode);
  152. if (enabled != mdp4_crtc->enabled) {
  153. if (enabled) {
  154. mdp4_enable(mdp4_kms);
  155. mdp4_irq_register(mdp4_kms, &mdp4_crtc->err);
  156. } else {
  157. mdp4_irq_unregister(mdp4_kms, &mdp4_crtc->err);
  158. mdp4_disable(mdp4_kms);
  159. }
  160. mdp4_crtc->enabled = enabled;
  161. }
  162. }
  163. static bool mdp4_crtc_mode_fixup(struct drm_crtc *crtc,
  164. const struct drm_display_mode *mode,
  165. struct drm_display_mode *adjusted_mode)
  166. {
  167. return true;
  168. }
  169. static void blend_setup(struct drm_crtc *crtc)
  170. {
  171. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  172. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  173. int i, ovlp = mdp4_crtc->ovlp;
  174. uint32_t mixer_cfg = 0;
  175. static const enum mdp4_mixer_stage_id stages[] = {
  176. STAGE_BASE, STAGE0, STAGE1, STAGE2, STAGE3,
  177. };
  178. /* statically (for now) map planes to mixer stage (z-order): */
  179. static const int idxs[] = {
  180. [VG1] = 1,
  181. [VG2] = 2,
  182. [RGB1] = 0,
  183. [RGB2] = 0,
  184. [RGB3] = 0,
  185. [VG3] = 3,
  186. [VG4] = 4,
  187. };
  188. bool alpha[4]= { false, false, false, false };
  189. mdp4_write(mdp4_kms, REG_MDP4_OVLP_TRANSP_LOW0(ovlp), 0);
  190. mdp4_write(mdp4_kms, REG_MDP4_OVLP_TRANSP_LOW1(ovlp), 0);
  191. mdp4_write(mdp4_kms, REG_MDP4_OVLP_TRANSP_HIGH0(ovlp), 0);
  192. mdp4_write(mdp4_kms, REG_MDP4_OVLP_TRANSP_HIGH1(ovlp), 0);
  193. /* TODO single register for all CRTCs, so this won't work properly
  194. * when multiple CRTCs are active..
  195. */
  196. for (i = 0; i < ARRAY_SIZE(mdp4_crtc->planes); i++) {
  197. struct drm_plane *plane = mdp4_crtc->planes[i];
  198. if (plane) {
  199. enum mdp4_pipe pipe_id = mdp4_plane_pipe(plane);
  200. int idx = idxs[pipe_id];
  201. if (idx > 0) {
  202. const struct mdp4_format *format =
  203. to_mdp4_format(msm_framebuffer_format(plane->fb));
  204. alpha[idx-1] = format->alpha_enable;
  205. }
  206. mixer_cfg |= mixercfg(mdp4_crtc->mixer, pipe_id, stages[idx]);
  207. }
  208. }
  209. /* this shouldn't happen.. and seems to cause underflow: */
  210. WARN_ON(!mixer_cfg);
  211. for (i = 0; i < 4; i++) {
  212. uint32_t op;
  213. if (alpha[i]) {
  214. op = MDP4_OVLP_STAGE_OP_FG_ALPHA(FG_PIXEL) |
  215. MDP4_OVLP_STAGE_OP_BG_ALPHA(FG_PIXEL) |
  216. MDP4_OVLP_STAGE_OP_BG_INV_ALPHA;
  217. } else {
  218. op = MDP4_OVLP_STAGE_OP_FG_ALPHA(FG_CONST) |
  219. MDP4_OVLP_STAGE_OP_BG_ALPHA(BG_CONST);
  220. }
  221. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_FG_ALPHA(ovlp, i), 0xff);
  222. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_BG_ALPHA(ovlp, i), 0x00);
  223. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_OP(ovlp, i), op);
  224. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_CO3(ovlp, i), 1);
  225. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_TRANSP_LOW0(ovlp, i), 0);
  226. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_TRANSP_LOW1(ovlp, i), 0);
  227. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_TRANSP_HIGH0(ovlp, i), 0);
  228. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STAGE_TRANSP_HIGH1(ovlp, i), 0);
  229. }
  230. mdp4_write(mdp4_kms, REG_MDP4_LAYERMIXER_IN_CFG, mixer_cfg);
  231. }
  232. static int mdp4_crtc_mode_set(struct drm_crtc *crtc,
  233. struct drm_display_mode *mode,
  234. struct drm_display_mode *adjusted_mode,
  235. int x, int y,
  236. struct drm_framebuffer *old_fb)
  237. {
  238. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  239. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  240. enum mdp4_dma dma = mdp4_crtc->dma;
  241. int ret, ovlp = mdp4_crtc->ovlp;
  242. mode = adjusted_mode;
  243. DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  244. mdp4_crtc->name, mode->base.id, mode->name,
  245. mode->vrefresh, mode->clock,
  246. mode->hdisplay, mode->hsync_start,
  247. mode->hsync_end, mode->htotal,
  248. mode->vdisplay, mode->vsync_start,
  249. mode->vsync_end, mode->vtotal,
  250. mode->type, mode->flags);
  251. mdp4_write(mdp4_kms, REG_MDP4_DMA_SRC_SIZE(dma),
  252. MDP4_DMA_SRC_SIZE_WIDTH(mode->hdisplay) |
  253. MDP4_DMA_SRC_SIZE_HEIGHT(mode->vdisplay));
  254. /* take data from pipe: */
  255. mdp4_write(mdp4_kms, REG_MDP4_DMA_SRC_BASE(dma), 0);
  256. mdp4_write(mdp4_kms, REG_MDP4_DMA_SRC_STRIDE(dma),
  257. crtc->fb->pitches[0]);
  258. mdp4_write(mdp4_kms, REG_MDP4_DMA_DST_SIZE(dma),
  259. MDP4_DMA_DST_SIZE_WIDTH(0) |
  260. MDP4_DMA_DST_SIZE_HEIGHT(0));
  261. mdp4_write(mdp4_kms, REG_MDP4_OVLP_BASE(ovlp), 0);
  262. mdp4_write(mdp4_kms, REG_MDP4_OVLP_SIZE(ovlp),
  263. MDP4_OVLP_SIZE_WIDTH(mode->hdisplay) |
  264. MDP4_OVLP_SIZE_HEIGHT(mode->vdisplay));
  265. mdp4_write(mdp4_kms, REG_MDP4_OVLP_STRIDE(ovlp),
  266. crtc->fb->pitches[0]);
  267. mdp4_write(mdp4_kms, REG_MDP4_OVLP_CFG(ovlp), 1);
  268. update_fb(crtc, false, crtc->fb);
  269. ret = mdp4_plane_mode_set(mdp4_crtc->plane, crtc, crtc->fb,
  270. 0, 0, mode->hdisplay, mode->vdisplay,
  271. x << 16, y << 16,
  272. mode->hdisplay << 16, mode->vdisplay << 16);
  273. if (ret) {
  274. dev_err(crtc->dev->dev, "%s: failed to set mode on plane: %d\n",
  275. mdp4_crtc->name, ret);
  276. return ret;
  277. }
  278. if (dma == DMA_E) {
  279. mdp4_write(mdp4_kms, REG_MDP4_DMA_E_QUANT(0), 0x00ff0000);
  280. mdp4_write(mdp4_kms, REG_MDP4_DMA_E_QUANT(1), 0x00ff0000);
  281. mdp4_write(mdp4_kms, REG_MDP4_DMA_E_QUANT(2), 0x00ff0000);
  282. }
  283. return 0;
  284. }
  285. static void mdp4_crtc_prepare(struct drm_crtc *crtc)
  286. {
  287. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  288. DBG("%s", mdp4_crtc->name);
  289. /* make sure we hold a ref to mdp clks while setting up mode: */
  290. mdp4_enable(get_kms(crtc));
  291. mdp4_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  292. }
  293. static void mdp4_crtc_commit(struct drm_crtc *crtc)
  294. {
  295. mdp4_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  296. crtc_flush(crtc);
  297. /* drop the ref to mdp clk's that we got in prepare: */
  298. mdp4_disable(get_kms(crtc));
  299. }
  300. static int mdp4_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  301. struct drm_framebuffer *old_fb)
  302. {
  303. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  304. struct drm_plane *plane = mdp4_crtc->plane;
  305. struct drm_display_mode *mode = &crtc->mode;
  306. update_fb(crtc, false, crtc->fb);
  307. return mdp4_plane_mode_set(plane, crtc, crtc->fb,
  308. 0, 0, mode->hdisplay, mode->vdisplay,
  309. x << 16, y << 16,
  310. mode->hdisplay << 16, mode->vdisplay << 16);
  311. }
  312. static void mdp4_crtc_load_lut(struct drm_crtc *crtc)
  313. {
  314. }
  315. static int mdp4_crtc_page_flip(struct drm_crtc *crtc,
  316. struct drm_framebuffer *new_fb,
  317. struct drm_pending_vblank_event *event,
  318. uint32_t page_flip_flags)
  319. {
  320. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  321. struct drm_device *dev = crtc->dev;
  322. struct drm_gem_object *obj;
  323. if (mdp4_crtc->event) {
  324. dev_err(dev->dev, "already pending flip!\n");
  325. return -EBUSY;
  326. }
  327. obj = msm_framebuffer_bo(new_fb, 0);
  328. mdp4_crtc->event = event;
  329. update_fb(crtc, true, new_fb);
  330. return msm_gem_queue_inactive_cb(obj, &mdp4_crtc->pageflip_cb);
  331. }
  332. static int mdp4_crtc_set_property(struct drm_crtc *crtc,
  333. struct drm_property *property, uint64_t val)
  334. {
  335. // XXX
  336. return -EINVAL;
  337. }
  338. #define CURSOR_WIDTH 64
  339. #define CURSOR_HEIGHT 64
  340. /* called from IRQ to update cursor related registers (if needed). The
  341. * cursor registers, other than x/y position, appear not to be double
  342. * buffered, and changing them other than from vblank seems to trigger
  343. * underflow.
  344. */
  345. static void update_cursor(struct drm_crtc *crtc)
  346. {
  347. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  348. enum mdp4_dma dma = mdp4_crtc->dma;
  349. unsigned long flags;
  350. spin_lock_irqsave(&mdp4_crtc->cursor.lock, flags);
  351. if (mdp4_crtc->cursor.stale) {
  352. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  353. struct drm_gem_object *next_bo = mdp4_crtc->cursor.next_bo;
  354. struct drm_gem_object *prev_bo = mdp4_crtc->cursor.scanout_bo;
  355. uint32_t iova = mdp4_crtc->cursor.next_iova;
  356. if (next_bo) {
  357. /* take a obj ref + iova ref when we start scanning out: */
  358. drm_gem_object_reference(next_bo);
  359. msm_gem_get_iova_locked(next_bo, mdp4_kms->id, &iova);
  360. /* enable cursor: */
  361. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_SIZE(dma),
  362. MDP4_DMA_CURSOR_SIZE_WIDTH(mdp4_crtc->cursor.width) |
  363. MDP4_DMA_CURSOR_SIZE_HEIGHT(mdp4_crtc->cursor.height));
  364. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_BASE(dma), iova);
  365. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_BLEND_CONFIG(dma),
  366. MDP4_DMA_CURSOR_BLEND_CONFIG_FORMAT(CURSOR_ARGB) |
  367. MDP4_DMA_CURSOR_BLEND_CONFIG_CURSOR_EN);
  368. } else {
  369. /* disable cursor: */
  370. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_BASE(dma), 0);
  371. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_BLEND_CONFIG(dma),
  372. MDP4_DMA_CURSOR_BLEND_CONFIG_FORMAT(CURSOR_ARGB));
  373. }
  374. /* and drop the iova ref + obj rev when done scanning out: */
  375. if (prev_bo)
  376. drm_flip_work_queue(&mdp4_crtc->unref_cursor_work, prev_bo);
  377. mdp4_crtc->cursor.scanout_bo = next_bo;
  378. mdp4_crtc->cursor.stale = false;
  379. }
  380. spin_unlock_irqrestore(&mdp4_crtc->cursor.lock, flags);
  381. }
  382. static int mdp4_crtc_cursor_set(struct drm_crtc *crtc,
  383. struct drm_file *file_priv, uint32_t handle,
  384. uint32_t width, uint32_t height)
  385. {
  386. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  387. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  388. struct drm_device *dev = crtc->dev;
  389. struct drm_gem_object *cursor_bo, *old_bo;
  390. unsigned long flags;
  391. uint32_t iova;
  392. int ret;
  393. if ((width > CURSOR_WIDTH) || (height > CURSOR_HEIGHT)) {
  394. dev_err(dev->dev, "bad cursor size: %dx%d\n", width, height);
  395. return -EINVAL;
  396. }
  397. if (handle) {
  398. cursor_bo = drm_gem_object_lookup(dev, file_priv, handle);
  399. if (!cursor_bo)
  400. return -ENOENT;
  401. } else {
  402. cursor_bo = NULL;
  403. }
  404. if (cursor_bo) {
  405. ret = msm_gem_get_iova(cursor_bo, mdp4_kms->id, &iova);
  406. if (ret)
  407. goto fail;
  408. } else {
  409. iova = 0;
  410. }
  411. spin_lock_irqsave(&mdp4_crtc->cursor.lock, flags);
  412. old_bo = mdp4_crtc->cursor.next_bo;
  413. mdp4_crtc->cursor.next_bo = cursor_bo;
  414. mdp4_crtc->cursor.next_iova = iova;
  415. mdp4_crtc->cursor.width = width;
  416. mdp4_crtc->cursor.height = height;
  417. mdp4_crtc->cursor.stale = true;
  418. spin_unlock_irqrestore(&mdp4_crtc->cursor.lock, flags);
  419. if (old_bo) {
  420. /* drop our previous reference: */
  421. msm_gem_put_iova(old_bo, mdp4_kms->id);
  422. drm_gem_object_unreference_unlocked(old_bo);
  423. }
  424. return 0;
  425. fail:
  426. drm_gem_object_unreference_unlocked(cursor_bo);
  427. return ret;
  428. }
  429. static int mdp4_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  430. {
  431. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  432. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  433. enum mdp4_dma dma = mdp4_crtc->dma;
  434. mdp4_write(mdp4_kms, REG_MDP4_DMA_CURSOR_POS(dma),
  435. MDP4_DMA_CURSOR_POS_X(x) |
  436. MDP4_DMA_CURSOR_POS_Y(y));
  437. return 0;
  438. }
  439. static const struct drm_crtc_funcs mdp4_crtc_funcs = {
  440. .set_config = drm_crtc_helper_set_config,
  441. .destroy = mdp4_crtc_destroy,
  442. .page_flip = mdp4_crtc_page_flip,
  443. .set_property = mdp4_crtc_set_property,
  444. .cursor_set = mdp4_crtc_cursor_set,
  445. .cursor_move = mdp4_crtc_cursor_move,
  446. };
  447. static const struct drm_crtc_helper_funcs mdp4_crtc_helper_funcs = {
  448. .dpms = mdp4_crtc_dpms,
  449. .mode_fixup = mdp4_crtc_mode_fixup,
  450. .mode_set = mdp4_crtc_mode_set,
  451. .prepare = mdp4_crtc_prepare,
  452. .commit = mdp4_crtc_commit,
  453. .mode_set_base = mdp4_crtc_mode_set_base,
  454. .load_lut = mdp4_crtc_load_lut,
  455. };
  456. static void mdp4_crtc_vblank_irq(struct mdp4_irq *irq, uint32_t irqstatus)
  457. {
  458. struct mdp4_crtc *mdp4_crtc = container_of(irq, struct mdp4_crtc, vblank);
  459. struct drm_crtc *crtc = &mdp4_crtc->base;
  460. struct msm_drm_private *priv = crtc->dev->dev_private;
  461. update_cursor(crtc);
  462. complete_flip(crtc, false);
  463. mdp4_irq_unregister(get_kms(crtc), &mdp4_crtc->vblank);
  464. drm_flip_work_commit(&mdp4_crtc->unref_fb_work, priv->wq);
  465. drm_flip_work_commit(&mdp4_crtc->unref_cursor_work, priv->wq);
  466. }
  467. static void mdp4_crtc_err_irq(struct mdp4_irq *irq, uint32_t irqstatus)
  468. {
  469. struct mdp4_crtc *mdp4_crtc = container_of(irq, struct mdp4_crtc, err);
  470. struct drm_crtc *crtc = &mdp4_crtc->base;
  471. DBG("%s: error: %08x", mdp4_crtc->name, irqstatus);
  472. crtc_flush(crtc);
  473. }
  474. uint32_t mdp4_crtc_vblank(struct drm_crtc *crtc)
  475. {
  476. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  477. return mdp4_crtc->vblank.irqmask;
  478. }
  479. void mdp4_crtc_cancel_pending_flip(struct drm_crtc *crtc)
  480. {
  481. complete_flip(crtc, true);
  482. }
  483. /* set dma config, ie. the format the encoder wants. */
  484. void mdp4_crtc_set_config(struct drm_crtc *crtc, uint32_t config)
  485. {
  486. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  487. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  488. mdp4_write(mdp4_kms, REG_MDP4_DMA_CONFIG(mdp4_crtc->dma), config);
  489. }
  490. /* set interface for routing crtc->encoder: */
  491. void mdp4_crtc_set_intf(struct drm_crtc *crtc, enum mdp4_intf intf)
  492. {
  493. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  494. struct mdp4_kms *mdp4_kms = get_kms(crtc);
  495. uint32_t intf_sel;
  496. intf_sel = mdp4_read(mdp4_kms, REG_MDP4_DISP_INTF_SEL);
  497. switch (mdp4_crtc->dma) {
  498. case DMA_P:
  499. intf_sel &= ~MDP4_DISP_INTF_SEL_PRIM__MASK;
  500. intf_sel |= MDP4_DISP_INTF_SEL_PRIM(intf);
  501. break;
  502. case DMA_S:
  503. intf_sel &= ~MDP4_DISP_INTF_SEL_SEC__MASK;
  504. intf_sel |= MDP4_DISP_INTF_SEL_SEC(intf);
  505. break;
  506. case DMA_E:
  507. intf_sel &= ~MDP4_DISP_INTF_SEL_EXT__MASK;
  508. intf_sel |= MDP4_DISP_INTF_SEL_EXT(intf);
  509. break;
  510. }
  511. if (intf == INTF_DSI_VIDEO) {
  512. intf_sel &= ~MDP4_DISP_INTF_SEL_DSI_CMD;
  513. intf_sel |= MDP4_DISP_INTF_SEL_DSI_VIDEO;
  514. mdp4_crtc->mixer = 0;
  515. } else if (intf == INTF_DSI_CMD) {
  516. intf_sel &= ~MDP4_DISP_INTF_SEL_DSI_VIDEO;
  517. intf_sel |= MDP4_DISP_INTF_SEL_DSI_CMD;
  518. mdp4_crtc->mixer = 0;
  519. } else if (intf == INTF_LCDC_DTV){
  520. mdp4_crtc->mixer = 1;
  521. }
  522. blend_setup(crtc);
  523. DBG("%s: intf_sel=%08x", mdp4_crtc->name, intf_sel);
  524. mdp4_write(mdp4_kms, REG_MDP4_DISP_INTF_SEL, intf_sel);
  525. }
  526. static void set_attach(struct drm_crtc *crtc, enum mdp4_pipe pipe_id,
  527. struct drm_plane *plane)
  528. {
  529. struct mdp4_crtc *mdp4_crtc = to_mdp4_crtc(crtc);
  530. BUG_ON(pipe_id >= ARRAY_SIZE(mdp4_crtc->planes));
  531. if (mdp4_crtc->planes[pipe_id] == plane)
  532. return;
  533. mdp4_crtc->planes[pipe_id] = plane;
  534. blend_setup(crtc);
  535. if (mdp4_crtc->enabled && (plane != mdp4_crtc->plane))
  536. crtc_flush(crtc);
  537. }
  538. void mdp4_crtc_attach(struct drm_crtc *crtc, struct drm_plane *plane)
  539. {
  540. set_attach(crtc, mdp4_plane_pipe(plane), plane);
  541. }
  542. void mdp4_crtc_detach(struct drm_crtc *crtc, struct drm_plane *plane)
  543. {
  544. set_attach(crtc, mdp4_plane_pipe(plane), NULL);
  545. }
  546. static const char *dma_names[] = {
  547. "DMA_P", "DMA_S", "DMA_E",
  548. };
  549. /* initialize crtc */
  550. struct drm_crtc *mdp4_crtc_init(struct drm_device *dev,
  551. struct drm_plane *plane, int id, int ovlp_id,
  552. enum mdp4_dma dma_id)
  553. {
  554. struct drm_crtc *crtc = NULL;
  555. struct mdp4_crtc *mdp4_crtc;
  556. int ret;
  557. mdp4_crtc = kzalloc(sizeof(*mdp4_crtc), GFP_KERNEL);
  558. if (!mdp4_crtc) {
  559. ret = -ENOMEM;
  560. goto fail;
  561. }
  562. crtc = &mdp4_crtc->base;
  563. mdp4_crtc->plane = plane;
  564. mdp4_crtc->ovlp = ovlp_id;
  565. mdp4_crtc->dma = dma_id;
  566. mdp4_crtc->vblank.irqmask = dma2irq(mdp4_crtc->dma);
  567. mdp4_crtc->vblank.irq = mdp4_crtc_vblank_irq;
  568. mdp4_crtc->err.irqmask = dma2err(mdp4_crtc->dma);
  569. mdp4_crtc->err.irq = mdp4_crtc_err_irq;
  570. snprintf(mdp4_crtc->name, sizeof(mdp4_crtc->name), "%s:%d",
  571. dma_names[dma_id], ovlp_id);
  572. spin_lock_init(&mdp4_crtc->cursor.lock);
  573. ret = drm_flip_work_init(&mdp4_crtc->unref_fb_work, 16,
  574. "unref fb", unref_fb_worker);
  575. if (ret)
  576. goto fail;
  577. ret = drm_flip_work_init(&mdp4_crtc->unref_cursor_work, 64,
  578. "unref cursor", unref_cursor_worker);
  579. INIT_FENCE_CB(&mdp4_crtc->pageflip_cb, pageflip_cb);
  580. drm_crtc_init(dev, crtc, &mdp4_crtc_funcs);
  581. drm_crtc_helper_add(crtc, &mdp4_crtc_helper_funcs);
  582. mdp4_plane_install_properties(mdp4_crtc->plane, &crtc->base);
  583. return crtc;
  584. fail:
  585. if (crtc)
  586. mdp4_crtc_destroy(crtc);
  587. return ERR_PTR(ret);
  588. }