vc4_kms.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (C) 2015 Broadcom
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. /**
  9. * DOC: VC4 KMS
  10. *
  11. * This is the general code for implementing KMS mode setting that
  12. * doesn't clearly associate with any of the other objects (plane,
  13. * crtc, HDMI encoder).
  14. */
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_atomic.h>
  17. #include <drm/drm_atomic_helper.h>
  18. #include <drm/drm_crtc_helper.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include <drm/drm_fb_helper.h>
  21. #include <drm/drm_fb_cma_helper.h>
  22. #include <drm/drm_gem_framebuffer_helper.h>
  23. #include "vc4_drv.h"
  24. #include "vc4_regs.h"
  25. struct vc4_ctm_state {
  26. struct drm_private_state base;
  27. struct drm_color_ctm *ctm;
  28. int fifo;
  29. };
  30. static struct vc4_ctm_state *to_vc4_ctm_state(struct drm_private_state *priv)
  31. {
  32. return container_of(priv, struct vc4_ctm_state, base);
  33. }
  34. static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state,
  35. struct drm_private_obj *manager)
  36. {
  37. struct drm_device *dev = state->dev;
  38. struct vc4_dev *vc4 = dev->dev_private;
  39. struct drm_private_state *priv_state;
  40. int ret;
  41. ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx);
  42. if (ret)
  43. return ERR_PTR(ret);
  44. priv_state = drm_atomic_get_private_obj_state(state, manager);
  45. if (IS_ERR(priv_state))
  46. return ERR_CAST(priv_state);
  47. return to_vc4_ctm_state(priv_state);
  48. }
  49. static struct drm_private_state *
  50. vc4_ctm_duplicate_state(struct drm_private_obj *obj)
  51. {
  52. struct vc4_ctm_state *state;
  53. state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
  54. if (!state)
  55. return NULL;
  56. __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
  57. return &state->base;
  58. }
  59. static void vc4_ctm_destroy_state(struct drm_private_obj *obj,
  60. struct drm_private_state *state)
  61. {
  62. struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state);
  63. kfree(ctm_state);
  64. }
  65. static const struct drm_private_state_funcs vc4_ctm_state_funcs = {
  66. .atomic_duplicate_state = vc4_ctm_duplicate_state,
  67. .atomic_destroy_state = vc4_ctm_destroy_state,
  68. };
  69. /* Converts a DRM S31.32 value to the HW S0.9 format. */
  70. static u16 vc4_ctm_s31_32_to_s0_9(u64 in)
  71. {
  72. u16 r;
  73. /* Sign bit. */
  74. r = in & BIT_ULL(63) ? BIT(9) : 0;
  75. if ((in & GENMASK_ULL(62, 32)) > 0) {
  76. /* We have zero integer bits so we can only saturate here. */
  77. r |= GENMASK(8, 0);
  78. } else {
  79. /* Otherwise take the 9 most important fractional bits. */
  80. r |= (in >> 23) & GENMASK(8, 0);
  81. }
  82. return r;
  83. }
  84. static void
  85. vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state)
  86. {
  87. struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state);
  88. struct drm_color_ctm *ctm = ctm_state->ctm;
  89. if (ctm_state->fifo) {
  90. HVS_WRITE(SCALER_OLEDCOEF2,
  91. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]),
  92. SCALER_OLEDCOEF2_R_TO_R) |
  93. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]),
  94. SCALER_OLEDCOEF2_R_TO_G) |
  95. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]),
  96. SCALER_OLEDCOEF2_R_TO_B));
  97. HVS_WRITE(SCALER_OLEDCOEF1,
  98. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]),
  99. SCALER_OLEDCOEF1_G_TO_R) |
  100. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]),
  101. SCALER_OLEDCOEF1_G_TO_G) |
  102. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]),
  103. SCALER_OLEDCOEF1_G_TO_B));
  104. HVS_WRITE(SCALER_OLEDCOEF0,
  105. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]),
  106. SCALER_OLEDCOEF0_B_TO_R) |
  107. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]),
  108. SCALER_OLEDCOEF0_B_TO_G) |
  109. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]),
  110. SCALER_OLEDCOEF0_B_TO_B));
  111. }
  112. HVS_WRITE(SCALER_OLEDOFFS,
  113. VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO));
  114. }
  115. static void
  116. vc4_atomic_complete_commit(struct drm_atomic_state *state)
  117. {
  118. struct drm_device *dev = state->dev;
  119. struct vc4_dev *vc4 = to_vc4_dev(dev);
  120. drm_atomic_helper_wait_for_fences(dev, state, false);
  121. drm_atomic_helper_wait_for_dependencies(state);
  122. drm_atomic_helper_commit_modeset_disables(dev, state);
  123. vc4_ctm_commit(vc4, state);
  124. drm_atomic_helper_commit_planes(dev, state, 0);
  125. drm_atomic_helper_commit_modeset_enables(dev, state);
  126. /* Make sure that drm_atomic_helper_wait_for_vblanks()
  127. * actually waits for vblank. If we're doing a full atomic
  128. * modeset (as opposed to a vc4_update_plane() short circuit),
  129. * then we need to wait for scanout to be done with our
  130. * display lists before we free it and potentially reallocate
  131. * and overwrite the dlist memory with a new modeset.
  132. */
  133. state->legacy_cursor_update = false;
  134. drm_atomic_helper_commit_hw_done(state);
  135. drm_atomic_helper_wait_for_vblanks(dev, state);
  136. drm_atomic_helper_cleanup_planes(dev, state);
  137. drm_atomic_helper_commit_cleanup_done(state);
  138. drm_atomic_state_put(state);
  139. up(&vc4->async_modeset);
  140. }
  141. static void commit_work(struct work_struct *work)
  142. {
  143. struct drm_atomic_state *state = container_of(work,
  144. struct drm_atomic_state,
  145. commit_work);
  146. vc4_atomic_complete_commit(state);
  147. }
  148. /**
  149. * vc4_atomic_commit - commit validated state object
  150. * @dev: DRM device
  151. * @state: the driver state object
  152. * @nonblock: nonblocking commit
  153. *
  154. * This function commits a with drm_atomic_helper_check() pre-validated state
  155. * object. This can still fail when e.g. the framebuffer reservation fails. For
  156. * now this doesn't implement asynchronous commits.
  157. *
  158. * RETURNS
  159. * Zero for success or -errno.
  160. */
  161. static int vc4_atomic_commit(struct drm_device *dev,
  162. struct drm_atomic_state *state,
  163. bool nonblock)
  164. {
  165. struct vc4_dev *vc4 = to_vc4_dev(dev);
  166. int ret;
  167. if (state->async_update) {
  168. ret = down_interruptible(&vc4->async_modeset);
  169. if (ret)
  170. return ret;
  171. ret = drm_atomic_helper_prepare_planes(dev, state);
  172. if (ret) {
  173. up(&vc4->async_modeset);
  174. return ret;
  175. }
  176. drm_atomic_helper_async_commit(dev, state);
  177. drm_atomic_helper_cleanup_planes(dev, state);
  178. up(&vc4->async_modeset);
  179. return 0;
  180. }
  181. ret = drm_atomic_helper_setup_commit(state, nonblock);
  182. if (ret)
  183. return ret;
  184. INIT_WORK(&state->commit_work, commit_work);
  185. ret = down_interruptible(&vc4->async_modeset);
  186. if (ret)
  187. return ret;
  188. ret = drm_atomic_helper_prepare_planes(dev, state);
  189. if (ret) {
  190. up(&vc4->async_modeset);
  191. return ret;
  192. }
  193. if (!nonblock) {
  194. ret = drm_atomic_helper_wait_for_fences(dev, state, true);
  195. if (ret) {
  196. drm_atomic_helper_cleanup_planes(dev, state);
  197. up(&vc4->async_modeset);
  198. return ret;
  199. }
  200. }
  201. /*
  202. * This is the point of no return - everything below never fails except
  203. * when the hw goes bonghits. Which means we can commit the new state on
  204. * the software side now.
  205. */
  206. BUG_ON(drm_atomic_helper_swap_state(state, false) < 0);
  207. /*
  208. * Everything below can be run asynchronously without the need to grab
  209. * any modeset locks at all under one condition: It must be guaranteed
  210. * that the asynchronous work has either been cancelled (if the driver
  211. * supports it, which at least requires that the framebuffers get
  212. * cleaned up with drm_atomic_helper_cleanup_planes()) or completed
  213. * before the new state gets committed on the software side with
  214. * drm_atomic_helper_swap_state().
  215. *
  216. * This scheme allows new atomic state updates to be prepared and
  217. * checked in parallel to the asynchronous completion of the previous
  218. * update. Which is important since compositors need to figure out the
  219. * composition of the next frame right after having submitted the
  220. * current layout.
  221. */
  222. drm_atomic_state_get(state);
  223. if (nonblock)
  224. queue_work(system_unbound_wq, &state->commit_work);
  225. else
  226. vc4_atomic_complete_commit(state);
  227. return 0;
  228. }
  229. static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
  230. struct drm_file *file_priv,
  231. const struct drm_mode_fb_cmd2 *mode_cmd)
  232. {
  233. struct drm_mode_fb_cmd2 mode_cmd_local;
  234. /* If the user didn't specify a modifier, use the
  235. * vc4_set_tiling_ioctl() state for the BO.
  236. */
  237. if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
  238. struct drm_gem_object *gem_obj;
  239. struct vc4_bo *bo;
  240. gem_obj = drm_gem_object_lookup(file_priv,
  241. mode_cmd->handles[0]);
  242. if (!gem_obj) {
  243. DRM_DEBUG("Failed to look up GEM BO %d\n",
  244. mode_cmd->handles[0]);
  245. return ERR_PTR(-ENOENT);
  246. }
  247. bo = to_vc4_bo(gem_obj);
  248. mode_cmd_local = *mode_cmd;
  249. if (bo->t_format) {
  250. mode_cmd_local.modifier[0] =
  251. DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
  252. } else {
  253. mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
  254. }
  255. drm_gem_object_put_unlocked(gem_obj);
  256. mode_cmd = &mode_cmd_local;
  257. }
  258. return drm_gem_fb_create(dev, file_priv, mode_cmd);
  259. }
  260. /* Our CTM has some peculiar limitations: we can only enable it for one CRTC
  261. * at a time and the HW only supports S0.9 scalars. To account for the latter,
  262. * we don't allow userland to set a CTM that we have no hope of approximating.
  263. */
  264. static int
  265. vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
  266. {
  267. struct vc4_dev *vc4 = to_vc4_dev(dev);
  268. struct vc4_ctm_state *ctm_state = NULL;
  269. struct drm_crtc *crtc;
  270. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  271. struct drm_color_ctm *ctm;
  272. int i;
  273. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  274. /* CTM is being disabled. */
  275. if (!new_crtc_state->ctm && old_crtc_state->ctm) {
  276. ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
  277. if (IS_ERR(ctm_state))
  278. return PTR_ERR(ctm_state);
  279. ctm_state->fifo = 0;
  280. }
  281. }
  282. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  283. if (new_crtc_state->ctm == old_crtc_state->ctm)
  284. continue;
  285. if (!ctm_state) {
  286. ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
  287. if (IS_ERR(ctm_state))
  288. return PTR_ERR(ctm_state);
  289. }
  290. /* CTM is being enabled or the matrix changed. */
  291. if (new_crtc_state->ctm) {
  292. /* fifo is 1-based since 0 disables CTM. */
  293. int fifo = to_vc4_crtc(crtc)->channel + 1;
  294. /* Check userland isn't trying to turn on CTM for more
  295. * than one CRTC at a time.
  296. */
  297. if (ctm_state->fifo && ctm_state->fifo != fifo) {
  298. DRM_DEBUG_DRIVER("Too many CTM configured\n");
  299. return -EINVAL;
  300. }
  301. /* Check we can approximate the specified CTM.
  302. * We disallow scalars |c| > 1.0 since the HW has
  303. * no integer bits.
  304. */
  305. ctm = new_crtc_state->ctm->data;
  306. for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) {
  307. u64 val = ctm->matrix[i];
  308. val &= ~BIT_ULL(63);
  309. if (val > BIT_ULL(32))
  310. return -EINVAL;
  311. }
  312. ctm_state->fifo = fifo;
  313. ctm_state->ctm = ctm;
  314. }
  315. }
  316. return 0;
  317. }
  318. static int
  319. vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
  320. {
  321. int ret;
  322. ret = vc4_ctm_atomic_check(dev, state);
  323. if (ret < 0)
  324. return ret;
  325. return drm_atomic_helper_check(dev, state);
  326. }
  327. static const struct drm_mode_config_funcs vc4_mode_funcs = {
  328. .output_poll_changed = drm_fb_helper_output_poll_changed,
  329. .atomic_check = vc4_atomic_check,
  330. .atomic_commit = vc4_atomic_commit,
  331. .fb_create = vc4_fb_create,
  332. };
  333. int vc4_kms_load(struct drm_device *dev)
  334. {
  335. struct vc4_dev *vc4 = to_vc4_dev(dev);
  336. struct vc4_ctm_state *ctm_state;
  337. int ret;
  338. sema_init(&vc4->async_modeset, 1);
  339. /* Set support for vblank irq fast disable, before drm_vblank_init() */
  340. dev->vblank_disable_immediate = true;
  341. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  342. if (ret < 0) {
  343. dev_err(dev->dev, "failed to initialize vblank\n");
  344. return ret;
  345. }
  346. dev->mode_config.max_width = 2048;
  347. dev->mode_config.max_height = 2048;
  348. dev->mode_config.funcs = &vc4_mode_funcs;
  349. dev->mode_config.preferred_depth = 24;
  350. dev->mode_config.async_page_flip = true;
  351. dev->mode_config.allow_fb_modifiers = true;
  352. drm_modeset_lock_init(&vc4->ctm_state_lock);
  353. ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL);
  354. if (!ctm_state)
  355. return -ENOMEM;
  356. drm_atomic_private_obj_init(&vc4->ctm_manager, &ctm_state->base,
  357. &vc4_ctm_state_funcs);
  358. drm_mode_config_reset(dev);
  359. if (dev->mode_config.num_connector)
  360. drm_fb_cma_fbdev_init(dev, 32, 0);
  361. drm_kms_helper_poll_init(dev);
  362. return 0;
  363. }