vc4_kms.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2015 Broadcom
  4. */
  5. /**
  6. * DOC: VC4 KMS
  7. *
  8. * This is the general code for implementing KMS mode setting that
  9. * doesn't clearly associate with any of the other objects (plane,
  10. * crtc, HDMI encoder).
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/sort.h>
  14. #include <drm/drm_atomic.h>
  15. #include <drm/drm_atomic_helper.h>
  16. #include <drm/drm_crtc.h>
  17. #include <drm/drm_fourcc.h>
  18. #include <drm/drm_gem_framebuffer_helper.h>
  19. #include <drm/drm_probe_helper.h>
  20. #include <drm/drm_vblank.h>
  21. #include "vc4_drv.h"
  22. #include "vc4_regs.h"
  23. struct vc4_ctm_state {
  24. struct drm_private_state base;
  25. struct drm_color_ctm *ctm;
  26. int fifo;
  27. };
  28. #define to_vc4_ctm_state(_state) \
  29. container_of_const(_state, struct vc4_ctm_state, base)
  30. struct vc4_load_tracker_state {
  31. struct drm_private_state base;
  32. u64 hvs_load;
  33. u64 membus_load;
  34. };
  35. #define to_vc4_load_tracker_state(_state) \
  36. container_of_const(_state, struct vc4_load_tracker_state, base)
  37. static struct vc4_ctm_state *vc4_get_ctm_state(struct drm_atomic_state *state,
  38. struct drm_private_obj *manager)
  39. {
  40. struct drm_device *dev = state->dev;
  41. struct vc4_dev *vc4 = to_vc4_dev(dev);
  42. struct drm_private_state *priv_state;
  43. int ret;
  44. ret = drm_modeset_lock(&vc4->ctm_state_lock, state->acquire_ctx);
  45. if (ret)
  46. return ERR_PTR(ret);
  47. priv_state = drm_atomic_get_private_obj_state(state, manager);
  48. if (IS_ERR(priv_state))
  49. return ERR_CAST(priv_state);
  50. return to_vc4_ctm_state(priv_state);
  51. }
  52. static struct drm_private_state *
  53. vc4_ctm_duplicate_state(struct drm_private_obj *obj)
  54. {
  55. struct vc4_ctm_state *state;
  56. state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
  57. if (!state)
  58. return NULL;
  59. __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
  60. return &state->base;
  61. }
  62. static void vc4_ctm_destroy_state(struct drm_private_obj *obj,
  63. struct drm_private_state *state)
  64. {
  65. struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(state);
  66. kfree(ctm_state);
  67. }
  68. static const struct drm_private_state_funcs vc4_ctm_state_funcs = {
  69. .atomic_duplicate_state = vc4_ctm_duplicate_state,
  70. .atomic_destroy_state = vc4_ctm_destroy_state,
  71. };
  72. static void vc4_ctm_obj_fini(struct drm_device *dev, void *unused)
  73. {
  74. struct vc4_dev *vc4 = to_vc4_dev(dev);
  75. drm_atomic_private_obj_fini(&vc4->ctm_manager);
  76. }
  77. static int vc4_ctm_obj_init(struct vc4_dev *vc4)
  78. {
  79. struct vc4_ctm_state *ctm_state;
  80. drm_modeset_lock_init(&vc4->ctm_state_lock);
  81. ctm_state = kzalloc(sizeof(*ctm_state), GFP_KERNEL);
  82. if (!ctm_state)
  83. return -ENOMEM;
  84. drm_atomic_private_obj_init(&vc4->base, &vc4->ctm_manager, &ctm_state->base,
  85. &vc4_ctm_state_funcs);
  86. return drmm_add_action_or_reset(&vc4->base, vc4_ctm_obj_fini, NULL);
  87. }
  88. /* Converts a DRM S31.32 value to the HW S0.9 format. */
  89. static u16 vc4_ctm_s31_32_to_s0_9(u64 in)
  90. {
  91. u16 r;
  92. /* Sign bit. */
  93. r = in & BIT_ULL(63) ? BIT(9) : 0;
  94. if ((in & GENMASK_ULL(62, 32)) > 0) {
  95. /* We have zero integer bits so we can only saturate here. */
  96. r |= GENMASK(8, 0);
  97. } else {
  98. /* Otherwise take the 9 most important fractional bits. */
  99. r |= (in >> 23) & GENMASK(8, 0);
  100. }
  101. return r;
  102. }
  103. static void
  104. vc4_ctm_commit(struct vc4_dev *vc4, struct drm_atomic_state *state)
  105. {
  106. struct vc4_hvs *hvs = vc4->hvs;
  107. struct vc4_ctm_state *ctm_state = to_vc4_ctm_state(vc4->ctm_manager.state);
  108. struct drm_color_ctm *ctm = ctm_state->ctm;
  109. if (ctm_state->fifo) {
  110. HVS_WRITE(SCALER_OLEDCOEF2,
  111. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[0]),
  112. SCALER_OLEDCOEF2_R_TO_R) |
  113. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[3]),
  114. SCALER_OLEDCOEF2_R_TO_G) |
  115. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[6]),
  116. SCALER_OLEDCOEF2_R_TO_B));
  117. HVS_WRITE(SCALER_OLEDCOEF1,
  118. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[1]),
  119. SCALER_OLEDCOEF1_G_TO_R) |
  120. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[4]),
  121. SCALER_OLEDCOEF1_G_TO_G) |
  122. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[7]),
  123. SCALER_OLEDCOEF1_G_TO_B));
  124. HVS_WRITE(SCALER_OLEDCOEF0,
  125. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[2]),
  126. SCALER_OLEDCOEF0_B_TO_R) |
  127. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[5]),
  128. SCALER_OLEDCOEF0_B_TO_G) |
  129. VC4_SET_FIELD(vc4_ctm_s31_32_to_s0_9(ctm->matrix[8]),
  130. SCALER_OLEDCOEF0_B_TO_B));
  131. }
  132. HVS_WRITE(SCALER_OLEDOFFS,
  133. VC4_SET_FIELD(ctm_state->fifo, SCALER_OLEDOFFS_DISPFIFO));
  134. }
  135. struct vc4_hvs_state *
  136. vc4_hvs_get_new_global_state(const struct drm_atomic_state *state)
  137. {
  138. struct vc4_dev *vc4 = to_vc4_dev(state->dev);
  139. struct drm_private_state *priv_state;
  140. priv_state = drm_atomic_get_new_private_obj_state(state, &vc4->hvs_channels);
  141. if (!priv_state)
  142. return ERR_PTR(-EINVAL);
  143. return to_vc4_hvs_state(priv_state);
  144. }
  145. struct vc4_hvs_state *
  146. vc4_hvs_get_old_global_state(const struct drm_atomic_state *state)
  147. {
  148. struct vc4_dev *vc4 = to_vc4_dev(state->dev);
  149. struct drm_private_state *priv_state;
  150. priv_state = drm_atomic_get_old_private_obj_state(state, &vc4->hvs_channels);
  151. if (!priv_state)
  152. return ERR_PTR(-EINVAL);
  153. return to_vc4_hvs_state(priv_state);
  154. }
  155. struct vc4_hvs_state *
  156. vc4_hvs_get_global_state(struct drm_atomic_state *state)
  157. {
  158. struct vc4_dev *vc4 = to_vc4_dev(state->dev);
  159. struct drm_private_state *priv_state;
  160. priv_state = drm_atomic_get_private_obj_state(state, &vc4->hvs_channels);
  161. if (IS_ERR(priv_state))
  162. return ERR_CAST(priv_state);
  163. return to_vc4_hvs_state(priv_state);
  164. }
  165. static void vc4_hvs_pv_muxing_commit(struct vc4_dev *vc4,
  166. struct drm_atomic_state *state)
  167. {
  168. struct vc4_hvs *hvs = vc4->hvs;
  169. struct drm_crtc_state *crtc_state;
  170. struct drm_crtc *crtc;
  171. unsigned int i;
  172. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  173. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  174. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
  175. u32 dispctrl;
  176. u32 dsp3_mux;
  177. if (!crtc_state->active)
  178. continue;
  179. if (vc4_state->assigned_channel != 2)
  180. continue;
  181. /*
  182. * SCALER_DISPCTRL_DSP3 = X, where X < 2 means 'connect DSP3 to
  183. * FIFO X'.
  184. * SCALER_DISPCTRL_DSP3 = 3 means 'disable DSP 3'.
  185. *
  186. * DSP3 is connected to FIFO2 unless the transposer is
  187. * enabled. In this case, FIFO 2 is directly accessed by the
  188. * TXP IP, and we need to disable the FIFO2 -> pixelvalve1
  189. * route.
  190. */
  191. if (vc4_crtc->feeds_txp)
  192. dsp3_mux = VC4_SET_FIELD(3, SCALER_DISPCTRL_DSP3_MUX);
  193. else
  194. dsp3_mux = VC4_SET_FIELD(2, SCALER_DISPCTRL_DSP3_MUX);
  195. dispctrl = HVS_READ(SCALER_DISPCTRL) &
  196. ~SCALER_DISPCTRL_DSP3_MUX_MASK;
  197. HVS_WRITE(SCALER_DISPCTRL, dispctrl | dsp3_mux);
  198. }
  199. }
  200. static void vc5_hvs_pv_muxing_commit(struct vc4_dev *vc4,
  201. struct drm_atomic_state *state)
  202. {
  203. struct vc4_hvs *hvs = vc4->hvs;
  204. struct drm_crtc_state *crtc_state;
  205. struct drm_crtc *crtc;
  206. unsigned char mux;
  207. unsigned int i;
  208. u32 reg;
  209. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  210. struct vc4_crtc_state *vc4_state = to_vc4_crtc_state(crtc_state);
  211. struct vc4_crtc *vc4_crtc = to_vc4_crtc(crtc);
  212. unsigned int channel = vc4_state->assigned_channel;
  213. if (!vc4_state->update_muxing)
  214. continue;
  215. switch (vc4_crtc->data->hvs_output) {
  216. case 2:
  217. drm_WARN_ON(&vc4->base,
  218. VC4_GET_FIELD(HVS_READ(SCALER_DISPCTRL),
  219. SCALER_DISPCTRL_DSP3_MUX) == channel);
  220. mux = (channel == 2) ? 0 : 1;
  221. reg = HVS_READ(SCALER_DISPECTRL);
  222. HVS_WRITE(SCALER_DISPECTRL,
  223. (reg & ~SCALER_DISPECTRL_DSP2_MUX_MASK) |
  224. VC4_SET_FIELD(mux, SCALER_DISPECTRL_DSP2_MUX));
  225. break;
  226. case 3:
  227. if (channel == VC4_HVS_CHANNEL_DISABLED)
  228. mux = 3;
  229. else
  230. mux = channel;
  231. reg = HVS_READ(SCALER_DISPCTRL);
  232. HVS_WRITE(SCALER_DISPCTRL,
  233. (reg & ~SCALER_DISPCTRL_DSP3_MUX_MASK) |
  234. VC4_SET_FIELD(mux, SCALER_DISPCTRL_DSP3_MUX));
  235. break;
  236. case 4:
  237. if (channel == VC4_HVS_CHANNEL_DISABLED)
  238. mux = 3;
  239. else
  240. mux = channel;
  241. reg = HVS_READ(SCALER_DISPEOLN);
  242. HVS_WRITE(SCALER_DISPEOLN,
  243. (reg & ~SCALER_DISPEOLN_DSP4_MUX_MASK) |
  244. VC4_SET_FIELD(mux, SCALER_DISPEOLN_DSP4_MUX));
  245. break;
  246. case 5:
  247. if (channel == VC4_HVS_CHANNEL_DISABLED)
  248. mux = 3;
  249. else
  250. mux = channel;
  251. reg = HVS_READ(SCALER_DISPDITHER);
  252. HVS_WRITE(SCALER_DISPDITHER,
  253. (reg & ~SCALER_DISPDITHER_DSP5_MUX_MASK) |
  254. VC4_SET_FIELD(mux, SCALER_DISPDITHER_DSP5_MUX));
  255. break;
  256. default:
  257. break;
  258. }
  259. }
  260. }
  261. static void vc4_atomic_commit_tail(struct drm_atomic_state *state)
  262. {
  263. struct drm_device *dev = state->dev;
  264. struct vc4_dev *vc4 = to_vc4_dev(dev);
  265. struct vc4_hvs *hvs = vc4->hvs;
  266. struct drm_crtc_state *new_crtc_state;
  267. struct vc4_hvs_state *new_hvs_state;
  268. struct drm_crtc *crtc;
  269. struct vc4_hvs_state *old_hvs_state;
  270. unsigned int channel;
  271. int i;
  272. old_hvs_state = vc4_hvs_get_old_global_state(state);
  273. if (WARN_ON(IS_ERR(old_hvs_state)))
  274. return;
  275. new_hvs_state = vc4_hvs_get_new_global_state(state);
  276. if (WARN_ON(IS_ERR(new_hvs_state)))
  277. return;
  278. for_each_new_crtc_in_state(state, crtc, new_crtc_state, i) {
  279. struct vc4_crtc_state *vc4_crtc_state;
  280. if (!new_crtc_state->commit)
  281. continue;
  282. vc4_crtc_state = to_vc4_crtc_state(new_crtc_state);
  283. vc4_hvs_mask_underrun(hvs, vc4_crtc_state->assigned_channel);
  284. }
  285. for (channel = 0; channel < HVS_NUM_CHANNELS; channel++) {
  286. struct drm_crtc_commit *commit;
  287. int ret;
  288. if (!old_hvs_state->fifo_state[channel].in_use)
  289. continue;
  290. commit = old_hvs_state->fifo_state[channel].pending_commit;
  291. if (!commit)
  292. continue;
  293. ret = drm_crtc_commit_wait(commit);
  294. if (ret)
  295. drm_err(dev, "Timed out waiting for commit\n");
  296. drm_crtc_commit_put(commit);
  297. old_hvs_state->fifo_state[channel].pending_commit = NULL;
  298. }
  299. if (vc4->gen == VC4_GEN_5) {
  300. unsigned long state_rate = max(old_hvs_state->core_clock_rate,
  301. new_hvs_state->core_clock_rate);
  302. unsigned long core_rate = clamp_t(unsigned long, state_rate,
  303. 500000000, hvs->max_core_rate);
  304. drm_dbg(dev, "Raising the core clock at %lu Hz\n", core_rate);
  305. /*
  306. * Do a temporary request on the core clock during the
  307. * modeset.
  308. */
  309. WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate));
  310. }
  311. drm_atomic_helper_commit_modeset_disables(dev, state);
  312. vc4_ctm_commit(vc4, state);
  313. if (vc4->gen == VC4_GEN_5)
  314. vc5_hvs_pv_muxing_commit(vc4, state);
  315. else
  316. vc4_hvs_pv_muxing_commit(vc4, state);
  317. drm_atomic_helper_commit_planes(dev, state,
  318. DRM_PLANE_COMMIT_ACTIVE_ONLY);
  319. drm_atomic_helper_commit_modeset_enables(dev, state);
  320. drm_atomic_helper_fake_vblank(state);
  321. drm_atomic_helper_commit_hw_done(state);
  322. drm_atomic_helper_wait_for_flip_done(dev, state);
  323. drm_atomic_helper_cleanup_planes(dev, state);
  324. if (vc4->gen == VC4_GEN_5) {
  325. unsigned long core_rate = min_t(unsigned long,
  326. hvs->max_core_rate,
  327. new_hvs_state->core_clock_rate);
  328. drm_dbg(dev, "Running the core clock at %lu Hz\n", core_rate);
  329. /*
  330. * Request a clock rate based on the current HVS
  331. * requirements.
  332. */
  333. WARN_ON(clk_set_min_rate(hvs->core_clk, core_rate));
  334. drm_dbg(dev, "Core clock actual rate: %lu Hz\n",
  335. clk_get_rate(hvs->core_clk));
  336. }
  337. }
  338. static int vc4_atomic_commit_setup(struct drm_atomic_state *state)
  339. {
  340. struct drm_crtc_state *crtc_state;
  341. struct vc4_hvs_state *hvs_state;
  342. struct drm_crtc *crtc;
  343. unsigned int i;
  344. hvs_state = vc4_hvs_get_new_global_state(state);
  345. if (WARN_ON(IS_ERR(hvs_state)))
  346. return PTR_ERR(hvs_state);
  347. for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
  348. struct vc4_crtc_state *vc4_crtc_state =
  349. to_vc4_crtc_state(crtc_state);
  350. unsigned int channel =
  351. vc4_crtc_state->assigned_channel;
  352. if (channel == VC4_HVS_CHANNEL_DISABLED)
  353. continue;
  354. if (!hvs_state->fifo_state[channel].in_use)
  355. continue;
  356. hvs_state->fifo_state[channel].pending_commit =
  357. drm_crtc_commit_get(crtc_state->commit);
  358. }
  359. return 0;
  360. }
  361. static struct drm_framebuffer *vc4_fb_create(struct drm_device *dev,
  362. struct drm_file *file_priv,
  363. const struct drm_mode_fb_cmd2 *mode_cmd)
  364. {
  365. struct vc4_dev *vc4 = to_vc4_dev(dev);
  366. struct drm_mode_fb_cmd2 mode_cmd_local;
  367. if (WARN_ON_ONCE(vc4->gen == VC4_GEN_5))
  368. return ERR_PTR(-ENODEV);
  369. /* If the user didn't specify a modifier, use the
  370. * vc4_set_tiling_ioctl() state for the BO.
  371. */
  372. if (!(mode_cmd->flags & DRM_MODE_FB_MODIFIERS)) {
  373. struct drm_gem_object *gem_obj;
  374. struct vc4_bo *bo;
  375. gem_obj = drm_gem_object_lookup(file_priv,
  376. mode_cmd->handles[0]);
  377. if (!gem_obj) {
  378. DRM_DEBUG("Failed to look up GEM BO %d\n",
  379. mode_cmd->handles[0]);
  380. return ERR_PTR(-ENOENT);
  381. }
  382. bo = to_vc4_bo(gem_obj);
  383. mode_cmd_local = *mode_cmd;
  384. if (bo->t_format) {
  385. mode_cmd_local.modifier[0] =
  386. DRM_FORMAT_MOD_BROADCOM_VC4_T_TILED;
  387. } else {
  388. mode_cmd_local.modifier[0] = DRM_FORMAT_MOD_NONE;
  389. }
  390. drm_gem_object_put(gem_obj);
  391. mode_cmd = &mode_cmd_local;
  392. }
  393. return drm_gem_fb_create(dev, file_priv, mode_cmd);
  394. }
  395. /* Our CTM has some peculiar limitations: we can only enable it for one CRTC
  396. * at a time and the HW only supports S0.9 scalars. To account for the latter,
  397. * we don't allow userland to set a CTM that we have no hope of approximating.
  398. */
  399. static int
  400. vc4_ctm_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
  401. {
  402. struct vc4_dev *vc4 = to_vc4_dev(dev);
  403. struct vc4_ctm_state *ctm_state = NULL;
  404. struct drm_crtc *crtc;
  405. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  406. struct drm_color_ctm *ctm;
  407. int i;
  408. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  409. /* CTM is being disabled. */
  410. if (!new_crtc_state->ctm && old_crtc_state->ctm) {
  411. ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
  412. if (IS_ERR(ctm_state))
  413. return PTR_ERR(ctm_state);
  414. ctm_state->fifo = 0;
  415. }
  416. }
  417. for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
  418. if (new_crtc_state->ctm == old_crtc_state->ctm)
  419. continue;
  420. if (!ctm_state) {
  421. ctm_state = vc4_get_ctm_state(state, &vc4->ctm_manager);
  422. if (IS_ERR(ctm_state))
  423. return PTR_ERR(ctm_state);
  424. }
  425. /* CTM is being enabled or the matrix changed. */
  426. if (new_crtc_state->ctm) {
  427. struct vc4_crtc_state *vc4_crtc_state =
  428. to_vc4_crtc_state(new_crtc_state);
  429. /* fifo is 1-based since 0 disables CTM. */
  430. int fifo = vc4_crtc_state->assigned_channel + 1;
  431. /* Check userland isn't trying to turn on CTM for more
  432. * than one CRTC at a time.
  433. */
  434. if (ctm_state->fifo && ctm_state->fifo != fifo) {
  435. DRM_DEBUG_DRIVER("Too many CTM configured\n");
  436. return -EINVAL;
  437. }
  438. /* Check we can approximate the specified CTM.
  439. * We disallow scalars |c| > 1.0 since the HW has
  440. * no integer bits.
  441. */
  442. ctm = new_crtc_state->ctm->data;
  443. for (i = 0; i < ARRAY_SIZE(ctm->matrix); i++) {
  444. u64 val = ctm->matrix[i];
  445. val &= ~BIT_ULL(63);
  446. if (val > BIT_ULL(32))
  447. return -EINVAL;
  448. }
  449. ctm_state->fifo = fifo;
  450. ctm_state->ctm = ctm;
  451. }
  452. }
  453. return 0;
  454. }
  455. static int vc4_load_tracker_atomic_check(struct drm_atomic_state *state)
  456. {
  457. struct drm_plane_state *old_plane_state, *new_plane_state;
  458. struct vc4_dev *vc4 = to_vc4_dev(state->dev);
  459. struct vc4_load_tracker_state *load_state;
  460. struct drm_private_state *priv_state;
  461. struct drm_plane *plane;
  462. int i;
  463. priv_state = drm_atomic_get_private_obj_state(state,
  464. &vc4->load_tracker);
  465. if (IS_ERR(priv_state))
  466. return PTR_ERR(priv_state);
  467. load_state = to_vc4_load_tracker_state(priv_state);
  468. for_each_oldnew_plane_in_state(state, plane, old_plane_state,
  469. new_plane_state, i) {
  470. struct vc4_plane_state *vc4_plane_state;
  471. if (old_plane_state->fb && old_plane_state->crtc) {
  472. vc4_plane_state = to_vc4_plane_state(old_plane_state);
  473. load_state->membus_load -= vc4_plane_state->membus_load;
  474. load_state->hvs_load -= vc4_plane_state->hvs_load;
  475. }
  476. if (new_plane_state->fb && new_plane_state->crtc) {
  477. vc4_plane_state = to_vc4_plane_state(new_plane_state);
  478. load_state->membus_load += vc4_plane_state->membus_load;
  479. load_state->hvs_load += vc4_plane_state->hvs_load;
  480. }
  481. }
  482. /* Don't check the load when the tracker is disabled. */
  483. if (!vc4->load_tracker_enabled)
  484. return 0;
  485. /* The absolute limit is 2Gbyte/sec, but let's take a margin to let
  486. * the system work when other blocks are accessing the memory.
  487. */
  488. if (load_state->membus_load > SZ_1G + SZ_512M)
  489. return -ENOSPC;
  490. /* HVS clock is supposed to run @ 250Mhz, let's take a margin and
  491. * consider the maximum number of cycles is 240M.
  492. */
  493. if (load_state->hvs_load > 240000000ULL)
  494. return -ENOSPC;
  495. return 0;
  496. }
  497. static struct drm_private_state *
  498. vc4_load_tracker_duplicate_state(struct drm_private_obj *obj)
  499. {
  500. struct vc4_load_tracker_state *state;
  501. state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
  502. if (!state)
  503. return NULL;
  504. __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
  505. return &state->base;
  506. }
  507. static void vc4_load_tracker_destroy_state(struct drm_private_obj *obj,
  508. struct drm_private_state *state)
  509. {
  510. struct vc4_load_tracker_state *load_state;
  511. load_state = to_vc4_load_tracker_state(state);
  512. kfree(load_state);
  513. }
  514. static const struct drm_private_state_funcs vc4_load_tracker_state_funcs = {
  515. .atomic_duplicate_state = vc4_load_tracker_duplicate_state,
  516. .atomic_destroy_state = vc4_load_tracker_destroy_state,
  517. };
  518. static void vc4_load_tracker_obj_fini(struct drm_device *dev, void *unused)
  519. {
  520. struct vc4_dev *vc4 = to_vc4_dev(dev);
  521. drm_atomic_private_obj_fini(&vc4->load_tracker);
  522. }
  523. static int vc4_load_tracker_obj_init(struct vc4_dev *vc4)
  524. {
  525. struct vc4_load_tracker_state *load_state;
  526. load_state = kzalloc(sizeof(*load_state), GFP_KERNEL);
  527. if (!load_state)
  528. return -ENOMEM;
  529. drm_atomic_private_obj_init(&vc4->base, &vc4->load_tracker,
  530. &load_state->base,
  531. &vc4_load_tracker_state_funcs);
  532. return drmm_add_action_or_reset(&vc4->base, vc4_load_tracker_obj_fini, NULL);
  533. }
  534. static struct drm_private_state *
  535. vc4_hvs_channels_duplicate_state(struct drm_private_obj *obj)
  536. {
  537. struct vc4_hvs_state *old_state = to_vc4_hvs_state(obj->state);
  538. struct vc4_hvs_state *state;
  539. unsigned int i;
  540. state = kzalloc(sizeof(*state), GFP_KERNEL);
  541. if (!state)
  542. return NULL;
  543. __drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
  544. for (i = 0; i < HVS_NUM_CHANNELS; i++) {
  545. state->fifo_state[i].in_use = old_state->fifo_state[i].in_use;
  546. state->fifo_state[i].fifo_load = old_state->fifo_state[i].fifo_load;
  547. }
  548. state->core_clock_rate = old_state->core_clock_rate;
  549. return &state->base;
  550. }
  551. static void vc4_hvs_channels_destroy_state(struct drm_private_obj *obj,
  552. struct drm_private_state *state)
  553. {
  554. struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
  555. unsigned int i;
  556. for (i = 0; i < HVS_NUM_CHANNELS; i++) {
  557. if (!hvs_state->fifo_state[i].pending_commit)
  558. continue;
  559. drm_crtc_commit_put(hvs_state->fifo_state[i].pending_commit);
  560. }
  561. kfree(hvs_state);
  562. }
  563. static void vc4_hvs_channels_print_state(struct drm_printer *p,
  564. const struct drm_private_state *state)
  565. {
  566. const struct vc4_hvs_state *hvs_state = to_vc4_hvs_state(state);
  567. unsigned int i;
  568. drm_printf(p, "HVS State\n");
  569. drm_printf(p, "\tCore Clock Rate: %lu\n", hvs_state->core_clock_rate);
  570. for (i = 0; i < HVS_NUM_CHANNELS; i++) {
  571. drm_printf(p, "\tChannel %d\n", i);
  572. drm_printf(p, "\t\tin use=%d\n", hvs_state->fifo_state[i].in_use);
  573. drm_printf(p, "\t\tload=%lu\n", hvs_state->fifo_state[i].fifo_load);
  574. }
  575. }
  576. static const struct drm_private_state_funcs vc4_hvs_state_funcs = {
  577. .atomic_duplicate_state = vc4_hvs_channels_duplicate_state,
  578. .atomic_destroy_state = vc4_hvs_channels_destroy_state,
  579. .atomic_print_state = vc4_hvs_channels_print_state,
  580. };
  581. static void vc4_hvs_channels_obj_fini(struct drm_device *dev, void *unused)
  582. {
  583. struct vc4_dev *vc4 = to_vc4_dev(dev);
  584. drm_atomic_private_obj_fini(&vc4->hvs_channels);
  585. }
  586. static int vc4_hvs_channels_obj_init(struct vc4_dev *vc4)
  587. {
  588. struct vc4_hvs_state *state;
  589. state = kzalloc(sizeof(*state), GFP_KERNEL);
  590. if (!state)
  591. return -ENOMEM;
  592. drm_atomic_private_obj_init(&vc4->base, &vc4->hvs_channels,
  593. &state->base,
  594. &vc4_hvs_state_funcs);
  595. return drmm_add_action_or_reset(&vc4->base, vc4_hvs_channels_obj_fini, NULL);
  596. }
  597. static int cmp_vc4_crtc_hvs_output(const void *a, const void *b)
  598. {
  599. const struct vc4_crtc *crtc_a =
  600. to_vc4_crtc(*(const struct drm_crtc **)a);
  601. const struct vc4_crtc_data *data_a =
  602. vc4_crtc_to_vc4_crtc_data(crtc_a);
  603. const struct vc4_crtc *crtc_b =
  604. to_vc4_crtc(*(const struct drm_crtc **)b);
  605. const struct vc4_crtc_data *data_b =
  606. vc4_crtc_to_vc4_crtc_data(crtc_b);
  607. return data_a->hvs_output - data_b->hvs_output;
  608. }
  609. /*
  610. * The BCM2711 HVS has up to 7 outputs connected to the pixelvalves and
  611. * the TXP (and therefore all the CRTCs found on that platform).
  612. *
  613. * The naive (and our initial) implementation would just iterate over
  614. * all the active CRTCs, try to find a suitable FIFO, and then remove it
  615. * from the pool of available FIFOs. However, there are a few corner
  616. * cases that need to be considered:
  617. *
  618. * - When running in a dual-display setup (so with two CRTCs involved),
  619. * we can update the state of a single CRTC (for example by changing
  620. * its mode using xrandr under X11) without affecting the other. In
  621. * this case, the other CRTC wouldn't be in the state at all, so we
  622. * need to consider all the running CRTCs in the DRM device to assign
  623. * a FIFO, not just the one in the state.
  624. *
  625. * - To fix the above, we can't use drm_atomic_get_crtc_state on all
  626. * enabled CRTCs to pull their CRTC state into the global state, since
  627. * a page flip would start considering their vblank to complete. Since
  628. * we don't have a guarantee that they are actually active, that
  629. * vblank might never happen, and shouldn't even be considered if we
  630. * want to do a page flip on a single CRTC. That can be tested by
  631. * doing a modetest -v first on HDMI1 and then on HDMI0.
  632. *
  633. * - Since we need the pixelvalve to be disabled and enabled back when
  634. * the FIFO is changed, we should keep the FIFO assigned for as long
  635. * as the CRTC is enabled, only considering it free again once that
  636. * CRTC has been disabled. This can be tested by booting X11 on a
  637. * single display, and changing the resolution down and then back up.
  638. */
  639. static int vc4_pv_muxing_atomic_check(struct drm_device *dev,
  640. struct drm_atomic_state *state)
  641. {
  642. struct vc4_hvs_state *hvs_new_state;
  643. struct drm_crtc **sorted_crtcs;
  644. struct drm_crtc *crtc;
  645. unsigned int unassigned_channels = 0;
  646. unsigned int i;
  647. int ret;
  648. hvs_new_state = vc4_hvs_get_global_state(state);
  649. if (IS_ERR(hvs_new_state))
  650. return PTR_ERR(hvs_new_state);
  651. for (i = 0; i < ARRAY_SIZE(hvs_new_state->fifo_state); i++)
  652. if (!hvs_new_state->fifo_state[i].in_use)
  653. unassigned_channels |= BIT(i);
  654. /*
  655. * The problem we have to solve here is that we have up to 7
  656. * encoders, connected to up to 6 CRTCs.
  657. *
  658. * Those CRTCs, depending on the instance, can be routed to 1, 2
  659. * or 3 HVS FIFOs, and we need to set the muxing between FIFOs and
  660. * outputs in the HVS accordingly.
  661. *
  662. * It would be pretty hard to come up with an algorithm that
  663. * would generically solve this. However, the current routing
  664. * trees we support allow us to simplify a bit the problem.
  665. *
  666. * Indeed, with the current supported layouts, if we try to
  667. * assign in the ascending crtc index order the FIFOs, we can't
  668. * fall into the situation where an earlier CRTC that had
  669. * multiple routes is assigned one that was the only option for
  670. * a later CRTC.
  671. *
  672. * If the layout changes and doesn't give us that in the future,
  673. * we will need to have something smarter, but it works so far.
  674. */
  675. sorted_crtcs = kmalloc_array(dev->num_crtcs, sizeof(*sorted_crtcs), GFP_KERNEL);
  676. if (!sorted_crtcs)
  677. return -ENOMEM;
  678. i = 0;
  679. drm_for_each_crtc(crtc, dev)
  680. sorted_crtcs[i++] = crtc;
  681. sort(sorted_crtcs, i, sizeof(*sorted_crtcs), cmp_vc4_crtc_hvs_output, NULL);
  682. for (i = 0; i < dev->num_crtcs; i++) {
  683. struct vc4_crtc_state *old_vc4_crtc_state, *new_vc4_crtc_state;
  684. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  685. struct vc4_crtc *vc4_crtc;
  686. unsigned int matching_channels;
  687. unsigned int channel;
  688. crtc = sorted_crtcs[i];
  689. if (!crtc)
  690. continue;
  691. vc4_crtc = to_vc4_crtc(crtc);
  692. old_crtc_state = drm_atomic_get_old_crtc_state(state, crtc);
  693. if (!old_crtc_state)
  694. continue;
  695. old_vc4_crtc_state = to_vc4_crtc_state(old_crtc_state);
  696. new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  697. if (!new_crtc_state)
  698. continue;
  699. new_vc4_crtc_state = to_vc4_crtc_state(new_crtc_state);
  700. drm_dbg(dev, "%s: Trying to find a channel.\n", crtc->name);
  701. /* Nothing to do here, let's skip it */
  702. if (old_crtc_state->enable == new_crtc_state->enable) {
  703. if (new_crtc_state->enable)
  704. drm_dbg(dev, "%s: Already enabled, reusing channel %d.\n",
  705. crtc->name, new_vc4_crtc_state->assigned_channel);
  706. else
  707. drm_dbg(dev, "%s: Disabled, ignoring.\n", crtc->name);
  708. continue;
  709. }
  710. /* Muxing will need to be modified, mark it as such */
  711. new_vc4_crtc_state->update_muxing = true;
  712. /* If we're disabling our CRTC, we put back our channel */
  713. if (!new_crtc_state->enable) {
  714. channel = old_vc4_crtc_state->assigned_channel;
  715. drm_dbg(dev, "%s: Disabling, Freeing channel %d\n",
  716. crtc->name, channel);
  717. hvs_new_state->fifo_state[channel].in_use = false;
  718. new_vc4_crtc_state->assigned_channel = VC4_HVS_CHANNEL_DISABLED;
  719. continue;
  720. }
  721. matching_channels = unassigned_channels & vc4_crtc->data->hvs_available_channels;
  722. if (!matching_channels) {
  723. ret = -EINVAL;
  724. goto err_free_crtc_array;
  725. }
  726. channel = ffs(matching_channels) - 1;
  727. drm_dbg(dev, "Assigned HVS channel %d to CRTC %s\n", channel, crtc->name);
  728. new_vc4_crtc_state->assigned_channel = channel;
  729. unassigned_channels &= ~BIT(channel);
  730. hvs_new_state->fifo_state[channel].in_use = true;
  731. }
  732. kfree(sorted_crtcs);
  733. return 0;
  734. err_free_crtc_array:
  735. kfree(sorted_crtcs);
  736. return ret;
  737. }
  738. static int
  739. vc4_core_clock_atomic_check(struct drm_atomic_state *state)
  740. {
  741. struct vc4_dev *vc4 = to_vc4_dev(state->dev);
  742. struct drm_private_state *priv_state;
  743. struct vc4_hvs_state *hvs_new_state;
  744. struct vc4_load_tracker_state *load_state;
  745. struct drm_crtc_state *old_crtc_state, *new_crtc_state;
  746. struct drm_crtc *crtc;
  747. unsigned int num_outputs;
  748. unsigned long pixel_rate;
  749. unsigned long cob_rate;
  750. unsigned int i;
  751. priv_state = drm_atomic_get_private_obj_state(state,
  752. &vc4->load_tracker);
  753. if (IS_ERR(priv_state))
  754. return PTR_ERR(priv_state);
  755. load_state = to_vc4_load_tracker_state(priv_state);
  756. hvs_new_state = vc4_hvs_get_global_state(state);
  757. if (IS_ERR(hvs_new_state))
  758. return PTR_ERR(hvs_new_state);
  759. for_each_oldnew_crtc_in_state(state, crtc,
  760. old_crtc_state,
  761. new_crtc_state,
  762. i) {
  763. if (old_crtc_state->active) {
  764. struct vc4_crtc_state *old_vc4_state =
  765. to_vc4_crtc_state(old_crtc_state);
  766. unsigned int channel = old_vc4_state->assigned_channel;
  767. hvs_new_state->fifo_state[channel].fifo_load = 0;
  768. }
  769. if (new_crtc_state->active) {
  770. struct vc4_crtc_state *new_vc4_state =
  771. to_vc4_crtc_state(new_crtc_state);
  772. unsigned int channel = new_vc4_state->assigned_channel;
  773. hvs_new_state->fifo_state[channel].fifo_load =
  774. new_vc4_state->hvs_load;
  775. }
  776. }
  777. cob_rate = 0;
  778. num_outputs = 0;
  779. for (i = 0; i < HVS_NUM_CHANNELS; i++) {
  780. if (!hvs_new_state->fifo_state[i].in_use)
  781. continue;
  782. num_outputs++;
  783. cob_rate = max_t(unsigned long,
  784. hvs_new_state->fifo_state[i].fifo_load,
  785. cob_rate);
  786. }
  787. pixel_rate = load_state->hvs_load;
  788. if (num_outputs > 1) {
  789. pixel_rate = (pixel_rate * 40) / 100;
  790. } else {
  791. pixel_rate = (pixel_rate * 60) / 100;
  792. }
  793. hvs_new_state->core_clock_rate = max(cob_rate, pixel_rate);
  794. return 0;
  795. }
  796. static int
  797. vc4_atomic_check(struct drm_device *dev, struct drm_atomic_state *state)
  798. {
  799. int ret;
  800. ret = vc4_pv_muxing_atomic_check(dev, state);
  801. if (ret)
  802. return ret;
  803. ret = vc4_ctm_atomic_check(dev, state);
  804. if (ret < 0)
  805. return ret;
  806. ret = drm_atomic_helper_check(dev, state);
  807. if (ret)
  808. return ret;
  809. ret = vc4_load_tracker_atomic_check(state);
  810. if (ret)
  811. return ret;
  812. return vc4_core_clock_atomic_check(state);
  813. }
  814. static struct drm_mode_config_helper_funcs vc4_mode_config_helpers = {
  815. .atomic_commit_setup = vc4_atomic_commit_setup,
  816. .atomic_commit_tail = vc4_atomic_commit_tail,
  817. };
  818. static const struct drm_mode_config_funcs vc4_mode_funcs = {
  819. .atomic_check = vc4_atomic_check,
  820. .atomic_commit = drm_atomic_helper_commit,
  821. .fb_create = vc4_fb_create,
  822. };
  823. static const struct drm_mode_config_funcs vc5_mode_funcs = {
  824. .atomic_check = vc4_atomic_check,
  825. .atomic_commit = drm_atomic_helper_commit,
  826. .fb_create = drm_gem_fb_create,
  827. };
  828. int vc4_kms_load(struct drm_device *dev)
  829. {
  830. struct vc4_dev *vc4 = to_vc4_dev(dev);
  831. int ret;
  832. /*
  833. * The limits enforced by the load tracker aren't relevant for
  834. * the BCM2711, but the load tracker computations are used for
  835. * the core clock rate calculation.
  836. */
  837. if (vc4->gen == VC4_GEN_4) {
  838. /* Start with the load tracker enabled. Can be
  839. * disabled through the debugfs load_tracker file.
  840. */
  841. vc4->load_tracker_enabled = true;
  842. }
  843. /* Set support for vblank irq fast disable, before drm_vblank_init() */
  844. dev->vblank_disable_immediate = true;
  845. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  846. if (ret < 0) {
  847. dev_err(dev->dev, "failed to initialize vblank\n");
  848. return ret;
  849. }
  850. if (vc4->gen == VC4_GEN_5) {
  851. dev->mode_config.max_width = 7680;
  852. dev->mode_config.max_height = 7680;
  853. } else {
  854. dev->mode_config.max_width = 2048;
  855. dev->mode_config.max_height = 2048;
  856. }
  857. dev->mode_config.funcs = (vc4->gen > VC4_GEN_4) ? &vc5_mode_funcs : &vc4_mode_funcs;
  858. dev->mode_config.helper_private = &vc4_mode_config_helpers;
  859. dev->mode_config.preferred_depth = 24;
  860. dev->mode_config.async_page_flip = true;
  861. dev->mode_config.normalize_zpos = true;
  862. ret = vc4_ctm_obj_init(vc4);
  863. if (ret)
  864. return ret;
  865. ret = vc4_load_tracker_obj_init(vc4);
  866. if (ret)
  867. return ret;
  868. ret = vc4_hvs_channels_obj_init(vc4);
  869. if (ret)
  870. return ret;
  871. drm_mode_config_reset(dev);
  872. drm_kms_helper_poll_init(dev);
  873. return 0;
  874. }