exynos_drm_crtc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /* exynos_drm_crtc.c
  2. *
  3. * Copyright (c) 2011 Samsung Electronics Co., Ltd.
  4. * Authors:
  5. * Inki Dae <inki.dae@samsung.com>
  6. * Joonyoung Shim <jy0922.shim@samsung.com>
  7. * Seung-Woo Kim <sw0312.kim@samsung.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include "exynos_drm_crtc.h"
  17. #include "exynos_drm_drv.h"
  18. #include "exynos_drm_encoder.h"
  19. #include "exynos_drm_plane.h"
  20. #define to_exynos_crtc(x) container_of(x, struct exynos_drm_crtc,\
  21. drm_crtc)
  22. enum exynos_crtc_mode {
  23. CRTC_MODE_NORMAL, /* normal mode */
  24. CRTC_MODE_BLANK, /* The private plane of crtc is blank */
  25. };
  26. /*
  27. * Exynos specific crtc structure.
  28. *
  29. * @drm_crtc: crtc object.
  30. * @drm_plane: pointer of private plane object for this crtc
  31. * @manager: the manager associated with this crtc
  32. * @pipe: a crtc index created at load() with a new crtc object creation
  33. * and the crtc object would be set to private->crtc array
  34. * to get a crtc object corresponding to this pipe from private->crtc
  35. * array when irq interrupt occurred. the reason of using this pipe is that
  36. * drm framework doesn't support multiple irq yet.
  37. * we can refer to the crtc to current hardware interrupt occurred through
  38. * this pipe value.
  39. * @dpms: store the crtc dpms value
  40. * @mode: store the crtc mode value
  41. */
  42. struct exynos_drm_crtc {
  43. struct drm_crtc drm_crtc;
  44. struct drm_plane *plane;
  45. struct exynos_drm_manager *manager;
  46. unsigned int pipe;
  47. unsigned int dpms;
  48. enum exynos_crtc_mode mode;
  49. wait_queue_head_t pending_flip_queue;
  50. atomic_t pending_flip;
  51. };
  52. static void exynos_drm_crtc_dpms(struct drm_crtc *crtc, int mode)
  53. {
  54. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  55. struct exynos_drm_manager *manager = exynos_crtc->manager;
  56. DRM_DEBUG_KMS("crtc[%d] mode[%d]\n", crtc->base.id, mode);
  57. if (exynos_crtc->dpms == mode) {
  58. DRM_DEBUG_KMS("desired dpms mode is same as previous one.\n");
  59. return;
  60. }
  61. if (mode > DRM_MODE_DPMS_ON) {
  62. /* wait for the completion of page flip. */
  63. wait_event(exynos_crtc->pending_flip_queue,
  64. atomic_read(&exynos_crtc->pending_flip) == 0);
  65. drm_vblank_off(crtc->dev, exynos_crtc->pipe);
  66. }
  67. if (manager->ops->dpms)
  68. manager->ops->dpms(manager, mode);
  69. exynos_crtc->dpms = mode;
  70. }
  71. static void exynos_drm_crtc_prepare(struct drm_crtc *crtc)
  72. {
  73. /* drm framework doesn't check NULL. */
  74. }
  75. static void exynos_drm_crtc_commit(struct drm_crtc *crtc)
  76. {
  77. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  78. struct exynos_drm_manager *manager = exynos_crtc->manager;
  79. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  80. exynos_plane_commit(exynos_crtc->plane);
  81. if (manager->ops->commit)
  82. manager->ops->commit(manager);
  83. exynos_plane_dpms(exynos_crtc->plane, DRM_MODE_DPMS_ON);
  84. }
  85. static bool
  86. exynos_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  87. const struct drm_display_mode *mode,
  88. struct drm_display_mode *adjusted_mode)
  89. {
  90. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  91. struct exynos_drm_manager *manager = exynos_crtc->manager;
  92. if (manager->ops->mode_fixup)
  93. return manager->ops->mode_fixup(manager, mode, adjusted_mode);
  94. return true;
  95. }
  96. static int
  97. exynos_drm_crtc_mode_set(struct drm_crtc *crtc, struct drm_display_mode *mode,
  98. struct drm_display_mode *adjusted_mode, int x, int y,
  99. struct drm_framebuffer *old_fb)
  100. {
  101. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  102. struct exynos_drm_manager *manager = exynos_crtc->manager;
  103. struct drm_plane *plane = exynos_crtc->plane;
  104. unsigned int crtc_w;
  105. unsigned int crtc_h;
  106. int ret;
  107. /*
  108. * copy the mode data adjusted by mode_fixup() into crtc->mode
  109. * so that hardware can be seet to proper mode.
  110. */
  111. memcpy(&crtc->mode, adjusted_mode, sizeof(*adjusted_mode));
  112. crtc_w = crtc->primary->fb->width - x;
  113. crtc_h = crtc->primary->fb->height - y;
  114. if (manager->ops->mode_set)
  115. manager->ops->mode_set(manager, &crtc->mode);
  116. ret = exynos_plane_mode_set(plane, crtc, crtc->primary->fb, 0, 0, crtc_w, crtc_h,
  117. x, y, crtc_w, crtc_h);
  118. if (ret)
  119. return ret;
  120. plane->crtc = crtc;
  121. plane->fb = crtc->primary->fb;
  122. drm_framebuffer_reference(plane->fb);
  123. return 0;
  124. }
  125. static int exynos_drm_crtc_mode_set_commit(struct drm_crtc *crtc, int x, int y,
  126. struct drm_framebuffer *old_fb)
  127. {
  128. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  129. struct drm_plane *plane = exynos_crtc->plane;
  130. unsigned int crtc_w;
  131. unsigned int crtc_h;
  132. int ret;
  133. /* when framebuffer changing is requested, crtc's dpms should be on */
  134. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  135. DRM_ERROR("failed framebuffer changing request.\n");
  136. return -EPERM;
  137. }
  138. crtc_w = crtc->primary->fb->width - x;
  139. crtc_h = crtc->primary->fb->height - y;
  140. ret = exynos_plane_mode_set(plane, crtc, crtc->primary->fb, 0, 0, crtc_w, crtc_h,
  141. x, y, crtc_w, crtc_h);
  142. if (ret)
  143. return ret;
  144. exynos_drm_crtc_commit(crtc);
  145. return 0;
  146. }
  147. static int exynos_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  148. struct drm_framebuffer *old_fb)
  149. {
  150. return exynos_drm_crtc_mode_set_commit(crtc, x, y, old_fb);
  151. }
  152. static void exynos_drm_crtc_disable(struct drm_crtc *crtc)
  153. {
  154. struct drm_plane *plane;
  155. int ret;
  156. exynos_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  157. drm_for_each_legacy_plane(plane, &crtc->dev->mode_config.plane_list) {
  158. if (plane->crtc != crtc)
  159. continue;
  160. ret = plane->funcs->disable_plane(plane);
  161. if (ret)
  162. DRM_ERROR("Failed to disable plane %d\n", ret);
  163. }
  164. }
  165. static struct drm_crtc_helper_funcs exynos_crtc_helper_funcs = {
  166. .dpms = exynos_drm_crtc_dpms,
  167. .prepare = exynos_drm_crtc_prepare,
  168. .commit = exynos_drm_crtc_commit,
  169. .mode_fixup = exynos_drm_crtc_mode_fixup,
  170. .mode_set = exynos_drm_crtc_mode_set,
  171. .mode_set_base = exynos_drm_crtc_mode_set_base,
  172. .disable = exynos_drm_crtc_disable,
  173. };
  174. static int exynos_drm_crtc_page_flip(struct drm_crtc *crtc,
  175. struct drm_framebuffer *fb,
  176. struct drm_pending_vblank_event *event,
  177. uint32_t page_flip_flags)
  178. {
  179. struct drm_device *dev = crtc->dev;
  180. struct exynos_drm_private *dev_priv = dev->dev_private;
  181. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  182. struct drm_framebuffer *old_fb = crtc->primary->fb;
  183. int ret = -EINVAL;
  184. /* when the page flip is requested, crtc's dpms should be on */
  185. if (exynos_crtc->dpms > DRM_MODE_DPMS_ON) {
  186. DRM_ERROR("failed page flip request.\n");
  187. return -EINVAL;
  188. }
  189. mutex_lock(&dev->struct_mutex);
  190. if (event) {
  191. /*
  192. * the pipe from user always is 0 so we can set pipe number
  193. * of current owner to event.
  194. */
  195. event->pipe = exynos_crtc->pipe;
  196. ret = drm_vblank_get(dev, exynos_crtc->pipe);
  197. if (ret) {
  198. DRM_DEBUG("failed to acquire vblank counter\n");
  199. goto out;
  200. }
  201. spin_lock_irq(&dev->event_lock);
  202. list_add_tail(&event->base.link,
  203. &dev_priv->pageflip_event_list);
  204. atomic_set(&exynos_crtc->pending_flip, 1);
  205. spin_unlock_irq(&dev->event_lock);
  206. crtc->primary->fb = fb;
  207. ret = exynos_drm_crtc_mode_set_commit(crtc, crtc->x, crtc->y,
  208. NULL);
  209. if (ret) {
  210. crtc->primary->fb = old_fb;
  211. spin_lock_irq(&dev->event_lock);
  212. drm_vblank_put(dev, exynos_crtc->pipe);
  213. list_del(&event->base.link);
  214. spin_unlock_irq(&dev->event_lock);
  215. goto out;
  216. }
  217. }
  218. out:
  219. mutex_unlock(&dev->struct_mutex);
  220. return ret;
  221. }
  222. static void exynos_drm_crtc_destroy(struct drm_crtc *crtc)
  223. {
  224. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  225. struct exynos_drm_private *private = crtc->dev->dev_private;
  226. private->crtc[exynos_crtc->pipe] = NULL;
  227. drm_crtc_cleanup(crtc);
  228. kfree(exynos_crtc);
  229. }
  230. static int exynos_drm_crtc_set_property(struct drm_crtc *crtc,
  231. struct drm_property *property,
  232. uint64_t val)
  233. {
  234. struct drm_device *dev = crtc->dev;
  235. struct exynos_drm_private *dev_priv = dev->dev_private;
  236. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(crtc);
  237. if (property == dev_priv->crtc_mode_property) {
  238. enum exynos_crtc_mode mode = val;
  239. if (mode == exynos_crtc->mode)
  240. return 0;
  241. exynos_crtc->mode = mode;
  242. switch (mode) {
  243. case CRTC_MODE_NORMAL:
  244. exynos_drm_crtc_commit(crtc);
  245. break;
  246. case CRTC_MODE_BLANK:
  247. exynos_plane_dpms(exynos_crtc->plane,
  248. DRM_MODE_DPMS_OFF);
  249. break;
  250. default:
  251. break;
  252. }
  253. return 0;
  254. }
  255. return -EINVAL;
  256. }
  257. static struct drm_crtc_funcs exynos_crtc_funcs = {
  258. .set_config = drm_crtc_helper_set_config,
  259. .page_flip = exynos_drm_crtc_page_flip,
  260. .destroy = exynos_drm_crtc_destroy,
  261. .set_property = exynos_drm_crtc_set_property,
  262. };
  263. static const struct drm_prop_enum_list mode_names[] = {
  264. { CRTC_MODE_NORMAL, "normal" },
  265. { CRTC_MODE_BLANK, "blank" },
  266. };
  267. static void exynos_drm_crtc_attach_mode_property(struct drm_crtc *crtc)
  268. {
  269. struct drm_device *dev = crtc->dev;
  270. struct exynos_drm_private *dev_priv = dev->dev_private;
  271. struct drm_property *prop;
  272. prop = dev_priv->crtc_mode_property;
  273. if (!prop) {
  274. prop = drm_property_create_enum(dev, 0, "mode", mode_names,
  275. ARRAY_SIZE(mode_names));
  276. if (!prop)
  277. return;
  278. dev_priv->crtc_mode_property = prop;
  279. }
  280. drm_object_attach_property(&crtc->base, prop, 0);
  281. }
  282. int exynos_drm_crtc_create(struct exynos_drm_manager *manager)
  283. {
  284. struct exynos_drm_crtc *exynos_crtc;
  285. struct exynos_drm_private *private = manager->drm_dev->dev_private;
  286. struct drm_crtc *crtc;
  287. exynos_crtc = kzalloc(sizeof(*exynos_crtc), GFP_KERNEL);
  288. if (!exynos_crtc)
  289. return -ENOMEM;
  290. init_waitqueue_head(&exynos_crtc->pending_flip_queue);
  291. atomic_set(&exynos_crtc->pending_flip, 0);
  292. exynos_crtc->dpms = DRM_MODE_DPMS_OFF;
  293. exynos_crtc->manager = manager;
  294. exynos_crtc->pipe = manager->pipe;
  295. exynos_crtc->plane = exynos_plane_init(manager->drm_dev,
  296. 1 << manager->pipe, true);
  297. if (!exynos_crtc->plane) {
  298. kfree(exynos_crtc);
  299. return -ENOMEM;
  300. }
  301. manager->crtc = &exynos_crtc->drm_crtc;
  302. crtc = &exynos_crtc->drm_crtc;
  303. private->crtc[manager->pipe] = crtc;
  304. drm_crtc_init(manager->drm_dev, crtc, &exynos_crtc_funcs);
  305. drm_crtc_helper_add(crtc, &exynos_crtc_helper_funcs);
  306. exynos_drm_crtc_attach_mode_property(crtc);
  307. return 0;
  308. }
  309. int exynos_drm_crtc_enable_vblank(struct drm_device *dev, int pipe)
  310. {
  311. struct exynos_drm_private *private = dev->dev_private;
  312. struct exynos_drm_crtc *exynos_crtc =
  313. to_exynos_crtc(private->crtc[pipe]);
  314. struct exynos_drm_manager *manager = exynos_crtc->manager;
  315. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  316. return -EPERM;
  317. if (manager->ops->enable_vblank)
  318. manager->ops->enable_vblank(manager);
  319. return 0;
  320. }
  321. void exynos_drm_crtc_disable_vblank(struct drm_device *dev, int pipe)
  322. {
  323. struct exynos_drm_private *private = dev->dev_private;
  324. struct exynos_drm_crtc *exynos_crtc =
  325. to_exynos_crtc(private->crtc[pipe]);
  326. struct exynos_drm_manager *manager = exynos_crtc->manager;
  327. if (exynos_crtc->dpms != DRM_MODE_DPMS_ON)
  328. return;
  329. if (manager->ops->disable_vblank)
  330. manager->ops->disable_vblank(manager);
  331. }
  332. void exynos_drm_crtc_finish_pageflip(struct drm_device *dev, int pipe)
  333. {
  334. struct exynos_drm_private *dev_priv = dev->dev_private;
  335. struct drm_pending_vblank_event *e, *t;
  336. struct drm_crtc *drm_crtc = dev_priv->crtc[pipe];
  337. struct exynos_drm_crtc *exynos_crtc = to_exynos_crtc(drm_crtc);
  338. unsigned long flags;
  339. spin_lock_irqsave(&dev->event_lock, flags);
  340. list_for_each_entry_safe(e, t, &dev_priv->pageflip_event_list,
  341. base.link) {
  342. /* if event's pipe isn't same as crtc then ignore it. */
  343. if (pipe != e->pipe)
  344. continue;
  345. list_del(&e->base.link);
  346. drm_send_vblank_event(dev, -1, e);
  347. drm_vblank_put(dev, pipe);
  348. atomic_set(&exynos_crtc->pending_flip, 0);
  349. wake_up(&exynos_crtc->pending_flip_queue);
  350. }
  351. spin_unlock_irqrestore(&dev->event_lock, flags);
  352. }
  353. void exynos_drm_crtc_plane_mode_set(struct drm_crtc *crtc,
  354. struct exynos_drm_overlay *overlay)
  355. {
  356. struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
  357. if (manager->ops->win_mode_set)
  358. manager->ops->win_mode_set(manager, overlay);
  359. }
  360. void exynos_drm_crtc_plane_commit(struct drm_crtc *crtc, int zpos)
  361. {
  362. struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
  363. if (manager->ops->win_commit)
  364. manager->ops->win_commit(manager, zpos);
  365. }
  366. void exynos_drm_crtc_plane_enable(struct drm_crtc *crtc, int zpos)
  367. {
  368. struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
  369. if (manager->ops->win_enable)
  370. manager->ops->win_enable(manager, zpos);
  371. }
  372. void exynos_drm_crtc_plane_disable(struct drm_crtc *crtc, int zpos)
  373. {
  374. struct exynos_drm_manager *manager = to_exynos_crtc(crtc)->manager;
  375. if (manager->ops->win_disable)
  376. manager->ops->win_disable(manager, zpos);
  377. }
  378. void exynos_drm_crtc_complete_scanout(struct drm_framebuffer *fb)
  379. {
  380. struct exynos_drm_manager *manager;
  381. struct drm_device *dev = fb->dev;
  382. struct drm_crtc *crtc;
  383. /*
  384. * make sure that overlay data are updated to real hardware
  385. * for all encoders.
  386. */
  387. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  388. manager = to_exynos_crtc(crtc)->manager;
  389. /*
  390. * wait for vblank interrupt
  391. * - this makes sure that overlay data are updated to
  392. * real hardware.
  393. */
  394. if (manager->ops->wait_for_vblank)
  395. manager->ops->wait_for_vblank(manager);
  396. }
  397. }
  398. int exynos_drm_crtc_get_pipe_from_type(struct drm_device *drm_dev,
  399. unsigned int out_type)
  400. {
  401. struct drm_crtc *crtc;
  402. list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
  403. struct exynos_drm_crtc *exynos_crtc;
  404. exynos_crtc = to_exynos_crtc(crtc);
  405. if (exynos_crtc->manager->type == out_type)
  406. return exynos_crtc->manager->pipe;
  407. }
  408. return -EPERM;
  409. }