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_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>
31 #include "drm_crtc_internal.h"
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().
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]).
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.
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.
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
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.
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]).
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.
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.
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.
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().
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.
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.
112 * drm_color_ctm_s31_32_to_qm_n
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
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]
123 * A Q3.12 format number:
124 * - required bit: 3 + 12 = 15bits
125 * - range: [-2^2, 2^2 - 2^−15]
127 * NOTE: the m can be zero if all bit_precision are used to present fractional
130 u64 drm_color_ctm_s31_32_to_qm_n(u64 user_input, u32 m, u32 n)
132 u64 mag = (user_input & ~BIT_ULL(63)) >> (32 - n);
133 bool negative = !!(user_input & BIT_ULL(63));
136 WARN_ON(m > 32 || n > 32);
138 val = clamp_val(mag, 0, negative ?
139 BIT_ULL(n + m - 1) : BIT_ULL(n + m - 1) - 1);
141 return negative ? -val : val;
143 EXPORT_SYMBOL(drm_color_ctm_s31_32_to_qm_n);
146 * drm_crtc_enable_color_mgmt - enable color management properties
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)
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
160 * Drivers should use drm_atomic_helper_legacy_gamma_set() to implement the
161 * legacy &drm_crtc_funcs.gamma_set callback.
163 void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
164 uint degamma_lut_size,
168 struct drm_device *dev = crtc->dev;
169 struct drm_mode_config *config = &dev->mode_config;
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,
180 drm_object_attach_property(&crtc->base,
181 config->ctm_property, 0);
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,
191 EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);
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
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.
203 * Zero on success, negative errno on failure.
205 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
208 uint16_t *r_base, *g_base, *b_base;
211 crtc->gamma_size = gamma_size;
213 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
215 if (!crtc->gamma_store) {
216 crtc->gamma_size = 0;
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++) {
232 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
235 * drm_mode_gamma_set_ioctl - set the gamma table
238 * @file_priv: DRM file info
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.
243 * Called by the user via ioctl.
246 * Zero on success, negative errno on failure.
248 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
249 void *data, struct drm_file *file_priv)
251 struct drm_mode_crtc_lut *crtc_lut = data;
252 struct drm_crtc *crtc;
253 void *r_base, *g_base, *b_base;
255 struct drm_modeset_acquire_ctx ctx;
258 if (!drm_core_check_feature(dev, DRIVER_MODESET))
261 crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
265 if (crtc->funcs->gamma_set == NULL)
268 /* memcpy into gamma store */
269 if (crtc_lut->gamma_size != crtc->gamma_size)
272 DRM_MODESET_LOCK_ALL_BEGIN(dev, ctx, 0, ret);
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)) {
281 g_base = r_base + size;
282 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
287 b_base = g_base + size;
288 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
293 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
294 crtc->gamma_size, &ctx);
297 DRM_MODESET_LOCK_ALL_END(ctx, ret);
303 * drm_mode_gamma_get_ioctl - get the gamma table
306 * @file_priv: DRM file info
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
312 * Called by the user via ioctl.
315 * Zero on success, negative errno on failure.
317 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
318 void *data, struct drm_file *file_priv)
320 struct drm_mode_crtc_lut *crtc_lut = data;
321 struct drm_crtc *crtc;
322 void *r_base, *g_base, *b_base;
326 if (!drm_core_check_feature(dev, DRIVER_MODESET))
329 crtc = drm_crtc_find(dev, file_priv, crtc_lut->crtc_id);
333 /* memcpy into gamma store */
334 if (crtc_lut->gamma_size != crtc->gamma_size)
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)) {
345 g_base = r_base + size;
346 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
351 b_base = g_base + size;
352 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
357 drm_modeset_unlock(&crtc->mutex);
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",
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",
373 * drm_get_color_encoding_name - return a string for color encoding
374 * @encoding: color encoding to compute name of
376 * In contrast to the other drm_get_*_name functions this one here returns a
377 * const pointer and hence is threadsafe.
379 const char *drm_get_color_encoding_name(enum drm_color_encoding encoding)
381 if (WARN_ON(encoding >= ARRAY_SIZE(color_encoding_name)))
384 return color_encoding_name[encoding];
388 * drm_get_color_range_name - return a string for color range
389 * @range: color range to compute name of
391 * In contrast to the other drm_get_*_name functions this one here returns a
392 * const pointer and hence is threadsafe.
394 const char *drm_get_color_range_name(enum drm_color_range range)
396 if (WARN_ON(range >= ARRAY_SIZE(color_range_name)))
399 return color_range_name[range];
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
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.
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)
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)];
428 if (WARN_ON(supported_encodings == 0 ||
429 (supported_encodings & -BIT(DRM_COLOR_ENCODING_MAX)) != 0 ||
430 (supported_encodings & BIT(default_encoding)) == 0))
433 if (WARN_ON(supported_ranges == 0 ||
434 (supported_ranges & -BIT(DRM_COLOR_RANGE_MAX)) != 0 ||
435 (supported_ranges & BIT(default_range)) == 0))
439 for (i = 0; i < DRM_COLOR_ENCODING_MAX; i++) {
440 if ((supported_encodings & BIT(i)) == 0)
443 enum_list[len].type = i;
444 enum_list[len].name = color_encoding_name[i];
448 prop = drm_property_create_enum(dev, 0, "COLOR_ENCODING",
452 plane->color_encoding_property = prop;
453 drm_object_attach_property(&plane->base, prop, default_encoding);
455 plane->state->color_encoding = default_encoding;
458 for (i = 0; i < DRM_COLOR_RANGE_MAX; i++) {
459 if ((supported_ranges & BIT(i)) == 0)
462 enum_list[len].type = i;
463 enum_list[len].name = color_range_name[i];
467 prop = drm_property_create_enum(dev, 0, "COLOR_RANGE",
471 plane->color_range_property = prop;
472 drm_object_attach_property(&plane->base, prop, default_range);
474 plane->state->color_range = default_range;
478 EXPORT_SYMBOL(drm_plane_create_color_properties);
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
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.
489 * Returns 0 on success, -EINVAL on failure.
491 int drm_color_lut_check(const struct drm_property_blob *lut, u32 tests)
493 const struct drm_color_lut *entry;
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");
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");
521 EXPORT_SYMBOL(drm_color_lut_check);