2 * Copyright (c) 2016 Intel Corporation
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.
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
23 #include <linux/uaccess.h>
25 #include <drm/drm_atomic.h>
26 #include <drm/drm_color_mgmt.h>
27 #include <drm/drm_crtc.h>
28 #include <drm/drm_device.h>
29 #include <drm/drm_drv.h>
30 #include <drm/drm_print.h>
32 #include "drm_crtc_internal.h"
37 * Color management or color space adjustments is supported through a set of 5
38 * properties on the &drm_crtc object. They are set up by calling
39 * drm_crtc_enable_color_mgmt().
42 * Blob property to set the degamma lookup table (LUT) mapping pixel data
43 * from the framebuffer before it is given to the transformation matrix.
44 * The data is interpreted as an array of &struct drm_color_lut elements.
45 * Hardware might choose not to use the full precision of the LUT elements
46 * nor use all the elements of the LUT (for example the hardware might
47 * choose to interpolate between LUT[0] and LUT[4]).
49 * Setting this to NULL (blob property value set to 0) means a
50 * linear/pass-thru gamma table should be used. This is generally the
51 * driver boot-up state too. Drivers can access this blob through
52 * &drm_crtc_state.degamma_lut.
55 * Unsinged range property to give the size of the lookup table to be set
56 * on the DEGAMMA_LUT property (the size depends on the underlying
57 * hardware). If drivers support multiple LUT sizes then they should
58 * publish the largest size, and sub-sample smaller sized LUTs (e.g. for
59 * split-gamma modes) appropriately.
62 * Blob property to set the current transformation matrix (CTM) apply to
63 * pixel data after the lookup through the degamma LUT and before the
64 * lookup through the gamma LUT. The data is interpreted as a struct
67 * Setting this to NULL (blob property value set to 0) means a
68 * unit/pass-thru matrix should be used. This is generally the driver
69 * boot-up state too. Drivers can access the blob for the color conversion
70 * matrix through &drm_crtc_state.ctm.
73 * Blob property to set the gamma lookup table (LUT) mapping pixel data
74 * after the transformation matrix to data sent to the connector. The
75 * data is interpreted as an array of &struct drm_color_lut elements.
76 * Hardware might choose not to use the full precision of the LUT elements
77 * nor use all the elements of the LUT (for example the hardware might
78 * choose to interpolate between LUT[0] and LUT[4]).
80 * Setting this to NULL (blob property value set to 0) means a
81 * linear/pass-thru gamma table should be used. This is generally the
82 * driver boot-up state too. Drivers can access this blob through
83 * &drm_crtc_state.gamma_lut.
85 * Note that for mostly historical reasons stemming from Xorg heritage,
86 * this is also used to store the color map (also sometimes color lut, CLUT
87 * or color palette) for indexed formats like DRM_FORMAT_C8.
90 * Unsigned range property to give the size of the lookup table to be set
91 * on the GAMMA_LUT property (the size depends on the underlying hardware).
92 * If drivers support multiple LUT sizes then they should publish the
93 * largest size, and sub-sample smaller sized LUTs (e.g. for split-gamma
94 * modes) appropriately.
96 * There is also support for a legacy gamma table, which is set up by calling
97 * drm_mode_crtc_set_gamma_size(). The DRM core will then alias the legacy gamma
98 * ramp with "GAMMA_LUT" or, if that is unavailable, "DEGAMMA_LUT".
100 * Support for different non RGB color encodings is controlled through
101 * &drm_plane specific COLOR_ENCODING and COLOR_RANGE properties. They
102 * are set up by calling drm_plane_create_color_properties().
105 * Optional plane enum property to support different non RGB
106 * color encodings. The driver can provide a subset of standard
107 * enum values supported by the DRM plane.
110 * Optional plane enum property to support different non RGB
111 * color parameter ranges. The driver can provide a subset of
112 * standard enum values supported by the DRM plane.
116 * drm_color_ctm_s31_32_to_qm_n
118 * @user_input: input value
119 * @m: number of integer bits, only support m <= 32, include the sign-bit
120 * @n: number of fractional bits, only support n <= 32
122 * Convert and clamp S31.32 sign-magnitude to Qm.n (signed 2's complement).
123 * The sign-bit BIT(m+n-1) and above are 0 for positive value and 1 for negative
124 * the range of value is [-2^(m-1), 2^(m-1) - 2^-n]
127 * A Q3.12 format number:
128 * - required bit: 3 + 12 = 15bits
129 * - range: [-2^2, 2^2 - 2^−15]
131 * NOTE: the m can be zero if all bit_precision are used to present fractional
134 u64 drm_color_ctm_s31_32_to_qm_n(u64 user_input, u32 m, u32 n)
136 u64 mag = (user_input & ~BIT_ULL(63)) >> (32 - n);
137 bool negative = !!(user_input & BIT_ULL(63));
140 WARN_ON(m > 32 || n > 32);
142 val = clamp_val(mag, 0, negative ?
143 BIT_ULL(n + m - 1) : BIT_ULL(n + m - 1) - 1);
145 return negative ? -val : val;
147 EXPORT_SYMBOL(drm_color_ctm_s31_32_to_qm_n);
150 * drm_crtc_enable_color_mgmt - enable color management properties
152 * @degamma_lut_size: the size of the degamma lut (before CSC)
153 * @has_ctm: whether to attach ctm_property for CSC matrix
154 * @gamma_lut_size: the size of the gamma lut (after CSC)
156 * This function lets the driver enable the color correction
157 * properties on a CRTC. This includes 3 degamma, csc and gamma
158 * properties that userspace can set and 2 size properties to inform
159 * the userspace of the lut sizes. Each of the properties are
160 * optional. The gamma and degamma properties are only attached if
161 * their size is not 0 and ctm_property is only attached if has_ctm is
164 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
165 uint degamma_lut_size,
169 struct drm_device *dev = crtc->dev;
170 struct drm_mode_config *config = &dev->mode_config;
172 if (degamma_lut_size) {
173 drm_object_attach_property(&crtc->base,
174 config->degamma_lut_property, 0);
175 drm_object_attach_property(&crtc->base,
176 config->degamma_lut_size_property,
181 drm_object_attach_property(&crtc->base,
182 config->ctm_property, 0);
184 if (gamma_lut_size) {
185 drm_object_attach_property(&crtc->base,
186 config->gamma_lut_property, 0);
187 drm_object_attach_property(&crtc->base,
188 config->gamma_lut_size_property,
192 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
195 * drm_mode_crtc_set_gamma_size - set the gamma table size
196 * @crtc: CRTC to set the gamma table size for
197 * @gamma_size: size of the gamma table
199 * Drivers which support gamma tables should set this to the supported gamma
200 * table size when initializing the CRTC. Currently the drm core only supports a
201 * fixed gamma table size.
204 * Zero on success, negative errno on failure.
206 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
209 uint16_t *r_base, *g_base, *b_base;
212 crtc->gamma_size = gamma_size;
214 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
216 if (!crtc->gamma_store) {
217 crtc->gamma_size = 0;
221 r_base = crtc->gamma_store;
222 g_base = r_base + gamma_size;
223 b_base = g_base + gamma_size;
224 for (i = 0; i < gamma_size; i++) {
233 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
236 * drm_crtc_supports_legacy_gamma - does the crtc support legacy gamma correction table
239 * Returns true/false if the given crtc supports setting the legacy gamma
242 static bool drm_crtc_supports_legacy_gamma(struct drm_crtc *crtc)
244 u32 gamma_id = crtc->dev->mode_config.gamma_lut_property->base.id;
245 u32 degamma_id = crtc->dev->mode_config.degamma_lut_property->base.id;
247 if (!crtc->gamma_size)
250 if (crtc->funcs->gamma_set)
253 return !!(drm_mode_obj_find_prop_id(&crtc->base, gamma_id) ||
254 drm_mode_obj_find_prop_id(&crtc->base, degamma_id));
258 * drm_crtc_legacy_gamma_set - set the legacy gamma correction table
260 * @red: red correction table
261 * @green: green correction table
262 * @blue: blue correction table
263 * @size: size of the tables
264 * @ctx: lock acquire context
266 * Implements support for legacy gamma correction table for drivers
267 * that have set drm_crtc_funcs.gamma_set or that support color management
268 * through the DEGAMMA_LUT/GAMMA_LUT properties. See
269 * drm_crtc_enable_color_mgmt() and the containing chapter for
270 * how the atomic color management and gamma tables work.
272 * This function sets the gamma using drm_crtc_funcs.gamma_set if set, or
273 * alternatively using crtc color management properties.
275 static int drm_crtc_legacy_gamma_set(struct drm_crtc *crtc,
276 u16 *red, u16 *green, u16 *blue,
278 struct drm_modeset_acquire_ctx *ctx)
280 struct drm_device *dev = crtc->dev;
281 struct drm_atomic_state *state;
282 struct drm_crtc_state *crtc_state;
283 struct drm_property_blob *blob;
284 struct drm_color_lut *blob_data;
285 u32 gamma_id = dev->mode_config.gamma_lut_property->base.id;
286 u32 degamma_id = dev->mode_config.degamma_lut_property->base.id;
291 if (crtc->funcs->gamma_set)
292 return crtc->funcs->gamma_set(crtc, red, green, blue, size, ctx);
294 if (drm_mode_obj_find_prop_id(&crtc->base, gamma_id))
295 use_gamma_lut = true;
296 else if (drm_mode_obj_find_prop_id(&crtc->base, degamma_id))
297 use_gamma_lut = false;
301 state = drm_atomic_state_alloc(crtc->dev);
305 blob = drm_property_create_blob(dev,
306 sizeof(struct drm_color_lut) * size,
314 /* Prepare GAMMA_LUT with the legacy values. */
315 blob_data = blob->data;
316 for (i = 0; i < size; i++) {
317 blob_data[i].red = red[i];
318 blob_data[i].green = green[i];
319 blob_data[i].blue = blue[i];
322 state->acquire_ctx = ctx;
323 crtc_state = drm_atomic_get_crtc_state(state, crtc);
324 if (IS_ERR(crtc_state)) {
325 ret = PTR_ERR(crtc_state);
329 /* Set GAMMA_LUT and reset DEGAMMA_LUT and CTM */
330 replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
331 use_gamma_lut ? NULL : blob);
332 replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
333 replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
334 use_gamma_lut ? blob : NULL);
335 crtc_state->color_mgmt_changed |= replaced;
337 ret = drm_atomic_commit(state);
340 drm_atomic_state_put(state);
341 drm_property_blob_put(blob);
346 * drm_mode_gamma_set_ioctl - set the gamma table
349 * @file_priv: DRM file info
351 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
352 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
354 * Called by the user via ioctl.
357 * Zero on success, negative errno on failure.
359 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
360 void *data, struct drm_file *file_priv)
362 struct drm_mode_crtc_lut *crtc_lut = data;
363 struct drm_crtc *crtc;
364 void *r_base, *g_base, *b_base;
366 struct drm_modeset_acquire_ctx ctx;
369 if (!drm_core_check_feature(dev, DRIVER_MODESET))
372 crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
376 if (!drm_crtc_supports_legacy_gamma(crtc))
379 /* memcpy into gamma store */
380 if (crtc_lut->gamma_size != crtc->gamma_size)
383 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
385 size = crtc_lut->gamma_size * (sizeof(uint16_t));
386 r_base = crtc->gamma_store;
387 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
392 g_base = r_base + size;
393 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
398 b_base = g_base + size;
399 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
404 ret = drm_crtc_legacy_gamma_set(crtc, r_base, g_base, b_base,
405 crtc->gamma_size, &ctx);
408 DRM_MODESET_LOCK_ALL_END(dev, ctx, ret);
414 * drm_mode_gamma_get_ioctl - get the gamma table
417 * @file_priv: DRM file info
419 * Copy the current gamma table into the storage provided. This also provides
420 * the gamma table size the driver expects, which can be used to size the
423 * Called by the user via ioctl.
426 * Zero on success, negative errno on failure.
428 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
429 void *data, struct drm_file *file_priv)
431 struct drm_mode_crtc_lut *crtc_lut = data;
432 struct drm_crtc *crtc;
433 void *r_base, *g_base, *b_base;
437 if (!drm_core_check_feature(dev, DRIVER_MODESET))
440 crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
444 /* memcpy into gamma store */
445 if (crtc_lut->gamma_size != crtc->gamma_size)
448 drm_modeset_lock(&crtc->mutex, NULL);
449 size = crtc_lut->gamma_size * (sizeof(uint16_t));
450 r_base = crtc->gamma_store;
451 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
456 g_base = r_base + size;
457 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
462 b_base = g_base + size;
463 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
468 drm_modeset_unlock(&crtc->mutex);
472 static const char * const color_encoding_name[] = {
473 [DRM_COLOR_YCBCR_BT601] = "ITU-R BT.601 YCbCr",
474 [DRM_COLOR_YCBCR_BT709] = "ITU-R BT.709 YCbCr",
475 [DRM_COLOR_YCBCR_BT2020] = "ITU-R BT.2020 YCbCr",
478 static const char * const color_range_name[] = {
479 [DRM_COLOR_YCBCR_FULL_RANGE] = "YCbCr full range",
480 [DRM_COLOR_YCBCR_LIMITED_RANGE] = "YCbCr limited range",
484 * drm_get_color_encoding_name - return a string for color encoding
485 * @encoding: color encoding to compute name of
487 * In contrast to the other drm_get_*_name functions this one here returns a
488 * const pointer and hence is threadsafe.
490 const char *drm_get_color_encoding_name(enum drm_color_encoding encoding)
492 if (WARN_ON(encoding >= ARRAY_SIZE(color_encoding_name)))
495 return color_encoding_name[encoding];
499 * drm_get_color_range_name - return a string for color range
500 * @range: color range to compute name of
502 * In contrast to the other drm_get_*_name functions this one here returns a
503 * const pointer and hence is threadsafe.
505 const char *drm_get_color_range_name(enum drm_color_range range)
507 if (WARN_ON(range >= ARRAY_SIZE(color_range_name)))
510 return color_range_name[range];
514 * drm_plane_create_color_properties - color encoding related plane properties
515 * @plane: plane object
516 * @supported_encodings: bitfield indicating supported color encodings
517 * @supported_ranges: bitfileld indicating supported color ranges
518 * @default_encoding: default color encoding
519 * @default_range: default color range
521 * Create and attach plane specific COLOR_ENCODING and COLOR_RANGE
522 * properties to @plane. The supported encodings and ranges should
523 * be provided in supported_encodings and supported_ranges bitmasks.
524 * Each bit set in the bitmask indicates that its number as enum
525 * value is supported.
527 int drm_plane_create_color_properties(struct drm_plane *plane,
528 u32 supported_encodings,
529 u32 supported_ranges,
530 enum drm_color_encoding default_encoding,
531 enum drm_color_range default_range)
533 struct drm_device *dev = plane->dev;
534 struct drm_property *prop;
535 struct drm_prop_enum_list enum_list[MAX_T(int, DRM_COLOR_ENCODING_MAX,
536 DRM_COLOR_RANGE_MAX)];
539 if (WARN_ON(supported_encodings == 0 ||
540 (supported_encodings & -BIT(DRM_COLOR_ENCODING_MAX)) != 0 ||
541 (supported_encodings & BIT(default_encoding)) == 0))
544 if (WARN_ON(supported_ranges == 0 ||
545 (supported_ranges & -BIT(DRM_COLOR_RANGE_MAX)) != 0 ||
546 (supported_ranges & BIT(default_range)) == 0))
550 for (i = 0; i < DRM_COLOR_ENCODING_MAX; i++) {
551 if ((supported_encodings & BIT(i)) == 0)
554 enum_list[len].type = i;
555 enum_list[len].name = color_encoding_name[i];
559 prop = drm_property_create_enum(dev, 0, "COLOR_ENCODING",
563 plane->color_encoding_property = prop;
564 drm_object_attach_property(&plane->base, prop, default_encoding);
566 plane->state->color_encoding = default_encoding;
569 for (i = 0; i < DRM_COLOR_RANGE_MAX; i++) {
570 if ((supported_ranges & BIT(i)) == 0)
573 enum_list[len].type = i;
574 enum_list[len].name = color_range_name[i];
578 prop = drm_property_create_enum(dev, 0, "COLOR_RANGE",
582 plane->color_range_property = prop;
583 drm_object_attach_property(&plane->base, prop, default_range);
585 plane->state->color_range = default_range;
589 EXPORT_SYMBOL(drm_plane_create_color_properties);
592 * drm_color_lut_check - check validity of lookup table
593 * @lut: property blob containing LUT to check
594 * @tests: bitmask of tests to run
596 * Helper to check whether a userspace-provided lookup table is valid and
597 * satisfies hardware requirements. Drivers pass a bitmask indicating which of
598 * the tests in &drm_color_lut_tests should be performed.
600 * Returns 0 on success, -EINVAL on failure.
602 int drm_color_lut_check(const struct drm_property_blob *lut, u32 tests)
604 const struct drm_color_lut *entry;
611 for (i = 0; i < drm_color_lut_size(lut); i++) {
612 if (tests & DRM_COLOR_LUT_EQUAL_CHANNELS) {
613 if (entry[i].red != entry[i].blue ||
614 entry[i].red != entry[i].green) {
615 DRM_DEBUG_KMS("All LUT entries must have equal r/g/b\n");
620 if (i > 0 && tests & DRM_COLOR_LUT_NON_DECREASING) {
621 if (entry[i].red < entry[i - 1].red ||
622 entry[i].green < entry[i - 1].green ||
623 entry[i].blue < entry[i - 1].blue) {
624 DRM_DEBUG_KMS("LUT entries must never decrease.\n");
632 EXPORT_SYMBOL(drm_color_lut_check);