xref: /openbmc/linux/drivers/gpu/drm/drm_color_mgmt.c (revision 842ed298)
1 /*
2  * Copyright (c) 2016 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22 
23 #include <linux/uaccess.h>
24 
25 #include <drm/drm_color_mgmt.h>
26 #include <drm/drm_crtc.h>
27 #include <drm/drm_device.h>
28 #include <drm/drm_drv.h>
29 #include <drm/drm_print.h>
30 
31 #include "drm_crtc_internal.h"
32 
33 /**
34  * DOC: overview
35  *
36  * Color management or color space adjustments is supported through a set of 5
37  * properties on the &drm_crtc object. They are set up by calling
38  * drm_crtc_enable_color_mgmt().
39  *
40  * "DEGAMMA_LUT”:
41  *	Blob property to set the degamma lookup table (LUT) mapping pixel data
42  *	from the framebuffer before it is given to the transformation matrix.
43  *	The data is interpreted as an array of &struct drm_color_lut elements.
44  *	Hardware might choose not to use the full precision of the LUT elements
45  *	nor use all the elements of the LUT (for example the hardware might
46  *	choose to interpolate between LUT[0] and LUT[4]).
47  *
48  *	Setting this to NULL (blob property value set to 0) means a
49  *	linear/pass-thru gamma table should be used. This is generally the
50  *	driver boot-up state too. Drivers can access this blob through
51  *	&drm_crtc_state.degamma_lut.
52  *
53  * “DEGAMMA_LUT_SIZE”:
54  *	Unsinged range property to give the size of the lookup table to be set
55  *	on the DEGAMMA_LUT property (the size depends on the underlying
56  *	hardware). If drivers support multiple LUT sizes then they should
57  *	publish the largest size, and sub-sample smaller sized LUTs (e.g. for
58  *	split-gamma modes) appropriately.
59  *
60  * “CTM”:
61  *	Blob property to set the current transformation matrix (CTM) apply to
62  *	pixel data after the lookup through the degamma LUT and before the
63  *	lookup through the gamma LUT. The data is interpreted as a struct
64  *	&drm_color_ctm.
65  *
66  *	Setting this to NULL (blob property value set to 0) means a
67  *	unit/pass-thru matrix should be used. This is generally the driver
68  *	boot-up state too. Drivers can access the blob for the color conversion
69  *	matrix through &drm_crtc_state.ctm.
70  *
71  * “GAMMA_LUT”:
72  *	Blob property to set the gamma lookup table (LUT) mapping pixel data
73  *	after the transformation matrix to data sent to the connector. The
74  *	data is interpreted as an array of &struct drm_color_lut elements.
75  *	Hardware might choose not to use the full precision of the LUT elements
76  *	nor use all the elements of the LUT (for example the hardware might
77  *	choose to interpolate between LUT[0] and LUT[4]).
78  *
79  *	Setting this to NULL (blob property value set to 0) means a
80  *	linear/pass-thru gamma table should be used. This is generally the
81  *	driver boot-up state too. Drivers can access this blob through
82  *	&drm_crtc_state.gamma_lut.
83  *
84  * “GAMMA_LUT_SIZE”:
85  *	Unsigned range property to give the size of the lookup table to be set
86  *	on the GAMMA_LUT property (the size depends on the underlying hardware).
87  *	If drivers support multiple LUT sizes then they should publish the
88  *	largest size, and sub-sample smaller sized LUTs (e.g. for split-gamma
89  *	modes) appropriately.
90  *
91  * There is also support for a legacy gamma table, which is set up by calling
92  * drm_mode_crtc_set_gamma_size(). Drivers which support both should use
93  * drm_atomic_helper_legacy_gamma_set() to alias the legacy gamma ramp with the
94  * "GAMMA_LUT" property above.
95  *
96  * Support for different non RGB color encodings is controlled through
97  * &drm_plane specific COLOR_ENCODING and COLOR_RANGE properties. They
98  * are set up by calling drm_plane_create_color_properties().
99  *
100  * "COLOR_ENCODING":
101  * 	Optional plane enum property to support different non RGB
102  * 	color encodings. The driver can provide a subset of standard
103  * 	enum values supported by the DRM plane.
104  *
105  * "COLOR_RANGE":
106  * 	Optional plane enum property to support different non RGB
107  * 	color parameter ranges. The driver can provide a subset of
108  * 	standard enum values supported by the DRM plane.
109  */
110 
111 /**
112  * drm_color_ctm_s31_32_to_qm_n
113  *
114  * @user_input: input value
115  * @m: number of integer bits, only support m <= 32, include the sign-bit
116  * @n: number of fractional bits, only support n <= 32
117  *
118  * Convert and clamp S31.32 sign-magnitude to Qm.n (signed 2's complement).
119  * The sign-bit BIT(m+n-1) and above are 0 for positive value and 1 for negative
120  * the range of value is [-2^(m-1), 2^(m-1) - 2^-n]
121  *
122  * For example
123  * A Q3.12 format number:
124  * - required bit: 3 + 12 = 15bits
125  * - range: [-2^2, 2^2 - 2^−15]
126  *
127  * NOTE: the m can be zero if all bit_precision are used to present fractional
128  *       bits like Q0.32
129  */
130 u64 drm_color_ctm_s31_32_to_qm_n(u64 user_input, u32 m, u32 n)
131 {
132 	u64 mag = (user_input & ~BIT_ULL(63)) >> (32 - n);
133 	bool negative = !!(user_input & BIT_ULL(63));
134 	s64 val;
135 
136 	WARN_ON(m > 32 || n > 32);
137 
138 	val = clamp_val(mag, 0, negative ?
139 				BIT_ULL(n + m - 1) : BIT_ULL(n + m - 1) - 1);
140 
141 	return negative ? -val : val;
142 }
143 EXPORT_SYMBOL(drm_color_ctm_s31_32_to_qm_n);
144 
145 /**
146  * drm_crtc_enable_color_mgmt - enable color management properties
147  * @crtc: DRM CRTC
148  * @degamma_lut_size: the size of the degamma lut (before CSC)
149  * @has_ctm: whether to attach ctm_property for CSC matrix
150  * @gamma_lut_size: the size of the gamma lut (after CSC)
151  *
152  * This function lets the driver enable the color correction
153  * properties on a CRTC. This includes 3 degamma, csc and gamma
154  * properties that userspace can set and 2 size properties to inform
155  * the userspace of the lut sizes. Each of the properties are
156  * optional. The gamma and degamma properties are only attached if
157  * their size is not 0 and ctm_property is only attached if has_ctm is
158  * true.
159  *
160  * Drivers should use drm_atomic_helper_legacy_gamma_set() to implement the
161  * legacy &drm_crtc_funcs.gamma_set callback.
162  */
163 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
164 				uint degamma_lut_size,
165 				bool has_ctm,
166 				uint gamma_lut_size)
167 {
168 	struct drm_device *dev = crtc->dev;
169 	struct drm_mode_config *config = &dev->mode_config;
170 
171 	if (degamma_lut_size) {
172 		drm_object_attach_property(&crtc->base,
173 					   config->degamma_lut_property, 0);
174 		drm_object_attach_property(&crtc->base,
175 					   config->degamma_lut_size_property,
176 					   degamma_lut_size);
177 	}
178 
179 	if (has_ctm)
180 		drm_object_attach_property(&crtc->base,
181 					   config->ctm_property, 0);
182 
183 	if (gamma_lut_size) {
184 		drm_object_attach_property(&crtc->base,
185 					   config->gamma_lut_property, 0);
186 		drm_object_attach_property(&crtc->base,
187 					   config->gamma_lut_size_property,
188 					   gamma_lut_size);
189 	}
190 }
191 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
192 
193 /**
194  * drm_mode_crtc_set_gamma_size - set the gamma table size
195  * @crtc: CRTC to set the gamma table size for
196  * @gamma_size: size of the gamma table
197  *
198  * Drivers which support gamma tables should set this to the supported gamma
199  * table size when initializing the CRTC. Currently the drm core only supports a
200  * fixed gamma table size.
201  *
202  * Returns:
203  * Zero on success, negative errno on failure.
204  */
205 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
206 				 int gamma_size)
207 {
208 	uint16_t *r_base, *g_base, *b_base;
209 	int i;
210 
211 	crtc->gamma_size = gamma_size;
212 
213 	crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
214 				    GFP_KERNEL);
215 	if (!crtc->gamma_store) {
216 		crtc->gamma_size = 0;
217 		return -ENOMEM;
218 	}
219 
220 	r_base = crtc->gamma_store;
221 	g_base = r_base + gamma_size;
222 	b_base = g_base + gamma_size;
223 	for (i = 0; i < gamma_size; i++) {
224 		r_base[i] = i << 8;
225 		g_base[i] = i << 8;
226 		b_base[i] = i << 8;
227 	}
228 
229 
230 	return 0;
231 }
232 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
233 
234 /**
235  * drm_mode_gamma_set_ioctl - set the gamma table
236  * @dev: DRM device
237  * @data: ioctl data
238  * @file_priv: DRM file info
239  *
240  * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
241  * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
242  *
243  * Called by the user via ioctl.
244  *
245  * Returns:
246  * Zero on success, negative errno on failure.
247  */
248 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
249 			     void *data, struct drm_file *file_priv)
250 {
251 	struct drm_mode_crtc_lut *crtc_lut = data;
252 	struct drm_crtc *crtc;
253 	void *r_base, *g_base, *b_base;
254 	int size;
255 	struct drm_modeset_acquire_ctx ctx;
256 	int ret = 0;
257 
258 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
259 		return -EOPNOTSUPP;
260 
261 	crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
262 	if (!crtc)
263 		return -ENOENT;
264 
265 	if (crtc->funcs->gamma_set == NULL)
266 		return -ENOSYS;
267 
268 	/* memcpy into gamma store */
269 	if (crtc_lut->gamma_size != crtc->gamma_size)
270 		return -EINVAL;
271 
272 	DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
273 
274 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
275 	r_base = crtc->gamma_store;
276 	if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
277 		ret = -EFAULT;
278 		goto out;
279 	}
280 
281 	g_base = r_base + size;
282 	if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
283 		ret = -EFAULT;
284 		goto out;
285 	}
286 
287 	b_base = g_base + size;
288 	if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
289 		ret = -EFAULT;
290 		goto out;
291 	}
292 
293 	ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
294 				     crtc->gamma_size, &ctx);
295 
296 out:
297 	DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
298 	return ret;
299 
300 }
301 
302 /**
303  * drm_mode_gamma_get_ioctl - get the gamma table
304  * @dev: DRM device
305  * @data: ioctl data
306  * @file_priv: DRM file info
307  *
308  * Copy the current gamma table into the storage provided. This also provides
309  * the gamma table size the driver expects, which can be used to size the
310  * allocated storage.
311  *
312  * Called by the user via ioctl.
313  *
314  * Returns:
315  * Zero on success, negative errno on failure.
316  */
317 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
318 			     void *data, struct drm_file *file_priv)
319 {
320 	struct drm_mode_crtc_lut *crtc_lut = data;
321 	struct drm_crtc *crtc;
322 	void *r_base, *g_base, *b_base;
323 	int size;
324 	int ret = 0;
325 
326 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
327 		return -EOPNOTSUPP;
328 
329 	crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
330 	if (!crtc)
331 		return -ENOENT;
332 
333 	/* memcpy into gamma store */
334 	if (crtc_lut->gamma_size != crtc->gamma_size)
335 		return -EINVAL;
336 
337 	drm_modeset_lock(&crtc->mutex, NULL);
338 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
339 	r_base = crtc->gamma_store;
340 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
341 		ret = -EFAULT;
342 		goto out;
343 	}
344 
345 	g_base = r_base + size;
346 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
347 		ret = -EFAULT;
348 		goto out;
349 	}
350 
351 	b_base = g_base + size;
352 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
353 		ret = -EFAULT;
354 		goto out;
355 	}
356 out:
357 	drm_modeset_unlock(&crtc->mutex);
358 	return ret;
359 }
360 
361 static const char * const color_encoding_name[] = {
362 	[DRM_COLOR_YCBCR_BT601] = "ITU-R BT.601 YCbCr",
363 	[DRM_COLOR_YCBCR_BT709] = "ITU-R BT.709 YCbCr",
364 	[DRM_COLOR_YCBCR_BT2020] = "ITU-R BT.2020 YCbCr",
365 };
366 
367 static const char * const color_range_name[] = {
368 	[DRM_COLOR_YCBCR_FULL_RANGE] = "YCbCr full range",
369 	[DRM_COLOR_YCBCR_LIMITED_RANGE] = "YCbCr limited range",
370 };
371 
372 /**
373  * drm_get_color_encoding_name - return a string for color encoding
374  * @encoding: color encoding to compute name of
375  *
376  * In contrast to the other drm_get_*_name functions this one here returns a
377  * const pointer and hence is threadsafe.
378  */
379 const char *drm_get_color_encoding_name(enum drm_color_encoding encoding)
380 {
381 	if (WARN_ON(encoding >= ARRAY_SIZE(color_encoding_name)))
382 		return "unknown";
383 
384 	return color_encoding_name[encoding];
385 }
386 
387 /**
388  * drm_get_color_range_name - return a string for color range
389  * @range: color range to compute name of
390  *
391  * In contrast to the other drm_get_*_name functions this one here returns a
392  * const pointer and hence is threadsafe.
393  */
394 const char *drm_get_color_range_name(enum drm_color_range range)
395 {
396 	if (WARN_ON(range >= ARRAY_SIZE(color_range_name)))
397 		return "unknown";
398 
399 	return color_range_name[range];
400 }
401 
402 /**
403  * drm_plane_create_color_properties - color encoding related plane properties
404  * @plane: plane object
405  * @supported_encodings: bitfield indicating supported color encodings
406  * @supported_ranges: bitfileld indicating supported color ranges
407  * @default_encoding: default color encoding
408  * @default_range: default color range
409  *
410  * Create and attach plane specific COLOR_ENCODING and COLOR_RANGE
411  * properties to @plane. The supported encodings and ranges should
412  * be provided in supported_encodings and supported_ranges bitmasks.
413  * Each bit set in the bitmask indicates that its number as enum
414  * value is supported.
415  */
416 int drm_plane_create_color_properties(struct drm_plane *plane,
417 				      u32 supported_encodings,
418 				      u32 supported_ranges,
419 				      enum drm_color_encoding default_encoding,
420 				      enum drm_color_range default_range)
421 {
422 	struct drm_device *dev = plane->dev;
423 	struct drm_property *prop;
424 	struct drm_prop_enum_list enum_list[max_t(int, DRM_COLOR_ENCODING_MAX,
425 						       DRM_COLOR_RANGE_MAX)];
426 	int i, len;
427 
428 	if (WARN_ON(supported_encodings == 0 ||
429 		    (supported_encodings & -BIT(DRM_COLOR_ENCODING_MAX)) != 0 ||
430 		    (supported_encodings & BIT(default_encoding)) == 0))
431 		return -EINVAL;
432 
433 	if (WARN_ON(supported_ranges == 0 ||
434 		    (supported_ranges & -BIT(DRM_COLOR_RANGE_MAX)) != 0 ||
435 		    (supported_ranges & BIT(default_range)) == 0))
436 		return -EINVAL;
437 
438 	len = 0;
439 	for (i = 0; i < DRM_COLOR_ENCODING_MAX; i++) {
440 		if ((supported_encodings & BIT(i)) == 0)
441 			continue;
442 
443 		enum_list[len].type = i;
444 		enum_list[len].name = color_encoding_name[i];
445 		len++;
446 	}
447 
448 	prop = drm_property_create_enum(dev, 0, "COLOR_ENCODING",
449 					enum_list, len);
450 	if (!prop)
451 		return -ENOMEM;
452 	plane->color_encoding_property = prop;
453 	drm_object_attach_property(&plane->base, prop, default_encoding);
454 	if (plane->state)
455 		plane->state->color_encoding = default_encoding;
456 
457 	len = 0;
458 	for (i = 0; i < DRM_COLOR_RANGE_MAX; i++) {
459 		if ((supported_ranges & BIT(i)) == 0)
460 			continue;
461 
462 		enum_list[len].type = i;
463 		enum_list[len].name = color_range_name[i];
464 		len++;
465 	}
466 
467 	prop = drm_property_create_enum(dev, 0,	"COLOR_RANGE",
468 					enum_list, len);
469 	if (!prop)
470 		return -ENOMEM;
471 	plane->color_range_property = prop;
472 	drm_object_attach_property(&plane->base, prop, default_range);
473 	if (plane->state)
474 		plane->state->color_range = default_range;
475 
476 	return 0;
477 }
478 EXPORT_SYMBOL(drm_plane_create_color_properties);
479 
480 /**
481  * drm_color_lut_check - check validity of lookup table
482  * @lut: property blob containing LUT to check
483  * @tests: bitmask of tests to run
484  *
485  * Helper to check whether a userspace-provided lookup table is valid and
486  * satisfies hardware requirements.  Drivers pass a bitmask indicating which of
487  * the tests in &drm_color_lut_tests should be performed.
488  *
489  * Returns 0 on success, -EINVAL on failure.
490  */
491 int drm_color_lut_check(const struct drm_property_blob *lut, u32 tests)
492 {
493 	const struct drm_color_lut *entry;
494 	int i;
495 
496 	if (!lut || !tests)
497 		return 0;
498 
499 	entry = lut->data;
500 	for (i = 0; i < drm_color_lut_size(lut); i++) {
501 		if (tests & DRM_COLOR_LUT_EQUAL_CHANNELS) {
502 			if (entry[i].red != entry[i].blue ||
503 			    entry[i].red != entry[i].green) {
504 				DRM_DEBUG_KMS("All LUT entries must have equal r/g/b\n");
505 				return -EINVAL;
506 			}
507 		}
508 
509 		if (i > 0 && tests & DRM_COLOR_LUT_NON_DECREASING) {
510 			if (entry[i].red < entry[i - 1].red ||
511 			    entry[i].green < entry[i - 1].green ||
512 			    entry[i].blue < entry[i - 1].blue) {
513 				DRM_DEBUG_KMS("LUT entries must never decrease.\n");
514 				return -EINVAL;
515 			}
516 		}
517 	}
518 
519 	return 0;
520 }
521 EXPORT_SYMBOL(drm_color_lut_check);
522