2 * Copyright © 2016 Intel Corporation
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:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
25 #include "intel_color.h"
26 #include "intel_display_types.h"
28 #define CTM_COEFF_SIGN (1ULL << 63)
30 #define CTM_COEFF_1_0 (1ULL << 32)
31 #define CTM_COEFF_2_0 (CTM_COEFF_1_0 << 1)
32 #define CTM_COEFF_4_0 (CTM_COEFF_2_0 << 1)
33 #define CTM_COEFF_8_0 (CTM_COEFF_4_0 << 1)
34 #define CTM_COEFF_0_5 (CTM_COEFF_1_0 >> 1)
35 #define CTM_COEFF_0_25 (CTM_COEFF_0_5 >> 1)
36 #define CTM_COEFF_0_125 (CTM_COEFF_0_25 >> 1)
38 #define CTM_COEFF_LIMITED_RANGE ((235ULL - 16ULL) * CTM_COEFF_1_0 / 255)
40 #define CTM_COEFF_NEGATIVE(coeff) (((coeff) & CTM_COEFF_SIGN) != 0)
41 #define CTM_COEFF_ABS(coeff) ((coeff) & (CTM_COEFF_SIGN - 1))
43 #define LEGACY_LUT_LENGTH 256
48 * |R/Cr| | c0 c1 c2 | ( |R/Cr| |preoff0| ) |postoff0|
49 * |G/Y | = | c3 c4 c5 | x ( |G/Y | + |preoff1| ) + |postoff1|
50 * |B/Cb| | c6 c7 c8 | ( |B/Cb| |preoff2| ) |postoff2|
52 * ILK/SNB don't have explicit post offsets, and instead
53 * CSC_MODE_YUV_TO_RGB and CSC_BLACK_SCREEN_OFFSET are used:
54 * CSC_MODE_YUV_TO_RGB=0 + CSC_BLACK_SCREEN_OFFSET=0 -> 1/2, 0, 1/2
55 * CSC_MODE_YUV_TO_RGB=0 + CSC_BLACK_SCREEN_OFFSET=1 -> 1/2, 1/16, 1/2
56 * CSC_MODE_YUV_TO_RGB=1 + CSC_BLACK_SCREEN_OFFSET=0 -> 0, 0, 0
57 * CSC_MODE_YUV_TO_RGB=1 + CSC_BLACK_SCREEN_OFFSET=1 -> 1/16, 1/16, 1/16
61 * Extract the CSC coefficient from a CTM coefficient (in U32.32 fixed point
62 * format). This macro takes the coefficient we want transformed and the
63 * number of fractional bits.
65 * We only have a 9 bits precision window which slides depending on the value
66 * of the CTM coefficient and we write the value from bit 3. We also round the
69 #define ILK_CSC_COEFF_FP(coeff, fbits) \
70 (clamp_val(((coeff) >> (32 - (fbits) - 3)) + 4, 0, 0xfff) & 0xff8)
72 #define ILK_CSC_COEFF_LIMITED_RANGE 0x0dc0
73 #define ILK_CSC_COEFF_1_0 0x7800
75 #define ILK_CSC_POSTOFF_LIMITED_RANGE (16 * (1 << 12) / 255)
77 /* Nop pre/post offsets */
78 static const u16 ilk_csc_off_zero[3] = {};
81 static const u16 ilk_csc_coeff_identity[9] = {
82 ILK_CSC_COEFF_1_0, 0, 0,
83 0, ILK_CSC_COEFF_1_0, 0,
84 0, 0, ILK_CSC_COEFF_1_0,
87 /* Limited range RGB post offsets */
88 static const u16 ilk_csc_postoff_limited_range[3] = {
89 ILK_CSC_POSTOFF_LIMITED_RANGE,
90 ILK_CSC_POSTOFF_LIMITED_RANGE,
91 ILK_CSC_POSTOFF_LIMITED_RANGE,
94 /* Full range RGB -> limited range RGB matrix */
95 static const u16 ilk_csc_coeff_limited_range[9] = {
96 ILK_CSC_COEFF_LIMITED_RANGE, 0, 0,
97 0, ILK_CSC_COEFF_LIMITED_RANGE, 0,
98 0, 0, ILK_CSC_COEFF_LIMITED_RANGE,
101 /* BT.709 full range RGB -> limited range YCbCr matrix */
102 static const u16 ilk_csc_coeff_rgb_to_ycbcr[9] = {
103 0x1e08, 0x9cc0, 0xb528,
104 0x2ba8, 0x09d8, 0x37e8,
105 0xbce8, 0x9ad8, 0x1e08,
108 /* Limited range YCbCr post offsets */
109 static const u16 ilk_csc_postoff_rgb_to_ycbcr[3] = {
110 0x0800, 0x0100, 0x0800,
113 static bool lut_is_legacy(const struct drm_property_blob *lut)
115 return drm_color_lut_size(lut) == LEGACY_LUT_LENGTH;
118 static bool crtc_state_is_legacy_gamma(const struct intel_crtc_state *crtc_state)
120 return !crtc_state->base.degamma_lut &&
121 !crtc_state->base.ctm &&
122 crtc_state->base.gamma_lut &&
123 lut_is_legacy(crtc_state->base.gamma_lut);
127 * When using limited range, multiply the matrix given by userspace by
128 * the matrix that we would use for the limited range.
130 static u64 *ctm_mult_by_limited(u64 *result, const u64 *input)
134 for (i = 0; i < 9; i++) {
135 u64 user_coeff = input[i];
136 u32 limited_coeff = CTM_COEFF_LIMITED_RANGE;
137 u32 abs_coeff = clamp_val(CTM_COEFF_ABS(user_coeff), 0,
138 CTM_COEFF_4_0 - 1) >> 2;
141 * By scaling every co-efficient with limited range (16-235)
142 * vs full range (0-255) the final o/p will be scaled down to
143 * fit in the limited range supported by the panel.
145 result[i] = mul_u32_u32(limited_coeff, abs_coeff) >> 30;
146 result[i] |= user_coeff & CTM_COEFF_SIGN;
152 static void ilk_update_pipe_csc(struct intel_crtc *crtc,
155 const u16 postoff[3])
157 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
158 enum pipe pipe = crtc->pipe;
160 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), preoff[0]);
161 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), preoff[1]);
162 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), preoff[2]);
164 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff[0] << 16 | coeff[1]);
165 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), coeff[2] << 16);
167 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff[3] << 16 | coeff[4]);
168 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), coeff[5] << 16);
170 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), coeff[6] << 16 | coeff[7]);
171 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff[8] << 16);
173 if (INTEL_GEN(dev_priv) >= 7) {
174 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff[0]);
175 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff[1]);
176 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff[2]);
180 static void icl_update_output_csc(struct intel_crtc *crtc,
183 const u16 postoff[3])
185 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
186 enum pipe pipe = crtc->pipe;
188 I915_WRITE(PIPE_CSC_OUTPUT_PREOFF_HI(pipe), preoff[0]);
189 I915_WRITE(PIPE_CSC_OUTPUT_PREOFF_ME(pipe), preoff[1]);
190 I915_WRITE(PIPE_CSC_OUTPUT_PREOFF_LO(pipe), preoff[2]);
192 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_RY_GY(pipe), coeff[0] << 16 | coeff[1]);
193 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_BY(pipe), coeff[2] << 16);
195 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_RU_GU(pipe), coeff[3] << 16 | coeff[4]);
196 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_BU(pipe), coeff[5] << 16);
198 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_RV_GV(pipe), coeff[6] << 16 | coeff[7]);
199 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_BV(pipe), coeff[8] << 16);
201 I915_WRITE(PIPE_CSC_OUTPUT_POSTOFF_HI(pipe), postoff[0]);
202 I915_WRITE(PIPE_CSC_OUTPUT_POSTOFF_ME(pipe), postoff[1]);
203 I915_WRITE(PIPE_CSC_OUTPUT_POSTOFF_LO(pipe), postoff[2]);
206 static bool ilk_csc_limited_range(const struct intel_crtc_state *crtc_state)
208 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
211 * FIXME if there's a gamma LUT after the CSC, we should
212 * do the range compression using the gamma LUT instead.
214 return crtc_state->limited_color_range &&
215 (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
216 IS_GEN_RANGE(dev_priv, 9, 10));
219 static void ilk_csc_convert_ctm(const struct intel_crtc_state *crtc_state,
222 const struct drm_color_ctm *ctm = crtc_state->base.ctm->data;
227 if (ilk_csc_limited_range(crtc_state))
228 input = ctm_mult_by_limited(temp, ctm->matrix);
233 * Convert fixed point S31.32 input to format supported by the
236 for (i = 0; i < 9; i++) {
237 u64 abs_coeff = ((1ULL << 63) - 1) & input[i];
240 * Clamp input value to min/max supported by
243 abs_coeff = clamp_val(abs_coeff, 0, CTM_COEFF_4_0 - 1);
248 if (CTM_COEFF_NEGATIVE(input[i]))
249 coeffs[i] |= 1 << 15;
251 if (abs_coeff < CTM_COEFF_0_125)
252 coeffs[i] |= (3 << 12) |
253 ILK_CSC_COEFF_FP(abs_coeff, 12);
254 else if (abs_coeff < CTM_COEFF_0_25)
255 coeffs[i] |= (2 << 12) |
256 ILK_CSC_COEFF_FP(abs_coeff, 11);
257 else if (abs_coeff < CTM_COEFF_0_5)
258 coeffs[i] |= (1 << 12) |
259 ILK_CSC_COEFF_FP(abs_coeff, 10);
260 else if (abs_coeff < CTM_COEFF_1_0)
261 coeffs[i] |= ILK_CSC_COEFF_FP(abs_coeff, 9);
262 else if (abs_coeff < CTM_COEFF_2_0)
263 coeffs[i] |= (7 << 12) |
264 ILK_CSC_COEFF_FP(abs_coeff, 8);
266 coeffs[i] |= (6 << 12) |
267 ILK_CSC_COEFF_FP(abs_coeff, 7);
271 static void ilk_load_csc_matrix(const struct intel_crtc_state *crtc_state)
273 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
274 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
275 bool limited_color_range = ilk_csc_limited_range(crtc_state);
277 if (crtc_state->base.ctm) {
280 ilk_csc_convert_ctm(crtc_state, coeff);
281 ilk_update_pipe_csc(crtc, ilk_csc_off_zero, coeff,
282 limited_color_range ?
283 ilk_csc_postoff_limited_range :
285 } else if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB) {
286 ilk_update_pipe_csc(crtc, ilk_csc_off_zero,
287 ilk_csc_coeff_rgb_to_ycbcr,
288 ilk_csc_postoff_rgb_to_ycbcr);
289 } else if (limited_color_range) {
290 ilk_update_pipe_csc(crtc, ilk_csc_off_zero,
291 ilk_csc_coeff_limited_range,
292 ilk_csc_postoff_limited_range);
293 } else if (crtc_state->csc_enable) {
295 * On GLK+ both pipe CSC and degamma LUT are controlled
296 * by csc_enable. Hence for the cases where the degama
297 * LUT is needed but CSC is not we need to load an
300 WARN_ON(!IS_CANNONLAKE(dev_priv) && !IS_GEMINILAKE(dev_priv));
302 ilk_update_pipe_csc(crtc, ilk_csc_off_zero,
303 ilk_csc_coeff_identity,
307 I915_WRITE(PIPE_CSC_MODE(crtc->pipe), crtc_state->csc_mode);
310 static void icl_load_csc_matrix(const struct intel_crtc_state *crtc_state)
312 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
313 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
315 if (crtc_state->base.ctm) {
318 ilk_csc_convert_ctm(crtc_state, coeff);
319 ilk_update_pipe_csc(crtc, ilk_csc_off_zero,
320 coeff, ilk_csc_off_zero);
323 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB) {
324 icl_update_output_csc(crtc, ilk_csc_off_zero,
325 ilk_csc_coeff_rgb_to_ycbcr,
326 ilk_csc_postoff_rgb_to_ycbcr);
327 } else if (crtc_state->limited_color_range) {
328 icl_update_output_csc(crtc, ilk_csc_off_zero,
329 ilk_csc_coeff_limited_range,
330 ilk_csc_postoff_limited_range);
333 I915_WRITE(PIPE_CSC_MODE(crtc->pipe), crtc_state->csc_mode);
337 * Set up the pipe CSC unit on CherryView.
339 static void cherryview_load_csc_matrix(const struct intel_crtc_state *crtc_state)
341 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
342 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
343 enum pipe pipe = crtc->pipe;
345 if (crtc_state->base.ctm) {
346 const struct drm_color_ctm *ctm = crtc_state->base.ctm->data;
350 for (i = 0; i < ARRAY_SIZE(coeffs); i++) {
352 ((1ULL << 63) - 1) & ctm->matrix[i];
354 /* Round coefficient. */
355 abs_coeff += 1 << (32 - 13);
356 /* Clamp to hardware limits. */
357 abs_coeff = clamp_val(abs_coeff, 0, CTM_COEFF_8_0 - 1);
359 /* Write coefficients in S3.12 format. */
360 if (ctm->matrix[i] & (1ULL << 63))
362 coeffs[i] |= ((abs_coeff >> 32) & 7) << 12;
363 coeffs[i] |= (abs_coeff >> 20) & 0xfff;
366 I915_WRITE(CGM_PIPE_CSC_COEFF01(pipe),
367 coeffs[1] << 16 | coeffs[0]);
368 I915_WRITE(CGM_PIPE_CSC_COEFF23(pipe),
369 coeffs[3] << 16 | coeffs[2]);
370 I915_WRITE(CGM_PIPE_CSC_COEFF45(pipe),
371 coeffs[5] << 16 | coeffs[4]);
372 I915_WRITE(CGM_PIPE_CSC_COEFF67(pipe),
373 coeffs[7] << 16 | coeffs[6]);
374 I915_WRITE(CGM_PIPE_CSC_COEFF8(pipe), coeffs[8]);
377 I915_WRITE(CGM_PIPE_MODE(pipe), crtc_state->cgm_mode);
380 /* i965+ "10.6" bit interpolated format "even DW" (low 8 bits) */
381 static u32 i965_lut_10p6_ldw(const struct drm_color_lut *color)
383 return (color->red & 0xff) << 16 |
384 (color->green & 0xff) << 8 |
385 (color->blue & 0xff);
388 /* i965+ "10.6" interpolated format "odd DW" (high 8 bits) */
389 static u32 i965_lut_10p6_udw(const struct drm_color_lut *color)
391 return (color->red >> 8) << 16 |
392 (color->green >> 8) << 8 |
396 static u32 ilk_lut_10(const struct drm_color_lut *color)
398 return drm_color_lut_extract(color->red, 10) << 20 |
399 drm_color_lut_extract(color->green, 10) << 10 |
400 drm_color_lut_extract(color->blue, 10);
403 /* Loads the legacy palette/gamma unit for the CRTC. */
404 static void i9xx_load_luts_internal(const struct intel_crtc_state *crtc_state,
405 const struct drm_property_blob *blob)
407 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
408 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
409 enum pipe pipe = crtc->pipe;
412 if (HAS_GMCH(dev_priv)) {
413 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
414 assert_dsi_pll_enabled(dev_priv);
416 assert_pll_enabled(dev_priv, pipe);
420 const struct drm_color_lut *lut = blob->data;
422 for (i = 0; i < 256; i++) {
424 (drm_color_lut_extract(lut[i].red, 8) << 16) |
425 (drm_color_lut_extract(lut[i].green, 8) << 8) |
426 drm_color_lut_extract(lut[i].blue, 8);
428 if (HAS_GMCH(dev_priv))
429 I915_WRITE(PALETTE(pipe, i), word);
431 I915_WRITE(LGC_PALETTE(pipe, i), word);
436 static void i9xx_load_luts(const struct intel_crtc_state *crtc_state)
438 i9xx_load_luts_internal(crtc_state, crtc_state->base.gamma_lut);
441 static void i9xx_color_commit(const struct intel_crtc_state *crtc_state)
443 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
444 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
445 enum pipe pipe = crtc->pipe;
448 val = I915_READ(PIPECONF(pipe));
449 val &= ~PIPECONF_GAMMA_MODE_MASK_I9XX;
450 val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
451 I915_WRITE(PIPECONF(pipe), val);
454 static void ilk_color_commit(const struct intel_crtc_state *crtc_state)
456 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
457 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
458 enum pipe pipe = crtc->pipe;
461 val = I915_READ(PIPECONF(pipe));
462 val &= ~PIPECONF_GAMMA_MODE_MASK_ILK;
463 val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
464 I915_WRITE(PIPECONF(pipe), val);
466 ilk_load_csc_matrix(crtc_state);
469 static void hsw_color_commit(const struct intel_crtc_state *crtc_state)
471 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
472 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
474 I915_WRITE(GAMMA_MODE(crtc->pipe), crtc_state->gamma_mode);
476 ilk_load_csc_matrix(crtc_state);
479 static void skl_color_commit(const struct intel_crtc_state *crtc_state)
481 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
482 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
483 enum pipe pipe = crtc->pipe;
487 * We don't (yet) allow userspace to control the pipe background color,
488 * so force it to black, but apply pipe gamma and CSC appropriately
489 * so that its handling will match how we program our planes.
491 if (crtc_state->gamma_enable)
492 val |= SKL_BOTTOM_COLOR_GAMMA_ENABLE;
493 if (crtc_state->csc_enable)
494 val |= SKL_BOTTOM_COLOR_CSC_ENABLE;
495 I915_WRITE(SKL_BOTTOM_COLOR(pipe), val);
497 I915_WRITE(GAMMA_MODE(crtc->pipe), crtc_state->gamma_mode);
499 if (INTEL_GEN(dev_priv) >= 11)
500 icl_load_csc_matrix(crtc_state);
502 ilk_load_csc_matrix(crtc_state);
505 static void i965_load_lut_10p6(struct intel_crtc *crtc,
506 const struct drm_property_blob *blob)
508 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
509 const struct drm_color_lut *lut = blob->data;
510 int i, lut_size = drm_color_lut_size(blob);
511 enum pipe pipe = crtc->pipe;
513 for (i = 0; i < lut_size - 1; i++) {
514 I915_WRITE(PALETTE(pipe, 2 * i + 0),
515 i965_lut_10p6_ldw(&lut[i]));
516 I915_WRITE(PALETTE(pipe, 2 * i + 1),
517 i965_lut_10p6_udw(&lut[i]));
520 I915_WRITE(PIPEGCMAX(pipe, 0), lut[i].red);
521 I915_WRITE(PIPEGCMAX(pipe, 1), lut[i].green);
522 I915_WRITE(PIPEGCMAX(pipe, 2), lut[i].blue);
525 static void i965_load_luts(const struct intel_crtc_state *crtc_state)
527 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
528 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
530 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT)
531 i9xx_load_luts(crtc_state);
533 i965_load_lut_10p6(crtc, gamma_lut);
536 static void ilk_load_lut_10(struct intel_crtc *crtc,
537 const struct drm_property_blob *blob)
539 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
540 const struct drm_color_lut *lut = blob->data;
541 int i, lut_size = drm_color_lut_size(blob);
542 enum pipe pipe = crtc->pipe;
544 for (i = 0; i < lut_size; i++)
545 I915_WRITE(PREC_PALETTE(pipe, i), ilk_lut_10(&lut[i]));
548 static void ilk_load_luts(const struct intel_crtc_state *crtc_state)
550 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
551 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
553 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT)
554 i9xx_load_luts(crtc_state);
556 ilk_load_lut_10(crtc, gamma_lut);
559 static int ivb_lut_10_size(u32 prec_index)
561 if (prec_index & PAL_PREC_SPLIT_MODE)
568 * IVB/HSW Bspec / PAL_PREC_INDEX:
569 * "Restriction : Index auto increment mode is not
570 * supported and must not be enabled."
572 static void ivb_load_lut_10(struct intel_crtc *crtc,
573 const struct drm_property_blob *blob,
576 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
577 int hw_lut_size = ivb_lut_10_size(prec_index);
578 const struct drm_color_lut *lut = blob->data;
579 int i, lut_size = drm_color_lut_size(blob);
580 enum pipe pipe = crtc->pipe;
582 for (i = 0; i < hw_lut_size; i++) {
583 /* We discard half the user entries in split gamma mode */
584 const struct drm_color_lut *entry =
585 &lut[i * (lut_size - 1) / (hw_lut_size - 1)];
587 I915_WRITE(PREC_PAL_INDEX(pipe), prec_index++);
588 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_10(entry));
592 * Reset the index, otherwise it prevents the legacy palette to be
595 I915_WRITE(PREC_PAL_INDEX(pipe), 0);
598 /* On BDW+ the index auto increment mode actually works */
599 static void bdw_load_lut_10(struct intel_crtc *crtc,
600 const struct drm_property_blob *blob,
603 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
604 int hw_lut_size = ivb_lut_10_size(prec_index);
605 const struct drm_color_lut *lut = blob->data;
606 int i, lut_size = drm_color_lut_size(blob);
607 enum pipe pipe = crtc->pipe;
609 I915_WRITE(PREC_PAL_INDEX(pipe), prec_index |
610 PAL_PREC_AUTO_INCREMENT);
612 for (i = 0; i < hw_lut_size; i++) {
613 /* We discard half the user entries in split gamma mode */
614 const struct drm_color_lut *entry =
615 &lut[i * (lut_size - 1) / (hw_lut_size - 1)];
617 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_10(entry));
621 * Reset the index, otherwise it prevents the legacy palette to be
624 I915_WRITE(PREC_PAL_INDEX(pipe), 0);
627 static void ivb_load_lut_ext_max(struct intel_crtc *crtc)
629 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
630 enum pipe pipe = crtc->pipe;
632 /* Program the max register to clamp values > 1.0. */
633 I915_WRITE(PREC_PAL_EXT_GC_MAX(pipe, 0), 1 << 16);
634 I915_WRITE(PREC_PAL_EXT_GC_MAX(pipe, 1), 1 << 16);
635 I915_WRITE(PREC_PAL_EXT_GC_MAX(pipe, 2), 1 << 16);
638 * Program the gc max 2 register to clamp values > 1.0.
639 * ToDo: Extend the ABI to be able to program values
642 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
643 I915_WRITE(PREC_PAL_EXT2_GC_MAX(pipe, 0), 1 << 16);
644 I915_WRITE(PREC_PAL_EXT2_GC_MAX(pipe, 1), 1 << 16);
645 I915_WRITE(PREC_PAL_EXT2_GC_MAX(pipe, 2), 1 << 16);
649 static void ivb_load_luts(const struct intel_crtc_state *crtc_state)
651 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
652 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
653 const struct drm_property_blob *degamma_lut = crtc_state->base.degamma_lut;
655 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT) {
656 i9xx_load_luts(crtc_state);
657 } else if (crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT) {
658 ivb_load_lut_10(crtc, degamma_lut, PAL_PREC_SPLIT_MODE |
659 PAL_PREC_INDEX_VALUE(0));
660 ivb_load_lut_ext_max(crtc);
661 ivb_load_lut_10(crtc, gamma_lut, PAL_PREC_SPLIT_MODE |
662 PAL_PREC_INDEX_VALUE(512));
664 const struct drm_property_blob *blob = gamma_lut ?: degamma_lut;
666 ivb_load_lut_10(crtc, blob,
667 PAL_PREC_INDEX_VALUE(0));
668 ivb_load_lut_ext_max(crtc);
672 static void bdw_load_luts(const struct intel_crtc_state *crtc_state)
674 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
675 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
676 const struct drm_property_blob *degamma_lut = crtc_state->base.degamma_lut;
678 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT) {
679 i9xx_load_luts(crtc_state);
680 } else if (crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT) {
681 bdw_load_lut_10(crtc, degamma_lut, PAL_PREC_SPLIT_MODE |
682 PAL_PREC_INDEX_VALUE(0));
683 ivb_load_lut_ext_max(crtc);
684 bdw_load_lut_10(crtc, gamma_lut, PAL_PREC_SPLIT_MODE |
685 PAL_PREC_INDEX_VALUE(512));
687 const struct drm_property_blob *blob = gamma_lut ?: degamma_lut;
689 bdw_load_lut_10(crtc, blob,
690 PAL_PREC_INDEX_VALUE(0));
691 ivb_load_lut_ext_max(crtc);
695 static void glk_load_degamma_lut(const struct intel_crtc_state *crtc_state)
697 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
698 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
699 enum pipe pipe = crtc->pipe;
700 const u32 lut_size = INTEL_INFO(dev_priv)->color.degamma_lut_size;
701 const struct drm_color_lut *lut = crtc_state->base.degamma_lut->data;
705 * When setting the auto-increment bit, the hardware seems to
706 * ignore the index bits, so we need to reset it to index 0
709 I915_WRITE(PRE_CSC_GAMC_INDEX(pipe), 0);
710 I915_WRITE(PRE_CSC_GAMC_INDEX(pipe), PRE_CSC_GAMC_AUTO_INCREMENT);
712 for (i = 0; i < lut_size; i++) {
714 * First 33 entries represent range from 0 to 1.0
715 * 34th and 35th entry will represent extended range
716 * inputs 3.0 and 7.0 respectively, currently clamped
717 * at 1.0. Since the precision is 16bit, the user
718 * value can be directly filled to register.
719 * The pipe degamma table in GLK+ onwards doesn't
720 * support different values per channel, so this just
721 * programs green value which will be equal to Red and
722 * Blue into the lut registers.
723 * ToDo: Extend to max 7.0. Enable 32 bit input value
724 * as compared to just 16 to achieve this.
726 I915_WRITE(PRE_CSC_GAMC_DATA(pipe), lut[i].green);
729 /* Clamp values > 1.0. */
731 I915_WRITE(PRE_CSC_GAMC_DATA(pipe), 1 << 16);
734 static void glk_load_degamma_lut_linear(const struct intel_crtc_state *crtc_state)
736 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
737 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
738 enum pipe pipe = crtc->pipe;
739 const u32 lut_size = INTEL_INFO(dev_priv)->color.degamma_lut_size;
743 * When setting the auto-increment bit, the hardware seems to
744 * ignore the index bits, so we need to reset it to index 0
747 I915_WRITE(PRE_CSC_GAMC_INDEX(pipe), 0);
748 I915_WRITE(PRE_CSC_GAMC_INDEX(pipe), PRE_CSC_GAMC_AUTO_INCREMENT);
750 for (i = 0; i < lut_size; i++) {
751 u32 v = (i << 16) / (lut_size - 1);
753 I915_WRITE(PRE_CSC_GAMC_DATA(pipe), v);
756 /* Clamp values > 1.0. */
758 I915_WRITE(PRE_CSC_GAMC_DATA(pipe), 1 << 16);
761 static void glk_load_luts(const struct intel_crtc_state *crtc_state)
763 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
764 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
767 * On GLK+ both pipe CSC and degamma LUT are controlled
768 * by csc_enable. Hence for the cases where the CSC is
769 * needed but degamma LUT is not we need to load a
770 * linear degamma LUT. In fact we'll just always load
771 * the degama LUT so that we don't have to reload
772 * it every time the pipe CSC is being enabled.
774 if (crtc_state->base.degamma_lut)
775 glk_load_degamma_lut(crtc_state);
777 glk_load_degamma_lut_linear(crtc_state);
779 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT) {
780 i9xx_load_luts(crtc_state);
782 bdw_load_lut_10(crtc, gamma_lut, PAL_PREC_INDEX_VALUE(0));
783 ivb_load_lut_ext_max(crtc);
787 /* ilk+ "12.4" interpolated format (high 10 bits) */
788 static u32 ilk_lut_12p4_udw(const struct drm_color_lut *color)
790 return (color->red >> 6) << 20 | (color->green >> 6) << 10 |
794 /* ilk+ "12.4" interpolated format (low 6 bits) */
795 static u32 ilk_lut_12p4_ldw(const struct drm_color_lut *color)
797 return (color->red & 0x3f) << 24 | (color->green & 0x3f) << 14 |
798 (color->blue & 0x3f) << 4;
802 icl_load_gcmax(const struct intel_crtc_state *crtc_state,
803 const struct drm_color_lut *color)
805 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
806 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
807 enum pipe pipe = crtc->pipe;
809 /* Fixme: LUT entries are 16 bit only, so we can prog 0xFFFF max */
810 I915_WRITE(PREC_PAL_GC_MAX(pipe, 0), color->red);
811 I915_WRITE(PREC_PAL_GC_MAX(pipe, 1), color->green);
812 I915_WRITE(PREC_PAL_GC_MAX(pipe, 2), color->blue);
816 icl_program_gamma_superfine_segment(const struct intel_crtc_state *crtc_state)
818 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
819 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
820 const struct drm_property_blob *blob = crtc_state->base.gamma_lut;
821 const struct drm_color_lut *lut = blob->data;
822 enum pipe pipe = crtc->pipe;
826 * Program Super Fine segment (let's call it seg1)...
828 * Super Fine segment's step is 1/(8 * 128 * 256) and it has
829 * 9 entries, corresponding to values 0, 1/(8 * 128 * 256),
830 * 2/(8 * 128 * 256) ... 8/(8 * 128 * 256).
832 I915_WRITE(PREC_PAL_MULTI_SEG_INDEX(pipe), PAL_PREC_AUTO_INCREMENT);
834 for (i = 0; i < 9; i++) {
835 const struct drm_color_lut *entry = &lut[i];
837 I915_WRITE(PREC_PAL_MULTI_SEG_DATA(pipe),
838 ilk_lut_12p4_ldw(entry));
839 I915_WRITE(PREC_PAL_MULTI_SEG_DATA(pipe),
840 ilk_lut_12p4_udw(entry));
845 icl_program_gamma_multi_segment(const struct intel_crtc_state *crtc_state)
847 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
848 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
849 const struct drm_property_blob *blob = crtc_state->base.gamma_lut;
850 const struct drm_color_lut *lut = blob->data;
851 const struct drm_color_lut *entry;
852 enum pipe pipe = crtc->pipe;
856 * Program Fine segment (let's call it seg2)...
858 * Fine segment's step is 1/(128 * 256) i.e. 1/(128 * 256), 2/(128 * 256)
859 * ... 256/(128 * 256). So in order to program fine segment of LUT we
860 * need to pick every 8th entry in the LUT, and program 256 indexes.
862 * PAL_PREC_INDEX[0] and PAL_PREC_INDEX[1] map to seg2[1],
863 * seg2[0] being unused by the hardware.
865 I915_WRITE(PREC_PAL_INDEX(pipe), PAL_PREC_AUTO_INCREMENT);
867 for (i = 1; i < 257; i++) {
869 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_12p4_ldw(entry));
870 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_12p4_udw(entry));
874 * Program Coarse segment (let's call it seg3)...
876 * Coarse segment starts from index 0 and it's step is 1/256 ie 0,
877 * 1/256, 2/256 ... 256/256. As per the description of each entry in LUT
878 * above, we need to pick every (8 * 128)th entry in LUT, and
879 * program 256 of those.
881 * Spec is not very clear about if entries seg3[0] and seg3[1] are
882 * being used or not, but we still need to program these to advance
885 for (i = 0; i < 256; i++) {
886 entry = &lut[i * 8 * 128];
887 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_12p4_ldw(entry));
888 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_12p4_udw(entry));
891 /* The last entry in the LUT is to be programmed in GCMAX */
892 entry = &lut[256 * 8 * 128];
893 icl_load_gcmax(crtc_state, entry);
894 ivb_load_lut_ext_max(crtc);
897 static void icl_load_luts(const struct intel_crtc_state *crtc_state)
899 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
900 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
902 if (crtc_state->base.degamma_lut)
903 glk_load_degamma_lut(crtc_state);
905 switch (crtc_state->gamma_mode & GAMMA_MODE_MODE_MASK) {
906 case GAMMA_MODE_MODE_8BIT:
907 i9xx_load_luts(crtc_state);
909 case GAMMA_MODE_MODE_12BIT_MULTI_SEGMENTED:
910 icl_program_gamma_superfine_segment(crtc_state);
911 icl_program_gamma_multi_segment(crtc_state);
914 bdw_load_lut_10(crtc, gamma_lut, PAL_PREC_INDEX_VALUE(0));
915 ivb_load_lut_ext_max(crtc);
919 static u32 chv_cgm_degamma_ldw(const struct drm_color_lut *color)
921 return drm_color_lut_extract(color->green, 14) << 16 |
922 drm_color_lut_extract(color->blue, 14);
925 static u32 chv_cgm_degamma_udw(const struct drm_color_lut *color)
927 return drm_color_lut_extract(color->red, 14);
930 static void chv_load_cgm_degamma(struct intel_crtc *crtc,
931 const struct drm_property_blob *blob)
933 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
934 const struct drm_color_lut *lut = blob->data;
935 int i, lut_size = drm_color_lut_size(blob);
936 enum pipe pipe = crtc->pipe;
938 for (i = 0; i < lut_size; i++) {
939 I915_WRITE(CGM_PIPE_DEGAMMA(pipe, i, 0),
940 chv_cgm_degamma_ldw(&lut[i]));
941 I915_WRITE(CGM_PIPE_DEGAMMA(pipe, i, 1),
942 chv_cgm_degamma_udw(&lut[i]));
946 static u32 chv_cgm_gamma_ldw(const struct drm_color_lut *color)
948 return drm_color_lut_extract(color->green, 10) << 16 |
949 drm_color_lut_extract(color->blue, 10);
952 static u32 chv_cgm_gamma_udw(const struct drm_color_lut *color)
954 return drm_color_lut_extract(color->red, 10);
957 static void chv_load_cgm_gamma(struct intel_crtc *crtc,
958 const struct drm_property_blob *blob)
960 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
961 const struct drm_color_lut *lut = blob->data;
962 int i, lut_size = drm_color_lut_size(blob);
963 enum pipe pipe = crtc->pipe;
965 for (i = 0; i < lut_size; i++) {
966 I915_WRITE(CGM_PIPE_GAMMA(pipe, i, 0),
967 chv_cgm_gamma_ldw(&lut[i]));
968 I915_WRITE(CGM_PIPE_GAMMA(pipe, i, 1),
969 chv_cgm_gamma_udw(&lut[i]));
973 static void chv_load_luts(const struct intel_crtc_state *crtc_state)
975 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
976 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
977 const struct drm_property_blob *degamma_lut = crtc_state->base.degamma_lut;
979 cherryview_load_csc_matrix(crtc_state);
981 if (crtc_state_is_legacy_gamma(crtc_state)) {
982 i9xx_load_luts(crtc_state);
987 chv_load_cgm_degamma(crtc, degamma_lut);
990 chv_load_cgm_gamma(crtc, gamma_lut);
993 void intel_color_load_luts(const struct intel_crtc_state *crtc_state)
995 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
997 dev_priv->display.load_luts(crtc_state);
1000 void intel_color_commit(const struct intel_crtc_state *crtc_state)
1002 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1004 dev_priv->display.color_commit(crtc_state);
1007 int intel_color_check(struct intel_crtc_state *crtc_state)
1009 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1011 return dev_priv->display.color_check(crtc_state);
1014 void intel_color_get_config(struct intel_crtc_state *crtc_state)
1016 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1018 if (dev_priv->display.read_luts)
1019 dev_priv->display.read_luts(crtc_state);
1022 static bool need_plane_update(struct intel_plane *plane,
1023 const struct intel_crtc_state *crtc_state)
1025 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1028 * On pre-SKL the pipe gamma enable and pipe csc enable for
1029 * the pipe bottom color are configured via the primary plane.
1030 * We have to reconfigure that even if the plane is inactive.
1032 return crtc_state->active_planes & BIT(plane->id) ||
1033 (INTEL_GEN(dev_priv) < 9 &&
1034 plane->id == PLANE_PRIMARY);
1038 intel_color_add_affected_planes(struct intel_crtc_state *new_crtc_state)
1040 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
1041 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1042 struct intel_atomic_state *state =
1043 to_intel_atomic_state(new_crtc_state->base.state);
1044 const struct intel_crtc_state *old_crtc_state =
1045 intel_atomic_get_old_crtc_state(state, crtc);
1046 struct intel_plane *plane;
1048 if (!new_crtc_state->base.active ||
1049 drm_atomic_crtc_needs_modeset(&new_crtc_state->base))
1052 if (new_crtc_state->gamma_enable == old_crtc_state->gamma_enable &&
1053 new_crtc_state->csc_enable == old_crtc_state->csc_enable)
1056 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
1057 struct intel_plane_state *plane_state;
1059 if (!need_plane_update(plane, new_crtc_state))
1062 plane_state = intel_atomic_get_plane_state(state, plane);
1063 if (IS_ERR(plane_state))
1064 return PTR_ERR(plane_state);
1066 new_crtc_state->update_planes |= BIT(plane->id);
1072 static int check_lut_size(const struct drm_property_blob *lut, int expected)
1079 len = drm_color_lut_size(lut);
1080 if (len != expected) {
1081 DRM_DEBUG_KMS("Invalid LUT size; got %d, expected %d\n",
1089 static int check_luts(const struct intel_crtc_state *crtc_state)
1091 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
1092 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
1093 const struct drm_property_blob *degamma_lut = crtc_state->base.degamma_lut;
1094 int gamma_length, degamma_length;
1095 u32 gamma_tests, degamma_tests;
1097 /* Always allow legacy gamma LUT with no further checking. */
1098 if (crtc_state_is_legacy_gamma(crtc_state))
1101 /* C8 relies on its palette being stored in the legacy LUT */
1102 if (crtc_state->c8_planes) {
1103 DRM_DEBUG_KMS("C8 pixelformat requires the legacy LUT\n");
1107 degamma_length = INTEL_INFO(dev_priv)->color.degamma_lut_size;
1108 gamma_length = INTEL_INFO(dev_priv)->color.gamma_lut_size;
1109 degamma_tests = INTEL_INFO(dev_priv)->color.degamma_lut_tests;
1110 gamma_tests = INTEL_INFO(dev_priv)->color.gamma_lut_tests;
1112 if (check_lut_size(degamma_lut, degamma_length) ||
1113 check_lut_size(gamma_lut, gamma_length))
1116 if (drm_color_lut_check(degamma_lut, degamma_tests) ||
1117 drm_color_lut_check(gamma_lut, gamma_tests))
1123 static u32 i9xx_gamma_mode(struct intel_crtc_state *crtc_state)
1125 if (!crtc_state->gamma_enable ||
1126 crtc_state_is_legacy_gamma(crtc_state))
1127 return GAMMA_MODE_MODE_8BIT;
1129 return GAMMA_MODE_MODE_10BIT; /* i965+ only */
1132 static int i9xx_color_check(struct intel_crtc_state *crtc_state)
1136 ret = check_luts(crtc_state);
1140 crtc_state->gamma_enable =
1141 crtc_state->base.gamma_lut &&
1142 !crtc_state->c8_planes;
1144 crtc_state->gamma_mode = i9xx_gamma_mode(crtc_state);
1146 ret = intel_color_add_affected_planes(crtc_state);
1153 static u32 chv_cgm_mode(const struct intel_crtc_state *crtc_state)
1157 if (crtc_state_is_legacy_gamma(crtc_state))
1160 if (crtc_state->base.degamma_lut)
1161 cgm_mode |= CGM_PIPE_MODE_DEGAMMA;
1162 if (crtc_state->base.ctm)
1163 cgm_mode |= CGM_PIPE_MODE_CSC;
1164 if (crtc_state->base.gamma_lut)
1165 cgm_mode |= CGM_PIPE_MODE_GAMMA;
1171 * CHV color pipeline:
1172 * u0.10 -> CGM degamma -> u0.14 -> CGM csc -> u0.14 -> CGM gamma ->
1173 * u0.10 -> WGC csc -> u0.10 -> pipe gamma -> u0.10
1175 * We always bypass the WGC csc and use the CGM csc
1176 * instead since it has degamma and better precision.
1178 static int chv_color_check(struct intel_crtc_state *crtc_state)
1182 ret = check_luts(crtc_state);
1187 * Pipe gamma will be used only for the legacy LUT.
1188 * Otherwise we bypass it and use the CGM gamma instead.
1190 crtc_state->gamma_enable =
1191 crtc_state_is_legacy_gamma(crtc_state) &&
1192 !crtc_state->c8_planes;
1194 crtc_state->gamma_mode = GAMMA_MODE_MODE_8BIT;
1196 crtc_state->cgm_mode = chv_cgm_mode(crtc_state);
1198 ret = intel_color_add_affected_planes(crtc_state);
1205 static u32 ilk_gamma_mode(const struct intel_crtc_state *crtc_state)
1207 if (!crtc_state->gamma_enable ||
1208 crtc_state_is_legacy_gamma(crtc_state))
1209 return GAMMA_MODE_MODE_8BIT;
1211 return GAMMA_MODE_MODE_10BIT;
1214 static u32 ilk_csc_mode(const struct intel_crtc_state *crtc_state)
1217 * CSC comes after the LUT in RGB->YCbCr mode.
1218 * RGB->YCbCr needs the limited range offsets added to
1219 * the output. RGB limited range output is handled by
1220 * the hw automagically elsewhere.
1222 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
1223 return CSC_BLACK_SCREEN_OFFSET;
1225 return CSC_MODE_YUV_TO_RGB |
1226 CSC_POSITION_BEFORE_GAMMA;
1229 static int ilk_color_check(struct intel_crtc_state *crtc_state)
1233 ret = check_luts(crtc_state);
1237 crtc_state->gamma_enable =
1238 crtc_state->base.gamma_lut &&
1239 !crtc_state->c8_planes;
1242 * We don't expose the ctm on ilk/snb currently, also RGB
1243 * limited range output is handled by the hw automagically.
1245 crtc_state->csc_enable =
1246 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB;
1248 crtc_state->gamma_mode = ilk_gamma_mode(crtc_state);
1250 crtc_state->csc_mode = ilk_csc_mode(crtc_state);
1252 ret = intel_color_add_affected_planes(crtc_state);
1259 static u32 ivb_gamma_mode(const struct intel_crtc_state *crtc_state)
1261 if (!crtc_state->gamma_enable ||
1262 crtc_state_is_legacy_gamma(crtc_state))
1263 return GAMMA_MODE_MODE_8BIT;
1264 else if (crtc_state->base.gamma_lut &&
1265 crtc_state->base.degamma_lut)
1266 return GAMMA_MODE_MODE_SPLIT;
1268 return GAMMA_MODE_MODE_10BIT;
1271 static u32 ivb_csc_mode(const struct intel_crtc_state *crtc_state)
1273 bool limited_color_range = ilk_csc_limited_range(crtc_state);
1276 * CSC comes after the LUT in degamma, RGB->YCbCr,
1277 * and RGB full->limited range mode.
1279 if (crtc_state->base.degamma_lut ||
1280 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
1281 limited_color_range)
1284 return CSC_POSITION_BEFORE_GAMMA;
1287 static int ivb_color_check(struct intel_crtc_state *crtc_state)
1289 bool limited_color_range = ilk_csc_limited_range(crtc_state);
1292 ret = check_luts(crtc_state);
1296 crtc_state->gamma_enable =
1297 (crtc_state->base.gamma_lut ||
1298 crtc_state->base.degamma_lut) &&
1299 !crtc_state->c8_planes;
1301 crtc_state->csc_enable =
1302 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
1303 crtc_state->base.ctm || limited_color_range;
1305 crtc_state->gamma_mode = ivb_gamma_mode(crtc_state);
1307 crtc_state->csc_mode = ivb_csc_mode(crtc_state);
1309 ret = intel_color_add_affected_planes(crtc_state);
1316 static u32 glk_gamma_mode(const struct intel_crtc_state *crtc_state)
1318 if (!crtc_state->gamma_enable ||
1319 crtc_state_is_legacy_gamma(crtc_state))
1320 return GAMMA_MODE_MODE_8BIT;
1322 return GAMMA_MODE_MODE_10BIT;
1325 static int glk_color_check(struct intel_crtc_state *crtc_state)
1329 ret = check_luts(crtc_state);
1333 crtc_state->gamma_enable =
1334 crtc_state->base.gamma_lut &&
1335 !crtc_state->c8_planes;
1337 /* On GLK+ degamma LUT is controlled by csc_enable */
1338 crtc_state->csc_enable =
1339 crtc_state->base.degamma_lut ||
1340 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
1341 crtc_state->base.ctm || crtc_state->limited_color_range;
1343 crtc_state->gamma_mode = glk_gamma_mode(crtc_state);
1345 crtc_state->csc_mode = 0;
1347 ret = intel_color_add_affected_planes(crtc_state);
1354 static u32 icl_gamma_mode(const struct intel_crtc_state *crtc_state)
1358 if (crtc_state->base.degamma_lut)
1359 gamma_mode |= PRE_CSC_GAMMA_ENABLE;
1361 if (crtc_state->base.gamma_lut &&
1362 !crtc_state->c8_planes)
1363 gamma_mode |= POST_CSC_GAMMA_ENABLE;
1365 if (!crtc_state->base.gamma_lut ||
1366 crtc_state_is_legacy_gamma(crtc_state))
1367 gamma_mode |= GAMMA_MODE_MODE_8BIT;
1369 gamma_mode |= GAMMA_MODE_MODE_12BIT_MULTI_SEGMENTED;
1374 static u32 icl_csc_mode(const struct intel_crtc_state *crtc_state)
1378 if (crtc_state->base.ctm)
1379 csc_mode |= ICL_CSC_ENABLE;
1381 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
1382 crtc_state->limited_color_range)
1383 csc_mode |= ICL_OUTPUT_CSC_ENABLE;
1388 static int icl_color_check(struct intel_crtc_state *crtc_state)
1392 ret = check_luts(crtc_state);
1396 crtc_state->gamma_mode = icl_gamma_mode(crtc_state);
1398 crtc_state->csc_mode = icl_csc_mode(crtc_state);
1403 static int i9xx_gamma_precision(const struct intel_crtc_state *crtc_state)
1405 switch (crtc_state->gamma_mode) {
1406 case GAMMA_MODE_MODE_8BIT:
1408 case GAMMA_MODE_MODE_10BIT:
1411 MISSING_CASE(crtc_state->gamma_mode);
1416 static int ilk_gamma_precision(const struct intel_crtc_state *crtc_state)
1418 if ((crtc_state->csc_mode & CSC_POSITION_BEFORE_GAMMA) == 0)
1421 switch (crtc_state->gamma_mode) {
1422 case GAMMA_MODE_MODE_8BIT:
1424 case GAMMA_MODE_MODE_10BIT:
1427 MISSING_CASE(crtc_state->gamma_mode);
1432 static int chv_gamma_precision(const struct intel_crtc_state *crtc_state)
1434 if (crtc_state->cgm_mode & CGM_PIPE_MODE_GAMMA)
1437 return i9xx_gamma_precision(crtc_state);
1440 static int glk_gamma_precision(const struct intel_crtc_state *crtc_state)
1442 switch (crtc_state->gamma_mode) {
1443 case GAMMA_MODE_MODE_8BIT:
1445 case GAMMA_MODE_MODE_10BIT:
1448 MISSING_CASE(crtc_state->gamma_mode);
1453 int intel_color_get_gamma_bit_precision(const struct intel_crtc_state *crtc_state)
1455 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1456 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1458 if (!crtc_state->gamma_enable)
1461 if (HAS_GMCH(dev_priv)) {
1462 if (IS_CHERRYVIEW(dev_priv))
1463 return chv_gamma_precision(crtc_state);
1465 return i9xx_gamma_precision(crtc_state);
1467 if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
1468 return glk_gamma_precision(crtc_state);
1469 else if (IS_IRONLAKE(dev_priv))
1470 return ilk_gamma_precision(crtc_state);
1476 static bool err_check(struct drm_color_lut *lut1,
1477 struct drm_color_lut *lut2, u32 err)
1479 return ((abs((long)lut2->red - lut1->red)) <= err) &&
1480 ((abs((long)lut2->blue - lut1->blue)) <= err) &&
1481 ((abs((long)lut2->green - lut1->green)) <= err);
1484 static bool intel_color_lut_entry_equal(struct drm_color_lut *lut1,
1485 struct drm_color_lut *lut2,
1486 int lut_size, u32 err)
1490 for (i = 0; i < lut_size; i++) {
1491 if (!err_check(&lut1[i], &lut2[i], err))
1498 bool intel_color_lut_equal(struct drm_property_blob *blob1,
1499 struct drm_property_blob *blob2,
1500 u32 gamma_mode, u32 bit_precision)
1502 struct drm_color_lut *lut1, *lut2;
1503 int lut_size1, lut_size2;
1506 if (!blob1 != !blob2)
1512 lut_size1 = drm_color_lut_size(blob1);
1513 lut_size2 = drm_color_lut_size(blob2);
1515 /* check sw and hw lut size */
1516 switch (gamma_mode) {
1517 case GAMMA_MODE_MODE_8BIT:
1518 case GAMMA_MODE_MODE_10BIT:
1519 if (lut_size1 != lut_size2)
1523 MISSING_CASE(gamma_mode);
1530 err = 0xffff >> bit_precision;
1532 /* check sw and hw lut entry to be equal */
1533 switch (gamma_mode) {
1534 case GAMMA_MODE_MODE_8BIT:
1535 case GAMMA_MODE_MODE_10BIT:
1536 if (!intel_color_lut_entry_equal(lut1, lut2,
1541 MISSING_CASE(gamma_mode);
1548 /* convert hw value with given bit_precision to lut property val */
1549 static u32 intel_color_lut_pack(u32 val, u32 bit_precision)
1551 u32 max = 0xffff >> (16 - bit_precision);
1553 val = clamp_val(val, 0, max);
1555 if (bit_precision < 16)
1556 val <<= 16 - bit_precision;
1561 static struct drm_property_blob *
1562 i9xx_read_lut_8(const struct intel_crtc_state *crtc_state)
1564 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1565 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1566 enum pipe pipe = crtc->pipe;
1567 struct drm_property_blob *blob;
1568 struct drm_color_lut *blob_data;
1571 blob = drm_property_create_blob(&dev_priv->drm,
1572 sizeof(struct drm_color_lut) * LEGACY_LUT_LENGTH,
1577 blob_data = blob->data;
1579 for (i = 0; i < LEGACY_LUT_LENGTH; i++) {
1580 if (HAS_GMCH(dev_priv))
1581 val = I915_READ(PALETTE(pipe, i));
1583 val = I915_READ(LGC_PALETTE(pipe, i));
1585 blob_data[i].red = intel_color_lut_pack(REG_FIELD_GET(
1586 LGC_PALETTE_RED_MASK, val), 8);
1587 blob_data[i].green = intel_color_lut_pack(REG_FIELD_GET(
1588 LGC_PALETTE_GREEN_MASK, val), 8);
1589 blob_data[i].blue = intel_color_lut_pack(REG_FIELD_GET(
1590 LGC_PALETTE_BLUE_MASK, val), 8);
1596 static void i9xx_read_luts(struct intel_crtc_state *crtc_state)
1598 crtc_state->base.gamma_lut = i9xx_read_lut_8(crtc_state);
1601 static struct drm_property_blob *
1602 i965_read_lut_10p6(const struct intel_crtc_state *crtc_state)
1604 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1605 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1606 u32 lut_size = INTEL_INFO(dev_priv)->color.gamma_lut_size;
1607 enum pipe pipe = crtc->pipe;
1608 struct drm_property_blob *blob;
1609 struct drm_color_lut *blob_data;
1612 blob = drm_property_create_blob(&dev_priv->drm,
1613 sizeof(struct drm_color_lut) * lut_size,
1618 blob_data = blob->data;
1620 for (i = 0; i < lut_size - 1; i++) {
1621 val1 = I915_READ(PALETTE(pipe, 2 * i + 0));
1622 val2 = I915_READ(PALETTE(pipe, 2 * i + 1));
1624 blob_data[i].red = REG_FIELD_GET(PALETTE_RED_MASK, val2) << 8 |
1625 REG_FIELD_GET(PALETTE_RED_MASK, val1);
1626 blob_data[i].green = REG_FIELD_GET(PALETTE_GREEN_MASK, val2) << 8 |
1627 REG_FIELD_GET(PALETTE_GREEN_MASK, val1);
1628 blob_data[i].blue = REG_FIELD_GET(PALETTE_BLUE_MASK, val2) << 8 |
1629 REG_FIELD_GET(PALETTE_BLUE_MASK, val1);
1632 blob_data[i].red = REG_FIELD_GET(PIPEGCMAX_RGB_MASK,
1633 I915_READ(PIPEGCMAX(pipe, 0)));
1634 blob_data[i].green = REG_FIELD_GET(PIPEGCMAX_RGB_MASK,
1635 I915_READ(PIPEGCMAX(pipe, 1)));
1636 blob_data[i].blue = REG_FIELD_GET(PIPEGCMAX_RGB_MASK,
1637 I915_READ(PIPEGCMAX(pipe, 2)));
1642 static void i965_read_luts(struct intel_crtc_state *crtc_state)
1644 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT)
1645 crtc_state->base.gamma_lut = i9xx_read_lut_8(crtc_state);
1647 crtc_state->base.gamma_lut = i965_read_lut_10p6(crtc_state);
1650 static struct drm_property_blob *
1651 chv_read_cgm_lut(const struct intel_crtc_state *crtc_state)
1653 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1654 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1655 u32 lut_size = INTEL_INFO(dev_priv)->color.gamma_lut_size;
1656 enum pipe pipe = crtc->pipe;
1657 struct drm_property_blob *blob;
1658 struct drm_color_lut *blob_data;
1661 blob = drm_property_create_blob(&dev_priv->drm,
1662 sizeof(struct drm_color_lut) * lut_size,
1667 blob_data = blob->data;
1669 for (i = 0; i < lut_size; i++) {
1670 val = I915_READ(CGM_PIPE_GAMMA(pipe, i, 0));
1671 blob_data[i].green = intel_color_lut_pack(REG_FIELD_GET(
1672 CGM_PIPE_GAMMA_GREEN_MASK, val), 10);
1673 blob_data[i].blue = intel_color_lut_pack(REG_FIELD_GET(
1674 CGM_PIPE_GAMMA_BLUE_MASK, val), 10);
1676 val = I915_READ(CGM_PIPE_GAMMA(pipe, i, 1));
1677 blob_data[i].red = intel_color_lut_pack(REG_FIELD_GET(
1678 CGM_PIPE_GAMMA_RED_MASK, val), 10);
1684 static void chv_read_luts(struct intel_crtc_state *crtc_state)
1686 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT)
1687 crtc_state->base.gamma_lut = i9xx_read_lut_8(crtc_state);
1689 crtc_state->base.gamma_lut = chv_read_cgm_lut(crtc_state);
1692 static struct drm_property_blob *
1693 ilk_read_lut_10(const struct intel_crtc_state *crtc_state)
1695 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1696 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1697 u32 lut_size = INTEL_INFO(dev_priv)->color.gamma_lut_size;
1698 enum pipe pipe = crtc->pipe;
1699 struct drm_property_blob *blob;
1700 struct drm_color_lut *blob_data;
1703 blob = drm_property_create_blob(&dev_priv->drm,
1704 sizeof(struct drm_color_lut) * lut_size,
1709 blob_data = blob->data;
1711 for (i = 0; i < lut_size; i++) {
1712 val = I915_READ(PREC_PALETTE(pipe, i));
1714 blob_data[i].red = intel_color_lut_pack(REG_FIELD_GET(
1715 PREC_PALETTE_RED_MASK, val), 10);
1716 blob_data[i].green = intel_color_lut_pack(REG_FIELD_GET(
1717 PREC_PALETTE_GREEN_MASK, val), 10);
1718 blob_data[i].blue = intel_color_lut_pack(REG_FIELD_GET(
1719 PREC_PALETTE_BLUE_MASK, val), 10);
1725 static void ilk_read_luts(struct intel_crtc_state *crtc_state)
1727 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT)
1728 crtc_state->base.gamma_lut = i9xx_read_lut_8(crtc_state);
1730 crtc_state->base.gamma_lut = ilk_read_lut_10(crtc_state);
1733 static struct drm_property_blob *
1734 glk_read_lut_10(const struct intel_crtc_state *crtc_state, u32 prec_index)
1736 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
1737 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1738 int hw_lut_size = ivb_lut_10_size(prec_index);
1739 enum pipe pipe = crtc->pipe;
1740 struct drm_property_blob *blob;
1741 struct drm_color_lut *blob_data;
1744 blob = drm_property_create_blob(&dev_priv->drm,
1745 sizeof(struct drm_color_lut) * hw_lut_size,
1750 blob_data = blob->data;
1752 I915_WRITE(PREC_PAL_INDEX(pipe), prec_index |
1753 PAL_PREC_AUTO_INCREMENT);
1755 for (i = 0; i < hw_lut_size; i++) {
1756 val = I915_READ(PREC_PAL_DATA(pipe));
1758 blob_data[i].red = intel_color_lut_pack(REG_FIELD_GET(
1759 PREC_PAL_DATA_RED_MASK, val), 10);
1760 blob_data[i].green = intel_color_lut_pack(REG_FIELD_GET(
1761 PREC_PAL_DATA_GREEN_MASK, val), 10);
1762 blob_data[i].blue = intel_color_lut_pack(REG_FIELD_GET(
1763 PREC_PAL_DATA_BLUE_MASK, val), 10);
1766 I915_WRITE(PREC_PAL_INDEX(pipe), 0);
1771 static void glk_read_luts(struct intel_crtc_state *crtc_state)
1773 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT)
1774 crtc_state->base.gamma_lut = i9xx_read_lut_8(crtc_state);
1776 crtc_state->base.gamma_lut = glk_read_lut_10(crtc_state, PAL_PREC_INDEX_VALUE(0));
1779 void intel_color_init(struct intel_crtc *crtc)
1781 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1782 bool has_ctm = INTEL_INFO(dev_priv)->color.degamma_lut_size != 0;
1784 drm_mode_crtc_set_gamma_size(&crtc->base, 256);
1786 if (HAS_GMCH(dev_priv)) {
1787 if (IS_CHERRYVIEW(dev_priv)) {
1788 dev_priv->display.color_check = chv_color_check;
1789 dev_priv->display.color_commit = i9xx_color_commit;
1790 dev_priv->display.load_luts = chv_load_luts;
1791 dev_priv->display.read_luts = chv_read_luts;
1792 } else if (INTEL_GEN(dev_priv) >= 4) {
1793 dev_priv->display.color_check = i9xx_color_check;
1794 dev_priv->display.color_commit = i9xx_color_commit;
1795 dev_priv->display.load_luts = i965_load_luts;
1796 dev_priv->display.read_luts = i965_read_luts;
1798 dev_priv->display.color_check = i9xx_color_check;
1799 dev_priv->display.color_commit = i9xx_color_commit;
1800 dev_priv->display.load_luts = i9xx_load_luts;
1801 dev_priv->display.read_luts = i9xx_read_luts;
1804 if (INTEL_GEN(dev_priv) >= 11)
1805 dev_priv->display.color_check = icl_color_check;
1806 else if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
1807 dev_priv->display.color_check = glk_color_check;
1808 else if (INTEL_GEN(dev_priv) >= 7)
1809 dev_priv->display.color_check = ivb_color_check;
1811 dev_priv->display.color_check = ilk_color_check;
1813 if (INTEL_GEN(dev_priv) >= 9)
1814 dev_priv->display.color_commit = skl_color_commit;
1815 else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
1816 dev_priv->display.color_commit = hsw_color_commit;
1818 dev_priv->display.color_commit = ilk_color_commit;
1820 if (INTEL_GEN(dev_priv) >= 11) {
1821 dev_priv->display.load_luts = icl_load_luts;
1822 } else if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv)) {
1823 dev_priv->display.load_luts = glk_load_luts;
1824 dev_priv->display.read_luts = glk_read_luts;
1825 } else if (INTEL_GEN(dev_priv) >= 8) {
1826 dev_priv->display.load_luts = bdw_load_luts;
1827 } else if (INTEL_GEN(dev_priv) >= 7) {
1828 dev_priv->display.load_luts = ivb_load_luts;
1830 dev_priv->display.load_luts = ilk_load_luts;
1831 dev_priv->display.read_luts = ilk_read_luts;
1835 drm_crtc_enable_color_mgmt(&crtc->base,
1836 INTEL_INFO(dev_priv)->color.degamma_lut_size,
1838 INTEL_INFO(dev_priv)->color.gamma_lut_size);