2 * Copyright 2018 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "amdgpu_mode.h"
27 #include "amdgpu_dm.h"
29 #include "modules/color/color_gamma.h"
30 #include "basics/conversion.h"
35 * The DC interface to HW gives us the following color management blocks
38 * - Input gamma LUT (de-normalized)
39 * - Input CSC (normalized)
40 * - Surface degamma LUT (normalized)
41 * - Surface CSC (normalized)
42 * - Surface regamma LUT (normalized)
43 * - Output CSC (normalized)
45 * But these aren't a direct mapping to DRM color properties. The current DRM
46 * interface exposes CRTC degamma, CRTC CTM and CRTC regamma while our hardware
47 * is essentially giving:
49 * Plane CTM -> Plane degamma -> Plane CTM -> Plane regamma -> Plane CTM
51 * The input gamma LUT block isn't really applicable here since it operates
52 * on the actual input data itself rather than the HW fp representation. The
53 * input and output CSC blocks are technically available to use as part of
54 * the DC interface but are typically used internally by DC for conversions
55 * between color spaces. These could be blended together with user
56 * adjustments in the future but for now these should remain untouched.
58 * The pipe blending also happens after these blocks so we don't actually
59 * support any CRTC props with correct blending with multiple planes - but we
60 * can still support CRTC color management properties in DM in most single
61 * plane cases correctly with clever management of the DC interface in DM.
63 * As per DRM documentation, blocks should be in hardware bypass when their
64 * respective property is set to NULL. A linear DGM/RGM LUT should also
65 * considered as putting the respective block into bypass mode.
67 * This means that the following
68 * configuration is assumed to be the default:
70 * Plane DGM Bypass -> Plane CTM Bypass -> Plane RGM Bypass -> ...
71 * CRTC DGM Bypass -> CRTC CTM Bypass -> CRTC RGM Bypass
74 #define MAX_DRM_LUT_VALUE 0xFFFF
77 * amdgpu_dm_init_color_mod - Initialize the color module.
79 * We're not using the full color module, only certain components.
80 * Only call setup functions for components that we need.
82 void amdgpu_dm_init_color_mod(void)
84 setup_x_points_distribution();
88 * __extract_blob_lut - Extracts the DRM lut and lut size from a blob.
89 * @blob: DRM color mgmt property blob
95 static const struct drm_color_lut *
96 __extract_blob_lut(const struct drm_property_blob *blob, uint32_t *size)
98 *size = blob ? drm_color_lut_size(blob) : 0;
99 return blob ? (struct drm_color_lut *)blob->data : NULL;
103 * __is_lut_linear - check if the given lut is a linear mapping of values
104 * @lut: given lut to check values
107 * It is considered linear if the lut represents:
108 * f(a) = (0xFF00/MAX_COLOR_LUT_ENTRIES-1)a; for integer a in [0,
109 * MAX_COLOR_LUT_ENTRIES)
112 * True if the given lut is a linear mapping of values, i.e. it acts like a
113 * bypass LUT. Otherwise, false.
115 static bool __is_lut_linear(const struct drm_color_lut *lut, uint32_t size)
121 for (i = 0; i < size; i++) {
122 /* All color values should equal */
123 if ((lut[i].red != lut[i].green) || (lut[i].green != lut[i].blue))
126 expected = i * MAX_DRM_LUT_VALUE / (size-1);
128 /* Allow a +/-1 error. */
129 delta = lut[i].red - expected;
130 if (delta < -1 || 1 < delta)
137 * __drm_lut_to_dc_gamma - convert the drm_color_lut to dc_gamma.
138 * @lut: DRM lookup table for color conversion
139 * @gamma: DC gamma to set entries
140 * @is_legacy: legacy or atomic gamma
142 * The conversion depends on the size of the lut - whether or not it's legacy.
144 static void __drm_lut_to_dc_gamma(const struct drm_color_lut *lut,
145 struct dc_gamma *gamma, bool is_legacy)
151 for (i = 0; i < MAX_COLOR_LEGACY_LUT_ENTRIES; i++) {
152 r = drm_color_lut_extract(lut[i].red, 16);
153 g = drm_color_lut_extract(lut[i].green, 16);
154 b = drm_color_lut_extract(lut[i].blue, 16);
156 gamma->entries.red[i] = dc_fixpt_from_int(r);
157 gamma->entries.green[i] = dc_fixpt_from_int(g);
158 gamma->entries.blue[i] = dc_fixpt_from_int(b);
164 for (i = 0; i < MAX_COLOR_LUT_ENTRIES; i++) {
165 r = drm_color_lut_extract(lut[i].red, 16);
166 g = drm_color_lut_extract(lut[i].green, 16);
167 b = drm_color_lut_extract(lut[i].blue, 16);
169 gamma->entries.red[i] = dc_fixpt_from_fraction(r, MAX_DRM_LUT_VALUE);
170 gamma->entries.green[i] = dc_fixpt_from_fraction(g, MAX_DRM_LUT_VALUE);
171 gamma->entries.blue[i] = dc_fixpt_from_fraction(b, MAX_DRM_LUT_VALUE);
176 * __drm_ctm_to_dc_matrix - converts a DRM CTM to a DC CSC float matrix
177 * @ctm: DRM color transformation matrix
178 * @matrix: DC CSC float matrix
180 * The matrix needs to be a 3x4 (12 entry) matrix.
182 static void __drm_ctm_to_dc_matrix(const struct drm_color_ctm *ctm,
183 struct fixed31_32 *matrix)
189 * DRM gives a 3x3 matrix, but DC wants 3x4. Assuming we're operating
190 * with homogeneous coordinates, augment the matrix with 0's.
192 * The format provided is S31.32, using signed-magnitude representation.
193 * Our fixed31_32 is also S31.32, but is using 2's complement. We have
194 * to convert from signed-magnitude to 2's complement.
196 for (i = 0; i < 12; i++) {
197 /* Skip 4th element */
199 matrix[i] = dc_fixpt_zero;
203 /* gamut_remap_matrix[i] = ctm[i - floor(i/4)] */
204 val = ctm->matrix[i - (i / 4)];
205 /* If negative, convert to 2's complement. */
206 if (val & (1ULL << 63))
207 val = -(val & ~(1ULL << 63));
209 matrix[i].value = val;
214 * __set_legacy_tf - Calculates the legacy transfer function
215 * @func: transfer function
216 * @lut: lookup table that defines the color space
217 * @lut_size: size of respective lut
218 * @has_rom: if ROM can be used for hardcoded curve
220 * Only for sRGB input space
223 * 0 in case of success, -ENOMEM if fails
225 static int __set_legacy_tf(struct dc_transfer_func *func,
226 const struct drm_color_lut *lut, uint32_t lut_size,
229 struct dc_gamma *gamma = NULL;
230 struct calculate_buffer cal_buffer = {0};
233 ASSERT(lut && lut_size == MAX_COLOR_LEGACY_LUT_ENTRIES);
235 cal_buffer.buffer_index = -1;
237 gamma = dc_create_gamma();
241 gamma->type = GAMMA_RGB_256;
242 gamma->num_entries = lut_size;
243 __drm_lut_to_dc_gamma(lut, gamma, true);
245 res = mod_color_calculate_regamma_params(func, gamma, true, has_rom,
248 dc_gamma_release(&gamma);
250 return res ? 0 : -ENOMEM;
254 * __set_output_tf - calculates the output transfer function based on expected input space.
255 * @func: transfer function
256 * @lut: lookup table that defines the color space
257 * @lut_size: size of respective lut
258 * @has_rom: if ROM can be used for hardcoded curve
261 * 0 in case of success. -ENOMEM if fails.
263 static int __set_output_tf(struct dc_transfer_func *func,
264 const struct drm_color_lut *lut, uint32_t lut_size,
267 struct dc_gamma *gamma = NULL;
268 struct calculate_buffer cal_buffer = {0};
271 ASSERT(lut && lut_size == MAX_COLOR_LUT_ENTRIES);
273 cal_buffer.buffer_index = -1;
275 gamma = dc_create_gamma();
279 gamma->num_entries = lut_size;
280 __drm_lut_to_dc_gamma(lut, gamma, false);
282 if (func->tf == TRANSFER_FUNCTION_LINEAR) {
284 * Color module doesn't like calculating regamma params
285 * on top of a linear input. But degamma params can be used
286 * instead to simulate this.
288 gamma->type = GAMMA_CUSTOM;
289 res = mod_color_calculate_degamma_params(NULL, func,
293 * Assume sRGB. The actual mapping will depend on whether the
294 * input was legacy or not.
296 gamma->type = GAMMA_CS_TFM_1D;
297 res = mod_color_calculate_regamma_params(func, gamma, false,
298 has_rom, NULL, &cal_buffer);
301 dc_gamma_release(&gamma);
303 return res ? 0 : -ENOMEM;
307 * __set_input_tf - calculates the input transfer function based on expected
309 * @func: transfer function
310 * @lut: lookup table that defines the color space
311 * @lut_size: size of respective lut.
314 * 0 in case of success. -ENOMEM if fails.
316 static int __set_input_tf(struct dc_transfer_func *func,
317 const struct drm_color_lut *lut, uint32_t lut_size)
319 struct dc_gamma *gamma = NULL;
322 gamma = dc_create_gamma();
326 gamma->type = GAMMA_CUSTOM;
327 gamma->num_entries = lut_size;
329 __drm_lut_to_dc_gamma(lut, gamma, false);
331 res = mod_color_calculate_degamma_params(NULL, func, gamma, true);
332 dc_gamma_release(&gamma);
334 return res ? 0 : -ENOMEM;
338 * amdgpu_dm_verify_lut_sizes - verifies if DRM luts match the hw supported sizes
339 * @crtc_state: the DRM CRTC state
341 * Verifies that the Degamma and Gamma LUTs attached to the &crtc_state
342 * are of the expected size.
345 * 0 on success. -EINVAL if any lut sizes are invalid.
347 int amdgpu_dm_verify_lut_sizes(const struct drm_crtc_state *crtc_state)
349 const struct drm_color_lut *lut = NULL;
352 lut = __extract_blob_lut(crtc_state->degamma_lut, &size);
353 if (lut && size != MAX_COLOR_LUT_ENTRIES) {
355 "Invalid Degamma LUT size. Should be %u but got %u.\n",
356 MAX_COLOR_LUT_ENTRIES, size);
360 lut = __extract_blob_lut(crtc_state->gamma_lut, &size);
361 if (lut && size != MAX_COLOR_LUT_ENTRIES &&
362 size != MAX_COLOR_LEGACY_LUT_ENTRIES) {
364 "Invalid Gamma LUT size. Should be %u (or %u for legacy) but got %u.\n",
365 MAX_COLOR_LUT_ENTRIES, MAX_COLOR_LEGACY_LUT_ENTRIES,
374 * amdgpu_dm_update_crtc_color_mgmt: Maps DRM color management to DC stream.
375 * @crtc: amdgpu_dm crtc state
377 * With no plane level color management properties we're free to use any
378 * of the HW blocks as long as the CRTC CTM always comes before the
379 * CRTC RGM and after the CRTC DGM.
381 * - The CRTC RGM block will be placed in the RGM LUT block if it is non-linear.
382 * - The CRTC DGM block will be placed in the DGM LUT block if it is non-linear.
383 * - The CRTC CTM will be placed in the gamut remap block if it is non-linear.
385 * The RGM block is typically more fully featured and accurate across
386 * all ASICs - DCE can't support a custom non-linear CRTC DGM.
388 * For supporting both plane level color management and CRTC level color
389 * management at once we have to either restrict the usage of CRTC properties
390 * or blend adjustments together.
393 * 0 on success. Error code if setup fails.
395 int amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state *crtc)
397 struct dc_stream_state *stream = crtc->stream;
398 struct amdgpu_device *adev = drm_to_adev(crtc->base.state->dev);
399 bool has_rom = adev->asic_type <= CHIP_RAVEN;
400 struct drm_color_ctm *ctm = NULL;
401 const struct drm_color_lut *degamma_lut, *regamma_lut;
402 uint32_t degamma_size, regamma_size;
403 bool has_regamma, has_degamma;
407 r = amdgpu_dm_verify_lut_sizes(&crtc->base);
411 degamma_lut = __extract_blob_lut(crtc->base.degamma_lut, °amma_size);
412 regamma_lut = __extract_blob_lut(crtc->base.gamma_lut, ®amma_size);
415 degamma_lut && !__is_lut_linear(degamma_lut, degamma_size);
418 regamma_lut && !__is_lut_linear(regamma_lut, regamma_size);
420 is_legacy = regamma_size == MAX_COLOR_LEGACY_LUT_ENTRIES;
422 /* Reset all adjustments. */
423 crtc->cm_has_degamma = false;
424 crtc->cm_is_degamma_srgb = false;
426 /* Setup regamma and degamma. */
429 * Legacy regamma forces us to use the sRGB RGM as a base.
430 * This also means we can't use linear DGM since DGM needs
431 * to use sRGB as a base as well, resulting in incorrect CRTC
434 * TODO: Just map this to the standard regamma interface
435 * instead since this isn't really right. One of the cases
436 * where this setup currently fails is trying to do an
437 * inverse color ramp in legacy userspace.
439 crtc->cm_is_degamma_srgb = true;
440 stream->out_transfer_func->type = TF_TYPE_DISTRIBUTED_POINTS;
441 stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
443 r = __set_legacy_tf(stream->out_transfer_func, regamma_lut,
444 regamma_size, has_rom);
447 } else if (has_regamma) {
448 /* If atomic regamma, CRTC RGM goes into RGM LUT. */
449 stream->out_transfer_func->type = TF_TYPE_DISTRIBUTED_POINTS;
450 stream->out_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
452 r = __set_output_tf(stream->out_transfer_func, regamma_lut,
453 regamma_size, has_rom);
458 * No CRTC RGM means we can just put the block into bypass
459 * since we don't have any plane level adjustments using it.
461 stream->out_transfer_func->type = TF_TYPE_BYPASS;
462 stream->out_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
466 * CRTC DGM goes into DGM LUT. It would be nice to place it
467 * into the RGM since it's a more featured block but we'd
468 * have to place the CTM in the OCSC in that case.
470 crtc->cm_has_degamma = has_degamma;
472 /* Setup CRTC CTM. */
473 if (crtc->base.ctm) {
474 ctm = (struct drm_color_ctm *)crtc->base.ctm->data;
477 * Gamut remapping must be used for gamma correction
478 * since it comes before the regamma correction.
480 * OCSC could be used for gamma correction, but we'd need to
481 * blend the adjustments together with the required output
482 * conversion matrix - so just use the gamut remap block
485 __drm_ctm_to_dc_matrix(ctm, stream->gamut_remap_matrix.matrix);
487 stream->gamut_remap_matrix.enable_remap = true;
488 stream->csc_color_matrix.enable_adjustment = false;
491 stream->gamut_remap_matrix.enable_remap = false;
492 stream->csc_color_matrix.enable_adjustment = false;
499 * amdgpu_dm_update_plane_color_mgmt: Maps DRM color management to DC plane.
500 * @crtc: amdgpu_dm crtc state
501 * @dc_plane_state: target DC surface
503 * Update the underlying dc_stream_state's input transfer function (ITF) in
504 * preparation for hardware commit. The transfer function used depends on
505 * the preparation done on the stream for color management.
508 * 0 on success. -ENOMEM if mem allocation fails.
510 int amdgpu_dm_update_plane_color_mgmt(struct dm_crtc_state *crtc,
511 struct dc_plane_state *dc_plane_state)
513 const struct drm_color_lut *degamma_lut;
514 enum dc_transfer_func_predefined tf = TRANSFER_FUNCTION_SRGB;
515 uint32_t degamma_size;
518 /* Get the correct base transfer function for implicit degamma. */
519 switch (dc_plane_state->format) {
520 case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
521 case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
522 /* DC doesn't have a transfer function for BT601 specifically. */
523 tf = TRANSFER_FUNCTION_BT709;
529 if (crtc->cm_has_degamma) {
530 degamma_lut = __extract_blob_lut(crtc->base.degamma_lut,
532 ASSERT(degamma_size == MAX_COLOR_LUT_ENTRIES);
534 dc_plane_state->in_transfer_func->type =
535 TF_TYPE_DISTRIBUTED_POINTS;
538 * This case isn't fully correct, but also fairly
539 * uncommon. This is userspace trying to use a
540 * legacy gamma LUT + atomic degamma LUT
543 * Legacy gamma requires the input to be in linear
544 * space, so that means we need to apply an sRGB
545 * degamma. But color module also doesn't support
546 * a user ramp in this case so the degamma will
549 * Even if we did support it, it's still not right:
551 * Input -> CRTC DGM -> sRGB DGM -> CRTC CTM ->
552 * sRGB RGM -> CRTC RGM -> Output
554 * The CSC will be done in the wrong space since
555 * we're applying an sRGB DGM on top of the CRTC
558 * TODO: Don't use the legacy gamma interface and just
559 * map these to the atomic one instead.
561 if (crtc->cm_is_degamma_srgb)
562 dc_plane_state->in_transfer_func->tf = tf;
564 dc_plane_state->in_transfer_func->tf =
565 TRANSFER_FUNCTION_LINEAR;
567 r = __set_input_tf(dc_plane_state->in_transfer_func,
568 degamma_lut, degamma_size);
571 } else if (crtc->cm_is_degamma_srgb) {
573 * For legacy gamma support we need the regamma input
574 * in linear space. Assume that the input is sRGB.
576 dc_plane_state->in_transfer_func->type = TF_TYPE_PREDEFINED;
577 dc_plane_state->in_transfer_func->tf = tf;
579 if (tf != TRANSFER_FUNCTION_SRGB &&
580 !mod_color_calculate_degamma_params(NULL,
581 dc_plane_state->in_transfer_func, NULL, false))
584 /* ...Otherwise we can just bypass the DGM block. */
585 dc_plane_state->in_transfer_func->type = TF_TYPE_BYPASS;
586 dc_plane_state->in_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;