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