malidp_crtc.c 16 KB

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