malidp_crtc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
  4. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  5. *
  6. * ARM Mali DP500/DP550/DP650 driver (crtc operations)
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/pm_runtime.h>
  10. #include <video/videomode.h>
  11. #include <drm/drm_atomic.h>
  12. #include <drm/drm_atomic_helper.h>
  13. #include <drm/drm_crtc.h>
  14. #include <drm/drm_framebuffer.h>
  15. #include <drm/drm_print.h>
  16. #include <drm/drm_probe_helper.h>
  17. #include <drm/drm_vblank.h>
  18. #include "malidp_drv.h"
  19. #include "malidp_hw.h"
  20. static enum drm_mode_status malidp_crtc_mode_valid(struct drm_crtc *crtc,
  21. const struct drm_display_mode *mode)
  22. {
  23. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  24. struct malidp_hw_device *hwdev = malidp->dev;
  25. /*
  26. * check that the hardware can drive the required clock rate,
  27. * but skip the check if the clock is meant to be disabled (req_rate = 0)
  28. */
  29. long rate, req_rate = mode->crtc_clock * 1000;
  30. if (req_rate) {
  31. rate = clk_round_rate(hwdev->pxlclk, req_rate);
  32. if (rate != req_rate) {
  33. DRM_DEBUG_DRIVER("pxlclk doesn't support %ld Hz\n",
  34. req_rate);
  35. return MODE_NOCLOCK;
  36. }
  37. }
  38. return MODE_OK;
  39. }
  40. static void malidp_crtc_atomic_enable(struct drm_crtc *crtc,
  41. struct drm_atomic_state *state)
  42. {
  43. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  44. struct malidp_hw_device *hwdev = malidp->dev;
  45. struct videomode vm;
  46. int err = pm_runtime_get_sync(crtc->dev->dev);
  47. if (err < 0) {
  48. DRM_DEBUG_DRIVER("Failed to enable runtime power management: %d\n", err);
  49. return;
  50. }
  51. drm_display_mode_to_videomode(&crtc->state->adjusted_mode, &vm);
  52. clk_prepare_enable(hwdev->pxlclk);
  53. /* We rely on firmware to set mclk to a sensible level. */
  54. clk_set_rate(hwdev->pxlclk, crtc->state->adjusted_mode.crtc_clock * 1000);
  55. hwdev->hw->modeset(hwdev, &vm);
  56. hwdev->hw->leave_config_mode(hwdev);
  57. drm_crtc_vblank_on(crtc);
  58. }
  59. static void malidp_crtc_atomic_disable(struct drm_crtc *crtc,
  60. struct drm_atomic_state *state)
  61. {
  62. struct drm_crtc_state *old_state = drm_atomic_get_old_crtc_state(state,
  63. crtc);
  64. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  65. struct malidp_hw_device *hwdev = malidp->dev;
  66. int err;
  67. /* always disable planes on the CRTC that is being turned off */
  68. drm_atomic_helper_disable_planes_on_crtc(old_state, false);
  69. drm_crtc_vblank_off(crtc);
  70. hwdev->hw->enter_config_mode(hwdev);
  71. clk_disable_unprepare(hwdev->pxlclk);
  72. err = pm_runtime_put(crtc->dev->dev);
  73. if (err < 0) {
  74. DRM_DEBUG_DRIVER("Failed to disable runtime power management: %d\n", err);
  75. }
  76. }
  77. static const struct gamma_curve_segment {
  78. u16 start;
  79. u16 end;
  80. } segments[MALIDP_COEFFTAB_NUM_COEFFS] = {
  81. /* sector 0 */
  82. { 0, 0 }, { 1, 1 }, { 2, 2 }, { 3, 3 },
  83. { 4, 4 }, { 5, 5 }, { 6, 6 }, { 7, 7 },
  84. { 8, 8 }, { 9, 9 }, { 10, 10 }, { 11, 11 },
  85. { 12, 12 }, { 13, 13 }, { 14, 14 }, { 15, 15 },
  86. /* sector 1 */
  87. { 16, 19 }, { 20, 23 }, { 24, 27 }, { 28, 31 },
  88. /* sector 2 */
  89. { 32, 39 }, { 40, 47 }, { 48, 55 }, { 56, 63 },
  90. /* sector 3 */
  91. { 64, 79 }, { 80, 95 }, { 96, 111 }, { 112, 127 },
  92. /* sector 4 */
  93. { 128, 159 }, { 160, 191 }, { 192, 223 }, { 224, 255 },
  94. /* sector 5 */
  95. { 256, 319 }, { 320, 383 }, { 384, 447 }, { 448, 511 },
  96. /* sector 6 */
  97. { 512, 639 }, { 640, 767 }, { 768, 895 }, { 896, 1023 },
  98. { 1024, 1151 }, { 1152, 1279 }, { 1280, 1407 }, { 1408, 1535 },
  99. { 1536, 1663 }, { 1664, 1791 }, { 1792, 1919 }, { 1920, 2047 },
  100. { 2048, 2175 }, { 2176, 2303 }, { 2304, 2431 }, { 2432, 2559 },
  101. { 2560, 2687 }, { 2688, 2815 }, { 2816, 2943 }, { 2944, 3071 },
  102. { 3072, 3199 }, { 3200, 3327 }, { 3328, 3455 }, { 3456, 3583 },
  103. { 3584, 3711 }, { 3712, 3839 }, { 3840, 3967 }, { 3968, 4095 },
  104. };
  105. #define DE_COEFTAB_DATA(a, b) ((((a) & 0xfff) << 16) | (((b) & 0xfff)))
  106. static void malidp_generate_gamma_table(struct drm_property_blob *lut_blob,
  107. u32 coeffs[MALIDP_COEFFTAB_NUM_COEFFS])
  108. {
  109. struct drm_color_lut *lut = (struct drm_color_lut *)lut_blob->data;
  110. int i;
  111. for (i = 0; i < MALIDP_COEFFTAB_NUM_COEFFS; ++i) {
  112. u32 a, b, delta_in, out_start, out_end;
  113. delta_in = segments[i].end - segments[i].start;
  114. /* DP has 12-bit internal precision for its LUTs. */
  115. out_start = drm_color_lut_extract(lut[segments[i].start].green,
  116. 12);
  117. out_end = drm_color_lut_extract(lut[segments[i].end].green, 12);
  118. a = (delta_in == 0) ? 0 : ((out_end - out_start) * 256) / delta_in;
  119. b = out_start;
  120. coeffs[i] = DE_COEFTAB_DATA(a, b);
  121. }
  122. }
  123. /*
  124. * Check if there is a new gamma LUT and if it is of an acceptable size. Also,
  125. * reject any LUTs that use distinct red, green, and blue curves.
  126. */
  127. static int malidp_crtc_atomic_check_gamma(struct drm_crtc *crtc,
  128. struct drm_crtc_state *state)
  129. {
  130. struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
  131. struct drm_color_lut *lut;
  132. size_t lut_size;
  133. int i;
  134. if (!state->color_mgmt_changed || !state->gamma_lut)
  135. return 0;
  136. if (crtc->state->gamma_lut &&
  137. (crtc->state->gamma_lut->base.id == state->gamma_lut->base.id))
  138. return 0;
  139. if (state->gamma_lut->length % sizeof(struct drm_color_lut))
  140. return -EINVAL;
  141. lut_size = state->gamma_lut->length / sizeof(struct drm_color_lut);
  142. if (lut_size != MALIDP_GAMMA_LUT_SIZE)
  143. return -EINVAL;
  144. lut = (struct drm_color_lut *)state->gamma_lut->data;
  145. for (i = 0; i < lut_size; ++i)
  146. if (!((lut[i].red == lut[i].green) &&
  147. (lut[i].red == lut[i].blue)))
  148. return -EINVAL;
  149. if (!state->mode_changed) {
  150. int ret;
  151. state->mode_changed = true;
  152. /*
  153. * Kerneldoc for drm_atomic_helper_check_modeset mandates that
  154. * it be invoked when the driver sets ->mode_changed. Since
  155. * changing the gamma LUT doesn't depend on any external
  156. * resources, it is safe to call it only once.
  157. */
  158. ret = drm_atomic_helper_check_modeset(crtc->dev, state->state);
  159. if (ret)
  160. return ret;
  161. }
  162. malidp_generate_gamma_table(state->gamma_lut, mc->gamma_coeffs);
  163. return 0;
  164. }
  165. /*
  166. * Check if there is a new CTM and if it contains valid input. Valid here means
  167. * that the number is inside the representable range for a Q3.12 number,
  168. * excluding truncating the fractional part of the input data.
  169. *
  170. * The COLORADJ registers can be changed atomically.
  171. */
  172. static int malidp_crtc_atomic_check_ctm(struct drm_crtc *crtc,
  173. struct drm_crtc_state *state)
  174. {
  175. struct malidp_crtc_state *mc = to_malidp_crtc_state(state);
  176. struct drm_color_ctm *ctm;
  177. int i;
  178. if (!state->color_mgmt_changed)
  179. return 0;
  180. if (!state->ctm)
  181. return 0;
  182. if (crtc->state->ctm && (crtc->state->ctm->base.id ==
  183. state->ctm->base.id))
  184. return 0;
  185. /*
  186. * The size of the ctm is checked in
  187. * drm_property_replace_blob_from_id.
  188. */
  189. ctm = (struct drm_color_ctm *)state->ctm->data;
  190. for (i = 0; i < ARRAY_SIZE(ctm->matrix); ++i) {
  191. /* Convert from S31.32 to Q3.12. */
  192. s64 val = ctm->matrix[i];
  193. u32 mag = ((((u64)val) & ~BIT_ULL(63)) >> 20) &
  194. GENMASK_ULL(14, 0);
  195. /*
  196. * Convert to 2s complement and check the destination's top bit
  197. * for overflow. NB: Can't check before converting or it'd
  198. * incorrectly reject the case:
  199. * sign == 1
  200. * mag == 0x2000
  201. */
  202. if (val & BIT_ULL(63))
  203. mag = ~mag + 1;
  204. if (!!(val & BIT_ULL(63)) != !!(mag & BIT(14)))
  205. return -EINVAL;
  206. mc->coloradj_coeffs[i] = mag;
  207. }
  208. return 0;
  209. }
  210. static int malidp_crtc_atomic_check_scaling(struct drm_crtc *crtc,
  211. struct drm_crtc_state *state)
  212. {
  213. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  214. struct malidp_hw_device *hwdev = malidp->dev;
  215. struct malidp_crtc_state *cs = to_malidp_crtc_state(state);
  216. struct malidp_se_config *s = &cs->scaler_config;
  217. struct drm_plane *plane;
  218. struct videomode vm;
  219. const struct drm_plane_state *pstate;
  220. u32 h_upscale_factor = 0; /* U16.16 */
  221. u32 v_upscale_factor = 0; /* U16.16 */
  222. u8 scaling = cs->scaled_planes_mask;
  223. int ret;
  224. if (!scaling) {
  225. s->scale_enable = false;
  226. goto mclk_calc;
  227. }
  228. /* The scaling engine can only handle one plane at a time. */
  229. if (scaling & (scaling - 1))
  230. return -EINVAL;
  231. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
  232. struct malidp_plane *mp = to_malidp_plane(plane);
  233. u32 phase;
  234. if (!(mp->layer->id & scaling))
  235. continue;
  236. /*
  237. * Convert crtc_[w|h] to U32.32, then divide by U16.16 src_[w|h]
  238. * to get the U16.16 result.
  239. */
  240. h_upscale_factor = div_u64((u64)pstate->crtc_w << 32,
  241. pstate->src_w);
  242. v_upscale_factor = div_u64((u64)pstate->crtc_h << 32,
  243. pstate->src_h);
  244. s->enhancer_enable = ((h_upscale_factor >> 16) >= 2 ||
  245. (v_upscale_factor >> 16) >= 2);
  246. if (pstate->rotation & MALIDP_ROTATED_MASK) {
  247. s->input_w = pstate->src_h >> 16;
  248. s->input_h = pstate->src_w >> 16;
  249. } else {
  250. s->input_w = pstate->src_w >> 16;
  251. s->input_h = pstate->src_h >> 16;
  252. }
  253. s->output_w = pstate->crtc_w;
  254. s->output_h = pstate->crtc_h;
  255. #define SE_N_PHASE 4
  256. #define SE_SHIFT_N_PHASE 12
  257. /* Calculate initial_phase and delta_phase for horizontal. */
  258. phase = s->input_w;
  259. s->h_init_phase =
  260. ((phase << SE_N_PHASE) / s->output_w + 1) / 2;
  261. phase = s->input_w;
  262. phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
  263. s->h_delta_phase = phase / s->output_w;
  264. /* Same for vertical. */
  265. phase = s->input_h;
  266. s->v_init_phase =
  267. ((phase << SE_N_PHASE) / s->output_h + 1) / 2;
  268. phase = s->input_h;
  269. phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
  270. s->v_delta_phase = phase / s->output_h;
  271. #undef SE_N_PHASE
  272. #undef SE_SHIFT_N_PHASE
  273. s->plane_src_id = mp->layer->id;
  274. }
  275. s->scale_enable = true;
  276. s->hcoeff = malidp_se_select_coeffs(h_upscale_factor);
  277. s->vcoeff = malidp_se_select_coeffs(v_upscale_factor);
  278. mclk_calc:
  279. drm_display_mode_to_videomode(&state->adjusted_mode, &vm);
  280. ret = hwdev->hw->se_calc_mclk(hwdev, s, &vm);
  281. if (ret < 0)
  282. return -EINVAL;
  283. return 0;
  284. }
  285. static int malidp_crtc_atomic_check(struct drm_crtc *crtc,
  286. struct drm_atomic_state *state)
  287. {
  288. struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state,
  289. crtc);
  290. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  291. struct malidp_hw_device *hwdev = malidp->dev;
  292. struct drm_plane *plane;
  293. const struct drm_plane_state *pstate;
  294. u32 rot_mem_free, rot_mem_usable;
  295. int rotated_planes = 0;
  296. int ret;
  297. /*
  298. * check if there is enough rotation memory available for planes
  299. * that need 90° and 270° rotion or planes that are compressed.
  300. * Each plane has set its required memory size in the ->plane_check()
  301. * callback, here we only make sure that the sums are less that the
  302. * total usable memory.
  303. *
  304. * The rotation memory allocation algorithm (for each plane):
  305. * a. If no more rotated or compressed planes exist, all remaining
  306. * rotate memory in the bank is available for use by the plane.
  307. * b. If other rotated or compressed planes exist, and plane's
  308. * layer ID is DE_VIDEO1, it can use all the memory from first bank
  309. * if secondary rotation memory bank is available, otherwise it can
  310. * use up to half the bank's memory.
  311. * c. If other rotated or compressed planes exist, and plane's layer ID
  312. * is not DE_VIDEO1, it can use half of the available memory.
  313. *
  314. * Note: this algorithm assumes that the order in which the planes are
  315. * checked always has DE_VIDEO1 plane first in the list if it is
  316. * rotated. Because that is how we create the planes in the first
  317. * place, under current DRM version things work, but if ever the order
  318. * in which drm_atomic_crtc_state_for_each_plane() iterates over planes
  319. * changes, we need to pre-sort the planes before validation.
  320. */
  321. /* first count the number of rotated planes */
  322. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
  323. struct drm_framebuffer *fb = pstate->fb;
  324. if ((pstate->rotation & MALIDP_ROTATED_MASK) || fb->modifier)
  325. rotated_planes++;
  326. }
  327. rot_mem_free = hwdev->rotation_memory[0];
  328. /*
  329. * if we have more than 1 plane using rotation memory, use the second
  330. * block of rotation memory as well
  331. */
  332. if (rotated_planes > 1)
  333. rot_mem_free += hwdev->rotation_memory[1];
  334. /* now validate the rotation memory requirements */
  335. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, crtc_state) {
  336. struct malidp_plane *mp = to_malidp_plane(plane);
  337. struct malidp_plane_state *ms = to_malidp_plane_state(pstate);
  338. struct drm_framebuffer *fb = pstate->fb;
  339. if ((pstate->rotation & MALIDP_ROTATED_MASK) || fb->modifier) {
  340. /* process current plane */
  341. rotated_planes--;
  342. if (!rotated_planes) {
  343. /* no more rotated planes, we can use what's left */
  344. rot_mem_usable = rot_mem_free;
  345. } else {
  346. if ((mp->layer->id != DE_VIDEO1) ||
  347. (hwdev->rotation_memory[1] == 0))
  348. rot_mem_usable = rot_mem_free / 2;
  349. else
  350. rot_mem_usable = hwdev->rotation_memory[0];
  351. }
  352. rot_mem_free -= rot_mem_usable;
  353. if (ms->rotmem_size > rot_mem_usable)
  354. return -EINVAL;
  355. }
  356. }
  357. /* If only the writeback routing has changed, we don't need a modeset */
  358. if (crtc_state->connectors_changed) {
  359. u32 old_mask = crtc->state->connector_mask;
  360. u32 new_mask = crtc_state->connector_mask;
  361. if ((old_mask ^ new_mask) ==
  362. (1 << drm_connector_index(&malidp->mw_connector.base)))
  363. crtc_state->connectors_changed = false;
  364. }
  365. ret = malidp_crtc_atomic_check_gamma(crtc, crtc_state);
  366. ret = ret ? ret : malidp_crtc_atomic_check_ctm(crtc, crtc_state);
  367. ret = ret ? ret : malidp_crtc_atomic_check_scaling(crtc, crtc_state);
  368. return ret;
  369. }
  370. static const struct drm_crtc_helper_funcs malidp_crtc_helper_funcs = {
  371. .mode_valid = malidp_crtc_mode_valid,
  372. .atomic_check = malidp_crtc_atomic_check,
  373. .atomic_enable = malidp_crtc_atomic_enable,
  374. .atomic_disable = malidp_crtc_atomic_disable,
  375. };
  376. static struct drm_crtc_state *malidp_crtc_duplicate_state(struct drm_crtc *crtc)
  377. {
  378. struct malidp_crtc_state *state, *old_state;
  379. if (WARN_ON(!crtc->state))
  380. return NULL;
  381. old_state = to_malidp_crtc_state(crtc->state);
  382. state = kmalloc(sizeof(*state), GFP_KERNEL);
  383. if (!state)
  384. return NULL;
  385. __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
  386. memcpy(state->gamma_coeffs, old_state->gamma_coeffs,
  387. sizeof(state->gamma_coeffs));
  388. memcpy(state->coloradj_coeffs, old_state->coloradj_coeffs,
  389. sizeof(state->coloradj_coeffs));
  390. memcpy(&state->scaler_config, &old_state->scaler_config,
  391. sizeof(state->scaler_config));
  392. state->scaled_planes_mask = 0;
  393. return &state->base;
  394. }
  395. static void malidp_crtc_destroy_state(struct drm_crtc *crtc,
  396. struct drm_crtc_state *state)
  397. {
  398. struct malidp_crtc_state *mali_state = NULL;
  399. if (state) {
  400. mali_state = to_malidp_crtc_state(state);
  401. __drm_atomic_helper_crtc_destroy_state(state);
  402. }
  403. kfree(mali_state);
  404. }
  405. static void malidp_crtc_reset(struct drm_crtc *crtc)
  406. {
  407. struct malidp_crtc_state *state =
  408. kzalloc(sizeof(*state), GFP_KERNEL);
  409. if (crtc->state)
  410. malidp_crtc_destroy_state(crtc, crtc->state);
  411. if (state)
  412. __drm_atomic_helper_crtc_reset(crtc, &state->base);
  413. else
  414. __drm_atomic_helper_crtc_reset(crtc, NULL);
  415. }
  416. static int malidp_crtc_enable_vblank(struct drm_crtc *crtc)
  417. {
  418. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  419. struct malidp_hw_device *hwdev = malidp->dev;
  420. malidp_hw_enable_irq(hwdev, MALIDP_DE_BLOCK,
  421. hwdev->hw->map.de_irq_map.vsync_irq);
  422. return 0;
  423. }
  424. static void malidp_crtc_disable_vblank(struct drm_crtc *crtc)
  425. {
  426. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  427. struct malidp_hw_device *hwdev = malidp->dev;
  428. malidp_hw_disable_irq(hwdev, MALIDP_DE_BLOCK,
  429. hwdev->hw->map.de_irq_map.vsync_irq);
  430. }
  431. static const struct drm_crtc_funcs malidp_crtc_funcs = {
  432. .set_config = drm_atomic_helper_set_config,
  433. .page_flip = drm_atomic_helper_page_flip,
  434. .reset = malidp_crtc_reset,
  435. .atomic_duplicate_state = malidp_crtc_duplicate_state,
  436. .atomic_destroy_state = malidp_crtc_destroy_state,
  437. .enable_vblank = malidp_crtc_enable_vblank,
  438. .disable_vblank = malidp_crtc_disable_vblank,
  439. };
  440. int malidp_crtc_init(struct drm_device *drm)
  441. {
  442. struct malidp_drm *malidp = drm_to_malidp(drm);
  443. struct drm_plane *primary = NULL, *plane;
  444. int ret;
  445. ret = malidp_de_planes_init(drm);
  446. if (ret < 0) {
  447. DRM_ERROR("Failed to initialise planes\n");
  448. return ret;
  449. }
  450. drm_for_each_plane(plane, drm) {
  451. if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
  452. primary = plane;
  453. break;
  454. }
  455. }
  456. if (!primary) {
  457. DRM_ERROR("no primary plane found\n");
  458. return -EINVAL;
  459. }
  460. ret = drmm_crtc_init_with_planes(drm, &malidp->crtc, primary, NULL,
  461. &malidp_crtc_funcs, NULL);
  462. if (ret)
  463. return ret;
  464. drm_crtc_helper_add(&malidp->crtc, &malidp_crtc_helper_funcs);
  465. drm_mode_crtc_set_gamma_size(&malidp->crtc, MALIDP_GAMMA_LUT_SIZE);
  466. /* No inverse-gamma: it is per-plane. */
  467. drm_crtc_enable_color_mgmt(&malidp->crtc, 0, true, MALIDP_GAMMA_LUT_SIZE);
  468. malidp_se_set_enh_coeffs(malidp->dev);
  469. return 0;
  470. }