omap_crtc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * drivers/gpu/drm/omapdrm/omap_crtc.c
  3. *
  4. * Copyright (C) 2011 Texas Instruments
  5. * Author: Rob Clark <rob@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published by
  9. * the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/completion.h>
  20. #include <drm/drm_crtc.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include <drm/drm_mode.h>
  23. #include <drm/drm_plane_helper.h>
  24. #include "omap_drv.h"
  25. #define to_omap_crtc(x) container_of(x, struct omap_crtc, base)
  26. enum omap_page_flip_state {
  27. OMAP_PAGE_FLIP_IDLE,
  28. OMAP_PAGE_FLIP_WAIT,
  29. OMAP_PAGE_FLIP_QUEUED,
  30. OMAP_PAGE_FLIP_CANCELLED,
  31. };
  32. struct omap_crtc {
  33. struct drm_crtc base;
  34. const char *name;
  35. int pipe;
  36. enum omap_channel channel;
  37. struct omap_overlay_manager_info info;
  38. struct drm_encoder *current_encoder;
  39. /*
  40. * Temporary: eventually this will go away, but it is needed
  41. * for now to keep the output's happy. (They only need
  42. * mgr->id.) Eventually this will be replaced w/ something
  43. * more common-panel-framework-y
  44. */
  45. struct omap_overlay_manager *mgr;
  46. struct omap_video_timings timings;
  47. bool enabled;
  48. struct omap_drm_irq vblank_irq;
  49. struct omap_drm_irq error_irq;
  50. /* list of framebuffers to unpin */
  51. struct list_head pending_unpins;
  52. /*
  53. * flip_state flag indicates the current page flap state: IDLE if no
  54. * page queue has been submitted, WAIT when waiting for GEM async
  55. * completion, QUEUED when the page flip has been queued to the hardware
  56. * or CANCELLED when the CRTC is turned off before the flip gets queued
  57. * to the hardware. The flip event, if any, is stored in flip_event, and
  58. * the framebuffer queued for page flip is stored in flip_fb. The
  59. * flip_wait wait queue is used to wait for page flip completion.
  60. *
  61. * The flip_work work queue handles page flip requests without caring
  62. * about what context the GEM async callback is called from. Possibly we
  63. * should just make omap_gem always call the cb from the worker so we
  64. * don't have to care about this.
  65. */
  66. enum omap_page_flip_state flip_state;
  67. struct drm_pending_vblank_event *flip_event;
  68. struct drm_framebuffer *flip_fb;
  69. wait_queue_head_t flip_wait;
  70. struct work_struct flip_work;
  71. struct completion completion;
  72. bool ignore_digit_sync_lost;
  73. };
  74. struct omap_framebuffer_unpin {
  75. struct list_head list;
  76. struct drm_framebuffer *fb;
  77. };
  78. /* -----------------------------------------------------------------------------
  79. * Helper Functions
  80. */
  81. uint32_t pipe2vbl(struct drm_crtc *crtc)
  82. {
  83. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  84. return dispc_mgr_get_vsync_irq(omap_crtc->channel);
  85. }
  86. const struct omap_video_timings *omap_crtc_timings(struct drm_crtc *crtc)
  87. {
  88. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  89. return &omap_crtc->timings;
  90. }
  91. enum omap_channel omap_crtc_channel(struct drm_crtc *crtc)
  92. {
  93. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  94. return omap_crtc->channel;
  95. }
  96. /* -----------------------------------------------------------------------------
  97. * DSS Manager Functions
  98. */
  99. /*
  100. * Manager-ops, callbacks from output when they need to configure
  101. * the upstream part of the video pipe.
  102. *
  103. * Most of these we can ignore until we add support for command-mode
  104. * panels.. for video-mode the crtc-helpers already do an adequate
  105. * job of sequencing the setup of the video pipe in the proper order
  106. */
  107. /* ovl-mgr-id -> crtc */
  108. static struct omap_crtc *omap_crtcs[8];
  109. /* we can probably ignore these until we support command-mode panels: */
  110. static int omap_crtc_connect(struct omap_overlay_manager *mgr,
  111. struct omap_dss_device *dst)
  112. {
  113. if (mgr->output)
  114. return -EINVAL;
  115. if ((mgr->supported_outputs & dst->id) == 0)
  116. return -EINVAL;
  117. dst->manager = mgr;
  118. mgr->output = dst;
  119. return 0;
  120. }
  121. static void omap_crtc_disconnect(struct omap_overlay_manager *mgr,
  122. struct omap_dss_device *dst)
  123. {
  124. mgr->output->manager = NULL;
  125. mgr->output = NULL;
  126. }
  127. static void omap_crtc_start_update(struct omap_overlay_manager *mgr)
  128. {
  129. }
  130. /* Called only from omap_crtc_setup and suspend/resume handlers. */
  131. static void omap_crtc_set_enabled(struct drm_crtc *crtc, bool enable)
  132. {
  133. struct drm_device *dev = crtc->dev;
  134. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  135. enum omap_channel channel = omap_crtc->channel;
  136. struct omap_irq_wait *wait;
  137. u32 framedone_irq, vsync_irq;
  138. int ret;
  139. if (dispc_mgr_is_enabled(channel) == enable)
  140. return;
  141. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  142. /*
  143. * Digit output produces some sync lost interrupts during the
  144. * first frame when enabling, so we need to ignore those.
  145. */
  146. omap_crtc->ignore_digit_sync_lost = true;
  147. }
  148. framedone_irq = dispc_mgr_get_framedone_irq(channel);
  149. vsync_irq = dispc_mgr_get_vsync_irq(channel);
  150. if (enable) {
  151. wait = omap_irq_wait_init(dev, vsync_irq, 1);
  152. } else {
  153. /*
  154. * When we disable the digit output, we need to wait for
  155. * FRAMEDONE to know that DISPC has finished with the output.
  156. *
  157. * OMAP2/3 does not have FRAMEDONE irq for digit output, and in
  158. * that case we need to use vsync interrupt, and wait for both
  159. * even and odd frames.
  160. */
  161. if (framedone_irq)
  162. wait = omap_irq_wait_init(dev, framedone_irq, 1);
  163. else
  164. wait = omap_irq_wait_init(dev, vsync_irq, 2);
  165. }
  166. dispc_mgr_enable(channel, enable);
  167. ret = omap_irq_wait(dev, wait, msecs_to_jiffies(100));
  168. if (ret) {
  169. dev_err(dev->dev, "%s: timeout waiting for %s\n",
  170. omap_crtc->name, enable ? "enable" : "disable");
  171. }
  172. if (omap_crtc->channel == OMAP_DSS_CHANNEL_DIGIT) {
  173. omap_crtc->ignore_digit_sync_lost = false;
  174. /* make sure the irq handler sees the value above */
  175. mb();
  176. }
  177. }
  178. static int omap_crtc_enable(struct omap_overlay_manager *mgr)
  179. {
  180. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  181. dispc_mgr_setup(omap_crtc->channel, &omap_crtc->info);
  182. dispc_mgr_set_timings(omap_crtc->channel,
  183. &omap_crtc->timings);
  184. omap_crtc_set_enabled(&omap_crtc->base, true);
  185. return 0;
  186. }
  187. static void omap_crtc_disable(struct omap_overlay_manager *mgr)
  188. {
  189. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  190. omap_crtc_set_enabled(&omap_crtc->base, false);
  191. }
  192. static void omap_crtc_set_timings(struct omap_overlay_manager *mgr,
  193. const struct omap_video_timings *timings)
  194. {
  195. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  196. DBG("%s", omap_crtc->name);
  197. omap_crtc->timings = *timings;
  198. }
  199. static void omap_crtc_set_lcd_config(struct omap_overlay_manager *mgr,
  200. const struct dss_lcd_mgr_config *config)
  201. {
  202. struct omap_crtc *omap_crtc = omap_crtcs[mgr->id];
  203. DBG("%s", omap_crtc->name);
  204. dispc_mgr_set_lcd_config(omap_crtc->channel, config);
  205. }
  206. static int omap_crtc_register_framedone_handler(
  207. struct omap_overlay_manager *mgr,
  208. void (*handler)(void *), void *data)
  209. {
  210. return 0;
  211. }
  212. static void omap_crtc_unregister_framedone_handler(
  213. struct omap_overlay_manager *mgr,
  214. void (*handler)(void *), void *data)
  215. {
  216. }
  217. static const struct dss_mgr_ops mgr_ops = {
  218. .connect = omap_crtc_connect,
  219. .disconnect = omap_crtc_disconnect,
  220. .start_update = omap_crtc_start_update,
  221. .enable = omap_crtc_enable,
  222. .disable = omap_crtc_disable,
  223. .set_timings = omap_crtc_set_timings,
  224. .set_lcd_config = omap_crtc_set_lcd_config,
  225. .register_framedone_handler = omap_crtc_register_framedone_handler,
  226. .unregister_framedone_handler = omap_crtc_unregister_framedone_handler,
  227. };
  228. /* -----------------------------------------------------------------------------
  229. * Setup, Flush and Page Flip
  230. */
  231. void omap_crtc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  232. {
  233. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  234. struct drm_device *dev = crtc->dev;
  235. unsigned long flags;
  236. spin_lock_irqsave(&dev->event_lock, flags);
  237. /* Only complete events queued for our file handle. */
  238. if (omap_crtc->flip_event &&
  239. file == omap_crtc->flip_event->base.file_priv) {
  240. drm_send_vblank_event(dev, omap_crtc->pipe,
  241. omap_crtc->flip_event);
  242. omap_crtc->flip_event = NULL;
  243. }
  244. spin_unlock_irqrestore(&dev->event_lock, flags);
  245. }
  246. /* Must be called with dev->event_lock locked. */
  247. static void omap_crtc_complete_page_flip(struct drm_crtc *crtc,
  248. enum omap_page_flip_state state)
  249. {
  250. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  251. struct drm_device *dev = crtc->dev;
  252. if (omap_crtc->flip_event) {
  253. drm_send_vblank_event(dev, omap_crtc->pipe,
  254. omap_crtc->flip_event);
  255. omap_crtc->flip_event = NULL;
  256. }
  257. omap_crtc->flip_state = state;
  258. if (state == OMAP_PAGE_FLIP_IDLE)
  259. wake_up(&omap_crtc->flip_wait);
  260. }
  261. static bool omap_crtc_page_flip_pending(struct drm_crtc *crtc)
  262. {
  263. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  264. struct drm_device *dev = crtc->dev;
  265. unsigned long flags;
  266. bool pending;
  267. spin_lock_irqsave(&dev->event_lock, flags);
  268. pending = omap_crtc->flip_state != OMAP_PAGE_FLIP_IDLE;
  269. spin_unlock_irqrestore(&dev->event_lock, flags);
  270. return pending;
  271. }
  272. static void omap_crtc_wait_page_flip(struct drm_crtc *crtc)
  273. {
  274. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  275. struct drm_device *dev = crtc->dev;
  276. bool cancelled = false;
  277. unsigned long flags;
  278. /*
  279. * If we're still waiting for the GEM async operation to complete just
  280. * cancel the page flip, as we're holding the CRTC mutex preventing the
  281. * page flip work handler from queueing the page flip.
  282. *
  283. * We can't release the reference to the frame buffer here as the async
  284. * operation doesn't keep its own reference to the buffer. We'll just
  285. * let the page flip work queue handle that.
  286. */
  287. spin_lock_irqsave(&dev->event_lock, flags);
  288. if (omap_crtc->flip_state == OMAP_PAGE_FLIP_WAIT) {
  289. omap_crtc_complete_page_flip(crtc, OMAP_PAGE_FLIP_CANCELLED);
  290. cancelled = true;
  291. }
  292. spin_unlock_irqrestore(&dev->event_lock, flags);
  293. if (cancelled)
  294. return;
  295. if (wait_event_timeout(omap_crtc->flip_wait,
  296. !omap_crtc_page_flip_pending(crtc),
  297. msecs_to_jiffies(50)))
  298. return;
  299. dev_warn(crtc->dev->dev, "page flip timeout!\n");
  300. spin_lock_irqsave(&dev->event_lock, flags);
  301. omap_crtc_complete_page_flip(crtc, OMAP_PAGE_FLIP_IDLE);
  302. spin_unlock_irqrestore(&dev->event_lock, flags);
  303. }
  304. static void omap_crtc_error_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  305. {
  306. struct omap_crtc *omap_crtc =
  307. container_of(irq, struct omap_crtc, error_irq);
  308. if (omap_crtc->ignore_digit_sync_lost) {
  309. irqstatus &= ~DISPC_IRQ_SYNC_LOST_DIGIT;
  310. if (!irqstatus)
  311. return;
  312. }
  313. DRM_ERROR_RATELIMITED("%s: errors: %08x\n", omap_crtc->name, irqstatus);
  314. }
  315. static void omap_crtc_vblank_irq(struct omap_drm_irq *irq, uint32_t irqstatus)
  316. {
  317. struct omap_crtc *omap_crtc =
  318. container_of(irq, struct omap_crtc, vblank_irq);
  319. struct drm_device *dev = omap_crtc->base.dev;
  320. unsigned long flags;
  321. if (dispc_mgr_go_busy(omap_crtc->channel))
  322. return;
  323. DBG("%s: apply done", omap_crtc->name);
  324. __omap_irq_unregister(dev, &omap_crtc->vblank_irq);
  325. /* wakeup userspace */
  326. spin_lock_irqsave(&dev->event_lock, flags);
  327. omap_crtc_complete_page_flip(&omap_crtc->base, OMAP_PAGE_FLIP_IDLE);
  328. spin_unlock_irqrestore(&dev->event_lock, flags);
  329. complete(&omap_crtc->completion);
  330. }
  331. int omap_crtc_flush(struct drm_crtc *crtc)
  332. {
  333. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  334. struct omap_framebuffer_unpin *fb, *next;
  335. DBG("%s: GO", omap_crtc->name);
  336. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  337. WARN_ON(omap_crtc->vblank_irq.registered);
  338. dispc_runtime_get();
  339. if (dispc_mgr_is_enabled(omap_crtc->channel)) {
  340. dispc_mgr_go(omap_crtc->channel);
  341. omap_irq_register(crtc->dev, &omap_crtc->vblank_irq);
  342. WARN_ON(!wait_for_completion_timeout(&omap_crtc->completion,
  343. msecs_to_jiffies(100)));
  344. reinit_completion(&omap_crtc->completion);
  345. }
  346. dispc_runtime_put();
  347. /* Unpin and unreference pending framebuffers. */
  348. list_for_each_entry_safe(fb, next, &omap_crtc->pending_unpins, list) {
  349. omap_framebuffer_unpin(fb->fb);
  350. drm_framebuffer_unreference(fb->fb);
  351. list_del(&fb->list);
  352. kfree(fb);
  353. }
  354. return 0;
  355. }
  356. int omap_crtc_queue_unpin(struct drm_crtc *crtc, struct drm_framebuffer *fb)
  357. {
  358. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  359. struct omap_framebuffer_unpin *unpin;
  360. unpin = kzalloc(sizeof(*unpin), GFP_KERNEL);
  361. if (!unpin)
  362. return -ENOMEM;
  363. unpin->fb = fb;
  364. list_add_tail(&unpin->list, &omap_crtc->pending_unpins);
  365. return 0;
  366. }
  367. static void omap_crtc_setup(struct drm_crtc *crtc)
  368. {
  369. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  370. struct omap_drm_private *priv = crtc->dev->dev_private;
  371. struct drm_encoder *encoder = NULL;
  372. unsigned int i;
  373. DBG("%s: enabled=%d", omap_crtc->name, omap_crtc->enabled);
  374. dispc_runtime_get();
  375. for (i = 0; i < priv->num_encoders; i++) {
  376. if (priv->encoders[i]->crtc == crtc) {
  377. encoder = priv->encoders[i];
  378. break;
  379. }
  380. }
  381. if (omap_crtc->current_encoder && encoder != omap_crtc->current_encoder)
  382. omap_encoder_set_enabled(omap_crtc->current_encoder, false);
  383. omap_crtc->current_encoder = encoder;
  384. if (!omap_crtc->enabled) {
  385. if (encoder)
  386. omap_encoder_set_enabled(encoder, false);
  387. } else {
  388. if (encoder) {
  389. omap_encoder_set_enabled(encoder, false);
  390. omap_encoder_update(encoder, omap_crtc->mgr,
  391. &omap_crtc->timings);
  392. omap_encoder_set_enabled(encoder, true);
  393. }
  394. }
  395. dispc_runtime_put();
  396. }
  397. /* -----------------------------------------------------------------------------
  398. * CRTC Functions
  399. */
  400. static void omap_crtc_destroy(struct drm_crtc *crtc)
  401. {
  402. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  403. DBG("%s", omap_crtc->name);
  404. WARN_ON(omap_crtc->vblank_irq.registered);
  405. omap_irq_unregister(crtc->dev, &omap_crtc->error_irq);
  406. drm_crtc_cleanup(crtc);
  407. kfree(omap_crtc);
  408. }
  409. static void omap_crtc_dpms(struct drm_crtc *crtc, int mode)
  410. {
  411. struct omap_drm_private *priv = crtc->dev->dev_private;
  412. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  413. bool enable = (mode == DRM_MODE_DPMS_ON);
  414. int i;
  415. DBG("%s: %d", omap_crtc->name, mode);
  416. if (enable == omap_crtc->enabled)
  417. return;
  418. if (!enable) {
  419. omap_crtc_wait_page_flip(crtc);
  420. dispc_runtime_get();
  421. drm_crtc_vblank_off(crtc);
  422. dispc_runtime_put();
  423. }
  424. /* Enable/disable all planes associated with the CRTC. */
  425. for (i = 0; i < priv->num_planes; i++) {
  426. struct drm_plane *plane = priv->planes[i];
  427. if (plane->crtc == crtc)
  428. WARN_ON(omap_plane_set_enable(plane, enable));
  429. }
  430. omap_crtc->enabled = enable;
  431. omap_crtc_setup(crtc);
  432. omap_crtc_flush(crtc);
  433. if (enable) {
  434. dispc_runtime_get();
  435. drm_crtc_vblank_on(crtc);
  436. dispc_runtime_put();
  437. }
  438. }
  439. static bool omap_crtc_mode_fixup(struct drm_crtc *crtc,
  440. const struct drm_display_mode *mode,
  441. struct drm_display_mode *adjusted_mode)
  442. {
  443. return true;
  444. }
  445. static int omap_crtc_mode_set(struct drm_crtc *crtc,
  446. struct drm_display_mode *mode,
  447. struct drm_display_mode *adjusted_mode,
  448. int x, int y,
  449. struct drm_framebuffer *old_fb)
  450. {
  451. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  452. mode = adjusted_mode;
  453. DBG("%s: set mode: %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x",
  454. omap_crtc->name, mode->base.id, mode->name,
  455. mode->vrefresh, mode->clock,
  456. mode->hdisplay, mode->hsync_start,
  457. mode->hsync_end, mode->htotal,
  458. mode->vdisplay, mode->vsync_start,
  459. mode->vsync_end, mode->vtotal,
  460. mode->type, mode->flags);
  461. copy_timings_drm_to_omap(&omap_crtc->timings, mode);
  462. /*
  463. * The primary plane CRTC can be reset if the plane is disabled directly
  464. * through the universal plane API. Set it again here.
  465. */
  466. crtc->primary->crtc = crtc;
  467. return omap_plane_mode_set(crtc->primary, crtc, crtc->primary->fb,
  468. 0, 0, mode->hdisplay, mode->vdisplay,
  469. x, y, mode->hdisplay, mode->vdisplay);
  470. }
  471. static void omap_crtc_prepare(struct drm_crtc *crtc)
  472. {
  473. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  474. DBG("%s", omap_crtc->name);
  475. omap_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  476. }
  477. static void omap_crtc_commit(struct drm_crtc *crtc)
  478. {
  479. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  480. DBG("%s", omap_crtc->name);
  481. omap_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  482. }
  483. static int omap_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  484. struct drm_framebuffer *old_fb)
  485. {
  486. struct drm_plane *plane = crtc->primary;
  487. struct drm_display_mode *mode = &crtc->mode;
  488. int ret;
  489. ret = omap_plane_mode_set(plane, crtc, crtc->primary->fb,
  490. 0, 0, mode->hdisplay, mode->vdisplay,
  491. x, y, mode->hdisplay, mode->vdisplay);
  492. if (ret < 0)
  493. return ret;
  494. return omap_crtc_flush(crtc);
  495. }
  496. static void page_flip_worker(struct work_struct *work)
  497. {
  498. struct omap_crtc *omap_crtc =
  499. container_of(work, struct omap_crtc, flip_work);
  500. struct drm_crtc *crtc = &omap_crtc->base;
  501. struct drm_display_mode *mode = &crtc->mode;
  502. struct drm_device *dev = crtc->dev;
  503. struct drm_framebuffer *fb;
  504. struct drm_gem_object *bo;
  505. unsigned long flags;
  506. bool queue_flip;
  507. drm_modeset_lock(&crtc->mutex, NULL);
  508. spin_lock_irqsave(&dev->event_lock, flags);
  509. /*
  510. * The page flip could have been cancelled while waiting for the GEM
  511. * async operation to complete. Don't queue the flip in that case.
  512. */
  513. if (omap_crtc->flip_state == OMAP_PAGE_FLIP_WAIT) {
  514. omap_crtc->flip_state = OMAP_PAGE_FLIP_QUEUED;
  515. queue_flip = true;
  516. } else {
  517. omap_crtc->flip_state = OMAP_PAGE_FLIP_IDLE;
  518. queue_flip = false;
  519. }
  520. fb = omap_crtc->flip_fb;
  521. omap_crtc->flip_fb = NULL;
  522. spin_unlock_irqrestore(&dev->event_lock, flags);
  523. if (queue_flip) {
  524. omap_plane_mode_set(crtc->primary, crtc, fb,
  525. 0, 0, mode->hdisplay, mode->vdisplay,
  526. crtc->x, crtc->y, mode->hdisplay,
  527. mode->vdisplay);
  528. omap_crtc_flush(crtc);
  529. }
  530. drm_modeset_unlock(&crtc->mutex);
  531. bo = omap_framebuffer_bo(fb, 0);
  532. drm_gem_object_unreference_unlocked(bo);
  533. drm_framebuffer_unreference(fb);
  534. }
  535. static void page_flip_cb(void *arg)
  536. {
  537. struct drm_crtc *crtc = arg;
  538. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  539. struct omap_drm_private *priv = crtc->dev->dev_private;
  540. /* avoid assumptions about what ctxt we are called from: */
  541. queue_work(priv->wq, &omap_crtc->flip_work);
  542. }
  543. static int omap_crtc_page_flip(struct drm_crtc *crtc,
  544. struct drm_framebuffer *fb,
  545. struct drm_pending_vblank_event *event,
  546. uint32_t page_flip_flags)
  547. {
  548. struct drm_device *dev = crtc->dev;
  549. struct omap_crtc *omap_crtc = to_omap_crtc(crtc);
  550. struct drm_plane *primary = crtc->primary;
  551. struct drm_gem_object *bo;
  552. unsigned long flags;
  553. DBG("%d -> %d (event=%p)", primary->fb ? primary->fb->base.id : -1,
  554. fb->base.id, event);
  555. spin_lock_irqsave(&dev->event_lock, flags);
  556. if (omap_crtc->flip_state != OMAP_PAGE_FLIP_IDLE) {
  557. spin_unlock_irqrestore(&dev->event_lock, flags);
  558. dev_err(dev->dev, "already a pending flip\n");
  559. return -EBUSY;
  560. }
  561. /*
  562. * Store a reference to the framebuffer queued for page flip in the CRTC
  563. * private structure. We can't rely on crtc->primary->fb in the page
  564. * flip worker, as a racing CRTC disable (due for instance to an
  565. * explicit framebuffer deletion from userspace) would set that field to
  566. * NULL before the worker gets a change to run.
  567. */
  568. drm_framebuffer_reference(fb);
  569. omap_crtc->flip_fb = fb;
  570. omap_crtc->flip_event = event;
  571. omap_crtc->flip_state = OMAP_PAGE_FLIP_WAIT;
  572. primary->fb = fb;
  573. spin_unlock_irqrestore(&dev->event_lock, flags);
  574. /*
  575. * Hold a reference temporarily until the crtc is updated
  576. * and takes the reference to the bo. This avoids it
  577. * getting freed from under us:
  578. */
  579. bo = omap_framebuffer_bo(fb, 0);
  580. drm_gem_object_reference(bo);
  581. omap_gem_op_async(bo, OMAP_GEM_READ, page_flip_cb, crtc);
  582. return 0;
  583. }
  584. static int omap_crtc_set_property(struct drm_crtc *crtc,
  585. struct drm_property *property, uint64_t val)
  586. {
  587. if (property == crtc->dev->mode_config.rotation_property) {
  588. crtc->invert_dimensions =
  589. !!(val & ((1LL << DRM_ROTATE_90) | (1LL << DRM_ROTATE_270)));
  590. }
  591. return omap_plane_set_property(crtc->primary, property, val);
  592. }
  593. static const struct drm_crtc_funcs omap_crtc_funcs = {
  594. .set_config = drm_crtc_helper_set_config,
  595. .destroy = omap_crtc_destroy,
  596. .page_flip = omap_crtc_page_flip,
  597. .set_property = omap_crtc_set_property,
  598. };
  599. static const struct drm_crtc_helper_funcs omap_crtc_helper_funcs = {
  600. .dpms = omap_crtc_dpms,
  601. .mode_fixup = omap_crtc_mode_fixup,
  602. .mode_set = omap_crtc_mode_set,
  603. .prepare = omap_crtc_prepare,
  604. .commit = omap_crtc_commit,
  605. .mode_set_base = omap_crtc_mode_set_base,
  606. };
  607. /* -----------------------------------------------------------------------------
  608. * Init and Cleanup
  609. */
  610. static const char *channel_names[] = {
  611. [OMAP_DSS_CHANNEL_LCD] = "lcd",
  612. [OMAP_DSS_CHANNEL_DIGIT] = "tv",
  613. [OMAP_DSS_CHANNEL_LCD2] = "lcd2",
  614. [OMAP_DSS_CHANNEL_LCD3] = "lcd3",
  615. };
  616. void omap_crtc_pre_init(void)
  617. {
  618. dss_install_mgr_ops(&mgr_ops);
  619. }
  620. void omap_crtc_pre_uninit(void)
  621. {
  622. dss_uninstall_mgr_ops();
  623. }
  624. /* initialize crtc */
  625. struct drm_crtc *omap_crtc_init(struct drm_device *dev,
  626. struct drm_plane *plane, enum omap_channel channel, int id)
  627. {
  628. struct drm_crtc *crtc = NULL;
  629. struct omap_crtc *omap_crtc;
  630. struct omap_overlay_manager_info *info;
  631. int ret;
  632. DBG("%s", channel_names[channel]);
  633. omap_crtc = kzalloc(sizeof(*omap_crtc), GFP_KERNEL);
  634. if (!omap_crtc)
  635. return NULL;
  636. crtc = &omap_crtc->base;
  637. INIT_WORK(&omap_crtc->flip_work, page_flip_worker);
  638. init_waitqueue_head(&omap_crtc->flip_wait);
  639. INIT_LIST_HEAD(&omap_crtc->pending_unpins);
  640. init_completion(&omap_crtc->completion);
  641. omap_crtc->channel = channel;
  642. omap_crtc->name = channel_names[channel];
  643. omap_crtc->pipe = id;
  644. omap_crtc->vblank_irq.irqmask = pipe2vbl(crtc);
  645. omap_crtc->vblank_irq.irq = omap_crtc_vblank_irq;
  646. omap_crtc->error_irq.irqmask =
  647. dispc_mgr_get_sync_lost_irq(channel);
  648. omap_crtc->error_irq.irq = omap_crtc_error_irq;
  649. omap_irq_register(dev, &omap_crtc->error_irq);
  650. /* temporary: */
  651. omap_crtc->mgr = omap_dss_get_overlay_manager(channel);
  652. /* TODO: fix hard-coded setup.. add properties! */
  653. info = &omap_crtc->info;
  654. info->default_color = 0x00000000;
  655. info->trans_key = 0x00000000;
  656. info->trans_key_type = OMAP_DSS_COLOR_KEY_GFX_DST;
  657. info->trans_enabled = false;
  658. ret = drm_crtc_init_with_planes(dev, crtc, plane, NULL,
  659. &omap_crtc_funcs);
  660. if (ret < 0) {
  661. kfree(omap_crtc);
  662. return NULL;
  663. }
  664. drm_crtc_helper_add(crtc, &omap_crtc_helper_funcs);
  665. omap_plane_install_properties(crtc->primary, &crtc->base);
  666. omap_crtcs[channel] = omap_crtc;
  667. return crtc;
  668. }