malidp_crtc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * (C) COPYRIGHT 2016 ARM Limited. All rights reserved.
  3. * Author: Liviu Dudau <Liviu.Dudau@arm.com>
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * ARM Mali DP500/DP550/DP650 driver (crtc operations)
  11. */
  12. #include <drm/drmP.h>
  13. #include <drm/drm_atomic.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <linux/clk.h>
  18. #include <linux/pm_runtime.h>
  19. #include <video/videomode.h>
  20. #include "malidp_drv.h"
  21. #include "malidp_hw.h"
  22. static enum drm_mode_status malidp_crtc_mode_valid(struct drm_crtc *crtc,
  23. const struct drm_display_mode *mode)
  24. {
  25. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  26. struct malidp_hw_device *hwdev = malidp->dev;
  27. /*
  28. * check that the hardware can drive the required clock rate,
  29. * but skip the check if the clock is meant to be disabled (req_rate = 0)
  30. */
  31. long rate, req_rate = mode->crtc_clock * 1000;
  32. if (req_rate) {
  33. rate = clk_round_rate(hwdev->pxlclk, req_rate);
  34. if (rate != req_rate) {
  35. DRM_DEBUG_DRIVER("pxlclk doesn't support %ld Hz\n",
  36. req_rate);
  37. return MODE_NOCLOCK;
  38. }
  39. }
  40. return MODE_OK;
  41. }
  42. static void malidp_crtc_atomic_enable(struct drm_crtc *crtc,
  43. struct drm_crtc_state *old_state)
  44. {
  45. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  46. struct malidp_hw_device *hwdev = malidp->dev;
  47. struct videomode vm;
  48. int err = pm_runtime_get_sync(crtc->dev->dev);
  49. if (err < 0) {
  50. DRM_DEBUG_DRIVER("Failed to enable runtime power management: %d\n", err);
  51. return;
  52. }
  53. drm_display_mode_to_videomode(&crtc->state->adjusted_mode, &vm);
  54. clk_prepare_enable(hwdev->pxlclk);
  55. /* We rely on firmware to set mclk to a sensible level. */
  56. clk_set_rate(hwdev->pxlclk, crtc->state->adjusted_mode.crtc_clock * 1000);
  57. hwdev->hw->modeset(hwdev, &vm);
  58. hwdev->hw->leave_config_mode(hwdev);
  59. drm_crtc_vblank_on(crtc);
  60. }
  61. static void malidp_crtc_atomic_disable(struct drm_crtc *crtc,
  62. struct drm_crtc_state *old_state)
  63. {
  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_atomic_replace_property_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. s->input_w = pstate->src_w >> 16;
  247. s->input_h = pstate->src_h >> 16;
  248. s->output_w = pstate->crtc_w;
  249. s->output_h = pstate->crtc_h;
  250. #define SE_N_PHASE 4
  251. #define SE_SHIFT_N_PHASE 12
  252. /* Calculate initial_phase and delta_phase for horizontal. */
  253. phase = s->input_w;
  254. s->h_init_phase =
  255. ((phase << SE_N_PHASE) / s->output_w + 1) / 2;
  256. phase = s->input_w;
  257. phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
  258. s->h_delta_phase = phase / s->output_w;
  259. /* Same for vertical. */
  260. phase = s->input_h;
  261. s->v_init_phase =
  262. ((phase << SE_N_PHASE) / s->output_h + 1) / 2;
  263. phase = s->input_h;
  264. phase <<= (SE_SHIFT_N_PHASE + SE_N_PHASE);
  265. s->v_delta_phase = phase / s->output_h;
  266. #undef SE_N_PHASE
  267. #undef SE_SHIFT_N_PHASE
  268. s->plane_src_id = mp->layer->id;
  269. }
  270. s->scale_enable = true;
  271. s->hcoeff = malidp_se_select_coeffs(h_upscale_factor);
  272. s->vcoeff = malidp_se_select_coeffs(v_upscale_factor);
  273. mclk_calc:
  274. drm_display_mode_to_videomode(&state->adjusted_mode, &vm);
  275. ret = hwdev->hw->se_calc_mclk(hwdev, s, &vm);
  276. if (ret < 0)
  277. return -EINVAL;
  278. return 0;
  279. }
  280. static int malidp_crtc_atomic_check(struct drm_crtc *crtc,
  281. struct drm_crtc_state *state)
  282. {
  283. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  284. struct malidp_hw_device *hwdev = malidp->dev;
  285. struct drm_plane *plane;
  286. const struct drm_plane_state *pstate;
  287. u32 rot_mem_free, rot_mem_usable;
  288. int rotated_planes = 0;
  289. int ret;
  290. /*
  291. * check if there is enough rotation memory available for planes
  292. * that need 90° and 270° rotation. Each plane has set its required
  293. * memory size in the ->plane_check() callback, here we only make
  294. * sure that the sums are less that the total usable memory.
  295. *
  296. * The rotation memory allocation algorithm (for each plane):
  297. * a. If no more rotated planes exist, all remaining rotate
  298. * memory in the bank is available for use by the plane.
  299. * b. If other rotated planes exist, and plane's layer ID is
  300. * DE_VIDEO1, it can use all the memory from first bank if
  301. * secondary rotation memory bank is available, otherwise it can
  302. * use up to half the bank's memory.
  303. * c. If other rotated planes exist, and plane's layer ID is not
  304. * DE_VIDEO1, it can use half of the available memory
  305. *
  306. * Note: this algorithm assumes that the order in which the planes are
  307. * checked always has DE_VIDEO1 plane first in the list if it is
  308. * rotated. Because that is how we create the planes in the first
  309. * place, under current DRM version things work, but if ever the order
  310. * in which drm_atomic_crtc_state_for_each_plane() iterates over planes
  311. * changes, we need to pre-sort the planes before validation.
  312. */
  313. /* first count the number of rotated planes */
  314. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
  315. if (pstate->rotation & MALIDP_ROTATED_MASK)
  316. rotated_planes++;
  317. }
  318. rot_mem_free = hwdev->rotation_memory[0];
  319. /*
  320. * if we have more than 1 plane using rotation memory, use the second
  321. * block of rotation memory as well
  322. */
  323. if (rotated_planes > 1)
  324. rot_mem_free += hwdev->rotation_memory[1];
  325. /* now validate the rotation memory requirements */
  326. drm_atomic_crtc_state_for_each_plane_state(plane, pstate, state) {
  327. struct malidp_plane *mp = to_malidp_plane(plane);
  328. struct malidp_plane_state *ms = to_malidp_plane_state(pstate);
  329. if (pstate->rotation & MALIDP_ROTATED_MASK) {
  330. /* process current plane */
  331. rotated_planes--;
  332. if (!rotated_planes) {
  333. /* no more rotated planes, we can use what's left */
  334. rot_mem_usable = rot_mem_free;
  335. } else {
  336. if ((mp->layer->id != DE_VIDEO1) ||
  337. (hwdev->rotation_memory[1] == 0))
  338. rot_mem_usable = rot_mem_free / 2;
  339. else
  340. rot_mem_usable = hwdev->rotation_memory[0];
  341. }
  342. rot_mem_free -= rot_mem_usable;
  343. if (ms->rotmem_size > rot_mem_usable)
  344. return -EINVAL;
  345. }
  346. }
  347. ret = malidp_crtc_atomic_check_gamma(crtc, state);
  348. ret = ret ? ret : malidp_crtc_atomic_check_ctm(crtc, state);
  349. ret = ret ? ret : malidp_crtc_atomic_check_scaling(crtc, state);
  350. return ret;
  351. }
  352. static const struct drm_crtc_helper_funcs malidp_crtc_helper_funcs = {
  353. .mode_valid = malidp_crtc_mode_valid,
  354. .atomic_check = malidp_crtc_atomic_check,
  355. .atomic_enable = malidp_crtc_atomic_enable,
  356. .atomic_disable = malidp_crtc_atomic_disable,
  357. };
  358. static struct drm_crtc_state *malidp_crtc_duplicate_state(struct drm_crtc *crtc)
  359. {
  360. struct malidp_crtc_state *state, *old_state;
  361. if (WARN_ON(!crtc->state))
  362. return NULL;
  363. old_state = to_malidp_crtc_state(crtc->state);
  364. state = kmalloc(sizeof(*state), GFP_KERNEL);
  365. if (!state)
  366. return NULL;
  367. __drm_atomic_helper_crtc_duplicate_state(crtc, &state->base);
  368. memcpy(state->gamma_coeffs, old_state->gamma_coeffs,
  369. sizeof(state->gamma_coeffs));
  370. memcpy(state->coloradj_coeffs, old_state->coloradj_coeffs,
  371. sizeof(state->coloradj_coeffs));
  372. memcpy(&state->scaler_config, &old_state->scaler_config,
  373. sizeof(state->scaler_config));
  374. state->scaled_planes_mask = 0;
  375. return &state->base;
  376. }
  377. static void malidp_crtc_reset(struct drm_crtc *crtc)
  378. {
  379. struct malidp_crtc_state *state = NULL;
  380. if (crtc->state) {
  381. state = to_malidp_crtc_state(crtc->state);
  382. __drm_atomic_helper_crtc_destroy_state(crtc->state);
  383. }
  384. kfree(state);
  385. state = kzalloc(sizeof(*state), GFP_KERNEL);
  386. if (state) {
  387. crtc->state = &state->base;
  388. crtc->state->crtc = crtc;
  389. }
  390. }
  391. static void malidp_crtc_destroy_state(struct drm_crtc *crtc,
  392. struct drm_crtc_state *state)
  393. {
  394. struct malidp_crtc_state *mali_state = NULL;
  395. if (state) {
  396. mali_state = to_malidp_crtc_state(state);
  397. __drm_atomic_helper_crtc_destroy_state(state);
  398. }
  399. kfree(mali_state);
  400. }
  401. static int malidp_crtc_enable_vblank(struct drm_crtc *crtc)
  402. {
  403. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  404. struct malidp_hw_device *hwdev = malidp->dev;
  405. malidp_hw_enable_irq(hwdev, MALIDP_DE_BLOCK,
  406. hwdev->hw->map.de_irq_map.vsync_irq);
  407. return 0;
  408. }
  409. static void malidp_crtc_disable_vblank(struct drm_crtc *crtc)
  410. {
  411. struct malidp_drm *malidp = crtc_to_malidp_device(crtc);
  412. struct malidp_hw_device *hwdev = malidp->dev;
  413. malidp_hw_disable_irq(hwdev, MALIDP_DE_BLOCK,
  414. hwdev->hw->map.de_irq_map.vsync_irq);
  415. }
  416. static const struct drm_crtc_funcs malidp_crtc_funcs = {
  417. .gamma_set = drm_atomic_helper_legacy_gamma_set,
  418. .destroy = drm_crtc_cleanup,
  419. .set_config = drm_atomic_helper_set_config,
  420. .page_flip = drm_atomic_helper_page_flip,
  421. .reset = malidp_crtc_reset,
  422. .atomic_duplicate_state = malidp_crtc_duplicate_state,
  423. .atomic_destroy_state = malidp_crtc_destroy_state,
  424. .enable_vblank = malidp_crtc_enable_vblank,
  425. .disable_vblank = malidp_crtc_disable_vblank,
  426. };
  427. int malidp_crtc_init(struct drm_device *drm)
  428. {
  429. struct malidp_drm *malidp = drm->dev_private;
  430. struct drm_plane *primary = NULL, *plane;
  431. int ret;
  432. ret = malidp_de_planes_init(drm);
  433. if (ret < 0) {
  434. DRM_ERROR("Failed to initialise planes\n");
  435. return ret;
  436. }
  437. drm_for_each_plane(plane, drm) {
  438. if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
  439. primary = plane;
  440. break;
  441. }
  442. }
  443. if (!primary) {
  444. DRM_ERROR("no primary plane found\n");
  445. ret = -EINVAL;
  446. goto crtc_cleanup_planes;
  447. }
  448. ret = drm_crtc_init_with_planes(drm, &malidp->crtc, primary, NULL,
  449. &malidp_crtc_funcs, NULL);
  450. if (ret)
  451. goto crtc_cleanup_planes;
  452. drm_crtc_helper_add(&malidp->crtc, &malidp_crtc_helper_funcs);
  453. drm_mode_crtc_set_gamma_size(&malidp->crtc, MALIDP_GAMMA_LUT_SIZE);
  454. /* No inverse-gamma: it is per-plane. */
  455. drm_crtc_enable_color_mgmt(&malidp->crtc, 0, true, MALIDP_GAMMA_LUT_SIZE);
  456. malidp_se_set_enh_coeffs(malidp->dev);
  457. return 0;
  458. crtc_cleanup_planes:
  459. malidp_de_planes_destroy(drm);
  460. return ret;
  461. }