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_drv.h"
27 #define CTM_COEFF_SIGN (1ULL << 63)
29 #define CTM_COEFF_1_0 (1ULL << 32)
30 #define CTM_COEFF_2_0 (CTM_COEFF_1_0 << 1)
31 #define CTM_COEFF_4_0 (CTM_COEFF_2_0 << 1)
32 #define CTM_COEFF_8_0 (CTM_COEFF_4_0 << 1)
33 #define CTM_COEFF_0_5 (CTM_COEFF_1_0 >> 1)
34 #define CTM_COEFF_0_25 (CTM_COEFF_0_5 >> 1)
35 #define CTM_COEFF_0_125 (CTM_COEFF_0_25 >> 1)
37 #define CTM_COEFF_LIMITED_RANGE ((235ULL - 16ULL) * CTM_COEFF_1_0 / 255)
39 #define CTM_COEFF_NEGATIVE(coeff) (((coeff) & CTM_COEFF_SIGN) != 0)
40 #define CTM_COEFF_ABS(coeff) ((coeff) & (CTM_COEFF_SIGN - 1))
42 #define LEGACY_LUT_LENGTH 256
44 * Extract the CSC coefficient from a CTM coefficient (in U32.32 fixed point
45 * format). This macro takes the coefficient we want transformed and the
46 * number of fractional bits.
48 * We only have a 9 bits precision window which slides depending on the value
49 * of the CTM coefficient and we write the value from bit 3. We also round the
52 #define ILK_CSC_COEFF_FP(coeff, fbits) \
53 (clamp_val(((coeff) >> (32 - (fbits) - 3)) + 4, 0, 0xfff) & 0xff8)
55 #define ILK_CSC_COEFF_LIMITED_RANGE 0x0dc0
56 #define ILK_CSC_COEFF_1_0 0x7800
58 #define ILK_CSC_POSTOFF_LIMITED_RANGE (16 * (1 << 12) / 255)
60 static const u16 ilk_csc_off_zero[3] = {};
62 static const u16 ilk_csc_coeff_identity[9] = {
63 ILK_CSC_COEFF_1_0, 0, 0,
64 0, ILK_CSC_COEFF_1_0, 0,
65 0, 0, ILK_CSC_COEFF_1_0,
68 static const u16 ilk_csc_postoff_limited_range[3] = {
69 ILK_CSC_POSTOFF_LIMITED_RANGE,
70 ILK_CSC_POSTOFF_LIMITED_RANGE,
71 ILK_CSC_POSTOFF_LIMITED_RANGE,
74 static const u16 ilk_csc_coeff_limited_range[9] = {
75 ILK_CSC_COEFF_LIMITED_RANGE, 0, 0,
76 0, ILK_CSC_COEFF_LIMITED_RANGE, 0,
77 0, 0, ILK_CSC_COEFF_LIMITED_RANGE,
81 * These values are direct register values specified in the Bspec,
82 * for RGB->YUV conversion matrix (colorspace BT709)
84 static const u16 ilk_csc_coeff_rgb_to_ycbcr[9] = {
85 0x1e08, 0x9cc0, 0xb528,
86 0x2ba8, 0x09d8, 0x37e8,
87 0xbce8, 0x9ad8, 0x1e08,
90 /* Post offset values for RGB->YCBCR conversion */
91 static const u16 ilk_csc_postoff_rgb_to_ycbcr[3] = {
92 0x0800, 0x0100, 0x0800,
95 static bool lut_is_legacy(const struct drm_property_blob *lut)
97 return drm_color_lut_size(lut) == LEGACY_LUT_LENGTH;
100 static bool crtc_state_is_legacy_gamma(const struct intel_crtc_state *crtc_state)
102 return !crtc_state->base.degamma_lut &&
103 !crtc_state->base.ctm &&
104 crtc_state->base.gamma_lut &&
105 lut_is_legacy(crtc_state->base.gamma_lut);
109 * When using limited range, multiply the matrix given by userspace by
110 * the matrix that we would use for the limited range.
112 static u64 *ctm_mult_by_limited(u64 *result, const u64 *input)
116 for (i = 0; i < 9; i++) {
117 u64 user_coeff = input[i];
118 u32 limited_coeff = CTM_COEFF_LIMITED_RANGE;
119 u32 abs_coeff = clamp_val(CTM_COEFF_ABS(user_coeff), 0,
120 CTM_COEFF_4_0 - 1) >> 2;
123 * By scaling every co-efficient with limited range (16-235)
124 * vs full range (0-255) the final o/p will be scaled down to
125 * fit in the limited range supported by the panel.
127 result[i] = mul_u32_u32(limited_coeff, abs_coeff) >> 30;
128 result[i] |= user_coeff & CTM_COEFF_SIGN;
134 static void ilk_update_pipe_csc(struct intel_crtc *crtc,
137 const u16 postoff[3])
139 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
140 enum pipe pipe = crtc->pipe;
142 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), preoff[0]);
143 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), preoff[1]);
144 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), preoff[2]);
146 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff[0] << 16 | coeff[1]);
147 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), coeff[2] << 16);
149 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff[3] << 16 | coeff[4]);
150 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), coeff[5] << 16);
152 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), coeff[6] << 16 | coeff[7]);
153 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff[8] << 16);
155 if (INTEL_GEN(dev_priv) >= 7) {
156 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff[0]);
157 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff[1]);
158 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff[2]);
162 static void icl_update_output_csc(struct intel_crtc *crtc,
165 const u16 postoff[3])
167 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
168 enum pipe pipe = crtc->pipe;
170 I915_WRITE(PIPE_CSC_OUTPUT_PREOFF_HI(pipe), preoff[0]);
171 I915_WRITE(PIPE_CSC_OUTPUT_PREOFF_ME(pipe), preoff[1]);
172 I915_WRITE(PIPE_CSC_OUTPUT_PREOFF_LO(pipe), preoff[2]);
174 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_RY_GY(pipe), coeff[0] << 16 | coeff[1]);
175 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_BY(pipe), coeff[2]);
177 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_RU_GU(pipe), coeff[3] << 16 | coeff[4]);
178 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_BU(pipe), coeff[5]);
180 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_RV_GV(pipe), coeff[6] << 16 | coeff[7]);
181 I915_WRITE(PIPE_CSC_OUTPUT_COEFF_BV(pipe), coeff[8]);
183 I915_WRITE(PIPE_CSC_OUTPUT_POSTOFF_HI(pipe), postoff[0]);
184 I915_WRITE(PIPE_CSC_OUTPUT_POSTOFF_ME(pipe), postoff[1]);
185 I915_WRITE(PIPE_CSC_OUTPUT_POSTOFF_LO(pipe), postoff[2]);
188 static bool ilk_csc_limited_range(const struct intel_crtc_state *crtc_state)
190 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
193 * FIXME if there's a gamma LUT after the CSC, we should
194 * do the range compression using the gamma LUT instead.
196 return crtc_state->limited_color_range &&
197 (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv) ||
198 IS_GEN_RANGE(dev_priv, 9, 10));
201 static void ilk_csc_convert_ctm(const struct intel_crtc_state *crtc_state,
204 const struct drm_color_ctm *ctm = crtc_state->base.ctm->data;
209 if (ilk_csc_limited_range(crtc_state))
210 input = ctm_mult_by_limited(temp, ctm->matrix);
215 * Convert fixed point S31.32 input to format supported by the
218 for (i = 0; i < 9; i++) {
219 u64 abs_coeff = ((1ULL << 63) - 1) & input[i];
222 * Clamp input value to min/max supported by
225 abs_coeff = clamp_val(abs_coeff, 0, CTM_COEFF_4_0 - 1);
230 if (CTM_COEFF_NEGATIVE(input[i]))
231 coeffs[i] |= 1 << 15;
233 if (abs_coeff < CTM_COEFF_0_125)
234 coeffs[i] |= (3 << 12) |
235 ILK_CSC_COEFF_FP(abs_coeff, 12);
236 else if (abs_coeff < CTM_COEFF_0_25)
237 coeffs[i] |= (2 << 12) |
238 ILK_CSC_COEFF_FP(abs_coeff, 11);
239 else if (abs_coeff < CTM_COEFF_0_5)
240 coeffs[i] |= (1 << 12) |
241 ILK_CSC_COEFF_FP(abs_coeff, 10);
242 else if (abs_coeff < CTM_COEFF_1_0)
243 coeffs[i] |= ILK_CSC_COEFF_FP(abs_coeff, 9);
244 else if (abs_coeff < CTM_COEFF_2_0)
245 coeffs[i] |= (7 << 12) |
246 ILK_CSC_COEFF_FP(abs_coeff, 8);
248 coeffs[i] |= (6 << 12) |
249 ILK_CSC_COEFF_FP(abs_coeff, 7);
253 static void ilk_load_csc_matrix(const struct intel_crtc_state *crtc_state)
255 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
256 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
257 bool limited_color_range = ilk_csc_limited_range(crtc_state);
259 if (crtc_state->base.ctm) {
262 ilk_csc_convert_ctm(crtc_state, coeff);
263 ilk_update_pipe_csc(crtc, ilk_csc_off_zero, coeff,
264 limited_color_range ?
265 ilk_csc_postoff_limited_range :
267 } else if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB) {
268 ilk_update_pipe_csc(crtc, ilk_csc_off_zero,
269 ilk_csc_coeff_rgb_to_ycbcr,
270 ilk_csc_postoff_rgb_to_ycbcr);
271 } else if (limited_color_range) {
272 ilk_update_pipe_csc(crtc, ilk_csc_off_zero,
273 ilk_csc_coeff_limited_range,
274 ilk_csc_postoff_limited_range);
275 } else if (crtc_state->csc_enable) {
277 * On GLK+ both pipe CSC and degamma LUT are controlled
278 * by csc_enable. Hence for the cases where the degama
279 * LUT is needed but CSC is not we need to load an
282 WARN_ON(!IS_CANNONLAKE(dev_priv) && !IS_GEMINILAKE(dev_priv));
284 ilk_update_pipe_csc(crtc, ilk_csc_off_zero,
285 ilk_csc_coeff_identity,
289 I915_WRITE(PIPE_CSC_MODE(crtc->pipe), crtc_state->csc_mode);
292 static void icl_load_csc_matrix(const struct intel_crtc_state *crtc_state)
294 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
295 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
297 if (crtc_state->base.ctm) {
300 ilk_csc_convert_ctm(crtc_state, coeff);
301 ilk_update_pipe_csc(crtc, ilk_csc_off_zero,
302 coeff, ilk_csc_off_zero);
305 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB) {
306 icl_update_output_csc(crtc, ilk_csc_off_zero,
307 ilk_csc_coeff_rgb_to_ycbcr,
308 ilk_csc_postoff_rgb_to_ycbcr);
309 } else if (crtc_state->limited_color_range) {
310 icl_update_output_csc(crtc, ilk_csc_off_zero,
311 ilk_csc_coeff_limited_range,
312 ilk_csc_postoff_limited_range);
315 I915_WRITE(PIPE_CSC_MODE(crtc->pipe), crtc_state->csc_mode);
319 * Set up the pipe CSC unit on CherryView.
321 static void cherryview_load_csc_matrix(const struct intel_crtc_state *crtc_state)
323 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
324 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
325 enum pipe pipe = crtc->pipe;
327 if (crtc_state->base.ctm) {
328 const struct drm_color_ctm *ctm = crtc_state->base.ctm->data;
332 for (i = 0; i < ARRAY_SIZE(coeffs); i++) {
334 ((1ULL << 63) - 1) & ctm->matrix[i];
336 /* Round coefficient. */
337 abs_coeff += 1 << (32 - 13);
338 /* Clamp to hardware limits. */
339 abs_coeff = clamp_val(abs_coeff, 0, CTM_COEFF_8_0 - 1);
341 /* Write coefficients in S3.12 format. */
342 if (ctm->matrix[i] & (1ULL << 63))
344 coeffs[i] |= ((abs_coeff >> 32) & 7) << 12;
345 coeffs[i] |= (abs_coeff >> 20) & 0xfff;
348 I915_WRITE(CGM_PIPE_CSC_COEFF01(pipe),
349 coeffs[1] << 16 | coeffs[0]);
350 I915_WRITE(CGM_PIPE_CSC_COEFF23(pipe),
351 coeffs[3] << 16 | coeffs[2]);
352 I915_WRITE(CGM_PIPE_CSC_COEFF45(pipe),
353 coeffs[5] << 16 | coeffs[4]);
354 I915_WRITE(CGM_PIPE_CSC_COEFF67(pipe),
355 coeffs[7] << 16 | coeffs[6]);
356 I915_WRITE(CGM_PIPE_CSC_COEFF8(pipe), coeffs[8]);
359 I915_WRITE(CGM_PIPE_MODE(pipe), crtc_state->cgm_mode);
362 static u32 ilk_lut_10(const struct drm_color_lut *color)
364 return drm_color_lut_extract(color->red, 10) << 20 |
365 drm_color_lut_extract(color->green, 10) << 10 |
366 drm_color_lut_extract(color->blue, 10);
369 /* Loads the legacy palette/gamma unit for the CRTC. */
370 static void i9xx_load_luts_internal(const struct intel_crtc_state *crtc_state,
371 const struct drm_property_blob *blob)
373 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
374 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
375 enum pipe pipe = crtc->pipe;
378 if (HAS_GMCH(dev_priv)) {
379 if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI))
380 assert_dsi_pll_enabled(dev_priv);
382 assert_pll_enabled(dev_priv, pipe);
386 const struct drm_color_lut *lut = blob->data;
388 for (i = 0; i < 256; i++) {
390 (drm_color_lut_extract(lut[i].red, 8) << 16) |
391 (drm_color_lut_extract(lut[i].green, 8) << 8) |
392 drm_color_lut_extract(lut[i].blue, 8);
394 if (HAS_GMCH(dev_priv))
395 I915_WRITE(PALETTE(pipe, i), word);
397 I915_WRITE(LGC_PALETTE(pipe, i), word);
402 static void i9xx_load_luts(const struct intel_crtc_state *crtc_state)
404 i9xx_load_luts_internal(crtc_state, crtc_state->base.gamma_lut);
407 static void i9xx_color_commit(const struct intel_crtc_state *crtc_state)
409 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
410 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
411 enum pipe pipe = crtc->pipe;
414 val = I915_READ(PIPECONF(pipe));
415 val &= ~PIPECONF_GAMMA_MODE_MASK_I9XX;
416 val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
417 I915_WRITE(PIPECONF(pipe), val);
420 static void ilk_color_commit(const struct intel_crtc_state *crtc_state)
422 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
423 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
424 enum pipe pipe = crtc->pipe;
427 val = I915_READ(PIPECONF(pipe));
428 val &= ~PIPECONF_GAMMA_MODE_MASK_ILK;
429 val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
430 I915_WRITE(PIPECONF(pipe), val);
432 ilk_load_csc_matrix(crtc_state);
435 static void hsw_color_commit(const struct intel_crtc_state *crtc_state)
437 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
438 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
440 I915_WRITE(GAMMA_MODE(crtc->pipe), crtc_state->gamma_mode);
442 ilk_load_csc_matrix(crtc_state);
445 static void skl_color_commit(const struct intel_crtc_state *crtc_state)
447 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
448 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
449 enum pipe pipe = crtc->pipe;
453 * We don't (yet) allow userspace to control the pipe background color,
454 * so force it to black, but apply pipe gamma and CSC appropriately
455 * so that its handling will match how we program our planes.
457 if (crtc_state->gamma_enable)
458 val |= SKL_BOTTOM_COLOR_GAMMA_ENABLE;
459 if (crtc_state->csc_enable)
460 val |= SKL_BOTTOM_COLOR_CSC_ENABLE;
461 I915_WRITE(SKL_BOTTOM_COLOR(pipe), val);
463 I915_WRITE(GAMMA_MODE(crtc->pipe), crtc_state->gamma_mode);
465 if (INTEL_GEN(dev_priv) >= 11)
466 icl_load_csc_matrix(crtc_state);
468 ilk_load_csc_matrix(crtc_state);
471 static void ilk_load_lut_10(struct intel_crtc *crtc,
472 const struct drm_property_blob *blob)
474 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
475 const struct drm_color_lut *lut = blob->data;
476 int i, lut_size = drm_color_lut_size(blob);
477 enum pipe pipe = crtc->pipe;
479 for (i = 0; i < lut_size; i++)
480 I915_WRITE(PREC_PALETTE(pipe, i), ilk_lut_10(&lut[i]));
483 static void ilk_load_luts(const struct intel_crtc_state *crtc_state)
485 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
486 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
488 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT)
489 i9xx_load_luts(crtc_state);
491 ilk_load_lut_10(crtc, gamma_lut);
495 * IVB/HSW Bspec / PAL_PREC_INDEX:
496 * "Restriction : Index auto increment mode is not
497 * supported and must not be enabled."
499 static void ivb_load_lut_10(struct intel_crtc *crtc,
500 const struct drm_property_blob *blob,
501 u32 prec_index, bool duplicate)
503 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
504 const struct drm_color_lut *lut = blob->data;
505 int i, lut_size = drm_color_lut_size(blob);
506 enum pipe pipe = crtc->pipe;
509 * We advertise the split gamma sizes. When not using split
510 * gamma we just duplicate each entry.
512 * TODO: expose the full LUT to userspace
515 for (i = 0; i < lut_size; i++) {
516 I915_WRITE(PREC_PAL_INDEX(pipe), prec_index++);
517 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_10(&lut[i]));
518 I915_WRITE(PREC_PAL_INDEX(pipe), prec_index++);
519 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_10(&lut[i]));
522 for (i = 0; i < lut_size; i++) {
523 I915_WRITE(PREC_PAL_INDEX(pipe), prec_index++);
524 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_10(&lut[i]));
529 * Reset the index, otherwise it prevents the legacy palette to be
532 I915_WRITE(PREC_PAL_INDEX(pipe), 0);
535 /* On BDW+ the index auto increment mode actually works */
536 static void bdw_load_lut_10(struct intel_crtc *crtc,
537 const struct drm_property_blob *blob,
538 u32 prec_index, bool duplicate)
540 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
541 const struct drm_color_lut *lut = blob->data;
542 int i, lut_size = drm_color_lut_size(blob);
543 enum pipe pipe = crtc->pipe;
545 I915_WRITE(PREC_PAL_INDEX(pipe), prec_index |
546 PAL_PREC_AUTO_INCREMENT);
549 * We advertise the split gamma sizes. When not using split
550 * gamma we just duplicate each entry.
552 * TODO: expose the full LUT to userspace
555 for (i = 0; i < lut_size; i++) {
556 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_10(&lut[i]));
557 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_10(&lut[i]));
560 for (i = 0; i < lut_size; i++)
561 I915_WRITE(PREC_PAL_DATA(pipe), ilk_lut_10(&lut[i]));
565 * Reset the index, otherwise it prevents the legacy palette to be
568 I915_WRITE(PREC_PAL_INDEX(pipe), 0);
571 static void ivb_load_lut_10_max(struct intel_crtc *crtc)
573 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
574 enum pipe pipe = crtc->pipe;
576 /* Program the max register to clamp values > 1.0. */
577 I915_WRITE(PREC_PAL_EXT_GC_MAX(pipe, 0), 1 << 16);
578 I915_WRITE(PREC_PAL_EXT_GC_MAX(pipe, 1), 1 << 16);
579 I915_WRITE(PREC_PAL_EXT_GC_MAX(pipe, 2), 1 << 16);
582 * Program the gc max 2 register to clamp values > 1.0.
583 * ToDo: Extend the ABI to be able to program values
586 if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv)) {
587 I915_WRITE(PREC_PAL_EXT2_GC_MAX(pipe, 0), 1 << 16);
588 I915_WRITE(PREC_PAL_EXT2_GC_MAX(pipe, 1), 1 << 16);
589 I915_WRITE(PREC_PAL_EXT2_GC_MAX(pipe, 2), 1 << 16);
593 static void ivb_load_luts(const struct intel_crtc_state *crtc_state)
595 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
596 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
597 const struct drm_property_blob *degamma_lut = crtc_state->base.degamma_lut;
599 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT) {
600 i9xx_load_luts(crtc_state);
601 } else if (crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT) {
602 ivb_load_lut_10(crtc, degamma_lut, PAL_PREC_SPLIT_MODE |
603 PAL_PREC_INDEX_VALUE(0), false);
604 ivb_load_lut_10_max(crtc);
605 ivb_load_lut_10(crtc, gamma_lut, PAL_PREC_SPLIT_MODE |
606 PAL_PREC_INDEX_VALUE(512), false);
608 const struct drm_property_blob *blob = gamma_lut ?: degamma_lut;
610 ivb_load_lut_10(crtc, blob,
611 PAL_PREC_INDEX_VALUE(0), true);
612 ivb_load_lut_10_max(crtc);
616 static void bdw_load_luts(const struct intel_crtc_state *crtc_state)
618 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
619 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
620 const struct drm_property_blob *degamma_lut = crtc_state->base.degamma_lut;
622 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT) {
623 i9xx_load_luts(crtc_state);
624 } else if (crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT) {
625 bdw_load_lut_10(crtc, degamma_lut, PAL_PREC_SPLIT_MODE |
626 PAL_PREC_INDEX_VALUE(0), false);
627 ivb_load_lut_10_max(crtc);
628 bdw_load_lut_10(crtc, gamma_lut, PAL_PREC_SPLIT_MODE |
629 PAL_PREC_INDEX_VALUE(512), false);
631 const struct drm_property_blob *blob = gamma_lut ?: degamma_lut;
633 bdw_load_lut_10(crtc, blob,
634 PAL_PREC_INDEX_VALUE(0), true);
635 ivb_load_lut_10_max(crtc);
639 static void glk_load_degamma_lut(const struct intel_crtc_state *crtc_state)
641 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
642 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
643 enum pipe pipe = crtc->pipe;
644 const u32 lut_size = INTEL_INFO(dev_priv)->color.degamma_lut_size;
645 const struct drm_color_lut *lut = crtc_state->base.degamma_lut->data;
649 * When setting the auto-increment bit, the hardware seems to
650 * ignore the index bits, so we need to reset it to index 0
653 I915_WRITE(PRE_CSC_GAMC_INDEX(pipe), 0);
654 I915_WRITE(PRE_CSC_GAMC_INDEX(pipe), PRE_CSC_GAMC_AUTO_INCREMENT);
656 for (i = 0; i < lut_size; i++) {
658 * First 33 entries represent range from 0 to 1.0
659 * 34th and 35th entry will represent extended range
660 * inputs 3.0 and 7.0 respectively, currently clamped
661 * at 1.0. Since the precision is 16bit, the user
662 * value can be directly filled to register.
663 * The pipe degamma table in GLK+ onwards doesn't
664 * support different values per channel, so this just
665 * programs green value which will be equal to Red and
666 * Blue into the lut registers.
667 * ToDo: Extend to max 7.0. Enable 32 bit input value
668 * as compared to just 16 to achieve this.
670 I915_WRITE(PRE_CSC_GAMC_DATA(pipe), lut[i].green);
673 /* Clamp values > 1.0. */
675 I915_WRITE(PRE_CSC_GAMC_DATA(pipe), 1 << 16);
678 static void glk_load_degamma_lut_linear(const struct intel_crtc_state *crtc_state)
680 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
681 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
682 enum pipe pipe = crtc->pipe;
683 const u32 lut_size = INTEL_INFO(dev_priv)->color.degamma_lut_size;
687 * When setting the auto-increment bit, the hardware seems to
688 * ignore the index bits, so we need to reset it to index 0
691 I915_WRITE(PRE_CSC_GAMC_INDEX(pipe), 0);
692 I915_WRITE(PRE_CSC_GAMC_INDEX(pipe), PRE_CSC_GAMC_AUTO_INCREMENT);
694 for (i = 0; i < lut_size; i++) {
695 u32 v = (i << 16) / (lut_size - 1);
697 I915_WRITE(PRE_CSC_GAMC_DATA(pipe), v);
700 /* Clamp values > 1.0. */
702 I915_WRITE(PRE_CSC_GAMC_DATA(pipe), 1 << 16);
705 static void glk_load_luts(const struct intel_crtc_state *crtc_state)
707 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
708 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
711 * On GLK+ both pipe CSC and degamma LUT are controlled
712 * by csc_enable. Hence for the cases where the CSC is
713 * needed but degamma LUT is not we need to load a
714 * linear degamma LUT. In fact we'll just always load
715 * the degama LUT so that we don't have to reload
716 * it every time the pipe CSC is being enabled.
718 if (crtc_state->base.degamma_lut)
719 glk_load_degamma_lut(crtc_state);
721 glk_load_degamma_lut_linear(crtc_state);
723 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_8BIT) {
724 i9xx_load_luts(crtc_state);
726 bdw_load_lut_10(crtc, gamma_lut, PAL_PREC_INDEX_VALUE(0), false);
727 ivb_load_lut_10_max(crtc);
731 static void icl_load_luts(const struct intel_crtc_state *crtc_state)
733 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
734 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
736 if (crtc_state->base.degamma_lut)
737 glk_load_degamma_lut(crtc_state);
739 if ((crtc_state->gamma_mode & GAMMA_MODE_MODE_MASK) ==
740 GAMMA_MODE_MODE_8BIT) {
741 i9xx_load_luts(crtc_state);
743 bdw_load_lut_10(crtc, gamma_lut, PAL_PREC_INDEX_VALUE(0), false);
744 ivb_load_lut_10_max(crtc);
748 static void cherryview_load_luts(const struct intel_crtc_state *crtc_state)
750 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
751 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
752 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
753 const struct drm_property_blob *degamma_lut = crtc_state->base.degamma_lut;
754 enum pipe pipe = crtc->pipe;
756 cherryview_load_csc_matrix(crtc_state);
758 if (crtc_state_is_legacy_gamma(crtc_state)) {
759 i9xx_load_luts(crtc_state);
764 const struct drm_color_lut *lut = degamma_lut->data;
765 int i, lut_size = INTEL_INFO(dev_priv)->color.degamma_lut_size;
767 for (i = 0; i < lut_size; i++) {
770 /* Write LUT in U0.14 format. */
772 (drm_color_lut_extract(lut[i].green, 14) << 16) |
773 drm_color_lut_extract(lut[i].blue, 14);
774 word1 = drm_color_lut_extract(lut[i].red, 14);
776 I915_WRITE(CGM_PIPE_DEGAMMA(pipe, i, 0), word0);
777 I915_WRITE(CGM_PIPE_DEGAMMA(pipe, i, 1), word1);
782 const struct drm_color_lut *lut = gamma_lut->data;
783 int i, lut_size = INTEL_INFO(dev_priv)->color.gamma_lut_size;
785 for (i = 0; i < lut_size; i++) {
788 /* Write LUT in U0.10 format. */
790 (drm_color_lut_extract(lut[i].green, 10) << 16) |
791 drm_color_lut_extract(lut[i].blue, 10);
792 word1 = drm_color_lut_extract(lut[i].red, 10);
794 I915_WRITE(CGM_PIPE_GAMMA(pipe, i, 0), word0);
795 I915_WRITE(CGM_PIPE_GAMMA(pipe, i, 1), word1);
800 void intel_color_load_luts(const struct intel_crtc_state *crtc_state)
802 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
804 dev_priv->display.load_luts(crtc_state);
807 void intel_color_commit(const struct intel_crtc_state *crtc_state)
809 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
811 dev_priv->display.color_commit(crtc_state);
814 int intel_color_check(struct intel_crtc_state *crtc_state)
816 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
818 return dev_priv->display.color_check(crtc_state);
821 static bool need_plane_update(struct intel_plane *plane,
822 const struct intel_crtc_state *crtc_state)
824 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
827 * On pre-SKL the pipe gamma enable and pipe csc enable for
828 * the pipe bottom color are configured via the primary plane.
829 * We have to reconfigure that even if the plane is inactive.
831 return crtc_state->active_planes & BIT(plane->id) ||
832 (INTEL_GEN(dev_priv) < 9 &&
833 plane->id == PLANE_PRIMARY);
837 intel_color_add_affected_planes(struct intel_crtc_state *new_crtc_state)
839 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->base.crtc);
840 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
841 struct intel_atomic_state *state =
842 to_intel_atomic_state(new_crtc_state->base.state);
843 const struct intel_crtc_state *old_crtc_state =
844 intel_atomic_get_old_crtc_state(state, crtc);
845 struct intel_plane *plane;
847 if (!new_crtc_state->base.active ||
848 drm_atomic_crtc_needs_modeset(&new_crtc_state->base))
851 if (new_crtc_state->gamma_enable == old_crtc_state->gamma_enable &&
852 new_crtc_state->csc_enable == old_crtc_state->csc_enable)
855 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
856 struct intel_plane_state *plane_state;
858 if (!need_plane_update(plane, new_crtc_state))
861 plane_state = intel_atomic_get_plane_state(state, plane);
862 if (IS_ERR(plane_state))
863 return PTR_ERR(plane_state);
865 new_crtc_state->update_planes |= BIT(plane->id);
871 static int check_lut_size(const struct drm_property_blob *lut, int expected)
878 len = drm_color_lut_size(lut);
879 if (len != expected) {
880 DRM_DEBUG_KMS("Invalid LUT size; got %d, expected %d\n",
888 static int check_luts(const struct intel_crtc_state *crtc_state)
890 struct drm_i915_private *dev_priv = to_i915(crtc_state->base.crtc->dev);
891 const struct drm_property_blob *gamma_lut = crtc_state->base.gamma_lut;
892 const struct drm_property_blob *degamma_lut = crtc_state->base.degamma_lut;
893 int gamma_length, degamma_length;
894 u32 gamma_tests, degamma_tests;
896 /* Always allow legacy gamma LUT with no further checking. */
897 if (crtc_state_is_legacy_gamma(crtc_state))
900 /* C8 relies on its palette being stored in the legacy LUT */
901 if (crtc_state->c8_planes)
904 degamma_length = INTEL_INFO(dev_priv)->color.degamma_lut_size;
905 gamma_length = INTEL_INFO(dev_priv)->color.gamma_lut_size;
906 degamma_tests = INTEL_INFO(dev_priv)->color.degamma_lut_tests;
907 gamma_tests = INTEL_INFO(dev_priv)->color.gamma_lut_tests;
909 if (check_lut_size(degamma_lut, degamma_length) ||
910 check_lut_size(gamma_lut, gamma_length))
913 if (drm_color_lut_check(degamma_lut, degamma_tests) ||
914 drm_color_lut_check(gamma_lut, gamma_tests))
920 static int i9xx_color_check(struct intel_crtc_state *crtc_state)
924 ret = check_luts(crtc_state);
928 crtc_state->gamma_enable =
929 crtc_state->base.gamma_lut &&
930 !crtc_state->c8_planes;
932 crtc_state->gamma_mode = GAMMA_MODE_MODE_8BIT;
934 ret = intel_color_add_affected_planes(crtc_state);
941 static u32 chv_cgm_mode(const struct intel_crtc_state *crtc_state)
945 if (crtc_state_is_legacy_gamma(crtc_state))
948 if (crtc_state->base.degamma_lut)
949 cgm_mode |= CGM_PIPE_MODE_DEGAMMA;
950 if (crtc_state->base.ctm)
951 cgm_mode |= CGM_PIPE_MODE_CSC;
952 if (crtc_state->base.gamma_lut)
953 cgm_mode |= CGM_PIPE_MODE_GAMMA;
959 * CHV color pipeline:
960 * u0.10 -> CGM degamma -> u0.14 -> CGM csc -> u0.14 -> CGM gamma ->
961 * u0.10 -> WGC csc -> u0.10 -> pipe gamma -> u0.10
963 * We always bypass the WGC csc and use the CGM csc
964 * instead since it has degamma and better precision.
966 static int chv_color_check(struct intel_crtc_state *crtc_state)
970 ret = check_luts(crtc_state);
975 * Pipe gamma will be used only for the legacy LUT.
976 * Otherwise we bypass it and use the CGM gamma instead.
978 crtc_state->gamma_enable =
979 crtc_state_is_legacy_gamma(crtc_state) &&
980 !crtc_state->c8_planes;
982 crtc_state->gamma_mode = GAMMA_MODE_MODE_8BIT;
984 crtc_state->cgm_mode = chv_cgm_mode(crtc_state);
986 ret = intel_color_add_affected_planes(crtc_state);
993 static u32 ilk_gamma_mode(const struct intel_crtc_state *crtc_state)
995 if (!crtc_state->gamma_enable ||
996 crtc_state_is_legacy_gamma(crtc_state))
997 return GAMMA_MODE_MODE_8BIT;
999 return GAMMA_MODE_MODE_10BIT;
1002 static int ilk_color_check(struct intel_crtc_state *crtc_state)
1006 ret = check_luts(crtc_state);
1010 crtc_state->gamma_enable =
1011 crtc_state->base.gamma_lut &&
1012 !crtc_state->c8_planes;
1015 * We don't expose the ctm on ilk/snb currently,
1016 * nor do we enable YCbCr output. Also RGB limited
1017 * range output is handled by the hw automagically.
1019 crtc_state->csc_enable = false;
1021 crtc_state->gamma_mode = ilk_gamma_mode(crtc_state);
1023 crtc_state->csc_mode = 0;
1025 ret = intel_color_add_affected_planes(crtc_state);
1032 static u32 ivb_gamma_mode(const struct intel_crtc_state *crtc_state)
1034 if (!crtc_state->gamma_enable ||
1035 crtc_state_is_legacy_gamma(crtc_state))
1036 return GAMMA_MODE_MODE_8BIT;
1037 else if (crtc_state->base.gamma_lut &&
1038 crtc_state->base.degamma_lut)
1039 return GAMMA_MODE_MODE_SPLIT;
1041 return GAMMA_MODE_MODE_10BIT;
1044 static u32 ivb_csc_mode(const struct intel_crtc_state *crtc_state)
1046 bool limited_color_range = ilk_csc_limited_range(crtc_state);
1049 * CSC comes after the LUT in degamma, RGB->YCbCr,
1050 * and RGB full->limited range mode.
1052 if (crtc_state->base.degamma_lut ||
1053 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
1054 limited_color_range)
1057 return CSC_POSITION_BEFORE_GAMMA;
1060 static int ivb_color_check(struct intel_crtc_state *crtc_state)
1062 bool limited_color_range = ilk_csc_limited_range(crtc_state);
1065 ret = check_luts(crtc_state);
1069 crtc_state->gamma_enable =
1070 (crtc_state->base.gamma_lut ||
1071 crtc_state->base.degamma_lut) &&
1072 !crtc_state->c8_planes;
1074 crtc_state->csc_enable =
1075 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
1076 crtc_state->base.ctm || limited_color_range;
1078 crtc_state->gamma_mode = ivb_gamma_mode(crtc_state);
1080 crtc_state->csc_mode = ivb_csc_mode(crtc_state);
1082 ret = intel_color_add_affected_planes(crtc_state);
1089 static u32 glk_gamma_mode(const struct intel_crtc_state *crtc_state)
1091 if (!crtc_state->gamma_enable ||
1092 crtc_state_is_legacy_gamma(crtc_state))
1093 return GAMMA_MODE_MODE_8BIT;
1095 return GAMMA_MODE_MODE_10BIT;
1098 static int glk_color_check(struct intel_crtc_state *crtc_state)
1102 ret = check_luts(crtc_state);
1106 crtc_state->gamma_enable =
1107 crtc_state->base.gamma_lut &&
1108 !crtc_state->c8_planes;
1110 /* On GLK+ degamma LUT is controlled by csc_enable */
1111 crtc_state->csc_enable =
1112 crtc_state->base.degamma_lut ||
1113 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
1114 crtc_state->base.ctm || crtc_state->limited_color_range;
1116 crtc_state->gamma_mode = glk_gamma_mode(crtc_state);
1118 crtc_state->csc_mode = 0;
1120 ret = intel_color_add_affected_planes(crtc_state);
1127 static u32 icl_gamma_mode(const struct intel_crtc_state *crtc_state)
1131 if (crtc_state->base.degamma_lut)
1132 gamma_mode |= PRE_CSC_GAMMA_ENABLE;
1134 if (crtc_state->base.gamma_lut &&
1135 !crtc_state->c8_planes)
1136 gamma_mode |= POST_CSC_GAMMA_ENABLE;
1138 if (!crtc_state->base.gamma_lut ||
1139 crtc_state_is_legacy_gamma(crtc_state))
1140 gamma_mode |= GAMMA_MODE_MODE_8BIT;
1142 gamma_mode |= GAMMA_MODE_MODE_10BIT;
1147 static u32 icl_csc_mode(const struct intel_crtc_state *crtc_state)
1151 if (crtc_state->base.ctm)
1152 csc_mode |= ICL_CSC_ENABLE;
1154 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
1155 crtc_state->limited_color_range)
1156 csc_mode |= ICL_OUTPUT_CSC_ENABLE;
1161 static int icl_color_check(struct intel_crtc_state *crtc_state)
1165 ret = check_luts(crtc_state);
1169 crtc_state->gamma_mode = icl_gamma_mode(crtc_state);
1171 crtc_state->csc_mode = icl_csc_mode(crtc_state);
1176 void intel_color_init(struct intel_crtc *crtc)
1178 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1179 bool has_ctm = INTEL_INFO(dev_priv)->color.degamma_lut_size != 0;
1181 drm_mode_crtc_set_gamma_size(&crtc->base, 256);
1183 if (HAS_GMCH(dev_priv)) {
1184 if (IS_CHERRYVIEW(dev_priv)) {
1185 dev_priv->display.color_check = chv_color_check;
1186 dev_priv->display.color_commit = i9xx_color_commit;
1187 dev_priv->display.load_luts = cherryview_load_luts;
1189 dev_priv->display.color_check = i9xx_color_check;
1190 dev_priv->display.color_commit = i9xx_color_commit;
1191 dev_priv->display.load_luts = i9xx_load_luts;
1194 if (INTEL_GEN(dev_priv) >= 11)
1195 dev_priv->display.color_check = icl_color_check;
1196 else if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
1197 dev_priv->display.color_check = glk_color_check;
1198 else if (INTEL_GEN(dev_priv) >= 7)
1199 dev_priv->display.color_check = ivb_color_check;
1201 dev_priv->display.color_check = ilk_color_check;
1203 if (INTEL_GEN(dev_priv) >= 9)
1204 dev_priv->display.color_commit = skl_color_commit;
1205 else if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
1206 dev_priv->display.color_commit = hsw_color_commit;
1208 dev_priv->display.color_commit = ilk_color_commit;
1210 if (INTEL_GEN(dev_priv) >= 11)
1211 dev_priv->display.load_luts = icl_load_luts;
1212 else if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv))
1213 dev_priv->display.load_luts = glk_load_luts;
1214 else if (INTEL_GEN(dev_priv) >= 8)
1215 dev_priv->display.load_luts = bdw_load_luts;
1216 else if (INTEL_GEN(dev_priv) >= 7)
1217 dev_priv->display.load_luts = ivb_load_luts;
1219 dev_priv->display.load_luts = ilk_load_luts;
1222 drm_crtc_enable_color_mgmt(&crtc->base,
1223 INTEL_INFO(dev_priv)->color.degamma_lut_size,
1225 INTEL_INFO(dev_priv)->color.gamma_lut_size);