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 unless
1337 * we either write each entry twice when using posted
1338 * writes, or we use non-posted writes.
1339 *
1340 * If palette anti-collision is active during LUT
1341 * register writes:
1342 * - posted writes simply get dropped and thus the LUT
1343 * contents may not be correctly updated
1344 * - non-posted writes are blocked and thus the LUT
1345 * contents are always correct, but simultaneous CPU
1346 * MMIO access will start to fail
1347 *
1348 * Choose the lesser of two evils and use posted writes.
1349 * Using posted writes is also faster, even when having
1350 * to write each register twice.
1351 */
1352 for (i = 0; i < 256; i++) {
1353 ilk_lut_write(crtc_state, LGC_PALETTE(pipe, i),
1354 i9xx_lut_8(&lut[i]));
1355 if (crtc_state->dsb_color_vblank)
1356 ilk_lut_write(crtc_state, LGC_PALETTE(pipe, i),
1357 i9xx_lut_8(&lut[i]));
1358 }
1359 }
1360
ilk_load_lut_10(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob)1361 static void ilk_load_lut_10(const struct intel_crtc_state *crtc_state,
1362 const struct drm_property_blob *blob)
1363 {
1364 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1365 const struct drm_color_lut *lut = blob->data;
1366 int i, lut_size = drm_color_lut_size(blob);
1367 enum pipe pipe = crtc->pipe;
1368
1369 for (i = 0; i < lut_size; i++)
1370 ilk_lut_write(crtc_state, PREC_PALETTE(pipe, i),
1371 ilk_lut_10(&lut[i]));
1372 }
1373
ilk_load_luts(const struct intel_crtc_state * crtc_state)1374 static void ilk_load_luts(const struct intel_crtc_state *crtc_state)
1375 {
1376 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1377 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1378 const struct drm_property_blob *blob = post_csc_lut ?: pre_csc_lut;
1379
1380 switch (crtc_state->gamma_mode) {
1381 case GAMMA_MODE_MODE_8BIT:
1382 ilk_load_lut_8(crtc_state, blob);
1383 break;
1384 case GAMMA_MODE_MODE_10BIT:
1385 ilk_load_lut_10(crtc_state, blob);
1386 break;
1387 default:
1388 MISSING_CASE(crtc_state->gamma_mode);
1389 break;
1390 }
1391 }
1392
ivb_lut_10_size(u32 prec_index)1393 static int ivb_lut_10_size(u32 prec_index)
1394 {
1395 if (prec_index & PAL_PREC_SPLIT_MODE)
1396 return 512;
1397 else
1398 return 1024;
1399 }
1400
1401 /*
1402 * IVB/HSW Bspec / PAL_PREC_INDEX:
1403 * "Restriction : Index auto increment mode is not
1404 * supported and must not be enabled."
1405 */
ivb_load_lut_10(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob,u32 prec_index)1406 static void ivb_load_lut_10(const struct intel_crtc_state *crtc_state,
1407 const struct drm_property_blob *blob,
1408 u32 prec_index)
1409 {
1410 const struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1411 const struct drm_color_lut *lut = blob->data;
1412 int i, lut_size = drm_color_lut_size(blob);
1413 enum pipe pipe = crtc->pipe;
1414
1415 for (i = 0; i < lut_size; i++) {
1416 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1417 prec_index + i);
1418 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1419 ilk_lut_10(&lut[i]));
1420 }
1421
1422 /*
1423 * Reset the index, otherwise it prevents the legacy palette to be
1424 * written properly.
1425 */
1426 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1427 PAL_PREC_INDEX_VALUE(0));
1428 }
1429
1430 /* 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)1431 static void bdw_load_lut_10(const struct intel_crtc_state *crtc_state,
1432 const struct drm_property_blob *blob,
1433 u32 prec_index)
1434 {
1435 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1436 const struct drm_color_lut *lut = blob->data;
1437 int i, lut_size = drm_color_lut_size(blob);
1438 enum pipe pipe = crtc->pipe;
1439
1440 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1441 prec_index);
1442 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1443 PAL_PREC_AUTO_INCREMENT |
1444 prec_index);
1445
1446 for (i = 0; i < lut_size; i++)
1447 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1448 ilk_lut_10(&lut[i]));
1449
1450 /*
1451 * Reset the index, otherwise it prevents the legacy palette to be
1452 * written properly.
1453 */
1454 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1455 PAL_PREC_INDEX_VALUE(0));
1456 }
1457
ivb_load_lut_ext_max(const struct intel_crtc_state * crtc_state)1458 static void ivb_load_lut_ext_max(const struct intel_crtc_state *crtc_state)
1459 {
1460 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1461 enum pipe pipe = crtc->pipe;
1462
1463 /* Program the max register to clamp values > 1.0. */
1464 ilk_lut_write(crtc_state, PREC_PAL_EXT_GC_MAX(pipe, 0), 1 << 16);
1465 ilk_lut_write(crtc_state, PREC_PAL_EXT_GC_MAX(pipe, 1), 1 << 16);
1466 ilk_lut_write(crtc_state, PREC_PAL_EXT_GC_MAX(pipe, 2), 1 << 16);
1467 }
1468
glk_load_lut_ext2_max(const struct intel_crtc_state * crtc_state)1469 static void glk_load_lut_ext2_max(const struct intel_crtc_state *crtc_state)
1470 {
1471 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1472 enum pipe pipe = crtc->pipe;
1473
1474 /* Program the max register to clamp values > 1.0. */
1475 ilk_lut_write(crtc_state, PREC_PAL_EXT2_GC_MAX(pipe, 0), 1 << 16);
1476 ilk_lut_write(crtc_state, PREC_PAL_EXT2_GC_MAX(pipe, 1), 1 << 16);
1477 ilk_lut_write(crtc_state, PREC_PAL_EXT2_GC_MAX(pipe, 2), 1 << 16);
1478 }
1479
ivb_load_luts(const struct intel_crtc_state * crtc_state)1480 static void ivb_load_luts(const struct intel_crtc_state *crtc_state)
1481 {
1482 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1483 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1484 const struct drm_property_blob *blob = post_csc_lut ?: pre_csc_lut;
1485
1486 switch (crtc_state->gamma_mode) {
1487 case GAMMA_MODE_MODE_8BIT:
1488 ilk_load_lut_8(crtc_state, blob);
1489 break;
1490 case GAMMA_MODE_MODE_SPLIT:
1491 ivb_load_lut_10(crtc_state, pre_csc_lut, PAL_PREC_SPLIT_MODE |
1492 PAL_PREC_INDEX_VALUE(0));
1493 ivb_load_lut_ext_max(crtc_state);
1494 ivb_load_lut_10(crtc_state, post_csc_lut, PAL_PREC_SPLIT_MODE |
1495 PAL_PREC_INDEX_VALUE(512));
1496 break;
1497 case GAMMA_MODE_MODE_10BIT:
1498 ivb_load_lut_10(crtc_state, blob,
1499 PAL_PREC_INDEX_VALUE(0));
1500 ivb_load_lut_ext_max(crtc_state);
1501 break;
1502 default:
1503 MISSING_CASE(crtc_state->gamma_mode);
1504 break;
1505 }
1506 }
1507
bdw_load_luts(const struct intel_crtc_state * crtc_state)1508 static void bdw_load_luts(const struct intel_crtc_state *crtc_state)
1509 {
1510 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1511 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1512 const struct drm_property_blob *blob = post_csc_lut ?: pre_csc_lut;
1513
1514 switch (crtc_state->gamma_mode) {
1515 case GAMMA_MODE_MODE_8BIT:
1516 ilk_load_lut_8(crtc_state, blob);
1517 break;
1518 case GAMMA_MODE_MODE_SPLIT:
1519 bdw_load_lut_10(crtc_state, pre_csc_lut, PAL_PREC_SPLIT_MODE |
1520 PAL_PREC_INDEX_VALUE(0));
1521 ivb_load_lut_ext_max(crtc_state);
1522 bdw_load_lut_10(crtc_state, post_csc_lut, PAL_PREC_SPLIT_MODE |
1523 PAL_PREC_INDEX_VALUE(512));
1524 break;
1525 case GAMMA_MODE_MODE_10BIT:
1526 bdw_load_lut_10(crtc_state, blob,
1527 PAL_PREC_INDEX_VALUE(0));
1528 ivb_load_lut_ext_max(crtc_state);
1529 break;
1530 default:
1531 MISSING_CASE(crtc_state->gamma_mode);
1532 break;
1533 }
1534 }
1535
glk_degamma_lut_size(struct drm_i915_private * i915)1536 static int glk_degamma_lut_size(struct drm_i915_private *i915)
1537 {
1538 if (DISPLAY_VER(i915) >= 13)
1539 return 131;
1540 else
1541 return 35;
1542 }
1543
glk_degamma_lut(const struct drm_color_lut * color)1544 static u32 glk_degamma_lut(const struct drm_color_lut *color)
1545 {
1546 return color->green;
1547 }
1548
glk_degamma_lut_pack(struct drm_color_lut * entry,u32 val)1549 static void glk_degamma_lut_pack(struct drm_color_lut *entry, u32 val)
1550 {
1551 /* PRE_CSC_GAMC_DATA is 3.16, clamp to 0.16 */
1552 entry->red = entry->green = entry->blue = min(val, 0xffffu);
1553 }
1554
mtl_degamma_lut(const struct drm_color_lut * color)1555 static u32 mtl_degamma_lut(const struct drm_color_lut *color)
1556 {
1557 return drm_color_lut_extract(color->green, 24);
1558 }
1559
mtl_degamma_lut_pack(struct drm_color_lut * entry,u32 val)1560 static void mtl_degamma_lut_pack(struct drm_color_lut *entry, u32 val)
1561 {
1562 /* PRE_CSC_GAMC_DATA is 3.24, clamp to 0.16 */
1563 entry->red = entry->green = entry->blue =
1564 intel_color_lut_pack(min(val, 0xffffffu), 24);
1565 }
1566
glk_load_degamma_lut(const struct intel_crtc_state * crtc_state,const struct drm_property_blob * blob)1567 static void glk_load_degamma_lut(const struct intel_crtc_state *crtc_state,
1568 const struct drm_property_blob *blob)
1569 {
1570 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1571 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1572 const struct drm_color_lut *lut = blob->data;
1573 int i, lut_size = drm_color_lut_size(blob);
1574 enum pipe pipe = crtc->pipe;
1575
1576 /*
1577 * When setting the auto-increment bit, the hardware seems to
1578 * ignore the index bits, so we need to reset it to index 0
1579 * separately.
1580 */
1581 ilk_lut_write(crtc_state, PRE_CSC_GAMC_INDEX(pipe),
1582 PRE_CSC_GAMC_INDEX_VALUE(0));
1583 ilk_lut_write(crtc_state, PRE_CSC_GAMC_INDEX(pipe),
1584 PRE_CSC_GAMC_AUTO_INCREMENT |
1585 PRE_CSC_GAMC_INDEX_VALUE(0));
1586
1587 for (i = 0; i < lut_size; i++) {
1588 /*
1589 * First lut_size entries represent range from 0 to 1.0
1590 * 3 additional lut entries will represent extended range
1591 * inputs 3.0 and 7.0 respectively, currently clamped
1592 * at 1.0. Since the precision is 16bit, the user
1593 * value can be directly filled to register.
1594 * The pipe degamma table in GLK+ onwards doesn't
1595 * support different values per channel, so this just
1596 * programs green value which will be equal to Red and
1597 * Blue into the lut registers.
1598 * ToDo: Extend to max 7.0. Enable 32 bit input value
1599 * as compared to just 16 to achieve this.
1600 */
1601 ilk_lut_write(crtc_state, PRE_CSC_GAMC_DATA(pipe),
1602 DISPLAY_VER(i915) >= 14 ?
1603 mtl_degamma_lut(&lut[i]) : glk_degamma_lut(&lut[i]));
1604 }
1605
1606 /* Clamp values > 1.0. */
1607 while (i++ < glk_degamma_lut_size(i915))
1608 ilk_lut_write(crtc_state, PRE_CSC_GAMC_DATA(pipe),
1609 DISPLAY_VER(i915) >= 14 ?
1610 1 << 24 : 1 << 16);
1611
1612 ilk_lut_write(crtc_state, PRE_CSC_GAMC_INDEX(pipe), 0);
1613 }
1614
glk_load_luts(const struct intel_crtc_state * crtc_state)1615 static void glk_load_luts(const struct intel_crtc_state *crtc_state)
1616 {
1617 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1618 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1619
1620 if (pre_csc_lut)
1621 glk_load_degamma_lut(crtc_state, pre_csc_lut);
1622
1623 switch (crtc_state->gamma_mode) {
1624 case GAMMA_MODE_MODE_8BIT:
1625 ilk_load_lut_8(crtc_state, post_csc_lut);
1626 break;
1627 case GAMMA_MODE_MODE_10BIT:
1628 bdw_load_lut_10(crtc_state, post_csc_lut, PAL_PREC_INDEX_VALUE(0));
1629 ivb_load_lut_ext_max(crtc_state);
1630 glk_load_lut_ext2_max(crtc_state);
1631 break;
1632 default:
1633 MISSING_CASE(crtc_state->gamma_mode);
1634 break;
1635 }
1636 }
1637
1638 static void
ivb_load_lut_max(const struct intel_crtc_state * crtc_state,const struct drm_color_lut * color)1639 ivb_load_lut_max(const struct intel_crtc_state *crtc_state,
1640 const struct drm_color_lut *color)
1641 {
1642 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1643 enum pipe pipe = crtc->pipe;
1644
1645 /* FIXME LUT entries are 16 bit only, so we can prog 0xFFFF max */
1646 ilk_lut_write(crtc_state, PREC_PAL_GC_MAX(pipe, 0), color->red);
1647 ilk_lut_write(crtc_state, PREC_PAL_GC_MAX(pipe, 1), color->green);
1648 ilk_lut_write(crtc_state, PREC_PAL_GC_MAX(pipe, 2), color->blue);
1649 }
1650
1651 static void
icl_program_gamma_superfine_segment(const struct intel_crtc_state * crtc_state)1652 icl_program_gamma_superfine_segment(const struct intel_crtc_state *crtc_state)
1653 {
1654 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1655 const struct drm_property_blob *blob = crtc_state->post_csc_lut;
1656 const struct drm_color_lut *lut = blob->data;
1657 enum pipe pipe = crtc->pipe;
1658 int i;
1659
1660 /*
1661 * Program Super Fine segment (let's call it seg1)...
1662 *
1663 * Super Fine segment's step is 1/(8 * 128 * 256) and it has
1664 * 9 entries, corresponding to values 0, 1/(8 * 128 * 256),
1665 * 2/(8 * 128 * 256) ... 8/(8 * 128 * 256).
1666 */
1667 ilk_lut_write(crtc_state, PREC_PAL_MULTI_SEG_INDEX(pipe),
1668 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
1669 ilk_lut_write(crtc_state, PREC_PAL_MULTI_SEG_INDEX(pipe),
1670 PAL_PREC_AUTO_INCREMENT |
1671 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
1672
1673 for (i = 0; i < 9; i++) {
1674 const struct drm_color_lut *entry = &lut[i];
1675
1676 ilk_lut_write(crtc_state, PREC_PAL_MULTI_SEG_DATA(pipe),
1677 ilk_lut_12p4_ldw(entry));
1678 ilk_lut_write(crtc_state, PREC_PAL_MULTI_SEG_DATA(pipe),
1679 ilk_lut_12p4_udw(entry));
1680 }
1681
1682 ilk_lut_write(crtc_state, PREC_PAL_MULTI_SEG_INDEX(pipe),
1683 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
1684 }
1685
1686 static void
icl_program_gamma_multi_segment(const struct intel_crtc_state * crtc_state)1687 icl_program_gamma_multi_segment(const struct intel_crtc_state *crtc_state)
1688 {
1689 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1690 const struct drm_property_blob *blob = crtc_state->post_csc_lut;
1691 const struct drm_color_lut *lut = blob->data;
1692 const struct drm_color_lut *entry;
1693 enum pipe pipe = crtc->pipe;
1694 int i;
1695
1696 /*
1697 * Program Fine segment (let's call it seg2)...
1698 *
1699 * Fine segment's step is 1/(128 * 256) i.e. 1/(128 * 256), 2/(128 * 256)
1700 * ... 256/(128 * 256). So in order to program fine segment of LUT we
1701 * need to pick every 8th entry in the LUT, and program 256 indexes.
1702 *
1703 * PAL_PREC_INDEX[0] and PAL_PREC_INDEX[1] map to seg2[1],
1704 * seg2[0] being unused by the hardware.
1705 */
1706 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1707 PAL_PREC_INDEX_VALUE(0));
1708 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1709 PAL_PREC_AUTO_INCREMENT |
1710 PAL_PREC_INDEX_VALUE(0));
1711
1712 for (i = 1; i < 257; i++) {
1713 entry = &lut[i * 8];
1714
1715 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1716 ilk_lut_12p4_ldw(entry));
1717 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1718 ilk_lut_12p4_udw(entry));
1719 }
1720
1721 /*
1722 * Program Coarse segment (let's call it seg3)...
1723 *
1724 * Coarse segment starts from index 0 and it's step is 1/256 ie 0,
1725 * 1/256, 2/256 ... 256/256. As per the description of each entry in LUT
1726 * above, we need to pick every (8 * 128)th entry in LUT, and
1727 * program 256 of those.
1728 *
1729 * Spec is not very clear about if entries seg3[0] and seg3[1] are
1730 * being used or not, but we still need to program these to advance
1731 * the index.
1732 */
1733 for (i = 0; i < 256; i++) {
1734 entry = &lut[i * 8 * 128];
1735
1736 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1737 ilk_lut_12p4_ldw(entry));
1738 ilk_lut_write(crtc_state, PREC_PAL_DATA(pipe),
1739 ilk_lut_12p4_udw(entry));
1740 }
1741
1742 ilk_lut_write(crtc_state, PREC_PAL_INDEX(pipe),
1743 PAL_PREC_INDEX_VALUE(0));
1744
1745 /* The last entry in the LUT is to be programmed in GCMAX */
1746 entry = &lut[256 * 8 * 128];
1747 ivb_load_lut_max(crtc_state, entry);
1748 }
1749
icl_load_luts(const struct intel_crtc_state * crtc_state)1750 static void icl_load_luts(const struct intel_crtc_state *crtc_state)
1751 {
1752 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1753 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1754
1755 if (pre_csc_lut)
1756 glk_load_degamma_lut(crtc_state, pre_csc_lut);
1757
1758 switch (crtc_state->gamma_mode & GAMMA_MODE_MODE_MASK) {
1759 case GAMMA_MODE_MODE_8BIT:
1760 ilk_load_lut_8(crtc_state, post_csc_lut);
1761 break;
1762 case GAMMA_MODE_MODE_12BIT_MULTI_SEG:
1763 icl_program_gamma_superfine_segment(crtc_state);
1764 icl_program_gamma_multi_segment(crtc_state);
1765 ivb_load_lut_ext_max(crtc_state);
1766 glk_load_lut_ext2_max(crtc_state);
1767 break;
1768 case GAMMA_MODE_MODE_10BIT:
1769 bdw_load_lut_10(crtc_state, post_csc_lut, PAL_PREC_INDEX_VALUE(0));
1770 ivb_load_lut_ext_max(crtc_state);
1771 glk_load_lut_ext2_max(crtc_state);
1772 break;
1773 default:
1774 MISSING_CASE(crtc_state->gamma_mode);
1775 break;
1776 }
1777 }
1778
vlv_load_luts(const struct intel_crtc_state * crtc_state)1779 static void vlv_load_luts(const struct intel_crtc_state *crtc_state)
1780 {
1781 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1782
1783 if (crtc_state->wgc_enable)
1784 vlv_load_wgc_csc(crtc, &crtc_state->csc);
1785
1786 i965_load_luts(crtc_state);
1787 }
1788
chv_cgm_degamma_ldw(const struct drm_color_lut * color)1789 static u32 chv_cgm_degamma_ldw(const struct drm_color_lut *color)
1790 {
1791 return REG_FIELD_PREP(CGM_PIPE_DEGAMMA_GREEN_LDW_MASK, drm_color_lut_extract(color->green, 14)) |
1792 REG_FIELD_PREP(CGM_PIPE_DEGAMMA_BLUE_LDW_MASK, drm_color_lut_extract(color->blue, 14));
1793 }
1794
chv_cgm_degamma_udw(const struct drm_color_lut * color)1795 static u32 chv_cgm_degamma_udw(const struct drm_color_lut *color)
1796 {
1797 return REG_FIELD_PREP(CGM_PIPE_DEGAMMA_RED_UDW_MASK, drm_color_lut_extract(color->red, 14));
1798 }
1799
chv_cgm_degamma_pack(struct drm_color_lut * entry,u32 ldw,u32 udw)1800 static void chv_cgm_degamma_pack(struct drm_color_lut *entry, u32 ldw, u32 udw)
1801 {
1802 entry->green = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_DEGAMMA_GREEN_LDW_MASK, ldw), 14);
1803 entry->blue = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_DEGAMMA_BLUE_LDW_MASK, ldw), 14);
1804 entry->red = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_DEGAMMA_RED_UDW_MASK, udw), 14);
1805 }
1806
chv_load_cgm_degamma(struct intel_crtc * crtc,const struct drm_property_blob * blob)1807 static void chv_load_cgm_degamma(struct intel_crtc *crtc,
1808 const struct drm_property_blob *blob)
1809 {
1810 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1811 const struct drm_color_lut *lut = blob->data;
1812 int i, lut_size = drm_color_lut_size(blob);
1813 enum pipe pipe = crtc->pipe;
1814
1815 for (i = 0; i < lut_size; i++) {
1816 intel_de_write_fw(i915, CGM_PIPE_DEGAMMA(pipe, i, 0),
1817 chv_cgm_degamma_ldw(&lut[i]));
1818 intel_de_write_fw(i915, CGM_PIPE_DEGAMMA(pipe, i, 1),
1819 chv_cgm_degamma_udw(&lut[i]));
1820 }
1821 }
1822
chv_cgm_gamma_ldw(const struct drm_color_lut * color)1823 static u32 chv_cgm_gamma_ldw(const struct drm_color_lut *color)
1824 {
1825 return REG_FIELD_PREP(CGM_PIPE_GAMMA_GREEN_LDW_MASK, drm_color_lut_extract(color->green, 10)) |
1826 REG_FIELD_PREP(CGM_PIPE_GAMMA_BLUE_LDW_MASK, drm_color_lut_extract(color->blue, 10));
1827 }
1828
chv_cgm_gamma_udw(const struct drm_color_lut * color)1829 static u32 chv_cgm_gamma_udw(const struct drm_color_lut *color)
1830 {
1831 return REG_FIELD_PREP(CGM_PIPE_GAMMA_RED_UDW_MASK, drm_color_lut_extract(color->red, 10));
1832 }
1833
chv_cgm_gamma_pack(struct drm_color_lut * entry,u32 ldw,u32 udw)1834 static void chv_cgm_gamma_pack(struct drm_color_lut *entry, u32 ldw, u32 udw)
1835 {
1836 entry->green = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_GAMMA_GREEN_LDW_MASK, ldw), 10);
1837 entry->blue = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_GAMMA_BLUE_LDW_MASK, ldw), 10);
1838 entry->red = intel_color_lut_pack(REG_FIELD_GET(CGM_PIPE_GAMMA_RED_UDW_MASK, udw), 10);
1839 }
1840
chv_load_cgm_gamma(struct intel_crtc * crtc,const struct drm_property_blob * blob)1841 static void chv_load_cgm_gamma(struct intel_crtc *crtc,
1842 const struct drm_property_blob *blob)
1843 {
1844 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1845 const struct drm_color_lut *lut = blob->data;
1846 int i, lut_size = drm_color_lut_size(blob);
1847 enum pipe pipe = crtc->pipe;
1848
1849 for (i = 0; i < lut_size; i++) {
1850 intel_de_write_fw(i915, CGM_PIPE_GAMMA(pipe, i, 0),
1851 chv_cgm_gamma_ldw(&lut[i]));
1852 intel_de_write_fw(i915, CGM_PIPE_GAMMA(pipe, i, 1),
1853 chv_cgm_gamma_udw(&lut[i]));
1854 }
1855 }
1856
chv_load_luts(const struct intel_crtc_state * crtc_state)1857 static void chv_load_luts(const struct intel_crtc_state *crtc_state)
1858 {
1859 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1860 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1861 const struct drm_property_blob *pre_csc_lut = crtc_state->pre_csc_lut;
1862 const struct drm_property_blob *post_csc_lut = crtc_state->post_csc_lut;
1863
1864 if (crtc_state->cgm_mode & CGM_PIPE_MODE_CSC)
1865 chv_load_cgm_csc(crtc, &crtc_state->csc);
1866
1867 if (crtc_state->cgm_mode & CGM_PIPE_MODE_DEGAMMA)
1868 chv_load_cgm_degamma(crtc, pre_csc_lut);
1869
1870 if (crtc_state->cgm_mode & CGM_PIPE_MODE_GAMMA)
1871 chv_load_cgm_gamma(crtc, post_csc_lut);
1872 else
1873 i965_load_luts(crtc_state);
1874
1875 intel_de_write_fw(i915, CGM_PIPE_MODE(crtc->pipe),
1876 crtc_state->cgm_mode);
1877 }
1878
intel_color_load_luts(const struct intel_crtc_state * crtc_state)1879 void intel_color_load_luts(const struct intel_crtc_state *crtc_state)
1880 {
1881 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1882
1883 if (crtc_state->dsb_color_vblank)
1884 return;
1885
1886 i915->display.funcs.color->load_luts(crtc_state);
1887 }
1888
intel_color_commit_noarm(const struct intel_crtc_state * crtc_state)1889 void intel_color_commit_noarm(const struct intel_crtc_state *crtc_state)
1890 {
1891 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1892
1893 if (i915->display.funcs.color->color_commit_noarm)
1894 i915->display.funcs.color->color_commit_noarm(crtc_state);
1895 }
1896
intel_color_commit_arm(const struct intel_crtc_state * crtc_state)1897 void intel_color_commit_arm(const struct intel_crtc_state *crtc_state)
1898 {
1899 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1900
1901 i915->display.funcs.color->color_commit_arm(crtc_state);
1902
1903 if (crtc_state->dsb_color_commit)
1904 intel_dsb_commit(crtc_state->dsb_color_commit, false);
1905 }
1906
intel_color_post_update(const struct intel_crtc_state * crtc_state)1907 void intel_color_post_update(const struct intel_crtc_state *crtc_state)
1908 {
1909 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1910
1911 if (i915->display.funcs.color->color_post_update)
1912 i915->display.funcs.color->color_post_update(crtc_state);
1913 }
1914
intel_color_modeset(const struct intel_crtc_state * crtc_state)1915 void intel_color_modeset(const struct intel_crtc_state *crtc_state)
1916 {
1917 struct intel_display *display = to_intel_display(crtc_state);
1918
1919 intel_color_load_luts(crtc_state);
1920 intel_color_commit_noarm(crtc_state);
1921 intel_color_commit_arm(crtc_state);
1922
1923 if (DISPLAY_VER(display) < 9) {
1924 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1925 struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1926
1927 /* update DSPCNTR to configure gamma/csc for pipe bottom color */
1928 plane->disable_arm(NULL, plane, crtc_state);
1929 }
1930 }
1931
intel_color_prepare_commit(struct intel_atomic_state * state,struct intel_crtc * crtc)1932 void intel_color_prepare_commit(struct intel_atomic_state *state,
1933 struct intel_crtc *crtc)
1934 {
1935 struct drm_i915_private *i915 = to_i915(state->base.dev);
1936 struct intel_crtc_state *crtc_state =
1937 intel_atomic_get_new_crtc_state(state, crtc);
1938
1939 if (!crtc_state->hw.active ||
1940 intel_crtc_needs_modeset(crtc_state))
1941 return;
1942
1943 if (!intel_crtc_needs_color_update(crtc_state))
1944 return;
1945
1946 if (!crtc_state->pre_csc_lut && !crtc_state->post_csc_lut)
1947 return;
1948
1949 crtc_state->dsb_color_vblank = intel_dsb_prepare(state, crtc, INTEL_DSB_1, 1024);
1950 if (!crtc_state->dsb_color_vblank)
1951 return;
1952
1953 i915->display.funcs.color->load_luts(crtc_state);
1954
1955 intel_dsb_finish(crtc_state->dsb_color_vblank);
1956
1957 crtc_state->dsb_color_commit = intel_dsb_prepare(state, crtc, INTEL_DSB_0, 16);
1958 if (!crtc_state->dsb_color_commit) {
1959 intel_dsb_cleanup(crtc_state->dsb_color_vblank);
1960 crtc_state->dsb_color_vblank = NULL;
1961 return;
1962 }
1963
1964 intel_dsb_chain(state, crtc_state->dsb_color_commit,
1965 crtc_state->dsb_color_vblank, true);
1966
1967 intel_dsb_finish(crtc_state->dsb_color_commit);
1968 }
1969
intel_color_cleanup_commit(struct intel_crtc_state * crtc_state)1970 void intel_color_cleanup_commit(struct intel_crtc_state *crtc_state)
1971 {
1972 if (crtc_state->dsb_color_commit) {
1973 intel_dsb_cleanup(crtc_state->dsb_color_commit);
1974 crtc_state->dsb_color_commit = NULL;
1975 }
1976
1977 if (crtc_state->dsb_color_vblank) {
1978 intel_dsb_cleanup(crtc_state->dsb_color_vblank);
1979 crtc_state->dsb_color_vblank = NULL;
1980 }
1981 }
1982
intel_color_wait_commit(const struct intel_crtc_state * crtc_state)1983 void intel_color_wait_commit(const struct intel_crtc_state *crtc_state)
1984 {
1985 if (crtc_state->dsb_color_commit)
1986 intel_dsb_wait(crtc_state->dsb_color_commit);
1987 if (crtc_state->dsb_color_vblank)
1988 intel_dsb_wait(crtc_state->dsb_color_vblank);
1989 }
1990
intel_color_uses_dsb(const struct intel_crtc_state * crtc_state)1991 bool intel_color_uses_dsb(const struct intel_crtc_state *crtc_state)
1992 {
1993 return crtc_state->dsb_color_vblank;
1994 }
1995
intel_can_preload_luts(struct intel_atomic_state * state,struct intel_crtc * crtc)1996 static bool intel_can_preload_luts(struct intel_atomic_state *state,
1997 struct intel_crtc *crtc)
1998 {
1999 const struct intel_crtc_state *old_crtc_state =
2000 intel_atomic_get_old_crtc_state(state, crtc);
2001
2002 return !old_crtc_state->post_csc_lut &&
2003 !old_crtc_state->pre_csc_lut;
2004 }
2005
vlv_can_preload_luts(struct intel_atomic_state * state,struct intel_crtc * crtc)2006 static bool vlv_can_preload_luts(struct intel_atomic_state *state,
2007 struct intel_crtc *crtc)
2008 {
2009 const struct intel_crtc_state *old_crtc_state =
2010 intel_atomic_get_old_crtc_state(state, crtc);
2011
2012 return !old_crtc_state->wgc_enable &&
2013 !old_crtc_state->post_csc_lut;
2014 }
2015
chv_can_preload_luts(struct intel_atomic_state * state,struct intel_crtc * crtc)2016 static bool chv_can_preload_luts(struct intel_atomic_state *state,
2017 struct intel_crtc *crtc)
2018 {
2019 const struct intel_crtc_state *old_crtc_state =
2020 intel_atomic_get_old_crtc_state(state, crtc);
2021 const struct intel_crtc_state *new_crtc_state =
2022 intel_atomic_get_new_crtc_state(state, crtc);
2023
2024 /*
2025 * CGM_PIPE_MODE is itself single buffered. We'd have to
2026 * somehow split it out from chv_load_luts() if we wanted
2027 * the ability to preload the CGM LUTs/CSC without tearing.
2028 */
2029 if (old_crtc_state->cgm_mode || new_crtc_state->cgm_mode)
2030 return false;
2031
2032 return vlv_can_preload_luts(state, crtc);
2033 }
2034
intel_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2035 int intel_color_check(struct intel_atomic_state *state,
2036 struct intel_crtc *crtc)
2037 {
2038 struct drm_i915_private *i915 = to_i915(state->base.dev);
2039 const struct intel_crtc_state *old_crtc_state =
2040 intel_atomic_get_old_crtc_state(state, crtc);
2041 struct intel_crtc_state *new_crtc_state =
2042 intel_atomic_get_new_crtc_state(state, crtc);
2043
2044 /*
2045 * May need to update pipe gamma enable bits
2046 * when C8 planes are getting enabled/disabled.
2047 */
2048 if (!old_crtc_state->c8_planes != !new_crtc_state->c8_planes)
2049 new_crtc_state->uapi.color_mgmt_changed = true;
2050
2051 if (!intel_crtc_needs_color_update(new_crtc_state))
2052 return 0;
2053
2054 return i915->display.funcs.color->color_check(state, crtc);
2055 }
2056
intel_color_get_config(struct intel_crtc_state * crtc_state)2057 void intel_color_get_config(struct intel_crtc_state *crtc_state)
2058 {
2059 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2060
2061 if (i915->display.funcs.color->get_config)
2062 i915->display.funcs.color->get_config(crtc_state);
2063
2064 i915->display.funcs.color->read_luts(crtc_state);
2065
2066 if (i915->display.funcs.color->read_csc)
2067 i915->display.funcs.color->read_csc(crtc_state);
2068 }
2069
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)2070 bool intel_color_lut_equal(const struct intel_crtc_state *crtc_state,
2071 const struct drm_property_blob *blob1,
2072 const struct drm_property_blob *blob2,
2073 bool is_pre_csc_lut)
2074 {
2075 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2076
2077 /*
2078 * FIXME c8_planes readout missing thus
2079 * .read_luts() doesn't read out post_csc_lut.
2080 */
2081 if (!is_pre_csc_lut && crtc_state->c8_planes)
2082 return true;
2083
2084 return i915->display.funcs.color->lut_equal(crtc_state, blob1, blob2,
2085 is_pre_csc_lut);
2086 }
2087
need_plane_update(struct intel_plane * plane,const struct intel_crtc_state * crtc_state)2088 static bool need_plane_update(struct intel_plane *plane,
2089 const struct intel_crtc_state *crtc_state)
2090 {
2091 struct drm_i915_private *i915 = to_i915(plane->base.dev);
2092
2093 /*
2094 * On pre-SKL the pipe gamma enable and pipe csc enable for
2095 * the pipe bottom color are configured via the primary plane.
2096 * We have to reconfigure that even if the plane is inactive.
2097 */
2098 return crtc_state->active_planes & BIT(plane->id) ||
2099 (DISPLAY_VER(i915) < 9 &&
2100 plane->id == PLANE_PRIMARY);
2101 }
2102
2103 static int
intel_color_add_affected_planes(struct intel_atomic_state * state,struct intel_crtc * crtc)2104 intel_color_add_affected_planes(struct intel_atomic_state *state,
2105 struct intel_crtc *crtc)
2106 {
2107 struct drm_i915_private *i915 = to_i915(state->base.dev);
2108 const struct intel_crtc_state *old_crtc_state =
2109 intel_atomic_get_old_crtc_state(state, crtc);
2110 struct intel_crtc_state *new_crtc_state =
2111 intel_atomic_get_new_crtc_state(state, crtc);
2112 struct intel_plane *plane;
2113
2114 if (!new_crtc_state->hw.active ||
2115 intel_crtc_needs_modeset(new_crtc_state))
2116 return 0;
2117
2118 if (new_crtc_state->gamma_enable == old_crtc_state->gamma_enable &&
2119 new_crtc_state->csc_enable == old_crtc_state->csc_enable)
2120 return 0;
2121
2122 for_each_intel_plane_on_crtc(&i915->drm, crtc, plane) {
2123 struct intel_plane_state *plane_state;
2124
2125 if (!need_plane_update(plane, new_crtc_state))
2126 continue;
2127
2128 plane_state = intel_atomic_get_plane_state(state, plane);
2129 if (IS_ERR(plane_state))
2130 return PTR_ERR(plane_state);
2131
2132 new_crtc_state->update_planes |= BIT(plane->id);
2133 new_crtc_state->async_flip_planes = 0;
2134 new_crtc_state->do_async_flip = false;
2135
2136 /* plane control register changes blocked by CxSR */
2137 if (HAS_GMCH(i915))
2138 new_crtc_state->disable_cxsr = true;
2139 }
2140
2141 return 0;
2142 }
2143
intel_gamma_lut_tests(const struct intel_crtc_state * crtc_state)2144 static u32 intel_gamma_lut_tests(const struct intel_crtc_state *crtc_state)
2145 {
2146 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2147 const struct drm_property_blob *gamma_lut = crtc_state->hw.gamma_lut;
2148
2149 if (lut_is_legacy(gamma_lut))
2150 return 0;
2151
2152 return DISPLAY_INFO(i915)->color.gamma_lut_tests;
2153 }
2154
intel_degamma_lut_tests(const struct intel_crtc_state * crtc_state)2155 static u32 intel_degamma_lut_tests(const struct intel_crtc_state *crtc_state)
2156 {
2157 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2158
2159 return DISPLAY_INFO(i915)->color.degamma_lut_tests;
2160 }
2161
intel_gamma_lut_size(const struct intel_crtc_state * crtc_state)2162 static int intel_gamma_lut_size(const struct intel_crtc_state *crtc_state)
2163 {
2164 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2165 const struct drm_property_blob *gamma_lut = crtc_state->hw.gamma_lut;
2166
2167 if (lut_is_legacy(gamma_lut))
2168 return LEGACY_LUT_LENGTH;
2169
2170 return DISPLAY_INFO(i915)->color.gamma_lut_size;
2171 }
2172
intel_degamma_lut_size(const struct intel_crtc_state * crtc_state)2173 static u32 intel_degamma_lut_size(const struct intel_crtc_state *crtc_state)
2174 {
2175 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2176
2177 return DISPLAY_INFO(i915)->color.degamma_lut_size;
2178 }
2179
check_lut_size(struct drm_i915_private * i915,const struct drm_property_blob * lut,int expected)2180 static int check_lut_size(struct drm_i915_private *i915,
2181 const struct drm_property_blob *lut, int expected)
2182 {
2183 int len;
2184
2185 if (!lut)
2186 return 0;
2187
2188 len = drm_color_lut_size(lut);
2189 if (len != expected) {
2190 drm_dbg_kms(&i915->drm, "Invalid LUT size; got %d, expected %d\n",
2191 len, expected);
2192 return -EINVAL;
2193 }
2194
2195 return 0;
2196 }
2197
_check_luts(const struct intel_crtc_state * crtc_state,u32 degamma_tests,u32 gamma_tests)2198 static int _check_luts(const struct intel_crtc_state *crtc_state,
2199 u32 degamma_tests, u32 gamma_tests)
2200 {
2201 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2202 const struct drm_property_blob *gamma_lut = crtc_state->hw.gamma_lut;
2203 const struct drm_property_blob *degamma_lut = crtc_state->hw.degamma_lut;
2204 int gamma_length, degamma_length;
2205
2206 /* C8 relies on its palette being stored in the legacy LUT */
2207 if (crtc_state->c8_planes && !lut_is_legacy(crtc_state->hw.gamma_lut)) {
2208 drm_dbg_kms(&i915->drm,
2209 "C8 pixelformat requires the legacy LUT\n");
2210 return -EINVAL;
2211 }
2212
2213 degamma_length = intel_degamma_lut_size(crtc_state);
2214 gamma_length = intel_gamma_lut_size(crtc_state);
2215
2216 if (check_lut_size(i915, degamma_lut, degamma_length) ||
2217 check_lut_size(i915, gamma_lut, gamma_length))
2218 return -EINVAL;
2219
2220 if (drm_color_lut_check(degamma_lut, degamma_tests) ||
2221 drm_color_lut_check(gamma_lut, gamma_tests))
2222 return -EINVAL;
2223
2224 return 0;
2225 }
2226
check_luts(const struct intel_crtc_state * crtc_state)2227 static int check_luts(const struct intel_crtc_state *crtc_state)
2228 {
2229 return _check_luts(crtc_state,
2230 intel_degamma_lut_tests(crtc_state),
2231 intel_gamma_lut_tests(crtc_state));
2232 }
2233
i9xx_gamma_mode(struct intel_crtc_state * crtc_state)2234 static u32 i9xx_gamma_mode(struct intel_crtc_state *crtc_state)
2235 {
2236 if (!crtc_state->gamma_enable ||
2237 lut_is_legacy(crtc_state->hw.gamma_lut))
2238 return GAMMA_MODE_MODE_8BIT;
2239 else
2240 return GAMMA_MODE_MODE_10BIT;
2241 }
2242
i9xx_lut_10_diff(u16 a,u16 b)2243 static int i9xx_lut_10_diff(u16 a, u16 b)
2244 {
2245 return drm_color_lut_extract(a, 10) -
2246 drm_color_lut_extract(b, 10);
2247 }
2248
i9xx_check_lut_10(struct drm_i915_private * dev_priv,const struct drm_property_blob * blob)2249 static int i9xx_check_lut_10(struct drm_i915_private *dev_priv,
2250 const struct drm_property_blob *blob)
2251 {
2252 const struct drm_color_lut *lut = blob->data;
2253 int lut_size = drm_color_lut_size(blob);
2254 const struct drm_color_lut *a = &lut[lut_size - 2];
2255 const struct drm_color_lut *b = &lut[lut_size - 1];
2256
2257 if (i9xx_lut_10_diff(b->red, a->red) > 0x7f ||
2258 i9xx_lut_10_diff(b->green, a->green) > 0x7f ||
2259 i9xx_lut_10_diff(b->blue, a->blue) > 0x7f) {
2260 drm_dbg_kms(&dev_priv->drm, "Last gamma LUT entry exceeds max slope\n");
2261 return -EINVAL;
2262 }
2263
2264 return 0;
2265 }
2266
intel_color_assert_luts(const struct intel_crtc_state * crtc_state)2267 void intel_color_assert_luts(const struct intel_crtc_state *crtc_state)
2268 {
2269 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2270
2271 /* make sure {pre,post}_csc_lut were correctly assigned */
2272 if (DISPLAY_VER(i915) >= 11 || HAS_GMCH(i915)) {
2273 drm_WARN_ON(&i915->drm,
2274 crtc_state->pre_csc_lut != crtc_state->hw.degamma_lut);
2275 drm_WARN_ON(&i915->drm,
2276 crtc_state->post_csc_lut != crtc_state->hw.gamma_lut);
2277 } else if (DISPLAY_VER(i915) == 10) {
2278 drm_WARN_ON(&i915->drm,
2279 crtc_state->post_csc_lut == crtc_state->hw.gamma_lut &&
2280 crtc_state->pre_csc_lut != crtc_state->hw.degamma_lut &&
2281 crtc_state->pre_csc_lut != i915->display.color.glk_linear_degamma_lut);
2282 drm_WARN_ON(&i915->drm,
2283 !ilk_lut_limited_range(crtc_state) &&
2284 crtc_state->post_csc_lut != NULL &&
2285 crtc_state->post_csc_lut != crtc_state->hw.gamma_lut);
2286 } else if (crtc_state->gamma_mode != GAMMA_MODE_MODE_SPLIT) {
2287 drm_WARN_ON(&i915->drm,
2288 crtc_state->pre_csc_lut != crtc_state->hw.degamma_lut &&
2289 crtc_state->pre_csc_lut != crtc_state->hw.gamma_lut);
2290 drm_WARN_ON(&i915->drm,
2291 !ilk_lut_limited_range(crtc_state) &&
2292 crtc_state->post_csc_lut != crtc_state->hw.degamma_lut &&
2293 crtc_state->post_csc_lut != crtc_state->hw.gamma_lut);
2294 }
2295 }
2296
intel_assign_luts(struct intel_crtc_state * crtc_state)2297 static void intel_assign_luts(struct intel_crtc_state *crtc_state)
2298 {
2299 drm_property_replace_blob(&crtc_state->pre_csc_lut,
2300 crtc_state->hw.degamma_lut);
2301 drm_property_replace_blob(&crtc_state->post_csc_lut,
2302 crtc_state->hw.gamma_lut);
2303 }
2304
i9xx_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2305 static int i9xx_color_check(struct intel_atomic_state *state,
2306 struct intel_crtc *crtc)
2307 {
2308 struct drm_i915_private *i915 = to_i915(state->base.dev);
2309 struct intel_crtc_state *crtc_state =
2310 intel_atomic_get_new_crtc_state(state, crtc);
2311 int ret;
2312
2313 ret = check_luts(crtc_state);
2314 if (ret)
2315 return ret;
2316
2317 crtc_state->gamma_enable =
2318 crtc_state->hw.gamma_lut &&
2319 !crtc_state->c8_planes;
2320
2321 crtc_state->gamma_mode = i9xx_gamma_mode(crtc_state);
2322
2323 if (DISPLAY_VER(i915) < 4 &&
2324 crtc_state->gamma_mode == GAMMA_MODE_MODE_10BIT) {
2325 ret = i9xx_check_lut_10(i915, crtc_state->hw.gamma_lut);
2326 if (ret)
2327 return ret;
2328 }
2329
2330 ret = intel_color_add_affected_planes(state, crtc);
2331 if (ret)
2332 return ret;
2333
2334 intel_assign_luts(crtc_state);
2335
2336 crtc_state->preload_luts = intel_can_preload_luts(state, crtc);
2337
2338 return 0;
2339 }
2340
2341 /*
2342 * VLV color pipeline:
2343 * u0.10 -> WGC csc -> u0.10 -> pipe gamma -> u0.10
2344 */
vlv_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2345 static int vlv_color_check(struct intel_atomic_state *state,
2346 struct intel_crtc *crtc)
2347 {
2348 struct intel_crtc_state *crtc_state =
2349 intel_atomic_get_new_crtc_state(state, crtc);
2350 int ret;
2351
2352 ret = check_luts(crtc_state);
2353 if (ret)
2354 return ret;
2355
2356 crtc_state->gamma_enable =
2357 crtc_state->hw.gamma_lut &&
2358 !crtc_state->c8_planes;
2359
2360 crtc_state->gamma_mode = i9xx_gamma_mode(crtc_state);
2361
2362 crtc_state->wgc_enable = crtc_state->hw.ctm;
2363
2364 ret = intel_color_add_affected_planes(state, crtc);
2365 if (ret)
2366 return ret;
2367
2368 intel_assign_luts(crtc_state);
2369
2370 vlv_assign_csc(crtc_state);
2371
2372 crtc_state->preload_luts = vlv_can_preload_luts(state, crtc);
2373
2374 return 0;
2375 }
2376
chv_cgm_mode(const struct intel_crtc_state * crtc_state)2377 static u32 chv_cgm_mode(const struct intel_crtc_state *crtc_state)
2378 {
2379 u32 cgm_mode = 0;
2380
2381 if (crtc_state->hw.degamma_lut)
2382 cgm_mode |= CGM_PIPE_MODE_DEGAMMA;
2383 if (crtc_state->hw.ctm)
2384 cgm_mode |= CGM_PIPE_MODE_CSC;
2385 if (crtc_state->hw.gamma_lut &&
2386 !lut_is_legacy(crtc_state->hw.gamma_lut))
2387 cgm_mode |= CGM_PIPE_MODE_GAMMA;
2388
2389 /*
2390 * Toggling the CGM CSC on/off outside of the tiny window
2391 * between start of vblank and frame start causes underruns.
2392 * Always enable the CGM CSC as a workaround.
2393 */
2394 cgm_mode |= CGM_PIPE_MODE_CSC;
2395
2396 return cgm_mode;
2397 }
2398
2399 /*
2400 * CHV color pipeline:
2401 * u0.10 -> CGM degamma -> u0.14 -> CGM csc -> u0.14 -> CGM gamma ->
2402 * u0.10 -> WGC csc -> u0.10 -> pipe gamma -> u0.10
2403 *
2404 * We always bypass the WGC csc and use the CGM csc
2405 * instead since it has degamma and better precision.
2406 */
chv_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2407 static int chv_color_check(struct intel_atomic_state *state,
2408 struct intel_crtc *crtc)
2409 {
2410 struct intel_crtc_state *crtc_state =
2411 intel_atomic_get_new_crtc_state(state, crtc);
2412 int ret;
2413
2414 ret = check_luts(crtc_state);
2415 if (ret)
2416 return ret;
2417
2418 /*
2419 * Pipe gamma will be used only for the legacy LUT.
2420 * Otherwise we bypass it and use the CGM gamma instead.
2421 */
2422 crtc_state->gamma_enable =
2423 lut_is_legacy(crtc_state->hw.gamma_lut) &&
2424 !crtc_state->c8_planes;
2425
2426 crtc_state->gamma_mode = GAMMA_MODE_MODE_8BIT;
2427
2428 crtc_state->cgm_mode = chv_cgm_mode(crtc_state);
2429
2430 /*
2431 * We always bypass the WGC CSC and use the CGM CSC
2432 * instead since it has degamma and better precision.
2433 */
2434 crtc_state->wgc_enable = false;
2435
2436 ret = intel_color_add_affected_planes(state, crtc);
2437 if (ret)
2438 return ret;
2439
2440 intel_assign_luts(crtc_state);
2441
2442 chv_assign_csc(crtc_state);
2443
2444 crtc_state->preload_luts = chv_can_preload_luts(state, crtc);
2445
2446 return 0;
2447 }
2448
ilk_gamma_enable(const struct intel_crtc_state * crtc_state)2449 static bool ilk_gamma_enable(const struct intel_crtc_state *crtc_state)
2450 {
2451 return (crtc_state->hw.gamma_lut ||
2452 crtc_state->hw.degamma_lut) &&
2453 !crtc_state->c8_planes;
2454 }
2455
ilk_csc_enable(const struct intel_crtc_state * crtc_state)2456 static bool ilk_csc_enable(const struct intel_crtc_state *crtc_state)
2457 {
2458 return crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
2459 ilk_csc_limited_range(crtc_state) ||
2460 crtc_state->hw.ctm;
2461 }
2462
ilk_gamma_mode(const struct intel_crtc_state * crtc_state)2463 static u32 ilk_gamma_mode(const struct intel_crtc_state *crtc_state)
2464 {
2465 if (!crtc_state->gamma_enable ||
2466 lut_is_legacy(crtc_state->hw.gamma_lut))
2467 return GAMMA_MODE_MODE_8BIT;
2468 else
2469 return GAMMA_MODE_MODE_10BIT;
2470 }
2471
ilk_csc_mode(const struct intel_crtc_state * crtc_state)2472 static u32 ilk_csc_mode(const struct intel_crtc_state *crtc_state)
2473 {
2474 /*
2475 * CSC comes after the LUT in RGB->YCbCr mode.
2476 * RGB->YCbCr needs the limited range offsets added to
2477 * the output. RGB limited range output is handled by
2478 * the hw automagically elsewhere.
2479 */
2480 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
2481 return CSC_BLACK_SCREEN_OFFSET;
2482
2483 if (crtc_state->hw.degamma_lut)
2484 return CSC_MODE_YUV_TO_RGB;
2485
2486 return CSC_MODE_YUV_TO_RGB |
2487 CSC_POSITION_BEFORE_GAMMA;
2488 }
2489
ilk_assign_luts(struct intel_crtc_state * crtc_state)2490 static int ilk_assign_luts(struct intel_crtc_state *crtc_state)
2491 {
2492 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2493
2494 if (ilk_lut_limited_range(crtc_state)) {
2495 struct drm_property_blob *gamma_lut;
2496
2497 gamma_lut = create_resized_lut(i915, crtc_state->hw.gamma_lut,
2498 drm_color_lut_size(crtc_state->hw.gamma_lut),
2499 true);
2500 if (IS_ERR(gamma_lut))
2501 return PTR_ERR(gamma_lut);
2502
2503 drm_property_replace_blob(&crtc_state->post_csc_lut, gamma_lut);
2504
2505 drm_property_blob_put(gamma_lut);
2506
2507 drm_property_replace_blob(&crtc_state->pre_csc_lut, crtc_state->hw.degamma_lut);
2508
2509 return 0;
2510 }
2511
2512 if (crtc_state->hw.degamma_lut ||
2513 crtc_state->csc_mode & CSC_POSITION_BEFORE_GAMMA) {
2514 drm_property_replace_blob(&crtc_state->pre_csc_lut,
2515 crtc_state->hw.degamma_lut);
2516 drm_property_replace_blob(&crtc_state->post_csc_lut,
2517 crtc_state->hw.gamma_lut);
2518 } else {
2519 drm_property_replace_blob(&crtc_state->pre_csc_lut,
2520 crtc_state->hw.gamma_lut);
2521 drm_property_replace_blob(&crtc_state->post_csc_lut,
2522 NULL);
2523 }
2524
2525 return 0;
2526 }
2527
ilk_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2528 static int ilk_color_check(struct intel_atomic_state *state,
2529 struct intel_crtc *crtc)
2530 {
2531 struct drm_i915_private *i915 = to_i915(state->base.dev);
2532 struct intel_crtc_state *crtc_state =
2533 intel_atomic_get_new_crtc_state(state, crtc);
2534 int ret;
2535
2536 ret = check_luts(crtc_state);
2537 if (ret)
2538 return ret;
2539
2540 if (crtc_state->hw.degamma_lut && crtc_state->hw.gamma_lut) {
2541 drm_dbg_kms(&i915->drm,
2542 "Degamma and gamma together are not possible\n");
2543 return -EINVAL;
2544 }
2545
2546 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB &&
2547 crtc_state->hw.ctm) {
2548 drm_dbg_kms(&i915->drm,
2549 "YCbCr and CTM together are not possible\n");
2550 return -EINVAL;
2551 }
2552
2553 crtc_state->gamma_enable = ilk_gamma_enable(crtc_state);
2554
2555 crtc_state->csc_enable = ilk_csc_enable(crtc_state);
2556
2557 crtc_state->gamma_mode = ilk_gamma_mode(crtc_state);
2558
2559 crtc_state->csc_mode = ilk_csc_mode(crtc_state);
2560
2561 ret = intel_color_add_affected_planes(state, crtc);
2562 if (ret)
2563 return ret;
2564
2565 ret = ilk_assign_luts(crtc_state);
2566 if (ret)
2567 return ret;
2568
2569 ilk_assign_csc(crtc_state);
2570
2571 crtc_state->preload_luts = intel_can_preload_luts(state, crtc);
2572
2573 return 0;
2574 }
2575
ivb_gamma_mode(const struct intel_crtc_state * crtc_state)2576 static u32 ivb_gamma_mode(const struct intel_crtc_state *crtc_state)
2577 {
2578 if (crtc_state->hw.degamma_lut && crtc_state->hw.gamma_lut)
2579 return GAMMA_MODE_MODE_SPLIT;
2580
2581 return ilk_gamma_mode(crtc_state);
2582 }
2583
ivb_csc_mode(const struct intel_crtc_state * crtc_state)2584 static u32 ivb_csc_mode(const struct intel_crtc_state *crtc_state)
2585 {
2586 bool limited_color_range = ilk_csc_limited_range(crtc_state);
2587
2588 /*
2589 * CSC comes after the LUT in degamma, RGB->YCbCr,
2590 * and RGB full->limited range mode.
2591 */
2592 if (crtc_state->hw.degamma_lut ||
2593 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
2594 limited_color_range)
2595 return 0;
2596
2597 return CSC_POSITION_BEFORE_GAMMA;
2598 }
2599
ivb_assign_luts(struct intel_crtc_state * crtc_state)2600 static int ivb_assign_luts(struct intel_crtc_state *crtc_state)
2601 {
2602 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2603 struct drm_property_blob *degamma_lut, *gamma_lut;
2604
2605 if (crtc_state->gamma_mode != GAMMA_MODE_MODE_SPLIT)
2606 return ilk_assign_luts(crtc_state);
2607
2608 drm_WARN_ON(&i915->drm, drm_color_lut_size(crtc_state->hw.degamma_lut) != 1024);
2609 drm_WARN_ON(&i915->drm, drm_color_lut_size(crtc_state->hw.gamma_lut) != 1024);
2610
2611 degamma_lut = create_resized_lut(i915, crtc_state->hw.degamma_lut, 512,
2612 false);
2613 if (IS_ERR(degamma_lut))
2614 return PTR_ERR(degamma_lut);
2615
2616 gamma_lut = create_resized_lut(i915, crtc_state->hw.gamma_lut, 512,
2617 ilk_lut_limited_range(crtc_state));
2618 if (IS_ERR(gamma_lut)) {
2619 drm_property_blob_put(degamma_lut);
2620 return PTR_ERR(gamma_lut);
2621 }
2622
2623 drm_property_replace_blob(&crtc_state->pre_csc_lut, degamma_lut);
2624 drm_property_replace_blob(&crtc_state->post_csc_lut, gamma_lut);
2625
2626 drm_property_blob_put(degamma_lut);
2627 drm_property_blob_put(gamma_lut);
2628
2629 return 0;
2630 }
2631
ivb_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2632 static int ivb_color_check(struct intel_atomic_state *state,
2633 struct intel_crtc *crtc)
2634 {
2635 struct drm_i915_private *i915 = to_i915(state->base.dev);
2636 struct intel_crtc_state *crtc_state =
2637 intel_atomic_get_new_crtc_state(state, crtc);
2638 int ret;
2639
2640 ret = check_luts(crtc_state);
2641 if (ret)
2642 return ret;
2643
2644 if (crtc_state->c8_planes && crtc_state->hw.degamma_lut) {
2645 drm_dbg_kms(&i915->drm,
2646 "C8 pixelformat and degamma together are not possible\n");
2647 return -EINVAL;
2648 }
2649
2650 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB &&
2651 crtc_state->hw.ctm) {
2652 drm_dbg_kms(&i915->drm,
2653 "YCbCr and CTM together are not possible\n");
2654 return -EINVAL;
2655 }
2656
2657 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB &&
2658 crtc_state->hw.degamma_lut && crtc_state->hw.gamma_lut) {
2659 drm_dbg_kms(&i915->drm,
2660 "YCbCr and degamma+gamma together are not possible\n");
2661 return -EINVAL;
2662 }
2663
2664 crtc_state->gamma_enable = ilk_gamma_enable(crtc_state);
2665
2666 crtc_state->csc_enable = ilk_csc_enable(crtc_state);
2667
2668 crtc_state->gamma_mode = ivb_gamma_mode(crtc_state);
2669
2670 crtc_state->csc_mode = ivb_csc_mode(crtc_state);
2671
2672 ret = intel_color_add_affected_planes(state, crtc);
2673 if (ret)
2674 return ret;
2675
2676 ret = ivb_assign_luts(crtc_state);
2677 if (ret)
2678 return ret;
2679
2680 ilk_assign_csc(crtc_state);
2681
2682 crtc_state->preload_luts = intel_can_preload_luts(state, crtc);
2683
2684 return 0;
2685 }
2686
glk_gamma_mode(const struct intel_crtc_state * crtc_state)2687 static u32 glk_gamma_mode(const struct intel_crtc_state *crtc_state)
2688 {
2689 if (!crtc_state->gamma_enable ||
2690 lut_is_legacy(crtc_state->hw.gamma_lut))
2691 return GAMMA_MODE_MODE_8BIT;
2692 else
2693 return GAMMA_MODE_MODE_10BIT;
2694 }
2695
glk_use_pre_csc_lut_for_gamma(const struct intel_crtc_state * crtc_state)2696 static bool glk_use_pre_csc_lut_for_gamma(const struct intel_crtc_state *crtc_state)
2697 {
2698 return crtc_state->hw.gamma_lut &&
2699 !crtc_state->c8_planes &&
2700 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB;
2701 }
2702
glk_assign_luts(struct intel_crtc_state * crtc_state)2703 static int glk_assign_luts(struct intel_crtc_state *crtc_state)
2704 {
2705 struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
2706
2707 if (glk_use_pre_csc_lut_for_gamma(crtc_state)) {
2708 struct drm_property_blob *gamma_lut;
2709
2710 gamma_lut = create_resized_lut(i915, crtc_state->hw.gamma_lut,
2711 DISPLAY_INFO(i915)->color.degamma_lut_size,
2712 false);
2713 if (IS_ERR(gamma_lut))
2714 return PTR_ERR(gamma_lut);
2715
2716 drm_property_replace_blob(&crtc_state->pre_csc_lut, gamma_lut);
2717 drm_property_replace_blob(&crtc_state->post_csc_lut, NULL);
2718
2719 drm_property_blob_put(gamma_lut);
2720
2721 return 0;
2722 }
2723
2724 if (ilk_lut_limited_range(crtc_state)) {
2725 struct drm_property_blob *gamma_lut;
2726
2727 gamma_lut = create_resized_lut(i915, crtc_state->hw.gamma_lut,
2728 drm_color_lut_size(crtc_state->hw.gamma_lut),
2729 true);
2730 if (IS_ERR(gamma_lut))
2731 return PTR_ERR(gamma_lut);
2732
2733 drm_property_replace_blob(&crtc_state->post_csc_lut, gamma_lut);
2734
2735 drm_property_blob_put(gamma_lut);
2736 } else {
2737 drm_property_replace_blob(&crtc_state->post_csc_lut, crtc_state->hw.gamma_lut);
2738 }
2739
2740 drm_property_replace_blob(&crtc_state->pre_csc_lut, crtc_state->hw.degamma_lut);
2741
2742 /*
2743 * On GLK+ both pipe CSC and degamma LUT are controlled
2744 * by csc_enable. Hence for the cases where the CSC is
2745 * needed but degamma LUT is not we need to load a
2746 * linear degamma LUT.
2747 */
2748 if (crtc_state->csc_enable && !crtc_state->pre_csc_lut)
2749 drm_property_replace_blob(&crtc_state->pre_csc_lut,
2750 i915->display.color.glk_linear_degamma_lut);
2751
2752 return 0;
2753 }
2754
glk_check_luts(const struct intel_crtc_state * crtc_state)2755 static int glk_check_luts(const struct intel_crtc_state *crtc_state)
2756 {
2757 u32 degamma_tests = intel_degamma_lut_tests(crtc_state);
2758 u32 gamma_tests = intel_gamma_lut_tests(crtc_state);
2759
2760 if (glk_use_pre_csc_lut_for_gamma(crtc_state))
2761 gamma_tests |= degamma_tests;
2762
2763 return _check_luts(crtc_state, degamma_tests, gamma_tests);
2764 }
2765
glk_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2766 static int glk_color_check(struct intel_atomic_state *state,
2767 struct intel_crtc *crtc)
2768 {
2769 struct drm_i915_private *i915 = to_i915(state->base.dev);
2770 struct intel_crtc_state *crtc_state =
2771 intel_atomic_get_new_crtc_state(state, crtc);
2772 int ret;
2773
2774 ret = glk_check_luts(crtc_state);
2775 if (ret)
2776 return ret;
2777
2778 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB &&
2779 crtc_state->hw.ctm) {
2780 drm_dbg_kms(&i915->drm,
2781 "YCbCr and CTM together are not possible\n");
2782 return -EINVAL;
2783 }
2784
2785 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB &&
2786 crtc_state->hw.degamma_lut && crtc_state->hw.gamma_lut) {
2787 drm_dbg_kms(&i915->drm,
2788 "YCbCr and degamma+gamma together are not possible\n");
2789 return -EINVAL;
2790 }
2791
2792 crtc_state->gamma_enable =
2793 !glk_use_pre_csc_lut_for_gamma(crtc_state) &&
2794 crtc_state->hw.gamma_lut &&
2795 !crtc_state->c8_planes;
2796
2797 /* On GLK+ degamma LUT is controlled by csc_enable */
2798 crtc_state->csc_enable =
2799 glk_use_pre_csc_lut_for_gamma(crtc_state) ||
2800 crtc_state->hw.degamma_lut ||
2801 crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
2802 crtc_state->hw.ctm || ilk_csc_limited_range(crtc_state);
2803
2804 crtc_state->gamma_mode = glk_gamma_mode(crtc_state);
2805
2806 crtc_state->csc_mode = 0;
2807
2808 ret = intel_color_add_affected_planes(state, crtc);
2809 if (ret)
2810 return ret;
2811
2812 ret = glk_assign_luts(crtc_state);
2813 if (ret)
2814 return ret;
2815
2816 ilk_assign_csc(crtc_state);
2817
2818 crtc_state->preload_luts = intel_can_preload_luts(state, crtc);
2819
2820 return 0;
2821 }
2822
icl_gamma_mode(const struct intel_crtc_state * crtc_state)2823 static u32 icl_gamma_mode(const struct intel_crtc_state *crtc_state)
2824 {
2825 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2826 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2827 u32 gamma_mode = 0;
2828
2829 if (crtc_state->hw.degamma_lut)
2830 gamma_mode |= PRE_CSC_GAMMA_ENABLE;
2831
2832 if (crtc_state->hw.gamma_lut &&
2833 !crtc_state->c8_planes)
2834 gamma_mode |= POST_CSC_GAMMA_ENABLE;
2835
2836 if (!crtc_state->hw.gamma_lut ||
2837 lut_is_legacy(crtc_state->hw.gamma_lut))
2838 gamma_mode |= GAMMA_MODE_MODE_8BIT;
2839 /*
2840 * Enable 10bit gamma for D13
2841 * ToDo: Extend to Logarithmic Gamma once the new UAPI
2842 * is accepted and implemented by a userspace consumer
2843 */
2844 else if (DISPLAY_VER(i915) >= 13)
2845 gamma_mode |= GAMMA_MODE_MODE_10BIT;
2846 else
2847 gamma_mode |= GAMMA_MODE_MODE_12BIT_MULTI_SEG;
2848
2849 return gamma_mode;
2850 }
2851
icl_csc_mode(const struct intel_crtc_state * crtc_state)2852 static u32 icl_csc_mode(const struct intel_crtc_state *crtc_state)
2853 {
2854 u32 csc_mode = 0;
2855
2856 if (crtc_state->hw.ctm)
2857 csc_mode |= ICL_CSC_ENABLE;
2858
2859 if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB ||
2860 crtc_state->limited_color_range)
2861 csc_mode |= ICL_OUTPUT_CSC_ENABLE;
2862
2863 return csc_mode;
2864 }
2865
icl_color_check(struct intel_atomic_state * state,struct intel_crtc * crtc)2866 static int icl_color_check(struct intel_atomic_state *state,
2867 struct intel_crtc *crtc)
2868 {
2869 struct intel_crtc_state *crtc_state =
2870 intel_atomic_get_new_crtc_state(state, crtc);
2871 int ret;
2872
2873 ret = check_luts(crtc_state);
2874 if (ret)
2875 return ret;
2876
2877 crtc_state->gamma_mode = icl_gamma_mode(crtc_state);
2878
2879 crtc_state->csc_mode = icl_csc_mode(crtc_state);
2880
2881 intel_assign_luts(crtc_state);
2882
2883 icl_assign_csc(crtc_state);
2884
2885 crtc_state->preload_luts = intel_can_preload_luts(state, crtc);
2886
2887 return 0;
2888 }
2889
i9xx_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2890 static int i9xx_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2891 {
2892 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
2893 return 0;
2894
2895 switch (crtc_state->gamma_mode) {
2896 case GAMMA_MODE_MODE_8BIT:
2897 return 8;
2898 case GAMMA_MODE_MODE_10BIT:
2899 return 10;
2900 default:
2901 MISSING_CASE(crtc_state->gamma_mode);
2902 return 0;
2903 }
2904 }
2905
i9xx_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)2906 static int i9xx_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2907 {
2908 return 0;
2909 }
2910
i965_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2911 static int i965_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2912 {
2913 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
2914 return 0;
2915
2916 switch (crtc_state->gamma_mode) {
2917 case GAMMA_MODE_MODE_8BIT:
2918 return 8;
2919 case GAMMA_MODE_MODE_10BIT:
2920 return 16;
2921 default:
2922 MISSING_CASE(crtc_state->gamma_mode);
2923 return 0;
2924 }
2925 }
2926
ilk_gamma_mode_precision(u32 gamma_mode)2927 static int ilk_gamma_mode_precision(u32 gamma_mode)
2928 {
2929 switch (gamma_mode) {
2930 case GAMMA_MODE_MODE_8BIT:
2931 return 8;
2932 case GAMMA_MODE_MODE_10BIT:
2933 return 10;
2934 default:
2935 MISSING_CASE(gamma_mode);
2936 return 0;
2937 }
2938 }
2939
ilk_has_post_csc_lut(const struct intel_crtc_state * crtc_state)2940 static bool ilk_has_post_csc_lut(const struct intel_crtc_state *crtc_state)
2941 {
2942 if (crtc_state->c8_planes)
2943 return true;
2944
2945 return crtc_state->gamma_enable &&
2946 (crtc_state->csc_mode & CSC_POSITION_BEFORE_GAMMA) != 0;
2947 }
2948
ilk_has_pre_csc_lut(const struct intel_crtc_state * crtc_state)2949 static bool ilk_has_pre_csc_lut(const struct intel_crtc_state *crtc_state)
2950 {
2951 return crtc_state->gamma_enable &&
2952 (crtc_state->csc_mode & CSC_POSITION_BEFORE_GAMMA) == 0;
2953 }
2954
ilk_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2955 static int ilk_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2956 {
2957 if (!ilk_has_post_csc_lut(crtc_state))
2958 return 0;
2959
2960 return ilk_gamma_mode_precision(crtc_state->gamma_mode);
2961 }
2962
ilk_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)2963 static int ilk_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2964 {
2965 if (!ilk_has_pre_csc_lut(crtc_state))
2966 return 0;
2967
2968 return ilk_gamma_mode_precision(crtc_state->gamma_mode);
2969 }
2970
ivb_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2971 static int ivb_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2972 {
2973 if (crtc_state->gamma_enable &&
2974 crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
2975 return 10;
2976
2977 return ilk_post_csc_lut_precision(crtc_state);
2978 }
2979
ivb_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)2980 static int ivb_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2981 {
2982 if (crtc_state->gamma_enable &&
2983 crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
2984 return 10;
2985
2986 return ilk_pre_csc_lut_precision(crtc_state);
2987 }
2988
chv_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)2989 static int chv_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2990 {
2991 if (crtc_state->cgm_mode & CGM_PIPE_MODE_GAMMA)
2992 return 10;
2993
2994 return i965_post_csc_lut_precision(crtc_state);
2995 }
2996
chv_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)2997 static int chv_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
2998 {
2999 if (crtc_state->cgm_mode & CGM_PIPE_MODE_DEGAMMA)
3000 return 14;
3001
3002 return 0;
3003 }
3004
glk_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)3005 static int glk_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
3006 {
3007 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3008 return 0;
3009
3010 return ilk_gamma_mode_precision(crtc_state->gamma_mode);
3011 }
3012
glk_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)3013 static int glk_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
3014 {
3015 if (!crtc_state->csc_enable)
3016 return 0;
3017
3018 return 16;
3019 }
3020
icl_has_post_csc_lut(const struct intel_crtc_state * crtc_state)3021 static bool icl_has_post_csc_lut(const struct intel_crtc_state *crtc_state)
3022 {
3023 if (crtc_state->c8_planes)
3024 return true;
3025
3026 return crtc_state->gamma_mode & POST_CSC_GAMMA_ENABLE;
3027 }
3028
icl_has_pre_csc_lut(const struct intel_crtc_state * crtc_state)3029 static bool icl_has_pre_csc_lut(const struct intel_crtc_state *crtc_state)
3030 {
3031 return crtc_state->gamma_mode & PRE_CSC_GAMMA_ENABLE;
3032 }
3033
icl_post_csc_lut_precision(const struct intel_crtc_state * crtc_state)3034 static int icl_post_csc_lut_precision(const struct intel_crtc_state *crtc_state)
3035 {
3036 if (!icl_has_post_csc_lut(crtc_state))
3037 return 0;
3038
3039 switch (crtc_state->gamma_mode & GAMMA_MODE_MODE_MASK) {
3040 case GAMMA_MODE_MODE_8BIT:
3041 return 8;
3042 case GAMMA_MODE_MODE_10BIT:
3043 return 10;
3044 case GAMMA_MODE_MODE_12BIT_MULTI_SEG:
3045 return 16;
3046 default:
3047 MISSING_CASE(crtc_state->gamma_mode);
3048 return 0;
3049 }
3050 }
3051
icl_pre_csc_lut_precision(const struct intel_crtc_state * crtc_state)3052 static int icl_pre_csc_lut_precision(const struct intel_crtc_state *crtc_state)
3053 {
3054 if (!icl_has_pre_csc_lut(crtc_state))
3055 return 0;
3056
3057 return 16;
3058 }
3059
err_check(const struct drm_color_lut * lut1,const struct drm_color_lut * lut2,u32 err)3060 static bool err_check(const struct drm_color_lut *lut1,
3061 const struct drm_color_lut *lut2, u32 err)
3062 {
3063 return ((abs((long)lut2->red - lut1->red)) <= err) &&
3064 ((abs((long)lut2->blue - lut1->blue)) <= err) &&
3065 ((abs((long)lut2->green - lut1->green)) <= err);
3066 }
3067
intel_lut_entries_equal(const struct drm_color_lut * lut1,const struct drm_color_lut * lut2,int lut_size,u32 err)3068 static bool intel_lut_entries_equal(const struct drm_color_lut *lut1,
3069 const struct drm_color_lut *lut2,
3070 int lut_size, u32 err)
3071 {
3072 int i;
3073
3074 for (i = 0; i < lut_size; i++) {
3075 if (!err_check(&lut1[i], &lut2[i], err))
3076 return false;
3077 }
3078
3079 return true;
3080 }
3081
intel_lut_equal(const struct drm_property_blob * blob1,const struct drm_property_blob * blob2,int check_size,int precision)3082 static bool intel_lut_equal(const struct drm_property_blob *blob1,
3083 const struct drm_property_blob *blob2,
3084 int check_size, int precision)
3085 {
3086 const struct drm_color_lut *lut1, *lut2;
3087 int lut_size1, lut_size2;
3088 u32 err;
3089
3090 if (!blob1 != !blob2)
3091 return false;
3092
3093 if (!blob1 != !precision)
3094 return false;
3095
3096 if (!blob1)
3097 return true;
3098
3099 lut_size1 = drm_color_lut_size(blob1);
3100 lut_size2 = drm_color_lut_size(blob2);
3101
3102 if (lut_size1 != lut_size2)
3103 return false;
3104
3105 if (check_size > lut_size1)
3106 return false;
3107
3108 lut1 = blob1->data;
3109 lut2 = blob2->data;
3110
3111 err = 0xffff >> precision;
3112
3113 if (!check_size)
3114 check_size = lut_size1;
3115
3116 return intel_lut_entries_equal(lut1, lut2, check_size, err);
3117 }
3118
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)3119 static bool i9xx_lut_equal(const struct intel_crtc_state *crtc_state,
3120 const struct drm_property_blob *blob1,
3121 const struct drm_property_blob *blob2,
3122 bool is_pre_csc_lut)
3123 {
3124 int check_size = 0;
3125
3126 if (is_pre_csc_lut)
3127 return intel_lut_equal(blob1, blob2, 0,
3128 i9xx_pre_csc_lut_precision(crtc_state));
3129
3130 /* 10bit mode last entry is implicit, just skip it */
3131 if (crtc_state->gamma_mode == GAMMA_MODE_MODE_10BIT)
3132 check_size = 128;
3133
3134 return intel_lut_equal(blob1, blob2, check_size,
3135 i9xx_post_csc_lut_precision(crtc_state));
3136 }
3137
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)3138 static bool i965_lut_equal(const struct intel_crtc_state *crtc_state,
3139 const struct drm_property_blob *blob1,
3140 const struct drm_property_blob *blob2,
3141 bool is_pre_csc_lut)
3142 {
3143 if (is_pre_csc_lut)
3144 return intel_lut_equal(blob1, blob2, 0,
3145 i9xx_pre_csc_lut_precision(crtc_state));
3146 else
3147 return intel_lut_equal(blob1, blob2, 0,
3148 i965_post_csc_lut_precision(crtc_state));
3149 }
3150
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)3151 static bool chv_lut_equal(const struct intel_crtc_state *crtc_state,
3152 const struct drm_property_blob *blob1,
3153 const struct drm_property_blob *blob2,
3154 bool is_pre_csc_lut)
3155 {
3156 if (is_pre_csc_lut)
3157 return intel_lut_equal(blob1, blob2, 0,
3158 chv_pre_csc_lut_precision(crtc_state));
3159 else
3160 return intel_lut_equal(blob1, blob2, 0,
3161 chv_post_csc_lut_precision(crtc_state));
3162 }
3163
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)3164 static bool ilk_lut_equal(const struct intel_crtc_state *crtc_state,
3165 const struct drm_property_blob *blob1,
3166 const struct drm_property_blob *blob2,
3167 bool is_pre_csc_lut)
3168 {
3169 if (is_pre_csc_lut)
3170 return intel_lut_equal(blob1, blob2, 0,
3171 ilk_pre_csc_lut_precision(crtc_state));
3172 else
3173 return intel_lut_equal(blob1, blob2, 0,
3174 ilk_post_csc_lut_precision(crtc_state));
3175 }
3176
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)3177 static bool ivb_lut_equal(const struct intel_crtc_state *crtc_state,
3178 const struct drm_property_blob *blob1,
3179 const struct drm_property_blob *blob2,
3180 bool is_pre_csc_lut)
3181 {
3182 if (is_pre_csc_lut)
3183 return intel_lut_equal(blob1, blob2, 0,
3184 ivb_pre_csc_lut_precision(crtc_state));
3185 else
3186 return intel_lut_equal(blob1, blob2, 0,
3187 ivb_post_csc_lut_precision(crtc_state));
3188 }
3189
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)3190 static bool glk_lut_equal(const struct intel_crtc_state *crtc_state,
3191 const struct drm_property_blob *blob1,
3192 const struct drm_property_blob *blob2,
3193 bool is_pre_csc_lut)
3194 {
3195 if (is_pre_csc_lut)
3196 return intel_lut_equal(blob1, blob2, 0,
3197 glk_pre_csc_lut_precision(crtc_state));
3198 else
3199 return intel_lut_equal(blob1, blob2, 0,
3200 glk_post_csc_lut_precision(crtc_state));
3201 }
3202
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)3203 static bool icl_lut_equal(const struct intel_crtc_state *crtc_state,
3204 const struct drm_property_blob *blob1,
3205 const struct drm_property_blob *blob2,
3206 bool is_pre_csc_lut)
3207 {
3208 int check_size = 0;
3209
3210 if (is_pre_csc_lut)
3211 return intel_lut_equal(blob1, blob2, 0,
3212 icl_pre_csc_lut_precision(crtc_state));
3213
3214 /* hw readout broken except for the super fine segment :( */
3215 if ((crtc_state->gamma_mode & GAMMA_MODE_MODE_MASK) ==
3216 GAMMA_MODE_MODE_12BIT_MULTI_SEG)
3217 check_size = 9;
3218
3219 return intel_lut_equal(blob1, blob2, check_size,
3220 icl_post_csc_lut_precision(crtc_state));
3221 }
3222
i9xx_read_lut_8(struct intel_crtc * crtc)3223 static struct drm_property_blob *i9xx_read_lut_8(struct intel_crtc *crtc)
3224 {
3225 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3226 enum pipe pipe = crtc->pipe;
3227 struct drm_property_blob *blob;
3228 struct drm_color_lut *lut;
3229 int i;
3230
3231 blob = drm_property_create_blob(&dev_priv->drm,
3232 sizeof(lut[0]) * LEGACY_LUT_LENGTH,
3233 NULL);
3234 if (IS_ERR(blob))
3235 return NULL;
3236
3237 lut = blob->data;
3238
3239 for (i = 0; i < LEGACY_LUT_LENGTH; i++) {
3240 u32 val = intel_de_read_fw(dev_priv,
3241 PALETTE(dev_priv, pipe, i));
3242
3243 i9xx_lut_8_pack(&lut[i], val);
3244 }
3245
3246 return blob;
3247 }
3248
i9xx_read_lut_10(struct intel_crtc * crtc)3249 static struct drm_property_blob *i9xx_read_lut_10(struct intel_crtc *crtc)
3250 {
3251 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3252 u32 lut_size = DISPLAY_INFO(dev_priv)->color.gamma_lut_size;
3253 enum pipe pipe = crtc->pipe;
3254 struct drm_property_blob *blob;
3255 struct drm_color_lut *lut;
3256 u32 ldw, udw;
3257 int i;
3258
3259 blob = drm_property_create_blob(&dev_priv->drm,
3260 lut_size * sizeof(lut[0]), NULL);
3261 if (IS_ERR(blob))
3262 return NULL;
3263
3264 lut = blob->data;
3265
3266 for (i = 0; i < lut_size - 1; i++) {
3267 ldw = intel_de_read_fw(dev_priv,
3268 PALETTE(dev_priv, pipe, 2 * i + 0));
3269 udw = intel_de_read_fw(dev_priv,
3270 PALETTE(dev_priv, pipe, 2 * i + 1));
3271
3272 i9xx_lut_10_pack(&lut[i], ldw, udw);
3273 }
3274
3275 i9xx_lut_10_pack_slope(&lut[i], ldw, udw);
3276
3277 return blob;
3278 }
3279
i9xx_read_luts(struct intel_crtc_state * crtc_state)3280 static void i9xx_read_luts(struct intel_crtc_state *crtc_state)
3281 {
3282 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3283
3284 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3285 return;
3286
3287 switch (crtc_state->gamma_mode) {
3288 case GAMMA_MODE_MODE_8BIT:
3289 crtc_state->post_csc_lut = i9xx_read_lut_8(crtc);
3290 break;
3291 case GAMMA_MODE_MODE_10BIT:
3292 crtc_state->post_csc_lut = i9xx_read_lut_10(crtc);
3293 break;
3294 default:
3295 MISSING_CASE(crtc_state->gamma_mode);
3296 break;
3297 }
3298 }
3299
i965_read_lut_10p6(struct intel_crtc * crtc)3300 static struct drm_property_blob *i965_read_lut_10p6(struct intel_crtc *crtc)
3301 {
3302 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3303 int i, lut_size = DISPLAY_INFO(dev_priv)->color.gamma_lut_size;
3304 enum pipe pipe = crtc->pipe;
3305 struct drm_property_blob *blob;
3306 struct drm_color_lut *lut;
3307
3308 blob = drm_property_create_blob(&dev_priv->drm,
3309 sizeof(lut[0]) * lut_size,
3310 NULL);
3311 if (IS_ERR(blob))
3312 return NULL;
3313
3314 lut = blob->data;
3315
3316 for (i = 0; i < lut_size - 1; i++) {
3317 u32 ldw = intel_de_read_fw(dev_priv,
3318 PALETTE(dev_priv, pipe, 2 * i + 0));
3319 u32 udw = intel_de_read_fw(dev_priv,
3320 PALETTE(dev_priv, pipe, 2 * i + 1));
3321
3322 i965_lut_10p6_pack(&lut[i], ldw, udw);
3323 }
3324
3325 lut[i].red = i965_lut_11p6_max_pack(intel_de_read_fw(dev_priv, PIPEGCMAX(dev_priv, pipe, 0)));
3326 lut[i].green = i965_lut_11p6_max_pack(intel_de_read_fw(dev_priv, PIPEGCMAX(dev_priv, pipe, 1)));
3327 lut[i].blue = i965_lut_11p6_max_pack(intel_de_read_fw(dev_priv, PIPEGCMAX(dev_priv, pipe, 2)));
3328
3329 return blob;
3330 }
3331
i965_read_luts(struct intel_crtc_state * crtc_state)3332 static void i965_read_luts(struct intel_crtc_state *crtc_state)
3333 {
3334 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3335
3336 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3337 return;
3338
3339 switch (crtc_state->gamma_mode) {
3340 case GAMMA_MODE_MODE_8BIT:
3341 crtc_state->post_csc_lut = i9xx_read_lut_8(crtc);
3342 break;
3343 case GAMMA_MODE_MODE_10BIT:
3344 crtc_state->post_csc_lut = i965_read_lut_10p6(crtc);
3345 break;
3346 default:
3347 MISSING_CASE(crtc_state->gamma_mode);
3348 break;
3349 }
3350 }
3351
chv_read_cgm_degamma(struct intel_crtc * crtc)3352 static struct drm_property_blob *chv_read_cgm_degamma(struct intel_crtc *crtc)
3353 {
3354 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3355 int i, lut_size = DISPLAY_INFO(dev_priv)->color.degamma_lut_size;
3356 enum pipe pipe = crtc->pipe;
3357 struct drm_property_blob *blob;
3358 struct drm_color_lut *lut;
3359
3360 blob = drm_property_create_blob(&dev_priv->drm,
3361 sizeof(lut[0]) * lut_size,
3362 NULL);
3363 if (IS_ERR(blob))
3364 return NULL;
3365
3366 lut = blob->data;
3367
3368 for (i = 0; i < lut_size; i++) {
3369 u32 ldw = intel_de_read_fw(dev_priv, CGM_PIPE_DEGAMMA(pipe, i, 0));
3370 u32 udw = intel_de_read_fw(dev_priv, CGM_PIPE_DEGAMMA(pipe, i, 1));
3371
3372 chv_cgm_degamma_pack(&lut[i], ldw, udw);
3373 }
3374
3375 return blob;
3376 }
3377
chv_read_cgm_gamma(struct intel_crtc * crtc)3378 static struct drm_property_blob *chv_read_cgm_gamma(struct intel_crtc *crtc)
3379 {
3380 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3381 int i, lut_size = DISPLAY_INFO(i915)->color.gamma_lut_size;
3382 enum pipe pipe = crtc->pipe;
3383 struct drm_property_blob *blob;
3384 struct drm_color_lut *lut;
3385
3386 blob = drm_property_create_blob(&i915->drm,
3387 sizeof(lut[0]) * lut_size,
3388 NULL);
3389 if (IS_ERR(blob))
3390 return NULL;
3391
3392 lut = blob->data;
3393
3394 for (i = 0; i < lut_size; i++) {
3395 u32 ldw = intel_de_read_fw(i915, CGM_PIPE_GAMMA(pipe, i, 0));
3396 u32 udw = intel_de_read_fw(i915, CGM_PIPE_GAMMA(pipe, i, 1));
3397
3398 chv_cgm_gamma_pack(&lut[i], ldw, udw);
3399 }
3400
3401 return blob;
3402 }
3403
chv_get_config(struct intel_crtc_state * crtc_state)3404 static void chv_get_config(struct intel_crtc_state *crtc_state)
3405 {
3406 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3407 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3408
3409 crtc_state->cgm_mode = intel_de_read(i915, CGM_PIPE_MODE(crtc->pipe));
3410
3411 i9xx_get_config(crtc_state);
3412 }
3413
chv_read_luts(struct intel_crtc_state * crtc_state)3414 static void chv_read_luts(struct intel_crtc_state *crtc_state)
3415 {
3416 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3417
3418 if (crtc_state->cgm_mode & CGM_PIPE_MODE_DEGAMMA)
3419 crtc_state->pre_csc_lut = chv_read_cgm_degamma(crtc);
3420
3421 if (crtc_state->cgm_mode & CGM_PIPE_MODE_GAMMA)
3422 crtc_state->post_csc_lut = chv_read_cgm_gamma(crtc);
3423 else
3424 i965_read_luts(crtc_state);
3425 }
3426
ilk_read_lut_8(struct intel_crtc * crtc)3427 static struct drm_property_blob *ilk_read_lut_8(struct intel_crtc *crtc)
3428 {
3429 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3430 enum pipe pipe = crtc->pipe;
3431 struct drm_property_blob *blob;
3432 struct drm_color_lut *lut;
3433 int i;
3434
3435 blob = drm_property_create_blob(&i915->drm,
3436 sizeof(lut[0]) * LEGACY_LUT_LENGTH,
3437 NULL);
3438 if (IS_ERR(blob))
3439 return NULL;
3440
3441 lut = blob->data;
3442
3443 for (i = 0; i < LEGACY_LUT_LENGTH; i++) {
3444 u32 val = intel_de_read_fw(i915, LGC_PALETTE(pipe, i));
3445
3446 i9xx_lut_8_pack(&lut[i], val);
3447 }
3448
3449 return blob;
3450 }
3451
ilk_read_lut_10(struct intel_crtc * crtc)3452 static struct drm_property_blob *ilk_read_lut_10(struct intel_crtc *crtc)
3453 {
3454 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3455 int i, lut_size = DISPLAY_INFO(i915)->color.gamma_lut_size;
3456 enum pipe pipe = crtc->pipe;
3457 struct drm_property_blob *blob;
3458 struct drm_color_lut *lut;
3459
3460 blob = drm_property_create_blob(&i915->drm,
3461 sizeof(lut[0]) * lut_size,
3462 NULL);
3463 if (IS_ERR(blob))
3464 return NULL;
3465
3466 lut = blob->data;
3467
3468 for (i = 0; i < lut_size; i++) {
3469 u32 val = intel_de_read_fw(i915, PREC_PALETTE(pipe, i));
3470
3471 ilk_lut_10_pack(&lut[i], val);
3472 }
3473
3474 return blob;
3475 }
3476
ilk_get_config(struct intel_crtc_state * crtc_state)3477 static void ilk_get_config(struct intel_crtc_state *crtc_state)
3478 {
3479 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3480
3481 crtc_state->csc_mode = ilk_read_csc_mode(crtc);
3482
3483 i9xx_get_config(crtc_state);
3484 }
3485
ilk_read_luts(struct intel_crtc_state * crtc_state)3486 static void ilk_read_luts(struct intel_crtc_state *crtc_state)
3487 {
3488 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3489 struct drm_property_blob **blob =
3490 ilk_has_post_csc_lut(crtc_state) ?
3491 &crtc_state->post_csc_lut : &crtc_state->pre_csc_lut;
3492
3493 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3494 return;
3495
3496 switch (crtc_state->gamma_mode) {
3497 case GAMMA_MODE_MODE_8BIT:
3498 *blob = ilk_read_lut_8(crtc);
3499 break;
3500 case GAMMA_MODE_MODE_10BIT:
3501 *blob = ilk_read_lut_10(crtc);
3502 break;
3503 default:
3504 MISSING_CASE(crtc_state->gamma_mode);
3505 break;
3506 }
3507 }
3508
3509 /*
3510 * IVB/HSW Bspec / PAL_PREC_INDEX:
3511 * "Restriction : Index auto increment mode is not
3512 * supported and must not be enabled."
3513 */
ivb_read_lut_10(struct intel_crtc * crtc,u32 prec_index)3514 static struct drm_property_blob *ivb_read_lut_10(struct intel_crtc *crtc,
3515 u32 prec_index)
3516 {
3517 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3518 int i, lut_size = ivb_lut_10_size(prec_index);
3519 enum pipe pipe = crtc->pipe;
3520 struct drm_property_blob *blob;
3521 struct drm_color_lut *lut;
3522
3523 blob = drm_property_create_blob(&dev_priv->drm,
3524 sizeof(lut[0]) * lut_size,
3525 NULL);
3526 if (IS_ERR(blob))
3527 return NULL;
3528
3529 lut = blob->data;
3530
3531 for (i = 0; i < lut_size; i++) {
3532 u32 val;
3533
3534 intel_de_write_fw(dev_priv, PREC_PAL_INDEX(pipe),
3535 prec_index + i);
3536 val = intel_de_read_fw(dev_priv, PREC_PAL_DATA(pipe));
3537
3538 ilk_lut_10_pack(&lut[i], val);
3539 }
3540
3541 intel_de_write_fw(dev_priv, PREC_PAL_INDEX(pipe),
3542 PAL_PREC_INDEX_VALUE(0));
3543
3544 return blob;
3545 }
3546
ivb_read_luts(struct intel_crtc_state * crtc_state)3547 static void ivb_read_luts(struct intel_crtc_state *crtc_state)
3548 {
3549 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3550 struct drm_property_blob **blob =
3551 ilk_has_post_csc_lut(crtc_state) ?
3552 &crtc_state->post_csc_lut : &crtc_state->pre_csc_lut;
3553
3554 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3555 return;
3556
3557 switch (crtc_state->gamma_mode) {
3558 case GAMMA_MODE_MODE_8BIT:
3559 *blob = ilk_read_lut_8(crtc);
3560 break;
3561 case GAMMA_MODE_MODE_SPLIT:
3562 crtc_state->pre_csc_lut =
3563 ivb_read_lut_10(crtc, PAL_PREC_SPLIT_MODE |
3564 PAL_PREC_INDEX_VALUE(0));
3565 crtc_state->post_csc_lut =
3566 ivb_read_lut_10(crtc, PAL_PREC_SPLIT_MODE |
3567 PAL_PREC_INDEX_VALUE(512));
3568 break;
3569 case GAMMA_MODE_MODE_10BIT:
3570 *blob = ivb_read_lut_10(crtc, PAL_PREC_INDEX_VALUE(0));
3571 break;
3572 default:
3573 MISSING_CASE(crtc_state->gamma_mode);
3574 break;
3575 }
3576 }
3577
3578 /* On BDW+ the index auto increment mode actually works */
bdw_read_lut_10(struct intel_crtc * crtc,u32 prec_index)3579 static struct drm_property_blob *bdw_read_lut_10(struct intel_crtc *crtc,
3580 u32 prec_index)
3581 {
3582 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3583 int i, lut_size = ivb_lut_10_size(prec_index);
3584 enum pipe pipe = crtc->pipe;
3585 struct drm_property_blob *blob;
3586 struct drm_color_lut *lut;
3587
3588 blob = drm_property_create_blob(&i915->drm,
3589 sizeof(lut[0]) * lut_size,
3590 NULL);
3591 if (IS_ERR(blob))
3592 return NULL;
3593
3594 lut = blob->data;
3595
3596 intel_de_write_fw(i915, PREC_PAL_INDEX(pipe),
3597 prec_index);
3598 intel_de_write_fw(i915, PREC_PAL_INDEX(pipe),
3599 PAL_PREC_AUTO_INCREMENT |
3600 prec_index);
3601
3602 for (i = 0; i < lut_size; i++) {
3603 u32 val = intel_de_read_fw(i915, PREC_PAL_DATA(pipe));
3604
3605 ilk_lut_10_pack(&lut[i], val);
3606 }
3607
3608 intel_de_write_fw(i915, PREC_PAL_INDEX(pipe),
3609 PAL_PREC_INDEX_VALUE(0));
3610
3611 return blob;
3612 }
3613
bdw_read_luts(struct intel_crtc_state * crtc_state)3614 static void bdw_read_luts(struct intel_crtc_state *crtc_state)
3615 {
3616 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3617 struct drm_property_blob **blob =
3618 ilk_has_post_csc_lut(crtc_state) ?
3619 &crtc_state->post_csc_lut : &crtc_state->pre_csc_lut;
3620
3621 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3622 return;
3623
3624 switch (crtc_state->gamma_mode) {
3625 case GAMMA_MODE_MODE_8BIT:
3626 *blob = ilk_read_lut_8(crtc);
3627 break;
3628 case GAMMA_MODE_MODE_SPLIT:
3629 crtc_state->pre_csc_lut =
3630 bdw_read_lut_10(crtc, PAL_PREC_SPLIT_MODE |
3631 PAL_PREC_INDEX_VALUE(0));
3632 crtc_state->post_csc_lut =
3633 bdw_read_lut_10(crtc, PAL_PREC_SPLIT_MODE |
3634 PAL_PREC_INDEX_VALUE(512));
3635 break;
3636 case GAMMA_MODE_MODE_10BIT:
3637 *blob = bdw_read_lut_10(crtc, PAL_PREC_INDEX_VALUE(0));
3638 break;
3639 default:
3640 MISSING_CASE(crtc_state->gamma_mode);
3641 break;
3642 }
3643 }
3644
glk_read_degamma_lut(struct intel_crtc * crtc)3645 static struct drm_property_blob *glk_read_degamma_lut(struct intel_crtc *crtc)
3646 {
3647 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3648 int i, lut_size = DISPLAY_INFO(dev_priv)->color.degamma_lut_size;
3649 enum pipe pipe = crtc->pipe;
3650 struct drm_property_blob *blob;
3651 struct drm_color_lut *lut;
3652
3653 blob = drm_property_create_blob(&dev_priv->drm,
3654 sizeof(lut[0]) * lut_size,
3655 NULL);
3656 if (IS_ERR(blob))
3657 return NULL;
3658
3659 lut = blob->data;
3660
3661 /*
3662 * When setting the auto-increment bit, the hardware seems to
3663 * ignore the index bits, so we need to reset it to index 0
3664 * separately.
3665 */
3666 intel_de_write_fw(dev_priv, PRE_CSC_GAMC_INDEX(pipe),
3667 PRE_CSC_GAMC_INDEX_VALUE(0));
3668 intel_de_write_fw(dev_priv, PRE_CSC_GAMC_INDEX(pipe),
3669 PRE_CSC_GAMC_AUTO_INCREMENT |
3670 PRE_CSC_GAMC_INDEX_VALUE(0));
3671
3672 for (i = 0; i < lut_size; i++) {
3673 u32 val = intel_de_read_fw(dev_priv, PRE_CSC_GAMC_DATA(pipe));
3674
3675 if (DISPLAY_VER(dev_priv) >= 14)
3676 mtl_degamma_lut_pack(&lut[i], val);
3677 else
3678 glk_degamma_lut_pack(&lut[i], val);
3679 }
3680
3681 intel_de_write_fw(dev_priv, PRE_CSC_GAMC_INDEX(pipe),
3682 PRE_CSC_GAMC_INDEX_VALUE(0));
3683
3684 return blob;
3685 }
3686
glk_read_luts(struct intel_crtc_state * crtc_state)3687 static void glk_read_luts(struct intel_crtc_state *crtc_state)
3688 {
3689 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3690
3691 if (crtc_state->csc_enable)
3692 crtc_state->pre_csc_lut = glk_read_degamma_lut(crtc);
3693
3694 if (!crtc_state->gamma_enable && !crtc_state->c8_planes)
3695 return;
3696
3697 switch (crtc_state->gamma_mode) {
3698 case GAMMA_MODE_MODE_8BIT:
3699 crtc_state->post_csc_lut = ilk_read_lut_8(crtc);
3700 break;
3701 case GAMMA_MODE_MODE_10BIT:
3702 crtc_state->post_csc_lut = bdw_read_lut_10(crtc, PAL_PREC_INDEX_VALUE(0));
3703 break;
3704 default:
3705 MISSING_CASE(crtc_state->gamma_mode);
3706 break;
3707 }
3708 }
3709
3710 static struct drm_property_blob *
icl_read_lut_multi_segment(struct intel_crtc * crtc)3711 icl_read_lut_multi_segment(struct intel_crtc *crtc)
3712 {
3713 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3714 int i, lut_size = DISPLAY_INFO(i915)->color.gamma_lut_size;
3715 enum pipe pipe = crtc->pipe;
3716 struct drm_property_blob *blob;
3717 struct drm_color_lut *lut;
3718
3719 blob = drm_property_create_blob(&i915->drm,
3720 sizeof(lut[0]) * lut_size,
3721 NULL);
3722 if (IS_ERR(blob))
3723 return NULL;
3724
3725 lut = blob->data;
3726
3727 intel_de_write_fw(i915, PREC_PAL_MULTI_SEG_INDEX(pipe),
3728 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
3729 intel_de_write_fw(i915, PREC_PAL_MULTI_SEG_INDEX(pipe),
3730 PAL_PREC_MULTI_SEG_AUTO_INCREMENT |
3731 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
3732
3733 for (i = 0; i < 9; i++) {
3734 u32 ldw = intel_de_read_fw(i915, PREC_PAL_MULTI_SEG_DATA(pipe));
3735 u32 udw = intel_de_read_fw(i915, PREC_PAL_MULTI_SEG_DATA(pipe));
3736
3737 ilk_lut_12p4_pack(&lut[i], ldw, udw);
3738 }
3739
3740 intel_de_write_fw(i915, PREC_PAL_MULTI_SEG_INDEX(pipe),
3741 PAL_PREC_MULTI_SEG_INDEX_VALUE(0));
3742
3743 /*
3744 * FIXME readouts from PAL_PREC_DATA register aren't giving
3745 * correct values in the case of fine and coarse segments.
3746 * Restricting readouts only for super fine segment as of now.
3747 */
3748
3749 return blob;
3750 }
3751
icl_read_luts(struct intel_crtc_state * crtc_state)3752 static void icl_read_luts(struct intel_crtc_state *crtc_state)
3753 {
3754 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3755
3756 if (icl_has_pre_csc_lut(crtc_state))
3757 crtc_state->pre_csc_lut = glk_read_degamma_lut(crtc);
3758
3759 if (!icl_has_post_csc_lut(crtc_state))
3760 return;
3761
3762 switch (crtc_state->gamma_mode & GAMMA_MODE_MODE_MASK) {
3763 case GAMMA_MODE_MODE_8BIT:
3764 crtc_state->post_csc_lut = ilk_read_lut_8(crtc);
3765 break;
3766 case GAMMA_MODE_MODE_10BIT:
3767 crtc_state->post_csc_lut = bdw_read_lut_10(crtc, PAL_PREC_INDEX_VALUE(0));
3768 break;
3769 case GAMMA_MODE_MODE_12BIT_MULTI_SEG:
3770 crtc_state->post_csc_lut = icl_read_lut_multi_segment(crtc);
3771 break;
3772 default:
3773 MISSING_CASE(crtc_state->gamma_mode);
3774 break;
3775 }
3776 }
3777
3778 static const struct intel_color_funcs chv_color_funcs = {
3779 .color_check = chv_color_check,
3780 .color_commit_arm = i9xx_color_commit_arm,
3781 .load_luts = chv_load_luts,
3782 .read_luts = chv_read_luts,
3783 .lut_equal = chv_lut_equal,
3784 .read_csc = chv_read_csc,
3785 .get_config = chv_get_config,
3786 };
3787
3788 static const struct intel_color_funcs vlv_color_funcs = {
3789 .color_check = vlv_color_check,
3790 .color_commit_arm = i9xx_color_commit_arm,
3791 .load_luts = vlv_load_luts,
3792 .read_luts = i965_read_luts,
3793 .lut_equal = i965_lut_equal,
3794 .read_csc = vlv_read_csc,
3795 .get_config = i9xx_get_config,
3796 };
3797
3798 static const struct intel_color_funcs i965_color_funcs = {
3799 .color_check = i9xx_color_check,
3800 .color_commit_arm = i9xx_color_commit_arm,
3801 .load_luts = i965_load_luts,
3802 .read_luts = i965_read_luts,
3803 .lut_equal = i965_lut_equal,
3804 .get_config = i9xx_get_config,
3805 };
3806
3807 static const struct intel_color_funcs i9xx_color_funcs = {
3808 .color_check = i9xx_color_check,
3809 .color_commit_arm = i9xx_color_commit_arm,
3810 .load_luts = i9xx_load_luts,
3811 .read_luts = i9xx_read_luts,
3812 .lut_equal = i9xx_lut_equal,
3813 .get_config = i9xx_get_config,
3814 };
3815
3816 static const struct intel_color_funcs tgl_color_funcs = {
3817 .color_check = icl_color_check,
3818 .color_commit_noarm = icl_color_commit_noarm,
3819 .color_commit_arm = icl_color_commit_arm,
3820 .load_luts = icl_load_luts,
3821 .read_luts = icl_read_luts,
3822 .lut_equal = icl_lut_equal,
3823 .read_csc = icl_read_csc,
3824 .get_config = skl_get_config,
3825 };
3826
3827 static const struct intel_color_funcs icl_color_funcs = {
3828 .color_check = icl_color_check,
3829 .color_commit_noarm = icl_color_commit_noarm,
3830 .color_commit_arm = icl_color_commit_arm,
3831 .color_post_update = icl_color_post_update,
3832 .load_luts = icl_load_luts,
3833 .read_luts = icl_read_luts,
3834 .lut_equal = icl_lut_equal,
3835 .read_csc = icl_read_csc,
3836 .get_config = skl_get_config,
3837 };
3838
3839 static const struct intel_color_funcs glk_color_funcs = {
3840 .color_check = glk_color_check,
3841 .color_commit_noarm = skl_color_commit_noarm,
3842 .color_commit_arm = skl_color_commit_arm,
3843 .load_luts = glk_load_luts,
3844 .read_luts = glk_read_luts,
3845 .lut_equal = glk_lut_equal,
3846 .read_csc = skl_read_csc,
3847 .get_config = skl_get_config,
3848 };
3849
3850 static const struct intel_color_funcs skl_color_funcs = {
3851 .color_check = ivb_color_check,
3852 .color_commit_noarm = skl_color_commit_noarm,
3853 .color_commit_arm = skl_color_commit_arm,
3854 .load_luts = bdw_load_luts,
3855 .read_luts = bdw_read_luts,
3856 .lut_equal = ivb_lut_equal,
3857 .read_csc = skl_read_csc,
3858 .get_config = skl_get_config,
3859 };
3860
3861 static const struct intel_color_funcs bdw_color_funcs = {
3862 .color_check = ivb_color_check,
3863 .color_commit_noarm = ilk_color_commit_noarm,
3864 .color_commit_arm = hsw_color_commit_arm,
3865 .load_luts = bdw_load_luts,
3866 .read_luts = bdw_read_luts,
3867 .lut_equal = ivb_lut_equal,
3868 .read_csc = ilk_read_csc,
3869 .get_config = hsw_get_config,
3870 };
3871
3872 static const struct intel_color_funcs hsw_color_funcs = {
3873 .color_check = ivb_color_check,
3874 .color_commit_noarm = ilk_color_commit_noarm,
3875 .color_commit_arm = hsw_color_commit_arm,
3876 .load_luts = ivb_load_luts,
3877 .read_luts = ivb_read_luts,
3878 .lut_equal = ivb_lut_equal,
3879 .read_csc = ilk_read_csc,
3880 .get_config = hsw_get_config,
3881 };
3882
3883 static const struct intel_color_funcs ivb_color_funcs = {
3884 .color_check = ivb_color_check,
3885 .color_commit_noarm = ilk_color_commit_noarm,
3886 .color_commit_arm = ilk_color_commit_arm,
3887 .load_luts = ivb_load_luts,
3888 .read_luts = ivb_read_luts,
3889 .lut_equal = ivb_lut_equal,
3890 .read_csc = ilk_read_csc,
3891 .get_config = ilk_get_config,
3892 };
3893
3894 static const struct intel_color_funcs ilk_color_funcs = {
3895 .color_check = ilk_color_check,
3896 .color_commit_noarm = ilk_color_commit_noarm,
3897 .color_commit_arm = ilk_color_commit_arm,
3898 .load_luts = ilk_load_luts,
3899 .read_luts = ilk_read_luts,
3900 .lut_equal = ilk_lut_equal,
3901 .read_csc = ilk_read_csc,
3902 .get_config = ilk_get_config,
3903 };
3904
intel_color_crtc_init(struct intel_crtc * crtc)3905 void intel_color_crtc_init(struct intel_crtc *crtc)
3906 {
3907 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3908 int degamma_lut_size, gamma_lut_size;
3909 bool has_ctm;
3910
3911 drm_mode_crtc_set_gamma_size(&crtc->base, 256);
3912
3913 gamma_lut_size = DISPLAY_INFO(i915)->color.gamma_lut_size;
3914 degamma_lut_size = DISPLAY_INFO(i915)->color.degamma_lut_size;
3915 has_ctm = DISPLAY_VER(i915) >= 5;
3916
3917 /*
3918 * "DPALETTE_A: NOTE: The 8-bit (non-10-bit) mode is the
3919 * only mode supported by Alviso and Grantsdale."
3920 *
3921 * Actually looks like this affects all of gen3.
3922 * Confirmed on alv,cst,pnv. Mobile gen2 parts (alm,mgm)
3923 * are confirmed not to suffer from this restriction.
3924 */
3925 if (DISPLAY_VER(i915) == 3 && crtc->pipe == PIPE_A)
3926 gamma_lut_size = 256;
3927
3928 drm_crtc_enable_color_mgmt(&crtc->base, degamma_lut_size,
3929 has_ctm, gamma_lut_size);
3930 }
3931
intel_color_init(struct drm_i915_private * i915)3932 int intel_color_init(struct drm_i915_private *i915)
3933 {
3934 struct drm_property_blob *blob;
3935
3936 if (DISPLAY_VER(i915) != 10)
3937 return 0;
3938
3939 blob = create_linear_lut(i915,
3940 DISPLAY_INFO(i915)->color.degamma_lut_size);
3941 if (IS_ERR(blob))
3942 return PTR_ERR(blob);
3943
3944 i915->display.color.glk_linear_degamma_lut = blob;
3945
3946 return 0;
3947 }
3948
intel_color_init_hooks(struct drm_i915_private * i915)3949 void intel_color_init_hooks(struct drm_i915_private *i915)
3950 {
3951 if (HAS_GMCH(i915)) {
3952 if (IS_CHERRYVIEW(i915))
3953 i915->display.funcs.color = &chv_color_funcs;
3954 else if (IS_VALLEYVIEW(i915))
3955 i915->display.funcs.color = &vlv_color_funcs;
3956 else if (DISPLAY_VER(i915) >= 4)
3957 i915->display.funcs.color = &i965_color_funcs;
3958 else
3959 i915->display.funcs.color = &i9xx_color_funcs;
3960 } else {
3961 if (DISPLAY_VER(i915) >= 12)
3962 i915->display.funcs.color = &tgl_color_funcs;
3963 else if (DISPLAY_VER(i915) == 11)
3964 i915->display.funcs.color = &icl_color_funcs;
3965 else if (DISPLAY_VER(i915) == 10)
3966 i915->display.funcs.color = &glk_color_funcs;
3967 else if (DISPLAY_VER(i915) == 9)
3968 i915->display.funcs.color = &skl_color_funcs;
3969 else if (DISPLAY_VER(i915) == 8)
3970 i915->display.funcs.color = &bdw_color_funcs;
3971 else if (IS_HASWELL(i915))
3972 i915->display.funcs.color = &hsw_color_funcs;
3973 else if (DISPLAY_VER(i915) == 7)
3974 i915->display.funcs.color = &ivb_color_funcs;
3975 else
3976 i915->display.funcs.color = &ilk_color_funcs;
3977 }
3978 }
3979