1 /*
2 * Copyright © 2016 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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.
22 *
23 */
24
25 #include "i9xx_plane_regs.h"
26 #include "intel_color.h"
27 #include "intel_color_regs.h"
28 #include "intel_de.h"
29 #include "intel_display_types.h"
30 #include "intel_dsb.h"
31
32 struct intel_color_funcs {
33 int (*color_check)(struct intel_atomic_state *state,
34 struct intel_crtc *crtc);
35 /*
36 * Program non-arming double buffered color management registers
37 * before vblank evasion. The registers should then latch after
38 * the arming register is written (by color_commit_arm()) during
39 * the next vblank start, alongside any other double buffered
40 * registers involved with the same commit. This hook is optional.
41 */
42 void (*color_commit_noarm)(const struct intel_crtc_state *crtc_state);
43 /*
44 * Program arming double buffered color management registers
45 * during vblank evasion. The registers (and whatever other registers
46 * they arm that were written by color_commit_noarm) should then latch
47 * during the next vblank start, alongside any other double buffered
48 * registers involved with the same commit.
49 */
50 void (*color_commit_arm)(const struct intel_crtc_state *crtc_state);
51 /*
52 * Perform any extra tasks needed after all the
53 * double buffered registers have been latched.
54 */
55 void (*color_post_update)(const struct intel_crtc_state *crtc_state);
56 /*
57 * Load LUTs (and other single buffered color management
58 * registers). Will (hopefully) be called during the vblank
59 * following the latching of any double buffered registers
60 * involved with the same commit.
61 */
62 void (*load_luts)(const struct intel_crtc_state *crtc_state);
63 /*
64 * Read out the LUTs from the hardware into the software state.
65 * Used by eg. the hardware state checker.
66 */
67 void (*read_luts)(struct intel_crtc_state *crtc_state);
68 /*
69 * Compare the LUTs
70 */
71 bool (*lut_equal)(const struct intel_crtc_state *crtc_state,
72 const struct drm_property_blob *blob1,
73 const struct drm_property_blob *blob2,
74 bool is_pre_csc_lut);
75 /*
76 * Read out the CSCs (if any) from the hardware into the
77 * software state. Used by eg. the hardware state checker.
78 */
79 void (*read_csc)(struct intel_crtc_state *crtc_state);
80 /*
81 * Read config other than LUTs and CSCs, before them. Optional.
82 */
83 void (*get_config)(struct intel_crtc_state *crtc_state);
84 };
85
86 #define CTM_COEFF_SIGN (1ULL << 63)
87
88 #define CTM_COEFF_1_0 (1ULL << 32)
89 #define CTM_COEFF_2_0 (CTM_COEFF_1_0 << 1)
90 #define CTM_COEFF_4_0 (CTM_COEFF_2_0 << 1)
91 #define CTM_COEFF_8_0 (CTM_COEFF_4_0 << 1)
92 #define CTM_COEFF_0_5 (CTM_COEFF_1_0 >> 1)
93 #define CTM_COEFF_0_25 (CTM_COEFF_0_5 >> 1)
94 #define CTM_COEFF_0_125 (CTM_COEFF_0_25 >> 1)
95
96 #define CTM_COEFF_LIMITED_RANGE ((235ULL - 16ULL) * CTM_COEFF_1_0 / 255)
97
98 #define CTM_COEFF_NEGATIVE(coeff) (((coeff) & CTM_COEFF_SIGN) != 0)
99 #define CTM_COEFF_ABS(coeff) ((coeff) & (CTM_COEFF_SIGN - 1))
100
101 #define LEGACY_LUT_LENGTH 256
102
103 /*
104 * ILK+ csc matrix:
105 *
106 * |R/Cr| | c0 c1 c2 | ( |R/Cr| |preoff0| ) |postoff0|
107 * |G/Y | = | c3 c4 c5 | x ( |G/Y | + |preoff1| ) + |postoff1|
108 * |B/Cb| | c6 c7 c8 | ( |B/Cb| |preoff2| ) |postoff2|
109 *
110 * ILK/SNB don't have explicit post offsets, and instead
111 * CSC_MODE_YUV_TO_RGB and CSC_BLACK_SCREEN_OFFSET are used:
112 * CSC_MODE_YUV_TO_RGB=0 + CSC_BLACK_SCREEN_OFFSET=0 -> 1/2, 0, 1/2
113 * CSC_MODE_YUV_TO_RGB=0 + CSC_BLACK_SCREEN_OFFSET=1 -> 1/2, 1/16, 1/2
114 * CSC_MODE_YUV_TO_RGB=1 + CSC_BLACK_SCREEN_OFFSET=0 -> 0, 0, 0
115 * CSC_MODE_YUV_TO_RGB=1 + CSC_BLACK_SCREEN_OFFSET=1 -> 1/16, 1/16, 1/16
116 */
117
118 /*
119 * Extract the CSC coefficient from a CTM coefficient (in U32.32 fixed point
120 * format). This macro takes the coefficient we want transformed and the
121 * number of fractional bits.
122 *
123 * We only have a 9 bits precision window which slides depending on the value
124 * of the CTM coefficient and we write the value from bit 3. We also round the
125 * value.
126 */
127 #define ILK_CSC_COEFF_FP(coeff, fbits) \
128 (clamp_val(((coeff) >> (32 - (fbits) - 3)) + 4, 0, 0xfff) & 0xff8)
129
130 #define ILK_CSC_COEFF_1_0 0x7800
131 #define ILK_CSC_COEFF_LIMITED_RANGE ((235 - 16) << (12 - 8)) /* exponent 0 */
132 #define ILK_CSC_POSTOFF_LIMITED_RANGE (16 << (12 - 8))
133
134 static const struct intel_csc_matrix ilk_csc_matrix_identity = {
135 .preoff = {},
136 .coeff = {
137 ILK_CSC_COEFF_1_0, 0, 0,
138 0, ILK_CSC_COEFF_1_0, 0,
139 0, 0, ILK_CSC_COEFF_1_0,
140 },
141 .postoff = {},
142 };
143
144 /* Full range RGB -> limited range RGB matrix */
145 static const struct intel_csc_matrix ilk_csc_matrix_limited_range = {
146 .preoff = {},
147 .coeff = {
148 ILK_CSC_COEFF_LIMITED_RANGE, 0, 0,
149 0, ILK_CSC_COEFF_LIMITED_RANGE, 0,
150 0, 0, ILK_CSC_COEFF_LIMITED_RANGE,
151 },
152 .postoff = {
153 ILK_CSC_POSTOFF_LIMITED_RANGE,
154 ILK_CSC_POSTOFF_LIMITED_RANGE,
155 ILK_CSC_POSTOFF_LIMITED_RANGE,
156 },
157 };
158
159 /* BT.709 full range RGB -> limited range YCbCr matrix */
160 static const struct intel_csc_matrix ilk_csc_matrix_rgb_to_ycbcr = {
161 .preoff = {},
162 .coeff = {
163 0x1e08, 0x9cc0, 0xb528,
164 0x2ba8, 0x09d8, 0x37e8,
165 0xbce8, 0x9ad8, 0x1e08,
166 },
167 .postoff = {
168 0x0800, 0x0100, 0x0800,
169 },
170 };
171
intel_csc_clear(struct intel_csc_matrix * csc)172 static void intel_csc_clear(struct intel_csc_matrix *csc)
173 {
174 memset(csc, 0, sizeof(*csc));
175 }
176
lut_is_legacy(const struct drm_property_blob * lut)177 static bool lut_is_legacy(const struct drm_property_blob *lut)
178 {
179 return lut && drm_color_lut_size(lut) == LEGACY_LUT_LENGTH;
180 }
181
182 /*
183 * When using limited range, multiply the matrix given by userspace by
184 * the matrix that we would use for the limited range.
185 */
ctm_mult_by_limited(u64 * result,const u64 * input)186 static u64 *ctm_mult_by_limited(u64 *result, const u64 *input)
187 {
188 int i;
189
190 for (i = 0; i < 9; i++) {
191 u64 user_coeff = input[i];
192 u32 limited_coeff = CTM_COEFF_LIMITED_RANGE;
193 u32 abs_coeff = clamp_val(CTM_COEFF_ABS(user_coeff), 0,
194 CTM_COEFF_4_0 - 1) >> 2;
195
196 /*
197 * By scaling every co-efficient with limited range (16-235)
198 * vs full range (0-255) the final o/p will be scaled down to
199 * fit in the limited range supported by the panel.
200 */
201 result[i] = mul_u32_u32(limited_coeff, abs_coeff) >> 30;
202 result[i] |= user_coeff & CTM_COEFF_SIGN;
203 }
204
205 return result;
206 }
207
ilk_update_pipe_csc(struct intel_crtc * crtc,const struct intel_csc_matrix * csc)208 static void ilk_update_pipe_csc(struct intel_crtc *crtc,
209 const struct intel_csc_matrix *csc)
210 {
211 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
212 enum pipe pipe = crtc->pipe;
213
214 intel_de_write_fw(i915, PIPE_CSC_PREOFF_HI(pipe), csc->preoff[0]);
215 intel_de_write_fw(i915, PIPE_CSC_PREOFF_ME(pipe), csc->preoff[1]);
216 intel_de_write_fw(i915, PIPE_CSC_PREOFF_LO(pipe), csc->preoff[2]);
217
218 intel_de_write_fw(i915, PIPE_CSC_COEFF_RY_GY(pipe),
219 csc->coeff[0] << 16 | csc->coeff[1]);
220 intel_de_write_fw(i915, PIPE_CSC_COEFF_BY(pipe),
221 csc->coeff[2] << 16);
222
223 intel_de_write_fw(i915, PIPE_CSC_COEFF_RU_GU(pipe),
224 csc->coeff[3] << 16 | csc->coeff[4]);
225 intel_de_write_fw(i915, PIPE_CSC_COEFF_BU(pipe),
226 csc->coeff[5] << 16);
227
228 intel_de_write_fw(i915, PIPE_CSC_COEFF_RV_GV(pipe),
229 csc->coeff[6] << 16 | csc->coeff[7]);
230 intel_de_write_fw(i915, PIPE_CSC_COEFF_BV(pipe),
231 csc->coeff[8] << 16);
232
233 if (DISPLAY_VER(i915) < 7)
234 return;
235
236 intel_de_write_fw(i915, PIPE_CSC_POSTOFF_HI(pipe), csc->postoff[0]);
237 intel_de_write_fw(i915, PIPE_CSC_POSTOFF_ME(pipe), csc->postoff[1]);
238 intel_de_write_fw(i915, PIPE_CSC_POSTOFF_LO(pipe), csc->postoff[2]);
239 }
240
ilk_read_pipe_csc(struct intel_crtc * crtc,struct intel_csc_matrix * csc)241 static void ilk_read_pipe_csc(struct intel_crtc *crtc,
242 struct intel_csc_matrix *csc)
243 {
244 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
245 enum pipe pipe = crtc->pipe;
246 u32 tmp;
247
248 csc->preoff[0] = intel_de_read_fw(i915, PIPE_CSC_PREOFF_HI(pipe));
249 csc->preoff[1] = intel_de_read_fw(i915, PIPE_CSC_PREOFF_ME(pipe));
250 csc->preoff[2] = intel_de_read_fw(i915, PIPE_CSC_PREOFF_LO(pipe));
251
252 tmp = intel_de_read_fw(i915, PIPE_CSC_COEFF_RY_GY(pipe));
253 csc->coeff[0] = tmp >> 16;
254 csc->coeff[1] = tmp & 0xffff;
255 tmp = intel_de_read_fw(i915, PIPE_CSC_COEFF_BY(pipe));
256 csc->coeff[2] = tmp >> 16;
257
258 tmp = intel_de_read_fw(i915, PIPE_CSC_COEFF_RU_GU(pipe));
259 csc->coeff[3] = tmp >> 16;
260 csc->coeff[4] = tmp & 0xffff;
261 tmp = intel_de_read_fw(i915, PIPE_CSC_COEFF_BU(pipe));
262 csc->coeff[5] = tmp >> 16;
263
264 tmp = intel_de_read_fw(i915, PIPE_CSC_COEFF_RV_GV(pipe));
265 csc->coeff[6] = tmp >> 16;
266 csc->coeff[7] = tmp & 0xffff;
267 tmp = intel_de_read_fw(i915, PIPE_CSC_COEFF_BV(pipe));
268 csc->coeff[8] = tmp >> 16;
269
270 if (DISPLAY_VER(i915) < 7)
271 return;
272
273 csc->postoff[0] = intel_de_read_fw(i915, PIPE_CSC_POSTOFF_HI(pipe));
274 csc->postoff[1] = intel_de_read_fw(i915, PIPE_CSC_POSTOFF_ME(pipe));
275 csc->postoff[2] = intel_de_read_fw(i915, PIPE_CSC_POSTOFF_LO(pipe));
276 }
277
ilk_read_csc(struct intel_crtc_state * crtc_state)278 static void ilk_read_csc(struct intel_crtc_state *crtc_state)
279 {
280 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
281
282 if (crtc_state->csc_enable)
283 ilk_read_pipe_csc(crtc, &crtc_state->csc);
284 }
285
skl_read_csc(struct intel_crtc_state * crtc_state)286 static void skl_read_csc(struct intel_crtc_state *crtc_state)
287 {
288 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
289
290 /*
291 * Display WA #1184: skl,glk
292 * Wa_1406463849: icl
293 *
294 * Danger! On SKL-ICL *reads* from the CSC coeff/offset registers
295 * will disarm an already armed CSC double buffer update.
296 * So this must not be called while armed. Fortunately the state checker
297 * readout happens only after the update has been already been latched.
298 *
299 * On earlier and later platforms only writes to said registers will
300 * disarm the update. This is considered normal behavior and also
301 * happens with various other hardware units.
302 */
303 if (crtc_state->csc_enable)
304 ilk_read_pipe_csc(crtc, &crtc_state->csc);
305 }
306
icl_update_output_csc(struct intel_crtc * crtc,const struct intel_csc_matrix * csc)307 static void icl_update_output_csc(struct intel_crtc *crtc,
308 const struct intel_csc_matrix *csc)
309 {
310 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
311 enum pipe pipe = crtc->pipe;
312
313 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_PREOFF_HI(pipe), csc->preoff[0]);
314 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_PREOFF_ME(pipe), csc->preoff[1]);
315 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_PREOFF_LO(pipe), csc->preoff[2]);
316
317 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_COEFF_RY_GY(pipe),
318 csc->coeff[0] << 16 | csc->coeff[1]);
319 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_COEFF_BY(pipe),
320 csc->coeff[2] << 16);
321
322 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_COEFF_RU_GU(pipe),
323 csc->coeff[3] << 16 | csc->coeff[4]);
324 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_COEFF_BU(pipe),
325 csc->coeff[5] << 16);
326
327 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_COEFF_RV_GV(pipe),
328 csc->coeff[6] << 16 | csc->coeff[7]);
329 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_COEFF_BV(pipe),
330 csc->coeff[8] << 16);
331
332 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_POSTOFF_HI(pipe), csc->postoff[0]);
333 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_POSTOFF_ME(pipe), csc->postoff[1]);
334 intel_de_write_fw(i915, PIPE_CSC_OUTPUT_POSTOFF_LO(pipe), csc->postoff[2]);
335 }
336
icl_read_output_csc(struct intel_crtc * crtc,struct intel_csc_matrix * csc)337 static void icl_read_output_csc(struct intel_crtc *crtc,
338 struct intel_csc_matrix *csc)
339 {
340 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
341 enum pipe pipe = crtc->pipe;
342 u32 tmp;
343
344 csc->preoff[0] = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_PREOFF_HI(pipe));
345 csc->preoff[1] = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_PREOFF_ME(pipe));
346 csc->preoff[2] = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_PREOFF_LO(pipe));
347
348 tmp = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_COEFF_RY_GY(pipe));
349 csc->coeff[0] = tmp >> 16;
350 csc->coeff[1] = tmp & 0xffff;
351 tmp = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_COEFF_BY(pipe));
352 csc->coeff[2] = tmp >> 16;
353
354 tmp = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_COEFF_RU_GU(pipe));
355 csc->coeff[3] = tmp >> 16;
356 csc->coeff[4] = tmp & 0xffff;
357 tmp = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_COEFF_BU(pipe));
358 csc->coeff[5] = tmp >> 16;
359
360 tmp = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_COEFF_RV_GV(pipe));
361 csc->coeff[6] = tmp >> 16;
362 csc->coeff[7] = tmp & 0xffff;
363 tmp = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_COEFF_BV(pipe));
364 csc->coeff[8] = tmp >> 16;
365
366 csc->postoff[0] = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_POSTOFF_HI(pipe));
367 csc->postoff[1] = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_POSTOFF_ME(pipe));
368 csc->postoff[2] = intel_de_read_fw(i915, PIPE_CSC_OUTPUT_POSTOFF_LO(pipe));
369 }
370
icl_read_csc(struct intel_crtc_state * crtc_state)371 static void icl_read_csc(struct intel_crtc_state *crtc_state)
372 {
373 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
374
375 /*
376 * Wa_1406463849: icl
377 *
378 * See skl_read_csc()
379 */
380 if (crtc_state->csc_mode & ICL_CSC_ENABLE)
381 ilk_read_pipe_csc(crtc, &crtc_state->csc);
382
383 if (crtc_state->csc_mode & ICL_OUTPUT_CSC_ENABLE)
384 icl_read_output_csc(crtc, &crtc_state->output_csc);
385 }
386
ilk_limited_range(const struct intel_crtc_state * crtc_state)387 static bool ilk_limited_range(const struct intel_crtc_state *crtc_state)
388 {
389 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
390
391 /* icl+ have dedicated output CSC */
392 if (DISPLAY_VER(i915) >= 11)
393 return false;
394
395 /* pre-hsw have TRANSCONF_COLOR_RANGE_SELECT */
396 if (DISPLAY_VER(i915) < 7 || IS_IVYBRIDGE(i915))
397 return false;
398
399 return crtc_state->limited_color_range;
400 }
401
ilk_lut_limited_range(const struct intel_crtc_state * crtc_state)402 static bool ilk_lut_limited_range(const struct intel_crtc_state *crtc_state)
403 {
404 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
405
406 if (!ilk_limited_range(crtc_state))
407 return false;
408
409 if (crtc_state->c8_planes)
410 return false;
411
412 if (DISPLAY_VER(i915) == 10)
413 return crtc_state->hw.gamma_lut;
414 else
415 return crtc_state->hw.gamma_lut &&
416 (crtc_state->hw.degamma_lut || crtc_state->hw.ctm);
417 }
418
ilk_csc_limited_range(const struct intel_crtc_state * crtc_state)419 static bool ilk_csc_limited_range(const struct intel_crtc_state *crtc_state)
420 {
421 if (!ilk_limited_range(crtc_state))
422 return false;
423
424 return !ilk_lut_limited_range(crtc_state);
425 }
426
ilk_csc_copy(struct drm_i915_private * i915,struct intel_csc_matrix * dst,const struct intel_csc_matrix * src)427 static void ilk_csc_copy(struct drm_i915_private *i915,
428 struct intel_csc_matrix *dst,
429 const struct intel_csc_matrix *src)
430 {
431 *dst = *src;
432
433 if (DISPLAY_VER(i915) < 7)
434 memset(dst->postoff, 0, sizeof(dst->postoff));
435 }
436
ilk_csc_convert_ctm(const struct intel_crtc_state * crtc_state,struct intel_csc_matrix * csc,bool limited_color_range)437 static void ilk_csc_convert_ctm(const struct intel_crtc_state *crtc_state,
438 struct intel_csc_matrix *csc,
439 bool limited_color_range)
440 {
441 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
442 const struct drm_color_ctm *ctm = crtc_state->hw.ctm->data;
443 const u64 *input;
444 u64 temp[9];
445 int i;
446
447 /* for preoff/postoff */
448 if (limited_color_range)
449 ilk_csc_copy(i915, csc, &ilk_csc_matrix_limited_range);
450 else
451 ilk_csc_copy(i915, csc, &ilk_csc_matrix_identity);
452
453 if (limited_color_range)
454 input = ctm_mult_by_limited(temp, ctm->matrix);
455 else
456 input = ctm->matrix;
457
458 /*
459 * Convert fixed point S31.32 input to format supported by the
460 * hardware.
461 */
462 for (i = 0; i < 9; i++) {
463 u64 abs_coeff = ((1ULL << 63) - 1) & input[i];
464
465 /*
466 * Clamp input value to min/max supported by
467 * hardware.
468 */
469 abs_coeff = clamp_val(abs_coeff, 0, CTM_COEFF_4_0 - 1);
470
471 csc->coeff[i] = 0;
472
473 /* sign bit */
474 if (CTM_COEFF_NEGATIVE(input[i]))
475 csc->coeff[i] |= 1 << 15;
476
477 if (abs_coeff < CTM_COEFF_0_125)
478 csc->coeff[i] |= (3 << 12) |
479 ILK_CSC_COEFF_FP(abs_coeff, 12);
480 else if (abs_coeff < CTM_COEFF_0_25)
481 csc->coeff[i] |= (2 << 12) |
482 ILK_CSC_COEFF_FP(abs_coeff, 11);
483 else if (abs_coeff < CTM_COEFF_0_5)
484 csc->coeff[i] |= (1 << 12) |
485 ILK_CSC_COEFF_FP(abs_coeff, 10);
486 else if (abs_coeff < CTM_COEFF_1_0)
487 csc->coeff[i] |= ILK_CSC_COEFF_FP(abs_coeff, 9);
488 else if (abs_coeff < CTM_COEFF_2_0)
489 csc->coeff[i] |= (7 << 12) |
490 ILK_CSC_COEFF_FP(abs_coeff, 8);
491 else
492 csc->coeff[i] |= (6 << 12) |
493 ILK_CSC_COEFF_FP(abs_coeff, 7);
494 }
495 }
496
ilk_assign_csc(struct intel_crtc_state * crtc_state)497 static void ilk_assign_csc(struct intel_crtc_state *crtc_state)
498 {
499 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
500 bool limited_color_range = ilk_csc_limited_range(crtc_state);
501
502 if (crtc_state->hw.ctm) {
503 drm_WARN_ON(&i915->drm, !crtc_state->csc_enable);
504
505 ilk_csc_convert_ctm(crtc_state, &crtc_state->csc, limited_color_range);
506 } else if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB) {
507 drm_WARN_ON(&i915->drm, !crtc_state->csc_enable);
508
509 ilk_csc_copy(i915, &crtc_state->csc, &ilk_csc_matrix_rgb_to_ycbcr);
510 } else if (limited_color_range) {
511 drm_WARN_ON(&i915->drm, !crtc_state->csc_enable);
512
513 ilk_csc_copy(i915, &crtc_state->csc, &ilk_csc_matrix_limited_range);
514 } else if (crtc_state->csc_enable) {
515 /*
516 * On GLK both pipe CSC and degamma LUT are controlled
517 * by csc_enable. Hence for the cases where the degama
518 * LUT is needed but CSC is not we need to load an
519 * identity matrix.
520 */
521 drm_WARN_ON(&i915->drm, !IS_GEMINILAKE(i915));
522
523 ilk_csc_copy(i915, &crtc_state->csc, &ilk_csc_matrix_identity);
524 } else {
525 intel_csc_clear(&crtc_state->csc);
526 }
527 }
528
ilk_load_csc_matrix(const struct intel_crtc_state * crtc_state)529 static void ilk_load_csc_matrix(const struct intel_crtc_state *crtc_state)
530 {
531 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
532
533 if (crtc_state->csc_enable)
534 ilk_update_pipe_csc(crtc, &crtc_state->csc);
535 }
536
icl_assign_csc(struct intel_crtc_state * crtc_state)537 static void icl_assign_csc(struct intel_crtc_state *crtc_state)
538 {
539 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
540
541 if (crtc_state->hw.ctm) {
542 drm_WARN_ON(&i915->drm, (crtc_state->csc_mode & ICL_CSC_ENABLE) == 0);
543
544 ilk_csc_convert_ctm(crtc_state, &crtc_state->csc, false);
545 } else {
546 drm_WARN_ON(&i915->drm, (crtc_state->csc_mode & ICL_CSC_ENABLE) != 0);
547
548 intel_csc_clear(&crtc_state->csc);
549 }
550
551 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB) {
552 drm_WARN_ON(&i915->drm, (crtc_state->csc_mode & ICL_OUTPUT_CSC_ENABLE) == 0);
553
554 ilk_csc_copy(i915, &crtc_state->output_csc, &ilk_csc_matrix_rgb_to_ycbcr);
555 } else if (crtc_state->limited_color_range) {
556 drm_WARN_ON(&i915->drm, (crtc_state->csc_mode & ICL_OUTPUT_CSC_ENABLE) == 0);
557
558 ilk_csc_copy(i915, &crtc_state->output_csc, &ilk_csc_matrix_limited_range);
559 } else {
560 drm_WARN_ON(&i915->drm, (crtc_state->csc_mode & ICL_OUTPUT_CSC_ENABLE) != 0);
561
562 intel_csc_clear(&crtc_state->output_csc);
563 }
564 }
565
icl_load_csc_matrix(const struct intel_crtc_state * crtc_state)566 static void icl_load_csc_matrix(const struct intel_crtc_state *crtc_state)
567 {
568 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
569
570 if (crtc_state->csc_mode & ICL_CSC_ENABLE)
571 ilk_update_pipe_csc(crtc, &crtc_state->csc);
572
573 if (crtc_state->csc_mode & ICL_OUTPUT_CSC_ENABLE)
574 icl_update_output_csc(crtc, &crtc_state->output_csc);
575 }
576
ctm_to_twos_complement(u64 coeff,int int_bits,int frac_bits)577 static u16 ctm_to_twos_complement(u64 coeff, int int_bits, int frac_bits)
578 {
579 s64 c = CTM_COEFF_ABS(coeff);
580
581 /* leave an extra bit for rounding */
582 c >>= 32 - frac_bits - 1;
583
584 /* round and drop the extra bit */
585 c = (c + 1) >> 1;
586
587 if (CTM_COEFF_NEGATIVE(coeff))
588 c = -c;
589
590 c = clamp(c, -(s64)BIT(int_bits + frac_bits - 1),
591 (s64)(BIT(int_bits + frac_bits - 1) - 1));
592
593 return c & (BIT(int_bits + frac_bits) - 1);
594 }
595
596 /*
597 * VLV/CHV Wide Gamut Color Correction (WGC) CSC
598 * |r| | c0 c1 c2 | |r|
599 * |g| = | c3 c4 c5 | x |g|
600 * |b| | c6 c7 c8 | |b|
601 *
602 * Coefficients are two's complement s2.10.
603 */
vlv_wgc_csc_convert_ctm(const struct intel_crtc_state * crtc_state,struct intel_csc_matrix * csc)604 static void vlv_wgc_csc_convert_ctm(const struct intel_crtc_state *crtc_state,
605 struct intel_csc_matrix *csc)
606 {
607 const struct drm_color_ctm *ctm = crtc_state->hw.ctm->data;
608 int i;
609
610 for (i = 0; i < 9; i++)
611 csc->coeff[i] = ctm_to_twos_complement(ctm->matrix[i], 2, 10);
612 }
613
vlv_load_wgc_csc(struct intel_crtc * crtc,const struct intel_csc_matrix * csc)614 static void vlv_load_wgc_csc(struct intel_crtc *crtc,
615 const struct intel_csc_matrix *csc)
616 {
617 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
618 enum pipe pipe = crtc->pipe;
619
620 intel_de_write_fw(dev_priv, PIPE_WGC_C01_C00(dev_priv, pipe),
621 csc->coeff[1] << 16 | csc->coeff[0]);
622 intel_de_write_fw(dev_priv, PIPE_WGC_C02(dev_priv, pipe),
623 csc->coeff[2]);
624
625 intel_de_write_fw(dev_priv, PIPE_WGC_C11_C10(dev_priv, pipe),
626 csc->coeff[4] << 16 | csc->coeff[3]);
627 intel_de_write_fw(dev_priv, PIPE_WGC_C12(dev_priv, pipe),
628 csc->coeff[5]);
629
630 intel_de_write_fw(dev_priv, PIPE_WGC_C21_C20(dev_priv, pipe),
631 csc->coeff[7] << 16 | csc->coeff[6]);
632 intel_de_write_fw(dev_priv, PIPE_WGC_C22(dev_priv, pipe),
633 csc->coeff[8]);
634 }
635
vlv_read_wgc_csc(struct intel_crtc * crtc,struct intel_csc_matrix * csc)636 static void vlv_read_wgc_csc(struct intel_crtc *crtc,
637 struct intel_csc_matrix *csc)
638 {
639 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
640 enum pipe pipe = crtc->pipe;
641 u32 tmp;
642
643 tmp = intel_de_read_fw(dev_priv, PIPE_WGC_C01_C00(dev_priv, pipe));
644 csc->coeff[0] = tmp & 0xffff;
645 csc->coeff[1] = tmp >> 16;
646
647 tmp = intel_de_read_fw(dev_priv, PIPE_WGC_C02(dev_priv, pipe));
648 csc->coeff[2] = tmp & 0xffff;
649
650 tmp = intel_de_read_fw(dev_priv, PIPE_WGC_C11_C10(dev_priv, pipe));
651 csc->coeff[3] = tmp & 0xffff;
652 csc->coeff[4] = tmp >> 16;
653
654 tmp = intel_de_read_fw(dev_priv, PIPE_WGC_C12(dev_priv, pipe));
655 csc->coeff[5] = tmp & 0xffff;
656
657 tmp = intel_de_read_fw(dev_priv, PIPE_WGC_C21_C20(dev_priv, pipe));
658 csc->coeff[6] = tmp & 0xffff;
659 csc->coeff[7] = tmp >> 16;
660
661 tmp = intel_de_read_fw(dev_priv, PIPE_WGC_C22(dev_priv, pipe));
662 csc->coeff[8] = tmp & 0xffff;
663 }
664
vlv_read_csc(struct intel_crtc_state * crtc_state)665 static void vlv_read_csc(struct intel_crtc_state *crtc_state)
666 {
667 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
668
669 if (crtc_state->wgc_enable)
670 vlv_read_wgc_csc(crtc, &crtc_state->csc);
671 }
672
vlv_assign_csc(struct intel_crtc_state * crtc_state)673 static void vlv_assign_csc(struct intel_crtc_state *crtc_state)
674 {
675 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
676
677 if (crtc_state->hw.ctm) {
678 drm_WARN_ON(&i915->drm, !crtc_state->wgc_enable);
679
680 vlv_wgc_csc_convert_ctm(crtc_state, &crtc_state->csc);
681 } else {
682 drm_WARN_ON(&i915->drm, crtc_state->wgc_enable);
683
684 intel_csc_clear(&crtc_state->csc);
685 }
686 }
687
688 /*
689 * CHV Color Gamut Mapping (CGM) CSC
690 * |r| | c0 c1 c2 | |r|
691 * |g| = | c3 c4 c5 | x |g|
692 * |b| | c6 c7 c8 | |b|
693 *
694 * Coefficients are two's complement s4.12.
695 */
chv_cgm_csc_convert_ctm(const struct intel_crtc_state * crtc_state,struct intel_csc_matrix * csc)696 static void chv_cgm_csc_convert_ctm(const struct intel_crtc_state *crtc_state,
697 struct intel_csc_matrix *csc)
698 {
699 const struct drm_color_ctm *ctm = crtc_state->hw.ctm->data;
700 int i;
701
702 for (i = 0; i < 9; i++)
703 csc->coeff[i] = ctm_to_twos_complement(ctm->matrix[i], 4, 12);
704 }
705
706 #define CHV_CGM_CSC_COEFF_1_0 (1 << 12)
707
708 static const struct intel_csc_matrix chv_cgm_csc_matrix_identity = {
709 .coeff = {
710 CHV_CGM_CSC_COEFF_1_0, 0, 0,
711 0, CHV_CGM_CSC_COEFF_1_0, 0,
712 0, 0, CHV_CGM_CSC_COEFF_1_0,
713 },
714 };
715
chv_load_cgm_csc(struct intel_crtc * crtc,const struct intel_csc_matrix * csc)716 static void chv_load_cgm_csc(struct intel_crtc *crtc,
717 const struct intel_csc_matrix *csc)
718 {
719 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
720 enum pipe pipe = crtc->pipe;
721
722 intel_de_write_fw(i915, CGM_PIPE_CSC_COEFF01(pipe),
723 csc->coeff[1] << 16 | csc->coeff[0]);
724 intel_de_write_fw(i915, CGM_PIPE_CSC_COEFF23(pipe),
725 csc->coeff[3] << 16 | csc->coeff[2]);
726 intel_de_write_fw(i915, CGM_PIPE_CSC_COEFF45(pipe),
727 csc->coeff[5] << 16 | csc->coeff[4]);
728 intel_de_write_fw(i915, CGM_PIPE_CSC_COEFF67(pipe),
729 csc->coeff[7] << 16 | csc->coeff[6]);
730 intel_de_write_fw(i915, CGM_PIPE_CSC_COEFF8(pipe),
731 csc->coeff[8]);
732 }
733
chv_read_cgm_csc(struct intel_crtc * crtc,struct intel_csc_matrix * csc)734 static void chv_read_cgm_csc(struct intel_crtc *crtc,
735 struct intel_csc_matrix *csc)
736 {
737 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
738 enum pipe pipe = crtc->pipe;
739 u32 tmp;
740
741 tmp = intel_de_read_fw(i915, CGM_PIPE_CSC_COEFF01(pipe));
742 csc->coeff[0] = tmp & 0xffff;
743 csc->coeff[1] = tmp >> 16;
744
745 tmp = intel_de_read_fw(i915, CGM_PIPE_CSC_COEFF23(pipe));
746 csc->coeff[2] = tmp & 0xffff;
747 csc->coeff[3] = tmp >> 16;
748
749 tmp = intel_de_read_fw(i915, CGM_PIPE_CSC_COEFF45(pipe));
750 csc->coeff[4] = tmp & 0xffff;
751 csc->coeff[5] = tmp >> 16;
752
753 tmp = intel_de_read_fw(i915, CGM_PIPE_CSC_COEFF67(pipe));
754 csc->coeff[6] = tmp & 0xffff;
755 csc->coeff[7] = tmp >> 16;
756
757 tmp = intel_de_read_fw(i915, CGM_PIPE_CSC_COEFF8(pipe));
758 csc->coeff[8] = tmp & 0xffff;
759 }
760
chv_read_csc(struct intel_crtc_state * crtc_state)761 static void chv_read_csc(struct intel_crtc_state *crtc_state)
762 {
763 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
764
765 if (crtc_state->cgm_mode & CGM_PIPE_MODE_CSC)
766 chv_read_cgm_csc(crtc, &crtc_state->csc);
767 }
768
chv_assign_csc(struct intel_crtc_state * crtc_state)769 static void chv_assign_csc(struct intel_crtc_state *crtc_state)
770 {
771 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
772
773 drm_WARN_ON(&i915->drm, crtc_state->wgc_enable);
774
775 if (crtc_state->hw.ctm) {
776 drm_WARN_ON(&i915->drm, (crtc_state->cgm_mode & CGM_PIPE_MODE_CSC) == 0);
777
778 chv_cgm_csc_convert_ctm(crtc_state, &crtc_state->csc);
779 } else {
780 drm_WARN_ON(&i915->drm, (crtc_state->cgm_mode & CGM_PIPE_MODE_CSC) == 0);
781
782 crtc_state->csc = chv_cgm_csc_matrix_identity;
783 }
784 }
785
786 /* convert hw value with given bit_precision to lut property val */
intel_color_lut_pack(u32 val,int bit_precision)787 static u32 intel_color_lut_pack(u32 val, int bit_precision)
788 {
789 if (bit_precision > 16)
790 return DIV_ROUND_CLOSEST_ULL(mul_u32_u32(val, (1 << 16) - 1),
791 (1 << bit_precision) - 1);
792 else
793 return DIV_ROUND_CLOSEST(val * ((1 << 16) - 1),
794 (1 << bit_precision) - 1);
795 }
796
i9xx_lut_8(const struct drm_color_lut * color)797 static u32 i9xx_lut_8(const struct drm_color_lut *color)
798 {
799 return REG_FIELD_PREP(PALETTE_RED_MASK, drm_color_lut_extract(color->red, 8)) |
800 REG_FIELD_PREP(PALETTE_GREEN_MASK, drm_color_lut_extract(color->green, 8)) |
801 REG_FIELD_PREP(PALETTE_BLUE_MASK, drm_color_lut_extract(color->blue, 8));
802 }
803
i9xx_lut_8_pack(struct drm_color_lut * entry,u32 val)804 static void i9xx_lut_8_pack(struct drm_color_lut *entry, u32 val)
805 {
806 entry->red = intel_color_lut_pack(REG_FIELD_GET(PALETTE_RED_MASK, val), 8);
807 entry->green = intel_color_lut_pack(REG_FIELD_GET(PALETTE_GREEN_MASK, val), 8);
808 entry->blue = intel_color_lut_pack(REG_FIELD_GET(PALETTE_BLUE_MASK, val), 8);
809 }
810
811 /* i8xx/i9xx+ 10bit slope format "even DW" (low 8 bits) */
_i9xx_lut_10_ldw(u16 a)812 static u32 _i9xx_lut_10_ldw(u16 a)
813 {
814 return drm_color_lut_extract(a, 10) & 0xff;
815 }
816
i9xx_lut_10_ldw(const struct drm_color_lut * color)817 static u32 i9xx_lut_10_ldw(const struct drm_color_lut *color)
818 {
819 return REG_FIELD_PREP(PALETTE_RED_MASK, _i9xx_lut_10_ldw(color[0].red)) |
820 REG_FIELD_PREP(PALETTE_GREEN_MASK, _i9xx_lut_10_ldw(color[0].green)) |
821 REG_FIELD_PREP(PALETTE_BLUE_MASK, _i9xx_lut_10_ldw(color[0].blue));
822 }
823
824 /* i8xx/i9xx+ 10bit slope format "odd DW" (high 2 bits + slope) */
_i9xx_lut_10_udw(u16 a,u16 b)825 static u32 _i9xx_lut_10_udw(u16 a, u16 b)
826 {
827 unsigned int mantissa, exponent;
828
829 a = drm_color_lut_extract(a, 10);
830 b = drm_color_lut_extract(b, 10);
831
832 /* b = a + 8 * m * 2 ^ -e */
833 mantissa = clamp(b - a, 0, 0x7f);
834 exponent = 3;
835 while (mantissa > 0xf) {
836 mantissa >>= 1;
837 exponent--;
838 }
839
840 return (exponent << 6) |
841 (mantissa << 2) |
842 (a >> 8);
843 }
844
i9xx_lut_10_udw(const struct drm_color_lut * color)845 static u32 i9xx_lut_10_udw(const struct drm_color_lut *color)
846 {
847 return REG_FIELD_PREP(PALETTE_RED_MASK, _i9xx_lut_10_udw(color[0].red, color[1].red)) |
848 REG_FIELD_PREP(PALETTE_GREEN_MASK, _i9xx_lut_10_udw(color[0].green, color[1].green)) |
849 REG_FIELD_PREP(PALETTE_BLUE_MASK, _i9xx_lut_10_udw(color[0].blue, color[1].blue));
850 }
851
i9xx_lut_10_pack(struct drm_color_lut * color,u32 ldw,u32 udw)852 static void i9xx_lut_10_pack(struct drm_color_lut *color,
853 u32 ldw, u32 udw)
854 {
855 u16 red = REG_FIELD_GET(PALETTE_10BIT_RED_LDW_MASK, ldw) |
856 REG_FIELD_GET(PALETTE_10BIT_RED_UDW_MASK, udw) << 8;
857 u16 green = REG_FIELD_GET(PALETTE_10BIT_GREEN_LDW_MASK, ldw) |
858 REG_FIELD_GET(PALETTE_10BIT_GREEN_UDW_MASK, udw) << 8;
859 u16 blue = REG_FIELD_GET(PALETTE_10BIT_BLUE_LDW_MASK, ldw) |
860 REG_FIELD_GET(PALETTE_10BIT_BLUE_UDW_MASK, udw) << 8;
861
862 color->red = intel_color_lut_pack(red, 10);
863 color->green = intel_color_lut_pack(green, 10);
864 color->blue = intel_color_lut_pack(blue, 10);
865 }
866
i9xx_lut_10_pack_slope(struct drm_color_lut * color,u32 ldw,u32 udw)867 static void i9xx_lut_10_pack_slope(struct drm_color_lut *color,
868 u32 ldw, u32 udw)
869 {
870 int r_exp = REG_FIELD_GET(PALETTE_10BIT_RED_EXP_MASK, udw);
871 int r_mant = REG_FIELD_GET(PALETTE_10BIT_RED_MANT_MASK, udw);
872 int g_exp = REG_FIELD_GET(PALETTE_10BIT_GREEN_EXP_MASK, udw);
873 int g_mant = REG_FIELD_GET(PALETTE_10BIT_GREEN_MANT_MASK, udw);
874 int b_exp = REG_FIELD_GET(PALETTE_10BIT_BLUE_EXP_MASK, udw);
875 int b_mant = REG_FIELD_GET(PALETTE_10BIT_BLUE_MANT_MASK, udw);
876
877 i9xx_lut_10_pack(color, ldw, udw);
878
879 color->red += r_mant << (3 - r_exp);
880 color->green += g_mant << (3 - g_exp);
881 color->blue += b_mant << (3 - b_exp);
882 }
883
884 /* i965+ "10.6" bit interpolated format "even DW" (low 8 bits) */
i965_lut_10p6_ldw(const struct drm_color_lut * color)885 static u32 i965_lut_10p6_ldw(const struct drm_color_lut *color)
886 {
887 return REG_FIELD_PREP(PALETTE_RED_MASK, color->red & 0xff) |
888 REG_FIELD_PREP(PALETTE_GREEN_MASK, color->green & 0xff) |
889 REG_FIELD_PREP(PALETTE_BLUE_MASK, color->blue & 0xff);
890 }
891
892 /* i965+ "10.6" interpolated format "odd DW" (high 8 bits) */
i965_lut_10p6_udw(const struct drm_color_lut * color)893 static u32 i965_lut_10p6_udw(const struct drm_color_lut *color)
894 {
895 return REG_FIELD_PREP(PALETTE_RED_MASK, color->red >> 8) |
896 REG_FIELD_PREP(PALETTE_GREEN_MASK, color->green >> 8) |
897 REG_FIELD_PREP(PALETTE_BLUE_MASK, color->blue >> 8);
898 }
899
i965_lut_10p6_pack(struct drm_color_lut * entry,u32 ldw,u32 udw)900 static void i965_lut_10p6_pack(struct drm_color_lut *entry, u32 ldw, u32 udw)
901 {
902 entry->red = REG_FIELD_GET(PALETTE_RED_MASK, udw) << 8 |
903 REG_FIELD_GET(PALETTE_RED_MASK, ldw);
904 entry->green = REG_FIELD_GET(PALETTE_GREEN_MASK, udw) << 8 |
905 REG_FIELD_GET(PALETTE_GREEN_MASK, ldw);
906 entry->blue = REG_FIELD_GET(PALETTE_BLUE_MASK, udw) << 8 |
907 REG_FIELD_GET(PALETTE_BLUE_MASK, ldw);
908 }
909
i965_lut_11p6_max_pack(u32 val)910 static u16 i965_lut_11p6_max_pack(u32 val)
911 {
912 /* PIPEGCMAX is 11.6, clamp to 10.6 */
913 return min(val, 0xffffu);
914 }
915
ilk_lut_10(const struct drm_color_lut * color)916 static u32 ilk_lut_10(const struct drm_color_lut *color)
917 {
918 return REG_FIELD_PREP(PREC_PALETTE_10_RED_MASK, drm_color_lut_extract(color->red, 10)) |
919 REG_FIELD_PREP(PREC_PALETTE_10_GREEN_MASK, drm_color_lut_extract(color->green, 10)) |
920 REG_FIELD_PREP(PREC_PALETTE_10_BLUE_MASK, drm_color_lut_extract(color->blue, 10));
921 }
922
ilk_lut_10_pack(struct drm_color_lut * entry,u32 val)923 static void ilk_lut_10_pack(struct drm_color_lut *entry, u32 val)
924 {
925 entry->red = intel_color_lut_pack(REG_FIELD_GET(PREC_PALETTE_10_RED_MASK, val), 10);
926 entry->green = intel_color_lut_pack(REG_FIELD_GET(PREC_PALETTE_10_GREEN_MASK, val), 10);
927 entry->blue = intel_color_lut_pack(REG_FIELD_GET(PREC_PALETTE_10_BLUE_MASK, val), 10);
928 }
929
930 /* ilk+ "12.4" interpolated format (low 6 bits) */
ilk_lut_12p4_ldw(const struct drm_color_lut * color)931 static u32 ilk_lut_12p4_ldw(const struct drm_color_lut *color)
932 {
933 return REG_FIELD_PREP(PREC_PALETTE_12P4_RED_LDW_MASK, color->red & 0x3f) |
934 REG_FIELD_PREP(PREC_PALETTE_12P4_GREEN_LDW_MASK, color->green & 0x3f) |
935 REG_FIELD_PREP(PREC_PALETTE_12P4_BLUE_LDW_MASK, color->blue & 0x3f);
936 }
937
938 /* ilk+ "12.4" interpolated format (high 10 bits) */
ilk_lut_12p4_udw(const struct drm_color_lut * color)939 static u32 ilk_lut_12p4_udw(const struct drm_color_lut *color)
940 {
941 return REG_FIELD_PREP(PREC_PALETTE_12P4_RED_UDW_MASK, color->red >> 6) |
942 REG_FIELD_PREP(PREC_PALETTE_12P4_GREEN_UDW_MASK, color->green >> 6) |
943 REG_FIELD_PREP(PREC_PALETTE_12P4_BLUE_UDW_MASK, color->blue >> 6);
944 }
945
ilk_lut_12p4_pack(struct drm_color_lut * entry,u32 ldw,u32 udw)946 static void ilk_lut_12p4_pack(struct drm_color_lut *entry, u32 ldw, u32 udw)
947 {
948 entry->red = REG_FIELD_GET(PREC_PALETTE_12P4_RED_UDW_MASK, udw) << 6 |
949 REG_FIELD_GET(PREC_PALETTE_12P4_RED_LDW_MASK, ldw);
950 entry->green = REG_FIELD_GET(PREC_PALETTE_12P4_GREEN_UDW_MASK, udw) << 6 |
951 REG_FIELD_GET(PREC_PALETTE_12P4_GREEN_LDW_MASK, ldw);
952 entry->blue = REG_FIELD_GET(PREC_PALETTE_12P4_BLUE_UDW_MASK, udw) << 6 |
953 REG_FIELD_GET(PREC_PALETTE_12P4_BLUE_LDW_MASK, ldw);
954 }
955
icl_color_commit_noarm(const struct intel_crtc_state * crtc_state)956 static void icl_color_commit_noarm(const struct intel_crtc_state *crtc_state)
957 {
958 /*
959 * Despite Wa_1406463849, ICL no longer suffers from the SKL
960 * DC5/PSR CSC black screen issue (see skl_color_commit_noarm()).
961 * Possibly due to the extra sticky CSC arming
962 * (see icl_color_post_update()).
963 *
964 * On TGL+ all CSC arming issues have been properly fixed.
965 */
966 icl_load_csc_matrix(crtc_state);
967 }
968
skl_color_commit_noarm(const struct intel_crtc_state * crtc_state)969 static void skl_color_commit_noarm(const struct intel_crtc_state *crtc_state)
970 {
971 /*
972 * Possibly related to display WA #1184, SKL CSC loses the latched
973 * CSC coeff/offset register values if the CSC registers are disarmed
974 * between DC5 exit and PSR exit. This will cause the plane(s) to
975 * output all black (until CSC_MODE is rearmed and properly latched).
976 * Once PSR exit (and proper register latching) has occurred the
977 * danger is over. Thus when PSR is enabled the CSC coeff/offset
978 * register programming will be peformed from skl_color_commit_arm()
979 * which is called after PSR exit.
980 */
981 if (!crtc_state->has_psr)
982 ilk_load_csc_matrix(crtc_state);
983 }
984
ilk_color_commit_noarm(const struct intel_crtc_state * crtc_state)985 static void ilk_color_commit_noarm(const struct intel_crtc_state *crtc_state)
986 {
987 ilk_load_csc_matrix(crtc_state);
988 }
989
i9xx_color_commit_arm(const struct intel_crtc_state * crtc_state)990 static void i9xx_color_commit_arm(const struct intel_crtc_state *crtc_state)
991 {
992 /* update TRANSCONF GAMMA_MODE */
993 i9xx_set_pipeconf(crtc_state);
994 }
995
ilk_color_commit_arm(const struct intel_crtc_state * crtc_state)996 static void ilk_color_commit_arm(const struct intel_crtc_state *crtc_state)
997 {
998 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
999 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1000
1001 /* update TRANSCONF GAMMA_MODE */
1002 ilk_set_pipeconf(crtc_state);
1003
1004 intel_de_write_fw(i915, PIPE_CSC_MODE(crtc->pipe),
1005 crtc_state->csc_mode);
1006 }
1007
hsw_color_commit_arm(const struct intel_crtc_state * crtc_state)1008 static void hsw_color_commit_arm(const struct intel_crtc_state *crtc_state)
1009 {
1010 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1011 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1012
1013 intel_de_write(i915, GAMMA_MODE(crtc->pipe),
1014 crtc_state->gamma_mode);
1015
1016 intel_de_write_fw(i915, PIPE_CSC_MODE(crtc->pipe),
1017 crtc_state->csc_mode);
1018 }
1019
hsw_read_gamma_mode(struct intel_crtc * crtc)1020 static u32 hsw_read_gamma_mode(struct intel_crtc *crtc)
1021 {
1022 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1023
1024 return intel_de_read(i915, GAMMA_MODE(crtc->pipe));
1025 }
1026
ilk_read_csc_mode(struct intel_crtc * crtc)1027 static u32 ilk_read_csc_mode(struct intel_crtc *crtc)
1028 {
1029 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1030
1031 return intel_de_read(i915, PIPE_CSC_MODE(crtc->pipe));
1032 }
1033
i9xx_get_config(struct intel_crtc_state * crtc_state)1034 static void i9xx_get_config(struct intel_crtc_state *crtc_state)
1035 {
1036 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1037 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1038 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1039 enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
1040 u32 tmp;
1041
1042 tmp = intel_de_read(dev_priv, DSPCNTR(dev_priv, i9xx_plane));
1043
1044 if (tmp & DISP_PIPE_GAMMA_ENABLE)
1045 crtc_state->gamma_enable = true;
1046
1047 if (!HAS_GMCH(dev_priv) && tmp & DISP_PIPE_CSC_ENABLE)
1048 crtc_state->csc_enable = true;
1049 }
1050
hsw_get_config(struct intel_crtc_state * crtc_state)1051 static void hsw_get_config(struct intel_crtc_state *crtc_state)
1052 {
1053 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1054
1055 crtc_state->gamma_mode = hsw_read_gamma_mode(crtc);
1056 crtc_state->csc_mode = ilk_read_csc_mode(crtc);
1057
1058 i9xx_get_config(crtc_state);
1059 }
1060
skl_get_config(struct intel_crtc_state * crtc_state)1061 static void skl_get_config(struct intel_crtc_state *crtc_state)
1062 {
1063 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1064 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1065 u32 tmp;
1066
1067 crtc_state->gamma_mode = hsw_read_gamma_mode(crtc);
1068 crtc_state->csc_mode = ilk_read_csc_mode(crtc);
1069
1070 tmp = intel_de_read(i915, SKL_BOTTOM_COLOR(crtc->pipe));
1071
1072 if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
1073 crtc_state->gamma_enable = true;
1074
1075 if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
1076 crtc_state->csc_enable = true;
1077 }
1078
skl_color_commit_arm(const struct intel_crtc_state * crtc_state)1079 static void skl_color_commit_arm(const struct intel_crtc_state *crtc_state)
1080 {
1081 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1082 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1083 enum pipe pipe = crtc->pipe;
1084 u32 val = 0;
1085
1086 if (crtc_state->has_psr)
1087 ilk_load_csc_matrix(crtc_state);
1088
1089 /*
1090 * We don't (yet) allow userspace to control the pipe background color,
1091 * so force it to black, but apply pipe gamma and CSC appropriately
1092 * so that its handling will match how we program our planes.
1093 */
1094 if (crtc_state->gamma_enable)
1095 val |= SKL_BOTTOM_COLOR_GAMMA_ENABLE;
1096 if (crtc_state->csc_enable)
1097 val |= SKL_BOTTOM_COLOR_CSC_ENABLE;
1098 intel_de_write(i915, SKL_BOTTOM_COLOR(pipe), val);
1099
1100 intel_de_write(i915, GAMMA_MODE(crtc->pipe),
1101 crtc_state->gamma_mode);
1102
1103 intel_de_write_fw(i915, PIPE_CSC_MODE(crtc->pipe),
1104 crtc_state->csc_mode);
1105 }
1106
icl_color_commit_arm(const struct intel_crtc_state * crtc_state)1107 static void icl_color_commit_arm(const struct intel_crtc_state *crtc_state)
1108 {
1109 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1110 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1111 enum pipe pipe = crtc->pipe;
1112
1113 /*
1114 * We don't (yet) allow userspace to control the pipe background color,
1115 * so force it to black.
1116 */
1117 intel_de_write(i915, SKL_BOTTOM_COLOR(pipe), 0);
1118
1119 intel_de_write(i915, GAMMA_MODE(crtc->pipe),
1120 crtc_state->gamma_mode);
1121
1122 intel_de_write_fw(i915, PIPE_CSC_MODE(crtc->pipe),
1123 crtc_state->csc_mode);
1124 }
1125
icl_color_post_update(const struct intel_crtc_state * crtc_state)1126 static void icl_color_post_update(const struct intel_crtc_state *crtc_state)
1127 {
1128 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1129 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1130
1131 /*
1132 * Despite Wa_1406463849, ICL CSC is no longer disarmed by
1133 * coeff/offset register *writes*. Instead, once CSC_MODE
1134 * is armed it stays armed, even after it has been latched.
1135 * Afterwards the coeff/offset registers become effectively
1136 * self-arming. That self-arming must be disabled before the
1137 * next icl_color_commit_noarm() tries to write the next set
1138 * of coeff/offset registers. Fortunately register *reads*
1139 * do still disarm the CSC. Naturally this must not be done
1140 * until the previously written CSC registers have actually
1141 * been latched.
1142 *
1143 * TGL+ no longer need this workaround.
1144 */
1145 intel_de_read_fw(i915, PIPE_CSC_PREOFF_HI(crtc->pipe));
1146 }
1147
1148 static struct drm_property_blob *
create_linear_lut(struct drm_i915_private * i915,int lut_size)1149 create_linear_lut(struct drm_i915_private *i915, int lut_size)
1150 {
1151 struct drm_property_blob *blob;
1152 struct drm_color_lut *lut;
1153 int i;
1154
1155 blob = drm_property_create_blob(&i915->drm,
1156 sizeof(lut[0]) * lut_size,
1157 NULL);
1158 if (IS_ERR(blob))
1159 return blob;
1160
1161 lut = blob->data;
1162
1163 for (i = 0; i < lut_size; i++) {
1164 u16 val = 0xffff * i / (lut_size - 1);
1165
1166 lut[i].red = val;
1167 lut[i].green = val;
1168 lut[i].blue = val;
1169 }
1170
1171 return blob;
1172 }
1173
lut_limited_range(unsigned int value)1174 static u16 lut_limited_range(unsigned int value)
1175 {
1176 unsigned int min = 16 << 8;
1177 unsigned int max = 235 << 8;
1178
1179 return value * (max - min) / 0xffff + min;
1180 }
1181
1182 static struct drm_property_blob *
create_resized_lut(struct drm_i915_private * i915,const struct drm_property_blob * blob_in,int lut_out_size,bool limited_color_range)1183 create_resized_lut(struct drm_i915_private *i915,
1184 const struct drm_property_blob *blob_in, int lut_out_size,
1185 bool limited_color_range)
1186 {
1187 int i, lut_in_size = drm_color_lut_size(blob_in);
1188 struct drm_property_blob *blob_out;
1189 const struct drm_color_lut *lut_in;
1190 struct drm_color_lut *lut_out;
1191
1192 blob_out = drm_property_create_blob(&i915->drm,
1193 sizeof(lut_out[0]) * lut_out_size,
1194 NULL);
1195 if (IS_ERR(blob_out))
1196 return blob_out;
1197
1198 lut_in = blob_in->data;
1199 lut_out = blob_out->data;
1200
1201 for (i = 0; i < lut_out_size; i++) {
1202 const struct drm_color_lut *entry =
1203 &lut_in[i * (lut_in_size - 1) / (lut_out_size - 1)];
1204
1205 if (limited_color_range) {
1206 lut_out[i].red = lut_limited_range(entry->red);
1207 lut_out[i].green = lut_limited_range(entry->green);
1208 lut_out[i].blue = lut_limited_range(entry->blue);
1209 } else {
1210 lut_out[i] = *entry;
1211 }
1212 }
1213
1214 return blob_out;
1215 }
1216
i9xx_load_lut_8(struct intel_crtc * crtc,const struct drm_property_blob * blob)1217 static void i9xx_load_lut_8(struct intel_crtc *crtc,
1218 const struct drm_property_blob *blob)
1219 {
1220 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1221 const struct drm_color_lut *lut;
1222 enum pipe pipe = crtc->pipe;
1223 int i;
1224
1225 if (!blob)
1226 return;
1227
1228 lut = blob->data;
1229
1230 for (i = 0; i < 256; i++)
1231 intel_de_write_fw(dev_priv, PALETTE(dev_priv, pipe, i),
1232 i9xx_lut_8(&lut[i]));
1233 }
1234
i9xx_load_lut_10(struct intel_crtc * crtc,const struct drm_property_blob * blob)1235 static void i9xx_load_lut_10(struct intel_crtc *crtc,
1236 const struct drm_property_blob *blob)
1237 {
1238 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1239 const struct drm_color_lut *lut = blob->data;
1240 int i, lut_size = drm_color_lut_size(blob);
1241 enum pipe pipe = crtc->pipe;
1242
1243 for (i = 0; i < lut_size - 1; i++) {
1244 intel_de_write_fw(dev_priv,
1245 PALETTE(dev_priv, pipe, 2 * i + 0),
1246 i9xx_lut_10_ldw(&lut[i]));
1247 intel_de_write_fw(dev_priv,
1248 PALETTE(dev_priv, pipe, 2 * i + 1),
1249 i9xx_lut_10_udw(&lut[i]));
1250 }
1251 }
1252
i9xx_load_luts(const struct intel_crtc_state * crtc_state)1253 static void i9xx_load_luts(const struct intel_crtc_state *crtc_state)
1254 {
1255 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1256 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1257
1258 switch (crtc_state->gamma_mode) {
1259 case GAMMA_MODE_MODE_8BIT:
1260 i9xx_load_lut_8(crtc, post_csc_lut);
1261 break;
1262 case GAMMA_MODE_MODE_10BIT:
1263 i9xx_load_lut_10(crtc, post_csc_lut);
1264 break;
1265 default:
1266 MISSING_CASE(crtc_state->gamma_mode);
1267 break;
1268 }
1269 }
1270
i965_load_lut_10p6(struct intel_crtc * crtc,const struct drm_property_blob * blob)1271 static void i965_load_lut_10p6(struct intel_crtc *crtc,
1272 const struct drm_property_blob *blob)
1273 {
1274 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1275 const struct drm_color_lut *lut = blob->data;
1276 int i, lut_size = drm_color_lut_size(blob);
1277 enum pipe pipe = crtc->pipe;
1278
1279 for (i = 0; i < lut_size - 1; i++) {
1280 intel_de_write_fw(dev_priv,
1281 PALETTE(dev_priv, pipe, 2 * i + 0),
1282 i965_lut_10p6_ldw(&lut[i]));
1283 intel_de_write_fw(dev_priv,
1284 PALETTE(dev_priv, pipe, 2 * i + 1),
1285 i965_lut_10p6_udw(&lut[i]));
1286 }
1287
1288 intel_de_write_fw(dev_priv, PIPEGCMAX(dev_priv, pipe, 0), lut[i].red);
1289 intel_de_write_fw(dev_priv, PIPEGCMAX(dev_priv, pipe, 1), lut[i].green);
1290 intel_de_write_fw(dev_priv, PIPEGCMAX(dev_priv, pipe, 2), lut[i].blue);
1291 }
1292
i965_load_luts(const struct intel_crtc_state * crtc_state)1293 static void i965_load_luts(const struct intel_crtc_state *crtc_state)
1294 {
1295 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1296 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1297
1298 switch (crtc_state->gamma_mode) {
1299 case GAMMA_MODE_MODE_8BIT:
1300 i9xx_load_lut_8(crtc, post_csc_lut);
1301 break;
1302 case GAMMA_MODE_MODE_10BIT:
1303 i965_load_lut_10p6(crtc, post_csc_lut);
1304 break;
1305 default:
1306 MISSING_CASE(crtc_state->gamma_mode);
1307 break;
1308 }
1309 }
1310
ilk_lut_write(const struct intel_crtc_state * crtc_state,i915_reg_t reg,u32 val)1311 static void ilk_lut_write(const struct intel_crtc_state *crtc_state,
1312 i915_reg_t reg, u32 val)
1313 {
1314 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1315
1316 if (crtc_state->dsb_color_vblank)
1317 intel_dsb_reg_write(crtc_state->dsb_color_vblank, reg, val);
1318 else
1319 intel_de_write_fw(i915, reg, val);
1320 }
1321
ilk_load_lut_8(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob)1322 static void ilk_load_lut_8(const struct intel_crtc_state *crtc_state,
1323 const struct drm_property_blob *blob)
1324 {
1325 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1326 const struct drm_color_lut *lut;
1327 enum pipe pipe = crtc->pipe;
1328 int i;
1329
1330 if (!blob)
1331 return;
1332
1333 lut = blob->data;
1334
1335 /*
1336 * DSB fails to correctly load the legacy LUT
1337 * unless we either write each entry twice,
1338 * or use non-posted writes
1339 */
1340 if (crtc_state->dsb_color_vblank)
1341 intel_dsb_nonpost_start(crtc_state->dsb_color_vblank);
1342
1343 for (i = 0; i < 256; i++)
1344 ilk_lut_write(crtc_state, LGC_PALETTE(pipe, i),
1345 i9xx_lut_8(&lut[i]));
1346
1347 if (crtc_state->dsb_color_vblank)
1348 intel_dsb_nonpost_end(crtc_state->dsb_color_vblank);
1349 }
1350
ilk_load_lut_10(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob)1351 static void ilk_load_lut_10(const struct intel_crtc_state *crtc_state,
1352 const struct drm_property_blob *blob)
1353 {
1354 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1355 const struct drm_color_lut *lut = blob->data;
1356 int i, lut_size = drm_color_lut_size(blob);
1357 enum pipe pipe = crtc->pipe;
1358
1359 for (i = 0; i < lut_size; i++)
1360 ilk_lut_write(crtc_state, PREC_PALETTE(pipe, i),
1361 ilk_lut_10(&lut[i]));
1362 }
1363
ilk_load_luts(const struct intel_crtc_state * crtc_state)1364 static void ilk_load_luts(const struct intel_crtc_state *crtc_state)
1365 {
1366 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1367 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1368 const struct drm_property_blob *blob = post_csc_lut ?: pre_csc_lut;
1369
1370 switch (crtc_state->gamma_mode) {
1371 case GAMMA_MODE_MODE_8BIT:
1372 ilk_load_lut_8(crtc_state, blob);
1373 break;
1374 case GAMMA_MODE_MODE_10BIT:
1375 ilk_load_lut_10(crtc_state, blob);
1376 break;
1377 default:
1378 MISSING_CASE(crtc_state->gamma_mode);
1379 break;
1380 }
1381 }
1382
ivb_lut_10_size(u32 prec_index)1383 static int ivb_lut_10_size(u32 prec_index)
1384 {
1385 if (prec_index & PAL_PREC_SPLIT_MODE)
1386 return 512;
1387 else
1388 return 1024;
1389 }
1390
1391 /*
1392 * IVB/HSW Bspec / PAL_PREC_INDEX:
1393 * "Restriction : Index auto increment mode is not
1394 * supported and must not be enabled."
1395 */
ivb_load_lut_10(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob,u32 prec_index)1396 static void ivb_load_lut_10(const struct intel_crtc_state *crtc_state,
1397 const struct drm_property_blob *blob,
1398 u32 prec_index)
1399 {
1400 const struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1401 const struct drm_color_lut *lut = blob->data;
1402 int i, lut_size = drm_color_lut_size(blob);
1403 enum pipe pipe = crtc->pipe;
1404
1405 for (i = 0; i < lut_size; i++) {
1406 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1407 prec_index + i);
1408 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1409 ilk_lut_10(&lut[i]));
1410 }
1411
1412 /*
1413 * Reset the index, otherwise it prevents the legacy palette to be
1414 * written properly.
1415 */
1416 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1417 PAL_PREC_INDEX_VALUE(0));
1418 }
1419
1420 /* On BDW+ the index auto increment mode actually works */
bdw_load_lut_10(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob,u32 prec_index)1421 static void bdw_load_lut_10(const struct intel_crtc_state *crtc_state,
1422 const struct drm_property_blob *blob,
1423 u32 prec_index)
1424 {
1425 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1426 const struct drm_color_lut *lut = blob->data;
1427 int i, lut_size = drm_color_lut_size(blob);
1428 enum pipe pipe = crtc->pipe;
1429
1430 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1431 prec_index);
1432 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1433 PAL_PREC_AUTO_INCREMENT |
1434 prec_index);
1435
1436 for (i = 0; i < lut_size; i++)
1437 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1438 ilk_lut_10(&lut[i]));
1439
1440 /*
1441 * Reset the index, otherwise it prevents the legacy palette to be
1442 * written properly.
1443 */
1444 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1445 PAL_PREC_INDEX_VALUE(0));
1446 }
1447
ivb_load_lut_ext_max(const struct intel_crtc_state * crtc_state)1448 static void ivb_load_lut_ext_max(const struct intel_crtc_state *crtc_state)
1449 {
1450 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1451 enum pipe pipe = crtc->pipe;
1452
1453 /* Program the max register to clamp values > 1.0. */
1454 ilk_lut_write(crtc_state, PREC_PAL_EXT_GC_MAX(pipe, 0), 1 << 16);
1455 ilk_lut_write(crtc_state, PREC_PAL_EXT_GC_MAX(pipe, 1), 1 << 16);
1456 ilk_lut_write(crtc_state, PREC_PAL_EXT_GC_MAX(pipe, 2), 1 << 16);
1457 }
1458
glk_load_lut_ext2_max(const struct intel_crtc_state * crtc_state)1459 static void glk_load_lut_ext2_max(const struct intel_crtc_state *crtc_state)
1460 {
1461 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1462 enum pipe pipe = crtc->pipe;
1463
1464 /* Program the max register to clamp values > 1.0. */
1465 ilk_lut_write(crtc_state, PREC_PAL_EXT2_GC_MAX(pipe, 0), 1 << 16);
1466 ilk_lut_write(crtc_state, PREC_PAL_EXT2_GC_MAX(pipe, 1), 1 << 16);
1467 ilk_lut_write(crtc_state, PREC_PAL_EXT2_GC_MAX(pipe, 2), 1 << 16);
1468 }
1469
ivb_load_luts(const struct intel_crtc_state * crtc_state)1470 static void ivb_load_luts(const struct intel_crtc_state *crtc_state)
1471 {
1472 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1473 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1474 const struct drm_property_blob *blob = post_csc_lut ?: pre_csc_lut;
1475
1476 switch (crtc_state->gamma_mode) {
1477 case GAMMA_MODE_MODE_8BIT:
1478 ilk_load_lut_8(crtc_state, blob);
1479 break;
1480 case GAMMA_MODE_MODE_SPLIT:
1481 ivb_load_lut_10(crtc_state, pre_csc_lut, PAL_PREC_SPLIT_MODE |
1482 PAL_PREC_INDEX_VALUE(0));
1483 ivb_load_lut_ext_max(crtc_state);
1484 ivb_load_lut_10(crtc_state, post_csc_lut, PAL_PREC_SPLIT_MODE |
1485 PAL_PREC_INDEX_VALUE(512));
1486 break;
1487 case GAMMA_MODE_MODE_10BIT:
1488 ivb_load_lut_10(crtc_state, blob,
1489 PAL_PREC_INDEX_VALUE(0));
1490 ivb_load_lut_ext_max(crtc_state);
1491 break;
1492 default:
1493 MISSING_CASE(crtc_state->gamma_mode);
1494 break;
1495 }
1496 }
1497
bdw_load_luts(const struct intel_crtc_state * crtc_state)1498 static void bdw_load_luts(const struct intel_crtc_state *crtc_state)
1499 {
1500 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1501 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1502 const struct drm_property_blob *blob = post_csc_lut ?: pre_csc_lut;
1503
1504 switch (crtc_state->gamma_mode) {
1505 case GAMMA_MODE_MODE_8BIT:
1506 ilk_load_lut_8(crtc_state, blob);
1507 break;
1508 case GAMMA_MODE_MODE_SPLIT:
1509 bdw_load_lut_10(crtc_state, pre_csc_lut, PAL_PREC_SPLIT_MODE |
1510 PAL_PREC_INDEX_VALUE(0));
1511 ivb_load_lut_ext_max(crtc_state);
1512 bdw_load_lut_10(crtc_state, post_csc_lut, PAL_PREC_SPLIT_MODE |
1513 PAL_PREC_INDEX_VALUE(512));
1514 break;
1515 case GAMMA_MODE_MODE_10BIT:
1516 bdw_load_lut_10(crtc_state, blob,
1517 PAL_PREC_INDEX_VALUE(0));
1518 ivb_load_lut_ext_max(crtc_state);
1519 break;
1520 default:
1521 MISSING_CASE(crtc_state->gamma_mode);
1522 break;
1523 }
1524 }
1525
glk_degamma_lut_size(struct drm_i915_private * i915)1526 static int glk_degamma_lut_size(struct drm_i915_private *i915)
1527 {
1528 if (DISPLAY_VER(i915) >= 13)
1529 return 131;
1530 else
1531 return 35;
1532 }
1533
glk_degamma_lut(const struct drm_color_lut * color)1534 static u32 glk_degamma_lut(const struct drm_color_lut *color)
1535 {
1536 return color->green;
1537 }
1538
glk_degamma_lut_pack(struct drm_color_lut * entry,u32 val)1539 static void glk_degamma_lut_pack(struct drm_color_lut *entry, u32 val)
1540 {
1541 /* PRE_CSC_GAMC_DATA is 3.16, clamp to 0.16 */
1542 entry->red = entry->green = entry->blue = min(val, 0xffffu);
1543 }
1544
mtl_degamma_lut(const struct drm_color_lut * color)1545 static u32 mtl_degamma_lut(const struct drm_color_lut *color)
1546 {
1547 return drm_color_lut_extract(color->green, 24);
1548 }
1549
mtl_degamma_lut_pack(struct drm_color_lut * entry,u32 val)1550 static void mtl_degamma_lut_pack(struct drm_color_lut *entry, u32 val)
1551 {
1552 /* PRE_CSC_GAMC_DATA is 3.24, clamp to 0.16 */
1553 entry->red = entry->green = entry->blue =
1554 intel_color_lut_pack(min(val, 0xffffffu), 24);
1555 }
1556
glk_load_degamma_lut(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob)1557 static void glk_load_degamma_lut(const struct intel_crtc_state *crtc_state,
1558 const struct drm_property_blob *blob)
1559 {
1560 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1561 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1562 const struct drm_color_lut *lut = blob->data;
1563 int i, lut_size = drm_color_lut_size(blob);
1564 enum pipe pipe = crtc->pipe;
1565
1566 /*
1567 * When setting the auto-increment bit, the hardware seems to
1568 * ignore the index bits, so we need to reset it to index 0
1569 * separately.
1570 */
1571 ilk_lut_write(crtc_state, PRE_CSC_GAMC_INDEX(pipe),
1572 PRE_CSC_GAMC_INDEX_VALUE(0));
1573 ilk_lut_write(crtc_state, PRE_CSC_GAMC_INDEX(pipe),
1574 PRE_CSC_GAMC_AUTO_INCREMENT |
1575 PRE_CSC_GAMC_INDEX_VALUE(0));
1576
1577 for (i = 0; i < lut_size; i++) {
1578 /*
1579 * First lut_size entries represent range from 0 to 1.0
1580 * 3 additional lut entries will represent extended range
1581 * inputs 3.0 and 7.0 respectively, currently clamped
1582 * at 1.0. Since the precision is 16bit, the user
1583 * value can be directly filled to register.
1584 * The pipe degamma table in GLK+ onwards doesn't
1585 * support different values per channel, so this just
1586 * programs green value which will be equal to Red and
1587 * Blue into the lut registers.
1588 * ToDo: Extend to max 7.0. Enable 32 bit input value
1589 * as compared to just 16 to achieve this.
1590 */
1591 ilk_lut_write(crtc_state, PRE_CSC_GAMC_DATA(pipe),
1592 DISPLAY_VER(i915) >= 14 ?
1593 mtl_degamma_lut(&lut[i]) : glk_degamma_lut(&lut[i]));
1594 }
1595
1596 /* Clamp values > 1.0. */
1597 while (i++ < glk_degamma_lut_size(i915))
1598 ilk_lut_write(crtc_state, PRE_CSC_GAMC_DATA(pipe),
1599 DISPLAY_VER(i915) >= 14 ?
1600 1 << 24 : 1 << 16);
1601
1602 ilk_lut_write(crtc_state, PRE_CSC_GAMC_INDEX(pipe), 0);
1603 }
1604
glk_load_luts(const struct intel_crtc_state * crtc_state)1605 static void glk_load_luts(const struct intel_crtc_state *crtc_state)
1606 {
1607 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1608 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1609
1610 if (pre_csc_lut)
1611 glk_load_degamma_lut(crtc_state, pre_csc_lut);
1612
1613 switch (crtc_state->gamma_mode) {
1614 case GAMMA_MODE_MODE_8BIT:
1615 ilk_load_lut_8(crtc_state, post_csc_lut);
1616 break;
1617 case GAMMA_MODE_MODE_10BIT:
1618 bdw_load_lut_10(crtc_state, post_csc_lut, PAL_PREC_INDEX_VALUE(0));
1619 ivb_load_lut_ext_max(crtc_state);
1620 glk_load_lut_ext2_max(crtc_state);
1621 break;
1622 default:
1623 MISSING_CASE(crtc_state->gamma_mode);
1624 break;
1625 }
1626 }
1627
1628 static void
ivb_load_lut_max(const struct intel_crtc_state * crtc_state,const struct drm_color_lut * color)1629 ivb_load_lut_max(const struct intel_crtc_state *crtc_state,
1630 const struct drm_color_lut *color)
1631 {
1632 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1633 enum pipe pipe = crtc->pipe;
1634
1635 /* FIXME LUT entries are 16 bit only, so we can prog 0xFFFF max */
1636 ilk_lut_write(crtc_state, PREC_PAL_GC_MAX(pipe, 0), color->red);
1637 ilk_lut_write(crtc_state, PREC_PAL_GC_MAX(pipe, 1), color->green);
1638 ilk_lut_write(crtc_state, PREC_PAL_GC_MAX(pipe, 2), color->blue);
1639 }
1640
1641 static void
icl_program_gamma_superfine_segment(const struct intel_crtc_state * crtc_state)1642 icl_program_gamma_superfine_segment(const struct intel_crtc_state *crtc_state)
1643 {
1644 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1645 const struct drm_property_blob *blob = crtc_state->post_csc_lut;
1646 const struct drm_color_lut *lut = blob->data;
1647 enum pipe pipe = crtc->pipe;
1648 int i;
1649
1650 /*
1651 * Program Super Fine segment (let's call it seg1)...
1652 *
1653 * Super Fine segment's step is 1/(8 * 128 * 256) and it has
1654 * 9 entries, corresponding to values 0, 1/(8 * 128 * 256),
1655 * 2/(8 * 128 * 256) ... 8/(8 * 128 * 256).
1656 */
1657 ilk_lut_write(crtc_state, PREC_PAL_MULTI_SEG_INDEX(pipe),
1658 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
1659 ilk_lut_write(crtc_state, PREC_PAL_MULTI_SEG_INDEX(pipe),
1660 PAL_PREC_AUTO_INCREMENT |
1661 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
1662
1663 for (i = 0; i < 9; i++) {
1664 const struct drm_color_lut *entry = &lut[i];
1665
1666 ilk_lut_write(crtc_state, PREC_PAL_MULTI_SEG_DATA(pipe),
1667 ilk_lut_12p4_ldw(entry));
1668 ilk_lut_write(crtc_state, PREC_PAL_MULTI_SEG_DATA(pipe),
1669 ilk_lut_12p4_udw(entry));
1670 }
1671
1672 ilk_lut_write(crtc_state, PREC_PAL_MULTI_SEG_INDEX(pipe),
1673 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
1674 }
1675
1676 static void
icl_program_gamma_multi_segment(const struct intel_crtc_state * crtc_state)1677 icl_program_gamma_multi_segment(const struct intel_crtc_state *crtc_state)
1678 {
1679 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1680 const struct drm_property_blob *blob = crtc_state->post_csc_lut;
1681 const struct drm_color_lut *lut = blob->data;
1682 const struct drm_color_lut *entry;
1683 enum pipe pipe = crtc->pipe;
1684 int i;
1685
1686 /*
1687 * Program Fine segment (let's call it seg2)...
1688 *
1689 * Fine segment's step is 1/(128 * 256) i.e. 1/(128 * 256), 2/(128 * 256)
1690 * ... 256/(128 * 256). So in order to program fine segment of LUT we
1691 * need to pick every 8th entry in the LUT, and program 256 indexes.
1692 *
1693 * PAL_PREC_INDEX[0] and PAL_PREC_INDEX[1] map to seg2[1],
1694 * seg2[0] being unused by the hardware.
1695 */
1696 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1697 PAL_PREC_INDEX_VALUE(0));
1698 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1699 PAL_PREC_AUTO_INCREMENT |
1700 PAL_PREC_INDEX_VALUE(0));
1701
1702 for (i = 1; i < 257; i++) {
1703 entry = &lut[i * 8];
1704
1705 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1706 ilk_lut_12p4_ldw(entry));
1707 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1708 ilk_lut_12p4_udw(entry));
1709 }
1710
1711 /*
1712 * Program Coarse segment (let's call it seg3)...
1713 *
1714 * Coarse segment starts from index 0 and it's step is 1/256 ie 0,
1715 * 1/256, 2/256 ... 256/256. As per the description of each entry in LUT
1716 * above, we need to pick every (8 * 128)th entry in LUT, and
1717 * program 256 of those.
1718 *
1719 * Spec is not very clear about if entries seg3[0] and seg3[1] are
1720 * being used or not, but we still need to program these to advance
1721 * the index.
1722 */
1723 for (i = 0; i < 256; i++) {
1724 entry = &lut[i * 8 * 128];
1725
1726 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1727 ilk_lut_12p4_ldw(entry));
1728 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1729 ilk_lut_12p4_udw(entry));
1730 }
1731
1732 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1733 PAL_PREC_INDEX_VALUE(0));
1734
1735 /* The last entry in the LUT is to be programmed in GCMAX */
1736 entry = &lut[256 * 8 * 128];
1737 ivb_load_lut_max(crtc_state, entry);
1738 }
1739
icl_load_luts(const struct intel_crtc_state * crtc_state)1740 static void icl_load_luts(const struct intel_crtc_state *crtc_state)
1741 {
1742 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1743 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1744
1745 if (pre_csc_lut)
1746 glk_load_degamma_lut(crtc_state, pre_csc_lut);
1747
1748 switch (crtc_state->gamma_mode & GAMMA_MODE_MODE_MASK) {
1749 case GAMMA_MODE_MODE_8BIT:
1750 ilk_load_lut_8(crtc_state, post_csc_lut);
1751 break;
1752 case GAMMA_MODE_MODE_12BIT_MULTI_SEG:
1753 icl_program_gamma_superfine_segment(crtc_state);
1754 icl_program_gamma_multi_segment(crtc_state);
1755 ivb_load_lut_ext_max(crtc_state);
1756 glk_load_lut_ext2_max(crtc_state);
1757 break;
1758 case GAMMA_MODE_MODE_10BIT:
1759 bdw_load_lut_10(crtc_state, post_csc_lut, PAL_PREC_INDEX_VALUE(0));
1760 ivb_load_lut_ext_max(crtc_state);
1761 glk_load_lut_ext2_max(crtc_state);
1762 break;
1763 default:
1764 MISSING_CASE(crtc_state->gamma_mode);
1765 break;
1766 }
1767 }
1768
vlv_load_luts(const struct intel_crtc_state * crtc_state)1769 static void vlv_load_luts(const struct intel_crtc_state *crtc_state)
1770 {
1771 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1772
1773 if (crtc_state->wgc_enable)
1774 vlv_load_wgc_csc(crtc, &crtc_state->csc);
1775
1776 i965_load_luts(crtc_state);
1777 }
1778
chv_cgm_degamma_ldw(const struct drm_color_lut * color)1779 static u32 chv_cgm_degamma_ldw(const struct drm_color_lut *color)
1780 {
1781 return REG_FIELD_PREP(CGM_PIPE_DEGAMMA_GREEN_LDW_MASK, drm_color_lut_extract(color->green, 14)) |
1782 REG_FIELD_PREP(CGM_PIPE_DEGAMMA_BLUE_LDW_MASK, drm_color_lut_extract(color->blue, 14));
1783 }
1784
chv_cgm_degamma_udw(const struct drm_color_lut * color)1785 static u32 chv_cgm_degamma_udw(const struct drm_color_lut *color)
1786 {
1787 return REG_FIELD_PREP(CGM_PIPE_DEGAMMA_RED_UDW_MASK, drm_color_lut_extract(color->red, 14));
1788 }
1789
chv_cgm_degamma_pack(struct drm_color_lut * entry,u32 ldw,u32 udw)1790 static void chv_cgm_degamma_pack(struct drm_color_lut *entry, u32 ldw, u32 udw)
1791 {
1792 entry->green = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_DEGAMMA_GREEN_LDW_MASK, ldw), 14);
1793 entry->blue = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_DEGAMMA_BLUE_LDW_MASK, ldw), 14);
1794 entry->red = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_DEGAMMA_RED_UDW_MASK, udw), 14);
1795 }
1796
chv_load_cgm_degamma(struct intel_crtc * crtc,const struct drm_property_blob * blob)1797 static void chv_load_cgm_degamma(struct intel_crtc *crtc,
1798 const struct drm_property_blob *blob)
1799 {
1800 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1801 const struct drm_color_lut *lut = blob->data;
1802 int i, lut_size = drm_color_lut_size(blob);
1803 enum pipe pipe = crtc->pipe;
1804
1805 for (i = 0; i < lut_size; i++) {
1806 intel_de_write_fw(i915, CGM_PIPE_DEGAMMA(pipe, i, 0),
1807 chv_cgm_degamma_ldw(&lut[i]));
1808 intel_de_write_fw(i915, CGM_PIPE_DEGAMMA(pipe, i, 1),
1809 chv_cgm_degamma_udw(&lut[i]));
1810 }
1811 }
1812
chv_cgm_gamma_ldw(const struct drm_color_lut * color)1813 static u32 chv_cgm_gamma_ldw(const struct drm_color_lut *color)
1814 {
1815 return REG_FIELD_PREP(CGM_PIPE_GAMMA_GREEN_LDW_MASK, drm_color_lut_extract(color->green, 10)) |
1816 REG_FIELD_PREP(CGM_PIPE_GAMMA_BLUE_LDW_MASK, drm_color_lut_extract(color->blue, 10));
1817 }
1818
chv_cgm_gamma_udw(const struct drm_color_lut * color)1819 static u32 chv_cgm_gamma_udw(const struct drm_color_lut *color)
1820 {
1821 return REG_FIELD_PREP(CGM_PIPE_GAMMA_RED_UDW_MASK, drm_color_lut_extract(color->red, 10));
1822 }
1823
chv_cgm_gamma_pack(struct drm_color_lut * entry,u32 ldw,u32 udw)1824 static void chv_cgm_gamma_pack(struct drm_color_lut *entry, u32 ldw, u32 udw)
1825 {
1826 entry->green = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_GAMMA_GREEN_LDW_MASK, ldw), 10);
1827 entry->blue = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_GAMMA_BLUE_LDW_MASK, ldw), 10);
1828 entry->red = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_GAMMA_RED_UDW_MASK, udw), 10);
1829 }
1830
chv_load_cgm_gamma(struct intel_crtc * crtc,const struct drm_property_blob * blob)1831 static void chv_load_cgm_gamma(struct intel_crtc *crtc,
1832 const struct drm_property_blob *blob)
1833 {
1834 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1835 const struct drm_color_lut *lut = blob->data;
1836 int i, lut_size = drm_color_lut_size(blob);
1837 enum pipe pipe = crtc->pipe;
1838
1839 for (i = 0; i < lut_size; i++) {
1840 intel_de_write_fw(i915, CGM_PIPE_GAMMA(pipe, i, 0),
1841 chv_cgm_gamma_ldw(&lut[i]));
1842 intel_de_write_fw(i915, CGM_PIPE_GAMMA(pipe, i, 1),
1843 chv_cgm_gamma_udw(&lut[i]));
1844 }
1845 }
1846
chv_load_luts(const struct intel_crtc_state * crtc_state)1847 static void chv_load_luts(const struct intel_crtc_state *crtc_state)
1848 {
1849 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1850 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1851 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1852 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1853
1854 if (crtc_state->cgm_mode & CGM_PIPE_MODE_CSC)
1855 chv_load_cgm_csc(crtc, &crtc_state->csc);
1856
1857 if (crtc_state->cgm_mode & CGM_PIPE_MODE_DEGAMMA)
1858 chv_load_cgm_degamma(crtc, pre_csc_lut);
1859
1860 if (crtc_state->cgm_mode & CGM_PIPE_MODE_GAMMA)
1861 chv_load_cgm_gamma(crtc, post_csc_lut);
1862 else
1863 i965_load_luts(crtc_state);
1864
1865 intel_de_write_fw(i915, CGM_PIPE_MODE(crtc->pipe),
1866 crtc_state->cgm_mode);
1867 }
1868
intel_color_load_luts(const struct intel_crtc_state * crtc_state)1869 void intel_color_load_luts(const struct intel_crtc_state *crtc_state)
1870 {
1871 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1872
1873 if (crtc_state->dsb_color_vblank)
1874 return;
1875
1876 i915->display.funcs.color->load_luts(crtc_state);
1877 }
1878
intel_color_commit_noarm(const struct intel_crtc_state * crtc_state)1879 void intel_color_commit_noarm(const struct intel_crtc_state *crtc_state)
1880 {
1881 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1882
1883 if (i915->display.funcs.color->color_commit_noarm)
1884 i915->display.funcs.color->color_commit_noarm(crtc_state);
1885 }
1886
intel_color_commit_arm(const struct intel_crtc_state * crtc_state)1887 void intel_color_commit_arm(const struct intel_crtc_state *crtc_state)
1888 {
1889 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1890
1891 i915->display.funcs.color->color_commit_arm(crtc_state);
1892
1893 if (crtc_state->dsb_color_commit)
1894 intel_dsb_commit(crtc_state->dsb_color_commit, false);
1895 }
1896
intel_color_post_update(const struct intel_crtc_state * crtc_state)1897 void intel_color_post_update(const struct intel_crtc_state *crtc_state)
1898 {
1899 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1900
1901 if (i915->display.funcs.color->color_post_update)
1902 i915->display.funcs.color->color_post_update(crtc_state);
1903 }
1904
intel_color_prepare_commit(struct intel_atomic_state * state,struct intel_crtc * crtc)1905 void intel_color_prepare_commit(struct intel_atomic_state *state,
1906 struct intel_crtc *crtc)
1907 {
1908 struct drm_i915_private *i915 = to_i915(state->base.dev);
1909 struct intel_crtc_state *crtc_state =
1910 intel_atomic_get_new_crtc_state(state, crtc);
1911
1912 if (!crtc_state->hw.active ||
1913 intel_crtc_needs_modeset(crtc_state))
1914 return;
1915
1916 if (!intel_crtc_needs_color_update(crtc_state))
1917 return;
1918
1919 if (!crtc_state->pre_csc_lut && !crtc_state->post_csc_lut)
1920 return;
1921
1922 crtc_state->dsb_color_vblank = intel_dsb_prepare(state, crtc, INTEL_DSB_1, 1024);
1923 if (!crtc_state->dsb_color_vblank)
1924 return;
1925
1926 i915->display.funcs.color->load_luts(crtc_state);
1927
1928 intel_dsb_finish(crtc_state->dsb_color_vblank);
1929
1930 crtc_state->dsb_color_commit = intel_dsb_prepare(state, crtc, INTEL_DSB_0, 16);
1931 if (!crtc_state->dsb_color_commit) {
1932 intel_dsb_cleanup(crtc_state->dsb_color_vblank);
1933 crtc_state->dsb_color_vblank = NULL;
1934 return;
1935 }
1936
1937 intel_dsb_chain(state, crtc_state->dsb_color_commit,
1938 crtc_state->dsb_color_vblank, true);
1939
1940 intel_dsb_finish(crtc_state->dsb_color_commit);
1941 }
1942
intel_color_cleanup_commit(struct intel_crtc_state * crtc_state)1943 void intel_color_cleanup_commit(struct intel_crtc_state *crtc_state)
1944 {
1945 if (crtc_state->dsb_color_commit) {
1946 intel_dsb_cleanup(crtc_state->dsb_color_commit);
1947 crtc_state->dsb_color_commit = NULL;
1948 }
1949
1950 if (crtc_state->dsb_color_vblank) {
1951 intel_dsb_cleanup(crtc_state->dsb_color_vblank);
1952 crtc_state->dsb_color_vblank = NULL;
1953 }
1954 }
1955
intel_color_wait_commit(const struct intel_crtc_state * crtc_state)1956 void intel_color_wait_commit(const struct intel_crtc_state *crtc_state)
1957 {
1958 if (crtc_state->dsb_color_commit)
1959 intel_dsb_wait(crtc_state->dsb_color_commit);
1960 if (crtc_state->dsb_color_vblank)
1961 intel_dsb_wait(crtc_state->dsb_color_vblank);
1962 }
1963
intel_color_uses_dsb(const struct intel_crtc_state * crtc_state)1964 bool intel_color_uses_dsb(const struct intel_crtc_state *crtc_state)
1965 {
1966 return crtc_state->dsb_color_vblank;
1967 }
1968
intel_can_preload_luts(struct intel_atomic_state * state,struct intel_crtc * crtc)1969 static bool intel_can_preload_luts(struct intel_atomic_state *state,
1970 struct intel_crtc *crtc)
1971 {
1972 const struct intel_crtc_state *old_crtc_state =
1973 intel_atomic_get_old_crtc_state(state, crtc);
1974
1975 return !old_crtc_state->post_csc_lut &&
1976 !old_crtc_state->pre_csc_lut;
1977 }
1978
vlv_can_preload_luts(struct intel_atomic_state * state,struct intel_crtc * crtc)1979 static bool vlv_can_preload_luts(struct intel_atomic_state *state,
1980 struct intel_crtc *crtc)
1981 {
1982 const struct intel_crtc_state *old_crtc_state =
1983 intel_atomic_get_old_crtc_state(state, crtc);
1984
1985 return !old_crtc_state->wgc_enable &&
1986 !old_crtc_state->post_csc_lut;
1987 }
1988
chv_can_preload_luts(struct intel_atomic_state * state,struct intel_crtc * crtc)1989 static bool chv_can_preload_luts(struct intel_atomic_state *state,
1990 struct intel_crtc *crtc)
1991 {
1992 const struct intel_crtc_state *old_crtc_state =
1993 intel_atomic_get_old_crtc_state(state, crtc);
1994 const struct intel_crtc_state *new_crtc_state =
1995 intel_atomic_get_new_crtc_state(state, crtc);
1996
1997 /*
1998 * CGM_PIPE_MODE is itself single buffered. We'd have to
1999 * somehow split it out from chv_load_luts() if we wanted
2000 * the ability to preload the CGM LUTs/CSC without tearing.
2001 */
2002 if (old_crtc_state->cgm_mode || new_crtc_state->cgm_mode)
2003 return false;
2004
2005 return vlv_can_preload_luts(state, crtc);
2006 }
2007
intel_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2008 int intel_color_check(struct intel_atomic_state *state,
2009 struct intel_crtc *crtc)
2010 {
2011 struct drm_i915_private *i915 = to_i915(state->base.dev);
2012 const struct intel_crtc_state *old_crtc_state =
2013 intel_atomic_get_old_crtc_state(state, crtc);
2014 struct intel_crtc_state *new_crtc_state =
2015 intel_atomic_get_new_crtc_state(state, crtc);
2016
2017 /*
2018 * May need to update pipe gamma enable bits
2019 * when C8 planes are getting enabled/disabled.
2020 */
2021 if (!old_crtc_state->c8_planes != !new_crtc_state->c8_planes)
2022 new_crtc_state->uapi.color_mgmt_changed = true;
2023
2024 if (!intel_crtc_needs_color_update(new_crtc_state))
2025 return 0;
2026
2027 return i915->display.funcs.color->color_check(state, crtc);
2028 }
2029
intel_color_get_config(struct intel_crtc_state * crtc_state)2030 void intel_color_get_config(struct intel_crtc_state *crtc_state)
2031 {
2032 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2033
2034 if (i915->display.funcs.color->get_config)
2035 i915->display.funcs.color->get_config(crtc_state);
2036
2037 i915->display.funcs.color->read_luts(crtc_state);
2038
2039 if (i915->display.funcs.color->read_csc)
2040 i915->display.funcs.color->read_csc(crtc_state);
2041 }
2042
intel_color_lut_equal(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob1,const struct drm_property_blob * blob2,bool is_pre_csc_lut)2043 bool intel_color_lut_equal(const struct intel_crtc_state *crtc_state,
2044 const struct drm_property_blob *blob1,
2045 const struct drm_property_blob *blob2,
2046 bool is_pre_csc_lut)
2047 {
2048 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2049
2050 /*
2051 * FIXME c8_planes readout missing thus
2052 * .read_luts() doesn't read out post_csc_lut.
2053 */
2054 if (!is_pre_csc_lut && crtc_state->c8_planes)
2055 return true;
2056
2057 return i915->display.funcs.color->lut_equal(crtc_state, blob1, blob2,
2058 is_pre_csc_lut);
2059 }
2060
need_plane_update(struct intel_plane * plane,const struct intel_crtc_state * crtc_state)2061 static bool need_plane_update(struct intel_plane *plane,
2062 const struct intel_crtc_state *crtc_state)
2063 {
2064 struct drm_i915_private *i915 = to_i915(plane->base.dev);
2065
2066 /*
2067 * On pre-SKL the pipe gamma enable and pipe csc enable for
2068 * the pipe bottom color are configured via the primary plane.
2069 * We have to reconfigure that even if the plane is inactive.
2070 */
2071 return crtc_state->active_planes & BIT(plane->id) ||
2072 (DISPLAY_VER(i915) < 9 &&
2073 plane->id == PLANE_PRIMARY);
2074 }
2075
2076 static int
intel_color_add_affected_planes(struct intel_atomic_state * state,struct intel_crtc * crtc)2077 intel_color_add_affected_planes(struct intel_atomic_state *state,
2078 struct intel_crtc *crtc)
2079 {
2080 struct drm_i915_private *i915 = to_i915(state->base.dev);
2081 const struct intel_crtc_state *old_crtc_state =
2082 intel_atomic_get_old_crtc_state(state, crtc);
2083 struct intel_crtc_state *new_crtc_state =
2084 intel_atomic_get_new_crtc_state(state, crtc);
2085 struct intel_plane *plane;
2086
2087 if (!new_crtc_state->hw.active ||
2088 intel_crtc_needs_modeset(new_crtc_state))
2089 return 0;
2090
2091 if (new_crtc_state->gamma_enable == old_crtc_state->gamma_enable &&
2092 new_crtc_state->csc_enable == old_crtc_state->csc_enable)
2093 return 0;
2094
2095 for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
2096 struct intel_plane_state *plane_state;
2097
2098 if (!need_plane_update(plane, new_crtc_state))
2099 continue;
2100
2101 plane_state = intel_atomic_get_plane_state(state, plane);
2102 if (IS_ERR(plane_state))
2103 return PTR_ERR(plane_state);
2104
2105 new_crtc_state->update_planes |= BIT(plane->id);
2106 new_crtc_state->async_flip_planes = 0;
2107 new_crtc_state->do_async_flip = false;
2108
2109 /* plane control register changes blocked by CxSR */
2110 if (HAS_GMCH(i915))
2111 new_crtc_state->disable_cxsr = true;
2112 }
2113
2114 return 0;
2115 }
2116
intel_gamma_lut_tests(const struct intel_crtc_state * crtc_state)2117 static u32 intel_gamma_lut_tests(const struct intel_crtc_state *crtc_state)
2118 {
2119 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2120 const struct drm_property_blob *gamma_lut = crtc_state->hw.gamma_lut;
2121
2122 if (lut_is_legacy(gamma_lut))
2123 return 0;
2124
2125 return DISPLAY_INFO(i915)->color.gamma_lut_tests;
2126 }
2127
intel_degamma_lut_tests(const struct intel_crtc_state * crtc_state)2128 static u32 intel_degamma_lut_tests(const struct intel_crtc_state *crtc_state)
2129 {
2130 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2131
2132 return DISPLAY_INFO(i915)->color.degamma_lut_tests;
2133 }
2134
intel_gamma_lut_size(const struct intel_crtc_state * crtc_state)2135 static int intel_gamma_lut_size(const struct intel_crtc_state *crtc_state)
2136 {
2137 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2138 const struct drm_property_blob *gamma_lut = crtc_state->hw.gamma_lut;
2139
2140 if (lut_is_legacy(gamma_lut))
2141 return LEGACY_LUT_LENGTH;
2142
2143 return DISPLAY_INFO(i915)->color.gamma_lut_size;
2144 }
2145
intel_degamma_lut_size(const struct intel_crtc_state * crtc_state)2146 static u32 intel_degamma_lut_size(const struct intel_crtc_state *crtc_state)
2147 {
2148 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2149
2150 return DISPLAY_INFO(i915)->color.degamma_lut_size;
2151 }
2152
check_lut_size(struct drm_i915_private * i915,const struct drm_property_blob * lut,int expected)2153 static int check_lut_size(struct drm_i915_private *i915,
2154 const struct drm_property_blob *lut, int expected)
2155 {
2156 int len;
2157
2158 if (!lut)
2159 return 0;
2160
2161 len = drm_color_lut_size(lut);
2162 if (len != expected) {
2163 drm_dbg_kms(&i915->drm, "Invalid LUT size; got %d, expected %d\n",
2164 len, expected);
2165 return -EINVAL;
2166 }
2167
2168 return 0;
2169 }
2170
_check_luts(const struct intel_crtc_state * crtc_state,u32 degamma_tests,u32 gamma_tests)2171 static int _check_luts(const struct intel_crtc_state *crtc_state,
2172 u32 degamma_tests, u32 gamma_tests)
2173 {
2174 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2175 const struct drm_property_blob *gamma_lut = crtc_state->hw.gamma_lut;
2176 const struct drm_property_blob *degamma_lut = crtc_state->hw.degamma_lut;
2177 int gamma_length, degamma_length;
2178
2179 /* C8 relies on its palette being stored in the legacy LUT */
2180 if (crtc_state->c8_planes && !lut_is_legacy(crtc_state->hw.gamma_lut)) {
2181 drm_dbg_kms(&i915->drm,
2182 "C8 pixelformat requires the legacy LUT\n");
2183 return -EINVAL;
2184 }
2185
2186 degamma_length = intel_degamma_lut_size(crtc_state);
2187 gamma_length = intel_gamma_lut_size(crtc_state);
2188
2189 if (check_lut_size(i915, degamma_lut, degamma_length) ||
2190 check_lut_size(i915, gamma_lut, gamma_length))
2191 return -EINVAL;
2192
2193 if (drm_color_lut_check(degamma_lut, degamma_tests) ||
2194 drm_color_lut_check(gamma_lut, gamma_tests))
2195 return -EINVAL;
2196
2197 return 0;
2198 }
2199
check_luts(const struct intel_crtc_state * crtc_state)2200 static int check_luts(const struct intel_crtc_state *crtc_state)
2201 {
2202 return _check_luts(crtc_state,
2203 intel_degamma_lut_tests(crtc_state),
2204 intel_gamma_lut_tests(crtc_state));
2205 }
2206
i9xx_gamma_mode(struct intel_crtc_state * crtc_state)2207 static u32 i9xx_gamma_mode(struct intel_crtc_state *crtc_state)
2208 {
2209 if (!crtc_state->gamma_enable ||
2210 lut_is_legacy(crtc_state->hw.gamma_lut))
2211 return GAMMA_MODE_MODE_8BIT;
2212 else
2213 return GAMMA_MODE_MODE_10BIT;
2214 }
2215
i9xx_lut_10_diff(u16 a,u16 b)2216 static int i9xx_lut_10_diff(u16 a, u16 b)
2217 {
2218 return drm_color_lut_extract(a, 10) -
2219 drm_color_lut_extract(b, 10);
2220 }
2221
i9xx_check_lut_10(struct drm_i915_private * dev_priv,const struct drm_property_blob * blob)2222 static int i9xx_check_lut_10(struct drm_i915_private *dev_priv,
2223 const struct drm_property_blob *blob)
2224 {
2225 const struct drm_color_lut *lut = blob->data;
2226 int lut_size = drm_color_lut_size(blob);
2227 const struct drm_color_lut *a = &lut[lut_size - 2];
2228 const struct drm_color_lut *b = &lut[lut_size - 1];
2229
2230 if (i9xx_lut_10_diff(b->red, a->red) > 0x7f ||
2231 i9xx_lut_10_diff(b->green, a->green) > 0x7f ||
2232 i9xx_lut_10_diff(b->blue, a->blue) > 0x7f) {
2233 drm_dbg_kms(&dev_priv->drm, "Last gamma LUT entry exceeds max slope\n");
2234 return -EINVAL;
2235 }
2236
2237 return 0;
2238 }
2239
intel_color_assert_luts(const struct intel_crtc_state * crtc_state)2240 void intel_color_assert_luts(const struct intel_crtc_state *crtc_state)
2241 {
2242 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2243
2244 /* make sure {pre,post}_csc_lut were correctly assigned */
2245 if (DISPLAY_VER(i915) >= 11 || HAS_GMCH(i915)) {
2246 drm_WARN_ON(&i915->drm,
2247 crtc_state->pre_csc_lut != crtc_state->hw.degamma_lut);
2248 drm_WARN_ON(&i915->drm,
2249 crtc_state->post_csc_lut != crtc_state->hw.gamma_lut);
2250 } else if (DISPLAY_VER(i915) == 10) {
2251 drm_WARN_ON(&i915->drm,
2252 crtc_state->post_csc_lut == crtc_state->hw.gamma_lut &&
2253 crtc_state->pre_csc_lut != crtc_state->hw.degamma_lut &&
2254 crtc_state->pre_csc_lut != i915->display.color.glk_linear_degamma_lut);
2255 drm_WARN_ON(&i915->drm,
2256 !ilk_lut_limited_range(crtc_state) &&
2257 crtc_state->post_csc_lut != NULL &&
2258 crtc_state->post_csc_lut != crtc_state->hw.gamma_lut);
2259 } else if (crtc_state->gamma_mode != GAMMA_MODE_MODE_SPLIT) {
2260 drm_WARN_ON(&i915->drm,
2261 crtc_state->pre_csc_lut != crtc_state->hw.degamma_lut &&
2262 crtc_state->pre_csc_lut != crtc_state->hw.gamma_lut);
2263 drm_WARN_ON(&i915->drm,
2264 !ilk_lut_limited_range(crtc_state) &&
2265 crtc_state->post_csc_lut != crtc_state->hw.degamma_lut &&
2266 crtc_state->post_csc_lut != crtc_state->hw.gamma_lut);
2267 }
2268 }
2269
intel_assign_luts(struct intel_crtc_state * crtc_state)2270 static void intel_assign_luts(struct intel_crtc_state *crtc_state)
2271 {
2272 drm_property_replace_blob(&crtc_state->pre_csc_lut,
2273 crtc_state->hw.degamma_lut);
2274 drm_property_replace_blob(&crtc_state->post_csc_lut,
2275 crtc_state->hw.gamma_lut);
2276 }
2277
i9xx_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2278 static int i9xx_color_check(struct intel_atomic_state *state,
2279 struct intel_crtc *crtc)
2280 {
2281 struct drm_i915_private *i915 = to_i915(state->base.dev);
2282 struct intel_crtc_state *crtc_state =
2283 intel_atomic_get_new_crtc_state(state, crtc);
2284 int ret;
2285
2286 ret = check_luts(crtc_state);
2287 if (ret)
2288 return ret;
2289
2290 crtc_state->gamma_enable =
2291 crtc_state->hw.gamma_lut &&
2292 !crtc_state->c8_planes;
2293
2294 crtc_state->gamma_mode = i9xx_gamma_mode(crtc_state);
2295
2296 if (DISPLAY_VER(i915) < 4 &&
2297 crtc_state->gamma_mode == GAMMA_MODE_MODE_10BIT) {
2298 ret = i9xx_check_lut_10(i915, crtc_state->hw.gamma_lut);
2299 if (ret)
2300 return ret;
2301 }
2302
2303 ret = intel_color_add_affected_planes(state, crtc);
2304 if (ret)
2305 return ret;
2306
2307 intel_assign_luts(crtc_state);
2308
2309 crtc_state->preload_luts = intel_can_preload_luts(state, crtc);
2310
2311 return 0;
2312 }
2313
2314 /*
2315 * VLV color pipeline:
2316 * u0.10 -> WGC csc -> u0.10 -> pipe gamma -> u0.10
2317 */
vlv_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2318 static int vlv_color_check(struct intel_atomic_state *state,
2319 struct intel_crtc *crtc)
2320 {
2321 struct intel_crtc_state *crtc_state =
2322 intel_atomic_get_new_crtc_state(state, crtc);
2323 int ret;
2324
2325 ret = check_luts(crtc_state);
2326 if (ret)
2327 return ret;
2328
2329 crtc_state->gamma_enable =
2330 crtc_state->hw.gamma_lut &&
2331 !crtc_state->c8_planes;
2332
2333 crtc_state->gamma_mode = i9xx_gamma_mode(crtc_state);
2334
2335 crtc_state->wgc_enable = crtc_state->hw.ctm;
2336
2337 ret = intel_color_add_affected_planes(state, crtc);
2338 if (ret)
2339 return ret;
2340
2341 intel_assign_luts(crtc_state);
2342
2343 vlv_assign_csc(crtc_state);
2344
2345 crtc_state->preload_luts = vlv_can_preload_luts(state, crtc);
2346
2347 return 0;
2348 }
2349
chv_cgm_mode(const struct intel_crtc_state * crtc_state)2350 static u32 chv_cgm_mode(const struct intel_crtc_state *crtc_state)
2351 {
2352 u32 cgm_mode = 0;
2353
2354 if (crtc_state->hw.degamma_lut)
2355 cgm_mode |= CGM_PIPE_MODE_DEGAMMA;
2356 if (crtc_state->hw.ctm)
2357 cgm_mode |= CGM_PIPE_MODE_CSC;
2358 if (crtc_state->hw.gamma_lut &&
2359 !lut_is_legacy(crtc_state->hw.gamma_lut))
2360 cgm_mode |= CGM_PIPE_MODE_GAMMA;
2361
2362 /*
2363 * Toggling the CGM CSC on/off outside of the tiny window
2364 * between start of vblank and frame start causes underruns.
2365 * Always enable the CGM CSC as a workaround.
2366 */
2367 cgm_mode |= CGM_PIPE_MODE_CSC;
2368
2369 return cgm_mode;
2370 }
2371
2372 /*
2373 * CHV color pipeline:
2374 * u0.10 -> CGM degamma -> u0.14 -> CGM csc -> u0.14 -> CGM gamma ->
2375 * u0.10 -> WGC csc -> u0.10 -> pipe gamma -> u0.10
2376 *
2377 * We always bypass the WGC csc and use the CGM csc
2378 * instead since it has degamma and better precision.
2379 */
chv_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2380 static int chv_color_check(struct intel_atomic_state *state,
2381 struct intel_crtc *crtc)
2382 {
2383 struct intel_crtc_state *crtc_state =
2384 intel_atomic_get_new_crtc_state(state, crtc);
2385 int ret;
2386
2387 ret = check_luts(crtc_state);
2388 if (ret)
2389 return ret;
2390
2391 /*
2392 * Pipe gamma will be used only for the legacy LUT.
2393 * Otherwise we bypass it and use the CGM gamma instead.
2394 */
2395 crtc_state->gamma_enable =
2396 lut_is_legacy(crtc_state->hw.gamma_lut) &&
2397 !crtc_state->c8_planes;
2398
2399 crtc_state->gamma_mode = GAMMA_MODE_MODE_8BIT;
2400
2401 crtc_state->cgm_mode = chv_cgm_mode(crtc_state);
2402
2403 /*
2404 * We always bypass the WGC CSC and use the CGM CSC
2405 * instead since it has degamma and better precision.
2406 */
2407 crtc_state->wgc_enable = false;
2408
2409 ret = intel_color_add_affected_planes(state, crtc);
2410 if (ret)
2411 return ret;
2412
2413 intel_assign_luts(crtc_state);
2414
2415 chv_assign_csc(crtc_state);
2416
2417 crtc_state->preload_luts = chv_can_preload_luts(state, crtc);
2418
2419 return 0;
2420 }
2421
ilk_gamma_enable(const struct intel_crtc_state * crtc_state)2422 static bool ilk_gamma_enable(const struct intel_crtc_state *crtc_state)
2423 {
2424 return (crtc_state->hw.gamma_lut ||
2425 crtc_state->hw.degamma_lut) &&
2426 !crtc_state->c8_planes;
2427 }
2428
ilk_csc_enable(const struct intel_crtc_state * crtc_state)2429 static bool ilk_csc_enable(const struct intel_crtc_state *crtc_state)
2430 {
2431 return crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
2432 ilk_csc_limited_range(crtc_state) ||
2433 crtc_state->hw.ctm;
2434 }
2435
ilk_gamma_mode(const struct intel_crtc_state * crtc_state)2436 static u32 ilk_gamma_mode(const struct intel_crtc_state *crtc_state)
2437 {
2438 if (!crtc_state->gamma_enable ||
2439 lut_is_legacy(crtc_state->hw.gamma_lut))
2440 return GAMMA_MODE_MODE_8BIT;
2441 else
2442 return GAMMA_MODE_MODE_10BIT;
2443 }
2444
ilk_csc_mode(const struct intel_crtc_state * crtc_state)2445 static u32 ilk_csc_mode(const struct intel_crtc_state *crtc_state)
2446 {
2447 /*
2448 * CSC comes after the LUT in RGB->YCbCr mode.
2449 * RGB->YCbCr needs the limited range offsets added to
2450 * the output. RGB limited range output is handled by
2451 * the hw automagically elsewhere.
2452 */
2453 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
2454 return CSC_BLACK_SCREEN_OFFSET;
2455
2456 if (crtc_state->hw.degamma_lut)
2457 return CSC_MODE_YUV_TO_RGB;
2458
2459 return CSC_MODE_YUV_TO_RGB |
2460 CSC_POSITION_BEFORE_GAMMA;
2461 }
2462
ilk_assign_luts(struct intel_crtc_state * crtc_state)2463 static int ilk_assign_luts(struct intel_crtc_state *crtc_state)
2464 {
2465 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2466
2467 if (ilk_lut_limited_range(crtc_state)) {
2468 struct drm_property_blob *gamma_lut;
2469
2470 gamma_lut = create_resized_lut(i915, crtc_state->hw.gamma_lut,
2471 drm_color_lut_size(crtc_state->hw.gamma_lut),
2472 true);
2473 if (IS_ERR(gamma_lut))
2474 return PTR_ERR(gamma_lut);
2475
2476 drm_property_replace_blob(&crtc_state->post_csc_lut, gamma_lut);
2477
2478 drm_property_blob_put(gamma_lut);
2479
2480 drm_property_replace_blob(&crtc_state->pre_csc_lut, crtc_state->hw.degamma_lut);
2481
2482 return 0;
2483 }
2484
2485 if (crtc_state->hw.degamma_lut ||
2486 crtc_state->csc_mode & CSC_POSITION_BEFORE_GAMMA) {
2487 drm_property_replace_blob(&crtc_state->pre_csc_lut,
2488 crtc_state->hw.degamma_lut);
2489 drm_property_replace_blob(&crtc_state->post_csc_lut,
2490 crtc_state->hw.gamma_lut);
2491 } else {
2492 drm_property_replace_blob(&crtc_state->pre_csc_lut,
2493 crtc_state->hw.gamma_lut);
2494 drm_property_replace_blob(&crtc_state->post_csc_lut,
2495 NULL);
2496 }
2497
2498 return 0;
2499 }
2500
ilk_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2501 static int ilk_color_check(struct intel_atomic_state *state,
2502 struct intel_crtc *crtc)
2503 {
2504 struct drm_i915_private *i915 = to_i915(state->base.dev);
2505 struct intel_crtc_state *crtc_state =
2506 intel_atomic_get_new_crtc_state(state, crtc);
2507 int ret;
2508
2509 ret = check_luts(crtc_state);
2510 if (ret)
2511 return ret;
2512
2513 if (crtc_state->hw.degamma_lut && crtc_state->hw.gamma_lut) {
2514 drm_dbg_kms(&i915->drm,
2515 "Degamma and gamma together are not possible\n");
2516 return -EINVAL;
2517 }
2518
2519 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB &&
2520 crtc_state->hw.ctm) {
2521 drm_dbg_kms(&i915->drm,
2522 "YCbCr and CTM together are not possible\n");
2523 return -EINVAL;
2524 }
2525
2526 crtc_state->gamma_enable = ilk_gamma_enable(crtc_state);
2527
2528 crtc_state->csc_enable = ilk_csc_enable(crtc_state);
2529
2530 crtc_state->gamma_mode = ilk_gamma_mode(crtc_state);
2531
2532 crtc_state->csc_mode = ilk_csc_mode(crtc_state);
2533
2534 ret = intel_color_add_affected_planes(state, crtc);
2535 if (ret)
2536 return ret;
2537
2538 ret = ilk_assign_luts(crtc_state);
2539 if (ret)
2540 return ret;
2541
2542 ilk_assign_csc(crtc_state);
2543
2544 crtc_state->preload_luts = intel_can_preload_luts(state, crtc);
2545
2546 return 0;
2547 }
2548
ivb_gamma_mode(const struct intel_crtc_state * crtc_state)2549 static u32 ivb_gamma_mode(const struct intel_crtc_state *crtc_state)
2550 {
2551 if (crtc_state->hw.degamma_lut && crtc_state->hw.gamma_lut)
2552 return GAMMA_MODE_MODE_SPLIT;
2553
2554 return ilk_gamma_mode(crtc_state);
2555 }
2556
ivb_csc_mode(const struct intel_crtc_state * crtc_state)2557 static u32 ivb_csc_mode(const struct intel_crtc_state *crtc_state)
2558 {
2559 bool limited_color_range = ilk_csc_limited_range(crtc_state);
2560
2561 /*
2562 * CSC comes after the LUT in degamma, RGB->YCbCr,
2563 * and RGB full->limited range mode.
2564 */
2565 if (crtc_state->hw.degamma_lut ||
2566 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
2567 limited_color_range)
2568 return 0;
2569
2570 return CSC_POSITION_BEFORE_GAMMA;
2571 }
2572
ivb_assign_luts(struct intel_crtc_state * crtc_state)2573 static int ivb_assign_luts(struct intel_crtc_state *crtc_state)
2574 {
2575 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2576 struct drm_property_blob *degamma_lut, *gamma_lut;
2577
2578 if (crtc_state->gamma_mode != GAMMA_MODE_MODE_SPLIT)
2579 return ilk_assign_luts(crtc_state);
2580
2581 drm_WARN_ON(&i915->drm, drm_color_lut_size(crtc_state->hw.degamma_lut) != 1024);
2582 drm_WARN_ON(&i915->drm, drm_color_lut_size(crtc_state->hw.gamma_lut) != 1024);
2583
2584 degamma_lut = create_resized_lut(i915, crtc_state->hw.degamma_lut, 512,
2585 false);
2586 if (IS_ERR(degamma_lut))
2587 return PTR_ERR(degamma_lut);
2588
2589 gamma_lut = create_resized_lut(i915, crtc_state->hw.gamma_lut, 512,
2590 ilk_lut_limited_range(crtc_state));
2591 if (IS_ERR(gamma_lut)) {
2592 drm_property_blob_put(degamma_lut);
2593 return PTR_ERR(gamma_lut);
2594 }
2595
2596 drm_property_replace_blob(&crtc_state->pre_csc_lut, degamma_lut);
2597 drm_property_replace_blob(&crtc_state->post_csc_lut, gamma_lut);
2598
2599 drm_property_blob_put(degamma_lut);
2600 drm_property_blob_put(gamma_lut);
2601
2602 return 0;
2603 }
2604
ivb_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2605 static int ivb_color_check(struct intel_atomic_state *state,
2606 struct intel_crtc *crtc)
2607 {
2608 struct drm_i915_private *i915 = to_i915(state->base.dev);
2609 struct intel_crtc_state *crtc_state =
2610 intel_atomic_get_new_crtc_state(state, crtc);
2611 int ret;
2612
2613 ret = check_luts(crtc_state);
2614 if (ret)
2615 return ret;
2616
2617 if (crtc_state->c8_planes && crtc_state->hw.degamma_lut) {
2618 drm_dbg_kms(&i915->drm,
2619 "C8 pixelformat and degamma together are not possible\n");
2620 return -EINVAL;
2621 }
2622
2623 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB &&
2624 crtc_state->hw.ctm) {
2625 drm_dbg_kms(&i915->drm,
2626 "YCbCr and CTM together are not possible\n");
2627 return -EINVAL;
2628 }
2629
2630 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB &&
2631 crtc_state->hw.degamma_lut && crtc_state->hw.gamma_lut) {
2632 drm_dbg_kms(&i915->drm,
2633 "YCbCr and degamma+gamma together are not possible\n");
2634 return -EINVAL;
2635 }
2636
2637 crtc_state->gamma_enable = ilk_gamma_enable(crtc_state);
2638
2639 crtc_state->csc_enable = ilk_csc_enable(crtc_state);
2640
2641 crtc_state->gamma_mode = ivb_gamma_mode(crtc_state);
2642
2643 crtc_state->csc_mode = ivb_csc_mode(crtc_state);
2644
2645 ret = intel_color_add_affected_planes(state, crtc);
2646 if (ret)
2647 return ret;
2648
2649 ret = ivb_assign_luts(crtc_state);
2650 if (ret)
2651 return ret;
2652
2653 ilk_assign_csc(crtc_state);
2654
2655 crtc_state->preload_luts = intel_can_preload_luts(state, crtc);
2656
2657 return 0;
2658 }
2659
glk_gamma_mode(const struct intel_crtc_state * crtc_state)2660 static u32 glk_gamma_mode(const struct intel_crtc_state *crtc_state)
2661 {
2662 if (!crtc_state->gamma_enable ||
2663 lut_is_legacy(crtc_state->hw.gamma_lut))
2664 return GAMMA_MODE_MODE_8BIT;
2665 else
2666 return GAMMA_MODE_MODE_10BIT;
2667 }
2668
glk_use_pre_csc_lut_for_gamma(const struct intel_crtc_state * crtc_state)2669 static bool glk_use_pre_csc_lut_for_gamma(const struct intel_crtc_state *crtc_state)
2670 {
2671 return crtc_state->hw.gamma_lut &&
2672 !crtc_state->c8_planes &&
2673 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB;
2674 }
2675
glk_assign_luts(struct intel_crtc_state * crtc_state)2676 static int glk_assign_luts(struct intel_crtc_state *crtc_state)
2677 {
2678 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2679
2680 if (glk_use_pre_csc_lut_for_gamma(crtc_state)) {
2681 struct drm_property_blob *gamma_lut;
2682
2683 gamma_lut = create_resized_lut(i915, crtc_state->hw.gamma_lut,
2684 DISPLAY_INFO(i915)->color.degamma_lut_size,
2685 false);
2686 if (IS_ERR(gamma_lut))
2687 return PTR_ERR(gamma_lut);
2688
2689 drm_property_replace_blob(&crtc_state->pre_csc_lut, gamma_lut);
2690 drm_property_replace_blob(&crtc_state->post_csc_lut, NULL);
2691
2692 drm_property_blob_put(gamma_lut);
2693
2694 return 0;
2695 }
2696
2697 if (ilk_lut_limited_range(crtc_state)) {
2698 struct drm_property_blob *gamma_lut;
2699
2700 gamma_lut = create_resized_lut(i915, crtc_state->hw.gamma_lut,
2701 drm_color_lut_size(crtc_state->hw.gamma_lut),
2702 true);
2703 if (IS_ERR(gamma_lut))
2704 return PTR_ERR(gamma_lut);
2705
2706 drm_property_replace_blob(&crtc_state->post_csc_lut, gamma_lut);
2707
2708 drm_property_blob_put(gamma_lut);
2709 } else {
2710 drm_property_replace_blob(&crtc_state->post_csc_lut, crtc_state->hw.gamma_lut);
2711 }
2712
2713 drm_property_replace_blob(&crtc_state->pre_csc_lut, crtc_state->hw.degamma_lut);
2714
2715 /*
2716 * On GLK+ both pipe CSC and degamma LUT are controlled
2717 * by csc_enable. Hence for the cases where the CSC is
2718 * needed but degamma LUT is not we need to load a
2719 * linear degamma LUT.
2720 */
2721 if (crtc_state->csc_enable && !crtc_state->pre_csc_lut)
2722 drm_property_replace_blob(&crtc_state->pre_csc_lut,
2723 i915->display.color.glk_linear_degamma_lut);
2724
2725 return 0;
2726 }
2727
glk_check_luts(const struct intel_crtc_state * crtc_state)2728 static int glk_check_luts(const struct intel_crtc_state *crtc_state)
2729 {
2730 u32 degamma_tests = intel_degamma_lut_tests(crtc_state);
2731 u32 gamma_tests = intel_gamma_lut_tests(crtc_state);
2732
2733 if (glk_use_pre_csc_lut_for_gamma(crtc_state))
2734 gamma_tests |= degamma_tests;
2735
2736 return _check_luts(crtc_state, degamma_tests, gamma_tests);
2737 }
2738
glk_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2739 static int glk_color_check(struct intel_atomic_state *state,
2740 struct intel_crtc *crtc)
2741 {
2742 struct drm_i915_private *i915 = to_i915(state->base.dev);
2743 struct intel_crtc_state *crtc_state =
2744 intel_atomic_get_new_crtc_state(state, crtc);
2745 int ret;
2746
2747 ret = glk_check_luts(crtc_state);
2748 if (ret)
2749 return ret;
2750
2751 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB &&
2752 crtc_state->hw.ctm) {
2753 drm_dbg_kms(&i915->drm,
2754 "YCbCr and CTM together are not possible\n");
2755 return -EINVAL;
2756 }
2757
2758 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB &&
2759 crtc_state->hw.degamma_lut && crtc_state->hw.gamma_lut) {
2760 drm_dbg_kms(&i915->drm,
2761 "YCbCr and degamma+gamma together are not possible\n");
2762 return -EINVAL;
2763 }
2764
2765 crtc_state->gamma_enable =
2766 !glk_use_pre_csc_lut_for_gamma(crtc_state) &&
2767 crtc_state->hw.gamma_lut &&
2768 !crtc_state->c8_planes;
2769
2770 /* On GLK+ degamma LUT is controlled by csc_enable */
2771 crtc_state->csc_enable =
2772 glk_use_pre_csc_lut_for_gamma(crtc_state) ||
2773 crtc_state->hw.degamma_lut ||
2774 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
2775 crtc_state->hw.ctm || ilk_csc_limited_range(crtc_state);
2776
2777 crtc_state->gamma_mode = glk_gamma_mode(crtc_state);
2778
2779 crtc_state->csc_mode = 0;
2780
2781 ret = intel_color_add_affected_planes(state, crtc);
2782 if (ret)
2783 return ret;
2784
2785 ret = glk_assign_luts(crtc_state);
2786 if (ret)
2787 return ret;
2788
2789 ilk_assign_csc(crtc_state);
2790
2791 crtc_state->preload_luts = intel_can_preload_luts(state, crtc);
2792
2793 return 0;
2794 }
2795
icl_gamma_mode(const struct intel_crtc_state * crtc_state)2796 static u32 icl_gamma_mode(const struct intel_crtc_state *crtc_state)
2797 {
2798 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2799 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2800 u32 gamma_mode = 0;
2801
2802 if (crtc_state->hw.degamma_lut)
2803 gamma_mode |= PRE_CSC_GAMMA_ENABLE;
2804
2805 if (crtc_state->hw.gamma_lut &&
2806 !crtc_state->c8_planes)
2807 gamma_mode |= POST_CSC_GAMMA_ENABLE;
2808
2809 if (!crtc_state->hw.gamma_lut ||
2810 lut_is_legacy(crtc_state->hw.gamma_lut))
2811 gamma_mode |= GAMMA_MODE_MODE_8BIT;
2812 /*
2813 * Enable 10bit gamma for D13
2814 * ToDo: Extend to Logarithmic Gamma once the new UAPI
2815 * is accepted and implemented by a userspace consumer
2816 */
2817 else if (DISPLAY_VER(i915) >= 13)
2818 gamma_mode |= GAMMA_MODE_MODE_10BIT;
2819 else
2820 gamma_mode |= GAMMA_MODE_MODE_12BIT_MULTI_SEG;
2821
2822 return gamma_mode;
2823 }
2824
icl_csc_mode(const struct intel_crtc_state * crtc_state)2825 static u32 icl_csc_mode(const struct intel_crtc_state *crtc_state)
2826 {
2827 u32 csc_mode = 0;
2828
2829 if (crtc_state->hw.ctm)
2830 csc_mode |= ICL_CSC_ENABLE;
2831
2832 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
2833 crtc_state->limited_color_range)
2834 csc_mode |= ICL_OUTPUT_CSC_ENABLE;
2835
2836 return csc_mode;
2837 }
2838
icl_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2839 static int icl_color_check(struct intel_atomic_state *state,
2840 struct intel_crtc *crtc)
2841 {
2842 struct intel_crtc_state *crtc_state =
2843 intel_atomic_get_new_crtc_state(state, crtc);
2844 int ret;
2845
2846 ret = check_luts(crtc_state);
2847 if (ret)
2848 return ret;
2849
2850 crtc_state->gamma_mode = icl_gamma_mode(crtc_state);
2851
2852 crtc_state->csc_mode = icl_csc_mode(crtc_state);
2853
2854 intel_assign_luts(crtc_state);
2855
2856 icl_assign_csc(crtc_state);
2857
2858 crtc_state->preload_luts = intel_can_preload_luts(state, crtc);
2859
2860 return 0;
2861 }
2862
i9xx_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2863 static int i9xx_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2864 {
2865 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
2866 return 0;
2867
2868 switch (crtc_state->gamma_mode) {
2869 case GAMMA_MODE_MODE_8BIT:
2870 return 8;
2871 case GAMMA_MODE_MODE_10BIT:
2872 return 10;
2873 default:
2874 MISSING_CASE(crtc_state->gamma_mode);
2875 return 0;
2876 }
2877 }
2878
i9xx_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)2879 static int i9xx_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2880 {
2881 return 0;
2882 }
2883
i965_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2884 static int i965_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2885 {
2886 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
2887 return 0;
2888
2889 switch (crtc_state->gamma_mode) {
2890 case GAMMA_MODE_MODE_8BIT:
2891 return 8;
2892 case GAMMA_MODE_MODE_10BIT:
2893 return 16;
2894 default:
2895 MISSING_CASE(crtc_state->gamma_mode);
2896 return 0;
2897 }
2898 }
2899
ilk_gamma_mode_precision(u32 gamma_mode)2900 static int ilk_gamma_mode_precision(u32 gamma_mode)
2901 {
2902 switch (gamma_mode) {
2903 case GAMMA_MODE_MODE_8BIT:
2904 return 8;
2905 case GAMMA_MODE_MODE_10BIT:
2906 return 10;
2907 default:
2908 MISSING_CASE(gamma_mode);
2909 return 0;
2910 }
2911 }
2912
ilk_has_post_csc_lut(const struct intel_crtc_state * crtc_state)2913 static bool ilk_has_post_csc_lut(const struct intel_crtc_state *crtc_state)
2914 {
2915 if (crtc_state->c8_planes)
2916 return true;
2917
2918 return crtc_state->gamma_enable &&
2919 (crtc_state->csc_mode & CSC_POSITION_BEFORE_GAMMA) != 0;
2920 }
2921
ilk_has_pre_csc_lut(const struct intel_crtc_state * crtc_state)2922 static bool ilk_has_pre_csc_lut(const struct intel_crtc_state *crtc_state)
2923 {
2924 return crtc_state->gamma_enable &&
2925 (crtc_state->csc_mode & CSC_POSITION_BEFORE_GAMMA) == 0;
2926 }
2927
ilk_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2928 static int ilk_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2929 {
2930 if (!ilk_has_post_csc_lut(crtc_state))
2931 return 0;
2932
2933 return ilk_gamma_mode_precision(crtc_state->gamma_mode);
2934 }
2935
ilk_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)2936 static int ilk_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2937 {
2938 if (!ilk_has_pre_csc_lut(crtc_state))
2939 return 0;
2940
2941 return ilk_gamma_mode_precision(crtc_state->gamma_mode);
2942 }
2943
ivb_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2944 static int ivb_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2945 {
2946 if (crtc_state->gamma_enable &&
2947 crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
2948 return 10;
2949
2950 return ilk_post_csc_lut_precision(crtc_state);
2951 }
2952
ivb_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)2953 static int ivb_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2954 {
2955 if (crtc_state->gamma_enable &&
2956 crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
2957 return 10;
2958
2959 return ilk_pre_csc_lut_precision(crtc_state);
2960 }
2961
chv_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2962 static int chv_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2963 {
2964 if (crtc_state->cgm_mode & CGM_PIPE_MODE_GAMMA)
2965 return 10;
2966
2967 return i965_post_csc_lut_precision(crtc_state);
2968 }
2969
chv_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)2970 static int chv_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2971 {
2972 if (crtc_state->cgm_mode & CGM_PIPE_MODE_DEGAMMA)
2973 return 14;
2974
2975 return 0;
2976 }
2977
glk_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2978 static int glk_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2979 {
2980 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
2981 return 0;
2982
2983 return ilk_gamma_mode_precision(crtc_state->gamma_mode);
2984 }
2985
glk_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)2986 static int glk_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2987 {
2988 if (!crtc_state->csc_enable)
2989 return 0;
2990
2991 return 16;
2992 }
2993
icl_has_post_csc_lut(const struct intel_crtc_state * crtc_state)2994 static bool icl_has_post_csc_lut(const struct intel_crtc_state *crtc_state)
2995 {
2996 if (crtc_state->c8_planes)
2997 return true;
2998
2999 return crtc_state->gamma_mode & POST_CSC_GAMMA_ENABLE;
3000 }
3001
icl_has_pre_csc_lut(const struct intel_crtc_state * crtc_state)3002 static bool icl_has_pre_csc_lut(const struct intel_crtc_state *crtc_state)
3003 {
3004 return crtc_state->gamma_mode & PRE_CSC_GAMMA_ENABLE;
3005 }
3006
icl_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)3007 static int icl_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
3008 {
3009 if (!icl_has_post_csc_lut(crtc_state))
3010 return 0;
3011
3012 switch (crtc_state->gamma_mode & GAMMA_MODE_MODE_MASK) {
3013 case GAMMA_MODE_MODE_8BIT:
3014 return 8;
3015 case GAMMA_MODE_MODE_10BIT:
3016 return 10;
3017 case GAMMA_MODE_MODE_12BIT_MULTI_SEG:
3018 return 16;
3019 default:
3020 MISSING_CASE(crtc_state->gamma_mode);
3021 return 0;
3022 }
3023 }
3024
icl_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)3025 static int icl_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
3026 {
3027 if (!icl_has_pre_csc_lut(crtc_state))
3028 return 0;
3029
3030 return 16;
3031 }
3032
err_check(const struct drm_color_lut * lut1,const struct drm_color_lut * lut2,u32 err)3033 static bool err_check(const struct drm_color_lut *lut1,
3034 const struct drm_color_lut *lut2, u32 err)
3035 {
3036 return ((abs((long)lut2->red - lut1->red)) <= err) &&
3037 ((abs((long)lut2->blue - lut1->blue)) <= err) &&
3038 ((abs((long)lut2->green - lut1->green)) <= err);
3039 }
3040
intel_lut_entries_equal(const struct drm_color_lut * lut1,const struct drm_color_lut * lut2,int lut_size,u32 err)3041 static bool intel_lut_entries_equal(const struct drm_color_lut *lut1,
3042 const struct drm_color_lut *lut2,
3043 int lut_size, u32 err)
3044 {
3045 int i;
3046
3047 for (i = 0; i < lut_size; i++) {
3048 if (!err_check(&lut1[i], &lut2[i], err))
3049 return false;
3050 }
3051
3052 return true;
3053 }
3054
intel_lut_equal(const struct drm_property_blob * blob1,const struct drm_property_blob * blob2,int check_size,int precision)3055 static bool intel_lut_equal(const struct drm_property_blob *blob1,
3056 const struct drm_property_blob *blob2,
3057 int check_size, int precision)
3058 {
3059 const struct drm_color_lut *lut1, *lut2;
3060 int lut_size1, lut_size2;
3061 u32 err;
3062
3063 if (!blob1 != !blob2)
3064 return false;
3065
3066 if (!blob1 != !precision)
3067 return false;
3068
3069 if (!blob1)
3070 return true;
3071
3072 lut_size1 = drm_color_lut_size(blob1);
3073 lut_size2 = drm_color_lut_size(blob2);
3074
3075 if (lut_size1 != lut_size2)
3076 return false;
3077
3078 if (check_size > lut_size1)
3079 return false;
3080
3081 lut1 = blob1->data;
3082 lut2 = blob2->data;
3083
3084 err = 0xffff >> precision;
3085
3086 if (!check_size)
3087 check_size = lut_size1;
3088
3089 return intel_lut_entries_equal(lut1, lut2, check_size, err);
3090 }
3091
i9xx_lut_equal(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob1,const struct drm_property_blob * blob2,bool is_pre_csc_lut)3092 static bool i9xx_lut_equal(const struct intel_crtc_state *crtc_state,
3093 const struct drm_property_blob *blob1,
3094 const struct drm_property_blob *blob2,
3095 bool is_pre_csc_lut)
3096 {
3097 int check_size = 0;
3098
3099 if (is_pre_csc_lut)
3100 return intel_lut_equal(blob1, blob2, 0,
3101 i9xx_pre_csc_lut_precision(crtc_state));
3102
3103 /* 10bit mode last entry is implicit, just skip it */
3104 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_10BIT)
3105 check_size = 128;
3106
3107 return intel_lut_equal(blob1, blob2, check_size,
3108 i9xx_post_csc_lut_precision(crtc_state));
3109 }
3110
i965_lut_equal(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob1,const struct drm_property_blob * blob2,bool is_pre_csc_lut)3111 static bool i965_lut_equal(const struct intel_crtc_state *crtc_state,
3112 const struct drm_property_blob *blob1,
3113 const struct drm_property_blob *blob2,
3114 bool is_pre_csc_lut)
3115 {
3116 if (is_pre_csc_lut)
3117 return intel_lut_equal(blob1, blob2, 0,
3118 i9xx_pre_csc_lut_precision(crtc_state));
3119 else
3120 return intel_lut_equal(blob1, blob2, 0,
3121 i965_post_csc_lut_precision(crtc_state));
3122 }
3123
chv_lut_equal(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob1,const struct drm_property_blob * blob2,bool is_pre_csc_lut)3124 static bool chv_lut_equal(const struct intel_crtc_state *crtc_state,
3125 const struct drm_property_blob *blob1,
3126 const struct drm_property_blob *blob2,
3127 bool is_pre_csc_lut)
3128 {
3129 if (is_pre_csc_lut)
3130 return intel_lut_equal(blob1, blob2, 0,
3131 chv_pre_csc_lut_precision(crtc_state));
3132 else
3133 return intel_lut_equal(blob1, blob2, 0,
3134 chv_post_csc_lut_precision(crtc_state));
3135 }
3136
ilk_lut_equal(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob1,const struct drm_property_blob * blob2,bool is_pre_csc_lut)3137 static bool ilk_lut_equal(const struct intel_crtc_state *crtc_state,
3138 const struct drm_property_blob *blob1,
3139 const struct drm_property_blob *blob2,
3140 bool is_pre_csc_lut)
3141 {
3142 if (is_pre_csc_lut)
3143 return intel_lut_equal(blob1, blob2, 0,
3144 ilk_pre_csc_lut_precision(crtc_state));
3145 else
3146 return intel_lut_equal(blob1, blob2, 0,
3147 ilk_post_csc_lut_precision(crtc_state));
3148 }
3149
ivb_lut_equal(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob1,const struct drm_property_blob * blob2,bool is_pre_csc_lut)3150 static bool ivb_lut_equal(const struct intel_crtc_state *crtc_state,
3151 const struct drm_property_blob *blob1,
3152 const struct drm_property_blob *blob2,
3153 bool is_pre_csc_lut)
3154 {
3155 if (is_pre_csc_lut)
3156 return intel_lut_equal(blob1, blob2, 0,
3157 ivb_pre_csc_lut_precision(crtc_state));
3158 else
3159 return intel_lut_equal(blob1, blob2, 0,
3160 ivb_post_csc_lut_precision(crtc_state));
3161 }
3162
glk_lut_equal(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob1,const struct drm_property_blob * blob2,bool is_pre_csc_lut)3163 static bool glk_lut_equal(const struct intel_crtc_state *crtc_state,
3164 const struct drm_property_blob *blob1,
3165 const struct drm_property_blob *blob2,
3166 bool is_pre_csc_lut)
3167 {
3168 if (is_pre_csc_lut)
3169 return intel_lut_equal(blob1, blob2, 0,
3170 glk_pre_csc_lut_precision(crtc_state));
3171 else
3172 return intel_lut_equal(blob1, blob2, 0,
3173 glk_post_csc_lut_precision(crtc_state));
3174 }
3175
icl_lut_equal(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob1,const struct drm_property_blob * blob2,bool is_pre_csc_lut)3176 static bool icl_lut_equal(const struct intel_crtc_state *crtc_state,
3177 const struct drm_property_blob *blob1,
3178 const struct drm_property_blob *blob2,
3179 bool is_pre_csc_lut)
3180 {
3181 int check_size = 0;
3182
3183 if (is_pre_csc_lut)
3184 return intel_lut_equal(blob1, blob2, 0,
3185 icl_pre_csc_lut_precision(crtc_state));
3186
3187 /* hw readout broken except for the super fine segment :( */
3188 if ((crtc_state->gamma_mode & GAMMA_MODE_MODE_MASK) ==
3189 GAMMA_MODE_MODE_12BIT_MULTI_SEG)
3190 check_size = 9;
3191
3192 return intel_lut_equal(blob1, blob2, check_size,
3193 icl_post_csc_lut_precision(crtc_state));
3194 }
3195
i9xx_read_lut_8(struct intel_crtc * crtc)3196 static struct drm_property_blob *i9xx_read_lut_8(struct intel_crtc *crtc)
3197 {
3198 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3199 enum pipe pipe = crtc->pipe;
3200 struct drm_property_blob *blob;
3201 struct drm_color_lut *lut;
3202 int i;
3203
3204 blob = drm_property_create_blob(&dev_priv->drm,
3205 sizeof(lut[0]) * LEGACY_LUT_LENGTH,
3206 NULL);
3207 if (IS_ERR(blob))
3208 return NULL;
3209
3210 lut = blob->data;
3211
3212 for (i = 0; i < LEGACY_LUT_LENGTH; i++) {
3213 u32 val = intel_de_read_fw(dev_priv,
3214 PALETTE(dev_priv, pipe, i));
3215
3216 i9xx_lut_8_pack(&lut[i], val);
3217 }
3218
3219 return blob;
3220 }
3221
i9xx_read_lut_10(struct intel_crtc * crtc)3222 static struct drm_property_blob *i9xx_read_lut_10(struct intel_crtc *crtc)
3223 {
3224 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3225 u32 lut_size = DISPLAY_INFO(dev_priv)->color.gamma_lut_size;
3226 enum pipe pipe = crtc->pipe;
3227 struct drm_property_blob *blob;
3228 struct drm_color_lut *lut;
3229 u32 ldw, udw;
3230 int i;
3231
3232 blob = drm_property_create_blob(&dev_priv->drm,
3233 lut_size * sizeof(lut[0]), NULL);
3234 if (IS_ERR(blob))
3235 return NULL;
3236
3237 lut = blob->data;
3238
3239 for (i = 0; i < lut_size - 1; i++) {
3240 ldw = intel_de_read_fw(dev_priv,
3241 PALETTE(dev_priv, pipe, 2 * i + 0));
3242 udw = intel_de_read_fw(dev_priv,
3243 PALETTE(dev_priv, pipe, 2 * i + 1));
3244
3245 i9xx_lut_10_pack(&lut[i], ldw, udw);
3246 }
3247
3248 i9xx_lut_10_pack_slope(&lut[i], ldw, udw);
3249
3250 return blob;
3251 }
3252
i9xx_read_luts(struct intel_crtc_state * crtc_state)3253 static void i9xx_read_luts(struct intel_crtc_state *crtc_state)
3254 {
3255 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3256
3257 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3258 return;
3259
3260 switch (crtc_state->gamma_mode) {
3261 case GAMMA_MODE_MODE_8BIT:
3262 crtc_state->post_csc_lut = i9xx_read_lut_8(crtc);
3263 break;
3264 case GAMMA_MODE_MODE_10BIT:
3265 crtc_state->post_csc_lut = i9xx_read_lut_10(crtc);
3266 break;
3267 default:
3268 MISSING_CASE(crtc_state->gamma_mode);
3269 break;
3270 }
3271 }
3272
i965_read_lut_10p6(struct intel_crtc * crtc)3273 static struct drm_property_blob *i965_read_lut_10p6(struct intel_crtc *crtc)
3274 {
3275 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3276 int i, lut_size = DISPLAY_INFO(dev_priv)->color.gamma_lut_size;
3277 enum pipe pipe = crtc->pipe;
3278 struct drm_property_blob *blob;
3279 struct drm_color_lut *lut;
3280
3281 blob = drm_property_create_blob(&dev_priv->drm,
3282 sizeof(lut[0]) * lut_size,
3283 NULL);
3284 if (IS_ERR(blob))
3285 return NULL;
3286
3287 lut = blob->data;
3288
3289 for (i = 0; i < lut_size - 1; i++) {
3290 u32 ldw = intel_de_read_fw(dev_priv,
3291 PALETTE(dev_priv, pipe, 2 * i + 0));
3292 u32 udw = intel_de_read_fw(dev_priv,
3293 PALETTE(dev_priv, pipe, 2 * i + 1));
3294
3295 i965_lut_10p6_pack(&lut[i], ldw, udw);
3296 }
3297
3298 lut[i].red = i965_lut_11p6_max_pack(intel_de_read_fw(dev_priv, PIPEGCMAX(dev_priv, pipe, 0)));
3299 lut[i].green = i965_lut_11p6_max_pack(intel_de_read_fw(dev_priv, PIPEGCMAX(dev_priv, pipe, 1)));
3300 lut[i].blue = i965_lut_11p6_max_pack(intel_de_read_fw(dev_priv, PIPEGCMAX(dev_priv, pipe, 2)));
3301
3302 return blob;
3303 }
3304
i965_read_luts(struct intel_crtc_state * crtc_state)3305 static void i965_read_luts(struct intel_crtc_state *crtc_state)
3306 {
3307 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3308
3309 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3310 return;
3311
3312 switch (crtc_state->gamma_mode) {
3313 case GAMMA_MODE_MODE_8BIT:
3314 crtc_state->post_csc_lut = i9xx_read_lut_8(crtc);
3315 break;
3316 case GAMMA_MODE_MODE_10BIT:
3317 crtc_state->post_csc_lut = i965_read_lut_10p6(crtc);
3318 break;
3319 default:
3320 MISSING_CASE(crtc_state->gamma_mode);
3321 break;
3322 }
3323 }
3324
chv_read_cgm_degamma(struct intel_crtc * crtc)3325 static struct drm_property_blob *chv_read_cgm_degamma(struct intel_crtc *crtc)
3326 {
3327 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3328 int i, lut_size = DISPLAY_INFO(dev_priv)->color.degamma_lut_size;
3329 enum pipe pipe = crtc->pipe;
3330 struct drm_property_blob *blob;
3331 struct drm_color_lut *lut;
3332
3333 blob = drm_property_create_blob(&dev_priv->drm,
3334 sizeof(lut[0]) * lut_size,
3335 NULL);
3336 if (IS_ERR(blob))
3337 return NULL;
3338
3339 lut = blob->data;
3340
3341 for (i = 0; i < lut_size; i++) {
3342 u32 ldw = intel_de_read_fw(dev_priv, CGM_PIPE_DEGAMMA(pipe, i, 0));
3343 u32 udw = intel_de_read_fw(dev_priv, CGM_PIPE_DEGAMMA(pipe, i, 1));
3344
3345 chv_cgm_degamma_pack(&lut[i], ldw, udw);
3346 }
3347
3348 return blob;
3349 }
3350
chv_read_cgm_gamma(struct intel_crtc * crtc)3351 static struct drm_property_blob *chv_read_cgm_gamma(struct intel_crtc *crtc)
3352 {
3353 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3354 int i, lut_size = DISPLAY_INFO(i915)->color.gamma_lut_size;
3355 enum pipe pipe = crtc->pipe;
3356 struct drm_property_blob *blob;
3357 struct drm_color_lut *lut;
3358
3359 blob = drm_property_create_blob(&i915->drm,
3360 sizeof(lut[0]) * lut_size,
3361 NULL);
3362 if (IS_ERR(blob))
3363 return NULL;
3364
3365 lut = blob->data;
3366
3367 for (i = 0; i < lut_size; i++) {
3368 u32 ldw = intel_de_read_fw(i915, CGM_PIPE_GAMMA(pipe, i, 0));
3369 u32 udw = intel_de_read_fw(i915, CGM_PIPE_GAMMA(pipe, i, 1));
3370
3371 chv_cgm_gamma_pack(&lut[i], ldw, udw);
3372 }
3373
3374 return blob;
3375 }
3376
chv_get_config(struct intel_crtc_state * crtc_state)3377 static void chv_get_config(struct intel_crtc_state *crtc_state)
3378 {
3379 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3380 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3381
3382 crtc_state->cgm_mode = intel_de_read(i915, CGM_PIPE_MODE(crtc->pipe));
3383
3384 i9xx_get_config(crtc_state);
3385 }
3386
chv_read_luts(struct intel_crtc_state * crtc_state)3387 static void chv_read_luts(struct intel_crtc_state *crtc_state)
3388 {
3389 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3390
3391 if (crtc_state->cgm_mode & CGM_PIPE_MODE_DEGAMMA)
3392 crtc_state->pre_csc_lut = chv_read_cgm_degamma(crtc);
3393
3394 if (crtc_state->cgm_mode & CGM_PIPE_MODE_GAMMA)
3395 crtc_state->post_csc_lut = chv_read_cgm_gamma(crtc);
3396 else
3397 i965_read_luts(crtc_state);
3398 }
3399
ilk_read_lut_8(struct intel_crtc * crtc)3400 static struct drm_property_blob *ilk_read_lut_8(struct intel_crtc *crtc)
3401 {
3402 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3403 enum pipe pipe = crtc->pipe;
3404 struct drm_property_blob *blob;
3405 struct drm_color_lut *lut;
3406 int i;
3407
3408 blob = drm_property_create_blob(&i915->drm,
3409 sizeof(lut[0]) * LEGACY_LUT_LENGTH,
3410 NULL);
3411 if (IS_ERR(blob))
3412 return NULL;
3413
3414 lut = blob->data;
3415
3416 for (i = 0; i < LEGACY_LUT_LENGTH; i++) {
3417 u32 val = intel_de_read_fw(i915, LGC_PALETTE(pipe, i));
3418
3419 i9xx_lut_8_pack(&lut[i], val);
3420 }
3421
3422 return blob;
3423 }
3424
ilk_read_lut_10(struct intel_crtc * crtc)3425 static struct drm_property_blob *ilk_read_lut_10(struct intel_crtc *crtc)
3426 {
3427 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3428 int i, lut_size = DISPLAY_INFO(i915)->color.gamma_lut_size;
3429 enum pipe pipe = crtc->pipe;
3430 struct drm_property_blob *blob;
3431 struct drm_color_lut *lut;
3432
3433 blob = drm_property_create_blob(&i915->drm,
3434 sizeof(lut[0]) * lut_size,
3435 NULL);
3436 if (IS_ERR(blob))
3437 return NULL;
3438
3439 lut = blob->data;
3440
3441 for (i = 0; i < lut_size; i++) {
3442 u32 val = intel_de_read_fw(i915, PREC_PALETTE(pipe, i));
3443
3444 ilk_lut_10_pack(&lut[i], val);
3445 }
3446
3447 return blob;
3448 }
3449
ilk_get_config(struct intel_crtc_state * crtc_state)3450 static void ilk_get_config(struct intel_crtc_state *crtc_state)
3451 {
3452 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3453
3454 crtc_state->csc_mode = ilk_read_csc_mode(crtc);
3455
3456 i9xx_get_config(crtc_state);
3457 }
3458
ilk_read_luts(struct intel_crtc_state * crtc_state)3459 static void ilk_read_luts(struct intel_crtc_state *crtc_state)
3460 {
3461 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3462 struct drm_property_blob **blob =
3463 ilk_has_post_csc_lut(crtc_state) ?
3464 &crtc_state->post_csc_lut : &crtc_state->pre_csc_lut;
3465
3466 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3467 return;
3468
3469 switch (crtc_state->gamma_mode) {
3470 case GAMMA_MODE_MODE_8BIT:
3471 *blob = ilk_read_lut_8(crtc);
3472 break;
3473 case GAMMA_MODE_MODE_10BIT:
3474 *blob = ilk_read_lut_10(crtc);
3475 break;
3476 default:
3477 MISSING_CASE(crtc_state->gamma_mode);
3478 break;
3479 }
3480 }
3481
3482 /*
3483 * IVB/HSW Bspec / PAL_PREC_INDEX:
3484 * "Restriction : Index auto increment mode is not
3485 * supported and must not be enabled."
3486 */
ivb_read_lut_10(struct intel_crtc * crtc,u32 prec_index)3487 static struct drm_property_blob *ivb_read_lut_10(struct intel_crtc *crtc,
3488 u32 prec_index)
3489 {
3490 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3491 int i, lut_size = ivb_lut_10_size(prec_index);
3492 enum pipe pipe = crtc->pipe;
3493 struct drm_property_blob *blob;
3494 struct drm_color_lut *lut;
3495
3496 blob = drm_property_create_blob(&dev_priv->drm,
3497 sizeof(lut[0]) * lut_size,
3498 NULL);
3499 if (IS_ERR(blob))
3500 return NULL;
3501
3502 lut = blob->data;
3503
3504 for (i = 0; i < lut_size; i++) {
3505 u32 val;
3506
3507 intel_de_write_fw(dev_priv, PREC_PAL_INDEX(pipe),
3508 prec_index + i);
3509 val = intel_de_read_fw(dev_priv, PREC_PAL_DATA(pipe));
3510
3511 ilk_lut_10_pack(&lut[i], val);
3512 }
3513
3514 intel_de_write_fw(dev_priv, PREC_PAL_INDEX(pipe),
3515 PAL_PREC_INDEX_VALUE(0));
3516
3517 return blob;
3518 }
3519
ivb_read_luts(struct intel_crtc_state * crtc_state)3520 static void ivb_read_luts(struct intel_crtc_state *crtc_state)
3521 {
3522 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3523 struct drm_property_blob **blob =
3524 ilk_has_post_csc_lut(crtc_state) ?
3525 &crtc_state->post_csc_lut : &crtc_state->pre_csc_lut;
3526
3527 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3528 return;
3529
3530 switch (crtc_state->gamma_mode) {
3531 case GAMMA_MODE_MODE_8BIT:
3532 *blob = ilk_read_lut_8(crtc);
3533 break;
3534 case GAMMA_MODE_MODE_SPLIT:
3535 crtc_state->pre_csc_lut =
3536 ivb_read_lut_10(crtc, PAL_PREC_SPLIT_MODE |
3537 PAL_PREC_INDEX_VALUE(0));
3538 crtc_state->post_csc_lut =
3539 ivb_read_lut_10(crtc, PAL_PREC_SPLIT_MODE |
3540 PAL_PREC_INDEX_VALUE(512));
3541 break;
3542 case GAMMA_MODE_MODE_10BIT:
3543 *blob = ivb_read_lut_10(crtc, PAL_PREC_INDEX_VALUE(0));
3544 break;
3545 default:
3546 MISSING_CASE(crtc_state->gamma_mode);
3547 break;
3548 }
3549 }
3550
3551 /* On BDW+ the index auto increment mode actually works */
bdw_read_lut_10(struct intel_crtc * crtc,u32 prec_index)3552 static struct drm_property_blob *bdw_read_lut_10(struct intel_crtc *crtc,
3553 u32 prec_index)
3554 {
3555 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3556 int i, lut_size = ivb_lut_10_size(prec_index);
3557 enum pipe pipe = crtc->pipe;
3558 struct drm_property_blob *blob;
3559 struct drm_color_lut *lut;
3560
3561 blob = drm_property_create_blob(&i915->drm,
3562 sizeof(lut[0]) * lut_size,
3563 NULL);
3564 if (IS_ERR(blob))
3565 return NULL;
3566
3567 lut = blob->data;
3568
3569 intel_de_write_fw(i915, PREC_PAL_INDEX(pipe),
3570 prec_index);
3571 intel_de_write_fw(i915, PREC_PAL_INDEX(pipe),
3572 PAL_PREC_AUTO_INCREMENT |
3573 prec_index);
3574
3575 for (i = 0; i < lut_size; i++) {
3576 u32 val = intel_de_read_fw(i915, PREC_PAL_DATA(pipe));
3577
3578 ilk_lut_10_pack(&lut[i], val);
3579 }
3580
3581 intel_de_write_fw(i915, PREC_PAL_INDEX(pipe),
3582 PAL_PREC_INDEX_VALUE(0));
3583
3584 return blob;
3585 }
3586
bdw_read_luts(struct intel_crtc_state * crtc_state)3587 static void bdw_read_luts(struct intel_crtc_state *crtc_state)
3588 {
3589 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3590 struct drm_property_blob **blob =
3591 ilk_has_post_csc_lut(crtc_state) ?
3592 &crtc_state->post_csc_lut : &crtc_state->pre_csc_lut;
3593
3594 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3595 return;
3596
3597 switch (crtc_state->gamma_mode) {
3598 case GAMMA_MODE_MODE_8BIT:
3599 *blob = ilk_read_lut_8(crtc);
3600 break;
3601 case GAMMA_MODE_MODE_SPLIT:
3602 crtc_state->pre_csc_lut =
3603 bdw_read_lut_10(crtc, PAL_PREC_SPLIT_MODE |
3604 PAL_PREC_INDEX_VALUE(0));
3605 crtc_state->post_csc_lut =
3606 bdw_read_lut_10(crtc, PAL_PREC_SPLIT_MODE |
3607 PAL_PREC_INDEX_VALUE(512));
3608 break;
3609 case GAMMA_MODE_MODE_10BIT:
3610 *blob = bdw_read_lut_10(crtc, PAL_PREC_INDEX_VALUE(0));
3611 break;
3612 default:
3613 MISSING_CASE(crtc_state->gamma_mode);
3614 break;
3615 }
3616 }
3617
glk_read_degamma_lut(struct intel_crtc * crtc)3618 static struct drm_property_blob *glk_read_degamma_lut(struct intel_crtc *crtc)
3619 {
3620 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3621 int i, lut_size = DISPLAY_INFO(dev_priv)->color.degamma_lut_size;
3622 enum pipe pipe = crtc->pipe;
3623 struct drm_property_blob *blob;
3624 struct drm_color_lut *lut;
3625
3626 blob = drm_property_create_blob(&dev_priv->drm,
3627 sizeof(lut[0]) * lut_size,
3628 NULL);
3629 if (IS_ERR(blob))
3630 return NULL;
3631
3632 lut = blob->data;
3633
3634 /*
3635 * When setting the auto-increment bit, the hardware seems to
3636 * ignore the index bits, so we need to reset it to index 0
3637 * separately.
3638 */
3639 intel_de_write_fw(dev_priv, PRE_CSC_GAMC_INDEX(pipe),
3640 PRE_CSC_GAMC_INDEX_VALUE(0));
3641 intel_de_write_fw(dev_priv, PRE_CSC_GAMC_INDEX(pipe),
3642 PRE_CSC_GAMC_AUTO_INCREMENT |
3643 PRE_CSC_GAMC_INDEX_VALUE(0));
3644
3645 for (i = 0; i < lut_size; i++) {
3646 u32 val = intel_de_read_fw(dev_priv, PRE_CSC_GAMC_DATA(pipe));
3647
3648 if (DISPLAY_VER(dev_priv) >= 14)
3649 mtl_degamma_lut_pack(&lut[i], val);
3650 else
3651 glk_degamma_lut_pack(&lut[i], val);
3652 }
3653
3654 intel_de_write_fw(dev_priv, PRE_CSC_GAMC_INDEX(pipe),
3655 PRE_CSC_GAMC_INDEX_VALUE(0));
3656
3657 return blob;
3658 }
3659
glk_read_luts(struct intel_crtc_state * crtc_state)3660 static void glk_read_luts(struct intel_crtc_state *crtc_state)
3661 {
3662 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3663
3664 if (crtc_state->csc_enable)
3665 crtc_state->pre_csc_lut = glk_read_degamma_lut(crtc);
3666
3667 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3668 return;
3669
3670 switch (crtc_state->gamma_mode) {
3671 case GAMMA_MODE_MODE_8BIT:
3672 crtc_state->post_csc_lut = ilk_read_lut_8(crtc);
3673 break;
3674 case GAMMA_MODE_MODE_10BIT:
3675 crtc_state->post_csc_lut = bdw_read_lut_10(crtc, PAL_PREC_INDEX_VALUE(0));
3676 break;
3677 default:
3678 MISSING_CASE(crtc_state->gamma_mode);
3679 break;
3680 }
3681 }
3682
3683 static struct drm_property_blob *
icl_read_lut_multi_segment(struct intel_crtc * crtc)3684 icl_read_lut_multi_segment(struct intel_crtc *crtc)
3685 {
3686 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3687 int i, lut_size = DISPLAY_INFO(i915)->color.gamma_lut_size;
3688 enum pipe pipe = crtc->pipe;
3689 struct drm_property_blob *blob;
3690 struct drm_color_lut *lut;
3691
3692 blob = drm_property_create_blob(&i915->drm,
3693 sizeof(lut[0]) * lut_size,
3694 NULL);
3695 if (IS_ERR(blob))
3696 return NULL;
3697
3698 lut = blob->data;
3699
3700 intel_de_write_fw(i915, PREC_PAL_MULTI_SEG_INDEX(pipe),
3701 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
3702 intel_de_write_fw(i915, PREC_PAL_MULTI_SEG_INDEX(pipe),
3703 PAL_PREC_MULTI_SEG_AUTO_INCREMENT |
3704 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
3705
3706 for (i = 0; i < 9; i++) {
3707 u32 ldw = intel_de_read_fw(i915, PREC_PAL_MULTI_SEG_DATA(pipe));
3708 u32 udw = intel_de_read_fw(i915, PREC_PAL_MULTI_SEG_DATA(pipe));
3709
3710 ilk_lut_12p4_pack(&lut[i], ldw, udw);
3711 }
3712
3713 intel_de_write_fw(i915, PREC_PAL_MULTI_SEG_INDEX(pipe),
3714 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
3715
3716 /*
3717 * FIXME readouts from PAL_PREC_DATA register aren't giving
3718 * correct values in the case of fine and coarse segments.
3719 * Restricting readouts only for super fine segment as of now.
3720 */
3721
3722 return blob;
3723 }
3724
icl_read_luts(struct intel_crtc_state * crtc_state)3725 static void icl_read_luts(struct intel_crtc_state *crtc_state)
3726 {
3727 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3728
3729 if (icl_has_pre_csc_lut(crtc_state))
3730 crtc_state->pre_csc_lut = glk_read_degamma_lut(crtc);
3731
3732 if (!icl_has_post_csc_lut(crtc_state))
3733 return;
3734
3735 switch (crtc_state->gamma_mode & GAMMA_MODE_MODE_MASK) {
3736 case GAMMA_MODE_MODE_8BIT:
3737 crtc_state->post_csc_lut = ilk_read_lut_8(crtc);
3738 break;
3739 case GAMMA_MODE_MODE_10BIT:
3740 crtc_state->post_csc_lut = bdw_read_lut_10(crtc, PAL_PREC_INDEX_VALUE(0));
3741 break;
3742 case GAMMA_MODE_MODE_12BIT_MULTI_SEG:
3743 crtc_state->post_csc_lut = icl_read_lut_multi_segment(crtc);
3744 break;
3745 default:
3746 MISSING_CASE(crtc_state->gamma_mode);
3747 break;
3748 }
3749 }
3750
3751 static const struct intel_color_funcs chv_color_funcs = {
3752 .color_check = chv_color_check,
3753 .color_commit_arm = i9xx_color_commit_arm,
3754 .load_luts = chv_load_luts,
3755 .read_luts = chv_read_luts,
3756 .lut_equal = chv_lut_equal,
3757 .read_csc = chv_read_csc,
3758 .get_config = chv_get_config,
3759 };
3760
3761 static const struct intel_color_funcs vlv_color_funcs = {
3762 .color_check = vlv_color_check,
3763 .color_commit_arm = i9xx_color_commit_arm,
3764 .load_luts = vlv_load_luts,
3765 .read_luts = i965_read_luts,
3766 .lut_equal = i965_lut_equal,
3767 .read_csc = vlv_read_csc,
3768 .get_config = i9xx_get_config,
3769 };
3770
3771 static const struct intel_color_funcs i965_color_funcs = {
3772 .color_check = i9xx_color_check,
3773 .color_commit_arm = i9xx_color_commit_arm,
3774 .load_luts = i965_load_luts,
3775 .read_luts = i965_read_luts,
3776 .lut_equal = i965_lut_equal,
3777 .get_config = i9xx_get_config,
3778 };
3779
3780 static const struct intel_color_funcs i9xx_color_funcs = {
3781 .color_check = i9xx_color_check,
3782 .color_commit_arm = i9xx_color_commit_arm,
3783 .load_luts = i9xx_load_luts,
3784 .read_luts = i9xx_read_luts,
3785 .lut_equal = i9xx_lut_equal,
3786 .get_config = i9xx_get_config,
3787 };
3788
3789 static const struct intel_color_funcs tgl_color_funcs = {
3790 .color_check = icl_color_check,
3791 .color_commit_noarm = icl_color_commit_noarm,
3792 .color_commit_arm = icl_color_commit_arm,
3793 .load_luts = icl_load_luts,
3794 .read_luts = icl_read_luts,
3795 .lut_equal = icl_lut_equal,
3796 .read_csc = icl_read_csc,
3797 .get_config = skl_get_config,
3798 };
3799
3800 static const struct intel_color_funcs icl_color_funcs = {
3801 .color_check = icl_color_check,
3802 .color_commit_noarm = icl_color_commit_noarm,
3803 .color_commit_arm = icl_color_commit_arm,
3804 .color_post_update = icl_color_post_update,
3805 .load_luts = icl_load_luts,
3806 .read_luts = icl_read_luts,
3807 .lut_equal = icl_lut_equal,
3808 .read_csc = icl_read_csc,
3809 .get_config = skl_get_config,
3810 };
3811
3812 static const struct intel_color_funcs glk_color_funcs = {
3813 .color_check = glk_color_check,
3814 .color_commit_noarm = skl_color_commit_noarm,
3815 .color_commit_arm = skl_color_commit_arm,
3816 .load_luts = glk_load_luts,
3817 .read_luts = glk_read_luts,
3818 .lut_equal = glk_lut_equal,
3819 .read_csc = skl_read_csc,
3820 .get_config = skl_get_config,
3821 };
3822
3823 static const struct intel_color_funcs skl_color_funcs = {
3824 .color_check = ivb_color_check,
3825 .color_commit_noarm = skl_color_commit_noarm,
3826 .color_commit_arm = skl_color_commit_arm,
3827 .load_luts = bdw_load_luts,
3828 .read_luts = bdw_read_luts,
3829 .lut_equal = ivb_lut_equal,
3830 .read_csc = skl_read_csc,
3831 .get_config = skl_get_config,
3832 };
3833
3834 static const struct intel_color_funcs bdw_color_funcs = {
3835 .color_check = ivb_color_check,
3836 .color_commit_noarm = ilk_color_commit_noarm,
3837 .color_commit_arm = hsw_color_commit_arm,
3838 .load_luts = bdw_load_luts,
3839 .read_luts = bdw_read_luts,
3840 .lut_equal = ivb_lut_equal,
3841 .read_csc = ilk_read_csc,
3842 .get_config = hsw_get_config,
3843 };
3844
3845 static const struct intel_color_funcs hsw_color_funcs = {
3846 .color_check = ivb_color_check,
3847 .color_commit_noarm = ilk_color_commit_noarm,
3848 .color_commit_arm = hsw_color_commit_arm,
3849 .load_luts = ivb_load_luts,
3850 .read_luts = ivb_read_luts,
3851 .lut_equal = ivb_lut_equal,
3852 .read_csc = ilk_read_csc,
3853 .get_config = hsw_get_config,
3854 };
3855
3856 static const struct intel_color_funcs ivb_color_funcs = {
3857 .color_check = ivb_color_check,
3858 .color_commit_noarm = ilk_color_commit_noarm,
3859 .color_commit_arm = ilk_color_commit_arm,
3860 .load_luts = ivb_load_luts,
3861 .read_luts = ivb_read_luts,
3862 .lut_equal = ivb_lut_equal,
3863 .read_csc = ilk_read_csc,
3864 .get_config = ilk_get_config,
3865 };
3866
3867 static const struct intel_color_funcs ilk_color_funcs = {
3868 .color_check = ilk_color_check,
3869 .color_commit_noarm = ilk_color_commit_noarm,
3870 .color_commit_arm = ilk_color_commit_arm,
3871 .load_luts = ilk_load_luts,
3872 .read_luts = ilk_read_luts,
3873 .lut_equal = ilk_lut_equal,
3874 .read_csc = ilk_read_csc,
3875 .get_config = ilk_get_config,
3876 };
3877
intel_color_crtc_init(struct intel_crtc * crtc)3878 void intel_color_crtc_init(struct intel_crtc *crtc)
3879 {
3880 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3881 int degamma_lut_size, gamma_lut_size;
3882 bool has_ctm;
3883
3884 drm_mode_crtc_set_gamma_size(&crtc->base, 256);
3885
3886 gamma_lut_size = DISPLAY_INFO(i915)->color.gamma_lut_size;
3887 degamma_lut_size = DISPLAY_INFO(i915)->color.degamma_lut_size;
3888 has_ctm = DISPLAY_VER(i915) >= 5;
3889
3890 /*
3891 * "DPALETTE_A: NOTE: The 8-bit (non-10-bit) mode is the
3892 * only mode supported by Alviso and Grantsdale."
3893 *
3894 * Actually looks like this affects all of gen3.
3895 * Confirmed on alv,cst,pnv. Mobile gen2 parts (alm,mgm)
3896 * are confirmed not to suffer from this restriction.
3897 */
3898 if (DISPLAY_VER(i915) == 3 && crtc->pipe == PIPE_A)
3899 gamma_lut_size = 256;
3900
3901 drm_crtc_enable_color_mgmt(&crtc->base, degamma_lut_size,
3902 has_ctm, gamma_lut_size);
3903 }
3904
intel_color_init(struct drm_i915_private * i915)3905 int intel_color_init(struct drm_i915_private *i915)
3906 {
3907 struct drm_property_blob *blob;
3908
3909 if (DISPLAY_VER(i915) != 10)
3910 return 0;
3911
3912 blob = create_linear_lut(i915,
3913 DISPLAY_INFO(i915)->color.degamma_lut_size);
3914 if (IS_ERR(blob))
3915 return PTR_ERR(blob);
3916
3917 i915->display.color.glk_linear_degamma_lut = blob;
3918
3919 return 0;
3920 }
3921
intel_color_init_hooks(struct drm_i915_private * i915)3922 void intel_color_init_hooks(struct drm_i915_private *i915)
3923 {
3924 if (HAS_GMCH(i915)) {
3925 if (IS_CHERRYVIEW(i915))
3926 i915->display.funcs.color = &chv_color_funcs;
3927 else if (IS_VALLEYVIEW(i915))
3928 i915->display.funcs.color = &vlv_color_funcs;
3929 else if (DISPLAY_VER(i915) >= 4)
3930 i915->display.funcs.color = &i965_color_funcs;
3931 else
3932 i915->display.funcs.color = &i9xx_color_funcs;
3933 } else {
3934 if (DISPLAY_VER(i915) >= 12)
3935 i915->display.funcs.color = &tgl_color_funcs;
3936 else if (DISPLAY_VER(i915) == 11)
3937 i915->display.funcs.color = &icl_color_funcs;
3938 else if (DISPLAY_VER(i915) == 10)
3939 i915->display.funcs.color = &glk_color_funcs;
3940 else if (DISPLAY_VER(i915) == 9)
3941 i915->display.funcs.color = &skl_color_funcs;
3942 else if (DISPLAY_VER(i915) == 8)
3943 i915->display.funcs.color = &bdw_color_funcs;
3944 else if (IS_HASWELL(i915))
3945 i915->display.funcs.color = &hsw_color_funcs;
3946 else if (DISPLAY_VER(i915) == 7)
3947 i915->display.funcs.color = &ivb_color_funcs;
3948 else
3949 i915->display.funcs.color = &ilk_color_funcs;
3950 }
3951 }
3952