1 /*
2  * Copyright 2018 Advanced Micro Devices, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors: AMD
23  *
24  */
25 #include "amdgpu.h"
26 #include "amdgpu_mode.h"
27 #include "amdgpu_dm.h"
28 #include "dc.h"
29 #include "modules/color/color_gamma.h"
30 #include "basics/conversion.h"
31 
32 /*
33  * The DC interface to HW gives us the following color management blocks
34  * per pipe (surface):
35  *
36  * - Input gamma LUT (de-normalized)
37  * - Input CSC (normalized)
38  * - Surface degamma LUT (normalized)
39  * - Surface CSC (normalized)
40  * - Surface regamma LUT (normalized)
41  * - Output CSC (normalized)
42  *
43  * But these aren't a direct mapping to DRM color properties. The current DRM
44  * interface exposes CRTC degamma, CRTC CTM and CRTC regamma while our hardware
45  * is essentially giving:
46  *
47  * Plane CTM -> Plane degamma -> Plane CTM -> Plane regamma -> Plane CTM
48  *
49  * The input gamma LUT block isn't really applicable here since it operates
50  * on the actual input data itself rather than the HW fp representation. The
51  * input and output CSC blocks are technically available to use as part of
52  * the DC interface but are typically used internally by DC for conversions
53  * between color spaces. These could be blended together with user
54  * adjustments in the future but for now these should remain untouched.
55  *
56  * The pipe blending also happens after these blocks so we don't actually
57  * support any CRTC props with correct blending with multiple planes - but we
58  * can still support CRTC color management properties in DM in most single
59  * plane cases correctly with clever management of the DC interface in DM.
60  *
61  * As per DRM documentation, blocks should be in hardware bypass when their
62  * respective property is set to NULL. A linear DGM/RGM LUT should also
63  * considered as putting the respective block into bypass mode.
64  *
65  * This means that the following
66  * configuration is assumed to be the default:
67  *
68  * Plane DGM Bypass -> Plane CTM Bypass -> Plane RGM Bypass -> ...
69  * CRTC DGM Bypass -> CRTC CTM Bypass -> CRTC RGM Bypass
70  */
71 
72 #define MAX_DRM_LUT_VALUE 0xFFFF
73 
74 /*
75  * Initialize the color module.
76  *
77  * We're not using the full color module, only certain components.
78  * Only call setup functions for components that we need.
79  */
80 void amdgpu_dm_init_color_mod(void)
81 {
82 	setup_x_points_distribution();
83 }
84 
85 /* Extracts the DRM lut and lut size from a blob. */
86 static const struct drm_color_lut *
87 __extract_blob_lut(const struct drm_property_blob *blob, uint32_t *size)
88 {
89 	*size = blob ? drm_color_lut_size(blob) : 0;
90 	return blob ? (struct drm_color_lut *)blob->data : NULL;
91 }
92 
93 /*
94  * Return true if the given lut is a linear mapping of values, i.e. it acts
95  * like a bypass LUT.
96  *
97  * It is considered linear if the lut represents:
98  * f(a) = (0xFF00/MAX_COLOR_LUT_ENTRIES-1)a; for integer a in
99  *                                           [0, MAX_COLOR_LUT_ENTRIES)
100  */
101 static bool __is_lut_linear(const struct drm_color_lut *lut, uint32_t size)
102 {
103 	int i;
104 	uint32_t expected;
105 	int delta;
106 
107 	for (i = 0; i < size; i++) {
108 		/* All color values should equal */
109 		if ((lut[i].red != lut[i].green) || (lut[i].green != lut[i].blue))
110 			return false;
111 
112 		expected = i * MAX_DRM_LUT_VALUE / (size-1);
113 
114 		/* Allow a +/-1 error. */
115 		delta = lut[i].red - expected;
116 		if (delta < -1 || 1 < delta)
117 			return false;
118 	}
119 	return true;
120 }
121 
122 /*
123  * Convert the drm_color_lut to dc_gamma. The conversion depends on the size
124  * of the lut - whether or not it's legacy.
125  */
126 static void __drm_lut_to_dc_gamma(const struct drm_color_lut *lut,
127 				  struct dc_gamma *gamma, bool is_legacy)
128 {
129 	uint32_t r, g, b;
130 	int i;
131 
132 	if (is_legacy) {
133 		for (i = 0; i < MAX_COLOR_LEGACY_LUT_ENTRIES; i++) {
134 			r = drm_color_lut_extract(lut[i].red, 16);
135 			g = drm_color_lut_extract(lut[i].green, 16);
136 			b = drm_color_lut_extract(lut[i].blue, 16);
137 
138 			gamma->entries.red[i] = dc_fixpt_from_int(r);
139 			gamma->entries.green[i] = dc_fixpt_from_int(g);
140 			gamma->entries.blue[i] = dc_fixpt_from_int(b);
141 		}
142 		return;
143 	}
144 
145 	/* else */
146 	for (i = 0; i < MAX_COLOR_LUT_ENTRIES; i++) {
147 		r = drm_color_lut_extract(lut[i].red, 16);
148 		g = drm_color_lut_extract(lut[i].green, 16);
149 		b = drm_color_lut_extract(lut[i].blue, 16);
150 
151 		gamma->entries.red[i] = dc_fixpt_from_fraction(r, MAX_DRM_LUT_VALUE);
152 		gamma->entries.green[i] = dc_fixpt_from_fraction(g, MAX_DRM_LUT_VALUE);
153 		gamma->entries.blue[i] = dc_fixpt_from_fraction(b, MAX_DRM_LUT_VALUE);
154 	}
155 }
156 
157 /*
158  * Converts a DRM CTM to a DC CSC float matrix.
159  * The matrix needs to be a 3x4 (12 entry) matrix.
160  */
161 static void __drm_ctm_to_dc_matrix(const struct drm_color_ctm *ctm,
162 				   struct fixed31_32 *matrix)
163 {
164 	int64_t val;
165 	int i;
166 
167 	/*
168 	 * DRM gives a 3x3 matrix, but DC wants 3x4. Assuming we're operating
169 	 * with homogeneous coordinates, augment the matrix with 0's.
170 	 *
171 	 * The format provided is S31.32, using signed-magnitude representation.
172 	 * Our fixed31_32 is also S31.32, but is using 2's complement. We have
173 	 * to convert from signed-magnitude to 2's complement.
174 	 */
175 	for (i = 0; i < 12; i++) {
176 		/* Skip 4th element */
177 		if (i % 4 == 3) {
178 			matrix[i] = dc_fixpt_zero;
179 			continue;
180 		}
181 
182 		/* gamut_remap_matrix[i] = ctm[i - floor(i/4)] */
183 		val = ctm->matrix[i - (i / 4)];
184 		/* If negative, convert to 2's complement. */
185 		if (val & (1ULL << 63))
186 			val = -(val & ~(1ULL << 63));
187 
188 		matrix[i].value = val;
189 	}
190 }
191 
192 /* Calculates the legacy transfer function - only for sRGB input space. */
193 static int __set_legacy_tf(struct dc_transfer_func *func,
194 			   const struct drm_color_lut *lut, uint32_t lut_size,
195 			   bool has_rom)
196 {
197 	struct dc_gamma *gamma = NULL;
198 	struct calculate_buffer cal_buffer = {0};
199 	bool res;
200 
201 	ASSERT(lut && lut_size == MAX_COLOR_LEGACY_LUT_ENTRIES);
202 
203 	cal_buffer.buffer_index = -1;
204 
205 	gamma = dc_create_gamma();
206 	if (!gamma)
207 		return -ENOMEM;
208 
209 	gamma->type = GAMMA_RGB_256;
210 	gamma->num_entries = lut_size;
211 	__drm_lut_to_dc_gamma(lut, gamma, true);
212 
213 	res = mod_color_calculate_regamma_params(func, gamma, true, has_rom,
214 						 NULL, &cal_buffer);
215 
216 	dc_gamma_release(&gamma);
217 
218 	return res ? 0 : -ENOMEM;
219 }
220 
221 /* Calculates the output transfer function based on expected input space. */
222 static int __set_output_tf(struct dc_transfer_func *func,
223 			   const struct drm_color_lut *lut, uint32_t lut_size,
224 			   bool has_rom)
225 {
226 	struct dc_gamma *gamma = NULL;
227 	struct calculate_buffer cal_buffer = {0};
228 	bool res;
229 
230 	ASSERT(lut && lut_size == MAX_COLOR_LUT_ENTRIES);
231 
232 	cal_buffer.buffer_index = -1;
233 
234 	gamma = dc_create_gamma();
235 	if (!gamma)
236 		return -ENOMEM;
237 
238 	gamma->num_entries = lut_size;
239 	__drm_lut_to_dc_gamma(lut, gamma, false);
240 
241 	if (func->tf == TRANSFER_FUNCTION_LINEAR) {
242 		/*
243 		 * Color module doesn't like calculating regamma params
244 		 * on top of a linear input. But degamma params can be used
245 		 * instead to simulate this.
246 		 */
247 		gamma->type = GAMMA_CUSTOM;
248 		res = mod_color_calculate_degamma_params(NULL, func,
249 							gamma, true);
250 	} else {
251 		/*
252 		 * Assume sRGB. The actual mapping will depend on whether the
253 		 * input was legacy or not.
254 		 */
255 		gamma->type = GAMMA_CS_TFM_1D;
256 		res = mod_color_calculate_regamma_params(func, gamma, false,
257 							 has_rom, NULL, &cal_buffer);
258 	}
259 
260 	dc_gamma_release(&gamma);
261 
262 	return res ? 0 : -ENOMEM;
263 }
264 
265 /* Caculates the input transfer function based on expected input space. */
266 static int __set_input_tf(struct dc_transfer_func *func,
267 			  const struct drm_color_lut *lut, uint32_t lut_size)
268 {
269 	struct dc_gamma *gamma = NULL;
270 	bool res;
271 
272 	gamma = dc_create_gamma();
273 	if (!gamma)
274 		return -ENOMEM;
275 
276 	gamma->type = GAMMA_CUSTOM;
277 	gamma->num_entries = lut_size;
278 
279 	__drm_lut_to_dc_gamma(lut, gamma, false);
280 
281 	res = mod_color_calculate_degamma_params(NULL, func, gamma, true);
282 	dc_gamma_release(&gamma);
283 
284 	return res ? 0 : -ENOMEM;
285 }
286 
287 /**
288  * amdgpu_dm_verify_lut_sizes
289  * @crtc_state: the DRM CRTC state
290  *
291  * Verifies that the Degamma and Gamma LUTs attached to the |crtc_state| are of
292  * the expected size.
293  *
294  * Returns 0 on success.
295  */
296 int amdgpu_dm_verify_lut_sizes(const struct drm_crtc_state *crtc_state)
297 {
298 	const struct drm_color_lut *lut = NULL;
299 	uint32_t size = 0;
300 
301 	lut = __extract_blob_lut(crtc_state->degamma_lut, &size);
302 	if (lut && size != MAX_COLOR_LUT_ENTRIES) {
303 		DRM_DEBUG_DRIVER(
304 			"Invalid Degamma LUT size. Should be %u but got %u.\n",
305 			MAX_COLOR_LUT_ENTRIES, size);
306 		return -EINVAL;
307 	}
308 
309 	lut = __extract_blob_lut(crtc_state->gamma_lut, &size);
310 	if (lut && size != MAX_COLOR_LUT_ENTRIES &&
311 	    size != MAX_COLOR_LEGACY_LUT_ENTRIES) {
312 		DRM_DEBUG_DRIVER(
313 			"Invalid Gamma LUT size. Should be %u (or %u for legacy) but got %u.\n",
314 			MAX_COLOR_LUT_ENTRIES, MAX_COLOR_LEGACY_LUT_ENTRIES,
315 			size);
316 		return -EINVAL;
317 	}
318 
319 	return 0;
320 }
321 
322 /**
323  * amdgpu_dm_update_crtc_color_mgmt: Maps DRM color management to DC stream.
324  * @crtc: amdgpu_dm crtc state
325  *
326  * With no plane level color management properties we're free to use any
327  * of the HW blocks as long as the CRTC CTM always comes before the
328  * CRTC RGM and after the CRTC DGM.
329  *
330  * The CRTC RGM block will be placed in the RGM LUT block if it is non-linear.
331  * The CRTC DGM block will be placed in the DGM LUT block if it is non-linear.
332  * The CRTC CTM will be placed in the gamut remap block if it is non-linear.
333  *
334  * The RGM block is typically more fully featured and accurate across
335  * all ASICs - DCE can't support a custom non-linear CRTC DGM.
336  *
337  * For supporting both plane level color management and CRTC level color
338  * management at once we have to either restrict the usage of CRTC properties
339  * or blend adjustments together.
340  *
341  * Returns 0 on success.
342  */
343 int amdgpu_dm_update_crtc_color_mgmt(struct dm_crtc_state *crtc)
344 {
345 	struct dc_stream_state *stream = crtc->stream;
346 	struct amdgpu_device *adev = drm_to_adev(crtc->base.state->dev);
347 	bool has_rom = adev->asic_type <= CHIP_RAVEN;
348 	struct drm_color_ctm *ctm = NULL;
349 	const struct drm_color_lut *degamma_lut, *regamma_lut;
350 	uint32_t degamma_size, regamma_size;
351 	bool has_regamma, has_degamma;
352 	bool is_legacy;
353 	int r;
354 
355 	r = amdgpu_dm_verify_lut_sizes(&crtc->base);
356 	if (r)
357 		return r;
358 
359 	degamma_lut = __extract_blob_lut(crtc->base.degamma_lut, &degamma_size);
360 	regamma_lut = __extract_blob_lut(crtc->base.gamma_lut, &regamma_size);
361 
362 	has_degamma =
363 		degamma_lut && !__is_lut_linear(degamma_lut, degamma_size);
364 
365 	has_regamma =
366 		regamma_lut && !__is_lut_linear(regamma_lut, regamma_size);
367 
368 	is_legacy = regamma_size == MAX_COLOR_LEGACY_LUT_ENTRIES;
369 
370 	/* Reset all adjustments. */
371 	crtc->cm_has_degamma = false;
372 	crtc->cm_is_degamma_srgb = false;
373 
374 	/* Setup regamma and degamma. */
375 	if (is_legacy) {
376 		/*
377 		 * Legacy regamma forces us to use the sRGB RGM as a base.
378 		 * This also means we can't use linear DGM since DGM needs
379 		 * to use sRGB as a base as well, resulting in incorrect CRTC
380 		 * DGM and CRTC CTM.
381 		 *
382 		 * TODO: Just map this to the standard regamma interface
383 		 * instead since this isn't really right. One of the cases
384 		 * where this setup currently fails is trying to do an
385 		 * inverse color ramp in legacy userspace.
386 		 */
387 		crtc->cm_is_degamma_srgb = true;
388 		stream->out_transfer_func->type = TF_TYPE_DISTRIBUTED_POINTS;
389 		stream->out_transfer_func->tf = TRANSFER_FUNCTION_SRGB;
390 
391 		r = __set_legacy_tf(stream->out_transfer_func, regamma_lut,
392 				    regamma_size, has_rom);
393 		if (r)
394 			return r;
395 	} else if (has_regamma) {
396 		/* CRTC RGM goes into RGM LUT. */
397 		stream->out_transfer_func->type = TF_TYPE_DISTRIBUTED_POINTS;
398 		stream->out_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
399 
400 		r = __set_output_tf(stream->out_transfer_func, regamma_lut,
401 				    regamma_size, has_rom);
402 		if (r)
403 			return r;
404 	} else {
405 		/*
406 		 * No CRTC RGM means we can just put the block into bypass
407 		 * since we don't have any plane level adjustments using it.
408 		 */
409 		stream->out_transfer_func->type = TF_TYPE_BYPASS;
410 		stream->out_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
411 	}
412 
413 	/*
414 	 * CRTC DGM goes into DGM LUT. It would be nice to place it
415 	 * into the RGM since it's a more featured block but we'd
416 	 * have to place the CTM in the OCSC in that case.
417 	 */
418 	crtc->cm_has_degamma = has_degamma;
419 
420 	/* Setup CRTC CTM. */
421 	if (crtc->base.ctm) {
422 		ctm = (struct drm_color_ctm *)crtc->base.ctm->data;
423 
424 		/*
425 		 * Gamut remapping must be used for gamma correction
426 		 * since it comes before the regamma correction.
427 		 *
428 		 * OCSC could be used for gamma correction, but we'd need to
429 		 * blend the adjustments together with the required output
430 		 * conversion matrix - so just use the gamut remap block
431 		 * for now.
432 		 */
433 		__drm_ctm_to_dc_matrix(ctm, stream->gamut_remap_matrix.matrix);
434 
435 		stream->gamut_remap_matrix.enable_remap = true;
436 		stream->csc_color_matrix.enable_adjustment = false;
437 	} else {
438 		/* Bypass CTM. */
439 		stream->gamut_remap_matrix.enable_remap = false;
440 		stream->csc_color_matrix.enable_adjustment = false;
441 	}
442 
443 	return 0;
444 }
445 
446 /**
447  * amdgpu_dm_update_plane_color_mgmt: Maps DRM color management to DC plane.
448  * @crtc: amdgpu_dm crtc state
449  * @dc_plane_state: target DC surface
450  *
451  * Update the underlying dc_stream_state's input transfer function (ITF) in
452  * preparation for hardware commit. The transfer function used depends on
453  * the prepartion done on the stream for color management.
454  *
455  * Returns 0 on success.
456  */
457 int amdgpu_dm_update_plane_color_mgmt(struct dm_crtc_state *crtc,
458 				      struct dc_plane_state *dc_plane_state)
459 {
460 	const struct drm_color_lut *degamma_lut;
461 	enum dc_transfer_func_predefined tf = TRANSFER_FUNCTION_SRGB;
462 	uint32_t degamma_size;
463 	int r;
464 
465 	/* Get the correct base transfer function for implicit degamma. */
466 	switch (dc_plane_state->format) {
467 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCbCr:
468 	case SURFACE_PIXEL_FORMAT_VIDEO_420_YCrCb:
469 		/* DC doesn't have a transfer function for BT601 specifically. */
470 		tf = TRANSFER_FUNCTION_BT709;
471 		break;
472 	default:
473 		break;
474 	}
475 
476 	if (crtc->cm_has_degamma) {
477 		degamma_lut = __extract_blob_lut(crtc->base.degamma_lut,
478 						 &degamma_size);
479 		ASSERT(degamma_size == MAX_COLOR_LUT_ENTRIES);
480 
481 		dc_plane_state->in_transfer_func->type =
482 			TF_TYPE_DISTRIBUTED_POINTS;
483 
484 		/*
485 		 * This case isn't fully correct, but also fairly
486 		 * uncommon. This is userspace trying to use a
487 		 * legacy gamma LUT + atomic degamma LUT
488 		 * at the same time.
489 		 *
490 		 * Legacy gamma requires the input to be in linear
491 		 * space, so that means we need to apply an sRGB
492 		 * degamma. But color module also doesn't support
493 		 * a user ramp in this case so the degamma will
494 		 * be lost.
495 		 *
496 		 * Even if we did support it, it's still not right:
497 		 *
498 		 * Input -> CRTC DGM -> sRGB DGM -> CRTC CTM ->
499 		 * sRGB RGM -> CRTC RGM -> Output
500 		 *
501 		 * The CSC will be done in the wrong space since
502 		 * we're applying an sRGB DGM on top of the CRTC
503 		 * DGM.
504 		 *
505 		 * TODO: Don't use the legacy gamma interface and just
506 		 * map these to the atomic one instead.
507 		 */
508 		if (crtc->cm_is_degamma_srgb)
509 			dc_plane_state->in_transfer_func->tf = tf;
510 		else
511 			dc_plane_state->in_transfer_func->tf =
512 				TRANSFER_FUNCTION_LINEAR;
513 
514 		r = __set_input_tf(dc_plane_state->in_transfer_func,
515 				   degamma_lut, degamma_size);
516 		if (r)
517 			return r;
518 	} else if (crtc->cm_is_degamma_srgb) {
519 		/*
520 		 * For legacy gamma support we need the regamma input
521 		 * in linear space. Assume that the input is sRGB.
522 		 */
523 		dc_plane_state->in_transfer_func->type = TF_TYPE_PREDEFINED;
524 		dc_plane_state->in_transfer_func->tf = tf;
525 
526 		if (tf != TRANSFER_FUNCTION_SRGB &&
527 		    !mod_color_calculate_degamma_params(NULL,
528 			    dc_plane_state->in_transfer_func, NULL, false))
529 			return -ENOMEM;
530 	} else {
531 		/* ...Otherwise we can just bypass the DGM block. */
532 		dc_plane_state->in_transfer_func->type = TF_TYPE_BYPASS;
533 		dc_plane_state->in_transfer_func->tf = TRANSFER_FUNCTION_LINEAR;
534 	}
535 
536 	return 0;
537 }
538