1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright 2018 Advanced Micro Devices, Inc.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: AMD
24 *
25 */
26
27 #include <drm/drm_colorop.h>
28
29 #include "amdgpu.h"
30 #include "amdgpu_mode.h"
31 #include "amdgpu_dm.h"
32 #include "amdgpu_dm_colorop.h"
33 #include "dc.h"
34 #include "modules/color/color_gamma.h"
35
36 /**
37 * DOC: overview
38 *
39 * We have three types of color management in the AMD display driver.
40 * 1. the legacy &drm_crtc DEGAMMA, CTM, and GAMMA properties
41 * 2. AMD driver private color management on &drm_plane and &drm_crtc
42 * 3. AMD plane color pipeline
43 *
44 * The CRTC properties are the original color management. When they were
45 * implemented per-plane color management was not a thing yet. Because
46 * of that we could get away with plumbing the DEGAMMA and CTM
47 * properties to pre-blending HW functions. This is incompatible with
48 * per-plane color management, such as via the AMD private properties or
49 * the new drm_plane color pipeline. The only compatible CRTC property
50 * with per-plane color management is the GAMMA property as it is
51 * applied post-blending.
52 *
53 * The AMD driver private color management properties are only exposed
54 * when the kernel is built explicitly with -DAMD_PRIVATE_COLOR. They
55 * are temporary building blocks on the path to full-fledged &drm_plane
56 * and &drm_crtc color pipelines and lay the driver's groundwork for the
57 * color pipelines.
58 *
59 * The AMD plane color pipeline describes AMD's &drm_colorops via the
60 * &drm_plane's COLOR_PIPELINE property.
61 *
62 * drm_crtc Properties
63 * -------------------
64 *
65 * The DC interface to HW gives us the following color management blocks
66 * per pipe (surface):
67 *
68 * - Input gamma LUT (de-normalized)
69 * - Input CSC (normalized)
70 * - Surface degamma LUT (normalized)
71 * - Surface CSC (normalized)
72 * - Surface regamma LUT (normalized)
73 * - Output CSC (normalized)
74 *
75 * But these aren't a direct mapping to DRM color properties. The
76 * current DRM interface exposes CRTC degamma, CRTC CTM and CRTC regamma
77 * while our hardware is essentially giving:
78 *
79 * Plane CTM -> Plane degamma -> Plane CTM -> Plane regamma -> Plane CTM
80 *
81 * The input gamma LUT block isn't really applicable here since it
82 * operates on the actual input data itself rather than the HW fp
83 * representation. The input and output CSC blocks are technically
84 * available to use as part of the DC interface but are typically used
85 * internally by DC for conversions between color spaces. These could be
86 * blended together with user adjustments in the future but for now
87 * these should remain untouched.
88 *
89 * The pipe blending also happens after these blocks so we don't
90 * actually support any CRTC props with correct blending with multiple
91 * planes - but we can still support CRTC color management properties in
92 * DM in most single plane cases correctly with clever management of the
93 * DC interface in DM.
94 *
95 * As per DRM documentation, blocks should be in hardware bypass when
96 * their respective property is set to NULL. A linear DGM/RGM LUT should
97 * also considered as putting the respective block into bypass mode.
98 *
99 * This means that the following configuration is assumed to be the
100 * default:
101 *
102 * Plane DGM Bypass -> Plane CTM Bypass -> Plane RGM Bypass -> ... CRTC
103 * DGM Bypass -> CRTC CTM Bypass -> CRTC RGM Bypass
104 *
105 * AMD Private Color Management on drm_plane
106 * -----------------------------------------
107 *
108 * The AMD private color management properties on a &drm_plane are:
109 *
110 * - AMD_PLANE_DEGAMMA_LUT
111 * - AMD_PLANE_DEGAMMA_LUT_SIZE
112 * - AMD_PLANE_DEGAMMA_TF
113 * - AMD_PLANE_HDR_MULT
114 * - AMD_PLANE_CTM
115 * - AMD_PLANE_SHAPER_LUT
116 * - AMD_PLANE_SHAPER_LUT_SIZE
117 * - AMD_PLANE_SHAPER_TF
118 * - AMD_PLANE_LUT3D
119 * - AMD_PLANE_LUT3D_SIZE
120 * - AMD_PLANE_BLEND_LUT
121 * - AMD_PLANE_BLEND_LUT_SIZE
122 * - AMD_PLANE_BLEND_TF
123 *
124 * The AMD private color management property on a &drm_crtc is:
125 *
126 * - AMD_CRTC_REGAMMA_TF
127 *
128 * Use of these properties is discouraged.
129 *
130 * AMD plane color pipeline
131 * ------------------------
132 *
133 * The AMD &drm_plane color pipeline is advertised for DCN generations
134 * 3.0 and newer. It exposes these elements in this order:
135 *
136 * 1. 1D curve colorop
137 * 2. Multiplier
138 * 3. 3x4 CTM
139 * 4. 1D curve colorop
140 * 5. 1D LUT
141 * 6. 3D LUT
142 * 7. 1D curve colorop
143 * 8. 1D LUT
144 *
145 * The multiplier (#2) is a simple multiplier that is applied to all
146 * channels.
147 *
148 * The 3x4 CTM (#3) is a simple 3x4 matrix.
149 *
150 * #1, and #7 are non-linear to linear curves. #4 is a linear to
151 * non-linear curve. They support sRGB, PQ, and BT.709/BT.2020 EOTFs or
152 * their inverse.
153 *
154 * The 1D LUTs (#5 and #8) are plain 4096 entry LUTs.
155 *
156 * The 3DLUT (#6) is a tetrahedrally interpolated 17 cube LUT.
157 *
158 */
159
160 #define MAX_DRM_LUT_VALUE 0xFFFF
161 #define MAX_DRM_LUT32_VALUE 0xFFFFFFFF
162 #define SDR_WHITE_LEVEL_INIT_VALUE 80
163
164 /**
165 * amdgpu_dm_init_color_mod - Initialize the color module.
166 *
167 * We're not using the full color module, only certain components.
168 * Only call setup functions for components that we need.
169 */
amdgpu_dm_init_color_mod(void)170 void amdgpu_dm_init_color_mod(void)
171 {
172 setup_x_points_distribution();
173 }
174
amdgpu_dm_fixpt_from_s3132(__u64 x)175 static inline struct fixed31_32 amdgpu_dm_fixpt_from_s3132(__u64 x)
176 {
177 struct fixed31_32 val;
178
179 /* If negative, convert to 2's complement. */
180 if (x & (1ULL << 63))
181 x = -(x & ~(1ULL << 63));
182
183 val.value = x;
184 return val;
185 }
186
187 #ifdef AMD_PRIVATE_COLOR
188 /* Pre-defined Transfer Functions (TF)
189 *
190 * AMD driver supports pre-defined mathematical functions for transferring
191 * between encoded values and optical/linear space. Depending on HW color caps,
192 * ROMs and curves built by the AMD color module support these transforms.
193 *
194 * The driver-specific color implementation exposes properties for pre-blending
195 * degamma TF, shaper TF (before 3D LUT), and blend(dpp.ogam) TF and
196 * post-blending regamma (mpc.ogam) TF. However, only pre-blending degamma
197 * supports ROM curves. AMD color module uses pre-defined coefficients to build
198 * curves for the other blocks. What can be done by each color block is
199 * described by struct dpp_color_capsand struct mpc_color_caps.
200 *
201 * AMD driver-specific color API exposes the following pre-defined transfer
202 * functions:
203 *
204 * - Identity: linear/identity relationship between pixel value and
205 * luminance value;
206 * - Gamma 2.2, Gamma 2.4, Gamma 2.6: pure power functions;
207 * - sRGB: 2.4: The piece-wise transfer function from IEC 61966-2-1:1999;
208 * - BT.709: has a linear segment in the bottom part and then a power function
209 * with a 0.45 (~1/2.22) gamma for the rest of the range; standardized by
210 * ITU-R BT.709-6;
211 * - PQ (Perceptual Quantizer): used for HDR display, allows luminance range
212 * capability of 0 to 10,000 nits; standardized by SMPTE ST 2084.
213 *
214 * The AMD color model is designed with an assumption that SDR (sRGB, BT.709,
215 * Gamma 2.2, etc.) peak white maps (normalized to 1.0 FP) to 80 nits in the PQ
216 * system. This has the implication that PQ EOTF (non-linear to linear) maps to
217 * [0.0..125.0] where 125.0 = 10,000 nits / 80 nits.
218 *
219 * Non-linear and linear forms are described in the table below:
220 *
221 * ┌───────────┬─────────────────────┬──────────────────────┐
222 * │ │ Non-linear │ Linear │
223 * ├───────────┼─────────────────────┼──────────────────────┤
224 * │ sRGB │ UNORM or [0.0, 1.0] │ [0.0, 1.0] │
225 * ├───────────┼─────────────────────┼──────────────────────┤
226 * │ BT709 │ UNORM or [0.0, 1.0] │ [0.0, 1.0] │
227 * ├───────────┼─────────────────────┼──────────────────────┤
228 * │ Gamma 2.x │ UNORM or [0.0, 1.0] │ [0.0, 1.0] │
229 * ├───────────┼─────────────────────┼──────────────────────┤
230 * │ PQ │ UNORM or FP16 CCCS* │ [0.0, 125.0] │
231 * ├───────────┼─────────────────────┼──────────────────────┤
232 * │ Identity │ UNORM or FP16 CCCS* │ [0.0, 1.0] or CCCS** │
233 * └───────────┴─────────────────────┴──────────────────────┘
234 * * CCCS: Windows canonical composition color space
235 * ** Respectively
236 *
237 * In the driver-specific API, color block names attached to TF properties
238 * suggest the intention regarding non-linear encoding pixel's luminance
239 * values. As some newer encodings don't use gamma curve, we make encoding and
240 * decoding explicit by defining an enum list of transfer functions supported
241 * in terms of EOTF and inverse EOTF, where:
242 *
243 * - EOTF (electro-optical transfer function): is the transfer function to go
244 * from the encoded value to an optical (linear) value. De-gamma functions
245 * traditionally do this.
246 * - Inverse EOTF (simply the inverse of the EOTF): is usually intended to go
247 * from an optical/linear space (which might have been used for blending)
248 * back to the encoded values. Gamma functions traditionally do this.
249 */
250 static const char * const
251 amdgpu_transfer_function_names[] = {
252 [AMDGPU_TRANSFER_FUNCTION_DEFAULT] = "Default",
253 [AMDGPU_TRANSFER_FUNCTION_IDENTITY] = "Identity",
254 [AMDGPU_TRANSFER_FUNCTION_SRGB_EOTF] = "sRGB EOTF",
255 [AMDGPU_TRANSFER_FUNCTION_BT709_INV_OETF] = "BT.709 inv_OETF",
256 [AMDGPU_TRANSFER_FUNCTION_PQ_EOTF] = "PQ EOTF",
257 [AMDGPU_TRANSFER_FUNCTION_GAMMA22_EOTF] = "Gamma 2.2 EOTF",
258 [AMDGPU_TRANSFER_FUNCTION_GAMMA24_EOTF] = "Gamma 2.4 EOTF",
259 [AMDGPU_TRANSFER_FUNCTION_GAMMA26_EOTF] = "Gamma 2.6 EOTF",
260 [AMDGPU_TRANSFER_FUNCTION_SRGB_INV_EOTF] = "sRGB inv_EOTF",
261 [AMDGPU_TRANSFER_FUNCTION_BT709_OETF] = "BT.709 OETF",
262 [AMDGPU_TRANSFER_FUNCTION_PQ_INV_EOTF] = "PQ inv_EOTF",
263 [AMDGPU_TRANSFER_FUNCTION_GAMMA22_INV_EOTF] = "Gamma 2.2 inv_EOTF",
264 [AMDGPU_TRANSFER_FUNCTION_GAMMA24_INV_EOTF] = "Gamma 2.4 inv_EOTF",
265 [AMDGPU_TRANSFER_FUNCTION_GAMMA26_INV_EOTF] = "Gamma 2.6 inv_EOTF",
266 };
267
268 static const u32 amdgpu_eotf =
269 BIT(AMDGPU_TRANSFER_FUNCTION_SRGB_EOTF) |
270 BIT(AMDGPU_TRANSFER_FUNCTION_BT709_INV_OETF) |
271 BIT(AMDGPU_TRANSFER_FUNCTION_PQ_EOTF) |
272 BIT(AMDGPU_TRANSFER_FUNCTION_GAMMA22_EOTF) |
273 BIT(AMDGPU_TRANSFER_FUNCTION_GAMMA24_EOTF) |
274 BIT(AMDGPU_TRANSFER_FUNCTION_GAMMA26_EOTF);
275
276 static const u32 amdgpu_inv_eotf =
277 BIT(AMDGPU_TRANSFER_FUNCTION_SRGB_INV_EOTF) |
278 BIT(AMDGPU_TRANSFER_FUNCTION_BT709_OETF) |
279 BIT(AMDGPU_TRANSFER_FUNCTION_PQ_INV_EOTF) |
280 BIT(AMDGPU_TRANSFER_FUNCTION_GAMMA22_INV_EOTF) |
281 BIT(AMDGPU_TRANSFER_FUNCTION_GAMMA24_INV_EOTF) |
282 BIT(AMDGPU_TRANSFER_FUNCTION_GAMMA26_INV_EOTF);
283
284 static struct drm_property *
amdgpu_create_tf_property(struct drm_device * dev,const char * name,u32 supported_tf)285 amdgpu_create_tf_property(struct drm_device *dev,
286 const char *name,
287 u32 supported_tf)
288 {
289 u32 transfer_functions = supported_tf |
290 BIT(AMDGPU_TRANSFER_FUNCTION_DEFAULT) |
291 BIT(AMDGPU_TRANSFER_FUNCTION_IDENTITY);
292 struct drm_prop_enum_list enum_list[AMDGPU_TRANSFER_FUNCTION_COUNT];
293 int i, len;
294
295 len = 0;
296 for (i = 0; i < AMDGPU_TRANSFER_FUNCTION_COUNT; i++) {
297 if ((transfer_functions & BIT(i)) == 0)
298 continue;
299
300 enum_list[len].type = i;
301 enum_list[len].name = amdgpu_transfer_function_names[i];
302 len++;
303 }
304
305 return drm_property_create_enum(dev, DRM_MODE_PROP_ENUM,
306 name, enum_list, len);
307 }
308
309 int
amdgpu_dm_create_color_properties(struct amdgpu_device * adev)310 amdgpu_dm_create_color_properties(struct amdgpu_device *adev)
311 {
312 struct drm_property *prop;
313
314 prop = drm_property_create(adev_to_drm(adev),
315 DRM_MODE_PROP_BLOB,
316 "AMD_PLANE_DEGAMMA_LUT", 0);
317 if (!prop)
318 return -ENOMEM;
319 adev->mode_info.plane_degamma_lut_property = prop;
320
321 prop = drm_property_create_range(adev_to_drm(adev),
322 DRM_MODE_PROP_IMMUTABLE,
323 "AMD_PLANE_DEGAMMA_LUT_SIZE",
324 0, UINT_MAX);
325 if (!prop)
326 return -ENOMEM;
327 adev->mode_info.plane_degamma_lut_size_property = prop;
328
329 prop = amdgpu_create_tf_property(adev_to_drm(adev),
330 "AMD_PLANE_DEGAMMA_TF",
331 amdgpu_eotf);
332 if (!prop)
333 return -ENOMEM;
334 adev->mode_info.plane_degamma_tf_property = prop;
335
336 prop = drm_property_create_range(adev_to_drm(adev),
337 0, "AMD_PLANE_HDR_MULT", 0, U64_MAX);
338 if (!prop)
339 return -ENOMEM;
340 adev->mode_info.plane_hdr_mult_property = prop;
341
342 prop = drm_property_create(adev_to_drm(adev),
343 DRM_MODE_PROP_BLOB,
344 "AMD_PLANE_CTM", 0);
345 if (!prop)
346 return -ENOMEM;
347 adev->mode_info.plane_ctm_property = prop;
348
349 prop = drm_property_create(adev_to_drm(adev),
350 DRM_MODE_PROP_BLOB,
351 "AMD_PLANE_SHAPER_LUT", 0);
352 if (!prop)
353 return -ENOMEM;
354 adev->mode_info.plane_shaper_lut_property = prop;
355
356 prop = drm_property_create_range(adev_to_drm(adev),
357 DRM_MODE_PROP_IMMUTABLE,
358 "AMD_PLANE_SHAPER_LUT_SIZE", 0, UINT_MAX);
359 if (!prop)
360 return -ENOMEM;
361 adev->mode_info.plane_shaper_lut_size_property = prop;
362
363 prop = amdgpu_create_tf_property(adev_to_drm(adev),
364 "AMD_PLANE_SHAPER_TF",
365 amdgpu_inv_eotf);
366 if (!prop)
367 return -ENOMEM;
368 adev->mode_info.plane_shaper_tf_property = prop;
369
370 prop = drm_property_create(adev_to_drm(adev),
371 DRM_MODE_PROP_BLOB,
372 "AMD_PLANE_LUT3D", 0);
373 if (!prop)
374 return -ENOMEM;
375 adev->mode_info.plane_lut3d_property = prop;
376
377 prop = drm_property_create_range(adev_to_drm(adev),
378 DRM_MODE_PROP_IMMUTABLE,
379 "AMD_PLANE_LUT3D_SIZE", 0, UINT_MAX);
380 if (!prop)
381 return -ENOMEM;
382 adev->mode_info.plane_lut3d_size_property = prop;
383
384 prop = drm_property_create(adev_to_drm(adev),
385 DRM_MODE_PROP_BLOB,
386 "AMD_PLANE_BLEND_LUT", 0);
387 if (!prop)
388 return -ENOMEM;
389 adev->mode_info.plane_blend_lut_property = prop;
390
391 prop = drm_property_create_range(adev_to_drm(adev),
392 DRM_MODE_PROP_IMMUTABLE,
393 "AMD_PLANE_BLEND_LUT_SIZE", 0, UINT_MAX);
394 if (!prop)
395 return -ENOMEM;
396 adev->mode_info.plane_blend_lut_size_property = prop;
397
398 prop = amdgpu_create_tf_property(adev_to_drm(adev),
399 "AMD_PLANE_BLEND_TF",
400 amdgpu_eotf);
401 if (!prop)
402 return -ENOMEM;
403 adev->mode_info.plane_blend_tf_property = prop;
404
405 prop = amdgpu_create_tf_property(adev_to_drm(adev),
406 "AMD_CRTC_REGAMMA_TF",
407 amdgpu_inv_eotf);
408 if (!prop)
409 return -ENOMEM;
410 adev->mode_info.regamma_tf_property = prop;
411
412 return 0;
413 }
414 #endif
415
416 /**
417 * __extract_blob_lut - Extracts the DRM lut and lut size from a blob.
418 * @blob: DRM color mgmt property blob
419 * @size: lut size
420 *
421 * Returns:
422 * DRM LUT or NULL
423 */
424 static const struct drm_color_lut *
__extract_blob_lut(const struct drm_property_blob * blob,uint32_t * size)425 __extract_blob_lut(const struct drm_property_blob *blob, uint32_t *size)
426 {
427 *size = blob ? drm_color_lut_size(blob) : 0;
428 return blob ? (struct drm_color_lut *)blob->data : NULL;
429 }
430
431 /**
432 * __extract_blob_lut32 - Extracts the DRM lut and lut size from a blob.
433 * @blob: DRM color mgmt property blob
434 * @size: lut size
435 *
436 * Returns:
437 * DRM LUT or NULL
438 */
439 static const struct drm_color_lut32 *
__extract_blob_lut32(const struct drm_property_blob * blob,uint32_t * size)440 __extract_blob_lut32(const struct drm_property_blob *blob, uint32_t *size)
441 {
442 *size = blob ? drm_color_lut32_size(blob) : 0;
443 return blob ? (struct drm_color_lut32 *)blob->data : NULL;
444 }
445
446 /**
447 * __is_lut_linear - check if the given lut is a linear mapping of values
448 * @lut: given lut to check values
449 * @size: lut size
450 *
451 * It is considered linear if the lut represents:
452 * f(a) = (0xFF00/MAX_COLOR_LUT_ENTRIES-1)a; for integer a in [0,
453 * MAX_COLOR_LUT_ENTRIES)
454 *
455 * Returns:
456 * True if the given lut is a linear mapping of values, i.e. it acts like a
457 * bypass LUT. Otherwise, false.
458 */
__is_lut_linear(const struct drm_color_lut * lut,uint32_t size)459 static bool __is_lut_linear(const struct drm_color_lut *lut, uint32_t size)
460 {
461 int i;
462 uint32_t expected;
463 int delta;
464
465 for (i = 0; i < size; i++) {
466 /* All color values should equal */
467 if ((lut[i].red != lut[i].green) || (lut[i].green != lut[i].blue))
468 return false;
469
470 expected = i * MAX_DRM_LUT_VALUE / (size-1);
471
472 /* Allow a +/-1 error. */
473 delta = lut[i].red - expected;
474 if (delta < -1 || 1 < delta)
475 return false;
476 }
477 return true;
478 }
479
480 /**
481 * __drm_lut_to_dc_gamma - convert the drm_color_lut to dc_gamma.
482 * @lut: DRM lookup table for color conversion
483 * @gamma: DC gamma to set entries
484 * @is_legacy: legacy or atomic gamma
485 *
486 * The conversion depends on the size of the lut - whether or not it's legacy.
487 */
__drm_lut_to_dc_gamma(const struct drm_color_lut * lut,struct dc_gamma * gamma,bool is_legacy)488 static void __drm_lut_to_dc_gamma(const struct drm_color_lut *lut,
489 struct dc_gamma *gamma, bool is_legacy)
490 {
491 uint32_t r, g, b;
492 int i;
493
494 if (is_legacy) {
495 for (i = 0; i < MAX_COLOR_LEGACY_LUT_ENTRIES; i++) {
496 r = drm_color_lut_extract(lut[i].red, 16);
497 g = drm_color_lut_extract(lut[i].green, 16);
498 b = drm_color_lut_extract(lut[i].blue, 16);
499
500 gamma->entries.red[i] = dc_fixpt_from_int(r);
501 gamma->entries.green[i] = dc_fixpt_from_int(g);
502 gamma->entries.blue[i] = dc_fixpt_from_int(b);
503 }
504 return;
505 }
506
507 /* else */
508 for (i = 0; i < MAX_COLOR_LUT_ENTRIES; i++) {
509 r = drm_color_lut_extract(lut[i].red, 16);
510 g = drm_color_lut_extract(lut[i].green, 16);
511 b = drm_color_lut_extract(lut[i].blue, 16);
512
513 gamma->entries.red[i] = dc_fixpt_from_fraction(r, MAX_DRM_LUT_VALUE);
514 gamma->entries.green[i] = dc_fixpt_from_fraction(g, MAX_DRM_LUT_VALUE);
515 gamma->entries.blue[i] = dc_fixpt_from_fraction(b, MAX_DRM_LUT_VALUE);
516 }
517 }
518
519 /**
520 * __drm_lut32_to_dc_gamma - convert the drm_color_lut to dc_gamma.
521 * @lut: DRM lookup table for color conversion
522 * @gamma: DC gamma to set entries
523 *
524 * The conversion depends on the size of the lut - whether or not it's legacy.
525 */
__drm_lut32_to_dc_gamma(const struct drm_color_lut32 * lut,struct dc_gamma * gamma)526 static void __drm_lut32_to_dc_gamma(const struct drm_color_lut32 *lut, struct dc_gamma *gamma)
527 {
528 int i;
529
530 for (i = 0; i < MAX_COLOR_LUT_ENTRIES; i++) {
531 gamma->entries.red[i] = dc_fixpt_from_fraction(lut[i].red, MAX_DRM_LUT32_VALUE);
532 gamma->entries.green[i] = dc_fixpt_from_fraction(lut[i].green, MAX_DRM_LUT32_VALUE);
533 gamma->entries.blue[i] = dc_fixpt_from_fraction(lut[i].blue, MAX_DRM_LUT32_VALUE);
534 }
535 }
536
537 /**
538 * __drm_ctm_to_dc_matrix - converts a DRM CTM to a DC CSC float matrix
539 * @ctm: DRM color transformation matrix
540 * @matrix: DC CSC float matrix
541 *
542 * The matrix needs to be a 3x4 (12 entry) matrix.
543 */
__drm_ctm_to_dc_matrix(const struct drm_color_ctm * ctm,struct fixed31_32 * matrix)544 static void __drm_ctm_to_dc_matrix(const struct drm_color_ctm *ctm,
545 struct fixed31_32 *matrix)
546 {
547 int i;
548
549 /*
550 * DRM gives a 3x3 matrix, but DC wants 3x4. Assuming we're operating
551 * with homogeneous coordinates, augment the matrix with 0's.
552 *
553 * The format provided is S31.32, using signed-magnitude representation.
554 * Our fixed31_32 is also S31.32, but is using 2's complement. We have
555 * to convert from signed-magnitude to 2's complement.
556 */
557 for (i = 0; i < 12; i++) {
558 /* Skip 4th element */
559 if (i % 4 == 3) {
560 matrix[i] = dc_fixpt_zero;
561 continue;
562 }
563
564 /* gamut_remap_matrix[i] = ctm[i - floor(i/4)] */
565 matrix[i] = amdgpu_dm_fixpt_from_s3132(ctm->matrix[i - (i / 4)]);
566 }
567 }
568
569 /**
570 * __drm_ctm_3x4_to_dc_matrix - converts a DRM CTM 3x4 to a DC CSC float matrix
571 * @ctm: DRM color transformation matrix with 3x4 dimensions
572 * @matrix: DC CSC float matrix
573 *
574 * The matrix needs to be a 3x4 (12 entry) matrix.
575 */
__drm_ctm_3x4_to_dc_matrix(const struct drm_color_ctm_3x4 * ctm,struct fixed31_32 * matrix)576 static void __drm_ctm_3x4_to_dc_matrix(const struct drm_color_ctm_3x4 *ctm,
577 struct fixed31_32 *matrix)
578 {
579 int i;
580
581 /* The format provided is S31.32, using signed-magnitude representation.
582 * Our fixed31_32 is also S31.32, but is using 2's complement. We have
583 * to convert from signed-magnitude to 2's complement.
584 */
585 for (i = 0; i < 12; i++) {
586 /* gamut_remap_matrix[i] = ctm[i - floor(i/4)] */
587 matrix[i] = amdgpu_dm_fixpt_from_s3132(ctm->matrix[i]);
588 }
589 }
590
591 /**
592 * __set_legacy_tf - Calculates the legacy transfer function
593 * @func: transfer function
594 * @lut: lookup table that defines the color space
595 * @lut_size: size of respective lut
596 * @has_rom: if ROM can be used for hardcoded curve
597 *
598 * Only for sRGB input space
599 *
600 * Returns:
601 * 0 in case of success, -ENOMEM if fails
602 */
__set_legacy_tf(struct dc_transfer_func * func,const struct drm_color_lut * lut,uint32_t lut_size,bool has_rom)603 static int __set_legacy_tf(struct dc_transfer_func *func,
604 const struct drm_color_lut *lut, uint32_t lut_size,
605 bool has_rom)
606 {
607 struct dc_gamma *gamma = NULL;
608 struct calculate_buffer cal_buffer = {0};
609 bool res;
610
611 ASSERT(lut && lut_size == MAX_COLOR_LEGACY_LUT_ENTRIES);
612
613 cal_buffer.buffer_index = -1;
614
615 gamma = dc_create_gamma();
616 if (!gamma)
617 return -ENOMEM;
618
619 gamma->type = GAMMA_RGB_256;
620 gamma->num_entries = lut_size;
621 __drm_lut_to_dc_gamma(lut, gamma, true);
622
623 res = mod_color_calculate_regamma_params(func, gamma, true, has_rom,
624 NULL, &cal_buffer);
625
626 dc_gamma_release(&gamma);
627
628 return res ? 0 : -ENOMEM;
629 }
630
631 /**
632 * __set_output_tf - calculates the output transfer function based on expected input space.
633 * @func: transfer function
634 * @lut: lookup table that defines the color space
635 * @lut_size: size of respective lut
636 * @has_rom: if ROM can be used for hardcoded curve
637 *
638 * Returns:
639 * 0 in case of success. -ENOMEM if fails.
640 */
__set_output_tf(struct dc_transfer_func * func,const struct drm_color_lut * lut,uint32_t lut_size,bool has_rom)641 static int __set_output_tf(struct dc_transfer_func *func,
642 const struct drm_color_lut *lut, uint32_t lut_size,
643 bool has_rom)
644 {
645 struct dc_gamma *gamma = NULL;
646 struct calculate_buffer cal_buffer = {0};
647 bool res;
648
649 cal_buffer.buffer_index = -1;
650
651 if (lut_size) {
652 ASSERT(lut && lut_size == MAX_COLOR_LUT_ENTRIES);
653
654 gamma = dc_create_gamma();
655 if (!gamma)
656 return -ENOMEM;
657
658 gamma->num_entries = lut_size;
659 __drm_lut_to_dc_gamma(lut, gamma, false);
660 }
661
662 if (func->tf == TRANSFER_FUNCTION_LINEAR) {
663 /*
664 * Color module doesn't like calculating regamma params
665 * on top of a linear input. But degamma params can be used
666 * instead to simulate this.
667 */
668 if (gamma)
669 gamma->type = GAMMA_CUSTOM;
670 res = mod_color_calculate_degamma_params(NULL, func,
671 gamma, gamma != NULL);
672 } else {
673 /*
674 * Assume sRGB. The actual mapping will depend on whether the
675 * input was legacy or not.
676 */
677 if (gamma)
678 gamma->type = GAMMA_CS_TFM_1D;
679 res = mod_color_calculate_regamma_params(func, gamma, gamma != NULL,
680 has_rom, NULL, &cal_buffer);
681 }
682
683 if (gamma)
684 dc_gamma_release(&gamma);
685
686 return res ? 0 : -ENOMEM;
687 }
688
689 /**
690 * __set_output_tf_32 - calculates the output transfer function based on expected input space.
691 * @func: transfer function
692 * @lut: lookup table that defines the color space
693 * @lut_size: size of respective lut
694 * @has_rom: if ROM can be used for hardcoded curve
695 *
696 * Returns:
697 * 0 in case of success. -ENOMEM if fails.
698 */
__set_output_tf_32(struct dc_transfer_func * func,const struct drm_color_lut32 * lut,uint32_t lut_size,bool has_rom)699 static int __set_output_tf_32(struct dc_transfer_func *func,
700 const struct drm_color_lut32 *lut, uint32_t lut_size,
701 bool has_rom)
702 {
703 struct dc_gamma *gamma = NULL;
704 struct calculate_buffer cal_buffer = {0};
705 bool res;
706
707 cal_buffer.buffer_index = -1;
708
709 if (lut_size) {
710 gamma = dc_create_gamma();
711 if (!gamma)
712 return -ENOMEM;
713
714 gamma->num_entries = lut_size;
715 __drm_lut32_to_dc_gamma(lut, gamma);
716 }
717
718 if (func->tf == TRANSFER_FUNCTION_LINEAR) {
719 /*
720 * Color module doesn't like calculating regamma params
721 * on top of a linear input. But degamma params can be used
722 * instead to simulate this.
723 */
724 if (gamma)
725 gamma->type = GAMMA_CUSTOM;
726 res = mod_color_calculate_degamma_params(NULL, func,
727 gamma, gamma != NULL);
728 } else {
729 /*
730 * Assume sRGB. The actual mapping will depend on whether the
731 * input was legacy or not.
732 */
733 if (gamma)
734 gamma->type = GAMMA_CS_TFM_1D;
735 res = mod_color_calculate_regamma_params(func, gamma, gamma != NULL,
736 has_rom, NULL, &cal_buffer);
737 }
738
739 if (gamma)
740 dc_gamma_release(&gamma);
741
742 return res ? 0 : -ENOMEM;
743 }
744
745
amdgpu_dm_set_atomic_regamma(struct dc_transfer_func * out_tf,const struct drm_color_lut * regamma_lut,uint32_t regamma_size,bool has_rom,enum dc_transfer_func_predefined tf)746 static int amdgpu_dm_set_atomic_regamma(struct dc_transfer_func *out_tf,
747 const struct drm_color_lut *regamma_lut,
748 uint32_t regamma_size, bool has_rom,
749 enum dc_transfer_func_predefined tf)
750 {
751 int ret = 0;
752
753 if (regamma_size || tf != TRANSFER_FUNCTION_LINEAR) {
754 /*
755 * CRTC RGM goes into RGM LUT.
756 *
757 * Note: there is no implicit sRGB regamma here. We are using
758 * degamma calculation from color module to calculate the curve
759 * from a linear base if gamma TF is not set. However, if gamma
760 * TF (!= Linear) and LUT are set at the same time, we will use
761 * regamma calculation, and the color module will combine the
762 * pre-defined TF and the custom LUT values into the LUT that's
763 * actually programmed.
764 */
765 out_tf->type = TF_TYPE_DISTRIBUTED_POINTS;
766 out_tf->tf = tf;
767 out_tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
768
769 ret = __set_output_tf(out_tf, regamma_lut, regamma_size, has_rom);
770 } else {
771 /*
772 * No CRTC RGM means we can just put the block into bypass
773 * since we don't have any plane level adjustments using it.
774 */
775 out_tf->type = TF_TYPE_BYPASS;
776 out_tf->tf = TRANSFER_FUNCTION_LINEAR;
777 }
778
779 return ret;
780 }
781
782 /**
783 * __set_input_tf - calculates the input transfer function based on expected
784 * input space.
785 * @caps: dc color capabilities
786 * @func: transfer function
787 * @lut: lookup table that defines the color space
788 * @lut_size: size of respective lut.
789 *
790 * Returns:
791 * 0 in case of success. -ENOMEM if fails.
792 */
__set_input_tf(struct dc_color_caps * caps,struct dc_transfer_func * func,const struct drm_color_lut * lut,uint32_t lut_size)793 static int __set_input_tf(struct dc_color_caps *caps, struct dc_transfer_func *func,
794 const struct drm_color_lut *lut, uint32_t lut_size)
795 {
796 struct dc_gamma *gamma = NULL;
797 bool res;
798
799 if (lut_size) {
800 gamma = dc_create_gamma();
801 if (!gamma)
802 return -ENOMEM;
803
804 gamma->type = GAMMA_CUSTOM;
805 gamma->num_entries = lut_size;
806
807 __drm_lut_to_dc_gamma(lut, gamma, false);
808 }
809
810 res = mod_color_calculate_degamma_params(caps, func, gamma, gamma != NULL);
811
812 if (gamma)
813 dc_gamma_release(&gamma);
814
815 return res ? 0 : -ENOMEM;
816 }
817
818 /**
819 * __set_input_tf_32 - calculates the input transfer function based on expected
820 * input space.
821 * @caps: dc color capabilities
822 * @func: transfer function
823 * @lut: lookup table that defines the color space
824 * @lut_size: size of respective lut.
825 *
826 * Returns:
827 * 0 in case of success. -ENOMEM if fails.
828 */
__set_input_tf_32(struct dc_color_caps * caps,struct dc_transfer_func * func,const struct drm_color_lut32 * lut,uint32_t lut_size)829 static int __set_input_tf_32(struct dc_color_caps *caps, struct dc_transfer_func *func,
830 const struct drm_color_lut32 *lut, uint32_t lut_size)
831 {
832 struct dc_gamma *gamma = NULL;
833 bool res;
834
835 if (lut_size) {
836 gamma = dc_create_gamma();
837 if (!gamma)
838 return -ENOMEM;
839
840 gamma->type = GAMMA_CUSTOM;
841 gamma->num_entries = lut_size;
842
843 __drm_lut32_to_dc_gamma(lut, gamma);
844 }
845
846 res = mod_color_calculate_degamma_params(caps, func, gamma, gamma != NULL);
847
848 if (gamma)
849 dc_gamma_release(&gamma);
850
851 return res ? 0 : -ENOMEM;
852 }
853
854 static enum dc_transfer_func_predefined
amdgpu_tf_to_dc_tf(enum amdgpu_transfer_function tf)855 amdgpu_tf_to_dc_tf(enum amdgpu_transfer_function tf)
856 {
857 switch (tf) {
858 default:
859 case AMDGPU_TRANSFER_FUNCTION_DEFAULT:
860 case AMDGPU_TRANSFER_FUNCTION_IDENTITY:
861 return TRANSFER_FUNCTION_LINEAR;
862 case AMDGPU_TRANSFER_FUNCTION_SRGB_EOTF:
863 case AMDGPU_TRANSFER_FUNCTION_SRGB_INV_EOTF:
864 return TRANSFER_FUNCTION_SRGB;
865 case AMDGPU_TRANSFER_FUNCTION_BT709_OETF:
866 case AMDGPU_TRANSFER_FUNCTION_BT709_INV_OETF:
867 return TRANSFER_FUNCTION_BT709;
868 case AMDGPU_TRANSFER_FUNCTION_PQ_EOTF:
869 case AMDGPU_TRANSFER_FUNCTION_PQ_INV_EOTF:
870 return TRANSFER_FUNCTION_PQ;
871 case AMDGPU_TRANSFER_FUNCTION_GAMMA22_EOTF:
872 case AMDGPU_TRANSFER_FUNCTION_GAMMA22_INV_EOTF:
873 return TRANSFER_FUNCTION_GAMMA22;
874 case AMDGPU_TRANSFER_FUNCTION_GAMMA24_EOTF:
875 case AMDGPU_TRANSFER_FUNCTION_GAMMA24_INV_EOTF:
876 return TRANSFER_FUNCTION_GAMMA24;
877 case AMDGPU_TRANSFER_FUNCTION_GAMMA26_EOTF:
878 case AMDGPU_TRANSFER_FUNCTION_GAMMA26_INV_EOTF:
879 return TRANSFER_FUNCTION_GAMMA26;
880 }
881 }
882
883 static enum dc_transfer_func_predefined
amdgpu_colorop_tf_to_dc_tf(enum drm_colorop_curve_1d_type tf)884 amdgpu_colorop_tf_to_dc_tf(enum drm_colorop_curve_1d_type tf)
885 {
886 switch (tf) {
887 case DRM_COLOROP_1D_CURVE_SRGB_EOTF:
888 case DRM_COLOROP_1D_CURVE_SRGB_INV_EOTF:
889 return TRANSFER_FUNCTION_SRGB;
890 case DRM_COLOROP_1D_CURVE_PQ_125_EOTF:
891 case DRM_COLOROP_1D_CURVE_PQ_125_INV_EOTF:
892 return TRANSFER_FUNCTION_PQ;
893 case DRM_COLOROP_1D_CURVE_BT2020_INV_OETF:
894 case DRM_COLOROP_1D_CURVE_BT2020_OETF:
895 return TRANSFER_FUNCTION_BT709;
896 case DRM_COLOROP_1D_CURVE_GAMMA22:
897 case DRM_COLOROP_1D_CURVE_GAMMA22_INV:
898 return TRANSFER_FUNCTION_GAMMA22;
899 default:
900 return TRANSFER_FUNCTION_LINEAR;
901 }
902 }
903
__to_dc_lut3d_color(struct dc_rgb * rgb,const struct drm_color_lut lut,int bit_precision)904 static void __to_dc_lut3d_color(struct dc_rgb *rgb,
905 const struct drm_color_lut lut,
906 int bit_precision)
907 {
908 rgb->red = drm_color_lut_extract(lut.red, bit_precision);
909 rgb->green = drm_color_lut_extract(lut.green, bit_precision);
910 rgb->blue = drm_color_lut_extract(lut.blue, bit_precision);
911 }
912
__drm_3dlut_to_dc_3dlut(const struct drm_color_lut * lut,uint32_t lut3d_size,struct tetrahedral_params * params,bool use_tetrahedral_9,int bit_depth)913 static void __drm_3dlut_to_dc_3dlut(const struct drm_color_lut *lut,
914 uint32_t lut3d_size,
915 struct tetrahedral_params *params,
916 bool use_tetrahedral_9,
917 int bit_depth)
918 {
919 struct dc_rgb *lut0;
920 struct dc_rgb *lut1;
921 struct dc_rgb *lut2;
922 struct dc_rgb *lut3;
923 int lut_i, i;
924
925
926 if (use_tetrahedral_9) {
927 lut0 = params->tetrahedral_9.lut0;
928 lut1 = params->tetrahedral_9.lut1;
929 lut2 = params->tetrahedral_9.lut2;
930 lut3 = params->tetrahedral_9.lut3;
931 } else {
932 lut0 = params->tetrahedral_17.lut0;
933 lut1 = params->tetrahedral_17.lut1;
934 lut2 = params->tetrahedral_17.lut2;
935 lut3 = params->tetrahedral_17.lut3;
936 }
937
938 for (lut_i = 0, i = 0; i < lut3d_size - 4; lut_i++, i += 4) {
939 /*
940 * We should consider the 3D LUT RGB values are distributed
941 * along four arrays lut0-3 where the first sizes 1229 and the
942 * other 1228. The bit depth supported for 3dlut channel is
943 * 12-bit, but DC also supports 10-bit.
944 *
945 * TODO: improve color pipeline API to enable the userspace set
946 * bit depth and 3D LUT size/stride, as specified by VA-API.
947 */
948 __to_dc_lut3d_color(&lut0[lut_i], lut[i], bit_depth);
949 __to_dc_lut3d_color(&lut1[lut_i], lut[i + 1], bit_depth);
950 __to_dc_lut3d_color(&lut2[lut_i], lut[i + 2], bit_depth);
951 __to_dc_lut3d_color(&lut3[lut_i], lut[i + 3], bit_depth);
952 }
953 /* lut0 has 1229 points (lut_size/4 + 1) */
954 __to_dc_lut3d_color(&lut0[lut_i], lut[i], bit_depth);
955 }
956
__to_dc_lut3d_32_color(struct dc_rgb * rgb,const struct drm_color_lut32 lut,int bit_precision)957 static void __to_dc_lut3d_32_color(struct dc_rgb *rgb,
958 const struct drm_color_lut32 lut,
959 int bit_precision)
960 {
961 rgb->red = drm_color_lut32_extract(lut.red, bit_precision);
962 rgb->green = drm_color_lut32_extract(lut.green, bit_precision);
963 rgb->blue = drm_color_lut32_extract(lut.blue, bit_precision);
964 }
965
__drm_3dlut32_to_dc_3dlut(const struct drm_color_lut32 * lut,uint32_t lut3d_size,struct tetrahedral_params * params,bool use_tetrahedral_9,int bit_depth)966 static void __drm_3dlut32_to_dc_3dlut(const struct drm_color_lut32 *lut,
967 uint32_t lut3d_size,
968 struct tetrahedral_params *params,
969 bool use_tetrahedral_9,
970 int bit_depth)
971 {
972 struct dc_rgb *lut0;
973 struct dc_rgb *lut1;
974 struct dc_rgb *lut2;
975 struct dc_rgb *lut3;
976 int lut_i, i;
977
978
979 if (use_tetrahedral_9) {
980 lut0 = params->tetrahedral_9.lut0;
981 lut1 = params->tetrahedral_9.lut1;
982 lut2 = params->tetrahedral_9.lut2;
983 lut3 = params->tetrahedral_9.lut3;
984 } else {
985 lut0 = params->tetrahedral_17.lut0;
986 lut1 = params->tetrahedral_17.lut1;
987 lut2 = params->tetrahedral_17.lut2;
988 lut3 = params->tetrahedral_17.lut3;
989 }
990
991 for (lut_i = 0, i = 0; i < lut3d_size - 4; lut_i++, i += 4) {
992 /*
993 * We should consider the 3D LUT RGB values are distributed
994 * along four arrays lut0-3 where the first sizes 1229 and the
995 * other 1228. The bit depth supported for 3dlut channel is
996 * 12-bit, but DC also supports 10-bit.
997 *
998 * TODO: improve color pipeline API to enable the userspace set
999 * bit depth and 3D LUT size/stride, as specified by VA-API.
1000 */
1001 __to_dc_lut3d_32_color(&lut0[lut_i], lut[i], bit_depth);
1002 __to_dc_lut3d_32_color(&lut1[lut_i], lut[i + 1], bit_depth);
1003 __to_dc_lut3d_32_color(&lut2[lut_i], lut[i + 2], bit_depth);
1004 __to_dc_lut3d_32_color(&lut3[lut_i], lut[i + 3], bit_depth);
1005 }
1006 /* lut0 has 1229 points (lut_size/4 + 1) */
1007 __to_dc_lut3d_32_color(&lut0[lut_i], lut[i], bit_depth);
1008 }
1009
1010 /* amdgpu_dm_atomic_lut3d - set DRM 3D LUT to DC stream
1011 * @drm_lut3d: user 3D LUT
1012 * @drm_lut3d_size: size of 3D LUT
1013 * @lut3d: DC 3D LUT
1014 *
1015 * Map user 3D LUT data to DC 3D LUT and all necessary bits to program it
1016 * on DCN accordingly.
1017 */
amdgpu_dm_atomic_lut3d(const struct drm_color_lut * drm_lut3d,uint32_t drm_lut3d_size,struct dc_3dlut * lut)1018 static void amdgpu_dm_atomic_lut3d(const struct drm_color_lut *drm_lut3d,
1019 uint32_t drm_lut3d_size,
1020 struct dc_3dlut *lut)
1021 {
1022 if (!drm_lut3d_size) {
1023 lut->state.bits.initialized = 0;
1024 } else {
1025 /* Stride and bit depth are not programmable by API yet.
1026 * Therefore, only supports 17x17x17 3D LUT (12-bit).
1027 */
1028 lut->lut_3d.use_tetrahedral_9 = false;
1029 lut->lut_3d.use_12bits = true;
1030 lut->state.bits.initialized = 1;
1031 __drm_3dlut_to_dc_3dlut(drm_lut3d, drm_lut3d_size, &lut->lut_3d,
1032 lut->lut_3d.use_tetrahedral_9,
1033 MAX_COLOR_3DLUT_BITDEPTH);
1034 }
1035 }
1036
amdgpu_dm_atomic_shaper_lut(const struct drm_color_lut * shaper_lut,bool has_rom,enum dc_transfer_func_predefined tf,uint32_t shaper_size,struct dc_transfer_func * func_shaper)1037 static int amdgpu_dm_atomic_shaper_lut(const struct drm_color_lut *shaper_lut,
1038 bool has_rom,
1039 enum dc_transfer_func_predefined tf,
1040 uint32_t shaper_size,
1041 struct dc_transfer_func *func_shaper)
1042 {
1043 int ret = 0;
1044
1045 if (shaper_size || tf != TRANSFER_FUNCTION_LINEAR) {
1046 /*
1047 * If user shaper LUT is set, we assume a linear color space
1048 * (linearized by degamma 1D LUT or not).
1049 */
1050 func_shaper->type = TF_TYPE_DISTRIBUTED_POINTS;
1051 func_shaper->tf = tf;
1052 func_shaper->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
1053
1054 ret = __set_output_tf(func_shaper, shaper_lut, shaper_size, has_rom);
1055 } else {
1056 func_shaper->type = TF_TYPE_BYPASS;
1057 func_shaper->tf = TRANSFER_FUNCTION_LINEAR;
1058 }
1059
1060 return ret;
1061 }
1062
amdgpu_dm_atomic_blend_lut(const struct drm_color_lut * blend_lut,bool has_rom,enum dc_transfer_func_predefined tf,uint32_t blend_size,struct dc_transfer_func * func_blend)1063 static int amdgpu_dm_atomic_blend_lut(const struct drm_color_lut *blend_lut,
1064 bool has_rom,
1065 enum dc_transfer_func_predefined tf,
1066 uint32_t blend_size,
1067 struct dc_transfer_func *func_blend)
1068 {
1069 int ret = 0;
1070
1071 if (blend_size || tf != TRANSFER_FUNCTION_LINEAR) {
1072 /*
1073 * DRM plane gamma LUT or TF means we are linearizing color
1074 * space before blending (similar to degamma programming). As
1075 * we don't have hardcoded curve support, or we use AMD color
1076 * module to fill the parameters that will be translated to HW
1077 * points.
1078 */
1079 func_blend->type = TF_TYPE_DISTRIBUTED_POINTS;
1080 func_blend->tf = tf;
1081 func_blend->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
1082
1083 ret = __set_input_tf(NULL, func_blend, blend_lut, blend_size);
1084 } else {
1085 func_blend->type = TF_TYPE_BYPASS;
1086 func_blend->tf = TRANSFER_FUNCTION_LINEAR;
1087 }
1088
1089 return ret;
1090 }
1091
1092 /**
1093 * amdgpu_dm_verify_lut3d_size - verifies if 3D LUT is supported and if user
1094 * shaper and 3D LUTs match the hw supported size
1095 * @adev: amdgpu device
1096 * @plane_state: the DRM plane state
1097 *
1098 * Verifies if pre-blending (DPP) 3D LUT is supported by the HW (DCN 2.0 or
1099 * newer) and if the user shaper and 3D LUTs match the supported size.
1100 *
1101 * Returns:
1102 * 0 on success. -EINVAL if lut size are invalid.
1103 */
amdgpu_dm_verify_lut3d_size(struct amdgpu_device * adev,struct drm_plane_state * plane_state)1104 int amdgpu_dm_verify_lut3d_size(struct amdgpu_device *adev,
1105 struct drm_plane_state *plane_state)
1106 {
1107 struct dm_plane_state *dm_plane_state = to_dm_plane_state(plane_state);
1108 const struct drm_color_lut *shaper = NULL, *lut3d = NULL;
1109 uint32_t exp_size, size, dim_size = MAX_COLOR_3DLUT_SIZE;
1110 bool has_3dlut = adev->dm.dc->caps.color.dpp.hw_3d_lut || adev->dm.dc->caps.color.mpc.preblend;
1111
1112 /* shaper LUT is only available if 3D LUT color caps */
1113 exp_size = has_3dlut ? MAX_COLOR_LUT_ENTRIES : 0;
1114 shaper = __extract_blob_lut(dm_plane_state->shaper_lut, &size);
1115
1116 if (shaper && size != exp_size) {
1117 drm_dbg(&adev->ddev,
1118 "Invalid Shaper LUT size. Should be %u but got %u.\n",
1119 exp_size, size);
1120 return -EINVAL;
1121 }
1122
1123 /* The number of 3D LUT entries is the dimension size cubed */
1124 exp_size = has_3dlut ? dim_size * dim_size * dim_size : 0;
1125 lut3d = __extract_blob_lut(dm_plane_state->lut3d, &size);
1126
1127 if (lut3d && size != exp_size) {
1128 drm_dbg(&adev->ddev,
1129 "Invalid 3D LUT size. Should be %u but got %u.\n",
1130 exp_size, size);
1131 return -EINVAL;
1132 }
1133
1134 return 0;
1135 }
1136
1137 /**
1138 * amdgpu_dm_verify_lut_sizes - verifies if DRM luts match the hw supported sizes
1139 * @crtc_state: the DRM CRTC state
1140 *
1141 * Verifies that the Degamma and Gamma LUTs attached to the &crtc_state
1142 * are of the expected size.
1143 *
1144 * Returns:
1145 * 0 on success. -EINVAL if any lut sizes are invalid.
1146 */
amdgpu_dm_verify_lut_sizes(const struct drm_crtc_state * crtc_state)1147 int amdgpu_dm_verify_lut_sizes(const struct drm_crtc_state *crtc_state)
1148 {
1149 const struct drm_color_lut *lut = NULL;
1150 uint32_t size = 0;
1151
1152 lut = __extract_blob_lut(crtc_state->degamma_lut, &size);
1153 if (lut && size != MAX_COLOR_LUT_ENTRIES) {
1154 DRM_DEBUG_DRIVER(
1155 "Invalid Degamma LUT size. Should be %u but got %u.\n",
1156 MAX_COLOR_LUT_ENTRIES, size);
1157 return -EINVAL;
1158 }
1159
1160 lut = __extract_blob_lut(crtc_state->gamma_lut, &size);
1161 if (lut && size != MAX_COLOR_LUT_ENTRIES &&
1162 size != MAX_COLOR_LEGACY_LUT_ENTRIES) {
1163 DRM_DEBUG_DRIVER(
1164 "Invalid Gamma LUT size. Should be %u (or %u for legacy) but got %u.\n",
1165 MAX_COLOR_LUT_ENTRIES, MAX_COLOR_LEGACY_LUT_ENTRIES,
1166 size);
1167 return -EINVAL;
1168 }
1169
1170 return 0;
1171 }
1172
1173 /**
1174 * amdgpu_dm_check_crtc_color_mgmt: Check if DRM color props are programmable by DC.
1175 * @crtc: amdgpu_dm crtc state
1176 * @check_only: only check color state without update dc stream
1177 *
1178 * This function just verifies CRTC LUT sizes, if there is enough space for
1179 * output transfer function and if its parameters can be calculated by AMD
1180 * color module. It also adjusts some settings for programming CRTC degamma at
1181 * plane stage, using plane DGM block.
1182 *
1183 * The RGM block is typically more fully featured and accurate across
1184 * all ASICs - DCE can't support a custom non-linear CRTC DGM.
1185 *
1186 * For supporting both plane level color management and CRTC level color
1187 * management at once we have to either restrict the usage of some CRTC
1188 * properties or blend adjustments together.
1189 *
1190 * Returns:
1191 * 0 on success. Error code if validation fails.
1192 */
1193
amdgpu_dm_check_crtc_color_mgmt(struct dm_crtc_state * crtc,bool check_only)1194 int amdgpu_dm_check_crtc_color_mgmt(struct dm_crtc_state *crtc,
1195 bool check_only)
1196 {
1197 struct dc_stream_state *stream = crtc->stream;
1198 struct amdgpu_device *adev = drm_to_adev(crtc->base.state->dev);
1199 bool has_rom = adev->asic_type <= CHIP_RAVEN;
1200 struct dc_transfer_func *out_tf;
1201 const struct drm_color_lut *degamma_lut, *regamma_lut;
1202 uint32_t degamma_size, regamma_size;
1203 bool has_regamma, has_degamma;
1204 enum dc_transfer_func_predefined tf = TRANSFER_FUNCTION_LINEAR;
1205 bool is_legacy;
1206 int r;
1207
1208 tf = amdgpu_tf_to_dc_tf(crtc->regamma_tf);
1209
1210 r = amdgpu_dm_verify_lut_sizes(&crtc->base);
1211 if (r)
1212 return r;
1213
1214 degamma_lut = __extract_blob_lut(crtc->base.degamma_lut, °amma_size);
1215 regamma_lut = __extract_blob_lut(crtc->base.gamma_lut, ®amma_size);
1216
1217 has_degamma =
1218 degamma_lut && !__is_lut_linear(degamma_lut, degamma_size);
1219
1220 has_regamma =
1221 regamma_lut && !__is_lut_linear(regamma_lut, regamma_size);
1222
1223 is_legacy = regamma_size == MAX_COLOR_LEGACY_LUT_ENTRIES;
1224
1225 /* Reset all adjustments. */
1226 crtc->cm_has_degamma = false;
1227 crtc->cm_is_degamma_srgb = false;
1228
1229 if (check_only) {
1230 out_tf = kvzalloc_obj(*out_tf);
1231 if (!out_tf)
1232 return -ENOMEM;
1233 } else {
1234 out_tf = &stream->out_transfer_func;
1235 }
1236
1237 /* Setup regamma and degamma. */
1238 if (is_legacy) {
1239 /*
1240 * Legacy regamma forces us to use the sRGB RGM as a base.
1241 * This also means we can't use linear DGM since DGM needs
1242 * to use sRGB as a base as well, resulting in incorrect CRTC
1243 * DGM and CRTC CTM.
1244 *
1245 * TODO: Just map this to the standard regamma interface
1246 * instead since this isn't really right. One of the cases
1247 * where this setup currently fails is trying to do an
1248 * inverse color ramp in legacy userspace.
1249 */
1250 crtc->cm_is_degamma_srgb = true;
1251 out_tf->type = TF_TYPE_DISTRIBUTED_POINTS;
1252 out_tf->tf = TRANSFER_FUNCTION_SRGB;
1253 /*
1254 * Note: although we pass has_rom as parameter here, we never
1255 * actually use ROM because the color module only takes the ROM
1256 * path if transfer_func->type == PREDEFINED.
1257 *
1258 * See more in mod_color_calculate_regamma_params()
1259 */
1260 r = __set_legacy_tf(out_tf, regamma_lut,
1261 regamma_size, has_rom);
1262 } else {
1263 regamma_size = has_regamma ? regamma_size : 0;
1264 r = amdgpu_dm_set_atomic_regamma(out_tf, regamma_lut,
1265 regamma_size, has_rom, tf);
1266 }
1267
1268 /*
1269 * CRTC DGM goes into DGM LUT. It would be nice to place it
1270 * into the RGM since it's a more featured block but we'd
1271 * have to place the CTM in the OCSC in that case.
1272 */
1273 crtc->cm_has_degamma = has_degamma;
1274 if (check_only)
1275 kvfree(out_tf);
1276
1277 return r;
1278 }
1279
1280 /**
1281 * amdgpu_dm_update_crtc_color_mgmt: Maps DRM color management to DC stream.
1282 * @crtc: amdgpu_dm crtc state
1283 *
1284 * With no plane level color management properties we're free to use any
1285 * of the HW blocks as long as the CRTC CTM always comes before the
1286 * CRTC RGM and after the CRTC DGM.
1287 *
1288 * - The CRTC RGM block will be placed in the RGM LUT block if it is non-linear.
1289 * - The CRTC DGM block will be placed in the DGM LUT block if it is non-linear.
1290 * - The CRTC CTM will be placed in the gamut remap block if it is non-linear.
1291 *
1292 * The RGM block is typically more fully featured and accurate across
1293 * all ASICs - DCE can't support a custom non-linear CRTC DGM.
1294 *
1295 * For supporting both plane level color management and CRTC level color
1296 * management at once we have to either restrict the usage of CRTC properties
1297 * or blend adjustments together.
1298 *
1299 * Returns:
1300 * 0 on success. Error code if setup fails.
1301 */
amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state * crtc)1302 int amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state *crtc)
1303 {
1304 struct dc_stream_state *stream = crtc->stream;
1305 struct drm_color_ctm *ctm = NULL;
1306 int ret;
1307
1308 ret = amdgpu_dm_check_crtc_color_mgmt(crtc, false);
1309 if (ret)
1310 return ret;
1311
1312 /* Setup CRTC CTM. */
1313 if (crtc->base.ctm) {
1314 ctm = (struct drm_color_ctm *)crtc->base.ctm->data;
1315
1316 /*
1317 * Gamut remapping must be used for gamma correction
1318 * since it comes before the regamma correction.
1319 *
1320 * OCSC could be used for gamma correction, but we'd need to
1321 * blend the adjustments together with the required output
1322 * conversion matrix - so just use the gamut remap block
1323 * for now.
1324 */
1325 __drm_ctm_to_dc_matrix(ctm, stream->gamut_remap_matrix.matrix);
1326
1327 stream->gamut_remap_matrix.enable_remap = true;
1328 stream->csc_color_matrix.enable_adjustment = false;
1329 } else {
1330 /* Bypass CTM. */
1331 stream->gamut_remap_matrix.enable_remap = false;
1332 stream->csc_color_matrix.enable_adjustment = false;
1333 }
1334
1335 return 0;
1336 }
1337
1338 static int
map_crtc_degamma_to_dc_plane(struct dm_crtc_state * crtc,struct dc_plane_state * dc_plane_state,struct dc_color_caps * caps)1339 map_crtc_degamma_to_dc_plane(struct dm_crtc_state *crtc,
1340 struct dc_plane_state *dc_plane_state,
1341 struct dc_color_caps *caps)
1342 {
1343 const struct drm_color_lut *degamma_lut;
1344 enum dc_transfer_func_predefined tf = TRANSFER_FUNCTION_SRGB;
1345 uint32_t degamma_size;
1346 int r;
1347
1348 /* Get the correct base transfer function for implicit degamma. */
1349 switch (dc_plane_state->format) {
1350 case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
1351 case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
1352 /* DC doesn't have a transfer function for BT601 specifically. */
1353 tf = TRANSFER_FUNCTION_BT709;
1354 break;
1355 default:
1356 break;
1357 }
1358
1359 if (crtc->cm_has_degamma) {
1360 degamma_lut = __extract_blob_lut(crtc->base.degamma_lut,
1361 °amma_size);
1362 ASSERT(degamma_size == MAX_COLOR_LUT_ENTRIES);
1363
1364 dc_plane_state->in_transfer_func.type = TF_TYPE_DISTRIBUTED_POINTS;
1365
1366 /*
1367 * This case isn't fully correct, but also fairly
1368 * uncommon. This is userspace trying to use a
1369 * legacy gamma LUT + atomic degamma LUT
1370 * at the same time.
1371 *
1372 * Legacy gamma requires the input to be in linear
1373 * space, so that means we need to apply an sRGB
1374 * degamma. But color module also doesn't support
1375 * a user ramp in this case so the degamma will
1376 * be lost.
1377 *
1378 * Even if we did support it, it's still not right:
1379 *
1380 * Input -> CRTC DGM -> sRGB DGM -> CRTC CTM ->
1381 * sRGB RGM -> CRTC RGM -> Output
1382 *
1383 * The CSC will be done in the wrong space since
1384 * we're applying an sRGB DGM on top of the CRTC
1385 * DGM.
1386 *
1387 * TODO: Don't use the legacy gamma interface and just
1388 * map these to the atomic one instead.
1389 */
1390 if (crtc->cm_is_degamma_srgb)
1391 dc_plane_state->in_transfer_func.tf = tf;
1392 else
1393 dc_plane_state->in_transfer_func.tf =
1394 TRANSFER_FUNCTION_LINEAR;
1395
1396 r = __set_input_tf(caps, &dc_plane_state->in_transfer_func,
1397 degamma_lut, degamma_size);
1398 if (r)
1399 return r;
1400 } else {
1401 /*
1402 * For legacy gamma support we need the regamma input
1403 * in linear space. Assume that the input is sRGB.
1404 */
1405 dc_plane_state->in_transfer_func.type = TF_TYPE_PREDEFINED;
1406 dc_plane_state->in_transfer_func.tf = tf;
1407
1408 if (tf != TRANSFER_FUNCTION_SRGB &&
1409 !mod_color_calculate_degamma_params(caps,
1410 &dc_plane_state->in_transfer_func,
1411 NULL, false))
1412 return -ENOMEM;
1413 }
1414
1415 return 0;
1416 }
1417
1418 static int
__set_dm_plane_degamma(struct drm_plane_state * plane_state,struct dc_plane_state * dc_plane_state,struct dc_color_caps * color_caps)1419 __set_dm_plane_degamma(struct drm_plane_state *plane_state,
1420 struct dc_plane_state *dc_plane_state,
1421 struct dc_color_caps *color_caps)
1422 {
1423 struct dm_plane_state *dm_plane_state = to_dm_plane_state(plane_state);
1424 const struct drm_color_lut *degamma_lut;
1425 enum amdgpu_transfer_function tf = AMDGPU_TRANSFER_FUNCTION_DEFAULT;
1426 uint32_t degamma_size;
1427 bool has_degamma_lut;
1428 int ret;
1429
1430 degamma_lut = __extract_blob_lut(dm_plane_state->degamma_lut,
1431 °amma_size);
1432
1433 has_degamma_lut = degamma_lut &&
1434 !__is_lut_linear(degamma_lut, degamma_size);
1435
1436 tf = dm_plane_state->degamma_tf;
1437
1438 /* If we don't have plane degamma LUT nor TF to set on DC, we have
1439 * nothing to do here, return.
1440 */
1441 if (!has_degamma_lut && tf == AMDGPU_TRANSFER_FUNCTION_DEFAULT)
1442 return -EINVAL;
1443
1444 dc_plane_state->in_transfer_func.tf = amdgpu_tf_to_dc_tf(tf);
1445
1446 if (has_degamma_lut) {
1447 ASSERT(degamma_size == MAX_COLOR_LUT_ENTRIES);
1448
1449 dc_plane_state->in_transfer_func.type =
1450 TF_TYPE_DISTRIBUTED_POINTS;
1451
1452 ret = __set_input_tf(color_caps, &dc_plane_state->in_transfer_func,
1453 degamma_lut, degamma_size);
1454 if (ret)
1455 return ret;
1456 } else {
1457 dc_plane_state->in_transfer_func.type =
1458 TF_TYPE_PREDEFINED;
1459
1460 if (!mod_color_calculate_degamma_params(color_caps,
1461 &dc_plane_state->in_transfer_func, NULL, false))
1462 return -ENOMEM;
1463 }
1464 return 0;
1465 }
1466
1467 static int
__set_colorop_in_tf_1d_curve(struct dc_plane_state * dc_plane_state,struct drm_colorop_state * colorop_state)1468 __set_colorop_in_tf_1d_curve(struct dc_plane_state *dc_plane_state,
1469 struct drm_colorop_state *colorop_state)
1470 {
1471 struct dc_transfer_func *tf = &dc_plane_state->in_transfer_func;
1472 struct drm_colorop *colorop = colorop_state->colorop;
1473 struct drm_device *drm = colorop->dev;
1474
1475 if (colorop->type != DRM_COLOROP_1D_CURVE)
1476 return -EINVAL;
1477
1478 if (!(BIT(colorop_state->curve_1d_type) & amdgpu_dm_supported_degam_tfs))
1479 return -EINVAL;
1480
1481 if (colorop_state->bypass) {
1482 tf->type = TF_TYPE_BYPASS;
1483 tf->tf = TRANSFER_FUNCTION_LINEAR;
1484 return 0;
1485 }
1486
1487 drm_dbg(drm, "Degamma colorop with ID: %d\n", colorop->base.id);
1488
1489 tf->type = TF_TYPE_PREDEFINED;
1490 tf->tf = amdgpu_colorop_tf_to_dc_tf(colorop_state->curve_1d_type);
1491
1492 return 0;
1493 }
1494
1495 static int
__set_dm_plane_colorop_degamma(struct drm_plane_state * plane_state,struct dc_plane_state * dc_plane_state,struct drm_colorop * colorop)1496 __set_dm_plane_colorop_degamma(struct drm_plane_state *plane_state,
1497 struct dc_plane_state *dc_plane_state,
1498 struct drm_colorop *colorop)
1499 {
1500 struct drm_colorop *old_colorop;
1501 struct drm_colorop_state *colorop_state = NULL, *new_colorop_state;
1502 struct drm_atomic_state *state = plane_state->state;
1503 int i = 0;
1504
1505 old_colorop = colorop;
1506
1507 /* 1st op: 1d curve - degamma */
1508 for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
1509 if (new_colorop_state->colorop == old_colorop &&
1510 (BIT(new_colorop_state->curve_1d_type) & amdgpu_dm_supported_degam_tfs)) {
1511 colorop_state = new_colorop_state;
1512 break;
1513 }
1514 }
1515
1516 if (!colorop_state)
1517 return -EINVAL;
1518
1519 return __set_colorop_in_tf_1d_curve(dc_plane_state, colorop_state);
1520 }
1521
1522 static int
__set_dm_plane_colorop_3x4_matrix(struct drm_plane_state * plane_state,struct dc_plane_state * dc_plane_state,struct drm_colorop * colorop)1523 __set_dm_plane_colorop_3x4_matrix(struct drm_plane_state *plane_state,
1524 struct dc_plane_state *dc_plane_state,
1525 struct drm_colorop *colorop)
1526 {
1527 struct drm_colorop *old_colorop;
1528 struct drm_colorop_state *colorop_state = NULL, *new_colorop_state;
1529 struct drm_atomic_state *state = plane_state->state;
1530 const struct drm_device *dev = colorop->dev;
1531 const struct drm_property_blob *blob;
1532 struct drm_color_ctm_3x4 *ctm = NULL;
1533 int i = 0;
1534
1535 /* 3x4 matrix */
1536 old_colorop = colorop;
1537 for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
1538 if (new_colorop_state->colorop == old_colorop &&
1539 new_colorop_state->colorop->type == DRM_COLOROP_CTM_3X4) {
1540 colorop_state = new_colorop_state;
1541 break;
1542 }
1543 }
1544
1545 if (colorop_state && !colorop_state->bypass && colorop->type == DRM_COLOROP_CTM_3X4) {
1546 drm_dbg(dev, "3x4 matrix colorop with ID: %d\n", colorop->base.id);
1547 blob = colorop_state->data;
1548 if (blob->length == sizeof(struct drm_color_ctm_3x4)) {
1549 ctm = (struct drm_color_ctm_3x4 *) blob->data;
1550 __drm_ctm_3x4_to_dc_matrix(ctm, dc_plane_state->gamut_remap_matrix.matrix);
1551 dc_plane_state->gamut_remap_matrix.enable_remap = true;
1552 dc_plane_state->input_csc_color_matrix.enable_adjustment = false;
1553 } else {
1554 drm_warn(dev, "blob->length (%zu) isn't equal to drm_color_ctm_3x4 (%zu)\n",
1555 blob->length, sizeof(struct drm_color_ctm_3x4));
1556 return -EINVAL;
1557 }
1558 }
1559
1560 return 0;
1561 }
1562
1563 static int
__set_dm_plane_colorop_multiplier(struct drm_plane_state * plane_state,struct dc_plane_state * dc_plane_state,struct drm_colorop * colorop)1564 __set_dm_plane_colorop_multiplier(struct drm_plane_state *plane_state,
1565 struct dc_plane_state *dc_plane_state,
1566 struct drm_colorop *colorop)
1567 {
1568 struct drm_colorop *old_colorop;
1569 struct drm_colorop_state *colorop_state = NULL, *new_colorop_state;
1570 struct drm_atomic_state *state = plane_state->state;
1571 const struct drm_device *dev = colorop->dev;
1572 int i = 0;
1573
1574 /* Multiplier */
1575 old_colorop = colorop;
1576 for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
1577 if (new_colorop_state->colorop == old_colorop &&
1578 new_colorop_state->colorop->type == DRM_COLOROP_MULTIPLIER) {
1579 colorop_state = new_colorop_state;
1580 break;
1581 }
1582 }
1583
1584 if (colorop_state && !colorop_state->bypass && colorop->type == DRM_COLOROP_MULTIPLIER) {
1585 drm_dbg(dev, "Multiplier colorop with ID: %d\n", colorop->base.id);
1586 dc_plane_state->hdr_mult = amdgpu_dm_fixpt_from_s3132(colorop_state->multiplier);
1587 }
1588
1589 return 0;
1590 }
1591
1592 static int
__set_dm_plane_colorop_shaper(struct drm_plane_state * plane_state,struct dc_plane_state * dc_plane_state,struct drm_colorop * colorop)1593 __set_dm_plane_colorop_shaper(struct drm_plane_state *plane_state,
1594 struct dc_plane_state *dc_plane_state,
1595 struct drm_colorop *colorop)
1596 {
1597 struct drm_colorop *old_colorop;
1598 struct drm_colorop_state *colorop_state = NULL, *new_colorop_state;
1599 struct drm_atomic_state *state = plane_state->state;
1600 enum dc_transfer_func_predefined default_tf = TRANSFER_FUNCTION_LINEAR;
1601 struct dc_transfer_func *tf = &dc_plane_state->in_shaper_func;
1602 const struct drm_color_lut32 *shaper_lut;
1603 struct drm_device *dev = colorop->dev;
1604 bool enabled = false;
1605 u32 shaper_size;
1606 int i = 0, ret = 0;
1607
1608 /* 1D Curve - SHAPER TF */
1609 old_colorop = colorop;
1610 for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
1611 if (new_colorop_state->colorop == old_colorop &&
1612 (BIT(new_colorop_state->curve_1d_type) & amdgpu_dm_supported_shaper_tfs)) {
1613 colorop_state = new_colorop_state;
1614 break;
1615 }
1616 }
1617
1618 if (colorop_state && !colorop_state->bypass && colorop->type == DRM_COLOROP_1D_CURVE) {
1619 drm_dbg(dev, "Shaper TF colorop with ID: %d\n", colorop->base.id);
1620 tf->type = TF_TYPE_DISTRIBUTED_POINTS;
1621 tf->tf = default_tf = amdgpu_colorop_tf_to_dc_tf(colorop_state->curve_1d_type);
1622 tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
1623 ret = __set_output_tf(tf, 0, 0, false);
1624 if (ret)
1625 return ret;
1626 enabled = true;
1627 }
1628
1629 /* 1D LUT - SHAPER LUT */
1630 colorop = old_colorop->next;
1631 if (!colorop) {
1632 drm_dbg(dev, "no Shaper LUT colorop found\n");
1633 return -EINVAL;
1634 }
1635
1636 old_colorop = colorop;
1637 for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
1638 if (new_colorop_state->colorop == old_colorop &&
1639 new_colorop_state->colorop->type == DRM_COLOROP_1D_LUT) {
1640 colorop_state = new_colorop_state;
1641 break;
1642 }
1643 }
1644
1645 if (colorop_state && !colorop_state->bypass && colorop->type == DRM_COLOROP_1D_LUT) {
1646 drm_dbg(dev, "Shaper LUT colorop with ID: %d\n", colorop->base.id);
1647 tf->type = TF_TYPE_DISTRIBUTED_POINTS;
1648 tf->tf = default_tf;
1649 tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
1650 shaper_lut = __extract_blob_lut32(colorop_state->data, &shaper_size);
1651 shaper_size = shaper_lut != NULL ? shaper_size : 0;
1652
1653 /* Custom LUT size must be the same as supported size */
1654 if (shaper_size == colorop->size) {
1655 ret = __set_output_tf_32(tf, shaper_lut, shaper_size, false);
1656 if (ret)
1657 return ret;
1658 enabled = true;
1659 }
1660 }
1661
1662 if (!enabled)
1663 tf->type = TF_TYPE_BYPASS;
1664
1665 return 0;
1666 }
1667
1668 /* __set_colorop_3dlut - set DRM 3D LUT to DC stream
1669 * @drm_lut3d: user 3D LUT
1670 * @drm_lut3d_size: size of 3D LUT
1671 * @lut3d: DC 3D LUT
1672 *
1673 * Map user 3D LUT data to DC 3D LUT and all necessary bits to program it
1674 * on DCN accordingly.
1675 *
1676 * Returns:
1677 * 0 on success. -EINVAL if drm_lut3d_size is zero.
1678 */
__set_colorop_3dlut(const struct drm_color_lut32 * drm_lut3d,uint32_t drm_lut3d_size,struct dc_3dlut * lut)1679 static int __set_colorop_3dlut(const struct drm_color_lut32 *drm_lut3d,
1680 uint32_t drm_lut3d_size,
1681 struct dc_3dlut *lut)
1682 {
1683 if (!drm_lut3d_size) {
1684 lut->state.bits.initialized = 0;
1685 return -EINVAL;
1686 }
1687
1688 /* Only supports 17x17x17 3D LUT (12-bit) now */
1689 lut->lut_3d.use_12bits = true;
1690 lut->lut_3d.use_tetrahedral_9 = false;
1691
1692 lut->state.bits.initialized = 1;
1693 __drm_3dlut32_to_dc_3dlut(drm_lut3d, drm_lut3d_size, &lut->lut_3d,
1694 lut->lut_3d.use_tetrahedral_9, 12);
1695
1696 return 0;
1697 }
1698
1699 static int
__set_dm_plane_colorop_3dlut(struct drm_plane_state * plane_state,struct dc_plane_state * dc_plane_state,struct drm_colorop * colorop)1700 __set_dm_plane_colorop_3dlut(struct drm_plane_state *plane_state,
1701 struct dc_plane_state *dc_plane_state,
1702 struct drm_colorop *colorop)
1703 {
1704 struct drm_colorop *old_colorop;
1705 struct drm_colorop_state *colorop_state = NULL, *new_colorop_state;
1706 struct dc_transfer_func *tf = &dc_plane_state->in_shaper_func;
1707 struct drm_atomic_state *state = plane_state->state;
1708 const struct amdgpu_device *adev = drm_to_adev(colorop->dev);
1709 const struct drm_device *dev = colorop->dev;
1710 const struct drm_color_lut32 *lut3d;
1711 uint32_t lut3d_size;
1712 int i = 0, ret = 0;
1713
1714 /* 3D LUT */
1715 old_colorop = colorop;
1716 for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
1717 if (new_colorop_state->colorop == old_colorop &&
1718 new_colorop_state->colorop->type == DRM_COLOROP_3D_LUT) {
1719 colorop_state = new_colorop_state;
1720 break;
1721 }
1722 }
1723
1724 if (colorop_state && !colorop_state->bypass && colorop->type == DRM_COLOROP_3D_LUT) {
1725 if (!adev->dm.dc->caps.color.dpp.hw_3d_lut) {
1726 drm_dbg(dev, "3D LUT is not supported by hardware\n");
1727 return -EINVAL;
1728 }
1729
1730 drm_dbg(dev, "3D LUT colorop with ID: %d\n", colorop->base.id);
1731 lut3d = __extract_blob_lut32(colorop_state->data, &lut3d_size);
1732 lut3d_size = lut3d != NULL ? lut3d_size : 0;
1733 ret = __set_colorop_3dlut(lut3d, lut3d_size, &dc_plane_state->lut3d_func);
1734 if (ret) {
1735 drm_dbg(dev, "3D LUT colorop with ID: %d has LUT size = %d\n",
1736 colorop->base.id, lut3d_size);
1737 return ret;
1738 }
1739
1740 /* 3D LUT requires shaper. If shaper colorop is bypassed, enable shaper curve
1741 * with TRANSFER_FUNCTION_LINEAR
1742 */
1743 if (tf->type == TF_TYPE_BYPASS) {
1744 tf->type = TF_TYPE_DISTRIBUTED_POINTS;
1745 tf->tf = TRANSFER_FUNCTION_LINEAR;
1746 tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
1747 ret = __set_output_tf_32(tf, NULL, 0, false);
1748 }
1749 }
1750
1751 return ret;
1752 }
1753
1754 static int
__set_dm_plane_colorop_blend(struct drm_plane_state * plane_state,struct dc_plane_state * dc_plane_state,struct drm_colorop * colorop)1755 __set_dm_plane_colorop_blend(struct drm_plane_state *plane_state,
1756 struct dc_plane_state *dc_plane_state,
1757 struct drm_colorop *colorop)
1758 {
1759 struct drm_colorop *old_colorop;
1760 struct drm_colorop_state *colorop_state = NULL, *new_colorop_state;
1761 struct drm_atomic_state *state = plane_state->state;
1762 enum dc_transfer_func_predefined default_tf = TRANSFER_FUNCTION_LINEAR;
1763 struct dc_transfer_func *tf = &dc_plane_state->blend_tf;
1764 const struct drm_color_lut32 *blend_lut = NULL;
1765 struct drm_device *dev = colorop->dev;
1766 uint32_t blend_size = 0;
1767 int i = 0;
1768
1769 /* 1D Curve - BLND TF */
1770 old_colorop = colorop;
1771 for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
1772 if (new_colorop_state->colorop == old_colorop &&
1773 (BIT(new_colorop_state->curve_1d_type) & amdgpu_dm_supported_blnd_tfs)) {
1774 colorop_state = new_colorop_state;
1775 break;
1776 }
1777 }
1778
1779 if (colorop_state && !colorop_state->bypass && colorop->type == DRM_COLOROP_1D_CURVE &&
1780 (BIT(colorop_state->curve_1d_type) & amdgpu_dm_supported_blnd_tfs)) {
1781 drm_dbg(dev, "Blend TF colorop with ID: %d\n", colorop->base.id);
1782 tf->type = TF_TYPE_DISTRIBUTED_POINTS;
1783 tf->tf = default_tf = amdgpu_colorop_tf_to_dc_tf(colorop_state->curve_1d_type);
1784 tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
1785 __set_input_tf_32(NULL, tf, blend_lut, blend_size);
1786 }
1787
1788 /* 1D Curve - BLND LUT */
1789 colorop = old_colorop->next;
1790 if (!colorop) {
1791 drm_dbg(dev, "no Blend LUT colorop found\n");
1792 return -EINVAL;
1793 }
1794
1795 old_colorop = colorop;
1796 for_each_new_colorop_in_state(state, colorop, new_colorop_state, i) {
1797 if (new_colorop_state->colorop == old_colorop &&
1798 new_colorop_state->colorop->type == DRM_COLOROP_1D_LUT) {
1799 colorop_state = new_colorop_state;
1800 break;
1801 }
1802 }
1803
1804 if (colorop_state && !colorop_state->bypass && colorop->type == DRM_COLOROP_1D_LUT &&
1805 (BIT(colorop_state->curve_1d_type) & amdgpu_dm_supported_blnd_tfs)) {
1806 drm_dbg(dev, "Blend LUT colorop with ID: %d\n", colorop->base.id);
1807 tf->type = TF_TYPE_DISTRIBUTED_POINTS;
1808 tf->tf = default_tf;
1809 tf->sdr_ref_white_level = SDR_WHITE_LEVEL_INIT_VALUE;
1810 blend_lut = __extract_blob_lut32(colorop_state->data, &blend_size);
1811 blend_size = blend_lut != NULL ? blend_size : 0;
1812
1813 /* Custom LUT size must be the same as supported size */
1814 if (blend_size == colorop->size)
1815 __set_input_tf_32(NULL, tf, blend_lut, blend_size);
1816 }
1817
1818 return 0;
1819 }
1820
1821 static int
amdgpu_dm_plane_set_color_properties(struct drm_plane_state * plane_state,struct dc_plane_state * dc_plane_state)1822 amdgpu_dm_plane_set_color_properties(struct drm_plane_state *plane_state,
1823 struct dc_plane_state *dc_plane_state)
1824 {
1825 struct dm_plane_state *dm_plane_state = to_dm_plane_state(plane_state);
1826 enum amdgpu_transfer_function shaper_tf = AMDGPU_TRANSFER_FUNCTION_DEFAULT;
1827 enum amdgpu_transfer_function blend_tf = AMDGPU_TRANSFER_FUNCTION_DEFAULT;
1828 const struct drm_color_lut *shaper_lut, *lut3d, *blend_lut;
1829 uint32_t shaper_size, lut3d_size, blend_size;
1830 int ret;
1831
1832 dc_plane_state->hdr_mult = amdgpu_dm_fixpt_from_s3132(dm_plane_state->hdr_mult);
1833
1834 shaper_lut = __extract_blob_lut(dm_plane_state->shaper_lut, &shaper_size);
1835 shaper_size = shaper_lut != NULL ? shaper_size : 0;
1836 shaper_tf = dm_plane_state->shaper_tf;
1837 lut3d = __extract_blob_lut(dm_plane_state->lut3d, &lut3d_size);
1838 lut3d_size = lut3d != NULL ? lut3d_size : 0;
1839
1840 amdgpu_dm_atomic_lut3d(lut3d, lut3d_size, &dc_plane_state->lut3d_func);
1841 ret = amdgpu_dm_atomic_shaper_lut(shaper_lut, false,
1842 amdgpu_tf_to_dc_tf(shaper_tf),
1843 shaper_size,
1844 &dc_plane_state->in_shaper_func);
1845 if (ret) {
1846 drm_dbg_kms(plane_state->plane->dev,
1847 "setting plane %d shaper LUT failed.\n",
1848 plane_state->plane->index);
1849
1850 return ret;
1851 }
1852
1853 blend_tf = dm_plane_state->blend_tf;
1854 blend_lut = __extract_blob_lut(dm_plane_state->blend_lut, &blend_size);
1855 blend_size = blend_lut != NULL ? blend_size : 0;
1856
1857 ret = amdgpu_dm_atomic_blend_lut(blend_lut, false,
1858 amdgpu_tf_to_dc_tf(blend_tf),
1859 blend_size, &dc_plane_state->blend_tf);
1860 if (ret) {
1861 drm_dbg_kms(plane_state->plane->dev,
1862 "setting plane %d gamma lut failed.\n",
1863 plane_state->plane->index);
1864
1865 return ret;
1866 }
1867
1868 return 0;
1869 }
1870
1871 static int
amdgpu_dm_plane_set_colorop_properties(struct drm_plane_state * plane_state,struct dc_plane_state * dc_plane_state)1872 amdgpu_dm_plane_set_colorop_properties(struct drm_plane_state *plane_state,
1873 struct dc_plane_state *dc_plane_state)
1874 {
1875 struct drm_colorop *colorop = plane_state->color_pipeline;
1876 struct drm_device *dev = plane_state->plane->dev;
1877 struct amdgpu_device *adev = drm_to_adev(dev);
1878 int ret;
1879
1880 /* 1D Curve - DEGAM TF */
1881 if (!colorop)
1882 return -EINVAL;
1883
1884 ret = __set_dm_plane_colorop_degamma(plane_state, dc_plane_state, colorop);
1885 if (ret)
1886 return ret;
1887
1888 /* Multiplier */
1889 colorop = colorop->next;
1890 if (!colorop) {
1891 drm_dbg(dev, "no multiplier colorop found\n");
1892 return -EINVAL;
1893 }
1894
1895 ret = __set_dm_plane_colorop_multiplier(plane_state, dc_plane_state, colorop);
1896 if (ret)
1897 return ret;
1898
1899 /* 3x4 matrix */
1900 colorop = colorop->next;
1901 if (!colorop) {
1902 drm_dbg(dev, "no 3x4 matrix colorop found\n");
1903 return -EINVAL;
1904 }
1905
1906 ret = __set_dm_plane_colorop_3x4_matrix(plane_state, dc_plane_state, colorop);
1907 if (ret)
1908 return ret;
1909
1910 if (adev->dm.dc->caps.color.dpp.hw_3d_lut) {
1911 /* 1D Curve & LUT - SHAPER TF & LUT */
1912 colorop = colorop->next;
1913 if (!colorop) {
1914 drm_dbg(dev, "no Shaper TF colorop found\n");
1915 return -EINVAL;
1916 }
1917
1918 ret = __set_dm_plane_colorop_shaper(plane_state, dc_plane_state, colorop);
1919 if (ret)
1920 return ret;
1921
1922 /* Shaper LUT colorop is already handled, just skip here */
1923 colorop = colorop->next;
1924 if (!colorop)
1925 return -EINVAL;
1926
1927 /* 3D LUT */
1928 colorop = colorop->next;
1929 if (!colorop) {
1930 drm_dbg(dev, "no 3D LUT colorop found\n");
1931 return -EINVAL;
1932 }
1933
1934 ret = __set_dm_plane_colorop_3dlut(plane_state, dc_plane_state, colorop);
1935 if (ret)
1936 return ret;
1937 }
1938
1939 /* 1D Curve & LUT - BLND TF & LUT */
1940 colorop = colorop->next;
1941 if (!colorop) {
1942 drm_dbg(dev, "no Blend TF colorop found\n");
1943 return -EINVAL;
1944 }
1945
1946 ret = __set_dm_plane_colorop_blend(plane_state, dc_plane_state, colorop);
1947 if (ret)
1948 return ret;
1949
1950 /* BLND LUT colorop is already handled, just skip here */
1951 colorop = colorop->next;
1952 if (!colorop)
1953 return -EINVAL;
1954
1955 return 0;
1956 }
1957
1958 /**
1959 * amdgpu_dm_update_plane_color_mgmt: Maps DRM color management to DC plane.
1960 * @crtc: amdgpu_dm crtc state
1961 * @plane_state: DRM plane state
1962 * @dc_plane_state: target DC surface
1963 *
1964 * Update the underlying dc_stream_state's input transfer function (ITF) in
1965 * preparation for hardware commit. The transfer function used depends on
1966 * the preparation done on the stream for color management.
1967 *
1968 * Returns:
1969 * 0 on success. -ENOMEM if mem allocation fails.
1970 */
amdgpu_dm_update_plane_color_mgmt(struct dm_crtc_state * crtc,struct drm_plane_state * plane_state,struct dc_plane_state * dc_plane_state)1971 int amdgpu_dm_update_plane_color_mgmt(struct dm_crtc_state *crtc,
1972 struct drm_plane_state *plane_state,
1973 struct dc_plane_state *dc_plane_state)
1974 {
1975 struct amdgpu_device *adev = drm_to_adev(crtc->base.state->dev);
1976 struct dm_plane_state *dm_plane_state = to_dm_plane_state(plane_state);
1977 struct drm_color_ctm_3x4 *ctm = NULL;
1978 struct dc_color_caps *color_caps = NULL;
1979 bool has_crtc_cm_degamma;
1980 int ret;
1981
1982 ret = amdgpu_dm_verify_lut3d_size(adev, plane_state);
1983 if (ret) {
1984 drm_dbg_driver(&adev->ddev, "amdgpu_dm_verify_lut3d_size() failed\n");
1985 return ret;
1986 }
1987
1988 if (dc_plane_state->ctx && dc_plane_state->ctx->dc)
1989 color_caps = &dc_plane_state->ctx->dc->caps.color;
1990
1991 /* Initially, we can just bypass the DGM block. */
1992 dc_plane_state->in_transfer_func.type = TF_TYPE_BYPASS;
1993 dc_plane_state->in_transfer_func.tf = TRANSFER_FUNCTION_LINEAR;
1994
1995 /* After, we start to update values according to color props */
1996 has_crtc_cm_degamma = (crtc->cm_has_degamma || crtc->cm_is_degamma_srgb);
1997
1998 ret = __set_dm_plane_degamma(plane_state, dc_plane_state, color_caps);
1999 if (ret == -ENOMEM)
2000 return ret;
2001
2002 /* We only have one degamma block available (pre-blending) for the
2003 * whole color correction pipeline, so that we can't actually perform
2004 * plane and CRTC degamma at the same time. Explicitly reject atomic
2005 * updates when userspace sets both plane and CRTC degamma properties.
2006 */
2007 if (has_crtc_cm_degamma && ret != -EINVAL) {
2008 drm_dbg_kms(crtc->base.crtc->dev,
2009 "doesn't support plane and CRTC degamma at the same time\n");
2010 return -EINVAL;
2011 }
2012
2013 /* If we are here, it means we don't have plane degamma settings, check
2014 * if we have CRTC degamma waiting for mapping to pre-blending degamma
2015 * block
2016 */
2017 if (has_crtc_cm_degamma) {
2018 /*
2019 * AMD HW doesn't have post-blending degamma caps. When DRM
2020 * CRTC atomic degamma is set, we maps it to DPP degamma block
2021 * (pre-blending) or, on legacy gamma, we use DPP degamma to
2022 * linearize (implicit degamma) from sRGB/BT709 according to
2023 * the input space.
2024 */
2025 ret = map_crtc_degamma_to_dc_plane(crtc, dc_plane_state, color_caps);
2026 if (ret)
2027 return ret;
2028 }
2029
2030 /* Setup CRTC CTM. */
2031 if (dm_plane_state->ctm) {
2032 ctm = (struct drm_color_ctm_3x4 *)dm_plane_state->ctm->data;
2033 /*
2034 * DCN2 and older don't support both pre-blending and
2035 * post-blending gamut remap. For this HW family, if we have
2036 * the plane and CRTC CTMs simultaneously, CRTC CTM takes
2037 * priority, and we discard plane CTM, as implemented in
2038 * dcn10_program_gamut_remap(). However, DCN3+ has DPP
2039 * (pre-blending) and MPC (post-blending) `gamut remap` blocks;
2040 * therefore, we can program plane and CRTC CTMs together by
2041 * mapping CRTC CTM to MPC and keeping plane CTM setup at DPP,
2042 * as it's done by dcn30_program_gamut_remap().
2043 */
2044 __drm_ctm_3x4_to_dc_matrix(ctm, dc_plane_state->gamut_remap_matrix.matrix);
2045
2046 dc_plane_state->gamut_remap_matrix.enable_remap = true;
2047 dc_plane_state->input_csc_color_matrix.enable_adjustment = false;
2048 } else {
2049 /* Bypass CTM. */
2050 dc_plane_state->gamut_remap_matrix.enable_remap = false;
2051 dc_plane_state->input_csc_color_matrix.enable_adjustment = false;
2052 }
2053
2054 if (!amdgpu_dm_plane_set_colorop_properties(plane_state, dc_plane_state))
2055 return 0;
2056
2057 return amdgpu_dm_plane_set_color_properties(plane_state, dc_plane_state);
2058 }
2059